2 * Copyright 2008-2009 Katholieke Universiteit Leuven
3 * Copyright 2010 INRIA Saclay
4 * Copyright 2012-2014 Ecole Normale Superieure
5 * Copyright 2014 INRIA Rocquencourt
6 * Copyright 2016 Sven Verdoolaege
8 * Use of this software is governed by the MIT license
10 * Written by Sven Verdoolaege, K.U.Leuven, Departement
11 * Computerwetenschappen, Celestijnenlaan 200A, B-3001 Leuven, Belgium
12 * and INRIA Saclay - Ile-de-France, Parc Club Orsay Universite,
13 * ZAC des vignes, 4 rue Jacques Monod, 91893 Orsay, France
14 * and Ecole Normale Superieure, 45 rue d’Ulm, 75230 Paris, France
15 * and Inria Paris - Rocquencourt, Domaine de Voluceau - Rocquencourt,
16 * B.P. 105 - 78153 Le Chesnay, France
20 #include <isl_ctx_private.h>
21 #include <isl_map_private.h>
23 #include <isl/constraint.h>
24 #include "isl_space_private.h"
25 #include "isl_equalities.h"
26 #include <isl_lp_private.h>
30 #include <isl_reordering.h>
31 #include "isl_sample.h"
35 #include <isl_mat_private.h>
36 #include <isl_vec_private.h>
37 #include <isl_dim_map.h>
38 #include <isl_local_space_private.h>
39 #include <isl_aff_private.h>
40 #include <isl_options_private.h>
41 #include <isl_morph.h>
42 #include <isl_val_private.h>
43 #include <isl/deprecated/map_int.h>
44 #include <isl/deprecated/set_int.h>
46 static unsigned n(__isl_keep isl_space
*dim
, enum isl_dim_type type
)
49 case isl_dim_param
: return dim
->nparam
;
50 case isl_dim_in
: return dim
->n_in
;
51 case isl_dim_out
: return dim
->n_out
;
52 case isl_dim_all
: return dim
->nparam
+ dim
->n_in
+ dim
->n_out
;
57 static unsigned pos(__isl_keep isl_space
*dim
, enum isl_dim_type type
)
60 case isl_dim_param
: return 1;
61 case isl_dim_in
: return 1 + dim
->nparam
;
62 case isl_dim_out
: return 1 + dim
->nparam
+ dim
->n_in
;
67 unsigned isl_basic_map_dim(__isl_keep isl_basic_map
*bmap
,
68 enum isl_dim_type type
)
73 case isl_dim_cst
: return 1;
76 case isl_dim_out
: return isl_space_dim(bmap
->dim
, type
);
77 case isl_dim_div
: return bmap
->n_div
;
78 case isl_dim_all
: return isl_basic_map_total_dim(bmap
);
83 unsigned isl_map_dim(__isl_keep isl_map
*map
, enum isl_dim_type type
)
85 return map
? n(map
->dim
, type
) : 0;
88 unsigned isl_set_dim(__isl_keep isl_set
*set
, enum isl_dim_type type
)
90 return set
? n(set
->dim
, type
) : 0;
93 unsigned isl_basic_map_offset(struct isl_basic_map
*bmap
,
94 enum isl_dim_type type
)
103 case isl_dim_cst
: return 0;
104 case isl_dim_param
: return 1;
105 case isl_dim_in
: return 1 + space
->nparam
;
106 case isl_dim_out
: return 1 + space
->nparam
+ space
->n_in
;
107 case isl_dim_div
: return 1 + space
->nparam
+ space
->n_in
+
113 unsigned isl_basic_set_offset(struct isl_basic_set
*bset
,
114 enum isl_dim_type type
)
116 return isl_basic_map_offset(bset
, type
);
119 static unsigned map_offset(struct isl_map
*map
, enum isl_dim_type type
)
121 return pos(map
->dim
, type
);
124 unsigned isl_basic_set_dim(__isl_keep isl_basic_set
*bset
,
125 enum isl_dim_type type
)
127 return isl_basic_map_dim(bset
, type
);
130 unsigned isl_basic_set_n_dim(__isl_keep isl_basic_set
*bset
)
132 return isl_basic_set_dim(bset
, isl_dim_set
);
135 unsigned isl_basic_set_n_param(__isl_keep isl_basic_set
*bset
)
137 return isl_basic_set_dim(bset
, isl_dim_param
);
140 unsigned isl_basic_set_total_dim(const struct isl_basic_set
*bset
)
144 return isl_space_dim(bset
->dim
, isl_dim_all
) + bset
->n_div
;
147 unsigned isl_set_n_dim(__isl_keep isl_set
*set
)
149 return isl_set_dim(set
, isl_dim_set
);
152 unsigned isl_set_n_param(__isl_keep isl_set
*set
)
154 return isl_set_dim(set
, isl_dim_param
);
157 unsigned isl_basic_map_n_in(const struct isl_basic_map
*bmap
)
159 return bmap
? bmap
->dim
->n_in
: 0;
162 unsigned isl_basic_map_n_out(const struct isl_basic_map
*bmap
)
164 return bmap
? bmap
->dim
->n_out
: 0;
167 unsigned isl_basic_map_n_param(const struct isl_basic_map
*bmap
)
169 return bmap
? bmap
->dim
->nparam
: 0;
172 unsigned isl_basic_map_n_div(const struct isl_basic_map
*bmap
)
174 return bmap
? bmap
->n_div
: 0;
177 unsigned isl_basic_map_total_dim(const struct isl_basic_map
*bmap
)
179 return bmap
? isl_space_dim(bmap
->dim
, isl_dim_all
) + bmap
->n_div
: 0;
182 unsigned isl_map_n_in(const struct isl_map
*map
)
184 return map
? map
->dim
->n_in
: 0;
187 unsigned isl_map_n_out(const struct isl_map
*map
)
189 return map
? map
->dim
->n_out
: 0;
192 unsigned isl_map_n_param(const struct isl_map
*map
)
194 return map
? map
->dim
->nparam
: 0;
197 int isl_map_compatible_domain(struct isl_map
*map
, struct isl_set
*set
)
202 m
= isl_space_match(map
->dim
, isl_dim_param
, set
->dim
, isl_dim_param
);
205 return isl_space_tuple_is_equal(map
->dim
, isl_dim_in
,
206 set
->dim
, isl_dim_set
);
209 int isl_basic_map_compatible_domain(struct isl_basic_map
*bmap
,
210 struct isl_basic_set
*bset
)
215 m
= isl_space_match(bmap
->dim
, isl_dim_param
, bset
->dim
, isl_dim_param
);
218 return isl_space_tuple_is_equal(bmap
->dim
, isl_dim_in
,
219 bset
->dim
, isl_dim_set
);
222 int isl_map_compatible_range(__isl_keep isl_map
*map
, __isl_keep isl_set
*set
)
227 m
= isl_space_match(map
->dim
, isl_dim_param
, set
->dim
, isl_dim_param
);
230 return isl_space_tuple_is_equal(map
->dim
, isl_dim_out
,
231 set
->dim
, isl_dim_set
);
234 int isl_basic_map_compatible_range(struct isl_basic_map
*bmap
,
235 struct isl_basic_set
*bset
)
240 m
= isl_space_match(bmap
->dim
, isl_dim_param
, bset
->dim
, isl_dim_param
);
243 return isl_space_tuple_is_equal(bmap
->dim
, isl_dim_out
,
244 bset
->dim
, isl_dim_set
);
247 isl_ctx
*isl_basic_map_get_ctx(__isl_keep isl_basic_map
*bmap
)
249 return bmap
? bmap
->ctx
: NULL
;
252 isl_ctx
*isl_basic_set_get_ctx(__isl_keep isl_basic_set
*bset
)
254 return bset
? bset
->ctx
: NULL
;
257 isl_ctx
*isl_map_get_ctx(__isl_keep isl_map
*map
)
259 return map
? map
->ctx
: NULL
;
262 isl_ctx
*isl_set_get_ctx(__isl_keep isl_set
*set
)
264 return set
? set
->ctx
: NULL
;
267 __isl_give isl_space
*isl_basic_map_get_space(__isl_keep isl_basic_map
*bmap
)
271 return isl_space_copy(bmap
->dim
);
274 __isl_give isl_space
*isl_basic_set_get_space(__isl_keep isl_basic_set
*bset
)
278 return isl_space_copy(bset
->dim
);
281 /* Extract the divs in "bmap" as a matrix.
283 __isl_give isl_mat
*isl_basic_map_get_divs(__isl_keep isl_basic_map
*bmap
)
294 ctx
= isl_basic_map_get_ctx(bmap
);
295 total
= isl_space_dim(bmap
->dim
, isl_dim_all
);
296 cols
= 1 + 1 + total
+ bmap
->n_div
;
297 div
= isl_mat_alloc(ctx
, bmap
->n_div
, cols
);
301 for (i
= 0; i
< bmap
->n_div
; ++i
)
302 isl_seq_cpy(div
->row
[i
], bmap
->div
[i
], cols
);
307 /* Extract the divs in "bset" as a matrix.
309 __isl_give isl_mat
*isl_basic_set_get_divs(__isl_keep isl_basic_set
*bset
)
311 return isl_basic_map_get_divs(bset
);
314 __isl_give isl_local_space
*isl_basic_map_get_local_space(
315 __isl_keep isl_basic_map
*bmap
)
322 div
= isl_basic_map_get_divs(bmap
);
323 return isl_local_space_alloc_div(isl_space_copy(bmap
->dim
), div
);
326 __isl_give isl_local_space
*isl_basic_set_get_local_space(
327 __isl_keep isl_basic_set
*bset
)
329 return isl_basic_map_get_local_space(bset
);
332 /* For each known div d = floor(f/m), add the constraints
335 * -(f-(n-1)) + m d >= 0
337 * Do not finalize the result.
339 static __isl_give isl_basic_map
*add_known_div_constraints(
340 __isl_take isl_basic_map
*bmap
)
347 n_div
= isl_basic_map_dim(bmap
, isl_dim_div
);
350 bmap
= isl_basic_map_cow(bmap
);
351 bmap
= isl_basic_map_extend_constraints(bmap
, 0, 2 * n_div
);
354 for (i
= 0; i
< n_div
; ++i
) {
355 if (isl_int_is_zero(bmap
->div
[i
][0]))
357 if (isl_basic_map_add_div_constraints(bmap
, i
) < 0)
358 return isl_basic_map_free(bmap
);
364 __isl_give isl_basic_map
*isl_basic_map_from_local_space(
365 __isl_take isl_local_space
*ls
)
374 n_div
= isl_local_space_dim(ls
, isl_dim_div
);
375 bmap
= isl_basic_map_alloc_space(isl_local_space_get_space(ls
),
376 n_div
, 0, 2 * n_div
);
378 for (i
= 0; i
< n_div
; ++i
)
379 if (isl_basic_map_alloc_div(bmap
) < 0)
382 for (i
= 0; i
< n_div
; ++i
)
383 isl_seq_cpy(bmap
->div
[i
], ls
->div
->row
[i
], ls
->div
->n_col
);
384 bmap
= add_known_div_constraints(bmap
);
386 isl_local_space_free(ls
);
389 isl_local_space_free(ls
);
390 isl_basic_map_free(bmap
);
394 __isl_give isl_basic_set
*isl_basic_set_from_local_space(
395 __isl_take isl_local_space
*ls
)
397 return isl_basic_map_from_local_space(ls
);
400 __isl_give isl_space
*isl_map_get_space(__isl_keep isl_map
*map
)
404 return isl_space_copy(map
->dim
);
407 __isl_give isl_space
*isl_set_get_space(__isl_keep isl_set
*set
)
411 return isl_space_copy(set
->dim
);
414 __isl_give isl_basic_map
*isl_basic_map_set_tuple_name(
415 __isl_take isl_basic_map
*bmap
, enum isl_dim_type type
, const char *s
)
417 bmap
= isl_basic_map_cow(bmap
);
420 bmap
->dim
= isl_space_set_tuple_name(bmap
->dim
, type
, s
);
423 bmap
= isl_basic_map_finalize(bmap
);
426 isl_basic_map_free(bmap
);
430 __isl_give isl_basic_set
*isl_basic_set_set_tuple_name(
431 __isl_take isl_basic_set
*bset
, const char *s
)
433 return isl_basic_map_set_tuple_name(bset
, isl_dim_set
, s
);
436 const char *isl_basic_map_get_tuple_name(__isl_keep isl_basic_map
*bmap
,
437 enum isl_dim_type type
)
439 return bmap
? isl_space_get_tuple_name(bmap
->dim
, type
) : NULL
;
442 __isl_give isl_map
*isl_map_set_tuple_name(__isl_take isl_map
*map
,
443 enum isl_dim_type type
, const char *s
)
447 map
= isl_map_cow(map
);
451 map
->dim
= isl_space_set_tuple_name(map
->dim
, type
, s
);
455 for (i
= 0; i
< map
->n
; ++i
) {
456 map
->p
[i
] = isl_basic_map_set_tuple_name(map
->p
[i
], type
, s
);
467 /* Replace the identifier of the tuple of type "type" by "id".
469 __isl_give isl_basic_map
*isl_basic_map_set_tuple_id(
470 __isl_take isl_basic_map
*bmap
,
471 enum isl_dim_type type
, __isl_take isl_id
*id
)
473 bmap
= isl_basic_map_cow(bmap
);
476 bmap
->dim
= isl_space_set_tuple_id(bmap
->dim
, type
, id
);
478 return isl_basic_map_free(bmap
);
479 bmap
= isl_basic_map_finalize(bmap
);
486 /* Replace the identifier of the tuple by "id".
488 __isl_give isl_basic_set
*isl_basic_set_set_tuple_id(
489 __isl_take isl_basic_set
*bset
, __isl_take isl_id
*id
)
491 return isl_basic_map_set_tuple_id(bset
, isl_dim_set
, id
);
494 /* Does the input or output tuple have a name?
496 isl_bool
isl_map_has_tuple_name(__isl_keep isl_map
*map
, enum isl_dim_type type
)
498 return map
? isl_space_has_tuple_name(map
->dim
, type
) : isl_bool_error
;
501 const char *isl_map_get_tuple_name(__isl_keep isl_map
*map
,
502 enum isl_dim_type type
)
504 return map
? isl_space_get_tuple_name(map
->dim
, type
) : NULL
;
507 __isl_give isl_set
*isl_set_set_tuple_name(__isl_take isl_set
*set
,
510 return (isl_set
*)isl_map_set_tuple_name((isl_map
*)set
, isl_dim_set
, s
);
513 __isl_give isl_map
*isl_map_set_tuple_id(__isl_take isl_map
*map
,
514 enum isl_dim_type type
, __isl_take isl_id
*id
)
516 map
= isl_map_cow(map
);
520 map
->dim
= isl_space_set_tuple_id(map
->dim
, type
, id
);
522 return isl_map_reset_space(map
, isl_space_copy(map
->dim
));
528 __isl_give isl_set
*isl_set_set_tuple_id(__isl_take isl_set
*set
,
529 __isl_take isl_id
*id
)
531 return isl_map_set_tuple_id(set
, isl_dim_set
, id
);
534 __isl_give isl_map
*isl_map_reset_tuple_id(__isl_take isl_map
*map
,
535 enum isl_dim_type type
)
537 map
= isl_map_cow(map
);
541 map
->dim
= isl_space_reset_tuple_id(map
->dim
, type
);
543 return isl_map_reset_space(map
, isl_space_copy(map
->dim
));
546 __isl_give isl_set
*isl_set_reset_tuple_id(__isl_take isl_set
*set
)
548 return isl_map_reset_tuple_id(set
, isl_dim_set
);
551 isl_bool
isl_map_has_tuple_id(__isl_keep isl_map
*map
, enum isl_dim_type type
)
553 return map
? isl_space_has_tuple_id(map
->dim
, type
) : isl_bool_error
;
556 __isl_give isl_id
*isl_map_get_tuple_id(__isl_keep isl_map
*map
,
557 enum isl_dim_type type
)
559 return map
? isl_space_get_tuple_id(map
->dim
, type
) : NULL
;
562 isl_bool
isl_set_has_tuple_id(__isl_keep isl_set
*set
)
564 return isl_map_has_tuple_id(set
, isl_dim_set
);
567 __isl_give isl_id
*isl_set_get_tuple_id(__isl_keep isl_set
*set
)
569 return isl_map_get_tuple_id(set
, isl_dim_set
);
572 /* Does the set tuple have a name?
574 isl_bool
isl_set_has_tuple_name(__isl_keep isl_set
*set
)
577 return isl_bool_error
;
578 return isl_space_has_tuple_name(set
->dim
, isl_dim_set
);
582 const char *isl_basic_set_get_tuple_name(__isl_keep isl_basic_set
*bset
)
584 return bset
? isl_space_get_tuple_name(bset
->dim
, isl_dim_set
) : NULL
;
587 const char *isl_set_get_tuple_name(__isl_keep isl_set
*set
)
589 return set
? isl_space_get_tuple_name(set
->dim
, isl_dim_set
) : NULL
;
592 const char *isl_basic_map_get_dim_name(__isl_keep isl_basic_map
*bmap
,
593 enum isl_dim_type type
, unsigned pos
)
595 return bmap
? isl_space_get_dim_name(bmap
->dim
, type
, pos
) : NULL
;
598 const char *isl_basic_set_get_dim_name(__isl_keep isl_basic_set
*bset
,
599 enum isl_dim_type type
, unsigned pos
)
601 return bset
? isl_space_get_dim_name(bset
->dim
, type
, pos
) : NULL
;
604 /* Does the given dimension have a name?
606 isl_bool
isl_map_has_dim_name(__isl_keep isl_map
*map
,
607 enum isl_dim_type type
, unsigned pos
)
610 return isl_bool_error
;
611 return isl_space_has_dim_name(map
->dim
, type
, pos
);
614 const char *isl_map_get_dim_name(__isl_keep isl_map
*map
,
615 enum isl_dim_type type
, unsigned pos
)
617 return map
? isl_space_get_dim_name(map
->dim
, type
, pos
) : NULL
;
620 const char *isl_set_get_dim_name(__isl_keep isl_set
*set
,
621 enum isl_dim_type type
, unsigned pos
)
623 return set
? isl_space_get_dim_name(set
->dim
, type
, pos
) : NULL
;
626 /* Does the given dimension have a name?
628 isl_bool
isl_set_has_dim_name(__isl_keep isl_set
*set
,
629 enum isl_dim_type type
, unsigned pos
)
632 return isl_bool_error
;
633 return isl_space_has_dim_name(set
->dim
, type
, pos
);
636 __isl_give isl_basic_map
*isl_basic_map_set_dim_name(
637 __isl_take isl_basic_map
*bmap
,
638 enum isl_dim_type type
, unsigned pos
, const char *s
)
640 bmap
= isl_basic_map_cow(bmap
);
643 bmap
->dim
= isl_space_set_dim_name(bmap
->dim
, type
, pos
, s
);
646 return isl_basic_map_finalize(bmap
);
648 isl_basic_map_free(bmap
);
652 __isl_give isl_map
*isl_map_set_dim_name(__isl_take isl_map
*map
,
653 enum isl_dim_type type
, unsigned pos
, const char *s
)
657 map
= isl_map_cow(map
);
661 map
->dim
= isl_space_set_dim_name(map
->dim
, type
, pos
, s
);
665 for (i
= 0; i
< map
->n
; ++i
) {
666 map
->p
[i
] = isl_basic_map_set_dim_name(map
->p
[i
], type
, pos
, s
);
677 __isl_give isl_basic_set
*isl_basic_set_set_dim_name(
678 __isl_take isl_basic_set
*bset
,
679 enum isl_dim_type type
, unsigned pos
, const char *s
)
681 return (isl_basic_set
*)isl_basic_map_set_dim_name(
682 (isl_basic_map
*)bset
, type
, pos
, s
);
685 __isl_give isl_set
*isl_set_set_dim_name(__isl_take isl_set
*set
,
686 enum isl_dim_type type
, unsigned pos
, const char *s
)
688 return (isl_set
*)isl_map_set_dim_name((isl_map
*)set
, type
, pos
, s
);
691 isl_bool
isl_basic_map_has_dim_id(__isl_keep isl_basic_map
*bmap
,
692 enum isl_dim_type type
, unsigned pos
)
695 return isl_bool_error
;
696 return isl_space_has_dim_id(bmap
->dim
, type
, pos
);
699 __isl_give isl_id
*isl_basic_set_get_dim_id(__isl_keep isl_basic_set
*bset
,
700 enum isl_dim_type type
, unsigned pos
)
702 return bset
? isl_space_get_dim_id(bset
->dim
, type
, pos
) : NULL
;
705 isl_bool
isl_map_has_dim_id(__isl_keep isl_map
*map
,
706 enum isl_dim_type type
, unsigned pos
)
708 return map
? isl_space_has_dim_id(map
->dim
, type
, pos
) : isl_bool_error
;
711 __isl_give isl_id
*isl_map_get_dim_id(__isl_keep isl_map
*map
,
712 enum isl_dim_type type
, unsigned pos
)
714 return map
? isl_space_get_dim_id(map
->dim
, type
, pos
) : NULL
;
717 isl_bool
isl_set_has_dim_id(__isl_keep isl_set
*set
,
718 enum isl_dim_type type
, unsigned pos
)
720 return isl_map_has_dim_id(set
, type
, pos
);
723 __isl_give isl_id
*isl_set_get_dim_id(__isl_keep isl_set
*set
,
724 enum isl_dim_type type
, unsigned pos
)
726 return isl_map_get_dim_id(set
, type
, pos
);
729 __isl_give isl_map
*isl_map_set_dim_id(__isl_take isl_map
*map
,
730 enum isl_dim_type type
, unsigned pos
, __isl_take isl_id
*id
)
732 map
= isl_map_cow(map
);
736 map
->dim
= isl_space_set_dim_id(map
->dim
, type
, pos
, id
);
738 return isl_map_reset_space(map
, isl_space_copy(map
->dim
));
744 __isl_give isl_set
*isl_set_set_dim_id(__isl_take isl_set
*set
,
745 enum isl_dim_type type
, unsigned pos
, __isl_take isl_id
*id
)
747 return isl_map_set_dim_id(set
, type
, pos
, id
);
750 int isl_map_find_dim_by_id(__isl_keep isl_map
*map
, enum isl_dim_type type
,
751 __isl_keep isl_id
*id
)
755 return isl_space_find_dim_by_id(map
->dim
, type
, id
);
758 int isl_set_find_dim_by_id(__isl_keep isl_set
*set
, enum isl_dim_type type
,
759 __isl_keep isl_id
*id
)
761 return isl_map_find_dim_by_id(set
, type
, id
);
764 /* Return the position of the dimension of the given type and name
766 * Return -1 if no such dimension can be found.
768 int isl_basic_map_find_dim_by_name(__isl_keep isl_basic_map
*bmap
,
769 enum isl_dim_type type
, const char *name
)
773 return isl_space_find_dim_by_name(bmap
->dim
, type
, name
);
776 int isl_map_find_dim_by_name(__isl_keep isl_map
*map
, enum isl_dim_type type
,
781 return isl_space_find_dim_by_name(map
->dim
, type
, name
);
784 int isl_set_find_dim_by_name(__isl_keep isl_set
*set
, enum isl_dim_type type
,
787 return isl_map_find_dim_by_name(set
, type
, name
);
790 /* Reset the user pointer on all identifiers of parameters and tuples
791 * of the space of "map".
793 __isl_give isl_map
*isl_map_reset_user(__isl_take isl_map
*map
)
797 space
= isl_map_get_space(map
);
798 space
= isl_space_reset_user(space
);
799 map
= isl_map_reset_space(map
, space
);
804 /* Reset the user pointer on all identifiers of parameters and tuples
805 * of the space of "set".
807 __isl_give isl_set
*isl_set_reset_user(__isl_take isl_set
*set
)
809 return isl_map_reset_user(set
);
812 int isl_basic_map_is_rational(__isl_keep isl_basic_map
*bmap
)
816 return ISL_F_ISSET(bmap
, ISL_BASIC_MAP_RATIONAL
);
819 int isl_basic_set_is_rational(__isl_keep isl_basic_set
*bset
)
821 return isl_basic_map_is_rational(bset
);
824 /* Does "bmap" contain any rational points?
826 * If "bmap" has an equality for each dimension, equating the dimension
827 * to an integer constant, then it has no rational points, even if it
828 * is marked as rational.
830 int isl_basic_map_has_rational(__isl_keep isl_basic_map
*bmap
)
832 int has_rational
= 1;
837 if (isl_basic_map_plain_is_empty(bmap
))
839 if (!isl_basic_map_is_rational(bmap
))
841 bmap
= isl_basic_map_copy(bmap
);
842 bmap
= isl_basic_map_implicit_equalities(bmap
);
845 total
= isl_basic_map_total_dim(bmap
);
846 if (bmap
->n_eq
== total
) {
848 for (i
= 0; i
< bmap
->n_eq
; ++i
) {
849 j
= isl_seq_first_non_zero(bmap
->eq
[i
] + 1, total
);
852 if (!isl_int_is_one(bmap
->eq
[i
][1 + j
]) &&
853 !isl_int_is_negone(bmap
->eq
[i
][1 + j
]))
855 j
= isl_seq_first_non_zero(bmap
->eq
[i
] + 1 + j
+ 1,
863 isl_basic_map_free(bmap
);
868 /* Does "map" contain any rational points?
870 int isl_map_has_rational(__isl_keep isl_map
*map
)
877 for (i
= 0; i
< map
->n
; ++i
) {
878 has_rational
= isl_basic_map_has_rational(map
->p
[i
]);
879 if (has_rational
< 0)
887 /* Does "set" contain any rational points?
889 int isl_set_has_rational(__isl_keep isl_set
*set
)
891 return isl_map_has_rational(set
);
894 /* Is this basic set a parameter domain?
896 int isl_basic_set_is_params(__isl_keep isl_basic_set
*bset
)
900 return isl_space_is_params(bset
->dim
);
903 /* Is this set a parameter domain?
905 isl_bool
isl_set_is_params(__isl_keep isl_set
*set
)
908 return isl_bool_error
;
909 return isl_space_is_params(set
->dim
);
912 /* Is this map actually a parameter domain?
913 * Users should never call this function. Outside of isl,
914 * a map can never be a parameter domain.
916 int isl_map_is_params(__isl_keep isl_map
*map
)
920 return isl_space_is_params(map
->dim
);
923 static struct isl_basic_map
*basic_map_init(struct isl_ctx
*ctx
,
924 struct isl_basic_map
*bmap
, unsigned extra
,
925 unsigned n_eq
, unsigned n_ineq
)
928 size_t row_size
= 1 + isl_space_dim(bmap
->dim
, isl_dim_all
) + extra
;
933 bmap
->block
= isl_blk_alloc(ctx
, (n_ineq
+ n_eq
) * row_size
);
934 if (isl_blk_is_error(bmap
->block
))
937 bmap
->ineq
= isl_alloc_array(ctx
, isl_int
*, n_ineq
+ n_eq
);
938 if ((n_ineq
+ n_eq
) && !bmap
->ineq
)
942 bmap
->block2
= isl_blk_empty();
945 bmap
->block2
= isl_blk_alloc(ctx
, extra
* (1 + row_size
));
946 if (isl_blk_is_error(bmap
->block2
))
949 bmap
->div
= isl_alloc_array(ctx
, isl_int
*, extra
);
954 for (i
= 0; i
< n_ineq
+ n_eq
; ++i
)
955 bmap
->ineq
[i
] = bmap
->block
.data
+ i
* row_size
;
957 for (i
= 0; i
< extra
; ++i
)
958 bmap
->div
[i
] = bmap
->block2
.data
+ i
* (1 + row_size
);
962 bmap
->c_size
= n_eq
+ n_ineq
;
963 bmap
->eq
= bmap
->ineq
+ n_ineq
;
972 isl_basic_map_free(bmap
);
976 struct isl_basic_set
*isl_basic_set_alloc(struct isl_ctx
*ctx
,
977 unsigned nparam
, unsigned dim
, unsigned extra
,
978 unsigned n_eq
, unsigned n_ineq
)
980 struct isl_basic_map
*bmap
;
983 space
= isl_space_set_alloc(ctx
, nparam
, dim
);
987 bmap
= isl_basic_map_alloc_space(space
, extra
, n_eq
, n_ineq
);
988 return (struct isl_basic_set
*)bmap
;
991 struct isl_basic_set
*isl_basic_set_alloc_space(__isl_take isl_space
*dim
,
992 unsigned extra
, unsigned n_eq
, unsigned n_ineq
)
994 struct isl_basic_map
*bmap
;
997 isl_assert(dim
->ctx
, dim
->n_in
== 0, goto error
);
998 bmap
= isl_basic_map_alloc_space(dim
, extra
, n_eq
, n_ineq
);
999 return (struct isl_basic_set
*)bmap
;
1001 isl_space_free(dim
);
1005 struct isl_basic_map
*isl_basic_map_alloc_space(__isl_take isl_space
*dim
,
1006 unsigned extra
, unsigned n_eq
, unsigned n_ineq
)
1008 struct isl_basic_map
*bmap
;
1012 bmap
= isl_calloc_type(dim
->ctx
, struct isl_basic_map
);
1017 return basic_map_init(dim
->ctx
, bmap
, extra
, n_eq
, n_ineq
);
1019 isl_space_free(dim
);
1023 struct isl_basic_map
*isl_basic_map_alloc(struct isl_ctx
*ctx
,
1024 unsigned nparam
, unsigned in
, unsigned out
, unsigned extra
,
1025 unsigned n_eq
, unsigned n_ineq
)
1027 struct isl_basic_map
*bmap
;
1030 dim
= isl_space_alloc(ctx
, nparam
, in
, out
);
1034 bmap
= isl_basic_map_alloc_space(dim
, extra
, n_eq
, n_ineq
);
1038 static void dup_constraints(
1039 struct isl_basic_map
*dst
, struct isl_basic_map
*src
)
1042 unsigned total
= isl_basic_map_total_dim(src
);
1044 for (i
= 0; i
< src
->n_eq
; ++i
) {
1045 int j
= isl_basic_map_alloc_equality(dst
);
1046 isl_seq_cpy(dst
->eq
[j
], src
->eq
[i
], 1+total
);
1049 for (i
= 0; i
< src
->n_ineq
; ++i
) {
1050 int j
= isl_basic_map_alloc_inequality(dst
);
1051 isl_seq_cpy(dst
->ineq
[j
], src
->ineq
[i
], 1+total
);
1054 for (i
= 0; i
< src
->n_div
; ++i
) {
1055 int j
= isl_basic_map_alloc_div(dst
);
1056 isl_seq_cpy(dst
->div
[j
], src
->div
[i
], 1+1+total
);
1058 ISL_F_SET(dst
, ISL_BASIC_SET_FINAL
);
1061 struct isl_basic_map
*isl_basic_map_dup(struct isl_basic_map
*bmap
)
1063 struct isl_basic_map
*dup
;
1067 dup
= isl_basic_map_alloc_space(isl_space_copy(bmap
->dim
),
1068 bmap
->n_div
, bmap
->n_eq
, bmap
->n_ineq
);
1071 dup_constraints(dup
, bmap
);
1072 dup
->flags
= bmap
->flags
;
1073 dup
->sample
= isl_vec_copy(bmap
->sample
);
1077 struct isl_basic_set
*isl_basic_set_dup(struct isl_basic_set
*bset
)
1079 struct isl_basic_map
*dup
;
1081 dup
= isl_basic_map_dup((struct isl_basic_map
*)bset
);
1082 return (struct isl_basic_set
*)dup
;
1085 struct isl_basic_set
*isl_basic_set_copy(struct isl_basic_set
*bset
)
1090 if (ISL_F_ISSET(bset
, ISL_BASIC_SET_FINAL
)) {
1094 return isl_basic_set_dup(bset
);
1097 struct isl_set
*isl_set_copy(struct isl_set
*set
)
1106 struct isl_basic_map
*isl_basic_map_copy(struct isl_basic_map
*bmap
)
1111 if (ISL_F_ISSET(bmap
, ISL_BASIC_SET_FINAL
)) {
1115 bmap
= isl_basic_map_dup(bmap
);
1117 ISL_F_SET(bmap
, ISL_BASIC_SET_FINAL
);
1121 struct isl_map
*isl_map_copy(struct isl_map
*map
)
1130 __isl_null isl_basic_map
*isl_basic_map_free(__isl_take isl_basic_map
*bmap
)
1135 if (--bmap
->ref
> 0)
1138 isl_ctx_deref(bmap
->ctx
);
1140 isl_blk_free(bmap
->ctx
, bmap
->block2
);
1142 isl_blk_free(bmap
->ctx
, bmap
->block
);
1143 isl_vec_free(bmap
->sample
);
1144 isl_space_free(bmap
->dim
);
1150 __isl_null isl_basic_set
*isl_basic_set_free(__isl_take isl_basic_set
*bset
)
1152 return isl_basic_map_free((struct isl_basic_map
*)bset
);
1155 static int room_for_con(struct isl_basic_map
*bmap
, unsigned n
)
1157 return bmap
->n_eq
+ bmap
->n_ineq
+ n
<= bmap
->c_size
;
1160 __isl_give isl_map
*isl_map_align_params_map_map_and(
1161 __isl_take isl_map
*map1
, __isl_take isl_map
*map2
,
1162 __isl_give isl_map
*(*fn
)(__isl_take isl_map
*map1
,
1163 __isl_take isl_map
*map2
))
1167 if (isl_space_match(map1
->dim
, isl_dim_param
, map2
->dim
, isl_dim_param
))
1168 return fn(map1
, map2
);
1169 if (!isl_space_has_named_params(map1
->dim
) ||
1170 !isl_space_has_named_params(map2
->dim
))
1171 isl_die(map1
->ctx
, isl_error_invalid
,
1172 "unaligned unnamed parameters", goto error
);
1173 map1
= isl_map_align_params(map1
, isl_map_get_space(map2
));
1174 map2
= isl_map_align_params(map2
, isl_map_get_space(map1
));
1175 return fn(map1
, map2
);
1182 isl_bool
isl_map_align_params_map_map_and_test(__isl_keep isl_map
*map1
,
1183 __isl_keep isl_map
*map2
,
1184 isl_bool (*fn
)(__isl_keep isl_map
*map1
, __isl_keep isl_map
*map2
))
1189 return isl_bool_error
;
1190 if (isl_space_match(map1
->dim
, isl_dim_param
, map2
->dim
, isl_dim_param
))
1191 return fn(map1
, map2
);
1192 if (!isl_space_has_named_params(map1
->dim
) ||
1193 !isl_space_has_named_params(map2
->dim
))
1194 isl_die(map1
->ctx
, isl_error_invalid
,
1195 "unaligned unnamed parameters", return isl_bool_error
);
1196 map1
= isl_map_copy(map1
);
1197 map2
= isl_map_copy(map2
);
1198 map1
= isl_map_align_params(map1
, isl_map_get_space(map2
));
1199 map2
= isl_map_align_params(map2
, isl_map_get_space(map1
));
1206 int isl_basic_map_alloc_equality(struct isl_basic_map
*bmap
)
1208 struct isl_ctx
*ctx
;
1212 isl_assert(ctx
, room_for_con(bmap
, 1), return -1);
1213 isl_assert(ctx
, (bmap
->eq
- bmap
->ineq
) + bmap
->n_eq
<= bmap
->c_size
,
1215 ISL_F_CLR(bmap
, ISL_BASIC_MAP_NORMALIZED
);
1216 ISL_F_CLR(bmap
, ISL_BASIC_MAP_NO_REDUNDANT
);
1217 ISL_F_CLR(bmap
, ISL_BASIC_MAP_NO_IMPLICIT
);
1218 ISL_F_CLR(bmap
, ISL_BASIC_MAP_ALL_EQUALITIES
);
1219 ISL_F_CLR(bmap
, ISL_BASIC_MAP_NORMALIZED_DIVS
);
1220 if ((bmap
->eq
- bmap
->ineq
) + bmap
->n_eq
== bmap
->c_size
) {
1222 int j
= isl_basic_map_alloc_inequality(bmap
);
1226 bmap
->ineq
[j
] = bmap
->ineq
[bmap
->n_ineq
- 1];
1227 bmap
->ineq
[bmap
->n_ineq
- 1] = bmap
->eq
[-1];
1234 isl_seq_clr(bmap
->eq
[bmap
->n_eq
] + 1 + isl_basic_map_total_dim(bmap
),
1235 bmap
->extra
- bmap
->n_div
);
1236 return bmap
->n_eq
++;
1239 int isl_basic_set_alloc_equality(struct isl_basic_set
*bset
)
1241 return isl_basic_map_alloc_equality((struct isl_basic_map
*)bset
);
1244 int isl_basic_map_free_equality(struct isl_basic_map
*bmap
, unsigned n
)
1248 isl_assert(bmap
->ctx
, n
<= bmap
->n_eq
, return -1);
1253 int isl_basic_set_free_equality(struct isl_basic_set
*bset
, unsigned n
)
1255 return isl_basic_map_free_equality((struct isl_basic_map
*)bset
, n
);
1258 int isl_basic_map_drop_equality(struct isl_basic_map
*bmap
, unsigned pos
)
1263 isl_assert(bmap
->ctx
, pos
< bmap
->n_eq
, return -1);
1265 if (pos
!= bmap
->n_eq
- 1) {
1267 bmap
->eq
[pos
] = bmap
->eq
[bmap
->n_eq
- 1];
1268 bmap
->eq
[bmap
->n_eq
- 1] = t
;
1274 int isl_basic_set_drop_equality(struct isl_basic_set
*bset
, unsigned pos
)
1276 return isl_basic_map_drop_equality((struct isl_basic_map
*)bset
, pos
);
1279 /* Turn inequality "pos" of "bmap" into an equality.
1281 * In particular, we move the inequality in front of the equalities
1282 * and move the last inequality in the position of the moved inequality.
1283 * Note that isl_tab_make_equalities_explicit depends on this particular
1284 * change in the ordering of the constraints.
1286 void isl_basic_map_inequality_to_equality(
1287 struct isl_basic_map
*bmap
, unsigned pos
)
1291 t
= bmap
->ineq
[pos
];
1292 bmap
->ineq
[pos
] = bmap
->ineq
[bmap
->n_ineq
- 1];
1293 bmap
->ineq
[bmap
->n_ineq
- 1] = bmap
->eq
[-1];
1298 ISL_F_CLR(bmap
, ISL_BASIC_MAP_NO_REDUNDANT
);
1299 ISL_F_CLR(bmap
, ISL_BASIC_MAP_NORMALIZED
);
1300 ISL_F_CLR(bmap
, ISL_BASIC_MAP_NORMALIZED_DIVS
);
1301 ISL_F_CLR(bmap
, ISL_BASIC_MAP_ALL_EQUALITIES
);
1304 static int room_for_ineq(struct isl_basic_map
*bmap
, unsigned n
)
1306 return bmap
->n_ineq
+ n
<= bmap
->eq
- bmap
->ineq
;
1309 int isl_basic_map_alloc_inequality(struct isl_basic_map
*bmap
)
1311 struct isl_ctx
*ctx
;
1315 isl_assert(ctx
, room_for_ineq(bmap
, 1), return -1);
1316 ISL_F_CLR(bmap
, ISL_BASIC_MAP_NO_IMPLICIT
);
1317 ISL_F_CLR(bmap
, ISL_BASIC_MAP_NO_REDUNDANT
);
1318 ISL_F_CLR(bmap
, ISL_BASIC_MAP_NORMALIZED
);
1319 ISL_F_CLR(bmap
, ISL_BASIC_MAP_ALL_EQUALITIES
);
1320 isl_seq_clr(bmap
->ineq
[bmap
->n_ineq
] +
1321 1 + isl_basic_map_total_dim(bmap
),
1322 bmap
->extra
- bmap
->n_div
);
1323 return bmap
->n_ineq
++;
1326 int isl_basic_set_alloc_inequality(struct isl_basic_set
*bset
)
1328 return isl_basic_map_alloc_inequality((struct isl_basic_map
*)bset
);
1331 int isl_basic_map_free_inequality(struct isl_basic_map
*bmap
, unsigned n
)
1335 isl_assert(bmap
->ctx
, n
<= bmap
->n_ineq
, return -1);
1340 int isl_basic_set_free_inequality(struct isl_basic_set
*bset
, unsigned n
)
1342 return isl_basic_map_free_inequality((struct isl_basic_map
*)bset
, n
);
1345 int isl_basic_map_drop_inequality(struct isl_basic_map
*bmap
, unsigned pos
)
1350 isl_assert(bmap
->ctx
, pos
< bmap
->n_ineq
, return -1);
1352 if (pos
!= bmap
->n_ineq
- 1) {
1353 t
= bmap
->ineq
[pos
];
1354 bmap
->ineq
[pos
] = bmap
->ineq
[bmap
->n_ineq
- 1];
1355 bmap
->ineq
[bmap
->n_ineq
- 1] = t
;
1356 ISL_F_CLR(bmap
, ISL_BASIC_MAP_NORMALIZED
);
1362 int isl_basic_set_drop_inequality(struct isl_basic_set
*bset
, unsigned pos
)
1364 return isl_basic_map_drop_inequality((struct isl_basic_map
*)bset
, pos
);
1367 __isl_give isl_basic_map
*isl_basic_map_add_eq(__isl_take isl_basic_map
*bmap
,
1372 bmap
= isl_basic_map_extend_constraints(bmap
, 1, 0);
1375 k
= isl_basic_map_alloc_equality(bmap
);
1378 isl_seq_cpy(bmap
->eq
[k
], eq
, 1 + isl_basic_map_total_dim(bmap
));
1381 isl_basic_map_free(bmap
);
1385 __isl_give isl_basic_set
*isl_basic_set_add_eq(__isl_take isl_basic_set
*bset
,
1388 return (isl_basic_set
*)
1389 isl_basic_map_add_eq((isl_basic_map
*)bset
, eq
);
1392 __isl_give isl_basic_map
*isl_basic_map_add_ineq(__isl_take isl_basic_map
*bmap
,
1397 bmap
= isl_basic_map_extend_constraints(bmap
, 0, 1);
1400 k
= isl_basic_map_alloc_inequality(bmap
);
1403 isl_seq_cpy(bmap
->ineq
[k
], ineq
, 1 + isl_basic_map_total_dim(bmap
));
1406 isl_basic_map_free(bmap
);
1410 __isl_give isl_basic_set
*isl_basic_set_add_ineq(__isl_take isl_basic_set
*bset
,
1413 return (isl_basic_set
*)
1414 isl_basic_map_add_ineq((isl_basic_map
*)bset
, ineq
);
1417 int isl_basic_map_alloc_div(struct isl_basic_map
*bmap
)
1421 isl_assert(bmap
->ctx
, bmap
->n_div
< bmap
->extra
, return -1);
1422 isl_seq_clr(bmap
->div
[bmap
->n_div
] +
1423 1 + 1 + isl_basic_map_total_dim(bmap
),
1424 bmap
->extra
- bmap
->n_div
);
1425 ISL_F_CLR(bmap
, ISL_BASIC_MAP_NORMALIZED_DIVS
);
1426 return bmap
->n_div
++;
1429 int isl_basic_set_alloc_div(struct isl_basic_set
*bset
)
1431 return isl_basic_map_alloc_div((struct isl_basic_map
*)bset
);
1434 int isl_basic_map_free_div(struct isl_basic_map
*bmap
, unsigned n
)
1438 isl_assert(bmap
->ctx
, n
<= bmap
->n_div
, return -1);
1443 int isl_basic_set_free_div(struct isl_basic_set
*bset
, unsigned n
)
1445 return isl_basic_map_free_div((struct isl_basic_map
*)bset
, n
);
1448 /* Copy constraint from src to dst, putting the vars of src at offset
1449 * dim_off in dst and the divs of src at offset div_off in dst.
1450 * If both sets are actually map, then dim_off applies to the input
1453 static void copy_constraint(struct isl_basic_map
*dst_map
, isl_int
*dst
,
1454 struct isl_basic_map
*src_map
, isl_int
*src
,
1455 unsigned in_off
, unsigned out_off
, unsigned div_off
)
1457 unsigned src_nparam
= isl_basic_map_n_param(src_map
);
1458 unsigned dst_nparam
= isl_basic_map_n_param(dst_map
);
1459 unsigned src_in
= isl_basic_map_n_in(src_map
);
1460 unsigned dst_in
= isl_basic_map_n_in(dst_map
);
1461 unsigned src_out
= isl_basic_map_n_out(src_map
);
1462 unsigned dst_out
= isl_basic_map_n_out(dst_map
);
1463 isl_int_set(dst
[0], src
[0]);
1464 isl_seq_cpy(dst
+1, src
+1, isl_min(dst_nparam
, src_nparam
));
1465 if (dst_nparam
> src_nparam
)
1466 isl_seq_clr(dst
+1+src_nparam
,
1467 dst_nparam
- src_nparam
);
1468 isl_seq_clr(dst
+1+dst_nparam
, in_off
);
1469 isl_seq_cpy(dst
+1+dst_nparam
+in_off
,
1471 isl_min(dst_in
-in_off
, src_in
));
1472 if (dst_in
-in_off
> src_in
)
1473 isl_seq_clr(dst
+1+dst_nparam
+in_off
+src_in
,
1474 dst_in
- in_off
- src_in
);
1475 isl_seq_clr(dst
+1+dst_nparam
+dst_in
, out_off
);
1476 isl_seq_cpy(dst
+1+dst_nparam
+dst_in
+out_off
,
1477 src
+1+src_nparam
+src_in
,
1478 isl_min(dst_out
-out_off
, src_out
));
1479 if (dst_out
-out_off
> src_out
)
1480 isl_seq_clr(dst
+1+dst_nparam
+dst_in
+out_off
+src_out
,
1481 dst_out
- out_off
- src_out
);
1482 isl_seq_clr(dst
+1+dst_nparam
+dst_in
+dst_out
, div_off
);
1483 isl_seq_cpy(dst
+1+dst_nparam
+dst_in
+dst_out
+div_off
,
1484 src
+1+src_nparam
+src_in
+src_out
,
1485 isl_min(dst_map
->extra
-div_off
, src_map
->n_div
));
1486 if (dst_map
->n_div
-div_off
> src_map
->n_div
)
1487 isl_seq_clr(dst
+1+dst_nparam
+dst_in
+dst_out
+
1488 div_off
+src_map
->n_div
,
1489 dst_map
->n_div
- div_off
- src_map
->n_div
);
1492 static void copy_div(struct isl_basic_map
*dst_map
, isl_int
*dst
,
1493 struct isl_basic_map
*src_map
, isl_int
*src
,
1494 unsigned in_off
, unsigned out_off
, unsigned div_off
)
1496 isl_int_set(dst
[0], src
[0]);
1497 copy_constraint(dst_map
, dst
+1, src_map
, src
+1, in_off
, out_off
, div_off
);
1500 static struct isl_basic_map
*add_constraints(struct isl_basic_map
*bmap1
,
1501 struct isl_basic_map
*bmap2
, unsigned i_pos
, unsigned o_pos
)
1506 if (!bmap1
|| !bmap2
)
1509 div_off
= bmap1
->n_div
;
1511 for (i
= 0; i
< bmap2
->n_eq
; ++i
) {
1512 int i1
= isl_basic_map_alloc_equality(bmap1
);
1515 copy_constraint(bmap1
, bmap1
->eq
[i1
], bmap2
, bmap2
->eq
[i
],
1516 i_pos
, o_pos
, div_off
);
1519 for (i
= 0; i
< bmap2
->n_ineq
; ++i
) {
1520 int i1
= isl_basic_map_alloc_inequality(bmap1
);
1523 copy_constraint(bmap1
, bmap1
->ineq
[i1
], bmap2
, bmap2
->ineq
[i
],
1524 i_pos
, o_pos
, div_off
);
1527 for (i
= 0; i
< bmap2
->n_div
; ++i
) {
1528 int i1
= isl_basic_map_alloc_div(bmap1
);
1531 copy_div(bmap1
, bmap1
->div
[i1
], bmap2
, bmap2
->div
[i
],
1532 i_pos
, o_pos
, div_off
);
1535 isl_basic_map_free(bmap2
);
1540 isl_basic_map_free(bmap1
);
1541 isl_basic_map_free(bmap2
);
1545 struct isl_basic_set
*isl_basic_set_add_constraints(struct isl_basic_set
*bset1
,
1546 struct isl_basic_set
*bset2
, unsigned pos
)
1548 return (struct isl_basic_set
*)
1549 add_constraints((struct isl_basic_map
*)bset1
,
1550 (struct isl_basic_map
*)bset2
, 0, pos
);
1553 struct isl_basic_map
*isl_basic_map_extend_space(struct isl_basic_map
*base
,
1554 __isl_take isl_space
*dim
, unsigned extra
,
1555 unsigned n_eq
, unsigned n_ineq
)
1557 struct isl_basic_map
*ext
;
1567 dims_ok
= isl_space_is_equal(base
->dim
, dim
) &&
1568 base
->extra
>= base
->n_div
+ extra
;
1570 if (dims_ok
&& room_for_con(base
, n_eq
+ n_ineq
) &&
1571 room_for_ineq(base
, n_ineq
)) {
1572 isl_space_free(dim
);
1576 isl_assert(base
->ctx
, base
->dim
->nparam
<= dim
->nparam
, goto error
);
1577 isl_assert(base
->ctx
, base
->dim
->n_in
<= dim
->n_in
, goto error
);
1578 isl_assert(base
->ctx
, base
->dim
->n_out
<= dim
->n_out
, goto error
);
1579 extra
+= base
->extra
;
1581 n_ineq
+= base
->n_ineq
;
1583 ext
= isl_basic_map_alloc_space(dim
, extra
, n_eq
, n_ineq
);
1589 ext
->sample
= isl_vec_copy(base
->sample
);
1590 flags
= base
->flags
;
1591 ext
= add_constraints(ext
, base
, 0, 0);
1594 ISL_F_CLR(ext
, ISL_BASIC_SET_FINAL
);
1600 isl_space_free(dim
);
1601 isl_basic_map_free(base
);
1605 struct isl_basic_set
*isl_basic_set_extend_space(struct isl_basic_set
*base
,
1606 __isl_take isl_space
*dim
, unsigned extra
,
1607 unsigned n_eq
, unsigned n_ineq
)
1609 return (struct isl_basic_set
*)
1610 isl_basic_map_extend_space((struct isl_basic_map
*)base
, dim
,
1611 extra
, n_eq
, n_ineq
);
1614 struct isl_basic_map
*isl_basic_map_extend_constraints(
1615 struct isl_basic_map
*base
, unsigned n_eq
, unsigned n_ineq
)
1619 return isl_basic_map_extend_space(base
, isl_space_copy(base
->dim
),
1623 struct isl_basic_map
*isl_basic_map_extend(struct isl_basic_map
*base
,
1624 unsigned nparam
, unsigned n_in
, unsigned n_out
, unsigned extra
,
1625 unsigned n_eq
, unsigned n_ineq
)
1627 struct isl_basic_map
*bmap
;
1632 dim
= isl_space_alloc(base
->ctx
, nparam
, n_in
, n_out
);
1636 bmap
= isl_basic_map_extend_space(base
, dim
, extra
, n_eq
, n_ineq
);
1639 isl_basic_map_free(base
);
1643 struct isl_basic_set
*isl_basic_set_extend(struct isl_basic_set
*base
,
1644 unsigned nparam
, unsigned dim
, unsigned extra
,
1645 unsigned n_eq
, unsigned n_ineq
)
1647 return (struct isl_basic_set
*)
1648 isl_basic_map_extend((struct isl_basic_map
*)base
,
1649 nparam
, 0, dim
, extra
, n_eq
, n_ineq
);
1652 struct isl_basic_set
*isl_basic_set_extend_constraints(
1653 struct isl_basic_set
*base
, unsigned n_eq
, unsigned n_ineq
)
1655 return (struct isl_basic_set
*)
1656 isl_basic_map_extend_constraints((struct isl_basic_map
*)base
,
1660 struct isl_basic_set
*isl_basic_set_cow(struct isl_basic_set
*bset
)
1662 return (struct isl_basic_set
*)
1663 isl_basic_map_cow((struct isl_basic_map
*)bset
);
1666 struct isl_basic_map
*isl_basic_map_cow(struct isl_basic_map
*bmap
)
1671 if (bmap
->ref
> 1) {
1673 bmap
= isl_basic_map_dup(bmap
);
1676 ISL_F_CLR(bmap
, ISL_BASIC_SET_FINAL
);
1677 ISL_F_CLR(bmap
, ISL_BASIC_MAP_REDUCED_COEFFICIENTS
);
1682 struct isl_set
*isl_set_cow(struct isl_set
*set
)
1690 return isl_set_dup(set
);
1693 struct isl_map
*isl_map_cow(struct isl_map
*map
)
1701 return isl_map_dup(map
);
1704 static void swap_vars(struct isl_blk blk
, isl_int
*a
,
1705 unsigned a_len
, unsigned b_len
)
1707 isl_seq_cpy(blk
.data
, a
+a_len
, b_len
);
1708 isl_seq_cpy(blk
.data
+b_len
, a
, a_len
);
1709 isl_seq_cpy(a
, blk
.data
, b_len
+a_len
);
1712 static __isl_give isl_basic_map
*isl_basic_map_swap_vars(
1713 __isl_take isl_basic_map
*bmap
, unsigned pos
, unsigned n1
, unsigned n2
)
1721 isl_assert(bmap
->ctx
,
1722 pos
+ n1
+ n2
<= 1 + isl_basic_map_total_dim(bmap
), goto error
);
1724 if (n1
== 0 || n2
== 0)
1727 bmap
= isl_basic_map_cow(bmap
);
1731 blk
= isl_blk_alloc(bmap
->ctx
, n1
+ n2
);
1732 if (isl_blk_is_error(blk
))
1735 for (i
= 0; i
< bmap
->n_eq
; ++i
)
1737 bmap
->eq
[i
] + pos
, n1
, n2
);
1739 for (i
= 0; i
< bmap
->n_ineq
; ++i
)
1741 bmap
->ineq
[i
] + pos
, n1
, n2
);
1743 for (i
= 0; i
< bmap
->n_div
; ++i
)
1745 bmap
->div
[i
]+1 + pos
, n1
, n2
);
1747 isl_blk_free(bmap
->ctx
, blk
);
1749 ISL_F_CLR(bmap
, ISL_BASIC_SET_NORMALIZED
);
1750 bmap
= isl_basic_map_gauss(bmap
, NULL
);
1751 return isl_basic_map_finalize(bmap
);
1753 isl_basic_map_free(bmap
);
1757 struct isl_basic_map
*isl_basic_map_set_to_empty(struct isl_basic_map
*bmap
)
1763 total
= isl_basic_map_total_dim(bmap
);
1764 isl_basic_map_free_div(bmap
, bmap
->n_div
);
1765 isl_basic_map_free_inequality(bmap
, bmap
->n_ineq
);
1767 isl_basic_map_free_equality(bmap
, bmap
->n_eq
-1);
1769 i
= isl_basic_map_alloc_equality(bmap
);
1773 isl_int_set_si(bmap
->eq
[i
][0], 1);
1774 isl_seq_clr(bmap
->eq
[i
]+1, total
);
1775 ISL_F_SET(bmap
, ISL_BASIC_MAP_EMPTY
);
1776 isl_vec_free(bmap
->sample
);
1777 bmap
->sample
= NULL
;
1778 return isl_basic_map_finalize(bmap
);
1780 isl_basic_map_free(bmap
);
1784 struct isl_basic_set
*isl_basic_set_set_to_empty(struct isl_basic_set
*bset
)
1786 return (struct isl_basic_set
*)
1787 isl_basic_map_set_to_empty((struct isl_basic_map
*)bset
);
1790 /* Swap divs "a" and "b" in "bmap" (without modifying any of the constraints
1793 static void swap_div(__isl_keep isl_basic_map
*bmap
, int a
, int b
)
1795 isl_int
*t
= bmap
->div
[a
];
1796 bmap
->div
[a
] = bmap
->div
[b
];
1800 /* Swap divs "a" and "b" in "bmap" and adjust the constraints and
1801 * div definitions accordingly.
1803 void isl_basic_map_swap_div(struct isl_basic_map
*bmap
, int a
, int b
)
1806 unsigned off
= isl_space_dim(bmap
->dim
, isl_dim_all
);
1808 swap_div(bmap
, a
, b
);
1810 for (i
= 0; i
< bmap
->n_eq
; ++i
)
1811 isl_int_swap(bmap
->eq
[i
][1+off
+a
], bmap
->eq
[i
][1+off
+b
]);
1813 for (i
= 0; i
< bmap
->n_ineq
; ++i
)
1814 isl_int_swap(bmap
->ineq
[i
][1+off
+a
], bmap
->ineq
[i
][1+off
+b
]);
1816 for (i
= 0; i
< bmap
->n_div
; ++i
)
1817 isl_int_swap(bmap
->div
[i
][1+1+off
+a
], bmap
->div
[i
][1+1+off
+b
]);
1818 ISL_F_CLR(bmap
, ISL_BASIC_MAP_NORMALIZED
);
1821 /* Eliminate the specified n dimensions starting at first from the
1822 * constraints, without removing the dimensions from the space.
1823 * If the set is rational, the dimensions are eliminated using Fourier-Motzkin.
1825 __isl_give isl_map
*isl_map_eliminate(__isl_take isl_map
*map
,
1826 enum isl_dim_type type
, unsigned first
, unsigned n
)
1835 if (first
+ n
> isl_map_dim(map
, type
) || first
+ n
< first
)
1836 isl_die(map
->ctx
, isl_error_invalid
,
1837 "index out of bounds", goto error
);
1839 map
= isl_map_cow(map
);
1843 for (i
= 0; i
< map
->n
; ++i
) {
1844 map
->p
[i
] = isl_basic_map_eliminate(map
->p
[i
], type
, first
, n
);
1854 /* Eliminate the specified n dimensions starting at first from the
1855 * constraints, without removing the dimensions from the space.
1856 * If the set is rational, the dimensions are eliminated using Fourier-Motzkin.
1858 __isl_give isl_set
*isl_set_eliminate(__isl_take isl_set
*set
,
1859 enum isl_dim_type type
, unsigned first
, unsigned n
)
1861 return (isl_set
*)isl_map_eliminate((isl_map
*)set
, type
, first
, n
);
1864 /* Eliminate the specified n dimensions starting at first from the
1865 * constraints, without removing the dimensions from the space.
1866 * If the set is rational, the dimensions are eliminated using Fourier-Motzkin.
1868 __isl_give isl_set
*isl_set_eliminate_dims(__isl_take isl_set
*set
,
1869 unsigned first
, unsigned n
)
1871 return isl_set_eliminate(set
, isl_dim_set
, first
, n
);
1874 __isl_give isl_basic_map
*isl_basic_map_remove_divs(
1875 __isl_take isl_basic_map
*bmap
)
1879 bmap
= isl_basic_map_eliminate_vars(bmap
,
1880 isl_space_dim(bmap
->dim
, isl_dim_all
), bmap
->n_div
);
1884 return isl_basic_map_finalize(bmap
);
1887 __isl_give isl_basic_set
*isl_basic_set_remove_divs(
1888 __isl_take isl_basic_set
*bset
)
1890 return (struct isl_basic_set
*)isl_basic_map_remove_divs(
1891 (struct isl_basic_map
*)bset
);
1894 __isl_give isl_map
*isl_map_remove_divs(__isl_take isl_map
*map
)
1903 map
= isl_map_cow(map
);
1907 for (i
= 0; i
< map
->n
; ++i
) {
1908 map
->p
[i
] = isl_basic_map_remove_divs(map
->p
[i
]);
1918 __isl_give isl_set
*isl_set_remove_divs(__isl_take isl_set
*set
)
1920 return isl_map_remove_divs(set
);
1923 struct isl_basic_map
*isl_basic_map_remove_dims(struct isl_basic_map
*bmap
,
1924 enum isl_dim_type type
, unsigned first
, unsigned n
)
1928 isl_assert(bmap
->ctx
, first
+ n
<= isl_basic_map_dim(bmap
, type
),
1930 if (n
== 0 && !isl_space_is_named_or_nested(bmap
->dim
, type
))
1932 bmap
= isl_basic_map_eliminate_vars(bmap
,
1933 isl_basic_map_offset(bmap
, type
) - 1 + first
, n
);
1936 if (ISL_F_ISSET(bmap
, ISL_BASIC_MAP_EMPTY
) && type
== isl_dim_div
)
1938 bmap
= isl_basic_map_drop(bmap
, type
, first
, n
);
1941 isl_basic_map_free(bmap
);
1945 /* Return true if the definition of the given div (recursively) involves
1946 * any of the given variables.
1948 static int div_involves_vars(__isl_keep isl_basic_map
*bmap
, int div
,
1949 unsigned first
, unsigned n
)
1952 unsigned div_offset
= isl_basic_map_offset(bmap
, isl_dim_div
);
1954 if (isl_int_is_zero(bmap
->div
[div
][0]))
1956 if (isl_seq_first_non_zero(bmap
->div
[div
] + 1 + first
, n
) >= 0)
1959 for (i
= bmap
->n_div
- 1; i
>= 0; --i
) {
1960 if (isl_int_is_zero(bmap
->div
[div
][1 + div_offset
+ i
]))
1962 if (div_involves_vars(bmap
, i
, first
, n
))
1969 /* Try and add a lower and/or upper bound on "div" to "bmap"
1970 * based on inequality "i".
1971 * "total" is the total number of variables (excluding the divs).
1972 * "v" is a temporary object that can be used during the calculations.
1973 * If "lb" is set, then a lower bound should be constructed.
1974 * If "ub" is set, then an upper bound should be constructed.
1976 * The calling function has already checked that the inequality does not
1977 * reference "div", but we still need to check that the inequality is
1978 * of the right form. We'll consider the case where we want to construct
1979 * a lower bound. The construction of upper bounds is similar.
1981 * Let "div" be of the form
1983 * q = floor((a + f(x))/d)
1985 * We essentially check if constraint "i" is of the form
1989 * so that we can use it to derive a lower bound on "div".
1990 * However, we allow a slightly more general form
1994 * with the condition that the coefficients of g(x) - f(x) are all
1996 * Rewriting this constraint as
2000 * adding a + f(x) to both sides and dividing by d, we obtain
2002 * (a + f(x))/d >= (a-b)/d + (f(x)-g(x))/d
2004 * Taking the floor on both sides, we obtain
2006 * q >= floor((a-b)/d) + (f(x)-g(x))/d
2010 * (g(x)-f(x))/d + ceil((b-a)/d) + q >= 0
2012 * In the case of an upper bound, we construct the constraint
2014 * (g(x)+f(x))/d + floor((b+a)/d) - q >= 0
2017 static __isl_give isl_basic_map
*insert_bounds_on_div_from_ineq(
2018 __isl_take isl_basic_map
*bmap
, int div
, int i
,
2019 unsigned total
, isl_int v
, int lb
, int ub
)
2023 for (j
= 0; (lb
|| ub
) && j
< total
+ bmap
->n_div
; ++j
) {
2025 isl_int_sub(v
, bmap
->ineq
[i
][1 + j
],
2026 bmap
->div
[div
][1 + 1 + j
]);
2027 lb
= isl_int_is_divisible_by(v
, bmap
->div
[div
][0]);
2030 isl_int_add(v
, bmap
->ineq
[i
][1 + j
],
2031 bmap
->div
[div
][1 + 1 + j
]);
2032 ub
= isl_int_is_divisible_by(v
, bmap
->div
[div
][0]);
2038 bmap
= isl_basic_map_cow(bmap
);
2039 bmap
= isl_basic_map_extend_constraints(bmap
, 0, lb
+ ub
);
2041 int k
= isl_basic_map_alloc_inequality(bmap
);
2044 for (j
= 0; j
< 1 + total
+ bmap
->n_div
; ++j
) {
2045 isl_int_sub(bmap
->ineq
[k
][j
], bmap
->ineq
[i
][j
],
2046 bmap
->div
[div
][1 + j
]);
2047 isl_int_cdiv_q(bmap
->ineq
[k
][j
],
2048 bmap
->ineq
[k
][j
], bmap
->div
[div
][0]);
2050 isl_int_set_si(bmap
->ineq
[k
][1 + total
+ div
], 1);
2053 int k
= isl_basic_map_alloc_inequality(bmap
);
2056 for (j
= 0; j
< 1 + total
+ bmap
->n_div
; ++j
) {
2057 isl_int_add(bmap
->ineq
[k
][j
], bmap
->ineq
[i
][j
],
2058 bmap
->div
[div
][1 + j
]);
2059 isl_int_fdiv_q(bmap
->ineq
[k
][j
],
2060 bmap
->ineq
[k
][j
], bmap
->div
[div
][0]);
2062 isl_int_set_si(bmap
->ineq
[k
][1 + total
+ div
], -1);
2065 ISL_F_CLR(bmap
, ISL_BASIC_MAP_NORMALIZED
);
2068 isl_basic_map_free(bmap
);
2072 /* This function is called right before "div" is eliminated from "bmap"
2073 * using Fourier-Motzkin.
2074 * Look through the constraints of "bmap" for constraints on the argument
2075 * of the integer division and use them to construct constraints on the
2076 * integer division itself. These constraints can then be combined
2077 * during the Fourier-Motzkin elimination.
2078 * Note that it is only useful to introduce lower bounds on "div"
2079 * if "bmap" already contains upper bounds on "div" as the newly
2080 * introduce lower bounds can then be combined with the pre-existing
2081 * upper bounds. Similarly for upper bounds.
2082 * We therefore first check if "bmap" contains any lower and/or upper bounds
2085 * It is interesting to note that the introduction of these constraints
2086 * can indeed lead to more accurate results, even when compared to
2087 * deriving constraints on the argument of "div" from constraints on "div".
2088 * Consider, for example, the set
2090 * { [i,j,k] : 3 + i + 2j >= 0 and 2 * [(i+2j)/4] <= k }
2092 * The second constraint can be rewritten as
2094 * 2 * [(-i-2j+3)/4] + k >= 0
2096 * from which we can derive
2098 * -i - 2j + 3 >= -2k
2104 * Combined with the first constraint, we obtain
2106 * -3 <= 3 + 2k or k >= -3
2108 * If, on the other hand we derive a constraint on [(i+2j)/4] from
2109 * the first constraint, we obtain
2111 * [(i + 2j)/4] >= [-3/4] = -1
2113 * Combining this constraint with the second constraint, we obtain
2117 static __isl_give isl_basic_map
*insert_bounds_on_div(
2118 __isl_take isl_basic_map
*bmap
, int div
)
2121 int check_lb
, check_ub
;
2128 if (isl_int_is_zero(bmap
->div
[div
][0]))
2131 total
= isl_space_dim(bmap
->dim
, isl_dim_all
);
2135 for (i
= 0; (!check_lb
|| !check_ub
) && i
< bmap
->n_ineq
; ++i
) {
2136 int s
= isl_int_sgn(bmap
->ineq
[i
][1 + total
+ div
]);
2143 if (!check_lb
&& !check_ub
)
2148 for (i
= 0; bmap
&& i
< bmap
->n_ineq
; ++i
) {
2149 if (!isl_int_is_zero(bmap
->ineq
[i
][1 + total
+ div
]))
2152 bmap
= insert_bounds_on_div_from_ineq(bmap
, div
, i
, total
, v
,
2153 check_lb
, check_ub
);
2161 /* Remove all divs (recursively) involving any of the given dimensions
2162 * in their definitions.
2164 __isl_give isl_basic_map
*isl_basic_map_remove_divs_involving_dims(
2165 __isl_take isl_basic_map
*bmap
,
2166 enum isl_dim_type type
, unsigned first
, unsigned n
)
2172 isl_assert(bmap
->ctx
, first
+ n
<= isl_basic_map_dim(bmap
, type
),
2174 first
+= isl_basic_map_offset(bmap
, type
);
2176 for (i
= bmap
->n_div
- 1; i
>= 0; --i
) {
2177 if (!div_involves_vars(bmap
, i
, first
, n
))
2179 bmap
= insert_bounds_on_div(bmap
, i
);
2180 bmap
= isl_basic_map_remove_dims(bmap
, isl_dim_div
, i
, 1);
2188 isl_basic_map_free(bmap
);
2192 __isl_give isl_basic_set
*isl_basic_set_remove_divs_involving_dims(
2193 __isl_take isl_basic_set
*bset
,
2194 enum isl_dim_type type
, unsigned first
, unsigned n
)
2196 return isl_basic_map_remove_divs_involving_dims(bset
, type
, first
, n
);
2199 __isl_give isl_map
*isl_map_remove_divs_involving_dims(__isl_take isl_map
*map
,
2200 enum isl_dim_type type
, unsigned first
, unsigned n
)
2209 map
= isl_map_cow(map
);
2213 for (i
= 0; i
< map
->n
; ++i
) {
2214 map
->p
[i
] = isl_basic_map_remove_divs_involving_dims(map
->p
[i
],
2225 __isl_give isl_set
*isl_set_remove_divs_involving_dims(__isl_take isl_set
*set
,
2226 enum isl_dim_type type
, unsigned first
, unsigned n
)
2228 return (isl_set
*)isl_map_remove_divs_involving_dims((isl_map
*)set
,
2232 /* Does the desciption of "bmap" depend on the specified dimensions?
2233 * We also check whether the dimensions appear in any of the div definitions.
2234 * In principle there is no need for this check. If the dimensions appear
2235 * in a div definition, they also appear in the defining constraints of that
2238 isl_bool
isl_basic_map_involves_dims(__isl_keep isl_basic_map
*bmap
,
2239 enum isl_dim_type type
, unsigned first
, unsigned n
)
2244 return isl_bool_error
;
2246 if (first
+ n
> isl_basic_map_dim(bmap
, type
))
2247 isl_die(bmap
->ctx
, isl_error_invalid
,
2248 "index out of bounds", return isl_bool_error
);
2250 first
+= isl_basic_map_offset(bmap
, type
);
2251 for (i
= 0; i
< bmap
->n_eq
; ++i
)
2252 if (isl_seq_first_non_zero(bmap
->eq
[i
] + first
, n
) >= 0)
2253 return isl_bool_true
;
2254 for (i
= 0; i
< bmap
->n_ineq
; ++i
)
2255 if (isl_seq_first_non_zero(bmap
->ineq
[i
] + first
, n
) >= 0)
2256 return isl_bool_true
;
2257 for (i
= 0; i
< bmap
->n_div
; ++i
) {
2258 if (isl_int_is_zero(bmap
->div
[i
][0]))
2260 if (isl_seq_first_non_zero(bmap
->div
[i
] + 1 + first
, n
) >= 0)
2261 return isl_bool_true
;
2264 return isl_bool_false
;
2267 isl_bool
isl_map_involves_dims(__isl_keep isl_map
*map
,
2268 enum isl_dim_type type
, unsigned first
, unsigned n
)
2273 return isl_bool_error
;
2275 if (first
+ n
> isl_map_dim(map
, type
))
2276 isl_die(map
->ctx
, isl_error_invalid
,
2277 "index out of bounds", return isl_bool_error
);
2279 for (i
= 0; i
< map
->n
; ++i
) {
2280 isl_bool involves
= isl_basic_map_involves_dims(map
->p
[i
],
2282 if (involves
< 0 || involves
)
2286 return isl_bool_false
;
2289 isl_bool
isl_basic_set_involves_dims(__isl_keep isl_basic_set
*bset
,
2290 enum isl_dim_type type
, unsigned first
, unsigned n
)
2292 return isl_basic_map_involves_dims(bset
, type
, first
, n
);
2295 isl_bool
isl_set_involves_dims(__isl_keep isl_set
*set
,
2296 enum isl_dim_type type
, unsigned first
, unsigned n
)
2298 return isl_map_involves_dims(set
, type
, first
, n
);
2301 /* Return true if the definition of the given div is unknown or depends
2304 static int div_is_unknown(__isl_keep isl_basic_map
*bmap
, int div
)
2307 unsigned div_offset
= isl_basic_map_offset(bmap
, isl_dim_div
);
2309 if (isl_int_is_zero(bmap
->div
[div
][0]))
2312 for (i
= bmap
->n_div
- 1; i
>= 0; --i
) {
2313 if (isl_int_is_zero(bmap
->div
[div
][1 + div_offset
+ i
]))
2315 if (div_is_unknown(bmap
, i
))
2322 /* Remove all divs that are unknown or defined in terms of unknown divs.
2324 __isl_give isl_basic_map
*isl_basic_map_remove_unknown_divs(
2325 __isl_take isl_basic_map
*bmap
)
2332 for (i
= bmap
->n_div
- 1; i
>= 0; --i
) {
2333 if (!div_is_unknown(bmap
, i
))
2335 bmap
= isl_basic_map_remove_dims(bmap
, isl_dim_div
, i
, 1);
2344 /* Remove all divs that are unknown or defined in terms of unknown divs.
2346 __isl_give isl_basic_set
*isl_basic_set_remove_unknown_divs(
2347 __isl_take isl_basic_set
*bset
)
2349 return isl_basic_map_remove_unknown_divs(bset
);
2352 __isl_give isl_map
*isl_map_remove_unknown_divs(__isl_take isl_map
*map
)
2361 map
= isl_map_cow(map
);
2365 for (i
= 0; i
< map
->n
; ++i
) {
2366 map
->p
[i
] = isl_basic_map_remove_unknown_divs(map
->p
[i
]);
2376 __isl_give isl_set
*isl_set_remove_unknown_divs(__isl_take isl_set
*set
)
2378 return (isl_set
*)isl_map_remove_unknown_divs((isl_map
*)set
);
2381 __isl_give isl_basic_set
*isl_basic_set_remove_dims(
2382 __isl_take isl_basic_set
*bset
,
2383 enum isl_dim_type type
, unsigned first
, unsigned n
)
2385 return (isl_basic_set
*)
2386 isl_basic_map_remove_dims((isl_basic_map
*)bset
, type
, first
, n
);
2389 struct isl_map
*isl_map_remove_dims(struct isl_map
*map
,
2390 enum isl_dim_type type
, unsigned first
, unsigned n
)
2397 map
= isl_map_cow(map
);
2400 isl_assert(map
->ctx
, first
+ n
<= isl_map_dim(map
, type
), goto error
);
2402 for (i
= 0; i
< map
->n
; ++i
) {
2403 map
->p
[i
] = isl_basic_map_eliminate_vars(map
->p
[i
],
2404 isl_basic_map_offset(map
->p
[i
], type
) - 1 + first
, n
);
2408 map
= isl_map_drop(map
, type
, first
, n
);
2415 __isl_give isl_set
*isl_set_remove_dims(__isl_take isl_set
*bset
,
2416 enum isl_dim_type type
, unsigned first
, unsigned n
)
2418 return (isl_set
*)isl_map_remove_dims((isl_map
*)bset
, type
, first
, n
);
2421 /* Project out n inputs starting at first using Fourier-Motzkin */
2422 struct isl_map
*isl_map_remove_inputs(struct isl_map
*map
,
2423 unsigned first
, unsigned n
)
2425 return isl_map_remove_dims(map
, isl_dim_in
, first
, n
);
2428 static void dump_term(struct isl_basic_map
*bmap
,
2429 isl_int c
, int pos
, FILE *out
)
2432 unsigned in
= isl_basic_map_n_in(bmap
);
2433 unsigned dim
= in
+ isl_basic_map_n_out(bmap
);
2434 unsigned nparam
= isl_basic_map_n_param(bmap
);
2436 isl_int_print(out
, c
, 0);
2438 if (!isl_int_is_one(c
))
2439 isl_int_print(out
, c
, 0);
2440 if (pos
< 1 + nparam
) {
2441 name
= isl_space_get_dim_name(bmap
->dim
,
2442 isl_dim_param
, pos
- 1);
2444 fprintf(out
, "%s", name
);
2446 fprintf(out
, "p%d", pos
- 1);
2447 } else if (pos
< 1 + nparam
+ in
)
2448 fprintf(out
, "i%d", pos
- 1 - nparam
);
2449 else if (pos
< 1 + nparam
+ dim
)
2450 fprintf(out
, "o%d", pos
- 1 - nparam
- in
);
2452 fprintf(out
, "e%d", pos
- 1 - nparam
- dim
);
2456 static void dump_constraint_sign(struct isl_basic_map
*bmap
, isl_int
*c
,
2457 int sign
, FILE *out
)
2461 unsigned len
= 1 + isl_basic_map_total_dim(bmap
);
2465 for (i
= 0, first
= 1; i
< len
; ++i
) {
2466 if (isl_int_sgn(c
[i
]) * sign
<= 0)
2469 fprintf(out
, " + ");
2471 isl_int_abs(v
, c
[i
]);
2472 dump_term(bmap
, v
, i
, out
);
2479 static void dump_constraint(struct isl_basic_map
*bmap
, isl_int
*c
,
2480 const char *op
, FILE *out
, int indent
)
2484 fprintf(out
, "%*s", indent
, "");
2486 dump_constraint_sign(bmap
, c
, 1, out
);
2487 fprintf(out
, " %s ", op
);
2488 dump_constraint_sign(bmap
, c
, -1, out
);
2492 for (i
= bmap
->n_div
; i
< bmap
->extra
; ++i
) {
2493 if (isl_int_is_zero(c
[1+isl_space_dim(bmap
->dim
, isl_dim_all
)+i
]))
2495 fprintf(out
, "%*s", indent
, "");
2496 fprintf(out
, "ERROR: unused div coefficient not zero\n");
2501 static void dump_constraints(struct isl_basic_map
*bmap
,
2502 isl_int
**c
, unsigned n
,
2503 const char *op
, FILE *out
, int indent
)
2507 for (i
= 0; i
< n
; ++i
)
2508 dump_constraint(bmap
, c
[i
], op
, out
, indent
);
2511 static void dump_affine(struct isl_basic_map
*bmap
, isl_int
*exp
, FILE *out
)
2515 unsigned total
= isl_basic_map_total_dim(bmap
);
2517 for (j
= 0; j
< 1 + total
; ++j
) {
2518 if (isl_int_is_zero(exp
[j
]))
2520 if (!first
&& isl_int_is_pos(exp
[j
]))
2522 dump_term(bmap
, exp
[j
], j
, out
);
2527 static void dump(struct isl_basic_map
*bmap
, FILE *out
, int indent
)
2531 dump_constraints(bmap
, bmap
->eq
, bmap
->n_eq
, "=", out
, indent
);
2532 dump_constraints(bmap
, bmap
->ineq
, bmap
->n_ineq
, ">=", out
, indent
);
2534 for (i
= 0; i
< bmap
->n_div
; ++i
) {
2535 fprintf(out
, "%*s", indent
, "");
2536 fprintf(out
, "e%d = [(", i
);
2537 dump_affine(bmap
, bmap
->div
[i
]+1, out
);
2539 isl_int_print(out
, bmap
->div
[i
][0], 0);
2540 fprintf(out
, "]\n");
2544 void isl_basic_set_print_internal(struct isl_basic_set
*bset
,
2545 FILE *out
, int indent
)
2548 fprintf(out
, "null basic set\n");
2552 fprintf(out
, "%*s", indent
, "");
2553 fprintf(out
, "ref: %d, nparam: %d, dim: %d, extra: %d, flags: %x\n",
2554 bset
->ref
, bset
->dim
->nparam
, bset
->dim
->n_out
,
2555 bset
->extra
, bset
->flags
);
2556 dump((struct isl_basic_map
*)bset
, out
, indent
);
2559 void isl_basic_map_print_internal(struct isl_basic_map
*bmap
,
2560 FILE *out
, int indent
)
2563 fprintf(out
, "null basic map\n");
2567 fprintf(out
, "%*s", indent
, "");
2568 fprintf(out
, "ref: %d, nparam: %d, in: %d, out: %d, extra: %d, "
2569 "flags: %x, n_name: %d\n",
2571 bmap
->dim
->nparam
, bmap
->dim
->n_in
, bmap
->dim
->n_out
,
2572 bmap
->extra
, bmap
->flags
, bmap
->dim
->n_id
);
2573 dump(bmap
, out
, indent
);
2576 int isl_inequality_negate(struct isl_basic_map
*bmap
, unsigned pos
)
2581 total
= isl_basic_map_total_dim(bmap
);
2582 isl_assert(bmap
->ctx
, pos
< bmap
->n_ineq
, return -1);
2583 isl_seq_neg(bmap
->ineq
[pos
], bmap
->ineq
[pos
], 1 + total
);
2584 isl_int_sub_ui(bmap
->ineq
[pos
][0], bmap
->ineq
[pos
][0], 1);
2585 ISL_F_CLR(bmap
, ISL_BASIC_MAP_NORMALIZED
);
2589 __isl_give isl_set
*isl_set_alloc_space(__isl_take isl_space
*dim
, int n
,
2592 struct isl_set
*set
;
2596 isl_assert(dim
->ctx
, dim
->n_in
== 0, goto error
);
2597 isl_assert(dim
->ctx
, n
>= 0, goto error
);
2598 set
= isl_alloc(dim
->ctx
, struct isl_set
,
2599 sizeof(struct isl_set
) +
2600 (n
- 1) * sizeof(struct isl_basic_set
*));
2604 set
->ctx
= dim
->ctx
;
2605 isl_ctx_ref(set
->ctx
);
2613 isl_space_free(dim
);
2617 struct isl_set
*isl_set_alloc(struct isl_ctx
*ctx
,
2618 unsigned nparam
, unsigned dim
, int n
, unsigned flags
)
2620 struct isl_set
*set
;
2623 dims
= isl_space_alloc(ctx
, nparam
, 0, dim
);
2627 set
= isl_set_alloc_space(dims
, n
, flags
);
2631 /* Make sure "map" has room for at least "n" more basic maps.
2633 struct isl_map
*isl_map_grow(struct isl_map
*map
, int n
)
2636 struct isl_map
*grown
= NULL
;
2640 isl_assert(map
->ctx
, n
>= 0, goto error
);
2641 if (map
->n
+ n
<= map
->size
)
2643 grown
= isl_map_alloc_space(isl_map_get_space(map
), map
->n
+ n
, map
->flags
);
2646 for (i
= 0; i
< map
->n
; ++i
) {
2647 grown
->p
[i
] = isl_basic_map_copy(map
->p
[i
]);
2655 isl_map_free(grown
);
2660 /* Make sure "set" has room for at least "n" more basic sets.
2662 struct isl_set
*isl_set_grow(struct isl_set
*set
, int n
)
2664 return (struct isl_set
*)isl_map_grow((struct isl_map
*)set
, n
);
2667 struct isl_set
*isl_set_dup(struct isl_set
*set
)
2670 struct isl_set
*dup
;
2675 dup
= isl_set_alloc_space(isl_space_copy(set
->dim
), set
->n
, set
->flags
);
2678 for (i
= 0; i
< set
->n
; ++i
)
2679 dup
= isl_set_add_basic_set(dup
, isl_basic_set_copy(set
->p
[i
]));
2683 struct isl_set
*isl_set_from_basic_set(struct isl_basic_set
*bset
)
2685 return isl_map_from_basic_map(bset
);
2688 struct isl_map
*isl_map_from_basic_map(struct isl_basic_map
*bmap
)
2690 struct isl_map
*map
;
2695 map
= isl_map_alloc_space(isl_space_copy(bmap
->dim
), 1, ISL_MAP_DISJOINT
);
2696 return isl_map_add_basic_map(map
, bmap
);
2699 __isl_give isl_set
*isl_set_add_basic_set(__isl_take isl_set
*set
,
2700 __isl_take isl_basic_set
*bset
)
2702 return (struct isl_set
*)isl_map_add_basic_map((struct isl_map
*)set
,
2703 (struct isl_basic_map
*)bset
);
2706 __isl_null isl_set
*isl_set_free(__isl_take isl_set
*set
)
2716 isl_ctx_deref(set
->ctx
);
2717 for (i
= 0; i
< set
->n
; ++i
)
2718 isl_basic_set_free(set
->p
[i
]);
2719 isl_space_free(set
->dim
);
2725 void isl_set_print_internal(struct isl_set
*set
, FILE *out
, int indent
)
2730 fprintf(out
, "null set\n");
2734 fprintf(out
, "%*s", indent
, "");
2735 fprintf(out
, "ref: %d, n: %d, nparam: %d, dim: %d, flags: %x\n",
2736 set
->ref
, set
->n
, set
->dim
->nparam
, set
->dim
->n_out
,
2738 for (i
= 0; i
< set
->n
; ++i
) {
2739 fprintf(out
, "%*s", indent
, "");
2740 fprintf(out
, "basic set %d:\n", i
);
2741 isl_basic_set_print_internal(set
->p
[i
], out
, indent
+4);
2745 void isl_map_print_internal(struct isl_map
*map
, FILE *out
, int indent
)
2750 fprintf(out
, "null map\n");
2754 fprintf(out
, "%*s", indent
, "");
2755 fprintf(out
, "ref: %d, n: %d, nparam: %d, in: %d, out: %d, "
2756 "flags: %x, n_name: %d\n",
2757 map
->ref
, map
->n
, map
->dim
->nparam
, map
->dim
->n_in
,
2758 map
->dim
->n_out
, map
->flags
, map
->dim
->n_id
);
2759 for (i
= 0; i
< map
->n
; ++i
) {
2760 fprintf(out
, "%*s", indent
, "");
2761 fprintf(out
, "basic map %d:\n", i
);
2762 isl_basic_map_print_internal(map
->p
[i
], out
, indent
+4);
2766 struct isl_basic_map
*isl_basic_map_intersect_domain(
2767 struct isl_basic_map
*bmap
, struct isl_basic_set
*bset
)
2769 struct isl_basic_map
*bmap_domain
;
2774 isl_assert(bset
->ctx
, isl_space_match(bmap
->dim
, isl_dim_param
,
2775 bset
->dim
, isl_dim_param
), goto error
);
2777 if (isl_space_dim(bset
->dim
, isl_dim_set
) != 0)
2778 isl_assert(bset
->ctx
,
2779 isl_basic_map_compatible_domain(bmap
, bset
), goto error
);
2781 bmap
= isl_basic_map_cow(bmap
);
2784 bmap
= isl_basic_map_extend_space(bmap
, isl_space_copy(bmap
->dim
),
2785 bset
->n_div
, bset
->n_eq
, bset
->n_ineq
);
2786 bmap_domain
= isl_basic_map_from_domain(bset
);
2787 bmap
= add_constraints(bmap
, bmap_domain
, 0, 0);
2789 bmap
= isl_basic_map_simplify(bmap
);
2790 return isl_basic_map_finalize(bmap
);
2792 isl_basic_map_free(bmap
);
2793 isl_basic_set_free(bset
);
2797 struct isl_basic_map
*isl_basic_map_intersect_range(
2798 struct isl_basic_map
*bmap
, struct isl_basic_set
*bset
)
2800 struct isl_basic_map
*bmap_range
;
2805 isl_assert(bset
->ctx
, isl_space_match(bmap
->dim
, isl_dim_param
,
2806 bset
->dim
, isl_dim_param
), goto error
);
2808 if (isl_space_dim(bset
->dim
, isl_dim_set
) != 0)
2809 isl_assert(bset
->ctx
,
2810 isl_basic_map_compatible_range(bmap
, bset
), goto error
);
2812 if (isl_basic_set_plain_is_universe(bset
)) {
2813 isl_basic_set_free(bset
);
2817 bmap
= isl_basic_map_cow(bmap
);
2820 bmap
= isl_basic_map_extend_space(bmap
, isl_space_copy(bmap
->dim
),
2821 bset
->n_div
, bset
->n_eq
, bset
->n_ineq
);
2822 bmap_range
= isl_basic_map_from_basic_set(bset
, isl_space_copy(bset
->dim
));
2823 bmap
= add_constraints(bmap
, bmap_range
, 0, 0);
2825 bmap
= isl_basic_map_simplify(bmap
);
2826 return isl_basic_map_finalize(bmap
);
2828 isl_basic_map_free(bmap
);
2829 isl_basic_set_free(bset
);
2833 isl_bool
isl_basic_map_contains(__isl_keep isl_basic_map
*bmap
,
2834 __isl_keep isl_vec
*vec
)
2841 return isl_bool_error
;
2843 total
= 1 + isl_basic_map_total_dim(bmap
);
2844 if (total
!= vec
->size
)
2845 return isl_bool_error
;
2849 for (i
= 0; i
< bmap
->n_eq
; ++i
) {
2850 isl_seq_inner_product(vec
->el
, bmap
->eq
[i
], total
, &s
);
2851 if (!isl_int_is_zero(s
)) {
2853 return isl_bool_false
;
2857 for (i
= 0; i
< bmap
->n_ineq
; ++i
) {
2858 isl_seq_inner_product(vec
->el
, bmap
->ineq
[i
], total
, &s
);
2859 if (isl_int_is_neg(s
)) {
2861 return isl_bool_false
;
2867 return isl_bool_true
;
2870 isl_bool
isl_basic_set_contains(__isl_keep isl_basic_set
*bset
,
2871 __isl_keep isl_vec
*vec
)
2873 return isl_basic_map_contains((struct isl_basic_map
*)bset
, vec
);
2876 struct isl_basic_map
*isl_basic_map_intersect(
2877 struct isl_basic_map
*bmap1
, struct isl_basic_map
*bmap2
)
2879 struct isl_vec
*sample
= NULL
;
2881 if (!bmap1
|| !bmap2
)
2884 isl_assert(bmap1
->ctx
, isl_space_match(bmap1
->dim
, isl_dim_param
,
2885 bmap2
->dim
, isl_dim_param
), goto error
);
2886 if (isl_space_dim(bmap1
->dim
, isl_dim_all
) ==
2887 isl_space_dim(bmap1
->dim
, isl_dim_param
) &&
2888 isl_space_dim(bmap2
->dim
, isl_dim_all
) !=
2889 isl_space_dim(bmap2
->dim
, isl_dim_param
))
2890 return isl_basic_map_intersect(bmap2
, bmap1
);
2892 if (isl_space_dim(bmap2
->dim
, isl_dim_all
) !=
2893 isl_space_dim(bmap2
->dim
, isl_dim_param
))
2894 isl_assert(bmap1
->ctx
,
2895 isl_space_is_equal(bmap1
->dim
, bmap2
->dim
), goto error
);
2897 if (isl_basic_map_plain_is_empty(bmap1
)) {
2898 isl_basic_map_free(bmap2
);
2901 if (isl_basic_map_plain_is_empty(bmap2
)) {
2902 isl_basic_map_free(bmap1
);
2906 if (bmap1
->sample
&&
2907 isl_basic_map_contains(bmap1
, bmap1
->sample
) > 0 &&
2908 isl_basic_map_contains(bmap2
, bmap1
->sample
) > 0)
2909 sample
= isl_vec_copy(bmap1
->sample
);
2910 else if (bmap2
->sample
&&
2911 isl_basic_map_contains(bmap1
, bmap2
->sample
) > 0 &&
2912 isl_basic_map_contains(bmap2
, bmap2
->sample
) > 0)
2913 sample
= isl_vec_copy(bmap2
->sample
);
2915 bmap1
= isl_basic_map_cow(bmap1
);
2918 bmap1
= isl_basic_map_extend_space(bmap1
, isl_space_copy(bmap1
->dim
),
2919 bmap2
->n_div
, bmap2
->n_eq
, bmap2
->n_ineq
);
2920 bmap1
= add_constraints(bmap1
, bmap2
, 0, 0);
2923 isl_vec_free(sample
);
2925 isl_vec_free(bmap1
->sample
);
2926 bmap1
->sample
= sample
;
2929 bmap1
= isl_basic_map_simplify(bmap1
);
2930 return isl_basic_map_finalize(bmap1
);
2933 isl_vec_free(sample
);
2934 isl_basic_map_free(bmap1
);
2935 isl_basic_map_free(bmap2
);
2939 struct isl_basic_set
*isl_basic_set_intersect(
2940 struct isl_basic_set
*bset1
, struct isl_basic_set
*bset2
)
2942 return (struct isl_basic_set
*)
2943 isl_basic_map_intersect(
2944 (struct isl_basic_map
*)bset1
,
2945 (struct isl_basic_map
*)bset2
);
2948 __isl_give isl_basic_set
*isl_basic_set_intersect_params(
2949 __isl_take isl_basic_set
*bset1
, __isl_take isl_basic_set
*bset2
)
2951 return isl_basic_set_intersect(bset1
, bset2
);
2954 /* Special case of isl_map_intersect, where both map1 and map2
2955 * are convex, without any divs and such that either map1 or map2
2956 * contains a single constraint. This constraint is then simply
2957 * added to the other map.
2959 static __isl_give isl_map
*map_intersect_add_constraint(
2960 __isl_take isl_map
*map1
, __isl_take isl_map
*map2
)
2962 isl_assert(map1
->ctx
, map1
->n
== 1, goto error
);
2963 isl_assert(map2
->ctx
, map1
->n
== 1, goto error
);
2964 isl_assert(map1
->ctx
, map1
->p
[0]->n_div
== 0, goto error
);
2965 isl_assert(map2
->ctx
, map1
->p
[0]->n_div
== 0, goto error
);
2967 if (map2
->p
[0]->n_eq
+ map2
->p
[0]->n_ineq
!= 1)
2968 return isl_map_intersect(map2
, map1
);
2970 isl_assert(map2
->ctx
,
2971 map2
->p
[0]->n_eq
+ map2
->p
[0]->n_ineq
== 1, goto error
);
2973 map1
= isl_map_cow(map1
);
2976 if (isl_map_plain_is_empty(map1
)) {
2980 map1
->p
[0] = isl_basic_map_cow(map1
->p
[0]);
2981 if (map2
->p
[0]->n_eq
== 1)
2982 map1
->p
[0] = isl_basic_map_add_eq(map1
->p
[0], map2
->p
[0]->eq
[0]);
2984 map1
->p
[0] = isl_basic_map_add_ineq(map1
->p
[0],
2985 map2
->p
[0]->ineq
[0]);
2987 map1
->p
[0] = isl_basic_map_simplify(map1
->p
[0]);
2988 map1
->p
[0] = isl_basic_map_finalize(map1
->p
[0]);
2992 if (isl_basic_map_plain_is_empty(map1
->p
[0])) {
2993 isl_basic_map_free(map1
->p
[0]);
3006 /* map2 may be either a parameter domain or a map living in the same
3009 static __isl_give isl_map
*map_intersect_internal(__isl_take isl_map
*map1
,
3010 __isl_take isl_map
*map2
)
3019 if ((isl_map_plain_is_empty(map1
) ||
3020 isl_map_plain_is_universe(map2
)) &&
3021 isl_space_is_equal(map1
->dim
, map2
->dim
)) {
3025 if ((isl_map_plain_is_empty(map2
) ||
3026 isl_map_plain_is_universe(map1
)) &&
3027 isl_space_is_equal(map1
->dim
, map2
->dim
)) {
3032 if (map1
->n
== 1 && map2
->n
== 1 &&
3033 map1
->p
[0]->n_div
== 0 && map2
->p
[0]->n_div
== 0 &&
3034 isl_space_is_equal(map1
->dim
, map2
->dim
) &&
3035 (map1
->p
[0]->n_eq
+ map1
->p
[0]->n_ineq
== 1 ||
3036 map2
->p
[0]->n_eq
+ map2
->p
[0]->n_ineq
== 1))
3037 return map_intersect_add_constraint(map1
, map2
);
3039 if (isl_space_dim(map2
->dim
, isl_dim_all
) !=
3040 isl_space_dim(map2
->dim
, isl_dim_param
))
3041 isl_assert(map1
->ctx
,
3042 isl_space_is_equal(map1
->dim
, map2
->dim
), goto error
);
3044 if (ISL_F_ISSET(map1
, ISL_MAP_DISJOINT
) &&
3045 ISL_F_ISSET(map2
, ISL_MAP_DISJOINT
))
3046 ISL_FL_SET(flags
, ISL_MAP_DISJOINT
);
3048 result
= isl_map_alloc_space(isl_space_copy(map1
->dim
),
3049 map1
->n
* map2
->n
, flags
);
3052 for (i
= 0; i
< map1
->n
; ++i
)
3053 for (j
= 0; j
< map2
->n
; ++j
) {
3054 struct isl_basic_map
*part
;
3055 part
= isl_basic_map_intersect(
3056 isl_basic_map_copy(map1
->p
[i
]),
3057 isl_basic_map_copy(map2
->p
[j
]));
3058 if (isl_basic_map_is_empty(part
) < 0)
3059 part
= isl_basic_map_free(part
);
3060 result
= isl_map_add_basic_map(result
, part
);
3073 static __isl_give isl_map
*map_intersect(__isl_take isl_map
*map1
,
3074 __isl_take isl_map
*map2
)
3078 if (!isl_space_is_equal(map1
->dim
, map2
->dim
))
3079 isl_die(isl_map_get_ctx(map1
), isl_error_invalid
,
3080 "spaces don't match", goto error
);
3081 return map_intersect_internal(map1
, map2
);
3088 __isl_give isl_map
*isl_map_intersect(__isl_take isl_map
*map1
,
3089 __isl_take isl_map
*map2
)
3091 return isl_map_align_params_map_map_and(map1
, map2
, &map_intersect
);
3094 struct isl_set
*isl_set_intersect(struct isl_set
*set1
, struct isl_set
*set2
)
3096 return (struct isl_set
*)
3097 isl_map_intersect((struct isl_map
*)set1
,
3098 (struct isl_map
*)set2
);
3101 /* map_intersect_internal accepts intersections
3102 * with parameter domains, so we can just call that function.
3104 static __isl_give isl_map
*map_intersect_params(__isl_take isl_map
*map
,
3105 __isl_take isl_set
*params
)
3107 return map_intersect_internal(map
, params
);
3110 __isl_give isl_map
*isl_map_intersect_params(__isl_take isl_map
*map1
,
3111 __isl_take isl_map
*map2
)
3113 return isl_map_align_params_map_map_and(map1
, map2
, &map_intersect_params
);
3116 __isl_give isl_set
*isl_set_intersect_params(__isl_take isl_set
*set
,
3117 __isl_take isl_set
*params
)
3119 return isl_map_intersect_params(set
, params
);
3122 struct isl_basic_map
*isl_basic_map_reverse(struct isl_basic_map
*bmap
)
3125 unsigned pos
, n1
, n2
;
3129 bmap
= isl_basic_map_cow(bmap
);
3132 space
= isl_space_reverse(isl_space_copy(bmap
->dim
));
3133 pos
= isl_basic_map_offset(bmap
, isl_dim_in
);
3134 n1
= isl_basic_map_dim(bmap
, isl_dim_in
);
3135 n2
= isl_basic_map_dim(bmap
, isl_dim_out
);
3136 bmap
= isl_basic_map_swap_vars(bmap
, pos
, n1
, n2
);
3137 return isl_basic_map_reset_space(bmap
, space
);
3140 static __isl_give isl_basic_map
*basic_map_space_reset(
3141 __isl_take isl_basic_map
*bmap
, enum isl_dim_type type
)
3147 if (!isl_space_is_named_or_nested(bmap
->dim
, type
))
3150 space
= isl_basic_map_get_space(bmap
);
3151 space
= isl_space_reset(space
, type
);
3152 bmap
= isl_basic_map_reset_space(bmap
, space
);
3156 __isl_give isl_basic_map
*isl_basic_map_insert_dims(
3157 __isl_take isl_basic_map
*bmap
, enum isl_dim_type type
,
3158 unsigned pos
, unsigned n
)
3161 struct isl_basic_map
*res
;
3162 struct isl_dim_map
*dim_map
;
3163 unsigned total
, off
;
3164 enum isl_dim_type t
;
3167 return basic_map_space_reset(bmap
, type
);
3172 res_dim
= isl_space_insert_dims(isl_basic_map_get_space(bmap
), type
, pos
, n
);
3174 total
= isl_basic_map_total_dim(bmap
) + n
;
3175 dim_map
= isl_dim_map_alloc(bmap
->ctx
, total
);
3177 for (t
= isl_dim_param
; t
<= isl_dim_out
; ++t
) {
3179 isl_dim_map_dim(dim_map
, bmap
->dim
, t
, off
);
3181 unsigned size
= isl_basic_map_dim(bmap
, t
);
3182 isl_dim_map_dim_range(dim_map
, bmap
->dim
, t
,
3184 isl_dim_map_dim_range(dim_map
, bmap
->dim
, t
,
3185 pos
, size
- pos
, off
+ pos
+ n
);
3187 off
+= isl_space_dim(res_dim
, t
);
3189 isl_dim_map_div(dim_map
, bmap
, off
);
3191 res
= isl_basic_map_alloc_space(res_dim
,
3192 bmap
->n_div
, bmap
->n_eq
, bmap
->n_ineq
);
3193 if (isl_basic_map_is_rational(bmap
))
3194 res
= isl_basic_map_set_rational(res
);
3195 if (isl_basic_map_plain_is_empty(bmap
)) {
3196 isl_basic_map_free(bmap
);
3198 return isl_basic_map_set_to_empty(res
);
3200 res
= isl_basic_map_add_constraints_dim_map(res
, bmap
, dim_map
);
3201 return isl_basic_map_finalize(res
);
3204 __isl_give isl_basic_set
*isl_basic_set_insert_dims(
3205 __isl_take isl_basic_set
*bset
,
3206 enum isl_dim_type type
, unsigned pos
, unsigned n
)
3208 return isl_basic_map_insert_dims(bset
, type
, pos
, n
);
3211 __isl_give isl_basic_map
*isl_basic_map_add_dims(__isl_take isl_basic_map
*bmap
,
3212 enum isl_dim_type type
, unsigned n
)
3216 return isl_basic_map_insert_dims(bmap
, type
,
3217 isl_basic_map_dim(bmap
, type
), n
);
3220 __isl_give isl_basic_set
*isl_basic_set_add_dims(__isl_take isl_basic_set
*bset
,
3221 enum isl_dim_type type
, unsigned n
)
3225 isl_assert(bset
->ctx
, type
!= isl_dim_in
, goto error
);
3226 return isl_basic_map_add_dims(bset
, type
, n
);
3228 isl_basic_set_free(bset
);
3232 static __isl_give isl_map
*map_space_reset(__isl_take isl_map
*map
,
3233 enum isl_dim_type type
)
3237 if (!map
|| !isl_space_is_named_or_nested(map
->dim
, type
))
3240 space
= isl_map_get_space(map
);
3241 space
= isl_space_reset(space
, type
);
3242 map
= isl_map_reset_space(map
, space
);
3246 __isl_give isl_map
*isl_map_insert_dims(__isl_take isl_map
*map
,
3247 enum isl_dim_type type
, unsigned pos
, unsigned n
)
3252 return map_space_reset(map
, type
);
3254 map
= isl_map_cow(map
);
3258 map
->dim
= isl_space_insert_dims(map
->dim
, type
, pos
, n
);
3262 for (i
= 0; i
< map
->n
; ++i
) {
3263 map
->p
[i
] = isl_basic_map_insert_dims(map
->p
[i
], type
, pos
, n
);
3274 __isl_give isl_set
*isl_set_insert_dims(__isl_take isl_set
*set
,
3275 enum isl_dim_type type
, unsigned pos
, unsigned n
)
3277 return isl_map_insert_dims(set
, type
, pos
, n
);
3280 __isl_give isl_map
*isl_map_add_dims(__isl_take isl_map
*map
,
3281 enum isl_dim_type type
, unsigned n
)
3285 return isl_map_insert_dims(map
, type
, isl_map_dim(map
, type
), n
);
3288 __isl_give isl_set
*isl_set_add_dims(__isl_take isl_set
*set
,
3289 enum isl_dim_type type
, unsigned n
)
3293 isl_assert(set
->ctx
, type
!= isl_dim_in
, goto error
);
3294 return (isl_set
*)isl_map_add_dims((isl_map
*)set
, type
, n
);
3300 __isl_give isl_basic_map
*isl_basic_map_move_dims(
3301 __isl_take isl_basic_map
*bmap
,
3302 enum isl_dim_type dst_type
, unsigned dst_pos
,
3303 enum isl_dim_type src_type
, unsigned src_pos
, unsigned n
)
3305 struct isl_dim_map
*dim_map
;
3306 struct isl_basic_map
*res
;
3307 enum isl_dim_type t
;
3308 unsigned total
, off
;
3315 isl_assert(bmap
->ctx
, src_pos
+ n
<= isl_basic_map_dim(bmap
, src_type
),
3318 if (dst_type
== src_type
&& dst_pos
== src_pos
)
3321 isl_assert(bmap
->ctx
, dst_type
!= src_type
, goto error
);
3323 if (pos(bmap
->dim
, dst_type
) + dst_pos
==
3324 pos(bmap
->dim
, src_type
) + src_pos
+
3325 ((src_type
< dst_type
) ? n
: 0)) {
3326 bmap
= isl_basic_map_cow(bmap
);
3330 bmap
->dim
= isl_space_move_dims(bmap
->dim
, dst_type
, dst_pos
,
3331 src_type
, src_pos
, n
);
3335 bmap
= isl_basic_map_finalize(bmap
);
3340 total
= isl_basic_map_total_dim(bmap
);
3341 dim_map
= isl_dim_map_alloc(bmap
->ctx
, total
);
3344 for (t
= isl_dim_param
; t
<= isl_dim_out
; ++t
) {
3345 unsigned size
= isl_space_dim(bmap
->dim
, t
);
3346 if (t
== dst_type
) {
3347 isl_dim_map_dim_range(dim_map
, bmap
->dim
, t
,
3350 isl_dim_map_dim_range(dim_map
, bmap
->dim
, src_type
,
3353 isl_dim_map_dim_range(dim_map
, bmap
->dim
, t
,
3354 dst_pos
, size
- dst_pos
, off
);
3355 off
+= size
- dst_pos
;
3356 } else if (t
== src_type
) {
3357 isl_dim_map_dim_range(dim_map
, bmap
->dim
, t
,
3360 isl_dim_map_dim_range(dim_map
, bmap
->dim
, t
,
3361 src_pos
+ n
, size
- src_pos
- n
, off
);
3362 off
+= size
- src_pos
- n
;
3364 isl_dim_map_dim(dim_map
, bmap
->dim
, t
, off
);
3368 isl_dim_map_div(dim_map
, bmap
, off
);
3370 res
= isl_basic_map_alloc_space(isl_basic_map_get_space(bmap
),
3371 bmap
->n_div
, bmap
->n_eq
, bmap
->n_ineq
);
3372 bmap
= isl_basic_map_add_constraints_dim_map(res
, bmap
, dim_map
);
3376 bmap
->dim
= isl_space_move_dims(bmap
->dim
, dst_type
, dst_pos
,
3377 src_type
, src_pos
, n
);
3381 ISL_F_CLR(bmap
, ISL_BASIC_MAP_NORMALIZED
);
3382 bmap
= isl_basic_map_gauss(bmap
, NULL
);
3383 bmap
= isl_basic_map_finalize(bmap
);
3387 isl_basic_map_free(bmap
);
3391 __isl_give isl_basic_set
*isl_basic_set_move_dims(__isl_take isl_basic_set
*bset
,
3392 enum isl_dim_type dst_type
, unsigned dst_pos
,
3393 enum isl_dim_type src_type
, unsigned src_pos
, unsigned n
)
3395 return (isl_basic_set
*)isl_basic_map_move_dims(
3396 (isl_basic_map
*)bset
, dst_type
, dst_pos
, src_type
, src_pos
, n
);
3399 __isl_give isl_set
*isl_set_move_dims(__isl_take isl_set
*set
,
3400 enum isl_dim_type dst_type
, unsigned dst_pos
,
3401 enum isl_dim_type src_type
, unsigned src_pos
, unsigned n
)
3405 isl_assert(set
->ctx
, dst_type
!= isl_dim_in
, goto error
);
3406 return (isl_set
*)isl_map_move_dims((isl_map
*)set
, dst_type
, dst_pos
,
3407 src_type
, src_pos
, n
);
3413 __isl_give isl_map
*isl_map_move_dims(__isl_take isl_map
*map
,
3414 enum isl_dim_type dst_type
, unsigned dst_pos
,
3415 enum isl_dim_type src_type
, unsigned src_pos
, unsigned n
)
3424 isl_assert(map
->ctx
, src_pos
+ n
<= isl_map_dim(map
, src_type
),
3427 if (dst_type
== src_type
&& dst_pos
== src_pos
)
3430 isl_assert(map
->ctx
, dst_type
!= src_type
, goto error
);
3432 map
= isl_map_cow(map
);
3436 map
->dim
= isl_space_move_dims(map
->dim
, dst_type
, dst_pos
, src_type
, src_pos
, n
);
3440 for (i
= 0; i
< map
->n
; ++i
) {
3441 map
->p
[i
] = isl_basic_map_move_dims(map
->p
[i
],
3443 src_type
, src_pos
, n
);
3454 /* Move the specified dimensions to the last columns right before
3455 * the divs. Don't change the dimension specification of bmap.
3456 * That's the responsibility of the caller.
3458 static __isl_give isl_basic_map
*move_last(__isl_take isl_basic_map
*bmap
,
3459 enum isl_dim_type type
, unsigned first
, unsigned n
)
3461 struct isl_dim_map
*dim_map
;
3462 struct isl_basic_map
*res
;
3463 enum isl_dim_type t
;
3464 unsigned total
, off
;
3468 if (pos(bmap
->dim
, type
) + first
+ n
==
3469 1 + isl_space_dim(bmap
->dim
, isl_dim_all
))
3472 total
= isl_basic_map_total_dim(bmap
);
3473 dim_map
= isl_dim_map_alloc(bmap
->ctx
, total
);
3476 for (t
= isl_dim_param
; t
<= isl_dim_out
; ++t
) {
3477 unsigned size
= isl_space_dim(bmap
->dim
, t
);
3479 isl_dim_map_dim_range(dim_map
, bmap
->dim
, t
,
3482 isl_dim_map_dim_range(dim_map
, bmap
->dim
, t
,
3483 first
, n
, total
- bmap
->n_div
- n
);
3484 isl_dim_map_dim_range(dim_map
, bmap
->dim
, t
,
3485 first
+ n
, size
- (first
+ n
), off
);
3486 off
+= size
- (first
+ n
);
3488 isl_dim_map_dim(dim_map
, bmap
->dim
, t
, off
);
3492 isl_dim_map_div(dim_map
, bmap
, off
+ n
);
3494 res
= isl_basic_map_alloc_space(isl_basic_map_get_space(bmap
),
3495 bmap
->n_div
, bmap
->n_eq
, bmap
->n_ineq
);
3496 res
= isl_basic_map_add_constraints_dim_map(res
, bmap
, dim_map
);
3500 /* Insert "n" rows in the divs of "bmap".
3502 * The number of columns is not changed, which means that the last
3503 * dimensions of "bmap" are being reintepreted as the new divs.
3504 * The space of "bmap" is not adjusted, however, which means
3505 * that "bmap" is left in an inconsistent state. Removing "n" dimensions
3506 * from the space of "bmap" is the responsibility of the caller.
3508 static __isl_give isl_basic_map
*insert_div_rows(__isl_take isl_basic_map
*bmap
,
3516 bmap
= isl_basic_map_cow(bmap
);
3520 row_size
= 1 + isl_space_dim(bmap
->dim
, isl_dim_all
) + bmap
->extra
;
3521 old
= bmap
->block2
.data
;
3522 bmap
->block2
= isl_blk_extend(bmap
->ctx
, bmap
->block2
,
3523 (bmap
->extra
+ n
) * (1 + row_size
));
3524 if (!bmap
->block2
.data
)
3525 return isl_basic_map_free(bmap
);
3526 new_div
= isl_alloc_array(bmap
->ctx
, isl_int
*, bmap
->extra
+ n
);
3528 return isl_basic_map_free(bmap
);
3529 for (i
= 0; i
< n
; ++i
) {
3530 new_div
[i
] = bmap
->block2
.data
+
3531 (bmap
->extra
+ i
) * (1 + row_size
);
3532 isl_seq_clr(new_div
[i
], 1 + row_size
);
3534 for (i
= 0; i
< bmap
->extra
; ++i
)
3535 new_div
[n
+ i
] = bmap
->block2
.data
+ (bmap
->div
[i
] - old
);
3537 bmap
->div
= new_div
;
3544 /* Drop constraints from "bmap" that only involve the variables
3545 * of "type" in the range [first, first + n] that are not related
3546 * to any of the variables outside that interval.
3547 * These constraints cannot influence the values for the variables
3548 * outside the interval, except in case they cause "bmap" to be empty.
3549 * Only drop the constraints if "bmap" is known to be non-empty.
3551 static __isl_give isl_basic_map
*drop_irrelevant_constraints(
3552 __isl_take isl_basic_map
*bmap
, enum isl_dim_type type
,
3553 unsigned first
, unsigned n
)
3557 unsigned dim
, n_div
;
3560 non_empty
= isl_basic_map_plain_is_non_empty(bmap
);
3562 return isl_basic_map_free(bmap
);
3566 dim
= isl_basic_map_dim(bmap
, isl_dim_all
);
3567 n_div
= isl_basic_map_dim(bmap
, isl_dim_div
);
3568 groups
= isl_calloc_array(isl_basic_map_get_ctx(bmap
), int, dim
);
3570 return isl_basic_map_free(bmap
);
3571 first
+= isl_basic_map_offset(bmap
, type
) - 1;
3572 for (i
= 0; i
< first
; ++i
)
3574 for (i
= first
+ n
; i
< dim
- n_div
; ++i
)
3577 bmap
= isl_basic_map_drop_unrelated_constraints(bmap
, groups
);
3582 /* Turn the n dimensions of type type, starting at first
3583 * into existentially quantified variables.
3585 * If a subset of the projected out variables are unrelated
3586 * to any of the variables that remain, then the constraints
3587 * involving this subset are simply dropped first.
3589 __isl_give isl_basic_map
*isl_basic_map_project_out(
3590 __isl_take isl_basic_map
*bmap
,
3591 enum isl_dim_type type
, unsigned first
, unsigned n
)
3594 return basic_map_space_reset(bmap
, type
);
3595 if (type
== isl_dim_div
)
3596 isl_die(isl_basic_map_get_ctx(bmap
), isl_error_invalid
,
3597 "cannot project out existentially quantified variables",
3598 return isl_basic_map_free(bmap
));
3600 bmap
= drop_irrelevant_constraints(bmap
, type
, first
, n
);
3604 if (ISL_F_ISSET(bmap
, ISL_BASIC_MAP_RATIONAL
))
3605 return isl_basic_map_remove_dims(bmap
, type
, first
, n
);
3607 isl_assert(bmap
->ctx
, first
+ n
<= isl_basic_map_dim(bmap
, type
),
3610 bmap
= move_last(bmap
, type
, first
, n
);
3611 bmap
= isl_basic_map_cow(bmap
);
3612 bmap
= insert_div_rows(bmap
, n
);
3616 bmap
->dim
= isl_space_drop_dims(bmap
->dim
, type
, first
, n
);
3619 bmap
= isl_basic_map_simplify(bmap
);
3620 bmap
= isl_basic_map_drop_redundant_divs(bmap
);
3621 return isl_basic_map_finalize(bmap
);
3623 isl_basic_map_free(bmap
);
3627 /* Turn the n dimensions of type type, starting at first
3628 * into existentially quantified variables.
3630 struct isl_basic_set
*isl_basic_set_project_out(struct isl_basic_set
*bset
,
3631 enum isl_dim_type type
, unsigned first
, unsigned n
)
3633 return (isl_basic_set
*)isl_basic_map_project_out(
3634 (isl_basic_map
*)bset
, type
, first
, n
);
3637 /* Turn the n dimensions of type type, starting at first
3638 * into existentially quantified variables.
3640 __isl_give isl_map
*isl_map_project_out(__isl_take isl_map
*map
,
3641 enum isl_dim_type type
, unsigned first
, unsigned n
)
3649 return map_space_reset(map
, type
);
3651 isl_assert(map
->ctx
, first
+ n
<= isl_map_dim(map
, type
), goto error
);
3653 map
= isl_map_cow(map
);
3657 map
->dim
= isl_space_drop_dims(map
->dim
, type
, first
, n
);
3661 for (i
= 0; i
< map
->n
; ++i
) {
3662 map
->p
[i
] = isl_basic_map_project_out(map
->p
[i
], type
, first
, n
);
3673 /* Turn the n dimensions of type type, starting at first
3674 * into existentially quantified variables.
3676 __isl_give isl_set
*isl_set_project_out(__isl_take isl_set
*set
,
3677 enum isl_dim_type type
, unsigned first
, unsigned n
)
3679 return (isl_set
*)isl_map_project_out((isl_map
*)set
, type
, first
, n
);
3682 /* Return a map that projects the elements in "set" onto their
3683 * "n" set dimensions starting at "first".
3684 * "type" should be equal to isl_dim_set.
3686 __isl_give isl_map
*isl_set_project_onto_map(__isl_take isl_set
*set
,
3687 enum isl_dim_type type
, unsigned first
, unsigned n
)
3695 if (type
!= isl_dim_set
)
3696 isl_die(isl_set_get_ctx(set
), isl_error_invalid
,
3697 "only set dimensions can be projected out", goto error
);
3698 dim
= isl_set_dim(set
, isl_dim_set
);
3699 if (first
+ n
> dim
|| first
+ n
< first
)
3700 isl_die(isl_set_get_ctx(set
), isl_error_invalid
,
3701 "index out of bounds", goto error
);
3703 map
= isl_map_from_domain(set
);
3704 map
= isl_map_add_dims(map
, isl_dim_out
, n
);
3705 for (i
= 0; i
< n
; ++i
)
3706 map
= isl_map_equate(map
, isl_dim_in
, first
+ i
,
3714 static struct isl_basic_map
*add_divs(struct isl_basic_map
*bmap
, unsigned n
)
3718 for (i
= 0; i
< n
; ++i
) {
3719 j
= isl_basic_map_alloc_div(bmap
);
3722 isl_seq_clr(bmap
->div
[j
], 1+1+isl_basic_map_total_dim(bmap
));
3726 isl_basic_map_free(bmap
);
3730 struct isl_basic_map
*isl_basic_map_apply_range(
3731 struct isl_basic_map
*bmap1
, struct isl_basic_map
*bmap2
)
3733 isl_space
*dim_result
= NULL
;
3734 struct isl_basic_map
*bmap
;
3735 unsigned n_in
, n_out
, n
, nparam
, total
, pos
;
3736 struct isl_dim_map
*dim_map1
, *dim_map2
;
3738 if (!bmap1
|| !bmap2
)
3740 if (!isl_space_match(bmap1
->dim
, isl_dim_param
,
3741 bmap2
->dim
, isl_dim_param
))
3742 isl_die(isl_basic_map_get_ctx(bmap1
), isl_error_invalid
,
3743 "parameters don't match", goto error
);
3744 if (!isl_space_tuple_is_equal(bmap1
->dim
, isl_dim_out
,
3745 bmap2
->dim
, isl_dim_in
))
3746 isl_die(isl_basic_map_get_ctx(bmap1
), isl_error_invalid
,
3747 "spaces don't match", goto error
);
3749 dim_result
= isl_space_join(isl_space_copy(bmap1
->dim
),
3750 isl_space_copy(bmap2
->dim
));
3752 n_in
= isl_basic_map_n_in(bmap1
);
3753 n_out
= isl_basic_map_n_out(bmap2
);
3754 n
= isl_basic_map_n_out(bmap1
);
3755 nparam
= isl_basic_map_n_param(bmap1
);
3757 total
= nparam
+ n_in
+ n_out
+ bmap1
->n_div
+ bmap2
->n_div
+ n
;
3758 dim_map1
= isl_dim_map_alloc(bmap1
->ctx
, total
);
3759 dim_map2
= isl_dim_map_alloc(bmap1
->ctx
, total
);
3760 isl_dim_map_dim(dim_map1
, bmap1
->dim
, isl_dim_param
, pos
= 0);
3761 isl_dim_map_dim(dim_map2
, bmap2
->dim
, isl_dim_param
, pos
= 0);
3762 isl_dim_map_dim(dim_map1
, bmap1
->dim
, isl_dim_in
, pos
+= nparam
);
3763 isl_dim_map_dim(dim_map2
, bmap2
->dim
, isl_dim_out
, pos
+= n_in
);
3764 isl_dim_map_div(dim_map1
, bmap1
, pos
+= n_out
);
3765 isl_dim_map_div(dim_map2
, bmap2
, pos
+= bmap1
->n_div
);
3766 isl_dim_map_dim(dim_map1
, bmap1
->dim
, isl_dim_out
, pos
+= bmap2
->n_div
);
3767 isl_dim_map_dim(dim_map2
, bmap2
->dim
, isl_dim_in
, pos
);
3769 bmap
= isl_basic_map_alloc_space(dim_result
,
3770 bmap1
->n_div
+ bmap2
->n_div
+ n
,
3771 bmap1
->n_eq
+ bmap2
->n_eq
,
3772 bmap1
->n_ineq
+ bmap2
->n_ineq
);
3773 bmap
= isl_basic_map_add_constraints_dim_map(bmap
, bmap1
, dim_map1
);
3774 bmap
= isl_basic_map_add_constraints_dim_map(bmap
, bmap2
, dim_map2
);
3775 bmap
= add_divs(bmap
, n
);
3776 bmap
= isl_basic_map_simplify(bmap
);
3777 bmap
= isl_basic_map_drop_redundant_divs(bmap
);
3778 return isl_basic_map_finalize(bmap
);
3780 isl_basic_map_free(bmap1
);
3781 isl_basic_map_free(bmap2
);
3785 struct isl_basic_set
*isl_basic_set_apply(
3786 struct isl_basic_set
*bset
, struct isl_basic_map
*bmap
)
3791 isl_assert(bset
->ctx
, isl_basic_map_compatible_domain(bmap
, bset
),
3794 return (struct isl_basic_set
*)
3795 isl_basic_map_apply_range((struct isl_basic_map
*)bset
, bmap
);
3797 isl_basic_set_free(bset
);
3798 isl_basic_map_free(bmap
);
3802 struct isl_basic_map
*isl_basic_map_apply_domain(
3803 struct isl_basic_map
*bmap1
, struct isl_basic_map
*bmap2
)
3805 if (!bmap1
|| !bmap2
)
3808 isl_assert(bmap1
->ctx
,
3809 isl_basic_map_n_in(bmap1
) == isl_basic_map_n_in(bmap2
), goto error
);
3810 isl_assert(bmap1
->ctx
,
3811 isl_basic_map_n_param(bmap1
) == isl_basic_map_n_param(bmap2
),
3814 bmap1
= isl_basic_map_reverse(bmap1
);
3815 bmap1
= isl_basic_map_apply_range(bmap1
, bmap2
);
3816 return isl_basic_map_reverse(bmap1
);
3818 isl_basic_map_free(bmap1
);
3819 isl_basic_map_free(bmap2
);
3823 /* Given two basic maps A -> f(A) and B -> g(B), construct a basic map
3824 * A \cap B -> f(A) + f(B)
3826 struct isl_basic_map
*isl_basic_map_sum(
3827 struct isl_basic_map
*bmap1
, struct isl_basic_map
*bmap2
)
3829 unsigned n_in
, n_out
, nparam
, total
, pos
;
3830 struct isl_basic_map
*bmap
= NULL
;
3831 struct isl_dim_map
*dim_map1
, *dim_map2
;
3834 if (!bmap1
|| !bmap2
)
3837 isl_assert(bmap1
->ctx
, isl_space_is_equal(bmap1
->dim
, bmap2
->dim
),
3840 nparam
= isl_basic_map_n_param(bmap1
);
3841 n_in
= isl_basic_map_n_in(bmap1
);
3842 n_out
= isl_basic_map_n_out(bmap1
);
3844 total
= nparam
+ n_in
+ n_out
+ bmap1
->n_div
+ bmap2
->n_div
+ 2 * n_out
;
3845 dim_map1
= isl_dim_map_alloc(bmap1
->ctx
, total
);
3846 dim_map2
= isl_dim_map_alloc(bmap2
->ctx
, total
);
3847 isl_dim_map_dim(dim_map1
, bmap1
->dim
, isl_dim_param
, pos
= 0);
3848 isl_dim_map_dim(dim_map2
, bmap2
->dim
, isl_dim_param
, pos
);
3849 isl_dim_map_dim(dim_map1
, bmap1
->dim
, isl_dim_in
, pos
+= nparam
);
3850 isl_dim_map_dim(dim_map2
, bmap2
->dim
, isl_dim_in
, pos
);
3851 isl_dim_map_div(dim_map1
, bmap1
, pos
+= n_in
+ n_out
);
3852 isl_dim_map_div(dim_map2
, bmap2
, pos
+= bmap1
->n_div
);
3853 isl_dim_map_dim(dim_map1
, bmap1
->dim
, isl_dim_out
, pos
+= bmap2
->n_div
);
3854 isl_dim_map_dim(dim_map2
, bmap2
->dim
, isl_dim_out
, pos
+= n_out
);
3856 bmap
= isl_basic_map_alloc_space(isl_space_copy(bmap1
->dim
),
3857 bmap1
->n_div
+ bmap2
->n_div
+ 2 * n_out
,
3858 bmap1
->n_eq
+ bmap2
->n_eq
+ n_out
,
3859 bmap1
->n_ineq
+ bmap2
->n_ineq
);
3860 for (i
= 0; i
< n_out
; ++i
) {
3861 int j
= isl_basic_map_alloc_equality(bmap
);
3864 isl_seq_clr(bmap
->eq
[j
], 1+total
);
3865 isl_int_set_si(bmap
->eq
[j
][1+nparam
+n_in
+i
], -1);
3866 isl_int_set_si(bmap
->eq
[j
][1+pos
+i
], 1);
3867 isl_int_set_si(bmap
->eq
[j
][1+pos
-n_out
+i
], 1);
3869 bmap
= isl_basic_map_add_constraints_dim_map(bmap
, bmap1
, dim_map1
);
3870 bmap
= isl_basic_map_add_constraints_dim_map(bmap
, bmap2
, dim_map2
);
3871 bmap
= add_divs(bmap
, 2 * n_out
);
3873 bmap
= isl_basic_map_simplify(bmap
);
3874 return isl_basic_map_finalize(bmap
);
3876 isl_basic_map_free(bmap
);
3877 isl_basic_map_free(bmap1
);
3878 isl_basic_map_free(bmap2
);
3882 /* Given two maps A -> f(A) and B -> g(B), construct a map
3883 * A \cap B -> f(A) + f(B)
3885 struct isl_map
*isl_map_sum(struct isl_map
*map1
, struct isl_map
*map2
)
3887 struct isl_map
*result
;
3893 isl_assert(map1
->ctx
, isl_space_is_equal(map1
->dim
, map2
->dim
), goto error
);
3895 result
= isl_map_alloc_space(isl_space_copy(map1
->dim
),
3896 map1
->n
* map2
->n
, 0);
3899 for (i
= 0; i
< map1
->n
; ++i
)
3900 for (j
= 0; j
< map2
->n
; ++j
) {
3901 struct isl_basic_map
*part
;
3902 part
= isl_basic_map_sum(
3903 isl_basic_map_copy(map1
->p
[i
]),
3904 isl_basic_map_copy(map2
->p
[j
]));
3905 if (isl_basic_map_is_empty(part
))
3906 isl_basic_map_free(part
);
3908 result
= isl_map_add_basic_map(result
, part
);
3921 __isl_give isl_set
*isl_set_sum(__isl_take isl_set
*set1
,
3922 __isl_take isl_set
*set2
)
3924 return (isl_set
*)isl_map_sum((isl_map
*)set1
, (isl_map
*)set2
);
3927 /* Given a basic map A -> f(A), construct A -> -f(A).
3929 struct isl_basic_map
*isl_basic_map_neg(struct isl_basic_map
*bmap
)
3934 bmap
= isl_basic_map_cow(bmap
);
3938 n
= isl_basic_map_dim(bmap
, isl_dim_out
);
3939 off
= isl_basic_map_offset(bmap
, isl_dim_out
);
3940 for (i
= 0; i
< bmap
->n_eq
; ++i
)
3941 for (j
= 0; j
< n
; ++j
)
3942 isl_int_neg(bmap
->eq
[i
][off
+j
], bmap
->eq
[i
][off
+j
]);
3943 for (i
= 0; i
< bmap
->n_ineq
; ++i
)
3944 for (j
= 0; j
< n
; ++j
)
3945 isl_int_neg(bmap
->ineq
[i
][off
+j
], bmap
->ineq
[i
][off
+j
]);
3946 for (i
= 0; i
< bmap
->n_div
; ++i
)
3947 for (j
= 0; j
< n
; ++j
)
3948 isl_int_neg(bmap
->div
[i
][1+off
+j
], bmap
->div
[i
][1+off
+j
]);
3949 bmap
= isl_basic_map_gauss(bmap
, NULL
);
3950 return isl_basic_map_finalize(bmap
);
3953 __isl_give isl_basic_set
*isl_basic_set_neg(__isl_take isl_basic_set
*bset
)
3955 return isl_basic_map_neg(bset
);
3958 /* Given a map A -> f(A), construct A -> -f(A).
3960 struct isl_map
*isl_map_neg(struct isl_map
*map
)
3964 map
= isl_map_cow(map
);
3968 for (i
= 0; i
< map
->n
; ++i
) {
3969 map
->p
[i
] = isl_basic_map_neg(map
->p
[i
]);
3980 __isl_give isl_set
*isl_set_neg(__isl_take isl_set
*set
)
3982 return (isl_set
*)isl_map_neg((isl_map
*)set
);
3985 /* Given a basic map A -> f(A) and an integer d, construct a basic map
3986 * A -> floor(f(A)/d).
3988 struct isl_basic_map
*isl_basic_map_floordiv(struct isl_basic_map
*bmap
,
3991 unsigned n_in
, n_out
, nparam
, total
, pos
;
3992 struct isl_basic_map
*result
= NULL
;
3993 struct isl_dim_map
*dim_map
;
3999 nparam
= isl_basic_map_n_param(bmap
);
4000 n_in
= isl_basic_map_n_in(bmap
);
4001 n_out
= isl_basic_map_n_out(bmap
);
4003 total
= nparam
+ n_in
+ n_out
+ bmap
->n_div
+ n_out
;
4004 dim_map
= isl_dim_map_alloc(bmap
->ctx
, total
);
4005 isl_dim_map_dim(dim_map
, bmap
->dim
, isl_dim_param
, pos
= 0);
4006 isl_dim_map_dim(dim_map
, bmap
->dim
, isl_dim_in
, pos
+= nparam
);
4007 isl_dim_map_div(dim_map
, bmap
, pos
+= n_in
+ n_out
);
4008 isl_dim_map_dim(dim_map
, bmap
->dim
, isl_dim_out
, pos
+= bmap
->n_div
);
4010 result
= isl_basic_map_alloc_space(isl_space_copy(bmap
->dim
),
4011 bmap
->n_div
+ n_out
,
4012 bmap
->n_eq
, bmap
->n_ineq
+ 2 * n_out
);
4013 result
= isl_basic_map_add_constraints_dim_map(result
, bmap
, dim_map
);
4014 result
= add_divs(result
, n_out
);
4015 for (i
= 0; i
< n_out
; ++i
) {
4017 j
= isl_basic_map_alloc_inequality(result
);
4020 isl_seq_clr(result
->ineq
[j
], 1+total
);
4021 isl_int_neg(result
->ineq
[j
][1+nparam
+n_in
+i
], d
);
4022 isl_int_set_si(result
->ineq
[j
][1+pos
+i
], 1);
4023 j
= isl_basic_map_alloc_inequality(result
);
4026 isl_seq_clr(result
->ineq
[j
], 1+total
);
4027 isl_int_set(result
->ineq
[j
][1+nparam
+n_in
+i
], d
);
4028 isl_int_set_si(result
->ineq
[j
][1+pos
+i
], -1);
4029 isl_int_sub_ui(result
->ineq
[j
][0], d
, 1);
4032 result
= isl_basic_map_simplify(result
);
4033 return isl_basic_map_finalize(result
);
4035 isl_basic_map_free(result
);
4039 /* Given a map A -> f(A) and an integer d, construct a map
4040 * A -> floor(f(A)/d).
4042 struct isl_map
*isl_map_floordiv(struct isl_map
*map
, isl_int d
)
4046 map
= isl_map_cow(map
);
4050 ISL_F_CLR(map
, ISL_MAP_DISJOINT
);
4051 ISL_F_CLR(map
, ISL_MAP_NORMALIZED
);
4052 for (i
= 0; i
< map
->n
; ++i
) {
4053 map
->p
[i
] = isl_basic_map_floordiv(map
->p
[i
], d
);
4064 /* Given a map A -> f(A) and an integer d, construct a map
4065 * A -> floor(f(A)/d).
4067 __isl_give isl_map
*isl_map_floordiv_val(__isl_take isl_map
*map
,
4068 __isl_take isl_val
*d
)
4072 if (!isl_val_is_int(d
))
4073 isl_die(isl_val_get_ctx(d
), isl_error_invalid
,
4074 "expecting integer denominator", goto error
);
4075 map
= isl_map_floordiv(map
, d
->n
);
4084 static struct isl_basic_map
*var_equal(struct isl_basic_map
*bmap
, unsigned pos
)
4090 i
= isl_basic_map_alloc_equality(bmap
);
4093 nparam
= isl_basic_map_n_param(bmap
);
4094 n_in
= isl_basic_map_n_in(bmap
);
4095 isl_seq_clr(bmap
->eq
[i
], 1 + isl_basic_map_total_dim(bmap
));
4096 isl_int_set_si(bmap
->eq
[i
][1+nparam
+pos
], -1);
4097 isl_int_set_si(bmap
->eq
[i
][1+nparam
+n_in
+pos
], 1);
4098 return isl_basic_map_finalize(bmap
);
4100 isl_basic_map_free(bmap
);
4104 /* Add a constraints to "bmap" expressing i_pos < o_pos
4106 static struct isl_basic_map
*var_less(struct isl_basic_map
*bmap
, unsigned pos
)
4112 i
= isl_basic_map_alloc_inequality(bmap
);
4115 nparam
= isl_basic_map_n_param(bmap
);
4116 n_in
= isl_basic_map_n_in(bmap
);
4117 isl_seq_clr(bmap
->ineq
[i
], 1 + isl_basic_map_total_dim(bmap
));
4118 isl_int_set_si(bmap
->ineq
[i
][0], -1);
4119 isl_int_set_si(bmap
->ineq
[i
][1+nparam
+pos
], -1);
4120 isl_int_set_si(bmap
->ineq
[i
][1+nparam
+n_in
+pos
], 1);
4121 return isl_basic_map_finalize(bmap
);
4123 isl_basic_map_free(bmap
);
4127 /* Add a constraint to "bmap" expressing i_pos <= o_pos
4129 static __isl_give isl_basic_map
*var_less_or_equal(
4130 __isl_take isl_basic_map
*bmap
, unsigned pos
)
4136 i
= isl_basic_map_alloc_inequality(bmap
);
4139 nparam
= isl_basic_map_n_param(bmap
);
4140 n_in
= isl_basic_map_n_in(bmap
);
4141 isl_seq_clr(bmap
->ineq
[i
], 1 + isl_basic_map_total_dim(bmap
));
4142 isl_int_set_si(bmap
->ineq
[i
][1+nparam
+pos
], -1);
4143 isl_int_set_si(bmap
->ineq
[i
][1+nparam
+n_in
+pos
], 1);
4144 return isl_basic_map_finalize(bmap
);
4146 isl_basic_map_free(bmap
);
4150 /* Add a constraints to "bmap" expressing i_pos > o_pos
4152 static struct isl_basic_map
*var_more(struct isl_basic_map
*bmap
, unsigned pos
)
4158 i
= isl_basic_map_alloc_inequality(bmap
);
4161 nparam
= isl_basic_map_n_param(bmap
);
4162 n_in
= isl_basic_map_n_in(bmap
);
4163 isl_seq_clr(bmap
->ineq
[i
], 1 + isl_basic_map_total_dim(bmap
));
4164 isl_int_set_si(bmap
->ineq
[i
][0], -1);
4165 isl_int_set_si(bmap
->ineq
[i
][1+nparam
+pos
], 1);
4166 isl_int_set_si(bmap
->ineq
[i
][1+nparam
+n_in
+pos
], -1);
4167 return isl_basic_map_finalize(bmap
);
4169 isl_basic_map_free(bmap
);
4173 /* Add a constraint to "bmap" expressing i_pos >= o_pos
4175 static __isl_give isl_basic_map
*var_more_or_equal(
4176 __isl_take isl_basic_map
*bmap
, unsigned pos
)
4182 i
= isl_basic_map_alloc_inequality(bmap
);
4185 nparam
= isl_basic_map_n_param(bmap
);
4186 n_in
= isl_basic_map_n_in(bmap
);
4187 isl_seq_clr(bmap
->ineq
[i
], 1 + isl_basic_map_total_dim(bmap
));
4188 isl_int_set_si(bmap
->ineq
[i
][1+nparam
+pos
], 1);
4189 isl_int_set_si(bmap
->ineq
[i
][1+nparam
+n_in
+pos
], -1);
4190 return isl_basic_map_finalize(bmap
);
4192 isl_basic_map_free(bmap
);
4196 __isl_give isl_basic_map
*isl_basic_map_equal(
4197 __isl_take isl_space
*dim
, unsigned n_equal
)
4200 struct isl_basic_map
*bmap
;
4201 bmap
= isl_basic_map_alloc_space(dim
, 0, n_equal
, 0);
4204 for (i
= 0; i
< n_equal
&& bmap
; ++i
)
4205 bmap
= var_equal(bmap
, i
);
4206 return isl_basic_map_finalize(bmap
);
4209 /* Return a relation on of dimension "dim" expressing i_[0..pos] << o_[0..pos]
4211 __isl_give isl_basic_map
*isl_basic_map_less_at(__isl_take isl_space
*dim
,
4215 struct isl_basic_map
*bmap
;
4216 bmap
= isl_basic_map_alloc_space(dim
, 0, pos
, 1);
4219 for (i
= 0; i
< pos
&& bmap
; ++i
)
4220 bmap
= var_equal(bmap
, i
);
4222 bmap
= var_less(bmap
, pos
);
4223 return isl_basic_map_finalize(bmap
);
4226 /* Return a relation on of dimension "dim" expressing i_[0..pos] <<= o_[0..pos]
4228 __isl_give isl_basic_map
*isl_basic_map_less_or_equal_at(
4229 __isl_take isl_space
*dim
, unsigned pos
)
4232 isl_basic_map
*bmap
;
4234 bmap
= isl_basic_map_alloc_space(dim
, 0, pos
, 1);
4235 for (i
= 0; i
< pos
; ++i
)
4236 bmap
= var_equal(bmap
, i
);
4237 bmap
= var_less_or_equal(bmap
, pos
);
4238 return isl_basic_map_finalize(bmap
);
4241 /* Return a relation on pairs of sets of dimension "dim" expressing i_pos > o_pos
4243 __isl_give isl_basic_map
*isl_basic_map_more_at(__isl_take isl_space
*dim
,
4247 struct isl_basic_map
*bmap
;
4248 bmap
= isl_basic_map_alloc_space(dim
, 0, pos
, 1);
4251 for (i
= 0; i
< pos
&& bmap
; ++i
)
4252 bmap
= var_equal(bmap
, i
);
4254 bmap
= var_more(bmap
, pos
);
4255 return isl_basic_map_finalize(bmap
);
4258 /* Return a relation on of dimension "dim" expressing i_[0..pos] >>= o_[0..pos]
4260 __isl_give isl_basic_map
*isl_basic_map_more_or_equal_at(
4261 __isl_take isl_space
*dim
, unsigned pos
)
4264 isl_basic_map
*bmap
;
4266 bmap
= isl_basic_map_alloc_space(dim
, 0, pos
, 1);
4267 for (i
= 0; i
< pos
; ++i
)
4268 bmap
= var_equal(bmap
, i
);
4269 bmap
= var_more_or_equal(bmap
, pos
);
4270 return isl_basic_map_finalize(bmap
);
4273 static __isl_give isl_map
*map_lex_lte_first(__isl_take isl_space
*dims
,
4274 unsigned n
, int equal
)
4276 struct isl_map
*map
;
4279 if (n
== 0 && equal
)
4280 return isl_map_universe(dims
);
4282 map
= isl_map_alloc_space(isl_space_copy(dims
), n
, ISL_MAP_DISJOINT
);
4284 for (i
= 0; i
+ 1 < n
; ++i
)
4285 map
= isl_map_add_basic_map(map
,
4286 isl_basic_map_less_at(isl_space_copy(dims
), i
));
4289 map
= isl_map_add_basic_map(map
,
4290 isl_basic_map_less_or_equal_at(dims
, n
- 1));
4292 map
= isl_map_add_basic_map(map
,
4293 isl_basic_map_less_at(dims
, n
- 1));
4295 isl_space_free(dims
);
4300 static __isl_give isl_map
*map_lex_lte(__isl_take isl_space
*dims
, int equal
)
4304 return map_lex_lte_first(dims
, dims
->n_out
, equal
);
4307 __isl_give isl_map
*isl_map_lex_lt_first(__isl_take isl_space
*dim
, unsigned n
)
4309 return map_lex_lte_first(dim
, n
, 0);
4312 __isl_give isl_map
*isl_map_lex_le_first(__isl_take isl_space
*dim
, unsigned n
)
4314 return map_lex_lte_first(dim
, n
, 1);
4317 __isl_give isl_map
*isl_map_lex_lt(__isl_take isl_space
*set_dim
)
4319 return map_lex_lte(isl_space_map_from_set(set_dim
), 0);
4322 __isl_give isl_map
*isl_map_lex_le(__isl_take isl_space
*set_dim
)
4324 return map_lex_lte(isl_space_map_from_set(set_dim
), 1);
4327 static __isl_give isl_map
*map_lex_gte_first(__isl_take isl_space
*dims
,
4328 unsigned n
, int equal
)
4330 struct isl_map
*map
;
4333 if (n
== 0 && equal
)
4334 return isl_map_universe(dims
);
4336 map
= isl_map_alloc_space(isl_space_copy(dims
), n
, ISL_MAP_DISJOINT
);
4338 for (i
= 0; i
+ 1 < n
; ++i
)
4339 map
= isl_map_add_basic_map(map
,
4340 isl_basic_map_more_at(isl_space_copy(dims
), i
));
4343 map
= isl_map_add_basic_map(map
,
4344 isl_basic_map_more_or_equal_at(dims
, n
- 1));
4346 map
= isl_map_add_basic_map(map
,
4347 isl_basic_map_more_at(dims
, n
- 1));
4349 isl_space_free(dims
);
4354 static __isl_give isl_map
*map_lex_gte(__isl_take isl_space
*dims
, int equal
)
4358 return map_lex_gte_first(dims
, dims
->n_out
, equal
);
4361 __isl_give isl_map
*isl_map_lex_gt_first(__isl_take isl_space
*dim
, unsigned n
)
4363 return map_lex_gte_first(dim
, n
, 0);
4366 __isl_give isl_map
*isl_map_lex_ge_first(__isl_take isl_space
*dim
, unsigned n
)
4368 return map_lex_gte_first(dim
, n
, 1);
4371 __isl_give isl_map
*isl_map_lex_gt(__isl_take isl_space
*set_dim
)
4373 return map_lex_gte(isl_space_map_from_set(set_dim
), 0);
4376 __isl_give isl_map
*isl_map_lex_ge(__isl_take isl_space
*set_dim
)
4378 return map_lex_gte(isl_space_map_from_set(set_dim
), 1);
4381 __isl_give isl_map
*isl_set_lex_le_set(__isl_take isl_set
*set1
,
4382 __isl_take isl_set
*set2
)
4385 map
= isl_map_lex_le(isl_set_get_space(set1
));
4386 map
= isl_map_intersect_domain(map
, set1
);
4387 map
= isl_map_intersect_range(map
, set2
);
4391 __isl_give isl_map
*isl_set_lex_lt_set(__isl_take isl_set
*set1
,
4392 __isl_take isl_set
*set2
)
4395 map
= isl_map_lex_lt(isl_set_get_space(set1
));
4396 map
= isl_map_intersect_domain(map
, set1
);
4397 map
= isl_map_intersect_range(map
, set2
);
4401 __isl_give isl_map
*isl_set_lex_ge_set(__isl_take isl_set
*set1
,
4402 __isl_take isl_set
*set2
)
4405 map
= isl_map_lex_ge(isl_set_get_space(set1
));
4406 map
= isl_map_intersect_domain(map
, set1
);
4407 map
= isl_map_intersect_range(map
, set2
);
4411 __isl_give isl_map
*isl_set_lex_gt_set(__isl_take isl_set
*set1
,
4412 __isl_take isl_set
*set2
)
4415 map
= isl_map_lex_gt(isl_set_get_space(set1
));
4416 map
= isl_map_intersect_domain(map
, set1
);
4417 map
= isl_map_intersect_range(map
, set2
);
4421 __isl_give isl_map
*isl_map_lex_le_map(__isl_take isl_map
*map1
,
4422 __isl_take isl_map
*map2
)
4425 map
= isl_map_lex_le(isl_space_range(isl_map_get_space(map1
)));
4426 map
= isl_map_apply_domain(map
, isl_map_reverse(map1
));
4427 map
= isl_map_apply_range(map
, isl_map_reverse(map2
));
4431 __isl_give isl_map
*isl_map_lex_lt_map(__isl_take isl_map
*map1
,
4432 __isl_take isl_map
*map2
)
4435 map
= isl_map_lex_lt(isl_space_range(isl_map_get_space(map1
)));
4436 map
= isl_map_apply_domain(map
, isl_map_reverse(map1
));
4437 map
= isl_map_apply_range(map
, isl_map_reverse(map2
));
4441 __isl_give isl_map
*isl_map_lex_ge_map(__isl_take isl_map
*map1
,
4442 __isl_take isl_map
*map2
)
4445 map
= isl_map_lex_ge(isl_space_range(isl_map_get_space(map1
)));
4446 map
= isl_map_apply_domain(map
, isl_map_reverse(map1
));
4447 map
= isl_map_apply_range(map
, isl_map_reverse(map2
));
4451 __isl_give isl_map
*isl_map_lex_gt_map(__isl_take isl_map
*map1
,
4452 __isl_take isl_map
*map2
)
4455 map
= isl_map_lex_gt(isl_space_range(isl_map_get_space(map1
)));
4456 map
= isl_map_apply_domain(map
, isl_map_reverse(map1
));
4457 map
= isl_map_apply_range(map
, isl_map_reverse(map2
));
4461 __isl_give isl_basic_map
*isl_basic_map_from_basic_set(
4462 __isl_take isl_basic_set
*bset
, __isl_take isl_space
*dim
)
4464 struct isl_basic_map
*bmap
;
4466 bset
= isl_basic_set_cow(bset
);
4470 isl_assert(bset
->ctx
, isl_space_compatible(bset
->dim
, dim
), goto error
);
4471 isl_space_free(bset
->dim
);
4472 bmap
= (struct isl_basic_map
*) bset
;
4474 return isl_basic_map_finalize(bmap
);
4476 isl_basic_set_free(bset
);
4477 isl_space_free(dim
);
4481 /* For a div d = floor(f/m), add the constraint
4485 static int add_upper_div_constraint(__isl_keep isl_basic_map
*bmap
,
4486 unsigned pos
, isl_int
*div
)
4489 unsigned total
= isl_basic_map_total_dim(bmap
);
4491 i
= isl_basic_map_alloc_inequality(bmap
);
4494 isl_seq_cpy(bmap
->ineq
[i
], div
+ 1, 1 + total
);
4495 isl_int_neg(bmap
->ineq
[i
][1 + pos
], div
[0]);
4500 /* For a div d = floor(f/m), add the constraint
4502 * -(f-(n-1)) + m d >= 0
4504 static int add_lower_div_constraint(__isl_keep isl_basic_map
*bmap
,
4505 unsigned pos
, isl_int
*div
)
4508 unsigned total
= isl_basic_map_total_dim(bmap
);
4510 i
= isl_basic_map_alloc_inequality(bmap
);
4513 isl_seq_neg(bmap
->ineq
[i
], div
+ 1, 1 + total
);
4514 isl_int_set(bmap
->ineq
[i
][1 + pos
], div
[0]);
4515 isl_int_add(bmap
->ineq
[i
][0], bmap
->ineq
[i
][0], bmap
->ineq
[i
][1 + pos
]);
4516 isl_int_sub_ui(bmap
->ineq
[i
][0], bmap
->ineq
[i
][0], 1);
4521 /* For a div d = floor(f/m), add the constraints
4524 * -(f-(n-1)) + m d >= 0
4526 * Note that the second constraint is the negation of
4530 int isl_basic_map_add_div_constraints_var(__isl_keep isl_basic_map
*bmap
,
4531 unsigned pos
, isl_int
*div
)
4533 if (add_upper_div_constraint(bmap
, pos
, div
) < 0)
4535 if (add_lower_div_constraint(bmap
, pos
, div
) < 0)
4540 int isl_basic_set_add_div_constraints_var(__isl_keep isl_basic_set
*bset
,
4541 unsigned pos
, isl_int
*div
)
4543 return isl_basic_map_add_div_constraints_var((isl_basic_map
*)bset
,
4547 int isl_basic_map_add_div_constraints(struct isl_basic_map
*bmap
, unsigned div
)
4549 unsigned total
= isl_basic_map_total_dim(bmap
);
4550 unsigned div_pos
= total
- bmap
->n_div
+ div
;
4552 return isl_basic_map_add_div_constraints_var(bmap
, div_pos
,
4556 /* For each known div d = floor(f/m), add the constraints
4559 * -(f-(n-1)) + m d >= 0
4561 * Remove duplicate constraints in case of some these div constraints
4562 * already appear in "bmap".
4564 __isl_give isl_basic_map
*isl_basic_map_add_known_div_constraints(
4565 __isl_take isl_basic_map
*bmap
)
4571 n_div
= isl_basic_map_dim(bmap
, isl_dim_div
);
4575 bmap
= add_known_div_constraints(bmap
);
4576 bmap
= isl_basic_map_remove_duplicate_constraints(bmap
, NULL
, 0);
4577 bmap
= isl_basic_map_finalize(bmap
);
4581 /* Add the div constraint of sign "sign" for div "div" of "bmap".
4583 * In particular, if this div is of the form d = floor(f/m),
4584 * then add the constraint
4588 * if sign < 0 or the constraint
4590 * -(f-(n-1)) + m d >= 0
4594 int isl_basic_map_add_div_constraint(__isl_keep isl_basic_map
*bmap
,
4595 unsigned div
, int sign
)
4603 total
= isl_basic_map_total_dim(bmap
);
4604 div_pos
= total
- bmap
->n_div
+ div
;
4607 return add_upper_div_constraint(bmap
, div_pos
, bmap
->div
[div
]);
4609 return add_lower_div_constraint(bmap
, div_pos
, bmap
->div
[div
]);
4612 int isl_basic_set_add_div_constraints(struct isl_basic_set
*bset
, unsigned div
)
4614 return isl_basic_map_add_div_constraints(bset
, div
);
4617 struct isl_basic_set
*isl_basic_map_underlying_set(
4618 struct isl_basic_map
*bmap
)
4622 if (bmap
->dim
->nparam
== 0 && bmap
->dim
->n_in
== 0 &&
4624 !isl_space_is_named_or_nested(bmap
->dim
, isl_dim_in
) &&
4625 !isl_space_is_named_or_nested(bmap
->dim
, isl_dim_out
))
4626 return (struct isl_basic_set
*)bmap
;
4627 bmap
= isl_basic_map_cow(bmap
);
4630 bmap
->dim
= isl_space_underlying(bmap
->dim
, bmap
->n_div
);
4633 bmap
->extra
-= bmap
->n_div
;
4635 bmap
= isl_basic_map_finalize(bmap
);
4636 return (struct isl_basic_set
*)bmap
;
4638 isl_basic_map_free(bmap
);
4642 __isl_give isl_basic_set
*isl_basic_set_underlying_set(
4643 __isl_take isl_basic_set
*bset
)
4645 return isl_basic_map_underlying_set((isl_basic_map
*)bset
);
4648 /* Replace each element in "list" by the result of applying
4649 * isl_basic_map_underlying_set to the element.
4651 __isl_give isl_basic_set_list
*isl_basic_map_list_underlying_set(
4652 __isl_take isl_basic_map_list
*list
)
4659 n
= isl_basic_map_list_n_basic_map(list
);
4660 for (i
= 0; i
< n
; ++i
) {
4661 isl_basic_map
*bmap
;
4662 isl_basic_set
*bset
;
4664 bmap
= isl_basic_map_list_get_basic_map(list
, i
);
4665 bset
= isl_basic_set_underlying_set(bmap
);
4666 list
= isl_basic_set_list_set_basic_set(list
, i
, bset
);
4672 struct isl_basic_map
*isl_basic_map_overlying_set(
4673 struct isl_basic_set
*bset
, struct isl_basic_map
*like
)
4675 struct isl_basic_map
*bmap
;
4676 struct isl_ctx
*ctx
;
4683 isl_assert(ctx
, bset
->n_div
== 0, goto error
);
4684 isl_assert(ctx
, isl_basic_set_n_param(bset
) == 0, goto error
);
4685 isl_assert(ctx
, bset
->dim
->n_out
== isl_basic_map_total_dim(like
),
4687 if (like
->n_div
== 0) {
4688 isl_space
*space
= isl_basic_map_get_space(like
);
4689 isl_basic_map_free(like
);
4690 return isl_basic_map_reset_space(bset
, space
);
4692 bset
= isl_basic_set_cow(bset
);
4695 total
= bset
->dim
->n_out
+ bset
->extra
;
4696 bmap
= (struct isl_basic_map
*)bset
;
4697 isl_space_free(bmap
->dim
);
4698 bmap
->dim
= isl_space_copy(like
->dim
);
4701 bmap
->n_div
= like
->n_div
;
4702 bmap
->extra
+= like
->n_div
;
4706 ltotal
= total
- bmap
->extra
+ like
->extra
;
4709 bmap
->block2
= isl_blk_extend(ctx
, bmap
->block2
,
4710 bmap
->extra
* (1 + 1 + total
));
4711 if (isl_blk_is_error(bmap
->block2
))
4713 div
= isl_realloc_array(ctx
, bmap
->div
, isl_int
*, bmap
->extra
);
4717 for (i
= 0; i
< bmap
->extra
; ++i
)
4718 bmap
->div
[i
] = bmap
->block2
.data
+ i
* (1 + 1 + total
);
4719 for (i
= 0; i
< like
->n_div
; ++i
) {
4720 isl_seq_cpy(bmap
->div
[i
], like
->div
[i
], 1 + 1 + ltotal
);
4721 isl_seq_clr(bmap
->div
[i
]+1+1+ltotal
, total
- ltotal
);
4723 bmap
= isl_basic_map_add_known_div_constraints(bmap
);
4725 isl_basic_map_free(like
);
4726 bmap
= isl_basic_map_simplify(bmap
);
4727 bmap
= isl_basic_map_finalize(bmap
);
4730 isl_basic_map_free(like
);
4731 isl_basic_set_free(bset
);
4735 struct isl_basic_set
*isl_basic_set_from_underlying_set(
4736 struct isl_basic_set
*bset
, struct isl_basic_set
*like
)
4738 return (struct isl_basic_set
*)
4739 isl_basic_map_overlying_set(bset
, (struct isl_basic_map
*)like
);
4742 struct isl_set
*isl_set_from_underlying_set(
4743 struct isl_set
*set
, struct isl_basic_set
*like
)
4749 isl_assert(set
->ctx
, set
->dim
->n_out
== isl_basic_set_total_dim(like
),
4751 if (isl_space_is_equal(set
->dim
, like
->dim
) && like
->n_div
== 0) {
4752 isl_basic_set_free(like
);
4755 set
= isl_set_cow(set
);
4758 for (i
= 0; i
< set
->n
; ++i
) {
4759 set
->p
[i
] = isl_basic_set_from_underlying_set(set
->p
[i
],
4760 isl_basic_set_copy(like
));
4764 isl_space_free(set
->dim
);
4765 set
->dim
= isl_space_copy(like
->dim
);
4768 isl_basic_set_free(like
);
4771 isl_basic_set_free(like
);
4776 struct isl_set
*isl_map_underlying_set(struct isl_map
*map
)
4780 map
= isl_map_cow(map
);
4783 map
->dim
= isl_space_cow(map
->dim
);
4787 for (i
= 1; i
< map
->n
; ++i
)
4788 isl_assert(map
->ctx
, map
->p
[0]->n_div
== map
->p
[i
]->n_div
,
4790 for (i
= 0; i
< map
->n
; ++i
) {
4791 map
->p
[i
] = (struct isl_basic_map
*)
4792 isl_basic_map_underlying_set(map
->p
[i
]);
4797 map
->dim
= isl_space_underlying(map
->dim
, 0);
4799 isl_space_free(map
->dim
);
4800 map
->dim
= isl_space_copy(map
->p
[0]->dim
);
4804 return (struct isl_set
*)map
;
4810 struct isl_set
*isl_set_to_underlying_set(struct isl_set
*set
)
4812 return (struct isl_set
*)isl_map_underlying_set((struct isl_map
*)set
);
4815 /* Replace the space of "bmap" by "space".
4817 * If the space of "bmap" is identical to "space" (including the identifiers
4818 * of the input and output dimensions), then simply return the original input.
4820 __isl_give isl_basic_map
*isl_basic_map_reset_space(
4821 __isl_take isl_basic_map
*bmap
, __isl_take isl_space
*space
)
4827 equal
= isl_space_is_equal(bmap
->dim
, space
);
4828 if (equal
>= 0 && equal
)
4829 equal
= isl_space_match(bmap
->dim
, isl_dim_in
,
4831 if (equal
>= 0 && equal
)
4832 equal
= isl_space_match(bmap
->dim
, isl_dim_out
,
4833 space
, isl_dim_out
);
4837 isl_space_free(space
);
4840 bmap
= isl_basic_map_cow(bmap
);
4841 if (!bmap
|| !space
)
4844 isl_space_free(bmap
->dim
);
4847 bmap
= isl_basic_map_finalize(bmap
);
4851 isl_basic_map_free(bmap
);
4852 isl_space_free(space
);
4856 __isl_give isl_basic_set
*isl_basic_set_reset_space(
4857 __isl_take isl_basic_set
*bset
, __isl_take isl_space
*dim
)
4859 return (isl_basic_set
*)isl_basic_map_reset_space((isl_basic_map
*)bset
,
4863 __isl_give isl_map
*isl_map_reset_space(__isl_take isl_map
*map
,
4864 __isl_take isl_space
*dim
)
4868 map
= isl_map_cow(map
);
4872 for (i
= 0; i
< map
->n
; ++i
) {
4873 map
->p
[i
] = isl_basic_map_reset_space(map
->p
[i
],
4874 isl_space_copy(dim
));
4878 isl_space_free(map
->dim
);
4884 isl_space_free(dim
);
4888 __isl_give isl_set
*isl_set_reset_space(__isl_take isl_set
*set
,
4889 __isl_take isl_space
*dim
)
4891 return (struct isl_set
*) isl_map_reset_space((struct isl_map
*)set
, dim
);
4894 /* Compute the parameter domain of the given basic set.
4896 __isl_give isl_basic_set
*isl_basic_set_params(__isl_take isl_basic_set
*bset
)
4901 if (isl_basic_set_is_params(bset
))
4904 n
= isl_basic_set_dim(bset
, isl_dim_set
);
4905 bset
= isl_basic_set_project_out(bset
, isl_dim_set
, 0, n
);
4906 space
= isl_basic_set_get_space(bset
);
4907 space
= isl_space_params(space
);
4908 bset
= isl_basic_set_reset_space(bset
, space
);
4912 /* Construct a zero-dimensional basic set with the given parameter domain.
4914 __isl_give isl_basic_set
*isl_basic_set_from_params(
4915 __isl_take isl_basic_set
*bset
)
4918 space
= isl_basic_set_get_space(bset
);
4919 space
= isl_space_set_from_params(space
);
4920 bset
= isl_basic_set_reset_space(bset
, space
);
4924 /* Compute the parameter domain of the given set.
4926 __isl_give isl_set
*isl_set_params(__isl_take isl_set
*set
)
4931 if (isl_set_is_params(set
))
4934 n
= isl_set_dim(set
, isl_dim_set
);
4935 set
= isl_set_project_out(set
, isl_dim_set
, 0, n
);
4936 space
= isl_set_get_space(set
);
4937 space
= isl_space_params(space
);
4938 set
= isl_set_reset_space(set
, space
);
4942 /* Construct a zero-dimensional set with the given parameter domain.
4944 __isl_give isl_set
*isl_set_from_params(__isl_take isl_set
*set
)
4947 space
= isl_set_get_space(set
);
4948 space
= isl_space_set_from_params(space
);
4949 set
= isl_set_reset_space(set
, space
);
4953 /* Compute the parameter domain of the given map.
4955 __isl_give isl_set
*isl_map_params(__isl_take isl_map
*map
)
4960 n
= isl_map_dim(map
, isl_dim_in
);
4961 map
= isl_map_project_out(map
, isl_dim_in
, 0, n
);
4962 n
= isl_map_dim(map
, isl_dim_out
);
4963 map
= isl_map_project_out(map
, isl_dim_out
, 0, n
);
4964 space
= isl_map_get_space(map
);
4965 space
= isl_space_params(space
);
4966 map
= isl_map_reset_space(map
, space
);
4970 struct isl_basic_set
*isl_basic_map_domain(struct isl_basic_map
*bmap
)
4977 space
= isl_space_domain(isl_basic_map_get_space(bmap
));
4979 n_out
= isl_basic_map_n_out(bmap
);
4980 bmap
= isl_basic_map_project_out(bmap
, isl_dim_out
, 0, n_out
);
4982 return isl_basic_map_reset_space(bmap
, space
);
4985 int isl_basic_map_may_be_set(__isl_keep isl_basic_map
*bmap
)
4989 return isl_space_may_be_set(bmap
->dim
);
4992 /* Is this basic map actually a set?
4993 * Users should never call this function. Outside of isl,
4994 * the type should indicate whether something is a set or a map.
4996 int isl_basic_map_is_set(__isl_keep isl_basic_map
*bmap
)
5000 return isl_space_is_set(bmap
->dim
);
5003 struct isl_basic_set
*isl_basic_map_range(struct isl_basic_map
*bmap
)
5007 if (isl_basic_map_is_set(bmap
))
5009 return isl_basic_map_domain(isl_basic_map_reverse(bmap
));
5012 __isl_give isl_basic_map
*isl_basic_map_domain_map(
5013 __isl_take isl_basic_map
*bmap
)
5017 isl_basic_map
*domain
;
5018 int nparam
, n_in
, n_out
;
5021 nparam
= isl_basic_map_dim(bmap
, isl_dim_param
);
5022 n_in
= isl_basic_map_dim(bmap
, isl_dim_in
);
5023 n_out
= isl_basic_map_dim(bmap
, isl_dim_out
);
5025 dim
= isl_space_from_range(isl_space_domain(isl_basic_map_get_space(bmap
)));
5026 domain
= isl_basic_map_universe(dim
);
5028 bmap
= isl_basic_map_from_domain(isl_basic_map_wrap(bmap
));
5029 bmap
= isl_basic_map_apply_range(bmap
, domain
);
5030 bmap
= isl_basic_map_extend_constraints(bmap
, n_in
, 0);
5032 total
= isl_basic_map_total_dim(bmap
);
5034 for (i
= 0; i
< n_in
; ++i
) {
5035 k
= isl_basic_map_alloc_equality(bmap
);
5038 isl_seq_clr(bmap
->eq
[k
], 1 + total
);
5039 isl_int_set_si(bmap
->eq
[k
][1 + nparam
+ i
], -1);
5040 isl_int_set_si(bmap
->eq
[k
][1 + nparam
+ n_in
+ n_out
+ i
], 1);
5043 bmap
= isl_basic_map_gauss(bmap
, NULL
);
5044 return isl_basic_map_finalize(bmap
);
5046 isl_basic_map_free(bmap
);
5050 __isl_give isl_basic_map
*isl_basic_map_range_map(
5051 __isl_take isl_basic_map
*bmap
)
5055 isl_basic_map
*range
;
5056 int nparam
, n_in
, n_out
;
5059 nparam
= isl_basic_map_dim(bmap
, isl_dim_param
);
5060 n_in
= isl_basic_map_dim(bmap
, isl_dim_in
);
5061 n_out
= isl_basic_map_dim(bmap
, isl_dim_out
);
5063 dim
= isl_space_from_range(isl_space_range(isl_basic_map_get_space(bmap
)));
5064 range
= isl_basic_map_universe(dim
);
5066 bmap
= isl_basic_map_from_domain(isl_basic_map_wrap(bmap
));
5067 bmap
= isl_basic_map_apply_range(bmap
, range
);
5068 bmap
= isl_basic_map_extend_constraints(bmap
, n_out
, 0);
5070 total
= isl_basic_map_total_dim(bmap
);
5072 for (i
= 0; i
< n_out
; ++i
) {
5073 k
= isl_basic_map_alloc_equality(bmap
);
5076 isl_seq_clr(bmap
->eq
[k
], 1 + total
);
5077 isl_int_set_si(bmap
->eq
[k
][1 + nparam
+ n_in
+ i
], -1);
5078 isl_int_set_si(bmap
->eq
[k
][1 + nparam
+ n_in
+ n_out
+ i
], 1);
5081 bmap
= isl_basic_map_gauss(bmap
, NULL
);
5082 return isl_basic_map_finalize(bmap
);
5084 isl_basic_map_free(bmap
);
5088 int isl_map_may_be_set(__isl_keep isl_map
*map
)
5092 return isl_space_may_be_set(map
->dim
);
5095 /* Is this map actually a set?
5096 * Users should never call this function. Outside of isl,
5097 * the type should indicate whether something is a set or a map.
5099 int isl_map_is_set(__isl_keep isl_map
*map
)
5103 return isl_space_is_set(map
->dim
);
5106 struct isl_set
*isl_map_range(struct isl_map
*map
)
5109 struct isl_set
*set
;
5113 if (isl_map_is_set(map
))
5114 return (isl_set
*)map
;
5116 map
= isl_map_cow(map
);
5120 set
= (struct isl_set
*) map
;
5121 set
->dim
= isl_space_range(set
->dim
);
5124 for (i
= 0; i
< map
->n
; ++i
) {
5125 set
->p
[i
] = isl_basic_map_range(map
->p
[i
]);
5129 ISL_F_CLR(set
, ISL_MAP_DISJOINT
);
5130 ISL_F_CLR(set
, ISL_SET_NORMALIZED
);
5137 __isl_give isl_map
*isl_map_domain_map(__isl_take isl_map
*map
)
5141 map
= isl_map_cow(map
);
5145 map
->dim
= isl_space_domain_map(map
->dim
);
5148 for (i
= 0; i
< map
->n
; ++i
) {
5149 map
->p
[i
] = isl_basic_map_domain_map(map
->p
[i
]);
5153 ISL_F_CLR(map
, ISL_MAP_DISJOINT
);
5154 ISL_F_CLR(map
, ISL_MAP_NORMALIZED
);
5161 __isl_give isl_map
*isl_map_range_map(__isl_take isl_map
*map
)
5164 isl_space
*range_dim
;
5166 map
= isl_map_cow(map
);
5170 range_dim
= isl_space_range(isl_map_get_space(map
));
5171 range_dim
= isl_space_from_range(range_dim
);
5172 map
->dim
= isl_space_from_domain(isl_space_wrap(map
->dim
));
5173 map
->dim
= isl_space_join(map
->dim
, range_dim
);
5176 for (i
= 0; i
< map
->n
; ++i
) {
5177 map
->p
[i
] = isl_basic_map_range_map(map
->p
[i
]);
5181 ISL_F_CLR(map
, ISL_MAP_DISJOINT
);
5182 ISL_F_CLR(map
, ISL_MAP_NORMALIZED
);
5189 /* Given a wrapped map of the form A[B -> C],
5190 * return the map A[B -> C] -> B.
5192 __isl_give isl_map
*isl_set_wrapped_domain_map(__isl_take isl_set
*set
)
5199 if (!isl_set_has_tuple_id(set
))
5200 return isl_map_domain_map(isl_set_unwrap(set
));
5202 id
= isl_set_get_tuple_id(set
);
5203 map
= isl_map_domain_map(isl_set_unwrap(set
));
5204 map
= isl_map_set_tuple_id(map
, isl_dim_in
, id
);
5209 __isl_give isl_map
*isl_map_from_set(__isl_take isl_set
*set
,
5210 __isl_take isl_space
*dim
)
5213 struct isl_map
*map
= NULL
;
5215 set
= isl_set_cow(set
);
5218 isl_assert(set
->ctx
, isl_space_compatible(set
->dim
, dim
), goto error
);
5219 map
= (struct isl_map
*)set
;
5220 for (i
= 0; i
< set
->n
; ++i
) {
5221 map
->p
[i
] = isl_basic_map_from_basic_set(
5222 set
->p
[i
], isl_space_copy(dim
));
5226 isl_space_free(map
->dim
);
5230 isl_space_free(dim
);
5235 __isl_give isl_basic_map
*isl_basic_map_from_domain(
5236 __isl_take isl_basic_set
*bset
)
5238 return isl_basic_map_reverse(isl_basic_map_from_range(bset
));
5241 __isl_give isl_basic_map
*isl_basic_map_from_range(
5242 __isl_take isl_basic_set
*bset
)
5245 space
= isl_basic_set_get_space(bset
);
5246 space
= isl_space_from_range(space
);
5247 bset
= isl_basic_set_reset_space(bset
, space
);
5248 return (isl_basic_map
*)bset
;
5251 /* Create a relation with the given set as range.
5252 * The domain of the created relation is a zero-dimensional
5253 * flat anonymous space.
5255 __isl_give isl_map
*isl_map_from_range(__isl_take isl_set
*set
)
5258 space
= isl_set_get_space(set
);
5259 space
= isl_space_from_range(space
);
5260 set
= isl_set_reset_space(set
, space
);
5261 return (struct isl_map
*)set
;
5264 /* Create a relation with the given set as domain.
5265 * The range of the created relation is a zero-dimensional
5266 * flat anonymous space.
5268 __isl_give isl_map
*isl_map_from_domain(__isl_take isl_set
*set
)
5270 return isl_map_reverse(isl_map_from_range(set
));
5273 __isl_give isl_basic_map
*isl_basic_map_from_domain_and_range(
5274 __isl_take isl_basic_set
*domain
, __isl_take isl_basic_set
*range
)
5276 return isl_basic_map_apply_range(isl_basic_map_reverse(domain
), range
);
5279 __isl_give isl_map
*isl_map_from_domain_and_range(__isl_take isl_set
*domain
,
5280 __isl_take isl_set
*range
)
5282 return isl_map_apply_range(isl_map_reverse(domain
), range
);
5285 __isl_give isl_map
*isl_map_alloc_space(__isl_take isl_space
*dim
, int n
,
5288 struct isl_map
*map
;
5293 isl_die(dim
->ctx
, isl_error_internal
,
5294 "negative number of basic maps", goto error
);
5295 map
= isl_alloc(dim
->ctx
, struct isl_map
,
5296 sizeof(struct isl_map
) +
5297 (n
- 1) * sizeof(struct isl_basic_map
*));
5301 map
->ctx
= dim
->ctx
;
5302 isl_ctx_ref(map
->ctx
);
5310 isl_space_free(dim
);
5314 struct isl_map
*isl_map_alloc(struct isl_ctx
*ctx
,
5315 unsigned nparam
, unsigned in
, unsigned out
, int n
,
5318 struct isl_map
*map
;
5321 dims
= isl_space_alloc(ctx
, nparam
, in
, out
);
5325 map
= isl_map_alloc_space(dims
, n
, flags
);
5329 __isl_give isl_basic_map
*isl_basic_map_empty(__isl_take isl_space
*dim
)
5331 struct isl_basic_map
*bmap
;
5332 bmap
= isl_basic_map_alloc_space(dim
, 0, 1, 0);
5333 bmap
= isl_basic_map_set_to_empty(bmap
);
5337 __isl_give isl_basic_set
*isl_basic_set_empty(__isl_take isl_space
*dim
)
5339 struct isl_basic_set
*bset
;
5340 bset
= isl_basic_set_alloc_space(dim
, 0, 1, 0);
5341 bset
= isl_basic_set_set_to_empty(bset
);
5345 __isl_give isl_basic_map
*isl_basic_map_universe(__isl_take isl_space
*dim
)
5347 struct isl_basic_map
*bmap
;
5348 bmap
= isl_basic_map_alloc_space(dim
, 0, 0, 0);
5349 bmap
= isl_basic_map_finalize(bmap
);
5353 __isl_give isl_basic_set
*isl_basic_set_universe(__isl_take isl_space
*dim
)
5355 struct isl_basic_set
*bset
;
5356 bset
= isl_basic_set_alloc_space(dim
, 0, 0, 0);
5357 bset
= isl_basic_set_finalize(bset
);
5361 __isl_give isl_basic_map
*isl_basic_map_nat_universe(__isl_take isl_space
*dim
)
5364 unsigned total
= isl_space_dim(dim
, isl_dim_all
);
5365 isl_basic_map
*bmap
;
5367 bmap
= isl_basic_map_alloc_space(dim
, 0, 0, total
);
5368 for (i
= 0; i
< total
; ++i
) {
5369 int k
= isl_basic_map_alloc_inequality(bmap
);
5372 isl_seq_clr(bmap
->ineq
[k
], 1 + total
);
5373 isl_int_set_si(bmap
->ineq
[k
][1 + i
], 1);
5377 isl_basic_map_free(bmap
);
5381 __isl_give isl_basic_set
*isl_basic_set_nat_universe(__isl_take isl_space
*dim
)
5383 return isl_basic_map_nat_universe(dim
);
5386 __isl_give isl_map
*isl_map_nat_universe(__isl_take isl_space
*dim
)
5388 return isl_map_from_basic_map(isl_basic_map_nat_universe(dim
));
5391 __isl_give isl_set
*isl_set_nat_universe(__isl_take isl_space
*dim
)
5393 return isl_map_nat_universe(dim
);
5396 __isl_give isl_map
*isl_map_empty(__isl_take isl_space
*dim
)
5398 return isl_map_alloc_space(dim
, 0, ISL_MAP_DISJOINT
);
5401 __isl_give isl_set
*isl_set_empty(__isl_take isl_space
*dim
)
5403 return isl_set_alloc_space(dim
, 0, ISL_MAP_DISJOINT
);
5406 __isl_give isl_map
*isl_map_universe(__isl_take isl_space
*dim
)
5408 struct isl_map
*map
;
5411 map
= isl_map_alloc_space(isl_space_copy(dim
), 1, ISL_MAP_DISJOINT
);
5412 map
= isl_map_add_basic_map(map
, isl_basic_map_universe(dim
));
5416 __isl_give isl_set
*isl_set_universe(__isl_take isl_space
*dim
)
5418 struct isl_set
*set
;
5421 set
= isl_set_alloc_space(isl_space_copy(dim
), 1, ISL_MAP_DISJOINT
);
5422 set
= isl_set_add_basic_set(set
, isl_basic_set_universe(dim
));
5426 struct isl_map
*isl_map_dup(struct isl_map
*map
)
5429 struct isl_map
*dup
;
5433 dup
= isl_map_alloc_space(isl_space_copy(map
->dim
), map
->n
, map
->flags
);
5434 for (i
= 0; i
< map
->n
; ++i
)
5435 dup
= isl_map_add_basic_map(dup
, isl_basic_map_copy(map
->p
[i
]));
5439 __isl_give isl_map
*isl_map_add_basic_map(__isl_take isl_map
*map
,
5440 __isl_take isl_basic_map
*bmap
)
5444 if (isl_basic_map_plain_is_empty(bmap
)) {
5445 isl_basic_map_free(bmap
);
5448 isl_assert(map
->ctx
, isl_space_is_equal(map
->dim
, bmap
->dim
), goto error
);
5449 isl_assert(map
->ctx
, map
->n
< map
->size
, goto error
);
5450 map
->p
[map
->n
] = bmap
;
5452 ISL_F_CLR(map
, ISL_MAP_NORMALIZED
);
5458 isl_basic_map_free(bmap
);
5462 __isl_null isl_map
*isl_map_free(__isl_take isl_map
*map
)
5472 isl_ctx_deref(map
->ctx
);
5473 for (i
= 0; i
< map
->n
; ++i
)
5474 isl_basic_map_free(map
->p
[i
]);
5475 isl_space_free(map
->dim
);
5481 static struct isl_basic_map
*isl_basic_map_fix_pos_si(
5482 struct isl_basic_map
*bmap
, unsigned pos
, int value
)
5486 bmap
= isl_basic_map_cow(bmap
);
5487 bmap
= isl_basic_map_extend_constraints(bmap
, 1, 0);
5488 j
= isl_basic_map_alloc_equality(bmap
);
5491 isl_seq_clr(bmap
->eq
[j
] + 1, isl_basic_map_total_dim(bmap
));
5492 isl_int_set_si(bmap
->eq
[j
][pos
], -1);
5493 isl_int_set_si(bmap
->eq
[j
][0], value
);
5494 bmap
= isl_basic_map_simplify(bmap
);
5495 return isl_basic_map_finalize(bmap
);
5497 isl_basic_map_free(bmap
);
5501 static __isl_give isl_basic_map
*isl_basic_map_fix_pos(
5502 __isl_take isl_basic_map
*bmap
, unsigned pos
, isl_int value
)
5506 bmap
= isl_basic_map_cow(bmap
);
5507 bmap
= isl_basic_map_extend_constraints(bmap
, 1, 0);
5508 j
= isl_basic_map_alloc_equality(bmap
);
5511 isl_seq_clr(bmap
->eq
[j
] + 1, isl_basic_map_total_dim(bmap
));
5512 isl_int_set_si(bmap
->eq
[j
][pos
], -1);
5513 isl_int_set(bmap
->eq
[j
][0], value
);
5514 bmap
= isl_basic_map_simplify(bmap
);
5515 return isl_basic_map_finalize(bmap
);
5517 isl_basic_map_free(bmap
);
5521 struct isl_basic_map
*isl_basic_map_fix_si(struct isl_basic_map
*bmap
,
5522 enum isl_dim_type type
, unsigned pos
, int value
)
5526 isl_assert(bmap
->ctx
, pos
< isl_basic_map_dim(bmap
, type
), goto error
);
5527 return isl_basic_map_fix_pos_si(bmap
,
5528 isl_basic_map_offset(bmap
, type
) + pos
, value
);
5530 isl_basic_map_free(bmap
);
5534 __isl_give isl_basic_map
*isl_basic_map_fix(__isl_take isl_basic_map
*bmap
,
5535 enum isl_dim_type type
, unsigned pos
, isl_int value
)
5539 isl_assert(bmap
->ctx
, pos
< isl_basic_map_dim(bmap
, type
), goto error
);
5540 return isl_basic_map_fix_pos(bmap
,
5541 isl_basic_map_offset(bmap
, type
) + pos
, value
);
5543 isl_basic_map_free(bmap
);
5547 /* Fix the value of the variable at position "pos" of type "type" of "bmap"
5548 * to be equal to "v".
5550 __isl_give isl_basic_map
*isl_basic_map_fix_val(__isl_take isl_basic_map
*bmap
,
5551 enum isl_dim_type type
, unsigned pos
, __isl_take isl_val
*v
)
5555 if (!isl_val_is_int(v
))
5556 isl_die(isl_basic_map_get_ctx(bmap
), isl_error_invalid
,
5557 "expecting integer value", goto error
);
5558 if (pos
>= isl_basic_map_dim(bmap
, type
))
5559 isl_die(isl_basic_map_get_ctx(bmap
), isl_error_invalid
,
5560 "index out of bounds", goto error
);
5561 pos
+= isl_basic_map_offset(bmap
, type
);
5562 bmap
= isl_basic_map_fix_pos(bmap
, pos
, v
->n
);
5566 isl_basic_map_free(bmap
);
5571 /* Fix the value of the variable at position "pos" of type "type" of "bset"
5572 * to be equal to "v".
5574 __isl_give isl_basic_set
*isl_basic_set_fix_val(__isl_take isl_basic_set
*bset
,
5575 enum isl_dim_type type
, unsigned pos
, __isl_take isl_val
*v
)
5577 return isl_basic_map_fix_val(bset
, type
, pos
, v
);
5580 struct isl_basic_set
*isl_basic_set_fix_si(struct isl_basic_set
*bset
,
5581 enum isl_dim_type type
, unsigned pos
, int value
)
5583 return (struct isl_basic_set
*)
5584 isl_basic_map_fix_si((struct isl_basic_map
*)bset
,
5588 __isl_give isl_basic_set
*isl_basic_set_fix(__isl_take isl_basic_set
*bset
,
5589 enum isl_dim_type type
, unsigned pos
, isl_int value
)
5591 return (struct isl_basic_set
*)
5592 isl_basic_map_fix((struct isl_basic_map
*)bset
,
5596 struct isl_basic_map
*isl_basic_map_fix_input_si(struct isl_basic_map
*bmap
,
5597 unsigned input
, int value
)
5599 return isl_basic_map_fix_si(bmap
, isl_dim_in
, input
, value
);
5602 struct isl_basic_set
*isl_basic_set_fix_dim_si(struct isl_basic_set
*bset
,
5603 unsigned dim
, int value
)
5605 return (struct isl_basic_set
*)
5606 isl_basic_map_fix_si((struct isl_basic_map
*)bset
,
5607 isl_dim_set
, dim
, value
);
5610 static int remove_if_empty(__isl_keep isl_map
*map
, int i
)
5612 int empty
= isl_basic_map_plain_is_empty(map
->p
[i
]);
5619 isl_basic_map_free(map
->p
[i
]);
5620 if (i
!= map
->n
- 1) {
5621 ISL_F_CLR(map
, ISL_MAP_NORMALIZED
);
5622 map
->p
[i
] = map
->p
[map
->n
- 1];
5629 /* Perform "fn" on each basic map of "map", where we may not be holding
5630 * the only reference to "map".
5631 * In particular, "fn" should be a semantics preserving operation
5632 * that we want to apply to all copies of "map". We therefore need
5633 * to be careful not to modify "map" in a way that breaks "map"
5634 * in case anything goes wrong.
5636 __isl_give isl_map
*isl_map_inline_foreach_basic_map(__isl_take isl_map
*map
,
5637 __isl_give isl_basic_map
*(*fn
)(__isl_take isl_basic_map
*bmap
))
5639 struct isl_basic_map
*bmap
;
5645 for (i
= map
->n
- 1; i
>= 0; --i
) {
5646 bmap
= isl_basic_map_copy(map
->p
[i
]);
5650 isl_basic_map_free(map
->p
[i
]);
5652 if (remove_if_empty(map
, i
) < 0)
5662 struct isl_map
*isl_map_fix_si(struct isl_map
*map
,
5663 enum isl_dim_type type
, unsigned pos
, int value
)
5667 map
= isl_map_cow(map
);
5671 isl_assert(map
->ctx
, pos
< isl_map_dim(map
, type
), goto error
);
5672 for (i
= map
->n
- 1; i
>= 0; --i
) {
5673 map
->p
[i
] = isl_basic_map_fix_si(map
->p
[i
], type
, pos
, value
);
5674 if (remove_if_empty(map
, i
) < 0)
5677 ISL_F_CLR(map
, ISL_MAP_NORMALIZED
);
5684 __isl_give isl_set
*isl_set_fix_si(__isl_take isl_set
*set
,
5685 enum isl_dim_type type
, unsigned pos
, int value
)
5687 return (struct isl_set
*)
5688 isl_map_fix_si((struct isl_map
*)set
, type
, pos
, value
);
5691 __isl_give isl_map
*isl_map_fix(__isl_take isl_map
*map
,
5692 enum isl_dim_type type
, unsigned pos
, isl_int value
)
5696 map
= isl_map_cow(map
);
5700 isl_assert(map
->ctx
, pos
< isl_map_dim(map
, type
), goto error
);
5701 for (i
= 0; i
< map
->n
; ++i
) {
5702 map
->p
[i
] = isl_basic_map_fix(map
->p
[i
], type
, pos
, value
);
5706 ISL_F_CLR(map
, ISL_MAP_NORMALIZED
);
5713 __isl_give isl_set
*isl_set_fix(__isl_take isl_set
*set
,
5714 enum isl_dim_type type
, unsigned pos
, isl_int value
)
5716 return (struct isl_set
*)isl_map_fix((isl_map
*)set
, type
, pos
, value
);
5719 /* Fix the value of the variable at position "pos" of type "type" of "map"
5720 * to be equal to "v".
5722 __isl_give isl_map
*isl_map_fix_val(__isl_take isl_map
*map
,
5723 enum isl_dim_type type
, unsigned pos
, __isl_take isl_val
*v
)
5727 map
= isl_map_cow(map
);
5731 if (!isl_val_is_int(v
))
5732 isl_die(isl_map_get_ctx(map
), isl_error_invalid
,
5733 "expecting integer value", goto error
);
5734 if (pos
>= isl_map_dim(map
, type
))
5735 isl_die(isl_map_get_ctx(map
), isl_error_invalid
,
5736 "index out of bounds", goto error
);
5737 for (i
= map
->n
- 1; i
>= 0; --i
) {
5738 map
->p
[i
] = isl_basic_map_fix_val(map
->p
[i
], type
, pos
,
5740 if (remove_if_empty(map
, i
) < 0)
5743 ISL_F_CLR(map
, ISL_MAP_NORMALIZED
);
5752 /* Fix the value of the variable at position "pos" of type "type" of "set"
5753 * to be equal to "v".
5755 __isl_give isl_set
*isl_set_fix_val(__isl_take isl_set
*set
,
5756 enum isl_dim_type type
, unsigned pos
, __isl_take isl_val
*v
)
5758 return isl_map_fix_val(set
, type
, pos
, v
);
5761 struct isl_map
*isl_map_fix_input_si(struct isl_map
*map
,
5762 unsigned input
, int value
)
5764 return isl_map_fix_si(map
, isl_dim_in
, input
, value
);
5767 struct isl_set
*isl_set_fix_dim_si(struct isl_set
*set
, unsigned dim
, int value
)
5769 return (struct isl_set
*)
5770 isl_map_fix_si((struct isl_map
*)set
, isl_dim_set
, dim
, value
);
5773 static __isl_give isl_basic_map
*basic_map_bound_si(
5774 __isl_take isl_basic_map
*bmap
,
5775 enum isl_dim_type type
, unsigned pos
, int value
, int upper
)
5781 isl_assert(bmap
->ctx
, pos
< isl_basic_map_dim(bmap
, type
), goto error
);
5782 pos
+= isl_basic_map_offset(bmap
, type
);
5783 bmap
= isl_basic_map_cow(bmap
);
5784 bmap
= isl_basic_map_extend_constraints(bmap
, 0, 1);
5785 j
= isl_basic_map_alloc_inequality(bmap
);
5788 isl_seq_clr(bmap
->ineq
[j
], 1 + isl_basic_map_total_dim(bmap
));
5790 isl_int_set_si(bmap
->ineq
[j
][pos
], -1);
5791 isl_int_set_si(bmap
->ineq
[j
][0], value
);
5793 isl_int_set_si(bmap
->ineq
[j
][pos
], 1);
5794 isl_int_set_si(bmap
->ineq
[j
][0], -value
);
5796 bmap
= isl_basic_map_simplify(bmap
);
5797 return isl_basic_map_finalize(bmap
);
5799 isl_basic_map_free(bmap
);
5803 __isl_give isl_basic_map
*isl_basic_map_lower_bound_si(
5804 __isl_take isl_basic_map
*bmap
,
5805 enum isl_dim_type type
, unsigned pos
, int value
)
5807 return basic_map_bound_si(bmap
, type
, pos
, value
, 0);
5810 /* Constrain the values of the given dimension to be no greater than "value".
5812 __isl_give isl_basic_map
*isl_basic_map_upper_bound_si(
5813 __isl_take isl_basic_map
*bmap
,
5814 enum isl_dim_type type
, unsigned pos
, int value
)
5816 return basic_map_bound_si(bmap
, type
, pos
, value
, 1);
5819 struct isl_basic_set
*isl_basic_set_lower_bound_dim(struct isl_basic_set
*bset
,
5820 unsigned dim
, isl_int value
)
5824 bset
= isl_basic_set_cow(bset
);
5825 bset
= isl_basic_set_extend_constraints(bset
, 0, 1);
5826 j
= isl_basic_set_alloc_inequality(bset
);
5829 isl_seq_clr(bset
->ineq
[j
], 1 + isl_basic_set_total_dim(bset
));
5830 isl_int_set_si(bset
->ineq
[j
][1 + isl_basic_set_n_param(bset
) + dim
], 1);
5831 isl_int_neg(bset
->ineq
[j
][0], value
);
5832 bset
= isl_basic_set_simplify(bset
);
5833 return isl_basic_set_finalize(bset
);
5835 isl_basic_set_free(bset
);
5839 static __isl_give isl_map
*map_bound_si(__isl_take isl_map
*map
,
5840 enum isl_dim_type type
, unsigned pos
, int value
, int upper
)
5844 map
= isl_map_cow(map
);
5848 isl_assert(map
->ctx
, pos
< isl_map_dim(map
, type
), goto error
);
5849 for (i
= 0; i
< map
->n
; ++i
) {
5850 map
->p
[i
] = basic_map_bound_si(map
->p
[i
],
5851 type
, pos
, value
, upper
);
5855 ISL_F_CLR(map
, ISL_MAP_NORMALIZED
);
5862 __isl_give isl_map
*isl_map_lower_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
, 0);
5868 __isl_give isl_map
*isl_map_upper_bound_si(__isl_take isl_map
*map
,
5869 enum isl_dim_type type
, unsigned pos
, int value
)
5871 return map_bound_si(map
, type
, pos
, value
, 1);
5874 __isl_give isl_set
*isl_set_lower_bound_si(__isl_take isl_set
*set
,
5875 enum isl_dim_type type
, unsigned pos
, int value
)
5877 return (struct isl_set
*)
5878 isl_map_lower_bound_si((struct isl_map
*)set
, type
, pos
, value
);
5881 __isl_give isl_set
*isl_set_upper_bound_si(__isl_take isl_set
*set
,
5882 enum isl_dim_type type
, unsigned pos
, int value
)
5884 return isl_map_upper_bound_si(set
, type
, pos
, value
);
5887 /* Bound the given variable of "bmap" from below (or above is "upper"
5888 * is set) to "value".
5890 static __isl_give isl_basic_map
*basic_map_bound(
5891 __isl_take isl_basic_map
*bmap
,
5892 enum isl_dim_type type
, unsigned pos
, isl_int value
, int upper
)
5898 if (pos
>= isl_basic_map_dim(bmap
, type
))
5899 isl_die(bmap
->ctx
, isl_error_invalid
,
5900 "index out of bounds", goto error
);
5901 pos
+= isl_basic_map_offset(bmap
, type
);
5902 bmap
= isl_basic_map_cow(bmap
);
5903 bmap
= isl_basic_map_extend_constraints(bmap
, 0, 1);
5904 j
= isl_basic_map_alloc_inequality(bmap
);
5907 isl_seq_clr(bmap
->ineq
[j
], 1 + isl_basic_map_total_dim(bmap
));
5909 isl_int_set_si(bmap
->ineq
[j
][pos
], -1);
5910 isl_int_set(bmap
->ineq
[j
][0], value
);
5912 isl_int_set_si(bmap
->ineq
[j
][pos
], 1);
5913 isl_int_neg(bmap
->ineq
[j
][0], value
);
5915 bmap
= isl_basic_map_simplify(bmap
);
5916 return isl_basic_map_finalize(bmap
);
5918 isl_basic_map_free(bmap
);
5922 /* Bound the given variable of "map" from below (or above is "upper"
5923 * is set) to "value".
5925 static __isl_give isl_map
*map_bound(__isl_take isl_map
*map
,
5926 enum isl_dim_type type
, unsigned pos
, isl_int value
, int upper
)
5930 map
= isl_map_cow(map
);
5934 if (pos
>= isl_map_dim(map
, type
))
5935 isl_die(map
->ctx
, isl_error_invalid
,
5936 "index out of bounds", goto error
);
5937 for (i
= map
->n
- 1; i
>= 0; --i
) {
5938 map
->p
[i
] = basic_map_bound(map
->p
[i
], type
, pos
, value
, upper
);
5939 if (remove_if_empty(map
, i
) < 0)
5942 ISL_F_CLR(map
, ISL_MAP_NORMALIZED
);
5949 __isl_give isl_map
*isl_map_lower_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
, 0);
5955 __isl_give isl_map
*isl_map_upper_bound(__isl_take isl_map
*map
,
5956 enum isl_dim_type type
, unsigned pos
, isl_int value
)
5958 return map_bound(map
, type
, pos
, value
, 1);
5961 __isl_give isl_set
*isl_set_lower_bound(__isl_take isl_set
*set
,
5962 enum isl_dim_type type
, unsigned pos
, isl_int value
)
5964 return isl_map_lower_bound(set
, type
, pos
, value
);
5967 __isl_give isl_set
*isl_set_upper_bound(__isl_take isl_set
*set
,
5968 enum isl_dim_type type
, unsigned pos
, isl_int value
)
5970 return isl_map_upper_bound(set
, type
, pos
, value
);
5973 /* Force the values of the variable at position "pos" of type "type" of "set"
5974 * to be no smaller than "value".
5976 __isl_give isl_set
*isl_set_lower_bound_val(__isl_take isl_set
*set
,
5977 enum isl_dim_type type
, unsigned pos
, __isl_take isl_val
*value
)
5981 if (!isl_val_is_int(value
))
5982 isl_die(isl_set_get_ctx(set
), isl_error_invalid
,
5983 "expecting integer value", goto error
);
5984 set
= isl_set_lower_bound(set
, type
, pos
, value
->n
);
5985 isl_val_free(value
);
5988 isl_val_free(value
);
5993 /* Force the values of the variable at position "pos" of type "type" of "set"
5994 * to be no greater than "value".
5996 __isl_give isl_set
*isl_set_upper_bound_val(__isl_take isl_set
*set
,
5997 enum isl_dim_type type
, unsigned pos
, __isl_take isl_val
*value
)
6001 if (!isl_val_is_int(value
))
6002 isl_die(isl_set_get_ctx(set
), isl_error_invalid
,
6003 "expecting integer value", goto error
);
6004 set
= isl_set_upper_bound(set
, type
, pos
, value
->n
);
6005 isl_val_free(value
);
6008 isl_val_free(value
);
6013 struct isl_set
*isl_set_lower_bound_dim(struct isl_set
*set
, unsigned dim
,
6018 set
= isl_set_cow(set
);
6022 isl_assert(set
->ctx
, dim
< isl_set_n_dim(set
), goto error
);
6023 for (i
= 0; i
< set
->n
; ++i
) {
6024 set
->p
[i
] = isl_basic_set_lower_bound_dim(set
->p
[i
], dim
, value
);
6034 struct isl_map
*isl_map_reverse(struct isl_map
*map
)
6038 map
= isl_map_cow(map
);
6042 map
->dim
= isl_space_reverse(map
->dim
);
6045 for (i
= 0; i
< map
->n
; ++i
) {
6046 map
->p
[i
] = isl_basic_map_reverse(map
->p
[i
]);
6050 ISL_F_CLR(map
, ISL_MAP_NORMALIZED
);
6057 static struct isl_map
*isl_basic_map_partial_lexopt(
6058 struct isl_basic_map
*bmap
, struct isl_basic_set
*dom
,
6059 struct isl_set
**empty
, int max
)
6061 return isl_tab_basic_map_partial_lexopt(bmap
, dom
, empty
, max
);
6064 struct isl_map
*isl_basic_map_partial_lexmax(
6065 struct isl_basic_map
*bmap
, struct isl_basic_set
*dom
,
6066 struct isl_set
**empty
)
6068 return isl_basic_map_partial_lexopt(bmap
, dom
, empty
, 1);
6071 struct isl_map
*isl_basic_map_partial_lexmin(
6072 struct isl_basic_map
*bmap
, struct isl_basic_set
*dom
,
6073 struct isl_set
**empty
)
6075 return isl_basic_map_partial_lexopt(bmap
, dom
, empty
, 0);
6078 struct isl_set
*isl_basic_set_partial_lexmin(
6079 struct isl_basic_set
*bset
, struct isl_basic_set
*dom
,
6080 struct isl_set
**empty
)
6082 return (struct isl_set
*)
6083 isl_basic_map_partial_lexmin((struct isl_basic_map
*)bset
,
6087 struct isl_set
*isl_basic_set_partial_lexmax(
6088 struct isl_basic_set
*bset
, struct isl_basic_set
*dom
,
6089 struct isl_set
**empty
)
6091 return (struct isl_set
*)
6092 isl_basic_map_partial_lexmax((struct isl_basic_map
*)bset
,
6096 __isl_give isl_pw_multi_aff
*isl_basic_map_partial_lexmin_pw_multi_aff(
6097 __isl_take isl_basic_map
*bmap
, __isl_take isl_basic_set
*dom
,
6098 __isl_give isl_set
**empty
)
6100 return isl_basic_map_partial_lexopt_pw_multi_aff(bmap
, dom
, empty
, 0);
6103 __isl_give isl_pw_multi_aff
*isl_basic_map_partial_lexmax_pw_multi_aff(
6104 __isl_take isl_basic_map
*bmap
, __isl_take isl_basic_set
*dom
,
6105 __isl_give isl_set
**empty
)
6107 return isl_basic_map_partial_lexopt_pw_multi_aff(bmap
, dom
, empty
, 1);
6110 __isl_give isl_pw_multi_aff
*isl_basic_set_partial_lexmin_pw_multi_aff(
6111 __isl_take isl_basic_set
*bset
, __isl_take isl_basic_set
*dom
,
6112 __isl_give isl_set
**empty
)
6114 return isl_basic_map_partial_lexmin_pw_multi_aff(bset
, dom
, empty
);
6117 __isl_give isl_pw_multi_aff
*isl_basic_set_partial_lexmax_pw_multi_aff(
6118 __isl_take isl_basic_set
*bset
, __isl_take isl_basic_set
*dom
,
6119 __isl_give isl_set
**empty
)
6121 return isl_basic_map_partial_lexmax_pw_multi_aff(bset
, dom
, empty
);
6124 __isl_give isl_pw_multi_aff
*isl_basic_map_lexopt_pw_multi_aff(
6125 __isl_take isl_basic_map
*bmap
, int max
)
6127 isl_basic_set
*dom
= NULL
;
6128 isl_space
*dom_space
;
6132 dom_space
= isl_space_domain(isl_space_copy(bmap
->dim
));
6133 dom
= isl_basic_set_universe(dom_space
);
6134 return isl_basic_map_partial_lexopt_pw_multi_aff(bmap
, dom
, NULL
, max
);
6136 isl_basic_map_free(bmap
);
6140 __isl_give isl_pw_multi_aff
*isl_basic_map_lexmin_pw_multi_aff(
6141 __isl_take isl_basic_map
*bmap
)
6143 return isl_basic_map_lexopt_pw_multi_aff(bmap
, 0);
6147 #define TYPE isl_pw_multi_aff
6149 #define SUFFIX _pw_multi_aff
6151 #define EMPTY isl_pw_multi_aff_empty
6153 #define ADD isl_pw_multi_aff_union_add
6154 #include "isl_map_lexopt_templ.c"
6156 /* Given a map "map", compute the lexicographically minimal
6157 * (or maximal) image element for each domain element in dom,
6158 * in the form of an isl_pw_multi_aff.
6159 * If "empty" is not NULL, then set *empty to those elements in dom that
6160 * do not have an image element.
6162 * We first compute the lexicographically minimal or maximal element
6163 * in the first basic map. This results in a partial solution "res"
6164 * and a subset "todo" of dom that still need to be handled.
6165 * We then consider each of the remaining maps in "map" and successively
6166 * update both "res" and "todo".
6167 * If "empty" is NULL, then the todo sets are not needed and therefore
6168 * also not computed.
6170 static __isl_give isl_pw_multi_aff
*isl_map_partial_lexopt_aligned_pw_multi_aff(
6171 __isl_take isl_map
*map
, __isl_take isl_set
*dom
,
6172 __isl_give isl_set
**empty
, int max
)
6175 isl_pw_multi_aff
*res
;
6181 if (isl_map_plain_is_empty(map
)) {
6186 return isl_pw_multi_aff_from_map(map
);
6189 res
= basic_map_partial_lexopt_pw_multi_aff(
6190 isl_basic_map_copy(map
->p
[0]),
6191 isl_set_copy(dom
), empty
, max
);
6195 for (i
= 1; i
< map
->n
; ++i
) {
6196 isl_pw_multi_aff
*res_i
;
6198 res_i
= basic_map_partial_lexopt_pw_multi_aff(
6199 isl_basic_map_copy(map
->p
[i
]),
6200 isl_set_copy(dom
), empty
, max
);
6203 res
= isl_pw_multi_aff_union_lexmax(res
, res_i
);
6205 res
= isl_pw_multi_aff_union_lexmin(res
, res_i
);
6208 todo
= isl_set_intersect(todo
, *empty
);
6227 #define TYPE isl_map
6231 #define EMPTY isl_map_empty
6233 #define ADD isl_map_union_disjoint
6234 #include "isl_map_lexopt_templ.c"
6236 /* Given a map "map", compute the lexicographically minimal
6237 * (or maximal) image element for each domain element in "dom",
6238 * in the form of an isl_map.
6239 * If "empty" is not NULL, then set *empty to those elements in "dom" that
6240 * do not have an image element.
6242 * If the input consists of more than one disjunct, then first
6243 * compute the desired result in the form of an isl_pw_multi_aff and
6244 * then convert that into an isl_map.
6246 * This function used to have an explicit implementation in terms
6247 * of isl_maps, but it would continually intersect the domains of
6248 * partial results with the complement of the domain of the next
6249 * partial solution, potentially leading to an explosion in the number
6250 * of disjuncts if there are several disjuncts in the input.
6251 * An even earlier implementation of this function would look for
6252 * better results in the domain of the partial result and for extra
6253 * results in the complement of this domain, which would lead to
6254 * even more splintering.
6256 static __isl_give isl_map
*isl_map_partial_lexopt_aligned(
6257 __isl_take isl_map
*map
, __isl_take isl_set
*dom
,
6258 __isl_give isl_set
**empty
, int max
)
6260 struct isl_map
*res
;
6261 isl_pw_multi_aff
*pma
;
6266 if (isl_map_plain_is_empty(map
)) {
6275 res
= basic_map_partial_lexopt(isl_basic_map_copy(map
->p
[0]),
6281 pma
= isl_map_partial_lexopt_aligned_pw_multi_aff(map
, dom
, empty
, max
);
6282 return isl_map_from_pw_multi_aff(pma
);
6291 __isl_give isl_map
*isl_map_partial_lexmax(
6292 __isl_take isl_map
*map
, __isl_take isl_set
*dom
,
6293 __isl_give isl_set
**empty
)
6295 return isl_map_partial_lexopt(map
, dom
, empty
, 1);
6298 __isl_give isl_map
*isl_map_partial_lexmin(
6299 __isl_take isl_map
*map
, __isl_take isl_set
*dom
,
6300 __isl_give isl_set
**empty
)
6302 return isl_map_partial_lexopt(map
, dom
, empty
, 0);
6305 __isl_give isl_set
*isl_set_partial_lexmin(
6306 __isl_take isl_set
*set
, __isl_take isl_set
*dom
,
6307 __isl_give isl_set
**empty
)
6309 return (struct isl_set
*)
6310 isl_map_partial_lexmin((struct isl_map
*)set
,
6314 __isl_give isl_set
*isl_set_partial_lexmax(
6315 __isl_take isl_set
*set
, __isl_take isl_set
*dom
,
6316 __isl_give isl_set
**empty
)
6318 return (struct isl_set
*)
6319 isl_map_partial_lexmax((struct isl_map
*)set
,
6323 /* Compute the lexicographic minimum (or maximum if "max" is set)
6324 * of "bmap" over its domain.
6326 * Since we are not interested in the part of the domain space where
6327 * there is no solution, we initialize the domain to those constraints
6328 * of "bmap" that only involve the parameters and the input dimensions.
6329 * This relieves the parametric programming engine from detecting those
6330 * inequalities and transferring them to the context. More importantly,
6331 * it ensures that those inequalities are transferred first and not
6332 * intermixed with inequalities that actually split the domain.
6334 __isl_give isl_map
*isl_basic_map_lexopt(__isl_take isl_basic_map
*bmap
, int max
)
6338 isl_basic_map
*copy
;
6341 n_div
= isl_basic_map_dim(bmap
, isl_dim_div
);
6342 n_out
= isl_basic_map_dim(bmap
, isl_dim_out
);
6343 copy
= isl_basic_map_copy(bmap
);
6344 copy
= isl_basic_map_drop_constraints_involving_dims(copy
,
6345 isl_dim_div
, 0, n_div
);
6346 copy
= isl_basic_map_drop_constraints_involving_dims(copy
,
6347 isl_dim_out
, 0, n_out
);
6348 dom
= isl_basic_map_domain(copy
);
6349 return isl_basic_map_partial_lexopt(bmap
, dom
, NULL
, max
);
6352 __isl_give isl_map
*isl_basic_map_lexmin(__isl_take isl_basic_map
*bmap
)
6354 return isl_basic_map_lexopt(bmap
, 0);
6357 __isl_give isl_map
*isl_basic_map_lexmax(__isl_take isl_basic_map
*bmap
)
6359 return isl_basic_map_lexopt(bmap
, 1);
6362 __isl_give isl_set
*isl_basic_set_lexmin(__isl_take isl_basic_set
*bset
)
6364 return (isl_set
*)isl_basic_map_lexmin((isl_basic_map
*)bset
);
6367 __isl_give isl_set
*isl_basic_set_lexmax(__isl_take isl_basic_set
*bset
)
6369 return (isl_set
*)isl_basic_map_lexmax((isl_basic_map
*)bset
);
6372 /* Extract the first and only affine expression from list
6373 * and then add it to *pwaff with the given dom.
6374 * This domain is known to be disjoint from other domains
6375 * because of the way isl_basic_map_foreach_lexmax works.
6377 static int update_dim_opt(__isl_take isl_basic_set
*dom
,
6378 __isl_take isl_aff_list
*list
, void *user
)
6380 isl_ctx
*ctx
= isl_basic_set_get_ctx(dom
);
6382 isl_pw_aff
**pwaff
= user
;
6383 isl_pw_aff
*pwaff_i
;
6387 if (isl_aff_list_n_aff(list
) != 1)
6388 isl_die(ctx
, isl_error_internal
,
6389 "expecting single element list", goto error
);
6391 aff
= isl_aff_list_get_aff(list
, 0);
6392 pwaff_i
= isl_pw_aff_alloc(isl_set_from_basic_set(dom
), aff
);
6394 *pwaff
= isl_pw_aff_add_disjoint(*pwaff
, pwaff_i
);
6396 isl_aff_list_free(list
);
6400 isl_basic_set_free(dom
);
6401 isl_aff_list_free(list
);
6405 /* Given a basic map with one output dimension, compute the minimum or
6406 * maximum of that dimension as an isl_pw_aff.
6408 * The isl_pw_aff is constructed by having isl_basic_map_foreach_lexopt
6409 * call update_dim_opt on each leaf of the result.
6411 static __isl_give isl_pw_aff
*basic_map_dim_opt(__isl_keep isl_basic_map
*bmap
,
6414 isl_space
*dim
= isl_basic_map_get_space(bmap
);
6418 dim
= isl_space_from_domain(isl_space_domain(dim
));
6419 dim
= isl_space_add_dims(dim
, isl_dim_out
, 1);
6420 pwaff
= isl_pw_aff_empty(dim
);
6422 r
= isl_basic_map_foreach_lexopt(bmap
, max
, &update_dim_opt
, &pwaff
);
6424 return isl_pw_aff_free(pwaff
);
6429 /* Compute the minimum or maximum of the given output dimension
6430 * as a function of the parameters and the input dimensions,
6431 * but independently of the other output dimensions.
6433 * We first project out the other output dimension and then compute
6434 * the "lexicographic" maximum in each basic map, combining the results
6435 * using isl_pw_aff_union_max.
6437 static __isl_give isl_pw_aff
*map_dim_opt(__isl_take isl_map
*map
, int pos
,
6444 n_out
= isl_map_dim(map
, isl_dim_out
);
6445 map
= isl_map_project_out(map
, isl_dim_out
, pos
+ 1, n_out
- (pos
+ 1));
6446 map
= isl_map_project_out(map
, isl_dim_out
, 0, pos
);
6451 isl_space
*dim
= isl_map_get_space(map
);
6453 return isl_pw_aff_empty(dim
);
6456 pwaff
= basic_map_dim_opt(map
->p
[0], max
);
6457 for (i
= 1; i
< map
->n
; ++i
) {
6458 isl_pw_aff
*pwaff_i
;
6460 pwaff_i
= basic_map_dim_opt(map
->p
[i
], max
);
6461 pwaff
= isl_pw_aff_union_opt(pwaff
, pwaff_i
, max
);
6469 /* Compute the maximum of the given output dimension as a function of the
6470 * parameters and input dimensions, but independently of
6471 * the other output dimensions.
6473 __isl_give isl_pw_aff
*isl_map_dim_max(__isl_take isl_map
*map
, int pos
)
6475 return map_dim_opt(map
, pos
, 1);
6478 /* Compute the minimum or maximum of the given set dimension
6479 * as a function of the parameters,
6480 * but independently of the other set dimensions.
6482 static __isl_give isl_pw_aff
*set_dim_opt(__isl_take isl_set
*set
, int pos
,
6485 return map_dim_opt(set
, pos
, max
);
6488 /* Compute the maximum of the given set dimension as a function of the
6489 * parameters, but independently of the other set dimensions.
6491 __isl_give isl_pw_aff
*isl_set_dim_max(__isl_take isl_set
*set
, int pos
)
6493 return set_dim_opt(set
, pos
, 1);
6496 /* Compute the minimum of the given set dimension as a function of the
6497 * parameters, but independently of the other set dimensions.
6499 __isl_give isl_pw_aff
*isl_set_dim_min(__isl_take isl_set
*set
, int pos
)
6501 return set_dim_opt(set
, pos
, 0);
6504 /* Apply a preimage specified by "mat" on the parameters of "bset".
6505 * bset is assumed to have only parameters and divs.
6507 static struct isl_basic_set
*basic_set_parameter_preimage(
6508 struct isl_basic_set
*bset
, struct isl_mat
*mat
)
6515 bset
->dim
= isl_space_cow(bset
->dim
);
6519 nparam
= isl_basic_set_dim(bset
, isl_dim_param
);
6521 isl_assert(bset
->ctx
, mat
->n_row
== 1 + nparam
, goto error
);
6523 bset
->dim
->nparam
= 0;
6524 bset
->dim
->n_out
= nparam
;
6525 bset
= isl_basic_set_preimage(bset
, mat
);
6527 bset
->dim
->nparam
= bset
->dim
->n_out
;
6528 bset
->dim
->n_out
= 0;
6533 isl_basic_set_free(bset
);
6537 /* Apply a preimage specified by "mat" on the parameters of "set".
6538 * set is assumed to have only parameters and divs.
6540 static __isl_give isl_set
*set_parameter_preimage(__isl_take isl_set
*set
,
6541 __isl_take isl_mat
*mat
)
6549 nparam
= isl_set_dim(set
, isl_dim_param
);
6551 if (mat
->n_row
!= 1 + nparam
)
6552 isl_die(isl_set_get_ctx(set
), isl_error_internal
,
6553 "unexpected number of rows", goto error
);
6555 space
= isl_set_get_space(set
);
6556 space
= isl_space_move_dims(space
, isl_dim_set
, 0,
6557 isl_dim_param
, 0, nparam
);
6558 set
= isl_set_reset_space(set
, space
);
6559 set
= isl_set_preimage(set
, mat
);
6560 nparam
= isl_set_dim(set
, isl_dim_out
);
6561 space
= isl_set_get_space(set
);
6562 space
= isl_space_move_dims(space
, isl_dim_param
, 0,
6563 isl_dim_out
, 0, nparam
);
6564 set
= isl_set_reset_space(set
, space
);
6572 /* Intersect the basic set "bset" with the affine space specified by the
6573 * equalities in "eq".
6575 static struct isl_basic_set
*basic_set_append_equalities(
6576 struct isl_basic_set
*bset
, struct isl_mat
*eq
)
6584 bset
= isl_basic_set_extend_space(bset
, isl_space_copy(bset
->dim
), 0,
6589 len
= 1 + isl_space_dim(bset
->dim
, isl_dim_all
) + bset
->extra
;
6590 for (i
= 0; i
< eq
->n_row
; ++i
) {
6591 k
= isl_basic_set_alloc_equality(bset
);
6594 isl_seq_cpy(bset
->eq
[k
], eq
->row
[i
], eq
->n_col
);
6595 isl_seq_clr(bset
->eq
[k
] + eq
->n_col
, len
- eq
->n_col
);
6599 bset
= isl_basic_set_gauss(bset
, NULL
);
6600 bset
= isl_basic_set_finalize(bset
);
6605 isl_basic_set_free(bset
);
6609 /* Intersect the set "set" with the affine space specified by the
6610 * equalities in "eq".
6612 static struct isl_set
*set_append_equalities(struct isl_set
*set
,
6620 for (i
= 0; i
< set
->n
; ++i
) {
6621 set
->p
[i
] = basic_set_append_equalities(set
->p
[i
],
6634 /* Given a basic set "bset" that only involves parameters and existentially
6635 * quantified variables, return the index of the first equality
6636 * that only involves parameters. If there is no such equality then
6637 * return bset->n_eq.
6639 * This function assumes that isl_basic_set_gauss has been called on "bset".
6641 static int first_parameter_equality(__isl_keep isl_basic_set
*bset
)
6644 unsigned nparam
, n_div
;
6649 nparam
= isl_basic_set_dim(bset
, isl_dim_param
);
6650 n_div
= isl_basic_set_dim(bset
, isl_dim_div
);
6652 for (i
= 0, j
= n_div
- 1; i
< bset
->n_eq
&& j
>= 0; --j
) {
6653 if (!isl_int_is_zero(bset
->eq
[i
][1 + nparam
+ j
]))
6660 /* Compute an explicit representation for the existentially quantified
6661 * variables in "bset" by computing the "minimal value" of the set
6662 * variables. Since there are no set variables, the computation of
6663 * the minimal value essentially computes an explicit representation
6664 * of the non-empty part(s) of "bset".
6666 * The input only involves parameters and existentially quantified variables.
6667 * All equalities among parameters have been removed.
6669 * Since the existentially quantified variables in the result are in general
6670 * going to be different from those in the input, we first replace
6671 * them by the minimal number of variables based on their equalities.
6672 * This should simplify the parametric integer programming.
6674 static __isl_give isl_set
*base_compute_divs(__isl_take isl_basic_set
*bset
)
6676 isl_morph
*morph1
, *morph2
;
6682 if (bset
->n_eq
== 0)
6683 return isl_basic_set_lexmin(bset
);
6685 morph1
= isl_basic_set_parameter_compression(bset
);
6686 bset
= isl_morph_basic_set(isl_morph_copy(morph1
), bset
);
6687 bset
= isl_basic_set_lift(bset
);
6688 morph2
= isl_basic_set_variable_compression(bset
, isl_dim_set
);
6689 bset
= isl_morph_basic_set(morph2
, bset
);
6690 n
= isl_basic_set_dim(bset
, isl_dim_set
);
6691 bset
= isl_basic_set_project_out(bset
, isl_dim_set
, 0, n
);
6693 set
= isl_basic_set_lexmin(bset
);
6695 set
= isl_morph_set(isl_morph_inverse(morph1
), set
);
6700 /* Project the given basic set onto its parameter domain, possibly introducing
6701 * new, explicit, existential variables in the constraints.
6702 * The input has parameters and (possibly implicit) existential variables.
6703 * The output has the same parameters, but only
6704 * explicit existentially quantified variables.
6706 * The actual projection is performed by pip, but pip doesn't seem
6707 * to like equalities very much, so we first remove the equalities
6708 * among the parameters by performing a variable compression on
6709 * the parameters. Afterward, an inverse transformation is performed
6710 * and the equalities among the parameters are inserted back in.
6712 * The variable compression on the parameters may uncover additional
6713 * equalities that were only implicit before. We therefore check
6714 * if there are any new parameter equalities in the result and
6715 * if so recurse. The removal of parameter equalities is required
6716 * for the parameter compression performed by base_compute_divs.
6718 static struct isl_set
*parameter_compute_divs(struct isl_basic_set
*bset
)
6722 struct isl_mat
*T
, *T2
;
6723 struct isl_set
*set
;
6726 bset
= isl_basic_set_cow(bset
);
6730 if (bset
->n_eq
== 0)
6731 return base_compute_divs(bset
);
6733 bset
= isl_basic_set_gauss(bset
, NULL
);
6736 if (isl_basic_set_plain_is_empty(bset
))
6737 return isl_set_from_basic_set(bset
);
6739 i
= first_parameter_equality(bset
);
6740 if (i
== bset
->n_eq
)
6741 return base_compute_divs(bset
);
6743 nparam
= isl_basic_set_dim(bset
, isl_dim_param
);
6744 eq
= isl_mat_sub_alloc6(bset
->ctx
, bset
->eq
, i
, bset
->n_eq
- i
,
6746 eq
= isl_mat_cow(eq
);
6747 T
= isl_mat_variable_compression(isl_mat_copy(eq
), &T2
);
6748 if (T
&& T
->n_col
== 0) {
6752 bset
= isl_basic_set_set_to_empty(bset
);
6753 return isl_set_from_basic_set(bset
);
6755 bset
= basic_set_parameter_preimage(bset
, T
);
6757 i
= first_parameter_equality(bset
);
6760 else if (i
== bset
->n_eq
)
6761 set
= base_compute_divs(bset
);
6763 set
= parameter_compute_divs(bset
);
6764 set
= set_parameter_preimage(set
, T2
);
6765 set
= set_append_equalities(set
, eq
);
6769 /* Insert the divs from "ls" before those of "bmap".
6771 * The number of columns is not changed, which means that the last
6772 * dimensions of "bmap" are being reintepreted as the divs from "ls".
6773 * The caller is responsible for removing the same number of dimensions
6774 * from the space of "bmap".
6776 static __isl_give isl_basic_map
*insert_divs_from_local_space(
6777 __isl_take isl_basic_map
*bmap
, __isl_keep isl_local_space
*ls
)
6783 n_div
= isl_local_space_dim(ls
, isl_dim_div
);
6787 old_n_div
= bmap
->n_div
;
6788 bmap
= insert_div_rows(bmap
, n_div
);
6792 for (i
= 0; i
< n_div
; ++i
) {
6793 isl_seq_cpy(bmap
->div
[i
], ls
->div
->row
[i
], ls
->div
->n_col
);
6794 isl_seq_clr(bmap
->div
[i
] + ls
->div
->n_col
, old_n_div
);
6800 /* Replace the space of "bmap" by the space and divs of "ls".
6802 * If "ls" has any divs, then we simplify the result since we may
6803 * have discovered some additional equalities that could simplify
6804 * the div expressions.
6806 static __isl_give isl_basic_map
*basic_replace_space_by_local_space(
6807 __isl_take isl_basic_map
*bmap
, __isl_take isl_local_space
*ls
)
6811 bmap
= isl_basic_map_cow(bmap
);
6815 n_div
= isl_local_space_dim(ls
, isl_dim_div
);
6816 bmap
= insert_divs_from_local_space(bmap
, ls
);
6820 isl_space_free(bmap
->dim
);
6821 bmap
->dim
= isl_local_space_get_space(ls
);
6825 isl_local_space_free(ls
);
6827 bmap
= isl_basic_map_simplify(bmap
);
6828 bmap
= isl_basic_map_finalize(bmap
);
6831 isl_basic_map_free(bmap
);
6832 isl_local_space_free(ls
);
6836 /* Replace the space of "map" by the space and divs of "ls".
6838 static __isl_give isl_map
*replace_space_by_local_space(__isl_take isl_map
*map
,
6839 __isl_take isl_local_space
*ls
)
6843 map
= isl_map_cow(map
);
6847 for (i
= 0; i
< map
->n
; ++i
) {
6848 map
->p
[i
] = basic_replace_space_by_local_space(map
->p
[i
],
6849 isl_local_space_copy(ls
));
6853 isl_space_free(map
->dim
);
6854 map
->dim
= isl_local_space_get_space(ls
);
6858 isl_local_space_free(ls
);
6861 isl_local_space_free(ls
);
6866 /* Compute an explicit representation for the existentially
6867 * quantified variables for which do not know any explicit representation yet.
6869 * We first sort the existentially quantified variables so that the
6870 * existentially quantified variables for which we already have an explicit
6871 * representation are placed before those for which we do not.
6872 * The input dimensions, the output dimensions and the existentially
6873 * quantified variables for which we already have an explicit
6874 * representation are then turned into parameters.
6875 * compute_divs returns a map with the same parameters and
6876 * no input or output dimensions and the dimension specification
6877 * is reset to that of the input, including the existentially quantified
6878 * variables for which we already had an explicit representation.
6880 static struct isl_map
*compute_divs(struct isl_basic_map
*bmap
)
6882 struct isl_basic_set
*bset
;
6883 struct isl_set
*set
;
6884 struct isl_map
*map
;
6886 isl_local_space
*ls
;
6893 bmap
= isl_basic_map_sort_divs(bmap
);
6894 bmap
= isl_basic_map_cow(bmap
);
6898 n_known
= isl_basic_map_first_unknown_div(bmap
);
6900 return isl_map_from_basic_map(isl_basic_map_free(bmap
));
6902 nparam
= isl_basic_map_dim(bmap
, isl_dim_param
);
6903 n_in
= isl_basic_map_dim(bmap
, isl_dim_in
);
6904 n_out
= isl_basic_map_dim(bmap
, isl_dim_out
);
6905 dim
= isl_space_set_alloc(bmap
->ctx
,
6906 nparam
+ n_in
+ n_out
+ n_known
, 0);
6910 ls
= isl_basic_map_get_local_space(bmap
);
6911 ls
= isl_local_space_drop_dims(ls
, isl_dim_div
,
6912 n_known
, bmap
->n_div
- n_known
);
6914 for (i
= n_known
; i
< bmap
->n_div
; ++i
)
6915 swap_div(bmap
, i
- n_known
, i
);
6916 bmap
->n_div
-= n_known
;
6917 bmap
->extra
-= n_known
;
6919 bmap
= isl_basic_map_reset_space(bmap
, dim
);
6920 bset
= (struct isl_basic_set
*)bmap
;
6922 set
= parameter_compute_divs(bset
);
6923 map
= (struct isl_map
*)set
;
6924 map
= replace_space_by_local_space(map
, ls
);
6928 isl_basic_map_free(bmap
);
6932 /* Remove the explicit representation of local variable "div",
6935 __isl_give isl_basic_map
*isl_basic_map_mark_div_unknown(
6936 __isl_take isl_basic_map
*bmap
, int div
)
6940 known
= isl_basic_map_div_is_known(bmap
, div
);
6942 return isl_basic_map_free(bmap
);
6946 bmap
= isl_basic_map_cow(bmap
);
6949 isl_int_set_si(bmap
->div
[div
][0], 0);
6953 /* Does local variable "div" of "bmap" have an explicit representation?
6955 isl_bool
isl_basic_map_div_is_known(__isl_keep isl_basic_map
*bmap
, int div
)
6958 return isl_bool_error
;
6959 if (div
< 0 || div
>= isl_basic_map_dim(bmap
, isl_dim_div
))
6960 isl_die(isl_basic_map_get_ctx(bmap
), isl_error_invalid
,
6961 "position out of bounds", return isl_bool_error
);
6962 return !isl_int_is_zero(bmap
->div
[div
][0]);
6965 /* Return the position of the first local variable that does not
6966 * have an explicit representation.
6967 * Return the total number of local variables if they all have
6968 * an explicit representation.
6969 * Return -1 on error.
6971 int isl_basic_map_first_unknown_div(__isl_keep isl_basic_map
*bmap
)
6978 for (i
= 0; i
< bmap
->n_div
; ++i
) {
6979 if (!isl_basic_map_div_is_known(bmap
, i
))
6985 /* Does "bmap" have an explicit representation for all local variables?
6987 isl_bool
isl_basic_map_divs_known(__isl_keep isl_basic_map
*bmap
)
6991 n
= isl_basic_map_dim(bmap
, isl_dim_div
);
6992 first
= isl_basic_map_first_unknown_div(bmap
);
6994 return isl_bool_error
;
6998 /* Do all basic maps in "map" have an explicit representation
6999 * for all local variables?
7001 isl_bool
isl_map_divs_known(__isl_keep isl_map
*map
)
7006 return isl_bool_error
;
7008 for (i
= 0; i
< map
->n
; ++i
) {
7009 int known
= isl_basic_map_divs_known(map
->p
[i
]);
7014 return isl_bool_true
;
7017 /* If bmap contains any unknown divs, then compute explicit
7018 * expressions for them. However, this computation may be
7019 * quite expensive, so first try to remove divs that aren't
7022 struct isl_map
*isl_basic_map_compute_divs(struct isl_basic_map
*bmap
)
7025 struct isl_map
*map
;
7027 known
= isl_basic_map_divs_known(bmap
);
7031 return isl_map_from_basic_map(bmap
);
7033 bmap
= isl_basic_map_drop_redundant_divs(bmap
);
7035 known
= isl_basic_map_divs_known(bmap
);
7039 return isl_map_from_basic_map(bmap
);
7041 map
= compute_divs(bmap
);
7044 isl_basic_map_free(bmap
);
7048 struct isl_map
*isl_map_compute_divs(struct isl_map
*map
)
7052 struct isl_map
*res
;
7059 known
= isl_map_divs_known(map
);
7067 res
= isl_basic_map_compute_divs(isl_basic_map_copy(map
->p
[0]));
7068 for (i
= 1 ; i
< map
->n
; ++i
) {
7070 r2
= isl_basic_map_compute_divs(isl_basic_map_copy(map
->p
[i
]));
7071 if (ISL_F_ISSET(map
, ISL_MAP_DISJOINT
))
7072 res
= isl_map_union_disjoint(res
, r2
);
7074 res
= isl_map_union(res
, r2
);
7081 struct isl_set
*isl_basic_set_compute_divs(struct isl_basic_set
*bset
)
7083 return (struct isl_set
*)
7084 isl_basic_map_compute_divs((struct isl_basic_map
*)bset
);
7087 struct isl_set
*isl_set_compute_divs(struct isl_set
*set
)
7089 return (struct isl_set
*)
7090 isl_map_compute_divs((struct isl_map
*)set
);
7093 struct isl_set
*isl_map_domain(struct isl_map
*map
)
7096 struct isl_set
*set
;
7101 map
= isl_map_cow(map
);
7105 set
= (struct isl_set
*)map
;
7106 set
->dim
= isl_space_domain(set
->dim
);
7109 for (i
= 0; i
< map
->n
; ++i
) {
7110 set
->p
[i
] = isl_basic_map_domain(map
->p
[i
]);
7114 ISL_F_CLR(set
, ISL_MAP_DISJOINT
);
7115 ISL_F_CLR(set
, ISL_SET_NORMALIZED
);
7122 /* Return the union of "map1" and "map2", where we assume for now that
7123 * "map1" and "map2" are disjoint. Note that the basic maps inside
7124 * "map1" or "map2" may not be disjoint from each other.
7125 * Also note that this function is also called from isl_map_union,
7126 * which takes care of handling the situation where "map1" and "map2"
7127 * may not be disjoint.
7129 * If one of the inputs is empty, we can simply return the other input.
7130 * Similarly, if one of the inputs is universal, then it is equal to the union.
7132 static __isl_give isl_map
*map_union_disjoint(__isl_take isl_map
*map1
,
7133 __isl_take isl_map
*map2
)
7137 struct isl_map
*map
= NULL
;
7143 if (!isl_space_is_equal(map1
->dim
, map2
->dim
))
7144 isl_die(isl_map_get_ctx(map1
), isl_error_invalid
,
7145 "spaces don't match", goto error
);
7156 is_universe
= isl_map_plain_is_universe(map1
);
7157 if (is_universe
< 0)
7164 is_universe
= isl_map_plain_is_universe(map2
);
7165 if (is_universe
< 0)
7172 if (ISL_F_ISSET(map1
, ISL_MAP_DISJOINT
) &&
7173 ISL_F_ISSET(map2
, ISL_MAP_DISJOINT
))
7174 ISL_FL_SET(flags
, ISL_MAP_DISJOINT
);
7176 map
= isl_map_alloc_space(isl_space_copy(map1
->dim
),
7177 map1
->n
+ map2
->n
, flags
);
7180 for (i
= 0; i
< map1
->n
; ++i
) {
7181 map
= isl_map_add_basic_map(map
,
7182 isl_basic_map_copy(map1
->p
[i
]));
7186 for (i
= 0; i
< map2
->n
; ++i
) {
7187 map
= isl_map_add_basic_map(map
,
7188 isl_basic_map_copy(map2
->p
[i
]));
7202 /* Return the union of "map1" and "map2", where "map1" and "map2" are
7203 * guaranteed to be disjoint by the caller.
7205 * Note that this functions is called from within isl_map_make_disjoint,
7206 * so we have to be careful not to touch the constraints of the inputs
7209 __isl_give isl_map
*isl_map_union_disjoint(__isl_take isl_map
*map1
,
7210 __isl_take isl_map
*map2
)
7212 return isl_map_align_params_map_map_and(map1
, map2
, &map_union_disjoint
);
7215 /* Return the union of "map1" and "map2", where "map1" and "map2" may
7216 * not be disjoint. The parameters are assumed to have been aligned.
7218 * We currently simply call map_union_disjoint, the internal operation
7219 * of which does not really depend on the inputs being disjoint.
7220 * If the result contains more than one basic map, then we clear
7221 * the disjoint flag since the result may contain basic maps from
7222 * both inputs and these are not guaranteed to be disjoint.
7224 * As a special case, if "map1" and "map2" are obviously equal,
7225 * then we simply return "map1".
7227 static __isl_give isl_map
*map_union_aligned(__isl_take isl_map
*map1
,
7228 __isl_take isl_map
*map2
)
7235 equal
= isl_map_plain_is_equal(map1
, map2
);
7243 map1
= map_union_disjoint(map1
, map2
);
7247 ISL_F_CLR(map1
, ISL_MAP_DISJOINT
);
7255 /* Return the union of "map1" and "map2", where "map1" and "map2" may
7258 __isl_give isl_map
*isl_map_union(__isl_take isl_map
*map1
,
7259 __isl_take isl_map
*map2
)
7261 return isl_map_align_params_map_map_and(map1
, map2
, &map_union_aligned
);
7264 struct isl_set
*isl_set_union_disjoint(
7265 struct isl_set
*set1
, struct isl_set
*set2
)
7267 return (struct isl_set
*)
7268 isl_map_union_disjoint(
7269 (struct isl_map
*)set1
, (struct isl_map
*)set2
);
7272 struct isl_set
*isl_set_union(struct isl_set
*set1
, struct isl_set
*set2
)
7274 return (struct isl_set
*)
7275 isl_map_union((struct isl_map
*)set1
, (struct isl_map
*)set2
);
7278 /* Apply "fn" to pairs of elements from "map" and "set" and collect
7281 * "map" and "set" are assumed to be compatible and non-NULL.
7283 static __isl_give isl_map
*map_intersect_set(__isl_take isl_map
*map
,
7284 __isl_take isl_set
*set
,
7285 __isl_give isl_basic_map
*fn(__isl_take isl_basic_map
*bmap
,
7286 __isl_take isl_basic_set
*bset
))
7289 struct isl_map
*result
;
7292 if (isl_set_plain_is_universe(set
)) {
7297 if (ISL_F_ISSET(map
, ISL_MAP_DISJOINT
) &&
7298 ISL_F_ISSET(set
, ISL_MAP_DISJOINT
))
7299 ISL_FL_SET(flags
, ISL_MAP_DISJOINT
);
7301 result
= isl_map_alloc_space(isl_space_copy(map
->dim
),
7302 map
->n
* set
->n
, flags
);
7303 for (i
= 0; result
&& i
< map
->n
; ++i
)
7304 for (j
= 0; j
< set
->n
; ++j
) {
7305 result
= isl_map_add_basic_map(result
,
7306 fn(isl_basic_map_copy(map
->p
[i
]),
7307 isl_basic_set_copy(set
->p
[j
])));
7317 static __isl_give isl_map
*map_intersect_range(__isl_take isl_map
*map
,
7318 __isl_take isl_set
*set
)
7323 if (!isl_map_compatible_range(map
, set
))
7324 isl_die(set
->ctx
, isl_error_invalid
,
7325 "incompatible spaces", goto error
);
7327 return map_intersect_set(map
, set
, &isl_basic_map_intersect_range
);
7334 __isl_give isl_map
*isl_map_intersect_range(__isl_take isl_map
*map
,
7335 __isl_take isl_set
*set
)
7337 return isl_map_align_params_map_map_and(map
, set
, &map_intersect_range
);
7340 static __isl_give isl_map
*map_intersect_domain(__isl_take isl_map
*map
,
7341 __isl_take isl_set
*set
)
7346 if (!isl_map_compatible_domain(map
, set
))
7347 isl_die(set
->ctx
, isl_error_invalid
,
7348 "incompatible spaces", goto error
);
7350 return map_intersect_set(map
, set
, &isl_basic_map_intersect_domain
);
7357 __isl_give isl_map
*isl_map_intersect_domain(__isl_take isl_map
*map
,
7358 __isl_take isl_set
*set
)
7360 return isl_map_align_params_map_map_and(map
, set
,
7361 &map_intersect_domain
);
7364 static __isl_give isl_map
*map_apply_domain(__isl_take isl_map
*map1
,
7365 __isl_take isl_map
*map2
)
7369 map1
= isl_map_reverse(map1
);
7370 map1
= isl_map_apply_range(map1
, map2
);
7371 return isl_map_reverse(map1
);
7378 __isl_give isl_map
*isl_map_apply_domain(__isl_take isl_map
*map1
,
7379 __isl_take isl_map
*map2
)
7381 return isl_map_align_params_map_map_and(map1
, map2
, &map_apply_domain
);
7384 static __isl_give isl_map
*map_apply_range(__isl_take isl_map
*map1
,
7385 __isl_take isl_map
*map2
)
7387 isl_space
*dim_result
;
7388 struct isl_map
*result
;
7394 dim_result
= isl_space_join(isl_space_copy(map1
->dim
),
7395 isl_space_copy(map2
->dim
));
7397 result
= isl_map_alloc_space(dim_result
, map1
->n
* map2
->n
, 0);
7400 for (i
= 0; i
< map1
->n
; ++i
)
7401 for (j
= 0; j
< map2
->n
; ++j
) {
7402 result
= isl_map_add_basic_map(result
,
7403 isl_basic_map_apply_range(
7404 isl_basic_map_copy(map1
->p
[i
]),
7405 isl_basic_map_copy(map2
->p
[j
])));
7411 if (result
&& result
->n
<= 1)
7412 ISL_F_SET(result
, ISL_MAP_DISJOINT
);
7420 __isl_give isl_map
*isl_map_apply_range(__isl_take isl_map
*map1
,
7421 __isl_take isl_map
*map2
)
7423 return isl_map_align_params_map_map_and(map1
, map2
, &map_apply_range
);
7427 * returns range - domain
7429 struct isl_basic_set
*isl_basic_map_deltas(struct isl_basic_map
*bmap
)
7431 isl_space
*target_space
;
7432 struct isl_basic_set
*bset
;
7439 isl_assert(bmap
->ctx
, isl_space_tuple_is_equal(bmap
->dim
, isl_dim_in
,
7440 bmap
->dim
, isl_dim_out
),
7442 target_space
= isl_space_domain(isl_basic_map_get_space(bmap
));
7443 dim
= isl_basic_map_n_in(bmap
);
7444 nparam
= isl_basic_map_n_param(bmap
);
7445 bmap
= isl_basic_map_from_range(isl_basic_map_wrap(bmap
));
7446 bmap
= isl_basic_map_add_dims(bmap
, isl_dim_in
, dim
);
7447 bmap
= isl_basic_map_extend_constraints(bmap
, dim
, 0);
7448 for (i
= 0; i
< dim
; ++i
) {
7449 int j
= isl_basic_map_alloc_equality(bmap
);
7451 bmap
= isl_basic_map_free(bmap
);
7454 isl_seq_clr(bmap
->eq
[j
], 1 + isl_basic_map_total_dim(bmap
));
7455 isl_int_set_si(bmap
->eq
[j
][1+nparam
+i
], 1);
7456 isl_int_set_si(bmap
->eq
[j
][1+nparam
+dim
+i
], 1);
7457 isl_int_set_si(bmap
->eq
[j
][1+nparam
+2*dim
+i
], -1);
7459 bset
= isl_basic_map_domain(bmap
);
7460 bset
= isl_basic_set_reset_space(bset
, target_space
);
7463 isl_basic_map_free(bmap
);
7468 * returns range - domain
7470 __isl_give isl_set
*isl_map_deltas(__isl_take isl_map
*map
)
7474 struct isl_set
*result
;
7479 isl_assert(map
->ctx
, isl_space_tuple_is_equal(map
->dim
, isl_dim_in
,
7480 map
->dim
, isl_dim_out
),
7482 dim
= isl_map_get_space(map
);
7483 dim
= isl_space_domain(dim
);
7484 result
= isl_set_alloc_space(dim
, map
->n
, 0);
7487 for (i
= 0; i
< map
->n
; ++i
)
7488 result
= isl_set_add_basic_set(result
,
7489 isl_basic_map_deltas(isl_basic_map_copy(map
->p
[i
])));
7498 * returns [domain -> range] -> range - domain
7500 __isl_give isl_basic_map
*isl_basic_map_deltas_map(
7501 __isl_take isl_basic_map
*bmap
)
7505 isl_basic_map
*domain
;
7509 if (!isl_space_tuple_is_equal(bmap
->dim
, isl_dim_in
,
7510 bmap
->dim
, isl_dim_out
))
7511 isl_die(bmap
->ctx
, isl_error_invalid
,
7512 "domain and range don't match", goto error
);
7514 nparam
= isl_basic_map_dim(bmap
, isl_dim_param
);
7515 n
= isl_basic_map_dim(bmap
, isl_dim_in
);
7517 dim
= isl_space_from_range(isl_space_domain(isl_basic_map_get_space(bmap
)));
7518 domain
= isl_basic_map_universe(dim
);
7520 bmap
= isl_basic_map_from_domain(isl_basic_map_wrap(bmap
));
7521 bmap
= isl_basic_map_apply_range(bmap
, domain
);
7522 bmap
= isl_basic_map_extend_constraints(bmap
, n
, 0);
7524 total
= isl_basic_map_total_dim(bmap
);
7526 for (i
= 0; i
< n
; ++i
) {
7527 k
= isl_basic_map_alloc_equality(bmap
);
7530 isl_seq_clr(bmap
->eq
[k
], 1 + total
);
7531 isl_int_set_si(bmap
->eq
[k
][1 + nparam
+ i
], 1);
7532 isl_int_set_si(bmap
->eq
[k
][1 + nparam
+ n
+ i
], -1);
7533 isl_int_set_si(bmap
->eq
[k
][1 + nparam
+ n
+ n
+ i
], 1);
7536 bmap
= isl_basic_map_gauss(bmap
, NULL
);
7537 return isl_basic_map_finalize(bmap
);
7539 isl_basic_map_free(bmap
);
7544 * returns [domain -> range] -> range - domain
7546 __isl_give isl_map
*isl_map_deltas_map(__isl_take isl_map
*map
)
7549 isl_space
*domain_dim
;
7554 if (!isl_space_tuple_is_equal(map
->dim
, isl_dim_in
,
7555 map
->dim
, isl_dim_out
))
7556 isl_die(map
->ctx
, isl_error_invalid
,
7557 "domain and range don't match", goto error
);
7559 map
= isl_map_cow(map
);
7563 domain_dim
= isl_space_from_range(isl_space_domain(isl_map_get_space(map
)));
7564 map
->dim
= isl_space_from_domain(isl_space_wrap(map
->dim
));
7565 map
->dim
= isl_space_join(map
->dim
, domain_dim
);
7568 for (i
= 0; i
< map
->n
; ++i
) {
7569 map
->p
[i
] = isl_basic_map_deltas_map(map
->p
[i
]);
7573 ISL_F_CLR(map
, ISL_MAP_NORMALIZED
);
7580 static __isl_give isl_basic_map
*basic_map_identity(__isl_take isl_space
*dims
)
7582 struct isl_basic_map
*bmap
;
7590 nparam
= dims
->nparam
;
7592 bmap
= isl_basic_map_alloc_space(dims
, 0, dim
, 0);
7596 for (i
= 0; i
< dim
; ++i
) {
7597 int j
= isl_basic_map_alloc_equality(bmap
);
7600 isl_seq_clr(bmap
->eq
[j
], 1 + isl_basic_map_total_dim(bmap
));
7601 isl_int_set_si(bmap
->eq
[j
][1+nparam
+i
], 1);
7602 isl_int_set_si(bmap
->eq
[j
][1+nparam
+dim
+i
], -1);
7604 return isl_basic_map_finalize(bmap
);
7606 isl_basic_map_free(bmap
);
7610 __isl_give isl_basic_map
*isl_basic_map_identity(__isl_take isl_space
*dim
)
7614 if (dim
->n_in
!= dim
->n_out
)
7615 isl_die(dim
->ctx
, isl_error_invalid
,
7616 "number of input and output dimensions needs to be "
7617 "the same", goto error
);
7618 return basic_map_identity(dim
);
7620 isl_space_free(dim
);
7624 __isl_give isl_map
*isl_map_identity(__isl_take isl_space
*dim
)
7626 return isl_map_from_basic_map(isl_basic_map_identity(dim
));
7629 __isl_give isl_map
*isl_set_identity(__isl_take isl_set
*set
)
7631 isl_space
*dim
= isl_set_get_space(set
);
7633 id
= isl_map_identity(isl_space_map_from_set(dim
));
7634 return isl_map_intersect_range(id
, set
);
7637 /* Construct a basic set with all set dimensions having only non-negative
7640 __isl_give isl_basic_set
*isl_basic_set_positive_orthant(
7641 __isl_take isl_space
*space
)
7646 struct isl_basic_set
*bset
;
7650 nparam
= space
->nparam
;
7652 bset
= isl_basic_set_alloc_space(space
, 0, 0, dim
);
7655 for (i
= 0; i
< dim
; ++i
) {
7656 int k
= isl_basic_set_alloc_inequality(bset
);
7659 isl_seq_clr(bset
->ineq
[k
], 1 + isl_basic_set_total_dim(bset
));
7660 isl_int_set_si(bset
->ineq
[k
][1 + nparam
+ i
], 1);
7664 isl_basic_set_free(bset
);
7668 /* Construct the half-space x_pos >= 0.
7670 static __isl_give isl_basic_set
*nonneg_halfspace(__isl_take isl_space
*dim
,
7674 isl_basic_set
*nonneg
;
7676 nonneg
= isl_basic_set_alloc_space(dim
, 0, 0, 1);
7677 k
= isl_basic_set_alloc_inequality(nonneg
);
7680 isl_seq_clr(nonneg
->ineq
[k
], 1 + isl_basic_set_total_dim(nonneg
));
7681 isl_int_set_si(nonneg
->ineq
[k
][pos
], 1);
7683 return isl_basic_set_finalize(nonneg
);
7685 isl_basic_set_free(nonneg
);
7689 /* Construct the half-space x_pos <= -1.
7691 static __isl_give isl_basic_set
*neg_halfspace(__isl_take isl_space
*dim
, int pos
)
7696 neg
= isl_basic_set_alloc_space(dim
, 0, 0, 1);
7697 k
= isl_basic_set_alloc_inequality(neg
);
7700 isl_seq_clr(neg
->ineq
[k
], 1 + isl_basic_set_total_dim(neg
));
7701 isl_int_set_si(neg
->ineq
[k
][0], -1);
7702 isl_int_set_si(neg
->ineq
[k
][pos
], -1);
7704 return isl_basic_set_finalize(neg
);
7706 isl_basic_set_free(neg
);
7710 __isl_give isl_set
*isl_set_split_dims(__isl_take isl_set
*set
,
7711 enum isl_dim_type type
, unsigned first
, unsigned n
)
7715 isl_basic_set
*nonneg
;
7723 isl_assert(set
->ctx
, first
+ n
<= isl_set_dim(set
, type
), goto error
);
7725 offset
= pos(set
->dim
, type
);
7726 for (i
= 0; i
< n
; ++i
) {
7727 nonneg
= nonneg_halfspace(isl_set_get_space(set
),
7728 offset
+ first
+ i
);
7729 neg
= neg_halfspace(isl_set_get_space(set
), offset
+ first
+ i
);
7731 set
= isl_set_intersect(set
, isl_basic_set_union(nonneg
, neg
));
7740 static int foreach_orthant(__isl_take isl_set
*set
, int *signs
, int first
,
7741 int len
, int (*fn
)(__isl_take isl_set
*orthant
, int *signs
, void *user
),
7748 if (isl_set_plain_is_empty(set
)) {
7753 return fn(set
, signs
, user
);
7756 half
= isl_set_from_basic_set(nonneg_halfspace(isl_set_get_space(set
),
7758 half
= isl_set_intersect(half
, isl_set_copy(set
));
7759 if (foreach_orthant(half
, signs
, first
+ 1, len
, fn
, user
) < 0)
7763 half
= isl_set_from_basic_set(neg_halfspace(isl_set_get_space(set
),
7765 half
= isl_set_intersect(half
, set
);
7766 return foreach_orthant(half
, signs
, first
+ 1, len
, fn
, user
);
7772 /* Call "fn" on the intersections of "set" with each of the orthants
7773 * (except for obviously empty intersections). The orthant is identified
7774 * by the signs array, with each entry having value 1 or -1 according
7775 * to the sign of the corresponding variable.
7777 int isl_set_foreach_orthant(__isl_keep isl_set
*set
,
7778 int (*fn
)(__isl_take isl_set
*orthant
, int *signs
, void *user
),
7788 if (isl_set_plain_is_empty(set
))
7791 nparam
= isl_set_dim(set
, isl_dim_param
);
7792 nvar
= isl_set_dim(set
, isl_dim_set
);
7794 signs
= isl_alloc_array(set
->ctx
, int, nparam
+ nvar
);
7796 r
= foreach_orthant(isl_set_copy(set
), signs
, 0, nparam
+ nvar
,
7804 isl_bool
isl_set_is_equal(__isl_keep isl_set
*set1
, __isl_keep isl_set
*set2
)
7806 return isl_map_is_equal((struct isl_map
*)set1
, (struct isl_map
*)set2
);
7809 isl_bool
isl_basic_map_is_subset(__isl_keep isl_basic_map
*bmap1
,
7810 __isl_keep isl_basic_map
*bmap2
)
7813 struct isl_map
*map1
;
7814 struct isl_map
*map2
;
7816 if (!bmap1
|| !bmap2
)
7817 return isl_bool_error
;
7819 map1
= isl_map_from_basic_map(isl_basic_map_copy(bmap1
));
7820 map2
= isl_map_from_basic_map(isl_basic_map_copy(bmap2
));
7822 is_subset
= isl_map_is_subset(map1
, map2
);
7830 isl_bool
isl_basic_set_is_subset(__isl_keep isl_basic_set
*bset1
,
7831 __isl_keep isl_basic_set
*bset2
)
7833 return isl_basic_map_is_subset(bset1
, bset2
);
7836 isl_bool
isl_basic_map_is_equal(__isl_keep isl_basic_map
*bmap1
,
7837 __isl_keep isl_basic_map
*bmap2
)
7841 if (!bmap1
|| !bmap2
)
7842 return isl_bool_error
;
7843 is_subset
= isl_basic_map_is_subset(bmap1
, bmap2
);
7844 if (is_subset
!= isl_bool_true
)
7846 is_subset
= isl_basic_map_is_subset(bmap2
, bmap1
);
7850 isl_bool
isl_basic_set_is_equal(__isl_keep isl_basic_set
*bset1
,
7851 __isl_keep isl_basic_set
*bset2
)
7853 return isl_basic_map_is_equal(
7854 (struct isl_basic_map
*)bset1
, (struct isl_basic_map
*)bset2
);
7857 isl_bool
isl_map_is_empty(__isl_keep isl_map
*map
)
7863 return isl_bool_error
;
7864 for (i
= 0; i
< map
->n
; ++i
) {
7865 is_empty
= isl_basic_map_is_empty(map
->p
[i
]);
7867 return isl_bool_error
;
7869 return isl_bool_false
;
7871 return isl_bool_true
;
7874 isl_bool
isl_map_plain_is_empty(__isl_keep isl_map
*map
)
7876 return map
? map
->n
== 0 : isl_bool_error
;
7879 isl_bool
isl_set_plain_is_empty(__isl_keep isl_set
*set
)
7881 return set
? set
->n
== 0 : isl_bool_error
;
7884 isl_bool
isl_set_is_empty(__isl_keep isl_set
*set
)
7886 return isl_map_is_empty((struct isl_map
*)set
);
7889 int isl_map_has_equal_space(__isl_keep isl_map
*map1
, __isl_keep isl_map
*map2
)
7894 return isl_space_is_equal(map1
->dim
, map2
->dim
);
7897 int isl_set_has_equal_space(__isl_keep isl_set
*set1
, __isl_keep isl_set
*set2
)
7902 return isl_space_is_equal(set1
->dim
, set2
->dim
);
7905 static isl_bool
map_is_equal(__isl_keep isl_map
*map1
, __isl_keep isl_map
*map2
)
7910 return isl_bool_error
;
7911 is_subset
= isl_map_is_subset(map1
, map2
);
7912 if (is_subset
!= isl_bool_true
)
7914 is_subset
= isl_map_is_subset(map2
, map1
);
7918 isl_bool
isl_map_is_equal(__isl_keep isl_map
*map1
, __isl_keep isl_map
*map2
)
7920 return isl_map_align_params_map_map_and_test(map1
, map2
, &map_is_equal
);
7923 isl_bool
isl_basic_map_is_strict_subset(
7924 struct isl_basic_map
*bmap1
, struct isl_basic_map
*bmap2
)
7928 if (!bmap1
|| !bmap2
)
7929 return isl_bool_error
;
7930 is_subset
= isl_basic_map_is_subset(bmap1
, bmap2
);
7931 if (is_subset
!= isl_bool_true
)
7933 is_subset
= isl_basic_map_is_subset(bmap2
, bmap1
);
7934 if (is_subset
== isl_bool_error
)
7939 isl_bool
isl_map_is_strict_subset(__isl_keep isl_map
*map1
,
7940 __isl_keep isl_map
*map2
)
7945 return isl_bool_error
;
7946 is_subset
= isl_map_is_subset(map1
, map2
);
7947 if (is_subset
!= isl_bool_true
)
7949 is_subset
= isl_map_is_subset(map2
, map1
);
7950 if (is_subset
== isl_bool_error
)
7955 isl_bool
isl_set_is_strict_subset(__isl_keep isl_set
*set1
,
7956 __isl_keep isl_set
*set2
)
7958 return isl_map_is_strict_subset((isl_map
*)set1
, (isl_map
*)set2
);
7961 /* Is "bmap" obviously equal to the universe with the same space?
7963 * That is, does it not have any constraints?
7965 isl_bool
isl_basic_map_plain_is_universe(__isl_keep isl_basic_map
*bmap
)
7968 return isl_bool_error
;
7969 return bmap
->n_eq
== 0 && bmap
->n_ineq
== 0;
7972 /* Is "bset" obviously equal to the universe with the same space?
7974 isl_bool
isl_basic_set_plain_is_universe(__isl_keep isl_basic_set
*bset
)
7976 return isl_basic_map_plain_is_universe(bset
);
7979 /* If "c" does not involve any existentially quantified variables,
7980 * then set *univ to false and abort
7982 static isl_stat
involves_divs(__isl_take isl_constraint
*c
, void *user
)
7984 isl_bool
*univ
= user
;
7987 n
= isl_constraint_dim(c
, isl_dim_div
);
7988 *univ
= isl_constraint_involves_dims(c
, isl_dim_div
, 0, n
);
7989 isl_constraint_free(c
);
7990 if (*univ
< 0 || !*univ
)
7991 return isl_stat_error
;
7995 /* Is "bmap" equal to the universe with the same space?
7997 * First check if it is obviously equal to the universe.
7998 * If not and if there are any constraints not involving
7999 * existentially quantified variables, then it is certainly
8000 * not equal to the universe.
8001 * Otherwise, check if the universe is a subset of "bmap".
8003 isl_bool
isl_basic_map_is_universe(__isl_keep isl_basic_map
*bmap
)
8006 isl_basic_map
*test
;
8008 univ
= isl_basic_map_plain_is_universe(bmap
);
8009 if (univ
< 0 || univ
)
8011 if (isl_basic_map_dim(bmap
, isl_dim_div
) == 0)
8012 return isl_bool_false
;
8013 univ
= isl_bool_true
;
8014 if (isl_basic_map_foreach_constraint(bmap
, &involves_divs
, &univ
) < 0 &&
8016 return isl_bool_error
;
8017 if (univ
< 0 || !univ
)
8019 test
= isl_basic_map_universe(isl_basic_map_get_space(bmap
));
8020 univ
= isl_basic_map_is_subset(test
, bmap
);
8021 isl_basic_map_free(test
);
8025 /* Is "bset" equal to the universe with the same space?
8027 isl_bool
isl_basic_set_is_universe(__isl_keep isl_basic_set
*bset
)
8029 return isl_basic_map_is_universe(bset
);
8032 isl_bool
isl_map_plain_is_universe(__isl_keep isl_map
*map
)
8037 return isl_bool_error
;
8039 for (i
= 0; i
< map
->n
; ++i
) {
8040 isl_bool r
= isl_basic_map_plain_is_universe(map
->p
[i
]);
8045 return isl_bool_false
;
8048 isl_bool
isl_set_plain_is_universe(__isl_keep isl_set
*set
)
8050 return isl_map_plain_is_universe((isl_map
*) set
);
8053 isl_bool
isl_basic_map_is_empty(__isl_keep isl_basic_map
*bmap
)
8055 struct isl_basic_set
*bset
= NULL
;
8056 struct isl_vec
*sample
= NULL
;
8057 isl_bool empty
, non_empty
;
8060 return isl_bool_error
;
8062 if (ISL_F_ISSET(bmap
, ISL_BASIC_MAP_EMPTY
))
8063 return isl_bool_true
;
8065 if (isl_basic_map_plain_is_universe(bmap
))
8066 return isl_bool_false
;
8068 if (ISL_F_ISSET(bmap
, ISL_BASIC_MAP_RATIONAL
)) {
8069 struct isl_basic_map
*copy
= isl_basic_map_copy(bmap
);
8070 copy
= isl_basic_map_remove_redundancies(copy
);
8071 empty
= isl_basic_map_plain_is_empty(copy
);
8072 isl_basic_map_free(copy
);
8076 non_empty
= isl_basic_map_plain_is_non_empty(bmap
);
8078 return isl_bool_error
;
8080 return isl_bool_false
;
8081 isl_vec_free(bmap
->sample
);
8082 bmap
->sample
= NULL
;
8083 bset
= isl_basic_map_underlying_set(isl_basic_map_copy(bmap
));
8085 return isl_bool_error
;
8086 sample
= isl_basic_set_sample_vec(bset
);
8088 return isl_bool_error
;
8089 empty
= sample
->size
== 0;
8090 isl_vec_free(bmap
->sample
);
8091 bmap
->sample
= sample
;
8093 ISL_F_SET(bmap
, ISL_BASIC_MAP_EMPTY
);
8098 isl_bool
isl_basic_map_plain_is_empty(__isl_keep isl_basic_map
*bmap
)
8101 return isl_bool_error
;
8102 return ISL_F_ISSET(bmap
, ISL_BASIC_MAP_EMPTY
);
8105 isl_bool
isl_basic_set_plain_is_empty(__isl_keep isl_basic_set
*bset
)
8108 return isl_bool_error
;
8109 return ISL_F_ISSET(bset
, ISL_BASIC_SET_EMPTY
);
8112 /* Is "bmap" known to be non-empty?
8114 * That is, is the cached sample still valid?
8116 isl_bool
isl_basic_map_plain_is_non_empty(__isl_keep isl_basic_map
*bmap
)
8121 return isl_bool_error
;
8123 return isl_bool_false
;
8124 total
= 1 + isl_basic_map_total_dim(bmap
);
8125 if (bmap
->sample
->size
!= total
)
8126 return isl_bool_false
;
8127 return isl_basic_map_contains(bmap
, bmap
->sample
);
8130 isl_bool
isl_basic_set_is_empty(__isl_keep isl_basic_set
*bset
)
8132 return isl_basic_map_is_empty((struct isl_basic_map
*)bset
);
8135 struct isl_map
*isl_basic_map_union(
8136 struct isl_basic_map
*bmap1
, struct isl_basic_map
*bmap2
)
8138 struct isl_map
*map
;
8139 if (!bmap1
|| !bmap2
)
8142 isl_assert(bmap1
->ctx
, isl_space_is_equal(bmap1
->dim
, bmap2
->dim
), goto error
);
8144 map
= isl_map_alloc_space(isl_space_copy(bmap1
->dim
), 2, 0);
8147 map
= isl_map_add_basic_map(map
, bmap1
);
8148 map
= isl_map_add_basic_map(map
, bmap2
);
8151 isl_basic_map_free(bmap1
);
8152 isl_basic_map_free(bmap2
);
8156 struct isl_set
*isl_basic_set_union(
8157 struct isl_basic_set
*bset1
, struct isl_basic_set
*bset2
)
8159 return (struct isl_set
*)isl_basic_map_union(
8160 (struct isl_basic_map
*)bset1
,
8161 (struct isl_basic_map
*)bset2
);
8164 /* Order divs such that any div only depends on previous divs */
8165 struct isl_basic_map
*isl_basic_map_order_divs(struct isl_basic_map
*bmap
)
8173 off
= isl_space_dim(bmap
->dim
, isl_dim_all
);
8175 for (i
= 0; i
< bmap
->n_div
; ++i
) {
8177 if (isl_int_is_zero(bmap
->div
[i
][0]))
8179 pos
= isl_seq_first_non_zero(bmap
->div
[i
]+1+1+off
+i
,
8184 isl_die(isl_basic_map_get_ctx(bmap
), isl_error_internal
,
8185 "integer division depends on itself",
8186 return isl_basic_map_free(bmap
));
8187 isl_basic_map_swap_div(bmap
, i
, i
+ pos
);
8193 struct isl_basic_set
*isl_basic_set_order_divs(struct isl_basic_set
*bset
)
8195 return (struct isl_basic_set
*)
8196 isl_basic_map_order_divs((struct isl_basic_map
*)bset
);
8199 __isl_give isl_map
*isl_map_order_divs(__isl_take isl_map
*map
)
8206 for (i
= 0; i
< map
->n
; ++i
) {
8207 map
->p
[i
] = isl_basic_map_order_divs(map
->p
[i
]);
8218 /* Apply the expansion computed by isl_merge_divs.
8219 * The expansion itself is given by "exp" while the resulting
8220 * list of divs is given by "div".
8222 * Move the integer divisions of "bset" into the right position
8223 * according to "exp" and then introduce the additional integer
8224 * divisions, adding div constraints.
8225 * The moving should be done first to avoid moving coefficients
8226 * in the definitions of the extra integer divisions.
8228 __isl_give isl_basic_set
*isl_basic_set_expand_divs(
8229 __isl_take isl_basic_set
*bset
, __isl_take isl_mat
*div
, int *exp
)
8234 bset
= isl_basic_set_cow(bset
);
8238 if (div
->n_row
< bset
->n_div
)
8239 isl_die(isl_mat_get_ctx(div
), isl_error_invalid
,
8240 "not an expansion", goto error
);
8242 n_div
= bset
->n_div
;
8243 bset
= isl_basic_map_extend_space(bset
, isl_space_copy(bset
->dim
),
8244 div
->n_row
- n_div
, 0,
8245 2 * (div
->n_row
- n_div
));
8247 for (i
= n_div
; i
< div
->n_row
; ++i
)
8248 if (isl_basic_set_alloc_div(bset
) < 0)
8251 for (j
= n_div
- 1; j
>= 0; --j
) {
8254 isl_basic_map_swap_div(bset
, j
, exp
[j
]);
8257 for (i
= 0; i
< div
->n_row
; ++i
) {
8258 if (j
< n_div
&& exp
[j
] == i
) {
8261 isl_seq_cpy(bset
->div
[i
], div
->row
[i
], div
->n_col
);
8262 if (!isl_basic_map_div_is_known(bset
, i
))
8264 if (isl_basic_map_add_div_constraints(bset
, i
) < 0)
8272 isl_basic_set_free(bset
);
8277 /* Look for a div in dst that corresponds to the div "div" in src.
8278 * The divs before "div" in src and dst are assumed to be the same.
8280 * Returns -1 if no corresponding div was found and the position
8281 * of the corresponding div in dst otherwise.
8283 static int find_div(struct isl_basic_map
*dst
,
8284 struct isl_basic_map
*src
, unsigned div
)
8288 unsigned total
= isl_space_dim(src
->dim
, isl_dim_all
);
8290 isl_assert(dst
->ctx
, div
<= dst
->n_div
, return -1);
8291 for (i
= div
; i
< dst
->n_div
; ++i
)
8292 if (isl_seq_eq(dst
->div
[i
], src
->div
[div
], 1+1+total
+div
) &&
8293 isl_seq_first_non_zero(dst
->div
[i
]+1+1+total
+div
,
8294 dst
->n_div
- div
) == -1)
8299 /* Align the divs of "dst" to those of "src", adding divs from "src"
8300 * if needed. That is, make sure that the first src->n_div divs
8301 * of the result are equal to those of src.
8303 * The result is not finalized as by design it will have redundant
8304 * divs if any divs from "src" were copied.
8306 __isl_give isl_basic_map
*isl_basic_map_align_divs(
8307 __isl_take isl_basic_map
*dst
, __isl_keep isl_basic_map
*src
)
8310 int known
, extended
;
8314 return isl_basic_map_free(dst
);
8316 if (src
->n_div
== 0)
8319 known
= isl_basic_map_divs_known(src
);
8321 return isl_basic_map_free(dst
);
8323 isl_die(isl_basic_map_get_ctx(src
), isl_error_invalid
,
8324 "some src divs are unknown",
8325 return isl_basic_map_free(dst
));
8327 src
= isl_basic_map_order_divs(src
);
8330 total
= isl_space_dim(src
->dim
, isl_dim_all
);
8331 for (i
= 0; i
< src
->n_div
; ++i
) {
8332 int j
= find_div(dst
, src
, i
);
8335 int extra
= src
->n_div
- i
;
8336 dst
= isl_basic_map_cow(dst
);
8339 dst
= isl_basic_map_extend_space(dst
,
8340 isl_space_copy(dst
->dim
),
8341 extra
, 0, 2 * extra
);
8344 j
= isl_basic_map_alloc_div(dst
);
8346 return isl_basic_map_free(dst
);
8347 isl_seq_cpy(dst
->div
[j
], src
->div
[i
], 1+1+total
+i
);
8348 isl_seq_clr(dst
->div
[j
]+1+1+total
+i
, dst
->n_div
- i
);
8349 if (isl_basic_map_add_div_constraints(dst
, j
) < 0)
8350 return isl_basic_map_free(dst
);
8353 isl_basic_map_swap_div(dst
, i
, j
);
8358 struct isl_basic_set
*isl_basic_set_align_divs(
8359 struct isl_basic_set
*dst
, struct isl_basic_set
*src
)
8361 return (struct isl_basic_set
*)isl_basic_map_align_divs(
8362 (struct isl_basic_map
*)dst
, (struct isl_basic_map
*)src
);
8365 struct isl_map
*isl_map_align_divs(struct isl_map
*map
)
8373 map
= isl_map_compute_divs(map
);
8374 map
= isl_map_cow(map
);
8378 for (i
= 1; i
< map
->n
; ++i
)
8379 map
->p
[0] = isl_basic_map_align_divs(map
->p
[0], map
->p
[i
]);
8380 for (i
= 1; i
< map
->n
; ++i
) {
8381 map
->p
[i
] = isl_basic_map_align_divs(map
->p
[i
], map
->p
[0]);
8383 return isl_map_free(map
);
8386 ISL_F_CLR(map
, ISL_MAP_NORMALIZED
);
8390 struct isl_set
*isl_set_align_divs(struct isl_set
*set
)
8392 return (struct isl_set
*)isl_map_align_divs((struct isl_map
*)set
);
8395 /* Align the divs of the basic maps in "map" to those
8396 * of the basic maps in "list", as well as to the other basic maps in "map".
8397 * The elements in "list" are assumed to have known divs.
8399 __isl_give isl_map
*isl_map_align_divs_to_basic_map_list(
8400 __isl_take isl_map
*map
, __isl_keep isl_basic_map_list
*list
)
8404 map
= isl_map_compute_divs(map
);
8405 map
= isl_map_cow(map
);
8407 return isl_map_free(map
);
8411 n
= isl_basic_map_list_n_basic_map(list
);
8412 for (i
= 0; i
< n
; ++i
) {
8413 isl_basic_map
*bmap
;
8415 bmap
= isl_basic_map_list_get_basic_map(list
, i
);
8416 map
->p
[0] = isl_basic_map_align_divs(map
->p
[0], bmap
);
8417 isl_basic_map_free(bmap
);
8420 return isl_map_free(map
);
8422 return isl_map_align_divs(map
);
8425 /* Align the divs of each element of "list" to those of "bmap".
8426 * Both "bmap" and the elements of "list" are assumed to have known divs.
8428 __isl_give isl_basic_map_list
*isl_basic_map_list_align_divs_to_basic_map(
8429 __isl_take isl_basic_map_list
*list
, __isl_keep isl_basic_map
*bmap
)
8434 return isl_basic_map_list_free(list
);
8436 n
= isl_basic_map_list_n_basic_map(list
);
8437 for (i
= 0; i
< n
; ++i
) {
8438 isl_basic_map
*bmap_i
;
8440 bmap_i
= isl_basic_map_list_get_basic_map(list
, i
);
8441 bmap_i
= isl_basic_map_align_divs(bmap_i
, bmap
);
8442 list
= isl_basic_map_list_set_basic_map(list
, i
, bmap_i
);
8448 static __isl_give isl_set
*set_apply( __isl_take isl_set
*set
,
8449 __isl_take isl_map
*map
)
8453 isl_assert(set
->ctx
, isl_map_compatible_domain(map
, set
), goto error
);
8454 map
= isl_map_intersect_domain(map
, set
);
8455 set
= isl_map_range(map
);
8463 __isl_give isl_set
*isl_set_apply( __isl_take isl_set
*set
,
8464 __isl_take isl_map
*map
)
8466 return isl_map_align_params_map_map_and(set
, map
, &set_apply
);
8469 /* There is no need to cow as removing empty parts doesn't change
8470 * the meaning of the set.
8472 struct isl_map
*isl_map_remove_empty_parts(struct isl_map
*map
)
8479 for (i
= map
->n
- 1; i
>= 0; --i
)
8480 remove_if_empty(map
, i
);
8485 struct isl_set
*isl_set_remove_empty_parts(struct isl_set
*set
)
8487 return (struct isl_set
*)
8488 isl_map_remove_empty_parts((struct isl_map
*)set
);
8491 struct isl_basic_map
*isl_map_copy_basic_map(struct isl_map
*map
)
8493 struct isl_basic_map
*bmap
;
8494 if (!map
|| map
->n
== 0)
8496 bmap
= map
->p
[map
->n
-1];
8497 isl_assert(map
->ctx
, ISL_F_ISSET(bmap
, ISL_BASIC_SET_FINAL
), return NULL
);
8498 return isl_basic_map_copy(bmap
);
8501 struct isl_basic_set
*isl_set_copy_basic_set(struct isl_set
*set
)
8503 return (struct isl_basic_set
*)
8504 isl_map_copy_basic_map((struct isl_map
*)set
);
8507 __isl_give isl_map
*isl_map_drop_basic_map(__isl_take isl_map
*map
,
8508 __isl_keep isl_basic_map
*bmap
)
8514 for (i
= map
->n
-1; i
>= 0; --i
) {
8515 if (map
->p
[i
] != bmap
)
8517 map
= isl_map_cow(map
);
8520 isl_basic_map_free(map
->p
[i
]);
8521 if (i
!= map
->n
-1) {
8522 ISL_F_CLR(map
, ISL_SET_NORMALIZED
);
8523 map
->p
[i
] = map
->p
[map
->n
-1];
8534 struct isl_set
*isl_set_drop_basic_set(struct isl_set
*set
,
8535 struct isl_basic_set
*bset
)
8537 return (struct isl_set
*)isl_map_drop_basic_map((struct isl_map
*)set
,
8538 (struct isl_basic_map
*)bset
);
8541 /* Given two basic sets bset1 and bset2, compute the maximal difference
8542 * between the values of dimension pos in bset1 and those in bset2
8543 * for any common value of the parameters and dimensions preceding pos.
8545 static enum isl_lp_result
basic_set_maximal_difference_at(
8546 __isl_keep isl_basic_set
*bset1
, __isl_keep isl_basic_set
*bset2
,
8547 int pos
, isl_int
*opt
)
8550 struct isl_basic_map
*bmap1
= NULL
;
8551 struct isl_basic_map
*bmap2
= NULL
;
8552 struct isl_ctx
*ctx
;
8553 struct isl_vec
*obj
;
8556 unsigned dim1
, dim2
;
8557 enum isl_lp_result res
;
8559 if (!bset1
|| !bset2
)
8560 return isl_lp_error
;
8562 nparam
= isl_basic_set_n_param(bset1
);
8563 dim1
= isl_basic_set_n_dim(bset1
);
8564 dim2
= isl_basic_set_n_dim(bset2
);
8565 dims
= isl_space_alloc(bset1
->ctx
, nparam
, pos
, dim1
- pos
);
8566 bmap1
= isl_basic_map_from_basic_set(isl_basic_set_copy(bset1
), dims
);
8567 dims
= isl_space_alloc(bset2
->ctx
, nparam
, pos
, dim2
- pos
);
8568 bmap2
= isl_basic_map_from_basic_set(isl_basic_set_copy(bset2
), dims
);
8569 if (!bmap1
|| !bmap2
)
8571 bmap1
= isl_basic_map_cow(bmap1
);
8572 bmap1
= isl_basic_map_extend(bmap1
, nparam
,
8573 pos
, (dim1
- pos
) + (dim2
- pos
),
8574 bmap2
->n_div
, bmap2
->n_eq
, bmap2
->n_ineq
);
8575 bmap1
= add_constraints(bmap1
, bmap2
, 0, dim1
- pos
);
8578 total
= isl_basic_map_total_dim(bmap1
);
8580 obj
= isl_vec_alloc(ctx
, 1 + total
);
8583 isl_seq_clr(obj
->block
.data
, 1 + total
);
8584 isl_int_set_si(obj
->block
.data
[1+nparam
+pos
], 1);
8585 isl_int_set_si(obj
->block
.data
[1+nparam
+pos
+(dim1
-pos
)], -1);
8586 res
= isl_basic_map_solve_lp(bmap1
, 1, obj
->block
.data
, ctx
->one
,
8588 isl_basic_map_free(bmap1
);
8592 isl_basic_map_free(bmap2
);
8594 isl_basic_map_free(bmap1
);
8595 return isl_lp_error
;
8598 /* Given two _disjoint_ basic sets bset1 and bset2, check whether
8599 * for any common value of the parameters and dimensions preceding pos
8600 * in both basic sets, the values of dimension pos in bset1 are
8601 * smaller or larger than those in bset2.
8604 * 1 if bset1 follows bset2
8605 * -1 if bset1 precedes bset2
8606 * 0 if bset1 and bset2 are incomparable
8607 * -2 if some error occurred.
8609 int isl_basic_set_compare_at(struct isl_basic_set
*bset1
,
8610 struct isl_basic_set
*bset2
, int pos
)
8613 enum isl_lp_result res
;
8618 res
= basic_set_maximal_difference_at(bset1
, bset2
, pos
, &opt
);
8620 if (res
== isl_lp_empty
)
8622 else if ((res
== isl_lp_ok
&& isl_int_is_pos(opt
)) ||
8623 res
== isl_lp_unbounded
)
8625 else if (res
== isl_lp_ok
&& isl_int_is_neg(opt
))
8634 /* Given two basic sets bset1 and bset2, check whether
8635 * for any common value of the parameters and dimensions preceding pos
8636 * there is a value of dimension pos in bset1 that is larger
8637 * than a value of the same dimension in bset2.
8640 * 1 if there exists such a pair
8641 * 0 if there is no such pair, but there is a pair of equal values
8643 * -2 if some error occurred.
8645 int isl_basic_set_follows_at(__isl_keep isl_basic_set
*bset1
,
8646 __isl_keep isl_basic_set
*bset2
, int pos
)
8649 enum isl_lp_result res
;
8654 res
= basic_set_maximal_difference_at(bset1
, bset2
, pos
, &opt
);
8656 if (res
== isl_lp_empty
)
8658 else if ((res
== isl_lp_ok
&& isl_int_is_pos(opt
)) ||
8659 res
== isl_lp_unbounded
)
8661 else if (res
== isl_lp_ok
&& isl_int_is_neg(opt
))
8663 else if (res
== isl_lp_ok
)
8672 /* Given two sets set1 and set2, check whether
8673 * for any common value of the parameters and dimensions preceding pos
8674 * there is a value of dimension pos in set1 that is larger
8675 * than a value of the same dimension in set2.
8678 * 1 if there exists such a pair
8679 * 0 if there is no such pair, but there is a pair of equal values
8681 * -2 if some error occurred.
8683 int isl_set_follows_at(__isl_keep isl_set
*set1
,
8684 __isl_keep isl_set
*set2
, int pos
)
8692 for (i
= 0; i
< set1
->n
; ++i
)
8693 for (j
= 0; j
< set2
->n
; ++j
) {
8695 f
= isl_basic_set_follows_at(set1
->p
[i
], set2
->p
[j
], pos
);
8696 if (f
== 1 || f
== -2)
8705 static int isl_basic_map_plain_has_fixed_var(__isl_keep isl_basic_map
*bmap
,
8706 unsigned pos
, isl_int
*val
)
8714 total
= isl_basic_map_total_dim(bmap
);
8715 for (i
= 0, d
= total
-1; i
< bmap
->n_eq
&& d
+1 > pos
; ++i
) {
8716 for (; d
+1 > pos
; --d
)
8717 if (!isl_int_is_zero(bmap
->eq
[i
][1+d
]))
8721 if (isl_seq_first_non_zero(bmap
->eq
[i
]+1, d
) != -1)
8723 if (isl_seq_first_non_zero(bmap
->eq
[i
]+1+d
+1, total
-d
-1) != -1)
8725 if (!isl_int_is_one(bmap
->eq
[i
][1+d
]))
8728 isl_int_neg(*val
, bmap
->eq
[i
][0]);
8734 static int isl_map_plain_has_fixed_var(__isl_keep isl_map
*map
,
8735 unsigned pos
, isl_int
*val
)
8747 return isl_basic_map_plain_has_fixed_var(map
->p
[0], pos
, val
);
8750 fixed
= isl_basic_map_plain_has_fixed_var(map
->p
[0], pos
, &v
);
8751 for (i
= 1; fixed
== 1 && i
< map
->n
; ++i
) {
8752 fixed
= isl_basic_map_plain_has_fixed_var(map
->p
[i
], pos
, &tmp
);
8753 if (fixed
== 1 && isl_int_ne(tmp
, v
))
8757 isl_int_set(*val
, v
);
8763 static int isl_basic_set_plain_has_fixed_var(__isl_keep isl_basic_set
*bset
,
8764 unsigned pos
, isl_int
*val
)
8766 return isl_basic_map_plain_has_fixed_var((struct isl_basic_map
*)bset
,
8770 static int isl_set_plain_has_fixed_var(__isl_keep isl_set
*set
, unsigned pos
,
8773 return isl_map_plain_has_fixed_var((struct isl_map
*)set
, pos
, val
);
8776 int isl_basic_map_plain_is_fixed(__isl_keep isl_basic_map
*bmap
,
8777 enum isl_dim_type type
, unsigned pos
, isl_int
*val
)
8779 if (pos
>= isl_basic_map_dim(bmap
, type
))
8781 return isl_basic_map_plain_has_fixed_var(bmap
,
8782 isl_basic_map_offset(bmap
, type
) - 1 + pos
, val
);
8785 /* If "bmap" obviously lies on a hyperplane where the given dimension
8786 * has a fixed value, then return that value.
8787 * Otherwise return NaN.
8789 __isl_give isl_val
*isl_basic_map_plain_get_val_if_fixed(
8790 __isl_keep isl_basic_map
*bmap
,
8791 enum isl_dim_type type
, unsigned pos
)
8799 ctx
= isl_basic_map_get_ctx(bmap
);
8800 v
= isl_val_alloc(ctx
);
8803 fixed
= isl_basic_map_plain_is_fixed(bmap
, type
, pos
, &v
->n
);
8805 return isl_val_free(v
);
8807 isl_int_set_si(v
->d
, 1);
8811 return isl_val_nan(ctx
);
8814 int isl_map_plain_is_fixed(__isl_keep isl_map
*map
,
8815 enum isl_dim_type type
, unsigned pos
, isl_int
*val
)
8817 if (pos
>= isl_map_dim(map
, type
))
8819 return isl_map_plain_has_fixed_var(map
,
8820 map_offset(map
, type
) - 1 + pos
, val
);
8823 /* If "map" obviously lies on a hyperplane where the given dimension
8824 * has a fixed value, then return that value.
8825 * Otherwise return NaN.
8827 __isl_give isl_val
*isl_map_plain_get_val_if_fixed(__isl_keep isl_map
*map
,
8828 enum isl_dim_type type
, unsigned pos
)
8836 ctx
= isl_map_get_ctx(map
);
8837 v
= isl_val_alloc(ctx
);
8840 fixed
= isl_map_plain_is_fixed(map
, type
, pos
, &v
->n
);
8842 return isl_val_free(v
);
8844 isl_int_set_si(v
->d
, 1);
8848 return isl_val_nan(ctx
);
8851 /* If "set" obviously lies on a hyperplane where the given dimension
8852 * has a fixed value, then return that value.
8853 * Otherwise return NaN.
8855 __isl_give isl_val
*isl_set_plain_get_val_if_fixed(__isl_keep isl_set
*set
,
8856 enum isl_dim_type type
, unsigned pos
)
8858 return isl_map_plain_get_val_if_fixed(set
, type
, pos
);
8861 int isl_set_plain_is_fixed(__isl_keep isl_set
*set
,
8862 enum isl_dim_type type
, unsigned pos
, isl_int
*val
)
8864 return isl_map_plain_is_fixed(set
, type
, pos
, val
);
8867 /* Check if dimension dim has fixed value and if so and if val is not NULL,
8868 * then return this fixed value in *val.
8870 int isl_basic_set_plain_dim_is_fixed(__isl_keep isl_basic_set
*bset
,
8871 unsigned dim
, isl_int
*val
)
8873 return isl_basic_set_plain_has_fixed_var(bset
,
8874 isl_basic_set_n_param(bset
) + dim
, val
);
8877 /* Check if dimension dim has fixed value and if so and if val is not NULL,
8878 * then return this fixed value in *val.
8880 int isl_set_plain_dim_is_fixed(__isl_keep isl_set
*set
,
8881 unsigned dim
, isl_int
*val
)
8883 return isl_set_plain_has_fixed_var(set
, isl_set_n_param(set
) + dim
, val
);
8886 /* Check if input variable in has fixed value and if so and if val is not NULL,
8887 * then return this fixed value in *val.
8889 int isl_map_plain_input_is_fixed(__isl_keep isl_map
*map
,
8890 unsigned in
, isl_int
*val
)
8892 return isl_map_plain_has_fixed_var(map
, isl_map_n_param(map
) + in
, val
);
8895 /* Check if dimension dim has an (obvious) fixed lower bound and if so
8896 * and if val is not NULL, then return this lower bound in *val.
8898 int isl_basic_set_plain_dim_has_fixed_lower_bound(
8899 __isl_keep isl_basic_set
*bset
, unsigned dim
, isl_int
*val
)
8901 int i
, i_eq
= -1, i_ineq
= -1;
8908 total
= isl_basic_set_total_dim(bset
);
8909 nparam
= isl_basic_set_n_param(bset
);
8910 for (i
= 0; i
< bset
->n_eq
; ++i
) {
8911 if (isl_int_is_zero(bset
->eq
[i
][1+nparam
+dim
]))
8917 for (i
= 0; i
< bset
->n_ineq
; ++i
) {
8918 if (!isl_int_is_pos(bset
->ineq
[i
][1+nparam
+dim
]))
8920 if (i_eq
!= -1 || i_ineq
!= -1)
8924 if (i_eq
== -1 && i_ineq
== -1)
8926 c
= i_eq
!= -1 ? bset
->eq
[i_eq
] : bset
->ineq
[i_ineq
];
8927 /* The coefficient should always be one due to normalization. */
8928 if (!isl_int_is_one(c
[1+nparam
+dim
]))
8930 if (isl_seq_first_non_zero(c
+1, nparam
+dim
) != -1)
8932 if (isl_seq_first_non_zero(c
+1+nparam
+dim
+1,
8933 total
- nparam
- dim
- 1) != -1)
8936 isl_int_neg(*val
, c
[0]);
8940 int isl_set_plain_dim_has_fixed_lower_bound(__isl_keep isl_set
*set
,
8941 unsigned dim
, isl_int
*val
)
8953 return isl_basic_set_plain_dim_has_fixed_lower_bound(set
->p
[0],
8957 fixed
= isl_basic_set_plain_dim_has_fixed_lower_bound(set
->p
[0],
8959 for (i
= 1; fixed
== 1 && i
< set
->n
; ++i
) {
8960 fixed
= isl_basic_set_plain_dim_has_fixed_lower_bound(set
->p
[i
],
8962 if (fixed
== 1 && isl_int_ne(tmp
, v
))
8966 isl_int_set(*val
, v
);
8972 /* Return -1 if the constraint "c1" should be sorted before "c2"
8973 * and 1 if it should be sorted after "c2".
8974 * Return 0 if the two constraints are the same (up to the constant term).
8976 * In particular, if a constraint involves later variables than another
8977 * then it is sorted after this other constraint.
8978 * uset_gist depends on constraints without existentially quantified
8979 * variables sorting first.
8981 * For constraints that have the same latest variable, those
8982 * with the same coefficient for this latest variable (first in absolute value
8983 * and then in actual value) are grouped together.
8984 * This is useful for detecting pairs of constraints that can
8985 * be chained in their printed representation.
8987 * Finally, within a group, constraints are sorted according to
8988 * their coefficients (excluding the constant term).
8990 static int sort_constraint_cmp(const void *p1
, const void *p2
, void *arg
)
8992 isl_int
**c1
= (isl_int
**) p1
;
8993 isl_int
**c2
= (isl_int
**) p2
;
8995 unsigned size
= *(unsigned *) arg
;
8998 l1
= isl_seq_last_non_zero(*c1
+ 1, size
);
8999 l2
= isl_seq_last_non_zero(*c2
+ 1, size
);
9004 cmp
= isl_int_abs_cmp((*c1
)[1 + l1
], (*c2
)[1 + l1
]);
9007 cmp
= isl_int_cmp((*c1
)[1 + l1
], (*c2
)[1 + l1
]);
9011 return isl_seq_cmp(*c1
+ 1, *c2
+ 1, size
);
9014 /* Return -1 if the constraint "c1" of "bmap" is sorted before "c2"
9015 * by isl_basic_map_sort_constraints, 1 if it is sorted after "c2"
9016 * and 0 if the two constraints are the same (up to the constant term).
9018 int isl_basic_map_constraint_cmp(__isl_keep isl_basic_map
*bmap
,
9019 isl_int
*c1
, isl_int
*c2
)
9025 total
= isl_basic_map_total_dim(bmap
);
9026 return sort_constraint_cmp(&c1
, &c2
, &total
);
9029 __isl_give isl_basic_map
*isl_basic_map_sort_constraints(
9030 __isl_take isl_basic_map
*bmap
)
9036 if (bmap
->n_ineq
== 0)
9038 if (ISL_F_ISSET(bmap
, ISL_BASIC_MAP_NORMALIZED
))
9040 total
= isl_basic_map_total_dim(bmap
);
9041 if (isl_sort(bmap
->ineq
, bmap
->n_ineq
, sizeof(isl_int
*),
9042 &sort_constraint_cmp
, &total
) < 0)
9043 return isl_basic_map_free(bmap
);
9047 __isl_give isl_basic_set
*isl_basic_set_sort_constraints(
9048 __isl_take isl_basic_set
*bset
)
9050 return (struct isl_basic_set
*)isl_basic_map_sort_constraints(
9051 (struct isl_basic_map
*)bset
);
9054 struct isl_basic_map
*isl_basic_map_normalize(struct isl_basic_map
*bmap
)
9058 if (ISL_F_ISSET(bmap
, ISL_BASIC_MAP_NORMALIZED
))
9060 bmap
= isl_basic_map_remove_redundancies(bmap
);
9061 bmap
= isl_basic_map_sort_constraints(bmap
);
9063 ISL_F_SET(bmap
, ISL_BASIC_MAP_NORMALIZED
);
9067 struct isl_basic_set
*isl_basic_set_normalize(struct isl_basic_set
*bset
)
9069 return (struct isl_basic_set
*)isl_basic_map_normalize(
9070 (struct isl_basic_map
*)bset
);
9073 int isl_basic_map_plain_cmp(const __isl_keep isl_basic_map
*bmap1
,
9074 const __isl_keep isl_basic_map
*bmap2
)
9079 if (!bmap1
|| !bmap2
)
9084 if (ISL_F_ISSET(bmap1
, ISL_BASIC_MAP_RATIONAL
) !=
9085 ISL_F_ISSET(bmap2
, ISL_BASIC_MAP_RATIONAL
))
9086 return ISL_F_ISSET(bmap1
, ISL_BASIC_MAP_RATIONAL
) ? -1 : 1;
9087 if (isl_basic_map_n_param(bmap1
) != isl_basic_map_n_param(bmap2
))
9088 return isl_basic_map_n_param(bmap1
) - isl_basic_map_n_param(bmap2
);
9089 if (isl_basic_map_n_in(bmap1
) != isl_basic_map_n_in(bmap2
))
9090 return isl_basic_map_n_out(bmap1
) - isl_basic_map_n_out(bmap2
);
9091 if (isl_basic_map_n_out(bmap1
) != isl_basic_map_n_out(bmap2
))
9092 return isl_basic_map_n_out(bmap1
) - isl_basic_map_n_out(bmap2
);
9093 if (ISL_F_ISSET(bmap1
, ISL_BASIC_MAP_EMPTY
) &&
9094 ISL_F_ISSET(bmap2
, ISL_BASIC_MAP_EMPTY
))
9096 if (ISL_F_ISSET(bmap1
, ISL_BASIC_MAP_EMPTY
))
9098 if (ISL_F_ISSET(bmap2
, ISL_BASIC_MAP_EMPTY
))
9100 if (bmap1
->n_eq
!= bmap2
->n_eq
)
9101 return bmap1
->n_eq
- bmap2
->n_eq
;
9102 if (bmap1
->n_ineq
!= bmap2
->n_ineq
)
9103 return bmap1
->n_ineq
- bmap2
->n_ineq
;
9104 if (bmap1
->n_div
!= bmap2
->n_div
)
9105 return bmap1
->n_div
- bmap2
->n_div
;
9106 total
= isl_basic_map_total_dim(bmap1
);
9107 for (i
= 0; i
< bmap1
->n_eq
; ++i
) {
9108 cmp
= isl_seq_cmp(bmap1
->eq
[i
], bmap2
->eq
[i
], 1+total
);
9112 for (i
= 0; i
< bmap1
->n_ineq
; ++i
) {
9113 cmp
= isl_seq_cmp(bmap1
->ineq
[i
], bmap2
->ineq
[i
], 1+total
);
9117 for (i
= 0; i
< bmap1
->n_div
; ++i
) {
9118 cmp
= isl_seq_cmp(bmap1
->div
[i
], bmap2
->div
[i
], 1+1+total
);
9125 int isl_basic_set_plain_cmp(const __isl_keep isl_basic_set
*bset1
,
9126 const __isl_keep isl_basic_set
*bset2
)
9128 return isl_basic_map_plain_cmp(bset1
, bset2
);
9131 int isl_set_plain_cmp(__isl_keep isl_set
*set1
, __isl_keep isl_set
*set2
)
9137 if (set1
->n
!= set2
->n
)
9138 return set1
->n
- set2
->n
;
9140 for (i
= 0; i
< set1
->n
; ++i
) {
9141 cmp
= isl_basic_set_plain_cmp(set1
->p
[i
], set2
->p
[i
]);
9149 isl_bool
isl_basic_map_plain_is_equal(__isl_keep isl_basic_map
*bmap1
,
9150 __isl_keep isl_basic_map
*bmap2
)
9152 if (!bmap1
|| !bmap2
)
9153 return isl_bool_error
;
9154 return isl_basic_map_plain_cmp(bmap1
, bmap2
) == 0;
9157 isl_bool
isl_basic_set_plain_is_equal(__isl_keep isl_basic_set
*bset1
,
9158 __isl_keep isl_basic_set
*bset2
)
9160 return isl_basic_map_plain_is_equal((isl_basic_map
*)bset1
,
9161 (isl_basic_map
*)bset2
);
9164 static int qsort_bmap_cmp(const void *p1
, const void *p2
)
9166 const struct isl_basic_map
*bmap1
= *(const struct isl_basic_map
**)p1
;
9167 const struct isl_basic_map
*bmap2
= *(const struct isl_basic_map
**)p2
;
9169 return isl_basic_map_plain_cmp(bmap1
, bmap2
);
9172 /* Sort the basic maps of "map" and remove duplicate basic maps.
9174 * While removing basic maps, we make sure that the basic maps remain
9175 * sorted because isl_map_normalize expects the basic maps of the result
9178 static __isl_give isl_map
*sort_and_remove_duplicates(__isl_take isl_map
*map
)
9182 map
= isl_map_remove_empty_parts(map
);
9185 qsort(map
->p
, map
->n
, sizeof(struct isl_basic_map
*), qsort_bmap_cmp
);
9186 for (i
= map
->n
- 1; i
>= 1; --i
) {
9187 if (!isl_basic_map_plain_is_equal(map
->p
[i
- 1], map
->p
[i
]))
9189 isl_basic_map_free(map
->p
[i
-1]);
9190 for (j
= i
; j
< map
->n
; ++j
)
9191 map
->p
[j
- 1] = map
->p
[j
];
9198 /* Remove obvious duplicates among the basic maps of "map".
9200 * Unlike isl_map_normalize, this function does not remove redundant
9201 * constraints and only removes duplicates that have exactly the same
9202 * constraints in the input. It does sort the constraints and
9203 * the basic maps to ease the detection of duplicates.
9205 * If "map" has already been normalized or if the basic maps are
9206 * disjoint, then there can be no duplicates.
9208 __isl_give isl_map
*isl_map_remove_obvious_duplicates(__isl_take isl_map
*map
)
9211 isl_basic_map
*bmap
;
9217 if (ISL_F_ISSET(map
, ISL_MAP_NORMALIZED
| ISL_MAP_DISJOINT
))
9219 for (i
= 0; i
< map
->n
; ++i
) {
9220 bmap
= isl_basic_map_copy(map
->p
[i
]);
9221 bmap
= isl_basic_map_sort_constraints(bmap
);
9223 return isl_map_free(map
);
9224 isl_basic_map_free(map
->p
[i
]);
9228 map
= sort_and_remove_duplicates(map
);
9232 /* We normalize in place, but if anything goes wrong we need
9233 * to return NULL, so we need to make sure we don't change the
9234 * meaning of any possible other copies of map.
9236 __isl_give isl_map
*isl_map_normalize(__isl_take isl_map
*map
)
9239 struct isl_basic_map
*bmap
;
9243 if (ISL_F_ISSET(map
, ISL_MAP_NORMALIZED
))
9245 for (i
= 0; i
< map
->n
; ++i
) {
9246 bmap
= isl_basic_map_normalize(isl_basic_map_copy(map
->p
[i
]));
9249 isl_basic_map_free(map
->p
[i
]);
9253 map
= sort_and_remove_duplicates(map
);
9255 ISL_F_SET(map
, ISL_MAP_NORMALIZED
);
9262 struct isl_set
*isl_set_normalize(struct isl_set
*set
)
9264 return (struct isl_set
*)isl_map_normalize((struct isl_map
*)set
);
9267 isl_bool
isl_map_plain_is_equal(__isl_keep isl_map
*map1
,
9268 __isl_keep isl_map
*map2
)
9274 return isl_bool_error
;
9277 return isl_bool_true
;
9278 if (!isl_space_is_equal(map1
->dim
, map2
->dim
))
9279 return isl_bool_false
;
9281 map1
= isl_map_copy(map1
);
9282 map2
= isl_map_copy(map2
);
9283 map1
= isl_map_normalize(map1
);
9284 map2
= isl_map_normalize(map2
);
9287 equal
= map1
->n
== map2
->n
;
9288 for (i
= 0; equal
&& i
< map1
->n
; ++i
) {
9289 equal
= isl_basic_map_plain_is_equal(map1
->p
[i
], map2
->p
[i
]);
9299 return isl_bool_error
;
9302 isl_bool
isl_set_plain_is_equal(__isl_keep isl_set
*set1
,
9303 __isl_keep isl_set
*set2
)
9305 return isl_map_plain_is_equal((struct isl_map
*)set1
,
9306 (struct isl_map
*)set2
);
9309 /* Return an interval that ranges from min to max (inclusive)
9311 struct isl_basic_set
*isl_basic_set_interval(struct isl_ctx
*ctx
,
9312 isl_int min
, isl_int max
)
9315 struct isl_basic_set
*bset
= NULL
;
9317 bset
= isl_basic_set_alloc(ctx
, 0, 1, 0, 0, 2);
9321 k
= isl_basic_set_alloc_inequality(bset
);
9324 isl_int_set_si(bset
->ineq
[k
][1], 1);
9325 isl_int_neg(bset
->ineq
[k
][0], min
);
9327 k
= isl_basic_set_alloc_inequality(bset
);
9330 isl_int_set_si(bset
->ineq
[k
][1], -1);
9331 isl_int_set(bset
->ineq
[k
][0], max
);
9335 isl_basic_set_free(bset
);
9339 /* Return the basic maps in "map" as a list.
9341 __isl_give isl_basic_map_list
*isl_map_get_basic_map_list(
9342 __isl_keep isl_map
*map
)
9346 isl_basic_map_list
*list
;
9350 ctx
= isl_map_get_ctx(map
);
9351 list
= isl_basic_map_list_alloc(ctx
, map
->n
);
9353 for (i
= 0; i
< map
->n
; ++i
) {
9354 isl_basic_map
*bmap
;
9356 bmap
= isl_basic_map_copy(map
->p
[i
]);
9357 list
= isl_basic_map_list_add(list
, bmap
);
9363 /* Return the intersection of the elements in the non-empty list "list".
9364 * All elements are assumed to live in the same space.
9366 __isl_give isl_basic_map
*isl_basic_map_list_intersect(
9367 __isl_take isl_basic_map_list
*list
)
9370 isl_basic_map
*bmap
;
9374 n
= isl_basic_map_list_n_basic_map(list
);
9376 isl_die(isl_basic_map_list_get_ctx(list
), isl_error_invalid
,
9377 "expecting non-empty list", goto error
);
9379 bmap
= isl_basic_map_list_get_basic_map(list
, 0);
9380 for (i
= 1; i
< n
; ++i
) {
9381 isl_basic_map
*bmap_i
;
9383 bmap_i
= isl_basic_map_list_get_basic_map(list
, i
);
9384 bmap
= isl_basic_map_intersect(bmap
, bmap_i
);
9387 isl_basic_map_list_free(list
);
9390 isl_basic_map_list_free(list
);
9394 /* Return the intersection of the elements in the non-empty list "list".
9395 * All elements are assumed to live in the same space.
9397 __isl_give isl_basic_set
*isl_basic_set_list_intersect(
9398 __isl_take isl_basic_set_list
*list
)
9400 return isl_basic_map_list_intersect(list
);
9403 /* Return the union of the elements in the non-empty list "list".
9404 * All elements are assumed to live in the same space.
9406 __isl_give isl_set
*isl_set_list_union(__isl_take isl_set_list
*list
)
9413 n
= isl_set_list_n_set(list
);
9415 isl_die(isl_set_list_get_ctx(list
), isl_error_invalid
,
9416 "expecting non-empty list", goto error
);
9418 set
= isl_set_list_get_set(list
, 0);
9419 for (i
= 1; i
< n
; ++i
) {
9422 set_i
= isl_set_list_get_set(list
, i
);
9423 set
= isl_set_union(set
, set_i
);
9426 isl_set_list_free(list
);
9429 isl_set_list_free(list
);
9433 /* Return the Cartesian product of the basic sets in list (in the given order).
9435 __isl_give isl_basic_set
*isl_basic_set_list_product(
9436 __isl_take
struct isl_basic_set_list
*list
)
9444 struct isl_basic_set
*product
= NULL
;
9448 isl_assert(list
->ctx
, list
->n
> 0, goto error
);
9449 isl_assert(list
->ctx
, list
->p
[0], goto error
);
9450 nparam
= isl_basic_set_n_param(list
->p
[0]);
9451 dim
= isl_basic_set_n_dim(list
->p
[0]);
9452 extra
= list
->p
[0]->n_div
;
9453 n_eq
= list
->p
[0]->n_eq
;
9454 n_ineq
= list
->p
[0]->n_ineq
;
9455 for (i
= 1; i
< list
->n
; ++i
) {
9456 isl_assert(list
->ctx
, list
->p
[i
], goto error
);
9457 isl_assert(list
->ctx
,
9458 nparam
== isl_basic_set_n_param(list
->p
[i
]), goto error
);
9459 dim
+= isl_basic_set_n_dim(list
->p
[i
]);
9460 extra
+= list
->p
[i
]->n_div
;
9461 n_eq
+= list
->p
[i
]->n_eq
;
9462 n_ineq
+= list
->p
[i
]->n_ineq
;
9464 product
= isl_basic_set_alloc(list
->ctx
, nparam
, dim
, extra
,
9469 for (i
= 0; i
< list
->n
; ++i
) {
9470 isl_basic_set_add_constraints(product
,
9471 isl_basic_set_copy(list
->p
[i
]), dim
);
9472 dim
+= isl_basic_set_n_dim(list
->p
[i
]);
9474 isl_basic_set_list_free(list
);
9477 isl_basic_set_free(product
);
9478 isl_basic_set_list_free(list
);
9482 struct isl_basic_map
*isl_basic_map_product(
9483 struct isl_basic_map
*bmap1
, struct isl_basic_map
*bmap2
)
9485 isl_space
*dim_result
= NULL
;
9486 struct isl_basic_map
*bmap
;
9487 unsigned in1
, in2
, out1
, out2
, nparam
, total
, pos
;
9488 struct isl_dim_map
*dim_map1
, *dim_map2
;
9490 if (!bmap1
|| !bmap2
)
9493 isl_assert(bmap1
->ctx
, isl_space_match(bmap1
->dim
, isl_dim_param
,
9494 bmap2
->dim
, isl_dim_param
), goto error
);
9495 dim_result
= isl_space_product(isl_space_copy(bmap1
->dim
),
9496 isl_space_copy(bmap2
->dim
));
9498 in1
= isl_basic_map_n_in(bmap1
);
9499 in2
= isl_basic_map_n_in(bmap2
);
9500 out1
= isl_basic_map_n_out(bmap1
);
9501 out2
= isl_basic_map_n_out(bmap2
);
9502 nparam
= isl_basic_map_n_param(bmap1
);
9504 total
= nparam
+ in1
+ in2
+ out1
+ out2
+ bmap1
->n_div
+ bmap2
->n_div
;
9505 dim_map1
= isl_dim_map_alloc(bmap1
->ctx
, total
);
9506 dim_map2
= isl_dim_map_alloc(bmap1
->ctx
, total
);
9507 isl_dim_map_dim(dim_map1
, bmap1
->dim
, isl_dim_param
, pos
= 0);
9508 isl_dim_map_dim(dim_map2
, bmap2
->dim
, isl_dim_param
, pos
= 0);
9509 isl_dim_map_dim(dim_map1
, bmap1
->dim
, isl_dim_in
, pos
+= nparam
);
9510 isl_dim_map_dim(dim_map2
, bmap2
->dim
, isl_dim_in
, pos
+= in1
);
9511 isl_dim_map_dim(dim_map1
, bmap1
->dim
, isl_dim_out
, pos
+= in2
);
9512 isl_dim_map_dim(dim_map2
, bmap2
->dim
, isl_dim_out
, pos
+= out1
);
9513 isl_dim_map_div(dim_map1
, bmap1
, pos
+= out2
);
9514 isl_dim_map_div(dim_map2
, bmap2
, pos
+= bmap1
->n_div
);
9516 bmap
= isl_basic_map_alloc_space(dim_result
,
9517 bmap1
->n_div
+ bmap2
->n_div
,
9518 bmap1
->n_eq
+ bmap2
->n_eq
,
9519 bmap1
->n_ineq
+ bmap2
->n_ineq
);
9520 bmap
= isl_basic_map_add_constraints_dim_map(bmap
, bmap1
, dim_map1
);
9521 bmap
= isl_basic_map_add_constraints_dim_map(bmap
, bmap2
, dim_map2
);
9522 bmap
= isl_basic_map_simplify(bmap
);
9523 return isl_basic_map_finalize(bmap
);
9525 isl_basic_map_free(bmap1
);
9526 isl_basic_map_free(bmap2
);
9530 __isl_give isl_basic_map
*isl_basic_map_flat_product(
9531 __isl_take isl_basic_map
*bmap1
, __isl_take isl_basic_map
*bmap2
)
9533 isl_basic_map
*prod
;
9535 prod
= isl_basic_map_product(bmap1
, bmap2
);
9536 prod
= isl_basic_map_flatten(prod
);
9540 __isl_give isl_basic_set
*isl_basic_set_flat_product(
9541 __isl_take isl_basic_set
*bset1
, __isl_take isl_basic_set
*bset2
)
9543 return isl_basic_map_flat_range_product(bset1
, bset2
);
9546 __isl_give isl_basic_map
*isl_basic_map_domain_product(
9547 __isl_take isl_basic_map
*bmap1
, __isl_take isl_basic_map
*bmap2
)
9549 isl_space
*space_result
= NULL
;
9550 isl_basic_map
*bmap
;
9551 unsigned in1
, in2
, out
, nparam
, total
, pos
;
9552 struct isl_dim_map
*dim_map1
, *dim_map2
;
9554 if (!bmap1
|| !bmap2
)
9557 space_result
= isl_space_domain_product(isl_space_copy(bmap1
->dim
),
9558 isl_space_copy(bmap2
->dim
));
9560 in1
= isl_basic_map_dim(bmap1
, isl_dim_in
);
9561 in2
= isl_basic_map_dim(bmap2
, isl_dim_in
);
9562 out
= isl_basic_map_dim(bmap1
, isl_dim_out
);
9563 nparam
= isl_basic_map_dim(bmap1
, isl_dim_param
);
9565 total
= nparam
+ in1
+ in2
+ out
+ bmap1
->n_div
+ bmap2
->n_div
;
9566 dim_map1
= isl_dim_map_alloc(bmap1
->ctx
, total
);
9567 dim_map2
= isl_dim_map_alloc(bmap1
->ctx
, total
);
9568 isl_dim_map_dim(dim_map1
, bmap1
->dim
, isl_dim_param
, pos
= 0);
9569 isl_dim_map_dim(dim_map2
, bmap2
->dim
, isl_dim_param
, pos
= 0);
9570 isl_dim_map_dim(dim_map1
, bmap1
->dim
, isl_dim_in
, pos
+= nparam
);
9571 isl_dim_map_dim(dim_map2
, bmap2
->dim
, isl_dim_in
, pos
+= in1
);
9572 isl_dim_map_dim(dim_map1
, bmap1
->dim
, isl_dim_out
, pos
+= in2
);
9573 isl_dim_map_dim(dim_map2
, bmap2
->dim
, isl_dim_out
, pos
);
9574 isl_dim_map_div(dim_map1
, bmap1
, pos
+= out
);
9575 isl_dim_map_div(dim_map2
, bmap2
, pos
+= bmap1
->n_div
);
9577 bmap
= isl_basic_map_alloc_space(space_result
,
9578 bmap1
->n_div
+ bmap2
->n_div
,
9579 bmap1
->n_eq
+ bmap2
->n_eq
,
9580 bmap1
->n_ineq
+ bmap2
->n_ineq
);
9581 bmap
= isl_basic_map_add_constraints_dim_map(bmap
, bmap1
, dim_map1
);
9582 bmap
= isl_basic_map_add_constraints_dim_map(bmap
, bmap2
, dim_map2
);
9583 bmap
= isl_basic_map_simplify(bmap
);
9584 return isl_basic_map_finalize(bmap
);
9586 isl_basic_map_free(bmap1
);
9587 isl_basic_map_free(bmap2
);
9591 __isl_give isl_basic_map
*isl_basic_map_range_product(
9592 __isl_take isl_basic_map
*bmap1
, __isl_take isl_basic_map
*bmap2
)
9594 isl_space
*dim_result
= NULL
;
9595 isl_basic_map
*bmap
;
9596 unsigned in
, out1
, out2
, nparam
, total
, pos
;
9597 struct isl_dim_map
*dim_map1
, *dim_map2
;
9599 if (!bmap1
|| !bmap2
)
9602 if (!isl_space_match(bmap1
->dim
, isl_dim_param
,
9603 bmap2
->dim
, isl_dim_param
))
9604 isl_die(isl_basic_map_get_ctx(bmap1
), isl_error_invalid
,
9605 "parameters don't match", goto error
);
9607 dim_result
= isl_space_range_product(isl_space_copy(bmap1
->dim
),
9608 isl_space_copy(bmap2
->dim
));
9610 in
= isl_basic_map_dim(bmap1
, isl_dim_in
);
9611 out1
= isl_basic_map_n_out(bmap1
);
9612 out2
= isl_basic_map_n_out(bmap2
);
9613 nparam
= isl_basic_map_n_param(bmap1
);
9615 total
= nparam
+ in
+ out1
+ out2
+ bmap1
->n_div
+ bmap2
->n_div
;
9616 dim_map1
= isl_dim_map_alloc(bmap1
->ctx
, total
);
9617 dim_map2
= isl_dim_map_alloc(bmap1
->ctx
, total
);
9618 isl_dim_map_dim(dim_map1
, bmap1
->dim
, isl_dim_param
, pos
= 0);
9619 isl_dim_map_dim(dim_map2
, bmap2
->dim
, isl_dim_param
, pos
= 0);
9620 isl_dim_map_dim(dim_map1
, bmap1
->dim
, isl_dim_in
, pos
+= nparam
);
9621 isl_dim_map_dim(dim_map2
, bmap2
->dim
, isl_dim_in
, pos
);
9622 isl_dim_map_dim(dim_map1
, bmap1
->dim
, isl_dim_out
, pos
+= in
);
9623 isl_dim_map_dim(dim_map2
, bmap2
->dim
, isl_dim_out
, pos
+= out1
);
9624 isl_dim_map_div(dim_map1
, bmap1
, pos
+= out2
);
9625 isl_dim_map_div(dim_map2
, bmap2
, pos
+= bmap1
->n_div
);
9627 bmap
= isl_basic_map_alloc_space(dim_result
,
9628 bmap1
->n_div
+ bmap2
->n_div
,
9629 bmap1
->n_eq
+ bmap2
->n_eq
,
9630 bmap1
->n_ineq
+ bmap2
->n_ineq
);
9631 bmap
= isl_basic_map_add_constraints_dim_map(bmap
, bmap1
, dim_map1
);
9632 bmap
= isl_basic_map_add_constraints_dim_map(bmap
, bmap2
, dim_map2
);
9633 bmap
= isl_basic_map_simplify(bmap
);
9634 return isl_basic_map_finalize(bmap
);
9636 isl_basic_map_free(bmap1
);
9637 isl_basic_map_free(bmap2
);
9641 __isl_give isl_basic_map
*isl_basic_map_flat_range_product(
9642 __isl_take isl_basic_map
*bmap1
, __isl_take isl_basic_map
*bmap2
)
9644 isl_basic_map
*prod
;
9646 prod
= isl_basic_map_range_product(bmap1
, bmap2
);
9647 prod
= isl_basic_map_flatten_range(prod
);
9651 /* Apply "basic_map_product" to each pair of basic maps in "map1" and "map2"
9652 * and collect the results.
9653 * The result live in the space obtained by calling "space_product"
9654 * on the spaces of "map1" and "map2".
9655 * If "remove_duplicates" is set then the result may contain duplicates
9656 * (even if the inputs do not) and so we try and remove the obvious
9659 static __isl_give isl_map
*map_product(__isl_take isl_map
*map1
,
9660 __isl_take isl_map
*map2
,
9661 __isl_give isl_space
*(*space_product
)(__isl_take isl_space
*left
,
9662 __isl_take isl_space
*right
),
9663 __isl_give isl_basic_map
*(*basic_map_product
)(
9664 __isl_take isl_basic_map
*left
,
9665 __isl_take isl_basic_map
*right
),
9666 int remove_duplicates
)
9669 struct isl_map
*result
;
9675 isl_assert(map1
->ctx
, isl_space_match(map1
->dim
, isl_dim_param
,
9676 map2
->dim
, isl_dim_param
), goto error
);
9678 if (ISL_F_ISSET(map1
, ISL_MAP_DISJOINT
) &&
9679 ISL_F_ISSET(map2
, ISL_MAP_DISJOINT
))
9680 ISL_FL_SET(flags
, ISL_MAP_DISJOINT
);
9682 result
= isl_map_alloc_space(space_product(isl_space_copy(map1
->dim
),
9683 isl_space_copy(map2
->dim
)),
9684 map1
->n
* map2
->n
, flags
);
9687 for (i
= 0; i
< map1
->n
; ++i
)
9688 for (j
= 0; j
< map2
->n
; ++j
) {
9689 struct isl_basic_map
*part
;
9690 part
= basic_map_product(isl_basic_map_copy(map1
->p
[i
]),
9691 isl_basic_map_copy(map2
->p
[j
]));
9692 if (isl_basic_map_is_empty(part
))
9693 isl_basic_map_free(part
);
9695 result
= isl_map_add_basic_map(result
, part
);
9699 if (remove_duplicates
)
9700 result
= isl_map_remove_obvious_duplicates(result
);
9710 /* Given two maps A -> B and C -> D, construct a map [A -> C] -> [B -> D]
9712 static __isl_give isl_map
*map_product_aligned(__isl_take isl_map
*map1
,
9713 __isl_take isl_map
*map2
)
9715 return map_product(map1
, map2
, &isl_space_product
,
9716 &isl_basic_map_product
, 0);
9719 __isl_give isl_map
*isl_map_product(__isl_take isl_map
*map1
,
9720 __isl_take isl_map
*map2
)
9722 return isl_map_align_params_map_map_and(map1
, map2
, &map_product_aligned
);
9725 /* Given two maps A -> B and C -> D, construct a map (A, C) -> (B, D)
9727 __isl_give isl_map
*isl_map_flat_product(__isl_take isl_map
*map1
,
9728 __isl_take isl_map
*map2
)
9732 prod
= isl_map_product(map1
, map2
);
9733 prod
= isl_map_flatten(prod
);
9737 /* Given two set A and B, construct its Cartesian product A x B.
9739 struct isl_set
*isl_set_product(struct isl_set
*set1
, struct isl_set
*set2
)
9741 return isl_map_range_product(set1
, set2
);
9744 __isl_give isl_set
*isl_set_flat_product(__isl_take isl_set
*set1
,
9745 __isl_take isl_set
*set2
)
9747 return isl_map_flat_range_product(set1
, set2
);
9750 /* Given two maps A -> B and C -> D, construct a map [A -> C] -> (B * D)
9752 static __isl_give isl_map
*map_domain_product_aligned(__isl_take isl_map
*map1
,
9753 __isl_take isl_map
*map2
)
9755 return map_product(map1
, map2
, &isl_space_domain_product
,
9756 &isl_basic_map_domain_product
, 1);
9759 /* Given two maps A -> B and C -> D, construct a map (A * C) -> [B -> D]
9761 static __isl_give isl_map
*map_range_product_aligned(__isl_take isl_map
*map1
,
9762 __isl_take isl_map
*map2
)
9764 return map_product(map1
, map2
, &isl_space_range_product
,
9765 &isl_basic_map_range_product
, 1);
9768 __isl_give isl_map
*isl_map_domain_product(__isl_take isl_map
*map1
,
9769 __isl_take isl_map
*map2
)
9771 return isl_map_align_params_map_map_and(map1
, map2
,
9772 &map_domain_product_aligned
);
9775 __isl_give isl_map
*isl_map_range_product(__isl_take isl_map
*map1
,
9776 __isl_take isl_map
*map2
)
9778 return isl_map_align_params_map_map_and(map1
, map2
,
9779 &map_range_product_aligned
);
9782 /* Given a map of the form [A -> B] -> [C -> D], return the map A -> C.
9784 __isl_give isl_map
*isl_map_factor_domain(__isl_take isl_map
*map
)
9787 int total1
, keep1
, total2
, keep2
;
9791 if (!isl_space_domain_is_wrapping(map
->dim
) ||
9792 !isl_space_range_is_wrapping(map
->dim
))
9793 isl_die(isl_map_get_ctx(map
), isl_error_invalid
,
9794 "not a product", return isl_map_free(map
));
9796 space
= isl_map_get_space(map
);
9797 total1
= isl_space_dim(space
, isl_dim_in
);
9798 total2
= isl_space_dim(space
, isl_dim_out
);
9799 space
= isl_space_factor_domain(space
);
9800 keep1
= isl_space_dim(space
, isl_dim_in
);
9801 keep2
= isl_space_dim(space
, isl_dim_out
);
9802 map
= isl_map_project_out(map
, isl_dim_in
, keep1
, total1
- keep1
);
9803 map
= isl_map_project_out(map
, isl_dim_out
, keep2
, total2
- keep2
);
9804 map
= isl_map_reset_space(map
, space
);
9809 /* Given a map of the form [A -> B] -> [C -> D], return the map B -> D.
9811 __isl_give isl_map
*isl_map_factor_range(__isl_take isl_map
*map
)
9814 int total1
, keep1
, total2
, keep2
;
9818 if (!isl_space_domain_is_wrapping(map
->dim
) ||
9819 !isl_space_range_is_wrapping(map
->dim
))
9820 isl_die(isl_map_get_ctx(map
), isl_error_invalid
,
9821 "not a product", return isl_map_free(map
));
9823 space
= isl_map_get_space(map
);
9824 total1
= isl_space_dim(space
, isl_dim_in
);
9825 total2
= isl_space_dim(space
, isl_dim_out
);
9826 space
= isl_space_factor_range(space
);
9827 keep1
= isl_space_dim(space
, isl_dim_in
);
9828 keep2
= isl_space_dim(space
, isl_dim_out
);
9829 map
= isl_map_project_out(map
, isl_dim_in
, 0, total1
- keep1
);
9830 map
= isl_map_project_out(map
, isl_dim_out
, 0, total2
- keep2
);
9831 map
= isl_map_reset_space(map
, space
);
9836 /* Given a map of the form [A -> B] -> C, return the map A -> C.
9838 __isl_give isl_map
*isl_map_domain_factor_domain(__isl_take isl_map
*map
)
9845 if (!isl_space_domain_is_wrapping(map
->dim
))
9846 isl_die(isl_map_get_ctx(map
), isl_error_invalid
,
9847 "domain is not a product", return isl_map_free(map
));
9849 space
= isl_map_get_space(map
);
9850 total
= isl_space_dim(space
, isl_dim_in
);
9851 space
= isl_space_domain_factor_domain(space
);
9852 keep
= isl_space_dim(space
, isl_dim_in
);
9853 map
= isl_map_project_out(map
, isl_dim_in
, keep
, total
- keep
);
9854 map
= isl_map_reset_space(map
, space
);
9859 /* Given a map of the form [A -> B] -> C, return the map B -> C.
9861 __isl_give isl_map
*isl_map_domain_factor_range(__isl_take isl_map
*map
)
9868 if (!isl_space_domain_is_wrapping(map
->dim
))
9869 isl_die(isl_map_get_ctx(map
), isl_error_invalid
,
9870 "domain is not a product", return isl_map_free(map
));
9872 space
= isl_map_get_space(map
);
9873 total
= isl_space_dim(space
, isl_dim_in
);
9874 space
= isl_space_domain_factor_range(space
);
9875 keep
= isl_space_dim(space
, isl_dim_in
);
9876 map
= isl_map_project_out(map
, isl_dim_in
, 0, total
- keep
);
9877 map
= isl_map_reset_space(map
, space
);
9882 /* Given a map A -> [B -> C], extract the map A -> B.
9884 __isl_give isl_map
*isl_map_range_factor_domain(__isl_take isl_map
*map
)
9891 if (!isl_space_range_is_wrapping(map
->dim
))
9892 isl_die(isl_map_get_ctx(map
), isl_error_invalid
,
9893 "range is not a product", return isl_map_free(map
));
9895 space
= isl_map_get_space(map
);
9896 total
= isl_space_dim(space
, isl_dim_out
);
9897 space
= isl_space_range_factor_domain(space
);
9898 keep
= isl_space_dim(space
, isl_dim_out
);
9899 map
= isl_map_project_out(map
, isl_dim_out
, keep
, total
- keep
);
9900 map
= isl_map_reset_space(map
, space
);
9905 /* Given a map A -> [B -> C], extract the map A -> C.
9907 __isl_give isl_map
*isl_map_range_factor_range(__isl_take isl_map
*map
)
9914 if (!isl_space_range_is_wrapping(map
->dim
))
9915 isl_die(isl_map_get_ctx(map
), isl_error_invalid
,
9916 "range is not a product", return isl_map_free(map
));
9918 space
= isl_map_get_space(map
);
9919 total
= isl_space_dim(space
, isl_dim_out
);
9920 space
= isl_space_range_factor_range(space
);
9921 keep
= isl_space_dim(space
, isl_dim_out
);
9922 map
= isl_map_project_out(map
, isl_dim_out
, 0, total
- keep
);
9923 map
= isl_map_reset_space(map
, space
);
9928 /* Given two maps A -> B and C -> D, construct a map (A, C) -> (B * D)
9930 __isl_give isl_map
*isl_map_flat_domain_product(__isl_take isl_map
*map1
,
9931 __isl_take isl_map
*map2
)
9935 prod
= isl_map_domain_product(map1
, map2
);
9936 prod
= isl_map_flatten_domain(prod
);
9940 /* Given two maps A -> B and C -> D, construct a map (A * C) -> (B, D)
9942 __isl_give isl_map
*isl_map_flat_range_product(__isl_take isl_map
*map1
,
9943 __isl_take isl_map
*map2
)
9947 prod
= isl_map_range_product(map1
, map2
);
9948 prod
= isl_map_flatten_range(prod
);
9952 uint32_t isl_basic_map_get_hash(__isl_keep isl_basic_map
*bmap
)
9955 uint32_t hash
= isl_hash_init();
9960 bmap
= isl_basic_map_copy(bmap
);
9961 bmap
= isl_basic_map_normalize(bmap
);
9964 total
= isl_basic_map_total_dim(bmap
);
9965 isl_hash_byte(hash
, bmap
->n_eq
& 0xFF);
9966 for (i
= 0; i
< bmap
->n_eq
; ++i
) {
9968 c_hash
= isl_seq_get_hash(bmap
->eq
[i
], 1 + total
);
9969 isl_hash_hash(hash
, c_hash
);
9971 isl_hash_byte(hash
, bmap
->n_ineq
& 0xFF);
9972 for (i
= 0; i
< bmap
->n_ineq
; ++i
) {
9974 c_hash
= isl_seq_get_hash(bmap
->ineq
[i
], 1 + total
);
9975 isl_hash_hash(hash
, c_hash
);
9977 isl_hash_byte(hash
, bmap
->n_div
& 0xFF);
9978 for (i
= 0; i
< bmap
->n_div
; ++i
) {
9980 if (isl_int_is_zero(bmap
->div
[i
][0]))
9982 isl_hash_byte(hash
, i
& 0xFF);
9983 c_hash
= isl_seq_get_hash(bmap
->div
[i
], 1 + 1 + total
);
9984 isl_hash_hash(hash
, c_hash
);
9986 isl_basic_map_free(bmap
);
9990 uint32_t isl_basic_set_get_hash(__isl_keep isl_basic_set
*bset
)
9992 return isl_basic_map_get_hash((isl_basic_map
*)bset
);
9995 uint32_t isl_map_get_hash(__isl_keep isl_map
*map
)
10002 map
= isl_map_copy(map
);
10003 map
= isl_map_normalize(map
);
10007 hash
= isl_hash_init();
10008 for (i
= 0; i
< map
->n
; ++i
) {
10009 uint32_t bmap_hash
;
10010 bmap_hash
= isl_basic_map_get_hash(map
->p
[i
]);
10011 isl_hash_hash(hash
, bmap_hash
);
10019 uint32_t isl_set_get_hash(__isl_keep isl_set
*set
)
10021 return isl_map_get_hash((isl_map
*)set
);
10024 /* Check if the value for dimension dim is completely determined
10025 * by the values of the other parameters and variables.
10026 * That is, check if dimension dim is involved in an equality.
10028 int isl_basic_set_dim_is_unique(struct isl_basic_set
*bset
, unsigned dim
)
10035 nparam
= isl_basic_set_n_param(bset
);
10036 for (i
= 0; i
< bset
->n_eq
; ++i
)
10037 if (!isl_int_is_zero(bset
->eq
[i
][1 + nparam
+ dim
]))
10042 /* Check if the value for dimension dim is completely determined
10043 * by the values of the other parameters and variables.
10044 * That is, check if dimension dim is involved in an equality
10045 * for each of the subsets.
10047 int isl_set_dim_is_unique(struct isl_set
*set
, unsigned dim
)
10053 for (i
= 0; i
< set
->n
; ++i
) {
10055 unique
= isl_basic_set_dim_is_unique(set
->p
[i
], dim
);
10062 /* Return the number of basic maps in the (current) representation of "map".
10064 int isl_map_n_basic_map(__isl_keep isl_map
*map
)
10066 return map
? map
->n
: 0;
10069 int isl_set_n_basic_set(__isl_keep isl_set
*set
)
10071 return set
? set
->n
: 0;
10074 isl_stat
isl_map_foreach_basic_map(__isl_keep isl_map
*map
,
10075 isl_stat (*fn
)(__isl_take isl_basic_map
*bmap
, void *user
), void *user
)
10080 return isl_stat_error
;
10082 for (i
= 0; i
< map
->n
; ++i
)
10083 if (fn(isl_basic_map_copy(map
->p
[i
]), user
) < 0)
10084 return isl_stat_error
;
10086 return isl_stat_ok
;
10089 isl_stat
isl_set_foreach_basic_set(__isl_keep isl_set
*set
,
10090 isl_stat (*fn
)(__isl_take isl_basic_set
*bset
, void *user
), void *user
)
10095 return isl_stat_error
;
10097 for (i
= 0; i
< set
->n
; ++i
)
10098 if (fn(isl_basic_set_copy(set
->p
[i
]), user
) < 0)
10099 return isl_stat_error
;
10101 return isl_stat_ok
;
10104 /* Return a list of basic sets, the union of which is equal to "set".
10106 __isl_give isl_basic_set_list
*isl_set_get_basic_set_list(
10107 __isl_keep isl_set
*set
)
10110 isl_basic_set_list
*list
;
10115 list
= isl_basic_set_list_alloc(isl_set_get_ctx(set
), set
->n
);
10116 for (i
= 0; i
< set
->n
; ++i
) {
10117 isl_basic_set
*bset
;
10119 bset
= isl_basic_set_copy(set
->p
[i
]);
10120 list
= isl_basic_set_list_add(list
, bset
);
10126 __isl_give isl_basic_set
*isl_basic_set_lift(__isl_take isl_basic_set
*bset
)
10133 bset
= isl_basic_set_cow(bset
);
10137 dim
= isl_basic_set_get_space(bset
);
10138 dim
= isl_space_lift(dim
, bset
->n_div
);
10141 isl_space_free(bset
->dim
);
10143 bset
->extra
-= bset
->n_div
;
10146 bset
= isl_basic_set_finalize(bset
);
10150 isl_basic_set_free(bset
);
10154 __isl_give isl_set
*isl_set_lift(__isl_take isl_set
*set
)
10160 set
= isl_set_align_divs(set
);
10165 set
= isl_set_cow(set
);
10169 n_div
= set
->p
[0]->n_div
;
10170 dim
= isl_set_get_space(set
);
10171 dim
= isl_space_lift(dim
, n_div
);
10174 isl_space_free(set
->dim
);
10177 for (i
= 0; i
< set
->n
; ++i
) {
10178 set
->p
[i
] = isl_basic_set_lift(set
->p
[i
]);
10189 __isl_give isl_map
*isl_set_lifting(__isl_take isl_set
*set
)
10192 struct isl_basic_map
*bmap
;
10199 set
= isl_set_align_divs(set
);
10204 dim
= isl_set_get_space(set
);
10205 if (set
->n
== 0 || set
->p
[0]->n_div
== 0) {
10207 return isl_map_identity(isl_space_map_from_set(dim
));
10210 n_div
= set
->p
[0]->n_div
;
10211 dim
= isl_space_map_from_set(dim
);
10212 n_param
= isl_space_dim(dim
, isl_dim_param
);
10213 n_set
= isl_space_dim(dim
, isl_dim_in
);
10214 dim
= isl_space_extend(dim
, n_param
, n_set
, n_set
+ n_div
);
10215 bmap
= isl_basic_map_alloc_space(dim
, 0, n_set
, 2 * n_div
);
10216 for (i
= 0; i
< n_set
; ++i
)
10217 bmap
= var_equal(bmap
, i
);
10219 total
= n_param
+ n_set
+ n_set
+ n_div
;
10220 for (i
= 0; i
< n_div
; ++i
) {
10221 k
= isl_basic_map_alloc_inequality(bmap
);
10224 isl_seq_cpy(bmap
->ineq
[k
], set
->p
[0]->div
[i
]+1, 1+n_param
);
10225 isl_seq_clr(bmap
->ineq
[k
]+1+n_param
, n_set
);
10226 isl_seq_cpy(bmap
->ineq
[k
]+1+n_param
+n_set
,
10227 set
->p
[0]->div
[i
]+1+1+n_param
, n_set
+ n_div
);
10228 isl_int_neg(bmap
->ineq
[k
][1+n_param
+n_set
+n_set
+i
],
10229 set
->p
[0]->div
[i
][0]);
10231 l
= isl_basic_map_alloc_inequality(bmap
);
10234 isl_seq_neg(bmap
->ineq
[l
], bmap
->ineq
[k
], 1 + total
);
10235 isl_int_add(bmap
->ineq
[l
][0], bmap
->ineq
[l
][0],
10236 set
->p
[0]->div
[i
][0]);
10237 isl_int_sub_ui(bmap
->ineq
[l
][0], bmap
->ineq
[l
][0], 1);
10241 bmap
= isl_basic_map_simplify(bmap
);
10242 bmap
= isl_basic_map_finalize(bmap
);
10243 return isl_map_from_basic_map(bmap
);
10246 isl_basic_map_free(bmap
);
10250 int isl_basic_set_size(__isl_keep isl_basic_set
*bset
)
10258 dim
= isl_basic_set_total_dim(bset
);
10259 size
+= bset
->n_eq
* (1 + dim
);
10260 size
+= bset
->n_ineq
* (1 + dim
);
10261 size
+= bset
->n_div
* (2 + dim
);
10266 int isl_set_size(__isl_keep isl_set
*set
)
10274 for (i
= 0; i
< set
->n
; ++i
)
10275 size
+= isl_basic_set_size(set
->p
[i
]);
10280 /* Check if there is any lower bound (if lower == 0) and/or upper
10281 * bound (if upper == 0) on the specified dim.
10283 static isl_bool
basic_map_dim_is_bounded(__isl_keep isl_basic_map
*bmap
,
10284 enum isl_dim_type type
, unsigned pos
, int lower
, int upper
)
10289 return isl_bool_error
;
10291 isl_assert(bmap
->ctx
, pos
< isl_basic_map_dim(bmap
, type
),
10292 return isl_bool_error
);
10294 pos
+= isl_basic_map_offset(bmap
, type
);
10296 for (i
= 0; i
< bmap
->n_div
; ++i
) {
10297 if (isl_int_is_zero(bmap
->div
[i
][0]))
10299 if (!isl_int_is_zero(bmap
->div
[i
][1 + pos
]))
10300 return isl_bool_true
;
10303 for (i
= 0; i
< bmap
->n_eq
; ++i
)
10304 if (!isl_int_is_zero(bmap
->eq
[i
][pos
]))
10305 return isl_bool_true
;
10307 for (i
= 0; i
< bmap
->n_ineq
; ++i
) {
10308 int sgn
= isl_int_sgn(bmap
->ineq
[i
][pos
]);
10315 return lower
&& upper
;
10318 int isl_basic_map_dim_is_bounded(__isl_keep isl_basic_map
*bmap
,
10319 enum isl_dim_type type
, unsigned pos
)
10321 return basic_map_dim_is_bounded(bmap
, type
, pos
, 0, 0);
10324 isl_bool
isl_basic_map_dim_has_lower_bound(__isl_keep isl_basic_map
*bmap
,
10325 enum isl_dim_type type
, unsigned pos
)
10327 return basic_map_dim_is_bounded(bmap
, type
, pos
, 0, 1);
10330 isl_bool
isl_basic_map_dim_has_upper_bound(__isl_keep isl_basic_map
*bmap
,
10331 enum isl_dim_type type
, unsigned pos
)
10333 return basic_map_dim_is_bounded(bmap
, type
, pos
, 1, 0);
10336 int isl_map_dim_is_bounded(__isl_keep isl_map
*map
,
10337 enum isl_dim_type type
, unsigned pos
)
10344 for (i
= 0; i
< map
->n
; ++i
) {
10346 bounded
= isl_basic_map_dim_is_bounded(map
->p
[i
], type
, pos
);
10347 if (bounded
< 0 || !bounded
)
10354 /* Return 1 if the specified dim is involved in both an upper bound
10355 * and a lower bound.
10357 int isl_set_dim_is_bounded(__isl_keep isl_set
*set
,
10358 enum isl_dim_type type
, unsigned pos
)
10360 return isl_map_dim_is_bounded((isl_map
*)set
, type
, pos
);
10363 /* Does "map" have a bound (according to "fn") for any of its basic maps?
10365 static isl_bool
has_any_bound(__isl_keep isl_map
*map
,
10366 enum isl_dim_type type
, unsigned pos
,
10367 isl_bool (*fn
)(__isl_keep isl_basic_map
*bmap
,
10368 enum isl_dim_type type
, unsigned pos
))
10373 return isl_bool_error
;
10375 for (i
= 0; i
< map
->n
; ++i
) {
10377 bounded
= fn(map
->p
[i
], type
, pos
);
10378 if (bounded
< 0 || bounded
)
10382 return isl_bool_false
;
10385 /* Return 1 if the specified dim is involved in any lower bound.
10387 isl_bool
isl_set_dim_has_any_lower_bound(__isl_keep isl_set
*set
,
10388 enum isl_dim_type type
, unsigned pos
)
10390 return has_any_bound(set
, type
, pos
,
10391 &isl_basic_map_dim_has_lower_bound
);
10394 /* Return 1 if the specified dim is involved in any upper bound.
10396 isl_bool
isl_set_dim_has_any_upper_bound(__isl_keep isl_set
*set
,
10397 enum isl_dim_type type
, unsigned pos
)
10399 return has_any_bound(set
, type
, pos
,
10400 &isl_basic_map_dim_has_upper_bound
);
10403 /* Does "map" have a bound (according to "fn") for all of its basic maps?
10405 static isl_bool
has_bound(__isl_keep isl_map
*map
,
10406 enum isl_dim_type type
, unsigned pos
,
10407 isl_bool (*fn
)(__isl_keep isl_basic_map
*bmap
,
10408 enum isl_dim_type type
, unsigned pos
))
10413 return isl_bool_error
;
10415 for (i
= 0; i
< map
->n
; ++i
) {
10417 bounded
= fn(map
->p
[i
], type
, pos
);
10418 if (bounded
< 0 || !bounded
)
10422 return isl_bool_true
;
10425 /* Return 1 if the specified dim has a lower bound (in each of its basic sets).
10427 isl_bool
isl_set_dim_has_lower_bound(__isl_keep isl_set
*set
,
10428 enum isl_dim_type type
, unsigned pos
)
10430 return has_bound(set
, type
, pos
, &isl_basic_map_dim_has_lower_bound
);
10433 /* Return 1 if the specified dim has an upper bound (in each of its basic sets).
10435 isl_bool
isl_set_dim_has_upper_bound(__isl_keep isl_set
*set
,
10436 enum isl_dim_type type
, unsigned pos
)
10438 return has_bound(set
, type
, pos
, &isl_basic_map_dim_has_upper_bound
);
10441 /* For each of the "n" variables starting at "first", determine
10442 * the sign of the variable and put the results in the first "n"
10443 * elements of the array "signs".
10445 * 1 means that the variable is non-negative
10446 * -1 means that the variable is non-positive
10447 * 0 means the variable attains both positive and negative values.
10449 int isl_basic_set_vars_get_sign(__isl_keep isl_basic_set
*bset
,
10450 unsigned first
, unsigned n
, int *signs
)
10452 isl_vec
*bound
= NULL
;
10453 struct isl_tab
*tab
= NULL
;
10454 struct isl_tab_undo
*snap
;
10457 if (!bset
|| !signs
)
10460 bound
= isl_vec_alloc(bset
->ctx
, 1 + isl_basic_set_total_dim(bset
));
10461 tab
= isl_tab_from_basic_set(bset
, 0);
10462 if (!bound
|| !tab
)
10465 isl_seq_clr(bound
->el
, bound
->size
);
10466 isl_int_set_si(bound
->el
[0], -1);
10468 snap
= isl_tab_snap(tab
);
10469 for (i
= 0; i
< n
; ++i
) {
10472 isl_int_set_si(bound
->el
[1 + first
+ i
], -1);
10473 if (isl_tab_add_ineq(tab
, bound
->el
) < 0)
10475 empty
= tab
->empty
;
10476 isl_int_set_si(bound
->el
[1 + first
+ i
], 0);
10477 if (isl_tab_rollback(tab
, snap
) < 0)
10485 isl_int_set_si(bound
->el
[1 + first
+ i
], 1);
10486 if (isl_tab_add_ineq(tab
, bound
->el
) < 0)
10488 empty
= tab
->empty
;
10489 isl_int_set_si(bound
->el
[1 + first
+ i
], 0);
10490 if (isl_tab_rollback(tab
, snap
) < 0)
10493 signs
[i
] = empty
? -1 : 0;
10497 isl_vec_free(bound
);
10501 isl_vec_free(bound
);
10505 int isl_basic_set_dims_get_sign(__isl_keep isl_basic_set
*bset
,
10506 enum isl_dim_type type
, unsigned first
, unsigned n
, int *signs
)
10508 if (!bset
|| !signs
)
10510 isl_assert(bset
->ctx
, first
+ n
<= isl_basic_set_dim(bset
, type
),
10513 first
+= pos(bset
->dim
, type
) - 1;
10514 return isl_basic_set_vars_get_sign(bset
, first
, n
, signs
);
10517 /* Is it possible for the integer division "div" to depend (possibly
10518 * indirectly) on any output dimensions?
10520 * If the div is undefined, then we conservatively assume that it
10521 * may depend on them.
10522 * Otherwise, we check if it actually depends on them or on any integer
10523 * divisions that may depend on them.
10525 static int div_may_involve_output(__isl_keep isl_basic_map
*bmap
, int div
)
10528 unsigned n_out
, o_out
;
10529 unsigned n_div
, o_div
;
10531 if (isl_int_is_zero(bmap
->div
[div
][0]))
10534 n_out
= isl_basic_map_dim(bmap
, isl_dim_out
);
10535 o_out
= isl_basic_map_offset(bmap
, isl_dim_out
);
10537 if (isl_seq_first_non_zero(bmap
->div
[div
] + 1 + o_out
, n_out
) != -1)
10540 n_div
= isl_basic_map_dim(bmap
, isl_dim_div
);
10541 o_div
= isl_basic_map_offset(bmap
, isl_dim_div
);
10543 for (i
= 0; i
< n_div
; ++i
) {
10544 if (isl_int_is_zero(bmap
->div
[div
][1 + o_div
+ i
]))
10546 if (div_may_involve_output(bmap
, i
))
10553 /* Return the first integer division of "bmap" in the range
10554 * [first, first + n[ that may depend on any output dimensions and
10555 * that has a non-zero coefficient in "c" (where the first coefficient
10556 * in "c" corresponds to integer division "first").
10558 static int first_div_may_involve_output(__isl_keep isl_basic_map
*bmap
,
10559 isl_int
*c
, int first
, int n
)
10566 for (k
= first
; k
< first
+ n
; ++k
) {
10567 if (isl_int_is_zero(c
[k
]))
10569 if (div_may_involve_output(bmap
, k
))
10576 /* Look for a pair of inequality constraints in "bmap" of the form
10578 * -l + i >= 0 or i >= l
10580 * n + l - i >= 0 or i <= l + n
10582 * with n < "m" and i the output dimension at position "pos".
10583 * (Note that n >= 0 as otherwise the two constraints would conflict.)
10584 * Furthermore, "l" is only allowed to involve parameters, input dimensions
10585 * and earlier output dimensions, as well as integer divisions that do
10586 * not involve any of the output dimensions.
10588 * Return the index of the first inequality constraint or bmap->n_ineq
10589 * if no such pair can be found.
10591 static int find_modulo_constraint_pair(__isl_keep isl_basic_map
*bmap
,
10592 int pos
, isl_int m
)
10597 unsigned n_div
, o_div
;
10598 unsigned n_out
, o_out
;
10604 ctx
= isl_basic_map_get_ctx(bmap
);
10605 total
= isl_basic_map_total_dim(bmap
);
10606 n_out
= isl_basic_map_dim(bmap
, isl_dim_out
);
10607 o_out
= isl_basic_map_offset(bmap
, isl_dim_out
);
10608 n_div
= isl_basic_map_dim(bmap
, isl_dim_div
);
10609 o_div
= isl_basic_map_offset(bmap
, isl_dim_div
);
10610 for (i
= 0; i
< bmap
->n_ineq
; ++i
) {
10611 if (!isl_int_abs_eq(bmap
->ineq
[i
][o_out
+ pos
], ctx
->one
))
10613 if (isl_seq_first_non_zero(bmap
->ineq
[i
] + o_out
+ pos
+ 1,
10614 n_out
- (pos
+ 1)) != -1)
10616 if (first_div_may_involve_output(bmap
, bmap
->ineq
[i
] + o_div
,
10619 for (j
= i
+ 1; j
< bmap
->n_ineq
; ++j
) {
10620 if (!isl_int_abs_eq(bmap
->ineq
[j
][o_out
+ pos
],
10623 if (!isl_seq_is_neg(bmap
->ineq
[i
] + 1,
10624 bmap
->ineq
[j
] + 1, total
))
10628 if (j
>= bmap
->n_ineq
)
10630 isl_int_add(bmap
->ineq
[i
][0],
10631 bmap
->ineq
[i
][0], bmap
->ineq
[j
][0]);
10632 less
= isl_int_abs_lt(bmap
->ineq
[i
][0], m
);
10633 isl_int_sub(bmap
->ineq
[i
][0],
10634 bmap
->ineq
[i
][0], bmap
->ineq
[j
][0]);
10637 if (isl_int_is_one(bmap
->ineq
[i
][o_out
+ pos
]))
10643 return bmap
->n_ineq
;
10646 /* Return the index of the equality of "bmap" that defines
10647 * the output dimension "pos" in terms of earlier dimensions.
10648 * The equality may also involve integer divisions, as long
10649 * as those integer divisions are defined in terms of
10650 * parameters or input dimensions.
10651 * In this case, *div is set to the number of integer divisions and
10652 * *ineq is set to the number of inequality constraints (provided
10653 * div and ineq are not NULL).
10655 * The equality may also involve a single integer division involving
10656 * the output dimensions (typically only output dimension "pos") as
10657 * long as the coefficient of output dimension "pos" is 1 or -1 and
10658 * there is a pair of constraints i >= l and i <= l + n, with i referring
10659 * to output dimension "pos", l an expression involving only earlier
10660 * dimensions and n smaller than the coefficient of the integer division
10661 * in the equality. In this case, the output dimension can be defined
10662 * in terms of a modulo expression that does not involve the integer division.
10663 * *div is then set to this single integer division and
10664 * *ineq is set to the index of constraint i >= l.
10666 * Return bmap->n_eq if there is no such equality.
10667 * Return -1 on error.
10669 int isl_basic_map_output_defining_equality(__isl_keep isl_basic_map
*bmap
,
10670 int pos
, int *div
, int *ineq
)
10673 unsigned n_out
, o_out
;
10674 unsigned n_div
, o_div
;
10679 n_out
= isl_basic_map_dim(bmap
, isl_dim_out
);
10680 o_out
= isl_basic_map_offset(bmap
, isl_dim_out
);
10681 n_div
= isl_basic_map_dim(bmap
, isl_dim_div
);
10682 o_div
= isl_basic_map_offset(bmap
, isl_dim_div
);
10685 *ineq
= bmap
->n_ineq
;
10688 for (j
= 0; j
< bmap
->n_eq
; ++j
) {
10689 if (isl_int_is_zero(bmap
->eq
[j
][o_out
+ pos
]))
10691 if (isl_seq_first_non_zero(bmap
->eq
[j
] + o_out
+ pos
+ 1,
10692 n_out
- (pos
+ 1)) != -1)
10694 k
= first_div_may_involve_output(bmap
, bmap
->eq
[j
] + o_div
,
10698 if (!isl_int_is_one(bmap
->eq
[j
][o_out
+ pos
]) &&
10699 !isl_int_is_negone(bmap
->eq
[j
][o_out
+ pos
]))
10701 if (first_div_may_involve_output(bmap
, bmap
->eq
[j
] + o_div
,
10702 k
+ 1, n_div
- (k
+1)) < n_div
)
10704 l
= find_modulo_constraint_pair(bmap
, pos
,
10705 bmap
->eq
[j
][o_div
+ k
]);
10708 if (l
>= bmap
->n_ineq
)
10720 /* Check if the given basic map is obviously single-valued.
10721 * In particular, for each output dimension, check that there is
10722 * an equality that defines the output dimension in terms of
10723 * earlier dimensions.
10725 isl_bool
isl_basic_map_plain_is_single_valued(__isl_keep isl_basic_map
*bmap
)
10731 return isl_bool_error
;
10733 n_out
= isl_basic_map_dim(bmap
, isl_dim_out
);
10735 for (i
= 0; i
< n_out
; ++i
) {
10738 eq
= isl_basic_map_output_defining_equality(bmap
, i
,
10741 return isl_bool_error
;
10742 if (eq
>= bmap
->n_eq
)
10743 return isl_bool_false
;
10746 return isl_bool_true
;
10749 /* Check if the given basic map is single-valued.
10750 * We simply compute
10754 * and check if the result is a subset of the identity mapping.
10756 isl_bool
isl_basic_map_is_single_valued(__isl_keep isl_basic_map
*bmap
)
10759 isl_basic_map
*test
;
10763 sv
= isl_basic_map_plain_is_single_valued(bmap
);
10767 test
= isl_basic_map_reverse(isl_basic_map_copy(bmap
));
10768 test
= isl_basic_map_apply_range(test
, isl_basic_map_copy(bmap
));
10770 space
= isl_basic_map_get_space(bmap
);
10771 space
= isl_space_map_from_set(isl_space_range(space
));
10772 id
= isl_basic_map_identity(space
);
10774 sv
= isl_basic_map_is_subset(test
, id
);
10776 isl_basic_map_free(test
);
10777 isl_basic_map_free(id
);
10782 /* Check if the given map is obviously single-valued.
10784 isl_bool
isl_map_plain_is_single_valued(__isl_keep isl_map
*map
)
10787 return isl_bool_error
;
10789 return isl_bool_true
;
10791 return isl_bool_false
;
10793 return isl_basic_map_plain_is_single_valued(map
->p
[0]);
10796 /* Check if the given map is single-valued.
10797 * We simply compute
10801 * and check if the result is a subset of the identity mapping.
10803 isl_bool
isl_map_is_single_valued(__isl_keep isl_map
*map
)
10810 sv
= isl_map_plain_is_single_valued(map
);
10814 test
= isl_map_reverse(isl_map_copy(map
));
10815 test
= isl_map_apply_range(test
, isl_map_copy(map
));
10817 dim
= isl_space_map_from_set(isl_space_range(isl_map_get_space(map
)));
10818 id
= isl_map_identity(dim
);
10820 sv
= isl_map_is_subset(test
, id
);
10822 isl_map_free(test
);
10828 isl_bool
isl_map_is_injective(__isl_keep isl_map
*map
)
10832 map
= isl_map_copy(map
);
10833 map
= isl_map_reverse(map
);
10834 in
= isl_map_is_single_valued(map
);
10840 /* Check if the given map is obviously injective.
10842 isl_bool
isl_map_plain_is_injective(__isl_keep isl_map
*map
)
10846 map
= isl_map_copy(map
);
10847 map
= isl_map_reverse(map
);
10848 in
= isl_map_plain_is_single_valued(map
);
10854 isl_bool
isl_map_is_bijective(__isl_keep isl_map
*map
)
10858 sv
= isl_map_is_single_valued(map
);
10862 return isl_map_is_injective(map
);
10865 isl_bool
isl_set_is_singleton(__isl_keep isl_set
*set
)
10867 return isl_map_is_single_valued((isl_map
*)set
);
10870 /* Does "map" only map elements to themselves?
10872 * If the domain and range spaces are different, then "map"
10873 * is considered not to be an identity relation, even if it is empty.
10874 * Otherwise, construct the maximal identity relation and
10875 * check whether "map" is a subset of this relation.
10877 isl_bool
isl_map_is_identity(__isl_keep isl_map
*map
)
10881 isl_bool equal
, is_identity
;
10883 space
= isl_map_get_space(map
);
10884 equal
= isl_space_tuple_is_equal(space
, isl_dim_in
, space
, isl_dim_out
);
10885 isl_space_free(space
);
10886 if (equal
< 0 || !equal
)
10889 id
= isl_map_identity(isl_map_get_space(map
));
10890 is_identity
= isl_map_is_subset(map
, id
);
10893 return is_identity
;
10896 int isl_map_is_translation(__isl_keep isl_map
*map
)
10901 delta
= isl_map_deltas(isl_map_copy(map
));
10902 ok
= isl_set_is_singleton(delta
);
10903 isl_set_free(delta
);
10908 static int unique(isl_int
*p
, unsigned pos
, unsigned len
)
10910 if (isl_seq_first_non_zero(p
, pos
) != -1)
10912 if (isl_seq_first_non_zero(p
+ pos
+ 1, len
- pos
- 1) != -1)
10917 int isl_basic_set_is_box(__isl_keep isl_basic_set
*bset
)
10926 if (isl_basic_set_dim(bset
, isl_dim_div
) != 0)
10929 nvar
= isl_basic_set_dim(bset
, isl_dim_set
);
10930 ovar
= isl_space_offset(bset
->dim
, isl_dim_set
);
10931 for (j
= 0; j
< nvar
; ++j
) {
10932 int lower
= 0, upper
= 0;
10933 for (i
= 0; i
< bset
->n_eq
; ++i
) {
10934 if (isl_int_is_zero(bset
->eq
[i
][1 + ovar
+ j
]))
10936 if (!unique(bset
->eq
[i
] + 1 + ovar
, j
, nvar
))
10940 if (i
< bset
->n_eq
)
10942 for (i
= 0; i
< bset
->n_ineq
; ++i
) {
10943 if (isl_int_is_zero(bset
->ineq
[i
][1 + ovar
+ j
]))
10945 if (!unique(bset
->ineq
[i
] + 1 + ovar
, j
, nvar
))
10947 if (isl_int_is_pos(bset
->ineq
[i
][1 + ovar
+ j
]))
10952 if (!lower
|| !upper
)
10959 int isl_set_is_box(__isl_keep isl_set
*set
)
10966 return isl_basic_set_is_box(set
->p
[0]);
10969 isl_bool
isl_basic_set_is_wrapping(__isl_keep isl_basic_set
*bset
)
10972 return isl_bool_error
;
10974 return isl_space_is_wrapping(bset
->dim
);
10977 isl_bool
isl_set_is_wrapping(__isl_keep isl_set
*set
)
10980 return isl_bool_error
;
10982 return isl_space_is_wrapping(set
->dim
);
10985 /* Modify the space of "map" through a call to "change".
10986 * If "can_change" is set (not NULL), then first call it to check
10987 * if the modification is allowed, printing the error message "cannot_change"
10990 static __isl_give isl_map
*isl_map_change_space(__isl_take isl_map
*map
,
10991 isl_bool (*can_change
)(__isl_keep isl_map
*map
),
10992 const char *cannot_change
,
10993 __isl_give isl_space
*(*change
)(__isl_take isl_space
*space
))
11001 ok
= can_change
? can_change(map
) : isl_bool_true
;
11003 return isl_map_free(map
);
11005 isl_die(isl_map_get_ctx(map
), isl_error_invalid
, cannot_change
,
11006 return isl_map_free(map
));
11008 space
= change(isl_map_get_space(map
));
11009 map
= isl_map_reset_space(map
, space
);
11014 /* Is the domain of "map" a wrapped relation?
11016 isl_bool
isl_map_domain_is_wrapping(__isl_keep isl_map
*map
)
11019 return isl_bool_error
;
11021 return isl_space_domain_is_wrapping(map
->dim
);
11024 /* Is the range of "map" a wrapped relation?
11026 isl_bool
isl_map_range_is_wrapping(__isl_keep isl_map
*map
)
11029 return isl_bool_error
;
11031 return isl_space_range_is_wrapping(map
->dim
);
11034 __isl_give isl_basic_set
*isl_basic_map_wrap(__isl_take isl_basic_map
*bmap
)
11036 bmap
= isl_basic_map_cow(bmap
);
11040 bmap
->dim
= isl_space_wrap(bmap
->dim
);
11044 bmap
= isl_basic_map_finalize(bmap
);
11046 return (isl_basic_set
*)bmap
;
11048 isl_basic_map_free(bmap
);
11052 /* Given a map A -> B, return the set (A -> B).
11054 __isl_give isl_set
*isl_map_wrap(__isl_take isl_map
*map
)
11056 return isl_map_change_space(map
, NULL
, NULL
, &isl_space_wrap
);
11059 __isl_give isl_basic_map
*isl_basic_set_unwrap(__isl_take isl_basic_set
*bset
)
11061 bset
= isl_basic_set_cow(bset
);
11065 bset
->dim
= isl_space_unwrap(bset
->dim
);
11069 bset
= isl_basic_set_finalize(bset
);
11071 return (isl_basic_map
*)bset
;
11073 isl_basic_set_free(bset
);
11077 /* Given a set (A -> B), return the map A -> B.
11078 * Error out if "set" is not of the form (A -> B).
11080 __isl_give isl_map
*isl_set_unwrap(__isl_take isl_set
*set
)
11082 return isl_map_change_space(set
, &isl_set_is_wrapping
,
11083 "not a wrapping set", &isl_space_unwrap
);
11086 __isl_give isl_basic_map
*isl_basic_map_reset(__isl_take isl_basic_map
*bmap
,
11087 enum isl_dim_type type
)
11092 if (!isl_space_is_named_or_nested(bmap
->dim
, type
))
11095 bmap
= isl_basic_map_cow(bmap
);
11099 bmap
->dim
= isl_space_reset(bmap
->dim
, type
);
11103 bmap
= isl_basic_map_finalize(bmap
);
11107 isl_basic_map_free(bmap
);
11111 __isl_give isl_map
*isl_map_reset(__isl_take isl_map
*map
,
11112 enum isl_dim_type type
)
11119 if (!isl_space_is_named_or_nested(map
->dim
, type
))
11122 map
= isl_map_cow(map
);
11126 for (i
= 0; i
< map
->n
; ++i
) {
11127 map
->p
[i
] = isl_basic_map_reset(map
->p
[i
], type
);
11131 map
->dim
= isl_space_reset(map
->dim
, type
);
11141 __isl_give isl_basic_map
*isl_basic_map_flatten(__isl_take isl_basic_map
*bmap
)
11146 if (!bmap
->dim
->nested
[0] && !bmap
->dim
->nested
[1])
11149 bmap
= isl_basic_map_cow(bmap
);
11153 bmap
->dim
= isl_space_flatten(bmap
->dim
);
11157 bmap
= isl_basic_map_finalize(bmap
);
11161 isl_basic_map_free(bmap
);
11165 __isl_give isl_basic_set
*isl_basic_set_flatten(__isl_take isl_basic_set
*bset
)
11167 return (isl_basic_set
*)isl_basic_map_flatten((isl_basic_map
*)bset
);
11170 __isl_give isl_basic_map
*isl_basic_map_flatten_domain(
11171 __isl_take isl_basic_map
*bmap
)
11176 if (!bmap
->dim
->nested
[0])
11179 bmap
= isl_basic_map_cow(bmap
);
11183 bmap
->dim
= isl_space_flatten_domain(bmap
->dim
);
11187 bmap
= isl_basic_map_finalize(bmap
);
11191 isl_basic_map_free(bmap
);
11195 __isl_give isl_basic_map
*isl_basic_map_flatten_range(
11196 __isl_take isl_basic_map
*bmap
)
11201 if (!bmap
->dim
->nested
[1])
11204 bmap
= isl_basic_map_cow(bmap
);
11208 bmap
->dim
= isl_space_flatten_range(bmap
->dim
);
11212 bmap
= isl_basic_map_finalize(bmap
);
11216 isl_basic_map_free(bmap
);
11220 /* Remove any internal structure from the spaces of domain and range of "map".
11222 __isl_give isl_map
*isl_map_flatten(__isl_take isl_map
*map
)
11227 if (!map
->dim
->nested
[0] && !map
->dim
->nested
[1])
11230 return isl_map_change_space(map
, NULL
, NULL
, &isl_space_flatten
);
11233 __isl_give isl_set
*isl_set_flatten(__isl_take isl_set
*set
)
11235 return (isl_set
*)isl_map_flatten((isl_map
*)set
);
11238 __isl_give isl_map
*isl_set_flatten_map(__isl_take isl_set
*set
)
11240 isl_space
*dim
, *flat_dim
;
11243 dim
= isl_set_get_space(set
);
11244 flat_dim
= isl_space_flatten(isl_space_copy(dim
));
11245 map
= isl_map_identity(isl_space_join(isl_space_reverse(dim
), flat_dim
));
11246 map
= isl_map_intersect_domain(map
, set
);
11251 /* Remove any internal structure from the space of the domain of "map".
11253 __isl_give isl_map
*isl_map_flatten_domain(__isl_take isl_map
*map
)
11258 if (!map
->dim
->nested
[0])
11261 return isl_map_change_space(map
, NULL
, NULL
, &isl_space_flatten_domain
);
11264 /* Remove any internal structure from the space of the range of "map".
11266 __isl_give isl_map
*isl_map_flatten_range(__isl_take isl_map
*map
)
11271 if (!map
->dim
->nested
[1])
11274 return isl_map_change_space(map
, NULL
, NULL
, &isl_space_flatten_range
);
11277 /* Reorder the dimensions of "bmap" according to the given dim_map
11278 * and set the dimension specification to "dim" and
11279 * perform Gaussian elimination on the result.
11281 __isl_give isl_basic_map
*isl_basic_map_realign(__isl_take isl_basic_map
*bmap
,
11282 __isl_take isl_space
*dim
, __isl_take
struct isl_dim_map
*dim_map
)
11284 isl_basic_map
*res
;
11287 bmap
= isl_basic_map_cow(bmap
);
11288 if (!bmap
|| !dim
|| !dim_map
)
11291 flags
= bmap
->flags
;
11292 ISL_FL_CLR(flags
, ISL_BASIC_MAP_FINAL
);
11293 ISL_FL_CLR(flags
, ISL_BASIC_MAP_NORMALIZED
);
11294 ISL_FL_CLR(flags
, ISL_BASIC_MAP_NORMALIZED_DIVS
);
11295 res
= isl_basic_map_alloc_space(dim
,
11296 bmap
->n_div
, bmap
->n_eq
, bmap
->n_ineq
);
11297 res
= isl_basic_map_add_constraints_dim_map(res
, bmap
, dim_map
);
11299 res
->flags
= flags
;
11300 res
= isl_basic_map_gauss(res
, NULL
);
11301 res
= isl_basic_map_finalize(res
);
11305 isl_basic_map_free(bmap
);
11306 isl_space_free(dim
);
11310 /* Reorder the dimensions of "map" according to given reordering.
11312 __isl_give isl_map
*isl_map_realign(__isl_take isl_map
*map
,
11313 __isl_take isl_reordering
*r
)
11316 struct isl_dim_map
*dim_map
;
11318 map
= isl_map_cow(map
);
11319 dim_map
= isl_dim_map_from_reordering(r
);
11320 if (!map
|| !r
|| !dim_map
)
11323 for (i
= 0; i
< map
->n
; ++i
) {
11324 struct isl_dim_map
*dim_map_i
;
11326 dim_map_i
= isl_dim_map_extend(dim_map
, map
->p
[i
]);
11328 map
->p
[i
] = isl_basic_map_realign(map
->p
[i
],
11329 isl_space_copy(r
->dim
), dim_map_i
);
11335 map
= isl_map_reset_space(map
, isl_space_copy(r
->dim
));
11337 isl_reordering_free(r
);
11343 isl_reordering_free(r
);
11347 __isl_give isl_set
*isl_set_realign(__isl_take isl_set
*set
,
11348 __isl_take isl_reordering
*r
)
11350 return (isl_set
*)isl_map_realign((isl_map
*)set
, r
);
11353 __isl_give isl_map
*isl_map_align_params(__isl_take isl_map
*map
,
11354 __isl_take isl_space
*model
)
11358 if (!map
|| !model
)
11361 ctx
= isl_space_get_ctx(model
);
11362 if (!isl_space_has_named_params(model
))
11363 isl_die(ctx
, isl_error_invalid
,
11364 "model has unnamed parameters", goto error
);
11365 if (!isl_space_has_named_params(map
->dim
))
11366 isl_die(ctx
, isl_error_invalid
,
11367 "relation has unnamed parameters", goto error
);
11368 if (!isl_space_match(map
->dim
, isl_dim_param
, model
, isl_dim_param
)) {
11369 isl_reordering
*exp
;
11371 model
= isl_space_drop_dims(model
, isl_dim_in
,
11372 0, isl_space_dim(model
, isl_dim_in
));
11373 model
= isl_space_drop_dims(model
, isl_dim_out
,
11374 0, isl_space_dim(model
, isl_dim_out
));
11375 exp
= isl_parameter_alignment_reordering(map
->dim
, model
);
11376 exp
= isl_reordering_extend_space(exp
, isl_map_get_space(map
));
11377 map
= isl_map_realign(map
, exp
);
11380 isl_space_free(model
);
11383 isl_space_free(model
);
11388 __isl_give isl_set
*isl_set_align_params(__isl_take isl_set
*set
,
11389 __isl_take isl_space
*model
)
11391 return isl_map_align_params(set
, model
);
11394 /* Align the parameters of "bmap" to those of "model", introducing
11395 * additional parameters if needed.
11397 __isl_give isl_basic_map
*isl_basic_map_align_params(
11398 __isl_take isl_basic_map
*bmap
, __isl_take isl_space
*model
)
11402 if (!bmap
|| !model
)
11405 ctx
= isl_space_get_ctx(model
);
11406 if (!isl_space_has_named_params(model
))
11407 isl_die(ctx
, isl_error_invalid
,
11408 "model has unnamed parameters", goto error
);
11409 if (!isl_space_has_named_params(bmap
->dim
))
11410 isl_die(ctx
, isl_error_invalid
,
11411 "relation has unnamed parameters", goto error
);
11412 if (!isl_space_match(bmap
->dim
, isl_dim_param
, model
, isl_dim_param
)) {
11413 isl_reordering
*exp
;
11414 struct isl_dim_map
*dim_map
;
11416 model
= isl_space_drop_dims(model
, isl_dim_in
,
11417 0, isl_space_dim(model
, isl_dim_in
));
11418 model
= isl_space_drop_dims(model
, isl_dim_out
,
11419 0, isl_space_dim(model
, isl_dim_out
));
11420 exp
= isl_parameter_alignment_reordering(bmap
->dim
, model
);
11421 exp
= isl_reordering_extend_space(exp
,
11422 isl_basic_map_get_space(bmap
));
11423 dim_map
= isl_dim_map_from_reordering(exp
);
11424 bmap
= isl_basic_map_realign(bmap
,
11425 exp
? isl_space_copy(exp
->dim
) : NULL
,
11426 isl_dim_map_extend(dim_map
, bmap
));
11427 isl_reordering_free(exp
);
11431 isl_space_free(model
);
11434 isl_space_free(model
);
11435 isl_basic_map_free(bmap
);
11439 /* Align the parameters of "bset" to those of "model", introducing
11440 * additional parameters if needed.
11442 __isl_give isl_basic_set
*isl_basic_set_align_params(
11443 __isl_take isl_basic_set
*bset
, __isl_take isl_space
*model
)
11445 return isl_basic_map_align_params(bset
, model
);
11448 __isl_give isl_mat
*isl_basic_map_equalities_matrix(
11449 __isl_keep isl_basic_map
*bmap
, enum isl_dim_type c1
,
11450 enum isl_dim_type c2
, enum isl_dim_type c3
,
11451 enum isl_dim_type c4
, enum isl_dim_type c5
)
11453 enum isl_dim_type c
[5] = { c1
, c2
, c3
, c4
, c5
};
11454 struct isl_mat
*mat
;
11460 mat
= isl_mat_alloc(bmap
->ctx
, bmap
->n_eq
,
11461 isl_basic_map_total_dim(bmap
) + 1);
11464 for (i
= 0; i
< bmap
->n_eq
; ++i
)
11465 for (j
= 0, pos
= 0; j
< 5; ++j
) {
11466 int off
= isl_basic_map_offset(bmap
, c
[j
]);
11467 for (k
= 0; k
< isl_basic_map_dim(bmap
, c
[j
]); ++k
) {
11468 isl_int_set(mat
->row
[i
][pos
],
11469 bmap
->eq
[i
][off
+ k
]);
11477 __isl_give isl_mat
*isl_basic_map_inequalities_matrix(
11478 __isl_keep isl_basic_map
*bmap
, enum isl_dim_type c1
,
11479 enum isl_dim_type c2
, enum isl_dim_type c3
,
11480 enum isl_dim_type c4
, enum isl_dim_type c5
)
11482 enum isl_dim_type c
[5] = { c1
, c2
, c3
, c4
, c5
};
11483 struct isl_mat
*mat
;
11489 mat
= isl_mat_alloc(bmap
->ctx
, bmap
->n_ineq
,
11490 isl_basic_map_total_dim(bmap
) + 1);
11493 for (i
= 0; i
< bmap
->n_ineq
; ++i
)
11494 for (j
= 0, pos
= 0; j
< 5; ++j
) {
11495 int off
= isl_basic_map_offset(bmap
, c
[j
]);
11496 for (k
= 0; k
< isl_basic_map_dim(bmap
, c
[j
]); ++k
) {
11497 isl_int_set(mat
->row
[i
][pos
],
11498 bmap
->ineq
[i
][off
+ k
]);
11506 __isl_give isl_basic_map
*isl_basic_map_from_constraint_matrices(
11507 __isl_take isl_space
*dim
,
11508 __isl_take isl_mat
*eq
, __isl_take isl_mat
*ineq
, enum isl_dim_type c1
,
11509 enum isl_dim_type c2
, enum isl_dim_type c3
,
11510 enum isl_dim_type c4
, enum isl_dim_type c5
)
11512 enum isl_dim_type c
[5] = { c1
, c2
, c3
, c4
, c5
};
11513 isl_basic_map
*bmap
;
11519 if (!dim
|| !eq
|| !ineq
)
11522 if (eq
->n_col
!= ineq
->n_col
)
11523 isl_die(dim
->ctx
, isl_error_invalid
,
11524 "equalities and inequalities matrices should have "
11525 "same number of columns", goto error
);
11527 total
= 1 + isl_space_dim(dim
, isl_dim_all
);
11529 if (eq
->n_col
< total
)
11530 isl_die(dim
->ctx
, isl_error_invalid
,
11531 "number of columns too small", goto error
);
11533 extra
= eq
->n_col
- total
;
11535 bmap
= isl_basic_map_alloc_space(isl_space_copy(dim
), extra
,
11536 eq
->n_row
, ineq
->n_row
);
11539 for (i
= 0; i
< extra
; ++i
) {
11540 k
= isl_basic_map_alloc_div(bmap
);
11543 isl_int_set_si(bmap
->div
[k
][0], 0);
11545 for (i
= 0; i
< eq
->n_row
; ++i
) {
11546 l
= isl_basic_map_alloc_equality(bmap
);
11549 for (j
= 0, pos
= 0; j
< 5; ++j
) {
11550 int off
= isl_basic_map_offset(bmap
, c
[j
]);
11551 for (k
= 0; k
< isl_basic_map_dim(bmap
, c
[j
]); ++k
) {
11552 isl_int_set(bmap
->eq
[l
][off
+ k
],
11558 for (i
= 0; i
< ineq
->n_row
; ++i
) {
11559 l
= isl_basic_map_alloc_inequality(bmap
);
11562 for (j
= 0, pos
= 0; j
< 5; ++j
) {
11563 int off
= isl_basic_map_offset(bmap
, c
[j
]);
11564 for (k
= 0; k
< isl_basic_map_dim(bmap
, c
[j
]); ++k
) {
11565 isl_int_set(bmap
->ineq
[l
][off
+ k
],
11566 ineq
->row
[i
][pos
]);
11572 isl_space_free(dim
);
11574 isl_mat_free(ineq
);
11576 bmap
= isl_basic_map_simplify(bmap
);
11577 return isl_basic_map_finalize(bmap
);
11579 isl_space_free(dim
);
11581 isl_mat_free(ineq
);
11585 __isl_give isl_mat
*isl_basic_set_equalities_matrix(
11586 __isl_keep isl_basic_set
*bset
, enum isl_dim_type c1
,
11587 enum isl_dim_type c2
, enum isl_dim_type c3
, enum isl_dim_type c4
)
11589 return isl_basic_map_equalities_matrix((isl_basic_map
*)bset
,
11590 c1
, c2
, c3
, c4
, isl_dim_in
);
11593 __isl_give isl_mat
*isl_basic_set_inequalities_matrix(
11594 __isl_keep isl_basic_set
*bset
, enum isl_dim_type c1
,
11595 enum isl_dim_type c2
, enum isl_dim_type c3
, enum isl_dim_type c4
)
11597 return isl_basic_map_inequalities_matrix((isl_basic_map
*)bset
,
11598 c1
, c2
, c3
, c4
, isl_dim_in
);
11601 __isl_give isl_basic_set
*isl_basic_set_from_constraint_matrices(
11602 __isl_take isl_space
*dim
,
11603 __isl_take isl_mat
*eq
, __isl_take isl_mat
*ineq
, enum isl_dim_type c1
,
11604 enum isl_dim_type c2
, enum isl_dim_type c3
, enum isl_dim_type c4
)
11606 return (isl_basic_set
*)
11607 isl_basic_map_from_constraint_matrices(dim
, eq
, ineq
,
11608 c1
, c2
, c3
, c4
, isl_dim_in
);
11611 isl_bool
isl_basic_map_can_zip(__isl_keep isl_basic_map
*bmap
)
11614 return isl_bool_error
;
11616 return isl_space_can_zip(bmap
->dim
);
11619 isl_bool
isl_map_can_zip(__isl_keep isl_map
*map
)
11622 return isl_bool_error
;
11624 return isl_space_can_zip(map
->dim
);
11627 /* Given a basic map (A -> B) -> (C -> D), return the corresponding basic map
11628 * (A -> C) -> (B -> D).
11630 __isl_give isl_basic_map
*isl_basic_map_zip(__isl_take isl_basic_map
*bmap
)
11639 if (!isl_basic_map_can_zip(bmap
))
11640 isl_die(bmap
->ctx
, isl_error_invalid
,
11641 "basic map cannot be zipped", goto error
);
11642 pos
= isl_basic_map_offset(bmap
, isl_dim_in
) +
11643 isl_space_dim(bmap
->dim
->nested
[0], isl_dim_in
);
11644 n1
= isl_space_dim(bmap
->dim
->nested
[0], isl_dim_out
);
11645 n2
= isl_space_dim(bmap
->dim
->nested
[1], isl_dim_in
);
11646 bmap
= isl_basic_map_cow(bmap
);
11647 bmap
= isl_basic_map_swap_vars(bmap
, pos
, n1
, n2
);
11650 bmap
->dim
= isl_space_zip(bmap
->dim
);
11653 bmap
= isl_basic_map_mark_final(bmap
);
11656 isl_basic_map_free(bmap
);
11660 /* Given a map (A -> B) -> (C -> D), return the corresponding map
11661 * (A -> C) -> (B -> D).
11663 __isl_give isl_map
*isl_map_zip(__isl_take isl_map
*map
)
11670 if (!isl_map_can_zip(map
))
11671 isl_die(map
->ctx
, isl_error_invalid
, "map cannot be zipped",
11674 map
= isl_map_cow(map
);
11678 for (i
= 0; i
< map
->n
; ++i
) {
11679 map
->p
[i
] = isl_basic_map_zip(map
->p
[i
]);
11684 map
->dim
= isl_space_zip(map
->dim
);
11694 /* Can we apply isl_basic_map_curry to "bmap"?
11695 * That is, does it have a nested relation in its domain?
11697 isl_bool
isl_basic_map_can_curry(__isl_keep isl_basic_map
*bmap
)
11700 return isl_bool_error
;
11702 return isl_space_can_curry(bmap
->dim
);
11705 /* Can we apply isl_map_curry to "map"?
11706 * That is, does it have a nested relation in its domain?
11708 isl_bool
isl_map_can_curry(__isl_keep isl_map
*map
)
11711 return isl_bool_error
;
11713 return isl_space_can_curry(map
->dim
);
11716 /* Given a basic map (A -> B) -> C, return the corresponding basic map
11719 __isl_give isl_basic_map
*isl_basic_map_curry(__isl_take isl_basic_map
*bmap
)
11725 if (!isl_basic_map_can_curry(bmap
))
11726 isl_die(bmap
->ctx
, isl_error_invalid
,
11727 "basic map cannot be curried", goto error
);
11728 bmap
= isl_basic_map_cow(bmap
);
11731 bmap
->dim
= isl_space_curry(bmap
->dim
);
11734 bmap
= isl_basic_map_mark_final(bmap
);
11737 isl_basic_map_free(bmap
);
11741 /* Given a map (A -> B) -> C, return the corresponding map
11744 __isl_give isl_map
*isl_map_curry(__isl_take isl_map
*map
)
11746 return isl_map_change_space(map
, &isl_map_can_curry
,
11747 "map cannot be curried", &isl_space_curry
);
11750 /* Can isl_map_range_curry be applied to "map"?
11751 * That is, does it have a nested relation in its range,
11752 * the domain of which is itself a nested relation?
11754 isl_bool
isl_map_can_range_curry(__isl_keep isl_map
*map
)
11757 return isl_bool_error
;
11759 return isl_space_can_range_curry(map
->dim
);
11762 /* Given a map A -> ((B -> C) -> D), return the corresponding map
11763 * A -> (B -> (C -> D)).
11765 __isl_give isl_map
*isl_map_range_curry(__isl_take isl_map
*map
)
11767 return isl_map_change_space(map
, &isl_map_can_range_curry
,
11768 "map range cannot be curried",
11769 &isl_space_range_curry
);
11772 /* Can we apply isl_basic_map_uncurry to "bmap"?
11773 * That is, does it have a nested relation in its domain?
11775 isl_bool
isl_basic_map_can_uncurry(__isl_keep isl_basic_map
*bmap
)
11778 return isl_bool_error
;
11780 return isl_space_can_uncurry(bmap
->dim
);
11783 /* Can we apply isl_map_uncurry to "map"?
11784 * That is, does it have a nested relation in its domain?
11786 isl_bool
isl_map_can_uncurry(__isl_keep isl_map
*map
)
11789 return isl_bool_error
;
11791 return isl_space_can_uncurry(map
->dim
);
11794 /* Given a basic map A -> (B -> C), return the corresponding basic map
11797 __isl_give isl_basic_map
*isl_basic_map_uncurry(__isl_take isl_basic_map
*bmap
)
11803 if (!isl_basic_map_can_uncurry(bmap
))
11804 isl_die(bmap
->ctx
, isl_error_invalid
,
11805 "basic map cannot be uncurried",
11806 return isl_basic_map_free(bmap
));
11807 bmap
= isl_basic_map_cow(bmap
);
11810 bmap
->dim
= isl_space_uncurry(bmap
->dim
);
11812 return isl_basic_map_free(bmap
);
11813 bmap
= isl_basic_map_mark_final(bmap
);
11817 /* Given a map A -> (B -> C), return the corresponding map
11820 __isl_give isl_map
*isl_map_uncurry(__isl_take isl_map
*map
)
11822 return isl_map_change_space(map
, &isl_map_can_uncurry
,
11823 "map cannot be uncurried", &isl_space_uncurry
);
11826 /* Construct a basic map mapping the domain of the affine expression
11827 * to a one-dimensional range prescribed by the affine expression.
11829 * A NaN affine expression cannot be converted to a basic map.
11831 __isl_give isl_basic_map
*isl_basic_map_from_aff(__isl_take isl_aff
*aff
)
11836 isl_local_space
*ls
;
11837 isl_basic_map
*bmap
= NULL
;
11841 is_nan
= isl_aff_is_nan(aff
);
11845 isl_die(isl_aff_get_ctx(aff
), isl_error_invalid
,
11846 "cannot convert NaN", goto error
);
11848 ls
= isl_aff_get_local_space(aff
);
11849 bmap
= isl_basic_map_from_local_space(ls
);
11850 bmap
= isl_basic_map_extend_constraints(bmap
, 1, 0);
11851 k
= isl_basic_map_alloc_equality(bmap
);
11855 pos
= isl_basic_map_offset(bmap
, isl_dim_out
);
11856 isl_seq_cpy(bmap
->eq
[k
], aff
->v
->el
+ 1, pos
);
11857 isl_int_neg(bmap
->eq
[k
][pos
], aff
->v
->el
[0]);
11858 isl_seq_cpy(bmap
->eq
[k
] + pos
+ 1, aff
->v
->el
+ 1 + pos
,
11859 aff
->v
->size
- (pos
+ 1));
11862 bmap
= isl_basic_map_finalize(bmap
);
11866 isl_basic_map_free(bmap
);
11870 /* Construct a map mapping the domain of the affine expression
11871 * to a one-dimensional range prescribed by the affine expression.
11873 __isl_give isl_map
*isl_map_from_aff(__isl_take isl_aff
*aff
)
11875 isl_basic_map
*bmap
;
11877 bmap
= isl_basic_map_from_aff(aff
);
11878 return isl_map_from_basic_map(bmap
);
11881 /* Construct a basic map mapping the domain the multi-affine expression
11882 * to its range, with each dimension in the range equated to the
11883 * corresponding affine expression.
11885 __isl_give isl_basic_map
*isl_basic_map_from_multi_aff(
11886 __isl_take isl_multi_aff
*maff
)
11890 isl_basic_map
*bmap
;
11895 if (isl_space_dim(maff
->space
, isl_dim_out
) != maff
->n
)
11896 isl_die(isl_multi_aff_get_ctx(maff
), isl_error_internal
,
11897 "invalid space", goto error
);
11899 space
= isl_space_domain(isl_multi_aff_get_space(maff
));
11900 bmap
= isl_basic_map_universe(isl_space_from_domain(space
));
11902 for (i
= 0; i
< maff
->n
; ++i
) {
11904 isl_basic_map
*bmap_i
;
11906 aff
= isl_aff_copy(maff
->p
[i
]);
11907 bmap_i
= isl_basic_map_from_aff(aff
);
11909 bmap
= isl_basic_map_flat_range_product(bmap
, bmap_i
);
11912 bmap
= isl_basic_map_reset_space(bmap
, isl_multi_aff_get_space(maff
));
11914 isl_multi_aff_free(maff
);
11917 isl_multi_aff_free(maff
);
11921 /* Construct a map mapping the domain the multi-affine expression
11922 * to its range, with each dimension in the range equated to the
11923 * corresponding affine expression.
11925 __isl_give isl_map
*isl_map_from_multi_aff(__isl_take isl_multi_aff
*maff
)
11927 isl_basic_map
*bmap
;
11929 bmap
= isl_basic_map_from_multi_aff(maff
);
11930 return isl_map_from_basic_map(bmap
);
11933 /* Construct a basic map mapping a domain in the given space to
11934 * to an n-dimensional range, with n the number of elements in the list,
11935 * where each coordinate in the range is prescribed by the
11936 * corresponding affine expression.
11937 * The domains of all affine expressions in the list are assumed to match
11940 __isl_give isl_basic_map
*isl_basic_map_from_aff_list(
11941 __isl_take isl_space
*domain_dim
, __isl_take isl_aff_list
*list
)
11945 isl_basic_map
*bmap
;
11950 dim
= isl_space_from_domain(domain_dim
);
11951 bmap
= isl_basic_map_universe(dim
);
11953 for (i
= 0; i
< list
->n
; ++i
) {
11955 isl_basic_map
*bmap_i
;
11957 aff
= isl_aff_copy(list
->p
[i
]);
11958 bmap_i
= isl_basic_map_from_aff(aff
);
11960 bmap
= isl_basic_map_flat_range_product(bmap
, bmap_i
);
11963 isl_aff_list_free(list
);
11967 __isl_give isl_set
*isl_set_equate(__isl_take isl_set
*set
,
11968 enum isl_dim_type type1
, int pos1
, enum isl_dim_type type2
, int pos2
)
11970 return isl_map_equate(set
, type1
, pos1
, type2
, pos2
);
11973 /* Construct a basic map where the given dimensions are equal to each other.
11975 static __isl_give isl_basic_map
*equator(__isl_take isl_space
*space
,
11976 enum isl_dim_type type1
, int pos1
, enum isl_dim_type type2
, int pos2
)
11978 isl_basic_map
*bmap
= NULL
;
11984 if (pos1
>= isl_space_dim(space
, type1
))
11985 isl_die(isl_space_get_ctx(space
), isl_error_invalid
,
11986 "index out of bounds", goto error
);
11987 if (pos2
>= isl_space_dim(space
, type2
))
11988 isl_die(isl_space_get_ctx(space
), isl_error_invalid
,
11989 "index out of bounds", goto error
);
11991 if (type1
== type2
&& pos1
== pos2
)
11992 return isl_basic_map_universe(space
);
11994 bmap
= isl_basic_map_alloc_space(isl_space_copy(space
), 0, 1, 0);
11995 i
= isl_basic_map_alloc_equality(bmap
);
11998 isl_seq_clr(bmap
->eq
[i
], 1 + isl_basic_map_total_dim(bmap
));
11999 pos1
+= isl_basic_map_offset(bmap
, type1
);
12000 pos2
+= isl_basic_map_offset(bmap
, type2
);
12001 isl_int_set_si(bmap
->eq
[i
][pos1
], -1);
12002 isl_int_set_si(bmap
->eq
[i
][pos2
], 1);
12003 bmap
= isl_basic_map_finalize(bmap
);
12004 isl_space_free(space
);
12007 isl_space_free(space
);
12008 isl_basic_map_free(bmap
);
12012 /* Add a constraint imposing that the given two dimensions are equal.
12014 __isl_give isl_basic_map
*isl_basic_map_equate(__isl_take isl_basic_map
*bmap
,
12015 enum isl_dim_type type1
, int pos1
, enum isl_dim_type type2
, int pos2
)
12019 eq
= equator(isl_basic_map_get_space(bmap
), type1
, pos1
, type2
, pos2
);
12021 bmap
= isl_basic_map_intersect(bmap
, eq
);
12026 /* Add a constraint imposing that the given two dimensions are equal.
12028 __isl_give isl_map
*isl_map_equate(__isl_take isl_map
*map
,
12029 enum isl_dim_type type1
, int pos1
, enum isl_dim_type type2
, int pos2
)
12031 isl_basic_map
*bmap
;
12033 bmap
= equator(isl_map_get_space(map
), type1
, pos1
, type2
, pos2
);
12035 map
= isl_map_intersect(map
, isl_map_from_basic_map(bmap
));
12040 /* Add a constraint imposing that the given two dimensions have opposite values.
12042 __isl_give isl_map
*isl_map_oppose(__isl_take isl_map
*map
,
12043 enum isl_dim_type type1
, int pos1
, enum isl_dim_type type2
, int pos2
)
12045 isl_basic_map
*bmap
= NULL
;
12051 if (pos1
>= isl_map_dim(map
, type1
))
12052 isl_die(map
->ctx
, isl_error_invalid
,
12053 "index out of bounds", goto error
);
12054 if (pos2
>= isl_map_dim(map
, type2
))
12055 isl_die(map
->ctx
, isl_error_invalid
,
12056 "index out of bounds", goto error
);
12058 bmap
= isl_basic_map_alloc_space(isl_map_get_space(map
), 0, 1, 0);
12059 i
= isl_basic_map_alloc_equality(bmap
);
12062 isl_seq_clr(bmap
->eq
[i
], 1 + isl_basic_map_total_dim(bmap
));
12063 pos1
+= isl_basic_map_offset(bmap
, type1
);
12064 pos2
+= isl_basic_map_offset(bmap
, type2
);
12065 isl_int_set_si(bmap
->eq
[i
][pos1
], 1);
12066 isl_int_set_si(bmap
->eq
[i
][pos2
], 1);
12067 bmap
= isl_basic_map_finalize(bmap
);
12069 map
= isl_map_intersect(map
, isl_map_from_basic_map(bmap
));
12073 isl_basic_map_free(bmap
);
12078 /* Construct a constraint imposing that the value of the first dimension is
12079 * greater than or equal to that of the second.
12081 static __isl_give isl_constraint
*constraint_order_ge(
12082 __isl_take isl_space
*space
, enum isl_dim_type type1
, int pos1
,
12083 enum isl_dim_type type2
, int pos2
)
12090 c
= isl_constraint_alloc_inequality(isl_local_space_from_space(space
));
12092 if (pos1
>= isl_constraint_dim(c
, type1
))
12093 isl_die(isl_constraint_get_ctx(c
), isl_error_invalid
,
12094 "index out of bounds", return isl_constraint_free(c
));
12095 if (pos2
>= isl_constraint_dim(c
, type2
))
12096 isl_die(isl_constraint_get_ctx(c
), isl_error_invalid
,
12097 "index out of bounds", return isl_constraint_free(c
));
12099 if (type1
== type2
&& pos1
== pos2
)
12102 c
= isl_constraint_set_coefficient_si(c
, type1
, pos1
, 1);
12103 c
= isl_constraint_set_coefficient_si(c
, type2
, pos2
, -1);
12108 /* Add a constraint imposing that the value of the first dimension is
12109 * greater than or equal to that of the second.
12111 __isl_give isl_basic_map
*isl_basic_map_order_ge(__isl_take isl_basic_map
*bmap
,
12112 enum isl_dim_type type1
, int pos1
, enum isl_dim_type type2
, int pos2
)
12117 if (type1
== type2
&& pos1
== pos2
)
12119 space
= isl_basic_map_get_space(bmap
);
12120 c
= constraint_order_ge(space
, type1
, pos1
, type2
, pos2
);
12121 bmap
= isl_basic_map_add_constraint(bmap
, c
);
12126 /* Add a constraint imposing that the value of the first dimension is
12127 * greater than or equal to that of the second.
12129 __isl_give isl_map
*isl_map_order_ge(__isl_take isl_map
*map
,
12130 enum isl_dim_type type1
, int pos1
, enum isl_dim_type type2
, int pos2
)
12135 if (type1
== type2
&& pos1
== pos2
)
12137 space
= isl_map_get_space(map
);
12138 c
= constraint_order_ge(space
, type1
, pos1
, type2
, pos2
);
12139 map
= isl_map_add_constraint(map
, c
);
12144 /* Add a constraint imposing that the value of the first dimension is
12145 * less than or equal to that of the second.
12147 __isl_give isl_map
*isl_map_order_le(__isl_take isl_map
*map
,
12148 enum isl_dim_type type1
, int pos1
, enum isl_dim_type type2
, int pos2
)
12150 return isl_map_order_ge(map
, type2
, pos2
, type1
, pos1
);
12153 /* Construct a basic map where the value of the first dimension is
12154 * greater than that of the second.
12156 static __isl_give isl_basic_map
*greator(__isl_take isl_space
*space
,
12157 enum isl_dim_type type1
, int pos1
, enum isl_dim_type type2
, int pos2
)
12159 isl_basic_map
*bmap
= NULL
;
12165 if (pos1
>= isl_space_dim(space
, type1
))
12166 isl_die(isl_space_get_ctx(space
), isl_error_invalid
,
12167 "index out of bounds", goto error
);
12168 if (pos2
>= isl_space_dim(space
, type2
))
12169 isl_die(isl_space_get_ctx(space
), isl_error_invalid
,
12170 "index out of bounds", goto error
);
12172 if (type1
== type2
&& pos1
== pos2
)
12173 return isl_basic_map_empty(space
);
12175 bmap
= isl_basic_map_alloc_space(space
, 0, 0, 1);
12176 i
= isl_basic_map_alloc_inequality(bmap
);
12178 return isl_basic_map_free(bmap
);
12179 isl_seq_clr(bmap
->ineq
[i
], 1 + isl_basic_map_total_dim(bmap
));
12180 pos1
+= isl_basic_map_offset(bmap
, type1
);
12181 pos2
+= isl_basic_map_offset(bmap
, type2
);
12182 isl_int_set_si(bmap
->ineq
[i
][pos1
], 1);
12183 isl_int_set_si(bmap
->ineq
[i
][pos2
], -1);
12184 isl_int_set_si(bmap
->ineq
[i
][0], -1);
12185 bmap
= isl_basic_map_finalize(bmap
);
12189 isl_space_free(space
);
12190 isl_basic_map_free(bmap
);
12194 /* Add a constraint imposing that the value of the first dimension is
12195 * greater than that of the second.
12197 __isl_give isl_basic_map
*isl_basic_map_order_gt(__isl_take isl_basic_map
*bmap
,
12198 enum isl_dim_type type1
, int pos1
, enum isl_dim_type type2
, int pos2
)
12202 gt
= greator(isl_basic_map_get_space(bmap
), type1
, pos1
, type2
, pos2
);
12204 bmap
= isl_basic_map_intersect(bmap
, gt
);
12209 /* Add a constraint imposing that the value of the first dimension is
12210 * greater than that of the second.
12212 __isl_give isl_map
*isl_map_order_gt(__isl_take isl_map
*map
,
12213 enum isl_dim_type type1
, int pos1
, enum isl_dim_type type2
, int pos2
)
12215 isl_basic_map
*bmap
;
12217 bmap
= greator(isl_map_get_space(map
), type1
, pos1
, type2
, pos2
);
12219 map
= isl_map_intersect(map
, isl_map_from_basic_map(bmap
));
12224 /* Add a constraint imposing that the value of the first dimension is
12225 * smaller than that of the second.
12227 __isl_give isl_map
*isl_map_order_lt(__isl_take isl_map
*map
,
12228 enum isl_dim_type type1
, int pos1
, enum isl_dim_type type2
, int pos2
)
12230 return isl_map_order_gt(map
, type2
, pos2
, type1
, pos1
);
12233 __isl_give isl_aff
*isl_basic_map_get_div(__isl_keep isl_basic_map
*bmap
,
12237 isl_local_space
*ls
;
12242 if (!isl_basic_map_divs_known(bmap
))
12243 isl_die(isl_basic_map_get_ctx(bmap
), isl_error_invalid
,
12244 "some divs are unknown", return NULL
);
12246 ls
= isl_basic_map_get_local_space(bmap
);
12247 div
= isl_local_space_get_div(ls
, pos
);
12248 isl_local_space_free(ls
);
12253 __isl_give isl_aff
*isl_basic_set_get_div(__isl_keep isl_basic_set
*bset
,
12256 return isl_basic_map_get_div(bset
, pos
);
12259 /* Plug in "subs" for dimension "type", "pos" of "bset".
12261 * Let i be the dimension to replace and let "subs" be of the form
12265 * Any integer division with a non-zero coefficient for i,
12267 * floor((a i + g)/m)
12271 * floor((a f + d g)/(m d))
12273 * Constraints of the form
12281 * We currently require that "subs" is an integral expression.
12282 * Handling rational expressions may require us to add stride constraints
12283 * as we do in isl_basic_set_preimage_multi_aff.
12285 __isl_give isl_basic_set
*isl_basic_set_substitute(
12286 __isl_take isl_basic_set
*bset
,
12287 enum isl_dim_type type
, unsigned pos
, __isl_keep isl_aff
*subs
)
12293 if (bset
&& isl_basic_set_plain_is_empty(bset
))
12296 bset
= isl_basic_set_cow(bset
);
12297 if (!bset
|| !subs
)
12300 ctx
= isl_basic_set_get_ctx(bset
);
12301 if (!isl_space_is_equal(bset
->dim
, subs
->ls
->dim
))
12302 isl_die(ctx
, isl_error_invalid
,
12303 "spaces don't match", goto error
);
12304 if (isl_local_space_dim(subs
->ls
, isl_dim_div
) != 0)
12305 isl_die(ctx
, isl_error_unsupported
,
12306 "cannot handle divs yet", goto error
);
12307 if (!isl_int_is_one(subs
->v
->el
[0]))
12308 isl_die(ctx
, isl_error_invalid
,
12309 "can only substitute integer expressions", goto error
);
12311 pos
+= isl_basic_set_offset(bset
, type
);
12315 for (i
= 0; i
< bset
->n_eq
; ++i
) {
12316 if (isl_int_is_zero(bset
->eq
[i
][pos
]))
12318 isl_int_set(v
, bset
->eq
[i
][pos
]);
12319 isl_int_set_si(bset
->eq
[i
][pos
], 0);
12320 isl_seq_combine(bset
->eq
[i
], subs
->v
->el
[0], bset
->eq
[i
],
12321 v
, subs
->v
->el
+ 1, subs
->v
->size
- 1);
12324 for (i
= 0; i
< bset
->n_ineq
; ++i
) {
12325 if (isl_int_is_zero(bset
->ineq
[i
][pos
]))
12327 isl_int_set(v
, bset
->ineq
[i
][pos
]);
12328 isl_int_set_si(bset
->ineq
[i
][pos
], 0);
12329 isl_seq_combine(bset
->ineq
[i
], subs
->v
->el
[0], bset
->ineq
[i
],
12330 v
, subs
->v
->el
+ 1, subs
->v
->size
- 1);
12333 for (i
= 0; i
< bset
->n_div
; ++i
) {
12334 if (isl_int_is_zero(bset
->div
[i
][1 + pos
]))
12336 isl_int_set(v
, bset
->div
[i
][1 + pos
]);
12337 isl_int_set_si(bset
->div
[i
][1 + pos
], 0);
12338 isl_seq_combine(bset
->div
[i
] + 1,
12339 subs
->v
->el
[0], bset
->div
[i
] + 1,
12340 v
, subs
->v
->el
+ 1, subs
->v
->size
- 1);
12341 isl_int_mul(bset
->div
[i
][0], bset
->div
[i
][0], subs
->v
->el
[0]);
12346 bset
= isl_basic_set_simplify(bset
);
12347 return isl_basic_set_finalize(bset
);
12349 isl_basic_set_free(bset
);
12353 /* Plug in "subs" for dimension "type", "pos" of "set".
12355 __isl_give isl_set
*isl_set_substitute(__isl_take isl_set
*set
,
12356 enum isl_dim_type type
, unsigned pos
, __isl_keep isl_aff
*subs
)
12360 if (set
&& isl_set_plain_is_empty(set
))
12363 set
= isl_set_cow(set
);
12367 for (i
= set
->n
- 1; i
>= 0; --i
) {
12368 set
->p
[i
] = isl_basic_set_substitute(set
->p
[i
], type
, pos
, subs
);
12369 if (remove_if_empty(set
, i
) < 0)
12379 /* Check if the range of "ma" is compatible with the domain or range
12380 * (depending on "type") of "bmap".
12381 * Return -1 if anything is wrong.
12383 static int check_basic_map_compatible_range_multi_aff(
12384 __isl_keep isl_basic_map
*bmap
, enum isl_dim_type type
,
12385 __isl_keep isl_multi_aff
*ma
)
12388 isl_space
*ma_space
;
12390 ma_space
= isl_multi_aff_get_space(ma
);
12392 m
= isl_space_match(bmap
->dim
, isl_dim_param
, ma_space
, isl_dim_param
);
12396 isl_die(isl_basic_map_get_ctx(bmap
), isl_error_invalid
,
12397 "parameters don't match", goto error
);
12398 m
= isl_space_tuple_is_equal(bmap
->dim
, type
, ma_space
, isl_dim_out
);
12402 isl_die(isl_basic_map_get_ctx(bmap
), isl_error_invalid
,
12403 "spaces don't match", goto error
);
12405 isl_space_free(ma_space
);
12408 isl_space_free(ma_space
);
12412 /* Copy the divs from "ma" to "bmap", adding zeros for the "n_before"
12413 * coefficients before the transformed range of dimensions,
12414 * the "n_after" coefficients after the transformed range of dimensions
12415 * and the coefficients of the other divs in "bmap".
12417 static int set_ma_divs(__isl_keep isl_basic_map
*bmap
,
12418 __isl_keep isl_multi_aff
*ma
, int n_before
, int n_after
, int n_div
)
12423 isl_local_space
*ls
;
12428 ls
= isl_aff_get_domain_local_space(ma
->p
[0]);
12432 n_param
= isl_local_space_dim(ls
, isl_dim_param
);
12433 n_set
= isl_local_space_dim(ls
, isl_dim_set
);
12434 for (i
= 0; i
< n_div
; ++i
) {
12435 int o_bmap
= 0, o_ls
= 0;
12437 isl_seq_cpy(bmap
->div
[i
], ls
->div
->row
[i
], 1 + 1 + n_param
);
12438 o_bmap
+= 1 + 1 + n_param
;
12439 o_ls
+= 1 + 1 + n_param
;
12440 isl_seq_clr(bmap
->div
[i
] + o_bmap
, n_before
);
12441 o_bmap
+= n_before
;
12442 isl_seq_cpy(bmap
->div
[i
] + o_bmap
,
12443 ls
->div
->row
[i
] + o_ls
, n_set
);
12446 isl_seq_clr(bmap
->div
[i
] + o_bmap
, n_after
);
12448 isl_seq_cpy(bmap
->div
[i
] + o_bmap
,
12449 ls
->div
->row
[i
] + o_ls
, n_div
);
12452 isl_seq_clr(bmap
->div
[i
] + o_bmap
, bmap
->n_div
- n_div
);
12453 if (isl_basic_set_add_div_constraints(bmap
, i
) < 0)
12457 isl_local_space_free(ls
);
12460 isl_local_space_free(ls
);
12464 /* How many stride constraints does "ma" enforce?
12465 * That is, how many of the affine expressions have a denominator
12466 * different from one?
12468 static int multi_aff_strides(__isl_keep isl_multi_aff
*ma
)
12473 for (i
= 0; i
< ma
->n
; ++i
)
12474 if (!isl_int_is_one(ma
->p
[i
]->v
->el
[0]))
12480 /* For each affine expression in ma of the form
12482 * x_i = (f_i y + h_i)/m_i
12484 * with m_i different from one, add a constraint to "bmap"
12487 * f_i y + h_i = m_i alpha_i
12489 * with alpha_i an additional existentially quantified variable.
12491 * The input variables of "ma" correspond to a subset of the variables
12492 * of "bmap". There are "n_before" variables in "bmap" before this
12493 * subset and "n_after" variables after this subset.
12494 * The integer divisions of the affine expressions in "ma" are assumed
12495 * to have been aligned. There are "n_div_ma" of them and
12496 * they appear first in "bmap", straight after the "n_after" variables.
12498 static __isl_give isl_basic_map
*add_ma_strides(
12499 __isl_take isl_basic_map
*bmap
, __isl_keep isl_multi_aff
*ma
,
12500 int n_before
, int n_after
, int n_div_ma
)
12508 total
= isl_basic_map_total_dim(bmap
);
12509 n_param
= isl_multi_aff_dim(ma
, isl_dim_param
);
12510 n_in
= isl_multi_aff_dim(ma
, isl_dim_in
);
12511 for (i
= 0; i
< ma
->n
; ++i
) {
12512 int o_bmap
= 0, o_ma
= 1;
12514 if (isl_int_is_one(ma
->p
[i
]->v
->el
[0]))
12516 div
= isl_basic_map_alloc_div(bmap
);
12517 k
= isl_basic_map_alloc_equality(bmap
);
12518 if (div
< 0 || k
< 0)
12520 isl_int_set_si(bmap
->div
[div
][0], 0);
12521 isl_seq_cpy(bmap
->eq
[k
] + o_bmap
,
12522 ma
->p
[i
]->v
->el
+ o_ma
, 1 + n_param
);
12523 o_bmap
+= 1 + n_param
;
12524 o_ma
+= 1 + n_param
;
12525 isl_seq_clr(bmap
->eq
[k
] + o_bmap
, n_before
);
12526 o_bmap
+= n_before
;
12527 isl_seq_cpy(bmap
->eq
[k
] + o_bmap
,
12528 ma
->p
[i
]->v
->el
+ o_ma
, n_in
);
12531 isl_seq_clr(bmap
->eq
[k
] + o_bmap
, n_after
);
12533 isl_seq_cpy(bmap
->eq
[k
] + o_bmap
,
12534 ma
->p
[i
]->v
->el
+ o_ma
, n_div_ma
);
12535 o_bmap
+= n_div_ma
;
12537 isl_seq_clr(bmap
->eq
[k
] + o_bmap
, 1 + total
- o_bmap
);
12538 isl_int_neg(bmap
->eq
[k
][1 + total
], ma
->p
[i
]->v
->el
[0]);
12544 isl_basic_map_free(bmap
);
12548 /* Replace the domain or range space (depending on "type) of "space" by "set".
12550 static __isl_give isl_space
*isl_space_set(__isl_take isl_space
*space
,
12551 enum isl_dim_type type
, __isl_take isl_space
*set
)
12553 if (type
== isl_dim_in
) {
12554 space
= isl_space_range(space
);
12555 space
= isl_space_map_from_domain_and_range(set
, space
);
12557 space
= isl_space_domain(space
);
12558 space
= isl_space_map_from_domain_and_range(space
, set
);
12564 /* Compute the preimage of the domain or range (depending on "type")
12565 * of "bmap" under the function represented by "ma".
12566 * In other words, plug in "ma" in the domain or range of "bmap".
12567 * The result is a basic map that lives in the same space as "bmap"
12568 * except that the domain or range has been replaced by
12569 * the domain space of "ma".
12571 * If bmap is represented by
12573 * A(p) + S u + B x + T v + C(divs) >= 0,
12575 * where u and x are input and output dimensions if type == isl_dim_out
12576 * while x and v are input and output dimensions if type == isl_dim_in,
12577 * and ma is represented by
12579 * x = D(p) + F(y) + G(divs')
12581 * then the result is
12583 * A(p) + B D(p) + S u + B F(y) + T v + B G(divs') + C(divs) >= 0
12585 * The divs in the input set are similarly adjusted.
12588 * floor((a_i(p) + s u + b_i x + t v + c_i(divs))/n_i)
12592 * floor((a_i(p) + b_i D(p) + s u + b_i F(y) + t v +
12593 * B_i G(divs') + c_i(divs))/n_i)
12595 * If bmap is not a rational map and if F(y) involves any denominators
12597 * x_i = (f_i y + h_i)/m_i
12599 * then additional constraints are added to ensure that we only
12600 * map back integer points. That is we enforce
12602 * f_i y + h_i = m_i alpha_i
12604 * with alpha_i an additional existentially quantified variable.
12606 * We first copy over the divs from "ma".
12607 * Then we add the modified constraints and divs from "bmap".
12608 * Finally, we add the stride constraints, if needed.
12610 __isl_give isl_basic_map
*isl_basic_map_preimage_multi_aff(
12611 __isl_take isl_basic_map
*bmap
, enum isl_dim_type type
,
12612 __isl_take isl_multi_aff
*ma
)
12616 isl_basic_map
*res
= NULL
;
12617 int n_before
, n_after
, n_div_bmap
, n_div_ma
;
12618 isl_int f
, c1
, c2
, g
;
12619 int rational
, strides
;
12626 ma
= isl_multi_aff_align_divs(ma
);
12629 if (check_basic_map_compatible_range_multi_aff(bmap
, type
, ma
) < 0)
12632 if (type
== isl_dim_in
) {
12634 n_after
= isl_basic_map_dim(bmap
, isl_dim_out
);
12636 n_before
= isl_basic_map_dim(bmap
, isl_dim_in
);
12639 n_div_bmap
= isl_basic_map_dim(bmap
, isl_dim_div
);
12640 n_div_ma
= ma
->n
? isl_aff_dim(ma
->p
[0], isl_dim_div
) : 0;
12642 space
= isl_multi_aff_get_domain_space(ma
);
12643 space
= isl_space_set(isl_basic_map_get_space(bmap
), type
, space
);
12644 rational
= isl_basic_map_is_rational(bmap
);
12645 strides
= rational
? 0 : multi_aff_strides(ma
);
12646 res
= isl_basic_map_alloc_space(space
, n_div_ma
+ n_div_bmap
+ strides
,
12647 bmap
->n_eq
+ strides
, bmap
->n_ineq
+ 2 * n_div_ma
);
12649 res
= isl_basic_map_set_rational(res
);
12651 for (i
= 0; i
< n_div_ma
+ n_div_bmap
; ++i
)
12652 if (isl_basic_map_alloc_div(res
) < 0)
12655 if (set_ma_divs(res
, ma
, n_before
, n_after
, n_div_ma
) < 0)
12658 for (i
= 0; i
< bmap
->n_eq
; ++i
) {
12659 k
= isl_basic_map_alloc_equality(res
);
12662 isl_seq_preimage(res
->eq
[k
], bmap
->eq
[i
], ma
, n_before
,
12663 n_after
, n_div_ma
, n_div_bmap
, f
, c1
, c2
, g
, 0);
12666 for (i
= 0; i
< bmap
->n_ineq
; ++i
) {
12667 k
= isl_basic_map_alloc_inequality(res
);
12670 isl_seq_preimage(res
->ineq
[k
], bmap
->ineq
[i
], ma
, n_before
,
12671 n_after
, n_div_ma
, n_div_bmap
, f
, c1
, c2
, g
, 0);
12674 for (i
= 0; i
< bmap
->n_div
; ++i
) {
12675 if (isl_int_is_zero(bmap
->div
[i
][0])) {
12676 isl_int_set_si(res
->div
[n_div_ma
+ i
][0], 0);
12679 isl_seq_preimage(res
->div
[n_div_ma
+ i
], bmap
->div
[i
], ma
,
12680 n_before
, n_after
, n_div_ma
, n_div_bmap
,
12685 res
= add_ma_strides(res
, ma
, n_before
, n_after
, n_div_ma
);
12691 isl_basic_map_free(bmap
);
12692 isl_multi_aff_free(ma
);
12693 res
= isl_basic_set_simplify(res
);
12694 return isl_basic_map_finalize(res
);
12700 isl_basic_map_free(bmap
);
12701 isl_multi_aff_free(ma
);
12702 isl_basic_map_free(res
);
12706 /* Compute the preimage of "bset" under the function represented by "ma".
12707 * In other words, plug in "ma" in "bset". The result is a basic set
12708 * that lives in the domain space of "ma".
12710 __isl_give isl_basic_set
*isl_basic_set_preimage_multi_aff(
12711 __isl_take isl_basic_set
*bset
, __isl_take isl_multi_aff
*ma
)
12713 return isl_basic_map_preimage_multi_aff(bset
, isl_dim_set
, ma
);
12716 /* Compute the preimage of the domain of "bmap" under the function
12717 * represented by "ma".
12718 * In other words, plug in "ma" in the domain of "bmap".
12719 * The result is a basic map that lives in the same space as "bmap"
12720 * except that the domain has been replaced by the domain space of "ma".
12722 __isl_give isl_basic_map
*isl_basic_map_preimage_domain_multi_aff(
12723 __isl_take isl_basic_map
*bmap
, __isl_take isl_multi_aff
*ma
)
12725 return isl_basic_map_preimage_multi_aff(bmap
, isl_dim_in
, ma
);
12728 /* Compute the preimage of the range of "bmap" under the function
12729 * represented by "ma".
12730 * In other words, plug in "ma" in the range of "bmap".
12731 * The result is a basic map that lives in the same space as "bmap"
12732 * except that the range has been replaced by the domain space of "ma".
12734 __isl_give isl_basic_map
*isl_basic_map_preimage_range_multi_aff(
12735 __isl_take isl_basic_map
*bmap
, __isl_take isl_multi_aff
*ma
)
12737 return isl_basic_map_preimage_multi_aff(bmap
, isl_dim_out
, ma
);
12740 /* Check if the range of "ma" is compatible with the domain or range
12741 * (depending on "type") of "map".
12742 * Return -1 if anything is wrong.
12744 static int check_map_compatible_range_multi_aff(
12745 __isl_keep isl_map
*map
, enum isl_dim_type type
,
12746 __isl_keep isl_multi_aff
*ma
)
12749 isl_space
*ma_space
;
12751 ma_space
= isl_multi_aff_get_space(ma
);
12752 m
= isl_space_tuple_is_equal(map
->dim
, type
, ma_space
, isl_dim_out
);
12753 isl_space_free(ma_space
);
12755 isl_die(isl_map_get_ctx(map
), isl_error_invalid
,
12756 "spaces don't match", return -1);
12760 /* Compute the preimage of the domain or range (depending on "type")
12761 * of "map" under the function represented by "ma".
12762 * In other words, plug in "ma" in the domain or range of "map".
12763 * The result is a map that lives in the same space as "map"
12764 * except that the domain or range has been replaced by
12765 * the domain space of "ma".
12767 * The parameters are assumed to have been aligned.
12769 static __isl_give isl_map
*map_preimage_multi_aff(__isl_take isl_map
*map
,
12770 enum isl_dim_type type
, __isl_take isl_multi_aff
*ma
)
12775 map
= isl_map_cow(map
);
12776 ma
= isl_multi_aff_align_divs(ma
);
12779 if (check_map_compatible_range_multi_aff(map
, type
, ma
) < 0)
12782 for (i
= 0; i
< map
->n
; ++i
) {
12783 map
->p
[i
] = isl_basic_map_preimage_multi_aff(map
->p
[i
], type
,
12784 isl_multi_aff_copy(ma
));
12789 space
= isl_multi_aff_get_domain_space(ma
);
12790 space
= isl_space_set(isl_map_get_space(map
), type
, space
);
12792 isl_space_free(map
->dim
);
12797 isl_multi_aff_free(ma
);
12799 ISL_F_CLR(map
, ISL_MAP_DISJOINT
);
12800 ISL_F_CLR(map
, ISL_SET_NORMALIZED
);
12803 isl_multi_aff_free(ma
);
12808 /* Compute the preimage of the domain or range (depending on "type")
12809 * of "map" under the function represented by "ma".
12810 * In other words, plug in "ma" in the domain or range of "map".
12811 * The result is a map that lives in the same space as "map"
12812 * except that the domain or range has been replaced by
12813 * the domain space of "ma".
12815 __isl_give isl_map
*isl_map_preimage_multi_aff(__isl_take isl_map
*map
,
12816 enum isl_dim_type type
, __isl_take isl_multi_aff
*ma
)
12821 if (isl_space_match(map
->dim
, isl_dim_param
, ma
->space
, isl_dim_param
))
12822 return map_preimage_multi_aff(map
, type
, ma
);
12824 if (!isl_space_has_named_params(map
->dim
) ||
12825 !isl_space_has_named_params(ma
->space
))
12826 isl_die(map
->ctx
, isl_error_invalid
,
12827 "unaligned unnamed parameters", goto error
);
12828 map
= isl_map_align_params(map
, isl_multi_aff_get_space(ma
));
12829 ma
= isl_multi_aff_align_params(ma
, isl_map_get_space(map
));
12831 return map_preimage_multi_aff(map
, type
, ma
);
12833 isl_multi_aff_free(ma
);
12834 return isl_map_free(map
);
12837 /* Compute the preimage of "set" under the function represented by "ma".
12838 * In other words, plug in "ma" in "set". The result is a set
12839 * that lives in the domain space of "ma".
12841 __isl_give isl_set
*isl_set_preimage_multi_aff(__isl_take isl_set
*set
,
12842 __isl_take isl_multi_aff
*ma
)
12844 return isl_map_preimage_multi_aff(set
, isl_dim_set
, ma
);
12847 /* Compute the preimage of the domain of "map" under the function
12848 * represented by "ma".
12849 * In other words, plug in "ma" in the domain of "map".
12850 * The result is a map that lives in the same space as "map"
12851 * except that the domain has been replaced by the domain space of "ma".
12853 __isl_give isl_map
*isl_map_preimage_domain_multi_aff(__isl_take isl_map
*map
,
12854 __isl_take isl_multi_aff
*ma
)
12856 return isl_map_preimage_multi_aff(map
, isl_dim_in
, ma
);
12859 /* Compute the preimage of the range of "map" under the function
12860 * represented by "ma".
12861 * In other words, plug in "ma" in the range of "map".
12862 * The result is a map that lives in the same space as "map"
12863 * except that the range has been replaced by the domain space of "ma".
12865 __isl_give isl_map
*isl_map_preimage_range_multi_aff(__isl_take isl_map
*map
,
12866 __isl_take isl_multi_aff
*ma
)
12868 return isl_map_preimage_multi_aff(map
, isl_dim_out
, ma
);
12871 /* Compute the preimage of "map" under the function represented by "pma".
12872 * In other words, plug in "pma" in the domain or range of "map".
12873 * The result is a map that lives in the same space as "map",
12874 * except that the space of type "type" has been replaced by
12875 * the domain space of "pma".
12877 * The parameters of "map" and "pma" are assumed to have been aligned.
12879 static __isl_give isl_map
*isl_map_preimage_pw_multi_aff_aligned(
12880 __isl_take isl_map
*map
, enum isl_dim_type type
,
12881 __isl_take isl_pw_multi_aff
*pma
)
12890 isl_pw_multi_aff_free(pma
);
12891 res
= isl_map_empty(isl_map_get_space(map
));
12896 res
= isl_map_preimage_multi_aff(isl_map_copy(map
), type
,
12897 isl_multi_aff_copy(pma
->p
[0].maff
));
12898 if (type
== isl_dim_in
)
12899 res
= isl_map_intersect_domain(res
,
12900 isl_map_copy(pma
->p
[0].set
));
12902 res
= isl_map_intersect_range(res
,
12903 isl_map_copy(pma
->p
[0].set
));
12905 for (i
= 1; i
< pma
->n
; ++i
) {
12908 res_i
= isl_map_preimage_multi_aff(isl_map_copy(map
), type
,
12909 isl_multi_aff_copy(pma
->p
[i
].maff
));
12910 if (type
== isl_dim_in
)
12911 res_i
= isl_map_intersect_domain(res_i
,
12912 isl_map_copy(pma
->p
[i
].set
));
12914 res_i
= isl_map_intersect_range(res_i
,
12915 isl_map_copy(pma
->p
[i
].set
));
12916 res
= isl_map_union(res
, res_i
);
12919 isl_pw_multi_aff_free(pma
);
12923 isl_pw_multi_aff_free(pma
);
12928 /* Compute the preimage of "map" under the function represented by "pma".
12929 * In other words, plug in "pma" in the domain or range of "map".
12930 * The result is a map that lives in the same space as "map",
12931 * except that the space of type "type" has been replaced by
12932 * the domain space of "pma".
12934 __isl_give isl_map
*isl_map_preimage_pw_multi_aff(__isl_take isl_map
*map
,
12935 enum isl_dim_type type
, __isl_take isl_pw_multi_aff
*pma
)
12940 if (isl_space_match(map
->dim
, isl_dim_param
, pma
->dim
, isl_dim_param
))
12941 return isl_map_preimage_pw_multi_aff_aligned(map
, type
, pma
);
12943 if (!isl_space_has_named_params(map
->dim
) ||
12944 !isl_space_has_named_params(pma
->dim
))
12945 isl_die(map
->ctx
, isl_error_invalid
,
12946 "unaligned unnamed parameters", goto error
);
12947 map
= isl_map_align_params(map
, isl_pw_multi_aff_get_space(pma
));
12948 pma
= isl_pw_multi_aff_align_params(pma
, isl_map_get_space(map
));
12950 return isl_map_preimage_pw_multi_aff_aligned(map
, type
, pma
);
12952 isl_pw_multi_aff_free(pma
);
12953 return isl_map_free(map
);
12956 /* Compute the preimage of "set" under the function represented by "pma".
12957 * In other words, plug in "pma" in "set". The result is a set
12958 * that lives in the domain space of "pma".
12960 __isl_give isl_set
*isl_set_preimage_pw_multi_aff(__isl_take isl_set
*set
,
12961 __isl_take isl_pw_multi_aff
*pma
)
12963 return isl_map_preimage_pw_multi_aff(set
, isl_dim_set
, pma
);
12966 /* Compute the preimage of the domain of "map" under the function
12967 * represented by "pma".
12968 * In other words, plug in "pma" in the domain of "map".
12969 * The result is a map that lives in the same space as "map",
12970 * except that domain space has been replaced by the domain space of "pma".
12972 __isl_give isl_map
*isl_map_preimage_domain_pw_multi_aff(
12973 __isl_take isl_map
*map
, __isl_take isl_pw_multi_aff
*pma
)
12975 return isl_map_preimage_pw_multi_aff(map
, isl_dim_in
, pma
);
12978 /* Compute the preimage of the range of "map" under the function
12979 * represented by "pma".
12980 * In other words, plug in "pma" in the range of "map".
12981 * The result is a map that lives in the same space as "map",
12982 * except that range space has been replaced by the domain space of "pma".
12984 __isl_give isl_map
*isl_map_preimage_range_pw_multi_aff(
12985 __isl_take isl_map
*map
, __isl_take isl_pw_multi_aff
*pma
)
12987 return isl_map_preimage_pw_multi_aff(map
, isl_dim_out
, pma
);
12990 /* Compute the preimage of "map" under the function represented by "mpa".
12991 * In other words, plug in "mpa" in the domain or range of "map".
12992 * The result is a map that lives in the same space as "map",
12993 * except that the space of type "type" has been replaced by
12994 * the domain space of "mpa".
12996 * If the map does not involve any constraints that refer to the
12997 * dimensions of the substituted space, then the only possible
12998 * effect of "mpa" on the map is to map the space to a different space.
12999 * We create a separate isl_multi_aff to effectuate this change
13000 * in order to avoid spurious splitting of the map along the pieces
13003 __isl_give isl_map
*isl_map_preimage_multi_pw_aff(__isl_take isl_map
*map
,
13004 enum isl_dim_type type
, __isl_take isl_multi_pw_aff
*mpa
)
13007 isl_pw_multi_aff
*pma
;
13012 n
= isl_map_dim(map
, type
);
13013 if (!isl_map_involves_dims(map
, type
, 0, n
)) {
13017 space
= isl_multi_pw_aff_get_space(mpa
);
13018 isl_multi_pw_aff_free(mpa
);
13019 ma
= isl_multi_aff_zero(space
);
13020 return isl_map_preimage_multi_aff(map
, type
, ma
);
13023 pma
= isl_pw_multi_aff_from_multi_pw_aff(mpa
);
13024 return isl_map_preimage_pw_multi_aff(map
, type
, pma
);
13027 isl_multi_pw_aff_free(mpa
);
13031 /* Compute the preimage of "map" under the function represented by "mpa".
13032 * In other words, plug in "mpa" in the domain "map".
13033 * The result is a map that lives in the same space as "map",
13034 * except that domain space has been replaced by the domain space of "mpa".
13036 __isl_give isl_map
*isl_map_preimage_domain_multi_pw_aff(
13037 __isl_take isl_map
*map
, __isl_take isl_multi_pw_aff
*mpa
)
13039 return isl_map_preimage_multi_pw_aff(map
, isl_dim_in
, mpa
);
13042 /* Compute the preimage of "set" by the function represented by "mpa".
13043 * In other words, plug in "mpa" in "set".
13045 __isl_give isl_set
*isl_set_preimage_multi_pw_aff(__isl_take isl_set
*set
,
13046 __isl_take isl_multi_pw_aff
*mpa
)
13048 return isl_map_preimage_multi_pw_aff(set
, isl_dim_set
, mpa
);