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 int isl_map_find_dim_by_name(__isl_keep isl_map
*map
, enum isl_dim_type type
,
723 return isl_space_find_dim_by_name(map
->dim
, type
, name
);
726 int isl_set_find_dim_by_name(__isl_keep isl_set
*set
, enum isl_dim_type type
,
729 return isl_map_find_dim_by_name(set
, type
, name
);
732 /* Reset the user pointer on all identifiers of parameters and tuples
733 * of the space of "map".
735 __isl_give isl_map
*isl_map_reset_user(__isl_take isl_map
*map
)
739 space
= isl_map_get_space(map
);
740 space
= isl_space_reset_user(space
);
741 map
= isl_map_reset_space(map
, space
);
746 /* Reset the user pointer on all identifiers of parameters and tuples
747 * of the space of "set".
749 __isl_give isl_set
*isl_set_reset_user(__isl_take isl_set
*set
)
751 return isl_map_reset_user(set
);
754 int isl_basic_map_is_rational(__isl_keep isl_basic_map
*bmap
)
758 return ISL_F_ISSET(bmap
, ISL_BASIC_MAP_RATIONAL
);
761 int isl_basic_set_is_rational(__isl_keep isl_basic_set
*bset
)
763 return isl_basic_map_is_rational(bset
);
766 /* Does "bmap" contain any rational points?
768 * If "bmap" has an equality for each dimension, equating the dimension
769 * to an integer constant, then it has no rational points, even if it
770 * is marked as rational.
772 int isl_basic_map_has_rational(__isl_keep isl_basic_map
*bmap
)
774 int has_rational
= 1;
779 if (isl_basic_map_plain_is_empty(bmap
))
781 if (!isl_basic_map_is_rational(bmap
))
783 bmap
= isl_basic_map_copy(bmap
);
784 bmap
= isl_basic_map_implicit_equalities(bmap
);
787 total
= isl_basic_map_total_dim(bmap
);
788 if (bmap
->n_eq
== total
) {
790 for (i
= 0; i
< bmap
->n_eq
; ++i
) {
791 j
= isl_seq_first_non_zero(bmap
->eq
[i
] + 1, total
);
794 if (!isl_int_is_one(bmap
->eq
[i
][1 + j
]) &&
795 !isl_int_is_negone(bmap
->eq
[i
][1 + j
]))
797 j
= isl_seq_first_non_zero(bmap
->eq
[i
] + 1 + j
+ 1,
805 isl_basic_map_free(bmap
);
810 /* Does "map" contain any rational points?
812 int isl_map_has_rational(__isl_keep isl_map
*map
)
819 for (i
= 0; i
< map
->n
; ++i
) {
820 has_rational
= isl_basic_map_has_rational(map
->p
[i
]);
821 if (has_rational
< 0)
829 /* Does "set" contain any rational points?
831 int isl_set_has_rational(__isl_keep isl_set
*set
)
833 return isl_map_has_rational(set
);
836 /* Is this basic set a parameter domain?
838 int isl_basic_set_is_params(__isl_keep isl_basic_set
*bset
)
842 return isl_space_is_params(bset
->dim
);
845 /* Is this set a parameter domain?
847 int isl_set_is_params(__isl_keep isl_set
*set
)
851 return isl_space_is_params(set
->dim
);
854 /* Is this map actually a parameter domain?
855 * Users should never call this function. Outside of isl,
856 * a map can never be a parameter domain.
858 int isl_map_is_params(__isl_keep isl_map
*map
)
862 return isl_space_is_params(map
->dim
);
865 static struct isl_basic_map
*basic_map_init(struct isl_ctx
*ctx
,
866 struct isl_basic_map
*bmap
, unsigned extra
,
867 unsigned n_eq
, unsigned n_ineq
)
870 size_t row_size
= 1 + isl_space_dim(bmap
->dim
, isl_dim_all
) + extra
;
875 bmap
->block
= isl_blk_alloc(ctx
, (n_ineq
+ n_eq
) * row_size
);
876 if (isl_blk_is_error(bmap
->block
))
879 bmap
->ineq
= isl_alloc_array(ctx
, isl_int
*, n_ineq
+ n_eq
);
880 if ((n_ineq
+ n_eq
) && !bmap
->ineq
)
884 bmap
->block2
= isl_blk_empty();
887 bmap
->block2
= isl_blk_alloc(ctx
, extra
* (1 + row_size
));
888 if (isl_blk_is_error(bmap
->block2
))
891 bmap
->div
= isl_alloc_array(ctx
, isl_int
*, extra
);
896 for (i
= 0; i
< n_ineq
+ n_eq
; ++i
)
897 bmap
->ineq
[i
] = bmap
->block
.data
+ i
* row_size
;
899 for (i
= 0; i
< extra
; ++i
)
900 bmap
->div
[i
] = bmap
->block2
.data
+ i
* (1 + row_size
);
904 bmap
->c_size
= n_eq
+ n_ineq
;
905 bmap
->eq
= bmap
->ineq
+ n_ineq
;
914 isl_basic_map_free(bmap
);
918 struct isl_basic_set
*isl_basic_set_alloc(struct isl_ctx
*ctx
,
919 unsigned nparam
, unsigned dim
, unsigned extra
,
920 unsigned n_eq
, unsigned n_ineq
)
922 struct isl_basic_map
*bmap
;
925 space
= isl_space_set_alloc(ctx
, nparam
, dim
);
929 bmap
= isl_basic_map_alloc_space(space
, extra
, n_eq
, n_ineq
);
930 return (struct isl_basic_set
*)bmap
;
933 struct isl_basic_set
*isl_basic_set_alloc_space(__isl_take isl_space
*dim
,
934 unsigned extra
, unsigned n_eq
, unsigned n_ineq
)
936 struct isl_basic_map
*bmap
;
939 isl_assert(dim
->ctx
, dim
->n_in
== 0, goto error
);
940 bmap
= isl_basic_map_alloc_space(dim
, extra
, n_eq
, n_ineq
);
941 return (struct isl_basic_set
*)bmap
;
947 struct isl_basic_map
*isl_basic_map_alloc_space(__isl_take isl_space
*dim
,
948 unsigned extra
, unsigned n_eq
, unsigned n_ineq
)
950 struct isl_basic_map
*bmap
;
954 bmap
= isl_calloc_type(dim
->ctx
, struct isl_basic_map
);
959 return basic_map_init(dim
->ctx
, bmap
, extra
, n_eq
, n_ineq
);
965 struct isl_basic_map
*isl_basic_map_alloc(struct isl_ctx
*ctx
,
966 unsigned nparam
, unsigned in
, unsigned out
, unsigned extra
,
967 unsigned n_eq
, unsigned n_ineq
)
969 struct isl_basic_map
*bmap
;
972 dim
= isl_space_alloc(ctx
, nparam
, in
, out
);
976 bmap
= isl_basic_map_alloc_space(dim
, extra
, n_eq
, n_ineq
);
980 static void dup_constraints(
981 struct isl_basic_map
*dst
, struct isl_basic_map
*src
)
984 unsigned total
= isl_basic_map_total_dim(src
);
986 for (i
= 0; i
< src
->n_eq
; ++i
) {
987 int j
= isl_basic_map_alloc_equality(dst
);
988 isl_seq_cpy(dst
->eq
[j
], src
->eq
[i
], 1+total
);
991 for (i
= 0; i
< src
->n_ineq
; ++i
) {
992 int j
= isl_basic_map_alloc_inequality(dst
);
993 isl_seq_cpy(dst
->ineq
[j
], src
->ineq
[i
], 1+total
);
996 for (i
= 0; i
< src
->n_div
; ++i
) {
997 int j
= isl_basic_map_alloc_div(dst
);
998 isl_seq_cpy(dst
->div
[j
], src
->div
[i
], 1+1+total
);
1000 ISL_F_SET(dst
, ISL_BASIC_SET_FINAL
);
1003 struct isl_basic_map
*isl_basic_map_dup(struct isl_basic_map
*bmap
)
1005 struct isl_basic_map
*dup
;
1009 dup
= isl_basic_map_alloc_space(isl_space_copy(bmap
->dim
),
1010 bmap
->n_div
, bmap
->n_eq
, bmap
->n_ineq
);
1013 dup_constraints(dup
, bmap
);
1014 dup
->flags
= bmap
->flags
;
1015 dup
->sample
= isl_vec_copy(bmap
->sample
);
1019 struct isl_basic_set
*isl_basic_set_dup(struct isl_basic_set
*bset
)
1021 struct isl_basic_map
*dup
;
1023 dup
= isl_basic_map_dup((struct isl_basic_map
*)bset
);
1024 return (struct isl_basic_set
*)dup
;
1027 struct isl_basic_set
*isl_basic_set_copy(struct isl_basic_set
*bset
)
1032 if (ISL_F_ISSET(bset
, ISL_BASIC_SET_FINAL
)) {
1036 return isl_basic_set_dup(bset
);
1039 struct isl_set
*isl_set_copy(struct isl_set
*set
)
1048 struct isl_basic_map
*isl_basic_map_copy(struct isl_basic_map
*bmap
)
1053 if (ISL_F_ISSET(bmap
, ISL_BASIC_SET_FINAL
)) {
1057 bmap
= isl_basic_map_dup(bmap
);
1059 ISL_F_SET(bmap
, ISL_BASIC_SET_FINAL
);
1063 struct isl_map
*isl_map_copy(struct isl_map
*map
)
1072 __isl_null isl_basic_map
*isl_basic_map_free(__isl_take isl_basic_map
*bmap
)
1077 if (--bmap
->ref
> 0)
1080 isl_ctx_deref(bmap
->ctx
);
1082 isl_blk_free(bmap
->ctx
, bmap
->block2
);
1084 isl_blk_free(bmap
->ctx
, bmap
->block
);
1085 isl_vec_free(bmap
->sample
);
1086 isl_space_free(bmap
->dim
);
1092 __isl_null isl_basic_set
*isl_basic_set_free(__isl_take isl_basic_set
*bset
)
1094 return isl_basic_map_free((struct isl_basic_map
*)bset
);
1097 static int room_for_con(struct isl_basic_map
*bmap
, unsigned n
)
1099 return bmap
->n_eq
+ bmap
->n_ineq
+ n
<= bmap
->c_size
;
1102 __isl_give isl_map
*isl_map_align_params_map_map_and(
1103 __isl_take isl_map
*map1
, __isl_take isl_map
*map2
,
1104 __isl_give isl_map
*(*fn
)(__isl_take isl_map
*map1
,
1105 __isl_take isl_map
*map2
))
1109 if (isl_space_match(map1
->dim
, isl_dim_param
, map2
->dim
, isl_dim_param
))
1110 return fn(map1
, map2
);
1111 if (!isl_space_has_named_params(map1
->dim
) ||
1112 !isl_space_has_named_params(map2
->dim
))
1113 isl_die(map1
->ctx
, isl_error_invalid
,
1114 "unaligned unnamed parameters", goto error
);
1115 map1
= isl_map_align_params(map1
, isl_map_get_space(map2
));
1116 map2
= isl_map_align_params(map2
, isl_map_get_space(map1
));
1117 return fn(map1
, map2
);
1124 int isl_map_align_params_map_map_and_test(__isl_keep isl_map
*map1
,
1125 __isl_keep isl_map
*map2
,
1126 int (*fn
)(__isl_keep isl_map
*map1
, __isl_keep isl_map
*map2
))
1132 if (isl_space_match(map1
->dim
, isl_dim_param
, map2
->dim
, isl_dim_param
))
1133 return fn(map1
, map2
);
1134 if (!isl_space_has_named_params(map1
->dim
) ||
1135 !isl_space_has_named_params(map2
->dim
))
1136 isl_die(map1
->ctx
, isl_error_invalid
,
1137 "unaligned unnamed parameters", return -1);
1138 map1
= isl_map_copy(map1
);
1139 map2
= isl_map_copy(map2
);
1140 map1
= isl_map_align_params(map1
, isl_map_get_space(map2
));
1141 map2
= isl_map_align_params(map2
, isl_map_get_space(map1
));
1148 int isl_basic_map_alloc_equality(struct isl_basic_map
*bmap
)
1150 struct isl_ctx
*ctx
;
1154 isl_assert(ctx
, room_for_con(bmap
, 1), return -1);
1155 isl_assert(ctx
, (bmap
->eq
- bmap
->ineq
) + bmap
->n_eq
<= bmap
->c_size
,
1157 ISL_F_CLR(bmap
, ISL_BASIC_MAP_NORMALIZED
);
1158 ISL_F_CLR(bmap
, ISL_BASIC_MAP_NO_REDUNDANT
);
1159 ISL_F_CLR(bmap
, ISL_BASIC_MAP_NO_IMPLICIT
);
1160 ISL_F_CLR(bmap
, ISL_BASIC_MAP_ALL_EQUALITIES
);
1161 ISL_F_CLR(bmap
, ISL_BASIC_MAP_NORMALIZED_DIVS
);
1162 if ((bmap
->eq
- bmap
->ineq
) + bmap
->n_eq
== bmap
->c_size
) {
1164 int j
= isl_basic_map_alloc_inequality(bmap
);
1168 bmap
->ineq
[j
] = bmap
->ineq
[bmap
->n_ineq
- 1];
1169 bmap
->ineq
[bmap
->n_ineq
- 1] = bmap
->eq
[-1];
1176 isl_seq_clr(bmap
->eq
[bmap
->n_eq
] + 1 + isl_basic_map_total_dim(bmap
),
1177 bmap
->extra
- bmap
->n_div
);
1178 return bmap
->n_eq
++;
1181 int isl_basic_set_alloc_equality(struct isl_basic_set
*bset
)
1183 return isl_basic_map_alloc_equality((struct isl_basic_map
*)bset
);
1186 int isl_basic_map_free_equality(struct isl_basic_map
*bmap
, unsigned n
)
1190 isl_assert(bmap
->ctx
, n
<= bmap
->n_eq
, return -1);
1195 int isl_basic_set_free_equality(struct isl_basic_set
*bset
, unsigned n
)
1197 return isl_basic_map_free_equality((struct isl_basic_map
*)bset
, n
);
1200 int isl_basic_map_drop_equality(struct isl_basic_map
*bmap
, unsigned pos
)
1205 isl_assert(bmap
->ctx
, pos
< bmap
->n_eq
, return -1);
1207 if (pos
!= bmap
->n_eq
- 1) {
1209 bmap
->eq
[pos
] = bmap
->eq
[bmap
->n_eq
- 1];
1210 bmap
->eq
[bmap
->n_eq
- 1] = t
;
1216 int isl_basic_set_drop_equality(struct isl_basic_set
*bset
, unsigned pos
)
1218 return isl_basic_map_drop_equality((struct isl_basic_map
*)bset
, pos
);
1221 /* Turn inequality "pos" of "bmap" into an equality.
1223 * In particular, we move the inequality in front of the equalities
1224 * and move the last inequality in the position of the moved inequality.
1225 * Note that isl_tab_make_equalities_explicit depends on this particular
1226 * change in the ordering of the constraints.
1228 void isl_basic_map_inequality_to_equality(
1229 struct isl_basic_map
*bmap
, unsigned pos
)
1233 t
= bmap
->ineq
[pos
];
1234 bmap
->ineq
[pos
] = bmap
->ineq
[bmap
->n_ineq
- 1];
1235 bmap
->ineq
[bmap
->n_ineq
- 1] = bmap
->eq
[-1];
1240 ISL_F_CLR(bmap
, ISL_BASIC_MAP_NO_REDUNDANT
);
1241 ISL_F_CLR(bmap
, ISL_BASIC_MAP_NORMALIZED
);
1242 ISL_F_CLR(bmap
, ISL_BASIC_MAP_NORMALIZED_DIVS
);
1243 ISL_F_CLR(bmap
, ISL_BASIC_MAP_ALL_EQUALITIES
);
1246 static int room_for_ineq(struct isl_basic_map
*bmap
, unsigned n
)
1248 return bmap
->n_ineq
+ n
<= bmap
->eq
- bmap
->ineq
;
1251 int isl_basic_map_alloc_inequality(struct isl_basic_map
*bmap
)
1253 struct isl_ctx
*ctx
;
1257 isl_assert(ctx
, room_for_ineq(bmap
, 1), return -1);
1258 ISL_F_CLR(bmap
, ISL_BASIC_MAP_NO_IMPLICIT
);
1259 ISL_F_CLR(bmap
, ISL_BASIC_MAP_NO_REDUNDANT
);
1260 ISL_F_CLR(bmap
, ISL_BASIC_MAP_NORMALIZED
);
1261 ISL_F_CLR(bmap
, ISL_BASIC_MAP_ALL_EQUALITIES
);
1262 isl_seq_clr(bmap
->ineq
[bmap
->n_ineq
] +
1263 1 + isl_basic_map_total_dim(bmap
),
1264 bmap
->extra
- bmap
->n_div
);
1265 return bmap
->n_ineq
++;
1268 int isl_basic_set_alloc_inequality(struct isl_basic_set
*bset
)
1270 return isl_basic_map_alloc_inequality((struct isl_basic_map
*)bset
);
1273 int isl_basic_map_free_inequality(struct isl_basic_map
*bmap
, unsigned n
)
1277 isl_assert(bmap
->ctx
, n
<= bmap
->n_ineq
, return -1);
1282 int isl_basic_set_free_inequality(struct isl_basic_set
*bset
, unsigned n
)
1284 return isl_basic_map_free_inequality((struct isl_basic_map
*)bset
, n
);
1287 int isl_basic_map_drop_inequality(struct isl_basic_map
*bmap
, unsigned pos
)
1292 isl_assert(bmap
->ctx
, pos
< bmap
->n_ineq
, return -1);
1294 if (pos
!= bmap
->n_ineq
- 1) {
1295 t
= bmap
->ineq
[pos
];
1296 bmap
->ineq
[pos
] = bmap
->ineq
[bmap
->n_ineq
- 1];
1297 bmap
->ineq
[bmap
->n_ineq
- 1] = t
;
1298 ISL_F_CLR(bmap
, ISL_BASIC_MAP_NORMALIZED
);
1304 int isl_basic_set_drop_inequality(struct isl_basic_set
*bset
, unsigned pos
)
1306 return isl_basic_map_drop_inequality((struct isl_basic_map
*)bset
, pos
);
1309 __isl_give isl_basic_map
*isl_basic_map_add_eq(__isl_take isl_basic_map
*bmap
,
1314 bmap
= isl_basic_map_extend_constraints(bmap
, 1, 0);
1317 k
= isl_basic_map_alloc_equality(bmap
);
1320 isl_seq_cpy(bmap
->eq
[k
], eq
, 1 + isl_basic_map_total_dim(bmap
));
1323 isl_basic_map_free(bmap
);
1327 __isl_give isl_basic_set
*isl_basic_set_add_eq(__isl_take isl_basic_set
*bset
,
1330 return (isl_basic_set
*)
1331 isl_basic_map_add_eq((isl_basic_map
*)bset
, eq
);
1334 __isl_give isl_basic_map
*isl_basic_map_add_ineq(__isl_take isl_basic_map
*bmap
,
1339 bmap
= isl_basic_map_extend_constraints(bmap
, 0, 1);
1342 k
= isl_basic_map_alloc_inequality(bmap
);
1345 isl_seq_cpy(bmap
->ineq
[k
], ineq
, 1 + isl_basic_map_total_dim(bmap
));
1348 isl_basic_map_free(bmap
);
1352 __isl_give isl_basic_set
*isl_basic_set_add_ineq(__isl_take isl_basic_set
*bset
,
1355 return (isl_basic_set
*)
1356 isl_basic_map_add_ineq((isl_basic_map
*)bset
, ineq
);
1359 int isl_basic_map_alloc_div(struct isl_basic_map
*bmap
)
1363 isl_assert(bmap
->ctx
, bmap
->n_div
< bmap
->extra
, return -1);
1364 isl_seq_clr(bmap
->div
[bmap
->n_div
] +
1365 1 + 1 + isl_basic_map_total_dim(bmap
),
1366 bmap
->extra
- bmap
->n_div
);
1367 ISL_F_CLR(bmap
, ISL_BASIC_MAP_NORMALIZED_DIVS
);
1368 return bmap
->n_div
++;
1371 int isl_basic_set_alloc_div(struct isl_basic_set
*bset
)
1373 return isl_basic_map_alloc_div((struct isl_basic_map
*)bset
);
1376 int isl_basic_map_free_div(struct isl_basic_map
*bmap
, unsigned n
)
1380 isl_assert(bmap
->ctx
, n
<= bmap
->n_div
, return -1);
1385 int isl_basic_set_free_div(struct isl_basic_set
*bset
, unsigned n
)
1387 return isl_basic_map_free_div((struct isl_basic_map
*)bset
, n
);
1390 /* Copy constraint from src to dst, putting the vars of src at offset
1391 * dim_off in dst and the divs of src at offset div_off in dst.
1392 * If both sets are actually map, then dim_off applies to the input
1395 static void copy_constraint(struct isl_basic_map
*dst_map
, isl_int
*dst
,
1396 struct isl_basic_map
*src_map
, isl_int
*src
,
1397 unsigned in_off
, unsigned out_off
, unsigned div_off
)
1399 unsigned src_nparam
= isl_basic_map_n_param(src_map
);
1400 unsigned dst_nparam
= isl_basic_map_n_param(dst_map
);
1401 unsigned src_in
= isl_basic_map_n_in(src_map
);
1402 unsigned dst_in
= isl_basic_map_n_in(dst_map
);
1403 unsigned src_out
= isl_basic_map_n_out(src_map
);
1404 unsigned dst_out
= isl_basic_map_n_out(dst_map
);
1405 isl_int_set(dst
[0], src
[0]);
1406 isl_seq_cpy(dst
+1, src
+1, isl_min(dst_nparam
, src_nparam
));
1407 if (dst_nparam
> src_nparam
)
1408 isl_seq_clr(dst
+1+src_nparam
,
1409 dst_nparam
- src_nparam
);
1410 isl_seq_clr(dst
+1+dst_nparam
, in_off
);
1411 isl_seq_cpy(dst
+1+dst_nparam
+in_off
,
1413 isl_min(dst_in
-in_off
, src_in
));
1414 if (dst_in
-in_off
> src_in
)
1415 isl_seq_clr(dst
+1+dst_nparam
+in_off
+src_in
,
1416 dst_in
- in_off
- src_in
);
1417 isl_seq_clr(dst
+1+dst_nparam
+dst_in
, out_off
);
1418 isl_seq_cpy(dst
+1+dst_nparam
+dst_in
+out_off
,
1419 src
+1+src_nparam
+src_in
,
1420 isl_min(dst_out
-out_off
, src_out
));
1421 if (dst_out
-out_off
> src_out
)
1422 isl_seq_clr(dst
+1+dst_nparam
+dst_in
+out_off
+src_out
,
1423 dst_out
- out_off
- src_out
);
1424 isl_seq_clr(dst
+1+dst_nparam
+dst_in
+dst_out
, div_off
);
1425 isl_seq_cpy(dst
+1+dst_nparam
+dst_in
+dst_out
+div_off
,
1426 src
+1+src_nparam
+src_in
+src_out
,
1427 isl_min(dst_map
->extra
-div_off
, src_map
->n_div
));
1428 if (dst_map
->n_div
-div_off
> src_map
->n_div
)
1429 isl_seq_clr(dst
+1+dst_nparam
+dst_in
+dst_out
+
1430 div_off
+src_map
->n_div
,
1431 dst_map
->n_div
- div_off
- src_map
->n_div
);
1434 static void copy_div(struct isl_basic_map
*dst_map
, isl_int
*dst
,
1435 struct isl_basic_map
*src_map
, isl_int
*src
,
1436 unsigned in_off
, unsigned out_off
, unsigned div_off
)
1438 isl_int_set(dst
[0], src
[0]);
1439 copy_constraint(dst_map
, dst
+1, src_map
, src
+1, in_off
, out_off
, div_off
);
1442 static struct isl_basic_map
*add_constraints(struct isl_basic_map
*bmap1
,
1443 struct isl_basic_map
*bmap2
, unsigned i_pos
, unsigned o_pos
)
1448 if (!bmap1
|| !bmap2
)
1451 div_off
= bmap1
->n_div
;
1453 for (i
= 0; i
< bmap2
->n_eq
; ++i
) {
1454 int i1
= isl_basic_map_alloc_equality(bmap1
);
1457 copy_constraint(bmap1
, bmap1
->eq
[i1
], bmap2
, bmap2
->eq
[i
],
1458 i_pos
, o_pos
, div_off
);
1461 for (i
= 0; i
< bmap2
->n_ineq
; ++i
) {
1462 int i1
= isl_basic_map_alloc_inequality(bmap1
);
1465 copy_constraint(bmap1
, bmap1
->ineq
[i1
], bmap2
, bmap2
->ineq
[i
],
1466 i_pos
, o_pos
, div_off
);
1469 for (i
= 0; i
< bmap2
->n_div
; ++i
) {
1470 int i1
= isl_basic_map_alloc_div(bmap1
);
1473 copy_div(bmap1
, bmap1
->div
[i1
], bmap2
, bmap2
->div
[i
],
1474 i_pos
, o_pos
, div_off
);
1477 isl_basic_map_free(bmap2
);
1482 isl_basic_map_free(bmap1
);
1483 isl_basic_map_free(bmap2
);
1487 struct isl_basic_set
*isl_basic_set_add_constraints(struct isl_basic_set
*bset1
,
1488 struct isl_basic_set
*bset2
, unsigned pos
)
1490 return (struct isl_basic_set
*)
1491 add_constraints((struct isl_basic_map
*)bset1
,
1492 (struct isl_basic_map
*)bset2
, 0, pos
);
1495 struct isl_basic_map
*isl_basic_map_extend_space(struct isl_basic_map
*base
,
1496 __isl_take isl_space
*dim
, unsigned extra
,
1497 unsigned n_eq
, unsigned n_ineq
)
1499 struct isl_basic_map
*ext
;
1509 dims_ok
= isl_space_is_equal(base
->dim
, dim
) &&
1510 base
->extra
>= base
->n_div
+ extra
;
1512 if (dims_ok
&& room_for_con(base
, n_eq
+ n_ineq
) &&
1513 room_for_ineq(base
, n_ineq
)) {
1514 isl_space_free(dim
);
1518 isl_assert(base
->ctx
, base
->dim
->nparam
<= dim
->nparam
, goto error
);
1519 isl_assert(base
->ctx
, base
->dim
->n_in
<= dim
->n_in
, goto error
);
1520 isl_assert(base
->ctx
, base
->dim
->n_out
<= dim
->n_out
, goto error
);
1521 extra
+= base
->extra
;
1523 n_ineq
+= base
->n_ineq
;
1525 ext
= isl_basic_map_alloc_space(dim
, extra
, n_eq
, n_ineq
);
1531 ext
->sample
= isl_vec_copy(base
->sample
);
1532 flags
= base
->flags
;
1533 ext
= add_constraints(ext
, base
, 0, 0);
1536 ISL_F_CLR(ext
, ISL_BASIC_SET_FINAL
);
1542 isl_space_free(dim
);
1543 isl_basic_map_free(base
);
1547 struct isl_basic_set
*isl_basic_set_extend_space(struct isl_basic_set
*base
,
1548 __isl_take isl_space
*dim
, unsigned extra
,
1549 unsigned n_eq
, unsigned n_ineq
)
1551 return (struct isl_basic_set
*)
1552 isl_basic_map_extend_space((struct isl_basic_map
*)base
, dim
,
1553 extra
, n_eq
, n_ineq
);
1556 struct isl_basic_map
*isl_basic_map_extend_constraints(
1557 struct isl_basic_map
*base
, unsigned n_eq
, unsigned n_ineq
)
1561 return isl_basic_map_extend_space(base
, isl_space_copy(base
->dim
),
1565 struct isl_basic_map
*isl_basic_map_extend(struct isl_basic_map
*base
,
1566 unsigned nparam
, unsigned n_in
, unsigned n_out
, unsigned extra
,
1567 unsigned n_eq
, unsigned n_ineq
)
1569 struct isl_basic_map
*bmap
;
1574 dim
= isl_space_alloc(base
->ctx
, nparam
, n_in
, n_out
);
1578 bmap
= isl_basic_map_extend_space(base
, dim
, extra
, n_eq
, n_ineq
);
1581 isl_basic_map_free(base
);
1585 struct isl_basic_set
*isl_basic_set_extend(struct isl_basic_set
*base
,
1586 unsigned nparam
, unsigned dim
, unsigned extra
,
1587 unsigned n_eq
, unsigned n_ineq
)
1589 return (struct isl_basic_set
*)
1590 isl_basic_map_extend((struct isl_basic_map
*)base
,
1591 nparam
, 0, dim
, extra
, n_eq
, n_ineq
);
1594 struct isl_basic_set
*isl_basic_set_extend_constraints(
1595 struct isl_basic_set
*base
, unsigned n_eq
, unsigned n_ineq
)
1597 return (struct isl_basic_set
*)
1598 isl_basic_map_extend_constraints((struct isl_basic_map
*)base
,
1602 struct isl_basic_set
*isl_basic_set_cow(struct isl_basic_set
*bset
)
1604 return (struct isl_basic_set
*)
1605 isl_basic_map_cow((struct isl_basic_map
*)bset
);
1608 struct isl_basic_map
*isl_basic_map_cow(struct isl_basic_map
*bmap
)
1613 if (bmap
->ref
> 1) {
1615 bmap
= isl_basic_map_dup(bmap
);
1618 ISL_F_CLR(bmap
, ISL_BASIC_SET_FINAL
);
1622 struct isl_set
*isl_set_cow(struct isl_set
*set
)
1630 return isl_set_dup(set
);
1633 struct isl_map
*isl_map_cow(struct isl_map
*map
)
1641 return isl_map_dup(map
);
1644 static void swap_vars(struct isl_blk blk
, isl_int
*a
,
1645 unsigned a_len
, unsigned b_len
)
1647 isl_seq_cpy(blk
.data
, a
+a_len
, b_len
);
1648 isl_seq_cpy(blk
.data
+b_len
, a
, a_len
);
1649 isl_seq_cpy(a
, blk
.data
, b_len
+a_len
);
1652 static __isl_give isl_basic_map
*isl_basic_map_swap_vars(
1653 __isl_take isl_basic_map
*bmap
, unsigned pos
, unsigned n1
, unsigned n2
)
1661 isl_assert(bmap
->ctx
,
1662 pos
+ n1
+ n2
<= 1 + isl_basic_map_total_dim(bmap
), goto error
);
1664 if (n1
== 0 || n2
== 0)
1667 bmap
= isl_basic_map_cow(bmap
);
1671 blk
= isl_blk_alloc(bmap
->ctx
, n1
+ n2
);
1672 if (isl_blk_is_error(blk
))
1675 for (i
= 0; i
< bmap
->n_eq
; ++i
)
1677 bmap
->eq
[i
] + pos
, n1
, n2
);
1679 for (i
= 0; i
< bmap
->n_ineq
; ++i
)
1681 bmap
->ineq
[i
] + pos
, n1
, n2
);
1683 for (i
= 0; i
< bmap
->n_div
; ++i
)
1685 bmap
->div
[i
]+1 + pos
, n1
, n2
);
1687 isl_blk_free(bmap
->ctx
, blk
);
1689 ISL_F_CLR(bmap
, ISL_BASIC_SET_NORMALIZED
);
1690 bmap
= isl_basic_map_gauss(bmap
, NULL
);
1691 return isl_basic_map_finalize(bmap
);
1693 isl_basic_map_free(bmap
);
1697 static __isl_give isl_basic_set
*isl_basic_set_swap_vars(
1698 __isl_take isl_basic_set
*bset
, unsigned n
)
1706 nparam
= isl_basic_set_n_param(bset
);
1707 dim
= isl_basic_set_n_dim(bset
);
1708 isl_assert(bset
->ctx
, n
<= dim
, goto error
);
1710 return isl_basic_map_swap_vars(bset
, 1 + nparam
, n
, dim
- n
);
1712 isl_basic_set_free(bset
);
1716 struct isl_basic_map
*isl_basic_map_set_to_empty(struct isl_basic_map
*bmap
)
1722 total
= isl_basic_map_total_dim(bmap
);
1723 isl_basic_map_free_div(bmap
, bmap
->n_div
);
1724 isl_basic_map_free_inequality(bmap
, bmap
->n_ineq
);
1726 isl_basic_map_free_equality(bmap
, bmap
->n_eq
-1);
1728 i
= isl_basic_map_alloc_equality(bmap
);
1732 isl_int_set_si(bmap
->eq
[i
][0], 1);
1733 isl_seq_clr(bmap
->eq
[i
]+1, total
);
1734 ISL_F_SET(bmap
, ISL_BASIC_MAP_EMPTY
);
1735 isl_vec_free(bmap
->sample
);
1736 bmap
->sample
= NULL
;
1737 return isl_basic_map_finalize(bmap
);
1739 isl_basic_map_free(bmap
);
1743 struct isl_basic_set
*isl_basic_set_set_to_empty(struct isl_basic_set
*bset
)
1745 return (struct isl_basic_set
*)
1746 isl_basic_map_set_to_empty((struct isl_basic_map
*)bset
);
1749 /* Swap divs "a" and "b" in "bmap" (without modifying any of the constraints
1752 static void swap_div(__isl_keep isl_basic_map
*bmap
, int a
, int b
)
1754 isl_int
*t
= bmap
->div
[a
];
1755 bmap
->div
[a
] = bmap
->div
[b
];
1759 /* Swap divs "a" and "b" in "bmap" and adjust the constraints and
1760 * div definitions accordingly.
1762 void isl_basic_map_swap_div(struct isl_basic_map
*bmap
, int a
, int b
)
1765 unsigned off
= isl_space_dim(bmap
->dim
, isl_dim_all
);
1767 swap_div(bmap
, a
, b
);
1769 for (i
= 0; i
< bmap
->n_eq
; ++i
)
1770 isl_int_swap(bmap
->eq
[i
][1+off
+a
], bmap
->eq
[i
][1+off
+b
]);
1772 for (i
= 0; i
< bmap
->n_ineq
; ++i
)
1773 isl_int_swap(bmap
->ineq
[i
][1+off
+a
], bmap
->ineq
[i
][1+off
+b
]);
1775 for (i
= 0; i
< bmap
->n_div
; ++i
)
1776 isl_int_swap(bmap
->div
[i
][1+1+off
+a
], bmap
->div
[i
][1+1+off
+b
]);
1777 ISL_F_CLR(bmap
, ISL_BASIC_MAP_NORMALIZED
);
1780 /* Eliminate the specified n dimensions starting at first from the
1781 * constraints, without removing the dimensions from the space.
1782 * If the set is rational, the dimensions are eliminated using Fourier-Motzkin.
1784 __isl_give isl_map
*isl_map_eliminate(__isl_take isl_map
*map
,
1785 enum isl_dim_type type
, unsigned first
, unsigned n
)
1794 if (first
+ n
> isl_map_dim(map
, type
) || first
+ n
< first
)
1795 isl_die(map
->ctx
, isl_error_invalid
,
1796 "index out of bounds", goto error
);
1798 map
= isl_map_cow(map
);
1802 for (i
= 0; i
< map
->n
; ++i
) {
1803 map
->p
[i
] = isl_basic_map_eliminate(map
->p
[i
], type
, first
, n
);
1813 /* Eliminate the specified n dimensions starting at first from the
1814 * constraints, without removing the dimensions from the space.
1815 * If the set is rational, the dimensions are eliminated using Fourier-Motzkin.
1817 __isl_give isl_set
*isl_set_eliminate(__isl_take isl_set
*set
,
1818 enum isl_dim_type type
, unsigned first
, unsigned n
)
1820 return (isl_set
*)isl_map_eliminate((isl_map
*)set
, type
, first
, n
);
1823 /* Eliminate the specified n dimensions starting at first from the
1824 * constraints, without removing the dimensions from the space.
1825 * If the set is rational, the dimensions are eliminated using Fourier-Motzkin.
1827 __isl_give isl_set
*isl_set_eliminate_dims(__isl_take isl_set
*set
,
1828 unsigned first
, unsigned n
)
1830 return isl_set_eliminate(set
, isl_dim_set
, first
, n
);
1833 __isl_give isl_basic_map
*isl_basic_map_remove_divs(
1834 __isl_take isl_basic_map
*bmap
)
1838 bmap
= isl_basic_map_eliminate_vars(bmap
,
1839 isl_space_dim(bmap
->dim
, isl_dim_all
), bmap
->n_div
);
1843 return isl_basic_map_finalize(bmap
);
1846 __isl_give isl_basic_set
*isl_basic_set_remove_divs(
1847 __isl_take isl_basic_set
*bset
)
1849 return (struct isl_basic_set
*)isl_basic_map_remove_divs(
1850 (struct isl_basic_map
*)bset
);
1853 __isl_give isl_map
*isl_map_remove_divs(__isl_take isl_map
*map
)
1862 map
= isl_map_cow(map
);
1866 for (i
= 0; i
< map
->n
; ++i
) {
1867 map
->p
[i
] = isl_basic_map_remove_divs(map
->p
[i
]);
1877 __isl_give isl_set
*isl_set_remove_divs(__isl_take isl_set
*set
)
1879 return isl_map_remove_divs(set
);
1882 struct isl_basic_map
*isl_basic_map_remove_dims(struct isl_basic_map
*bmap
,
1883 enum isl_dim_type type
, unsigned first
, unsigned n
)
1887 isl_assert(bmap
->ctx
, first
+ n
<= isl_basic_map_dim(bmap
, type
),
1889 if (n
== 0 && !isl_space_is_named_or_nested(bmap
->dim
, type
))
1891 bmap
= isl_basic_map_eliminate_vars(bmap
,
1892 isl_basic_map_offset(bmap
, type
) - 1 + first
, n
);
1895 if (ISL_F_ISSET(bmap
, ISL_BASIC_MAP_EMPTY
) && type
== isl_dim_div
)
1897 bmap
= isl_basic_map_drop(bmap
, type
, first
, n
);
1900 isl_basic_map_free(bmap
);
1904 /* Return true if the definition of the given div (recursively) involves
1905 * any of the given variables.
1907 static int div_involves_vars(__isl_keep isl_basic_map
*bmap
, int div
,
1908 unsigned first
, unsigned n
)
1911 unsigned div_offset
= isl_basic_map_offset(bmap
, isl_dim_div
);
1913 if (isl_int_is_zero(bmap
->div
[div
][0]))
1915 if (isl_seq_first_non_zero(bmap
->div
[div
] + 1 + first
, n
) >= 0)
1918 for (i
= bmap
->n_div
- 1; i
>= 0; --i
) {
1919 if (isl_int_is_zero(bmap
->div
[div
][1 + div_offset
+ i
]))
1921 if (div_involves_vars(bmap
, i
, first
, n
))
1928 /* Try and add a lower and/or upper bound on "div" to "bmap"
1929 * based on inequality "i".
1930 * "total" is the total number of variables (excluding the divs).
1931 * "v" is a temporary object that can be used during the calculations.
1932 * If "lb" is set, then a lower bound should be constructed.
1933 * If "ub" is set, then an upper bound should be constructed.
1935 * The calling function has already checked that the inequality does not
1936 * reference "div", but we still need to check that the inequality is
1937 * of the right form. We'll consider the case where we want to construct
1938 * a lower bound. The construction of upper bounds is similar.
1940 * Let "div" be of the form
1942 * q = floor((a + f(x))/d)
1944 * We essentially check if constraint "i" is of the form
1948 * so that we can use it to derive a lower bound on "div".
1949 * However, we allow a slightly more general form
1953 * with the condition that the coefficients of g(x) - f(x) are all
1955 * Rewriting this constraint as
1959 * adding a + f(x) to both sides and dividing by d, we obtain
1961 * (a + f(x))/d >= (a-b)/d + (f(x)-g(x))/d
1963 * Taking the floor on both sides, we obtain
1965 * q >= floor((a-b)/d) + (f(x)-g(x))/d
1969 * (g(x)-f(x))/d + ceil((b-a)/d) + q >= 0
1971 * In the case of an upper bound, we construct the constraint
1973 * (g(x)+f(x))/d + floor((b+a)/d) - q >= 0
1976 static __isl_give isl_basic_map
*insert_bounds_on_div_from_ineq(
1977 __isl_take isl_basic_map
*bmap
, int div
, int i
,
1978 unsigned total
, isl_int v
, int lb
, int ub
)
1982 for (j
= 0; (lb
|| ub
) && j
< total
+ bmap
->n_div
; ++j
) {
1984 isl_int_sub(v
, bmap
->ineq
[i
][1 + j
],
1985 bmap
->div
[div
][1 + 1 + j
]);
1986 lb
= isl_int_is_divisible_by(v
, bmap
->div
[div
][0]);
1989 isl_int_add(v
, bmap
->ineq
[i
][1 + j
],
1990 bmap
->div
[div
][1 + 1 + j
]);
1991 ub
= isl_int_is_divisible_by(v
, bmap
->div
[div
][0]);
1997 bmap
= isl_basic_map_cow(bmap
);
1998 bmap
= isl_basic_map_extend_constraints(bmap
, 0, lb
+ ub
);
2000 int k
= isl_basic_map_alloc_inequality(bmap
);
2003 for (j
= 0; j
< 1 + total
+ bmap
->n_div
; ++j
) {
2004 isl_int_sub(bmap
->ineq
[k
][j
], bmap
->ineq
[i
][j
],
2005 bmap
->div
[div
][1 + j
]);
2006 isl_int_cdiv_q(bmap
->ineq
[k
][j
],
2007 bmap
->ineq
[k
][j
], bmap
->div
[div
][0]);
2009 isl_int_set_si(bmap
->ineq
[k
][1 + total
+ div
], 1);
2012 int k
= isl_basic_map_alloc_inequality(bmap
);
2015 for (j
= 0; j
< 1 + total
+ bmap
->n_div
; ++j
) {
2016 isl_int_add(bmap
->ineq
[k
][j
], bmap
->ineq
[i
][j
],
2017 bmap
->div
[div
][1 + j
]);
2018 isl_int_fdiv_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 ISL_F_CLR(bmap
, ISL_BASIC_MAP_NORMALIZED
);
2027 isl_basic_map_free(bmap
);
2031 /* This function is called right before "div" is eliminated from "bmap"
2032 * using Fourier-Motzkin.
2033 * Look through the constraints of "bmap" for constraints on the argument
2034 * of the integer division and use them to construct constraints on the
2035 * integer division itself. These constraints can then be combined
2036 * during the Fourier-Motzkin elimination.
2037 * Note that it is only useful to introduce lower bounds on "div"
2038 * if "bmap" already contains upper bounds on "div" as the newly
2039 * introduce lower bounds can then be combined with the pre-existing
2040 * upper bounds. Similarly for upper bounds.
2041 * We therefore first check if "bmap" contains any lower and/or upper bounds
2044 * It is interesting to note that the introduction of these constraints
2045 * can indeed lead to more accurate results, even when compared to
2046 * deriving constraints on the argument of "div" from constraints on "div".
2047 * Consider, for example, the set
2049 * { [i,j,k] : 3 + i + 2j >= 0 and 2 * [(i+2j)/4] <= k }
2051 * The second constraint can be rewritten as
2053 * 2 * [(-i-2j+3)/4] + k >= 0
2055 * from which we can derive
2057 * -i - 2j + 3 >= -2k
2063 * Combined with the first constraint, we obtain
2065 * -3 <= 3 + 2k or k >= -3
2067 * If, on the other hand we derive a constraint on [(i+2j)/4] from
2068 * the first constraint, we obtain
2070 * [(i + 2j)/4] >= [-3/4] = -1
2072 * Combining this constraint with the second constraint, we obtain
2076 static __isl_give isl_basic_map
*insert_bounds_on_div(
2077 __isl_take isl_basic_map
*bmap
, int div
)
2080 int check_lb
, check_ub
;
2087 if (isl_int_is_zero(bmap
->div
[div
][0]))
2090 total
= isl_space_dim(bmap
->dim
, isl_dim_all
);
2094 for (i
= 0; (!check_lb
|| !check_ub
) && i
< bmap
->n_ineq
; ++i
) {
2095 int s
= isl_int_sgn(bmap
->ineq
[i
][1 + total
+ div
]);
2102 if (!check_lb
&& !check_ub
)
2107 for (i
= 0; bmap
&& i
< bmap
->n_ineq
; ++i
) {
2108 if (!isl_int_is_zero(bmap
->ineq
[i
][1 + total
+ div
]))
2111 bmap
= insert_bounds_on_div_from_ineq(bmap
, div
, i
, total
, v
,
2112 check_lb
, check_ub
);
2120 /* Remove all divs (recursively) involving any of the given dimensions
2121 * in their definitions.
2123 __isl_give isl_basic_map
*isl_basic_map_remove_divs_involving_dims(
2124 __isl_take isl_basic_map
*bmap
,
2125 enum isl_dim_type type
, unsigned first
, unsigned n
)
2131 isl_assert(bmap
->ctx
, first
+ n
<= isl_basic_map_dim(bmap
, type
),
2133 first
+= isl_basic_map_offset(bmap
, type
);
2135 for (i
= bmap
->n_div
- 1; i
>= 0; --i
) {
2136 if (!div_involves_vars(bmap
, i
, first
, n
))
2138 bmap
= insert_bounds_on_div(bmap
, i
);
2139 bmap
= isl_basic_map_remove_dims(bmap
, isl_dim_div
, i
, 1);
2147 isl_basic_map_free(bmap
);
2151 __isl_give isl_basic_set
*isl_basic_set_remove_divs_involving_dims(
2152 __isl_take isl_basic_set
*bset
,
2153 enum isl_dim_type type
, unsigned first
, unsigned n
)
2155 return isl_basic_map_remove_divs_involving_dims(bset
, type
, first
, n
);
2158 __isl_give isl_map
*isl_map_remove_divs_involving_dims(__isl_take isl_map
*map
,
2159 enum isl_dim_type type
, unsigned first
, unsigned n
)
2168 map
= isl_map_cow(map
);
2172 for (i
= 0; i
< map
->n
; ++i
) {
2173 map
->p
[i
] = isl_basic_map_remove_divs_involving_dims(map
->p
[i
],
2184 __isl_give isl_set
*isl_set_remove_divs_involving_dims(__isl_take isl_set
*set
,
2185 enum isl_dim_type type
, unsigned first
, unsigned n
)
2187 return (isl_set
*)isl_map_remove_divs_involving_dims((isl_map
*)set
,
2191 /* Does the desciption of "bmap" depend on the specified dimensions?
2192 * We also check whether the dimensions appear in any of the div definitions.
2193 * In principle there is no need for this check. If the dimensions appear
2194 * in a div definition, they also appear in the defining constraints of that
2197 int isl_basic_map_involves_dims(__isl_keep isl_basic_map
*bmap
,
2198 enum isl_dim_type type
, unsigned first
, unsigned n
)
2205 if (first
+ n
> isl_basic_map_dim(bmap
, type
))
2206 isl_die(bmap
->ctx
, isl_error_invalid
,
2207 "index out of bounds", return -1);
2209 first
+= isl_basic_map_offset(bmap
, type
);
2210 for (i
= 0; i
< bmap
->n_eq
; ++i
)
2211 if (isl_seq_first_non_zero(bmap
->eq
[i
] + first
, n
) >= 0)
2213 for (i
= 0; i
< bmap
->n_ineq
; ++i
)
2214 if (isl_seq_first_non_zero(bmap
->ineq
[i
] + first
, n
) >= 0)
2216 for (i
= 0; i
< bmap
->n_div
; ++i
) {
2217 if (isl_int_is_zero(bmap
->div
[i
][0]))
2219 if (isl_seq_first_non_zero(bmap
->div
[i
] + 1 + first
, n
) >= 0)
2226 int isl_map_involves_dims(__isl_keep isl_map
*map
,
2227 enum isl_dim_type type
, unsigned first
, unsigned n
)
2234 if (first
+ n
> isl_map_dim(map
, type
))
2235 isl_die(map
->ctx
, isl_error_invalid
,
2236 "index out of bounds", return -1);
2238 for (i
= 0; i
< map
->n
; ++i
) {
2239 int involves
= isl_basic_map_involves_dims(map
->p
[i
],
2241 if (involves
< 0 || involves
)
2248 int isl_basic_set_involves_dims(__isl_keep isl_basic_set
*bset
,
2249 enum isl_dim_type type
, unsigned first
, unsigned n
)
2251 return isl_basic_map_involves_dims(bset
, type
, first
, n
);
2254 int isl_set_involves_dims(__isl_keep isl_set
*set
,
2255 enum isl_dim_type type
, unsigned first
, unsigned n
)
2257 return isl_map_involves_dims(set
, type
, first
, n
);
2260 /* Return true if the definition of the given div is unknown or depends
2263 static int div_is_unknown(__isl_keep isl_basic_map
*bmap
, int div
)
2266 unsigned div_offset
= isl_basic_map_offset(bmap
, isl_dim_div
);
2268 if (isl_int_is_zero(bmap
->div
[div
][0]))
2271 for (i
= bmap
->n_div
- 1; i
>= 0; --i
) {
2272 if (isl_int_is_zero(bmap
->div
[div
][1 + div_offset
+ i
]))
2274 if (div_is_unknown(bmap
, i
))
2281 /* Remove all divs that are unknown or defined in terms of unknown divs.
2283 __isl_give isl_basic_map
*isl_basic_map_remove_unknown_divs(
2284 __isl_take isl_basic_map
*bmap
)
2291 for (i
= bmap
->n_div
- 1; i
>= 0; --i
) {
2292 if (!div_is_unknown(bmap
, i
))
2294 bmap
= isl_basic_map_remove_dims(bmap
, isl_dim_div
, i
, 1);
2303 /* Remove all divs that are unknown or defined in terms of unknown divs.
2305 __isl_give isl_basic_set
*isl_basic_set_remove_unknown_divs(
2306 __isl_take isl_basic_set
*bset
)
2308 return isl_basic_map_remove_unknown_divs(bset
);
2311 __isl_give isl_map
*isl_map_remove_unknown_divs(__isl_take isl_map
*map
)
2320 map
= isl_map_cow(map
);
2324 for (i
= 0; i
< map
->n
; ++i
) {
2325 map
->p
[i
] = isl_basic_map_remove_unknown_divs(map
->p
[i
]);
2335 __isl_give isl_set
*isl_set_remove_unknown_divs(__isl_take isl_set
*set
)
2337 return (isl_set
*)isl_map_remove_unknown_divs((isl_map
*)set
);
2340 __isl_give isl_basic_set
*isl_basic_set_remove_dims(
2341 __isl_take isl_basic_set
*bset
,
2342 enum isl_dim_type type
, unsigned first
, unsigned n
)
2344 return (isl_basic_set
*)
2345 isl_basic_map_remove_dims((isl_basic_map
*)bset
, type
, first
, n
);
2348 struct isl_map
*isl_map_remove_dims(struct isl_map
*map
,
2349 enum isl_dim_type type
, unsigned first
, unsigned n
)
2356 map
= isl_map_cow(map
);
2359 isl_assert(map
->ctx
, first
+ n
<= isl_map_dim(map
, type
), goto error
);
2361 for (i
= 0; i
< map
->n
; ++i
) {
2362 map
->p
[i
] = isl_basic_map_eliminate_vars(map
->p
[i
],
2363 isl_basic_map_offset(map
->p
[i
], type
) - 1 + first
, n
);
2367 map
= isl_map_drop(map
, type
, first
, n
);
2374 __isl_give isl_set
*isl_set_remove_dims(__isl_take isl_set
*bset
,
2375 enum isl_dim_type type
, unsigned first
, unsigned n
)
2377 return (isl_set
*)isl_map_remove_dims((isl_map
*)bset
, type
, first
, n
);
2380 /* Project out n inputs starting at first using Fourier-Motzkin */
2381 struct isl_map
*isl_map_remove_inputs(struct isl_map
*map
,
2382 unsigned first
, unsigned n
)
2384 return isl_map_remove_dims(map
, isl_dim_in
, first
, n
);
2387 static void dump_term(struct isl_basic_map
*bmap
,
2388 isl_int c
, int pos
, FILE *out
)
2391 unsigned in
= isl_basic_map_n_in(bmap
);
2392 unsigned dim
= in
+ isl_basic_map_n_out(bmap
);
2393 unsigned nparam
= isl_basic_map_n_param(bmap
);
2395 isl_int_print(out
, c
, 0);
2397 if (!isl_int_is_one(c
))
2398 isl_int_print(out
, c
, 0);
2399 if (pos
< 1 + nparam
) {
2400 name
= isl_space_get_dim_name(bmap
->dim
,
2401 isl_dim_param
, pos
- 1);
2403 fprintf(out
, "%s", name
);
2405 fprintf(out
, "p%d", pos
- 1);
2406 } else if (pos
< 1 + nparam
+ in
)
2407 fprintf(out
, "i%d", pos
- 1 - nparam
);
2408 else if (pos
< 1 + nparam
+ dim
)
2409 fprintf(out
, "o%d", pos
- 1 - nparam
- in
);
2411 fprintf(out
, "e%d", pos
- 1 - nparam
- dim
);
2415 static void dump_constraint_sign(struct isl_basic_map
*bmap
, isl_int
*c
,
2416 int sign
, FILE *out
)
2420 unsigned len
= 1 + isl_basic_map_total_dim(bmap
);
2424 for (i
= 0, first
= 1; i
< len
; ++i
) {
2425 if (isl_int_sgn(c
[i
]) * sign
<= 0)
2428 fprintf(out
, " + ");
2430 isl_int_abs(v
, c
[i
]);
2431 dump_term(bmap
, v
, i
, out
);
2438 static void dump_constraint(struct isl_basic_map
*bmap
, isl_int
*c
,
2439 const char *op
, FILE *out
, int indent
)
2443 fprintf(out
, "%*s", indent
, "");
2445 dump_constraint_sign(bmap
, c
, 1, out
);
2446 fprintf(out
, " %s ", op
);
2447 dump_constraint_sign(bmap
, c
, -1, out
);
2451 for (i
= bmap
->n_div
; i
< bmap
->extra
; ++i
) {
2452 if (isl_int_is_zero(c
[1+isl_space_dim(bmap
->dim
, isl_dim_all
)+i
]))
2454 fprintf(out
, "%*s", indent
, "");
2455 fprintf(out
, "ERROR: unused div coefficient not zero\n");
2460 static void dump_constraints(struct isl_basic_map
*bmap
,
2461 isl_int
**c
, unsigned n
,
2462 const char *op
, FILE *out
, int indent
)
2466 for (i
= 0; i
< n
; ++i
)
2467 dump_constraint(bmap
, c
[i
], op
, out
, indent
);
2470 static void dump_affine(struct isl_basic_map
*bmap
, isl_int
*exp
, FILE *out
)
2474 unsigned total
= isl_basic_map_total_dim(bmap
);
2476 for (j
= 0; j
< 1 + total
; ++j
) {
2477 if (isl_int_is_zero(exp
[j
]))
2479 if (!first
&& isl_int_is_pos(exp
[j
]))
2481 dump_term(bmap
, exp
[j
], j
, out
);
2486 static void dump(struct isl_basic_map
*bmap
, FILE *out
, int indent
)
2490 dump_constraints(bmap
, bmap
->eq
, bmap
->n_eq
, "=", out
, indent
);
2491 dump_constraints(bmap
, bmap
->ineq
, bmap
->n_ineq
, ">=", out
, indent
);
2493 for (i
= 0; i
< bmap
->n_div
; ++i
) {
2494 fprintf(out
, "%*s", indent
, "");
2495 fprintf(out
, "e%d = [(", i
);
2496 dump_affine(bmap
, bmap
->div
[i
]+1, out
);
2498 isl_int_print(out
, bmap
->div
[i
][0], 0);
2499 fprintf(out
, "]\n");
2503 void isl_basic_set_print_internal(struct isl_basic_set
*bset
,
2504 FILE *out
, int indent
)
2507 fprintf(out
, "null basic set\n");
2511 fprintf(out
, "%*s", indent
, "");
2512 fprintf(out
, "ref: %d, nparam: %d, dim: %d, extra: %d, flags: %x\n",
2513 bset
->ref
, bset
->dim
->nparam
, bset
->dim
->n_out
,
2514 bset
->extra
, bset
->flags
);
2515 dump((struct isl_basic_map
*)bset
, out
, indent
);
2518 void isl_basic_map_print_internal(struct isl_basic_map
*bmap
,
2519 FILE *out
, int indent
)
2522 fprintf(out
, "null basic map\n");
2526 fprintf(out
, "%*s", indent
, "");
2527 fprintf(out
, "ref: %d, nparam: %d, in: %d, out: %d, extra: %d, "
2528 "flags: %x, n_name: %d\n",
2530 bmap
->dim
->nparam
, bmap
->dim
->n_in
, bmap
->dim
->n_out
,
2531 bmap
->extra
, bmap
->flags
, bmap
->dim
->n_id
);
2532 dump(bmap
, out
, indent
);
2535 int isl_inequality_negate(struct isl_basic_map
*bmap
, unsigned pos
)
2540 total
= isl_basic_map_total_dim(bmap
);
2541 isl_assert(bmap
->ctx
, pos
< bmap
->n_ineq
, return -1);
2542 isl_seq_neg(bmap
->ineq
[pos
], bmap
->ineq
[pos
], 1 + total
);
2543 isl_int_sub_ui(bmap
->ineq
[pos
][0], bmap
->ineq
[pos
][0], 1);
2544 ISL_F_CLR(bmap
, ISL_BASIC_MAP_NORMALIZED
);
2548 __isl_give isl_set
*isl_set_alloc_space(__isl_take isl_space
*dim
, int n
,
2551 struct isl_set
*set
;
2555 isl_assert(dim
->ctx
, dim
->n_in
== 0, goto error
);
2556 isl_assert(dim
->ctx
, n
>= 0, goto error
);
2557 set
= isl_alloc(dim
->ctx
, struct isl_set
,
2558 sizeof(struct isl_set
) +
2559 (n
- 1) * sizeof(struct isl_basic_set
*));
2563 set
->ctx
= dim
->ctx
;
2564 isl_ctx_ref(set
->ctx
);
2572 isl_space_free(dim
);
2576 struct isl_set
*isl_set_alloc(struct isl_ctx
*ctx
,
2577 unsigned nparam
, unsigned dim
, int n
, unsigned flags
)
2579 struct isl_set
*set
;
2582 dims
= isl_space_alloc(ctx
, nparam
, 0, dim
);
2586 set
= isl_set_alloc_space(dims
, n
, flags
);
2590 /* Make sure "map" has room for at least "n" more basic maps.
2592 struct isl_map
*isl_map_grow(struct isl_map
*map
, int n
)
2595 struct isl_map
*grown
= NULL
;
2599 isl_assert(map
->ctx
, n
>= 0, goto error
);
2600 if (map
->n
+ n
<= map
->size
)
2602 grown
= isl_map_alloc_space(isl_map_get_space(map
), map
->n
+ n
, map
->flags
);
2605 for (i
= 0; i
< map
->n
; ++i
) {
2606 grown
->p
[i
] = isl_basic_map_copy(map
->p
[i
]);
2614 isl_map_free(grown
);
2619 /* Make sure "set" has room for at least "n" more basic sets.
2621 struct isl_set
*isl_set_grow(struct isl_set
*set
, int n
)
2623 return (struct isl_set
*)isl_map_grow((struct isl_map
*)set
, n
);
2626 struct isl_set
*isl_set_dup(struct isl_set
*set
)
2629 struct isl_set
*dup
;
2634 dup
= isl_set_alloc_space(isl_space_copy(set
->dim
), set
->n
, set
->flags
);
2637 for (i
= 0; i
< set
->n
; ++i
)
2638 dup
= isl_set_add_basic_set(dup
, isl_basic_set_copy(set
->p
[i
]));
2642 struct isl_set
*isl_set_from_basic_set(struct isl_basic_set
*bset
)
2644 return isl_map_from_basic_map(bset
);
2647 struct isl_map
*isl_map_from_basic_map(struct isl_basic_map
*bmap
)
2649 struct isl_map
*map
;
2654 map
= isl_map_alloc_space(isl_space_copy(bmap
->dim
), 1, ISL_MAP_DISJOINT
);
2655 return isl_map_add_basic_map(map
, bmap
);
2658 __isl_give isl_set
*isl_set_add_basic_set(__isl_take isl_set
*set
,
2659 __isl_take isl_basic_set
*bset
)
2661 return (struct isl_set
*)isl_map_add_basic_map((struct isl_map
*)set
,
2662 (struct isl_basic_map
*)bset
);
2665 __isl_null isl_set
*isl_set_free(__isl_take isl_set
*set
)
2675 isl_ctx_deref(set
->ctx
);
2676 for (i
= 0; i
< set
->n
; ++i
)
2677 isl_basic_set_free(set
->p
[i
]);
2678 isl_space_free(set
->dim
);
2684 void isl_set_print_internal(struct isl_set
*set
, FILE *out
, int indent
)
2689 fprintf(out
, "null set\n");
2693 fprintf(out
, "%*s", indent
, "");
2694 fprintf(out
, "ref: %d, n: %d, nparam: %d, dim: %d, flags: %x\n",
2695 set
->ref
, set
->n
, set
->dim
->nparam
, set
->dim
->n_out
,
2697 for (i
= 0; i
< set
->n
; ++i
) {
2698 fprintf(out
, "%*s", indent
, "");
2699 fprintf(out
, "basic set %d:\n", i
);
2700 isl_basic_set_print_internal(set
->p
[i
], out
, indent
+4);
2704 void isl_map_print_internal(struct isl_map
*map
, FILE *out
, int indent
)
2709 fprintf(out
, "null map\n");
2713 fprintf(out
, "%*s", indent
, "");
2714 fprintf(out
, "ref: %d, n: %d, nparam: %d, in: %d, out: %d, "
2715 "flags: %x, n_name: %d\n",
2716 map
->ref
, map
->n
, map
->dim
->nparam
, map
->dim
->n_in
,
2717 map
->dim
->n_out
, map
->flags
, map
->dim
->n_id
);
2718 for (i
= 0; i
< map
->n
; ++i
) {
2719 fprintf(out
, "%*s", indent
, "");
2720 fprintf(out
, "basic map %d:\n", i
);
2721 isl_basic_map_print_internal(map
->p
[i
], out
, indent
+4);
2725 struct isl_basic_map
*isl_basic_map_intersect_domain(
2726 struct isl_basic_map
*bmap
, struct isl_basic_set
*bset
)
2728 struct isl_basic_map
*bmap_domain
;
2733 isl_assert(bset
->ctx
, isl_space_match(bmap
->dim
, isl_dim_param
,
2734 bset
->dim
, isl_dim_param
), goto error
);
2736 if (isl_space_dim(bset
->dim
, isl_dim_set
) != 0)
2737 isl_assert(bset
->ctx
,
2738 isl_basic_map_compatible_domain(bmap
, bset
), goto error
);
2740 bmap
= isl_basic_map_cow(bmap
);
2743 bmap
= isl_basic_map_extend_space(bmap
, isl_space_copy(bmap
->dim
),
2744 bset
->n_div
, bset
->n_eq
, bset
->n_ineq
);
2745 bmap_domain
= isl_basic_map_from_domain(bset
);
2746 bmap
= add_constraints(bmap
, bmap_domain
, 0, 0);
2748 bmap
= isl_basic_map_simplify(bmap
);
2749 return isl_basic_map_finalize(bmap
);
2751 isl_basic_map_free(bmap
);
2752 isl_basic_set_free(bset
);
2756 struct isl_basic_map
*isl_basic_map_intersect_range(
2757 struct isl_basic_map
*bmap
, struct isl_basic_set
*bset
)
2759 struct isl_basic_map
*bmap_range
;
2764 isl_assert(bset
->ctx
, isl_space_match(bmap
->dim
, isl_dim_param
,
2765 bset
->dim
, isl_dim_param
), goto error
);
2767 if (isl_space_dim(bset
->dim
, isl_dim_set
) != 0)
2768 isl_assert(bset
->ctx
,
2769 isl_basic_map_compatible_range(bmap
, bset
), goto error
);
2771 if (isl_basic_set_is_universe(bset
)) {
2772 isl_basic_set_free(bset
);
2776 bmap
= isl_basic_map_cow(bmap
);
2779 bmap
= isl_basic_map_extend_space(bmap
, isl_space_copy(bmap
->dim
),
2780 bset
->n_div
, bset
->n_eq
, bset
->n_ineq
);
2781 bmap_range
= isl_basic_map_from_basic_set(bset
, isl_space_copy(bset
->dim
));
2782 bmap
= add_constraints(bmap
, bmap_range
, 0, 0);
2784 bmap
= isl_basic_map_simplify(bmap
);
2785 return isl_basic_map_finalize(bmap
);
2787 isl_basic_map_free(bmap
);
2788 isl_basic_set_free(bset
);
2792 int isl_basic_map_contains(struct isl_basic_map
*bmap
, struct isl_vec
*vec
)
2801 total
= 1 + isl_basic_map_total_dim(bmap
);
2802 if (total
!= vec
->size
)
2807 for (i
= 0; i
< bmap
->n_eq
; ++i
) {
2808 isl_seq_inner_product(vec
->el
, bmap
->eq
[i
], total
, &s
);
2809 if (!isl_int_is_zero(s
)) {
2815 for (i
= 0; i
< bmap
->n_ineq
; ++i
) {
2816 isl_seq_inner_product(vec
->el
, bmap
->ineq
[i
], total
, &s
);
2817 if (isl_int_is_neg(s
)) {
2828 int isl_basic_set_contains(struct isl_basic_set
*bset
, struct isl_vec
*vec
)
2830 return isl_basic_map_contains((struct isl_basic_map
*)bset
, vec
);
2833 struct isl_basic_map
*isl_basic_map_intersect(
2834 struct isl_basic_map
*bmap1
, struct isl_basic_map
*bmap2
)
2836 struct isl_vec
*sample
= NULL
;
2838 if (!bmap1
|| !bmap2
)
2841 isl_assert(bmap1
->ctx
, isl_space_match(bmap1
->dim
, isl_dim_param
,
2842 bmap2
->dim
, isl_dim_param
), goto error
);
2843 if (isl_space_dim(bmap1
->dim
, isl_dim_all
) ==
2844 isl_space_dim(bmap1
->dim
, isl_dim_param
) &&
2845 isl_space_dim(bmap2
->dim
, isl_dim_all
) !=
2846 isl_space_dim(bmap2
->dim
, isl_dim_param
))
2847 return isl_basic_map_intersect(bmap2
, bmap1
);
2849 if (isl_space_dim(bmap2
->dim
, isl_dim_all
) !=
2850 isl_space_dim(bmap2
->dim
, isl_dim_param
))
2851 isl_assert(bmap1
->ctx
,
2852 isl_space_is_equal(bmap1
->dim
, bmap2
->dim
), goto error
);
2854 if (bmap1
->sample
&&
2855 isl_basic_map_contains(bmap1
, bmap1
->sample
) > 0 &&
2856 isl_basic_map_contains(bmap2
, bmap1
->sample
) > 0)
2857 sample
= isl_vec_copy(bmap1
->sample
);
2858 else if (bmap2
->sample
&&
2859 isl_basic_map_contains(bmap1
, bmap2
->sample
) > 0 &&
2860 isl_basic_map_contains(bmap2
, bmap2
->sample
) > 0)
2861 sample
= isl_vec_copy(bmap2
->sample
);
2863 bmap1
= isl_basic_map_cow(bmap1
);
2866 bmap1
= isl_basic_map_extend_space(bmap1
, isl_space_copy(bmap1
->dim
),
2867 bmap2
->n_div
, bmap2
->n_eq
, bmap2
->n_ineq
);
2868 bmap1
= add_constraints(bmap1
, bmap2
, 0, 0);
2871 isl_vec_free(sample
);
2873 isl_vec_free(bmap1
->sample
);
2874 bmap1
->sample
= sample
;
2877 bmap1
= isl_basic_map_simplify(bmap1
);
2878 return isl_basic_map_finalize(bmap1
);
2881 isl_vec_free(sample
);
2882 isl_basic_map_free(bmap1
);
2883 isl_basic_map_free(bmap2
);
2887 struct isl_basic_set
*isl_basic_set_intersect(
2888 struct isl_basic_set
*bset1
, struct isl_basic_set
*bset2
)
2890 return (struct isl_basic_set
*)
2891 isl_basic_map_intersect(
2892 (struct isl_basic_map
*)bset1
,
2893 (struct isl_basic_map
*)bset2
);
2896 __isl_give isl_basic_set
*isl_basic_set_intersect_params(
2897 __isl_take isl_basic_set
*bset1
, __isl_take isl_basic_set
*bset2
)
2899 return isl_basic_set_intersect(bset1
, bset2
);
2902 /* Special case of isl_map_intersect, where both map1 and map2
2903 * are convex, without any divs and such that either map1 or map2
2904 * contains a single constraint. This constraint is then simply
2905 * added to the other map.
2907 static __isl_give isl_map
*map_intersect_add_constraint(
2908 __isl_take isl_map
*map1
, __isl_take isl_map
*map2
)
2910 isl_assert(map1
->ctx
, map1
->n
== 1, goto error
);
2911 isl_assert(map2
->ctx
, map1
->n
== 1, goto error
);
2912 isl_assert(map1
->ctx
, map1
->p
[0]->n_div
== 0, goto error
);
2913 isl_assert(map2
->ctx
, map1
->p
[0]->n_div
== 0, goto error
);
2915 if (map2
->p
[0]->n_eq
+ map2
->p
[0]->n_ineq
!= 1)
2916 return isl_map_intersect(map2
, map1
);
2918 isl_assert(map2
->ctx
,
2919 map2
->p
[0]->n_eq
+ map2
->p
[0]->n_ineq
== 1, goto error
);
2921 map1
= isl_map_cow(map1
);
2924 if (isl_map_plain_is_empty(map1
)) {
2928 map1
->p
[0] = isl_basic_map_cow(map1
->p
[0]);
2929 if (map2
->p
[0]->n_eq
== 1)
2930 map1
->p
[0] = isl_basic_map_add_eq(map1
->p
[0], map2
->p
[0]->eq
[0]);
2932 map1
->p
[0] = isl_basic_map_add_ineq(map1
->p
[0],
2933 map2
->p
[0]->ineq
[0]);
2935 map1
->p
[0] = isl_basic_map_simplify(map1
->p
[0]);
2936 map1
->p
[0] = isl_basic_map_finalize(map1
->p
[0]);
2940 if (isl_basic_map_plain_is_empty(map1
->p
[0])) {
2941 isl_basic_map_free(map1
->p
[0]);
2954 /* map2 may be either a parameter domain or a map living in the same
2957 static __isl_give isl_map
*map_intersect_internal(__isl_take isl_map
*map1
,
2958 __isl_take isl_map
*map2
)
2967 if ((isl_map_plain_is_empty(map1
) ||
2968 isl_map_plain_is_universe(map2
)) &&
2969 isl_space_is_equal(map1
->dim
, map2
->dim
)) {
2973 if ((isl_map_plain_is_empty(map2
) ||
2974 isl_map_plain_is_universe(map1
)) &&
2975 isl_space_is_equal(map1
->dim
, map2
->dim
)) {
2980 if (map1
->n
== 1 && map2
->n
== 1 &&
2981 map1
->p
[0]->n_div
== 0 && map2
->p
[0]->n_div
== 0 &&
2982 isl_space_is_equal(map1
->dim
, map2
->dim
) &&
2983 (map1
->p
[0]->n_eq
+ map1
->p
[0]->n_ineq
== 1 ||
2984 map2
->p
[0]->n_eq
+ map2
->p
[0]->n_ineq
== 1))
2985 return map_intersect_add_constraint(map1
, map2
);
2987 if (isl_space_dim(map2
->dim
, isl_dim_all
) !=
2988 isl_space_dim(map2
->dim
, isl_dim_param
))
2989 isl_assert(map1
->ctx
,
2990 isl_space_is_equal(map1
->dim
, map2
->dim
), goto error
);
2992 if (ISL_F_ISSET(map1
, ISL_MAP_DISJOINT
) &&
2993 ISL_F_ISSET(map2
, ISL_MAP_DISJOINT
))
2994 ISL_FL_SET(flags
, ISL_MAP_DISJOINT
);
2996 result
= isl_map_alloc_space(isl_space_copy(map1
->dim
),
2997 map1
->n
* map2
->n
, flags
);
3000 for (i
= 0; i
< map1
->n
; ++i
)
3001 for (j
= 0; j
< map2
->n
; ++j
) {
3002 struct isl_basic_map
*part
;
3003 part
= isl_basic_map_intersect(
3004 isl_basic_map_copy(map1
->p
[i
]),
3005 isl_basic_map_copy(map2
->p
[j
]));
3006 if (isl_basic_map_is_empty(part
) < 0)
3007 part
= isl_basic_map_free(part
);
3008 result
= isl_map_add_basic_map(result
, part
);
3021 static __isl_give isl_map
*map_intersect(__isl_take isl_map
*map1
,
3022 __isl_take isl_map
*map2
)
3026 if (!isl_space_is_equal(map1
->dim
, map2
->dim
))
3027 isl_die(isl_map_get_ctx(map1
), isl_error_invalid
,
3028 "spaces don't match", goto error
);
3029 return map_intersect_internal(map1
, map2
);
3036 __isl_give isl_map
*isl_map_intersect(__isl_take isl_map
*map1
,
3037 __isl_take isl_map
*map2
)
3039 return isl_map_align_params_map_map_and(map1
, map2
, &map_intersect
);
3042 struct isl_set
*isl_set_intersect(struct isl_set
*set1
, struct isl_set
*set2
)
3044 return (struct isl_set
*)
3045 isl_map_intersect((struct isl_map
*)set1
,
3046 (struct isl_map
*)set2
);
3049 /* map_intersect_internal accepts intersections
3050 * with parameter domains, so we can just call that function.
3052 static __isl_give isl_map
*map_intersect_params(__isl_take isl_map
*map
,
3053 __isl_take isl_set
*params
)
3055 return map_intersect_internal(map
, params
);
3058 __isl_give isl_map
*isl_map_intersect_params(__isl_take isl_map
*map1
,
3059 __isl_take isl_map
*map2
)
3061 return isl_map_align_params_map_map_and(map1
, map2
, &map_intersect_params
);
3064 __isl_give isl_set
*isl_set_intersect_params(__isl_take isl_set
*set
,
3065 __isl_take isl_set
*params
)
3067 return isl_map_intersect_params(set
, params
);
3070 struct isl_basic_map
*isl_basic_map_reverse(struct isl_basic_map
*bmap
)
3073 struct isl_basic_set
*bset
;
3078 bmap
= isl_basic_map_cow(bmap
);
3081 dim
= isl_space_reverse(isl_space_copy(bmap
->dim
));
3082 in
= isl_basic_map_n_in(bmap
);
3083 bset
= isl_basic_set_from_basic_map(bmap
);
3084 bset
= isl_basic_set_swap_vars(bset
, in
);
3085 return isl_basic_map_from_basic_set(bset
, dim
);
3088 static __isl_give isl_basic_map
*basic_map_space_reset(
3089 __isl_take isl_basic_map
*bmap
, enum isl_dim_type type
)
3095 if (!isl_space_is_named_or_nested(bmap
->dim
, type
))
3098 space
= isl_basic_map_get_space(bmap
);
3099 space
= isl_space_reset(space
, type
);
3100 bmap
= isl_basic_map_reset_space(bmap
, space
);
3104 __isl_give isl_basic_map
*isl_basic_map_insert_dims(
3105 __isl_take isl_basic_map
*bmap
, enum isl_dim_type type
,
3106 unsigned pos
, unsigned n
)
3109 struct isl_basic_map
*res
;
3110 struct isl_dim_map
*dim_map
;
3111 unsigned total
, off
;
3112 enum isl_dim_type t
;
3115 return basic_map_space_reset(bmap
, type
);
3120 res_dim
= isl_space_insert_dims(isl_basic_map_get_space(bmap
), type
, pos
, n
);
3122 total
= isl_basic_map_total_dim(bmap
) + n
;
3123 dim_map
= isl_dim_map_alloc(bmap
->ctx
, total
);
3125 for (t
= isl_dim_param
; t
<= isl_dim_out
; ++t
) {
3127 isl_dim_map_dim(dim_map
, bmap
->dim
, t
, off
);
3129 unsigned size
= isl_basic_map_dim(bmap
, t
);
3130 isl_dim_map_dim_range(dim_map
, bmap
->dim
, t
,
3132 isl_dim_map_dim_range(dim_map
, bmap
->dim
, t
,
3133 pos
, size
- pos
, off
+ pos
+ n
);
3135 off
+= isl_space_dim(res_dim
, t
);
3137 isl_dim_map_div(dim_map
, bmap
, off
);
3139 res
= isl_basic_map_alloc_space(res_dim
,
3140 bmap
->n_div
, bmap
->n_eq
, bmap
->n_ineq
);
3141 if (isl_basic_map_is_rational(bmap
))
3142 res
= isl_basic_map_set_rational(res
);
3143 if (isl_basic_map_plain_is_empty(bmap
)) {
3144 isl_basic_map_free(bmap
);
3146 return isl_basic_map_set_to_empty(res
);
3148 res
= isl_basic_map_add_constraints_dim_map(res
, bmap
, dim_map
);
3149 return isl_basic_map_finalize(res
);
3152 __isl_give isl_basic_set
*isl_basic_set_insert_dims(
3153 __isl_take isl_basic_set
*bset
,
3154 enum isl_dim_type type
, unsigned pos
, unsigned n
)
3156 return isl_basic_map_insert_dims(bset
, type
, pos
, n
);
3159 __isl_give isl_basic_map
*isl_basic_map_add(__isl_take isl_basic_map
*bmap
,
3160 enum isl_dim_type type
, unsigned n
)
3164 return isl_basic_map_insert_dims(bmap
, type
,
3165 isl_basic_map_dim(bmap
, type
), n
);
3168 __isl_give isl_basic_set
*isl_basic_set_add_dims(__isl_take isl_basic_set
*bset
,
3169 enum isl_dim_type type
, unsigned n
)
3173 isl_assert(bset
->ctx
, type
!= isl_dim_in
, goto error
);
3174 return (isl_basic_set
*)isl_basic_map_add((isl_basic_map
*)bset
, type
, n
);
3176 isl_basic_set_free(bset
);
3180 static __isl_give isl_map
*map_space_reset(__isl_take isl_map
*map
,
3181 enum isl_dim_type type
)
3185 if (!map
|| !isl_space_is_named_or_nested(map
->dim
, type
))
3188 space
= isl_map_get_space(map
);
3189 space
= isl_space_reset(space
, type
);
3190 map
= isl_map_reset_space(map
, space
);
3194 __isl_give isl_map
*isl_map_insert_dims(__isl_take isl_map
*map
,
3195 enum isl_dim_type type
, unsigned pos
, unsigned n
)
3200 return map_space_reset(map
, type
);
3202 map
= isl_map_cow(map
);
3206 map
->dim
= isl_space_insert_dims(map
->dim
, type
, pos
, n
);
3210 for (i
= 0; i
< map
->n
; ++i
) {
3211 map
->p
[i
] = isl_basic_map_insert_dims(map
->p
[i
], type
, pos
, n
);
3222 __isl_give isl_set
*isl_set_insert_dims(__isl_take isl_set
*set
,
3223 enum isl_dim_type type
, unsigned pos
, unsigned n
)
3225 return isl_map_insert_dims(set
, type
, pos
, n
);
3228 __isl_give isl_map
*isl_map_add_dims(__isl_take isl_map
*map
,
3229 enum isl_dim_type type
, unsigned n
)
3233 return isl_map_insert_dims(map
, type
, isl_map_dim(map
, type
), n
);
3236 __isl_give isl_set
*isl_set_add_dims(__isl_take isl_set
*set
,
3237 enum isl_dim_type type
, unsigned n
)
3241 isl_assert(set
->ctx
, type
!= isl_dim_in
, goto error
);
3242 return (isl_set
*)isl_map_add_dims((isl_map
*)set
, type
, n
);
3248 __isl_give isl_basic_map
*isl_basic_map_move_dims(
3249 __isl_take isl_basic_map
*bmap
,
3250 enum isl_dim_type dst_type
, unsigned dst_pos
,
3251 enum isl_dim_type src_type
, unsigned src_pos
, unsigned n
)
3253 struct isl_dim_map
*dim_map
;
3254 struct isl_basic_map
*res
;
3255 enum isl_dim_type t
;
3256 unsigned total
, off
;
3263 isl_assert(bmap
->ctx
, src_pos
+ n
<= isl_basic_map_dim(bmap
, src_type
),
3266 if (dst_type
== src_type
&& dst_pos
== src_pos
)
3269 isl_assert(bmap
->ctx
, dst_type
!= src_type
, goto error
);
3271 if (pos(bmap
->dim
, dst_type
) + dst_pos
==
3272 pos(bmap
->dim
, src_type
) + src_pos
+
3273 ((src_type
< dst_type
) ? n
: 0)) {
3274 bmap
= isl_basic_map_cow(bmap
);
3278 bmap
->dim
= isl_space_move_dims(bmap
->dim
, dst_type
, dst_pos
,
3279 src_type
, src_pos
, n
);
3283 bmap
= isl_basic_map_finalize(bmap
);
3288 total
= isl_basic_map_total_dim(bmap
);
3289 dim_map
= isl_dim_map_alloc(bmap
->ctx
, total
);
3292 for (t
= isl_dim_param
; t
<= isl_dim_out
; ++t
) {
3293 unsigned size
= isl_space_dim(bmap
->dim
, t
);
3294 if (t
== dst_type
) {
3295 isl_dim_map_dim_range(dim_map
, bmap
->dim
, t
,
3298 isl_dim_map_dim_range(dim_map
, bmap
->dim
, src_type
,
3301 isl_dim_map_dim_range(dim_map
, bmap
->dim
, t
,
3302 dst_pos
, size
- dst_pos
, off
);
3303 off
+= size
- dst_pos
;
3304 } else if (t
== src_type
) {
3305 isl_dim_map_dim_range(dim_map
, bmap
->dim
, t
,
3308 isl_dim_map_dim_range(dim_map
, bmap
->dim
, t
,
3309 src_pos
+ n
, size
- src_pos
- n
, off
);
3310 off
+= size
- src_pos
- n
;
3312 isl_dim_map_dim(dim_map
, bmap
->dim
, t
, off
);
3316 isl_dim_map_div(dim_map
, bmap
, off
);
3318 res
= isl_basic_map_alloc_space(isl_basic_map_get_space(bmap
),
3319 bmap
->n_div
, bmap
->n_eq
, bmap
->n_ineq
);
3320 bmap
= isl_basic_map_add_constraints_dim_map(res
, bmap
, dim_map
);
3324 bmap
->dim
= isl_space_move_dims(bmap
->dim
, dst_type
, dst_pos
,
3325 src_type
, src_pos
, n
);
3329 ISL_F_CLR(bmap
, ISL_BASIC_MAP_NORMALIZED
);
3330 bmap
= isl_basic_map_gauss(bmap
, NULL
);
3331 bmap
= isl_basic_map_finalize(bmap
);
3335 isl_basic_map_free(bmap
);
3339 __isl_give isl_basic_set
*isl_basic_set_move_dims(__isl_take isl_basic_set
*bset
,
3340 enum isl_dim_type dst_type
, unsigned dst_pos
,
3341 enum isl_dim_type src_type
, unsigned src_pos
, unsigned n
)
3343 return (isl_basic_set
*)isl_basic_map_move_dims(
3344 (isl_basic_map
*)bset
, dst_type
, dst_pos
, src_type
, src_pos
, n
);
3347 __isl_give isl_set
*isl_set_move_dims(__isl_take isl_set
*set
,
3348 enum isl_dim_type dst_type
, unsigned dst_pos
,
3349 enum isl_dim_type src_type
, unsigned src_pos
, unsigned n
)
3353 isl_assert(set
->ctx
, dst_type
!= isl_dim_in
, goto error
);
3354 return (isl_set
*)isl_map_move_dims((isl_map
*)set
, dst_type
, dst_pos
,
3355 src_type
, src_pos
, n
);
3361 __isl_give isl_map
*isl_map_move_dims(__isl_take isl_map
*map
,
3362 enum isl_dim_type dst_type
, unsigned dst_pos
,
3363 enum isl_dim_type src_type
, unsigned src_pos
, unsigned n
)
3372 isl_assert(map
->ctx
, src_pos
+ n
<= isl_map_dim(map
, src_type
),
3375 if (dst_type
== src_type
&& dst_pos
== src_pos
)
3378 isl_assert(map
->ctx
, dst_type
!= src_type
, goto error
);
3380 map
= isl_map_cow(map
);
3384 map
->dim
= isl_space_move_dims(map
->dim
, dst_type
, dst_pos
, src_type
, src_pos
, n
);
3388 for (i
= 0; i
< map
->n
; ++i
) {
3389 map
->p
[i
] = isl_basic_map_move_dims(map
->p
[i
],
3391 src_type
, src_pos
, n
);
3402 /* Move the specified dimensions to the last columns right before
3403 * the divs. Don't change the dimension specification of bmap.
3404 * That's the responsibility of the caller.
3406 static __isl_give isl_basic_map
*move_last(__isl_take isl_basic_map
*bmap
,
3407 enum isl_dim_type type
, unsigned first
, unsigned n
)
3409 struct isl_dim_map
*dim_map
;
3410 struct isl_basic_map
*res
;
3411 enum isl_dim_type t
;
3412 unsigned total
, off
;
3416 if (pos(bmap
->dim
, type
) + first
+ n
==
3417 1 + isl_space_dim(bmap
->dim
, isl_dim_all
))
3420 total
= isl_basic_map_total_dim(bmap
);
3421 dim_map
= isl_dim_map_alloc(bmap
->ctx
, total
);
3424 for (t
= isl_dim_param
; t
<= isl_dim_out
; ++t
) {
3425 unsigned size
= isl_space_dim(bmap
->dim
, t
);
3427 isl_dim_map_dim_range(dim_map
, bmap
->dim
, t
,
3430 isl_dim_map_dim_range(dim_map
, bmap
->dim
, t
,
3431 first
, n
, total
- bmap
->n_div
- n
);
3432 isl_dim_map_dim_range(dim_map
, bmap
->dim
, t
,
3433 first
+ n
, size
- (first
+ n
), off
);
3434 off
+= size
- (first
+ n
);
3436 isl_dim_map_dim(dim_map
, bmap
->dim
, t
, off
);
3440 isl_dim_map_div(dim_map
, bmap
, off
+ n
);
3442 res
= isl_basic_map_alloc_space(isl_basic_map_get_space(bmap
),
3443 bmap
->n_div
, bmap
->n_eq
, bmap
->n_ineq
);
3444 res
= isl_basic_map_add_constraints_dim_map(res
, bmap
, dim_map
);
3448 /* Insert "n" rows in the divs of "bmap".
3450 * The number of columns is not changed, which means that the last
3451 * dimensions of "bmap" are being reintepreted as the new divs.
3452 * The space of "bmap" is not adjusted, however, which means
3453 * that "bmap" is left in an inconsistent state. Removing "n" dimensions
3454 * from the space of "bmap" is the responsibility of the caller.
3456 static __isl_give isl_basic_map
*insert_div_rows(__isl_take isl_basic_map
*bmap
,
3464 bmap
= isl_basic_map_cow(bmap
);
3468 row_size
= 1 + isl_space_dim(bmap
->dim
, isl_dim_all
) + bmap
->extra
;
3469 old
= bmap
->block2
.data
;
3470 bmap
->block2
= isl_blk_extend(bmap
->ctx
, bmap
->block2
,
3471 (bmap
->extra
+ n
) * (1 + row_size
));
3472 if (!bmap
->block2
.data
)
3473 return isl_basic_map_free(bmap
);
3474 new_div
= isl_alloc_array(bmap
->ctx
, isl_int
*, bmap
->extra
+ n
);
3476 return isl_basic_map_free(bmap
);
3477 for (i
= 0; i
< n
; ++i
) {
3478 new_div
[i
] = bmap
->block2
.data
+
3479 (bmap
->extra
+ i
) * (1 + row_size
);
3480 isl_seq_clr(new_div
[i
], 1 + row_size
);
3482 for (i
= 0; i
< bmap
->extra
; ++i
)
3483 new_div
[n
+ i
] = bmap
->block2
.data
+ (bmap
->div
[i
] - old
);
3485 bmap
->div
= new_div
;
3492 /* Turn the n dimensions of type type, starting at first
3493 * into existentially quantified variables.
3495 __isl_give isl_basic_map
*isl_basic_map_project_out(
3496 __isl_take isl_basic_map
*bmap
,
3497 enum isl_dim_type type
, unsigned first
, unsigned n
)
3500 return basic_map_space_reset(bmap
, type
);
3505 if (ISL_F_ISSET(bmap
, ISL_BASIC_MAP_RATIONAL
))
3506 return isl_basic_map_remove_dims(bmap
, type
, first
, n
);
3508 isl_assert(bmap
->ctx
, first
+ n
<= isl_basic_map_dim(bmap
, type
),
3511 bmap
= move_last(bmap
, type
, first
, n
);
3512 bmap
= isl_basic_map_cow(bmap
);
3513 bmap
= insert_div_rows(bmap
, n
);
3517 bmap
->dim
= isl_space_drop_dims(bmap
->dim
, type
, first
, n
);
3520 bmap
= isl_basic_map_simplify(bmap
);
3521 bmap
= isl_basic_map_drop_redundant_divs(bmap
);
3522 return isl_basic_map_finalize(bmap
);
3524 isl_basic_map_free(bmap
);
3528 /* Turn the n dimensions of type type, starting at first
3529 * into existentially quantified variables.
3531 struct isl_basic_set
*isl_basic_set_project_out(struct isl_basic_set
*bset
,
3532 enum isl_dim_type type
, unsigned first
, unsigned n
)
3534 return (isl_basic_set
*)isl_basic_map_project_out(
3535 (isl_basic_map
*)bset
, type
, first
, n
);
3538 /* Turn the n dimensions of type type, starting at first
3539 * into existentially quantified variables.
3541 __isl_give isl_map
*isl_map_project_out(__isl_take isl_map
*map
,
3542 enum isl_dim_type type
, unsigned first
, unsigned n
)
3550 return map_space_reset(map
, type
);
3552 isl_assert(map
->ctx
, first
+ n
<= isl_map_dim(map
, type
), goto error
);
3554 map
= isl_map_cow(map
);
3558 map
->dim
= isl_space_drop_dims(map
->dim
, type
, first
, n
);
3562 for (i
= 0; i
< map
->n
; ++i
) {
3563 map
->p
[i
] = isl_basic_map_project_out(map
->p
[i
], type
, first
, n
);
3574 /* Turn the n dimensions of type type, starting at first
3575 * into existentially quantified variables.
3577 __isl_give isl_set
*isl_set_project_out(__isl_take isl_set
*set
,
3578 enum isl_dim_type type
, unsigned first
, unsigned n
)
3580 return (isl_set
*)isl_map_project_out((isl_map
*)set
, type
, first
, n
);
3583 static struct isl_basic_map
*add_divs(struct isl_basic_map
*bmap
, unsigned n
)
3587 for (i
= 0; i
< n
; ++i
) {
3588 j
= isl_basic_map_alloc_div(bmap
);
3591 isl_seq_clr(bmap
->div
[j
], 1+1+isl_basic_map_total_dim(bmap
));
3595 isl_basic_map_free(bmap
);
3599 struct isl_basic_map
*isl_basic_map_apply_range(
3600 struct isl_basic_map
*bmap1
, struct isl_basic_map
*bmap2
)
3602 isl_space
*dim_result
= NULL
;
3603 struct isl_basic_map
*bmap
;
3604 unsigned n_in
, n_out
, n
, nparam
, total
, pos
;
3605 struct isl_dim_map
*dim_map1
, *dim_map2
;
3607 if (!bmap1
|| !bmap2
)
3609 if (!isl_space_match(bmap1
->dim
, isl_dim_param
,
3610 bmap2
->dim
, isl_dim_param
))
3611 isl_die(isl_basic_map_get_ctx(bmap1
), isl_error_invalid
,
3612 "parameters don't match", goto error
);
3613 if (!isl_space_tuple_is_equal(bmap1
->dim
, isl_dim_out
,
3614 bmap2
->dim
, isl_dim_in
))
3615 isl_die(isl_basic_map_get_ctx(bmap1
), isl_error_invalid
,
3616 "spaces don't match", goto error
);
3618 dim_result
= isl_space_join(isl_space_copy(bmap1
->dim
),
3619 isl_space_copy(bmap2
->dim
));
3621 n_in
= isl_basic_map_n_in(bmap1
);
3622 n_out
= isl_basic_map_n_out(bmap2
);
3623 n
= isl_basic_map_n_out(bmap1
);
3624 nparam
= isl_basic_map_n_param(bmap1
);
3626 total
= nparam
+ n_in
+ n_out
+ bmap1
->n_div
+ bmap2
->n_div
+ n
;
3627 dim_map1
= isl_dim_map_alloc(bmap1
->ctx
, total
);
3628 dim_map2
= isl_dim_map_alloc(bmap1
->ctx
, total
);
3629 isl_dim_map_dim(dim_map1
, bmap1
->dim
, isl_dim_param
, pos
= 0);
3630 isl_dim_map_dim(dim_map2
, bmap2
->dim
, isl_dim_param
, pos
= 0);
3631 isl_dim_map_dim(dim_map1
, bmap1
->dim
, isl_dim_in
, pos
+= nparam
);
3632 isl_dim_map_dim(dim_map2
, bmap2
->dim
, isl_dim_out
, pos
+= n_in
);
3633 isl_dim_map_div(dim_map1
, bmap1
, pos
+= n_out
);
3634 isl_dim_map_div(dim_map2
, bmap2
, pos
+= bmap1
->n_div
);
3635 isl_dim_map_dim(dim_map1
, bmap1
->dim
, isl_dim_out
, pos
+= bmap2
->n_div
);
3636 isl_dim_map_dim(dim_map2
, bmap2
->dim
, isl_dim_in
, pos
);
3638 bmap
= isl_basic_map_alloc_space(dim_result
,
3639 bmap1
->n_div
+ bmap2
->n_div
+ n
,
3640 bmap1
->n_eq
+ bmap2
->n_eq
,
3641 bmap1
->n_ineq
+ bmap2
->n_ineq
);
3642 bmap
= isl_basic_map_add_constraints_dim_map(bmap
, bmap1
, dim_map1
);
3643 bmap
= isl_basic_map_add_constraints_dim_map(bmap
, bmap2
, dim_map2
);
3644 bmap
= add_divs(bmap
, n
);
3645 bmap
= isl_basic_map_simplify(bmap
);
3646 bmap
= isl_basic_map_drop_redundant_divs(bmap
);
3647 return isl_basic_map_finalize(bmap
);
3649 isl_basic_map_free(bmap1
);
3650 isl_basic_map_free(bmap2
);
3654 struct isl_basic_set
*isl_basic_set_apply(
3655 struct isl_basic_set
*bset
, struct isl_basic_map
*bmap
)
3660 isl_assert(bset
->ctx
, isl_basic_map_compatible_domain(bmap
, bset
),
3663 return (struct isl_basic_set
*)
3664 isl_basic_map_apply_range((struct isl_basic_map
*)bset
, bmap
);
3666 isl_basic_set_free(bset
);
3667 isl_basic_map_free(bmap
);
3671 struct isl_basic_map
*isl_basic_map_apply_domain(
3672 struct isl_basic_map
*bmap1
, struct isl_basic_map
*bmap2
)
3674 if (!bmap1
|| !bmap2
)
3677 isl_assert(bmap1
->ctx
,
3678 isl_basic_map_n_in(bmap1
) == isl_basic_map_n_in(bmap2
), goto error
);
3679 isl_assert(bmap1
->ctx
,
3680 isl_basic_map_n_param(bmap1
) == isl_basic_map_n_param(bmap2
),
3683 bmap1
= isl_basic_map_reverse(bmap1
);
3684 bmap1
= isl_basic_map_apply_range(bmap1
, bmap2
);
3685 return isl_basic_map_reverse(bmap1
);
3687 isl_basic_map_free(bmap1
);
3688 isl_basic_map_free(bmap2
);
3692 /* Given two basic maps A -> f(A) and B -> g(B), construct a basic map
3693 * A \cap B -> f(A) + f(B)
3695 struct isl_basic_map
*isl_basic_map_sum(
3696 struct isl_basic_map
*bmap1
, struct isl_basic_map
*bmap2
)
3698 unsigned n_in
, n_out
, nparam
, total
, pos
;
3699 struct isl_basic_map
*bmap
= NULL
;
3700 struct isl_dim_map
*dim_map1
, *dim_map2
;
3703 if (!bmap1
|| !bmap2
)
3706 isl_assert(bmap1
->ctx
, isl_space_is_equal(bmap1
->dim
, bmap2
->dim
),
3709 nparam
= isl_basic_map_n_param(bmap1
);
3710 n_in
= isl_basic_map_n_in(bmap1
);
3711 n_out
= isl_basic_map_n_out(bmap1
);
3713 total
= nparam
+ n_in
+ n_out
+ bmap1
->n_div
+ bmap2
->n_div
+ 2 * n_out
;
3714 dim_map1
= isl_dim_map_alloc(bmap1
->ctx
, total
);
3715 dim_map2
= isl_dim_map_alloc(bmap2
->ctx
, total
);
3716 isl_dim_map_dim(dim_map1
, bmap1
->dim
, isl_dim_param
, pos
= 0);
3717 isl_dim_map_dim(dim_map2
, bmap2
->dim
, isl_dim_param
, pos
);
3718 isl_dim_map_dim(dim_map1
, bmap1
->dim
, isl_dim_in
, pos
+= nparam
);
3719 isl_dim_map_dim(dim_map2
, bmap2
->dim
, isl_dim_in
, pos
);
3720 isl_dim_map_div(dim_map1
, bmap1
, pos
+= n_in
+ n_out
);
3721 isl_dim_map_div(dim_map2
, bmap2
, pos
+= bmap1
->n_div
);
3722 isl_dim_map_dim(dim_map1
, bmap1
->dim
, isl_dim_out
, pos
+= bmap2
->n_div
);
3723 isl_dim_map_dim(dim_map2
, bmap2
->dim
, isl_dim_out
, pos
+= n_out
);
3725 bmap
= isl_basic_map_alloc_space(isl_space_copy(bmap1
->dim
),
3726 bmap1
->n_div
+ bmap2
->n_div
+ 2 * n_out
,
3727 bmap1
->n_eq
+ bmap2
->n_eq
+ n_out
,
3728 bmap1
->n_ineq
+ bmap2
->n_ineq
);
3729 for (i
= 0; i
< n_out
; ++i
) {
3730 int j
= isl_basic_map_alloc_equality(bmap
);
3733 isl_seq_clr(bmap
->eq
[j
], 1+total
);
3734 isl_int_set_si(bmap
->eq
[j
][1+nparam
+n_in
+i
], -1);
3735 isl_int_set_si(bmap
->eq
[j
][1+pos
+i
], 1);
3736 isl_int_set_si(bmap
->eq
[j
][1+pos
-n_out
+i
], 1);
3738 bmap
= isl_basic_map_add_constraints_dim_map(bmap
, bmap1
, dim_map1
);
3739 bmap
= isl_basic_map_add_constraints_dim_map(bmap
, bmap2
, dim_map2
);
3740 bmap
= add_divs(bmap
, 2 * n_out
);
3742 bmap
= isl_basic_map_simplify(bmap
);
3743 return isl_basic_map_finalize(bmap
);
3745 isl_basic_map_free(bmap
);
3746 isl_basic_map_free(bmap1
);
3747 isl_basic_map_free(bmap2
);
3751 /* Given two maps A -> f(A) and B -> g(B), construct a map
3752 * A \cap B -> f(A) + f(B)
3754 struct isl_map
*isl_map_sum(struct isl_map
*map1
, struct isl_map
*map2
)
3756 struct isl_map
*result
;
3762 isl_assert(map1
->ctx
, isl_space_is_equal(map1
->dim
, map2
->dim
), goto error
);
3764 result
= isl_map_alloc_space(isl_space_copy(map1
->dim
),
3765 map1
->n
* map2
->n
, 0);
3768 for (i
= 0; i
< map1
->n
; ++i
)
3769 for (j
= 0; j
< map2
->n
; ++j
) {
3770 struct isl_basic_map
*part
;
3771 part
= isl_basic_map_sum(
3772 isl_basic_map_copy(map1
->p
[i
]),
3773 isl_basic_map_copy(map2
->p
[j
]));
3774 if (isl_basic_map_is_empty(part
))
3775 isl_basic_map_free(part
);
3777 result
= isl_map_add_basic_map(result
, part
);
3790 __isl_give isl_set
*isl_set_sum(__isl_take isl_set
*set1
,
3791 __isl_take isl_set
*set2
)
3793 return (isl_set
*)isl_map_sum((isl_map
*)set1
, (isl_map
*)set2
);
3796 /* Given a basic map A -> f(A), construct A -> -f(A).
3798 struct isl_basic_map
*isl_basic_map_neg(struct isl_basic_map
*bmap
)
3803 bmap
= isl_basic_map_cow(bmap
);
3807 n
= isl_basic_map_dim(bmap
, isl_dim_out
);
3808 off
= isl_basic_map_offset(bmap
, isl_dim_out
);
3809 for (i
= 0; i
< bmap
->n_eq
; ++i
)
3810 for (j
= 0; j
< n
; ++j
)
3811 isl_int_neg(bmap
->eq
[i
][off
+j
], bmap
->eq
[i
][off
+j
]);
3812 for (i
= 0; i
< bmap
->n_ineq
; ++i
)
3813 for (j
= 0; j
< n
; ++j
)
3814 isl_int_neg(bmap
->ineq
[i
][off
+j
], bmap
->ineq
[i
][off
+j
]);
3815 for (i
= 0; i
< bmap
->n_div
; ++i
)
3816 for (j
= 0; j
< n
; ++j
)
3817 isl_int_neg(bmap
->div
[i
][1+off
+j
], bmap
->div
[i
][1+off
+j
]);
3818 bmap
= isl_basic_map_gauss(bmap
, NULL
);
3819 return isl_basic_map_finalize(bmap
);
3822 __isl_give isl_basic_set
*isl_basic_set_neg(__isl_take isl_basic_set
*bset
)
3824 return isl_basic_map_neg(bset
);
3827 /* Given a map A -> f(A), construct A -> -f(A).
3829 struct isl_map
*isl_map_neg(struct isl_map
*map
)
3833 map
= isl_map_cow(map
);
3837 for (i
= 0; i
< map
->n
; ++i
) {
3838 map
->p
[i
] = isl_basic_map_neg(map
->p
[i
]);
3849 __isl_give isl_set
*isl_set_neg(__isl_take isl_set
*set
)
3851 return (isl_set
*)isl_map_neg((isl_map
*)set
);
3854 /* Given a basic map A -> f(A) and an integer d, construct a basic map
3855 * A -> floor(f(A)/d).
3857 struct isl_basic_map
*isl_basic_map_floordiv(struct isl_basic_map
*bmap
,
3860 unsigned n_in
, n_out
, nparam
, total
, pos
;
3861 struct isl_basic_map
*result
= NULL
;
3862 struct isl_dim_map
*dim_map
;
3868 nparam
= isl_basic_map_n_param(bmap
);
3869 n_in
= isl_basic_map_n_in(bmap
);
3870 n_out
= isl_basic_map_n_out(bmap
);
3872 total
= nparam
+ n_in
+ n_out
+ bmap
->n_div
+ n_out
;
3873 dim_map
= isl_dim_map_alloc(bmap
->ctx
, total
);
3874 isl_dim_map_dim(dim_map
, bmap
->dim
, isl_dim_param
, pos
= 0);
3875 isl_dim_map_dim(dim_map
, bmap
->dim
, isl_dim_in
, pos
+= nparam
);
3876 isl_dim_map_div(dim_map
, bmap
, pos
+= n_in
+ n_out
);
3877 isl_dim_map_dim(dim_map
, bmap
->dim
, isl_dim_out
, pos
+= bmap
->n_div
);
3879 result
= isl_basic_map_alloc_space(isl_space_copy(bmap
->dim
),
3880 bmap
->n_div
+ n_out
,
3881 bmap
->n_eq
, bmap
->n_ineq
+ 2 * n_out
);
3882 result
= isl_basic_map_add_constraints_dim_map(result
, bmap
, dim_map
);
3883 result
= add_divs(result
, n_out
);
3884 for (i
= 0; i
< n_out
; ++i
) {
3886 j
= isl_basic_map_alloc_inequality(result
);
3889 isl_seq_clr(result
->ineq
[j
], 1+total
);
3890 isl_int_neg(result
->ineq
[j
][1+nparam
+n_in
+i
], d
);
3891 isl_int_set_si(result
->ineq
[j
][1+pos
+i
], 1);
3892 j
= isl_basic_map_alloc_inequality(result
);
3895 isl_seq_clr(result
->ineq
[j
], 1+total
);
3896 isl_int_set(result
->ineq
[j
][1+nparam
+n_in
+i
], d
);
3897 isl_int_set_si(result
->ineq
[j
][1+pos
+i
], -1);
3898 isl_int_sub_ui(result
->ineq
[j
][0], d
, 1);
3901 result
= isl_basic_map_simplify(result
);
3902 return isl_basic_map_finalize(result
);
3904 isl_basic_map_free(result
);
3908 /* Given a map A -> f(A) and an integer d, construct a map
3909 * A -> floor(f(A)/d).
3911 struct isl_map
*isl_map_floordiv(struct isl_map
*map
, isl_int d
)
3915 map
= isl_map_cow(map
);
3919 ISL_F_CLR(map
, ISL_MAP_DISJOINT
);
3920 ISL_F_CLR(map
, ISL_MAP_NORMALIZED
);
3921 for (i
= 0; i
< map
->n
; ++i
) {
3922 map
->p
[i
] = isl_basic_map_floordiv(map
->p
[i
], d
);
3933 /* Given a map A -> f(A) and an integer d, construct a map
3934 * A -> floor(f(A)/d).
3936 __isl_give isl_map
*isl_map_floordiv_val(__isl_take isl_map
*map
,
3937 __isl_take isl_val
*d
)
3941 if (!isl_val_is_int(d
))
3942 isl_die(isl_val_get_ctx(d
), isl_error_invalid
,
3943 "expecting integer denominator", goto error
);
3944 map
= isl_map_floordiv(map
, d
->n
);
3953 static struct isl_basic_map
*var_equal(struct isl_basic_map
*bmap
, unsigned pos
)
3959 i
= isl_basic_map_alloc_equality(bmap
);
3962 nparam
= isl_basic_map_n_param(bmap
);
3963 n_in
= isl_basic_map_n_in(bmap
);
3964 isl_seq_clr(bmap
->eq
[i
], 1 + isl_basic_map_total_dim(bmap
));
3965 isl_int_set_si(bmap
->eq
[i
][1+nparam
+pos
], -1);
3966 isl_int_set_si(bmap
->eq
[i
][1+nparam
+n_in
+pos
], 1);
3967 return isl_basic_map_finalize(bmap
);
3969 isl_basic_map_free(bmap
);
3973 /* Add a constraints to "bmap" expressing i_pos < o_pos
3975 static struct isl_basic_map
*var_less(struct isl_basic_map
*bmap
, unsigned pos
)
3981 i
= isl_basic_map_alloc_inequality(bmap
);
3984 nparam
= isl_basic_map_n_param(bmap
);
3985 n_in
= isl_basic_map_n_in(bmap
);
3986 isl_seq_clr(bmap
->ineq
[i
], 1 + isl_basic_map_total_dim(bmap
));
3987 isl_int_set_si(bmap
->ineq
[i
][0], -1);
3988 isl_int_set_si(bmap
->ineq
[i
][1+nparam
+pos
], -1);
3989 isl_int_set_si(bmap
->ineq
[i
][1+nparam
+n_in
+pos
], 1);
3990 return isl_basic_map_finalize(bmap
);
3992 isl_basic_map_free(bmap
);
3996 /* Add a constraint to "bmap" expressing i_pos <= o_pos
3998 static __isl_give isl_basic_map
*var_less_or_equal(
3999 __isl_take isl_basic_map
*bmap
, unsigned pos
)
4005 i
= isl_basic_map_alloc_inequality(bmap
);
4008 nparam
= isl_basic_map_n_param(bmap
);
4009 n_in
= isl_basic_map_n_in(bmap
);
4010 isl_seq_clr(bmap
->ineq
[i
], 1 + isl_basic_map_total_dim(bmap
));
4011 isl_int_set_si(bmap
->ineq
[i
][1+nparam
+pos
], -1);
4012 isl_int_set_si(bmap
->ineq
[i
][1+nparam
+n_in
+pos
], 1);
4013 return isl_basic_map_finalize(bmap
);
4015 isl_basic_map_free(bmap
);
4019 /* Add a constraints to "bmap" expressing i_pos > o_pos
4021 static struct isl_basic_map
*var_more(struct isl_basic_map
*bmap
, unsigned pos
)
4027 i
= isl_basic_map_alloc_inequality(bmap
);
4030 nparam
= isl_basic_map_n_param(bmap
);
4031 n_in
= isl_basic_map_n_in(bmap
);
4032 isl_seq_clr(bmap
->ineq
[i
], 1 + isl_basic_map_total_dim(bmap
));
4033 isl_int_set_si(bmap
->ineq
[i
][0], -1);
4034 isl_int_set_si(bmap
->ineq
[i
][1+nparam
+pos
], 1);
4035 isl_int_set_si(bmap
->ineq
[i
][1+nparam
+n_in
+pos
], -1);
4036 return isl_basic_map_finalize(bmap
);
4038 isl_basic_map_free(bmap
);
4042 /* Add a constraint to "bmap" expressing i_pos >= o_pos
4044 static __isl_give isl_basic_map
*var_more_or_equal(
4045 __isl_take isl_basic_map
*bmap
, unsigned pos
)
4051 i
= isl_basic_map_alloc_inequality(bmap
);
4054 nparam
= isl_basic_map_n_param(bmap
);
4055 n_in
= isl_basic_map_n_in(bmap
);
4056 isl_seq_clr(bmap
->ineq
[i
], 1 + isl_basic_map_total_dim(bmap
));
4057 isl_int_set_si(bmap
->ineq
[i
][1+nparam
+pos
], 1);
4058 isl_int_set_si(bmap
->ineq
[i
][1+nparam
+n_in
+pos
], -1);
4059 return isl_basic_map_finalize(bmap
);
4061 isl_basic_map_free(bmap
);
4065 __isl_give isl_basic_map
*isl_basic_map_equal(
4066 __isl_take isl_space
*dim
, unsigned n_equal
)
4069 struct isl_basic_map
*bmap
;
4070 bmap
= isl_basic_map_alloc_space(dim
, 0, n_equal
, 0);
4073 for (i
= 0; i
< n_equal
&& bmap
; ++i
)
4074 bmap
= var_equal(bmap
, i
);
4075 return isl_basic_map_finalize(bmap
);
4078 /* Return a relation on of dimension "dim" expressing i_[0..pos] << o_[0..pos]
4080 __isl_give isl_basic_map
*isl_basic_map_less_at(__isl_take isl_space
*dim
,
4084 struct isl_basic_map
*bmap
;
4085 bmap
= isl_basic_map_alloc_space(dim
, 0, pos
, 1);
4088 for (i
= 0; i
< pos
&& bmap
; ++i
)
4089 bmap
= var_equal(bmap
, i
);
4091 bmap
= var_less(bmap
, pos
);
4092 return isl_basic_map_finalize(bmap
);
4095 /* Return a relation on of dimension "dim" expressing i_[0..pos] <<= o_[0..pos]
4097 __isl_give isl_basic_map
*isl_basic_map_less_or_equal_at(
4098 __isl_take isl_space
*dim
, unsigned pos
)
4101 isl_basic_map
*bmap
;
4103 bmap
= isl_basic_map_alloc_space(dim
, 0, pos
, 1);
4104 for (i
= 0; i
< pos
; ++i
)
4105 bmap
= var_equal(bmap
, i
);
4106 bmap
= var_less_or_equal(bmap
, pos
);
4107 return isl_basic_map_finalize(bmap
);
4110 /* Return a relation on pairs of sets of dimension "dim" expressing i_pos > o_pos
4112 __isl_give isl_basic_map
*isl_basic_map_more_at(__isl_take isl_space
*dim
,
4116 struct isl_basic_map
*bmap
;
4117 bmap
= isl_basic_map_alloc_space(dim
, 0, pos
, 1);
4120 for (i
= 0; i
< pos
&& bmap
; ++i
)
4121 bmap
= var_equal(bmap
, i
);
4123 bmap
= var_more(bmap
, pos
);
4124 return isl_basic_map_finalize(bmap
);
4127 /* Return a relation on of dimension "dim" expressing i_[0..pos] >>= o_[0..pos]
4129 __isl_give isl_basic_map
*isl_basic_map_more_or_equal_at(
4130 __isl_take isl_space
*dim
, unsigned pos
)
4133 isl_basic_map
*bmap
;
4135 bmap
= isl_basic_map_alloc_space(dim
, 0, pos
, 1);
4136 for (i
= 0; i
< pos
; ++i
)
4137 bmap
= var_equal(bmap
, i
);
4138 bmap
= var_more_or_equal(bmap
, pos
);
4139 return isl_basic_map_finalize(bmap
);
4142 static __isl_give isl_map
*map_lex_lte_first(__isl_take isl_space
*dims
,
4143 unsigned n
, int equal
)
4145 struct isl_map
*map
;
4148 if (n
== 0 && equal
)
4149 return isl_map_universe(dims
);
4151 map
= isl_map_alloc_space(isl_space_copy(dims
), n
, ISL_MAP_DISJOINT
);
4153 for (i
= 0; i
+ 1 < n
; ++i
)
4154 map
= isl_map_add_basic_map(map
,
4155 isl_basic_map_less_at(isl_space_copy(dims
), i
));
4158 map
= isl_map_add_basic_map(map
,
4159 isl_basic_map_less_or_equal_at(dims
, n
- 1));
4161 map
= isl_map_add_basic_map(map
,
4162 isl_basic_map_less_at(dims
, n
- 1));
4164 isl_space_free(dims
);
4169 static __isl_give isl_map
*map_lex_lte(__isl_take isl_space
*dims
, int equal
)
4173 return map_lex_lte_first(dims
, dims
->n_out
, equal
);
4176 __isl_give isl_map
*isl_map_lex_lt_first(__isl_take isl_space
*dim
, unsigned n
)
4178 return map_lex_lte_first(dim
, n
, 0);
4181 __isl_give isl_map
*isl_map_lex_le_first(__isl_take isl_space
*dim
, unsigned n
)
4183 return map_lex_lte_first(dim
, n
, 1);
4186 __isl_give isl_map
*isl_map_lex_lt(__isl_take isl_space
*set_dim
)
4188 return map_lex_lte(isl_space_map_from_set(set_dim
), 0);
4191 __isl_give isl_map
*isl_map_lex_le(__isl_take isl_space
*set_dim
)
4193 return map_lex_lte(isl_space_map_from_set(set_dim
), 1);
4196 static __isl_give isl_map
*map_lex_gte_first(__isl_take isl_space
*dims
,
4197 unsigned n
, int equal
)
4199 struct isl_map
*map
;
4202 if (n
== 0 && equal
)
4203 return isl_map_universe(dims
);
4205 map
= isl_map_alloc_space(isl_space_copy(dims
), n
, ISL_MAP_DISJOINT
);
4207 for (i
= 0; i
+ 1 < n
; ++i
)
4208 map
= isl_map_add_basic_map(map
,
4209 isl_basic_map_more_at(isl_space_copy(dims
), i
));
4212 map
= isl_map_add_basic_map(map
,
4213 isl_basic_map_more_or_equal_at(dims
, n
- 1));
4215 map
= isl_map_add_basic_map(map
,
4216 isl_basic_map_more_at(dims
, n
- 1));
4218 isl_space_free(dims
);
4223 static __isl_give isl_map
*map_lex_gte(__isl_take isl_space
*dims
, int equal
)
4227 return map_lex_gte_first(dims
, dims
->n_out
, equal
);
4230 __isl_give isl_map
*isl_map_lex_gt_first(__isl_take isl_space
*dim
, unsigned n
)
4232 return map_lex_gte_first(dim
, n
, 0);
4235 __isl_give isl_map
*isl_map_lex_ge_first(__isl_take isl_space
*dim
, unsigned n
)
4237 return map_lex_gte_first(dim
, n
, 1);
4240 __isl_give isl_map
*isl_map_lex_gt(__isl_take isl_space
*set_dim
)
4242 return map_lex_gte(isl_space_map_from_set(set_dim
), 0);
4245 __isl_give isl_map
*isl_map_lex_ge(__isl_take isl_space
*set_dim
)
4247 return map_lex_gte(isl_space_map_from_set(set_dim
), 1);
4250 __isl_give isl_map
*isl_set_lex_le_set(__isl_take isl_set
*set1
,
4251 __isl_take isl_set
*set2
)
4254 map
= isl_map_lex_le(isl_set_get_space(set1
));
4255 map
= isl_map_intersect_domain(map
, set1
);
4256 map
= isl_map_intersect_range(map
, set2
);
4260 __isl_give isl_map
*isl_set_lex_lt_set(__isl_take isl_set
*set1
,
4261 __isl_take isl_set
*set2
)
4264 map
= isl_map_lex_lt(isl_set_get_space(set1
));
4265 map
= isl_map_intersect_domain(map
, set1
);
4266 map
= isl_map_intersect_range(map
, set2
);
4270 __isl_give isl_map
*isl_set_lex_ge_set(__isl_take isl_set
*set1
,
4271 __isl_take isl_set
*set2
)
4274 map
= isl_map_lex_ge(isl_set_get_space(set1
));
4275 map
= isl_map_intersect_domain(map
, set1
);
4276 map
= isl_map_intersect_range(map
, set2
);
4280 __isl_give isl_map
*isl_set_lex_gt_set(__isl_take isl_set
*set1
,
4281 __isl_take isl_set
*set2
)
4284 map
= isl_map_lex_gt(isl_set_get_space(set1
));
4285 map
= isl_map_intersect_domain(map
, set1
);
4286 map
= isl_map_intersect_range(map
, set2
);
4290 __isl_give isl_map
*isl_map_lex_le_map(__isl_take isl_map
*map1
,
4291 __isl_take isl_map
*map2
)
4294 map
= isl_map_lex_le(isl_space_range(isl_map_get_space(map1
)));
4295 map
= isl_map_apply_domain(map
, isl_map_reverse(map1
));
4296 map
= isl_map_apply_range(map
, isl_map_reverse(map2
));
4300 __isl_give isl_map
*isl_map_lex_lt_map(__isl_take isl_map
*map1
,
4301 __isl_take isl_map
*map2
)
4304 map
= isl_map_lex_lt(isl_space_range(isl_map_get_space(map1
)));
4305 map
= isl_map_apply_domain(map
, isl_map_reverse(map1
));
4306 map
= isl_map_apply_range(map
, isl_map_reverse(map2
));
4310 __isl_give isl_map
*isl_map_lex_ge_map(__isl_take isl_map
*map1
,
4311 __isl_take isl_map
*map2
)
4314 map
= isl_map_lex_ge(isl_space_range(isl_map_get_space(map1
)));
4315 map
= isl_map_apply_domain(map
, isl_map_reverse(map1
));
4316 map
= isl_map_apply_range(map
, isl_map_reverse(map2
));
4320 __isl_give isl_map
*isl_map_lex_gt_map(__isl_take isl_map
*map1
,
4321 __isl_take isl_map
*map2
)
4324 map
= isl_map_lex_gt(isl_space_range(isl_map_get_space(map1
)));
4325 map
= isl_map_apply_domain(map
, isl_map_reverse(map1
));
4326 map
= isl_map_apply_range(map
, isl_map_reverse(map2
));
4330 __isl_give isl_basic_map
*isl_basic_map_from_basic_set(
4331 __isl_take isl_basic_set
*bset
, __isl_take isl_space
*dim
)
4333 struct isl_basic_map
*bmap
;
4335 bset
= isl_basic_set_cow(bset
);
4339 isl_assert(bset
->ctx
, isl_space_compatible(bset
->dim
, dim
), goto error
);
4340 isl_space_free(bset
->dim
);
4341 bmap
= (struct isl_basic_map
*) bset
;
4343 return isl_basic_map_finalize(bmap
);
4345 isl_basic_set_free(bset
);
4346 isl_space_free(dim
);
4350 struct isl_basic_set
*isl_basic_set_from_basic_map(struct isl_basic_map
*bmap
)
4354 if (bmap
->dim
->n_in
== 0)
4355 return (struct isl_basic_set
*)bmap
;
4356 bmap
= isl_basic_map_cow(bmap
);
4359 bmap
->dim
= isl_space_as_set_space(bmap
->dim
);
4362 bmap
= isl_basic_map_finalize(bmap
);
4363 return (struct isl_basic_set
*)bmap
;
4365 isl_basic_map_free(bmap
);
4369 /* For a div d = floor(f/m), add the constraint
4373 static int add_upper_div_constraint(__isl_keep isl_basic_map
*bmap
,
4374 unsigned pos
, isl_int
*div
)
4377 unsigned total
= isl_basic_map_total_dim(bmap
);
4379 i
= isl_basic_map_alloc_inequality(bmap
);
4382 isl_seq_cpy(bmap
->ineq
[i
], div
+ 1, 1 + total
);
4383 isl_int_neg(bmap
->ineq
[i
][1 + pos
], div
[0]);
4388 /* For a div d = floor(f/m), add the constraint
4390 * -(f-(n-1)) + m d >= 0
4392 static int add_lower_div_constraint(__isl_keep isl_basic_map
*bmap
,
4393 unsigned pos
, isl_int
*div
)
4396 unsigned total
= isl_basic_map_total_dim(bmap
);
4398 i
= isl_basic_map_alloc_inequality(bmap
);
4401 isl_seq_neg(bmap
->ineq
[i
], div
+ 1, 1 + total
);
4402 isl_int_set(bmap
->ineq
[i
][1 + pos
], div
[0]);
4403 isl_int_add(bmap
->ineq
[i
][0], bmap
->ineq
[i
][0], bmap
->ineq
[i
][1 + pos
]);
4404 isl_int_sub_ui(bmap
->ineq
[i
][0], bmap
->ineq
[i
][0], 1);
4409 /* For a div d = floor(f/m), add the constraints
4412 * -(f-(n-1)) + m d >= 0
4414 * Note that the second constraint is the negation of
4418 int isl_basic_map_add_div_constraints_var(__isl_keep isl_basic_map
*bmap
,
4419 unsigned pos
, isl_int
*div
)
4421 if (add_upper_div_constraint(bmap
, pos
, div
) < 0)
4423 if (add_lower_div_constraint(bmap
, pos
, div
) < 0)
4428 int isl_basic_set_add_div_constraints_var(__isl_keep isl_basic_set
*bset
,
4429 unsigned pos
, isl_int
*div
)
4431 return isl_basic_map_add_div_constraints_var((isl_basic_map
*)bset
,
4435 int isl_basic_map_add_div_constraints(struct isl_basic_map
*bmap
, unsigned div
)
4437 unsigned total
= isl_basic_map_total_dim(bmap
);
4438 unsigned div_pos
= total
- bmap
->n_div
+ div
;
4440 return isl_basic_map_add_div_constraints_var(bmap
, div_pos
,
4444 /* For each known div d = floor(f/m), add the constraints
4447 * -(f-(n-1)) + m d >= 0
4449 __isl_give isl_basic_map
*isl_basic_map_add_known_div_constraints(
4450 __isl_take isl_basic_map
*bmap
)
4457 n_div
= isl_basic_map_dim(bmap
, isl_dim_div
);
4460 bmap
= isl_basic_map_cow(bmap
);
4461 bmap
= isl_basic_map_extend_constraints(bmap
, 0, 2 * n_div
);
4464 for (i
= 0; i
< n_div
; ++i
) {
4465 if (isl_int_is_zero(bmap
->div
[i
][0]))
4467 if (isl_basic_map_add_div_constraints(bmap
, i
) < 0)
4468 return isl_basic_map_free(bmap
);
4471 bmap
= isl_basic_map_remove_duplicate_constraints(bmap
, NULL
, 0);
4472 bmap
= isl_basic_map_finalize(bmap
);
4476 /* Add the div constraint of sign "sign" for div "div" of "bmap".
4478 * In particular, if this div is of the form d = floor(f/m),
4479 * then add the constraint
4483 * if sign < 0 or the constraint
4485 * -(f-(n-1)) + m d >= 0
4489 int isl_basic_map_add_div_constraint(__isl_keep isl_basic_map
*bmap
,
4490 unsigned div
, int sign
)
4498 total
= isl_basic_map_total_dim(bmap
);
4499 div_pos
= total
- bmap
->n_div
+ div
;
4502 return add_upper_div_constraint(bmap
, div_pos
, bmap
->div
[div
]);
4504 return add_lower_div_constraint(bmap
, div_pos
, bmap
->div
[div
]);
4507 int isl_basic_set_add_div_constraints(struct isl_basic_set
*bset
, unsigned div
)
4509 return isl_basic_map_add_div_constraints(bset
, div
);
4512 struct isl_basic_set
*isl_basic_map_underlying_set(
4513 struct isl_basic_map
*bmap
)
4517 if (bmap
->dim
->nparam
== 0 && bmap
->dim
->n_in
== 0 &&
4519 !isl_space_is_named_or_nested(bmap
->dim
, isl_dim_in
) &&
4520 !isl_space_is_named_or_nested(bmap
->dim
, isl_dim_out
))
4521 return (struct isl_basic_set
*)bmap
;
4522 bmap
= isl_basic_map_cow(bmap
);
4525 bmap
->dim
= isl_space_underlying(bmap
->dim
, bmap
->n_div
);
4528 bmap
->extra
-= bmap
->n_div
;
4530 bmap
= isl_basic_map_finalize(bmap
);
4531 return (struct isl_basic_set
*)bmap
;
4533 isl_basic_map_free(bmap
);
4537 __isl_give isl_basic_set
*isl_basic_set_underlying_set(
4538 __isl_take isl_basic_set
*bset
)
4540 return isl_basic_map_underlying_set((isl_basic_map
*)bset
);
4543 /* Replace each element in "list" by the result of applying
4544 * isl_basic_set_underlying_set to the element.
4546 __isl_give isl_basic_set_list
*isl_basic_set_list_underlying_set(
4547 __isl_take isl_basic_set_list
*list
)
4554 n
= isl_basic_set_list_n_basic_set(list
);
4555 for (i
= 0; i
< n
; ++i
) {
4556 isl_basic_set
*bset
;
4558 bset
= isl_basic_set_list_get_basic_set(list
, i
);
4559 bset
= isl_basic_set_underlying_set(bset
);
4560 list
= isl_basic_set_list_set_basic_set(list
, i
, bset
);
4566 struct isl_basic_map
*isl_basic_map_overlying_set(
4567 struct isl_basic_set
*bset
, struct isl_basic_map
*like
)
4569 struct isl_basic_map
*bmap
;
4570 struct isl_ctx
*ctx
;
4577 isl_assert(ctx
, bset
->n_div
== 0, goto error
);
4578 isl_assert(ctx
, isl_basic_set_n_param(bset
) == 0, goto error
);
4579 isl_assert(ctx
, bset
->dim
->n_out
== isl_basic_map_total_dim(like
),
4581 if (isl_space_is_equal(bset
->dim
, like
->dim
) && like
->n_div
== 0) {
4582 isl_basic_map_free(like
);
4583 return (struct isl_basic_map
*)bset
;
4585 bset
= isl_basic_set_cow(bset
);
4588 total
= bset
->dim
->n_out
+ bset
->extra
;
4589 bmap
= (struct isl_basic_map
*)bset
;
4590 isl_space_free(bmap
->dim
);
4591 bmap
->dim
= isl_space_copy(like
->dim
);
4594 bmap
->n_div
= like
->n_div
;
4595 bmap
->extra
+= like
->n_div
;
4599 ltotal
= total
- bmap
->extra
+ like
->extra
;
4602 bmap
->block2
= isl_blk_extend(ctx
, bmap
->block2
,
4603 bmap
->extra
* (1 + 1 + total
));
4604 if (isl_blk_is_error(bmap
->block2
))
4606 div
= isl_realloc_array(ctx
, bmap
->div
, isl_int
*, bmap
->extra
);
4610 for (i
= 0; i
< bmap
->extra
; ++i
)
4611 bmap
->div
[i
] = bmap
->block2
.data
+ i
* (1 + 1 + total
);
4612 for (i
= 0; i
< like
->n_div
; ++i
) {
4613 isl_seq_cpy(bmap
->div
[i
], like
->div
[i
], 1 + 1 + ltotal
);
4614 isl_seq_clr(bmap
->div
[i
]+1+1+ltotal
, total
- ltotal
);
4616 bmap
= isl_basic_map_add_known_div_constraints(bmap
);
4618 isl_basic_map_free(like
);
4619 bmap
= isl_basic_map_simplify(bmap
);
4620 bmap
= isl_basic_map_finalize(bmap
);
4623 isl_basic_map_free(like
);
4624 isl_basic_set_free(bset
);
4628 struct isl_basic_set
*isl_basic_set_from_underlying_set(
4629 struct isl_basic_set
*bset
, struct isl_basic_set
*like
)
4631 return (struct isl_basic_set
*)
4632 isl_basic_map_overlying_set(bset
, (struct isl_basic_map
*)like
);
4635 struct isl_set
*isl_set_from_underlying_set(
4636 struct isl_set
*set
, struct isl_basic_set
*like
)
4642 isl_assert(set
->ctx
, set
->dim
->n_out
== isl_basic_set_total_dim(like
),
4644 if (isl_space_is_equal(set
->dim
, like
->dim
) && like
->n_div
== 0) {
4645 isl_basic_set_free(like
);
4648 set
= isl_set_cow(set
);
4651 for (i
= 0; i
< set
->n
; ++i
) {
4652 set
->p
[i
] = isl_basic_set_from_underlying_set(set
->p
[i
],
4653 isl_basic_set_copy(like
));
4657 isl_space_free(set
->dim
);
4658 set
->dim
= isl_space_copy(like
->dim
);
4661 isl_basic_set_free(like
);
4664 isl_basic_set_free(like
);
4669 struct isl_set
*isl_map_underlying_set(struct isl_map
*map
)
4673 map
= isl_map_cow(map
);
4676 map
->dim
= isl_space_cow(map
->dim
);
4680 for (i
= 1; i
< map
->n
; ++i
)
4681 isl_assert(map
->ctx
, map
->p
[0]->n_div
== map
->p
[i
]->n_div
,
4683 for (i
= 0; i
< map
->n
; ++i
) {
4684 map
->p
[i
] = (struct isl_basic_map
*)
4685 isl_basic_map_underlying_set(map
->p
[i
]);
4690 map
->dim
= isl_space_underlying(map
->dim
, 0);
4692 isl_space_free(map
->dim
);
4693 map
->dim
= isl_space_copy(map
->p
[0]->dim
);
4697 return (struct isl_set
*)map
;
4703 struct isl_set
*isl_set_to_underlying_set(struct isl_set
*set
)
4705 return (struct isl_set
*)isl_map_underlying_set((struct isl_map
*)set
);
4708 __isl_give isl_basic_map
*isl_basic_map_reset_space(
4709 __isl_take isl_basic_map
*bmap
, __isl_take isl_space
*dim
)
4711 bmap
= isl_basic_map_cow(bmap
);
4715 isl_space_free(bmap
->dim
);
4718 bmap
= isl_basic_map_finalize(bmap
);
4722 isl_basic_map_free(bmap
);
4723 isl_space_free(dim
);
4727 __isl_give isl_basic_set
*isl_basic_set_reset_space(
4728 __isl_take isl_basic_set
*bset
, __isl_take isl_space
*dim
)
4730 return (isl_basic_set
*)isl_basic_map_reset_space((isl_basic_map
*)bset
,
4734 __isl_give isl_map
*isl_map_reset_space(__isl_take isl_map
*map
,
4735 __isl_take isl_space
*dim
)
4739 map
= isl_map_cow(map
);
4743 for (i
= 0; i
< map
->n
; ++i
) {
4744 map
->p
[i
] = isl_basic_map_reset_space(map
->p
[i
],
4745 isl_space_copy(dim
));
4749 isl_space_free(map
->dim
);
4755 isl_space_free(dim
);
4759 __isl_give isl_set
*isl_set_reset_space(__isl_take isl_set
*set
,
4760 __isl_take isl_space
*dim
)
4762 return (struct isl_set
*) isl_map_reset_space((struct isl_map
*)set
, dim
);
4765 /* Compute the parameter domain of the given basic set.
4767 __isl_give isl_basic_set
*isl_basic_set_params(__isl_take isl_basic_set
*bset
)
4772 if (isl_basic_set_is_params(bset
))
4775 n
= isl_basic_set_dim(bset
, isl_dim_set
);
4776 bset
= isl_basic_set_project_out(bset
, isl_dim_set
, 0, n
);
4777 space
= isl_basic_set_get_space(bset
);
4778 space
= isl_space_params(space
);
4779 bset
= isl_basic_set_reset_space(bset
, space
);
4783 /* Construct a zero-dimensional basic set with the given parameter domain.
4785 __isl_give isl_basic_set
*isl_basic_set_from_params(
4786 __isl_take isl_basic_set
*bset
)
4789 space
= isl_basic_set_get_space(bset
);
4790 space
= isl_space_set_from_params(space
);
4791 bset
= isl_basic_set_reset_space(bset
, space
);
4795 /* Compute the parameter domain of the given set.
4797 __isl_give isl_set
*isl_set_params(__isl_take isl_set
*set
)
4802 if (isl_set_is_params(set
))
4805 n
= isl_set_dim(set
, isl_dim_set
);
4806 set
= isl_set_project_out(set
, isl_dim_set
, 0, n
);
4807 space
= isl_set_get_space(set
);
4808 space
= isl_space_params(space
);
4809 set
= isl_set_reset_space(set
, space
);
4813 /* Construct a zero-dimensional set with the given parameter domain.
4815 __isl_give isl_set
*isl_set_from_params(__isl_take isl_set
*set
)
4818 space
= isl_set_get_space(set
);
4819 space
= isl_space_set_from_params(space
);
4820 set
= isl_set_reset_space(set
, space
);
4824 /* Compute the parameter domain of the given map.
4826 __isl_give isl_set
*isl_map_params(__isl_take isl_map
*map
)
4831 n
= isl_map_dim(map
, isl_dim_in
);
4832 map
= isl_map_project_out(map
, isl_dim_in
, 0, n
);
4833 n
= isl_map_dim(map
, isl_dim_out
);
4834 map
= isl_map_project_out(map
, isl_dim_out
, 0, n
);
4835 space
= isl_map_get_space(map
);
4836 space
= isl_space_params(space
);
4837 map
= isl_map_reset_space(map
, space
);
4841 struct isl_basic_set
*isl_basic_map_domain(struct isl_basic_map
*bmap
)
4844 struct isl_basic_set
*domain
;
4850 dim
= isl_space_domain(isl_basic_map_get_space(bmap
));
4852 n_in
= isl_basic_map_n_in(bmap
);
4853 n_out
= isl_basic_map_n_out(bmap
);
4854 domain
= isl_basic_set_from_basic_map(bmap
);
4855 domain
= isl_basic_set_project_out(domain
, isl_dim_set
, n_in
, n_out
);
4857 domain
= isl_basic_set_reset_space(domain
, dim
);
4862 int isl_basic_map_may_be_set(__isl_keep isl_basic_map
*bmap
)
4866 return isl_space_may_be_set(bmap
->dim
);
4869 /* Is this basic map actually a set?
4870 * Users should never call this function. Outside of isl,
4871 * the type should indicate whether something is a set or a map.
4873 int isl_basic_map_is_set(__isl_keep isl_basic_map
*bmap
)
4877 return isl_space_is_set(bmap
->dim
);
4880 struct isl_basic_set
*isl_basic_map_range(struct isl_basic_map
*bmap
)
4884 if (isl_basic_map_is_set(bmap
))
4886 return isl_basic_map_domain(isl_basic_map_reverse(bmap
));
4889 __isl_give isl_basic_map
*isl_basic_map_domain_map(
4890 __isl_take isl_basic_map
*bmap
)
4894 isl_basic_map
*domain
;
4895 int nparam
, n_in
, n_out
;
4898 nparam
= isl_basic_map_dim(bmap
, isl_dim_param
);
4899 n_in
= isl_basic_map_dim(bmap
, isl_dim_in
);
4900 n_out
= isl_basic_map_dim(bmap
, isl_dim_out
);
4902 dim
= isl_space_from_range(isl_space_domain(isl_basic_map_get_space(bmap
)));
4903 domain
= isl_basic_map_universe(dim
);
4905 bmap
= isl_basic_map_from_domain(isl_basic_map_wrap(bmap
));
4906 bmap
= isl_basic_map_apply_range(bmap
, domain
);
4907 bmap
= isl_basic_map_extend_constraints(bmap
, n_in
, 0);
4909 total
= isl_basic_map_total_dim(bmap
);
4911 for (i
= 0; i
< n_in
; ++i
) {
4912 k
= isl_basic_map_alloc_equality(bmap
);
4915 isl_seq_clr(bmap
->eq
[k
], 1 + total
);
4916 isl_int_set_si(bmap
->eq
[k
][1 + nparam
+ i
], -1);
4917 isl_int_set_si(bmap
->eq
[k
][1 + nparam
+ n_in
+ n_out
+ i
], 1);
4920 bmap
= isl_basic_map_gauss(bmap
, NULL
);
4921 return isl_basic_map_finalize(bmap
);
4923 isl_basic_map_free(bmap
);
4927 __isl_give isl_basic_map
*isl_basic_map_range_map(
4928 __isl_take isl_basic_map
*bmap
)
4932 isl_basic_map
*range
;
4933 int nparam
, n_in
, n_out
;
4936 nparam
= isl_basic_map_dim(bmap
, isl_dim_param
);
4937 n_in
= isl_basic_map_dim(bmap
, isl_dim_in
);
4938 n_out
= isl_basic_map_dim(bmap
, isl_dim_out
);
4940 dim
= isl_space_from_range(isl_space_range(isl_basic_map_get_space(bmap
)));
4941 range
= isl_basic_map_universe(dim
);
4943 bmap
= isl_basic_map_from_domain(isl_basic_map_wrap(bmap
));
4944 bmap
= isl_basic_map_apply_range(bmap
, range
);
4945 bmap
= isl_basic_map_extend_constraints(bmap
, n_out
, 0);
4947 total
= isl_basic_map_total_dim(bmap
);
4949 for (i
= 0; i
< n_out
; ++i
) {
4950 k
= isl_basic_map_alloc_equality(bmap
);
4953 isl_seq_clr(bmap
->eq
[k
], 1 + total
);
4954 isl_int_set_si(bmap
->eq
[k
][1 + nparam
+ n_in
+ i
], -1);
4955 isl_int_set_si(bmap
->eq
[k
][1 + nparam
+ n_in
+ n_out
+ i
], 1);
4958 bmap
= isl_basic_map_gauss(bmap
, NULL
);
4959 return isl_basic_map_finalize(bmap
);
4961 isl_basic_map_free(bmap
);
4965 int isl_map_may_be_set(__isl_keep isl_map
*map
)
4969 return isl_space_may_be_set(map
->dim
);
4972 /* Is this map actually a set?
4973 * Users should never call this function. Outside of isl,
4974 * the type should indicate whether something is a set or a map.
4976 int isl_map_is_set(__isl_keep isl_map
*map
)
4980 return isl_space_is_set(map
->dim
);
4983 struct isl_set
*isl_map_range(struct isl_map
*map
)
4986 struct isl_set
*set
;
4990 if (isl_map_is_set(map
))
4991 return (isl_set
*)map
;
4993 map
= isl_map_cow(map
);
4997 set
= (struct isl_set
*) map
;
4998 set
->dim
= isl_space_range(set
->dim
);
5001 for (i
= 0; i
< map
->n
; ++i
) {
5002 set
->p
[i
] = isl_basic_map_range(map
->p
[i
]);
5006 ISL_F_CLR(set
, ISL_MAP_DISJOINT
);
5007 ISL_F_CLR(set
, ISL_SET_NORMALIZED
);
5014 __isl_give isl_map
*isl_map_domain_map(__isl_take isl_map
*map
)
5018 map
= isl_map_cow(map
);
5022 map
->dim
= isl_space_domain_map(map
->dim
);
5025 for (i
= 0; i
< map
->n
; ++i
) {
5026 map
->p
[i
] = isl_basic_map_domain_map(map
->p
[i
]);
5030 ISL_F_CLR(map
, ISL_MAP_DISJOINT
);
5031 ISL_F_CLR(map
, ISL_MAP_NORMALIZED
);
5038 __isl_give isl_map
*isl_map_range_map(__isl_take isl_map
*map
)
5041 isl_space
*range_dim
;
5043 map
= isl_map_cow(map
);
5047 range_dim
= isl_space_range(isl_map_get_space(map
));
5048 range_dim
= isl_space_from_range(range_dim
);
5049 map
->dim
= isl_space_from_domain(isl_space_wrap(map
->dim
));
5050 map
->dim
= isl_space_join(map
->dim
, range_dim
);
5053 for (i
= 0; i
< map
->n
; ++i
) {
5054 map
->p
[i
] = isl_basic_map_range_map(map
->p
[i
]);
5058 ISL_F_CLR(map
, ISL_MAP_DISJOINT
);
5059 ISL_F_CLR(map
, ISL_MAP_NORMALIZED
);
5066 __isl_give isl_map
*isl_map_from_set(__isl_take isl_set
*set
,
5067 __isl_take isl_space
*dim
)
5070 struct isl_map
*map
= NULL
;
5072 set
= isl_set_cow(set
);
5075 isl_assert(set
->ctx
, isl_space_compatible(set
->dim
, dim
), goto error
);
5076 map
= (struct isl_map
*)set
;
5077 for (i
= 0; i
< set
->n
; ++i
) {
5078 map
->p
[i
] = isl_basic_map_from_basic_set(
5079 set
->p
[i
], isl_space_copy(dim
));
5083 isl_space_free(map
->dim
);
5087 isl_space_free(dim
);
5092 __isl_give isl_basic_map
*isl_basic_map_from_domain(
5093 __isl_take isl_basic_set
*bset
)
5095 return isl_basic_map_reverse(isl_basic_map_from_range(bset
));
5098 __isl_give isl_basic_map
*isl_basic_map_from_range(
5099 __isl_take isl_basic_set
*bset
)
5102 space
= isl_basic_set_get_space(bset
);
5103 space
= isl_space_from_range(space
);
5104 bset
= isl_basic_set_reset_space(bset
, space
);
5105 return (isl_basic_map
*)bset
;
5108 /* Create a relation with the given set as range.
5109 * The domain of the created relation is a zero-dimensional
5110 * flat anonymous space.
5112 __isl_give isl_map
*isl_map_from_range(__isl_take isl_set
*set
)
5115 space
= isl_set_get_space(set
);
5116 space
= isl_space_from_range(space
);
5117 set
= isl_set_reset_space(set
, space
);
5118 return (struct isl_map
*)set
;
5121 /* Create a relation with the given set as domain.
5122 * The range of the created relation is a zero-dimensional
5123 * flat anonymous space.
5125 __isl_give isl_map
*isl_map_from_domain(__isl_take isl_set
*set
)
5127 return isl_map_reverse(isl_map_from_range(set
));
5130 __isl_give isl_basic_map
*isl_basic_map_from_domain_and_range(
5131 __isl_take isl_basic_set
*domain
, __isl_take isl_basic_set
*range
)
5133 return isl_basic_map_apply_range(isl_basic_map_reverse(domain
), range
);
5136 __isl_give isl_map
*isl_map_from_domain_and_range(__isl_take isl_set
*domain
,
5137 __isl_take isl_set
*range
)
5139 return isl_map_apply_range(isl_map_reverse(domain
), range
);
5142 struct isl_set
*isl_set_from_map(struct isl_map
*map
)
5145 struct isl_set
*set
= NULL
;
5149 map
= isl_map_cow(map
);
5152 map
->dim
= isl_space_as_set_space(map
->dim
);
5155 set
= (struct isl_set
*)map
;
5156 for (i
= 0; i
< map
->n
; ++i
) {
5157 set
->p
[i
] = isl_basic_set_from_basic_map(map
->p
[i
]);
5167 __isl_give isl_map
*isl_map_alloc_space(__isl_take isl_space
*dim
, int n
,
5170 struct isl_map
*map
;
5175 isl_die(dim
->ctx
, isl_error_internal
,
5176 "negative number of basic maps", goto error
);
5177 map
= isl_alloc(dim
->ctx
, struct isl_map
,
5178 sizeof(struct isl_map
) +
5179 (n
- 1) * sizeof(struct isl_basic_map
*));
5183 map
->ctx
= dim
->ctx
;
5184 isl_ctx_ref(map
->ctx
);
5192 isl_space_free(dim
);
5196 struct isl_map
*isl_map_alloc(struct isl_ctx
*ctx
,
5197 unsigned nparam
, unsigned in
, unsigned out
, int n
,
5200 struct isl_map
*map
;
5203 dims
= isl_space_alloc(ctx
, nparam
, in
, out
);
5207 map
= isl_map_alloc_space(dims
, n
, flags
);
5211 __isl_give isl_basic_map
*isl_basic_map_empty(__isl_take isl_space
*dim
)
5213 struct isl_basic_map
*bmap
;
5214 bmap
= isl_basic_map_alloc_space(dim
, 0, 1, 0);
5215 bmap
= isl_basic_map_set_to_empty(bmap
);
5219 __isl_give isl_basic_set
*isl_basic_set_empty(__isl_take isl_space
*dim
)
5221 struct isl_basic_set
*bset
;
5222 bset
= isl_basic_set_alloc_space(dim
, 0, 1, 0);
5223 bset
= isl_basic_set_set_to_empty(bset
);
5227 struct isl_basic_map
*isl_basic_map_empty_like(struct isl_basic_map
*model
)
5229 struct isl_basic_map
*bmap
;
5232 bmap
= isl_basic_map_alloc_space(isl_space_copy(model
->dim
), 0, 1, 0);
5233 bmap
= isl_basic_map_set_to_empty(bmap
);
5237 struct isl_basic_map
*isl_basic_map_empty_like_map(struct isl_map
*model
)
5239 struct isl_basic_map
*bmap
;
5242 bmap
= isl_basic_map_alloc_space(isl_space_copy(model
->dim
), 0, 1, 0);
5243 bmap
= isl_basic_map_set_to_empty(bmap
);
5247 struct isl_basic_set
*isl_basic_set_empty_like(struct isl_basic_set
*model
)
5249 struct isl_basic_set
*bset
;
5252 bset
= isl_basic_set_alloc_space(isl_space_copy(model
->dim
), 0, 1, 0);
5253 bset
= isl_basic_set_set_to_empty(bset
);
5257 __isl_give isl_basic_map
*isl_basic_map_universe(__isl_take isl_space
*dim
)
5259 struct isl_basic_map
*bmap
;
5260 bmap
= isl_basic_map_alloc_space(dim
, 0, 0, 0);
5261 bmap
= isl_basic_map_finalize(bmap
);
5265 __isl_give isl_basic_set
*isl_basic_set_universe(__isl_take isl_space
*dim
)
5267 struct isl_basic_set
*bset
;
5268 bset
= isl_basic_set_alloc_space(dim
, 0, 0, 0);
5269 bset
= isl_basic_set_finalize(bset
);
5273 __isl_give isl_basic_map
*isl_basic_map_nat_universe(__isl_take isl_space
*dim
)
5276 unsigned total
= isl_space_dim(dim
, isl_dim_all
);
5277 isl_basic_map
*bmap
;
5279 bmap
= isl_basic_map_alloc_space(dim
, 0, 0, total
);
5280 for (i
= 0; i
< total
; ++i
) {
5281 int k
= isl_basic_map_alloc_inequality(bmap
);
5284 isl_seq_clr(bmap
->ineq
[k
], 1 + total
);
5285 isl_int_set_si(bmap
->ineq
[k
][1 + i
], 1);
5289 isl_basic_map_free(bmap
);
5293 __isl_give isl_basic_set
*isl_basic_set_nat_universe(__isl_take isl_space
*dim
)
5295 return isl_basic_map_nat_universe(dim
);
5298 __isl_give isl_map
*isl_map_nat_universe(__isl_take isl_space
*dim
)
5300 return isl_map_from_basic_map(isl_basic_map_nat_universe(dim
));
5303 __isl_give isl_set
*isl_set_nat_universe(__isl_take isl_space
*dim
)
5305 return isl_map_nat_universe(dim
);
5308 __isl_give isl_basic_map
*isl_basic_map_universe_like(
5309 __isl_keep isl_basic_map
*model
)
5313 return isl_basic_map_alloc_space(isl_space_copy(model
->dim
), 0, 0, 0);
5316 struct isl_basic_set
*isl_basic_set_universe_like(struct isl_basic_set
*model
)
5320 return isl_basic_set_alloc_space(isl_space_copy(model
->dim
), 0, 0, 0);
5323 __isl_give isl_basic_set
*isl_basic_set_universe_like_set(
5324 __isl_keep isl_set
*model
)
5328 return isl_basic_set_alloc_space(isl_space_copy(model
->dim
), 0, 0, 0);
5331 __isl_give isl_map
*isl_map_empty(__isl_take isl_space
*dim
)
5333 return isl_map_alloc_space(dim
, 0, ISL_MAP_DISJOINT
);
5336 struct isl_map
*isl_map_empty_like(struct isl_map
*model
)
5340 return isl_map_alloc_space(isl_space_copy(model
->dim
), 0, ISL_MAP_DISJOINT
);
5343 struct isl_map
*isl_map_empty_like_basic_map(struct isl_basic_map
*model
)
5347 return isl_map_alloc_space(isl_space_copy(model
->dim
), 0, ISL_MAP_DISJOINT
);
5350 __isl_give isl_set
*isl_set_empty(__isl_take isl_space
*dim
)
5352 return isl_set_alloc_space(dim
, 0, ISL_MAP_DISJOINT
);
5355 struct isl_set
*isl_set_empty_like(struct isl_set
*model
)
5359 return isl_set_empty(isl_space_copy(model
->dim
));
5362 __isl_give isl_map
*isl_map_universe(__isl_take isl_space
*dim
)
5364 struct isl_map
*map
;
5367 map
= isl_map_alloc_space(isl_space_copy(dim
), 1, ISL_MAP_DISJOINT
);
5368 map
= isl_map_add_basic_map(map
, isl_basic_map_universe(dim
));
5372 __isl_give isl_set
*isl_set_universe(__isl_take isl_space
*dim
)
5374 struct isl_set
*set
;
5377 set
= isl_set_alloc_space(isl_space_copy(dim
), 1, ISL_MAP_DISJOINT
);
5378 set
= isl_set_add_basic_set(set
, isl_basic_set_universe(dim
));
5382 __isl_give isl_set
*isl_set_universe_like(__isl_keep isl_set
*model
)
5386 return isl_set_universe(isl_space_copy(model
->dim
));
5389 struct isl_map
*isl_map_dup(struct isl_map
*map
)
5392 struct isl_map
*dup
;
5396 dup
= isl_map_alloc_space(isl_space_copy(map
->dim
), map
->n
, map
->flags
);
5397 for (i
= 0; i
< map
->n
; ++i
)
5398 dup
= isl_map_add_basic_map(dup
, isl_basic_map_copy(map
->p
[i
]));
5402 __isl_give isl_map
*isl_map_add_basic_map(__isl_take isl_map
*map
,
5403 __isl_take isl_basic_map
*bmap
)
5407 if (isl_basic_map_plain_is_empty(bmap
)) {
5408 isl_basic_map_free(bmap
);
5411 isl_assert(map
->ctx
, isl_space_is_equal(map
->dim
, bmap
->dim
), goto error
);
5412 isl_assert(map
->ctx
, map
->n
< map
->size
, goto error
);
5413 map
->p
[map
->n
] = bmap
;
5415 ISL_F_CLR(map
, ISL_MAP_NORMALIZED
);
5421 isl_basic_map_free(bmap
);
5425 __isl_null isl_map
*isl_map_free(__isl_take isl_map
*map
)
5435 isl_ctx_deref(map
->ctx
);
5436 for (i
= 0; i
< map
->n
; ++i
)
5437 isl_basic_map_free(map
->p
[i
]);
5438 isl_space_free(map
->dim
);
5444 struct isl_map
*isl_map_extend(struct isl_map
*base
,
5445 unsigned nparam
, unsigned n_in
, unsigned n_out
)
5449 base
= isl_map_cow(base
);
5453 base
->dim
= isl_space_extend(base
->dim
, nparam
, n_in
, n_out
);
5456 for (i
= 0; i
< base
->n
; ++i
) {
5457 base
->p
[i
] = isl_basic_map_extend_space(base
->p
[i
],
5458 isl_space_copy(base
->dim
), 0, 0, 0);
5468 struct isl_set
*isl_set_extend(struct isl_set
*base
,
5469 unsigned nparam
, unsigned dim
)
5471 return (struct isl_set
*)isl_map_extend((struct isl_map
*)base
,
5475 static struct isl_basic_map
*isl_basic_map_fix_pos_si(
5476 struct isl_basic_map
*bmap
, unsigned pos
, int value
)
5480 bmap
= isl_basic_map_cow(bmap
);
5481 bmap
= isl_basic_map_extend_constraints(bmap
, 1, 0);
5482 j
= isl_basic_map_alloc_equality(bmap
);
5485 isl_seq_clr(bmap
->eq
[j
] + 1, isl_basic_map_total_dim(bmap
));
5486 isl_int_set_si(bmap
->eq
[j
][pos
], -1);
5487 isl_int_set_si(bmap
->eq
[j
][0], value
);
5488 bmap
= isl_basic_map_simplify(bmap
);
5489 return isl_basic_map_finalize(bmap
);
5491 isl_basic_map_free(bmap
);
5495 static __isl_give isl_basic_map
*isl_basic_map_fix_pos(
5496 __isl_take isl_basic_map
*bmap
, unsigned pos
, isl_int value
)
5500 bmap
= isl_basic_map_cow(bmap
);
5501 bmap
= isl_basic_map_extend_constraints(bmap
, 1, 0);
5502 j
= isl_basic_map_alloc_equality(bmap
);
5505 isl_seq_clr(bmap
->eq
[j
] + 1, isl_basic_map_total_dim(bmap
));
5506 isl_int_set_si(bmap
->eq
[j
][pos
], -1);
5507 isl_int_set(bmap
->eq
[j
][0], value
);
5508 bmap
= isl_basic_map_simplify(bmap
);
5509 return isl_basic_map_finalize(bmap
);
5511 isl_basic_map_free(bmap
);
5515 struct isl_basic_map
*isl_basic_map_fix_si(struct isl_basic_map
*bmap
,
5516 enum isl_dim_type type
, unsigned pos
, int value
)
5520 isl_assert(bmap
->ctx
, pos
< isl_basic_map_dim(bmap
, type
), goto error
);
5521 return isl_basic_map_fix_pos_si(bmap
,
5522 isl_basic_map_offset(bmap
, type
) + pos
, value
);
5524 isl_basic_map_free(bmap
);
5528 __isl_give isl_basic_map
*isl_basic_map_fix(__isl_take isl_basic_map
*bmap
,
5529 enum isl_dim_type type
, unsigned pos
, isl_int value
)
5533 isl_assert(bmap
->ctx
, pos
< isl_basic_map_dim(bmap
, type
), goto error
);
5534 return isl_basic_map_fix_pos(bmap
,
5535 isl_basic_map_offset(bmap
, type
) + pos
, value
);
5537 isl_basic_map_free(bmap
);
5541 /* Fix the value of the variable at position "pos" of type "type" of "bmap"
5542 * to be equal to "v".
5544 __isl_give isl_basic_map
*isl_basic_map_fix_val(__isl_take isl_basic_map
*bmap
,
5545 enum isl_dim_type type
, unsigned pos
, __isl_take isl_val
*v
)
5549 if (!isl_val_is_int(v
))
5550 isl_die(isl_basic_map_get_ctx(bmap
), isl_error_invalid
,
5551 "expecting integer value", goto error
);
5552 if (pos
>= isl_basic_map_dim(bmap
, type
))
5553 isl_die(isl_basic_map_get_ctx(bmap
), isl_error_invalid
,
5554 "index out of bounds", goto error
);
5555 pos
+= isl_basic_map_offset(bmap
, type
);
5556 bmap
= isl_basic_map_fix_pos(bmap
, pos
, v
->n
);
5560 isl_basic_map_free(bmap
);
5565 /* Fix the value of the variable at position "pos" of type "type" of "bset"
5566 * to be equal to "v".
5568 __isl_give isl_basic_set
*isl_basic_set_fix_val(__isl_take isl_basic_set
*bset
,
5569 enum isl_dim_type type
, unsigned pos
, __isl_take isl_val
*v
)
5571 return isl_basic_map_fix_val(bset
, type
, pos
, v
);
5574 struct isl_basic_set
*isl_basic_set_fix_si(struct isl_basic_set
*bset
,
5575 enum isl_dim_type type
, unsigned pos
, int value
)
5577 return (struct isl_basic_set
*)
5578 isl_basic_map_fix_si((struct isl_basic_map
*)bset
,
5582 __isl_give isl_basic_set
*isl_basic_set_fix(__isl_take isl_basic_set
*bset
,
5583 enum isl_dim_type type
, unsigned pos
, isl_int value
)
5585 return (struct isl_basic_set
*)
5586 isl_basic_map_fix((struct isl_basic_map
*)bset
,
5590 struct isl_basic_map
*isl_basic_map_fix_input_si(struct isl_basic_map
*bmap
,
5591 unsigned input
, int value
)
5593 return isl_basic_map_fix_si(bmap
, isl_dim_in
, input
, value
);
5596 struct isl_basic_set
*isl_basic_set_fix_dim_si(struct isl_basic_set
*bset
,
5597 unsigned dim
, int value
)
5599 return (struct isl_basic_set
*)
5600 isl_basic_map_fix_si((struct isl_basic_map
*)bset
,
5601 isl_dim_set
, dim
, value
);
5604 static int remove_if_empty(__isl_keep isl_map
*map
, int i
)
5606 int empty
= isl_basic_map_plain_is_empty(map
->p
[i
]);
5613 isl_basic_map_free(map
->p
[i
]);
5614 if (i
!= map
->n
- 1) {
5615 ISL_F_CLR(map
, ISL_MAP_NORMALIZED
);
5616 map
->p
[i
] = map
->p
[map
->n
- 1];
5623 /* Perform "fn" on each basic map of "map", where we may not be holding
5624 * the only reference to "map".
5625 * In particular, "fn" should be a semantics preserving operation
5626 * that we want to apply to all copies of "map". We therefore need
5627 * to be careful not to modify "map" in a way that breaks "map"
5628 * in case anything goes wrong.
5630 __isl_give isl_map
*isl_map_inline_foreach_basic_map(__isl_take isl_map
*map
,
5631 __isl_give isl_basic_map
*(*fn
)(__isl_take isl_basic_map
*bmap
))
5633 struct isl_basic_map
*bmap
;
5639 for (i
= map
->n
- 1; i
>= 0; --i
) {
5640 bmap
= isl_basic_map_copy(map
->p
[i
]);
5644 isl_basic_map_free(map
->p
[i
]);
5646 if (remove_if_empty(map
, i
) < 0)
5656 struct isl_map
*isl_map_fix_si(struct isl_map
*map
,
5657 enum isl_dim_type type
, unsigned pos
, int value
)
5661 map
= isl_map_cow(map
);
5665 isl_assert(map
->ctx
, pos
< isl_map_dim(map
, type
), goto error
);
5666 for (i
= map
->n
- 1; i
>= 0; --i
) {
5667 map
->p
[i
] = isl_basic_map_fix_si(map
->p
[i
], type
, pos
, value
);
5668 if (remove_if_empty(map
, i
) < 0)
5671 ISL_F_CLR(map
, ISL_MAP_NORMALIZED
);
5678 __isl_give isl_set
*isl_set_fix_si(__isl_take isl_set
*set
,
5679 enum isl_dim_type type
, unsigned pos
, int value
)
5681 return (struct isl_set
*)
5682 isl_map_fix_si((struct isl_map
*)set
, type
, pos
, value
);
5685 __isl_give isl_map
*isl_map_fix(__isl_take isl_map
*map
,
5686 enum isl_dim_type type
, unsigned pos
, isl_int value
)
5690 map
= isl_map_cow(map
);
5694 isl_assert(map
->ctx
, pos
< isl_map_dim(map
, type
), goto error
);
5695 for (i
= 0; i
< map
->n
; ++i
) {
5696 map
->p
[i
] = isl_basic_map_fix(map
->p
[i
], type
, pos
, value
);
5700 ISL_F_CLR(map
, ISL_MAP_NORMALIZED
);
5707 __isl_give isl_set
*isl_set_fix(__isl_take isl_set
*set
,
5708 enum isl_dim_type type
, unsigned pos
, isl_int value
)
5710 return (struct isl_set
*)isl_map_fix((isl_map
*)set
, type
, pos
, value
);
5713 /* Fix the value of the variable at position "pos" of type "type" of "map"
5714 * to be equal to "v".
5716 __isl_give isl_map
*isl_map_fix_val(__isl_take isl_map
*map
,
5717 enum isl_dim_type type
, unsigned pos
, __isl_take isl_val
*v
)
5721 map
= isl_map_cow(map
);
5725 if (!isl_val_is_int(v
))
5726 isl_die(isl_map_get_ctx(map
), isl_error_invalid
,
5727 "expecting integer value", goto error
);
5728 if (pos
>= isl_map_dim(map
, type
))
5729 isl_die(isl_map_get_ctx(map
), isl_error_invalid
,
5730 "index out of bounds", goto error
);
5731 for (i
= map
->n
- 1; i
>= 0; --i
) {
5732 map
->p
[i
] = isl_basic_map_fix_val(map
->p
[i
], type
, pos
,
5734 if (remove_if_empty(map
, i
) < 0)
5737 ISL_F_CLR(map
, ISL_MAP_NORMALIZED
);
5746 /* Fix the value of the variable at position "pos" of type "type" of "set"
5747 * to be equal to "v".
5749 __isl_give isl_set
*isl_set_fix_val(__isl_take isl_set
*set
,
5750 enum isl_dim_type type
, unsigned pos
, __isl_take isl_val
*v
)
5752 return isl_map_fix_val(set
, type
, pos
, v
);
5755 struct isl_map
*isl_map_fix_input_si(struct isl_map
*map
,
5756 unsigned input
, int value
)
5758 return isl_map_fix_si(map
, isl_dim_in
, input
, value
);
5761 struct isl_set
*isl_set_fix_dim_si(struct isl_set
*set
, unsigned dim
, int value
)
5763 return (struct isl_set
*)
5764 isl_map_fix_si((struct isl_map
*)set
, isl_dim_set
, dim
, value
);
5767 static __isl_give isl_basic_map
*basic_map_bound_si(
5768 __isl_take isl_basic_map
*bmap
,
5769 enum isl_dim_type type
, unsigned pos
, int value
, int upper
)
5775 isl_assert(bmap
->ctx
, pos
< isl_basic_map_dim(bmap
, type
), goto error
);
5776 pos
+= isl_basic_map_offset(bmap
, type
);
5777 bmap
= isl_basic_map_cow(bmap
);
5778 bmap
= isl_basic_map_extend_constraints(bmap
, 0, 1);
5779 j
= isl_basic_map_alloc_inequality(bmap
);
5782 isl_seq_clr(bmap
->ineq
[j
], 1 + isl_basic_map_total_dim(bmap
));
5784 isl_int_set_si(bmap
->ineq
[j
][pos
], -1);
5785 isl_int_set_si(bmap
->ineq
[j
][0], value
);
5787 isl_int_set_si(bmap
->ineq
[j
][pos
], 1);
5788 isl_int_set_si(bmap
->ineq
[j
][0], -value
);
5790 bmap
= isl_basic_map_simplify(bmap
);
5791 return isl_basic_map_finalize(bmap
);
5793 isl_basic_map_free(bmap
);
5797 __isl_give isl_basic_map
*isl_basic_map_lower_bound_si(
5798 __isl_take isl_basic_map
*bmap
,
5799 enum isl_dim_type type
, unsigned pos
, int value
)
5801 return basic_map_bound_si(bmap
, type
, pos
, value
, 0);
5804 /* Constrain the values of the given dimension to be no greater than "value".
5806 __isl_give isl_basic_map
*isl_basic_map_upper_bound_si(
5807 __isl_take isl_basic_map
*bmap
,
5808 enum isl_dim_type type
, unsigned pos
, int value
)
5810 return basic_map_bound_si(bmap
, type
, pos
, value
, 1);
5813 struct isl_basic_set
*isl_basic_set_lower_bound_dim(struct isl_basic_set
*bset
,
5814 unsigned dim
, isl_int value
)
5818 bset
= isl_basic_set_cow(bset
);
5819 bset
= isl_basic_set_extend_constraints(bset
, 0, 1);
5820 j
= isl_basic_set_alloc_inequality(bset
);
5823 isl_seq_clr(bset
->ineq
[j
], 1 + isl_basic_set_total_dim(bset
));
5824 isl_int_set_si(bset
->ineq
[j
][1 + isl_basic_set_n_param(bset
) + dim
], 1);
5825 isl_int_neg(bset
->ineq
[j
][0], value
);
5826 bset
= isl_basic_set_simplify(bset
);
5827 return isl_basic_set_finalize(bset
);
5829 isl_basic_set_free(bset
);
5833 static __isl_give isl_map
*map_bound_si(__isl_take isl_map
*map
,
5834 enum isl_dim_type type
, unsigned pos
, int value
, int upper
)
5838 map
= isl_map_cow(map
);
5842 isl_assert(map
->ctx
, pos
< isl_map_dim(map
, type
), goto error
);
5843 for (i
= 0; i
< map
->n
; ++i
) {
5844 map
->p
[i
] = basic_map_bound_si(map
->p
[i
],
5845 type
, pos
, value
, upper
);
5849 ISL_F_CLR(map
, ISL_MAP_NORMALIZED
);
5856 __isl_give isl_map
*isl_map_lower_bound_si(__isl_take isl_map
*map
,
5857 enum isl_dim_type type
, unsigned pos
, int value
)
5859 return map_bound_si(map
, type
, pos
, value
, 0);
5862 __isl_give isl_map
*isl_map_upper_bound_si(__isl_take isl_map
*map
,
5863 enum isl_dim_type type
, unsigned pos
, int value
)
5865 return map_bound_si(map
, type
, pos
, value
, 1);
5868 __isl_give isl_set
*isl_set_lower_bound_si(__isl_take isl_set
*set
,
5869 enum isl_dim_type type
, unsigned pos
, int value
)
5871 return (struct isl_set
*)
5872 isl_map_lower_bound_si((struct isl_map
*)set
, type
, pos
, value
);
5875 __isl_give isl_set
*isl_set_upper_bound_si(__isl_take isl_set
*set
,
5876 enum isl_dim_type type
, unsigned pos
, int value
)
5878 return isl_map_upper_bound_si(set
, type
, pos
, value
);
5881 /* Bound the given variable of "bmap" from below (or above is "upper"
5882 * is set) to "value".
5884 static __isl_give isl_basic_map
*basic_map_bound(
5885 __isl_take isl_basic_map
*bmap
,
5886 enum isl_dim_type type
, unsigned pos
, isl_int value
, int upper
)
5892 if (pos
>= isl_basic_map_dim(bmap
, type
))
5893 isl_die(bmap
->ctx
, isl_error_invalid
,
5894 "index out of bounds", goto error
);
5895 pos
+= isl_basic_map_offset(bmap
, type
);
5896 bmap
= isl_basic_map_cow(bmap
);
5897 bmap
= isl_basic_map_extend_constraints(bmap
, 0, 1);
5898 j
= isl_basic_map_alloc_inequality(bmap
);
5901 isl_seq_clr(bmap
->ineq
[j
], 1 + isl_basic_map_total_dim(bmap
));
5903 isl_int_set_si(bmap
->ineq
[j
][pos
], -1);
5904 isl_int_set(bmap
->ineq
[j
][0], value
);
5906 isl_int_set_si(bmap
->ineq
[j
][pos
], 1);
5907 isl_int_neg(bmap
->ineq
[j
][0], value
);
5909 bmap
= isl_basic_map_simplify(bmap
);
5910 return isl_basic_map_finalize(bmap
);
5912 isl_basic_map_free(bmap
);
5916 /* Bound the given variable of "map" from below (or above is "upper"
5917 * is set) to "value".
5919 static __isl_give isl_map
*map_bound(__isl_take isl_map
*map
,
5920 enum isl_dim_type type
, unsigned pos
, isl_int value
, int upper
)
5924 map
= isl_map_cow(map
);
5928 if (pos
>= isl_map_dim(map
, type
))
5929 isl_die(map
->ctx
, isl_error_invalid
,
5930 "index out of bounds", goto error
);
5931 for (i
= map
->n
- 1; i
>= 0; --i
) {
5932 map
->p
[i
] = basic_map_bound(map
->p
[i
], type
, pos
, value
, upper
);
5933 if (remove_if_empty(map
, i
) < 0)
5936 ISL_F_CLR(map
, ISL_MAP_NORMALIZED
);
5943 __isl_give isl_map
*isl_map_lower_bound(__isl_take isl_map
*map
,
5944 enum isl_dim_type type
, unsigned pos
, isl_int value
)
5946 return map_bound(map
, type
, pos
, value
, 0);
5949 __isl_give isl_map
*isl_map_upper_bound(__isl_take isl_map
*map
,
5950 enum isl_dim_type type
, unsigned pos
, isl_int value
)
5952 return map_bound(map
, type
, pos
, value
, 1);
5955 __isl_give isl_set
*isl_set_lower_bound(__isl_take isl_set
*set
,
5956 enum isl_dim_type type
, unsigned pos
, isl_int value
)
5958 return isl_map_lower_bound(set
, type
, pos
, value
);
5961 __isl_give isl_set
*isl_set_upper_bound(__isl_take isl_set
*set
,
5962 enum isl_dim_type type
, unsigned pos
, isl_int value
)
5964 return isl_map_upper_bound(set
, type
, pos
, value
);
5967 /* Force the values of the variable at position "pos" of type "type" of "set"
5968 * to be no smaller than "value".
5970 __isl_give isl_set
*isl_set_lower_bound_val(__isl_take isl_set
*set
,
5971 enum isl_dim_type type
, unsigned pos
, __isl_take isl_val
*value
)
5975 if (!isl_val_is_int(value
))
5976 isl_die(isl_set_get_ctx(set
), isl_error_invalid
,
5977 "expecting integer value", goto error
);
5978 set
= isl_set_lower_bound(set
, type
, pos
, value
->n
);
5979 isl_val_free(value
);
5982 isl_val_free(value
);
5987 /* Force the values of the variable at position "pos" of type "type" of "set"
5988 * to be no greater than "value".
5990 __isl_give isl_set
*isl_set_upper_bound_val(__isl_take isl_set
*set
,
5991 enum isl_dim_type type
, unsigned pos
, __isl_take isl_val
*value
)
5995 if (!isl_val_is_int(value
))
5996 isl_die(isl_set_get_ctx(set
), isl_error_invalid
,
5997 "expecting integer value", goto error
);
5998 set
= isl_set_upper_bound(set
, type
, pos
, value
->n
);
5999 isl_val_free(value
);
6002 isl_val_free(value
);
6007 struct isl_set
*isl_set_lower_bound_dim(struct isl_set
*set
, unsigned dim
,
6012 set
= isl_set_cow(set
);
6016 isl_assert(set
->ctx
, dim
< isl_set_n_dim(set
), goto error
);
6017 for (i
= 0; i
< set
->n
; ++i
) {
6018 set
->p
[i
] = isl_basic_set_lower_bound_dim(set
->p
[i
], dim
, value
);
6028 struct isl_map
*isl_map_reverse(struct isl_map
*map
)
6032 map
= isl_map_cow(map
);
6036 map
->dim
= isl_space_reverse(map
->dim
);
6039 for (i
= 0; i
< map
->n
; ++i
) {
6040 map
->p
[i
] = isl_basic_map_reverse(map
->p
[i
]);
6044 ISL_F_CLR(map
, ISL_MAP_NORMALIZED
);
6051 static struct isl_map
*isl_basic_map_partial_lexopt(
6052 struct isl_basic_map
*bmap
, struct isl_basic_set
*dom
,
6053 struct isl_set
**empty
, int max
)
6055 return isl_tab_basic_map_partial_lexopt(bmap
, dom
, empty
, max
);
6058 struct isl_map
*isl_basic_map_partial_lexmax(
6059 struct isl_basic_map
*bmap
, struct isl_basic_set
*dom
,
6060 struct isl_set
**empty
)
6062 return isl_basic_map_partial_lexopt(bmap
, dom
, empty
, 1);
6065 struct isl_map
*isl_basic_map_partial_lexmin(
6066 struct isl_basic_map
*bmap
, struct isl_basic_set
*dom
,
6067 struct isl_set
**empty
)
6069 return isl_basic_map_partial_lexopt(bmap
, dom
, empty
, 0);
6072 struct isl_set
*isl_basic_set_partial_lexmin(
6073 struct isl_basic_set
*bset
, struct isl_basic_set
*dom
,
6074 struct isl_set
**empty
)
6076 return (struct isl_set
*)
6077 isl_basic_map_partial_lexmin((struct isl_basic_map
*)bset
,
6081 struct isl_set
*isl_basic_set_partial_lexmax(
6082 struct isl_basic_set
*bset
, struct isl_basic_set
*dom
,
6083 struct isl_set
**empty
)
6085 return (struct isl_set
*)
6086 isl_basic_map_partial_lexmax((struct isl_basic_map
*)bset
,
6090 __isl_give isl_pw_multi_aff
*isl_basic_map_partial_lexmin_pw_multi_aff(
6091 __isl_take isl_basic_map
*bmap
, __isl_take isl_basic_set
*dom
,
6092 __isl_give isl_set
**empty
)
6094 return isl_basic_map_partial_lexopt_pw_multi_aff(bmap
, dom
, empty
, 0);
6097 __isl_give isl_pw_multi_aff
*isl_basic_map_partial_lexmax_pw_multi_aff(
6098 __isl_take isl_basic_map
*bmap
, __isl_take isl_basic_set
*dom
,
6099 __isl_give isl_set
**empty
)
6101 return isl_basic_map_partial_lexopt_pw_multi_aff(bmap
, dom
, empty
, 1);
6104 __isl_give isl_pw_multi_aff
*isl_basic_set_partial_lexmin_pw_multi_aff(
6105 __isl_take isl_basic_set
*bset
, __isl_take isl_basic_set
*dom
,
6106 __isl_give isl_set
**empty
)
6108 return isl_basic_map_partial_lexmin_pw_multi_aff(bset
, dom
, empty
);
6111 __isl_give isl_pw_multi_aff
*isl_basic_set_partial_lexmax_pw_multi_aff(
6112 __isl_take isl_basic_set
*bset
, __isl_take isl_basic_set
*dom
,
6113 __isl_give isl_set
**empty
)
6115 return isl_basic_map_partial_lexmax_pw_multi_aff(bset
, dom
, empty
);
6118 __isl_give isl_pw_multi_aff
*isl_basic_map_lexopt_pw_multi_aff(
6119 __isl_take isl_basic_map
*bmap
, int max
)
6121 isl_basic_set
*dom
= NULL
;
6122 isl_space
*dom_space
;
6126 dom_space
= isl_space_domain(isl_space_copy(bmap
->dim
));
6127 dom
= isl_basic_set_universe(dom_space
);
6128 return isl_basic_map_partial_lexopt_pw_multi_aff(bmap
, dom
, NULL
, max
);
6130 isl_basic_map_free(bmap
);
6134 __isl_give isl_pw_multi_aff
*isl_basic_map_lexmin_pw_multi_aff(
6135 __isl_take isl_basic_map
*bmap
)
6137 return isl_basic_map_lexopt_pw_multi_aff(bmap
, 0);
6141 #define TYPE isl_pw_multi_aff
6143 #define SUFFIX _pw_multi_aff
6145 #define EMPTY isl_pw_multi_aff_empty
6147 #define ADD isl_pw_multi_aff_union_add
6148 #include "isl_map_lexopt_templ.c"
6150 /* Given a map "map", compute the lexicographically minimal
6151 * (or maximal) image element for each domain element in dom,
6152 * in the form of an isl_pw_multi_aff.
6153 * Set *empty to those elements in dom that do not have an image element.
6155 * We first compute the lexicographically minimal or maximal element
6156 * in the first basic map. This results in a partial solution "res"
6157 * and a subset "todo" of dom that still need to be handled.
6158 * We then consider each of the remaining maps in "map" and successively
6159 * update both "res" and "todo".
6161 static __isl_give isl_pw_multi_aff
*isl_map_partial_lexopt_aligned_pw_multi_aff(
6162 __isl_take isl_map
*map
, __isl_take isl_set
*dom
,
6163 __isl_give isl_set
**empty
, int max
)
6166 isl_pw_multi_aff
*res
;
6172 if (isl_map_plain_is_empty(map
)) {
6177 return isl_pw_multi_aff_from_map(map
);
6180 res
= basic_map_partial_lexopt_pw_multi_aff(
6181 isl_basic_map_copy(map
->p
[0]),
6182 isl_set_copy(dom
), &todo
, max
);
6184 for (i
= 1; i
< map
->n
; ++i
) {
6185 isl_pw_multi_aff
*res_i
;
6188 res_i
= basic_map_partial_lexopt_pw_multi_aff(
6189 isl_basic_map_copy(map
->p
[i
]),
6190 isl_set_copy(dom
), &todo_i
, max
);
6193 res
= isl_pw_multi_aff_union_lexmax(res
, res_i
);
6195 res
= isl_pw_multi_aff_union_lexmin(res
, res_i
);
6197 todo
= isl_set_intersect(todo
, todo_i
);
6218 #define TYPE isl_map
6222 #define EMPTY isl_map_empty
6224 #define ADD isl_map_union_disjoint
6225 #include "isl_map_lexopt_templ.c"
6227 /* Given a map "map", compute the lexicographically minimal
6228 * (or maximal) image element for each domain element in dom.
6229 * Set *empty to those elements in dom that do not have an image element.
6231 * We first compute the lexicographically minimal or maximal element
6232 * in the first basic map. This results in a partial solution "res"
6233 * and a subset "todo" of dom that still need to be handled.
6234 * We then consider each of the remaining maps in "map" and successively
6235 * update both "res" and "todo".
6237 * Let res^k and todo^k be the results after k steps and let i = k + 1.
6238 * Assume we are computing the lexicographical maximum.
6239 * We first compute the lexicographically maximal element in basic map i.
6240 * This results in a partial solution res_i and a subset todo_i.
6241 * Then we combine these results with those obtain for the first k basic maps
6242 * to obtain a result that is valid for the first k+1 basic maps.
6243 * In particular, the set where there is no solution is the set where
6244 * there is no solution for the first k basic maps and also no solution
6245 * for the ith basic map, i.e.,
6247 * todo^i = todo^k * todo_i
6249 * On dom(res^k) * dom(res_i), we need to pick the larger of the two
6250 * solutions, arbitrarily breaking ties in favor of res^k.
6251 * That is, when res^k(a) >= res_i(a), we pick res^k and
6252 * when res^k(a) < res_i(a), we pick res_i. (Here, ">=" and "<" denote
6253 * the lexicographic order.)
6254 * In practice, we compute
6256 * res^k * (res_i . "<=")
6260 * res_i * (res^k . "<")
6262 * Finally, we consider the symmetric difference of dom(res^k) and dom(res_i),
6263 * where only one of res^k and res_i provides a solution and we simply pick
6270 * Note that we only compute these intersections when dom(res^k) intersects
6271 * dom(res_i). Otherwise, the only effect of these intersections is to
6272 * potentially break up res^k and res_i into smaller pieces.
6273 * We want to avoid such splintering as much as possible.
6274 * In fact, an earlier implementation of this function would look for
6275 * better results in the domain of res^k and for extra results in todo^k,
6276 * but this would always result in a splintering according to todo^k,
6277 * even when the domain of basic map i is disjoint from the domains of
6278 * the previous basic maps.
6280 static __isl_give isl_map
*isl_map_partial_lexopt_aligned(
6281 __isl_take isl_map
*map
, __isl_take isl_set
*dom
,
6282 __isl_give isl_set
**empty
, int max
)
6285 struct isl_map
*res
;
6286 struct isl_set
*todo
;
6291 if (isl_map_plain_is_empty(map
)) {
6299 res
= basic_map_partial_lexopt(isl_basic_map_copy(map
->p
[0]),
6300 isl_set_copy(dom
), &todo
, max
);
6302 for (i
= 1; i
< map
->n
; ++i
) {
6306 isl_space
*dim
= isl_space_range(isl_map_get_space(res
));
6308 res_i
= basic_map_partial_lexopt(isl_basic_map_copy(map
->p
[i
]),
6309 isl_set_copy(dom
), &todo_i
, max
);
6312 lt
= isl_map_lex_lt(isl_space_copy(dim
));
6313 le
= isl_map_lex_le(dim
);
6315 lt
= isl_map_lex_gt(isl_space_copy(dim
));
6316 le
= isl_map_lex_ge(dim
);
6318 lt
= isl_map_apply_range(isl_map_copy(res
), lt
);
6319 lt
= isl_map_intersect(lt
, isl_map_copy(res_i
));
6320 le
= isl_map_apply_range(isl_map_copy(res_i
), le
);
6321 le
= isl_map_intersect(le
, isl_map_copy(res
));
6323 if (!isl_map_is_empty(lt
) || !isl_map_is_empty(le
)) {
6324 res
= isl_map_intersect_domain(res
,
6325 isl_set_copy(todo_i
));
6326 res_i
= isl_map_intersect_domain(res_i
,
6327 isl_set_copy(todo
));
6330 res
= isl_map_union_disjoint(res
, res_i
);
6331 res
= isl_map_union_disjoint(res
, lt
);
6332 res
= isl_map_union_disjoint(res
, le
);
6334 todo
= isl_set_intersect(todo
, todo_i
);
6354 __isl_give isl_map
*isl_map_partial_lexmax(
6355 __isl_take isl_map
*map
, __isl_take isl_set
*dom
,
6356 __isl_give isl_set
**empty
)
6358 return isl_map_partial_lexopt(map
, dom
, empty
, 1);
6361 __isl_give isl_map
*isl_map_partial_lexmin(
6362 __isl_take isl_map
*map
, __isl_take isl_set
*dom
,
6363 __isl_give isl_set
**empty
)
6365 return isl_map_partial_lexopt(map
, dom
, empty
, 0);
6368 __isl_give isl_set
*isl_set_partial_lexmin(
6369 __isl_take isl_set
*set
, __isl_take isl_set
*dom
,
6370 __isl_give isl_set
**empty
)
6372 return (struct isl_set
*)
6373 isl_map_partial_lexmin((struct isl_map
*)set
,
6377 __isl_give isl_set
*isl_set_partial_lexmax(
6378 __isl_take isl_set
*set
, __isl_take isl_set
*dom
,
6379 __isl_give isl_set
**empty
)
6381 return (struct isl_set
*)
6382 isl_map_partial_lexmax((struct isl_map
*)set
,
6386 /* Compute the lexicographic minimum (or maximum if "max" is set)
6387 * of "bmap" over its domain.
6389 * Since we are not interested in the part of the domain space where
6390 * there is no solution, we initialize the domain to those constraints
6391 * of "bmap" that only involve the parameters and the input dimensions.
6392 * This relieves the parametric programming engine from detecting those
6393 * inequalities and transferring them to the context. More importantly,
6394 * it ensures that those inequalities are transferred first and not
6395 * intermixed with inequalities that actually split the domain.
6397 __isl_give isl_map
*isl_basic_map_lexopt(__isl_take isl_basic_map
*bmap
, int max
)
6401 isl_basic_map
*copy
;
6404 n_div
= isl_basic_map_dim(bmap
, isl_dim_div
);
6405 n_out
= isl_basic_map_dim(bmap
, isl_dim_out
);
6406 copy
= isl_basic_map_copy(bmap
);
6407 copy
= isl_basic_map_drop_constraints_involving_dims(copy
,
6408 isl_dim_div
, 0, n_div
);
6409 copy
= isl_basic_map_drop_constraints_involving_dims(copy
,
6410 isl_dim_out
, 0, n_out
);
6411 dom
= isl_basic_map_domain(copy
);
6412 return isl_basic_map_partial_lexopt(bmap
, dom
, NULL
, max
);
6415 __isl_give isl_map
*isl_basic_map_lexmin(__isl_take isl_basic_map
*bmap
)
6417 return isl_basic_map_lexopt(bmap
, 0);
6420 __isl_give isl_map
*isl_basic_map_lexmax(__isl_take isl_basic_map
*bmap
)
6422 return isl_basic_map_lexopt(bmap
, 1);
6425 __isl_give isl_set
*isl_basic_set_lexmin(__isl_take isl_basic_set
*bset
)
6427 return (isl_set
*)isl_basic_map_lexmin((isl_basic_map
*)bset
);
6430 __isl_give isl_set
*isl_basic_set_lexmax(__isl_take isl_basic_set
*bset
)
6432 return (isl_set
*)isl_basic_map_lexmax((isl_basic_map
*)bset
);
6435 /* Extract the first and only affine expression from list
6436 * and then add it to *pwaff with the given dom.
6437 * This domain is known to be disjoint from other domains
6438 * because of the way isl_basic_map_foreach_lexmax works.
6440 static int update_dim_opt(__isl_take isl_basic_set
*dom
,
6441 __isl_take isl_aff_list
*list
, void *user
)
6443 isl_ctx
*ctx
= isl_basic_set_get_ctx(dom
);
6445 isl_pw_aff
**pwaff
= user
;
6446 isl_pw_aff
*pwaff_i
;
6450 if (isl_aff_list_n_aff(list
) != 1)
6451 isl_die(ctx
, isl_error_internal
,
6452 "expecting single element list", goto error
);
6454 aff
= isl_aff_list_get_aff(list
, 0);
6455 pwaff_i
= isl_pw_aff_alloc(isl_set_from_basic_set(dom
), aff
);
6457 *pwaff
= isl_pw_aff_add_disjoint(*pwaff
, pwaff_i
);
6459 isl_aff_list_free(list
);
6463 isl_basic_set_free(dom
);
6464 isl_aff_list_free(list
);
6468 /* Given a basic map with one output dimension, compute the minimum or
6469 * maximum of that dimension as an isl_pw_aff.
6471 * The isl_pw_aff is constructed by having isl_basic_map_foreach_lexopt
6472 * call update_dim_opt on each leaf of the result.
6474 static __isl_give isl_pw_aff
*basic_map_dim_opt(__isl_keep isl_basic_map
*bmap
,
6477 isl_space
*dim
= isl_basic_map_get_space(bmap
);
6481 dim
= isl_space_from_domain(isl_space_domain(dim
));
6482 dim
= isl_space_add_dims(dim
, isl_dim_out
, 1);
6483 pwaff
= isl_pw_aff_empty(dim
);
6485 r
= isl_basic_map_foreach_lexopt(bmap
, max
, &update_dim_opt
, &pwaff
);
6487 return isl_pw_aff_free(pwaff
);
6492 /* Compute the minimum or maximum of the given output dimension
6493 * as a function of the parameters and the input dimensions,
6494 * but independently of the other output dimensions.
6496 * We first project out the other output dimension and then compute
6497 * the "lexicographic" maximum in each basic map, combining the results
6498 * using isl_pw_aff_union_max.
6500 static __isl_give isl_pw_aff
*map_dim_opt(__isl_take isl_map
*map
, int pos
,
6507 n_out
= isl_map_dim(map
, isl_dim_out
);
6508 map
= isl_map_project_out(map
, isl_dim_out
, pos
+ 1, n_out
- (pos
+ 1));
6509 map
= isl_map_project_out(map
, isl_dim_out
, 0, pos
);
6514 isl_space
*dim
= isl_map_get_space(map
);
6516 return isl_pw_aff_empty(dim
);
6519 pwaff
= basic_map_dim_opt(map
->p
[0], max
);
6520 for (i
= 1; i
< map
->n
; ++i
) {
6521 isl_pw_aff
*pwaff_i
;
6523 pwaff_i
= basic_map_dim_opt(map
->p
[i
], max
);
6524 pwaff
= isl_pw_aff_union_opt(pwaff
, pwaff_i
, max
);
6532 /* Compute the maximum of the given output dimension as a function of the
6533 * parameters and input dimensions, but independently of
6534 * the other output dimensions.
6536 __isl_give isl_pw_aff
*isl_map_dim_max(__isl_take isl_map
*map
, int pos
)
6538 return map_dim_opt(map
, pos
, 1);
6541 /* Compute the minimum or maximum of the given set dimension
6542 * as a function of the parameters,
6543 * but independently of the other set dimensions.
6545 static __isl_give isl_pw_aff
*set_dim_opt(__isl_take isl_set
*set
, int pos
,
6548 return map_dim_opt(set
, pos
, max
);
6551 /* Compute the maximum of the given set dimension as a function of the
6552 * parameters, but independently of the other set dimensions.
6554 __isl_give isl_pw_aff
*isl_set_dim_max(__isl_take isl_set
*set
, int pos
)
6556 return set_dim_opt(set
, pos
, 1);
6559 /* Compute the minimum of the given set dimension as a function of the
6560 * parameters, but independently of the other set dimensions.
6562 __isl_give isl_pw_aff
*isl_set_dim_min(__isl_take isl_set
*set
, int pos
)
6564 return set_dim_opt(set
, pos
, 0);
6567 /* Apply a preimage specified by "mat" on the parameters of "bset".
6568 * bset is assumed to have only parameters and divs.
6570 static struct isl_basic_set
*basic_set_parameter_preimage(
6571 struct isl_basic_set
*bset
, struct isl_mat
*mat
)
6578 bset
->dim
= isl_space_cow(bset
->dim
);
6582 nparam
= isl_basic_set_dim(bset
, isl_dim_param
);
6584 isl_assert(bset
->ctx
, mat
->n_row
== 1 + nparam
, goto error
);
6586 bset
->dim
->nparam
= 0;
6587 bset
->dim
->n_out
= nparam
;
6588 bset
= isl_basic_set_preimage(bset
, mat
);
6590 bset
->dim
->nparam
= bset
->dim
->n_out
;
6591 bset
->dim
->n_out
= 0;
6596 isl_basic_set_free(bset
);
6600 /* Apply a preimage specified by "mat" on the parameters of "set".
6601 * set is assumed to have only parameters and divs.
6603 static struct isl_set
*set_parameter_preimage(
6604 struct isl_set
*set
, struct isl_mat
*mat
)
6606 isl_space
*dim
= NULL
;
6612 dim
= isl_space_copy(set
->dim
);
6613 dim
= isl_space_cow(dim
);
6617 nparam
= isl_set_dim(set
, isl_dim_param
);
6619 isl_assert(set
->ctx
, mat
->n_row
== 1 + nparam
, goto error
);
6622 dim
->n_out
= nparam
;
6623 isl_set_reset_space(set
, dim
);
6624 set
= isl_set_preimage(set
, mat
);
6627 dim
= isl_space_copy(set
->dim
);
6628 dim
= isl_space_cow(dim
);
6631 dim
->nparam
= dim
->n_out
;
6633 isl_set_reset_space(set
, dim
);
6636 isl_space_free(dim
);
6643 /* Intersect the basic set "bset" with the affine space specified by the
6644 * equalities in "eq".
6646 static struct isl_basic_set
*basic_set_append_equalities(
6647 struct isl_basic_set
*bset
, struct isl_mat
*eq
)
6655 bset
= isl_basic_set_extend_space(bset
, isl_space_copy(bset
->dim
), 0,
6660 len
= 1 + isl_space_dim(bset
->dim
, isl_dim_all
) + bset
->extra
;
6661 for (i
= 0; i
< eq
->n_row
; ++i
) {
6662 k
= isl_basic_set_alloc_equality(bset
);
6665 isl_seq_cpy(bset
->eq
[k
], eq
->row
[i
], eq
->n_col
);
6666 isl_seq_clr(bset
->eq
[k
] + eq
->n_col
, len
- eq
->n_col
);
6670 bset
= isl_basic_set_gauss(bset
, NULL
);
6671 bset
= isl_basic_set_finalize(bset
);
6676 isl_basic_set_free(bset
);
6680 /* Intersect the set "set" with the affine space specified by the
6681 * equalities in "eq".
6683 static struct isl_set
*set_append_equalities(struct isl_set
*set
,
6691 for (i
= 0; i
< set
->n
; ++i
) {
6692 set
->p
[i
] = basic_set_append_equalities(set
->p
[i
],
6705 /* Given a basic set "bset" that only involves parameters and existentially
6706 * quantified variables, return the index of the first equality
6707 * that only involves parameters. If there is no such equality then
6708 * return bset->n_eq.
6710 * This function assumes that isl_basic_set_gauss has been called on "bset".
6712 static int first_parameter_equality(__isl_keep isl_basic_set
*bset
)
6715 unsigned nparam
, n_div
;
6720 nparam
= isl_basic_set_dim(bset
, isl_dim_param
);
6721 n_div
= isl_basic_set_dim(bset
, isl_dim_div
);
6723 for (i
= 0, j
= n_div
- 1; i
< bset
->n_eq
&& j
>= 0; --j
) {
6724 if (!isl_int_is_zero(bset
->eq
[i
][1 + nparam
+ j
]))
6731 /* Compute an explicit representation for the existentially quantified
6732 * variables in "bset" by computing the "minimal value" of the set
6733 * variables. Since there are no set variables, the computation of
6734 * the minimal value essentially computes an explicit representation
6735 * of the non-empty part(s) of "bset".
6737 * The input only involves parameters and existentially quantified variables.
6738 * All equalities among parameters have been removed.
6740 * Since the existentially quantified variables in the result are in general
6741 * going to be different from those in the input, we first replace
6742 * them by the minimal number of variables based on their equalities.
6743 * This should simplify the parametric integer programming.
6745 static __isl_give isl_set
*base_compute_divs(__isl_take isl_basic_set
*bset
)
6747 isl_morph
*morph1
, *morph2
;
6753 if (bset
->n_eq
== 0)
6754 return isl_basic_set_lexmin(bset
);
6756 morph1
= isl_basic_set_parameter_compression(bset
);
6757 bset
= isl_morph_basic_set(isl_morph_copy(morph1
), bset
);
6758 bset
= isl_basic_set_lift(bset
);
6759 morph2
= isl_basic_set_variable_compression(bset
, isl_dim_set
);
6760 bset
= isl_morph_basic_set(morph2
, bset
);
6761 n
= isl_basic_set_dim(bset
, isl_dim_set
);
6762 bset
= isl_basic_set_project_out(bset
, isl_dim_set
, 0, n
);
6764 set
= isl_basic_set_lexmin(bset
);
6766 set
= isl_morph_set(isl_morph_inverse(morph1
), set
);
6771 /* Project the given basic set onto its parameter domain, possibly introducing
6772 * new, explicit, existential variables in the constraints.
6773 * The input has parameters and (possibly implicit) existential variables.
6774 * The output has the same parameters, but only
6775 * explicit existentially quantified variables.
6777 * The actual projection is performed by pip, but pip doesn't seem
6778 * to like equalities very much, so we first remove the equalities
6779 * among the parameters by performing a variable compression on
6780 * the parameters. Afterward, an inverse transformation is performed
6781 * and the equalities among the parameters are inserted back in.
6783 * The variable compression on the parameters may uncover additional
6784 * equalities that were only implicit before. We therefore check
6785 * if there are any new parameter equalities in the result and
6786 * if so recurse. The removal of parameter equalities is required
6787 * for the parameter compression performed by base_compute_divs.
6789 static struct isl_set
*parameter_compute_divs(struct isl_basic_set
*bset
)
6793 struct isl_mat
*T
, *T2
;
6794 struct isl_set
*set
;
6797 bset
= isl_basic_set_cow(bset
);
6801 if (bset
->n_eq
== 0)
6802 return base_compute_divs(bset
);
6804 bset
= isl_basic_set_gauss(bset
, NULL
);
6807 if (isl_basic_set_plain_is_empty(bset
))
6808 return isl_set_from_basic_set(bset
);
6810 i
= first_parameter_equality(bset
);
6811 if (i
== bset
->n_eq
)
6812 return base_compute_divs(bset
);
6814 nparam
= isl_basic_set_dim(bset
, isl_dim_param
);
6815 eq
= isl_mat_sub_alloc6(bset
->ctx
, bset
->eq
, i
, bset
->n_eq
- i
,
6817 eq
= isl_mat_cow(eq
);
6818 T
= isl_mat_variable_compression(isl_mat_copy(eq
), &T2
);
6819 if (T
&& T
->n_col
== 0) {
6823 bset
= isl_basic_set_set_to_empty(bset
);
6824 return isl_set_from_basic_set(bset
);
6826 bset
= basic_set_parameter_preimage(bset
, T
);
6828 i
= first_parameter_equality(bset
);
6831 else if (i
== bset
->n_eq
)
6832 set
= base_compute_divs(bset
);
6834 set
= parameter_compute_divs(bset
);
6835 set
= set_parameter_preimage(set
, T2
);
6836 set
= set_append_equalities(set
, eq
);
6840 /* Insert the divs from "ls" before those of "bmap".
6842 * The number of columns is not changed, which means that the last
6843 * dimensions of "bmap" are being reintepreted as the divs from "ls".
6844 * The caller is responsible for removing the same number of dimensions
6845 * from the space of "bmap".
6847 static __isl_give isl_basic_map
*insert_divs_from_local_space(
6848 __isl_take isl_basic_map
*bmap
, __isl_keep isl_local_space
*ls
)
6854 n_div
= isl_local_space_dim(ls
, isl_dim_div
);
6858 old_n_div
= bmap
->n_div
;
6859 bmap
= insert_div_rows(bmap
, n_div
);
6863 for (i
= 0; i
< n_div
; ++i
) {
6864 isl_seq_cpy(bmap
->div
[i
], ls
->div
->row
[i
], ls
->div
->n_col
);
6865 isl_seq_clr(bmap
->div
[i
] + ls
->div
->n_col
, old_n_div
);
6871 /* Replace the space of "bmap" by the space and divs of "ls".
6873 * If "ls" has any divs, then we simplify the result since we may
6874 * have discovered some additional equalities that could simplify
6875 * the div expressions.
6877 static __isl_give isl_basic_map
*basic_replace_space_by_local_space(
6878 __isl_take isl_basic_map
*bmap
, __isl_take isl_local_space
*ls
)
6882 bmap
= isl_basic_map_cow(bmap
);
6886 n_div
= isl_local_space_dim(ls
, isl_dim_div
);
6887 bmap
= insert_divs_from_local_space(bmap
, ls
);
6891 isl_space_free(bmap
->dim
);
6892 bmap
->dim
= isl_local_space_get_space(ls
);
6896 isl_local_space_free(ls
);
6898 bmap
= isl_basic_map_simplify(bmap
);
6899 bmap
= isl_basic_map_finalize(bmap
);
6902 isl_basic_map_free(bmap
);
6903 isl_local_space_free(ls
);
6907 /* Replace the space of "map" by the space and divs of "ls".
6909 static __isl_give isl_map
*replace_space_by_local_space(__isl_take isl_map
*map
,
6910 __isl_take isl_local_space
*ls
)
6914 map
= isl_map_cow(map
);
6918 for (i
= 0; i
< map
->n
; ++i
) {
6919 map
->p
[i
] = basic_replace_space_by_local_space(map
->p
[i
],
6920 isl_local_space_copy(ls
));
6924 isl_space_free(map
->dim
);
6925 map
->dim
= isl_local_space_get_space(ls
);
6929 isl_local_space_free(ls
);
6932 isl_local_space_free(ls
);
6937 /* Compute an explicit representation for the existentially
6938 * quantified variables for which do not know any explicit representation yet.
6940 * We first sort the existentially quantified variables so that the
6941 * existentially quantified variables for which we already have an explicit
6942 * representation are placed before those for which we do not.
6943 * The input dimensions, the output dimensions and the existentially
6944 * quantified variables for which we already have an explicit
6945 * representation are then turned into parameters.
6946 * compute_divs returns a map with the same parameters and
6947 * no input or output dimensions and the dimension specification
6948 * is reset to that of the input, including the existentially quantified
6949 * variables for which we already had an explicit representation.
6951 static struct isl_map
*compute_divs(struct isl_basic_map
*bmap
)
6953 struct isl_basic_set
*bset
;
6954 struct isl_set
*set
;
6955 struct isl_map
*map
;
6957 isl_local_space
*ls
;
6964 bmap
= isl_basic_map_sort_divs(bmap
);
6965 bmap
= isl_basic_map_cow(bmap
);
6969 for (n_known
= 0; n_known
< bmap
->n_div
; ++n_known
)
6970 if (isl_int_is_zero(bmap
->div
[n_known
][0]))
6973 nparam
= isl_basic_map_dim(bmap
, isl_dim_param
);
6974 n_in
= isl_basic_map_dim(bmap
, isl_dim_in
);
6975 n_out
= isl_basic_map_dim(bmap
, isl_dim_out
);
6976 dim
= isl_space_set_alloc(bmap
->ctx
,
6977 nparam
+ n_in
+ n_out
+ n_known
, 0);
6981 ls
= isl_basic_map_get_local_space(bmap
);
6982 ls
= isl_local_space_drop_dims(ls
, isl_dim_div
,
6983 n_known
, bmap
->n_div
- n_known
);
6985 for (i
= n_known
; i
< bmap
->n_div
; ++i
)
6986 swap_div(bmap
, i
- n_known
, i
);
6987 bmap
->n_div
-= n_known
;
6988 bmap
->extra
-= n_known
;
6990 bmap
= isl_basic_map_reset_space(bmap
, dim
);
6991 bset
= (struct isl_basic_set
*)bmap
;
6993 set
= parameter_compute_divs(bset
);
6994 map
= (struct isl_map
*)set
;
6995 map
= replace_space_by_local_space(map
, ls
);
6999 isl_basic_map_free(bmap
);
7003 int isl_basic_map_divs_known(__isl_keep isl_basic_map
*bmap
)
7011 off
= isl_space_dim(bmap
->dim
, isl_dim_all
);
7012 for (i
= 0; i
< bmap
->n_div
; ++i
) {
7013 if (isl_int_is_zero(bmap
->div
[i
][0]))
7015 isl_assert(bmap
->ctx
, isl_int_is_zero(bmap
->div
[i
][1+1+off
+i
]),
7021 static int map_divs_known(__isl_keep isl_map
*map
)
7028 for (i
= 0; i
< map
->n
; ++i
) {
7029 int known
= isl_basic_map_divs_known(map
->p
[i
]);
7037 /* If bmap contains any unknown divs, then compute explicit
7038 * expressions for them. However, this computation may be
7039 * quite expensive, so first try to remove divs that aren't
7042 struct isl_map
*isl_basic_map_compute_divs(struct isl_basic_map
*bmap
)
7045 struct isl_map
*map
;
7047 known
= isl_basic_map_divs_known(bmap
);
7051 return isl_map_from_basic_map(bmap
);
7053 bmap
= isl_basic_map_drop_redundant_divs(bmap
);
7055 known
= isl_basic_map_divs_known(bmap
);
7059 return isl_map_from_basic_map(bmap
);
7061 map
= compute_divs(bmap
);
7064 isl_basic_map_free(bmap
);
7068 struct isl_map
*isl_map_compute_divs(struct isl_map
*map
)
7072 struct isl_map
*res
;
7079 known
= map_divs_known(map
);
7087 res
= isl_basic_map_compute_divs(isl_basic_map_copy(map
->p
[0]));
7088 for (i
= 1 ; i
< map
->n
; ++i
) {
7090 r2
= isl_basic_map_compute_divs(isl_basic_map_copy(map
->p
[i
]));
7091 if (ISL_F_ISSET(map
, ISL_MAP_DISJOINT
))
7092 res
= isl_map_union_disjoint(res
, r2
);
7094 res
= isl_map_union(res
, r2
);
7101 struct isl_set
*isl_basic_set_compute_divs(struct isl_basic_set
*bset
)
7103 return (struct isl_set
*)
7104 isl_basic_map_compute_divs((struct isl_basic_map
*)bset
);
7107 struct isl_set
*isl_set_compute_divs(struct isl_set
*set
)
7109 return (struct isl_set
*)
7110 isl_map_compute_divs((struct isl_map
*)set
);
7113 struct isl_set
*isl_map_domain(struct isl_map
*map
)
7116 struct isl_set
*set
;
7121 map
= isl_map_cow(map
);
7125 set
= (struct isl_set
*)map
;
7126 set
->dim
= isl_space_domain(set
->dim
);
7129 for (i
= 0; i
< map
->n
; ++i
) {
7130 set
->p
[i
] = isl_basic_map_domain(map
->p
[i
]);
7134 ISL_F_CLR(set
, ISL_MAP_DISJOINT
);
7135 ISL_F_CLR(set
, ISL_SET_NORMALIZED
);
7142 /* Return the union of "map1" and "map2", where we assume for now that
7143 * "map1" and "map2" are disjoint. Note that the basic maps inside
7144 * "map1" or "map2" may not be disjoint from each other.
7145 * Also note that this function is also called from isl_map_union,
7146 * which takes care of handling the situation where "map1" and "map2"
7147 * may not be disjoint.
7149 * If one of the inputs is empty, we can simply return the other input.
7150 * Similarly, if one of the inputs is universal, then it is equal to the union.
7152 static __isl_give isl_map
*map_union_disjoint(__isl_take isl_map
*map1
,
7153 __isl_take isl_map
*map2
)
7157 struct isl_map
*map
= NULL
;
7163 if (!isl_space_is_equal(map1
->dim
, map2
->dim
))
7164 isl_die(isl_map_get_ctx(map1
), isl_error_invalid
,
7165 "spaces don't match", goto error
);
7176 is_universe
= isl_map_plain_is_universe(map1
);
7177 if (is_universe
< 0)
7184 is_universe
= isl_map_plain_is_universe(map2
);
7185 if (is_universe
< 0)
7192 if (ISL_F_ISSET(map1
, ISL_MAP_DISJOINT
) &&
7193 ISL_F_ISSET(map2
, ISL_MAP_DISJOINT
))
7194 ISL_FL_SET(flags
, ISL_MAP_DISJOINT
);
7196 map
= isl_map_alloc_space(isl_space_copy(map1
->dim
),
7197 map1
->n
+ map2
->n
, flags
);
7200 for (i
= 0; i
< map1
->n
; ++i
) {
7201 map
= isl_map_add_basic_map(map
,
7202 isl_basic_map_copy(map1
->p
[i
]));
7206 for (i
= 0; i
< map2
->n
; ++i
) {
7207 map
= isl_map_add_basic_map(map
,
7208 isl_basic_map_copy(map2
->p
[i
]));
7222 /* Return the union of "map1" and "map2", where "map1" and "map2" are
7223 * guaranteed to be disjoint by the caller.
7225 * Note that this functions is called from within isl_map_make_disjoint,
7226 * so we have to be careful not to touch the constraints of the inputs
7229 __isl_give isl_map
*isl_map_union_disjoint(__isl_take isl_map
*map1
,
7230 __isl_take isl_map
*map2
)
7232 return isl_map_align_params_map_map_and(map1
, map2
, &map_union_disjoint
);
7235 /* Return the union of "map1" and "map2", where "map1" and "map2" may
7236 * not be disjoint. The parameters are assumed to have been aligned.
7238 * We currently simply call map_union_disjoint, the internal operation
7239 * of which does not really depend on the inputs being disjoint.
7240 * If the result contains more than one basic map, then we clear
7241 * the disjoint flag since the result may contain basic maps from
7242 * both inputs and these are not guaranteed to be disjoint.
7244 * As a special case, if "map1" and "map2" are obviously equal,
7245 * then we simply return "map1".
7247 static __isl_give isl_map
*map_union_aligned(__isl_take isl_map
*map1
,
7248 __isl_take isl_map
*map2
)
7255 equal
= isl_map_plain_is_equal(map1
, map2
);
7263 map1
= map_union_disjoint(map1
, map2
);
7267 ISL_F_CLR(map1
, ISL_MAP_DISJOINT
);
7275 /* Return the union of "map1" and "map2", where "map1" and "map2" may
7278 __isl_give isl_map
*isl_map_union(__isl_take isl_map
*map1
,
7279 __isl_take isl_map
*map2
)
7281 return isl_map_align_params_map_map_and(map1
, map2
, &map_union_aligned
);
7284 struct isl_set
*isl_set_union_disjoint(
7285 struct isl_set
*set1
, struct isl_set
*set2
)
7287 return (struct isl_set
*)
7288 isl_map_union_disjoint(
7289 (struct isl_map
*)set1
, (struct isl_map
*)set2
);
7292 struct isl_set
*isl_set_union(struct isl_set
*set1
, struct isl_set
*set2
)
7294 return (struct isl_set
*)
7295 isl_map_union((struct isl_map
*)set1
, (struct isl_map
*)set2
);
7298 /* Apply "fn" to pairs of elements from "map" and "set" and collect
7301 * "map" and "set" are assumed to be compatible and non-NULL.
7303 static __isl_give isl_map
*map_intersect_set(__isl_take isl_map
*map
,
7304 __isl_take isl_set
*set
,
7305 __isl_give isl_basic_map
*fn(__isl_take isl_basic_map
*bmap
,
7306 __isl_take isl_basic_set
*bset
))
7309 struct isl_map
*result
;
7312 if (isl_set_plain_is_universe(set
)) {
7317 if (ISL_F_ISSET(map
, ISL_MAP_DISJOINT
) &&
7318 ISL_F_ISSET(set
, ISL_MAP_DISJOINT
))
7319 ISL_FL_SET(flags
, ISL_MAP_DISJOINT
);
7321 result
= isl_map_alloc_space(isl_space_copy(map
->dim
),
7322 map
->n
* set
->n
, flags
);
7323 for (i
= 0; result
&& i
< map
->n
; ++i
)
7324 for (j
= 0; j
< set
->n
; ++j
) {
7325 result
= isl_map_add_basic_map(result
,
7326 fn(isl_basic_map_copy(map
->p
[i
]),
7327 isl_basic_set_copy(set
->p
[j
])));
7337 static __isl_give isl_map
*map_intersect_range(__isl_take isl_map
*map
,
7338 __isl_take isl_set
*set
)
7343 if (!isl_map_compatible_range(map
, set
))
7344 isl_die(set
->ctx
, isl_error_invalid
,
7345 "incompatible spaces", goto error
);
7347 return map_intersect_set(map
, set
, &isl_basic_map_intersect_range
);
7354 __isl_give isl_map
*isl_map_intersect_range(__isl_take isl_map
*map
,
7355 __isl_take isl_set
*set
)
7357 return isl_map_align_params_map_map_and(map
, set
, &map_intersect_range
);
7360 static __isl_give isl_map
*map_intersect_domain(__isl_take isl_map
*map
,
7361 __isl_take isl_set
*set
)
7366 if (!isl_map_compatible_domain(map
, set
))
7367 isl_die(set
->ctx
, isl_error_invalid
,
7368 "incompatible spaces", goto error
);
7370 return map_intersect_set(map
, set
, &isl_basic_map_intersect_domain
);
7377 __isl_give isl_map
*isl_map_intersect_domain(__isl_take isl_map
*map
,
7378 __isl_take isl_set
*set
)
7380 return isl_map_align_params_map_map_and(map
, set
,
7381 &map_intersect_domain
);
7384 static __isl_give isl_map
*map_apply_domain(__isl_take isl_map
*map1
,
7385 __isl_take isl_map
*map2
)
7389 map1
= isl_map_reverse(map1
);
7390 map1
= isl_map_apply_range(map1
, map2
);
7391 return isl_map_reverse(map1
);
7398 __isl_give isl_map
*isl_map_apply_domain(__isl_take isl_map
*map1
,
7399 __isl_take isl_map
*map2
)
7401 return isl_map_align_params_map_map_and(map1
, map2
, &map_apply_domain
);
7404 static __isl_give isl_map
*map_apply_range(__isl_take isl_map
*map1
,
7405 __isl_take isl_map
*map2
)
7407 isl_space
*dim_result
;
7408 struct isl_map
*result
;
7414 dim_result
= isl_space_join(isl_space_copy(map1
->dim
),
7415 isl_space_copy(map2
->dim
));
7417 result
= isl_map_alloc_space(dim_result
, map1
->n
* map2
->n
, 0);
7420 for (i
= 0; i
< map1
->n
; ++i
)
7421 for (j
= 0; j
< map2
->n
; ++j
) {
7422 result
= isl_map_add_basic_map(result
,
7423 isl_basic_map_apply_range(
7424 isl_basic_map_copy(map1
->p
[i
]),
7425 isl_basic_map_copy(map2
->p
[j
])));
7431 if (result
&& result
->n
<= 1)
7432 ISL_F_SET(result
, ISL_MAP_DISJOINT
);
7440 __isl_give isl_map
*isl_map_apply_range(__isl_take isl_map
*map1
,
7441 __isl_take isl_map
*map2
)
7443 return isl_map_align_params_map_map_and(map1
, map2
, &map_apply_range
);
7447 * returns range - domain
7449 struct isl_basic_set
*isl_basic_map_deltas(struct isl_basic_map
*bmap
)
7451 isl_space
*dims
, *target_dim
;
7452 struct isl_basic_set
*bset
;
7459 isl_assert(bmap
->ctx
, isl_space_tuple_is_equal(bmap
->dim
, isl_dim_in
,
7460 bmap
->dim
, isl_dim_out
),
7462 target_dim
= isl_space_domain(isl_basic_map_get_space(bmap
));
7463 dim
= isl_basic_map_n_in(bmap
);
7464 nparam
= isl_basic_map_n_param(bmap
);
7465 bset
= isl_basic_set_from_basic_map(bmap
);
7466 bset
= isl_basic_set_cow(bset
);
7467 dims
= isl_basic_set_get_space(bset
);
7468 dims
= isl_space_add_dims(dims
, isl_dim_set
, dim
);
7469 bset
= isl_basic_set_extend_space(bset
, dims
, 0, dim
, 0);
7470 bset
= isl_basic_set_swap_vars(bset
, 2*dim
);
7471 for (i
= 0; i
< dim
; ++i
) {
7472 int j
= isl_basic_map_alloc_equality(
7473 (struct isl_basic_map
*)bset
);
7475 bset
= isl_basic_set_free(bset
);
7478 isl_seq_clr(bset
->eq
[j
], 1 + isl_basic_set_total_dim(bset
));
7479 isl_int_set_si(bset
->eq
[j
][1+nparam
+i
], 1);
7480 isl_int_set_si(bset
->eq
[j
][1+nparam
+dim
+i
], 1);
7481 isl_int_set_si(bset
->eq
[j
][1+nparam
+2*dim
+i
], -1);
7483 bset
= isl_basic_set_project_out(bset
, isl_dim_set
, dim
, 2*dim
);
7484 bset
= isl_basic_set_reset_space(bset
, target_dim
);
7487 isl_basic_map_free(bmap
);
7492 * returns range - domain
7494 __isl_give isl_set
*isl_map_deltas(__isl_take isl_map
*map
)
7498 struct isl_set
*result
;
7503 isl_assert(map
->ctx
, isl_space_tuple_is_equal(map
->dim
, isl_dim_in
,
7504 map
->dim
, isl_dim_out
),
7506 dim
= isl_map_get_space(map
);
7507 dim
= isl_space_domain(dim
);
7508 result
= isl_set_alloc_space(dim
, map
->n
, 0);
7511 for (i
= 0; i
< map
->n
; ++i
)
7512 result
= isl_set_add_basic_set(result
,
7513 isl_basic_map_deltas(isl_basic_map_copy(map
->p
[i
])));
7522 * returns [domain -> range] -> range - domain
7524 __isl_give isl_basic_map
*isl_basic_map_deltas_map(
7525 __isl_take isl_basic_map
*bmap
)
7529 isl_basic_map
*domain
;
7533 if (!isl_space_tuple_is_equal(bmap
->dim
, isl_dim_in
,
7534 bmap
->dim
, isl_dim_out
))
7535 isl_die(bmap
->ctx
, isl_error_invalid
,
7536 "domain and range don't match", goto error
);
7538 nparam
= isl_basic_map_dim(bmap
, isl_dim_param
);
7539 n
= isl_basic_map_dim(bmap
, isl_dim_in
);
7541 dim
= isl_space_from_range(isl_space_domain(isl_basic_map_get_space(bmap
)));
7542 domain
= isl_basic_map_universe(dim
);
7544 bmap
= isl_basic_map_from_domain(isl_basic_map_wrap(bmap
));
7545 bmap
= isl_basic_map_apply_range(bmap
, domain
);
7546 bmap
= isl_basic_map_extend_constraints(bmap
, n
, 0);
7548 total
= isl_basic_map_total_dim(bmap
);
7550 for (i
= 0; i
< n
; ++i
) {
7551 k
= isl_basic_map_alloc_equality(bmap
);
7554 isl_seq_clr(bmap
->eq
[k
], 1 + total
);
7555 isl_int_set_si(bmap
->eq
[k
][1 + nparam
+ i
], 1);
7556 isl_int_set_si(bmap
->eq
[k
][1 + nparam
+ n
+ i
], -1);
7557 isl_int_set_si(bmap
->eq
[k
][1 + nparam
+ n
+ n
+ i
], 1);
7560 bmap
= isl_basic_map_gauss(bmap
, NULL
);
7561 return isl_basic_map_finalize(bmap
);
7563 isl_basic_map_free(bmap
);
7568 * returns [domain -> range] -> range - domain
7570 __isl_give isl_map
*isl_map_deltas_map(__isl_take isl_map
*map
)
7573 isl_space
*domain_dim
;
7578 if (!isl_space_tuple_is_equal(map
->dim
, isl_dim_in
,
7579 map
->dim
, isl_dim_out
))
7580 isl_die(map
->ctx
, isl_error_invalid
,
7581 "domain and range don't match", goto error
);
7583 map
= isl_map_cow(map
);
7587 domain_dim
= isl_space_from_range(isl_space_domain(isl_map_get_space(map
)));
7588 map
->dim
= isl_space_from_domain(isl_space_wrap(map
->dim
));
7589 map
->dim
= isl_space_join(map
->dim
, domain_dim
);
7592 for (i
= 0; i
< map
->n
; ++i
) {
7593 map
->p
[i
] = isl_basic_map_deltas_map(map
->p
[i
]);
7597 ISL_F_CLR(map
, ISL_MAP_NORMALIZED
);
7604 static __isl_give isl_basic_map
*basic_map_identity(__isl_take isl_space
*dims
)
7606 struct isl_basic_map
*bmap
;
7614 nparam
= dims
->nparam
;
7616 bmap
= isl_basic_map_alloc_space(dims
, 0, dim
, 0);
7620 for (i
= 0; i
< dim
; ++i
) {
7621 int j
= isl_basic_map_alloc_equality(bmap
);
7624 isl_seq_clr(bmap
->eq
[j
], 1 + isl_basic_map_total_dim(bmap
));
7625 isl_int_set_si(bmap
->eq
[j
][1+nparam
+i
], 1);
7626 isl_int_set_si(bmap
->eq
[j
][1+nparam
+dim
+i
], -1);
7628 return isl_basic_map_finalize(bmap
);
7630 isl_basic_map_free(bmap
);
7634 __isl_give isl_basic_map
*isl_basic_map_identity(__isl_take isl_space
*dim
)
7638 if (dim
->n_in
!= dim
->n_out
)
7639 isl_die(dim
->ctx
, isl_error_invalid
,
7640 "number of input and output dimensions needs to be "
7641 "the same", goto error
);
7642 return basic_map_identity(dim
);
7644 isl_space_free(dim
);
7648 struct isl_basic_map
*isl_basic_map_identity_like(struct isl_basic_map
*model
)
7650 if (!model
|| !model
->dim
)
7652 return isl_basic_map_identity(isl_space_copy(model
->dim
));
7655 __isl_give isl_map
*isl_map_identity(__isl_take isl_space
*dim
)
7657 return isl_map_from_basic_map(isl_basic_map_identity(dim
));
7660 struct isl_map
*isl_map_identity_like(struct isl_map
*model
)
7662 if (!model
|| !model
->dim
)
7664 return isl_map_identity(isl_space_copy(model
->dim
));
7667 struct isl_map
*isl_map_identity_like_basic_map(struct isl_basic_map
*model
)
7669 if (!model
|| !model
->dim
)
7671 return isl_map_identity(isl_space_copy(model
->dim
));
7674 __isl_give isl_map
*isl_set_identity(__isl_take isl_set
*set
)
7676 isl_space
*dim
= isl_set_get_space(set
);
7678 id
= isl_map_identity(isl_space_map_from_set(dim
));
7679 return isl_map_intersect_range(id
, set
);
7682 /* Construct a basic set with all set dimensions having only non-negative
7685 __isl_give isl_basic_set
*isl_basic_set_positive_orthant(
7686 __isl_take isl_space
*space
)
7691 struct isl_basic_set
*bset
;
7695 nparam
= space
->nparam
;
7697 bset
= isl_basic_set_alloc_space(space
, 0, 0, dim
);
7700 for (i
= 0; i
< dim
; ++i
) {
7701 int k
= isl_basic_set_alloc_inequality(bset
);
7704 isl_seq_clr(bset
->ineq
[k
], 1 + isl_basic_set_total_dim(bset
));
7705 isl_int_set_si(bset
->ineq
[k
][1 + nparam
+ i
], 1);
7709 isl_basic_set_free(bset
);
7713 /* Construct the half-space x_pos >= 0.
7715 static __isl_give isl_basic_set
*nonneg_halfspace(__isl_take isl_space
*dim
,
7719 isl_basic_set
*nonneg
;
7721 nonneg
= isl_basic_set_alloc_space(dim
, 0, 0, 1);
7722 k
= isl_basic_set_alloc_inequality(nonneg
);
7725 isl_seq_clr(nonneg
->ineq
[k
], 1 + isl_basic_set_total_dim(nonneg
));
7726 isl_int_set_si(nonneg
->ineq
[k
][pos
], 1);
7728 return isl_basic_set_finalize(nonneg
);
7730 isl_basic_set_free(nonneg
);
7734 /* Construct the half-space x_pos <= -1.
7736 static __isl_give isl_basic_set
*neg_halfspace(__isl_take isl_space
*dim
, int pos
)
7741 neg
= isl_basic_set_alloc_space(dim
, 0, 0, 1);
7742 k
= isl_basic_set_alloc_inequality(neg
);
7745 isl_seq_clr(neg
->ineq
[k
], 1 + isl_basic_set_total_dim(neg
));
7746 isl_int_set_si(neg
->ineq
[k
][0], -1);
7747 isl_int_set_si(neg
->ineq
[k
][pos
], -1);
7749 return isl_basic_set_finalize(neg
);
7751 isl_basic_set_free(neg
);
7755 __isl_give isl_set
*isl_set_split_dims(__isl_take isl_set
*set
,
7756 enum isl_dim_type type
, unsigned first
, unsigned n
)
7760 isl_basic_set
*nonneg
;
7768 isl_assert(set
->ctx
, first
+ n
<= isl_set_dim(set
, type
), goto error
);
7770 offset
= pos(set
->dim
, type
);
7771 for (i
= 0; i
< n
; ++i
) {
7772 nonneg
= nonneg_halfspace(isl_set_get_space(set
),
7773 offset
+ first
+ i
);
7774 neg
= neg_halfspace(isl_set_get_space(set
), offset
+ first
+ i
);
7776 set
= isl_set_intersect(set
, isl_basic_set_union(nonneg
, neg
));
7785 static int foreach_orthant(__isl_take isl_set
*set
, int *signs
, int first
,
7786 int len
, int (*fn
)(__isl_take isl_set
*orthant
, int *signs
, void *user
),
7793 if (isl_set_plain_is_empty(set
)) {
7798 return fn(set
, signs
, user
);
7801 half
= isl_set_from_basic_set(nonneg_halfspace(isl_set_get_space(set
),
7803 half
= isl_set_intersect(half
, isl_set_copy(set
));
7804 if (foreach_orthant(half
, signs
, first
+ 1, len
, fn
, user
) < 0)
7808 half
= isl_set_from_basic_set(neg_halfspace(isl_set_get_space(set
),
7810 half
= isl_set_intersect(half
, set
);
7811 return foreach_orthant(half
, signs
, first
+ 1, len
, fn
, user
);
7817 /* Call "fn" on the intersections of "set" with each of the orthants
7818 * (except for obviously empty intersections). The orthant is identified
7819 * by the signs array, with each entry having value 1 or -1 according
7820 * to the sign of the corresponding variable.
7822 int isl_set_foreach_orthant(__isl_keep isl_set
*set
,
7823 int (*fn
)(__isl_take isl_set
*orthant
, int *signs
, void *user
),
7833 if (isl_set_plain_is_empty(set
))
7836 nparam
= isl_set_dim(set
, isl_dim_param
);
7837 nvar
= isl_set_dim(set
, isl_dim_set
);
7839 signs
= isl_alloc_array(set
->ctx
, int, nparam
+ nvar
);
7841 r
= foreach_orthant(isl_set_copy(set
), signs
, 0, nparam
+ nvar
,
7849 int isl_set_is_equal(struct isl_set
*set1
, struct isl_set
*set2
)
7851 return isl_map_is_equal((struct isl_map
*)set1
, (struct isl_map
*)set2
);
7854 int isl_basic_map_is_subset(
7855 struct isl_basic_map
*bmap1
, struct isl_basic_map
*bmap2
)
7858 struct isl_map
*map1
;
7859 struct isl_map
*map2
;
7861 if (!bmap1
|| !bmap2
)
7864 map1
= isl_map_from_basic_map(isl_basic_map_copy(bmap1
));
7865 map2
= isl_map_from_basic_map(isl_basic_map_copy(bmap2
));
7867 is_subset
= isl_map_is_subset(map1
, map2
);
7875 int isl_basic_set_is_subset(__isl_keep isl_basic_set
*bset1
,
7876 __isl_keep isl_basic_set
*bset2
)
7878 return isl_basic_map_is_subset(bset1
, bset2
);
7881 int isl_basic_map_is_equal(
7882 struct isl_basic_map
*bmap1
, struct isl_basic_map
*bmap2
)
7886 if (!bmap1
|| !bmap2
)
7888 is_subset
= isl_basic_map_is_subset(bmap1
, bmap2
);
7891 is_subset
= isl_basic_map_is_subset(bmap2
, bmap1
);
7895 int isl_basic_set_is_equal(
7896 struct isl_basic_set
*bset1
, struct isl_basic_set
*bset2
)
7898 return isl_basic_map_is_equal(
7899 (struct isl_basic_map
*)bset1
, (struct isl_basic_map
*)bset2
);
7902 int isl_map_is_empty(struct isl_map
*map
)
7909 for (i
= 0; i
< map
->n
; ++i
) {
7910 is_empty
= isl_basic_map_is_empty(map
->p
[i
]);
7919 int isl_map_plain_is_empty(__isl_keep isl_map
*map
)
7921 return map
? map
->n
== 0 : -1;
7924 int isl_map_fast_is_empty(__isl_keep isl_map
*map
)
7926 return isl_map_plain_is_empty(map
);
7929 int isl_set_plain_is_empty(struct isl_set
*set
)
7931 return set
? set
->n
== 0 : -1;
7934 int isl_set_fast_is_empty(__isl_keep isl_set
*set
)
7936 return isl_set_plain_is_empty(set
);
7939 int isl_set_is_empty(struct isl_set
*set
)
7941 return isl_map_is_empty((struct isl_map
*)set
);
7944 int isl_map_has_equal_space(__isl_keep isl_map
*map1
, __isl_keep isl_map
*map2
)
7949 return isl_space_is_equal(map1
->dim
, map2
->dim
);
7952 int isl_set_has_equal_space(__isl_keep isl_set
*set1
, __isl_keep isl_set
*set2
)
7957 return isl_space_is_equal(set1
->dim
, set2
->dim
);
7960 static int map_is_equal(__isl_keep isl_map
*map1
, __isl_keep isl_map
*map2
)
7966 is_subset
= isl_map_is_subset(map1
, map2
);
7969 is_subset
= isl_map_is_subset(map2
, map1
);
7973 int isl_map_is_equal(__isl_keep isl_map
*map1
, __isl_keep isl_map
*map2
)
7975 return isl_map_align_params_map_map_and_test(map1
, map2
, &map_is_equal
);
7978 int isl_basic_map_is_strict_subset(
7979 struct isl_basic_map
*bmap1
, struct isl_basic_map
*bmap2
)
7983 if (!bmap1
|| !bmap2
)
7985 is_subset
= isl_basic_map_is_subset(bmap1
, bmap2
);
7988 is_subset
= isl_basic_map_is_subset(bmap2
, bmap1
);
7989 if (is_subset
== -1)
7994 int isl_map_is_strict_subset(struct isl_map
*map1
, struct isl_map
*map2
)
8000 is_subset
= isl_map_is_subset(map1
, map2
);
8003 is_subset
= isl_map_is_subset(map2
, map1
);
8004 if (is_subset
== -1)
8009 int isl_set_is_strict_subset(__isl_keep isl_set
*set1
, __isl_keep isl_set
*set2
)
8011 return isl_map_is_strict_subset((isl_map
*)set1
, (isl_map
*)set2
);
8014 int isl_basic_map_is_universe(struct isl_basic_map
*bmap
)
8018 return bmap
->n_eq
== 0 && bmap
->n_ineq
== 0;
8021 int isl_basic_set_is_universe(struct isl_basic_set
*bset
)
8025 return bset
->n_eq
== 0 && bset
->n_ineq
== 0;
8028 int isl_map_plain_is_universe(__isl_keep isl_map
*map
)
8035 for (i
= 0; i
< map
->n
; ++i
) {
8036 int r
= isl_basic_map_is_universe(map
->p
[i
]);
8044 int isl_set_plain_is_universe(__isl_keep isl_set
*set
)
8046 return isl_map_plain_is_universe((isl_map
*) set
);
8049 int isl_set_fast_is_universe(__isl_keep isl_set
*set
)
8051 return isl_set_plain_is_universe(set
);
8054 int isl_basic_map_is_empty(struct isl_basic_map
*bmap
)
8056 struct isl_basic_set
*bset
= NULL
;
8057 struct isl_vec
*sample
= NULL
;
8064 if (ISL_F_ISSET(bmap
, ISL_BASIC_MAP_EMPTY
))
8067 if (isl_basic_map_is_universe(bmap
))
8070 if (ISL_F_ISSET(bmap
, ISL_BASIC_MAP_RATIONAL
)) {
8071 struct isl_basic_map
*copy
= isl_basic_map_copy(bmap
);
8072 copy
= isl_basic_map_remove_redundancies(copy
);
8073 empty
= isl_basic_map_plain_is_empty(copy
);
8074 isl_basic_map_free(copy
);
8078 total
= 1 + isl_basic_map_total_dim(bmap
);
8079 if (bmap
->sample
&& bmap
->sample
->size
== total
) {
8080 int contains
= isl_basic_map_contains(bmap
, bmap
->sample
);
8086 isl_vec_free(bmap
->sample
);
8087 bmap
->sample
= NULL
;
8088 bset
= isl_basic_map_underlying_set(isl_basic_map_copy(bmap
));
8091 sample
= isl_basic_set_sample_vec(bset
);
8094 empty
= sample
->size
== 0;
8095 isl_vec_free(bmap
->sample
);
8096 bmap
->sample
= sample
;
8098 ISL_F_SET(bmap
, ISL_BASIC_MAP_EMPTY
);
8103 int isl_basic_map_plain_is_empty(__isl_keep isl_basic_map
*bmap
)
8107 return ISL_F_ISSET(bmap
, ISL_BASIC_MAP_EMPTY
);
8110 int isl_basic_map_fast_is_empty(__isl_keep isl_basic_map
*bmap
)
8112 return isl_basic_map_plain_is_empty(bmap
);
8115 int isl_basic_set_plain_is_empty(__isl_keep isl_basic_set
*bset
)
8119 return ISL_F_ISSET(bset
, ISL_BASIC_SET_EMPTY
);
8122 int isl_basic_set_fast_is_empty(__isl_keep isl_basic_set
*bset
)
8124 return isl_basic_set_plain_is_empty(bset
);
8127 int isl_basic_set_is_empty(struct isl_basic_set
*bset
)
8129 return isl_basic_map_is_empty((struct isl_basic_map
*)bset
);
8132 struct isl_map
*isl_basic_map_union(
8133 struct isl_basic_map
*bmap1
, struct isl_basic_map
*bmap2
)
8135 struct isl_map
*map
;
8136 if (!bmap1
|| !bmap2
)
8139 isl_assert(bmap1
->ctx
, isl_space_is_equal(bmap1
->dim
, bmap2
->dim
), goto error
);
8141 map
= isl_map_alloc_space(isl_space_copy(bmap1
->dim
), 2, 0);
8144 map
= isl_map_add_basic_map(map
, bmap1
);
8145 map
= isl_map_add_basic_map(map
, bmap2
);
8148 isl_basic_map_free(bmap1
);
8149 isl_basic_map_free(bmap2
);
8153 struct isl_set
*isl_basic_set_union(
8154 struct isl_basic_set
*bset1
, struct isl_basic_set
*bset2
)
8156 return (struct isl_set
*)isl_basic_map_union(
8157 (struct isl_basic_map
*)bset1
,
8158 (struct isl_basic_map
*)bset2
);
8161 /* Order divs such that any div only depends on previous divs */
8162 struct isl_basic_map
*isl_basic_map_order_divs(struct isl_basic_map
*bmap
)
8170 off
= isl_space_dim(bmap
->dim
, isl_dim_all
);
8172 for (i
= 0; i
< bmap
->n_div
; ++i
) {
8174 if (isl_int_is_zero(bmap
->div
[i
][0]))
8176 pos
= isl_seq_first_non_zero(bmap
->div
[i
]+1+1+off
+i
,
8180 isl_basic_map_swap_div(bmap
, i
, i
+ pos
);
8186 struct isl_basic_set
*isl_basic_set_order_divs(struct isl_basic_set
*bset
)
8188 return (struct isl_basic_set
*)
8189 isl_basic_map_order_divs((struct isl_basic_map
*)bset
);
8192 __isl_give isl_map
*isl_map_order_divs(__isl_take isl_map
*map
)
8199 for (i
= 0; i
< map
->n
; ++i
) {
8200 map
->p
[i
] = isl_basic_map_order_divs(map
->p
[i
]);
8211 /* Apply the expansion computed by isl_merge_divs.
8212 * The expansion itself is given by "exp" while the resulting
8213 * list of divs is given by "div".
8215 __isl_give isl_basic_set
*isl_basic_set_expand_divs(
8216 __isl_take isl_basic_set
*bset
, __isl_take isl_mat
*div
, int *exp
)
8221 bset
= isl_basic_set_cow(bset
);
8225 if (div
->n_row
< bset
->n_div
)
8226 isl_die(isl_mat_get_ctx(div
), isl_error_invalid
,
8227 "not an expansion", goto error
);
8229 n_div
= bset
->n_div
;
8230 bset
= isl_basic_map_extend_space(bset
, isl_space_copy(bset
->dim
),
8231 div
->n_row
- n_div
, 0,
8232 2 * (div
->n_row
- n_div
));
8234 for (i
= n_div
; i
< div
->n_row
; ++i
)
8235 if (isl_basic_set_alloc_div(bset
) < 0)
8239 for (i
= div
->n_row
- 1; i
>= 0; --i
) {
8240 if (j
>= 0 && exp
[j
] == i
) {
8242 isl_basic_map_swap_div(bset
, i
, j
);
8245 isl_seq_cpy(bset
->div
[i
], div
->row
[i
], div
->n_col
);
8246 if (isl_basic_map_add_div_constraints(bset
, i
) < 0)
8254 isl_basic_set_free(bset
);
8259 /* Look for a div in dst that corresponds to the div "div" in src.
8260 * The divs before "div" in src and dst are assumed to be the same.
8262 * Returns -1 if no corresponding div was found and the position
8263 * of the corresponding div in dst otherwise.
8265 static int find_div(struct isl_basic_map
*dst
,
8266 struct isl_basic_map
*src
, unsigned div
)
8270 unsigned total
= isl_space_dim(src
->dim
, isl_dim_all
);
8272 isl_assert(dst
->ctx
, div
<= dst
->n_div
, return -1);
8273 for (i
= div
; i
< dst
->n_div
; ++i
)
8274 if (isl_seq_eq(dst
->div
[i
], src
->div
[div
], 1+1+total
+div
) &&
8275 isl_seq_first_non_zero(dst
->div
[i
]+1+1+total
+div
,
8276 dst
->n_div
- div
) == -1)
8281 /* Align the divs of "dst" to those of "src", adding divs from "src"
8282 * if needed. That is, make sure that the first src->n_div divs
8283 * of the result are equal to those of src.
8285 * The result is not finalized as by design it will have redundant
8286 * divs if any divs from "src" were copied.
8288 __isl_give isl_basic_map
*isl_basic_map_align_divs(
8289 __isl_take isl_basic_map
*dst
, __isl_keep isl_basic_map
*src
)
8292 int known
, extended
;
8296 return isl_basic_map_free(dst
);
8298 if (src
->n_div
== 0)
8301 known
= isl_basic_map_divs_known(src
);
8303 return isl_basic_map_free(dst
);
8305 isl_die(isl_basic_map_get_ctx(src
), isl_error_invalid
,
8306 "some src divs are unknown",
8307 return isl_basic_map_free(dst
));
8309 src
= isl_basic_map_order_divs(src
);
8312 total
= isl_space_dim(src
->dim
, isl_dim_all
);
8313 for (i
= 0; i
< src
->n_div
; ++i
) {
8314 int j
= find_div(dst
, src
, i
);
8317 int extra
= src
->n_div
- i
;
8318 dst
= isl_basic_map_cow(dst
);
8321 dst
= isl_basic_map_extend_space(dst
,
8322 isl_space_copy(dst
->dim
),
8323 extra
, 0, 2 * extra
);
8326 j
= isl_basic_map_alloc_div(dst
);
8328 return isl_basic_map_free(dst
);
8329 isl_seq_cpy(dst
->div
[j
], src
->div
[i
], 1+1+total
+i
);
8330 isl_seq_clr(dst
->div
[j
]+1+1+total
+i
, dst
->n_div
- i
);
8331 if (isl_basic_map_add_div_constraints(dst
, j
) < 0)
8332 return isl_basic_map_free(dst
);
8335 isl_basic_map_swap_div(dst
, i
, j
);
8340 struct isl_basic_set
*isl_basic_set_align_divs(
8341 struct isl_basic_set
*dst
, struct isl_basic_set
*src
)
8343 return (struct isl_basic_set
*)isl_basic_map_align_divs(
8344 (struct isl_basic_map
*)dst
, (struct isl_basic_map
*)src
);
8347 struct isl_map
*isl_map_align_divs(struct isl_map
*map
)
8355 map
= isl_map_compute_divs(map
);
8356 map
= isl_map_cow(map
);
8360 for (i
= 1; i
< map
->n
; ++i
)
8361 map
->p
[0] = isl_basic_map_align_divs(map
->p
[0], map
->p
[i
]);
8362 for (i
= 1; i
< map
->n
; ++i
) {
8363 map
->p
[i
] = isl_basic_map_align_divs(map
->p
[i
], map
->p
[0]);
8365 return isl_map_free(map
);
8368 ISL_F_CLR(map
, ISL_MAP_NORMALIZED
);
8372 struct isl_set
*isl_set_align_divs(struct isl_set
*set
)
8374 return (struct isl_set
*)isl_map_align_divs((struct isl_map
*)set
);
8377 /* Align the divs of the basic sets in "set" to those
8378 * of the basic sets in "list", as well as to the other basic sets in "set".
8379 * The elements in "list" are assumed to have known divs.
8381 __isl_give isl_set
*isl_set_align_divs_to_basic_set_list(
8382 __isl_take isl_set
*set
, __isl_keep isl_basic_set_list
*list
)
8386 set
= isl_set_compute_divs(set
);
8387 set
= isl_set_cow(set
);
8389 return isl_set_free(set
);
8393 n
= isl_basic_set_list_n_basic_set(list
);
8394 for (i
= 0; i
< n
; ++i
) {
8395 isl_basic_set
*bset
;
8397 bset
= isl_basic_set_list_get_basic_set(list
, i
);
8398 set
->p
[0] = isl_basic_set_align_divs(set
->p
[0], bset
);
8399 isl_basic_set_free(bset
);
8402 return isl_set_free(set
);
8404 return isl_set_align_divs(set
);
8407 /* Align the divs of each element of "list" to those of "bset".
8408 * Both "bset" and the elements of "list" are assumed to have known divs.
8410 __isl_give isl_basic_set_list
*isl_basic_set_list_align_divs_to_basic_set(
8411 __isl_take isl_basic_set_list
*list
, __isl_keep isl_basic_set
*bset
)
8416 return isl_basic_set_list_free(list
);
8418 n
= isl_basic_set_list_n_basic_set(list
);
8419 for (i
= 0; i
< n
; ++i
) {
8420 isl_basic_set
*bset_i
;
8422 bset_i
= isl_basic_set_list_get_basic_set(list
, i
);
8423 bset_i
= isl_basic_set_align_divs(bset_i
, bset
);
8424 list
= isl_basic_set_list_set_basic_set(list
, i
, bset_i
);
8430 static __isl_give isl_set
*set_apply( __isl_take isl_set
*set
,
8431 __isl_take isl_map
*map
)
8435 isl_assert(set
->ctx
, isl_map_compatible_domain(map
, set
), goto error
);
8436 map
= isl_map_intersect_domain(map
, set
);
8437 set
= isl_map_range(map
);
8445 __isl_give isl_set
*isl_set_apply( __isl_take isl_set
*set
,
8446 __isl_take isl_map
*map
)
8448 return isl_map_align_params_map_map_and(set
, map
, &set_apply
);
8451 /* There is no need to cow as removing empty parts doesn't change
8452 * the meaning of the set.
8454 struct isl_map
*isl_map_remove_empty_parts(struct isl_map
*map
)
8461 for (i
= map
->n
- 1; i
>= 0; --i
)
8462 remove_if_empty(map
, i
);
8467 struct isl_set
*isl_set_remove_empty_parts(struct isl_set
*set
)
8469 return (struct isl_set
*)
8470 isl_map_remove_empty_parts((struct isl_map
*)set
);
8473 struct isl_basic_map
*isl_map_copy_basic_map(struct isl_map
*map
)
8475 struct isl_basic_map
*bmap
;
8476 if (!map
|| map
->n
== 0)
8478 bmap
= map
->p
[map
->n
-1];
8479 isl_assert(map
->ctx
, ISL_F_ISSET(bmap
, ISL_BASIC_SET_FINAL
), return NULL
);
8480 return isl_basic_map_copy(bmap
);
8483 struct isl_basic_set
*isl_set_copy_basic_set(struct isl_set
*set
)
8485 return (struct isl_basic_set
*)
8486 isl_map_copy_basic_map((struct isl_map
*)set
);
8489 __isl_give isl_map
*isl_map_drop_basic_map(__isl_take isl_map
*map
,
8490 __isl_keep isl_basic_map
*bmap
)
8496 for (i
= map
->n
-1; i
>= 0; --i
) {
8497 if (map
->p
[i
] != bmap
)
8499 map
= isl_map_cow(map
);
8502 isl_basic_map_free(map
->p
[i
]);
8503 if (i
!= map
->n
-1) {
8504 ISL_F_CLR(map
, ISL_SET_NORMALIZED
);
8505 map
->p
[i
] = map
->p
[map
->n
-1];
8516 struct isl_set
*isl_set_drop_basic_set(struct isl_set
*set
,
8517 struct isl_basic_set
*bset
)
8519 return (struct isl_set
*)isl_map_drop_basic_map((struct isl_map
*)set
,
8520 (struct isl_basic_map
*)bset
);
8523 /* Given two basic sets bset1 and bset2, compute the maximal difference
8524 * between the values of dimension pos in bset1 and those in bset2
8525 * for any common value of the parameters and dimensions preceding pos.
8527 static enum isl_lp_result
basic_set_maximal_difference_at(
8528 __isl_keep isl_basic_set
*bset1
, __isl_keep isl_basic_set
*bset2
,
8529 int pos
, isl_int
*opt
)
8532 struct isl_basic_map
*bmap1
= NULL
;
8533 struct isl_basic_map
*bmap2
= NULL
;
8534 struct isl_ctx
*ctx
;
8535 struct isl_vec
*obj
;
8538 unsigned dim1
, dim2
;
8539 enum isl_lp_result res
;
8541 if (!bset1
|| !bset2
)
8542 return isl_lp_error
;
8544 nparam
= isl_basic_set_n_param(bset1
);
8545 dim1
= isl_basic_set_n_dim(bset1
);
8546 dim2
= isl_basic_set_n_dim(bset2
);
8547 dims
= isl_space_alloc(bset1
->ctx
, nparam
, pos
, dim1
- pos
);
8548 bmap1
= isl_basic_map_from_basic_set(isl_basic_set_copy(bset1
), dims
);
8549 dims
= isl_space_alloc(bset2
->ctx
, nparam
, pos
, dim2
- pos
);
8550 bmap2
= isl_basic_map_from_basic_set(isl_basic_set_copy(bset2
), dims
);
8551 if (!bmap1
|| !bmap2
)
8553 bmap1
= isl_basic_map_cow(bmap1
);
8554 bmap1
= isl_basic_map_extend(bmap1
, nparam
,
8555 pos
, (dim1
- pos
) + (dim2
- pos
),
8556 bmap2
->n_div
, bmap2
->n_eq
, bmap2
->n_ineq
);
8557 bmap1
= add_constraints(bmap1
, bmap2
, 0, dim1
- pos
);
8560 total
= isl_basic_map_total_dim(bmap1
);
8562 obj
= isl_vec_alloc(ctx
, 1 + total
);
8565 isl_seq_clr(obj
->block
.data
, 1 + total
);
8566 isl_int_set_si(obj
->block
.data
[1+nparam
+pos
], 1);
8567 isl_int_set_si(obj
->block
.data
[1+nparam
+pos
+(dim1
-pos
)], -1);
8568 res
= isl_basic_map_solve_lp(bmap1
, 1, obj
->block
.data
, ctx
->one
,
8570 isl_basic_map_free(bmap1
);
8574 isl_basic_map_free(bmap2
);
8576 isl_basic_map_free(bmap1
);
8577 return isl_lp_error
;
8580 /* Given two _disjoint_ basic sets bset1 and bset2, check whether
8581 * for any common value of the parameters and dimensions preceding pos
8582 * in both basic sets, the values of dimension pos in bset1 are
8583 * smaller or larger than those in bset2.
8586 * 1 if bset1 follows bset2
8587 * -1 if bset1 precedes bset2
8588 * 0 if bset1 and bset2 are incomparable
8589 * -2 if some error occurred.
8591 int isl_basic_set_compare_at(struct isl_basic_set
*bset1
,
8592 struct isl_basic_set
*bset2
, int pos
)
8595 enum isl_lp_result res
;
8600 res
= basic_set_maximal_difference_at(bset1
, bset2
, pos
, &opt
);
8602 if (res
== isl_lp_empty
)
8604 else if ((res
== isl_lp_ok
&& isl_int_is_pos(opt
)) ||
8605 res
== isl_lp_unbounded
)
8607 else if (res
== isl_lp_ok
&& isl_int_is_neg(opt
))
8616 /* Given two basic sets bset1 and bset2, check whether
8617 * for any common value of the parameters and dimensions preceding pos
8618 * there is a value of dimension pos in bset1 that is larger
8619 * than a value of the same dimension in bset2.
8622 * 1 if there exists such a pair
8623 * 0 if there is no such pair, but there is a pair of equal values
8625 * -2 if some error occurred.
8627 int isl_basic_set_follows_at(__isl_keep isl_basic_set
*bset1
,
8628 __isl_keep isl_basic_set
*bset2
, int pos
)
8631 enum isl_lp_result res
;
8636 res
= basic_set_maximal_difference_at(bset1
, bset2
, pos
, &opt
);
8638 if (res
== isl_lp_empty
)
8640 else if ((res
== isl_lp_ok
&& isl_int_is_pos(opt
)) ||
8641 res
== isl_lp_unbounded
)
8643 else if (res
== isl_lp_ok
&& isl_int_is_neg(opt
))
8645 else if (res
== isl_lp_ok
)
8654 /* Given two sets set1 and set2, check whether
8655 * for any common value of the parameters and dimensions preceding pos
8656 * there is a value of dimension pos in set1 that is larger
8657 * than a value of the same dimension in set2.
8660 * 1 if there exists such a pair
8661 * 0 if there is no such pair, but there is a pair of equal values
8663 * -2 if some error occurred.
8665 int isl_set_follows_at(__isl_keep isl_set
*set1
,
8666 __isl_keep isl_set
*set2
, int pos
)
8674 for (i
= 0; i
< set1
->n
; ++i
)
8675 for (j
= 0; j
< set2
->n
; ++j
) {
8677 f
= isl_basic_set_follows_at(set1
->p
[i
], set2
->p
[j
], pos
);
8678 if (f
== 1 || f
== -2)
8687 static int isl_basic_map_plain_has_fixed_var(__isl_keep isl_basic_map
*bmap
,
8688 unsigned pos
, isl_int
*val
)
8696 total
= isl_basic_map_total_dim(bmap
);
8697 for (i
= 0, d
= total
-1; i
< bmap
->n_eq
&& d
+1 > pos
; ++i
) {
8698 for (; d
+1 > pos
; --d
)
8699 if (!isl_int_is_zero(bmap
->eq
[i
][1+d
]))
8703 if (isl_seq_first_non_zero(bmap
->eq
[i
]+1, d
) != -1)
8705 if (isl_seq_first_non_zero(bmap
->eq
[i
]+1+d
+1, total
-d
-1) != -1)
8707 if (!isl_int_is_one(bmap
->eq
[i
][1+d
]))
8710 isl_int_neg(*val
, bmap
->eq
[i
][0]);
8716 static int isl_map_plain_has_fixed_var(__isl_keep isl_map
*map
,
8717 unsigned pos
, isl_int
*val
)
8729 return isl_basic_map_plain_has_fixed_var(map
->p
[0], pos
, val
);
8732 fixed
= isl_basic_map_plain_has_fixed_var(map
->p
[0], pos
, &v
);
8733 for (i
= 1; fixed
== 1 && i
< map
->n
; ++i
) {
8734 fixed
= isl_basic_map_plain_has_fixed_var(map
->p
[i
], pos
, &tmp
);
8735 if (fixed
== 1 && isl_int_ne(tmp
, v
))
8739 isl_int_set(*val
, v
);
8745 static int isl_basic_set_plain_has_fixed_var(__isl_keep isl_basic_set
*bset
,
8746 unsigned pos
, isl_int
*val
)
8748 return isl_basic_map_plain_has_fixed_var((struct isl_basic_map
*)bset
,
8752 static int isl_set_plain_has_fixed_var(__isl_keep isl_set
*set
, unsigned pos
,
8755 return isl_map_plain_has_fixed_var((struct isl_map
*)set
, pos
, val
);
8758 int isl_basic_map_plain_is_fixed(__isl_keep isl_basic_map
*bmap
,
8759 enum isl_dim_type type
, unsigned pos
, isl_int
*val
)
8761 if (pos
>= isl_basic_map_dim(bmap
, type
))
8763 return isl_basic_map_plain_has_fixed_var(bmap
,
8764 isl_basic_map_offset(bmap
, type
) - 1 + pos
, val
);
8767 /* If "bmap" obviously lies on a hyperplane where the given dimension
8768 * has a fixed value, then return that value.
8769 * Otherwise return NaN.
8771 __isl_give isl_val
*isl_basic_map_plain_get_val_if_fixed(
8772 __isl_keep isl_basic_map
*bmap
,
8773 enum isl_dim_type type
, unsigned pos
)
8781 ctx
= isl_basic_map_get_ctx(bmap
);
8782 v
= isl_val_alloc(ctx
);
8785 fixed
= isl_basic_map_plain_is_fixed(bmap
, type
, pos
, &v
->n
);
8787 return isl_val_free(v
);
8789 isl_int_set_si(v
->d
, 1);
8793 return isl_val_nan(ctx
);
8796 int isl_map_plain_is_fixed(__isl_keep isl_map
*map
,
8797 enum isl_dim_type type
, unsigned pos
, isl_int
*val
)
8799 if (pos
>= isl_map_dim(map
, type
))
8801 return isl_map_plain_has_fixed_var(map
,
8802 map_offset(map
, type
) - 1 + pos
, val
);
8805 /* If "map" obviously lies on a hyperplane where the given dimension
8806 * has a fixed value, then return that value.
8807 * Otherwise return NaN.
8809 __isl_give isl_val
*isl_map_plain_get_val_if_fixed(__isl_keep isl_map
*map
,
8810 enum isl_dim_type type
, unsigned pos
)
8818 ctx
= isl_map_get_ctx(map
);
8819 v
= isl_val_alloc(ctx
);
8822 fixed
= isl_map_plain_is_fixed(map
, type
, pos
, &v
->n
);
8824 return isl_val_free(v
);
8826 isl_int_set_si(v
->d
, 1);
8830 return isl_val_nan(ctx
);
8833 /* If "set" obviously lies on a hyperplane where the given dimension
8834 * has a fixed value, then return that value.
8835 * Otherwise return NaN.
8837 __isl_give isl_val
*isl_set_plain_get_val_if_fixed(__isl_keep isl_set
*set
,
8838 enum isl_dim_type type
, unsigned pos
)
8840 return isl_map_plain_get_val_if_fixed(set
, type
, pos
);
8843 int isl_set_plain_is_fixed(__isl_keep isl_set
*set
,
8844 enum isl_dim_type type
, unsigned pos
, isl_int
*val
)
8846 return isl_map_plain_is_fixed(set
, type
, pos
, val
);
8849 int isl_map_fast_is_fixed(__isl_keep isl_map
*map
,
8850 enum isl_dim_type type
, unsigned pos
, isl_int
*val
)
8852 return isl_map_plain_is_fixed(map
, type
, pos
, val
);
8855 /* Check if dimension dim has fixed value and if so and if val is not NULL,
8856 * then return this fixed value in *val.
8858 int isl_basic_set_plain_dim_is_fixed(__isl_keep isl_basic_set
*bset
,
8859 unsigned dim
, isl_int
*val
)
8861 return isl_basic_set_plain_has_fixed_var(bset
,
8862 isl_basic_set_n_param(bset
) + dim
, val
);
8865 /* Check if dimension dim has fixed value and if so and if val is not NULL,
8866 * then return this fixed value in *val.
8868 int isl_set_plain_dim_is_fixed(__isl_keep isl_set
*set
,
8869 unsigned dim
, isl_int
*val
)
8871 return isl_set_plain_has_fixed_var(set
, isl_set_n_param(set
) + dim
, val
);
8874 int isl_set_fast_dim_is_fixed(__isl_keep isl_set
*set
,
8875 unsigned dim
, isl_int
*val
)
8877 return isl_set_plain_dim_is_fixed(set
, dim
, val
);
8880 /* Check if input variable in has fixed value and if so and if val is not NULL,
8881 * then return this fixed value in *val.
8883 int isl_map_plain_input_is_fixed(__isl_keep isl_map
*map
,
8884 unsigned in
, isl_int
*val
)
8886 return isl_map_plain_has_fixed_var(map
, isl_map_n_param(map
) + in
, val
);
8889 /* Check if dimension dim has an (obvious) fixed lower bound and if so
8890 * and if val is not NULL, then return this lower bound in *val.
8892 int isl_basic_set_plain_dim_has_fixed_lower_bound(
8893 __isl_keep isl_basic_set
*bset
, unsigned dim
, isl_int
*val
)
8895 int i
, i_eq
= -1, i_ineq
= -1;
8902 total
= isl_basic_set_total_dim(bset
);
8903 nparam
= isl_basic_set_n_param(bset
);
8904 for (i
= 0; i
< bset
->n_eq
; ++i
) {
8905 if (isl_int_is_zero(bset
->eq
[i
][1+nparam
+dim
]))
8911 for (i
= 0; i
< bset
->n_ineq
; ++i
) {
8912 if (!isl_int_is_pos(bset
->ineq
[i
][1+nparam
+dim
]))
8914 if (i_eq
!= -1 || i_ineq
!= -1)
8918 if (i_eq
== -1 && i_ineq
== -1)
8920 c
= i_eq
!= -1 ? bset
->eq
[i_eq
] : bset
->ineq
[i_ineq
];
8921 /* The coefficient should always be one due to normalization. */
8922 if (!isl_int_is_one(c
[1+nparam
+dim
]))
8924 if (isl_seq_first_non_zero(c
+1, nparam
+dim
) != -1)
8926 if (isl_seq_first_non_zero(c
+1+nparam
+dim
+1,
8927 total
- nparam
- dim
- 1) != -1)
8930 isl_int_neg(*val
, c
[0]);
8934 int isl_set_plain_dim_has_fixed_lower_bound(__isl_keep isl_set
*set
,
8935 unsigned dim
, isl_int
*val
)
8947 return isl_basic_set_plain_dim_has_fixed_lower_bound(set
->p
[0],
8951 fixed
= isl_basic_set_plain_dim_has_fixed_lower_bound(set
->p
[0],
8953 for (i
= 1; fixed
== 1 && i
< set
->n
; ++i
) {
8954 fixed
= isl_basic_set_plain_dim_has_fixed_lower_bound(set
->p
[i
],
8956 if (fixed
== 1 && isl_int_ne(tmp
, v
))
8960 isl_int_set(*val
, v
);
8966 /* uset_gist depends on constraints without existentially quantified
8967 * variables sorting first.
8969 static int sort_constraint_cmp(const void *p1
, const void *p2
, void *arg
)
8971 isl_int
**c1
= (isl_int
**) p1
;
8972 isl_int
**c2
= (isl_int
**) p2
;
8974 unsigned size
= *(unsigned *) arg
;
8976 l1
= isl_seq_last_non_zero(*c1
+ 1, size
);
8977 l2
= isl_seq_last_non_zero(*c2
+ 1, size
);
8982 return isl_seq_cmp(*c1
+ 1, *c2
+ 1, size
);
8985 static struct isl_basic_map
*isl_basic_map_sort_constraints(
8986 struct isl_basic_map
*bmap
)
8992 if (bmap
->n_ineq
== 0)
8994 if (ISL_F_ISSET(bmap
, ISL_BASIC_MAP_NORMALIZED
))
8996 total
= isl_basic_map_total_dim(bmap
);
8997 if (isl_sort(bmap
->ineq
, bmap
->n_ineq
, sizeof(isl_int
*),
8998 &sort_constraint_cmp
, &total
) < 0)
8999 return isl_basic_map_free(bmap
);
9003 __isl_give isl_basic_set
*isl_basic_set_sort_constraints(
9004 __isl_take isl_basic_set
*bset
)
9006 return (struct isl_basic_set
*)isl_basic_map_sort_constraints(
9007 (struct isl_basic_map
*)bset
);
9010 struct isl_basic_map
*isl_basic_map_normalize(struct isl_basic_map
*bmap
)
9014 if (ISL_F_ISSET(bmap
, ISL_BASIC_MAP_NORMALIZED
))
9016 bmap
= isl_basic_map_remove_redundancies(bmap
);
9017 bmap
= isl_basic_map_sort_constraints(bmap
);
9019 ISL_F_SET(bmap
, ISL_BASIC_MAP_NORMALIZED
);
9023 struct isl_basic_set
*isl_basic_set_normalize(struct isl_basic_set
*bset
)
9025 return (struct isl_basic_set
*)isl_basic_map_normalize(
9026 (struct isl_basic_map
*)bset
);
9029 int isl_basic_map_plain_cmp(const __isl_keep isl_basic_map
*bmap1
,
9030 const __isl_keep isl_basic_map
*bmap2
)
9035 if (!bmap1
|| !bmap2
)
9040 if (ISL_F_ISSET(bmap1
, ISL_BASIC_MAP_RATIONAL
) !=
9041 ISL_F_ISSET(bmap2
, ISL_BASIC_MAP_RATIONAL
))
9042 return ISL_F_ISSET(bmap1
, ISL_BASIC_MAP_RATIONAL
) ? -1 : 1;
9043 if (isl_basic_map_n_param(bmap1
) != isl_basic_map_n_param(bmap2
))
9044 return isl_basic_map_n_param(bmap1
) - isl_basic_map_n_param(bmap2
);
9045 if (isl_basic_map_n_in(bmap1
) != isl_basic_map_n_in(bmap2
))
9046 return isl_basic_map_n_out(bmap1
) - isl_basic_map_n_out(bmap2
);
9047 if (isl_basic_map_n_out(bmap1
) != isl_basic_map_n_out(bmap2
))
9048 return isl_basic_map_n_out(bmap1
) - isl_basic_map_n_out(bmap2
);
9049 if (ISL_F_ISSET(bmap1
, ISL_BASIC_MAP_EMPTY
) &&
9050 ISL_F_ISSET(bmap2
, ISL_BASIC_MAP_EMPTY
))
9052 if (ISL_F_ISSET(bmap1
, ISL_BASIC_MAP_EMPTY
))
9054 if (ISL_F_ISSET(bmap2
, ISL_BASIC_MAP_EMPTY
))
9056 if (bmap1
->n_eq
!= bmap2
->n_eq
)
9057 return bmap1
->n_eq
- bmap2
->n_eq
;
9058 if (bmap1
->n_ineq
!= bmap2
->n_ineq
)
9059 return bmap1
->n_ineq
- bmap2
->n_ineq
;
9060 if (bmap1
->n_div
!= bmap2
->n_div
)
9061 return bmap1
->n_div
- bmap2
->n_div
;
9062 total
= isl_basic_map_total_dim(bmap1
);
9063 for (i
= 0; i
< bmap1
->n_eq
; ++i
) {
9064 cmp
= isl_seq_cmp(bmap1
->eq
[i
], bmap2
->eq
[i
], 1+total
);
9068 for (i
= 0; i
< bmap1
->n_ineq
; ++i
) {
9069 cmp
= isl_seq_cmp(bmap1
->ineq
[i
], bmap2
->ineq
[i
], 1+total
);
9073 for (i
= 0; i
< bmap1
->n_div
; ++i
) {
9074 cmp
= isl_seq_cmp(bmap1
->div
[i
], bmap2
->div
[i
], 1+1+total
);
9081 int isl_basic_set_plain_cmp(const __isl_keep isl_basic_set
*bset1
,
9082 const __isl_keep isl_basic_set
*bset2
)
9084 return isl_basic_map_plain_cmp(bset1
, bset2
);
9087 int isl_set_plain_cmp(__isl_keep isl_set
*set1
, __isl_keep isl_set
*set2
)
9093 if (set1
->n
!= set2
->n
)
9094 return set1
->n
- set2
->n
;
9096 for (i
= 0; i
< set1
->n
; ++i
) {
9097 cmp
= isl_basic_set_plain_cmp(set1
->p
[i
], set2
->p
[i
]);
9105 int isl_basic_map_plain_is_equal(__isl_keep isl_basic_map
*bmap1
,
9106 __isl_keep isl_basic_map
*bmap2
)
9108 return isl_basic_map_plain_cmp(bmap1
, bmap2
) == 0;
9111 int isl_basic_set_plain_is_equal(__isl_keep isl_basic_set
*bset1
,
9112 __isl_keep isl_basic_set
*bset2
)
9114 return isl_basic_map_plain_is_equal((isl_basic_map
*)bset1
,
9115 (isl_basic_map
*)bset2
);
9118 static int qsort_bmap_cmp(const void *p1
, const void *p2
)
9120 const struct isl_basic_map
*bmap1
= *(const struct isl_basic_map
**)p1
;
9121 const struct isl_basic_map
*bmap2
= *(const struct isl_basic_map
**)p2
;
9123 return isl_basic_map_plain_cmp(bmap1
, bmap2
);
9126 /* Sort the basic maps of "map" and remove duplicate basic maps.
9128 * While removing basic maps, we make sure that the basic maps remain
9129 * sorted because isl_map_normalize expects the basic maps of the result
9132 static __isl_give isl_map
*sort_and_remove_duplicates(__isl_take isl_map
*map
)
9136 map
= isl_map_remove_empty_parts(map
);
9139 qsort(map
->p
, map
->n
, sizeof(struct isl_basic_map
*), qsort_bmap_cmp
);
9140 for (i
= map
->n
- 1; i
>= 1; --i
) {
9141 if (!isl_basic_map_plain_is_equal(map
->p
[i
- 1], map
->p
[i
]))
9143 isl_basic_map_free(map
->p
[i
-1]);
9144 for (j
= i
; j
< map
->n
; ++j
)
9145 map
->p
[j
- 1] = map
->p
[j
];
9152 /* Remove obvious duplicates among the basic maps of "map".
9154 * Unlike isl_map_normalize, this function does not remove redundant
9155 * constraints and only removes duplicates that have exactly the same
9156 * constraints in the input. It does sort the constraints and
9157 * the basic maps to ease the detection of duplicates.
9159 * If "map" has already been normalized or if the basic maps are
9160 * disjoint, then there can be no duplicates.
9162 __isl_give isl_map
*isl_map_remove_obvious_duplicates(__isl_take isl_map
*map
)
9165 isl_basic_map
*bmap
;
9171 if (ISL_F_ISSET(map
, ISL_MAP_NORMALIZED
| ISL_MAP_DISJOINT
))
9173 for (i
= 0; i
< map
->n
; ++i
) {
9174 bmap
= isl_basic_map_copy(map
->p
[i
]);
9175 bmap
= isl_basic_map_sort_constraints(bmap
);
9177 return isl_map_free(map
);
9178 isl_basic_map_free(map
->p
[i
]);
9182 map
= sort_and_remove_duplicates(map
);
9186 /* We normalize in place, but if anything goes wrong we need
9187 * to return NULL, so we need to make sure we don't change the
9188 * meaning of any possible other copies of map.
9190 __isl_give isl_map
*isl_map_normalize(__isl_take isl_map
*map
)
9193 struct isl_basic_map
*bmap
;
9197 if (ISL_F_ISSET(map
, ISL_MAP_NORMALIZED
))
9199 for (i
= 0; i
< map
->n
; ++i
) {
9200 bmap
= isl_basic_map_normalize(isl_basic_map_copy(map
->p
[i
]));
9203 isl_basic_map_free(map
->p
[i
]);
9207 map
= sort_and_remove_duplicates(map
);
9209 ISL_F_SET(map
, ISL_MAP_NORMALIZED
);
9216 struct isl_set
*isl_set_normalize(struct isl_set
*set
)
9218 return (struct isl_set
*)isl_map_normalize((struct isl_map
*)set
);
9221 int isl_map_plain_is_equal(__isl_keep isl_map
*map1
, __isl_keep isl_map
*map2
)
9231 if (!isl_space_is_equal(map1
->dim
, map2
->dim
))
9234 map1
= isl_map_copy(map1
);
9235 map2
= isl_map_copy(map2
);
9236 map1
= isl_map_normalize(map1
);
9237 map2
= isl_map_normalize(map2
);
9240 equal
= map1
->n
== map2
->n
;
9241 for (i
= 0; equal
&& i
< map1
->n
; ++i
) {
9242 equal
= isl_basic_map_plain_is_equal(map1
->p
[i
], map2
->p
[i
]);
9255 int isl_map_fast_is_equal(__isl_keep isl_map
*map1
, __isl_keep isl_map
*map2
)
9257 return isl_map_plain_is_equal(map1
, map2
);
9260 int isl_set_plain_is_equal(__isl_keep isl_set
*set1
, __isl_keep isl_set
*set2
)
9262 return isl_map_plain_is_equal((struct isl_map
*)set1
,
9263 (struct isl_map
*)set2
);
9266 int isl_set_fast_is_equal(__isl_keep isl_set
*set1
, __isl_keep isl_set
*set2
)
9268 return isl_set_plain_is_equal(set1
, set2
);
9271 /* Return an interval that ranges from min to max (inclusive)
9273 struct isl_basic_set
*isl_basic_set_interval(struct isl_ctx
*ctx
,
9274 isl_int min
, isl_int max
)
9277 struct isl_basic_set
*bset
= NULL
;
9279 bset
= isl_basic_set_alloc(ctx
, 0, 1, 0, 0, 2);
9283 k
= isl_basic_set_alloc_inequality(bset
);
9286 isl_int_set_si(bset
->ineq
[k
][1], 1);
9287 isl_int_neg(bset
->ineq
[k
][0], min
);
9289 k
= isl_basic_set_alloc_inequality(bset
);
9292 isl_int_set_si(bset
->ineq
[k
][1], -1);
9293 isl_int_set(bset
->ineq
[k
][0], max
);
9297 isl_basic_set_free(bset
);
9301 /* Return the basic sets in "set" as a list.
9303 __isl_give isl_basic_set_list
*isl_set_get_basic_set_list(
9304 __isl_keep isl_set
*set
)
9308 isl_basic_set_list
*list
;
9312 ctx
= isl_set_get_ctx(set
);
9313 list
= isl_basic_set_list_alloc(ctx
, set
->n
);
9315 for (i
= 0; i
< set
->n
; ++i
) {
9316 isl_basic_set
*bset
;
9318 bset
= isl_basic_set_copy(set
->p
[i
]);
9319 list
= isl_basic_set_list_add(list
, bset
);
9325 /* Return the intersection of the elements in the non-empty list "list".
9326 * All elements are assumed to live in the same space.
9328 __isl_give isl_basic_set
*isl_basic_set_list_intersect(
9329 __isl_take
struct isl_basic_set_list
*list
)
9332 isl_basic_set
*bset
;
9336 n
= isl_basic_set_list_n_basic_set(list
);
9338 isl_die(isl_basic_set_list_get_ctx(list
), isl_error_invalid
,
9339 "expecting non-empty list", goto error
);
9341 bset
= isl_basic_set_list_get_basic_set(list
, 0);
9342 for (i
= 1; i
< n
; ++i
) {
9343 isl_basic_set
*bset_i
;
9345 bset_i
= isl_basic_set_list_get_basic_set(list
, i
);
9346 bset
= isl_basic_set_intersect(bset
, bset_i
);
9349 isl_basic_set_list_free(list
);
9352 isl_basic_set_list_free(list
);
9356 /* Return the Cartesian product of the basic sets in list (in the given order).
9358 __isl_give isl_basic_set
*isl_basic_set_list_product(
9359 __isl_take
struct isl_basic_set_list
*list
)
9367 struct isl_basic_set
*product
= NULL
;
9371 isl_assert(list
->ctx
, list
->n
> 0, goto error
);
9372 isl_assert(list
->ctx
, list
->p
[0], goto error
);
9373 nparam
= isl_basic_set_n_param(list
->p
[0]);
9374 dim
= isl_basic_set_n_dim(list
->p
[0]);
9375 extra
= list
->p
[0]->n_div
;
9376 n_eq
= list
->p
[0]->n_eq
;
9377 n_ineq
= list
->p
[0]->n_ineq
;
9378 for (i
= 1; i
< list
->n
; ++i
) {
9379 isl_assert(list
->ctx
, list
->p
[i
], goto error
);
9380 isl_assert(list
->ctx
,
9381 nparam
== isl_basic_set_n_param(list
->p
[i
]), goto error
);
9382 dim
+= isl_basic_set_n_dim(list
->p
[i
]);
9383 extra
+= list
->p
[i
]->n_div
;
9384 n_eq
+= list
->p
[i
]->n_eq
;
9385 n_ineq
+= list
->p
[i
]->n_ineq
;
9387 product
= isl_basic_set_alloc(list
->ctx
, nparam
, dim
, extra
,
9392 for (i
= 0; i
< list
->n
; ++i
) {
9393 isl_basic_set_add_constraints(product
,
9394 isl_basic_set_copy(list
->p
[i
]), dim
);
9395 dim
+= isl_basic_set_n_dim(list
->p
[i
]);
9397 isl_basic_set_list_free(list
);
9400 isl_basic_set_free(product
);
9401 isl_basic_set_list_free(list
);
9405 struct isl_basic_map
*isl_basic_map_product(
9406 struct isl_basic_map
*bmap1
, struct isl_basic_map
*bmap2
)
9408 isl_space
*dim_result
= NULL
;
9409 struct isl_basic_map
*bmap
;
9410 unsigned in1
, in2
, out1
, out2
, nparam
, total
, pos
;
9411 struct isl_dim_map
*dim_map1
, *dim_map2
;
9413 if (!bmap1
|| !bmap2
)
9416 isl_assert(bmap1
->ctx
, isl_space_match(bmap1
->dim
, isl_dim_param
,
9417 bmap2
->dim
, isl_dim_param
), goto error
);
9418 dim_result
= isl_space_product(isl_space_copy(bmap1
->dim
),
9419 isl_space_copy(bmap2
->dim
));
9421 in1
= isl_basic_map_n_in(bmap1
);
9422 in2
= isl_basic_map_n_in(bmap2
);
9423 out1
= isl_basic_map_n_out(bmap1
);
9424 out2
= isl_basic_map_n_out(bmap2
);
9425 nparam
= isl_basic_map_n_param(bmap1
);
9427 total
= nparam
+ in1
+ in2
+ out1
+ out2
+ bmap1
->n_div
+ bmap2
->n_div
;
9428 dim_map1
= isl_dim_map_alloc(bmap1
->ctx
, total
);
9429 dim_map2
= isl_dim_map_alloc(bmap1
->ctx
, total
);
9430 isl_dim_map_dim(dim_map1
, bmap1
->dim
, isl_dim_param
, pos
= 0);
9431 isl_dim_map_dim(dim_map2
, bmap2
->dim
, isl_dim_param
, pos
= 0);
9432 isl_dim_map_dim(dim_map1
, bmap1
->dim
, isl_dim_in
, pos
+= nparam
);
9433 isl_dim_map_dim(dim_map2
, bmap2
->dim
, isl_dim_in
, pos
+= in1
);
9434 isl_dim_map_dim(dim_map1
, bmap1
->dim
, isl_dim_out
, pos
+= in2
);
9435 isl_dim_map_dim(dim_map2
, bmap2
->dim
, isl_dim_out
, pos
+= out1
);
9436 isl_dim_map_div(dim_map1
, bmap1
, pos
+= out2
);
9437 isl_dim_map_div(dim_map2
, bmap2
, pos
+= bmap1
->n_div
);
9439 bmap
= isl_basic_map_alloc_space(dim_result
,
9440 bmap1
->n_div
+ bmap2
->n_div
,
9441 bmap1
->n_eq
+ bmap2
->n_eq
,
9442 bmap1
->n_ineq
+ bmap2
->n_ineq
);
9443 bmap
= isl_basic_map_add_constraints_dim_map(bmap
, bmap1
, dim_map1
);
9444 bmap
= isl_basic_map_add_constraints_dim_map(bmap
, bmap2
, dim_map2
);
9445 bmap
= isl_basic_map_simplify(bmap
);
9446 return isl_basic_map_finalize(bmap
);
9448 isl_basic_map_free(bmap1
);
9449 isl_basic_map_free(bmap2
);
9453 __isl_give isl_basic_map
*isl_basic_map_flat_product(
9454 __isl_take isl_basic_map
*bmap1
, __isl_take isl_basic_map
*bmap2
)
9456 isl_basic_map
*prod
;
9458 prod
= isl_basic_map_product(bmap1
, bmap2
);
9459 prod
= isl_basic_map_flatten(prod
);
9463 __isl_give isl_basic_set
*isl_basic_set_flat_product(
9464 __isl_take isl_basic_set
*bset1
, __isl_take isl_basic_set
*bset2
)
9466 return isl_basic_map_flat_range_product(bset1
, bset2
);
9469 __isl_give isl_basic_map
*isl_basic_map_domain_product(
9470 __isl_take isl_basic_map
*bmap1
, __isl_take isl_basic_map
*bmap2
)
9472 isl_space
*space_result
= NULL
;
9473 isl_basic_map
*bmap
;
9474 unsigned in1
, in2
, out
, nparam
, total
, pos
;
9475 struct isl_dim_map
*dim_map1
, *dim_map2
;
9477 if (!bmap1
|| !bmap2
)
9480 space_result
= isl_space_domain_product(isl_space_copy(bmap1
->dim
),
9481 isl_space_copy(bmap2
->dim
));
9483 in1
= isl_basic_map_dim(bmap1
, isl_dim_in
);
9484 in2
= isl_basic_map_dim(bmap2
, isl_dim_in
);
9485 out
= isl_basic_map_dim(bmap1
, isl_dim_out
);
9486 nparam
= isl_basic_map_dim(bmap1
, isl_dim_param
);
9488 total
= nparam
+ in1
+ in2
+ out
+ bmap1
->n_div
+ bmap2
->n_div
;
9489 dim_map1
= isl_dim_map_alloc(bmap1
->ctx
, total
);
9490 dim_map2
= isl_dim_map_alloc(bmap1
->ctx
, total
);
9491 isl_dim_map_dim(dim_map1
, bmap1
->dim
, isl_dim_param
, pos
= 0);
9492 isl_dim_map_dim(dim_map2
, bmap2
->dim
, isl_dim_param
, pos
= 0);
9493 isl_dim_map_dim(dim_map1
, bmap1
->dim
, isl_dim_in
, pos
+= nparam
);
9494 isl_dim_map_dim(dim_map2
, bmap2
->dim
, isl_dim_in
, pos
+= in1
);
9495 isl_dim_map_dim(dim_map1
, bmap1
->dim
, isl_dim_out
, pos
+= in2
);
9496 isl_dim_map_dim(dim_map2
, bmap2
->dim
, isl_dim_out
, pos
);
9497 isl_dim_map_div(dim_map1
, bmap1
, pos
+= out
);
9498 isl_dim_map_div(dim_map2
, bmap2
, pos
+= bmap1
->n_div
);
9500 bmap
= isl_basic_map_alloc_space(space_result
,
9501 bmap1
->n_div
+ bmap2
->n_div
,
9502 bmap1
->n_eq
+ bmap2
->n_eq
,
9503 bmap1
->n_ineq
+ bmap2
->n_ineq
);
9504 bmap
= isl_basic_map_add_constraints_dim_map(bmap
, bmap1
, dim_map1
);
9505 bmap
= isl_basic_map_add_constraints_dim_map(bmap
, bmap2
, dim_map2
);
9506 bmap
= isl_basic_map_simplify(bmap
);
9507 return isl_basic_map_finalize(bmap
);
9509 isl_basic_map_free(bmap1
);
9510 isl_basic_map_free(bmap2
);
9514 __isl_give isl_basic_map
*isl_basic_map_range_product(
9515 __isl_take isl_basic_map
*bmap1
, __isl_take isl_basic_map
*bmap2
)
9517 isl_space
*dim_result
= NULL
;
9518 isl_basic_map
*bmap
;
9519 unsigned in
, out1
, out2
, nparam
, total
, pos
;
9520 struct isl_dim_map
*dim_map1
, *dim_map2
;
9522 if (!bmap1
|| !bmap2
)
9525 if (!isl_space_match(bmap1
->dim
, isl_dim_param
,
9526 bmap2
->dim
, isl_dim_param
))
9527 isl_die(isl_basic_map_get_ctx(bmap1
), isl_error_invalid
,
9528 "parameters don't match", goto error
);
9530 dim_result
= isl_space_range_product(isl_space_copy(bmap1
->dim
),
9531 isl_space_copy(bmap2
->dim
));
9533 in
= isl_basic_map_dim(bmap1
, isl_dim_in
);
9534 out1
= isl_basic_map_n_out(bmap1
);
9535 out2
= isl_basic_map_n_out(bmap2
);
9536 nparam
= isl_basic_map_n_param(bmap1
);
9538 total
= nparam
+ in
+ out1
+ out2
+ bmap1
->n_div
+ bmap2
->n_div
;
9539 dim_map1
= isl_dim_map_alloc(bmap1
->ctx
, total
);
9540 dim_map2
= isl_dim_map_alloc(bmap1
->ctx
, total
);
9541 isl_dim_map_dim(dim_map1
, bmap1
->dim
, isl_dim_param
, pos
= 0);
9542 isl_dim_map_dim(dim_map2
, bmap2
->dim
, isl_dim_param
, pos
= 0);
9543 isl_dim_map_dim(dim_map1
, bmap1
->dim
, isl_dim_in
, pos
+= nparam
);
9544 isl_dim_map_dim(dim_map2
, bmap2
->dim
, isl_dim_in
, pos
);
9545 isl_dim_map_dim(dim_map1
, bmap1
->dim
, isl_dim_out
, pos
+= in
);
9546 isl_dim_map_dim(dim_map2
, bmap2
->dim
, isl_dim_out
, pos
+= out1
);
9547 isl_dim_map_div(dim_map1
, bmap1
, pos
+= out2
);
9548 isl_dim_map_div(dim_map2
, bmap2
, pos
+= bmap1
->n_div
);
9550 bmap
= isl_basic_map_alloc_space(dim_result
,
9551 bmap1
->n_div
+ bmap2
->n_div
,
9552 bmap1
->n_eq
+ bmap2
->n_eq
,
9553 bmap1
->n_ineq
+ bmap2
->n_ineq
);
9554 bmap
= isl_basic_map_add_constraints_dim_map(bmap
, bmap1
, dim_map1
);
9555 bmap
= isl_basic_map_add_constraints_dim_map(bmap
, bmap2
, dim_map2
);
9556 bmap
= isl_basic_map_simplify(bmap
);
9557 return isl_basic_map_finalize(bmap
);
9559 isl_basic_map_free(bmap1
);
9560 isl_basic_map_free(bmap2
);
9564 __isl_give isl_basic_map
*isl_basic_map_flat_range_product(
9565 __isl_take isl_basic_map
*bmap1
, __isl_take isl_basic_map
*bmap2
)
9567 isl_basic_map
*prod
;
9569 prod
= isl_basic_map_range_product(bmap1
, bmap2
);
9570 prod
= isl_basic_map_flatten_range(prod
);
9574 /* Apply "basic_map_product" to each pair of basic maps in "map1" and "map2"
9575 * and collect the results.
9576 * The result live in the space obtained by calling "space_product"
9577 * on the spaces of "map1" and "map2".
9578 * If "remove_duplicates" is set then the result may contain duplicates
9579 * (even if the inputs do not) and so we try and remove the obvious
9582 static __isl_give isl_map
*map_product(__isl_take isl_map
*map1
,
9583 __isl_take isl_map
*map2
,
9584 __isl_give isl_space
*(*space_product
)(__isl_take isl_space
*left
,
9585 __isl_take isl_space
*right
),
9586 __isl_give isl_basic_map
*(*basic_map_product
)(
9587 __isl_take isl_basic_map
*left
,
9588 __isl_take isl_basic_map
*right
),
9589 int remove_duplicates
)
9592 struct isl_map
*result
;
9598 isl_assert(map1
->ctx
, isl_space_match(map1
->dim
, isl_dim_param
,
9599 map2
->dim
, isl_dim_param
), goto error
);
9601 if (ISL_F_ISSET(map1
, ISL_MAP_DISJOINT
) &&
9602 ISL_F_ISSET(map2
, ISL_MAP_DISJOINT
))
9603 ISL_FL_SET(flags
, ISL_MAP_DISJOINT
);
9605 result
= isl_map_alloc_space(space_product(isl_space_copy(map1
->dim
),
9606 isl_space_copy(map2
->dim
)),
9607 map1
->n
* map2
->n
, flags
);
9610 for (i
= 0; i
< map1
->n
; ++i
)
9611 for (j
= 0; j
< map2
->n
; ++j
) {
9612 struct isl_basic_map
*part
;
9613 part
= basic_map_product(isl_basic_map_copy(map1
->p
[i
]),
9614 isl_basic_map_copy(map2
->p
[j
]));
9615 if (isl_basic_map_is_empty(part
))
9616 isl_basic_map_free(part
);
9618 result
= isl_map_add_basic_map(result
, part
);
9622 if (remove_duplicates
)
9623 result
= isl_map_remove_obvious_duplicates(result
);
9633 /* Given two maps A -> B and C -> D, construct a map [A -> C] -> [B -> D]
9635 static __isl_give isl_map
*map_product_aligned(__isl_take isl_map
*map1
,
9636 __isl_take isl_map
*map2
)
9638 return map_product(map1
, map2
, &isl_space_product
,
9639 &isl_basic_map_product
, 0);
9642 __isl_give isl_map
*isl_map_product(__isl_take isl_map
*map1
,
9643 __isl_take isl_map
*map2
)
9645 return isl_map_align_params_map_map_and(map1
, map2
, &map_product_aligned
);
9648 /* Given two maps A -> B and C -> D, construct a map (A, C) -> (B, D)
9650 __isl_give isl_map
*isl_map_flat_product(__isl_take isl_map
*map1
,
9651 __isl_take isl_map
*map2
)
9655 prod
= isl_map_product(map1
, map2
);
9656 prod
= isl_map_flatten(prod
);
9660 /* Given two set A and B, construct its Cartesian product A x B.
9662 struct isl_set
*isl_set_product(struct isl_set
*set1
, struct isl_set
*set2
)
9664 return isl_map_range_product(set1
, set2
);
9667 __isl_give isl_set
*isl_set_flat_product(__isl_take isl_set
*set1
,
9668 __isl_take isl_set
*set2
)
9670 return isl_map_flat_range_product(set1
, set2
);
9673 /* Given two maps A -> B and C -> D, construct a map [A -> C] -> (B * D)
9675 static __isl_give isl_map
*map_domain_product_aligned(__isl_take isl_map
*map1
,
9676 __isl_take isl_map
*map2
)
9678 return map_product(map1
, map2
, &isl_space_domain_product
,
9679 &isl_basic_map_domain_product
, 1);
9682 /* Given two maps A -> B and C -> D, construct a map (A * C) -> [B -> D]
9684 static __isl_give isl_map
*map_range_product_aligned(__isl_take isl_map
*map1
,
9685 __isl_take isl_map
*map2
)
9687 return map_product(map1
, map2
, &isl_space_range_product
,
9688 &isl_basic_map_range_product
, 1);
9691 __isl_give isl_map
*isl_map_domain_product(__isl_take isl_map
*map1
,
9692 __isl_take isl_map
*map2
)
9694 return isl_map_align_params_map_map_and(map1
, map2
,
9695 &map_domain_product_aligned
);
9698 __isl_give isl_map
*isl_map_range_product(__isl_take isl_map
*map1
,
9699 __isl_take isl_map
*map2
)
9701 return isl_map_align_params_map_map_and(map1
, map2
,
9702 &map_range_product_aligned
);
9705 /* Given a map A -> [B -> C], extract the map A -> B.
9707 __isl_give isl_map
*isl_map_range_factor_domain(__isl_take isl_map
*map
)
9714 if (!isl_space_range_is_wrapping(map
->dim
))
9715 isl_die(isl_map_get_ctx(map
), isl_error_invalid
,
9716 "range is not a product", return isl_map_free(map
));
9718 space
= isl_map_get_space(map
);
9719 total
= isl_space_dim(space
, isl_dim_out
);
9720 space
= isl_space_range_factor_domain(space
);
9721 keep
= isl_space_dim(space
, isl_dim_out
);
9722 map
= isl_map_project_out(map
, isl_dim_out
, keep
, total
- keep
);
9723 map
= isl_map_reset_space(map
, space
);
9728 /* Given a map A -> [B -> C], extract the map A -> C.
9730 __isl_give isl_map
*isl_map_range_factor_range(__isl_take isl_map
*map
)
9737 if (!isl_space_range_is_wrapping(map
->dim
))
9738 isl_die(isl_map_get_ctx(map
), isl_error_invalid
,
9739 "range is not a product", return isl_map_free(map
));
9741 space
= isl_map_get_space(map
);
9742 total
= isl_space_dim(space
, isl_dim_out
);
9743 space
= isl_space_range_factor_range(space
);
9744 keep
= isl_space_dim(space
, isl_dim_out
);
9745 map
= isl_map_project_out(map
, isl_dim_out
, 0, total
- keep
);
9746 map
= isl_map_reset_space(map
, space
);
9751 /* Given two maps A -> B and C -> D, construct a map (A, C) -> (B * D)
9753 __isl_give isl_map
*isl_map_flat_domain_product(__isl_take isl_map
*map1
,
9754 __isl_take isl_map
*map2
)
9758 prod
= isl_map_domain_product(map1
, map2
);
9759 prod
= isl_map_flatten_domain(prod
);
9763 /* Given two maps A -> B and C -> D, construct a map (A * C) -> (B, D)
9765 __isl_give isl_map
*isl_map_flat_range_product(__isl_take isl_map
*map1
,
9766 __isl_take isl_map
*map2
)
9770 prod
= isl_map_range_product(map1
, map2
);
9771 prod
= isl_map_flatten_range(prod
);
9775 uint32_t isl_basic_map_get_hash(__isl_keep isl_basic_map
*bmap
)
9778 uint32_t hash
= isl_hash_init();
9783 bmap
= isl_basic_map_copy(bmap
);
9784 bmap
= isl_basic_map_normalize(bmap
);
9787 total
= isl_basic_map_total_dim(bmap
);
9788 isl_hash_byte(hash
, bmap
->n_eq
& 0xFF);
9789 for (i
= 0; i
< bmap
->n_eq
; ++i
) {
9791 c_hash
= isl_seq_get_hash(bmap
->eq
[i
], 1 + total
);
9792 isl_hash_hash(hash
, c_hash
);
9794 isl_hash_byte(hash
, bmap
->n_ineq
& 0xFF);
9795 for (i
= 0; i
< bmap
->n_ineq
; ++i
) {
9797 c_hash
= isl_seq_get_hash(bmap
->ineq
[i
], 1 + total
);
9798 isl_hash_hash(hash
, c_hash
);
9800 isl_hash_byte(hash
, bmap
->n_div
& 0xFF);
9801 for (i
= 0; i
< bmap
->n_div
; ++i
) {
9803 if (isl_int_is_zero(bmap
->div
[i
][0]))
9805 isl_hash_byte(hash
, i
& 0xFF);
9806 c_hash
= isl_seq_get_hash(bmap
->div
[i
], 1 + 1 + total
);
9807 isl_hash_hash(hash
, c_hash
);
9809 isl_basic_map_free(bmap
);
9813 uint32_t isl_basic_set_get_hash(__isl_keep isl_basic_set
*bset
)
9815 return isl_basic_map_get_hash((isl_basic_map
*)bset
);
9818 uint32_t isl_map_get_hash(__isl_keep isl_map
*map
)
9825 map
= isl_map_copy(map
);
9826 map
= isl_map_normalize(map
);
9830 hash
= isl_hash_init();
9831 for (i
= 0; i
< map
->n
; ++i
) {
9833 bmap_hash
= isl_basic_map_get_hash(map
->p
[i
]);
9834 isl_hash_hash(hash
, bmap_hash
);
9842 uint32_t isl_set_get_hash(__isl_keep isl_set
*set
)
9844 return isl_map_get_hash((isl_map
*)set
);
9847 /* Check if the value for dimension dim is completely determined
9848 * by the values of the other parameters and variables.
9849 * That is, check if dimension dim is involved in an equality.
9851 int isl_basic_set_dim_is_unique(struct isl_basic_set
*bset
, unsigned dim
)
9858 nparam
= isl_basic_set_n_param(bset
);
9859 for (i
= 0; i
< bset
->n_eq
; ++i
)
9860 if (!isl_int_is_zero(bset
->eq
[i
][1 + nparam
+ dim
]))
9865 /* Check if the value for dimension dim is completely determined
9866 * by the values of the other parameters and variables.
9867 * That is, check if dimension dim is involved in an equality
9868 * for each of the subsets.
9870 int isl_set_dim_is_unique(struct isl_set
*set
, unsigned dim
)
9876 for (i
= 0; i
< set
->n
; ++i
) {
9878 unique
= isl_basic_set_dim_is_unique(set
->p
[i
], dim
);
9885 /* Return the number of basic maps in the (current) representation of "map".
9887 int isl_map_n_basic_map(__isl_keep isl_map
*map
)
9889 return map
? map
->n
: 0;
9892 int isl_set_n_basic_set(__isl_keep isl_set
*set
)
9894 return set
? set
->n
: 0;
9897 int isl_map_foreach_basic_map(__isl_keep isl_map
*map
,
9898 int (*fn
)(__isl_take isl_basic_map
*bmap
, void *user
), void *user
)
9905 for (i
= 0; i
< map
->n
; ++i
)
9906 if (fn(isl_basic_map_copy(map
->p
[i
]), user
) < 0)
9912 int isl_set_foreach_basic_set(__isl_keep isl_set
*set
,
9913 int (*fn
)(__isl_take isl_basic_set
*bset
, void *user
), void *user
)
9920 for (i
= 0; i
< set
->n
; ++i
)
9921 if (fn(isl_basic_set_copy(set
->p
[i
]), user
) < 0)
9927 __isl_give isl_basic_set
*isl_basic_set_lift(__isl_take isl_basic_set
*bset
)
9934 bset
= isl_basic_set_cow(bset
);
9938 dim
= isl_basic_set_get_space(bset
);
9939 dim
= isl_space_lift(dim
, bset
->n_div
);
9942 isl_space_free(bset
->dim
);
9944 bset
->extra
-= bset
->n_div
;
9947 bset
= isl_basic_set_finalize(bset
);
9951 isl_basic_set_free(bset
);
9955 __isl_give isl_set
*isl_set_lift(__isl_take isl_set
*set
)
9961 set
= isl_set_align_divs(set
);
9966 set
= isl_set_cow(set
);
9970 n_div
= set
->p
[0]->n_div
;
9971 dim
= isl_set_get_space(set
);
9972 dim
= isl_space_lift(dim
, n_div
);
9975 isl_space_free(set
->dim
);
9978 for (i
= 0; i
< set
->n
; ++i
) {
9979 set
->p
[i
] = isl_basic_set_lift(set
->p
[i
]);
9990 __isl_give isl_map
*isl_set_lifting(__isl_take isl_set
*set
)
9993 struct isl_basic_map
*bmap
;
10000 set
= isl_set_align_divs(set
);
10005 dim
= isl_set_get_space(set
);
10006 if (set
->n
== 0 || set
->p
[0]->n_div
== 0) {
10008 return isl_map_identity(isl_space_map_from_set(dim
));
10011 n_div
= set
->p
[0]->n_div
;
10012 dim
= isl_space_map_from_set(dim
);
10013 n_param
= isl_space_dim(dim
, isl_dim_param
);
10014 n_set
= isl_space_dim(dim
, isl_dim_in
);
10015 dim
= isl_space_extend(dim
, n_param
, n_set
, n_set
+ n_div
);
10016 bmap
= isl_basic_map_alloc_space(dim
, 0, n_set
, 2 * n_div
);
10017 for (i
= 0; i
< n_set
; ++i
)
10018 bmap
= var_equal(bmap
, i
);
10020 total
= n_param
+ n_set
+ n_set
+ n_div
;
10021 for (i
= 0; i
< n_div
; ++i
) {
10022 k
= isl_basic_map_alloc_inequality(bmap
);
10025 isl_seq_cpy(bmap
->ineq
[k
], set
->p
[0]->div
[i
]+1, 1+n_param
);
10026 isl_seq_clr(bmap
->ineq
[k
]+1+n_param
, n_set
);
10027 isl_seq_cpy(bmap
->ineq
[k
]+1+n_param
+n_set
,
10028 set
->p
[0]->div
[i
]+1+1+n_param
, n_set
+ n_div
);
10029 isl_int_neg(bmap
->ineq
[k
][1+n_param
+n_set
+n_set
+i
],
10030 set
->p
[0]->div
[i
][0]);
10032 l
= isl_basic_map_alloc_inequality(bmap
);
10035 isl_seq_neg(bmap
->ineq
[l
], bmap
->ineq
[k
], 1 + total
);
10036 isl_int_add(bmap
->ineq
[l
][0], bmap
->ineq
[l
][0],
10037 set
->p
[0]->div
[i
][0]);
10038 isl_int_sub_ui(bmap
->ineq
[l
][0], bmap
->ineq
[l
][0], 1);
10042 bmap
= isl_basic_map_simplify(bmap
);
10043 bmap
= isl_basic_map_finalize(bmap
);
10044 return isl_map_from_basic_map(bmap
);
10047 isl_basic_map_free(bmap
);
10051 int isl_basic_set_size(__isl_keep isl_basic_set
*bset
)
10059 dim
= isl_basic_set_total_dim(bset
);
10060 size
+= bset
->n_eq
* (1 + dim
);
10061 size
+= bset
->n_ineq
* (1 + dim
);
10062 size
+= bset
->n_div
* (2 + dim
);
10067 int isl_set_size(__isl_keep isl_set
*set
)
10075 for (i
= 0; i
< set
->n
; ++i
)
10076 size
+= isl_basic_set_size(set
->p
[i
]);
10081 /* Check if there is any lower bound (if lower == 0) and/or upper
10082 * bound (if upper == 0) on the specified dim.
10084 static int basic_map_dim_is_bounded(__isl_keep isl_basic_map
*bmap
,
10085 enum isl_dim_type type
, unsigned pos
, int lower
, int upper
)
10092 isl_assert(bmap
->ctx
, pos
< isl_basic_map_dim(bmap
, type
), return -1);
10094 pos
+= isl_basic_map_offset(bmap
, type
);
10096 for (i
= 0; i
< bmap
->n_div
; ++i
) {
10097 if (isl_int_is_zero(bmap
->div
[i
][0]))
10099 if (!isl_int_is_zero(bmap
->div
[i
][1 + pos
]))
10103 for (i
= 0; i
< bmap
->n_eq
; ++i
)
10104 if (!isl_int_is_zero(bmap
->eq
[i
][pos
]))
10107 for (i
= 0; i
< bmap
->n_ineq
; ++i
) {
10108 int sgn
= isl_int_sgn(bmap
->ineq
[i
][pos
]);
10115 return lower
&& upper
;
10118 int isl_basic_map_dim_is_bounded(__isl_keep isl_basic_map
*bmap
,
10119 enum isl_dim_type type
, unsigned pos
)
10121 return basic_map_dim_is_bounded(bmap
, type
, pos
, 0, 0);
10124 int isl_basic_map_dim_has_lower_bound(__isl_keep isl_basic_map
*bmap
,
10125 enum isl_dim_type type
, unsigned pos
)
10127 return basic_map_dim_is_bounded(bmap
, type
, pos
, 0, 1);
10130 int isl_basic_map_dim_has_upper_bound(__isl_keep isl_basic_map
*bmap
,
10131 enum isl_dim_type type
, unsigned pos
)
10133 return basic_map_dim_is_bounded(bmap
, type
, pos
, 1, 0);
10136 int isl_map_dim_is_bounded(__isl_keep isl_map
*map
,
10137 enum isl_dim_type type
, unsigned pos
)
10144 for (i
= 0; i
< map
->n
; ++i
) {
10146 bounded
= isl_basic_map_dim_is_bounded(map
->p
[i
], type
, pos
);
10147 if (bounded
< 0 || !bounded
)
10154 /* Return 1 if the specified dim is involved in both an upper bound
10155 * and a lower bound.
10157 int isl_set_dim_is_bounded(__isl_keep isl_set
*set
,
10158 enum isl_dim_type type
, unsigned pos
)
10160 return isl_map_dim_is_bounded((isl_map
*)set
, type
, pos
);
10163 /* Does "map" have a bound (according to "fn") for any of its basic maps?
10165 static int has_any_bound(__isl_keep isl_map
*map
,
10166 enum isl_dim_type type
, unsigned pos
,
10167 int (*fn
)(__isl_keep isl_basic_map
*bmap
,
10168 enum isl_dim_type type
, unsigned pos
))
10175 for (i
= 0; i
< map
->n
; ++i
) {
10177 bounded
= fn(map
->p
[i
], type
, pos
);
10178 if (bounded
< 0 || bounded
)
10185 /* Return 1 if the specified dim is involved in any lower bound.
10187 int isl_set_dim_has_any_lower_bound(__isl_keep isl_set
*set
,
10188 enum isl_dim_type type
, unsigned pos
)
10190 return has_any_bound(set
, type
, pos
,
10191 &isl_basic_map_dim_has_lower_bound
);
10194 /* Return 1 if the specified dim is involved in any upper bound.
10196 int isl_set_dim_has_any_upper_bound(__isl_keep isl_set
*set
,
10197 enum isl_dim_type type
, unsigned pos
)
10199 return has_any_bound(set
, type
, pos
,
10200 &isl_basic_map_dim_has_upper_bound
);
10203 /* Does "map" have a bound (according to "fn") for all of its basic maps?
10205 static int has_bound(__isl_keep isl_map
*map
,
10206 enum isl_dim_type type
, unsigned pos
,
10207 int (*fn
)(__isl_keep isl_basic_map
*bmap
,
10208 enum isl_dim_type type
, unsigned pos
))
10215 for (i
= 0; i
< map
->n
; ++i
) {
10217 bounded
= fn(map
->p
[i
], type
, pos
);
10218 if (bounded
< 0 || !bounded
)
10225 /* Return 1 if the specified dim has a lower bound (in each of its basic sets).
10227 int isl_set_dim_has_lower_bound(__isl_keep isl_set
*set
,
10228 enum isl_dim_type type
, unsigned pos
)
10230 return has_bound(set
, type
, pos
, &isl_basic_map_dim_has_lower_bound
);
10233 /* Return 1 if the specified dim has an upper bound (in each of its basic sets).
10235 int isl_set_dim_has_upper_bound(__isl_keep isl_set
*set
,
10236 enum isl_dim_type type
, unsigned pos
)
10238 return has_bound(set
, type
, pos
, &isl_basic_map_dim_has_upper_bound
);
10241 /* For each of the "n" variables starting at "first", determine
10242 * the sign of the variable and put the results in the first "n"
10243 * elements of the array "signs".
10245 * 1 means that the variable is non-negative
10246 * -1 means that the variable is non-positive
10247 * 0 means the variable attains both positive and negative values.
10249 int isl_basic_set_vars_get_sign(__isl_keep isl_basic_set
*bset
,
10250 unsigned first
, unsigned n
, int *signs
)
10252 isl_vec
*bound
= NULL
;
10253 struct isl_tab
*tab
= NULL
;
10254 struct isl_tab_undo
*snap
;
10257 if (!bset
|| !signs
)
10260 bound
= isl_vec_alloc(bset
->ctx
, 1 + isl_basic_set_total_dim(bset
));
10261 tab
= isl_tab_from_basic_set(bset
, 0);
10262 if (!bound
|| !tab
)
10265 isl_seq_clr(bound
->el
, bound
->size
);
10266 isl_int_set_si(bound
->el
[0], -1);
10268 snap
= isl_tab_snap(tab
);
10269 for (i
= 0; i
< n
; ++i
) {
10272 isl_int_set_si(bound
->el
[1 + first
+ i
], -1);
10273 if (isl_tab_add_ineq(tab
, bound
->el
) < 0)
10275 empty
= tab
->empty
;
10276 isl_int_set_si(bound
->el
[1 + first
+ i
], 0);
10277 if (isl_tab_rollback(tab
, snap
) < 0)
10285 isl_int_set_si(bound
->el
[1 + first
+ i
], 1);
10286 if (isl_tab_add_ineq(tab
, bound
->el
) < 0)
10288 empty
= tab
->empty
;
10289 isl_int_set_si(bound
->el
[1 + first
+ i
], 0);
10290 if (isl_tab_rollback(tab
, snap
) < 0)
10293 signs
[i
] = empty
? -1 : 0;
10297 isl_vec_free(bound
);
10301 isl_vec_free(bound
);
10305 int isl_basic_set_dims_get_sign(__isl_keep isl_basic_set
*bset
,
10306 enum isl_dim_type type
, unsigned first
, unsigned n
, int *signs
)
10308 if (!bset
|| !signs
)
10310 isl_assert(bset
->ctx
, first
+ n
<= isl_basic_set_dim(bset
, type
),
10313 first
+= pos(bset
->dim
, type
) - 1;
10314 return isl_basic_set_vars_get_sign(bset
, first
, n
, signs
);
10317 /* Is it possible for the integer division "div" to depend (possibly
10318 * indirectly) on any output dimensions?
10320 * If the div is undefined, then we conservatively assume that it
10321 * may depend on them.
10322 * Otherwise, we check if it actually depends on them or on any integer
10323 * divisions that may depend on them.
10325 static int div_may_involve_output(__isl_keep isl_basic_map
*bmap
, int div
)
10328 unsigned n_out
, o_out
;
10329 unsigned n_div
, o_div
;
10331 if (isl_int_is_zero(bmap
->div
[div
][0]))
10334 n_out
= isl_basic_map_dim(bmap
, isl_dim_out
);
10335 o_out
= isl_basic_map_offset(bmap
, isl_dim_out
);
10337 if (isl_seq_first_non_zero(bmap
->div
[div
] + 1 + o_out
, n_out
) != -1)
10340 n_div
= isl_basic_map_dim(bmap
, isl_dim_div
);
10341 o_div
= isl_basic_map_offset(bmap
, isl_dim_div
);
10343 for (i
= 0; i
< n_div
; ++i
) {
10344 if (isl_int_is_zero(bmap
->div
[div
][1 + o_div
+ i
]))
10346 if (div_may_involve_output(bmap
, i
))
10353 /* Return the index of the equality of "bmap" that defines
10354 * the output dimension "pos" in terms of earlier dimensions.
10355 * The equality may also involve integer divisions, as long
10356 * as those integer divisions are defined in terms of
10357 * parameters or input dimensions.
10358 * Return bmap->n_eq if there is no such equality.
10359 * Return -1 on error.
10361 int isl_basic_map_output_defining_equality(__isl_keep isl_basic_map
*bmap
,
10365 unsigned n_out
, o_out
;
10366 unsigned n_div
, o_div
;
10371 n_out
= isl_basic_map_dim(bmap
, isl_dim_out
);
10372 o_out
= isl_basic_map_offset(bmap
, isl_dim_out
);
10373 n_div
= isl_basic_map_dim(bmap
, isl_dim_div
);
10374 o_div
= isl_basic_map_offset(bmap
, isl_dim_div
);
10376 for (j
= 0; j
< bmap
->n_eq
; ++j
) {
10377 if (isl_int_is_zero(bmap
->eq
[j
][o_out
+ pos
]))
10379 if (isl_seq_first_non_zero(bmap
->eq
[j
] + o_out
+ pos
+ 1,
10380 n_out
- (pos
+ 1)) != -1)
10382 for (k
= 0; k
< n_div
; ++k
) {
10383 if (isl_int_is_zero(bmap
->eq
[j
][o_div
+ k
]))
10385 if (div_may_involve_output(bmap
, k
))
10395 /* Check if the given basic map is obviously single-valued.
10396 * In particular, for each output dimension, check that there is
10397 * an equality that defines the output dimension in terms of
10398 * earlier dimensions.
10400 int isl_basic_map_plain_is_single_valued(__isl_keep isl_basic_map
*bmap
)
10408 n_out
= isl_basic_map_dim(bmap
, isl_dim_out
);
10410 for (i
= 0; i
< n_out
; ++i
) {
10413 eq
= isl_basic_map_output_defining_equality(bmap
, i
);
10416 if (eq
>= bmap
->n_eq
)
10423 /* Check if the given basic map is single-valued.
10424 * We simply compute
10428 * and check if the result is a subset of the identity mapping.
10430 int isl_basic_map_is_single_valued(__isl_keep isl_basic_map
*bmap
)
10433 isl_basic_map
*test
;
10437 sv
= isl_basic_map_plain_is_single_valued(bmap
);
10441 test
= isl_basic_map_reverse(isl_basic_map_copy(bmap
));
10442 test
= isl_basic_map_apply_range(test
, isl_basic_map_copy(bmap
));
10444 space
= isl_basic_map_get_space(bmap
);
10445 space
= isl_space_map_from_set(isl_space_range(space
));
10446 id
= isl_basic_map_identity(space
);
10448 sv
= isl_basic_map_is_subset(test
, id
);
10450 isl_basic_map_free(test
);
10451 isl_basic_map_free(id
);
10456 /* Check if the given map is obviously single-valued.
10458 int isl_map_plain_is_single_valued(__isl_keep isl_map
*map
)
10467 return isl_basic_map_plain_is_single_valued(map
->p
[0]);
10470 /* Check if the given map is single-valued.
10471 * We simply compute
10475 * and check if the result is a subset of the identity mapping.
10477 int isl_map_is_single_valued(__isl_keep isl_map
*map
)
10484 sv
= isl_map_plain_is_single_valued(map
);
10488 test
= isl_map_reverse(isl_map_copy(map
));
10489 test
= isl_map_apply_range(test
, isl_map_copy(map
));
10491 dim
= isl_space_map_from_set(isl_space_range(isl_map_get_space(map
)));
10492 id
= isl_map_identity(dim
);
10494 sv
= isl_map_is_subset(test
, id
);
10496 isl_map_free(test
);
10502 int isl_map_is_injective(__isl_keep isl_map
*map
)
10506 map
= isl_map_copy(map
);
10507 map
= isl_map_reverse(map
);
10508 in
= isl_map_is_single_valued(map
);
10514 /* Check if the given map is obviously injective.
10516 int isl_map_plain_is_injective(__isl_keep isl_map
*map
)
10520 map
= isl_map_copy(map
);
10521 map
= isl_map_reverse(map
);
10522 in
= isl_map_plain_is_single_valued(map
);
10528 int isl_map_is_bijective(__isl_keep isl_map
*map
)
10532 sv
= isl_map_is_single_valued(map
);
10536 return isl_map_is_injective(map
);
10539 int isl_set_is_singleton(__isl_keep isl_set
*set
)
10541 return isl_map_is_single_valued((isl_map
*)set
);
10544 int isl_map_is_translation(__isl_keep isl_map
*map
)
10549 delta
= isl_map_deltas(isl_map_copy(map
));
10550 ok
= isl_set_is_singleton(delta
);
10551 isl_set_free(delta
);
10556 static int unique(isl_int
*p
, unsigned pos
, unsigned len
)
10558 if (isl_seq_first_non_zero(p
, pos
) != -1)
10560 if (isl_seq_first_non_zero(p
+ pos
+ 1, len
- pos
- 1) != -1)
10565 int isl_basic_set_is_box(__isl_keep isl_basic_set
*bset
)
10574 if (isl_basic_set_dim(bset
, isl_dim_div
) != 0)
10577 nvar
= isl_basic_set_dim(bset
, isl_dim_set
);
10578 ovar
= isl_space_offset(bset
->dim
, isl_dim_set
);
10579 for (j
= 0; j
< nvar
; ++j
) {
10580 int lower
= 0, upper
= 0;
10581 for (i
= 0; i
< bset
->n_eq
; ++i
) {
10582 if (isl_int_is_zero(bset
->eq
[i
][1 + ovar
+ j
]))
10584 if (!unique(bset
->eq
[i
] + 1 + ovar
, j
, nvar
))
10588 if (i
< bset
->n_eq
)
10590 for (i
= 0; i
< bset
->n_ineq
; ++i
) {
10591 if (isl_int_is_zero(bset
->ineq
[i
][1 + ovar
+ j
]))
10593 if (!unique(bset
->ineq
[i
] + 1 + ovar
, j
, nvar
))
10595 if (isl_int_is_pos(bset
->ineq
[i
][1 + ovar
+ j
]))
10600 if (!lower
|| !upper
)
10607 int isl_set_is_box(__isl_keep isl_set
*set
)
10614 return isl_basic_set_is_box(set
->p
[0]);
10617 int isl_basic_set_is_wrapping(__isl_keep isl_basic_set
*bset
)
10622 return isl_space_is_wrapping(bset
->dim
);
10625 int isl_set_is_wrapping(__isl_keep isl_set
*set
)
10630 return isl_space_is_wrapping(set
->dim
);
10633 /* Is the domain of "map" a wrapped relation?
10635 int isl_map_domain_is_wrapping(__isl_keep isl_map
*map
)
10640 return isl_space_domain_is_wrapping(map
->dim
);
10643 /* Is the range of "map" a wrapped relation?
10645 int isl_map_range_is_wrapping(__isl_keep isl_map
*map
)
10650 return isl_space_range_is_wrapping(map
->dim
);
10653 __isl_give isl_basic_set
*isl_basic_map_wrap(__isl_take isl_basic_map
*bmap
)
10655 bmap
= isl_basic_map_cow(bmap
);
10659 bmap
->dim
= isl_space_wrap(bmap
->dim
);
10663 bmap
= isl_basic_map_finalize(bmap
);
10665 return (isl_basic_set
*)bmap
;
10667 isl_basic_map_free(bmap
);
10671 __isl_give isl_set
*isl_map_wrap(__isl_take isl_map
*map
)
10675 map
= isl_map_cow(map
);
10679 for (i
= 0; i
< map
->n
; ++i
) {
10680 map
->p
[i
] = (isl_basic_map
*)isl_basic_map_wrap(map
->p
[i
]);
10684 map
->dim
= isl_space_wrap(map
->dim
);
10688 return (isl_set
*)map
;
10694 __isl_give isl_basic_map
*isl_basic_set_unwrap(__isl_take isl_basic_set
*bset
)
10696 bset
= isl_basic_set_cow(bset
);
10700 bset
->dim
= isl_space_unwrap(bset
->dim
);
10704 bset
= isl_basic_set_finalize(bset
);
10706 return (isl_basic_map
*)bset
;
10708 isl_basic_set_free(bset
);
10712 __isl_give isl_map
*isl_set_unwrap(__isl_take isl_set
*set
)
10719 if (!isl_set_is_wrapping(set
))
10720 isl_die(set
->ctx
, isl_error_invalid
, "not a wrapping set",
10723 set
= isl_set_cow(set
);
10727 for (i
= 0; i
< set
->n
; ++i
) {
10728 set
->p
[i
] = (isl_basic_set
*)isl_basic_set_unwrap(set
->p
[i
]);
10733 set
->dim
= isl_space_unwrap(set
->dim
);
10737 return (isl_map
*)set
;
10743 __isl_give isl_basic_map
*isl_basic_map_reset(__isl_take isl_basic_map
*bmap
,
10744 enum isl_dim_type type
)
10749 if (!isl_space_is_named_or_nested(bmap
->dim
, type
))
10752 bmap
= isl_basic_map_cow(bmap
);
10756 bmap
->dim
= isl_space_reset(bmap
->dim
, type
);
10760 bmap
= isl_basic_map_finalize(bmap
);
10764 isl_basic_map_free(bmap
);
10768 __isl_give isl_map
*isl_map_reset(__isl_take isl_map
*map
,
10769 enum isl_dim_type type
)
10776 if (!isl_space_is_named_or_nested(map
->dim
, type
))
10779 map
= isl_map_cow(map
);
10783 for (i
= 0; i
< map
->n
; ++i
) {
10784 map
->p
[i
] = isl_basic_map_reset(map
->p
[i
], type
);
10788 map
->dim
= isl_space_reset(map
->dim
, type
);
10798 __isl_give isl_basic_map
*isl_basic_map_flatten(__isl_take isl_basic_map
*bmap
)
10803 if (!bmap
->dim
->nested
[0] && !bmap
->dim
->nested
[1])
10806 bmap
= isl_basic_map_cow(bmap
);
10810 bmap
->dim
= isl_space_flatten(bmap
->dim
);
10814 bmap
= isl_basic_map_finalize(bmap
);
10818 isl_basic_map_free(bmap
);
10822 __isl_give isl_basic_set
*isl_basic_set_flatten(__isl_take isl_basic_set
*bset
)
10824 return (isl_basic_set
*)isl_basic_map_flatten((isl_basic_map
*)bset
);
10827 __isl_give isl_basic_map
*isl_basic_map_flatten_domain(
10828 __isl_take isl_basic_map
*bmap
)
10833 if (!bmap
->dim
->nested
[0])
10836 bmap
= isl_basic_map_cow(bmap
);
10840 bmap
->dim
= isl_space_flatten_domain(bmap
->dim
);
10844 bmap
= isl_basic_map_finalize(bmap
);
10848 isl_basic_map_free(bmap
);
10852 __isl_give isl_basic_map
*isl_basic_map_flatten_range(
10853 __isl_take isl_basic_map
*bmap
)
10858 if (!bmap
->dim
->nested
[1])
10861 bmap
= isl_basic_map_cow(bmap
);
10865 bmap
->dim
= isl_space_flatten_range(bmap
->dim
);
10869 bmap
= isl_basic_map_finalize(bmap
);
10873 isl_basic_map_free(bmap
);
10877 __isl_give isl_map
*isl_map_flatten(__isl_take isl_map
*map
)
10884 if (!map
->dim
->nested
[0] && !map
->dim
->nested
[1])
10887 map
= isl_map_cow(map
);
10891 for (i
= 0; i
< map
->n
; ++i
) {
10892 map
->p
[i
] = isl_basic_map_flatten(map
->p
[i
]);
10896 map
->dim
= isl_space_flatten(map
->dim
);
10906 __isl_give isl_set
*isl_set_flatten(__isl_take isl_set
*set
)
10908 return (isl_set
*)isl_map_flatten((isl_map
*)set
);
10911 __isl_give isl_map
*isl_set_flatten_map(__isl_take isl_set
*set
)
10913 isl_space
*dim
, *flat_dim
;
10916 dim
= isl_set_get_space(set
);
10917 flat_dim
= isl_space_flatten(isl_space_copy(dim
));
10918 map
= isl_map_identity(isl_space_join(isl_space_reverse(dim
), flat_dim
));
10919 map
= isl_map_intersect_domain(map
, set
);
10924 __isl_give isl_map
*isl_map_flatten_domain(__isl_take isl_map
*map
)
10931 if (!map
->dim
->nested
[0])
10934 map
= isl_map_cow(map
);
10938 for (i
= 0; i
< map
->n
; ++i
) {
10939 map
->p
[i
] = isl_basic_map_flatten_domain(map
->p
[i
]);
10943 map
->dim
= isl_space_flatten_domain(map
->dim
);
10953 __isl_give isl_map
*isl_map_flatten_range(__isl_take isl_map
*map
)
10960 if (!map
->dim
->nested
[1])
10963 map
= isl_map_cow(map
);
10967 for (i
= 0; i
< map
->n
; ++i
) {
10968 map
->p
[i
] = isl_basic_map_flatten_range(map
->p
[i
]);
10972 map
->dim
= isl_space_flatten_range(map
->dim
);
10982 /* Reorder the dimensions of "bmap" according to the given dim_map
10983 * and set the dimension specification to "dim".
10985 __isl_give isl_basic_map
*isl_basic_map_realign(__isl_take isl_basic_map
*bmap
,
10986 __isl_take isl_space
*dim
, __isl_take
struct isl_dim_map
*dim_map
)
10988 isl_basic_map
*res
;
10991 bmap
= isl_basic_map_cow(bmap
);
10992 if (!bmap
|| !dim
|| !dim_map
)
10995 flags
= bmap
->flags
;
10996 ISL_FL_CLR(flags
, ISL_BASIC_MAP_FINAL
);
10997 ISL_FL_CLR(flags
, ISL_BASIC_MAP_NORMALIZED
);
10998 ISL_FL_CLR(flags
, ISL_BASIC_MAP_NORMALIZED_DIVS
);
10999 res
= isl_basic_map_alloc_space(dim
,
11000 bmap
->n_div
, bmap
->n_eq
, bmap
->n_ineq
);
11001 res
= isl_basic_map_add_constraints_dim_map(res
, bmap
, dim_map
);
11003 res
->flags
= flags
;
11004 res
= isl_basic_map_finalize(res
);
11008 isl_basic_map_free(bmap
);
11009 isl_space_free(dim
);
11013 /* Reorder the dimensions of "map" according to given reordering.
11015 __isl_give isl_map
*isl_map_realign(__isl_take isl_map
*map
,
11016 __isl_take isl_reordering
*r
)
11019 struct isl_dim_map
*dim_map
;
11021 map
= isl_map_cow(map
);
11022 dim_map
= isl_dim_map_from_reordering(r
);
11023 if (!map
|| !r
|| !dim_map
)
11026 for (i
= 0; i
< map
->n
; ++i
) {
11027 struct isl_dim_map
*dim_map_i
;
11029 dim_map_i
= isl_dim_map_extend(dim_map
, map
->p
[i
]);
11031 map
->p
[i
] = isl_basic_map_realign(map
->p
[i
],
11032 isl_space_copy(r
->dim
), dim_map_i
);
11038 map
= isl_map_reset_space(map
, isl_space_copy(r
->dim
));
11040 isl_reordering_free(r
);
11046 isl_reordering_free(r
);
11050 __isl_give isl_set
*isl_set_realign(__isl_take isl_set
*set
,
11051 __isl_take isl_reordering
*r
)
11053 return (isl_set
*)isl_map_realign((isl_map
*)set
, r
);
11056 __isl_give isl_map
*isl_map_align_params(__isl_take isl_map
*map
,
11057 __isl_take isl_space
*model
)
11061 if (!map
|| !model
)
11064 ctx
= isl_space_get_ctx(model
);
11065 if (!isl_space_has_named_params(model
))
11066 isl_die(ctx
, isl_error_invalid
,
11067 "model has unnamed parameters", goto error
);
11068 if (!isl_space_has_named_params(map
->dim
))
11069 isl_die(ctx
, isl_error_invalid
,
11070 "relation has unnamed parameters", goto error
);
11071 if (!isl_space_match(map
->dim
, isl_dim_param
, model
, isl_dim_param
)) {
11072 isl_reordering
*exp
;
11074 model
= isl_space_drop_dims(model
, isl_dim_in
,
11075 0, isl_space_dim(model
, isl_dim_in
));
11076 model
= isl_space_drop_dims(model
, isl_dim_out
,
11077 0, isl_space_dim(model
, isl_dim_out
));
11078 exp
= isl_parameter_alignment_reordering(map
->dim
, model
);
11079 exp
= isl_reordering_extend_space(exp
, isl_map_get_space(map
));
11080 map
= isl_map_realign(map
, exp
);
11083 isl_space_free(model
);
11086 isl_space_free(model
);
11091 __isl_give isl_set
*isl_set_align_params(__isl_take isl_set
*set
,
11092 __isl_take isl_space
*model
)
11094 return isl_map_align_params(set
, model
);
11097 /* Align the parameters of "bmap" to those of "model", introducing
11098 * additional parameters if needed.
11100 __isl_give isl_basic_map
*isl_basic_map_align_params(
11101 __isl_take isl_basic_map
*bmap
, __isl_take isl_space
*model
)
11105 if (!bmap
|| !model
)
11108 ctx
= isl_space_get_ctx(model
);
11109 if (!isl_space_has_named_params(model
))
11110 isl_die(ctx
, isl_error_invalid
,
11111 "model has unnamed parameters", goto error
);
11112 if (!isl_space_has_named_params(bmap
->dim
))
11113 isl_die(ctx
, isl_error_invalid
,
11114 "relation has unnamed parameters", goto error
);
11115 if (!isl_space_match(bmap
->dim
, isl_dim_param
, model
, isl_dim_param
)) {
11116 isl_reordering
*exp
;
11117 struct isl_dim_map
*dim_map
;
11119 model
= isl_space_drop_dims(model
, isl_dim_in
,
11120 0, isl_space_dim(model
, isl_dim_in
));
11121 model
= isl_space_drop_dims(model
, isl_dim_out
,
11122 0, isl_space_dim(model
, isl_dim_out
));
11123 exp
= isl_parameter_alignment_reordering(bmap
->dim
, model
);
11124 exp
= isl_reordering_extend_space(exp
,
11125 isl_basic_map_get_space(bmap
));
11126 dim_map
= isl_dim_map_from_reordering(exp
);
11127 bmap
= isl_basic_map_realign(bmap
,
11128 exp
? isl_space_copy(exp
->dim
) : NULL
,
11129 isl_dim_map_extend(dim_map
, bmap
));
11130 isl_reordering_free(exp
);
11134 isl_space_free(model
);
11137 isl_space_free(model
);
11138 isl_basic_map_free(bmap
);
11142 /* Align the parameters of "bset" to those of "model", introducing
11143 * additional parameters if needed.
11145 __isl_give isl_basic_set
*isl_basic_set_align_params(
11146 __isl_take isl_basic_set
*bset
, __isl_take isl_space
*model
)
11148 return isl_basic_map_align_params(bset
, model
);
11151 __isl_give isl_mat
*isl_basic_map_equalities_matrix(
11152 __isl_keep isl_basic_map
*bmap
, enum isl_dim_type c1
,
11153 enum isl_dim_type c2
, enum isl_dim_type c3
,
11154 enum isl_dim_type c4
, enum isl_dim_type c5
)
11156 enum isl_dim_type c
[5] = { c1
, c2
, c3
, c4
, c5
};
11157 struct isl_mat
*mat
;
11163 mat
= isl_mat_alloc(bmap
->ctx
, bmap
->n_eq
,
11164 isl_basic_map_total_dim(bmap
) + 1);
11167 for (i
= 0; i
< bmap
->n_eq
; ++i
)
11168 for (j
= 0, pos
= 0; j
< 5; ++j
) {
11169 int off
= isl_basic_map_offset(bmap
, c
[j
]);
11170 for (k
= 0; k
< isl_basic_map_dim(bmap
, c
[j
]); ++k
) {
11171 isl_int_set(mat
->row
[i
][pos
],
11172 bmap
->eq
[i
][off
+ k
]);
11180 __isl_give isl_mat
*isl_basic_map_inequalities_matrix(
11181 __isl_keep isl_basic_map
*bmap
, enum isl_dim_type c1
,
11182 enum isl_dim_type c2
, enum isl_dim_type c3
,
11183 enum isl_dim_type c4
, enum isl_dim_type c5
)
11185 enum isl_dim_type c
[5] = { c1
, c2
, c3
, c4
, c5
};
11186 struct isl_mat
*mat
;
11192 mat
= isl_mat_alloc(bmap
->ctx
, bmap
->n_ineq
,
11193 isl_basic_map_total_dim(bmap
) + 1);
11196 for (i
= 0; i
< bmap
->n_ineq
; ++i
)
11197 for (j
= 0, pos
= 0; j
< 5; ++j
) {
11198 int off
= isl_basic_map_offset(bmap
, c
[j
]);
11199 for (k
= 0; k
< isl_basic_map_dim(bmap
, c
[j
]); ++k
) {
11200 isl_int_set(mat
->row
[i
][pos
],
11201 bmap
->ineq
[i
][off
+ k
]);
11209 __isl_give isl_basic_map
*isl_basic_map_from_constraint_matrices(
11210 __isl_take isl_space
*dim
,
11211 __isl_take isl_mat
*eq
, __isl_take isl_mat
*ineq
, enum isl_dim_type c1
,
11212 enum isl_dim_type c2
, enum isl_dim_type c3
,
11213 enum isl_dim_type c4
, enum isl_dim_type c5
)
11215 enum isl_dim_type c
[5] = { c1
, c2
, c3
, c4
, c5
};
11216 isl_basic_map
*bmap
;
11222 if (!dim
|| !eq
|| !ineq
)
11225 if (eq
->n_col
!= ineq
->n_col
)
11226 isl_die(dim
->ctx
, isl_error_invalid
,
11227 "equalities and inequalities matrices should have "
11228 "same number of columns", goto error
);
11230 total
= 1 + isl_space_dim(dim
, isl_dim_all
);
11232 if (eq
->n_col
< total
)
11233 isl_die(dim
->ctx
, isl_error_invalid
,
11234 "number of columns too small", goto error
);
11236 extra
= eq
->n_col
- total
;
11238 bmap
= isl_basic_map_alloc_space(isl_space_copy(dim
), extra
,
11239 eq
->n_row
, ineq
->n_row
);
11242 for (i
= 0; i
< extra
; ++i
) {
11243 k
= isl_basic_map_alloc_div(bmap
);
11246 isl_int_set_si(bmap
->div
[k
][0], 0);
11248 for (i
= 0; i
< eq
->n_row
; ++i
) {
11249 l
= isl_basic_map_alloc_equality(bmap
);
11252 for (j
= 0, pos
= 0; j
< 5; ++j
) {
11253 int off
= isl_basic_map_offset(bmap
, c
[j
]);
11254 for (k
= 0; k
< isl_basic_map_dim(bmap
, c
[j
]); ++k
) {
11255 isl_int_set(bmap
->eq
[l
][off
+ k
],
11261 for (i
= 0; i
< ineq
->n_row
; ++i
) {
11262 l
= isl_basic_map_alloc_inequality(bmap
);
11265 for (j
= 0, pos
= 0; j
< 5; ++j
) {
11266 int off
= isl_basic_map_offset(bmap
, c
[j
]);
11267 for (k
= 0; k
< isl_basic_map_dim(bmap
, c
[j
]); ++k
) {
11268 isl_int_set(bmap
->ineq
[l
][off
+ k
],
11269 ineq
->row
[i
][pos
]);
11275 isl_space_free(dim
);
11277 isl_mat_free(ineq
);
11279 bmap
= isl_basic_map_simplify(bmap
);
11280 return isl_basic_map_finalize(bmap
);
11282 isl_space_free(dim
);
11284 isl_mat_free(ineq
);
11288 __isl_give isl_mat
*isl_basic_set_equalities_matrix(
11289 __isl_keep isl_basic_set
*bset
, enum isl_dim_type c1
,
11290 enum isl_dim_type c2
, enum isl_dim_type c3
, enum isl_dim_type c4
)
11292 return isl_basic_map_equalities_matrix((isl_basic_map
*)bset
,
11293 c1
, c2
, c3
, c4
, isl_dim_in
);
11296 __isl_give isl_mat
*isl_basic_set_inequalities_matrix(
11297 __isl_keep isl_basic_set
*bset
, enum isl_dim_type c1
,
11298 enum isl_dim_type c2
, enum isl_dim_type c3
, enum isl_dim_type c4
)
11300 return isl_basic_map_inequalities_matrix((isl_basic_map
*)bset
,
11301 c1
, c2
, c3
, c4
, isl_dim_in
);
11304 __isl_give isl_basic_set
*isl_basic_set_from_constraint_matrices(
11305 __isl_take isl_space
*dim
,
11306 __isl_take isl_mat
*eq
, __isl_take isl_mat
*ineq
, enum isl_dim_type c1
,
11307 enum isl_dim_type c2
, enum isl_dim_type c3
, enum isl_dim_type c4
)
11309 return (isl_basic_set
*)
11310 isl_basic_map_from_constraint_matrices(dim
, eq
, ineq
,
11311 c1
, c2
, c3
, c4
, isl_dim_in
);
11314 int isl_basic_map_can_zip(__isl_keep isl_basic_map
*bmap
)
11319 return isl_space_can_zip(bmap
->dim
);
11322 int isl_map_can_zip(__isl_keep isl_map
*map
)
11327 return isl_space_can_zip(map
->dim
);
11330 /* Given a basic map (A -> B) -> (C -> D), return the corresponding basic map
11331 * (A -> C) -> (B -> D).
11333 __isl_give isl_basic_map
*isl_basic_map_zip(__isl_take isl_basic_map
*bmap
)
11342 if (!isl_basic_map_can_zip(bmap
))
11343 isl_die(bmap
->ctx
, isl_error_invalid
,
11344 "basic map cannot be zipped", goto error
);
11345 pos
= isl_basic_map_offset(bmap
, isl_dim_in
) +
11346 isl_space_dim(bmap
->dim
->nested
[0], isl_dim_in
);
11347 n1
= isl_space_dim(bmap
->dim
->nested
[0], isl_dim_out
);
11348 n2
= isl_space_dim(bmap
->dim
->nested
[1], isl_dim_in
);
11349 bmap
= isl_basic_map_cow(bmap
);
11350 bmap
= isl_basic_map_swap_vars(bmap
, pos
, n1
, n2
);
11353 bmap
->dim
= isl_space_zip(bmap
->dim
);
11358 isl_basic_map_free(bmap
);
11362 /* Given a map (A -> B) -> (C -> D), return the corresponding map
11363 * (A -> C) -> (B -> D).
11365 __isl_give isl_map
*isl_map_zip(__isl_take isl_map
*map
)
11372 if (!isl_map_can_zip(map
))
11373 isl_die(map
->ctx
, isl_error_invalid
, "map cannot be zipped",
11376 map
= isl_map_cow(map
);
11380 for (i
= 0; i
< map
->n
; ++i
) {
11381 map
->p
[i
] = isl_basic_map_zip(map
->p
[i
]);
11386 map
->dim
= isl_space_zip(map
->dim
);
11396 /* Can we apply isl_basic_map_curry to "bmap"?
11397 * That is, does it have a nested relation in its domain?
11399 int isl_basic_map_can_curry(__isl_keep isl_basic_map
*bmap
)
11404 return isl_space_can_curry(bmap
->dim
);
11407 /* Can we apply isl_map_curry to "map"?
11408 * That is, does it have a nested relation in its domain?
11410 int isl_map_can_curry(__isl_keep isl_map
*map
)
11415 return isl_space_can_curry(map
->dim
);
11418 /* Given a basic map (A -> B) -> C, return the corresponding basic map
11421 __isl_give isl_basic_map
*isl_basic_map_curry(__isl_take isl_basic_map
*bmap
)
11427 if (!isl_basic_map_can_curry(bmap
))
11428 isl_die(bmap
->ctx
, isl_error_invalid
,
11429 "basic map cannot be curried", goto error
);
11430 bmap
= isl_basic_map_cow(bmap
);
11433 bmap
->dim
= isl_space_curry(bmap
->dim
);
11438 isl_basic_map_free(bmap
);
11442 /* Given a map (A -> B) -> C, return the corresponding map
11445 __isl_give isl_map
*isl_map_curry(__isl_take isl_map
*map
)
11452 if (!isl_map_can_curry(map
))
11453 isl_die(map
->ctx
, isl_error_invalid
, "map cannot be curried",
11456 map
= isl_map_cow(map
);
11460 for (i
= 0; i
< map
->n
; ++i
) {
11461 map
->p
[i
] = isl_basic_map_curry(map
->p
[i
]);
11466 map
->dim
= isl_space_curry(map
->dim
);
11476 /* Can we apply isl_basic_map_uncurry to "bmap"?
11477 * That is, does it have a nested relation in its domain?
11479 int isl_basic_map_can_uncurry(__isl_keep isl_basic_map
*bmap
)
11484 return isl_space_can_uncurry(bmap
->dim
);
11487 /* Can we apply isl_map_uncurry to "map"?
11488 * That is, does it have a nested relation in its domain?
11490 int isl_map_can_uncurry(__isl_keep isl_map
*map
)
11495 return isl_space_can_uncurry(map
->dim
);
11498 /* Given a basic map A -> (B -> C), return the corresponding basic map
11501 __isl_give isl_basic_map
*isl_basic_map_uncurry(__isl_take isl_basic_map
*bmap
)
11507 if (!isl_basic_map_can_uncurry(bmap
))
11508 isl_die(bmap
->ctx
, isl_error_invalid
,
11509 "basic map cannot be uncurried",
11510 return isl_basic_map_free(bmap
));
11511 bmap
= isl_basic_map_cow(bmap
);
11514 bmap
->dim
= isl_space_uncurry(bmap
->dim
);
11516 return isl_basic_map_free(bmap
);
11520 /* Given a map A -> (B -> C), return the corresponding map
11523 __isl_give isl_map
*isl_map_uncurry(__isl_take isl_map
*map
)
11530 if (!isl_map_can_uncurry(map
))
11531 isl_die(map
->ctx
, isl_error_invalid
, "map cannot be uncurried",
11532 return isl_map_free(map
));
11534 map
= isl_map_cow(map
);
11538 for (i
= 0; i
< map
->n
; ++i
) {
11539 map
->p
[i
] = isl_basic_map_uncurry(map
->p
[i
]);
11541 return isl_map_free(map
);
11544 map
->dim
= isl_space_uncurry(map
->dim
);
11546 return isl_map_free(map
);
11551 /* Construct a basic map mapping the domain of the affine expression
11552 * to a one-dimensional range prescribed by the affine expression.
11554 __isl_give isl_basic_map
*isl_basic_map_from_aff(__isl_take isl_aff
*aff
)
11558 isl_local_space
*ls
;
11559 isl_basic_map
*bmap
;
11564 ls
= isl_aff_get_local_space(aff
);
11565 bmap
= isl_basic_map_from_local_space(ls
);
11566 bmap
= isl_basic_map_extend_constraints(bmap
, 1, 0);
11567 k
= isl_basic_map_alloc_equality(bmap
);
11571 pos
= isl_basic_map_offset(bmap
, isl_dim_out
);
11572 isl_seq_cpy(bmap
->eq
[k
], aff
->v
->el
+ 1, pos
);
11573 isl_int_neg(bmap
->eq
[k
][pos
], aff
->v
->el
[0]);
11574 isl_seq_cpy(bmap
->eq
[k
] + pos
+ 1, aff
->v
->el
+ 1 + pos
,
11575 aff
->v
->size
- (pos
+ 1));
11578 bmap
= isl_basic_map_finalize(bmap
);
11582 isl_basic_map_free(bmap
);
11586 /* Construct a map mapping the domain of the affine expression
11587 * to a one-dimensional range prescribed by the affine expression.
11589 __isl_give isl_map
*isl_map_from_aff(__isl_take isl_aff
*aff
)
11591 isl_basic_map
*bmap
;
11593 bmap
= isl_basic_map_from_aff(aff
);
11594 return isl_map_from_basic_map(bmap
);
11597 /* Construct a basic map mapping the domain the multi-affine expression
11598 * to its range, with each dimension in the range equated to the
11599 * corresponding affine expression.
11601 __isl_give isl_basic_map
*isl_basic_map_from_multi_aff(
11602 __isl_take isl_multi_aff
*maff
)
11606 isl_basic_map
*bmap
;
11611 if (isl_space_dim(maff
->space
, isl_dim_out
) != maff
->n
)
11612 isl_die(isl_multi_aff_get_ctx(maff
), isl_error_internal
,
11613 "invalid space", goto error
);
11615 space
= isl_space_domain(isl_multi_aff_get_space(maff
));
11616 bmap
= isl_basic_map_universe(isl_space_from_domain(space
));
11618 for (i
= 0; i
< maff
->n
; ++i
) {
11620 isl_basic_map
*bmap_i
;
11622 aff
= isl_aff_copy(maff
->p
[i
]);
11623 bmap_i
= isl_basic_map_from_aff(aff
);
11625 bmap
= isl_basic_map_flat_range_product(bmap
, bmap_i
);
11628 bmap
= isl_basic_map_reset_space(bmap
, isl_multi_aff_get_space(maff
));
11630 isl_multi_aff_free(maff
);
11633 isl_multi_aff_free(maff
);
11637 /* Construct a map mapping the domain the multi-affine expression
11638 * to its range, with each dimension in the range equated to the
11639 * corresponding affine expression.
11641 __isl_give isl_map
*isl_map_from_multi_aff(__isl_take isl_multi_aff
*maff
)
11643 isl_basic_map
*bmap
;
11645 bmap
= isl_basic_map_from_multi_aff(maff
);
11646 return isl_map_from_basic_map(bmap
);
11649 /* Construct a basic map mapping a domain in the given space to
11650 * to an n-dimensional range, with n the number of elements in the list,
11651 * where each coordinate in the range is prescribed by the
11652 * corresponding affine expression.
11653 * The domains of all affine expressions in the list are assumed to match
11656 __isl_give isl_basic_map
*isl_basic_map_from_aff_list(
11657 __isl_take isl_space
*domain_dim
, __isl_take isl_aff_list
*list
)
11661 isl_basic_map
*bmap
;
11666 dim
= isl_space_from_domain(domain_dim
);
11667 bmap
= isl_basic_map_universe(dim
);
11669 for (i
= 0; i
< list
->n
; ++i
) {
11671 isl_basic_map
*bmap_i
;
11673 aff
= isl_aff_copy(list
->p
[i
]);
11674 bmap_i
= isl_basic_map_from_aff(aff
);
11676 bmap
= isl_basic_map_flat_range_product(bmap
, bmap_i
);
11679 isl_aff_list_free(list
);
11683 __isl_give isl_set
*isl_set_equate(__isl_take isl_set
*set
,
11684 enum isl_dim_type type1
, int pos1
, enum isl_dim_type type2
, int pos2
)
11686 return isl_map_equate(set
, type1
, pos1
, type2
, pos2
);
11689 /* Construct a basic map where the given dimensions are equal to each other.
11691 static __isl_give isl_basic_map
*equator(__isl_take isl_space
*space
,
11692 enum isl_dim_type type1
, int pos1
, enum isl_dim_type type2
, int pos2
)
11694 isl_basic_map
*bmap
= NULL
;
11700 if (pos1
>= isl_space_dim(space
, type1
))
11701 isl_die(isl_space_get_ctx(space
), isl_error_invalid
,
11702 "index out of bounds", goto error
);
11703 if (pos2
>= isl_space_dim(space
, type2
))
11704 isl_die(isl_space_get_ctx(space
), isl_error_invalid
,
11705 "index out of bounds", goto error
);
11707 if (type1
== type2
&& pos1
== pos2
)
11708 return isl_basic_map_universe(space
);
11710 bmap
= isl_basic_map_alloc_space(isl_space_copy(space
), 0, 1, 0);
11711 i
= isl_basic_map_alloc_equality(bmap
);
11714 isl_seq_clr(bmap
->eq
[i
], 1 + isl_basic_map_total_dim(bmap
));
11715 pos1
+= isl_basic_map_offset(bmap
, type1
);
11716 pos2
+= isl_basic_map_offset(bmap
, type2
);
11717 isl_int_set_si(bmap
->eq
[i
][pos1
], -1);
11718 isl_int_set_si(bmap
->eq
[i
][pos2
], 1);
11719 bmap
= isl_basic_map_finalize(bmap
);
11720 isl_space_free(space
);
11723 isl_space_free(space
);
11724 isl_basic_map_free(bmap
);
11728 /* Add a constraint imposing that the given two dimensions are equal.
11730 __isl_give isl_basic_map
*isl_basic_map_equate(__isl_take isl_basic_map
*bmap
,
11731 enum isl_dim_type type1
, int pos1
, enum isl_dim_type type2
, int pos2
)
11735 eq
= equator(isl_basic_map_get_space(bmap
), type1
, pos1
, type2
, pos2
);
11737 bmap
= isl_basic_map_intersect(bmap
, eq
);
11742 /* Add a constraint imposing that the given two dimensions are equal.
11744 __isl_give isl_map
*isl_map_equate(__isl_take isl_map
*map
,
11745 enum isl_dim_type type1
, int pos1
, enum isl_dim_type type2
, int pos2
)
11747 isl_basic_map
*bmap
;
11749 bmap
= equator(isl_map_get_space(map
), type1
, pos1
, type2
, pos2
);
11751 map
= isl_map_intersect(map
, isl_map_from_basic_map(bmap
));
11756 /* Add a constraint imposing that the given two dimensions have opposite values.
11758 __isl_give isl_map
*isl_map_oppose(__isl_take isl_map
*map
,
11759 enum isl_dim_type type1
, int pos1
, enum isl_dim_type type2
, int pos2
)
11761 isl_basic_map
*bmap
= NULL
;
11767 if (pos1
>= isl_map_dim(map
, type1
))
11768 isl_die(map
->ctx
, isl_error_invalid
,
11769 "index out of bounds", goto error
);
11770 if (pos2
>= isl_map_dim(map
, type2
))
11771 isl_die(map
->ctx
, isl_error_invalid
,
11772 "index out of bounds", goto error
);
11774 bmap
= isl_basic_map_alloc_space(isl_map_get_space(map
), 0, 1, 0);
11775 i
= isl_basic_map_alloc_equality(bmap
);
11778 isl_seq_clr(bmap
->eq
[i
], 1 + isl_basic_map_total_dim(bmap
));
11779 pos1
+= isl_basic_map_offset(bmap
, type1
);
11780 pos2
+= isl_basic_map_offset(bmap
, type2
);
11781 isl_int_set_si(bmap
->eq
[i
][pos1
], 1);
11782 isl_int_set_si(bmap
->eq
[i
][pos2
], 1);
11783 bmap
= isl_basic_map_finalize(bmap
);
11785 map
= isl_map_intersect(map
, isl_map_from_basic_map(bmap
));
11789 isl_basic_map_free(bmap
);
11794 /* Construct a constraint imposing that the value of the first dimension is
11795 * greater than or equal to that of the second.
11797 static __isl_give isl_constraint
*constraint_order_ge(
11798 __isl_take isl_space
*space
, enum isl_dim_type type1
, int pos1
,
11799 enum isl_dim_type type2
, int pos2
)
11806 c
= isl_inequality_alloc(isl_local_space_from_space(space
));
11808 if (pos1
>= isl_constraint_dim(c
, type1
))
11809 isl_die(isl_constraint_get_ctx(c
), isl_error_invalid
,
11810 "index out of bounds", return isl_constraint_free(c
));
11811 if (pos2
>= isl_constraint_dim(c
, type2
))
11812 isl_die(isl_constraint_get_ctx(c
), isl_error_invalid
,
11813 "index out of bounds", return isl_constraint_free(c
));
11815 if (type1
== type2
&& pos1
== pos2
)
11818 c
= isl_constraint_set_coefficient_si(c
, type1
, pos1
, 1);
11819 c
= isl_constraint_set_coefficient_si(c
, type2
, pos2
, -1);
11824 /* Add a constraint imposing that the value of the first dimension is
11825 * greater than or equal to that of the second.
11827 __isl_give isl_basic_map
*isl_basic_map_order_ge(__isl_take isl_basic_map
*bmap
,
11828 enum isl_dim_type type1
, int pos1
, enum isl_dim_type type2
, int pos2
)
11833 if (type1
== type2
&& pos1
== pos2
)
11835 space
= isl_basic_map_get_space(bmap
);
11836 c
= constraint_order_ge(space
, type1
, pos1
, type2
, pos2
);
11837 bmap
= isl_basic_map_add_constraint(bmap
, c
);
11842 /* Add a constraint imposing that the value of the first dimension is
11843 * greater than or equal to that of the second.
11845 __isl_give isl_map
*isl_map_order_ge(__isl_take isl_map
*map
,
11846 enum isl_dim_type type1
, int pos1
, enum isl_dim_type type2
, int pos2
)
11851 if (type1
== type2
&& pos1
== pos2
)
11853 space
= isl_map_get_space(map
);
11854 c
= constraint_order_ge(space
, type1
, pos1
, type2
, pos2
);
11855 map
= isl_map_add_constraint(map
, c
);
11860 /* Add a constraint imposing that the value of the first dimension is
11861 * less than or equal to that of the second.
11863 __isl_give isl_map
*isl_map_order_le(__isl_take isl_map
*map
,
11864 enum isl_dim_type type1
, int pos1
, enum isl_dim_type type2
, int pos2
)
11866 return isl_map_order_ge(map
, type2
, pos2
, type1
, pos1
);
11869 /* Construct a basic map where the value of the first dimension is
11870 * greater than that of the second.
11872 static __isl_give isl_basic_map
*greator(__isl_take isl_space
*space
,
11873 enum isl_dim_type type1
, int pos1
, enum isl_dim_type type2
, int pos2
)
11875 isl_basic_map
*bmap
= NULL
;
11881 if (pos1
>= isl_space_dim(space
, type1
))
11882 isl_die(isl_space_get_ctx(space
), isl_error_invalid
,
11883 "index out of bounds", goto error
);
11884 if (pos2
>= isl_space_dim(space
, type2
))
11885 isl_die(isl_space_get_ctx(space
), isl_error_invalid
,
11886 "index out of bounds", goto error
);
11888 if (type1
== type2
&& pos1
== pos2
)
11889 return isl_basic_map_empty(space
);
11891 bmap
= isl_basic_map_alloc_space(space
, 0, 0, 1);
11892 i
= isl_basic_map_alloc_inequality(bmap
);
11894 return isl_basic_map_free(bmap
);
11895 isl_seq_clr(bmap
->ineq
[i
], 1 + isl_basic_map_total_dim(bmap
));
11896 pos1
+= isl_basic_map_offset(bmap
, type1
);
11897 pos2
+= isl_basic_map_offset(bmap
, type2
);
11898 isl_int_set_si(bmap
->ineq
[i
][pos1
], 1);
11899 isl_int_set_si(bmap
->ineq
[i
][pos2
], -1);
11900 isl_int_set_si(bmap
->ineq
[i
][0], -1);
11901 bmap
= isl_basic_map_finalize(bmap
);
11905 isl_space_free(space
);
11906 isl_basic_map_free(bmap
);
11910 /* Add a constraint imposing that the value of the first dimension is
11911 * greater than that of the second.
11913 __isl_give isl_basic_map
*isl_basic_map_order_gt(__isl_take isl_basic_map
*bmap
,
11914 enum isl_dim_type type1
, int pos1
, enum isl_dim_type type2
, int pos2
)
11918 gt
= greator(isl_basic_map_get_space(bmap
), type1
, pos1
, type2
, pos2
);
11920 bmap
= isl_basic_map_intersect(bmap
, gt
);
11925 /* Add a constraint imposing that the value of the first dimension is
11926 * greater than that of the second.
11928 __isl_give isl_map
*isl_map_order_gt(__isl_take isl_map
*map
,
11929 enum isl_dim_type type1
, int pos1
, enum isl_dim_type type2
, int pos2
)
11931 isl_basic_map
*bmap
;
11933 bmap
= greator(isl_map_get_space(map
), type1
, pos1
, type2
, pos2
);
11935 map
= isl_map_intersect(map
, isl_map_from_basic_map(bmap
));
11940 /* Add a constraint imposing that the value of the first dimension is
11941 * smaller than that of the second.
11943 __isl_give isl_map
*isl_map_order_lt(__isl_take isl_map
*map
,
11944 enum isl_dim_type type1
, int pos1
, enum isl_dim_type type2
, int pos2
)
11946 return isl_map_order_gt(map
, type2
, pos2
, type1
, pos1
);
11949 __isl_give isl_aff
*isl_basic_map_get_div(__isl_keep isl_basic_map
*bmap
,
11953 isl_local_space
*ls
;
11958 if (!isl_basic_map_divs_known(bmap
))
11959 isl_die(isl_basic_map_get_ctx(bmap
), isl_error_invalid
,
11960 "some divs are unknown", return NULL
);
11962 ls
= isl_basic_map_get_local_space(bmap
);
11963 div
= isl_local_space_get_div(ls
, pos
);
11964 isl_local_space_free(ls
);
11969 __isl_give isl_aff
*isl_basic_set_get_div(__isl_keep isl_basic_set
*bset
,
11972 return isl_basic_map_get_div(bset
, pos
);
11975 /* Plug in "subs" for dimension "type", "pos" of "bset".
11977 * Let i be the dimension to replace and let "subs" be of the form
11981 * Any integer division with a non-zero coefficient for i,
11983 * floor((a i + g)/m)
11987 * floor((a f + d g)/(m d))
11989 * Constraints of the form
11997 * We currently require that "subs" is an integral expression.
11998 * Handling rational expressions may require us to add stride constraints
11999 * as we do in isl_basic_set_preimage_multi_aff.
12001 __isl_give isl_basic_set
*isl_basic_set_substitute(
12002 __isl_take isl_basic_set
*bset
,
12003 enum isl_dim_type type
, unsigned pos
, __isl_keep isl_aff
*subs
)
12009 if (bset
&& isl_basic_set_plain_is_empty(bset
))
12012 bset
= isl_basic_set_cow(bset
);
12013 if (!bset
|| !subs
)
12016 ctx
= isl_basic_set_get_ctx(bset
);
12017 if (!isl_space_is_equal(bset
->dim
, subs
->ls
->dim
))
12018 isl_die(ctx
, isl_error_invalid
,
12019 "spaces don't match", goto error
);
12020 if (isl_local_space_dim(subs
->ls
, isl_dim_div
) != 0)
12021 isl_die(ctx
, isl_error_unsupported
,
12022 "cannot handle divs yet", goto error
);
12023 if (!isl_int_is_one(subs
->v
->el
[0]))
12024 isl_die(ctx
, isl_error_invalid
,
12025 "can only substitute integer expressions", goto error
);
12027 pos
+= isl_basic_set_offset(bset
, type
);
12031 for (i
= 0; i
< bset
->n_eq
; ++i
) {
12032 if (isl_int_is_zero(bset
->eq
[i
][pos
]))
12034 isl_int_set(v
, bset
->eq
[i
][pos
]);
12035 isl_int_set_si(bset
->eq
[i
][pos
], 0);
12036 isl_seq_combine(bset
->eq
[i
], subs
->v
->el
[0], bset
->eq
[i
],
12037 v
, subs
->v
->el
+ 1, subs
->v
->size
- 1);
12040 for (i
= 0; i
< bset
->n_ineq
; ++i
) {
12041 if (isl_int_is_zero(bset
->ineq
[i
][pos
]))
12043 isl_int_set(v
, bset
->ineq
[i
][pos
]);
12044 isl_int_set_si(bset
->ineq
[i
][pos
], 0);
12045 isl_seq_combine(bset
->ineq
[i
], subs
->v
->el
[0], bset
->ineq
[i
],
12046 v
, subs
->v
->el
+ 1, subs
->v
->size
- 1);
12049 for (i
= 0; i
< bset
->n_div
; ++i
) {
12050 if (isl_int_is_zero(bset
->div
[i
][1 + pos
]))
12052 isl_int_set(v
, bset
->div
[i
][1 + pos
]);
12053 isl_int_set_si(bset
->div
[i
][1 + pos
], 0);
12054 isl_seq_combine(bset
->div
[i
] + 1,
12055 subs
->v
->el
[0], bset
->div
[i
] + 1,
12056 v
, subs
->v
->el
+ 1, subs
->v
->size
- 1);
12057 isl_int_mul(bset
->div
[i
][0], bset
->div
[i
][0], subs
->v
->el
[0]);
12062 bset
= isl_basic_set_simplify(bset
);
12063 return isl_basic_set_finalize(bset
);
12065 isl_basic_set_free(bset
);
12069 /* Plug in "subs" for dimension "type", "pos" of "set".
12071 __isl_give isl_set
*isl_set_substitute(__isl_take isl_set
*set
,
12072 enum isl_dim_type type
, unsigned pos
, __isl_keep isl_aff
*subs
)
12076 if (set
&& isl_set_plain_is_empty(set
))
12079 set
= isl_set_cow(set
);
12083 for (i
= set
->n
- 1; i
>= 0; --i
) {
12084 set
->p
[i
] = isl_basic_set_substitute(set
->p
[i
], type
, pos
, subs
);
12085 if (remove_if_empty(set
, i
) < 0)
12095 /* Check if the range of "ma" is compatible with the domain or range
12096 * (depending on "type") of "bmap".
12097 * Return -1 if anything is wrong.
12099 static int check_basic_map_compatible_range_multi_aff(
12100 __isl_keep isl_basic_map
*bmap
, enum isl_dim_type type
,
12101 __isl_keep isl_multi_aff
*ma
)
12104 isl_space
*ma_space
;
12106 ma_space
= isl_multi_aff_get_space(ma
);
12108 m
= isl_space_match(bmap
->dim
, isl_dim_param
, ma_space
, isl_dim_param
);
12112 isl_die(isl_basic_map_get_ctx(bmap
), isl_error_invalid
,
12113 "parameters don't match", goto error
);
12114 m
= isl_space_tuple_is_equal(bmap
->dim
, type
, ma_space
, isl_dim_out
);
12118 isl_die(isl_basic_map_get_ctx(bmap
), isl_error_invalid
,
12119 "spaces don't match", goto error
);
12121 isl_space_free(ma_space
);
12124 isl_space_free(ma_space
);
12128 /* Copy the divs from "ma" to "bmap", adding zeros for the "n_before"
12129 * coefficients before the transformed range of dimensions,
12130 * the "n_after" coefficients after the transformed range of dimensions
12131 * and the coefficients of the other divs in "bmap".
12133 static int set_ma_divs(__isl_keep isl_basic_map
*bmap
,
12134 __isl_keep isl_multi_aff
*ma
, int n_before
, int n_after
, int n_div
)
12139 isl_local_space
*ls
;
12144 ls
= isl_aff_get_domain_local_space(ma
->p
[0]);
12148 n_param
= isl_local_space_dim(ls
, isl_dim_param
);
12149 n_set
= isl_local_space_dim(ls
, isl_dim_set
);
12150 for (i
= 0; i
< n_div
; ++i
) {
12151 int o_bmap
= 0, o_ls
= 0;
12153 isl_seq_cpy(bmap
->div
[i
], ls
->div
->row
[i
], 1 + 1 + n_param
);
12154 o_bmap
+= 1 + 1 + n_param
;
12155 o_ls
+= 1 + 1 + n_param
;
12156 isl_seq_clr(bmap
->div
[i
] + o_bmap
, n_before
);
12157 o_bmap
+= n_before
;
12158 isl_seq_cpy(bmap
->div
[i
] + o_bmap
,
12159 ls
->div
->row
[i
] + o_ls
, n_set
);
12162 isl_seq_clr(bmap
->div
[i
] + o_bmap
, n_after
);
12164 isl_seq_cpy(bmap
->div
[i
] + o_bmap
,
12165 ls
->div
->row
[i
] + o_ls
, n_div
);
12168 isl_seq_clr(bmap
->div
[i
] + o_bmap
, bmap
->n_div
- n_div
);
12169 if (isl_basic_set_add_div_constraints(bmap
, i
) < 0)
12173 isl_local_space_free(ls
);
12176 isl_local_space_free(ls
);
12180 /* How many stride constraints does "ma" enforce?
12181 * That is, how many of the affine expressions have a denominator
12182 * different from one?
12184 static int multi_aff_strides(__isl_keep isl_multi_aff
*ma
)
12189 for (i
= 0; i
< ma
->n
; ++i
)
12190 if (!isl_int_is_one(ma
->p
[i
]->v
->el
[0]))
12196 /* For each affine expression in ma of the form
12198 * x_i = (f_i y + h_i)/m_i
12200 * with m_i different from one, add a constraint to "bmap"
12203 * f_i y + h_i = m_i alpha_i
12205 * with alpha_i an additional existentially quantified variable.
12207 static __isl_give isl_basic_map
*add_ma_strides(
12208 __isl_take isl_basic_map
*bmap
, __isl_keep isl_multi_aff
*ma
,
12209 int n_before
, int n_after
)
12218 total
= isl_basic_map_total_dim(bmap
);
12219 n_param
= isl_multi_aff_dim(ma
, isl_dim_param
);
12220 n_in
= isl_multi_aff_dim(ma
, isl_dim_in
);
12221 n_div
= isl_multi_aff_dim(ma
, isl_dim_div
);
12222 for (i
= 0; i
< ma
->n
; ++i
) {
12223 int o_bmap
= 0, o_ma
= 1;
12225 if (isl_int_is_one(ma
->p
[i
]->v
->el
[0]))
12227 div
= isl_basic_map_alloc_div(bmap
);
12228 k
= isl_basic_map_alloc_equality(bmap
);
12229 if (div
< 0 || k
< 0)
12231 isl_int_set_si(bmap
->div
[div
][0], 0);
12232 isl_seq_cpy(bmap
->eq
[k
] + o_bmap
,
12233 ma
->p
[i
]->v
->el
+ o_ma
, 1 + n_param
);
12234 o_bmap
+= 1 + n_param
;
12235 o_ma
+= 1 + n_param
;
12236 isl_seq_clr(bmap
->eq
[k
] + o_bmap
, n_before
);
12237 o_bmap
+= n_before
;
12238 isl_seq_cpy(bmap
->eq
[k
] + o_bmap
,
12239 ma
->p
[i
]->v
->el
+ o_ma
, n_in
);
12242 isl_seq_clr(bmap
->eq
[k
] + o_bmap
, n_after
);
12244 isl_seq_cpy(bmap
->eq
[k
] + o_bmap
,
12245 ma
->p
[i
]->v
->el
+ o_ma
, n_div
);
12248 isl_seq_clr(bmap
->eq
[k
] + o_bmap
, 1 + total
- o_bmap
);
12249 isl_int_neg(bmap
->eq
[k
][1 + total
], ma
->p
[i
]->v
->el
[0]);
12255 isl_basic_map_free(bmap
);
12259 /* Replace the domain or range space (depending on "type) of "space" by "set".
12261 static __isl_give isl_space
*isl_space_set(__isl_take isl_space
*space
,
12262 enum isl_dim_type type
, __isl_take isl_space
*set
)
12264 if (type
== isl_dim_in
) {
12265 space
= isl_space_range(space
);
12266 space
= isl_space_map_from_domain_and_range(set
, space
);
12268 space
= isl_space_domain(space
);
12269 space
= isl_space_map_from_domain_and_range(space
, set
);
12275 /* Compute the preimage of the domain or range (depending on "type")
12276 * of "bmap" under the function represented by "ma".
12277 * In other words, plug in "ma" in the domain or range of "bmap".
12278 * The result is a basic map that lives in the same space as "bmap"
12279 * except that the domain or range has been replaced by
12280 * the domain space of "ma".
12282 * If bmap is represented by
12284 * A(p) + S u + B x + T v + C(divs) >= 0,
12286 * where u and x are input and output dimensions if type == isl_dim_out
12287 * while x and v are input and output dimensions if type == isl_dim_in,
12288 * and ma is represented by
12290 * x = D(p) + F(y) + G(divs')
12292 * then the result is
12294 * A(p) + B D(p) + S u + B F(y) + T v + B G(divs') + C(divs) >= 0
12296 * The divs in the input set are similarly adjusted.
12299 * floor((a_i(p) + s u + b_i x + t v + c_i(divs))/n_i)
12303 * floor((a_i(p) + b_i D(p) + s u + b_i F(y) + t v +
12304 * B_i G(divs') + c_i(divs))/n_i)
12306 * If bmap is not a rational map and if F(y) involves any denominators
12308 * x_i = (f_i y + h_i)/m_i
12310 * then additional constraints are added to ensure that we only
12311 * map back integer points. That is we enforce
12313 * f_i y + h_i = m_i alpha_i
12315 * with alpha_i an additional existentially quantified variable.
12317 * We first copy over the divs from "ma".
12318 * Then we add the modified constraints and divs from "bmap".
12319 * Finally, we add the stride constraints, if needed.
12321 __isl_give isl_basic_map
*isl_basic_map_preimage_multi_aff(
12322 __isl_take isl_basic_map
*bmap
, enum isl_dim_type type
,
12323 __isl_take isl_multi_aff
*ma
)
12327 isl_basic_map
*res
= NULL
;
12328 int n_before
, n_after
, n_div_bmap
, n_div_ma
;
12329 isl_int f
, c1
, c2
, g
;
12330 int rational
, strides
;
12337 ma
= isl_multi_aff_align_divs(ma
);
12340 if (check_basic_map_compatible_range_multi_aff(bmap
, type
, ma
) < 0)
12343 if (type
== isl_dim_in
) {
12345 n_after
= isl_basic_map_dim(bmap
, isl_dim_out
);
12347 n_before
= isl_basic_map_dim(bmap
, isl_dim_in
);
12350 n_div_bmap
= isl_basic_map_dim(bmap
, isl_dim_div
);
12351 n_div_ma
= ma
->n
? isl_aff_dim(ma
->p
[0], isl_dim_div
) : 0;
12353 space
= isl_multi_aff_get_domain_space(ma
);
12354 space
= isl_space_set(isl_basic_map_get_space(bmap
), type
, space
);
12355 rational
= isl_basic_map_is_rational(bmap
);
12356 strides
= rational
? 0 : multi_aff_strides(ma
);
12357 res
= isl_basic_map_alloc_space(space
, n_div_ma
+ n_div_bmap
+ strides
,
12358 bmap
->n_eq
+ strides
, bmap
->n_ineq
+ 2 * n_div_ma
);
12360 res
= isl_basic_map_set_rational(res
);
12362 for (i
= 0; i
< n_div_ma
+ n_div_bmap
; ++i
)
12363 if (isl_basic_map_alloc_div(res
) < 0)
12366 if (set_ma_divs(res
, ma
, n_before
, n_after
, n_div_ma
) < 0)
12369 for (i
= 0; i
< bmap
->n_eq
; ++i
) {
12370 k
= isl_basic_map_alloc_equality(res
);
12373 isl_seq_preimage(res
->eq
[k
], bmap
->eq
[i
], ma
, n_before
,
12374 n_after
, n_div_ma
, n_div_bmap
, f
, c1
, c2
, g
, 0);
12377 for (i
= 0; i
< bmap
->n_ineq
; ++i
) {
12378 k
= isl_basic_map_alloc_inequality(res
);
12381 isl_seq_preimage(res
->ineq
[k
], bmap
->ineq
[i
], ma
, n_before
,
12382 n_after
, n_div_ma
, n_div_bmap
, f
, c1
, c2
, g
, 0);
12385 for (i
= 0; i
< bmap
->n_div
; ++i
) {
12386 if (isl_int_is_zero(bmap
->div
[i
][0])) {
12387 isl_int_set_si(res
->div
[n_div_ma
+ i
][0], 0);
12390 isl_seq_preimage(res
->div
[n_div_ma
+ i
], bmap
->div
[i
], ma
,
12391 n_before
, n_after
, n_div_ma
, n_div_bmap
,
12396 res
= add_ma_strides(res
, ma
, n_before
, n_after
);
12402 isl_basic_map_free(bmap
);
12403 isl_multi_aff_free(ma
);
12404 res
= isl_basic_set_simplify(res
);
12405 return isl_basic_map_finalize(res
);
12411 isl_basic_map_free(bmap
);
12412 isl_multi_aff_free(ma
);
12413 isl_basic_map_free(res
);
12417 /* Compute the preimage of "bset" under the function represented by "ma".
12418 * In other words, plug in "ma" in "bset". The result is a basic set
12419 * that lives in the domain space of "ma".
12421 __isl_give isl_basic_set
*isl_basic_set_preimage_multi_aff(
12422 __isl_take isl_basic_set
*bset
, __isl_take isl_multi_aff
*ma
)
12424 return isl_basic_map_preimage_multi_aff(bset
, isl_dim_set
, ma
);
12427 /* Compute the preimage of the domain of "bmap" under the function
12428 * represented by "ma".
12429 * In other words, plug in "ma" in the domain of "bmap".
12430 * The result is a basic map that lives in the same space as "bmap"
12431 * except that the domain has been replaced by the domain space of "ma".
12433 __isl_give isl_basic_map
*isl_basic_map_preimage_domain_multi_aff(
12434 __isl_take isl_basic_map
*bmap
, __isl_take isl_multi_aff
*ma
)
12436 return isl_basic_map_preimage_multi_aff(bmap
, isl_dim_in
, ma
);
12439 /* Compute the preimage of the range of "bmap" under the function
12440 * represented by "ma".
12441 * In other words, plug in "ma" in the range of "bmap".
12442 * The result is a basic map that lives in the same space as "bmap"
12443 * except that the range has been replaced by the domain space of "ma".
12445 __isl_give isl_basic_map
*isl_basic_map_preimage_range_multi_aff(
12446 __isl_take isl_basic_map
*bmap
, __isl_take isl_multi_aff
*ma
)
12448 return isl_basic_map_preimage_multi_aff(bmap
, isl_dim_out
, ma
);
12451 /* Check if the range of "ma" is compatible with the domain or range
12452 * (depending on "type") of "map".
12453 * Return -1 if anything is wrong.
12455 static int check_map_compatible_range_multi_aff(
12456 __isl_keep isl_map
*map
, enum isl_dim_type type
,
12457 __isl_keep isl_multi_aff
*ma
)
12460 isl_space
*ma_space
;
12462 ma_space
= isl_multi_aff_get_space(ma
);
12463 m
= isl_space_tuple_is_equal(map
->dim
, type
, ma_space
, isl_dim_out
);
12464 isl_space_free(ma_space
);
12466 isl_die(isl_map_get_ctx(map
), isl_error_invalid
,
12467 "spaces don't match", return -1);
12471 /* Compute the preimage of the domain or range (depending on "type")
12472 * of "map" under the function represented by "ma".
12473 * In other words, plug in "ma" in the domain or range of "map".
12474 * The result is a map that lives in the same space as "map"
12475 * except that the domain or range has been replaced by
12476 * the domain space of "ma".
12478 * The parameters are assumed to have been aligned.
12480 static __isl_give isl_map
*map_preimage_multi_aff(__isl_take isl_map
*map
,
12481 enum isl_dim_type type
, __isl_take isl_multi_aff
*ma
)
12486 map
= isl_map_cow(map
);
12487 ma
= isl_multi_aff_align_divs(ma
);
12490 if (check_map_compatible_range_multi_aff(map
, type
, ma
) < 0)
12493 for (i
= 0; i
< map
->n
; ++i
) {
12494 map
->p
[i
] = isl_basic_map_preimage_multi_aff(map
->p
[i
], type
,
12495 isl_multi_aff_copy(ma
));
12500 space
= isl_multi_aff_get_domain_space(ma
);
12501 space
= isl_space_set(isl_map_get_space(map
), type
, space
);
12503 isl_space_free(map
->dim
);
12508 isl_multi_aff_free(ma
);
12510 ISL_F_CLR(map
, ISL_MAP_DISJOINT
);
12511 ISL_F_CLR(map
, ISL_SET_NORMALIZED
);
12514 isl_multi_aff_free(ma
);
12519 /* Compute the preimage of the domain or range (depending on "type")
12520 * of "map" under the function represented by "ma".
12521 * In other words, plug in "ma" in the domain or range of "map".
12522 * The result is a map that lives in the same space as "map"
12523 * except that the domain or range has been replaced by
12524 * the domain space of "ma".
12526 __isl_give isl_map
*isl_map_preimage_multi_aff(__isl_take isl_map
*map
,
12527 enum isl_dim_type type
, __isl_take isl_multi_aff
*ma
)
12532 if (isl_space_match(map
->dim
, isl_dim_param
, ma
->space
, isl_dim_param
))
12533 return map_preimage_multi_aff(map
, type
, ma
);
12535 if (!isl_space_has_named_params(map
->dim
) ||
12536 !isl_space_has_named_params(ma
->space
))
12537 isl_die(map
->ctx
, isl_error_invalid
,
12538 "unaligned unnamed parameters", goto error
);
12539 map
= isl_map_align_params(map
, isl_multi_aff_get_space(ma
));
12540 ma
= isl_multi_aff_align_params(ma
, isl_map_get_space(map
));
12542 return map_preimage_multi_aff(map
, type
, ma
);
12544 isl_multi_aff_free(ma
);
12545 return isl_map_free(map
);
12548 /* Compute the preimage of "set" under the function represented by "ma".
12549 * In other words, plug in "ma" in "set". The result is a set
12550 * that lives in the domain space of "ma".
12552 __isl_give isl_set
*isl_set_preimage_multi_aff(__isl_take isl_set
*set
,
12553 __isl_take isl_multi_aff
*ma
)
12555 return isl_map_preimage_multi_aff(set
, isl_dim_set
, ma
);
12558 /* Compute the preimage of the domain of "map" under the function
12559 * represented by "ma".
12560 * In other words, plug in "ma" in the domain of "map".
12561 * The result is a map that lives in the same space as "map"
12562 * except that the domain has been replaced by the domain space of "ma".
12564 __isl_give isl_map
*isl_map_preimage_domain_multi_aff(__isl_take isl_map
*map
,
12565 __isl_take isl_multi_aff
*ma
)
12567 return isl_map_preimage_multi_aff(map
, isl_dim_in
, ma
);
12570 /* Compute the preimage of the range of "map" under the function
12571 * represented by "ma".
12572 * In other words, plug in "ma" in the range of "map".
12573 * The result is a map that lives in the same space as "map"
12574 * except that the range has been replaced by the domain space of "ma".
12576 __isl_give isl_map
*isl_map_preimage_range_multi_aff(__isl_take isl_map
*map
,
12577 __isl_take isl_multi_aff
*ma
)
12579 return isl_map_preimage_multi_aff(map
, isl_dim_out
, ma
);
12582 /* Compute the preimage of "map" under the function represented by "pma".
12583 * In other words, plug in "pma" in the domain or range of "map".
12584 * The result is a map that lives in the same space as "map",
12585 * except that the space of type "type" has been replaced by
12586 * the domain space of "pma".
12588 * The parameters of "map" and "pma" are assumed to have been aligned.
12590 static __isl_give isl_map
*isl_map_preimage_pw_multi_aff_aligned(
12591 __isl_take isl_map
*map
, enum isl_dim_type type
,
12592 __isl_take isl_pw_multi_aff
*pma
)
12601 isl_pw_multi_aff_free(pma
);
12602 res
= isl_map_empty(isl_map_get_space(map
));
12607 res
= isl_map_preimage_multi_aff(isl_map_copy(map
), type
,
12608 isl_multi_aff_copy(pma
->p
[0].maff
));
12609 if (type
== isl_dim_in
)
12610 res
= isl_map_intersect_domain(res
,
12611 isl_map_copy(pma
->p
[0].set
));
12613 res
= isl_map_intersect_range(res
,
12614 isl_map_copy(pma
->p
[0].set
));
12616 for (i
= 1; i
< pma
->n
; ++i
) {
12619 res_i
= isl_map_preimage_multi_aff(isl_map_copy(map
), type
,
12620 isl_multi_aff_copy(pma
->p
[i
].maff
));
12621 if (type
== isl_dim_in
)
12622 res_i
= isl_map_intersect_domain(res_i
,
12623 isl_map_copy(pma
->p
[i
].set
));
12625 res_i
= isl_map_intersect_range(res_i
,
12626 isl_map_copy(pma
->p
[i
].set
));
12627 res
= isl_map_union(res
, res_i
);
12630 isl_pw_multi_aff_free(pma
);
12634 isl_pw_multi_aff_free(pma
);
12639 /* Compute the preimage of "map" under the function represented by "pma".
12640 * In other words, plug in "pma" in the domain or range of "map".
12641 * The result is a map that lives in the same space as "map",
12642 * except that the space of type "type" has been replaced by
12643 * the domain space of "pma".
12645 __isl_give isl_map
*isl_map_preimage_pw_multi_aff(__isl_take isl_map
*map
,
12646 enum isl_dim_type type
, __isl_take isl_pw_multi_aff
*pma
)
12651 if (isl_space_match(map
->dim
, isl_dim_param
, pma
->dim
, isl_dim_param
))
12652 return isl_map_preimage_pw_multi_aff_aligned(map
, type
, pma
);
12654 if (!isl_space_has_named_params(map
->dim
) ||
12655 !isl_space_has_named_params(pma
->dim
))
12656 isl_die(map
->ctx
, isl_error_invalid
,
12657 "unaligned unnamed parameters", goto error
);
12658 map
= isl_map_align_params(map
, isl_pw_multi_aff_get_space(pma
));
12659 pma
= isl_pw_multi_aff_align_params(pma
, isl_map_get_space(map
));
12661 return isl_map_preimage_pw_multi_aff_aligned(map
, type
, pma
);
12663 isl_pw_multi_aff_free(pma
);
12664 return isl_map_free(map
);
12667 /* Compute the preimage of "set" under the function represented by "pma".
12668 * In other words, plug in "pma" in "set". The result is a set
12669 * that lives in the domain space of "pma".
12671 __isl_give isl_set
*isl_set_preimage_pw_multi_aff(__isl_take isl_set
*set
,
12672 __isl_take isl_pw_multi_aff
*pma
)
12674 return isl_map_preimage_pw_multi_aff(set
, isl_dim_set
, pma
);
12677 /* Compute the preimage of the domain of "map" under the function
12678 * represented by "pma".
12679 * In other words, plug in "pma" in the domain of "map".
12680 * The result is a map that lives in the same space as "map",
12681 * except that domain space has been replaced by the domain space of "pma".
12683 __isl_give isl_map
*isl_map_preimage_domain_pw_multi_aff(
12684 __isl_take isl_map
*map
, __isl_take isl_pw_multi_aff
*pma
)
12686 return isl_map_preimage_pw_multi_aff(map
, isl_dim_in
, pma
);
12689 /* Compute the preimage of the range of "map" under the function
12690 * represented by "pma".
12691 * In other words, plug in "pma" in the range of "map".
12692 * The result is a map that lives in the same space as "map",
12693 * except that range space has been replaced by the domain space of "pma".
12695 __isl_give isl_map
*isl_map_preimage_range_pw_multi_aff(
12696 __isl_take isl_map
*map
, __isl_take isl_pw_multi_aff
*pma
)
12698 return isl_map_preimage_pw_multi_aff(map
, isl_dim_out
, pma
);
12701 /* Compute the preimage of "map" under the function represented by "mpa".
12702 * In other words, plug in "mpa" in the domain or range of "map".
12703 * The result is a map that lives in the same space as "map",
12704 * except that the space of type "type" has been replaced by
12705 * the domain space of "mpa".
12707 * If the map does not involve any constraints that refer to the
12708 * dimensions of the substituted space, then the only possible
12709 * effect of "mpa" on the map is to map the space to a different space.
12710 * We create a separate isl_multi_aff to effectuate this change
12711 * in order to avoid spurious splitting of the map along the pieces
12714 __isl_give isl_map
*isl_map_preimage_multi_pw_aff(__isl_take isl_map
*map
,
12715 enum isl_dim_type type
, __isl_take isl_multi_pw_aff
*mpa
)
12718 isl_pw_multi_aff
*pma
;
12723 n
= isl_map_dim(map
, type
);
12724 if (!isl_map_involves_dims(map
, type
, 0, n
)) {
12728 space
= isl_multi_pw_aff_get_space(mpa
);
12729 isl_multi_pw_aff_free(mpa
);
12730 ma
= isl_multi_aff_zero(space
);
12731 return isl_map_preimage_multi_aff(map
, type
, ma
);
12734 pma
= isl_pw_multi_aff_from_multi_pw_aff(mpa
);
12735 return isl_map_preimage_pw_multi_aff(map
, type
, pma
);
12738 isl_multi_pw_aff_free(mpa
);
12742 /* Compute the preimage of "map" under the function represented by "mpa".
12743 * In other words, plug in "mpa" in the domain "map".
12744 * The result is a map that lives in the same space as "map",
12745 * except that domain space has been replaced by the domain space of "mpa".
12747 __isl_give isl_map
*isl_map_preimage_domain_multi_pw_aff(
12748 __isl_take isl_map
*map
, __isl_take isl_multi_pw_aff
*mpa
)
12750 return isl_map_preimage_multi_pw_aff(map
, isl_dim_in
, mpa
);
12753 /* Compute the preimage of "set" by the function represented by "mpa".
12754 * In other words, plug in "mpa" in "set".
12756 __isl_give isl_set
*isl_set_preimage_multi_pw_aff(__isl_take isl_set
*set
,
12757 __isl_take isl_multi_pw_aff
*mpa
)
12759 return isl_map_preimage_multi_pw_aff(set
, isl_dim_set
, mpa
);