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
)
7759 isl_basic_set
*nonneg
;
7767 isl_assert(set
->ctx
, first
+ n
<= isl_set_dim(set
, type
), goto error
);
7769 for (i
= 0; i
< n
; ++i
) {
7770 nonneg
= nonneg_halfspace(isl_set_get_space(set
),
7771 pos(set
->dim
, type
) + first
+ i
);
7772 neg
= neg_halfspace(isl_set_get_space(set
),
7773 pos(set
->dim
, type
) + first
+ i
);
7775 set
= isl_set_intersect(set
, isl_basic_set_union(nonneg
, neg
));
7784 static int foreach_orthant(__isl_take isl_set
*set
, int *signs
, int first
,
7785 int len
, int (*fn
)(__isl_take isl_set
*orthant
, int *signs
, void *user
),
7792 if (isl_set_plain_is_empty(set
)) {
7797 return fn(set
, signs
, user
);
7800 half
= isl_set_from_basic_set(nonneg_halfspace(isl_set_get_space(set
),
7802 half
= isl_set_intersect(half
, isl_set_copy(set
));
7803 if (foreach_orthant(half
, signs
, first
+ 1, len
, fn
, user
) < 0)
7807 half
= isl_set_from_basic_set(neg_halfspace(isl_set_get_space(set
),
7809 half
= isl_set_intersect(half
, set
);
7810 return foreach_orthant(half
, signs
, first
+ 1, len
, fn
, user
);
7816 /* Call "fn" on the intersections of "set" with each of the orthants
7817 * (except for obviously empty intersections). The orthant is identified
7818 * by the signs array, with each entry having value 1 or -1 according
7819 * to the sign of the corresponding variable.
7821 int isl_set_foreach_orthant(__isl_keep isl_set
*set
,
7822 int (*fn
)(__isl_take isl_set
*orthant
, int *signs
, void *user
),
7832 if (isl_set_plain_is_empty(set
))
7835 nparam
= isl_set_dim(set
, isl_dim_param
);
7836 nvar
= isl_set_dim(set
, isl_dim_set
);
7838 signs
= isl_alloc_array(set
->ctx
, int, nparam
+ nvar
);
7840 r
= foreach_orthant(isl_set_copy(set
), signs
, 0, nparam
+ nvar
,
7848 int isl_set_is_equal(struct isl_set
*set1
, struct isl_set
*set2
)
7850 return isl_map_is_equal((struct isl_map
*)set1
, (struct isl_map
*)set2
);
7853 int isl_basic_map_is_subset(
7854 struct isl_basic_map
*bmap1
, struct isl_basic_map
*bmap2
)
7857 struct isl_map
*map1
;
7858 struct isl_map
*map2
;
7860 if (!bmap1
|| !bmap2
)
7863 map1
= isl_map_from_basic_map(isl_basic_map_copy(bmap1
));
7864 map2
= isl_map_from_basic_map(isl_basic_map_copy(bmap2
));
7866 is_subset
= isl_map_is_subset(map1
, map2
);
7874 int isl_basic_set_is_subset(__isl_keep isl_basic_set
*bset1
,
7875 __isl_keep isl_basic_set
*bset2
)
7877 return isl_basic_map_is_subset(bset1
, bset2
);
7880 int isl_basic_map_is_equal(
7881 struct isl_basic_map
*bmap1
, struct isl_basic_map
*bmap2
)
7885 if (!bmap1
|| !bmap2
)
7887 is_subset
= isl_basic_map_is_subset(bmap1
, bmap2
);
7890 is_subset
= isl_basic_map_is_subset(bmap2
, bmap1
);
7894 int isl_basic_set_is_equal(
7895 struct isl_basic_set
*bset1
, struct isl_basic_set
*bset2
)
7897 return isl_basic_map_is_equal(
7898 (struct isl_basic_map
*)bset1
, (struct isl_basic_map
*)bset2
);
7901 int isl_map_is_empty(struct isl_map
*map
)
7908 for (i
= 0; i
< map
->n
; ++i
) {
7909 is_empty
= isl_basic_map_is_empty(map
->p
[i
]);
7918 int isl_map_plain_is_empty(__isl_keep isl_map
*map
)
7920 return map
? map
->n
== 0 : -1;
7923 int isl_map_fast_is_empty(__isl_keep isl_map
*map
)
7925 return isl_map_plain_is_empty(map
);
7928 int isl_set_plain_is_empty(struct isl_set
*set
)
7930 return set
? set
->n
== 0 : -1;
7933 int isl_set_fast_is_empty(__isl_keep isl_set
*set
)
7935 return isl_set_plain_is_empty(set
);
7938 int isl_set_is_empty(struct isl_set
*set
)
7940 return isl_map_is_empty((struct isl_map
*)set
);
7943 int isl_map_has_equal_space(__isl_keep isl_map
*map1
, __isl_keep isl_map
*map2
)
7948 return isl_space_is_equal(map1
->dim
, map2
->dim
);
7951 int isl_set_has_equal_space(__isl_keep isl_set
*set1
, __isl_keep isl_set
*set2
)
7956 return isl_space_is_equal(set1
->dim
, set2
->dim
);
7959 static int map_is_equal(__isl_keep isl_map
*map1
, __isl_keep isl_map
*map2
)
7965 is_subset
= isl_map_is_subset(map1
, map2
);
7968 is_subset
= isl_map_is_subset(map2
, map1
);
7972 int isl_map_is_equal(__isl_keep isl_map
*map1
, __isl_keep isl_map
*map2
)
7974 return isl_map_align_params_map_map_and_test(map1
, map2
, &map_is_equal
);
7977 int isl_basic_map_is_strict_subset(
7978 struct isl_basic_map
*bmap1
, struct isl_basic_map
*bmap2
)
7982 if (!bmap1
|| !bmap2
)
7984 is_subset
= isl_basic_map_is_subset(bmap1
, bmap2
);
7987 is_subset
= isl_basic_map_is_subset(bmap2
, bmap1
);
7988 if (is_subset
== -1)
7993 int isl_map_is_strict_subset(struct isl_map
*map1
, struct isl_map
*map2
)
7999 is_subset
= isl_map_is_subset(map1
, map2
);
8002 is_subset
= isl_map_is_subset(map2
, map1
);
8003 if (is_subset
== -1)
8008 int isl_set_is_strict_subset(__isl_keep isl_set
*set1
, __isl_keep isl_set
*set2
)
8010 return isl_map_is_strict_subset((isl_map
*)set1
, (isl_map
*)set2
);
8013 int isl_basic_map_is_universe(struct isl_basic_map
*bmap
)
8017 return bmap
->n_eq
== 0 && bmap
->n_ineq
== 0;
8020 int isl_basic_set_is_universe(struct isl_basic_set
*bset
)
8024 return bset
->n_eq
== 0 && bset
->n_ineq
== 0;
8027 int isl_map_plain_is_universe(__isl_keep isl_map
*map
)
8034 for (i
= 0; i
< map
->n
; ++i
) {
8035 int r
= isl_basic_map_is_universe(map
->p
[i
]);
8043 int isl_set_plain_is_universe(__isl_keep isl_set
*set
)
8045 return isl_map_plain_is_universe((isl_map
*) set
);
8048 int isl_set_fast_is_universe(__isl_keep isl_set
*set
)
8050 return isl_set_plain_is_universe(set
);
8053 int isl_basic_map_is_empty(struct isl_basic_map
*bmap
)
8055 struct isl_basic_set
*bset
= NULL
;
8056 struct isl_vec
*sample
= NULL
;
8063 if (ISL_F_ISSET(bmap
, ISL_BASIC_MAP_EMPTY
))
8066 if (isl_basic_map_is_universe(bmap
))
8069 if (ISL_F_ISSET(bmap
, ISL_BASIC_MAP_RATIONAL
)) {
8070 struct isl_basic_map
*copy
= isl_basic_map_copy(bmap
);
8071 copy
= isl_basic_map_remove_redundancies(copy
);
8072 empty
= isl_basic_map_plain_is_empty(copy
);
8073 isl_basic_map_free(copy
);
8077 total
= 1 + isl_basic_map_total_dim(bmap
);
8078 if (bmap
->sample
&& bmap
->sample
->size
== total
) {
8079 int contains
= isl_basic_map_contains(bmap
, bmap
->sample
);
8085 isl_vec_free(bmap
->sample
);
8086 bmap
->sample
= NULL
;
8087 bset
= isl_basic_map_underlying_set(isl_basic_map_copy(bmap
));
8090 sample
= isl_basic_set_sample_vec(bset
);
8093 empty
= sample
->size
== 0;
8094 isl_vec_free(bmap
->sample
);
8095 bmap
->sample
= sample
;
8097 ISL_F_SET(bmap
, ISL_BASIC_MAP_EMPTY
);
8102 int isl_basic_map_plain_is_empty(__isl_keep isl_basic_map
*bmap
)
8106 return ISL_F_ISSET(bmap
, ISL_BASIC_MAP_EMPTY
);
8109 int isl_basic_map_fast_is_empty(__isl_keep isl_basic_map
*bmap
)
8111 return isl_basic_map_plain_is_empty(bmap
);
8114 int isl_basic_set_plain_is_empty(__isl_keep isl_basic_set
*bset
)
8118 return ISL_F_ISSET(bset
, ISL_BASIC_SET_EMPTY
);
8121 int isl_basic_set_fast_is_empty(__isl_keep isl_basic_set
*bset
)
8123 return isl_basic_set_plain_is_empty(bset
);
8126 int isl_basic_set_is_empty(struct isl_basic_set
*bset
)
8128 return isl_basic_map_is_empty((struct isl_basic_map
*)bset
);
8131 struct isl_map
*isl_basic_map_union(
8132 struct isl_basic_map
*bmap1
, struct isl_basic_map
*bmap2
)
8134 struct isl_map
*map
;
8135 if (!bmap1
|| !bmap2
)
8138 isl_assert(bmap1
->ctx
, isl_space_is_equal(bmap1
->dim
, bmap2
->dim
), goto error
);
8140 map
= isl_map_alloc_space(isl_space_copy(bmap1
->dim
), 2, 0);
8143 map
= isl_map_add_basic_map(map
, bmap1
);
8144 map
= isl_map_add_basic_map(map
, bmap2
);
8147 isl_basic_map_free(bmap1
);
8148 isl_basic_map_free(bmap2
);
8152 struct isl_set
*isl_basic_set_union(
8153 struct isl_basic_set
*bset1
, struct isl_basic_set
*bset2
)
8155 return (struct isl_set
*)isl_basic_map_union(
8156 (struct isl_basic_map
*)bset1
,
8157 (struct isl_basic_map
*)bset2
);
8160 /* Order divs such that any div only depends on previous divs */
8161 struct isl_basic_map
*isl_basic_map_order_divs(struct isl_basic_map
*bmap
)
8169 off
= isl_space_dim(bmap
->dim
, isl_dim_all
);
8171 for (i
= 0; i
< bmap
->n_div
; ++i
) {
8173 if (isl_int_is_zero(bmap
->div
[i
][0]))
8175 pos
= isl_seq_first_non_zero(bmap
->div
[i
]+1+1+off
+i
,
8179 isl_basic_map_swap_div(bmap
, i
, i
+ pos
);
8185 struct isl_basic_set
*isl_basic_set_order_divs(struct isl_basic_set
*bset
)
8187 return (struct isl_basic_set
*)
8188 isl_basic_map_order_divs((struct isl_basic_map
*)bset
);
8191 __isl_give isl_map
*isl_map_order_divs(__isl_take isl_map
*map
)
8198 for (i
= 0; i
< map
->n
; ++i
) {
8199 map
->p
[i
] = isl_basic_map_order_divs(map
->p
[i
]);
8210 /* Apply the expansion computed by isl_merge_divs.
8211 * The expansion itself is given by "exp" while the resulting
8212 * list of divs is given by "div".
8214 __isl_give isl_basic_set
*isl_basic_set_expand_divs(
8215 __isl_take isl_basic_set
*bset
, __isl_take isl_mat
*div
, int *exp
)
8220 bset
= isl_basic_set_cow(bset
);
8224 if (div
->n_row
< bset
->n_div
)
8225 isl_die(isl_mat_get_ctx(div
), isl_error_invalid
,
8226 "not an expansion", goto error
);
8228 n_div
= bset
->n_div
;
8229 bset
= isl_basic_map_extend_space(bset
, isl_space_copy(bset
->dim
),
8230 div
->n_row
- n_div
, 0,
8231 2 * (div
->n_row
- n_div
));
8233 for (i
= n_div
; i
< div
->n_row
; ++i
)
8234 if (isl_basic_set_alloc_div(bset
) < 0)
8238 for (i
= div
->n_row
- 1; i
>= 0; --i
) {
8239 if (j
>= 0 && exp
[j
] == i
) {
8241 isl_basic_map_swap_div(bset
, i
, j
);
8244 isl_seq_cpy(bset
->div
[i
], div
->row
[i
], div
->n_col
);
8245 if (isl_basic_map_add_div_constraints(bset
, i
) < 0)
8253 isl_basic_set_free(bset
);
8258 /* Look for a div in dst that corresponds to the div "div" in src.
8259 * The divs before "div" in src and dst are assumed to be the same.
8261 * Returns -1 if no corresponding div was found and the position
8262 * of the corresponding div in dst otherwise.
8264 static int find_div(struct isl_basic_map
*dst
,
8265 struct isl_basic_map
*src
, unsigned div
)
8269 unsigned total
= isl_space_dim(src
->dim
, isl_dim_all
);
8271 isl_assert(dst
->ctx
, div
<= dst
->n_div
, return -1);
8272 for (i
= div
; i
< dst
->n_div
; ++i
)
8273 if (isl_seq_eq(dst
->div
[i
], src
->div
[div
], 1+1+total
+div
) &&
8274 isl_seq_first_non_zero(dst
->div
[i
]+1+1+total
+div
,
8275 dst
->n_div
- div
) == -1)
8280 /* Align the divs of "dst" to those of "src", adding divs from "src"
8281 * if needed. That is, make sure that the first src->n_div divs
8282 * of the result are equal to those of src.
8284 * The result is not finalized as by design it will have redundant
8285 * divs if any divs from "src" were copied.
8287 __isl_give isl_basic_map
*isl_basic_map_align_divs(
8288 __isl_take isl_basic_map
*dst
, __isl_keep isl_basic_map
*src
)
8291 int known
, extended
;
8295 return isl_basic_map_free(dst
);
8297 if (src
->n_div
== 0)
8300 known
= isl_basic_map_divs_known(src
);
8302 return isl_basic_map_free(dst
);
8304 isl_die(isl_basic_map_get_ctx(src
), isl_error_invalid
,
8305 "some src divs are unknown",
8306 return isl_basic_map_free(dst
));
8308 src
= isl_basic_map_order_divs(src
);
8311 total
= isl_space_dim(src
->dim
, isl_dim_all
);
8312 for (i
= 0; i
< src
->n_div
; ++i
) {
8313 int j
= find_div(dst
, src
, i
);
8316 int extra
= src
->n_div
- i
;
8317 dst
= isl_basic_map_cow(dst
);
8320 dst
= isl_basic_map_extend_space(dst
,
8321 isl_space_copy(dst
->dim
),
8322 extra
, 0, 2 * extra
);
8325 j
= isl_basic_map_alloc_div(dst
);
8327 return isl_basic_map_free(dst
);
8328 isl_seq_cpy(dst
->div
[j
], src
->div
[i
], 1+1+total
+i
);
8329 isl_seq_clr(dst
->div
[j
]+1+1+total
+i
, dst
->n_div
- i
);
8330 if (isl_basic_map_add_div_constraints(dst
, j
) < 0)
8331 return isl_basic_map_free(dst
);
8334 isl_basic_map_swap_div(dst
, i
, j
);
8339 struct isl_basic_set
*isl_basic_set_align_divs(
8340 struct isl_basic_set
*dst
, struct isl_basic_set
*src
)
8342 return (struct isl_basic_set
*)isl_basic_map_align_divs(
8343 (struct isl_basic_map
*)dst
, (struct isl_basic_map
*)src
);
8346 struct isl_map
*isl_map_align_divs(struct isl_map
*map
)
8354 map
= isl_map_compute_divs(map
);
8355 map
= isl_map_cow(map
);
8359 for (i
= 1; i
< map
->n
; ++i
)
8360 map
->p
[0] = isl_basic_map_align_divs(map
->p
[0], map
->p
[i
]);
8361 for (i
= 1; i
< map
->n
; ++i
) {
8362 map
->p
[i
] = isl_basic_map_align_divs(map
->p
[i
], map
->p
[0]);
8364 return isl_map_free(map
);
8367 ISL_F_CLR(map
, ISL_MAP_NORMALIZED
);
8371 struct isl_set
*isl_set_align_divs(struct isl_set
*set
)
8373 return (struct isl_set
*)isl_map_align_divs((struct isl_map
*)set
);
8376 /* Align the divs of the basic sets in "set" to those
8377 * of the basic sets in "list", as well as to the other basic sets in "set".
8378 * The elements in "list" are assumed to have known divs.
8380 __isl_give isl_set
*isl_set_align_divs_to_basic_set_list(
8381 __isl_take isl_set
*set
, __isl_keep isl_basic_set_list
*list
)
8385 set
= isl_set_compute_divs(set
);
8386 set
= isl_set_cow(set
);
8388 return isl_set_free(set
);
8392 n
= isl_basic_set_list_n_basic_set(list
);
8393 for (i
= 0; i
< n
; ++i
) {
8394 isl_basic_set
*bset
;
8396 bset
= isl_basic_set_list_get_basic_set(list
, i
);
8397 set
->p
[0] = isl_basic_set_align_divs(set
->p
[0], bset
);
8398 isl_basic_set_free(bset
);
8401 return isl_set_free(set
);
8403 return isl_set_align_divs(set
);
8406 /* Align the divs of each element of "list" to those of "bset".
8407 * Both "bset" and the elements of "list" are assumed to have known divs.
8409 __isl_give isl_basic_set_list
*isl_basic_set_list_align_divs_to_basic_set(
8410 __isl_take isl_basic_set_list
*list
, __isl_keep isl_basic_set
*bset
)
8415 return isl_basic_set_list_free(list
);
8417 n
= isl_basic_set_list_n_basic_set(list
);
8418 for (i
= 0; i
< n
; ++i
) {
8419 isl_basic_set
*bset_i
;
8421 bset_i
= isl_basic_set_list_get_basic_set(list
, i
);
8422 bset_i
= isl_basic_set_align_divs(bset_i
, bset
);
8423 list
= isl_basic_set_list_set_basic_set(list
, i
, bset_i
);
8429 static __isl_give isl_set
*set_apply( __isl_take isl_set
*set
,
8430 __isl_take isl_map
*map
)
8434 isl_assert(set
->ctx
, isl_map_compatible_domain(map
, set
), goto error
);
8435 map
= isl_map_intersect_domain(map
, set
);
8436 set
= isl_map_range(map
);
8444 __isl_give isl_set
*isl_set_apply( __isl_take isl_set
*set
,
8445 __isl_take isl_map
*map
)
8447 return isl_map_align_params_map_map_and(set
, map
, &set_apply
);
8450 /* There is no need to cow as removing empty parts doesn't change
8451 * the meaning of the set.
8453 struct isl_map
*isl_map_remove_empty_parts(struct isl_map
*map
)
8460 for (i
= map
->n
- 1; i
>= 0; --i
)
8461 remove_if_empty(map
, i
);
8466 struct isl_set
*isl_set_remove_empty_parts(struct isl_set
*set
)
8468 return (struct isl_set
*)
8469 isl_map_remove_empty_parts((struct isl_map
*)set
);
8472 struct isl_basic_map
*isl_map_copy_basic_map(struct isl_map
*map
)
8474 struct isl_basic_map
*bmap
;
8475 if (!map
|| map
->n
== 0)
8477 bmap
= map
->p
[map
->n
-1];
8478 isl_assert(map
->ctx
, ISL_F_ISSET(bmap
, ISL_BASIC_SET_FINAL
), return NULL
);
8479 return isl_basic_map_copy(bmap
);
8482 struct isl_basic_set
*isl_set_copy_basic_set(struct isl_set
*set
)
8484 return (struct isl_basic_set
*)
8485 isl_map_copy_basic_map((struct isl_map
*)set
);
8488 __isl_give isl_map
*isl_map_drop_basic_map(__isl_take isl_map
*map
,
8489 __isl_keep isl_basic_map
*bmap
)
8495 for (i
= map
->n
-1; i
>= 0; --i
) {
8496 if (map
->p
[i
] != bmap
)
8498 map
= isl_map_cow(map
);
8501 isl_basic_map_free(map
->p
[i
]);
8502 if (i
!= map
->n
-1) {
8503 ISL_F_CLR(map
, ISL_SET_NORMALIZED
);
8504 map
->p
[i
] = map
->p
[map
->n
-1];
8515 struct isl_set
*isl_set_drop_basic_set(struct isl_set
*set
,
8516 struct isl_basic_set
*bset
)
8518 return (struct isl_set
*)isl_map_drop_basic_map((struct isl_map
*)set
,
8519 (struct isl_basic_map
*)bset
);
8522 /* Given two basic sets bset1 and bset2, compute the maximal difference
8523 * between the values of dimension pos in bset1 and those in bset2
8524 * for any common value of the parameters and dimensions preceding pos.
8526 static enum isl_lp_result
basic_set_maximal_difference_at(
8527 __isl_keep isl_basic_set
*bset1
, __isl_keep isl_basic_set
*bset2
,
8528 int pos
, isl_int
*opt
)
8531 struct isl_basic_map
*bmap1
= NULL
;
8532 struct isl_basic_map
*bmap2
= NULL
;
8533 struct isl_ctx
*ctx
;
8534 struct isl_vec
*obj
;
8537 unsigned dim1
, dim2
;
8538 enum isl_lp_result res
;
8540 if (!bset1
|| !bset2
)
8541 return isl_lp_error
;
8543 nparam
= isl_basic_set_n_param(bset1
);
8544 dim1
= isl_basic_set_n_dim(bset1
);
8545 dim2
= isl_basic_set_n_dim(bset2
);
8546 dims
= isl_space_alloc(bset1
->ctx
, nparam
, pos
, dim1
- pos
);
8547 bmap1
= isl_basic_map_from_basic_set(isl_basic_set_copy(bset1
), dims
);
8548 dims
= isl_space_alloc(bset2
->ctx
, nparam
, pos
, dim2
- pos
);
8549 bmap2
= isl_basic_map_from_basic_set(isl_basic_set_copy(bset2
), dims
);
8550 if (!bmap1
|| !bmap2
)
8552 bmap1
= isl_basic_map_cow(bmap1
);
8553 bmap1
= isl_basic_map_extend(bmap1
, nparam
,
8554 pos
, (dim1
- pos
) + (dim2
- pos
),
8555 bmap2
->n_div
, bmap2
->n_eq
, bmap2
->n_ineq
);
8556 bmap1
= add_constraints(bmap1
, bmap2
, 0, dim1
- pos
);
8559 total
= isl_basic_map_total_dim(bmap1
);
8561 obj
= isl_vec_alloc(ctx
, 1 + total
);
8564 isl_seq_clr(obj
->block
.data
, 1 + total
);
8565 isl_int_set_si(obj
->block
.data
[1+nparam
+pos
], 1);
8566 isl_int_set_si(obj
->block
.data
[1+nparam
+pos
+(dim1
-pos
)], -1);
8567 res
= isl_basic_map_solve_lp(bmap1
, 1, obj
->block
.data
, ctx
->one
,
8569 isl_basic_map_free(bmap1
);
8573 isl_basic_map_free(bmap2
);
8575 isl_basic_map_free(bmap1
);
8576 return isl_lp_error
;
8579 /* Given two _disjoint_ basic sets bset1 and bset2, check whether
8580 * for any common value of the parameters and dimensions preceding pos
8581 * in both basic sets, the values of dimension pos in bset1 are
8582 * smaller or larger than those in bset2.
8585 * 1 if bset1 follows bset2
8586 * -1 if bset1 precedes bset2
8587 * 0 if bset1 and bset2 are incomparable
8588 * -2 if some error occurred.
8590 int isl_basic_set_compare_at(struct isl_basic_set
*bset1
,
8591 struct isl_basic_set
*bset2
, int pos
)
8594 enum isl_lp_result res
;
8599 res
= basic_set_maximal_difference_at(bset1
, bset2
, pos
, &opt
);
8601 if (res
== isl_lp_empty
)
8603 else if ((res
== isl_lp_ok
&& isl_int_is_pos(opt
)) ||
8604 res
== isl_lp_unbounded
)
8606 else if (res
== isl_lp_ok
&& isl_int_is_neg(opt
))
8615 /* Given two basic sets bset1 and bset2, check whether
8616 * for any common value of the parameters and dimensions preceding pos
8617 * there is a value of dimension pos in bset1 that is larger
8618 * than a value of the same dimension in bset2.
8621 * 1 if there exists such a pair
8622 * 0 if there is no such pair, but there is a pair of equal values
8624 * -2 if some error occurred.
8626 int isl_basic_set_follows_at(__isl_keep isl_basic_set
*bset1
,
8627 __isl_keep isl_basic_set
*bset2
, int pos
)
8630 enum isl_lp_result res
;
8635 res
= basic_set_maximal_difference_at(bset1
, bset2
, pos
, &opt
);
8637 if (res
== isl_lp_empty
)
8639 else if ((res
== isl_lp_ok
&& isl_int_is_pos(opt
)) ||
8640 res
== isl_lp_unbounded
)
8642 else if (res
== isl_lp_ok
&& isl_int_is_neg(opt
))
8644 else if (res
== isl_lp_ok
)
8653 /* Given two sets set1 and set2, check whether
8654 * for any common value of the parameters and dimensions preceding pos
8655 * there is a value of dimension pos in set1 that is larger
8656 * than a value of the same dimension in set2.
8659 * 1 if there exists such a pair
8660 * 0 if there is no such pair, but there is a pair of equal values
8662 * -2 if some error occurred.
8664 int isl_set_follows_at(__isl_keep isl_set
*set1
,
8665 __isl_keep isl_set
*set2
, int pos
)
8673 for (i
= 0; i
< set1
->n
; ++i
)
8674 for (j
= 0; j
< set2
->n
; ++j
) {
8676 f
= isl_basic_set_follows_at(set1
->p
[i
], set2
->p
[j
], pos
);
8677 if (f
== 1 || f
== -2)
8686 static int isl_basic_map_plain_has_fixed_var(__isl_keep isl_basic_map
*bmap
,
8687 unsigned pos
, isl_int
*val
)
8695 total
= isl_basic_map_total_dim(bmap
);
8696 for (i
= 0, d
= total
-1; i
< bmap
->n_eq
&& d
+1 > pos
; ++i
) {
8697 for (; d
+1 > pos
; --d
)
8698 if (!isl_int_is_zero(bmap
->eq
[i
][1+d
]))
8702 if (isl_seq_first_non_zero(bmap
->eq
[i
]+1, d
) != -1)
8704 if (isl_seq_first_non_zero(bmap
->eq
[i
]+1+d
+1, total
-d
-1) != -1)
8706 if (!isl_int_is_one(bmap
->eq
[i
][1+d
]))
8709 isl_int_neg(*val
, bmap
->eq
[i
][0]);
8715 static int isl_map_plain_has_fixed_var(__isl_keep isl_map
*map
,
8716 unsigned pos
, isl_int
*val
)
8728 return isl_basic_map_plain_has_fixed_var(map
->p
[0], pos
, val
);
8731 fixed
= isl_basic_map_plain_has_fixed_var(map
->p
[0], pos
, &v
);
8732 for (i
= 1; fixed
== 1 && i
< map
->n
; ++i
) {
8733 fixed
= isl_basic_map_plain_has_fixed_var(map
->p
[i
], pos
, &tmp
);
8734 if (fixed
== 1 && isl_int_ne(tmp
, v
))
8738 isl_int_set(*val
, v
);
8744 static int isl_basic_set_plain_has_fixed_var(__isl_keep isl_basic_set
*bset
,
8745 unsigned pos
, isl_int
*val
)
8747 return isl_basic_map_plain_has_fixed_var((struct isl_basic_map
*)bset
,
8751 static int isl_set_plain_has_fixed_var(__isl_keep isl_set
*set
, unsigned pos
,
8754 return isl_map_plain_has_fixed_var((struct isl_map
*)set
, pos
, val
);
8757 int isl_basic_map_plain_is_fixed(__isl_keep isl_basic_map
*bmap
,
8758 enum isl_dim_type type
, unsigned pos
, isl_int
*val
)
8760 if (pos
>= isl_basic_map_dim(bmap
, type
))
8762 return isl_basic_map_plain_has_fixed_var(bmap
,
8763 isl_basic_map_offset(bmap
, type
) - 1 + pos
, val
);
8766 /* If "bmap" obviously lies on a hyperplane where the given dimension
8767 * has a fixed value, then return that value.
8768 * Otherwise return NaN.
8770 __isl_give isl_val
*isl_basic_map_plain_get_val_if_fixed(
8771 __isl_keep isl_basic_map
*bmap
,
8772 enum isl_dim_type type
, unsigned pos
)
8780 ctx
= isl_basic_map_get_ctx(bmap
);
8781 v
= isl_val_alloc(ctx
);
8784 fixed
= isl_basic_map_plain_is_fixed(bmap
, type
, pos
, &v
->n
);
8786 return isl_val_free(v
);
8788 isl_int_set_si(v
->d
, 1);
8792 return isl_val_nan(ctx
);
8795 int isl_map_plain_is_fixed(__isl_keep isl_map
*map
,
8796 enum isl_dim_type type
, unsigned pos
, isl_int
*val
)
8798 if (pos
>= isl_map_dim(map
, type
))
8800 return isl_map_plain_has_fixed_var(map
,
8801 map_offset(map
, type
) - 1 + pos
, val
);
8804 /* If "map" obviously lies on a hyperplane where the given dimension
8805 * has a fixed value, then return that value.
8806 * Otherwise return NaN.
8808 __isl_give isl_val
*isl_map_plain_get_val_if_fixed(__isl_keep isl_map
*map
,
8809 enum isl_dim_type type
, unsigned pos
)
8817 ctx
= isl_map_get_ctx(map
);
8818 v
= isl_val_alloc(ctx
);
8821 fixed
= isl_map_plain_is_fixed(map
, type
, pos
, &v
->n
);
8823 return isl_val_free(v
);
8825 isl_int_set_si(v
->d
, 1);
8829 return isl_val_nan(ctx
);
8832 /* If "set" obviously lies on a hyperplane where the given dimension
8833 * has a fixed value, then return that value.
8834 * Otherwise return NaN.
8836 __isl_give isl_val
*isl_set_plain_get_val_if_fixed(__isl_keep isl_set
*set
,
8837 enum isl_dim_type type
, unsigned pos
)
8839 return isl_map_plain_get_val_if_fixed(set
, type
, pos
);
8842 int isl_set_plain_is_fixed(__isl_keep isl_set
*set
,
8843 enum isl_dim_type type
, unsigned pos
, isl_int
*val
)
8845 return isl_map_plain_is_fixed(set
, type
, pos
, val
);
8848 int isl_map_fast_is_fixed(__isl_keep isl_map
*map
,
8849 enum isl_dim_type type
, unsigned pos
, isl_int
*val
)
8851 return isl_map_plain_is_fixed(map
, type
, pos
, val
);
8854 /* Check if dimension dim has fixed value and if so and if val is not NULL,
8855 * then return this fixed value in *val.
8857 int isl_basic_set_plain_dim_is_fixed(__isl_keep isl_basic_set
*bset
,
8858 unsigned dim
, isl_int
*val
)
8860 return isl_basic_set_plain_has_fixed_var(bset
,
8861 isl_basic_set_n_param(bset
) + dim
, val
);
8864 /* Check if dimension dim has fixed value and if so and if val is not NULL,
8865 * then return this fixed value in *val.
8867 int isl_set_plain_dim_is_fixed(__isl_keep isl_set
*set
,
8868 unsigned dim
, isl_int
*val
)
8870 return isl_set_plain_has_fixed_var(set
, isl_set_n_param(set
) + dim
, val
);
8873 int isl_set_fast_dim_is_fixed(__isl_keep isl_set
*set
,
8874 unsigned dim
, isl_int
*val
)
8876 return isl_set_plain_dim_is_fixed(set
, dim
, val
);
8879 /* Check if input variable in has fixed value and if so and if val is not NULL,
8880 * then return this fixed value in *val.
8882 int isl_map_plain_input_is_fixed(__isl_keep isl_map
*map
,
8883 unsigned in
, isl_int
*val
)
8885 return isl_map_plain_has_fixed_var(map
, isl_map_n_param(map
) + in
, val
);
8888 /* Check if dimension dim has an (obvious) fixed lower bound and if so
8889 * and if val is not NULL, then return this lower bound in *val.
8891 int isl_basic_set_plain_dim_has_fixed_lower_bound(
8892 __isl_keep isl_basic_set
*bset
, unsigned dim
, isl_int
*val
)
8894 int i
, i_eq
= -1, i_ineq
= -1;
8901 total
= isl_basic_set_total_dim(bset
);
8902 nparam
= isl_basic_set_n_param(bset
);
8903 for (i
= 0; i
< bset
->n_eq
; ++i
) {
8904 if (isl_int_is_zero(bset
->eq
[i
][1+nparam
+dim
]))
8910 for (i
= 0; i
< bset
->n_ineq
; ++i
) {
8911 if (!isl_int_is_pos(bset
->ineq
[i
][1+nparam
+dim
]))
8913 if (i_eq
!= -1 || i_ineq
!= -1)
8917 if (i_eq
== -1 && i_ineq
== -1)
8919 c
= i_eq
!= -1 ? bset
->eq
[i_eq
] : bset
->ineq
[i_ineq
];
8920 /* The coefficient should always be one due to normalization. */
8921 if (!isl_int_is_one(c
[1+nparam
+dim
]))
8923 if (isl_seq_first_non_zero(c
+1, nparam
+dim
) != -1)
8925 if (isl_seq_first_non_zero(c
+1+nparam
+dim
+1,
8926 total
- nparam
- dim
- 1) != -1)
8929 isl_int_neg(*val
, c
[0]);
8933 int isl_set_plain_dim_has_fixed_lower_bound(__isl_keep isl_set
*set
,
8934 unsigned dim
, isl_int
*val
)
8946 return isl_basic_set_plain_dim_has_fixed_lower_bound(set
->p
[0],
8950 fixed
= isl_basic_set_plain_dim_has_fixed_lower_bound(set
->p
[0],
8952 for (i
= 1; fixed
== 1 && i
< set
->n
; ++i
) {
8953 fixed
= isl_basic_set_plain_dim_has_fixed_lower_bound(set
->p
[i
],
8955 if (fixed
== 1 && isl_int_ne(tmp
, v
))
8959 isl_int_set(*val
, v
);
8965 /* uset_gist depends on constraints without existentially quantified
8966 * variables sorting first.
8968 static int sort_constraint_cmp(const void *p1
, const void *p2
, void *arg
)
8970 isl_int
**c1
= (isl_int
**) p1
;
8971 isl_int
**c2
= (isl_int
**) p2
;
8973 unsigned size
= *(unsigned *) arg
;
8975 l1
= isl_seq_last_non_zero(*c1
+ 1, size
);
8976 l2
= isl_seq_last_non_zero(*c2
+ 1, size
);
8981 return isl_seq_cmp(*c1
+ 1, *c2
+ 1, size
);
8984 static struct isl_basic_map
*isl_basic_map_sort_constraints(
8985 struct isl_basic_map
*bmap
)
8991 if (bmap
->n_ineq
== 0)
8993 if (ISL_F_ISSET(bmap
, ISL_BASIC_MAP_NORMALIZED
))
8995 total
= isl_basic_map_total_dim(bmap
);
8996 if (isl_sort(bmap
->ineq
, bmap
->n_ineq
, sizeof(isl_int
*),
8997 &sort_constraint_cmp
, &total
) < 0)
8998 return isl_basic_map_free(bmap
);
9002 __isl_give isl_basic_set
*isl_basic_set_sort_constraints(
9003 __isl_take isl_basic_set
*bset
)
9005 return (struct isl_basic_set
*)isl_basic_map_sort_constraints(
9006 (struct isl_basic_map
*)bset
);
9009 struct isl_basic_map
*isl_basic_map_normalize(struct isl_basic_map
*bmap
)
9013 if (ISL_F_ISSET(bmap
, ISL_BASIC_MAP_NORMALIZED
))
9015 bmap
= isl_basic_map_remove_redundancies(bmap
);
9016 bmap
= isl_basic_map_sort_constraints(bmap
);
9018 ISL_F_SET(bmap
, ISL_BASIC_MAP_NORMALIZED
);
9022 struct isl_basic_set
*isl_basic_set_normalize(struct isl_basic_set
*bset
)
9024 return (struct isl_basic_set
*)isl_basic_map_normalize(
9025 (struct isl_basic_map
*)bset
);
9028 int isl_basic_map_plain_cmp(const __isl_keep isl_basic_map
*bmap1
,
9029 const __isl_keep isl_basic_map
*bmap2
)
9036 if (ISL_F_ISSET(bmap1
, ISL_BASIC_MAP_RATIONAL
) !=
9037 ISL_F_ISSET(bmap2
, ISL_BASIC_MAP_RATIONAL
))
9038 return ISL_F_ISSET(bmap1
, ISL_BASIC_MAP_RATIONAL
) ? -1 : 1;
9039 if (isl_basic_map_n_param(bmap1
) != isl_basic_map_n_param(bmap2
))
9040 return isl_basic_map_n_param(bmap1
) - isl_basic_map_n_param(bmap2
);
9041 if (isl_basic_map_n_in(bmap1
) != isl_basic_map_n_in(bmap2
))
9042 return isl_basic_map_n_out(bmap1
) - isl_basic_map_n_out(bmap2
);
9043 if (isl_basic_map_n_out(bmap1
) != isl_basic_map_n_out(bmap2
))
9044 return isl_basic_map_n_out(bmap1
) - isl_basic_map_n_out(bmap2
);
9045 if (ISL_F_ISSET(bmap1
, ISL_BASIC_MAP_EMPTY
) &&
9046 ISL_F_ISSET(bmap2
, ISL_BASIC_MAP_EMPTY
))
9048 if (ISL_F_ISSET(bmap1
, ISL_BASIC_MAP_EMPTY
))
9050 if (ISL_F_ISSET(bmap2
, ISL_BASIC_MAP_EMPTY
))
9052 if (bmap1
->n_eq
!= bmap2
->n_eq
)
9053 return bmap1
->n_eq
- bmap2
->n_eq
;
9054 if (bmap1
->n_ineq
!= bmap2
->n_ineq
)
9055 return bmap1
->n_ineq
- bmap2
->n_ineq
;
9056 if (bmap1
->n_div
!= bmap2
->n_div
)
9057 return bmap1
->n_div
- bmap2
->n_div
;
9058 total
= isl_basic_map_total_dim(bmap1
);
9059 for (i
= 0; i
< bmap1
->n_eq
; ++i
) {
9060 cmp
= isl_seq_cmp(bmap1
->eq
[i
], bmap2
->eq
[i
], 1+total
);
9064 for (i
= 0; i
< bmap1
->n_ineq
; ++i
) {
9065 cmp
= isl_seq_cmp(bmap1
->ineq
[i
], bmap2
->ineq
[i
], 1+total
);
9069 for (i
= 0; i
< bmap1
->n_div
; ++i
) {
9070 cmp
= isl_seq_cmp(bmap1
->div
[i
], bmap2
->div
[i
], 1+1+total
);
9077 int isl_basic_set_plain_cmp(const __isl_keep isl_basic_set
*bset1
,
9078 const __isl_keep isl_basic_set
*bset2
)
9080 return isl_basic_map_plain_cmp(bset1
, bset2
);
9083 int isl_set_plain_cmp(__isl_keep isl_set
*set1
, __isl_keep isl_set
*set2
)
9089 if (set1
->n
!= set2
->n
)
9090 return set1
->n
- set2
->n
;
9092 for (i
= 0; i
< set1
->n
; ++i
) {
9093 cmp
= isl_basic_set_plain_cmp(set1
->p
[i
], set2
->p
[i
]);
9101 int isl_basic_map_plain_is_equal(__isl_keep isl_basic_map
*bmap1
,
9102 __isl_keep isl_basic_map
*bmap2
)
9104 return isl_basic_map_plain_cmp(bmap1
, bmap2
) == 0;
9107 int isl_basic_set_plain_is_equal(__isl_keep isl_basic_set
*bset1
,
9108 __isl_keep isl_basic_set
*bset2
)
9110 return isl_basic_map_plain_is_equal((isl_basic_map
*)bset1
,
9111 (isl_basic_map
*)bset2
);
9114 static int qsort_bmap_cmp(const void *p1
, const void *p2
)
9116 const struct isl_basic_map
*bmap1
= *(const struct isl_basic_map
**)p1
;
9117 const struct isl_basic_map
*bmap2
= *(const struct isl_basic_map
**)p2
;
9119 return isl_basic_map_plain_cmp(bmap1
, bmap2
);
9122 /* Sort the basic maps of "map" and remove duplicate basic maps.
9124 * While removing basic maps, we make sure that the basic maps remain
9125 * sorted because isl_map_normalize expects the basic maps of the result
9128 static __isl_give isl_map
*sort_and_remove_duplicates(__isl_take isl_map
*map
)
9132 map
= isl_map_remove_empty_parts(map
);
9135 qsort(map
->p
, map
->n
, sizeof(struct isl_basic_map
*), qsort_bmap_cmp
);
9136 for (i
= map
->n
- 1; i
>= 1; --i
) {
9137 if (!isl_basic_map_plain_is_equal(map
->p
[i
- 1], map
->p
[i
]))
9139 isl_basic_map_free(map
->p
[i
-1]);
9140 for (j
= i
; j
< map
->n
; ++j
)
9141 map
->p
[j
- 1] = map
->p
[j
];
9148 /* Remove obvious duplicates among the basic maps of "map".
9150 * Unlike isl_map_normalize, this function does not remove redundant
9151 * constraints and only removes duplicates that have exactly the same
9152 * constraints in the input. It does sort the constraints and
9153 * the basic maps to ease the detection of duplicates.
9155 * If "map" has already been normalized or if the basic maps are
9156 * disjoint, then there can be no duplicates.
9158 __isl_give isl_map
*isl_map_remove_obvious_duplicates(__isl_take isl_map
*map
)
9161 isl_basic_map
*bmap
;
9167 if (ISL_F_ISSET(map
, ISL_MAP_NORMALIZED
| ISL_MAP_DISJOINT
))
9169 for (i
= 0; i
< map
->n
; ++i
) {
9170 bmap
= isl_basic_map_copy(map
->p
[i
]);
9171 bmap
= isl_basic_map_sort_constraints(bmap
);
9173 return isl_map_free(map
);
9174 isl_basic_map_free(map
->p
[i
]);
9178 map
= sort_and_remove_duplicates(map
);
9182 /* We normalize in place, but if anything goes wrong we need
9183 * to return NULL, so we need to make sure we don't change the
9184 * meaning of any possible other copies of map.
9186 __isl_give isl_map
*isl_map_normalize(__isl_take isl_map
*map
)
9189 struct isl_basic_map
*bmap
;
9193 if (ISL_F_ISSET(map
, ISL_MAP_NORMALIZED
))
9195 for (i
= 0; i
< map
->n
; ++i
) {
9196 bmap
= isl_basic_map_normalize(isl_basic_map_copy(map
->p
[i
]));
9199 isl_basic_map_free(map
->p
[i
]);
9203 map
= sort_and_remove_duplicates(map
);
9205 ISL_F_SET(map
, ISL_MAP_NORMALIZED
);
9212 struct isl_set
*isl_set_normalize(struct isl_set
*set
)
9214 return (struct isl_set
*)isl_map_normalize((struct isl_map
*)set
);
9217 int isl_map_plain_is_equal(__isl_keep isl_map
*map1
, __isl_keep isl_map
*map2
)
9227 if (!isl_space_is_equal(map1
->dim
, map2
->dim
))
9230 map1
= isl_map_copy(map1
);
9231 map2
= isl_map_copy(map2
);
9232 map1
= isl_map_normalize(map1
);
9233 map2
= isl_map_normalize(map2
);
9236 equal
= map1
->n
== map2
->n
;
9237 for (i
= 0; equal
&& i
< map1
->n
; ++i
) {
9238 equal
= isl_basic_map_plain_is_equal(map1
->p
[i
], map2
->p
[i
]);
9251 int isl_map_fast_is_equal(__isl_keep isl_map
*map1
, __isl_keep isl_map
*map2
)
9253 return isl_map_plain_is_equal(map1
, map2
);
9256 int isl_set_plain_is_equal(__isl_keep isl_set
*set1
, __isl_keep isl_set
*set2
)
9258 return isl_map_plain_is_equal((struct isl_map
*)set1
,
9259 (struct isl_map
*)set2
);
9262 int isl_set_fast_is_equal(__isl_keep isl_set
*set1
, __isl_keep isl_set
*set2
)
9264 return isl_set_plain_is_equal(set1
, set2
);
9267 /* Return an interval that ranges from min to max (inclusive)
9269 struct isl_basic_set
*isl_basic_set_interval(struct isl_ctx
*ctx
,
9270 isl_int min
, isl_int max
)
9273 struct isl_basic_set
*bset
= NULL
;
9275 bset
= isl_basic_set_alloc(ctx
, 0, 1, 0, 0, 2);
9279 k
= isl_basic_set_alloc_inequality(bset
);
9282 isl_int_set_si(bset
->ineq
[k
][1], 1);
9283 isl_int_neg(bset
->ineq
[k
][0], min
);
9285 k
= isl_basic_set_alloc_inequality(bset
);
9288 isl_int_set_si(bset
->ineq
[k
][1], -1);
9289 isl_int_set(bset
->ineq
[k
][0], max
);
9293 isl_basic_set_free(bset
);
9297 /* Return the basic sets in "set" as a list.
9299 __isl_give isl_basic_set_list
*isl_set_get_basic_set_list(
9300 __isl_keep isl_set
*set
)
9304 isl_basic_set_list
*list
;
9308 ctx
= isl_set_get_ctx(set
);
9309 list
= isl_basic_set_list_alloc(ctx
, set
->n
);
9311 for (i
= 0; i
< set
->n
; ++i
) {
9312 isl_basic_set
*bset
;
9314 bset
= isl_basic_set_copy(set
->p
[i
]);
9315 list
= isl_basic_set_list_add(list
, bset
);
9321 /* Return the intersection of the elements in the non-empty list "list".
9322 * All elements are assumed to live in the same space.
9324 __isl_give isl_basic_set
*isl_basic_set_list_intersect(
9325 __isl_take
struct isl_basic_set_list
*list
)
9328 isl_basic_set
*bset
;
9332 n
= isl_basic_set_list_n_basic_set(list
);
9334 isl_die(isl_basic_set_list_get_ctx(list
), isl_error_invalid
,
9335 "expecting non-empty list", goto error
);
9337 bset
= isl_basic_set_list_get_basic_set(list
, 0);
9338 for (i
= 1; i
< n
; ++i
) {
9339 isl_basic_set
*bset_i
;
9341 bset_i
= isl_basic_set_list_get_basic_set(list
, i
);
9342 bset
= isl_basic_set_intersect(bset
, bset_i
);
9345 isl_basic_set_list_free(list
);
9348 isl_basic_set_list_free(list
);
9352 /* Return the Cartesian product of the basic sets in list (in the given order).
9354 __isl_give isl_basic_set
*isl_basic_set_list_product(
9355 __isl_take
struct isl_basic_set_list
*list
)
9363 struct isl_basic_set
*product
= NULL
;
9367 isl_assert(list
->ctx
, list
->n
> 0, goto error
);
9368 isl_assert(list
->ctx
, list
->p
[0], goto error
);
9369 nparam
= isl_basic_set_n_param(list
->p
[0]);
9370 dim
= isl_basic_set_n_dim(list
->p
[0]);
9371 extra
= list
->p
[0]->n_div
;
9372 n_eq
= list
->p
[0]->n_eq
;
9373 n_ineq
= list
->p
[0]->n_ineq
;
9374 for (i
= 1; i
< list
->n
; ++i
) {
9375 isl_assert(list
->ctx
, list
->p
[i
], goto error
);
9376 isl_assert(list
->ctx
,
9377 nparam
== isl_basic_set_n_param(list
->p
[i
]), goto error
);
9378 dim
+= isl_basic_set_n_dim(list
->p
[i
]);
9379 extra
+= list
->p
[i
]->n_div
;
9380 n_eq
+= list
->p
[i
]->n_eq
;
9381 n_ineq
+= list
->p
[i
]->n_ineq
;
9383 product
= isl_basic_set_alloc(list
->ctx
, nparam
, dim
, extra
,
9388 for (i
= 0; i
< list
->n
; ++i
) {
9389 isl_basic_set_add_constraints(product
,
9390 isl_basic_set_copy(list
->p
[i
]), dim
);
9391 dim
+= isl_basic_set_n_dim(list
->p
[i
]);
9393 isl_basic_set_list_free(list
);
9396 isl_basic_set_free(product
);
9397 isl_basic_set_list_free(list
);
9401 struct isl_basic_map
*isl_basic_map_product(
9402 struct isl_basic_map
*bmap1
, struct isl_basic_map
*bmap2
)
9404 isl_space
*dim_result
= NULL
;
9405 struct isl_basic_map
*bmap
;
9406 unsigned in1
, in2
, out1
, out2
, nparam
, total
, pos
;
9407 struct isl_dim_map
*dim_map1
, *dim_map2
;
9409 if (!bmap1
|| !bmap2
)
9412 isl_assert(bmap1
->ctx
, isl_space_match(bmap1
->dim
, isl_dim_param
,
9413 bmap2
->dim
, isl_dim_param
), goto error
);
9414 dim_result
= isl_space_product(isl_space_copy(bmap1
->dim
),
9415 isl_space_copy(bmap2
->dim
));
9417 in1
= isl_basic_map_n_in(bmap1
);
9418 in2
= isl_basic_map_n_in(bmap2
);
9419 out1
= isl_basic_map_n_out(bmap1
);
9420 out2
= isl_basic_map_n_out(bmap2
);
9421 nparam
= isl_basic_map_n_param(bmap1
);
9423 total
= nparam
+ in1
+ in2
+ out1
+ out2
+ bmap1
->n_div
+ bmap2
->n_div
;
9424 dim_map1
= isl_dim_map_alloc(bmap1
->ctx
, total
);
9425 dim_map2
= isl_dim_map_alloc(bmap1
->ctx
, total
);
9426 isl_dim_map_dim(dim_map1
, bmap1
->dim
, isl_dim_param
, pos
= 0);
9427 isl_dim_map_dim(dim_map2
, bmap2
->dim
, isl_dim_param
, pos
= 0);
9428 isl_dim_map_dim(dim_map1
, bmap1
->dim
, isl_dim_in
, pos
+= nparam
);
9429 isl_dim_map_dim(dim_map2
, bmap2
->dim
, isl_dim_in
, pos
+= in1
);
9430 isl_dim_map_dim(dim_map1
, bmap1
->dim
, isl_dim_out
, pos
+= in2
);
9431 isl_dim_map_dim(dim_map2
, bmap2
->dim
, isl_dim_out
, pos
+= out1
);
9432 isl_dim_map_div(dim_map1
, bmap1
, pos
+= out2
);
9433 isl_dim_map_div(dim_map2
, bmap2
, pos
+= bmap1
->n_div
);
9435 bmap
= isl_basic_map_alloc_space(dim_result
,
9436 bmap1
->n_div
+ bmap2
->n_div
,
9437 bmap1
->n_eq
+ bmap2
->n_eq
,
9438 bmap1
->n_ineq
+ bmap2
->n_ineq
);
9439 bmap
= isl_basic_map_add_constraints_dim_map(bmap
, bmap1
, dim_map1
);
9440 bmap
= isl_basic_map_add_constraints_dim_map(bmap
, bmap2
, dim_map2
);
9441 bmap
= isl_basic_map_simplify(bmap
);
9442 return isl_basic_map_finalize(bmap
);
9444 isl_basic_map_free(bmap1
);
9445 isl_basic_map_free(bmap2
);
9449 __isl_give isl_basic_map
*isl_basic_map_flat_product(
9450 __isl_take isl_basic_map
*bmap1
, __isl_take isl_basic_map
*bmap2
)
9452 isl_basic_map
*prod
;
9454 prod
= isl_basic_map_product(bmap1
, bmap2
);
9455 prod
= isl_basic_map_flatten(prod
);
9459 __isl_give isl_basic_set
*isl_basic_set_flat_product(
9460 __isl_take isl_basic_set
*bset1
, __isl_take isl_basic_set
*bset2
)
9462 return isl_basic_map_flat_range_product(bset1
, bset2
);
9465 __isl_give isl_basic_map
*isl_basic_map_domain_product(
9466 __isl_take isl_basic_map
*bmap1
, __isl_take isl_basic_map
*bmap2
)
9468 isl_space
*space_result
= NULL
;
9469 isl_basic_map
*bmap
;
9470 unsigned in1
, in2
, out
, nparam
, total
, pos
;
9471 struct isl_dim_map
*dim_map1
, *dim_map2
;
9473 if (!bmap1
|| !bmap2
)
9476 space_result
= isl_space_domain_product(isl_space_copy(bmap1
->dim
),
9477 isl_space_copy(bmap2
->dim
));
9479 in1
= isl_basic_map_dim(bmap1
, isl_dim_in
);
9480 in2
= isl_basic_map_dim(bmap2
, isl_dim_in
);
9481 out
= isl_basic_map_dim(bmap1
, isl_dim_out
);
9482 nparam
= isl_basic_map_dim(bmap1
, isl_dim_param
);
9484 total
= nparam
+ in1
+ in2
+ out
+ bmap1
->n_div
+ bmap2
->n_div
;
9485 dim_map1
= isl_dim_map_alloc(bmap1
->ctx
, total
);
9486 dim_map2
= isl_dim_map_alloc(bmap1
->ctx
, total
);
9487 isl_dim_map_dim(dim_map1
, bmap1
->dim
, isl_dim_param
, pos
= 0);
9488 isl_dim_map_dim(dim_map2
, bmap2
->dim
, isl_dim_param
, pos
= 0);
9489 isl_dim_map_dim(dim_map1
, bmap1
->dim
, isl_dim_in
, pos
+= nparam
);
9490 isl_dim_map_dim(dim_map2
, bmap2
->dim
, isl_dim_in
, pos
+= in1
);
9491 isl_dim_map_dim(dim_map1
, bmap1
->dim
, isl_dim_out
, pos
+= in2
);
9492 isl_dim_map_dim(dim_map2
, bmap2
->dim
, isl_dim_out
, pos
);
9493 isl_dim_map_div(dim_map1
, bmap1
, pos
+= out
);
9494 isl_dim_map_div(dim_map2
, bmap2
, pos
+= bmap1
->n_div
);
9496 bmap
= isl_basic_map_alloc_space(space_result
,
9497 bmap1
->n_div
+ bmap2
->n_div
,
9498 bmap1
->n_eq
+ bmap2
->n_eq
,
9499 bmap1
->n_ineq
+ bmap2
->n_ineq
);
9500 bmap
= isl_basic_map_add_constraints_dim_map(bmap
, bmap1
, dim_map1
);
9501 bmap
= isl_basic_map_add_constraints_dim_map(bmap
, bmap2
, dim_map2
);
9502 bmap
= isl_basic_map_simplify(bmap
);
9503 return isl_basic_map_finalize(bmap
);
9505 isl_basic_map_free(bmap1
);
9506 isl_basic_map_free(bmap2
);
9510 __isl_give isl_basic_map
*isl_basic_map_range_product(
9511 __isl_take isl_basic_map
*bmap1
, __isl_take isl_basic_map
*bmap2
)
9513 isl_space
*dim_result
= NULL
;
9514 isl_basic_map
*bmap
;
9515 unsigned in
, out1
, out2
, nparam
, total
, pos
;
9516 struct isl_dim_map
*dim_map1
, *dim_map2
;
9518 if (!bmap1
|| !bmap2
)
9521 if (!isl_space_match(bmap1
->dim
, isl_dim_param
,
9522 bmap2
->dim
, isl_dim_param
))
9523 isl_die(isl_basic_map_get_ctx(bmap1
), isl_error_invalid
,
9524 "parameters don't match", goto error
);
9526 dim_result
= isl_space_range_product(isl_space_copy(bmap1
->dim
),
9527 isl_space_copy(bmap2
->dim
));
9529 in
= isl_basic_map_dim(bmap1
, isl_dim_in
);
9530 out1
= isl_basic_map_n_out(bmap1
);
9531 out2
= isl_basic_map_n_out(bmap2
);
9532 nparam
= isl_basic_map_n_param(bmap1
);
9534 total
= nparam
+ in
+ out1
+ out2
+ bmap1
->n_div
+ bmap2
->n_div
;
9535 dim_map1
= isl_dim_map_alloc(bmap1
->ctx
, total
);
9536 dim_map2
= isl_dim_map_alloc(bmap1
->ctx
, total
);
9537 isl_dim_map_dim(dim_map1
, bmap1
->dim
, isl_dim_param
, pos
= 0);
9538 isl_dim_map_dim(dim_map2
, bmap2
->dim
, isl_dim_param
, pos
= 0);
9539 isl_dim_map_dim(dim_map1
, bmap1
->dim
, isl_dim_in
, pos
+= nparam
);
9540 isl_dim_map_dim(dim_map2
, bmap2
->dim
, isl_dim_in
, pos
);
9541 isl_dim_map_dim(dim_map1
, bmap1
->dim
, isl_dim_out
, pos
+= in
);
9542 isl_dim_map_dim(dim_map2
, bmap2
->dim
, isl_dim_out
, pos
+= out1
);
9543 isl_dim_map_div(dim_map1
, bmap1
, pos
+= out2
);
9544 isl_dim_map_div(dim_map2
, bmap2
, pos
+= bmap1
->n_div
);
9546 bmap
= isl_basic_map_alloc_space(dim_result
,
9547 bmap1
->n_div
+ bmap2
->n_div
,
9548 bmap1
->n_eq
+ bmap2
->n_eq
,
9549 bmap1
->n_ineq
+ bmap2
->n_ineq
);
9550 bmap
= isl_basic_map_add_constraints_dim_map(bmap
, bmap1
, dim_map1
);
9551 bmap
= isl_basic_map_add_constraints_dim_map(bmap
, bmap2
, dim_map2
);
9552 bmap
= isl_basic_map_simplify(bmap
);
9553 return isl_basic_map_finalize(bmap
);
9555 isl_basic_map_free(bmap1
);
9556 isl_basic_map_free(bmap2
);
9560 __isl_give isl_basic_map
*isl_basic_map_flat_range_product(
9561 __isl_take isl_basic_map
*bmap1
, __isl_take isl_basic_map
*bmap2
)
9563 isl_basic_map
*prod
;
9565 prod
= isl_basic_map_range_product(bmap1
, bmap2
);
9566 prod
= isl_basic_map_flatten_range(prod
);
9570 /* Apply "basic_map_product" to each pair of basic maps in "map1" and "map2"
9571 * and collect the results.
9572 * The result live in the space obtained by calling "space_product"
9573 * on the spaces of "map1" and "map2".
9574 * If "remove_duplicates" is set then the result may contain duplicates
9575 * (even if the inputs do not) and so we try and remove the obvious
9578 static __isl_give isl_map
*map_product(__isl_take isl_map
*map1
,
9579 __isl_take isl_map
*map2
,
9580 __isl_give isl_space
*(*space_product
)(__isl_take isl_space
*left
,
9581 __isl_take isl_space
*right
),
9582 __isl_give isl_basic_map
*(*basic_map_product
)(
9583 __isl_take isl_basic_map
*left
,
9584 __isl_take isl_basic_map
*right
),
9585 int remove_duplicates
)
9588 struct isl_map
*result
;
9594 isl_assert(map1
->ctx
, isl_space_match(map1
->dim
, isl_dim_param
,
9595 map2
->dim
, isl_dim_param
), goto error
);
9597 if (ISL_F_ISSET(map1
, ISL_MAP_DISJOINT
) &&
9598 ISL_F_ISSET(map2
, ISL_MAP_DISJOINT
))
9599 ISL_FL_SET(flags
, ISL_MAP_DISJOINT
);
9601 result
= isl_map_alloc_space(space_product(isl_space_copy(map1
->dim
),
9602 isl_space_copy(map2
->dim
)),
9603 map1
->n
* map2
->n
, flags
);
9606 for (i
= 0; i
< map1
->n
; ++i
)
9607 for (j
= 0; j
< map2
->n
; ++j
) {
9608 struct isl_basic_map
*part
;
9609 part
= basic_map_product(isl_basic_map_copy(map1
->p
[i
]),
9610 isl_basic_map_copy(map2
->p
[j
]));
9611 if (isl_basic_map_is_empty(part
))
9612 isl_basic_map_free(part
);
9614 result
= isl_map_add_basic_map(result
, part
);
9618 if (remove_duplicates
)
9619 result
= isl_map_remove_obvious_duplicates(result
);
9629 /* Given two maps A -> B and C -> D, construct a map [A -> C] -> [B -> D]
9631 static __isl_give isl_map
*map_product_aligned(__isl_take isl_map
*map1
,
9632 __isl_take isl_map
*map2
)
9634 return map_product(map1
, map2
, &isl_space_product
,
9635 &isl_basic_map_product
, 0);
9638 __isl_give isl_map
*isl_map_product(__isl_take isl_map
*map1
,
9639 __isl_take isl_map
*map2
)
9641 return isl_map_align_params_map_map_and(map1
, map2
, &map_product_aligned
);
9644 /* Given two maps A -> B and C -> D, construct a map (A, C) -> (B, D)
9646 __isl_give isl_map
*isl_map_flat_product(__isl_take isl_map
*map1
,
9647 __isl_take isl_map
*map2
)
9651 prod
= isl_map_product(map1
, map2
);
9652 prod
= isl_map_flatten(prod
);
9656 /* Given two set A and B, construct its Cartesian product A x B.
9658 struct isl_set
*isl_set_product(struct isl_set
*set1
, struct isl_set
*set2
)
9660 return isl_map_range_product(set1
, set2
);
9663 __isl_give isl_set
*isl_set_flat_product(__isl_take isl_set
*set1
,
9664 __isl_take isl_set
*set2
)
9666 return isl_map_flat_range_product(set1
, set2
);
9669 /* Given two maps A -> B and C -> D, construct a map [A -> C] -> (B * D)
9671 static __isl_give isl_map
*map_domain_product_aligned(__isl_take isl_map
*map1
,
9672 __isl_take isl_map
*map2
)
9674 return map_product(map1
, map2
, &isl_space_domain_product
,
9675 &isl_basic_map_domain_product
, 1);
9678 /* Given two maps A -> B and C -> D, construct a map (A * C) -> [B -> D]
9680 static __isl_give isl_map
*map_range_product_aligned(__isl_take isl_map
*map1
,
9681 __isl_take isl_map
*map2
)
9683 return map_product(map1
, map2
, &isl_space_range_product
,
9684 &isl_basic_map_range_product
, 1);
9687 __isl_give isl_map
*isl_map_domain_product(__isl_take isl_map
*map1
,
9688 __isl_take isl_map
*map2
)
9690 return isl_map_align_params_map_map_and(map1
, map2
,
9691 &map_domain_product_aligned
);
9694 __isl_give isl_map
*isl_map_range_product(__isl_take isl_map
*map1
,
9695 __isl_take isl_map
*map2
)
9697 return isl_map_align_params_map_map_and(map1
, map2
,
9698 &map_range_product_aligned
);
9701 /* Given a map A -> [B -> C], extract the map A -> B.
9703 __isl_give isl_map
*isl_map_range_factor_domain(__isl_take isl_map
*map
)
9710 if (!isl_space_range_is_wrapping(map
->dim
))
9711 isl_die(isl_map_get_ctx(map
), isl_error_invalid
,
9712 "range is not a product", return isl_map_free(map
));
9714 space
= isl_map_get_space(map
);
9715 total
= isl_space_dim(space
, isl_dim_out
);
9716 space
= isl_space_range_factor_domain(space
);
9717 keep
= isl_space_dim(space
, isl_dim_out
);
9718 map
= isl_map_project_out(map
, isl_dim_out
, keep
, total
- keep
);
9719 map
= isl_map_reset_space(map
, space
);
9724 /* Given a map A -> [B -> C], extract the map A -> C.
9726 __isl_give isl_map
*isl_map_range_factor_range(__isl_take isl_map
*map
)
9733 if (!isl_space_range_is_wrapping(map
->dim
))
9734 isl_die(isl_map_get_ctx(map
), isl_error_invalid
,
9735 "range is not a product", return isl_map_free(map
));
9737 space
= isl_map_get_space(map
);
9738 total
= isl_space_dim(space
, isl_dim_out
);
9739 space
= isl_space_range_factor_range(space
);
9740 keep
= isl_space_dim(space
, isl_dim_out
);
9741 map
= isl_map_project_out(map
, isl_dim_out
, 0, total
- keep
);
9742 map
= isl_map_reset_space(map
, space
);
9747 /* Given two maps A -> B and C -> D, construct a map (A, C) -> (B * D)
9749 __isl_give isl_map
*isl_map_flat_domain_product(__isl_take isl_map
*map1
,
9750 __isl_take isl_map
*map2
)
9754 prod
= isl_map_domain_product(map1
, map2
);
9755 prod
= isl_map_flatten_domain(prod
);
9759 /* Given two maps A -> B and C -> D, construct a map (A * C) -> (B, D)
9761 __isl_give isl_map
*isl_map_flat_range_product(__isl_take isl_map
*map1
,
9762 __isl_take isl_map
*map2
)
9766 prod
= isl_map_range_product(map1
, map2
);
9767 prod
= isl_map_flatten_range(prod
);
9771 uint32_t isl_basic_map_get_hash(__isl_keep isl_basic_map
*bmap
)
9774 uint32_t hash
= isl_hash_init();
9779 bmap
= isl_basic_map_copy(bmap
);
9780 bmap
= isl_basic_map_normalize(bmap
);
9783 total
= isl_basic_map_total_dim(bmap
);
9784 isl_hash_byte(hash
, bmap
->n_eq
& 0xFF);
9785 for (i
= 0; i
< bmap
->n_eq
; ++i
) {
9787 c_hash
= isl_seq_get_hash(bmap
->eq
[i
], 1 + total
);
9788 isl_hash_hash(hash
, c_hash
);
9790 isl_hash_byte(hash
, bmap
->n_ineq
& 0xFF);
9791 for (i
= 0; i
< bmap
->n_ineq
; ++i
) {
9793 c_hash
= isl_seq_get_hash(bmap
->ineq
[i
], 1 + total
);
9794 isl_hash_hash(hash
, c_hash
);
9796 isl_hash_byte(hash
, bmap
->n_div
& 0xFF);
9797 for (i
= 0; i
< bmap
->n_div
; ++i
) {
9799 if (isl_int_is_zero(bmap
->div
[i
][0]))
9801 isl_hash_byte(hash
, i
& 0xFF);
9802 c_hash
= isl_seq_get_hash(bmap
->div
[i
], 1 + 1 + total
);
9803 isl_hash_hash(hash
, c_hash
);
9805 isl_basic_map_free(bmap
);
9809 uint32_t isl_basic_set_get_hash(__isl_keep isl_basic_set
*bset
)
9811 return isl_basic_map_get_hash((isl_basic_map
*)bset
);
9814 uint32_t isl_map_get_hash(__isl_keep isl_map
*map
)
9821 map
= isl_map_copy(map
);
9822 map
= isl_map_normalize(map
);
9826 hash
= isl_hash_init();
9827 for (i
= 0; i
< map
->n
; ++i
) {
9829 bmap_hash
= isl_basic_map_get_hash(map
->p
[i
]);
9830 isl_hash_hash(hash
, bmap_hash
);
9838 uint32_t isl_set_get_hash(__isl_keep isl_set
*set
)
9840 return isl_map_get_hash((isl_map
*)set
);
9843 /* Check if the value for dimension dim is completely determined
9844 * by the values of the other parameters and variables.
9845 * That is, check if dimension dim is involved in an equality.
9847 int isl_basic_set_dim_is_unique(struct isl_basic_set
*bset
, unsigned dim
)
9854 nparam
= isl_basic_set_n_param(bset
);
9855 for (i
= 0; i
< bset
->n_eq
; ++i
)
9856 if (!isl_int_is_zero(bset
->eq
[i
][1 + nparam
+ dim
]))
9861 /* Check if the value for dimension dim is completely determined
9862 * by the values of the other parameters and variables.
9863 * That is, check if dimension dim is involved in an equality
9864 * for each of the subsets.
9866 int isl_set_dim_is_unique(struct isl_set
*set
, unsigned dim
)
9872 for (i
= 0; i
< set
->n
; ++i
) {
9874 unique
= isl_basic_set_dim_is_unique(set
->p
[i
], dim
);
9881 /* Return the number of basic maps in the (current) representation of "map".
9883 int isl_map_n_basic_map(__isl_keep isl_map
*map
)
9885 return map
? map
->n
: 0;
9888 int isl_set_n_basic_set(__isl_keep isl_set
*set
)
9890 return set
? set
->n
: 0;
9893 int isl_map_foreach_basic_map(__isl_keep isl_map
*map
,
9894 int (*fn
)(__isl_take isl_basic_map
*bmap
, void *user
), void *user
)
9901 for (i
= 0; i
< map
->n
; ++i
)
9902 if (fn(isl_basic_map_copy(map
->p
[i
]), user
) < 0)
9908 int isl_set_foreach_basic_set(__isl_keep isl_set
*set
,
9909 int (*fn
)(__isl_take isl_basic_set
*bset
, void *user
), void *user
)
9916 for (i
= 0; i
< set
->n
; ++i
)
9917 if (fn(isl_basic_set_copy(set
->p
[i
]), user
) < 0)
9923 __isl_give isl_basic_set
*isl_basic_set_lift(__isl_take isl_basic_set
*bset
)
9930 bset
= isl_basic_set_cow(bset
);
9934 dim
= isl_basic_set_get_space(bset
);
9935 dim
= isl_space_lift(dim
, bset
->n_div
);
9938 isl_space_free(bset
->dim
);
9940 bset
->extra
-= bset
->n_div
;
9943 bset
= isl_basic_set_finalize(bset
);
9947 isl_basic_set_free(bset
);
9951 __isl_give isl_set
*isl_set_lift(__isl_take isl_set
*set
)
9957 set
= isl_set_align_divs(set
);
9962 set
= isl_set_cow(set
);
9966 n_div
= set
->p
[0]->n_div
;
9967 dim
= isl_set_get_space(set
);
9968 dim
= isl_space_lift(dim
, n_div
);
9971 isl_space_free(set
->dim
);
9974 for (i
= 0; i
< set
->n
; ++i
) {
9975 set
->p
[i
] = isl_basic_set_lift(set
->p
[i
]);
9986 __isl_give isl_map
*isl_set_lifting(__isl_take isl_set
*set
)
9989 struct isl_basic_map
*bmap
;
9996 set
= isl_set_align_divs(set
);
10001 dim
= isl_set_get_space(set
);
10002 if (set
->n
== 0 || set
->p
[0]->n_div
== 0) {
10004 return isl_map_identity(isl_space_map_from_set(dim
));
10007 n_div
= set
->p
[0]->n_div
;
10008 dim
= isl_space_map_from_set(dim
);
10009 n_param
= isl_space_dim(dim
, isl_dim_param
);
10010 n_set
= isl_space_dim(dim
, isl_dim_in
);
10011 dim
= isl_space_extend(dim
, n_param
, n_set
, n_set
+ n_div
);
10012 bmap
= isl_basic_map_alloc_space(dim
, 0, n_set
, 2 * n_div
);
10013 for (i
= 0; i
< n_set
; ++i
)
10014 bmap
= var_equal(bmap
, i
);
10016 total
= n_param
+ n_set
+ n_set
+ n_div
;
10017 for (i
= 0; i
< n_div
; ++i
) {
10018 k
= isl_basic_map_alloc_inequality(bmap
);
10021 isl_seq_cpy(bmap
->ineq
[k
], set
->p
[0]->div
[i
]+1, 1+n_param
);
10022 isl_seq_clr(bmap
->ineq
[k
]+1+n_param
, n_set
);
10023 isl_seq_cpy(bmap
->ineq
[k
]+1+n_param
+n_set
,
10024 set
->p
[0]->div
[i
]+1+1+n_param
, n_set
+ n_div
);
10025 isl_int_neg(bmap
->ineq
[k
][1+n_param
+n_set
+n_set
+i
],
10026 set
->p
[0]->div
[i
][0]);
10028 l
= isl_basic_map_alloc_inequality(bmap
);
10031 isl_seq_neg(bmap
->ineq
[l
], bmap
->ineq
[k
], 1 + total
);
10032 isl_int_add(bmap
->ineq
[l
][0], bmap
->ineq
[l
][0],
10033 set
->p
[0]->div
[i
][0]);
10034 isl_int_sub_ui(bmap
->ineq
[l
][0], bmap
->ineq
[l
][0], 1);
10038 bmap
= isl_basic_map_simplify(bmap
);
10039 bmap
= isl_basic_map_finalize(bmap
);
10040 return isl_map_from_basic_map(bmap
);
10043 isl_basic_map_free(bmap
);
10047 int isl_basic_set_size(__isl_keep isl_basic_set
*bset
)
10055 dim
= isl_basic_set_total_dim(bset
);
10056 size
+= bset
->n_eq
* (1 + dim
);
10057 size
+= bset
->n_ineq
* (1 + dim
);
10058 size
+= bset
->n_div
* (2 + dim
);
10063 int isl_set_size(__isl_keep isl_set
*set
)
10071 for (i
= 0; i
< set
->n
; ++i
)
10072 size
+= isl_basic_set_size(set
->p
[i
]);
10077 /* Check if there is any lower bound (if lower == 0) and/or upper
10078 * bound (if upper == 0) on the specified dim.
10080 static int basic_map_dim_is_bounded(__isl_keep isl_basic_map
*bmap
,
10081 enum isl_dim_type type
, unsigned pos
, int lower
, int upper
)
10088 isl_assert(bmap
->ctx
, pos
< isl_basic_map_dim(bmap
, type
), return -1);
10090 pos
+= isl_basic_map_offset(bmap
, type
);
10092 for (i
= 0; i
< bmap
->n_div
; ++i
) {
10093 if (isl_int_is_zero(bmap
->div
[i
][0]))
10095 if (!isl_int_is_zero(bmap
->div
[i
][1 + pos
]))
10099 for (i
= 0; i
< bmap
->n_eq
; ++i
)
10100 if (!isl_int_is_zero(bmap
->eq
[i
][pos
]))
10103 for (i
= 0; i
< bmap
->n_ineq
; ++i
) {
10104 int sgn
= isl_int_sgn(bmap
->ineq
[i
][pos
]);
10111 return lower
&& upper
;
10114 int isl_basic_map_dim_is_bounded(__isl_keep isl_basic_map
*bmap
,
10115 enum isl_dim_type type
, unsigned pos
)
10117 return basic_map_dim_is_bounded(bmap
, type
, pos
, 0, 0);
10120 int isl_basic_map_dim_has_lower_bound(__isl_keep isl_basic_map
*bmap
,
10121 enum isl_dim_type type
, unsigned pos
)
10123 return basic_map_dim_is_bounded(bmap
, type
, pos
, 0, 1);
10126 int isl_basic_map_dim_has_upper_bound(__isl_keep isl_basic_map
*bmap
,
10127 enum isl_dim_type type
, unsigned pos
)
10129 return basic_map_dim_is_bounded(bmap
, type
, pos
, 1, 0);
10132 int isl_map_dim_is_bounded(__isl_keep isl_map
*map
,
10133 enum isl_dim_type type
, unsigned pos
)
10140 for (i
= 0; i
< map
->n
; ++i
) {
10142 bounded
= isl_basic_map_dim_is_bounded(map
->p
[i
], type
, pos
);
10143 if (bounded
< 0 || !bounded
)
10150 /* Return 1 if the specified dim is involved in both an upper bound
10151 * and a lower bound.
10153 int isl_set_dim_is_bounded(__isl_keep isl_set
*set
,
10154 enum isl_dim_type type
, unsigned pos
)
10156 return isl_map_dim_is_bounded((isl_map
*)set
, type
, pos
);
10159 /* Does "map" have a bound (according to "fn") for any of its basic maps?
10161 static int has_any_bound(__isl_keep isl_map
*map
,
10162 enum isl_dim_type type
, unsigned pos
,
10163 int (*fn
)(__isl_keep isl_basic_map
*bmap
,
10164 enum isl_dim_type type
, unsigned pos
))
10171 for (i
= 0; i
< map
->n
; ++i
) {
10173 bounded
= fn(map
->p
[i
], type
, pos
);
10174 if (bounded
< 0 || bounded
)
10181 /* Return 1 if the specified dim is involved in any lower bound.
10183 int isl_set_dim_has_any_lower_bound(__isl_keep isl_set
*set
,
10184 enum isl_dim_type type
, unsigned pos
)
10186 return has_any_bound(set
, type
, pos
,
10187 &isl_basic_map_dim_has_lower_bound
);
10190 /* Return 1 if the specified dim is involved in any upper bound.
10192 int isl_set_dim_has_any_upper_bound(__isl_keep isl_set
*set
,
10193 enum isl_dim_type type
, unsigned pos
)
10195 return has_any_bound(set
, type
, pos
,
10196 &isl_basic_map_dim_has_upper_bound
);
10199 /* Does "map" have a bound (according to "fn") for all of its basic maps?
10201 static int has_bound(__isl_keep isl_map
*map
,
10202 enum isl_dim_type type
, unsigned pos
,
10203 int (*fn
)(__isl_keep isl_basic_map
*bmap
,
10204 enum isl_dim_type type
, unsigned pos
))
10211 for (i
= 0; i
< map
->n
; ++i
) {
10213 bounded
= fn(map
->p
[i
], type
, pos
);
10214 if (bounded
< 0 || !bounded
)
10221 /* Return 1 if the specified dim has a lower bound (in each of its basic sets).
10223 int isl_set_dim_has_lower_bound(__isl_keep isl_set
*set
,
10224 enum isl_dim_type type
, unsigned pos
)
10226 return has_bound(set
, type
, pos
, &isl_basic_map_dim_has_lower_bound
);
10229 /* Return 1 if the specified dim has an upper bound (in each of its basic sets).
10231 int isl_set_dim_has_upper_bound(__isl_keep isl_set
*set
,
10232 enum isl_dim_type type
, unsigned pos
)
10234 return has_bound(set
, type
, pos
, &isl_basic_map_dim_has_upper_bound
);
10237 /* For each of the "n" variables starting at "first", determine
10238 * the sign of the variable and put the results in the first "n"
10239 * elements of the array "signs".
10241 * 1 means that the variable is non-negative
10242 * -1 means that the variable is non-positive
10243 * 0 means the variable attains both positive and negative values.
10245 int isl_basic_set_vars_get_sign(__isl_keep isl_basic_set
*bset
,
10246 unsigned first
, unsigned n
, int *signs
)
10248 isl_vec
*bound
= NULL
;
10249 struct isl_tab
*tab
= NULL
;
10250 struct isl_tab_undo
*snap
;
10253 if (!bset
|| !signs
)
10256 bound
= isl_vec_alloc(bset
->ctx
, 1 + isl_basic_set_total_dim(bset
));
10257 tab
= isl_tab_from_basic_set(bset
, 0);
10258 if (!bound
|| !tab
)
10261 isl_seq_clr(bound
->el
, bound
->size
);
10262 isl_int_set_si(bound
->el
[0], -1);
10264 snap
= isl_tab_snap(tab
);
10265 for (i
= 0; i
< n
; ++i
) {
10268 isl_int_set_si(bound
->el
[1 + first
+ i
], -1);
10269 if (isl_tab_add_ineq(tab
, bound
->el
) < 0)
10271 empty
= tab
->empty
;
10272 isl_int_set_si(bound
->el
[1 + first
+ i
], 0);
10273 if (isl_tab_rollback(tab
, snap
) < 0)
10281 isl_int_set_si(bound
->el
[1 + first
+ i
], 1);
10282 if (isl_tab_add_ineq(tab
, bound
->el
) < 0)
10284 empty
= tab
->empty
;
10285 isl_int_set_si(bound
->el
[1 + first
+ i
], 0);
10286 if (isl_tab_rollback(tab
, snap
) < 0)
10289 signs
[i
] = empty
? -1 : 0;
10293 isl_vec_free(bound
);
10297 isl_vec_free(bound
);
10301 int isl_basic_set_dims_get_sign(__isl_keep isl_basic_set
*bset
,
10302 enum isl_dim_type type
, unsigned first
, unsigned n
, int *signs
)
10304 if (!bset
|| !signs
)
10306 isl_assert(bset
->ctx
, first
+ n
<= isl_basic_set_dim(bset
, type
),
10309 first
+= pos(bset
->dim
, type
) - 1;
10310 return isl_basic_set_vars_get_sign(bset
, first
, n
, signs
);
10313 /* Is it possible for the integer division "div" to depend (possibly
10314 * indirectly) on any output dimensions?
10316 * If the div is undefined, then we conservatively assume that it
10317 * may depend on them.
10318 * Otherwise, we check if it actually depends on them or on any integer
10319 * divisions that may depend on them.
10321 static int div_may_involve_output(__isl_keep isl_basic_map
*bmap
, int div
)
10324 unsigned n_out
, o_out
;
10325 unsigned n_div
, o_div
;
10327 if (isl_int_is_zero(bmap
->div
[div
][0]))
10330 n_out
= isl_basic_map_dim(bmap
, isl_dim_out
);
10331 o_out
= isl_basic_map_offset(bmap
, isl_dim_out
);
10333 if (isl_seq_first_non_zero(bmap
->div
[div
] + 1 + o_out
, n_out
) != -1)
10336 n_div
= isl_basic_map_dim(bmap
, isl_dim_div
);
10337 o_div
= isl_basic_map_offset(bmap
, isl_dim_div
);
10339 for (i
= 0; i
< n_div
; ++i
) {
10340 if (isl_int_is_zero(bmap
->div
[div
][1 + o_div
+ i
]))
10342 if (div_may_involve_output(bmap
, i
))
10349 /* Return the index of the equality of "bmap" that defines
10350 * the output dimension "pos" in terms of earlier dimensions.
10351 * The equality may also involve integer divisions, as long
10352 * as those integer divisions are defined in terms of
10353 * parameters or input dimensions.
10354 * Return bmap->n_eq if there is no such equality.
10355 * Return -1 on error.
10357 int isl_basic_map_output_defining_equality(__isl_keep isl_basic_map
*bmap
,
10361 unsigned n_out
, o_out
;
10362 unsigned n_div
, o_div
;
10367 n_out
= isl_basic_map_dim(bmap
, isl_dim_out
);
10368 o_out
= isl_basic_map_offset(bmap
, isl_dim_out
);
10369 n_div
= isl_basic_map_dim(bmap
, isl_dim_div
);
10370 o_div
= isl_basic_map_offset(bmap
, isl_dim_div
);
10372 for (j
= 0; j
< bmap
->n_eq
; ++j
) {
10373 if (isl_int_is_zero(bmap
->eq
[j
][o_out
+ pos
]))
10375 if (isl_seq_first_non_zero(bmap
->eq
[j
] + o_out
+ pos
+ 1,
10376 n_out
- (pos
+ 1)) != -1)
10378 for (k
= 0; k
< n_div
; ++k
) {
10379 if (isl_int_is_zero(bmap
->eq
[j
][o_div
+ k
]))
10381 if (div_may_involve_output(bmap
, k
))
10391 /* Check if the given basic map is obviously single-valued.
10392 * In particular, for each output dimension, check that there is
10393 * an equality that defines the output dimension in terms of
10394 * earlier dimensions.
10396 int isl_basic_map_plain_is_single_valued(__isl_keep isl_basic_map
*bmap
)
10404 n_out
= isl_basic_map_dim(bmap
, isl_dim_out
);
10406 for (i
= 0; i
< n_out
; ++i
) {
10409 eq
= isl_basic_map_output_defining_equality(bmap
, i
);
10412 if (eq
>= bmap
->n_eq
)
10419 /* Check if the given basic map is single-valued.
10420 * We simply compute
10424 * and check if the result is a subset of the identity mapping.
10426 int isl_basic_map_is_single_valued(__isl_keep isl_basic_map
*bmap
)
10429 isl_basic_map
*test
;
10433 sv
= isl_basic_map_plain_is_single_valued(bmap
);
10437 test
= isl_basic_map_reverse(isl_basic_map_copy(bmap
));
10438 test
= isl_basic_map_apply_range(test
, isl_basic_map_copy(bmap
));
10440 space
= isl_basic_map_get_space(bmap
);
10441 space
= isl_space_map_from_set(isl_space_range(space
));
10442 id
= isl_basic_map_identity(space
);
10444 sv
= isl_basic_map_is_subset(test
, id
);
10446 isl_basic_map_free(test
);
10447 isl_basic_map_free(id
);
10452 /* Check if the given map is obviously single-valued.
10454 int isl_map_plain_is_single_valued(__isl_keep isl_map
*map
)
10463 return isl_basic_map_plain_is_single_valued(map
->p
[0]);
10466 /* Check if the given map is single-valued.
10467 * We simply compute
10471 * and check if the result is a subset of the identity mapping.
10473 int isl_map_is_single_valued(__isl_keep isl_map
*map
)
10480 sv
= isl_map_plain_is_single_valued(map
);
10484 test
= isl_map_reverse(isl_map_copy(map
));
10485 test
= isl_map_apply_range(test
, isl_map_copy(map
));
10487 dim
= isl_space_map_from_set(isl_space_range(isl_map_get_space(map
)));
10488 id
= isl_map_identity(dim
);
10490 sv
= isl_map_is_subset(test
, id
);
10492 isl_map_free(test
);
10498 int isl_map_is_injective(__isl_keep isl_map
*map
)
10502 map
= isl_map_copy(map
);
10503 map
= isl_map_reverse(map
);
10504 in
= isl_map_is_single_valued(map
);
10510 /* Check if the given map is obviously injective.
10512 int isl_map_plain_is_injective(__isl_keep isl_map
*map
)
10516 map
= isl_map_copy(map
);
10517 map
= isl_map_reverse(map
);
10518 in
= isl_map_plain_is_single_valued(map
);
10524 int isl_map_is_bijective(__isl_keep isl_map
*map
)
10528 sv
= isl_map_is_single_valued(map
);
10532 return isl_map_is_injective(map
);
10535 int isl_set_is_singleton(__isl_keep isl_set
*set
)
10537 return isl_map_is_single_valued((isl_map
*)set
);
10540 int isl_map_is_translation(__isl_keep isl_map
*map
)
10545 delta
= isl_map_deltas(isl_map_copy(map
));
10546 ok
= isl_set_is_singleton(delta
);
10547 isl_set_free(delta
);
10552 static int unique(isl_int
*p
, unsigned pos
, unsigned len
)
10554 if (isl_seq_first_non_zero(p
, pos
) != -1)
10556 if (isl_seq_first_non_zero(p
+ pos
+ 1, len
- pos
- 1) != -1)
10561 int isl_basic_set_is_box(__isl_keep isl_basic_set
*bset
)
10570 if (isl_basic_set_dim(bset
, isl_dim_div
) != 0)
10573 nvar
= isl_basic_set_dim(bset
, isl_dim_set
);
10574 ovar
= isl_space_offset(bset
->dim
, isl_dim_set
);
10575 for (j
= 0; j
< nvar
; ++j
) {
10576 int lower
= 0, upper
= 0;
10577 for (i
= 0; i
< bset
->n_eq
; ++i
) {
10578 if (isl_int_is_zero(bset
->eq
[i
][1 + ovar
+ j
]))
10580 if (!unique(bset
->eq
[i
] + 1 + ovar
, j
, nvar
))
10584 if (i
< bset
->n_eq
)
10586 for (i
= 0; i
< bset
->n_ineq
; ++i
) {
10587 if (isl_int_is_zero(bset
->ineq
[i
][1 + ovar
+ j
]))
10589 if (!unique(bset
->ineq
[i
] + 1 + ovar
, j
, nvar
))
10591 if (isl_int_is_pos(bset
->ineq
[i
][1 + ovar
+ j
]))
10596 if (!lower
|| !upper
)
10603 int isl_set_is_box(__isl_keep isl_set
*set
)
10610 return isl_basic_set_is_box(set
->p
[0]);
10613 int isl_basic_set_is_wrapping(__isl_keep isl_basic_set
*bset
)
10618 return isl_space_is_wrapping(bset
->dim
);
10621 int isl_set_is_wrapping(__isl_keep isl_set
*set
)
10626 return isl_space_is_wrapping(set
->dim
);
10629 /* Is the domain of "map" a wrapped relation?
10631 int isl_map_domain_is_wrapping(__isl_keep isl_map
*map
)
10636 return isl_space_domain_is_wrapping(map
->dim
);
10639 /* Is the range of "map" a wrapped relation?
10641 int isl_map_range_is_wrapping(__isl_keep isl_map
*map
)
10646 return isl_space_range_is_wrapping(map
->dim
);
10649 __isl_give isl_basic_set
*isl_basic_map_wrap(__isl_take isl_basic_map
*bmap
)
10651 bmap
= isl_basic_map_cow(bmap
);
10655 bmap
->dim
= isl_space_wrap(bmap
->dim
);
10659 bmap
= isl_basic_map_finalize(bmap
);
10661 return (isl_basic_set
*)bmap
;
10663 isl_basic_map_free(bmap
);
10667 __isl_give isl_set
*isl_map_wrap(__isl_take isl_map
*map
)
10671 map
= isl_map_cow(map
);
10675 for (i
= 0; i
< map
->n
; ++i
) {
10676 map
->p
[i
] = (isl_basic_map
*)isl_basic_map_wrap(map
->p
[i
]);
10680 map
->dim
= isl_space_wrap(map
->dim
);
10684 return (isl_set
*)map
;
10690 __isl_give isl_basic_map
*isl_basic_set_unwrap(__isl_take isl_basic_set
*bset
)
10692 bset
= isl_basic_set_cow(bset
);
10696 bset
->dim
= isl_space_unwrap(bset
->dim
);
10700 bset
= isl_basic_set_finalize(bset
);
10702 return (isl_basic_map
*)bset
;
10704 isl_basic_set_free(bset
);
10708 __isl_give isl_map
*isl_set_unwrap(__isl_take isl_set
*set
)
10715 if (!isl_set_is_wrapping(set
))
10716 isl_die(set
->ctx
, isl_error_invalid
, "not a wrapping set",
10719 set
= isl_set_cow(set
);
10723 for (i
= 0; i
< set
->n
; ++i
) {
10724 set
->p
[i
] = (isl_basic_set
*)isl_basic_set_unwrap(set
->p
[i
]);
10729 set
->dim
= isl_space_unwrap(set
->dim
);
10733 return (isl_map
*)set
;
10739 __isl_give isl_basic_map
*isl_basic_map_reset(__isl_take isl_basic_map
*bmap
,
10740 enum isl_dim_type type
)
10745 if (!isl_space_is_named_or_nested(bmap
->dim
, type
))
10748 bmap
= isl_basic_map_cow(bmap
);
10752 bmap
->dim
= isl_space_reset(bmap
->dim
, type
);
10756 bmap
= isl_basic_map_finalize(bmap
);
10760 isl_basic_map_free(bmap
);
10764 __isl_give isl_map
*isl_map_reset(__isl_take isl_map
*map
,
10765 enum isl_dim_type type
)
10772 if (!isl_space_is_named_or_nested(map
->dim
, type
))
10775 map
= isl_map_cow(map
);
10779 for (i
= 0; i
< map
->n
; ++i
) {
10780 map
->p
[i
] = isl_basic_map_reset(map
->p
[i
], type
);
10784 map
->dim
= isl_space_reset(map
->dim
, type
);
10794 __isl_give isl_basic_map
*isl_basic_map_flatten(__isl_take isl_basic_map
*bmap
)
10799 if (!bmap
->dim
->nested
[0] && !bmap
->dim
->nested
[1])
10802 bmap
= isl_basic_map_cow(bmap
);
10806 bmap
->dim
= isl_space_flatten(bmap
->dim
);
10810 bmap
= isl_basic_map_finalize(bmap
);
10814 isl_basic_map_free(bmap
);
10818 __isl_give isl_basic_set
*isl_basic_set_flatten(__isl_take isl_basic_set
*bset
)
10820 return (isl_basic_set
*)isl_basic_map_flatten((isl_basic_map
*)bset
);
10823 __isl_give isl_basic_map
*isl_basic_map_flatten_domain(
10824 __isl_take isl_basic_map
*bmap
)
10829 if (!bmap
->dim
->nested
[0])
10832 bmap
= isl_basic_map_cow(bmap
);
10836 bmap
->dim
= isl_space_flatten_domain(bmap
->dim
);
10840 bmap
= isl_basic_map_finalize(bmap
);
10844 isl_basic_map_free(bmap
);
10848 __isl_give isl_basic_map
*isl_basic_map_flatten_range(
10849 __isl_take isl_basic_map
*bmap
)
10854 if (!bmap
->dim
->nested
[1])
10857 bmap
= isl_basic_map_cow(bmap
);
10861 bmap
->dim
= isl_space_flatten_range(bmap
->dim
);
10865 bmap
= isl_basic_map_finalize(bmap
);
10869 isl_basic_map_free(bmap
);
10873 __isl_give isl_map
*isl_map_flatten(__isl_take isl_map
*map
)
10880 if (!map
->dim
->nested
[0] && !map
->dim
->nested
[1])
10883 map
= isl_map_cow(map
);
10887 for (i
= 0; i
< map
->n
; ++i
) {
10888 map
->p
[i
] = isl_basic_map_flatten(map
->p
[i
]);
10892 map
->dim
= isl_space_flatten(map
->dim
);
10902 __isl_give isl_set
*isl_set_flatten(__isl_take isl_set
*set
)
10904 return (isl_set
*)isl_map_flatten((isl_map
*)set
);
10907 __isl_give isl_map
*isl_set_flatten_map(__isl_take isl_set
*set
)
10909 isl_space
*dim
, *flat_dim
;
10912 dim
= isl_set_get_space(set
);
10913 flat_dim
= isl_space_flatten(isl_space_copy(dim
));
10914 map
= isl_map_identity(isl_space_join(isl_space_reverse(dim
), flat_dim
));
10915 map
= isl_map_intersect_domain(map
, set
);
10920 __isl_give isl_map
*isl_map_flatten_domain(__isl_take isl_map
*map
)
10927 if (!map
->dim
->nested
[0])
10930 map
= isl_map_cow(map
);
10934 for (i
= 0; i
< map
->n
; ++i
) {
10935 map
->p
[i
] = isl_basic_map_flatten_domain(map
->p
[i
]);
10939 map
->dim
= isl_space_flatten_domain(map
->dim
);
10949 __isl_give isl_map
*isl_map_flatten_range(__isl_take isl_map
*map
)
10956 if (!map
->dim
->nested
[1])
10959 map
= isl_map_cow(map
);
10963 for (i
= 0; i
< map
->n
; ++i
) {
10964 map
->p
[i
] = isl_basic_map_flatten_range(map
->p
[i
]);
10968 map
->dim
= isl_space_flatten_range(map
->dim
);
10978 /* Reorder the dimensions of "bmap" according to the given dim_map
10979 * and set the dimension specification to "dim".
10981 __isl_give isl_basic_map
*isl_basic_map_realign(__isl_take isl_basic_map
*bmap
,
10982 __isl_take isl_space
*dim
, __isl_take
struct isl_dim_map
*dim_map
)
10984 isl_basic_map
*res
;
10987 bmap
= isl_basic_map_cow(bmap
);
10988 if (!bmap
|| !dim
|| !dim_map
)
10991 flags
= bmap
->flags
;
10992 ISL_FL_CLR(flags
, ISL_BASIC_MAP_FINAL
);
10993 ISL_FL_CLR(flags
, ISL_BASIC_MAP_NORMALIZED
);
10994 ISL_FL_CLR(flags
, ISL_BASIC_MAP_NORMALIZED_DIVS
);
10995 res
= isl_basic_map_alloc_space(dim
,
10996 bmap
->n_div
, bmap
->n_eq
, bmap
->n_ineq
);
10997 res
= isl_basic_map_add_constraints_dim_map(res
, bmap
, dim_map
);
10999 res
->flags
= flags
;
11000 res
= isl_basic_map_finalize(res
);
11004 isl_basic_map_free(bmap
);
11005 isl_space_free(dim
);
11009 /* Reorder the dimensions of "map" according to given reordering.
11011 __isl_give isl_map
*isl_map_realign(__isl_take isl_map
*map
,
11012 __isl_take isl_reordering
*r
)
11015 struct isl_dim_map
*dim_map
;
11017 map
= isl_map_cow(map
);
11018 dim_map
= isl_dim_map_from_reordering(r
);
11019 if (!map
|| !r
|| !dim_map
)
11022 for (i
= 0; i
< map
->n
; ++i
) {
11023 struct isl_dim_map
*dim_map_i
;
11025 dim_map_i
= isl_dim_map_extend(dim_map
, map
->p
[i
]);
11027 map
->p
[i
] = isl_basic_map_realign(map
->p
[i
],
11028 isl_space_copy(r
->dim
), dim_map_i
);
11034 map
= isl_map_reset_space(map
, isl_space_copy(r
->dim
));
11036 isl_reordering_free(r
);
11042 isl_reordering_free(r
);
11046 __isl_give isl_set
*isl_set_realign(__isl_take isl_set
*set
,
11047 __isl_take isl_reordering
*r
)
11049 return (isl_set
*)isl_map_realign((isl_map
*)set
, r
);
11052 __isl_give isl_map
*isl_map_align_params(__isl_take isl_map
*map
,
11053 __isl_take isl_space
*model
)
11057 if (!map
|| !model
)
11060 ctx
= isl_space_get_ctx(model
);
11061 if (!isl_space_has_named_params(model
))
11062 isl_die(ctx
, isl_error_invalid
,
11063 "model has unnamed parameters", goto error
);
11064 if (!isl_space_has_named_params(map
->dim
))
11065 isl_die(ctx
, isl_error_invalid
,
11066 "relation has unnamed parameters", goto error
);
11067 if (!isl_space_match(map
->dim
, isl_dim_param
, model
, isl_dim_param
)) {
11068 isl_reordering
*exp
;
11070 model
= isl_space_drop_dims(model
, isl_dim_in
,
11071 0, isl_space_dim(model
, isl_dim_in
));
11072 model
= isl_space_drop_dims(model
, isl_dim_out
,
11073 0, isl_space_dim(model
, isl_dim_out
));
11074 exp
= isl_parameter_alignment_reordering(map
->dim
, model
);
11075 exp
= isl_reordering_extend_space(exp
, isl_map_get_space(map
));
11076 map
= isl_map_realign(map
, exp
);
11079 isl_space_free(model
);
11082 isl_space_free(model
);
11087 __isl_give isl_set
*isl_set_align_params(__isl_take isl_set
*set
,
11088 __isl_take isl_space
*model
)
11090 return isl_map_align_params(set
, model
);
11093 /* Align the parameters of "bmap" to those of "model", introducing
11094 * additional parameters if needed.
11096 __isl_give isl_basic_map
*isl_basic_map_align_params(
11097 __isl_take isl_basic_map
*bmap
, __isl_take isl_space
*model
)
11101 if (!bmap
|| !model
)
11104 ctx
= isl_space_get_ctx(model
);
11105 if (!isl_space_has_named_params(model
))
11106 isl_die(ctx
, isl_error_invalid
,
11107 "model has unnamed parameters", goto error
);
11108 if (!isl_space_has_named_params(bmap
->dim
))
11109 isl_die(ctx
, isl_error_invalid
,
11110 "relation has unnamed parameters", goto error
);
11111 if (!isl_space_match(bmap
->dim
, isl_dim_param
, model
, isl_dim_param
)) {
11112 isl_reordering
*exp
;
11113 struct isl_dim_map
*dim_map
;
11115 model
= isl_space_drop_dims(model
, isl_dim_in
,
11116 0, isl_space_dim(model
, isl_dim_in
));
11117 model
= isl_space_drop_dims(model
, isl_dim_out
,
11118 0, isl_space_dim(model
, isl_dim_out
));
11119 exp
= isl_parameter_alignment_reordering(bmap
->dim
, model
);
11120 exp
= isl_reordering_extend_space(exp
,
11121 isl_basic_map_get_space(bmap
));
11122 dim_map
= isl_dim_map_from_reordering(exp
);
11123 bmap
= isl_basic_map_realign(bmap
,
11124 exp
? isl_space_copy(exp
->dim
) : NULL
,
11125 isl_dim_map_extend(dim_map
, bmap
));
11126 isl_reordering_free(exp
);
11130 isl_space_free(model
);
11133 isl_space_free(model
);
11134 isl_basic_map_free(bmap
);
11138 /* Align the parameters of "bset" to those of "model", introducing
11139 * additional parameters if needed.
11141 __isl_give isl_basic_set
*isl_basic_set_align_params(
11142 __isl_take isl_basic_set
*bset
, __isl_take isl_space
*model
)
11144 return isl_basic_map_align_params(bset
, model
);
11147 __isl_give isl_mat
*isl_basic_map_equalities_matrix(
11148 __isl_keep isl_basic_map
*bmap
, enum isl_dim_type c1
,
11149 enum isl_dim_type c2
, enum isl_dim_type c3
,
11150 enum isl_dim_type c4
, enum isl_dim_type c5
)
11152 enum isl_dim_type c
[5] = { c1
, c2
, c3
, c4
, c5
};
11153 struct isl_mat
*mat
;
11159 mat
= isl_mat_alloc(bmap
->ctx
, bmap
->n_eq
,
11160 isl_basic_map_total_dim(bmap
) + 1);
11163 for (i
= 0; i
< bmap
->n_eq
; ++i
)
11164 for (j
= 0, pos
= 0; j
< 5; ++j
) {
11165 int off
= isl_basic_map_offset(bmap
, c
[j
]);
11166 for (k
= 0; k
< isl_basic_map_dim(bmap
, c
[j
]); ++k
) {
11167 isl_int_set(mat
->row
[i
][pos
],
11168 bmap
->eq
[i
][off
+ k
]);
11176 __isl_give isl_mat
*isl_basic_map_inequalities_matrix(
11177 __isl_keep isl_basic_map
*bmap
, enum isl_dim_type c1
,
11178 enum isl_dim_type c2
, enum isl_dim_type c3
,
11179 enum isl_dim_type c4
, enum isl_dim_type c5
)
11181 enum isl_dim_type c
[5] = { c1
, c2
, c3
, c4
, c5
};
11182 struct isl_mat
*mat
;
11188 mat
= isl_mat_alloc(bmap
->ctx
, bmap
->n_ineq
,
11189 isl_basic_map_total_dim(bmap
) + 1);
11192 for (i
= 0; i
< bmap
->n_ineq
; ++i
)
11193 for (j
= 0, pos
= 0; j
< 5; ++j
) {
11194 int off
= isl_basic_map_offset(bmap
, c
[j
]);
11195 for (k
= 0; k
< isl_basic_map_dim(bmap
, c
[j
]); ++k
) {
11196 isl_int_set(mat
->row
[i
][pos
],
11197 bmap
->ineq
[i
][off
+ k
]);
11205 __isl_give isl_basic_map
*isl_basic_map_from_constraint_matrices(
11206 __isl_take isl_space
*dim
,
11207 __isl_take isl_mat
*eq
, __isl_take isl_mat
*ineq
, enum isl_dim_type c1
,
11208 enum isl_dim_type c2
, enum isl_dim_type c3
,
11209 enum isl_dim_type c4
, enum isl_dim_type c5
)
11211 enum isl_dim_type c
[5] = { c1
, c2
, c3
, c4
, c5
};
11212 isl_basic_map
*bmap
;
11218 if (!dim
|| !eq
|| !ineq
)
11221 if (eq
->n_col
!= ineq
->n_col
)
11222 isl_die(dim
->ctx
, isl_error_invalid
,
11223 "equalities and inequalities matrices should have "
11224 "same number of columns", goto error
);
11226 total
= 1 + isl_space_dim(dim
, isl_dim_all
);
11228 if (eq
->n_col
< total
)
11229 isl_die(dim
->ctx
, isl_error_invalid
,
11230 "number of columns too small", goto error
);
11232 extra
= eq
->n_col
- total
;
11234 bmap
= isl_basic_map_alloc_space(isl_space_copy(dim
), extra
,
11235 eq
->n_row
, ineq
->n_row
);
11238 for (i
= 0; i
< extra
; ++i
) {
11239 k
= isl_basic_map_alloc_div(bmap
);
11242 isl_int_set_si(bmap
->div
[k
][0], 0);
11244 for (i
= 0; i
< eq
->n_row
; ++i
) {
11245 l
= isl_basic_map_alloc_equality(bmap
);
11248 for (j
= 0, pos
= 0; j
< 5; ++j
) {
11249 int off
= isl_basic_map_offset(bmap
, c
[j
]);
11250 for (k
= 0; k
< isl_basic_map_dim(bmap
, c
[j
]); ++k
) {
11251 isl_int_set(bmap
->eq
[l
][off
+ k
],
11257 for (i
= 0; i
< ineq
->n_row
; ++i
) {
11258 l
= isl_basic_map_alloc_inequality(bmap
);
11261 for (j
= 0, pos
= 0; j
< 5; ++j
) {
11262 int off
= isl_basic_map_offset(bmap
, c
[j
]);
11263 for (k
= 0; k
< isl_basic_map_dim(bmap
, c
[j
]); ++k
) {
11264 isl_int_set(bmap
->ineq
[l
][off
+ k
],
11265 ineq
->row
[i
][pos
]);
11271 isl_space_free(dim
);
11273 isl_mat_free(ineq
);
11275 bmap
= isl_basic_map_simplify(bmap
);
11276 return isl_basic_map_finalize(bmap
);
11278 isl_space_free(dim
);
11280 isl_mat_free(ineq
);
11284 __isl_give isl_mat
*isl_basic_set_equalities_matrix(
11285 __isl_keep isl_basic_set
*bset
, enum isl_dim_type c1
,
11286 enum isl_dim_type c2
, enum isl_dim_type c3
, enum isl_dim_type c4
)
11288 return isl_basic_map_equalities_matrix((isl_basic_map
*)bset
,
11289 c1
, c2
, c3
, c4
, isl_dim_in
);
11292 __isl_give isl_mat
*isl_basic_set_inequalities_matrix(
11293 __isl_keep isl_basic_set
*bset
, enum isl_dim_type c1
,
11294 enum isl_dim_type c2
, enum isl_dim_type c3
, enum isl_dim_type c4
)
11296 return isl_basic_map_inequalities_matrix((isl_basic_map
*)bset
,
11297 c1
, c2
, c3
, c4
, isl_dim_in
);
11300 __isl_give isl_basic_set
*isl_basic_set_from_constraint_matrices(
11301 __isl_take isl_space
*dim
,
11302 __isl_take isl_mat
*eq
, __isl_take isl_mat
*ineq
, enum isl_dim_type c1
,
11303 enum isl_dim_type c2
, enum isl_dim_type c3
, enum isl_dim_type c4
)
11305 return (isl_basic_set
*)
11306 isl_basic_map_from_constraint_matrices(dim
, eq
, ineq
,
11307 c1
, c2
, c3
, c4
, isl_dim_in
);
11310 int isl_basic_map_can_zip(__isl_keep isl_basic_map
*bmap
)
11315 return isl_space_can_zip(bmap
->dim
);
11318 int isl_map_can_zip(__isl_keep isl_map
*map
)
11323 return isl_space_can_zip(map
->dim
);
11326 /* Given a basic map (A -> B) -> (C -> D), return the corresponding basic map
11327 * (A -> C) -> (B -> D).
11329 __isl_give isl_basic_map
*isl_basic_map_zip(__isl_take isl_basic_map
*bmap
)
11338 if (!isl_basic_map_can_zip(bmap
))
11339 isl_die(bmap
->ctx
, isl_error_invalid
,
11340 "basic map cannot be zipped", goto error
);
11341 pos
= isl_basic_map_offset(bmap
, isl_dim_in
) +
11342 isl_space_dim(bmap
->dim
->nested
[0], isl_dim_in
);
11343 n1
= isl_space_dim(bmap
->dim
->nested
[0], isl_dim_out
);
11344 n2
= isl_space_dim(bmap
->dim
->nested
[1], isl_dim_in
);
11345 bmap
= isl_basic_map_cow(bmap
);
11346 bmap
= isl_basic_map_swap_vars(bmap
, pos
, n1
, n2
);
11349 bmap
->dim
= isl_space_zip(bmap
->dim
);
11354 isl_basic_map_free(bmap
);
11358 /* Given a map (A -> B) -> (C -> D), return the corresponding map
11359 * (A -> C) -> (B -> D).
11361 __isl_give isl_map
*isl_map_zip(__isl_take isl_map
*map
)
11368 if (!isl_map_can_zip(map
))
11369 isl_die(map
->ctx
, isl_error_invalid
, "map cannot be zipped",
11372 map
= isl_map_cow(map
);
11376 for (i
= 0; i
< map
->n
; ++i
) {
11377 map
->p
[i
] = isl_basic_map_zip(map
->p
[i
]);
11382 map
->dim
= isl_space_zip(map
->dim
);
11392 /* Can we apply isl_basic_map_curry to "bmap"?
11393 * That is, does it have a nested relation in its domain?
11395 int isl_basic_map_can_curry(__isl_keep isl_basic_map
*bmap
)
11400 return isl_space_can_curry(bmap
->dim
);
11403 /* Can we apply isl_map_curry to "map"?
11404 * That is, does it have a nested relation in its domain?
11406 int isl_map_can_curry(__isl_keep isl_map
*map
)
11411 return isl_space_can_curry(map
->dim
);
11414 /* Given a basic map (A -> B) -> C, return the corresponding basic map
11417 __isl_give isl_basic_map
*isl_basic_map_curry(__isl_take isl_basic_map
*bmap
)
11423 if (!isl_basic_map_can_curry(bmap
))
11424 isl_die(bmap
->ctx
, isl_error_invalid
,
11425 "basic map cannot be curried", goto error
);
11426 bmap
= isl_basic_map_cow(bmap
);
11429 bmap
->dim
= isl_space_curry(bmap
->dim
);
11434 isl_basic_map_free(bmap
);
11438 /* Given a map (A -> B) -> C, return the corresponding map
11441 __isl_give isl_map
*isl_map_curry(__isl_take isl_map
*map
)
11448 if (!isl_map_can_curry(map
))
11449 isl_die(map
->ctx
, isl_error_invalid
, "map cannot be curried",
11452 map
= isl_map_cow(map
);
11456 for (i
= 0; i
< map
->n
; ++i
) {
11457 map
->p
[i
] = isl_basic_map_curry(map
->p
[i
]);
11462 map
->dim
= isl_space_curry(map
->dim
);
11472 /* Can we apply isl_basic_map_uncurry to "bmap"?
11473 * That is, does it have a nested relation in its domain?
11475 int isl_basic_map_can_uncurry(__isl_keep isl_basic_map
*bmap
)
11480 return isl_space_can_uncurry(bmap
->dim
);
11483 /* Can we apply isl_map_uncurry to "map"?
11484 * That is, does it have a nested relation in its domain?
11486 int isl_map_can_uncurry(__isl_keep isl_map
*map
)
11491 return isl_space_can_uncurry(map
->dim
);
11494 /* Given a basic map A -> (B -> C), return the corresponding basic map
11497 __isl_give isl_basic_map
*isl_basic_map_uncurry(__isl_take isl_basic_map
*bmap
)
11503 if (!isl_basic_map_can_uncurry(bmap
))
11504 isl_die(bmap
->ctx
, isl_error_invalid
,
11505 "basic map cannot be uncurried",
11506 return isl_basic_map_free(bmap
));
11507 bmap
= isl_basic_map_cow(bmap
);
11510 bmap
->dim
= isl_space_uncurry(bmap
->dim
);
11512 return isl_basic_map_free(bmap
);
11516 /* Given a map A -> (B -> C), return the corresponding map
11519 __isl_give isl_map
*isl_map_uncurry(__isl_take isl_map
*map
)
11526 if (!isl_map_can_uncurry(map
))
11527 isl_die(map
->ctx
, isl_error_invalid
, "map cannot be uncurried",
11528 return isl_map_free(map
));
11530 map
= isl_map_cow(map
);
11534 for (i
= 0; i
< map
->n
; ++i
) {
11535 map
->p
[i
] = isl_basic_map_uncurry(map
->p
[i
]);
11537 return isl_map_free(map
);
11540 map
->dim
= isl_space_uncurry(map
->dim
);
11542 return isl_map_free(map
);
11547 /* Construct a basic map mapping the domain of the affine expression
11548 * to a one-dimensional range prescribed by the affine expression.
11550 __isl_give isl_basic_map
*isl_basic_map_from_aff(__isl_take isl_aff
*aff
)
11554 isl_local_space
*ls
;
11555 isl_basic_map
*bmap
;
11560 ls
= isl_aff_get_local_space(aff
);
11561 bmap
= isl_basic_map_from_local_space(ls
);
11562 bmap
= isl_basic_map_extend_constraints(bmap
, 1, 0);
11563 k
= isl_basic_map_alloc_equality(bmap
);
11567 pos
= isl_basic_map_offset(bmap
, isl_dim_out
);
11568 isl_seq_cpy(bmap
->eq
[k
], aff
->v
->el
+ 1, pos
);
11569 isl_int_neg(bmap
->eq
[k
][pos
], aff
->v
->el
[0]);
11570 isl_seq_cpy(bmap
->eq
[k
] + pos
+ 1, aff
->v
->el
+ 1 + pos
,
11571 aff
->v
->size
- (pos
+ 1));
11574 bmap
= isl_basic_map_finalize(bmap
);
11578 isl_basic_map_free(bmap
);
11582 /* Construct a map mapping the domain of the affine expression
11583 * to a one-dimensional range prescribed by the affine expression.
11585 __isl_give isl_map
*isl_map_from_aff(__isl_take isl_aff
*aff
)
11587 isl_basic_map
*bmap
;
11589 bmap
= isl_basic_map_from_aff(aff
);
11590 return isl_map_from_basic_map(bmap
);
11593 /* Construct a basic map mapping the domain the multi-affine expression
11594 * to its range, with each dimension in the range equated to the
11595 * corresponding affine expression.
11597 __isl_give isl_basic_map
*isl_basic_map_from_multi_aff(
11598 __isl_take isl_multi_aff
*maff
)
11602 isl_basic_map
*bmap
;
11607 if (isl_space_dim(maff
->space
, isl_dim_out
) != maff
->n
)
11608 isl_die(isl_multi_aff_get_ctx(maff
), isl_error_internal
,
11609 "invalid space", goto error
);
11611 space
= isl_space_domain(isl_multi_aff_get_space(maff
));
11612 bmap
= isl_basic_map_universe(isl_space_from_domain(space
));
11614 for (i
= 0; i
< maff
->n
; ++i
) {
11616 isl_basic_map
*bmap_i
;
11618 aff
= isl_aff_copy(maff
->p
[i
]);
11619 bmap_i
= isl_basic_map_from_aff(aff
);
11621 bmap
= isl_basic_map_flat_range_product(bmap
, bmap_i
);
11624 bmap
= isl_basic_map_reset_space(bmap
, isl_multi_aff_get_space(maff
));
11626 isl_multi_aff_free(maff
);
11629 isl_multi_aff_free(maff
);
11633 /* Construct a map mapping the domain the multi-affine expression
11634 * to its range, with each dimension in the range equated to the
11635 * corresponding affine expression.
11637 __isl_give isl_map
*isl_map_from_multi_aff(__isl_take isl_multi_aff
*maff
)
11639 isl_basic_map
*bmap
;
11641 bmap
= isl_basic_map_from_multi_aff(maff
);
11642 return isl_map_from_basic_map(bmap
);
11645 /* Construct a basic map mapping a domain in the given space to
11646 * to an n-dimensional range, with n the number of elements in the list,
11647 * where each coordinate in the range is prescribed by the
11648 * corresponding affine expression.
11649 * The domains of all affine expressions in the list are assumed to match
11652 __isl_give isl_basic_map
*isl_basic_map_from_aff_list(
11653 __isl_take isl_space
*domain_dim
, __isl_take isl_aff_list
*list
)
11657 isl_basic_map
*bmap
;
11662 dim
= isl_space_from_domain(domain_dim
);
11663 bmap
= isl_basic_map_universe(dim
);
11665 for (i
= 0; i
< list
->n
; ++i
) {
11667 isl_basic_map
*bmap_i
;
11669 aff
= isl_aff_copy(list
->p
[i
]);
11670 bmap_i
= isl_basic_map_from_aff(aff
);
11672 bmap
= isl_basic_map_flat_range_product(bmap
, bmap_i
);
11675 isl_aff_list_free(list
);
11679 __isl_give isl_set
*isl_set_equate(__isl_take isl_set
*set
,
11680 enum isl_dim_type type1
, int pos1
, enum isl_dim_type type2
, int pos2
)
11682 return isl_map_equate(set
, type1
, pos1
, type2
, pos2
);
11685 /* Construct a basic map where the given dimensions are equal to each other.
11687 static __isl_give isl_basic_map
*equator(__isl_take isl_space
*space
,
11688 enum isl_dim_type type1
, int pos1
, enum isl_dim_type type2
, int pos2
)
11690 isl_basic_map
*bmap
= NULL
;
11696 if (pos1
>= isl_space_dim(space
, type1
))
11697 isl_die(isl_space_get_ctx(space
), isl_error_invalid
,
11698 "index out of bounds", goto error
);
11699 if (pos2
>= isl_space_dim(space
, type2
))
11700 isl_die(isl_space_get_ctx(space
), isl_error_invalid
,
11701 "index out of bounds", goto error
);
11703 if (type1
== type2
&& pos1
== pos2
)
11704 return isl_basic_map_universe(space
);
11706 bmap
= isl_basic_map_alloc_space(isl_space_copy(space
), 0, 1, 0);
11707 i
= isl_basic_map_alloc_equality(bmap
);
11710 isl_seq_clr(bmap
->eq
[i
], 1 + isl_basic_map_total_dim(bmap
));
11711 pos1
+= isl_basic_map_offset(bmap
, type1
);
11712 pos2
+= isl_basic_map_offset(bmap
, type2
);
11713 isl_int_set_si(bmap
->eq
[i
][pos1
], -1);
11714 isl_int_set_si(bmap
->eq
[i
][pos2
], 1);
11715 bmap
= isl_basic_map_finalize(bmap
);
11716 isl_space_free(space
);
11719 isl_space_free(space
);
11720 isl_basic_map_free(bmap
);
11724 /* Add a constraint imposing that the given two dimensions are equal.
11726 __isl_give isl_basic_map
*isl_basic_map_equate(__isl_take isl_basic_map
*bmap
,
11727 enum isl_dim_type type1
, int pos1
, enum isl_dim_type type2
, int pos2
)
11731 eq
= equator(isl_basic_map_get_space(bmap
), type1
, pos1
, type2
, pos2
);
11733 bmap
= isl_basic_map_intersect(bmap
, eq
);
11738 /* Add a constraint imposing that the given two dimensions are equal.
11740 __isl_give isl_map
*isl_map_equate(__isl_take isl_map
*map
,
11741 enum isl_dim_type type1
, int pos1
, enum isl_dim_type type2
, int pos2
)
11743 isl_basic_map
*bmap
;
11745 bmap
= equator(isl_map_get_space(map
), type1
, pos1
, type2
, pos2
);
11747 map
= isl_map_intersect(map
, isl_map_from_basic_map(bmap
));
11752 /* Add a constraint imposing that the given two dimensions have opposite values.
11754 __isl_give isl_map
*isl_map_oppose(__isl_take isl_map
*map
,
11755 enum isl_dim_type type1
, int pos1
, enum isl_dim_type type2
, int pos2
)
11757 isl_basic_map
*bmap
= NULL
;
11763 if (pos1
>= isl_map_dim(map
, type1
))
11764 isl_die(map
->ctx
, isl_error_invalid
,
11765 "index out of bounds", goto error
);
11766 if (pos2
>= isl_map_dim(map
, type2
))
11767 isl_die(map
->ctx
, isl_error_invalid
,
11768 "index out of bounds", goto error
);
11770 bmap
= isl_basic_map_alloc_space(isl_map_get_space(map
), 0, 1, 0);
11771 i
= isl_basic_map_alloc_equality(bmap
);
11774 isl_seq_clr(bmap
->eq
[i
], 1 + isl_basic_map_total_dim(bmap
));
11775 pos1
+= isl_basic_map_offset(bmap
, type1
);
11776 pos2
+= isl_basic_map_offset(bmap
, type2
);
11777 isl_int_set_si(bmap
->eq
[i
][pos1
], 1);
11778 isl_int_set_si(bmap
->eq
[i
][pos2
], 1);
11779 bmap
= isl_basic_map_finalize(bmap
);
11781 map
= isl_map_intersect(map
, isl_map_from_basic_map(bmap
));
11785 isl_basic_map_free(bmap
);
11790 /* Construct a constraint imposing that the value of the first dimension is
11791 * greater than or equal to that of the second.
11793 static __isl_give isl_constraint
*constraint_order_ge(
11794 __isl_take isl_space
*space
, enum isl_dim_type type1
, int pos1
,
11795 enum isl_dim_type type2
, int pos2
)
11802 c
= isl_inequality_alloc(isl_local_space_from_space(space
));
11804 if (pos1
>= isl_constraint_dim(c
, type1
))
11805 isl_die(isl_constraint_get_ctx(c
), isl_error_invalid
,
11806 "index out of bounds", return isl_constraint_free(c
));
11807 if (pos2
>= isl_constraint_dim(c
, type2
))
11808 isl_die(isl_constraint_get_ctx(c
), isl_error_invalid
,
11809 "index out of bounds", return isl_constraint_free(c
));
11811 if (type1
== type2
&& pos1
== pos2
)
11814 c
= isl_constraint_set_coefficient_si(c
, type1
, pos1
, 1);
11815 c
= isl_constraint_set_coefficient_si(c
, type2
, pos2
, -1);
11820 /* Add a constraint imposing that the value of the first dimension is
11821 * greater than or equal to that of the second.
11823 __isl_give isl_basic_map
*isl_basic_map_order_ge(__isl_take isl_basic_map
*bmap
,
11824 enum isl_dim_type type1
, int pos1
, enum isl_dim_type type2
, int pos2
)
11829 if (type1
== type2
&& pos1
== pos2
)
11831 space
= isl_basic_map_get_space(bmap
);
11832 c
= constraint_order_ge(space
, type1
, pos1
, type2
, pos2
);
11833 bmap
= isl_basic_map_add_constraint(bmap
, c
);
11838 /* Add a constraint imposing that the value of the first dimension is
11839 * greater than or equal to that of the second.
11841 __isl_give isl_map
*isl_map_order_ge(__isl_take isl_map
*map
,
11842 enum isl_dim_type type1
, int pos1
, enum isl_dim_type type2
, int pos2
)
11847 if (type1
== type2
&& pos1
== pos2
)
11849 space
= isl_map_get_space(map
);
11850 c
= constraint_order_ge(space
, type1
, pos1
, type2
, pos2
);
11851 map
= isl_map_add_constraint(map
, c
);
11856 /* Add a constraint imposing that the value of the first dimension is
11857 * less than or equal to that of the second.
11859 __isl_give isl_map
*isl_map_order_le(__isl_take isl_map
*map
,
11860 enum isl_dim_type type1
, int pos1
, enum isl_dim_type type2
, int pos2
)
11862 return isl_map_order_ge(map
, type2
, pos2
, type1
, pos1
);
11865 /* Construct a basic map where the value of the first dimension is
11866 * greater than that of the second.
11868 static __isl_give isl_basic_map
*greator(__isl_take isl_space
*space
,
11869 enum isl_dim_type type1
, int pos1
, enum isl_dim_type type2
, int pos2
)
11871 isl_basic_map
*bmap
= NULL
;
11877 if (pos1
>= isl_space_dim(space
, type1
))
11878 isl_die(isl_space_get_ctx(space
), isl_error_invalid
,
11879 "index out of bounds", goto error
);
11880 if (pos2
>= isl_space_dim(space
, type2
))
11881 isl_die(isl_space_get_ctx(space
), isl_error_invalid
,
11882 "index out of bounds", goto error
);
11884 if (type1
== type2
&& pos1
== pos2
)
11885 return isl_basic_map_empty(space
);
11887 bmap
= isl_basic_map_alloc_space(space
, 0, 0, 1);
11888 i
= isl_basic_map_alloc_inequality(bmap
);
11890 return isl_basic_map_free(bmap
);
11891 isl_seq_clr(bmap
->ineq
[i
], 1 + isl_basic_map_total_dim(bmap
));
11892 pos1
+= isl_basic_map_offset(bmap
, type1
);
11893 pos2
+= isl_basic_map_offset(bmap
, type2
);
11894 isl_int_set_si(bmap
->ineq
[i
][pos1
], 1);
11895 isl_int_set_si(bmap
->ineq
[i
][pos2
], -1);
11896 isl_int_set_si(bmap
->ineq
[i
][0], -1);
11897 bmap
= isl_basic_map_finalize(bmap
);
11901 isl_space_free(space
);
11902 isl_basic_map_free(bmap
);
11906 /* Add a constraint imposing that the value of the first dimension is
11907 * greater than that of the second.
11909 __isl_give isl_basic_map
*isl_basic_map_order_gt(__isl_take isl_basic_map
*bmap
,
11910 enum isl_dim_type type1
, int pos1
, enum isl_dim_type type2
, int pos2
)
11914 gt
= greator(isl_basic_map_get_space(bmap
), type1
, pos1
, type2
, pos2
);
11916 bmap
= isl_basic_map_intersect(bmap
, gt
);
11921 /* Add a constraint imposing that the value of the first dimension is
11922 * greater than that of the second.
11924 __isl_give isl_map
*isl_map_order_gt(__isl_take isl_map
*map
,
11925 enum isl_dim_type type1
, int pos1
, enum isl_dim_type type2
, int pos2
)
11927 isl_basic_map
*bmap
;
11929 bmap
= greator(isl_map_get_space(map
), type1
, pos1
, type2
, pos2
);
11931 map
= isl_map_intersect(map
, isl_map_from_basic_map(bmap
));
11936 /* Add a constraint imposing that the value of the first dimension is
11937 * smaller than that of the second.
11939 __isl_give isl_map
*isl_map_order_lt(__isl_take isl_map
*map
,
11940 enum isl_dim_type type1
, int pos1
, enum isl_dim_type type2
, int pos2
)
11942 return isl_map_order_gt(map
, type2
, pos2
, type1
, pos1
);
11945 __isl_give isl_aff
*isl_basic_map_get_div(__isl_keep isl_basic_map
*bmap
,
11949 isl_local_space
*ls
;
11954 if (!isl_basic_map_divs_known(bmap
))
11955 isl_die(isl_basic_map_get_ctx(bmap
), isl_error_invalid
,
11956 "some divs are unknown", return NULL
);
11958 ls
= isl_basic_map_get_local_space(bmap
);
11959 div
= isl_local_space_get_div(ls
, pos
);
11960 isl_local_space_free(ls
);
11965 __isl_give isl_aff
*isl_basic_set_get_div(__isl_keep isl_basic_set
*bset
,
11968 return isl_basic_map_get_div(bset
, pos
);
11971 /* Plug in "subs" for dimension "type", "pos" of "bset".
11973 * Let i be the dimension to replace and let "subs" be of the form
11977 * Any integer division with a non-zero coefficient for i,
11979 * floor((a i + g)/m)
11983 * floor((a f + d g)/(m d))
11985 * Constraints of the form
11993 * We currently require that "subs" is an integral expression.
11994 * Handling rational expressions may require us to add stride constraints
11995 * as we do in isl_basic_set_preimage_multi_aff.
11997 __isl_give isl_basic_set
*isl_basic_set_substitute(
11998 __isl_take isl_basic_set
*bset
,
11999 enum isl_dim_type type
, unsigned pos
, __isl_keep isl_aff
*subs
)
12005 if (bset
&& isl_basic_set_plain_is_empty(bset
))
12008 bset
= isl_basic_set_cow(bset
);
12009 if (!bset
|| !subs
)
12012 ctx
= isl_basic_set_get_ctx(bset
);
12013 if (!isl_space_is_equal(bset
->dim
, subs
->ls
->dim
))
12014 isl_die(ctx
, isl_error_invalid
,
12015 "spaces don't match", goto error
);
12016 if (isl_local_space_dim(subs
->ls
, isl_dim_div
) != 0)
12017 isl_die(ctx
, isl_error_unsupported
,
12018 "cannot handle divs yet", goto error
);
12019 if (!isl_int_is_one(subs
->v
->el
[0]))
12020 isl_die(ctx
, isl_error_invalid
,
12021 "can only substitute integer expressions", goto error
);
12023 pos
+= isl_basic_set_offset(bset
, type
);
12027 for (i
= 0; i
< bset
->n_eq
; ++i
) {
12028 if (isl_int_is_zero(bset
->eq
[i
][pos
]))
12030 isl_int_set(v
, bset
->eq
[i
][pos
]);
12031 isl_int_set_si(bset
->eq
[i
][pos
], 0);
12032 isl_seq_combine(bset
->eq
[i
], subs
->v
->el
[0], bset
->eq
[i
],
12033 v
, subs
->v
->el
+ 1, subs
->v
->size
- 1);
12036 for (i
= 0; i
< bset
->n_ineq
; ++i
) {
12037 if (isl_int_is_zero(bset
->ineq
[i
][pos
]))
12039 isl_int_set(v
, bset
->ineq
[i
][pos
]);
12040 isl_int_set_si(bset
->ineq
[i
][pos
], 0);
12041 isl_seq_combine(bset
->ineq
[i
], subs
->v
->el
[0], bset
->ineq
[i
],
12042 v
, subs
->v
->el
+ 1, subs
->v
->size
- 1);
12045 for (i
= 0; i
< bset
->n_div
; ++i
) {
12046 if (isl_int_is_zero(bset
->div
[i
][1 + pos
]))
12048 isl_int_set(v
, bset
->div
[i
][1 + pos
]);
12049 isl_int_set_si(bset
->div
[i
][1 + pos
], 0);
12050 isl_seq_combine(bset
->div
[i
] + 1,
12051 subs
->v
->el
[0], bset
->div
[i
] + 1,
12052 v
, subs
->v
->el
+ 1, subs
->v
->size
- 1);
12053 isl_int_mul(bset
->div
[i
][0], bset
->div
[i
][0], subs
->v
->el
[0]);
12058 bset
= isl_basic_set_simplify(bset
);
12059 return isl_basic_set_finalize(bset
);
12061 isl_basic_set_free(bset
);
12065 /* Plug in "subs" for dimension "type", "pos" of "set".
12067 __isl_give isl_set
*isl_set_substitute(__isl_take isl_set
*set
,
12068 enum isl_dim_type type
, unsigned pos
, __isl_keep isl_aff
*subs
)
12072 if (set
&& isl_set_plain_is_empty(set
))
12075 set
= isl_set_cow(set
);
12079 for (i
= set
->n
- 1; i
>= 0; --i
) {
12080 set
->p
[i
] = isl_basic_set_substitute(set
->p
[i
], type
, pos
, subs
);
12081 if (remove_if_empty(set
, i
) < 0)
12091 /* Check if the range of "ma" is compatible with the domain or range
12092 * (depending on "type") of "bmap".
12093 * Return -1 if anything is wrong.
12095 static int check_basic_map_compatible_range_multi_aff(
12096 __isl_keep isl_basic_map
*bmap
, enum isl_dim_type type
,
12097 __isl_keep isl_multi_aff
*ma
)
12100 isl_space
*ma_space
;
12102 ma_space
= isl_multi_aff_get_space(ma
);
12104 m
= isl_space_match(bmap
->dim
, isl_dim_param
, ma_space
, isl_dim_param
);
12108 isl_die(isl_basic_map_get_ctx(bmap
), isl_error_invalid
,
12109 "parameters don't match", goto error
);
12110 m
= isl_space_tuple_is_equal(bmap
->dim
, type
, ma_space
, isl_dim_out
);
12114 isl_die(isl_basic_map_get_ctx(bmap
), isl_error_invalid
,
12115 "spaces don't match", goto error
);
12117 isl_space_free(ma_space
);
12120 isl_space_free(ma_space
);
12124 /* Copy the divs from "ma" to "bmap", adding zeros for the "n_before"
12125 * coefficients before the transformed range of dimensions,
12126 * the "n_after" coefficients after the transformed range of dimensions
12127 * and the coefficients of the other divs in "bmap".
12129 static int set_ma_divs(__isl_keep isl_basic_map
*bmap
,
12130 __isl_keep isl_multi_aff
*ma
, int n_before
, int n_after
, int n_div
)
12135 isl_local_space
*ls
;
12140 ls
= isl_aff_get_domain_local_space(ma
->p
[0]);
12144 n_param
= isl_local_space_dim(ls
, isl_dim_param
);
12145 n_set
= isl_local_space_dim(ls
, isl_dim_set
);
12146 for (i
= 0; i
< n_div
; ++i
) {
12147 int o_bmap
= 0, o_ls
= 0;
12149 isl_seq_cpy(bmap
->div
[i
], ls
->div
->row
[i
], 1 + 1 + n_param
);
12150 o_bmap
+= 1 + 1 + n_param
;
12151 o_ls
+= 1 + 1 + n_param
;
12152 isl_seq_clr(bmap
->div
[i
] + o_bmap
, n_before
);
12153 o_bmap
+= n_before
;
12154 isl_seq_cpy(bmap
->div
[i
] + o_bmap
,
12155 ls
->div
->row
[i
] + o_ls
, n_set
);
12158 isl_seq_clr(bmap
->div
[i
] + o_bmap
, n_after
);
12160 isl_seq_cpy(bmap
->div
[i
] + o_bmap
,
12161 ls
->div
->row
[i
] + o_ls
, n_div
);
12164 isl_seq_clr(bmap
->div
[i
] + o_bmap
, bmap
->n_div
- n_div
);
12165 if (isl_basic_set_add_div_constraints(bmap
, i
) < 0)
12169 isl_local_space_free(ls
);
12172 isl_local_space_free(ls
);
12176 /* How many stride constraints does "ma" enforce?
12177 * That is, how many of the affine expressions have a denominator
12178 * different from one?
12180 static int multi_aff_strides(__isl_keep isl_multi_aff
*ma
)
12185 for (i
= 0; i
< ma
->n
; ++i
)
12186 if (!isl_int_is_one(ma
->p
[i
]->v
->el
[0]))
12192 /* For each affine expression in ma of the form
12194 * x_i = (f_i y + h_i)/m_i
12196 * with m_i different from one, add a constraint to "bmap"
12199 * f_i y + h_i = m_i alpha_i
12201 * with alpha_i an additional existentially quantified variable.
12203 static __isl_give isl_basic_map
*add_ma_strides(
12204 __isl_take isl_basic_map
*bmap
, __isl_keep isl_multi_aff
*ma
,
12205 int n_before
, int n_after
)
12214 total
= isl_basic_map_total_dim(bmap
);
12215 n_param
= isl_multi_aff_dim(ma
, isl_dim_param
);
12216 n_in
= isl_multi_aff_dim(ma
, isl_dim_in
);
12217 n_div
= isl_multi_aff_dim(ma
, isl_dim_div
);
12218 for (i
= 0; i
< ma
->n
; ++i
) {
12219 int o_bmap
= 0, o_ma
= 1;
12221 if (isl_int_is_one(ma
->p
[i
]->v
->el
[0]))
12223 div
= isl_basic_map_alloc_div(bmap
);
12224 k
= isl_basic_map_alloc_equality(bmap
);
12225 if (div
< 0 || k
< 0)
12227 isl_int_set_si(bmap
->div
[div
][0], 0);
12228 isl_seq_cpy(bmap
->eq
[k
] + o_bmap
,
12229 ma
->p
[i
]->v
->el
+ o_ma
, 1 + n_param
);
12230 o_bmap
+= 1 + n_param
;
12231 o_ma
+= 1 + n_param
;
12232 isl_seq_clr(bmap
->eq
[k
] + o_bmap
, n_before
);
12233 o_bmap
+= n_before
;
12234 isl_seq_cpy(bmap
->eq
[k
] + o_bmap
,
12235 ma
->p
[i
]->v
->el
+ o_ma
, n_in
);
12238 isl_seq_clr(bmap
->eq
[k
] + o_bmap
, n_after
);
12240 isl_seq_cpy(bmap
->eq
[k
] + o_bmap
,
12241 ma
->p
[i
]->v
->el
+ o_ma
, n_div
);
12244 isl_seq_clr(bmap
->eq
[k
] + o_bmap
, 1 + total
- o_bmap
);
12245 isl_int_neg(bmap
->eq
[k
][1 + total
], ma
->p
[i
]->v
->el
[0]);
12251 isl_basic_map_free(bmap
);
12255 /* Replace the domain or range space (depending on "type) of "space" by "set".
12257 static __isl_give isl_space
*isl_space_set(__isl_take isl_space
*space
,
12258 enum isl_dim_type type
, __isl_take isl_space
*set
)
12260 if (type
== isl_dim_in
) {
12261 space
= isl_space_range(space
);
12262 space
= isl_space_map_from_domain_and_range(set
, space
);
12264 space
= isl_space_domain(space
);
12265 space
= isl_space_map_from_domain_and_range(space
, set
);
12271 /* Compute the preimage of the domain or range (depending on "type")
12272 * of "bmap" under the function represented by "ma".
12273 * In other words, plug in "ma" in the domain or range of "bmap".
12274 * The result is a basic map that lives in the same space as "bmap"
12275 * except that the domain or range has been replaced by
12276 * the domain space of "ma".
12278 * If bmap is represented by
12280 * A(p) + S u + B x + T v + C(divs) >= 0,
12282 * where u and x are input and output dimensions if type == isl_dim_out
12283 * while x and v are input and output dimensions if type == isl_dim_in,
12284 * and ma is represented by
12286 * x = D(p) + F(y) + G(divs')
12288 * then the result is
12290 * A(p) + B D(p) + S u + B F(y) + T v + B G(divs') + C(divs) >= 0
12292 * The divs in the input set are similarly adjusted.
12295 * floor((a_i(p) + s u + b_i x + t v + c_i(divs))/n_i)
12299 * floor((a_i(p) + b_i D(p) + s u + b_i F(y) + t v +
12300 * B_i G(divs') + c_i(divs))/n_i)
12302 * If bmap is not a rational map and if F(y) involves any denominators
12304 * x_i = (f_i y + h_i)/m_i
12306 * then additional constraints are added to ensure that we only
12307 * map back integer points. That is we enforce
12309 * f_i y + h_i = m_i alpha_i
12311 * with alpha_i an additional existentially quantified variable.
12313 * We first copy over the divs from "ma".
12314 * Then we add the modified constraints and divs from "bmap".
12315 * Finally, we add the stride constraints, if needed.
12317 __isl_give isl_basic_map
*isl_basic_map_preimage_multi_aff(
12318 __isl_take isl_basic_map
*bmap
, enum isl_dim_type type
,
12319 __isl_take isl_multi_aff
*ma
)
12323 isl_basic_map
*res
= NULL
;
12324 int n_before
, n_after
, n_div_bmap
, n_div_ma
;
12325 isl_int f
, c1
, c2
, g
;
12326 int rational
, strides
;
12333 ma
= isl_multi_aff_align_divs(ma
);
12336 if (check_basic_map_compatible_range_multi_aff(bmap
, type
, ma
) < 0)
12339 if (type
== isl_dim_in
) {
12341 n_after
= isl_basic_map_dim(bmap
, isl_dim_out
);
12343 n_before
= isl_basic_map_dim(bmap
, isl_dim_in
);
12346 n_div_bmap
= isl_basic_map_dim(bmap
, isl_dim_div
);
12347 n_div_ma
= ma
->n
? isl_aff_dim(ma
->p
[0], isl_dim_div
) : 0;
12349 space
= isl_multi_aff_get_domain_space(ma
);
12350 space
= isl_space_set(isl_basic_map_get_space(bmap
), type
, space
);
12351 rational
= isl_basic_map_is_rational(bmap
);
12352 strides
= rational
? 0 : multi_aff_strides(ma
);
12353 res
= isl_basic_map_alloc_space(space
, n_div_ma
+ n_div_bmap
+ strides
,
12354 bmap
->n_eq
+ strides
, bmap
->n_ineq
+ 2 * n_div_ma
);
12356 res
= isl_basic_map_set_rational(res
);
12358 for (i
= 0; i
< n_div_ma
+ n_div_bmap
; ++i
)
12359 if (isl_basic_map_alloc_div(res
) < 0)
12362 if (set_ma_divs(res
, ma
, n_before
, n_after
, n_div_ma
) < 0)
12365 for (i
= 0; i
< bmap
->n_eq
; ++i
) {
12366 k
= isl_basic_map_alloc_equality(res
);
12369 isl_seq_preimage(res
->eq
[k
], bmap
->eq
[i
], ma
, n_before
,
12370 n_after
, n_div_ma
, n_div_bmap
, f
, c1
, c2
, g
, 0);
12373 for (i
= 0; i
< bmap
->n_ineq
; ++i
) {
12374 k
= isl_basic_map_alloc_inequality(res
);
12377 isl_seq_preimage(res
->ineq
[k
], bmap
->ineq
[i
], ma
, n_before
,
12378 n_after
, n_div_ma
, n_div_bmap
, f
, c1
, c2
, g
, 0);
12381 for (i
= 0; i
< bmap
->n_div
; ++i
) {
12382 if (isl_int_is_zero(bmap
->div
[i
][0])) {
12383 isl_int_set_si(res
->div
[n_div_ma
+ i
][0], 0);
12386 isl_seq_preimage(res
->div
[n_div_ma
+ i
], bmap
->div
[i
], ma
,
12387 n_before
, n_after
, n_div_ma
, n_div_bmap
,
12392 res
= add_ma_strides(res
, ma
, n_before
, n_after
);
12398 isl_basic_map_free(bmap
);
12399 isl_multi_aff_free(ma
);
12400 res
= isl_basic_set_simplify(res
);
12401 return isl_basic_map_finalize(res
);
12407 isl_basic_map_free(bmap
);
12408 isl_multi_aff_free(ma
);
12409 isl_basic_map_free(res
);
12413 /* Compute the preimage of "bset" under the function represented by "ma".
12414 * In other words, plug in "ma" in "bset". The result is a basic set
12415 * that lives in the domain space of "ma".
12417 __isl_give isl_basic_set
*isl_basic_set_preimage_multi_aff(
12418 __isl_take isl_basic_set
*bset
, __isl_take isl_multi_aff
*ma
)
12420 return isl_basic_map_preimage_multi_aff(bset
, isl_dim_set
, ma
);
12423 /* Compute the preimage of the domain of "bmap" under the function
12424 * represented by "ma".
12425 * In other words, plug in "ma" in the domain of "bmap".
12426 * The result is a basic map that lives in the same space as "bmap"
12427 * except that the domain has been replaced by the domain space of "ma".
12429 __isl_give isl_basic_map
*isl_basic_map_preimage_domain_multi_aff(
12430 __isl_take isl_basic_map
*bmap
, __isl_take isl_multi_aff
*ma
)
12432 return isl_basic_map_preimage_multi_aff(bmap
, isl_dim_in
, ma
);
12435 /* Compute the preimage of the range of "bmap" under the function
12436 * represented by "ma".
12437 * In other words, plug in "ma" in the range of "bmap".
12438 * The result is a basic map that lives in the same space as "bmap"
12439 * except that the range has been replaced by the domain space of "ma".
12441 __isl_give isl_basic_map
*isl_basic_map_preimage_range_multi_aff(
12442 __isl_take isl_basic_map
*bmap
, __isl_take isl_multi_aff
*ma
)
12444 return isl_basic_map_preimage_multi_aff(bmap
, isl_dim_out
, ma
);
12447 /* Check if the range of "ma" is compatible with the domain or range
12448 * (depending on "type") of "map".
12449 * Return -1 if anything is wrong.
12451 static int check_map_compatible_range_multi_aff(
12452 __isl_keep isl_map
*map
, enum isl_dim_type type
,
12453 __isl_keep isl_multi_aff
*ma
)
12456 isl_space
*ma_space
;
12458 ma_space
= isl_multi_aff_get_space(ma
);
12459 m
= isl_space_tuple_is_equal(map
->dim
, type
, ma_space
, isl_dim_out
);
12460 isl_space_free(ma_space
);
12462 isl_die(isl_map_get_ctx(map
), isl_error_invalid
,
12463 "spaces don't match", return -1);
12467 /* Compute the preimage of the domain or range (depending on "type")
12468 * of "map" under the function represented by "ma".
12469 * In other words, plug in "ma" in the domain or range of "map".
12470 * The result is a map that lives in the same space as "map"
12471 * except that the domain or range has been replaced by
12472 * the domain space of "ma".
12474 * The parameters are assumed to have been aligned.
12476 static __isl_give isl_map
*map_preimage_multi_aff(__isl_take isl_map
*map
,
12477 enum isl_dim_type type
, __isl_take isl_multi_aff
*ma
)
12482 map
= isl_map_cow(map
);
12483 ma
= isl_multi_aff_align_divs(ma
);
12486 if (check_map_compatible_range_multi_aff(map
, type
, ma
) < 0)
12489 for (i
= 0; i
< map
->n
; ++i
) {
12490 map
->p
[i
] = isl_basic_map_preimage_multi_aff(map
->p
[i
], type
,
12491 isl_multi_aff_copy(ma
));
12496 space
= isl_multi_aff_get_domain_space(ma
);
12497 space
= isl_space_set(isl_map_get_space(map
), type
, space
);
12499 isl_space_free(map
->dim
);
12504 isl_multi_aff_free(ma
);
12506 ISL_F_CLR(map
, ISL_MAP_DISJOINT
);
12507 ISL_F_CLR(map
, ISL_SET_NORMALIZED
);
12510 isl_multi_aff_free(ma
);
12515 /* Compute the preimage of the domain or range (depending on "type")
12516 * of "map" under the function represented by "ma".
12517 * In other words, plug in "ma" in the domain or range of "map".
12518 * The result is a map that lives in the same space as "map"
12519 * except that the domain or range has been replaced by
12520 * the domain space of "ma".
12522 __isl_give isl_map
*isl_map_preimage_multi_aff(__isl_take isl_map
*map
,
12523 enum isl_dim_type type
, __isl_take isl_multi_aff
*ma
)
12528 if (isl_space_match(map
->dim
, isl_dim_param
, ma
->space
, isl_dim_param
))
12529 return map_preimage_multi_aff(map
, type
, ma
);
12531 if (!isl_space_has_named_params(map
->dim
) ||
12532 !isl_space_has_named_params(ma
->space
))
12533 isl_die(map
->ctx
, isl_error_invalid
,
12534 "unaligned unnamed parameters", goto error
);
12535 map
= isl_map_align_params(map
, isl_multi_aff_get_space(ma
));
12536 ma
= isl_multi_aff_align_params(ma
, isl_map_get_space(map
));
12538 return map_preimage_multi_aff(map
, type
, ma
);
12540 isl_multi_aff_free(ma
);
12541 return isl_map_free(map
);
12544 /* Compute the preimage of "set" under the function represented by "ma".
12545 * In other words, plug in "ma" in "set". The result is a set
12546 * that lives in the domain space of "ma".
12548 __isl_give isl_set
*isl_set_preimage_multi_aff(__isl_take isl_set
*set
,
12549 __isl_take isl_multi_aff
*ma
)
12551 return isl_map_preimage_multi_aff(set
, isl_dim_set
, ma
);
12554 /* Compute the preimage of the domain of "map" under the function
12555 * represented by "ma".
12556 * In other words, plug in "ma" in the domain of "map".
12557 * The result is a map that lives in the same space as "map"
12558 * except that the domain has been replaced by the domain space of "ma".
12560 __isl_give isl_map
*isl_map_preimage_domain_multi_aff(__isl_take isl_map
*map
,
12561 __isl_take isl_multi_aff
*ma
)
12563 return isl_map_preimage_multi_aff(map
, isl_dim_in
, ma
);
12566 /* Compute the preimage of the range of "map" under the function
12567 * represented by "ma".
12568 * In other words, plug in "ma" in the range of "map".
12569 * The result is a map that lives in the same space as "map"
12570 * except that the range has been replaced by the domain space of "ma".
12572 __isl_give isl_map
*isl_map_preimage_range_multi_aff(__isl_take isl_map
*map
,
12573 __isl_take isl_multi_aff
*ma
)
12575 return isl_map_preimage_multi_aff(map
, isl_dim_out
, ma
);
12578 /* Compute the preimage of "map" under the function represented by "pma".
12579 * In other words, plug in "pma" in the domain or range of "map".
12580 * The result is a map that lives in the same space as "map",
12581 * except that the space of type "type" has been replaced by
12582 * the domain space of "pma".
12584 * The parameters of "map" and "pma" are assumed to have been aligned.
12586 static __isl_give isl_map
*isl_map_preimage_pw_multi_aff_aligned(
12587 __isl_take isl_map
*map
, enum isl_dim_type type
,
12588 __isl_take isl_pw_multi_aff
*pma
)
12597 isl_pw_multi_aff_free(pma
);
12598 res
= isl_map_empty(isl_map_get_space(map
));
12603 res
= isl_map_preimage_multi_aff(isl_map_copy(map
), type
,
12604 isl_multi_aff_copy(pma
->p
[0].maff
));
12605 if (type
== isl_dim_in
)
12606 res
= isl_map_intersect_domain(res
,
12607 isl_map_copy(pma
->p
[0].set
));
12609 res
= isl_map_intersect_range(res
,
12610 isl_map_copy(pma
->p
[0].set
));
12612 for (i
= 1; i
< pma
->n
; ++i
) {
12615 res_i
= isl_map_preimage_multi_aff(isl_map_copy(map
), type
,
12616 isl_multi_aff_copy(pma
->p
[i
].maff
));
12617 if (type
== isl_dim_in
)
12618 res_i
= isl_map_intersect_domain(res_i
,
12619 isl_map_copy(pma
->p
[i
].set
));
12621 res_i
= isl_map_intersect_range(res_i
,
12622 isl_map_copy(pma
->p
[i
].set
));
12623 res
= isl_map_union(res
, res_i
);
12626 isl_pw_multi_aff_free(pma
);
12630 isl_pw_multi_aff_free(pma
);
12635 /* Compute the preimage of "map" under the function represented by "pma".
12636 * In other words, plug in "pma" in the domain or range of "map".
12637 * The result is a map that lives in the same space as "map",
12638 * except that the space of type "type" has been replaced by
12639 * the domain space of "pma".
12641 __isl_give isl_map
*isl_map_preimage_pw_multi_aff(__isl_take isl_map
*map
,
12642 enum isl_dim_type type
, __isl_take isl_pw_multi_aff
*pma
)
12647 if (isl_space_match(map
->dim
, isl_dim_param
, pma
->dim
, isl_dim_param
))
12648 return isl_map_preimage_pw_multi_aff_aligned(map
, type
, pma
);
12650 if (!isl_space_has_named_params(map
->dim
) ||
12651 !isl_space_has_named_params(pma
->dim
))
12652 isl_die(map
->ctx
, isl_error_invalid
,
12653 "unaligned unnamed parameters", goto error
);
12654 map
= isl_map_align_params(map
, isl_pw_multi_aff_get_space(pma
));
12655 pma
= isl_pw_multi_aff_align_params(pma
, isl_map_get_space(map
));
12657 return isl_map_preimage_pw_multi_aff_aligned(map
, type
, pma
);
12659 isl_pw_multi_aff_free(pma
);
12660 return isl_map_free(map
);
12663 /* Compute the preimage of "set" under the function represented by "pma".
12664 * In other words, plug in "pma" in "set". The result is a set
12665 * that lives in the domain space of "pma".
12667 __isl_give isl_set
*isl_set_preimage_pw_multi_aff(__isl_take isl_set
*set
,
12668 __isl_take isl_pw_multi_aff
*pma
)
12670 return isl_map_preimage_pw_multi_aff(set
, isl_dim_set
, pma
);
12673 /* Compute the preimage of the domain of "map" under the function
12674 * represented by "pma".
12675 * In other words, plug in "pma" in the domain of "map".
12676 * The result is a map that lives in the same space as "map",
12677 * except that domain space has been replaced by the domain space of "pma".
12679 __isl_give isl_map
*isl_map_preimage_domain_pw_multi_aff(
12680 __isl_take isl_map
*map
, __isl_take isl_pw_multi_aff
*pma
)
12682 return isl_map_preimage_pw_multi_aff(map
, isl_dim_in
, pma
);
12685 /* Compute the preimage of the range of "map" under the function
12686 * represented by "pma".
12687 * In other words, plug in "pma" in the range of "map".
12688 * The result is a map that lives in the same space as "map",
12689 * except that range space has been replaced by the domain space of "pma".
12691 __isl_give isl_map
*isl_map_preimage_range_pw_multi_aff(
12692 __isl_take isl_map
*map
, __isl_take isl_pw_multi_aff
*pma
)
12694 return isl_map_preimage_pw_multi_aff(map
, isl_dim_out
, pma
);
12697 /* Compute the preimage of "map" under the function represented by "mpa".
12698 * In other words, plug in "mpa" in the domain or range of "map".
12699 * The result is a map that lives in the same space as "map",
12700 * except that the space of type "type" has been replaced by
12701 * the domain space of "mpa".
12703 * If the map does not involve any constraints that refer to the
12704 * dimensions of the substituted space, then the only possible
12705 * effect of "mpa" on the map is to map the space to a different space.
12706 * We create a separate isl_multi_aff to effectuate this change
12707 * in order to avoid spurious splitting of the map along the pieces
12710 __isl_give isl_map
*isl_map_preimage_multi_pw_aff(__isl_take isl_map
*map
,
12711 enum isl_dim_type type
, __isl_take isl_multi_pw_aff
*mpa
)
12714 isl_pw_multi_aff
*pma
;
12719 n
= isl_map_dim(map
, type
);
12720 if (!isl_map_involves_dims(map
, type
, 0, n
)) {
12724 space
= isl_multi_pw_aff_get_space(mpa
);
12725 isl_multi_pw_aff_free(mpa
);
12726 ma
= isl_multi_aff_zero(space
);
12727 return isl_map_preimage_multi_aff(map
, type
, ma
);
12730 pma
= isl_pw_multi_aff_from_multi_pw_aff(mpa
);
12731 return isl_map_preimage_pw_multi_aff(map
, type
, pma
);
12734 isl_multi_pw_aff_free(mpa
);
12738 /* Compute the preimage of "map" under the function represented by "mpa".
12739 * In other words, plug in "mpa" in the domain "map".
12740 * The result is a map that lives in the same space as "map",
12741 * except that domain space has been replaced by the domain space of "mpa".
12743 __isl_give isl_map
*isl_map_preimage_domain_multi_pw_aff(
12744 __isl_take isl_map
*map
, __isl_take isl_multi_pw_aff
*mpa
)
12746 return isl_map_preimage_multi_pw_aff(map
, isl_dim_in
, mpa
);
12749 /* Compute the preimage of "set" by the function represented by "mpa".
12750 * In other words, plug in "mpa" in "set".
12752 __isl_give isl_set
*isl_set_preimage_multi_pw_aff(__isl_take isl_set
*set
,
12753 __isl_take isl_multi_pw_aff
*mpa
)
12755 return isl_map_preimage_multi_pw_aff(set
, isl_dim_set
, mpa
);