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 isl_bool
isl_basic_map_compatible_domain(__isl_keep isl_basic_map
*bmap
,
210 __isl_keep isl_basic_set
*bset
)
214 return isl_bool_error
;
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-(m-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 /* Has "map" been marked as a rational map?
820 * In particular, have all basic maps in "map" been marked this way?
821 * An empty map is not considered to be rational.
822 * Maps where only some of the basic maps are marked rational
825 isl_bool
isl_map_is_rational(__isl_keep isl_map
*map
)
831 return isl_bool_error
;
833 return isl_bool_false
;
834 rational
= isl_basic_map_is_rational(map
->p
[0]);
837 for (i
= 1; i
< map
->n
; ++i
) {
840 rational_i
= isl_basic_map_is_rational(map
->p
[i
]);
843 if (rational
!= rational_i
)
844 isl_die(isl_map_get_ctx(map
), isl_error_unsupported
,
845 "mixed rational and integer basic maps "
846 "not supported", return isl_bool_error
);
852 /* Has "set" been marked as a rational set?
853 * In particular, have all basic set in "set" been marked this way?
854 * An empty set is not considered to be rational.
855 * Sets where only some of the basic sets are marked rational
858 isl_bool
isl_set_is_rational(__isl_keep isl_set
*set
)
860 return isl_map_is_rational(set
);
863 int isl_basic_set_is_rational(__isl_keep isl_basic_set
*bset
)
865 return isl_basic_map_is_rational(bset
);
868 /* Does "bmap" contain any rational points?
870 * If "bmap" has an equality for each dimension, equating the dimension
871 * to an integer constant, then it has no rational points, even if it
872 * is marked as rational.
874 int isl_basic_map_has_rational(__isl_keep isl_basic_map
*bmap
)
876 int has_rational
= 1;
881 if (isl_basic_map_plain_is_empty(bmap
))
883 if (!isl_basic_map_is_rational(bmap
))
885 bmap
= isl_basic_map_copy(bmap
);
886 bmap
= isl_basic_map_implicit_equalities(bmap
);
889 total
= isl_basic_map_total_dim(bmap
);
890 if (bmap
->n_eq
== total
) {
892 for (i
= 0; i
< bmap
->n_eq
; ++i
) {
893 j
= isl_seq_first_non_zero(bmap
->eq
[i
] + 1, total
);
896 if (!isl_int_is_one(bmap
->eq
[i
][1 + j
]) &&
897 !isl_int_is_negone(bmap
->eq
[i
][1 + j
]))
899 j
= isl_seq_first_non_zero(bmap
->eq
[i
] + 1 + j
+ 1,
907 isl_basic_map_free(bmap
);
912 /* Does "map" contain any rational points?
914 int isl_map_has_rational(__isl_keep isl_map
*map
)
921 for (i
= 0; i
< map
->n
; ++i
) {
922 has_rational
= isl_basic_map_has_rational(map
->p
[i
]);
923 if (has_rational
< 0)
931 /* Does "set" contain any rational points?
933 int isl_set_has_rational(__isl_keep isl_set
*set
)
935 return isl_map_has_rational(set
);
938 /* Is this basic set a parameter domain?
940 int isl_basic_set_is_params(__isl_keep isl_basic_set
*bset
)
944 return isl_space_is_params(bset
->dim
);
947 /* Is this set a parameter domain?
949 isl_bool
isl_set_is_params(__isl_keep isl_set
*set
)
952 return isl_bool_error
;
953 return isl_space_is_params(set
->dim
);
956 /* Is this map actually a parameter domain?
957 * Users should never call this function. Outside of isl,
958 * a map can never be a parameter domain.
960 int isl_map_is_params(__isl_keep isl_map
*map
)
964 return isl_space_is_params(map
->dim
);
967 static struct isl_basic_map
*basic_map_init(struct isl_ctx
*ctx
,
968 struct isl_basic_map
*bmap
, unsigned extra
,
969 unsigned n_eq
, unsigned n_ineq
)
972 size_t row_size
= 1 + isl_space_dim(bmap
->dim
, isl_dim_all
) + extra
;
977 bmap
->block
= isl_blk_alloc(ctx
, (n_ineq
+ n_eq
) * row_size
);
978 if (isl_blk_is_error(bmap
->block
))
981 bmap
->ineq
= isl_alloc_array(ctx
, isl_int
*, n_ineq
+ n_eq
);
982 if ((n_ineq
+ n_eq
) && !bmap
->ineq
)
986 bmap
->block2
= isl_blk_empty();
989 bmap
->block2
= isl_blk_alloc(ctx
, extra
* (1 + row_size
));
990 if (isl_blk_is_error(bmap
->block2
))
993 bmap
->div
= isl_alloc_array(ctx
, isl_int
*, extra
);
998 for (i
= 0; i
< n_ineq
+ n_eq
; ++i
)
999 bmap
->ineq
[i
] = bmap
->block
.data
+ i
* row_size
;
1001 for (i
= 0; i
< extra
; ++i
)
1002 bmap
->div
[i
] = bmap
->block2
.data
+ i
* (1 + row_size
);
1006 bmap
->c_size
= n_eq
+ n_ineq
;
1007 bmap
->eq
= bmap
->ineq
+ n_ineq
;
1008 bmap
->extra
= extra
;
1012 bmap
->sample
= NULL
;
1016 isl_basic_map_free(bmap
);
1020 struct isl_basic_set
*isl_basic_set_alloc(struct isl_ctx
*ctx
,
1021 unsigned nparam
, unsigned dim
, unsigned extra
,
1022 unsigned n_eq
, unsigned n_ineq
)
1024 struct isl_basic_map
*bmap
;
1027 space
= isl_space_set_alloc(ctx
, nparam
, dim
);
1031 bmap
= isl_basic_map_alloc_space(space
, extra
, n_eq
, n_ineq
);
1032 return (struct isl_basic_set
*)bmap
;
1035 struct isl_basic_set
*isl_basic_set_alloc_space(__isl_take isl_space
*dim
,
1036 unsigned extra
, unsigned n_eq
, unsigned n_ineq
)
1038 struct isl_basic_map
*bmap
;
1041 isl_assert(dim
->ctx
, dim
->n_in
== 0, goto error
);
1042 bmap
= isl_basic_map_alloc_space(dim
, extra
, n_eq
, n_ineq
);
1043 return (struct isl_basic_set
*)bmap
;
1045 isl_space_free(dim
);
1049 struct isl_basic_map
*isl_basic_map_alloc_space(__isl_take isl_space
*dim
,
1050 unsigned extra
, unsigned n_eq
, unsigned n_ineq
)
1052 struct isl_basic_map
*bmap
;
1056 bmap
= isl_calloc_type(dim
->ctx
, struct isl_basic_map
);
1061 return basic_map_init(dim
->ctx
, bmap
, extra
, n_eq
, n_ineq
);
1063 isl_space_free(dim
);
1067 struct isl_basic_map
*isl_basic_map_alloc(struct isl_ctx
*ctx
,
1068 unsigned nparam
, unsigned in
, unsigned out
, unsigned extra
,
1069 unsigned n_eq
, unsigned n_ineq
)
1071 struct isl_basic_map
*bmap
;
1074 dim
= isl_space_alloc(ctx
, nparam
, in
, out
);
1078 bmap
= isl_basic_map_alloc_space(dim
, extra
, n_eq
, n_ineq
);
1082 static void dup_constraints(
1083 struct isl_basic_map
*dst
, struct isl_basic_map
*src
)
1086 unsigned total
= isl_basic_map_total_dim(src
);
1088 for (i
= 0; i
< src
->n_eq
; ++i
) {
1089 int j
= isl_basic_map_alloc_equality(dst
);
1090 isl_seq_cpy(dst
->eq
[j
], src
->eq
[i
], 1+total
);
1093 for (i
= 0; i
< src
->n_ineq
; ++i
) {
1094 int j
= isl_basic_map_alloc_inequality(dst
);
1095 isl_seq_cpy(dst
->ineq
[j
], src
->ineq
[i
], 1+total
);
1098 for (i
= 0; i
< src
->n_div
; ++i
) {
1099 int j
= isl_basic_map_alloc_div(dst
);
1100 isl_seq_cpy(dst
->div
[j
], src
->div
[i
], 1+1+total
);
1102 ISL_F_SET(dst
, ISL_BASIC_SET_FINAL
);
1105 struct isl_basic_map
*isl_basic_map_dup(struct isl_basic_map
*bmap
)
1107 struct isl_basic_map
*dup
;
1111 dup
= isl_basic_map_alloc_space(isl_space_copy(bmap
->dim
),
1112 bmap
->n_div
, bmap
->n_eq
, bmap
->n_ineq
);
1115 dup_constraints(dup
, bmap
);
1116 dup
->flags
= bmap
->flags
;
1117 dup
->sample
= isl_vec_copy(bmap
->sample
);
1121 struct isl_basic_set
*isl_basic_set_dup(struct isl_basic_set
*bset
)
1123 struct isl_basic_map
*dup
;
1125 dup
= isl_basic_map_dup((struct isl_basic_map
*)bset
);
1126 return (struct isl_basic_set
*)dup
;
1129 struct isl_basic_set
*isl_basic_set_copy(struct isl_basic_set
*bset
)
1134 if (ISL_F_ISSET(bset
, ISL_BASIC_SET_FINAL
)) {
1138 return isl_basic_set_dup(bset
);
1141 struct isl_set
*isl_set_copy(struct isl_set
*set
)
1150 struct isl_basic_map
*isl_basic_map_copy(struct isl_basic_map
*bmap
)
1155 if (ISL_F_ISSET(bmap
, ISL_BASIC_SET_FINAL
)) {
1159 bmap
= isl_basic_map_dup(bmap
);
1161 ISL_F_SET(bmap
, ISL_BASIC_SET_FINAL
);
1165 struct isl_map
*isl_map_copy(struct isl_map
*map
)
1174 __isl_null isl_basic_map
*isl_basic_map_free(__isl_take isl_basic_map
*bmap
)
1179 if (--bmap
->ref
> 0)
1182 isl_ctx_deref(bmap
->ctx
);
1184 isl_blk_free(bmap
->ctx
, bmap
->block2
);
1186 isl_blk_free(bmap
->ctx
, bmap
->block
);
1187 isl_vec_free(bmap
->sample
);
1188 isl_space_free(bmap
->dim
);
1194 __isl_null isl_basic_set
*isl_basic_set_free(__isl_take isl_basic_set
*bset
)
1196 return isl_basic_map_free((struct isl_basic_map
*)bset
);
1199 static int room_for_con(struct isl_basic_map
*bmap
, unsigned n
)
1201 return bmap
->n_eq
+ bmap
->n_ineq
+ n
<= bmap
->c_size
;
1204 __isl_give isl_map
*isl_map_align_params_map_map_and(
1205 __isl_take isl_map
*map1
, __isl_take isl_map
*map2
,
1206 __isl_give isl_map
*(*fn
)(__isl_take isl_map
*map1
,
1207 __isl_take isl_map
*map2
))
1211 if (isl_space_match(map1
->dim
, isl_dim_param
, map2
->dim
, isl_dim_param
))
1212 return fn(map1
, map2
);
1213 if (!isl_space_has_named_params(map1
->dim
) ||
1214 !isl_space_has_named_params(map2
->dim
))
1215 isl_die(map1
->ctx
, isl_error_invalid
,
1216 "unaligned unnamed parameters", goto error
);
1217 map1
= isl_map_align_params(map1
, isl_map_get_space(map2
));
1218 map2
= isl_map_align_params(map2
, isl_map_get_space(map1
));
1219 return fn(map1
, map2
);
1226 isl_bool
isl_map_align_params_map_map_and_test(__isl_keep isl_map
*map1
,
1227 __isl_keep isl_map
*map2
,
1228 isl_bool (*fn
)(__isl_keep isl_map
*map1
, __isl_keep isl_map
*map2
))
1233 return isl_bool_error
;
1234 if (isl_space_match(map1
->dim
, isl_dim_param
, map2
->dim
, isl_dim_param
))
1235 return fn(map1
, map2
);
1236 if (!isl_space_has_named_params(map1
->dim
) ||
1237 !isl_space_has_named_params(map2
->dim
))
1238 isl_die(map1
->ctx
, isl_error_invalid
,
1239 "unaligned unnamed parameters", return isl_bool_error
);
1240 map1
= isl_map_copy(map1
);
1241 map2
= isl_map_copy(map2
);
1242 map1
= isl_map_align_params(map1
, isl_map_get_space(map2
));
1243 map2
= isl_map_align_params(map2
, isl_map_get_space(map1
));
1250 int isl_basic_map_alloc_equality(struct isl_basic_map
*bmap
)
1252 struct isl_ctx
*ctx
;
1256 isl_assert(ctx
, room_for_con(bmap
, 1), return -1);
1257 isl_assert(ctx
, (bmap
->eq
- bmap
->ineq
) + bmap
->n_eq
<= bmap
->c_size
,
1259 ISL_F_CLR(bmap
, ISL_BASIC_MAP_NORMALIZED
);
1260 ISL_F_CLR(bmap
, ISL_BASIC_MAP_NO_REDUNDANT
);
1261 ISL_F_CLR(bmap
, ISL_BASIC_MAP_NO_IMPLICIT
);
1262 ISL_F_CLR(bmap
, ISL_BASIC_MAP_ALL_EQUALITIES
);
1263 ISL_F_CLR(bmap
, ISL_BASIC_MAP_NORMALIZED_DIVS
);
1264 if ((bmap
->eq
- bmap
->ineq
) + bmap
->n_eq
== bmap
->c_size
) {
1266 int j
= isl_basic_map_alloc_inequality(bmap
);
1270 bmap
->ineq
[j
] = bmap
->ineq
[bmap
->n_ineq
- 1];
1271 bmap
->ineq
[bmap
->n_ineq
- 1] = bmap
->eq
[-1];
1278 isl_seq_clr(bmap
->eq
[bmap
->n_eq
] + 1 + isl_basic_map_total_dim(bmap
),
1279 bmap
->extra
- bmap
->n_div
);
1280 return bmap
->n_eq
++;
1283 int isl_basic_set_alloc_equality(struct isl_basic_set
*bset
)
1285 return isl_basic_map_alloc_equality((struct isl_basic_map
*)bset
);
1288 int isl_basic_map_free_equality(struct isl_basic_map
*bmap
, unsigned n
)
1292 isl_assert(bmap
->ctx
, n
<= bmap
->n_eq
, return -1);
1297 int isl_basic_set_free_equality(struct isl_basic_set
*bset
, unsigned n
)
1299 return isl_basic_map_free_equality((struct isl_basic_map
*)bset
, n
);
1302 int isl_basic_map_drop_equality(struct isl_basic_map
*bmap
, unsigned pos
)
1307 isl_assert(bmap
->ctx
, pos
< bmap
->n_eq
, return -1);
1309 if (pos
!= bmap
->n_eq
- 1) {
1311 bmap
->eq
[pos
] = bmap
->eq
[bmap
->n_eq
- 1];
1312 bmap
->eq
[bmap
->n_eq
- 1] = t
;
1318 int isl_basic_set_drop_equality(struct isl_basic_set
*bset
, unsigned pos
)
1320 return isl_basic_map_drop_equality((struct isl_basic_map
*)bset
, pos
);
1323 /* Turn inequality "pos" of "bmap" into an equality.
1325 * In particular, we move the inequality in front of the equalities
1326 * and move the last inequality in the position of the moved inequality.
1327 * Note that isl_tab_make_equalities_explicit depends on this particular
1328 * change in the ordering of the constraints.
1330 void isl_basic_map_inequality_to_equality(
1331 struct isl_basic_map
*bmap
, unsigned pos
)
1335 t
= bmap
->ineq
[pos
];
1336 bmap
->ineq
[pos
] = bmap
->ineq
[bmap
->n_ineq
- 1];
1337 bmap
->ineq
[bmap
->n_ineq
- 1] = bmap
->eq
[-1];
1342 ISL_F_CLR(bmap
, ISL_BASIC_MAP_NO_REDUNDANT
);
1343 ISL_F_CLR(bmap
, ISL_BASIC_MAP_NORMALIZED
);
1344 ISL_F_CLR(bmap
, ISL_BASIC_MAP_NORMALIZED_DIVS
);
1345 ISL_F_CLR(bmap
, ISL_BASIC_MAP_ALL_EQUALITIES
);
1348 static int room_for_ineq(struct isl_basic_map
*bmap
, unsigned n
)
1350 return bmap
->n_ineq
+ n
<= bmap
->eq
- bmap
->ineq
;
1353 int isl_basic_map_alloc_inequality(struct isl_basic_map
*bmap
)
1355 struct isl_ctx
*ctx
;
1359 isl_assert(ctx
, room_for_ineq(bmap
, 1), return -1);
1360 ISL_F_CLR(bmap
, ISL_BASIC_MAP_NO_IMPLICIT
);
1361 ISL_F_CLR(bmap
, ISL_BASIC_MAP_NO_REDUNDANT
);
1362 ISL_F_CLR(bmap
, ISL_BASIC_MAP_NORMALIZED
);
1363 ISL_F_CLR(bmap
, ISL_BASIC_MAP_ALL_EQUALITIES
);
1364 isl_seq_clr(bmap
->ineq
[bmap
->n_ineq
] +
1365 1 + isl_basic_map_total_dim(bmap
),
1366 bmap
->extra
- bmap
->n_div
);
1367 return bmap
->n_ineq
++;
1370 int isl_basic_set_alloc_inequality(struct isl_basic_set
*bset
)
1372 return isl_basic_map_alloc_inequality((struct isl_basic_map
*)bset
);
1375 int isl_basic_map_free_inequality(struct isl_basic_map
*bmap
, unsigned n
)
1379 isl_assert(bmap
->ctx
, n
<= bmap
->n_ineq
, return -1);
1384 int isl_basic_set_free_inequality(struct isl_basic_set
*bset
, unsigned n
)
1386 return isl_basic_map_free_inequality((struct isl_basic_map
*)bset
, n
);
1389 int isl_basic_map_drop_inequality(struct isl_basic_map
*bmap
, unsigned pos
)
1394 isl_assert(bmap
->ctx
, pos
< bmap
->n_ineq
, return -1);
1396 if (pos
!= bmap
->n_ineq
- 1) {
1397 t
= bmap
->ineq
[pos
];
1398 bmap
->ineq
[pos
] = bmap
->ineq
[bmap
->n_ineq
- 1];
1399 bmap
->ineq
[bmap
->n_ineq
- 1] = t
;
1400 ISL_F_CLR(bmap
, ISL_BASIC_MAP_NORMALIZED
);
1406 int isl_basic_set_drop_inequality(struct isl_basic_set
*bset
, unsigned pos
)
1408 return isl_basic_map_drop_inequality((struct isl_basic_map
*)bset
, pos
);
1411 __isl_give isl_basic_map
*isl_basic_map_add_eq(__isl_take isl_basic_map
*bmap
,
1416 bmap
= isl_basic_map_extend_constraints(bmap
, 1, 0);
1419 k
= isl_basic_map_alloc_equality(bmap
);
1422 isl_seq_cpy(bmap
->eq
[k
], eq
, 1 + isl_basic_map_total_dim(bmap
));
1425 isl_basic_map_free(bmap
);
1429 __isl_give isl_basic_set
*isl_basic_set_add_eq(__isl_take isl_basic_set
*bset
,
1432 return (isl_basic_set
*)
1433 isl_basic_map_add_eq((isl_basic_map
*)bset
, eq
);
1436 __isl_give isl_basic_map
*isl_basic_map_add_ineq(__isl_take isl_basic_map
*bmap
,
1441 bmap
= isl_basic_map_extend_constraints(bmap
, 0, 1);
1444 k
= isl_basic_map_alloc_inequality(bmap
);
1447 isl_seq_cpy(bmap
->ineq
[k
], ineq
, 1 + isl_basic_map_total_dim(bmap
));
1450 isl_basic_map_free(bmap
);
1454 __isl_give isl_basic_set
*isl_basic_set_add_ineq(__isl_take isl_basic_set
*bset
,
1457 return (isl_basic_set
*)
1458 isl_basic_map_add_ineq((isl_basic_map
*)bset
, ineq
);
1461 int isl_basic_map_alloc_div(struct isl_basic_map
*bmap
)
1465 isl_assert(bmap
->ctx
, bmap
->n_div
< bmap
->extra
, return -1);
1466 isl_seq_clr(bmap
->div
[bmap
->n_div
] +
1467 1 + 1 + isl_basic_map_total_dim(bmap
),
1468 bmap
->extra
- bmap
->n_div
);
1469 ISL_F_CLR(bmap
, ISL_BASIC_MAP_NORMALIZED_DIVS
);
1470 return bmap
->n_div
++;
1473 int isl_basic_set_alloc_div(struct isl_basic_set
*bset
)
1475 return isl_basic_map_alloc_div((struct isl_basic_map
*)bset
);
1478 /* Insert an extra integer division, prescribed by "div", to "bmap"
1479 * at (integer division) position "pos".
1481 * The integer division is first added at the end and then moved
1482 * into the right position.
1484 __isl_give isl_basic_map
*isl_basic_map_insert_div(
1485 __isl_take isl_basic_map
*bmap
, int pos
, __isl_keep isl_vec
*div
)
1489 bmap
= isl_basic_map_cow(bmap
);
1491 return isl_basic_map_free(bmap
);
1493 if (div
->size
!= 1 + 1 + isl_basic_map_dim(bmap
, isl_dim_all
))
1494 isl_die(isl_basic_map_get_ctx(bmap
), isl_error_invalid
,
1495 "unexpected size", return isl_basic_map_free(bmap
));
1496 n_div
= isl_basic_map_dim(bmap
, isl_dim_div
);
1497 if (pos
< 0 || pos
> n_div
)
1498 isl_die(isl_basic_map_get_ctx(bmap
), isl_error_invalid
,
1499 "invalid position", return isl_basic_map_free(bmap
));
1501 bmap
= isl_basic_map_extend_space(bmap
,
1502 isl_basic_map_get_space(bmap
), 1, 0, 2);
1503 k
= isl_basic_map_alloc_div(bmap
);
1505 return isl_basic_map_free(bmap
);
1506 isl_seq_cpy(bmap
->div
[k
], div
->el
, div
->size
);
1507 isl_int_set_si(bmap
->div
[k
][div
->size
], 0);
1509 for (i
= k
; i
> pos
; --i
)
1510 isl_basic_map_swap_div(bmap
, i
, i
- 1);
1515 int isl_basic_map_free_div(struct isl_basic_map
*bmap
, unsigned n
)
1519 isl_assert(bmap
->ctx
, n
<= bmap
->n_div
, return -1);
1524 int isl_basic_set_free_div(struct isl_basic_set
*bset
, unsigned n
)
1526 return isl_basic_map_free_div((struct isl_basic_map
*)bset
, n
);
1529 /* Copy constraint from src to dst, putting the vars of src at offset
1530 * dim_off in dst and the divs of src at offset div_off in dst.
1531 * If both sets are actually map, then dim_off applies to the input
1534 static void copy_constraint(struct isl_basic_map
*dst_map
, isl_int
*dst
,
1535 struct isl_basic_map
*src_map
, isl_int
*src
,
1536 unsigned in_off
, unsigned out_off
, unsigned div_off
)
1538 unsigned src_nparam
= isl_basic_map_n_param(src_map
);
1539 unsigned dst_nparam
= isl_basic_map_n_param(dst_map
);
1540 unsigned src_in
= isl_basic_map_n_in(src_map
);
1541 unsigned dst_in
= isl_basic_map_n_in(dst_map
);
1542 unsigned src_out
= isl_basic_map_n_out(src_map
);
1543 unsigned dst_out
= isl_basic_map_n_out(dst_map
);
1544 isl_int_set(dst
[0], src
[0]);
1545 isl_seq_cpy(dst
+1, src
+1, isl_min(dst_nparam
, src_nparam
));
1546 if (dst_nparam
> src_nparam
)
1547 isl_seq_clr(dst
+1+src_nparam
,
1548 dst_nparam
- src_nparam
);
1549 isl_seq_clr(dst
+1+dst_nparam
, in_off
);
1550 isl_seq_cpy(dst
+1+dst_nparam
+in_off
,
1552 isl_min(dst_in
-in_off
, src_in
));
1553 if (dst_in
-in_off
> src_in
)
1554 isl_seq_clr(dst
+1+dst_nparam
+in_off
+src_in
,
1555 dst_in
- in_off
- src_in
);
1556 isl_seq_clr(dst
+1+dst_nparam
+dst_in
, out_off
);
1557 isl_seq_cpy(dst
+1+dst_nparam
+dst_in
+out_off
,
1558 src
+1+src_nparam
+src_in
,
1559 isl_min(dst_out
-out_off
, src_out
));
1560 if (dst_out
-out_off
> src_out
)
1561 isl_seq_clr(dst
+1+dst_nparam
+dst_in
+out_off
+src_out
,
1562 dst_out
- out_off
- src_out
);
1563 isl_seq_clr(dst
+1+dst_nparam
+dst_in
+dst_out
, div_off
);
1564 isl_seq_cpy(dst
+1+dst_nparam
+dst_in
+dst_out
+div_off
,
1565 src
+1+src_nparam
+src_in
+src_out
,
1566 isl_min(dst_map
->extra
-div_off
, src_map
->n_div
));
1567 if (dst_map
->n_div
-div_off
> src_map
->n_div
)
1568 isl_seq_clr(dst
+1+dst_nparam
+dst_in
+dst_out
+
1569 div_off
+src_map
->n_div
,
1570 dst_map
->n_div
- div_off
- src_map
->n_div
);
1573 static void copy_div(struct isl_basic_map
*dst_map
, isl_int
*dst
,
1574 struct isl_basic_map
*src_map
, isl_int
*src
,
1575 unsigned in_off
, unsigned out_off
, unsigned div_off
)
1577 isl_int_set(dst
[0], src
[0]);
1578 copy_constraint(dst_map
, dst
+1, src_map
, src
+1, in_off
, out_off
, div_off
);
1581 static struct isl_basic_map
*add_constraints(struct isl_basic_map
*bmap1
,
1582 struct isl_basic_map
*bmap2
, unsigned i_pos
, unsigned o_pos
)
1587 if (!bmap1
|| !bmap2
)
1590 div_off
= bmap1
->n_div
;
1592 for (i
= 0; i
< bmap2
->n_eq
; ++i
) {
1593 int i1
= isl_basic_map_alloc_equality(bmap1
);
1596 copy_constraint(bmap1
, bmap1
->eq
[i1
], bmap2
, bmap2
->eq
[i
],
1597 i_pos
, o_pos
, div_off
);
1600 for (i
= 0; i
< bmap2
->n_ineq
; ++i
) {
1601 int i1
= isl_basic_map_alloc_inequality(bmap1
);
1604 copy_constraint(bmap1
, bmap1
->ineq
[i1
], bmap2
, bmap2
->ineq
[i
],
1605 i_pos
, o_pos
, div_off
);
1608 for (i
= 0; i
< bmap2
->n_div
; ++i
) {
1609 int i1
= isl_basic_map_alloc_div(bmap1
);
1612 copy_div(bmap1
, bmap1
->div
[i1
], bmap2
, bmap2
->div
[i
],
1613 i_pos
, o_pos
, div_off
);
1616 isl_basic_map_free(bmap2
);
1621 isl_basic_map_free(bmap1
);
1622 isl_basic_map_free(bmap2
);
1626 struct isl_basic_set
*isl_basic_set_add_constraints(struct isl_basic_set
*bset1
,
1627 struct isl_basic_set
*bset2
, unsigned pos
)
1629 return (struct isl_basic_set
*)
1630 add_constraints((struct isl_basic_map
*)bset1
,
1631 (struct isl_basic_map
*)bset2
, 0, pos
);
1634 struct isl_basic_map
*isl_basic_map_extend_space(struct isl_basic_map
*base
,
1635 __isl_take isl_space
*dim
, unsigned extra
,
1636 unsigned n_eq
, unsigned n_ineq
)
1638 struct isl_basic_map
*ext
;
1648 dims_ok
= isl_space_is_equal(base
->dim
, dim
) &&
1649 base
->extra
>= base
->n_div
+ extra
;
1651 if (dims_ok
&& room_for_con(base
, n_eq
+ n_ineq
) &&
1652 room_for_ineq(base
, n_ineq
)) {
1653 isl_space_free(dim
);
1657 isl_assert(base
->ctx
, base
->dim
->nparam
<= dim
->nparam
, goto error
);
1658 isl_assert(base
->ctx
, base
->dim
->n_in
<= dim
->n_in
, goto error
);
1659 isl_assert(base
->ctx
, base
->dim
->n_out
<= dim
->n_out
, goto error
);
1660 extra
+= base
->extra
;
1662 n_ineq
+= base
->n_ineq
;
1664 ext
= isl_basic_map_alloc_space(dim
, extra
, n_eq
, n_ineq
);
1670 ext
->sample
= isl_vec_copy(base
->sample
);
1671 flags
= base
->flags
;
1672 ext
= add_constraints(ext
, base
, 0, 0);
1675 ISL_F_CLR(ext
, ISL_BASIC_SET_FINAL
);
1681 isl_space_free(dim
);
1682 isl_basic_map_free(base
);
1686 struct isl_basic_set
*isl_basic_set_extend_space(struct isl_basic_set
*base
,
1687 __isl_take isl_space
*dim
, unsigned extra
,
1688 unsigned n_eq
, unsigned n_ineq
)
1690 return (struct isl_basic_set
*)
1691 isl_basic_map_extend_space((struct isl_basic_map
*)base
, dim
,
1692 extra
, n_eq
, n_ineq
);
1695 struct isl_basic_map
*isl_basic_map_extend_constraints(
1696 struct isl_basic_map
*base
, unsigned n_eq
, unsigned n_ineq
)
1700 return isl_basic_map_extend_space(base
, isl_space_copy(base
->dim
),
1704 struct isl_basic_map
*isl_basic_map_extend(struct isl_basic_map
*base
,
1705 unsigned nparam
, unsigned n_in
, unsigned n_out
, unsigned extra
,
1706 unsigned n_eq
, unsigned n_ineq
)
1708 struct isl_basic_map
*bmap
;
1713 dim
= isl_space_alloc(base
->ctx
, nparam
, n_in
, n_out
);
1717 bmap
= isl_basic_map_extend_space(base
, dim
, extra
, n_eq
, n_ineq
);
1720 isl_basic_map_free(base
);
1724 struct isl_basic_set
*isl_basic_set_extend(struct isl_basic_set
*base
,
1725 unsigned nparam
, unsigned dim
, unsigned extra
,
1726 unsigned n_eq
, unsigned n_ineq
)
1728 return (struct isl_basic_set
*)
1729 isl_basic_map_extend((struct isl_basic_map
*)base
,
1730 nparam
, 0, dim
, extra
, n_eq
, n_ineq
);
1733 struct isl_basic_set
*isl_basic_set_extend_constraints(
1734 struct isl_basic_set
*base
, unsigned n_eq
, unsigned n_ineq
)
1736 return (struct isl_basic_set
*)
1737 isl_basic_map_extend_constraints((struct isl_basic_map
*)base
,
1741 struct isl_basic_set
*isl_basic_set_cow(struct isl_basic_set
*bset
)
1743 return (struct isl_basic_set
*)
1744 isl_basic_map_cow((struct isl_basic_map
*)bset
);
1747 struct isl_basic_map
*isl_basic_map_cow(struct isl_basic_map
*bmap
)
1752 if (bmap
->ref
> 1) {
1754 bmap
= isl_basic_map_dup(bmap
);
1757 ISL_F_CLR(bmap
, ISL_BASIC_SET_FINAL
);
1758 ISL_F_CLR(bmap
, ISL_BASIC_MAP_REDUCED_COEFFICIENTS
);
1763 /* Clear all cached information in "map", either because it is about
1764 * to be modified or because it is being freed.
1765 * Always return the same pointer that is passed in.
1766 * This is needed for the use in isl_map_free.
1768 static __isl_give isl_map
*clear_caches(__isl_take isl_map
*map
)
1770 isl_basic_map_free(map
->cached_simple_hull
[0]);
1771 isl_basic_map_free(map
->cached_simple_hull
[1]);
1772 map
->cached_simple_hull
[0] = NULL
;
1773 map
->cached_simple_hull
[1] = NULL
;
1777 struct isl_set
*isl_set_cow(struct isl_set
*set
)
1779 return isl_map_cow(set
);
1782 /* Return an isl_map that is equal to "map" and that has only
1783 * a single reference.
1785 * If the original input already has only one reference, then
1786 * simply return it, but clear all cached information, since
1787 * it may be rendered invalid by the operations that will be
1788 * performed on the result.
1790 * Otherwise, create a duplicate (without any cached information).
1792 struct isl_map
*isl_map_cow(struct isl_map
*map
)
1798 return clear_caches(map
);
1800 return isl_map_dup(map
);
1803 static void swap_vars(struct isl_blk blk
, isl_int
*a
,
1804 unsigned a_len
, unsigned b_len
)
1806 isl_seq_cpy(blk
.data
, a
+a_len
, b_len
);
1807 isl_seq_cpy(blk
.data
+b_len
, a
, a_len
);
1808 isl_seq_cpy(a
, blk
.data
, b_len
+a_len
);
1811 static __isl_give isl_basic_map
*isl_basic_map_swap_vars(
1812 __isl_take isl_basic_map
*bmap
, unsigned pos
, unsigned n1
, unsigned n2
)
1820 isl_assert(bmap
->ctx
,
1821 pos
+ n1
+ n2
<= 1 + isl_basic_map_total_dim(bmap
), goto error
);
1823 if (n1
== 0 || n2
== 0)
1826 bmap
= isl_basic_map_cow(bmap
);
1830 blk
= isl_blk_alloc(bmap
->ctx
, n1
+ n2
);
1831 if (isl_blk_is_error(blk
))
1834 for (i
= 0; i
< bmap
->n_eq
; ++i
)
1836 bmap
->eq
[i
] + pos
, n1
, n2
);
1838 for (i
= 0; i
< bmap
->n_ineq
; ++i
)
1840 bmap
->ineq
[i
] + pos
, n1
, n2
);
1842 for (i
= 0; i
< bmap
->n_div
; ++i
)
1844 bmap
->div
[i
]+1 + pos
, n1
, n2
);
1846 isl_blk_free(bmap
->ctx
, blk
);
1848 ISL_F_CLR(bmap
, ISL_BASIC_SET_NORMALIZED
);
1849 bmap
= isl_basic_map_gauss(bmap
, NULL
);
1850 return isl_basic_map_finalize(bmap
);
1852 isl_basic_map_free(bmap
);
1856 struct isl_basic_map
*isl_basic_map_set_to_empty(struct isl_basic_map
*bmap
)
1862 total
= isl_basic_map_total_dim(bmap
);
1863 isl_basic_map_free_div(bmap
, bmap
->n_div
);
1864 isl_basic_map_free_inequality(bmap
, bmap
->n_ineq
);
1866 isl_basic_map_free_equality(bmap
, bmap
->n_eq
-1);
1868 i
= isl_basic_map_alloc_equality(bmap
);
1872 isl_int_set_si(bmap
->eq
[i
][0], 1);
1873 isl_seq_clr(bmap
->eq
[i
]+1, total
);
1874 ISL_F_SET(bmap
, ISL_BASIC_MAP_EMPTY
);
1875 isl_vec_free(bmap
->sample
);
1876 bmap
->sample
= NULL
;
1877 return isl_basic_map_finalize(bmap
);
1879 isl_basic_map_free(bmap
);
1883 struct isl_basic_set
*isl_basic_set_set_to_empty(struct isl_basic_set
*bset
)
1885 return (struct isl_basic_set
*)
1886 isl_basic_map_set_to_empty((struct isl_basic_map
*)bset
);
1889 /* Swap divs "a" and "b" in "bmap" (without modifying any of the constraints
1892 static void swap_div(__isl_keep isl_basic_map
*bmap
, int a
, int b
)
1894 isl_int
*t
= bmap
->div
[a
];
1895 bmap
->div
[a
] = bmap
->div
[b
];
1899 /* Swap divs "a" and "b" in "bmap" and adjust the constraints and
1900 * div definitions accordingly.
1902 void isl_basic_map_swap_div(struct isl_basic_map
*bmap
, int a
, int b
)
1905 unsigned off
= isl_space_dim(bmap
->dim
, isl_dim_all
);
1907 swap_div(bmap
, a
, b
);
1909 for (i
= 0; i
< bmap
->n_eq
; ++i
)
1910 isl_int_swap(bmap
->eq
[i
][1+off
+a
], bmap
->eq
[i
][1+off
+b
]);
1912 for (i
= 0; i
< bmap
->n_ineq
; ++i
)
1913 isl_int_swap(bmap
->ineq
[i
][1+off
+a
], bmap
->ineq
[i
][1+off
+b
]);
1915 for (i
= 0; i
< bmap
->n_div
; ++i
)
1916 isl_int_swap(bmap
->div
[i
][1+1+off
+a
], bmap
->div
[i
][1+1+off
+b
]);
1917 ISL_F_CLR(bmap
, ISL_BASIC_MAP_NORMALIZED
);
1920 /* Swap divs "a" and "b" in "bset" and adjust the constraints and
1921 * div definitions accordingly.
1923 void isl_basic_set_swap_div(__isl_keep isl_basic_set
*bset
, int a
, int b
)
1925 isl_basic_map_swap_div(bset
, a
, b
);
1928 /* Eliminate the specified n dimensions starting at first from the
1929 * constraints, without removing the dimensions from the space.
1930 * If the set is rational, the dimensions are eliminated using Fourier-Motzkin.
1932 __isl_give isl_map
*isl_map_eliminate(__isl_take isl_map
*map
,
1933 enum isl_dim_type type
, unsigned first
, unsigned n
)
1942 if (first
+ n
> isl_map_dim(map
, type
) || first
+ n
< first
)
1943 isl_die(map
->ctx
, isl_error_invalid
,
1944 "index out of bounds", goto error
);
1946 map
= isl_map_cow(map
);
1950 for (i
= 0; i
< map
->n
; ++i
) {
1951 map
->p
[i
] = isl_basic_map_eliminate(map
->p
[i
], type
, first
, n
);
1961 /* Eliminate the specified n dimensions starting at first from the
1962 * constraints, without removing the dimensions from the space.
1963 * If the set is rational, the dimensions are eliminated using Fourier-Motzkin.
1965 __isl_give isl_set
*isl_set_eliminate(__isl_take isl_set
*set
,
1966 enum isl_dim_type type
, unsigned first
, unsigned n
)
1968 return (isl_set
*)isl_map_eliminate((isl_map
*)set
, type
, first
, n
);
1971 /* Eliminate the specified n dimensions starting at first from the
1972 * constraints, without removing the dimensions from the space.
1973 * If the set is rational, the dimensions are eliminated using Fourier-Motzkin.
1975 __isl_give isl_set
*isl_set_eliminate_dims(__isl_take isl_set
*set
,
1976 unsigned first
, unsigned n
)
1978 return isl_set_eliminate(set
, isl_dim_set
, first
, n
);
1981 __isl_give isl_basic_map
*isl_basic_map_remove_divs(
1982 __isl_take isl_basic_map
*bmap
)
1986 bmap
= isl_basic_map_eliminate_vars(bmap
,
1987 isl_space_dim(bmap
->dim
, isl_dim_all
), bmap
->n_div
);
1991 return isl_basic_map_finalize(bmap
);
1994 __isl_give isl_basic_set
*isl_basic_set_remove_divs(
1995 __isl_take isl_basic_set
*bset
)
1997 return (struct isl_basic_set
*)isl_basic_map_remove_divs(
1998 (struct isl_basic_map
*)bset
);
2001 __isl_give isl_map
*isl_map_remove_divs(__isl_take isl_map
*map
)
2010 map
= isl_map_cow(map
);
2014 for (i
= 0; i
< map
->n
; ++i
) {
2015 map
->p
[i
] = isl_basic_map_remove_divs(map
->p
[i
]);
2025 __isl_give isl_set
*isl_set_remove_divs(__isl_take isl_set
*set
)
2027 return isl_map_remove_divs(set
);
2030 struct isl_basic_map
*isl_basic_map_remove_dims(struct isl_basic_map
*bmap
,
2031 enum isl_dim_type type
, unsigned first
, unsigned n
)
2035 isl_assert(bmap
->ctx
, first
+ n
<= isl_basic_map_dim(bmap
, type
),
2037 if (n
== 0 && !isl_space_is_named_or_nested(bmap
->dim
, type
))
2039 bmap
= isl_basic_map_eliminate_vars(bmap
,
2040 isl_basic_map_offset(bmap
, type
) - 1 + first
, n
);
2043 if (ISL_F_ISSET(bmap
, ISL_BASIC_MAP_EMPTY
) && type
== isl_dim_div
)
2045 bmap
= isl_basic_map_drop(bmap
, type
, first
, n
);
2048 isl_basic_map_free(bmap
);
2052 /* Return true if the definition of the given div (recursively) involves
2053 * any of the given variables.
2055 static int div_involves_vars(__isl_keep isl_basic_map
*bmap
, int div
,
2056 unsigned first
, unsigned n
)
2059 unsigned div_offset
= isl_basic_map_offset(bmap
, isl_dim_div
);
2061 if (isl_int_is_zero(bmap
->div
[div
][0]))
2063 if (isl_seq_first_non_zero(bmap
->div
[div
] + 1 + first
, n
) >= 0)
2066 for (i
= bmap
->n_div
- 1; i
>= 0; --i
) {
2067 if (isl_int_is_zero(bmap
->div
[div
][1 + div_offset
+ i
]))
2069 if (div_involves_vars(bmap
, i
, first
, n
))
2076 /* Try and add a lower and/or upper bound on "div" to "bmap"
2077 * based on inequality "i".
2078 * "total" is the total number of variables (excluding the divs).
2079 * "v" is a temporary object that can be used during the calculations.
2080 * If "lb" is set, then a lower bound should be constructed.
2081 * If "ub" is set, then an upper bound should be constructed.
2083 * The calling function has already checked that the inequality does not
2084 * reference "div", but we still need to check that the inequality is
2085 * of the right form. We'll consider the case where we want to construct
2086 * a lower bound. The construction of upper bounds is similar.
2088 * Let "div" be of the form
2090 * q = floor((a + f(x))/d)
2092 * We essentially check if constraint "i" is of the form
2096 * so that we can use it to derive a lower bound on "div".
2097 * However, we allow a slightly more general form
2101 * with the condition that the coefficients of g(x) - f(x) are all
2103 * Rewriting this constraint as
2107 * adding a + f(x) to both sides and dividing by d, we obtain
2109 * (a + f(x))/d >= (a-b)/d + (f(x)-g(x))/d
2111 * Taking the floor on both sides, we obtain
2113 * q >= floor((a-b)/d) + (f(x)-g(x))/d
2117 * (g(x)-f(x))/d + ceil((b-a)/d) + q >= 0
2119 * In the case of an upper bound, we construct the constraint
2121 * (g(x)+f(x))/d + floor((b+a)/d) - q >= 0
2124 static __isl_give isl_basic_map
*insert_bounds_on_div_from_ineq(
2125 __isl_take isl_basic_map
*bmap
, int div
, int i
,
2126 unsigned total
, isl_int v
, int lb
, int ub
)
2130 for (j
= 0; (lb
|| ub
) && j
< total
+ bmap
->n_div
; ++j
) {
2132 isl_int_sub(v
, bmap
->ineq
[i
][1 + j
],
2133 bmap
->div
[div
][1 + 1 + j
]);
2134 lb
= isl_int_is_divisible_by(v
, bmap
->div
[div
][0]);
2137 isl_int_add(v
, bmap
->ineq
[i
][1 + j
],
2138 bmap
->div
[div
][1 + 1 + j
]);
2139 ub
= isl_int_is_divisible_by(v
, bmap
->div
[div
][0]);
2145 bmap
= isl_basic_map_cow(bmap
);
2146 bmap
= isl_basic_map_extend_constraints(bmap
, 0, lb
+ ub
);
2148 int k
= isl_basic_map_alloc_inequality(bmap
);
2151 for (j
= 0; j
< 1 + total
+ bmap
->n_div
; ++j
) {
2152 isl_int_sub(bmap
->ineq
[k
][j
], bmap
->ineq
[i
][j
],
2153 bmap
->div
[div
][1 + j
]);
2154 isl_int_cdiv_q(bmap
->ineq
[k
][j
],
2155 bmap
->ineq
[k
][j
], bmap
->div
[div
][0]);
2157 isl_int_set_si(bmap
->ineq
[k
][1 + total
+ div
], 1);
2160 int k
= isl_basic_map_alloc_inequality(bmap
);
2163 for (j
= 0; j
< 1 + total
+ bmap
->n_div
; ++j
) {
2164 isl_int_add(bmap
->ineq
[k
][j
], bmap
->ineq
[i
][j
],
2165 bmap
->div
[div
][1 + j
]);
2166 isl_int_fdiv_q(bmap
->ineq
[k
][j
],
2167 bmap
->ineq
[k
][j
], bmap
->div
[div
][0]);
2169 isl_int_set_si(bmap
->ineq
[k
][1 + total
+ div
], -1);
2172 ISL_F_CLR(bmap
, ISL_BASIC_MAP_NORMALIZED
);
2175 isl_basic_map_free(bmap
);
2179 /* This function is called right before "div" is eliminated from "bmap"
2180 * using Fourier-Motzkin.
2181 * Look through the constraints of "bmap" for constraints on the argument
2182 * of the integer division and use them to construct constraints on the
2183 * integer division itself. These constraints can then be combined
2184 * during the Fourier-Motzkin elimination.
2185 * Note that it is only useful to introduce lower bounds on "div"
2186 * if "bmap" already contains upper bounds on "div" as the newly
2187 * introduce lower bounds can then be combined with the pre-existing
2188 * upper bounds. Similarly for upper bounds.
2189 * We therefore first check if "bmap" contains any lower and/or upper bounds
2192 * It is interesting to note that the introduction of these constraints
2193 * can indeed lead to more accurate results, even when compared to
2194 * deriving constraints on the argument of "div" from constraints on "div".
2195 * Consider, for example, the set
2197 * { [i,j,k] : 3 + i + 2j >= 0 and 2 * [(i+2j)/4] <= k }
2199 * The second constraint can be rewritten as
2201 * 2 * [(-i-2j+3)/4] + k >= 0
2203 * from which we can derive
2205 * -i - 2j + 3 >= -2k
2211 * Combined with the first constraint, we obtain
2213 * -3 <= 3 + 2k or k >= -3
2215 * If, on the other hand we derive a constraint on [(i+2j)/4] from
2216 * the first constraint, we obtain
2218 * [(i + 2j)/4] >= [-3/4] = -1
2220 * Combining this constraint with the second constraint, we obtain
2224 static __isl_give isl_basic_map
*insert_bounds_on_div(
2225 __isl_take isl_basic_map
*bmap
, int div
)
2228 int check_lb
, check_ub
;
2235 if (isl_int_is_zero(bmap
->div
[div
][0]))
2238 total
= isl_space_dim(bmap
->dim
, isl_dim_all
);
2242 for (i
= 0; (!check_lb
|| !check_ub
) && i
< bmap
->n_ineq
; ++i
) {
2243 int s
= isl_int_sgn(bmap
->ineq
[i
][1 + total
+ div
]);
2250 if (!check_lb
&& !check_ub
)
2255 for (i
= 0; bmap
&& i
< bmap
->n_ineq
; ++i
) {
2256 if (!isl_int_is_zero(bmap
->ineq
[i
][1 + total
+ div
]))
2259 bmap
= insert_bounds_on_div_from_ineq(bmap
, div
, i
, total
, v
,
2260 check_lb
, check_ub
);
2268 /* Remove all divs (recursively) involving any of the given dimensions
2269 * in their definitions.
2271 __isl_give isl_basic_map
*isl_basic_map_remove_divs_involving_dims(
2272 __isl_take isl_basic_map
*bmap
,
2273 enum isl_dim_type type
, unsigned first
, unsigned n
)
2279 isl_assert(bmap
->ctx
, first
+ n
<= isl_basic_map_dim(bmap
, type
),
2281 first
+= isl_basic_map_offset(bmap
, type
);
2283 for (i
= bmap
->n_div
- 1; i
>= 0; --i
) {
2284 if (!div_involves_vars(bmap
, i
, first
, n
))
2286 bmap
= insert_bounds_on_div(bmap
, i
);
2287 bmap
= isl_basic_map_remove_dims(bmap
, isl_dim_div
, i
, 1);
2295 isl_basic_map_free(bmap
);
2299 __isl_give isl_basic_set
*isl_basic_set_remove_divs_involving_dims(
2300 __isl_take isl_basic_set
*bset
,
2301 enum isl_dim_type type
, unsigned first
, unsigned n
)
2303 return isl_basic_map_remove_divs_involving_dims(bset
, type
, first
, n
);
2306 __isl_give isl_map
*isl_map_remove_divs_involving_dims(__isl_take isl_map
*map
,
2307 enum isl_dim_type type
, unsigned first
, unsigned n
)
2316 map
= isl_map_cow(map
);
2320 for (i
= 0; i
< map
->n
; ++i
) {
2321 map
->p
[i
] = isl_basic_map_remove_divs_involving_dims(map
->p
[i
],
2332 __isl_give isl_set
*isl_set_remove_divs_involving_dims(__isl_take isl_set
*set
,
2333 enum isl_dim_type type
, unsigned first
, unsigned n
)
2335 return (isl_set
*)isl_map_remove_divs_involving_dims((isl_map
*)set
,
2339 /* Does the desciption of "bmap" depend on the specified dimensions?
2340 * We also check whether the dimensions appear in any of the div definitions.
2341 * In principle there is no need for this check. If the dimensions appear
2342 * in a div definition, they also appear in the defining constraints of that
2345 isl_bool
isl_basic_map_involves_dims(__isl_keep isl_basic_map
*bmap
,
2346 enum isl_dim_type type
, unsigned first
, unsigned n
)
2351 return isl_bool_error
;
2353 if (first
+ n
> isl_basic_map_dim(bmap
, type
))
2354 isl_die(bmap
->ctx
, isl_error_invalid
,
2355 "index out of bounds", return isl_bool_error
);
2357 first
+= isl_basic_map_offset(bmap
, type
);
2358 for (i
= 0; i
< bmap
->n_eq
; ++i
)
2359 if (isl_seq_first_non_zero(bmap
->eq
[i
] + first
, n
) >= 0)
2360 return isl_bool_true
;
2361 for (i
= 0; i
< bmap
->n_ineq
; ++i
)
2362 if (isl_seq_first_non_zero(bmap
->ineq
[i
] + first
, n
) >= 0)
2363 return isl_bool_true
;
2364 for (i
= 0; i
< bmap
->n_div
; ++i
) {
2365 if (isl_int_is_zero(bmap
->div
[i
][0]))
2367 if (isl_seq_first_non_zero(bmap
->div
[i
] + 1 + first
, n
) >= 0)
2368 return isl_bool_true
;
2371 return isl_bool_false
;
2374 isl_bool
isl_map_involves_dims(__isl_keep isl_map
*map
,
2375 enum isl_dim_type type
, unsigned first
, unsigned n
)
2380 return isl_bool_error
;
2382 if (first
+ n
> isl_map_dim(map
, type
))
2383 isl_die(map
->ctx
, isl_error_invalid
,
2384 "index out of bounds", return isl_bool_error
);
2386 for (i
= 0; i
< map
->n
; ++i
) {
2387 isl_bool involves
= isl_basic_map_involves_dims(map
->p
[i
],
2389 if (involves
< 0 || involves
)
2393 return isl_bool_false
;
2396 isl_bool
isl_basic_set_involves_dims(__isl_keep isl_basic_set
*bset
,
2397 enum isl_dim_type type
, unsigned first
, unsigned n
)
2399 return isl_basic_map_involves_dims(bset
, type
, first
, n
);
2402 isl_bool
isl_set_involves_dims(__isl_keep isl_set
*set
,
2403 enum isl_dim_type type
, unsigned first
, unsigned n
)
2405 return isl_map_involves_dims(set
, type
, first
, n
);
2408 /* Does local variable "div" of "bmap" have a complete explicit representation?
2409 * Having a complete explicit representation requires not only
2410 * an explicit representation, but also that all local variables
2411 * that appear in this explicit representation in turn have
2412 * a complete explicit representation.
2414 isl_bool
isl_basic_map_div_is_known(__isl_keep isl_basic_map
*bmap
, int div
)
2417 unsigned div_offset
= isl_basic_map_offset(bmap
, isl_dim_div
);
2420 marked
= isl_basic_map_div_is_marked_unknown(bmap
, div
);
2421 if (marked
< 0 || marked
)
2422 return isl_bool_not(marked
);
2424 for (i
= bmap
->n_div
- 1; i
>= 0; --i
) {
2427 if (isl_int_is_zero(bmap
->div
[div
][1 + div_offset
+ i
]))
2429 known
= isl_basic_map_div_is_known(bmap
, i
);
2430 if (known
< 0 || !known
)
2434 return isl_bool_true
;
2437 /* Does local variable "div" of "bset" have a complete explicit representation?
2439 isl_bool
isl_basic_set_div_is_known(__isl_keep isl_basic_set
*bset
, int div
)
2441 return isl_basic_map_div_is_known(bset
, div
);
2444 /* Remove all divs that are unknown or defined in terms of unknown divs.
2446 __isl_give isl_basic_map
*isl_basic_map_remove_unknown_divs(
2447 __isl_take isl_basic_map
*bmap
)
2454 for (i
= bmap
->n_div
- 1; i
>= 0; --i
) {
2455 if (isl_basic_map_div_is_known(bmap
, i
))
2457 bmap
= isl_basic_map_remove_dims(bmap
, isl_dim_div
, i
, 1);
2466 /* Remove all divs that are unknown or defined in terms of unknown divs.
2468 __isl_give isl_basic_set
*isl_basic_set_remove_unknown_divs(
2469 __isl_take isl_basic_set
*bset
)
2471 return isl_basic_map_remove_unknown_divs(bset
);
2474 __isl_give isl_map
*isl_map_remove_unknown_divs(__isl_take isl_map
*map
)
2483 map
= isl_map_cow(map
);
2487 for (i
= 0; i
< map
->n
; ++i
) {
2488 map
->p
[i
] = isl_basic_map_remove_unknown_divs(map
->p
[i
]);
2498 __isl_give isl_set
*isl_set_remove_unknown_divs(__isl_take isl_set
*set
)
2500 return (isl_set
*)isl_map_remove_unknown_divs((isl_map
*)set
);
2503 __isl_give isl_basic_set
*isl_basic_set_remove_dims(
2504 __isl_take isl_basic_set
*bset
,
2505 enum isl_dim_type type
, unsigned first
, unsigned n
)
2507 return (isl_basic_set
*)
2508 isl_basic_map_remove_dims((isl_basic_map
*)bset
, type
, first
, n
);
2511 struct isl_map
*isl_map_remove_dims(struct isl_map
*map
,
2512 enum isl_dim_type type
, unsigned first
, unsigned n
)
2519 map
= isl_map_cow(map
);
2522 isl_assert(map
->ctx
, first
+ n
<= isl_map_dim(map
, type
), goto error
);
2524 for (i
= 0; i
< map
->n
; ++i
) {
2525 map
->p
[i
] = isl_basic_map_eliminate_vars(map
->p
[i
],
2526 isl_basic_map_offset(map
->p
[i
], type
) - 1 + first
, n
);
2530 map
= isl_map_drop(map
, type
, first
, n
);
2537 __isl_give isl_set
*isl_set_remove_dims(__isl_take isl_set
*bset
,
2538 enum isl_dim_type type
, unsigned first
, unsigned n
)
2540 return (isl_set
*)isl_map_remove_dims((isl_map
*)bset
, type
, first
, n
);
2543 /* Project out n inputs starting at first using Fourier-Motzkin */
2544 struct isl_map
*isl_map_remove_inputs(struct isl_map
*map
,
2545 unsigned first
, unsigned n
)
2547 return isl_map_remove_dims(map
, isl_dim_in
, first
, n
);
2550 static void dump_term(struct isl_basic_map
*bmap
,
2551 isl_int c
, int pos
, FILE *out
)
2554 unsigned in
= isl_basic_map_n_in(bmap
);
2555 unsigned dim
= in
+ isl_basic_map_n_out(bmap
);
2556 unsigned nparam
= isl_basic_map_n_param(bmap
);
2558 isl_int_print(out
, c
, 0);
2560 if (!isl_int_is_one(c
))
2561 isl_int_print(out
, c
, 0);
2562 if (pos
< 1 + nparam
) {
2563 name
= isl_space_get_dim_name(bmap
->dim
,
2564 isl_dim_param
, pos
- 1);
2566 fprintf(out
, "%s", name
);
2568 fprintf(out
, "p%d", pos
- 1);
2569 } else if (pos
< 1 + nparam
+ in
)
2570 fprintf(out
, "i%d", pos
- 1 - nparam
);
2571 else if (pos
< 1 + nparam
+ dim
)
2572 fprintf(out
, "o%d", pos
- 1 - nparam
- in
);
2574 fprintf(out
, "e%d", pos
- 1 - nparam
- dim
);
2578 static void dump_constraint_sign(struct isl_basic_map
*bmap
, isl_int
*c
,
2579 int sign
, FILE *out
)
2583 unsigned len
= 1 + isl_basic_map_total_dim(bmap
);
2587 for (i
= 0, first
= 1; i
< len
; ++i
) {
2588 if (isl_int_sgn(c
[i
]) * sign
<= 0)
2591 fprintf(out
, " + ");
2593 isl_int_abs(v
, c
[i
]);
2594 dump_term(bmap
, v
, i
, out
);
2601 static void dump_constraint(struct isl_basic_map
*bmap
, isl_int
*c
,
2602 const char *op
, FILE *out
, int indent
)
2606 fprintf(out
, "%*s", indent
, "");
2608 dump_constraint_sign(bmap
, c
, 1, out
);
2609 fprintf(out
, " %s ", op
);
2610 dump_constraint_sign(bmap
, c
, -1, out
);
2614 for (i
= bmap
->n_div
; i
< bmap
->extra
; ++i
) {
2615 if (isl_int_is_zero(c
[1+isl_space_dim(bmap
->dim
, isl_dim_all
)+i
]))
2617 fprintf(out
, "%*s", indent
, "");
2618 fprintf(out
, "ERROR: unused div coefficient not zero\n");
2623 static void dump_constraints(struct isl_basic_map
*bmap
,
2624 isl_int
**c
, unsigned n
,
2625 const char *op
, FILE *out
, int indent
)
2629 for (i
= 0; i
< n
; ++i
)
2630 dump_constraint(bmap
, c
[i
], op
, out
, indent
);
2633 static void dump_affine(struct isl_basic_map
*bmap
, isl_int
*exp
, FILE *out
)
2637 unsigned total
= isl_basic_map_total_dim(bmap
);
2639 for (j
= 0; j
< 1 + total
; ++j
) {
2640 if (isl_int_is_zero(exp
[j
]))
2642 if (!first
&& isl_int_is_pos(exp
[j
]))
2644 dump_term(bmap
, exp
[j
], j
, out
);
2649 static void dump(struct isl_basic_map
*bmap
, FILE *out
, int indent
)
2653 dump_constraints(bmap
, bmap
->eq
, bmap
->n_eq
, "=", out
, indent
);
2654 dump_constraints(bmap
, bmap
->ineq
, bmap
->n_ineq
, ">=", out
, indent
);
2656 for (i
= 0; i
< bmap
->n_div
; ++i
) {
2657 fprintf(out
, "%*s", indent
, "");
2658 fprintf(out
, "e%d = [(", i
);
2659 dump_affine(bmap
, bmap
->div
[i
]+1, out
);
2661 isl_int_print(out
, bmap
->div
[i
][0], 0);
2662 fprintf(out
, "]\n");
2666 void isl_basic_set_print_internal(struct isl_basic_set
*bset
,
2667 FILE *out
, int indent
)
2670 fprintf(out
, "null basic set\n");
2674 fprintf(out
, "%*s", indent
, "");
2675 fprintf(out
, "ref: %d, nparam: %d, dim: %d, extra: %d, flags: %x\n",
2676 bset
->ref
, bset
->dim
->nparam
, bset
->dim
->n_out
,
2677 bset
->extra
, bset
->flags
);
2678 dump((struct isl_basic_map
*)bset
, out
, indent
);
2681 void isl_basic_map_print_internal(struct isl_basic_map
*bmap
,
2682 FILE *out
, int indent
)
2685 fprintf(out
, "null basic map\n");
2689 fprintf(out
, "%*s", indent
, "");
2690 fprintf(out
, "ref: %d, nparam: %d, in: %d, out: %d, extra: %d, "
2691 "flags: %x, n_name: %d\n",
2693 bmap
->dim
->nparam
, bmap
->dim
->n_in
, bmap
->dim
->n_out
,
2694 bmap
->extra
, bmap
->flags
, bmap
->dim
->n_id
);
2695 dump(bmap
, out
, indent
);
2698 int isl_inequality_negate(struct isl_basic_map
*bmap
, unsigned pos
)
2703 total
= isl_basic_map_total_dim(bmap
);
2704 isl_assert(bmap
->ctx
, pos
< bmap
->n_ineq
, return -1);
2705 isl_seq_neg(bmap
->ineq
[pos
], bmap
->ineq
[pos
], 1 + total
);
2706 isl_int_sub_ui(bmap
->ineq
[pos
][0], bmap
->ineq
[pos
][0], 1);
2707 ISL_F_CLR(bmap
, ISL_BASIC_MAP_NORMALIZED
);
2711 __isl_give isl_set
*isl_set_alloc_space(__isl_take isl_space
*space
, int n
,
2716 if (isl_space_dim(space
, isl_dim_in
) != 0)
2717 isl_die(isl_space_get_ctx(space
), isl_error_invalid
,
2718 "set cannot have input dimensions", goto error
);
2719 return isl_map_alloc_space(space
, n
, flags
);
2721 isl_space_free(space
);
2725 struct isl_set
*isl_set_alloc(struct isl_ctx
*ctx
,
2726 unsigned nparam
, unsigned dim
, int n
, unsigned flags
)
2728 struct isl_set
*set
;
2731 dims
= isl_space_alloc(ctx
, nparam
, 0, dim
);
2735 set
= isl_set_alloc_space(dims
, n
, flags
);
2739 /* Make sure "map" has room for at least "n" more basic maps.
2741 struct isl_map
*isl_map_grow(struct isl_map
*map
, int n
)
2744 struct isl_map
*grown
= NULL
;
2748 isl_assert(map
->ctx
, n
>= 0, goto error
);
2749 if (map
->n
+ n
<= map
->size
)
2751 grown
= isl_map_alloc_space(isl_map_get_space(map
), map
->n
+ n
, map
->flags
);
2754 for (i
= 0; i
< map
->n
; ++i
) {
2755 grown
->p
[i
] = isl_basic_map_copy(map
->p
[i
]);
2763 isl_map_free(grown
);
2768 /* Make sure "set" has room for at least "n" more basic sets.
2770 struct isl_set
*isl_set_grow(struct isl_set
*set
, int n
)
2772 return (struct isl_set
*)isl_map_grow((struct isl_map
*)set
, n
);
2775 struct isl_set
*isl_set_dup(struct isl_set
*set
)
2778 struct isl_set
*dup
;
2783 dup
= isl_set_alloc_space(isl_space_copy(set
->dim
), set
->n
, set
->flags
);
2786 for (i
= 0; i
< set
->n
; ++i
)
2787 dup
= isl_set_add_basic_set(dup
, isl_basic_set_copy(set
->p
[i
]));
2791 struct isl_set
*isl_set_from_basic_set(struct isl_basic_set
*bset
)
2793 return isl_map_from_basic_map(bset
);
2796 struct isl_map
*isl_map_from_basic_map(struct isl_basic_map
*bmap
)
2798 struct isl_map
*map
;
2803 map
= isl_map_alloc_space(isl_space_copy(bmap
->dim
), 1, ISL_MAP_DISJOINT
);
2804 return isl_map_add_basic_map(map
, bmap
);
2807 __isl_give isl_set
*isl_set_add_basic_set(__isl_take isl_set
*set
,
2808 __isl_take isl_basic_set
*bset
)
2810 return (struct isl_set
*)isl_map_add_basic_map((struct isl_map
*)set
,
2811 (struct isl_basic_map
*)bset
);
2814 __isl_null isl_set
*isl_set_free(__isl_take isl_set
*set
)
2816 return isl_map_free(set
);
2819 void isl_set_print_internal(struct isl_set
*set
, FILE *out
, int indent
)
2824 fprintf(out
, "null set\n");
2828 fprintf(out
, "%*s", indent
, "");
2829 fprintf(out
, "ref: %d, n: %d, nparam: %d, dim: %d, flags: %x\n",
2830 set
->ref
, set
->n
, set
->dim
->nparam
, set
->dim
->n_out
,
2832 for (i
= 0; i
< set
->n
; ++i
) {
2833 fprintf(out
, "%*s", indent
, "");
2834 fprintf(out
, "basic set %d:\n", i
);
2835 isl_basic_set_print_internal(set
->p
[i
], out
, indent
+4);
2839 void isl_map_print_internal(struct isl_map
*map
, FILE *out
, int indent
)
2844 fprintf(out
, "null map\n");
2848 fprintf(out
, "%*s", indent
, "");
2849 fprintf(out
, "ref: %d, n: %d, nparam: %d, in: %d, out: %d, "
2850 "flags: %x, n_name: %d\n",
2851 map
->ref
, map
->n
, map
->dim
->nparam
, map
->dim
->n_in
,
2852 map
->dim
->n_out
, map
->flags
, map
->dim
->n_id
);
2853 for (i
= 0; i
< map
->n
; ++i
) {
2854 fprintf(out
, "%*s", indent
, "");
2855 fprintf(out
, "basic map %d:\n", i
);
2856 isl_basic_map_print_internal(map
->p
[i
], out
, indent
+4);
2860 struct isl_basic_map
*isl_basic_map_intersect_domain(
2861 struct isl_basic_map
*bmap
, struct isl_basic_set
*bset
)
2863 struct isl_basic_map
*bmap_domain
;
2868 isl_assert(bset
->ctx
, isl_space_match(bmap
->dim
, isl_dim_param
,
2869 bset
->dim
, isl_dim_param
), goto error
);
2871 if (isl_space_dim(bset
->dim
, isl_dim_set
) != 0)
2872 isl_assert(bset
->ctx
,
2873 isl_basic_map_compatible_domain(bmap
, bset
), goto error
);
2875 bmap
= isl_basic_map_cow(bmap
);
2878 bmap
= isl_basic_map_extend_space(bmap
, isl_space_copy(bmap
->dim
),
2879 bset
->n_div
, bset
->n_eq
, bset
->n_ineq
);
2880 bmap_domain
= isl_basic_map_from_domain(bset
);
2881 bmap
= add_constraints(bmap
, bmap_domain
, 0, 0);
2883 bmap
= isl_basic_map_simplify(bmap
);
2884 return isl_basic_map_finalize(bmap
);
2886 isl_basic_map_free(bmap
);
2887 isl_basic_set_free(bset
);
2891 struct isl_basic_map
*isl_basic_map_intersect_range(
2892 struct isl_basic_map
*bmap
, struct isl_basic_set
*bset
)
2894 struct isl_basic_map
*bmap_range
;
2899 isl_assert(bset
->ctx
, isl_space_match(bmap
->dim
, isl_dim_param
,
2900 bset
->dim
, isl_dim_param
), goto error
);
2902 if (isl_space_dim(bset
->dim
, isl_dim_set
) != 0)
2903 isl_assert(bset
->ctx
,
2904 isl_basic_map_compatible_range(bmap
, bset
), goto error
);
2906 if (isl_basic_set_plain_is_universe(bset
)) {
2907 isl_basic_set_free(bset
);
2911 bmap
= isl_basic_map_cow(bmap
);
2914 bmap
= isl_basic_map_extend_space(bmap
, isl_space_copy(bmap
->dim
),
2915 bset
->n_div
, bset
->n_eq
, bset
->n_ineq
);
2916 bmap_range
= isl_basic_map_from_basic_set(bset
, isl_space_copy(bset
->dim
));
2917 bmap
= add_constraints(bmap
, bmap_range
, 0, 0);
2919 bmap
= isl_basic_map_simplify(bmap
);
2920 return isl_basic_map_finalize(bmap
);
2922 isl_basic_map_free(bmap
);
2923 isl_basic_set_free(bset
);
2927 isl_bool
isl_basic_map_contains(__isl_keep isl_basic_map
*bmap
,
2928 __isl_keep isl_vec
*vec
)
2935 return isl_bool_error
;
2937 total
= 1 + isl_basic_map_total_dim(bmap
);
2938 if (total
!= vec
->size
)
2939 return isl_bool_error
;
2943 for (i
= 0; i
< bmap
->n_eq
; ++i
) {
2944 isl_seq_inner_product(vec
->el
, bmap
->eq
[i
], total
, &s
);
2945 if (!isl_int_is_zero(s
)) {
2947 return isl_bool_false
;
2951 for (i
= 0; i
< bmap
->n_ineq
; ++i
) {
2952 isl_seq_inner_product(vec
->el
, bmap
->ineq
[i
], total
, &s
);
2953 if (isl_int_is_neg(s
)) {
2955 return isl_bool_false
;
2961 return isl_bool_true
;
2964 isl_bool
isl_basic_set_contains(__isl_keep isl_basic_set
*bset
,
2965 __isl_keep isl_vec
*vec
)
2967 return isl_basic_map_contains((struct isl_basic_map
*)bset
, vec
);
2970 struct isl_basic_map
*isl_basic_map_intersect(
2971 struct isl_basic_map
*bmap1
, struct isl_basic_map
*bmap2
)
2973 struct isl_vec
*sample
= NULL
;
2975 if (!bmap1
|| !bmap2
)
2978 isl_assert(bmap1
->ctx
, isl_space_match(bmap1
->dim
, isl_dim_param
,
2979 bmap2
->dim
, isl_dim_param
), goto error
);
2980 if (isl_space_dim(bmap1
->dim
, isl_dim_all
) ==
2981 isl_space_dim(bmap1
->dim
, isl_dim_param
) &&
2982 isl_space_dim(bmap2
->dim
, isl_dim_all
) !=
2983 isl_space_dim(bmap2
->dim
, isl_dim_param
))
2984 return isl_basic_map_intersect(bmap2
, bmap1
);
2986 if (isl_space_dim(bmap2
->dim
, isl_dim_all
) !=
2987 isl_space_dim(bmap2
->dim
, isl_dim_param
))
2988 isl_assert(bmap1
->ctx
,
2989 isl_space_is_equal(bmap1
->dim
, bmap2
->dim
), goto error
);
2991 if (isl_basic_map_plain_is_empty(bmap1
)) {
2992 isl_basic_map_free(bmap2
);
2995 if (isl_basic_map_plain_is_empty(bmap2
)) {
2996 isl_basic_map_free(bmap1
);
3000 if (bmap1
->sample
&&
3001 isl_basic_map_contains(bmap1
, bmap1
->sample
) > 0 &&
3002 isl_basic_map_contains(bmap2
, bmap1
->sample
) > 0)
3003 sample
= isl_vec_copy(bmap1
->sample
);
3004 else if (bmap2
->sample
&&
3005 isl_basic_map_contains(bmap1
, bmap2
->sample
) > 0 &&
3006 isl_basic_map_contains(bmap2
, bmap2
->sample
) > 0)
3007 sample
= isl_vec_copy(bmap2
->sample
);
3009 bmap1
= isl_basic_map_cow(bmap1
);
3012 bmap1
= isl_basic_map_extend_space(bmap1
, isl_space_copy(bmap1
->dim
),
3013 bmap2
->n_div
, bmap2
->n_eq
, bmap2
->n_ineq
);
3014 bmap1
= add_constraints(bmap1
, bmap2
, 0, 0);
3017 isl_vec_free(sample
);
3019 isl_vec_free(bmap1
->sample
);
3020 bmap1
->sample
= sample
;
3023 bmap1
= isl_basic_map_simplify(bmap1
);
3024 return isl_basic_map_finalize(bmap1
);
3027 isl_vec_free(sample
);
3028 isl_basic_map_free(bmap1
);
3029 isl_basic_map_free(bmap2
);
3033 struct isl_basic_set
*isl_basic_set_intersect(
3034 struct isl_basic_set
*bset1
, struct isl_basic_set
*bset2
)
3036 return (struct isl_basic_set
*)
3037 isl_basic_map_intersect(
3038 (struct isl_basic_map
*)bset1
,
3039 (struct isl_basic_map
*)bset2
);
3042 __isl_give isl_basic_set
*isl_basic_set_intersect_params(
3043 __isl_take isl_basic_set
*bset1
, __isl_take isl_basic_set
*bset2
)
3045 return isl_basic_set_intersect(bset1
, bset2
);
3048 /* Special case of isl_map_intersect, where both map1 and map2
3049 * are convex, without any divs and such that either map1 or map2
3050 * contains a single constraint. This constraint is then simply
3051 * added to the other map.
3053 static __isl_give isl_map
*map_intersect_add_constraint(
3054 __isl_take isl_map
*map1
, __isl_take isl_map
*map2
)
3056 isl_assert(map1
->ctx
, map1
->n
== 1, goto error
);
3057 isl_assert(map2
->ctx
, map1
->n
== 1, goto error
);
3058 isl_assert(map1
->ctx
, map1
->p
[0]->n_div
== 0, goto error
);
3059 isl_assert(map2
->ctx
, map1
->p
[0]->n_div
== 0, goto error
);
3061 if (map2
->p
[0]->n_eq
+ map2
->p
[0]->n_ineq
!= 1)
3062 return isl_map_intersect(map2
, map1
);
3064 isl_assert(map2
->ctx
,
3065 map2
->p
[0]->n_eq
+ map2
->p
[0]->n_ineq
== 1, goto error
);
3067 map1
= isl_map_cow(map1
);
3070 if (isl_map_plain_is_empty(map1
)) {
3074 map1
->p
[0] = isl_basic_map_cow(map1
->p
[0]);
3075 if (map2
->p
[0]->n_eq
== 1)
3076 map1
->p
[0] = isl_basic_map_add_eq(map1
->p
[0], map2
->p
[0]->eq
[0]);
3078 map1
->p
[0] = isl_basic_map_add_ineq(map1
->p
[0],
3079 map2
->p
[0]->ineq
[0]);
3081 map1
->p
[0] = isl_basic_map_simplify(map1
->p
[0]);
3082 map1
->p
[0] = isl_basic_map_finalize(map1
->p
[0]);
3086 if (isl_basic_map_plain_is_empty(map1
->p
[0])) {
3087 isl_basic_map_free(map1
->p
[0]);
3100 /* map2 may be either a parameter domain or a map living in the same
3103 static __isl_give isl_map
*map_intersect_internal(__isl_take isl_map
*map1
,
3104 __isl_take isl_map
*map2
)
3113 if ((isl_map_plain_is_empty(map1
) ||
3114 isl_map_plain_is_universe(map2
)) &&
3115 isl_space_is_equal(map1
->dim
, map2
->dim
)) {
3119 if ((isl_map_plain_is_empty(map2
) ||
3120 isl_map_plain_is_universe(map1
)) &&
3121 isl_space_is_equal(map1
->dim
, map2
->dim
)) {
3126 if (map1
->n
== 1 && map2
->n
== 1 &&
3127 map1
->p
[0]->n_div
== 0 && map2
->p
[0]->n_div
== 0 &&
3128 isl_space_is_equal(map1
->dim
, map2
->dim
) &&
3129 (map1
->p
[0]->n_eq
+ map1
->p
[0]->n_ineq
== 1 ||
3130 map2
->p
[0]->n_eq
+ map2
->p
[0]->n_ineq
== 1))
3131 return map_intersect_add_constraint(map1
, map2
);
3133 if (isl_space_dim(map2
->dim
, isl_dim_all
) !=
3134 isl_space_dim(map2
->dim
, isl_dim_param
))
3135 isl_assert(map1
->ctx
,
3136 isl_space_is_equal(map1
->dim
, map2
->dim
), goto error
);
3138 if (ISL_F_ISSET(map1
, ISL_MAP_DISJOINT
) &&
3139 ISL_F_ISSET(map2
, ISL_MAP_DISJOINT
))
3140 ISL_FL_SET(flags
, ISL_MAP_DISJOINT
);
3142 result
= isl_map_alloc_space(isl_space_copy(map1
->dim
),
3143 map1
->n
* map2
->n
, flags
);
3146 for (i
= 0; i
< map1
->n
; ++i
)
3147 for (j
= 0; j
< map2
->n
; ++j
) {
3148 struct isl_basic_map
*part
;
3149 part
= isl_basic_map_intersect(
3150 isl_basic_map_copy(map1
->p
[i
]),
3151 isl_basic_map_copy(map2
->p
[j
]));
3152 if (isl_basic_map_is_empty(part
) < 0)
3153 part
= isl_basic_map_free(part
);
3154 result
= isl_map_add_basic_map(result
, part
);
3167 static __isl_give isl_map
*map_intersect(__isl_take isl_map
*map1
,
3168 __isl_take isl_map
*map2
)
3172 if (!isl_space_is_equal(map1
->dim
, map2
->dim
))
3173 isl_die(isl_map_get_ctx(map1
), isl_error_invalid
,
3174 "spaces don't match", goto error
);
3175 return map_intersect_internal(map1
, map2
);
3182 __isl_give isl_map
*isl_map_intersect(__isl_take isl_map
*map1
,
3183 __isl_take isl_map
*map2
)
3185 return isl_map_align_params_map_map_and(map1
, map2
, &map_intersect
);
3188 struct isl_set
*isl_set_intersect(struct isl_set
*set1
, struct isl_set
*set2
)
3190 return (struct isl_set
*)
3191 isl_map_intersect((struct isl_map
*)set1
,
3192 (struct isl_map
*)set2
);
3195 /* map_intersect_internal accepts intersections
3196 * with parameter domains, so we can just call that function.
3198 static __isl_give isl_map
*map_intersect_params(__isl_take isl_map
*map
,
3199 __isl_take isl_set
*params
)
3201 return map_intersect_internal(map
, params
);
3204 __isl_give isl_map
*isl_map_intersect_params(__isl_take isl_map
*map1
,
3205 __isl_take isl_map
*map2
)
3207 return isl_map_align_params_map_map_and(map1
, map2
, &map_intersect_params
);
3210 __isl_give isl_set
*isl_set_intersect_params(__isl_take isl_set
*set
,
3211 __isl_take isl_set
*params
)
3213 return isl_map_intersect_params(set
, params
);
3216 struct isl_basic_map
*isl_basic_map_reverse(struct isl_basic_map
*bmap
)
3219 unsigned pos
, n1
, n2
;
3223 bmap
= isl_basic_map_cow(bmap
);
3226 space
= isl_space_reverse(isl_space_copy(bmap
->dim
));
3227 pos
= isl_basic_map_offset(bmap
, isl_dim_in
);
3228 n1
= isl_basic_map_dim(bmap
, isl_dim_in
);
3229 n2
= isl_basic_map_dim(bmap
, isl_dim_out
);
3230 bmap
= isl_basic_map_swap_vars(bmap
, pos
, n1
, n2
);
3231 return isl_basic_map_reset_space(bmap
, space
);
3234 static __isl_give isl_basic_map
*basic_map_space_reset(
3235 __isl_take isl_basic_map
*bmap
, enum isl_dim_type type
)
3241 if (!isl_space_is_named_or_nested(bmap
->dim
, type
))
3244 space
= isl_basic_map_get_space(bmap
);
3245 space
= isl_space_reset(space
, type
);
3246 bmap
= isl_basic_map_reset_space(bmap
, space
);
3250 __isl_give isl_basic_map
*isl_basic_map_insert_dims(
3251 __isl_take isl_basic_map
*bmap
, enum isl_dim_type type
,
3252 unsigned pos
, unsigned n
)
3255 struct isl_basic_map
*res
;
3256 struct isl_dim_map
*dim_map
;
3257 unsigned total
, off
;
3258 enum isl_dim_type t
;
3261 return basic_map_space_reset(bmap
, type
);
3266 res_dim
= isl_space_insert_dims(isl_basic_map_get_space(bmap
), type
, pos
, n
);
3268 total
= isl_basic_map_total_dim(bmap
) + n
;
3269 dim_map
= isl_dim_map_alloc(bmap
->ctx
, total
);
3271 for (t
= isl_dim_param
; t
<= isl_dim_out
; ++t
) {
3273 isl_dim_map_dim(dim_map
, bmap
->dim
, t
, off
);
3275 unsigned size
= isl_basic_map_dim(bmap
, t
);
3276 isl_dim_map_dim_range(dim_map
, bmap
->dim
, t
,
3278 isl_dim_map_dim_range(dim_map
, bmap
->dim
, t
,
3279 pos
, size
- pos
, off
+ pos
+ n
);
3281 off
+= isl_space_dim(res_dim
, t
);
3283 isl_dim_map_div(dim_map
, bmap
, off
);
3285 res
= isl_basic_map_alloc_space(res_dim
,
3286 bmap
->n_div
, bmap
->n_eq
, bmap
->n_ineq
);
3287 if (isl_basic_map_is_rational(bmap
))
3288 res
= isl_basic_map_set_rational(res
);
3289 if (isl_basic_map_plain_is_empty(bmap
)) {
3290 isl_basic_map_free(bmap
);
3292 return isl_basic_map_set_to_empty(res
);
3294 res
= isl_basic_map_add_constraints_dim_map(res
, bmap
, dim_map
);
3295 return isl_basic_map_finalize(res
);
3298 __isl_give isl_basic_set
*isl_basic_set_insert_dims(
3299 __isl_take isl_basic_set
*bset
,
3300 enum isl_dim_type type
, unsigned pos
, unsigned n
)
3302 return isl_basic_map_insert_dims(bset
, type
, pos
, n
);
3305 __isl_give isl_basic_map
*isl_basic_map_add_dims(__isl_take isl_basic_map
*bmap
,
3306 enum isl_dim_type type
, unsigned n
)
3310 return isl_basic_map_insert_dims(bmap
, type
,
3311 isl_basic_map_dim(bmap
, type
), n
);
3314 __isl_give isl_basic_set
*isl_basic_set_add_dims(__isl_take isl_basic_set
*bset
,
3315 enum isl_dim_type type
, unsigned n
)
3319 isl_assert(bset
->ctx
, type
!= isl_dim_in
, goto error
);
3320 return isl_basic_map_add_dims(bset
, type
, n
);
3322 isl_basic_set_free(bset
);
3326 static __isl_give isl_map
*map_space_reset(__isl_take isl_map
*map
,
3327 enum isl_dim_type type
)
3331 if (!map
|| !isl_space_is_named_or_nested(map
->dim
, type
))
3334 space
= isl_map_get_space(map
);
3335 space
= isl_space_reset(space
, type
);
3336 map
= isl_map_reset_space(map
, space
);
3340 __isl_give isl_map
*isl_map_insert_dims(__isl_take isl_map
*map
,
3341 enum isl_dim_type type
, unsigned pos
, unsigned n
)
3346 return map_space_reset(map
, type
);
3348 map
= isl_map_cow(map
);
3352 map
->dim
= isl_space_insert_dims(map
->dim
, type
, pos
, n
);
3356 for (i
= 0; i
< map
->n
; ++i
) {
3357 map
->p
[i
] = isl_basic_map_insert_dims(map
->p
[i
], type
, pos
, n
);
3368 __isl_give isl_set
*isl_set_insert_dims(__isl_take isl_set
*set
,
3369 enum isl_dim_type type
, unsigned pos
, unsigned n
)
3371 return isl_map_insert_dims(set
, type
, pos
, n
);
3374 __isl_give isl_map
*isl_map_add_dims(__isl_take isl_map
*map
,
3375 enum isl_dim_type type
, unsigned n
)
3379 return isl_map_insert_dims(map
, type
, isl_map_dim(map
, type
), n
);
3382 __isl_give isl_set
*isl_set_add_dims(__isl_take isl_set
*set
,
3383 enum isl_dim_type type
, unsigned n
)
3387 isl_assert(set
->ctx
, type
!= isl_dim_in
, goto error
);
3388 return (isl_set
*)isl_map_add_dims((isl_map
*)set
, type
, n
);
3394 __isl_give isl_basic_map
*isl_basic_map_move_dims(
3395 __isl_take isl_basic_map
*bmap
,
3396 enum isl_dim_type dst_type
, unsigned dst_pos
,
3397 enum isl_dim_type src_type
, unsigned src_pos
, unsigned n
)
3399 struct isl_dim_map
*dim_map
;
3400 struct isl_basic_map
*res
;
3401 enum isl_dim_type t
;
3402 unsigned total
, off
;
3409 isl_assert(bmap
->ctx
, src_pos
+ n
<= isl_basic_map_dim(bmap
, src_type
),
3412 if (dst_type
== src_type
&& dst_pos
== src_pos
)
3415 isl_assert(bmap
->ctx
, dst_type
!= src_type
, goto error
);
3417 if (pos(bmap
->dim
, dst_type
) + dst_pos
==
3418 pos(bmap
->dim
, src_type
) + src_pos
+
3419 ((src_type
< dst_type
) ? n
: 0)) {
3420 bmap
= isl_basic_map_cow(bmap
);
3424 bmap
->dim
= isl_space_move_dims(bmap
->dim
, dst_type
, dst_pos
,
3425 src_type
, src_pos
, n
);
3429 bmap
= isl_basic_map_finalize(bmap
);
3434 total
= isl_basic_map_total_dim(bmap
);
3435 dim_map
= isl_dim_map_alloc(bmap
->ctx
, total
);
3438 for (t
= isl_dim_param
; t
<= isl_dim_out
; ++t
) {
3439 unsigned size
= isl_space_dim(bmap
->dim
, t
);
3440 if (t
== dst_type
) {
3441 isl_dim_map_dim_range(dim_map
, bmap
->dim
, t
,
3444 isl_dim_map_dim_range(dim_map
, bmap
->dim
, src_type
,
3447 isl_dim_map_dim_range(dim_map
, bmap
->dim
, t
,
3448 dst_pos
, size
- dst_pos
, off
);
3449 off
+= size
- dst_pos
;
3450 } else if (t
== src_type
) {
3451 isl_dim_map_dim_range(dim_map
, bmap
->dim
, t
,
3454 isl_dim_map_dim_range(dim_map
, bmap
->dim
, t
,
3455 src_pos
+ n
, size
- src_pos
- n
, off
);
3456 off
+= size
- src_pos
- n
;
3458 isl_dim_map_dim(dim_map
, bmap
->dim
, t
, off
);
3462 isl_dim_map_div(dim_map
, bmap
, off
);
3464 res
= isl_basic_map_alloc_space(isl_basic_map_get_space(bmap
),
3465 bmap
->n_div
, bmap
->n_eq
, bmap
->n_ineq
);
3466 bmap
= isl_basic_map_add_constraints_dim_map(res
, bmap
, dim_map
);
3470 bmap
->dim
= isl_space_move_dims(bmap
->dim
, dst_type
, dst_pos
,
3471 src_type
, src_pos
, n
);
3475 ISL_F_CLR(bmap
, ISL_BASIC_MAP_NORMALIZED
);
3476 bmap
= isl_basic_map_gauss(bmap
, NULL
);
3477 bmap
= isl_basic_map_finalize(bmap
);
3481 isl_basic_map_free(bmap
);
3485 __isl_give isl_basic_set
*isl_basic_set_move_dims(__isl_take isl_basic_set
*bset
,
3486 enum isl_dim_type dst_type
, unsigned dst_pos
,
3487 enum isl_dim_type src_type
, unsigned src_pos
, unsigned n
)
3489 return (isl_basic_set
*)isl_basic_map_move_dims(
3490 (isl_basic_map
*)bset
, dst_type
, dst_pos
, src_type
, src_pos
, n
);
3493 __isl_give isl_set
*isl_set_move_dims(__isl_take isl_set
*set
,
3494 enum isl_dim_type dst_type
, unsigned dst_pos
,
3495 enum isl_dim_type src_type
, unsigned src_pos
, unsigned n
)
3499 isl_assert(set
->ctx
, dst_type
!= isl_dim_in
, goto error
);
3500 return (isl_set
*)isl_map_move_dims((isl_map
*)set
, dst_type
, dst_pos
,
3501 src_type
, src_pos
, n
);
3507 __isl_give isl_map
*isl_map_move_dims(__isl_take isl_map
*map
,
3508 enum isl_dim_type dst_type
, unsigned dst_pos
,
3509 enum isl_dim_type src_type
, unsigned src_pos
, unsigned n
)
3518 isl_assert(map
->ctx
, src_pos
+ n
<= isl_map_dim(map
, src_type
),
3521 if (dst_type
== src_type
&& dst_pos
== src_pos
)
3524 isl_assert(map
->ctx
, dst_type
!= src_type
, goto error
);
3526 map
= isl_map_cow(map
);
3530 map
->dim
= isl_space_move_dims(map
->dim
, dst_type
, dst_pos
, src_type
, src_pos
, n
);
3534 for (i
= 0; i
< map
->n
; ++i
) {
3535 map
->p
[i
] = isl_basic_map_move_dims(map
->p
[i
],
3537 src_type
, src_pos
, n
);
3548 /* Move the specified dimensions to the last columns right before
3549 * the divs. Don't change the dimension specification of bmap.
3550 * That's the responsibility of the caller.
3552 static __isl_give isl_basic_map
*move_last(__isl_take isl_basic_map
*bmap
,
3553 enum isl_dim_type type
, unsigned first
, unsigned n
)
3555 struct isl_dim_map
*dim_map
;
3556 struct isl_basic_map
*res
;
3557 enum isl_dim_type t
;
3558 unsigned total
, off
;
3562 if (pos(bmap
->dim
, type
) + first
+ n
==
3563 1 + isl_space_dim(bmap
->dim
, isl_dim_all
))
3566 total
= isl_basic_map_total_dim(bmap
);
3567 dim_map
= isl_dim_map_alloc(bmap
->ctx
, total
);
3570 for (t
= isl_dim_param
; t
<= isl_dim_out
; ++t
) {
3571 unsigned size
= isl_space_dim(bmap
->dim
, t
);
3573 isl_dim_map_dim_range(dim_map
, bmap
->dim
, t
,
3576 isl_dim_map_dim_range(dim_map
, bmap
->dim
, t
,
3577 first
, n
, total
- bmap
->n_div
- n
);
3578 isl_dim_map_dim_range(dim_map
, bmap
->dim
, t
,
3579 first
+ n
, size
- (first
+ n
), off
);
3580 off
+= size
- (first
+ n
);
3582 isl_dim_map_dim(dim_map
, bmap
->dim
, t
, off
);
3586 isl_dim_map_div(dim_map
, bmap
, off
+ n
);
3588 res
= isl_basic_map_alloc_space(isl_basic_map_get_space(bmap
),
3589 bmap
->n_div
, bmap
->n_eq
, bmap
->n_ineq
);
3590 res
= isl_basic_map_add_constraints_dim_map(res
, bmap
, dim_map
);
3594 /* Insert "n" rows in the divs of "bmap".
3596 * The number of columns is not changed, which means that the last
3597 * dimensions of "bmap" are being reintepreted as the new divs.
3598 * The space of "bmap" is not adjusted, however, which means
3599 * that "bmap" is left in an inconsistent state. Removing "n" dimensions
3600 * from the space of "bmap" is the responsibility of the caller.
3602 static __isl_give isl_basic_map
*insert_div_rows(__isl_take isl_basic_map
*bmap
,
3610 bmap
= isl_basic_map_cow(bmap
);
3614 row_size
= 1 + isl_space_dim(bmap
->dim
, isl_dim_all
) + bmap
->extra
;
3615 old
= bmap
->block2
.data
;
3616 bmap
->block2
= isl_blk_extend(bmap
->ctx
, bmap
->block2
,
3617 (bmap
->extra
+ n
) * (1 + row_size
));
3618 if (!bmap
->block2
.data
)
3619 return isl_basic_map_free(bmap
);
3620 new_div
= isl_alloc_array(bmap
->ctx
, isl_int
*, bmap
->extra
+ n
);
3622 return isl_basic_map_free(bmap
);
3623 for (i
= 0; i
< n
; ++i
) {
3624 new_div
[i
] = bmap
->block2
.data
+
3625 (bmap
->extra
+ i
) * (1 + row_size
);
3626 isl_seq_clr(new_div
[i
], 1 + row_size
);
3628 for (i
= 0; i
< bmap
->extra
; ++i
)
3629 new_div
[n
+ i
] = bmap
->block2
.data
+ (bmap
->div
[i
] - old
);
3631 bmap
->div
= new_div
;
3638 /* Drop constraints from "bmap" that only involve the variables
3639 * of "type" in the range [first, first + n] that are not related
3640 * to any of the variables outside that interval.
3641 * These constraints cannot influence the values for the variables
3642 * outside the interval, except in case they cause "bmap" to be empty.
3643 * Only drop the constraints if "bmap" is known to be non-empty.
3645 static __isl_give isl_basic_map
*drop_irrelevant_constraints(
3646 __isl_take isl_basic_map
*bmap
, enum isl_dim_type type
,
3647 unsigned first
, unsigned n
)
3651 unsigned dim
, n_div
;
3654 non_empty
= isl_basic_map_plain_is_non_empty(bmap
);
3656 return isl_basic_map_free(bmap
);
3660 dim
= isl_basic_map_dim(bmap
, isl_dim_all
);
3661 n_div
= isl_basic_map_dim(bmap
, isl_dim_div
);
3662 groups
= isl_calloc_array(isl_basic_map_get_ctx(bmap
), int, dim
);
3664 return isl_basic_map_free(bmap
);
3665 first
+= isl_basic_map_offset(bmap
, type
) - 1;
3666 for (i
= 0; i
< first
; ++i
)
3668 for (i
= first
+ n
; i
< dim
- n_div
; ++i
)
3671 bmap
= isl_basic_map_drop_unrelated_constraints(bmap
, groups
);
3676 /* Turn the n dimensions of type type, starting at first
3677 * into existentially quantified variables.
3679 * If a subset of the projected out variables are unrelated
3680 * to any of the variables that remain, then the constraints
3681 * involving this subset are simply dropped first.
3683 __isl_give isl_basic_map
*isl_basic_map_project_out(
3684 __isl_take isl_basic_map
*bmap
,
3685 enum isl_dim_type type
, unsigned first
, unsigned n
)
3688 return basic_map_space_reset(bmap
, type
);
3689 if (type
== isl_dim_div
)
3690 isl_die(isl_basic_map_get_ctx(bmap
), isl_error_invalid
,
3691 "cannot project out existentially quantified variables",
3692 return isl_basic_map_free(bmap
));
3694 bmap
= drop_irrelevant_constraints(bmap
, type
, first
, n
);
3698 if (ISL_F_ISSET(bmap
, ISL_BASIC_MAP_RATIONAL
))
3699 return isl_basic_map_remove_dims(bmap
, type
, first
, n
);
3701 isl_assert(bmap
->ctx
, first
+ n
<= isl_basic_map_dim(bmap
, type
),
3704 bmap
= move_last(bmap
, type
, first
, n
);
3705 bmap
= isl_basic_map_cow(bmap
);
3706 bmap
= insert_div_rows(bmap
, n
);
3710 bmap
->dim
= isl_space_drop_dims(bmap
->dim
, type
, first
, n
);
3713 bmap
= isl_basic_map_simplify(bmap
);
3714 bmap
= isl_basic_map_drop_redundant_divs(bmap
);
3715 return isl_basic_map_finalize(bmap
);
3717 isl_basic_map_free(bmap
);
3721 /* Turn the n dimensions of type type, starting at first
3722 * into existentially quantified variables.
3724 struct isl_basic_set
*isl_basic_set_project_out(struct isl_basic_set
*bset
,
3725 enum isl_dim_type type
, unsigned first
, unsigned n
)
3727 return (isl_basic_set
*)isl_basic_map_project_out(
3728 (isl_basic_map
*)bset
, type
, first
, n
);
3731 /* Turn the n dimensions of type type, starting at first
3732 * into existentially quantified variables.
3734 __isl_give isl_map
*isl_map_project_out(__isl_take isl_map
*map
,
3735 enum isl_dim_type type
, unsigned first
, unsigned n
)
3743 return map_space_reset(map
, type
);
3745 isl_assert(map
->ctx
, first
+ n
<= isl_map_dim(map
, type
), goto error
);
3747 map
= isl_map_cow(map
);
3751 map
->dim
= isl_space_drop_dims(map
->dim
, type
, first
, n
);
3755 for (i
= 0; i
< map
->n
; ++i
) {
3756 map
->p
[i
] = isl_basic_map_project_out(map
->p
[i
], type
, first
, n
);
3767 /* Turn the n dimensions of type type, starting at first
3768 * into existentially quantified variables.
3770 __isl_give isl_set
*isl_set_project_out(__isl_take isl_set
*set
,
3771 enum isl_dim_type type
, unsigned first
, unsigned n
)
3773 return (isl_set
*)isl_map_project_out((isl_map
*)set
, type
, first
, n
);
3776 /* Return a map that projects the elements in "set" onto their
3777 * "n" set dimensions starting at "first".
3778 * "type" should be equal to isl_dim_set.
3780 __isl_give isl_map
*isl_set_project_onto_map(__isl_take isl_set
*set
,
3781 enum isl_dim_type type
, unsigned first
, unsigned n
)
3789 if (type
!= isl_dim_set
)
3790 isl_die(isl_set_get_ctx(set
), isl_error_invalid
,
3791 "only set dimensions can be projected out", goto error
);
3792 dim
= isl_set_dim(set
, isl_dim_set
);
3793 if (first
+ n
> dim
|| first
+ n
< first
)
3794 isl_die(isl_set_get_ctx(set
), isl_error_invalid
,
3795 "index out of bounds", goto error
);
3797 map
= isl_map_from_domain(set
);
3798 map
= isl_map_add_dims(map
, isl_dim_out
, n
);
3799 for (i
= 0; i
< n
; ++i
)
3800 map
= isl_map_equate(map
, isl_dim_in
, first
+ i
,
3808 static struct isl_basic_map
*add_divs(struct isl_basic_map
*bmap
, unsigned n
)
3812 for (i
= 0; i
< n
; ++i
) {
3813 j
= isl_basic_map_alloc_div(bmap
);
3816 isl_seq_clr(bmap
->div
[j
], 1+1+isl_basic_map_total_dim(bmap
));
3820 isl_basic_map_free(bmap
);
3824 struct isl_basic_map
*isl_basic_map_apply_range(
3825 struct isl_basic_map
*bmap1
, struct isl_basic_map
*bmap2
)
3827 isl_space
*dim_result
= NULL
;
3828 struct isl_basic_map
*bmap
;
3829 unsigned n_in
, n_out
, n
, nparam
, total
, pos
;
3830 struct isl_dim_map
*dim_map1
, *dim_map2
;
3832 if (!bmap1
|| !bmap2
)
3834 if (!isl_space_match(bmap1
->dim
, isl_dim_param
,
3835 bmap2
->dim
, isl_dim_param
))
3836 isl_die(isl_basic_map_get_ctx(bmap1
), isl_error_invalid
,
3837 "parameters don't match", goto error
);
3838 if (!isl_space_tuple_is_equal(bmap1
->dim
, isl_dim_out
,
3839 bmap2
->dim
, isl_dim_in
))
3840 isl_die(isl_basic_map_get_ctx(bmap1
), isl_error_invalid
,
3841 "spaces don't match", goto error
);
3843 dim_result
= isl_space_join(isl_space_copy(bmap1
->dim
),
3844 isl_space_copy(bmap2
->dim
));
3846 n_in
= isl_basic_map_n_in(bmap1
);
3847 n_out
= isl_basic_map_n_out(bmap2
);
3848 n
= isl_basic_map_n_out(bmap1
);
3849 nparam
= isl_basic_map_n_param(bmap1
);
3851 total
= nparam
+ n_in
+ n_out
+ bmap1
->n_div
+ bmap2
->n_div
+ n
;
3852 dim_map1
= isl_dim_map_alloc(bmap1
->ctx
, total
);
3853 dim_map2
= isl_dim_map_alloc(bmap1
->ctx
, total
);
3854 isl_dim_map_dim(dim_map1
, bmap1
->dim
, isl_dim_param
, pos
= 0);
3855 isl_dim_map_dim(dim_map2
, bmap2
->dim
, isl_dim_param
, pos
= 0);
3856 isl_dim_map_dim(dim_map1
, bmap1
->dim
, isl_dim_in
, pos
+= nparam
);
3857 isl_dim_map_dim(dim_map2
, bmap2
->dim
, isl_dim_out
, pos
+= n_in
);
3858 isl_dim_map_div(dim_map1
, bmap1
, pos
+= n_out
);
3859 isl_dim_map_div(dim_map2
, bmap2
, pos
+= bmap1
->n_div
);
3860 isl_dim_map_dim(dim_map1
, bmap1
->dim
, isl_dim_out
, pos
+= bmap2
->n_div
);
3861 isl_dim_map_dim(dim_map2
, bmap2
->dim
, isl_dim_in
, pos
);
3863 bmap
= isl_basic_map_alloc_space(dim_result
,
3864 bmap1
->n_div
+ bmap2
->n_div
+ n
,
3865 bmap1
->n_eq
+ bmap2
->n_eq
,
3866 bmap1
->n_ineq
+ bmap2
->n_ineq
);
3867 bmap
= isl_basic_map_add_constraints_dim_map(bmap
, bmap1
, dim_map1
);
3868 bmap
= isl_basic_map_add_constraints_dim_map(bmap
, bmap2
, dim_map2
);
3869 bmap
= add_divs(bmap
, n
);
3870 bmap
= isl_basic_map_simplify(bmap
);
3871 bmap
= isl_basic_map_drop_redundant_divs(bmap
);
3872 return isl_basic_map_finalize(bmap
);
3874 isl_basic_map_free(bmap1
);
3875 isl_basic_map_free(bmap2
);
3879 struct isl_basic_set
*isl_basic_set_apply(
3880 struct isl_basic_set
*bset
, struct isl_basic_map
*bmap
)
3885 isl_assert(bset
->ctx
, isl_basic_map_compatible_domain(bmap
, bset
),
3888 return (struct isl_basic_set
*)
3889 isl_basic_map_apply_range((struct isl_basic_map
*)bset
, bmap
);
3891 isl_basic_set_free(bset
);
3892 isl_basic_map_free(bmap
);
3896 struct isl_basic_map
*isl_basic_map_apply_domain(
3897 struct isl_basic_map
*bmap1
, struct isl_basic_map
*bmap2
)
3899 if (!bmap1
|| !bmap2
)
3902 isl_assert(bmap1
->ctx
,
3903 isl_basic_map_n_in(bmap1
) == isl_basic_map_n_in(bmap2
), goto error
);
3904 isl_assert(bmap1
->ctx
,
3905 isl_basic_map_n_param(bmap1
) == isl_basic_map_n_param(bmap2
),
3908 bmap1
= isl_basic_map_reverse(bmap1
);
3909 bmap1
= isl_basic_map_apply_range(bmap1
, bmap2
);
3910 return isl_basic_map_reverse(bmap1
);
3912 isl_basic_map_free(bmap1
);
3913 isl_basic_map_free(bmap2
);
3917 /* Given two basic maps A -> f(A) and B -> g(B), construct a basic map
3918 * A \cap B -> f(A) + f(B)
3920 struct isl_basic_map
*isl_basic_map_sum(
3921 struct isl_basic_map
*bmap1
, struct isl_basic_map
*bmap2
)
3923 unsigned n_in
, n_out
, nparam
, total
, pos
;
3924 struct isl_basic_map
*bmap
= NULL
;
3925 struct isl_dim_map
*dim_map1
, *dim_map2
;
3928 if (!bmap1
|| !bmap2
)
3931 isl_assert(bmap1
->ctx
, isl_space_is_equal(bmap1
->dim
, bmap2
->dim
),
3934 nparam
= isl_basic_map_n_param(bmap1
);
3935 n_in
= isl_basic_map_n_in(bmap1
);
3936 n_out
= isl_basic_map_n_out(bmap1
);
3938 total
= nparam
+ n_in
+ n_out
+ bmap1
->n_div
+ bmap2
->n_div
+ 2 * n_out
;
3939 dim_map1
= isl_dim_map_alloc(bmap1
->ctx
, total
);
3940 dim_map2
= isl_dim_map_alloc(bmap2
->ctx
, total
);
3941 isl_dim_map_dim(dim_map1
, bmap1
->dim
, isl_dim_param
, pos
= 0);
3942 isl_dim_map_dim(dim_map2
, bmap2
->dim
, isl_dim_param
, pos
);
3943 isl_dim_map_dim(dim_map1
, bmap1
->dim
, isl_dim_in
, pos
+= nparam
);
3944 isl_dim_map_dim(dim_map2
, bmap2
->dim
, isl_dim_in
, pos
);
3945 isl_dim_map_div(dim_map1
, bmap1
, pos
+= n_in
+ n_out
);
3946 isl_dim_map_div(dim_map2
, bmap2
, pos
+= bmap1
->n_div
);
3947 isl_dim_map_dim(dim_map1
, bmap1
->dim
, isl_dim_out
, pos
+= bmap2
->n_div
);
3948 isl_dim_map_dim(dim_map2
, bmap2
->dim
, isl_dim_out
, pos
+= n_out
);
3950 bmap
= isl_basic_map_alloc_space(isl_space_copy(bmap1
->dim
),
3951 bmap1
->n_div
+ bmap2
->n_div
+ 2 * n_out
,
3952 bmap1
->n_eq
+ bmap2
->n_eq
+ n_out
,
3953 bmap1
->n_ineq
+ bmap2
->n_ineq
);
3954 for (i
= 0; i
< n_out
; ++i
) {
3955 int j
= isl_basic_map_alloc_equality(bmap
);
3958 isl_seq_clr(bmap
->eq
[j
], 1+total
);
3959 isl_int_set_si(bmap
->eq
[j
][1+nparam
+n_in
+i
], -1);
3960 isl_int_set_si(bmap
->eq
[j
][1+pos
+i
], 1);
3961 isl_int_set_si(bmap
->eq
[j
][1+pos
-n_out
+i
], 1);
3963 bmap
= isl_basic_map_add_constraints_dim_map(bmap
, bmap1
, dim_map1
);
3964 bmap
= isl_basic_map_add_constraints_dim_map(bmap
, bmap2
, dim_map2
);
3965 bmap
= add_divs(bmap
, 2 * n_out
);
3967 bmap
= isl_basic_map_simplify(bmap
);
3968 return isl_basic_map_finalize(bmap
);
3970 isl_basic_map_free(bmap
);
3971 isl_basic_map_free(bmap1
);
3972 isl_basic_map_free(bmap2
);
3976 /* Given two maps A -> f(A) and B -> g(B), construct a map
3977 * A \cap B -> f(A) + f(B)
3979 struct isl_map
*isl_map_sum(struct isl_map
*map1
, struct isl_map
*map2
)
3981 struct isl_map
*result
;
3987 isl_assert(map1
->ctx
, isl_space_is_equal(map1
->dim
, map2
->dim
), goto error
);
3989 result
= isl_map_alloc_space(isl_space_copy(map1
->dim
),
3990 map1
->n
* map2
->n
, 0);
3993 for (i
= 0; i
< map1
->n
; ++i
)
3994 for (j
= 0; j
< map2
->n
; ++j
) {
3995 struct isl_basic_map
*part
;
3996 part
= isl_basic_map_sum(
3997 isl_basic_map_copy(map1
->p
[i
]),
3998 isl_basic_map_copy(map2
->p
[j
]));
3999 if (isl_basic_map_is_empty(part
))
4000 isl_basic_map_free(part
);
4002 result
= isl_map_add_basic_map(result
, part
);
4015 __isl_give isl_set
*isl_set_sum(__isl_take isl_set
*set1
,
4016 __isl_take isl_set
*set2
)
4018 return (isl_set
*)isl_map_sum((isl_map
*)set1
, (isl_map
*)set2
);
4021 /* Given a basic map A -> f(A), construct A -> -f(A).
4023 struct isl_basic_map
*isl_basic_map_neg(struct isl_basic_map
*bmap
)
4028 bmap
= isl_basic_map_cow(bmap
);
4032 n
= isl_basic_map_dim(bmap
, isl_dim_out
);
4033 off
= isl_basic_map_offset(bmap
, isl_dim_out
);
4034 for (i
= 0; i
< bmap
->n_eq
; ++i
)
4035 for (j
= 0; j
< n
; ++j
)
4036 isl_int_neg(bmap
->eq
[i
][off
+j
], bmap
->eq
[i
][off
+j
]);
4037 for (i
= 0; i
< bmap
->n_ineq
; ++i
)
4038 for (j
= 0; j
< n
; ++j
)
4039 isl_int_neg(bmap
->ineq
[i
][off
+j
], bmap
->ineq
[i
][off
+j
]);
4040 for (i
= 0; i
< bmap
->n_div
; ++i
)
4041 for (j
= 0; j
< n
; ++j
)
4042 isl_int_neg(bmap
->div
[i
][1+off
+j
], bmap
->div
[i
][1+off
+j
]);
4043 bmap
= isl_basic_map_gauss(bmap
, NULL
);
4044 return isl_basic_map_finalize(bmap
);
4047 __isl_give isl_basic_set
*isl_basic_set_neg(__isl_take isl_basic_set
*bset
)
4049 return isl_basic_map_neg(bset
);
4052 /* Given a map A -> f(A), construct A -> -f(A).
4054 struct isl_map
*isl_map_neg(struct isl_map
*map
)
4058 map
= isl_map_cow(map
);
4062 for (i
= 0; i
< map
->n
; ++i
) {
4063 map
->p
[i
] = isl_basic_map_neg(map
->p
[i
]);
4074 __isl_give isl_set
*isl_set_neg(__isl_take isl_set
*set
)
4076 return (isl_set
*)isl_map_neg((isl_map
*)set
);
4079 /* Given a basic map A -> f(A) and an integer d, construct a basic map
4080 * A -> floor(f(A)/d).
4082 struct isl_basic_map
*isl_basic_map_floordiv(struct isl_basic_map
*bmap
,
4085 unsigned n_in
, n_out
, nparam
, total
, pos
;
4086 struct isl_basic_map
*result
= NULL
;
4087 struct isl_dim_map
*dim_map
;
4093 nparam
= isl_basic_map_n_param(bmap
);
4094 n_in
= isl_basic_map_n_in(bmap
);
4095 n_out
= isl_basic_map_n_out(bmap
);
4097 total
= nparam
+ n_in
+ n_out
+ bmap
->n_div
+ n_out
;
4098 dim_map
= isl_dim_map_alloc(bmap
->ctx
, total
);
4099 isl_dim_map_dim(dim_map
, bmap
->dim
, isl_dim_param
, pos
= 0);
4100 isl_dim_map_dim(dim_map
, bmap
->dim
, isl_dim_in
, pos
+= nparam
);
4101 isl_dim_map_div(dim_map
, bmap
, pos
+= n_in
+ n_out
);
4102 isl_dim_map_dim(dim_map
, bmap
->dim
, isl_dim_out
, pos
+= bmap
->n_div
);
4104 result
= isl_basic_map_alloc_space(isl_space_copy(bmap
->dim
),
4105 bmap
->n_div
+ n_out
,
4106 bmap
->n_eq
, bmap
->n_ineq
+ 2 * n_out
);
4107 result
= isl_basic_map_add_constraints_dim_map(result
, bmap
, dim_map
);
4108 result
= add_divs(result
, n_out
);
4109 for (i
= 0; i
< n_out
; ++i
) {
4111 j
= isl_basic_map_alloc_inequality(result
);
4114 isl_seq_clr(result
->ineq
[j
], 1+total
);
4115 isl_int_neg(result
->ineq
[j
][1+nparam
+n_in
+i
], d
);
4116 isl_int_set_si(result
->ineq
[j
][1+pos
+i
], 1);
4117 j
= isl_basic_map_alloc_inequality(result
);
4120 isl_seq_clr(result
->ineq
[j
], 1+total
);
4121 isl_int_set(result
->ineq
[j
][1+nparam
+n_in
+i
], d
);
4122 isl_int_set_si(result
->ineq
[j
][1+pos
+i
], -1);
4123 isl_int_sub_ui(result
->ineq
[j
][0], d
, 1);
4126 result
= isl_basic_map_simplify(result
);
4127 return isl_basic_map_finalize(result
);
4129 isl_basic_map_free(result
);
4133 /* Given a map A -> f(A) and an integer d, construct a map
4134 * A -> floor(f(A)/d).
4136 struct isl_map
*isl_map_floordiv(struct isl_map
*map
, isl_int d
)
4140 map
= isl_map_cow(map
);
4144 ISL_F_CLR(map
, ISL_MAP_DISJOINT
);
4145 ISL_F_CLR(map
, ISL_MAP_NORMALIZED
);
4146 for (i
= 0; i
< map
->n
; ++i
) {
4147 map
->p
[i
] = isl_basic_map_floordiv(map
->p
[i
], d
);
4158 /* Given a map A -> f(A) and an integer d, construct a map
4159 * A -> floor(f(A)/d).
4161 __isl_give isl_map
*isl_map_floordiv_val(__isl_take isl_map
*map
,
4162 __isl_take isl_val
*d
)
4166 if (!isl_val_is_int(d
))
4167 isl_die(isl_val_get_ctx(d
), isl_error_invalid
,
4168 "expecting integer denominator", goto error
);
4169 map
= isl_map_floordiv(map
, d
->n
);
4178 static struct isl_basic_map
*var_equal(struct isl_basic_map
*bmap
, unsigned pos
)
4184 i
= isl_basic_map_alloc_equality(bmap
);
4187 nparam
= isl_basic_map_n_param(bmap
);
4188 n_in
= isl_basic_map_n_in(bmap
);
4189 isl_seq_clr(bmap
->eq
[i
], 1 + isl_basic_map_total_dim(bmap
));
4190 isl_int_set_si(bmap
->eq
[i
][1+nparam
+pos
], -1);
4191 isl_int_set_si(bmap
->eq
[i
][1+nparam
+n_in
+pos
], 1);
4192 return isl_basic_map_finalize(bmap
);
4194 isl_basic_map_free(bmap
);
4198 /* Add a constraints to "bmap" expressing i_pos < o_pos
4200 static struct isl_basic_map
*var_less(struct isl_basic_map
*bmap
, unsigned pos
)
4206 i
= isl_basic_map_alloc_inequality(bmap
);
4209 nparam
= isl_basic_map_n_param(bmap
);
4210 n_in
= isl_basic_map_n_in(bmap
);
4211 isl_seq_clr(bmap
->ineq
[i
], 1 + isl_basic_map_total_dim(bmap
));
4212 isl_int_set_si(bmap
->ineq
[i
][0], -1);
4213 isl_int_set_si(bmap
->ineq
[i
][1+nparam
+pos
], -1);
4214 isl_int_set_si(bmap
->ineq
[i
][1+nparam
+n_in
+pos
], 1);
4215 return isl_basic_map_finalize(bmap
);
4217 isl_basic_map_free(bmap
);
4221 /* Add a constraint to "bmap" expressing i_pos <= o_pos
4223 static __isl_give isl_basic_map
*var_less_or_equal(
4224 __isl_take isl_basic_map
*bmap
, unsigned pos
)
4230 i
= isl_basic_map_alloc_inequality(bmap
);
4233 nparam
= isl_basic_map_n_param(bmap
);
4234 n_in
= isl_basic_map_n_in(bmap
);
4235 isl_seq_clr(bmap
->ineq
[i
], 1 + isl_basic_map_total_dim(bmap
));
4236 isl_int_set_si(bmap
->ineq
[i
][1+nparam
+pos
], -1);
4237 isl_int_set_si(bmap
->ineq
[i
][1+nparam
+n_in
+pos
], 1);
4238 return isl_basic_map_finalize(bmap
);
4240 isl_basic_map_free(bmap
);
4244 /* Add a constraints to "bmap" expressing i_pos > o_pos
4246 static struct isl_basic_map
*var_more(struct isl_basic_map
*bmap
, unsigned pos
)
4252 i
= isl_basic_map_alloc_inequality(bmap
);
4255 nparam
= isl_basic_map_n_param(bmap
);
4256 n_in
= isl_basic_map_n_in(bmap
);
4257 isl_seq_clr(bmap
->ineq
[i
], 1 + isl_basic_map_total_dim(bmap
));
4258 isl_int_set_si(bmap
->ineq
[i
][0], -1);
4259 isl_int_set_si(bmap
->ineq
[i
][1+nparam
+pos
], 1);
4260 isl_int_set_si(bmap
->ineq
[i
][1+nparam
+n_in
+pos
], -1);
4261 return isl_basic_map_finalize(bmap
);
4263 isl_basic_map_free(bmap
);
4267 /* Add a constraint to "bmap" expressing i_pos >= o_pos
4269 static __isl_give isl_basic_map
*var_more_or_equal(
4270 __isl_take isl_basic_map
*bmap
, unsigned pos
)
4276 i
= isl_basic_map_alloc_inequality(bmap
);
4279 nparam
= isl_basic_map_n_param(bmap
);
4280 n_in
= isl_basic_map_n_in(bmap
);
4281 isl_seq_clr(bmap
->ineq
[i
], 1 + isl_basic_map_total_dim(bmap
));
4282 isl_int_set_si(bmap
->ineq
[i
][1+nparam
+pos
], 1);
4283 isl_int_set_si(bmap
->ineq
[i
][1+nparam
+n_in
+pos
], -1);
4284 return isl_basic_map_finalize(bmap
);
4286 isl_basic_map_free(bmap
);
4290 __isl_give isl_basic_map
*isl_basic_map_equal(
4291 __isl_take isl_space
*dim
, unsigned n_equal
)
4294 struct isl_basic_map
*bmap
;
4295 bmap
= isl_basic_map_alloc_space(dim
, 0, n_equal
, 0);
4298 for (i
= 0; i
< n_equal
&& bmap
; ++i
)
4299 bmap
= var_equal(bmap
, i
);
4300 return isl_basic_map_finalize(bmap
);
4303 /* Return a relation on of dimension "dim" expressing i_[0..pos] << o_[0..pos]
4305 __isl_give isl_basic_map
*isl_basic_map_less_at(__isl_take isl_space
*dim
,
4309 struct isl_basic_map
*bmap
;
4310 bmap
= isl_basic_map_alloc_space(dim
, 0, pos
, 1);
4313 for (i
= 0; i
< pos
&& bmap
; ++i
)
4314 bmap
= var_equal(bmap
, i
);
4316 bmap
= var_less(bmap
, pos
);
4317 return isl_basic_map_finalize(bmap
);
4320 /* Return a relation on of dimension "dim" expressing i_[0..pos] <<= o_[0..pos]
4322 __isl_give isl_basic_map
*isl_basic_map_less_or_equal_at(
4323 __isl_take isl_space
*dim
, unsigned pos
)
4326 isl_basic_map
*bmap
;
4328 bmap
= isl_basic_map_alloc_space(dim
, 0, pos
, 1);
4329 for (i
= 0; i
< pos
; ++i
)
4330 bmap
= var_equal(bmap
, i
);
4331 bmap
= var_less_or_equal(bmap
, pos
);
4332 return isl_basic_map_finalize(bmap
);
4335 /* Return a relation on pairs of sets of dimension "dim" expressing i_pos > o_pos
4337 __isl_give isl_basic_map
*isl_basic_map_more_at(__isl_take isl_space
*dim
,
4341 struct isl_basic_map
*bmap
;
4342 bmap
= isl_basic_map_alloc_space(dim
, 0, pos
, 1);
4345 for (i
= 0; i
< pos
&& bmap
; ++i
)
4346 bmap
= var_equal(bmap
, i
);
4348 bmap
= var_more(bmap
, pos
);
4349 return isl_basic_map_finalize(bmap
);
4352 /* Return a relation on of dimension "dim" expressing i_[0..pos] >>= o_[0..pos]
4354 __isl_give isl_basic_map
*isl_basic_map_more_or_equal_at(
4355 __isl_take isl_space
*dim
, unsigned pos
)
4358 isl_basic_map
*bmap
;
4360 bmap
= isl_basic_map_alloc_space(dim
, 0, pos
, 1);
4361 for (i
= 0; i
< pos
; ++i
)
4362 bmap
= var_equal(bmap
, i
);
4363 bmap
= var_more_or_equal(bmap
, pos
);
4364 return isl_basic_map_finalize(bmap
);
4367 static __isl_give isl_map
*map_lex_lte_first(__isl_take isl_space
*dims
,
4368 unsigned n
, int equal
)
4370 struct isl_map
*map
;
4373 if (n
== 0 && equal
)
4374 return isl_map_universe(dims
);
4376 map
= isl_map_alloc_space(isl_space_copy(dims
), n
, ISL_MAP_DISJOINT
);
4378 for (i
= 0; i
+ 1 < n
; ++i
)
4379 map
= isl_map_add_basic_map(map
,
4380 isl_basic_map_less_at(isl_space_copy(dims
), i
));
4383 map
= isl_map_add_basic_map(map
,
4384 isl_basic_map_less_or_equal_at(dims
, n
- 1));
4386 map
= isl_map_add_basic_map(map
,
4387 isl_basic_map_less_at(dims
, n
- 1));
4389 isl_space_free(dims
);
4394 static __isl_give isl_map
*map_lex_lte(__isl_take isl_space
*dims
, int equal
)
4398 return map_lex_lte_first(dims
, dims
->n_out
, equal
);
4401 __isl_give isl_map
*isl_map_lex_lt_first(__isl_take isl_space
*dim
, unsigned n
)
4403 return map_lex_lte_first(dim
, n
, 0);
4406 __isl_give isl_map
*isl_map_lex_le_first(__isl_take isl_space
*dim
, unsigned n
)
4408 return map_lex_lte_first(dim
, n
, 1);
4411 __isl_give isl_map
*isl_map_lex_lt(__isl_take isl_space
*set_dim
)
4413 return map_lex_lte(isl_space_map_from_set(set_dim
), 0);
4416 __isl_give isl_map
*isl_map_lex_le(__isl_take isl_space
*set_dim
)
4418 return map_lex_lte(isl_space_map_from_set(set_dim
), 1);
4421 static __isl_give isl_map
*map_lex_gte_first(__isl_take isl_space
*dims
,
4422 unsigned n
, int equal
)
4424 struct isl_map
*map
;
4427 if (n
== 0 && equal
)
4428 return isl_map_universe(dims
);
4430 map
= isl_map_alloc_space(isl_space_copy(dims
), n
, ISL_MAP_DISJOINT
);
4432 for (i
= 0; i
+ 1 < n
; ++i
)
4433 map
= isl_map_add_basic_map(map
,
4434 isl_basic_map_more_at(isl_space_copy(dims
), i
));
4437 map
= isl_map_add_basic_map(map
,
4438 isl_basic_map_more_or_equal_at(dims
, n
- 1));
4440 map
= isl_map_add_basic_map(map
,
4441 isl_basic_map_more_at(dims
, n
- 1));
4443 isl_space_free(dims
);
4448 static __isl_give isl_map
*map_lex_gte(__isl_take isl_space
*dims
, int equal
)
4452 return map_lex_gte_first(dims
, dims
->n_out
, equal
);
4455 __isl_give isl_map
*isl_map_lex_gt_first(__isl_take isl_space
*dim
, unsigned n
)
4457 return map_lex_gte_first(dim
, n
, 0);
4460 __isl_give isl_map
*isl_map_lex_ge_first(__isl_take isl_space
*dim
, unsigned n
)
4462 return map_lex_gte_first(dim
, n
, 1);
4465 __isl_give isl_map
*isl_map_lex_gt(__isl_take isl_space
*set_dim
)
4467 return map_lex_gte(isl_space_map_from_set(set_dim
), 0);
4470 __isl_give isl_map
*isl_map_lex_ge(__isl_take isl_space
*set_dim
)
4472 return map_lex_gte(isl_space_map_from_set(set_dim
), 1);
4475 __isl_give isl_map
*isl_set_lex_le_set(__isl_take isl_set
*set1
,
4476 __isl_take isl_set
*set2
)
4479 map
= isl_map_lex_le(isl_set_get_space(set1
));
4480 map
= isl_map_intersect_domain(map
, set1
);
4481 map
= isl_map_intersect_range(map
, set2
);
4485 __isl_give isl_map
*isl_set_lex_lt_set(__isl_take isl_set
*set1
,
4486 __isl_take isl_set
*set2
)
4489 map
= isl_map_lex_lt(isl_set_get_space(set1
));
4490 map
= isl_map_intersect_domain(map
, set1
);
4491 map
= isl_map_intersect_range(map
, set2
);
4495 __isl_give isl_map
*isl_set_lex_ge_set(__isl_take isl_set
*set1
,
4496 __isl_take isl_set
*set2
)
4499 map
= isl_map_lex_ge(isl_set_get_space(set1
));
4500 map
= isl_map_intersect_domain(map
, set1
);
4501 map
= isl_map_intersect_range(map
, set2
);
4505 __isl_give isl_map
*isl_set_lex_gt_set(__isl_take isl_set
*set1
,
4506 __isl_take isl_set
*set2
)
4509 map
= isl_map_lex_gt(isl_set_get_space(set1
));
4510 map
= isl_map_intersect_domain(map
, set1
);
4511 map
= isl_map_intersect_range(map
, set2
);
4515 __isl_give isl_map
*isl_map_lex_le_map(__isl_take isl_map
*map1
,
4516 __isl_take isl_map
*map2
)
4519 map
= isl_map_lex_le(isl_space_range(isl_map_get_space(map1
)));
4520 map
= isl_map_apply_domain(map
, isl_map_reverse(map1
));
4521 map
= isl_map_apply_range(map
, isl_map_reverse(map2
));
4525 __isl_give isl_map
*isl_map_lex_lt_map(__isl_take isl_map
*map1
,
4526 __isl_take isl_map
*map2
)
4529 map
= isl_map_lex_lt(isl_space_range(isl_map_get_space(map1
)));
4530 map
= isl_map_apply_domain(map
, isl_map_reverse(map1
));
4531 map
= isl_map_apply_range(map
, isl_map_reverse(map2
));
4535 __isl_give isl_map
*isl_map_lex_ge_map(__isl_take isl_map
*map1
,
4536 __isl_take isl_map
*map2
)
4539 map
= isl_map_lex_ge(isl_space_range(isl_map_get_space(map1
)));
4540 map
= isl_map_apply_domain(map
, isl_map_reverse(map1
));
4541 map
= isl_map_apply_range(map
, isl_map_reverse(map2
));
4545 __isl_give isl_map
*isl_map_lex_gt_map(__isl_take isl_map
*map1
,
4546 __isl_take isl_map
*map2
)
4549 map
= isl_map_lex_gt(isl_space_range(isl_map_get_space(map1
)));
4550 map
= isl_map_apply_domain(map
, isl_map_reverse(map1
));
4551 map
= isl_map_apply_range(map
, isl_map_reverse(map2
));
4555 __isl_give isl_basic_map
*isl_basic_map_from_basic_set(
4556 __isl_take isl_basic_set
*bset
, __isl_take isl_space
*dim
)
4558 struct isl_basic_map
*bmap
;
4560 bset
= isl_basic_set_cow(bset
);
4564 isl_assert(bset
->ctx
, isl_space_compatible(bset
->dim
, dim
), goto error
);
4565 isl_space_free(bset
->dim
);
4566 bmap
= (struct isl_basic_map
*) bset
;
4568 return isl_basic_map_finalize(bmap
);
4570 isl_basic_set_free(bset
);
4571 isl_space_free(dim
);
4575 /* For a div d = floor(f/m), add the constraint
4579 static int add_upper_div_constraint(__isl_keep isl_basic_map
*bmap
,
4580 unsigned pos
, isl_int
*div
)
4583 unsigned total
= isl_basic_map_total_dim(bmap
);
4585 i
= isl_basic_map_alloc_inequality(bmap
);
4588 isl_seq_cpy(bmap
->ineq
[i
], div
+ 1, 1 + total
);
4589 isl_int_neg(bmap
->ineq
[i
][1 + pos
], div
[0]);
4594 /* For a div d = floor(f/m), add the constraint
4596 * -(f-(m-1)) + m d >= 0
4598 static int add_lower_div_constraint(__isl_keep isl_basic_map
*bmap
,
4599 unsigned pos
, isl_int
*div
)
4602 unsigned total
= isl_basic_map_total_dim(bmap
);
4604 i
= isl_basic_map_alloc_inequality(bmap
);
4607 isl_seq_neg(bmap
->ineq
[i
], div
+ 1, 1 + total
);
4608 isl_int_set(bmap
->ineq
[i
][1 + pos
], div
[0]);
4609 isl_int_add(bmap
->ineq
[i
][0], bmap
->ineq
[i
][0], bmap
->ineq
[i
][1 + pos
]);
4610 isl_int_sub_ui(bmap
->ineq
[i
][0], bmap
->ineq
[i
][0], 1);
4615 /* For a div d = floor(f/m), add the constraints
4618 * -(f-(m-1)) + m d >= 0
4620 * Note that the second constraint is the negation of
4624 int isl_basic_map_add_div_constraints_var(__isl_keep isl_basic_map
*bmap
,
4625 unsigned pos
, isl_int
*div
)
4627 if (add_upper_div_constraint(bmap
, pos
, div
) < 0)
4629 if (add_lower_div_constraint(bmap
, pos
, div
) < 0)
4634 int isl_basic_set_add_div_constraints_var(__isl_keep isl_basic_set
*bset
,
4635 unsigned pos
, isl_int
*div
)
4637 return isl_basic_map_add_div_constraints_var((isl_basic_map
*)bset
,
4641 int isl_basic_map_add_div_constraints(struct isl_basic_map
*bmap
, unsigned div
)
4643 unsigned total
= isl_basic_map_total_dim(bmap
);
4644 unsigned div_pos
= total
- bmap
->n_div
+ div
;
4646 return isl_basic_map_add_div_constraints_var(bmap
, div_pos
,
4650 /* For each known div d = floor(f/m), add the constraints
4653 * -(f-(m-1)) + m d >= 0
4655 * Remove duplicate constraints in case of some these div constraints
4656 * already appear in "bmap".
4658 __isl_give isl_basic_map
*isl_basic_map_add_known_div_constraints(
4659 __isl_take isl_basic_map
*bmap
)
4665 n_div
= isl_basic_map_dim(bmap
, isl_dim_div
);
4669 bmap
= add_known_div_constraints(bmap
);
4670 bmap
= isl_basic_map_remove_duplicate_constraints(bmap
, NULL
, 0);
4671 bmap
= isl_basic_map_finalize(bmap
);
4675 /* Add the div constraint of sign "sign" for div "div" of "bmap".
4677 * In particular, if this div is of the form d = floor(f/m),
4678 * then add the constraint
4682 * if sign < 0 or the constraint
4684 * -(f-(m-1)) + m d >= 0
4688 int isl_basic_map_add_div_constraint(__isl_keep isl_basic_map
*bmap
,
4689 unsigned div
, int sign
)
4697 total
= isl_basic_map_total_dim(bmap
);
4698 div_pos
= total
- bmap
->n_div
+ div
;
4701 return add_upper_div_constraint(bmap
, div_pos
, bmap
->div
[div
]);
4703 return add_lower_div_constraint(bmap
, div_pos
, bmap
->div
[div
]);
4706 int isl_basic_set_add_div_constraints(struct isl_basic_set
*bset
, unsigned div
)
4708 return isl_basic_map_add_div_constraints(bset
, div
);
4711 struct isl_basic_set
*isl_basic_map_underlying_set(
4712 struct isl_basic_map
*bmap
)
4716 if (bmap
->dim
->nparam
== 0 && bmap
->dim
->n_in
== 0 &&
4718 !isl_space_is_named_or_nested(bmap
->dim
, isl_dim_in
) &&
4719 !isl_space_is_named_or_nested(bmap
->dim
, isl_dim_out
))
4720 return (struct isl_basic_set
*)bmap
;
4721 bmap
= isl_basic_map_cow(bmap
);
4724 bmap
->dim
= isl_space_underlying(bmap
->dim
, bmap
->n_div
);
4727 bmap
->extra
-= bmap
->n_div
;
4729 bmap
= isl_basic_map_finalize(bmap
);
4730 return (struct isl_basic_set
*)bmap
;
4732 isl_basic_map_free(bmap
);
4736 __isl_give isl_basic_set
*isl_basic_set_underlying_set(
4737 __isl_take isl_basic_set
*bset
)
4739 return isl_basic_map_underlying_set((isl_basic_map
*)bset
);
4742 /* Replace each element in "list" by the result of applying
4743 * isl_basic_map_underlying_set to the element.
4745 __isl_give isl_basic_set_list
*isl_basic_map_list_underlying_set(
4746 __isl_take isl_basic_map_list
*list
)
4753 n
= isl_basic_map_list_n_basic_map(list
);
4754 for (i
= 0; i
< n
; ++i
) {
4755 isl_basic_map
*bmap
;
4756 isl_basic_set
*bset
;
4758 bmap
= isl_basic_map_list_get_basic_map(list
, i
);
4759 bset
= isl_basic_set_underlying_set(bmap
);
4760 list
= isl_basic_set_list_set_basic_set(list
, i
, bset
);
4766 struct isl_basic_map
*isl_basic_map_overlying_set(
4767 struct isl_basic_set
*bset
, struct isl_basic_map
*like
)
4769 struct isl_basic_map
*bmap
;
4770 struct isl_ctx
*ctx
;
4777 isl_assert(ctx
, bset
->n_div
== 0, goto error
);
4778 isl_assert(ctx
, isl_basic_set_n_param(bset
) == 0, goto error
);
4779 isl_assert(ctx
, bset
->dim
->n_out
== isl_basic_map_total_dim(like
),
4781 if (like
->n_div
== 0) {
4782 isl_space
*space
= isl_basic_map_get_space(like
);
4783 isl_basic_map_free(like
);
4784 return isl_basic_map_reset_space(bset
, space
);
4786 bset
= isl_basic_set_cow(bset
);
4789 total
= bset
->dim
->n_out
+ bset
->extra
;
4790 bmap
= (struct isl_basic_map
*)bset
;
4791 isl_space_free(bmap
->dim
);
4792 bmap
->dim
= isl_space_copy(like
->dim
);
4795 bmap
->n_div
= like
->n_div
;
4796 bmap
->extra
+= like
->n_div
;
4800 ltotal
= total
- bmap
->extra
+ like
->extra
;
4803 bmap
->block2
= isl_blk_extend(ctx
, bmap
->block2
,
4804 bmap
->extra
* (1 + 1 + total
));
4805 if (isl_blk_is_error(bmap
->block2
))
4807 div
= isl_realloc_array(ctx
, bmap
->div
, isl_int
*, bmap
->extra
);
4811 for (i
= 0; i
< bmap
->extra
; ++i
)
4812 bmap
->div
[i
] = bmap
->block2
.data
+ i
* (1 + 1 + total
);
4813 for (i
= 0; i
< like
->n_div
; ++i
) {
4814 isl_seq_cpy(bmap
->div
[i
], like
->div
[i
], 1 + 1 + ltotal
);
4815 isl_seq_clr(bmap
->div
[i
]+1+1+ltotal
, total
- ltotal
);
4817 bmap
= isl_basic_map_add_known_div_constraints(bmap
);
4819 isl_basic_map_free(like
);
4820 bmap
= isl_basic_map_simplify(bmap
);
4821 bmap
= isl_basic_map_finalize(bmap
);
4824 isl_basic_map_free(like
);
4825 isl_basic_set_free(bset
);
4829 struct isl_basic_set
*isl_basic_set_from_underlying_set(
4830 struct isl_basic_set
*bset
, struct isl_basic_set
*like
)
4832 return (struct isl_basic_set
*)
4833 isl_basic_map_overlying_set(bset
, (struct isl_basic_map
*)like
);
4836 struct isl_set
*isl_set_from_underlying_set(
4837 struct isl_set
*set
, struct isl_basic_set
*like
)
4843 isl_assert(set
->ctx
, set
->dim
->n_out
== isl_basic_set_total_dim(like
),
4845 if (isl_space_is_equal(set
->dim
, like
->dim
) && like
->n_div
== 0) {
4846 isl_basic_set_free(like
);
4849 set
= isl_set_cow(set
);
4852 for (i
= 0; i
< set
->n
; ++i
) {
4853 set
->p
[i
] = isl_basic_set_from_underlying_set(set
->p
[i
],
4854 isl_basic_set_copy(like
));
4858 isl_space_free(set
->dim
);
4859 set
->dim
= isl_space_copy(like
->dim
);
4862 isl_basic_set_free(like
);
4865 isl_basic_set_free(like
);
4870 struct isl_set
*isl_map_underlying_set(struct isl_map
*map
)
4874 map
= isl_map_cow(map
);
4877 map
->dim
= isl_space_cow(map
->dim
);
4881 for (i
= 1; i
< map
->n
; ++i
)
4882 isl_assert(map
->ctx
, map
->p
[0]->n_div
== map
->p
[i
]->n_div
,
4884 for (i
= 0; i
< map
->n
; ++i
) {
4885 map
->p
[i
] = (struct isl_basic_map
*)
4886 isl_basic_map_underlying_set(map
->p
[i
]);
4891 map
->dim
= isl_space_underlying(map
->dim
, 0);
4893 isl_space_free(map
->dim
);
4894 map
->dim
= isl_space_copy(map
->p
[0]->dim
);
4898 return (struct isl_set
*)map
;
4904 struct isl_set
*isl_set_to_underlying_set(struct isl_set
*set
)
4906 return (struct isl_set
*)isl_map_underlying_set((struct isl_map
*)set
);
4909 /* Replace the space of "bmap" by "space".
4911 * If the space of "bmap" is identical to "space" (including the identifiers
4912 * of the input and output dimensions), then simply return the original input.
4914 __isl_give isl_basic_map
*isl_basic_map_reset_space(
4915 __isl_take isl_basic_map
*bmap
, __isl_take isl_space
*space
)
4921 equal
= isl_space_is_equal(bmap
->dim
, space
);
4922 if (equal
>= 0 && equal
)
4923 equal
= isl_space_match(bmap
->dim
, isl_dim_in
,
4925 if (equal
>= 0 && equal
)
4926 equal
= isl_space_match(bmap
->dim
, isl_dim_out
,
4927 space
, isl_dim_out
);
4931 isl_space_free(space
);
4934 bmap
= isl_basic_map_cow(bmap
);
4935 if (!bmap
|| !space
)
4938 isl_space_free(bmap
->dim
);
4941 bmap
= isl_basic_map_finalize(bmap
);
4945 isl_basic_map_free(bmap
);
4946 isl_space_free(space
);
4950 __isl_give isl_basic_set
*isl_basic_set_reset_space(
4951 __isl_take isl_basic_set
*bset
, __isl_take isl_space
*dim
)
4953 return (isl_basic_set
*)isl_basic_map_reset_space((isl_basic_map
*)bset
,
4957 __isl_give isl_map
*isl_map_reset_space(__isl_take isl_map
*map
,
4958 __isl_take isl_space
*dim
)
4962 map
= isl_map_cow(map
);
4966 for (i
= 0; i
< map
->n
; ++i
) {
4967 map
->p
[i
] = isl_basic_map_reset_space(map
->p
[i
],
4968 isl_space_copy(dim
));
4972 isl_space_free(map
->dim
);
4978 isl_space_free(dim
);
4982 __isl_give isl_set
*isl_set_reset_space(__isl_take isl_set
*set
,
4983 __isl_take isl_space
*dim
)
4985 return (struct isl_set
*) isl_map_reset_space((struct isl_map
*)set
, dim
);
4988 /* Compute the parameter domain of the given basic set.
4990 __isl_give isl_basic_set
*isl_basic_set_params(__isl_take isl_basic_set
*bset
)
4995 if (isl_basic_set_is_params(bset
))
4998 n
= isl_basic_set_dim(bset
, isl_dim_set
);
4999 bset
= isl_basic_set_project_out(bset
, isl_dim_set
, 0, n
);
5000 space
= isl_basic_set_get_space(bset
);
5001 space
= isl_space_params(space
);
5002 bset
= isl_basic_set_reset_space(bset
, space
);
5006 /* Construct a zero-dimensional basic set with the given parameter domain.
5008 __isl_give isl_basic_set
*isl_basic_set_from_params(
5009 __isl_take isl_basic_set
*bset
)
5012 space
= isl_basic_set_get_space(bset
);
5013 space
= isl_space_set_from_params(space
);
5014 bset
= isl_basic_set_reset_space(bset
, space
);
5018 /* Compute the parameter domain of the given set.
5020 __isl_give isl_set
*isl_set_params(__isl_take isl_set
*set
)
5025 if (isl_set_is_params(set
))
5028 n
= isl_set_dim(set
, isl_dim_set
);
5029 set
= isl_set_project_out(set
, isl_dim_set
, 0, n
);
5030 space
= isl_set_get_space(set
);
5031 space
= isl_space_params(space
);
5032 set
= isl_set_reset_space(set
, space
);
5036 /* Construct a zero-dimensional set with the given parameter domain.
5038 __isl_give isl_set
*isl_set_from_params(__isl_take isl_set
*set
)
5041 space
= isl_set_get_space(set
);
5042 space
= isl_space_set_from_params(space
);
5043 set
= isl_set_reset_space(set
, space
);
5047 /* Compute the parameter domain of the given map.
5049 __isl_give isl_set
*isl_map_params(__isl_take isl_map
*map
)
5054 n
= isl_map_dim(map
, isl_dim_in
);
5055 map
= isl_map_project_out(map
, isl_dim_in
, 0, n
);
5056 n
= isl_map_dim(map
, isl_dim_out
);
5057 map
= isl_map_project_out(map
, isl_dim_out
, 0, n
);
5058 space
= isl_map_get_space(map
);
5059 space
= isl_space_params(space
);
5060 map
= isl_map_reset_space(map
, space
);
5064 struct isl_basic_set
*isl_basic_map_domain(struct isl_basic_map
*bmap
)
5071 space
= isl_space_domain(isl_basic_map_get_space(bmap
));
5073 n_out
= isl_basic_map_n_out(bmap
);
5074 bmap
= isl_basic_map_project_out(bmap
, isl_dim_out
, 0, n_out
);
5076 return isl_basic_map_reset_space(bmap
, space
);
5079 int isl_basic_map_may_be_set(__isl_keep isl_basic_map
*bmap
)
5083 return isl_space_may_be_set(bmap
->dim
);
5086 /* Is this basic map actually a set?
5087 * Users should never call this function. Outside of isl,
5088 * the type should indicate whether something is a set or a map.
5090 int isl_basic_map_is_set(__isl_keep isl_basic_map
*bmap
)
5094 return isl_space_is_set(bmap
->dim
);
5097 struct isl_basic_set
*isl_basic_map_range(struct isl_basic_map
*bmap
)
5101 if (isl_basic_map_is_set(bmap
))
5103 return isl_basic_map_domain(isl_basic_map_reverse(bmap
));
5106 __isl_give isl_basic_map
*isl_basic_map_domain_map(
5107 __isl_take isl_basic_map
*bmap
)
5111 isl_basic_map
*domain
;
5112 int nparam
, n_in
, n_out
;
5115 nparam
= isl_basic_map_dim(bmap
, isl_dim_param
);
5116 n_in
= isl_basic_map_dim(bmap
, isl_dim_in
);
5117 n_out
= isl_basic_map_dim(bmap
, isl_dim_out
);
5119 dim
= isl_space_from_range(isl_space_domain(isl_basic_map_get_space(bmap
)));
5120 domain
= isl_basic_map_universe(dim
);
5122 bmap
= isl_basic_map_from_domain(isl_basic_map_wrap(bmap
));
5123 bmap
= isl_basic_map_apply_range(bmap
, domain
);
5124 bmap
= isl_basic_map_extend_constraints(bmap
, n_in
, 0);
5126 total
= isl_basic_map_total_dim(bmap
);
5128 for (i
= 0; i
< n_in
; ++i
) {
5129 k
= isl_basic_map_alloc_equality(bmap
);
5132 isl_seq_clr(bmap
->eq
[k
], 1 + total
);
5133 isl_int_set_si(bmap
->eq
[k
][1 + nparam
+ i
], -1);
5134 isl_int_set_si(bmap
->eq
[k
][1 + nparam
+ n_in
+ n_out
+ i
], 1);
5137 bmap
= isl_basic_map_gauss(bmap
, NULL
);
5138 return isl_basic_map_finalize(bmap
);
5140 isl_basic_map_free(bmap
);
5144 __isl_give isl_basic_map
*isl_basic_map_range_map(
5145 __isl_take isl_basic_map
*bmap
)
5149 isl_basic_map
*range
;
5150 int nparam
, n_in
, n_out
;
5153 nparam
= isl_basic_map_dim(bmap
, isl_dim_param
);
5154 n_in
= isl_basic_map_dim(bmap
, isl_dim_in
);
5155 n_out
= isl_basic_map_dim(bmap
, isl_dim_out
);
5157 dim
= isl_space_from_range(isl_space_range(isl_basic_map_get_space(bmap
)));
5158 range
= isl_basic_map_universe(dim
);
5160 bmap
= isl_basic_map_from_domain(isl_basic_map_wrap(bmap
));
5161 bmap
= isl_basic_map_apply_range(bmap
, range
);
5162 bmap
= isl_basic_map_extend_constraints(bmap
, n_out
, 0);
5164 total
= isl_basic_map_total_dim(bmap
);
5166 for (i
= 0; i
< n_out
; ++i
) {
5167 k
= isl_basic_map_alloc_equality(bmap
);
5170 isl_seq_clr(bmap
->eq
[k
], 1 + total
);
5171 isl_int_set_si(bmap
->eq
[k
][1 + nparam
+ n_in
+ i
], -1);
5172 isl_int_set_si(bmap
->eq
[k
][1 + nparam
+ n_in
+ n_out
+ i
], 1);
5175 bmap
= isl_basic_map_gauss(bmap
, NULL
);
5176 return isl_basic_map_finalize(bmap
);
5178 isl_basic_map_free(bmap
);
5182 int isl_map_may_be_set(__isl_keep isl_map
*map
)
5186 return isl_space_may_be_set(map
->dim
);
5189 /* Is this map actually a set?
5190 * Users should never call this function. Outside of isl,
5191 * the type should indicate whether something is a set or a map.
5193 int isl_map_is_set(__isl_keep isl_map
*map
)
5197 return isl_space_is_set(map
->dim
);
5200 struct isl_set
*isl_map_range(struct isl_map
*map
)
5203 struct isl_set
*set
;
5207 if (isl_map_is_set(map
))
5208 return (isl_set
*)map
;
5210 map
= isl_map_cow(map
);
5214 set
= (struct isl_set
*) map
;
5215 set
->dim
= isl_space_range(set
->dim
);
5218 for (i
= 0; i
< map
->n
; ++i
) {
5219 set
->p
[i
] = isl_basic_map_range(map
->p
[i
]);
5223 ISL_F_CLR(set
, ISL_MAP_DISJOINT
);
5224 ISL_F_CLR(set
, ISL_SET_NORMALIZED
);
5231 __isl_give isl_map
*isl_map_domain_map(__isl_take isl_map
*map
)
5235 map
= isl_map_cow(map
);
5239 map
->dim
= isl_space_domain_map(map
->dim
);
5242 for (i
= 0; i
< map
->n
; ++i
) {
5243 map
->p
[i
] = isl_basic_map_domain_map(map
->p
[i
]);
5247 ISL_F_CLR(map
, ISL_MAP_DISJOINT
);
5248 ISL_F_CLR(map
, ISL_MAP_NORMALIZED
);
5255 __isl_give isl_map
*isl_map_range_map(__isl_take isl_map
*map
)
5258 isl_space
*range_dim
;
5260 map
= isl_map_cow(map
);
5264 range_dim
= isl_space_range(isl_map_get_space(map
));
5265 range_dim
= isl_space_from_range(range_dim
);
5266 map
->dim
= isl_space_from_domain(isl_space_wrap(map
->dim
));
5267 map
->dim
= isl_space_join(map
->dim
, range_dim
);
5270 for (i
= 0; i
< map
->n
; ++i
) {
5271 map
->p
[i
] = isl_basic_map_range_map(map
->p
[i
]);
5275 ISL_F_CLR(map
, ISL_MAP_DISJOINT
);
5276 ISL_F_CLR(map
, ISL_MAP_NORMALIZED
);
5283 /* Given a wrapped map of the form A[B -> C],
5284 * return the map A[B -> C] -> B.
5286 __isl_give isl_map
*isl_set_wrapped_domain_map(__isl_take isl_set
*set
)
5293 if (!isl_set_has_tuple_id(set
))
5294 return isl_map_domain_map(isl_set_unwrap(set
));
5296 id
= isl_set_get_tuple_id(set
);
5297 map
= isl_map_domain_map(isl_set_unwrap(set
));
5298 map
= isl_map_set_tuple_id(map
, isl_dim_in
, id
);
5303 __isl_give isl_map
*isl_map_from_set(__isl_take isl_set
*set
,
5304 __isl_take isl_space
*dim
)
5307 struct isl_map
*map
= NULL
;
5309 set
= isl_set_cow(set
);
5312 isl_assert(set
->ctx
, isl_space_compatible(set
->dim
, dim
), goto error
);
5313 map
= (struct isl_map
*)set
;
5314 for (i
= 0; i
< set
->n
; ++i
) {
5315 map
->p
[i
] = isl_basic_map_from_basic_set(
5316 set
->p
[i
], isl_space_copy(dim
));
5320 isl_space_free(map
->dim
);
5324 isl_space_free(dim
);
5329 __isl_give isl_basic_map
*isl_basic_map_from_domain(
5330 __isl_take isl_basic_set
*bset
)
5332 return isl_basic_map_reverse(isl_basic_map_from_range(bset
));
5335 __isl_give isl_basic_map
*isl_basic_map_from_range(
5336 __isl_take isl_basic_set
*bset
)
5339 space
= isl_basic_set_get_space(bset
);
5340 space
= isl_space_from_range(space
);
5341 bset
= isl_basic_set_reset_space(bset
, space
);
5342 return (isl_basic_map
*)bset
;
5345 /* Create a relation with the given set as range.
5346 * The domain of the created relation is a zero-dimensional
5347 * flat anonymous space.
5349 __isl_give isl_map
*isl_map_from_range(__isl_take isl_set
*set
)
5352 space
= isl_set_get_space(set
);
5353 space
= isl_space_from_range(space
);
5354 set
= isl_set_reset_space(set
, space
);
5355 return (struct isl_map
*)set
;
5358 /* Create a relation with the given set as domain.
5359 * The range of the created relation is a zero-dimensional
5360 * flat anonymous space.
5362 __isl_give isl_map
*isl_map_from_domain(__isl_take isl_set
*set
)
5364 return isl_map_reverse(isl_map_from_range(set
));
5367 __isl_give isl_basic_map
*isl_basic_map_from_domain_and_range(
5368 __isl_take isl_basic_set
*domain
, __isl_take isl_basic_set
*range
)
5370 return isl_basic_map_apply_range(isl_basic_map_reverse(domain
), range
);
5373 __isl_give isl_map
*isl_map_from_domain_and_range(__isl_take isl_set
*domain
,
5374 __isl_take isl_set
*range
)
5376 return isl_map_apply_range(isl_map_reverse(domain
), range
);
5379 /* Return a newly allocated isl_map with given space and flags and
5380 * room for "n" basic maps.
5381 * Make sure that all cached information is cleared.
5383 __isl_give isl_map
*isl_map_alloc_space(__isl_take isl_space
*space
, int n
,
5386 struct isl_map
*map
;
5391 isl_die(space
->ctx
, isl_error_internal
,
5392 "negative number of basic maps", goto error
);
5393 map
= isl_calloc(space
->ctx
, struct isl_map
,
5394 sizeof(struct isl_map
) +
5395 (n
- 1) * sizeof(struct isl_basic_map
*));
5399 map
->ctx
= space
->ctx
;
5400 isl_ctx_ref(map
->ctx
);
5408 isl_space_free(space
);
5412 struct isl_map
*isl_map_alloc(struct isl_ctx
*ctx
,
5413 unsigned nparam
, unsigned in
, unsigned out
, int n
,
5416 struct isl_map
*map
;
5419 dims
= isl_space_alloc(ctx
, nparam
, in
, out
);
5423 map
= isl_map_alloc_space(dims
, n
, flags
);
5427 __isl_give isl_basic_map
*isl_basic_map_empty(__isl_take isl_space
*dim
)
5429 struct isl_basic_map
*bmap
;
5430 bmap
= isl_basic_map_alloc_space(dim
, 0, 1, 0);
5431 bmap
= isl_basic_map_set_to_empty(bmap
);
5435 __isl_give isl_basic_set
*isl_basic_set_empty(__isl_take isl_space
*dim
)
5437 struct isl_basic_set
*bset
;
5438 bset
= isl_basic_set_alloc_space(dim
, 0, 1, 0);
5439 bset
= isl_basic_set_set_to_empty(bset
);
5443 __isl_give isl_basic_map
*isl_basic_map_universe(__isl_take isl_space
*dim
)
5445 struct isl_basic_map
*bmap
;
5446 bmap
= isl_basic_map_alloc_space(dim
, 0, 0, 0);
5447 bmap
= isl_basic_map_finalize(bmap
);
5451 __isl_give isl_basic_set
*isl_basic_set_universe(__isl_take isl_space
*dim
)
5453 struct isl_basic_set
*bset
;
5454 bset
= isl_basic_set_alloc_space(dim
, 0, 0, 0);
5455 bset
= isl_basic_set_finalize(bset
);
5459 __isl_give isl_basic_map
*isl_basic_map_nat_universe(__isl_take isl_space
*dim
)
5462 unsigned total
= isl_space_dim(dim
, isl_dim_all
);
5463 isl_basic_map
*bmap
;
5465 bmap
= isl_basic_map_alloc_space(dim
, 0, 0, total
);
5466 for (i
= 0; i
< total
; ++i
) {
5467 int k
= isl_basic_map_alloc_inequality(bmap
);
5470 isl_seq_clr(bmap
->ineq
[k
], 1 + total
);
5471 isl_int_set_si(bmap
->ineq
[k
][1 + i
], 1);
5475 isl_basic_map_free(bmap
);
5479 __isl_give isl_basic_set
*isl_basic_set_nat_universe(__isl_take isl_space
*dim
)
5481 return isl_basic_map_nat_universe(dim
);
5484 __isl_give isl_map
*isl_map_nat_universe(__isl_take isl_space
*dim
)
5486 return isl_map_from_basic_map(isl_basic_map_nat_universe(dim
));
5489 __isl_give isl_set
*isl_set_nat_universe(__isl_take isl_space
*dim
)
5491 return isl_map_nat_universe(dim
);
5494 __isl_give isl_map
*isl_map_empty(__isl_take isl_space
*dim
)
5496 return isl_map_alloc_space(dim
, 0, ISL_MAP_DISJOINT
);
5499 __isl_give isl_set
*isl_set_empty(__isl_take isl_space
*dim
)
5501 return isl_set_alloc_space(dim
, 0, ISL_MAP_DISJOINT
);
5504 __isl_give isl_map
*isl_map_universe(__isl_take isl_space
*dim
)
5506 struct isl_map
*map
;
5509 map
= isl_map_alloc_space(isl_space_copy(dim
), 1, ISL_MAP_DISJOINT
);
5510 map
= isl_map_add_basic_map(map
, isl_basic_map_universe(dim
));
5514 __isl_give isl_set
*isl_set_universe(__isl_take isl_space
*dim
)
5516 struct isl_set
*set
;
5519 set
= isl_set_alloc_space(isl_space_copy(dim
), 1, ISL_MAP_DISJOINT
);
5520 set
= isl_set_add_basic_set(set
, isl_basic_set_universe(dim
));
5524 struct isl_map
*isl_map_dup(struct isl_map
*map
)
5527 struct isl_map
*dup
;
5531 dup
= isl_map_alloc_space(isl_space_copy(map
->dim
), map
->n
, map
->flags
);
5532 for (i
= 0; i
< map
->n
; ++i
)
5533 dup
= isl_map_add_basic_map(dup
, isl_basic_map_copy(map
->p
[i
]));
5537 __isl_give isl_map
*isl_map_add_basic_map(__isl_take isl_map
*map
,
5538 __isl_take isl_basic_map
*bmap
)
5542 if (isl_basic_map_plain_is_empty(bmap
)) {
5543 isl_basic_map_free(bmap
);
5546 isl_assert(map
->ctx
, isl_space_is_equal(map
->dim
, bmap
->dim
), goto error
);
5547 isl_assert(map
->ctx
, map
->n
< map
->size
, goto error
);
5548 map
->p
[map
->n
] = bmap
;
5550 ISL_F_CLR(map
, ISL_MAP_NORMALIZED
);
5556 isl_basic_map_free(bmap
);
5560 __isl_null isl_map
*isl_map_free(__isl_take isl_map
*map
)
5571 isl_ctx_deref(map
->ctx
);
5572 for (i
= 0; i
< map
->n
; ++i
)
5573 isl_basic_map_free(map
->p
[i
]);
5574 isl_space_free(map
->dim
);
5580 static struct isl_basic_map
*isl_basic_map_fix_pos_si(
5581 struct isl_basic_map
*bmap
, unsigned pos
, int value
)
5585 bmap
= isl_basic_map_cow(bmap
);
5586 bmap
= isl_basic_map_extend_constraints(bmap
, 1, 0);
5587 j
= isl_basic_map_alloc_equality(bmap
);
5590 isl_seq_clr(bmap
->eq
[j
] + 1, isl_basic_map_total_dim(bmap
));
5591 isl_int_set_si(bmap
->eq
[j
][pos
], -1);
5592 isl_int_set_si(bmap
->eq
[j
][0], value
);
5593 bmap
= isl_basic_map_simplify(bmap
);
5594 return isl_basic_map_finalize(bmap
);
5596 isl_basic_map_free(bmap
);
5600 static __isl_give isl_basic_map
*isl_basic_map_fix_pos(
5601 __isl_take isl_basic_map
*bmap
, unsigned pos
, isl_int value
)
5605 bmap
= isl_basic_map_cow(bmap
);
5606 bmap
= isl_basic_map_extend_constraints(bmap
, 1, 0);
5607 j
= isl_basic_map_alloc_equality(bmap
);
5610 isl_seq_clr(bmap
->eq
[j
] + 1, isl_basic_map_total_dim(bmap
));
5611 isl_int_set_si(bmap
->eq
[j
][pos
], -1);
5612 isl_int_set(bmap
->eq
[j
][0], value
);
5613 bmap
= isl_basic_map_simplify(bmap
);
5614 return isl_basic_map_finalize(bmap
);
5616 isl_basic_map_free(bmap
);
5620 struct isl_basic_map
*isl_basic_map_fix_si(struct isl_basic_map
*bmap
,
5621 enum isl_dim_type type
, unsigned pos
, int value
)
5625 isl_assert(bmap
->ctx
, pos
< isl_basic_map_dim(bmap
, type
), goto error
);
5626 return isl_basic_map_fix_pos_si(bmap
,
5627 isl_basic_map_offset(bmap
, type
) + pos
, value
);
5629 isl_basic_map_free(bmap
);
5633 __isl_give isl_basic_map
*isl_basic_map_fix(__isl_take isl_basic_map
*bmap
,
5634 enum isl_dim_type type
, unsigned pos
, isl_int value
)
5638 isl_assert(bmap
->ctx
, pos
< isl_basic_map_dim(bmap
, type
), goto error
);
5639 return isl_basic_map_fix_pos(bmap
,
5640 isl_basic_map_offset(bmap
, type
) + pos
, value
);
5642 isl_basic_map_free(bmap
);
5646 /* Fix the value of the variable at position "pos" of type "type" of "bmap"
5647 * to be equal to "v".
5649 __isl_give isl_basic_map
*isl_basic_map_fix_val(__isl_take isl_basic_map
*bmap
,
5650 enum isl_dim_type type
, unsigned pos
, __isl_take isl_val
*v
)
5654 if (!isl_val_is_int(v
))
5655 isl_die(isl_basic_map_get_ctx(bmap
), isl_error_invalid
,
5656 "expecting integer value", goto error
);
5657 if (pos
>= isl_basic_map_dim(bmap
, type
))
5658 isl_die(isl_basic_map_get_ctx(bmap
), isl_error_invalid
,
5659 "index out of bounds", goto error
);
5660 pos
+= isl_basic_map_offset(bmap
, type
);
5661 bmap
= isl_basic_map_fix_pos(bmap
, pos
, v
->n
);
5665 isl_basic_map_free(bmap
);
5670 /* Fix the value of the variable at position "pos" of type "type" of "bset"
5671 * to be equal to "v".
5673 __isl_give isl_basic_set
*isl_basic_set_fix_val(__isl_take isl_basic_set
*bset
,
5674 enum isl_dim_type type
, unsigned pos
, __isl_take isl_val
*v
)
5676 return isl_basic_map_fix_val(bset
, type
, pos
, v
);
5679 struct isl_basic_set
*isl_basic_set_fix_si(struct isl_basic_set
*bset
,
5680 enum isl_dim_type type
, unsigned pos
, int value
)
5682 return (struct isl_basic_set
*)
5683 isl_basic_map_fix_si((struct isl_basic_map
*)bset
,
5687 __isl_give isl_basic_set
*isl_basic_set_fix(__isl_take isl_basic_set
*bset
,
5688 enum isl_dim_type type
, unsigned pos
, isl_int value
)
5690 return (struct isl_basic_set
*)
5691 isl_basic_map_fix((struct isl_basic_map
*)bset
,
5695 struct isl_basic_map
*isl_basic_map_fix_input_si(struct isl_basic_map
*bmap
,
5696 unsigned input
, int value
)
5698 return isl_basic_map_fix_si(bmap
, isl_dim_in
, input
, value
);
5701 struct isl_basic_set
*isl_basic_set_fix_dim_si(struct isl_basic_set
*bset
,
5702 unsigned dim
, int value
)
5704 return (struct isl_basic_set
*)
5705 isl_basic_map_fix_si((struct isl_basic_map
*)bset
,
5706 isl_dim_set
, dim
, value
);
5709 static int remove_if_empty(__isl_keep isl_map
*map
, int i
)
5711 int empty
= isl_basic_map_plain_is_empty(map
->p
[i
]);
5718 isl_basic_map_free(map
->p
[i
]);
5719 if (i
!= map
->n
- 1) {
5720 ISL_F_CLR(map
, ISL_MAP_NORMALIZED
);
5721 map
->p
[i
] = map
->p
[map
->n
- 1];
5728 /* Perform "fn" on each basic map of "map", where we may not be holding
5729 * the only reference to "map".
5730 * In particular, "fn" should be a semantics preserving operation
5731 * that we want to apply to all copies of "map". We therefore need
5732 * to be careful not to modify "map" in a way that breaks "map"
5733 * in case anything goes wrong.
5735 __isl_give isl_map
*isl_map_inline_foreach_basic_map(__isl_take isl_map
*map
,
5736 __isl_give isl_basic_map
*(*fn
)(__isl_take isl_basic_map
*bmap
))
5738 struct isl_basic_map
*bmap
;
5744 for (i
= map
->n
- 1; i
>= 0; --i
) {
5745 bmap
= isl_basic_map_copy(map
->p
[i
]);
5749 isl_basic_map_free(map
->p
[i
]);
5751 if (remove_if_empty(map
, i
) < 0)
5761 struct isl_map
*isl_map_fix_si(struct isl_map
*map
,
5762 enum isl_dim_type type
, unsigned pos
, int value
)
5766 map
= isl_map_cow(map
);
5770 isl_assert(map
->ctx
, pos
< isl_map_dim(map
, type
), goto error
);
5771 for (i
= map
->n
- 1; i
>= 0; --i
) {
5772 map
->p
[i
] = isl_basic_map_fix_si(map
->p
[i
], type
, pos
, value
);
5773 if (remove_if_empty(map
, i
) < 0)
5776 ISL_F_CLR(map
, ISL_MAP_NORMALIZED
);
5783 __isl_give isl_set
*isl_set_fix_si(__isl_take isl_set
*set
,
5784 enum isl_dim_type type
, unsigned pos
, int value
)
5786 return (struct isl_set
*)
5787 isl_map_fix_si((struct isl_map
*)set
, type
, pos
, value
);
5790 __isl_give isl_map
*isl_map_fix(__isl_take isl_map
*map
,
5791 enum isl_dim_type type
, unsigned pos
, isl_int value
)
5795 map
= isl_map_cow(map
);
5799 isl_assert(map
->ctx
, pos
< isl_map_dim(map
, type
), goto error
);
5800 for (i
= 0; i
< map
->n
; ++i
) {
5801 map
->p
[i
] = isl_basic_map_fix(map
->p
[i
], type
, pos
, value
);
5805 ISL_F_CLR(map
, ISL_MAP_NORMALIZED
);
5812 __isl_give isl_set
*isl_set_fix(__isl_take isl_set
*set
,
5813 enum isl_dim_type type
, unsigned pos
, isl_int value
)
5815 return (struct isl_set
*)isl_map_fix((isl_map
*)set
, type
, pos
, value
);
5818 /* Fix the value of the variable at position "pos" of type "type" of "map"
5819 * to be equal to "v".
5821 __isl_give isl_map
*isl_map_fix_val(__isl_take isl_map
*map
,
5822 enum isl_dim_type type
, unsigned pos
, __isl_take isl_val
*v
)
5826 map
= isl_map_cow(map
);
5830 if (!isl_val_is_int(v
))
5831 isl_die(isl_map_get_ctx(map
), isl_error_invalid
,
5832 "expecting integer value", goto error
);
5833 if (pos
>= isl_map_dim(map
, type
))
5834 isl_die(isl_map_get_ctx(map
), isl_error_invalid
,
5835 "index out of bounds", goto error
);
5836 for (i
= map
->n
- 1; i
>= 0; --i
) {
5837 map
->p
[i
] = isl_basic_map_fix_val(map
->p
[i
], type
, pos
,
5839 if (remove_if_empty(map
, i
) < 0)
5842 ISL_F_CLR(map
, ISL_MAP_NORMALIZED
);
5851 /* Fix the value of the variable at position "pos" of type "type" of "set"
5852 * to be equal to "v".
5854 __isl_give isl_set
*isl_set_fix_val(__isl_take isl_set
*set
,
5855 enum isl_dim_type type
, unsigned pos
, __isl_take isl_val
*v
)
5857 return isl_map_fix_val(set
, type
, pos
, v
);
5860 struct isl_map
*isl_map_fix_input_si(struct isl_map
*map
,
5861 unsigned input
, int value
)
5863 return isl_map_fix_si(map
, isl_dim_in
, input
, value
);
5866 struct isl_set
*isl_set_fix_dim_si(struct isl_set
*set
, unsigned dim
, int value
)
5868 return (struct isl_set
*)
5869 isl_map_fix_si((struct isl_map
*)set
, isl_dim_set
, dim
, value
);
5872 static __isl_give isl_basic_map
*basic_map_bound_si(
5873 __isl_take isl_basic_map
*bmap
,
5874 enum isl_dim_type type
, unsigned pos
, int value
, int upper
)
5880 isl_assert(bmap
->ctx
, pos
< isl_basic_map_dim(bmap
, type
), goto error
);
5881 pos
+= isl_basic_map_offset(bmap
, type
);
5882 bmap
= isl_basic_map_cow(bmap
);
5883 bmap
= isl_basic_map_extend_constraints(bmap
, 0, 1);
5884 j
= isl_basic_map_alloc_inequality(bmap
);
5887 isl_seq_clr(bmap
->ineq
[j
], 1 + isl_basic_map_total_dim(bmap
));
5889 isl_int_set_si(bmap
->ineq
[j
][pos
], -1);
5890 isl_int_set_si(bmap
->ineq
[j
][0], value
);
5892 isl_int_set_si(bmap
->ineq
[j
][pos
], 1);
5893 isl_int_set_si(bmap
->ineq
[j
][0], -value
);
5895 bmap
= isl_basic_map_simplify(bmap
);
5896 return isl_basic_map_finalize(bmap
);
5898 isl_basic_map_free(bmap
);
5902 __isl_give isl_basic_map
*isl_basic_map_lower_bound_si(
5903 __isl_take isl_basic_map
*bmap
,
5904 enum isl_dim_type type
, unsigned pos
, int value
)
5906 return basic_map_bound_si(bmap
, type
, pos
, value
, 0);
5909 /* Constrain the values of the given dimension to be no greater than "value".
5911 __isl_give isl_basic_map
*isl_basic_map_upper_bound_si(
5912 __isl_take isl_basic_map
*bmap
,
5913 enum isl_dim_type type
, unsigned pos
, int value
)
5915 return basic_map_bound_si(bmap
, type
, pos
, value
, 1);
5918 struct isl_basic_set
*isl_basic_set_lower_bound_dim(struct isl_basic_set
*bset
,
5919 unsigned dim
, isl_int value
)
5923 bset
= isl_basic_set_cow(bset
);
5924 bset
= isl_basic_set_extend_constraints(bset
, 0, 1);
5925 j
= isl_basic_set_alloc_inequality(bset
);
5928 isl_seq_clr(bset
->ineq
[j
], 1 + isl_basic_set_total_dim(bset
));
5929 isl_int_set_si(bset
->ineq
[j
][1 + isl_basic_set_n_param(bset
) + dim
], 1);
5930 isl_int_neg(bset
->ineq
[j
][0], value
);
5931 bset
= isl_basic_set_simplify(bset
);
5932 return isl_basic_set_finalize(bset
);
5934 isl_basic_set_free(bset
);
5938 static __isl_give isl_map
*map_bound_si(__isl_take isl_map
*map
,
5939 enum isl_dim_type type
, unsigned pos
, int value
, int upper
)
5943 map
= isl_map_cow(map
);
5947 isl_assert(map
->ctx
, pos
< isl_map_dim(map
, type
), goto error
);
5948 for (i
= 0; i
< map
->n
; ++i
) {
5949 map
->p
[i
] = basic_map_bound_si(map
->p
[i
],
5950 type
, pos
, value
, upper
);
5954 ISL_F_CLR(map
, ISL_MAP_NORMALIZED
);
5961 __isl_give isl_map
*isl_map_lower_bound_si(__isl_take isl_map
*map
,
5962 enum isl_dim_type type
, unsigned pos
, int value
)
5964 return map_bound_si(map
, type
, pos
, value
, 0);
5967 __isl_give isl_map
*isl_map_upper_bound_si(__isl_take isl_map
*map
,
5968 enum isl_dim_type type
, unsigned pos
, int value
)
5970 return map_bound_si(map
, type
, pos
, value
, 1);
5973 __isl_give isl_set
*isl_set_lower_bound_si(__isl_take isl_set
*set
,
5974 enum isl_dim_type type
, unsigned pos
, int value
)
5976 return (struct isl_set
*)
5977 isl_map_lower_bound_si((struct isl_map
*)set
, type
, pos
, value
);
5980 __isl_give isl_set
*isl_set_upper_bound_si(__isl_take isl_set
*set
,
5981 enum isl_dim_type type
, unsigned pos
, int value
)
5983 return isl_map_upper_bound_si(set
, type
, pos
, value
);
5986 /* Bound the given variable of "bmap" from below (or above is "upper"
5987 * is set) to "value".
5989 static __isl_give isl_basic_map
*basic_map_bound(
5990 __isl_take isl_basic_map
*bmap
,
5991 enum isl_dim_type type
, unsigned pos
, isl_int value
, int upper
)
5997 if (pos
>= isl_basic_map_dim(bmap
, type
))
5998 isl_die(bmap
->ctx
, isl_error_invalid
,
5999 "index out of bounds", goto error
);
6000 pos
+= isl_basic_map_offset(bmap
, type
);
6001 bmap
= isl_basic_map_cow(bmap
);
6002 bmap
= isl_basic_map_extend_constraints(bmap
, 0, 1);
6003 j
= isl_basic_map_alloc_inequality(bmap
);
6006 isl_seq_clr(bmap
->ineq
[j
], 1 + isl_basic_map_total_dim(bmap
));
6008 isl_int_set_si(bmap
->ineq
[j
][pos
], -1);
6009 isl_int_set(bmap
->ineq
[j
][0], value
);
6011 isl_int_set_si(bmap
->ineq
[j
][pos
], 1);
6012 isl_int_neg(bmap
->ineq
[j
][0], value
);
6014 bmap
= isl_basic_map_simplify(bmap
);
6015 return isl_basic_map_finalize(bmap
);
6017 isl_basic_map_free(bmap
);
6021 /* Bound the given variable of "map" from below (or above is "upper"
6022 * is set) to "value".
6024 static __isl_give isl_map
*map_bound(__isl_take isl_map
*map
,
6025 enum isl_dim_type type
, unsigned pos
, isl_int value
, int upper
)
6029 map
= isl_map_cow(map
);
6033 if (pos
>= isl_map_dim(map
, type
))
6034 isl_die(map
->ctx
, isl_error_invalid
,
6035 "index out of bounds", goto error
);
6036 for (i
= map
->n
- 1; i
>= 0; --i
) {
6037 map
->p
[i
] = basic_map_bound(map
->p
[i
], type
, pos
, value
, upper
);
6038 if (remove_if_empty(map
, i
) < 0)
6041 ISL_F_CLR(map
, ISL_MAP_NORMALIZED
);
6048 __isl_give isl_map
*isl_map_lower_bound(__isl_take isl_map
*map
,
6049 enum isl_dim_type type
, unsigned pos
, isl_int value
)
6051 return map_bound(map
, type
, pos
, value
, 0);
6054 __isl_give isl_map
*isl_map_upper_bound(__isl_take isl_map
*map
,
6055 enum isl_dim_type type
, unsigned pos
, isl_int value
)
6057 return map_bound(map
, type
, pos
, value
, 1);
6060 __isl_give isl_set
*isl_set_lower_bound(__isl_take isl_set
*set
,
6061 enum isl_dim_type type
, unsigned pos
, isl_int value
)
6063 return isl_map_lower_bound(set
, type
, pos
, value
);
6066 __isl_give isl_set
*isl_set_upper_bound(__isl_take isl_set
*set
,
6067 enum isl_dim_type type
, unsigned pos
, isl_int value
)
6069 return isl_map_upper_bound(set
, type
, pos
, value
);
6072 /* Force the values of the variable at position "pos" of type "type" of "set"
6073 * to be no smaller than "value".
6075 __isl_give isl_set
*isl_set_lower_bound_val(__isl_take isl_set
*set
,
6076 enum isl_dim_type type
, unsigned pos
, __isl_take isl_val
*value
)
6080 if (!isl_val_is_int(value
))
6081 isl_die(isl_set_get_ctx(set
), isl_error_invalid
,
6082 "expecting integer value", goto error
);
6083 set
= isl_set_lower_bound(set
, type
, pos
, value
->n
);
6084 isl_val_free(value
);
6087 isl_val_free(value
);
6092 /* Force the values of the variable at position "pos" of type "type" of "set"
6093 * to be no greater than "value".
6095 __isl_give isl_set
*isl_set_upper_bound_val(__isl_take isl_set
*set
,
6096 enum isl_dim_type type
, unsigned pos
, __isl_take isl_val
*value
)
6100 if (!isl_val_is_int(value
))
6101 isl_die(isl_set_get_ctx(set
), isl_error_invalid
,
6102 "expecting integer value", goto error
);
6103 set
= isl_set_upper_bound(set
, type
, pos
, value
->n
);
6104 isl_val_free(value
);
6107 isl_val_free(value
);
6112 struct isl_set
*isl_set_lower_bound_dim(struct isl_set
*set
, unsigned dim
,
6117 set
= isl_set_cow(set
);
6121 isl_assert(set
->ctx
, dim
< isl_set_n_dim(set
), goto error
);
6122 for (i
= 0; i
< set
->n
; ++i
) {
6123 set
->p
[i
] = isl_basic_set_lower_bound_dim(set
->p
[i
], dim
, value
);
6133 struct isl_map
*isl_map_reverse(struct isl_map
*map
)
6137 map
= isl_map_cow(map
);
6141 map
->dim
= isl_space_reverse(map
->dim
);
6144 for (i
= 0; i
< map
->n
; ++i
) {
6145 map
->p
[i
] = isl_basic_map_reverse(map
->p
[i
]);
6149 ISL_F_CLR(map
, ISL_MAP_NORMALIZED
);
6157 #define TYPE isl_pw_multi_aff
6159 #define SUFFIX _pw_multi_aff
6161 #define EMPTY isl_pw_multi_aff_empty
6163 #define ADD isl_pw_multi_aff_union_add
6164 #include "isl_map_lexopt_templ.c"
6166 /* Given a map "map", compute the lexicographically minimal
6167 * (or maximal) image element for each domain element in dom,
6168 * in the form of an isl_pw_multi_aff.
6169 * If "empty" is not NULL, then set *empty to those elements in dom that
6170 * do not have an image element.
6171 * If "flags" includes ISL_OPT_FULL, then "dom" is NULL and the optimum
6172 * should be computed over the domain of "map". "empty" is also NULL
6175 * We first compute the lexicographically minimal or maximal element
6176 * in the first basic map. This results in a partial solution "res"
6177 * and a subset "todo" of dom that still need to be handled.
6178 * We then consider each of the remaining maps in "map" and successively
6179 * update both "res" and "todo".
6180 * If "empty" is NULL, then the todo sets are not needed and therefore
6181 * also not computed.
6183 static __isl_give isl_pw_multi_aff
*isl_map_partial_lexopt_aligned_pw_multi_aff(
6184 __isl_take isl_map
*map
, __isl_take isl_set
*dom
,
6185 __isl_give isl_set
**empty
, unsigned flags
)
6189 isl_pw_multi_aff
*res
;
6192 full
= ISL_FL_ISSET(flags
, ISL_OPT_FULL
);
6193 if (!map
|| (!full
&& !dom
))
6196 if (isl_map_plain_is_empty(map
)) {
6201 return isl_pw_multi_aff_from_map(map
);
6204 res
= basic_map_partial_lexopt_pw_multi_aff(
6205 isl_basic_map_copy(map
->p
[0]),
6206 isl_set_copy(dom
), empty
, flags
);
6210 for (i
= 1; i
< map
->n
; ++i
) {
6211 isl_pw_multi_aff
*res_i
;
6213 res_i
= basic_map_partial_lexopt_pw_multi_aff(
6214 isl_basic_map_copy(map
->p
[i
]),
6215 isl_set_copy(dom
), empty
, flags
);
6217 if (ISL_FL_ISSET(flags
, ISL_OPT_MAX
))
6218 res
= isl_pw_multi_aff_union_lexmax(res
, res_i
);
6220 res
= isl_pw_multi_aff_union_lexmin(res
, res_i
);
6223 todo
= isl_set_intersect(todo
, *empty
);
6242 #define TYPE isl_map
6246 #define EMPTY isl_map_empty
6248 #define ADD isl_map_union_disjoint
6249 #include "isl_map_lexopt_templ.c"
6251 /* Given a map "map", compute the lexicographically minimal
6252 * (or maximal) image element for each domain element in "dom",
6253 * in the form of an isl_map.
6254 * If "empty" is not NULL, then set *empty to those elements in "dom" that
6255 * do not have an image element.
6256 * If "flags" includes ISL_OPT_FULL, then "dom" is NULL and the optimum
6257 * should be computed over the domain of "map". "empty" is also NULL
6260 * If the input consists of more than one disjunct, then first
6261 * compute the desired result in the form of an isl_pw_multi_aff and
6262 * then convert that into an isl_map.
6264 * This function used to have an explicit implementation in terms
6265 * of isl_maps, but it would continually intersect the domains of
6266 * partial results with the complement of the domain of the next
6267 * partial solution, potentially leading to an explosion in the number
6268 * of disjuncts if there are several disjuncts in the input.
6269 * An even earlier implementation of this function would look for
6270 * better results in the domain of the partial result and for extra
6271 * results in the complement of this domain, which would lead to
6272 * even more splintering.
6274 static __isl_give isl_map
*isl_map_partial_lexopt_aligned(
6275 __isl_take isl_map
*map
, __isl_take isl_set
*dom
,
6276 __isl_give isl_set
**empty
, unsigned flags
)
6279 struct isl_map
*res
;
6280 isl_pw_multi_aff
*pma
;
6282 full
= ISL_FL_ISSET(flags
, ISL_OPT_FULL
);
6283 if (!map
|| (!full
&& !dom
))
6286 if (isl_map_plain_is_empty(map
)) {
6295 res
= basic_map_partial_lexopt(isl_basic_map_copy(map
->p
[0]),
6301 pma
= isl_map_partial_lexopt_aligned_pw_multi_aff(map
, dom
, empty
,
6303 return isl_map_from_pw_multi_aff(pma
);
6312 __isl_give isl_map
*isl_map_partial_lexmax(
6313 __isl_take isl_map
*map
, __isl_take isl_set
*dom
,
6314 __isl_give isl_set
**empty
)
6316 return isl_map_partial_lexopt(map
, dom
, empty
, ISL_OPT_MAX
);
6319 __isl_give isl_map
*isl_map_partial_lexmin(
6320 __isl_take isl_map
*map
, __isl_take isl_set
*dom
,
6321 __isl_give isl_set
**empty
)
6323 return isl_map_partial_lexopt(map
, dom
, empty
, 0);
6326 __isl_give isl_set
*isl_set_partial_lexmin(
6327 __isl_take isl_set
*set
, __isl_take isl_set
*dom
,
6328 __isl_give isl_set
**empty
)
6330 return (struct isl_set
*)
6331 isl_map_partial_lexmin((struct isl_map
*)set
,
6335 __isl_give isl_set
*isl_set_partial_lexmax(
6336 __isl_take isl_set
*set
, __isl_take isl_set
*dom
,
6337 __isl_give isl_set
**empty
)
6339 return (struct isl_set
*)
6340 isl_map_partial_lexmax((struct isl_map
*)set
,
6344 /* Compute the lexicographic minimum (or maximum if "flags" includes
6345 * ISL_OPT_MAX) of "bset" over its parametric domain.
6347 __isl_give isl_set
*isl_basic_set_lexopt(__isl_take isl_basic_set
*bset
,
6350 return isl_basic_map_lexopt(bset
, flags
);
6353 __isl_give isl_map
*isl_basic_map_lexmax(__isl_take isl_basic_map
*bmap
)
6355 return isl_basic_map_lexopt(bmap
, ISL_OPT_MAX
);
6358 __isl_give isl_set
*isl_basic_set_lexmin(__isl_take isl_basic_set
*bset
)
6360 return (isl_set
*)isl_basic_map_lexmin((isl_basic_map
*)bset
);
6363 __isl_give isl_set
*isl_basic_set_lexmax(__isl_take isl_basic_set
*bset
)
6365 return (isl_set
*)isl_basic_map_lexmax((isl_basic_map
*)bset
);
6368 /* Compute the lexicographic minimum of "bset" over its parametric domain
6369 * for the purpose of quantifier elimination.
6370 * That is, find an explicit representation for all the existentially
6371 * quantified variables in "bset" by computing their lexicographic
6374 static __isl_give isl_set
*isl_basic_set_lexmin_compute_divs(
6375 __isl_take isl_basic_set
*bset
)
6377 return isl_basic_set_lexopt(bset
, ISL_OPT_QE
);
6380 /* Extract the first and only affine expression from list
6381 * and then add it to *pwaff with the given dom.
6382 * This domain is known to be disjoint from other domains
6383 * because of the way isl_basic_map_foreach_lexmax works.
6385 static int update_dim_opt(__isl_take isl_basic_set
*dom
,
6386 __isl_take isl_aff_list
*list
, void *user
)
6388 isl_ctx
*ctx
= isl_basic_set_get_ctx(dom
);
6390 isl_pw_aff
**pwaff
= user
;
6391 isl_pw_aff
*pwaff_i
;
6395 if (isl_aff_list_n_aff(list
) != 1)
6396 isl_die(ctx
, isl_error_internal
,
6397 "expecting single element list", goto error
);
6399 aff
= isl_aff_list_get_aff(list
, 0);
6400 pwaff_i
= isl_pw_aff_alloc(isl_set_from_basic_set(dom
), aff
);
6402 *pwaff
= isl_pw_aff_add_disjoint(*pwaff
, pwaff_i
);
6404 isl_aff_list_free(list
);
6408 isl_basic_set_free(dom
);
6409 isl_aff_list_free(list
);
6413 /* Given a basic map with one output dimension, compute the minimum or
6414 * maximum of that dimension as an isl_pw_aff.
6416 * The isl_pw_aff is constructed by having isl_basic_map_foreach_lexopt
6417 * call update_dim_opt on each leaf of the result.
6419 static __isl_give isl_pw_aff
*basic_map_dim_opt(__isl_keep isl_basic_map
*bmap
,
6422 isl_space
*dim
= isl_basic_map_get_space(bmap
);
6426 dim
= isl_space_from_domain(isl_space_domain(dim
));
6427 dim
= isl_space_add_dims(dim
, isl_dim_out
, 1);
6428 pwaff
= isl_pw_aff_empty(dim
);
6430 r
= isl_basic_map_foreach_lexopt(bmap
, max
, &update_dim_opt
, &pwaff
);
6432 return isl_pw_aff_free(pwaff
);
6437 /* Compute the minimum or maximum of the given output dimension
6438 * as a function of the parameters and the input dimensions,
6439 * but independently of the other output dimensions.
6441 * We first project out the other output dimension and then compute
6442 * the "lexicographic" maximum in each basic map, combining the results
6443 * using isl_pw_aff_union_max.
6445 static __isl_give isl_pw_aff
*map_dim_opt(__isl_take isl_map
*map
, int pos
,
6452 n_out
= isl_map_dim(map
, isl_dim_out
);
6453 map
= isl_map_project_out(map
, isl_dim_out
, pos
+ 1, n_out
- (pos
+ 1));
6454 map
= isl_map_project_out(map
, isl_dim_out
, 0, pos
);
6459 isl_space
*dim
= isl_map_get_space(map
);
6461 return isl_pw_aff_empty(dim
);
6464 pwaff
= basic_map_dim_opt(map
->p
[0], max
);
6465 for (i
= 1; i
< map
->n
; ++i
) {
6466 isl_pw_aff
*pwaff_i
;
6468 pwaff_i
= basic_map_dim_opt(map
->p
[i
], max
);
6469 pwaff
= isl_pw_aff_union_opt(pwaff
, pwaff_i
, max
);
6477 /* Compute the minimum of the given output dimension as a function of the
6478 * parameters and input dimensions, but independently of
6479 * the other output dimensions.
6481 __isl_give isl_pw_aff
*isl_map_dim_min(__isl_take isl_map
*map
, int pos
)
6483 return map_dim_opt(map
, pos
, 0);
6486 /* Compute the maximum of the given output dimension as a function of the
6487 * parameters and input dimensions, but independently of
6488 * the other output dimensions.
6490 __isl_give isl_pw_aff
*isl_map_dim_max(__isl_take isl_map
*map
, int pos
)
6492 return map_dim_opt(map
, pos
, 1);
6495 /* Compute the minimum or maximum of the given set dimension
6496 * as a function of the parameters,
6497 * but independently of the other set dimensions.
6499 static __isl_give isl_pw_aff
*set_dim_opt(__isl_take isl_set
*set
, int pos
,
6502 return map_dim_opt(set
, pos
, max
);
6505 /* Compute the maximum of the given set dimension as a function of the
6506 * parameters, but independently of the other set dimensions.
6508 __isl_give isl_pw_aff
*isl_set_dim_max(__isl_take isl_set
*set
, int pos
)
6510 return set_dim_opt(set
, pos
, 1);
6513 /* Compute the minimum of the given set dimension as a function of the
6514 * parameters, but independently of the other set dimensions.
6516 __isl_give isl_pw_aff
*isl_set_dim_min(__isl_take isl_set
*set
, int pos
)
6518 return set_dim_opt(set
, pos
, 0);
6521 /* Apply a preimage specified by "mat" on the parameters of "bset".
6522 * bset is assumed to have only parameters and divs.
6524 static struct isl_basic_set
*basic_set_parameter_preimage(
6525 struct isl_basic_set
*bset
, struct isl_mat
*mat
)
6532 bset
->dim
= isl_space_cow(bset
->dim
);
6536 nparam
= isl_basic_set_dim(bset
, isl_dim_param
);
6538 isl_assert(bset
->ctx
, mat
->n_row
== 1 + nparam
, goto error
);
6540 bset
->dim
->nparam
= 0;
6541 bset
->dim
->n_out
= nparam
;
6542 bset
= isl_basic_set_preimage(bset
, mat
);
6544 bset
->dim
->nparam
= bset
->dim
->n_out
;
6545 bset
->dim
->n_out
= 0;
6550 isl_basic_set_free(bset
);
6554 /* Apply a preimage specified by "mat" on the parameters of "set".
6555 * set is assumed to have only parameters and divs.
6557 static __isl_give isl_set
*set_parameter_preimage(__isl_take isl_set
*set
,
6558 __isl_take isl_mat
*mat
)
6566 nparam
= isl_set_dim(set
, isl_dim_param
);
6568 if (mat
->n_row
!= 1 + nparam
)
6569 isl_die(isl_set_get_ctx(set
), isl_error_internal
,
6570 "unexpected number of rows", goto error
);
6572 space
= isl_set_get_space(set
);
6573 space
= isl_space_move_dims(space
, isl_dim_set
, 0,
6574 isl_dim_param
, 0, nparam
);
6575 set
= isl_set_reset_space(set
, space
);
6576 set
= isl_set_preimage(set
, mat
);
6577 nparam
= isl_set_dim(set
, isl_dim_out
);
6578 space
= isl_set_get_space(set
);
6579 space
= isl_space_move_dims(space
, isl_dim_param
, 0,
6580 isl_dim_out
, 0, nparam
);
6581 set
= isl_set_reset_space(set
, space
);
6589 /* Intersect the basic set "bset" with the affine space specified by the
6590 * equalities in "eq".
6592 static struct isl_basic_set
*basic_set_append_equalities(
6593 struct isl_basic_set
*bset
, struct isl_mat
*eq
)
6601 bset
= isl_basic_set_extend_space(bset
, isl_space_copy(bset
->dim
), 0,
6606 len
= 1 + isl_space_dim(bset
->dim
, isl_dim_all
) + bset
->extra
;
6607 for (i
= 0; i
< eq
->n_row
; ++i
) {
6608 k
= isl_basic_set_alloc_equality(bset
);
6611 isl_seq_cpy(bset
->eq
[k
], eq
->row
[i
], eq
->n_col
);
6612 isl_seq_clr(bset
->eq
[k
] + eq
->n_col
, len
- eq
->n_col
);
6616 bset
= isl_basic_set_gauss(bset
, NULL
);
6617 bset
= isl_basic_set_finalize(bset
);
6622 isl_basic_set_free(bset
);
6626 /* Intersect the set "set" with the affine space specified by the
6627 * equalities in "eq".
6629 static struct isl_set
*set_append_equalities(struct isl_set
*set
,
6637 for (i
= 0; i
< set
->n
; ++i
) {
6638 set
->p
[i
] = basic_set_append_equalities(set
->p
[i
],
6651 /* Given a basic set "bset" that only involves parameters and existentially
6652 * quantified variables, return the index of the first equality
6653 * that only involves parameters. If there is no such equality then
6654 * return bset->n_eq.
6656 * This function assumes that isl_basic_set_gauss has been called on "bset".
6658 static int first_parameter_equality(__isl_keep isl_basic_set
*bset
)
6661 unsigned nparam
, n_div
;
6666 nparam
= isl_basic_set_dim(bset
, isl_dim_param
);
6667 n_div
= isl_basic_set_dim(bset
, isl_dim_div
);
6669 for (i
= 0, j
= n_div
- 1; i
< bset
->n_eq
&& j
>= 0; --j
) {
6670 if (!isl_int_is_zero(bset
->eq
[i
][1 + nparam
+ j
]))
6677 /* Compute an explicit representation for the existentially quantified
6678 * variables in "bset" by computing the "minimal value" of the set
6679 * variables. Since there are no set variables, the computation of
6680 * the minimal value essentially computes an explicit representation
6681 * of the non-empty part(s) of "bset".
6683 * The input only involves parameters and existentially quantified variables.
6684 * All equalities among parameters have been removed.
6686 * Since the existentially quantified variables in the result are in general
6687 * going to be different from those in the input, we first replace
6688 * them by the minimal number of variables based on their equalities.
6689 * This should simplify the parametric integer programming.
6691 static __isl_give isl_set
*base_compute_divs(__isl_take isl_basic_set
*bset
)
6693 isl_morph
*morph1
, *morph2
;
6699 if (bset
->n_eq
== 0)
6700 return isl_basic_set_lexmin_compute_divs(bset
);
6702 morph1
= isl_basic_set_parameter_compression(bset
);
6703 bset
= isl_morph_basic_set(isl_morph_copy(morph1
), bset
);
6704 bset
= isl_basic_set_lift(bset
);
6705 morph2
= isl_basic_set_variable_compression(bset
, isl_dim_set
);
6706 bset
= isl_morph_basic_set(morph2
, bset
);
6707 n
= isl_basic_set_dim(bset
, isl_dim_set
);
6708 bset
= isl_basic_set_project_out(bset
, isl_dim_set
, 0, n
);
6710 set
= isl_basic_set_lexmin_compute_divs(bset
);
6712 set
= isl_morph_set(isl_morph_inverse(morph1
), set
);
6717 /* Project the given basic set onto its parameter domain, possibly introducing
6718 * new, explicit, existential variables in the constraints.
6719 * The input has parameters and (possibly implicit) existential variables.
6720 * The output has the same parameters, but only
6721 * explicit existentially quantified variables.
6723 * The actual projection is performed by pip, but pip doesn't seem
6724 * to like equalities very much, so we first remove the equalities
6725 * among the parameters by performing a variable compression on
6726 * the parameters. Afterward, an inverse transformation is performed
6727 * and the equalities among the parameters are inserted back in.
6729 * The variable compression on the parameters may uncover additional
6730 * equalities that were only implicit before. We therefore check
6731 * if there are any new parameter equalities in the result and
6732 * if so recurse. The removal of parameter equalities is required
6733 * for the parameter compression performed by base_compute_divs.
6735 static struct isl_set
*parameter_compute_divs(struct isl_basic_set
*bset
)
6739 struct isl_mat
*T
, *T2
;
6740 struct isl_set
*set
;
6743 bset
= isl_basic_set_cow(bset
);
6747 if (bset
->n_eq
== 0)
6748 return base_compute_divs(bset
);
6750 bset
= isl_basic_set_gauss(bset
, NULL
);
6753 if (isl_basic_set_plain_is_empty(bset
))
6754 return isl_set_from_basic_set(bset
);
6756 i
= first_parameter_equality(bset
);
6757 if (i
== bset
->n_eq
)
6758 return base_compute_divs(bset
);
6760 nparam
= isl_basic_set_dim(bset
, isl_dim_param
);
6761 eq
= isl_mat_sub_alloc6(bset
->ctx
, bset
->eq
, i
, bset
->n_eq
- i
,
6763 eq
= isl_mat_cow(eq
);
6764 T
= isl_mat_variable_compression(isl_mat_copy(eq
), &T2
);
6765 if (T
&& T
->n_col
== 0) {
6769 bset
= isl_basic_set_set_to_empty(bset
);
6770 return isl_set_from_basic_set(bset
);
6772 bset
= basic_set_parameter_preimage(bset
, T
);
6774 i
= first_parameter_equality(bset
);
6777 else if (i
== bset
->n_eq
)
6778 set
= base_compute_divs(bset
);
6780 set
= parameter_compute_divs(bset
);
6781 set
= set_parameter_preimage(set
, T2
);
6782 set
= set_append_equalities(set
, eq
);
6786 /* Insert the divs from "ls" before those of "bmap".
6788 * The number of columns is not changed, which means that the last
6789 * dimensions of "bmap" are being reintepreted as the divs from "ls".
6790 * The caller is responsible for removing the same number of dimensions
6791 * from the space of "bmap".
6793 static __isl_give isl_basic_map
*insert_divs_from_local_space(
6794 __isl_take isl_basic_map
*bmap
, __isl_keep isl_local_space
*ls
)
6800 n_div
= isl_local_space_dim(ls
, isl_dim_div
);
6804 old_n_div
= bmap
->n_div
;
6805 bmap
= insert_div_rows(bmap
, n_div
);
6809 for (i
= 0; i
< n_div
; ++i
) {
6810 isl_seq_cpy(bmap
->div
[i
], ls
->div
->row
[i
], ls
->div
->n_col
);
6811 isl_seq_clr(bmap
->div
[i
] + ls
->div
->n_col
, old_n_div
);
6817 /* Replace the space of "bmap" by the space and divs of "ls".
6819 * If "ls" has any divs, then we simplify the result since we may
6820 * have discovered some additional equalities that could simplify
6821 * the div expressions.
6823 static __isl_give isl_basic_map
*basic_replace_space_by_local_space(
6824 __isl_take isl_basic_map
*bmap
, __isl_take isl_local_space
*ls
)
6828 bmap
= isl_basic_map_cow(bmap
);
6832 n_div
= isl_local_space_dim(ls
, isl_dim_div
);
6833 bmap
= insert_divs_from_local_space(bmap
, ls
);
6837 isl_space_free(bmap
->dim
);
6838 bmap
->dim
= isl_local_space_get_space(ls
);
6842 isl_local_space_free(ls
);
6844 bmap
= isl_basic_map_simplify(bmap
);
6845 bmap
= isl_basic_map_finalize(bmap
);
6848 isl_basic_map_free(bmap
);
6849 isl_local_space_free(ls
);
6853 /* Replace the space of "map" by the space and divs of "ls".
6855 static __isl_give isl_map
*replace_space_by_local_space(__isl_take isl_map
*map
,
6856 __isl_take isl_local_space
*ls
)
6860 map
= isl_map_cow(map
);
6864 for (i
= 0; i
< map
->n
; ++i
) {
6865 map
->p
[i
] = basic_replace_space_by_local_space(map
->p
[i
],
6866 isl_local_space_copy(ls
));
6870 isl_space_free(map
->dim
);
6871 map
->dim
= isl_local_space_get_space(ls
);
6875 isl_local_space_free(ls
);
6878 isl_local_space_free(ls
);
6883 /* Compute an explicit representation for the existentially
6884 * quantified variables for which do not know any explicit representation yet.
6886 * We first sort the existentially quantified variables so that the
6887 * existentially quantified variables for which we already have an explicit
6888 * representation are placed before those for which we do not.
6889 * The input dimensions, the output dimensions and the existentially
6890 * quantified variables for which we already have an explicit
6891 * representation are then turned into parameters.
6892 * compute_divs returns a map with the same parameters and
6893 * no input or output dimensions and the dimension specification
6894 * is reset to that of the input, including the existentially quantified
6895 * variables for which we already had an explicit representation.
6897 static struct isl_map
*compute_divs(struct isl_basic_map
*bmap
)
6899 struct isl_basic_set
*bset
;
6900 struct isl_set
*set
;
6901 struct isl_map
*map
;
6903 isl_local_space
*ls
;
6910 bmap
= isl_basic_map_sort_divs(bmap
);
6911 bmap
= isl_basic_map_cow(bmap
);
6915 n_known
= isl_basic_map_first_unknown_div(bmap
);
6917 return isl_map_from_basic_map(isl_basic_map_free(bmap
));
6919 nparam
= isl_basic_map_dim(bmap
, isl_dim_param
);
6920 n_in
= isl_basic_map_dim(bmap
, isl_dim_in
);
6921 n_out
= isl_basic_map_dim(bmap
, isl_dim_out
);
6922 dim
= isl_space_set_alloc(bmap
->ctx
,
6923 nparam
+ n_in
+ n_out
+ n_known
, 0);
6927 ls
= isl_basic_map_get_local_space(bmap
);
6928 ls
= isl_local_space_drop_dims(ls
, isl_dim_div
,
6929 n_known
, bmap
->n_div
- n_known
);
6931 for (i
= n_known
; i
< bmap
->n_div
; ++i
)
6932 swap_div(bmap
, i
- n_known
, i
);
6933 bmap
->n_div
-= n_known
;
6934 bmap
->extra
-= n_known
;
6936 bmap
= isl_basic_map_reset_space(bmap
, dim
);
6937 bset
= (struct isl_basic_set
*)bmap
;
6939 set
= parameter_compute_divs(bset
);
6940 map
= (struct isl_map
*)set
;
6941 map
= replace_space_by_local_space(map
, ls
);
6945 isl_basic_map_free(bmap
);
6949 /* Remove the explicit representation of local variable "div",
6952 __isl_give isl_basic_map
*isl_basic_map_mark_div_unknown(
6953 __isl_take isl_basic_map
*bmap
, int div
)
6957 unknown
= isl_basic_map_div_is_marked_unknown(bmap
, div
);
6959 return isl_basic_map_free(bmap
);
6963 bmap
= isl_basic_map_cow(bmap
);
6966 isl_int_set_si(bmap
->div
[div
][0], 0);
6970 /* Is local variable "div" of "bmap" marked as not having an explicit
6972 * Note that even if "div" is not marked in this way and therefore
6973 * has an explicit representation, this representation may still
6974 * depend (indirectly) on other local variables that do not
6975 * have an explicit representation.
6977 isl_bool
isl_basic_map_div_is_marked_unknown(__isl_keep isl_basic_map
*bmap
,
6981 return isl_bool_error
;
6982 if (div
< 0 || div
>= isl_basic_map_dim(bmap
, isl_dim_div
))
6983 isl_die(isl_basic_map_get_ctx(bmap
), isl_error_invalid
,
6984 "position out of bounds", return isl_bool_error
);
6985 return isl_int_is_zero(bmap
->div
[div
][0]);
6988 /* Return the position of the first local variable that does not
6989 * have an explicit representation.
6990 * Return the total number of local variables if they all have
6991 * an explicit representation.
6992 * Return -1 on error.
6994 int isl_basic_map_first_unknown_div(__isl_keep isl_basic_map
*bmap
)
7001 for (i
= 0; i
< bmap
->n_div
; ++i
) {
7002 if (!isl_basic_map_div_is_known(bmap
, i
))
7008 /* Return the position of the first local variable that does not
7009 * have an explicit representation.
7010 * Return the total number of local variables if they all have
7011 * an explicit representation.
7012 * Return -1 on error.
7014 int isl_basic_set_first_unknown_div(__isl_keep isl_basic_set
*bset
)
7016 return isl_basic_map_first_unknown_div(bset
);
7019 /* Does "bmap" have an explicit representation for all local variables?
7021 isl_bool
isl_basic_map_divs_known(__isl_keep isl_basic_map
*bmap
)
7025 n
= isl_basic_map_dim(bmap
, isl_dim_div
);
7026 first
= isl_basic_map_first_unknown_div(bmap
);
7028 return isl_bool_error
;
7032 /* Do all basic maps in "map" have an explicit representation
7033 * for all local variables?
7035 isl_bool
isl_map_divs_known(__isl_keep isl_map
*map
)
7040 return isl_bool_error
;
7042 for (i
= 0; i
< map
->n
; ++i
) {
7043 int known
= isl_basic_map_divs_known(map
->p
[i
]);
7048 return isl_bool_true
;
7051 /* If bmap contains any unknown divs, then compute explicit
7052 * expressions for them. However, this computation may be
7053 * quite expensive, so first try to remove divs that aren't
7056 struct isl_map
*isl_basic_map_compute_divs(struct isl_basic_map
*bmap
)
7059 struct isl_map
*map
;
7061 known
= isl_basic_map_divs_known(bmap
);
7065 return isl_map_from_basic_map(bmap
);
7067 bmap
= isl_basic_map_drop_redundant_divs(bmap
);
7069 known
= isl_basic_map_divs_known(bmap
);
7073 return isl_map_from_basic_map(bmap
);
7075 map
= compute_divs(bmap
);
7078 isl_basic_map_free(bmap
);
7082 struct isl_map
*isl_map_compute_divs(struct isl_map
*map
)
7086 struct isl_map
*res
;
7093 known
= isl_map_divs_known(map
);
7101 res
= isl_basic_map_compute_divs(isl_basic_map_copy(map
->p
[0]));
7102 for (i
= 1 ; i
< map
->n
; ++i
) {
7104 r2
= isl_basic_map_compute_divs(isl_basic_map_copy(map
->p
[i
]));
7105 if (ISL_F_ISSET(map
, ISL_MAP_DISJOINT
))
7106 res
= isl_map_union_disjoint(res
, r2
);
7108 res
= isl_map_union(res
, r2
);
7115 struct isl_set
*isl_basic_set_compute_divs(struct isl_basic_set
*bset
)
7117 return (struct isl_set
*)
7118 isl_basic_map_compute_divs((struct isl_basic_map
*)bset
);
7121 struct isl_set
*isl_set_compute_divs(struct isl_set
*set
)
7123 return (struct isl_set
*)
7124 isl_map_compute_divs((struct isl_map
*)set
);
7127 struct isl_set
*isl_map_domain(struct isl_map
*map
)
7130 struct isl_set
*set
;
7135 map
= isl_map_cow(map
);
7139 set
= (struct isl_set
*)map
;
7140 set
->dim
= isl_space_domain(set
->dim
);
7143 for (i
= 0; i
< map
->n
; ++i
) {
7144 set
->p
[i
] = isl_basic_map_domain(map
->p
[i
]);
7148 ISL_F_CLR(set
, ISL_MAP_DISJOINT
);
7149 ISL_F_CLR(set
, ISL_SET_NORMALIZED
);
7156 /* Return the union of "map1" and "map2", where we assume for now that
7157 * "map1" and "map2" are disjoint. Note that the basic maps inside
7158 * "map1" or "map2" may not be disjoint from each other.
7159 * Also note that this function is also called from isl_map_union,
7160 * which takes care of handling the situation where "map1" and "map2"
7161 * may not be disjoint.
7163 * If one of the inputs is empty, we can simply return the other input.
7164 * Similarly, if one of the inputs is universal, then it is equal to the union.
7166 static __isl_give isl_map
*map_union_disjoint(__isl_take isl_map
*map1
,
7167 __isl_take isl_map
*map2
)
7171 struct isl_map
*map
= NULL
;
7177 if (!isl_space_is_equal(map1
->dim
, map2
->dim
))
7178 isl_die(isl_map_get_ctx(map1
), isl_error_invalid
,
7179 "spaces don't match", goto error
);
7190 is_universe
= isl_map_plain_is_universe(map1
);
7191 if (is_universe
< 0)
7198 is_universe
= isl_map_plain_is_universe(map2
);
7199 if (is_universe
< 0)
7206 if (ISL_F_ISSET(map1
, ISL_MAP_DISJOINT
) &&
7207 ISL_F_ISSET(map2
, ISL_MAP_DISJOINT
))
7208 ISL_FL_SET(flags
, ISL_MAP_DISJOINT
);
7210 map
= isl_map_alloc_space(isl_space_copy(map1
->dim
),
7211 map1
->n
+ map2
->n
, flags
);
7214 for (i
= 0; i
< map1
->n
; ++i
) {
7215 map
= isl_map_add_basic_map(map
,
7216 isl_basic_map_copy(map1
->p
[i
]));
7220 for (i
= 0; i
< map2
->n
; ++i
) {
7221 map
= isl_map_add_basic_map(map
,
7222 isl_basic_map_copy(map2
->p
[i
]));
7236 /* Return the union of "map1" and "map2", where "map1" and "map2" are
7237 * guaranteed to be disjoint by the caller.
7239 * Note that this functions is called from within isl_map_make_disjoint,
7240 * so we have to be careful not to touch the constraints of the inputs
7243 __isl_give isl_map
*isl_map_union_disjoint(__isl_take isl_map
*map1
,
7244 __isl_take isl_map
*map2
)
7246 return isl_map_align_params_map_map_and(map1
, map2
, &map_union_disjoint
);
7249 /* Return the union of "map1" and "map2", where "map1" and "map2" may
7250 * not be disjoint. The parameters are assumed to have been aligned.
7252 * We currently simply call map_union_disjoint, the internal operation
7253 * of which does not really depend on the inputs being disjoint.
7254 * If the result contains more than one basic map, then we clear
7255 * the disjoint flag since the result may contain basic maps from
7256 * both inputs and these are not guaranteed to be disjoint.
7258 * As a special case, if "map1" and "map2" are obviously equal,
7259 * then we simply return "map1".
7261 static __isl_give isl_map
*map_union_aligned(__isl_take isl_map
*map1
,
7262 __isl_take isl_map
*map2
)
7269 equal
= isl_map_plain_is_equal(map1
, map2
);
7277 map1
= map_union_disjoint(map1
, map2
);
7281 ISL_F_CLR(map1
, ISL_MAP_DISJOINT
);
7289 /* Return the union of "map1" and "map2", where "map1" and "map2" may
7292 __isl_give isl_map
*isl_map_union(__isl_take isl_map
*map1
,
7293 __isl_take isl_map
*map2
)
7295 return isl_map_align_params_map_map_and(map1
, map2
, &map_union_aligned
);
7298 struct isl_set
*isl_set_union_disjoint(
7299 struct isl_set
*set1
, struct isl_set
*set2
)
7301 return (struct isl_set
*)
7302 isl_map_union_disjoint(
7303 (struct isl_map
*)set1
, (struct isl_map
*)set2
);
7306 struct isl_set
*isl_set_union(struct isl_set
*set1
, struct isl_set
*set2
)
7308 return (struct isl_set
*)
7309 isl_map_union((struct isl_map
*)set1
, (struct isl_map
*)set2
);
7312 /* Apply "fn" to pairs of elements from "map" and "set" and collect
7315 * "map" and "set" are assumed to be compatible and non-NULL.
7317 static __isl_give isl_map
*map_intersect_set(__isl_take isl_map
*map
,
7318 __isl_take isl_set
*set
,
7319 __isl_give isl_basic_map
*fn(__isl_take isl_basic_map
*bmap
,
7320 __isl_take isl_basic_set
*bset
))
7323 struct isl_map
*result
;
7326 if (isl_set_plain_is_universe(set
)) {
7331 if (ISL_F_ISSET(map
, ISL_MAP_DISJOINT
) &&
7332 ISL_F_ISSET(set
, ISL_MAP_DISJOINT
))
7333 ISL_FL_SET(flags
, ISL_MAP_DISJOINT
);
7335 result
= isl_map_alloc_space(isl_space_copy(map
->dim
),
7336 map
->n
* set
->n
, flags
);
7337 for (i
= 0; result
&& i
< map
->n
; ++i
)
7338 for (j
= 0; j
< set
->n
; ++j
) {
7339 result
= isl_map_add_basic_map(result
,
7340 fn(isl_basic_map_copy(map
->p
[i
]),
7341 isl_basic_set_copy(set
->p
[j
])));
7351 static __isl_give isl_map
*map_intersect_range(__isl_take isl_map
*map
,
7352 __isl_take isl_set
*set
)
7357 if (!isl_map_compatible_range(map
, set
))
7358 isl_die(set
->ctx
, isl_error_invalid
,
7359 "incompatible spaces", goto error
);
7361 return map_intersect_set(map
, set
, &isl_basic_map_intersect_range
);
7368 __isl_give isl_map
*isl_map_intersect_range(__isl_take isl_map
*map
,
7369 __isl_take isl_set
*set
)
7371 return isl_map_align_params_map_map_and(map
, set
, &map_intersect_range
);
7374 static __isl_give isl_map
*map_intersect_domain(__isl_take isl_map
*map
,
7375 __isl_take isl_set
*set
)
7380 if (!isl_map_compatible_domain(map
, set
))
7381 isl_die(set
->ctx
, isl_error_invalid
,
7382 "incompatible spaces", goto error
);
7384 return map_intersect_set(map
, set
, &isl_basic_map_intersect_domain
);
7391 __isl_give isl_map
*isl_map_intersect_domain(__isl_take isl_map
*map
,
7392 __isl_take isl_set
*set
)
7394 return isl_map_align_params_map_map_and(map
, set
,
7395 &map_intersect_domain
);
7398 static __isl_give isl_map
*map_apply_domain(__isl_take isl_map
*map1
,
7399 __isl_take isl_map
*map2
)
7403 map1
= isl_map_reverse(map1
);
7404 map1
= isl_map_apply_range(map1
, map2
);
7405 return isl_map_reverse(map1
);
7412 __isl_give isl_map
*isl_map_apply_domain(__isl_take isl_map
*map1
,
7413 __isl_take isl_map
*map2
)
7415 return isl_map_align_params_map_map_and(map1
, map2
, &map_apply_domain
);
7418 static __isl_give isl_map
*map_apply_range(__isl_take isl_map
*map1
,
7419 __isl_take isl_map
*map2
)
7421 isl_space
*dim_result
;
7422 struct isl_map
*result
;
7428 dim_result
= isl_space_join(isl_space_copy(map1
->dim
),
7429 isl_space_copy(map2
->dim
));
7431 result
= isl_map_alloc_space(dim_result
, map1
->n
* map2
->n
, 0);
7434 for (i
= 0; i
< map1
->n
; ++i
)
7435 for (j
= 0; j
< map2
->n
; ++j
) {
7436 result
= isl_map_add_basic_map(result
,
7437 isl_basic_map_apply_range(
7438 isl_basic_map_copy(map1
->p
[i
]),
7439 isl_basic_map_copy(map2
->p
[j
])));
7445 if (result
&& result
->n
<= 1)
7446 ISL_F_SET(result
, ISL_MAP_DISJOINT
);
7454 __isl_give isl_map
*isl_map_apply_range(__isl_take isl_map
*map1
,
7455 __isl_take isl_map
*map2
)
7457 return isl_map_align_params_map_map_and(map1
, map2
, &map_apply_range
);
7461 * returns range - domain
7463 struct isl_basic_set
*isl_basic_map_deltas(struct isl_basic_map
*bmap
)
7465 isl_space
*target_space
;
7466 struct isl_basic_set
*bset
;
7473 isl_assert(bmap
->ctx
, isl_space_tuple_is_equal(bmap
->dim
, isl_dim_in
,
7474 bmap
->dim
, isl_dim_out
),
7476 target_space
= isl_space_domain(isl_basic_map_get_space(bmap
));
7477 dim
= isl_basic_map_n_in(bmap
);
7478 nparam
= isl_basic_map_n_param(bmap
);
7479 bmap
= isl_basic_map_from_range(isl_basic_map_wrap(bmap
));
7480 bmap
= isl_basic_map_add_dims(bmap
, isl_dim_in
, dim
);
7481 bmap
= isl_basic_map_extend_constraints(bmap
, dim
, 0);
7482 for (i
= 0; i
< dim
; ++i
) {
7483 int j
= isl_basic_map_alloc_equality(bmap
);
7485 bmap
= isl_basic_map_free(bmap
);
7488 isl_seq_clr(bmap
->eq
[j
], 1 + isl_basic_map_total_dim(bmap
));
7489 isl_int_set_si(bmap
->eq
[j
][1+nparam
+i
], 1);
7490 isl_int_set_si(bmap
->eq
[j
][1+nparam
+dim
+i
], 1);
7491 isl_int_set_si(bmap
->eq
[j
][1+nparam
+2*dim
+i
], -1);
7493 bset
= isl_basic_map_domain(bmap
);
7494 bset
= isl_basic_set_reset_space(bset
, target_space
);
7497 isl_basic_map_free(bmap
);
7502 * returns range - domain
7504 __isl_give isl_set
*isl_map_deltas(__isl_take isl_map
*map
)
7508 struct isl_set
*result
;
7513 isl_assert(map
->ctx
, isl_space_tuple_is_equal(map
->dim
, isl_dim_in
,
7514 map
->dim
, isl_dim_out
),
7516 dim
= isl_map_get_space(map
);
7517 dim
= isl_space_domain(dim
);
7518 result
= isl_set_alloc_space(dim
, map
->n
, 0);
7521 for (i
= 0; i
< map
->n
; ++i
)
7522 result
= isl_set_add_basic_set(result
,
7523 isl_basic_map_deltas(isl_basic_map_copy(map
->p
[i
])));
7532 * returns [domain -> range] -> range - domain
7534 __isl_give isl_basic_map
*isl_basic_map_deltas_map(
7535 __isl_take isl_basic_map
*bmap
)
7539 isl_basic_map
*domain
;
7543 if (!isl_space_tuple_is_equal(bmap
->dim
, isl_dim_in
,
7544 bmap
->dim
, isl_dim_out
))
7545 isl_die(bmap
->ctx
, isl_error_invalid
,
7546 "domain and range don't match", goto error
);
7548 nparam
= isl_basic_map_dim(bmap
, isl_dim_param
);
7549 n
= isl_basic_map_dim(bmap
, isl_dim_in
);
7551 dim
= isl_space_from_range(isl_space_domain(isl_basic_map_get_space(bmap
)));
7552 domain
= isl_basic_map_universe(dim
);
7554 bmap
= isl_basic_map_from_domain(isl_basic_map_wrap(bmap
));
7555 bmap
= isl_basic_map_apply_range(bmap
, domain
);
7556 bmap
= isl_basic_map_extend_constraints(bmap
, n
, 0);
7558 total
= isl_basic_map_total_dim(bmap
);
7560 for (i
= 0; i
< n
; ++i
) {
7561 k
= isl_basic_map_alloc_equality(bmap
);
7564 isl_seq_clr(bmap
->eq
[k
], 1 + total
);
7565 isl_int_set_si(bmap
->eq
[k
][1 + nparam
+ i
], 1);
7566 isl_int_set_si(bmap
->eq
[k
][1 + nparam
+ n
+ i
], -1);
7567 isl_int_set_si(bmap
->eq
[k
][1 + nparam
+ n
+ n
+ i
], 1);
7570 bmap
= isl_basic_map_gauss(bmap
, NULL
);
7571 return isl_basic_map_finalize(bmap
);
7573 isl_basic_map_free(bmap
);
7578 * returns [domain -> range] -> range - domain
7580 __isl_give isl_map
*isl_map_deltas_map(__isl_take isl_map
*map
)
7583 isl_space
*domain_dim
;
7588 if (!isl_space_tuple_is_equal(map
->dim
, isl_dim_in
,
7589 map
->dim
, isl_dim_out
))
7590 isl_die(map
->ctx
, isl_error_invalid
,
7591 "domain and range don't match", goto error
);
7593 map
= isl_map_cow(map
);
7597 domain_dim
= isl_space_from_range(isl_space_domain(isl_map_get_space(map
)));
7598 map
->dim
= isl_space_from_domain(isl_space_wrap(map
->dim
));
7599 map
->dim
= isl_space_join(map
->dim
, domain_dim
);
7602 for (i
= 0; i
< map
->n
; ++i
) {
7603 map
->p
[i
] = isl_basic_map_deltas_map(map
->p
[i
]);
7607 ISL_F_CLR(map
, ISL_MAP_NORMALIZED
);
7614 static __isl_give isl_basic_map
*basic_map_identity(__isl_take isl_space
*dims
)
7616 struct isl_basic_map
*bmap
;
7624 nparam
= dims
->nparam
;
7626 bmap
= isl_basic_map_alloc_space(dims
, 0, dim
, 0);
7630 for (i
= 0; i
< dim
; ++i
) {
7631 int j
= isl_basic_map_alloc_equality(bmap
);
7634 isl_seq_clr(bmap
->eq
[j
], 1 + isl_basic_map_total_dim(bmap
));
7635 isl_int_set_si(bmap
->eq
[j
][1+nparam
+i
], 1);
7636 isl_int_set_si(bmap
->eq
[j
][1+nparam
+dim
+i
], -1);
7638 return isl_basic_map_finalize(bmap
);
7640 isl_basic_map_free(bmap
);
7644 __isl_give isl_basic_map
*isl_basic_map_identity(__isl_take isl_space
*dim
)
7648 if (dim
->n_in
!= dim
->n_out
)
7649 isl_die(dim
->ctx
, isl_error_invalid
,
7650 "number of input and output dimensions needs to be "
7651 "the same", goto error
);
7652 return basic_map_identity(dim
);
7654 isl_space_free(dim
);
7658 __isl_give isl_map
*isl_map_identity(__isl_take isl_space
*dim
)
7660 return isl_map_from_basic_map(isl_basic_map_identity(dim
));
7663 __isl_give isl_map
*isl_set_identity(__isl_take isl_set
*set
)
7665 isl_space
*dim
= isl_set_get_space(set
);
7667 id
= isl_map_identity(isl_space_map_from_set(dim
));
7668 return isl_map_intersect_range(id
, set
);
7671 /* Construct a basic set with all set dimensions having only non-negative
7674 __isl_give isl_basic_set
*isl_basic_set_positive_orthant(
7675 __isl_take isl_space
*space
)
7680 struct isl_basic_set
*bset
;
7684 nparam
= space
->nparam
;
7686 bset
= isl_basic_set_alloc_space(space
, 0, 0, dim
);
7689 for (i
= 0; i
< dim
; ++i
) {
7690 int k
= isl_basic_set_alloc_inequality(bset
);
7693 isl_seq_clr(bset
->ineq
[k
], 1 + isl_basic_set_total_dim(bset
));
7694 isl_int_set_si(bset
->ineq
[k
][1 + nparam
+ i
], 1);
7698 isl_basic_set_free(bset
);
7702 /* Construct the half-space x_pos >= 0.
7704 static __isl_give isl_basic_set
*nonneg_halfspace(__isl_take isl_space
*dim
,
7708 isl_basic_set
*nonneg
;
7710 nonneg
= isl_basic_set_alloc_space(dim
, 0, 0, 1);
7711 k
= isl_basic_set_alloc_inequality(nonneg
);
7714 isl_seq_clr(nonneg
->ineq
[k
], 1 + isl_basic_set_total_dim(nonneg
));
7715 isl_int_set_si(nonneg
->ineq
[k
][pos
], 1);
7717 return isl_basic_set_finalize(nonneg
);
7719 isl_basic_set_free(nonneg
);
7723 /* Construct the half-space x_pos <= -1.
7725 static __isl_give isl_basic_set
*neg_halfspace(__isl_take isl_space
*dim
, int pos
)
7730 neg
= isl_basic_set_alloc_space(dim
, 0, 0, 1);
7731 k
= isl_basic_set_alloc_inequality(neg
);
7734 isl_seq_clr(neg
->ineq
[k
], 1 + isl_basic_set_total_dim(neg
));
7735 isl_int_set_si(neg
->ineq
[k
][0], -1);
7736 isl_int_set_si(neg
->ineq
[k
][pos
], -1);
7738 return isl_basic_set_finalize(neg
);
7740 isl_basic_set_free(neg
);
7744 __isl_give isl_set
*isl_set_split_dims(__isl_take isl_set
*set
,
7745 enum isl_dim_type type
, unsigned first
, unsigned n
)
7749 isl_basic_set
*nonneg
;
7757 isl_assert(set
->ctx
, first
+ n
<= isl_set_dim(set
, type
), goto error
);
7759 offset
= pos(set
->dim
, type
);
7760 for (i
= 0; i
< n
; ++i
) {
7761 nonneg
= nonneg_halfspace(isl_set_get_space(set
),
7762 offset
+ first
+ i
);
7763 neg
= neg_halfspace(isl_set_get_space(set
), offset
+ first
+ i
);
7765 set
= isl_set_intersect(set
, isl_basic_set_union(nonneg
, neg
));
7774 static int foreach_orthant(__isl_take isl_set
*set
, int *signs
, int first
,
7775 int len
, int (*fn
)(__isl_take isl_set
*orthant
, int *signs
, void *user
),
7782 if (isl_set_plain_is_empty(set
)) {
7787 return fn(set
, signs
, user
);
7790 half
= isl_set_from_basic_set(nonneg_halfspace(isl_set_get_space(set
),
7792 half
= isl_set_intersect(half
, isl_set_copy(set
));
7793 if (foreach_orthant(half
, signs
, first
+ 1, len
, fn
, user
) < 0)
7797 half
= isl_set_from_basic_set(neg_halfspace(isl_set_get_space(set
),
7799 half
= isl_set_intersect(half
, set
);
7800 return foreach_orthant(half
, signs
, first
+ 1, len
, fn
, user
);
7806 /* Call "fn" on the intersections of "set" with each of the orthants
7807 * (except for obviously empty intersections). The orthant is identified
7808 * by the signs array, with each entry having value 1 or -1 according
7809 * to the sign of the corresponding variable.
7811 int isl_set_foreach_orthant(__isl_keep isl_set
*set
,
7812 int (*fn
)(__isl_take isl_set
*orthant
, int *signs
, void *user
),
7822 if (isl_set_plain_is_empty(set
))
7825 nparam
= isl_set_dim(set
, isl_dim_param
);
7826 nvar
= isl_set_dim(set
, isl_dim_set
);
7828 signs
= isl_alloc_array(set
->ctx
, int, nparam
+ nvar
);
7830 r
= foreach_orthant(isl_set_copy(set
), signs
, 0, nparam
+ nvar
,
7838 isl_bool
isl_set_is_equal(__isl_keep isl_set
*set1
, __isl_keep isl_set
*set2
)
7840 return isl_map_is_equal((struct isl_map
*)set1
, (struct isl_map
*)set2
);
7843 isl_bool
isl_basic_map_is_subset(__isl_keep isl_basic_map
*bmap1
,
7844 __isl_keep isl_basic_map
*bmap2
)
7847 struct isl_map
*map1
;
7848 struct isl_map
*map2
;
7850 if (!bmap1
|| !bmap2
)
7851 return isl_bool_error
;
7853 map1
= isl_map_from_basic_map(isl_basic_map_copy(bmap1
));
7854 map2
= isl_map_from_basic_map(isl_basic_map_copy(bmap2
));
7856 is_subset
= isl_map_is_subset(map1
, map2
);
7864 isl_bool
isl_basic_set_is_subset(__isl_keep isl_basic_set
*bset1
,
7865 __isl_keep isl_basic_set
*bset2
)
7867 return isl_basic_map_is_subset(bset1
, bset2
);
7870 isl_bool
isl_basic_map_is_equal(__isl_keep isl_basic_map
*bmap1
,
7871 __isl_keep isl_basic_map
*bmap2
)
7875 if (!bmap1
|| !bmap2
)
7876 return isl_bool_error
;
7877 is_subset
= isl_basic_map_is_subset(bmap1
, bmap2
);
7878 if (is_subset
!= isl_bool_true
)
7880 is_subset
= isl_basic_map_is_subset(bmap2
, bmap1
);
7884 isl_bool
isl_basic_set_is_equal(__isl_keep isl_basic_set
*bset1
,
7885 __isl_keep isl_basic_set
*bset2
)
7887 return isl_basic_map_is_equal(
7888 (struct isl_basic_map
*)bset1
, (struct isl_basic_map
*)bset2
);
7891 isl_bool
isl_map_is_empty(__isl_keep isl_map
*map
)
7897 return isl_bool_error
;
7898 for (i
= 0; i
< map
->n
; ++i
) {
7899 is_empty
= isl_basic_map_is_empty(map
->p
[i
]);
7901 return isl_bool_error
;
7903 return isl_bool_false
;
7905 return isl_bool_true
;
7908 isl_bool
isl_map_plain_is_empty(__isl_keep isl_map
*map
)
7910 return map
? map
->n
== 0 : isl_bool_error
;
7913 isl_bool
isl_set_plain_is_empty(__isl_keep isl_set
*set
)
7915 return set
? set
->n
== 0 : isl_bool_error
;
7918 isl_bool
isl_set_is_empty(__isl_keep isl_set
*set
)
7920 return isl_map_is_empty((struct isl_map
*)set
);
7923 int isl_map_has_equal_space(__isl_keep isl_map
*map1
, __isl_keep isl_map
*map2
)
7928 return isl_space_is_equal(map1
->dim
, map2
->dim
);
7931 int isl_set_has_equal_space(__isl_keep isl_set
*set1
, __isl_keep isl_set
*set2
)
7936 return isl_space_is_equal(set1
->dim
, set2
->dim
);
7939 static isl_bool
map_is_equal(__isl_keep isl_map
*map1
, __isl_keep isl_map
*map2
)
7944 return isl_bool_error
;
7945 is_subset
= isl_map_is_subset(map1
, map2
);
7946 if (is_subset
!= isl_bool_true
)
7948 is_subset
= isl_map_is_subset(map2
, map1
);
7952 /* Is "map1" equal to "map2"?
7954 * First check if they are obviously equal.
7955 * If not, then perform a more detailed analysis.
7957 isl_bool
isl_map_is_equal(__isl_keep isl_map
*map1
, __isl_keep isl_map
*map2
)
7961 equal
= isl_map_plain_is_equal(map1
, map2
);
7962 if (equal
< 0 || equal
)
7964 return isl_map_align_params_map_map_and_test(map1
, map2
, &map_is_equal
);
7967 isl_bool
isl_basic_map_is_strict_subset(
7968 struct isl_basic_map
*bmap1
, struct isl_basic_map
*bmap2
)
7972 if (!bmap1
|| !bmap2
)
7973 return isl_bool_error
;
7974 is_subset
= isl_basic_map_is_subset(bmap1
, bmap2
);
7975 if (is_subset
!= isl_bool_true
)
7977 is_subset
= isl_basic_map_is_subset(bmap2
, bmap1
);
7978 if (is_subset
== isl_bool_error
)
7983 isl_bool
isl_map_is_strict_subset(__isl_keep isl_map
*map1
,
7984 __isl_keep isl_map
*map2
)
7989 return isl_bool_error
;
7990 is_subset
= isl_map_is_subset(map1
, map2
);
7991 if (is_subset
!= isl_bool_true
)
7993 is_subset
= isl_map_is_subset(map2
, map1
);
7994 if (is_subset
== isl_bool_error
)
7999 isl_bool
isl_set_is_strict_subset(__isl_keep isl_set
*set1
,
8000 __isl_keep isl_set
*set2
)
8002 return isl_map_is_strict_subset((isl_map
*)set1
, (isl_map
*)set2
);
8005 /* Is "bmap" obviously equal to the universe with the same space?
8007 * That is, does it not have any constraints?
8009 isl_bool
isl_basic_map_plain_is_universe(__isl_keep isl_basic_map
*bmap
)
8012 return isl_bool_error
;
8013 return bmap
->n_eq
== 0 && bmap
->n_ineq
== 0;
8016 /* Is "bset" obviously equal to the universe with the same space?
8018 isl_bool
isl_basic_set_plain_is_universe(__isl_keep isl_basic_set
*bset
)
8020 return isl_basic_map_plain_is_universe(bset
);
8023 /* If "c" does not involve any existentially quantified variables,
8024 * then set *univ to false and abort
8026 static isl_stat
involves_divs(__isl_take isl_constraint
*c
, void *user
)
8028 isl_bool
*univ
= user
;
8031 n
= isl_constraint_dim(c
, isl_dim_div
);
8032 *univ
= isl_constraint_involves_dims(c
, isl_dim_div
, 0, n
);
8033 isl_constraint_free(c
);
8034 if (*univ
< 0 || !*univ
)
8035 return isl_stat_error
;
8039 /* Is "bmap" equal to the universe with the same space?
8041 * First check if it is obviously equal to the universe.
8042 * If not and if there are any constraints not involving
8043 * existentially quantified variables, then it is certainly
8044 * not equal to the universe.
8045 * Otherwise, check if the universe is a subset of "bmap".
8047 isl_bool
isl_basic_map_is_universe(__isl_keep isl_basic_map
*bmap
)
8050 isl_basic_map
*test
;
8052 univ
= isl_basic_map_plain_is_universe(bmap
);
8053 if (univ
< 0 || univ
)
8055 if (isl_basic_map_dim(bmap
, isl_dim_div
) == 0)
8056 return isl_bool_false
;
8057 univ
= isl_bool_true
;
8058 if (isl_basic_map_foreach_constraint(bmap
, &involves_divs
, &univ
) < 0 &&
8060 return isl_bool_error
;
8061 if (univ
< 0 || !univ
)
8063 test
= isl_basic_map_universe(isl_basic_map_get_space(bmap
));
8064 univ
= isl_basic_map_is_subset(test
, bmap
);
8065 isl_basic_map_free(test
);
8069 /* Is "bset" equal to the universe with the same space?
8071 isl_bool
isl_basic_set_is_universe(__isl_keep isl_basic_set
*bset
)
8073 return isl_basic_map_is_universe(bset
);
8076 isl_bool
isl_map_plain_is_universe(__isl_keep isl_map
*map
)
8081 return isl_bool_error
;
8083 for (i
= 0; i
< map
->n
; ++i
) {
8084 isl_bool r
= isl_basic_map_plain_is_universe(map
->p
[i
]);
8089 return isl_bool_false
;
8092 isl_bool
isl_set_plain_is_universe(__isl_keep isl_set
*set
)
8094 return isl_map_plain_is_universe((isl_map
*) set
);
8097 isl_bool
isl_basic_map_is_empty(__isl_keep isl_basic_map
*bmap
)
8099 struct isl_basic_set
*bset
= NULL
;
8100 struct isl_vec
*sample
= NULL
;
8101 isl_bool empty
, non_empty
;
8104 return isl_bool_error
;
8106 if (ISL_F_ISSET(bmap
, ISL_BASIC_MAP_EMPTY
))
8107 return isl_bool_true
;
8109 if (isl_basic_map_plain_is_universe(bmap
))
8110 return isl_bool_false
;
8112 if (ISL_F_ISSET(bmap
, ISL_BASIC_MAP_RATIONAL
)) {
8113 struct isl_basic_map
*copy
= isl_basic_map_copy(bmap
);
8114 copy
= isl_basic_map_remove_redundancies(copy
);
8115 empty
= isl_basic_map_plain_is_empty(copy
);
8116 isl_basic_map_free(copy
);
8120 non_empty
= isl_basic_map_plain_is_non_empty(bmap
);
8122 return isl_bool_error
;
8124 return isl_bool_false
;
8125 isl_vec_free(bmap
->sample
);
8126 bmap
->sample
= NULL
;
8127 bset
= isl_basic_map_underlying_set(isl_basic_map_copy(bmap
));
8129 return isl_bool_error
;
8130 sample
= isl_basic_set_sample_vec(bset
);
8132 return isl_bool_error
;
8133 empty
= sample
->size
== 0;
8134 isl_vec_free(bmap
->sample
);
8135 bmap
->sample
= sample
;
8137 ISL_F_SET(bmap
, ISL_BASIC_MAP_EMPTY
);
8142 isl_bool
isl_basic_map_plain_is_empty(__isl_keep isl_basic_map
*bmap
)
8145 return isl_bool_error
;
8146 return ISL_F_ISSET(bmap
, ISL_BASIC_MAP_EMPTY
);
8149 isl_bool
isl_basic_set_plain_is_empty(__isl_keep isl_basic_set
*bset
)
8152 return isl_bool_error
;
8153 return ISL_F_ISSET(bset
, ISL_BASIC_SET_EMPTY
);
8156 /* Is "bmap" known to be non-empty?
8158 * That is, is the cached sample still valid?
8160 isl_bool
isl_basic_map_plain_is_non_empty(__isl_keep isl_basic_map
*bmap
)
8165 return isl_bool_error
;
8167 return isl_bool_false
;
8168 total
= 1 + isl_basic_map_total_dim(bmap
);
8169 if (bmap
->sample
->size
!= total
)
8170 return isl_bool_false
;
8171 return isl_basic_map_contains(bmap
, bmap
->sample
);
8174 isl_bool
isl_basic_set_is_empty(__isl_keep isl_basic_set
*bset
)
8176 return isl_basic_map_is_empty((struct isl_basic_map
*)bset
);
8179 struct isl_map
*isl_basic_map_union(
8180 struct isl_basic_map
*bmap1
, struct isl_basic_map
*bmap2
)
8182 struct isl_map
*map
;
8183 if (!bmap1
|| !bmap2
)
8186 isl_assert(bmap1
->ctx
, isl_space_is_equal(bmap1
->dim
, bmap2
->dim
), goto error
);
8188 map
= isl_map_alloc_space(isl_space_copy(bmap1
->dim
), 2, 0);
8191 map
= isl_map_add_basic_map(map
, bmap1
);
8192 map
= isl_map_add_basic_map(map
, bmap2
);
8195 isl_basic_map_free(bmap1
);
8196 isl_basic_map_free(bmap2
);
8200 struct isl_set
*isl_basic_set_union(
8201 struct isl_basic_set
*bset1
, struct isl_basic_set
*bset2
)
8203 return (struct isl_set
*)isl_basic_map_union(
8204 (struct isl_basic_map
*)bset1
,
8205 (struct isl_basic_map
*)bset2
);
8208 /* Order divs such that any div only depends on previous divs */
8209 struct isl_basic_map
*isl_basic_map_order_divs(struct isl_basic_map
*bmap
)
8217 off
= isl_space_dim(bmap
->dim
, isl_dim_all
);
8219 for (i
= 0; i
< bmap
->n_div
; ++i
) {
8221 if (isl_int_is_zero(bmap
->div
[i
][0]))
8223 pos
= isl_seq_first_non_zero(bmap
->div
[i
]+1+1+off
+i
,
8228 isl_die(isl_basic_map_get_ctx(bmap
), isl_error_internal
,
8229 "integer division depends on itself",
8230 return isl_basic_map_free(bmap
));
8231 isl_basic_map_swap_div(bmap
, i
, i
+ pos
);
8237 struct isl_basic_set
*isl_basic_set_order_divs(struct isl_basic_set
*bset
)
8239 return (struct isl_basic_set
*)
8240 isl_basic_map_order_divs((struct isl_basic_map
*)bset
);
8243 __isl_give isl_map
*isl_map_order_divs(__isl_take isl_map
*map
)
8250 for (i
= 0; i
< map
->n
; ++i
) {
8251 map
->p
[i
] = isl_basic_map_order_divs(map
->p
[i
]);
8262 /* Apply the expansion computed by isl_merge_divs.
8263 * The expansion itself is given by "exp" while the resulting
8264 * list of divs is given by "div".
8266 * Move the integer divisions of "bmap" into the right position
8267 * according to "exp" and then introduce the additional integer
8268 * divisions, adding div constraints.
8269 * The moving should be done first to avoid moving coefficients
8270 * in the definitions of the extra integer divisions.
8272 __isl_give isl_basic_map
*isl_basic_map_expand_divs(
8273 __isl_take isl_basic_set
*bmap
, __isl_take isl_mat
*div
, int *exp
)
8278 bmap
= isl_basic_map_cow(bmap
);
8282 if (div
->n_row
< bmap
->n_div
)
8283 isl_die(isl_mat_get_ctx(div
), isl_error_invalid
,
8284 "not an expansion", goto error
);
8286 n_div
= bmap
->n_div
;
8287 bmap
= isl_basic_map_extend_space(bmap
, isl_space_copy(bmap
->dim
),
8288 div
->n_row
- n_div
, 0,
8289 2 * (div
->n_row
- n_div
));
8291 for (i
= n_div
; i
< div
->n_row
; ++i
)
8292 if (isl_basic_map_alloc_div(bmap
) < 0)
8295 for (j
= n_div
- 1; j
>= 0; --j
) {
8298 isl_basic_map_swap_div(bmap
, j
, exp
[j
]);
8301 for (i
= 0; i
< div
->n_row
; ++i
) {
8302 if (j
< n_div
&& exp
[j
] == i
) {
8305 isl_seq_cpy(bmap
->div
[i
], div
->row
[i
], div
->n_col
);
8306 if (isl_basic_map_div_is_marked_unknown(bmap
, i
))
8308 if (isl_basic_map_add_div_constraints(bmap
, i
) < 0)
8316 isl_basic_map_free(bmap
);
8321 /* Apply the expansion computed by isl_merge_divs.
8322 * The expansion itself is given by "exp" while the resulting
8323 * list of divs is given by "div".
8325 __isl_give isl_basic_set
*isl_basic_set_expand_divs(
8326 __isl_take isl_basic_set
*bset
, __isl_take isl_mat
*div
, int *exp
)
8328 return isl_basic_map_expand_divs(bset
, div
, exp
);
8331 /* Look for a div in dst that corresponds to the div "div" in src.
8332 * The divs before "div" in src and dst are assumed to be the same.
8334 * Returns -1 if no corresponding div was found and the position
8335 * of the corresponding div in dst otherwise.
8337 static int find_div(struct isl_basic_map
*dst
,
8338 struct isl_basic_map
*src
, unsigned div
)
8342 unsigned total
= isl_space_dim(src
->dim
, isl_dim_all
);
8344 isl_assert(dst
->ctx
, div
<= dst
->n_div
, return -1);
8345 for (i
= div
; i
< dst
->n_div
; ++i
)
8346 if (isl_seq_eq(dst
->div
[i
], src
->div
[div
], 1+1+total
+div
) &&
8347 isl_seq_first_non_zero(dst
->div
[i
]+1+1+total
+div
,
8348 dst
->n_div
- div
) == -1)
8353 /* Align the divs of "dst" to those of "src", adding divs from "src"
8354 * if needed. That is, make sure that the first src->n_div divs
8355 * of the result are equal to those of src.
8357 * The result is not finalized as by design it will have redundant
8358 * divs if any divs from "src" were copied.
8360 __isl_give isl_basic_map
*isl_basic_map_align_divs(
8361 __isl_take isl_basic_map
*dst
, __isl_keep isl_basic_map
*src
)
8364 int known
, extended
;
8368 return isl_basic_map_free(dst
);
8370 if (src
->n_div
== 0)
8373 known
= isl_basic_map_divs_known(src
);
8375 return isl_basic_map_free(dst
);
8377 isl_die(isl_basic_map_get_ctx(src
), isl_error_invalid
,
8378 "some src divs are unknown",
8379 return isl_basic_map_free(dst
));
8381 src
= isl_basic_map_order_divs(src
);
8384 total
= isl_space_dim(src
->dim
, isl_dim_all
);
8385 for (i
= 0; i
< src
->n_div
; ++i
) {
8386 int j
= find_div(dst
, src
, i
);
8389 int extra
= src
->n_div
- i
;
8390 dst
= isl_basic_map_cow(dst
);
8393 dst
= isl_basic_map_extend_space(dst
,
8394 isl_space_copy(dst
->dim
),
8395 extra
, 0, 2 * extra
);
8398 j
= isl_basic_map_alloc_div(dst
);
8400 return isl_basic_map_free(dst
);
8401 isl_seq_cpy(dst
->div
[j
], src
->div
[i
], 1+1+total
+i
);
8402 isl_seq_clr(dst
->div
[j
]+1+1+total
+i
, dst
->n_div
- i
);
8403 if (isl_basic_map_add_div_constraints(dst
, j
) < 0)
8404 return isl_basic_map_free(dst
);
8407 isl_basic_map_swap_div(dst
, i
, j
);
8412 struct isl_basic_set
*isl_basic_set_align_divs(
8413 struct isl_basic_set
*dst
, struct isl_basic_set
*src
)
8415 return (struct isl_basic_set
*)isl_basic_map_align_divs(
8416 (struct isl_basic_map
*)dst
, (struct isl_basic_map
*)src
);
8419 struct isl_map
*isl_map_align_divs(struct isl_map
*map
)
8427 map
= isl_map_compute_divs(map
);
8428 map
= isl_map_cow(map
);
8432 for (i
= 1; i
< map
->n
; ++i
)
8433 map
->p
[0] = isl_basic_map_align_divs(map
->p
[0], map
->p
[i
]);
8434 for (i
= 1; i
< map
->n
; ++i
) {
8435 map
->p
[i
] = isl_basic_map_align_divs(map
->p
[i
], map
->p
[0]);
8437 return isl_map_free(map
);
8440 ISL_F_CLR(map
, ISL_MAP_NORMALIZED
);
8444 struct isl_set
*isl_set_align_divs(struct isl_set
*set
)
8446 return (struct isl_set
*)isl_map_align_divs((struct isl_map
*)set
);
8449 /* Align the divs of the basic maps in "map" to those
8450 * of the basic maps in "list", as well as to the other basic maps in "map".
8451 * The elements in "list" are assumed to have known divs.
8453 __isl_give isl_map
*isl_map_align_divs_to_basic_map_list(
8454 __isl_take isl_map
*map
, __isl_keep isl_basic_map_list
*list
)
8458 map
= isl_map_compute_divs(map
);
8459 map
= isl_map_cow(map
);
8461 return isl_map_free(map
);
8465 n
= isl_basic_map_list_n_basic_map(list
);
8466 for (i
= 0; i
< n
; ++i
) {
8467 isl_basic_map
*bmap
;
8469 bmap
= isl_basic_map_list_get_basic_map(list
, i
);
8470 map
->p
[0] = isl_basic_map_align_divs(map
->p
[0], bmap
);
8471 isl_basic_map_free(bmap
);
8474 return isl_map_free(map
);
8476 return isl_map_align_divs(map
);
8479 /* Align the divs of each element of "list" to those of "bmap".
8480 * Both "bmap" and the elements of "list" are assumed to have known divs.
8482 __isl_give isl_basic_map_list
*isl_basic_map_list_align_divs_to_basic_map(
8483 __isl_take isl_basic_map_list
*list
, __isl_keep isl_basic_map
*bmap
)
8488 return isl_basic_map_list_free(list
);
8490 n
= isl_basic_map_list_n_basic_map(list
);
8491 for (i
= 0; i
< n
; ++i
) {
8492 isl_basic_map
*bmap_i
;
8494 bmap_i
= isl_basic_map_list_get_basic_map(list
, i
);
8495 bmap_i
= isl_basic_map_align_divs(bmap_i
, bmap
);
8496 list
= isl_basic_map_list_set_basic_map(list
, i
, bmap_i
);
8502 static __isl_give isl_set
*set_apply( __isl_take isl_set
*set
,
8503 __isl_take isl_map
*map
)
8507 isl_assert(set
->ctx
, isl_map_compatible_domain(map
, set
), goto error
);
8508 map
= isl_map_intersect_domain(map
, set
);
8509 set
= isl_map_range(map
);
8517 __isl_give isl_set
*isl_set_apply( __isl_take isl_set
*set
,
8518 __isl_take isl_map
*map
)
8520 return isl_map_align_params_map_map_and(set
, map
, &set_apply
);
8523 /* There is no need to cow as removing empty parts doesn't change
8524 * the meaning of the set.
8526 struct isl_map
*isl_map_remove_empty_parts(struct isl_map
*map
)
8533 for (i
= map
->n
- 1; i
>= 0; --i
)
8534 remove_if_empty(map
, i
);
8539 struct isl_set
*isl_set_remove_empty_parts(struct isl_set
*set
)
8541 return (struct isl_set
*)
8542 isl_map_remove_empty_parts((struct isl_map
*)set
);
8545 struct isl_basic_map
*isl_map_copy_basic_map(struct isl_map
*map
)
8547 struct isl_basic_map
*bmap
;
8548 if (!map
|| map
->n
== 0)
8550 bmap
= map
->p
[map
->n
-1];
8551 isl_assert(map
->ctx
, ISL_F_ISSET(bmap
, ISL_BASIC_SET_FINAL
), return NULL
);
8552 return isl_basic_map_copy(bmap
);
8555 struct isl_basic_set
*isl_set_copy_basic_set(struct isl_set
*set
)
8557 return (struct isl_basic_set
*)
8558 isl_map_copy_basic_map((struct isl_map
*)set
);
8561 __isl_give isl_map
*isl_map_drop_basic_map(__isl_take isl_map
*map
,
8562 __isl_keep isl_basic_map
*bmap
)
8568 for (i
= map
->n
-1; i
>= 0; --i
) {
8569 if (map
->p
[i
] != bmap
)
8571 map
= isl_map_cow(map
);
8574 isl_basic_map_free(map
->p
[i
]);
8575 if (i
!= map
->n
-1) {
8576 ISL_F_CLR(map
, ISL_SET_NORMALIZED
);
8577 map
->p
[i
] = map
->p
[map
->n
-1];
8588 struct isl_set
*isl_set_drop_basic_set(struct isl_set
*set
,
8589 struct isl_basic_set
*bset
)
8591 return (struct isl_set
*)isl_map_drop_basic_map((struct isl_map
*)set
,
8592 (struct isl_basic_map
*)bset
);
8595 /* Given two basic sets bset1 and bset2, compute the maximal difference
8596 * between the values of dimension pos in bset1 and those in bset2
8597 * for any common value of the parameters and dimensions preceding pos.
8599 static enum isl_lp_result
basic_set_maximal_difference_at(
8600 __isl_keep isl_basic_set
*bset1
, __isl_keep isl_basic_set
*bset2
,
8601 int pos
, isl_int
*opt
)
8604 struct isl_basic_map
*bmap1
= NULL
;
8605 struct isl_basic_map
*bmap2
= NULL
;
8606 struct isl_ctx
*ctx
;
8607 struct isl_vec
*obj
;
8610 unsigned dim1
, dim2
;
8611 enum isl_lp_result res
;
8613 if (!bset1
|| !bset2
)
8614 return isl_lp_error
;
8616 nparam
= isl_basic_set_n_param(bset1
);
8617 dim1
= isl_basic_set_n_dim(bset1
);
8618 dim2
= isl_basic_set_n_dim(bset2
);
8619 dims
= isl_space_alloc(bset1
->ctx
, nparam
, pos
, dim1
- pos
);
8620 bmap1
= isl_basic_map_from_basic_set(isl_basic_set_copy(bset1
), dims
);
8621 dims
= isl_space_alloc(bset2
->ctx
, nparam
, pos
, dim2
- pos
);
8622 bmap2
= isl_basic_map_from_basic_set(isl_basic_set_copy(bset2
), dims
);
8623 if (!bmap1
|| !bmap2
)
8625 bmap1
= isl_basic_map_cow(bmap1
);
8626 bmap1
= isl_basic_map_extend(bmap1
, nparam
,
8627 pos
, (dim1
- pos
) + (dim2
- pos
),
8628 bmap2
->n_div
, bmap2
->n_eq
, bmap2
->n_ineq
);
8629 bmap1
= add_constraints(bmap1
, bmap2
, 0, dim1
- pos
);
8632 total
= isl_basic_map_total_dim(bmap1
);
8634 obj
= isl_vec_alloc(ctx
, 1 + total
);
8637 isl_seq_clr(obj
->block
.data
, 1 + total
);
8638 isl_int_set_si(obj
->block
.data
[1+nparam
+pos
], 1);
8639 isl_int_set_si(obj
->block
.data
[1+nparam
+pos
+(dim1
-pos
)], -1);
8640 res
= isl_basic_map_solve_lp(bmap1
, 1, obj
->block
.data
, ctx
->one
,
8642 isl_basic_map_free(bmap1
);
8646 isl_basic_map_free(bmap2
);
8648 isl_basic_map_free(bmap1
);
8649 return isl_lp_error
;
8652 /* Given two _disjoint_ basic sets bset1 and bset2, check whether
8653 * for any common value of the parameters and dimensions preceding pos
8654 * in both basic sets, the values of dimension pos in bset1 are
8655 * smaller or larger than those in bset2.
8658 * 1 if bset1 follows bset2
8659 * -1 if bset1 precedes bset2
8660 * 0 if bset1 and bset2 are incomparable
8661 * -2 if some error occurred.
8663 int isl_basic_set_compare_at(struct isl_basic_set
*bset1
,
8664 struct isl_basic_set
*bset2
, int pos
)
8667 enum isl_lp_result res
;
8672 res
= basic_set_maximal_difference_at(bset1
, bset2
, pos
, &opt
);
8674 if (res
== isl_lp_empty
)
8676 else if ((res
== isl_lp_ok
&& isl_int_is_pos(opt
)) ||
8677 res
== isl_lp_unbounded
)
8679 else if (res
== isl_lp_ok
&& isl_int_is_neg(opt
))
8688 /* Given two basic sets bset1 and bset2, check whether
8689 * for any common value of the parameters and dimensions preceding pos
8690 * there is a value of dimension pos in bset1 that is larger
8691 * than a value of the same dimension in bset2.
8694 * 1 if there exists such a pair
8695 * 0 if there is no such pair, but there is a pair of equal values
8697 * -2 if some error occurred.
8699 int isl_basic_set_follows_at(__isl_keep isl_basic_set
*bset1
,
8700 __isl_keep isl_basic_set
*bset2
, int pos
)
8703 enum isl_lp_result res
;
8708 res
= basic_set_maximal_difference_at(bset1
, bset2
, pos
, &opt
);
8710 if (res
== isl_lp_empty
)
8712 else if ((res
== isl_lp_ok
&& isl_int_is_pos(opt
)) ||
8713 res
== isl_lp_unbounded
)
8715 else if (res
== isl_lp_ok
&& isl_int_is_neg(opt
))
8717 else if (res
== isl_lp_ok
)
8726 /* Given two sets set1 and set2, check whether
8727 * for any common value of the parameters and dimensions preceding pos
8728 * there is a value of dimension pos in set1 that is larger
8729 * than a value of the same dimension in set2.
8732 * 1 if there exists such a pair
8733 * 0 if there is no such pair, but there is a pair of equal values
8735 * -2 if some error occurred.
8737 int isl_set_follows_at(__isl_keep isl_set
*set1
,
8738 __isl_keep isl_set
*set2
, int pos
)
8746 for (i
= 0; i
< set1
->n
; ++i
)
8747 for (j
= 0; j
< set2
->n
; ++j
) {
8749 f
= isl_basic_set_follows_at(set1
->p
[i
], set2
->p
[j
], pos
);
8750 if (f
== 1 || f
== -2)
8759 static int isl_basic_map_plain_has_fixed_var(__isl_keep isl_basic_map
*bmap
,
8760 unsigned pos
, isl_int
*val
)
8768 total
= isl_basic_map_total_dim(bmap
);
8769 for (i
= 0, d
= total
-1; i
< bmap
->n_eq
&& d
+1 > pos
; ++i
) {
8770 for (; d
+1 > pos
; --d
)
8771 if (!isl_int_is_zero(bmap
->eq
[i
][1+d
]))
8775 if (isl_seq_first_non_zero(bmap
->eq
[i
]+1, d
) != -1)
8777 if (isl_seq_first_non_zero(bmap
->eq
[i
]+1+d
+1, total
-d
-1) != -1)
8779 if (!isl_int_is_one(bmap
->eq
[i
][1+d
]))
8782 isl_int_neg(*val
, bmap
->eq
[i
][0]);
8788 static int isl_map_plain_has_fixed_var(__isl_keep isl_map
*map
,
8789 unsigned pos
, isl_int
*val
)
8801 return isl_basic_map_plain_has_fixed_var(map
->p
[0], pos
, val
);
8804 fixed
= isl_basic_map_plain_has_fixed_var(map
->p
[0], pos
, &v
);
8805 for (i
= 1; fixed
== 1 && i
< map
->n
; ++i
) {
8806 fixed
= isl_basic_map_plain_has_fixed_var(map
->p
[i
], pos
, &tmp
);
8807 if (fixed
== 1 && isl_int_ne(tmp
, v
))
8811 isl_int_set(*val
, v
);
8817 static int isl_basic_set_plain_has_fixed_var(__isl_keep isl_basic_set
*bset
,
8818 unsigned pos
, isl_int
*val
)
8820 return isl_basic_map_plain_has_fixed_var((struct isl_basic_map
*)bset
,
8824 static int isl_set_plain_has_fixed_var(__isl_keep isl_set
*set
, unsigned pos
,
8827 return isl_map_plain_has_fixed_var((struct isl_map
*)set
, pos
, val
);
8830 int isl_basic_map_plain_is_fixed(__isl_keep isl_basic_map
*bmap
,
8831 enum isl_dim_type type
, unsigned pos
, isl_int
*val
)
8833 if (pos
>= isl_basic_map_dim(bmap
, type
))
8835 return isl_basic_map_plain_has_fixed_var(bmap
,
8836 isl_basic_map_offset(bmap
, type
) - 1 + pos
, val
);
8839 /* If "bmap" obviously lies on a hyperplane where the given dimension
8840 * has a fixed value, then return that value.
8841 * Otherwise return NaN.
8843 __isl_give isl_val
*isl_basic_map_plain_get_val_if_fixed(
8844 __isl_keep isl_basic_map
*bmap
,
8845 enum isl_dim_type type
, unsigned pos
)
8853 ctx
= isl_basic_map_get_ctx(bmap
);
8854 v
= isl_val_alloc(ctx
);
8857 fixed
= isl_basic_map_plain_is_fixed(bmap
, type
, pos
, &v
->n
);
8859 return isl_val_free(v
);
8861 isl_int_set_si(v
->d
, 1);
8865 return isl_val_nan(ctx
);
8868 int isl_map_plain_is_fixed(__isl_keep isl_map
*map
,
8869 enum isl_dim_type type
, unsigned pos
, isl_int
*val
)
8871 if (pos
>= isl_map_dim(map
, type
))
8873 return isl_map_plain_has_fixed_var(map
,
8874 map_offset(map
, type
) - 1 + pos
, val
);
8877 /* If "map" obviously lies on a hyperplane where the given dimension
8878 * has a fixed value, then return that value.
8879 * Otherwise return NaN.
8881 __isl_give isl_val
*isl_map_plain_get_val_if_fixed(__isl_keep isl_map
*map
,
8882 enum isl_dim_type type
, unsigned pos
)
8890 ctx
= isl_map_get_ctx(map
);
8891 v
= isl_val_alloc(ctx
);
8894 fixed
= isl_map_plain_is_fixed(map
, type
, pos
, &v
->n
);
8896 return isl_val_free(v
);
8898 isl_int_set_si(v
->d
, 1);
8902 return isl_val_nan(ctx
);
8905 /* If "set" obviously lies on a hyperplane where the given dimension
8906 * has a fixed value, then return that value.
8907 * Otherwise return NaN.
8909 __isl_give isl_val
*isl_set_plain_get_val_if_fixed(__isl_keep isl_set
*set
,
8910 enum isl_dim_type type
, unsigned pos
)
8912 return isl_map_plain_get_val_if_fixed(set
, type
, pos
);
8915 int isl_set_plain_is_fixed(__isl_keep isl_set
*set
,
8916 enum isl_dim_type type
, unsigned pos
, isl_int
*val
)
8918 return isl_map_plain_is_fixed(set
, type
, pos
, val
);
8921 /* Check if dimension dim has fixed value and if so and if val is not NULL,
8922 * then return this fixed value in *val.
8924 int isl_basic_set_plain_dim_is_fixed(__isl_keep isl_basic_set
*bset
,
8925 unsigned dim
, isl_int
*val
)
8927 return isl_basic_set_plain_has_fixed_var(bset
,
8928 isl_basic_set_n_param(bset
) + dim
, val
);
8931 /* Check if dimension dim has fixed value and if so and if val is not NULL,
8932 * then return this fixed value in *val.
8934 int isl_set_plain_dim_is_fixed(__isl_keep isl_set
*set
,
8935 unsigned dim
, isl_int
*val
)
8937 return isl_set_plain_has_fixed_var(set
, isl_set_n_param(set
) + dim
, val
);
8940 /* Check if input variable in has fixed value and if so and if val is not NULL,
8941 * then return this fixed value in *val.
8943 int isl_map_plain_input_is_fixed(__isl_keep isl_map
*map
,
8944 unsigned in
, isl_int
*val
)
8946 return isl_map_plain_has_fixed_var(map
, isl_map_n_param(map
) + in
, val
);
8949 /* Check if dimension dim has an (obvious) fixed lower bound and if so
8950 * and if val is not NULL, then return this lower bound in *val.
8952 int isl_basic_set_plain_dim_has_fixed_lower_bound(
8953 __isl_keep isl_basic_set
*bset
, unsigned dim
, isl_int
*val
)
8955 int i
, i_eq
= -1, i_ineq
= -1;
8962 total
= isl_basic_set_total_dim(bset
);
8963 nparam
= isl_basic_set_n_param(bset
);
8964 for (i
= 0; i
< bset
->n_eq
; ++i
) {
8965 if (isl_int_is_zero(bset
->eq
[i
][1+nparam
+dim
]))
8971 for (i
= 0; i
< bset
->n_ineq
; ++i
) {
8972 if (!isl_int_is_pos(bset
->ineq
[i
][1+nparam
+dim
]))
8974 if (i_eq
!= -1 || i_ineq
!= -1)
8978 if (i_eq
== -1 && i_ineq
== -1)
8980 c
= i_eq
!= -1 ? bset
->eq
[i_eq
] : bset
->ineq
[i_ineq
];
8981 /* The coefficient should always be one due to normalization. */
8982 if (!isl_int_is_one(c
[1+nparam
+dim
]))
8984 if (isl_seq_first_non_zero(c
+1, nparam
+dim
) != -1)
8986 if (isl_seq_first_non_zero(c
+1+nparam
+dim
+1,
8987 total
- nparam
- dim
- 1) != -1)
8990 isl_int_neg(*val
, c
[0]);
8994 int isl_set_plain_dim_has_fixed_lower_bound(__isl_keep isl_set
*set
,
8995 unsigned dim
, isl_int
*val
)
9007 return isl_basic_set_plain_dim_has_fixed_lower_bound(set
->p
[0],
9011 fixed
= isl_basic_set_plain_dim_has_fixed_lower_bound(set
->p
[0],
9013 for (i
= 1; fixed
== 1 && i
< set
->n
; ++i
) {
9014 fixed
= isl_basic_set_plain_dim_has_fixed_lower_bound(set
->p
[i
],
9016 if (fixed
== 1 && isl_int_ne(tmp
, v
))
9020 isl_int_set(*val
, v
);
9026 /* Return -1 if the constraint "c1" should be sorted before "c2"
9027 * and 1 if it should be sorted after "c2".
9028 * Return 0 if the two constraints are the same (up to the constant term).
9030 * In particular, if a constraint involves later variables than another
9031 * then it is sorted after this other constraint.
9032 * uset_gist depends on constraints without existentially quantified
9033 * variables sorting first.
9035 * For constraints that have the same latest variable, those
9036 * with the same coefficient for this latest variable (first in absolute value
9037 * and then in actual value) are grouped together.
9038 * This is useful for detecting pairs of constraints that can
9039 * be chained in their printed representation.
9041 * Finally, within a group, constraints are sorted according to
9042 * their coefficients (excluding the constant term).
9044 static int sort_constraint_cmp(const void *p1
, const void *p2
, void *arg
)
9046 isl_int
**c1
= (isl_int
**) p1
;
9047 isl_int
**c2
= (isl_int
**) p2
;
9049 unsigned size
= *(unsigned *) arg
;
9052 l1
= isl_seq_last_non_zero(*c1
+ 1, size
);
9053 l2
= isl_seq_last_non_zero(*c2
+ 1, size
);
9058 cmp
= isl_int_abs_cmp((*c1
)[1 + l1
], (*c2
)[1 + l1
]);
9061 cmp
= isl_int_cmp((*c1
)[1 + l1
], (*c2
)[1 + l1
]);
9065 return isl_seq_cmp(*c1
+ 1, *c2
+ 1, size
);
9068 /* Return -1 if the constraint "c1" of "bmap" is sorted before "c2"
9069 * by isl_basic_map_sort_constraints, 1 if it is sorted after "c2"
9070 * and 0 if the two constraints are the same (up to the constant term).
9072 int isl_basic_map_constraint_cmp(__isl_keep isl_basic_map
*bmap
,
9073 isl_int
*c1
, isl_int
*c2
)
9079 total
= isl_basic_map_total_dim(bmap
);
9080 return sort_constraint_cmp(&c1
, &c2
, &total
);
9083 __isl_give isl_basic_map
*isl_basic_map_sort_constraints(
9084 __isl_take isl_basic_map
*bmap
)
9090 if (bmap
->n_ineq
== 0)
9092 if (ISL_F_ISSET(bmap
, ISL_BASIC_MAP_NORMALIZED
))
9094 total
= isl_basic_map_total_dim(bmap
);
9095 if (isl_sort(bmap
->ineq
, bmap
->n_ineq
, sizeof(isl_int
*),
9096 &sort_constraint_cmp
, &total
) < 0)
9097 return isl_basic_map_free(bmap
);
9101 __isl_give isl_basic_set
*isl_basic_set_sort_constraints(
9102 __isl_take isl_basic_set
*bset
)
9104 return (struct isl_basic_set
*)isl_basic_map_sort_constraints(
9105 (struct isl_basic_map
*)bset
);
9108 struct isl_basic_map
*isl_basic_map_normalize(struct isl_basic_map
*bmap
)
9112 if (ISL_F_ISSET(bmap
, ISL_BASIC_MAP_NORMALIZED
))
9114 bmap
= isl_basic_map_remove_redundancies(bmap
);
9115 bmap
= isl_basic_map_sort_constraints(bmap
);
9117 ISL_F_SET(bmap
, ISL_BASIC_MAP_NORMALIZED
);
9121 struct isl_basic_set
*isl_basic_set_normalize(struct isl_basic_set
*bset
)
9123 return (struct isl_basic_set
*)isl_basic_map_normalize(
9124 (struct isl_basic_map
*)bset
);
9127 int isl_basic_map_plain_cmp(const __isl_keep isl_basic_map
*bmap1
,
9128 const __isl_keep isl_basic_map
*bmap2
)
9133 if (!bmap1
|| !bmap2
)
9138 if (ISL_F_ISSET(bmap1
, ISL_BASIC_MAP_RATIONAL
) !=
9139 ISL_F_ISSET(bmap2
, ISL_BASIC_MAP_RATIONAL
))
9140 return ISL_F_ISSET(bmap1
, ISL_BASIC_MAP_RATIONAL
) ? -1 : 1;
9141 if (isl_basic_map_n_param(bmap1
) != isl_basic_map_n_param(bmap2
))
9142 return isl_basic_map_n_param(bmap1
) - isl_basic_map_n_param(bmap2
);
9143 if (isl_basic_map_n_in(bmap1
) != isl_basic_map_n_in(bmap2
))
9144 return isl_basic_map_n_out(bmap1
) - isl_basic_map_n_out(bmap2
);
9145 if (isl_basic_map_n_out(bmap1
) != isl_basic_map_n_out(bmap2
))
9146 return isl_basic_map_n_out(bmap1
) - isl_basic_map_n_out(bmap2
);
9147 if (ISL_F_ISSET(bmap1
, ISL_BASIC_MAP_EMPTY
) &&
9148 ISL_F_ISSET(bmap2
, ISL_BASIC_MAP_EMPTY
))
9150 if (ISL_F_ISSET(bmap1
, ISL_BASIC_MAP_EMPTY
))
9152 if (ISL_F_ISSET(bmap2
, ISL_BASIC_MAP_EMPTY
))
9154 if (bmap1
->n_eq
!= bmap2
->n_eq
)
9155 return bmap1
->n_eq
- bmap2
->n_eq
;
9156 if (bmap1
->n_ineq
!= bmap2
->n_ineq
)
9157 return bmap1
->n_ineq
- bmap2
->n_ineq
;
9158 if (bmap1
->n_div
!= bmap2
->n_div
)
9159 return bmap1
->n_div
- bmap2
->n_div
;
9160 total
= isl_basic_map_total_dim(bmap1
);
9161 for (i
= 0; i
< bmap1
->n_eq
; ++i
) {
9162 cmp
= isl_seq_cmp(bmap1
->eq
[i
], bmap2
->eq
[i
], 1+total
);
9166 for (i
= 0; i
< bmap1
->n_ineq
; ++i
) {
9167 cmp
= isl_seq_cmp(bmap1
->ineq
[i
], bmap2
->ineq
[i
], 1+total
);
9171 for (i
= 0; i
< bmap1
->n_div
; ++i
) {
9172 cmp
= isl_seq_cmp(bmap1
->div
[i
], bmap2
->div
[i
], 1+1+total
);
9179 int isl_basic_set_plain_cmp(const __isl_keep isl_basic_set
*bset1
,
9180 const __isl_keep isl_basic_set
*bset2
)
9182 return isl_basic_map_plain_cmp(bset1
, bset2
);
9185 int isl_set_plain_cmp(__isl_keep isl_set
*set1
, __isl_keep isl_set
*set2
)
9191 if (set1
->n
!= set2
->n
)
9192 return set1
->n
- set2
->n
;
9194 for (i
= 0; i
< set1
->n
; ++i
) {
9195 cmp
= isl_basic_set_plain_cmp(set1
->p
[i
], set2
->p
[i
]);
9203 isl_bool
isl_basic_map_plain_is_equal(__isl_keep isl_basic_map
*bmap1
,
9204 __isl_keep isl_basic_map
*bmap2
)
9206 if (!bmap1
|| !bmap2
)
9207 return isl_bool_error
;
9208 return isl_basic_map_plain_cmp(bmap1
, bmap2
) == 0;
9211 isl_bool
isl_basic_set_plain_is_equal(__isl_keep isl_basic_set
*bset1
,
9212 __isl_keep isl_basic_set
*bset2
)
9214 return isl_basic_map_plain_is_equal((isl_basic_map
*)bset1
,
9215 (isl_basic_map
*)bset2
);
9218 static int qsort_bmap_cmp(const void *p1
, const void *p2
)
9220 const struct isl_basic_map
*bmap1
= *(const struct isl_basic_map
**)p1
;
9221 const struct isl_basic_map
*bmap2
= *(const struct isl_basic_map
**)p2
;
9223 return isl_basic_map_plain_cmp(bmap1
, bmap2
);
9226 /* Sort the basic maps of "map" and remove duplicate basic maps.
9228 * While removing basic maps, we make sure that the basic maps remain
9229 * sorted because isl_map_normalize expects the basic maps of the result
9232 static __isl_give isl_map
*sort_and_remove_duplicates(__isl_take isl_map
*map
)
9236 map
= isl_map_remove_empty_parts(map
);
9239 qsort(map
->p
, map
->n
, sizeof(struct isl_basic_map
*), qsort_bmap_cmp
);
9240 for (i
= map
->n
- 1; i
>= 1; --i
) {
9241 if (!isl_basic_map_plain_is_equal(map
->p
[i
- 1], map
->p
[i
]))
9243 isl_basic_map_free(map
->p
[i
-1]);
9244 for (j
= i
; j
< map
->n
; ++j
)
9245 map
->p
[j
- 1] = map
->p
[j
];
9252 /* Remove obvious duplicates among the basic maps of "map".
9254 * Unlike isl_map_normalize, this function does not remove redundant
9255 * constraints and only removes duplicates that have exactly the same
9256 * constraints in the input. It does sort the constraints and
9257 * the basic maps to ease the detection of duplicates.
9259 * If "map" has already been normalized or if the basic maps are
9260 * disjoint, then there can be no duplicates.
9262 __isl_give isl_map
*isl_map_remove_obvious_duplicates(__isl_take isl_map
*map
)
9265 isl_basic_map
*bmap
;
9271 if (ISL_F_ISSET(map
, ISL_MAP_NORMALIZED
| ISL_MAP_DISJOINT
))
9273 for (i
= 0; i
< map
->n
; ++i
) {
9274 bmap
= isl_basic_map_copy(map
->p
[i
]);
9275 bmap
= isl_basic_map_sort_constraints(bmap
);
9277 return isl_map_free(map
);
9278 isl_basic_map_free(map
->p
[i
]);
9282 map
= sort_and_remove_duplicates(map
);
9286 /* We normalize in place, but if anything goes wrong we need
9287 * to return NULL, so we need to make sure we don't change the
9288 * meaning of any possible other copies of map.
9290 __isl_give isl_map
*isl_map_normalize(__isl_take isl_map
*map
)
9293 struct isl_basic_map
*bmap
;
9297 if (ISL_F_ISSET(map
, ISL_MAP_NORMALIZED
))
9299 for (i
= 0; i
< map
->n
; ++i
) {
9300 bmap
= isl_basic_map_normalize(isl_basic_map_copy(map
->p
[i
]));
9303 isl_basic_map_free(map
->p
[i
]);
9307 map
= sort_and_remove_duplicates(map
);
9309 ISL_F_SET(map
, ISL_MAP_NORMALIZED
);
9316 struct isl_set
*isl_set_normalize(struct isl_set
*set
)
9318 return (struct isl_set
*)isl_map_normalize((struct isl_map
*)set
);
9321 isl_bool
isl_map_plain_is_equal(__isl_keep isl_map
*map1
,
9322 __isl_keep isl_map
*map2
)
9328 return isl_bool_error
;
9331 return isl_bool_true
;
9332 if (!isl_space_is_equal(map1
->dim
, map2
->dim
))
9333 return isl_bool_false
;
9335 map1
= isl_map_copy(map1
);
9336 map2
= isl_map_copy(map2
);
9337 map1
= isl_map_normalize(map1
);
9338 map2
= isl_map_normalize(map2
);
9341 equal
= map1
->n
== map2
->n
;
9342 for (i
= 0; equal
&& i
< map1
->n
; ++i
) {
9343 equal
= isl_basic_map_plain_is_equal(map1
->p
[i
], map2
->p
[i
]);
9353 return isl_bool_error
;
9356 isl_bool
isl_set_plain_is_equal(__isl_keep isl_set
*set1
,
9357 __isl_keep isl_set
*set2
)
9359 return isl_map_plain_is_equal((struct isl_map
*)set1
,
9360 (struct isl_map
*)set2
);
9363 /* Return an interval that ranges from min to max (inclusive)
9365 struct isl_basic_set
*isl_basic_set_interval(struct isl_ctx
*ctx
,
9366 isl_int min
, isl_int max
)
9369 struct isl_basic_set
*bset
= NULL
;
9371 bset
= isl_basic_set_alloc(ctx
, 0, 1, 0, 0, 2);
9375 k
= isl_basic_set_alloc_inequality(bset
);
9378 isl_int_set_si(bset
->ineq
[k
][1], 1);
9379 isl_int_neg(bset
->ineq
[k
][0], min
);
9381 k
= isl_basic_set_alloc_inequality(bset
);
9384 isl_int_set_si(bset
->ineq
[k
][1], -1);
9385 isl_int_set(bset
->ineq
[k
][0], max
);
9389 isl_basic_set_free(bset
);
9393 /* Return the basic maps in "map" as a list.
9395 __isl_give isl_basic_map_list
*isl_map_get_basic_map_list(
9396 __isl_keep isl_map
*map
)
9400 isl_basic_map_list
*list
;
9404 ctx
= isl_map_get_ctx(map
);
9405 list
= isl_basic_map_list_alloc(ctx
, map
->n
);
9407 for (i
= 0; i
< map
->n
; ++i
) {
9408 isl_basic_map
*bmap
;
9410 bmap
= isl_basic_map_copy(map
->p
[i
]);
9411 list
= isl_basic_map_list_add(list
, bmap
);
9417 /* Return the intersection of the elements in the non-empty list "list".
9418 * All elements are assumed to live in the same space.
9420 __isl_give isl_basic_map
*isl_basic_map_list_intersect(
9421 __isl_take isl_basic_map_list
*list
)
9424 isl_basic_map
*bmap
;
9428 n
= isl_basic_map_list_n_basic_map(list
);
9430 isl_die(isl_basic_map_list_get_ctx(list
), isl_error_invalid
,
9431 "expecting non-empty list", goto error
);
9433 bmap
= isl_basic_map_list_get_basic_map(list
, 0);
9434 for (i
= 1; i
< n
; ++i
) {
9435 isl_basic_map
*bmap_i
;
9437 bmap_i
= isl_basic_map_list_get_basic_map(list
, i
);
9438 bmap
= isl_basic_map_intersect(bmap
, bmap_i
);
9441 isl_basic_map_list_free(list
);
9444 isl_basic_map_list_free(list
);
9448 /* Return the intersection of the elements in the non-empty list "list".
9449 * All elements are assumed to live in the same space.
9451 __isl_give isl_basic_set
*isl_basic_set_list_intersect(
9452 __isl_take isl_basic_set_list
*list
)
9454 return isl_basic_map_list_intersect(list
);
9457 /* Return the union of the elements in the non-empty list "list".
9458 * All elements are assumed to live in the same space.
9460 __isl_give isl_set
*isl_set_list_union(__isl_take isl_set_list
*list
)
9467 n
= isl_set_list_n_set(list
);
9469 isl_die(isl_set_list_get_ctx(list
), isl_error_invalid
,
9470 "expecting non-empty list", goto error
);
9472 set
= isl_set_list_get_set(list
, 0);
9473 for (i
= 1; i
< n
; ++i
) {
9476 set_i
= isl_set_list_get_set(list
, i
);
9477 set
= isl_set_union(set
, set_i
);
9480 isl_set_list_free(list
);
9483 isl_set_list_free(list
);
9487 /* Return the Cartesian product of the basic sets in list (in the given order).
9489 __isl_give isl_basic_set
*isl_basic_set_list_product(
9490 __isl_take
struct isl_basic_set_list
*list
)
9498 struct isl_basic_set
*product
= NULL
;
9502 isl_assert(list
->ctx
, list
->n
> 0, goto error
);
9503 isl_assert(list
->ctx
, list
->p
[0], goto error
);
9504 nparam
= isl_basic_set_n_param(list
->p
[0]);
9505 dim
= isl_basic_set_n_dim(list
->p
[0]);
9506 extra
= list
->p
[0]->n_div
;
9507 n_eq
= list
->p
[0]->n_eq
;
9508 n_ineq
= list
->p
[0]->n_ineq
;
9509 for (i
= 1; i
< list
->n
; ++i
) {
9510 isl_assert(list
->ctx
, list
->p
[i
], goto error
);
9511 isl_assert(list
->ctx
,
9512 nparam
== isl_basic_set_n_param(list
->p
[i
]), goto error
);
9513 dim
+= isl_basic_set_n_dim(list
->p
[i
]);
9514 extra
+= list
->p
[i
]->n_div
;
9515 n_eq
+= list
->p
[i
]->n_eq
;
9516 n_ineq
+= list
->p
[i
]->n_ineq
;
9518 product
= isl_basic_set_alloc(list
->ctx
, nparam
, dim
, extra
,
9523 for (i
= 0; i
< list
->n
; ++i
) {
9524 isl_basic_set_add_constraints(product
,
9525 isl_basic_set_copy(list
->p
[i
]), dim
);
9526 dim
+= isl_basic_set_n_dim(list
->p
[i
]);
9528 isl_basic_set_list_free(list
);
9531 isl_basic_set_free(product
);
9532 isl_basic_set_list_free(list
);
9536 struct isl_basic_map
*isl_basic_map_product(
9537 struct isl_basic_map
*bmap1
, struct isl_basic_map
*bmap2
)
9539 isl_space
*dim_result
= NULL
;
9540 struct isl_basic_map
*bmap
;
9541 unsigned in1
, in2
, out1
, out2
, nparam
, total
, pos
;
9542 struct isl_dim_map
*dim_map1
, *dim_map2
;
9544 if (!bmap1
|| !bmap2
)
9547 isl_assert(bmap1
->ctx
, isl_space_match(bmap1
->dim
, isl_dim_param
,
9548 bmap2
->dim
, isl_dim_param
), goto error
);
9549 dim_result
= isl_space_product(isl_space_copy(bmap1
->dim
),
9550 isl_space_copy(bmap2
->dim
));
9552 in1
= isl_basic_map_n_in(bmap1
);
9553 in2
= isl_basic_map_n_in(bmap2
);
9554 out1
= isl_basic_map_n_out(bmap1
);
9555 out2
= isl_basic_map_n_out(bmap2
);
9556 nparam
= isl_basic_map_n_param(bmap1
);
9558 total
= nparam
+ in1
+ in2
+ out1
+ out2
+ bmap1
->n_div
+ bmap2
->n_div
;
9559 dim_map1
= isl_dim_map_alloc(bmap1
->ctx
, total
);
9560 dim_map2
= isl_dim_map_alloc(bmap1
->ctx
, total
);
9561 isl_dim_map_dim(dim_map1
, bmap1
->dim
, isl_dim_param
, pos
= 0);
9562 isl_dim_map_dim(dim_map2
, bmap2
->dim
, isl_dim_param
, pos
= 0);
9563 isl_dim_map_dim(dim_map1
, bmap1
->dim
, isl_dim_in
, pos
+= nparam
);
9564 isl_dim_map_dim(dim_map2
, bmap2
->dim
, isl_dim_in
, pos
+= in1
);
9565 isl_dim_map_dim(dim_map1
, bmap1
->dim
, isl_dim_out
, pos
+= in2
);
9566 isl_dim_map_dim(dim_map2
, bmap2
->dim
, isl_dim_out
, pos
+= out1
);
9567 isl_dim_map_div(dim_map1
, bmap1
, pos
+= out2
);
9568 isl_dim_map_div(dim_map2
, bmap2
, pos
+= bmap1
->n_div
);
9570 bmap
= isl_basic_map_alloc_space(dim_result
,
9571 bmap1
->n_div
+ bmap2
->n_div
,
9572 bmap1
->n_eq
+ bmap2
->n_eq
,
9573 bmap1
->n_ineq
+ bmap2
->n_ineq
);
9574 bmap
= isl_basic_map_add_constraints_dim_map(bmap
, bmap1
, dim_map1
);
9575 bmap
= isl_basic_map_add_constraints_dim_map(bmap
, bmap2
, dim_map2
);
9576 bmap
= isl_basic_map_simplify(bmap
);
9577 return isl_basic_map_finalize(bmap
);
9579 isl_basic_map_free(bmap1
);
9580 isl_basic_map_free(bmap2
);
9584 __isl_give isl_basic_map
*isl_basic_map_flat_product(
9585 __isl_take isl_basic_map
*bmap1
, __isl_take isl_basic_map
*bmap2
)
9587 isl_basic_map
*prod
;
9589 prod
= isl_basic_map_product(bmap1
, bmap2
);
9590 prod
= isl_basic_map_flatten(prod
);
9594 __isl_give isl_basic_set
*isl_basic_set_flat_product(
9595 __isl_take isl_basic_set
*bset1
, __isl_take isl_basic_set
*bset2
)
9597 return isl_basic_map_flat_range_product(bset1
, bset2
);
9600 __isl_give isl_basic_map
*isl_basic_map_domain_product(
9601 __isl_take isl_basic_map
*bmap1
, __isl_take isl_basic_map
*bmap2
)
9603 isl_space
*space_result
= NULL
;
9604 isl_basic_map
*bmap
;
9605 unsigned in1
, in2
, out
, nparam
, total
, pos
;
9606 struct isl_dim_map
*dim_map1
, *dim_map2
;
9608 if (!bmap1
|| !bmap2
)
9611 space_result
= isl_space_domain_product(isl_space_copy(bmap1
->dim
),
9612 isl_space_copy(bmap2
->dim
));
9614 in1
= isl_basic_map_dim(bmap1
, isl_dim_in
);
9615 in2
= isl_basic_map_dim(bmap2
, isl_dim_in
);
9616 out
= isl_basic_map_dim(bmap1
, isl_dim_out
);
9617 nparam
= isl_basic_map_dim(bmap1
, isl_dim_param
);
9619 total
= nparam
+ in1
+ in2
+ out
+ bmap1
->n_div
+ bmap2
->n_div
;
9620 dim_map1
= isl_dim_map_alloc(bmap1
->ctx
, total
);
9621 dim_map2
= isl_dim_map_alloc(bmap1
->ctx
, total
);
9622 isl_dim_map_dim(dim_map1
, bmap1
->dim
, isl_dim_param
, pos
= 0);
9623 isl_dim_map_dim(dim_map2
, bmap2
->dim
, isl_dim_param
, pos
= 0);
9624 isl_dim_map_dim(dim_map1
, bmap1
->dim
, isl_dim_in
, pos
+= nparam
);
9625 isl_dim_map_dim(dim_map2
, bmap2
->dim
, isl_dim_in
, pos
+= in1
);
9626 isl_dim_map_dim(dim_map1
, bmap1
->dim
, isl_dim_out
, pos
+= in2
);
9627 isl_dim_map_dim(dim_map2
, bmap2
->dim
, isl_dim_out
, pos
);
9628 isl_dim_map_div(dim_map1
, bmap1
, pos
+= out
);
9629 isl_dim_map_div(dim_map2
, bmap2
, pos
+= bmap1
->n_div
);
9631 bmap
= isl_basic_map_alloc_space(space_result
,
9632 bmap1
->n_div
+ bmap2
->n_div
,
9633 bmap1
->n_eq
+ bmap2
->n_eq
,
9634 bmap1
->n_ineq
+ bmap2
->n_ineq
);
9635 bmap
= isl_basic_map_add_constraints_dim_map(bmap
, bmap1
, dim_map1
);
9636 bmap
= isl_basic_map_add_constraints_dim_map(bmap
, bmap2
, dim_map2
);
9637 bmap
= isl_basic_map_simplify(bmap
);
9638 return isl_basic_map_finalize(bmap
);
9640 isl_basic_map_free(bmap1
);
9641 isl_basic_map_free(bmap2
);
9645 __isl_give isl_basic_map
*isl_basic_map_range_product(
9646 __isl_take isl_basic_map
*bmap1
, __isl_take isl_basic_map
*bmap2
)
9649 isl_space
*dim_result
= NULL
;
9650 isl_basic_map
*bmap
;
9651 unsigned in
, out1
, out2
, nparam
, total
, pos
;
9652 struct isl_dim_map
*dim_map1
, *dim_map2
;
9654 rational
= isl_basic_map_is_rational(bmap1
);
9655 if (rational
>= 0 && rational
)
9656 rational
= isl_basic_map_is_rational(bmap2
);
9657 if (!bmap1
|| !bmap2
|| rational
< 0)
9660 if (!isl_space_match(bmap1
->dim
, isl_dim_param
,
9661 bmap2
->dim
, isl_dim_param
))
9662 isl_die(isl_basic_map_get_ctx(bmap1
), isl_error_invalid
,
9663 "parameters don't match", goto error
);
9665 dim_result
= isl_space_range_product(isl_space_copy(bmap1
->dim
),
9666 isl_space_copy(bmap2
->dim
));
9668 in
= isl_basic_map_dim(bmap1
, isl_dim_in
);
9669 out1
= isl_basic_map_n_out(bmap1
);
9670 out2
= isl_basic_map_n_out(bmap2
);
9671 nparam
= isl_basic_map_n_param(bmap1
);
9673 total
= nparam
+ in
+ out1
+ out2
+ bmap1
->n_div
+ bmap2
->n_div
;
9674 dim_map1
= isl_dim_map_alloc(bmap1
->ctx
, total
);
9675 dim_map2
= isl_dim_map_alloc(bmap1
->ctx
, total
);
9676 isl_dim_map_dim(dim_map1
, bmap1
->dim
, isl_dim_param
, pos
= 0);
9677 isl_dim_map_dim(dim_map2
, bmap2
->dim
, isl_dim_param
, pos
= 0);
9678 isl_dim_map_dim(dim_map1
, bmap1
->dim
, isl_dim_in
, pos
+= nparam
);
9679 isl_dim_map_dim(dim_map2
, bmap2
->dim
, isl_dim_in
, pos
);
9680 isl_dim_map_dim(dim_map1
, bmap1
->dim
, isl_dim_out
, pos
+= in
);
9681 isl_dim_map_dim(dim_map2
, bmap2
->dim
, isl_dim_out
, pos
+= out1
);
9682 isl_dim_map_div(dim_map1
, bmap1
, pos
+= out2
);
9683 isl_dim_map_div(dim_map2
, bmap2
, pos
+= bmap1
->n_div
);
9685 bmap
= isl_basic_map_alloc_space(dim_result
,
9686 bmap1
->n_div
+ bmap2
->n_div
,
9687 bmap1
->n_eq
+ bmap2
->n_eq
,
9688 bmap1
->n_ineq
+ bmap2
->n_ineq
);
9689 bmap
= isl_basic_map_add_constraints_dim_map(bmap
, bmap1
, dim_map1
);
9690 bmap
= isl_basic_map_add_constraints_dim_map(bmap
, bmap2
, dim_map2
);
9692 bmap
= isl_basic_map_set_rational(bmap
);
9693 bmap
= isl_basic_map_simplify(bmap
);
9694 return isl_basic_map_finalize(bmap
);
9696 isl_basic_map_free(bmap1
);
9697 isl_basic_map_free(bmap2
);
9701 __isl_give isl_basic_map
*isl_basic_map_flat_range_product(
9702 __isl_take isl_basic_map
*bmap1
, __isl_take isl_basic_map
*bmap2
)
9704 isl_basic_map
*prod
;
9706 prod
= isl_basic_map_range_product(bmap1
, bmap2
);
9707 prod
= isl_basic_map_flatten_range(prod
);
9711 /* Apply "basic_map_product" to each pair of basic maps in "map1" and "map2"
9712 * and collect the results.
9713 * The result live in the space obtained by calling "space_product"
9714 * on the spaces of "map1" and "map2".
9715 * If "remove_duplicates" is set then the result may contain duplicates
9716 * (even if the inputs do not) and so we try and remove the obvious
9719 static __isl_give isl_map
*map_product(__isl_take isl_map
*map1
,
9720 __isl_take isl_map
*map2
,
9721 __isl_give isl_space
*(*space_product
)(__isl_take isl_space
*left
,
9722 __isl_take isl_space
*right
),
9723 __isl_give isl_basic_map
*(*basic_map_product
)(
9724 __isl_take isl_basic_map
*left
,
9725 __isl_take isl_basic_map
*right
),
9726 int remove_duplicates
)
9729 struct isl_map
*result
;
9735 isl_assert(map1
->ctx
, isl_space_match(map1
->dim
, isl_dim_param
,
9736 map2
->dim
, isl_dim_param
), goto error
);
9738 if (ISL_F_ISSET(map1
, ISL_MAP_DISJOINT
) &&
9739 ISL_F_ISSET(map2
, ISL_MAP_DISJOINT
))
9740 ISL_FL_SET(flags
, ISL_MAP_DISJOINT
);
9742 result
= isl_map_alloc_space(space_product(isl_space_copy(map1
->dim
),
9743 isl_space_copy(map2
->dim
)),
9744 map1
->n
* map2
->n
, flags
);
9747 for (i
= 0; i
< map1
->n
; ++i
)
9748 for (j
= 0; j
< map2
->n
; ++j
) {
9749 struct isl_basic_map
*part
;
9750 part
= basic_map_product(isl_basic_map_copy(map1
->p
[i
]),
9751 isl_basic_map_copy(map2
->p
[j
]));
9752 if (isl_basic_map_is_empty(part
))
9753 isl_basic_map_free(part
);
9755 result
= isl_map_add_basic_map(result
, part
);
9759 if (remove_duplicates
)
9760 result
= isl_map_remove_obvious_duplicates(result
);
9770 /* Given two maps A -> B and C -> D, construct a map [A -> C] -> [B -> D]
9772 static __isl_give isl_map
*map_product_aligned(__isl_take isl_map
*map1
,
9773 __isl_take isl_map
*map2
)
9775 return map_product(map1
, map2
, &isl_space_product
,
9776 &isl_basic_map_product
, 0);
9779 __isl_give isl_map
*isl_map_product(__isl_take isl_map
*map1
,
9780 __isl_take isl_map
*map2
)
9782 return isl_map_align_params_map_map_and(map1
, map2
, &map_product_aligned
);
9785 /* Given two maps A -> B and C -> D, construct a map (A, C) -> (B, D)
9787 __isl_give isl_map
*isl_map_flat_product(__isl_take isl_map
*map1
,
9788 __isl_take isl_map
*map2
)
9792 prod
= isl_map_product(map1
, map2
);
9793 prod
= isl_map_flatten(prod
);
9797 /* Given two set A and B, construct its Cartesian product A x B.
9799 struct isl_set
*isl_set_product(struct isl_set
*set1
, struct isl_set
*set2
)
9801 return isl_map_range_product(set1
, set2
);
9804 __isl_give isl_set
*isl_set_flat_product(__isl_take isl_set
*set1
,
9805 __isl_take isl_set
*set2
)
9807 return isl_map_flat_range_product(set1
, set2
);
9810 /* Given two maps A -> B and C -> D, construct a map [A -> C] -> (B * D)
9812 static __isl_give isl_map
*map_domain_product_aligned(__isl_take isl_map
*map1
,
9813 __isl_take isl_map
*map2
)
9815 return map_product(map1
, map2
, &isl_space_domain_product
,
9816 &isl_basic_map_domain_product
, 1);
9819 /* Given two maps A -> B and C -> D, construct a map (A * C) -> [B -> D]
9821 static __isl_give isl_map
*map_range_product_aligned(__isl_take isl_map
*map1
,
9822 __isl_take isl_map
*map2
)
9824 return map_product(map1
, map2
, &isl_space_range_product
,
9825 &isl_basic_map_range_product
, 1);
9828 __isl_give isl_map
*isl_map_domain_product(__isl_take isl_map
*map1
,
9829 __isl_take isl_map
*map2
)
9831 return isl_map_align_params_map_map_and(map1
, map2
,
9832 &map_domain_product_aligned
);
9835 __isl_give isl_map
*isl_map_range_product(__isl_take isl_map
*map1
,
9836 __isl_take isl_map
*map2
)
9838 return isl_map_align_params_map_map_and(map1
, map2
,
9839 &map_range_product_aligned
);
9842 /* Given a map of the form [A -> B] -> [C -> D], return the map A -> C.
9844 __isl_give isl_map
*isl_map_factor_domain(__isl_take isl_map
*map
)
9847 int total1
, keep1
, total2
, keep2
;
9851 if (!isl_space_domain_is_wrapping(map
->dim
) ||
9852 !isl_space_range_is_wrapping(map
->dim
))
9853 isl_die(isl_map_get_ctx(map
), isl_error_invalid
,
9854 "not a product", return isl_map_free(map
));
9856 space
= isl_map_get_space(map
);
9857 total1
= isl_space_dim(space
, isl_dim_in
);
9858 total2
= isl_space_dim(space
, isl_dim_out
);
9859 space
= isl_space_factor_domain(space
);
9860 keep1
= isl_space_dim(space
, isl_dim_in
);
9861 keep2
= isl_space_dim(space
, isl_dim_out
);
9862 map
= isl_map_project_out(map
, isl_dim_in
, keep1
, total1
- keep1
);
9863 map
= isl_map_project_out(map
, isl_dim_out
, keep2
, total2
- keep2
);
9864 map
= isl_map_reset_space(map
, space
);
9869 /* Given a map of the form [A -> B] -> [C -> D], return the map B -> D.
9871 __isl_give isl_map
*isl_map_factor_range(__isl_take isl_map
*map
)
9874 int total1
, keep1
, total2
, keep2
;
9878 if (!isl_space_domain_is_wrapping(map
->dim
) ||
9879 !isl_space_range_is_wrapping(map
->dim
))
9880 isl_die(isl_map_get_ctx(map
), isl_error_invalid
,
9881 "not a product", return isl_map_free(map
));
9883 space
= isl_map_get_space(map
);
9884 total1
= isl_space_dim(space
, isl_dim_in
);
9885 total2
= isl_space_dim(space
, isl_dim_out
);
9886 space
= isl_space_factor_range(space
);
9887 keep1
= isl_space_dim(space
, isl_dim_in
);
9888 keep2
= isl_space_dim(space
, isl_dim_out
);
9889 map
= isl_map_project_out(map
, isl_dim_in
, 0, total1
- keep1
);
9890 map
= isl_map_project_out(map
, isl_dim_out
, 0, total2
- keep2
);
9891 map
= isl_map_reset_space(map
, space
);
9896 /* Given a map of the form [A -> B] -> C, return the map A -> C.
9898 __isl_give isl_map
*isl_map_domain_factor_domain(__isl_take isl_map
*map
)
9905 if (!isl_space_domain_is_wrapping(map
->dim
))
9906 isl_die(isl_map_get_ctx(map
), isl_error_invalid
,
9907 "domain is not a product", return isl_map_free(map
));
9909 space
= isl_map_get_space(map
);
9910 total
= isl_space_dim(space
, isl_dim_in
);
9911 space
= isl_space_domain_factor_domain(space
);
9912 keep
= isl_space_dim(space
, isl_dim_in
);
9913 map
= isl_map_project_out(map
, isl_dim_in
, keep
, total
- keep
);
9914 map
= isl_map_reset_space(map
, space
);
9919 /* Given a map of the form [A -> B] -> C, return the map B -> C.
9921 __isl_give isl_map
*isl_map_domain_factor_range(__isl_take isl_map
*map
)
9928 if (!isl_space_domain_is_wrapping(map
->dim
))
9929 isl_die(isl_map_get_ctx(map
), isl_error_invalid
,
9930 "domain is not a product", return isl_map_free(map
));
9932 space
= isl_map_get_space(map
);
9933 total
= isl_space_dim(space
, isl_dim_in
);
9934 space
= isl_space_domain_factor_range(space
);
9935 keep
= isl_space_dim(space
, isl_dim_in
);
9936 map
= isl_map_project_out(map
, isl_dim_in
, 0, total
- keep
);
9937 map
= isl_map_reset_space(map
, space
);
9942 /* Given a map A -> [B -> C], extract the map A -> B.
9944 __isl_give isl_map
*isl_map_range_factor_domain(__isl_take isl_map
*map
)
9951 if (!isl_space_range_is_wrapping(map
->dim
))
9952 isl_die(isl_map_get_ctx(map
), isl_error_invalid
,
9953 "range is not a product", return isl_map_free(map
));
9955 space
= isl_map_get_space(map
);
9956 total
= isl_space_dim(space
, isl_dim_out
);
9957 space
= isl_space_range_factor_domain(space
);
9958 keep
= isl_space_dim(space
, isl_dim_out
);
9959 map
= isl_map_project_out(map
, isl_dim_out
, keep
, total
- keep
);
9960 map
= isl_map_reset_space(map
, space
);
9965 /* Given a map A -> [B -> C], extract the map A -> C.
9967 __isl_give isl_map
*isl_map_range_factor_range(__isl_take isl_map
*map
)
9974 if (!isl_space_range_is_wrapping(map
->dim
))
9975 isl_die(isl_map_get_ctx(map
), isl_error_invalid
,
9976 "range is not a product", return isl_map_free(map
));
9978 space
= isl_map_get_space(map
);
9979 total
= isl_space_dim(space
, isl_dim_out
);
9980 space
= isl_space_range_factor_range(space
);
9981 keep
= isl_space_dim(space
, isl_dim_out
);
9982 map
= isl_map_project_out(map
, isl_dim_out
, 0, total
- keep
);
9983 map
= isl_map_reset_space(map
, space
);
9988 /* Given two maps A -> B and C -> D, construct a map (A, C) -> (B * D)
9990 __isl_give isl_map
*isl_map_flat_domain_product(__isl_take isl_map
*map1
,
9991 __isl_take isl_map
*map2
)
9995 prod
= isl_map_domain_product(map1
, map2
);
9996 prod
= isl_map_flatten_domain(prod
);
10000 /* Given two maps A -> B and C -> D, construct a map (A * C) -> (B, D)
10002 __isl_give isl_map
*isl_map_flat_range_product(__isl_take isl_map
*map1
,
10003 __isl_take isl_map
*map2
)
10007 prod
= isl_map_range_product(map1
, map2
);
10008 prod
= isl_map_flatten_range(prod
);
10012 uint32_t isl_basic_map_get_hash(__isl_keep isl_basic_map
*bmap
)
10015 uint32_t hash
= isl_hash_init();
10020 bmap
= isl_basic_map_copy(bmap
);
10021 bmap
= isl_basic_map_normalize(bmap
);
10024 total
= isl_basic_map_total_dim(bmap
);
10025 isl_hash_byte(hash
, bmap
->n_eq
& 0xFF);
10026 for (i
= 0; i
< bmap
->n_eq
; ++i
) {
10028 c_hash
= isl_seq_get_hash(bmap
->eq
[i
], 1 + total
);
10029 isl_hash_hash(hash
, c_hash
);
10031 isl_hash_byte(hash
, bmap
->n_ineq
& 0xFF);
10032 for (i
= 0; i
< bmap
->n_ineq
; ++i
) {
10034 c_hash
= isl_seq_get_hash(bmap
->ineq
[i
], 1 + total
);
10035 isl_hash_hash(hash
, c_hash
);
10037 isl_hash_byte(hash
, bmap
->n_div
& 0xFF);
10038 for (i
= 0; i
< bmap
->n_div
; ++i
) {
10040 if (isl_int_is_zero(bmap
->div
[i
][0]))
10042 isl_hash_byte(hash
, i
& 0xFF);
10043 c_hash
= isl_seq_get_hash(bmap
->div
[i
], 1 + 1 + total
);
10044 isl_hash_hash(hash
, c_hash
);
10046 isl_basic_map_free(bmap
);
10050 uint32_t isl_basic_set_get_hash(__isl_keep isl_basic_set
*bset
)
10052 return isl_basic_map_get_hash((isl_basic_map
*)bset
);
10055 uint32_t isl_map_get_hash(__isl_keep isl_map
*map
)
10062 map
= isl_map_copy(map
);
10063 map
= isl_map_normalize(map
);
10067 hash
= isl_hash_init();
10068 for (i
= 0; i
< map
->n
; ++i
) {
10069 uint32_t bmap_hash
;
10070 bmap_hash
= isl_basic_map_get_hash(map
->p
[i
]);
10071 isl_hash_hash(hash
, bmap_hash
);
10079 uint32_t isl_set_get_hash(__isl_keep isl_set
*set
)
10081 return isl_map_get_hash((isl_map
*)set
);
10084 /* Check if the value for dimension dim is completely determined
10085 * by the values of the other parameters and variables.
10086 * That is, check if dimension dim is involved in an equality.
10088 int isl_basic_set_dim_is_unique(struct isl_basic_set
*bset
, unsigned dim
)
10095 nparam
= isl_basic_set_n_param(bset
);
10096 for (i
= 0; i
< bset
->n_eq
; ++i
)
10097 if (!isl_int_is_zero(bset
->eq
[i
][1 + nparam
+ dim
]))
10102 /* Check if the value for dimension dim is completely determined
10103 * by the values of the other parameters and variables.
10104 * That is, check if dimension dim is involved in an equality
10105 * for each of the subsets.
10107 int isl_set_dim_is_unique(struct isl_set
*set
, unsigned dim
)
10113 for (i
= 0; i
< set
->n
; ++i
) {
10115 unique
= isl_basic_set_dim_is_unique(set
->p
[i
], dim
);
10122 /* Return the number of basic maps in the (current) representation of "map".
10124 int isl_map_n_basic_map(__isl_keep isl_map
*map
)
10126 return map
? map
->n
: 0;
10129 int isl_set_n_basic_set(__isl_keep isl_set
*set
)
10131 return set
? set
->n
: 0;
10134 isl_stat
isl_map_foreach_basic_map(__isl_keep isl_map
*map
,
10135 isl_stat (*fn
)(__isl_take isl_basic_map
*bmap
, void *user
), void *user
)
10140 return isl_stat_error
;
10142 for (i
= 0; i
< map
->n
; ++i
)
10143 if (fn(isl_basic_map_copy(map
->p
[i
]), user
) < 0)
10144 return isl_stat_error
;
10146 return isl_stat_ok
;
10149 isl_stat
isl_set_foreach_basic_set(__isl_keep isl_set
*set
,
10150 isl_stat (*fn
)(__isl_take isl_basic_set
*bset
, void *user
), void *user
)
10155 return isl_stat_error
;
10157 for (i
= 0; i
< set
->n
; ++i
)
10158 if (fn(isl_basic_set_copy(set
->p
[i
]), user
) < 0)
10159 return isl_stat_error
;
10161 return isl_stat_ok
;
10164 /* Return a list of basic sets, the union of which is equal to "set".
10166 __isl_give isl_basic_set_list
*isl_set_get_basic_set_list(
10167 __isl_keep isl_set
*set
)
10170 isl_basic_set_list
*list
;
10175 list
= isl_basic_set_list_alloc(isl_set_get_ctx(set
), set
->n
);
10176 for (i
= 0; i
< set
->n
; ++i
) {
10177 isl_basic_set
*bset
;
10179 bset
= isl_basic_set_copy(set
->p
[i
]);
10180 list
= isl_basic_set_list_add(list
, bset
);
10186 __isl_give isl_basic_set
*isl_basic_set_lift(__isl_take isl_basic_set
*bset
)
10193 bset
= isl_basic_set_cow(bset
);
10197 dim
= isl_basic_set_get_space(bset
);
10198 dim
= isl_space_lift(dim
, bset
->n_div
);
10201 isl_space_free(bset
->dim
);
10203 bset
->extra
-= bset
->n_div
;
10206 bset
= isl_basic_set_finalize(bset
);
10210 isl_basic_set_free(bset
);
10214 __isl_give isl_set
*isl_set_lift(__isl_take isl_set
*set
)
10220 set
= isl_set_align_divs(set
);
10225 set
= isl_set_cow(set
);
10229 n_div
= set
->p
[0]->n_div
;
10230 dim
= isl_set_get_space(set
);
10231 dim
= isl_space_lift(dim
, n_div
);
10234 isl_space_free(set
->dim
);
10237 for (i
= 0; i
< set
->n
; ++i
) {
10238 set
->p
[i
] = isl_basic_set_lift(set
->p
[i
]);
10249 __isl_give isl_map
*isl_set_lifting(__isl_take isl_set
*set
)
10252 struct isl_basic_map
*bmap
;
10259 set
= isl_set_align_divs(set
);
10264 dim
= isl_set_get_space(set
);
10265 if (set
->n
== 0 || set
->p
[0]->n_div
== 0) {
10267 return isl_map_identity(isl_space_map_from_set(dim
));
10270 n_div
= set
->p
[0]->n_div
;
10271 dim
= isl_space_map_from_set(dim
);
10272 n_param
= isl_space_dim(dim
, isl_dim_param
);
10273 n_set
= isl_space_dim(dim
, isl_dim_in
);
10274 dim
= isl_space_extend(dim
, n_param
, n_set
, n_set
+ n_div
);
10275 bmap
= isl_basic_map_alloc_space(dim
, 0, n_set
, 2 * n_div
);
10276 for (i
= 0; i
< n_set
; ++i
)
10277 bmap
= var_equal(bmap
, i
);
10279 total
= n_param
+ n_set
+ n_set
+ n_div
;
10280 for (i
= 0; i
< n_div
; ++i
) {
10281 k
= isl_basic_map_alloc_inequality(bmap
);
10284 isl_seq_cpy(bmap
->ineq
[k
], set
->p
[0]->div
[i
]+1, 1+n_param
);
10285 isl_seq_clr(bmap
->ineq
[k
]+1+n_param
, n_set
);
10286 isl_seq_cpy(bmap
->ineq
[k
]+1+n_param
+n_set
,
10287 set
->p
[0]->div
[i
]+1+1+n_param
, n_set
+ n_div
);
10288 isl_int_neg(bmap
->ineq
[k
][1+n_param
+n_set
+n_set
+i
],
10289 set
->p
[0]->div
[i
][0]);
10291 l
= isl_basic_map_alloc_inequality(bmap
);
10294 isl_seq_neg(bmap
->ineq
[l
], bmap
->ineq
[k
], 1 + total
);
10295 isl_int_add(bmap
->ineq
[l
][0], bmap
->ineq
[l
][0],
10296 set
->p
[0]->div
[i
][0]);
10297 isl_int_sub_ui(bmap
->ineq
[l
][0], bmap
->ineq
[l
][0], 1);
10301 bmap
= isl_basic_map_simplify(bmap
);
10302 bmap
= isl_basic_map_finalize(bmap
);
10303 return isl_map_from_basic_map(bmap
);
10306 isl_basic_map_free(bmap
);
10310 int isl_basic_set_size(__isl_keep isl_basic_set
*bset
)
10318 dim
= isl_basic_set_total_dim(bset
);
10319 size
+= bset
->n_eq
* (1 + dim
);
10320 size
+= bset
->n_ineq
* (1 + dim
);
10321 size
+= bset
->n_div
* (2 + dim
);
10326 int isl_set_size(__isl_keep isl_set
*set
)
10334 for (i
= 0; i
< set
->n
; ++i
)
10335 size
+= isl_basic_set_size(set
->p
[i
]);
10340 /* Check if there is any lower bound (if lower == 0) and/or upper
10341 * bound (if upper == 0) on the specified dim.
10343 static isl_bool
basic_map_dim_is_bounded(__isl_keep isl_basic_map
*bmap
,
10344 enum isl_dim_type type
, unsigned pos
, int lower
, int upper
)
10349 return isl_bool_error
;
10351 isl_assert(bmap
->ctx
, pos
< isl_basic_map_dim(bmap
, type
),
10352 return isl_bool_error
);
10354 pos
+= isl_basic_map_offset(bmap
, type
);
10356 for (i
= 0; i
< bmap
->n_div
; ++i
) {
10357 if (isl_int_is_zero(bmap
->div
[i
][0]))
10359 if (!isl_int_is_zero(bmap
->div
[i
][1 + pos
]))
10360 return isl_bool_true
;
10363 for (i
= 0; i
< bmap
->n_eq
; ++i
)
10364 if (!isl_int_is_zero(bmap
->eq
[i
][pos
]))
10365 return isl_bool_true
;
10367 for (i
= 0; i
< bmap
->n_ineq
; ++i
) {
10368 int sgn
= isl_int_sgn(bmap
->ineq
[i
][pos
]);
10375 return lower
&& upper
;
10378 int isl_basic_map_dim_is_bounded(__isl_keep isl_basic_map
*bmap
,
10379 enum isl_dim_type type
, unsigned pos
)
10381 return basic_map_dim_is_bounded(bmap
, type
, pos
, 0, 0);
10384 isl_bool
isl_basic_map_dim_has_lower_bound(__isl_keep isl_basic_map
*bmap
,
10385 enum isl_dim_type type
, unsigned pos
)
10387 return basic_map_dim_is_bounded(bmap
, type
, pos
, 0, 1);
10390 isl_bool
isl_basic_map_dim_has_upper_bound(__isl_keep isl_basic_map
*bmap
,
10391 enum isl_dim_type type
, unsigned pos
)
10393 return basic_map_dim_is_bounded(bmap
, type
, pos
, 1, 0);
10396 int isl_map_dim_is_bounded(__isl_keep isl_map
*map
,
10397 enum isl_dim_type type
, unsigned pos
)
10404 for (i
= 0; i
< map
->n
; ++i
) {
10406 bounded
= isl_basic_map_dim_is_bounded(map
->p
[i
], type
, pos
);
10407 if (bounded
< 0 || !bounded
)
10414 /* Return 1 if the specified dim is involved in both an upper bound
10415 * and a lower bound.
10417 int isl_set_dim_is_bounded(__isl_keep isl_set
*set
,
10418 enum isl_dim_type type
, unsigned pos
)
10420 return isl_map_dim_is_bounded((isl_map
*)set
, type
, pos
);
10423 /* Does "map" have a bound (according to "fn") for any of its basic maps?
10425 static isl_bool
has_any_bound(__isl_keep isl_map
*map
,
10426 enum isl_dim_type type
, unsigned pos
,
10427 isl_bool (*fn
)(__isl_keep isl_basic_map
*bmap
,
10428 enum isl_dim_type type
, unsigned pos
))
10433 return isl_bool_error
;
10435 for (i
= 0; i
< map
->n
; ++i
) {
10437 bounded
= fn(map
->p
[i
], type
, pos
);
10438 if (bounded
< 0 || bounded
)
10442 return isl_bool_false
;
10445 /* Return 1 if the specified dim is involved in any lower bound.
10447 isl_bool
isl_set_dim_has_any_lower_bound(__isl_keep isl_set
*set
,
10448 enum isl_dim_type type
, unsigned pos
)
10450 return has_any_bound(set
, type
, pos
,
10451 &isl_basic_map_dim_has_lower_bound
);
10454 /* Return 1 if the specified dim is involved in any upper bound.
10456 isl_bool
isl_set_dim_has_any_upper_bound(__isl_keep isl_set
*set
,
10457 enum isl_dim_type type
, unsigned pos
)
10459 return has_any_bound(set
, type
, pos
,
10460 &isl_basic_map_dim_has_upper_bound
);
10463 /* Does "map" have a bound (according to "fn") for all of its basic maps?
10465 static isl_bool
has_bound(__isl_keep isl_map
*map
,
10466 enum isl_dim_type type
, unsigned pos
,
10467 isl_bool (*fn
)(__isl_keep isl_basic_map
*bmap
,
10468 enum isl_dim_type type
, unsigned pos
))
10473 return isl_bool_error
;
10475 for (i
= 0; i
< map
->n
; ++i
) {
10477 bounded
= fn(map
->p
[i
], type
, pos
);
10478 if (bounded
< 0 || !bounded
)
10482 return isl_bool_true
;
10485 /* Return 1 if the specified dim has a lower bound (in each of its basic sets).
10487 isl_bool
isl_set_dim_has_lower_bound(__isl_keep isl_set
*set
,
10488 enum isl_dim_type type
, unsigned pos
)
10490 return has_bound(set
, type
, pos
, &isl_basic_map_dim_has_lower_bound
);
10493 /* Return 1 if the specified dim has an upper bound (in each of its basic sets).
10495 isl_bool
isl_set_dim_has_upper_bound(__isl_keep isl_set
*set
,
10496 enum isl_dim_type type
, unsigned pos
)
10498 return has_bound(set
, type
, pos
, &isl_basic_map_dim_has_upper_bound
);
10501 /* For each of the "n" variables starting at "first", determine
10502 * the sign of the variable and put the results in the first "n"
10503 * elements of the array "signs".
10505 * 1 means that the variable is non-negative
10506 * -1 means that the variable is non-positive
10507 * 0 means the variable attains both positive and negative values.
10509 int isl_basic_set_vars_get_sign(__isl_keep isl_basic_set
*bset
,
10510 unsigned first
, unsigned n
, int *signs
)
10512 isl_vec
*bound
= NULL
;
10513 struct isl_tab
*tab
= NULL
;
10514 struct isl_tab_undo
*snap
;
10517 if (!bset
|| !signs
)
10520 bound
= isl_vec_alloc(bset
->ctx
, 1 + isl_basic_set_total_dim(bset
));
10521 tab
= isl_tab_from_basic_set(bset
, 0);
10522 if (!bound
|| !tab
)
10525 isl_seq_clr(bound
->el
, bound
->size
);
10526 isl_int_set_si(bound
->el
[0], -1);
10528 snap
= isl_tab_snap(tab
);
10529 for (i
= 0; i
< n
; ++i
) {
10532 isl_int_set_si(bound
->el
[1 + first
+ i
], -1);
10533 if (isl_tab_add_ineq(tab
, bound
->el
) < 0)
10535 empty
= tab
->empty
;
10536 isl_int_set_si(bound
->el
[1 + first
+ i
], 0);
10537 if (isl_tab_rollback(tab
, snap
) < 0)
10545 isl_int_set_si(bound
->el
[1 + first
+ i
], 1);
10546 if (isl_tab_add_ineq(tab
, bound
->el
) < 0)
10548 empty
= tab
->empty
;
10549 isl_int_set_si(bound
->el
[1 + first
+ i
], 0);
10550 if (isl_tab_rollback(tab
, snap
) < 0)
10553 signs
[i
] = empty
? -1 : 0;
10557 isl_vec_free(bound
);
10561 isl_vec_free(bound
);
10565 int isl_basic_set_dims_get_sign(__isl_keep isl_basic_set
*bset
,
10566 enum isl_dim_type type
, unsigned first
, unsigned n
, int *signs
)
10568 if (!bset
|| !signs
)
10570 isl_assert(bset
->ctx
, first
+ n
<= isl_basic_set_dim(bset
, type
),
10573 first
+= pos(bset
->dim
, type
) - 1;
10574 return isl_basic_set_vars_get_sign(bset
, first
, n
, signs
);
10577 /* Is it possible for the integer division "div" to depend (possibly
10578 * indirectly) on any output dimensions?
10580 * If the div is undefined, then we conservatively assume that it
10581 * may depend on them.
10582 * Otherwise, we check if it actually depends on them or on any integer
10583 * divisions that may depend on them.
10585 static int div_may_involve_output(__isl_keep isl_basic_map
*bmap
, int div
)
10588 unsigned n_out
, o_out
;
10589 unsigned n_div
, o_div
;
10591 if (isl_int_is_zero(bmap
->div
[div
][0]))
10594 n_out
= isl_basic_map_dim(bmap
, isl_dim_out
);
10595 o_out
= isl_basic_map_offset(bmap
, isl_dim_out
);
10597 if (isl_seq_first_non_zero(bmap
->div
[div
] + 1 + o_out
, n_out
) != -1)
10600 n_div
= isl_basic_map_dim(bmap
, isl_dim_div
);
10601 o_div
= isl_basic_map_offset(bmap
, isl_dim_div
);
10603 for (i
= 0; i
< n_div
; ++i
) {
10604 if (isl_int_is_zero(bmap
->div
[div
][1 + o_div
+ i
]))
10606 if (div_may_involve_output(bmap
, i
))
10613 /* Return the first integer division of "bmap" in the range
10614 * [first, first + n[ that may depend on any output dimensions and
10615 * that has a non-zero coefficient in "c" (where the first coefficient
10616 * in "c" corresponds to integer division "first").
10618 static int first_div_may_involve_output(__isl_keep isl_basic_map
*bmap
,
10619 isl_int
*c
, int first
, int n
)
10626 for (k
= first
; k
< first
+ n
; ++k
) {
10627 if (isl_int_is_zero(c
[k
]))
10629 if (div_may_involve_output(bmap
, k
))
10636 /* Look for a pair of inequality constraints in "bmap" of the form
10638 * -l + i >= 0 or i >= l
10640 * n + l - i >= 0 or i <= l + n
10642 * with n < "m" and i the output dimension at position "pos".
10643 * (Note that n >= 0 as otherwise the two constraints would conflict.)
10644 * Furthermore, "l" is only allowed to involve parameters, input dimensions
10645 * and earlier output dimensions, as well as integer divisions that do
10646 * not involve any of the output dimensions.
10648 * Return the index of the first inequality constraint or bmap->n_ineq
10649 * if no such pair can be found.
10651 static int find_modulo_constraint_pair(__isl_keep isl_basic_map
*bmap
,
10652 int pos
, isl_int m
)
10657 unsigned n_div
, o_div
;
10658 unsigned n_out
, o_out
;
10664 ctx
= isl_basic_map_get_ctx(bmap
);
10665 total
= isl_basic_map_total_dim(bmap
);
10666 n_out
= isl_basic_map_dim(bmap
, isl_dim_out
);
10667 o_out
= isl_basic_map_offset(bmap
, isl_dim_out
);
10668 n_div
= isl_basic_map_dim(bmap
, isl_dim_div
);
10669 o_div
= isl_basic_map_offset(bmap
, isl_dim_div
);
10670 for (i
= 0; i
< bmap
->n_ineq
; ++i
) {
10671 if (!isl_int_abs_eq(bmap
->ineq
[i
][o_out
+ pos
], ctx
->one
))
10673 if (isl_seq_first_non_zero(bmap
->ineq
[i
] + o_out
+ pos
+ 1,
10674 n_out
- (pos
+ 1)) != -1)
10676 if (first_div_may_involve_output(bmap
, bmap
->ineq
[i
] + o_div
,
10679 for (j
= i
+ 1; j
< bmap
->n_ineq
; ++j
) {
10680 if (!isl_int_abs_eq(bmap
->ineq
[j
][o_out
+ pos
],
10683 if (!isl_seq_is_neg(bmap
->ineq
[i
] + 1,
10684 bmap
->ineq
[j
] + 1, total
))
10688 if (j
>= bmap
->n_ineq
)
10690 isl_int_add(bmap
->ineq
[i
][0],
10691 bmap
->ineq
[i
][0], bmap
->ineq
[j
][0]);
10692 less
= isl_int_abs_lt(bmap
->ineq
[i
][0], m
);
10693 isl_int_sub(bmap
->ineq
[i
][0],
10694 bmap
->ineq
[i
][0], bmap
->ineq
[j
][0]);
10697 if (isl_int_is_one(bmap
->ineq
[i
][o_out
+ pos
]))
10703 return bmap
->n_ineq
;
10706 /* Return the index of the equality of "bmap" that defines
10707 * the output dimension "pos" in terms of earlier dimensions.
10708 * The equality may also involve integer divisions, as long
10709 * as those integer divisions are defined in terms of
10710 * parameters or input dimensions.
10711 * In this case, *div is set to the number of integer divisions and
10712 * *ineq is set to the number of inequality constraints (provided
10713 * div and ineq are not NULL).
10715 * The equality may also involve a single integer division involving
10716 * the output dimensions (typically only output dimension "pos") as
10717 * long as the coefficient of output dimension "pos" is 1 or -1 and
10718 * there is a pair of constraints i >= l and i <= l + n, with i referring
10719 * to output dimension "pos", l an expression involving only earlier
10720 * dimensions and n smaller than the coefficient of the integer division
10721 * in the equality. In this case, the output dimension can be defined
10722 * in terms of a modulo expression that does not involve the integer division.
10723 * *div is then set to this single integer division and
10724 * *ineq is set to the index of constraint i >= l.
10726 * Return bmap->n_eq if there is no such equality.
10727 * Return -1 on error.
10729 int isl_basic_map_output_defining_equality(__isl_keep isl_basic_map
*bmap
,
10730 int pos
, int *div
, int *ineq
)
10733 unsigned n_out
, o_out
;
10734 unsigned n_div
, o_div
;
10739 n_out
= isl_basic_map_dim(bmap
, isl_dim_out
);
10740 o_out
= isl_basic_map_offset(bmap
, isl_dim_out
);
10741 n_div
= isl_basic_map_dim(bmap
, isl_dim_div
);
10742 o_div
= isl_basic_map_offset(bmap
, isl_dim_div
);
10745 *ineq
= bmap
->n_ineq
;
10748 for (j
= 0; j
< bmap
->n_eq
; ++j
) {
10749 if (isl_int_is_zero(bmap
->eq
[j
][o_out
+ pos
]))
10751 if (isl_seq_first_non_zero(bmap
->eq
[j
] + o_out
+ pos
+ 1,
10752 n_out
- (pos
+ 1)) != -1)
10754 k
= first_div_may_involve_output(bmap
, bmap
->eq
[j
] + o_div
,
10758 if (!isl_int_is_one(bmap
->eq
[j
][o_out
+ pos
]) &&
10759 !isl_int_is_negone(bmap
->eq
[j
][o_out
+ pos
]))
10761 if (first_div_may_involve_output(bmap
, bmap
->eq
[j
] + o_div
,
10762 k
+ 1, n_div
- (k
+1)) < n_div
)
10764 l
= find_modulo_constraint_pair(bmap
, pos
,
10765 bmap
->eq
[j
][o_div
+ k
]);
10768 if (l
>= bmap
->n_ineq
)
10780 /* Check if the given basic map is obviously single-valued.
10781 * In particular, for each output dimension, check that there is
10782 * an equality that defines the output dimension in terms of
10783 * earlier dimensions.
10785 isl_bool
isl_basic_map_plain_is_single_valued(__isl_keep isl_basic_map
*bmap
)
10791 return isl_bool_error
;
10793 n_out
= isl_basic_map_dim(bmap
, isl_dim_out
);
10795 for (i
= 0; i
< n_out
; ++i
) {
10798 eq
= isl_basic_map_output_defining_equality(bmap
, i
,
10801 return isl_bool_error
;
10802 if (eq
>= bmap
->n_eq
)
10803 return isl_bool_false
;
10806 return isl_bool_true
;
10809 /* Check if the given basic map is single-valued.
10810 * We simply compute
10814 * and check if the result is a subset of the identity mapping.
10816 isl_bool
isl_basic_map_is_single_valued(__isl_keep isl_basic_map
*bmap
)
10819 isl_basic_map
*test
;
10823 sv
= isl_basic_map_plain_is_single_valued(bmap
);
10827 test
= isl_basic_map_reverse(isl_basic_map_copy(bmap
));
10828 test
= isl_basic_map_apply_range(test
, isl_basic_map_copy(bmap
));
10830 space
= isl_basic_map_get_space(bmap
);
10831 space
= isl_space_map_from_set(isl_space_range(space
));
10832 id
= isl_basic_map_identity(space
);
10834 sv
= isl_basic_map_is_subset(test
, id
);
10836 isl_basic_map_free(test
);
10837 isl_basic_map_free(id
);
10842 /* Check if the given map is obviously single-valued.
10844 isl_bool
isl_map_plain_is_single_valued(__isl_keep isl_map
*map
)
10847 return isl_bool_error
;
10849 return isl_bool_true
;
10851 return isl_bool_false
;
10853 return isl_basic_map_plain_is_single_valued(map
->p
[0]);
10856 /* Check if the given map is single-valued.
10857 * We simply compute
10861 * and check if the result is a subset of the identity mapping.
10863 isl_bool
isl_map_is_single_valued(__isl_keep isl_map
*map
)
10870 sv
= isl_map_plain_is_single_valued(map
);
10874 test
= isl_map_reverse(isl_map_copy(map
));
10875 test
= isl_map_apply_range(test
, isl_map_copy(map
));
10877 dim
= isl_space_map_from_set(isl_space_range(isl_map_get_space(map
)));
10878 id
= isl_map_identity(dim
);
10880 sv
= isl_map_is_subset(test
, id
);
10882 isl_map_free(test
);
10888 isl_bool
isl_map_is_injective(__isl_keep isl_map
*map
)
10892 map
= isl_map_copy(map
);
10893 map
= isl_map_reverse(map
);
10894 in
= isl_map_is_single_valued(map
);
10900 /* Check if the given map is obviously injective.
10902 isl_bool
isl_map_plain_is_injective(__isl_keep isl_map
*map
)
10906 map
= isl_map_copy(map
);
10907 map
= isl_map_reverse(map
);
10908 in
= isl_map_plain_is_single_valued(map
);
10914 isl_bool
isl_map_is_bijective(__isl_keep isl_map
*map
)
10918 sv
= isl_map_is_single_valued(map
);
10922 return isl_map_is_injective(map
);
10925 isl_bool
isl_set_is_singleton(__isl_keep isl_set
*set
)
10927 return isl_map_is_single_valued((isl_map
*)set
);
10930 /* Does "map" only map elements to themselves?
10932 * If the domain and range spaces are different, then "map"
10933 * is considered not to be an identity relation, even if it is empty.
10934 * Otherwise, construct the maximal identity relation and
10935 * check whether "map" is a subset of this relation.
10937 isl_bool
isl_map_is_identity(__isl_keep isl_map
*map
)
10941 isl_bool equal
, is_identity
;
10943 space
= isl_map_get_space(map
);
10944 equal
= isl_space_tuple_is_equal(space
, isl_dim_in
, space
, isl_dim_out
);
10945 isl_space_free(space
);
10946 if (equal
< 0 || !equal
)
10949 id
= isl_map_identity(isl_map_get_space(map
));
10950 is_identity
= isl_map_is_subset(map
, id
);
10953 return is_identity
;
10956 int isl_map_is_translation(__isl_keep isl_map
*map
)
10961 delta
= isl_map_deltas(isl_map_copy(map
));
10962 ok
= isl_set_is_singleton(delta
);
10963 isl_set_free(delta
);
10968 static int unique(isl_int
*p
, unsigned pos
, unsigned len
)
10970 if (isl_seq_first_non_zero(p
, pos
) != -1)
10972 if (isl_seq_first_non_zero(p
+ pos
+ 1, len
- pos
- 1) != -1)
10977 int isl_basic_set_is_box(__isl_keep isl_basic_set
*bset
)
10986 if (isl_basic_set_dim(bset
, isl_dim_div
) != 0)
10989 nvar
= isl_basic_set_dim(bset
, isl_dim_set
);
10990 ovar
= isl_space_offset(bset
->dim
, isl_dim_set
);
10991 for (j
= 0; j
< nvar
; ++j
) {
10992 int lower
= 0, upper
= 0;
10993 for (i
= 0; i
< bset
->n_eq
; ++i
) {
10994 if (isl_int_is_zero(bset
->eq
[i
][1 + ovar
+ j
]))
10996 if (!unique(bset
->eq
[i
] + 1 + ovar
, j
, nvar
))
11000 if (i
< bset
->n_eq
)
11002 for (i
= 0; i
< bset
->n_ineq
; ++i
) {
11003 if (isl_int_is_zero(bset
->ineq
[i
][1 + ovar
+ j
]))
11005 if (!unique(bset
->ineq
[i
] + 1 + ovar
, j
, nvar
))
11007 if (isl_int_is_pos(bset
->ineq
[i
][1 + ovar
+ j
]))
11012 if (!lower
|| !upper
)
11019 int isl_set_is_box(__isl_keep isl_set
*set
)
11026 return isl_basic_set_is_box(set
->p
[0]);
11029 isl_bool
isl_basic_set_is_wrapping(__isl_keep isl_basic_set
*bset
)
11032 return isl_bool_error
;
11034 return isl_space_is_wrapping(bset
->dim
);
11037 isl_bool
isl_set_is_wrapping(__isl_keep isl_set
*set
)
11040 return isl_bool_error
;
11042 return isl_space_is_wrapping(set
->dim
);
11045 /* Modify the space of "map" through a call to "change".
11046 * If "can_change" is set (not NULL), then first call it to check
11047 * if the modification is allowed, printing the error message "cannot_change"
11050 static __isl_give isl_map
*isl_map_change_space(__isl_take isl_map
*map
,
11051 isl_bool (*can_change
)(__isl_keep isl_map
*map
),
11052 const char *cannot_change
,
11053 __isl_give isl_space
*(*change
)(__isl_take isl_space
*space
))
11061 ok
= can_change
? can_change(map
) : isl_bool_true
;
11063 return isl_map_free(map
);
11065 isl_die(isl_map_get_ctx(map
), isl_error_invalid
, cannot_change
,
11066 return isl_map_free(map
));
11068 space
= change(isl_map_get_space(map
));
11069 map
= isl_map_reset_space(map
, space
);
11074 /* Is the domain of "map" a wrapped relation?
11076 isl_bool
isl_map_domain_is_wrapping(__isl_keep isl_map
*map
)
11079 return isl_bool_error
;
11081 return isl_space_domain_is_wrapping(map
->dim
);
11084 /* Is the range of "map" a wrapped relation?
11086 isl_bool
isl_map_range_is_wrapping(__isl_keep isl_map
*map
)
11089 return isl_bool_error
;
11091 return isl_space_range_is_wrapping(map
->dim
);
11094 __isl_give isl_basic_set
*isl_basic_map_wrap(__isl_take isl_basic_map
*bmap
)
11096 bmap
= isl_basic_map_cow(bmap
);
11100 bmap
->dim
= isl_space_wrap(bmap
->dim
);
11104 bmap
= isl_basic_map_finalize(bmap
);
11106 return (isl_basic_set
*)bmap
;
11108 isl_basic_map_free(bmap
);
11112 /* Given a map A -> B, return the set (A -> B).
11114 __isl_give isl_set
*isl_map_wrap(__isl_take isl_map
*map
)
11116 return isl_map_change_space(map
, NULL
, NULL
, &isl_space_wrap
);
11119 __isl_give isl_basic_map
*isl_basic_set_unwrap(__isl_take isl_basic_set
*bset
)
11121 bset
= isl_basic_set_cow(bset
);
11125 bset
->dim
= isl_space_unwrap(bset
->dim
);
11129 bset
= isl_basic_set_finalize(bset
);
11131 return (isl_basic_map
*)bset
;
11133 isl_basic_set_free(bset
);
11137 /* Given a set (A -> B), return the map A -> B.
11138 * Error out if "set" is not of the form (A -> B).
11140 __isl_give isl_map
*isl_set_unwrap(__isl_take isl_set
*set
)
11142 return isl_map_change_space(set
, &isl_set_is_wrapping
,
11143 "not a wrapping set", &isl_space_unwrap
);
11146 __isl_give isl_basic_map
*isl_basic_map_reset(__isl_take isl_basic_map
*bmap
,
11147 enum isl_dim_type type
)
11152 if (!isl_space_is_named_or_nested(bmap
->dim
, type
))
11155 bmap
= isl_basic_map_cow(bmap
);
11159 bmap
->dim
= isl_space_reset(bmap
->dim
, type
);
11163 bmap
= isl_basic_map_finalize(bmap
);
11167 isl_basic_map_free(bmap
);
11171 __isl_give isl_map
*isl_map_reset(__isl_take isl_map
*map
,
11172 enum isl_dim_type type
)
11179 if (!isl_space_is_named_or_nested(map
->dim
, type
))
11182 map
= isl_map_cow(map
);
11186 for (i
= 0; i
< map
->n
; ++i
) {
11187 map
->p
[i
] = isl_basic_map_reset(map
->p
[i
], type
);
11191 map
->dim
= isl_space_reset(map
->dim
, type
);
11201 __isl_give isl_basic_map
*isl_basic_map_flatten(__isl_take isl_basic_map
*bmap
)
11206 if (!bmap
->dim
->nested
[0] && !bmap
->dim
->nested
[1])
11209 bmap
= isl_basic_map_cow(bmap
);
11213 bmap
->dim
= isl_space_flatten(bmap
->dim
);
11217 bmap
= isl_basic_map_finalize(bmap
);
11221 isl_basic_map_free(bmap
);
11225 __isl_give isl_basic_set
*isl_basic_set_flatten(__isl_take isl_basic_set
*bset
)
11227 return (isl_basic_set
*)isl_basic_map_flatten((isl_basic_map
*)bset
);
11230 __isl_give isl_basic_map
*isl_basic_map_flatten_domain(
11231 __isl_take isl_basic_map
*bmap
)
11236 if (!bmap
->dim
->nested
[0])
11239 bmap
= isl_basic_map_cow(bmap
);
11243 bmap
->dim
= isl_space_flatten_domain(bmap
->dim
);
11247 bmap
= isl_basic_map_finalize(bmap
);
11251 isl_basic_map_free(bmap
);
11255 __isl_give isl_basic_map
*isl_basic_map_flatten_range(
11256 __isl_take isl_basic_map
*bmap
)
11261 if (!bmap
->dim
->nested
[1])
11264 bmap
= isl_basic_map_cow(bmap
);
11268 bmap
->dim
= isl_space_flatten_range(bmap
->dim
);
11272 bmap
= isl_basic_map_finalize(bmap
);
11276 isl_basic_map_free(bmap
);
11280 /* Remove any internal structure from the spaces of domain and range of "map".
11282 __isl_give isl_map
*isl_map_flatten(__isl_take isl_map
*map
)
11287 if (!map
->dim
->nested
[0] && !map
->dim
->nested
[1])
11290 return isl_map_change_space(map
, NULL
, NULL
, &isl_space_flatten
);
11293 __isl_give isl_set
*isl_set_flatten(__isl_take isl_set
*set
)
11295 return (isl_set
*)isl_map_flatten((isl_map
*)set
);
11298 __isl_give isl_map
*isl_set_flatten_map(__isl_take isl_set
*set
)
11300 isl_space
*dim
, *flat_dim
;
11303 dim
= isl_set_get_space(set
);
11304 flat_dim
= isl_space_flatten(isl_space_copy(dim
));
11305 map
= isl_map_identity(isl_space_join(isl_space_reverse(dim
), flat_dim
));
11306 map
= isl_map_intersect_domain(map
, set
);
11311 /* Remove any internal structure from the space of the domain of "map".
11313 __isl_give isl_map
*isl_map_flatten_domain(__isl_take isl_map
*map
)
11318 if (!map
->dim
->nested
[0])
11321 return isl_map_change_space(map
, NULL
, NULL
, &isl_space_flatten_domain
);
11324 /* Remove any internal structure from the space of the range of "map".
11326 __isl_give isl_map
*isl_map_flatten_range(__isl_take isl_map
*map
)
11331 if (!map
->dim
->nested
[1])
11334 return isl_map_change_space(map
, NULL
, NULL
, &isl_space_flatten_range
);
11337 /* Reorder the dimensions of "bmap" according to the given dim_map
11338 * and set the dimension specification to "dim" and
11339 * perform Gaussian elimination on the result.
11341 __isl_give isl_basic_map
*isl_basic_map_realign(__isl_take isl_basic_map
*bmap
,
11342 __isl_take isl_space
*dim
, __isl_take
struct isl_dim_map
*dim_map
)
11344 isl_basic_map
*res
;
11347 bmap
= isl_basic_map_cow(bmap
);
11348 if (!bmap
|| !dim
|| !dim_map
)
11351 flags
= bmap
->flags
;
11352 ISL_FL_CLR(flags
, ISL_BASIC_MAP_FINAL
);
11353 ISL_FL_CLR(flags
, ISL_BASIC_MAP_NORMALIZED
);
11354 ISL_FL_CLR(flags
, ISL_BASIC_MAP_NORMALIZED_DIVS
);
11355 res
= isl_basic_map_alloc_space(dim
,
11356 bmap
->n_div
, bmap
->n_eq
, bmap
->n_ineq
);
11357 res
= isl_basic_map_add_constraints_dim_map(res
, bmap
, dim_map
);
11359 res
->flags
= flags
;
11360 res
= isl_basic_map_gauss(res
, NULL
);
11361 res
= isl_basic_map_finalize(res
);
11365 isl_basic_map_free(bmap
);
11366 isl_space_free(dim
);
11370 /* Reorder the dimensions of "map" according to given reordering.
11372 __isl_give isl_map
*isl_map_realign(__isl_take isl_map
*map
,
11373 __isl_take isl_reordering
*r
)
11376 struct isl_dim_map
*dim_map
;
11378 map
= isl_map_cow(map
);
11379 dim_map
= isl_dim_map_from_reordering(r
);
11380 if (!map
|| !r
|| !dim_map
)
11383 for (i
= 0; i
< map
->n
; ++i
) {
11384 struct isl_dim_map
*dim_map_i
;
11386 dim_map_i
= isl_dim_map_extend(dim_map
, map
->p
[i
]);
11388 map
->p
[i
] = isl_basic_map_realign(map
->p
[i
],
11389 isl_space_copy(r
->dim
), dim_map_i
);
11395 map
= isl_map_reset_space(map
, isl_space_copy(r
->dim
));
11397 isl_reordering_free(r
);
11403 isl_reordering_free(r
);
11407 __isl_give isl_set
*isl_set_realign(__isl_take isl_set
*set
,
11408 __isl_take isl_reordering
*r
)
11410 return (isl_set
*)isl_map_realign((isl_map
*)set
, r
);
11413 __isl_give isl_map
*isl_map_align_params(__isl_take isl_map
*map
,
11414 __isl_take isl_space
*model
)
11418 if (!map
|| !model
)
11421 ctx
= isl_space_get_ctx(model
);
11422 if (!isl_space_has_named_params(model
))
11423 isl_die(ctx
, isl_error_invalid
,
11424 "model has unnamed parameters", goto error
);
11425 if (!isl_space_has_named_params(map
->dim
))
11426 isl_die(ctx
, isl_error_invalid
,
11427 "relation has unnamed parameters", goto error
);
11428 if (!isl_space_match(map
->dim
, isl_dim_param
, model
, isl_dim_param
)) {
11429 isl_reordering
*exp
;
11431 model
= isl_space_drop_dims(model
, isl_dim_in
,
11432 0, isl_space_dim(model
, isl_dim_in
));
11433 model
= isl_space_drop_dims(model
, isl_dim_out
,
11434 0, isl_space_dim(model
, isl_dim_out
));
11435 exp
= isl_parameter_alignment_reordering(map
->dim
, model
);
11436 exp
= isl_reordering_extend_space(exp
, isl_map_get_space(map
));
11437 map
= isl_map_realign(map
, exp
);
11440 isl_space_free(model
);
11443 isl_space_free(model
);
11448 __isl_give isl_set
*isl_set_align_params(__isl_take isl_set
*set
,
11449 __isl_take isl_space
*model
)
11451 return isl_map_align_params(set
, model
);
11454 /* Align the parameters of "bmap" to those of "model", introducing
11455 * additional parameters if needed.
11457 __isl_give isl_basic_map
*isl_basic_map_align_params(
11458 __isl_take isl_basic_map
*bmap
, __isl_take isl_space
*model
)
11462 if (!bmap
|| !model
)
11465 ctx
= isl_space_get_ctx(model
);
11466 if (!isl_space_has_named_params(model
))
11467 isl_die(ctx
, isl_error_invalid
,
11468 "model has unnamed parameters", goto error
);
11469 if (!isl_space_has_named_params(bmap
->dim
))
11470 isl_die(ctx
, isl_error_invalid
,
11471 "relation has unnamed parameters", goto error
);
11472 if (!isl_space_match(bmap
->dim
, isl_dim_param
, model
, isl_dim_param
)) {
11473 isl_reordering
*exp
;
11474 struct isl_dim_map
*dim_map
;
11476 model
= isl_space_drop_dims(model
, isl_dim_in
,
11477 0, isl_space_dim(model
, isl_dim_in
));
11478 model
= isl_space_drop_dims(model
, isl_dim_out
,
11479 0, isl_space_dim(model
, isl_dim_out
));
11480 exp
= isl_parameter_alignment_reordering(bmap
->dim
, model
);
11481 exp
= isl_reordering_extend_space(exp
,
11482 isl_basic_map_get_space(bmap
));
11483 dim_map
= isl_dim_map_from_reordering(exp
);
11484 bmap
= isl_basic_map_realign(bmap
,
11485 exp
? isl_space_copy(exp
->dim
) : NULL
,
11486 isl_dim_map_extend(dim_map
, bmap
));
11487 isl_reordering_free(exp
);
11491 isl_space_free(model
);
11494 isl_space_free(model
);
11495 isl_basic_map_free(bmap
);
11499 /* Align the parameters of "bset" to those of "model", introducing
11500 * additional parameters if needed.
11502 __isl_give isl_basic_set
*isl_basic_set_align_params(
11503 __isl_take isl_basic_set
*bset
, __isl_take isl_space
*model
)
11505 return isl_basic_map_align_params(bset
, model
);
11508 __isl_give isl_mat
*isl_basic_map_equalities_matrix(
11509 __isl_keep isl_basic_map
*bmap
, enum isl_dim_type c1
,
11510 enum isl_dim_type c2
, enum isl_dim_type c3
,
11511 enum isl_dim_type c4
, enum isl_dim_type c5
)
11513 enum isl_dim_type c
[5] = { c1
, c2
, c3
, c4
, c5
};
11514 struct isl_mat
*mat
;
11520 mat
= isl_mat_alloc(bmap
->ctx
, bmap
->n_eq
,
11521 isl_basic_map_total_dim(bmap
) + 1);
11524 for (i
= 0; i
< bmap
->n_eq
; ++i
)
11525 for (j
= 0, pos
= 0; j
< 5; ++j
) {
11526 int off
= isl_basic_map_offset(bmap
, c
[j
]);
11527 for (k
= 0; k
< isl_basic_map_dim(bmap
, c
[j
]); ++k
) {
11528 isl_int_set(mat
->row
[i
][pos
],
11529 bmap
->eq
[i
][off
+ k
]);
11537 __isl_give isl_mat
*isl_basic_map_inequalities_matrix(
11538 __isl_keep isl_basic_map
*bmap
, enum isl_dim_type c1
,
11539 enum isl_dim_type c2
, enum isl_dim_type c3
,
11540 enum isl_dim_type c4
, enum isl_dim_type c5
)
11542 enum isl_dim_type c
[5] = { c1
, c2
, c3
, c4
, c5
};
11543 struct isl_mat
*mat
;
11549 mat
= isl_mat_alloc(bmap
->ctx
, bmap
->n_ineq
,
11550 isl_basic_map_total_dim(bmap
) + 1);
11553 for (i
= 0; i
< bmap
->n_ineq
; ++i
)
11554 for (j
= 0, pos
= 0; j
< 5; ++j
) {
11555 int off
= isl_basic_map_offset(bmap
, c
[j
]);
11556 for (k
= 0; k
< isl_basic_map_dim(bmap
, c
[j
]); ++k
) {
11557 isl_int_set(mat
->row
[i
][pos
],
11558 bmap
->ineq
[i
][off
+ k
]);
11566 __isl_give isl_basic_map
*isl_basic_map_from_constraint_matrices(
11567 __isl_take isl_space
*dim
,
11568 __isl_take isl_mat
*eq
, __isl_take isl_mat
*ineq
, enum isl_dim_type c1
,
11569 enum isl_dim_type c2
, enum isl_dim_type c3
,
11570 enum isl_dim_type c4
, enum isl_dim_type c5
)
11572 enum isl_dim_type c
[5] = { c1
, c2
, c3
, c4
, c5
};
11573 isl_basic_map
*bmap
;
11579 if (!dim
|| !eq
|| !ineq
)
11582 if (eq
->n_col
!= ineq
->n_col
)
11583 isl_die(dim
->ctx
, isl_error_invalid
,
11584 "equalities and inequalities matrices should have "
11585 "same number of columns", goto error
);
11587 total
= 1 + isl_space_dim(dim
, isl_dim_all
);
11589 if (eq
->n_col
< total
)
11590 isl_die(dim
->ctx
, isl_error_invalid
,
11591 "number of columns too small", goto error
);
11593 extra
= eq
->n_col
- total
;
11595 bmap
= isl_basic_map_alloc_space(isl_space_copy(dim
), extra
,
11596 eq
->n_row
, ineq
->n_row
);
11599 for (i
= 0; i
< extra
; ++i
) {
11600 k
= isl_basic_map_alloc_div(bmap
);
11603 isl_int_set_si(bmap
->div
[k
][0], 0);
11605 for (i
= 0; i
< eq
->n_row
; ++i
) {
11606 l
= isl_basic_map_alloc_equality(bmap
);
11609 for (j
= 0, pos
= 0; j
< 5; ++j
) {
11610 int off
= isl_basic_map_offset(bmap
, c
[j
]);
11611 for (k
= 0; k
< isl_basic_map_dim(bmap
, c
[j
]); ++k
) {
11612 isl_int_set(bmap
->eq
[l
][off
+ k
],
11618 for (i
= 0; i
< ineq
->n_row
; ++i
) {
11619 l
= isl_basic_map_alloc_inequality(bmap
);
11622 for (j
= 0, pos
= 0; j
< 5; ++j
) {
11623 int off
= isl_basic_map_offset(bmap
, c
[j
]);
11624 for (k
= 0; k
< isl_basic_map_dim(bmap
, c
[j
]); ++k
) {
11625 isl_int_set(bmap
->ineq
[l
][off
+ k
],
11626 ineq
->row
[i
][pos
]);
11632 isl_space_free(dim
);
11634 isl_mat_free(ineq
);
11636 bmap
= isl_basic_map_simplify(bmap
);
11637 return isl_basic_map_finalize(bmap
);
11639 isl_space_free(dim
);
11641 isl_mat_free(ineq
);
11645 __isl_give isl_mat
*isl_basic_set_equalities_matrix(
11646 __isl_keep isl_basic_set
*bset
, enum isl_dim_type c1
,
11647 enum isl_dim_type c2
, enum isl_dim_type c3
, enum isl_dim_type c4
)
11649 return isl_basic_map_equalities_matrix((isl_basic_map
*)bset
,
11650 c1
, c2
, c3
, c4
, isl_dim_in
);
11653 __isl_give isl_mat
*isl_basic_set_inequalities_matrix(
11654 __isl_keep isl_basic_set
*bset
, enum isl_dim_type c1
,
11655 enum isl_dim_type c2
, enum isl_dim_type c3
, enum isl_dim_type c4
)
11657 return isl_basic_map_inequalities_matrix((isl_basic_map
*)bset
,
11658 c1
, c2
, c3
, c4
, isl_dim_in
);
11661 __isl_give isl_basic_set
*isl_basic_set_from_constraint_matrices(
11662 __isl_take isl_space
*dim
,
11663 __isl_take isl_mat
*eq
, __isl_take isl_mat
*ineq
, enum isl_dim_type c1
,
11664 enum isl_dim_type c2
, enum isl_dim_type c3
, enum isl_dim_type c4
)
11666 return (isl_basic_set
*)
11667 isl_basic_map_from_constraint_matrices(dim
, eq
, ineq
,
11668 c1
, c2
, c3
, c4
, isl_dim_in
);
11671 isl_bool
isl_basic_map_can_zip(__isl_keep isl_basic_map
*bmap
)
11674 return isl_bool_error
;
11676 return isl_space_can_zip(bmap
->dim
);
11679 isl_bool
isl_map_can_zip(__isl_keep isl_map
*map
)
11682 return isl_bool_error
;
11684 return isl_space_can_zip(map
->dim
);
11687 /* Given a basic map (A -> B) -> (C -> D), return the corresponding basic map
11688 * (A -> C) -> (B -> D).
11690 __isl_give isl_basic_map
*isl_basic_map_zip(__isl_take isl_basic_map
*bmap
)
11699 if (!isl_basic_map_can_zip(bmap
))
11700 isl_die(bmap
->ctx
, isl_error_invalid
,
11701 "basic map cannot be zipped", goto error
);
11702 pos
= isl_basic_map_offset(bmap
, isl_dim_in
) +
11703 isl_space_dim(bmap
->dim
->nested
[0], isl_dim_in
);
11704 n1
= isl_space_dim(bmap
->dim
->nested
[0], isl_dim_out
);
11705 n2
= isl_space_dim(bmap
->dim
->nested
[1], isl_dim_in
);
11706 bmap
= isl_basic_map_cow(bmap
);
11707 bmap
= isl_basic_map_swap_vars(bmap
, pos
, n1
, n2
);
11710 bmap
->dim
= isl_space_zip(bmap
->dim
);
11713 bmap
= isl_basic_map_mark_final(bmap
);
11716 isl_basic_map_free(bmap
);
11720 /* Given a map (A -> B) -> (C -> D), return the corresponding map
11721 * (A -> C) -> (B -> D).
11723 __isl_give isl_map
*isl_map_zip(__isl_take isl_map
*map
)
11730 if (!isl_map_can_zip(map
))
11731 isl_die(map
->ctx
, isl_error_invalid
, "map cannot be zipped",
11734 map
= isl_map_cow(map
);
11738 for (i
= 0; i
< map
->n
; ++i
) {
11739 map
->p
[i
] = isl_basic_map_zip(map
->p
[i
]);
11744 map
->dim
= isl_space_zip(map
->dim
);
11754 /* Can we apply isl_basic_map_curry to "bmap"?
11755 * That is, does it have a nested relation in its domain?
11757 isl_bool
isl_basic_map_can_curry(__isl_keep isl_basic_map
*bmap
)
11760 return isl_bool_error
;
11762 return isl_space_can_curry(bmap
->dim
);
11765 /* Can we apply isl_map_curry to "map"?
11766 * That is, does it have a nested relation in its domain?
11768 isl_bool
isl_map_can_curry(__isl_keep isl_map
*map
)
11771 return isl_bool_error
;
11773 return isl_space_can_curry(map
->dim
);
11776 /* Given a basic map (A -> B) -> C, return the corresponding basic map
11779 __isl_give isl_basic_map
*isl_basic_map_curry(__isl_take isl_basic_map
*bmap
)
11785 if (!isl_basic_map_can_curry(bmap
))
11786 isl_die(bmap
->ctx
, isl_error_invalid
,
11787 "basic map cannot be curried", goto error
);
11788 bmap
= isl_basic_map_cow(bmap
);
11791 bmap
->dim
= isl_space_curry(bmap
->dim
);
11794 bmap
= isl_basic_map_mark_final(bmap
);
11797 isl_basic_map_free(bmap
);
11801 /* Given a map (A -> B) -> C, return the corresponding map
11804 __isl_give isl_map
*isl_map_curry(__isl_take isl_map
*map
)
11806 return isl_map_change_space(map
, &isl_map_can_curry
,
11807 "map cannot be curried", &isl_space_curry
);
11810 /* Can isl_map_range_curry be applied to "map"?
11811 * That is, does it have a nested relation in its range,
11812 * the domain of which is itself a nested relation?
11814 isl_bool
isl_map_can_range_curry(__isl_keep isl_map
*map
)
11817 return isl_bool_error
;
11819 return isl_space_can_range_curry(map
->dim
);
11822 /* Given a map A -> ((B -> C) -> D), return the corresponding map
11823 * A -> (B -> (C -> D)).
11825 __isl_give isl_map
*isl_map_range_curry(__isl_take isl_map
*map
)
11827 return isl_map_change_space(map
, &isl_map_can_range_curry
,
11828 "map range cannot be curried",
11829 &isl_space_range_curry
);
11832 /* Can we apply isl_basic_map_uncurry to "bmap"?
11833 * That is, does it have a nested relation in its domain?
11835 isl_bool
isl_basic_map_can_uncurry(__isl_keep isl_basic_map
*bmap
)
11838 return isl_bool_error
;
11840 return isl_space_can_uncurry(bmap
->dim
);
11843 /* Can we apply isl_map_uncurry to "map"?
11844 * That is, does it have a nested relation in its domain?
11846 isl_bool
isl_map_can_uncurry(__isl_keep isl_map
*map
)
11849 return isl_bool_error
;
11851 return isl_space_can_uncurry(map
->dim
);
11854 /* Given a basic map A -> (B -> C), return the corresponding basic map
11857 __isl_give isl_basic_map
*isl_basic_map_uncurry(__isl_take isl_basic_map
*bmap
)
11863 if (!isl_basic_map_can_uncurry(bmap
))
11864 isl_die(bmap
->ctx
, isl_error_invalid
,
11865 "basic map cannot be uncurried",
11866 return isl_basic_map_free(bmap
));
11867 bmap
= isl_basic_map_cow(bmap
);
11870 bmap
->dim
= isl_space_uncurry(bmap
->dim
);
11872 return isl_basic_map_free(bmap
);
11873 bmap
= isl_basic_map_mark_final(bmap
);
11877 /* Given a map A -> (B -> C), return the corresponding map
11880 __isl_give isl_map
*isl_map_uncurry(__isl_take isl_map
*map
)
11882 return isl_map_change_space(map
, &isl_map_can_uncurry
,
11883 "map cannot be uncurried", &isl_space_uncurry
);
11886 /* Construct a basic map mapping the domain of the affine expression
11887 * to a one-dimensional range prescribed by the affine expression.
11888 * If "rational" is set, then construct a rational basic map.
11890 * A NaN affine expression cannot be converted to a basic map.
11892 static __isl_give isl_basic_map
*isl_basic_map_from_aff2(
11893 __isl_take isl_aff
*aff
, int rational
)
11898 isl_local_space
*ls
;
11899 isl_basic_map
*bmap
= NULL
;
11903 is_nan
= isl_aff_is_nan(aff
);
11907 isl_die(isl_aff_get_ctx(aff
), isl_error_invalid
,
11908 "cannot convert NaN", goto error
);
11910 ls
= isl_aff_get_local_space(aff
);
11911 bmap
= isl_basic_map_from_local_space(ls
);
11912 bmap
= isl_basic_map_extend_constraints(bmap
, 1, 0);
11913 k
= isl_basic_map_alloc_equality(bmap
);
11917 pos
= isl_basic_map_offset(bmap
, isl_dim_out
);
11918 isl_seq_cpy(bmap
->eq
[k
], aff
->v
->el
+ 1, pos
);
11919 isl_int_neg(bmap
->eq
[k
][pos
], aff
->v
->el
[0]);
11920 isl_seq_cpy(bmap
->eq
[k
] + pos
+ 1, aff
->v
->el
+ 1 + pos
,
11921 aff
->v
->size
- (pos
+ 1));
11925 bmap
= isl_basic_map_set_rational(bmap
);
11926 bmap
= isl_basic_map_finalize(bmap
);
11930 isl_basic_map_free(bmap
);
11934 /* Construct a basic map mapping the domain of the affine expression
11935 * to a one-dimensional range prescribed by the affine expression.
11937 __isl_give isl_basic_map
*isl_basic_map_from_aff(__isl_take isl_aff
*aff
)
11939 return isl_basic_map_from_aff2(aff
, 0);
11942 /* Construct a map mapping the domain of the affine expression
11943 * to a one-dimensional range prescribed by the affine expression.
11945 __isl_give isl_map
*isl_map_from_aff(__isl_take isl_aff
*aff
)
11947 isl_basic_map
*bmap
;
11949 bmap
= isl_basic_map_from_aff(aff
);
11950 return isl_map_from_basic_map(bmap
);
11953 /* Construct a basic map mapping the domain the multi-affine expression
11954 * to its range, with each dimension in the range equated to the
11955 * corresponding affine expression.
11956 * If "rational" is set, then construct a rational basic map.
11958 __isl_give isl_basic_map
*isl_basic_map_from_multi_aff2(
11959 __isl_take isl_multi_aff
*maff
, int rational
)
11963 isl_basic_map
*bmap
;
11968 if (isl_space_dim(maff
->space
, isl_dim_out
) != maff
->n
)
11969 isl_die(isl_multi_aff_get_ctx(maff
), isl_error_internal
,
11970 "invalid space", goto error
);
11972 space
= isl_space_domain(isl_multi_aff_get_space(maff
));
11973 bmap
= isl_basic_map_universe(isl_space_from_domain(space
));
11975 bmap
= isl_basic_map_set_rational(bmap
);
11977 for (i
= 0; i
< maff
->n
; ++i
) {
11979 isl_basic_map
*bmap_i
;
11981 aff
= isl_aff_copy(maff
->p
[i
]);
11982 bmap_i
= isl_basic_map_from_aff2(aff
, rational
);
11984 bmap
= isl_basic_map_flat_range_product(bmap
, bmap_i
);
11987 bmap
= isl_basic_map_reset_space(bmap
, isl_multi_aff_get_space(maff
));
11989 isl_multi_aff_free(maff
);
11992 isl_multi_aff_free(maff
);
11996 /* Construct a basic map mapping the domain the multi-affine expression
11997 * to its range, with each dimension in the range equated to the
11998 * corresponding affine expression.
12000 __isl_give isl_basic_map
*isl_basic_map_from_multi_aff(
12001 __isl_take isl_multi_aff
*ma
)
12003 return isl_basic_map_from_multi_aff2(ma
, 0);
12006 /* Construct a map mapping the domain the multi-affine expression
12007 * to its range, with each dimension in the range equated to the
12008 * corresponding affine expression.
12010 __isl_give isl_map
*isl_map_from_multi_aff(__isl_take isl_multi_aff
*maff
)
12012 isl_basic_map
*bmap
;
12014 bmap
= isl_basic_map_from_multi_aff(maff
);
12015 return isl_map_from_basic_map(bmap
);
12018 /* Construct a basic map mapping a domain in the given space to
12019 * to an n-dimensional range, with n the number of elements in the list,
12020 * where each coordinate in the range is prescribed by the
12021 * corresponding affine expression.
12022 * The domains of all affine expressions in the list are assumed to match
12025 __isl_give isl_basic_map
*isl_basic_map_from_aff_list(
12026 __isl_take isl_space
*domain_dim
, __isl_take isl_aff_list
*list
)
12030 isl_basic_map
*bmap
;
12035 dim
= isl_space_from_domain(domain_dim
);
12036 bmap
= isl_basic_map_universe(dim
);
12038 for (i
= 0; i
< list
->n
; ++i
) {
12040 isl_basic_map
*bmap_i
;
12042 aff
= isl_aff_copy(list
->p
[i
]);
12043 bmap_i
= isl_basic_map_from_aff(aff
);
12045 bmap
= isl_basic_map_flat_range_product(bmap
, bmap_i
);
12048 isl_aff_list_free(list
);
12052 __isl_give isl_set
*isl_set_equate(__isl_take isl_set
*set
,
12053 enum isl_dim_type type1
, int pos1
, enum isl_dim_type type2
, int pos2
)
12055 return isl_map_equate(set
, type1
, pos1
, type2
, pos2
);
12058 /* Construct a basic map where the given dimensions are equal to each other.
12060 static __isl_give isl_basic_map
*equator(__isl_take isl_space
*space
,
12061 enum isl_dim_type type1
, int pos1
, enum isl_dim_type type2
, int pos2
)
12063 isl_basic_map
*bmap
= NULL
;
12069 if (pos1
>= isl_space_dim(space
, type1
))
12070 isl_die(isl_space_get_ctx(space
), isl_error_invalid
,
12071 "index out of bounds", goto error
);
12072 if (pos2
>= isl_space_dim(space
, type2
))
12073 isl_die(isl_space_get_ctx(space
), isl_error_invalid
,
12074 "index out of bounds", goto error
);
12076 if (type1
== type2
&& pos1
== pos2
)
12077 return isl_basic_map_universe(space
);
12079 bmap
= isl_basic_map_alloc_space(isl_space_copy(space
), 0, 1, 0);
12080 i
= isl_basic_map_alloc_equality(bmap
);
12083 isl_seq_clr(bmap
->eq
[i
], 1 + isl_basic_map_total_dim(bmap
));
12084 pos1
+= isl_basic_map_offset(bmap
, type1
);
12085 pos2
+= isl_basic_map_offset(bmap
, type2
);
12086 isl_int_set_si(bmap
->eq
[i
][pos1
], -1);
12087 isl_int_set_si(bmap
->eq
[i
][pos2
], 1);
12088 bmap
= isl_basic_map_finalize(bmap
);
12089 isl_space_free(space
);
12092 isl_space_free(space
);
12093 isl_basic_map_free(bmap
);
12097 /* Add a constraint imposing that the given two dimensions are equal.
12099 __isl_give isl_basic_map
*isl_basic_map_equate(__isl_take isl_basic_map
*bmap
,
12100 enum isl_dim_type type1
, int pos1
, enum isl_dim_type type2
, int pos2
)
12104 eq
= equator(isl_basic_map_get_space(bmap
), type1
, pos1
, type2
, pos2
);
12106 bmap
= isl_basic_map_intersect(bmap
, eq
);
12111 /* Add a constraint imposing that the given two dimensions are equal.
12113 __isl_give isl_map
*isl_map_equate(__isl_take isl_map
*map
,
12114 enum isl_dim_type type1
, int pos1
, enum isl_dim_type type2
, int pos2
)
12116 isl_basic_map
*bmap
;
12118 bmap
= equator(isl_map_get_space(map
), type1
, pos1
, type2
, pos2
);
12120 map
= isl_map_intersect(map
, isl_map_from_basic_map(bmap
));
12125 /* Add a constraint imposing that the given two dimensions have opposite values.
12127 __isl_give isl_map
*isl_map_oppose(__isl_take isl_map
*map
,
12128 enum isl_dim_type type1
, int pos1
, enum isl_dim_type type2
, int pos2
)
12130 isl_basic_map
*bmap
= NULL
;
12136 if (pos1
>= isl_map_dim(map
, type1
))
12137 isl_die(map
->ctx
, isl_error_invalid
,
12138 "index out of bounds", goto error
);
12139 if (pos2
>= isl_map_dim(map
, type2
))
12140 isl_die(map
->ctx
, isl_error_invalid
,
12141 "index out of bounds", goto error
);
12143 bmap
= isl_basic_map_alloc_space(isl_map_get_space(map
), 0, 1, 0);
12144 i
= isl_basic_map_alloc_equality(bmap
);
12147 isl_seq_clr(bmap
->eq
[i
], 1 + isl_basic_map_total_dim(bmap
));
12148 pos1
+= isl_basic_map_offset(bmap
, type1
);
12149 pos2
+= isl_basic_map_offset(bmap
, type2
);
12150 isl_int_set_si(bmap
->eq
[i
][pos1
], 1);
12151 isl_int_set_si(bmap
->eq
[i
][pos2
], 1);
12152 bmap
= isl_basic_map_finalize(bmap
);
12154 map
= isl_map_intersect(map
, isl_map_from_basic_map(bmap
));
12158 isl_basic_map_free(bmap
);
12163 /* Construct a constraint imposing that the value of the first dimension is
12164 * greater than or equal to that of the second.
12166 static __isl_give isl_constraint
*constraint_order_ge(
12167 __isl_take isl_space
*space
, enum isl_dim_type type1
, int pos1
,
12168 enum isl_dim_type type2
, int pos2
)
12175 c
= isl_constraint_alloc_inequality(isl_local_space_from_space(space
));
12177 if (pos1
>= isl_constraint_dim(c
, type1
))
12178 isl_die(isl_constraint_get_ctx(c
), isl_error_invalid
,
12179 "index out of bounds", return isl_constraint_free(c
));
12180 if (pos2
>= isl_constraint_dim(c
, type2
))
12181 isl_die(isl_constraint_get_ctx(c
), isl_error_invalid
,
12182 "index out of bounds", return isl_constraint_free(c
));
12184 if (type1
== type2
&& pos1
== pos2
)
12187 c
= isl_constraint_set_coefficient_si(c
, type1
, pos1
, 1);
12188 c
= isl_constraint_set_coefficient_si(c
, type2
, pos2
, -1);
12193 /* Add a constraint imposing that the value of the first dimension is
12194 * greater than or equal to that of the second.
12196 __isl_give isl_basic_map
*isl_basic_map_order_ge(__isl_take isl_basic_map
*bmap
,
12197 enum isl_dim_type type1
, int pos1
, enum isl_dim_type type2
, int pos2
)
12202 if (type1
== type2
&& pos1
== pos2
)
12204 space
= isl_basic_map_get_space(bmap
);
12205 c
= constraint_order_ge(space
, type1
, pos1
, type2
, pos2
);
12206 bmap
= isl_basic_map_add_constraint(bmap
, c
);
12211 /* Add a constraint imposing that the value of the first dimension is
12212 * greater than or equal to that of the second.
12214 __isl_give isl_map
*isl_map_order_ge(__isl_take isl_map
*map
,
12215 enum isl_dim_type type1
, int pos1
, enum isl_dim_type type2
, int pos2
)
12220 if (type1
== type2
&& pos1
== pos2
)
12222 space
= isl_map_get_space(map
);
12223 c
= constraint_order_ge(space
, type1
, pos1
, type2
, pos2
);
12224 map
= isl_map_add_constraint(map
, c
);
12229 /* Add a constraint imposing that the value of the first dimension is
12230 * less than or equal to that of the second.
12232 __isl_give isl_map
*isl_map_order_le(__isl_take isl_map
*map
,
12233 enum isl_dim_type type1
, int pos1
, enum isl_dim_type type2
, int pos2
)
12235 return isl_map_order_ge(map
, type2
, pos2
, type1
, pos1
);
12238 /* Construct a basic map where the value of the first dimension is
12239 * greater than that of the second.
12241 static __isl_give isl_basic_map
*greator(__isl_take isl_space
*space
,
12242 enum isl_dim_type type1
, int pos1
, enum isl_dim_type type2
, int pos2
)
12244 isl_basic_map
*bmap
= NULL
;
12250 if (pos1
>= isl_space_dim(space
, type1
))
12251 isl_die(isl_space_get_ctx(space
), isl_error_invalid
,
12252 "index out of bounds", goto error
);
12253 if (pos2
>= isl_space_dim(space
, type2
))
12254 isl_die(isl_space_get_ctx(space
), isl_error_invalid
,
12255 "index out of bounds", goto error
);
12257 if (type1
== type2
&& pos1
== pos2
)
12258 return isl_basic_map_empty(space
);
12260 bmap
= isl_basic_map_alloc_space(space
, 0, 0, 1);
12261 i
= isl_basic_map_alloc_inequality(bmap
);
12263 return isl_basic_map_free(bmap
);
12264 isl_seq_clr(bmap
->ineq
[i
], 1 + isl_basic_map_total_dim(bmap
));
12265 pos1
+= isl_basic_map_offset(bmap
, type1
);
12266 pos2
+= isl_basic_map_offset(bmap
, type2
);
12267 isl_int_set_si(bmap
->ineq
[i
][pos1
], 1);
12268 isl_int_set_si(bmap
->ineq
[i
][pos2
], -1);
12269 isl_int_set_si(bmap
->ineq
[i
][0], -1);
12270 bmap
= isl_basic_map_finalize(bmap
);
12274 isl_space_free(space
);
12275 isl_basic_map_free(bmap
);
12279 /* Add a constraint imposing that the value of the first dimension is
12280 * greater than that of the second.
12282 __isl_give isl_basic_map
*isl_basic_map_order_gt(__isl_take isl_basic_map
*bmap
,
12283 enum isl_dim_type type1
, int pos1
, enum isl_dim_type type2
, int pos2
)
12287 gt
= greator(isl_basic_map_get_space(bmap
), type1
, pos1
, type2
, pos2
);
12289 bmap
= isl_basic_map_intersect(bmap
, gt
);
12294 /* Add a constraint imposing that the value of the first dimension is
12295 * greater than that of the second.
12297 __isl_give isl_map
*isl_map_order_gt(__isl_take isl_map
*map
,
12298 enum isl_dim_type type1
, int pos1
, enum isl_dim_type type2
, int pos2
)
12300 isl_basic_map
*bmap
;
12302 bmap
= greator(isl_map_get_space(map
), type1
, pos1
, type2
, pos2
);
12304 map
= isl_map_intersect(map
, isl_map_from_basic_map(bmap
));
12309 /* Add a constraint imposing that the value of the first dimension is
12310 * smaller than that of the second.
12312 __isl_give isl_map
*isl_map_order_lt(__isl_take isl_map
*map
,
12313 enum isl_dim_type type1
, int pos1
, enum isl_dim_type type2
, int pos2
)
12315 return isl_map_order_gt(map
, type2
, pos2
, type1
, pos1
);
12318 __isl_give isl_aff
*isl_basic_map_get_div(__isl_keep isl_basic_map
*bmap
,
12322 isl_local_space
*ls
;
12327 if (!isl_basic_map_divs_known(bmap
))
12328 isl_die(isl_basic_map_get_ctx(bmap
), isl_error_invalid
,
12329 "some divs are unknown", return NULL
);
12331 ls
= isl_basic_map_get_local_space(bmap
);
12332 div
= isl_local_space_get_div(ls
, pos
);
12333 isl_local_space_free(ls
);
12338 __isl_give isl_aff
*isl_basic_set_get_div(__isl_keep isl_basic_set
*bset
,
12341 return isl_basic_map_get_div(bset
, pos
);
12344 /* Plug in "subs" for dimension "type", "pos" of "bset".
12346 * Let i be the dimension to replace and let "subs" be of the form
12350 * Any integer division with a non-zero coefficient for i,
12352 * floor((a i + g)/m)
12356 * floor((a f + d g)/(m d))
12358 * Constraints of the form
12366 * We currently require that "subs" is an integral expression.
12367 * Handling rational expressions may require us to add stride constraints
12368 * as we do in isl_basic_set_preimage_multi_aff.
12370 __isl_give isl_basic_set
*isl_basic_set_substitute(
12371 __isl_take isl_basic_set
*bset
,
12372 enum isl_dim_type type
, unsigned pos
, __isl_keep isl_aff
*subs
)
12378 if (bset
&& isl_basic_set_plain_is_empty(bset
))
12381 bset
= isl_basic_set_cow(bset
);
12382 if (!bset
|| !subs
)
12385 ctx
= isl_basic_set_get_ctx(bset
);
12386 if (!isl_space_is_equal(bset
->dim
, subs
->ls
->dim
))
12387 isl_die(ctx
, isl_error_invalid
,
12388 "spaces don't match", goto error
);
12389 if (isl_local_space_dim(subs
->ls
, isl_dim_div
) != 0)
12390 isl_die(ctx
, isl_error_unsupported
,
12391 "cannot handle divs yet", goto error
);
12392 if (!isl_int_is_one(subs
->v
->el
[0]))
12393 isl_die(ctx
, isl_error_invalid
,
12394 "can only substitute integer expressions", goto error
);
12396 pos
+= isl_basic_set_offset(bset
, type
);
12400 for (i
= 0; i
< bset
->n_eq
; ++i
) {
12401 if (isl_int_is_zero(bset
->eq
[i
][pos
]))
12403 isl_int_set(v
, bset
->eq
[i
][pos
]);
12404 isl_int_set_si(bset
->eq
[i
][pos
], 0);
12405 isl_seq_combine(bset
->eq
[i
], subs
->v
->el
[0], bset
->eq
[i
],
12406 v
, subs
->v
->el
+ 1, subs
->v
->size
- 1);
12409 for (i
= 0; i
< bset
->n_ineq
; ++i
) {
12410 if (isl_int_is_zero(bset
->ineq
[i
][pos
]))
12412 isl_int_set(v
, bset
->ineq
[i
][pos
]);
12413 isl_int_set_si(bset
->ineq
[i
][pos
], 0);
12414 isl_seq_combine(bset
->ineq
[i
], subs
->v
->el
[0], bset
->ineq
[i
],
12415 v
, subs
->v
->el
+ 1, subs
->v
->size
- 1);
12418 for (i
= 0; i
< bset
->n_div
; ++i
) {
12419 if (isl_int_is_zero(bset
->div
[i
][1 + pos
]))
12421 isl_int_set(v
, bset
->div
[i
][1 + pos
]);
12422 isl_int_set_si(bset
->div
[i
][1 + pos
], 0);
12423 isl_seq_combine(bset
->div
[i
] + 1,
12424 subs
->v
->el
[0], bset
->div
[i
] + 1,
12425 v
, subs
->v
->el
+ 1, subs
->v
->size
- 1);
12426 isl_int_mul(bset
->div
[i
][0], bset
->div
[i
][0], subs
->v
->el
[0]);
12431 bset
= isl_basic_set_simplify(bset
);
12432 return isl_basic_set_finalize(bset
);
12434 isl_basic_set_free(bset
);
12438 /* Plug in "subs" for dimension "type", "pos" of "set".
12440 __isl_give isl_set
*isl_set_substitute(__isl_take isl_set
*set
,
12441 enum isl_dim_type type
, unsigned pos
, __isl_keep isl_aff
*subs
)
12445 if (set
&& isl_set_plain_is_empty(set
))
12448 set
= isl_set_cow(set
);
12452 for (i
= set
->n
- 1; i
>= 0; --i
) {
12453 set
->p
[i
] = isl_basic_set_substitute(set
->p
[i
], type
, pos
, subs
);
12454 if (remove_if_empty(set
, i
) < 0)
12464 /* Check if the range of "ma" is compatible with the domain or range
12465 * (depending on "type") of "bmap".
12466 * Return -1 if anything is wrong.
12468 static int check_basic_map_compatible_range_multi_aff(
12469 __isl_keep isl_basic_map
*bmap
, enum isl_dim_type type
,
12470 __isl_keep isl_multi_aff
*ma
)
12473 isl_space
*ma_space
;
12475 ma_space
= isl_multi_aff_get_space(ma
);
12477 m
= isl_space_match(bmap
->dim
, isl_dim_param
, ma_space
, isl_dim_param
);
12481 isl_die(isl_basic_map_get_ctx(bmap
), isl_error_invalid
,
12482 "parameters don't match", goto error
);
12483 m
= isl_space_tuple_is_equal(bmap
->dim
, type
, ma_space
, isl_dim_out
);
12487 isl_die(isl_basic_map_get_ctx(bmap
), isl_error_invalid
,
12488 "spaces don't match", goto error
);
12490 isl_space_free(ma_space
);
12493 isl_space_free(ma_space
);
12497 /* Copy the divs from "ma" to "bmap", adding zeros for the "n_before"
12498 * coefficients before the transformed range of dimensions,
12499 * the "n_after" coefficients after the transformed range of dimensions
12500 * and the coefficients of the other divs in "bmap".
12502 static int set_ma_divs(__isl_keep isl_basic_map
*bmap
,
12503 __isl_keep isl_multi_aff
*ma
, int n_before
, int n_after
, int n_div
)
12508 isl_local_space
*ls
;
12513 ls
= isl_aff_get_domain_local_space(ma
->p
[0]);
12517 n_param
= isl_local_space_dim(ls
, isl_dim_param
);
12518 n_set
= isl_local_space_dim(ls
, isl_dim_set
);
12519 for (i
= 0; i
< n_div
; ++i
) {
12520 int o_bmap
= 0, o_ls
= 0;
12522 isl_seq_cpy(bmap
->div
[i
], ls
->div
->row
[i
], 1 + 1 + n_param
);
12523 o_bmap
+= 1 + 1 + n_param
;
12524 o_ls
+= 1 + 1 + n_param
;
12525 isl_seq_clr(bmap
->div
[i
] + o_bmap
, n_before
);
12526 o_bmap
+= n_before
;
12527 isl_seq_cpy(bmap
->div
[i
] + o_bmap
,
12528 ls
->div
->row
[i
] + o_ls
, n_set
);
12531 isl_seq_clr(bmap
->div
[i
] + o_bmap
, n_after
);
12533 isl_seq_cpy(bmap
->div
[i
] + o_bmap
,
12534 ls
->div
->row
[i
] + o_ls
, n_div
);
12537 isl_seq_clr(bmap
->div
[i
] + o_bmap
, bmap
->n_div
- n_div
);
12538 if (isl_basic_set_add_div_constraints(bmap
, i
) < 0)
12542 isl_local_space_free(ls
);
12545 isl_local_space_free(ls
);
12549 /* How many stride constraints does "ma" enforce?
12550 * That is, how many of the affine expressions have a denominator
12551 * different from one?
12553 static int multi_aff_strides(__isl_keep isl_multi_aff
*ma
)
12558 for (i
= 0; i
< ma
->n
; ++i
)
12559 if (!isl_int_is_one(ma
->p
[i
]->v
->el
[0]))
12565 /* For each affine expression in ma of the form
12567 * x_i = (f_i y + h_i)/m_i
12569 * with m_i different from one, add a constraint to "bmap"
12572 * f_i y + h_i = m_i alpha_i
12574 * with alpha_i an additional existentially quantified variable.
12576 * The input variables of "ma" correspond to a subset of the variables
12577 * of "bmap". There are "n_before" variables in "bmap" before this
12578 * subset and "n_after" variables after this subset.
12579 * The integer divisions of the affine expressions in "ma" are assumed
12580 * to have been aligned. There are "n_div_ma" of them and
12581 * they appear first in "bmap", straight after the "n_after" variables.
12583 static __isl_give isl_basic_map
*add_ma_strides(
12584 __isl_take isl_basic_map
*bmap
, __isl_keep isl_multi_aff
*ma
,
12585 int n_before
, int n_after
, int n_div_ma
)
12593 total
= isl_basic_map_total_dim(bmap
);
12594 n_param
= isl_multi_aff_dim(ma
, isl_dim_param
);
12595 n_in
= isl_multi_aff_dim(ma
, isl_dim_in
);
12596 for (i
= 0; i
< ma
->n
; ++i
) {
12597 int o_bmap
= 0, o_ma
= 1;
12599 if (isl_int_is_one(ma
->p
[i
]->v
->el
[0]))
12601 div
= isl_basic_map_alloc_div(bmap
);
12602 k
= isl_basic_map_alloc_equality(bmap
);
12603 if (div
< 0 || k
< 0)
12605 isl_int_set_si(bmap
->div
[div
][0], 0);
12606 isl_seq_cpy(bmap
->eq
[k
] + o_bmap
,
12607 ma
->p
[i
]->v
->el
+ o_ma
, 1 + n_param
);
12608 o_bmap
+= 1 + n_param
;
12609 o_ma
+= 1 + n_param
;
12610 isl_seq_clr(bmap
->eq
[k
] + o_bmap
, n_before
);
12611 o_bmap
+= n_before
;
12612 isl_seq_cpy(bmap
->eq
[k
] + o_bmap
,
12613 ma
->p
[i
]->v
->el
+ o_ma
, n_in
);
12616 isl_seq_clr(bmap
->eq
[k
] + o_bmap
, n_after
);
12618 isl_seq_cpy(bmap
->eq
[k
] + o_bmap
,
12619 ma
->p
[i
]->v
->el
+ o_ma
, n_div_ma
);
12620 o_bmap
+= n_div_ma
;
12622 isl_seq_clr(bmap
->eq
[k
] + o_bmap
, 1 + total
- o_bmap
);
12623 isl_int_neg(bmap
->eq
[k
][1 + total
], ma
->p
[i
]->v
->el
[0]);
12629 isl_basic_map_free(bmap
);
12633 /* Replace the domain or range space (depending on "type) of "space" by "set".
12635 static __isl_give isl_space
*isl_space_set(__isl_take isl_space
*space
,
12636 enum isl_dim_type type
, __isl_take isl_space
*set
)
12638 if (type
== isl_dim_in
) {
12639 space
= isl_space_range(space
);
12640 space
= isl_space_map_from_domain_and_range(set
, space
);
12642 space
= isl_space_domain(space
);
12643 space
= isl_space_map_from_domain_and_range(space
, set
);
12649 /* Compute the preimage of the domain or range (depending on "type")
12650 * of "bmap" under the function represented by "ma".
12651 * In other words, plug in "ma" in the domain or range of "bmap".
12652 * The result is a basic map that lives in the same space as "bmap"
12653 * except that the domain or range has been replaced by
12654 * the domain space of "ma".
12656 * If bmap is represented by
12658 * A(p) + S u + B x + T v + C(divs) >= 0,
12660 * where u and x are input and output dimensions if type == isl_dim_out
12661 * while x and v are input and output dimensions if type == isl_dim_in,
12662 * and ma is represented by
12664 * x = D(p) + F(y) + G(divs')
12666 * then the result is
12668 * A(p) + B D(p) + S u + B F(y) + T v + B G(divs') + C(divs) >= 0
12670 * The divs in the input set are similarly adjusted.
12673 * floor((a_i(p) + s u + b_i x + t v + c_i(divs))/n_i)
12677 * floor((a_i(p) + b_i D(p) + s u + b_i F(y) + t v +
12678 * B_i G(divs') + c_i(divs))/n_i)
12680 * If bmap is not a rational map and if F(y) involves any denominators
12682 * x_i = (f_i y + h_i)/m_i
12684 * then additional constraints are added to ensure that we only
12685 * map back integer points. That is we enforce
12687 * f_i y + h_i = m_i alpha_i
12689 * with alpha_i an additional existentially quantified variable.
12691 * We first copy over the divs from "ma".
12692 * Then we add the modified constraints and divs from "bmap".
12693 * Finally, we add the stride constraints, if needed.
12695 __isl_give isl_basic_map
*isl_basic_map_preimage_multi_aff(
12696 __isl_take isl_basic_map
*bmap
, enum isl_dim_type type
,
12697 __isl_take isl_multi_aff
*ma
)
12701 isl_basic_map
*res
= NULL
;
12702 int n_before
, n_after
, n_div_bmap
, n_div_ma
;
12703 isl_int f
, c1
, c2
, g
;
12704 int rational
, strides
;
12711 ma
= isl_multi_aff_align_divs(ma
);
12714 if (check_basic_map_compatible_range_multi_aff(bmap
, type
, ma
) < 0)
12717 if (type
== isl_dim_in
) {
12719 n_after
= isl_basic_map_dim(bmap
, isl_dim_out
);
12721 n_before
= isl_basic_map_dim(bmap
, isl_dim_in
);
12724 n_div_bmap
= isl_basic_map_dim(bmap
, isl_dim_div
);
12725 n_div_ma
= ma
->n
? isl_aff_dim(ma
->p
[0], isl_dim_div
) : 0;
12727 space
= isl_multi_aff_get_domain_space(ma
);
12728 space
= isl_space_set(isl_basic_map_get_space(bmap
), type
, space
);
12729 rational
= isl_basic_map_is_rational(bmap
);
12730 strides
= rational
? 0 : multi_aff_strides(ma
);
12731 res
= isl_basic_map_alloc_space(space
, n_div_ma
+ n_div_bmap
+ strides
,
12732 bmap
->n_eq
+ strides
, bmap
->n_ineq
+ 2 * n_div_ma
);
12734 res
= isl_basic_map_set_rational(res
);
12736 for (i
= 0; i
< n_div_ma
+ n_div_bmap
; ++i
)
12737 if (isl_basic_map_alloc_div(res
) < 0)
12740 if (set_ma_divs(res
, ma
, n_before
, n_after
, n_div_ma
) < 0)
12743 for (i
= 0; i
< bmap
->n_eq
; ++i
) {
12744 k
= isl_basic_map_alloc_equality(res
);
12747 isl_seq_preimage(res
->eq
[k
], bmap
->eq
[i
], ma
, n_before
,
12748 n_after
, n_div_ma
, n_div_bmap
, f
, c1
, c2
, g
, 0);
12751 for (i
= 0; i
< bmap
->n_ineq
; ++i
) {
12752 k
= isl_basic_map_alloc_inequality(res
);
12755 isl_seq_preimage(res
->ineq
[k
], bmap
->ineq
[i
], ma
, n_before
,
12756 n_after
, n_div_ma
, n_div_bmap
, f
, c1
, c2
, g
, 0);
12759 for (i
= 0; i
< bmap
->n_div
; ++i
) {
12760 if (isl_int_is_zero(bmap
->div
[i
][0])) {
12761 isl_int_set_si(res
->div
[n_div_ma
+ i
][0], 0);
12764 isl_seq_preimage(res
->div
[n_div_ma
+ i
], bmap
->div
[i
], ma
,
12765 n_before
, n_after
, n_div_ma
, n_div_bmap
,
12770 res
= add_ma_strides(res
, ma
, n_before
, n_after
, n_div_ma
);
12776 isl_basic_map_free(bmap
);
12777 isl_multi_aff_free(ma
);
12778 res
= isl_basic_set_simplify(res
);
12779 return isl_basic_map_finalize(res
);
12785 isl_basic_map_free(bmap
);
12786 isl_multi_aff_free(ma
);
12787 isl_basic_map_free(res
);
12791 /* Compute the preimage of "bset" under the function represented by "ma".
12792 * In other words, plug in "ma" in "bset". The result is a basic set
12793 * that lives in the domain space of "ma".
12795 __isl_give isl_basic_set
*isl_basic_set_preimage_multi_aff(
12796 __isl_take isl_basic_set
*bset
, __isl_take isl_multi_aff
*ma
)
12798 return isl_basic_map_preimage_multi_aff(bset
, isl_dim_set
, ma
);
12801 /* Compute the preimage of the domain of "bmap" under the function
12802 * represented by "ma".
12803 * In other words, plug in "ma" in the domain of "bmap".
12804 * The result is a basic map that lives in the same space as "bmap"
12805 * except that the domain has been replaced by the domain space of "ma".
12807 __isl_give isl_basic_map
*isl_basic_map_preimage_domain_multi_aff(
12808 __isl_take isl_basic_map
*bmap
, __isl_take isl_multi_aff
*ma
)
12810 return isl_basic_map_preimage_multi_aff(bmap
, isl_dim_in
, ma
);
12813 /* Compute the preimage of the range of "bmap" under the function
12814 * represented by "ma".
12815 * In other words, plug in "ma" in the range of "bmap".
12816 * The result is a basic map that lives in the same space as "bmap"
12817 * except that the range has been replaced by the domain space of "ma".
12819 __isl_give isl_basic_map
*isl_basic_map_preimage_range_multi_aff(
12820 __isl_take isl_basic_map
*bmap
, __isl_take isl_multi_aff
*ma
)
12822 return isl_basic_map_preimage_multi_aff(bmap
, isl_dim_out
, ma
);
12825 /* Check if the range of "ma" is compatible with the domain or range
12826 * (depending on "type") of "map".
12827 * Return -1 if anything is wrong.
12829 static int check_map_compatible_range_multi_aff(
12830 __isl_keep isl_map
*map
, enum isl_dim_type type
,
12831 __isl_keep isl_multi_aff
*ma
)
12834 isl_space
*ma_space
;
12836 ma_space
= isl_multi_aff_get_space(ma
);
12837 m
= isl_space_tuple_is_equal(map
->dim
, type
, ma_space
, isl_dim_out
);
12838 isl_space_free(ma_space
);
12840 isl_die(isl_map_get_ctx(map
), isl_error_invalid
,
12841 "spaces don't match", return -1);
12845 /* Compute the preimage of the domain or range (depending on "type")
12846 * of "map" under the function represented by "ma".
12847 * In other words, plug in "ma" in the domain or range of "map".
12848 * The result is a map that lives in the same space as "map"
12849 * except that the domain or range has been replaced by
12850 * the domain space of "ma".
12852 * The parameters are assumed to have been aligned.
12854 static __isl_give isl_map
*map_preimage_multi_aff(__isl_take isl_map
*map
,
12855 enum isl_dim_type type
, __isl_take isl_multi_aff
*ma
)
12860 map
= isl_map_cow(map
);
12861 ma
= isl_multi_aff_align_divs(ma
);
12864 if (check_map_compatible_range_multi_aff(map
, type
, ma
) < 0)
12867 for (i
= 0; i
< map
->n
; ++i
) {
12868 map
->p
[i
] = isl_basic_map_preimage_multi_aff(map
->p
[i
], type
,
12869 isl_multi_aff_copy(ma
));
12874 space
= isl_multi_aff_get_domain_space(ma
);
12875 space
= isl_space_set(isl_map_get_space(map
), type
, space
);
12877 isl_space_free(map
->dim
);
12882 isl_multi_aff_free(ma
);
12884 ISL_F_CLR(map
, ISL_MAP_DISJOINT
);
12885 ISL_F_CLR(map
, ISL_SET_NORMALIZED
);
12888 isl_multi_aff_free(ma
);
12893 /* Compute the preimage of the domain or range (depending on "type")
12894 * of "map" under the function represented by "ma".
12895 * In other words, plug in "ma" in the domain or range of "map".
12896 * The result is a map that lives in the same space as "map"
12897 * except that the domain or range has been replaced by
12898 * the domain space of "ma".
12900 __isl_give isl_map
*isl_map_preimage_multi_aff(__isl_take isl_map
*map
,
12901 enum isl_dim_type type
, __isl_take isl_multi_aff
*ma
)
12906 if (isl_space_match(map
->dim
, isl_dim_param
, ma
->space
, isl_dim_param
))
12907 return map_preimage_multi_aff(map
, type
, ma
);
12909 if (!isl_space_has_named_params(map
->dim
) ||
12910 !isl_space_has_named_params(ma
->space
))
12911 isl_die(map
->ctx
, isl_error_invalid
,
12912 "unaligned unnamed parameters", goto error
);
12913 map
= isl_map_align_params(map
, isl_multi_aff_get_space(ma
));
12914 ma
= isl_multi_aff_align_params(ma
, isl_map_get_space(map
));
12916 return map_preimage_multi_aff(map
, type
, ma
);
12918 isl_multi_aff_free(ma
);
12919 return isl_map_free(map
);
12922 /* Compute the preimage of "set" under the function represented by "ma".
12923 * In other words, plug in "ma" in "set". The result is a set
12924 * that lives in the domain space of "ma".
12926 __isl_give isl_set
*isl_set_preimage_multi_aff(__isl_take isl_set
*set
,
12927 __isl_take isl_multi_aff
*ma
)
12929 return isl_map_preimage_multi_aff(set
, isl_dim_set
, ma
);
12932 /* Compute the preimage of the domain of "map" under the function
12933 * represented by "ma".
12934 * In other words, plug in "ma" in the domain of "map".
12935 * The result is a map that lives in the same space as "map"
12936 * except that the domain has been replaced by the domain space of "ma".
12938 __isl_give isl_map
*isl_map_preimage_domain_multi_aff(__isl_take isl_map
*map
,
12939 __isl_take isl_multi_aff
*ma
)
12941 return isl_map_preimage_multi_aff(map
, isl_dim_in
, ma
);
12944 /* Compute the preimage of the range of "map" under the function
12945 * represented by "ma".
12946 * In other words, plug in "ma" in the range of "map".
12947 * The result is a map that lives in the same space as "map"
12948 * except that the range has been replaced by the domain space of "ma".
12950 __isl_give isl_map
*isl_map_preimage_range_multi_aff(__isl_take isl_map
*map
,
12951 __isl_take isl_multi_aff
*ma
)
12953 return isl_map_preimage_multi_aff(map
, isl_dim_out
, ma
);
12956 /* Compute the preimage of "map" under the function represented by "pma".
12957 * In other words, plug in "pma" in the domain or range of "map".
12958 * The result is a map that lives in the same space as "map",
12959 * except that the space of type "type" has been replaced by
12960 * the domain space of "pma".
12962 * The parameters of "map" and "pma" are assumed to have been aligned.
12964 static __isl_give isl_map
*isl_map_preimage_pw_multi_aff_aligned(
12965 __isl_take isl_map
*map
, enum isl_dim_type type
,
12966 __isl_take isl_pw_multi_aff
*pma
)
12975 isl_pw_multi_aff_free(pma
);
12976 res
= isl_map_empty(isl_map_get_space(map
));
12981 res
= isl_map_preimage_multi_aff(isl_map_copy(map
), type
,
12982 isl_multi_aff_copy(pma
->p
[0].maff
));
12983 if (type
== isl_dim_in
)
12984 res
= isl_map_intersect_domain(res
,
12985 isl_map_copy(pma
->p
[0].set
));
12987 res
= isl_map_intersect_range(res
,
12988 isl_map_copy(pma
->p
[0].set
));
12990 for (i
= 1; i
< pma
->n
; ++i
) {
12993 res_i
= isl_map_preimage_multi_aff(isl_map_copy(map
), type
,
12994 isl_multi_aff_copy(pma
->p
[i
].maff
));
12995 if (type
== isl_dim_in
)
12996 res_i
= isl_map_intersect_domain(res_i
,
12997 isl_map_copy(pma
->p
[i
].set
));
12999 res_i
= isl_map_intersect_range(res_i
,
13000 isl_map_copy(pma
->p
[i
].set
));
13001 res
= isl_map_union(res
, res_i
);
13004 isl_pw_multi_aff_free(pma
);
13008 isl_pw_multi_aff_free(pma
);
13013 /* Compute the preimage of "map" under the function represented by "pma".
13014 * In other words, plug in "pma" in the domain or range of "map".
13015 * The result is a map that lives in the same space as "map",
13016 * except that the space of type "type" has been replaced by
13017 * the domain space of "pma".
13019 __isl_give isl_map
*isl_map_preimage_pw_multi_aff(__isl_take isl_map
*map
,
13020 enum isl_dim_type type
, __isl_take isl_pw_multi_aff
*pma
)
13025 if (isl_space_match(map
->dim
, isl_dim_param
, pma
->dim
, isl_dim_param
))
13026 return isl_map_preimage_pw_multi_aff_aligned(map
, type
, pma
);
13028 if (!isl_space_has_named_params(map
->dim
) ||
13029 !isl_space_has_named_params(pma
->dim
))
13030 isl_die(map
->ctx
, isl_error_invalid
,
13031 "unaligned unnamed parameters", goto error
);
13032 map
= isl_map_align_params(map
, isl_pw_multi_aff_get_space(pma
));
13033 pma
= isl_pw_multi_aff_align_params(pma
, isl_map_get_space(map
));
13035 return isl_map_preimage_pw_multi_aff_aligned(map
, type
, pma
);
13037 isl_pw_multi_aff_free(pma
);
13038 return isl_map_free(map
);
13041 /* Compute the preimage of "set" under the function represented by "pma".
13042 * In other words, plug in "pma" in "set". The result is a set
13043 * that lives in the domain space of "pma".
13045 __isl_give isl_set
*isl_set_preimage_pw_multi_aff(__isl_take isl_set
*set
,
13046 __isl_take isl_pw_multi_aff
*pma
)
13048 return isl_map_preimage_pw_multi_aff(set
, isl_dim_set
, pma
);
13051 /* Compute the preimage of the domain of "map" under the function
13052 * represented by "pma".
13053 * In other words, plug in "pma" in the domain of "map".
13054 * The result is a map that lives in the same space as "map",
13055 * except that domain space has been replaced by the domain space of "pma".
13057 __isl_give isl_map
*isl_map_preimage_domain_pw_multi_aff(
13058 __isl_take isl_map
*map
, __isl_take isl_pw_multi_aff
*pma
)
13060 return isl_map_preimage_pw_multi_aff(map
, isl_dim_in
, pma
);
13063 /* Compute the preimage of the range of "map" under the function
13064 * represented by "pma".
13065 * In other words, plug in "pma" in the range of "map".
13066 * The result is a map that lives in the same space as "map",
13067 * except that range space has been replaced by the domain space of "pma".
13069 __isl_give isl_map
*isl_map_preimage_range_pw_multi_aff(
13070 __isl_take isl_map
*map
, __isl_take isl_pw_multi_aff
*pma
)
13072 return isl_map_preimage_pw_multi_aff(map
, isl_dim_out
, pma
);
13075 /* Compute the preimage of "map" under the function represented by "mpa".
13076 * In other words, plug in "mpa" in the domain or range of "map".
13077 * The result is a map that lives in the same space as "map",
13078 * except that the space of type "type" has been replaced by
13079 * the domain space of "mpa".
13081 * If the map does not involve any constraints that refer to the
13082 * dimensions of the substituted space, then the only possible
13083 * effect of "mpa" on the map is to map the space to a different space.
13084 * We create a separate isl_multi_aff to effectuate this change
13085 * in order to avoid spurious splitting of the map along the pieces
13088 __isl_give isl_map
*isl_map_preimage_multi_pw_aff(__isl_take isl_map
*map
,
13089 enum isl_dim_type type
, __isl_take isl_multi_pw_aff
*mpa
)
13092 isl_pw_multi_aff
*pma
;
13097 n
= isl_map_dim(map
, type
);
13098 if (!isl_map_involves_dims(map
, type
, 0, n
)) {
13102 space
= isl_multi_pw_aff_get_space(mpa
);
13103 isl_multi_pw_aff_free(mpa
);
13104 ma
= isl_multi_aff_zero(space
);
13105 return isl_map_preimage_multi_aff(map
, type
, ma
);
13108 pma
= isl_pw_multi_aff_from_multi_pw_aff(mpa
);
13109 return isl_map_preimage_pw_multi_aff(map
, type
, pma
);
13112 isl_multi_pw_aff_free(mpa
);
13116 /* Compute the preimage of "map" under the function represented by "mpa".
13117 * In other words, plug in "mpa" in the domain "map".
13118 * The result is a map that lives in the same space as "map",
13119 * except that domain space has been replaced by the domain space of "mpa".
13121 __isl_give isl_map
*isl_map_preimage_domain_multi_pw_aff(
13122 __isl_take isl_map
*map
, __isl_take isl_multi_pw_aff
*mpa
)
13124 return isl_map_preimage_multi_pw_aff(map
, isl_dim_in
, mpa
);
13127 /* Compute the preimage of "set" by the function represented by "mpa".
13128 * In other words, plug in "mpa" in "set".
13130 __isl_give isl_set
*isl_set_preimage_multi_pw_aff(__isl_take isl_set
*set
,
13131 __isl_take isl_multi_pw_aff
*mpa
)
13133 return isl_map_preimage_multi_pw_aff(set
, isl_dim_set
, mpa
);