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(__isl_keep isl_basic_set
*bset
,
129 enum isl_dim_type type
)
131 return isl_basic_map_offset(bset
, type
);
134 static unsigned map_offset(__isl_keep 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(__isl_keep
const 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(__isl_keep
const 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 /* Check whether equality i of bset is a pure stride constraint
851 * on a single dimension, i.e., of the form
855 * with k a constant and e an existentially quantified variable.
857 isl_bool
isl_basic_set_eq_is_stride(__isl_keep isl_basic_set
*bset
, int i
)
866 return isl_bool_error
;
868 if (!isl_int_is_zero(bset
->eq
[i
][0]))
869 return isl_bool_false
;
871 nparam
= isl_basic_set_dim(bset
, isl_dim_param
);
872 d
= isl_basic_set_dim(bset
, isl_dim_set
);
873 n_div
= isl_basic_set_dim(bset
, isl_dim_div
);
875 if (isl_seq_first_non_zero(bset
->eq
[i
] + 1, nparam
) != -1)
876 return isl_bool_false
;
877 pos1
= isl_seq_first_non_zero(bset
->eq
[i
] + 1 + nparam
, d
);
879 return isl_bool_false
;
880 if (isl_seq_first_non_zero(bset
->eq
[i
] + 1 + nparam
+ pos1
+ 1,
882 return isl_bool_false
;
884 pos2
= isl_seq_first_non_zero(bset
->eq
[i
] + 1 + nparam
+ d
, n_div
);
886 return isl_bool_false
;
887 if (isl_seq_first_non_zero(bset
->eq
[i
] + 1 + nparam
+ d
+ pos2
+ 1,
888 n_div
- pos2
- 1) != -1)
889 return isl_bool_false
;
890 if (!isl_int_is_one(bset
->eq
[i
][1 + nparam
+ pos1
]) &&
891 !isl_int_is_negone(bset
->eq
[i
][1 + nparam
+ pos1
]))
892 return isl_bool_false
;
894 return isl_bool_true
;
897 /* Reset the user pointer on all identifiers of parameters and tuples
898 * of the space of "map".
900 __isl_give isl_map
*isl_map_reset_user(__isl_take isl_map
*map
)
904 space
= isl_map_get_space(map
);
905 space
= isl_space_reset_user(space
);
906 map
= isl_map_reset_space(map
, space
);
911 /* Reset the user pointer on all identifiers of parameters and tuples
912 * of the space of "set".
914 __isl_give isl_set
*isl_set_reset_user(__isl_take isl_set
*set
)
916 return isl_map_reset_user(set
);
919 isl_bool
isl_basic_map_is_rational(__isl_keep isl_basic_map
*bmap
)
922 return isl_bool_error
;
923 return ISL_F_ISSET(bmap
, ISL_BASIC_MAP_RATIONAL
);
926 /* Has "map" been marked as a rational map?
927 * In particular, have all basic maps in "map" been marked this way?
928 * An empty map is not considered to be rational.
929 * Maps where only some of the basic maps are marked rational
932 isl_bool
isl_map_is_rational(__isl_keep isl_map
*map
)
938 return isl_bool_error
;
940 return isl_bool_false
;
941 rational
= isl_basic_map_is_rational(map
->p
[0]);
944 for (i
= 1; i
< map
->n
; ++i
) {
947 rational_i
= isl_basic_map_is_rational(map
->p
[i
]);
950 if (rational
!= rational_i
)
951 isl_die(isl_map_get_ctx(map
), isl_error_unsupported
,
952 "mixed rational and integer basic maps "
953 "not supported", return isl_bool_error
);
959 /* Has "set" been marked as a rational set?
960 * In particular, have all basic set in "set" been marked this way?
961 * An empty set is not considered to be rational.
962 * Sets where only some of the basic sets are marked rational
965 isl_bool
isl_set_is_rational(__isl_keep isl_set
*set
)
967 return isl_map_is_rational(set
);
970 int isl_basic_set_is_rational(__isl_keep isl_basic_set
*bset
)
972 return isl_basic_map_is_rational(bset
);
975 /* Does "bmap" contain any rational points?
977 * If "bmap" has an equality for each dimension, equating the dimension
978 * to an integer constant, then it has no rational points, even if it
979 * is marked as rational.
981 isl_bool
isl_basic_map_has_rational(__isl_keep isl_basic_map
*bmap
)
983 isl_bool has_rational
= isl_bool_true
;
987 return isl_bool_error
;
988 if (isl_basic_map_plain_is_empty(bmap
))
989 return isl_bool_false
;
990 if (!isl_basic_map_is_rational(bmap
))
991 return isl_bool_false
;
992 bmap
= isl_basic_map_copy(bmap
);
993 bmap
= isl_basic_map_implicit_equalities(bmap
);
995 return isl_bool_error
;
996 total
= isl_basic_map_total_dim(bmap
);
997 if (bmap
->n_eq
== total
) {
999 for (i
= 0; i
< bmap
->n_eq
; ++i
) {
1000 j
= isl_seq_first_non_zero(bmap
->eq
[i
] + 1, total
);
1003 if (!isl_int_is_one(bmap
->eq
[i
][1 + j
]) &&
1004 !isl_int_is_negone(bmap
->eq
[i
][1 + j
]))
1006 j
= isl_seq_first_non_zero(bmap
->eq
[i
] + 1 + j
+ 1,
1011 if (i
== bmap
->n_eq
)
1012 has_rational
= isl_bool_false
;
1014 isl_basic_map_free(bmap
);
1016 return has_rational
;
1019 /* Does "map" contain any rational points?
1021 isl_bool
isl_map_has_rational(__isl_keep isl_map
*map
)
1024 isl_bool has_rational
;
1027 return isl_bool_error
;
1028 for (i
= 0; i
< map
->n
; ++i
) {
1029 has_rational
= isl_basic_map_has_rational(map
->p
[i
]);
1030 if (has_rational
< 0 || has_rational
)
1031 return has_rational
;
1033 return isl_bool_false
;
1036 /* Does "set" contain any rational points?
1038 isl_bool
isl_set_has_rational(__isl_keep isl_set
*set
)
1040 return isl_map_has_rational(set
);
1043 /* Is this basic set a parameter domain?
1045 isl_bool
isl_basic_set_is_params(__isl_keep isl_basic_set
*bset
)
1048 return isl_bool_error
;
1049 return isl_space_is_params(bset
->dim
);
1052 /* Is this set a parameter domain?
1054 isl_bool
isl_set_is_params(__isl_keep isl_set
*set
)
1057 return isl_bool_error
;
1058 return isl_space_is_params(set
->dim
);
1061 /* Is this map actually a parameter domain?
1062 * Users should never call this function. Outside of isl,
1063 * a map can never be a parameter domain.
1065 isl_bool
isl_map_is_params(__isl_keep isl_map
*map
)
1068 return isl_bool_error
;
1069 return isl_space_is_params(map
->dim
);
1072 static struct isl_basic_map
*basic_map_init(struct isl_ctx
*ctx
,
1073 struct isl_basic_map
*bmap
, unsigned extra
,
1074 unsigned n_eq
, unsigned n_ineq
)
1077 size_t row_size
= 1 + isl_space_dim(bmap
->dim
, isl_dim_all
) + extra
;
1082 bmap
->block
= isl_blk_alloc(ctx
, (n_ineq
+ n_eq
) * row_size
);
1083 if (isl_blk_is_error(bmap
->block
))
1086 bmap
->ineq
= isl_alloc_array(ctx
, isl_int
*, n_ineq
+ n_eq
);
1087 if ((n_ineq
+ n_eq
) && !bmap
->ineq
)
1091 bmap
->block2
= isl_blk_empty();
1094 bmap
->block2
= isl_blk_alloc(ctx
, extra
* (1 + row_size
));
1095 if (isl_blk_is_error(bmap
->block2
))
1098 bmap
->div
= isl_alloc_array(ctx
, isl_int
*, extra
);
1103 for (i
= 0; i
< n_ineq
+ n_eq
; ++i
)
1104 bmap
->ineq
[i
] = bmap
->block
.data
+ i
* row_size
;
1106 for (i
= 0; i
< extra
; ++i
)
1107 bmap
->div
[i
] = bmap
->block2
.data
+ i
* (1 + row_size
);
1111 bmap
->c_size
= n_eq
+ n_ineq
;
1112 bmap
->eq
= bmap
->ineq
+ n_ineq
;
1113 bmap
->extra
= extra
;
1117 bmap
->sample
= NULL
;
1121 isl_basic_map_free(bmap
);
1125 struct isl_basic_set
*isl_basic_set_alloc(struct isl_ctx
*ctx
,
1126 unsigned nparam
, unsigned dim
, unsigned extra
,
1127 unsigned n_eq
, unsigned n_ineq
)
1129 struct isl_basic_map
*bmap
;
1132 space
= isl_space_set_alloc(ctx
, nparam
, dim
);
1136 bmap
= isl_basic_map_alloc_space(space
, extra
, n_eq
, n_ineq
);
1137 return bset_from_bmap(bmap
);
1140 struct isl_basic_set
*isl_basic_set_alloc_space(__isl_take isl_space
*dim
,
1141 unsigned extra
, unsigned n_eq
, unsigned n_ineq
)
1143 struct isl_basic_map
*bmap
;
1146 isl_assert(dim
->ctx
, dim
->n_in
== 0, goto error
);
1147 bmap
= isl_basic_map_alloc_space(dim
, extra
, n_eq
, n_ineq
);
1148 return bset_from_bmap(bmap
);
1150 isl_space_free(dim
);
1154 struct isl_basic_map
*isl_basic_map_alloc_space(__isl_take isl_space
*dim
,
1155 unsigned extra
, unsigned n_eq
, unsigned n_ineq
)
1157 struct isl_basic_map
*bmap
;
1161 bmap
= isl_calloc_type(dim
->ctx
, struct isl_basic_map
);
1166 return basic_map_init(dim
->ctx
, bmap
, extra
, n_eq
, n_ineq
);
1168 isl_space_free(dim
);
1172 struct isl_basic_map
*isl_basic_map_alloc(struct isl_ctx
*ctx
,
1173 unsigned nparam
, unsigned in
, unsigned out
, unsigned extra
,
1174 unsigned n_eq
, unsigned n_ineq
)
1176 struct isl_basic_map
*bmap
;
1179 dim
= isl_space_alloc(ctx
, nparam
, in
, out
);
1183 bmap
= isl_basic_map_alloc_space(dim
, extra
, n_eq
, n_ineq
);
1187 static void dup_constraints(
1188 struct isl_basic_map
*dst
, struct isl_basic_map
*src
)
1191 unsigned total
= isl_basic_map_total_dim(src
);
1193 for (i
= 0; i
< src
->n_eq
; ++i
) {
1194 int j
= isl_basic_map_alloc_equality(dst
);
1195 isl_seq_cpy(dst
->eq
[j
], src
->eq
[i
], 1+total
);
1198 for (i
= 0; i
< src
->n_ineq
; ++i
) {
1199 int j
= isl_basic_map_alloc_inequality(dst
);
1200 isl_seq_cpy(dst
->ineq
[j
], src
->ineq
[i
], 1+total
);
1203 for (i
= 0; i
< src
->n_div
; ++i
) {
1204 int j
= isl_basic_map_alloc_div(dst
);
1205 isl_seq_cpy(dst
->div
[j
], src
->div
[i
], 1+1+total
);
1207 ISL_F_SET(dst
, ISL_BASIC_SET_FINAL
);
1210 __isl_give isl_basic_map
*isl_basic_map_dup(__isl_keep isl_basic_map
*bmap
)
1212 struct isl_basic_map
*dup
;
1216 dup
= isl_basic_map_alloc_space(isl_space_copy(bmap
->dim
),
1217 bmap
->n_div
, bmap
->n_eq
, bmap
->n_ineq
);
1220 dup_constraints(dup
, bmap
);
1221 dup
->flags
= bmap
->flags
;
1222 dup
->sample
= isl_vec_copy(bmap
->sample
);
1226 struct isl_basic_set
*isl_basic_set_dup(struct isl_basic_set
*bset
)
1228 struct isl_basic_map
*dup
;
1230 dup
= isl_basic_map_dup(bset_to_bmap(bset
));
1231 return bset_from_bmap(dup
);
1234 __isl_give isl_basic_set
*isl_basic_set_copy(__isl_keep isl_basic_set
*bset
)
1239 if (ISL_F_ISSET(bset
, ISL_BASIC_SET_FINAL
)) {
1243 return isl_basic_set_dup(bset
);
1246 struct isl_set
*isl_set_copy(struct isl_set
*set
)
1255 struct isl_basic_map
*isl_basic_map_copy(struct isl_basic_map
*bmap
)
1260 if (ISL_F_ISSET(bmap
, ISL_BASIC_SET_FINAL
)) {
1264 bmap
= isl_basic_map_dup(bmap
);
1266 ISL_F_SET(bmap
, ISL_BASIC_SET_FINAL
);
1270 struct isl_map
*isl_map_copy(struct isl_map
*map
)
1279 __isl_null isl_basic_map
*isl_basic_map_free(__isl_take isl_basic_map
*bmap
)
1284 if (--bmap
->ref
> 0)
1287 isl_ctx_deref(bmap
->ctx
);
1289 isl_blk_free(bmap
->ctx
, bmap
->block2
);
1291 isl_blk_free(bmap
->ctx
, bmap
->block
);
1292 isl_vec_free(bmap
->sample
);
1293 isl_space_free(bmap
->dim
);
1299 __isl_null isl_basic_set
*isl_basic_set_free(__isl_take isl_basic_set
*bset
)
1301 return isl_basic_map_free(bset_to_bmap(bset
));
1304 static int room_for_con(struct isl_basic_map
*bmap
, unsigned n
)
1306 return bmap
->n_eq
+ bmap
->n_ineq
+ n
<= bmap
->c_size
;
1309 /* Check that "map" has only named parameters, reporting an error
1312 isl_stat
isl_map_check_named_params(__isl_keep isl_map
*map
)
1314 return isl_space_check_named_params(isl_map_peek_space(map
));
1317 /* Check that "bmap1" and "bmap2" have the same parameters,
1318 * reporting an error if they do not.
1320 static isl_stat
isl_basic_map_check_equal_params(
1321 __isl_keep isl_basic_map
*bmap1
, __isl_keep isl_basic_map
*bmap2
)
1325 match
= isl_basic_map_has_equal_params(bmap1
, bmap2
);
1327 return isl_stat_error
;
1329 isl_die(isl_basic_map_get_ctx(bmap1
), isl_error_invalid
,
1330 "parameters don't match", return isl_stat_error
);
1334 __isl_give isl_map
*isl_map_align_params_map_map_and(
1335 __isl_take isl_map
*map1
, __isl_take isl_map
*map2
,
1336 __isl_give isl_map
*(*fn
)(__isl_take isl_map
*map1
,
1337 __isl_take isl_map
*map2
))
1341 if (isl_map_has_equal_params(map1
, map2
))
1342 return fn(map1
, map2
);
1343 if (isl_map_check_named_params(map1
) < 0)
1345 if (isl_map_check_named_params(map2
) < 0)
1347 map1
= isl_map_align_params(map1
, isl_map_get_space(map2
));
1348 map2
= isl_map_align_params(map2
, isl_map_get_space(map1
));
1349 return fn(map1
, map2
);
1356 isl_bool
isl_map_align_params_map_map_and_test(__isl_keep isl_map
*map1
,
1357 __isl_keep isl_map
*map2
,
1358 isl_bool (*fn
)(__isl_keep isl_map
*map1
, __isl_keep isl_map
*map2
))
1363 return isl_bool_error
;
1364 if (isl_map_has_equal_params(map1
, map2
))
1365 return fn(map1
, map2
);
1366 if (isl_map_check_named_params(map1
) < 0)
1367 return isl_bool_error
;
1368 if (isl_map_check_named_params(map2
) < 0)
1369 return isl_bool_error
;
1370 map1
= isl_map_copy(map1
);
1371 map2
= isl_map_copy(map2
);
1372 map1
= isl_map_align_params(map1
, isl_map_get_space(map2
));
1373 map2
= isl_map_align_params(map2
, isl_map_get_space(map1
));
1380 int isl_basic_map_alloc_equality(struct isl_basic_map
*bmap
)
1382 struct isl_ctx
*ctx
;
1386 isl_assert(ctx
, room_for_con(bmap
, 1), return -1);
1387 isl_assert(ctx
, (bmap
->eq
- bmap
->ineq
) + bmap
->n_eq
<= bmap
->c_size
,
1389 ISL_F_CLR(bmap
, ISL_BASIC_MAP_NORMALIZED
);
1390 ISL_F_CLR(bmap
, ISL_BASIC_MAP_NO_REDUNDANT
);
1391 ISL_F_CLR(bmap
, ISL_BASIC_MAP_NO_IMPLICIT
);
1392 ISL_F_CLR(bmap
, ISL_BASIC_MAP_ALL_EQUALITIES
);
1393 ISL_F_CLR(bmap
, ISL_BASIC_MAP_NORMALIZED_DIVS
);
1394 if ((bmap
->eq
- bmap
->ineq
) + bmap
->n_eq
== bmap
->c_size
) {
1396 int j
= isl_basic_map_alloc_inequality(bmap
);
1400 bmap
->ineq
[j
] = bmap
->ineq
[bmap
->n_ineq
- 1];
1401 bmap
->ineq
[bmap
->n_ineq
- 1] = bmap
->eq
[-1];
1408 isl_seq_clr(bmap
->eq
[bmap
->n_eq
] + 1 + isl_basic_map_total_dim(bmap
),
1409 bmap
->extra
- bmap
->n_div
);
1410 return bmap
->n_eq
++;
1413 int isl_basic_set_alloc_equality(struct isl_basic_set
*bset
)
1415 return isl_basic_map_alloc_equality(bset_to_bmap(bset
));
1418 int isl_basic_map_free_equality(struct isl_basic_map
*bmap
, unsigned n
)
1422 isl_assert(bmap
->ctx
, n
<= bmap
->n_eq
, return -1);
1427 int isl_basic_set_free_equality(struct isl_basic_set
*bset
, unsigned n
)
1429 return isl_basic_map_free_equality(bset_to_bmap(bset
), n
);
1432 int isl_basic_map_drop_equality(struct isl_basic_map
*bmap
, unsigned pos
)
1437 isl_assert(bmap
->ctx
, pos
< bmap
->n_eq
, return -1);
1439 if (pos
!= bmap
->n_eq
- 1) {
1441 bmap
->eq
[pos
] = bmap
->eq
[bmap
->n_eq
- 1];
1442 bmap
->eq
[bmap
->n_eq
- 1] = t
;
1448 /* Turn inequality "pos" of "bmap" into an equality.
1450 * In particular, we move the inequality in front of the equalities
1451 * and move the last inequality in the position of the moved inequality.
1452 * Note that isl_tab_make_equalities_explicit depends on this particular
1453 * change in the ordering of the constraints.
1455 void isl_basic_map_inequality_to_equality(
1456 struct isl_basic_map
*bmap
, unsigned pos
)
1460 t
= bmap
->ineq
[pos
];
1461 bmap
->ineq
[pos
] = bmap
->ineq
[bmap
->n_ineq
- 1];
1462 bmap
->ineq
[bmap
->n_ineq
- 1] = bmap
->eq
[-1];
1467 ISL_F_CLR(bmap
, ISL_BASIC_MAP_NO_REDUNDANT
);
1468 ISL_F_CLR(bmap
, ISL_BASIC_MAP_NORMALIZED
);
1469 ISL_F_CLR(bmap
, ISL_BASIC_MAP_NORMALIZED_DIVS
);
1470 ISL_F_CLR(bmap
, ISL_BASIC_MAP_ALL_EQUALITIES
);
1473 static int room_for_ineq(struct isl_basic_map
*bmap
, unsigned n
)
1475 return bmap
->n_ineq
+ n
<= bmap
->eq
- bmap
->ineq
;
1478 int isl_basic_map_alloc_inequality(struct isl_basic_map
*bmap
)
1480 struct isl_ctx
*ctx
;
1484 isl_assert(ctx
, room_for_ineq(bmap
, 1), return -1);
1485 ISL_F_CLR(bmap
, ISL_BASIC_MAP_NO_IMPLICIT
);
1486 ISL_F_CLR(bmap
, ISL_BASIC_MAP_NO_REDUNDANT
);
1487 ISL_F_CLR(bmap
, ISL_BASIC_MAP_NORMALIZED
);
1488 ISL_F_CLR(bmap
, ISL_BASIC_MAP_ALL_EQUALITIES
);
1489 isl_seq_clr(bmap
->ineq
[bmap
->n_ineq
] +
1490 1 + isl_basic_map_total_dim(bmap
),
1491 bmap
->extra
- bmap
->n_div
);
1492 return bmap
->n_ineq
++;
1495 int isl_basic_set_alloc_inequality(struct isl_basic_set
*bset
)
1497 return isl_basic_map_alloc_inequality(bset_to_bmap(bset
));
1500 int isl_basic_map_free_inequality(struct isl_basic_map
*bmap
, unsigned n
)
1504 isl_assert(bmap
->ctx
, n
<= bmap
->n_ineq
, return -1);
1509 int isl_basic_set_free_inequality(struct isl_basic_set
*bset
, unsigned n
)
1511 return isl_basic_map_free_inequality(bset_to_bmap(bset
), n
);
1514 int isl_basic_map_drop_inequality(struct isl_basic_map
*bmap
, unsigned pos
)
1519 isl_assert(bmap
->ctx
, pos
< bmap
->n_ineq
, return -1);
1521 if (pos
!= bmap
->n_ineq
- 1) {
1522 t
= bmap
->ineq
[pos
];
1523 bmap
->ineq
[pos
] = bmap
->ineq
[bmap
->n_ineq
- 1];
1524 bmap
->ineq
[bmap
->n_ineq
- 1] = t
;
1525 ISL_F_CLR(bmap
, ISL_BASIC_MAP_NORMALIZED
);
1531 int isl_basic_set_drop_inequality(struct isl_basic_set
*bset
, unsigned pos
)
1533 return isl_basic_map_drop_inequality(bset_to_bmap(bset
), pos
);
1536 __isl_give isl_basic_map
*isl_basic_map_add_eq(__isl_take isl_basic_map
*bmap
,
1541 bmap
= isl_basic_map_extend_constraints(bmap
, 1, 0);
1544 k
= isl_basic_map_alloc_equality(bmap
);
1547 isl_seq_cpy(bmap
->eq
[k
], eq
, 1 + isl_basic_map_total_dim(bmap
));
1550 isl_basic_map_free(bmap
);
1554 __isl_give isl_basic_set
*isl_basic_set_add_eq(__isl_take isl_basic_set
*bset
,
1557 return bset_from_bmap(isl_basic_map_add_eq(bset_to_bmap(bset
), eq
));
1560 __isl_give isl_basic_map
*isl_basic_map_add_ineq(__isl_take isl_basic_map
*bmap
,
1565 bmap
= isl_basic_map_extend_constraints(bmap
, 0, 1);
1568 k
= isl_basic_map_alloc_inequality(bmap
);
1571 isl_seq_cpy(bmap
->ineq
[k
], ineq
, 1 + isl_basic_map_total_dim(bmap
));
1574 isl_basic_map_free(bmap
);
1578 __isl_give isl_basic_set
*isl_basic_set_add_ineq(__isl_take isl_basic_set
*bset
,
1581 return bset_from_bmap(isl_basic_map_add_ineq(bset_to_bmap(bset
), ineq
));
1584 int isl_basic_map_alloc_div(struct isl_basic_map
*bmap
)
1588 isl_assert(bmap
->ctx
, bmap
->n_div
< bmap
->extra
, return -1);
1589 isl_seq_clr(bmap
->div
[bmap
->n_div
] +
1590 1 + 1 + isl_basic_map_total_dim(bmap
),
1591 bmap
->extra
- bmap
->n_div
);
1592 ISL_F_CLR(bmap
, ISL_BASIC_MAP_NORMALIZED_DIVS
);
1593 return bmap
->n_div
++;
1596 int isl_basic_set_alloc_div(struct isl_basic_set
*bset
)
1598 return isl_basic_map_alloc_div(bset_to_bmap(bset
));
1601 /* Check that there are "n" dimensions of type "type" starting at "first"
1604 static isl_stat
isl_basic_map_check_range(__isl_keep isl_basic_map
*bmap
,
1605 enum isl_dim_type type
, unsigned first
, unsigned n
)
1610 return isl_stat_error
;
1611 dim
= isl_basic_map_dim(bmap
, type
);
1612 if (first
+ n
> dim
|| first
+ n
< first
)
1613 isl_die(isl_basic_map_get_ctx(bmap
), isl_error_invalid
,
1614 "position or range out of bounds",
1615 return isl_stat_error
);
1619 /* Insert an extra integer division, prescribed by "div", to "bmap"
1620 * at (integer division) position "pos".
1622 * The integer division is first added at the end and then moved
1623 * into the right position.
1625 __isl_give isl_basic_map
*isl_basic_map_insert_div(
1626 __isl_take isl_basic_map
*bmap
, int pos
, __isl_keep isl_vec
*div
)
1630 bmap
= isl_basic_map_cow(bmap
);
1632 return isl_basic_map_free(bmap
);
1634 if (div
->size
!= 1 + 1 + isl_basic_map_dim(bmap
, isl_dim_all
))
1635 isl_die(isl_basic_map_get_ctx(bmap
), isl_error_invalid
,
1636 "unexpected size", return isl_basic_map_free(bmap
));
1637 if (isl_basic_map_check_range(bmap
, isl_dim_div
, pos
, 0) < 0)
1638 return isl_basic_map_free(bmap
);
1640 bmap
= isl_basic_map_extend_space(bmap
,
1641 isl_basic_map_get_space(bmap
), 1, 0, 2);
1642 k
= isl_basic_map_alloc_div(bmap
);
1644 return isl_basic_map_free(bmap
);
1645 isl_seq_cpy(bmap
->div
[k
], div
->el
, div
->size
);
1646 isl_int_set_si(bmap
->div
[k
][div
->size
], 0);
1648 for (i
= k
; i
> pos
; --i
)
1649 isl_basic_map_swap_div(bmap
, i
, i
- 1);
1654 isl_stat
isl_basic_map_free_div(struct isl_basic_map
*bmap
, unsigned n
)
1657 return isl_stat_error
;
1658 isl_assert(bmap
->ctx
, n
<= bmap
->n_div
, return isl_stat_error
);
1663 /* Copy constraint from src to dst, putting the vars of src at offset
1664 * dim_off in dst and the divs of src at offset div_off in dst.
1665 * If both sets are actually map, then dim_off applies to the input
1668 static void copy_constraint(struct isl_basic_map
*dst_map
, isl_int
*dst
,
1669 struct isl_basic_map
*src_map
, isl_int
*src
,
1670 unsigned in_off
, unsigned out_off
, unsigned div_off
)
1672 unsigned src_nparam
= isl_basic_map_dim(src_map
, isl_dim_param
);
1673 unsigned dst_nparam
= isl_basic_map_dim(dst_map
, isl_dim_param
);
1674 unsigned src_in
= isl_basic_map_dim(src_map
, isl_dim_in
);
1675 unsigned dst_in
= isl_basic_map_dim(dst_map
, isl_dim_in
);
1676 unsigned src_out
= isl_basic_map_dim(src_map
, isl_dim_out
);
1677 unsigned dst_out
= isl_basic_map_dim(dst_map
, isl_dim_out
);
1678 isl_int_set(dst
[0], src
[0]);
1679 isl_seq_cpy(dst
+1, src
+1, isl_min(dst_nparam
, src_nparam
));
1680 if (dst_nparam
> src_nparam
)
1681 isl_seq_clr(dst
+1+src_nparam
,
1682 dst_nparam
- src_nparam
);
1683 isl_seq_clr(dst
+1+dst_nparam
, in_off
);
1684 isl_seq_cpy(dst
+1+dst_nparam
+in_off
,
1686 isl_min(dst_in
-in_off
, src_in
));
1687 if (dst_in
-in_off
> src_in
)
1688 isl_seq_clr(dst
+1+dst_nparam
+in_off
+src_in
,
1689 dst_in
- in_off
- src_in
);
1690 isl_seq_clr(dst
+1+dst_nparam
+dst_in
, out_off
);
1691 isl_seq_cpy(dst
+1+dst_nparam
+dst_in
+out_off
,
1692 src
+1+src_nparam
+src_in
,
1693 isl_min(dst_out
-out_off
, src_out
));
1694 if (dst_out
-out_off
> src_out
)
1695 isl_seq_clr(dst
+1+dst_nparam
+dst_in
+out_off
+src_out
,
1696 dst_out
- out_off
- src_out
);
1697 isl_seq_clr(dst
+1+dst_nparam
+dst_in
+dst_out
, div_off
);
1698 isl_seq_cpy(dst
+1+dst_nparam
+dst_in
+dst_out
+div_off
,
1699 src
+1+src_nparam
+src_in
+src_out
,
1700 isl_min(dst_map
->extra
-div_off
, src_map
->n_div
));
1701 if (dst_map
->n_div
-div_off
> src_map
->n_div
)
1702 isl_seq_clr(dst
+1+dst_nparam
+dst_in
+dst_out
+
1703 div_off
+src_map
->n_div
,
1704 dst_map
->n_div
- div_off
- src_map
->n_div
);
1707 static void copy_div(struct isl_basic_map
*dst_map
, isl_int
*dst
,
1708 struct isl_basic_map
*src_map
, isl_int
*src
,
1709 unsigned in_off
, unsigned out_off
, unsigned div_off
)
1711 isl_int_set(dst
[0], src
[0]);
1712 copy_constraint(dst_map
, dst
+1, src_map
, src
+1, in_off
, out_off
, div_off
);
1715 static __isl_give isl_basic_map
*add_constraints(
1716 __isl_take isl_basic_map
*bmap1
, __isl_take isl_basic_map
*bmap2
,
1717 unsigned i_pos
, unsigned o_pos
)
1722 if (!bmap1
|| !bmap2
)
1725 div_off
= bmap1
->n_div
;
1727 for (i
= 0; i
< bmap2
->n_eq
; ++i
) {
1728 int i1
= isl_basic_map_alloc_equality(bmap1
);
1731 copy_constraint(bmap1
, bmap1
->eq
[i1
], bmap2
, bmap2
->eq
[i
],
1732 i_pos
, o_pos
, div_off
);
1735 for (i
= 0; i
< bmap2
->n_ineq
; ++i
) {
1736 int i1
= isl_basic_map_alloc_inequality(bmap1
);
1739 copy_constraint(bmap1
, bmap1
->ineq
[i1
], bmap2
, bmap2
->ineq
[i
],
1740 i_pos
, o_pos
, div_off
);
1743 for (i
= 0; i
< bmap2
->n_div
; ++i
) {
1744 int i1
= isl_basic_map_alloc_div(bmap1
);
1747 copy_div(bmap1
, bmap1
->div
[i1
], bmap2
, bmap2
->div
[i
],
1748 i_pos
, o_pos
, div_off
);
1751 isl_basic_map_free(bmap2
);
1756 isl_basic_map_free(bmap1
);
1757 isl_basic_map_free(bmap2
);
1761 struct isl_basic_set
*isl_basic_set_add_constraints(struct isl_basic_set
*bset1
,
1762 struct isl_basic_set
*bset2
, unsigned pos
)
1764 return bset_from_bmap(add_constraints(bset_to_bmap(bset1
),
1765 bset_to_bmap(bset2
), 0, pos
));
1768 __isl_give isl_basic_map
*isl_basic_map_extend_space(
1769 __isl_take isl_basic_map
*base
, __isl_take isl_space
*dim
,
1770 unsigned extra
, unsigned n_eq
, unsigned n_ineq
)
1772 struct isl_basic_map
*ext
;
1782 dims_ok
= isl_space_is_equal(base
->dim
, dim
) &&
1783 base
->extra
>= base
->n_div
+ extra
;
1785 if (dims_ok
&& room_for_con(base
, n_eq
+ n_ineq
) &&
1786 room_for_ineq(base
, n_ineq
)) {
1787 isl_space_free(dim
);
1791 isl_assert(base
->ctx
, base
->dim
->nparam
<= dim
->nparam
, goto error
);
1792 isl_assert(base
->ctx
, base
->dim
->n_in
<= dim
->n_in
, goto error
);
1793 isl_assert(base
->ctx
, base
->dim
->n_out
<= dim
->n_out
, goto error
);
1794 extra
+= base
->extra
;
1796 n_ineq
+= base
->n_ineq
;
1798 ext
= isl_basic_map_alloc_space(dim
, extra
, n_eq
, n_ineq
);
1804 ext
->sample
= isl_vec_copy(base
->sample
);
1805 flags
= base
->flags
;
1806 ext
= add_constraints(ext
, base
, 0, 0);
1809 ISL_F_CLR(ext
, ISL_BASIC_SET_FINAL
);
1815 isl_space_free(dim
);
1816 isl_basic_map_free(base
);
1820 struct isl_basic_set
*isl_basic_set_extend_space(struct isl_basic_set
*base
,
1821 __isl_take isl_space
*dim
, unsigned extra
,
1822 unsigned n_eq
, unsigned n_ineq
)
1824 return bset_from_bmap(isl_basic_map_extend_space(bset_to_bmap(base
),
1825 dim
, extra
, n_eq
, n_ineq
));
1828 struct isl_basic_map
*isl_basic_map_extend_constraints(
1829 struct isl_basic_map
*base
, unsigned n_eq
, unsigned n_ineq
)
1833 return isl_basic_map_extend_space(base
, isl_space_copy(base
->dim
),
1837 struct isl_basic_map
*isl_basic_map_extend(struct isl_basic_map
*base
,
1838 unsigned nparam
, unsigned n_in
, unsigned n_out
, unsigned extra
,
1839 unsigned n_eq
, unsigned n_ineq
)
1841 struct isl_basic_map
*bmap
;
1846 dim
= isl_space_alloc(base
->ctx
, nparam
, n_in
, n_out
);
1850 bmap
= isl_basic_map_extend_space(base
, dim
, extra
, n_eq
, n_ineq
);
1853 isl_basic_map_free(base
);
1857 struct isl_basic_set
*isl_basic_set_extend(struct isl_basic_set
*base
,
1858 unsigned nparam
, unsigned dim
, unsigned extra
,
1859 unsigned n_eq
, unsigned n_ineq
)
1861 return bset_from_bmap(isl_basic_map_extend(bset_to_bmap(base
),
1862 nparam
, 0, dim
, extra
, n_eq
, n_ineq
));
1865 struct isl_basic_set
*isl_basic_set_extend_constraints(
1866 struct isl_basic_set
*base
, unsigned n_eq
, unsigned n_ineq
)
1868 isl_basic_map
*bmap
= bset_to_bmap(base
);
1869 bmap
= isl_basic_map_extend_constraints(bmap
, n_eq
, n_ineq
);
1870 return bset_from_bmap(bmap
);
1873 __isl_give isl_basic_set
*isl_basic_set_cow(__isl_take isl_basic_set
*bset
)
1875 return bset_from_bmap(isl_basic_map_cow(bset_to_bmap(bset
)));
1878 struct isl_basic_map
*isl_basic_map_cow(struct isl_basic_map
*bmap
)
1883 if (bmap
->ref
> 1) {
1885 bmap
= isl_basic_map_dup(bmap
);
1888 ISL_F_CLR(bmap
, ISL_BASIC_SET_FINAL
);
1889 ISL_F_CLR(bmap
, ISL_BASIC_MAP_REDUCED_COEFFICIENTS
);
1894 /* Clear all cached information in "map", either because it is about
1895 * to be modified or because it is being freed.
1896 * Always return the same pointer that is passed in.
1897 * This is needed for the use in isl_map_free.
1899 static __isl_give isl_map
*clear_caches(__isl_take isl_map
*map
)
1901 isl_basic_map_free(map
->cached_simple_hull
[0]);
1902 isl_basic_map_free(map
->cached_simple_hull
[1]);
1903 map
->cached_simple_hull
[0] = NULL
;
1904 map
->cached_simple_hull
[1] = NULL
;
1908 struct isl_set
*isl_set_cow(struct isl_set
*set
)
1910 return isl_map_cow(set
);
1913 /* Return an isl_map that is equal to "map" and that has only
1914 * a single reference.
1916 * If the original input already has only one reference, then
1917 * simply return it, but clear all cached information, since
1918 * it may be rendered invalid by the operations that will be
1919 * performed on the result.
1921 * Otherwise, create a duplicate (without any cached information).
1923 struct isl_map
*isl_map_cow(struct isl_map
*map
)
1929 return clear_caches(map
);
1931 return isl_map_dup(map
);
1934 static void swap_vars(struct isl_blk blk
, isl_int
*a
,
1935 unsigned a_len
, unsigned b_len
)
1937 isl_seq_cpy(blk
.data
, a
+a_len
, b_len
);
1938 isl_seq_cpy(blk
.data
+b_len
, a
, a_len
);
1939 isl_seq_cpy(a
, blk
.data
, b_len
+a_len
);
1942 static __isl_give isl_basic_map
*isl_basic_map_swap_vars(
1943 __isl_take isl_basic_map
*bmap
, unsigned pos
, unsigned n1
, unsigned n2
)
1951 isl_assert(bmap
->ctx
,
1952 pos
+ n1
+ n2
<= 1 + isl_basic_map_total_dim(bmap
), goto error
);
1954 if (n1
== 0 || n2
== 0)
1957 bmap
= isl_basic_map_cow(bmap
);
1961 blk
= isl_blk_alloc(bmap
->ctx
, n1
+ n2
);
1962 if (isl_blk_is_error(blk
))
1965 for (i
= 0; i
< bmap
->n_eq
; ++i
)
1967 bmap
->eq
[i
] + pos
, n1
, n2
);
1969 for (i
= 0; i
< bmap
->n_ineq
; ++i
)
1971 bmap
->ineq
[i
] + pos
, n1
, n2
);
1973 for (i
= 0; i
< bmap
->n_div
; ++i
)
1975 bmap
->div
[i
]+1 + pos
, n1
, n2
);
1977 isl_blk_free(bmap
->ctx
, blk
);
1979 ISL_F_CLR(bmap
, ISL_BASIC_SET_NORMALIZED
);
1980 bmap
= isl_basic_map_gauss(bmap
, NULL
);
1981 return isl_basic_map_finalize(bmap
);
1983 isl_basic_map_free(bmap
);
1987 struct isl_basic_map
*isl_basic_map_set_to_empty(struct isl_basic_map
*bmap
)
1993 total
= isl_basic_map_total_dim(bmap
);
1994 if (isl_basic_map_free_div(bmap
, bmap
->n_div
) < 0)
1995 return isl_basic_map_free(bmap
);
1996 isl_basic_map_free_inequality(bmap
, bmap
->n_ineq
);
1998 isl_basic_map_free_equality(bmap
, bmap
->n_eq
-1);
2000 i
= isl_basic_map_alloc_equality(bmap
);
2004 isl_int_set_si(bmap
->eq
[i
][0], 1);
2005 isl_seq_clr(bmap
->eq
[i
]+1, total
);
2006 ISL_F_SET(bmap
, ISL_BASIC_MAP_EMPTY
);
2007 isl_vec_free(bmap
->sample
);
2008 bmap
->sample
= NULL
;
2009 return isl_basic_map_finalize(bmap
);
2011 isl_basic_map_free(bmap
);
2015 struct isl_basic_set
*isl_basic_set_set_to_empty(struct isl_basic_set
*bset
)
2017 return bset_from_bmap(isl_basic_map_set_to_empty(bset_to_bmap(bset
)));
2020 __isl_give isl_basic_map
*isl_basic_map_set_rational(
2021 __isl_take isl_basic_map
*bmap
)
2026 if (ISL_F_ISSET(bmap
, ISL_BASIC_MAP_RATIONAL
))
2029 bmap
= isl_basic_map_cow(bmap
);
2033 ISL_F_SET(bmap
, ISL_BASIC_MAP_RATIONAL
);
2035 return isl_basic_map_finalize(bmap
);
2038 __isl_give isl_basic_set
*isl_basic_set_set_rational(
2039 __isl_take isl_basic_set
*bset
)
2041 return isl_basic_map_set_rational(bset
);
2044 __isl_give isl_basic_set
*isl_basic_set_set_integral(
2045 __isl_take isl_basic_set
*bset
)
2050 if (!ISL_F_ISSET(bset
, ISL_BASIC_MAP_RATIONAL
))
2053 bset
= isl_basic_set_cow(bset
);
2057 ISL_F_CLR(bset
, ISL_BASIC_MAP_RATIONAL
);
2059 return isl_basic_set_finalize(bset
);
2062 __isl_give isl_map
*isl_map_set_rational(__isl_take isl_map
*map
)
2066 map
= isl_map_cow(map
);
2069 for (i
= 0; i
< map
->n
; ++i
) {
2070 map
->p
[i
] = isl_basic_map_set_rational(map
->p
[i
]);
2080 __isl_give isl_set
*isl_set_set_rational(__isl_take isl_set
*set
)
2082 return isl_map_set_rational(set
);
2085 /* Swap divs "a" and "b" in "bmap" (without modifying any of the constraints
2088 static void swap_div(__isl_keep isl_basic_map
*bmap
, int a
, int b
)
2090 isl_int
*t
= bmap
->div
[a
];
2091 bmap
->div
[a
] = bmap
->div
[b
];
2095 /* Swap divs "a" and "b" in "bmap" and adjust the constraints and
2096 * div definitions accordingly.
2098 void isl_basic_map_swap_div(struct isl_basic_map
*bmap
, int a
, int b
)
2101 unsigned off
= isl_space_dim(bmap
->dim
, isl_dim_all
);
2103 swap_div(bmap
, a
, b
);
2105 for (i
= 0; i
< bmap
->n_eq
; ++i
)
2106 isl_int_swap(bmap
->eq
[i
][1+off
+a
], bmap
->eq
[i
][1+off
+b
]);
2108 for (i
= 0; i
< bmap
->n_ineq
; ++i
)
2109 isl_int_swap(bmap
->ineq
[i
][1+off
+a
], bmap
->ineq
[i
][1+off
+b
]);
2111 for (i
= 0; i
< bmap
->n_div
; ++i
)
2112 isl_int_swap(bmap
->div
[i
][1+1+off
+a
], bmap
->div
[i
][1+1+off
+b
]);
2113 ISL_F_CLR(bmap
, ISL_BASIC_MAP_NORMALIZED
);
2116 /* Swap divs "a" and "b" in "bset" and adjust the constraints and
2117 * div definitions accordingly.
2119 void isl_basic_set_swap_div(__isl_keep isl_basic_set
*bset
, int a
, int b
)
2121 isl_basic_map_swap_div(bset
, a
, b
);
2124 static void constraint_drop_vars(isl_int
*c
, unsigned n
, unsigned rem
)
2126 isl_seq_cpy(c
, c
+ n
, rem
);
2127 isl_seq_clr(c
+ rem
, n
);
2130 /* Drop n dimensions starting at first.
2132 * In principle, this frees up some extra variables as the number
2133 * of columns remains constant, but we would have to extend
2134 * the div array too as the number of rows in this array is assumed
2135 * to be equal to extra.
2137 struct isl_basic_set
*isl_basic_set_drop_dims(
2138 struct isl_basic_set
*bset
, unsigned first
, unsigned n
)
2140 return isl_basic_map_drop(bset_to_bmap(bset
), isl_dim_set
, first
, n
);
2143 /* Move "n" divs starting at "first" to the end of the list of divs.
2145 static struct isl_basic_map
*move_divs_last(struct isl_basic_map
*bmap
,
2146 unsigned first
, unsigned n
)
2151 if (first
+ n
== bmap
->n_div
)
2154 div
= isl_alloc_array(bmap
->ctx
, isl_int
*, n
);
2157 for (i
= 0; i
< n
; ++i
)
2158 div
[i
] = bmap
->div
[first
+ i
];
2159 for (i
= 0; i
< bmap
->n_div
- first
- n
; ++i
)
2160 bmap
->div
[first
+ i
] = bmap
->div
[first
+ n
+ i
];
2161 for (i
= 0; i
< n
; ++i
)
2162 bmap
->div
[bmap
->n_div
- n
+ i
] = div
[i
];
2166 isl_basic_map_free(bmap
);
2170 /* Drop "n" dimensions of type "type" starting at "first".
2172 * In principle, this frees up some extra variables as the number
2173 * of columns remains constant, but we would have to extend
2174 * the div array too as the number of rows in this array is assumed
2175 * to be equal to extra.
2177 struct isl_basic_map
*isl_basic_map_drop(struct isl_basic_map
*bmap
,
2178 enum isl_dim_type type
, unsigned first
, unsigned n
)
2188 dim
= isl_basic_map_dim(bmap
, type
);
2189 isl_assert(bmap
->ctx
, first
+ n
<= dim
, goto error
);
2191 if (n
== 0 && !isl_space_is_named_or_nested(bmap
->dim
, type
))
2194 bmap
= isl_basic_map_cow(bmap
);
2198 offset
= isl_basic_map_offset(bmap
, type
) + first
;
2199 left
= isl_basic_map_total_dim(bmap
) - (offset
- 1) - n
;
2200 for (i
= 0; i
< bmap
->n_eq
; ++i
)
2201 constraint_drop_vars(bmap
->eq
[i
]+offset
, n
, left
);
2203 for (i
= 0; i
< bmap
->n_ineq
; ++i
)
2204 constraint_drop_vars(bmap
->ineq
[i
]+offset
, n
, left
);
2206 for (i
= 0; i
< bmap
->n_div
; ++i
)
2207 constraint_drop_vars(bmap
->div
[i
]+1+offset
, n
, left
);
2209 if (type
== isl_dim_div
) {
2210 bmap
= move_divs_last(bmap
, first
, n
);
2213 if (isl_basic_map_free_div(bmap
, n
) < 0)
2214 return isl_basic_map_free(bmap
);
2216 bmap
->dim
= isl_space_drop_dims(bmap
->dim
, type
, first
, n
);
2220 ISL_F_CLR(bmap
, ISL_BASIC_MAP_NORMALIZED
);
2221 bmap
= isl_basic_map_simplify(bmap
);
2222 return isl_basic_map_finalize(bmap
);
2224 isl_basic_map_free(bmap
);
2228 __isl_give isl_basic_set
*isl_basic_set_drop(__isl_take isl_basic_set
*bset
,
2229 enum isl_dim_type type
, unsigned first
, unsigned n
)
2231 return bset_from_bmap(isl_basic_map_drop(bset_to_bmap(bset
),
2235 struct isl_map
*isl_map_drop(struct isl_map
*map
,
2236 enum isl_dim_type type
, unsigned first
, unsigned n
)
2243 isl_assert(map
->ctx
, first
+ n
<= isl_map_dim(map
, type
), goto error
);
2245 if (n
== 0 && !isl_space_is_named_or_nested(map
->dim
, type
))
2247 map
= isl_map_cow(map
);
2250 map
->dim
= isl_space_drop_dims(map
->dim
, type
, first
, n
);
2254 for (i
= 0; i
< map
->n
; ++i
) {
2255 map
->p
[i
] = isl_basic_map_drop(map
->p
[i
], type
, first
, n
);
2259 ISL_F_CLR(map
, ISL_MAP_NORMALIZED
);
2267 struct isl_set
*isl_set_drop(struct isl_set
*set
,
2268 enum isl_dim_type type
, unsigned first
, unsigned n
)
2270 return set_from_map(isl_map_drop(set_to_map(set
), type
, first
, n
));
2274 * We don't cow, as the div is assumed to be redundant.
2276 __isl_give isl_basic_map
*isl_basic_map_drop_div(
2277 __isl_take isl_basic_map
*bmap
, unsigned div
)
2285 pos
= 1 + isl_space_dim(bmap
->dim
, isl_dim_all
) + div
;
2287 isl_assert(bmap
->ctx
, div
< bmap
->n_div
, goto error
);
2289 for (i
= 0; i
< bmap
->n_eq
; ++i
)
2290 constraint_drop_vars(bmap
->eq
[i
]+pos
, 1, bmap
->extra
-div
-1);
2292 for (i
= 0; i
< bmap
->n_ineq
; ++i
) {
2293 if (!isl_int_is_zero(bmap
->ineq
[i
][pos
])) {
2294 isl_basic_map_drop_inequality(bmap
, i
);
2298 constraint_drop_vars(bmap
->ineq
[i
]+pos
, 1, bmap
->extra
-div
-1);
2301 for (i
= 0; i
< bmap
->n_div
; ++i
)
2302 constraint_drop_vars(bmap
->div
[i
]+1+pos
, 1, bmap
->extra
-div
-1);
2304 if (div
!= bmap
->n_div
- 1) {
2306 isl_int
*t
= bmap
->div
[div
];
2308 for (j
= div
; j
< bmap
->n_div
- 1; ++j
)
2309 bmap
->div
[j
] = bmap
->div
[j
+1];
2311 bmap
->div
[bmap
->n_div
- 1] = t
;
2313 ISL_F_CLR(bmap
, ISL_BASIC_MAP_NORMALIZED
);
2314 if (isl_basic_map_free_div(bmap
, 1) < 0)
2315 return isl_basic_map_free(bmap
);
2319 isl_basic_map_free(bmap
);
2323 /* Eliminate the specified n dimensions starting at first from the
2324 * constraints, without removing the dimensions from the space.
2325 * If the set is rational, the dimensions are eliminated using Fourier-Motzkin.
2327 __isl_give isl_map
*isl_map_eliminate(__isl_take isl_map
*map
,
2328 enum isl_dim_type type
, unsigned first
, unsigned n
)
2337 if (first
+ n
> isl_map_dim(map
, type
) || first
+ n
< first
)
2338 isl_die(map
->ctx
, isl_error_invalid
,
2339 "index out of bounds", goto error
);
2341 map
= isl_map_cow(map
);
2345 for (i
= 0; i
< map
->n
; ++i
) {
2346 map
->p
[i
] = isl_basic_map_eliminate(map
->p
[i
], type
, first
, n
);
2356 /* Eliminate the specified n dimensions starting at first from the
2357 * constraints, without removing the dimensions from the space.
2358 * If the set is rational, the dimensions are eliminated using Fourier-Motzkin.
2360 __isl_give isl_set
*isl_set_eliminate(__isl_take isl_set
*set
,
2361 enum isl_dim_type type
, unsigned first
, unsigned n
)
2363 return set_from_map(isl_map_eliminate(set_to_map(set
), type
, first
, n
));
2366 /* Eliminate the specified n dimensions starting at first from the
2367 * constraints, without removing the dimensions from the space.
2368 * If the set is rational, the dimensions are eliminated using Fourier-Motzkin.
2370 __isl_give isl_set
*isl_set_eliminate_dims(__isl_take isl_set
*set
,
2371 unsigned first
, unsigned n
)
2373 return isl_set_eliminate(set
, isl_dim_set
, first
, n
);
2376 __isl_give isl_basic_map
*isl_basic_map_remove_divs(
2377 __isl_take isl_basic_map
*bmap
)
2381 bmap
= isl_basic_map_eliminate_vars(bmap
,
2382 isl_space_dim(bmap
->dim
, isl_dim_all
), bmap
->n_div
);
2386 return isl_basic_map_finalize(bmap
);
2389 __isl_give isl_basic_set
*isl_basic_set_remove_divs(
2390 __isl_take isl_basic_set
*bset
)
2392 return bset_from_bmap(isl_basic_map_remove_divs(bset_to_bmap(bset
)));
2395 __isl_give isl_map
*isl_map_remove_divs(__isl_take isl_map
*map
)
2404 map
= isl_map_cow(map
);
2408 for (i
= 0; i
< map
->n
; ++i
) {
2409 map
->p
[i
] = isl_basic_map_remove_divs(map
->p
[i
]);
2419 __isl_give isl_set
*isl_set_remove_divs(__isl_take isl_set
*set
)
2421 return isl_map_remove_divs(set
);
2424 __isl_give isl_basic_map
*isl_basic_map_remove_dims(
2425 __isl_take isl_basic_map
*bmap
, enum isl_dim_type type
,
2426 unsigned first
, unsigned n
)
2428 if (isl_basic_map_check_range(bmap
, type
, first
, n
) < 0)
2429 return isl_basic_map_free(bmap
);
2430 if (n
== 0 && !isl_space_is_named_or_nested(bmap
->dim
, type
))
2432 bmap
= isl_basic_map_eliminate_vars(bmap
,
2433 isl_basic_map_offset(bmap
, type
) - 1 + first
, n
);
2436 if (ISL_F_ISSET(bmap
, ISL_BASIC_MAP_EMPTY
) && type
== isl_dim_div
)
2438 bmap
= isl_basic_map_drop(bmap
, type
, first
, n
);
2442 /* Return true if the definition of the given div (recursively) involves
2443 * any of the given variables.
2445 static isl_bool
div_involves_vars(__isl_keep isl_basic_map
*bmap
, int div
,
2446 unsigned first
, unsigned n
)
2449 unsigned div_offset
= isl_basic_map_offset(bmap
, isl_dim_div
);
2451 if (isl_int_is_zero(bmap
->div
[div
][0]))
2452 return isl_bool_false
;
2453 if (isl_seq_first_non_zero(bmap
->div
[div
] + 1 + first
, n
) >= 0)
2454 return isl_bool_true
;
2456 for (i
= bmap
->n_div
- 1; i
>= 0; --i
) {
2459 if (isl_int_is_zero(bmap
->div
[div
][1 + div_offset
+ i
]))
2461 involves
= div_involves_vars(bmap
, i
, first
, n
);
2462 if (involves
< 0 || involves
)
2466 return isl_bool_false
;
2469 /* Try and add a lower and/or upper bound on "div" to "bmap"
2470 * based on inequality "i".
2471 * "total" is the total number of variables (excluding the divs).
2472 * "v" is a temporary object that can be used during the calculations.
2473 * If "lb" is set, then a lower bound should be constructed.
2474 * If "ub" is set, then an upper bound should be constructed.
2476 * The calling function has already checked that the inequality does not
2477 * reference "div", but we still need to check that the inequality is
2478 * of the right form. We'll consider the case where we want to construct
2479 * a lower bound. The construction of upper bounds is similar.
2481 * Let "div" be of the form
2483 * q = floor((a + f(x))/d)
2485 * We essentially check if constraint "i" is of the form
2489 * so that we can use it to derive a lower bound on "div".
2490 * However, we allow a slightly more general form
2494 * with the condition that the coefficients of g(x) - f(x) are all
2496 * Rewriting this constraint as
2500 * adding a + f(x) to both sides and dividing by d, we obtain
2502 * (a + f(x))/d >= (a-b)/d + (f(x)-g(x))/d
2504 * Taking the floor on both sides, we obtain
2506 * q >= floor((a-b)/d) + (f(x)-g(x))/d
2510 * (g(x)-f(x))/d + ceil((b-a)/d) + q >= 0
2512 * In the case of an upper bound, we construct the constraint
2514 * (g(x)+f(x))/d + floor((b+a)/d) - q >= 0
2517 static __isl_give isl_basic_map
*insert_bounds_on_div_from_ineq(
2518 __isl_take isl_basic_map
*bmap
, int div
, int i
,
2519 unsigned total
, isl_int v
, int lb
, int ub
)
2523 for (j
= 0; (lb
|| ub
) && j
< total
+ bmap
->n_div
; ++j
) {
2525 isl_int_sub(v
, bmap
->ineq
[i
][1 + j
],
2526 bmap
->div
[div
][1 + 1 + j
]);
2527 lb
= isl_int_is_divisible_by(v
, bmap
->div
[div
][0]);
2530 isl_int_add(v
, bmap
->ineq
[i
][1 + j
],
2531 bmap
->div
[div
][1 + 1 + j
]);
2532 ub
= isl_int_is_divisible_by(v
, bmap
->div
[div
][0]);
2538 bmap
= isl_basic_map_cow(bmap
);
2539 bmap
= isl_basic_map_extend_constraints(bmap
, 0, lb
+ ub
);
2541 int k
= isl_basic_map_alloc_inequality(bmap
);
2544 for (j
= 0; j
< 1 + total
+ bmap
->n_div
; ++j
) {
2545 isl_int_sub(bmap
->ineq
[k
][j
], bmap
->ineq
[i
][j
],
2546 bmap
->div
[div
][1 + j
]);
2547 isl_int_cdiv_q(bmap
->ineq
[k
][j
],
2548 bmap
->ineq
[k
][j
], bmap
->div
[div
][0]);
2550 isl_int_set_si(bmap
->ineq
[k
][1 + total
+ div
], 1);
2553 int k
= isl_basic_map_alloc_inequality(bmap
);
2556 for (j
= 0; j
< 1 + total
+ bmap
->n_div
; ++j
) {
2557 isl_int_add(bmap
->ineq
[k
][j
], bmap
->ineq
[i
][j
],
2558 bmap
->div
[div
][1 + j
]);
2559 isl_int_fdiv_q(bmap
->ineq
[k
][j
],
2560 bmap
->ineq
[k
][j
], bmap
->div
[div
][0]);
2562 isl_int_set_si(bmap
->ineq
[k
][1 + total
+ div
], -1);
2565 ISL_F_CLR(bmap
, ISL_BASIC_MAP_NORMALIZED
);
2568 isl_basic_map_free(bmap
);
2572 /* This function is called right before "div" is eliminated from "bmap"
2573 * using Fourier-Motzkin.
2574 * Look through the constraints of "bmap" for constraints on the argument
2575 * of the integer division and use them to construct constraints on the
2576 * integer division itself. These constraints can then be combined
2577 * during the Fourier-Motzkin elimination.
2578 * Note that it is only useful to introduce lower bounds on "div"
2579 * if "bmap" already contains upper bounds on "div" as the newly
2580 * introduce lower bounds can then be combined with the pre-existing
2581 * upper bounds. Similarly for upper bounds.
2582 * We therefore first check if "bmap" contains any lower and/or upper bounds
2585 * It is interesting to note that the introduction of these constraints
2586 * can indeed lead to more accurate results, even when compared to
2587 * deriving constraints on the argument of "div" from constraints on "div".
2588 * Consider, for example, the set
2590 * { [i,j,k] : 3 + i + 2j >= 0 and 2 * [(i+2j)/4] <= k }
2592 * The second constraint can be rewritten as
2594 * 2 * [(-i-2j+3)/4] + k >= 0
2596 * from which we can derive
2598 * -i - 2j + 3 >= -2k
2604 * Combined with the first constraint, we obtain
2606 * -3 <= 3 + 2k or k >= -3
2608 * If, on the other hand we derive a constraint on [(i+2j)/4] from
2609 * the first constraint, we obtain
2611 * [(i + 2j)/4] >= [-3/4] = -1
2613 * Combining this constraint with the second constraint, we obtain
2617 static __isl_give isl_basic_map
*insert_bounds_on_div(
2618 __isl_take isl_basic_map
*bmap
, int div
)
2621 int check_lb
, check_ub
;
2628 if (isl_int_is_zero(bmap
->div
[div
][0]))
2631 total
= isl_space_dim(bmap
->dim
, isl_dim_all
);
2635 for (i
= 0; (!check_lb
|| !check_ub
) && i
< bmap
->n_ineq
; ++i
) {
2636 int s
= isl_int_sgn(bmap
->ineq
[i
][1 + total
+ div
]);
2643 if (!check_lb
&& !check_ub
)
2648 for (i
= 0; bmap
&& i
< bmap
->n_ineq
; ++i
) {
2649 if (!isl_int_is_zero(bmap
->ineq
[i
][1 + total
+ div
]))
2652 bmap
= insert_bounds_on_div_from_ineq(bmap
, div
, i
, total
, v
,
2653 check_lb
, check_ub
);
2661 /* Remove all divs (recursively) involving any of the given dimensions
2662 * in their definitions.
2664 __isl_give isl_basic_map
*isl_basic_map_remove_divs_involving_dims(
2665 __isl_take isl_basic_map
*bmap
,
2666 enum isl_dim_type type
, unsigned first
, unsigned n
)
2670 if (isl_basic_map_check_range(bmap
, type
, first
, n
) < 0)
2671 return isl_basic_map_free(bmap
);
2672 first
+= isl_basic_map_offset(bmap
, type
);
2674 for (i
= bmap
->n_div
- 1; i
>= 0; --i
) {
2677 involves
= div_involves_vars(bmap
, i
, first
, n
);
2679 return isl_basic_map_free(bmap
);
2682 bmap
= insert_bounds_on_div(bmap
, i
);
2683 bmap
= isl_basic_map_remove_dims(bmap
, isl_dim_div
, i
, 1);
2692 __isl_give isl_basic_set
*isl_basic_set_remove_divs_involving_dims(
2693 __isl_take isl_basic_set
*bset
,
2694 enum isl_dim_type type
, unsigned first
, unsigned n
)
2696 return isl_basic_map_remove_divs_involving_dims(bset
, type
, first
, n
);
2699 __isl_give isl_map
*isl_map_remove_divs_involving_dims(__isl_take isl_map
*map
,
2700 enum isl_dim_type type
, unsigned first
, unsigned n
)
2709 map
= isl_map_cow(map
);
2713 for (i
= 0; i
< map
->n
; ++i
) {
2714 map
->p
[i
] = isl_basic_map_remove_divs_involving_dims(map
->p
[i
],
2725 __isl_give isl_set
*isl_set_remove_divs_involving_dims(__isl_take isl_set
*set
,
2726 enum isl_dim_type type
, unsigned first
, unsigned n
)
2728 return set_from_map(isl_map_remove_divs_involving_dims(set_to_map(set
),
2732 /* Does the description of "bmap" depend on the specified dimensions?
2733 * We also check whether the dimensions appear in any of the div definitions.
2734 * In principle there is no need for this check. If the dimensions appear
2735 * in a div definition, they also appear in the defining constraints of that
2738 isl_bool
isl_basic_map_involves_dims(__isl_keep isl_basic_map
*bmap
,
2739 enum isl_dim_type type
, unsigned first
, unsigned n
)
2743 if (isl_basic_map_check_range(bmap
, type
, first
, n
) < 0)
2744 return isl_bool_error
;
2746 first
+= isl_basic_map_offset(bmap
, type
);
2747 for (i
= 0; i
< bmap
->n_eq
; ++i
)
2748 if (isl_seq_first_non_zero(bmap
->eq
[i
] + first
, n
) >= 0)
2749 return isl_bool_true
;
2750 for (i
= 0; i
< bmap
->n_ineq
; ++i
)
2751 if (isl_seq_first_non_zero(bmap
->ineq
[i
] + first
, n
) >= 0)
2752 return isl_bool_true
;
2753 for (i
= 0; i
< bmap
->n_div
; ++i
) {
2754 if (isl_int_is_zero(bmap
->div
[i
][0]))
2756 if (isl_seq_first_non_zero(bmap
->div
[i
] + 1 + first
, n
) >= 0)
2757 return isl_bool_true
;
2760 return isl_bool_false
;
2763 isl_bool
isl_map_involves_dims(__isl_keep isl_map
*map
,
2764 enum isl_dim_type type
, unsigned first
, unsigned n
)
2769 return isl_bool_error
;
2771 if (first
+ n
> isl_map_dim(map
, type
))
2772 isl_die(map
->ctx
, isl_error_invalid
,
2773 "index out of bounds", return isl_bool_error
);
2775 for (i
= 0; i
< map
->n
; ++i
) {
2776 isl_bool involves
= isl_basic_map_involves_dims(map
->p
[i
],
2778 if (involves
< 0 || involves
)
2782 return isl_bool_false
;
2785 isl_bool
isl_basic_set_involves_dims(__isl_keep isl_basic_set
*bset
,
2786 enum isl_dim_type type
, unsigned first
, unsigned n
)
2788 return isl_basic_map_involves_dims(bset
, type
, first
, n
);
2791 isl_bool
isl_set_involves_dims(__isl_keep isl_set
*set
,
2792 enum isl_dim_type type
, unsigned first
, unsigned n
)
2794 return isl_map_involves_dims(set
, type
, first
, n
);
2797 /* Drop all constraints in bmap that involve any of the dimensions
2798 * first to first+n-1.
2800 static __isl_give isl_basic_map
*isl_basic_map_drop_constraints_involving(
2801 __isl_take isl_basic_map
*bmap
, unsigned first
, unsigned n
)
2808 bmap
= isl_basic_map_cow(bmap
);
2813 for (i
= bmap
->n_eq
- 1; i
>= 0; --i
) {
2814 if (isl_seq_first_non_zero(bmap
->eq
[i
] + 1 + first
, n
) == -1)
2816 isl_basic_map_drop_equality(bmap
, i
);
2819 for (i
= bmap
->n_ineq
- 1; i
>= 0; --i
) {
2820 if (isl_seq_first_non_zero(bmap
->ineq
[i
] + 1 + first
, n
) == -1)
2822 isl_basic_map_drop_inequality(bmap
, i
);
2825 bmap
= isl_basic_map_add_known_div_constraints(bmap
);
2829 /* Drop all constraints in bset that involve any of the dimensions
2830 * first to first+n-1.
2832 __isl_give isl_basic_set
*isl_basic_set_drop_constraints_involving(
2833 __isl_take isl_basic_set
*bset
, unsigned first
, unsigned n
)
2835 return isl_basic_map_drop_constraints_involving(bset
, first
, n
);
2838 /* Drop all constraints in bmap that do not involve any of the dimensions
2839 * first to first + n - 1 of the given type.
2841 __isl_give isl_basic_map
*isl_basic_map_drop_constraints_not_involving_dims(
2842 __isl_take isl_basic_map
*bmap
,
2843 enum isl_dim_type type
, unsigned first
, unsigned n
)
2848 isl_space
*space
= isl_basic_map_get_space(bmap
);
2849 isl_basic_map_free(bmap
);
2850 return isl_basic_map_universe(space
);
2852 bmap
= isl_basic_map_cow(bmap
);
2856 if (isl_basic_map_check_range(bmap
, type
, first
, n
) < 0)
2857 return isl_basic_map_free(bmap
);
2859 first
+= isl_basic_map_offset(bmap
, type
) - 1;
2861 for (i
= bmap
->n_eq
- 1; i
>= 0; --i
) {
2862 if (isl_seq_first_non_zero(bmap
->eq
[i
] + 1 + first
, n
) != -1)
2864 isl_basic_map_drop_equality(bmap
, i
);
2867 for (i
= bmap
->n_ineq
- 1; i
>= 0; --i
) {
2868 if (isl_seq_first_non_zero(bmap
->ineq
[i
] + 1 + first
, n
) != -1)
2870 isl_basic_map_drop_inequality(bmap
, i
);
2873 bmap
= isl_basic_map_add_known_div_constraints(bmap
);
2877 /* Drop all constraints in bset that do not involve any of the dimensions
2878 * first to first + n - 1 of the given type.
2880 __isl_give isl_basic_set
*isl_basic_set_drop_constraints_not_involving_dims(
2881 __isl_take isl_basic_set
*bset
,
2882 enum isl_dim_type type
, unsigned first
, unsigned n
)
2884 return isl_basic_map_drop_constraints_not_involving_dims(bset
,
2888 /* Drop all constraints in bmap that involve any of the dimensions
2889 * first to first + n - 1 of the given type.
2891 __isl_give isl_basic_map
*isl_basic_map_drop_constraints_involving_dims(
2892 __isl_take isl_basic_map
*bmap
,
2893 enum isl_dim_type type
, unsigned first
, unsigned n
)
2900 if (isl_basic_map_check_range(bmap
, type
, first
, n
) < 0)
2901 return isl_basic_map_free(bmap
);
2903 bmap
= isl_basic_map_remove_divs_involving_dims(bmap
, type
, first
, n
);
2904 first
+= isl_basic_map_offset(bmap
, type
) - 1;
2905 return isl_basic_map_drop_constraints_involving(bmap
, first
, n
);
2908 /* Drop all constraints in bset that involve any of the dimensions
2909 * first to first + n - 1 of the given type.
2911 __isl_give isl_basic_set
*isl_basic_set_drop_constraints_involving_dims(
2912 __isl_take isl_basic_set
*bset
,
2913 enum isl_dim_type type
, unsigned first
, unsigned n
)
2915 return isl_basic_map_drop_constraints_involving_dims(bset
,
2919 /* Drop constraints from "map" by applying "drop" to each basic map.
2921 static __isl_give isl_map
*drop_constraints(__isl_take isl_map
*map
,
2922 enum isl_dim_type type
, unsigned first
, unsigned n
,
2923 __isl_give isl_basic_map
*(*drop
)(__isl_take isl_basic_map
*bmap
,
2924 enum isl_dim_type type
, unsigned first
, unsigned n
))
2932 dim
= isl_map_dim(map
, type
);
2933 if (first
+ n
> dim
|| first
+ n
< first
)
2934 isl_die(isl_map_get_ctx(map
), isl_error_invalid
,
2935 "index out of bounds", return isl_map_free(map
));
2937 map
= isl_map_cow(map
);
2941 for (i
= 0; i
< map
->n
; ++i
) {
2942 map
->p
[i
] = drop(map
->p
[i
], type
, first
, n
);
2944 return isl_map_free(map
);
2948 ISL_F_CLR(map
, ISL_MAP_DISJOINT
);
2953 /* Drop all constraints in map that involve any of the dimensions
2954 * first to first + n - 1 of the given type.
2956 __isl_give isl_map
*isl_map_drop_constraints_involving_dims(
2957 __isl_take isl_map
*map
,
2958 enum isl_dim_type type
, unsigned first
, unsigned n
)
2962 return drop_constraints(map
, type
, first
, n
,
2963 &isl_basic_map_drop_constraints_involving_dims
);
2966 /* Drop all constraints in "map" that do not involve any of the dimensions
2967 * first to first + n - 1 of the given type.
2969 __isl_give isl_map
*isl_map_drop_constraints_not_involving_dims(
2970 __isl_take isl_map
*map
,
2971 enum isl_dim_type type
, unsigned first
, unsigned n
)
2974 isl_space
*space
= isl_map_get_space(map
);
2976 return isl_map_universe(space
);
2978 return drop_constraints(map
, type
, first
, n
,
2979 &isl_basic_map_drop_constraints_not_involving_dims
);
2982 /* Drop all constraints in set that involve any of the dimensions
2983 * first to first + n - 1 of the given type.
2985 __isl_give isl_set
*isl_set_drop_constraints_involving_dims(
2986 __isl_take isl_set
*set
,
2987 enum isl_dim_type type
, unsigned first
, unsigned n
)
2989 return isl_map_drop_constraints_involving_dims(set
, type
, first
, n
);
2992 /* Drop all constraints in "set" that do not involve any of the dimensions
2993 * first to first + n - 1 of the given type.
2995 __isl_give isl_set
*isl_set_drop_constraints_not_involving_dims(
2996 __isl_take isl_set
*set
,
2997 enum isl_dim_type type
, unsigned first
, unsigned n
)
2999 return isl_map_drop_constraints_not_involving_dims(set
, type
, first
, n
);
3002 /* Does local variable "div" of "bmap" have a complete explicit representation?
3003 * Having a complete explicit representation requires not only
3004 * an explicit representation, but also that all local variables
3005 * that appear in this explicit representation in turn have
3006 * a complete explicit representation.
3008 isl_bool
isl_basic_map_div_is_known(__isl_keep isl_basic_map
*bmap
, int div
)
3011 unsigned div_offset
= isl_basic_map_offset(bmap
, isl_dim_div
);
3014 marked
= isl_basic_map_div_is_marked_unknown(bmap
, div
);
3015 if (marked
< 0 || marked
)
3016 return isl_bool_not(marked
);
3018 for (i
= bmap
->n_div
- 1; i
>= 0; --i
) {
3021 if (isl_int_is_zero(bmap
->div
[div
][1 + div_offset
+ i
]))
3023 known
= isl_basic_map_div_is_known(bmap
, i
);
3024 if (known
< 0 || !known
)
3028 return isl_bool_true
;
3031 /* Remove all divs that are unknown or defined in terms of unknown divs.
3033 __isl_give isl_basic_map
*isl_basic_map_remove_unknown_divs(
3034 __isl_take isl_basic_map
*bmap
)
3041 for (i
= bmap
->n_div
- 1; i
>= 0; --i
) {
3042 if (isl_basic_map_div_is_known(bmap
, i
))
3044 bmap
= isl_basic_map_remove_dims(bmap
, isl_dim_div
, i
, 1);
3053 /* Remove all divs that are unknown or defined in terms of unknown divs.
3055 __isl_give isl_basic_set
*isl_basic_set_remove_unknown_divs(
3056 __isl_take isl_basic_set
*bset
)
3058 return isl_basic_map_remove_unknown_divs(bset
);
3061 __isl_give isl_map
*isl_map_remove_unknown_divs(__isl_take isl_map
*map
)
3070 map
= isl_map_cow(map
);
3074 for (i
= 0; i
< map
->n
; ++i
) {
3075 map
->p
[i
] = isl_basic_map_remove_unknown_divs(map
->p
[i
]);
3085 __isl_give isl_set
*isl_set_remove_unknown_divs(__isl_take isl_set
*set
)
3087 return set_from_map(isl_map_remove_unknown_divs(set_to_map(set
)));
3090 __isl_give isl_basic_set
*isl_basic_set_remove_dims(
3091 __isl_take isl_basic_set
*bset
,
3092 enum isl_dim_type type
, unsigned first
, unsigned n
)
3094 isl_basic_map
*bmap
= bset_to_bmap(bset
);
3095 bmap
= isl_basic_map_remove_dims(bmap
, type
, first
, n
);
3096 return bset_from_bmap(bmap
);
3099 struct isl_map
*isl_map_remove_dims(struct isl_map
*map
,
3100 enum isl_dim_type type
, unsigned first
, unsigned n
)
3107 map
= isl_map_cow(map
);
3110 isl_assert(map
->ctx
, first
+ n
<= isl_map_dim(map
, type
), goto error
);
3112 for (i
= 0; i
< map
->n
; ++i
) {
3113 map
->p
[i
] = isl_basic_map_eliminate_vars(map
->p
[i
],
3114 isl_basic_map_offset(map
->p
[i
], type
) - 1 + first
, n
);
3118 map
= isl_map_drop(map
, type
, first
, n
);
3125 __isl_give isl_set
*isl_set_remove_dims(__isl_take isl_set
*bset
,
3126 enum isl_dim_type type
, unsigned first
, unsigned n
)
3128 return set_from_map(isl_map_remove_dims(set_to_map(bset
),
3132 /* Project out n inputs starting at first using Fourier-Motzkin */
3133 struct isl_map
*isl_map_remove_inputs(struct isl_map
*map
,
3134 unsigned first
, unsigned n
)
3136 return isl_map_remove_dims(map
, isl_dim_in
, first
, n
);
3139 static void dump_term(struct isl_basic_map
*bmap
,
3140 isl_int c
, int pos
, FILE *out
)
3143 unsigned in
= isl_basic_map_dim(bmap
, isl_dim_in
);
3144 unsigned dim
= in
+ isl_basic_map_dim(bmap
, isl_dim_out
);
3145 unsigned nparam
= isl_basic_map_dim(bmap
, isl_dim_param
);
3147 isl_int_print(out
, c
, 0);
3149 if (!isl_int_is_one(c
))
3150 isl_int_print(out
, c
, 0);
3151 if (pos
< 1 + nparam
) {
3152 name
= isl_space_get_dim_name(bmap
->dim
,
3153 isl_dim_param
, pos
- 1);
3155 fprintf(out
, "%s", name
);
3157 fprintf(out
, "p%d", pos
- 1);
3158 } else if (pos
< 1 + nparam
+ in
)
3159 fprintf(out
, "i%d", pos
- 1 - nparam
);
3160 else if (pos
< 1 + nparam
+ dim
)
3161 fprintf(out
, "o%d", pos
- 1 - nparam
- in
);
3163 fprintf(out
, "e%d", pos
- 1 - nparam
- dim
);
3167 static void dump_constraint_sign(struct isl_basic_map
*bmap
, isl_int
*c
,
3168 int sign
, FILE *out
)
3172 unsigned len
= 1 + isl_basic_map_total_dim(bmap
);
3176 for (i
= 0, first
= 1; i
< len
; ++i
) {
3177 if (isl_int_sgn(c
[i
]) * sign
<= 0)
3180 fprintf(out
, " + ");
3182 isl_int_abs(v
, c
[i
]);
3183 dump_term(bmap
, v
, i
, out
);
3190 static void dump_constraint(struct isl_basic_map
*bmap
, isl_int
*c
,
3191 const char *op
, FILE *out
, int indent
)
3195 fprintf(out
, "%*s", indent
, "");
3197 dump_constraint_sign(bmap
, c
, 1, out
);
3198 fprintf(out
, " %s ", op
);
3199 dump_constraint_sign(bmap
, c
, -1, out
);
3203 for (i
= bmap
->n_div
; i
< bmap
->extra
; ++i
) {
3204 if (isl_int_is_zero(c
[1+isl_space_dim(bmap
->dim
, isl_dim_all
)+i
]))
3206 fprintf(out
, "%*s", indent
, "");
3207 fprintf(out
, "ERROR: unused div coefficient not zero\n");
3212 static void dump_constraints(struct isl_basic_map
*bmap
,
3213 isl_int
**c
, unsigned n
,
3214 const char *op
, FILE *out
, int indent
)
3218 for (i
= 0; i
< n
; ++i
)
3219 dump_constraint(bmap
, c
[i
], op
, out
, indent
);
3222 static void dump_affine(struct isl_basic_map
*bmap
, isl_int
*exp
, FILE *out
)
3226 unsigned total
= isl_basic_map_total_dim(bmap
);
3228 for (j
= 0; j
< 1 + total
; ++j
) {
3229 if (isl_int_is_zero(exp
[j
]))
3231 if (!first
&& isl_int_is_pos(exp
[j
]))
3233 dump_term(bmap
, exp
[j
], j
, out
);
3238 static void dump(struct isl_basic_map
*bmap
, FILE *out
, int indent
)
3242 dump_constraints(bmap
, bmap
->eq
, bmap
->n_eq
, "=", out
, indent
);
3243 dump_constraints(bmap
, bmap
->ineq
, bmap
->n_ineq
, ">=", out
, indent
);
3245 for (i
= 0; i
< bmap
->n_div
; ++i
) {
3246 fprintf(out
, "%*s", indent
, "");
3247 fprintf(out
, "e%d = [(", i
);
3248 dump_affine(bmap
, bmap
->div
[i
]+1, out
);
3250 isl_int_print(out
, bmap
->div
[i
][0], 0);
3251 fprintf(out
, "]\n");
3255 void isl_basic_set_print_internal(struct isl_basic_set
*bset
,
3256 FILE *out
, int indent
)
3259 fprintf(out
, "null basic set\n");
3263 fprintf(out
, "%*s", indent
, "");
3264 fprintf(out
, "ref: %d, nparam: %d, dim: %d, extra: %d, flags: %x\n",
3265 bset
->ref
, bset
->dim
->nparam
, bset
->dim
->n_out
,
3266 bset
->extra
, bset
->flags
);
3267 dump(bset_to_bmap(bset
), out
, indent
);
3270 void isl_basic_map_print_internal(struct isl_basic_map
*bmap
,
3271 FILE *out
, int indent
)
3274 fprintf(out
, "null basic map\n");
3278 fprintf(out
, "%*s", indent
, "");
3279 fprintf(out
, "ref: %d, nparam: %d, in: %d, out: %d, extra: %d, "
3280 "flags: %x, n_name: %d\n",
3282 bmap
->dim
->nparam
, bmap
->dim
->n_in
, bmap
->dim
->n_out
,
3283 bmap
->extra
, bmap
->flags
, bmap
->dim
->n_id
);
3284 dump(bmap
, out
, indent
);
3287 int isl_inequality_negate(struct isl_basic_map
*bmap
, unsigned pos
)
3292 total
= isl_basic_map_total_dim(bmap
);
3293 isl_assert(bmap
->ctx
, pos
< bmap
->n_ineq
, return -1);
3294 isl_seq_neg(bmap
->ineq
[pos
], bmap
->ineq
[pos
], 1 + total
);
3295 isl_int_sub_ui(bmap
->ineq
[pos
][0], bmap
->ineq
[pos
][0], 1);
3296 ISL_F_CLR(bmap
, ISL_BASIC_MAP_NORMALIZED
);
3300 __isl_give isl_set
*isl_set_alloc_space(__isl_take isl_space
*space
, int n
,
3305 if (isl_space_dim(space
, isl_dim_in
) != 0)
3306 isl_die(isl_space_get_ctx(space
), isl_error_invalid
,
3307 "set cannot have input dimensions", goto error
);
3308 return isl_map_alloc_space(space
, n
, flags
);
3310 isl_space_free(space
);
3314 /* Make sure "map" has room for at least "n" more basic maps.
3316 struct isl_map
*isl_map_grow(struct isl_map
*map
, int n
)
3319 struct isl_map
*grown
= NULL
;
3323 isl_assert(map
->ctx
, n
>= 0, goto error
);
3324 if (map
->n
+ n
<= map
->size
)
3326 grown
= isl_map_alloc_space(isl_map_get_space(map
), map
->n
+ n
, map
->flags
);
3329 for (i
= 0; i
< map
->n
; ++i
) {
3330 grown
->p
[i
] = isl_basic_map_copy(map
->p
[i
]);
3338 isl_map_free(grown
);
3343 /* Make sure "set" has room for at least "n" more basic sets.
3345 struct isl_set
*isl_set_grow(struct isl_set
*set
, int n
)
3347 return set_from_map(isl_map_grow(set_to_map(set
), n
));
3350 struct isl_set
*isl_set_from_basic_set(struct isl_basic_set
*bset
)
3352 return isl_map_from_basic_map(bset
);
3355 struct isl_map
*isl_map_from_basic_map(struct isl_basic_map
*bmap
)
3357 struct isl_map
*map
;
3362 map
= isl_map_alloc_space(isl_space_copy(bmap
->dim
), 1, ISL_MAP_DISJOINT
);
3363 return isl_map_add_basic_map(map
, bmap
);
3366 __isl_give isl_set
*isl_set_add_basic_set(__isl_take isl_set
*set
,
3367 __isl_take isl_basic_set
*bset
)
3369 return set_from_map(isl_map_add_basic_map(set_to_map(set
),
3370 bset_to_bmap(bset
)));
3373 __isl_null isl_set
*isl_set_free(__isl_take isl_set
*set
)
3375 return isl_map_free(set
);
3378 void isl_set_print_internal(struct isl_set
*set
, FILE *out
, int indent
)
3383 fprintf(out
, "null set\n");
3387 fprintf(out
, "%*s", indent
, "");
3388 fprintf(out
, "ref: %d, n: %d, nparam: %d, dim: %d, flags: %x\n",
3389 set
->ref
, set
->n
, set
->dim
->nparam
, set
->dim
->n_out
,
3391 for (i
= 0; i
< set
->n
; ++i
) {
3392 fprintf(out
, "%*s", indent
, "");
3393 fprintf(out
, "basic set %d:\n", i
);
3394 isl_basic_set_print_internal(set
->p
[i
], out
, indent
+4);
3398 void isl_map_print_internal(struct isl_map
*map
, FILE *out
, int indent
)
3403 fprintf(out
, "null map\n");
3407 fprintf(out
, "%*s", indent
, "");
3408 fprintf(out
, "ref: %d, n: %d, nparam: %d, in: %d, out: %d, "
3409 "flags: %x, n_name: %d\n",
3410 map
->ref
, map
->n
, map
->dim
->nparam
, map
->dim
->n_in
,
3411 map
->dim
->n_out
, map
->flags
, map
->dim
->n_id
);
3412 for (i
= 0; i
< map
->n
; ++i
) {
3413 fprintf(out
, "%*s", indent
, "");
3414 fprintf(out
, "basic map %d:\n", i
);
3415 isl_basic_map_print_internal(map
->p
[i
], out
, indent
+4);
3419 __isl_give isl_basic_map
*isl_basic_map_intersect_domain(
3420 __isl_take isl_basic_map
*bmap
, __isl_take isl_basic_set
*bset
)
3422 struct isl_basic_map
*bmap_domain
;
3424 if (isl_basic_map_check_equal_params(bmap
, bset_to_bmap(bset
)) < 0)
3427 if (isl_space_dim(bset
->dim
, isl_dim_set
) != 0)
3428 isl_assert(bset
->ctx
,
3429 isl_basic_map_compatible_domain(bmap
, bset
), goto error
);
3431 bmap
= isl_basic_map_cow(bmap
);
3434 bmap
= isl_basic_map_extend_space(bmap
, isl_space_copy(bmap
->dim
),
3435 bset
->n_div
, bset
->n_eq
, bset
->n_ineq
);
3436 bmap_domain
= isl_basic_map_from_domain(bset
);
3437 bmap
= add_constraints(bmap
, bmap_domain
, 0, 0);
3439 bmap
= isl_basic_map_simplify(bmap
);
3440 return isl_basic_map_finalize(bmap
);
3442 isl_basic_map_free(bmap
);
3443 isl_basic_set_free(bset
);
3447 /* Check that the space of "bset" is the same as that of the range of "bmap".
3449 static isl_stat
isl_basic_map_check_compatible_range(
3450 __isl_keep isl_basic_map
*bmap
, __isl_keep isl_basic_set
*bset
)
3454 ok
= isl_basic_map_compatible_range(bmap
, bset
);
3456 return isl_stat_error
;
3458 isl_die(isl_basic_set_get_ctx(bset
), isl_error_invalid
,
3459 "incompatible spaces", return isl_stat_error
);
3464 __isl_give isl_basic_map
*isl_basic_map_intersect_range(
3465 __isl_take isl_basic_map
*bmap
, __isl_take isl_basic_set
*bset
)
3467 struct isl_basic_map
*bmap_range
;
3469 if (isl_basic_map_check_equal_params(bmap
, bset_to_bmap(bset
)) < 0)
3472 if (isl_space_dim(bset
->dim
, isl_dim_set
) != 0 &&
3473 isl_basic_map_check_compatible_range(bmap
, bset
) < 0)
3476 if (isl_basic_set_plain_is_universe(bset
)) {
3477 isl_basic_set_free(bset
);
3481 bmap
= isl_basic_map_cow(bmap
);
3484 bmap
= isl_basic_map_extend_space(bmap
, isl_space_copy(bmap
->dim
),
3485 bset
->n_div
, bset
->n_eq
, bset
->n_ineq
);
3486 bmap_range
= bset_to_bmap(bset
);
3487 bmap
= add_constraints(bmap
, bmap_range
, 0, 0);
3489 bmap
= isl_basic_map_simplify(bmap
);
3490 return isl_basic_map_finalize(bmap
);
3492 isl_basic_map_free(bmap
);
3493 isl_basic_set_free(bset
);
3497 isl_bool
isl_basic_map_contains(__isl_keep isl_basic_map
*bmap
,
3498 __isl_keep isl_vec
*vec
)
3505 return isl_bool_error
;
3507 total
= 1 + isl_basic_map_total_dim(bmap
);
3508 if (total
!= vec
->size
)
3509 return isl_bool_false
;
3513 for (i
= 0; i
< bmap
->n_eq
; ++i
) {
3514 isl_seq_inner_product(vec
->el
, bmap
->eq
[i
], total
, &s
);
3515 if (!isl_int_is_zero(s
)) {
3517 return isl_bool_false
;
3521 for (i
= 0; i
< bmap
->n_ineq
; ++i
) {
3522 isl_seq_inner_product(vec
->el
, bmap
->ineq
[i
], total
, &s
);
3523 if (isl_int_is_neg(s
)) {
3525 return isl_bool_false
;
3531 return isl_bool_true
;
3534 isl_bool
isl_basic_set_contains(__isl_keep isl_basic_set
*bset
,
3535 __isl_keep isl_vec
*vec
)
3537 return isl_basic_map_contains(bset_to_bmap(bset
), vec
);
3540 struct isl_basic_map
*isl_basic_map_intersect(
3541 struct isl_basic_map
*bmap1
, struct isl_basic_map
*bmap2
)
3543 struct isl_vec
*sample
= NULL
;
3545 if (isl_basic_map_check_equal_params(bmap1
, bmap2
) < 0)
3547 if (isl_space_dim(bmap1
->dim
, isl_dim_all
) ==
3548 isl_space_dim(bmap1
->dim
, isl_dim_param
) &&
3549 isl_space_dim(bmap2
->dim
, isl_dim_all
) !=
3550 isl_space_dim(bmap2
->dim
, isl_dim_param
))
3551 return isl_basic_map_intersect(bmap2
, bmap1
);
3553 if (isl_space_dim(bmap2
->dim
, isl_dim_all
) !=
3554 isl_space_dim(bmap2
->dim
, isl_dim_param
))
3555 isl_assert(bmap1
->ctx
,
3556 isl_space_is_equal(bmap1
->dim
, bmap2
->dim
), goto error
);
3558 if (isl_basic_map_plain_is_empty(bmap1
)) {
3559 isl_basic_map_free(bmap2
);
3562 if (isl_basic_map_plain_is_empty(bmap2
)) {
3563 isl_basic_map_free(bmap1
);
3567 if (bmap1
->sample
&&
3568 isl_basic_map_contains(bmap1
, bmap1
->sample
) > 0 &&
3569 isl_basic_map_contains(bmap2
, bmap1
->sample
) > 0)
3570 sample
= isl_vec_copy(bmap1
->sample
);
3571 else if (bmap2
->sample
&&
3572 isl_basic_map_contains(bmap1
, bmap2
->sample
) > 0 &&
3573 isl_basic_map_contains(bmap2
, bmap2
->sample
) > 0)
3574 sample
= isl_vec_copy(bmap2
->sample
);
3576 bmap1
= isl_basic_map_cow(bmap1
);
3579 bmap1
= isl_basic_map_extend_space(bmap1
, isl_space_copy(bmap1
->dim
),
3580 bmap2
->n_div
, bmap2
->n_eq
, bmap2
->n_ineq
);
3581 bmap1
= add_constraints(bmap1
, bmap2
, 0, 0);
3584 isl_vec_free(sample
);
3586 isl_vec_free(bmap1
->sample
);
3587 bmap1
->sample
= sample
;
3590 bmap1
= isl_basic_map_simplify(bmap1
);
3591 return isl_basic_map_finalize(bmap1
);
3594 isl_vec_free(sample
);
3595 isl_basic_map_free(bmap1
);
3596 isl_basic_map_free(bmap2
);
3600 struct isl_basic_set
*isl_basic_set_intersect(
3601 struct isl_basic_set
*bset1
, struct isl_basic_set
*bset2
)
3603 return bset_from_bmap(isl_basic_map_intersect(bset_to_bmap(bset1
),
3604 bset_to_bmap(bset2
)));
3607 __isl_give isl_basic_set
*isl_basic_set_intersect_params(
3608 __isl_take isl_basic_set
*bset1
, __isl_take isl_basic_set
*bset2
)
3610 return isl_basic_set_intersect(bset1
, bset2
);
3613 /* Special case of isl_map_intersect, where both map1 and map2
3614 * are convex, without any divs and such that either map1 or map2
3615 * contains a single constraint. This constraint is then simply
3616 * added to the other map.
3618 static __isl_give isl_map
*map_intersect_add_constraint(
3619 __isl_take isl_map
*map1
, __isl_take isl_map
*map2
)
3621 isl_assert(map1
->ctx
, map1
->n
== 1, goto error
);
3622 isl_assert(map2
->ctx
, map1
->n
== 1, goto error
);
3623 isl_assert(map1
->ctx
, map1
->p
[0]->n_div
== 0, goto error
);
3624 isl_assert(map2
->ctx
, map1
->p
[0]->n_div
== 0, goto error
);
3626 if (map2
->p
[0]->n_eq
+ map2
->p
[0]->n_ineq
!= 1)
3627 return isl_map_intersect(map2
, map1
);
3629 map1
= isl_map_cow(map1
);
3632 if (isl_map_plain_is_empty(map1
)) {
3636 map1
->p
[0] = isl_basic_map_cow(map1
->p
[0]);
3637 if (map2
->p
[0]->n_eq
== 1)
3638 map1
->p
[0] = isl_basic_map_add_eq(map1
->p
[0], map2
->p
[0]->eq
[0]);
3640 map1
->p
[0] = isl_basic_map_add_ineq(map1
->p
[0],
3641 map2
->p
[0]->ineq
[0]);
3643 map1
->p
[0] = isl_basic_map_simplify(map1
->p
[0]);
3644 map1
->p
[0] = isl_basic_map_finalize(map1
->p
[0]);
3648 if (isl_basic_map_plain_is_empty(map1
->p
[0])) {
3649 isl_basic_map_free(map1
->p
[0]);
3662 /* map2 may be either a parameter domain or a map living in the same
3665 static __isl_give isl_map
*map_intersect_internal(__isl_take isl_map
*map1
,
3666 __isl_take isl_map
*map2
)
3675 if ((isl_map_plain_is_empty(map1
) ||
3676 isl_map_plain_is_universe(map2
)) &&
3677 isl_space_is_equal(map1
->dim
, map2
->dim
)) {
3681 if ((isl_map_plain_is_empty(map2
) ||
3682 isl_map_plain_is_universe(map1
)) &&
3683 isl_space_is_equal(map1
->dim
, map2
->dim
)) {
3688 if (map1
->n
== 1 && map2
->n
== 1 &&
3689 map1
->p
[0]->n_div
== 0 && map2
->p
[0]->n_div
== 0 &&
3690 isl_space_is_equal(map1
->dim
, map2
->dim
) &&
3691 (map1
->p
[0]->n_eq
+ map1
->p
[0]->n_ineq
== 1 ||
3692 map2
->p
[0]->n_eq
+ map2
->p
[0]->n_ineq
== 1))
3693 return map_intersect_add_constraint(map1
, map2
);
3695 if (isl_space_dim(map2
->dim
, isl_dim_all
) !=
3696 isl_space_dim(map2
->dim
, isl_dim_param
))
3697 isl_assert(map1
->ctx
,
3698 isl_space_is_equal(map1
->dim
, map2
->dim
), goto error
);
3700 if (ISL_F_ISSET(map1
, ISL_MAP_DISJOINT
) &&
3701 ISL_F_ISSET(map2
, ISL_MAP_DISJOINT
))
3702 ISL_FL_SET(flags
, ISL_MAP_DISJOINT
);
3704 result
= isl_map_alloc_space(isl_space_copy(map1
->dim
),
3705 map1
->n
* map2
->n
, flags
);
3708 for (i
= 0; i
< map1
->n
; ++i
)
3709 for (j
= 0; j
< map2
->n
; ++j
) {
3710 struct isl_basic_map
*part
;
3711 part
= isl_basic_map_intersect(
3712 isl_basic_map_copy(map1
->p
[i
]),
3713 isl_basic_map_copy(map2
->p
[j
]));
3714 if (isl_basic_map_is_empty(part
) < 0)
3715 part
= isl_basic_map_free(part
);
3716 result
= isl_map_add_basic_map(result
, part
);
3729 static __isl_give isl_map
*map_intersect(__isl_take isl_map
*map1
,
3730 __isl_take isl_map
*map2
)
3734 if (!isl_space_is_equal(map1
->dim
, map2
->dim
))
3735 isl_die(isl_map_get_ctx(map1
), isl_error_invalid
,
3736 "spaces don't match", goto error
);
3737 return map_intersect_internal(map1
, map2
);
3744 __isl_give isl_map
*isl_map_intersect(__isl_take isl_map
*map1
,
3745 __isl_take isl_map
*map2
)
3747 return isl_map_align_params_map_map_and(map1
, map2
, &map_intersect
);
3750 struct isl_set
*isl_set_intersect(struct isl_set
*set1
, struct isl_set
*set2
)
3752 return set_from_map(isl_map_intersect(set_to_map(set1
),
3756 /* map_intersect_internal accepts intersections
3757 * with parameter domains, so we can just call that function.
3759 static __isl_give isl_map
*map_intersect_params(__isl_take isl_map
*map
,
3760 __isl_take isl_set
*params
)
3762 return map_intersect_internal(map
, params
);
3765 __isl_give isl_map
*isl_map_intersect_params(__isl_take isl_map
*map1
,
3766 __isl_take isl_map
*map2
)
3768 return isl_map_align_params_map_map_and(map1
, map2
, &map_intersect_params
);
3771 __isl_give isl_set
*isl_set_intersect_params(__isl_take isl_set
*set
,
3772 __isl_take isl_set
*params
)
3774 return isl_map_intersect_params(set
, params
);
3777 struct isl_basic_map
*isl_basic_map_reverse(struct isl_basic_map
*bmap
)
3780 unsigned pos
, n1
, n2
;
3784 bmap
= isl_basic_map_cow(bmap
);
3787 space
= isl_space_reverse(isl_space_copy(bmap
->dim
));
3788 pos
= isl_basic_map_offset(bmap
, isl_dim_in
);
3789 n1
= isl_basic_map_dim(bmap
, isl_dim_in
);
3790 n2
= isl_basic_map_dim(bmap
, isl_dim_out
);
3791 bmap
= isl_basic_map_swap_vars(bmap
, pos
, n1
, n2
);
3792 return isl_basic_map_reset_space(bmap
, space
);
3795 static __isl_give isl_basic_map
*basic_map_space_reset(
3796 __isl_take isl_basic_map
*bmap
, enum isl_dim_type type
)
3802 if (!isl_space_is_named_or_nested(bmap
->dim
, type
))
3805 space
= isl_basic_map_get_space(bmap
);
3806 space
= isl_space_reset(space
, type
);
3807 bmap
= isl_basic_map_reset_space(bmap
, space
);
3811 __isl_give isl_basic_map
*isl_basic_map_insert_dims(
3812 __isl_take isl_basic_map
*bmap
, enum isl_dim_type type
,
3813 unsigned pos
, unsigned n
)
3817 struct isl_basic_map
*res
;
3818 struct isl_dim_map
*dim_map
;
3819 unsigned total
, off
;
3820 enum isl_dim_type t
;
3823 return basic_map_space_reset(bmap
, type
);
3828 res_dim
= isl_space_insert_dims(isl_basic_map_get_space(bmap
), type
, pos
, n
);
3830 total
= isl_basic_map_total_dim(bmap
) + n
;
3831 dim_map
= isl_dim_map_alloc(bmap
->ctx
, total
);
3833 for (t
= isl_dim_param
; t
<= isl_dim_out
; ++t
) {
3835 isl_dim_map_dim(dim_map
, bmap
->dim
, t
, off
);
3837 unsigned size
= isl_basic_map_dim(bmap
, t
);
3838 isl_dim_map_dim_range(dim_map
, bmap
->dim
, t
,
3840 isl_dim_map_dim_range(dim_map
, bmap
->dim
, t
,
3841 pos
, size
- pos
, off
+ pos
+ n
);
3843 off
+= isl_space_dim(res_dim
, t
);
3845 isl_dim_map_div(dim_map
, bmap
, off
);
3847 res
= isl_basic_map_alloc_space(res_dim
,
3848 bmap
->n_div
, bmap
->n_eq
, bmap
->n_ineq
);
3849 rational
= isl_basic_map_is_rational(bmap
);
3851 res
= isl_basic_map_free(res
);
3853 res
= isl_basic_map_set_rational(res
);
3854 if (isl_basic_map_plain_is_empty(bmap
)) {
3855 isl_basic_map_free(bmap
);
3857 return isl_basic_map_set_to_empty(res
);
3859 res
= isl_basic_map_add_constraints_dim_map(res
, bmap
, dim_map
);
3860 return isl_basic_map_finalize(res
);
3863 __isl_give isl_basic_set
*isl_basic_set_insert_dims(
3864 __isl_take isl_basic_set
*bset
,
3865 enum isl_dim_type type
, unsigned pos
, unsigned n
)
3867 return isl_basic_map_insert_dims(bset
, type
, pos
, n
);
3870 __isl_give isl_basic_map
*isl_basic_map_add_dims(__isl_take isl_basic_map
*bmap
,
3871 enum isl_dim_type type
, unsigned n
)
3875 return isl_basic_map_insert_dims(bmap
, type
,
3876 isl_basic_map_dim(bmap
, type
), n
);
3879 __isl_give isl_basic_set
*isl_basic_set_add_dims(__isl_take isl_basic_set
*bset
,
3880 enum isl_dim_type type
, unsigned n
)
3884 isl_assert(bset
->ctx
, type
!= isl_dim_in
, goto error
);
3885 return isl_basic_map_add_dims(bset
, type
, n
);
3887 isl_basic_set_free(bset
);
3891 static __isl_give isl_map
*map_space_reset(__isl_take isl_map
*map
,
3892 enum isl_dim_type type
)
3896 if (!map
|| !isl_space_is_named_or_nested(map
->dim
, type
))
3899 space
= isl_map_get_space(map
);
3900 space
= isl_space_reset(space
, type
);
3901 map
= isl_map_reset_space(map
, space
);
3905 __isl_give isl_map
*isl_map_insert_dims(__isl_take isl_map
*map
,
3906 enum isl_dim_type type
, unsigned pos
, unsigned n
)
3911 return map_space_reset(map
, type
);
3913 map
= isl_map_cow(map
);
3917 map
->dim
= isl_space_insert_dims(map
->dim
, type
, pos
, n
);
3921 for (i
= 0; i
< map
->n
; ++i
) {
3922 map
->p
[i
] = isl_basic_map_insert_dims(map
->p
[i
], type
, pos
, n
);
3933 __isl_give isl_set
*isl_set_insert_dims(__isl_take isl_set
*set
,
3934 enum isl_dim_type type
, unsigned pos
, unsigned n
)
3936 return isl_map_insert_dims(set
, type
, pos
, n
);
3939 __isl_give isl_map
*isl_map_add_dims(__isl_take isl_map
*map
,
3940 enum isl_dim_type type
, unsigned n
)
3944 return isl_map_insert_dims(map
, type
, isl_map_dim(map
, type
), n
);
3947 __isl_give isl_set
*isl_set_add_dims(__isl_take isl_set
*set
,
3948 enum isl_dim_type type
, unsigned n
)
3952 isl_assert(set
->ctx
, type
!= isl_dim_in
, goto error
);
3953 return set_from_map(isl_map_add_dims(set_to_map(set
), type
, n
));
3959 __isl_give isl_basic_map
*isl_basic_map_move_dims(
3960 __isl_take isl_basic_map
*bmap
,
3961 enum isl_dim_type dst_type
, unsigned dst_pos
,
3962 enum isl_dim_type src_type
, unsigned src_pos
, unsigned n
)
3964 struct isl_dim_map
*dim_map
;
3965 struct isl_basic_map
*res
;
3966 enum isl_dim_type t
;
3967 unsigned total
, off
;
3972 bmap
= isl_basic_map_reset(bmap
, src_type
);
3973 bmap
= isl_basic_map_reset(bmap
, dst_type
);
3977 if (isl_basic_map_check_range(bmap
, src_type
, src_pos
, n
) < 0)
3978 return isl_basic_map_free(bmap
);
3980 if (dst_type
== src_type
&& dst_pos
== src_pos
)
3983 isl_assert(bmap
->ctx
, dst_type
!= src_type
, goto error
);
3985 if (pos(bmap
->dim
, dst_type
) + dst_pos
==
3986 pos(bmap
->dim
, src_type
) + src_pos
+
3987 ((src_type
< dst_type
) ? n
: 0)) {
3988 bmap
= isl_basic_map_cow(bmap
);
3992 bmap
->dim
= isl_space_move_dims(bmap
->dim
, dst_type
, dst_pos
,
3993 src_type
, src_pos
, n
);
3997 bmap
= isl_basic_map_finalize(bmap
);
4002 total
= isl_basic_map_total_dim(bmap
);
4003 dim_map
= isl_dim_map_alloc(bmap
->ctx
, total
);
4006 for (t
= isl_dim_param
; t
<= isl_dim_out
; ++t
) {
4007 unsigned size
= isl_space_dim(bmap
->dim
, t
);
4008 if (t
== dst_type
) {
4009 isl_dim_map_dim_range(dim_map
, bmap
->dim
, t
,
4012 isl_dim_map_dim_range(dim_map
, bmap
->dim
, src_type
,
4015 isl_dim_map_dim_range(dim_map
, bmap
->dim
, t
,
4016 dst_pos
, size
- dst_pos
, off
);
4017 off
+= size
- dst_pos
;
4018 } else if (t
== src_type
) {
4019 isl_dim_map_dim_range(dim_map
, bmap
->dim
, t
,
4022 isl_dim_map_dim_range(dim_map
, bmap
->dim
, t
,
4023 src_pos
+ n
, size
- src_pos
- n
, off
);
4024 off
+= size
- src_pos
- n
;
4026 isl_dim_map_dim(dim_map
, bmap
->dim
, t
, off
);
4030 isl_dim_map_div(dim_map
, bmap
, off
);
4032 res
= isl_basic_map_alloc_space(isl_basic_map_get_space(bmap
),
4033 bmap
->n_div
, bmap
->n_eq
, bmap
->n_ineq
);
4034 bmap
= isl_basic_map_add_constraints_dim_map(res
, bmap
, dim_map
);
4038 bmap
->dim
= isl_space_move_dims(bmap
->dim
, dst_type
, dst_pos
,
4039 src_type
, src_pos
, n
);
4043 ISL_F_CLR(bmap
, ISL_BASIC_MAP_NORMALIZED
);
4044 bmap
= isl_basic_map_gauss(bmap
, NULL
);
4045 bmap
= isl_basic_map_finalize(bmap
);
4049 isl_basic_map_free(bmap
);
4053 __isl_give isl_basic_set
*isl_basic_set_move_dims(__isl_take isl_basic_set
*bset
,
4054 enum isl_dim_type dst_type
, unsigned dst_pos
,
4055 enum isl_dim_type src_type
, unsigned src_pos
, unsigned n
)
4057 isl_basic_map
*bmap
= bset_to_bmap(bset
);
4058 bmap
= isl_basic_map_move_dims(bmap
, dst_type
, dst_pos
,
4059 src_type
, src_pos
, n
);
4060 return bset_from_bmap(bmap
);
4063 __isl_give isl_set
*isl_set_move_dims(__isl_take isl_set
*set
,
4064 enum isl_dim_type dst_type
, unsigned dst_pos
,
4065 enum isl_dim_type src_type
, unsigned src_pos
, unsigned n
)
4069 isl_assert(set
->ctx
, dst_type
!= isl_dim_in
, goto error
);
4070 return set_from_map(isl_map_move_dims(set_to_map(set
),
4071 dst_type
, dst_pos
, src_type
, src_pos
, n
));
4077 __isl_give isl_map
*isl_map_move_dims(__isl_take isl_map
*map
,
4078 enum isl_dim_type dst_type
, unsigned dst_pos
,
4079 enum isl_dim_type src_type
, unsigned src_pos
, unsigned n
)
4086 map
= isl_map_reset(map
, src_type
);
4087 map
= isl_map_reset(map
, dst_type
);
4091 isl_assert(map
->ctx
, src_pos
+ n
<= isl_map_dim(map
, src_type
),
4094 if (dst_type
== src_type
&& dst_pos
== src_pos
)
4097 isl_assert(map
->ctx
, dst_type
!= src_type
, goto error
);
4099 map
= isl_map_cow(map
);
4103 map
->dim
= isl_space_move_dims(map
->dim
, dst_type
, dst_pos
, src_type
, src_pos
, n
);
4107 for (i
= 0; i
< map
->n
; ++i
) {
4108 map
->p
[i
] = isl_basic_map_move_dims(map
->p
[i
],
4110 src_type
, src_pos
, n
);
4121 /* Move the specified dimensions to the last columns right before
4122 * the divs. Don't change the dimension specification of bmap.
4123 * That's the responsibility of the caller.
4125 static __isl_give isl_basic_map
*move_last(__isl_take isl_basic_map
*bmap
,
4126 enum isl_dim_type type
, unsigned first
, unsigned n
)
4128 struct isl_dim_map
*dim_map
;
4129 struct isl_basic_map
*res
;
4130 enum isl_dim_type t
;
4131 unsigned total
, off
;
4135 if (pos(bmap
->dim
, type
) + first
+ n
==
4136 1 + isl_space_dim(bmap
->dim
, isl_dim_all
))
4139 total
= isl_basic_map_total_dim(bmap
);
4140 dim_map
= isl_dim_map_alloc(bmap
->ctx
, total
);
4143 for (t
= isl_dim_param
; t
<= isl_dim_out
; ++t
) {
4144 unsigned size
= isl_space_dim(bmap
->dim
, t
);
4146 isl_dim_map_dim_range(dim_map
, bmap
->dim
, t
,
4149 isl_dim_map_dim_range(dim_map
, bmap
->dim
, t
,
4150 first
, n
, total
- bmap
->n_div
- n
);
4151 isl_dim_map_dim_range(dim_map
, bmap
->dim
, t
,
4152 first
+ n
, size
- (first
+ n
), off
);
4153 off
+= size
- (first
+ n
);
4155 isl_dim_map_dim(dim_map
, bmap
->dim
, t
, off
);
4159 isl_dim_map_div(dim_map
, bmap
, off
+ n
);
4161 res
= isl_basic_map_alloc_space(isl_basic_map_get_space(bmap
),
4162 bmap
->n_div
, bmap
->n_eq
, bmap
->n_ineq
);
4163 res
= isl_basic_map_add_constraints_dim_map(res
, bmap
, dim_map
);
4167 /* Insert "n" rows in the divs of "bmap".
4169 * The number of columns is not changed, which means that the last
4170 * dimensions of "bmap" are being reintepreted as the new divs.
4171 * The space of "bmap" is not adjusted, however, which means
4172 * that "bmap" is left in an inconsistent state. Removing "n" dimensions
4173 * from the space of "bmap" is the responsibility of the caller.
4175 static __isl_give isl_basic_map
*insert_div_rows(__isl_take isl_basic_map
*bmap
,
4183 bmap
= isl_basic_map_cow(bmap
);
4187 row_size
= 1 + isl_space_dim(bmap
->dim
, isl_dim_all
) + bmap
->extra
;
4188 old
= bmap
->block2
.data
;
4189 bmap
->block2
= isl_blk_extend(bmap
->ctx
, bmap
->block2
,
4190 (bmap
->extra
+ n
) * (1 + row_size
));
4191 if (!bmap
->block2
.data
)
4192 return isl_basic_map_free(bmap
);
4193 new_div
= isl_alloc_array(bmap
->ctx
, isl_int
*, bmap
->extra
+ n
);
4195 return isl_basic_map_free(bmap
);
4196 for (i
= 0; i
< n
; ++i
) {
4197 new_div
[i
] = bmap
->block2
.data
+
4198 (bmap
->extra
+ i
) * (1 + row_size
);
4199 isl_seq_clr(new_div
[i
], 1 + row_size
);
4201 for (i
= 0; i
< bmap
->extra
; ++i
)
4202 new_div
[n
+ i
] = bmap
->block2
.data
+ (bmap
->div
[i
] - old
);
4204 bmap
->div
= new_div
;
4211 /* Drop constraints from "bmap" that only involve the variables
4212 * of "type" in the range [first, first + n] that are not related
4213 * to any of the variables outside that interval.
4214 * These constraints cannot influence the values for the variables
4215 * outside the interval, except in case they cause "bmap" to be empty.
4216 * Only drop the constraints if "bmap" is known to be non-empty.
4218 static __isl_give isl_basic_map
*drop_irrelevant_constraints(
4219 __isl_take isl_basic_map
*bmap
, enum isl_dim_type type
,
4220 unsigned first
, unsigned n
)
4224 unsigned dim
, n_div
;
4227 non_empty
= isl_basic_map_plain_is_non_empty(bmap
);
4229 return isl_basic_map_free(bmap
);
4233 dim
= isl_basic_map_dim(bmap
, isl_dim_all
);
4234 n_div
= isl_basic_map_dim(bmap
, isl_dim_div
);
4235 groups
= isl_calloc_array(isl_basic_map_get_ctx(bmap
), int, dim
);
4237 return isl_basic_map_free(bmap
);
4238 first
+= isl_basic_map_offset(bmap
, type
) - 1;
4239 for (i
= 0; i
< first
; ++i
)
4241 for (i
= first
+ n
; i
< dim
- n_div
; ++i
)
4244 bmap
= isl_basic_map_drop_unrelated_constraints(bmap
, groups
);
4249 /* Turn the n dimensions of type type, starting at first
4250 * into existentially quantified variables.
4252 * If a subset of the projected out variables are unrelated
4253 * to any of the variables that remain, then the constraints
4254 * involving this subset are simply dropped first.
4256 __isl_give isl_basic_map
*isl_basic_map_project_out(
4257 __isl_take isl_basic_map
*bmap
,
4258 enum isl_dim_type type
, unsigned first
, unsigned n
)
4263 return basic_map_space_reset(bmap
, type
);
4264 if (type
== isl_dim_div
)
4265 isl_die(isl_basic_map_get_ctx(bmap
), isl_error_invalid
,
4266 "cannot project out existentially quantified variables",
4267 return isl_basic_map_free(bmap
));
4269 empty
= isl_basic_map_plain_is_empty(bmap
);
4271 return isl_basic_map_free(bmap
);
4273 bmap
= isl_basic_map_set_to_empty(bmap
);
4275 bmap
= drop_irrelevant_constraints(bmap
, type
, first
, n
);
4279 if (ISL_F_ISSET(bmap
, ISL_BASIC_MAP_RATIONAL
))
4280 return isl_basic_map_remove_dims(bmap
, type
, first
, n
);
4282 if (isl_basic_map_check_range(bmap
, type
, first
, n
) < 0)
4283 return isl_basic_map_free(bmap
);
4285 bmap
= move_last(bmap
, type
, first
, n
);
4286 bmap
= isl_basic_map_cow(bmap
);
4287 bmap
= insert_div_rows(bmap
, n
);
4291 bmap
->dim
= isl_space_drop_dims(bmap
->dim
, type
, first
, n
);
4294 bmap
= isl_basic_map_simplify(bmap
);
4295 bmap
= isl_basic_map_drop_redundant_divs(bmap
);
4296 return isl_basic_map_finalize(bmap
);
4298 isl_basic_map_free(bmap
);
4302 /* Turn the n dimensions of type type, starting at first
4303 * into existentially quantified variables.
4305 struct isl_basic_set
*isl_basic_set_project_out(struct isl_basic_set
*bset
,
4306 enum isl_dim_type type
, unsigned first
, unsigned n
)
4308 return bset_from_bmap(isl_basic_map_project_out(bset_to_bmap(bset
),
4312 /* Turn the n dimensions of type type, starting at first
4313 * into existentially quantified variables.
4315 __isl_give isl_map
*isl_map_project_out(__isl_take isl_map
*map
,
4316 enum isl_dim_type type
, unsigned first
, unsigned n
)
4324 return map_space_reset(map
, type
);
4326 isl_assert(map
->ctx
, first
+ n
<= isl_map_dim(map
, type
), goto error
);
4328 map
= isl_map_cow(map
);
4332 map
->dim
= isl_space_drop_dims(map
->dim
, type
, first
, n
);
4336 for (i
= 0; i
< map
->n
; ++i
) {
4337 map
->p
[i
] = isl_basic_map_project_out(map
->p
[i
], type
, first
, n
);
4348 /* Turn the n dimensions of type type, starting at first
4349 * into existentially quantified variables.
4351 __isl_give isl_set
*isl_set_project_out(__isl_take isl_set
*set
,
4352 enum isl_dim_type type
, unsigned first
, unsigned n
)
4354 return set_from_map(isl_map_project_out(set_to_map(set
),
4358 /* Return a map that projects the elements in "set" onto their
4359 * "n" set dimensions starting at "first".
4360 * "type" should be equal to isl_dim_set.
4362 __isl_give isl_map
*isl_set_project_onto_map(__isl_take isl_set
*set
,
4363 enum isl_dim_type type
, unsigned first
, unsigned n
)
4371 if (type
!= isl_dim_set
)
4372 isl_die(isl_set_get_ctx(set
), isl_error_invalid
,
4373 "only set dimensions can be projected out", goto error
);
4374 dim
= isl_set_dim(set
, isl_dim_set
);
4375 if (first
+ n
> dim
|| first
+ n
< first
)
4376 isl_die(isl_set_get_ctx(set
), isl_error_invalid
,
4377 "index out of bounds", goto error
);
4379 map
= isl_map_from_domain(set
);
4380 map
= isl_map_add_dims(map
, isl_dim_out
, n
);
4381 for (i
= 0; i
< n
; ++i
)
4382 map
= isl_map_equate(map
, isl_dim_in
, first
+ i
,
4390 static struct isl_basic_map
*add_divs(struct isl_basic_map
*bmap
, unsigned n
)
4394 for (i
= 0; i
< n
; ++i
) {
4395 j
= isl_basic_map_alloc_div(bmap
);
4398 isl_seq_clr(bmap
->div
[j
], 1+1+isl_basic_map_total_dim(bmap
));
4402 isl_basic_map_free(bmap
);
4406 struct isl_basic_map
*isl_basic_map_apply_range(
4407 struct isl_basic_map
*bmap1
, struct isl_basic_map
*bmap2
)
4409 isl_space
*dim_result
= NULL
;
4410 struct isl_basic_map
*bmap
;
4411 unsigned n_in
, n_out
, n
, nparam
, total
, pos
;
4412 struct isl_dim_map
*dim_map1
, *dim_map2
;
4414 if (isl_basic_map_check_equal_params(bmap1
, bmap2
) < 0)
4416 if (!isl_space_tuple_is_equal(bmap1
->dim
, isl_dim_out
,
4417 bmap2
->dim
, isl_dim_in
))
4418 isl_die(isl_basic_map_get_ctx(bmap1
), isl_error_invalid
,
4419 "spaces don't match", goto error
);
4421 dim_result
= isl_space_join(isl_space_copy(bmap1
->dim
),
4422 isl_space_copy(bmap2
->dim
));
4424 n_in
= isl_basic_map_dim(bmap1
, isl_dim_in
);
4425 n_out
= isl_basic_map_dim(bmap2
, isl_dim_out
);
4426 n
= isl_basic_map_dim(bmap1
, isl_dim_out
);
4427 nparam
= isl_basic_map_dim(bmap1
, isl_dim_param
);
4429 total
= nparam
+ n_in
+ n_out
+ bmap1
->n_div
+ bmap2
->n_div
+ n
;
4430 dim_map1
= isl_dim_map_alloc(bmap1
->ctx
, total
);
4431 dim_map2
= isl_dim_map_alloc(bmap1
->ctx
, total
);
4432 isl_dim_map_dim(dim_map1
, bmap1
->dim
, isl_dim_param
, pos
= 0);
4433 isl_dim_map_dim(dim_map2
, bmap2
->dim
, isl_dim_param
, pos
= 0);
4434 isl_dim_map_dim(dim_map1
, bmap1
->dim
, isl_dim_in
, pos
+= nparam
);
4435 isl_dim_map_dim(dim_map2
, bmap2
->dim
, isl_dim_out
, pos
+= n_in
);
4436 isl_dim_map_div(dim_map1
, bmap1
, pos
+= n_out
);
4437 isl_dim_map_div(dim_map2
, bmap2
, pos
+= bmap1
->n_div
);
4438 isl_dim_map_dim(dim_map1
, bmap1
->dim
, isl_dim_out
, pos
+= bmap2
->n_div
);
4439 isl_dim_map_dim(dim_map2
, bmap2
->dim
, isl_dim_in
, pos
);
4441 bmap
= isl_basic_map_alloc_space(dim_result
,
4442 bmap1
->n_div
+ bmap2
->n_div
+ n
,
4443 bmap1
->n_eq
+ bmap2
->n_eq
,
4444 bmap1
->n_ineq
+ bmap2
->n_ineq
);
4445 bmap
= isl_basic_map_add_constraints_dim_map(bmap
, bmap1
, dim_map1
);
4446 bmap
= isl_basic_map_add_constraints_dim_map(bmap
, bmap2
, dim_map2
);
4447 bmap
= add_divs(bmap
, n
);
4448 bmap
= isl_basic_map_simplify(bmap
);
4449 bmap
= isl_basic_map_drop_redundant_divs(bmap
);
4450 return isl_basic_map_finalize(bmap
);
4452 isl_basic_map_free(bmap1
);
4453 isl_basic_map_free(bmap2
);
4457 struct isl_basic_set
*isl_basic_set_apply(
4458 struct isl_basic_set
*bset
, struct isl_basic_map
*bmap
)
4463 isl_assert(bset
->ctx
, isl_basic_map_compatible_domain(bmap
, bset
),
4466 return bset_from_bmap(isl_basic_map_apply_range(bset_to_bmap(bset
),
4469 isl_basic_set_free(bset
);
4470 isl_basic_map_free(bmap
);
4474 struct isl_basic_map
*isl_basic_map_apply_domain(
4475 struct isl_basic_map
*bmap1
, struct isl_basic_map
*bmap2
)
4477 if (isl_basic_map_check_equal_params(bmap1
, bmap2
) < 0)
4479 if (!isl_space_tuple_is_equal(bmap1
->dim
, isl_dim_in
,
4480 bmap2
->dim
, isl_dim_in
))
4481 isl_die(isl_basic_map_get_ctx(bmap1
), isl_error_invalid
,
4482 "spaces don't match", goto error
);
4484 bmap1
= isl_basic_map_reverse(bmap1
);
4485 bmap1
= isl_basic_map_apply_range(bmap1
, bmap2
);
4486 return isl_basic_map_reverse(bmap1
);
4488 isl_basic_map_free(bmap1
);
4489 isl_basic_map_free(bmap2
);
4493 /* Given two basic maps A -> f(A) and B -> g(B), construct a basic map
4494 * A \cap B -> f(A) + f(B)
4496 struct isl_basic_map
*isl_basic_map_sum(
4497 struct isl_basic_map
*bmap1
, struct isl_basic_map
*bmap2
)
4499 unsigned n_in
, n_out
, nparam
, total
, pos
;
4500 struct isl_basic_map
*bmap
= NULL
;
4501 struct isl_dim_map
*dim_map1
, *dim_map2
;
4504 if (!bmap1
|| !bmap2
)
4507 isl_assert(bmap1
->ctx
, isl_space_is_equal(bmap1
->dim
, bmap2
->dim
),
4510 nparam
= isl_basic_map_dim(bmap1
, isl_dim_param
);
4511 n_in
= isl_basic_map_dim(bmap1
, isl_dim_in
);
4512 n_out
= isl_basic_map_dim(bmap1
, isl_dim_out
);
4514 total
= nparam
+ n_in
+ n_out
+ bmap1
->n_div
+ bmap2
->n_div
+ 2 * n_out
;
4515 dim_map1
= isl_dim_map_alloc(bmap1
->ctx
, total
);
4516 dim_map2
= isl_dim_map_alloc(bmap2
->ctx
, total
);
4517 isl_dim_map_dim(dim_map1
, bmap1
->dim
, isl_dim_param
, pos
= 0);
4518 isl_dim_map_dim(dim_map2
, bmap2
->dim
, isl_dim_param
, pos
);
4519 isl_dim_map_dim(dim_map1
, bmap1
->dim
, isl_dim_in
, pos
+= nparam
);
4520 isl_dim_map_dim(dim_map2
, bmap2
->dim
, isl_dim_in
, pos
);
4521 isl_dim_map_div(dim_map1
, bmap1
, pos
+= n_in
+ n_out
);
4522 isl_dim_map_div(dim_map2
, bmap2
, pos
+= bmap1
->n_div
);
4523 isl_dim_map_dim(dim_map1
, bmap1
->dim
, isl_dim_out
, pos
+= bmap2
->n_div
);
4524 isl_dim_map_dim(dim_map2
, bmap2
->dim
, isl_dim_out
, pos
+= n_out
);
4526 bmap
= isl_basic_map_alloc_space(isl_space_copy(bmap1
->dim
),
4527 bmap1
->n_div
+ bmap2
->n_div
+ 2 * n_out
,
4528 bmap1
->n_eq
+ bmap2
->n_eq
+ n_out
,
4529 bmap1
->n_ineq
+ bmap2
->n_ineq
);
4530 for (i
= 0; i
< n_out
; ++i
) {
4531 int j
= isl_basic_map_alloc_equality(bmap
);
4534 isl_seq_clr(bmap
->eq
[j
], 1+total
);
4535 isl_int_set_si(bmap
->eq
[j
][1+nparam
+n_in
+i
], -1);
4536 isl_int_set_si(bmap
->eq
[j
][1+pos
+i
], 1);
4537 isl_int_set_si(bmap
->eq
[j
][1+pos
-n_out
+i
], 1);
4539 bmap
= isl_basic_map_add_constraints_dim_map(bmap
, bmap1
, dim_map1
);
4540 bmap
= isl_basic_map_add_constraints_dim_map(bmap
, bmap2
, dim_map2
);
4541 bmap
= add_divs(bmap
, 2 * n_out
);
4543 bmap
= isl_basic_map_simplify(bmap
);
4544 return isl_basic_map_finalize(bmap
);
4546 isl_basic_map_free(bmap
);
4547 isl_basic_map_free(bmap1
);
4548 isl_basic_map_free(bmap2
);
4552 /* Given two maps A -> f(A) and B -> g(B), construct a map
4553 * A \cap B -> f(A) + f(B)
4555 struct isl_map
*isl_map_sum(struct isl_map
*map1
, struct isl_map
*map2
)
4557 struct isl_map
*result
;
4563 isl_assert(map1
->ctx
, isl_space_is_equal(map1
->dim
, map2
->dim
), goto error
);
4565 result
= isl_map_alloc_space(isl_space_copy(map1
->dim
),
4566 map1
->n
* map2
->n
, 0);
4569 for (i
= 0; i
< map1
->n
; ++i
)
4570 for (j
= 0; j
< map2
->n
; ++j
) {
4571 struct isl_basic_map
*part
;
4572 part
= isl_basic_map_sum(
4573 isl_basic_map_copy(map1
->p
[i
]),
4574 isl_basic_map_copy(map2
->p
[j
]));
4575 if (isl_basic_map_is_empty(part
))
4576 isl_basic_map_free(part
);
4578 result
= isl_map_add_basic_map(result
, part
);
4591 __isl_give isl_set
*isl_set_sum(__isl_take isl_set
*set1
,
4592 __isl_take isl_set
*set2
)
4594 return set_from_map(isl_map_sum(set_to_map(set1
), set_to_map(set2
)));
4597 /* Given a basic map A -> f(A), construct A -> -f(A).
4599 struct isl_basic_map
*isl_basic_map_neg(struct isl_basic_map
*bmap
)
4604 bmap
= isl_basic_map_cow(bmap
);
4608 n
= isl_basic_map_dim(bmap
, isl_dim_out
);
4609 off
= isl_basic_map_offset(bmap
, isl_dim_out
);
4610 for (i
= 0; i
< bmap
->n_eq
; ++i
)
4611 for (j
= 0; j
< n
; ++j
)
4612 isl_int_neg(bmap
->eq
[i
][off
+j
], bmap
->eq
[i
][off
+j
]);
4613 for (i
= 0; i
< bmap
->n_ineq
; ++i
)
4614 for (j
= 0; j
< n
; ++j
)
4615 isl_int_neg(bmap
->ineq
[i
][off
+j
], bmap
->ineq
[i
][off
+j
]);
4616 for (i
= 0; i
< bmap
->n_div
; ++i
)
4617 for (j
= 0; j
< n
; ++j
)
4618 isl_int_neg(bmap
->div
[i
][1+off
+j
], bmap
->div
[i
][1+off
+j
]);
4619 bmap
= isl_basic_map_gauss(bmap
, NULL
);
4620 return isl_basic_map_finalize(bmap
);
4623 __isl_give isl_basic_set
*isl_basic_set_neg(__isl_take isl_basic_set
*bset
)
4625 return isl_basic_map_neg(bset
);
4628 /* Given a map A -> f(A), construct A -> -f(A).
4630 struct isl_map
*isl_map_neg(struct isl_map
*map
)
4634 map
= isl_map_cow(map
);
4638 for (i
= 0; i
< map
->n
; ++i
) {
4639 map
->p
[i
] = isl_basic_map_neg(map
->p
[i
]);
4650 __isl_give isl_set
*isl_set_neg(__isl_take isl_set
*set
)
4652 return set_from_map(isl_map_neg(set_to_map(set
)));
4655 /* Given a basic map A -> f(A) and an integer d, construct a basic map
4656 * A -> floor(f(A)/d).
4658 struct isl_basic_map
*isl_basic_map_floordiv(struct isl_basic_map
*bmap
,
4661 unsigned n_in
, n_out
, nparam
, total
, pos
;
4662 struct isl_basic_map
*result
= NULL
;
4663 struct isl_dim_map
*dim_map
;
4669 nparam
= isl_basic_map_dim(bmap
, isl_dim_param
);
4670 n_in
= isl_basic_map_dim(bmap
, isl_dim_in
);
4671 n_out
= isl_basic_map_dim(bmap
, isl_dim_out
);
4673 total
= nparam
+ n_in
+ n_out
+ bmap
->n_div
+ n_out
;
4674 dim_map
= isl_dim_map_alloc(bmap
->ctx
, total
);
4675 isl_dim_map_dim(dim_map
, bmap
->dim
, isl_dim_param
, pos
= 0);
4676 isl_dim_map_dim(dim_map
, bmap
->dim
, isl_dim_in
, pos
+= nparam
);
4677 isl_dim_map_div(dim_map
, bmap
, pos
+= n_in
+ n_out
);
4678 isl_dim_map_dim(dim_map
, bmap
->dim
, isl_dim_out
, pos
+= bmap
->n_div
);
4680 result
= isl_basic_map_alloc_space(isl_space_copy(bmap
->dim
),
4681 bmap
->n_div
+ n_out
,
4682 bmap
->n_eq
, bmap
->n_ineq
+ 2 * n_out
);
4683 result
= isl_basic_map_add_constraints_dim_map(result
, bmap
, dim_map
);
4684 result
= add_divs(result
, n_out
);
4685 for (i
= 0; i
< n_out
; ++i
) {
4687 j
= isl_basic_map_alloc_inequality(result
);
4690 isl_seq_clr(result
->ineq
[j
], 1+total
);
4691 isl_int_neg(result
->ineq
[j
][1+nparam
+n_in
+i
], d
);
4692 isl_int_set_si(result
->ineq
[j
][1+pos
+i
], 1);
4693 j
= isl_basic_map_alloc_inequality(result
);
4696 isl_seq_clr(result
->ineq
[j
], 1+total
);
4697 isl_int_set(result
->ineq
[j
][1+nparam
+n_in
+i
], d
);
4698 isl_int_set_si(result
->ineq
[j
][1+pos
+i
], -1);
4699 isl_int_sub_ui(result
->ineq
[j
][0], d
, 1);
4702 result
= isl_basic_map_simplify(result
);
4703 return isl_basic_map_finalize(result
);
4705 isl_basic_map_free(result
);
4709 /* Given a map A -> f(A) and an integer d, construct a map
4710 * A -> floor(f(A)/d).
4712 struct isl_map
*isl_map_floordiv(struct isl_map
*map
, isl_int d
)
4716 map
= isl_map_cow(map
);
4720 ISL_F_CLR(map
, ISL_MAP_DISJOINT
);
4721 ISL_F_CLR(map
, ISL_MAP_NORMALIZED
);
4722 for (i
= 0; i
< map
->n
; ++i
) {
4723 map
->p
[i
] = isl_basic_map_floordiv(map
->p
[i
], d
);
4734 /* Given a map A -> f(A) and an integer d, construct a map
4735 * A -> floor(f(A)/d).
4737 __isl_give isl_map
*isl_map_floordiv_val(__isl_take isl_map
*map
,
4738 __isl_take isl_val
*d
)
4742 if (!isl_val_is_int(d
))
4743 isl_die(isl_val_get_ctx(d
), isl_error_invalid
,
4744 "expecting integer denominator", goto error
);
4745 map
= isl_map_floordiv(map
, d
->n
);
4754 static __isl_give isl_basic_map
*var_equal(__isl_take isl_basic_map
*bmap
,
4761 i
= isl_basic_map_alloc_equality(bmap
);
4764 nparam
= isl_basic_map_dim(bmap
, isl_dim_param
);
4765 n_in
= isl_basic_map_dim(bmap
, isl_dim_in
);
4766 isl_seq_clr(bmap
->eq
[i
], 1 + isl_basic_map_total_dim(bmap
));
4767 isl_int_set_si(bmap
->eq
[i
][1+nparam
+pos
], -1);
4768 isl_int_set_si(bmap
->eq
[i
][1+nparam
+n_in
+pos
], 1);
4769 return isl_basic_map_finalize(bmap
);
4771 isl_basic_map_free(bmap
);
4775 /* Add a constraint to "bmap" expressing i_pos < o_pos
4777 static __isl_give isl_basic_map
*var_less(__isl_take isl_basic_map
*bmap
,
4784 i
= isl_basic_map_alloc_inequality(bmap
);
4787 nparam
= isl_basic_map_dim(bmap
, isl_dim_param
);
4788 n_in
= isl_basic_map_dim(bmap
, isl_dim_in
);
4789 isl_seq_clr(bmap
->ineq
[i
], 1 + isl_basic_map_total_dim(bmap
));
4790 isl_int_set_si(bmap
->ineq
[i
][0], -1);
4791 isl_int_set_si(bmap
->ineq
[i
][1+nparam
+pos
], -1);
4792 isl_int_set_si(bmap
->ineq
[i
][1+nparam
+n_in
+pos
], 1);
4793 return isl_basic_map_finalize(bmap
);
4795 isl_basic_map_free(bmap
);
4799 /* Add a constraint to "bmap" expressing i_pos <= o_pos
4801 static __isl_give isl_basic_map
*var_less_or_equal(
4802 __isl_take isl_basic_map
*bmap
, unsigned pos
)
4808 i
= isl_basic_map_alloc_inequality(bmap
);
4811 nparam
= isl_basic_map_dim(bmap
, isl_dim_param
);
4812 n_in
= isl_basic_map_dim(bmap
, isl_dim_in
);
4813 isl_seq_clr(bmap
->ineq
[i
], 1 + isl_basic_map_total_dim(bmap
));
4814 isl_int_set_si(bmap
->ineq
[i
][1+nparam
+pos
], -1);
4815 isl_int_set_si(bmap
->ineq
[i
][1+nparam
+n_in
+pos
], 1);
4816 return isl_basic_map_finalize(bmap
);
4818 isl_basic_map_free(bmap
);
4822 /* Add a constraint to "bmap" expressing i_pos > o_pos
4824 static __isl_give isl_basic_map
*var_more(__isl_take isl_basic_map
*bmap
,
4831 i
= isl_basic_map_alloc_inequality(bmap
);
4834 nparam
= isl_basic_map_dim(bmap
, isl_dim_param
);
4835 n_in
= isl_basic_map_dim(bmap
, isl_dim_in
);
4836 isl_seq_clr(bmap
->ineq
[i
], 1 + isl_basic_map_total_dim(bmap
));
4837 isl_int_set_si(bmap
->ineq
[i
][0], -1);
4838 isl_int_set_si(bmap
->ineq
[i
][1+nparam
+pos
], 1);
4839 isl_int_set_si(bmap
->ineq
[i
][1+nparam
+n_in
+pos
], -1);
4840 return isl_basic_map_finalize(bmap
);
4842 isl_basic_map_free(bmap
);
4846 /* Add a constraint to "bmap" expressing i_pos >= o_pos
4848 static __isl_give isl_basic_map
*var_more_or_equal(
4849 __isl_take isl_basic_map
*bmap
, unsigned pos
)
4855 i
= isl_basic_map_alloc_inequality(bmap
);
4858 nparam
= isl_basic_map_dim(bmap
, isl_dim_param
);
4859 n_in
= isl_basic_map_dim(bmap
, isl_dim_in
);
4860 isl_seq_clr(bmap
->ineq
[i
], 1 + isl_basic_map_total_dim(bmap
));
4861 isl_int_set_si(bmap
->ineq
[i
][1+nparam
+pos
], 1);
4862 isl_int_set_si(bmap
->ineq
[i
][1+nparam
+n_in
+pos
], -1);
4863 return isl_basic_map_finalize(bmap
);
4865 isl_basic_map_free(bmap
);
4869 __isl_give isl_basic_map
*isl_basic_map_equal(
4870 __isl_take isl_space
*dim
, unsigned n_equal
)
4873 struct isl_basic_map
*bmap
;
4874 bmap
= isl_basic_map_alloc_space(dim
, 0, n_equal
, 0);
4877 for (i
= 0; i
< n_equal
&& bmap
; ++i
)
4878 bmap
= var_equal(bmap
, i
);
4879 return isl_basic_map_finalize(bmap
);
4882 /* Return a relation on of dimension "dim" expressing i_[0..pos] << o_[0..pos]
4884 __isl_give isl_basic_map
*isl_basic_map_less_at(__isl_take isl_space
*dim
,
4888 struct isl_basic_map
*bmap
;
4889 bmap
= isl_basic_map_alloc_space(dim
, 0, pos
, 1);
4892 for (i
= 0; i
< pos
&& bmap
; ++i
)
4893 bmap
= var_equal(bmap
, i
);
4895 bmap
= var_less(bmap
, pos
);
4896 return isl_basic_map_finalize(bmap
);
4899 /* Return a relation on "dim" expressing i_[0..pos] <<= o_[0..pos]
4901 __isl_give isl_basic_map
*isl_basic_map_less_or_equal_at(
4902 __isl_take isl_space
*dim
, unsigned pos
)
4905 isl_basic_map
*bmap
;
4907 bmap
= isl_basic_map_alloc_space(dim
, 0, pos
, 1);
4908 for (i
= 0; i
< pos
; ++i
)
4909 bmap
= var_equal(bmap
, i
);
4910 bmap
= var_less_or_equal(bmap
, pos
);
4911 return isl_basic_map_finalize(bmap
);
4914 /* Return a relation on "dim" expressing i_pos > o_pos
4916 __isl_give isl_basic_map
*isl_basic_map_more_at(__isl_take isl_space
*dim
,
4920 struct isl_basic_map
*bmap
;
4921 bmap
= isl_basic_map_alloc_space(dim
, 0, pos
, 1);
4924 for (i
= 0; i
< pos
&& bmap
; ++i
)
4925 bmap
= var_equal(bmap
, i
);
4927 bmap
= var_more(bmap
, pos
);
4928 return isl_basic_map_finalize(bmap
);
4931 /* Return a relation on "dim" expressing i_[0..pos] >>= o_[0..pos]
4933 __isl_give isl_basic_map
*isl_basic_map_more_or_equal_at(
4934 __isl_take isl_space
*dim
, unsigned pos
)
4937 isl_basic_map
*bmap
;
4939 bmap
= isl_basic_map_alloc_space(dim
, 0, pos
, 1);
4940 for (i
= 0; i
< pos
; ++i
)
4941 bmap
= var_equal(bmap
, i
);
4942 bmap
= var_more_or_equal(bmap
, pos
);
4943 return isl_basic_map_finalize(bmap
);
4946 static __isl_give isl_map
*map_lex_lte_first(__isl_take isl_space
*dims
,
4947 unsigned n
, int equal
)
4949 struct isl_map
*map
;
4952 if (n
== 0 && equal
)
4953 return isl_map_universe(dims
);
4955 map
= isl_map_alloc_space(isl_space_copy(dims
), n
, ISL_MAP_DISJOINT
);
4957 for (i
= 0; i
+ 1 < n
; ++i
)
4958 map
= isl_map_add_basic_map(map
,
4959 isl_basic_map_less_at(isl_space_copy(dims
), i
));
4962 map
= isl_map_add_basic_map(map
,
4963 isl_basic_map_less_or_equal_at(dims
, n
- 1));
4965 map
= isl_map_add_basic_map(map
,
4966 isl_basic_map_less_at(dims
, n
- 1));
4968 isl_space_free(dims
);
4973 static __isl_give isl_map
*map_lex_lte(__isl_take isl_space
*dims
, int equal
)
4977 return map_lex_lte_first(dims
, dims
->n_out
, equal
);
4980 __isl_give isl_map
*isl_map_lex_lt_first(__isl_take isl_space
*dim
, unsigned n
)
4982 return map_lex_lte_first(dim
, n
, 0);
4985 __isl_give isl_map
*isl_map_lex_le_first(__isl_take isl_space
*dim
, unsigned n
)
4987 return map_lex_lte_first(dim
, n
, 1);
4990 __isl_give isl_map
*isl_map_lex_lt(__isl_take isl_space
*set_dim
)
4992 return map_lex_lte(isl_space_map_from_set(set_dim
), 0);
4995 __isl_give isl_map
*isl_map_lex_le(__isl_take isl_space
*set_dim
)
4997 return map_lex_lte(isl_space_map_from_set(set_dim
), 1);
5000 static __isl_give isl_map
*map_lex_gte_first(__isl_take isl_space
*dims
,
5001 unsigned n
, int equal
)
5003 struct isl_map
*map
;
5006 if (n
== 0 && equal
)
5007 return isl_map_universe(dims
);
5009 map
= isl_map_alloc_space(isl_space_copy(dims
), n
, ISL_MAP_DISJOINT
);
5011 for (i
= 0; i
+ 1 < n
; ++i
)
5012 map
= isl_map_add_basic_map(map
,
5013 isl_basic_map_more_at(isl_space_copy(dims
), i
));
5016 map
= isl_map_add_basic_map(map
,
5017 isl_basic_map_more_or_equal_at(dims
, n
- 1));
5019 map
= isl_map_add_basic_map(map
,
5020 isl_basic_map_more_at(dims
, n
- 1));
5022 isl_space_free(dims
);
5027 static __isl_give isl_map
*map_lex_gte(__isl_take isl_space
*dims
, int equal
)
5031 return map_lex_gte_first(dims
, dims
->n_out
, equal
);
5034 __isl_give isl_map
*isl_map_lex_gt_first(__isl_take isl_space
*dim
, unsigned n
)
5036 return map_lex_gte_first(dim
, n
, 0);
5039 __isl_give isl_map
*isl_map_lex_ge_first(__isl_take isl_space
*dim
, unsigned n
)
5041 return map_lex_gte_first(dim
, n
, 1);
5044 __isl_give isl_map
*isl_map_lex_gt(__isl_take isl_space
*set_dim
)
5046 return map_lex_gte(isl_space_map_from_set(set_dim
), 0);
5049 __isl_give isl_map
*isl_map_lex_ge(__isl_take isl_space
*set_dim
)
5051 return map_lex_gte(isl_space_map_from_set(set_dim
), 1);
5054 __isl_give isl_map
*isl_set_lex_le_set(__isl_take isl_set
*set1
,
5055 __isl_take isl_set
*set2
)
5058 map
= isl_map_lex_le(isl_set_get_space(set1
));
5059 map
= isl_map_intersect_domain(map
, set1
);
5060 map
= isl_map_intersect_range(map
, set2
);
5064 __isl_give isl_map
*isl_set_lex_lt_set(__isl_take isl_set
*set1
,
5065 __isl_take isl_set
*set2
)
5068 map
= isl_map_lex_lt(isl_set_get_space(set1
));
5069 map
= isl_map_intersect_domain(map
, set1
);
5070 map
= isl_map_intersect_range(map
, set2
);
5074 __isl_give isl_map
*isl_set_lex_ge_set(__isl_take isl_set
*set1
,
5075 __isl_take isl_set
*set2
)
5078 map
= isl_map_lex_ge(isl_set_get_space(set1
));
5079 map
= isl_map_intersect_domain(map
, set1
);
5080 map
= isl_map_intersect_range(map
, set2
);
5084 __isl_give isl_map
*isl_set_lex_gt_set(__isl_take isl_set
*set1
,
5085 __isl_take isl_set
*set2
)
5088 map
= isl_map_lex_gt(isl_set_get_space(set1
));
5089 map
= isl_map_intersect_domain(map
, set1
);
5090 map
= isl_map_intersect_range(map
, set2
);
5094 __isl_give isl_map
*isl_map_lex_le_map(__isl_take isl_map
*map1
,
5095 __isl_take isl_map
*map2
)
5098 map
= isl_map_lex_le(isl_space_range(isl_map_get_space(map1
)));
5099 map
= isl_map_apply_domain(map
, isl_map_reverse(map1
));
5100 map
= isl_map_apply_range(map
, isl_map_reverse(map2
));
5104 __isl_give isl_map
*isl_map_lex_lt_map(__isl_take isl_map
*map1
,
5105 __isl_take isl_map
*map2
)
5108 map
= isl_map_lex_lt(isl_space_range(isl_map_get_space(map1
)));
5109 map
= isl_map_apply_domain(map
, isl_map_reverse(map1
));
5110 map
= isl_map_apply_range(map
, isl_map_reverse(map2
));
5114 __isl_give isl_map
*isl_map_lex_ge_map(__isl_take isl_map
*map1
,
5115 __isl_take isl_map
*map2
)
5118 map
= isl_map_lex_ge(isl_space_range(isl_map_get_space(map1
)));
5119 map
= isl_map_apply_domain(map
, isl_map_reverse(map1
));
5120 map
= isl_map_apply_range(map
, isl_map_reverse(map2
));
5124 __isl_give isl_map
*isl_map_lex_gt_map(__isl_take isl_map
*map1
,
5125 __isl_take isl_map
*map2
)
5128 map
= isl_map_lex_gt(isl_space_range(isl_map_get_space(map1
)));
5129 map
= isl_map_apply_domain(map
, isl_map_reverse(map1
));
5130 map
= isl_map_apply_range(map
, isl_map_reverse(map2
));
5134 /* For a div d = floor(f/m), add the constraint
5138 static isl_stat
add_upper_div_constraint(__isl_keep isl_basic_map
*bmap
,
5139 unsigned pos
, isl_int
*div
)
5142 unsigned total
= isl_basic_map_total_dim(bmap
);
5144 i
= isl_basic_map_alloc_inequality(bmap
);
5146 return isl_stat_error
;
5147 isl_seq_cpy(bmap
->ineq
[i
], div
+ 1, 1 + total
);
5148 isl_int_neg(bmap
->ineq
[i
][1 + pos
], div
[0]);
5153 /* For a div d = floor(f/m), add the constraint
5155 * -(f-(m-1)) + m d >= 0
5157 static isl_stat
add_lower_div_constraint(__isl_keep isl_basic_map
*bmap
,
5158 unsigned pos
, isl_int
*div
)
5161 unsigned total
= isl_basic_map_total_dim(bmap
);
5163 i
= isl_basic_map_alloc_inequality(bmap
);
5165 return isl_stat_error
;
5166 isl_seq_neg(bmap
->ineq
[i
], div
+ 1, 1 + total
);
5167 isl_int_set(bmap
->ineq
[i
][1 + pos
], div
[0]);
5168 isl_int_add(bmap
->ineq
[i
][0], bmap
->ineq
[i
][0], bmap
->ineq
[i
][1 + pos
]);
5169 isl_int_sub_ui(bmap
->ineq
[i
][0], bmap
->ineq
[i
][0], 1);
5174 /* For a div d = floor(f/m), add the constraints
5177 * -(f-(m-1)) + m d >= 0
5179 * Note that the second constraint is the negation of
5183 int isl_basic_map_add_div_constraints_var(__isl_keep isl_basic_map
*bmap
,
5184 unsigned pos
, isl_int
*div
)
5186 if (add_upper_div_constraint(bmap
, pos
, div
) < 0)
5188 if (add_lower_div_constraint(bmap
, pos
, div
) < 0)
5193 int isl_basic_set_add_div_constraints_var(__isl_keep isl_basic_set
*bset
,
5194 unsigned pos
, isl_int
*div
)
5196 return isl_basic_map_add_div_constraints_var(bset_to_bmap(bset
),
5200 int isl_basic_map_add_div_constraints(struct isl_basic_map
*bmap
, unsigned div
)
5202 unsigned total
= isl_basic_map_total_dim(bmap
);
5203 unsigned div_pos
= total
- bmap
->n_div
+ div
;
5205 return isl_basic_map_add_div_constraints_var(bmap
, div_pos
,
5209 /* For each known div d = floor(f/m), add the constraints
5212 * -(f-(m-1)) + m d >= 0
5214 * Remove duplicate constraints in case of some these div constraints
5215 * already appear in "bmap".
5217 __isl_give isl_basic_map
*isl_basic_map_add_known_div_constraints(
5218 __isl_take isl_basic_map
*bmap
)
5224 n_div
= isl_basic_map_dim(bmap
, isl_dim_div
);
5228 bmap
= add_known_div_constraints(bmap
);
5229 bmap
= isl_basic_map_remove_duplicate_constraints(bmap
, NULL
, 0);
5230 bmap
= isl_basic_map_finalize(bmap
);
5234 /* Add the div constraint of sign "sign" for div "div" of "bmap".
5236 * In particular, if this div is of the form d = floor(f/m),
5237 * then add the constraint
5241 * if sign < 0 or the constraint
5243 * -(f-(m-1)) + m d >= 0
5247 int isl_basic_map_add_div_constraint(__isl_keep isl_basic_map
*bmap
,
5248 unsigned div
, int sign
)
5256 total
= isl_basic_map_total_dim(bmap
);
5257 div_pos
= total
- bmap
->n_div
+ div
;
5260 return add_upper_div_constraint(bmap
, div_pos
, bmap
->div
[div
]);
5262 return add_lower_div_constraint(bmap
, div_pos
, bmap
->div
[div
]);
5265 struct isl_basic_set
*isl_basic_map_underlying_set(
5266 struct isl_basic_map
*bmap
)
5270 if (bmap
->dim
->nparam
== 0 && bmap
->dim
->n_in
== 0 &&
5272 !isl_space_is_named_or_nested(bmap
->dim
, isl_dim_in
) &&
5273 !isl_space_is_named_or_nested(bmap
->dim
, isl_dim_out
))
5274 return bset_from_bmap(bmap
);
5275 bmap
= isl_basic_map_cow(bmap
);
5278 bmap
->dim
= isl_space_underlying(bmap
->dim
, bmap
->n_div
);
5281 bmap
->extra
-= bmap
->n_div
;
5283 bmap
= isl_basic_map_finalize(bmap
);
5284 return bset_from_bmap(bmap
);
5286 isl_basic_map_free(bmap
);
5290 __isl_give isl_basic_set
*isl_basic_set_underlying_set(
5291 __isl_take isl_basic_set
*bset
)
5293 return isl_basic_map_underlying_set(bset_to_bmap(bset
));
5296 /* Replace each element in "list" by the result of applying
5297 * isl_basic_map_underlying_set to the element.
5299 __isl_give isl_basic_set_list
*isl_basic_map_list_underlying_set(
5300 __isl_take isl_basic_map_list
*list
)
5307 n
= isl_basic_map_list_n_basic_map(list
);
5308 for (i
= 0; i
< n
; ++i
) {
5309 isl_basic_map
*bmap
;
5310 isl_basic_set
*bset
;
5312 bmap
= isl_basic_map_list_get_basic_map(list
, i
);
5313 bset
= isl_basic_set_underlying_set(bmap
);
5314 list
= isl_basic_set_list_set_basic_set(list
, i
, bset
);
5320 struct isl_basic_map
*isl_basic_map_overlying_set(
5321 struct isl_basic_set
*bset
, struct isl_basic_map
*like
)
5323 struct isl_basic_map
*bmap
;
5324 struct isl_ctx
*ctx
;
5331 isl_assert(ctx
, bset
->n_div
== 0, goto error
);
5332 isl_assert(ctx
, isl_basic_set_n_param(bset
) == 0, goto error
);
5333 isl_assert(ctx
, bset
->dim
->n_out
== isl_basic_map_total_dim(like
),
5335 if (like
->n_div
== 0) {
5336 isl_space
*space
= isl_basic_map_get_space(like
);
5337 isl_basic_map_free(like
);
5338 return isl_basic_map_reset_space(bset
, space
);
5340 bset
= isl_basic_set_cow(bset
);
5343 total
= bset
->dim
->n_out
+ bset
->extra
;
5344 bmap
= bset_to_bmap(bset
);
5345 isl_space_free(bmap
->dim
);
5346 bmap
->dim
= isl_space_copy(like
->dim
);
5349 bmap
->n_div
= like
->n_div
;
5350 bmap
->extra
+= like
->n_div
;
5354 ltotal
= total
- bmap
->extra
+ like
->extra
;
5357 bmap
->block2
= isl_blk_extend(ctx
, bmap
->block2
,
5358 bmap
->extra
* (1 + 1 + total
));
5359 if (isl_blk_is_error(bmap
->block2
))
5361 div
= isl_realloc_array(ctx
, bmap
->div
, isl_int
*, bmap
->extra
);
5365 for (i
= 0; i
< bmap
->extra
; ++i
)
5366 bmap
->div
[i
] = bmap
->block2
.data
+ i
* (1 + 1 + total
);
5367 for (i
= 0; i
< like
->n_div
; ++i
) {
5368 isl_seq_cpy(bmap
->div
[i
], like
->div
[i
], 1 + 1 + ltotal
);
5369 isl_seq_clr(bmap
->div
[i
]+1+1+ltotal
, total
- ltotal
);
5371 bmap
= isl_basic_map_add_known_div_constraints(bmap
);
5373 isl_basic_map_free(like
);
5374 bmap
= isl_basic_map_simplify(bmap
);
5375 bmap
= isl_basic_map_finalize(bmap
);
5378 isl_basic_map_free(like
);
5379 isl_basic_set_free(bset
);
5383 struct isl_basic_set
*isl_basic_set_from_underlying_set(
5384 struct isl_basic_set
*bset
, struct isl_basic_set
*like
)
5386 return bset_from_bmap(isl_basic_map_overlying_set(bset
,
5387 bset_to_bmap(like
)));
5390 struct isl_set
*isl_map_underlying_set(struct isl_map
*map
)
5394 map
= isl_map_cow(map
);
5397 map
->dim
= isl_space_cow(map
->dim
);
5401 for (i
= 1; i
< map
->n
; ++i
)
5402 isl_assert(map
->ctx
, map
->p
[0]->n_div
== map
->p
[i
]->n_div
,
5404 for (i
= 0; i
< map
->n
; ++i
) {
5405 map
->p
[i
] = bset_to_bmap(
5406 isl_basic_map_underlying_set(map
->p
[i
]));
5411 map
->dim
= isl_space_underlying(map
->dim
, 0);
5413 isl_space_free(map
->dim
);
5414 map
->dim
= isl_space_copy(map
->p
[0]->dim
);
5418 return set_from_map(map
);
5424 /* Replace the space of "bmap" by "space".
5426 * If the space of "bmap" is identical to "space" (including the identifiers
5427 * of the input and output dimensions), then simply return the original input.
5429 __isl_give isl_basic_map
*isl_basic_map_reset_space(
5430 __isl_take isl_basic_map
*bmap
, __isl_take isl_space
*space
)
5433 isl_space
*bmap_space
;
5435 bmap_space
= isl_basic_map_peek_space(bmap
);
5436 equal
= isl_space_is_equal(bmap_space
, space
);
5437 if (equal
>= 0 && equal
)
5438 equal
= isl_space_has_equal_ids(bmap_space
, space
);
5442 isl_space_free(space
);
5445 bmap
= isl_basic_map_cow(bmap
);
5446 if (!bmap
|| !space
)
5449 isl_space_free(bmap
->dim
);
5452 bmap
= isl_basic_map_finalize(bmap
);
5456 isl_basic_map_free(bmap
);
5457 isl_space_free(space
);
5461 __isl_give isl_basic_set
*isl_basic_set_reset_space(
5462 __isl_take isl_basic_set
*bset
, __isl_take isl_space
*dim
)
5464 return bset_from_bmap(isl_basic_map_reset_space(bset_to_bmap(bset
),
5468 __isl_give isl_map
*isl_map_reset_space(__isl_take isl_map
*map
,
5469 __isl_take isl_space
*dim
)
5473 map
= isl_map_cow(map
);
5477 for (i
= 0; i
< map
->n
; ++i
) {
5478 map
->p
[i
] = isl_basic_map_reset_space(map
->p
[i
],
5479 isl_space_copy(dim
));
5483 isl_space_free(map
->dim
);
5489 isl_space_free(dim
);
5493 __isl_give isl_set
*isl_set_reset_space(__isl_take isl_set
*set
,
5494 __isl_take isl_space
*dim
)
5496 return set_from_map(isl_map_reset_space(set_to_map(set
), dim
));
5499 /* Compute the parameter domain of the given basic set.
5501 __isl_give isl_basic_set
*isl_basic_set_params(__isl_take isl_basic_set
*bset
)
5507 is_params
= isl_basic_set_is_params(bset
);
5509 return isl_basic_set_free(bset
);
5513 n
= isl_basic_set_dim(bset
, isl_dim_set
);
5514 bset
= isl_basic_set_project_out(bset
, isl_dim_set
, 0, n
);
5515 space
= isl_basic_set_get_space(bset
);
5516 space
= isl_space_params(space
);
5517 bset
= isl_basic_set_reset_space(bset
, space
);
5521 /* Construct a zero-dimensional basic set with the given parameter domain.
5523 __isl_give isl_basic_set
*isl_basic_set_from_params(
5524 __isl_take isl_basic_set
*bset
)
5527 space
= isl_basic_set_get_space(bset
);
5528 space
= isl_space_set_from_params(space
);
5529 bset
= isl_basic_set_reset_space(bset
, space
);
5533 /* Compute the parameter domain of the given set.
5535 __isl_give isl_set
*isl_set_params(__isl_take isl_set
*set
)
5540 if (isl_set_is_params(set
))
5543 n
= isl_set_dim(set
, isl_dim_set
);
5544 set
= isl_set_project_out(set
, isl_dim_set
, 0, n
);
5545 space
= isl_set_get_space(set
);
5546 space
= isl_space_params(space
);
5547 set
= isl_set_reset_space(set
, space
);
5551 /* Construct a zero-dimensional set with the given parameter domain.
5553 __isl_give isl_set
*isl_set_from_params(__isl_take isl_set
*set
)
5556 space
= isl_set_get_space(set
);
5557 space
= isl_space_set_from_params(space
);
5558 set
= isl_set_reset_space(set
, space
);
5562 /* Compute the parameter domain of the given map.
5564 __isl_give isl_set
*isl_map_params(__isl_take isl_map
*map
)
5569 n
= isl_map_dim(map
, isl_dim_in
);
5570 map
= isl_map_project_out(map
, isl_dim_in
, 0, n
);
5571 n
= isl_map_dim(map
, isl_dim_out
);
5572 map
= isl_map_project_out(map
, isl_dim_out
, 0, n
);
5573 space
= isl_map_get_space(map
);
5574 space
= isl_space_params(space
);
5575 map
= isl_map_reset_space(map
, space
);
5579 struct isl_basic_set
*isl_basic_map_domain(struct isl_basic_map
*bmap
)
5586 space
= isl_space_domain(isl_basic_map_get_space(bmap
));
5588 n_out
= isl_basic_map_dim(bmap
, isl_dim_out
);
5589 bmap
= isl_basic_map_project_out(bmap
, isl_dim_out
, 0, n_out
);
5591 return isl_basic_map_reset_space(bmap
, space
);
5594 isl_bool
isl_basic_map_may_be_set(__isl_keep isl_basic_map
*bmap
)
5597 return isl_bool_error
;
5598 return isl_space_may_be_set(bmap
->dim
);
5601 /* Is this basic map actually a set?
5602 * Users should never call this function. Outside of isl,
5603 * the type should indicate whether something is a set or a map.
5605 isl_bool
isl_basic_map_is_set(__isl_keep isl_basic_map
*bmap
)
5608 return isl_bool_error
;
5609 return isl_space_is_set(bmap
->dim
);
5612 struct isl_basic_set
*isl_basic_map_range(struct isl_basic_map
*bmap
)
5616 is_set
= isl_basic_map_is_set(bmap
);
5621 return isl_basic_map_domain(isl_basic_map_reverse(bmap
));
5623 isl_basic_map_free(bmap
);
5627 __isl_give isl_basic_map
*isl_basic_map_domain_map(
5628 __isl_take isl_basic_map
*bmap
)
5632 isl_basic_map
*domain
;
5633 int nparam
, n_in
, n_out
;
5635 nparam
= isl_basic_map_dim(bmap
, isl_dim_param
);
5636 n_in
= isl_basic_map_dim(bmap
, isl_dim_in
);
5637 n_out
= isl_basic_map_dim(bmap
, isl_dim_out
);
5639 dim
= isl_space_from_range(isl_space_domain(isl_basic_map_get_space(bmap
)));
5640 domain
= isl_basic_map_universe(dim
);
5642 bmap
= isl_basic_map_from_domain(isl_basic_map_wrap(bmap
));
5643 bmap
= isl_basic_map_apply_range(bmap
, domain
);
5644 bmap
= isl_basic_map_extend_constraints(bmap
, n_in
, 0);
5646 for (i
= 0; i
< n_in
; ++i
)
5647 bmap
= isl_basic_map_equate(bmap
, isl_dim_in
, i
,
5650 bmap
= isl_basic_map_gauss(bmap
, NULL
);
5651 return isl_basic_map_finalize(bmap
);
5654 __isl_give isl_basic_map
*isl_basic_map_range_map(
5655 __isl_take isl_basic_map
*bmap
)
5659 isl_basic_map
*range
;
5660 int nparam
, n_in
, n_out
;
5662 nparam
= isl_basic_map_dim(bmap
, isl_dim_param
);
5663 n_in
= isl_basic_map_dim(bmap
, isl_dim_in
);
5664 n_out
= isl_basic_map_dim(bmap
, isl_dim_out
);
5666 dim
= isl_space_from_range(isl_space_range(isl_basic_map_get_space(bmap
)));
5667 range
= isl_basic_map_universe(dim
);
5669 bmap
= isl_basic_map_from_domain(isl_basic_map_wrap(bmap
));
5670 bmap
= isl_basic_map_apply_range(bmap
, range
);
5671 bmap
= isl_basic_map_extend_constraints(bmap
, n_out
, 0);
5673 for (i
= 0; i
< n_out
; ++i
)
5674 bmap
= isl_basic_map_equate(bmap
, isl_dim_in
, n_in
+ i
,
5677 bmap
= isl_basic_map_gauss(bmap
, NULL
);
5678 return isl_basic_map_finalize(bmap
);
5681 int isl_map_may_be_set(__isl_keep isl_map
*map
)
5685 return isl_space_may_be_set(map
->dim
);
5688 /* Is this map actually a set?
5689 * Users should never call this function. Outside of isl,
5690 * the type should indicate whether something is a set or a map.
5692 isl_bool
isl_map_is_set(__isl_keep isl_map
*map
)
5695 return isl_bool_error
;
5696 return isl_space_is_set(map
->dim
);
5699 struct isl_set
*isl_map_range(struct isl_map
*map
)
5703 struct isl_set
*set
;
5705 is_set
= isl_map_is_set(map
);
5709 return set_from_map(map
);
5711 map
= isl_map_cow(map
);
5715 set
= set_from_map(map
);
5716 set
->dim
= isl_space_range(set
->dim
);
5719 for (i
= 0; i
< map
->n
; ++i
) {
5720 set
->p
[i
] = isl_basic_map_range(map
->p
[i
]);
5724 ISL_F_CLR(set
, ISL_MAP_DISJOINT
);
5725 ISL_F_CLR(set
, ISL_SET_NORMALIZED
);
5732 __isl_give isl_map
*isl_map_domain_map(__isl_take isl_map
*map
)
5736 map
= isl_map_cow(map
);
5740 map
->dim
= isl_space_domain_map(map
->dim
);
5743 for (i
= 0; i
< map
->n
; ++i
) {
5744 map
->p
[i
] = isl_basic_map_domain_map(map
->p
[i
]);
5748 ISL_F_CLR(map
, ISL_MAP_DISJOINT
);
5749 ISL_F_CLR(map
, ISL_MAP_NORMALIZED
);
5756 __isl_give isl_map
*isl_map_range_map(__isl_take isl_map
*map
)
5759 isl_space
*range_dim
;
5761 map
= isl_map_cow(map
);
5765 range_dim
= isl_space_range(isl_map_get_space(map
));
5766 range_dim
= isl_space_from_range(range_dim
);
5767 map
->dim
= isl_space_from_domain(isl_space_wrap(map
->dim
));
5768 map
->dim
= isl_space_join(map
->dim
, range_dim
);
5771 for (i
= 0; i
< map
->n
; ++i
) {
5772 map
->p
[i
] = isl_basic_map_range_map(map
->p
[i
]);
5776 ISL_F_CLR(map
, ISL_MAP_DISJOINT
);
5777 ISL_F_CLR(map
, ISL_MAP_NORMALIZED
);
5784 /* Given a wrapped map of the form A[B -> C],
5785 * return the map A[B -> C] -> B.
5787 __isl_give isl_map
*isl_set_wrapped_domain_map(__isl_take isl_set
*set
)
5794 if (!isl_set_has_tuple_id(set
))
5795 return isl_map_domain_map(isl_set_unwrap(set
));
5797 id
= isl_set_get_tuple_id(set
);
5798 map
= isl_map_domain_map(isl_set_unwrap(set
));
5799 map
= isl_map_set_tuple_id(map
, isl_dim_in
, id
);
5804 __isl_give isl_basic_map
*isl_basic_map_from_domain(
5805 __isl_take isl_basic_set
*bset
)
5807 return isl_basic_map_reverse(isl_basic_map_from_range(bset
));
5810 __isl_give isl_basic_map
*isl_basic_map_from_range(
5811 __isl_take isl_basic_set
*bset
)
5814 space
= isl_basic_set_get_space(bset
);
5815 space
= isl_space_from_range(space
);
5816 bset
= isl_basic_set_reset_space(bset
, space
);
5817 return bset_to_bmap(bset
);
5820 /* Create a relation with the given set as range.
5821 * The domain of the created relation is a zero-dimensional
5822 * flat anonymous space.
5824 __isl_give isl_map
*isl_map_from_range(__isl_take isl_set
*set
)
5827 space
= isl_set_get_space(set
);
5828 space
= isl_space_from_range(space
);
5829 set
= isl_set_reset_space(set
, space
);
5830 return set_to_map(set
);
5833 /* Create a relation with the given set as domain.
5834 * The range of the created relation is a zero-dimensional
5835 * flat anonymous space.
5837 __isl_give isl_map
*isl_map_from_domain(__isl_take isl_set
*set
)
5839 return isl_map_reverse(isl_map_from_range(set
));
5842 __isl_give isl_basic_map
*isl_basic_map_from_domain_and_range(
5843 __isl_take isl_basic_set
*domain
, __isl_take isl_basic_set
*range
)
5845 return isl_basic_map_apply_range(isl_basic_map_reverse(domain
), range
);
5848 __isl_give isl_map
*isl_map_from_domain_and_range(__isl_take isl_set
*domain
,
5849 __isl_take isl_set
*range
)
5851 return isl_map_apply_range(isl_map_reverse(domain
), range
);
5854 /* Return a newly allocated isl_map with given space and flags and
5855 * room for "n" basic maps.
5856 * Make sure that all cached information is cleared.
5858 __isl_give isl_map
*isl_map_alloc_space(__isl_take isl_space
*space
, int n
,
5861 struct isl_map
*map
;
5866 isl_die(space
->ctx
, isl_error_internal
,
5867 "negative number of basic maps", goto error
);
5868 map
= isl_calloc(space
->ctx
, struct isl_map
,
5869 sizeof(struct isl_map
) +
5870 (n
- 1) * sizeof(struct isl_basic_map
*));
5874 map
->ctx
= space
->ctx
;
5875 isl_ctx_ref(map
->ctx
);
5883 isl_space_free(space
);
5887 __isl_give isl_basic_map
*isl_basic_map_empty(__isl_take isl_space
*dim
)
5889 struct isl_basic_map
*bmap
;
5890 bmap
= isl_basic_map_alloc_space(dim
, 0, 1, 0);
5891 bmap
= isl_basic_map_set_to_empty(bmap
);
5895 __isl_give isl_basic_set
*isl_basic_set_empty(__isl_take isl_space
*dim
)
5897 struct isl_basic_set
*bset
;
5898 bset
= isl_basic_set_alloc_space(dim
, 0, 1, 0);
5899 bset
= isl_basic_set_set_to_empty(bset
);
5903 __isl_give isl_basic_map
*isl_basic_map_universe(__isl_take isl_space
*dim
)
5905 struct isl_basic_map
*bmap
;
5906 bmap
= isl_basic_map_alloc_space(dim
, 0, 0, 0);
5907 bmap
= isl_basic_map_finalize(bmap
);
5911 __isl_give isl_basic_set
*isl_basic_set_universe(__isl_take isl_space
*dim
)
5913 struct isl_basic_set
*bset
;
5914 bset
= isl_basic_set_alloc_space(dim
, 0, 0, 0);
5915 bset
= isl_basic_set_finalize(bset
);
5919 __isl_give isl_basic_map
*isl_basic_map_nat_universe(__isl_take isl_space
*dim
)
5922 unsigned total
= isl_space_dim(dim
, isl_dim_all
);
5923 isl_basic_map
*bmap
;
5925 bmap
= isl_basic_map_alloc_space(dim
, 0, 0, total
);
5926 for (i
= 0; i
< total
; ++i
) {
5927 int k
= isl_basic_map_alloc_inequality(bmap
);
5930 isl_seq_clr(bmap
->ineq
[k
], 1 + total
);
5931 isl_int_set_si(bmap
->ineq
[k
][1 + i
], 1);
5935 isl_basic_map_free(bmap
);
5939 __isl_give isl_basic_set
*isl_basic_set_nat_universe(__isl_take isl_space
*dim
)
5941 return isl_basic_map_nat_universe(dim
);
5944 __isl_give isl_map
*isl_map_nat_universe(__isl_take isl_space
*dim
)
5946 return isl_map_from_basic_map(isl_basic_map_nat_universe(dim
));
5949 __isl_give isl_set
*isl_set_nat_universe(__isl_take isl_space
*dim
)
5951 return isl_map_nat_universe(dim
);
5954 __isl_give isl_map
*isl_map_empty(__isl_take isl_space
*dim
)
5956 return isl_map_alloc_space(dim
, 0, ISL_MAP_DISJOINT
);
5959 __isl_give isl_set
*isl_set_empty(__isl_take isl_space
*dim
)
5961 return isl_set_alloc_space(dim
, 0, ISL_MAP_DISJOINT
);
5964 __isl_give isl_map
*isl_map_universe(__isl_take isl_space
*dim
)
5966 struct isl_map
*map
;
5969 map
= isl_map_alloc_space(isl_space_copy(dim
), 1, ISL_MAP_DISJOINT
);
5970 map
= isl_map_add_basic_map(map
, isl_basic_map_universe(dim
));
5974 __isl_give isl_set
*isl_set_universe(__isl_take isl_space
*dim
)
5976 struct isl_set
*set
;
5979 set
= isl_set_alloc_space(isl_space_copy(dim
), 1, ISL_MAP_DISJOINT
);
5980 set
= isl_set_add_basic_set(set
, isl_basic_set_universe(dim
));
5984 struct isl_map
*isl_map_dup(struct isl_map
*map
)
5987 struct isl_map
*dup
;
5991 dup
= isl_map_alloc_space(isl_space_copy(map
->dim
), map
->n
, map
->flags
);
5992 for (i
= 0; i
< map
->n
; ++i
)
5993 dup
= isl_map_add_basic_map(dup
, isl_basic_map_copy(map
->p
[i
]));
5997 __isl_give isl_map
*isl_map_add_basic_map(__isl_take isl_map
*map
,
5998 __isl_take isl_basic_map
*bmap
)
6002 if (isl_basic_map_plain_is_empty(bmap
)) {
6003 isl_basic_map_free(bmap
);
6006 isl_assert(map
->ctx
, isl_space_is_equal(map
->dim
, bmap
->dim
), goto error
);
6007 isl_assert(map
->ctx
, map
->n
< map
->size
, goto error
);
6008 map
->p
[map
->n
] = bmap
;
6010 ISL_F_CLR(map
, ISL_MAP_NORMALIZED
);
6016 isl_basic_map_free(bmap
);
6020 __isl_null isl_map
*isl_map_free(__isl_take isl_map
*map
)
6031 isl_ctx_deref(map
->ctx
);
6032 for (i
= 0; i
< map
->n
; ++i
)
6033 isl_basic_map_free(map
->p
[i
]);
6034 isl_space_free(map
->dim
);
6040 static struct isl_basic_map
*isl_basic_map_fix_pos_si(
6041 struct isl_basic_map
*bmap
, unsigned pos
, int value
)
6045 bmap
= isl_basic_map_cow(bmap
);
6046 bmap
= isl_basic_map_extend_constraints(bmap
, 1, 0);
6047 j
= isl_basic_map_alloc_equality(bmap
);
6050 isl_seq_clr(bmap
->eq
[j
] + 1, isl_basic_map_total_dim(bmap
));
6051 isl_int_set_si(bmap
->eq
[j
][pos
], -1);
6052 isl_int_set_si(bmap
->eq
[j
][0], value
);
6053 bmap
= isl_basic_map_simplify(bmap
);
6054 return isl_basic_map_finalize(bmap
);
6056 isl_basic_map_free(bmap
);
6060 static __isl_give isl_basic_map
*isl_basic_map_fix_pos(
6061 __isl_take isl_basic_map
*bmap
, unsigned pos
, isl_int value
)
6065 bmap
= isl_basic_map_cow(bmap
);
6066 bmap
= isl_basic_map_extend_constraints(bmap
, 1, 0);
6067 j
= isl_basic_map_alloc_equality(bmap
);
6070 isl_seq_clr(bmap
->eq
[j
] + 1, isl_basic_map_total_dim(bmap
));
6071 isl_int_set_si(bmap
->eq
[j
][pos
], -1);
6072 isl_int_set(bmap
->eq
[j
][0], value
);
6073 bmap
= isl_basic_map_simplify(bmap
);
6074 return isl_basic_map_finalize(bmap
);
6076 isl_basic_map_free(bmap
);
6080 struct isl_basic_map
*isl_basic_map_fix_si(struct isl_basic_map
*bmap
,
6081 enum isl_dim_type type
, unsigned pos
, int value
)
6083 if (isl_basic_map_check_range(bmap
, type
, pos
, 1) < 0)
6084 return isl_basic_map_free(bmap
);
6085 return isl_basic_map_fix_pos_si(bmap
,
6086 isl_basic_map_offset(bmap
, type
) + pos
, value
);
6089 __isl_give isl_basic_map
*isl_basic_map_fix(__isl_take isl_basic_map
*bmap
,
6090 enum isl_dim_type type
, unsigned pos
, isl_int value
)
6092 if (isl_basic_map_check_range(bmap
, type
, pos
, 1) < 0)
6093 return isl_basic_map_free(bmap
);
6094 return isl_basic_map_fix_pos(bmap
,
6095 isl_basic_map_offset(bmap
, type
) + pos
, value
);
6098 /* Fix the value of the variable at position "pos" of type "type" of "bmap"
6099 * to be equal to "v".
6101 __isl_give isl_basic_map
*isl_basic_map_fix_val(__isl_take isl_basic_map
*bmap
,
6102 enum isl_dim_type type
, unsigned pos
, __isl_take isl_val
*v
)
6106 if (!isl_val_is_int(v
))
6107 isl_die(isl_basic_map_get_ctx(bmap
), isl_error_invalid
,
6108 "expecting integer value", goto error
);
6109 if (isl_basic_map_check_range(bmap
, type
, pos
, 1) < 0)
6111 pos
+= isl_basic_map_offset(bmap
, type
);
6112 bmap
= isl_basic_map_fix_pos(bmap
, pos
, v
->n
);
6116 isl_basic_map_free(bmap
);
6121 /* Fix the value of the variable at position "pos" of type "type" of "bset"
6122 * to be equal to "v".
6124 __isl_give isl_basic_set
*isl_basic_set_fix_val(__isl_take isl_basic_set
*bset
,
6125 enum isl_dim_type type
, unsigned pos
, __isl_take isl_val
*v
)
6127 return isl_basic_map_fix_val(bset
, type
, pos
, v
);
6130 struct isl_basic_set
*isl_basic_set_fix_si(struct isl_basic_set
*bset
,
6131 enum isl_dim_type type
, unsigned pos
, int value
)
6133 return bset_from_bmap(isl_basic_map_fix_si(bset_to_bmap(bset
),
6137 __isl_give isl_basic_set
*isl_basic_set_fix(__isl_take isl_basic_set
*bset
,
6138 enum isl_dim_type type
, unsigned pos
, isl_int value
)
6140 return bset_from_bmap(isl_basic_map_fix(bset_to_bmap(bset
),
6144 struct isl_basic_map
*isl_basic_map_fix_input_si(struct isl_basic_map
*bmap
,
6145 unsigned input
, int value
)
6147 return isl_basic_map_fix_si(bmap
, isl_dim_in
, input
, value
);
6150 struct isl_basic_set
*isl_basic_set_fix_dim_si(struct isl_basic_set
*bset
,
6151 unsigned dim
, int value
)
6153 return bset_from_bmap(isl_basic_map_fix_si(bset_to_bmap(bset
),
6154 isl_dim_set
, dim
, value
));
6157 static int remove_if_empty(__isl_keep isl_map
*map
, int i
)
6159 int empty
= isl_basic_map_plain_is_empty(map
->p
[i
]);
6166 isl_basic_map_free(map
->p
[i
]);
6167 if (i
!= map
->n
- 1) {
6168 ISL_F_CLR(map
, ISL_MAP_NORMALIZED
);
6169 map
->p
[i
] = map
->p
[map
->n
- 1];
6176 /* Perform "fn" on each basic map of "map", where we may not be holding
6177 * the only reference to "map".
6178 * In particular, "fn" should be a semantics preserving operation
6179 * that we want to apply to all copies of "map". We therefore need
6180 * to be careful not to modify "map" in a way that breaks "map"
6181 * in case anything goes wrong.
6183 __isl_give isl_map
*isl_map_inline_foreach_basic_map(__isl_take isl_map
*map
,
6184 __isl_give isl_basic_map
*(*fn
)(__isl_take isl_basic_map
*bmap
))
6186 struct isl_basic_map
*bmap
;
6192 for (i
= map
->n
- 1; i
>= 0; --i
) {
6193 bmap
= isl_basic_map_copy(map
->p
[i
]);
6197 isl_basic_map_free(map
->p
[i
]);
6199 if (remove_if_empty(map
, i
) < 0)
6209 struct isl_map
*isl_map_fix_si(struct isl_map
*map
,
6210 enum isl_dim_type type
, unsigned pos
, int value
)
6214 map
= isl_map_cow(map
);
6218 isl_assert(map
->ctx
, pos
< isl_map_dim(map
, type
), goto error
);
6219 for (i
= map
->n
- 1; i
>= 0; --i
) {
6220 map
->p
[i
] = isl_basic_map_fix_si(map
->p
[i
], type
, pos
, value
);
6221 if (remove_if_empty(map
, i
) < 0)
6224 ISL_F_CLR(map
, ISL_MAP_NORMALIZED
);
6231 __isl_give isl_set
*isl_set_fix_si(__isl_take isl_set
*set
,
6232 enum isl_dim_type type
, unsigned pos
, int value
)
6234 return set_from_map(isl_map_fix_si(set_to_map(set
), type
, pos
, value
));
6237 __isl_give isl_map
*isl_map_fix(__isl_take isl_map
*map
,
6238 enum isl_dim_type type
, unsigned pos
, isl_int value
)
6242 map
= isl_map_cow(map
);
6246 isl_assert(map
->ctx
, pos
< isl_map_dim(map
, type
), goto error
);
6247 for (i
= 0; i
< map
->n
; ++i
) {
6248 map
->p
[i
] = isl_basic_map_fix(map
->p
[i
], type
, pos
, value
);
6252 ISL_F_CLR(map
, ISL_MAP_NORMALIZED
);
6259 __isl_give isl_set
*isl_set_fix(__isl_take isl_set
*set
,
6260 enum isl_dim_type type
, unsigned pos
, isl_int value
)
6262 return set_from_map(isl_map_fix(set_to_map(set
), type
, pos
, value
));
6265 /* Fix the value of the variable at position "pos" of type "type" of "map"
6266 * to be equal to "v".
6268 __isl_give isl_map
*isl_map_fix_val(__isl_take isl_map
*map
,
6269 enum isl_dim_type type
, unsigned pos
, __isl_take isl_val
*v
)
6273 map
= isl_map_cow(map
);
6277 if (!isl_val_is_int(v
))
6278 isl_die(isl_map_get_ctx(map
), isl_error_invalid
,
6279 "expecting integer value", goto error
);
6280 if (pos
>= isl_map_dim(map
, type
))
6281 isl_die(isl_map_get_ctx(map
), isl_error_invalid
,
6282 "index out of bounds", goto error
);
6283 for (i
= map
->n
- 1; i
>= 0; --i
) {
6284 map
->p
[i
] = isl_basic_map_fix_val(map
->p
[i
], type
, pos
,
6286 if (remove_if_empty(map
, i
) < 0)
6289 ISL_F_CLR(map
, ISL_MAP_NORMALIZED
);
6298 /* Fix the value of the variable at position "pos" of type "type" of "set"
6299 * to be equal to "v".
6301 __isl_give isl_set
*isl_set_fix_val(__isl_take isl_set
*set
,
6302 enum isl_dim_type type
, unsigned pos
, __isl_take isl_val
*v
)
6304 return isl_map_fix_val(set
, type
, pos
, v
);
6307 struct isl_map
*isl_map_fix_input_si(struct isl_map
*map
,
6308 unsigned input
, int value
)
6310 return isl_map_fix_si(map
, isl_dim_in
, input
, value
);
6313 struct isl_set
*isl_set_fix_dim_si(struct isl_set
*set
, unsigned dim
, int value
)
6315 return set_from_map(isl_map_fix_si(set_to_map(set
),
6316 isl_dim_set
, dim
, value
));
6319 static __isl_give isl_basic_map
*basic_map_bound_si(
6320 __isl_take isl_basic_map
*bmap
,
6321 enum isl_dim_type type
, unsigned pos
, int value
, int upper
)
6325 if (isl_basic_map_check_range(bmap
, type
, pos
, 1) < 0)
6326 return isl_basic_map_free(bmap
);
6327 pos
+= isl_basic_map_offset(bmap
, type
);
6328 bmap
= isl_basic_map_cow(bmap
);
6329 bmap
= isl_basic_map_extend_constraints(bmap
, 0, 1);
6330 j
= isl_basic_map_alloc_inequality(bmap
);
6333 isl_seq_clr(bmap
->ineq
[j
], 1 + isl_basic_map_total_dim(bmap
));
6335 isl_int_set_si(bmap
->ineq
[j
][pos
], -1);
6336 isl_int_set_si(bmap
->ineq
[j
][0], value
);
6338 isl_int_set_si(bmap
->ineq
[j
][pos
], 1);
6339 isl_int_set_si(bmap
->ineq
[j
][0], -value
);
6341 bmap
= isl_basic_map_simplify(bmap
);
6342 return isl_basic_map_finalize(bmap
);
6344 isl_basic_map_free(bmap
);
6348 __isl_give isl_basic_map
*isl_basic_map_lower_bound_si(
6349 __isl_take isl_basic_map
*bmap
,
6350 enum isl_dim_type type
, unsigned pos
, int value
)
6352 return basic_map_bound_si(bmap
, type
, pos
, value
, 0);
6355 /* Constrain the values of the given dimension to be no greater than "value".
6357 __isl_give isl_basic_map
*isl_basic_map_upper_bound_si(
6358 __isl_take isl_basic_map
*bmap
,
6359 enum isl_dim_type type
, unsigned pos
, int value
)
6361 return basic_map_bound_si(bmap
, type
, pos
, value
, 1);
6364 static __isl_give isl_map
*map_bound_si(__isl_take isl_map
*map
,
6365 enum isl_dim_type type
, unsigned pos
, int value
, int upper
)
6369 map
= isl_map_cow(map
);
6373 isl_assert(map
->ctx
, pos
< isl_map_dim(map
, type
), goto error
);
6374 for (i
= 0; i
< map
->n
; ++i
) {
6375 map
->p
[i
] = basic_map_bound_si(map
->p
[i
],
6376 type
, pos
, value
, upper
);
6380 ISL_F_CLR(map
, ISL_MAP_NORMALIZED
);
6387 __isl_give isl_map
*isl_map_lower_bound_si(__isl_take isl_map
*map
,
6388 enum isl_dim_type type
, unsigned pos
, int value
)
6390 return map_bound_si(map
, type
, pos
, value
, 0);
6393 __isl_give isl_map
*isl_map_upper_bound_si(__isl_take isl_map
*map
,
6394 enum isl_dim_type type
, unsigned pos
, int value
)
6396 return map_bound_si(map
, type
, pos
, value
, 1);
6399 __isl_give isl_set
*isl_set_lower_bound_si(__isl_take isl_set
*set
,
6400 enum isl_dim_type type
, unsigned pos
, int value
)
6402 return set_from_map(isl_map_lower_bound_si(set_to_map(set
),
6406 __isl_give isl_set
*isl_set_upper_bound_si(__isl_take isl_set
*set
,
6407 enum isl_dim_type type
, unsigned pos
, int value
)
6409 return isl_map_upper_bound_si(set
, type
, pos
, value
);
6412 /* Bound the given variable of "bmap" from below (or above is "upper"
6413 * is set) to "value".
6415 static __isl_give isl_basic_map
*basic_map_bound(
6416 __isl_take isl_basic_map
*bmap
,
6417 enum isl_dim_type type
, unsigned pos
, isl_int value
, int upper
)
6421 if (isl_basic_map_check_range(bmap
, type
, pos
, 1) < 0)
6422 return isl_basic_map_free(bmap
);
6423 pos
+= isl_basic_map_offset(bmap
, type
);
6424 bmap
= isl_basic_map_cow(bmap
);
6425 bmap
= isl_basic_map_extend_constraints(bmap
, 0, 1);
6426 j
= isl_basic_map_alloc_inequality(bmap
);
6429 isl_seq_clr(bmap
->ineq
[j
], 1 + isl_basic_map_total_dim(bmap
));
6431 isl_int_set_si(bmap
->ineq
[j
][pos
], -1);
6432 isl_int_set(bmap
->ineq
[j
][0], value
);
6434 isl_int_set_si(bmap
->ineq
[j
][pos
], 1);
6435 isl_int_neg(bmap
->ineq
[j
][0], value
);
6437 bmap
= isl_basic_map_simplify(bmap
);
6438 return isl_basic_map_finalize(bmap
);
6440 isl_basic_map_free(bmap
);
6444 /* Bound the given variable of "map" from below (or above is "upper"
6445 * is set) to "value".
6447 static __isl_give isl_map
*map_bound(__isl_take isl_map
*map
,
6448 enum isl_dim_type type
, unsigned pos
, isl_int value
, int upper
)
6452 map
= isl_map_cow(map
);
6456 if (pos
>= isl_map_dim(map
, type
))
6457 isl_die(map
->ctx
, isl_error_invalid
,
6458 "index out of bounds", goto error
);
6459 for (i
= map
->n
- 1; i
>= 0; --i
) {
6460 map
->p
[i
] = basic_map_bound(map
->p
[i
], type
, pos
, value
, upper
);
6461 if (remove_if_empty(map
, i
) < 0)
6464 ISL_F_CLR(map
, ISL_MAP_NORMALIZED
);
6471 __isl_give isl_map
*isl_map_lower_bound(__isl_take isl_map
*map
,
6472 enum isl_dim_type type
, unsigned pos
, isl_int value
)
6474 return map_bound(map
, type
, pos
, value
, 0);
6477 __isl_give isl_map
*isl_map_upper_bound(__isl_take isl_map
*map
,
6478 enum isl_dim_type type
, unsigned pos
, isl_int value
)
6480 return map_bound(map
, type
, pos
, value
, 1);
6483 __isl_give isl_set
*isl_set_lower_bound(__isl_take isl_set
*set
,
6484 enum isl_dim_type type
, unsigned pos
, isl_int value
)
6486 return isl_map_lower_bound(set
, type
, pos
, value
);
6489 __isl_give isl_set
*isl_set_upper_bound(__isl_take isl_set
*set
,
6490 enum isl_dim_type type
, unsigned pos
, isl_int value
)
6492 return isl_map_upper_bound(set
, type
, pos
, value
);
6495 /* Force the values of the variable at position "pos" of type "type" of "set"
6496 * to be no smaller than "value".
6498 __isl_give isl_set
*isl_set_lower_bound_val(__isl_take isl_set
*set
,
6499 enum isl_dim_type type
, unsigned pos
, __isl_take isl_val
*value
)
6503 if (!isl_val_is_int(value
))
6504 isl_die(isl_set_get_ctx(set
), isl_error_invalid
,
6505 "expecting integer value", goto error
);
6506 set
= isl_set_lower_bound(set
, type
, pos
, value
->n
);
6507 isl_val_free(value
);
6510 isl_val_free(value
);
6515 /* Force the values of the variable at position "pos" of type "type" of "set"
6516 * to be no greater than "value".
6518 __isl_give isl_set
*isl_set_upper_bound_val(__isl_take isl_set
*set
,
6519 enum isl_dim_type type
, unsigned pos
, __isl_take isl_val
*value
)
6523 if (!isl_val_is_int(value
))
6524 isl_die(isl_set_get_ctx(set
), isl_error_invalid
,
6525 "expecting integer value", goto error
);
6526 set
= isl_set_upper_bound(set
, type
, pos
, value
->n
);
6527 isl_val_free(value
);
6530 isl_val_free(value
);
6535 struct isl_map
*isl_map_reverse(struct isl_map
*map
)
6539 map
= isl_map_cow(map
);
6543 map
->dim
= isl_space_reverse(map
->dim
);
6546 for (i
= 0; i
< map
->n
; ++i
) {
6547 map
->p
[i
] = isl_basic_map_reverse(map
->p
[i
]);
6551 ISL_F_CLR(map
, ISL_MAP_NORMALIZED
);
6559 #define TYPE isl_pw_multi_aff
6561 #define SUFFIX _pw_multi_aff
6563 #define EMPTY isl_pw_multi_aff_empty
6565 #define ADD isl_pw_multi_aff_union_add
6566 #include "isl_map_lexopt_templ.c"
6568 /* Given a map "map", compute the lexicographically minimal
6569 * (or maximal) image element for each domain element in dom,
6570 * in the form of an isl_pw_multi_aff.
6571 * If "empty" is not NULL, then set *empty to those elements in dom that
6572 * do not have an image element.
6573 * If "flags" includes ISL_OPT_FULL, then "dom" is NULL and the optimum
6574 * should be computed over the domain of "map". "empty" is also NULL
6577 * We first compute the lexicographically minimal or maximal element
6578 * in the first basic map. This results in a partial solution "res"
6579 * and a subset "todo" of dom that still need to be handled.
6580 * We then consider each of the remaining maps in "map" and successively
6581 * update both "res" and "todo".
6582 * If "empty" is NULL, then the todo sets are not needed and therefore
6583 * also not computed.
6585 static __isl_give isl_pw_multi_aff
*isl_map_partial_lexopt_aligned_pw_multi_aff(
6586 __isl_take isl_map
*map
, __isl_take isl_set
*dom
,
6587 __isl_give isl_set
**empty
, unsigned flags
)
6591 isl_pw_multi_aff
*res
;
6594 full
= ISL_FL_ISSET(flags
, ISL_OPT_FULL
);
6595 if (!map
|| (!full
&& !dom
))
6598 if (isl_map_plain_is_empty(map
)) {
6603 return isl_pw_multi_aff_from_map(map
);
6606 res
= basic_map_partial_lexopt_pw_multi_aff(
6607 isl_basic_map_copy(map
->p
[0]),
6608 isl_set_copy(dom
), empty
, flags
);
6612 for (i
= 1; i
< map
->n
; ++i
) {
6613 isl_pw_multi_aff
*res_i
;
6615 res_i
= basic_map_partial_lexopt_pw_multi_aff(
6616 isl_basic_map_copy(map
->p
[i
]),
6617 isl_set_copy(dom
), empty
, flags
);
6619 if (ISL_FL_ISSET(flags
, ISL_OPT_MAX
))
6620 res
= isl_pw_multi_aff_union_lexmax(res
, res_i
);
6622 res
= isl_pw_multi_aff_union_lexmin(res
, res_i
);
6625 todo
= isl_set_intersect(todo
, *empty
);
6644 #define TYPE isl_map
6648 #define EMPTY isl_map_empty
6650 #define ADD isl_map_union_disjoint
6651 #include "isl_map_lexopt_templ.c"
6653 /* Given a map "map", compute the lexicographically minimal
6654 * (or maximal) image element for each domain element in "dom",
6655 * in the form of an isl_map.
6656 * If "empty" is not NULL, then set *empty to those elements in "dom" that
6657 * do not have an image element.
6658 * If "flags" includes ISL_OPT_FULL, then "dom" is NULL and the optimum
6659 * should be computed over the domain of "map". "empty" is also NULL
6662 * If the input consists of more than one disjunct, then first
6663 * compute the desired result in the form of an isl_pw_multi_aff and
6664 * then convert that into an isl_map.
6666 * This function used to have an explicit implementation in terms
6667 * of isl_maps, but it would continually intersect the domains of
6668 * partial results with the complement of the domain of the next
6669 * partial solution, potentially leading to an explosion in the number
6670 * of disjuncts if there are several disjuncts in the input.
6671 * An even earlier implementation of this function would look for
6672 * better results in the domain of the partial result and for extra
6673 * results in the complement of this domain, which would lead to
6674 * even more splintering.
6676 static __isl_give isl_map
*isl_map_partial_lexopt_aligned(
6677 __isl_take isl_map
*map
, __isl_take isl_set
*dom
,
6678 __isl_give isl_set
**empty
, unsigned flags
)
6681 struct isl_map
*res
;
6682 isl_pw_multi_aff
*pma
;
6684 full
= ISL_FL_ISSET(flags
, ISL_OPT_FULL
);
6685 if (!map
|| (!full
&& !dom
))
6688 if (isl_map_plain_is_empty(map
)) {
6697 res
= basic_map_partial_lexopt(isl_basic_map_copy(map
->p
[0]),
6703 pma
= isl_map_partial_lexopt_aligned_pw_multi_aff(map
, dom
, empty
,
6705 return isl_map_from_pw_multi_aff(pma
);
6714 __isl_give isl_map
*isl_map_partial_lexmax(
6715 __isl_take isl_map
*map
, __isl_take isl_set
*dom
,
6716 __isl_give isl_set
**empty
)
6718 return isl_map_partial_lexopt(map
, dom
, empty
, ISL_OPT_MAX
);
6721 __isl_give isl_map
*isl_map_partial_lexmin(
6722 __isl_take isl_map
*map
, __isl_take isl_set
*dom
,
6723 __isl_give isl_set
**empty
)
6725 return isl_map_partial_lexopt(map
, dom
, empty
, 0);
6728 __isl_give isl_set
*isl_set_partial_lexmin(
6729 __isl_take isl_set
*set
, __isl_take isl_set
*dom
,
6730 __isl_give isl_set
**empty
)
6732 return set_from_map(isl_map_partial_lexmin(set_to_map(set
),
6736 __isl_give isl_set
*isl_set_partial_lexmax(
6737 __isl_take isl_set
*set
, __isl_take isl_set
*dom
,
6738 __isl_give isl_set
**empty
)
6740 return set_from_map(isl_map_partial_lexmax(set_to_map(set
),
6744 /* Compute the lexicographic minimum (or maximum if "flags" includes
6745 * ISL_OPT_MAX) of "bset" over its parametric domain.
6747 __isl_give isl_set
*isl_basic_set_lexopt(__isl_take isl_basic_set
*bset
,
6750 return isl_basic_map_lexopt(bset
, flags
);
6753 __isl_give isl_map
*isl_basic_map_lexmax(__isl_take isl_basic_map
*bmap
)
6755 return isl_basic_map_lexopt(bmap
, ISL_OPT_MAX
);
6758 __isl_give isl_set
*isl_basic_set_lexmin(__isl_take isl_basic_set
*bset
)
6760 return set_from_map(isl_basic_map_lexmin(bset_to_bmap(bset
)));
6763 __isl_give isl_set
*isl_basic_set_lexmax(__isl_take isl_basic_set
*bset
)
6765 return set_from_map(isl_basic_map_lexmax(bset_to_bmap(bset
)));
6768 /* Compute the lexicographic minimum of "bset" over its parametric domain
6769 * for the purpose of quantifier elimination.
6770 * That is, find an explicit representation for all the existentially
6771 * quantified variables in "bset" by computing their lexicographic
6774 static __isl_give isl_set
*isl_basic_set_lexmin_compute_divs(
6775 __isl_take isl_basic_set
*bset
)
6777 return isl_basic_set_lexopt(bset
, ISL_OPT_QE
);
6780 /* Extract the first and only affine expression from list
6781 * and then add it to *pwaff with the given dom.
6782 * This domain is known to be disjoint from other domains
6783 * because of the way isl_basic_map_foreach_lexmax works.
6785 static isl_stat
update_dim_opt(__isl_take isl_basic_set
*dom
,
6786 __isl_take isl_aff_list
*list
, void *user
)
6788 isl_ctx
*ctx
= isl_basic_set_get_ctx(dom
);
6790 isl_pw_aff
**pwaff
= user
;
6791 isl_pw_aff
*pwaff_i
;
6795 if (isl_aff_list_n_aff(list
) != 1)
6796 isl_die(ctx
, isl_error_internal
,
6797 "expecting single element list", goto error
);
6799 aff
= isl_aff_list_get_aff(list
, 0);
6800 pwaff_i
= isl_pw_aff_alloc(isl_set_from_basic_set(dom
), aff
);
6802 *pwaff
= isl_pw_aff_add_disjoint(*pwaff
, pwaff_i
);
6804 isl_aff_list_free(list
);
6808 isl_basic_set_free(dom
);
6809 isl_aff_list_free(list
);
6810 return isl_stat_error
;
6813 /* Given a basic map with one output dimension, compute the minimum or
6814 * maximum of that dimension as an isl_pw_aff.
6816 * The isl_pw_aff is constructed by having isl_basic_map_foreach_lexopt
6817 * call update_dim_opt on each leaf of the result.
6819 static __isl_give isl_pw_aff
*basic_map_dim_opt(__isl_keep isl_basic_map
*bmap
,
6822 isl_space
*dim
= isl_basic_map_get_space(bmap
);
6826 dim
= isl_space_from_domain(isl_space_domain(dim
));
6827 dim
= isl_space_add_dims(dim
, isl_dim_out
, 1);
6828 pwaff
= isl_pw_aff_empty(dim
);
6830 r
= isl_basic_map_foreach_lexopt(bmap
, max
, &update_dim_opt
, &pwaff
);
6832 return isl_pw_aff_free(pwaff
);
6837 /* Compute the minimum or maximum of the given output dimension
6838 * as a function of the parameters and the input dimensions,
6839 * but independently of the other output dimensions.
6841 * We first project out the other output dimension and then compute
6842 * the "lexicographic" maximum in each basic map, combining the results
6843 * using isl_pw_aff_union_max.
6845 static __isl_give isl_pw_aff
*map_dim_opt(__isl_take isl_map
*map
, int pos
,
6852 n_out
= isl_map_dim(map
, isl_dim_out
);
6853 map
= isl_map_project_out(map
, isl_dim_out
, pos
+ 1, n_out
- (pos
+ 1));
6854 map
= isl_map_project_out(map
, isl_dim_out
, 0, pos
);
6859 isl_space
*dim
= isl_map_get_space(map
);
6861 return isl_pw_aff_empty(dim
);
6864 pwaff
= basic_map_dim_opt(map
->p
[0], max
);
6865 for (i
= 1; i
< map
->n
; ++i
) {
6866 isl_pw_aff
*pwaff_i
;
6868 pwaff_i
= basic_map_dim_opt(map
->p
[i
], max
);
6869 pwaff
= isl_pw_aff_union_opt(pwaff
, pwaff_i
, max
);
6877 /* Compute the minimum of the given output dimension as a function of the
6878 * parameters and input dimensions, but independently of
6879 * the other output dimensions.
6881 __isl_give isl_pw_aff
*isl_map_dim_min(__isl_take isl_map
*map
, int pos
)
6883 return map_dim_opt(map
, pos
, 0);
6886 /* Compute the maximum of the given output dimension as a function of the
6887 * parameters and input dimensions, but independently of
6888 * the other output dimensions.
6890 __isl_give isl_pw_aff
*isl_map_dim_max(__isl_take isl_map
*map
, int pos
)
6892 return map_dim_opt(map
, pos
, 1);
6895 /* Compute the minimum or maximum of the given set dimension
6896 * as a function of the parameters,
6897 * but independently of the other set dimensions.
6899 static __isl_give isl_pw_aff
*set_dim_opt(__isl_take isl_set
*set
, int pos
,
6902 return map_dim_opt(set
, pos
, max
);
6905 /* Compute the maximum of the given set dimension as a function of the
6906 * parameters, but independently of the other set dimensions.
6908 __isl_give isl_pw_aff
*isl_set_dim_max(__isl_take isl_set
*set
, int pos
)
6910 return set_dim_opt(set
, pos
, 1);
6913 /* Compute the minimum of the given set dimension as a function of the
6914 * parameters, but independently of the other set dimensions.
6916 __isl_give isl_pw_aff
*isl_set_dim_min(__isl_take isl_set
*set
, int pos
)
6918 return set_dim_opt(set
, pos
, 0);
6921 /* Apply a preimage specified by "mat" on the parameters of "bset".
6922 * bset is assumed to have only parameters and divs.
6924 static __isl_give isl_basic_set
*basic_set_parameter_preimage(
6925 __isl_take isl_basic_set
*bset
, __isl_take isl_mat
*mat
)
6932 bset
->dim
= isl_space_cow(bset
->dim
);
6936 nparam
= isl_basic_set_dim(bset
, isl_dim_param
);
6938 isl_assert(bset
->ctx
, mat
->n_row
== 1 + nparam
, goto error
);
6940 bset
->dim
->nparam
= 0;
6941 bset
->dim
->n_out
= nparam
;
6942 bset
= isl_basic_set_preimage(bset
, mat
);
6944 bset
->dim
->nparam
= bset
->dim
->n_out
;
6945 bset
->dim
->n_out
= 0;
6950 isl_basic_set_free(bset
);
6954 /* Apply a preimage specified by "mat" on the parameters of "set".
6955 * set is assumed to have only parameters and divs.
6957 static __isl_give isl_set
*set_parameter_preimage(__isl_take isl_set
*set
,
6958 __isl_take isl_mat
*mat
)
6966 nparam
= isl_set_dim(set
, isl_dim_param
);
6968 if (mat
->n_row
!= 1 + nparam
)
6969 isl_die(isl_set_get_ctx(set
), isl_error_internal
,
6970 "unexpected number of rows", goto error
);
6972 space
= isl_set_get_space(set
);
6973 space
= isl_space_move_dims(space
, isl_dim_set
, 0,
6974 isl_dim_param
, 0, nparam
);
6975 set
= isl_set_reset_space(set
, space
);
6976 set
= isl_set_preimage(set
, mat
);
6977 nparam
= isl_set_dim(set
, isl_dim_out
);
6978 space
= isl_set_get_space(set
);
6979 space
= isl_space_move_dims(space
, isl_dim_param
, 0,
6980 isl_dim_out
, 0, nparam
);
6981 set
= isl_set_reset_space(set
, space
);
6989 /* Intersect the basic set "bset" with the affine space specified by the
6990 * equalities in "eq".
6992 static __isl_give isl_basic_set
*basic_set_append_equalities(
6993 __isl_take isl_basic_set
*bset
, __isl_take isl_mat
*eq
)
7001 bset
= isl_basic_set_extend_space(bset
, isl_space_copy(bset
->dim
), 0,
7006 len
= 1 + isl_space_dim(bset
->dim
, isl_dim_all
) + bset
->extra
;
7007 for (i
= 0; i
< eq
->n_row
; ++i
) {
7008 k
= isl_basic_set_alloc_equality(bset
);
7011 isl_seq_cpy(bset
->eq
[k
], eq
->row
[i
], eq
->n_col
);
7012 isl_seq_clr(bset
->eq
[k
] + eq
->n_col
, len
- eq
->n_col
);
7016 bset
= isl_basic_set_gauss(bset
, NULL
);
7017 bset
= isl_basic_set_finalize(bset
);
7022 isl_basic_set_free(bset
);
7026 /* Intersect the set "set" with the affine space specified by the
7027 * equalities in "eq".
7029 static struct isl_set
*set_append_equalities(struct isl_set
*set
,
7037 for (i
= 0; i
< set
->n
; ++i
) {
7038 set
->p
[i
] = basic_set_append_equalities(set
->p
[i
],
7051 /* Given a basic set "bset" that only involves parameters and existentially
7052 * quantified variables, return the index of the first equality
7053 * that only involves parameters. If there is no such equality then
7054 * return bset->n_eq.
7056 * This function assumes that isl_basic_set_gauss has been called on "bset".
7058 static int first_parameter_equality(__isl_keep isl_basic_set
*bset
)
7061 unsigned nparam
, n_div
;
7066 nparam
= isl_basic_set_dim(bset
, isl_dim_param
);
7067 n_div
= isl_basic_set_dim(bset
, isl_dim_div
);
7069 for (i
= 0, j
= n_div
- 1; i
< bset
->n_eq
&& j
>= 0; --j
) {
7070 if (!isl_int_is_zero(bset
->eq
[i
][1 + nparam
+ j
]))
7077 /* Compute an explicit representation for the existentially quantified
7078 * variables in "bset" by computing the "minimal value" of the set
7079 * variables. Since there are no set variables, the computation of
7080 * the minimal value essentially computes an explicit representation
7081 * of the non-empty part(s) of "bset".
7083 * The input only involves parameters and existentially quantified variables.
7084 * All equalities among parameters have been removed.
7086 * Since the existentially quantified variables in the result are in general
7087 * going to be different from those in the input, we first replace
7088 * them by the minimal number of variables based on their equalities.
7089 * This should simplify the parametric integer programming.
7091 static __isl_give isl_set
*base_compute_divs(__isl_take isl_basic_set
*bset
)
7093 isl_morph
*morph1
, *morph2
;
7099 if (bset
->n_eq
== 0)
7100 return isl_basic_set_lexmin_compute_divs(bset
);
7102 morph1
= isl_basic_set_parameter_compression(bset
);
7103 bset
= isl_morph_basic_set(isl_morph_copy(morph1
), bset
);
7104 bset
= isl_basic_set_lift(bset
);
7105 morph2
= isl_basic_set_variable_compression(bset
, isl_dim_set
);
7106 bset
= isl_morph_basic_set(morph2
, bset
);
7107 n
= isl_basic_set_dim(bset
, isl_dim_set
);
7108 bset
= isl_basic_set_project_out(bset
, isl_dim_set
, 0, n
);
7110 set
= isl_basic_set_lexmin_compute_divs(bset
);
7112 set
= isl_morph_set(isl_morph_inverse(morph1
), set
);
7117 /* Project the given basic set onto its parameter domain, possibly introducing
7118 * new, explicit, existential variables in the constraints.
7119 * The input has parameters and (possibly implicit) existential variables.
7120 * The output has the same parameters, but only
7121 * explicit existentially quantified variables.
7123 * The actual projection is performed by pip, but pip doesn't seem
7124 * to like equalities very much, so we first remove the equalities
7125 * among the parameters by performing a variable compression on
7126 * the parameters. Afterward, an inverse transformation is performed
7127 * and the equalities among the parameters are inserted back in.
7129 * The variable compression on the parameters may uncover additional
7130 * equalities that were only implicit before. We therefore check
7131 * if there are any new parameter equalities in the result and
7132 * if so recurse. The removal of parameter equalities is required
7133 * for the parameter compression performed by base_compute_divs.
7135 static struct isl_set
*parameter_compute_divs(struct isl_basic_set
*bset
)
7139 struct isl_mat
*T
, *T2
;
7140 struct isl_set
*set
;
7143 bset
= isl_basic_set_cow(bset
);
7147 if (bset
->n_eq
== 0)
7148 return base_compute_divs(bset
);
7150 bset
= isl_basic_set_gauss(bset
, NULL
);
7153 if (isl_basic_set_plain_is_empty(bset
))
7154 return isl_set_from_basic_set(bset
);
7156 i
= first_parameter_equality(bset
);
7157 if (i
== bset
->n_eq
)
7158 return base_compute_divs(bset
);
7160 nparam
= isl_basic_set_dim(bset
, isl_dim_param
);
7161 eq
= isl_mat_sub_alloc6(bset
->ctx
, bset
->eq
, i
, bset
->n_eq
- i
,
7163 eq
= isl_mat_cow(eq
);
7164 T
= isl_mat_variable_compression(isl_mat_copy(eq
), &T2
);
7165 if (T
&& T
->n_col
== 0) {
7169 bset
= isl_basic_set_set_to_empty(bset
);
7170 return isl_set_from_basic_set(bset
);
7172 bset
= basic_set_parameter_preimage(bset
, T
);
7174 i
= first_parameter_equality(bset
);
7177 else if (i
== bset
->n_eq
)
7178 set
= base_compute_divs(bset
);
7180 set
= parameter_compute_divs(bset
);
7181 set
= set_parameter_preimage(set
, T2
);
7182 set
= set_append_equalities(set
, eq
);
7186 /* Insert the divs from "ls" before those of "bmap".
7188 * The number of columns is not changed, which means that the last
7189 * dimensions of "bmap" are being reintepreted as the divs from "ls".
7190 * The caller is responsible for removing the same number of dimensions
7191 * from the space of "bmap".
7193 static __isl_give isl_basic_map
*insert_divs_from_local_space(
7194 __isl_take isl_basic_map
*bmap
, __isl_keep isl_local_space
*ls
)
7200 n_div
= isl_local_space_dim(ls
, isl_dim_div
);
7204 old_n_div
= bmap
->n_div
;
7205 bmap
= insert_div_rows(bmap
, n_div
);
7209 for (i
= 0; i
< n_div
; ++i
) {
7210 isl_seq_cpy(bmap
->div
[i
], ls
->div
->row
[i
], ls
->div
->n_col
);
7211 isl_seq_clr(bmap
->div
[i
] + ls
->div
->n_col
, old_n_div
);
7217 /* Replace the space of "bmap" by the space and divs of "ls".
7219 * If "ls" has any divs, then we simplify the result since we may
7220 * have discovered some additional equalities that could simplify
7221 * the div expressions.
7223 static __isl_give isl_basic_map
*basic_replace_space_by_local_space(
7224 __isl_take isl_basic_map
*bmap
, __isl_take isl_local_space
*ls
)
7228 bmap
= isl_basic_map_cow(bmap
);
7232 n_div
= isl_local_space_dim(ls
, isl_dim_div
);
7233 bmap
= insert_divs_from_local_space(bmap
, ls
);
7237 isl_space_free(bmap
->dim
);
7238 bmap
->dim
= isl_local_space_get_space(ls
);
7242 isl_local_space_free(ls
);
7244 bmap
= isl_basic_map_simplify(bmap
);
7245 bmap
= isl_basic_map_finalize(bmap
);
7248 isl_basic_map_free(bmap
);
7249 isl_local_space_free(ls
);
7253 /* Replace the space of "map" by the space and divs of "ls".
7255 static __isl_give isl_map
*replace_space_by_local_space(__isl_take isl_map
*map
,
7256 __isl_take isl_local_space
*ls
)
7260 map
= isl_map_cow(map
);
7264 for (i
= 0; i
< map
->n
; ++i
) {
7265 map
->p
[i
] = basic_replace_space_by_local_space(map
->p
[i
],
7266 isl_local_space_copy(ls
));
7270 isl_space_free(map
->dim
);
7271 map
->dim
= isl_local_space_get_space(ls
);
7275 isl_local_space_free(ls
);
7278 isl_local_space_free(ls
);
7283 /* Compute an explicit representation for the existentially
7284 * quantified variables for which do not know any explicit representation yet.
7286 * We first sort the existentially quantified variables so that the
7287 * existentially quantified variables for which we already have an explicit
7288 * representation are placed before those for which we do not.
7289 * The input dimensions, the output dimensions and the existentially
7290 * quantified variables for which we already have an explicit
7291 * representation are then turned into parameters.
7292 * compute_divs returns a map with the same parameters and
7293 * no input or output dimensions and the dimension specification
7294 * is reset to that of the input, including the existentially quantified
7295 * variables for which we already had an explicit representation.
7297 static struct isl_map
*compute_divs(struct isl_basic_map
*bmap
)
7299 struct isl_basic_set
*bset
;
7300 struct isl_set
*set
;
7301 struct isl_map
*map
;
7303 isl_local_space
*ls
;
7310 bmap
= isl_basic_map_sort_divs(bmap
);
7311 bmap
= isl_basic_map_cow(bmap
);
7315 n_known
= isl_basic_map_first_unknown_div(bmap
);
7317 return isl_map_from_basic_map(isl_basic_map_free(bmap
));
7319 nparam
= isl_basic_map_dim(bmap
, isl_dim_param
);
7320 n_in
= isl_basic_map_dim(bmap
, isl_dim_in
);
7321 n_out
= isl_basic_map_dim(bmap
, isl_dim_out
);
7322 dim
= isl_space_set_alloc(bmap
->ctx
,
7323 nparam
+ n_in
+ n_out
+ n_known
, 0);
7327 ls
= isl_basic_map_get_local_space(bmap
);
7328 ls
= isl_local_space_drop_dims(ls
, isl_dim_div
,
7329 n_known
, bmap
->n_div
- n_known
);
7331 for (i
= n_known
; i
< bmap
->n_div
; ++i
)
7332 swap_div(bmap
, i
- n_known
, i
);
7333 bmap
->n_div
-= n_known
;
7334 bmap
->extra
-= n_known
;
7336 bmap
= isl_basic_map_reset_space(bmap
, dim
);
7337 bset
= bset_from_bmap(bmap
);
7339 set
= parameter_compute_divs(bset
);
7340 map
= set_to_map(set
);
7341 map
= replace_space_by_local_space(map
, ls
);
7345 isl_basic_map_free(bmap
);
7349 /* Remove the explicit representation of local variable "div",
7352 __isl_give isl_basic_map
*isl_basic_map_mark_div_unknown(
7353 __isl_take isl_basic_map
*bmap
, int div
)
7357 unknown
= isl_basic_map_div_is_marked_unknown(bmap
, div
);
7359 return isl_basic_map_free(bmap
);
7363 bmap
= isl_basic_map_cow(bmap
);
7366 isl_int_set_si(bmap
->div
[div
][0], 0);
7370 /* Is local variable "div" of "bmap" marked as not having an explicit
7372 * Note that even if "div" is not marked in this way and therefore
7373 * has an explicit representation, this representation may still
7374 * depend (indirectly) on other local variables that do not
7375 * have an explicit representation.
7377 isl_bool
isl_basic_map_div_is_marked_unknown(__isl_keep isl_basic_map
*bmap
,
7380 if (isl_basic_map_check_range(bmap
, isl_dim_div
, div
, 1) < 0)
7381 return isl_bool_error
;
7382 return isl_int_is_zero(bmap
->div
[div
][0]);
7385 /* Return the position of the first local variable that does not
7386 * have an explicit representation.
7387 * Return the total number of local variables if they all have
7388 * an explicit representation.
7389 * Return -1 on error.
7391 int isl_basic_map_first_unknown_div(__isl_keep isl_basic_map
*bmap
)
7398 for (i
= 0; i
< bmap
->n_div
; ++i
) {
7399 if (!isl_basic_map_div_is_known(bmap
, i
))
7405 /* Return the position of the first local variable that does not
7406 * have an explicit representation.
7407 * Return the total number of local variables if they all have
7408 * an explicit representation.
7409 * Return -1 on error.
7411 int isl_basic_set_first_unknown_div(__isl_keep isl_basic_set
*bset
)
7413 return isl_basic_map_first_unknown_div(bset
);
7416 /* Does "bmap" have an explicit representation for all local variables?
7418 isl_bool
isl_basic_map_divs_known(__isl_keep isl_basic_map
*bmap
)
7422 n
= isl_basic_map_dim(bmap
, isl_dim_div
);
7423 first
= isl_basic_map_first_unknown_div(bmap
);
7425 return isl_bool_error
;
7429 /* Do all basic maps in "map" have an explicit representation
7430 * for all local variables?
7432 isl_bool
isl_map_divs_known(__isl_keep isl_map
*map
)
7437 return isl_bool_error
;
7439 for (i
= 0; i
< map
->n
; ++i
) {
7440 int known
= isl_basic_map_divs_known(map
->p
[i
]);
7445 return isl_bool_true
;
7448 /* If bmap contains any unknown divs, then compute explicit
7449 * expressions for them. However, this computation may be
7450 * quite expensive, so first try to remove divs that aren't
7453 struct isl_map
*isl_basic_map_compute_divs(struct isl_basic_map
*bmap
)
7456 struct isl_map
*map
;
7458 known
= isl_basic_map_divs_known(bmap
);
7462 return isl_map_from_basic_map(bmap
);
7464 bmap
= isl_basic_map_drop_redundant_divs(bmap
);
7466 known
= isl_basic_map_divs_known(bmap
);
7470 return isl_map_from_basic_map(bmap
);
7472 map
= compute_divs(bmap
);
7475 isl_basic_map_free(bmap
);
7479 struct isl_map
*isl_map_compute_divs(struct isl_map
*map
)
7483 struct isl_map
*res
;
7490 known
= isl_map_divs_known(map
);
7498 res
= isl_basic_map_compute_divs(isl_basic_map_copy(map
->p
[0]));
7499 for (i
= 1 ; i
< map
->n
; ++i
) {
7501 r2
= isl_basic_map_compute_divs(isl_basic_map_copy(map
->p
[i
]));
7502 if (ISL_F_ISSET(map
, ISL_MAP_DISJOINT
))
7503 res
= isl_map_union_disjoint(res
, r2
);
7505 res
= isl_map_union(res
, r2
);
7512 struct isl_set
*isl_basic_set_compute_divs(struct isl_basic_set
*bset
)
7514 return set_from_map(isl_basic_map_compute_divs(bset_to_bmap(bset
)));
7517 struct isl_set
*isl_set_compute_divs(struct isl_set
*set
)
7519 return set_from_map(isl_map_compute_divs(set_to_map(set
)));
7522 struct isl_set
*isl_map_domain(struct isl_map
*map
)
7525 struct isl_set
*set
;
7530 map
= isl_map_cow(map
);
7534 set
= set_from_map(map
);
7535 set
->dim
= isl_space_domain(set
->dim
);
7538 for (i
= 0; i
< map
->n
; ++i
) {
7539 set
->p
[i
] = isl_basic_map_domain(map
->p
[i
]);
7543 ISL_F_CLR(set
, ISL_MAP_DISJOINT
);
7544 ISL_F_CLR(set
, ISL_SET_NORMALIZED
);
7551 /* Return the union of "map1" and "map2", where we assume for now that
7552 * "map1" and "map2" are disjoint. Note that the basic maps inside
7553 * "map1" or "map2" may not be disjoint from each other.
7554 * Also note that this function is also called from isl_map_union,
7555 * which takes care of handling the situation where "map1" and "map2"
7556 * may not be disjoint.
7558 * If one of the inputs is empty, we can simply return the other input.
7559 * Similarly, if one of the inputs is universal, then it is equal to the union.
7561 static __isl_give isl_map
*map_union_disjoint(__isl_take isl_map
*map1
,
7562 __isl_take isl_map
*map2
)
7566 struct isl_map
*map
= NULL
;
7572 if (!isl_space_is_equal(map1
->dim
, map2
->dim
))
7573 isl_die(isl_map_get_ctx(map1
), isl_error_invalid
,
7574 "spaces don't match", goto error
);
7585 is_universe
= isl_map_plain_is_universe(map1
);
7586 if (is_universe
< 0)
7593 is_universe
= isl_map_plain_is_universe(map2
);
7594 if (is_universe
< 0)
7601 if (ISL_F_ISSET(map1
, ISL_MAP_DISJOINT
) &&
7602 ISL_F_ISSET(map2
, ISL_MAP_DISJOINT
))
7603 ISL_FL_SET(flags
, ISL_MAP_DISJOINT
);
7605 map
= isl_map_alloc_space(isl_space_copy(map1
->dim
),
7606 map1
->n
+ map2
->n
, flags
);
7609 for (i
= 0; i
< map1
->n
; ++i
) {
7610 map
= isl_map_add_basic_map(map
,
7611 isl_basic_map_copy(map1
->p
[i
]));
7615 for (i
= 0; i
< map2
->n
; ++i
) {
7616 map
= isl_map_add_basic_map(map
,
7617 isl_basic_map_copy(map2
->p
[i
]));
7631 /* Return the union of "map1" and "map2", where "map1" and "map2" are
7632 * guaranteed to be disjoint by the caller.
7634 * Note that this functions is called from within isl_map_make_disjoint,
7635 * so we have to be careful not to touch the constraints of the inputs
7638 __isl_give isl_map
*isl_map_union_disjoint(__isl_take isl_map
*map1
,
7639 __isl_take isl_map
*map2
)
7641 return isl_map_align_params_map_map_and(map1
, map2
, &map_union_disjoint
);
7644 /* Return the union of "map1" and "map2", where "map1" and "map2" may
7645 * not be disjoint. The parameters are assumed to have been aligned.
7647 * We currently simply call map_union_disjoint, the internal operation
7648 * of which does not really depend on the inputs being disjoint.
7649 * If the result contains more than one basic map, then we clear
7650 * the disjoint flag since the result may contain basic maps from
7651 * both inputs and these are not guaranteed to be disjoint.
7653 * As a special case, if "map1" and "map2" are obviously equal,
7654 * then we simply return "map1".
7656 static __isl_give isl_map
*map_union_aligned(__isl_take isl_map
*map1
,
7657 __isl_take isl_map
*map2
)
7664 equal
= isl_map_plain_is_equal(map1
, map2
);
7672 map1
= map_union_disjoint(map1
, map2
);
7676 ISL_F_CLR(map1
, ISL_MAP_DISJOINT
);
7684 /* Return the union of "map1" and "map2", where "map1" and "map2" may
7687 __isl_give isl_map
*isl_map_union(__isl_take isl_map
*map1
,
7688 __isl_take isl_map
*map2
)
7690 return isl_map_align_params_map_map_and(map1
, map2
, &map_union_aligned
);
7693 struct isl_set
*isl_set_union_disjoint(
7694 struct isl_set
*set1
, struct isl_set
*set2
)
7696 return set_from_map(isl_map_union_disjoint(set_to_map(set1
),
7700 struct isl_set
*isl_set_union(struct isl_set
*set1
, struct isl_set
*set2
)
7702 return set_from_map(isl_map_union(set_to_map(set1
), set_to_map(set2
)));
7705 /* Apply "fn" to pairs of elements from "map" and "set" and collect
7708 * "map" and "set" are assumed to be compatible and non-NULL.
7710 static __isl_give isl_map
*map_intersect_set(__isl_take isl_map
*map
,
7711 __isl_take isl_set
*set
,
7712 __isl_give isl_basic_map
*fn(__isl_take isl_basic_map
*bmap
,
7713 __isl_take isl_basic_set
*bset
))
7716 struct isl_map
*result
;
7719 if (isl_set_plain_is_universe(set
)) {
7724 if (ISL_F_ISSET(map
, ISL_MAP_DISJOINT
) &&
7725 ISL_F_ISSET(set
, ISL_MAP_DISJOINT
))
7726 ISL_FL_SET(flags
, ISL_MAP_DISJOINT
);
7728 result
= isl_map_alloc_space(isl_space_copy(map
->dim
),
7729 map
->n
* set
->n
, flags
);
7730 for (i
= 0; result
&& i
< map
->n
; ++i
)
7731 for (j
= 0; j
< set
->n
; ++j
) {
7732 result
= isl_map_add_basic_map(result
,
7733 fn(isl_basic_map_copy(map
->p
[i
]),
7734 isl_basic_set_copy(set
->p
[j
])));
7744 static __isl_give isl_map
*map_intersect_range(__isl_take isl_map
*map
,
7745 __isl_take isl_set
*set
)
7749 ok
= isl_map_compatible_range(map
, set
);
7753 isl_die(set
->ctx
, isl_error_invalid
,
7754 "incompatible spaces", goto error
);
7756 return map_intersect_set(map
, set
, &isl_basic_map_intersect_range
);
7763 __isl_give isl_map
*isl_map_intersect_range(__isl_take isl_map
*map
,
7764 __isl_take isl_set
*set
)
7766 return isl_map_align_params_map_map_and(map
, set
, &map_intersect_range
);
7769 static __isl_give isl_map
*map_intersect_domain(__isl_take isl_map
*map
,
7770 __isl_take isl_set
*set
)
7774 ok
= isl_map_compatible_domain(map
, set
);
7778 isl_die(set
->ctx
, isl_error_invalid
,
7779 "incompatible spaces", goto error
);
7781 return map_intersect_set(map
, set
, &isl_basic_map_intersect_domain
);
7788 __isl_give isl_map
*isl_map_intersect_domain(__isl_take isl_map
*map
,
7789 __isl_take isl_set
*set
)
7791 return isl_map_align_params_map_map_and(map
, set
,
7792 &map_intersect_domain
);
7795 static __isl_give isl_map
*map_apply_domain(__isl_take isl_map
*map1
,
7796 __isl_take isl_map
*map2
)
7800 map1
= isl_map_reverse(map1
);
7801 map1
= isl_map_apply_range(map1
, map2
);
7802 return isl_map_reverse(map1
);
7809 __isl_give isl_map
*isl_map_apply_domain(__isl_take isl_map
*map1
,
7810 __isl_take isl_map
*map2
)
7812 return isl_map_align_params_map_map_and(map1
, map2
, &map_apply_domain
);
7815 static __isl_give isl_map
*map_apply_range(__isl_take isl_map
*map1
,
7816 __isl_take isl_map
*map2
)
7818 isl_space
*dim_result
;
7819 struct isl_map
*result
;
7825 dim_result
= isl_space_join(isl_space_copy(map1
->dim
),
7826 isl_space_copy(map2
->dim
));
7828 result
= isl_map_alloc_space(dim_result
, map1
->n
* map2
->n
, 0);
7831 for (i
= 0; i
< map1
->n
; ++i
)
7832 for (j
= 0; j
< map2
->n
; ++j
) {
7833 result
= isl_map_add_basic_map(result
,
7834 isl_basic_map_apply_range(
7835 isl_basic_map_copy(map1
->p
[i
]),
7836 isl_basic_map_copy(map2
->p
[j
])));
7842 if (result
&& result
->n
<= 1)
7843 ISL_F_SET(result
, ISL_MAP_DISJOINT
);
7851 __isl_give isl_map
*isl_map_apply_range(__isl_take isl_map
*map1
,
7852 __isl_take isl_map
*map2
)
7854 return isl_map_align_params_map_map_and(map1
, map2
, &map_apply_range
);
7858 * returns range - domain
7860 struct isl_basic_set
*isl_basic_map_deltas(struct isl_basic_map
*bmap
)
7862 isl_space
*target_space
;
7863 struct isl_basic_set
*bset
;
7870 isl_assert(bmap
->ctx
, isl_space_tuple_is_equal(bmap
->dim
, isl_dim_in
,
7871 bmap
->dim
, isl_dim_out
),
7873 target_space
= isl_space_domain(isl_basic_map_get_space(bmap
));
7874 dim
= isl_basic_map_dim(bmap
, isl_dim_in
);
7875 nparam
= isl_basic_map_dim(bmap
, isl_dim_param
);
7876 bmap
= isl_basic_map_from_range(isl_basic_map_wrap(bmap
));
7877 bmap
= isl_basic_map_add_dims(bmap
, isl_dim_in
, dim
);
7878 bmap
= isl_basic_map_extend_constraints(bmap
, dim
, 0);
7879 for (i
= 0; i
< dim
; ++i
) {
7880 int j
= isl_basic_map_alloc_equality(bmap
);
7882 bmap
= isl_basic_map_free(bmap
);
7885 isl_seq_clr(bmap
->eq
[j
], 1 + isl_basic_map_total_dim(bmap
));
7886 isl_int_set_si(bmap
->eq
[j
][1+nparam
+i
], 1);
7887 isl_int_set_si(bmap
->eq
[j
][1+nparam
+dim
+i
], 1);
7888 isl_int_set_si(bmap
->eq
[j
][1+nparam
+2*dim
+i
], -1);
7890 bset
= isl_basic_map_domain(bmap
);
7891 bset
= isl_basic_set_reset_space(bset
, target_space
);
7894 isl_basic_map_free(bmap
);
7899 * returns range - domain
7901 __isl_give isl_set
*isl_map_deltas(__isl_take isl_map
*map
)
7905 struct isl_set
*result
;
7910 isl_assert(map
->ctx
, isl_space_tuple_is_equal(map
->dim
, isl_dim_in
,
7911 map
->dim
, isl_dim_out
),
7913 dim
= isl_map_get_space(map
);
7914 dim
= isl_space_domain(dim
);
7915 result
= isl_set_alloc_space(dim
, map
->n
, 0);
7918 for (i
= 0; i
< map
->n
; ++i
)
7919 result
= isl_set_add_basic_set(result
,
7920 isl_basic_map_deltas(isl_basic_map_copy(map
->p
[i
])));
7929 * returns [domain -> range] -> range - domain
7931 __isl_give isl_basic_map
*isl_basic_map_deltas_map(
7932 __isl_take isl_basic_map
*bmap
)
7936 isl_basic_map
*domain
;
7940 if (!isl_space_tuple_is_equal(bmap
->dim
, isl_dim_in
,
7941 bmap
->dim
, isl_dim_out
))
7942 isl_die(bmap
->ctx
, isl_error_invalid
,
7943 "domain and range don't match", goto error
);
7945 nparam
= isl_basic_map_dim(bmap
, isl_dim_param
);
7946 n
= isl_basic_map_dim(bmap
, isl_dim_in
);
7948 dim
= isl_space_from_range(isl_space_domain(isl_basic_map_get_space(bmap
)));
7949 domain
= isl_basic_map_universe(dim
);
7951 bmap
= isl_basic_map_from_domain(isl_basic_map_wrap(bmap
));
7952 bmap
= isl_basic_map_apply_range(bmap
, domain
);
7953 bmap
= isl_basic_map_extend_constraints(bmap
, n
, 0);
7955 total
= isl_basic_map_total_dim(bmap
);
7957 for (i
= 0; i
< n
; ++i
) {
7958 k
= isl_basic_map_alloc_equality(bmap
);
7961 isl_seq_clr(bmap
->eq
[k
], 1 + total
);
7962 isl_int_set_si(bmap
->eq
[k
][1 + nparam
+ i
], 1);
7963 isl_int_set_si(bmap
->eq
[k
][1 + nparam
+ n
+ i
], -1);
7964 isl_int_set_si(bmap
->eq
[k
][1 + nparam
+ n
+ n
+ i
], 1);
7967 bmap
= isl_basic_map_gauss(bmap
, NULL
);
7968 return isl_basic_map_finalize(bmap
);
7970 isl_basic_map_free(bmap
);
7975 * returns [domain -> range] -> range - domain
7977 __isl_give isl_map
*isl_map_deltas_map(__isl_take isl_map
*map
)
7980 isl_space
*domain_dim
;
7985 if (!isl_space_tuple_is_equal(map
->dim
, isl_dim_in
,
7986 map
->dim
, isl_dim_out
))
7987 isl_die(map
->ctx
, isl_error_invalid
,
7988 "domain and range don't match", goto error
);
7990 map
= isl_map_cow(map
);
7994 domain_dim
= isl_space_from_range(isl_space_domain(isl_map_get_space(map
)));
7995 map
->dim
= isl_space_from_domain(isl_space_wrap(map
->dim
));
7996 map
->dim
= isl_space_join(map
->dim
, domain_dim
);
7999 for (i
= 0; i
< map
->n
; ++i
) {
8000 map
->p
[i
] = isl_basic_map_deltas_map(map
->p
[i
]);
8004 ISL_F_CLR(map
, ISL_MAP_NORMALIZED
);
8011 static __isl_give isl_basic_map
*basic_map_identity(__isl_take isl_space
*dims
)
8013 struct isl_basic_map
*bmap
;
8021 nparam
= dims
->nparam
;
8023 bmap
= isl_basic_map_alloc_space(dims
, 0, dim
, 0);
8027 for (i
= 0; i
< dim
; ++i
) {
8028 int j
= isl_basic_map_alloc_equality(bmap
);
8031 isl_seq_clr(bmap
->eq
[j
], 1 + isl_basic_map_total_dim(bmap
));
8032 isl_int_set_si(bmap
->eq
[j
][1+nparam
+i
], 1);
8033 isl_int_set_si(bmap
->eq
[j
][1+nparam
+dim
+i
], -1);
8035 return isl_basic_map_finalize(bmap
);
8037 isl_basic_map_free(bmap
);
8041 __isl_give isl_basic_map
*isl_basic_map_identity(__isl_take isl_space
*dim
)
8045 if (dim
->n_in
!= dim
->n_out
)
8046 isl_die(dim
->ctx
, isl_error_invalid
,
8047 "number of input and output dimensions needs to be "
8048 "the same", goto error
);
8049 return basic_map_identity(dim
);
8051 isl_space_free(dim
);
8055 __isl_give isl_map
*isl_map_identity(__isl_take isl_space
*dim
)
8057 return isl_map_from_basic_map(isl_basic_map_identity(dim
));
8060 __isl_give isl_map
*isl_set_identity(__isl_take isl_set
*set
)
8062 isl_space
*dim
= isl_set_get_space(set
);
8064 id
= isl_map_identity(isl_space_map_from_set(dim
));
8065 return isl_map_intersect_range(id
, set
);
8068 /* Construct a basic set with all set dimensions having only non-negative
8071 __isl_give isl_basic_set
*isl_basic_set_positive_orthant(
8072 __isl_take isl_space
*space
)
8077 struct isl_basic_set
*bset
;
8081 nparam
= space
->nparam
;
8083 bset
= isl_basic_set_alloc_space(space
, 0, 0, dim
);
8086 for (i
= 0; i
< dim
; ++i
) {
8087 int k
= isl_basic_set_alloc_inequality(bset
);
8090 isl_seq_clr(bset
->ineq
[k
], 1 + isl_basic_set_total_dim(bset
));
8091 isl_int_set_si(bset
->ineq
[k
][1 + nparam
+ i
], 1);
8095 isl_basic_set_free(bset
);
8099 /* Construct the half-space x_pos >= 0.
8101 static __isl_give isl_basic_set
*nonneg_halfspace(__isl_take isl_space
*dim
,
8105 isl_basic_set
*nonneg
;
8107 nonneg
= isl_basic_set_alloc_space(dim
, 0, 0, 1);
8108 k
= isl_basic_set_alloc_inequality(nonneg
);
8111 isl_seq_clr(nonneg
->ineq
[k
], 1 + isl_basic_set_total_dim(nonneg
));
8112 isl_int_set_si(nonneg
->ineq
[k
][pos
], 1);
8114 return isl_basic_set_finalize(nonneg
);
8116 isl_basic_set_free(nonneg
);
8120 /* Construct the half-space x_pos <= -1.
8122 static __isl_give isl_basic_set
*neg_halfspace(__isl_take isl_space
*dim
, int pos
)
8127 neg
= isl_basic_set_alloc_space(dim
, 0, 0, 1);
8128 k
= isl_basic_set_alloc_inequality(neg
);
8131 isl_seq_clr(neg
->ineq
[k
], 1 + isl_basic_set_total_dim(neg
));
8132 isl_int_set_si(neg
->ineq
[k
][0], -1);
8133 isl_int_set_si(neg
->ineq
[k
][pos
], -1);
8135 return isl_basic_set_finalize(neg
);
8137 isl_basic_set_free(neg
);
8141 __isl_give isl_set
*isl_set_split_dims(__isl_take isl_set
*set
,
8142 enum isl_dim_type type
, unsigned first
, unsigned n
)
8146 isl_basic_set
*nonneg
;
8154 isl_assert(set
->ctx
, first
+ n
<= isl_set_dim(set
, type
), goto error
);
8156 offset
= pos(set
->dim
, type
);
8157 for (i
= 0; i
< n
; ++i
) {
8158 nonneg
= nonneg_halfspace(isl_set_get_space(set
),
8159 offset
+ first
+ i
);
8160 neg
= neg_halfspace(isl_set_get_space(set
), offset
+ first
+ i
);
8162 set
= isl_set_intersect(set
, isl_basic_set_union(nonneg
, neg
));
8171 static isl_stat
foreach_orthant(__isl_take isl_set
*set
, int *signs
, int first
,
8173 isl_stat (*fn
)(__isl_take isl_set
*orthant
, int *signs
, void *user
),
8179 return isl_stat_error
;
8180 if (isl_set_plain_is_empty(set
)) {
8185 return fn(set
, signs
, user
);
8188 half
= isl_set_from_basic_set(nonneg_halfspace(isl_set_get_space(set
),
8190 half
= isl_set_intersect(half
, isl_set_copy(set
));
8191 if (foreach_orthant(half
, signs
, first
+ 1, len
, fn
, user
) < 0)
8195 half
= isl_set_from_basic_set(neg_halfspace(isl_set_get_space(set
),
8197 half
= isl_set_intersect(half
, set
);
8198 return foreach_orthant(half
, signs
, first
+ 1, len
, fn
, user
);
8201 return isl_stat_error
;
8204 /* Call "fn" on the intersections of "set" with each of the orthants
8205 * (except for obviously empty intersections). The orthant is identified
8206 * by the signs array, with each entry having value 1 or -1 according
8207 * to the sign of the corresponding variable.
8209 isl_stat
isl_set_foreach_orthant(__isl_keep isl_set
*set
,
8210 isl_stat (*fn
)(__isl_take isl_set
*orthant
, int *signs
, void *user
),
8219 return isl_stat_error
;
8220 if (isl_set_plain_is_empty(set
))
8223 nparam
= isl_set_dim(set
, isl_dim_param
);
8224 nvar
= isl_set_dim(set
, isl_dim_set
);
8226 signs
= isl_alloc_array(set
->ctx
, int, nparam
+ nvar
);
8228 r
= foreach_orthant(isl_set_copy(set
), signs
, 0, nparam
+ nvar
,
8236 isl_bool
isl_set_is_equal(__isl_keep isl_set
*set1
, __isl_keep isl_set
*set2
)
8238 return isl_map_is_equal(set_to_map(set1
), set_to_map(set2
));
8241 isl_bool
isl_basic_map_is_subset(__isl_keep isl_basic_map
*bmap1
,
8242 __isl_keep isl_basic_map
*bmap2
)
8245 struct isl_map
*map1
;
8246 struct isl_map
*map2
;
8248 if (!bmap1
|| !bmap2
)
8249 return isl_bool_error
;
8251 map1
= isl_map_from_basic_map(isl_basic_map_copy(bmap1
));
8252 map2
= isl_map_from_basic_map(isl_basic_map_copy(bmap2
));
8254 is_subset
= isl_map_is_subset(map1
, map2
);
8262 isl_bool
isl_basic_set_is_subset(__isl_keep isl_basic_set
*bset1
,
8263 __isl_keep isl_basic_set
*bset2
)
8265 return isl_basic_map_is_subset(bset1
, bset2
);
8268 isl_bool
isl_basic_map_is_equal(__isl_keep isl_basic_map
*bmap1
,
8269 __isl_keep isl_basic_map
*bmap2
)
8273 if (!bmap1
|| !bmap2
)
8274 return isl_bool_error
;
8275 is_subset
= isl_basic_map_is_subset(bmap1
, bmap2
);
8276 if (is_subset
!= isl_bool_true
)
8278 is_subset
= isl_basic_map_is_subset(bmap2
, bmap1
);
8282 isl_bool
isl_basic_set_is_equal(__isl_keep isl_basic_set
*bset1
,
8283 __isl_keep isl_basic_set
*bset2
)
8285 return isl_basic_map_is_equal(
8286 bset_to_bmap(bset1
), bset_to_bmap(bset2
));
8289 isl_bool
isl_map_is_empty(__isl_keep isl_map
*map
)
8295 return isl_bool_error
;
8296 for (i
= 0; i
< map
->n
; ++i
) {
8297 is_empty
= isl_basic_map_is_empty(map
->p
[i
]);
8299 return isl_bool_error
;
8301 return isl_bool_false
;
8303 return isl_bool_true
;
8306 isl_bool
isl_map_plain_is_empty(__isl_keep isl_map
*map
)
8308 return map
? map
->n
== 0 : isl_bool_error
;
8311 isl_bool
isl_set_plain_is_empty(__isl_keep isl_set
*set
)
8313 return set
? set
->n
== 0 : isl_bool_error
;
8316 isl_bool
isl_set_is_empty(__isl_keep isl_set
*set
)
8318 return isl_map_is_empty(set_to_map(set
));
8321 isl_bool
isl_map_has_equal_space(__isl_keep isl_map
*map1
,
8322 __isl_keep isl_map
*map2
)
8325 return isl_bool_error
;
8327 return isl_space_is_equal(map1
->dim
, map2
->dim
);
8330 isl_bool
isl_set_has_equal_space(__isl_keep isl_set
*set1
,
8331 __isl_keep isl_set
*set2
)
8334 return isl_bool_error
;
8336 return isl_space_is_equal(set1
->dim
, set2
->dim
);
8339 static isl_bool
map_is_equal(__isl_keep isl_map
*map1
, __isl_keep isl_map
*map2
)
8344 return isl_bool_error
;
8345 is_subset
= isl_map_is_subset(map1
, map2
);
8346 if (is_subset
!= isl_bool_true
)
8348 is_subset
= isl_map_is_subset(map2
, map1
);
8352 /* Is "map1" equal to "map2"?
8354 * First check if they are obviously equal.
8355 * If not, then perform a more detailed analysis.
8357 isl_bool
isl_map_is_equal(__isl_keep isl_map
*map1
, __isl_keep isl_map
*map2
)
8361 equal
= isl_map_plain_is_equal(map1
, map2
);
8362 if (equal
< 0 || equal
)
8364 return isl_map_align_params_map_map_and_test(map1
, map2
, &map_is_equal
);
8367 isl_bool
isl_basic_map_is_strict_subset(
8368 struct isl_basic_map
*bmap1
, struct isl_basic_map
*bmap2
)
8372 if (!bmap1
|| !bmap2
)
8373 return isl_bool_error
;
8374 is_subset
= isl_basic_map_is_subset(bmap1
, bmap2
);
8375 if (is_subset
!= isl_bool_true
)
8377 is_subset
= isl_basic_map_is_subset(bmap2
, bmap1
);
8378 if (is_subset
== isl_bool_error
)
8383 isl_bool
isl_map_is_strict_subset(__isl_keep isl_map
*map1
,
8384 __isl_keep isl_map
*map2
)
8389 return isl_bool_error
;
8390 is_subset
= isl_map_is_subset(map1
, map2
);
8391 if (is_subset
!= isl_bool_true
)
8393 is_subset
= isl_map_is_subset(map2
, map1
);
8394 if (is_subset
== isl_bool_error
)
8399 isl_bool
isl_set_is_strict_subset(__isl_keep isl_set
*set1
,
8400 __isl_keep isl_set
*set2
)
8402 return isl_map_is_strict_subset(set_to_map(set1
), set_to_map(set2
));
8405 /* Is "bmap" obviously equal to the universe with the same space?
8407 * That is, does it not have any constraints?
8409 isl_bool
isl_basic_map_plain_is_universe(__isl_keep isl_basic_map
*bmap
)
8412 return isl_bool_error
;
8413 return bmap
->n_eq
== 0 && bmap
->n_ineq
== 0;
8416 /* Is "bset" obviously equal to the universe with the same space?
8418 isl_bool
isl_basic_set_plain_is_universe(__isl_keep isl_basic_set
*bset
)
8420 return isl_basic_map_plain_is_universe(bset
);
8423 /* If "c" does not involve any existentially quantified variables,
8424 * then set *univ to false and abort
8426 static isl_stat
involves_divs(__isl_take isl_constraint
*c
, void *user
)
8428 isl_bool
*univ
= user
;
8431 n
= isl_constraint_dim(c
, isl_dim_div
);
8432 *univ
= isl_constraint_involves_dims(c
, isl_dim_div
, 0, n
);
8433 isl_constraint_free(c
);
8434 if (*univ
< 0 || !*univ
)
8435 return isl_stat_error
;
8439 /* Is "bmap" equal to the universe with the same space?
8441 * First check if it is obviously equal to the universe.
8442 * If not and if there are any constraints not involving
8443 * existentially quantified variables, then it is certainly
8444 * not equal to the universe.
8445 * Otherwise, check if the universe is a subset of "bmap".
8447 isl_bool
isl_basic_map_is_universe(__isl_keep isl_basic_map
*bmap
)
8450 isl_basic_map
*test
;
8452 univ
= isl_basic_map_plain_is_universe(bmap
);
8453 if (univ
< 0 || univ
)
8455 if (isl_basic_map_dim(bmap
, isl_dim_div
) == 0)
8456 return isl_bool_false
;
8457 univ
= isl_bool_true
;
8458 if (isl_basic_map_foreach_constraint(bmap
, &involves_divs
, &univ
) < 0 &&
8460 return isl_bool_error
;
8461 if (univ
< 0 || !univ
)
8463 test
= isl_basic_map_universe(isl_basic_map_get_space(bmap
));
8464 univ
= isl_basic_map_is_subset(test
, bmap
);
8465 isl_basic_map_free(test
);
8469 /* Is "bset" equal to the universe with the same space?
8471 isl_bool
isl_basic_set_is_universe(__isl_keep isl_basic_set
*bset
)
8473 return isl_basic_map_is_universe(bset
);
8476 isl_bool
isl_map_plain_is_universe(__isl_keep isl_map
*map
)
8481 return isl_bool_error
;
8483 for (i
= 0; i
< map
->n
; ++i
) {
8484 isl_bool r
= isl_basic_map_plain_is_universe(map
->p
[i
]);
8489 return isl_bool_false
;
8492 isl_bool
isl_set_plain_is_universe(__isl_keep isl_set
*set
)
8494 return isl_map_plain_is_universe(set_to_map(set
));
8497 isl_bool
isl_basic_map_is_empty(__isl_keep isl_basic_map
*bmap
)
8499 struct isl_basic_set
*bset
= NULL
;
8500 struct isl_vec
*sample
= NULL
;
8501 isl_bool empty
, non_empty
;
8504 return isl_bool_error
;
8506 if (ISL_F_ISSET(bmap
, ISL_BASIC_MAP_EMPTY
))
8507 return isl_bool_true
;
8509 if (isl_basic_map_plain_is_universe(bmap
))
8510 return isl_bool_false
;
8512 if (ISL_F_ISSET(bmap
, ISL_BASIC_MAP_RATIONAL
)) {
8513 struct isl_basic_map
*copy
= isl_basic_map_copy(bmap
);
8514 copy
= isl_basic_map_remove_redundancies(copy
);
8515 empty
= isl_basic_map_plain_is_empty(copy
);
8516 isl_basic_map_free(copy
);
8520 non_empty
= isl_basic_map_plain_is_non_empty(bmap
);
8522 return isl_bool_error
;
8524 return isl_bool_false
;
8525 isl_vec_free(bmap
->sample
);
8526 bmap
->sample
= NULL
;
8527 bset
= isl_basic_map_underlying_set(isl_basic_map_copy(bmap
));
8529 return isl_bool_error
;
8530 sample
= isl_basic_set_sample_vec(bset
);
8532 return isl_bool_error
;
8533 empty
= sample
->size
== 0;
8534 isl_vec_free(bmap
->sample
);
8535 bmap
->sample
= sample
;
8537 ISL_F_SET(bmap
, ISL_BASIC_MAP_EMPTY
);
8542 isl_bool
isl_basic_map_plain_is_empty(__isl_keep isl_basic_map
*bmap
)
8545 return isl_bool_error
;
8546 return ISL_F_ISSET(bmap
, ISL_BASIC_MAP_EMPTY
);
8549 isl_bool
isl_basic_set_plain_is_empty(__isl_keep isl_basic_set
*bset
)
8552 return isl_bool_error
;
8553 return ISL_F_ISSET(bset
, ISL_BASIC_SET_EMPTY
);
8556 /* Is "bmap" known to be non-empty?
8558 * That is, is the cached sample still valid?
8560 isl_bool
isl_basic_map_plain_is_non_empty(__isl_keep isl_basic_map
*bmap
)
8565 return isl_bool_error
;
8567 return isl_bool_false
;
8568 total
= 1 + isl_basic_map_total_dim(bmap
);
8569 if (bmap
->sample
->size
!= total
)
8570 return isl_bool_false
;
8571 return isl_basic_map_contains(bmap
, bmap
->sample
);
8574 isl_bool
isl_basic_set_is_empty(__isl_keep isl_basic_set
*bset
)
8576 return isl_basic_map_is_empty(bset_to_bmap(bset
));
8579 struct isl_map
*isl_basic_map_union(
8580 struct isl_basic_map
*bmap1
, struct isl_basic_map
*bmap2
)
8582 struct isl_map
*map
;
8583 if (!bmap1
|| !bmap2
)
8586 isl_assert(bmap1
->ctx
, isl_space_is_equal(bmap1
->dim
, bmap2
->dim
), goto error
);
8588 map
= isl_map_alloc_space(isl_space_copy(bmap1
->dim
), 2, 0);
8591 map
= isl_map_add_basic_map(map
, bmap1
);
8592 map
= isl_map_add_basic_map(map
, bmap2
);
8595 isl_basic_map_free(bmap1
);
8596 isl_basic_map_free(bmap2
);
8600 struct isl_set
*isl_basic_set_union(
8601 struct isl_basic_set
*bset1
, struct isl_basic_set
*bset2
)
8603 return set_from_map(isl_basic_map_union(bset_to_bmap(bset1
),
8604 bset_to_bmap(bset2
)));
8607 /* Order divs such that any div only depends on previous divs */
8608 struct isl_basic_map
*isl_basic_map_order_divs(struct isl_basic_map
*bmap
)
8616 off
= isl_space_dim(bmap
->dim
, isl_dim_all
);
8618 for (i
= 0; i
< bmap
->n_div
; ++i
) {
8620 if (isl_int_is_zero(bmap
->div
[i
][0]))
8622 pos
= isl_seq_first_non_zero(bmap
->div
[i
]+1+1+off
+i
,
8627 isl_die(isl_basic_map_get_ctx(bmap
), isl_error_internal
,
8628 "integer division depends on itself",
8629 return isl_basic_map_free(bmap
));
8630 isl_basic_map_swap_div(bmap
, i
, i
+ pos
);
8636 struct isl_basic_set
*isl_basic_set_order_divs(struct isl_basic_set
*bset
)
8638 return bset_from_bmap(isl_basic_map_order_divs(bset_to_bmap(bset
)));
8641 __isl_give isl_map
*isl_map_order_divs(__isl_take isl_map
*map
)
8648 for (i
= 0; i
< map
->n
; ++i
) {
8649 map
->p
[i
] = isl_basic_map_order_divs(map
->p
[i
]);
8660 /* Sort the local variables of "bset".
8662 __isl_give isl_basic_set
*isl_basic_set_sort_divs(
8663 __isl_take isl_basic_set
*bset
)
8665 return bset_from_bmap(isl_basic_map_sort_divs(bset_to_bmap(bset
)));
8668 /* Apply the expansion computed by isl_merge_divs.
8669 * The expansion itself is given by "exp" while the resulting
8670 * list of divs is given by "div".
8672 * Move the integer divisions of "bmap" into the right position
8673 * according to "exp" and then introduce the additional integer
8674 * divisions, adding div constraints.
8675 * The moving should be done first to avoid moving coefficients
8676 * in the definitions of the extra integer divisions.
8678 __isl_give isl_basic_map
*isl_basic_map_expand_divs(
8679 __isl_take isl_basic_map
*bmap
, __isl_take isl_mat
*div
, int *exp
)
8684 bmap
= isl_basic_map_cow(bmap
);
8688 if (div
->n_row
< bmap
->n_div
)
8689 isl_die(isl_mat_get_ctx(div
), isl_error_invalid
,
8690 "not an expansion", goto error
);
8692 n_div
= bmap
->n_div
;
8693 bmap
= isl_basic_map_extend_space(bmap
, isl_space_copy(bmap
->dim
),
8694 div
->n_row
- n_div
, 0,
8695 2 * (div
->n_row
- n_div
));
8697 for (i
= n_div
; i
< div
->n_row
; ++i
)
8698 if (isl_basic_map_alloc_div(bmap
) < 0)
8701 for (j
= n_div
- 1; j
>= 0; --j
) {
8704 isl_basic_map_swap_div(bmap
, j
, exp
[j
]);
8707 for (i
= 0; i
< div
->n_row
; ++i
) {
8708 if (j
< n_div
&& exp
[j
] == i
) {
8711 isl_seq_cpy(bmap
->div
[i
], div
->row
[i
], div
->n_col
);
8712 if (isl_basic_map_div_is_marked_unknown(bmap
, i
))
8714 if (isl_basic_map_add_div_constraints(bmap
, i
) < 0)
8722 isl_basic_map_free(bmap
);
8727 /* Apply the expansion computed by isl_merge_divs.
8728 * The expansion itself is given by "exp" while the resulting
8729 * list of divs is given by "div".
8731 __isl_give isl_basic_set
*isl_basic_set_expand_divs(
8732 __isl_take isl_basic_set
*bset
, __isl_take isl_mat
*div
, int *exp
)
8734 return isl_basic_map_expand_divs(bset
, div
, exp
);
8737 /* Look for a div in dst that corresponds to the div "div" in src.
8738 * The divs before "div" in src and dst are assumed to be the same.
8740 * Returns -1 if no corresponding div was found and the position
8741 * of the corresponding div in dst otherwise.
8743 static int find_div(__isl_keep isl_basic_map
*dst
,
8744 __isl_keep isl_basic_map
*src
, unsigned div
)
8748 unsigned total
= isl_space_dim(src
->dim
, isl_dim_all
);
8750 isl_assert(dst
->ctx
, div
<= dst
->n_div
, return -1);
8751 for (i
= div
; i
< dst
->n_div
; ++i
)
8752 if (isl_seq_eq(dst
->div
[i
], src
->div
[div
], 1+1+total
+div
) &&
8753 isl_seq_first_non_zero(dst
->div
[i
]+1+1+total
+div
,
8754 dst
->n_div
- div
) == -1)
8759 /* Align the divs of "dst" to those of "src", adding divs from "src"
8760 * if needed. That is, make sure that the first src->n_div divs
8761 * of the result are equal to those of src.
8763 * The result is not finalized as by design it will have redundant
8764 * divs if any divs from "src" were copied.
8766 __isl_give isl_basic_map
*isl_basic_map_align_divs(
8767 __isl_take isl_basic_map
*dst
, __isl_keep isl_basic_map
*src
)
8770 int known
, extended
;
8774 return isl_basic_map_free(dst
);
8776 if (src
->n_div
== 0)
8779 known
= isl_basic_map_divs_known(src
);
8781 return isl_basic_map_free(dst
);
8783 isl_die(isl_basic_map_get_ctx(src
), isl_error_invalid
,
8784 "some src divs are unknown",
8785 return isl_basic_map_free(dst
));
8787 src
= isl_basic_map_order_divs(src
);
8790 total
= isl_space_dim(src
->dim
, isl_dim_all
);
8791 for (i
= 0; i
< src
->n_div
; ++i
) {
8792 int j
= find_div(dst
, src
, i
);
8795 int extra
= src
->n_div
- i
;
8796 dst
= isl_basic_map_cow(dst
);
8799 dst
= isl_basic_map_extend_space(dst
,
8800 isl_space_copy(dst
->dim
),
8801 extra
, 0, 2 * extra
);
8804 j
= isl_basic_map_alloc_div(dst
);
8806 return isl_basic_map_free(dst
);
8807 isl_seq_cpy(dst
->div
[j
], src
->div
[i
], 1+1+total
+i
);
8808 isl_seq_clr(dst
->div
[j
]+1+1+total
+i
, dst
->n_div
- i
);
8809 if (isl_basic_map_add_div_constraints(dst
, j
) < 0)
8810 return isl_basic_map_free(dst
);
8813 isl_basic_map_swap_div(dst
, i
, j
);
8818 __isl_give isl_map
*isl_map_align_divs_internal(__isl_take isl_map
*map
)
8826 map
= isl_map_compute_divs(map
);
8827 map
= isl_map_cow(map
);
8831 for (i
= 1; i
< map
->n
; ++i
)
8832 map
->p
[0] = isl_basic_map_align_divs(map
->p
[0], map
->p
[i
]);
8833 for (i
= 1; i
< map
->n
; ++i
) {
8834 map
->p
[i
] = isl_basic_map_align_divs(map
->p
[i
], map
->p
[0]);
8836 return isl_map_free(map
);
8839 ISL_F_CLR(map
, ISL_MAP_NORMALIZED
);
8843 __isl_give isl_map
*isl_map_align_divs(__isl_take isl_map
*map
)
8845 return isl_map_align_divs_internal(map
);
8848 struct isl_set
*isl_set_align_divs(struct isl_set
*set
)
8850 return set_from_map(isl_map_align_divs_internal(set_to_map(set
)));
8853 /* Align the divs of the basic maps in "map" to those
8854 * of the basic maps in "list", as well as to the other basic maps in "map".
8855 * The elements in "list" are assumed to have known divs.
8857 __isl_give isl_map
*isl_map_align_divs_to_basic_map_list(
8858 __isl_take isl_map
*map
, __isl_keep isl_basic_map_list
*list
)
8862 map
= isl_map_compute_divs(map
);
8863 map
= isl_map_cow(map
);
8865 return isl_map_free(map
);
8869 n
= isl_basic_map_list_n_basic_map(list
);
8870 for (i
= 0; i
< n
; ++i
) {
8871 isl_basic_map
*bmap
;
8873 bmap
= isl_basic_map_list_get_basic_map(list
, i
);
8874 map
->p
[0] = isl_basic_map_align_divs(map
->p
[0], bmap
);
8875 isl_basic_map_free(bmap
);
8878 return isl_map_free(map
);
8880 return isl_map_align_divs_internal(map
);
8883 /* Align the divs of each element of "list" to those of "bmap".
8884 * Both "bmap" and the elements of "list" are assumed to have known divs.
8886 __isl_give isl_basic_map_list
*isl_basic_map_list_align_divs_to_basic_map(
8887 __isl_take isl_basic_map_list
*list
, __isl_keep isl_basic_map
*bmap
)
8892 return isl_basic_map_list_free(list
);
8894 n
= isl_basic_map_list_n_basic_map(list
);
8895 for (i
= 0; i
< n
; ++i
) {
8896 isl_basic_map
*bmap_i
;
8898 bmap_i
= isl_basic_map_list_get_basic_map(list
, i
);
8899 bmap_i
= isl_basic_map_align_divs(bmap_i
, bmap
);
8900 list
= isl_basic_map_list_set_basic_map(list
, i
, bmap_i
);
8906 static __isl_give isl_set
*set_apply( __isl_take isl_set
*set
,
8907 __isl_take isl_map
*map
)
8911 ok
= isl_map_compatible_domain(map
, set
);
8915 isl_die(isl_set_get_ctx(set
), isl_error_invalid
,
8916 "incompatible spaces", goto error
);
8917 map
= isl_map_intersect_domain(map
, set
);
8918 set
= isl_map_range(map
);
8926 __isl_give isl_set
*isl_set_apply( __isl_take isl_set
*set
,
8927 __isl_take isl_map
*map
)
8929 return isl_map_align_params_map_map_and(set
, map
, &set_apply
);
8932 /* There is no need to cow as removing empty parts doesn't change
8933 * the meaning of the set.
8935 struct isl_map
*isl_map_remove_empty_parts(struct isl_map
*map
)
8942 for (i
= map
->n
- 1; i
>= 0; --i
)
8943 remove_if_empty(map
, i
);
8948 struct isl_set
*isl_set_remove_empty_parts(struct isl_set
*set
)
8950 return set_from_map(isl_map_remove_empty_parts(set_to_map(set
)));
8953 /* Given two basic sets bset1 and bset2, compute the maximal difference
8954 * between the values of dimension pos in bset1 and those in bset2
8955 * for any common value of the parameters and dimensions preceding pos.
8957 static enum isl_lp_result
basic_set_maximal_difference_at(
8958 __isl_keep isl_basic_set
*bset1
, __isl_keep isl_basic_set
*bset2
,
8959 int pos
, isl_int
*opt
)
8961 isl_basic_map
*bmap1
;
8962 isl_basic_map
*bmap2
;
8963 struct isl_ctx
*ctx
;
8964 struct isl_vec
*obj
;
8968 enum isl_lp_result res
;
8970 if (!bset1
|| !bset2
)
8971 return isl_lp_error
;
8973 nparam
= isl_basic_set_n_param(bset1
);
8974 dim1
= isl_basic_set_n_dim(bset1
);
8976 bmap1
= isl_basic_map_from_range(isl_basic_set_copy(bset1
));
8977 bmap2
= isl_basic_map_from_range(isl_basic_set_copy(bset2
));
8978 bmap1
= isl_basic_map_move_dims(bmap1
, isl_dim_in
, 0,
8979 isl_dim_out
, 0, pos
);
8980 bmap2
= isl_basic_map_move_dims(bmap2
, isl_dim_in
, 0,
8981 isl_dim_out
, 0, pos
);
8982 bmap1
= isl_basic_map_range_product(bmap1
, bmap2
);
8984 return isl_lp_error
;
8986 total
= isl_basic_map_total_dim(bmap1
);
8988 obj
= isl_vec_alloc(ctx
, 1 + total
);
8991 isl_seq_clr(obj
->block
.data
, 1 + total
);
8992 isl_int_set_si(obj
->block
.data
[1+nparam
+pos
], 1);
8993 isl_int_set_si(obj
->block
.data
[1+nparam
+pos
+(dim1
-pos
)], -1);
8994 res
= isl_basic_map_solve_lp(bmap1
, 1, obj
->block
.data
, ctx
->one
,
8996 isl_basic_map_free(bmap1
);
9000 isl_basic_map_free(bmap1
);
9001 return isl_lp_error
;
9004 /* Given two _disjoint_ basic sets bset1 and bset2, check whether
9005 * for any common value of the parameters and dimensions preceding pos
9006 * in both basic sets, the values of dimension pos in bset1 are
9007 * smaller or larger than those in bset2.
9010 * 1 if bset1 follows bset2
9011 * -1 if bset1 precedes bset2
9012 * 0 if bset1 and bset2 are incomparable
9013 * -2 if some error occurred.
9015 int isl_basic_set_compare_at(struct isl_basic_set
*bset1
,
9016 struct isl_basic_set
*bset2
, int pos
)
9019 enum isl_lp_result res
;
9024 res
= basic_set_maximal_difference_at(bset1
, bset2
, pos
, &opt
);
9026 if (res
== isl_lp_empty
)
9028 else if ((res
== isl_lp_ok
&& isl_int_is_pos(opt
)) ||
9029 res
== isl_lp_unbounded
)
9031 else if (res
== isl_lp_ok
&& isl_int_is_neg(opt
))
9040 /* Given two basic sets bset1 and bset2, check whether
9041 * for any common value of the parameters and dimensions preceding pos
9042 * there is a value of dimension pos in bset1 that is larger
9043 * than a value of the same dimension in bset2.
9046 * 1 if there exists such a pair
9047 * 0 if there is no such pair, but there is a pair of equal values
9049 * -2 if some error occurred.
9051 int isl_basic_set_follows_at(__isl_keep isl_basic_set
*bset1
,
9052 __isl_keep isl_basic_set
*bset2
, int pos
)
9055 enum isl_lp_result res
;
9060 res
= basic_set_maximal_difference_at(bset1
, bset2
, pos
, &opt
);
9062 if (res
== isl_lp_empty
)
9064 else if ((res
== isl_lp_ok
&& isl_int_is_pos(opt
)) ||
9065 res
== isl_lp_unbounded
)
9067 else if (res
== isl_lp_ok
&& isl_int_is_neg(opt
))
9069 else if (res
== isl_lp_ok
)
9078 /* Given two sets set1 and set2, check whether
9079 * for any common value of the parameters and dimensions preceding pos
9080 * there is a value of dimension pos in set1 that is larger
9081 * than a value of the same dimension in set2.
9084 * 1 if there exists such a pair
9085 * 0 if there is no such pair, but there is a pair of equal values
9087 * -2 if some error occurred.
9089 int isl_set_follows_at(__isl_keep isl_set
*set1
,
9090 __isl_keep isl_set
*set2
, int pos
)
9098 for (i
= 0; i
< set1
->n
; ++i
)
9099 for (j
= 0; j
< set2
->n
; ++j
) {
9101 f
= isl_basic_set_follows_at(set1
->p
[i
], set2
->p
[j
], pos
);
9102 if (f
== 1 || f
== -2)
9111 static isl_bool
isl_basic_map_plain_has_fixed_var(
9112 __isl_keep isl_basic_map
*bmap
, unsigned pos
, isl_int
*val
)
9119 return isl_bool_error
;
9120 total
= isl_basic_map_total_dim(bmap
);
9121 for (i
= 0, d
= total
-1; i
< bmap
->n_eq
&& d
+1 > pos
; ++i
) {
9122 for (; d
+1 > pos
; --d
)
9123 if (!isl_int_is_zero(bmap
->eq
[i
][1+d
]))
9127 if (isl_seq_first_non_zero(bmap
->eq
[i
]+1, d
) != -1)
9128 return isl_bool_false
;
9129 if (isl_seq_first_non_zero(bmap
->eq
[i
]+1+d
+1, total
-d
-1) != -1)
9130 return isl_bool_false
;
9131 if (!isl_int_is_one(bmap
->eq
[i
][1+d
]))
9132 return isl_bool_false
;
9134 isl_int_neg(*val
, bmap
->eq
[i
][0]);
9135 return isl_bool_true
;
9137 return isl_bool_false
;
9140 static isl_bool
isl_map_plain_has_fixed_var(__isl_keep isl_map
*map
,
9141 unsigned pos
, isl_int
*val
)
9149 return isl_bool_error
;
9151 return isl_bool_false
;
9153 return isl_basic_map_plain_has_fixed_var(map
->p
[0], pos
, val
);
9156 fixed
= isl_basic_map_plain_has_fixed_var(map
->p
[0], pos
, &v
);
9157 for (i
= 1; fixed
== isl_bool_true
&& i
< map
->n
; ++i
) {
9158 fixed
= isl_basic_map_plain_has_fixed_var(map
->p
[i
], pos
, &tmp
);
9159 if (fixed
== isl_bool_true
&& isl_int_ne(tmp
, v
))
9160 fixed
= isl_bool_false
;
9163 isl_int_set(*val
, v
);
9169 static isl_bool
isl_basic_set_plain_has_fixed_var(
9170 __isl_keep isl_basic_set
*bset
, unsigned pos
, isl_int
*val
)
9172 return isl_basic_map_plain_has_fixed_var(bset_to_bmap(bset
),
9176 isl_bool
isl_basic_map_plain_is_fixed(__isl_keep isl_basic_map
*bmap
,
9177 enum isl_dim_type type
, unsigned pos
, isl_int
*val
)
9179 if (isl_basic_map_check_range(bmap
, type
, pos
, 1) < 0)
9180 return isl_bool_error
;
9181 return isl_basic_map_plain_has_fixed_var(bmap
,
9182 isl_basic_map_offset(bmap
, type
) - 1 + pos
, val
);
9185 /* If "bmap" obviously lies on a hyperplane where the given dimension
9186 * has a fixed value, then return that value.
9187 * Otherwise return NaN.
9189 __isl_give isl_val
*isl_basic_map_plain_get_val_if_fixed(
9190 __isl_keep isl_basic_map
*bmap
,
9191 enum isl_dim_type type
, unsigned pos
)
9199 ctx
= isl_basic_map_get_ctx(bmap
);
9200 v
= isl_val_alloc(ctx
);
9203 fixed
= isl_basic_map_plain_is_fixed(bmap
, type
, pos
, &v
->n
);
9205 return isl_val_free(v
);
9207 isl_int_set_si(v
->d
, 1);
9211 return isl_val_nan(ctx
);
9214 isl_bool
isl_map_plain_is_fixed(__isl_keep isl_map
*map
,
9215 enum isl_dim_type type
, unsigned pos
, isl_int
*val
)
9217 if (pos
>= isl_map_dim(map
, type
))
9218 isl_die(isl_map_get_ctx(map
), isl_error_invalid
,
9219 "position out of bounds", return isl_bool_error
);
9220 return isl_map_plain_has_fixed_var(map
,
9221 map_offset(map
, type
) - 1 + pos
, val
);
9224 /* If "map" obviously lies on a hyperplane where the given dimension
9225 * has a fixed value, then return that value.
9226 * Otherwise return NaN.
9228 __isl_give isl_val
*isl_map_plain_get_val_if_fixed(__isl_keep isl_map
*map
,
9229 enum isl_dim_type type
, unsigned pos
)
9237 ctx
= isl_map_get_ctx(map
);
9238 v
= isl_val_alloc(ctx
);
9241 fixed
= isl_map_plain_is_fixed(map
, type
, pos
, &v
->n
);
9243 return isl_val_free(v
);
9245 isl_int_set_si(v
->d
, 1);
9249 return isl_val_nan(ctx
);
9252 /* If "set" obviously lies on a hyperplane where the given dimension
9253 * has a fixed value, then return that value.
9254 * Otherwise return NaN.
9256 __isl_give isl_val
*isl_set_plain_get_val_if_fixed(__isl_keep isl_set
*set
,
9257 enum isl_dim_type type
, unsigned pos
)
9259 return isl_map_plain_get_val_if_fixed(set
, type
, pos
);
9262 isl_bool
isl_set_plain_is_fixed(__isl_keep isl_set
*set
,
9263 enum isl_dim_type type
, unsigned pos
, isl_int
*val
)
9265 return isl_map_plain_is_fixed(set
, type
, pos
, val
);
9268 /* Check if dimension dim has fixed value and if so and if val is not NULL,
9269 * then return this fixed value in *val.
9271 isl_bool
isl_basic_set_plain_dim_is_fixed(__isl_keep isl_basic_set
*bset
,
9272 unsigned dim
, isl_int
*val
)
9274 return isl_basic_set_plain_has_fixed_var(bset
,
9275 isl_basic_set_n_param(bset
) + dim
, val
);
9278 /* Return -1 if the constraint "c1" should be sorted before "c2"
9279 * and 1 if it should be sorted after "c2".
9280 * Return 0 if the two constraints are the same (up to the constant term).
9282 * In particular, if a constraint involves later variables than another
9283 * then it is sorted after this other constraint.
9284 * uset_gist depends on constraints without existentially quantified
9285 * variables sorting first.
9287 * For constraints that have the same latest variable, those
9288 * with the same coefficient for this latest variable (first in absolute value
9289 * and then in actual value) are grouped together.
9290 * This is useful for detecting pairs of constraints that can
9291 * be chained in their printed representation.
9293 * Finally, within a group, constraints are sorted according to
9294 * their coefficients (excluding the constant term).
9296 static int sort_constraint_cmp(const void *p1
, const void *p2
, void *arg
)
9298 isl_int
**c1
= (isl_int
**) p1
;
9299 isl_int
**c2
= (isl_int
**) p2
;
9301 unsigned size
= *(unsigned *) arg
;
9304 l1
= isl_seq_last_non_zero(*c1
+ 1, size
);
9305 l2
= isl_seq_last_non_zero(*c2
+ 1, size
);
9310 cmp
= isl_int_abs_cmp((*c1
)[1 + l1
], (*c2
)[1 + l1
]);
9313 cmp
= isl_int_cmp((*c1
)[1 + l1
], (*c2
)[1 + l1
]);
9317 return isl_seq_cmp(*c1
+ 1, *c2
+ 1, size
);
9320 /* Return -1 if the constraint "c1" of "bmap" is sorted before "c2"
9321 * by isl_basic_map_sort_constraints, 1 if it is sorted after "c2"
9322 * and 0 if the two constraints are the same (up to the constant term).
9324 int isl_basic_map_constraint_cmp(__isl_keep isl_basic_map
*bmap
,
9325 isl_int
*c1
, isl_int
*c2
)
9331 total
= isl_basic_map_total_dim(bmap
);
9332 return sort_constraint_cmp(&c1
, &c2
, &total
);
9335 __isl_give isl_basic_map
*isl_basic_map_sort_constraints(
9336 __isl_take isl_basic_map
*bmap
)
9342 if (bmap
->n_ineq
== 0)
9344 if (ISL_F_ISSET(bmap
, ISL_BASIC_MAP_NORMALIZED
))
9346 total
= isl_basic_map_total_dim(bmap
);
9347 if (isl_sort(bmap
->ineq
, bmap
->n_ineq
, sizeof(isl_int
*),
9348 &sort_constraint_cmp
, &total
) < 0)
9349 return isl_basic_map_free(bmap
);
9353 __isl_give isl_basic_set
*isl_basic_set_sort_constraints(
9354 __isl_take isl_basic_set
*bset
)
9356 isl_basic_map
*bmap
= bset_to_bmap(bset
);
9357 return bset_from_bmap(isl_basic_map_sort_constraints(bmap
));
9360 struct isl_basic_map
*isl_basic_map_normalize(struct isl_basic_map
*bmap
)
9364 if (ISL_F_ISSET(bmap
, ISL_BASIC_MAP_NORMALIZED
))
9366 bmap
= isl_basic_map_remove_redundancies(bmap
);
9367 bmap
= isl_basic_map_sort_constraints(bmap
);
9369 ISL_F_SET(bmap
, ISL_BASIC_MAP_NORMALIZED
);
9372 int isl_basic_map_plain_cmp(__isl_keep isl_basic_map
*bmap1
,
9373 __isl_keep isl_basic_map
*bmap2
)
9377 isl_space
*space1
, *space2
;
9379 if (!bmap1
|| !bmap2
)
9384 space1
= isl_basic_map_peek_space(bmap1
);
9385 space2
= isl_basic_map_peek_space(bmap2
);
9386 cmp
= isl_space_cmp(space1
, space2
);
9389 if (ISL_F_ISSET(bmap1
, ISL_BASIC_MAP_RATIONAL
) !=
9390 ISL_F_ISSET(bmap2
, ISL_BASIC_MAP_RATIONAL
))
9391 return ISL_F_ISSET(bmap1
, ISL_BASIC_MAP_RATIONAL
) ? -1 : 1;
9392 if (ISL_F_ISSET(bmap1
, ISL_BASIC_MAP_EMPTY
) &&
9393 ISL_F_ISSET(bmap2
, ISL_BASIC_MAP_EMPTY
))
9395 if (ISL_F_ISSET(bmap1
, ISL_BASIC_MAP_EMPTY
))
9397 if (ISL_F_ISSET(bmap2
, ISL_BASIC_MAP_EMPTY
))
9399 if (bmap1
->n_eq
!= bmap2
->n_eq
)
9400 return bmap1
->n_eq
- bmap2
->n_eq
;
9401 if (bmap1
->n_ineq
!= bmap2
->n_ineq
)
9402 return bmap1
->n_ineq
- bmap2
->n_ineq
;
9403 if (bmap1
->n_div
!= bmap2
->n_div
)
9404 return bmap1
->n_div
- bmap2
->n_div
;
9405 total
= isl_basic_map_total_dim(bmap1
);
9406 for (i
= 0; i
< bmap1
->n_eq
; ++i
) {
9407 cmp
= isl_seq_cmp(bmap1
->eq
[i
], bmap2
->eq
[i
], 1+total
);
9411 for (i
= 0; i
< bmap1
->n_ineq
; ++i
) {
9412 cmp
= isl_seq_cmp(bmap1
->ineq
[i
], bmap2
->ineq
[i
], 1+total
);
9416 for (i
= 0; i
< bmap1
->n_div
; ++i
) {
9417 cmp
= isl_seq_cmp(bmap1
->div
[i
], bmap2
->div
[i
], 1+1+total
);
9424 int isl_basic_set_plain_cmp(__isl_keep isl_basic_set
*bset1
,
9425 __isl_keep isl_basic_set
*bset2
)
9427 return isl_basic_map_plain_cmp(bset1
, bset2
);
9430 int isl_set_plain_cmp(__isl_keep isl_set
*set1
, __isl_keep isl_set
*set2
)
9436 if (set1
->n
!= set2
->n
)
9437 return set1
->n
- set2
->n
;
9439 for (i
= 0; i
< set1
->n
; ++i
) {
9440 cmp
= isl_basic_set_plain_cmp(set1
->p
[i
], set2
->p
[i
]);
9448 isl_bool
isl_basic_map_plain_is_equal(__isl_keep isl_basic_map
*bmap1
,
9449 __isl_keep isl_basic_map
*bmap2
)
9451 if (!bmap1
|| !bmap2
)
9452 return isl_bool_error
;
9453 return isl_basic_map_plain_cmp(bmap1
, bmap2
) == 0;
9456 isl_bool
isl_basic_set_plain_is_equal(__isl_keep isl_basic_set
*bset1
,
9457 __isl_keep isl_basic_set
*bset2
)
9459 return isl_basic_map_plain_is_equal(bset_to_bmap(bset1
),
9460 bset_to_bmap(bset2
));
9463 static int qsort_bmap_cmp(const void *p1
, const void *p2
)
9465 isl_basic_map
*bmap1
= *(isl_basic_map
**) p1
;
9466 isl_basic_map
*bmap2
= *(isl_basic_map
**) p2
;
9468 return isl_basic_map_plain_cmp(bmap1
, bmap2
);
9471 /* Sort the basic maps of "map" and remove duplicate basic maps.
9473 * While removing basic maps, we make sure that the basic maps remain
9474 * sorted because isl_map_normalize expects the basic maps of the result
9477 static __isl_give isl_map
*sort_and_remove_duplicates(__isl_take isl_map
*map
)
9481 map
= isl_map_remove_empty_parts(map
);
9484 qsort(map
->p
, map
->n
, sizeof(struct isl_basic_map
*), qsort_bmap_cmp
);
9485 for (i
= map
->n
- 1; i
>= 1; --i
) {
9486 if (!isl_basic_map_plain_is_equal(map
->p
[i
- 1], map
->p
[i
]))
9488 isl_basic_map_free(map
->p
[i
-1]);
9489 for (j
= i
; j
< map
->n
; ++j
)
9490 map
->p
[j
- 1] = map
->p
[j
];
9497 /* Remove obvious duplicates among the basic maps of "map".
9499 * Unlike isl_map_normalize, this function does not remove redundant
9500 * constraints and only removes duplicates that have exactly the same
9501 * constraints in the input. It does sort the constraints and
9502 * the basic maps to ease the detection of duplicates.
9504 * If "map" has already been normalized or if the basic maps are
9505 * disjoint, then there can be no duplicates.
9507 __isl_give isl_map
*isl_map_remove_obvious_duplicates(__isl_take isl_map
*map
)
9510 isl_basic_map
*bmap
;
9516 if (ISL_F_ISSET(map
, ISL_MAP_NORMALIZED
| ISL_MAP_DISJOINT
))
9518 for (i
= 0; i
< map
->n
; ++i
) {
9519 bmap
= isl_basic_map_copy(map
->p
[i
]);
9520 bmap
= isl_basic_map_sort_constraints(bmap
);
9522 return isl_map_free(map
);
9523 isl_basic_map_free(map
->p
[i
]);
9527 map
= sort_and_remove_duplicates(map
);
9531 /* We normalize in place, but if anything goes wrong we need
9532 * to return NULL, so we need to make sure we don't change the
9533 * meaning of any possible other copies of map.
9535 __isl_give isl_map
*isl_map_normalize(__isl_take isl_map
*map
)
9538 struct isl_basic_map
*bmap
;
9542 if (ISL_F_ISSET(map
, ISL_MAP_NORMALIZED
))
9544 for (i
= 0; i
< map
->n
; ++i
) {
9545 bmap
= isl_basic_map_normalize(isl_basic_map_copy(map
->p
[i
]));
9548 isl_basic_map_free(map
->p
[i
]);
9552 map
= sort_and_remove_duplicates(map
);
9554 ISL_F_SET(map
, ISL_MAP_NORMALIZED
);
9561 struct isl_set
*isl_set_normalize(struct isl_set
*set
)
9563 return set_from_map(isl_map_normalize(set_to_map(set
)));
9566 isl_bool
isl_map_plain_is_equal(__isl_keep isl_map
*map1
,
9567 __isl_keep isl_map
*map2
)
9573 return isl_bool_error
;
9576 return isl_bool_true
;
9577 if (!isl_space_is_equal(map1
->dim
, map2
->dim
))
9578 return isl_bool_false
;
9580 map1
= isl_map_copy(map1
);
9581 map2
= isl_map_copy(map2
);
9582 map1
= isl_map_normalize(map1
);
9583 map2
= isl_map_normalize(map2
);
9586 equal
= map1
->n
== map2
->n
;
9587 for (i
= 0; equal
&& i
< map1
->n
; ++i
) {
9588 equal
= isl_basic_map_plain_is_equal(map1
->p
[i
], map2
->p
[i
]);
9598 return isl_bool_error
;
9601 isl_bool
isl_set_plain_is_equal(__isl_keep isl_set
*set1
,
9602 __isl_keep isl_set
*set2
)
9604 return isl_map_plain_is_equal(set_to_map(set1
), set_to_map(set2
));
9607 /* Return the basic maps in "map" as a list.
9609 __isl_give isl_basic_map_list
*isl_map_get_basic_map_list(
9610 __isl_keep isl_map
*map
)
9614 isl_basic_map_list
*list
;
9618 ctx
= isl_map_get_ctx(map
);
9619 list
= isl_basic_map_list_alloc(ctx
, map
->n
);
9621 for (i
= 0; i
< map
->n
; ++i
) {
9622 isl_basic_map
*bmap
;
9624 bmap
= isl_basic_map_copy(map
->p
[i
]);
9625 list
= isl_basic_map_list_add(list
, bmap
);
9631 /* Return the intersection of the elements in the non-empty list "list".
9632 * All elements are assumed to live in the same space.
9634 __isl_give isl_basic_map
*isl_basic_map_list_intersect(
9635 __isl_take isl_basic_map_list
*list
)
9638 isl_basic_map
*bmap
;
9642 n
= isl_basic_map_list_n_basic_map(list
);
9644 isl_die(isl_basic_map_list_get_ctx(list
), isl_error_invalid
,
9645 "expecting non-empty list", goto error
);
9647 bmap
= isl_basic_map_list_get_basic_map(list
, 0);
9648 for (i
= 1; i
< n
; ++i
) {
9649 isl_basic_map
*bmap_i
;
9651 bmap_i
= isl_basic_map_list_get_basic_map(list
, i
);
9652 bmap
= isl_basic_map_intersect(bmap
, bmap_i
);
9655 isl_basic_map_list_free(list
);
9658 isl_basic_map_list_free(list
);
9662 /* Return the intersection of the elements in the non-empty list "list".
9663 * All elements are assumed to live in the same space.
9665 __isl_give isl_basic_set
*isl_basic_set_list_intersect(
9666 __isl_take isl_basic_set_list
*list
)
9668 return isl_basic_map_list_intersect(list
);
9671 /* Return the union of the elements of "list".
9672 * The list is required to have at least one element.
9674 __isl_give isl_set
*isl_basic_set_list_union(
9675 __isl_take isl_basic_set_list
*list
)
9679 isl_basic_set
*bset
;
9684 n
= isl_basic_set_list_n_basic_set(list
);
9686 isl_die(isl_basic_set_list_get_ctx(list
), isl_error_invalid
,
9687 "expecting non-empty list", goto error
);
9689 bset
= isl_basic_set_list_get_basic_set(list
, 0);
9690 space
= isl_basic_set_get_space(bset
);
9691 isl_basic_set_free(bset
);
9693 set
= isl_set_alloc_space(space
, n
, 0);
9694 for (i
= 0; i
< n
; ++i
) {
9695 bset
= isl_basic_set_list_get_basic_set(list
, i
);
9696 set
= isl_set_add_basic_set(set
, bset
);
9699 isl_basic_set_list_free(list
);
9702 isl_basic_set_list_free(list
);
9706 /* Return the union of the elements in the non-empty list "list".
9707 * All elements are assumed to live in the same space.
9709 __isl_give isl_set
*isl_set_list_union(__isl_take isl_set_list
*list
)
9716 n
= isl_set_list_n_set(list
);
9718 isl_die(isl_set_list_get_ctx(list
), isl_error_invalid
,
9719 "expecting non-empty list", goto error
);
9721 set
= isl_set_list_get_set(list
, 0);
9722 for (i
= 1; i
< n
; ++i
) {
9725 set_i
= isl_set_list_get_set(list
, i
);
9726 set
= isl_set_union(set
, set_i
);
9729 isl_set_list_free(list
);
9732 isl_set_list_free(list
);
9736 __isl_give isl_basic_map
*isl_basic_map_product(
9737 __isl_take isl_basic_map
*bmap1
, __isl_take isl_basic_map
*bmap2
)
9739 isl_space
*dim_result
= NULL
;
9740 struct isl_basic_map
*bmap
;
9741 unsigned in1
, in2
, out1
, out2
, nparam
, total
, pos
;
9742 struct isl_dim_map
*dim_map1
, *dim_map2
;
9744 if (isl_basic_map_check_equal_params(bmap1
, bmap2
) < 0)
9746 dim_result
= isl_space_product(isl_space_copy(bmap1
->dim
),
9747 isl_space_copy(bmap2
->dim
));
9749 in1
= isl_basic_map_dim(bmap1
, isl_dim_in
);
9750 in2
= isl_basic_map_dim(bmap2
, isl_dim_in
);
9751 out1
= isl_basic_map_dim(bmap1
, isl_dim_out
);
9752 out2
= isl_basic_map_dim(bmap2
, isl_dim_out
);
9753 nparam
= isl_basic_map_dim(bmap1
, isl_dim_param
);
9755 total
= nparam
+ in1
+ in2
+ out1
+ out2
+ bmap1
->n_div
+ bmap2
->n_div
;
9756 dim_map1
= isl_dim_map_alloc(bmap1
->ctx
, total
);
9757 dim_map2
= isl_dim_map_alloc(bmap1
->ctx
, total
);
9758 isl_dim_map_dim(dim_map1
, bmap1
->dim
, isl_dim_param
, pos
= 0);
9759 isl_dim_map_dim(dim_map2
, bmap2
->dim
, isl_dim_param
, pos
= 0);
9760 isl_dim_map_dim(dim_map1
, bmap1
->dim
, isl_dim_in
, pos
+= nparam
);
9761 isl_dim_map_dim(dim_map2
, bmap2
->dim
, isl_dim_in
, pos
+= in1
);
9762 isl_dim_map_dim(dim_map1
, bmap1
->dim
, isl_dim_out
, pos
+= in2
);
9763 isl_dim_map_dim(dim_map2
, bmap2
->dim
, isl_dim_out
, pos
+= out1
);
9764 isl_dim_map_div(dim_map1
, bmap1
, pos
+= out2
);
9765 isl_dim_map_div(dim_map2
, bmap2
, pos
+= bmap1
->n_div
);
9767 bmap
= isl_basic_map_alloc_space(dim_result
,
9768 bmap1
->n_div
+ bmap2
->n_div
,
9769 bmap1
->n_eq
+ bmap2
->n_eq
,
9770 bmap1
->n_ineq
+ bmap2
->n_ineq
);
9771 bmap
= isl_basic_map_add_constraints_dim_map(bmap
, bmap1
, dim_map1
);
9772 bmap
= isl_basic_map_add_constraints_dim_map(bmap
, bmap2
, dim_map2
);
9773 bmap
= isl_basic_map_simplify(bmap
);
9774 return isl_basic_map_finalize(bmap
);
9776 isl_basic_map_free(bmap1
);
9777 isl_basic_map_free(bmap2
);
9781 __isl_give isl_basic_map
*isl_basic_map_flat_product(
9782 __isl_take isl_basic_map
*bmap1
, __isl_take isl_basic_map
*bmap2
)
9784 isl_basic_map
*prod
;
9786 prod
= isl_basic_map_product(bmap1
, bmap2
);
9787 prod
= isl_basic_map_flatten(prod
);
9791 __isl_give isl_basic_set
*isl_basic_set_flat_product(
9792 __isl_take isl_basic_set
*bset1
, __isl_take isl_basic_set
*bset2
)
9794 return isl_basic_map_flat_range_product(bset1
, bset2
);
9797 __isl_give isl_basic_map
*isl_basic_map_domain_product(
9798 __isl_take isl_basic_map
*bmap1
, __isl_take isl_basic_map
*bmap2
)
9800 isl_space
*space_result
= NULL
;
9801 isl_basic_map
*bmap
;
9802 unsigned in1
, in2
, out
, nparam
, total
, pos
;
9803 struct isl_dim_map
*dim_map1
, *dim_map2
;
9805 if (!bmap1
|| !bmap2
)
9808 space_result
= isl_space_domain_product(isl_space_copy(bmap1
->dim
),
9809 isl_space_copy(bmap2
->dim
));
9811 in1
= isl_basic_map_dim(bmap1
, isl_dim_in
);
9812 in2
= isl_basic_map_dim(bmap2
, isl_dim_in
);
9813 out
= isl_basic_map_dim(bmap1
, isl_dim_out
);
9814 nparam
= isl_basic_map_dim(bmap1
, isl_dim_param
);
9816 total
= nparam
+ in1
+ in2
+ out
+ bmap1
->n_div
+ bmap2
->n_div
;
9817 dim_map1
= isl_dim_map_alloc(bmap1
->ctx
, total
);
9818 dim_map2
= isl_dim_map_alloc(bmap1
->ctx
, total
);
9819 isl_dim_map_dim(dim_map1
, bmap1
->dim
, isl_dim_param
, pos
= 0);
9820 isl_dim_map_dim(dim_map2
, bmap2
->dim
, isl_dim_param
, pos
= 0);
9821 isl_dim_map_dim(dim_map1
, bmap1
->dim
, isl_dim_in
, pos
+= nparam
);
9822 isl_dim_map_dim(dim_map2
, bmap2
->dim
, isl_dim_in
, pos
+= in1
);
9823 isl_dim_map_dim(dim_map1
, bmap1
->dim
, isl_dim_out
, pos
+= in2
);
9824 isl_dim_map_dim(dim_map2
, bmap2
->dim
, isl_dim_out
, pos
);
9825 isl_dim_map_div(dim_map1
, bmap1
, pos
+= out
);
9826 isl_dim_map_div(dim_map2
, bmap2
, pos
+= bmap1
->n_div
);
9828 bmap
= isl_basic_map_alloc_space(space_result
,
9829 bmap1
->n_div
+ bmap2
->n_div
,
9830 bmap1
->n_eq
+ bmap2
->n_eq
,
9831 bmap1
->n_ineq
+ bmap2
->n_ineq
);
9832 bmap
= isl_basic_map_add_constraints_dim_map(bmap
, bmap1
, dim_map1
);
9833 bmap
= isl_basic_map_add_constraints_dim_map(bmap
, bmap2
, dim_map2
);
9834 bmap
= isl_basic_map_simplify(bmap
);
9835 return isl_basic_map_finalize(bmap
);
9837 isl_basic_map_free(bmap1
);
9838 isl_basic_map_free(bmap2
);
9842 __isl_give isl_basic_map
*isl_basic_map_range_product(
9843 __isl_take isl_basic_map
*bmap1
, __isl_take isl_basic_map
*bmap2
)
9846 isl_space
*dim_result
= NULL
;
9847 isl_basic_map
*bmap
;
9848 unsigned in
, out1
, out2
, nparam
, total
, pos
;
9849 struct isl_dim_map
*dim_map1
, *dim_map2
;
9851 rational
= isl_basic_map_is_rational(bmap1
);
9852 if (rational
>= 0 && rational
)
9853 rational
= isl_basic_map_is_rational(bmap2
);
9854 if (!bmap1
|| !bmap2
|| rational
< 0)
9857 if (isl_basic_map_check_equal_params(bmap1
, bmap2
) < 0)
9860 dim_result
= isl_space_range_product(isl_space_copy(bmap1
->dim
),
9861 isl_space_copy(bmap2
->dim
));
9863 in
= isl_basic_map_dim(bmap1
, isl_dim_in
);
9864 out1
= isl_basic_map_dim(bmap1
, isl_dim_out
);
9865 out2
= isl_basic_map_dim(bmap2
, isl_dim_out
);
9866 nparam
= isl_basic_map_dim(bmap1
, isl_dim_param
);
9868 total
= nparam
+ in
+ out1
+ out2
+ bmap1
->n_div
+ bmap2
->n_div
;
9869 dim_map1
= isl_dim_map_alloc(bmap1
->ctx
, total
);
9870 dim_map2
= isl_dim_map_alloc(bmap1
->ctx
, total
);
9871 isl_dim_map_dim(dim_map1
, bmap1
->dim
, isl_dim_param
, pos
= 0);
9872 isl_dim_map_dim(dim_map2
, bmap2
->dim
, isl_dim_param
, pos
= 0);
9873 isl_dim_map_dim(dim_map1
, bmap1
->dim
, isl_dim_in
, pos
+= nparam
);
9874 isl_dim_map_dim(dim_map2
, bmap2
->dim
, isl_dim_in
, pos
);
9875 isl_dim_map_dim(dim_map1
, bmap1
->dim
, isl_dim_out
, pos
+= in
);
9876 isl_dim_map_dim(dim_map2
, bmap2
->dim
, isl_dim_out
, pos
+= out1
);
9877 isl_dim_map_div(dim_map1
, bmap1
, pos
+= out2
);
9878 isl_dim_map_div(dim_map2
, bmap2
, pos
+= bmap1
->n_div
);
9880 bmap
= isl_basic_map_alloc_space(dim_result
,
9881 bmap1
->n_div
+ bmap2
->n_div
,
9882 bmap1
->n_eq
+ bmap2
->n_eq
,
9883 bmap1
->n_ineq
+ bmap2
->n_ineq
);
9884 bmap
= isl_basic_map_add_constraints_dim_map(bmap
, bmap1
, dim_map1
);
9885 bmap
= isl_basic_map_add_constraints_dim_map(bmap
, bmap2
, dim_map2
);
9887 bmap
= isl_basic_map_set_rational(bmap
);
9888 bmap
= isl_basic_map_simplify(bmap
);
9889 return isl_basic_map_finalize(bmap
);
9891 isl_basic_map_free(bmap1
);
9892 isl_basic_map_free(bmap2
);
9896 __isl_give isl_basic_map
*isl_basic_map_flat_range_product(
9897 __isl_take isl_basic_map
*bmap1
, __isl_take isl_basic_map
*bmap2
)
9899 isl_basic_map
*prod
;
9901 prod
= isl_basic_map_range_product(bmap1
, bmap2
);
9902 prod
= isl_basic_map_flatten_range(prod
);
9906 /* Apply "basic_map_product" to each pair of basic maps in "map1" and "map2"
9907 * and collect the results.
9908 * The result live in the space obtained by calling "space_product"
9909 * on the spaces of "map1" and "map2".
9910 * If "remove_duplicates" is set then the result may contain duplicates
9911 * (even if the inputs do not) and so we try and remove the obvious
9914 static __isl_give isl_map
*map_product(__isl_take isl_map
*map1
,
9915 __isl_take isl_map
*map2
,
9916 __isl_give isl_space
*(*space_product
)(__isl_take isl_space
*left
,
9917 __isl_take isl_space
*right
),
9918 __isl_give isl_basic_map
*(*basic_map_product
)(
9919 __isl_take isl_basic_map
*left
,
9920 __isl_take isl_basic_map
*right
),
9921 int remove_duplicates
)
9924 struct isl_map
*result
;
9928 m
= isl_map_has_equal_params(map1
, map2
);
9932 isl_die(isl_map_get_ctx(map1
), isl_error_invalid
,
9933 "parameters don't match", goto error
);
9935 if (ISL_F_ISSET(map1
, ISL_MAP_DISJOINT
) &&
9936 ISL_F_ISSET(map2
, ISL_MAP_DISJOINT
))
9937 ISL_FL_SET(flags
, ISL_MAP_DISJOINT
);
9939 result
= isl_map_alloc_space(space_product(isl_space_copy(map1
->dim
),
9940 isl_space_copy(map2
->dim
)),
9941 map1
->n
* map2
->n
, flags
);
9944 for (i
= 0; i
< map1
->n
; ++i
)
9945 for (j
= 0; j
< map2
->n
; ++j
) {
9946 struct isl_basic_map
*part
;
9947 part
= basic_map_product(isl_basic_map_copy(map1
->p
[i
]),
9948 isl_basic_map_copy(map2
->p
[j
]));
9949 if (isl_basic_map_is_empty(part
))
9950 isl_basic_map_free(part
);
9952 result
= isl_map_add_basic_map(result
, part
);
9956 if (remove_duplicates
)
9957 result
= isl_map_remove_obvious_duplicates(result
);
9967 /* Given two maps A -> B and C -> D, construct a map [A -> C] -> [B -> D]
9969 static __isl_give isl_map
*map_product_aligned(__isl_take isl_map
*map1
,
9970 __isl_take isl_map
*map2
)
9972 return map_product(map1
, map2
, &isl_space_product
,
9973 &isl_basic_map_product
, 0);
9976 __isl_give isl_map
*isl_map_product(__isl_take isl_map
*map1
,
9977 __isl_take isl_map
*map2
)
9979 return isl_map_align_params_map_map_and(map1
, map2
, &map_product_aligned
);
9982 /* Given two maps A -> B and C -> D, construct a map (A, C) -> (B, D)
9984 __isl_give isl_map
*isl_map_flat_product(__isl_take isl_map
*map1
,
9985 __isl_take isl_map
*map2
)
9989 prod
= isl_map_product(map1
, map2
);
9990 prod
= isl_map_flatten(prod
);
9994 /* Given two set A and B, construct its Cartesian product A x B.
9996 struct isl_set
*isl_set_product(struct isl_set
*set1
, struct isl_set
*set2
)
9998 return isl_map_range_product(set1
, set2
);
10001 __isl_give isl_set
*isl_set_flat_product(__isl_take isl_set
*set1
,
10002 __isl_take isl_set
*set2
)
10004 return isl_map_flat_range_product(set1
, set2
);
10007 /* Given two maps A -> B and C -> D, construct a map [A -> C] -> (B * D)
10009 static __isl_give isl_map
*map_domain_product_aligned(__isl_take isl_map
*map1
,
10010 __isl_take isl_map
*map2
)
10012 return map_product(map1
, map2
, &isl_space_domain_product
,
10013 &isl_basic_map_domain_product
, 1);
10016 /* Given two maps A -> B and C -> D, construct a map (A * C) -> [B -> D]
10018 static __isl_give isl_map
*map_range_product_aligned(__isl_take isl_map
*map1
,
10019 __isl_take isl_map
*map2
)
10021 return map_product(map1
, map2
, &isl_space_range_product
,
10022 &isl_basic_map_range_product
, 1);
10025 __isl_give isl_map
*isl_map_domain_product(__isl_take isl_map
*map1
,
10026 __isl_take isl_map
*map2
)
10028 return isl_map_align_params_map_map_and(map1
, map2
,
10029 &map_domain_product_aligned
);
10032 __isl_give isl_map
*isl_map_range_product(__isl_take isl_map
*map1
,
10033 __isl_take isl_map
*map2
)
10035 return isl_map_align_params_map_map_and(map1
, map2
,
10036 &map_range_product_aligned
);
10039 /* Given a map of the form [A -> B] -> [C -> D], return the map A -> C.
10041 __isl_give isl_map
*isl_map_factor_domain(__isl_take isl_map
*map
)
10044 int total1
, keep1
, total2
, keep2
;
10048 if (!isl_space_domain_is_wrapping(map
->dim
) ||
10049 !isl_space_range_is_wrapping(map
->dim
))
10050 isl_die(isl_map_get_ctx(map
), isl_error_invalid
,
10051 "not a product", return isl_map_free(map
));
10053 space
= isl_map_get_space(map
);
10054 total1
= isl_space_dim(space
, isl_dim_in
);
10055 total2
= isl_space_dim(space
, isl_dim_out
);
10056 space
= isl_space_factor_domain(space
);
10057 keep1
= isl_space_dim(space
, isl_dim_in
);
10058 keep2
= isl_space_dim(space
, isl_dim_out
);
10059 map
= isl_map_project_out(map
, isl_dim_in
, keep1
, total1
- keep1
);
10060 map
= isl_map_project_out(map
, isl_dim_out
, keep2
, total2
- keep2
);
10061 map
= isl_map_reset_space(map
, space
);
10066 /* Given a map of the form [A -> B] -> [C -> D], return the map B -> D.
10068 __isl_give isl_map
*isl_map_factor_range(__isl_take isl_map
*map
)
10071 int total1
, keep1
, total2
, keep2
;
10075 if (!isl_space_domain_is_wrapping(map
->dim
) ||
10076 !isl_space_range_is_wrapping(map
->dim
))
10077 isl_die(isl_map_get_ctx(map
), isl_error_invalid
,
10078 "not a product", return isl_map_free(map
));
10080 space
= isl_map_get_space(map
);
10081 total1
= isl_space_dim(space
, isl_dim_in
);
10082 total2
= isl_space_dim(space
, isl_dim_out
);
10083 space
= isl_space_factor_range(space
);
10084 keep1
= isl_space_dim(space
, isl_dim_in
);
10085 keep2
= isl_space_dim(space
, isl_dim_out
);
10086 map
= isl_map_project_out(map
, isl_dim_in
, 0, total1
- keep1
);
10087 map
= isl_map_project_out(map
, isl_dim_out
, 0, total2
- keep2
);
10088 map
= isl_map_reset_space(map
, space
);
10093 /* Given a map of the form [A -> B] -> C, return the map A -> C.
10095 __isl_give isl_map
*isl_map_domain_factor_domain(__isl_take isl_map
*map
)
10102 if (!isl_space_domain_is_wrapping(map
->dim
))
10103 isl_die(isl_map_get_ctx(map
), isl_error_invalid
,
10104 "domain is not a product", return isl_map_free(map
));
10106 space
= isl_map_get_space(map
);
10107 total
= isl_space_dim(space
, isl_dim_in
);
10108 space
= isl_space_domain_factor_domain(space
);
10109 keep
= isl_space_dim(space
, isl_dim_in
);
10110 map
= isl_map_project_out(map
, isl_dim_in
, keep
, total
- keep
);
10111 map
= isl_map_reset_space(map
, space
);
10116 /* Given a map of the form [A -> B] -> C, return the map B -> C.
10118 __isl_give isl_map
*isl_map_domain_factor_range(__isl_take isl_map
*map
)
10125 if (!isl_space_domain_is_wrapping(map
->dim
))
10126 isl_die(isl_map_get_ctx(map
), isl_error_invalid
,
10127 "domain is not a product", return isl_map_free(map
));
10129 space
= isl_map_get_space(map
);
10130 total
= isl_space_dim(space
, isl_dim_in
);
10131 space
= isl_space_domain_factor_range(space
);
10132 keep
= isl_space_dim(space
, isl_dim_in
);
10133 map
= isl_map_project_out(map
, isl_dim_in
, 0, total
- keep
);
10134 map
= isl_map_reset_space(map
, space
);
10139 /* Given a map A -> [B -> C], extract the map A -> B.
10141 __isl_give isl_map
*isl_map_range_factor_domain(__isl_take isl_map
*map
)
10148 if (!isl_space_range_is_wrapping(map
->dim
))
10149 isl_die(isl_map_get_ctx(map
), isl_error_invalid
,
10150 "range is not a product", return isl_map_free(map
));
10152 space
= isl_map_get_space(map
);
10153 total
= isl_space_dim(space
, isl_dim_out
);
10154 space
= isl_space_range_factor_domain(space
);
10155 keep
= isl_space_dim(space
, isl_dim_out
);
10156 map
= isl_map_project_out(map
, isl_dim_out
, keep
, total
- keep
);
10157 map
= isl_map_reset_space(map
, space
);
10162 /* Given a map A -> [B -> C], extract the map A -> C.
10164 __isl_give isl_map
*isl_map_range_factor_range(__isl_take isl_map
*map
)
10171 if (!isl_space_range_is_wrapping(map
->dim
))
10172 isl_die(isl_map_get_ctx(map
), isl_error_invalid
,
10173 "range is not a product", return isl_map_free(map
));
10175 space
= isl_map_get_space(map
);
10176 total
= isl_space_dim(space
, isl_dim_out
);
10177 space
= isl_space_range_factor_range(space
);
10178 keep
= isl_space_dim(space
, isl_dim_out
);
10179 map
= isl_map_project_out(map
, isl_dim_out
, 0, total
- keep
);
10180 map
= isl_map_reset_space(map
, space
);
10185 /* Given two maps A -> B and C -> D, construct a map (A, C) -> (B * D)
10187 __isl_give isl_map
*isl_map_flat_domain_product(__isl_take isl_map
*map1
,
10188 __isl_take isl_map
*map2
)
10192 prod
= isl_map_domain_product(map1
, map2
);
10193 prod
= isl_map_flatten_domain(prod
);
10197 /* Given two maps A -> B and C -> D, construct a map (A * C) -> (B, D)
10199 __isl_give isl_map
*isl_map_flat_range_product(__isl_take isl_map
*map1
,
10200 __isl_take isl_map
*map2
)
10204 prod
= isl_map_range_product(map1
, map2
);
10205 prod
= isl_map_flatten_range(prod
);
10209 uint32_t isl_basic_map_get_hash(__isl_keep isl_basic_map
*bmap
)
10212 uint32_t hash
= isl_hash_init();
10217 bmap
= isl_basic_map_copy(bmap
);
10218 bmap
= isl_basic_map_normalize(bmap
);
10221 total
= isl_basic_map_total_dim(bmap
);
10222 isl_hash_byte(hash
, bmap
->n_eq
& 0xFF);
10223 for (i
= 0; i
< bmap
->n_eq
; ++i
) {
10225 c_hash
= isl_seq_get_hash(bmap
->eq
[i
], 1 + total
);
10226 isl_hash_hash(hash
, c_hash
);
10228 isl_hash_byte(hash
, bmap
->n_ineq
& 0xFF);
10229 for (i
= 0; i
< bmap
->n_ineq
; ++i
) {
10231 c_hash
= isl_seq_get_hash(bmap
->ineq
[i
], 1 + total
);
10232 isl_hash_hash(hash
, c_hash
);
10234 isl_hash_byte(hash
, bmap
->n_div
& 0xFF);
10235 for (i
= 0; i
< bmap
->n_div
; ++i
) {
10237 if (isl_int_is_zero(bmap
->div
[i
][0]))
10239 isl_hash_byte(hash
, i
& 0xFF);
10240 c_hash
= isl_seq_get_hash(bmap
->div
[i
], 1 + 1 + total
);
10241 isl_hash_hash(hash
, c_hash
);
10243 isl_basic_map_free(bmap
);
10247 uint32_t isl_basic_set_get_hash(__isl_keep isl_basic_set
*bset
)
10249 return isl_basic_map_get_hash(bset_to_bmap(bset
));
10252 uint32_t isl_map_get_hash(__isl_keep isl_map
*map
)
10259 map
= isl_map_copy(map
);
10260 map
= isl_map_normalize(map
);
10264 hash
= isl_hash_init();
10265 for (i
= 0; i
< map
->n
; ++i
) {
10266 uint32_t bmap_hash
;
10267 bmap_hash
= isl_basic_map_get_hash(map
->p
[i
]);
10268 isl_hash_hash(hash
, bmap_hash
);
10276 uint32_t isl_set_get_hash(__isl_keep isl_set
*set
)
10278 return isl_map_get_hash(set_to_map(set
));
10281 /* Return the number of basic maps in the (current) representation of "map".
10283 int isl_map_n_basic_map(__isl_keep isl_map
*map
)
10285 return map
? map
->n
: 0;
10288 int isl_set_n_basic_set(__isl_keep isl_set
*set
)
10290 return set
? set
->n
: 0;
10293 isl_stat
isl_map_foreach_basic_map(__isl_keep isl_map
*map
,
10294 isl_stat (*fn
)(__isl_take isl_basic_map
*bmap
, void *user
), void *user
)
10299 return isl_stat_error
;
10301 for (i
= 0; i
< map
->n
; ++i
)
10302 if (fn(isl_basic_map_copy(map
->p
[i
]), user
) < 0)
10303 return isl_stat_error
;
10305 return isl_stat_ok
;
10308 isl_stat
isl_set_foreach_basic_set(__isl_keep isl_set
*set
,
10309 isl_stat (*fn
)(__isl_take isl_basic_set
*bset
, void *user
), void *user
)
10314 return isl_stat_error
;
10316 for (i
= 0; i
< set
->n
; ++i
)
10317 if (fn(isl_basic_set_copy(set
->p
[i
]), user
) < 0)
10318 return isl_stat_error
;
10320 return isl_stat_ok
;
10323 /* Return a list of basic sets, the union of which is equal to "set".
10325 __isl_give isl_basic_set_list
*isl_set_get_basic_set_list(
10326 __isl_keep isl_set
*set
)
10329 isl_basic_set_list
*list
;
10334 list
= isl_basic_set_list_alloc(isl_set_get_ctx(set
), set
->n
);
10335 for (i
= 0; i
< set
->n
; ++i
) {
10336 isl_basic_set
*bset
;
10338 bset
= isl_basic_set_copy(set
->p
[i
]);
10339 list
= isl_basic_set_list_add(list
, bset
);
10345 __isl_give isl_basic_set
*isl_basic_set_lift(__isl_take isl_basic_set
*bset
)
10352 bset
= isl_basic_set_cow(bset
);
10356 dim
= isl_basic_set_get_space(bset
);
10357 dim
= isl_space_lift(dim
, bset
->n_div
);
10360 isl_space_free(bset
->dim
);
10362 bset
->extra
-= bset
->n_div
;
10365 bset
= isl_basic_set_finalize(bset
);
10369 isl_basic_set_free(bset
);
10373 __isl_give isl_set
*isl_set_lift(__isl_take isl_set
*set
)
10379 set
= set_from_map(isl_map_align_divs_internal(set_to_map(set
)));
10384 set
= isl_set_cow(set
);
10388 n_div
= set
->p
[0]->n_div
;
10389 dim
= isl_set_get_space(set
);
10390 dim
= isl_space_lift(dim
, n_div
);
10393 isl_space_free(set
->dim
);
10396 for (i
= 0; i
< set
->n
; ++i
) {
10397 set
->p
[i
] = isl_basic_set_lift(set
->p
[i
]);
10408 int isl_basic_set_size(__isl_keep isl_basic_set
*bset
)
10416 dim
= isl_basic_set_total_dim(bset
);
10417 size
+= bset
->n_eq
* (1 + dim
);
10418 size
+= bset
->n_ineq
* (1 + dim
);
10419 size
+= bset
->n_div
* (2 + dim
);
10424 int isl_set_size(__isl_keep isl_set
*set
)
10432 for (i
= 0; i
< set
->n
; ++i
)
10433 size
+= isl_basic_set_size(set
->p
[i
]);
10438 /* Check if there is any lower bound (if lower == 0) and/or upper
10439 * bound (if upper == 0) on the specified dim.
10441 static isl_bool
basic_map_dim_is_bounded(__isl_keep isl_basic_map
*bmap
,
10442 enum isl_dim_type type
, unsigned pos
, int lower
, int upper
)
10446 if (isl_basic_map_check_range(bmap
, type
, pos
, 1) < 0)
10447 return isl_bool_error
;
10449 pos
+= isl_basic_map_offset(bmap
, type
);
10451 for (i
= 0; i
< bmap
->n_div
; ++i
) {
10452 if (isl_int_is_zero(bmap
->div
[i
][0]))
10454 if (!isl_int_is_zero(bmap
->div
[i
][1 + pos
]))
10455 return isl_bool_true
;
10458 for (i
= 0; i
< bmap
->n_eq
; ++i
)
10459 if (!isl_int_is_zero(bmap
->eq
[i
][pos
]))
10460 return isl_bool_true
;
10462 for (i
= 0; i
< bmap
->n_ineq
; ++i
) {
10463 int sgn
= isl_int_sgn(bmap
->ineq
[i
][pos
]);
10470 return lower
&& upper
;
10473 isl_bool
isl_basic_map_dim_is_bounded(__isl_keep isl_basic_map
*bmap
,
10474 enum isl_dim_type type
, unsigned pos
)
10476 return basic_map_dim_is_bounded(bmap
, type
, pos
, 0, 0);
10479 isl_bool
isl_basic_map_dim_has_lower_bound(__isl_keep isl_basic_map
*bmap
,
10480 enum isl_dim_type type
, unsigned pos
)
10482 return basic_map_dim_is_bounded(bmap
, type
, pos
, 0, 1);
10485 isl_bool
isl_basic_map_dim_has_upper_bound(__isl_keep isl_basic_map
*bmap
,
10486 enum isl_dim_type type
, unsigned pos
)
10488 return basic_map_dim_is_bounded(bmap
, type
, pos
, 1, 0);
10491 isl_bool
isl_map_dim_is_bounded(__isl_keep isl_map
*map
,
10492 enum isl_dim_type type
, unsigned pos
)
10497 return isl_bool_error
;
10499 for (i
= 0; i
< map
->n
; ++i
) {
10501 bounded
= isl_basic_map_dim_is_bounded(map
->p
[i
], type
, pos
);
10502 if (bounded
< 0 || !bounded
)
10506 return isl_bool_true
;
10509 /* Return true if the specified dim is involved in both an upper bound
10510 * and a lower bound.
10512 isl_bool
isl_set_dim_is_bounded(__isl_keep isl_set
*set
,
10513 enum isl_dim_type type
, unsigned pos
)
10515 return isl_map_dim_is_bounded(set_to_map(set
), type
, pos
);
10518 /* Does "map" have a bound (according to "fn") for any of its basic maps?
10520 static isl_bool
has_any_bound(__isl_keep isl_map
*map
,
10521 enum isl_dim_type type
, unsigned pos
,
10522 isl_bool (*fn
)(__isl_keep isl_basic_map
*bmap
,
10523 enum isl_dim_type type
, unsigned pos
))
10528 return isl_bool_error
;
10530 for (i
= 0; i
< map
->n
; ++i
) {
10532 bounded
= fn(map
->p
[i
], type
, pos
);
10533 if (bounded
< 0 || bounded
)
10537 return isl_bool_false
;
10540 /* Return 1 if the specified dim is involved in any lower bound.
10542 isl_bool
isl_set_dim_has_any_lower_bound(__isl_keep isl_set
*set
,
10543 enum isl_dim_type type
, unsigned pos
)
10545 return has_any_bound(set
, type
, pos
,
10546 &isl_basic_map_dim_has_lower_bound
);
10549 /* Return 1 if the specified dim is involved in any upper bound.
10551 isl_bool
isl_set_dim_has_any_upper_bound(__isl_keep isl_set
*set
,
10552 enum isl_dim_type type
, unsigned pos
)
10554 return has_any_bound(set
, type
, pos
,
10555 &isl_basic_map_dim_has_upper_bound
);
10558 /* Does "map" have a bound (according to "fn") for all of its basic maps?
10560 static isl_bool
has_bound(__isl_keep isl_map
*map
,
10561 enum isl_dim_type type
, unsigned pos
,
10562 isl_bool (*fn
)(__isl_keep isl_basic_map
*bmap
,
10563 enum isl_dim_type type
, unsigned pos
))
10568 return isl_bool_error
;
10570 for (i
= 0; i
< map
->n
; ++i
) {
10572 bounded
= fn(map
->p
[i
], type
, pos
);
10573 if (bounded
< 0 || !bounded
)
10577 return isl_bool_true
;
10580 /* Return 1 if the specified dim has a lower bound (in each of its basic sets).
10582 isl_bool
isl_set_dim_has_lower_bound(__isl_keep isl_set
*set
,
10583 enum isl_dim_type type
, unsigned pos
)
10585 return has_bound(set
, type
, pos
, &isl_basic_map_dim_has_lower_bound
);
10588 /* Return 1 if the specified dim has an upper bound (in each of its basic sets).
10590 isl_bool
isl_set_dim_has_upper_bound(__isl_keep isl_set
*set
,
10591 enum isl_dim_type type
, unsigned pos
)
10593 return has_bound(set
, type
, pos
, &isl_basic_map_dim_has_upper_bound
);
10596 /* For each of the "n" variables starting at "first", determine
10597 * the sign of the variable and put the results in the first "n"
10598 * elements of the array "signs".
10600 * 1 means that the variable is non-negative
10601 * -1 means that the variable is non-positive
10602 * 0 means the variable attains both positive and negative values.
10604 isl_stat
isl_basic_set_vars_get_sign(__isl_keep isl_basic_set
*bset
,
10605 unsigned first
, unsigned n
, int *signs
)
10607 isl_vec
*bound
= NULL
;
10608 struct isl_tab
*tab
= NULL
;
10609 struct isl_tab_undo
*snap
;
10612 if (!bset
|| !signs
)
10613 return isl_stat_error
;
10615 bound
= isl_vec_alloc(bset
->ctx
, 1 + isl_basic_set_total_dim(bset
));
10616 tab
= isl_tab_from_basic_set(bset
, 0);
10617 if (!bound
|| !tab
)
10620 isl_seq_clr(bound
->el
, bound
->size
);
10621 isl_int_set_si(bound
->el
[0], -1);
10623 snap
= isl_tab_snap(tab
);
10624 for (i
= 0; i
< n
; ++i
) {
10627 isl_int_set_si(bound
->el
[1 + first
+ i
], -1);
10628 if (isl_tab_add_ineq(tab
, bound
->el
) < 0)
10630 empty
= tab
->empty
;
10631 isl_int_set_si(bound
->el
[1 + first
+ i
], 0);
10632 if (isl_tab_rollback(tab
, snap
) < 0)
10640 isl_int_set_si(bound
->el
[1 + first
+ i
], 1);
10641 if (isl_tab_add_ineq(tab
, bound
->el
) < 0)
10643 empty
= tab
->empty
;
10644 isl_int_set_si(bound
->el
[1 + first
+ i
], 0);
10645 if (isl_tab_rollback(tab
, snap
) < 0)
10648 signs
[i
] = empty
? -1 : 0;
10652 isl_vec_free(bound
);
10653 return isl_stat_ok
;
10656 isl_vec_free(bound
);
10657 return isl_stat_error
;
10660 isl_stat
isl_basic_set_dims_get_sign(__isl_keep isl_basic_set
*bset
,
10661 enum isl_dim_type type
, unsigned first
, unsigned n
, int *signs
)
10663 if (!bset
|| !signs
)
10664 return isl_stat_error
;
10665 isl_assert(bset
->ctx
, first
+ n
<= isl_basic_set_dim(bset
, type
),
10666 return isl_stat_error
);
10668 first
+= pos(bset
->dim
, type
) - 1;
10669 return isl_basic_set_vars_get_sign(bset
, first
, n
, signs
);
10672 /* Is it possible for the integer division "div" to depend (possibly
10673 * indirectly) on any output dimensions?
10675 * If the div is undefined, then we conservatively assume that it
10676 * may depend on them.
10677 * Otherwise, we check if it actually depends on them or on any integer
10678 * divisions that may depend on them.
10680 static isl_bool
div_may_involve_output(__isl_keep isl_basic_map
*bmap
, int div
)
10683 unsigned n_out
, o_out
;
10684 unsigned n_div
, o_div
;
10686 if (isl_int_is_zero(bmap
->div
[div
][0]))
10687 return isl_bool_true
;
10689 n_out
= isl_basic_map_dim(bmap
, isl_dim_out
);
10690 o_out
= isl_basic_map_offset(bmap
, isl_dim_out
);
10692 if (isl_seq_first_non_zero(bmap
->div
[div
] + 1 + o_out
, n_out
) != -1)
10693 return isl_bool_true
;
10695 n_div
= isl_basic_map_dim(bmap
, isl_dim_div
);
10696 o_div
= isl_basic_map_offset(bmap
, isl_dim_div
);
10698 for (i
= 0; i
< n_div
; ++i
) {
10699 isl_bool may_involve
;
10701 if (isl_int_is_zero(bmap
->div
[div
][1 + o_div
+ i
]))
10703 may_involve
= div_may_involve_output(bmap
, i
);
10704 if (may_involve
< 0 || may_involve
)
10705 return may_involve
;
10708 return isl_bool_false
;
10711 /* Return the first integer division of "bmap" in the range
10712 * [first, first + n[ that may depend on any output dimensions and
10713 * that has a non-zero coefficient in "c" (where the first coefficient
10714 * in "c" corresponds to integer division "first").
10716 static int first_div_may_involve_output(__isl_keep isl_basic_map
*bmap
,
10717 isl_int
*c
, int first
, int n
)
10724 for (k
= first
; k
< first
+ n
; ++k
) {
10725 isl_bool may_involve
;
10727 if (isl_int_is_zero(c
[k
]))
10729 may_involve
= div_may_involve_output(bmap
, k
);
10730 if (may_involve
< 0)
10739 /* Look for a pair of inequality constraints in "bmap" of the form
10741 * -l + i >= 0 or i >= l
10743 * n + l - i >= 0 or i <= l + n
10745 * with n < "m" and i the output dimension at position "pos".
10746 * (Note that n >= 0 as otherwise the two constraints would conflict.)
10747 * Furthermore, "l" is only allowed to involve parameters, input dimensions
10748 * and earlier output dimensions, as well as integer divisions that do
10749 * not involve any of the output dimensions.
10751 * Return the index of the first inequality constraint or bmap->n_ineq
10752 * if no such pair can be found.
10754 static int find_modulo_constraint_pair(__isl_keep isl_basic_map
*bmap
,
10755 int pos
, isl_int m
)
10760 unsigned n_div
, o_div
;
10761 unsigned n_out
, o_out
;
10767 ctx
= isl_basic_map_get_ctx(bmap
);
10768 total
= isl_basic_map_total_dim(bmap
);
10769 n_out
= isl_basic_map_dim(bmap
, isl_dim_out
);
10770 o_out
= isl_basic_map_offset(bmap
, isl_dim_out
);
10771 n_div
= isl_basic_map_dim(bmap
, isl_dim_div
);
10772 o_div
= isl_basic_map_offset(bmap
, isl_dim_div
);
10773 for (i
= 0; i
< bmap
->n_ineq
; ++i
) {
10774 if (!isl_int_abs_eq(bmap
->ineq
[i
][o_out
+ pos
], ctx
->one
))
10776 if (isl_seq_first_non_zero(bmap
->ineq
[i
] + o_out
+ pos
+ 1,
10777 n_out
- (pos
+ 1)) != -1)
10779 if (first_div_may_involve_output(bmap
, bmap
->ineq
[i
] + o_div
,
10782 for (j
= i
+ 1; j
< bmap
->n_ineq
; ++j
) {
10783 if (!isl_int_abs_eq(bmap
->ineq
[j
][o_out
+ pos
],
10786 if (!isl_seq_is_neg(bmap
->ineq
[i
] + 1,
10787 bmap
->ineq
[j
] + 1, total
))
10791 if (j
>= bmap
->n_ineq
)
10793 isl_int_add(bmap
->ineq
[i
][0],
10794 bmap
->ineq
[i
][0], bmap
->ineq
[j
][0]);
10795 less
= isl_int_abs_lt(bmap
->ineq
[i
][0], m
);
10796 isl_int_sub(bmap
->ineq
[i
][0],
10797 bmap
->ineq
[i
][0], bmap
->ineq
[j
][0]);
10800 if (isl_int_is_one(bmap
->ineq
[i
][o_out
+ pos
]))
10806 return bmap
->n_ineq
;
10809 /* Return the index of the equality of "bmap" that defines
10810 * the output dimension "pos" in terms of earlier dimensions.
10811 * The equality may also involve integer divisions, as long
10812 * as those integer divisions are defined in terms of
10813 * parameters or input dimensions.
10814 * In this case, *div is set to the number of integer divisions and
10815 * *ineq is set to the number of inequality constraints (provided
10816 * div and ineq are not NULL).
10818 * The equality may also involve a single integer division involving
10819 * the output dimensions (typically only output dimension "pos") as
10820 * long as the coefficient of output dimension "pos" is 1 or -1 and
10821 * there is a pair of constraints i >= l and i <= l + n, with i referring
10822 * to output dimension "pos", l an expression involving only earlier
10823 * dimensions and n smaller than the coefficient of the integer division
10824 * in the equality. In this case, the output dimension can be defined
10825 * in terms of a modulo expression that does not involve the integer division.
10826 * *div is then set to this single integer division and
10827 * *ineq is set to the index of constraint i >= l.
10829 * Return bmap->n_eq if there is no such equality.
10830 * Return -1 on error.
10832 int isl_basic_map_output_defining_equality(__isl_keep isl_basic_map
*bmap
,
10833 int pos
, int *div
, int *ineq
)
10836 unsigned n_out
, o_out
;
10837 unsigned n_div
, o_div
;
10842 n_out
= isl_basic_map_dim(bmap
, isl_dim_out
);
10843 o_out
= isl_basic_map_offset(bmap
, isl_dim_out
);
10844 n_div
= isl_basic_map_dim(bmap
, isl_dim_div
);
10845 o_div
= isl_basic_map_offset(bmap
, isl_dim_div
);
10848 *ineq
= bmap
->n_ineq
;
10851 for (j
= 0; j
< bmap
->n_eq
; ++j
) {
10852 if (isl_int_is_zero(bmap
->eq
[j
][o_out
+ pos
]))
10854 if (isl_seq_first_non_zero(bmap
->eq
[j
] + o_out
+ pos
+ 1,
10855 n_out
- (pos
+ 1)) != -1)
10857 k
= first_div_may_involve_output(bmap
, bmap
->eq
[j
] + o_div
,
10861 if (!isl_int_is_one(bmap
->eq
[j
][o_out
+ pos
]) &&
10862 !isl_int_is_negone(bmap
->eq
[j
][o_out
+ pos
]))
10864 if (first_div_may_involve_output(bmap
, bmap
->eq
[j
] + o_div
,
10865 k
+ 1, n_div
- (k
+1)) < n_div
)
10867 l
= find_modulo_constraint_pair(bmap
, pos
,
10868 bmap
->eq
[j
][o_div
+ k
]);
10871 if (l
>= bmap
->n_ineq
)
10883 /* Check if the given basic map is obviously single-valued.
10884 * In particular, for each output dimension, check that there is
10885 * an equality that defines the output dimension in terms of
10886 * earlier dimensions.
10888 isl_bool
isl_basic_map_plain_is_single_valued(__isl_keep isl_basic_map
*bmap
)
10894 return isl_bool_error
;
10896 n_out
= isl_basic_map_dim(bmap
, isl_dim_out
);
10898 for (i
= 0; i
< n_out
; ++i
) {
10901 eq
= isl_basic_map_output_defining_equality(bmap
, i
,
10904 return isl_bool_error
;
10905 if (eq
>= bmap
->n_eq
)
10906 return isl_bool_false
;
10909 return isl_bool_true
;
10912 /* Check if the given basic map is single-valued.
10913 * We simply compute
10917 * and check if the result is a subset of the identity mapping.
10919 isl_bool
isl_basic_map_is_single_valued(__isl_keep isl_basic_map
*bmap
)
10922 isl_basic_map
*test
;
10926 sv
= isl_basic_map_plain_is_single_valued(bmap
);
10930 test
= isl_basic_map_reverse(isl_basic_map_copy(bmap
));
10931 test
= isl_basic_map_apply_range(test
, isl_basic_map_copy(bmap
));
10933 space
= isl_basic_map_get_space(bmap
);
10934 space
= isl_space_map_from_set(isl_space_range(space
));
10935 id
= isl_basic_map_identity(space
);
10937 sv
= isl_basic_map_is_subset(test
, id
);
10939 isl_basic_map_free(test
);
10940 isl_basic_map_free(id
);
10945 /* Check if the given map is obviously single-valued.
10947 isl_bool
isl_map_plain_is_single_valued(__isl_keep isl_map
*map
)
10950 return isl_bool_error
;
10952 return isl_bool_true
;
10954 return isl_bool_false
;
10956 return isl_basic_map_plain_is_single_valued(map
->p
[0]);
10959 /* Check if the given map is single-valued.
10960 * We simply compute
10964 * and check if the result is a subset of the identity mapping.
10966 isl_bool
isl_map_is_single_valued(__isl_keep isl_map
*map
)
10973 sv
= isl_map_plain_is_single_valued(map
);
10977 test
= isl_map_reverse(isl_map_copy(map
));
10978 test
= isl_map_apply_range(test
, isl_map_copy(map
));
10980 dim
= isl_space_map_from_set(isl_space_range(isl_map_get_space(map
)));
10981 id
= isl_map_identity(dim
);
10983 sv
= isl_map_is_subset(test
, id
);
10985 isl_map_free(test
);
10991 isl_bool
isl_map_is_injective(__isl_keep isl_map
*map
)
10995 map
= isl_map_copy(map
);
10996 map
= isl_map_reverse(map
);
10997 in
= isl_map_is_single_valued(map
);
11003 /* Check if the given map is obviously injective.
11005 isl_bool
isl_map_plain_is_injective(__isl_keep isl_map
*map
)
11009 map
= isl_map_copy(map
);
11010 map
= isl_map_reverse(map
);
11011 in
= isl_map_plain_is_single_valued(map
);
11017 isl_bool
isl_map_is_bijective(__isl_keep isl_map
*map
)
11021 sv
= isl_map_is_single_valued(map
);
11025 return isl_map_is_injective(map
);
11028 isl_bool
isl_set_is_singleton(__isl_keep isl_set
*set
)
11030 return isl_map_is_single_valued(set_to_map(set
));
11033 /* Does "map" only map elements to themselves?
11035 * If the domain and range spaces are different, then "map"
11036 * is considered not to be an identity relation, even if it is empty.
11037 * Otherwise, construct the maximal identity relation and
11038 * check whether "map" is a subset of this relation.
11040 isl_bool
isl_map_is_identity(__isl_keep isl_map
*map
)
11044 isl_bool equal
, is_identity
;
11046 space
= isl_map_get_space(map
);
11047 equal
= isl_space_tuple_is_equal(space
, isl_dim_in
, space
, isl_dim_out
);
11048 isl_space_free(space
);
11049 if (equal
< 0 || !equal
)
11052 id
= isl_map_identity(isl_map_get_space(map
));
11053 is_identity
= isl_map_is_subset(map
, id
);
11056 return is_identity
;
11059 int isl_map_is_translation(__isl_keep isl_map
*map
)
11064 delta
= isl_map_deltas(isl_map_copy(map
));
11065 ok
= isl_set_is_singleton(delta
);
11066 isl_set_free(delta
);
11071 static int unique(isl_int
*p
, unsigned pos
, unsigned len
)
11073 if (isl_seq_first_non_zero(p
, pos
) != -1)
11075 if (isl_seq_first_non_zero(p
+ pos
+ 1, len
- pos
- 1) != -1)
11080 isl_bool
isl_basic_set_is_box(__isl_keep isl_basic_set
*bset
)
11087 return isl_bool_error
;
11089 if (isl_basic_set_dim(bset
, isl_dim_div
) != 0)
11090 return isl_bool_false
;
11092 nvar
= isl_basic_set_dim(bset
, isl_dim_set
);
11093 ovar
= isl_space_offset(bset
->dim
, isl_dim_set
);
11094 for (j
= 0; j
< nvar
; ++j
) {
11095 int lower
= 0, upper
= 0;
11096 for (i
= 0; i
< bset
->n_eq
; ++i
) {
11097 if (isl_int_is_zero(bset
->eq
[i
][1 + ovar
+ j
]))
11099 if (!unique(bset
->eq
[i
] + 1 + ovar
, j
, nvar
))
11100 return isl_bool_false
;
11103 if (i
< bset
->n_eq
)
11105 for (i
= 0; i
< bset
->n_ineq
; ++i
) {
11106 if (isl_int_is_zero(bset
->ineq
[i
][1 + ovar
+ j
]))
11108 if (!unique(bset
->ineq
[i
] + 1 + ovar
, j
, nvar
))
11109 return isl_bool_false
;
11110 if (isl_int_is_pos(bset
->ineq
[i
][1 + ovar
+ j
]))
11115 if (!lower
|| !upper
)
11116 return isl_bool_false
;
11119 return isl_bool_true
;
11122 isl_bool
isl_set_is_box(__isl_keep isl_set
*set
)
11125 return isl_bool_error
;
11127 return isl_bool_false
;
11129 return isl_basic_set_is_box(set
->p
[0]);
11132 isl_bool
isl_basic_set_is_wrapping(__isl_keep isl_basic_set
*bset
)
11135 return isl_bool_error
;
11137 return isl_space_is_wrapping(bset
->dim
);
11140 isl_bool
isl_set_is_wrapping(__isl_keep isl_set
*set
)
11143 return isl_bool_error
;
11145 return isl_space_is_wrapping(set
->dim
);
11148 /* Modify the space of "map" through a call to "change".
11149 * If "can_change" is set (not NULL), then first call it to check
11150 * if the modification is allowed, printing the error message "cannot_change"
11153 static __isl_give isl_map
*isl_map_change_space(__isl_take isl_map
*map
,
11154 isl_bool (*can_change
)(__isl_keep isl_map
*map
),
11155 const char *cannot_change
,
11156 __isl_give isl_space
*(*change
)(__isl_take isl_space
*space
))
11164 ok
= can_change
? can_change(map
) : isl_bool_true
;
11166 return isl_map_free(map
);
11168 isl_die(isl_map_get_ctx(map
), isl_error_invalid
, cannot_change
,
11169 return isl_map_free(map
));
11171 space
= change(isl_map_get_space(map
));
11172 map
= isl_map_reset_space(map
, space
);
11177 /* Is the domain of "map" a wrapped relation?
11179 isl_bool
isl_map_domain_is_wrapping(__isl_keep isl_map
*map
)
11182 return isl_bool_error
;
11184 return isl_space_domain_is_wrapping(map
->dim
);
11187 /* Is the range of "map" a wrapped relation?
11189 isl_bool
isl_map_range_is_wrapping(__isl_keep isl_map
*map
)
11192 return isl_bool_error
;
11194 return isl_space_range_is_wrapping(map
->dim
);
11197 __isl_give isl_basic_set
*isl_basic_map_wrap(__isl_take isl_basic_map
*bmap
)
11199 bmap
= isl_basic_map_cow(bmap
);
11203 bmap
->dim
= isl_space_wrap(bmap
->dim
);
11207 bmap
= isl_basic_map_finalize(bmap
);
11209 return bset_from_bmap(bmap
);
11211 isl_basic_map_free(bmap
);
11215 /* Given a map A -> B, return the set (A -> B).
11217 __isl_give isl_set
*isl_map_wrap(__isl_take isl_map
*map
)
11219 return isl_map_change_space(map
, NULL
, NULL
, &isl_space_wrap
);
11222 __isl_give isl_basic_map
*isl_basic_set_unwrap(__isl_take isl_basic_set
*bset
)
11224 bset
= isl_basic_set_cow(bset
);
11228 bset
->dim
= isl_space_unwrap(bset
->dim
);
11232 bset
= isl_basic_set_finalize(bset
);
11234 return bset_to_bmap(bset
);
11236 isl_basic_set_free(bset
);
11240 /* Given a set (A -> B), return the map A -> B.
11241 * Error out if "set" is not of the form (A -> B).
11243 __isl_give isl_map
*isl_set_unwrap(__isl_take isl_set
*set
)
11245 return isl_map_change_space(set
, &isl_set_is_wrapping
,
11246 "not a wrapping set", &isl_space_unwrap
);
11249 __isl_give isl_basic_map
*isl_basic_map_reset(__isl_take isl_basic_map
*bmap
,
11250 enum isl_dim_type type
)
11255 if (!isl_space_is_named_or_nested(bmap
->dim
, type
))
11258 bmap
= isl_basic_map_cow(bmap
);
11262 bmap
->dim
= isl_space_reset(bmap
->dim
, type
);
11266 bmap
= isl_basic_map_finalize(bmap
);
11270 isl_basic_map_free(bmap
);
11274 __isl_give isl_map
*isl_map_reset(__isl_take isl_map
*map
,
11275 enum isl_dim_type type
)
11282 if (!isl_space_is_named_or_nested(map
->dim
, type
))
11285 map
= isl_map_cow(map
);
11289 for (i
= 0; i
< map
->n
; ++i
) {
11290 map
->p
[i
] = isl_basic_map_reset(map
->p
[i
], type
);
11294 map
->dim
= isl_space_reset(map
->dim
, type
);
11304 __isl_give isl_basic_map
*isl_basic_map_flatten(__isl_take isl_basic_map
*bmap
)
11309 if (!bmap
->dim
->nested
[0] && !bmap
->dim
->nested
[1])
11312 bmap
= isl_basic_map_cow(bmap
);
11316 bmap
->dim
= isl_space_flatten(bmap
->dim
);
11320 bmap
= isl_basic_map_finalize(bmap
);
11324 isl_basic_map_free(bmap
);
11328 __isl_give isl_basic_set
*isl_basic_set_flatten(__isl_take isl_basic_set
*bset
)
11330 return bset_from_bmap(isl_basic_map_flatten(bset_to_bmap(bset
)));
11333 __isl_give isl_basic_map
*isl_basic_map_flatten_domain(
11334 __isl_take isl_basic_map
*bmap
)
11339 if (!bmap
->dim
->nested
[0])
11342 bmap
= isl_basic_map_cow(bmap
);
11346 bmap
->dim
= isl_space_flatten_domain(bmap
->dim
);
11350 bmap
= isl_basic_map_finalize(bmap
);
11354 isl_basic_map_free(bmap
);
11358 __isl_give isl_basic_map
*isl_basic_map_flatten_range(
11359 __isl_take isl_basic_map
*bmap
)
11364 if (!bmap
->dim
->nested
[1])
11367 bmap
= isl_basic_map_cow(bmap
);
11371 bmap
->dim
= isl_space_flatten_range(bmap
->dim
);
11375 bmap
= isl_basic_map_finalize(bmap
);
11379 isl_basic_map_free(bmap
);
11383 /* Remove any internal structure from the spaces of domain and range of "map".
11385 __isl_give isl_map
*isl_map_flatten(__isl_take isl_map
*map
)
11390 if (!map
->dim
->nested
[0] && !map
->dim
->nested
[1])
11393 return isl_map_change_space(map
, NULL
, NULL
, &isl_space_flatten
);
11396 __isl_give isl_set
*isl_set_flatten(__isl_take isl_set
*set
)
11398 return set_from_map(isl_map_flatten(set_to_map(set
)));
11401 __isl_give isl_map
*isl_set_flatten_map(__isl_take isl_set
*set
)
11403 isl_space
*dim
, *flat_dim
;
11406 dim
= isl_set_get_space(set
);
11407 flat_dim
= isl_space_flatten(isl_space_copy(dim
));
11408 map
= isl_map_identity(isl_space_join(isl_space_reverse(dim
), flat_dim
));
11409 map
= isl_map_intersect_domain(map
, set
);
11414 /* Remove any internal structure from the space of the domain of "map".
11416 __isl_give isl_map
*isl_map_flatten_domain(__isl_take isl_map
*map
)
11421 if (!map
->dim
->nested
[0])
11424 return isl_map_change_space(map
, NULL
, NULL
, &isl_space_flatten_domain
);
11427 /* Remove any internal structure from the space of the range of "map".
11429 __isl_give isl_map
*isl_map_flatten_range(__isl_take isl_map
*map
)
11434 if (!map
->dim
->nested
[1])
11437 return isl_map_change_space(map
, NULL
, NULL
, &isl_space_flatten_range
);
11440 /* Reorder the dimensions of "bmap" according to the given dim_map
11441 * and set the dimension specification to "dim" and
11442 * perform Gaussian elimination on the result.
11444 __isl_give isl_basic_map
*isl_basic_map_realign(__isl_take isl_basic_map
*bmap
,
11445 __isl_take isl_space
*dim
, __isl_take
struct isl_dim_map
*dim_map
)
11447 isl_basic_map
*res
;
11450 bmap
= isl_basic_map_cow(bmap
);
11451 if (!bmap
|| !dim
|| !dim_map
)
11454 flags
= bmap
->flags
;
11455 ISL_FL_CLR(flags
, ISL_BASIC_MAP_FINAL
);
11456 ISL_FL_CLR(flags
, ISL_BASIC_MAP_NORMALIZED
);
11457 ISL_FL_CLR(flags
, ISL_BASIC_MAP_NORMALIZED_DIVS
);
11458 res
= isl_basic_map_alloc_space(dim
,
11459 bmap
->n_div
, bmap
->n_eq
, bmap
->n_ineq
);
11460 res
= isl_basic_map_add_constraints_dim_map(res
, bmap
, dim_map
);
11462 res
->flags
= flags
;
11463 res
= isl_basic_map_gauss(res
, NULL
);
11464 res
= isl_basic_map_finalize(res
);
11468 isl_basic_map_free(bmap
);
11469 isl_space_free(dim
);
11473 /* Reorder the dimensions of "map" according to given reordering.
11475 __isl_give isl_map
*isl_map_realign(__isl_take isl_map
*map
,
11476 __isl_take isl_reordering
*r
)
11479 struct isl_dim_map
*dim_map
;
11481 map
= isl_map_cow(map
);
11482 dim_map
= isl_dim_map_from_reordering(r
);
11483 if (!map
|| !r
|| !dim_map
)
11486 for (i
= 0; i
< map
->n
; ++i
) {
11487 struct isl_dim_map
*dim_map_i
;
11489 dim_map_i
= isl_dim_map_extend(dim_map
, map
->p
[i
]);
11491 map
->p
[i
] = isl_basic_map_realign(map
->p
[i
],
11492 isl_space_copy(r
->dim
), dim_map_i
);
11498 map
= isl_map_reset_space(map
, isl_space_copy(r
->dim
));
11500 isl_reordering_free(r
);
11506 isl_reordering_free(r
);
11510 __isl_give isl_set
*isl_set_realign(__isl_take isl_set
*set
,
11511 __isl_take isl_reordering
*r
)
11513 return set_from_map(isl_map_realign(set_to_map(set
), r
));
11516 __isl_give isl_map
*isl_map_align_params(__isl_take isl_map
*map
,
11517 __isl_take isl_space
*model
)
11522 if (!map
|| !model
)
11525 ctx
= isl_space_get_ctx(model
);
11526 if (!isl_space_has_named_params(model
))
11527 isl_die(ctx
, isl_error_invalid
,
11528 "model has unnamed parameters", goto error
);
11529 if (isl_map_check_named_params(map
) < 0)
11531 aligned
= isl_map_space_has_equal_params(map
, model
);
11535 isl_reordering
*exp
;
11537 model
= isl_space_drop_dims(model
, isl_dim_in
,
11538 0, isl_space_dim(model
, isl_dim_in
));
11539 model
= isl_space_drop_dims(model
, isl_dim_out
,
11540 0, isl_space_dim(model
, isl_dim_out
));
11541 exp
= isl_parameter_alignment_reordering(map
->dim
, model
);
11542 exp
= isl_reordering_extend_space(exp
, isl_map_get_space(map
));
11543 map
= isl_map_realign(map
, exp
);
11546 isl_space_free(model
);
11549 isl_space_free(model
);
11554 __isl_give isl_set
*isl_set_align_params(__isl_take isl_set
*set
,
11555 __isl_take isl_space
*model
)
11557 return isl_map_align_params(set
, model
);
11560 /* Align the parameters of "bmap" to those of "model", introducing
11561 * additional parameters if needed.
11563 __isl_give isl_basic_map
*isl_basic_map_align_params(
11564 __isl_take isl_basic_map
*bmap
, __isl_take isl_space
*model
)
11567 isl_bool equal_params
;
11569 if (!bmap
|| !model
)
11572 ctx
= isl_space_get_ctx(model
);
11573 if (!isl_space_has_named_params(model
))
11574 isl_die(ctx
, isl_error_invalid
,
11575 "model has unnamed parameters", goto error
);
11576 if (!isl_space_has_named_params(bmap
->dim
))
11577 isl_die(ctx
, isl_error_invalid
,
11578 "relation has unnamed parameters", goto error
);
11579 equal_params
= isl_space_has_equal_params(bmap
->dim
, model
);
11580 if (equal_params
< 0)
11582 if (!equal_params
) {
11583 isl_reordering
*exp
;
11584 struct isl_dim_map
*dim_map
;
11586 model
= isl_space_drop_dims(model
, isl_dim_in
,
11587 0, isl_space_dim(model
, isl_dim_in
));
11588 model
= isl_space_drop_dims(model
, isl_dim_out
,
11589 0, isl_space_dim(model
, isl_dim_out
));
11590 exp
= isl_parameter_alignment_reordering(bmap
->dim
, model
);
11591 exp
= isl_reordering_extend_space(exp
,
11592 isl_basic_map_get_space(bmap
));
11593 dim_map
= isl_dim_map_from_reordering(exp
);
11594 bmap
= isl_basic_map_realign(bmap
,
11595 exp
? isl_space_copy(exp
->dim
) : NULL
,
11596 isl_dim_map_extend(dim_map
, bmap
));
11597 isl_reordering_free(exp
);
11601 isl_space_free(model
);
11604 isl_space_free(model
);
11605 isl_basic_map_free(bmap
);
11609 /* Do "bset" and "space" have the same parameters?
11611 isl_bool
isl_basic_set_space_has_equal_params(__isl_keep isl_basic_set
*bset
,
11612 __isl_keep isl_space
*space
)
11614 isl_space
*bset_space
;
11616 bset_space
= isl_basic_set_peek_space(bset
);
11617 return isl_space_has_equal_params(bset_space
, space
);
11620 /* Do "map" and "space" have the same parameters?
11622 isl_bool
isl_map_space_has_equal_params(__isl_keep isl_map
*map
,
11623 __isl_keep isl_space
*space
)
11625 isl_space
*map_space
;
11627 map_space
= isl_map_peek_space(map
);
11628 return isl_space_has_equal_params(map_space
, space
);
11631 /* Do "set" and "space" have the same parameters?
11633 isl_bool
isl_set_space_has_equal_params(__isl_keep isl_set
*set
,
11634 __isl_keep isl_space
*space
)
11636 return isl_map_space_has_equal_params(set_to_map(set
), space
);
11639 /* Align the parameters of "bset" to those of "model", introducing
11640 * additional parameters if needed.
11642 __isl_give isl_basic_set
*isl_basic_set_align_params(
11643 __isl_take isl_basic_set
*bset
, __isl_take isl_space
*model
)
11645 return isl_basic_map_align_params(bset
, model
);
11648 __isl_give isl_mat
*isl_basic_map_equalities_matrix(
11649 __isl_keep isl_basic_map
*bmap
, enum isl_dim_type c1
,
11650 enum isl_dim_type c2
, enum isl_dim_type c3
,
11651 enum isl_dim_type c4
, enum isl_dim_type c5
)
11653 enum isl_dim_type c
[5] = { c1
, c2
, c3
, c4
, c5
};
11654 struct isl_mat
*mat
;
11660 mat
= isl_mat_alloc(bmap
->ctx
, bmap
->n_eq
,
11661 isl_basic_map_total_dim(bmap
) + 1);
11664 for (i
= 0; i
< bmap
->n_eq
; ++i
)
11665 for (j
= 0, pos
= 0; j
< 5; ++j
) {
11666 int off
= isl_basic_map_offset(bmap
, c
[j
]);
11667 for (k
= 0; k
< isl_basic_map_dim(bmap
, c
[j
]); ++k
) {
11668 isl_int_set(mat
->row
[i
][pos
],
11669 bmap
->eq
[i
][off
+ k
]);
11677 __isl_give isl_mat
*isl_basic_map_inequalities_matrix(
11678 __isl_keep isl_basic_map
*bmap
, enum isl_dim_type c1
,
11679 enum isl_dim_type c2
, enum isl_dim_type c3
,
11680 enum isl_dim_type c4
, enum isl_dim_type c5
)
11682 enum isl_dim_type c
[5] = { c1
, c2
, c3
, c4
, c5
};
11683 struct isl_mat
*mat
;
11689 mat
= isl_mat_alloc(bmap
->ctx
, bmap
->n_ineq
,
11690 isl_basic_map_total_dim(bmap
) + 1);
11693 for (i
= 0; i
< bmap
->n_ineq
; ++i
)
11694 for (j
= 0, pos
= 0; j
< 5; ++j
) {
11695 int off
= isl_basic_map_offset(bmap
, c
[j
]);
11696 for (k
= 0; k
< isl_basic_map_dim(bmap
, c
[j
]); ++k
) {
11697 isl_int_set(mat
->row
[i
][pos
],
11698 bmap
->ineq
[i
][off
+ k
]);
11706 __isl_give isl_basic_map
*isl_basic_map_from_constraint_matrices(
11707 __isl_take isl_space
*dim
,
11708 __isl_take isl_mat
*eq
, __isl_take isl_mat
*ineq
, enum isl_dim_type c1
,
11709 enum isl_dim_type c2
, enum isl_dim_type c3
,
11710 enum isl_dim_type c4
, enum isl_dim_type c5
)
11712 enum isl_dim_type c
[5] = { c1
, c2
, c3
, c4
, c5
};
11713 isl_basic_map
*bmap
;
11719 if (!dim
|| !eq
|| !ineq
)
11722 if (eq
->n_col
!= ineq
->n_col
)
11723 isl_die(dim
->ctx
, isl_error_invalid
,
11724 "equalities and inequalities matrices should have "
11725 "same number of columns", goto error
);
11727 total
= 1 + isl_space_dim(dim
, isl_dim_all
);
11729 if (eq
->n_col
< total
)
11730 isl_die(dim
->ctx
, isl_error_invalid
,
11731 "number of columns too small", goto error
);
11733 extra
= eq
->n_col
- total
;
11735 bmap
= isl_basic_map_alloc_space(isl_space_copy(dim
), extra
,
11736 eq
->n_row
, ineq
->n_row
);
11739 for (i
= 0; i
< extra
; ++i
) {
11740 k
= isl_basic_map_alloc_div(bmap
);
11743 isl_int_set_si(bmap
->div
[k
][0], 0);
11745 for (i
= 0; i
< eq
->n_row
; ++i
) {
11746 l
= isl_basic_map_alloc_equality(bmap
);
11749 for (j
= 0, pos
= 0; j
< 5; ++j
) {
11750 int off
= isl_basic_map_offset(bmap
, c
[j
]);
11751 for (k
= 0; k
< isl_basic_map_dim(bmap
, c
[j
]); ++k
) {
11752 isl_int_set(bmap
->eq
[l
][off
+ k
],
11758 for (i
= 0; i
< ineq
->n_row
; ++i
) {
11759 l
= isl_basic_map_alloc_inequality(bmap
);
11762 for (j
= 0, pos
= 0; j
< 5; ++j
) {
11763 int off
= isl_basic_map_offset(bmap
, c
[j
]);
11764 for (k
= 0; k
< isl_basic_map_dim(bmap
, c
[j
]); ++k
) {
11765 isl_int_set(bmap
->ineq
[l
][off
+ k
],
11766 ineq
->row
[i
][pos
]);
11772 isl_space_free(dim
);
11774 isl_mat_free(ineq
);
11776 bmap
= isl_basic_map_simplify(bmap
);
11777 return isl_basic_map_finalize(bmap
);
11779 isl_space_free(dim
);
11781 isl_mat_free(ineq
);
11785 __isl_give isl_mat
*isl_basic_set_equalities_matrix(
11786 __isl_keep isl_basic_set
*bset
, enum isl_dim_type c1
,
11787 enum isl_dim_type c2
, enum isl_dim_type c3
, enum isl_dim_type c4
)
11789 return isl_basic_map_equalities_matrix(bset_to_bmap(bset
),
11790 c1
, c2
, c3
, c4
, isl_dim_in
);
11793 __isl_give isl_mat
*isl_basic_set_inequalities_matrix(
11794 __isl_keep isl_basic_set
*bset
, enum isl_dim_type c1
,
11795 enum isl_dim_type c2
, enum isl_dim_type c3
, enum isl_dim_type c4
)
11797 return isl_basic_map_inequalities_matrix(bset_to_bmap(bset
),
11798 c1
, c2
, c3
, c4
, isl_dim_in
);
11801 __isl_give isl_basic_set
*isl_basic_set_from_constraint_matrices(
11802 __isl_take isl_space
*dim
,
11803 __isl_take isl_mat
*eq
, __isl_take isl_mat
*ineq
, enum isl_dim_type c1
,
11804 enum isl_dim_type c2
, enum isl_dim_type c3
, enum isl_dim_type c4
)
11806 isl_basic_map
*bmap
;
11807 bmap
= isl_basic_map_from_constraint_matrices(dim
, eq
, ineq
,
11808 c1
, c2
, c3
, c4
, isl_dim_in
);
11809 return bset_from_bmap(bmap
);
11812 isl_bool
isl_basic_map_can_zip(__isl_keep isl_basic_map
*bmap
)
11815 return isl_bool_error
;
11817 return isl_space_can_zip(bmap
->dim
);
11820 isl_bool
isl_map_can_zip(__isl_keep isl_map
*map
)
11823 return isl_bool_error
;
11825 return isl_space_can_zip(map
->dim
);
11828 /* Given a basic map (A -> B) -> (C -> D), return the corresponding basic map
11829 * (A -> C) -> (B -> D).
11831 __isl_give isl_basic_map
*isl_basic_map_zip(__isl_take isl_basic_map
*bmap
)
11840 if (!isl_basic_map_can_zip(bmap
))
11841 isl_die(bmap
->ctx
, isl_error_invalid
,
11842 "basic map cannot be zipped", goto error
);
11843 pos
= isl_basic_map_offset(bmap
, isl_dim_in
) +
11844 isl_space_dim(bmap
->dim
->nested
[0], isl_dim_in
);
11845 n1
= isl_space_dim(bmap
->dim
->nested
[0], isl_dim_out
);
11846 n2
= isl_space_dim(bmap
->dim
->nested
[1], isl_dim_in
);
11847 bmap
= isl_basic_map_cow(bmap
);
11848 bmap
= isl_basic_map_swap_vars(bmap
, pos
, n1
, n2
);
11851 bmap
->dim
= isl_space_zip(bmap
->dim
);
11854 bmap
= isl_basic_map_mark_final(bmap
);
11857 isl_basic_map_free(bmap
);
11861 /* Given a map (A -> B) -> (C -> D), return the corresponding map
11862 * (A -> C) -> (B -> D).
11864 __isl_give isl_map
*isl_map_zip(__isl_take isl_map
*map
)
11871 if (!isl_map_can_zip(map
))
11872 isl_die(map
->ctx
, isl_error_invalid
, "map cannot be zipped",
11875 map
= isl_map_cow(map
);
11879 for (i
= 0; i
< map
->n
; ++i
) {
11880 map
->p
[i
] = isl_basic_map_zip(map
->p
[i
]);
11885 map
->dim
= isl_space_zip(map
->dim
);
11895 /* Can we apply isl_basic_map_curry to "bmap"?
11896 * That is, does it have a nested relation in its domain?
11898 isl_bool
isl_basic_map_can_curry(__isl_keep isl_basic_map
*bmap
)
11901 return isl_bool_error
;
11903 return isl_space_can_curry(bmap
->dim
);
11906 /* Can we apply isl_map_curry to "map"?
11907 * That is, does it have a nested relation in its domain?
11909 isl_bool
isl_map_can_curry(__isl_keep isl_map
*map
)
11912 return isl_bool_error
;
11914 return isl_space_can_curry(map
->dim
);
11917 /* Given a basic map (A -> B) -> C, return the corresponding basic map
11920 __isl_give isl_basic_map
*isl_basic_map_curry(__isl_take isl_basic_map
*bmap
)
11926 if (!isl_basic_map_can_curry(bmap
))
11927 isl_die(bmap
->ctx
, isl_error_invalid
,
11928 "basic map cannot be curried", goto error
);
11929 bmap
= isl_basic_map_cow(bmap
);
11932 bmap
->dim
= isl_space_curry(bmap
->dim
);
11935 bmap
= isl_basic_map_mark_final(bmap
);
11938 isl_basic_map_free(bmap
);
11942 /* Given a map (A -> B) -> C, return the corresponding map
11945 __isl_give isl_map
*isl_map_curry(__isl_take isl_map
*map
)
11947 return isl_map_change_space(map
, &isl_map_can_curry
,
11948 "map cannot be curried", &isl_space_curry
);
11951 /* Can isl_map_range_curry be applied to "map"?
11952 * That is, does it have a nested relation in its range,
11953 * the domain of which is itself a nested relation?
11955 isl_bool
isl_map_can_range_curry(__isl_keep isl_map
*map
)
11958 return isl_bool_error
;
11960 return isl_space_can_range_curry(map
->dim
);
11963 /* Given a map A -> ((B -> C) -> D), return the corresponding map
11964 * A -> (B -> (C -> D)).
11966 __isl_give isl_map
*isl_map_range_curry(__isl_take isl_map
*map
)
11968 return isl_map_change_space(map
, &isl_map_can_range_curry
,
11969 "map range cannot be curried",
11970 &isl_space_range_curry
);
11973 /* Can we apply isl_basic_map_uncurry to "bmap"?
11974 * That is, does it have a nested relation in its domain?
11976 isl_bool
isl_basic_map_can_uncurry(__isl_keep isl_basic_map
*bmap
)
11979 return isl_bool_error
;
11981 return isl_space_can_uncurry(bmap
->dim
);
11984 /* Can we apply isl_map_uncurry to "map"?
11985 * That is, does it have a nested relation in its domain?
11987 isl_bool
isl_map_can_uncurry(__isl_keep isl_map
*map
)
11990 return isl_bool_error
;
11992 return isl_space_can_uncurry(map
->dim
);
11995 /* Given a basic map A -> (B -> C), return the corresponding basic map
11998 __isl_give isl_basic_map
*isl_basic_map_uncurry(__isl_take isl_basic_map
*bmap
)
12004 if (!isl_basic_map_can_uncurry(bmap
))
12005 isl_die(bmap
->ctx
, isl_error_invalid
,
12006 "basic map cannot be uncurried",
12007 return isl_basic_map_free(bmap
));
12008 bmap
= isl_basic_map_cow(bmap
);
12011 bmap
->dim
= isl_space_uncurry(bmap
->dim
);
12013 return isl_basic_map_free(bmap
);
12014 bmap
= isl_basic_map_mark_final(bmap
);
12018 /* Given a map A -> (B -> C), return the corresponding map
12021 __isl_give isl_map
*isl_map_uncurry(__isl_take isl_map
*map
)
12023 return isl_map_change_space(map
, &isl_map_can_uncurry
,
12024 "map cannot be uncurried", &isl_space_uncurry
);
12027 /* Construct a basic map mapping the domain of the affine expression
12028 * to a one-dimensional range prescribed by the affine expression.
12029 * If "rational" is set, then construct a rational basic map.
12031 * A NaN affine expression cannot be converted to a basic map.
12033 static __isl_give isl_basic_map
*isl_basic_map_from_aff2(
12034 __isl_take isl_aff
*aff
, int rational
)
12039 isl_local_space
*ls
;
12040 isl_basic_map
*bmap
= NULL
;
12044 is_nan
= isl_aff_is_nan(aff
);
12048 isl_die(isl_aff_get_ctx(aff
), isl_error_invalid
,
12049 "cannot convert NaN", goto error
);
12051 ls
= isl_aff_get_local_space(aff
);
12052 bmap
= isl_basic_map_from_local_space(ls
);
12053 bmap
= isl_basic_map_extend_constraints(bmap
, 1, 0);
12054 k
= isl_basic_map_alloc_equality(bmap
);
12058 pos
= isl_basic_map_offset(bmap
, isl_dim_out
);
12059 isl_seq_cpy(bmap
->eq
[k
], aff
->v
->el
+ 1, pos
);
12060 isl_int_neg(bmap
->eq
[k
][pos
], aff
->v
->el
[0]);
12061 isl_seq_cpy(bmap
->eq
[k
] + pos
+ 1, aff
->v
->el
+ 1 + pos
,
12062 aff
->v
->size
- (pos
+ 1));
12066 bmap
= isl_basic_map_set_rational(bmap
);
12067 bmap
= isl_basic_map_gauss(bmap
, NULL
);
12068 bmap
= isl_basic_map_finalize(bmap
);
12072 isl_basic_map_free(bmap
);
12076 /* Construct a basic map mapping the domain of the affine expression
12077 * to a one-dimensional range prescribed by the affine expression.
12079 __isl_give isl_basic_map
*isl_basic_map_from_aff(__isl_take isl_aff
*aff
)
12081 return isl_basic_map_from_aff2(aff
, 0);
12084 /* Construct a map mapping the domain of the affine expression
12085 * to a one-dimensional range prescribed by the affine expression.
12087 __isl_give isl_map
*isl_map_from_aff(__isl_take isl_aff
*aff
)
12089 isl_basic_map
*bmap
;
12091 bmap
= isl_basic_map_from_aff(aff
);
12092 return isl_map_from_basic_map(bmap
);
12095 /* Construct a basic map mapping the domain the multi-affine expression
12096 * to its range, with each dimension in the range equated to the
12097 * corresponding affine expression.
12098 * If "rational" is set, then construct a rational basic map.
12100 __isl_give isl_basic_map
*isl_basic_map_from_multi_aff2(
12101 __isl_take isl_multi_aff
*maff
, int rational
)
12105 isl_basic_map
*bmap
;
12110 if (isl_space_dim(maff
->space
, isl_dim_out
) != maff
->n
)
12111 isl_die(isl_multi_aff_get_ctx(maff
), isl_error_internal
,
12112 "invalid space", goto error
);
12114 space
= isl_space_domain(isl_multi_aff_get_space(maff
));
12115 bmap
= isl_basic_map_universe(isl_space_from_domain(space
));
12117 bmap
= isl_basic_map_set_rational(bmap
);
12119 for (i
= 0; i
< maff
->n
; ++i
) {
12121 isl_basic_map
*bmap_i
;
12123 aff
= isl_aff_copy(maff
->p
[i
]);
12124 bmap_i
= isl_basic_map_from_aff2(aff
, rational
);
12126 bmap
= isl_basic_map_flat_range_product(bmap
, bmap_i
);
12129 bmap
= isl_basic_map_reset_space(bmap
, isl_multi_aff_get_space(maff
));
12131 isl_multi_aff_free(maff
);
12134 isl_multi_aff_free(maff
);
12138 /* Construct a basic map mapping the domain the multi-affine expression
12139 * to its range, with each dimension in the range equated to the
12140 * corresponding affine expression.
12142 __isl_give isl_basic_map
*isl_basic_map_from_multi_aff(
12143 __isl_take isl_multi_aff
*ma
)
12145 return isl_basic_map_from_multi_aff2(ma
, 0);
12148 /* Construct a map mapping the domain the multi-affine expression
12149 * to its range, with each dimension in the range equated to the
12150 * corresponding affine expression.
12152 __isl_give isl_map
*isl_map_from_multi_aff(__isl_take isl_multi_aff
*maff
)
12154 isl_basic_map
*bmap
;
12156 bmap
= isl_basic_map_from_multi_aff(maff
);
12157 return isl_map_from_basic_map(bmap
);
12160 /* Construct a basic map mapping a domain in the given space to
12161 * to an n-dimensional range, with n the number of elements in the list,
12162 * where each coordinate in the range is prescribed by the
12163 * corresponding affine expression.
12164 * The domains of all affine expressions in the list are assumed to match
12167 __isl_give isl_basic_map
*isl_basic_map_from_aff_list(
12168 __isl_take isl_space
*domain_dim
, __isl_take isl_aff_list
*list
)
12172 isl_basic_map
*bmap
;
12177 dim
= isl_space_from_domain(domain_dim
);
12178 bmap
= isl_basic_map_universe(dim
);
12180 for (i
= 0; i
< list
->n
; ++i
) {
12182 isl_basic_map
*bmap_i
;
12184 aff
= isl_aff_copy(list
->p
[i
]);
12185 bmap_i
= isl_basic_map_from_aff(aff
);
12187 bmap
= isl_basic_map_flat_range_product(bmap
, bmap_i
);
12190 isl_aff_list_free(list
);
12194 __isl_give isl_set
*isl_set_equate(__isl_take isl_set
*set
,
12195 enum isl_dim_type type1
, int pos1
, enum isl_dim_type type2
, int pos2
)
12197 return isl_map_equate(set
, type1
, pos1
, type2
, pos2
);
12200 /* Construct a basic map where the given dimensions are equal to each other.
12202 static __isl_give isl_basic_map
*equator(__isl_take isl_space
*space
,
12203 enum isl_dim_type type1
, int pos1
, enum isl_dim_type type2
, int pos2
)
12205 isl_basic_map
*bmap
= NULL
;
12211 if (pos1
>= isl_space_dim(space
, type1
))
12212 isl_die(isl_space_get_ctx(space
), isl_error_invalid
,
12213 "index out of bounds", goto error
);
12214 if (pos2
>= isl_space_dim(space
, type2
))
12215 isl_die(isl_space_get_ctx(space
), isl_error_invalid
,
12216 "index out of bounds", goto error
);
12218 if (type1
== type2
&& pos1
== pos2
)
12219 return isl_basic_map_universe(space
);
12221 bmap
= isl_basic_map_alloc_space(isl_space_copy(space
), 0, 1, 0);
12222 i
= isl_basic_map_alloc_equality(bmap
);
12225 isl_seq_clr(bmap
->eq
[i
], 1 + isl_basic_map_total_dim(bmap
));
12226 pos1
+= isl_basic_map_offset(bmap
, type1
);
12227 pos2
+= isl_basic_map_offset(bmap
, type2
);
12228 isl_int_set_si(bmap
->eq
[i
][pos1
], -1);
12229 isl_int_set_si(bmap
->eq
[i
][pos2
], 1);
12230 bmap
= isl_basic_map_finalize(bmap
);
12231 isl_space_free(space
);
12234 isl_space_free(space
);
12235 isl_basic_map_free(bmap
);
12239 /* Add a constraint imposing that the given two dimensions are equal.
12241 __isl_give isl_basic_map
*isl_basic_map_equate(__isl_take isl_basic_map
*bmap
,
12242 enum isl_dim_type type1
, int pos1
, enum isl_dim_type type2
, int pos2
)
12246 eq
= equator(isl_basic_map_get_space(bmap
), type1
, pos1
, type2
, pos2
);
12248 bmap
= isl_basic_map_intersect(bmap
, eq
);
12253 /* Add a constraint imposing that the given two dimensions are equal.
12255 __isl_give isl_map
*isl_map_equate(__isl_take isl_map
*map
,
12256 enum isl_dim_type type1
, int pos1
, enum isl_dim_type type2
, int pos2
)
12258 isl_basic_map
*bmap
;
12260 bmap
= equator(isl_map_get_space(map
), type1
, pos1
, type2
, pos2
);
12262 map
= isl_map_intersect(map
, isl_map_from_basic_map(bmap
));
12267 /* Add a constraint imposing that the given two dimensions have opposite values.
12269 __isl_give isl_map
*isl_map_oppose(__isl_take isl_map
*map
,
12270 enum isl_dim_type type1
, int pos1
, enum isl_dim_type type2
, int pos2
)
12272 isl_basic_map
*bmap
= NULL
;
12278 if (pos1
>= isl_map_dim(map
, type1
))
12279 isl_die(map
->ctx
, isl_error_invalid
,
12280 "index out of bounds", goto error
);
12281 if (pos2
>= isl_map_dim(map
, type2
))
12282 isl_die(map
->ctx
, isl_error_invalid
,
12283 "index out of bounds", goto error
);
12285 bmap
= isl_basic_map_alloc_space(isl_map_get_space(map
), 0, 1, 0);
12286 i
= isl_basic_map_alloc_equality(bmap
);
12289 isl_seq_clr(bmap
->eq
[i
], 1 + isl_basic_map_total_dim(bmap
));
12290 pos1
+= isl_basic_map_offset(bmap
, type1
);
12291 pos2
+= isl_basic_map_offset(bmap
, type2
);
12292 isl_int_set_si(bmap
->eq
[i
][pos1
], 1);
12293 isl_int_set_si(bmap
->eq
[i
][pos2
], 1);
12294 bmap
= isl_basic_map_finalize(bmap
);
12296 map
= isl_map_intersect(map
, isl_map_from_basic_map(bmap
));
12300 isl_basic_map_free(bmap
);
12305 /* Construct a constraint imposing that the value of the first dimension is
12306 * greater than or equal to that of the second.
12308 static __isl_give isl_constraint
*constraint_order_ge(
12309 __isl_take isl_space
*space
, enum isl_dim_type type1
, int pos1
,
12310 enum isl_dim_type type2
, int pos2
)
12317 c
= isl_constraint_alloc_inequality(isl_local_space_from_space(space
));
12319 if (pos1
>= isl_constraint_dim(c
, type1
))
12320 isl_die(isl_constraint_get_ctx(c
), isl_error_invalid
,
12321 "index out of bounds", return isl_constraint_free(c
));
12322 if (pos2
>= isl_constraint_dim(c
, type2
))
12323 isl_die(isl_constraint_get_ctx(c
), isl_error_invalid
,
12324 "index out of bounds", return isl_constraint_free(c
));
12326 if (type1
== type2
&& pos1
== pos2
)
12329 c
= isl_constraint_set_coefficient_si(c
, type1
, pos1
, 1);
12330 c
= isl_constraint_set_coefficient_si(c
, type2
, pos2
, -1);
12335 /* Add a constraint imposing that the value of the first dimension is
12336 * greater than or equal to that of the second.
12338 __isl_give isl_basic_map
*isl_basic_map_order_ge(__isl_take isl_basic_map
*bmap
,
12339 enum isl_dim_type type1
, int pos1
, enum isl_dim_type type2
, int pos2
)
12344 if (type1
== type2
&& pos1
== pos2
)
12346 space
= isl_basic_map_get_space(bmap
);
12347 c
= constraint_order_ge(space
, type1
, pos1
, type2
, pos2
);
12348 bmap
= isl_basic_map_add_constraint(bmap
, c
);
12353 /* Add a constraint imposing that the value of the first dimension is
12354 * greater than or equal to that of the second.
12356 __isl_give isl_map
*isl_map_order_ge(__isl_take isl_map
*map
,
12357 enum isl_dim_type type1
, int pos1
, enum isl_dim_type type2
, int pos2
)
12362 if (type1
== type2
&& pos1
== pos2
)
12364 space
= isl_map_get_space(map
);
12365 c
= constraint_order_ge(space
, type1
, pos1
, type2
, pos2
);
12366 map
= isl_map_add_constraint(map
, c
);
12371 /* Add a constraint imposing that the value of the first dimension is
12372 * less than or equal to that of the second.
12374 __isl_give isl_map
*isl_map_order_le(__isl_take isl_map
*map
,
12375 enum isl_dim_type type1
, int pos1
, enum isl_dim_type type2
, int pos2
)
12377 return isl_map_order_ge(map
, type2
, pos2
, type1
, pos1
);
12380 /* Construct a basic map where the value of the first dimension is
12381 * greater than that of the second.
12383 static __isl_give isl_basic_map
*greator(__isl_take isl_space
*space
,
12384 enum isl_dim_type type1
, int pos1
, enum isl_dim_type type2
, int pos2
)
12386 isl_basic_map
*bmap
= NULL
;
12392 if (pos1
>= isl_space_dim(space
, type1
))
12393 isl_die(isl_space_get_ctx(space
), isl_error_invalid
,
12394 "index out of bounds", goto error
);
12395 if (pos2
>= isl_space_dim(space
, type2
))
12396 isl_die(isl_space_get_ctx(space
), isl_error_invalid
,
12397 "index out of bounds", goto error
);
12399 if (type1
== type2
&& pos1
== pos2
)
12400 return isl_basic_map_empty(space
);
12402 bmap
= isl_basic_map_alloc_space(space
, 0, 0, 1);
12403 i
= isl_basic_map_alloc_inequality(bmap
);
12405 return isl_basic_map_free(bmap
);
12406 isl_seq_clr(bmap
->ineq
[i
], 1 + isl_basic_map_total_dim(bmap
));
12407 pos1
+= isl_basic_map_offset(bmap
, type1
);
12408 pos2
+= isl_basic_map_offset(bmap
, type2
);
12409 isl_int_set_si(bmap
->ineq
[i
][pos1
], 1);
12410 isl_int_set_si(bmap
->ineq
[i
][pos2
], -1);
12411 isl_int_set_si(bmap
->ineq
[i
][0], -1);
12412 bmap
= isl_basic_map_finalize(bmap
);
12416 isl_space_free(space
);
12417 isl_basic_map_free(bmap
);
12421 /* Add a constraint imposing that the value of the first dimension is
12422 * greater than that of the second.
12424 __isl_give isl_basic_map
*isl_basic_map_order_gt(__isl_take isl_basic_map
*bmap
,
12425 enum isl_dim_type type1
, int pos1
, enum isl_dim_type type2
, int pos2
)
12429 gt
= greator(isl_basic_map_get_space(bmap
), type1
, pos1
, type2
, pos2
);
12431 bmap
= isl_basic_map_intersect(bmap
, gt
);
12436 /* Add a constraint imposing that the value of the first dimension is
12437 * greater than that of the second.
12439 __isl_give isl_map
*isl_map_order_gt(__isl_take isl_map
*map
,
12440 enum isl_dim_type type1
, int pos1
, enum isl_dim_type type2
, int pos2
)
12442 isl_basic_map
*bmap
;
12444 bmap
= greator(isl_map_get_space(map
), type1
, pos1
, type2
, pos2
);
12446 map
= isl_map_intersect(map
, isl_map_from_basic_map(bmap
));
12451 /* Add a constraint imposing that the value of the first dimension is
12452 * smaller than that of the second.
12454 __isl_give isl_map
*isl_map_order_lt(__isl_take isl_map
*map
,
12455 enum isl_dim_type type1
, int pos1
, enum isl_dim_type type2
, int pos2
)
12457 return isl_map_order_gt(map
, type2
, pos2
, type1
, pos1
);
12460 __isl_give isl_aff
*isl_basic_map_get_div(__isl_keep isl_basic_map
*bmap
,
12464 isl_local_space
*ls
;
12469 if (!isl_basic_map_divs_known(bmap
))
12470 isl_die(isl_basic_map_get_ctx(bmap
), isl_error_invalid
,
12471 "some divs are unknown", return NULL
);
12473 ls
= isl_basic_map_get_local_space(bmap
);
12474 div
= isl_local_space_get_div(ls
, pos
);
12475 isl_local_space_free(ls
);
12480 __isl_give isl_aff
*isl_basic_set_get_div(__isl_keep isl_basic_set
*bset
,
12483 return isl_basic_map_get_div(bset
, pos
);
12486 /* Plug in "subs" for dimension "type", "pos" of "bset".
12488 * Let i be the dimension to replace and let "subs" be of the form
12492 * Any integer division with a non-zero coefficient for i,
12494 * floor((a i + g)/m)
12498 * floor((a f + d g)/(m d))
12500 * Constraints of the form
12508 * We currently require that "subs" is an integral expression.
12509 * Handling rational expressions may require us to add stride constraints
12510 * as we do in isl_basic_set_preimage_multi_aff.
12512 __isl_give isl_basic_set
*isl_basic_set_substitute(
12513 __isl_take isl_basic_set
*bset
,
12514 enum isl_dim_type type
, unsigned pos
, __isl_keep isl_aff
*subs
)
12520 if (bset
&& isl_basic_set_plain_is_empty(bset
))
12523 bset
= isl_basic_set_cow(bset
);
12524 if (!bset
|| !subs
)
12527 ctx
= isl_basic_set_get_ctx(bset
);
12528 if (!isl_space_is_equal(bset
->dim
, subs
->ls
->dim
))
12529 isl_die(ctx
, isl_error_invalid
,
12530 "spaces don't match", goto error
);
12531 if (isl_local_space_dim(subs
->ls
, isl_dim_div
) != 0)
12532 isl_die(ctx
, isl_error_unsupported
,
12533 "cannot handle divs yet", goto error
);
12534 if (!isl_int_is_one(subs
->v
->el
[0]))
12535 isl_die(ctx
, isl_error_invalid
,
12536 "can only substitute integer expressions", goto error
);
12538 pos
+= isl_basic_set_offset(bset
, type
);
12542 for (i
= 0; i
< bset
->n_eq
; ++i
) {
12543 if (isl_int_is_zero(bset
->eq
[i
][pos
]))
12545 isl_int_set(v
, bset
->eq
[i
][pos
]);
12546 isl_int_set_si(bset
->eq
[i
][pos
], 0);
12547 isl_seq_combine(bset
->eq
[i
], subs
->v
->el
[0], bset
->eq
[i
],
12548 v
, subs
->v
->el
+ 1, subs
->v
->size
- 1);
12551 for (i
= 0; i
< bset
->n_ineq
; ++i
) {
12552 if (isl_int_is_zero(bset
->ineq
[i
][pos
]))
12554 isl_int_set(v
, bset
->ineq
[i
][pos
]);
12555 isl_int_set_si(bset
->ineq
[i
][pos
], 0);
12556 isl_seq_combine(bset
->ineq
[i
], subs
->v
->el
[0], bset
->ineq
[i
],
12557 v
, subs
->v
->el
+ 1, subs
->v
->size
- 1);
12560 for (i
= 0; i
< bset
->n_div
; ++i
) {
12561 if (isl_int_is_zero(bset
->div
[i
][1 + pos
]))
12563 isl_int_set(v
, bset
->div
[i
][1 + pos
]);
12564 isl_int_set_si(bset
->div
[i
][1 + pos
], 0);
12565 isl_seq_combine(bset
->div
[i
] + 1,
12566 subs
->v
->el
[0], bset
->div
[i
] + 1,
12567 v
, subs
->v
->el
+ 1, subs
->v
->size
- 1);
12568 isl_int_mul(bset
->div
[i
][0], bset
->div
[i
][0], subs
->v
->el
[0]);
12573 bset
= isl_basic_set_simplify(bset
);
12574 return isl_basic_set_finalize(bset
);
12576 isl_basic_set_free(bset
);
12580 /* Plug in "subs" for dimension "type", "pos" of "set".
12582 __isl_give isl_set
*isl_set_substitute(__isl_take isl_set
*set
,
12583 enum isl_dim_type type
, unsigned pos
, __isl_keep isl_aff
*subs
)
12587 if (set
&& isl_set_plain_is_empty(set
))
12590 set
= isl_set_cow(set
);
12594 for (i
= set
->n
- 1; i
>= 0; --i
) {
12595 set
->p
[i
] = isl_basic_set_substitute(set
->p
[i
], type
, pos
, subs
);
12596 if (remove_if_empty(set
, i
) < 0)
12606 /* Check if the range of "ma" is compatible with the domain or range
12607 * (depending on "type") of "bmap".
12609 static isl_stat
check_basic_map_compatible_range_multi_aff(
12610 __isl_keep isl_basic_map
*bmap
, enum isl_dim_type type
,
12611 __isl_keep isl_multi_aff
*ma
)
12614 isl_space
*ma_space
;
12616 ma_space
= isl_multi_aff_get_space(ma
);
12618 m
= isl_space_has_equal_params(bmap
->dim
, ma_space
);
12622 isl_die(isl_basic_map_get_ctx(bmap
), isl_error_invalid
,
12623 "parameters don't match", goto error
);
12624 m
= isl_space_tuple_is_equal(bmap
->dim
, type
, ma_space
, isl_dim_out
);
12628 isl_die(isl_basic_map_get_ctx(bmap
), isl_error_invalid
,
12629 "spaces don't match", goto error
);
12631 isl_space_free(ma_space
);
12632 return isl_stat_ok
;
12634 isl_space_free(ma_space
);
12635 return isl_stat_error
;
12638 /* Copy the divs from "ma" to "bmap", adding zeros for the "n_before"
12639 * coefficients before the transformed range of dimensions,
12640 * the "n_after" coefficients after the transformed range of dimensions
12641 * and the coefficients of the other divs in "bmap".
12643 static int set_ma_divs(__isl_keep isl_basic_map
*bmap
,
12644 __isl_keep isl_multi_aff
*ma
, int n_before
, int n_after
, int n_div
)
12649 isl_local_space
*ls
;
12654 ls
= isl_aff_get_domain_local_space(ma
->p
[0]);
12658 n_param
= isl_local_space_dim(ls
, isl_dim_param
);
12659 n_set
= isl_local_space_dim(ls
, isl_dim_set
);
12660 for (i
= 0; i
< n_div
; ++i
) {
12661 int o_bmap
= 0, o_ls
= 0;
12663 isl_seq_cpy(bmap
->div
[i
], ls
->div
->row
[i
], 1 + 1 + n_param
);
12664 o_bmap
+= 1 + 1 + n_param
;
12665 o_ls
+= 1 + 1 + n_param
;
12666 isl_seq_clr(bmap
->div
[i
] + o_bmap
, n_before
);
12667 o_bmap
+= n_before
;
12668 isl_seq_cpy(bmap
->div
[i
] + o_bmap
,
12669 ls
->div
->row
[i
] + o_ls
, n_set
);
12672 isl_seq_clr(bmap
->div
[i
] + o_bmap
, n_after
);
12674 isl_seq_cpy(bmap
->div
[i
] + o_bmap
,
12675 ls
->div
->row
[i
] + o_ls
, n_div
);
12678 isl_seq_clr(bmap
->div
[i
] + o_bmap
, bmap
->n_div
- n_div
);
12679 if (isl_basic_map_add_div_constraints(bmap
, i
) < 0)
12683 isl_local_space_free(ls
);
12686 isl_local_space_free(ls
);
12690 /* How many stride constraints does "ma" enforce?
12691 * That is, how many of the affine expressions have a denominator
12692 * different from one?
12694 static int multi_aff_strides(__isl_keep isl_multi_aff
*ma
)
12699 for (i
= 0; i
< ma
->n
; ++i
)
12700 if (!isl_int_is_one(ma
->p
[i
]->v
->el
[0]))
12706 /* For each affine expression in ma of the form
12708 * x_i = (f_i y + h_i)/m_i
12710 * with m_i different from one, add a constraint to "bmap"
12713 * f_i y + h_i = m_i alpha_i
12715 * with alpha_i an additional existentially quantified variable.
12717 * The input variables of "ma" correspond to a subset of the variables
12718 * of "bmap". There are "n_before" variables in "bmap" before this
12719 * subset and "n_after" variables after this subset.
12720 * The integer divisions of the affine expressions in "ma" are assumed
12721 * to have been aligned. There are "n_div_ma" of them and
12722 * they appear first in "bmap", straight after the "n_after" variables.
12724 static __isl_give isl_basic_map
*add_ma_strides(
12725 __isl_take isl_basic_map
*bmap
, __isl_keep isl_multi_aff
*ma
,
12726 int n_before
, int n_after
, int n_div_ma
)
12734 total
= isl_basic_map_total_dim(bmap
);
12735 n_param
= isl_multi_aff_dim(ma
, isl_dim_param
);
12736 n_in
= isl_multi_aff_dim(ma
, isl_dim_in
);
12737 for (i
= 0; i
< ma
->n
; ++i
) {
12738 int o_bmap
= 0, o_ma
= 1;
12740 if (isl_int_is_one(ma
->p
[i
]->v
->el
[0]))
12742 div
= isl_basic_map_alloc_div(bmap
);
12743 k
= isl_basic_map_alloc_equality(bmap
);
12744 if (div
< 0 || k
< 0)
12746 isl_int_set_si(bmap
->div
[div
][0], 0);
12747 isl_seq_cpy(bmap
->eq
[k
] + o_bmap
,
12748 ma
->p
[i
]->v
->el
+ o_ma
, 1 + n_param
);
12749 o_bmap
+= 1 + n_param
;
12750 o_ma
+= 1 + n_param
;
12751 isl_seq_clr(bmap
->eq
[k
] + o_bmap
, n_before
);
12752 o_bmap
+= n_before
;
12753 isl_seq_cpy(bmap
->eq
[k
] + o_bmap
,
12754 ma
->p
[i
]->v
->el
+ o_ma
, n_in
);
12757 isl_seq_clr(bmap
->eq
[k
] + o_bmap
, n_after
);
12759 isl_seq_cpy(bmap
->eq
[k
] + o_bmap
,
12760 ma
->p
[i
]->v
->el
+ o_ma
, n_div_ma
);
12761 o_bmap
+= n_div_ma
;
12763 isl_seq_clr(bmap
->eq
[k
] + o_bmap
, 1 + total
- o_bmap
);
12764 isl_int_neg(bmap
->eq
[k
][1 + total
], ma
->p
[i
]->v
->el
[0]);
12770 isl_basic_map_free(bmap
);
12774 /* Replace the domain or range space (depending on "type) of "space" by "set".
12776 static __isl_give isl_space
*isl_space_set(__isl_take isl_space
*space
,
12777 enum isl_dim_type type
, __isl_take isl_space
*set
)
12779 if (type
== isl_dim_in
) {
12780 space
= isl_space_range(space
);
12781 space
= isl_space_map_from_domain_and_range(set
, space
);
12783 space
= isl_space_domain(space
);
12784 space
= isl_space_map_from_domain_and_range(space
, set
);
12790 /* Compute the preimage of the domain or range (depending on "type")
12791 * of "bmap" under the function represented by "ma".
12792 * In other words, plug in "ma" in the domain or range of "bmap".
12793 * The result is a basic map that lives in the same space as "bmap"
12794 * except that the domain or range has been replaced by
12795 * the domain space of "ma".
12797 * If bmap is represented by
12799 * A(p) + S u + B x + T v + C(divs) >= 0,
12801 * where u and x are input and output dimensions if type == isl_dim_out
12802 * while x and v are input and output dimensions if type == isl_dim_in,
12803 * and ma is represented by
12805 * x = D(p) + F(y) + G(divs')
12807 * then the result is
12809 * A(p) + B D(p) + S u + B F(y) + T v + B G(divs') + C(divs) >= 0
12811 * The divs in the input set are similarly adjusted.
12814 * floor((a_i(p) + s u + b_i x + t v + c_i(divs))/n_i)
12818 * floor((a_i(p) + b_i D(p) + s u + b_i F(y) + t v +
12819 * B_i G(divs') + c_i(divs))/n_i)
12821 * If bmap is not a rational map and if F(y) involves any denominators
12823 * x_i = (f_i y + h_i)/m_i
12825 * then additional constraints are added to ensure that we only
12826 * map back integer points. That is we enforce
12828 * f_i y + h_i = m_i alpha_i
12830 * with alpha_i an additional existentially quantified variable.
12832 * We first copy over the divs from "ma".
12833 * Then we add the modified constraints and divs from "bmap".
12834 * Finally, we add the stride constraints, if needed.
12836 __isl_give isl_basic_map
*isl_basic_map_preimage_multi_aff(
12837 __isl_take isl_basic_map
*bmap
, enum isl_dim_type type
,
12838 __isl_take isl_multi_aff
*ma
)
12842 isl_basic_map
*res
= NULL
;
12843 int n_before
, n_after
, n_div_bmap
, n_div_ma
;
12844 isl_int f
, c1
, c2
, g
;
12853 ma
= isl_multi_aff_align_divs(ma
);
12856 if (check_basic_map_compatible_range_multi_aff(bmap
, type
, ma
) < 0)
12859 if (type
== isl_dim_in
) {
12861 n_after
= isl_basic_map_dim(bmap
, isl_dim_out
);
12863 n_before
= isl_basic_map_dim(bmap
, isl_dim_in
);
12866 n_div_bmap
= isl_basic_map_dim(bmap
, isl_dim_div
);
12867 n_div_ma
= ma
->n
? isl_aff_dim(ma
->p
[0], isl_dim_div
) : 0;
12869 space
= isl_multi_aff_get_domain_space(ma
);
12870 space
= isl_space_set(isl_basic_map_get_space(bmap
), type
, space
);
12871 rational
= isl_basic_map_is_rational(bmap
);
12872 strides
= rational
? 0 : multi_aff_strides(ma
);
12873 res
= isl_basic_map_alloc_space(space
, n_div_ma
+ n_div_bmap
+ strides
,
12874 bmap
->n_eq
+ strides
, bmap
->n_ineq
+ 2 * n_div_ma
);
12876 res
= isl_basic_map_set_rational(res
);
12878 for (i
= 0; i
< n_div_ma
+ n_div_bmap
; ++i
)
12879 if (isl_basic_map_alloc_div(res
) < 0)
12882 if (set_ma_divs(res
, ma
, n_before
, n_after
, n_div_ma
) < 0)
12885 for (i
= 0; i
< bmap
->n_eq
; ++i
) {
12886 k
= isl_basic_map_alloc_equality(res
);
12889 isl_seq_preimage(res
->eq
[k
], bmap
->eq
[i
], ma
, n_before
,
12890 n_after
, n_div_ma
, n_div_bmap
, f
, c1
, c2
, g
, 0);
12893 for (i
= 0; i
< bmap
->n_ineq
; ++i
) {
12894 k
= isl_basic_map_alloc_inequality(res
);
12897 isl_seq_preimage(res
->ineq
[k
], bmap
->ineq
[i
], ma
, n_before
,
12898 n_after
, n_div_ma
, n_div_bmap
, f
, c1
, c2
, g
, 0);
12901 for (i
= 0; i
< bmap
->n_div
; ++i
) {
12902 if (isl_int_is_zero(bmap
->div
[i
][0])) {
12903 isl_int_set_si(res
->div
[n_div_ma
+ i
][0], 0);
12906 isl_seq_preimage(res
->div
[n_div_ma
+ i
], bmap
->div
[i
], ma
,
12907 n_before
, n_after
, n_div_ma
, n_div_bmap
,
12912 res
= add_ma_strides(res
, ma
, n_before
, n_after
, n_div_ma
);
12918 isl_basic_map_free(bmap
);
12919 isl_multi_aff_free(ma
);
12920 res
= isl_basic_map_simplify(res
);
12921 return isl_basic_map_finalize(res
);
12927 isl_basic_map_free(bmap
);
12928 isl_multi_aff_free(ma
);
12929 isl_basic_map_free(res
);
12933 /* Compute the preimage of "bset" under the function represented by "ma".
12934 * In other words, plug in "ma" in "bset". The result is a basic set
12935 * that lives in the domain space of "ma".
12937 __isl_give isl_basic_set
*isl_basic_set_preimage_multi_aff(
12938 __isl_take isl_basic_set
*bset
, __isl_take isl_multi_aff
*ma
)
12940 return isl_basic_map_preimage_multi_aff(bset
, isl_dim_set
, ma
);
12943 /* Compute the preimage of the domain of "bmap" under the function
12944 * represented by "ma".
12945 * In other words, plug in "ma" in the domain of "bmap".
12946 * The result is a basic map that lives in the same space as "bmap"
12947 * except that the domain has been replaced by the domain space of "ma".
12949 __isl_give isl_basic_map
*isl_basic_map_preimage_domain_multi_aff(
12950 __isl_take isl_basic_map
*bmap
, __isl_take isl_multi_aff
*ma
)
12952 return isl_basic_map_preimage_multi_aff(bmap
, isl_dim_in
, ma
);
12955 /* Compute the preimage of the range of "bmap" under the function
12956 * represented by "ma".
12957 * In other words, plug in "ma" in the range of "bmap".
12958 * The result is a basic map that lives in the same space as "bmap"
12959 * except that the range has been replaced by the domain space of "ma".
12961 __isl_give isl_basic_map
*isl_basic_map_preimage_range_multi_aff(
12962 __isl_take isl_basic_map
*bmap
, __isl_take isl_multi_aff
*ma
)
12964 return isl_basic_map_preimage_multi_aff(bmap
, isl_dim_out
, ma
);
12967 /* Check if the range of "ma" is compatible with the domain or range
12968 * (depending on "type") of "map".
12969 * Return isl_stat_error if anything is wrong.
12971 static isl_stat
check_map_compatible_range_multi_aff(
12972 __isl_keep isl_map
*map
, enum isl_dim_type type
,
12973 __isl_keep isl_multi_aff
*ma
)
12976 isl_space
*ma_space
;
12978 ma_space
= isl_multi_aff_get_space(ma
);
12979 m
= isl_space_tuple_is_equal(map
->dim
, type
, ma_space
, isl_dim_out
);
12980 isl_space_free(ma_space
);
12982 return isl_stat_error
;
12984 isl_die(isl_map_get_ctx(map
), isl_error_invalid
,
12985 "spaces don't match", return isl_stat_error
);
12986 return isl_stat_ok
;
12989 /* Compute the preimage of the domain or range (depending on "type")
12990 * of "map" under the function represented by "ma".
12991 * In other words, plug in "ma" in the domain or range of "map".
12992 * The result is a map that lives in the same space as "map"
12993 * except that the domain or range has been replaced by
12994 * the domain space of "ma".
12996 * The parameters are assumed to have been aligned.
12998 static __isl_give isl_map
*map_preimage_multi_aff(__isl_take isl_map
*map
,
12999 enum isl_dim_type type
, __isl_take isl_multi_aff
*ma
)
13004 map
= isl_map_cow(map
);
13005 ma
= isl_multi_aff_align_divs(ma
);
13008 if (check_map_compatible_range_multi_aff(map
, type
, ma
) < 0)
13011 for (i
= 0; i
< map
->n
; ++i
) {
13012 map
->p
[i
] = isl_basic_map_preimage_multi_aff(map
->p
[i
], type
,
13013 isl_multi_aff_copy(ma
));
13018 space
= isl_multi_aff_get_domain_space(ma
);
13019 space
= isl_space_set(isl_map_get_space(map
), type
, space
);
13021 isl_space_free(map
->dim
);
13026 isl_multi_aff_free(ma
);
13028 ISL_F_CLR(map
, ISL_MAP_DISJOINT
);
13029 ISL_F_CLR(map
, ISL_SET_NORMALIZED
);
13032 isl_multi_aff_free(ma
);
13037 /* Compute the preimage of the domain or range (depending on "type")
13038 * of "map" under the function represented by "ma".
13039 * In other words, plug in "ma" in the domain or range of "map".
13040 * The result is a map that lives in the same space as "map"
13041 * except that the domain or range has been replaced by
13042 * the domain space of "ma".
13044 __isl_give isl_map
*isl_map_preimage_multi_aff(__isl_take isl_map
*map
,
13045 enum isl_dim_type type
, __isl_take isl_multi_aff
*ma
)
13052 aligned
= isl_map_space_has_equal_params(map
, ma
->space
);
13056 return map_preimage_multi_aff(map
, type
, ma
);
13058 if (isl_map_check_named_params(map
) < 0)
13060 if (!isl_space_has_named_params(ma
->space
))
13061 isl_die(map
->ctx
, isl_error_invalid
,
13062 "unaligned unnamed parameters", goto error
);
13063 map
= isl_map_align_params(map
, isl_multi_aff_get_space(ma
));
13064 ma
= isl_multi_aff_align_params(ma
, isl_map_get_space(map
));
13066 return map_preimage_multi_aff(map
, type
, ma
);
13068 isl_multi_aff_free(ma
);
13069 return isl_map_free(map
);
13072 /* Compute the preimage of "set" under the function represented by "ma".
13073 * In other words, plug in "ma" in "set". The result is a set
13074 * that lives in the domain space of "ma".
13076 __isl_give isl_set
*isl_set_preimage_multi_aff(__isl_take isl_set
*set
,
13077 __isl_take isl_multi_aff
*ma
)
13079 return isl_map_preimage_multi_aff(set
, isl_dim_set
, ma
);
13082 /* Compute the preimage of the domain of "map" under the function
13083 * represented by "ma".
13084 * In other words, plug in "ma" in the domain of "map".
13085 * The result is a map that lives in the same space as "map"
13086 * except that the domain has been replaced by the domain space of "ma".
13088 __isl_give isl_map
*isl_map_preimage_domain_multi_aff(__isl_take isl_map
*map
,
13089 __isl_take isl_multi_aff
*ma
)
13091 return isl_map_preimage_multi_aff(map
, isl_dim_in
, ma
);
13094 /* Compute the preimage of the range of "map" under the function
13095 * represented by "ma".
13096 * In other words, plug in "ma" in the range of "map".
13097 * The result is a map that lives in the same space as "map"
13098 * except that the range has been replaced by the domain space of "ma".
13100 __isl_give isl_map
*isl_map_preimage_range_multi_aff(__isl_take isl_map
*map
,
13101 __isl_take isl_multi_aff
*ma
)
13103 return isl_map_preimage_multi_aff(map
, isl_dim_out
, ma
);
13106 /* Compute the preimage of "map" under the function represented by "pma".
13107 * In other words, plug in "pma" in the domain or range of "map".
13108 * The result is a map that lives in the same space as "map",
13109 * except that the space of type "type" has been replaced by
13110 * the domain space of "pma".
13112 * The parameters of "map" and "pma" are assumed to have been aligned.
13114 static __isl_give isl_map
*isl_map_preimage_pw_multi_aff_aligned(
13115 __isl_take isl_map
*map
, enum isl_dim_type type
,
13116 __isl_take isl_pw_multi_aff
*pma
)
13125 isl_pw_multi_aff_free(pma
);
13126 res
= isl_map_empty(isl_map_get_space(map
));
13131 res
= isl_map_preimage_multi_aff(isl_map_copy(map
), type
,
13132 isl_multi_aff_copy(pma
->p
[0].maff
));
13133 if (type
== isl_dim_in
)
13134 res
= isl_map_intersect_domain(res
,
13135 isl_map_copy(pma
->p
[0].set
));
13137 res
= isl_map_intersect_range(res
,
13138 isl_map_copy(pma
->p
[0].set
));
13140 for (i
= 1; i
< pma
->n
; ++i
) {
13143 res_i
= isl_map_preimage_multi_aff(isl_map_copy(map
), type
,
13144 isl_multi_aff_copy(pma
->p
[i
].maff
));
13145 if (type
== isl_dim_in
)
13146 res_i
= isl_map_intersect_domain(res_i
,
13147 isl_map_copy(pma
->p
[i
].set
));
13149 res_i
= isl_map_intersect_range(res_i
,
13150 isl_map_copy(pma
->p
[i
].set
));
13151 res
= isl_map_union(res
, res_i
);
13154 isl_pw_multi_aff_free(pma
);
13158 isl_pw_multi_aff_free(pma
);
13163 /* Compute the preimage of "map" under the function represented by "pma".
13164 * In other words, plug in "pma" in the domain or range of "map".
13165 * The result is a map that lives in the same space as "map",
13166 * except that the space of type "type" has been replaced by
13167 * the domain space of "pma".
13169 __isl_give isl_map
*isl_map_preimage_pw_multi_aff(__isl_take isl_map
*map
,
13170 enum isl_dim_type type
, __isl_take isl_pw_multi_aff
*pma
)
13177 aligned
= isl_map_space_has_equal_params(map
, pma
->dim
);
13181 return isl_map_preimage_pw_multi_aff_aligned(map
, type
, pma
);
13183 if (isl_map_check_named_params(map
) < 0)
13185 if (!isl_space_has_named_params(pma
->dim
))
13186 isl_die(map
->ctx
, isl_error_invalid
,
13187 "unaligned unnamed parameters", goto error
);
13188 map
= isl_map_align_params(map
, isl_pw_multi_aff_get_space(pma
));
13189 pma
= isl_pw_multi_aff_align_params(pma
, isl_map_get_space(map
));
13191 return isl_map_preimage_pw_multi_aff_aligned(map
, type
, pma
);
13193 isl_pw_multi_aff_free(pma
);
13194 return isl_map_free(map
);
13197 /* Compute the preimage of "set" under the function represented by "pma".
13198 * In other words, plug in "pma" in "set". The result is a set
13199 * that lives in the domain space of "pma".
13201 __isl_give isl_set
*isl_set_preimage_pw_multi_aff(__isl_take isl_set
*set
,
13202 __isl_take isl_pw_multi_aff
*pma
)
13204 return isl_map_preimage_pw_multi_aff(set
, isl_dim_set
, pma
);
13207 /* Compute the preimage of the domain of "map" under the function
13208 * represented by "pma".
13209 * In other words, plug in "pma" in the domain of "map".
13210 * The result is a map that lives in the same space as "map",
13211 * except that domain space has been replaced by the domain space of "pma".
13213 __isl_give isl_map
*isl_map_preimage_domain_pw_multi_aff(
13214 __isl_take isl_map
*map
, __isl_take isl_pw_multi_aff
*pma
)
13216 return isl_map_preimage_pw_multi_aff(map
, isl_dim_in
, pma
);
13219 /* Compute the preimage of the range of "map" under the function
13220 * represented by "pma".
13221 * In other words, plug in "pma" in the range of "map".
13222 * The result is a map that lives in the same space as "map",
13223 * except that range space has been replaced by the domain space of "pma".
13225 __isl_give isl_map
*isl_map_preimage_range_pw_multi_aff(
13226 __isl_take isl_map
*map
, __isl_take isl_pw_multi_aff
*pma
)
13228 return isl_map_preimage_pw_multi_aff(map
, isl_dim_out
, pma
);
13231 /* Compute the preimage of "map" under the function represented by "mpa".
13232 * In other words, plug in "mpa" in the domain or range of "map".
13233 * The result is a map that lives in the same space as "map",
13234 * except that the space of type "type" has been replaced by
13235 * the domain space of "mpa".
13237 * If the map does not involve any constraints that refer to the
13238 * dimensions of the substituted space, then the only possible
13239 * effect of "mpa" on the map is to map the space to a different space.
13240 * We create a separate isl_multi_aff to effectuate this change
13241 * in order to avoid spurious splitting of the map along the pieces
13244 __isl_give isl_map
*isl_map_preimage_multi_pw_aff(__isl_take isl_map
*map
,
13245 enum isl_dim_type type
, __isl_take isl_multi_pw_aff
*mpa
)
13248 isl_pw_multi_aff
*pma
;
13253 n
= isl_map_dim(map
, type
);
13254 if (!isl_map_involves_dims(map
, type
, 0, n
)) {
13258 space
= isl_multi_pw_aff_get_space(mpa
);
13259 isl_multi_pw_aff_free(mpa
);
13260 ma
= isl_multi_aff_zero(space
);
13261 return isl_map_preimage_multi_aff(map
, type
, ma
);
13264 pma
= isl_pw_multi_aff_from_multi_pw_aff(mpa
);
13265 return isl_map_preimage_pw_multi_aff(map
, type
, pma
);
13268 isl_multi_pw_aff_free(mpa
);
13272 /* Compute the preimage of "map" under the function represented by "mpa".
13273 * In other words, plug in "mpa" in the domain "map".
13274 * The result is a map that lives in the same space as "map",
13275 * except that domain space has been replaced by the domain space of "mpa".
13277 __isl_give isl_map
*isl_map_preimage_domain_multi_pw_aff(
13278 __isl_take isl_map
*map
, __isl_take isl_multi_pw_aff
*mpa
)
13280 return isl_map_preimage_multi_pw_aff(map
, isl_dim_in
, mpa
);
13283 /* Compute the preimage of "set" by the function represented by "mpa".
13284 * In other words, plug in "mpa" in "set".
13286 __isl_give isl_set
*isl_set_preimage_multi_pw_aff(__isl_take isl_set
*set
,
13287 __isl_take isl_multi_pw_aff
*mpa
)
13289 return isl_map_preimage_multi_pw_aff(set
, isl_dim_set
, mpa
);
13292 /* Are the "n" "coefficients" starting at "first" of the integer division
13293 * expressions at position "pos1" in "bmap1" and "pos2" in "bmap2" equal
13295 * The "coefficient" at position 0 is the denominator.
13296 * The "coefficient" at position 1 is the constant term.
13298 isl_bool
isl_basic_map_equal_div_expr_part(__isl_keep isl_basic_map
*bmap1
,
13299 int pos1
, __isl_keep isl_basic_map
*bmap2
, int pos2
,
13300 unsigned first
, unsigned n
)
13302 if (isl_basic_map_check_range(bmap1
, isl_dim_div
, pos1
, 1) < 0)
13303 return isl_bool_error
;
13304 if (isl_basic_map_check_range(bmap2
, isl_dim_div
, pos2
, 1) < 0)
13305 return isl_bool_error
;
13306 return isl_seq_eq(bmap1
->div
[pos1
] + first
,
13307 bmap2
->div
[pos2
] + first
, n
);
13310 /* Are the integer division expressions at position "pos1" in "bmap1" and
13311 * "pos2" in "bmap2" equal to each other, except that the constant terms
13314 isl_bool
isl_basic_map_equal_div_expr_except_constant(
13315 __isl_keep isl_basic_map
*bmap1
, int pos1
,
13316 __isl_keep isl_basic_map
*bmap2
, int pos2
)
13321 if (!bmap1
|| !bmap2
)
13322 return isl_bool_error
;
13323 total
= isl_basic_map_total_dim(bmap1
);
13324 if (total
!= isl_basic_map_total_dim(bmap2
))
13325 isl_die(isl_basic_map_get_ctx(bmap1
), isl_error_invalid
,
13326 "incomparable div expressions", return isl_bool_error
);
13327 equal
= isl_basic_map_equal_div_expr_part(bmap1
, pos1
, bmap2
, pos2
,
13329 if (equal
< 0 || !equal
)
13331 equal
= isl_basic_map_equal_div_expr_part(bmap1
, pos1
, bmap2
, pos2
,
13333 if (equal
< 0 || equal
)
13334 return isl_bool_not(equal
);
13335 return isl_basic_map_equal_div_expr_part(bmap1
, pos1
, bmap2
, pos2
,
13339 /* Replace the numerator of the constant term of the integer division
13340 * expression at position "div" in "bmap" by "value".
13341 * The caller guarantees that this does not change the meaning
13344 __isl_give isl_basic_map
*isl_basic_map_set_div_expr_constant_num_si_inplace(
13345 __isl_take isl_basic_map
*bmap
, int div
, int value
)
13347 if (isl_basic_map_check_range(bmap
, isl_dim_div
, div
, 1) < 0)
13348 return isl_basic_map_free(bmap
);
13350 isl_int_set_si(bmap
->div
[div
][1], value
);
13355 /* Is the point "inner" internal to inequality constraint "ineq"
13357 * The point is considered to be internal to the inequality constraint,
13358 * if it strictly lies on the positive side of the inequality constraint,
13359 * or if it lies on the constraint and the constraint is lexico-positive.
13361 static isl_bool
is_internal(__isl_keep isl_vec
*inner
,
13362 __isl_keep isl_basic_set
*bset
, int ineq
)
13368 if (!inner
|| !bset
)
13369 return isl_bool_error
;
13371 ctx
= isl_basic_set_get_ctx(bset
);
13372 isl_seq_inner_product(inner
->el
, bset
->ineq
[ineq
], inner
->size
,
13373 &ctx
->normalize_gcd
);
13374 if (!isl_int_is_zero(ctx
->normalize_gcd
))
13375 return isl_int_is_nonneg(ctx
->normalize_gcd
);
13377 total
= isl_basic_set_dim(bset
, isl_dim_all
);
13378 pos
= isl_seq_first_non_zero(bset
->ineq
[ineq
] + 1, total
);
13379 return isl_int_is_pos(bset
->ineq
[ineq
][1 + pos
]);
13382 /* Tighten the inequality constraints of "bset" that are outward with respect
13383 * to the point "vec".
13384 * That is, tighten the constraints that are not satisfied by "vec".
13386 * "vec" is a point internal to some superset S of "bset" that is used
13387 * to make the subsets of S disjoint, by tightening one half of the constraints
13388 * that separate two subsets. In particular, the constraints of S
13389 * are all satisfied by "vec" and should not be tightened.
13390 * Of the internal constraints, those that have "vec" on the outside
13391 * are tightened. The shared facet is included in the adjacent subset
13392 * with the opposite constraint.
13393 * For constraints that saturate "vec", this criterion cannot be used
13394 * to determine which of the two sides should be tightened.
13395 * Instead, the sign of the first non-zero coefficient is used
13396 * to make this choice. Note that this second criterion is never used
13397 * on the constraints of S since "vec" is interior to "S".
13399 __isl_give isl_basic_set
*isl_basic_set_tighten_outward(
13400 __isl_take isl_basic_set
*bset
, __isl_keep isl_vec
*vec
)
13404 bset
= isl_basic_set_cow(bset
);
13407 for (j
= 0; j
< bset
->n_ineq
; ++j
) {
13410 internal
= is_internal(vec
, bset
, j
);
13412 return isl_basic_set_free(bset
);
13415 isl_int_sub_ui(bset
->ineq
[j
][0], bset
->ineq
[j
][0], 1);
13421 /* Replace the variables x of type "type" starting at "first" in "bmap"
13422 * by x' with x = M x' with M the matrix trans.
13423 * That is, replace the corresponding coefficients c by c M.
13425 * The transformation matrix should be a square matrix.
13427 __isl_give isl_basic_map
*isl_basic_map_transform_dims(
13428 __isl_take isl_basic_map
*bmap
, enum isl_dim_type type
, unsigned first
,
13429 __isl_take isl_mat
*trans
)
13433 bmap
= isl_basic_map_cow(bmap
);
13434 if (!bmap
|| !trans
)
13437 if (trans
->n_row
!= trans
->n_col
)
13438 isl_die(trans
->ctx
, isl_error_invalid
,
13439 "expecting square transformation matrix", goto error
);
13440 if (first
+ trans
->n_row
> isl_basic_map_dim(bmap
, type
))
13441 isl_die(trans
->ctx
, isl_error_invalid
,
13442 "oversized transformation matrix", goto error
);
13444 pos
= isl_basic_map_offset(bmap
, type
) + first
;
13446 if (isl_mat_sub_transform(bmap
->eq
, bmap
->n_eq
, pos
,
13447 isl_mat_copy(trans
)) < 0)
13449 if (isl_mat_sub_transform(bmap
->ineq
, bmap
->n_ineq
, pos
,
13450 isl_mat_copy(trans
)) < 0)
13452 if (isl_mat_sub_transform(bmap
->div
, bmap
->n_div
, 1 + pos
,
13453 isl_mat_copy(trans
)) < 0)
13456 ISL_F_CLR(bmap
, ISL_BASIC_MAP_NORMALIZED
);
13457 ISL_F_CLR(bmap
, ISL_BASIC_MAP_NORMALIZED_DIVS
);
13459 isl_mat_free(trans
);
13462 isl_mat_free(trans
);
13463 isl_basic_map_free(bmap
);
13467 /* Replace the variables x of type "type" starting at "first" in "bset"
13468 * by x' with x = M x' with M the matrix trans.
13469 * That is, replace the corresponding coefficients c by c M.
13471 * The transformation matrix should be a square matrix.
13473 __isl_give isl_basic_set
*isl_basic_set_transform_dims(
13474 __isl_take isl_basic_set
*bset
, enum isl_dim_type type
, unsigned first
,
13475 __isl_take isl_mat
*trans
)
13477 return isl_basic_map_transform_dims(bset
, type
, first
, trans
);