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 INRIA Paris
7 * Copyright 2016 Sven Verdoolaege
9 * Use of this software is governed by the MIT license
11 * Written by Sven Verdoolaege, K.U.Leuven, Departement
12 * Computerwetenschappen, Celestijnenlaan 200A, B-3001 Leuven, Belgium
13 * and INRIA Saclay - Ile-de-France, Parc Club Orsay Universite,
14 * ZAC des vignes, 4 rue Jacques Monod, 91893 Orsay, France
15 * and Ecole Normale Superieure, 45 rue d’Ulm, 75230 Paris, France
16 * and Inria Paris - Rocquencourt, Domaine de Voluceau - Rocquencourt,
17 * B.P. 105 - 78153 Le Chesnay, France
18 * and Centre de Recherche Inria de Paris, 2 rue Simone Iff - Voie DQ12,
19 * CS 42112, 75589 Paris Cedex 12, France
23 #include <isl_ctx_private.h>
24 #include <isl_map_private.h>
26 #include <isl/constraint.h>
27 #include "isl_space_private.h"
28 #include "isl_equalities.h"
29 #include <isl_lp_private.h>
33 #include <isl_reordering.h>
34 #include "isl_sample.h"
38 #include <isl_mat_private.h>
39 #include <isl_vec_private.h>
40 #include <isl_dim_map.h>
41 #include <isl_local_space_private.h>
42 #include <isl_aff_private.h>
43 #include <isl_options_private.h>
44 #include <isl_morph.h>
45 #include <isl_val_private.h>
46 #include <isl/deprecated/map_int.h>
47 #include <isl/deprecated/set_int.h>
49 #include <bset_to_bmap.c>
50 #include <bset_from_bmap.c>
51 #include <set_to_map.c>
52 #include <set_from_map.c>
54 static unsigned n(__isl_keep isl_space
*dim
, enum isl_dim_type type
)
57 case isl_dim_param
: return dim
->nparam
;
58 case isl_dim_in
: return dim
->n_in
;
59 case isl_dim_out
: return dim
->n_out
;
60 case isl_dim_all
: return dim
->nparam
+ dim
->n_in
+ dim
->n_out
;
65 static unsigned pos(__isl_keep isl_space
*dim
, enum isl_dim_type type
)
68 case isl_dim_param
: return 1;
69 case isl_dim_in
: return 1 + dim
->nparam
;
70 case isl_dim_out
: return 1 + dim
->nparam
+ dim
->n_in
;
75 unsigned isl_basic_map_dim(__isl_keep isl_basic_map
*bmap
,
76 enum isl_dim_type type
)
81 case isl_dim_cst
: return 1;
84 case isl_dim_out
: return isl_space_dim(bmap
->dim
, type
);
85 case isl_dim_div
: return bmap
->n_div
;
86 case isl_dim_all
: return isl_basic_map_total_dim(bmap
);
91 /* Return the space of "map".
93 __isl_keep isl_space
*isl_map_peek_space(__isl_keep
const isl_map
*map
)
95 return map
? map
->dim
: NULL
;
98 unsigned isl_map_dim(__isl_keep isl_map
*map
, enum isl_dim_type type
)
100 return map
? n(map
->dim
, type
) : 0;
103 unsigned isl_set_dim(__isl_keep isl_set
*set
, enum isl_dim_type type
)
105 return set
? n(set
->dim
, type
) : 0;
108 unsigned isl_basic_map_offset(struct isl_basic_map
*bmap
,
109 enum isl_dim_type type
)
118 case isl_dim_cst
: return 0;
119 case isl_dim_param
: return 1;
120 case isl_dim_in
: return 1 + space
->nparam
;
121 case isl_dim_out
: return 1 + space
->nparam
+ space
->n_in
;
122 case isl_dim_div
: return 1 + space
->nparam
+ space
->n_in
+
128 unsigned isl_basic_set_offset(struct isl_basic_set
*bset
,
129 enum isl_dim_type type
)
131 return isl_basic_map_offset(bset
, type
);
134 static unsigned map_offset(struct isl_map
*map
, enum isl_dim_type type
)
136 return pos(map
->dim
, type
);
139 unsigned isl_basic_set_dim(__isl_keep isl_basic_set
*bset
,
140 enum isl_dim_type type
)
142 return isl_basic_map_dim(bset
, type
);
145 unsigned isl_basic_set_n_dim(__isl_keep isl_basic_set
*bset
)
147 return isl_basic_set_dim(bset
, isl_dim_set
);
150 unsigned isl_basic_set_n_param(__isl_keep isl_basic_set
*bset
)
152 return isl_basic_set_dim(bset
, isl_dim_param
);
155 unsigned isl_basic_set_total_dim(const struct isl_basic_set
*bset
)
159 return isl_space_dim(bset
->dim
, isl_dim_all
) + bset
->n_div
;
162 unsigned isl_set_n_dim(__isl_keep isl_set
*set
)
164 return isl_set_dim(set
, isl_dim_set
);
167 unsigned isl_set_n_param(__isl_keep isl_set
*set
)
169 return isl_set_dim(set
, isl_dim_param
);
172 unsigned isl_basic_map_n_in(__isl_keep
const isl_basic_map
*bmap
)
174 return bmap
? bmap
->dim
->n_in
: 0;
177 unsigned isl_basic_map_n_out(__isl_keep
const isl_basic_map
*bmap
)
179 return bmap
? bmap
->dim
->n_out
: 0;
182 unsigned isl_basic_map_n_param(__isl_keep
const isl_basic_map
*bmap
)
184 return bmap
? bmap
->dim
->nparam
: 0;
187 unsigned isl_basic_map_n_div(__isl_keep
const isl_basic_map
*bmap
)
189 return bmap
? bmap
->n_div
: 0;
192 unsigned isl_basic_map_total_dim(const struct isl_basic_map
*bmap
)
194 return bmap
? isl_space_dim(bmap
->dim
, isl_dim_all
) + bmap
->n_div
: 0;
197 unsigned isl_map_n_in(const struct isl_map
*map
)
199 return map
? map
->dim
->n_in
: 0;
202 unsigned isl_map_n_out(const struct isl_map
*map
)
204 return map
? map
->dim
->n_out
: 0;
207 unsigned isl_map_n_param(const struct isl_map
*map
)
209 return map
? map
->dim
->nparam
: 0;
212 /* Do "bmap1" and "bmap2" have the same parameters?
214 static isl_bool
isl_basic_map_has_equal_params(__isl_keep isl_basic_map
*bmap1
,
215 __isl_keep isl_basic_map
*bmap2
)
217 isl_space
*space1
, *space2
;
219 space1
= isl_basic_map_peek_space(bmap1
);
220 space2
= isl_basic_map_peek_space(bmap2
);
221 return isl_space_has_equal_params(space1
, space2
);
224 /* Do "map1" and "map2" have the same parameters?
226 isl_bool
isl_map_has_equal_params(__isl_keep isl_map
*map1
,
227 __isl_keep isl_map
*map2
)
229 isl_space
*space1
, *space2
;
231 space1
= isl_map_peek_space(map1
);
232 space2
= isl_map_peek_space(map2
);
233 return isl_space_has_equal_params(space1
, space2
);
236 /* Do "map" and "set" have the same parameters?
238 static isl_bool
isl_map_set_has_equal_params(__isl_keep isl_map
*map
,
239 __isl_keep isl_set
*set
)
241 return isl_map_has_equal_params(map
, set_to_map(set
));
244 isl_bool
isl_map_compatible_domain(__isl_keep isl_map
*map
,
245 __isl_keep isl_set
*set
)
249 return isl_bool_error
;
250 m
= isl_map_has_equal_params(map
, set_to_map(set
));
253 return isl_space_tuple_is_equal(map
->dim
, isl_dim_in
,
254 set
->dim
, isl_dim_set
);
257 isl_bool
isl_basic_map_compatible_domain(__isl_keep isl_basic_map
*bmap
,
258 __isl_keep isl_basic_set
*bset
)
262 return isl_bool_error
;
263 m
= isl_basic_map_has_equal_params(bmap
, bset_to_bmap(bset
));
266 return isl_space_tuple_is_equal(bmap
->dim
, isl_dim_in
,
267 bset
->dim
, isl_dim_set
);
270 isl_bool
isl_map_compatible_range(__isl_keep isl_map
*map
,
271 __isl_keep isl_set
*set
)
275 return isl_bool_error
;
276 m
= isl_map_has_equal_params(map
, set_to_map(set
));
279 return isl_space_tuple_is_equal(map
->dim
, isl_dim_out
,
280 set
->dim
, isl_dim_set
);
283 isl_bool
isl_basic_map_compatible_range(__isl_keep isl_basic_map
*bmap
,
284 __isl_keep isl_basic_set
*bset
)
288 return isl_bool_error
;
289 m
= isl_basic_map_has_equal_params(bmap
, bset_to_bmap(bset
));
292 return isl_space_tuple_is_equal(bmap
->dim
, isl_dim_out
,
293 bset
->dim
, isl_dim_set
);
296 isl_ctx
*isl_basic_map_get_ctx(__isl_keep isl_basic_map
*bmap
)
298 return bmap
? bmap
->ctx
: NULL
;
301 isl_ctx
*isl_basic_set_get_ctx(__isl_keep isl_basic_set
*bset
)
303 return bset
? bset
->ctx
: NULL
;
306 isl_ctx
*isl_map_get_ctx(__isl_keep isl_map
*map
)
308 return map
? map
->ctx
: NULL
;
311 isl_ctx
*isl_set_get_ctx(__isl_keep isl_set
*set
)
313 return set
? set
->ctx
: NULL
;
316 /* Return the space of "bmap".
318 __isl_keep isl_space
*isl_basic_map_peek_space(
319 __isl_keep
const isl_basic_map
*bmap
)
321 return bmap
? bmap
->dim
: NULL
;
324 /* Return the space of "bset".
326 __isl_keep isl_space
*isl_basic_set_peek_space(__isl_keep isl_basic_set
*bset
)
328 return isl_basic_map_peek_space(bset_to_bmap(bset
));
331 __isl_give isl_space
*isl_basic_map_get_space(__isl_keep isl_basic_map
*bmap
)
333 return isl_space_copy(isl_basic_map_peek_space(bmap
));
336 __isl_give isl_space
*isl_basic_set_get_space(__isl_keep isl_basic_set
*bset
)
338 return isl_basic_map_get_space(bset_to_bmap(bset
));
341 /* Extract the divs in "bmap" as a matrix.
343 __isl_give isl_mat
*isl_basic_map_get_divs(__isl_keep isl_basic_map
*bmap
)
354 ctx
= isl_basic_map_get_ctx(bmap
);
355 total
= isl_space_dim(bmap
->dim
, isl_dim_all
);
356 cols
= 1 + 1 + total
+ bmap
->n_div
;
357 div
= isl_mat_alloc(ctx
, bmap
->n_div
, cols
);
361 for (i
= 0; i
< bmap
->n_div
; ++i
)
362 isl_seq_cpy(div
->row
[i
], bmap
->div
[i
], cols
);
367 /* Extract the divs in "bset" as a matrix.
369 __isl_give isl_mat
*isl_basic_set_get_divs(__isl_keep isl_basic_set
*bset
)
371 return isl_basic_map_get_divs(bset
);
374 __isl_give isl_local_space
*isl_basic_map_get_local_space(
375 __isl_keep isl_basic_map
*bmap
)
382 div
= isl_basic_map_get_divs(bmap
);
383 return isl_local_space_alloc_div(isl_space_copy(bmap
->dim
), div
);
386 __isl_give isl_local_space
*isl_basic_set_get_local_space(
387 __isl_keep isl_basic_set
*bset
)
389 return isl_basic_map_get_local_space(bset
);
392 /* For each known div d = floor(f/m), add the constraints
395 * -(f-(m-1)) + m d >= 0
397 * Do not finalize the result.
399 static __isl_give isl_basic_map
*add_known_div_constraints(
400 __isl_take isl_basic_map
*bmap
)
407 n_div
= isl_basic_map_dim(bmap
, isl_dim_div
);
410 bmap
= isl_basic_map_cow(bmap
);
411 bmap
= isl_basic_map_extend_constraints(bmap
, 0, 2 * n_div
);
414 for (i
= 0; i
< n_div
; ++i
) {
415 if (isl_int_is_zero(bmap
->div
[i
][0]))
417 if (isl_basic_map_add_div_constraints(bmap
, i
) < 0)
418 return isl_basic_map_free(bmap
);
424 __isl_give isl_basic_map
*isl_basic_map_from_local_space(
425 __isl_take isl_local_space
*ls
)
434 n_div
= isl_local_space_dim(ls
, isl_dim_div
);
435 bmap
= isl_basic_map_alloc_space(isl_local_space_get_space(ls
),
436 n_div
, 0, 2 * n_div
);
438 for (i
= 0; i
< n_div
; ++i
)
439 if (isl_basic_map_alloc_div(bmap
) < 0)
442 for (i
= 0; i
< n_div
; ++i
)
443 isl_seq_cpy(bmap
->div
[i
], ls
->div
->row
[i
], ls
->div
->n_col
);
444 bmap
= add_known_div_constraints(bmap
);
446 isl_local_space_free(ls
);
449 isl_local_space_free(ls
);
450 isl_basic_map_free(bmap
);
454 __isl_give isl_basic_set
*isl_basic_set_from_local_space(
455 __isl_take isl_local_space
*ls
)
457 return isl_basic_map_from_local_space(ls
);
460 __isl_give isl_space
*isl_map_get_space(__isl_keep isl_map
*map
)
462 return isl_space_copy(isl_map_peek_space(map
));
465 __isl_give isl_space
*isl_set_get_space(__isl_keep isl_set
*set
)
469 return isl_space_copy(set
->dim
);
472 __isl_give isl_basic_map
*isl_basic_map_set_tuple_name(
473 __isl_take isl_basic_map
*bmap
, enum isl_dim_type type
, const char *s
)
475 bmap
= isl_basic_map_cow(bmap
);
478 bmap
->dim
= isl_space_set_tuple_name(bmap
->dim
, type
, s
);
481 bmap
= isl_basic_map_finalize(bmap
);
484 isl_basic_map_free(bmap
);
488 __isl_give isl_basic_set
*isl_basic_set_set_tuple_name(
489 __isl_take isl_basic_set
*bset
, const char *s
)
491 return isl_basic_map_set_tuple_name(bset
, isl_dim_set
, s
);
494 const char *isl_basic_map_get_tuple_name(__isl_keep isl_basic_map
*bmap
,
495 enum isl_dim_type type
)
497 return bmap
? isl_space_get_tuple_name(bmap
->dim
, type
) : NULL
;
500 __isl_give isl_map
*isl_map_set_tuple_name(__isl_take isl_map
*map
,
501 enum isl_dim_type type
, const char *s
)
505 map
= isl_map_cow(map
);
509 map
->dim
= isl_space_set_tuple_name(map
->dim
, type
, s
);
513 for (i
= 0; i
< map
->n
; ++i
) {
514 map
->p
[i
] = isl_basic_map_set_tuple_name(map
->p
[i
], type
, s
);
525 /* Replace the identifier of the tuple of type "type" by "id".
527 __isl_give isl_basic_map
*isl_basic_map_set_tuple_id(
528 __isl_take isl_basic_map
*bmap
,
529 enum isl_dim_type type
, __isl_take isl_id
*id
)
531 bmap
= isl_basic_map_cow(bmap
);
534 bmap
->dim
= isl_space_set_tuple_id(bmap
->dim
, type
, id
);
536 return isl_basic_map_free(bmap
);
537 bmap
= isl_basic_map_finalize(bmap
);
544 /* Replace the identifier of the tuple by "id".
546 __isl_give isl_basic_set
*isl_basic_set_set_tuple_id(
547 __isl_take isl_basic_set
*bset
, __isl_take isl_id
*id
)
549 return isl_basic_map_set_tuple_id(bset
, isl_dim_set
, id
);
552 /* Does the input or output tuple have a name?
554 isl_bool
isl_map_has_tuple_name(__isl_keep isl_map
*map
, enum isl_dim_type type
)
556 return map
? isl_space_has_tuple_name(map
->dim
, type
) : isl_bool_error
;
559 const char *isl_map_get_tuple_name(__isl_keep isl_map
*map
,
560 enum isl_dim_type type
)
562 return map
? isl_space_get_tuple_name(map
->dim
, type
) : NULL
;
565 __isl_give isl_set
*isl_set_set_tuple_name(__isl_take isl_set
*set
,
568 return set_from_map(isl_map_set_tuple_name(set_to_map(set
),
572 __isl_give isl_map
*isl_map_set_tuple_id(__isl_take isl_map
*map
,
573 enum isl_dim_type type
, __isl_take isl_id
*id
)
575 map
= isl_map_cow(map
);
579 map
->dim
= isl_space_set_tuple_id(map
->dim
, type
, id
);
581 return isl_map_reset_space(map
, isl_space_copy(map
->dim
));
587 __isl_give isl_set
*isl_set_set_tuple_id(__isl_take isl_set
*set
,
588 __isl_take isl_id
*id
)
590 return isl_map_set_tuple_id(set
, isl_dim_set
, id
);
593 __isl_give isl_map
*isl_map_reset_tuple_id(__isl_take isl_map
*map
,
594 enum isl_dim_type type
)
596 map
= isl_map_cow(map
);
600 map
->dim
= isl_space_reset_tuple_id(map
->dim
, type
);
602 return isl_map_reset_space(map
, isl_space_copy(map
->dim
));
605 __isl_give isl_set
*isl_set_reset_tuple_id(__isl_take isl_set
*set
)
607 return isl_map_reset_tuple_id(set
, isl_dim_set
);
610 isl_bool
isl_map_has_tuple_id(__isl_keep isl_map
*map
, enum isl_dim_type type
)
612 return map
? isl_space_has_tuple_id(map
->dim
, type
) : isl_bool_error
;
615 __isl_give isl_id
*isl_map_get_tuple_id(__isl_keep isl_map
*map
,
616 enum isl_dim_type type
)
618 return map
? isl_space_get_tuple_id(map
->dim
, type
) : NULL
;
621 isl_bool
isl_set_has_tuple_id(__isl_keep isl_set
*set
)
623 return isl_map_has_tuple_id(set
, isl_dim_set
);
626 __isl_give isl_id
*isl_set_get_tuple_id(__isl_keep isl_set
*set
)
628 return isl_map_get_tuple_id(set
, isl_dim_set
);
631 /* Does the set tuple have a name?
633 isl_bool
isl_set_has_tuple_name(__isl_keep isl_set
*set
)
636 return isl_bool_error
;
637 return isl_space_has_tuple_name(set
->dim
, isl_dim_set
);
641 const char *isl_basic_set_get_tuple_name(__isl_keep isl_basic_set
*bset
)
643 return bset
? isl_space_get_tuple_name(bset
->dim
, isl_dim_set
) : NULL
;
646 const char *isl_set_get_tuple_name(__isl_keep isl_set
*set
)
648 return set
? isl_space_get_tuple_name(set
->dim
, isl_dim_set
) : NULL
;
651 const char *isl_basic_map_get_dim_name(__isl_keep isl_basic_map
*bmap
,
652 enum isl_dim_type type
, unsigned pos
)
654 return bmap
? isl_space_get_dim_name(bmap
->dim
, type
, pos
) : NULL
;
657 const char *isl_basic_set_get_dim_name(__isl_keep isl_basic_set
*bset
,
658 enum isl_dim_type type
, unsigned pos
)
660 return bset
? isl_space_get_dim_name(bset
->dim
, type
, pos
) : NULL
;
663 /* Does the given dimension have a name?
665 isl_bool
isl_map_has_dim_name(__isl_keep isl_map
*map
,
666 enum isl_dim_type type
, unsigned pos
)
669 return isl_bool_error
;
670 return isl_space_has_dim_name(map
->dim
, type
, pos
);
673 const char *isl_map_get_dim_name(__isl_keep isl_map
*map
,
674 enum isl_dim_type type
, unsigned pos
)
676 return map
? isl_space_get_dim_name(map
->dim
, type
, pos
) : NULL
;
679 const char *isl_set_get_dim_name(__isl_keep isl_set
*set
,
680 enum isl_dim_type type
, unsigned pos
)
682 return set
? isl_space_get_dim_name(set
->dim
, type
, pos
) : NULL
;
685 /* Does the given dimension have a name?
687 isl_bool
isl_set_has_dim_name(__isl_keep isl_set
*set
,
688 enum isl_dim_type type
, unsigned pos
)
691 return isl_bool_error
;
692 return isl_space_has_dim_name(set
->dim
, type
, pos
);
695 __isl_give isl_basic_map
*isl_basic_map_set_dim_name(
696 __isl_take isl_basic_map
*bmap
,
697 enum isl_dim_type type
, unsigned pos
, const char *s
)
699 bmap
= isl_basic_map_cow(bmap
);
702 bmap
->dim
= isl_space_set_dim_name(bmap
->dim
, type
, pos
, s
);
705 return isl_basic_map_finalize(bmap
);
707 isl_basic_map_free(bmap
);
711 __isl_give isl_map
*isl_map_set_dim_name(__isl_take isl_map
*map
,
712 enum isl_dim_type type
, unsigned pos
, const char *s
)
716 map
= isl_map_cow(map
);
720 map
->dim
= isl_space_set_dim_name(map
->dim
, type
, pos
, s
);
724 for (i
= 0; i
< map
->n
; ++i
) {
725 map
->p
[i
] = isl_basic_map_set_dim_name(map
->p
[i
], type
, pos
, s
);
736 __isl_give isl_basic_set
*isl_basic_set_set_dim_name(
737 __isl_take isl_basic_set
*bset
,
738 enum isl_dim_type type
, unsigned pos
, const char *s
)
740 return bset_from_bmap(isl_basic_map_set_dim_name(bset_to_bmap(bset
),
744 __isl_give isl_set
*isl_set_set_dim_name(__isl_take isl_set
*set
,
745 enum isl_dim_type type
, unsigned pos
, const char *s
)
747 return set_from_map(isl_map_set_dim_name(set_to_map(set
),
751 isl_bool
isl_basic_map_has_dim_id(__isl_keep isl_basic_map
*bmap
,
752 enum isl_dim_type type
, unsigned pos
)
755 return isl_bool_error
;
756 return isl_space_has_dim_id(bmap
->dim
, type
, pos
);
759 __isl_give isl_id
*isl_basic_set_get_dim_id(__isl_keep isl_basic_set
*bset
,
760 enum isl_dim_type type
, unsigned pos
)
762 return bset
? isl_space_get_dim_id(bset
->dim
, type
, pos
) : NULL
;
765 isl_bool
isl_map_has_dim_id(__isl_keep isl_map
*map
,
766 enum isl_dim_type type
, unsigned pos
)
768 return map
? isl_space_has_dim_id(map
->dim
, type
, pos
) : isl_bool_error
;
771 __isl_give isl_id
*isl_map_get_dim_id(__isl_keep isl_map
*map
,
772 enum isl_dim_type type
, unsigned pos
)
774 return map
? isl_space_get_dim_id(map
->dim
, type
, pos
) : NULL
;
777 isl_bool
isl_set_has_dim_id(__isl_keep isl_set
*set
,
778 enum isl_dim_type type
, unsigned pos
)
780 return isl_map_has_dim_id(set
, type
, pos
);
783 __isl_give isl_id
*isl_set_get_dim_id(__isl_keep isl_set
*set
,
784 enum isl_dim_type type
, unsigned pos
)
786 return isl_map_get_dim_id(set
, type
, pos
);
789 __isl_give isl_map
*isl_map_set_dim_id(__isl_take isl_map
*map
,
790 enum isl_dim_type type
, unsigned pos
, __isl_take isl_id
*id
)
792 map
= isl_map_cow(map
);
796 map
->dim
= isl_space_set_dim_id(map
->dim
, type
, pos
, id
);
798 return isl_map_reset_space(map
, isl_space_copy(map
->dim
));
804 __isl_give isl_set
*isl_set_set_dim_id(__isl_take isl_set
*set
,
805 enum isl_dim_type type
, unsigned pos
, __isl_take isl_id
*id
)
807 return isl_map_set_dim_id(set
, type
, pos
, id
);
810 int isl_map_find_dim_by_id(__isl_keep isl_map
*map
, enum isl_dim_type type
,
811 __isl_keep isl_id
*id
)
815 return isl_space_find_dim_by_id(map
->dim
, type
, id
);
818 int isl_set_find_dim_by_id(__isl_keep isl_set
*set
, enum isl_dim_type type
,
819 __isl_keep isl_id
*id
)
821 return isl_map_find_dim_by_id(set
, type
, id
);
824 /* Return the position of the dimension of the given type and name
826 * Return -1 if no such dimension can be found.
828 int isl_basic_map_find_dim_by_name(__isl_keep isl_basic_map
*bmap
,
829 enum isl_dim_type type
, const char *name
)
833 return isl_space_find_dim_by_name(bmap
->dim
, type
, name
);
836 int isl_map_find_dim_by_name(__isl_keep isl_map
*map
, enum isl_dim_type type
,
841 return isl_space_find_dim_by_name(map
->dim
, type
, name
);
844 int isl_set_find_dim_by_name(__isl_keep isl_set
*set
, enum isl_dim_type type
,
847 return isl_map_find_dim_by_name(set
, type
, name
);
850 /* Reset the user pointer on all identifiers of parameters and tuples
851 * of the space of "map".
853 __isl_give isl_map
*isl_map_reset_user(__isl_take isl_map
*map
)
857 space
= isl_map_get_space(map
);
858 space
= isl_space_reset_user(space
);
859 map
= isl_map_reset_space(map
, space
);
864 /* Reset the user pointer on all identifiers of parameters and tuples
865 * of the space of "set".
867 __isl_give isl_set
*isl_set_reset_user(__isl_take isl_set
*set
)
869 return isl_map_reset_user(set
);
872 isl_bool
isl_basic_map_is_rational(__isl_keep isl_basic_map
*bmap
)
875 return isl_bool_error
;
876 return ISL_F_ISSET(bmap
, ISL_BASIC_MAP_RATIONAL
);
879 /* Has "map" been marked as a rational map?
880 * In particular, have all basic maps in "map" been marked this way?
881 * An empty map is not considered to be rational.
882 * Maps where only some of the basic maps are marked rational
885 isl_bool
isl_map_is_rational(__isl_keep isl_map
*map
)
891 return isl_bool_error
;
893 return isl_bool_false
;
894 rational
= isl_basic_map_is_rational(map
->p
[0]);
897 for (i
= 1; i
< map
->n
; ++i
) {
900 rational_i
= isl_basic_map_is_rational(map
->p
[i
]);
903 if (rational
!= rational_i
)
904 isl_die(isl_map_get_ctx(map
), isl_error_unsupported
,
905 "mixed rational and integer basic maps "
906 "not supported", return isl_bool_error
);
912 /* Has "set" been marked as a rational set?
913 * In particular, have all basic set in "set" been marked this way?
914 * An empty set is not considered to be rational.
915 * Sets where only some of the basic sets are marked rational
918 isl_bool
isl_set_is_rational(__isl_keep isl_set
*set
)
920 return isl_map_is_rational(set
);
923 int isl_basic_set_is_rational(__isl_keep isl_basic_set
*bset
)
925 return isl_basic_map_is_rational(bset
);
928 /* Does "bmap" contain any rational points?
930 * If "bmap" has an equality for each dimension, equating the dimension
931 * to an integer constant, then it has no rational points, even if it
932 * is marked as rational.
934 isl_bool
isl_basic_map_has_rational(__isl_keep isl_basic_map
*bmap
)
936 isl_bool has_rational
= isl_bool_true
;
940 return isl_bool_error
;
941 if (isl_basic_map_plain_is_empty(bmap
))
942 return isl_bool_false
;
943 if (!isl_basic_map_is_rational(bmap
))
944 return isl_bool_false
;
945 bmap
= isl_basic_map_copy(bmap
);
946 bmap
= isl_basic_map_implicit_equalities(bmap
);
948 return isl_bool_error
;
949 total
= isl_basic_map_total_dim(bmap
);
950 if (bmap
->n_eq
== total
) {
952 for (i
= 0; i
< bmap
->n_eq
; ++i
) {
953 j
= isl_seq_first_non_zero(bmap
->eq
[i
] + 1, total
);
956 if (!isl_int_is_one(bmap
->eq
[i
][1 + j
]) &&
957 !isl_int_is_negone(bmap
->eq
[i
][1 + j
]))
959 j
= isl_seq_first_non_zero(bmap
->eq
[i
] + 1 + j
+ 1,
965 has_rational
= isl_bool_false
;
967 isl_basic_map_free(bmap
);
972 /* Does "map" contain any rational points?
974 isl_bool
isl_map_has_rational(__isl_keep isl_map
*map
)
977 isl_bool has_rational
;
980 return isl_bool_error
;
981 for (i
= 0; i
< map
->n
; ++i
) {
982 has_rational
= isl_basic_map_has_rational(map
->p
[i
]);
983 if (has_rational
< 0 || has_rational
)
986 return isl_bool_false
;
989 /* Does "set" contain any rational points?
991 isl_bool
isl_set_has_rational(__isl_keep isl_set
*set
)
993 return isl_map_has_rational(set
);
996 /* Is this basic set a parameter domain?
998 isl_bool
isl_basic_set_is_params(__isl_keep isl_basic_set
*bset
)
1001 return isl_bool_error
;
1002 return isl_space_is_params(bset
->dim
);
1005 /* Is this set a parameter domain?
1007 isl_bool
isl_set_is_params(__isl_keep isl_set
*set
)
1010 return isl_bool_error
;
1011 return isl_space_is_params(set
->dim
);
1014 /* Is this map actually a parameter domain?
1015 * Users should never call this function. Outside of isl,
1016 * a map can never be a parameter domain.
1018 isl_bool
isl_map_is_params(__isl_keep isl_map
*map
)
1021 return isl_bool_error
;
1022 return isl_space_is_params(map
->dim
);
1025 static struct isl_basic_map
*basic_map_init(struct isl_ctx
*ctx
,
1026 struct isl_basic_map
*bmap
, unsigned extra
,
1027 unsigned n_eq
, unsigned n_ineq
)
1030 size_t row_size
= 1 + isl_space_dim(bmap
->dim
, isl_dim_all
) + extra
;
1035 bmap
->block
= isl_blk_alloc(ctx
, (n_ineq
+ n_eq
) * row_size
);
1036 if (isl_blk_is_error(bmap
->block
))
1039 bmap
->ineq
= isl_alloc_array(ctx
, isl_int
*, n_ineq
+ n_eq
);
1040 if ((n_ineq
+ n_eq
) && !bmap
->ineq
)
1044 bmap
->block2
= isl_blk_empty();
1047 bmap
->block2
= isl_blk_alloc(ctx
, extra
* (1 + row_size
));
1048 if (isl_blk_is_error(bmap
->block2
))
1051 bmap
->div
= isl_alloc_array(ctx
, isl_int
*, extra
);
1056 for (i
= 0; i
< n_ineq
+ n_eq
; ++i
)
1057 bmap
->ineq
[i
] = bmap
->block
.data
+ i
* row_size
;
1059 for (i
= 0; i
< extra
; ++i
)
1060 bmap
->div
[i
] = bmap
->block2
.data
+ i
* (1 + row_size
);
1064 bmap
->c_size
= n_eq
+ n_ineq
;
1065 bmap
->eq
= bmap
->ineq
+ n_ineq
;
1066 bmap
->extra
= extra
;
1070 bmap
->sample
= NULL
;
1074 isl_basic_map_free(bmap
);
1078 struct isl_basic_set
*isl_basic_set_alloc(struct isl_ctx
*ctx
,
1079 unsigned nparam
, unsigned dim
, unsigned extra
,
1080 unsigned n_eq
, unsigned n_ineq
)
1082 struct isl_basic_map
*bmap
;
1085 space
= isl_space_set_alloc(ctx
, nparam
, dim
);
1089 bmap
= isl_basic_map_alloc_space(space
, extra
, n_eq
, n_ineq
);
1090 return bset_from_bmap(bmap
);
1093 struct isl_basic_set
*isl_basic_set_alloc_space(__isl_take isl_space
*dim
,
1094 unsigned extra
, unsigned n_eq
, unsigned n_ineq
)
1096 struct isl_basic_map
*bmap
;
1099 isl_assert(dim
->ctx
, dim
->n_in
== 0, goto error
);
1100 bmap
= isl_basic_map_alloc_space(dim
, extra
, n_eq
, n_ineq
);
1101 return bset_from_bmap(bmap
);
1103 isl_space_free(dim
);
1107 struct isl_basic_map
*isl_basic_map_alloc_space(__isl_take isl_space
*dim
,
1108 unsigned extra
, unsigned n_eq
, unsigned n_ineq
)
1110 struct isl_basic_map
*bmap
;
1114 bmap
= isl_calloc_type(dim
->ctx
, struct isl_basic_map
);
1119 return basic_map_init(dim
->ctx
, bmap
, extra
, n_eq
, n_ineq
);
1121 isl_space_free(dim
);
1125 struct isl_basic_map
*isl_basic_map_alloc(struct isl_ctx
*ctx
,
1126 unsigned nparam
, unsigned in
, unsigned out
, unsigned extra
,
1127 unsigned n_eq
, unsigned n_ineq
)
1129 struct isl_basic_map
*bmap
;
1132 dim
= isl_space_alloc(ctx
, nparam
, in
, out
);
1136 bmap
= isl_basic_map_alloc_space(dim
, extra
, n_eq
, n_ineq
);
1140 static void dup_constraints(
1141 struct isl_basic_map
*dst
, struct isl_basic_map
*src
)
1144 unsigned total
= isl_basic_map_total_dim(src
);
1146 for (i
= 0; i
< src
->n_eq
; ++i
) {
1147 int j
= isl_basic_map_alloc_equality(dst
);
1148 isl_seq_cpy(dst
->eq
[j
], src
->eq
[i
], 1+total
);
1151 for (i
= 0; i
< src
->n_ineq
; ++i
) {
1152 int j
= isl_basic_map_alloc_inequality(dst
);
1153 isl_seq_cpy(dst
->ineq
[j
], src
->ineq
[i
], 1+total
);
1156 for (i
= 0; i
< src
->n_div
; ++i
) {
1157 int j
= isl_basic_map_alloc_div(dst
);
1158 isl_seq_cpy(dst
->div
[j
], src
->div
[i
], 1+1+total
);
1160 ISL_F_SET(dst
, ISL_BASIC_SET_FINAL
);
1163 struct isl_basic_map
*isl_basic_map_dup(struct isl_basic_map
*bmap
)
1165 struct isl_basic_map
*dup
;
1169 dup
= isl_basic_map_alloc_space(isl_space_copy(bmap
->dim
),
1170 bmap
->n_div
, bmap
->n_eq
, bmap
->n_ineq
);
1173 dup_constraints(dup
, bmap
);
1174 dup
->flags
= bmap
->flags
;
1175 dup
->sample
= isl_vec_copy(bmap
->sample
);
1179 struct isl_basic_set
*isl_basic_set_dup(struct isl_basic_set
*bset
)
1181 struct isl_basic_map
*dup
;
1183 dup
= isl_basic_map_dup(bset_to_bmap(bset
));
1184 return bset_from_bmap(dup
);
1187 struct isl_basic_set
*isl_basic_set_copy(struct isl_basic_set
*bset
)
1192 if (ISL_F_ISSET(bset
, ISL_BASIC_SET_FINAL
)) {
1196 return isl_basic_set_dup(bset
);
1199 struct isl_set
*isl_set_copy(struct isl_set
*set
)
1208 struct isl_basic_map
*isl_basic_map_copy(struct isl_basic_map
*bmap
)
1213 if (ISL_F_ISSET(bmap
, ISL_BASIC_SET_FINAL
)) {
1217 bmap
= isl_basic_map_dup(bmap
);
1219 ISL_F_SET(bmap
, ISL_BASIC_SET_FINAL
);
1223 struct isl_map
*isl_map_copy(struct isl_map
*map
)
1232 __isl_null isl_basic_map
*isl_basic_map_free(__isl_take isl_basic_map
*bmap
)
1237 if (--bmap
->ref
> 0)
1240 isl_ctx_deref(bmap
->ctx
);
1242 isl_blk_free(bmap
->ctx
, bmap
->block2
);
1244 isl_blk_free(bmap
->ctx
, bmap
->block
);
1245 isl_vec_free(bmap
->sample
);
1246 isl_space_free(bmap
->dim
);
1252 __isl_null isl_basic_set
*isl_basic_set_free(__isl_take isl_basic_set
*bset
)
1254 return isl_basic_map_free(bset_to_bmap(bset
));
1257 static int room_for_con(struct isl_basic_map
*bmap
, unsigned n
)
1259 return bmap
->n_eq
+ bmap
->n_ineq
+ n
<= bmap
->c_size
;
1262 /* Check that "map" has only named parameters, reporting an error
1265 isl_stat
isl_map_check_named_params(__isl_keep isl_map
*map
)
1267 return isl_space_check_named_params(isl_map_peek_space(map
));
1270 /* Check that "bmap1" and "bmap2" have the same parameters,
1271 * reporting an error if they do not.
1273 static isl_stat
isl_basic_map_check_equal_params(
1274 __isl_keep isl_basic_map
*bmap1
, __isl_keep isl_basic_map
*bmap2
)
1278 match
= isl_basic_map_has_equal_params(bmap1
, bmap2
);
1280 return isl_stat_error
;
1282 isl_die(isl_basic_map_get_ctx(bmap1
), isl_error_invalid
,
1283 "parameters don't match", return isl_stat_error
);
1287 __isl_give isl_map
*isl_map_align_params_map_map_and(
1288 __isl_take isl_map
*map1
, __isl_take isl_map
*map2
,
1289 __isl_give isl_map
*(*fn
)(__isl_take isl_map
*map1
,
1290 __isl_take isl_map
*map2
))
1294 if (isl_map_has_equal_params(map1
, map2
))
1295 return fn(map1
, map2
);
1296 if (isl_map_check_named_params(map1
) < 0)
1298 if (isl_map_check_named_params(map2
) < 0)
1300 map1
= isl_map_align_params(map1
, isl_map_get_space(map2
));
1301 map2
= isl_map_align_params(map2
, isl_map_get_space(map1
));
1302 return fn(map1
, map2
);
1309 isl_bool
isl_map_align_params_map_map_and_test(__isl_keep isl_map
*map1
,
1310 __isl_keep isl_map
*map2
,
1311 isl_bool (*fn
)(__isl_keep isl_map
*map1
, __isl_keep isl_map
*map2
))
1316 return isl_bool_error
;
1317 if (isl_map_has_equal_params(map1
, map2
))
1318 return fn(map1
, map2
);
1319 if (isl_map_check_named_params(map1
) < 0)
1320 return isl_bool_error
;
1321 if (isl_map_check_named_params(map2
) < 0)
1322 return isl_bool_error
;
1323 map1
= isl_map_copy(map1
);
1324 map2
= isl_map_copy(map2
);
1325 map1
= isl_map_align_params(map1
, isl_map_get_space(map2
));
1326 map2
= isl_map_align_params(map2
, isl_map_get_space(map1
));
1333 int isl_basic_map_alloc_equality(struct isl_basic_map
*bmap
)
1335 struct isl_ctx
*ctx
;
1339 isl_assert(ctx
, room_for_con(bmap
, 1), return -1);
1340 isl_assert(ctx
, (bmap
->eq
- bmap
->ineq
) + bmap
->n_eq
<= bmap
->c_size
,
1342 ISL_F_CLR(bmap
, ISL_BASIC_MAP_NORMALIZED
);
1343 ISL_F_CLR(bmap
, ISL_BASIC_MAP_NO_REDUNDANT
);
1344 ISL_F_CLR(bmap
, ISL_BASIC_MAP_NO_IMPLICIT
);
1345 ISL_F_CLR(bmap
, ISL_BASIC_MAP_ALL_EQUALITIES
);
1346 ISL_F_CLR(bmap
, ISL_BASIC_MAP_NORMALIZED_DIVS
);
1347 if ((bmap
->eq
- bmap
->ineq
) + bmap
->n_eq
== bmap
->c_size
) {
1349 int j
= isl_basic_map_alloc_inequality(bmap
);
1353 bmap
->ineq
[j
] = bmap
->ineq
[bmap
->n_ineq
- 1];
1354 bmap
->ineq
[bmap
->n_ineq
- 1] = bmap
->eq
[-1];
1361 isl_seq_clr(bmap
->eq
[bmap
->n_eq
] + 1 + isl_basic_map_total_dim(bmap
),
1362 bmap
->extra
- bmap
->n_div
);
1363 return bmap
->n_eq
++;
1366 int isl_basic_set_alloc_equality(struct isl_basic_set
*bset
)
1368 return isl_basic_map_alloc_equality(bset_to_bmap(bset
));
1371 int isl_basic_map_free_equality(struct isl_basic_map
*bmap
, unsigned n
)
1375 isl_assert(bmap
->ctx
, n
<= bmap
->n_eq
, return -1);
1380 int isl_basic_set_free_equality(struct isl_basic_set
*bset
, unsigned n
)
1382 return isl_basic_map_free_equality(bset_to_bmap(bset
), n
);
1385 int isl_basic_map_drop_equality(struct isl_basic_map
*bmap
, unsigned pos
)
1390 isl_assert(bmap
->ctx
, pos
< bmap
->n_eq
, return -1);
1392 if (pos
!= bmap
->n_eq
- 1) {
1394 bmap
->eq
[pos
] = bmap
->eq
[bmap
->n_eq
- 1];
1395 bmap
->eq
[bmap
->n_eq
- 1] = t
;
1401 /* Turn inequality "pos" of "bmap" into an equality.
1403 * In particular, we move the inequality in front of the equalities
1404 * and move the last inequality in the position of the moved inequality.
1405 * Note that isl_tab_make_equalities_explicit depends on this particular
1406 * change in the ordering of the constraints.
1408 void isl_basic_map_inequality_to_equality(
1409 struct isl_basic_map
*bmap
, unsigned pos
)
1413 t
= bmap
->ineq
[pos
];
1414 bmap
->ineq
[pos
] = bmap
->ineq
[bmap
->n_ineq
- 1];
1415 bmap
->ineq
[bmap
->n_ineq
- 1] = bmap
->eq
[-1];
1420 ISL_F_CLR(bmap
, ISL_BASIC_MAP_NO_REDUNDANT
);
1421 ISL_F_CLR(bmap
, ISL_BASIC_MAP_NORMALIZED
);
1422 ISL_F_CLR(bmap
, ISL_BASIC_MAP_NORMALIZED_DIVS
);
1423 ISL_F_CLR(bmap
, ISL_BASIC_MAP_ALL_EQUALITIES
);
1426 static int room_for_ineq(struct isl_basic_map
*bmap
, unsigned n
)
1428 return bmap
->n_ineq
+ n
<= bmap
->eq
- bmap
->ineq
;
1431 int isl_basic_map_alloc_inequality(struct isl_basic_map
*bmap
)
1433 struct isl_ctx
*ctx
;
1437 isl_assert(ctx
, room_for_ineq(bmap
, 1), return -1);
1438 ISL_F_CLR(bmap
, ISL_BASIC_MAP_NO_IMPLICIT
);
1439 ISL_F_CLR(bmap
, ISL_BASIC_MAP_NO_REDUNDANT
);
1440 ISL_F_CLR(bmap
, ISL_BASIC_MAP_NORMALIZED
);
1441 ISL_F_CLR(bmap
, ISL_BASIC_MAP_ALL_EQUALITIES
);
1442 isl_seq_clr(bmap
->ineq
[bmap
->n_ineq
] +
1443 1 + isl_basic_map_total_dim(bmap
),
1444 bmap
->extra
- bmap
->n_div
);
1445 return bmap
->n_ineq
++;
1448 int isl_basic_set_alloc_inequality(struct isl_basic_set
*bset
)
1450 return isl_basic_map_alloc_inequality(bset_to_bmap(bset
));
1453 int isl_basic_map_free_inequality(struct isl_basic_map
*bmap
, unsigned n
)
1457 isl_assert(bmap
->ctx
, n
<= bmap
->n_ineq
, return -1);
1462 int isl_basic_set_free_inequality(struct isl_basic_set
*bset
, unsigned n
)
1464 return isl_basic_map_free_inequality(bset_to_bmap(bset
), n
);
1467 int isl_basic_map_drop_inequality(struct isl_basic_map
*bmap
, unsigned pos
)
1472 isl_assert(bmap
->ctx
, pos
< bmap
->n_ineq
, return -1);
1474 if (pos
!= bmap
->n_ineq
- 1) {
1475 t
= bmap
->ineq
[pos
];
1476 bmap
->ineq
[pos
] = bmap
->ineq
[bmap
->n_ineq
- 1];
1477 bmap
->ineq
[bmap
->n_ineq
- 1] = t
;
1478 ISL_F_CLR(bmap
, ISL_BASIC_MAP_NORMALIZED
);
1484 int isl_basic_set_drop_inequality(struct isl_basic_set
*bset
, unsigned pos
)
1486 return isl_basic_map_drop_inequality(bset_to_bmap(bset
), pos
);
1489 __isl_give isl_basic_map
*isl_basic_map_add_eq(__isl_take isl_basic_map
*bmap
,
1494 bmap
= isl_basic_map_extend_constraints(bmap
, 1, 0);
1497 k
= isl_basic_map_alloc_equality(bmap
);
1500 isl_seq_cpy(bmap
->eq
[k
], eq
, 1 + isl_basic_map_total_dim(bmap
));
1503 isl_basic_map_free(bmap
);
1507 __isl_give isl_basic_set
*isl_basic_set_add_eq(__isl_take isl_basic_set
*bset
,
1510 return bset_from_bmap(isl_basic_map_add_eq(bset_to_bmap(bset
), eq
));
1513 __isl_give isl_basic_map
*isl_basic_map_add_ineq(__isl_take isl_basic_map
*bmap
,
1518 bmap
= isl_basic_map_extend_constraints(bmap
, 0, 1);
1521 k
= isl_basic_map_alloc_inequality(bmap
);
1524 isl_seq_cpy(bmap
->ineq
[k
], ineq
, 1 + isl_basic_map_total_dim(bmap
));
1527 isl_basic_map_free(bmap
);
1531 __isl_give isl_basic_set
*isl_basic_set_add_ineq(__isl_take isl_basic_set
*bset
,
1534 return bset_from_bmap(isl_basic_map_add_ineq(bset_to_bmap(bset
), ineq
));
1537 int isl_basic_map_alloc_div(struct isl_basic_map
*bmap
)
1541 isl_assert(bmap
->ctx
, bmap
->n_div
< bmap
->extra
, return -1);
1542 isl_seq_clr(bmap
->div
[bmap
->n_div
] +
1543 1 + 1 + isl_basic_map_total_dim(bmap
),
1544 bmap
->extra
- bmap
->n_div
);
1545 ISL_F_CLR(bmap
, ISL_BASIC_MAP_NORMALIZED_DIVS
);
1546 return bmap
->n_div
++;
1549 int isl_basic_set_alloc_div(struct isl_basic_set
*bset
)
1551 return isl_basic_map_alloc_div(bset_to_bmap(bset
));
1554 /* Check that there are "n" dimensions of type "type" starting at "first"
1557 static isl_stat
isl_basic_map_check_range(__isl_keep isl_basic_map
*bmap
,
1558 enum isl_dim_type type
, unsigned first
, unsigned n
)
1563 return isl_stat_error
;
1564 dim
= isl_basic_map_dim(bmap
, type
);
1565 if (first
+ n
> dim
|| first
+ n
< first
)
1566 isl_die(isl_basic_map_get_ctx(bmap
), isl_error_invalid
,
1567 "position or range out of bounds",
1568 return isl_stat_error
);
1572 /* Insert an extra integer division, prescribed by "div", to "bmap"
1573 * at (integer division) position "pos".
1575 * The integer division is first added at the end and then moved
1576 * into the right position.
1578 __isl_give isl_basic_map
*isl_basic_map_insert_div(
1579 __isl_take isl_basic_map
*bmap
, int pos
, __isl_keep isl_vec
*div
)
1583 bmap
= isl_basic_map_cow(bmap
);
1585 return isl_basic_map_free(bmap
);
1587 if (div
->size
!= 1 + 1 + isl_basic_map_dim(bmap
, isl_dim_all
))
1588 isl_die(isl_basic_map_get_ctx(bmap
), isl_error_invalid
,
1589 "unexpected size", return isl_basic_map_free(bmap
));
1590 if (isl_basic_map_check_range(bmap
, isl_dim_div
, pos
, 0) < 0)
1591 return isl_basic_map_free(bmap
);
1593 bmap
= isl_basic_map_extend_space(bmap
,
1594 isl_basic_map_get_space(bmap
), 1, 0, 2);
1595 k
= isl_basic_map_alloc_div(bmap
);
1597 return isl_basic_map_free(bmap
);
1598 isl_seq_cpy(bmap
->div
[k
], div
->el
, div
->size
);
1599 isl_int_set_si(bmap
->div
[k
][div
->size
], 0);
1601 for (i
= k
; i
> pos
; --i
)
1602 isl_basic_map_swap_div(bmap
, i
, i
- 1);
1607 isl_stat
isl_basic_map_free_div(struct isl_basic_map
*bmap
, unsigned n
)
1610 return isl_stat_error
;
1611 isl_assert(bmap
->ctx
, n
<= bmap
->n_div
, return isl_stat_error
);
1616 /* Copy constraint from src to dst, putting the vars of src at offset
1617 * dim_off in dst and the divs of src at offset div_off in dst.
1618 * If both sets are actually map, then dim_off applies to the input
1621 static void copy_constraint(struct isl_basic_map
*dst_map
, isl_int
*dst
,
1622 struct isl_basic_map
*src_map
, isl_int
*src
,
1623 unsigned in_off
, unsigned out_off
, unsigned div_off
)
1625 unsigned src_nparam
= isl_basic_map_dim(src_map
, isl_dim_param
);
1626 unsigned dst_nparam
= isl_basic_map_dim(dst_map
, isl_dim_param
);
1627 unsigned src_in
= isl_basic_map_dim(src_map
, isl_dim_in
);
1628 unsigned dst_in
= isl_basic_map_dim(dst_map
, isl_dim_in
);
1629 unsigned src_out
= isl_basic_map_dim(src_map
, isl_dim_out
);
1630 unsigned dst_out
= isl_basic_map_dim(dst_map
, isl_dim_out
);
1631 isl_int_set(dst
[0], src
[0]);
1632 isl_seq_cpy(dst
+1, src
+1, isl_min(dst_nparam
, src_nparam
));
1633 if (dst_nparam
> src_nparam
)
1634 isl_seq_clr(dst
+1+src_nparam
,
1635 dst_nparam
- src_nparam
);
1636 isl_seq_clr(dst
+1+dst_nparam
, in_off
);
1637 isl_seq_cpy(dst
+1+dst_nparam
+in_off
,
1639 isl_min(dst_in
-in_off
, src_in
));
1640 if (dst_in
-in_off
> src_in
)
1641 isl_seq_clr(dst
+1+dst_nparam
+in_off
+src_in
,
1642 dst_in
- in_off
- src_in
);
1643 isl_seq_clr(dst
+1+dst_nparam
+dst_in
, out_off
);
1644 isl_seq_cpy(dst
+1+dst_nparam
+dst_in
+out_off
,
1645 src
+1+src_nparam
+src_in
,
1646 isl_min(dst_out
-out_off
, src_out
));
1647 if (dst_out
-out_off
> src_out
)
1648 isl_seq_clr(dst
+1+dst_nparam
+dst_in
+out_off
+src_out
,
1649 dst_out
- out_off
- src_out
);
1650 isl_seq_clr(dst
+1+dst_nparam
+dst_in
+dst_out
, div_off
);
1651 isl_seq_cpy(dst
+1+dst_nparam
+dst_in
+dst_out
+div_off
,
1652 src
+1+src_nparam
+src_in
+src_out
,
1653 isl_min(dst_map
->extra
-div_off
, src_map
->n_div
));
1654 if (dst_map
->n_div
-div_off
> src_map
->n_div
)
1655 isl_seq_clr(dst
+1+dst_nparam
+dst_in
+dst_out
+
1656 div_off
+src_map
->n_div
,
1657 dst_map
->n_div
- div_off
- src_map
->n_div
);
1660 static void copy_div(struct isl_basic_map
*dst_map
, isl_int
*dst
,
1661 struct isl_basic_map
*src_map
, isl_int
*src
,
1662 unsigned in_off
, unsigned out_off
, unsigned div_off
)
1664 isl_int_set(dst
[0], src
[0]);
1665 copy_constraint(dst_map
, dst
+1, src_map
, src
+1, in_off
, out_off
, div_off
);
1668 static struct isl_basic_map
*add_constraints(struct isl_basic_map
*bmap1
,
1669 struct isl_basic_map
*bmap2
, unsigned i_pos
, unsigned o_pos
)
1674 if (!bmap1
|| !bmap2
)
1677 div_off
= bmap1
->n_div
;
1679 for (i
= 0; i
< bmap2
->n_eq
; ++i
) {
1680 int i1
= isl_basic_map_alloc_equality(bmap1
);
1683 copy_constraint(bmap1
, bmap1
->eq
[i1
], bmap2
, bmap2
->eq
[i
],
1684 i_pos
, o_pos
, div_off
);
1687 for (i
= 0; i
< bmap2
->n_ineq
; ++i
) {
1688 int i1
= isl_basic_map_alloc_inequality(bmap1
);
1691 copy_constraint(bmap1
, bmap1
->ineq
[i1
], bmap2
, bmap2
->ineq
[i
],
1692 i_pos
, o_pos
, div_off
);
1695 for (i
= 0; i
< bmap2
->n_div
; ++i
) {
1696 int i1
= isl_basic_map_alloc_div(bmap1
);
1699 copy_div(bmap1
, bmap1
->div
[i1
], bmap2
, bmap2
->div
[i
],
1700 i_pos
, o_pos
, div_off
);
1703 isl_basic_map_free(bmap2
);
1708 isl_basic_map_free(bmap1
);
1709 isl_basic_map_free(bmap2
);
1713 struct isl_basic_set
*isl_basic_set_add_constraints(struct isl_basic_set
*bset1
,
1714 struct isl_basic_set
*bset2
, unsigned pos
)
1716 return bset_from_bmap(add_constraints(bset_to_bmap(bset1
),
1717 bset_to_bmap(bset2
), 0, pos
));
1720 struct isl_basic_map
*isl_basic_map_extend_space(struct isl_basic_map
*base
,
1721 __isl_take isl_space
*dim
, unsigned extra
,
1722 unsigned n_eq
, unsigned n_ineq
)
1724 struct isl_basic_map
*ext
;
1734 dims_ok
= isl_space_is_equal(base
->dim
, dim
) &&
1735 base
->extra
>= base
->n_div
+ extra
;
1737 if (dims_ok
&& room_for_con(base
, n_eq
+ n_ineq
) &&
1738 room_for_ineq(base
, n_ineq
)) {
1739 isl_space_free(dim
);
1743 isl_assert(base
->ctx
, base
->dim
->nparam
<= dim
->nparam
, goto error
);
1744 isl_assert(base
->ctx
, base
->dim
->n_in
<= dim
->n_in
, goto error
);
1745 isl_assert(base
->ctx
, base
->dim
->n_out
<= dim
->n_out
, goto error
);
1746 extra
+= base
->extra
;
1748 n_ineq
+= base
->n_ineq
;
1750 ext
= isl_basic_map_alloc_space(dim
, extra
, n_eq
, n_ineq
);
1756 ext
->sample
= isl_vec_copy(base
->sample
);
1757 flags
= base
->flags
;
1758 ext
= add_constraints(ext
, base
, 0, 0);
1761 ISL_F_CLR(ext
, ISL_BASIC_SET_FINAL
);
1767 isl_space_free(dim
);
1768 isl_basic_map_free(base
);
1772 struct isl_basic_set
*isl_basic_set_extend_space(struct isl_basic_set
*base
,
1773 __isl_take isl_space
*dim
, unsigned extra
,
1774 unsigned n_eq
, unsigned n_ineq
)
1776 return bset_from_bmap(isl_basic_map_extend_space(bset_to_bmap(base
),
1777 dim
, extra
, n_eq
, n_ineq
));
1780 struct isl_basic_map
*isl_basic_map_extend_constraints(
1781 struct isl_basic_map
*base
, unsigned n_eq
, unsigned n_ineq
)
1785 return isl_basic_map_extend_space(base
, isl_space_copy(base
->dim
),
1789 struct isl_basic_map
*isl_basic_map_extend(struct isl_basic_map
*base
,
1790 unsigned nparam
, unsigned n_in
, unsigned n_out
, unsigned extra
,
1791 unsigned n_eq
, unsigned n_ineq
)
1793 struct isl_basic_map
*bmap
;
1798 dim
= isl_space_alloc(base
->ctx
, nparam
, n_in
, n_out
);
1802 bmap
= isl_basic_map_extend_space(base
, dim
, extra
, n_eq
, n_ineq
);
1805 isl_basic_map_free(base
);
1809 struct isl_basic_set
*isl_basic_set_extend(struct isl_basic_set
*base
,
1810 unsigned nparam
, unsigned dim
, unsigned extra
,
1811 unsigned n_eq
, unsigned n_ineq
)
1813 return bset_from_bmap(isl_basic_map_extend(bset_to_bmap(base
),
1814 nparam
, 0, dim
, extra
, n_eq
, n_ineq
));
1817 struct isl_basic_set
*isl_basic_set_extend_constraints(
1818 struct isl_basic_set
*base
, unsigned n_eq
, unsigned n_ineq
)
1820 isl_basic_map
*bmap
= bset_to_bmap(base
);
1821 bmap
= isl_basic_map_extend_constraints(bmap
, n_eq
, n_ineq
);
1822 return bset_from_bmap(bmap
);
1825 struct isl_basic_set
*isl_basic_set_cow(struct isl_basic_set
*bset
)
1827 return bset_from_bmap(isl_basic_map_cow(bset_to_bmap(bset
)));
1830 struct isl_basic_map
*isl_basic_map_cow(struct isl_basic_map
*bmap
)
1835 if (bmap
->ref
> 1) {
1837 bmap
= isl_basic_map_dup(bmap
);
1840 ISL_F_CLR(bmap
, ISL_BASIC_SET_FINAL
);
1841 ISL_F_CLR(bmap
, ISL_BASIC_MAP_REDUCED_COEFFICIENTS
);
1846 /* Clear all cached information in "map", either because it is about
1847 * to be modified or because it is being freed.
1848 * Always return the same pointer that is passed in.
1849 * This is needed for the use in isl_map_free.
1851 static __isl_give isl_map
*clear_caches(__isl_take isl_map
*map
)
1853 isl_basic_map_free(map
->cached_simple_hull
[0]);
1854 isl_basic_map_free(map
->cached_simple_hull
[1]);
1855 map
->cached_simple_hull
[0] = NULL
;
1856 map
->cached_simple_hull
[1] = NULL
;
1860 struct isl_set
*isl_set_cow(struct isl_set
*set
)
1862 return isl_map_cow(set
);
1865 /* Return an isl_map that is equal to "map" and that has only
1866 * a single reference.
1868 * If the original input already has only one reference, then
1869 * simply return it, but clear all cached information, since
1870 * it may be rendered invalid by the operations that will be
1871 * performed on the result.
1873 * Otherwise, create a duplicate (without any cached information).
1875 struct isl_map
*isl_map_cow(struct isl_map
*map
)
1881 return clear_caches(map
);
1883 return isl_map_dup(map
);
1886 static void swap_vars(struct isl_blk blk
, isl_int
*a
,
1887 unsigned a_len
, unsigned b_len
)
1889 isl_seq_cpy(blk
.data
, a
+a_len
, b_len
);
1890 isl_seq_cpy(blk
.data
+b_len
, a
, a_len
);
1891 isl_seq_cpy(a
, blk
.data
, b_len
+a_len
);
1894 static __isl_give isl_basic_map
*isl_basic_map_swap_vars(
1895 __isl_take isl_basic_map
*bmap
, unsigned pos
, unsigned n1
, unsigned n2
)
1903 isl_assert(bmap
->ctx
,
1904 pos
+ n1
+ n2
<= 1 + isl_basic_map_total_dim(bmap
), goto error
);
1906 if (n1
== 0 || n2
== 0)
1909 bmap
= isl_basic_map_cow(bmap
);
1913 blk
= isl_blk_alloc(bmap
->ctx
, n1
+ n2
);
1914 if (isl_blk_is_error(blk
))
1917 for (i
= 0; i
< bmap
->n_eq
; ++i
)
1919 bmap
->eq
[i
] + pos
, n1
, n2
);
1921 for (i
= 0; i
< bmap
->n_ineq
; ++i
)
1923 bmap
->ineq
[i
] + pos
, n1
, n2
);
1925 for (i
= 0; i
< bmap
->n_div
; ++i
)
1927 bmap
->div
[i
]+1 + pos
, n1
, n2
);
1929 isl_blk_free(bmap
->ctx
, blk
);
1931 ISL_F_CLR(bmap
, ISL_BASIC_SET_NORMALIZED
);
1932 bmap
= isl_basic_map_gauss(bmap
, NULL
);
1933 return isl_basic_map_finalize(bmap
);
1935 isl_basic_map_free(bmap
);
1939 struct isl_basic_map
*isl_basic_map_set_to_empty(struct isl_basic_map
*bmap
)
1945 total
= isl_basic_map_total_dim(bmap
);
1946 if (isl_basic_map_free_div(bmap
, bmap
->n_div
) < 0)
1947 return isl_basic_map_free(bmap
);
1948 isl_basic_map_free_inequality(bmap
, bmap
->n_ineq
);
1950 isl_basic_map_free_equality(bmap
, bmap
->n_eq
-1);
1952 i
= isl_basic_map_alloc_equality(bmap
);
1956 isl_int_set_si(bmap
->eq
[i
][0], 1);
1957 isl_seq_clr(bmap
->eq
[i
]+1, total
);
1958 ISL_F_SET(bmap
, ISL_BASIC_MAP_EMPTY
);
1959 isl_vec_free(bmap
->sample
);
1960 bmap
->sample
= NULL
;
1961 return isl_basic_map_finalize(bmap
);
1963 isl_basic_map_free(bmap
);
1967 struct isl_basic_set
*isl_basic_set_set_to_empty(struct isl_basic_set
*bset
)
1969 return bset_from_bmap(isl_basic_map_set_to_empty(bset_to_bmap(bset
)));
1972 /* Swap divs "a" and "b" in "bmap" (without modifying any of the constraints
1975 static void swap_div(__isl_keep isl_basic_map
*bmap
, int a
, int b
)
1977 isl_int
*t
= bmap
->div
[a
];
1978 bmap
->div
[a
] = bmap
->div
[b
];
1982 /* Swap divs "a" and "b" in "bmap" and adjust the constraints and
1983 * div definitions accordingly.
1985 void isl_basic_map_swap_div(struct isl_basic_map
*bmap
, int a
, int b
)
1988 unsigned off
= isl_space_dim(bmap
->dim
, isl_dim_all
);
1990 swap_div(bmap
, a
, b
);
1992 for (i
= 0; i
< bmap
->n_eq
; ++i
)
1993 isl_int_swap(bmap
->eq
[i
][1+off
+a
], bmap
->eq
[i
][1+off
+b
]);
1995 for (i
= 0; i
< bmap
->n_ineq
; ++i
)
1996 isl_int_swap(bmap
->ineq
[i
][1+off
+a
], bmap
->ineq
[i
][1+off
+b
]);
1998 for (i
= 0; i
< bmap
->n_div
; ++i
)
1999 isl_int_swap(bmap
->div
[i
][1+1+off
+a
], bmap
->div
[i
][1+1+off
+b
]);
2000 ISL_F_CLR(bmap
, ISL_BASIC_MAP_NORMALIZED
);
2003 /* Swap divs "a" and "b" in "bset" and adjust the constraints and
2004 * div definitions accordingly.
2006 void isl_basic_set_swap_div(__isl_keep isl_basic_set
*bset
, int a
, int b
)
2008 isl_basic_map_swap_div(bset
, a
, b
);
2011 /* Eliminate the specified n dimensions starting at first from the
2012 * constraints, without removing the dimensions from the space.
2013 * If the set is rational, the dimensions are eliminated using Fourier-Motzkin.
2015 __isl_give isl_map
*isl_map_eliminate(__isl_take isl_map
*map
,
2016 enum isl_dim_type type
, unsigned first
, unsigned n
)
2025 if (first
+ n
> isl_map_dim(map
, type
) || first
+ n
< first
)
2026 isl_die(map
->ctx
, isl_error_invalid
,
2027 "index out of bounds", goto error
);
2029 map
= isl_map_cow(map
);
2033 for (i
= 0; i
< map
->n
; ++i
) {
2034 map
->p
[i
] = isl_basic_map_eliminate(map
->p
[i
], type
, first
, n
);
2044 /* Eliminate the specified n dimensions starting at first from the
2045 * constraints, without removing the dimensions from the space.
2046 * If the set is rational, the dimensions are eliminated using Fourier-Motzkin.
2048 __isl_give isl_set
*isl_set_eliminate(__isl_take isl_set
*set
,
2049 enum isl_dim_type type
, unsigned first
, unsigned n
)
2051 return set_from_map(isl_map_eliminate(set_to_map(set
), type
, first
, n
));
2054 /* Eliminate the specified n dimensions starting at first from the
2055 * constraints, without removing the dimensions from the space.
2056 * If the set is rational, the dimensions are eliminated using Fourier-Motzkin.
2058 __isl_give isl_set
*isl_set_eliminate_dims(__isl_take isl_set
*set
,
2059 unsigned first
, unsigned n
)
2061 return isl_set_eliminate(set
, isl_dim_set
, first
, n
);
2064 __isl_give isl_basic_map
*isl_basic_map_remove_divs(
2065 __isl_take isl_basic_map
*bmap
)
2069 bmap
= isl_basic_map_eliminate_vars(bmap
,
2070 isl_space_dim(bmap
->dim
, isl_dim_all
), bmap
->n_div
);
2074 return isl_basic_map_finalize(bmap
);
2077 __isl_give isl_basic_set
*isl_basic_set_remove_divs(
2078 __isl_take isl_basic_set
*bset
)
2080 return bset_from_bmap(isl_basic_map_remove_divs(bset_to_bmap(bset
)));
2083 __isl_give isl_map
*isl_map_remove_divs(__isl_take isl_map
*map
)
2092 map
= isl_map_cow(map
);
2096 for (i
= 0; i
< map
->n
; ++i
) {
2097 map
->p
[i
] = isl_basic_map_remove_divs(map
->p
[i
]);
2107 __isl_give isl_set
*isl_set_remove_divs(__isl_take isl_set
*set
)
2109 return isl_map_remove_divs(set
);
2112 struct isl_basic_map
*isl_basic_map_remove_dims(struct isl_basic_map
*bmap
,
2113 enum isl_dim_type type
, unsigned first
, unsigned n
)
2115 if (isl_basic_map_check_range(bmap
, type
, first
, n
) < 0)
2116 return isl_basic_map_free(bmap
);
2117 if (n
== 0 && !isl_space_is_named_or_nested(bmap
->dim
, type
))
2119 bmap
= isl_basic_map_eliminate_vars(bmap
,
2120 isl_basic_map_offset(bmap
, type
) - 1 + first
, n
);
2123 if (ISL_F_ISSET(bmap
, ISL_BASIC_MAP_EMPTY
) && type
== isl_dim_div
)
2125 bmap
= isl_basic_map_drop(bmap
, type
, first
, n
);
2129 /* Return true if the definition of the given div (recursively) involves
2130 * any of the given variables.
2132 static isl_bool
div_involves_vars(__isl_keep isl_basic_map
*bmap
, int div
,
2133 unsigned first
, unsigned n
)
2136 unsigned div_offset
= isl_basic_map_offset(bmap
, isl_dim_div
);
2138 if (isl_int_is_zero(bmap
->div
[div
][0]))
2139 return isl_bool_false
;
2140 if (isl_seq_first_non_zero(bmap
->div
[div
] + 1 + first
, n
) >= 0)
2141 return isl_bool_true
;
2143 for (i
= bmap
->n_div
- 1; i
>= 0; --i
) {
2146 if (isl_int_is_zero(bmap
->div
[div
][1 + div_offset
+ i
]))
2148 involves
= div_involves_vars(bmap
, i
, first
, n
);
2149 if (involves
< 0 || involves
)
2153 return isl_bool_false
;
2156 /* Try and add a lower and/or upper bound on "div" to "bmap"
2157 * based on inequality "i".
2158 * "total" is the total number of variables (excluding the divs).
2159 * "v" is a temporary object that can be used during the calculations.
2160 * If "lb" is set, then a lower bound should be constructed.
2161 * If "ub" is set, then an upper bound should be constructed.
2163 * The calling function has already checked that the inequality does not
2164 * reference "div", but we still need to check that the inequality is
2165 * of the right form. We'll consider the case where we want to construct
2166 * a lower bound. The construction of upper bounds is similar.
2168 * Let "div" be of the form
2170 * q = floor((a + f(x))/d)
2172 * We essentially check if constraint "i" is of the form
2176 * so that we can use it to derive a lower bound on "div".
2177 * However, we allow a slightly more general form
2181 * with the condition that the coefficients of g(x) - f(x) are all
2183 * Rewriting this constraint as
2187 * adding a + f(x) to both sides and dividing by d, we obtain
2189 * (a + f(x))/d >= (a-b)/d + (f(x)-g(x))/d
2191 * Taking the floor on both sides, we obtain
2193 * q >= floor((a-b)/d) + (f(x)-g(x))/d
2197 * (g(x)-f(x))/d + ceil((b-a)/d) + q >= 0
2199 * In the case of an upper bound, we construct the constraint
2201 * (g(x)+f(x))/d + floor((b+a)/d) - q >= 0
2204 static __isl_give isl_basic_map
*insert_bounds_on_div_from_ineq(
2205 __isl_take isl_basic_map
*bmap
, int div
, int i
,
2206 unsigned total
, isl_int v
, int lb
, int ub
)
2210 for (j
= 0; (lb
|| ub
) && j
< total
+ bmap
->n_div
; ++j
) {
2212 isl_int_sub(v
, bmap
->ineq
[i
][1 + j
],
2213 bmap
->div
[div
][1 + 1 + j
]);
2214 lb
= isl_int_is_divisible_by(v
, bmap
->div
[div
][0]);
2217 isl_int_add(v
, bmap
->ineq
[i
][1 + j
],
2218 bmap
->div
[div
][1 + 1 + j
]);
2219 ub
= isl_int_is_divisible_by(v
, bmap
->div
[div
][0]);
2225 bmap
= isl_basic_map_cow(bmap
);
2226 bmap
= isl_basic_map_extend_constraints(bmap
, 0, lb
+ ub
);
2228 int k
= isl_basic_map_alloc_inequality(bmap
);
2231 for (j
= 0; j
< 1 + total
+ bmap
->n_div
; ++j
) {
2232 isl_int_sub(bmap
->ineq
[k
][j
], bmap
->ineq
[i
][j
],
2233 bmap
->div
[div
][1 + j
]);
2234 isl_int_cdiv_q(bmap
->ineq
[k
][j
],
2235 bmap
->ineq
[k
][j
], bmap
->div
[div
][0]);
2237 isl_int_set_si(bmap
->ineq
[k
][1 + total
+ div
], 1);
2240 int k
= isl_basic_map_alloc_inequality(bmap
);
2243 for (j
= 0; j
< 1 + total
+ bmap
->n_div
; ++j
) {
2244 isl_int_add(bmap
->ineq
[k
][j
], bmap
->ineq
[i
][j
],
2245 bmap
->div
[div
][1 + j
]);
2246 isl_int_fdiv_q(bmap
->ineq
[k
][j
],
2247 bmap
->ineq
[k
][j
], bmap
->div
[div
][0]);
2249 isl_int_set_si(bmap
->ineq
[k
][1 + total
+ div
], -1);
2252 ISL_F_CLR(bmap
, ISL_BASIC_MAP_NORMALIZED
);
2255 isl_basic_map_free(bmap
);
2259 /* This function is called right before "div" is eliminated from "bmap"
2260 * using Fourier-Motzkin.
2261 * Look through the constraints of "bmap" for constraints on the argument
2262 * of the integer division and use them to construct constraints on the
2263 * integer division itself. These constraints can then be combined
2264 * during the Fourier-Motzkin elimination.
2265 * Note that it is only useful to introduce lower bounds on "div"
2266 * if "bmap" already contains upper bounds on "div" as the newly
2267 * introduce lower bounds can then be combined with the pre-existing
2268 * upper bounds. Similarly for upper bounds.
2269 * We therefore first check if "bmap" contains any lower and/or upper bounds
2272 * It is interesting to note that the introduction of these constraints
2273 * can indeed lead to more accurate results, even when compared to
2274 * deriving constraints on the argument of "div" from constraints on "div".
2275 * Consider, for example, the set
2277 * { [i,j,k] : 3 + i + 2j >= 0 and 2 * [(i+2j)/4] <= k }
2279 * The second constraint can be rewritten as
2281 * 2 * [(-i-2j+3)/4] + k >= 0
2283 * from which we can derive
2285 * -i - 2j + 3 >= -2k
2291 * Combined with the first constraint, we obtain
2293 * -3 <= 3 + 2k or k >= -3
2295 * If, on the other hand we derive a constraint on [(i+2j)/4] from
2296 * the first constraint, we obtain
2298 * [(i + 2j)/4] >= [-3/4] = -1
2300 * Combining this constraint with the second constraint, we obtain
2304 static __isl_give isl_basic_map
*insert_bounds_on_div(
2305 __isl_take isl_basic_map
*bmap
, int div
)
2308 int check_lb
, check_ub
;
2315 if (isl_int_is_zero(bmap
->div
[div
][0]))
2318 total
= isl_space_dim(bmap
->dim
, isl_dim_all
);
2322 for (i
= 0; (!check_lb
|| !check_ub
) && i
< bmap
->n_ineq
; ++i
) {
2323 int s
= isl_int_sgn(bmap
->ineq
[i
][1 + total
+ div
]);
2330 if (!check_lb
&& !check_ub
)
2335 for (i
= 0; bmap
&& i
< bmap
->n_ineq
; ++i
) {
2336 if (!isl_int_is_zero(bmap
->ineq
[i
][1 + total
+ div
]))
2339 bmap
= insert_bounds_on_div_from_ineq(bmap
, div
, i
, total
, v
,
2340 check_lb
, check_ub
);
2348 /* Remove all divs (recursively) involving any of the given dimensions
2349 * in their definitions.
2351 __isl_give isl_basic_map
*isl_basic_map_remove_divs_involving_dims(
2352 __isl_take isl_basic_map
*bmap
,
2353 enum isl_dim_type type
, unsigned first
, unsigned n
)
2357 if (isl_basic_map_check_range(bmap
, type
, first
, n
) < 0)
2358 return isl_basic_map_free(bmap
);
2359 first
+= isl_basic_map_offset(bmap
, type
);
2361 for (i
= bmap
->n_div
- 1; i
>= 0; --i
) {
2364 involves
= div_involves_vars(bmap
, i
, first
, n
);
2366 return isl_basic_map_free(bmap
);
2369 bmap
= insert_bounds_on_div(bmap
, i
);
2370 bmap
= isl_basic_map_remove_dims(bmap
, isl_dim_div
, i
, 1);
2379 __isl_give isl_basic_set
*isl_basic_set_remove_divs_involving_dims(
2380 __isl_take isl_basic_set
*bset
,
2381 enum isl_dim_type type
, unsigned first
, unsigned n
)
2383 return isl_basic_map_remove_divs_involving_dims(bset
, type
, first
, n
);
2386 __isl_give isl_map
*isl_map_remove_divs_involving_dims(__isl_take isl_map
*map
,
2387 enum isl_dim_type type
, unsigned first
, unsigned n
)
2396 map
= isl_map_cow(map
);
2400 for (i
= 0; i
< map
->n
; ++i
) {
2401 map
->p
[i
] = isl_basic_map_remove_divs_involving_dims(map
->p
[i
],
2412 __isl_give isl_set
*isl_set_remove_divs_involving_dims(__isl_take isl_set
*set
,
2413 enum isl_dim_type type
, unsigned first
, unsigned n
)
2415 return set_from_map(isl_map_remove_divs_involving_dims(set_to_map(set
),
2419 /* Does the description of "bmap" depend on the specified dimensions?
2420 * We also check whether the dimensions appear in any of the div definitions.
2421 * In principle there is no need for this check. If the dimensions appear
2422 * in a div definition, they also appear in the defining constraints of that
2425 isl_bool
isl_basic_map_involves_dims(__isl_keep isl_basic_map
*bmap
,
2426 enum isl_dim_type type
, unsigned first
, unsigned n
)
2430 if (isl_basic_map_check_range(bmap
, type
, first
, n
) < 0)
2431 return isl_bool_error
;
2433 first
+= isl_basic_map_offset(bmap
, type
);
2434 for (i
= 0; i
< bmap
->n_eq
; ++i
)
2435 if (isl_seq_first_non_zero(bmap
->eq
[i
] + first
, n
) >= 0)
2436 return isl_bool_true
;
2437 for (i
= 0; i
< bmap
->n_ineq
; ++i
)
2438 if (isl_seq_first_non_zero(bmap
->ineq
[i
] + first
, n
) >= 0)
2439 return isl_bool_true
;
2440 for (i
= 0; i
< bmap
->n_div
; ++i
) {
2441 if (isl_int_is_zero(bmap
->div
[i
][0]))
2443 if (isl_seq_first_non_zero(bmap
->div
[i
] + 1 + first
, n
) >= 0)
2444 return isl_bool_true
;
2447 return isl_bool_false
;
2450 isl_bool
isl_map_involves_dims(__isl_keep isl_map
*map
,
2451 enum isl_dim_type type
, unsigned first
, unsigned n
)
2456 return isl_bool_error
;
2458 if (first
+ n
> isl_map_dim(map
, type
))
2459 isl_die(map
->ctx
, isl_error_invalid
,
2460 "index out of bounds", return isl_bool_error
);
2462 for (i
= 0; i
< map
->n
; ++i
) {
2463 isl_bool involves
= isl_basic_map_involves_dims(map
->p
[i
],
2465 if (involves
< 0 || involves
)
2469 return isl_bool_false
;
2472 isl_bool
isl_basic_set_involves_dims(__isl_keep isl_basic_set
*bset
,
2473 enum isl_dim_type type
, unsigned first
, unsigned n
)
2475 return isl_basic_map_involves_dims(bset
, type
, first
, n
);
2478 isl_bool
isl_set_involves_dims(__isl_keep isl_set
*set
,
2479 enum isl_dim_type type
, unsigned first
, unsigned n
)
2481 return isl_map_involves_dims(set
, type
, first
, n
);
2484 /* Drop all constraints in bmap that involve any of the dimensions
2485 * first to first+n-1.
2487 static __isl_give isl_basic_map
*isl_basic_map_drop_constraints_involving(
2488 __isl_take isl_basic_map
*bmap
, unsigned first
, unsigned n
)
2495 bmap
= isl_basic_map_cow(bmap
);
2500 for (i
= bmap
->n_eq
- 1; i
>= 0; --i
) {
2501 if (isl_seq_first_non_zero(bmap
->eq
[i
] + 1 + first
, n
) == -1)
2503 isl_basic_map_drop_equality(bmap
, i
);
2506 for (i
= bmap
->n_ineq
- 1; i
>= 0; --i
) {
2507 if (isl_seq_first_non_zero(bmap
->ineq
[i
] + 1 + first
, n
) == -1)
2509 isl_basic_map_drop_inequality(bmap
, i
);
2512 bmap
= isl_basic_map_add_known_div_constraints(bmap
);
2516 /* Drop all constraints in bset that involve any of the dimensions
2517 * first to first+n-1.
2519 __isl_give isl_basic_set
*isl_basic_set_drop_constraints_involving(
2520 __isl_take isl_basic_set
*bset
, unsigned first
, unsigned n
)
2522 return isl_basic_map_drop_constraints_involving(bset
, first
, n
);
2525 /* Drop all constraints in bmap that do not involve any of the dimensions
2526 * first to first + n - 1 of the given type.
2528 __isl_give isl_basic_map
*isl_basic_map_drop_constraints_not_involving_dims(
2529 __isl_take isl_basic_map
*bmap
,
2530 enum isl_dim_type type
, unsigned first
, unsigned n
)
2535 isl_space
*space
= isl_basic_map_get_space(bmap
);
2536 isl_basic_map_free(bmap
);
2537 return isl_basic_map_universe(space
);
2539 bmap
= isl_basic_map_cow(bmap
);
2543 if (isl_basic_map_check_range(bmap
, type
, first
, n
) < 0)
2544 return isl_basic_map_free(bmap
);
2546 first
+= isl_basic_map_offset(bmap
, type
) - 1;
2548 for (i
= bmap
->n_eq
- 1; i
>= 0; --i
) {
2549 if (isl_seq_first_non_zero(bmap
->eq
[i
] + 1 + first
, n
) != -1)
2551 isl_basic_map_drop_equality(bmap
, i
);
2554 for (i
= bmap
->n_ineq
- 1; i
>= 0; --i
) {
2555 if (isl_seq_first_non_zero(bmap
->ineq
[i
] + 1 + first
, n
) != -1)
2557 isl_basic_map_drop_inequality(bmap
, i
);
2560 bmap
= isl_basic_map_add_known_div_constraints(bmap
);
2564 /* Drop all constraints in bset that do not involve any of the dimensions
2565 * first to first + n - 1 of the given type.
2567 __isl_give isl_basic_set
*isl_basic_set_drop_constraints_not_involving_dims(
2568 __isl_take isl_basic_set
*bset
,
2569 enum isl_dim_type type
, unsigned first
, unsigned n
)
2571 return isl_basic_map_drop_constraints_not_involving_dims(bset
,
2575 /* Drop all constraints in bmap that involve any of the dimensions
2576 * first to first + n - 1 of the given type.
2578 __isl_give isl_basic_map
*isl_basic_map_drop_constraints_involving_dims(
2579 __isl_take isl_basic_map
*bmap
,
2580 enum isl_dim_type type
, unsigned first
, unsigned n
)
2587 if (isl_basic_map_check_range(bmap
, type
, first
, n
) < 0)
2588 return isl_basic_map_free(bmap
);
2590 bmap
= isl_basic_map_remove_divs_involving_dims(bmap
, type
, first
, n
);
2591 first
+= isl_basic_map_offset(bmap
, type
) - 1;
2592 return isl_basic_map_drop_constraints_involving(bmap
, first
, n
);
2595 /* Drop all constraints in bset that involve any of the dimensions
2596 * first to first + n - 1 of the given type.
2598 __isl_give isl_basic_set
*isl_basic_set_drop_constraints_involving_dims(
2599 __isl_take isl_basic_set
*bset
,
2600 enum isl_dim_type type
, unsigned first
, unsigned n
)
2602 return isl_basic_map_drop_constraints_involving_dims(bset
,
2606 /* Drop constraints from "map" by applying "drop" to each basic map.
2608 static __isl_give isl_map
*drop_constraints(__isl_take isl_map
*map
,
2609 enum isl_dim_type type
, unsigned first
, unsigned n
,
2610 __isl_give isl_basic_map
*(*drop
)(__isl_take isl_basic_map
*bmap
,
2611 enum isl_dim_type type
, unsigned first
, unsigned n
))
2619 dim
= isl_map_dim(map
, type
);
2620 if (first
+ n
> dim
|| first
+ n
< first
)
2621 isl_die(isl_map_get_ctx(map
), isl_error_invalid
,
2622 "index out of bounds", return isl_map_free(map
));
2624 map
= isl_map_cow(map
);
2628 for (i
= 0; i
< map
->n
; ++i
) {
2629 map
->p
[i
] = drop(map
->p
[i
], type
, first
, n
);
2631 return isl_map_free(map
);
2635 ISL_F_CLR(map
, ISL_MAP_DISJOINT
);
2640 /* Drop all constraints in map that involve any of the dimensions
2641 * first to first + n - 1 of the given type.
2643 __isl_give isl_map
*isl_map_drop_constraints_involving_dims(
2644 __isl_take isl_map
*map
,
2645 enum isl_dim_type type
, unsigned first
, unsigned n
)
2649 return drop_constraints(map
, type
, first
, n
,
2650 &isl_basic_map_drop_constraints_involving_dims
);
2653 /* Drop all constraints in "map" that do not involve any of the dimensions
2654 * first to first + n - 1 of the given type.
2656 __isl_give isl_map
*isl_map_drop_constraints_not_involving_dims(
2657 __isl_take isl_map
*map
,
2658 enum isl_dim_type type
, unsigned first
, unsigned n
)
2661 isl_space
*space
= isl_map_get_space(map
);
2663 return isl_map_universe(space
);
2665 return drop_constraints(map
, type
, first
, n
,
2666 &isl_basic_map_drop_constraints_not_involving_dims
);
2669 /* Drop all constraints in set that involve any of the dimensions
2670 * first to first + n - 1 of the given type.
2672 __isl_give isl_set
*isl_set_drop_constraints_involving_dims(
2673 __isl_take isl_set
*set
,
2674 enum isl_dim_type type
, unsigned first
, unsigned n
)
2676 return isl_map_drop_constraints_involving_dims(set
, type
, first
, n
);
2679 /* Drop all constraints in "set" that do not involve any of the dimensions
2680 * first to first + n - 1 of the given type.
2682 __isl_give isl_set
*isl_set_drop_constraints_not_involving_dims(
2683 __isl_take isl_set
*set
,
2684 enum isl_dim_type type
, unsigned first
, unsigned n
)
2686 return isl_map_drop_constraints_not_involving_dims(set
, type
, first
, n
);
2689 /* Does local variable "div" of "bmap" have a complete explicit representation?
2690 * Having a complete explicit representation requires not only
2691 * an explicit representation, but also that all local variables
2692 * that appear in this explicit representation in turn have
2693 * a complete explicit representation.
2695 isl_bool
isl_basic_map_div_is_known(__isl_keep isl_basic_map
*bmap
, int div
)
2698 unsigned div_offset
= isl_basic_map_offset(bmap
, isl_dim_div
);
2701 marked
= isl_basic_map_div_is_marked_unknown(bmap
, div
);
2702 if (marked
< 0 || marked
)
2703 return isl_bool_not(marked
);
2705 for (i
= bmap
->n_div
- 1; i
>= 0; --i
) {
2708 if (isl_int_is_zero(bmap
->div
[div
][1 + div_offset
+ i
]))
2710 known
= isl_basic_map_div_is_known(bmap
, i
);
2711 if (known
< 0 || !known
)
2715 return isl_bool_true
;
2718 /* Remove all divs that are unknown or defined in terms of unknown divs.
2720 __isl_give isl_basic_map
*isl_basic_map_remove_unknown_divs(
2721 __isl_take isl_basic_map
*bmap
)
2728 for (i
= bmap
->n_div
- 1; i
>= 0; --i
) {
2729 if (isl_basic_map_div_is_known(bmap
, i
))
2731 bmap
= isl_basic_map_remove_dims(bmap
, isl_dim_div
, i
, 1);
2740 /* Remove all divs that are unknown or defined in terms of unknown divs.
2742 __isl_give isl_basic_set
*isl_basic_set_remove_unknown_divs(
2743 __isl_take isl_basic_set
*bset
)
2745 return isl_basic_map_remove_unknown_divs(bset
);
2748 __isl_give isl_map
*isl_map_remove_unknown_divs(__isl_take isl_map
*map
)
2757 map
= isl_map_cow(map
);
2761 for (i
= 0; i
< map
->n
; ++i
) {
2762 map
->p
[i
] = isl_basic_map_remove_unknown_divs(map
->p
[i
]);
2772 __isl_give isl_set
*isl_set_remove_unknown_divs(__isl_take isl_set
*set
)
2774 return set_from_map(isl_map_remove_unknown_divs(set_to_map(set
)));
2777 __isl_give isl_basic_set
*isl_basic_set_remove_dims(
2778 __isl_take isl_basic_set
*bset
,
2779 enum isl_dim_type type
, unsigned first
, unsigned n
)
2781 isl_basic_map
*bmap
= bset_to_bmap(bset
);
2782 bmap
= isl_basic_map_remove_dims(bmap
, type
, first
, n
);
2783 return bset_from_bmap(bmap
);
2786 struct isl_map
*isl_map_remove_dims(struct isl_map
*map
,
2787 enum isl_dim_type type
, unsigned first
, unsigned n
)
2794 map
= isl_map_cow(map
);
2797 isl_assert(map
->ctx
, first
+ n
<= isl_map_dim(map
, type
), goto error
);
2799 for (i
= 0; i
< map
->n
; ++i
) {
2800 map
->p
[i
] = isl_basic_map_eliminate_vars(map
->p
[i
],
2801 isl_basic_map_offset(map
->p
[i
], type
) - 1 + first
, n
);
2805 map
= isl_map_drop(map
, type
, first
, n
);
2812 __isl_give isl_set
*isl_set_remove_dims(__isl_take isl_set
*bset
,
2813 enum isl_dim_type type
, unsigned first
, unsigned n
)
2815 return set_from_map(isl_map_remove_dims(set_to_map(bset
),
2819 /* Project out n inputs starting at first using Fourier-Motzkin */
2820 struct isl_map
*isl_map_remove_inputs(struct isl_map
*map
,
2821 unsigned first
, unsigned n
)
2823 return isl_map_remove_dims(map
, isl_dim_in
, first
, n
);
2826 static void dump_term(struct isl_basic_map
*bmap
,
2827 isl_int c
, int pos
, FILE *out
)
2830 unsigned in
= isl_basic_map_dim(bmap
, isl_dim_in
);
2831 unsigned dim
= in
+ isl_basic_map_dim(bmap
, isl_dim_out
);
2832 unsigned nparam
= isl_basic_map_dim(bmap
, isl_dim_param
);
2834 isl_int_print(out
, c
, 0);
2836 if (!isl_int_is_one(c
))
2837 isl_int_print(out
, c
, 0);
2838 if (pos
< 1 + nparam
) {
2839 name
= isl_space_get_dim_name(bmap
->dim
,
2840 isl_dim_param
, pos
- 1);
2842 fprintf(out
, "%s", name
);
2844 fprintf(out
, "p%d", pos
- 1);
2845 } else if (pos
< 1 + nparam
+ in
)
2846 fprintf(out
, "i%d", pos
- 1 - nparam
);
2847 else if (pos
< 1 + nparam
+ dim
)
2848 fprintf(out
, "o%d", pos
- 1 - nparam
- in
);
2850 fprintf(out
, "e%d", pos
- 1 - nparam
- dim
);
2854 static void dump_constraint_sign(struct isl_basic_map
*bmap
, isl_int
*c
,
2855 int sign
, FILE *out
)
2859 unsigned len
= 1 + isl_basic_map_total_dim(bmap
);
2863 for (i
= 0, first
= 1; i
< len
; ++i
) {
2864 if (isl_int_sgn(c
[i
]) * sign
<= 0)
2867 fprintf(out
, " + ");
2869 isl_int_abs(v
, c
[i
]);
2870 dump_term(bmap
, v
, i
, out
);
2877 static void dump_constraint(struct isl_basic_map
*bmap
, isl_int
*c
,
2878 const char *op
, FILE *out
, int indent
)
2882 fprintf(out
, "%*s", indent
, "");
2884 dump_constraint_sign(bmap
, c
, 1, out
);
2885 fprintf(out
, " %s ", op
);
2886 dump_constraint_sign(bmap
, c
, -1, out
);
2890 for (i
= bmap
->n_div
; i
< bmap
->extra
; ++i
) {
2891 if (isl_int_is_zero(c
[1+isl_space_dim(bmap
->dim
, isl_dim_all
)+i
]))
2893 fprintf(out
, "%*s", indent
, "");
2894 fprintf(out
, "ERROR: unused div coefficient not zero\n");
2899 static void dump_constraints(struct isl_basic_map
*bmap
,
2900 isl_int
**c
, unsigned n
,
2901 const char *op
, FILE *out
, int indent
)
2905 for (i
= 0; i
< n
; ++i
)
2906 dump_constraint(bmap
, c
[i
], op
, out
, indent
);
2909 static void dump_affine(struct isl_basic_map
*bmap
, isl_int
*exp
, FILE *out
)
2913 unsigned total
= isl_basic_map_total_dim(bmap
);
2915 for (j
= 0; j
< 1 + total
; ++j
) {
2916 if (isl_int_is_zero(exp
[j
]))
2918 if (!first
&& isl_int_is_pos(exp
[j
]))
2920 dump_term(bmap
, exp
[j
], j
, out
);
2925 static void dump(struct isl_basic_map
*bmap
, FILE *out
, int indent
)
2929 dump_constraints(bmap
, bmap
->eq
, bmap
->n_eq
, "=", out
, indent
);
2930 dump_constraints(bmap
, bmap
->ineq
, bmap
->n_ineq
, ">=", out
, indent
);
2932 for (i
= 0; i
< bmap
->n_div
; ++i
) {
2933 fprintf(out
, "%*s", indent
, "");
2934 fprintf(out
, "e%d = [(", i
);
2935 dump_affine(bmap
, bmap
->div
[i
]+1, out
);
2937 isl_int_print(out
, bmap
->div
[i
][0], 0);
2938 fprintf(out
, "]\n");
2942 void isl_basic_set_print_internal(struct isl_basic_set
*bset
,
2943 FILE *out
, int indent
)
2946 fprintf(out
, "null basic set\n");
2950 fprintf(out
, "%*s", indent
, "");
2951 fprintf(out
, "ref: %d, nparam: %d, dim: %d, extra: %d, flags: %x\n",
2952 bset
->ref
, bset
->dim
->nparam
, bset
->dim
->n_out
,
2953 bset
->extra
, bset
->flags
);
2954 dump(bset_to_bmap(bset
), out
, indent
);
2957 void isl_basic_map_print_internal(struct isl_basic_map
*bmap
,
2958 FILE *out
, int indent
)
2961 fprintf(out
, "null basic map\n");
2965 fprintf(out
, "%*s", indent
, "");
2966 fprintf(out
, "ref: %d, nparam: %d, in: %d, out: %d, extra: %d, "
2967 "flags: %x, n_name: %d\n",
2969 bmap
->dim
->nparam
, bmap
->dim
->n_in
, bmap
->dim
->n_out
,
2970 bmap
->extra
, bmap
->flags
, bmap
->dim
->n_id
);
2971 dump(bmap
, out
, indent
);
2974 int isl_inequality_negate(struct isl_basic_map
*bmap
, unsigned pos
)
2979 total
= isl_basic_map_total_dim(bmap
);
2980 isl_assert(bmap
->ctx
, pos
< bmap
->n_ineq
, return -1);
2981 isl_seq_neg(bmap
->ineq
[pos
], bmap
->ineq
[pos
], 1 + total
);
2982 isl_int_sub_ui(bmap
->ineq
[pos
][0], bmap
->ineq
[pos
][0], 1);
2983 ISL_F_CLR(bmap
, ISL_BASIC_MAP_NORMALIZED
);
2987 __isl_give isl_set
*isl_set_alloc_space(__isl_take isl_space
*space
, int n
,
2992 if (isl_space_dim(space
, isl_dim_in
) != 0)
2993 isl_die(isl_space_get_ctx(space
), isl_error_invalid
,
2994 "set cannot have input dimensions", goto error
);
2995 return isl_map_alloc_space(space
, n
, flags
);
2997 isl_space_free(space
);
3001 /* Make sure "map" has room for at least "n" more basic maps.
3003 struct isl_map
*isl_map_grow(struct isl_map
*map
, int n
)
3006 struct isl_map
*grown
= NULL
;
3010 isl_assert(map
->ctx
, n
>= 0, goto error
);
3011 if (map
->n
+ n
<= map
->size
)
3013 grown
= isl_map_alloc_space(isl_map_get_space(map
), map
->n
+ n
, map
->flags
);
3016 for (i
= 0; i
< map
->n
; ++i
) {
3017 grown
->p
[i
] = isl_basic_map_copy(map
->p
[i
]);
3025 isl_map_free(grown
);
3030 /* Make sure "set" has room for at least "n" more basic sets.
3032 struct isl_set
*isl_set_grow(struct isl_set
*set
, int n
)
3034 return set_from_map(isl_map_grow(set_to_map(set
), n
));
3037 struct isl_set
*isl_set_from_basic_set(struct isl_basic_set
*bset
)
3039 return isl_map_from_basic_map(bset
);
3042 struct isl_map
*isl_map_from_basic_map(struct isl_basic_map
*bmap
)
3044 struct isl_map
*map
;
3049 map
= isl_map_alloc_space(isl_space_copy(bmap
->dim
), 1, ISL_MAP_DISJOINT
);
3050 return isl_map_add_basic_map(map
, bmap
);
3053 __isl_give isl_set
*isl_set_add_basic_set(__isl_take isl_set
*set
,
3054 __isl_take isl_basic_set
*bset
)
3056 return set_from_map(isl_map_add_basic_map(set_to_map(set
),
3057 bset_to_bmap(bset
)));
3060 __isl_null isl_set
*isl_set_free(__isl_take isl_set
*set
)
3062 return isl_map_free(set
);
3065 void isl_set_print_internal(struct isl_set
*set
, FILE *out
, int indent
)
3070 fprintf(out
, "null set\n");
3074 fprintf(out
, "%*s", indent
, "");
3075 fprintf(out
, "ref: %d, n: %d, nparam: %d, dim: %d, flags: %x\n",
3076 set
->ref
, set
->n
, set
->dim
->nparam
, set
->dim
->n_out
,
3078 for (i
= 0; i
< set
->n
; ++i
) {
3079 fprintf(out
, "%*s", indent
, "");
3080 fprintf(out
, "basic set %d:\n", i
);
3081 isl_basic_set_print_internal(set
->p
[i
], out
, indent
+4);
3085 void isl_map_print_internal(struct isl_map
*map
, FILE *out
, int indent
)
3090 fprintf(out
, "null map\n");
3094 fprintf(out
, "%*s", indent
, "");
3095 fprintf(out
, "ref: %d, n: %d, nparam: %d, in: %d, out: %d, "
3096 "flags: %x, n_name: %d\n",
3097 map
->ref
, map
->n
, map
->dim
->nparam
, map
->dim
->n_in
,
3098 map
->dim
->n_out
, map
->flags
, map
->dim
->n_id
);
3099 for (i
= 0; i
< map
->n
; ++i
) {
3100 fprintf(out
, "%*s", indent
, "");
3101 fprintf(out
, "basic map %d:\n", i
);
3102 isl_basic_map_print_internal(map
->p
[i
], out
, indent
+4);
3106 struct isl_basic_map
*isl_basic_map_intersect_domain(
3107 struct isl_basic_map
*bmap
, struct isl_basic_set
*bset
)
3109 struct isl_basic_map
*bmap_domain
;
3111 if (isl_basic_map_check_equal_params(bmap
, bset_to_bmap(bset
)) < 0)
3114 if (isl_space_dim(bset
->dim
, isl_dim_set
) != 0)
3115 isl_assert(bset
->ctx
,
3116 isl_basic_map_compatible_domain(bmap
, bset
), goto error
);
3118 bmap
= isl_basic_map_cow(bmap
);
3121 bmap
= isl_basic_map_extend_space(bmap
, isl_space_copy(bmap
->dim
),
3122 bset
->n_div
, bset
->n_eq
, bset
->n_ineq
);
3123 bmap_domain
= isl_basic_map_from_domain(bset
);
3124 bmap
= add_constraints(bmap
, bmap_domain
, 0, 0);
3126 bmap
= isl_basic_map_simplify(bmap
);
3127 return isl_basic_map_finalize(bmap
);
3129 isl_basic_map_free(bmap
);
3130 isl_basic_set_free(bset
);
3134 /* Check that the space of "bset" is the same as that of the range of "bmap".
3136 static isl_stat
isl_basic_map_check_compatible_range(
3137 __isl_keep isl_basic_map
*bmap
, __isl_keep isl_basic_set
*bset
)
3141 ok
= isl_basic_map_compatible_range(bmap
, bset
);
3143 return isl_stat_error
;
3145 isl_die(isl_basic_set_get_ctx(bset
), isl_error_invalid
,
3146 "incompatible spaces", return isl_stat_error
);
3151 struct isl_basic_map
*isl_basic_map_intersect_range(
3152 struct isl_basic_map
*bmap
, struct isl_basic_set
*bset
)
3154 struct isl_basic_map
*bmap_range
;
3156 if (isl_basic_map_check_equal_params(bmap
, bset_to_bmap(bset
)) < 0)
3159 if (isl_space_dim(bset
->dim
, isl_dim_set
) != 0 &&
3160 isl_basic_map_check_compatible_range(bmap
, bset
) < 0)
3163 if (isl_basic_set_plain_is_universe(bset
)) {
3164 isl_basic_set_free(bset
);
3168 bmap
= isl_basic_map_cow(bmap
);
3171 bmap
= isl_basic_map_extend_space(bmap
, isl_space_copy(bmap
->dim
),
3172 bset
->n_div
, bset
->n_eq
, bset
->n_ineq
);
3173 bmap_range
= bset_to_bmap(bset
);
3174 bmap
= add_constraints(bmap
, bmap_range
, 0, 0);
3176 bmap
= isl_basic_map_simplify(bmap
);
3177 return isl_basic_map_finalize(bmap
);
3179 isl_basic_map_free(bmap
);
3180 isl_basic_set_free(bset
);
3184 isl_bool
isl_basic_map_contains(__isl_keep isl_basic_map
*bmap
,
3185 __isl_keep isl_vec
*vec
)
3192 return isl_bool_error
;
3194 total
= 1 + isl_basic_map_total_dim(bmap
);
3195 if (total
!= vec
->size
)
3196 return isl_bool_false
;
3200 for (i
= 0; i
< bmap
->n_eq
; ++i
) {
3201 isl_seq_inner_product(vec
->el
, bmap
->eq
[i
], total
, &s
);
3202 if (!isl_int_is_zero(s
)) {
3204 return isl_bool_false
;
3208 for (i
= 0; i
< bmap
->n_ineq
; ++i
) {
3209 isl_seq_inner_product(vec
->el
, bmap
->ineq
[i
], total
, &s
);
3210 if (isl_int_is_neg(s
)) {
3212 return isl_bool_false
;
3218 return isl_bool_true
;
3221 isl_bool
isl_basic_set_contains(__isl_keep isl_basic_set
*bset
,
3222 __isl_keep isl_vec
*vec
)
3224 return isl_basic_map_contains(bset_to_bmap(bset
), vec
);
3227 struct isl_basic_map
*isl_basic_map_intersect(
3228 struct isl_basic_map
*bmap1
, struct isl_basic_map
*bmap2
)
3230 struct isl_vec
*sample
= NULL
;
3232 if (isl_basic_map_check_equal_params(bmap1
, bmap2
) < 0)
3234 if (isl_space_dim(bmap1
->dim
, isl_dim_all
) ==
3235 isl_space_dim(bmap1
->dim
, isl_dim_param
) &&
3236 isl_space_dim(bmap2
->dim
, isl_dim_all
) !=
3237 isl_space_dim(bmap2
->dim
, isl_dim_param
))
3238 return isl_basic_map_intersect(bmap2
, bmap1
);
3240 if (isl_space_dim(bmap2
->dim
, isl_dim_all
) !=
3241 isl_space_dim(bmap2
->dim
, isl_dim_param
))
3242 isl_assert(bmap1
->ctx
,
3243 isl_space_is_equal(bmap1
->dim
, bmap2
->dim
), goto error
);
3245 if (isl_basic_map_plain_is_empty(bmap1
)) {
3246 isl_basic_map_free(bmap2
);
3249 if (isl_basic_map_plain_is_empty(bmap2
)) {
3250 isl_basic_map_free(bmap1
);
3254 if (bmap1
->sample
&&
3255 isl_basic_map_contains(bmap1
, bmap1
->sample
) > 0 &&
3256 isl_basic_map_contains(bmap2
, bmap1
->sample
) > 0)
3257 sample
= isl_vec_copy(bmap1
->sample
);
3258 else if (bmap2
->sample
&&
3259 isl_basic_map_contains(bmap1
, bmap2
->sample
) > 0 &&
3260 isl_basic_map_contains(bmap2
, bmap2
->sample
) > 0)
3261 sample
= isl_vec_copy(bmap2
->sample
);
3263 bmap1
= isl_basic_map_cow(bmap1
);
3266 bmap1
= isl_basic_map_extend_space(bmap1
, isl_space_copy(bmap1
->dim
),
3267 bmap2
->n_div
, bmap2
->n_eq
, bmap2
->n_ineq
);
3268 bmap1
= add_constraints(bmap1
, bmap2
, 0, 0);
3271 isl_vec_free(sample
);
3273 isl_vec_free(bmap1
->sample
);
3274 bmap1
->sample
= sample
;
3277 bmap1
= isl_basic_map_simplify(bmap1
);
3278 return isl_basic_map_finalize(bmap1
);
3281 isl_vec_free(sample
);
3282 isl_basic_map_free(bmap1
);
3283 isl_basic_map_free(bmap2
);
3287 struct isl_basic_set
*isl_basic_set_intersect(
3288 struct isl_basic_set
*bset1
, struct isl_basic_set
*bset2
)
3290 return bset_from_bmap(isl_basic_map_intersect(bset_to_bmap(bset1
),
3291 bset_to_bmap(bset2
)));
3294 __isl_give isl_basic_set
*isl_basic_set_intersect_params(
3295 __isl_take isl_basic_set
*bset1
, __isl_take isl_basic_set
*bset2
)
3297 return isl_basic_set_intersect(bset1
, bset2
);
3300 /* Special case of isl_map_intersect, where both map1 and map2
3301 * are convex, without any divs and such that either map1 or map2
3302 * contains a single constraint. This constraint is then simply
3303 * added to the other map.
3305 static __isl_give isl_map
*map_intersect_add_constraint(
3306 __isl_take isl_map
*map1
, __isl_take isl_map
*map2
)
3308 isl_assert(map1
->ctx
, map1
->n
== 1, goto error
);
3309 isl_assert(map2
->ctx
, map1
->n
== 1, goto error
);
3310 isl_assert(map1
->ctx
, map1
->p
[0]->n_div
== 0, goto error
);
3311 isl_assert(map2
->ctx
, map1
->p
[0]->n_div
== 0, goto error
);
3313 if (map2
->p
[0]->n_eq
+ map2
->p
[0]->n_ineq
!= 1)
3314 return isl_map_intersect(map2
, map1
);
3316 map1
= isl_map_cow(map1
);
3319 if (isl_map_plain_is_empty(map1
)) {
3323 map1
->p
[0] = isl_basic_map_cow(map1
->p
[0]);
3324 if (map2
->p
[0]->n_eq
== 1)
3325 map1
->p
[0] = isl_basic_map_add_eq(map1
->p
[0], map2
->p
[0]->eq
[0]);
3327 map1
->p
[0] = isl_basic_map_add_ineq(map1
->p
[0],
3328 map2
->p
[0]->ineq
[0]);
3330 map1
->p
[0] = isl_basic_map_simplify(map1
->p
[0]);
3331 map1
->p
[0] = isl_basic_map_finalize(map1
->p
[0]);
3335 if (isl_basic_map_plain_is_empty(map1
->p
[0])) {
3336 isl_basic_map_free(map1
->p
[0]);
3349 /* map2 may be either a parameter domain or a map living in the same
3352 static __isl_give isl_map
*map_intersect_internal(__isl_take isl_map
*map1
,
3353 __isl_take isl_map
*map2
)
3362 if ((isl_map_plain_is_empty(map1
) ||
3363 isl_map_plain_is_universe(map2
)) &&
3364 isl_space_is_equal(map1
->dim
, map2
->dim
)) {
3368 if ((isl_map_plain_is_empty(map2
) ||
3369 isl_map_plain_is_universe(map1
)) &&
3370 isl_space_is_equal(map1
->dim
, map2
->dim
)) {
3375 if (map1
->n
== 1 && map2
->n
== 1 &&
3376 map1
->p
[0]->n_div
== 0 && map2
->p
[0]->n_div
== 0 &&
3377 isl_space_is_equal(map1
->dim
, map2
->dim
) &&
3378 (map1
->p
[0]->n_eq
+ map1
->p
[0]->n_ineq
== 1 ||
3379 map2
->p
[0]->n_eq
+ map2
->p
[0]->n_ineq
== 1))
3380 return map_intersect_add_constraint(map1
, map2
);
3382 if (isl_space_dim(map2
->dim
, isl_dim_all
) !=
3383 isl_space_dim(map2
->dim
, isl_dim_param
))
3384 isl_assert(map1
->ctx
,
3385 isl_space_is_equal(map1
->dim
, map2
->dim
), goto error
);
3387 if (ISL_F_ISSET(map1
, ISL_MAP_DISJOINT
) &&
3388 ISL_F_ISSET(map2
, ISL_MAP_DISJOINT
))
3389 ISL_FL_SET(flags
, ISL_MAP_DISJOINT
);
3391 result
= isl_map_alloc_space(isl_space_copy(map1
->dim
),
3392 map1
->n
* map2
->n
, flags
);
3395 for (i
= 0; i
< map1
->n
; ++i
)
3396 for (j
= 0; j
< map2
->n
; ++j
) {
3397 struct isl_basic_map
*part
;
3398 part
= isl_basic_map_intersect(
3399 isl_basic_map_copy(map1
->p
[i
]),
3400 isl_basic_map_copy(map2
->p
[j
]));
3401 if (isl_basic_map_is_empty(part
) < 0)
3402 part
= isl_basic_map_free(part
);
3403 result
= isl_map_add_basic_map(result
, part
);
3416 static __isl_give isl_map
*map_intersect(__isl_take isl_map
*map1
,
3417 __isl_take isl_map
*map2
)
3421 if (!isl_space_is_equal(map1
->dim
, map2
->dim
))
3422 isl_die(isl_map_get_ctx(map1
), isl_error_invalid
,
3423 "spaces don't match", goto error
);
3424 return map_intersect_internal(map1
, map2
);
3431 __isl_give isl_map
*isl_map_intersect(__isl_take isl_map
*map1
,
3432 __isl_take isl_map
*map2
)
3434 return isl_map_align_params_map_map_and(map1
, map2
, &map_intersect
);
3437 struct isl_set
*isl_set_intersect(struct isl_set
*set1
, struct isl_set
*set2
)
3439 return set_from_map(isl_map_intersect(set_to_map(set1
),
3443 /* map_intersect_internal accepts intersections
3444 * with parameter domains, so we can just call that function.
3446 static __isl_give isl_map
*map_intersect_params(__isl_take isl_map
*map
,
3447 __isl_take isl_set
*params
)
3449 return map_intersect_internal(map
, params
);
3452 __isl_give isl_map
*isl_map_intersect_params(__isl_take isl_map
*map1
,
3453 __isl_take isl_map
*map2
)
3455 return isl_map_align_params_map_map_and(map1
, map2
, &map_intersect_params
);
3458 __isl_give isl_set
*isl_set_intersect_params(__isl_take isl_set
*set
,
3459 __isl_take isl_set
*params
)
3461 return isl_map_intersect_params(set
, params
);
3464 struct isl_basic_map
*isl_basic_map_reverse(struct isl_basic_map
*bmap
)
3467 unsigned pos
, n1
, n2
;
3471 bmap
= isl_basic_map_cow(bmap
);
3474 space
= isl_space_reverse(isl_space_copy(bmap
->dim
));
3475 pos
= isl_basic_map_offset(bmap
, isl_dim_in
);
3476 n1
= isl_basic_map_dim(bmap
, isl_dim_in
);
3477 n2
= isl_basic_map_dim(bmap
, isl_dim_out
);
3478 bmap
= isl_basic_map_swap_vars(bmap
, pos
, n1
, n2
);
3479 return isl_basic_map_reset_space(bmap
, space
);
3482 static __isl_give isl_basic_map
*basic_map_space_reset(
3483 __isl_take isl_basic_map
*bmap
, enum isl_dim_type type
)
3489 if (!isl_space_is_named_or_nested(bmap
->dim
, type
))
3492 space
= isl_basic_map_get_space(bmap
);
3493 space
= isl_space_reset(space
, type
);
3494 bmap
= isl_basic_map_reset_space(bmap
, space
);
3498 __isl_give isl_basic_map
*isl_basic_map_insert_dims(
3499 __isl_take isl_basic_map
*bmap
, enum isl_dim_type type
,
3500 unsigned pos
, unsigned n
)
3504 struct isl_basic_map
*res
;
3505 struct isl_dim_map
*dim_map
;
3506 unsigned total
, off
;
3507 enum isl_dim_type t
;
3510 return basic_map_space_reset(bmap
, type
);
3515 res_dim
= isl_space_insert_dims(isl_basic_map_get_space(bmap
), type
, pos
, n
);
3517 total
= isl_basic_map_total_dim(bmap
) + n
;
3518 dim_map
= isl_dim_map_alloc(bmap
->ctx
, total
);
3520 for (t
= isl_dim_param
; t
<= isl_dim_out
; ++t
) {
3522 isl_dim_map_dim(dim_map
, bmap
->dim
, t
, off
);
3524 unsigned size
= isl_basic_map_dim(bmap
, t
);
3525 isl_dim_map_dim_range(dim_map
, bmap
->dim
, t
,
3527 isl_dim_map_dim_range(dim_map
, bmap
->dim
, t
,
3528 pos
, size
- pos
, off
+ pos
+ n
);
3530 off
+= isl_space_dim(res_dim
, t
);
3532 isl_dim_map_div(dim_map
, bmap
, off
);
3534 res
= isl_basic_map_alloc_space(res_dim
,
3535 bmap
->n_div
, bmap
->n_eq
, bmap
->n_ineq
);
3536 rational
= isl_basic_map_is_rational(bmap
);
3538 res
= isl_basic_map_free(res
);
3540 res
= isl_basic_map_set_rational(res
);
3541 if (isl_basic_map_plain_is_empty(bmap
)) {
3542 isl_basic_map_free(bmap
);
3544 return isl_basic_map_set_to_empty(res
);
3546 res
= isl_basic_map_add_constraints_dim_map(res
, bmap
, dim_map
);
3547 return isl_basic_map_finalize(res
);
3550 __isl_give isl_basic_set
*isl_basic_set_insert_dims(
3551 __isl_take isl_basic_set
*bset
,
3552 enum isl_dim_type type
, unsigned pos
, unsigned n
)
3554 return isl_basic_map_insert_dims(bset
, type
, pos
, n
);
3557 __isl_give isl_basic_map
*isl_basic_map_add_dims(__isl_take isl_basic_map
*bmap
,
3558 enum isl_dim_type type
, unsigned n
)
3562 return isl_basic_map_insert_dims(bmap
, type
,
3563 isl_basic_map_dim(bmap
, type
), n
);
3566 __isl_give isl_basic_set
*isl_basic_set_add_dims(__isl_take isl_basic_set
*bset
,
3567 enum isl_dim_type type
, unsigned n
)
3571 isl_assert(bset
->ctx
, type
!= isl_dim_in
, goto error
);
3572 return isl_basic_map_add_dims(bset
, type
, n
);
3574 isl_basic_set_free(bset
);
3578 static __isl_give isl_map
*map_space_reset(__isl_take isl_map
*map
,
3579 enum isl_dim_type type
)
3583 if (!map
|| !isl_space_is_named_or_nested(map
->dim
, type
))
3586 space
= isl_map_get_space(map
);
3587 space
= isl_space_reset(space
, type
);
3588 map
= isl_map_reset_space(map
, space
);
3592 __isl_give isl_map
*isl_map_insert_dims(__isl_take isl_map
*map
,
3593 enum isl_dim_type type
, unsigned pos
, unsigned n
)
3598 return map_space_reset(map
, type
);
3600 map
= isl_map_cow(map
);
3604 map
->dim
= isl_space_insert_dims(map
->dim
, type
, pos
, n
);
3608 for (i
= 0; i
< map
->n
; ++i
) {
3609 map
->p
[i
] = isl_basic_map_insert_dims(map
->p
[i
], type
, pos
, n
);
3620 __isl_give isl_set
*isl_set_insert_dims(__isl_take isl_set
*set
,
3621 enum isl_dim_type type
, unsigned pos
, unsigned n
)
3623 return isl_map_insert_dims(set
, type
, pos
, n
);
3626 __isl_give isl_map
*isl_map_add_dims(__isl_take isl_map
*map
,
3627 enum isl_dim_type type
, unsigned n
)
3631 return isl_map_insert_dims(map
, type
, isl_map_dim(map
, type
), n
);
3634 __isl_give isl_set
*isl_set_add_dims(__isl_take isl_set
*set
,
3635 enum isl_dim_type type
, unsigned n
)
3639 isl_assert(set
->ctx
, type
!= isl_dim_in
, goto error
);
3640 return set_from_map(isl_map_add_dims(set_to_map(set
), type
, n
));
3646 __isl_give isl_basic_map
*isl_basic_map_move_dims(
3647 __isl_take isl_basic_map
*bmap
,
3648 enum isl_dim_type dst_type
, unsigned dst_pos
,
3649 enum isl_dim_type src_type
, unsigned src_pos
, unsigned n
)
3651 struct isl_dim_map
*dim_map
;
3652 struct isl_basic_map
*res
;
3653 enum isl_dim_type t
;
3654 unsigned total
, off
;
3661 if (isl_basic_map_check_range(bmap
, src_type
, src_pos
, n
) < 0)
3662 return isl_basic_map_free(bmap
);
3664 if (dst_type
== src_type
&& dst_pos
== src_pos
)
3667 isl_assert(bmap
->ctx
, dst_type
!= src_type
, goto error
);
3669 if (pos(bmap
->dim
, dst_type
) + dst_pos
==
3670 pos(bmap
->dim
, src_type
) + src_pos
+
3671 ((src_type
< dst_type
) ? n
: 0)) {
3672 bmap
= isl_basic_map_cow(bmap
);
3676 bmap
->dim
= isl_space_move_dims(bmap
->dim
, dst_type
, dst_pos
,
3677 src_type
, src_pos
, n
);
3681 bmap
= isl_basic_map_finalize(bmap
);
3686 total
= isl_basic_map_total_dim(bmap
);
3687 dim_map
= isl_dim_map_alloc(bmap
->ctx
, total
);
3690 for (t
= isl_dim_param
; t
<= isl_dim_out
; ++t
) {
3691 unsigned size
= isl_space_dim(bmap
->dim
, t
);
3692 if (t
== dst_type
) {
3693 isl_dim_map_dim_range(dim_map
, bmap
->dim
, t
,
3696 isl_dim_map_dim_range(dim_map
, bmap
->dim
, src_type
,
3699 isl_dim_map_dim_range(dim_map
, bmap
->dim
, t
,
3700 dst_pos
, size
- dst_pos
, off
);
3701 off
+= size
- dst_pos
;
3702 } else if (t
== src_type
) {
3703 isl_dim_map_dim_range(dim_map
, bmap
->dim
, t
,
3706 isl_dim_map_dim_range(dim_map
, bmap
->dim
, t
,
3707 src_pos
+ n
, size
- src_pos
- n
, off
);
3708 off
+= size
- src_pos
- n
;
3710 isl_dim_map_dim(dim_map
, bmap
->dim
, t
, off
);
3714 isl_dim_map_div(dim_map
, bmap
, off
);
3716 res
= isl_basic_map_alloc_space(isl_basic_map_get_space(bmap
),
3717 bmap
->n_div
, bmap
->n_eq
, bmap
->n_ineq
);
3718 bmap
= isl_basic_map_add_constraints_dim_map(res
, bmap
, dim_map
);
3722 bmap
->dim
= isl_space_move_dims(bmap
->dim
, dst_type
, dst_pos
,
3723 src_type
, src_pos
, n
);
3727 ISL_F_CLR(bmap
, ISL_BASIC_MAP_NORMALIZED
);
3728 bmap
= isl_basic_map_gauss(bmap
, NULL
);
3729 bmap
= isl_basic_map_finalize(bmap
);
3733 isl_basic_map_free(bmap
);
3737 __isl_give isl_basic_set
*isl_basic_set_move_dims(__isl_take isl_basic_set
*bset
,
3738 enum isl_dim_type dst_type
, unsigned dst_pos
,
3739 enum isl_dim_type src_type
, unsigned src_pos
, unsigned n
)
3741 isl_basic_map
*bmap
= bset_to_bmap(bset
);
3742 bmap
= isl_basic_map_move_dims(bmap
, dst_type
, dst_pos
,
3743 src_type
, src_pos
, n
);
3744 return bset_from_bmap(bmap
);
3747 __isl_give isl_set
*isl_set_move_dims(__isl_take isl_set
*set
,
3748 enum isl_dim_type dst_type
, unsigned dst_pos
,
3749 enum isl_dim_type src_type
, unsigned src_pos
, unsigned n
)
3753 isl_assert(set
->ctx
, dst_type
!= isl_dim_in
, goto error
);
3754 return set_from_map(isl_map_move_dims(set_to_map(set
),
3755 dst_type
, dst_pos
, src_type
, src_pos
, n
));
3761 __isl_give isl_map
*isl_map_move_dims(__isl_take isl_map
*map
,
3762 enum isl_dim_type dst_type
, unsigned dst_pos
,
3763 enum isl_dim_type src_type
, unsigned src_pos
, unsigned n
)
3772 isl_assert(map
->ctx
, src_pos
+ n
<= isl_map_dim(map
, src_type
),
3775 if (dst_type
== src_type
&& dst_pos
== src_pos
)
3778 isl_assert(map
->ctx
, dst_type
!= src_type
, goto error
);
3780 map
= isl_map_cow(map
);
3784 map
->dim
= isl_space_move_dims(map
->dim
, dst_type
, dst_pos
, src_type
, src_pos
, n
);
3788 for (i
= 0; i
< map
->n
; ++i
) {
3789 map
->p
[i
] = isl_basic_map_move_dims(map
->p
[i
],
3791 src_type
, src_pos
, n
);
3802 /* Move the specified dimensions to the last columns right before
3803 * the divs. Don't change the dimension specification of bmap.
3804 * That's the responsibility of the caller.
3806 static __isl_give isl_basic_map
*move_last(__isl_take isl_basic_map
*bmap
,
3807 enum isl_dim_type type
, unsigned first
, unsigned n
)
3809 struct isl_dim_map
*dim_map
;
3810 struct isl_basic_map
*res
;
3811 enum isl_dim_type t
;
3812 unsigned total
, off
;
3816 if (pos(bmap
->dim
, type
) + first
+ n
==
3817 1 + isl_space_dim(bmap
->dim
, isl_dim_all
))
3820 total
= isl_basic_map_total_dim(bmap
);
3821 dim_map
= isl_dim_map_alloc(bmap
->ctx
, total
);
3824 for (t
= isl_dim_param
; t
<= isl_dim_out
; ++t
) {
3825 unsigned size
= isl_space_dim(bmap
->dim
, t
);
3827 isl_dim_map_dim_range(dim_map
, bmap
->dim
, t
,
3830 isl_dim_map_dim_range(dim_map
, bmap
->dim
, t
,
3831 first
, n
, total
- bmap
->n_div
- n
);
3832 isl_dim_map_dim_range(dim_map
, bmap
->dim
, t
,
3833 first
+ n
, size
- (first
+ n
), off
);
3834 off
+= size
- (first
+ n
);
3836 isl_dim_map_dim(dim_map
, bmap
->dim
, t
, off
);
3840 isl_dim_map_div(dim_map
, bmap
, off
+ n
);
3842 res
= isl_basic_map_alloc_space(isl_basic_map_get_space(bmap
),
3843 bmap
->n_div
, bmap
->n_eq
, bmap
->n_ineq
);
3844 res
= isl_basic_map_add_constraints_dim_map(res
, bmap
, dim_map
);
3848 /* Insert "n" rows in the divs of "bmap".
3850 * The number of columns is not changed, which means that the last
3851 * dimensions of "bmap" are being reintepreted as the new divs.
3852 * The space of "bmap" is not adjusted, however, which means
3853 * that "bmap" is left in an inconsistent state. Removing "n" dimensions
3854 * from the space of "bmap" is the responsibility of the caller.
3856 static __isl_give isl_basic_map
*insert_div_rows(__isl_take isl_basic_map
*bmap
,
3864 bmap
= isl_basic_map_cow(bmap
);
3868 row_size
= 1 + isl_space_dim(bmap
->dim
, isl_dim_all
) + bmap
->extra
;
3869 old
= bmap
->block2
.data
;
3870 bmap
->block2
= isl_blk_extend(bmap
->ctx
, bmap
->block2
,
3871 (bmap
->extra
+ n
) * (1 + row_size
));
3872 if (!bmap
->block2
.data
)
3873 return isl_basic_map_free(bmap
);
3874 new_div
= isl_alloc_array(bmap
->ctx
, isl_int
*, bmap
->extra
+ n
);
3876 return isl_basic_map_free(bmap
);
3877 for (i
= 0; i
< n
; ++i
) {
3878 new_div
[i
] = bmap
->block2
.data
+
3879 (bmap
->extra
+ i
) * (1 + row_size
);
3880 isl_seq_clr(new_div
[i
], 1 + row_size
);
3882 for (i
= 0; i
< bmap
->extra
; ++i
)
3883 new_div
[n
+ i
] = bmap
->block2
.data
+ (bmap
->div
[i
] - old
);
3885 bmap
->div
= new_div
;
3892 /* Drop constraints from "bmap" that only involve the variables
3893 * of "type" in the range [first, first + n] that are not related
3894 * to any of the variables outside that interval.
3895 * These constraints cannot influence the values for the variables
3896 * outside the interval, except in case they cause "bmap" to be empty.
3897 * Only drop the constraints if "bmap" is known to be non-empty.
3899 static __isl_give isl_basic_map
*drop_irrelevant_constraints(
3900 __isl_take isl_basic_map
*bmap
, enum isl_dim_type type
,
3901 unsigned first
, unsigned n
)
3905 unsigned dim
, n_div
;
3908 non_empty
= isl_basic_map_plain_is_non_empty(bmap
);
3910 return isl_basic_map_free(bmap
);
3914 dim
= isl_basic_map_dim(bmap
, isl_dim_all
);
3915 n_div
= isl_basic_map_dim(bmap
, isl_dim_div
);
3916 groups
= isl_calloc_array(isl_basic_map_get_ctx(bmap
), int, dim
);
3918 return isl_basic_map_free(bmap
);
3919 first
+= isl_basic_map_offset(bmap
, type
) - 1;
3920 for (i
= 0; i
< first
; ++i
)
3922 for (i
= first
+ n
; i
< dim
- n_div
; ++i
)
3925 bmap
= isl_basic_map_drop_unrelated_constraints(bmap
, groups
);
3930 /* Turn the n dimensions of type type, starting at first
3931 * into existentially quantified variables.
3933 * If a subset of the projected out variables are unrelated
3934 * to any of the variables that remain, then the constraints
3935 * involving this subset are simply dropped first.
3937 __isl_give isl_basic_map
*isl_basic_map_project_out(
3938 __isl_take isl_basic_map
*bmap
,
3939 enum isl_dim_type type
, unsigned first
, unsigned n
)
3944 return basic_map_space_reset(bmap
, type
);
3945 if (type
== isl_dim_div
)
3946 isl_die(isl_basic_map_get_ctx(bmap
), isl_error_invalid
,
3947 "cannot project out existentially quantified variables",
3948 return isl_basic_map_free(bmap
));
3950 empty
= isl_basic_map_plain_is_empty(bmap
);
3952 return isl_basic_map_free(bmap
);
3954 bmap
= isl_basic_map_set_to_empty(bmap
);
3956 bmap
= drop_irrelevant_constraints(bmap
, type
, first
, n
);
3960 if (ISL_F_ISSET(bmap
, ISL_BASIC_MAP_RATIONAL
))
3961 return isl_basic_map_remove_dims(bmap
, type
, first
, n
);
3963 if (isl_basic_map_check_range(bmap
, type
, first
, n
) < 0)
3964 return isl_basic_map_free(bmap
);
3966 bmap
= move_last(bmap
, type
, first
, n
);
3967 bmap
= isl_basic_map_cow(bmap
);
3968 bmap
= insert_div_rows(bmap
, n
);
3972 bmap
->dim
= isl_space_drop_dims(bmap
->dim
, type
, first
, n
);
3975 bmap
= isl_basic_map_simplify(bmap
);
3976 bmap
= isl_basic_map_drop_redundant_divs(bmap
);
3977 return isl_basic_map_finalize(bmap
);
3979 isl_basic_map_free(bmap
);
3983 /* Turn the n dimensions of type type, starting at first
3984 * into existentially quantified variables.
3986 struct isl_basic_set
*isl_basic_set_project_out(struct isl_basic_set
*bset
,
3987 enum isl_dim_type type
, unsigned first
, unsigned n
)
3989 return bset_from_bmap(isl_basic_map_project_out(bset_to_bmap(bset
),
3993 /* Turn the n dimensions of type type, starting at first
3994 * into existentially quantified variables.
3996 __isl_give isl_map
*isl_map_project_out(__isl_take isl_map
*map
,
3997 enum isl_dim_type type
, unsigned first
, unsigned n
)
4005 return map_space_reset(map
, type
);
4007 isl_assert(map
->ctx
, first
+ n
<= isl_map_dim(map
, type
), goto error
);
4009 map
= isl_map_cow(map
);
4013 map
->dim
= isl_space_drop_dims(map
->dim
, type
, first
, n
);
4017 for (i
= 0; i
< map
->n
; ++i
) {
4018 map
->p
[i
] = isl_basic_map_project_out(map
->p
[i
], type
, first
, n
);
4029 /* Turn the n dimensions of type type, starting at first
4030 * into existentially quantified variables.
4032 __isl_give isl_set
*isl_set_project_out(__isl_take isl_set
*set
,
4033 enum isl_dim_type type
, unsigned first
, unsigned n
)
4035 return set_from_map(isl_map_project_out(set_to_map(set
),
4039 /* Return a map that projects the elements in "set" onto their
4040 * "n" set dimensions starting at "first".
4041 * "type" should be equal to isl_dim_set.
4043 __isl_give isl_map
*isl_set_project_onto_map(__isl_take isl_set
*set
,
4044 enum isl_dim_type type
, unsigned first
, unsigned n
)
4052 if (type
!= isl_dim_set
)
4053 isl_die(isl_set_get_ctx(set
), isl_error_invalid
,
4054 "only set dimensions can be projected out", goto error
);
4055 dim
= isl_set_dim(set
, isl_dim_set
);
4056 if (first
+ n
> dim
|| first
+ n
< first
)
4057 isl_die(isl_set_get_ctx(set
), isl_error_invalid
,
4058 "index out of bounds", goto error
);
4060 map
= isl_map_from_domain(set
);
4061 map
= isl_map_add_dims(map
, isl_dim_out
, n
);
4062 for (i
= 0; i
< n
; ++i
)
4063 map
= isl_map_equate(map
, isl_dim_in
, first
+ i
,
4071 static struct isl_basic_map
*add_divs(struct isl_basic_map
*bmap
, unsigned n
)
4075 for (i
= 0; i
< n
; ++i
) {
4076 j
= isl_basic_map_alloc_div(bmap
);
4079 isl_seq_clr(bmap
->div
[j
], 1+1+isl_basic_map_total_dim(bmap
));
4083 isl_basic_map_free(bmap
);
4087 struct isl_basic_map
*isl_basic_map_apply_range(
4088 struct isl_basic_map
*bmap1
, struct isl_basic_map
*bmap2
)
4090 isl_space
*dim_result
= NULL
;
4091 struct isl_basic_map
*bmap
;
4092 unsigned n_in
, n_out
, n
, nparam
, total
, pos
;
4093 struct isl_dim_map
*dim_map1
, *dim_map2
;
4095 if (isl_basic_map_check_equal_params(bmap1
, bmap2
) < 0)
4097 if (!isl_space_tuple_is_equal(bmap1
->dim
, isl_dim_out
,
4098 bmap2
->dim
, isl_dim_in
))
4099 isl_die(isl_basic_map_get_ctx(bmap1
), isl_error_invalid
,
4100 "spaces don't match", goto error
);
4102 dim_result
= isl_space_join(isl_space_copy(bmap1
->dim
),
4103 isl_space_copy(bmap2
->dim
));
4105 n_in
= isl_basic_map_dim(bmap1
, isl_dim_in
);
4106 n_out
= isl_basic_map_dim(bmap2
, isl_dim_out
);
4107 n
= isl_basic_map_dim(bmap1
, isl_dim_out
);
4108 nparam
= isl_basic_map_dim(bmap1
, isl_dim_param
);
4110 total
= nparam
+ n_in
+ n_out
+ bmap1
->n_div
+ bmap2
->n_div
+ n
;
4111 dim_map1
= isl_dim_map_alloc(bmap1
->ctx
, total
);
4112 dim_map2
= isl_dim_map_alloc(bmap1
->ctx
, total
);
4113 isl_dim_map_dim(dim_map1
, bmap1
->dim
, isl_dim_param
, pos
= 0);
4114 isl_dim_map_dim(dim_map2
, bmap2
->dim
, isl_dim_param
, pos
= 0);
4115 isl_dim_map_dim(dim_map1
, bmap1
->dim
, isl_dim_in
, pos
+= nparam
);
4116 isl_dim_map_dim(dim_map2
, bmap2
->dim
, isl_dim_out
, pos
+= n_in
);
4117 isl_dim_map_div(dim_map1
, bmap1
, pos
+= n_out
);
4118 isl_dim_map_div(dim_map2
, bmap2
, pos
+= bmap1
->n_div
);
4119 isl_dim_map_dim(dim_map1
, bmap1
->dim
, isl_dim_out
, pos
+= bmap2
->n_div
);
4120 isl_dim_map_dim(dim_map2
, bmap2
->dim
, isl_dim_in
, pos
);
4122 bmap
= isl_basic_map_alloc_space(dim_result
,
4123 bmap1
->n_div
+ bmap2
->n_div
+ n
,
4124 bmap1
->n_eq
+ bmap2
->n_eq
,
4125 bmap1
->n_ineq
+ bmap2
->n_ineq
);
4126 bmap
= isl_basic_map_add_constraints_dim_map(bmap
, bmap1
, dim_map1
);
4127 bmap
= isl_basic_map_add_constraints_dim_map(bmap
, bmap2
, dim_map2
);
4128 bmap
= add_divs(bmap
, n
);
4129 bmap
= isl_basic_map_simplify(bmap
);
4130 bmap
= isl_basic_map_drop_redundant_divs(bmap
);
4131 return isl_basic_map_finalize(bmap
);
4133 isl_basic_map_free(bmap1
);
4134 isl_basic_map_free(bmap2
);
4138 struct isl_basic_set
*isl_basic_set_apply(
4139 struct isl_basic_set
*bset
, struct isl_basic_map
*bmap
)
4144 isl_assert(bset
->ctx
, isl_basic_map_compatible_domain(bmap
, bset
),
4147 return bset_from_bmap(isl_basic_map_apply_range(bset_to_bmap(bset
),
4150 isl_basic_set_free(bset
);
4151 isl_basic_map_free(bmap
);
4155 struct isl_basic_map
*isl_basic_map_apply_domain(
4156 struct isl_basic_map
*bmap1
, struct isl_basic_map
*bmap2
)
4158 if (isl_basic_map_check_equal_params(bmap1
, bmap2
) < 0)
4160 if (!isl_space_tuple_is_equal(bmap1
->dim
, isl_dim_in
,
4161 bmap2
->dim
, isl_dim_in
))
4162 isl_die(isl_basic_map_get_ctx(bmap1
), isl_error_invalid
,
4163 "spaces don't match", goto error
);
4165 bmap1
= isl_basic_map_reverse(bmap1
);
4166 bmap1
= isl_basic_map_apply_range(bmap1
, bmap2
);
4167 return isl_basic_map_reverse(bmap1
);
4169 isl_basic_map_free(bmap1
);
4170 isl_basic_map_free(bmap2
);
4174 /* Given two basic maps A -> f(A) and B -> g(B), construct a basic map
4175 * A \cap B -> f(A) + f(B)
4177 struct isl_basic_map
*isl_basic_map_sum(
4178 struct isl_basic_map
*bmap1
, struct isl_basic_map
*bmap2
)
4180 unsigned n_in
, n_out
, nparam
, total
, pos
;
4181 struct isl_basic_map
*bmap
= NULL
;
4182 struct isl_dim_map
*dim_map1
, *dim_map2
;
4185 if (!bmap1
|| !bmap2
)
4188 isl_assert(bmap1
->ctx
, isl_space_is_equal(bmap1
->dim
, bmap2
->dim
),
4191 nparam
= isl_basic_map_dim(bmap1
, isl_dim_param
);
4192 n_in
= isl_basic_map_dim(bmap1
, isl_dim_in
);
4193 n_out
= isl_basic_map_dim(bmap1
, isl_dim_out
);
4195 total
= nparam
+ n_in
+ n_out
+ bmap1
->n_div
+ bmap2
->n_div
+ 2 * n_out
;
4196 dim_map1
= isl_dim_map_alloc(bmap1
->ctx
, total
);
4197 dim_map2
= isl_dim_map_alloc(bmap2
->ctx
, total
);
4198 isl_dim_map_dim(dim_map1
, bmap1
->dim
, isl_dim_param
, pos
= 0);
4199 isl_dim_map_dim(dim_map2
, bmap2
->dim
, isl_dim_param
, pos
);
4200 isl_dim_map_dim(dim_map1
, bmap1
->dim
, isl_dim_in
, pos
+= nparam
);
4201 isl_dim_map_dim(dim_map2
, bmap2
->dim
, isl_dim_in
, pos
);
4202 isl_dim_map_div(dim_map1
, bmap1
, pos
+= n_in
+ n_out
);
4203 isl_dim_map_div(dim_map2
, bmap2
, pos
+= bmap1
->n_div
);
4204 isl_dim_map_dim(dim_map1
, bmap1
->dim
, isl_dim_out
, pos
+= bmap2
->n_div
);
4205 isl_dim_map_dim(dim_map2
, bmap2
->dim
, isl_dim_out
, pos
+= n_out
);
4207 bmap
= isl_basic_map_alloc_space(isl_space_copy(bmap1
->dim
),
4208 bmap1
->n_div
+ bmap2
->n_div
+ 2 * n_out
,
4209 bmap1
->n_eq
+ bmap2
->n_eq
+ n_out
,
4210 bmap1
->n_ineq
+ bmap2
->n_ineq
);
4211 for (i
= 0; i
< n_out
; ++i
) {
4212 int j
= isl_basic_map_alloc_equality(bmap
);
4215 isl_seq_clr(bmap
->eq
[j
], 1+total
);
4216 isl_int_set_si(bmap
->eq
[j
][1+nparam
+n_in
+i
], -1);
4217 isl_int_set_si(bmap
->eq
[j
][1+pos
+i
], 1);
4218 isl_int_set_si(bmap
->eq
[j
][1+pos
-n_out
+i
], 1);
4220 bmap
= isl_basic_map_add_constraints_dim_map(bmap
, bmap1
, dim_map1
);
4221 bmap
= isl_basic_map_add_constraints_dim_map(bmap
, bmap2
, dim_map2
);
4222 bmap
= add_divs(bmap
, 2 * n_out
);
4224 bmap
= isl_basic_map_simplify(bmap
);
4225 return isl_basic_map_finalize(bmap
);
4227 isl_basic_map_free(bmap
);
4228 isl_basic_map_free(bmap1
);
4229 isl_basic_map_free(bmap2
);
4233 /* Given two maps A -> f(A) and B -> g(B), construct a map
4234 * A \cap B -> f(A) + f(B)
4236 struct isl_map
*isl_map_sum(struct isl_map
*map1
, struct isl_map
*map2
)
4238 struct isl_map
*result
;
4244 isl_assert(map1
->ctx
, isl_space_is_equal(map1
->dim
, map2
->dim
), goto error
);
4246 result
= isl_map_alloc_space(isl_space_copy(map1
->dim
),
4247 map1
->n
* map2
->n
, 0);
4250 for (i
= 0; i
< map1
->n
; ++i
)
4251 for (j
= 0; j
< map2
->n
; ++j
) {
4252 struct isl_basic_map
*part
;
4253 part
= isl_basic_map_sum(
4254 isl_basic_map_copy(map1
->p
[i
]),
4255 isl_basic_map_copy(map2
->p
[j
]));
4256 if (isl_basic_map_is_empty(part
))
4257 isl_basic_map_free(part
);
4259 result
= isl_map_add_basic_map(result
, part
);
4272 __isl_give isl_set
*isl_set_sum(__isl_take isl_set
*set1
,
4273 __isl_take isl_set
*set2
)
4275 return set_from_map(isl_map_sum(set_to_map(set1
), set_to_map(set2
)));
4278 /* Given a basic map A -> f(A), construct A -> -f(A).
4280 struct isl_basic_map
*isl_basic_map_neg(struct isl_basic_map
*bmap
)
4285 bmap
= isl_basic_map_cow(bmap
);
4289 n
= isl_basic_map_dim(bmap
, isl_dim_out
);
4290 off
= isl_basic_map_offset(bmap
, isl_dim_out
);
4291 for (i
= 0; i
< bmap
->n_eq
; ++i
)
4292 for (j
= 0; j
< n
; ++j
)
4293 isl_int_neg(bmap
->eq
[i
][off
+j
], bmap
->eq
[i
][off
+j
]);
4294 for (i
= 0; i
< bmap
->n_ineq
; ++i
)
4295 for (j
= 0; j
< n
; ++j
)
4296 isl_int_neg(bmap
->ineq
[i
][off
+j
], bmap
->ineq
[i
][off
+j
]);
4297 for (i
= 0; i
< bmap
->n_div
; ++i
)
4298 for (j
= 0; j
< n
; ++j
)
4299 isl_int_neg(bmap
->div
[i
][1+off
+j
], bmap
->div
[i
][1+off
+j
]);
4300 bmap
= isl_basic_map_gauss(bmap
, NULL
);
4301 return isl_basic_map_finalize(bmap
);
4304 __isl_give isl_basic_set
*isl_basic_set_neg(__isl_take isl_basic_set
*bset
)
4306 return isl_basic_map_neg(bset
);
4309 /* Given a map A -> f(A), construct A -> -f(A).
4311 struct isl_map
*isl_map_neg(struct isl_map
*map
)
4315 map
= isl_map_cow(map
);
4319 for (i
= 0; i
< map
->n
; ++i
) {
4320 map
->p
[i
] = isl_basic_map_neg(map
->p
[i
]);
4331 __isl_give isl_set
*isl_set_neg(__isl_take isl_set
*set
)
4333 return set_from_map(isl_map_neg(set_to_map(set
)));
4336 /* Given a basic map A -> f(A) and an integer d, construct a basic map
4337 * A -> floor(f(A)/d).
4339 struct isl_basic_map
*isl_basic_map_floordiv(struct isl_basic_map
*bmap
,
4342 unsigned n_in
, n_out
, nparam
, total
, pos
;
4343 struct isl_basic_map
*result
= NULL
;
4344 struct isl_dim_map
*dim_map
;
4350 nparam
= isl_basic_map_dim(bmap
, isl_dim_param
);
4351 n_in
= isl_basic_map_dim(bmap
, isl_dim_in
);
4352 n_out
= isl_basic_map_dim(bmap
, isl_dim_out
);
4354 total
= nparam
+ n_in
+ n_out
+ bmap
->n_div
+ n_out
;
4355 dim_map
= isl_dim_map_alloc(bmap
->ctx
, total
);
4356 isl_dim_map_dim(dim_map
, bmap
->dim
, isl_dim_param
, pos
= 0);
4357 isl_dim_map_dim(dim_map
, bmap
->dim
, isl_dim_in
, pos
+= nparam
);
4358 isl_dim_map_div(dim_map
, bmap
, pos
+= n_in
+ n_out
);
4359 isl_dim_map_dim(dim_map
, bmap
->dim
, isl_dim_out
, pos
+= bmap
->n_div
);
4361 result
= isl_basic_map_alloc_space(isl_space_copy(bmap
->dim
),
4362 bmap
->n_div
+ n_out
,
4363 bmap
->n_eq
, bmap
->n_ineq
+ 2 * n_out
);
4364 result
= isl_basic_map_add_constraints_dim_map(result
, bmap
, dim_map
);
4365 result
= add_divs(result
, n_out
);
4366 for (i
= 0; i
< n_out
; ++i
) {
4368 j
= isl_basic_map_alloc_inequality(result
);
4371 isl_seq_clr(result
->ineq
[j
], 1+total
);
4372 isl_int_neg(result
->ineq
[j
][1+nparam
+n_in
+i
], d
);
4373 isl_int_set_si(result
->ineq
[j
][1+pos
+i
], 1);
4374 j
= isl_basic_map_alloc_inequality(result
);
4377 isl_seq_clr(result
->ineq
[j
], 1+total
);
4378 isl_int_set(result
->ineq
[j
][1+nparam
+n_in
+i
], d
);
4379 isl_int_set_si(result
->ineq
[j
][1+pos
+i
], -1);
4380 isl_int_sub_ui(result
->ineq
[j
][0], d
, 1);
4383 result
= isl_basic_map_simplify(result
);
4384 return isl_basic_map_finalize(result
);
4386 isl_basic_map_free(result
);
4390 /* Given a map A -> f(A) and an integer d, construct a map
4391 * A -> floor(f(A)/d).
4393 struct isl_map
*isl_map_floordiv(struct isl_map
*map
, isl_int d
)
4397 map
= isl_map_cow(map
);
4401 ISL_F_CLR(map
, ISL_MAP_DISJOINT
);
4402 ISL_F_CLR(map
, ISL_MAP_NORMALIZED
);
4403 for (i
= 0; i
< map
->n
; ++i
) {
4404 map
->p
[i
] = isl_basic_map_floordiv(map
->p
[i
], d
);
4415 /* Given a map A -> f(A) and an integer d, construct a map
4416 * A -> floor(f(A)/d).
4418 __isl_give isl_map
*isl_map_floordiv_val(__isl_take isl_map
*map
,
4419 __isl_take isl_val
*d
)
4423 if (!isl_val_is_int(d
))
4424 isl_die(isl_val_get_ctx(d
), isl_error_invalid
,
4425 "expecting integer denominator", goto error
);
4426 map
= isl_map_floordiv(map
, d
->n
);
4435 static struct isl_basic_map
*var_equal(struct isl_basic_map
*bmap
, unsigned pos
)
4441 i
= isl_basic_map_alloc_equality(bmap
);
4444 nparam
= isl_basic_map_dim(bmap
, isl_dim_param
);
4445 n_in
= isl_basic_map_dim(bmap
, isl_dim_in
);
4446 isl_seq_clr(bmap
->eq
[i
], 1 + isl_basic_map_total_dim(bmap
));
4447 isl_int_set_si(bmap
->eq
[i
][1+nparam
+pos
], -1);
4448 isl_int_set_si(bmap
->eq
[i
][1+nparam
+n_in
+pos
], 1);
4449 return isl_basic_map_finalize(bmap
);
4451 isl_basic_map_free(bmap
);
4455 /* Add a constraint to "bmap" expressing i_pos < o_pos
4457 static struct isl_basic_map
*var_less(struct isl_basic_map
*bmap
, unsigned pos
)
4463 i
= isl_basic_map_alloc_inequality(bmap
);
4466 nparam
= isl_basic_map_dim(bmap
, isl_dim_param
);
4467 n_in
= isl_basic_map_dim(bmap
, isl_dim_in
);
4468 isl_seq_clr(bmap
->ineq
[i
], 1 + isl_basic_map_total_dim(bmap
));
4469 isl_int_set_si(bmap
->ineq
[i
][0], -1);
4470 isl_int_set_si(bmap
->ineq
[i
][1+nparam
+pos
], -1);
4471 isl_int_set_si(bmap
->ineq
[i
][1+nparam
+n_in
+pos
], 1);
4472 return isl_basic_map_finalize(bmap
);
4474 isl_basic_map_free(bmap
);
4478 /* Add a constraint to "bmap" expressing i_pos <= o_pos
4480 static __isl_give isl_basic_map
*var_less_or_equal(
4481 __isl_take isl_basic_map
*bmap
, unsigned pos
)
4487 i
= isl_basic_map_alloc_inequality(bmap
);
4490 nparam
= isl_basic_map_dim(bmap
, isl_dim_param
);
4491 n_in
= isl_basic_map_dim(bmap
, isl_dim_in
);
4492 isl_seq_clr(bmap
->ineq
[i
], 1 + isl_basic_map_total_dim(bmap
));
4493 isl_int_set_si(bmap
->ineq
[i
][1+nparam
+pos
], -1);
4494 isl_int_set_si(bmap
->ineq
[i
][1+nparam
+n_in
+pos
], 1);
4495 return isl_basic_map_finalize(bmap
);
4497 isl_basic_map_free(bmap
);
4501 /* Add a constraint to "bmap" expressing i_pos > o_pos
4503 static struct isl_basic_map
*var_more(struct isl_basic_map
*bmap
, unsigned pos
)
4509 i
= isl_basic_map_alloc_inequality(bmap
);
4512 nparam
= isl_basic_map_dim(bmap
, isl_dim_param
);
4513 n_in
= isl_basic_map_dim(bmap
, isl_dim_in
);
4514 isl_seq_clr(bmap
->ineq
[i
], 1 + isl_basic_map_total_dim(bmap
));
4515 isl_int_set_si(bmap
->ineq
[i
][0], -1);
4516 isl_int_set_si(bmap
->ineq
[i
][1+nparam
+pos
], 1);
4517 isl_int_set_si(bmap
->ineq
[i
][1+nparam
+n_in
+pos
], -1);
4518 return isl_basic_map_finalize(bmap
);
4520 isl_basic_map_free(bmap
);
4524 /* Add a constraint to "bmap" expressing i_pos >= o_pos
4526 static __isl_give isl_basic_map
*var_more_or_equal(
4527 __isl_take isl_basic_map
*bmap
, unsigned pos
)
4533 i
= isl_basic_map_alloc_inequality(bmap
);
4536 nparam
= isl_basic_map_dim(bmap
, isl_dim_param
);
4537 n_in
= isl_basic_map_dim(bmap
, isl_dim_in
);
4538 isl_seq_clr(bmap
->ineq
[i
], 1 + isl_basic_map_total_dim(bmap
));
4539 isl_int_set_si(bmap
->ineq
[i
][1+nparam
+pos
], 1);
4540 isl_int_set_si(bmap
->ineq
[i
][1+nparam
+n_in
+pos
], -1);
4541 return isl_basic_map_finalize(bmap
);
4543 isl_basic_map_free(bmap
);
4547 __isl_give isl_basic_map
*isl_basic_map_equal(
4548 __isl_take isl_space
*dim
, unsigned n_equal
)
4551 struct isl_basic_map
*bmap
;
4552 bmap
= isl_basic_map_alloc_space(dim
, 0, n_equal
, 0);
4555 for (i
= 0; i
< n_equal
&& bmap
; ++i
)
4556 bmap
= var_equal(bmap
, i
);
4557 return isl_basic_map_finalize(bmap
);
4560 /* Return a relation on of dimension "dim" expressing i_[0..pos] << o_[0..pos]
4562 __isl_give isl_basic_map
*isl_basic_map_less_at(__isl_take isl_space
*dim
,
4566 struct isl_basic_map
*bmap
;
4567 bmap
= isl_basic_map_alloc_space(dim
, 0, pos
, 1);
4570 for (i
= 0; i
< pos
&& bmap
; ++i
)
4571 bmap
= var_equal(bmap
, i
);
4573 bmap
= var_less(bmap
, pos
);
4574 return isl_basic_map_finalize(bmap
);
4577 /* Return a relation on "dim" expressing i_[0..pos] <<= o_[0..pos]
4579 __isl_give isl_basic_map
*isl_basic_map_less_or_equal_at(
4580 __isl_take isl_space
*dim
, unsigned pos
)
4583 isl_basic_map
*bmap
;
4585 bmap
= isl_basic_map_alloc_space(dim
, 0, pos
, 1);
4586 for (i
= 0; i
< pos
; ++i
)
4587 bmap
= var_equal(bmap
, i
);
4588 bmap
= var_less_or_equal(bmap
, pos
);
4589 return isl_basic_map_finalize(bmap
);
4592 /* Return a relation on "dim" expressing i_pos > o_pos
4594 __isl_give isl_basic_map
*isl_basic_map_more_at(__isl_take isl_space
*dim
,
4598 struct isl_basic_map
*bmap
;
4599 bmap
= isl_basic_map_alloc_space(dim
, 0, pos
, 1);
4602 for (i
= 0; i
< pos
&& bmap
; ++i
)
4603 bmap
= var_equal(bmap
, i
);
4605 bmap
= var_more(bmap
, pos
);
4606 return isl_basic_map_finalize(bmap
);
4609 /* Return a relation on "dim" expressing i_[0..pos] >>= o_[0..pos]
4611 __isl_give isl_basic_map
*isl_basic_map_more_or_equal_at(
4612 __isl_take isl_space
*dim
, unsigned pos
)
4615 isl_basic_map
*bmap
;
4617 bmap
= isl_basic_map_alloc_space(dim
, 0, pos
, 1);
4618 for (i
= 0; i
< pos
; ++i
)
4619 bmap
= var_equal(bmap
, i
);
4620 bmap
= var_more_or_equal(bmap
, pos
);
4621 return isl_basic_map_finalize(bmap
);
4624 static __isl_give isl_map
*map_lex_lte_first(__isl_take isl_space
*dims
,
4625 unsigned n
, int equal
)
4627 struct isl_map
*map
;
4630 if (n
== 0 && equal
)
4631 return isl_map_universe(dims
);
4633 map
= isl_map_alloc_space(isl_space_copy(dims
), n
, ISL_MAP_DISJOINT
);
4635 for (i
= 0; i
+ 1 < n
; ++i
)
4636 map
= isl_map_add_basic_map(map
,
4637 isl_basic_map_less_at(isl_space_copy(dims
), i
));
4640 map
= isl_map_add_basic_map(map
,
4641 isl_basic_map_less_or_equal_at(dims
, n
- 1));
4643 map
= isl_map_add_basic_map(map
,
4644 isl_basic_map_less_at(dims
, n
- 1));
4646 isl_space_free(dims
);
4651 static __isl_give isl_map
*map_lex_lte(__isl_take isl_space
*dims
, int equal
)
4655 return map_lex_lte_first(dims
, dims
->n_out
, equal
);
4658 __isl_give isl_map
*isl_map_lex_lt_first(__isl_take isl_space
*dim
, unsigned n
)
4660 return map_lex_lte_first(dim
, n
, 0);
4663 __isl_give isl_map
*isl_map_lex_le_first(__isl_take isl_space
*dim
, unsigned n
)
4665 return map_lex_lte_first(dim
, n
, 1);
4668 __isl_give isl_map
*isl_map_lex_lt(__isl_take isl_space
*set_dim
)
4670 return map_lex_lte(isl_space_map_from_set(set_dim
), 0);
4673 __isl_give isl_map
*isl_map_lex_le(__isl_take isl_space
*set_dim
)
4675 return map_lex_lte(isl_space_map_from_set(set_dim
), 1);
4678 static __isl_give isl_map
*map_lex_gte_first(__isl_take isl_space
*dims
,
4679 unsigned n
, int equal
)
4681 struct isl_map
*map
;
4684 if (n
== 0 && equal
)
4685 return isl_map_universe(dims
);
4687 map
= isl_map_alloc_space(isl_space_copy(dims
), n
, ISL_MAP_DISJOINT
);
4689 for (i
= 0; i
+ 1 < n
; ++i
)
4690 map
= isl_map_add_basic_map(map
,
4691 isl_basic_map_more_at(isl_space_copy(dims
), i
));
4694 map
= isl_map_add_basic_map(map
,
4695 isl_basic_map_more_or_equal_at(dims
, n
- 1));
4697 map
= isl_map_add_basic_map(map
,
4698 isl_basic_map_more_at(dims
, n
- 1));
4700 isl_space_free(dims
);
4705 static __isl_give isl_map
*map_lex_gte(__isl_take isl_space
*dims
, int equal
)
4709 return map_lex_gte_first(dims
, dims
->n_out
, equal
);
4712 __isl_give isl_map
*isl_map_lex_gt_first(__isl_take isl_space
*dim
, unsigned n
)
4714 return map_lex_gte_first(dim
, n
, 0);
4717 __isl_give isl_map
*isl_map_lex_ge_first(__isl_take isl_space
*dim
, unsigned n
)
4719 return map_lex_gte_first(dim
, n
, 1);
4722 __isl_give isl_map
*isl_map_lex_gt(__isl_take isl_space
*set_dim
)
4724 return map_lex_gte(isl_space_map_from_set(set_dim
), 0);
4727 __isl_give isl_map
*isl_map_lex_ge(__isl_take isl_space
*set_dim
)
4729 return map_lex_gte(isl_space_map_from_set(set_dim
), 1);
4732 __isl_give isl_map
*isl_set_lex_le_set(__isl_take isl_set
*set1
,
4733 __isl_take isl_set
*set2
)
4736 map
= isl_map_lex_le(isl_set_get_space(set1
));
4737 map
= isl_map_intersect_domain(map
, set1
);
4738 map
= isl_map_intersect_range(map
, set2
);
4742 __isl_give isl_map
*isl_set_lex_lt_set(__isl_take isl_set
*set1
,
4743 __isl_take isl_set
*set2
)
4746 map
= isl_map_lex_lt(isl_set_get_space(set1
));
4747 map
= isl_map_intersect_domain(map
, set1
);
4748 map
= isl_map_intersect_range(map
, set2
);
4752 __isl_give isl_map
*isl_set_lex_ge_set(__isl_take isl_set
*set1
,
4753 __isl_take isl_set
*set2
)
4756 map
= isl_map_lex_ge(isl_set_get_space(set1
));
4757 map
= isl_map_intersect_domain(map
, set1
);
4758 map
= isl_map_intersect_range(map
, set2
);
4762 __isl_give isl_map
*isl_set_lex_gt_set(__isl_take isl_set
*set1
,
4763 __isl_take isl_set
*set2
)
4766 map
= isl_map_lex_gt(isl_set_get_space(set1
));
4767 map
= isl_map_intersect_domain(map
, set1
);
4768 map
= isl_map_intersect_range(map
, set2
);
4772 __isl_give isl_map
*isl_map_lex_le_map(__isl_take isl_map
*map1
,
4773 __isl_take isl_map
*map2
)
4776 map
= isl_map_lex_le(isl_space_range(isl_map_get_space(map1
)));
4777 map
= isl_map_apply_domain(map
, isl_map_reverse(map1
));
4778 map
= isl_map_apply_range(map
, isl_map_reverse(map2
));
4782 __isl_give isl_map
*isl_map_lex_lt_map(__isl_take isl_map
*map1
,
4783 __isl_take isl_map
*map2
)
4786 map
= isl_map_lex_lt(isl_space_range(isl_map_get_space(map1
)));
4787 map
= isl_map_apply_domain(map
, isl_map_reverse(map1
));
4788 map
= isl_map_apply_range(map
, isl_map_reverse(map2
));
4792 __isl_give isl_map
*isl_map_lex_ge_map(__isl_take isl_map
*map1
,
4793 __isl_take isl_map
*map2
)
4796 map
= isl_map_lex_ge(isl_space_range(isl_map_get_space(map1
)));
4797 map
= isl_map_apply_domain(map
, isl_map_reverse(map1
));
4798 map
= isl_map_apply_range(map
, isl_map_reverse(map2
));
4802 __isl_give isl_map
*isl_map_lex_gt_map(__isl_take isl_map
*map1
,
4803 __isl_take isl_map
*map2
)
4806 map
= isl_map_lex_gt(isl_space_range(isl_map_get_space(map1
)));
4807 map
= isl_map_apply_domain(map
, isl_map_reverse(map1
));
4808 map
= isl_map_apply_range(map
, isl_map_reverse(map2
));
4812 /* For a div d = floor(f/m), add the constraint
4816 static isl_stat
add_upper_div_constraint(__isl_keep isl_basic_map
*bmap
,
4817 unsigned pos
, isl_int
*div
)
4820 unsigned total
= isl_basic_map_total_dim(bmap
);
4822 i
= isl_basic_map_alloc_inequality(bmap
);
4824 return isl_stat_error
;
4825 isl_seq_cpy(bmap
->ineq
[i
], div
+ 1, 1 + total
);
4826 isl_int_neg(bmap
->ineq
[i
][1 + pos
], div
[0]);
4831 /* For a div d = floor(f/m), add the constraint
4833 * -(f-(m-1)) + m d >= 0
4835 static isl_stat
add_lower_div_constraint(__isl_keep isl_basic_map
*bmap
,
4836 unsigned pos
, isl_int
*div
)
4839 unsigned total
= isl_basic_map_total_dim(bmap
);
4841 i
= isl_basic_map_alloc_inequality(bmap
);
4843 return isl_stat_error
;
4844 isl_seq_neg(bmap
->ineq
[i
], div
+ 1, 1 + total
);
4845 isl_int_set(bmap
->ineq
[i
][1 + pos
], div
[0]);
4846 isl_int_add(bmap
->ineq
[i
][0], bmap
->ineq
[i
][0], bmap
->ineq
[i
][1 + pos
]);
4847 isl_int_sub_ui(bmap
->ineq
[i
][0], bmap
->ineq
[i
][0], 1);
4852 /* For a div d = floor(f/m), add the constraints
4855 * -(f-(m-1)) + m d >= 0
4857 * Note that the second constraint is the negation of
4861 int isl_basic_map_add_div_constraints_var(__isl_keep isl_basic_map
*bmap
,
4862 unsigned pos
, isl_int
*div
)
4864 if (add_upper_div_constraint(bmap
, pos
, div
) < 0)
4866 if (add_lower_div_constraint(bmap
, pos
, div
) < 0)
4871 int isl_basic_set_add_div_constraints_var(__isl_keep isl_basic_set
*bset
,
4872 unsigned pos
, isl_int
*div
)
4874 return isl_basic_map_add_div_constraints_var(bset_to_bmap(bset
),
4878 int isl_basic_map_add_div_constraints(struct isl_basic_map
*bmap
, unsigned div
)
4880 unsigned total
= isl_basic_map_total_dim(bmap
);
4881 unsigned div_pos
= total
- bmap
->n_div
+ div
;
4883 return isl_basic_map_add_div_constraints_var(bmap
, div_pos
,
4887 /* For each known div d = floor(f/m), add the constraints
4890 * -(f-(m-1)) + m d >= 0
4892 * Remove duplicate constraints in case of some these div constraints
4893 * already appear in "bmap".
4895 __isl_give isl_basic_map
*isl_basic_map_add_known_div_constraints(
4896 __isl_take isl_basic_map
*bmap
)
4902 n_div
= isl_basic_map_dim(bmap
, isl_dim_div
);
4906 bmap
= add_known_div_constraints(bmap
);
4907 bmap
= isl_basic_map_remove_duplicate_constraints(bmap
, NULL
, 0);
4908 bmap
= isl_basic_map_finalize(bmap
);
4912 /* Add the div constraint of sign "sign" for div "div" of "bmap".
4914 * In particular, if this div is of the form d = floor(f/m),
4915 * then add the constraint
4919 * if sign < 0 or the constraint
4921 * -(f-(m-1)) + m d >= 0
4925 int isl_basic_map_add_div_constraint(__isl_keep isl_basic_map
*bmap
,
4926 unsigned div
, int sign
)
4934 total
= isl_basic_map_total_dim(bmap
);
4935 div_pos
= total
- bmap
->n_div
+ div
;
4938 return add_upper_div_constraint(bmap
, div_pos
, bmap
->div
[div
]);
4940 return add_lower_div_constraint(bmap
, div_pos
, bmap
->div
[div
]);
4943 struct isl_basic_set
*isl_basic_map_underlying_set(
4944 struct isl_basic_map
*bmap
)
4948 if (bmap
->dim
->nparam
== 0 && bmap
->dim
->n_in
== 0 &&
4950 !isl_space_is_named_or_nested(bmap
->dim
, isl_dim_in
) &&
4951 !isl_space_is_named_or_nested(bmap
->dim
, isl_dim_out
))
4952 return bset_from_bmap(bmap
);
4953 bmap
= isl_basic_map_cow(bmap
);
4956 bmap
->dim
= isl_space_underlying(bmap
->dim
, bmap
->n_div
);
4959 bmap
->extra
-= bmap
->n_div
;
4961 bmap
= isl_basic_map_finalize(bmap
);
4962 return bset_from_bmap(bmap
);
4964 isl_basic_map_free(bmap
);
4968 __isl_give isl_basic_set
*isl_basic_set_underlying_set(
4969 __isl_take isl_basic_set
*bset
)
4971 return isl_basic_map_underlying_set(bset_to_bmap(bset
));
4974 /* Replace each element in "list" by the result of applying
4975 * isl_basic_map_underlying_set to the element.
4977 __isl_give isl_basic_set_list
*isl_basic_map_list_underlying_set(
4978 __isl_take isl_basic_map_list
*list
)
4985 n
= isl_basic_map_list_n_basic_map(list
);
4986 for (i
= 0; i
< n
; ++i
) {
4987 isl_basic_map
*bmap
;
4988 isl_basic_set
*bset
;
4990 bmap
= isl_basic_map_list_get_basic_map(list
, i
);
4991 bset
= isl_basic_set_underlying_set(bmap
);
4992 list
= isl_basic_set_list_set_basic_set(list
, i
, bset
);
4998 struct isl_basic_map
*isl_basic_map_overlying_set(
4999 struct isl_basic_set
*bset
, struct isl_basic_map
*like
)
5001 struct isl_basic_map
*bmap
;
5002 struct isl_ctx
*ctx
;
5009 isl_assert(ctx
, bset
->n_div
== 0, goto error
);
5010 isl_assert(ctx
, isl_basic_set_n_param(bset
) == 0, goto error
);
5011 isl_assert(ctx
, bset
->dim
->n_out
== isl_basic_map_total_dim(like
),
5013 if (like
->n_div
== 0) {
5014 isl_space
*space
= isl_basic_map_get_space(like
);
5015 isl_basic_map_free(like
);
5016 return isl_basic_map_reset_space(bset
, space
);
5018 bset
= isl_basic_set_cow(bset
);
5021 total
= bset
->dim
->n_out
+ bset
->extra
;
5022 bmap
= bset_to_bmap(bset
);
5023 isl_space_free(bmap
->dim
);
5024 bmap
->dim
= isl_space_copy(like
->dim
);
5027 bmap
->n_div
= like
->n_div
;
5028 bmap
->extra
+= like
->n_div
;
5032 ltotal
= total
- bmap
->extra
+ like
->extra
;
5035 bmap
->block2
= isl_blk_extend(ctx
, bmap
->block2
,
5036 bmap
->extra
* (1 + 1 + total
));
5037 if (isl_blk_is_error(bmap
->block2
))
5039 div
= isl_realloc_array(ctx
, bmap
->div
, isl_int
*, bmap
->extra
);
5043 for (i
= 0; i
< bmap
->extra
; ++i
)
5044 bmap
->div
[i
] = bmap
->block2
.data
+ i
* (1 + 1 + total
);
5045 for (i
= 0; i
< like
->n_div
; ++i
) {
5046 isl_seq_cpy(bmap
->div
[i
], like
->div
[i
], 1 + 1 + ltotal
);
5047 isl_seq_clr(bmap
->div
[i
]+1+1+ltotal
, total
- ltotal
);
5049 bmap
= isl_basic_map_add_known_div_constraints(bmap
);
5051 isl_basic_map_free(like
);
5052 bmap
= isl_basic_map_simplify(bmap
);
5053 bmap
= isl_basic_map_finalize(bmap
);
5056 isl_basic_map_free(like
);
5057 isl_basic_set_free(bset
);
5061 struct isl_basic_set
*isl_basic_set_from_underlying_set(
5062 struct isl_basic_set
*bset
, struct isl_basic_set
*like
)
5064 return bset_from_bmap(isl_basic_map_overlying_set(bset
,
5065 bset_to_bmap(like
)));
5068 struct isl_set
*isl_map_underlying_set(struct isl_map
*map
)
5072 map
= isl_map_cow(map
);
5075 map
->dim
= isl_space_cow(map
->dim
);
5079 for (i
= 1; i
< map
->n
; ++i
)
5080 isl_assert(map
->ctx
, map
->p
[0]->n_div
== map
->p
[i
]->n_div
,
5082 for (i
= 0; i
< map
->n
; ++i
) {
5083 map
->p
[i
] = bset_to_bmap(
5084 isl_basic_map_underlying_set(map
->p
[i
]));
5089 map
->dim
= isl_space_underlying(map
->dim
, 0);
5091 isl_space_free(map
->dim
);
5092 map
->dim
= isl_space_copy(map
->p
[0]->dim
);
5096 return set_from_map(map
);
5102 /* Replace the space of "bmap" by "space".
5104 * If the space of "bmap" is identical to "space" (including the identifiers
5105 * of the input and output dimensions), then simply return the original input.
5107 __isl_give isl_basic_map
*isl_basic_map_reset_space(
5108 __isl_take isl_basic_map
*bmap
, __isl_take isl_space
*space
)
5111 isl_space
*bmap_space
;
5113 bmap_space
= isl_basic_map_peek_space(bmap
);
5114 equal
= isl_space_is_equal(bmap_space
, space
);
5115 if (equal
>= 0 && equal
)
5116 equal
= isl_space_has_equal_ids(bmap_space
, space
);
5120 isl_space_free(space
);
5123 bmap
= isl_basic_map_cow(bmap
);
5124 if (!bmap
|| !space
)
5127 isl_space_free(bmap
->dim
);
5130 bmap
= isl_basic_map_finalize(bmap
);
5134 isl_basic_map_free(bmap
);
5135 isl_space_free(space
);
5139 __isl_give isl_basic_set
*isl_basic_set_reset_space(
5140 __isl_take isl_basic_set
*bset
, __isl_take isl_space
*dim
)
5142 return bset_from_bmap(isl_basic_map_reset_space(bset_to_bmap(bset
),
5146 __isl_give isl_map
*isl_map_reset_space(__isl_take isl_map
*map
,
5147 __isl_take isl_space
*dim
)
5151 map
= isl_map_cow(map
);
5155 for (i
= 0; i
< map
->n
; ++i
) {
5156 map
->p
[i
] = isl_basic_map_reset_space(map
->p
[i
],
5157 isl_space_copy(dim
));
5161 isl_space_free(map
->dim
);
5167 isl_space_free(dim
);
5171 __isl_give isl_set
*isl_set_reset_space(__isl_take isl_set
*set
,
5172 __isl_take isl_space
*dim
)
5174 return set_from_map(isl_map_reset_space(set_to_map(set
), dim
));
5177 /* Compute the parameter domain of the given basic set.
5179 __isl_give isl_basic_set
*isl_basic_set_params(__isl_take isl_basic_set
*bset
)
5185 is_params
= isl_basic_set_is_params(bset
);
5187 return isl_basic_set_free(bset
);
5191 n
= isl_basic_set_dim(bset
, isl_dim_set
);
5192 bset
= isl_basic_set_project_out(bset
, isl_dim_set
, 0, n
);
5193 space
= isl_basic_set_get_space(bset
);
5194 space
= isl_space_params(space
);
5195 bset
= isl_basic_set_reset_space(bset
, space
);
5199 /* Construct a zero-dimensional basic set with the given parameter domain.
5201 __isl_give isl_basic_set
*isl_basic_set_from_params(
5202 __isl_take isl_basic_set
*bset
)
5205 space
= isl_basic_set_get_space(bset
);
5206 space
= isl_space_set_from_params(space
);
5207 bset
= isl_basic_set_reset_space(bset
, space
);
5211 /* Compute the parameter domain of the given set.
5213 __isl_give isl_set
*isl_set_params(__isl_take isl_set
*set
)
5218 if (isl_set_is_params(set
))
5221 n
= isl_set_dim(set
, isl_dim_set
);
5222 set
= isl_set_project_out(set
, isl_dim_set
, 0, n
);
5223 space
= isl_set_get_space(set
);
5224 space
= isl_space_params(space
);
5225 set
= isl_set_reset_space(set
, space
);
5229 /* Construct a zero-dimensional set with the given parameter domain.
5231 __isl_give isl_set
*isl_set_from_params(__isl_take isl_set
*set
)
5234 space
= isl_set_get_space(set
);
5235 space
= isl_space_set_from_params(space
);
5236 set
= isl_set_reset_space(set
, space
);
5240 /* Compute the parameter domain of the given map.
5242 __isl_give isl_set
*isl_map_params(__isl_take isl_map
*map
)
5247 n
= isl_map_dim(map
, isl_dim_in
);
5248 map
= isl_map_project_out(map
, isl_dim_in
, 0, n
);
5249 n
= isl_map_dim(map
, isl_dim_out
);
5250 map
= isl_map_project_out(map
, isl_dim_out
, 0, n
);
5251 space
= isl_map_get_space(map
);
5252 space
= isl_space_params(space
);
5253 map
= isl_map_reset_space(map
, space
);
5257 struct isl_basic_set
*isl_basic_map_domain(struct isl_basic_map
*bmap
)
5264 space
= isl_space_domain(isl_basic_map_get_space(bmap
));
5266 n_out
= isl_basic_map_dim(bmap
, isl_dim_out
);
5267 bmap
= isl_basic_map_project_out(bmap
, isl_dim_out
, 0, n_out
);
5269 return isl_basic_map_reset_space(bmap
, space
);
5272 isl_bool
isl_basic_map_may_be_set(__isl_keep isl_basic_map
*bmap
)
5275 return isl_bool_error
;
5276 return isl_space_may_be_set(bmap
->dim
);
5279 /* Is this basic map actually a set?
5280 * Users should never call this function. Outside of isl,
5281 * the type should indicate whether something is a set or a map.
5283 isl_bool
isl_basic_map_is_set(__isl_keep isl_basic_map
*bmap
)
5286 return isl_bool_error
;
5287 return isl_space_is_set(bmap
->dim
);
5290 struct isl_basic_set
*isl_basic_map_range(struct isl_basic_map
*bmap
)
5294 is_set
= isl_basic_map_is_set(bmap
);
5299 return isl_basic_map_domain(isl_basic_map_reverse(bmap
));
5301 isl_basic_map_free(bmap
);
5305 __isl_give isl_basic_map
*isl_basic_map_domain_map(
5306 __isl_take isl_basic_map
*bmap
)
5310 isl_basic_map
*domain
;
5311 int nparam
, n_in
, n_out
;
5313 nparam
= isl_basic_map_dim(bmap
, isl_dim_param
);
5314 n_in
= isl_basic_map_dim(bmap
, isl_dim_in
);
5315 n_out
= isl_basic_map_dim(bmap
, isl_dim_out
);
5317 dim
= isl_space_from_range(isl_space_domain(isl_basic_map_get_space(bmap
)));
5318 domain
= isl_basic_map_universe(dim
);
5320 bmap
= isl_basic_map_from_domain(isl_basic_map_wrap(bmap
));
5321 bmap
= isl_basic_map_apply_range(bmap
, domain
);
5322 bmap
= isl_basic_map_extend_constraints(bmap
, n_in
, 0);
5324 for (i
= 0; i
< n_in
; ++i
)
5325 bmap
= isl_basic_map_equate(bmap
, isl_dim_in
, i
,
5328 bmap
= isl_basic_map_gauss(bmap
, NULL
);
5329 return isl_basic_map_finalize(bmap
);
5332 __isl_give isl_basic_map
*isl_basic_map_range_map(
5333 __isl_take isl_basic_map
*bmap
)
5337 isl_basic_map
*range
;
5338 int nparam
, n_in
, n_out
;
5340 nparam
= isl_basic_map_dim(bmap
, isl_dim_param
);
5341 n_in
= isl_basic_map_dim(bmap
, isl_dim_in
);
5342 n_out
= isl_basic_map_dim(bmap
, isl_dim_out
);
5344 dim
= isl_space_from_range(isl_space_range(isl_basic_map_get_space(bmap
)));
5345 range
= isl_basic_map_universe(dim
);
5347 bmap
= isl_basic_map_from_domain(isl_basic_map_wrap(bmap
));
5348 bmap
= isl_basic_map_apply_range(bmap
, range
);
5349 bmap
= isl_basic_map_extend_constraints(bmap
, n_out
, 0);
5351 for (i
= 0; i
< n_out
; ++i
)
5352 bmap
= isl_basic_map_equate(bmap
, isl_dim_in
, n_in
+ i
,
5355 bmap
= isl_basic_map_gauss(bmap
, NULL
);
5356 return isl_basic_map_finalize(bmap
);
5359 int isl_map_may_be_set(__isl_keep isl_map
*map
)
5363 return isl_space_may_be_set(map
->dim
);
5366 /* Is this map actually a set?
5367 * Users should never call this function. Outside of isl,
5368 * the type should indicate whether something is a set or a map.
5370 isl_bool
isl_map_is_set(__isl_keep isl_map
*map
)
5373 return isl_bool_error
;
5374 return isl_space_is_set(map
->dim
);
5377 struct isl_set
*isl_map_range(struct isl_map
*map
)
5381 struct isl_set
*set
;
5383 is_set
= isl_map_is_set(map
);
5387 return set_from_map(map
);
5389 map
= isl_map_cow(map
);
5393 set
= set_from_map(map
);
5394 set
->dim
= isl_space_range(set
->dim
);
5397 for (i
= 0; i
< map
->n
; ++i
) {
5398 set
->p
[i
] = isl_basic_map_range(map
->p
[i
]);
5402 ISL_F_CLR(set
, ISL_MAP_DISJOINT
);
5403 ISL_F_CLR(set
, ISL_SET_NORMALIZED
);
5410 __isl_give isl_map
*isl_map_domain_map(__isl_take isl_map
*map
)
5414 map
= isl_map_cow(map
);
5418 map
->dim
= isl_space_domain_map(map
->dim
);
5421 for (i
= 0; i
< map
->n
; ++i
) {
5422 map
->p
[i
] = isl_basic_map_domain_map(map
->p
[i
]);
5426 ISL_F_CLR(map
, ISL_MAP_DISJOINT
);
5427 ISL_F_CLR(map
, ISL_MAP_NORMALIZED
);
5434 __isl_give isl_map
*isl_map_range_map(__isl_take isl_map
*map
)
5437 isl_space
*range_dim
;
5439 map
= isl_map_cow(map
);
5443 range_dim
= isl_space_range(isl_map_get_space(map
));
5444 range_dim
= isl_space_from_range(range_dim
);
5445 map
->dim
= isl_space_from_domain(isl_space_wrap(map
->dim
));
5446 map
->dim
= isl_space_join(map
->dim
, range_dim
);
5449 for (i
= 0; i
< map
->n
; ++i
) {
5450 map
->p
[i
] = isl_basic_map_range_map(map
->p
[i
]);
5454 ISL_F_CLR(map
, ISL_MAP_DISJOINT
);
5455 ISL_F_CLR(map
, ISL_MAP_NORMALIZED
);
5462 /* Given a wrapped map of the form A[B -> C],
5463 * return the map A[B -> C] -> B.
5465 __isl_give isl_map
*isl_set_wrapped_domain_map(__isl_take isl_set
*set
)
5472 if (!isl_set_has_tuple_id(set
))
5473 return isl_map_domain_map(isl_set_unwrap(set
));
5475 id
= isl_set_get_tuple_id(set
);
5476 map
= isl_map_domain_map(isl_set_unwrap(set
));
5477 map
= isl_map_set_tuple_id(map
, isl_dim_in
, id
);
5482 __isl_give isl_basic_map
*isl_basic_map_from_domain(
5483 __isl_take isl_basic_set
*bset
)
5485 return isl_basic_map_reverse(isl_basic_map_from_range(bset
));
5488 __isl_give isl_basic_map
*isl_basic_map_from_range(
5489 __isl_take isl_basic_set
*bset
)
5492 space
= isl_basic_set_get_space(bset
);
5493 space
= isl_space_from_range(space
);
5494 bset
= isl_basic_set_reset_space(bset
, space
);
5495 return bset_to_bmap(bset
);
5498 /* Create a relation with the given set as range.
5499 * The domain of the created relation is a zero-dimensional
5500 * flat anonymous space.
5502 __isl_give isl_map
*isl_map_from_range(__isl_take isl_set
*set
)
5505 space
= isl_set_get_space(set
);
5506 space
= isl_space_from_range(space
);
5507 set
= isl_set_reset_space(set
, space
);
5508 return set_to_map(set
);
5511 /* Create a relation with the given set as domain.
5512 * The range of the created relation is a zero-dimensional
5513 * flat anonymous space.
5515 __isl_give isl_map
*isl_map_from_domain(__isl_take isl_set
*set
)
5517 return isl_map_reverse(isl_map_from_range(set
));
5520 __isl_give isl_basic_map
*isl_basic_map_from_domain_and_range(
5521 __isl_take isl_basic_set
*domain
, __isl_take isl_basic_set
*range
)
5523 return isl_basic_map_apply_range(isl_basic_map_reverse(domain
), range
);
5526 __isl_give isl_map
*isl_map_from_domain_and_range(__isl_take isl_set
*domain
,
5527 __isl_take isl_set
*range
)
5529 return isl_map_apply_range(isl_map_reverse(domain
), range
);
5532 /* Return a newly allocated isl_map with given space and flags and
5533 * room for "n" basic maps.
5534 * Make sure that all cached information is cleared.
5536 __isl_give isl_map
*isl_map_alloc_space(__isl_take isl_space
*space
, int n
,
5539 struct isl_map
*map
;
5544 isl_die(space
->ctx
, isl_error_internal
,
5545 "negative number of basic maps", goto error
);
5546 map
= isl_calloc(space
->ctx
, struct isl_map
,
5547 sizeof(struct isl_map
) +
5548 (n
- 1) * sizeof(struct isl_basic_map
*));
5552 map
->ctx
= space
->ctx
;
5553 isl_ctx_ref(map
->ctx
);
5561 isl_space_free(space
);
5565 __isl_give isl_basic_map
*isl_basic_map_empty(__isl_take isl_space
*dim
)
5567 struct isl_basic_map
*bmap
;
5568 bmap
= isl_basic_map_alloc_space(dim
, 0, 1, 0);
5569 bmap
= isl_basic_map_set_to_empty(bmap
);
5573 __isl_give isl_basic_set
*isl_basic_set_empty(__isl_take isl_space
*dim
)
5575 struct isl_basic_set
*bset
;
5576 bset
= isl_basic_set_alloc_space(dim
, 0, 1, 0);
5577 bset
= isl_basic_set_set_to_empty(bset
);
5581 __isl_give isl_basic_map
*isl_basic_map_universe(__isl_take isl_space
*dim
)
5583 struct isl_basic_map
*bmap
;
5584 bmap
= isl_basic_map_alloc_space(dim
, 0, 0, 0);
5585 bmap
= isl_basic_map_finalize(bmap
);
5589 __isl_give isl_basic_set
*isl_basic_set_universe(__isl_take isl_space
*dim
)
5591 struct isl_basic_set
*bset
;
5592 bset
= isl_basic_set_alloc_space(dim
, 0, 0, 0);
5593 bset
= isl_basic_set_finalize(bset
);
5597 __isl_give isl_basic_map
*isl_basic_map_nat_universe(__isl_take isl_space
*dim
)
5600 unsigned total
= isl_space_dim(dim
, isl_dim_all
);
5601 isl_basic_map
*bmap
;
5603 bmap
= isl_basic_map_alloc_space(dim
, 0, 0, total
);
5604 for (i
= 0; i
< total
; ++i
) {
5605 int k
= isl_basic_map_alloc_inequality(bmap
);
5608 isl_seq_clr(bmap
->ineq
[k
], 1 + total
);
5609 isl_int_set_si(bmap
->ineq
[k
][1 + i
], 1);
5613 isl_basic_map_free(bmap
);
5617 __isl_give isl_basic_set
*isl_basic_set_nat_universe(__isl_take isl_space
*dim
)
5619 return isl_basic_map_nat_universe(dim
);
5622 __isl_give isl_map
*isl_map_nat_universe(__isl_take isl_space
*dim
)
5624 return isl_map_from_basic_map(isl_basic_map_nat_universe(dim
));
5627 __isl_give isl_set
*isl_set_nat_universe(__isl_take isl_space
*dim
)
5629 return isl_map_nat_universe(dim
);
5632 __isl_give isl_map
*isl_map_empty(__isl_take isl_space
*dim
)
5634 return isl_map_alloc_space(dim
, 0, ISL_MAP_DISJOINT
);
5637 __isl_give isl_set
*isl_set_empty(__isl_take isl_space
*dim
)
5639 return isl_set_alloc_space(dim
, 0, ISL_MAP_DISJOINT
);
5642 __isl_give isl_map
*isl_map_universe(__isl_take isl_space
*dim
)
5644 struct isl_map
*map
;
5647 map
= isl_map_alloc_space(isl_space_copy(dim
), 1, ISL_MAP_DISJOINT
);
5648 map
= isl_map_add_basic_map(map
, isl_basic_map_universe(dim
));
5652 __isl_give isl_set
*isl_set_universe(__isl_take isl_space
*dim
)
5654 struct isl_set
*set
;
5657 set
= isl_set_alloc_space(isl_space_copy(dim
), 1, ISL_MAP_DISJOINT
);
5658 set
= isl_set_add_basic_set(set
, isl_basic_set_universe(dim
));
5662 struct isl_map
*isl_map_dup(struct isl_map
*map
)
5665 struct isl_map
*dup
;
5669 dup
= isl_map_alloc_space(isl_space_copy(map
->dim
), map
->n
, map
->flags
);
5670 for (i
= 0; i
< map
->n
; ++i
)
5671 dup
= isl_map_add_basic_map(dup
, isl_basic_map_copy(map
->p
[i
]));
5675 __isl_give isl_map
*isl_map_add_basic_map(__isl_take isl_map
*map
,
5676 __isl_take isl_basic_map
*bmap
)
5680 if (isl_basic_map_plain_is_empty(bmap
)) {
5681 isl_basic_map_free(bmap
);
5684 isl_assert(map
->ctx
, isl_space_is_equal(map
->dim
, bmap
->dim
), goto error
);
5685 isl_assert(map
->ctx
, map
->n
< map
->size
, goto error
);
5686 map
->p
[map
->n
] = bmap
;
5688 ISL_F_CLR(map
, ISL_MAP_NORMALIZED
);
5694 isl_basic_map_free(bmap
);
5698 __isl_null isl_map
*isl_map_free(__isl_take isl_map
*map
)
5709 isl_ctx_deref(map
->ctx
);
5710 for (i
= 0; i
< map
->n
; ++i
)
5711 isl_basic_map_free(map
->p
[i
]);
5712 isl_space_free(map
->dim
);
5718 static struct isl_basic_map
*isl_basic_map_fix_pos_si(
5719 struct isl_basic_map
*bmap
, unsigned pos
, int value
)
5723 bmap
= isl_basic_map_cow(bmap
);
5724 bmap
= isl_basic_map_extend_constraints(bmap
, 1, 0);
5725 j
= isl_basic_map_alloc_equality(bmap
);
5728 isl_seq_clr(bmap
->eq
[j
] + 1, isl_basic_map_total_dim(bmap
));
5729 isl_int_set_si(bmap
->eq
[j
][pos
], -1);
5730 isl_int_set_si(bmap
->eq
[j
][0], value
);
5731 bmap
= isl_basic_map_simplify(bmap
);
5732 return isl_basic_map_finalize(bmap
);
5734 isl_basic_map_free(bmap
);
5738 static __isl_give isl_basic_map
*isl_basic_map_fix_pos(
5739 __isl_take isl_basic_map
*bmap
, unsigned pos
, isl_int value
)
5743 bmap
= isl_basic_map_cow(bmap
);
5744 bmap
= isl_basic_map_extend_constraints(bmap
, 1, 0);
5745 j
= isl_basic_map_alloc_equality(bmap
);
5748 isl_seq_clr(bmap
->eq
[j
] + 1, isl_basic_map_total_dim(bmap
));
5749 isl_int_set_si(bmap
->eq
[j
][pos
], -1);
5750 isl_int_set(bmap
->eq
[j
][0], value
);
5751 bmap
= isl_basic_map_simplify(bmap
);
5752 return isl_basic_map_finalize(bmap
);
5754 isl_basic_map_free(bmap
);
5758 struct isl_basic_map
*isl_basic_map_fix_si(struct isl_basic_map
*bmap
,
5759 enum isl_dim_type type
, unsigned pos
, int value
)
5761 if (isl_basic_map_check_range(bmap
, type
, pos
, 1) < 0)
5762 return isl_basic_map_free(bmap
);
5763 return isl_basic_map_fix_pos_si(bmap
,
5764 isl_basic_map_offset(bmap
, type
) + pos
, value
);
5767 __isl_give isl_basic_map
*isl_basic_map_fix(__isl_take isl_basic_map
*bmap
,
5768 enum isl_dim_type type
, unsigned pos
, isl_int value
)
5770 if (isl_basic_map_check_range(bmap
, type
, pos
, 1) < 0)
5771 return isl_basic_map_free(bmap
);
5772 return isl_basic_map_fix_pos(bmap
,
5773 isl_basic_map_offset(bmap
, type
) + pos
, value
);
5776 /* Fix the value of the variable at position "pos" of type "type" of "bmap"
5777 * to be equal to "v".
5779 __isl_give isl_basic_map
*isl_basic_map_fix_val(__isl_take isl_basic_map
*bmap
,
5780 enum isl_dim_type type
, unsigned pos
, __isl_take isl_val
*v
)
5784 if (!isl_val_is_int(v
))
5785 isl_die(isl_basic_map_get_ctx(bmap
), isl_error_invalid
,
5786 "expecting integer value", goto error
);
5787 if (isl_basic_map_check_range(bmap
, type
, pos
, 1) < 0)
5789 pos
+= isl_basic_map_offset(bmap
, type
);
5790 bmap
= isl_basic_map_fix_pos(bmap
, pos
, v
->n
);
5794 isl_basic_map_free(bmap
);
5799 /* Fix the value of the variable at position "pos" of type "type" of "bset"
5800 * to be equal to "v".
5802 __isl_give isl_basic_set
*isl_basic_set_fix_val(__isl_take isl_basic_set
*bset
,
5803 enum isl_dim_type type
, unsigned pos
, __isl_take isl_val
*v
)
5805 return isl_basic_map_fix_val(bset
, type
, pos
, v
);
5808 struct isl_basic_set
*isl_basic_set_fix_si(struct isl_basic_set
*bset
,
5809 enum isl_dim_type type
, unsigned pos
, int value
)
5811 return bset_from_bmap(isl_basic_map_fix_si(bset_to_bmap(bset
),
5815 __isl_give isl_basic_set
*isl_basic_set_fix(__isl_take isl_basic_set
*bset
,
5816 enum isl_dim_type type
, unsigned pos
, isl_int value
)
5818 return bset_from_bmap(isl_basic_map_fix(bset_to_bmap(bset
),
5822 struct isl_basic_map
*isl_basic_map_fix_input_si(struct isl_basic_map
*bmap
,
5823 unsigned input
, int value
)
5825 return isl_basic_map_fix_si(bmap
, isl_dim_in
, input
, value
);
5828 struct isl_basic_set
*isl_basic_set_fix_dim_si(struct isl_basic_set
*bset
,
5829 unsigned dim
, int value
)
5831 return bset_from_bmap(isl_basic_map_fix_si(bset_to_bmap(bset
),
5832 isl_dim_set
, dim
, value
));
5835 static int remove_if_empty(__isl_keep isl_map
*map
, int i
)
5837 int empty
= isl_basic_map_plain_is_empty(map
->p
[i
]);
5844 isl_basic_map_free(map
->p
[i
]);
5845 if (i
!= map
->n
- 1) {
5846 ISL_F_CLR(map
, ISL_MAP_NORMALIZED
);
5847 map
->p
[i
] = map
->p
[map
->n
- 1];
5854 /* Perform "fn" on each basic map of "map", where we may not be holding
5855 * the only reference to "map".
5856 * In particular, "fn" should be a semantics preserving operation
5857 * that we want to apply to all copies of "map". We therefore need
5858 * to be careful not to modify "map" in a way that breaks "map"
5859 * in case anything goes wrong.
5861 __isl_give isl_map
*isl_map_inline_foreach_basic_map(__isl_take isl_map
*map
,
5862 __isl_give isl_basic_map
*(*fn
)(__isl_take isl_basic_map
*bmap
))
5864 struct isl_basic_map
*bmap
;
5870 for (i
= map
->n
- 1; i
>= 0; --i
) {
5871 bmap
= isl_basic_map_copy(map
->p
[i
]);
5875 isl_basic_map_free(map
->p
[i
]);
5877 if (remove_if_empty(map
, i
) < 0)
5887 struct isl_map
*isl_map_fix_si(struct isl_map
*map
,
5888 enum isl_dim_type type
, unsigned pos
, int value
)
5892 map
= isl_map_cow(map
);
5896 isl_assert(map
->ctx
, pos
< isl_map_dim(map
, type
), goto error
);
5897 for (i
= map
->n
- 1; i
>= 0; --i
) {
5898 map
->p
[i
] = isl_basic_map_fix_si(map
->p
[i
], type
, pos
, value
);
5899 if (remove_if_empty(map
, i
) < 0)
5902 ISL_F_CLR(map
, ISL_MAP_NORMALIZED
);
5909 __isl_give isl_set
*isl_set_fix_si(__isl_take isl_set
*set
,
5910 enum isl_dim_type type
, unsigned pos
, int value
)
5912 return set_from_map(isl_map_fix_si(set_to_map(set
), type
, pos
, value
));
5915 __isl_give isl_map
*isl_map_fix(__isl_take isl_map
*map
,
5916 enum isl_dim_type type
, unsigned pos
, isl_int value
)
5920 map
= isl_map_cow(map
);
5924 isl_assert(map
->ctx
, pos
< isl_map_dim(map
, type
), goto error
);
5925 for (i
= 0; i
< map
->n
; ++i
) {
5926 map
->p
[i
] = isl_basic_map_fix(map
->p
[i
], type
, pos
, value
);
5930 ISL_F_CLR(map
, ISL_MAP_NORMALIZED
);
5937 __isl_give isl_set
*isl_set_fix(__isl_take isl_set
*set
,
5938 enum isl_dim_type type
, unsigned pos
, isl_int value
)
5940 return set_from_map(isl_map_fix(set_to_map(set
), type
, pos
, value
));
5943 /* Fix the value of the variable at position "pos" of type "type" of "map"
5944 * to be equal to "v".
5946 __isl_give isl_map
*isl_map_fix_val(__isl_take isl_map
*map
,
5947 enum isl_dim_type type
, unsigned pos
, __isl_take isl_val
*v
)
5951 map
= isl_map_cow(map
);
5955 if (!isl_val_is_int(v
))
5956 isl_die(isl_map_get_ctx(map
), isl_error_invalid
,
5957 "expecting integer value", goto error
);
5958 if (pos
>= isl_map_dim(map
, type
))
5959 isl_die(isl_map_get_ctx(map
), isl_error_invalid
,
5960 "index out of bounds", goto error
);
5961 for (i
= map
->n
- 1; i
>= 0; --i
) {
5962 map
->p
[i
] = isl_basic_map_fix_val(map
->p
[i
], type
, pos
,
5964 if (remove_if_empty(map
, i
) < 0)
5967 ISL_F_CLR(map
, ISL_MAP_NORMALIZED
);
5976 /* Fix the value of the variable at position "pos" of type "type" of "set"
5977 * to be equal to "v".
5979 __isl_give isl_set
*isl_set_fix_val(__isl_take isl_set
*set
,
5980 enum isl_dim_type type
, unsigned pos
, __isl_take isl_val
*v
)
5982 return isl_map_fix_val(set
, type
, pos
, v
);
5985 struct isl_map
*isl_map_fix_input_si(struct isl_map
*map
,
5986 unsigned input
, int value
)
5988 return isl_map_fix_si(map
, isl_dim_in
, input
, value
);
5991 struct isl_set
*isl_set_fix_dim_si(struct isl_set
*set
, unsigned dim
, int value
)
5993 return set_from_map(isl_map_fix_si(set_to_map(set
),
5994 isl_dim_set
, dim
, value
));
5997 static __isl_give isl_basic_map
*basic_map_bound_si(
5998 __isl_take isl_basic_map
*bmap
,
5999 enum isl_dim_type type
, unsigned pos
, int value
, int upper
)
6003 if (isl_basic_map_check_range(bmap
, type
, pos
, 1) < 0)
6004 return isl_basic_map_free(bmap
);
6005 pos
+= isl_basic_map_offset(bmap
, type
);
6006 bmap
= isl_basic_map_cow(bmap
);
6007 bmap
= isl_basic_map_extend_constraints(bmap
, 0, 1);
6008 j
= isl_basic_map_alloc_inequality(bmap
);
6011 isl_seq_clr(bmap
->ineq
[j
], 1 + isl_basic_map_total_dim(bmap
));
6013 isl_int_set_si(bmap
->ineq
[j
][pos
], -1);
6014 isl_int_set_si(bmap
->ineq
[j
][0], value
);
6016 isl_int_set_si(bmap
->ineq
[j
][pos
], 1);
6017 isl_int_set_si(bmap
->ineq
[j
][0], -value
);
6019 bmap
= isl_basic_map_simplify(bmap
);
6020 return isl_basic_map_finalize(bmap
);
6022 isl_basic_map_free(bmap
);
6026 __isl_give isl_basic_map
*isl_basic_map_lower_bound_si(
6027 __isl_take isl_basic_map
*bmap
,
6028 enum isl_dim_type type
, unsigned pos
, int value
)
6030 return basic_map_bound_si(bmap
, type
, pos
, value
, 0);
6033 /* Constrain the values of the given dimension to be no greater than "value".
6035 __isl_give isl_basic_map
*isl_basic_map_upper_bound_si(
6036 __isl_take isl_basic_map
*bmap
,
6037 enum isl_dim_type type
, unsigned pos
, int value
)
6039 return basic_map_bound_si(bmap
, type
, pos
, value
, 1);
6042 static __isl_give isl_map
*map_bound_si(__isl_take isl_map
*map
,
6043 enum isl_dim_type type
, unsigned pos
, int value
, int upper
)
6047 map
= isl_map_cow(map
);
6051 isl_assert(map
->ctx
, pos
< isl_map_dim(map
, type
), goto error
);
6052 for (i
= 0; i
< map
->n
; ++i
) {
6053 map
->p
[i
] = basic_map_bound_si(map
->p
[i
],
6054 type
, pos
, value
, upper
);
6058 ISL_F_CLR(map
, ISL_MAP_NORMALIZED
);
6065 __isl_give isl_map
*isl_map_lower_bound_si(__isl_take isl_map
*map
,
6066 enum isl_dim_type type
, unsigned pos
, int value
)
6068 return map_bound_si(map
, type
, pos
, value
, 0);
6071 __isl_give isl_map
*isl_map_upper_bound_si(__isl_take isl_map
*map
,
6072 enum isl_dim_type type
, unsigned pos
, int value
)
6074 return map_bound_si(map
, type
, pos
, value
, 1);
6077 __isl_give isl_set
*isl_set_lower_bound_si(__isl_take isl_set
*set
,
6078 enum isl_dim_type type
, unsigned pos
, int value
)
6080 return set_from_map(isl_map_lower_bound_si(set_to_map(set
),
6084 __isl_give isl_set
*isl_set_upper_bound_si(__isl_take isl_set
*set
,
6085 enum isl_dim_type type
, unsigned pos
, int value
)
6087 return isl_map_upper_bound_si(set
, type
, pos
, value
);
6090 /* Bound the given variable of "bmap" from below (or above is "upper"
6091 * is set) to "value".
6093 static __isl_give isl_basic_map
*basic_map_bound(
6094 __isl_take isl_basic_map
*bmap
,
6095 enum isl_dim_type type
, unsigned pos
, isl_int value
, int upper
)
6099 if (isl_basic_map_check_range(bmap
, type
, pos
, 1) < 0)
6100 return isl_basic_map_free(bmap
);
6101 pos
+= isl_basic_map_offset(bmap
, type
);
6102 bmap
= isl_basic_map_cow(bmap
);
6103 bmap
= isl_basic_map_extend_constraints(bmap
, 0, 1);
6104 j
= isl_basic_map_alloc_inequality(bmap
);
6107 isl_seq_clr(bmap
->ineq
[j
], 1 + isl_basic_map_total_dim(bmap
));
6109 isl_int_set_si(bmap
->ineq
[j
][pos
], -1);
6110 isl_int_set(bmap
->ineq
[j
][0], value
);
6112 isl_int_set_si(bmap
->ineq
[j
][pos
], 1);
6113 isl_int_neg(bmap
->ineq
[j
][0], value
);
6115 bmap
= isl_basic_map_simplify(bmap
);
6116 return isl_basic_map_finalize(bmap
);
6118 isl_basic_map_free(bmap
);
6122 /* Bound the given variable of "map" from below (or above is "upper"
6123 * is set) to "value".
6125 static __isl_give isl_map
*map_bound(__isl_take isl_map
*map
,
6126 enum isl_dim_type type
, unsigned pos
, isl_int value
, int upper
)
6130 map
= isl_map_cow(map
);
6134 if (pos
>= isl_map_dim(map
, type
))
6135 isl_die(map
->ctx
, isl_error_invalid
,
6136 "index out of bounds", goto error
);
6137 for (i
= map
->n
- 1; i
>= 0; --i
) {
6138 map
->p
[i
] = basic_map_bound(map
->p
[i
], type
, pos
, value
, upper
);
6139 if (remove_if_empty(map
, i
) < 0)
6142 ISL_F_CLR(map
, ISL_MAP_NORMALIZED
);
6149 __isl_give isl_map
*isl_map_lower_bound(__isl_take isl_map
*map
,
6150 enum isl_dim_type type
, unsigned pos
, isl_int value
)
6152 return map_bound(map
, type
, pos
, value
, 0);
6155 __isl_give isl_map
*isl_map_upper_bound(__isl_take isl_map
*map
,
6156 enum isl_dim_type type
, unsigned pos
, isl_int value
)
6158 return map_bound(map
, type
, pos
, value
, 1);
6161 __isl_give isl_set
*isl_set_lower_bound(__isl_take isl_set
*set
,
6162 enum isl_dim_type type
, unsigned pos
, isl_int value
)
6164 return isl_map_lower_bound(set
, type
, pos
, value
);
6167 __isl_give isl_set
*isl_set_upper_bound(__isl_take isl_set
*set
,
6168 enum isl_dim_type type
, unsigned pos
, isl_int value
)
6170 return isl_map_upper_bound(set
, type
, pos
, value
);
6173 /* Force the values of the variable at position "pos" of type "type" of "set"
6174 * to be no smaller than "value".
6176 __isl_give isl_set
*isl_set_lower_bound_val(__isl_take isl_set
*set
,
6177 enum isl_dim_type type
, unsigned pos
, __isl_take isl_val
*value
)
6181 if (!isl_val_is_int(value
))
6182 isl_die(isl_set_get_ctx(set
), isl_error_invalid
,
6183 "expecting integer value", goto error
);
6184 set
= isl_set_lower_bound(set
, type
, pos
, value
->n
);
6185 isl_val_free(value
);
6188 isl_val_free(value
);
6193 /* Force the values of the variable at position "pos" of type "type" of "set"
6194 * to be no greater than "value".
6196 __isl_give isl_set
*isl_set_upper_bound_val(__isl_take isl_set
*set
,
6197 enum isl_dim_type type
, unsigned pos
, __isl_take isl_val
*value
)
6201 if (!isl_val_is_int(value
))
6202 isl_die(isl_set_get_ctx(set
), isl_error_invalid
,
6203 "expecting integer value", goto error
);
6204 set
= isl_set_upper_bound(set
, type
, pos
, value
->n
);
6205 isl_val_free(value
);
6208 isl_val_free(value
);
6213 struct isl_map
*isl_map_reverse(struct isl_map
*map
)
6217 map
= isl_map_cow(map
);
6221 map
->dim
= isl_space_reverse(map
->dim
);
6224 for (i
= 0; i
< map
->n
; ++i
) {
6225 map
->p
[i
] = isl_basic_map_reverse(map
->p
[i
]);
6229 ISL_F_CLR(map
, ISL_MAP_NORMALIZED
);
6237 #define TYPE isl_pw_multi_aff
6239 #define SUFFIX _pw_multi_aff
6241 #define EMPTY isl_pw_multi_aff_empty
6243 #define ADD isl_pw_multi_aff_union_add
6244 #include "isl_map_lexopt_templ.c"
6246 /* Given a map "map", compute the lexicographically minimal
6247 * (or maximal) image element for each domain element in dom,
6248 * in the form of an isl_pw_multi_aff.
6249 * If "empty" is not NULL, then set *empty to those elements in dom that
6250 * do not have an image element.
6251 * If "flags" includes ISL_OPT_FULL, then "dom" is NULL and the optimum
6252 * should be computed over the domain of "map". "empty" is also NULL
6255 * We first compute the lexicographically minimal or maximal element
6256 * in the first basic map. This results in a partial solution "res"
6257 * and a subset "todo" of dom that still need to be handled.
6258 * We then consider each of the remaining maps in "map" and successively
6259 * update both "res" and "todo".
6260 * If "empty" is NULL, then the todo sets are not needed and therefore
6261 * also not computed.
6263 static __isl_give isl_pw_multi_aff
*isl_map_partial_lexopt_aligned_pw_multi_aff(
6264 __isl_take isl_map
*map
, __isl_take isl_set
*dom
,
6265 __isl_give isl_set
**empty
, unsigned flags
)
6269 isl_pw_multi_aff
*res
;
6272 full
= ISL_FL_ISSET(flags
, ISL_OPT_FULL
);
6273 if (!map
|| (!full
&& !dom
))
6276 if (isl_map_plain_is_empty(map
)) {
6281 return isl_pw_multi_aff_from_map(map
);
6284 res
= basic_map_partial_lexopt_pw_multi_aff(
6285 isl_basic_map_copy(map
->p
[0]),
6286 isl_set_copy(dom
), empty
, flags
);
6290 for (i
= 1; i
< map
->n
; ++i
) {
6291 isl_pw_multi_aff
*res_i
;
6293 res_i
= basic_map_partial_lexopt_pw_multi_aff(
6294 isl_basic_map_copy(map
->p
[i
]),
6295 isl_set_copy(dom
), empty
, flags
);
6297 if (ISL_FL_ISSET(flags
, ISL_OPT_MAX
))
6298 res
= isl_pw_multi_aff_union_lexmax(res
, res_i
);
6300 res
= isl_pw_multi_aff_union_lexmin(res
, res_i
);
6303 todo
= isl_set_intersect(todo
, *empty
);
6322 #define TYPE isl_map
6326 #define EMPTY isl_map_empty
6328 #define ADD isl_map_union_disjoint
6329 #include "isl_map_lexopt_templ.c"
6331 /* Given a map "map", compute the lexicographically minimal
6332 * (or maximal) image element for each domain element in "dom",
6333 * in the form of an isl_map.
6334 * If "empty" is not NULL, then set *empty to those elements in "dom" that
6335 * do not have an image element.
6336 * If "flags" includes ISL_OPT_FULL, then "dom" is NULL and the optimum
6337 * should be computed over the domain of "map". "empty" is also NULL
6340 * If the input consists of more than one disjunct, then first
6341 * compute the desired result in the form of an isl_pw_multi_aff and
6342 * then convert that into an isl_map.
6344 * This function used to have an explicit implementation in terms
6345 * of isl_maps, but it would continually intersect the domains of
6346 * partial results with the complement of the domain of the next
6347 * partial solution, potentially leading to an explosion in the number
6348 * of disjuncts if there are several disjuncts in the input.
6349 * An even earlier implementation of this function would look for
6350 * better results in the domain of the partial result and for extra
6351 * results in the complement of this domain, which would lead to
6352 * even more splintering.
6354 static __isl_give isl_map
*isl_map_partial_lexopt_aligned(
6355 __isl_take isl_map
*map
, __isl_take isl_set
*dom
,
6356 __isl_give isl_set
**empty
, unsigned flags
)
6359 struct isl_map
*res
;
6360 isl_pw_multi_aff
*pma
;
6362 full
= ISL_FL_ISSET(flags
, ISL_OPT_FULL
);
6363 if (!map
|| (!full
&& !dom
))
6366 if (isl_map_plain_is_empty(map
)) {
6375 res
= basic_map_partial_lexopt(isl_basic_map_copy(map
->p
[0]),
6381 pma
= isl_map_partial_lexopt_aligned_pw_multi_aff(map
, dom
, empty
,
6383 return isl_map_from_pw_multi_aff(pma
);
6392 __isl_give isl_map
*isl_map_partial_lexmax(
6393 __isl_take isl_map
*map
, __isl_take isl_set
*dom
,
6394 __isl_give isl_set
**empty
)
6396 return isl_map_partial_lexopt(map
, dom
, empty
, ISL_OPT_MAX
);
6399 __isl_give isl_map
*isl_map_partial_lexmin(
6400 __isl_take isl_map
*map
, __isl_take isl_set
*dom
,
6401 __isl_give isl_set
**empty
)
6403 return isl_map_partial_lexopt(map
, dom
, empty
, 0);
6406 __isl_give isl_set
*isl_set_partial_lexmin(
6407 __isl_take isl_set
*set
, __isl_take isl_set
*dom
,
6408 __isl_give isl_set
**empty
)
6410 return set_from_map(isl_map_partial_lexmin(set_to_map(set
),
6414 __isl_give isl_set
*isl_set_partial_lexmax(
6415 __isl_take isl_set
*set
, __isl_take isl_set
*dom
,
6416 __isl_give isl_set
**empty
)
6418 return set_from_map(isl_map_partial_lexmax(set_to_map(set
),
6422 /* Compute the lexicographic minimum (or maximum if "flags" includes
6423 * ISL_OPT_MAX) of "bset" over its parametric domain.
6425 __isl_give isl_set
*isl_basic_set_lexopt(__isl_take isl_basic_set
*bset
,
6428 return isl_basic_map_lexopt(bset
, flags
);
6431 __isl_give isl_map
*isl_basic_map_lexmax(__isl_take isl_basic_map
*bmap
)
6433 return isl_basic_map_lexopt(bmap
, ISL_OPT_MAX
);
6436 __isl_give isl_set
*isl_basic_set_lexmin(__isl_take isl_basic_set
*bset
)
6438 return set_from_map(isl_basic_map_lexmin(bset_to_bmap(bset
)));
6441 __isl_give isl_set
*isl_basic_set_lexmax(__isl_take isl_basic_set
*bset
)
6443 return set_from_map(isl_basic_map_lexmax(bset_to_bmap(bset
)));
6446 /* Compute the lexicographic minimum of "bset" over its parametric domain
6447 * for the purpose of quantifier elimination.
6448 * That is, find an explicit representation for all the existentially
6449 * quantified variables in "bset" by computing their lexicographic
6452 static __isl_give isl_set
*isl_basic_set_lexmin_compute_divs(
6453 __isl_take isl_basic_set
*bset
)
6455 return isl_basic_set_lexopt(bset
, ISL_OPT_QE
);
6458 /* Extract the first and only affine expression from list
6459 * and then add it to *pwaff with the given dom.
6460 * This domain is known to be disjoint from other domains
6461 * because of the way isl_basic_map_foreach_lexmax works.
6463 static isl_stat
update_dim_opt(__isl_take isl_basic_set
*dom
,
6464 __isl_take isl_aff_list
*list
, void *user
)
6466 isl_ctx
*ctx
= isl_basic_set_get_ctx(dom
);
6468 isl_pw_aff
**pwaff
= user
;
6469 isl_pw_aff
*pwaff_i
;
6473 if (isl_aff_list_n_aff(list
) != 1)
6474 isl_die(ctx
, isl_error_internal
,
6475 "expecting single element list", goto error
);
6477 aff
= isl_aff_list_get_aff(list
, 0);
6478 pwaff_i
= isl_pw_aff_alloc(isl_set_from_basic_set(dom
), aff
);
6480 *pwaff
= isl_pw_aff_add_disjoint(*pwaff
, pwaff_i
);
6482 isl_aff_list_free(list
);
6486 isl_basic_set_free(dom
);
6487 isl_aff_list_free(list
);
6488 return isl_stat_error
;
6491 /* Given a basic map with one output dimension, compute the minimum or
6492 * maximum of that dimension as an isl_pw_aff.
6494 * The isl_pw_aff is constructed by having isl_basic_map_foreach_lexopt
6495 * call update_dim_opt on each leaf of the result.
6497 static __isl_give isl_pw_aff
*basic_map_dim_opt(__isl_keep isl_basic_map
*bmap
,
6500 isl_space
*dim
= isl_basic_map_get_space(bmap
);
6504 dim
= isl_space_from_domain(isl_space_domain(dim
));
6505 dim
= isl_space_add_dims(dim
, isl_dim_out
, 1);
6506 pwaff
= isl_pw_aff_empty(dim
);
6508 r
= isl_basic_map_foreach_lexopt(bmap
, max
, &update_dim_opt
, &pwaff
);
6510 return isl_pw_aff_free(pwaff
);
6515 /* Compute the minimum or maximum of the given output dimension
6516 * as a function of the parameters and the input dimensions,
6517 * but independently of the other output dimensions.
6519 * We first project out the other output dimension and then compute
6520 * the "lexicographic" maximum in each basic map, combining the results
6521 * using isl_pw_aff_union_max.
6523 static __isl_give isl_pw_aff
*map_dim_opt(__isl_take isl_map
*map
, int pos
,
6530 n_out
= isl_map_dim(map
, isl_dim_out
);
6531 map
= isl_map_project_out(map
, isl_dim_out
, pos
+ 1, n_out
- (pos
+ 1));
6532 map
= isl_map_project_out(map
, isl_dim_out
, 0, pos
);
6537 isl_space
*dim
= isl_map_get_space(map
);
6539 return isl_pw_aff_empty(dim
);
6542 pwaff
= basic_map_dim_opt(map
->p
[0], max
);
6543 for (i
= 1; i
< map
->n
; ++i
) {
6544 isl_pw_aff
*pwaff_i
;
6546 pwaff_i
= basic_map_dim_opt(map
->p
[i
], max
);
6547 pwaff
= isl_pw_aff_union_opt(pwaff
, pwaff_i
, max
);
6555 /* Compute the minimum of the given output dimension as a function of the
6556 * parameters and input dimensions, but independently of
6557 * the other output dimensions.
6559 __isl_give isl_pw_aff
*isl_map_dim_min(__isl_take isl_map
*map
, int pos
)
6561 return map_dim_opt(map
, pos
, 0);
6564 /* Compute the maximum of the given output dimension as a function of the
6565 * parameters and input dimensions, but independently of
6566 * the other output dimensions.
6568 __isl_give isl_pw_aff
*isl_map_dim_max(__isl_take isl_map
*map
, int pos
)
6570 return map_dim_opt(map
, pos
, 1);
6573 /* Compute the minimum or maximum of the given set dimension
6574 * as a function of the parameters,
6575 * but independently of the other set dimensions.
6577 static __isl_give isl_pw_aff
*set_dim_opt(__isl_take isl_set
*set
, int pos
,
6580 return map_dim_opt(set
, pos
, max
);
6583 /* Compute the maximum of the given set dimension as a function of the
6584 * parameters, but independently of the other set dimensions.
6586 __isl_give isl_pw_aff
*isl_set_dim_max(__isl_take isl_set
*set
, int pos
)
6588 return set_dim_opt(set
, pos
, 1);
6591 /* Compute the minimum of the given set dimension as a function of the
6592 * parameters, but independently of the other set dimensions.
6594 __isl_give isl_pw_aff
*isl_set_dim_min(__isl_take isl_set
*set
, int pos
)
6596 return set_dim_opt(set
, pos
, 0);
6599 /* Apply a preimage specified by "mat" on the parameters of "bset".
6600 * bset is assumed to have only parameters and divs.
6602 static struct isl_basic_set
*basic_set_parameter_preimage(
6603 struct isl_basic_set
*bset
, struct isl_mat
*mat
)
6610 bset
->dim
= isl_space_cow(bset
->dim
);
6614 nparam
= isl_basic_set_dim(bset
, isl_dim_param
);
6616 isl_assert(bset
->ctx
, mat
->n_row
== 1 + nparam
, goto error
);
6618 bset
->dim
->nparam
= 0;
6619 bset
->dim
->n_out
= nparam
;
6620 bset
= isl_basic_set_preimage(bset
, mat
);
6622 bset
->dim
->nparam
= bset
->dim
->n_out
;
6623 bset
->dim
->n_out
= 0;
6628 isl_basic_set_free(bset
);
6632 /* Apply a preimage specified by "mat" on the parameters of "set".
6633 * set is assumed to have only parameters and divs.
6635 static __isl_give isl_set
*set_parameter_preimage(__isl_take isl_set
*set
,
6636 __isl_take isl_mat
*mat
)
6644 nparam
= isl_set_dim(set
, isl_dim_param
);
6646 if (mat
->n_row
!= 1 + nparam
)
6647 isl_die(isl_set_get_ctx(set
), isl_error_internal
,
6648 "unexpected number of rows", goto error
);
6650 space
= isl_set_get_space(set
);
6651 space
= isl_space_move_dims(space
, isl_dim_set
, 0,
6652 isl_dim_param
, 0, nparam
);
6653 set
= isl_set_reset_space(set
, space
);
6654 set
= isl_set_preimage(set
, mat
);
6655 nparam
= isl_set_dim(set
, isl_dim_out
);
6656 space
= isl_set_get_space(set
);
6657 space
= isl_space_move_dims(space
, isl_dim_param
, 0,
6658 isl_dim_out
, 0, nparam
);
6659 set
= isl_set_reset_space(set
, space
);
6667 /* Intersect the basic set "bset" with the affine space specified by the
6668 * equalities in "eq".
6670 static struct isl_basic_set
*basic_set_append_equalities(
6671 struct isl_basic_set
*bset
, struct isl_mat
*eq
)
6679 bset
= isl_basic_set_extend_space(bset
, isl_space_copy(bset
->dim
), 0,
6684 len
= 1 + isl_space_dim(bset
->dim
, isl_dim_all
) + bset
->extra
;
6685 for (i
= 0; i
< eq
->n_row
; ++i
) {
6686 k
= isl_basic_set_alloc_equality(bset
);
6689 isl_seq_cpy(bset
->eq
[k
], eq
->row
[i
], eq
->n_col
);
6690 isl_seq_clr(bset
->eq
[k
] + eq
->n_col
, len
- eq
->n_col
);
6694 bset
= isl_basic_set_gauss(bset
, NULL
);
6695 bset
= isl_basic_set_finalize(bset
);
6700 isl_basic_set_free(bset
);
6704 /* Intersect the set "set" with the affine space specified by the
6705 * equalities in "eq".
6707 static struct isl_set
*set_append_equalities(struct isl_set
*set
,
6715 for (i
= 0; i
< set
->n
; ++i
) {
6716 set
->p
[i
] = basic_set_append_equalities(set
->p
[i
],
6729 /* Given a basic set "bset" that only involves parameters and existentially
6730 * quantified variables, return the index of the first equality
6731 * that only involves parameters. If there is no such equality then
6732 * return bset->n_eq.
6734 * This function assumes that isl_basic_set_gauss has been called on "bset".
6736 static int first_parameter_equality(__isl_keep isl_basic_set
*bset
)
6739 unsigned nparam
, n_div
;
6744 nparam
= isl_basic_set_dim(bset
, isl_dim_param
);
6745 n_div
= isl_basic_set_dim(bset
, isl_dim_div
);
6747 for (i
= 0, j
= n_div
- 1; i
< bset
->n_eq
&& j
>= 0; --j
) {
6748 if (!isl_int_is_zero(bset
->eq
[i
][1 + nparam
+ j
]))
6755 /* Compute an explicit representation for the existentially quantified
6756 * variables in "bset" by computing the "minimal value" of the set
6757 * variables. Since there are no set variables, the computation of
6758 * the minimal value essentially computes an explicit representation
6759 * of the non-empty part(s) of "bset".
6761 * The input only involves parameters and existentially quantified variables.
6762 * All equalities among parameters have been removed.
6764 * Since the existentially quantified variables in the result are in general
6765 * going to be different from those in the input, we first replace
6766 * them by the minimal number of variables based on their equalities.
6767 * This should simplify the parametric integer programming.
6769 static __isl_give isl_set
*base_compute_divs(__isl_take isl_basic_set
*bset
)
6771 isl_morph
*morph1
, *morph2
;
6777 if (bset
->n_eq
== 0)
6778 return isl_basic_set_lexmin_compute_divs(bset
);
6780 morph1
= isl_basic_set_parameter_compression(bset
);
6781 bset
= isl_morph_basic_set(isl_morph_copy(morph1
), bset
);
6782 bset
= isl_basic_set_lift(bset
);
6783 morph2
= isl_basic_set_variable_compression(bset
, isl_dim_set
);
6784 bset
= isl_morph_basic_set(morph2
, bset
);
6785 n
= isl_basic_set_dim(bset
, isl_dim_set
);
6786 bset
= isl_basic_set_project_out(bset
, isl_dim_set
, 0, n
);
6788 set
= isl_basic_set_lexmin_compute_divs(bset
);
6790 set
= isl_morph_set(isl_morph_inverse(morph1
), set
);
6795 /* Project the given basic set onto its parameter domain, possibly introducing
6796 * new, explicit, existential variables in the constraints.
6797 * The input has parameters and (possibly implicit) existential variables.
6798 * The output has the same parameters, but only
6799 * explicit existentially quantified variables.
6801 * The actual projection is performed by pip, but pip doesn't seem
6802 * to like equalities very much, so we first remove the equalities
6803 * among the parameters by performing a variable compression on
6804 * the parameters. Afterward, an inverse transformation is performed
6805 * and the equalities among the parameters are inserted back in.
6807 * The variable compression on the parameters may uncover additional
6808 * equalities that were only implicit before. We therefore check
6809 * if there are any new parameter equalities in the result and
6810 * if so recurse. The removal of parameter equalities is required
6811 * for the parameter compression performed by base_compute_divs.
6813 static struct isl_set
*parameter_compute_divs(struct isl_basic_set
*bset
)
6817 struct isl_mat
*T
, *T2
;
6818 struct isl_set
*set
;
6821 bset
= isl_basic_set_cow(bset
);
6825 if (bset
->n_eq
== 0)
6826 return base_compute_divs(bset
);
6828 bset
= isl_basic_set_gauss(bset
, NULL
);
6831 if (isl_basic_set_plain_is_empty(bset
))
6832 return isl_set_from_basic_set(bset
);
6834 i
= first_parameter_equality(bset
);
6835 if (i
== bset
->n_eq
)
6836 return base_compute_divs(bset
);
6838 nparam
= isl_basic_set_dim(bset
, isl_dim_param
);
6839 eq
= isl_mat_sub_alloc6(bset
->ctx
, bset
->eq
, i
, bset
->n_eq
- i
,
6841 eq
= isl_mat_cow(eq
);
6842 T
= isl_mat_variable_compression(isl_mat_copy(eq
), &T2
);
6843 if (T
&& T
->n_col
== 0) {
6847 bset
= isl_basic_set_set_to_empty(bset
);
6848 return isl_set_from_basic_set(bset
);
6850 bset
= basic_set_parameter_preimage(bset
, T
);
6852 i
= first_parameter_equality(bset
);
6855 else if (i
== bset
->n_eq
)
6856 set
= base_compute_divs(bset
);
6858 set
= parameter_compute_divs(bset
);
6859 set
= set_parameter_preimage(set
, T2
);
6860 set
= set_append_equalities(set
, eq
);
6864 /* Insert the divs from "ls" before those of "bmap".
6866 * The number of columns is not changed, which means that the last
6867 * dimensions of "bmap" are being reintepreted as the divs from "ls".
6868 * The caller is responsible for removing the same number of dimensions
6869 * from the space of "bmap".
6871 static __isl_give isl_basic_map
*insert_divs_from_local_space(
6872 __isl_take isl_basic_map
*bmap
, __isl_keep isl_local_space
*ls
)
6878 n_div
= isl_local_space_dim(ls
, isl_dim_div
);
6882 old_n_div
= bmap
->n_div
;
6883 bmap
= insert_div_rows(bmap
, n_div
);
6887 for (i
= 0; i
< n_div
; ++i
) {
6888 isl_seq_cpy(bmap
->div
[i
], ls
->div
->row
[i
], ls
->div
->n_col
);
6889 isl_seq_clr(bmap
->div
[i
] + ls
->div
->n_col
, old_n_div
);
6895 /* Replace the space of "bmap" by the space and divs of "ls".
6897 * If "ls" has any divs, then we simplify the result since we may
6898 * have discovered some additional equalities that could simplify
6899 * the div expressions.
6901 static __isl_give isl_basic_map
*basic_replace_space_by_local_space(
6902 __isl_take isl_basic_map
*bmap
, __isl_take isl_local_space
*ls
)
6906 bmap
= isl_basic_map_cow(bmap
);
6910 n_div
= isl_local_space_dim(ls
, isl_dim_div
);
6911 bmap
= insert_divs_from_local_space(bmap
, ls
);
6915 isl_space_free(bmap
->dim
);
6916 bmap
->dim
= isl_local_space_get_space(ls
);
6920 isl_local_space_free(ls
);
6922 bmap
= isl_basic_map_simplify(bmap
);
6923 bmap
= isl_basic_map_finalize(bmap
);
6926 isl_basic_map_free(bmap
);
6927 isl_local_space_free(ls
);
6931 /* Replace the space of "map" by the space and divs of "ls".
6933 static __isl_give isl_map
*replace_space_by_local_space(__isl_take isl_map
*map
,
6934 __isl_take isl_local_space
*ls
)
6938 map
= isl_map_cow(map
);
6942 for (i
= 0; i
< map
->n
; ++i
) {
6943 map
->p
[i
] = basic_replace_space_by_local_space(map
->p
[i
],
6944 isl_local_space_copy(ls
));
6948 isl_space_free(map
->dim
);
6949 map
->dim
= isl_local_space_get_space(ls
);
6953 isl_local_space_free(ls
);
6956 isl_local_space_free(ls
);
6961 /* Compute an explicit representation for the existentially
6962 * quantified variables for which do not know any explicit representation yet.
6964 * We first sort the existentially quantified variables so that the
6965 * existentially quantified variables for which we already have an explicit
6966 * representation are placed before those for which we do not.
6967 * The input dimensions, the output dimensions and the existentially
6968 * quantified variables for which we already have an explicit
6969 * representation are then turned into parameters.
6970 * compute_divs returns a map with the same parameters and
6971 * no input or output dimensions and the dimension specification
6972 * is reset to that of the input, including the existentially quantified
6973 * variables for which we already had an explicit representation.
6975 static struct isl_map
*compute_divs(struct isl_basic_map
*bmap
)
6977 struct isl_basic_set
*bset
;
6978 struct isl_set
*set
;
6979 struct isl_map
*map
;
6981 isl_local_space
*ls
;
6988 bmap
= isl_basic_map_sort_divs(bmap
);
6989 bmap
= isl_basic_map_cow(bmap
);
6993 n_known
= isl_basic_map_first_unknown_div(bmap
);
6995 return isl_map_from_basic_map(isl_basic_map_free(bmap
));
6997 nparam
= isl_basic_map_dim(bmap
, isl_dim_param
);
6998 n_in
= isl_basic_map_dim(bmap
, isl_dim_in
);
6999 n_out
= isl_basic_map_dim(bmap
, isl_dim_out
);
7000 dim
= isl_space_set_alloc(bmap
->ctx
,
7001 nparam
+ n_in
+ n_out
+ n_known
, 0);
7005 ls
= isl_basic_map_get_local_space(bmap
);
7006 ls
= isl_local_space_drop_dims(ls
, isl_dim_div
,
7007 n_known
, bmap
->n_div
- n_known
);
7009 for (i
= n_known
; i
< bmap
->n_div
; ++i
)
7010 swap_div(bmap
, i
- n_known
, i
);
7011 bmap
->n_div
-= n_known
;
7012 bmap
->extra
-= n_known
;
7014 bmap
= isl_basic_map_reset_space(bmap
, dim
);
7015 bset
= bset_from_bmap(bmap
);
7017 set
= parameter_compute_divs(bset
);
7018 map
= set_to_map(set
);
7019 map
= replace_space_by_local_space(map
, ls
);
7023 isl_basic_map_free(bmap
);
7027 /* Remove the explicit representation of local variable "div",
7030 __isl_give isl_basic_map
*isl_basic_map_mark_div_unknown(
7031 __isl_take isl_basic_map
*bmap
, int div
)
7035 unknown
= isl_basic_map_div_is_marked_unknown(bmap
, div
);
7037 return isl_basic_map_free(bmap
);
7041 bmap
= isl_basic_map_cow(bmap
);
7044 isl_int_set_si(bmap
->div
[div
][0], 0);
7048 /* Is local variable "div" of "bmap" marked as not having an explicit
7050 * Note that even if "div" is not marked in this way and therefore
7051 * has an explicit representation, this representation may still
7052 * depend (indirectly) on other local variables that do not
7053 * have an explicit representation.
7055 isl_bool
isl_basic_map_div_is_marked_unknown(__isl_keep isl_basic_map
*bmap
,
7058 if (isl_basic_map_check_range(bmap
, isl_dim_div
, div
, 1) < 0)
7059 return isl_bool_error
;
7060 return isl_int_is_zero(bmap
->div
[div
][0]);
7063 /* Return the position of the first local variable that does not
7064 * have an explicit representation.
7065 * Return the total number of local variables if they all have
7066 * an explicit representation.
7067 * Return -1 on error.
7069 int isl_basic_map_first_unknown_div(__isl_keep isl_basic_map
*bmap
)
7076 for (i
= 0; i
< bmap
->n_div
; ++i
) {
7077 if (!isl_basic_map_div_is_known(bmap
, i
))
7083 /* Return the position of the first local variable that does not
7084 * have an explicit representation.
7085 * Return the total number of local variables if they all have
7086 * an explicit representation.
7087 * Return -1 on error.
7089 int isl_basic_set_first_unknown_div(__isl_keep isl_basic_set
*bset
)
7091 return isl_basic_map_first_unknown_div(bset
);
7094 /* Does "bmap" have an explicit representation for all local variables?
7096 isl_bool
isl_basic_map_divs_known(__isl_keep isl_basic_map
*bmap
)
7100 n
= isl_basic_map_dim(bmap
, isl_dim_div
);
7101 first
= isl_basic_map_first_unknown_div(bmap
);
7103 return isl_bool_error
;
7107 /* Do all basic maps in "map" have an explicit representation
7108 * for all local variables?
7110 isl_bool
isl_map_divs_known(__isl_keep isl_map
*map
)
7115 return isl_bool_error
;
7117 for (i
= 0; i
< map
->n
; ++i
) {
7118 int known
= isl_basic_map_divs_known(map
->p
[i
]);
7123 return isl_bool_true
;
7126 /* If bmap contains any unknown divs, then compute explicit
7127 * expressions for them. However, this computation may be
7128 * quite expensive, so first try to remove divs that aren't
7131 struct isl_map
*isl_basic_map_compute_divs(struct isl_basic_map
*bmap
)
7134 struct isl_map
*map
;
7136 known
= isl_basic_map_divs_known(bmap
);
7140 return isl_map_from_basic_map(bmap
);
7142 bmap
= isl_basic_map_drop_redundant_divs(bmap
);
7144 known
= isl_basic_map_divs_known(bmap
);
7148 return isl_map_from_basic_map(bmap
);
7150 map
= compute_divs(bmap
);
7153 isl_basic_map_free(bmap
);
7157 struct isl_map
*isl_map_compute_divs(struct isl_map
*map
)
7161 struct isl_map
*res
;
7168 known
= isl_map_divs_known(map
);
7176 res
= isl_basic_map_compute_divs(isl_basic_map_copy(map
->p
[0]));
7177 for (i
= 1 ; i
< map
->n
; ++i
) {
7179 r2
= isl_basic_map_compute_divs(isl_basic_map_copy(map
->p
[i
]));
7180 if (ISL_F_ISSET(map
, ISL_MAP_DISJOINT
))
7181 res
= isl_map_union_disjoint(res
, r2
);
7183 res
= isl_map_union(res
, r2
);
7190 struct isl_set
*isl_basic_set_compute_divs(struct isl_basic_set
*bset
)
7192 return set_from_map(isl_basic_map_compute_divs(bset_to_bmap(bset
)));
7195 struct isl_set
*isl_set_compute_divs(struct isl_set
*set
)
7197 return set_from_map(isl_map_compute_divs(set_to_map(set
)));
7200 struct isl_set
*isl_map_domain(struct isl_map
*map
)
7203 struct isl_set
*set
;
7208 map
= isl_map_cow(map
);
7212 set
= set_from_map(map
);
7213 set
->dim
= isl_space_domain(set
->dim
);
7216 for (i
= 0; i
< map
->n
; ++i
) {
7217 set
->p
[i
] = isl_basic_map_domain(map
->p
[i
]);
7221 ISL_F_CLR(set
, ISL_MAP_DISJOINT
);
7222 ISL_F_CLR(set
, ISL_SET_NORMALIZED
);
7229 /* Return the union of "map1" and "map2", where we assume for now that
7230 * "map1" and "map2" are disjoint. Note that the basic maps inside
7231 * "map1" or "map2" may not be disjoint from each other.
7232 * Also note that this function is also called from isl_map_union,
7233 * which takes care of handling the situation where "map1" and "map2"
7234 * may not be disjoint.
7236 * If one of the inputs is empty, we can simply return the other input.
7237 * Similarly, if one of the inputs is universal, then it is equal to the union.
7239 static __isl_give isl_map
*map_union_disjoint(__isl_take isl_map
*map1
,
7240 __isl_take isl_map
*map2
)
7244 struct isl_map
*map
= NULL
;
7250 if (!isl_space_is_equal(map1
->dim
, map2
->dim
))
7251 isl_die(isl_map_get_ctx(map1
), isl_error_invalid
,
7252 "spaces don't match", goto error
);
7263 is_universe
= isl_map_plain_is_universe(map1
);
7264 if (is_universe
< 0)
7271 is_universe
= isl_map_plain_is_universe(map2
);
7272 if (is_universe
< 0)
7279 if (ISL_F_ISSET(map1
, ISL_MAP_DISJOINT
) &&
7280 ISL_F_ISSET(map2
, ISL_MAP_DISJOINT
))
7281 ISL_FL_SET(flags
, ISL_MAP_DISJOINT
);
7283 map
= isl_map_alloc_space(isl_space_copy(map1
->dim
),
7284 map1
->n
+ map2
->n
, flags
);
7287 for (i
= 0; i
< map1
->n
; ++i
) {
7288 map
= isl_map_add_basic_map(map
,
7289 isl_basic_map_copy(map1
->p
[i
]));
7293 for (i
= 0; i
< map2
->n
; ++i
) {
7294 map
= isl_map_add_basic_map(map
,
7295 isl_basic_map_copy(map2
->p
[i
]));
7309 /* Return the union of "map1" and "map2", where "map1" and "map2" are
7310 * guaranteed to be disjoint by the caller.
7312 * Note that this functions is called from within isl_map_make_disjoint,
7313 * so we have to be careful not to touch the constraints of the inputs
7316 __isl_give isl_map
*isl_map_union_disjoint(__isl_take isl_map
*map1
,
7317 __isl_take isl_map
*map2
)
7319 return isl_map_align_params_map_map_and(map1
, map2
, &map_union_disjoint
);
7322 /* Return the union of "map1" and "map2", where "map1" and "map2" may
7323 * not be disjoint. The parameters are assumed to have been aligned.
7325 * We currently simply call map_union_disjoint, the internal operation
7326 * of which does not really depend on the inputs being disjoint.
7327 * If the result contains more than one basic map, then we clear
7328 * the disjoint flag since the result may contain basic maps from
7329 * both inputs and these are not guaranteed to be disjoint.
7331 * As a special case, if "map1" and "map2" are obviously equal,
7332 * then we simply return "map1".
7334 static __isl_give isl_map
*map_union_aligned(__isl_take isl_map
*map1
,
7335 __isl_take isl_map
*map2
)
7342 equal
= isl_map_plain_is_equal(map1
, map2
);
7350 map1
= map_union_disjoint(map1
, map2
);
7354 ISL_F_CLR(map1
, ISL_MAP_DISJOINT
);
7362 /* Return the union of "map1" and "map2", where "map1" and "map2" may
7365 __isl_give isl_map
*isl_map_union(__isl_take isl_map
*map1
,
7366 __isl_take isl_map
*map2
)
7368 return isl_map_align_params_map_map_and(map1
, map2
, &map_union_aligned
);
7371 struct isl_set
*isl_set_union_disjoint(
7372 struct isl_set
*set1
, struct isl_set
*set2
)
7374 return set_from_map(isl_map_union_disjoint(set_to_map(set1
),
7378 struct isl_set
*isl_set_union(struct isl_set
*set1
, struct isl_set
*set2
)
7380 return set_from_map(isl_map_union(set_to_map(set1
), set_to_map(set2
)));
7383 /* Apply "fn" to pairs of elements from "map" and "set" and collect
7386 * "map" and "set" are assumed to be compatible and non-NULL.
7388 static __isl_give isl_map
*map_intersect_set(__isl_take isl_map
*map
,
7389 __isl_take isl_set
*set
,
7390 __isl_give isl_basic_map
*fn(__isl_take isl_basic_map
*bmap
,
7391 __isl_take isl_basic_set
*bset
))
7394 struct isl_map
*result
;
7397 if (isl_set_plain_is_universe(set
)) {
7402 if (ISL_F_ISSET(map
, ISL_MAP_DISJOINT
) &&
7403 ISL_F_ISSET(set
, ISL_MAP_DISJOINT
))
7404 ISL_FL_SET(flags
, ISL_MAP_DISJOINT
);
7406 result
= isl_map_alloc_space(isl_space_copy(map
->dim
),
7407 map
->n
* set
->n
, flags
);
7408 for (i
= 0; result
&& i
< map
->n
; ++i
)
7409 for (j
= 0; j
< set
->n
; ++j
) {
7410 result
= isl_map_add_basic_map(result
,
7411 fn(isl_basic_map_copy(map
->p
[i
]),
7412 isl_basic_set_copy(set
->p
[j
])));
7422 static __isl_give isl_map
*map_intersect_range(__isl_take isl_map
*map
,
7423 __isl_take isl_set
*set
)
7427 ok
= isl_map_compatible_range(map
, set
);
7431 isl_die(set
->ctx
, isl_error_invalid
,
7432 "incompatible spaces", goto error
);
7434 return map_intersect_set(map
, set
, &isl_basic_map_intersect_range
);
7441 __isl_give isl_map
*isl_map_intersect_range(__isl_take isl_map
*map
,
7442 __isl_take isl_set
*set
)
7444 return isl_map_align_params_map_map_and(map
, set
, &map_intersect_range
);
7447 static __isl_give isl_map
*map_intersect_domain(__isl_take isl_map
*map
,
7448 __isl_take isl_set
*set
)
7452 ok
= isl_map_compatible_domain(map
, set
);
7456 isl_die(set
->ctx
, isl_error_invalid
,
7457 "incompatible spaces", goto error
);
7459 return map_intersect_set(map
, set
, &isl_basic_map_intersect_domain
);
7466 __isl_give isl_map
*isl_map_intersect_domain(__isl_take isl_map
*map
,
7467 __isl_take isl_set
*set
)
7469 return isl_map_align_params_map_map_and(map
, set
,
7470 &map_intersect_domain
);
7473 static __isl_give isl_map
*map_apply_domain(__isl_take isl_map
*map1
,
7474 __isl_take isl_map
*map2
)
7478 map1
= isl_map_reverse(map1
);
7479 map1
= isl_map_apply_range(map1
, map2
);
7480 return isl_map_reverse(map1
);
7487 __isl_give isl_map
*isl_map_apply_domain(__isl_take isl_map
*map1
,
7488 __isl_take isl_map
*map2
)
7490 return isl_map_align_params_map_map_and(map1
, map2
, &map_apply_domain
);
7493 static __isl_give isl_map
*map_apply_range(__isl_take isl_map
*map1
,
7494 __isl_take isl_map
*map2
)
7496 isl_space
*dim_result
;
7497 struct isl_map
*result
;
7503 dim_result
= isl_space_join(isl_space_copy(map1
->dim
),
7504 isl_space_copy(map2
->dim
));
7506 result
= isl_map_alloc_space(dim_result
, map1
->n
* map2
->n
, 0);
7509 for (i
= 0; i
< map1
->n
; ++i
)
7510 for (j
= 0; j
< map2
->n
; ++j
) {
7511 result
= isl_map_add_basic_map(result
,
7512 isl_basic_map_apply_range(
7513 isl_basic_map_copy(map1
->p
[i
]),
7514 isl_basic_map_copy(map2
->p
[j
])));
7520 if (result
&& result
->n
<= 1)
7521 ISL_F_SET(result
, ISL_MAP_DISJOINT
);
7529 __isl_give isl_map
*isl_map_apply_range(__isl_take isl_map
*map1
,
7530 __isl_take isl_map
*map2
)
7532 return isl_map_align_params_map_map_and(map1
, map2
, &map_apply_range
);
7536 * returns range - domain
7538 struct isl_basic_set
*isl_basic_map_deltas(struct isl_basic_map
*bmap
)
7540 isl_space
*target_space
;
7541 struct isl_basic_set
*bset
;
7548 isl_assert(bmap
->ctx
, isl_space_tuple_is_equal(bmap
->dim
, isl_dim_in
,
7549 bmap
->dim
, isl_dim_out
),
7551 target_space
= isl_space_domain(isl_basic_map_get_space(bmap
));
7552 dim
= isl_basic_map_dim(bmap
, isl_dim_in
);
7553 nparam
= isl_basic_map_dim(bmap
, isl_dim_param
);
7554 bmap
= isl_basic_map_from_range(isl_basic_map_wrap(bmap
));
7555 bmap
= isl_basic_map_add_dims(bmap
, isl_dim_in
, dim
);
7556 bmap
= isl_basic_map_extend_constraints(bmap
, dim
, 0);
7557 for (i
= 0; i
< dim
; ++i
) {
7558 int j
= isl_basic_map_alloc_equality(bmap
);
7560 bmap
= isl_basic_map_free(bmap
);
7563 isl_seq_clr(bmap
->eq
[j
], 1 + isl_basic_map_total_dim(bmap
));
7564 isl_int_set_si(bmap
->eq
[j
][1+nparam
+i
], 1);
7565 isl_int_set_si(bmap
->eq
[j
][1+nparam
+dim
+i
], 1);
7566 isl_int_set_si(bmap
->eq
[j
][1+nparam
+2*dim
+i
], -1);
7568 bset
= isl_basic_map_domain(bmap
);
7569 bset
= isl_basic_set_reset_space(bset
, target_space
);
7572 isl_basic_map_free(bmap
);
7577 * returns range - domain
7579 __isl_give isl_set
*isl_map_deltas(__isl_take isl_map
*map
)
7583 struct isl_set
*result
;
7588 isl_assert(map
->ctx
, isl_space_tuple_is_equal(map
->dim
, isl_dim_in
,
7589 map
->dim
, isl_dim_out
),
7591 dim
= isl_map_get_space(map
);
7592 dim
= isl_space_domain(dim
);
7593 result
= isl_set_alloc_space(dim
, map
->n
, 0);
7596 for (i
= 0; i
< map
->n
; ++i
)
7597 result
= isl_set_add_basic_set(result
,
7598 isl_basic_map_deltas(isl_basic_map_copy(map
->p
[i
])));
7607 * returns [domain -> range] -> range - domain
7609 __isl_give isl_basic_map
*isl_basic_map_deltas_map(
7610 __isl_take isl_basic_map
*bmap
)
7614 isl_basic_map
*domain
;
7618 if (!isl_space_tuple_is_equal(bmap
->dim
, isl_dim_in
,
7619 bmap
->dim
, isl_dim_out
))
7620 isl_die(bmap
->ctx
, isl_error_invalid
,
7621 "domain and range don't match", goto error
);
7623 nparam
= isl_basic_map_dim(bmap
, isl_dim_param
);
7624 n
= isl_basic_map_dim(bmap
, isl_dim_in
);
7626 dim
= isl_space_from_range(isl_space_domain(isl_basic_map_get_space(bmap
)));
7627 domain
= isl_basic_map_universe(dim
);
7629 bmap
= isl_basic_map_from_domain(isl_basic_map_wrap(bmap
));
7630 bmap
= isl_basic_map_apply_range(bmap
, domain
);
7631 bmap
= isl_basic_map_extend_constraints(bmap
, n
, 0);
7633 total
= isl_basic_map_total_dim(bmap
);
7635 for (i
= 0; i
< n
; ++i
) {
7636 k
= isl_basic_map_alloc_equality(bmap
);
7639 isl_seq_clr(bmap
->eq
[k
], 1 + total
);
7640 isl_int_set_si(bmap
->eq
[k
][1 + nparam
+ i
], 1);
7641 isl_int_set_si(bmap
->eq
[k
][1 + nparam
+ n
+ i
], -1);
7642 isl_int_set_si(bmap
->eq
[k
][1 + nparam
+ n
+ n
+ i
], 1);
7645 bmap
= isl_basic_map_gauss(bmap
, NULL
);
7646 return isl_basic_map_finalize(bmap
);
7648 isl_basic_map_free(bmap
);
7653 * returns [domain -> range] -> range - domain
7655 __isl_give isl_map
*isl_map_deltas_map(__isl_take isl_map
*map
)
7658 isl_space
*domain_dim
;
7663 if (!isl_space_tuple_is_equal(map
->dim
, isl_dim_in
,
7664 map
->dim
, isl_dim_out
))
7665 isl_die(map
->ctx
, isl_error_invalid
,
7666 "domain and range don't match", goto error
);
7668 map
= isl_map_cow(map
);
7672 domain_dim
= isl_space_from_range(isl_space_domain(isl_map_get_space(map
)));
7673 map
->dim
= isl_space_from_domain(isl_space_wrap(map
->dim
));
7674 map
->dim
= isl_space_join(map
->dim
, domain_dim
);
7677 for (i
= 0; i
< map
->n
; ++i
) {
7678 map
->p
[i
] = isl_basic_map_deltas_map(map
->p
[i
]);
7682 ISL_F_CLR(map
, ISL_MAP_NORMALIZED
);
7689 static __isl_give isl_basic_map
*basic_map_identity(__isl_take isl_space
*dims
)
7691 struct isl_basic_map
*bmap
;
7699 nparam
= dims
->nparam
;
7701 bmap
= isl_basic_map_alloc_space(dims
, 0, dim
, 0);
7705 for (i
= 0; i
< dim
; ++i
) {
7706 int j
= isl_basic_map_alloc_equality(bmap
);
7709 isl_seq_clr(bmap
->eq
[j
], 1 + isl_basic_map_total_dim(bmap
));
7710 isl_int_set_si(bmap
->eq
[j
][1+nparam
+i
], 1);
7711 isl_int_set_si(bmap
->eq
[j
][1+nparam
+dim
+i
], -1);
7713 return isl_basic_map_finalize(bmap
);
7715 isl_basic_map_free(bmap
);
7719 __isl_give isl_basic_map
*isl_basic_map_identity(__isl_take isl_space
*dim
)
7723 if (dim
->n_in
!= dim
->n_out
)
7724 isl_die(dim
->ctx
, isl_error_invalid
,
7725 "number of input and output dimensions needs to be "
7726 "the same", goto error
);
7727 return basic_map_identity(dim
);
7729 isl_space_free(dim
);
7733 __isl_give isl_map
*isl_map_identity(__isl_take isl_space
*dim
)
7735 return isl_map_from_basic_map(isl_basic_map_identity(dim
));
7738 __isl_give isl_map
*isl_set_identity(__isl_take isl_set
*set
)
7740 isl_space
*dim
= isl_set_get_space(set
);
7742 id
= isl_map_identity(isl_space_map_from_set(dim
));
7743 return isl_map_intersect_range(id
, set
);
7746 /* Construct a basic set with all set dimensions having only non-negative
7749 __isl_give isl_basic_set
*isl_basic_set_positive_orthant(
7750 __isl_take isl_space
*space
)
7755 struct isl_basic_set
*bset
;
7759 nparam
= space
->nparam
;
7761 bset
= isl_basic_set_alloc_space(space
, 0, 0, dim
);
7764 for (i
= 0; i
< dim
; ++i
) {
7765 int k
= isl_basic_set_alloc_inequality(bset
);
7768 isl_seq_clr(bset
->ineq
[k
], 1 + isl_basic_set_total_dim(bset
));
7769 isl_int_set_si(bset
->ineq
[k
][1 + nparam
+ i
], 1);
7773 isl_basic_set_free(bset
);
7777 /* Construct the half-space x_pos >= 0.
7779 static __isl_give isl_basic_set
*nonneg_halfspace(__isl_take isl_space
*dim
,
7783 isl_basic_set
*nonneg
;
7785 nonneg
= isl_basic_set_alloc_space(dim
, 0, 0, 1);
7786 k
= isl_basic_set_alloc_inequality(nonneg
);
7789 isl_seq_clr(nonneg
->ineq
[k
], 1 + isl_basic_set_total_dim(nonneg
));
7790 isl_int_set_si(nonneg
->ineq
[k
][pos
], 1);
7792 return isl_basic_set_finalize(nonneg
);
7794 isl_basic_set_free(nonneg
);
7798 /* Construct the half-space x_pos <= -1.
7800 static __isl_give isl_basic_set
*neg_halfspace(__isl_take isl_space
*dim
, int pos
)
7805 neg
= isl_basic_set_alloc_space(dim
, 0, 0, 1);
7806 k
= isl_basic_set_alloc_inequality(neg
);
7809 isl_seq_clr(neg
->ineq
[k
], 1 + isl_basic_set_total_dim(neg
));
7810 isl_int_set_si(neg
->ineq
[k
][0], -1);
7811 isl_int_set_si(neg
->ineq
[k
][pos
], -1);
7813 return isl_basic_set_finalize(neg
);
7815 isl_basic_set_free(neg
);
7819 __isl_give isl_set
*isl_set_split_dims(__isl_take isl_set
*set
,
7820 enum isl_dim_type type
, unsigned first
, unsigned n
)
7824 isl_basic_set
*nonneg
;
7832 isl_assert(set
->ctx
, first
+ n
<= isl_set_dim(set
, type
), goto error
);
7834 offset
= pos(set
->dim
, type
);
7835 for (i
= 0; i
< n
; ++i
) {
7836 nonneg
= nonneg_halfspace(isl_set_get_space(set
),
7837 offset
+ first
+ i
);
7838 neg
= neg_halfspace(isl_set_get_space(set
), offset
+ first
+ i
);
7840 set
= isl_set_intersect(set
, isl_basic_set_union(nonneg
, neg
));
7849 static isl_stat
foreach_orthant(__isl_take isl_set
*set
, int *signs
, int first
,
7851 isl_stat (*fn
)(__isl_take isl_set
*orthant
, int *signs
, void *user
),
7857 return isl_stat_error
;
7858 if (isl_set_plain_is_empty(set
)) {
7863 return fn(set
, signs
, user
);
7866 half
= isl_set_from_basic_set(nonneg_halfspace(isl_set_get_space(set
),
7868 half
= isl_set_intersect(half
, isl_set_copy(set
));
7869 if (foreach_orthant(half
, signs
, first
+ 1, len
, fn
, user
) < 0)
7873 half
= isl_set_from_basic_set(neg_halfspace(isl_set_get_space(set
),
7875 half
= isl_set_intersect(half
, set
);
7876 return foreach_orthant(half
, signs
, first
+ 1, len
, fn
, user
);
7879 return isl_stat_error
;
7882 /* Call "fn" on the intersections of "set" with each of the orthants
7883 * (except for obviously empty intersections). The orthant is identified
7884 * by the signs array, with each entry having value 1 or -1 according
7885 * to the sign of the corresponding variable.
7887 isl_stat
isl_set_foreach_orthant(__isl_keep isl_set
*set
,
7888 isl_stat (*fn
)(__isl_take isl_set
*orthant
, int *signs
, void *user
),
7897 return isl_stat_error
;
7898 if (isl_set_plain_is_empty(set
))
7901 nparam
= isl_set_dim(set
, isl_dim_param
);
7902 nvar
= isl_set_dim(set
, isl_dim_set
);
7904 signs
= isl_alloc_array(set
->ctx
, int, nparam
+ nvar
);
7906 r
= foreach_orthant(isl_set_copy(set
), signs
, 0, nparam
+ nvar
,
7914 isl_bool
isl_set_is_equal(__isl_keep isl_set
*set1
, __isl_keep isl_set
*set2
)
7916 return isl_map_is_equal(set_to_map(set1
), set_to_map(set2
));
7919 isl_bool
isl_basic_map_is_subset(__isl_keep isl_basic_map
*bmap1
,
7920 __isl_keep isl_basic_map
*bmap2
)
7923 struct isl_map
*map1
;
7924 struct isl_map
*map2
;
7926 if (!bmap1
|| !bmap2
)
7927 return isl_bool_error
;
7929 map1
= isl_map_from_basic_map(isl_basic_map_copy(bmap1
));
7930 map2
= isl_map_from_basic_map(isl_basic_map_copy(bmap2
));
7932 is_subset
= isl_map_is_subset(map1
, map2
);
7940 isl_bool
isl_basic_set_is_subset(__isl_keep isl_basic_set
*bset1
,
7941 __isl_keep isl_basic_set
*bset2
)
7943 return isl_basic_map_is_subset(bset1
, bset2
);
7946 isl_bool
isl_basic_map_is_equal(__isl_keep isl_basic_map
*bmap1
,
7947 __isl_keep isl_basic_map
*bmap2
)
7951 if (!bmap1
|| !bmap2
)
7952 return isl_bool_error
;
7953 is_subset
= isl_basic_map_is_subset(bmap1
, bmap2
);
7954 if (is_subset
!= isl_bool_true
)
7956 is_subset
= isl_basic_map_is_subset(bmap2
, bmap1
);
7960 isl_bool
isl_basic_set_is_equal(__isl_keep isl_basic_set
*bset1
,
7961 __isl_keep isl_basic_set
*bset2
)
7963 return isl_basic_map_is_equal(
7964 bset_to_bmap(bset1
), bset_to_bmap(bset2
));
7967 isl_bool
isl_map_is_empty(__isl_keep isl_map
*map
)
7973 return isl_bool_error
;
7974 for (i
= 0; i
< map
->n
; ++i
) {
7975 is_empty
= isl_basic_map_is_empty(map
->p
[i
]);
7977 return isl_bool_error
;
7979 return isl_bool_false
;
7981 return isl_bool_true
;
7984 isl_bool
isl_map_plain_is_empty(__isl_keep isl_map
*map
)
7986 return map
? map
->n
== 0 : isl_bool_error
;
7989 isl_bool
isl_set_plain_is_empty(__isl_keep isl_set
*set
)
7991 return set
? set
->n
== 0 : isl_bool_error
;
7994 isl_bool
isl_set_is_empty(__isl_keep isl_set
*set
)
7996 return isl_map_is_empty(set_to_map(set
));
7999 isl_bool
isl_map_has_equal_space(__isl_keep isl_map
*map1
,
8000 __isl_keep isl_map
*map2
)
8003 return isl_bool_error
;
8005 return isl_space_is_equal(map1
->dim
, map2
->dim
);
8008 isl_bool
isl_set_has_equal_space(__isl_keep isl_set
*set1
,
8009 __isl_keep isl_set
*set2
)
8012 return isl_bool_error
;
8014 return isl_space_is_equal(set1
->dim
, set2
->dim
);
8017 static isl_bool
map_is_equal(__isl_keep isl_map
*map1
, __isl_keep isl_map
*map2
)
8022 return isl_bool_error
;
8023 is_subset
= isl_map_is_subset(map1
, map2
);
8024 if (is_subset
!= isl_bool_true
)
8026 is_subset
= isl_map_is_subset(map2
, map1
);
8030 /* Is "map1" equal to "map2"?
8032 * First check if they are obviously equal.
8033 * If not, then perform a more detailed analysis.
8035 isl_bool
isl_map_is_equal(__isl_keep isl_map
*map1
, __isl_keep isl_map
*map2
)
8039 equal
= isl_map_plain_is_equal(map1
, map2
);
8040 if (equal
< 0 || equal
)
8042 return isl_map_align_params_map_map_and_test(map1
, map2
, &map_is_equal
);
8045 isl_bool
isl_basic_map_is_strict_subset(
8046 struct isl_basic_map
*bmap1
, struct isl_basic_map
*bmap2
)
8050 if (!bmap1
|| !bmap2
)
8051 return isl_bool_error
;
8052 is_subset
= isl_basic_map_is_subset(bmap1
, bmap2
);
8053 if (is_subset
!= isl_bool_true
)
8055 is_subset
= isl_basic_map_is_subset(bmap2
, bmap1
);
8056 if (is_subset
== isl_bool_error
)
8061 isl_bool
isl_map_is_strict_subset(__isl_keep isl_map
*map1
,
8062 __isl_keep isl_map
*map2
)
8067 return isl_bool_error
;
8068 is_subset
= isl_map_is_subset(map1
, map2
);
8069 if (is_subset
!= isl_bool_true
)
8071 is_subset
= isl_map_is_subset(map2
, map1
);
8072 if (is_subset
== isl_bool_error
)
8077 isl_bool
isl_set_is_strict_subset(__isl_keep isl_set
*set1
,
8078 __isl_keep isl_set
*set2
)
8080 return isl_map_is_strict_subset(set_to_map(set1
), set_to_map(set2
));
8083 /* Is "bmap" obviously equal to the universe with the same space?
8085 * That is, does it not have any constraints?
8087 isl_bool
isl_basic_map_plain_is_universe(__isl_keep isl_basic_map
*bmap
)
8090 return isl_bool_error
;
8091 return bmap
->n_eq
== 0 && bmap
->n_ineq
== 0;
8094 /* Is "bset" obviously equal to the universe with the same space?
8096 isl_bool
isl_basic_set_plain_is_universe(__isl_keep isl_basic_set
*bset
)
8098 return isl_basic_map_plain_is_universe(bset
);
8101 /* If "c" does not involve any existentially quantified variables,
8102 * then set *univ to false and abort
8104 static isl_stat
involves_divs(__isl_take isl_constraint
*c
, void *user
)
8106 isl_bool
*univ
= user
;
8109 n
= isl_constraint_dim(c
, isl_dim_div
);
8110 *univ
= isl_constraint_involves_dims(c
, isl_dim_div
, 0, n
);
8111 isl_constraint_free(c
);
8112 if (*univ
< 0 || !*univ
)
8113 return isl_stat_error
;
8117 /* Is "bmap" equal to the universe with the same space?
8119 * First check if it is obviously equal to the universe.
8120 * If not and if there are any constraints not involving
8121 * existentially quantified variables, then it is certainly
8122 * not equal to the universe.
8123 * Otherwise, check if the universe is a subset of "bmap".
8125 isl_bool
isl_basic_map_is_universe(__isl_keep isl_basic_map
*bmap
)
8128 isl_basic_map
*test
;
8130 univ
= isl_basic_map_plain_is_universe(bmap
);
8131 if (univ
< 0 || univ
)
8133 if (isl_basic_map_dim(bmap
, isl_dim_div
) == 0)
8134 return isl_bool_false
;
8135 univ
= isl_bool_true
;
8136 if (isl_basic_map_foreach_constraint(bmap
, &involves_divs
, &univ
) < 0 &&
8138 return isl_bool_error
;
8139 if (univ
< 0 || !univ
)
8141 test
= isl_basic_map_universe(isl_basic_map_get_space(bmap
));
8142 univ
= isl_basic_map_is_subset(test
, bmap
);
8143 isl_basic_map_free(test
);
8147 /* Is "bset" equal to the universe with the same space?
8149 isl_bool
isl_basic_set_is_universe(__isl_keep isl_basic_set
*bset
)
8151 return isl_basic_map_is_universe(bset
);
8154 isl_bool
isl_map_plain_is_universe(__isl_keep isl_map
*map
)
8159 return isl_bool_error
;
8161 for (i
= 0; i
< map
->n
; ++i
) {
8162 isl_bool r
= isl_basic_map_plain_is_universe(map
->p
[i
]);
8167 return isl_bool_false
;
8170 isl_bool
isl_set_plain_is_universe(__isl_keep isl_set
*set
)
8172 return isl_map_plain_is_universe(set_to_map(set
));
8175 isl_bool
isl_basic_map_is_empty(__isl_keep isl_basic_map
*bmap
)
8177 struct isl_basic_set
*bset
= NULL
;
8178 struct isl_vec
*sample
= NULL
;
8179 isl_bool empty
, non_empty
;
8182 return isl_bool_error
;
8184 if (ISL_F_ISSET(bmap
, ISL_BASIC_MAP_EMPTY
))
8185 return isl_bool_true
;
8187 if (isl_basic_map_plain_is_universe(bmap
))
8188 return isl_bool_false
;
8190 if (ISL_F_ISSET(bmap
, ISL_BASIC_MAP_RATIONAL
)) {
8191 struct isl_basic_map
*copy
= isl_basic_map_copy(bmap
);
8192 copy
= isl_basic_map_remove_redundancies(copy
);
8193 empty
= isl_basic_map_plain_is_empty(copy
);
8194 isl_basic_map_free(copy
);
8198 non_empty
= isl_basic_map_plain_is_non_empty(bmap
);
8200 return isl_bool_error
;
8202 return isl_bool_false
;
8203 isl_vec_free(bmap
->sample
);
8204 bmap
->sample
= NULL
;
8205 bset
= isl_basic_map_underlying_set(isl_basic_map_copy(bmap
));
8207 return isl_bool_error
;
8208 sample
= isl_basic_set_sample_vec(bset
);
8210 return isl_bool_error
;
8211 empty
= sample
->size
== 0;
8212 isl_vec_free(bmap
->sample
);
8213 bmap
->sample
= sample
;
8215 ISL_F_SET(bmap
, ISL_BASIC_MAP_EMPTY
);
8220 isl_bool
isl_basic_map_plain_is_empty(__isl_keep isl_basic_map
*bmap
)
8223 return isl_bool_error
;
8224 return ISL_F_ISSET(bmap
, ISL_BASIC_MAP_EMPTY
);
8227 isl_bool
isl_basic_set_plain_is_empty(__isl_keep isl_basic_set
*bset
)
8230 return isl_bool_error
;
8231 return ISL_F_ISSET(bset
, ISL_BASIC_SET_EMPTY
);
8234 /* Is "bmap" known to be non-empty?
8236 * That is, is the cached sample still valid?
8238 isl_bool
isl_basic_map_plain_is_non_empty(__isl_keep isl_basic_map
*bmap
)
8243 return isl_bool_error
;
8245 return isl_bool_false
;
8246 total
= 1 + isl_basic_map_total_dim(bmap
);
8247 if (bmap
->sample
->size
!= total
)
8248 return isl_bool_false
;
8249 return isl_basic_map_contains(bmap
, bmap
->sample
);
8252 isl_bool
isl_basic_set_is_empty(__isl_keep isl_basic_set
*bset
)
8254 return isl_basic_map_is_empty(bset_to_bmap(bset
));
8257 struct isl_map
*isl_basic_map_union(
8258 struct isl_basic_map
*bmap1
, struct isl_basic_map
*bmap2
)
8260 struct isl_map
*map
;
8261 if (!bmap1
|| !bmap2
)
8264 isl_assert(bmap1
->ctx
, isl_space_is_equal(bmap1
->dim
, bmap2
->dim
), goto error
);
8266 map
= isl_map_alloc_space(isl_space_copy(bmap1
->dim
), 2, 0);
8269 map
= isl_map_add_basic_map(map
, bmap1
);
8270 map
= isl_map_add_basic_map(map
, bmap2
);
8273 isl_basic_map_free(bmap1
);
8274 isl_basic_map_free(bmap2
);
8278 struct isl_set
*isl_basic_set_union(
8279 struct isl_basic_set
*bset1
, struct isl_basic_set
*bset2
)
8281 return set_from_map(isl_basic_map_union(bset_to_bmap(bset1
),
8282 bset_to_bmap(bset2
)));
8285 /* Order divs such that any div only depends on previous divs */
8286 struct isl_basic_map
*isl_basic_map_order_divs(struct isl_basic_map
*bmap
)
8294 off
= isl_space_dim(bmap
->dim
, isl_dim_all
);
8296 for (i
= 0; i
< bmap
->n_div
; ++i
) {
8298 if (isl_int_is_zero(bmap
->div
[i
][0]))
8300 pos
= isl_seq_first_non_zero(bmap
->div
[i
]+1+1+off
+i
,
8305 isl_die(isl_basic_map_get_ctx(bmap
), isl_error_internal
,
8306 "integer division depends on itself",
8307 return isl_basic_map_free(bmap
));
8308 isl_basic_map_swap_div(bmap
, i
, i
+ pos
);
8314 struct isl_basic_set
*isl_basic_set_order_divs(struct isl_basic_set
*bset
)
8316 return bset_from_bmap(isl_basic_map_order_divs(bset_to_bmap(bset
)));
8319 __isl_give isl_map
*isl_map_order_divs(__isl_take isl_map
*map
)
8326 for (i
= 0; i
< map
->n
; ++i
) {
8327 map
->p
[i
] = isl_basic_map_order_divs(map
->p
[i
]);
8338 /* Sort the local variables of "bset".
8340 __isl_give isl_basic_set
*isl_basic_set_sort_divs(
8341 __isl_take isl_basic_set
*bset
)
8343 return bset_from_bmap(isl_basic_map_sort_divs(bset_to_bmap(bset
)));
8346 /* Apply the expansion computed by isl_merge_divs.
8347 * The expansion itself is given by "exp" while the resulting
8348 * list of divs is given by "div".
8350 * Move the integer divisions of "bmap" into the right position
8351 * according to "exp" and then introduce the additional integer
8352 * divisions, adding div constraints.
8353 * The moving should be done first to avoid moving coefficients
8354 * in the definitions of the extra integer divisions.
8356 __isl_give isl_basic_map
*isl_basic_map_expand_divs(
8357 __isl_take isl_basic_map
*bmap
, __isl_take isl_mat
*div
, int *exp
)
8362 bmap
= isl_basic_map_cow(bmap
);
8366 if (div
->n_row
< bmap
->n_div
)
8367 isl_die(isl_mat_get_ctx(div
), isl_error_invalid
,
8368 "not an expansion", goto error
);
8370 n_div
= bmap
->n_div
;
8371 bmap
= isl_basic_map_extend_space(bmap
, isl_space_copy(bmap
->dim
),
8372 div
->n_row
- n_div
, 0,
8373 2 * (div
->n_row
- n_div
));
8375 for (i
= n_div
; i
< div
->n_row
; ++i
)
8376 if (isl_basic_map_alloc_div(bmap
) < 0)
8379 for (j
= n_div
- 1; j
>= 0; --j
) {
8382 isl_basic_map_swap_div(bmap
, j
, exp
[j
]);
8385 for (i
= 0; i
< div
->n_row
; ++i
) {
8386 if (j
< n_div
&& exp
[j
] == i
) {
8389 isl_seq_cpy(bmap
->div
[i
], div
->row
[i
], div
->n_col
);
8390 if (isl_basic_map_div_is_marked_unknown(bmap
, i
))
8392 if (isl_basic_map_add_div_constraints(bmap
, i
) < 0)
8400 isl_basic_map_free(bmap
);
8405 /* Apply the expansion computed by isl_merge_divs.
8406 * The expansion itself is given by "exp" while the resulting
8407 * list of divs is given by "div".
8409 __isl_give isl_basic_set
*isl_basic_set_expand_divs(
8410 __isl_take isl_basic_set
*bset
, __isl_take isl_mat
*div
, int *exp
)
8412 return isl_basic_map_expand_divs(bset
, div
, exp
);
8415 /* Look for a div in dst that corresponds to the div "div" in src.
8416 * The divs before "div" in src and dst are assumed to be the same.
8418 * Returns -1 if no corresponding div was found and the position
8419 * of the corresponding div in dst otherwise.
8421 static int find_div(struct isl_basic_map
*dst
,
8422 struct isl_basic_map
*src
, unsigned div
)
8426 unsigned total
= isl_space_dim(src
->dim
, isl_dim_all
);
8428 isl_assert(dst
->ctx
, div
<= dst
->n_div
, return -1);
8429 for (i
= div
; i
< dst
->n_div
; ++i
)
8430 if (isl_seq_eq(dst
->div
[i
], src
->div
[div
], 1+1+total
+div
) &&
8431 isl_seq_first_non_zero(dst
->div
[i
]+1+1+total
+div
,
8432 dst
->n_div
- div
) == -1)
8437 /* Align the divs of "dst" to those of "src", adding divs from "src"
8438 * if needed. That is, make sure that the first src->n_div divs
8439 * of the result are equal to those of src.
8441 * The result is not finalized as by design it will have redundant
8442 * divs if any divs from "src" were copied.
8444 __isl_give isl_basic_map
*isl_basic_map_align_divs(
8445 __isl_take isl_basic_map
*dst
, __isl_keep isl_basic_map
*src
)
8448 int known
, extended
;
8452 return isl_basic_map_free(dst
);
8454 if (src
->n_div
== 0)
8457 known
= isl_basic_map_divs_known(src
);
8459 return isl_basic_map_free(dst
);
8461 isl_die(isl_basic_map_get_ctx(src
), isl_error_invalid
,
8462 "some src divs are unknown",
8463 return isl_basic_map_free(dst
));
8465 src
= isl_basic_map_order_divs(src
);
8468 total
= isl_space_dim(src
->dim
, isl_dim_all
);
8469 for (i
= 0; i
< src
->n_div
; ++i
) {
8470 int j
= find_div(dst
, src
, i
);
8473 int extra
= src
->n_div
- i
;
8474 dst
= isl_basic_map_cow(dst
);
8477 dst
= isl_basic_map_extend_space(dst
,
8478 isl_space_copy(dst
->dim
),
8479 extra
, 0, 2 * extra
);
8482 j
= isl_basic_map_alloc_div(dst
);
8484 return isl_basic_map_free(dst
);
8485 isl_seq_cpy(dst
->div
[j
], src
->div
[i
], 1+1+total
+i
);
8486 isl_seq_clr(dst
->div
[j
]+1+1+total
+i
, dst
->n_div
- i
);
8487 if (isl_basic_map_add_div_constraints(dst
, j
) < 0)
8488 return isl_basic_map_free(dst
);
8491 isl_basic_map_swap_div(dst
, i
, j
);
8496 __isl_give isl_map
*isl_map_align_divs_internal(__isl_take isl_map
*map
)
8504 map
= isl_map_compute_divs(map
);
8505 map
= isl_map_cow(map
);
8509 for (i
= 1; i
< map
->n
; ++i
)
8510 map
->p
[0] = isl_basic_map_align_divs(map
->p
[0], map
->p
[i
]);
8511 for (i
= 1; i
< map
->n
; ++i
) {
8512 map
->p
[i
] = isl_basic_map_align_divs(map
->p
[i
], map
->p
[0]);
8514 return isl_map_free(map
);
8517 ISL_F_CLR(map
, ISL_MAP_NORMALIZED
);
8521 __isl_give isl_map
*isl_map_align_divs(__isl_take isl_map
*map
)
8523 return isl_map_align_divs_internal(map
);
8526 struct isl_set
*isl_set_align_divs(struct isl_set
*set
)
8528 return set_from_map(isl_map_align_divs_internal(set_to_map(set
)));
8531 /* Align the divs of the basic maps in "map" to those
8532 * of the basic maps in "list", as well as to the other basic maps in "map".
8533 * The elements in "list" are assumed to have known divs.
8535 __isl_give isl_map
*isl_map_align_divs_to_basic_map_list(
8536 __isl_take isl_map
*map
, __isl_keep isl_basic_map_list
*list
)
8540 map
= isl_map_compute_divs(map
);
8541 map
= isl_map_cow(map
);
8543 return isl_map_free(map
);
8547 n
= isl_basic_map_list_n_basic_map(list
);
8548 for (i
= 0; i
< n
; ++i
) {
8549 isl_basic_map
*bmap
;
8551 bmap
= isl_basic_map_list_get_basic_map(list
, i
);
8552 map
->p
[0] = isl_basic_map_align_divs(map
->p
[0], bmap
);
8553 isl_basic_map_free(bmap
);
8556 return isl_map_free(map
);
8558 return isl_map_align_divs_internal(map
);
8561 /* Align the divs of each element of "list" to those of "bmap".
8562 * Both "bmap" and the elements of "list" are assumed to have known divs.
8564 __isl_give isl_basic_map_list
*isl_basic_map_list_align_divs_to_basic_map(
8565 __isl_take isl_basic_map_list
*list
, __isl_keep isl_basic_map
*bmap
)
8570 return isl_basic_map_list_free(list
);
8572 n
= isl_basic_map_list_n_basic_map(list
);
8573 for (i
= 0; i
< n
; ++i
) {
8574 isl_basic_map
*bmap_i
;
8576 bmap_i
= isl_basic_map_list_get_basic_map(list
, i
);
8577 bmap_i
= isl_basic_map_align_divs(bmap_i
, bmap
);
8578 list
= isl_basic_map_list_set_basic_map(list
, i
, bmap_i
);
8584 static __isl_give isl_set
*set_apply( __isl_take isl_set
*set
,
8585 __isl_take isl_map
*map
)
8589 ok
= isl_map_compatible_domain(map
, set
);
8593 isl_die(isl_set_get_ctx(set
), isl_error_invalid
,
8594 "incompatible spaces", goto error
);
8595 map
= isl_map_intersect_domain(map
, set
);
8596 set
= isl_map_range(map
);
8604 __isl_give isl_set
*isl_set_apply( __isl_take isl_set
*set
,
8605 __isl_take isl_map
*map
)
8607 return isl_map_align_params_map_map_and(set
, map
, &set_apply
);
8610 /* There is no need to cow as removing empty parts doesn't change
8611 * the meaning of the set.
8613 struct isl_map
*isl_map_remove_empty_parts(struct isl_map
*map
)
8620 for (i
= map
->n
- 1; i
>= 0; --i
)
8621 remove_if_empty(map
, i
);
8626 struct isl_set
*isl_set_remove_empty_parts(struct isl_set
*set
)
8628 return set_from_map(isl_map_remove_empty_parts(set_to_map(set
)));
8631 /* Given two basic sets bset1 and bset2, compute the maximal difference
8632 * between the values of dimension pos in bset1 and those in bset2
8633 * for any common value of the parameters and dimensions preceding pos.
8635 static enum isl_lp_result
basic_set_maximal_difference_at(
8636 __isl_keep isl_basic_set
*bset1
, __isl_keep isl_basic_set
*bset2
,
8637 int pos
, isl_int
*opt
)
8639 isl_basic_map
*bmap1
;
8640 isl_basic_map
*bmap2
;
8641 struct isl_ctx
*ctx
;
8642 struct isl_vec
*obj
;
8646 enum isl_lp_result res
;
8648 if (!bset1
|| !bset2
)
8649 return isl_lp_error
;
8651 nparam
= isl_basic_set_n_param(bset1
);
8652 dim1
= isl_basic_set_n_dim(bset1
);
8654 bmap1
= isl_basic_map_from_range(isl_basic_set_copy(bset1
));
8655 bmap2
= isl_basic_map_from_range(isl_basic_set_copy(bset2
));
8656 bmap1
= isl_basic_map_move_dims(bmap1
, isl_dim_in
, 0,
8657 isl_dim_out
, 0, pos
);
8658 bmap2
= isl_basic_map_move_dims(bmap2
, isl_dim_in
, 0,
8659 isl_dim_out
, 0, pos
);
8660 bmap1
= isl_basic_map_range_product(bmap1
, bmap2
);
8662 return isl_lp_error
;
8664 total
= isl_basic_map_total_dim(bmap1
);
8666 obj
= isl_vec_alloc(ctx
, 1 + total
);
8669 isl_seq_clr(obj
->block
.data
, 1 + total
);
8670 isl_int_set_si(obj
->block
.data
[1+nparam
+pos
], 1);
8671 isl_int_set_si(obj
->block
.data
[1+nparam
+pos
+(dim1
-pos
)], -1);
8672 res
= isl_basic_map_solve_lp(bmap1
, 1, obj
->block
.data
, ctx
->one
,
8674 isl_basic_map_free(bmap1
);
8678 isl_basic_map_free(bmap1
);
8679 return isl_lp_error
;
8682 /* Given two _disjoint_ basic sets bset1 and bset2, check whether
8683 * for any common value of the parameters and dimensions preceding pos
8684 * in both basic sets, the values of dimension pos in bset1 are
8685 * smaller or larger than those in bset2.
8688 * 1 if bset1 follows bset2
8689 * -1 if bset1 precedes bset2
8690 * 0 if bset1 and bset2 are incomparable
8691 * -2 if some error occurred.
8693 int isl_basic_set_compare_at(struct isl_basic_set
*bset1
,
8694 struct isl_basic_set
*bset2
, int pos
)
8697 enum isl_lp_result res
;
8702 res
= basic_set_maximal_difference_at(bset1
, bset2
, pos
, &opt
);
8704 if (res
== isl_lp_empty
)
8706 else if ((res
== isl_lp_ok
&& isl_int_is_pos(opt
)) ||
8707 res
== isl_lp_unbounded
)
8709 else if (res
== isl_lp_ok
&& isl_int_is_neg(opt
))
8718 /* Given two basic sets bset1 and bset2, check whether
8719 * for any common value of the parameters and dimensions preceding pos
8720 * there is a value of dimension pos in bset1 that is larger
8721 * than a value of the same dimension in bset2.
8724 * 1 if there exists such a pair
8725 * 0 if there is no such pair, but there is a pair of equal values
8727 * -2 if some error occurred.
8729 int isl_basic_set_follows_at(__isl_keep isl_basic_set
*bset1
,
8730 __isl_keep isl_basic_set
*bset2
, int pos
)
8733 enum isl_lp_result res
;
8738 res
= basic_set_maximal_difference_at(bset1
, bset2
, pos
, &opt
);
8740 if (res
== isl_lp_empty
)
8742 else if ((res
== isl_lp_ok
&& isl_int_is_pos(opt
)) ||
8743 res
== isl_lp_unbounded
)
8745 else if (res
== isl_lp_ok
&& isl_int_is_neg(opt
))
8747 else if (res
== isl_lp_ok
)
8756 /* Given two sets set1 and set2, check whether
8757 * for any common value of the parameters and dimensions preceding pos
8758 * there is a value of dimension pos in set1 that is larger
8759 * than a value of the same dimension in set2.
8762 * 1 if there exists such a pair
8763 * 0 if there is no such pair, but there is a pair of equal values
8765 * -2 if some error occurred.
8767 int isl_set_follows_at(__isl_keep isl_set
*set1
,
8768 __isl_keep isl_set
*set2
, int pos
)
8776 for (i
= 0; i
< set1
->n
; ++i
)
8777 for (j
= 0; j
< set2
->n
; ++j
) {
8779 f
= isl_basic_set_follows_at(set1
->p
[i
], set2
->p
[j
], pos
);
8780 if (f
== 1 || f
== -2)
8789 static isl_bool
isl_basic_map_plain_has_fixed_var(
8790 __isl_keep isl_basic_map
*bmap
, unsigned pos
, isl_int
*val
)
8797 return isl_bool_error
;
8798 total
= isl_basic_map_total_dim(bmap
);
8799 for (i
= 0, d
= total
-1; i
< bmap
->n_eq
&& d
+1 > pos
; ++i
) {
8800 for (; d
+1 > pos
; --d
)
8801 if (!isl_int_is_zero(bmap
->eq
[i
][1+d
]))
8805 if (isl_seq_first_non_zero(bmap
->eq
[i
]+1, d
) != -1)
8806 return isl_bool_false
;
8807 if (isl_seq_first_non_zero(bmap
->eq
[i
]+1+d
+1, total
-d
-1) != -1)
8808 return isl_bool_false
;
8809 if (!isl_int_is_one(bmap
->eq
[i
][1+d
]))
8810 return isl_bool_false
;
8812 isl_int_neg(*val
, bmap
->eq
[i
][0]);
8813 return isl_bool_true
;
8815 return isl_bool_false
;
8818 static isl_bool
isl_map_plain_has_fixed_var(__isl_keep isl_map
*map
,
8819 unsigned pos
, isl_int
*val
)
8827 return isl_bool_error
;
8829 return isl_bool_false
;
8831 return isl_basic_map_plain_has_fixed_var(map
->p
[0], pos
, val
);
8834 fixed
= isl_basic_map_plain_has_fixed_var(map
->p
[0], pos
, &v
);
8835 for (i
= 1; fixed
== isl_bool_true
&& i
< map
->n
; ++i
) {
8836 fixed
= isl_basic_map_plain_has_fixed_var(map
->p
[i
], pos
, &tmp
);
8837 if (fixed
== isl_bool_true
&& isl_int_ne(tmp
, v
))
8838 fixed
= isl_bool_false
;
8841 isl_int_set(*val
, v
);
8847 static isl_bool
isl_basic_set_plain_has_fixed_var(
8848 __isl_keep isl_basic_set
*bset
, unsigned pos
, isl_int
*val
)
8850 return isl_basic_map_plain_has_fixed_var(bset_to_bmap(bset
),
8854 isl_bool
isl_basic_map_plain_is_fixed(__isl_keep isl_basic_map
*bmap
,
8855 enum isl_dim_type type
, unsigned pos
, isl_int
*val
)
8857 if (isl_basic_map_check_range(bmap
, type
, pos
, 1) < 0)
8858 return isl_bool_error
;
8859 return isl_basic_map_plain_has_fixed_var(bmap
,
8860 isl_basic_map_offset(bmap
, type
) - 1 + pos
, val
);
8863 /* If "bmap" obviously lies on a hyperplane where the given dimension
8864 * has a fixed value, then return that value.
8865 * Otherwise return NaN.
8867 __isl_give isl_val
*isl_basic_map_plain_get_val_if_fixed(
8868 __isl_keep isl_basic_map
*bmap
,
8869 enum isl_dim_type type
, unsigned pos
)
8877 ctx
= isl_basic_map_get_ctx(bmap
);
8878 v
= isl_val_alloc(ctx
);
8881 fixed
= isl_basic_map_plain_is_fixed(bmap
, type
, pos
, &v
->n
);
8883 return isl_val_free(v
);
8885 isl_int_set_si(v
->d
, 1);
8889 return isl_val_nan(ctx
);
8892 isl_bool
isl_map_plain_is_fixed(__isl_keep isl_map
*map
,
8893 enum isl_dim_type type
, unsigned pos
, isl_int
*val
)
8895 if (pos
>= isl_map_dim(map
, type
))
8896 isl_die(isl_map_get_ctx(map
), isl_error_invalid
,
8897 "position out of bounds", return isl_bool_error
);
8898 return isl_map_plain_has_fixed_var(map
,
8899 map_offset(map
, type
) - 1 + pos
, val
);
8902 /* If "map" obviously lies on a hyperplane where the given dimension
8903 * has a fixed value, then return that value.
8904 * Otherwise return NaN.
8906 __isl_give isl_val
*isl_map_plain_get_val_if_fixed(__isl_keep isl_map
*map
,
8907 enum isl_dim_type type
, unsigned pos
)
8915 ctx
= isl_map_get_ctx(map
);
8916 v
= isl_val_alloc(ctx
);
8919 fixed
= isl_map_plain_is_fixed(map
, type
, pos
, &v
->n
);
8921 return isl_val_free(v
);
8923 isl_int_set_si(v
->d
, 1);
8927 return isl_val_nan(ctx
);
8930 /* If "set" obviously lies on a hyperplane where the given dimension
8931 * has a fixed value, then return that value.
8932 * Otherwise return NaN.
8934 __isl_give isl_val
*isl_set_plain_get_val_if_fixed(__isl_keep isl_set
*set
,
8935 enum isl_dim_type type
, unsigned pos
)
8937 return isl_map_plain_get_val_if_fixed(set
, type
, pos
);
8940 isl_bool
isl_set_plain_is_fixed(__isl_keep isl_set
*set
,
8941 enum isl_dim_type type
, unsigned pos
, isl_int
*val
)
8943 return isl_map_plain_is_fixed(set
, type
, pos
, val
);
8946 /* Check if dimension dim has fixed value and if so and if val is not NULL,
8947 * then return this fixed value in *val.
8949 isl_bool
isl_basic_set_plain_dim_is_fixed(__isl_keep isl_basic_set
*bset
,
8950 unsigned dim
, isl_int
*val
)
8952 return isl_basic_set_plain_has_fixed_var(bset
,
8953 isl_basic_set_n_param(bset
) + dim
, val
);
8956 /* Return -1 if the constraint "c1" should be sorted before "c2"
8957 * and 1 if it should be sorted after "c2".
8958 * Return 0 if the two constraints are the same (up to the constant term).
8960 * In particular, if a constraint involves later variables than another
8961 * then it is sorted after this other constraint.
8962 * uset_gist depends on constraints without existentially quantified
8963 * variables sorting first.
8965 * For constraints that have the same latest variable, those
8966 * with the same coefficient for this latest variable (first in absolute value
8967 * and then in actual value) are grouped together.
8968 * This is useful for detecting pairs of constraints that can
8969 * be chained in their printed representation.
8971 * Finally, within a group, constraints are sorted according to
8972 * their coefficients (excluding the constant term).
8974 static int sort_constraint_cmp(const void *p1
, const void *p2
, void *arg
)
8976 isl_int
**c1
= (isl_int
**) p1
;
8977 isl_int
**c2
= (isl_int
**) p2
;
8979 unsigned size
= *(unsigned *) arg
;
8982 l1
= isl_seq_last_non_zero(*c1
+ 1, size
);
8983 l2
= isl_seq_last_non_zero(*c2
+ 1, size
);
8988 cmp
= isl_int_abs_cmp((*c1
)[1 + l1
], (*c2
)[1 + l1
]);
8991 cmp
= isl_int_cmp((*c1
)[1 + l1
], (*c2
)[1 + l1
]);
8995 return isl_seq_cmp(*c1
+ 1, *c2
+ 1, size
);
8998 /* Return -1 if the constraint "c1" of "bmap" is sorted before "c2"
8999 * by isl_basic_map_sort_constraints, 1 if it is sorted after "c2"
9000 * and 0 if the two constraints are the same (up to the constant term).
9002 int isl_basic_map_constraint_cmp(__isl_keep isl_basic_map
*bmap
,
9003 isl_int
*c1
, isl_int
*c2
)
9009 total
= isl_basic_map_total_dim(bmap
);
9010 return sort_constraint_cmp(&c1
, &c2
, &total
);
9013 __isl_give isl_basic_map
*isl_basic_map_sort_constraints(
9014 __isl_take isl_basic_map
*bmap
)
9020 if (bmap
->n_ineq
== 0)
9022 if (ISL_F_ISSET(bmap
, ISL_BASIC_MAP_NORMALIZED
))
9024 total
= isl_basic_map_total_dim(bmap
);
9025 if (isl_sort(bmap
->ineq
, bmap
->n_ineq
, sizeof(isl_int
*),
9026 &sort_constraint_cmp
, &total
) < 0)
9027 return isl_basic_map_free(bmap
);
9031 __isl_give isl_basic_set
*isl_basic_set_sort_constraints(
9032 __isl_take isl_basic_set
*bset
)
9034 isl_basic_map
*bmap
= bset_to_bmap(bset
);
9035 return bset_from_bmap(isl_basic_map_sort_constraints(bmap
));
9038 struct isl_basic_map
*isl_basic_map_normalize(struct isl_basic_map
*bmap
)
9042 if (ISL_F_ISSET(bmap
, ISL_BASIC_MAP_NORMALIZED
))
9044 bmap
= isl_basic_map_remove_redundancies(bmap
);
9045 bmap
= isl_basic_map_sort_constraints(bmap
);
9047 ISL_F_SET(bmap
, ISL_BASIC_MAP_NORMALIZED
);
9050 int isl_basic_map_plain_cmp(__isl_keep isl_basic_map
*bmap1
,
9051 __isl_keep isl_basic_map
*bmap2
)
9055 isl_space
*space1
, *space2
;
9057 if (!bmap1
|| !bmap2
)
9062 space1
= isl_basic_map_peek_space(bmap1
);
9063 space2
= isl_basic_map_peek_space(bmap2
);
9064 cmp
= isl_space_cmp(space1
, space2
);
9067 if (ISL_F_ISSET(bmap1
, ISL_BASIC_MAP_RATIONAL
) !=
9068 ISL_F_ISSET(bmap2
, ISL_BASIC_MAP_RATIONAL
))
9069 return ISL_F_ISSET(bmap1
, ISL_BASIC_MAP_RATIONAL
) ? -1 : 1;
9070 if (ISL_F_ISSET(bmap1
, ISL_BASIC_MAP_EMPTY
) &&
9071 ISL_F_ISSET(bmap2
, ISL_BASIC_MAP_EMPTY
))
9073 if (ISL_F_ISSET(bmap1
, ISL_BASIC_MAP_EMPTY
))
9075 if (ISL_F_ISSET(bmap2
, ISL_BASIC_MAP_EMPTY
))
9077 if (bmap1
->n_eq
!= bmap2
->n_eq
)
9078 return bmap1
->n_eq
- bmap2
->n_eq
;
9079 if (bmap1
->n_ineq
!= bmap2
->n_ineq
)
9080 return bmap1
->n_ineq
- bmap2
->n_ineq
;
9081 if (bmap1
->n_div
!= bmap2
->n_div
)
9082 return bmap1
->n_div
- bmap2
->n_div
;
9083 total
= isl_basic_map_total_dim(bmap1
);
9084 for (i
= 0; i
< bmap1
->n_eq
; ++i
) {
9085 cmp
= isl_seq_cmp(bmap1
->eq
[i
], bmap2
->eq
[i
], 1+total
);
9089 for (i
= 0; i
< bmap1
->n_ineq
; ++i
) {
9090 cmp
= isl_seq_cmp(bmap1
->ineq
[i
], bmap2
->ineq
[i
], 1+total
);
9094 for (i
= 0; i
< bmap1
->n_div
; ++i
) {
9095 cmp
= isl_seq_cmp(bmap1
->div
[i
], bmap2
->div
[i
], 1+1+total
);
9102 int isl_basic_set_plain_cmp(__isl_keep isl_basic_set
*bset1
,
9103 __isl_keep isl_basic_set
*bset2
)
9105 return isl_basic_map_plain_cmp(bset1
, bset2
);
9108 int isl_set_plain_cmp(__isl_keep isl_set
*set1
, __isl_keep isl_set
*set2
)
9114 if (set1
->n
!= set2
->n
)
9115 return set1
->n
- set2
->n
;
9117 for (i
= 0; i
< set1
->n
; ++i
) {
9118 cmp
= isl_basic_set_plain_cmp(set1
->p
[i
], set2
->p
[i
]);
9126 isl_bool
isl_basic_map_plain_is_equal(__isl_keep isl_basic_map
*bmap1
,
9127 __isl_keep isl_basic_map
*bmap2
)
9129 if (!bmap1
|| !bmap2
)
9130 return isl_bool_error
;
9131 return isl_basic_map_plain_cmp(bmap1
, bmap2
) == 0;
9134 isl_bool
isl_basic_set_plain_is_equal(__isl_keep isl_basic_set
*bset1
,
9135 __isl_keep isl_basic_set
*bset2
)
9137 return isl_basic_map_plain_is_equal(bset_to_bmap(bset1
),
9138 bset_to_bmap(bset2
));
9141 static int qsort_bmap_cmp(const void *p1
, const void *p2
)
9143 isl_basic_map
*bmap1
= *(isl_basic_map
**) p1
;
9144 isl_basic_map
*bmap2
= *(isl_basic_map
**) p2
;
9146 return isl_basic_map_plain_cmp(bmap1
, bmap2
);
9149 /* Sort the basic maps of "map" and remove duplicate basic maps.
9151 * While removing basic maps, we make sure that the basic maps remain
9152 * sorted because isl_map_normalize expects the basic maps of the result
9155 static __isl_give isl_map
*sort_and_remove_duplicates(__isl_take isl_map
*map
)
9159 map
= isl_map_remove_empty_parts(map
);
9162 qsort(map
->p
, map
->n
, sizeof(struct isl_basic_map
*), qsort_bmap_cmp
);
9163 for (i
= map
->n
- 1; i
>= 1; --i
) {
9164 if (!isl_basic_map_plain_is_equal(map
->p
[i
- 1], map
->p
[i
]))
9166 isl_basic_map_free(map
->p
[i
-1]);
9167 for (j
= i
; j
< map
->n
; ++j
)
9168 map
->p
[j
- 1] = map
->p
[j
];
9175 /* Remove obvious duplicates among the basic maps of "map".
9177 * Unlike isl_map_normalize, this function does not remove redundant
9178 * constraints and only removes duplicates that have exactly the same
9179 * constraints in the input. It does sort the constraints and
9180 * the basic maps to ease the detection of duplicates.
9182 * If "map" has already been normalized or if the basic maps are
9183 * disjoint, then there can be no duplicates.
9185 __isl_give isl_map
*isl_map_remove_obvious_duplicates(__isl_take isl_map
*map
)
9188 isl_basic_map
*bmap
;
9194 if (ISL_F_ISSET(map
, ISL_MAP_NORMALIZED
| ISL_MAP_DISJOINT
))
9196 for (i
= 0; i
< map
->n
; ++i
) {
9197 bmap
= isl_basic_map_copy(map
->p
[i
]);
9198 bmap
= isl_basic_map_sort_constraints(bmap
);
9200 return isl_map_free(map
);
9201 isl_basic_map_free(map
->p
[i
]);
9205 map
= sort_and_remove_duplicates(map
);
9209 /* We normalize in place, but if anything goes wrong we need
9210 * to return NULL, so we need to make sure we don't change the
9211 * meaning of any possible other copies of map.
9213 __isl_give isl_map
*isl_map_normalize(__isl_take isl_map
*map
)
9216 struct isl_basic_map
*bmap
;
9220 if (ISL_F_ISSET(map
, ISL_MAP_NORMALIZED
))
9222 for (i
= 0; i
< map
->n
; ++i
) {
9223 bmap
= isl_basic_map_normalize(isl_basic_map_copy(map
->p
[i
]));
9226 isl_basic_map_free(map
->p
[i
]);
9230 map
= sort_and_remove_duplicates(map
);
9232 ISL_F_SET(map
, ISL_MAP_NORMALIZED
);
9239 struct isl_set
*isl_set_normalize(struct isl_set
*set
)
9241 return set_from_map(isl_map_normalize(set_to_map(set
)));
9244 isl_bool
isl_map_plain_is_equal(__isl_keep isl_map
*map1
,
9245 __isl_keep isl_map
*map2
)
9251 return isl_bool_error
;
9254 return isl_bool_true
;
9255 if (!isl_space_is_equal(map1
->dim
, map2
->dim
))
9256 return isl_bool_false
;
9258 map1
= isl_map_copy(map1
);
9259 map2
= isl_map_copy(map2
);
9260 map1
= isl_map_normalize(map1
);
9261 map2
= isl_map_normalize(map2
);
9264 equal
= map1
->n
== map2
->n
;
9265 for (i
= 0; equal
&& i
< map1
->n
; ++i
) {
9266 equal
= isl_basic_map_plain_is_equal(map1
->p
[i
], map2
->p
[i
]);
9276 return isl_bool_error
;
9279 isl_bool
isl_set_plain_is_equal(__isl_keep isl_set
*set1
,
9280 __isl_keep isl_set
*set2
)
9282 return isl_map_plain_is_equal(set_to_map(set1
), set_to_map(set2
));
9285 /* Return the basic maps in "map" as a list.
9287 __isl_give isl_basic_map_list
*isl_map_get_basic_map_list(
9288 __isl_keep isl_map
*map
)
9292 isl_basic_map_list
*list
;
9296 ctx
= isl_map_get_ctx(map
);
9297 list
= isl_basic_map_list_alloc(ctx
, map
->n
);
9299 for (i
= 0; i
< map
->n
; ++i
) {
9300 isl_basic_map
*bmap
;
9302 bmap
= isl_basic_map_copy(map
->p
[i
]);
9303 list
= isl_basic_map_list_add(list
, bmap
);
9309 /* Return the intersection of the elements in the non-empty list "list".
9310 * All elements are assumed to live in the same space.
9312 __isl_give isl_basic_map
*isl_basic_map_list_intersect(
9313 __isl_take isl_basic_map_list
*list
)
9316 isl_basic_map
*bmap
;
9320 n
= isl_basic_map_list_n_basic_map(list
);
9322 isl_die(isl_basic_map_list_get_ctx(list
), isl_error_invalid
,
9323 "expecting non-empty list", goto error
);
9325 bmap
= isl_basic_map_list_get_basic_map(list
, 0);
9326 for (i
= 1; i
< n
; ++i
) {
9327 isl_basic_map
*bmap_i
;
9329 bmap_i
= isl_basic_map_list_get_basic_map(list
, i
);
9330 bmap
= isl_basic_map_intersect(bmap
, bmap_i
);
9333 isl_basic_map_list_free(list
);
9336 isl_basic_map_list_free(list
);
9340 /* Return the intersection of the elements in the non-empty list "list".
9341 * All elements are assumed to live in the same space.
9343 __isl_give isl_basic_set
*isl_basic_set_list_intersect(
9344 __isl_take isl_basic_set_list
*list
)
9346 return isl_basic_map_list_intersect(list
);
9349 /* Return the union of the elements of "list".
9350 * The list is required to have at least one element.
9352 __isl_give isl_set
*isl_basic_set_list_union(
9353 __isl_take isl_basic_set_list
*list
)
9357 isl_basic_set
*bset
;
9362 n
= isl_basic_set_list_n_basic_set(list
);
9364 isl_die(isl_basic_set_list_get_ctx(list
), isl_error_invalid
,
9365 "expecting non-empty list", goto error
);
9367 bset
= isl_basic_set_list_get_basic_set(list
, 0);
9368 space
= isl_basic_set_get_space(bset
);
9369 isl_basic_set_free(bset
);
9371 set
= isl_set_alloc_space(space
, n
, 0);
9372 for (i
= 0; i
< n
; ++i
) {
9373 bset
= isl_basic_set_list_get_basic_set(list
, i
);
9374 set
= isl_set_add_basic_set(set
, bset
);
9377 isl_basic_set_list_free(list
);
9380 isl_basic_set_list_free(list
);
9384 /* Return the union of the elements in the non-empty list "list".
9385 * All elements are assumed to live in the same space.
9387 __isl_give isl_set
*isl_set_list_union(__isl_take isl_set_list
*list
)
9394 n
= isl_set_list_n_set(list
);
9396 isl_die(isl_set_list_get_ctx(list
), isl_error_invalid
,
9397 "expecting non-empty list", goto error
);
9399 set
= isl_set_list_get_set(list
, 0);
9400 for (i
= 1; i
< n
; ++i
) {
9403 set_i
= isl_set_list_get_set(list
, i
);
9404 set
= isl_set_union(set
, set_i
);
9407 isl_set_list_free(list
);
9410 isl_set_list_free(list
);
9414 struct isl_basic_map
*isl_basic_map_product(
9415 struct isl_basic_map
*bmap1
, struct isl_basic_map
*bmap2
)
9417 isl_space
*dim_result
= NULL
;
9418 struct isl_basic_map
*bmap
;
9419 unsigned in1
, in2
, out1
, out2
, nparam
, total
, pos
;
9420 struct isl_dim_map
*dim_map1
, *dim_map2
;
9422 if (isl_basic_map_check_equal_params(bmap1
, bmap2
) < 0)
9424 dim_result
= isl_space_product(isl_space_copy(bmap1
->dim
),
9425 isl_space_copy(bmap2
->dim
));
9427 in1
= isl_basic_map_dim(bmap1
, isl_dim_in
);
9428 in2
= isl_basic_map_dim(bmap2
, isl_dim_in
);
9429 out1
= isl_basic_map_dim(bmap1
, isl_dim_out
);
9430 out2
= isl_basic_map_dim(bmap2
, isl_dim_out
);
9431 nparam
= isl_basic_map_dim(bmap1
, isl_dim_param
);
9433 total
= nparam
+ in1
+ in2
+ out1
+ out2
+ bmap1
->n_div
+ bmap2
->n_div
;
9434 dim_map1
= isl_dim_map_alloc(bmap1
->ctx
, total
);
9435 dim_map2
= isl_dim_map_alloc(bmap1
->ctx
, total
);
9436 isl_dim_map_dim(dim_map1
, bmap1
->dim
, isl_dim_param
, pos
= 0);
9437 isl_dim_map_dim(dim_map2
, bmap2
->dim
, isl_dim_param
, pos
= 0);
9438 isl_dim_map_dim(dim_map1
, bmap1
->dim
, isl_dim_in
, pos
+= nparam
);
9439 isl_dim_map_dim(dim_map2
, bmap2
->dim
, isl_dim_in
, pos
+= in1
);
9440 isl_dim_map_dim(dim_map1
, bmap1
->dim
, isl_dim_out
, pos
+= in2
);
9441 isl_dim_map_dim(dim_map2
, bmap2
->dim
, isl_dim_out
, pos
+= out1
);
9442 isl_dim_map_div(dim_map1
, bmap1
, pos
+= out2
);
9443 isl_dim_map_div(dim_map2
, bmap2
, pos
+= bmap1
->n_div
);
9445 bmap
= isl_basic_map_alloc_space(dim_result
,
9446 bmap1
->n_div
+ bmap2
->n_div
,
9447 bmap1
->n_eq
+ bmap2
->n_eq
,
9448 bmap1
->n_ineq
+ bmap2
->n_ineq
);
9449 bmap
= isl_basic_map_add_constraints_dim_map(bmap
, bmap1
, dim_map1
);
9450 bmap
= isl_basic_map_add_constraints_dim_map(bmap
, bmap2
, dim_map2
);
9451 bmap
= isl_basic_map_simplify(bmap
);
9452 return isl_basic_map_finalize(bmap
);
9454 isl_basic_map_free(bmap1
);
9455 isl_basic_map_free(bmap2
);
9459 __isl_give isl_basic_map
*isl_basic_map_flat_product(
9460 __isl_take isl_basic_map
*bmap1
, __isl_take isl_basic_map
*bmap2
)
9462 isl_basic_map
*prod
;
9464 prod
= isl_basic_map_product(bmap1
, bmap2
);
9465 prod
= isl_basic_map_flatten(prod
);
9469 __isl_give isl_basic_set
*isl_basic_set_flat_product(
9470 __isl_take isl_basic_set
*bset1
, __isl_take isl_basic_set
*bset2
)
9472 return isl_basic_map_flat_range_product(bset1
, bset2
);
9475 __isl_give isl_basic_map
*isl_basic_map_domain_product(
9476 __isl_take isl_basic_map
*bmap1
, __isl_take isl_basic_map
*bmap2
)
9478 isl_space
*space_result
= NULL
;
9479 isl_basic_map
*bmap
;
9480 unsigned in1
, in2
, out
, nparam
, total
, pos
;
9481 struct isl_dim_map
*dim_map1
, *dim_map2
;
9483 if (!bmap1
|| !bmap2
)
9486 space_result
= isl_space_domain_product(isl_space_copy(bmap1
->dim
),
9487 isl_space_copy(bmap2
->dim
));
9489 in1
= isl_basic_map_dim(bmap1
, isl_dim_in
);
9490 in2
= isl_basic_map_dim(bmap2
, isl_dim_in
);
9491 out
= isl_basic_map_dim(bmap1
, isl_dim_out
);
9492 nparam
= isl_basic_map_dim(bmap1
, isl_dim_param
);
9494 total
= nparam
+ in1
+ in2
+ out
+ bmap1
->n_div
+ bmap2
->n_div
;
9495 dim_map1
= isl_dim_map_alloc(bmap1
->ctx
, total
);
9496 dim_map2
= isl_dim_map_alloc(bmap1
->ctx
, total
);
9497 isl_dim_map_dim(dim_map1
, bmap1
->dim
, isl_dim_param
, pos
= 0);
9498 isl_dim_map_dim(dim_map2
, bmap2
->dim
, isl_dim_param
, pos
= 0);
9499 isl_dim_map_dim(dim_map1
, bmap1
->dim
, isl_dim_in
, pos
+= nparam
);
9500 isl_dim_map_dim(dim_map2
, bmap2
->dim
, isl_dim_in
, pos
+= in1
);
9501 isl_dim_map_dim(dim_map1
, bmap1
->dim
, isl_dim_out
, pos
+= in2
);
9502 isl_dim_map_dim(dim_map2
, bmap2
->dim
, isl_dim_out
, pos
);
9503 isl_dim_map_div(dim_map1
, bmap1
, pos
+= out
);
9504 isl_dim_map_div(dim_map2
, bmap2
, pos
+= bmap1
->n_div
);
9506 bmap
= isl_basic_map_alloc_space(space_result
,
9507 bmap1
->n_div
+ bmap2
->n_div
,
9508 bmap1
->n_eq
+ bmap2
->n_eq
,
9509 bmap1
->n_ineq
+ bmap2
->n_ineq
);
9510 bmap
= isl_basic_map_add_constraints_dim_map(bmap
, bmap1
, dim_map1
);
9511 bmap
= isl_basic_map_add_constraints_dim_map(bmap
, bmap2
, dim_map2
);
9512 bmap
= isl_basic_map_simplify(bmap
);
9513 return isl_basic_map_finalize(bmap
);
9515 isl_basic_map_free(bmap1
);
9516 isl_basic_map_free(bmap2
);
9520 __isl_give isl_basic_map
*isl_basic_map_range_product(
9521 __isl_take isl_basic_map
*bmap1
, __isl_take isl_basic_map
*bmap2
)
9524 isl_space
*dim_result
= NULL
;
9525 isl_basic_map
*bmap
;
9526 unsigned in
, out1
, out2
, nparam
, total
, pos
;
9527 struct isl_dim_map
*dim_map1
, *dim_map2
;
9529 rational
= isl_basic_map_is_rational(bmap1
);
9530 if (rational
>= 0 && rational
)
9531 rational
= isl_basic_map_is_rational(bmap2
);
9532 if (!bmap1
|| !bmap2
|| rational
< 0)
9535 if (isl_basic_map_check_equal_params(bmap1
, bmap2
) < 0)
9538 dim_result
= isl_space_range_product(isl_space_copy(bmap1
->dim
),
9539 isl_space_copy(bmap2
->dim
));
9541 in
= isl_basic_map_dim(bmap1
, isl_dim_in
);
9542 out1
= isl_basic_map_dim(bmap1
, isl_dim_out
);
9543 out2
= isl_basic_map_dim(bmap2
, isl_dim_out
);
9544 nparam
= isl_basic_map_dim(bmap1
, isl_dim_param
);
9546 total
= nparam
+ in
+ out1
+ out2
+ bmap1
->n_div
+ bmap2
->n_div
;
9547 dim_map1
= isl_dim_map_alloc(bmap1
->ctx
, total
);
9548 dim_map2
= isl_dim_map_alloc(bmap1
->ctx
, total
);
9549 isl_dim_map_dim(dim_map1
, bmap1
->dim
, isl_dim_param
, pos
= 0);
9550 isl_dim_map_dim(dim_map2
, bmap2
->dim
, isl_dim_param
, pos
= 0);
9551 isl_dim_map_dim(dim_map1
, bmap1
->dim
, isl_dim_in
, pos
+= nparam
);
9552 isl_dim_map_dim(dim_map2
, bmap2
->dim
, isl_dim_in
, pos
);
9553 isl_dim_map_dim(dim_map1
, bmap1
->dim
, isl_dim_out
, pos
+= in
);
9554 isl_dim_map_dim(dim_map2
, bmap2
->dim
, isl_dim_out
, pos
+= out1
);
9555 isl_dim_map_div(dim_map1
, bmap1
, pos
+= out2
);
9556 isl_dim_map_div(dim_map2
, bmap2
, pos
+= bmap1
->n_div
);
9558 bmap
= isl_basic_map_alloc_space(dim_result
,
9559 bmap1
->n_div
+ bmap2
->n_div
,
9560 bmap1
->n_eq
+ bmap2
->n_eq
,
9561 bmap1
->n_ineq
+ bmap2
->n_ineq
);
9562 bmap
= isl_basic_map_add_constraints_dim_map(bmap
, bmap1
, dim_map1
);
9563 bmap
= isl_basic_map_add_constraints_dim_map(bmap
, bmap2
, dim_map2
);
9565 bmap
= isl_basic_map_set_rational(bmap
);
9566 bmap
= isl_basic_map_simplify(bmap
);
9567 return isl_basic_map_finalize(bmap
);
9569 isl_basic_map_free(bmap1
);
9570 isl_basic_map_free(bmap2
);
9574 __isl_give isl_basic_map
*isl_basic_map_flat_range_product(
9575 __isl_take isl_basic_map
*bmap1
, __isl_take isl_basic_map
*bmap2
)
9577 isl_basic_map
*prod
;
9579 prod
= isl_basic_map_range_product(bmap1
, bmap2
);
9580 prod
= isl_basic_map_flatten_range(prod
);
9584 /* Apply "basic_map_product" to each pair of basic maps in "map1" and "map2"
9585 * and collect the results.
9586 * The result live in the space obtained by calling "space_product"
9587 * on the spaces of "map1" and "map2".
9588 * If "remove_duplicates" is set then the result may contain duplicates
9589 * (even if the inputs do not) and so we try and remove the obvious
9592 static __isl_give isl_map
*map_product(__isl_take isl_map
*map1
,
9593 __isl_take isl_map
*map2
,
9594 __isl_give isl_space
*(*space_product
)(__isl_take isl_space
*left
,
9595 __isl_take isl_space
*right
),
9596 __isl_give isl_basic_map
*(*basic_map_product
)(
9597 __isl_take isl_basic_map
*left
,
9598 __isl_take isl_basic_map
*right
),
9599 int remove_duplicates
)
9602 struct isl_map
*result
;
9606 m
= isl_map_has_equal_params(map1
, map2
);
9610 isl_die(isl_map_get_ctx(map1
), isl_error_invalid
,
9611 "parameters don't match", goto error
);
9613 if (ISL_F_ISSET(map1
, ISL_MAP_DISJOINT
) &&
9614 ISL_F_ISSET(map2
, ISL_MAP_DISJOINT
))
9615 ISL_FL_SET(flags
, ISL_MAP_DISJOINT
);
9617 result
= isl_map_alloc_space(space_product(isl_space_copy(map1
->dim
),
9618 isl_space_copy(map2
->dim
)),
9619 map1
->n
* map2
->n
, flags
);
9622 for (i
= 0; i
< map1
->n
; ++i
)
9623 for (j
= 0; j
< map2
->n
; ++j
) {
9624 struct isl_basic_map
*part
;
9625 part
= basic_map_product(isl_basic_map_copy(map1
->p
[i
]),
9626 isl_basic_map_copy(map2
->p
[j
]));
9627 if (isl_basic_map_is_empty(part
))
9628 isl_basic_map_free(part
);
9630 result
= isl_map_add_basic_map(result
, part
);
9634 if (remove_duplicates
)
9635 result
= isl_map_remove_obvious_duplicates(result
);
9645 /* Given two maps A -> B and C -> D, construct a map [A -> C] -> [B -> D]
9647 static __isl_give isl_map
*map_product_aligned(__isl_take isl_map
*map1
,
9648 __isl_take isl_map
*map2
)
9650 return map_product(map1
, map2
, &isl_space_product
,
9651 &isl_basic_map_product
, 0);
9654 __isl_give isl_map
*isl_map_product(__isl_take isl_map
*map1
,
9655 __isl_take isl_map
*map2
)
9657 return isl_map_align_params_map_map_and(map1
, map2
, &map_product_aligned
);
9660 /* Given two maps A -> B and C -> D, construct a map (A, C) -> (B, D)
9662 __isl_give isl_map
*isl_map_flat_product(__isl_take isl_map
*map1
,
9663 __isl_take isl_map
*map2
)
9667 prod
= isl_map_product(map1
, map2
);
9668 prod
= isl_map_flatten(prod
);
9672 /* Given two set A and B, construct its Cartesian product A x B.
9674 struct isl_set
*isl_set_product(struct isl_set
*set1
, struct isl_set
*set2
)
9676 return isl_map_range_product(set1
, set2
);
9679 __isl_give isl_set
*isl_set_flat_product(__isl_take isl_set
*set1
,
9680 __isl_take isl_set
*set2
)
9682 return isl_map_flat_range_product(set1
, set2
);
9685 /* Given two maps A -> B and C -> D, construct a map [A -> C] -> (B * D)
9687 static __isl_give isl_map
*map_domain_product_aligned(__isl_take isl_map
*map1
,
9688 __isl_take isl_map
*map2
)
9690 return map_product(map1
, map2
, &isl_space_domain_product
,
9691 &isl_basic_map_domain_product
, 1);
9694 /* Given two maps A -> B and C -> D, construct a map (A * C) -> [B -> D]
9696 static __isl_give isl_map
*map_range_product_aligned(__isl_take isl_map
*map1
,
9697 __isl_take isl_map
*map2
)
9699 return map_product(map1
, map2
, &isl_space_range_product
,
9700 &isl_basic_map_range_product
, 1);
9703 __isl_give isl_map
*isl_map_domain_product(__isl_take isl_map
*map1
,
9704 __isl_take isl_map
*map2
)
9706 return isl_map_align_params_map_map_and(map1
, map2
,
9707 &map_domain_product_aligned
);
9710 __isl_give isl_map
*isl_map_range_product(__isl_take isl_map
*map1
,
9711 __isl_take isl_map
*map2
)
9713 return isl_map_align_params_map_map_and(map1
, map2
,
9714 &map_range_product_aligned
);
9717 /* Given a map of the form [A -> B] -> [C -> D], return the map A -> C.
9719 __isl_give isl_map
*isl_map_factor_domain(__isl_take isl_map
*map
)
9722 int total1
, keep1
, total2
, keep2
;
9726 if (!isl_space_domain_is_wrapping(map
->dim
) ||
9727 !isl_space_range_is_wrapping(map
->dim
))
9728 isl_die(isl_map_get_ctx(map
), isl_error_invalid
,
9729 "not a product", return isl_map_free(map
));
9731 space
= isl_map_get_space(map
);
9732 total1
= isl_space_dim(space
, isl_dim_in
);
9733 total2
= isl_space_dim(space
, isl_dim_out
);
9734 space
= isl_space_factor_domain(space
);
9735 keep1
= isl_space_dim(space
, isl_dim_in
);
9736 keep2
= isl_space_dim(space
, isl_dim_out
);
9737 map
= isl_map_project_out(map
, isl_dim_in
, keep1
, total1
- keep1
);
9738 map
= isl_map_project_out(map
, isl_dim_out
, keep2
, total2
- keep2
);
9739 map
= isl_map_reset_space(map
, space
);
9744 /* Given a map of the form [A -> B] -> [C -> D], return the map B -> D.
9746 __isl_give isl_map
*isl_map_factor_range(__isl_take isl_map
*map
)
9749 int total1
, keep1
, total2
, keep2
;
9753 if (!isl_space_domain_is_wrapping(map
->dim
) ||
9754 !isl_space_range_is_wrapping(map
->dim
))
9755 isl_die(isl_map_get_ctx(map
), isl_error_invalid
,
9756 "not a product", return isl_map_free(map
));
9758 space
= isl_map_get_space(map
);
9759 total1
= isl_space_dim(space
, isl_dim_in
);
9760 total2
= isl_space_dim(space
, isl_dim_out
);
9761 space
= isl_space_factor_range(space
);
9762 keep1
= isl_space_dim(space
, isl_dim_in
);
9763 keep2
= isl_space_dim(space
, isl_dim_out
);
9764 map
= isl_map_project_out(map
, isl_dim_in
, 0, total1
- keep1
);
9765 map
= isl_map_project_out(map
, isl_dim_out
, 0, total2
- keep2
);
9766 map
= isl_map_reset_space(map
, space
);
9771 /* Given a map of the form [A -> B] -> C, return the map A -> C.
9773 __isl_give isl_map
*isl_map_domain_factor_domain(__isl_take isl_map
*map
)
9780 if (!isl_space_domain_is_wrapping(map
->dim
))
9781 isl_die(isl_map_get_ctx(map
), isl_error_invalid
,
9782 "domain is not a product", return isl_map_free(map
));
9784 space
= isl_map_get_space(map
);
9785 total
= isl_space_dim(space
, isl_dim_in
);
9786 space
= isl_space_domain_factor_domain(space
);
9787 keep
= isl_space_dim(space
, isl_dim_in
);
9788 map
= isl_map_project_out(map
, isl_dim_in
, keep
, total
- keep
);
9789 map
= isl_map_reset_space(map
, space
);
9794 /* Given a map of the form [A -> B] -> C, return the map B -> C.
9796 __isl_give isl_map
*isl_map_domain_factor_range(__isl_take isl_map
*map
)
9803 if (!isl_space_domain_is_wrapping(map
->dim
))
9804 isl_die(isl_map_get_ctx(map
), isl_error_invalid
,
9805 "domain is not a product", return isl_map_free(map
));
9807 space
= isl_map_get_space(map
);
9808 total
= isl_space_dim(space
, isl_dim_in
);
9809 space
= isl_space_domain_factor_range(space
);
9810 keep
= isl_space_dim(space
, isl_dim_in
);
9811 map
= isl_map_project_out(map
, isl_dim_in
, 0, total
- keep
);
9812 map
= isl_map_reset_space(map
, space
);
9817 /* Given a map A -> [B -> C], extract the map A -> B.
9819 __isl_give isl_map
*isl_map_range_factor_domain(__isl_take isl_map
*map
)
9826 if (!isl_space_range_is_wrapping(map
->dim
))
9827 isl_die(isl_map_get_ctx(map
), isl_error_invalid
,
9828 "range is not a product", return isl_map_free(map
));
9830 space
= isl_map_get_space(map
);
9831 total
= isl_space_dim(space
, isl_dim_out
);
9832 space
= isl_space_range_factor_domain(space
);
9833 keep
= isl_space_dim(space
, isl_dim_out
);
9834 map
= isl_map_project_out(map
, isl_dim_out
, keep
, total
- keep
);
9835 map
= isl_map_reset_space(map
, space
);
9840 /* Given a map A -> [B -> C], extract the map A -> C.
9842 __isl_give isl_map
*isl_map_range_factor_range(__isl_take isl_map
*map
)
9849 if (!isl_space_range_is_wrapping(map
->dim
))
9850 isl_die(isl_map_get_ctx(map
), isl_error_invalid
,
9851 "range is not a product", return isl_map_free(map
));
9853 space
= isl_map_get_space(map
);
9854 total
= isl_space_dim(space
, isl_dim_out
);
9855 space
= isl_space_range_factor_range(space
);
9856 keep
= isl_space_dim(space
, isl_dim_out
);
9857 map
= isl_map_project_out(map
, isl_dim_out
, 0, total
- keep
);
9858 map
= isl_map_reset_space(map
, space
);
9863 /* Given two maps A -> B and C -> D, construct a map (A, C) -> (B * D)
9865 __isl_give isl_map
*isl_map_flat_domain_product(__isl_take isl_map
*map1
,
9866 __isl_take isl_map
*map2
)
9870 prod
= isl_map_domain_product(map1
, map2
);
9871 prod
= isl_map_flatten_domain(prod
);
9875 /* Given two maps A -> B and C -> D, construct a map (A * C) -> (B, D)
9877 __isl_give isl_map
*isl_map_flat_range_product(__isl_take isl_map
*map1
,
9878 __isl_take isl_map
*map2
)
9882 prod
= isl_map_range_product(map1
, map2
);
9883 prod
= isl_map_flatten_range(prod
);
9887 uint32_t isl_basic_map_get_hash(__isl_keep isl_basic_map
*bmap
)
9890 uint32_t hash
= isl_hash_init();
9895 bmap
= isl_basic_map_copy(bmap
);
9896 bmap
= isl_basic_map_normalize(bmap
);
9899 total
= isl_basic_map_total_dim(bmap
);
9900 isl_hash_byte(hash
, bmap
->n_eq
& 0xFF);
9901 for (i
= 0; i
< bmap
->n_eq
; ++i
) {
9903 c_hash
= isl_seq_get_hash(bmap
->eq
[i
], 1 + total
);
9904 isl_hash_hash(hash
, c_hash
);
9906 isl_hash_byte(hash
, bmap
->n_ineq
& 0xFF);
9907 for (i
= 0; i
< bmap
->n_ineq
; ++i
) {
9909 c_hash
= isl_seq_get_hash(bmap
->ineq
[i
], 1 + total
);
9910 isl_hash_hash(hash
, c_hash
);
9912 isl_hash_byte(hash
, bmap
->n_div
& 0xFF);
9913 for (i
= 0; i
< bmap
->n_div
; ++i
) {
9915 if (isl_int_is_zero(bmap
->div
[i
][0]))
9917 isl_hash_byte(hash
, i
& 0xFF);
9918 c_hash
= isl_seq_get_hash(bmap
->div
[i
], 1 + 1 + total
);
9919 isl_hash_hash(hash
, c_hash
);
9921 isl_basic_map_free(bmap
);
9925 uint32_t isl_basic_set_get_hash(__isl_keep isl_basic_set
*bset
)
9927 return isl_basic_map_get_hash(bset_to_bmap(bset
));
9930 uint32_t isl_map_get_hash(__isl_keep isl_map
*map
)
9937 map
= isl_map_copy(map
);
9938 map
= isl_map_normalize(map
);
9942 hash
= isl_hash_init();
9943 for (i
= 0; i
< map
->n
; ++i
) {
9945 bmap_hash
= isl_basic_map_get_hash(map
->p
[i
]);
9946 isl_hash_hash(hash
, bmap_hash
);
9954 uint32_t isl_set_get_hash(__isl_keep isl_set
*set
)
9956 return isl_map_get_hash(set_to_map(set
));
9959 /* Return the number of basic maps in the (current) representation of "map".
9961 int isl_map_n_basic_map(__isl_keep isl_map
*map
)
9963 return map
? map
->n
: 0;
9966 int isl_set_n_basic_set(__isl_keep isl_set
*set
)
9968 return set
? set
->n
: 0;
9971 isl_stat
isl_map_foreach_basic_map(__isl_keep isl_map
*map
,
9972 isl_stat (*fn
)(__isl_take isl_basic_map
*bmap
, void *user
), void *user
)
9977 return isl_stat_error
;
9979 for (i
= 0; i
< map
->n
; ++i
)
9980 if (fn(isl_basic_map_copy(map
->p
[i
]), user
) < 0)
9981 return isl_stat_error
;
9986 isl_stat
isl_set_foreach_basic_set(__isl_keep isl_set
*set
,
9987 isl_stat (*fn
)(__isl_take isl_basic_set
*bset
, void *user
), void *user
)
9992 return isl_stat_error
;
9994 for (i
= 0; i
< set
->n
; ++i
)
9995 if (fn(isl_basic_set_copy(set
->p
[i
]), user
) < 0)
9996 return isl_stat_error
;
10001 /* Return a list of basic sets, the union of which is equal to "set".
10003 __isl_give isl_basic_set_list
*isl_set_get_basic_set_list(
10004 __isl_keep isl_set
*set
)
10007 isl_basic_set_list
*list
;
10012 list
= isl_basic_set_list_alloc(isl_set_get_ctx(set
), set
->n
);
10013 for (i
= 0; i
< set
->n
; ++i
) {
10014 isl_basic_set
*bset
;
10016 bset
= isl_basic_set_copy(set
->p
[i
]);
10017 list
= isl_basic_set_list_add(list
, bset
);
10023 __isl_give isl_basic_set
*isl_basic_set_lift(__isl_take isl_basic_set
*bset
)
10030 bset
= isl_basic_set_cow(bset
);
10034 dim
= isl_basic_set_get_space(bset
);
10035 dim
= isl_space_lift(dim
, bset
->n_div
);
10038 isl_space_free(bset
->dim
);
10040 bset
->extra
-= bset
->n_div
;
10043 bset
= isl_basic_set_finalize(bset
);
10047 isl_basic_set_free(bset
);
10051 __isl_give isl_set
*isl_set_lift(__isl_take isl_set
*set
)
10057 set
= set_from_map(isl_map_align_divs_internal(set_to_map(set
)));
10062 set
= isl_set_cow(set
);
10066 n_div
= set
->p
[0]->n_div
;
10067 dim
= isl_set_get_space(set
);
10068 dim
= isl_space_lift(dim
, n_div
);
10071 isl_space_free(set
->dim
);
10074 for (i
= 0; i
< set
->n
; ++i
) {
10075 set
->p
[i
] = isl_basic_set_lift(set
->p
[i
]);
10086 int isl_basic_set_size(__isl_keep isl_basic_set
*bset
)
10094 dim
= isl_basic_set_total_dim(bset
);
10095 size
+= bset
->n_eq
* (1 + dim
);
10096 size
+= bset
->n_ineq
* (1 + dim
);
10097 size
+= bset
->n_div
* (2 + dim
);
10102 int isl_set_size(__isl_keep isl_set
*set
)
10110 for (i
= 0; i
< set
->n
; ++i
)
10111 size
+= isl_basic_set_size(set
->p
[i
]);
10116 /* Check if there is any lower bound (if lower == 0) and/or upper
10117 * bound (if upper == 0) on the specified dim.
10119 static isl_bool
basic_map_dim_is_bounded(__isl_keep isl_basic_map
*bmap
,
10120 enum isl_dim_type type
, unsigned pos
, int lower
, int upper
)
10124 if (isl_basic_map_check_range(bmap
, type
, pos
, 1) < 0)
10125 return isl_bool_error
;
10127 pos
+= isl_basic_map_offset(bmap
, type
);
10129 for (i
= 0; i
< bmap
->n_div
; ++i
) {
10130 if (isl_int_is_zero(bmap
->div
[i
][0]))
10132 if (!isl_int_is_zero(bmap
->div
[i
][1 + pos
]))
10133 return isl_bool_true
;
10136 for (i
= 0; i
< bmap
->n_eq
; ++i
)
10137 if (!isl_int_is_zero(bmap
->eq
[i
][pos
]))
10138 return isl_bool_true
;
10140 for (i
= 0; i
< bmap
->n_ineq
; ++i
) {
10141 int sgn
= isl_int_sgn(bmap
->ineq
[i
][pos
]);
10148 return lower
&& upper
;
10151 isl_bool
isl_basic_map_dim_is_bounded(__isl_keep isl_basic_map
*bmap
,
10152 enum isl_dim_type type
, unsigned pos
)
10154 return basic_map_dim_is_bounded(bmap
, type
, pos
, 0, 0);
10157 isl_bool
isl_basic_map_dim_has_lower_bound(__isl_keep isl_basic_map
*bmap
,
10158 enum isl_dim_type type
, unsigned pos
)
10160 return basic_map_dim_is_bounded(bmap
, type
, pos
, 0, 1);
10163 isl_bool
isl_basic_map_dim_has_upper_bound(__isl_keep isl_basic_map
*bmap
,
10164 enum isl_dim_type type
, unsigned pos
)
10166 return basic_map_dim_is_bounded(bmap
, type
, pos
, 1, 0);
10169 isl_bool
isl_map_dim_is_bounded(__isl_keep isl_map
*map
,
10170 enum isl_dim_type type
, unsigned pos
)
10175 return isl_bool_error
;
10177 for (i
= 0; i
< map
->n
; ++i
) {
10179 bounded
= isl_basic_map_dim_is_bounded(map
->p
[i
], type
, pos
);
10180 if (bounded
< 0 || !bounded
)
10184 return isl_bool_true
;
10187 /* Return true if the specified dim is involved in both an upper bound
10188 * and a lower bound.
10190 isl_bool
isl_set_dim_is_bounded(__isl_keep isl_set
*set
,
10191 enum isl_dim_type type
, unsigned pos
)
10193 return isl_map_dim_is_bounded(set_to_map(set
), type
, pos
);
10196 /* Does "map" have a bound (according to "fn") for any of its basic maps?
10198 static isl_bool
has_any_bound(__isl_keep isl_map
*map
,
10199 enum isl_dim_type type
, unsigned pos
,
10200 isl_bool (*fn
)(__isl_keep isl_basic_map
*bmap
,
10201 enum isl_dim_type type
, unsigned pos
))
10206 return isl_bool_error
;
10208 for (i
= 0; i
< map
->n
; ++i
) {
10210 bounded
= fn(map
->p
[i
], type
, pos
);
10211 if (bounded
< 0 || bounded
)
10215 return isl_bool_false
;
10218 /* Return 1 if the specified dim is involved in any lower bound.
10220 isl_bool
isl_set_dim_has_any_lower_bound(__isl_keep isl_set
*set
,
10221 enum isl_dim_type type
, unsigned pos
)
10223 return has_any_bound(set
, type
, pos
,
10224 &isl_basic_map_dim_has_lower_bound
);
10227 /* Return 1 if the specified dim is involved in any upper bound.
10229 isl_bool
isl_set_dim_has_any_upper_bound(__isl_keep isl_set
*set
,
10230 enum isl_dim_type type
, unsigned pos
)
10232 return has_any_bound(set
, type
, pos
,
10233 &isl_basic_map_dim_has_upper_bound
);
10236 /* Does "map" have a bound (according to "fn") for all of its basic maps?
10238 static isl_bool
has_bound(__isl_keep isl_map
*map
,
10239 enum isl_dim_type type
, unsigned pos
,
10240 isl_bool (*fn
)(__isl_keep isl_basic_map
*bmap
,
10241 enum isl_dim_type type
, unsigned pos
))
10246 return isl_bool_error
;
10248 for (i
= 0; i
< map
->n
; ++i
) {
10250 bounded
= fn(map
->p
[i
], type
, pos
);
10251 if (bounded
< 0 || !bounded
)
10255 return isl_bool_true
;
10258 /* Return 1 if the specified dim has a lower bound (in each of its basic sets).
10260 isl_bool
isl_set_dim_has_lower_bound(__isl_keep isl_set
*set
,
10261 enum isl_dim_type type
, unsigned pos
)
10263 return has_bound(set
, type
, pos
, &isl_basic_map_dim_has_lower_bound
);
10266 /* Return 1 if the specified dim has an upper bound (in each of its basic sets).
10268 isl_bool
isl_set_dim_has_upper_bound(__isl_keep isl_set
*set
,
10269 enum isl_dim_type type
, unsigned pos
)
10271 return has_bound(set
, type
, pos
, &isl_basic_map_dim_has_upper_bound
);
10274 /* For each of the "n" variables starting at "first", determine
10275 * the sign of the variable and put the results in the first "n"
10276 * elements of the array "signs".
10278 * 1 means that the variable is non-negative
10279 * -1 means that the variable is non-positive
10280 * 0 means the variable attains both positive and negative values.
10282 isl_stat
isl_basic_set_vars_get_sign(__isl_keep isl_basic_set
*bset
,
10283 unsigned first
, unsigned n
, int *signs
)
10285 isl_vec
*bound
= NULL
;
10286 struct isl_tab
*tab
= NULL
;
10287 struct isl_tab_undo
*snap
;
10290 if (!bset
|| !signs
)
10291 return isl_stat_error
;
10293 bound
= isl_vec_alloc(bset
->ctx
, 1 + isl_basic_set_total_dim(bset
));
10294 tab
= isl_tab_from_basic_set(bset
, 0);
10295 if (!bound
|| !tab
)
10298 isl_seq_clr(bound
->el
, bound
->size
);
10299 isl_int_set_si(bound
->el
[0], -1);
10301 snap
= isl_tab_snap(tab
);
10302 for (i
= 0; i
< n
; ++i
) {
10305 isl_int_set_si(bound
->el
[1 + first
+ i
], -1);
10306 if (isl_tab_add_ineq(tab
, bound
->el
) < 0)
10308 empty
= tab
->empty
;
10309 isl_int_set_si(bound
->el
[1 + first
+ i
], 0);
10310 if (isl_tab_rollback(tab
, snap
) < 0)
10318 isl_int_set_si(bound
->el
[1 + first
+ i
], 1);
10319 if (isl_tab_add_ineq(tab
, bound
->el
) < 0)
10321 empty
= tab
->empty
;
10322 isl_int_set_si(bound
->el
[1 + first
+ i
], 0);
10323 if (isl_tab_rollback(tab
, snap
) < 0)
10326 signs
[i
] = empty
? -1 : 0;
10330 isl_vec_free(bound
);
10331 return isl_stat_ok
;
10334 isl_vec_free(bound
);
10335 return isl_stat_error
;
10338 isl_stat
isl_basic_set_dims_get_sign(__isl_keep isl_basic_set
*bset
,
10339 enum isl_dim_type type
, unsigned first
, unsigned n
, int *signs
)
10341 if (!bset
|| !signs
)
10342 return isl_stat_error
;
10343 isl_assert(bset
->ctx
, first
+ n
<= isl_basic_set_dim(bset
, type
),
10344 return isl_stat_error
);
10346 first
+= pos(bset
->dim
, type
) - 1;
10347 return isl_basic_set_vars_get_sign(bset
, first
, n
, signs
);
10350 /* Is it possible for the integer division "div" to depend (possibly
10351 * indirectly) on any output dimensions?
10353 * If the div is undefined, then we conservatively assume that it
10354 * may depend on them.
10355 * Otherwise, we check if it actually depends on them or on any integer
10356 * divisions that may depend on them.
10358 static isl_bool
div_may_involve_output(__isl_keep isl_basic_map
*bmap
, int div
)
10361 unsigned n_out
, o_out
;
10362 unsigned n_div
, o_div
;
10364 if (isl_int_is_zero(bmap
->div
[div
][0]))
10365 return isl_bool_true
;
10367 n_out
= isl_basic_map_dim(bmap
, isl_dim_out
);
10368 o_out
= isl_basic_map_offset(bmap
, isl_dim_out
);
10370 if (isl_seq_first_non_zero(bmap
->div
[div
] + 1 + o_out
, n_out
) != -1)
10371 return isl_bool_true
;
10373 n_div
= isl_basic_map_dim(bmap
, isl_dim_div
);
10374 o_div
= isl_basic_map_offset(bmap
, isl_dim_div
);
10376 for (i
= 0; i
< n_div
; ++i
) {
10377 isl_bool may_involve
;
10379 if (isl_int_is_zero(bmap
->div
[div
][1 + o_div
+ i
]))
10381 may_involve
= div_may_involve_output(bmap
, i
);
10382 if (may_involve
< 0 || may_involve
)
10383 return may_involve
;
10386 return isl_bool_false
;
10389 /* Return the first integer division of "bmap" in the range
10390 * [first, first + n[ that may depend on any output dimensions and
10391 * that has a non-zero coefficient in "c" (where the first coefficient
10392 * in "c" corresponds to integer division "first").
10394 static int first_div_may_involve_output(__isl_keep isl_basic_map
*bmap
,
10395 isl_int
*c
, int first
, int n
)
10402 for (k
= first
; k
< first
+ n
; ++k
) {
10403 isl_bool may_involve
;
10405 if (isl_int_is_zero(c
[k
]))
10407 may_involve
= div_may_involve_output(bmap
, k
);
10408 if (may_involve
< 0)
10417 /* Look for a pair of inequality constraints in "bmap" of the form
10419 * -l + i >= 0 or i >= l
10421 * n + l - i >= 0 or i <= l + n
10423 * with n < "m" and i the output dimension at position "pos".
10424 * (Note that n >= 0 as otherwise the two constraints would conflict.)
10425 * Furthermore, "l" is only allowed to involve parameters, input dimensions
10426 * and earlier output dimensions, as well as integer divisions that do
10427 * not involve any of the output dimensions.
10429 * Return the index of the first inequality constraint or bmap->n_ineq
10430 * if no such pair can be found.
10432 static int find_modulo_constraint_pair(__isl_keep isl_basic_map
*bmap
,
10433 int pos
, isl_int m
)
10438 unsigned n_div
, o_div
;
10439 unsigned n_out
, o_out
;
10445 ctx
= isl_basic_map_get_ctx(bmap
);
10446 total
= isl_basic_map_total_dim(bmap
);
10447 n_out
= isl_basic_map_dim(bmap
, isl_dim_out
);
10448 o_out
= isl_basic_map_offset(bmap
, isl_dim_out
);
10449 n_div
= isl_basic_map_dim(bmap
, isl_dim_div
);
10450 o_div
= isl_basic_map_offset(bmap
, isl_dim_div
);
10451 for (i
= 0; i
< bmap
->n_ineq
; ++i
) {
10452 if (!isl_int_abs_eq(bmap
->ineq
[i
][o_out
+ pos
], ctx
->one
))
10454 if (isl_seq_first_non_zero(bmap
->ineq
[i
] + o_out
+ pos
+ 1,
10455 n_out
- (pos
+ 1)) != -1)
10457 if (first_div_may_involve_output(bmap
, bmap
->ineq
[i
] + o_div
,
10460 for (j
= i
+ 1; j
< bmap
->n_ineq
; ++j
) {
10461 if (!isl_int_abs_eq(bmap
->ineq
[j
][o_out
+ pos
],
10464 if (!isl_seq_is_neg(bmap
->ineq
[i
] + 1,
10465 bmap
->ineq
[j
] + 1, total
))
10469 if (j
>= bmap
->n_ineq
)
10471 isl_int_add(bmap
->ineq
[i
][0],
10472 bmap
->ineq
[i
][0], bmap
->ineq
[j
][0]);
10473 less
= isl_int_abs_lt(bmap
->ineq
[i
][0], m
);
10474 isl_int_sub(bmap
->ineq
[i
][0],
10475 bmap
->ineq
[i
][0], bmap
->ineq
[j
][0]);
10478 if (isl_int_is_one(bmap
->ineq
[i
][o_out
+ pos
]))
10484 return bmap
->n_ineq
;
10487 /* Return the index of the equality of "bmap" that defines
10488 * the output dimension "pos" in terms of earlier dimensions.
10489 * The equality may also involve integer divisions, as long
10490 * as those integer divisions are defined in terms of
10491 * parameters or input dimensions.
10492 * In this case, *div is set to the number of integer divisions and
10493 * *ineq is set to the number of inequality constraints (provided
10494 * div and ineq are not NULL).
10496 * The equality may also involve a single integer division involving
10497 * the output dimensions (typically only output dimension "pos") as
10498 * long as the coefficient of output dimension "pos" is 1 or -1 and
10499 * there is a pair of constraints i >= l and i <= l + n, with i referring
10500 * to output dimension "pos", l an expression involving only earlier
10501 * dimensions and n smaller than the coefficient of the integer division
10502 * in the equality. In this case, the output dimension can be defined
10503 * in terms of a modulo expression that does not involve the integer division.
10504 * *div is then set to this single integer division and
10505 * *ineq is set to the index of constraint i >= l.
10507 * Return bmap->n_eq if there is no such equality.
10508 * Return -1 on error.
10510 int isl_basic_map_output_defining_equality(__isl_keep isl_basic_map
*bmap
,
10511 int pos
, int *div
, int *ineq
)
10514 unsigned n_out
, o_out
;
10515 unsigned n_div
, o_div
;
10520 n_out
= isl_basic_map_dim(bmap
, isl_dim_out
);
10521 o_out
= isl_basic_map_offset(bmap
, isl_dim_out
);
10522 n_div
= isl_basic_map_dim(bmap
, isl_dim_div
);
10523 o_div
= isl_basic_map_offset(bmap
, isl_dim_div
);
10526 *ineq
= bmap
->n_ineq
;
10529 for (j
= 0; j
< bmap
->n_eq
; ++j
) {
10530 if (isl_int_is_zero(bmap
->eq
[j
][o_out
+ pos
]))
10532 if (isl_seq_first_non_zero(bmap
->eq
[j
] + o_out
+ pos
+ 1,
10533 n_out
- (pos
+ 1)) != -1)
10535 k
= first_div_may_involve_output(bmap
, bmap
->eq
[j
] + o_div
,
10539 if (!isl_int_is_one(bmap
->eq
[j
][o_out
+ pos
]) &&
10540 !isl_int_is_negone(bmap
->eq
[j
][o_out
+ pos
]))
10542 if (first_div_may_involve_output(bmap
, bmap
->eq
[j
] + o_div
,
10543 k
+ 1, n_div
- (k
+1)) < n_div
)
10545 l
= find_modulo_constraint_pair(bmap
, pos
,
10546 bmap
->eq
[j
][o_div
+ k
]);
10549 if (l
>= bmap
->n_ineq
)
10561 /* Check if the given basic map is obviously single-valued.
10562 * In particular, for each output dimension, check that there is
10563 * an equality that defines the output dimension in terms of
10564 * earlier dimensions.
10566 isl_bool
isl_basic_map_plain_is_single_valued(__isl_keep isl_basic_map
*bmap
)
10572 return isl_bool_error
;
10574 n_out
= isl_basic_map_dim(bmap
, isl_dim_out
);
10576 for (i
= 0; i
< n_out
; ++i
) {
10579 eq
= isl_basic_map_output_defining_equality(bmap
, i
,
10582 return isl_bool_error
;
10583 if (eq
>= bmap
->n_eq
)
10584 return isl_bool_false
;
10587 return isl_bool_true
;
10590 /* Check if the given basic map is single-valued.
10591 * We simply compute
10595 * and check if the result is a subset of the identity mapping.
10597 isl_bool
isl_basic_map_is_single_valued(__isl_keep isl_basic_map
*bmap
)
10600 isl_basic_map
*test
;
10604 sv
= isl_basic_map_plain_is_single_valued(bmap
);
10608 test
= isl_basic_map_reverse(isl_basic_map_copy(bmap
));
10609 test
= isl_basic_map_apply_range(test
, isl_basic_map_copy(bmap
));
10611 space
= isl_basic_map_get_space(bmap
);
10612 space
= isl_space_map_from_set(isl_space_range(space
));
10613 id
= isl_basic_map_identity(space
);
10615 sv
= isl_basic_map_is_subset(test
, id
);
10617 isl_basic_map_free(test
);
10618 isl_basic_map_free(id
);
10623 /* Check if the given map is obviously single-valued.
10625 isl_bool
isl_map_plain_is_single_valued(__isl_keep isl_map
*map
)
10628 return isl_bool_error
;
10630 return isl_bool_true
;
10632 return isl_bool_false
;
10634 return isl_basic_map_plain_is_single_valued(map
->p
[0]);
10637 /* Check if the given map is single-valued.
10638 * We simply compute
10642 * and check if the result is a subset of the identity mapping.
10644 isl_bool
isl_map_is_single_valued(__isl_keep isl_map
*map
)
10651 sv
= isl_map_plain_is_single_valued(map
);
10655 test
= isl_map_reverse(isl_map_copy(map
));
10656 test
= isl_map_apply_range(test
, isl_map_copy(map
));
10658 dim
= isl_space_map_from_set(isl_space_range(isl_map_get_space(map
)));
10659 id
= isl_map_identity(dim
);
10661 sv
= isl_map_is_subset(test
, id
);
10663 isl_map_free(test
);
10669 isl_bool
isl_map_is_injective(__isl_keep isl_map
*map
)
10673 map
= isl_map_copy(map
);
10674 map
= isl_map_reverse(map
);
10675 in
= isl_map_is_single_valued(map
);
10681 /* Check if the given map is obviously injective.
10683 isl_bool
isl_map_plain_is_injective(__isl_keep isl_map
*map
)
10687 map
= isl_map_copy(map
);
10688 map
= isl_map_reverse(map
);
10689 in
= isl_map_plain_is_single_valued(map
);
10695 isl_bool
isl_map_is_bijective(__isl_keep isl_map
*map
)
10699 sv
= isl_map_is_single_valued(map
);
10703 return isl_map_is_injective(map
);
10706 isl_bool
isl_set_is_singleton(__isl_keep isl_set
*set
)
10708 return isl_map_is_single_valued(set_to_map(set
));
10711 /* Does "map" only map elements to themselves?
10713 * If the domain and range spaces are different, then "map"
10714 * is considered not to be an identity relation, even if it is empty.
10715 * Otherwise, construct the maximal identity relation and
10716 * check whether "map" is a subset of this relation.
10718 isl_bool
isl_map_is_identity(__isl_keep isl_map
*map
)
10722 isl_bool equal
, is_identity
;
10724 space
= isl_map_get_space(map
);
10725 equal
= isl_space_tuple_is_equal(space
, isl_dim_in
, space
, isl_dim_out
);
10726 isl_space_free(space
);
10727 if (equal
< 0 || !equal
)
10730 id
= isl_map_identity(isl_map_get_space(map
));
10731 is_identity
= isl_map_is_subset(map
, id
);
10734 return is_identity
;
10737 int isl_map_is_translation(__isl_keep isl_map
*map
)
10742 delta
= isl_map_deltas(isl_map_copy(map
));
10743 ok
= isl_set_is_singleton(delta
);
10744 isl_set_free(delta
);
10749 static int unique(isl_int
*p
, unsigned pos
, unsigned len
)
10751 if (isl_seq_first_non_zero(p
, pos
) != -1)
10753 if (isl_seq_first_non_zero(p
+ pos
+ 1, len
- pos
- 1) != -1)
10758 isl_bool
isl_basic_set_is_box(__isl_keep isl_basic_set
*bset
)
10765 return isl_bool_error
;
10767 if (isl_basic_set_dim(bset
, isl_dim_div
) != 0)
10768 return isl_bool_false
;
10770 nvar
= isl_basic_set_dim(bset
, isl_dim_set
);
10771 ovar
= isl_space_offset(bset
->dim
, isl_dim_set
);
10772 for (j
= 0; j
< nvar
; ++j
) {
10773 int lower
= 0, upper
= 0;
10774 for (i
= 0; i
< bset
->n_eq
; ++i
) {
10775 if (isl_int_is_zero(bset
->eq
[i
][1 + ovar
+ j
]))
10777 if (!unique(bset
->eq
[i
] + 1 + ovar
, j
, nvar
))
10778 return isl_bool_false
;
10781 if (i
< bset
->n_eq
)
10783 for (i
= 0; i
< bset
->n_ineq
; ++i
) {
10784 if (isl_int_is_zero(bset
->ineq
[i
][1 + ovar
+ j
]))
10786 if (!unique(bset
->ineq
[i
] + 1 + ovar
, j
, nvar
))
10787 return isl_bool_false
;
10788 if (isl_int_is_pos(bset
->ineq
[i
][1 + ovar
+ j
]))
10793 if (!lower
|| !upper
)
10794 return isl_bool_false
;
10797 return isl_bool_true
;
10800 isl_bool
isl_set_is_box(__isl_keep isl_set
*set
)
10803 return isl_bool_error
;
10805 return isl_bool_false
;
10807 return isl_basic_set_is_box(set
->p
[0]);
10810 isl_bool
isl_basic_set_is_wrapping(__isl_keep isl_basic_set
*bset
)
10813 return isl_bool_error
;
10815 return isl_space_is_wrapping(bset
->dim
);
10818 isl_bool
isl_set_is_wrapping(__isl_keep isl_set
*set
)
10821 return isl_bool_error
;
10823 return isl_space_is_wrapping(set
->dim
);
10826 /* Modify the space of "map" through a call to "change".
10827 * If "can_change" is set (not NULL), then first call it to check
10828 * if the modification is allowed, printing the error message "cannot_change"
10831 static __isl_give isl_map
*isl_map_change_space(__isl_take isl_map
*map
,
10832 isl_bool (*can_change
)(__isl_keep isl_map
*map
),
10833 const char *cannot_change
,
10834 __isl_give isl_space
*(*change
)(__isl_take isl_space
*space
))
10842 ok
= can_change
? can_change(map
) : isl_bool_true
;
10844 return isl_map_free(map
);
10846 isl_die(isl_map_get_ctx(map
), isl_error_invalid
, cannot_change
,
10847 return isl_map_free(map
));
10849 space
= change(isl_map_get_space(map
));
10850 map
= isl_map_reset_space(map
, space
);
10855 /* Is the domain of "map" a wrapped relation?
10857 isl_bool
isl_map_domain_is_wrapping(__isl_keep isl_map
*map
)
10860 return isl_bool_error
;
10862 return isl_space_domain_is_wrapping(map
->dim
);
10865 /* Is the range of "map" a wrapped relation?
10867 isl_bool
isl_map_range_is_wrapping(__isl_keep isl_map
*map
)
10870 return isl_bool_error
;
10872 return isl_space_range_is_wrapping(map
->dim
);
10875 __isl_give isl_basic_set
*isl_basic_map_wrap(__isl_take isl_basic_map
*bmap
)
10877 bmap
= isl_basic_map_cow(bmap
);
10881 bmap
->dim
= isl_space_wrap(bmap
->dim
);
10885 bmap
= isl_basic_map_finalize(bmap
);
10887 return bset_from_bmap(bmap
);
10889 isl_basic_map_free(bmap
);
10893 /* Given a map A -> B, return the set (A -> B).
10895 __isl_give isl_set
*isl_map_wrap(__isl_take isl_map
*map
)
10897 return isl_map_change_space(map
, NULL
, NULL
, &isl_space_wrap
);
10900 __isl_give isl_basic_map
*isl_basic_set_unwrap(__isl_take isl_basic_set
*bset
)
10902 bset
= isl_basic_set_cow(bset
);
10906 bset
->dim
= isl_space_unwrap(bset
->dim
);
10910 bset
= isl_basic_set_finalize(bset
);
10912 return bset_to_bmap(bset
);
10914 isl_basic_set_free(bset
);
10918 /* Given a set (A -> B), return the map A -> B.
10919 * Error out if "set" is not of the form (A -> B).
10921 __isl_give isl_map
*isl_set_unwrap(__isl_take isl_set
*set
)
10923 return isl_map_change_space(set
, &isl_set_is_wrapping
,
10924 "not a wrapping set", &isl_space_unwrap
);
10927 __isl_give isl_basic_map
*isl_basic_map_reset(__isl_take isl_basic_map
*bmap
,
10928 enum isl_dim_type type
)
10933 if (!isl_space_is_named_or_nested(bmap
->dim
, type
))
10936 bmap
= isl_basic_map_cow(bmap
);
10940 bmap
->dim
= isl_space_reset(bmap
->dim
, type
);
10944 bmap
= isl_basic_map_finalize(bmap
);
10948 isl_basic_map_free(bmap
);
10952 __isl_give isl_map
*isl_map_reset(__isl_take isl_map
*map
,
10953 enum isl_dim_type type
)
10960 if (!isl_space_is_named_or_nested(map
->dim
, type
))
10963 map
= isl_map_cow(map
);
10967 for (i
= 0; i
< map
->n
; ++i
) {
10968 map
->p
[i
] = isl_basic_map_reset(map
->p
[i
], type
);
10972 map
->dim
= isl_space_reset(map
->dim
, type
);
10982 __isl_give isl_basic_map
*isl_basic_map_flatten(__isl_take isl_basic_map
*bmap
)
10987 if (!bmap
->dim
->nested
[0] && !bmap
->dim
->nested
[1])
10990 bmap
= isl_basic_map_cow(bmap
);
10994 bmap
->dim
= isl_space_flatten(bmap
->dim
);
10998 bmap
= isl_basic_map_finalize(bmap
);
11002 isl_basic_map_free(bmap
);
11006 __isl_give isl_basic_set
*isl_basic_set_flatten(__isl_take isl_basic_set
*bset
)
11008 return bset_from_bmap(isl_basic_map_flatten(bset_to_bmap(bset
)));
11011 __isl_give isl_basic_map
*isl_basic_map_flatten_domain(
11012 __isl_take isl_basic_map
*bmap
)
11017 if (!bmap
->dim
->nested
[0])
11020 bmap
= isl_basic_map_cow(bmap
);
11024 bmap
->dim
= isl_space_flatten_domain(bmap
->dim
);
11028 bmap
= isl_basic_map_finalize(bmap
);
11032 isl_basic_map_free(bmap
);
11036 __isl_give isl_basic_map
*isl_basic_map_flatten_range(
11037 __isl_take isl_basic_map
*bmap
)
11042 if (!bmap
->dim
->nested
[1])
11045 bmap
= isl_basic_map_cow(bmap
);
11049 bmap
->dim
= isl_space_flatten_range(bmap
->dim
);
11053 bmap
= isl_basic_map_finalize(bmap
);
11057 isl_basic_map_free(bmap
);
11061 /* Remove any internal structure from the spaces of domain and range of "map".
11063 __isl_give isl_map
*isl_map_flatten(__isl_take isl_map
*map
)
11068 if (!map
->dim
->nested
[0] && !map
->dim
->nested
[1])
11071 return isl_map_change_space(map
, NULL
, NULL
, &isl_space_flatten
);
11074 __isl_give isl_set
*isl_set_flatten(__isl_take isl_set
*set
)
11076 return set_from_map(isl_map_flatten(set_to_map(set
)));
11079 __isl_give isl_map
*isl_set_flatten_map(__isl_take isl_set
*set
)
11081 isl_space
*dim
, *flat_dim
;
11084 dim
= isl_set_get_space(set
);
11085 flat_dim
= isl_space_flatten(isl_space_copy(dim
));
11086 map
= isl_map_identity(isl_space_join(isl_space_reverse(dim
), flat_dim
));
11087 map
= isl_map_intersect_domain(map
, set
);
11092 /* Remove any internal structure from the space of the domain of "map".
11094 __isl_give isl_map
*isl_map_flatten_domain(__isl_take isl_map
*map
)
11099 if (!map
->dim
->nested
[0])
11102 return isl_map_change_space(map
, NULL
, NULL
, &isl_space_flatten_domain
);
11105 /* Remove any internal structure from the space of the range of "map".
11107 __isl_give isl_map
*isl_map_flatten_range(__isl_take isl_map
*map
)
11112 if (!map
->dim
->nested
[1])
11115 return isl_map_change_space(map
, NULL
, NULL
, &isl_space_flatten_range
);
11118 /* Reorder the dimensions of "bmap" according to the given dim_map
11119 * and set the dimension specification to "dim" and
11120 * perform Gaussian elimination on the result.
11122 __isl_give isl_basic_map
*isl_basic_map_realign(__isl_take isl_basic_map
*bmap
,
11123 __isl_take isl_space
*dim
, __isl_take
struct isl_dim_map
*dim_map
)
11125 isl_basic_map
*res
;
11128 bmap
= isl_basic_map_cow(bmap
);
11129 if (!bmap
|| !dim
|| !dim_map
)
11132 flags
= bmap
->flags
;
11133 ISL_FL_CLR(flags
, ISL_BASIC_MAP_FINAL
);
11134 ISL_FL_CLR(flags
, ISL_BASIC_MAP_NORMALIZED
);
11135 ISL_FL_CLR(flags
, ISL_BASIC_MAP_NORMALIZED_DIVS
);
11136 res
= isl_basic_map_alloc_space(dim
,
11137 bmap
->n_div
, bmap
->n_eq
, bmap
->n_ineq
);
11138 res
= isl_basic_map_add_constraints_dim_map(res
, bmap
, dim_map
);
11140 res
->flags
= flags
;
11141 res
= isl_basic_map_gauss(res
, NULL
);
11142 res
= isl_basic_map_finalize(res
);
11146 isl_basic_map_free(bmap
);
11147 isl_space_free(dim
);
11151 /* Reorder the dimensions of "map" according to given reordering.
11153 __isl_give isl_map
*isl_map_realign(__isl_take isl_map
*map
,
11154 __isl_take isl_reordering
*r
)
11157 struct isl_dim_map
*dim_map
;
11159 map
= isl_map_cow(map
);
11160 dim_map
= isl_dim_map_from_reordering(r
);
11161 if (!map
|| !r
|| !dim_map
)
11164 for (i
= 0; i
< map
->n
; ++i
) {
11165 struct isl_dim_map
*dim_map_i
;
11167 dim_map_i
= isl_dim_map_extend(dim_map
, map
->p
[i
]);
11169 map
->p
[i
] = isl_basic_map_realign(map
->p
[i
],
11170 isl_space_copy(r
->dim
), dim_map_i
);
11176 map
= isl_map_reset_space(map
, isl_space_copy(r
->dim
));
11178 isl_reordering_free(r
);
11184 isl_reordering_free(r
);
11188 __isl_give isl_set
*isl_set_realign(__isl_take isl_set
*set
,
11189 __isl_take isl_reordering
*r
)
11191 return set_from_map(isl_map_realign(set_to_map(set
), r
));
11194 __isl_give isl_map
*isl_map_align_params(__isl_take isl_map
*map
,
11195 __isl_take isl_space
*model
)
11200 if (!map
|| !model
)
11203 ctx
= isl_space_get_ctx(model
);
11204 if (!isl_space_has_named_params(model
))
11205 isl_die(ctx
, isl_error_invalid
,
11206 "model has unnamed parameters", goto error
);
11207 if (isl_map_check_named_params(map
) < 0)
11209 aligned
= isl_map_space_has_equal_params(map
, model
);
11213 isl_reordering
*exp
;
11215 model
= isl_space_drop_dims(model
, isl_dim_in
,
11216 0, isl_space_dim(model
, isl_dim_in
));
11217 model
= isl_space_drop_dims(model
, isl_dim_out
,
11218 0, isl_space_dim(model
, isl_dim_out
));
11219 exp
= isl_parameter_alignment_reordering(map
->dim
, model
);
11220 exp
= isl_reordering_extend_space(exp
, isl_map_get_space(map
));
11221 map
= isl_map_realign(map
, exp
);
11224 isl_space_free(model
);
11227 isl_space_free(model
);
11232 __isl_give isl_set
*isl_set_align_params(__isl_take isl_set
*set
,
11233 __isl_take isl_space
*model
)
11235 return isl_map_align_params(set
, model
);
11238 /* Align the parameters of "bmap" to those of "model", introducing
11239 * additional parameters if needed.
11241 __isl_give isl_basic_map
*isl_basic_map_align_params(
11242 __isl_take isl_basic_map
*bmap
, __isl_take isl_space
*model
)
11245 isl_bool equal_params
;
11247 if (!bmap
|| !model
)
11250 ctx
= isl_space_get_ctx(model
);
11251 if (!isl_space_has_named_params(model
))
11252 isl_die(ctx
, isl_error_invalid
,
11253 "model has unnamed parameters", goto error
);
11254 if (!isl_space_has_named_params(bmap
->dim
))
11255 isl_die(ctx
, isl_error_invalid
,
11256 "relation has unnamed parameters", goto error
);
11257 equal_params
= isl_space_has_equal_params(bmap
->dim
, model
);
11258 if (equal_params
< 0)
11260 if (!equal_params
) {
11261 isl_reordering
*exp
;
11262 struct isl_dim_map
*dim_map
;
11264 model
= isl_space_drop_dims(model
, isl_dim_in
,
11265 0, isl_space_dim(model
, isl_dim_in
));
11266 model
= isl_space_drop_dims(model
, isl_dim_out
,
11267 0, isl_space_dim(model
, isl_dim_out
));
11268 exp
= isl_parameter_alignment_reordering(bmap
->dim
, model
);
11269 exp
= isl_reordering_extend_space(exp
,
11270 isl_basic_map_get_space(bmap
));
11271 dim_map
= isl_dim_map_from_reordering(exp
);
11272 bmap
= isl_basic_map_realign(bmap
,
11273 exp
? isl_space_copy(exp
->dim
) : NULL
,
11274 isl_dim_map_extend(dim_map
, bmap
));
11275 isl_reordering_free(exp
);
11279 isl_space_free(model
);
11282 isl_space_free(model
);
11283 isl_basic_map_free(bmap
);
11287 /* Do "bset" and "space" have the same parameters?
11289 isl_bool
isl_basic_set_space_has_equal_params(__isl_keep isl_basic_set
*bset
,
11290 __isl_keep isl_space
*space
)
11292 isl_space
*bset_space
;
11294 bset_space
= isl_basic_set_peek_space(bset
);
11295 return isl_space_has_equal_params(bset_space
, space
);
11298 /* Do "map" and "space" have the same parameters?
11300 isl_bool
isl_map_space_has_equal_params(__isl_keep isl_map
*map
,
11301 __isl_keep isl_space
*space
)
11303 isl_space
*map_space
;
11305 map_space
= isl_map_peek_space(map
);
11306 return isl_space_has_equal_params(map_space
, space
);
11309 /* Do "set" and "space" have the same parameters?
11311 isl_bool
isl_set_space_has_equal_params(__isl_keep isl_set
*set
,
11312 __isl_keep isl_space
*space
)
11314 return isl_map_space_has_equal_params(set_to_map(set
), space
);
11317 /* Align the parameters of "bset" to those of "model", introducing
11318 * additional parameters if needed.
11320 __isl_give isl_basic_set
*isl_basic_set_align_params(
11321 __isl_take isl_basic_set
*bset
, __isl_take isl_space
*model
)
11323 return isl_basic_map_align_params(bset
, model
);
11326 __isl_give isl_mat
*isl_basic_map_equalities_matrix(
11327 __isl_keep isl_basic_map
*bmap
, enum isl_dim_type c1
,
11328 enum isl_dim_type c2
, enum isl_dim_type c3
,
11329 enum isl_dim_type c4
, enum isl_dim_type c5
)
11331 enum isl_dim_type c
[5] = { c1
, c2
, c3
, c4
, c5
};
11332 struct isl_mat
*mat
;
11338 mat
= isl_mat_alloc(bmap
->ctx
, bmap
->n_eq
,
11339 isl_basic_map_total_dim(bmap
) + 1);
11342 for (i
= 0; i
< bmap
->n_eq
; ++i
)
11343 for (j
= 0, pos
= 0; j
< 5; ++j
) {
11344 int off
= isl_basic_map_offset(bmap
, c
[j
]);
11345 for (k
= 0; k
< isl_basic_map_dim(bmap
, c
[j
]); ++k
) {
11346 isl_int_set(mat
->row
[i
][pos
],
11347 bmap
->eq
[i
][off
+ k
]);
11355 __isl_give isl_mat
*isl_basic_map_inequalities_matrix(
11356 __isl_keep isl_basic_map
*bmap
, enum isl_dim_type c1
,
11357 enum isl_dim_type c2
, enum isl_dim_type c3
,
11358 enum isl_dim_type c4
, enum isl_dim_type c5
)
11360 enum isl_dim_type c
[5] = { c1
, c2
, c3
, c4
, c5
};
11361 struct isl_mat
*mat
;
11367 mat
= isl_mat_alloc(bmap
->ctx
, bmap
->n_ineq
,
11368 isl_basic_map_total_dim(bmap
) + 1);
11371 for (i
= 0; i
< bmap
->n_ineq
; ++i
)
11372 for (j
= 0, pos
= 0; j
< 5; ++j
) {
11373 int off
= isl_basic_map_offset(bmap
, c
[j
]);
11374 for (k
= 0; k
< isl_basic_map_dim(bmap
, c
[j
]); ++k
) {
11375 isl_int_set(mat
->row
[i
][pos
],
11376 bmap
->ineq
[i
][off
+ k
]);
11384 __isl_give isl_basic_map
*isl_basic_map_from_constraint_matrices(
11385 __isl_take isl_space
*dim
,
11386 __isl_take isl_mat
*eq
, __isl_take isl_mat
*ineq
, enum isl_dim_type c1
,
11387 enum isl_dim_type c2
, enum isl_dim_type c3
,
11388 enum isl_dim_type c4
, enum isl_dim_type c5
)
11390 enum isl_dim_type c
[5] = { c1
, c2
, c3
, c4
, c5
};
11391 isl_basic_map
*bmap
;
11397 if (!dim
|| !eq
|| !ineq
)
11400 if (eq
->n_col
!= ineq
->n_col
)
11401 isl_die(dim
->ctx
, isl_error_invalid
,
11402 "equalities and inequalities matrices should have "
11403 "same number of columns", goto error
);
11405 total
= 1 + isl_space_dim(dim
, isl_dim_all
);
11407 if (eq
->n_col
< total
)
11408 isl_die(dim
->ctx
, isl_error_invalid
,
11409 "number of columns too small", goto error
);
11411 extra
= eq
->n_col
- total
;
11413 bmap
= isl_basic_map_alloc_space(isl_space_copy(dim
), extra
,
11414 eq
->n_row
, ineq
->n_row
);
11417 for (i
= 0; i
< extra
; ++i
) {
11418 k
= isl_basic_map_alloc_div(bmap
);
11421 isl_int_set_si(bmap
->div
[k
][0], 0);
11423 for (i
= 0; i
< eq
->n_row
; ++i
) {
11424 l
= isl_basic_map_alloc_equality(bmap
);
11427 for (j
= 0, pos
= 0; j
< 5; ++j
) {
11428 int off
= isl_basic_map_offset(bmap
, c
[j
]);
11429 for (k
= 0; k
< isl_basic_map_dim(bmap
, c
[j
]); ++k
) {
11430 isl_int_set(bmap
->eq
[l
][off
+ k
],
11436 for (i
= 0; i
< ineq
->n_row
; ++i
) {
11437 l
= isl_basic_map_alloc_inequality(bmap
);
11440 for (j
= 0, pos
= 0; j
< 5; ++j
) {
11441 int off
= isl_basic_map_offset(bmap
, c
[j
]);
11442 for (k
= 0; k
< isl_basic_map_dim(bmap
, c
[j
]); ++k
) {
11443 isl_int_set(bmap
->ineq
[l
][off
+ k
],
11444 ineq
->row
[i
][pos
]);
11450 isl_space_free(dim
);
11452 isl_mat_free(ineq
);
11454 bmap
= isl_basic_map_simplify(bmap
);
11455 return isl_basic_map_finalize(bmap
);
11457 isl_space_free(dim
);
11459 isl_mat_free(ineq
);
11463 __isl_give isl_mat
*isl_basic_set_equalities_matrix(
11464 __isl_keep isl_basic_set
*bset
, enum isl_dim_type c1
,
11465 enum isl_dim_type c2
, enum isl_dim_type c3
, enum isl_dim_type c4
)
11467 return isl_basic_map_equalities_matrix(bset_to_bmap(bset
),
11468 c1
, c2
, c3
, c4
, isl_dim_in
);
11471 __isl_give isl_mat
*isl_basic_set_inequalities_matrix(
11472 __isl_keep isl_basic_set
*bset
, enum isl_dim_type c1
,
11473 enum isl_dim_type c2
, enum isl_dim_type c3
, enum isl_dim_type c4
)
11475 return isl_basic_map_inequalities_matrix(bset_to_bmap(bset
),
11476 c1
, c2
, c3
, c4
, isl_dim_in
);
11479 __isl_give isl_basic_set
*isl_basic_set_from_constraint_matrices(
11480 __isl_take isl_space
*dim
,
11481 __isl_take isl_mat
*eq
, __isl_take isl_mat
*ineq
, enum isl_dim_type c1
,
11482 enum isl_dim_type c2
, enum isl_dim_type c3
, enum isl_dim_type c4
)
11484 isl_basic_map
*bmap
;
11485 bmap
= isl_basic_map_from_constraint_matrices(dim
, eq
, ineq
,
11486 c1
, c2
, c3
, c4
, isl_dim_in
);
11487 return bset_from_bmap(bmap
);
11490 isl_bool
isl_basic_map_can_zip(__isl_keep isl_basic_map
*bmap
)
11493 return isl_bool_error
;
11495 return isl_space_can_zip(bmap
->dim
);
11498 isl_bool
isl_map_can_zip(__isl_keep isl_map
*map
)
11501 return isl_bool_error
;
11503 return isl_space_can_zip(map
->dim
);
11506 /* Given a basic map (A -> B) -> (C -> D), return the corresponding basic map
11507 * (A -> C) -> (B -> D).
11509 __isl_give isl_basic_map
*isl_basic_map_zip(__isl_take isl_basic_map
*bmap
)
11518 if (!isl_basic_map_can_zip(bmap
))
11519 isl_die(bmap
->ctx
, isl_error_invalid
,
11520 "basic map cannot be zipped", goto error
);
11521 pos
= isl_basic_map_offset(bmap
, isl_dim_in
) +
11522 isl_space_dim(bmap
->dim
->nested
[0], isl_dim_in
);
11523 n1
= isl_space_dim(bmap
->dim
->nested
[0], isl_dim_out
);
11524 n2
= isl_space_dim(bmap
->dim
->nested
[1], isl_dim_in
);
11525 bmap
= isl_basic_map_cow(bmap
);
11526 bmap
= isl_basic_map_swap_vars(bmap
, pos
, n1
, n2
);
11529 bmap
->dim
= isl_space_zip(bmap
->dim
);
11532 bmap
= isl_basic_map_mark_final(bmap
);
11535 isl_basic_map_free(bmap
);
11539 /* Given a map (A -> B) -> (C -> D), return the corresponding map
11540 * (A -> C) -> (B -> D).
11542 __isl_give isl_map
*isl_map_zip(__isl_take isl_map
*map
)
11549 if (!isl_map_can_zip(map
))
11550 isl_die(map
->ctx
, isl_error_invalid
, "map cannot be zipped",
11553 map
= isl_map_cow(map
);
11557 for (i
= 0; i
< map
->n
; ++i
) {
11558 map
->p
[i
] = isl_basic_map_zip(map
->p
[i
]);
11563 map
->dim
= isl_space_zip(map
->dim
);
11573 /* Can we apply isl_basic_map_curry to "bmap"?
11574 * That is, does it have a nested relation in its domain?
11576 isl_bool
isl_basic_map_can_curry(__isl_keep isl_basic_map
*bmap
)
11579 return isl_bool_error
;
11581 return isl_space_can_curry(bmap
->dim
);
11584 /* Can we apply isl_map_curry to "map"?
11585 * That is, does it have a nested relation in its domain?
11587 isl_bool
isl_map_can_curry(__isl_keep isl_map
*map
)
11590 return isl_bool_error
;
11592 return isl_space_can_curry(map
->dim
);
11595 /* Given a basic map (A -> B) -> C, return the corresponding basic map
11598 __isl_give isl_basic_map
*isl_basic_map_curry(__isl_take isl_basic_map
*bmap
)
11604 if (!isl_basic_map_can_curry(bmap
))
11605 isl_die(bmap
->ctx
, isl_error_invalid
,
11606 "basic map cannot be curried", goto error
);
11607 bmap
= isl_basic_map_cow(bmap
);
11610 bmap
->dim
= isl_space_curry(bmap
->dim
);
11613 bmap
= isl_basic_map_mark_final(bmap
);
11616 isl_basic_map_free(bmap
);
11620 /* Given a map (A -> B) -> C, return the corresponding map
11623 __isl_give isl_map
*isl_map_curry(__isl_take isl_map
*map
)
11625 return isl_map_change_space(map
, &isl_map_can_curry
,
11626 "map cannot be curried", &isl_space_curry
);
11629 /* Can isl_map_range_curry be applied to "map"?
11630 * That is, does it have a nested relation in its range,
11631 * the domain of which is itself a nested relation?
11633 isl_bool
isl_map_can_range_curry(__isl_keep isl_map
*map
)
11636 return isl_bool_error
;
11638 return isl_space_can_range_curry(map
->dim
);
11641 /* Given a map A -> ((B -> C) -> D), return the corresponding map
11642 * A -> (B -> (C -> D)).
11644 __isl_give isl_map
*isl_map_range_curry(__isl_take isl_map
*map
)
11646 return isl_map_change_space(map
, &isl_map_can_range_curry
,
11647 "map range cannot be curried",
11648 &isl_space_range_curry
);
11651 /* Can we apply isl_basic_map_uncurry to "bmap"?
11652 * That is, does it have a nested relation in its domain?
11654 isl_bool
isl_basic_map_can_uncurry(__isl_keep isl_basic_map
*bmap
)
11657 return isl_bool_error
;
11659 return isl_space_can_uncurry(bmap
->dim
);
11662 /* Can we apply isl_map_uncurry to "map"?
11663 * That is, does it have a nested relation in its domain?
11665 isl_bool
isl_map_can_uncurry(__isl_keep isl_map
*map
)
11668 return isl_bool_error
;
11670 return isl_space_can_uncurry(map
->dim
);
11673 /* Given a basic map A -> (B -> C), return the corresponding basic map
11676 __isl_give isl_basic_map
*isl_basic_map_uncurry(__isl_take isl_basic_map
*bmap
)
11682 if (!isl_basic_map_can_uncurry(bmap
))
11683 isl_die(bmap
->ctx
, isl_error_invalid
,
11684 "basic map cannot be uncurried",
11685 return isl_basic_map_free(bmap
));
11686 bmap
= isl_basic_map_cow(bmap
);
11689 bmap
->dim
= isl_space_uncurry(bmap
->dim
);
11691 return isl_basic_map_free(bmap
);
11692 bmap
= isl_basic_map_mark_final(bmap
);
11696 /* Given a map A -> (B -> C), return the corresponding map
11699 __isl_give isl_map
*isl_map_uncurry(__isl_take isl_map
*map
)
11701 return isl_map_change_space(map
, &isl_map_can_uncurry
,
11702 "map cannot be uncurried", &isl_space_uncurry
);
11705 /* Construct a basic map mapping the domain of the affine expression
11706 * to a one-dimensional range prescribed by the affine expression.
11707 * If "rational" is set, then construct a rational basic map.
11709 * A NaN affine expression cannot be converted to a basic map.
11711 static __isl_give isl_basic_map
*isl_basic_map_from_aff2(
11712 __isl_take isl_aff
*aff
, int rational
)
11717 isl_local_space
*ls
;
11718 isl_basic_map
*bmap
= NULL
;
11722 is_nan
= isl_aff_is_nan(aff
);
11726 isl_die(isl_aff_get_ctx(aff
), isl_error_invalid
,
11727 "cannot convert NaN", goto error
);
11729 ls
= isl_aff_get_local_space(aff
);
11730 bmap
= isl_basic_map_from_local_space(ls
);
11731 bmap
= isl_basic_map_extend_constraints(bmap
, 1, 0);
11732 k
= isl_basic_map_alloc_equality(bmap
);
11736 pos
= isl_basic_map_offset(bmap
, isl_dim_out
);
11737 isl_seq_cpy(bmap
->eq
[k
], aff
->v
->el
+ 1, pos
);
11738 isl_int_neg(bmap
->eq
[k
][pos
], aff
->v
->el
[0]);
11739 isl_seq_cpy(bmap
->eq
[k
] + pos
+ 1, aff
->v
->el
+ 1 + pos
,
11740 aff
->v
->size
- (pos
+ 1));
11744 bmap
= isl_basic_map_set_rational(bmap
);
11745 bmap
= isl_basic_map_finalize(bmap
);
11749 isl_basic_map_free(bmap
);
11753 /* Construct a basic map mapping the domain of the affine expression
11754 * to a one-dimensional range prescribed by the affine expression.
11756 __isl_give isl_basic_map
*isl_basic_map_from_aff(__isl_take isl_aff
*aff
)
11758 return isl_basic_map_from_aff2(aff
, 0);
11761 /* Construct a map mapping the domain of the affine expression
11762 * to a one-dimensional range prescribed by the affine expression.
11764 __isl_give isl_map
*isl_map_from_aff(__isl_take isl_aff
*aff
)
11766 isl_basic_map
*bmap
;
11768 bmap
= isl_basic_map_from_aff(aff
);
11769 return isl_map_from_basic_map(bmap
);
11772 /* Construct a basic map mapping the domain the multi-affine expression
11773 * to its range, with each dimension in the range equated to the
11774 * corresponding affine expression.
11775 * If "rational" is set, then construct a rational basic map.
11777 __isl_give isl_basic_map
*isl_basic_map_from_multi_aff2(
11778 __isl_take isl_multi_aff
*maff
, int rational
)
11782 isl_basic_map
*bmap
;
11787 if (isl_space_dim(maff
->space
, isl_dim_out
) != maff
->n
)
11788 isl_die(isl_multi_aff_get_ctx(maff
), isl_error_internal
,
11789 "invalid space", goto error
);
11791 space
= isl_space_domain(isl_multi_aff_get_space(maff
));
11792 bmap
= isl_basic_map_universe(isl_space_from_domain(space
));
11794 bmap
= isl_basic_map_set_rational(bmap
);
11796 for (i
= 0; i
< maff
->n
; ++i
) {
11798 isl_basic_map
*bmap_i
;
11800 aff
= isl_aff_copy(maff
->p
[i
]);
11801 bmap_i
= isl_basic_map_from_aff2(aff
, rational
);
11803 bmap
= isl_basic_map_flat_range_product(bmap
, bmap_i
);
11806 bmap
= isl_basic_map_reset_space(bmap
, isl_multi_aff_get_space(maff
));
11808 isl_multi_aff_free(maff
);
11811 isl_multi_aff_free(maff
);
11815 /* Construct a basic map mapping the domain the multi-affine expression
11816 * to its range, with each dimension in the range equated to the
11817 * corresponding affine expression.
11819 __isl_give isl_basic_map
*isl_basic_map_from_multi_aff(
11820 __isl_take isl_multi_aff
*ma
)
11822 return isl_basic_map_from_multi_aff2(ma
, 0);
11825 /* Construct a map mapping the domain the multi-affine expression
11826 * to its range, with each dimension in the range equated to the
11827 * corresponding affine expression.
11829 __isl_give isl_map
*isl_map_from_multi_aff(__isl_take isl_multi_aff
*maff
)
11831 isl_basic_map
*bmap
;
11833 bmap
= isl_basic_map_from_multi_aff(maff
);
11834 return isl_map_from_basic_map(bmap
);
11837 /* Construct a basic map mapping a domain in the given space to
11838 * to an n-dimensional range, with n the number of elements in the list,
11839 * where each coordinate in the range is prescribed by the
11840 * corresponding affine expression.
11841 * The domains of all affine expressions in the list are assumed to match
11844 __isl_give isl_basic_map
*isl_basic_map_from_aff_list(
11845 __isl_take isl_space
*domain_dim
, __isl_take isl_aff_list
*list
)
11849 isl_basic_map
*bmap
;
11854 dim
= isl_space_from_domain(domain_dim
);
11855 bmap
= isl_basic_map_universe(dim
);
11857 for (i
= 0; i
< list
->n
; ++i
) {
11859 isl_basic_map
*bmap_i
;
11861 aff
= isl_aff_copy(list
->p
[i
]);
11862 bmap_i
= isl_basic_map_from_aff(aff
);
11864 bmap
= isl_basic_map_flat_range_product(bmap
, bmap_i
);
11867 isl_aff_list_free(list
);
11871 __isl_give isl_set
*isl_set_equate(__isl_take isl_set
*set
,
11872 enum isl_dim_type type1
, int pos1
, enum isl_dim_type type2
, int pos2
)
11874 return isl_map_equate(set
, type1
, pos1
, type2
, pos2
);
11877 /* Construct a basic map where the given dimensions are equal to each other.
11879 static __isl_give isl_basic_map
*equator(__isl_take isl_space
*space
,
11880 enum isl_dim_type type1
, int pos1
, enum isl_dim_type type2
, int pos2
)
11882 isl_basic_map
*bmap
= NULL
;
11888 if (pos1
>= isl_space_dim(space
, type1
))
11889 isl_die(isl_space_get_ctx(space
), isl_error_invalid
,
11890 "index out of bounds", goto error
);
11891 if (pos2
>= isl_space_dim(space
, type2
))
11892 isl_die(isl_space_get_ctx(space
), isl_error_invalid
,
11893 "index out of bounds", goto error
);
11895 if (type1
== type2
&& pos1
== pos2
)
11896 return isl_basic_map_universe(space
);
11898 bmap
= isl_basic_map_alloc_space(isl_space_copy(space
), 0, 1, 0);
11899 i
= isl_basic_map_alloc_equality(bmap
);
11902 isl_seq_clr(bmap
->eq
[i
], 1 + isl_basic_map_total_dim(bmap
));
11903 pos1
+= isl_basic_map_offset(bmap
, type1
);
11904 pos2
+= isl_basic_map_offset(bmap
, type2
);
11905 isl_int_set_si(bmap
->eq
[i
][pos1
], -1);
11906 isl_int_set_si(bmap
->eq
[i
][pos2
], 1);
11907 bmap
= isl_basic_map_finalize(bmap
);
11908 isl_space_free(space
);
11911 isl_space_free(space
);
11912 isl_basic_map_free(bmap
);
11916 /* Add a constraint imposing that the given two dimensions are equal.
11918 __isl_give isl_basic_map
*isl_basic_map_equate(__isl_take isl_basic_map
*bmap
,
11919 enum isl_dim_type type1
, int pos1
, enum isl_dim_type type2
, int pos2
)
11923 eq
= equator(isl_basic_map_get_space(bmap
), type1
, pos1
, type2
, pos2
);
11925 bmap
= isl_basic_map_intersect(bmap
, eq
);
11930 /* Add a constraint imposing that the given two dimensions are equal.
11932 __isl_give isl_map
*isl_map_equate(__isl_take isl_map
*map
,
11933 enum isl_dim_type type1
, int pos1
, enum isl_dim_type type2
, int pos2
)
11935 isl_basic_map
*bmap
;
11937 bmap
= equator(isl_map_get_space(map
), type1
, pos1
, type2
, pos2
);
11939 map
= isl_map_intersect(map
, isl_map_from_basic_map(bmap
));
11944 /* Add a constraint imposing that the given two dimensions have opposite values.
11946 __isl_give isl_map
*isl_map_oppose(__isl_take isl_map
*map
,
11947 enum isl_dim_type type1
, int pos1
, enum isl_dim_type type2
, int pos2
)
11949 isl_basic_map
*bmap
= NULL
;
11955 if (pos1
>= isl_map_dim(map
, type1
))
11956 isl_die(map
->ctx
, isl_error_invalid
,
11957 "index out of bounds", goto error
);
11958 if (pos2
>= isl_map_dim(map
, type2
))
11959 isl_die(map
->ctx
, isl_error_invalid
,
11960 "index out of bounds", goto error
);
11962 bmap
= isl_basic_map_alloc_space(isl_map_get_space(map
), 0, 1, 0);
11963 i
= isl_basic_map_alloc_equality(bmap
);
11966 isl_seq_clr(bmap
->eq
[i
], 1 + isl_basic_map_total_dim(bmap
));
11967 pos1
+= isl_basic_map_offset(bmap
, type1
);
11968 pos2
+= isl_basic_map_offset(bmap
, type2
);
11969 isl_int_set_si(bmap
->eq
[i
][pos1
], 1);
11970 isl_int_set_si(bmap
->eq
[i
][pos2
], 1);
11971 bmap
= isl_basic_map_finalize(bmap
);
11973 map
= isl_map_intersect(map
, isl_map_from_basic_map(bmap
));
11977 isl_basic_map_free(bmap
);
11982 /* Construct a constraint imposing that the value of the first dimension is
11983 * greater than or equal to that of the second.
11985 static __isl_give isl_constraint
*constraint_order_ge(
11986 __isl_take isl_space
*space
, enum isl_dim_type type1
, int pos1
,
11987 enum isl_dim_type type2
, int pos2
)
11994 c
= isl_constraint_alloc_inequality(isl_local_space_from_space(space
));
11996 if (pos1
>= isl_constraint_dim(c
, type1
))
11997 isl_die(isl_constraint_get_ctx(c
), isl_error_invalid
,
11998 "index out of bounds", return isl_constraint_free(c
));
11999 if (pos2
>= isl_constraint_dim(c
, type2
))
12000 isl_die(isl_constraint_get_ctx(c
), isl_error_invalid
,
12001 "index out of bounds", return isl_constraint_free(c
));
12003 if (type1
== type2
&& pos1
== pos2
)
12006 c
= isl_constraint_set_coefficient_si(c
, type1
, pos1
, 1);
12007 c
= isl_constraint_set_coefficient_si(c
, type2
, pos2
, -1);
12012 /* Add a constraint imposing that the value of the first dimension is
12013 * greater than or equal to that of the second.
12015 __isl_give isl_basic_map
*isl_basic_map_order_ge(__isl_take isl_basic_map
*bmap
,
12016 enum isl_dim_type type1
, int pos1
, enum isl_dim_type type2
, int pos2
)
12021 if (type1
== type2
&& pos1
== pos2
)
12023 space
= isl_basic_map_get_space(bmap
);
12024 c
= constraint_order_ge(space
, type1
, pos1
, type2
, pos2
);
12025 bmap
= isl_basic_map_add_constraint(bmap
, c
);
12030 /* Add a constraint imposing that the value of the first dimension is
12031 * greater than or equal to that of the second.
12033 __isl_give isl_map
*isl_map_order_ge(__isl_take isl_map
*map
,
12034 enum isl_dim_type type1
, int pos1
, enum isl_dim_type type2
, int pos2
)
12039 if (type1
== type2
&& pos1
== pos2
)
12041 space
= isl_map_get_space(map
);
12042 c
= constraint_order_ge(space
, type1
, pos1
, type2
, pos2
);
12043 map
= isl_map_add_constraint(map
, c
);
12048 /* Add a constraint imposing that the value of the first dimension is
12049 * less than or equal to that of the second.
12051 __isl_give isl_map
*isl_map_order_le(__isl_take isl_map
*map
,
12052 enum isl_dim_type type1
, int pos1
, enum isl_dim_type type2
, int pos2
)
12054 return isl_map_order_ge(map
, type2
, pos2
, type1
, pos1
);
12057 /* Construct a basic map where the value of the first dimension is
12058 * greater than that of the second.
12060 static __isl_give isl_basic_map
*greator(__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_empty(space
);
12079 bmap
= isl_basic_map_alloc_space(space
, 0, 0, 1);
12080 i
= isl_basic_map_alloc_inequality(bmap
);
12082 return isl_basic_map_free(bmap
);
12083 isl_seq_clr(bmap
->ineq
[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
->ineq
[i
][pos1
], 1);
12087 isl_int_set_si(bmap
->ineq
[i
][pos2
], -1);
12088 isl_int_set_si(bmap
->ineq
[i
][0], -1);
12089 bmap
= isl_basic_map_finalize(bmap
);
12093 isl_space_free(space
);
12094 isl_basic_map_free(bmap
);
12098 /* Add a constraint imposing that the value of the first dimension is
12099 * greater than that of the second.
12101 __isl_give isl_basic_map
*isl_basic_map_order_gt(__isl_take isl_basic_map
*bmap
,
12102 enum isl_dim_type type1
, int pos1
, enum isl_dim_type type2
, int pos2
)
12106 gt
= greator(isl_basic_map_get_space(bmap
), type1
, pos1
, type2
, pos2
);
12108 bmap
= isl_basic_map_intersect(bmap
, gt
);
12113 /* Add a constraint imposing that the value of the first dimension is
12114 * greater than that of the second.
12116 __isl_give isl_map
*isl_map_order_gt(__isl_take isl_map
*map
,
12117 enum isl_dim_type type1
, int pos1
, enum isl_dim_type type2
, int pos2
)
12119 isl_basic_map
*bmap
;
12121 bmap
= greator(isl_map_get_space(map
), type1
, pos1
, type2
, pos2
);
12123 map
= isl_map_intersect(map
, isl_map_from_basic_map(bmap
));
12128 /* Add a constraint imposing that the value of the first dimension is
12129 * smaller than that of the second.
12131 __isl_give isl_map
*isl_map_order_lt(__isl_take isl_map
*map
,
12132 enum isl_dim_type type1
, int pos1
, enum isl_dim_type type2
, int pos2
)
12134 return isl_map_order_gt(map
, type2
, pos2
, type1
, pos1
);
12137 __isl_give isl_aff
*isl_basic_map_get_div(__isl_keep isl_basic_map
*bmap
,
12141 isl_local_space
*ls
;
12146 if (!isl_basic_map_divs_known(bmap
))
12147 isl_die(isl_basic_map_get_ctx(bmap
), isl_error_invalid
,
12148 "some divs are unknown", return NULL
);
12150 ls
= isl_basic_map_get_local_space(bmap
);
12151 div
= isl_local_space_get_div(ls
, pos
);
12152 isl_local_space_free(ls
);
12157 __isl_give isl_aff
*isl_basic_set_get_div(__isl_keep isl_basic_set
*bset
,
12160 return isl_basic_map_get_div(bset
, pos
);
12163 /* Plug in "subs" for dimension "type", "pos" of "bset".
12165 * Let i be the dimension to replace and let "subs" be of the form
12169 * Any integer division with a non-zero coefficient for i,
12171 * floor((a i + g)/m)
12175 * floor((a f + d g)/(m d))
12177 * Constraints of the form
12185 * We currently require that "subs" is an integral expression.
12186 * Handling rational expressions may require us to add stride constraints
12187 * as we do in isl_basic_set_preimage_multi_aff.
12189 __isl_give isl_basic_set
*isl_basic_set_substitute(
12190 __isl_take isl_basic_set
*bset
,
12191 enum isl_dim_type type
, unsigned pos
, __isl_keep isl_aff
*subs
)
12197 if (bset
&& isl_basic_set_plain_is_empty(bset
))
12200 bset
= isl_basic_set_cow(bset
);
12201 if (!bset
|| !subs
)
12204 ctx
= isl_basic_set_get_ctx(bset
);
12205 if (!isl_space_is_equal(bset
->dim
, subs
->ls
->dim
))
12206 isl_die(ctx
, isl_error_invalid
,
12207 "spaces don't match", goto error
);
12208 if (isl_local_space_dim(subs
->ls
, isl_dim_div
) != 0)
12209 isl_die(ctx
, isl_error_unsupported
,
12210 "cannot handle divs yet", goto error
);
12211 if (!isl_int_is_one(subs
->v
->el
[0]))
12212 isl_die(ctx
, isl_error_invalid
,
12213 "can only substitute integer expressions", goto error
);
12215 pos
+= isl_basic_set_offset(bset
, type
);
12219 for (i
= 0; i
< bset
->n_eq
; ++i
) {
12220 if (isl_int_is_zero(bset
->eq
[i
][pos
]))
12222 isl_int_set(v
, bset
->eq
[i
][pos
]);
12223 isl_int_set_si(bset
->eq
[i
][pos
], 0);
12224 isl_seq_combine(bset
->eq
[i
], subs
->v
->el
[0], bset
->eq
[i
],
12225 v
, subs
->v
->el
+ 1, subs
->v
->size
- 1);
12228 for (i
= 0; i
< bset
->n_ineq
; ++i
) {
12229 if (isl_int_is_zero(bset
->ineq
[i
][pos
]))
12231 isl_int_set(v
, bset
->ineq
[i
][pos
]);
12232 isl_int_set_si(bset
->ineq
[i
][pos
], 0);
12233 isl_seq_combine(bset
->ineq
[i
], subs
->v
->el
[0], bset
->ineq
[i
],
12234 v
, subs
->v
->el
+ 1, subs
->v
->size
- 1);
12237 for (i
= 0; i
< bset
->n_div
; ++i
) {
12238 if (isl_int_is_zero(bset
->div
[i
][1 + pos
]))
12240 isl_int_set(v
, bset
->div
[i
][1 + pos
]);
12241 isl_int_set_si(bset
->div
[i
][1 + pos
], 0);
12242 isl_seq_combine(bset
->div
[i
] + 1,
12243 subs
->v
->el
[0], bset
->div
[i
] + 1,
12244 v
, subs
->v
->el
+ 1, subs
->v
->size
- 1);
12245 isl_int_mul(bset
->div
[i
][0], bset
->div
[i
][0], subs
->v
->el
[0]);
12250 bset
= isl_basic_set_simplify(bset
);
12251 return isl_basic_set_finalize(bset
);
12253 isl_basic_set_free(bset
);
12257 /* Plug in "subs" for dimension "type", "pos" of "set".
12259 __isl_give isl_set
*isl_set_substitute(__isl_take isl_set
*set
,
12260 enum isl_dim_type type
, unsigned pos
, __isl_keep isl_aff
*subs
)
12264 if (set
&& isl_set_plain_is_empty(set
))
12267 set
= isl_set_cow(set
);
12271 for (i
= set
->n
- 1; i
>= 0; --i
) {
12272 set
->p
[i
] = isl_basic_set_substitute(set
->p
[i
], type
, pos
, subs
);
12273 if (remove_if_empty(set
, i
) < 0)
12283 /* Check if the range of "ma" is compatible with the domain or range
12284 * (depending on "type") of "bmap".
12286 static isl_stat
check_basic_map_compatible_range_multi_aff(
12287 __isl_keep isl_basic_map
*bmap
, enum isl_dim_type type
,
12288 __isl_keep isl_multi_aff
*ma
)
12291 isl_space
*ma_space
;
12293 ma_space
= isl_multi_aff_get_space(ma
);
12295 m
= isl_space_has_equal_params(bmap
->dim
, ma_space
);
12299 isl_die(isl_basic_map_get_ctx(bmap
), isl_error_invalid
,
12300 "parameters don't match", goto error
);
12301 m
= isl_space_tuple_is_equal(bmap
->dim
, type
, ma_space
, isl_dim_out
);
12305 isl_die(isl_basic_map_get_ctx(bmap
), isl_error_invalid
,
12306 "spaces don't match", goto error
);
12308 isl_space_free(ma_space
);
12309 return isl_stat_ok
;
12311 isl_space_free(ma_space
);
12312 return isl_stat_error
;
12315 /* Copy the divs from "ma" to "bmap", adding zeros for the "n_before"
12316 * coefficients before the transformed range of dimensions,
12317 * the "n_after" coefficients after the transformed range of dimensions
12318 * and the coefficients of the other divs in "bmap".
12320 static int set_ma_divs(__isl_keep isl_basic_map
*bmap
,
12321 __isl_keep isl_multi_aff
*ma
, int n_before
, int n_after
, int n_div
)
12326 isl_local_space
*ls
;
12331 ls
= isl_aff_get_domain_local_space(ma
->p
[0]);
12335 n_param
= isl_local_space_dim(ls
, isl_dim_param
);
12336 n_set
= isl_local_space_dim(ls
, isl_dim_set
);
12337 for (i
= 0; i
< n_div
; ++i
) {
12338 int o_bmap
= 0, o_ls
= 0;
12340 isl_seq_cpy(bmap
->div
[i
], ls
->div
->row
[i
], 1 + 1 + n_param
);
12341 o_bmap
+= 1 + 1 + n_param
;
12342 o_ls
+= 1 + 1 + n_param
;
12343 isl_seq_clr(bmap
->div
[i
] + o_bmap
, n_before
);
12344 o_bmap
+= n_before
;
12345 isl_seq_cpy(bmap
->div
[i
] + o_bmap
,
12346 ls
->div
->row
[i
] + o_ls
, n_set
);
12349 isl_seq_clr(bmap
->div
[i
] + o_bmap
, n_after
);
12351 isl_seq_cpy(bmap
->div
[i
] + o_bmap
,
12352 ls
->div
->row
[i
] + o_ls
, n_div
);
12355 isl_seq_clr(bmap
->div
[i
] + o_bmap
, bmap
->n_div
- n_div
);
12356 if (isl_basic_map_add_div_constraints(bmap
, i
) < 0)
12360 isl_local_space_free(ls
);
12363 isl_local_space_free(ls
);
12367 /* How many stride constraints does "ma" enforce?
12368 * That is, how many of the affine expressions have a denominator
12369 * different from one?
12371 static int multi_aff_strides(__isl_keep isl_multi_aff
*ma
)
12376 for (i
= 0; i
< ma
->n
; ++i
)
12377 if (!isl_int_is_one(ma
->p
[i
]->v
->el
[0]))
12383 /* For each affine expression in ma of the form
12385 * x_i = (f_i y + h_i)/m_i
12387 * with m_i different from one, add a constraint to "bmap"
12390 * f_i y + h_i = m_i alpha_i
12392 * with alpha_i an additional existentially quantified variable.
12394 * The input variables of "ma" correspond to a subset of the variables
12395 * of "bmap". There are "n_before" variables in "bmap" before this
12396 * subset and "n_after" variables after this subset.
12397 * The integer divisions of the affine expressions in "ma" are assumed
12398 * to have been aligned. There are "n_div_ma" of them and
12399 * they appear first in "bmap", straight after the "n_after" variables.
12401 static __isl_give isl_basic_map
*add_ma_strides(
12402 __isl_take isl_basic_map
*bmap
, __isl_keep isl_multi_aff
*ma
,
12403 int n_before
, int n_after
, int n_div_ma
)
12411 total
= isl_basic_map_total_dim(bmap
);
12412 n_param
= isl_multi_aff_dim(ma
, isl_dim_param
);
12413 n_in
= isl_multi_aff_dim(ma
, isl_dim_in
);
12414 for (i
= 0; i
< ma
->n
; ++i
) {
12415 int o_bmap
= 0, o_ma
= 1;
12417 if (isl_int_is_one(ma
->p
[i
]->v
->el
[0]))
12419 div
= isl_basic_map_alloc_div(bmap
);
12420 k
= isl_basic_map_alloc_equality(bmap
);
12421 if (div
< 0 || k
< 0)
12423 isl_int_set_si(bmap
->div
[div
][0], 0);
12424 isl_seq_cpy(bmap
->eq
[k
] + o_bmap
,
12425 ma
->p
[i
]->v
->el
+ o_ma
, 1 + n_param
);
12426 o_bmap
+= 1 + n_param
;
12427 o_ma
+= 1 + n_param
;
12428 isl_seq_clr(bmap
->eq
[k
] + o_bmap
, n_before
);
12429 o_bmap
+= n_before
;
12430 isl_seq_cpy(bmap
->eq
[k
] + o_bmap
,
12431 ma
->p
[i
]->v
->el
+ o_ma
, n_in
);
12434 isl_seq_clr(bmap
->eq
[k
] + o_bmap
, n_after
);
12436 isl_seq_cpy(bmap
->eq
[k
] + o_bmap
,
12437 ma
->p
[i
]->v
->el
+ o_ma
, n_div_ma
);
12438 o_bmap
+= n_div_ma
;
12440 isl_seq_clr(bmap
->eq
[k
] + o_bmap
, 1 + total
- o_bmap
);
12441 isl_int_neg(bmap
->eq
[k
][1 + total
], ma
->p
[i
]->v
->el
[0]);
12447 isl_basic_map_free(bmap
);
12451 /* Replace the domain or range space (depending on "type) of "space" by "set".
12453 static __isl_give isl_space
*isl_space_set(__isl_take isl_space
*space
,
12454 enum isl_dim_type type
, __isl_take isl_space
*set
)
12456 if (type
== isl_dim_in
) {
12457 space
= isl_space_range(space
);
12458 space
= isl_space_map_from_domain_and_range(set
, space
);
12460 space
= isl_space_domain(space
);
12461 space
= isl_space_map_from_domain_and_range(space
, set
);
12467 /* Compute the preimage of the domain or range (depending on "type")
12468 * of "bmap" under the function represented by "ma".
12469 * In other words, plug in "ma" in the domain or range of "bmap".
12470 * The result is a basic map that lives in the same space as "bmap"
12471 * except that the domain or range has been replaced by
12472 * the domain space of "ma".
12474 * If bmap is represented by
12476 * A(p) + S u + B x + T v + C(divs) >= 0,
12478 * where u and x are input and output dimensions if type == isl_dim_out
12479 * while x and v are input and output dimensions if type == isl_dim_in,
12480 * and ma is represented by
12482 * x = D(p) + F(y) + G(divs')
12484 * then the result is
12486 * A(p) + B D(p) + S u + B F(y) + T v + B G(divs') + C(divs) >= 0
12488 * The divs in the input set are similarly adjusted.
12491 * floor((a_i(p) + s u + b_i x + t v + c_i(divs))/n_i)
12495 * floor((a_i(p) + b_i D(p) + s u + b_i F(y) + t v +
12496 * B_i G(divs') + c_i(divs))/n_i)
12498 * If bmap is not a rational map and if F(y) involves any denominators
12500 * x_i = (f_i y + h_i)/m_i
12502 * then additional constraints are added to ensure that we only
12503 * map back integer points. That is we enforce
12505 * f_i y + h_i = m_i alpha_i
12507 * with alpha_i an additional existentially quantified variable.
12509 * We first copy over the divs from "ma".
12510 * Then we add the modified constraints and divs from "bmap".
12511 * Finally, we add the stride constraints, if needed.
12513 __isl_give isl_basic_map
*isl_basic_map_preimage_multi_aff(
12514 __isl_take isl_basic_map
*bmap
, enum isl_dim_type type
,
12515 __isl_take isl_multi_aff
*ma
)
12519 isl_basic_map
*res
= NULL
;
12520 int n_before
, n_after
, n_div_bmap
, n_div_ma
;
12521 isl_int f
, c1
, c2
, g
;
12530 ma
= isl_multi_aff_align_divs(ma
);
12533 if (check_basic_map_compatible_range_multi_aff(bmap
, type
, ma
) < 0)
12536 if (type
== isl_dim_in
) {
12538 n_after
= isl_basic_map_dim(bmap
, isl_dim_out
);
12540 n_before
= isl_basic_map_dim(bmap
, isl_dim_in
);
12543 n_div_bmap
= isl_basic_map_dim(bmap
, isl_dim_div
);
12544 n_div_ma
= ma
->n
? isl_aff_dim(ma
->p
[0], isl_dim_div
) : 0;
12546 space
= isl_multi_aff_get_domain_space(ma
);
12547 space
= isl_space_set(isl_basic_map_get_space(bmap
), type
, space
);
12548 rational
= isl_basic_map_is_rational(bmap
);
12549 strides
= rational
? 0 : multi_aff_strides(ma
);
12550 res
= isl_basic_map_alloc_space(space
, n_div_ma
+ n_div_bmap
+ strides
,
12551 bmap
->n_eq
+ strides
, bmap
->n_ineq
+ 2 * n_div_ma
);
12553 res
= isl_basic_map_set_rational(res
);
12555 for (i
= 0; i
< n_div_ma
+ n_div_bmap
; ++i
)
12556 if (isl_basic_map_alloc_div(res
) < 0)
12559 if (set_ma_divs(res
, ma
, n_before
, n_after
, n_div_ma
) < 0)
12562 for (i
= 0; i
< bmap
->n_eq
; ++i
) {
12563 k
= isl_basic_map_alloc_equality(res
);
12566 isl_seq_preimage(res
->eq
[k
], bmap
->eq
[i
], ma
, n_before
,
12567 n_after
, n_div_ma
, n_div_bmap
, f
, c1
, c2
, g
, 0);
12570 for (i
= 0; i
< bmap
->n_ineq
; ++i
) {
12571 k
= isl_basic_map_alloc_inequality(res
);
12574 isl_seq_preimage(res
->ineq
[k
], bmap
->ineq
[i
], ma
, n_before
,
12575 n_after
, n_div_ma
, n_div_bmap
, f
, c1
, c2
, g
, 0);
12578 for (i
= 0; i
< bmap
->n_div
; ++i
) {
12579 if (isl_int_is_zero(bmap
->div
[i
][0])) {
12580 isl_int_set_si(res
->div
[n_div_ma
+ i
][0], 0);
12583 isl_seq_preimage(res
->div
[n_div_ma
+ i
], bmap
->div
[i
], ma
,
12584 n_before
, n_after
, n_div_ma
, n_div_bmap
,
12589 res
= add_ma_strides(res
, ma
, n_before
, n_after
, n_div_ma
);
12595 isl_basic_map_free(bmap
);
12596 isl_multi_aff_free(ma
);
12597 res
= isl_basic_map_simplify(res
);
12598 return isl_basic_map_finalize(res
);
12604 isl_basic_map_free(bmap
);
12605 isl_multi_aff_free(ma
);
12606 isl_basic_map_free(res
);
12610 /* Compute the preimage of "bset" under the function represented by "ma".
12611 * In other words, plug in "ma" in "bset". The result is a basic set
12612 * that lives in the domain space of "ma".
12614 __isl_give isl_basic_set
*isl_basic_set_preimage_multi_aff(
12615 __isl_take isl_basic_set
*bset
, __isl_take isl_multi_aff
*ma
)
12617 return isl_basic_map_preimage_multi_aff(bset
, isl_dim_set
, ma
);
12620 /* Compute the preimage of the domain of "bmap" under the function
12621 * represented by "ma".
12622 * In other words, plug in "ma" in the domain of "bmap".
12623 * The result is a basic map that lives in the same space as "bmap"
12624 * except that the domain has been replaced by the domain space of "ma".
12626 __isl_give isl_basic_map
*isl_basic_map_preimage_domain_multi_aff(
12627 __isl_take isl_basic_map
*bmap
, __isl_take isl_multi_aff
*ma
)
12629 return isl_basic_map_preimage_multi_aff(bmap
, isl_dim_in
, ma
);
12632 /* Compute the preimage of the range of "bmap" under the function
12633 * represented by "ma".
12634 * In other words, plug in "ma" in the range of "bmap".
12635 * The result is a basic map that lives in the same space as "bmap"
12636 * except that the range has been replaced by the domain space of "ma".
12638 __isl_give isl_basic_map
*isl_basic_map_preimage_range_multi_aff(
12639 __isl_take isl_basic_map
*bmap
, __isl_take isl_multi_aff
*ma
)
12641 return isl_basic_map_preimage_multi_aff(bmap
, isl_dim_out
, ma
);
12644 /* Check if the range of "ma" is compatible with the domain or range
12645 * (depending on "type") of "map".
12646 * Return isl_stat_error if anything is wrong.
12648 static isl_stat
check_map_compatible_range_multi_aff(
12649 __isl_keep isl_map
*map
, enum isl_dim_type type
,
12650 __isl_keep isl_multi_aff
*ma
)
12653 isl_space
*ma_space
;
12655 ma_space
= isl_multi_aff_get_space(ma
);
12656 m
= isl_space_tuple_is_equal(map
->dim
, type
, ma_space
, isl_dim_out
);
12657 isl_space_free(ma_space
);
12659 return isl_stat_error
;
12661 isl_die(isl_map_get_ctx(map
), isl_error_invalid
,
12662 "spaces don't match", return isl_stat_error
);
12663 return isl_stat_ok
;
12666 /* Compute the preimage of the domain or range (depending on "type")
12667 * of "map" under the function represented by "ma".
12668 * In other words, plug in "ma" in the domain or range of "map".
12669 * The result is a map that lives in the same space as "map"
12670 * except that the domain or range has been replaced by
12671 * the domain space of "ma".
12673 * The parameters are assumed to have been aligned.
12675 static __isl_give isl_map
*map_preimage_multi_aff(__isl_take isl_map
*map
,
12676 enum isl_dim_type type
, __isl_take isl_multi_aff
*ma
)
12681 map
= isl_map_cow(map
);
12682 ma
= isl_multi_aff_align_divs(ma
);
12685 if (check_map_compatible_range_multi_aff(map
, type
, ma
) < 0)
12688 for (i
= 0; i
< map
->n
; ++i
) {
12689 map
->p
[i
] = isl_basic_map_preimage_multi_aff(map
->p
[i
], type
,
12690 isl_multi_aff_copy(ma
));
12695 space
= isl_multi_aff_get_domain_space(ma
);
12696 space
= isl_space_set(isl_map_get_space(map
), type
, space
);
12698 isl_space_free(map
->dim
);
12703 isl_multi_aff_free(ma
);
12705 ISL_F_CLR(map
, ISL_MAP_DISJOINT
);
12706 ISL_F_CLR(map
, ISL_SET_NORMALIZED
);
12709 isl_multi_aff_free(ma
);
12714 /* Compute the preimage of the domain or range (depending on "type")
12715 * of "map" under the function represented by "ma".
12716 * In other words, plug in "ma" in the domain or range of "map".
12717 * The result is a map that lives in the same space as "map"
12718 * except that the domain or range has been replaced by
12719 * the domain space of "ma".
12721 __isl_give isl_map
*isl_map_preimage_multi_aff(__isl_take isl_map
*map
,
12722 enum isl_dim_type type
, __isl_take isl_multi_aff
*ma
)
12729 aligned
= isl_map_space_has_equal_params(map
, ma
->space
);
12733 return map_preimage_multi_aff(map
, type
, ma
);
12735 if (isl_map_check_named_params(map
) < 0)
12737 if (!isl_space_has_named_params(ma
->space
))
12738 isl_die(map
->ctx
, isl_error_invalid
,
12739 "unaligned unnamed parameters", goto error
);
12740 map
= isl_map_align_params(map
, isl_multi_aff_get_space(ma
));
12741 ma
= isl_multi_aff_align_params(ma
, isl_map_get_space(map
));
12743 return map_preimage_multi_aff(map
, type
, ma
);
12745 isl_multi_aff_free(ma
);
12746 return isl_map_free(map
);
12749 /* Compute the preimage of "set" under the function represented by "ma".
12750 * In other words, plug in "ma" in "set". The result is a set
12751 * that lives in the domain space of "ma".
12753 __isl_give isl_set
*isl_set_preimage_multi_aff(__isl_take isl_set
*set
,
12754 __isl_take isl_multi_aff
*ma
)
12756 return isl_map_preimage_multi_aff(set
, isl_dim_set
, ma
);
12759 /* Compute the preimage of the domain of "map" under the function
12760 * represented by "ma".
12761 * In other words, plug in "ma" in the domain of "map".
12762 * The result is a map that lives in the same space as "map"
12763 * except that the domain has been replaced by the domain space of "ma".
12765 __isl_give isl_map
*isl_map_preimage_domain_multi_aff(__isl_take isl_map
*map
,
12766 __isl_take isl_multi_aff
*ma
)
12768 return isl_map_preimage_multi_aff(map
, isl_dim_in
, ma
);
12771 /* Compute the preimage of the range of "map" under the function
12772 * represented by "ma".
12773 * In other words, plug in "ma" in the range of "map".
12774 * The result is a map that lives in the same space as "map"
12775 * except that the range has been replaced by the domain space of "ma".
12777 __isl_give isl_map
*isl_map_preimage_range_multi_aff(__isl_take isl_map
*map
,
12778 __isl_take isl_multi_aff
*ma
)
12780 return isl_map_preimage_multi_aff(map
, isl_dim_out
, ma
);
12783 /* Compute the preimage of "map" under the function represented by "pma".
12784 * In other words, plug in "pma" in the domain or range of "map".
12785 * The result is a map that lives in the same space as "map",
12786 * except that the space of type "type" has been replaced by
12787 * the domain space of "pma".
12789 * The parameters of "map" and "pma" are assumed to have been aligned.
12791 static __isl_give isl_map
*isl_map_preimage_pw_multi_aff_aligned(
12792 __isl_take isl_map
*map
, enum isl_dim_type type
,
12793 __isl_take isl_pw_multi_aff
*pma
)
12802 isl_pw_multi_aff_free(pma
);
12803 res
= isl_map_empty(isl_map_get_space(map
));
12808 res
= isl_map_preimage_multi_aff(isl_map_copy(map
), type
,
12809 isl_multi_aff_copy(pma
->p
[0].maff
));
12810 if (type
== isl_dim_in
)
12811 res
= isl_map_intersect_domain(res
,
12812 isl_map_copy(pma
->p
[0].set
));
12814 res
= isl_map_intersect_range(res
,
12815 isl_map_copy(pma
->p
[0].set
));
12817 for (i
= 1; i
< pma
->n
; ++i
) {
12820 res_i
= isl_map_preimage_multi_aff(isl_map_copy(map
), type
,
12821 isl_multi_aff_copy(pma
->p
[i
].maff
));
12822 if (type
== isl_dim_in
)
12823 res_i
= isl_map_intersect_domain(res_i
,
12824 isl_map_copy(pma
->p
[i
].set
));
12826 res_i
= isl_map_intersect_range(res_i
,
12827 isl_map_copy(pma
->p
[i
].set
));
12828 res
= isl_map_union(res
, res_i
);
12831 isl_pw_multi_aff_free(pma
);
12835 isl_pw_multi_aff_free(pma
);
12840 /* Compute the preimage of "map" under the function represented by "pma".
12841 * In other words, plug in "pma" in the domain or range of "map".
12842 * The result is a map that lives in the same space as "map",
12843 * except that the space of type "type" has been replaced by
12844 * the domain space of "pma".
12846 __isl_give isl_map
*isl_map_preimage_pw_multi_aff(__isl_take isl_map
*map
,
12847 enum isl_dim_type type
, __isl_take isl_pw_multi_aff
*pma
)
12854 aligned
= isl_map_space_has_equal_params(map
, pma
->dim
);
12858 return isl_map_preimage_pw_multi_aff_aligned(map
, type
, pma
);
12860 if (isl_map_check_named_params(map
) < 0)
12862 if (!isl_space_has_named_params(pma
->dim
))
12863 isl_die(map
->ctx
, isl_error_invalid
,
12864 "unaligned unnamed parameters", goto error
);
12865 map
= isl_map_align_params(map
, isl_pw_multi_aff_get_space(pma
));
12866 pma
= isl_pw_multi_aff_align_params(pma
, isl_map_get_space(map
));
12868 return isl_map_preimage_pw_multi_aff_aligned(map
, type
, pma
);
12870 isl_pw_multi_aff_free(pma
);
12871 return isl_map_free(map
);
12874 /* Compute the preimage of "set" under the function represented by "pma".
12875 * In other words, plug in "pma" in "set". The result is a set
12876 * that lives in the domain space of "pma".
12878 __isl_give isl_set
*isl_set_preimage_pw_multi_aff(__isl_take isl_set
*set
,
12879 __isl_take isl_pw_multi_aff
*pma
)
12881 return isl_map_preimage_pw_multi_aff(set
, isl_dim_set
, pma
);
12884 /* Compute the preimage of the domain of "map" under the function
12885 * represented by "pma".
12886 * In other words, plug in "pma" in the domain of "map".
12887 * The result is a map that lives in the same space as "map",
12888 * except that domain space has been replaced by the domain space of "pma".
12890 __isl_give isl_map
*isl_map_preimage_domain_pw_multi_aff(
12891 __isl_take isl_map
*map
, __isl_take isl_pw_multi_aff
*pma
)
12893 return isl_map_preimage_pw_multi_aff(map
, isl_dim_in
, pma
);
12896 /* Compute the preimage of the range of "map" under the function
12897 * represented by "pma".
12898 * In other words, plug in "pma" in the range of "map".
12899 * The result is a map that lives in the same space as "map",
12900 * except that range space has been replaced by the domain space of "pma".
12902 __isl_give isl_map
*isl_map_preimage_range_pw_multi_aff(
12903 __isl_take isl_map
*map
, __isl_take isl_pw_multi_aff
*pma
)
12905 return isl_map_preimage_pw_multi_aff(map
, isl_dim_out
, pma
);
12908 /* Compute the preimage of "map" under the function represented by "mpa".
12909 * In other words, plug in "mpa" in the domain or range of "map".
12910 * The result is a map that lives in the same space as "map",
12911 * except that the space of type "type" has been replaced by
12912 * the domain space of "mpa".
12914 * If the map does not involve any constraints that refer to the
12915 * dimensions of the substituted space, then the only possible
12916 * effect of "mpa" on the map is to map the space to a different space.
12917 * We create a separate isl_multi_aff to effectuate this change
12918 * in order to avoid spurious splitting of the map along the pieces
12921 __isl_give isl_map
*isl_map_preimage_multi_pw_aff(__isl_take isl_map
*map
,
12922 enum isl_dim_type type
, __isl_take isl_multi_pw_aff
*mpa
)
12925 isl_pw_multi_aff
*pma
;
12930 n
= isl_map_dim(map
, type
);
12931 if (!isl_map_involves_dims(map
, type
, 0, n
)) {
12935 space
= isl_multi_pw_aff_get_space(mpa
);
12936 isl_multi_pw_aff_free(mpa
);
12937 ma
= isl_multi_aff_zero(space
);
12938 return isl_map_preimage_multi_aff(map
, type
, ma
);
12941 pma
= isl_pw_multi_aff_from_multi_pw_aff(mpa
);
12942 return isl_map_preimage_pw_multi_aff(map
, type
, pma
);
12945 isl_multi_pw_aff_free(mpa
);
12949 /* Compute the preimage of "map" under the function represented by "mpa".
12950 * In other words, plug in "mpa" in the domain "map".
12951 * The result is a map that lives in the same space as "map",
12952 * except that domain space has been replaced by the domain space of "mpa".
12954 __isl_give isl_map
*isl_map_preimage_domain_multi_pw_aff(
12955 __isl_take isl_map
*map
, __isl_take isl_multi_pw_aff
*mpa
)
12957 return isl_map_preimage_multi_pw_aff(map
, isl_dim_in
, mpa
);
12960 /* Compute the preimage of "set" by the function represented by "mpa".
12961 * In other words, plug in "mpa" in "set".
12963 __isl_give isl_set
*isl_set_preimage_multi_pw_aff(__isl_take isl_set
*set
,
12964 __isl_take isl_multi_pw_aff
*mpa
)
12966 return isl_map_preimage_multi_pw_aff(set
, isl_dim_set
, mpa
);
12969 /* Are the "n" "coefficients" starting at "first" of the integer division
12970 * expressions at position "pos1" in "bmap1" and "pos2" in "bmap2" equal
12972 * The "coefficient" at position 0 is the denominator.
12973 * The "coefficient" at position 1 is the constant term.
12975 isl_bool
isl_basic_map_equal_div_expr_part(__isl_keep isl_basic_map
*bmap1
,
12976 int pos1
, __isl_keep isl_basic_map
*bmap2
, int pos2
,
12977 unsigned first
, unsigned n
)
12979 if (isl_basic_map_check_range(bmap1
, isl_dim_div
, pos1
, 1) < 0)
12980 return isl_bool_error
;
12981 if (isl_basic_map_check_range(bmap2
, isl_dim_div
, pos2
, 1) < 0)
12982 return isl_bool_error
;
12983 return isl_seq_eq(bmap1
->div
[pos1
] + first
,
12984 bmap2
->div
[pos2
] + first
, n
);
12987 /* Are the integer division expressions at position "pos1" in "bmap1" and
12988 * "pos2" in "bmap2" equal to each other, except that the constant terms
12991 isl_bool
isl_basic_map_equal_div_expr_except_constant(
12992 __isl_keep isl_basic_map
*bmap1
, int pos1
,
12993 __isl_keep isl_basic_map
*bmap2
, int pos2
)
12998 if (!bmap1
|| !bmap2
)
12999 return isl_bool_error
;
13000 total
= isl_basic_map_total_dim(bmap1
);
13001 if (total
!= isl_basic_map_total_dim(bmap2
))
13002 isl_die(isl_basic_map_get_ctx(bmap1
), isl_error_invalid
,
13003 "incomparable div expressions", return isl_bool_error
);
13004 equal
= isl_basic_map_equal_div_expr_part(bmap1
, pos1
, bmap2
, pos2
,
13006 if (equal
< 0 || !equal
)
13008 equal
= isl_basic_map_equal_div_expr_part(bmap1
, pos1
, bmap2
, pos2
,
13010 if (equal
< 0 || equal
)
13011 return isl_bool_not(equal
);
13012 return isl_basic_map_equal_div_expr_part(bmap1
, pos1
, bmap2
, pos2
,
13016 /* Replace the numerator of the constant term of the integer division
13017 * expression at position "div" in "bmap" by "value".
13018 * The caller guarantees that this does not change the meaning
13021 __isl_give isl_basic_map
*isl_basic_map_set_div_expr_constant_num_si_inplace(
13022 __isl_take isl_basic_map
*bmap
, int div
, int value
)
13024 if (isl_basic_map_check_range(bmap
, isl_dim_div
, div
, 1) < 0)
13025 return isl_basic_map_free(bmap
);
13027 isl_int_set_si(bmap
->div
[div
][1], value
);
13032 /* Is the point "inner" internal to inequality constraint "ineq"
13034 * The point is considered to be internal to the inequality constraint,
13035 * if it strictly lies on the positive side of the inequality constraint,
13036 * or if it lies on the constraint and the constraint is lexico-positive.
13038 static isl_bool
is_internal(__isl_keep isl_vec
*inner
,
13039 __isl_keep isl_basic_set
*bset
, int ineq
)
13045 if (!inner
|| !bset
)
13046 return isl_bool_error
;
13048 ctx
= isl_basic_set_get_ctx(bset
);
13049 isl_seq_inner_product(inner
->el
, bset
->ineq
[ineq
], inner
->size
,
13050 &ctx
->normalize_gcd
);
13051 if (!isl_int_is_zero(ctx
->normalize_gcd
))
13052 return isl_int_is_nonneg(ctx
->normalize_gcd
);
13054 total
= isl_basic_set_dim(bset
, isl_dim_all
);
13055 pos
= isl_seq_first_non_zero(bset
->ineq
[ineq
] + 1, total
);
13056 return isl_int_is_pos(bset
->ineq
[ineq
][1 + pos
]);
13059 /* Tighten the inequality constraints of "bset" that are outward with respect
13060 * to the point "vec".
13061 * That is, tighten the constraints that are not satisfied by "vec".
13063 * "vec" is a point internal to some superset S of "bset" that is used
13064 * to make the subsets of S disjoint, by tightening one half of the constraints
13065 * that separate two subsets. In particular, the constraints of S
13066 * are all satisfied by "vec" and should not be tightened.
13067 * Of the internal constraints, those that have "vec" on the outside
13068 * are tightened. The shared facet is included in the adjacent subset
13069 * with the opposite constraint.
13070 * For constraints that saturate "vec", this criterion cannot be used
13071 * to determine which of the two sides should be tightened.
13072 * Instead, the sign of the first non-zero coefficient is used
13073 * to make this choice. Note that this second criterion is never used
13074 * on the constraints of S since "vec" is interior to "S".
13076 __isl_give isl_basic_set
*isl_basic_set_tighten_outward(
13077 __isl_take isl_basic_set
*bset
, __isl_keep isl_vec
*vec
)
13081 bset
= isl_basic_set_cow(bset
);
13084 for (j
= 0; j
< bset
->n_ineq
; ++j
) {
13087 internal
= is_internal(vec
, bset
, j
);
13089 return isl_basic_set_free(bset
);
13092 isl_int_sub_ui(bset
->ineq
[j
][0], bset
->ineq
[j
][0], 1);
13098 /* Replace the variables x of type "type" starting at "first" in "bmap"
13099 * by x' with x = M x' with M the matrix trans.
13100 * That is, replace the corresponding coefficients c by c M.
13102 * The transformation matrix should be a square matrix.
13104 __isl_give isl_basic_map
*isl_basic_map_transform_dims(
13105 __isl_take isl_basic_map
*bmap
, enum isl_dim_type type
, unsigned first
,
13106 __isl_take isl_mat
*trans
)
13110 bmap
= isl_basic_map_cow(bmap
);
13111 if (!bmap
|| !trans
)
13114 if (trans
->n_row
!= trans
->n_col
)
13115 isl_die(trans
->ctx
, isl_error_invalid
,
13116 "expecting square transformation matrix", goto error
);
13117 if (first
+ trans
->n_row
> isl_basic_map_dim(bmap
, type
))
13118 isl_die(trans
->ctx
, isl_error_invalid
,
13119 "oversized transformation matrix", goto error
);
13121 pos
= isl_basic_map_offset(bmap
, type
) + first
;
13123 if (isl_mat_sub_transform(bmap
->eq
, bmap
->n_eq
, pos
,
13124 isl_mat_copy(trans
)) < 0)
13126 if (isl_mat_sub_transform(bmap
->ineq
, bmap
->n_ineq
, pos
,
13127 isl_mat_copy(trans
)) < 0)
13129 if (isl_mat_sub_transform(bmap
->div
, bmap
->n_div
, 1 + pos
,
13130 isl_mat_copy(trans
)) < 0)
13133 ISL_F_CLR(bmap
, ISL_BASIC_MAP_NORMALIZED
);
13134 ISL_F_CLR(bmap
, ISL_BASIC_MAP_NORMALIZED_DIVS
);
13136 isl_mat_free(trans
);
13139 isl_mat_free(trans
);
13140 isl_basic_map_free(bmap
);
13144 /* Replace the variables x of type "type" starting at "first" in "bset"
13145 * by x' with x = M x' with M the matrix trans.
13146 * That is, replace the corresponding coefficients c by c M.
13148 * The transformation matrix should be a square matrix.
13150 __isl_give isl_basic_set
*isl_basic_set_transform_dims(
13151 __isl_take isl_basic_set
*bset
, enum isl_dim_type type
, unsigned first
,
13152 __isl_take isl_mat
*trans
)
13154 return isl_basic_map_transform_dims(bset
, type
, first
, trans
);