2 * Copyright 2008-2009 Katholieke Universiteit Leuven
3 * Copyright 2010 INRIA Saclay
4 * Copyright 2012 Ecole Normale Superieure
6 * Use of this software is governed by the GNU LGPLv2.1 license
8 * Written by Sven Verdoolaege, K.U.Leuven, Departement
9 * Computerwetenschappen, Celestijnenlaan 200A, B-3001 Leuven, Belgium
10 * and INRIA Saclay - Ile-de-France, Parc Club Orsay Universite,
11 * ZAC des vignes, 4 rue Jacques Monod, 91893 Orsay, France
12 * and Ecole Normale Superieure, 45 rue d’Ulm, 75230 Paris, France
16 #include <isl_ctx_private.h>
17 #include <isl_map_private.h>
19 #include "isl_space_private.h"
20 #include "isl_equalities.h"
21 #include <isl_list_private.h>
26 #include "isl_map_piplib.h"
27 #include <isl_reordering.h>
28 #include "isl_sample.h"
31 #include <isl_mat_private.h>
32 #include <isl_dim_map.h>
33 #include <isl_local_space_private.h>
34 #include <isl_aff_private.h>
35 #include <isl_options_private.h>
37 static unsigned n(__isl_keep isl_space
*dim
, enum isl_dim_type type
)
40 case isl_dim_param
: return dim
->nparam
;
41 case isl_dim_in
: return dim
->n_in
;
42 case isl_dim_out
: return dim
->n_out
;
43 case isl_dim_all
: return dim
->nparam
+ dim
->n_in
+ dim
->n_out
;
48 static unsigned pos(__isl_keep isl_space
*dim
, enum isl_dim_type type
)
51 case isl_dim_param
: return 1;
52 case isl_dim_in
: return 1 + dim
->nparam
;
53 case isl_dim_out
: return 1 + dim
->nparam
+ dim
->n_in
;
58 unsigned isl_basic_map_dim(__isl_keep isl_basic_map
*bmap
,
59 enum isl_dim_type type
)
64 case isl_dim_cst
: return 1;
67 case isl_dim_out
: return isl_space_dim(bmap
->dim
, type
);
68 case isl_dim_div
: return bmap
->n_div
;
69 case isl_dim_all
: return isl_basic_map_total_dim(bmap
);
74 unsigned isl_map_dim(__isl_keep isl_map
*map
, enum isl_dim_type type
)
76 return map
? n(map
->dim
, type
) : 0;
79 unsigned isl_set_dim(__isl_keep isl_set
*set
, enum isl_dim_type type
)
81 return set
? n(set
->dim
, type
) : 0;
84 unsigned isl_basic_map_offset(struct isl_basic_map
*bmap
,
85 enum isl_dim_type type
)
87 isl_space
*dim
= bmap
->dim
;
89 case isl_dim_cst
: return 0;
90 case isl_dim_param
: return 1;
91 case isl_dim_in
: return 1 + dim
->nparam
;
92 case isl_dim_out
: return 1 + dim
->nparam
+ dim
->n_in
;
93 case isl_dim_div
: return 1 + dim
->nparam
+ dim
->n_in
+ dim
->n_out
;
98 unsigned isl_basic_set_offset(struct isl_basic_set
*bset
,
99 enum isl_dim_type type
)
101 return isl_basic_map_offset(bset
, type
);
104 static unsigned map_offset(struct isl_map
*map
, enum isl_dim_type type
)
106 return pos(map
->dim
, type
);
109 unsigned isl_basic_set_dim(__isl_keep isl_basic_set
*bset
,
110 enum isl_dim_type type
)
112 return isl_basic_map_dim(bset
, type
);
115 unsigned isl_basic_set_n_dim(__isl_keep isl_basic_set
*bset
)
117 return isl_basic_set_dim(bset
, isl_dim_set
);
120 unsigned isl_basic_set_n_param(__isl_keep isl_basic_set
*bset
)
122 return isl_basic_set_dim(bset
, isl_dim_param
);
125 unsigned isl_basic_set_total_dim(const struct isl_basic_set
*bset
)
129 return isl_space_dim(bset
->dim
, isl_dim_all
) + bset
->n_div
;
132 unsigned isl_set_n_dim(__isl_keep isl_set
*set
)
134 return isl_set_dim(set
, isl_dim_set
);
137 unsigned isl_set_n_param(__isl_keep isl_set
*set
)
139 return isl_set_dim(set
, isl_dim_param
);
142 unsigned isl_basic_map_n_in(const struct isl_basic_map
*bmap
)
144 return bmap
? bmap
->dim
->n_in
: 0;
147 unsigned isl_basic_map_n_out(const struct isl_basic_map
*bmap
)
149 return bmap
? bmap
->dim
->n_out
: 0;
152 unsigned isl_basic_map_n_param(const struct isl_basic_map
*bmap
)
154 return bmap
? bmap
->dim
->nparam
: 0;
157 unsigned isl_basic_map_n_div(const struct isl_basic_map
*bmap
)
159 return bmap
? bmap
->n_div
: 0;
162 unsigned isl_basic_map_total_dim(const struct isl_basic_map
*bmap
)
164 return bmap
? isl_space_dim(bmap
->dim
, isl_dim_all
) + bmap
->n_div
: 0;
167 unsigned isl_map_n_in(const struct isl_map
*map
)
169 return map
? map
->dim
->n_in
: 0;
172 unsigned isl_map_n_out(const struct isl_map
*map
)
174 return map
? map
->dim
->n_out
: 0;
177 unsigned isl_map_n_param(const struct isl_map
*map
)
179 return map
? map
->dim
->nparam
: 0;
182 int isl_map_compatible_domain(struct isl_map
*map
, struct isl_set
*set
)
187 m
= isl_space_match(map
->dim
, isl_dim_param
, set
->dim
, isl_dim_param
);
190 return isl_space_tuple_match(map
->dim
, isl_dim_in
, set
->dim
, isl_dim_set
);
193 int isl_basic_map_compatible_domain(struct isl_basic_map
*bmap
,
194 struct isl_basic_set
*bset
)
199 m
= isl_space_match(bmap
->dim
, isl_dim_param
, bset
->dim
, isl_dim_param
);
202 return isl_space_tuple_match(bmap
->dim
, isl_dim_in
, bset
->dim
, isl_dim_set
);
205 int isl_map_compatible_range(__isl_keep isl_map
*map
, __isl_keep isl_set
*set
)
210 m
= isl_space_match(map
->dim
, isl_dim_param
, set
->dim
, isl_dim_param
);
213 return isl_space_tuple_match(map
->dim
, isl_dim_out
, set
->dim
, isl_dim_set
);
216 int isl_basic_map_compatible_range(struct isl_basic_map
*bmap
,
217 struct isl_basic_set
*bset
)
222 m
= isl_space_match(bmap
->dim
, isl_dim_param
, bset
->dim
, isl_dim_param
);
225 return isl_space_tuple_match(bmap
->dim
, isl_dim_out
, bset
->dim
, isl_dim_set
);
228 isl_ctx
*isl_basic_map_get_ctx(__isl_keep isl_basic_map
*bmap
)
230 return bmap
? bmap
->ctx
: NULL
;
233 isl_ctx
*isl_basic_set_get_ctx(__isl_keep isl_basic_set
*bset
)
235 return bset
? bset
->ctx
: NULL
;
238 isl_ctx
*isl_map_get_ctx(__isl_keep isl_map
*map
)
240 return map
? map
->ctx
: NULL
;
243 isl_ctx
*isl_set_get_ctx(__isl_keep isl_set
*set
)
245 return set
? set
->ctx
: NULL
;
248 __isl_give isl_space
*isl_basic_map_get_space(__isl_keep isl_basic_map
*bmap
)
252 return isl_space_copy(bmap
->dim
);
255 __isl_give isl_space
*isl_basic_set_get_space(__isl_keep isl_basic_set
*bset
)
259 return isl_space_copy(bset
->dim
);
262 /* Extract the divs in "bmap" as a matrix.
264 __isl_give isl_mat
*isl_basic_map_get_divs(__isl_keep isl_basic_map
*bmap
)
275 ctx
= isl_basic_map_get_ctx(bmap
);
276 total
= isl_space_dim(bmap
->dim
, isl_dim_all
);
277 cols
= 1 + 1 + total
+ bmap
->n_div
;
278 div
= isl_mat_alloc(ctx
, bmap
->n_div
, cols
);
282 for (i
= 0; i
< bmap
->n_div
; ++i
)
283 isl_seq_cpy(div
->row
[i
], bmap
->div
[i
], cols
);
288 __isl_give isl_local_space
*isl_basic_map_get_local_space(
289 __isl_keep isl_basic_map
*bmap
)
296 div
= isl_basic_map_get_divs(bmap
);
297 return isl_local_space_alloc_div(isl_space_copy(bmap
->dim
), div
);
300 __isl_give isl_local_space
*isl_basic_set_get_local_space(
301 __isl_keep isl_basic_set
*bset
)
303 return isl_basic_map_get_local_space(bset
);
306 __isl_give isl_basic_map
*isl_basic_map_from_local_space(
307 __isl_take isl_local_space
*ls
)
316 n_div
= isl_local_space_dim(ls
, isl_dim_div
);
317 bmap
= isl_basic_map_alloc_space(isl_local_space_get_space(ls
),
318 n_div
, 0, 2 * n_div
);
320 for (i
= 0; i
< n_div
; ++i
)
321 if (isl_basic_map_alloc_div(bmap
) < 0)
324 for (i
= 0; i
< n_div
; ++i
) {
325 isl_seq_cpy(bmap
->div
[i
], ls
->div
->row
[i
], ls
->div
->n_col
);
326 if (isl_basic_map_add_div_constraints(bmap
, i
) < 0)
330 isl_local_space_free(ls
);
333 isl_local_space_free(ls
);
334 isl_basic_map_free(bmap
);
338 __isl_give isl_basic_set
*isl_basic_set_from_local_space(
339 __isl_take isl_local_space
*ls
)
341 return isl_basic_map_from_local_space(ls
);
344 __isl_give isl_space
*isl_map_get_space(__isl_keep isl_map
*map
)
348 return isl_space_copy(map
->dim
);
351 __isl_give isl_space
*isl_set_get_space(__isl_keep isl_set
*set
)
355 return isl_space_copy(set
->dim
);
358 __isl_give isl_basic_map
*isl_basic_map_set_tuple_name(
359 __isl_take isl_basic_map
*bmap
, enum isl_dim_type type
, const char *s
)
361 bmap
= isl_basic_map_cow(bmap
);
364 bmap
->dim
= isl_space_set_tuple_name(bmap
->dim
, type
, s
);
367 bmap
= isl_basic_map_finalize(bmap
);
370 isl_basic_map_free(bmap
);
374 __isl_give isl_basic_set
*isl_basic_set_set_tuple_name(
375 __isl_take isl_basic_set
*bset
, const char *s
)
377 return isl_basic_map_set_tuple_name(bset
, isl_dim_set
, s
);
380 const char *isl_basic_map_get_tuple_name(__isl_keep isl_basic_map
*bmap
,
381 enum isl_dim_type type
)
383 return bmap
? isl_space_get_tuple_name(bmap
->dim
, type
) : NULL
;
386 __isl_give isl_map
*isl_map_set_tuple_name(__isl_take isl_map
*map
,
387 enum isl_dim_type type
, const char *s
)
391 map
= isl_map_cow(map
);
395 map
->dim
= isl_space_set_tuple_name(map
->dim
, type
, s
);
399 for (i
= 0; i
< map
->n
; ++i
) {
400 map
->p
[i
] = isl_basic_map_set_tuple_name(map
->p
[i
], type
, s
);
411 /* Does the input or output tuple have a name?
413 int isl_map_has_tuple_name(__isl_keep isl_map
*map
, enum isl_dim_type type
)
415 return map
? isl_space_has_tuple_name(map
->dim
, type
) : -1;
418 const char *isl_map_get_tuple_name(__isl_keep isl_map
*map
,
419 enum isl_dim_type type
)
421 return map
? isl_space_get_tuple_name(map
->dim
, type
) : NULL
;
424 __isl_give isl_set
*isl_set_set_tuple_name(__isl_take isl_set
*set
,
427 return (isl_set
*)isl_map_set_tuple_name((isl_map
*)set
, isl_dim_set
, s
);
430 __isl_give isl_map
*isl_map_set_tuple_id(__isl_take isl_map
*map
,
431 enum isl_dim_type type
, __isl_take isl_id
*id
)
433 map
= isl_map_cow(map
);
435 return isl_id_free(id
);
437 map
->dim
= isl_space_set_tuple_id(map
->dim
, type
, id
);
439 return isl_map_reset_space(map
, isl_space_copy(map
->dim
));
442 __isl_give isl_set
*isl_set_set_tuple_id(__isl_take isl_set
*set
,
443 __isl_take isl_id
*id
)
445 return isl_map_set_tuple_id(set
, isl_dim_set
, id
);
448 __isl_give isl_map
*isl_map_reset_tuple_id(__isl_take isl_map
*map
,
449 enum isl_dim_type type
)
451 map
= isl_map_cow(map
);
455 map
->dim
= isl_space_reset_tuple_id(map
->dim
, type
);
457 return isl_map_reset_space(map
, isl_space_copy(map
->dim
));
460 __isl_give isl_set
*isl_set_reset_tuple_id(__isl_take isl_set
*set
)
462 return isl_map_reset_tuple_id(set
, isl_dim_set
);
465 int isl_map_has_tuple_id(__isl_keep isl_map
*map
, enum isl_dim_type type
)
467 return map
? isl_space_has_tuple_id(map
->dim
, type
) : -1;
470 __isl_give isl_id
*isl_map_get_tuple_id(__isl_keep isl_map
*map
,
471 enum isl_dim_type type
)
473 return map
? isl_space_get_tuple_id(map
->dim
, type
) : NULL
;
476 int isl_set_has_tuple_id(__isl_keep isl_set
*set
)
478 return isl_map_has_tuple_id(set
, isl_dim_set
);
481 __isl_give isl_id
*isl_set_get_tuple_id(__isl_keep isl_set
*set
)
483 return isl_map_get_tuple_id(set
, isl_dim_set
);
486 /* Does the set tuple have a name?
488 int isl_set_has_tuple_name(__isl_keep isl_set
*set
)
490 return set
? isl_space_has_tuple_name(set
->dim
, isl_dim_set
) : -1;
494 const char *isl_basic_set_get_tuple_name(__isl_keep isl_basic_set
*bset
)
496 return bset
? isl_space_get_tuple_name(bset
->dim
, isl_dim_set
) : NULL
;
499 const char *isl_set_get_tuple_name(__isl_keep isl_set
*set
)
501 return set
? isl_space_get_tuple_name(set
->dim
, isl_dim_set
) : NULL
;
504 const char *isl_basic_map_get_dim_name(__isl_keep isl_basic_map
*bmap
,
505 enum isl_dim_type type
, unsigned pos
)
507 return bmap
? isl_space_get_dim_name(bmap
->dim
, type
, pos
) : NULL
;
510 const char *isl_basic_set_get_dim_name(__isl_keep isl_basic_set
*bset
,
511 enum isl_dim_type type
, unsigned pos
)
513 return bset
? isl_space_get_dim_name(bset
->dim
, type
, pos
) : NULL
;
516 /* Does the given dimension have a name?
518 int isl_map_has_dim_name(__isl_keep isl_map
*map
,
519 enum isl_dim_type type
, unsigned pos
)
521 return map
? isl_space_has_dim_name(map
->dim
, type
, pos
) : -1;
524 const char *isl_map_get_dim_name(__isl_keep isl_map
*map
,
525 enum isl_dim_type type
, unsigned pos
)
527 return map
? isl_space_get_dim_name(map
->dim
, type
, pos
) : NULL
;
530 const char *isl_set_get_dim_name(__isl_keep isl_set
*set
,
531 enum isl_dim_type type
, unsigned pos
)
533 return set
? isl_space_get_dim_name(set
->dim
, type
, pos
) : NULL
;
536 /* Does the given dimension have a name?
538 int isl_set_has_dim_name(__isl_keep isl_set
*set
,
539 enum isl_dim_type type
, unsigned pos
)
541 return set
? isl_space_has_dim_name(set
->dim
, type
, pos
) : -1;
544 __isl_give isl_basic_map
*isl_basic_map_set_dim_name(
545 __isl_take isl_basic_map
*bmap
,
546 enum isl_dim_type type
, unsigned pos
, const char *s
)
548 bmap
= isl_basic_map_cow(bmap
);
551 bmap
->dim
= isl_space_set_dim_name(bmap
->dim
, type
, pos
, s
);
554 return isl_basic_map_finalize(bmap
);
556 isl_basic_map_free(bmap
);
560 __isl_give isl_map
*isl_map_set_dim_name(__isl_take isl_map
*map
,
561 enum isl_dim_type type
, unsigned pos
, const char *s
)
565 map
= isl_map_cow(map
);
569 map
->dim
= isl_space_set_dim_name(map
->dim
, type
, pos
, s
);
573 for (i
= 0; i
< map
->n
; ++i
) {
574 map
->p
[i
] = isl_basic_map_set_dim_name(map
->p
[i
], type
, pos
, s
);
585 __isl_give isl_basic_set
*isl_basic_set_set_dim_name(
586 __isl_take isl_basic_set
*bset
,
587 enum isl_dim_type type
, unsigned pos
, const char *s
)
589 return (isl_basic_set
*)isl_basic_map_set_dim_name(
590 (isl_basic_map
*)bset
, type
, pos
, s
);
593 __isl_give isl_set
*isl_set_set_dim_name(__isl_take isl_set
*set
,
594 enum isl_dim_type type
, unsigned pos
, const char *s
)
596 return (isl_set
*)isl_map_set_dim_name((isl_map
*)set
, type
, pos
, s
);
599 int isl_basic_map_has_dim_id(__isl_keep isl_basic_map
*bmap
,
600 enum isl_dim_type type
, unsigned pos
)
602 return bmap
? isl_space_has_dim_id(bmap
->dim
, type
, pos
) : -1;
605 __isl_give isl_id
*isl_basic_set_get_dim_id(__isl_keep isl_basic_set
*bset
,
606 enum isl_dim_type type
, unsigned pos
)
608 return bset
? isl_space_get_dim_id(bset
->dim
, type
, pos
) : NULL
;
611 int isl_map_has_dim_id(__isl_keep isl_map
*map
,
612 enum isl_dim_type type
, unsigned pos
)
614 return map
? isl_space_has_dim_id(map
->dim
, type
, pos
) : -1;
617 __isl_give isl_id
*isl_map_get_dim_id(__isl_keep isl_map
*map
,
618 enum isl_dim_type type
, unsigned pos
)
620 return map
? isl_space_get_dim_id(map
->dim
, type
, pos
) : NULL
;
623 int isl_set_has_dim_id(__isl_keep isl_set
*set
,
624 enum isl_dim_type type
, unsigned pos
)
626 return isl_map_has_dim_id(set
, type
, pos
);
629 __isl_give isl_id
*isl_set_get_dim_id(__isl_keep isl_set
*set
,
630 enum isl_dim_type type
, unsigned pos
)
632 return isl_map_get_dim_id(set
, type
, pos
);
635 __isl_give isl_map
*isl_map_set_dim_id(__isl_take isl_map
*map
,
636 enum isl_dim_type type
, unsigned pos
, __isl_take isl_id
*id
)
638 map
= isl_map_cow(map
);
640 return isl_id_free(id
);
642 map
->dim
= isl_space_set_dim_id(map
->dim
, type
, pos
, id
);
644 return isl_map_reset_space(map
, isl_space_copy(map
->dim
));
647 __isl_give isl_set
*isl_set_set_dim_id(__isl_take isl_set
*set
,
648 enum isl_dim_type type
, unsigned pos
, __isl_take isl_id
*id
)
650 return isl_map_set_dim_id(set
, type
, pos
, id
);
653 int isl_map_find_dim_by_id(__isl_keep isl_map
*map
, enum isl_dim_type type
,
654 __isl_keep isl_id
*id
)
658 return isl_space_find_dim_by_id(map
->dim
, type
, id
);
661 int isl_set_find_dim_by_id(__isl_keep isl_set
*set
, enum isl_dim_type type
,
662 __isl_keep isl_id
*id
)
664 return isl_map_find_dim_by_id(set
, type
, id
);
667 int isl_map_find_dim_by_name(__isl_keep isl_map
*map
, enum isl_dim_type type
,
672 return isl_space_find_dim_by_name(map
->dim
, type
, name
);
675 int isl_set_find_dim_by_name(__isl_keep isl_set
*set
, enum isl_dim_type type
,
678 return isl_map_find_dim_by_name(set
, type
, name
);
681 int isl_basic_map_is_rational(__isl_keep isl_basic_map
*bmap
)
685 return ISL_F_ISSET(bmap
, ISL_BASIC_MAP_RATIONAL
);
688 int isl_basic_set_is_rational(__isl_keep isl_basic_set
*bset
)
690 return isl_basic_map_is_rational(bset
);
693 /* Is this basic set a parameter domain?
695 int isl_basic_set_is_params(__isl_keep isl_basic_set
*bset
)
699 return isl_space_is_params(bset
->dim
);
702 /* Is this set a parameter domain?
704 int isl_set_is_params(__isl_keep isl_set
*set
)
708 return isl_space_is_params(set
->dim
);
711 /* Is this map actually a parameter domain?
712 * Users should never call this function. Outside of isl,
713 * a map can never be a parameter domain.
715 int isl_map_is_params(__isl_keep isl_map
*map
)
719 return isl_space_is_params(map
->dim
);
722 static struct isl_basic_map
*basic_map_init(struct isl_ctx
*ctx
,
723 struct isl_basic_map
*bmap
, unsigned extra
,
724 unsigned n_eq
, unsigned n_ineq
)
727 size_t row_size
= 1 + isl_space_dim(bmap
->dim
, isl_dim_all
) + extra
;
732 bmap
->block
= isl_blk_alloc(ctx
, (n_ineq
+ n_eq
) * row_size
);
733 if (isl_blk_is_error(bmap
->block
))
736 bmap
->ineq
= isl_alloc_array(ctx
, isl_int
*, n_ineq
+ n_eq
);
741 bmap
->block2
= isl_blk_empty();
744 bmap
->block2
= isl_blk_alloc(ctx
, extra
* (1 + row_size
));
745 if (isl_blk_is_error(bmap
->block2
))
748 bmap
->div
= isl_alloc_array(ctx
, isl_int
*, extra
);
753 for (i
= 0; i
< n_ineq
+ n_eq
; ++i
)
754 bmap
->ineq
[i
] = bmap
->block
.data
+ i
* row_size
;
756 for (i
= 0; i
< extra
; ++i
)
757 bmap
->div
[i
] = bmap
->block2
.data
+ i
* (1 + row_size
);
761 bmap
->c_size
= n_eq
+ n_ineq
;
762 bmap
->eq
= bmap
->ineq
+ n_ineq
;
771 isl_basic_map_free(bmap
);
775 struct isl_basic_set
*isl_basic_set_alloc(struct isl_ctx
*ctx
,
776 unsigned nparam
, unsigned dim
, unsigned extra
,
777 unsigned n_eq
, unsigned n_ineq
)
779 struct isl_basic_map
*bmap
;
782 space
= isl_space_set_alloc(ctx
, nparam
, dim
);
786 bmap
= isl_basic_map_alloc_space(space
, extra
, n_eq
, n_ineq
);
787 return (struct isl_basic_set
*)bmap
;
790 struct isl_basic_set
*isl_basic_set_alloc_space(__isl_take isl_space
*dim
,
791 unsigned extra
, unsigned n_eq
, unsigned n_ineq
)
793 struct isl_basic_map
*bmap
;
796 isl_assert(dim
->ctx
, dim
->n_in
== 0, goto error
);
797 bmap
= isl_basic_map_alloc_space(dim
, extra
, n_eq
, n_ineq
);
798 return (struct isl_basic_set
*)bmap
;
804 struct isl_basic_map
*isl_basic_map_alloc_space(__isl_take isl_space
*dim
,
805 unsigned extra
, unsigned n_eq
, unsigned n_ineq
)
807 struct isl_basic_map
*bmap
;
811 bmap
= isl_calloc_type(dim
->ctx
, struct isl_basic_map
);
816 return basic_map_init(dim
->ctx
, bmap
, extra
, n_eq
, n_ineq
);
822 struct isl_basic_map
*isl_basic_map_alloc(struct isl_ctx
*ctx
,
823 unsigned nparam
, unsigned in
, unsigned out
, unsigned extra
,
824 unsigned n_eq
, unsigned n_ineq
)
826 struct isl_basic_map
*bmap
;
829 dim
= isl_space_alloc(ctx
, nparam
, in
, out
);
833 bmap
= isl_basic_map_alloc_space(dim
, extra
, n_eq
, n_ineq
);
837 static void dup_constraints(
838 struct isl_basic_map
*dst
, struct isl_basic_map
*src
)
841 unsigned total
= isl_basic_map_total_dim(src
);
843 for (i
= 0; i
< src
->n_eq
; ++i
) {
844 int j
= isl_basic_map_alloc_equality(dst
);
845 isl_seq_cpy(dst
->eq
[j
], src
->eq
[i
], 1+total
);
848 for (i
= 0; i
< src
->n_ineq
; ++i
) {
849 int j
= isl_basic_map_alloc_inequality(dst
);
850 isl_seq_cpy(dst
->ineq
[j
], src
->ineq
[i
], 1+total
);
853 for (i
= 0; i
< src
->n_div
; ++i
) {
854 int j
= isl_basic_map_alloc_div(dst
);
855 isl_seq_cpy(dst
->div
[j
], src
->div
[i
], 1+1+total
);
857 ISL_F_SET(dst
, ISL_BASIC_SET_FINAL
);
860 struct isl_basic_map
*isl_basic_map_dup(struct isl_basic_map
*bmap
)
862 struct isl_basic_map
*dup
;
866 dup
= isl_basic_map_alloc_space(isl_space_copy(bmap
->dim
),
867 bmap
->n_div
, bmap
->n_eq
, bmap
->n_ineq
);
870 dup_constraints(dup
, bmap
);
871 dup
->flags
= bmap
->flags
;
872 dup
->sample
= isl_vec_copy(bmap
->sample
);
876 struct isl_basic_set
*isl_basic_set_dup(struct isl_basic_set
*bset
)
878 struct isl_basic_map
*dup
;
880 dup
= isl_basic_map_dup((struct isl_basic_map
*)bset
);
881 return (struct isl_basic_set
*)dup
;
884 struct isl_basic_set
*isl_basic_set_copy(struct isl_basic_set
*bset
)
889 if (ISL_F_ISSET(bset
, ISL_BASIC_SET_FINAL
)) {
893 return isl_basic_set_dup(bset
);
896 struct isl_set
*isl_set_copy(struct isl_set
*set
)
905 struct isl_basic_map
*isl_basic_map_copy(struct isl_basic_map
*bmap
)
910 if (ISL_F_ISSET(bmap
, ISL_BASIC_SET_FINAL
)) {
914 bmap
= isl_basic_map_dup(bmap
);
916 ISL_F_SET(bmap
, ISL_BASIC_SET_FINAL
);
920 struct isl_map
*isl_map_copy(struct isl_map
*map
)
929 void isl_basic_map_free(struct isl_basic_map
*bmap
)
937 isl_ctx_deref(bmap
->ctx
);
939 isl_blk_free(bmap
->ctx
, bmap
->block2
);
941 isl_blk_free(bmap
->ctx
, bmap
->block
);
942 isl_vec_free(bmap
->sample
);
943 isl_space_free(bmap
->dim
);
947 void isl_basic_set_free(struct isl_basic_set
*bset
)
949 isl_basic_map_free((struct isl_basic_map
*)bset
);
952 static int room_for_con(struct isl_basic_map
*bmap
, unsigned n
)
954 return bmap
->n_eq
+ bmap
->n_ineq
+ n
<= bmap
->c_size
;
957 __isl_give isl_map
*isl_map_align_params_map_map_and(
958 __isl_take isl_map
*map1
, __isl_take isl_map
*map2
,
959 __isl_give isl_map
*(*fn
)(__isl_take isl_map
*map1
,
960 __isl_take isl_map
*map2
))
964 if (isl_space_match(map1
->dim
, isl_dim_param
, map2
->dim
, isl_dim_param
))
965 return fn(map1
, map2
);
966 if (!isl_space_has_named_params(map1
->dim
) ||
967 !isl_space_has_named_params(map2
->dim
))
968 isl_die(map1
->ctx
, isl_error_invalid
,
969 "unaligned unnamed parameters", goto error
);
970 map1
= isl_map_align_params(map1
, isl_map_get_space(map2
));
971 map2
= isl_map_align_params(map2
, isl_map_get_space(map1
));
972 return fn(map1
, map2
);
979 int isl_map_align_params_map_map_and_test(__isl_keep isl_map
*map1
,
980 __isl_keep isl_map
*map2
,
981 int (*fn
)(__isl_keep isl_map
*map1
, __isl_keep isl_map
*map2
))
987 if (isl_space_match(map1
->dim
, isl_dim_param
, map2
->dim
, isl_dim_param
))
988 return fn(map1
, map2
);
989 if (!isl_space_has_named_params(map1
->dim
) ||
990 !isl_space_has_named_params(map2
->dim
))
991 isl_die(map1
->ctx
, isl_error_invalid
,
992 "unaligned unnamed parameters", return -1);
993 map1
= isl_map_copy(map1
);
994 map2
= isl_map_copy(map2
);
995 map1
= isl_map_align_params(map1
, isl_map_get_space(map2
));
996 map2
= isl_map_align_params(map2
, isl_map_get_space(map1
));
1003 int isl_basic_map_alloc_equality(struct isl_basic_map
*bmap
)
1005 struct isl_ctx
*ctx
;
1009 isl_assert(ctx
, room_for_con(bmap
, 1), return -1);
1010 isl_assert(ctx
, (bmap
->eq
- bmap
->ineq
) + bmap
->n_eq
<= bmap
->c_size
,
1012 ISL_F_CLR(bmap
, ISL_BASIC_MAP_NORMALIZED
);
1013 ISL_F_CLR(bmap
, ISL_BASIC_MAP_NO_REDUNDANT
);
1014 ISL_F_CLR(bmap
, ISL_BASIC_MAP_NO_IMPLICIT
);
1015 ISL_F_CLR(bmap
, ISL_BASIC_MAP_ALL_EQUALITIES
);
1016 ISL_F_CLR(bmap
, ISL_BASIC_MAP_NORMALIZED_DIVS
);
1017 if ((bmap
->eq
- bmap
->ineq
) + bmap
->n_eq
== bmap
->c_size
) {
1019 int j
= isl_basic_map_alloc_inequality(bmap
);
1023 bmap
->ineq
[j
] = bmap
->ineq
[bmap
->n_ineq
- 1];
1024 bmap
->ineq
[bmap
->n_ineq
- 1] = bmap
->eq
[-1];
1031 isl_seq_clr(bmap
->eq
[bmap
->n_eq
] + 1 + isl_basic_map_total_dim(bmap
),
1032 bmap
->extra
- bmap
->n_div
);
1033 return bmap
->n_eq
++;
1036 int isl_basic_set_alloc_equality(struct isl_basic_set
*bset
)
1038 return isl_basic_map_alloc_equality((struct isl_basic_map
*)bset
);
1041 int isl_basic_map_free_equality(struct isl_basic_map
*bmap
, unsigned n
)
1045 isl_assert(bmap
->ctx
, n
<= bmap
->n_eq
, return -1);
1050 int isl_basic_set_free_equality(struct isl_basic_set
*bset
, unsigned n
)
1052 return isl_basic_map_free_equality((struct isl_basic_map
*)bset
, n
);
1055 int isl_basic_map_drop_equality(struct isl_basic_map
*bmap
, unsigned pos
)
1060 isl_assert(bmap
->ctx
, pos
< bmap
->n_eq
, return -1);
1062 if (pos
!= bmap
->n_eq
- 1) {
1064 bmap
->eq
[pos
] = bmap
->eq
[bmap
->n_eq
- 1];
1065 bmap
->eq
[bmap
->n_eq
- 1] = t
;
1071 int isl_basic_set_drop_equality(struct isl_basic_set
*bset
, unsigned pos
)
1073 return isl_basic_map_drop_equality((struct isl_basic_map
*)bset
, pos
);
1076 void isl_basic_map_inequality_to_equality(
1077 struct isl_basic_map
*bmap
, unsigned pos
)
1081 t
= bmap
->ineq
[pos
];
1082 bmap
->ineq
[pos
] = bmap
->ineq
[bmap
->n_ineq
- 1];
1083 bmap
->ineq
[bmap
->n_ineq
- 1] = bmap
->eq
[-1];
1088 ISL_F_CLR(bmap
, ISL_BASIC_MAP_NO_REDUNDANT
);
1089 ISL_F_CLR(bmap
, ISL_BASIC_MAP_NORMALIZED
);
1090 ISL_F_CLR(bmap
, ISL_BASIC_MAP_NORMALIZED_DIVS
);
1091 ISL_F_CLR(bmap
, ISL_BASIC_MAP_ALL_EQUALITIES
);
1094 static int room_for_ineq(struct isl_basic_map
*bmap
, unsigned n
)
1096 return bmap
->n_ineq
+ n
<= bmap
->eq
- bmap
->ineq
;
1099 int isl_basic_map_alloc_inequality(struct isl_basic_map
*bmap
)
1101 struct isl_ctx
*ctx
;
1105 isl_assert(ctx
, room_for_ineq(bmap
, 1), return -1);
1106 ISL_F_CLR(bmap
, ISL_BASIC_MAP_NO_IMPLICIT
);
1107 ISL_F_CLR(bmap
, ISL_BASIC_MAP_NO_REDUNDANT
);
1108 ISL_F_CLR(bmap
, ISL_BASIC_MAP_NORMALIZED
);
1109 ISL_F_CLR(bmap
, ISL_BASIC_MAP_ALL_EQUALITIES
);
1110 isl_seq_clr(bmap
->ineq
[bmap
->n_ineq
] +
1111 1 + isl_basic_map_total_dim(bmap
),
1112 bmap
->extra
- bmap
->n_div
);
1113 return bmap
->n_ineq
++;
1116 int isl_basic_set_alloc_inequality(struct isl_basic_set
*bset
)
1118 return isl_basic_map_alloc_inequality((struct isl_basic_map
*)bset
);
1121 int isl_basic_map_free_inequality(struct isl_basic_map
*bmap
, unsigned n
)
1125 isl_assert(bmap
->ctx
, n
<= bmap
->n_ineq
, return -1);
1130 int isl_basic_set_free_inequality(struct isl_basic_set
*bset
, unsigned n
)
1132 return isl_basic_map_free_inequality((struct isl_basic_map
*)bset
, n
);
1135 int isl_basic_map_drop_inequality(struct isl_basic_map
*bmap
, unsigned pos
)
1140 isl_assert(bmap
->ctx
, pos
< bmap
->n_ineq
, return -1);
1142 if (pos
!= bmap
->n_ineq
- 1) {
1143 t
= bmap
->ineq
[pos
];
1144 bmap
->ineq
[pos
] = bmap
->ineq
[bmap
->n_ineq
- 1];
1145 bmap
->ineq
[bmap
->n_ineq
- 1] = t
;
1146 ISL_F_CLR(bmap
, ISL_BASIC_MAP_NORMALIZED
);
1152 int isl_basic_set_drop_inequality(struct isl_basic_set
*bset
, unsigned pos
)
1154 return isl_basic_map_drop_inequality((struct isl_basic_map
*)bset
, pos
);
1157 __isl_give isl_basic_map
*isl_basic_map_add_eq(__isl_take isl_basic_map
*bmap
,
1162 bmap
= isl_basic_map_extend_constraints(bmap
, 1, 0);
1165 k
= isl_basic_map_alloc_equality(bmap
);
1168 isl_seq_cpy(bmap
->eq
[k
], eq
, 1 + isl_basic_map_total_dim(bmap
));
1171 isl_basic_map_free(bmap
);
1175 __isl_give isl_basic_set
*isl_basic_set_add_eq(__isl_take isl_basic_set
*bset
,
1178 return (isl_basic_set
*)
1179 isl_basic_map_add_eq((isl_basic_map
*)bset
, eq
);
1182 __isl_give isl_basic_map
*isl_basic_map_add_ineq(__isl_take isl_basic_map
*bmap
,
1187 bmap
= isl_basic_map_extend_constraints(bmap
, 0, 1);
1190 k
= isl_basic_map_alloc_inequality(bmap
);
1193 isl_seq_cpy(bmap
->ineq
[k
], ineq
, 1 + isl_basic_map_total_dim(bmap
));
1196 isl_basic_map_free(bmap
);
1200 __isl_give isl_basic_set
*isl_basic_set_add_ineq(__isl_take isl_basic_set
*bset
,
1203 return (isl_basic_set
*)
1204 isl_basic_map_add_ineq((isl_basic_map
*)bset
, ineq
);
1207 int isl_basic_map_alloc_div(struct isl_basic_map
*bmap
)
1211 isl_assert(bmap
->ctx
, bmap
->n_div
< bmap
->extra
, return -1);
1212 isl_seq_clr(bmap
->div
[bmap
->n_div
] +
1213 1 + 1 + isl_basic_map_total_dim(bmap
),
1214 bmap
->extra
- bmap
->n_div
);
1215 ISL_F_CLR(bmap
, ISL_BASIC_MAP_NORMALIZED_DIVS
);
1216 return bmap
->n_div
++;
1219 int isl_basic_set_alloc_div(struct isl_basic_set
*bset
)
1221 return isl_basic_map_alloc_div((struct isl_basic_map
*)bset
);
1224 int isl_basic_map_free_div(struct isl_basic_map
*bmap
, unsigned n
)
1228 isl_assert(bmap
->ctx
, n
<= bmap
->n_div
, return -1);
1233 int isl_basic_set_free_div(struct isl_basic_set
*bset
, unsigned n
)
1235 return isl_basic_map_free_div((struct isl_basic_map
*)bset
, n
);
1238 /* Copy constraint from src to dst, putting the vars of src at offset
1239 * dim_off in dst and the divs of src at offset div_off in dst.
1240 * If both sets are actually map, then dim_off applies to the input
1243 static void copy_constraint(struct isl_basic_map
*dst_map
, isl_int
*dst
,
1244 struct isl_basic_map
*src_map
, isl_int
*src
,
1245 unsigned in_off
, unsigned out_off
, unsigned div_off
)
1247 unsigned src_nparam
= isl_basic_map_n_param(src_map
);
1248 unsigned dst_nparam
= isl_basic_map_n_param(dst_map
);
1249 unsigned src_in
= isl_basic_map_n_in(src_map
);
1250 unsigned dst_in
= isl_basic_map_n_in(dst_map
);
1251 unsigned src_out
= isl_basic_map_n_out(src_map
);
1252 unsigned dst_out
= isl_basic_map_n_out(dst_map
);
1253 isl_int_set(dst
[0], src
[0]);
1254 isl_seq_cpy(dst
+1, src
+1, isl_min(dst_nparam
, src_nparam
));
1255 if (dst_nparam
> src_nparam
)
1256 isl_seq_clr(dst
+1+src_nparam
,
1257 dst_nparam
- src_nparam
);
1258 isl_seq_clr(dst
+1+dst_nparam
, in_off
);
1259 isl_seq_cpy(dst
+1+dst_nparam
+in_off
,
1261 isl_min(dst_in
-in_off
, src_in
));
1262 if (dst_in
-in_off
> src_in
)
1263 isl_seq_clr(dst
+1+dst_nparam
+in_off
+src_in
,
1264 dst_in
- in_off
- src_in
);
1265 isl_seq_clr(dst
+1+dst_nparam
+dst_in
, out_off
);
1266 isl_seq_cpy(dst
+1+dst_nparam
+dst_in
+out_off
,
1267 src
+1+src_nparam
+src_in
,
1268 isl_min(dst_out
-out_off
, src_out
));
1269 if (dst_out
-out_off
> src_out
)
1270 isl_seq_clr(dst
+1+dst_nparam
+dst_in
+out_off
+src_out
,
1271 dst_out
- out_off
- src_out
);
1272 isl_seq_clr(dst
+1+dst_nparam
+dst_in
+dst_out
, div_off
);
1273 isl_seq_cpy(dst
+1+dst_nparam
+dst_in
+dst_out
+div_off
,
1274 src
+1+src_nparam
+src_in
+src_out
,
1275 isl_min(dst_map
->extra
-div_off
, src_map
->n_div
));
1276 if (dst_map
->n_div
-div_off
> src_map
->n_div
)
1277 isl_seq_clr(dst
+1+dst_nparam
+dst_in
+dst_out
+
1278 div_off
+src_map
->n_div
,
1279 dst_map
->n_div
- div_off
- src_map
->n_div
);
1282 static void copy_div(struct isl_basic_map
*dst_map
, isl_int
*dst
,
1283 struct isl_basic_map
*src_map
, isl_int
*src
,
1284 unsigned in_off
, unsigned out_off
, unsigned div_off
)
1286 isl_int_set(dst
[0], src
[0]);
1287 copy_constraint(dst_map
, dst
+1, src_map
, src
+1, in_off
, out_off
, div_off
);
1290 static struct isl_basic_map
*add_constraints(struct isl_basic_map
*bmap1
,
1291 struct isl_basic_map
*bmap2
, unsigned i_pos
, unsigned o_pos
)
1296 if (!bmap1
|| !bmap2
)
1299 div_off
= bmap1
->n_div
;
1301 for (i
= 0; i
< bmap2
->n_eq
; ++i
) {
1302 int i1
= isl_basic_map_alloc_equality(bmap1
);
1305 copy_constraint(bmap1
, bmap1
->eq
[i1
], bmap2
, bmap2
->eq
[i
],
1306 i_pos
, o_pos
, div_off
);
1309 for (i
= 0; i
< bmap2
->n_ineq
; ++i
) {
1310 int i1
= isl_basic_map_alloc_inequality(bmap1
);
1313 copy_constraint(bmap1
, bmap1
->ineq
[i1
], bmap2
, bmap2
->ineq
[i
],
1314 i_pos
, o_pos
, div_off
);
1317 for (i
= 0; i
< bmap2
->n_div
; ++i
) {
1318 int i1
= isl_basic_map_alloc_div(bmap1
);
1321 copy_div(bmap1
, bmap1
->div
[i1
], bmap2
, bmap2
->div
[i
],
1322 i_pos
, o_pos
, div_off
);
1325 isl_basic_map_free(bmap2
);
1330 isl_basic_map_free(bmap1
);
1331 isl_basic_map_free(bmap2
);
1335 struct isl_basic_set
*isl_basic_set_add_constraints(struct isl_basic_set
*bset1
,
1336 struct isl_basic_set
*bset2
, unsigned pos
)
1338 return (struct isl_basic_set
*)
1339 add_constraints((struct isl_basic_map
*)bset1
,
1340 (struct isl_basic_map
*)bset2
, 0, pos
);
1343 struct isl_basic_map
*isl_basic_map_extend_space(struct isl_basic_map
*base
,
1344 __isl_take isl_space
*dim
, unsigned extra
,
1345 unsigned n_eq
, unsigned n_ineq
)
1347 struct isl_basic_map
*ext
;
1357 dims_ok
= isl_space_is_equal(base
->dim
, dim
) &&
1358 base
->extra
>= base
->n_div
+ extra
;
1360 if (dims_ok
&& room_for_con(base
, n_eq
+ n_ineq
) &&
1361 room_for_ineq(base
, n_ineq
)) {
1362 isl_space_free(dim
);
1366 isl_assert(base
->ctx
, base
->dim
->nparam
<= dim
->nparam
, goto error
);
1367 isl_assert(base
->ctx
, base
->dim
->n_in
<= dim
->n_in
, goto error
);
1368 isl_assert(base
->ctx
, base
->dim
->n_out
<= dim
->n_out
, goto error
);
1369 extra
+= base
->extra
;
1371 n_ineq
+= base
->n_ineq
;
1373 ext
= isl_basic_map_alloc_space(dim
, extra
, n_eq
, n_ineq
);
1379 ext
->sample
= isl_vec_copy(base
->sample
);
1380 flags
= base
->flags
;
1381 ext
= add_constraints(ext
, base
, 0, 0);
1384 ISL_F_CLR(ext
, ISL_BASIC_SET_FINAL
);
1390 isl_space_free(dim
);
1391 isl_basic_map_free(base
);
1395 struct isl_basic_set
*isl_basic_set_extend_space(struct isl_basic_set
*base
,
1396 __isl_take isl_space
*dim
, unsigned extra
,
1397 unsigned n_eq
, unsigned n_ineq
)
1399 return (struct isl_basic_set
*)
1400 isl_basic_map_extend_space((struct isl_basic_map
*)base
, dim
,
1401 extra
, n_eq
, n_ineq
);
1404 struct isl_basic_map
*isl_basic_map_extend_constraints(
1405 struct isl_basic_map
*base
, unsigned n_eq
, unsigned n_ineq
)
1409 return isl_basic_map_extend_space(base
, isl_space_copy(base
->dim
),
1413 struct isl_basic_map
*isl_basic_map_extend(struct isl_basic_map
*base
,
1414 unsigned nparam
, unsigned n_in
, unsigned n_out
, unsigned extra
,
1415 unsigned n_eq
, unsigned n_ineq
)
1417 struct isl_basic_map
*bmap
;
1422 dim
= isl_space_alloc(base
->ctx
, nparam
, n_in
, n_out
);
1426 bmap
= isl_basic_map_extend_space(base
, dim
, extra
, n_eq
, n_ineq
);
1429 isl_basic_map_free(base
);
1433 struct isl_basic_set
*isl_basic_set_extend(struct isl_basic_set
*base
,
1434 unsigned nparam
, unsigned dim
, unsigned extra
,
1435 unsigned n_eq
, unsigned n_ineq
)
1437 return (struct isl_basic_set
*)
1438 isl_basic_map_extend((struct isl_basic_map
*)base
,
1439 nparam
, 0, dim
, extra
, n_eq
, n_ineq
);
1442 struct isl_basic_set
*isl_basic_set_extend_constraints(
1443 struct isl_basic_set
*base
, unsigned n_eq
, unsigned n_ineq
)
1445 return (struct isl_basic_set
*)
1446 isl_basic_map_extend_constraints((struct isl_basic_map
*)base
,
1450 struct isl_basic_set
*isl_basic_set_cow(struct isl_basic_set
*bset
)
1452 return (struct isl_basic_set
*)
1453 isl_basic_map_cow((struct isl_basic_map
*)bset
);
1456 struct isl_basic_map
*isl_basic_map_cow(struct isl_basic_map
*bmap
)
1461 if (bmap
->ref
> 1) {
1463 bmap
= isl_basic_map_dup(bmap
);
1466 ISL_F_CLR(bmap
, ISL_BASIC_SET_FINAL
);
1470 struct isl_set
*isl_set_cow(struct isl_set
*set
)
1478 return isl_set_dup(set
);
1481 struct isl_map
*isl_map_cow(struct isl_map
*map
)
1489 return isl_map_dup(map
);
1492 static void swap_vars(struct isl_blk blk
, isl_int
*a
,
1493 unsigned a_len
, unsigned b_len
)
1495 isl_seq_cpy(blk
.data
, a
+a_len
, b_len
);
1496 isl_seq_cpy(blk
.data
+b_len
, a
, a_len
);
1497 isl_seq_cpy(a
, blk
.data
, b_len
+a_len
);
1500 static __isl_give isl_basic_map
*isl_basic_map_swap_vars(
1501 __isl_take isl_basic_map
*bmap
, unsigned pos
, unsigned n1
, unsigned n2
)
1509 isl_assert(bmap
->ctx
,
1510 pos
+ n1
+ n2
<= 1 + isl_basic_map_total_dim(bmap
), goto error
);
1512 if (n1
== 0 || n2
== 0)
1515 bmap
= isl_basic_map_cow(bmap
);
1519 blk
= isl_blk_alloc(bmap
->ctx
, n1
+ n2
);
1520 if (isl_blk_is_error(blk
))
1523 for (i
= 0; i
< bmap
->n_eq
; ++i
)
1525 bmap
->eq
[i
] + pos
, n1
, n2
);
1527 for (i
= 0; i
< bmap
->n_ineq
; ++i
)
1529 bmap
->ineq
[i
] + pos
, n1
, n2
);
1531 for (i
= 0; i
< bmap
->n_div
; ++i
)
1533 bmap
->div
[i
]+1 + pos
, n1
, n2
);
1535 isl_blk_free(bmap
->ctx
, blk
);
1537 ISL_F_CLR(bmap
, ISL_BASIC_SET_NORMALIZED
);
1538 bmap
= isl_basic_map_gauss(bmap
, NULL
);
1539 return isl_basic_map_finalize(bmap
);
1541 isl_basic_map_free(bmap
);
1545 static __isl_give isl_basic_set
*isl_basic_set_swap_vars(
1546 __isl_take isl_basic_set
*bset
, unsigned n
)
1551 nparam
= isl_basic_set_n_param(bset
);
1552 dim
= isl_basic_set_n_dim(bset
);
1553 isl_assert(bset
->ctx
, n
<= dim
, goto error
);
1555 return isl_basic_map_swap_vars(bset
, 1 + nparam
, n
, dim
- n
);
1557 isl_basic_set_free(bset
);
1561 struct isl_basic_map
*isl_basic_map_set_to_empty(struct isl_basic_map
*bmap
)
1567 total
= isl_basic_map_total_dim(bmap
);
1568 isl_basic_map_free_div(bmap
, bmap
->n_div
);
1569 isl_basic_map_free_inequality(bmap
, bmap
->n_ineq
);
1571 isl_basic_map_free_equality(bmap
, bmap
->n_eq
-1);
1573 i
= isl_basic_map_alloc_equality(bmap
);
1577 isl_int_set_si(bmap
->eq
[i
][0], 1);
1578 isl_seq_clr(bmap
->eq
[i
]+1, total
);
1579 ISL_F_SET(bmap
, ISL_BASIC_MAP_EMPTY
);
1580 isl_vec_free(bmap
->sample
);
1581 bmap
->sample
= NULL
;
1582 return isl_basic_map_finalize(bmap
);
1584 isl_basic_map_free(bmap
);
1588 struct isl_basic_set
*isl_basic_set_set_to_empty(struct isl_basic_set
*bset
)
1590 return (struct isl_basic_set
*)
1591 isl_basic_map_set_to_empty((struct isl_basic_map
*)bset
);
1594 void isl_basic_map_swap_div(struct isl_basic_map
*bmap
, int a
, int b
)
1597 unsigned off
= isl_space_dim(bmap
->dim
, isl_dim_all
);
1598 isl_int
*t
= bmap
->div
[a
];
1599 bmap
->div
[a
] = bmap
->div
[b
];
1602 for (i
= 0; i
< bmap
->n_eq
; ++i
)
1603 isl_int_swap(bmap
->eq
[i
][1+off
+a
], bmap
->eq
[i
][1+off
+b
]);
1605 for (i
= 0; i
< bmap
->n_ineq
; ++i
)
1606 isl_int_swap(bmap
->ineq
[i
][1+off
+a
], bmap
->ineq
[i
][1+off
+b
]);
1608 for (i
= 0; i
< bmap
->n_div
; ++i
)
1609 isl_int_swap(bmap
->div
[i
][1+1+off
+a
], bmap
->div
[i
][1+1+off
+b
]);
1610 ISL_F_CLR(bmap
, ISL_BASIC_MAP_NORMALIZED
);
1613 /* Eliminate the specified n dimensions starting at first from the
1614 * constraints, without removing the dimensions from the space.
1615 * If the set is rational, the dimensions are eliminated using Fourier-Motzkin.
1617 __isl_give isl_map
*isl_map_eliminate(__isl_take isl_map
*map
,
1618 enum isl_dim_type type
, unsigned first
, unsigned n
)
1627 if (first
+ n
> isl_map_dim(map
, type
) || first
+ n
< first
)
1628 isl_die(map
->ctx
, isl_error_invalid
,
1629 "index out of bounds", goto error
);
1631 map
= isl_map_cow(map
);
1635 for (i
= 0; i
< map
->n
; ++i
) {
1636 map
->p
[i
] = isl_basic_map_eliminate(map
->p
[i
], type
, first
, n
);
1646 /* Eliminate the specified n dimensions starting at first from the
1647 * constraints, without removing the dimensions from the space.
1648 * If the set is rational, the dimensions are eliminated using Fourier-Motzkin.
1650 __isl_give isl_set
*isl_set_eliminate(__isl_take isl_set
*set
,
1651 enum isl_dim_type type
, unsigned first
, unsigned n
)
1653 return (isl_set
*)isl_map_eliminate((isl_map
*)set
, type
, first
, n
);
1656 /* Eliminate the specified n dimensions starting at first from the
1657 * constraints, without removing the dimensions from the space.
1658 * If the set is rational, the dimensions are eliminated using Fourier-Motzkin.
1660 __isl_give isl_set
*isl_set_eliminate_dims(__isl_take isl_set
*set
,
1661 unsigned first
, unsigned n
)
1663 return isl_set_eliminate(set
, isl_dim_set
, first
, n
);
1666 __isl_give isl_basic_map
*isl_basic_map_remove_divs(
1667 __isl_take isl_basic_map
*bmap
)
1671 bmap
= isl_basic_map_eliminate_vars(bmap
,
1672 isl_space_dim(bmap
->dim
, isl_dim_all
), bmap
->n_div
);
1676 return isl_basic_map_finalize(bmap
);
1679 __isl_give isl_basic_set
*isl_basic_set_remove_divs(
1680 __isl_take isl_basic_set
*bset
)
1682 return (struct isl_basic_set
*)isl_basic_map_remove_divs(
1683 (struct isl_basic_map
*)bset
);
1686 __isl_give isl_map
*isl_map_remove_divs(__isl_take isl_map
*map
)
1695 map
= isl_map_cow(map
);
1699 for (i
= 0; i
< map
->n
; ++i
) {
1700 map
->p
[i
] = isl_basic_map_remove_divs(map
->p
[i
]);
1710 __isl_give isl_set
*isl_set_remove_divs(__isl_take isl_set
*set
)
1712 return isl_map_remove_divs(set
);
1715 struct isl_basic_map
*isl_basic_map_remove_dims(struct isl_basic_map
*bmap
,
1716 enum isl_dim_type type
, unsigned first
, unsigned n
)
1720 isl_assert(bmap
->ctx
, first
+ n
<= isl_basic_map_dim(bmap
, type
),
1722 if (n
== 0 && !isl_space_is_named_or_nested(bmap
->dim
, type
))
1724 bmap
= isl_basic_map_eliminate_vars(bmap
,
1725 isl_basic_map_offset(bmap
, type
) - 1 + first
, n
);
1728 if (ISL_F_ISSET(bmap
, ISL_BASIC_MAP_EMPTY
) && type
== isl_dim_div
)
1730 bmap
= isl_basic_map_drop(bmap
, type
, first
, n
);
1733 isl_basic_map_free(bmap
);
1737 /* Return true if the definition of the given div (recursively) involves
1738 * any of the given variables.
1740 static int div_involves_vars(__isl_keep isl_basic_map
*bmap
, int div
,
1741 unsigned first
, unsigned n
)
1744 unsigned div_offset
= isl_basic_map_offset(bmap
, isl_dim_div
);
1746 if (isl_int_is_zero(bmap
->div
[div
][0]))
1748 if (isl_seq_first_non_zero(bmap
->div
[div
] + 1 + first
, n
) >= 0)
1751 for (i
= bmap
->n_div
- 1; i
>= 0; --i
) {
1752 if (isl_int_is_zero(bmap
->div
[div
][1 + div_offset
+ i
]))
1754 if (div_involves_vars(bmap
, i
, first
, n
))
1761 /* Try and add a lower and/or upper bound on "div" to "bmap"
1762 * based on inequality "i".
1763 * "total" is the total number of variables (excluding the divs).
1764 * "v" is a temporary object that can be used during the calculations.
1765 * If "lb" is set, then a lower bound should be constructed.
1766 * If "ub" is set, then an upper bound should be constructed.
1768 * The calling function has already checked that the inequality does not
1769 * reference "div", but we still need to check that the inequality is
1770 * of the right form. We'll consider the case where we want to construct
1771 * a lower bound. The construction of upper bounds is similar.
1773 * Let "div" be of the form
1775 * q = floor((a + f(x))/d)
1777 * We essentially check if constraint "i" is of the form
1781 * so that we can use it to derive a lower bound on "div".
1782 * However, we allow a slightly more general form
1786 * with the condition that the coefficients of g(x) - f(x) are all
1788 * Rewriting this constraint as
1792 * adding a + f(x) to both sides and dividing by d, we obtain
1794 * (a + f(x))/d >= (a-b)/d + (f(x)-g(x))/d
1796 * Taking the floor on both sides, we obtain
1798 * q >= floor((a-b)/d) + (f(x)-g(x))/d
1802 * (g(x)-f(x))/d + ceil((b-a)/d) + q >= 0
1804 * In the case of an upper bound, we construct the constraint
1806 * (g(x)+f(x))/d + floor((b+a)/d) - q >= 0
1809 static __isl_give isl_basic_map
*insert_bounds_on_div_from_ineq(
1810 __isl_take isl_basic_map
*bmap
, int div
, int i
,
1811 unsigned total
, isl_int v
, int lb
, int ub
)
1815 for (j
= 0; (lb
|| ub
) && j
< total
+ bmap
->n_div
; ++j
) {
1817 isl_int_sub(v
, bmap
->ineq
[i
][1 + j
],
1818 bmap
->div
[div
][1 + 1 + j
]);
1819 lb
= isl_int_is_divisible_by(v
, bmap
->div
[div
][0]);
1822 isl_int_add(v
, bmap
->ineq
[i
][1 + j
],
1823 bmap
->div
[div
][1 + 1 + j
]);
1824 ub
= isl_int_is_divisible_by(v
, bmap
->div
[div
][0]);
1830 bmap
= isl_basic_map_extend_constraints(bmap
, 0, lb
+ ub
);
1832 int k
= isl_basic_map_alloc_inequality(bmap
);
1835 for (j
= 0; j
< 1 + total
+ bmap
->n_div
; ++j
) {
1836 isl_int_sub(bmap
->ineq
[k
][j
], bmap
->ineq
[i
][j
],
1837 bmap
->div
[div
][1 + j
]);
1838 isl_int_cdiv_q(bmap
->ineq
[k
][j
],
1839 bmap
->ineq
[k
][j
], bmap
->div
[div
][0]);
1841 isl_int_set_si(bmap
->ineq
[k
][1 + total
+ div
], 1);
1844 int k
= isl_basic_map_alloc_inequality(bmap
);
1847 for (j
= 0; j
< 1 + total
+ bmap
->n_div
; ++j
) {
1848 isl_int_add(bmap
->ineq
[k
][j
], bmap
->ineq
[i
][j
],
1849 bmap
->div
[div
][1 + j
]);
1850 isl_int_fdiv_q(bmap
->ineq
[k
][j
],
1851 bmap
->ineq
[k
][j
], bmap
->div
[div
][0]);
1853 isl_int_set_si(bmap
->ineq
[k
][1 + total
+ div
], -1);
1858 isl_basic_map_free(bmap
);
1862 /* This function is called right before "div" is eliminated from "bmap"
1863 * using Fourier-Motzkin.
1864 * Look through the constraints of "bmap" for constraints on the argument
1865 * of the integer division and use them to construct constraints on the
1866 * integer division itself. These constraints can then be combined
1867 * during the Fourier-Motzkin elimination.
1868 * Note that it is only useful to introduce lower bounds on "div"
1869 * if "bmap" already contains upper bounds on "div" as the newly
1870 * introduce lower bounds can then be combined with the pre-existing
1871 * upper bounds. Similarly for upper bounds.
1872 * We therefore first check if "bmap" contains any lower and/or upper bounds
1875 * It is interesting to note that the introduction of these constraints
1876 * can indeed lead to more accurate results, even when compared to
1877 * deriving constraints on the argument of "div" from constraints on "div".
1878 * Consider, for example, the set
1880 * { [i,j,k] : 3 + i + 2j >= 0 and 2 * [(i+2j)/4] <= k }
1882 * The second constraint can be rewritten as
1884 * 2 * [(-i-2j+3)/4] + k >= 0
1886 * from which we can derive
1888 * -i - 2j + 3 >= -2k
1894 * Combined with the first constraint, we obtain
1896 * -3 <= 3 + 2k or k >= -3
1898 * If, on the other hand we derive a constraint on [(i+2j)/4] from
1899 * the first constraint, we obtain
1901 * [(i + 2j)/4] >= [-3/4] = -1
1903 * Combining this constraint with the second constraint, we obtain
1907 static __isl_give isl_basic_map
*insert_bounds_on_div(
1908 __isl_take isl_basic_map
*bmap
, int div
)
1911 int check_lb
, check_ub
;
1918 if (isl_int_is_zero(bmap
->div
[div
][0]))
1921 total
= isl_space_dim(bmap
->dim
, isl_dim_all
);
1925 for (i
= 0; (!check_lb
|| !check_ub
) && i
< bmap
->n_ineq
; ++i
) {
1926 int s
= isl_int_sgn(bmap
->ineq
[i
][1 + total
+ div
]);
1933 if (!check_lb
&& !check_ub
)
1938 for (i
= 0; bmap
&& i
< bmap
->n_ineq
; ++i
) {
1939 if (!isl_int_is_zero(bmap
->ineq
[i
][1 + total
+ div
]))
1942 bmap
= insert_bounds_on_div_from_ineq(bmap
, div
, i
, total
, v
,
1943 check_lb
, check_ub
);
1951 /* Remove all divs (recursively) involving any of the given dimensions
1952 * in their definitions.
1954 __isl_give isl_basic_map
*isl_basic_map_remove_divs_involving_dims(
1955 __isl_take isl_basic_map
*bmap
,
1956 enum isl_dim_type type
, unsigned first
, unsigned n
)
1962 isl_assert(bmap
->ctx
, first
+ n
<= isl_basic_map_dim(bmap
, type
),
1964 first
+= isl_basic_map_offset(bmap
, type
);
1966 for (i
= bmap
->n_div
- 1; i
>= 0; --i
) {
1967 if (!div_involves_vars(bmap
, i
, first
, n
))
1969 bmap
= insert_bounds_on_div(bmap
, i
);
1970 bmap
= isl_basic_map_remove_dims(bmap
, isl_dim_div
, i
, 1);
1978 isl_basic_map_free(bmap
);
1982 __isl_give isl_basic_set
*isl_basic_set_remove_divs_involving_dims(
1983 __isl_take isl_basic_set
*bset
,
1984 enum isl_dim_type type
, unsigned first
, unsigned n
)
1986 return isl_basic_map_remove_divs_involving_dims(bset
, type
, first
, n
);
1989 __isl_give isl_map
*isl_map_remove_divs_involving_dims(__isl_take isl_map
*map
,
1990 enum isl_dim_type type
, unsigned first
, unsigned n
)
1999 map
= isl_map_cow(map
);
2003 for (i
= 0; i
< map
->n
; ++i
) {
2004 map
->p
[i
] = isl_basic_map_remove_divs_involving_dims(map
->p
[i
],
2015 __isl_give isl_set
*isl_set_remove_divs_involving_dims(__isl_take isl_set
*set
,
2016 enum isl_dim_type type
, unsigned first
, unsigned n
)
2018 return (isl_set
*)isl_map_remove_divs_involving_dims((isl_map
*)set
,
2022 /* Does the desciption of "bmap" depend on the specified dimensions?
2023 * We also check whether the dimensions appear in any of the div definitions.
2024 * In principle there is no need for this check. If the dimensions appear
2025 * in a div definition, they also appear in the defining constraints of that
2028 int isl_basic_map_involves_dims(__isl_keep isl_basic_map
*bmap
,
2029 enum isl_dim_type type
, unsigned first
, unsigned n
)
2036 if (first
+ n
> isl_basic_map_dim(bmap
, type
))
2037 isl_die(bmap
->ctx
, isl_error_invalid
,
2038 "index out of bounds", return -1);
2040 first
+= isl_basic_map_offset(bmap
, type
);
2041 for (i
= 0; i
< bmap
->n_eq
; ++i
)
2042 if (isl_seq_first_non_zero(bmap
->eq
[i
] + first
, n
) >= 0)
2044 for (i
= 0; i
< bmap
->n_ineq
; ++i
)
2045 if (isl_seq_first_non_zero(bmap
->ineq
[i
] + first
, n
) >= 0)
2047 for (i
= 0; i
< bmap
->n_div
; ++i
) {
2048 if (isl_int_is_zero(bmap
->div
[i
][0]))
2050 if (isl_seq_first_non_zero(bmap
->div
[i
] + 1 + first
, n
) >= 0)
2057 int isl_map_involves_dims(__isl_keep isl_map
*map
,
2058 enum isl_dim_type type
, unsigned first
, unsigned n
)
2065 if (first
+ n
> isl_map_dim(map
, type
))
2066 isl_die(map
->ctx
, isl_error_invalid
,
2067 "index out of bounds", return -1);
2069 for (i
= 0; i
< map
->n
; ++i
) {
2070 int involves
= isl_basic_map_involves_dims(map
->p
[i
],
2072 if (involves
< 0 || involves
)
2079 int isl_basic_set_involves_dims(__isl_keep isl_basic_set
*bset
,
2080 enum isl_dim_type type
, unsigned first
, unsigned n
)
2082 return isl_basic_map_involves_dims(bset
, type
, first
, n
);
2085 int isl_set_involves_dims(__isl_keep isl_set
*set
,
2086 enum isl_dim_type type
, unsigned first
, unsigned n
)
2088 return isl_map_involves_dims(set
, type
, first
, n
);
2091 /* Return true if the definition of the given div is unknown or depends
2094 static int div_is_unknown(__isl_keep isl_basic_map
*bmap
, int div
)
2097 unsigned div_offset
= isl_basic_map_offset(bmap
, isl_dim_div
);
2099 if (isl_int_is_zero(bmap
->div
[div
][0]))
2102 for (i
= bmap
->n_div
- 1; i
>= 0; --i
) {
2103 if (isl_int_is_zero(bmap
->div
[div
][1 + div_offset
+ i
]))
2105 if (div_is_unknown(bmap
, i
))
2112 /* Remove all divs that are unknown or defined in terms of unknown divs.
2114 __isl_give isl_basic_map
*isl_basic_map_remove_unknown_divs(
2115 __isl_take isl_basic_map
*bmap
)
2122 for (i
= bmap
->n_div
- 1; i
>= 0; --i
) {
2123 if (!div_is_unknown(bmap
, i
))
2125 bmap
= isl_basic_map_remove_dims(bmap
, isl_dim_div
, i
, 1);
2134 __isl_give isl_map
*isl_map_remove_unknown_divs(__isl_take isl_map
*map
)
2143 map
= isl_map_cow(map
);
2147 for (i
= 0; i
< map
->n
; ++i
) {
2148 map
->p
[i
] = isl_basic_map_remove_unknown_divs(map
->p
[i
]);
2158 __isl_give isl_set
*isl_set_remove_unknown_divs(__isl_take isl_set
*set
)
2160 return (isl_set
*)isl_map_remove_unknown_divs((isl_map
*)set
);
2163 __isl_give isl_basic_set
*isl_basic_set_remove_dims(
2164 __isl_take isl_basic_set
*bset
,
2165 enum isl_dim_type type
, unsigned first
, unsigned n
)
2167 return (isl_basic_set
*)
2168 isl_basic_map_remove_dims((isl_basic_map
*)bset
, type
, first
, n
);
2171 struct isl_map
*isl_map_remove_dims(struct isl_map
*map
,
2172 enum isl_dim_type type
, unsigned first
, unsigned n
)
2179 map
= isl_map_cow(map
);
2182 isl_assert(map
->ctx
, first
+ n
<= isl_map_dim(map
, type
), goto error
);
2184 for (i
= 0; i
< map
->n
; ++i
) {
2185 map
->p
[i
] = isl_basic_map_eliminate_vars(map
->p
[i
],
2186 isl_basic_map_offset(map
->p
[i
], type
) - 1 + first
, n
);
2190 map
= isl_map_drop(map
, type
, first
, n
);
2197 __isl_give isl_set
*isl_set_remove_dims(__isl_take isl_set
*bset
,
2198 enum isl_dim_type type
, unsigned first
, unsigned n
)
2200 return (isl_set
*)isl_map_remove_dims((isl_map
*)bset
, type
, first
, n
);
2203 /* Project out n inputs starting at first using Fourier-Motzkin */
2204 struct isl_map
*isl_map_remove_inputs(struct isl_map
*map
,
2205 unsigned first
, unsigned n
)
2207 return isl_map_remove_dims(map
, isl_dim_in
, first
, n
);
2210 static void dump_term(struct isl_basic_map
*bmap
,
2211 isl_int c
, int pos
, FILE *out
)
2214 unsigned in
= isl_basic_map_n_in(bmap
);
2215 unsigned dim
= in
+ isl_basic_map_n_out(bmap
);
2216 unsigned nparam
= isl_basic_map_n_param(bmap
);
2218 isl_int_print(out
, c
, 0);
2220 if (!isl_int_is_one(c
))
2221 isl_int_print(out
, c
, 0);
2222 if (pos
< 1 + nparam
) {
2223 name
= isl_space_get_dim_name(bmap
->dim
,
2224 isl_dim_param
, pos
- 1);
2226 fprintf(out
, "%s", name
);
2228 fprintf(out
, "p%d", pos
- 1);
2229 } else if (pos
< 1 + nparam
+ in
)
2230 fprintf(out
, "i%d", pos
- 1 - nparam
);
2231 else if (pos
< 1 + nparam
+ dim
)
2232 fprintf(out
, "o%d", pos
- 1 - nparam
- in
);
2234 fprintf(out
, "e%d", pos
- 1 - nparam
- dim
);
2238 static void dump_constraint_sign(struct isl_basic_map
*bmap
, isl_int
*c
,
2239 int sign
, FILE *out
)
2243 unsigned len
= 1 + isl_basic_map_total_dim(bmap
);
2247 for (i
= 0, first
= 1; i
< len
; ++i
) {
2248 if (isl_int_sgn(c
[i
]) * sign
<= 0)
2251 fprintf(out
, " + ");
2253 isl_int_abs(v
, c
[i
]);
2254 dump_term(bmap
, v
, i
, out
);
2261 static void dump_constraint(struct isl_basic_map
*bmap
, isl_int
*c
,
2262 const char *op
, FILE *out
, int indent
)
2266 fprintf(out
, "%*s", indent
, "");
2268 dump_constraint_sign(bmap
, c
, 1, out
);
2269 fprintf(out
, " %s ", op
);
2270 dump_constraint_sign(bmap
, c
, -1, out
);
2274 for (i
= bmap
->n_div
; i
< bmap
->extra
; ++i
) {
2275 if (isl_int_is_zero(c
[1+isl_space_dim(bmap
->dim
, isl_dim_all
)+i
]))
2277 fprintf(out
, "%*s", indent
, "");
2278 fprintf(out
, "ERROR: unused div coefficient not zero\n");
2283 static void dump_constraints(struct isl_basic_map
*bmap
,
2284 isl_int
**c
, unsigned n
,
2285 const char *op
, FILE *out
, int indent
)
2289 for (i
= 0; i
< n
; ++i
)
2290 dump_constraint(bmap
, c
[i
], op
, out
, indent
);
2293 static void dump_affine(struct isl_basic_map
*bmap
, isl_int
*exp
, FILE *out
)
2297 unsigned total
= isl_basic_map_total_dim(bmap
);
2299 for (j
= 0; j
< 1 + total
; ++j
) {
2300 if (isl_int_is_zero(exp
[j
]))
2302 if (!first
&& isl_int_is_pos(exp
[j
]))
2304 dump_term(bmap
, exp
[j
], j
, out
);
2309 static void dump(struct isl_basic_map
*bmap
, FILE *out
, int indent
)
2313 dump_constraints(bmap
, bmap
->eq
, bmap
->n_eq
, "=", out
, indent
);
2314 dump_constraints(bmap
, bmap
->ineq
, bmap
->n_ineq
, ">=", out
, indent
);
2316 for (i
= 0; i
< bmap
->n_div
; ++i
) {
2317 fprintf(out
, "%*s", indent
, "");
2318 fprintf(out
, "e%d = [(", i
);
2319 dump_affine(bmap
, bmap
->div
[i
]+1, out
);
2321 isl_int_print(out
, bmap
->div
[i
][0], 0);
2322 fprintf(out
, "]\n");
2326 void isl_basic_set_print_internal(struct isl_basic_set
*bset
,
2327 FILE *out
, int indent
)
2330 fprintf(out
, "null basic set\n");
2334 fprintf(out
, "%*s", indent
, "");
2335 fprintf(out
, "ref: %d, nparam: %d, dim: %d, extra: %d, flags: %x\n",
2336 bset
->ref
, bset
->dim
->nparam
, bset
->dim
->n_out
,
2337 bset
->extra
, bset
->flags
);
2338 dump((struct isl_basic_map
*)bset
, out
, indent
);
2341 void isl_basic_map_print_internal(struct isl_basic_map
*bmap
,
2342 FILE *out
, int indent
)
2345 fprintf(out
, "null basic map\n");
2349 fprintf(out
, "%*s", indent
, "");
2350 fprintf(out
, "ref: %d, nparam: %d, in: %d, out: %d, extra: %d, "
2351 "flags: %x, n_name: %d\n",
2353 bmap
->dim
->nparam
, bmap
->dim
->n_in
, bmap
->dim
->n_out
,
2354 bmap
->extra
, bmap
->flags
, bmap
->dim
->n_id
);
2355 dump(bmap
, out
, indent
);
2358 int isl_inequality_negate(struct isl_basic_map
*bmap
, unsigned pos
)
2363 total
= isl_basic_map_total_dim(bmap
);
2364 isl_assert(bmap
->ctx
, pos
< bmap
->n_ineq
, return -1);
2365 isl_seq_neg(bmap
->ineq
[pos
], bmap
->ineq
[pos
], 1 + total
);
2366 isl_int_sub_ui(bmap
->ineq
[pos
][0], bmap
->ineq
[pos
][0], 1);
2367 ISL_F_CLR(bmap
, ISL_BASIC_MAP_NORMALIZED
);
2371 __isl_give isl_set
*isl_set_alloc_space(__isl_take isl_space
*dim
, int n
,
2374 struct isl_set
*set
;
2378 isl_assert(dim
->ctx
, dim
->n_in
== 0, goto error
);
2379 isl_assert(dim
->ctx
, n
>= 0, goto error
);
2380 set
= isl_alloc(dim
->ctx
, struct isl_set
,
2381 sizeof(struct isl_set
) +
2382 (n
- 1) * sizeof(struct isl_basic_set
*));
2386 set
->ctx
= dim
->ctx
;
2387 isl_ctx_ref(set
->ctx
);
2395 isl_space_free(dim
);
2399 struct isl_set
*isl_set_alloc(struct isl_ctx
*ctx
,
2400 unsigned nparam
, unsigned dim
, int n
, unsigned flags
)
2402 struct isl_set
*set
;
2405 dims
= isl_space_alloc(ctx
, nparam
, 0, dim
);
2409 set
= isl_set_alloc_space(dims
, n
, flags
);
2413 /* Make sure "map" has room for at least "n" more basic maps.
2415 struct isl_map
*isl_map_grow(struct isl_map
*map
, int n
)
2418 struct isl_map
*grown
= NULL
;
2422 isl_assert(map
->ctx
, n
>= 0, goto error
);
2423 if (map
->n
+ n
<= map
->size
)
2425 grown
= isl_map_alloc_space(isl_map_get_space(map
), map
->n
+ n
, map
->flags
);
2428 for (i
= 0; i
< map
->n
; ++i
) {
2429 grown
->p
[i
] = isl_basic_map_copy(map
->p
[i
]);
2437 isl_map_free(grown
);
2442 /* Make sure "set" has room for at least "n" more basic sets.
2444 struct isl_set
*isl_set_grow(struct isl_set
*set
, int n
)
2446 return (struct isl_set
*)isl_map_grow((struct isl_map
*)set
, n
);
2449 struct isl_set
*isl_set_dup(struct isl_set
*set
)
2452 struct isl_set
*dup
;
2457 dup
= isl_set_alloc_space(isl_space_copy(set
->dim
), set
->n
, set
->flags
);
2460 for (i
= 0; i
< set
->n
; ++i
)
2461 dup
= isl_set_add_basic_set(dup
, isl_basic_set_copy(set
->p
[i
]));
2465 struct isl_set
*isl_set_from_basic_set(struct isl_basic_set
*bset
)
2467 return isl_map_from_basic_map(bset
);
2470 struct isl_map
*isl_map_from_basic_map(struct isl_basic_map
*bmap
)
2472 struct isl_map
*map
;
2477 map
= isl_map_alloc_space(isl_space_copy(bmap
->dim
), 1, ISL_MAP_DISJOINT
);
2478 return isl_map_add_basic_map(map
, bmap
);
2481 __isl_give isl_set
*isl_set_add_basic_set(__isl_take isl_set
*set
,
2482 __isl_take isl_basic_set
*bset
)
2484 return (struct isl_set
*)isl_map_add_basic_map((struct isl_map
*)set
,
2485 (struct isl_basic_map
*)bset
);
2488 void *isl_set_free(__isl_take isl_set
*set
)
2498 isl_ctx_deref(set
->ctx
);
2499 for (i
= 0; i
< set
->n
; ++i
)
2500 isl_basic_set_free(set
->p
[i
]);
2501 isl_space_free(set
->dim
);
2507 void isl_set_print_internal(struct isl_set
*set
, FILE *out
, int indent
)
2512 fprintf(out
, "null set\n");
2516 fprintf(out
, "%*s", indent
, "");
2517 fprintf(out
, "ref: %d, n: %d, nparam: %d, dim: %d, flags: %x\n",
2518 set
->ref
, set
->n
, set
->dim
->nparam
, set
->dim
->n_out
,
2520 for (i
= 0; i
< set
->n
; ++i
) {
2521 fprintf(out
, "%*s", indent
, "");
2522 fprintf(out
, "basic set %d:\n", i
);
2523 isl_basic_set_print_internal(set
->p
[i
], out
, indent
+4);
2527 void isl_map_print_internal(struct isl_map
*map
, FILE *out
, int indent
)
2532 fprintf(out
, "null map\n");
2536 fprintf(out
, "%*s", indent
, "");
2537 fprintf(out
, "ref: %d, n: %d, nparam: %d, in: %d, out: %d, "
2538 "flags: %x, n_name: %d\n",
2539 map
->ref
, map
->n
, map
->dim
->nparam
, map
->dim
->n_in
,
2540 map
->dim
->n_out
, map
->flags
, map
->dim
->n_id
);
2541 for (i
= 0; i
< map
->n
; ++i
) {
2542 fprintf(out
, "%*s", indent
, "");
2543 fprintf(out
, "basic map %d:\n", i
);
2544 isl_basic_map_print_internal(map
->p
[i
], out
, indent
+4);
2548 struct isl_basic_map
*isl_basic_map_intersect_domain(
2549 struct isl_basic_map
*bmap
, struct isl_basic_set
*bset
)
2551 struct isl_basic_map
*bmap_domain
;
2556 isl_assert(bset
->ctx
, isl_space_match(bmap
->dim
, isl_dim_param
,
2557 bset
->dim
, isl_dim_param
), goto error
);
2559 if (isl_space_dim(bset
->dim
, isl_dim_set
) != 0)
2560 isl_assert(bset
->ctx
,
2561 isl_basic_map_compatible_domain(bmap
, bset
), goto error
);
2563 bmap
= isl_basic_map_cow(bmap
);
2566 bmap
= isl_basic_map_extend_space(bmap
, isl_space_copy(bmap
->dim
),
2567 bset
->n_div
, bset
->n_eq
, bset
->n_ineq
);
2568 bmap_domain
= isl_basic_map_from_domain(bset
);
2569 bmap
= add_constraints(bmap
, bmap_domain
, 0, 0);
2571 bmap
= isl_basic_map_simplify(bmap
);
2572 return isl_basic_map_finalize(bmap
);
2574 isl_basic_map_free(bmap
);
2575 isl_basic_set_free(bset
);
2579 struct isl_basic_map
*isl_basic_map_intersect_range(
2580 struct isl_basic_map
*bmap
, struct isl_basic_set
*bset
)
2582 struct isl_basic_map
*bmap_range
;
2587 isl_assert(bset
->ctx
, isl_space_match(bmap
->dim
, isl_dim_param
,
2588 bset
->dim
, isl_dim_param
), goto error
);
2590 if (isl_space_dim(bset
->dim
, isl_dim_set
) != 0)
2591 isl_assert(bset
->ctx
,
2592 isl_basic_map_compatible_range(bmap
, bset
), goto error
);
2594 if (isl_basic_set_is_universe(bset
)) {
2595 isl_basic_set_free(bset
);
2599 bmap
= isl_basic_map_cow(bmap
);
2602 bmap
= isl_basic_map_extend_space(bmap
, isl_space_copy(bmap
->dim
),
2603 bset
->n_div
, bset
->n_eq
, bset
->n_ineq
);
2604 bmap_range
= isl_basic_map_from_basic_set(bset
, isl_space_copy(bset
->dim
));
2605 bmap
= add_constraints(bmap
, bmap_range
, 0, 0);
2607 bmap
= isl_basic_map_simplify(bmap
);
2608 return isl_basic_map_finalize(bmap
);
2610 isl_basic_map_free(bmap
);
2611 isl_basic_set_free(bset
);
2615 int isl_basic_map_contains(struct isl_basic_map
*bmap
, struct isl_vec
*vec
)
2621 total
= 1 + isl_basic_map_total_dim(bmap
);
2622 if (total
!= vec
->size
)
2627 for (i
= 0; i
< bmap
->n_eq
; ++i
) {
2628 isl_seq_inner_product(vec
->el
, bmap
->eq
[i
], total
, &s
);
2629 if (!isl_int_is_zero(s
)) {
2635 for (i
= 0; i
< bmap
->n_ineq
; ++i
) {
2636 isl_seq_inner_product(vec
->el
, bmap
->ineq
[i
], total
, &s
);
2637 if (isl_int_is_neg(s
)) {
2648 int isl_basic_set_contains(struct isl_basic_set
*bset
, struct isl_vec
*vec
)
2650 return isl_basic_map_contains((struct isl_basic_map
*)bset
, vec
);
2653 struct isl_basic_map
*isl_basic_map_intersect(
2654 struct isl_basic_map
*bmap1
, struct isl_basic_map
*bmap2
)
2656 struct isl_vec
*sample
= NULL
;
2658 if (!bmap1
|| !bmap2
)
2661 isl_assert(bmap1
->ctx
, isl_space_match(bmap1
->dim
, isl_dim_param
,
2662 bmap2
->dim
, isl_dim_param
), goto error
);
2663 if (isl_space_dim(bmap1
->dim
, isl_dim_all
) ==
2664 isl_space_dim(bmap1
->dim
, isl_dim_param
) &&
2665 isl_space_dim(bmap2
->dim
, isl_dim_all
) !=
2666 isl_space_dim(bmap2
->dim
, isl_dim_param
))
2667 return isl_basic_map_intersect(bmap2
, bmap1
);
2669 if (isl_space_dim(bmap2
->dim
, isl_dim_all
) !=
2670 isl_space_dim(bmap2
->dim
, isl_dim_param
))
2671 isl_assert(bmap1
->ctx
,
2672 isl_space_is_equal(bmap1
->dim
, bmap2
->dim
), goto error
);
2674 if (bmap1
->sample
&&
2675 isl_basic_map_contains(bmap1
, bmap1
->sample
) > 0 &&
2676 isl_basic_map_contains(bmap2
, bmap1
->sample
) > 0)
2677 sample
= isl_vec_copy(bmap1
->sample
);
2678 else if (bmap2
->sample
&&
2679 isl_basic_map_contains(bmap1
, bmap2
->sample
) > 0 &&
2680 isl_basic_map_contains(bmap2
, bmap2
->sample
) > 0)
2681 sample
= isl_vec_copy(bmap2
->sample
);
2683 bmap1
= isl_basic_map_cow(bmap1
);
2686 bmap1
= isl_basic_map_extend_space(bmap1
, isl_space_copy(bmap1
->dim
),
2687 bmap2
->n_div
, bmap2
->n_eq
, bmap2
->n_ineq
);
2688 bmap1
= add_constraints(bmap1
, bmap2
, 0, 0);
2691 isl_vec_free(sample
);
2693 isl_vec_free(bmap1
->sample
);
2694 bmap1
->sample
= sample
;
2697 bmap1
= isl_basic_map_simplify(bmap1
);
2698 return isl_basic_map_finalize(bmap1
);
2701 isl_vec_free(sample
);
2702 isl_basic_map_free(bmap1
);
2703 isl_basic_map_free(bmap2
);
2707 struct isl_basic_set
*isl_basic_set_intersect(
2708 struct isl_basic_set
*bset1
, struct isl_basic_set
*bset2
)
2710 return (struct isl_basic_set
*)
2711 isl_basic_map_intersect(
2712 (struct isl_basic_map
*)bset1
,
2713 (struct isl_basic_map
*)bset2
);
2716 __isl_give isl_basic_set
*isl_basic_set_intersect_params(
2717 __isl_take isl_basic_set
*bset1
, __isl_take isl_basic_set
*bset2
)
2719 return isl_basic_set_intersect(bset1
, bset2
);
2722 /* Special case of isl_map_intersect, where both map1 and map2
2723 * are convex, without any divs and such that either map1 or map2
2724 * contains a single constraint. This constraint is then simply
2725 * added to the other map.
2727 static __isl_give isl_map
*map_intersect_add_constraint(
2728 __isl_take isl_map
*map1
, __isl_take isl_map
*map2
)
2730 isl_assert(map1
->ctx
, map1
->n
== 1, goto error
);
2731 isl_assert(map2
->ctx
, map1
->n
== 1, goto error
);
2732 isl_assert(map1
->ctx
, map1
->p
[0]->n_div
== 0, goto error
);
2733 isl_assert(map2
->ctx
, map1
->p
[0]->n_div
== 0, goto error
);
2735 if (map2
->p
[0]->n_eq
+ map2
->p
[0]->n_ineq
!= 1)
2736 return isl_map_intersect(map2
, map1
);
2738 isl_assert(map2
->ctx
,
2739 map2
->p
[0]->n_eq
+ map2
->p
[0]->n_ineq
== 1, goto error
);
2741 map1
= isl_map_cow(map1
);
2744 if (isl_map_plain_is_empty(map1
)) {
2748 map1
->p
[0] = isl_basic_map_cow(map1
->p
[0]);
2749 if (map2
->p
[0]->n_eq
== 1)
2750 map1
->p
[0] = isl_basic_map_add_eq(map1
->p
[0], map2
->p
[0]->eq
[0]);
2752 map1
->p
[0] = isl_basic_map_add_ineq(map1
->p
[0],
2753 map2
->p
[0]->ineq
[0]);
2755 map1
->p
[0] = isl_basic_map_simplify(map1
->p
[0]);
2756 map1
->p
[0] = isl_basic_map_finalize(map1
->p
[0]);
2760 if (isl_basic_map_plain_is_empty(map1
->p
[0])) {
2761 isl_basic_map_free(map1
->p
[0]);
2774 /* map2 may be either a parameter domain or a map living in the same
2777 static __isl_give isl_map
*map_intersect_internal(__isl_take isl_map
*map1
,
2778 __isl_take isl_map
*map2
)
2781 struct isl_map
*result
;
2787 if (isl_map_plain_is_empty(map1
) &&
2788 isl_space_is_equal(map1
->dim
, map2
->dim
)) {
2792 if (isl_map_plain_is_empty(map2
) &&
2793 isl_space_is_equal(map1
->dim
, map2
->dim
)) {
2798 if (map1
->n
== 1 && map2
->n
== 1 &&
2799 map1
->p
[0]->n_div
== 0 && map2
->p
[0]->n_div
== 0 &&
2800 isl_space_is_equal(map1
->dim
, map2
->dim
) &&
2801 (map1
->p
[0]->n_eq
+ map1
->p
[0]->n_ineq
== 1 ||
2802 map2
->p
[0]->n_eq
+ map2
->p
[0]->n_ineq
== 1))
2803 return map_intersect_add_constraint(map1
, map2
);
2805 if (isl_space_dim(map2
->dim
, isl_dim_all
) !=
2806 isl_space_dim(map2
->dim
, isl_dim_param
))
2807 isl_assert(map1
->ctx
,
2808 isl_space_is_equal(map1
->dim
, map2
->dim
), goto error
);
2810 if (ISL_F_ISSET(map1
, ISL_MAP_DISJOINT
) &&
2811 ISL_F_ISSET(map2
, ISL_MAP_DISJOINT
))
2812 ISL_FL_SET(flags
, ISL_MAP_DISJOINT
);
2814 result
= isl_map_alloc_space(isl_space_copy(map1
->dim
),
2815 map1
->n
* map2
->n
, flags
);
2818 for (i
= 0; i
< map1
->n
; ++i
)
2819 for (j
= 0; j
< map2
->n
; ++j
) {
2820 struct isl_basic_map
*part
;
2821 part
= isl_basic_map_intersect(
2822 isl_basic_map_copy(map1
->p
[i
]),
2823 isl_basic_map_copy(map2
->p
[j
]));
2824 if (isl_basic_map_is_empty(part
))
2825 isl_basic_map_free(part
);
2827 result
= isl_map_add_basic_map(result
, part
);
2840 static __isl_give isl_map
*map_intersect(__isl_take isl_map
*map1
,
2841 __isl_take isl_map
*map2
)
2845 if (!isl_space_is_equal(map1
->dim
, map2
->dim
))
2846 isl_die(isl_map_get_ctx(map1
), isl_error_invalid
,
2847 "spaces don't match", goto error
);
2848 return map_intersect_internal(map1
, map2
);
2855 __isl_give isl_map
*isl_map_intersect(__isl_take isl_map
*map1
,
2856 __isl_take isl_map
*map2
)
2858 return isl_map_align_params_map_map_and(map1
, map2
, &map_intersect
);
2861 struct isl_set
*isl_set_intersect(struct isl_set
*set1
, struct isl_set
*set2
)
2863 return (struct isl_set
*)
2864 isl_map_intersect((struct isl_map
*)set1
,
2865 (struct isl_map
*)set2
);
2868 /* map_intersect_internal accepts intersections
2869 * with parameter domains, so we can just call that function.
2871 static __isl_give isl_map
*map_intersect_params(__isl_take isl_map
*map
,
2872 __isl_take isl_set
*params
)
2874 return map_intersect_internal(map
, params
);
2877 __isl_give isl_map
*isl_map_intersect_params(__isl_take isl_map
*map1
,
2878 __isl_take isl_map
*map2
)
2880 return isl_map_align_params_map_map_and(map1
, map2
, &map_intersect_params
);
2883 __isl_give isl_set
*isl_set_intersect_params(__isl_take isl_set
*set
,
2884 __isl_take isl_set
*params
)
2886 return isl_map_intersect_params(set
, params
);
2889 struct isl_basic_map
*isl_basic_map_reverse(struct isl_basic_map
*bmap
)
2892 struct isl_basic_set
*bset
;
2897 bmap
= isl_basic_map_cow(bmap
);
2900 dim
= isl_space_reverse(isl_space_copy(bmap
->dim
));
2901 in
= isl_basic_map_n_in(bmap
);
2902 bset
= isl_basic_set_from_basic_map(bmap
);
2903 bset
= isl_basic_set_swap_vars(bset
, in
);
2904 return isl_basic_map_from_basic_set(bset
, dim
);
2907 static __isl_give isl_basic_map
*basic_map_space_reset(
2908 __isl_take isl_basic_map
*bmap
, enum isl_dim_type type
)
2912 if (!isl_space_is_named_or_nested(bmap
->dim
, type
))
2915 space
= isl_basic_map_get_space(bmap
);
2916 space
= isl_space_reset(space
, type
);
2917 bmap
= isl_basic_map_reset_space(bmap
, space
);
2921 __isl_give isl_basic_map
*isl_basic_map_insert_dims(
2922 __isl_take isl_basic_map
*bmap
, enum isl_dim_type type
,
2923 unsigned pos
, unsigned n
)
2926 struct isl_basic_map
*res
;
2927 struct isl_dim_map
*dim_map
;
2928 unsigned total
, off
;
2929 enum isl_dim_type t
;
2932 return basic_map_space_reset(bmap
, type
);
2937 res_dim
= isl_space_insert_dims(isl_basic_map_get_space(bmap
), type
, pos
, n
);
2939 total
= isl_basic_map_total_dim(bmap
) + n
;
2940 dim_map
= isl_dim_map_alloc(bmap
->ctx
, total
);
2942 for (t
= isl_dim_param
; t
<= isl_dim_out
; ++t
) {
2944 isl_dim_map_dim(dim_map
, bmap
->dim
, t
, off
);
2946 unsigned size
= isl_basic_map_dim(bmap
, t
);
2947 isl_dim_map_dim_range(dim_map
, bmap
->dim
, t
,
2949 isl_dim_map_dim_range(dim_map
, bmap
->dim
, t
,
2950 pos
, size
- pos
, off
+ pos
+ n
);
2952 off
+= isl_space_dim(res_dim
, t
);
2954 isl_dim_map_div(dim_map
, bmap
, off
);
2956 res
= isl_basic_map_alloc_space(res_dim
,
2957 bmap
->n_div
, bmap
->n_eq
, bmap
->n_ineq
);
2958 if (isl_basic_map_is_rational(bmap
))
2959 res
= isl_basic_map_set_rational(res
);
2960 if (isl_basic_map_plain_is_empty(bmap
)) {
2961 isl_basic_map_free(bmap
);
2962 return isl_basic_map_set_to_empty(res
);
2964 res
= isl_basic_map_add_constraints_dim_map(res
, bmap
, dim_map
);
2965 return isl_basic_map_finalize(res
);
2968 __isl_give isl_basic_set
*isl_basic_set_insert_dims(
2969 __isl_take isl_basic_set
*bset
,
2970 enum isl_dim_type type
, unsigned pos
, unsigned n
)
2972 return isl_basic_map_insert_dims(bset
, type
, pos
, n
);
2975 __isl_give isl_basic_map
*isl_basic_map_add(__isl_take isl_basic_map
*bmap
,
2976 enum isl_dim_type type
, unsigned n
)
2980 return isl_basic_map_insert_dims(bmap
, type
,
2981 isl_basic_map_dim(bmap
, type
), n
);
2984 __isl_give isl_basic_set
*isl_basic_set_add(__isl_take isl_basic_set
*bset
,
2985 enum isl_dim_type type
, unsigned n
)
2989 isl_assert(bset
->ctx
, type
!= isl_dim_in
, goto error
);
2990 return (isl_basic_set
*)isl_basic_map_add((isl_basic_map
*)bset
, type
, n
);
2992 isl_basic_set_free(bset
);
2996 static __isl_give isl_map
*map_space_reset(__isl_take isl_map
*map
,
2997 enum isl_dim_type type
)
3001 if (!map
|| !isl_space_is_named_or_nested(map
->dim
, type
))
3004 space
= isl_map_get_space(map
);
3005 space
= isl_space_reset(space
, type
);
3006 map
= isl_map_reset_space(map
, space
);
3010 __isl_give isl_map
*isl_map_insert_dims(__isl_take isl_map
*map
,
3011 enum isl_dim_type type
, unsigned pos
, unsigned n
)
3016 return map_space_reset(map
, type
);
3018 map
= isl_map_cow(map
);
3022 map
->dim
= isl_space_insert_dims(map
->dim
, type
, pos
, n
);
3026 for (i
= 0; i
< map
->n
; ++i
) {
3027 map
->p
[i
] = isl_basic_map_insert_dims(map
->p
[i
], type
, pos
, n
);
3038 __isl_give isl_set
*isl_set_insert_dims(__isl_take isl_set
*set
,
3039 enum isl_dim_type type
, unsigned pos
, unsigned n
)
3041 return isl_map_insert_dims(set
, type
, pos
, n
);
3044 __isl_give isl_map
*isl_map_add_dims(__isl_take isl_map
*map
,
3045 enum isl_dim_type type
, unsigned n
)
3049 return isl_map_insert_dims(map
, type
, isl_map_dim(map
, type
), n
);
3052 __isl_give isl_set
*isl_set_add_dims(__isl_take isl_set
*set
,
3053 enum isl_dim_type type
, unsigned n
)
3057 isl_assert(set
->ctx
, type
!= isl_dim_in
, goto error
);
3058 return (isl_set
*)isl_map_add_dims((isl_map
*)set
, type
, n
);
3064 __isl_give isl_basic_map
*isl_basic_map_move_dims(
3065 __isl_take isl_basic_map
*bmap
,
3066 enum isl_dim_type dst_type
, unsigned dst_pos
,
3067 enum isl_dim_type src_type
, unsigned src_pos
, unsigned n
)
3069 struct isl_dim_map
*dim_map
;
3070 struct isl_basic_map
*res
;
3071 enum isl_dim_type t
;
3072 unsigned total
, off
;
3079 isl_assert(bmap
->ctx
, src_pos
+ n
<= isl_basic_map_dim(bmap
, src_type
),
3082 if (dst_type
== src_type
&& dst_pos
== src_pos
)
3085 isl_assert(bmap
->ctx
, dst_type
!= src_type
, goto error
);
3087 if (pos(bmap
->dim
, dst_type
) + dst_pos
==
3088 pos(bmap
->dim
, src_type
) + src_pos
+
3089 ((src_type
< dst_type
) ? n
: 0)) {
3090 bmap
= isl_basic_map_cow(bmap
);
3094 bmap
->dim
= isl_space_move_dims(bmap
->dim
, dst_type
, dst_pos
,
3095 src_type
, src_pos
, n
);
3099 bmap
= isl_basic_map_finalize(bmap
);
3104 total
= isl_basic_map_total_dim(bmap
);
3105 dim_map
= isl_dim_map_alloc(bmap
->ctx
, total
);
3108 for (t
= isl_dim_param
; t
<= isl_dim_out
; ++t
) {
3109 unsigned size
= isl_space_dim(bmap
->dim
, t
);
3110 if (t
== dst_type
) {
3111 isl_dim_map_dim_range(dim_map
, bmap
->dim
, t
,
3114 isl_dim_map_dim_range(dim_map
, bmap
->dim
, src_type
,
3117 isl_dim_map_dim_range(dim_map
, bmap
->dim
, t
,
3118 dst_pos
, size
- dst_pos
, off
);
3119 off
+= size
- dst_pos
;
3120 } else if (t
== src_type
) {
3121 isl_dim_map_dim_range(dim_map
, bmap
->dim
, t
,
3124 isl_dim_map_dim_range(dim_map
, bmap
->dim
, t
,
3125 src_pos
+ n
, size
- src_pos
- n
, off
);
3126 off
+= size
- src_pos
- n
;
3128 isl_dim_map_dim(dim_map
, bmap
->dim
, t
, off
);
3132 isl_dim_map_div(dim_map
, bmap
, off
);
3134 res
= isl_basic_map_alloc_space(isl_basic_map_get_space(bmap
),
3135 bmap
->n_div
, bmap
->n_eq
, bmap
->n_ineq
);
3136 bmap
= isl_basic_map_add_constraints_dim_map(res
, bmap
, dim_map
);
3138 bmap
->dim
= isl_space_move_dims(bmap
->dim
, dst_type
, dst_pos
,
3139 src_type
, src_pos
, n
);
3143 ISL_F_CLR(bmap
, ISL_BASIC_MAP_NORMALIZED
);
3144 bmap
= isl_basic_map_gauss(bmap
, NULL
);
3145 bmap
= isl_basic_map_finalize(bmap
);
3149 isl_basic_map_free(bmap
);
3153 __isl_give isl_basic_set
*isl_basic_set_move_dims(__isl_take isl_basic_set
*bset
,
3154 enum isl_dim_type dst_type
, unsigned dst_pos
,
3155 enum isl_dim_type src_type
, unsigned src_pos
, unsigned n
)
3157 return (isl_basic_set
*)isl_basic_map_move_dims(
3158 (isl_basic_map
*)bset
, dst_type
, dst_pos
, src_type
, src_pos
, n
);
3161 __isl_give isl_set
*isl_set_move_dims(__isl_take isl_set
*set
,
3162 enum isl_dim_type dst_type
, unsigned dst_pos
,
3163 enum isl_dim_type src_type
, unsigned src_pos
, unsigned n
)
3167 isl_assert(set
->ctx
, dst_type
!= isl_dim_in
, goto error
);
3168 return (isl_set
*)isl_map_move_dims((isl_map
*)set
, dst_type
, dst_pos
,
3169 src_type
, src_pos
, n
);
3175 __isl_give isl_map
*isl_map_move_dims(__isl_take isl_map
*map
,
3176 enum isl_dim_type dst_type
, unsigned dst_pos
,
3177 enum isl_dim_type src_type
, unsigned src_pos
, unsigned n
)
3186 isl_assert(map
->ctx
, src_pos
+ n
<= isl_map_dim(map
, src_type
),
3189 if (dst_type
== src_type
&& dst_pos
== src_pos
)
3192 isl_assert(map
->ctx
, dst_type
!= src_type
, goto error
);
3194 map
= isl_map_cow(map
);
3198 map
->dim
= isl_space_move_dims(map
->dim
, dst_type
, dst_pos
, src_type
, src_pos
, n
);
3202 for (i
= 0; i
< map
->n
; ++i
) {
3203 map
->p
[i
] = isl_basic_map_move_dims(map
->p
[i
],
3205 src_type
, src_pos
, n
);
3216 /* Move the specified dimensions to the last columns right before
3217 * the divs. Don't change the dimension specification of bmap.
3218 * That's the responsibility of the caller.
3220 static __isl_give isl_basic_map
*move_last(__isl_take isl_basic_map
*bmap
,
3221 enum isl_dim_type type
, unsigned first
, unsigned n
)
3223 struct isl_dim_map
*dim_map
;
3224 struct isl_basic_map
*res
;
3225 enum isl_dim_type t
;
3226 unsigned total
, off
;
3230 if (pos(bmap
->dim
, type
) + first
+ n
==
3231 1 + isl_space_dim(bmap
->dim
, isl_dim_all
))
3234 total
= isl_basic_map_total_dim(bmap
);
3235 dim_map
= isl_dim_map_alloc(bmap
->ctx
, total
);
3238 for (t
= isl_dim_param
; t
<= isl_dim_out
; ++t
) {
3239 unsigned size
= isl_space_dim(bmap
->dim
, t
);
3241 isl_dim_map_dim_range(dim_map
, bmap
->dim
, t
,
3244 isl_dim_map_dim_range(dim_map
, bmap
->dim
, t
,
3245 first
, n
, total
- bmap
->n_div
- n
);
3246 isl_dim_map_dim_range(dim_map
, bmap
->dim
, t
,
3247 first
+ n
, size
- (first
+ n
), off
);
3248 off
+= size
- (first
+ n
);
3250 isl_dim_map_dim(dim_map
, bmap
->dim
, t
, off
);
3254 isl_dim_map_div(dim_map
, bmap
, off
+ n
);
3256 res
= isl_basic_map_alloc_space(isl_basic_map_get_space(bmap
),
3257 bmap
->n_div
, bmap
->n_eq
, bmap
->n_ineq
);
3258 res
= isl_basic_map_add_constraints_dim_map(res
, bmap
, dim_map
);
3262 /* Turn the n dimensions of type type, starting at first
3263 * into existentially quantified variables.
3265 __isl_give isl_basic_map
*isl_basic_map_project_out(
3266 __isl_take isl_basic_map
*bmap
,
3267 enum isl_dim_type type
, unsigned first
, unsigned n
)
3275 return basic_map_space_reset(bmap
, type
);
3280 if (ISL_F_ISSET(bmap
, ISL_BASIC_MAP_RATIONAL
))
3281 return isl_basic_map_remove_dims(bmap
, type
, first
, n
);
3283 isl_assert(bmap
->ctx
, first
+ n
<= isl_basic_map_dim(bmap
, type
),
3286 bmap
= move_last(bmap
, type
, first
, n
);
3287 bmap
= isl_basic_map_cow(bmap
);
3291 row_size
= 1 + isl_space_dim(bmap
->dim
, isl_dim_all
) + bmap
->extra
;
3292 old
= bmap
->block2
.data
;
3293 bmap
->block2
= isl_blk_extend(bmap
->ctx
, bmap
->block2
,
3294 (bmap
->extra
+ n
) * (1 + row_size
));
3295 if (!bmap
->block2
.data
)
3297 new_div
= isl_alloc_array(bmap
->ctx
, isl_int
*, bmap
->extra
+ n
);
3300 for (i
= 0; i
< n
; ++i
) {
3301 new_div
[i
] = bmap
->block2
.data
+
3302 (bmap
->extra
+ i
) * (1 + row_size
);
3303 isl_seq_clr(new_div
[i
], 1 + row_size
);
3305 for (i
= 0; i
< bmap
->extra
; ++i
)
3306 new_div
[n
+ i
] = bmap
->block2
.data
+ (bmap
->div
[i
] - old
);
3308 bmap
->div
= new_div
;
3312 bmap
->dim
= isl_space_drop_dims(bmap
->dim
, type
, first
, n
);
3315 bmap
= isl_basic_map_simplify(bmap
);
3316 bmap
= isl_basic_map_drop_redundant_divs(bmap
);
3317 return isl_basic_map_finalize(bmap
);
3319 isl_basic_map_free(bmap
);
3323 /* Turn the n dimensions of type type, starting at first
3324 * into existentially quantified variables.
3326 struct isl_basic_set
*isl_basic_set_project_out(struct isl_basic_set
*bset
,
3327 enum isl_dim_type type
, unsigned first
, unsigned n
)
3329 return (isl_basic_set
*)isl_basic_map_project_out(
3330 (isl_basic_map
*)bset
, type
, first
, n
);
3333 /* Turn the n dimensions of type type, starting at first
3334 * into existentially quantified variables.
3336 __isl_give isl_map
*isl_map_project_out(__isl_take isl_map
*map
,
3337 enum isl_dim_type type
, unsigned first
, unsigned n
)
3345 return map_space_reset(map
, type
);
3347 isl_assert(map
->ctx
, first
+ n
<= isl_map_dim(map
, type
), goto error
);
3349 map
= isl_map_cow(map
);
3353 map
->dim
= isl_space_drop_dims(map
->dim
, type
, first
, n
);
3357 for (i
= 0; i
< map
->n
; ++i
) {
3358 map
->p
[i
] = isl_basic_map_project_out(map
->p
[i
], type
, first
, n
);
3369 /* Turn the n dimensions of type type, starting at first
3370 * into existentially quantified variables.
3372 __isl_give isl_set
*isl_set_project_out(__isl_take isl_set
*set
,
3373 enum isl_dim_type type
, unsigned first
, unsigned n
)
3375 return (isl_set
*)isl_map_project_out((isl_map
*)set
, type
, first
, n
);
3378 static struct isl_basic_map
*add_divs(struct isl_basic_map
*bmap
, unsigned n
)
3382 for (i
= 0; i
< n
; ++i
) {
3383 j
= isl_basic_map_alloc_div(bmap
);
3386 isl_seq_clr(bmap
->div
[j
], 1+1+isl_basic_map_total_dim(bmap
));
3390 isl_basic_map_free(bmap
);
3394 struct isl_basic_map
*isl_basic_map_apply_range(
3395 struct isl_basic_map
*bmap1
, struct isl_basic_map
*bmap2
)
3397 isl_space
*dim_result
= NULL
;
3398 struct isl_basic_map
*bmap
;
3399 unsigned n_in
, n_out
, n
, nparam
, total
, pos
;
3400 struct isl_dim_map
*dim_map1
, *dim_map2
;
3402 if (!bmap1
|| !bmap2
)
3405 dim_result
= isl_space_join(isl_space_copy(bmap1
->dim
),
3406 isl_space_copy(bmap2
->dim
));
3408 n_in
= isl_basic_map_n_in(bmap1
);
3409 n_out
= isl_basic_map_n_out(bmap2
);
3410 n
= isl_basic_map_n_out(bmap1
);
3411 nparam
= isl_basic_map_n_param(bmap1
);
3413 total
= nparam
+ n_in
+ n_out
+ bmap1
->n_div
+ bmap2
->n_div
+ n
;
3414 dim_map1
= isl_dim_map_alloc(bmap1
->ctx
, total
);
3415 dim_map2
= isl_dim_map_alloc(bmap1
->ctx
, total
);
3416 isl_dim_map_dim(dim_map1
, bmap1
->dim
, isl_dim_param
, pos
= 0);
3417 isl_dim_map_dim(dim_map2
, bmap2
->dim
, isl_dim_param
, pos
= 0);
3418 isl_dim_map_dim(dim_map1
, bmap1
->dim
, isl_dim_in
, pos
+= nparam
);
3419 isl_dim_map_dim(dim_map2
, bmap2
->dim
, isl_dim_out
, pos
+= n_in
);
3420 isl_dim_map_div(dim_map1
, bmap1
, pos
+= n_out
);
3421 isl_dim_map_div(dim_map2
, bmap2
, pos
+= bmap1
->n_div
);
3422 isl_dim_map_dim(dim_map1
, bmap1
->dim
, isl_dim_out
, pos
+= bmap2
->n_div
);
3423 isl_dim_map_dim(dim_map2
, bmap2
->dim
, isl_dim_in
, pos
);
3425 bmap
= isl_basic_map_alloc_space(dim_result
,
3426 bmap1
->n_div
+ bmap2
->n_div
+ n
,
3427 bmap1
->n_eq
+ bmap2
->n_eq
,
3428 bmap1
->n_ineq
+ bmap2
->n_ineq
);
3429 bmap
= isl_basic_map_add_constraints_dim_map(bmap
, bmap1
, dim_map1
);
3430 bmap
= isl_basic_map_add_constraints_dim_map(bmap
, bmap2
, dim_map2
);
3431 bmap
= add_divs(bmap
, n
);
3432 bmap
= isl_basic_map_simplify(bmap
);
3433 bmap
= isl_basic_map_drop_redundant_divs(bmap
);
3434 return isl_basic_map_finalize(bmap
);
3436 isl_basic_map_free(bmap1
);
3437 isl_basic_map_free(bmap2
);
3441 struct isl_basic_set
*isl_basic_set_apply(
3442 struct isl_basic_set
*bset
, struct isl_basic_map
*bmap
)
3447 isl_assert(bset
->ctx
, isl_basic_map_compatible_domain(bmap
, bset
),
3450 return (struct isl_basic_set
*)
3451 isl_basic_map_apply_range((struct isl_basic_map
*)bset
, bmap
);
3453 isl_basic_set_free(bset
);
3454 isl_basic_map_free(bmap
);
3458 struct isl_basic_map
*isl_basic_map_apply_domain(
3459 struct isl_basic_map
*bmap1
, struct isl_basic_map
*bmap2
)
3461 if (!bmap1
|| !bmap2
)
3464 isl_assert(bmap1
->ctx
,
3465 isl_basic_map_n_in(bmap1
) == isl_basic_map_n_in(bmap2
), goto error
);
3466 isl_assert(bmap1
->ctx
,
3467 isl_basic_map_n_param(bmap1
) == isl_basic_map_n_param(bmap2
),
3470 bmap1
= isl_basic_map_reverse(bmap1
);
3471 bmap1
= isl_basic_map_apply_range(bmap1
, bmap2
);
3472 return isl_basic_map_reverse(bmap1
);
3474 isl_basic_map_free(bmap1
);
3475 isl_basic_map_free(bmap2
);
3479 /* Given two basic maps A -> f(A) and B -> g(B), construct a basic map
3480 * A \cap B -> f(A) + f(B)
3482 struct isl_basic_map
*isl_basic_map_sum(
3483 struct isl_basic_map
*bmap1
, struct isl_basic_map
*bmap2
)
3485 unsigned n_in
, n_out
, nparam
, total
, pos
;
3486 struct isl_basic_map
*bmap
= NULL
;
3487 struct isl_dim_map
*dim_map1
, *dim_map2
;
3490 if (!bmap1
|| !bmap2
)
3493 isl_assert(bmap1
->ctx
, isl_space_is_equal(bmap1
->dim
, bmap2
->dim
),
3496 nparam
= isl_basic_map_n_param(bmap1
);
3497 n_in
= isl_basic_map_n_in(bmap1
);
3498 n_out
= isl_basic_map_n_out(bmap1
);
3500 total
= nparam
+ n_in
+ n_out
+ bmap1
->n_div
+ bmap2
->n_div
+ 2 * n_out
;
3501 dim_map1
= isl_dim_map_alloc(bmap1
->ctx
, total
);
3502 dim_map2
= isl_dim_map_alloc(bmap2
->ctx
, total
);
3503 isl_dim_map_dim(dim_map1
, bmap1
->dim
, isl_dim_param
, pos
= 0);
3504 isl_dim_map_dim(dim_map2
, bmap2
->dim
, isl_dim_param
, pos
);
3505 isl_dim_map_dim(dim_map1
, bmap1
->dim
, isl_dim_in
, pos
+= nparam
);
3506 isl_dim_map_dim(dim_map2
, bmap2
->dim
, isl_dim_in
, pos
);
3507 isl_dim_map_div(dim_map1
, bmap1
, pos
+= n_in
+ n_out
);
3508 isl_dim_map_div(dim_map2
, bmap2
, pos
+= bmap1
->n_div
);
3509 isl_dim_map_dim(dim_map1
, bmap1
->dim
, isl_dim_out
, pos
+= bmap2
->n_div
);
3510 isl_dim_map_dim(dim_map2
, bmap2
->dim
, isl_dim_out
, pos
+= n_out
);
3512 bmap
= isl_basic_map_alloc_space(isl_space_copy(bmap1
->dim
),
3513 bmap1
->n_div
+ bmap2
->n_div
+ 2 * n_out
,
3514 bmap1
->n_eq
+ bmap2
->n_eq
+ n_out
,
3515 bmap1
->n_ineq
+ bmap2
->n_ineq
);
3516 for (i
= 0; i
< n_out
; ++i
) {
3517 int j
= isl_basic_map_alloc_equality(bmap
);
3520 isl_seq_clr(bmap
->eq
[j
], 1+total
);
3521 isl_int_set_si(bmap
->eq
[j
][1+nparam
+n_in
+i
], -1);
3522 isl_int_set_si(bmap
->eq
[j
][1+pos
+i
], 1);
3523 isl_int_set_si(bmap
->eq
[j
][1+pos
-n_out
+i
], 1);
3525 bmap
= isl_basic_map_add_constraints_dim_map(bmap
, bmap1
, dim_map1
);
3526 bmap
= isl_basic_map_add_constraints_dim_map(bmap
, bmap2
, dim_map2
);
3527 bmap
= add_divs(bmap
, 2 * n_out
);
3529 bmap
= isl_basic_map_simplify(bmap
);
3530 return isl_basic_map_finalize(bmap
);
3532 isl_basic_map_free(bmap
);
3533 isl_basic_map_free(bmap1
);
3534 isl_basic_map_free(bmap2
);
3538 /* Given two maps A -> f(A) and B -> g(B), construct a map
3539 * A \cap B -> f(A) + f(B)
3541 struct isl_map
*isl_map_sum(struct isl_map
*map1
, struct isl_map
*map2
)
3543 struct isl_map
*result
;
3549 isl_assert(map1
->ctx
, isl_space_is_equal(map1
->dim
, map2
->dim
), goto error
);
3551 result
= isl_map_alloc_space(isl_space_copy(map1
->dim
),
3552 map1
->n
* map2
->n
, 0);
3555 for (i
= 0; i
< map1
->n
; ++i
)
3556 for (j
= 0; j
< map2
->n
; ++j
) {
3557 struct isl_basic_map
*part
;
3558 part
= isl_basic_map_sum(
3559 isl_basic_map_copy(map1
->p
[i
]),
3560 isl_basic_map_copy(map2
->p
[j
]));
3561 if (isl_basic_map_is_empty(part
))
3562 isl_basic_map_free(part
);
3564 result
= isl_map_add_basic_map(result
, part
);
3577 __isl_give isl_set
*isl_set_sum(__isl_take isl_set
*set1
,
3578 __isl_take isl_set
*set2
)
3580 return (isl_set
*)isl_map_sum((isl_map
*)set1
, (isl_map
*)set2
);
3583 /* Given a basic map A -> f(A), construct A -> -f(A).
3585 struct isl_basic_map
*isl_basic_map_neg(struct isl_basic_map
*bmap
)
3590 bmap
= isl_basic_map_cow(bmap
);
3594 n
= isl_basic_map_dim(bmap
, isl_dim_out
);
3595 off
= isl_basic_map_offset(bmap
, isl_dim_out
);
3596 for (i
= 0; i
< bmap
->n_eq
; ++i
)
3597 for (j
= 0; j
< n
; ++j
)
3598 isl_int_neg(bmap
->eq
[i
][off
+j
], bmap
->eq
[i
][off
+j
]);
3599 for (i
= 0; i
< bmap
->n_ineq
; ++i
)
3600 for (j
= 0; j
< n
; ++j
)
3601 isl_int_neg(bmap
->ineq
[i
][off
+j
], bmap
->ineq
[i
][off
+j
]);
3602 for (i
= 0; i
< bmap
->n_div
; ++i
)
3603 for (j
= 0; j
< n
; ++j
)
3604 isl_int_neg(bmap
->div
[i
][1+off
+j
], bmap
->div
[i
][1+off
+j
]);
3605 bmap
= isl_basic_map_gauss(bmap
, NULL
);
3606 return isl_basic_map_finalize(bmap
);
3609 __isl_give isl_basic_set
*isl_basic_set_neg(__isl_take isl_basic_set
*bset
)
3611 return isl_basic_map_neg(bset
);
3614 /* Given a map A -> f(A), construct A -> -f(A).
3616 struct isl_map
*isl_map_neg(struct isl_map
*map
)
3620 map
= isl_map_cow(map
);
3624 for (i
= 0; i
< map
->n
; ++i
) {
3625 map
->p
[i
] = isl_basic_map_neg(map
->p
[i
]);
3636 __isl_give isl_set
*isl_set_neg(__isl_take isl_set
*set
)
3638 return (isl_set
*)isl_map_neg((isl_map
*)set
);
3641 /* Given a basic map A -> f(A) and an integer d, construct a basic map
3642 * A -> floor(f(A)/d).
3644 struct isl_basic_map
*isl_basic_map_floordiv(struct isl_basic_map
*bmap
,
3647 unsigned n_in
, n_out
, nparam
, total
, pos
;
3648 struct isl_basic_map
*result
= NULL
;
3649 struct isl_dim_map
*dim_map
;
3655 nparam
= isl_basic_map_n_param(bmap
);
3656 n_in
= isl_basic_map_n_in(bmap
);
3657 n_out
= isl_basic_map_n_out(bmap
);
3659 total
= nparam
+ n_in
+ n_out
+ bmap
->n_div
+ n_out
;
3660 dim_map
= isl_dim_map_alloc(bmap
->ctx
, total
);
3661 isl_dim_map_dim(dim_map
, bmap
->dim
, isl_dim_param
, pos
= 0);
3662 isl_dim_map_dim(dim_map
, bmap
->dim
, isl_dim_in
, pos
+= nparam
);
3663 isl_dim_map_div(dim_map
, bmap
, pos
+= n_in
+ n_out
);
3664 isl_dim_map_dim(dim_map
, bmap
->dim
, isl_dim_out
, pos
+= bmap
->n_div
);
3666 result
= isl_basic_map_alloc_space(isl_space_copy(bmap
->dim
),
3667 bmap
->n_div
+ n_out
,
3668 bmap
->n_eq
, bmap
->n_ineq
+ 2 * n_out
);
3669 result
= isl_basic_map_add_constraints_dim_map(result
, bmap
, dim_map
);
3670 result
= add_divs(result
, n_out
);
3671 for (i
= 0; i
< n_out
; ++i
) {
3673 j
= isl_basic_map_alloc_inequality(result
);
3676 isl_seq_clr(result
->ineq
[j
], 1+total
);
3677 isl_int_neg(result
->ineq
[j
][1+nparam
+n_in
+i
], d
);
3678 isl_int_set_si(result
->ineq
[j
][1+pos
+i
], 1);
3679 j
= isl_basic_map_alloc_inequality(result
);
3682 isl_seq_clr(result
->ineq
[j
], 1+total
);
3683 isl_int_set(result
->ineq
[j
][1+nparam
+n_in
+i
], d
);
3684 isl_int_set_si(result
->ineq
[j
][1+pos
+i
], -1);
3685 isl_int_sub_ui(result
->ineq
[j
][0], d
, 1);
3688 result
= isl_basic_map_simplify(result
);
3689 return isl_basic_map_finalize(result
);
3691 isl_basic_map_free(result
);
3695 /* Given a map A -> f(A) and an integer d, construct a map
3696 * A -> floor(f(A)/d).
3698 struct isl_map
*isl_map_floordiv(struct isl_map
*map
, isl_int d
)
3702 map
= isl_map_cow(map
);
3706 ISL_F_CLR(map
, ISL_MAP_DISJOINT
);
3707 ISL_F_CLR(map
, ISL_MAP_NORMALIZED
);
3708 for (i
= 0; i
< map
->n
; ++i
) {
3709 map
->p
[i
] = isl_basic_map_floordiv(map
->p
[i
], d
);
3720 static struct isl_basic_map
*var_equal(struct isl_basic_map
*bmap
, unsigned pos
)
3726 i
= isl_basic_map_alloc_equality(bmap
);
3729 nparam
= isl_basic_map_n_param(bmap
);
3730 n_in
= isl_basic_map_n_in(bmap
);
3731 isl_seq_clr(bmap
->eq
[i
], 1 + isl_basic_map_total_dim(bmap
));
3732 isl_int_set_si(bmap
->eq
[i
][1+nparam
+pos
], -1);
3733 isl_int_set_si(bmap
->eq
[i
][1+nparam
+n_in
+pos
], 1);
3734 return isl_basic_map_finalize(bmap
);
3736 isl_basic_map_free(bmap
);
3740 /* Add a constraints to "bmap" expressing i_pos < o_pos
3742 static struct isl_basic_map
*var_less(struct isl_basic_map
*bmap
, unsigned pos
)
3748 i
= isl_basic_map_alloc_inequality(bmap
);
3751 nparam
= isl_basic_map_n_param(bmap
);
3752 n_in
= isl_basic_map_n_in(bmap
);
3753 isl_seq_clr(bmap
->ineq
[i
], 1 + isl_basic_map_total_dim(bmap
));
3754 isl_int_set_si(bmap
->ineq
[i
][0], -1);
3755 isl_int_set_si(bmap
->ineq
[i
][1+nparam
+pos
], -1);
3756 isl_int_set_si(bmap
->ineq
[i
][1+nparam
+n_in
+pos
], 1);
3757 return isl_basic_map_finalize(bmap
);
3759 isl_basic_map_free(bmap
);
3763 /* Add a constraint to "bmap" expressing i_pos <= o_pos
3765 static __isl_give isl_basic_map
*var_less_or_equal(
3766 __isl_take isl_basic_map
*bmap
, unsigned pos
)
3772 i
= isl_basic_map_alloc_inequality(bmap
);
3775 nparam
= isl_basic_map_n_param(bmap
);
3776 n_in
= isl_basic_map_n_in(bmap
);
3777 isl_seq_clr(bmap
->ineq
[i
], 1 + isl_basic_map_total_dim(bmap
));
3778 isl_int_set_si(bmap
->ineq
[i
][1+nparam
+pos
], -1);
3779 isl_int_set_si(bmap
->ineq
[i
][1+nparam
+n_in
+pos
], 1);
3780 return isl_basic_map_finalize(bmap
);
3782 isl_basic_map_free(bmap
);
3786 /* Add a constraints to "bmap" expressing i_pos > o_pos
3788 static struct isl_basic_map
*var_more(struct isl_basic_map
*bmap
, unsigned pos
)
3794 i
= isl_basic_map_alloc_inequality(bmap
);
3797 nparam
= isl_basic_map_n_param(bmap
);
3798 n_in
= isl_basic_map_n_in(bmap
);
3799 isl_seq_clr(bmap
->ineq
[i
], 1 + isl_basic_map_total_dim(bmap
));
3800 isl_int_set_si(bmap
->ineq
[i
][0], -1);
3801 isl_int_set_si(bmap
->ineq
[i
][1+nparam
+pos
], 1);
3802 isl_int_set_si(bmap
->ineq
[i
][1+nparam
+n_in
+pos
], -1);
3803 return isl_basic_map_finalize(bmap
);
3805 isl_basic_map_free(bmap
);
3809 /* Add a constraint to "bmap" expressing i_pos >= o_pos
3811 static __isl_give isl_basic_map
*var_more_or_equal(
3812 __isl_take isl_basic_map
*bmap
, unsigned pos
)
3818 i
= isl_basic_map_alloc_inequality(bmap
);
3821 nparam
= isl_basic_map_n_param(bmap
);
3822 n_in
= isl_basic_map_n_in(bmap
);
3823 isl_seq_clr(bmap
->ineq
[i
], 1 + isl_basic_map_total_dim(bmap
));
3824 isl_int_set_si(bmap
->ineq
[i
][1+nparam
+pos
], 1);
3825 isl_int_set_si(bmap
->ineq
[i
][1+nparam
+n_in
+pos
], -1);
3826 return isl_basic_map_finalize(bmap
);
3828 isl_basic_map_free(bmap
);
3832 __isl_give isl_basic_map
*isl_basic_map_equal(
3833 __isl_take isl_space
*dim
, unsigned n_equal
)
3836 struct isl_basic_map
*bmap
;
3837 bmap
= isl_basic_map_alloc_space(dim
, 0, n_equal
, 0);
3840 for (i
= 0; i
< n_equal
&& bmap
; ++i
)
3841 bmap
= var_equal(bmap
, i
);
3842 return isl_basic_map_finalize(bmap
);
3845 /* Return a relation on of dimension "dim" expressing i_[0..pos] << o_[0..pos]
3847 __isl_give isl_basic_map
*isl_basic_map_less_at(__isl_take isl_space
*dim
,
3851 struct isl_basic_map
*bmap
;
3852 bmap
= isl_basic_map_alloc_space(dim
, 0, pos
, 1);
3855 for (i
= 0; i
< pos
&& bmap
; ++i
)
3856 bmap
= var_equal(bmap
, i
);
3858 bmap
= var_less(bmap
, pos
);
3859 return isl_basic_map_finalize(bmap
);
3862 /* Return a relation on of dimension "dim" expressing i_[0..pos] <<= o_[0..pos]
3864 __isl_give isl_basic_map
*isl_basic_map_less_or_equal_at(
3865 __isl_take isl_space
*dim
, unsigned pos
)
3868 isl_basic_map
*bmap
;
3870 bmap
= isl_basic_map_alloc_space(dim
, 0, pos
, 1);
3871 for (i
= 0; i
< pos
; ++i
)
3872 bmap
= var_equal(bmap
, i
);
3873 bmap
= var_less_or_equal(bmap
, pos
);
3874 return isl_basic_map_finalize(bmap
);
3877 /* Return a relation on pairs of sets of dimension "dim" expressing i_pos > o_pos
3879 __isl_give isl_basic_map
*isl_basic_map_more_at(__isl_take isl_space
*dim
,
3883 struct isl_basic_map
*bmap
;
3884 bmap
= isl_basic_map_alloc_space(dim
, 0, pos
, 1);
3887 for (i
= 0; i
< pos
&& bmap
; ++i
)
3888 bmap
= var_equal(bmap
, i
);
3890 bmap
= var_more(bmap
, pos
);
3891 return isl_basic_map_finalize(bmap
);
3894 /* Return a relation on of dimension "dim" expressing i_[0..pos] >>= o_[0..pos]
3896 __isl_give isl_basic_map
*isl_basic_map_more_or_equal_at(
3897 __isl_take isl_space
*dim
, unsigned pos
)
3900 isl_basic_map
*bmap
;
3902 bmap
= isl_basic_map_alloc_space(dim
, 0, pos
, 1);
3903 for (i
= 0; i
< pos
; ++i
)
3904 bmap
= var_equal(bmap
, i
);
3905 bmap
= var_more_or_equal(bmap
, pos
);
3906 return isl_basic_map_finalize(bmap
);
3909 static __isl_give isl_map
*map_lex_lte_first(__isl_take isl_space
*dims
,
3910 unsigned n
, int equal
)
3912 struct isl_map
*map
;
3915 if (n
== 0 && equal
)
3916 return isl_map_universe(dims
);
3918 map
= isl_map_alloc_space(isl_space_copy(dims
), n
, ISL_MAP_DISJOINT
);
3920 for (i
= 0; i
+ 1 < n
; ++i
)
3921 map
= isl_map_add_basic_map(map
,
3922 isl_basic_map_less_at(isl_space_copy(dims
), i
));
3925 map
= isl_map_add_basic_map(map
,
3926 isl_basic_map_less_or_equal_at(dims
, n
- 1));
3928 map
= isl_map_add_basic_map(map
,
3929 isl_basic_map_less_at(dims
, n
- 1));
3931 isl_space_free(dims
);
3936 static __isl_give isl_map
*map_lex_lte(__isl_take isl_space
*dims
, int equal
)
3940 return map_lex_lte_first(dims
, dims
->n_out
, equal
);
3943 __isl_give isl_map
*isl_map_lex_lt_first(__isl_take isl_space
*dim
, unsigned n
)
3945 return map_lex_lte_first(dim
, n
, 0);
3948 __isl_give isl_map
*isl_map_lex_le_first(__isl_take isl_space
*dim
, unsigned n
)
3950 return map_lex_lte_first(dim
, n
, 1);
3953 __isl_give isl_map
*isl_map_lex_lt(__isl_take isl_space
*set_dim
)
3955 return map_lex_lte(isl_space_map_from_set(set_dim
), 0);
3958 __isl_give isl_map
*isl_map_lex_le(__isl_take isl_space
*set_dim
)
3960 return map_lex_lte(isl_space_map_from_set(set_dim
), 1);
3963 static __isl_give isl_map
*map_lex_gte_first(__isl_take isl_space
*dims
,
3964 unsigned n
, int equal
)
3966 struct isl_map
*map
;
3969 if (n
== 0 && equal
)
3970 return isl_map_universe(dims
);
3972 map
= isl_map_alloc_space(isl_space_copy(dims
), n
, ISL_MAP_DISJOINT
);
3974 for (i
= 0; i
+ 1 < n
; ++i
)
3975 map
= isl_map_add_basic_map(map
,
3976 isl_basic_map_more_at(isl_space_copy(dims
), i
));
3979 map
= isl_map_add_basic_map(map
,
3980 isl_basic_map_more_or_equal_at(dims
, n
- 1));
3982 map
= isl_map_add_basic_map(map
,
3983 isl_basic_map_more_at(dims
, n
- 1));
3985 isl_space_free(dims
);
3990 static __isl_give isl_map
*map_lex_gte(__isl_take isl_space
*dims
, int equal
)
3994 return map_lex_gte_first(dims
, dims
->n_out
, equal
);
3997 __isl_give isl_map
*isl_map_lex_gt_first(__isl_take isl_space
*dim
, unsigned n
)
3999 return map_lex_gte_first(dim
, n
, 0);
4002 __isl_give isl_map
*isl_map_lex_ge_first(__isl_take isl_space
*dim
, unsigned n
)
4004 return map_lex_gte_first(dim
, n
, 1);
4007 __isl_give isl_map
*isl_map_lex_gt(__isl_take isl_space
*set_dim
)
4009 return map_lex_gte(isl_space_map_from_set(set_dim
), 0);
4012 __isl_give isl_map
*isl_map_lex_ge(__isl_take isl_space
*set_dim
)
4014 return map_lex_gte(isl_space_map_from_set(set_dim
), 1);
4017 __isl_give isl_map
*isl_set_lex_le_set(__isl_take isl_set
*set1
,
4018 __isl_take isl_set
*set2
)
4021 map
= isl_map_lex_le(isl_set_get_space(set1
));
4022 map
= isl_map_intersect_domain(map
, set1
);
4023 map
= isl_map_intersect_range(map
, set2
);
4027 __isl_give isl_map
*isl_set_lex_lt_set(__isl_take isl_set
*set1
,
4028 __isl_take isl_set
*set2
)
4031 map
= isl_map_lex_lt(isl_set_get_space(set1
));
4032 map
= isl_map_intersect_domain(map
, set1
);
4033 map
= isl_map_intersect_range(map
, set2
);
4037 __isl_give isl_map
*isl_set_lex_ge_set(__isl_take isl_set
*set1
,
4038 __isl_take isl_set
*set2
)
4041 map
= isl_map_lex_ge(isl_set_get_space(set1
));
4042 map
= isl_map_intersect_domain(map
, set1
);
4043 map
= isl_map_intersect_range(map
, set2
);
4047 __isl_give isl_map
*isl_set_lex_gt_set(__isl_take isl_set
*set1
,
4048 __isl_take isl_set
*set2
)
4051 map
= isl_map_lex_gt(isl_set_get_space(set1
));
4052 map
= isl_map_intersect_domain(map
, set1
);
4053 map
= isl_map_intersect_range(map
, set2
);
4057 __isl_give isl_map
*isl_map_lex_le_map(__isl_take isl_map
*map1
,
4058 __isl_take isl_map
*map2
)
4061 map
= isl_map_lex_le(isl_space_range(isl_map_get_space(map1
)));
4062 map
= isl_map_apply_domain(map
, isl_map_reverse(map1
));
4063 map
= isl_map_apply_range(map
, isl_map_reverse(map2
));
4067 __isl_give isl_map
*isl_map_lex_lt_map(__isl_take isl_map
*map1
,
4068 __isl_take isl_map
*map2
)
4071 map
= isl_map_lex_lt(isl_space_range(isl_map_get_space(map1
)));
4072 map
= isl_map_apply_domain(map
, isl_map_reverse(map1
));
4073 map
= isl_map_apply_range(map
, isl_map_reverse(map2
));
4077 __isl_give isl_map
*isl_map_lex_ge_map(__isl_take isl_map
*map1
,
4078 __isl_take isl_map
*map2
)
4081 map
= isl_map_lex_ge(isl_space_range(isl_map_get_space(map1
)));
4082 map
= isl_map_apply_domain(map
, isl_map_reverse(map1
));
4083 map
= isl_map_apply_range(map
, isl_map_reverse(map2
));
4087 __isl_give isl_map
*isl_map_lex_gt_map(__isl_take isl_map
*map1
,
4088 __isl_take isl_map
*map2
)
4091 map
= isl_map_lex_gt(isl_space_range(isl_map_get_space(map1
)));
4092 map
= isl_map_apply_domain(map
, isl_map_reverse(map1
));
4093 map
= isl_map_apply_range(map
, isl_map_reverse(map2
));
4097 __isl_give isl_basic_map
*isl_basic_map_from_basic_set(
4098 __isl_take isl_basic_set
*bset
, __isl_take isl_space
*dim
)
4100 struct isl_basic_map
*bmap
;
4102 bset
= isl_basic_set_cow(bset
);
4106 isl_assert(bset
->ctx
, isl_space_compatible(bset
->dim
, dim
), goto error
);
4107 isl_space_free(bset
->dim
);
4108 bmap
= (struct isl_basic_map
*) bset
;
4110 return isl_basic_map_finalize(bmap
);
4112 isl_basic_set_free(bset
);
4113 isl_space_free(dim
);
4117 struct isl_basic_set
*isl_basic_set_from_basic_map(struct isl_basic_map
*bmap
)
4121 if (bmap
->dim
->n_in
== 0)
4122 return (struct isl_basic_set
*)bmap
;
4123 bmap
= isl_basic_map_cow(bmap
);
4126 bmap
->dim
= isl_space_as_set_space(bmap
->dim
);
4129 bmap
= isl_basic_map_finalize(bmap
);
4130 return (struct isl_basic_set
*)bmap
;
4132 isl_basic_map_free(bmap
);
4136 /* For a div d = floor(f/m), add the constraints
4139 * -(f-(n-1)) + m d >= 0
4141 * Note that the second constraint is the negation of
4145 int isl_basic_map_add_div_constraints_var(__isl_keep isl_basic_map
*bmap
,
4146 unsigned pos
, isl_int
*div
)
4149 unsigned total
= isl_basic_map_total_dim(bmap
);
4151 i
= isl_basic_map_alloc_inequality(bmap
);
4154 isl_seq_cpy(bmap
->ineq
[i
], div
+ 1, 1 + total
);
4155 isl_int_neg(bmap
->ineq
[i
][1 + pos
], div
[0]);
4157 j
= isl_basic_map_alloc_inequality(bmap
);
4160 isl_seq_neg(bmap
->ineq
[j
], bmap
->ineq
[i
], 1 + total
);
4161 isl_int_add(bmap
->ineq
[j
][0], bmap
->ineq
[j
][0], bmap
->ineq
[j
][1 + pos
]);
4162 isl_int_sub_ui(bmap
->ineq
[j
][0], bmap
->ineq
[j
][0], 1);
4166 int isl_basic_set_add_div_constraints_var(__isl_keep isl_basic_set
*bset
,
4167 unsigned pos
, isl_int
*div
)
4169 return isl_basic_map_add_div_constraints_var((isl_basic_map
*)bset
,
4173 int isl_basic_map_add_div_constraints(struct isl_basic_map
*bmap
, unsigned div
)
4175 unsigned total
= isl_basic_map_total_dim(bmap
);
4176 unsigned div_pos
= total
- bmap
->n_div
+ div
;
4178 return isl_basic_map_add_div_constraints_var(bmap
, div_pos
,
4182 struct isl_basic_set
*isl_basic_map_underlying_set(
4183 struct isl_basic_map
*bmap
)
4187 if (bmap
->dim
->nparam
== 0 && bmap
->dim
->n_in
== 0 &&
4189 !isl_space_is_named_or_nested(bmap
->dim
, isl_dim_in
) &&
4190 !isl_space_is_named_or_nested(bmap
->dim
, isl_dim_out
))
4191 return (struct isl_basic_set
*)bmap
;
4192 bmap
= isl_basic_map_cow(bmap
);
4195 bmap
->dim
= isl_space_underlying(bmap
->dim
, bmap
->n_div
);
4198 bmap
->extra
-= bmap
->n_div
;
4200 bmap
= isl_basic_map_finalize(bmap
);
4201 return (struct isl_basic_set
*)bmap
;
4206 __isl_give isl_basic_set
*isl_basic_set_underlying_set(
4207 __isl_take isl_basic_set
*bset
)
4209 return isl_basic_map_underlying_set((isl_basic_map
*)bset
);
4212 struct isl_basic_map
*isl_basic_map_overlying_set(
4213 struct isl_basic_set
*bset
, struct isl_basic_map
*like
)
4215 struct isl_basic_map
*bmap
;
4216 struct isl_ctx
*ctx
;
4223 isl_assert(ctx
, bset
->n_div
== 0, goto error
);
4224 isl_assert(ctx
, isl_basic_set_n_param(bset
) == 0, goto error
);
4225 isl_assert(ctx
, bset
->dim
->n_out
== isl_basic_map_total_dim(like
),
4227 if (isl_space_is_equal(bset
->dim
, like
->dim
) && like
->n_div
== 0) {
4228 isl_basic_map_free(like
);
4229 return (struct isl_basic_map
*)bset
;
4231 bset
= isl_basic_set_cow(bset
);
4234 total
= bset
->dim
->n_out
+ bset
->extra
;
4235 bmap
= (struct isl_basic_map
*)bset
;
4236 isl_space_free(bmap
->dim
);
4237 bmap
->dim
= isl_space_copy(like
->dim
);
4240 bmap
->n_div
= like
->n_div
;
4241 bmap
->extra
+= like
->n_div
;
4245 ltotal
= total
- bmap
->extra
+ like
->extra
;
4248 bmap
->block2
= isl_blk_extend(ctx
, bmap
->block2
,
4249 bmap
->extra
* (1 + 1 + total
));
4250 if (isl_blk_is_error(bmap
->block2
))
4252 div
= isl_realloc_array(ctx
, bmap
->div
, isl_int
*, bmap
->extra
);
4256 for (i
= 0; i
< bmap
->extra
; ++i
)
4257 bmap
->div
[i
] = bmap
->block2
.data
+ i
* (1 + 1 + total
);
4258 for (i
= 0; i
< like
->n_div
; ++i
) {
4259 isl_seq_cpy(bmap
->div
[i
], like
->div
[i
], 1 + 1 + ltotal
);
4260 isl_seq_clr(bmap
->div
[i
]+1+1+ltotal
, total
- ltotal
);
4262 bmap
= isl_basic_map_extend_constraints(bmap
,
4263 0, 2 * like
->n_div
);
4264 for (i
= 0; i
< like
->n_div
; ++i
) {
4265 if (isl_int_is_zero(bmap
->div
[i
][0]))
4267 if (isl_basic_map_add_div_constraints(bmap
, i
) < 0)
4271 isl_basic_map_free(like
);
4272 bmap
= isl_basic_map_simplify(bmap
);
4273 bmap
= isl_basic_map_finalize(bmap
);
4276 isl_basic_map_free(like
);
4277 isl_basic_set_free(bset
);
4281 struct isl_basic_set
*isl_basic_set_from_underlying_set(
4282 struct isl_basic_set
*bset
, struct isl_basic_set
*like
)
4284 return (struct isl_basic_set
*)
4285 isl_basic_map_overlying_set(bset
, (struct isl_basic_map
*)like
);
4288 struct isl_set
*isl_set_from_underlying_set(
4289 struct isl_set
*set
, struct isl_basic_set
*like
)
4295 isl_assert(set
->ctx
, set
->dim
->n_out
== isl_basic_set_total_dim(like
),
4297 if (isl_space_is_equal(set
->dim
, like
->dim
) && like
->n_div
== 0) {
4298 isl_basic_set_free(like
);
4301 set
= isl_set_cow(set
);
4304 for (i
= 0; i
< set
->n
; ++i
) {
4305 set
->p
[i
] = isl_basic_set_from_underlying_set(set
->p
[i
],
4306 isl_basic_set_copy(like
));
4310 isl_space_free(set
->dim
);
4311 set
->dim
= isl_space_copy(like
->dim
);
4314 isl_basic_set_free(like
);
4317 isl_basic_set_free(like
);
4322 struct isl_set
*isl_map_underlying_set(struct isl_map
*map
)
4326 map
= isl_map_cow(map
);
4329 map
->dim
= isl_space_cow(map
->dim
);
4333 for (i
= 1; i
< map
->n
; ++i
)
4334 isl_assert(map
->ctx
, map
->p
[0]->n_div
== map
->p
[i
]->n_div
,
4336 for (i
= 0; i
< map
->n
; ++i
) {
4337 map
->p
[i
] = (struct isl_basic_map
*)
4338 isl_basic_map_underlying_set(map
->p
[i
]);
4343 map
->dim
= isl_space_underlying(map
->dim
, 0);
4345 isl_space_free(map
->dim
);
4346 map
->dim
= isl_space_copy(map
->p
[0]->dim
);
4350 return (struct isl_set
*)map
;
4356 struct isl_set
*isl_set_to_underlying_set(struct isl_set
*set
)
4358 return (struct isl_set
*)isl_map_underlying_set((struct isl_map
*)set
);
4361 __isl_give isl_basic_map
*isl_basic_map_reset_space(
4362 __isl_take isl_basic_map
*bmap
, __isl_take isl_space
*dim
)
4364 bmap
= isl_basic_map_cow(bmap
);
4368 isl_space_free(bmap
->dim
);
4371 bmap
= isl_basic_map_finalize(bmap
);
4375 isl_basic_map_free(bmap
);
4376 isl_space_free(dim
);
4380 __isl_give isl_basic_set
*isl_basic_set_reset_space(
4381 __isl_take isl_basic_set
*bset
, __isl_take isl_space
*dim
)
4383 return (isl_basic_set
*)isl_basic_map_reset_space((isl_basic_map
*)bset
,
4387 __isl_give isl_map
*isl_map_reset_space(__isl_take isl_map
*map
,
4388 __isl_take isl_space
*dim
)
4392 map
= isl_map_cow(map
);
4396 for (i
= 0; i
< map
->n
; ++i
) {
4397 map
->p
[i
] = isl_basic_map_reset_space(map
->p
[i
],
4398 isl_space_copy(dim
));
4402 isl_space_free(map
->dim
);
4408 isl_space_free(dim
);
4412 __isl_give isl_set
*isl_set_reset_space(__isl_take isl_set
*set
,
4413 __isl_take isl_space
*dim
)
4415 return (struct isl_set
*) isl_map_reset_space((struct isl_map
*)set
, dim
);
4418 /* Compute the parameter domain of the given basic set.
4420 __isl_give isl_basic_set
*isl_basic_set_params(__isl_take isl_basic_set
*bset
)
4425 if (isl_basic_set_is_params(bset
))
4428 n
= isl_basic_set_dim(bset
, isl_dim_set
);
4429 bset
= isl_basic_set_project_out(bset
, isl_dim_set
, 0, n
);
4430 space
= isl_basic_set_get_space(bset
);
4431 space
= isl_space_params(space
);
4432 bset
= isl_basic_set_reset_space(bset
, space
);
4436 /* Compute the parameter domain of the given set.
4438 __isl_give isl_set
*isl_set_params(__isl_take isl_set
*set
)
4443 if (isl_set_is_params(set
))
4446 n
= isl_set_dim(set
, isl_dim_set
);
4447 set
= isl_set_project_out(set
, isl_dim_set
, 0, n
);
4448 space
= isl_set_get_space(set
);
4449 space
= isl_space_params(space
);
4450 set
= isl_set_reset_space(set
, space
);
4454 /* Construct a zero-dimensional set with the given parameter domain.
4456 __isl_give isl_set
*isl_set_from_params(__isl_take isl_set
*set
)
4459 space
= isl_set_get_space(set
);
4460 space
= isl_space_set_from_params(space
);
4461 set
= isl_set_reset_space(set
, space
);
4465 /* Compute the parameter domain of the given map.
4467 __isl_give isl_set
*isl_map_params(__isl_take isl_map
*map
)
4472 n
= isl_map_dim(map
, isl_dim_in
);
4473 map
= isl_map_project_out(map
, isl_dim_in
, 0, n
);
4474 n
= isl_map_dim(map
, isl_dim_out
);
4475 map
= isl_map_project_out(map
, isl_dim_out
, 0, n
);
4476 space
= isl_map_get_space(map
);
4477 space
= isl_space_params(space
);
4478 map
= isl_map_reset_space(map
, space
);
4482 struct isl_basic_set
*isl_basic_map_domain(struct isl_basic_map
*bmap
)
4485 struct isl_basic_set
*domain
;
4491 dim
= isl_space_domain(isl_basic_map_get_space(bmap
));
4493 n_in
= isl_basic_map_n_in(bmap
);
4494 n_out
= isl_basic_map_n_out(bmap
);
4495 domain
= isl_basic_set_from_basic_map(bmap
);
4496 domain
= isl_basic_set_project_out(domain
, isl_dim_set
, n_in
, n_out
);
4498 domain
= isl_basic_set_reset_space(domain
, dim
);
4503 int isl_basic_map_may_be_set(__isl_keep isl_basic_map
*bmap
)
4507 return isl_space_may_be_set(bmap
->dim
);
4510 /* Is this basic map actually a set?
4511 * Users should never call this function. Outside of isl,
4512 * the type should indicate whether something is a set or a map.
4514 int isl_basic_map_is_set(__isl_keep isl_basic_map
*bmap
)
4518 return isl_space_is_set(bmap
->dim
);
4521 struct isl_basic_set
*isl_basic_map_range(struct isl_basic_map
*bmap
)
4525 if (isl_basic_map_is_set(bmap
))
4527 return isl_basic_map_domain(isl_basic_map_reverse(bmap
));
4530 __isl_give isl_basic_map
*isl_basic_map_domain_map(
4531 __isl_take isl_basic_map
*bmap
)
4535 isl_basic_map
*domain
;
4536 int nparam
, n_in
, n_out
;
4539 nparam
= isl_basic_map_dim(bmap
, isl_dim_param
);
4540 n_in
= isl_basic_map_dim(bmap
, isl_dim_in
);
4541 n_out
= isl_basic_map_dim(bmap
, isl_dim_out
);
4543 dim
= isl_space_from_range(isl_space_domain(isl_basic_map_get_space(bmap
)));
4544 domain
= isl_basic_map_universe(dim
);
4546 bmap
= isl_basic_map_from_domain(isl_basic_map_wrap(bmap
));
4547 bmap
= isl_basic_map_apply_range(bmap
, domain
);
4548 bmap
= isl_basic_map_extend_constraints(bmap
, n_in
, 0);
4550 total
= isl_basic_map_total_dim(bmap
);
4552 for (i
= 0; i
< n_in
; ++i
) {
4553 k
= isl_basic_map_alloc_equality(bmap
);
4556 isl_seq_clr(bmap
->eq
[k
], 1 + total
);
4557 isl_int_set_si(bmap
->eq
[k
][1 + nparam
+ i
], -1);
4558 isl_int_set_si(bmap
->eq
[k
][1 + nparam
+ n_in
+ n_out
+ i
], 1);
4561 bmap
= isl_basic_map_gauss(bmap
, NULL
);
4562 return isl_basic_map_finalize(bmap
);
4564 isl_basic_map_free(bmap
);
4568 __isl_give isl_basic_map
*isl_basic_map_range_map(
4569 __isl_take isl_basic_map
*bmap
)
4573 isl_basic_map
*range
;
4574 int nparam
, n_in
, n_out
;
4577 nparam
= isl_basic_map_dim(bmap
, isl_dim_param
);
4578 n_in
= isl_basic_map_dim(bmap
, isl_dim_in
);
4579 n_out
= isl_basic_map_dim(bmap
, isl_dim_out
);
4581 dim
= isl_space_from_range(isl_space_range(isl_basic_map_get_space(bmap
)));
4582 range
= isl_basic_map_universe(dim
);
4584 bmap
= isl_basic_map_from_domain(isl_basic_map_wrap(bmap
));
4585 bmap
= isl_basic_map_apply_range(bmap
, range
);
4586 bmap
= isl_basic_map_extend_constraints(bmap
, n_out
, 0);
4588 total
= isl_basic_map_total_dim(bmap
);
4590 for (i
= 0; i
< n_out
; ++i
) {
4591 k
= isl_basic_map_alloc_equality(bmap
);
4594 isl_seq_clr(bmap
->eq
[k
], 1 + total
);
4595 isl_int_set_si(bmap
->eq
[k
][1 + nparam
+ n_in
+ i
], -1);
4596 isl_int_set_si(bmap
->eq
[k
][1 + nparam
+ n_in
+ n_out
+ i
], 1);
4599 bmap
= isl_basic_map_gauss(bmap
, NULL
);
4600 return isl_basic_map_finalize(bmap
);
4602 isl_basic_map_free(bmap
);
4606 int isl_map_may_be_set(__isl_keep isl_map
*map
)
4610 return isl_space_may_be_set(map
->dim
);
4613 /* Is this map actually a set?
4614 * Users should never call this function. Outside of isl,
4615 * the type should indicate whether something is a set or a map.
4617 int isl_map_is_set(__isl_keep isl_map
*map
)
4621 return isl_space_is_set(map
->dim
);
4624 struct isl_set
*isl_map_range(struct isl_map
*map
)
4627 struct isl_set
*set
;
4631 if (isl_map_is_set(map
))
4632 return (isl_set
*)map
;
4634 map
= isl_map_cow(map
);
4638 set
= (struct isl_set
*) map
;
4639 set
->dim
= isl_space_range(set
->dim
);
4642 for (i
= 0; i
< map
->n
; ++i
) {
4643 set
->p
[i
] = isl_basic_map_range(map
->p
[i
]);
4647 ISL_F_CLR(set
, ISL_MAP_DISJOINT
);
4648 ISL_F_CLR(set
, ISL_SET_NORMALIZED
);
4655 __isl_give isl_map
*isl_map_domain_map(__isl_take isl_map
*map
)
4658 isl_space
*domain_dim
;
4660 map
= isl_map_cow(map
);
4664 domain_dim
= isl_space_from_range(isl_space_domain(isl_map_get_space(map
)));
4665 map
->dim
= isl_space_from_domain(isl_space_wrap(map
->dim
));
4666 map
->dim
= isl_space_join(map
->dim
, domain_dim
);
4669 for (i
= 0; i
< map
->n
; ++i
) {
4670 map
->p
[i
] = isl_basic_map_domain_map(map
->p
[i
]);
4674 ISL_F_CLR(map
, ISL_MAP_DISJOINT
);
4675 ISL_F_CLR(map
, ISL_MAP_NORMALIZED
);
4682 __isl_give isl_map
*isl_map_range_map(__isl_take isl_map
*map
)
4685 isl_space
*range_dim
;
4687 map
= isl_map_cow(map
);
4691 range_dim
= isl_space_range(isl_map_get_space(map
));
4692 range_dim
= isl_space_from_range(range_dim
);
4693 map
->dim
= isl_space_from_domain(isl_space_wrap(map
->dim
));
4694 map
->dim
= isl_space_join(map
->dim
, range_dim
);
4697 for (i
= 0; i
< map
->n
; ++i
) {
4698 map
->p
[i
] = isl_basic_map_range_map(map
->p
[i
]);
4702 ISL_F_CLR(map
, ISL_MAP_DISJOINT
);
4703 ISL_F_CLR(map
, ISL_MAP_NORMALIZED
);
4710 __isl_give isl_map
*isl_map_from_set(__isl_take isl_set
*set
,
4711 __isl_take isl_space
*dim
)
4714 struct isl_map
*map
= NULL
;
4716 set
= isl_set_cow(set
);
4719 isl_assert(set
->ctx
, isl_space_compatible(set
->dim
, dim
), goto error
);
4720 map
= (struct isl_map
*)set
;
4721 for (i
= 0; i
< set
->n
; ++i
) {
4722 map
->p
[i
] = isl_basic_map_from_basic_set(
4723 set
->p
[i
], isl_space_copy(dim
));
4727 isl_space_free(map
->dim
);
4731 isl_space_free(dim
);
4736 __isl_give isl_basic_map
*isl_basic_map_from_domain(
4737 __isl_take isl_basic_set
*bset
)
4739 return isl_basic_map_reverse(isl_basic_map_from_range(bset
));
4742 __isl_give isl_basic_map
*isl_basic_map_from_range(
4743 __isl_take isl_basic_set
*bset
)
4746 space
= isl_basic_set_get_space(bset
);
4747 space
= isl_space_from_range(space
);
4748 bset
= isl_basic_set_reset_space(bset
, space
);
4749 return (isl_basic_map
*)bset
;
4752 struct isl_map
*isl_map_from_range(struct isl_set
*set
)
4755 space
= isl_set_get_space(set
);
4756 space
= isl_space_from_range(space
);
4757 set
= isl_set_reset_space(set
, space
);
4758 return (struct isl_map
*)set
;
4761 __isl_give isl_map
*isl_map_from_domain(__isl_take isl_set
*set
)
4763 return isl_map_reverse(isl_map_from_range(set
));
4766 __isl_give isl_basic_map
*isl_basic_map_from_domain_and_range(
4767 __isl_take isl_basic_set
*domain
, __isl_take isl_basic_set
*range
)
4769 return isl_basic_map_apply_range(isl_basic_map_reverse(domain
), range
);
4772 __isl_give isl_map
*isl_map_from_domain_and_range(__isl_take isl_set
*domain
,
4773 __isl_take isl_set
*range
)
4775 return isl_map_apply_range(isl_map_reverse(domain
), range
);
4778 struct isl_set
*isl_set_from_map(struct isl_map
*map
)
4781 struct isl_set
*set
= NULL
;
4785 map
= isl_map_cow(map
);
4788 map
->dim
= isl_space_as_set_space(map
->dim
);
4791 set
= (struct isl_set
*)map
;
4792 for (i
= 0; i
< map
->n
; ++i
) {
4793 set
->p
[i
] = isl_basic_set_from_basic_map(map
->p
[i
]);
4803 __isl_give isl_map
*isl_map_alloc_space(__isl_take isl_space
*dim
, int n
,
4806 struct isl_map
*map
;
4811 isl_die(dim
->ctx
, isl_error_internal
,
4812 "negative number of basic maps", goto error
);
4813 map
= isl_alloc(dim
->ctx
, struct isl_map
,
4814 sizeof(struct isl_map
) +
4815 (n
- 1) * sizeof(struct isl_basic_map
*));
4819 map
->ctx
= dim
->ctx
;
4820 isl_ctx_ref(map
->ctx
);
4828 isl_space_free(dim
);
4832 struct isl_map
*isl_map_alloc(struct isl_ctx
*ctx
,
4833 unsigned nparam
, unsigned in
, unsigned out
, int n
,
4836 struct isl_map
*map
;
4839 dims
= isl_space_alloc(ctx
, nparam
, in
, out
);
4843 map
= isl_map_alloc_space(dims
, n
, flags
);
4847 __isl_give isl_basic_map
*isl_basic_map_empty(__isl_take isl_space
*dim
)
4849 struct isl_basic_map
*bmap
;
4850 bmap
= isl_basic_map_alloc_space(dim
, 0, 1, 0);
4851 bmap
= isl_basic_map_set_to_empty(bmap
);
4855 __isl_give isl_basic_set
*isl_basic_set_empty(__isl_take isl_space
*dim
)
4857 struct isl_basic_set
*bset
;
4858 bset
= isl_basic_set_alloc_space(dim
, 0, 1, 0);
4859 bset
= isl_basic_set_set_to_empty(bset
);
4863 struct isl_basic_map
*isl_basic_map_empty_like(struct isl_basic_map
*model
)
4865 struct isl_basic_map
*bmap
;
4868 bmap
= isl_basic_map_alloc_space(isl_space_copy(model
->dim
), 0, 1, 0);
4869 bmap
= isl_basic_map_set_to_empty(bmap
);
4873 struct isl_basic_map
*isl_basic_map_empty_like_map(struct isl_map
*model
)
4875 struct isl_basic_map
*bmap
;
4878 bmap
= isl_basic_map_alloc_space(isl_space_copy(model
->dim
), 0, 1, 0);
4879 bmap
= isl_basic_map_set_to_empty(bmap
);
4883 struct isl_basic_set
*isl_basic_set_empty_like(struct isl_basic_set
*model
)
4885 struct isl_basic_set
*bset
;
4888 bset
= isl_basic_set_alloc_space(isl_space_copy(model
->dim
), 0, 1, 0);
4889 bset
= isl_basic_set_set_to_empty(bset
);
4893 __isl_give isl_basic_map
*isl_basic_map_universe(__isl_take isl_space
*dim
)
4895 struct isl_basic_map
*bmap
;
4896 bmap
= isl_basic_map_alloc_space(dim
, 0, 0, 0);
4897 bmap
= isl_basic_map_finalize(bmap
);
4901 __isl_give isl_basic_set
*isl_basic_set_universe(__isl_take isl_space
*dim
)
4903 struct isl_basic_set
*bset
;
4904 bset
= isl_basic_set_alloc_space(dim
, 0, 0, 0);
4905 bset
= isl_basic_set_finalize(bset
);
4909 __isl_give isl_basic_map
*isl_basic_map_nat_universe(__isl_take isl_space
*dim
)
4912 unsigned total
= isl_space_dim(dim
, isl_dim_all
);
4913 isl_basic_map
*bmap
;
4915 bmap
= isl_basic_map_alloc_space(dim
, 0, 0, total
);
4916 for (i
= 0; i
< total
; ++i
) {
4917 int k
= isl_basic_map_alloc_inequality(bmap
);
4920 isl_seq_clr(bmap
->ineq
[k
], 1 + total
);
4921 isl_int_set_si(bmap
->ineq
[k
][1 + i
], 1);
4925 isl_basic_map_free(bmap
);
4929 __isl_give isl_basic_set
*isl_basic_set_nat_universe(__isl_take isl_space
*dim
)
4931 return isl_basic_map_nat_universe(dim
);
4934 __isl_give isl_map
*isl_map_nat_universe(__isl_take isl_space
*dim
)
4936 return isl_map_from_basic_map(isl_basic_map_nat_universe(dim
));
4939 __isl_give isl_set
*isl_set_nat_universe(__isl_take isl_space
*dim
)
4941 return isl_map_nat_universe(dim
);
4944 __isl_give isl_basic_map
*isl_basic_map_universe_like(
4945 __isl_keep isl_basic_map
*model
)
4949 return isl_basic_map_alloc_space(isl_space_copy(model
->dim
), 0, 0, 0);
4952 struct isl_basic_set
*isl_basic_set_universe_like(struct isl_basic_set
*model
)
4956 return isl_basic_set_alloc_space(isl_space_copy(model
->dim
), 0, 0, 0);
4959 __isl_give isl_basic_set
*isl_basic_set_universe_like_set(
4960 __isl_keep isl_set
*model
)
4964 return isl_basic_set_alloc_space(isl_space_copy(model
->dim
), 0, 0, 0);
4967 __isl_give isl_map
*isl_map_empty(__isl_take isl_space
*dim
)
4969 return isl_map_alloc_space(dim
, 0, ISL_MAP_DISJOINT
);
4972 struct isl_map
*isl_map_empty_like(struct isl_map
*model
)
4976 return isl_map_alloc_space(isl_space_copy(model
->dim
), 0, ISL_MAP_DISJOINT
);
4979 struct isl_map
*isl_map_empty_like_basic_map(struct isl_basic_map
*model
)
4983 return isl_map_alloc_space(isl_space_copy(model
->dim
), 0, ISL_MAP_DISJOINT
);
4986 __isl_give isl_set
*isl_set_empty(__isl_take isl_space
*dim
)
4988 return isl_set_alloc_space(dim
, 0, ISL_MAP_DISJOINT
);
4991 struct isl_set
*isl_set_empty_like(struct isl_set
*model
)
4995 return isl_set_empty(isl_space_copy(model
->dim
));
4998 __isl_give isl_map
*isl_map_universe(__isl_take isl_space
*dim
)
5000 struct isl_map
*map
;
5003 map
= isl_map_alloc_space(isl_space_copy(dim
), 1, ISL_MAP_DISJOINT
);
5004 map
= isl_map_add_basic_map(map
, isl_basic_map_universe(dim
));
5008 __isl_give isl_set
*isl_set_universe(__isl_take isl_space
*dim
)
5010 struct isl_set
*set
;
5013 set
= isl_set_alloc_space(isl_space_copy(dim
), 1, ISL_MAP_DISJOINT
);
5014 set
= isl_set_add_basic_set(set
, isl_basic_set_universe(dim
));
5018 __isl_give isl_set
*isl_set_universe_like(__isl_keep isl_set
*model
)
5022 return isl_set_universe(isl_space_copy(model
->dim
));
5025 struct isl_map
*isl_map_dup(struct isl_map
*map
)
5028 struct isl_map
*dup
;
5032 dup
= isl_map_alloc_space(isl_space_copy(map
->dim
), map
->n
, map
->flags
);
5033 for (i
= 0; i
< map
->n
; ++i
)
5034 dup
= isl_map_add_basic_map(dup
, isl_basic_map_copy(map
->p
[i
]));
5038 __isl_give isl_map
*isl_map_add_basic_map(__isl_take isl_map
*map
,
5039 __isl_take isl_basic_map
*bmap
)
5043 if (isl_basic_map_plain_is_empty(bmap
)) {
5044 isl_basic_map_free(bmap
);
5047 isl_assert(map
->ctx
, isl_space_is_equal(map
->dim
, bmap
->dim
), goto error
);
5048 isl_assert(map
->ctx
, map
->n
< map
->size
, goto error
);
5049 map
->p
[map
->n
] = bmap
;
5051 ISL_F_CLR(map
, ISL_MAP_NORMALIZED
);
5057 isl_basic_map_free(bmap
);
5061 void isl_map_free(struct isl_map
*map
)
5071 isl_ctx_deref(map
->ctx
);
5072 for (i
= 0; i
< map
->n
; ++i
)
5073 isl_basic_map_free(map
->p
[i
]);
5074 isl_space_free(map
->dim
);
5078 struct isl_map
*isl_map_extend(struct isl_map
*base
,
5079 unsigned nparam
, unsigned n_in
, unsigned n_out
)
5083 base
= isl_map_cow(base
);
5087 base
->dim
= isl_space_extend(base
->dim
, nparam
, n_in
, n_out
);
5090 for (i
= 0; i
< base
->n
; ++i
) {
5091 base
->p
[i
] = isl_basic_map_extend_space(base
->p
[i
],
5092 isl_space_copy(base
->dim
), 0, 0, 0);
5102 struct isl_set
*isl_set_extend(struct isl_set
*base
,
5103 unsigned nparam
, unsigned dim
)
5105 return (struct isl_set
*)isl_map_extend((struct isl_map
*)base
,
5109 static struct isl_basic_map
*isl_basic_map_fix_pos_si(
5110 struct isl_basic_map
*bmap
, unsigned pos
, int value
)
5114 bmap
= isl_basic_map_cow(bmap
);
5115 bmap
= isl_basic_map_extend_constraints(bmap
, 1, 0);
5116 j
= isl_basic_map_alloc_equality(bmap
);
5119 isl_seq_clr(bmap
->eq
[j
] + 1, isl_basic_map_total_dim(bmap
));
5120 isl_int_set_si(bmap
->eq
[j
][pos
], -1);
5121 isl_int_set_si(bmap
->eq
[j
][0], value
);
5122 bmap
= isl_basic_map_simplify(bmap
);
5123 return isl_basic_map_finalize(bmap
);
5125 isl_basic_map_free(bmap
);
5129 static __isl_give isl_basic_map
*isl_basic_map_fix_pos(
5130 __isl_take isl_basic_map
*bmap
, unsigned pos
, isl_int value
)
5134 bmap
= isl_basic_map_cow(bmap
);
5135 bmap
= isl_basic_map_extend_constraints(bmap
, 1, 0);
5136 j
= isl_basic_map_alloc_equality(bmap
);
5139 isl_seq_clr(bmap
->eq
[j
] + 1, isl_basic_map_total_dim(bmap
));
5140 isl_int_set_si(bmap
->eq
[j
][pos
], -1);
5141 isl_int_set(bmap
->eq
[j
][0], value
);
5142 bmap
= isl_basic_map_simplify(bmap
);
5143 return isl_basic_map_finalize(bmap
);
5145 isl_basic_map_free(bmap
);
5149 struct isl_basic_map
*isl_basic_map_fix_si(struct isl_basic_map
*bmap
,
5150 enum isl_dim_type type
, unsigned pos
, int value
)
5154 isl_assert(bmap
->ctx
, pos
< isl_basic_map_dim(bmap
, type
), goto error
);
5155 return isl_basic_map_fix_pos_si(bmap
,
5156 isl_basic_map_offset(bmap
, type
) + pos
, value
);
5158 isl_basic_map_free(bmap
);
5162 __isl_give isl_basic_map
*isl_basic_map_fix(__isl_take isl_basic_map
*bmap
,
5163 enum isl_dim_type type
, unsigned pos
, isl_int value
)
5167 isl_assert(bmap
->ctx
, pos
< isl_basic_map_dim(bmap
, type
), goto error
);
5168 return isl_basic_map_fix_pos(bmap
,
5169 isl_basic_map_offset(bmap
, type
) + pos
, value
);
5171 isl_basic_map_free(bmap
);
5175 struct isl_basic_set
*isl_basic_set_fix_si(struct isl_basic_set
*bset
,
5176 enum isl_dim_type type
, unsigned pos
, int value
)
5178 return (struct isl_basic_set
*)
5179 isl_basic_map_fix_si((struct isl_basic_map
*)bset
,
5183 __isl_give isl_basic_set
*isl_basic_set_fix(__isl_take isl_basic_set
*bset
,
5184 enum isl_dim_type type
, unsigned pos
, isl_int value
)
5186 return (struct isl_basic_set
*)
5187 isl_basic_map_fix((struct isl_basic_map
*)bset
,
5191 struct isl_basic_map
*isl_basic_map_fix_input_si(struct isl_basic_map
*bmap
,
5192 unsigned input
, int value
)
5194 return isl_basic_map_fix_si(bmap
, isl_dim_in
, input
, value
);
5197 struct isl_basic_set
*isl_basic_set_fix_dim_si(struct isl_basic_set
*bset
,
5198 unsigned dim
, int value
)
5200 return (struct isl_basic_set
*)
5201 isl_basic_map_fix_si((struct isl_basic_map
*)bset
,
5202 isl_dim_set
, dim
, value
);
5205 static int remove_if_empty(__isl_keep isl_map
*map
, int i
)
5207 int empty
= isl_basic_map_plain_is_empty(map
->p
[i
]);
5214 isl_basic_map_free(map
->p
[i
]);
5215 if (i
!= map
->n
- 1) {
5216 ISL_F_CLR(map
, ISL_MAP_NORMALIZED
);
5217 map
->p
[i
] = map
->p
[map
->n
- 1];
5224 struct isl_map
*isl_map_fix_si(struct isl_map
*map
,
5225 enum isl_dim_type type
, unsigned pos
, int value
)
5229 map
= isl_map_cow(map
);
5233 isl_assert(map
->ctx
, pos
< isl_map_dim(map
, type
), goto error
);
5234 for (i
= map
->n
- 1; i
>= 0; --i
) {
5235 map
->p
[i
] = isl_basic_map_fix_si(map
->p
[i
], type
, pos
, value
);
5236 if (remove_if_empty(map
, i
) < 0)
5239 ISL_F_CLR(map
, ISL_MAP_NORMALIZED
);
5246 __isl_give isl_set
*isl_set_fix_si(__isl_take isl_set
*set
,
5247 enum isl_dim_type type
, unsigned pos
, int value
)
5249 return (struct isl_set
*)
5250 isl_map_fix_si((struct isl_map
*)set
, type
, pos
, value
);
5253 __isl_give isl_map
*isl_map_fix(__isl_take isl_map
*map
,
5254 enum isl_dim_type type
, unsigned pos
, isl_int value
)
5258 map
= isl_map_cow(map
);
5262 isl_assert(map
->ctx
, pos
< isl_map_dim(map
, type
), goto error
);
5263 for (i
= 0; i
< map
->n
; ++i
) {
5264 map
->p
[i
] = isl_basic_map_fix(map
->p
[i
], type
, pos
, value
);
5268 ISL_F_CLR(map
, ISL_MAP_NORMALIZED
);
5275 __isl_give isl_set
*isl_set_fix(__isl_take isl_set
*set
,
5276 enum isl_dim_type type
, unsigned pos
, isl_int value
)
5278 return (struct isl_set
*)isl_map_fix((isl_map
*)set
, type
, pos
, value
);
5281 struct isl_map
*isl_map_fix_input_si(struct isl_map
*map
,
5282 unsigned input
, int value
)
5284 return isl_map_fix_si(map
, isl_dim_in
, input
, value
);
5287 struct isl_set
*isl_set_fix_dim_si(struct isl_set
*set
, unsigned dim
, int value
)
5289 return (struct isl_set
*)
5290 isl_map_fix_si((struct isl_map
*)set
, isl_dim_set
, dim
, value
);
5293 static __isl_give isl_basic_map
*basic_map_bound_si(
5294 __isl_take isl_basic_map
*bmap
,
5295 enum isl_dim_type type
, unsigned pos
, int value
, int upper
)
5301 isl_assert(bmap
->ctx
, pos
< isl_basic_map_dim(bmap
, type
), goto error
);
5302 pos
+= isl_basic_map_offset(bmap
, type
);
5303 bmap
= isl_basic_map_cow(bmap
);
5304 bmap
= isl_basic_map_extend_constraints(bmap
, 0, 1);
5305 j
= isl_basic_map_alloc_inequality(bmap
);
5308 isl_seq_clr(bmap
->ineq
[j
], 1 + isl_basic_map_total_dim(bmap
));
5310 isl_int_set_si(bmap
->ineq
[j
][pos
], -1);
5311 isl_int_set_si(bmap
->ineq
[j
][0], value
);
5313 isl_int_set_si(bmap
->ineq
[j
][pos
], 1);
5314 isl_int_set_si(bmap
->ineq
[j
][0], -value
);
5316 bmap
= isl_basic_map_simplify(bmap
);
5317 return isl_basic_map_finalize(bmap
);
5319 isl_basic_map_free(bmap
);
5323 __isl_give isl_basic_map
*isl_basic_map_lower_bound_si(
5324 __isl_take isl_basic_map
*bmap
,
5325 enum isl_dim_type type
, unsigned pos
, int value
)
5327 return basic_map_bound_si(bmap
, type
, pos
, value
, 0);
5330 struct isl_basic_set
*isl_basic_set_lower_bound_dim(struct isl_basic_set
*bset
,
5331 unsigned dim
, isl_int value
)
5335 bset
= isl_basic_set_cow(bset
);
5336 bset
= isl_basic_set_extend_constraints(bset
, 0, 1);
5337 j
= isl_basic_set_alloc_inequality(bset
);
5340 isl_seq_clr(bset
->ineq
[j
], 1 + isl_basic_set_total_dim(bset
));
5341 isl_int_set_si(bset
->ineq
[j
][1 + isl_basic_set_n_param(bset
) + dim
], 1);
5342 isl_int_neg(bset
->ineq
[j
][0], value
);
5343 bset
= isl_basic_set_simplify(bset
);
5344 return isl_basic_set_finalize(bset
);
5346 isl_basic_set_free(bset
);
5350 static __isl_give isl_map
*map_bound_si(__isl_take isl_map
*map
,
5351 enum isl_dim_type type
, unsigned pos
, int value
, int upper
)
5355 map
= isl_map_cow(map
);
5359 isl_assert(map
->ctx
, pos
< isl_map_dim(map
, type
), goto error
);
5360 for (i
= 0; i
< map
->n
; ++i
) {
5361 map
->p
[i
] = basic_map_bound_si(map
->p
[i
],
5362 type
, pos
, value
, upper
);
5366 ISL_F_CLR(map
, ISL_MAP_NORMALIZED
);
5373 __isl_give isl_map
*isl_map_lower_bound_si(__isl_take isl_map
*map
,
5374 enum isl_dim_type type
, unsigned pos
, int value
)
5376 return map_bound_si(map
, type
, pos
, value
, 0);
5379 __isl_give isl_map
*isl_map_upper_bound_si(__isl_take isl_map
*map
,
5380 enum isl_dim_type type
, unsigned pos
, int value
)
5382 return map_bound_si(map
, type
, pos
, value
, 1);
5385 __isl_give isl_set
*isl_set_lower_bound_si(__isl_take isl_set
*set
,
5386 enum isl_dim_type type
, unsigned pos
, int value
)
5388 return (struct isl_set
*)
5389 isl_map_lower_bound_si((struct isl_map
*)set
, type
, pos
, value
);
5392 __isl_give isl_set
*isl_set_upper_bound_si(__isl_take isl_set
*set
,
5393 enum isl_dim_type type
, unsigned pos
, int value
)
5395 return isl_map_upper_bound_si(set
, type
, pos
, value
);
5398 /* Bound the given variable of "bmap" from below (or above is "upper"
5399 * is set) to "value".
5401 static __isl_give isl_basic_map
*basic_map_bound(
5402 __isl_take isl_basic_map
*bmap
,
5403 enum isl_dim_type type
, unsigned pos
, isl_int value
, int upper
)
5409 if (pos
>= isl_basic_map_dim(bmap
, type
))
5410 isl_die(bmap
->ctx
, isl_error_invalid
,
5411 "index out of bounds", goto error
);
5412 pos
+= isl_basic_map_offset(bmap
, type
);
5413 bmap
= isl_basic_map_cow(bmap
);
5414 bmap
= isl_basic_map_extend_constraints(bmap
, 0, 1);
5415 j
= isl_basic_map_alloc_inequality(bmap
);
5418 isl_seq_clr(bmap
->ineq
[j
], 1 + isl_basic_map_total_dim(bmap
));
5420 isl_int_set_si(bmap
->ineq
[j
][pos
], -1);
5421 isl_int_set(bmap
->ineq
[j
][0], value
);
5423 isl_int_set_si(bmap
->ineq
[j
][pos
], 1);
5424 isl_int_neg(bmap
->ineq
[j
][0], value
);
5426 bmap
= isl_basic_map_simplify(bmap
);
5427 return isl_basic_map_finalize(bmap
);
5429 isl_basic_map_free(bmap
);
5433 /* Bound the given variable of "map" from below (or above is "upper"
5434 * is set) to "value".
5436 static __isl_give isl_map
*map_bound(__isl_take isl_map
*map
,
5437 enum isl_dim_type type
, unsigned pos
, isl_int value
, int upper
)
5441 map
= isl_map_cow(map
);
5445 if (pos
>= isl_map_dim(map
, type
))
5446 isl_die(map
->ctx
, isl_error_invalid
,
5447 "index out of bounds", goto error
);
5448 for (i
= map
->n
- 1; i
>= 0; --i
) {
5449 map
->p
[i
] = basic_map_bound(map
->p
[i
], type
, pos
, value
, upper
);
5450 if (remove_if_empty(map
, i
) < 0)
5453 ISL_F_CLR(map
, ISL_MAP_NORMALIZED
);
5460 __isl_give isl_map
*isl_map_lower_bound(__isl_take isl_map
*map
,
5461 enum isl_dim_type type
, unsigned pos
, isl_int value
)
5463 return map_bound(map
, type
, pos
, value
, 0);
5466 __isl_give isl_map
*isl_map_upper_bound(__isl_take isl_map
*map
,
5467 enum isl_dim_type type
, unsigned pos
, isl_int value
)
5469 return map_bound(map
, type
, pos
, value
, 1);
5472 __isl_give isl_set
*isl_set_lower_bound(__isl_take isl_set
*set
,
5473 enum isl_dim_type type
, unsigned pos
, isl_int value
)
5475 return isl_map_lower_bound(set
, type
, pos
, value
);
5478 __isl_give isl_set
*isl_set_upper_bound(__isl_take isl_set
*set
,
5479 enum isl_dim_type type
, unsigned pos
, isl_int value
)
5481 return isl_map_upper_bound(set
, type
, pos
, value
);
5484 struct isl_set
*isl_set_lower_bound_dim(struct isl_set
*set
, unsigned dim
,
5489 set
= isl_set_cow(set
);
5493 isl_assert(set
->ctx
, dim
< isl_set_n_dim(set
), goto error
);
5494 for (i
= 0; i
< set
->n
; ++i
) {
5495 set
->p
[i
] = isl_basic_set_lower_bound_dim(set
->p
[i
], dim
, value
);
5505 struct isl_map
*isl_map_reverse(struct isl_map
*map
)
5509 map
= isl_map_cow(map
);
5513 map
->dim
= isl_space_reverse(map
->dim
);
5516 for (i
= 0; i
< map
->n
; ++i
) {
5517 map
->p
[i
] = isl_basic_map_reverse(map
->p
[i
]);
5521 ISL_F_CLR(map
, ISL_MAP_NORMALIZED
);
5528 static struct isl_map
*isl_basic_map_partial_lexopt(
5529 struct isl_basic_map
*bmap
, struct isl_basic_set
*dom
,
5530 struct isl_set
**empty
, int max
)
5534 if (bmap
->ctx
->opt
->pip
== ISL_PIP_PIP
)
5535 return isl_pip_basic_map_lexopt(bmap
, dom
, empty
, max
);
5537 return isl_tab_basic_map_partial_lexopt(bmap
, dom
, empty
, max
);
5539 isl_basic_map_free(bmap
);
5540 isl_basic_set_free(dom
);
5546 struct isl_map
*isl_basic_map_partial_lexmax(
5547 struct isl_basic_map
*bmap
, struct isl_basic_set
*dom
,
5548 struct isl_set
**empty
)
5550 return isl_basic_map_partial_lexopt(bmap
, dom
, empty
, 1);
5553 struct isl_map
*isl_basic_map_partial_lexmin(
5554 struct isl_basic_map
*bmap
, struct isl_basic_set
*dom
,
5555 struct isl_set
**empty
)
5557 return isl_basic_map_partial_lexopt(bmap
, dom
, empty
, 0);
5560 struct isl_set
*isl_basic_set_partial_lexmin(
5561 struct isl_basic_set
*bset
, struct isl_basic_set
*dom
,
5562 struct isl_set
**empty
)
5564 return (struct isl_set
*)
5565 isl_basic_map_partial_lexmin((struct isl_basic_map
*)bset
,
5569 struct isl_set
*isl_basic_set_partial_lexmax(
5570 struct isl_basic_set
*bset
, struct isl_basic_set
*dom
,
5571 struct isl_set
**empty
)
5573 return (struct isl_set
*)
5574 isl_basic_map_partial_lexmax((struct isl_basic_map
*)bset
,
5578 __isl_give isl_pw_multi_aff
*isl_basic_map_partial_lexmin_pw_multi_aff(
5579 __isl_take isl_basic_map
*bmap
, __isl_take isl_basic_set
*dom
,
5580 __isl_give isl_set
**empty
)
5582 return isl_basic_map_partial_lexopt_pw_multi_aff(bmap
, dom
, empty
, 0);
5585 __isl_give isl_pw_multi_aff
*isl_basic_map_partial_lexmax_pw_multi_aff(
5586 __isl_take isl_basic_map
*bmap
, __isl_take isl_basic_set
*dom
,
5587 __isl_give isl_set
**empty
)
5589 return isl_basic_map_partial_lexopt_pw_multi_aff(bmap
, dom
, empty
, 1);
5592 __isl_give isl_pw_multi_aff
*isl_basic_set_partial_lexmin_pw_multi_aff(
5593 __isl_take isl_basic_set
*bset
, __isl_take isl_basic_set
*dom
,
5594 __isl_give isl_set
**empty
)
5596 return isl_basic_map_partial_lexmin_pw_multi_aff(bset
, dom
, empty
);
5599 __isl_give isl_pw_multi_aff
*isl_basic_set_partial_lexmax_pw_multi_aff(
5600 __isl_take isl_basic_set
*bset
, __isl_take isl_basic_set
*dom
,
5601 __isl_give isl_set
**empty
)
5603 return isl_basic_map_partial_lexmax_pw_multi_aff(bset
, dom
, empty
);
5606 __isl_give isl_pw_multi_aff
*isl_basic_map_lexopt_pw_multi_aff(
5607 __isl_take isl_basic_map
*bmap
, int max
)
5609 isl_basic_set
*dom
= NULL
;
5610 isl_space
*dom_space
;
5614 dom_space
= isl_space_domain(isl_space_copy(bmap
->dim
));
5615 dom
= isl_basic_set_universe(dom_space
);
5616 return isl_basic_map_partial_lexopt_pw_multi_aff(bmap
, dom
, NULL
, max
);
5618 isl_basic_map_free(bmap
);
5622 __isl_give isl_pw_multi_aff
*isl_basic_map_lexmin_pw_multi_aff(
5623 __isl_take isl_basic_map
*bmap
)
5625 return isl_basic_map_lexopt_pw_multi_aff(bmap
, 0);
5629 #define TYPE isl_pw_multi_aff
5631 #define SUFFIX _pw_multi_aff
5633 #define EMPTY isl_pw_multi_aff_empty
5635 #define ADD isl_pw_multi_aff_union_add
5636 #include "isl_map_lexopt_templ.c"
5638 /* Given a map "map", compute the lexicographically minimal
5639 * (or maximal) image element for each domain element in dom,
5640 * in the form of an isl_pw_multi_aff.
5641 * Set *empty to those elements in dom that do not have an image element.
5643 * We first compute the lexicographically minimal or maximal element
5644 * in the first basic map. This results in a partial solution "res"
5645 * and a subset "todo" of dom that still need to be handled.
5646 * We then consider each of the remaining maps in "map" and successively
5647 * update both "res" and "todo".
5649 static __isl_give isl_pw_multi_aff
*isl_map_partial_lexopt_aligned_pw_multi_aff(
5650 __isl_take isl_map
*map
, __isl_take isl_set
*dom
,
5651 __isl_give isl_set
**empty
, int max
)
5654 isl_pw_multi_aff
*res
;
5660 if (isl_map_plain_is_empty(map
)) {
5665 return isl_pw_multi_aff_from_map(map
);
5668 res
= basic_map_partial_lexopt_pw_multi_aff(
5669 isl_basic_map_copy(map
->p
[0]),
5670 isl_set_copy(dom
), &todo
, max
);
5672 for (i
= 1; i
< map
->n
; ++i
) {
5673 isl_pw_multi_aff
*res_i
;
5676 res_i
= basic_map_partial_lexopt_pw_multi_aff(
5677 isl_basic_map_copy(map
->p
[i
]),
5678 isl_set_copy(dom
), &todo_i
, max
);
5681 res
= isl_pw_multi_aff_union_lexmax(res
, res_i
);
5683 res
= isl_pw_multi_aff_union_lexmin(res
, res_i
);
5685 todo
= isl_set_intersect(todo
, todo_i
);
5706 #define TYPE isl_map
5710 #define EMPTY isl_map_empty
5712 #define ADD isl_map_union_disjoint
5713 #include "isl_map_lexopt_templ.c"
5715 /* Given a map "map", compute the lexicographically minimal
5716 * (or maximal) image element for each domain element in dom.
5717 * Set *empty to those elements in dom that do not have an image element.
5719 * We first compute the lexicographically minimal or maximal element
5720 * in the first basic map. This results in a partial solution "res"
5721 * and a subset "todo" of dom that still need to be handled.
5722 * We then consider each of the remaining maps in "map" and successively
5723 * update both "res" and "todo".
5725 * Let res^k and todo^k be the results after k steps and let i = k + 1.
5726 * Assume we are computing the lexicographical maximum.
5727 * We first compute the lexicographically maximal element in basic map i.
5728 * This results in a partial solution res_i and a subset todo_i.
5729 * Then we combine these results with those obtain for the first k basic maps
5730 * to obtain a result that is valid for the first k+1 basic maps.
5731 * In particular, the set where there is no solution is the set where
5732 * there is no solution for the first k basic maps and also no solution
5733 * for the ith basic map, i.e.,
5735 * todo^i = todo^k * todo_i
5737 * On dom(res^k) * dom(res_i), we need to pick the larger of the two
5738 * solutions, arbitrarily breaking ties in favor of res^k.
5739 * That is, when res^k(a) >= res_i(a), we pick res^k and
5740 * when res^k(a) < res_i(a), we pick res_i. (Here, ">=" and "<" denote
5741 * the lexicographic order.)
5742 * In practice, we compute
5744 * res^k * (res_i . "<=")
5748 * res_i * (res^k . "<")
5750 * Finally, we consider the symmetric difference of dom(res^k) and dom(res_i),
5751 * where only one of res^k and res_i provides a solution and we simply pick
5758 * Note that we only compute these intersections when dom(res^k) intersects
5759 * dom(res_i). Otherwise, the only effect of these intersections is to
5760 * potentially break up res^k and res_i into smaller pieces.
5761 * We want to avoid such splintering as much as possible.
5762 * In fact, an earlier implementation of this function would look for
5763 * better results in the domain of res^k and for extra results in todo^k,
5764 * but this would always result in a splintering according to todo^k,
5765 * even when the domain of basic map i is disjoint from the domains of
5766 * the previous basic maps.
5768 static __isl_give isl_map
*isl_map_partial_lexopt_aligned(
5769 __isl_take isl_map
*map
, __isl_take isl_set
*dom
,
5770 __isl_give isl_set
**empty
, int max
)
5773 struct isl_map
*res
;
5774 struct isl_set
*todo
;
5779 if (isl_map_plain_is_empty(map
)) {
5787 res
= basic_map_partial_lexopt(isl_basic_map_copy(map
->p
[0]),
5788 isl_set_copy(dom
), &todo
, max
);
5790 for (i
= 1; i
< map
->n
; ++i
) {
5794 isl_space
*dim
= isl_space_range(isl_map_get_space(res
));
5796 res_i
= basic_map_partial_lexopt(isl_basic_map_copy(map
->p
[i
]),
5797 isl_set_copy(dom
), &todo_i
, max
);
5800 lt
= isl_map_lex_lt(isl_space_copy(dim
));
5801 le
= isl_map_lex_le(dim
);
5803 lt
= isl_map_lex_gt(isl_space_copy(dim
));
5804 le
= isl_map_lex_ge(dim
);
5806 lt
= isl_map_apply_range(isl_map_copy(res
), lt
);
5807 lt
= isl_map_intersect(lt
, isl_map_copy(res_i
));
5808 le
= isl_map_apply_range(isl_map_copy(res_i
), le
);
5809 le
= isl_map_intersect(le
, isl_map_copy(res
));
5811 if (!isl_map_is_empty(lt
) || !isl_map_is_empty(le
)) {
5812 res
= isl_map_intersect_domain(res
,
5813 isl_set_copy(todo_i
));
5814 res_i
= isl_map_intersect_domain(res_i
,
5815 isl_set_copy(todo
));
5818 res
= isl_map_union_disjoint(res
, res_i
);
5819 res
= isl_map_union_disjoint(res
, lt
);
5820 res
= isl_map_union_disjoint(res
, le
);
5822 todo
= isl_set_intersect(todo
, todo_i
);
5842 __isl_give isl_map
*isl_map_partial_lexmax(
5843 __isl_take isl_map
*map
, __isl_take isl_set
*dom
,
5844 __isl_give isl_set
**empty
)
5846 return isl_map_partial_lexopt(map
, dom
, empty
, 1);
5849 __isl_give isl_map
*isl_map_partial_lexmin(
5850 __isl_take isl_map
*map
, __isl_take isl_set
*dom
,
5851 __isl_give isl_set
**empty
)
5853 return isl_map_partial_lexopt(map
, dom
, empty
, 0);
5856 __isl_give isl_set
*isl_set_partial_lexmin(
5857 __isl_take isl_set
*set
, __isl_take isl_set
*dom
,
5858 __isl_give isl_set
**empty
)
5860 return (struct isl_set
*)
5861 isl_map_partial_lexmin((struct isl_map
*)set
,
5865 __isl_give isl_set
*isl_set_partial_lexmax(
5866 __isl_take isl_set
*set
, __isl_take isl_set
*dom
,
5867 __isl_give isl_set
**empty
)
5869 return (struct isl_set
*)
5870 isl_map_partial_lexmax((struct isl_map
*)set
,
5874 __isl_give isl_map
*isl_basic_map_lexopt(__isl_take isl_basic_map
*bmap
, int max
)
5876 struct isl_basic_set
*dom
= NULL
;
5881 dom_dim
= isl_space_domain(isl_space_copy(bmap
->dim
));
5882 dom
= isl_basic_set_universe(dom_dim
);
5883 return isl_basic_map_partial_lexopt(bmap
, dom
, NULL
, max
);
5885 isl_basic_map_free(bmap
);
5889 __isl_give isl_map
*isl_basic_map_lexmin(__isl_take isl_basic_map
*bmap
)
5891 return isl_basic_map_lexopt(bmap
, 0);
5894 __isl_give isl_map
*isl_basic_map_lexmax(__isl_take isl_basic_map
*bmap
)
5896 return isl_basic_map_lexopt(bmap
, 1);
5899 __isl_give isl_set
*isl_basic_set_lexmin(__isl_take isl_basic_set
*bset
)
5901 return (isl_set
*)isl_basic_map_lexmin((isl_basic_map
*)bset
);
5904 __isl_give isl_set
*isl_basic_set_lexmax(__isl_take isl_basic_set
*bset
)
5906 return (isl_set
*)isl_basic_map_lexmax((isl_basic_map
*)bset
);
5909 __isl_give isl_set
*isl_set_lexmin(__isl_take isl_set
*set
)
5911 return (isl_set
*)isl_map_lexmin((isl_map
*)set
);
5914 __isl_give isl_set
*isl_set_lexmax(__isl_take isl_set
*set
)
5916 return (isl_set
*)isl_map_lexmax((isl_map
*)set
);
5919 /* Extract the first and only affine expression from list
5920 * and then add it to *pwaff with the given dom.
5921 * This domain is known to be disjoint from other domains
5922 * because of the way isl_basic_map_foreach_lexmax works.
5924 static int update_dim_opt(__isl_take isl_basic_set
*dom
,
5925 __isl_take isl_aff_list
*list
, void *user
)
5927 isl_ctx
*ctx
= isl_basic_set_get_ctx(dom
);
5929 isl_pw_aff
**pwaff
= user
;
5930 isl_pw_aff
*pwaff_i
;
5932 if (isl_aff_list_n_aff(list
) != 1)
5933 isl_die(ctx
, isl_error_internal
,
5934 "expecting single element list", goto error
);
5936 aff
= isl_aff_list_get_aff(list
, 0);
5937 pwaff_i
= isl_pw_aff_alloc(isl_set_from_basic_set(dom
), aff
);
5939 *pwaff
= isl_pw_aff_add_disjoint(*pwaff
, pwaff_i
);
5941 isl_aff_list_free(list
);
5945 isl_basic_set_free(dom
);
5946 isl_aff_list_free(list
);
5950 /* Given a basic map with one output dimension, compute the minimum or
5951 * maximum of that dimension as an isl_pw_aff.
5953 * The isl_pw_aff is constructed by having isl_basic_map_foreach_lexopt
5954 * call update_dim_opt on each leaf of the result.
5956 static __isl_give isl_pw_aff
*basic_map_dim_opt(__isl_keep isl_basic_map
*bmap
,
5959 isl_space
*dim
= isl_basic_map_get_space(bmap
);
5963 dim
= isl_space_from_domain(isl_space_domain(dim
));
5964 dim
= isl_space_add_dims(dim
, isl_dim_out
, 1);
5965 pwaff
= isl_pw_aff_empty(dim
);
5967 r
= isl_basic_map_foreach_lexopt(bmap
, max
, &update_dim_opt
, &pwaff
);
5969 return isl_pw_aff_free(pwaff
);
5974 /* Compute the minimum or maximum of the given output dimension
5975 * as a function of the parameters and the input dimensions,
5976 * but independently of the other output dimensions.
5978 * We first project out the other output dimension and then compute
5979 * the "lexicographic" maximum in each basic map, combining the results
5980 * using isl_pw_aff_union_max.
5982 static __isl_give isl_pw_aff
*map_dim_opt(__isl_take isl_map
*map
, int pos
,
5989 n_out
= isl_map_dim(map
, isl_dim_out
);
5990 map
= isl_map_project_out(map
, isl_dim_out
, pos
+ 1, n_out
- (pos
+ 1));
5991 map
= isl_map_project_out(map
, isl_dim_out
, 0, pos
);
5996 isl_space
*dim
= isl_map_get_space(map
);
5997 dim
= isl_space_domain(isl_space_from_range(dim
));
5999 return isl_pw_aff_empty(dim
);
6002 pwaff
= basic_map_dim_opt(map
->p
[0], max
);
6003 for (i
= 1; i
< map
->n
; ++i
) {
6004 isl_pw_aff
*pwaff_i
;
6006 pwaff_i
= basic_map_dim_opt(map
->p
[i
], max
);
6007 pwaff
= isl_pw_aff_union_opt(pwaff
, pwaff_i
, max
);
6015 /* Compute the maximum of the given output dimension as a function of the
6016 * parameters and input dimensions, but independently of
6017 * the other output dimensions.
6019 __isl_give isl_pw_aff
*isl_map_dim_max(__isl_take isl_map
*map
, int pos
)
6021 return map_dim_opt(map
, pos
, 1);
6024 /* Compute the minimum or maximum of the given set dimension
6025 * as a function of the parameters,
6026 * but independently of the other set dimensions.
6028 static __isl_give isl_pw_aff
*set_dim_opt(__isl_take isl_set
*set
, int pos
,
6031 return map_dim_opt(set
, pos
, max
);
6034 /* Compute the maximum of the given set dimension as a function of the
6035 * parameters, but independently of the other set dimensions.
6037 __isl_give isl_pw_aff
*isl_set_dim_max(__isl_take isl_set
*set
, int pos
)
6039 return set_dim_opt(set
, pos
, 1);
6042 /* Compute the minimum of the given set dimension as a function of the
6043 * parameters, but independently of the other set dimensions.
6045 __isl_give isl_pw_aff
*isl_set_dim_min(__isl_take isl_set
*set
, int pos
)
6047 return set_dim_opt(set
, pos
, 0);
6050 /* Apply a preimage specified by "mat" on the parameters of "bset".
6051 * bset is assumed to have only parameters and divs.
6053 static struct isl_basic_set
*basic_set_parameter_preimage(
6054 struct isl_basic_set
*bset
, struct isl_mat
*mat
)
6061 bset
->dim
= isl_space_cow(bset
->dim
);
6065 nparam
= isl_basic_set_dim(bset
, isl_dim_param
);
6067 isl_assert(bset
->ctx
, mat
->n_row
== 1 + nparam
, goto error
);
6069 bset
->dim
->nparam
= 0;
6070 bset
->dim
->n_out
= nparam
;
6071 bset
= isl_basic_set_preimage(bset
, mat
);
6073 bset
->dim
->nparam
= bset
->dim
->n_out
;
6074 bset
->dim
->n_out
= 0;
6079 isl_basic_set_free(bset
);
6083 /* Apply a preimage specified by "mat" on the parameters of "set".
6084 * set is assumed to have only parameters and divs.
6086 static struct isl_set
*set_parameter_preimage(
6087 struct isl_set
*set
, struct isl_mat
*mat
)
6089 isl_space
*dim
= NULL
;
6095 dim
= isl_space_copy(set
->dim
);
6096 dim
= isl_space_cow(dim
);
6100 nparam
= isl_set_dim(set
, isl_dim_param
);
6102 isl_assert(set
->ctx
, mat
->n_row
== 1 + nparam
, goto error
);
6105 dim
->n_out
= nparam
;
6106 isl_set_reset_space(set
, dim
);
6107 set
= isl_set_preimage(set
, mat
);
6110 dim
= isl_space_copy(set
->dim
);
6111 dim
= isl_space_cow(dim
);
6114 dim
->nparam
= dim
->n_out
;
6116 isl_set_reset_space(set
, dim
);
6119 isl_space_free(dim
);
6126 /* Intersect the basic set "bset" with the affine space specified by the
6127 * equalities in "eq".
6129 static struct isl_basic_set
*basic_set_append_equalities(
6130 struct isl_basic_set
*bset
, struct isl_mat
*eq
)
6138 bset
= isl_basic_set_extend_space(bset
, isl_space_copy(bset
->dim
), 0,
6143 len
= 1 + isl_space_dim(bset
->dim
, isl_dim_all
) + bset
->extra
;
6144 for (i
= 0; i
< eq
->n_row
; ++i
) {
6145 k
= isl_basic_set_alloc_equality(bset
);
6148 isl_seq_cpy(bset
->eq
[k
], eq
->row
[i
], eq
->n_col
);
6149 isl_seq_clr(bset
->eq
[k
] + eq
->n_col
, len
- eq
->n_col
);
6153 bset
= isl_basic_set_gauss(bset
, NULL
);
6154 bset
= isl_basic_set_finalize(bset
);
6159 isl_basic_set_free(bset
);
6163 /* Intersect the set "set" with the affine space specified by the
6164 * equalities in "eq".
6166 static struct isl_set
*set_append_equalities(struct isl_set
*set
,
6174 for (i
= 0; i
< set
->n
; ++i
) {
6175 set
->p
[i
] = basic_set_append_equalities(set
->p
[i
],
6188 /* Project the given basic set onto its parameter domain, possibly introducing
6189 * new, explicit, existential variables in the constraints.
6190 * The input has parameters and (possibly implicit) existential variables.
6191 * The output has the same parameters, but only
6192 * explicit existentially quantified variables.
6194 * The actual projection is performed by pip, but pip doesn't seem
6195 * to like equalities very much, so we first remove the equalities
6196 * among the parameters by performing a variable compression on
6197 * the parameters. Afterward, an inverse transformation is performed
6198 * and the equalities among the parameters are inserted back in.
6200 static struct isl_set
*parameter_compute_divs(struct isl_basic_set
*bset
)
6204 struct isl_mat
*T
, *T2
;
6205 struct isl_set
*set
;
6206 unsigned nparam
, n_div
;
6208 bset
= isl_basic_set_cow(bset
);
6212 if (bset
->n_eq
== 0)
6213 return isl_basic_set_lexmin(bset
);
6215 isl_basic_set_gauss(bset
, NULL
);
6217 nparam
= isl_basic_set_dim(bset
, isl_dim_param
);
6218 n_div
= isl_basic_set_dim(bset
, isl_dim_div
);
6220 for (i
= 0, j
= n_div
- 1; i
< bset
->n_eq
&& j
>= 0; --j
) {
6221 if (!isl_int_is_zero(bset
->eq
[i
][1 + nparam
+ j
]))
6224 if (i
== bset
->n_eq
)
6225 return isl_basic_set_lexmin(bset
);
6227 eq
= isl_mat_sub_alloc6(bset
->ctx
, bset
->eq
, i
, bset
->n_eq
- i
,
6229 eq
= isl_mat_cow(eq
);
6230 T
= isl_mat_variable_compression(isl_mat_copy(eq
), &T2
);
6231 if (T
&& T
->n_col
== 0) {
6235 bset
= isl_basic_set_set_to_empty(bset
);
6236 return isl_set_from_basic_set(bset
);
6238 bset
= basic_set_parameter_preimage(bset
, T
);
6240 set
= isl_basic_set_lexmin(bset
);
6241 set
= set_parameter_preimage(set
, T2
);
6242 set
= set_append_equalities(set
, eq
);
6246 /* Compute an explicit representation for all the existentially
6247 * quantified variables.
6248 * The input and output dimensions are first turned into parameters.
6249 * compute_divs then returns a map with the same parameters and
6250 * no input or output dimensions and the dimension specification
6251 * is reset to that of the input.
6253 static struct isl_map
*compute_divs(struct isl_basic_map
*bmap
)
6255 struct isl_basic_set
*bset
;
6256 struct isl_set
*set
;
6257 struct isl_map
*map
;
6258 isl_space
*dim
, *orig_dim
= NULL
;
6263 bmap
= isl_basic_map_cow(bmap
);
6267 nparam
= isl_basic_map_dim(bmap
, isl_dim_param
);
6268 n_in
= isl_basic_map_dim(bmap
, isl_dim_in
);
6269 n_out
= isl_basic_map_dim(bmap
, isl_dim_out
);
6270 dim
= isl_space_set_alloc(bmap
->ctx
, nparam
+ n_in
+ n_out
, 0);
6274 orig_dim
= bmap
->dim
;
6276 bset
= (struct isl_basic_set
*)bmap
;
6278 set
= parameter_compute_divs(bset
);
6279 map
= (struct isl_map
*)set
;
6280 map
= isl_map_reset_space(map
, orig_dim
);
6284 isl_basic_map_free(bmap
);
6288 int isl_basic_map_divs_known(__isl_keep isl_basic_map
*bmap
)
6296 off
= isl_space_dim(bmap
->dim
, isl_dim_all
);
6297 for (i
= 0; i
< bmap
->n_div
; ++i
) {
6298 if (isl_int_is_zero(bmap
->div
[i
][0]))
6300 isl_assert(bmap
->ctx
, isl_int_is_zero(bmap
->div
[i
][1+1+off
+i
]),
6306 static int map_divs_known(__isl_keep isl_map
*map
)
6313 for (i
= 0; i
< map
->n
; ++i
) {
6314 int known
= isl_basic_map_divs_known(map
->p
[i
]);
6322 /* If bmap contains any unknown divs, then compute explicit
6323 * expressions for them. However, this computation may be
6324 * quite expensive, so first try to remove divs that aren't
6327 struct isl_map
*isl_basic_map_compute_divs(struct isl_basic_map
*bmap
)
6330 struct isl_map
*map
;
6332 known
= isl_basic_map_divs_known(bmap
);
6336 return isl_map_from_basic_map(bmap
);
6338 bmap
= isl_basic_map_drop_redundant_divs(bmap
);
6340 known
= isl_basic_map_divs_known(bmap
);
6344 return isl_map_from_basic_map(bmap
);
6346 map
= compute_divs(bmap
);
6349 isl_basic_map_free(bmap
);
6353 struct isl_map
*isl_map_compute_divs(struct isl_map
*map
)
6357 struct isl_map
*res
;
6364 known
= map_divs_known(map
);
6372 res
= isl_basic_map_compute_divs(isl_basic_map_copy(map
->p
[0]));
6373 for (i
= 1 ; i
< map
->n
; ++i
) {
6375 r2
= isl_basic_map_compute_divs(isl_basic_map_copy(map
->p
[i
]));
6376 if (ISL_F_ISSET(map
, ISL_MAP_DISJOINT
))
6377 res
= isl_map_union_disjoint(res
, r2
);
6379 res
= isl_map_union(res
, r2
);
6386 struct isl_set
*isl_basic_set_compute_divs(struct isl_basic_set
*bset
)
6388 return (struct isl_set
*)
6389 isl_basic_map_compute_divs((struct isl_basic_map
*)bset
);
6392 struct isl_set
*isl_set_compute_divs(struct isl_set
*set
)
6394 return (struct isl_set
*)
6395 isl_map_compute_divs((struct isl_map
*)set
);
6398 struct isl_set
*isl_map_domain(struct isl_map
*map
)
6401 struct isl_set
*set
;
6406 map
= isl_map_cow(map
);
6410 set
= (struct isl_set
*)map
;
6411 set
->dim
= isl_space_domain(set
->dim
);
6414 for (i
= 0; i
< map
->n
; ++i
) {
6415 set
->p
[i
] = isl_basic_map_domain(map
->p
[i
]);
6419 ISL_F_CLR(set
, ISL_MAP_DISJOINT
);
6420 ISL_F_CLR(set
, ISL_SET_NORMALIZED
);
6427 static __isl_give isl_map
*map_union_disjoint(__isl_take isl_map
*map1
,
6428 __isl_take isl_map
*map2
)
6432 struct isl_map
*map
= NULL
;
6446 isl_assert(map1
->ctx
, isl_space_is_equal(map1
->dim
, map2
->dim
), goto error
);
6448 if (ISL_F_ISSET(map1
, ISL_MAP_DISJOINT
) &&
6449 ISL_F_ISSET(map2
, ISL_MAP_DISJOINT
))
6450 ISL_FL_SET(flags
, ISL_MAP_DISJOINT
);
6452 map
= isl_map_alloc_space(isl_space_copy(map1
->dim
),
6453 map1
->n
+ map2
->n
, flags
);
6456 for (i
= 0; i
< map1
->n
; ++i
) {
6457 map
= isl_map_add_basic_map(map
,
6458 isl_basic_map_copy(map1
->p
[i
]));
6462 for (i
= 0; i
< map2
->n
; ++i
) {
6463 map
= isl_map_add_basic_map(map
,
6464 isl_basic_map_copy(map2
->p
[i
]));
6478 __isl_give isl_map
*isl_map_union_disjoint(__isl_take isl_map
*map1
,
6479 __isl_take isl_map
*map2
)
6481 return isl_map_align_params_map_map_and(map1
, map2
, &map_union_disjoint
);
6484 struct isl_map
*isl_map_union(struct isl_map
*map1
, struct isl_map
*map2
)
6486 map1
= isl_map_union_disjoint(map1
, map2
);
6490 ISL_F_CLR(map1
, ISL_MAP_DISJOINT
);
6494 struct isl_set
*isl_set_union_disjoint(
6495 struct isl_set
*set1
, struct isl_set
*set2
)
6497 return (struct isl_set
*)
6498 isl_map_union_disjoint(
6499 (struct isl_map
*)set1
, (struct isl_map
*)set2
);
6502 struct isl_set
*isl_set_union(struct isl_set
*set1
, struct isl_set
*set2
)
6504 return (struct isl_set
*)
6505 isl_map_union((struct isl_map
*)set1
, (struct isl_map
*)set2
);
6508 static __isl_give isl_map
*map_intersect_range(__isl_take isl_map
*map
,
6509 __isl_take isl_set
*set
)
6512 struct isl_map
*result
;
6518 if (!isl_space_match(map
->dim
, isl_dim_param
, set
->dim
, isl_dim_param
))
6519 isl_die(set
->ctx
, isl_error_invalid
,
6520 "parameters don't match", goto error
);
6522 if (isl_space_dim(set
->dim
, isl_dim_set
) != 0 &&
6523 !isl_map_compatible_range(map
, set
))
6524 isl_die(set
->ctx
, isl_error_invalid
,
6525 "incompatible spaces", goto error
);
6527 if (isl_set_plain_is_universe(set
)) {
6532 if (ISL_F_ISSET(map
, ISL_MAP_DISJOINT
) &&
6533 ISL_F_ISSET(set
, ISL_MAP_DISJOINT
))
6534 ISL_FL_SET(flags
, ISL_MAP_DISJOINT
);
6536 result
= isl_map_alloc_space(isl_space_copy(map
->dim
),
6537 map
->n
* set
->n
, flags
);
6540 for (i
= 0; i
< map
->n
; ++i
)
6541 for (j
= 0; j
< set
->n
; ++j
) {
6542 result
= isl_map_add_basic_map(result
,
6543 isl_basic_map_intersect_range(
6544 isl_basic_map_copy(map
->p
[i
]),
6545 isl_basic_set_copy(set
->p
[j
])));
6558 __isl_give isl_map
*isl_map_intersect_range(__isl_take isl_map
*map
,
6559 __isl_take isl_set
*set
)
6561 return isl_map_align_params_map_map_and(map
, set
, &map_intersect_range
);
6564 struct isl_map
*isl_map_intersect_domain(
6565 struct isl_map
*map
, struct isl_set
*set
)
6567 return isl_map_reverse(
6568 isl_map_intersect_range(isl_map_reverse(map
), set
));
6571 static __isl_give isl_map
*map_apply_domain(__isl_take isl_map
*map1
,
6572 __isl_take isl_map
*map2
)
6576 map1
= isl_map_reverse(map1
);
6577 map1
= isl_map_apply_range(map1
, map2
);
6578 return isl_map_reverse(map1
);
6585 __isl_give isl_map
*isl_map_apply_domain(__isl_take isl_map
*map1
,
6586 __isl_take isl_map
*map2
)
6588 return isl_map_align_params_map_map_and(map1
, map2
, &map_apply_domain
);
6591 static __isl_give isl_map
*map_apply_range(__isl_take isl_map
*map1
,
6592 __isl_take isl_map
*map2
)
6594 isl_space
*dim_result
;
6595 struct isl_map
*result
;
6601 dim_result
= isl_space_join(isl_space_copy(map1
->dim
),
6602 isl_space_copy(map2
->dim
));
6604 result
= isl_map_alloc_space(dim_result
, map1
->n
* map2
->n
, 0);
6607 for (i
= 0; i
< map1
->n
; ++i
)
6608 for (j
= 0; j
< map2
->n
; ++j
) {
6609 result
= isl_map_add_basic_map(result
,
6610 isl_basic_map_apply_range(
6611 isl_basic_map_copy(map1
->p
[i
]),
6612 isl_basic_map_copy(map2
->p
[j
])));
6618 if (result
&& result
->n
<= 1)
6619 ISL_F_SET(result
, ISL_MAP_DISJOINT
);
6627 __isl_give isl_map
*isl_map_apply_range(__isl_take isl_map
*map1
,
6628 __isl_take isl_map
*map2
)
6630 return isl_map_align_params_map_map_and(map1
, map2
, &map_apply_range
);
6634 * returns range - domain
6636 struct isl_basic_set
*isl_basic_map_deltas(struct isl_basic_map
*bmap
)
6638 isl_space
*dims
, *target_dim
;
6639 struct isl_basic_set
*bset
;
6646 isl_assert(bmap
->ctx
, isl_space_tuple_match(bmap
->dim
, isl_dim_in
,
6647 bmap
->dim
, isl_dim_out
),
6649 target_dim
= isl_space_domain(isl_basic_map_get_space(bmap
));
6650 dim
= isl_basic_map_n_in(bmap
);
6651 nparam
= isl_basic_map_n_param(bmap
);
6652 bset
= isl_basic_set_from_basic_map(bmap
);
6653 bset
= isl_basic_set_cow(bset
);
6654 dims
= isl_basic_set_get_space(bset
);
6655 dims
= isl_space_add_dims(dims
, isl_dim_set
, dim
);
6656 bset
= isl_basic_set_extend_space(bset
, dims
, 0, dim
, 0);
6657 bset
= isl_basic_set_swap_vars(bset
, 2*dim
);
6658 for (i
= 0; i
< dim
; ++i
) {
6659 int j
= isl_basic_map_alloc_equality(
6660 (struct isl_basic_map
*)bset
);
6663 isl_seq_clr(bset
->eq
[j
], 1 + isl_basic_set_total_dim(bset
));
6664 isl_int_set_si(bset
->eq
[j
][1+nparam
+i
], 1);
6665 isl_int_set_si(bset
->eq
[j
][1+nparam
+dim
+i
], 1);
6666 isl_int_set_si(bset
->eq
[j
][1+nparam
+2*dim
+i
], -1);
6668 bset
= isl_basic_set_project_out(bset
, isl_dim_set
, dim
, 2*dim
);
6669 bset
= isl_basic_set_reset_space(bset
, target_dim
);
6672 isl_basic_map_free(bmap
);
6677 * returns range - domain
6679 __isl_give isl_set
*isl_map_deltas(__isl_take isl_map
*map
)
6683 struct isl_set
*result
;
6688 isl_assert(map
->ctx
, isl_space_tuple_match(map
->dim
, isl_dim_in
,
6689 map
->dim
, isl_dim_out
),
6691 dim
= isl_map_get_space(map
);
6692 dim
= isl_space_domain(dim
);
6693 result
= isl_set_alloc_space(dim
, map
->n
, 0);
6696 for (i
= 0; i
< map
->n
; ++i
)
6697 result
= isl_set_add_basic_set(result
,
6698 isl_basic_map_deltas(isl_basic_map_copy(map
->p
[i
])));
6707 * returns [domain -> range] -> range - domain
6709 __isl_give isl_basic_map
*isl_basic_map_deltas_map(
6710 __isl_take isl_basic_map
*bmap
)
6714 isl_basic_map
*domain
;
6718 if (!isl_space_tuple_match(bmap
->dim
, isl_dim_in
, bmap
->dim
, isl_dim_out
))
6719 isl_die(bmap
->ctx
, isl_error_invalid
,
6720 "domain and range don't match", goto error
);
6722 nparam
= isl_basic_map_dim(bmap
, isl_dim_param
);
6723 n
= isl_basic_map_dim(bmap
, isl_dim_in
);
6725 dim
= isl_space_from_range(isl_space_domain(isl_basic_map_get_space(bmap
)));
6726 domain
= isl_basic_map_universe(dim
);
6728 bmap
= isl_basic_map_from_domain(isl_basic_map_wrap(bmap
));
6729 bmap
= isl_basic_map_apply_range(bmap
, domain
);
6730 bmap
= isl_basic_map_extend_constraints(bmap
, n
, 0);
6732 total
= isl_basic_map_total_dim(bmap
);
6734 for (i
= 0; i
< n
; ++i
) {
6735 k
= isl_basic_map_alloc_equality(bmap
);
6738 isl_seq_clr(bmap
->eq
[k
], 1 + total
);
6739 isl_int_set_si(bmap
->eq
[k
][1 + nparam
+ i
], 1);
6740 isl_int_set_si(bmap
->eq
[k
][1 + nparam
+ n
+ i
], -1);
6741 isl_int_set_si(bmap
->eq
[k
][1 + nparam
+ n
+ n
+ i
], 1);
6744 bmap
= isl_basic_map_gauss(bmap
, NULL
);
6745 return isl_basic_map_finalize(bmap
);
6747 isl_basic_map_free(bmap
);
6752 * returns [domain -> range] -> range - domain
6754 __isl_give isl_map
*isl_map_deltas_map(__isl_take isl_map
*map
)
6757 isl_space
*domain_dim
;
6762 if (!isl_space_tuple_match(map
->dim
, isl_dim_in
, map
->dim
, isl_dim_out
))
6763 isl_die(map
->ctx
, isl_error_invalid
,
6764 "domain and range don't match", goto error
);
6766 map
= isl_map_cow(map
);
6770 domain_dim
= isl_space_from_range(isl_space_domain(isl_map_get_space(map
)));
6771 map
->dim
= isl_space_from_domain(isl_space_wrap(map
->dim
));
6772 map
->dim
= isl_space_join(map
->dim
, domain_dim
);
6775 for (i
= 0; i
< map
->n
; ++i
) {
6776 map
->p
[i
] = isl_basic_map_deltas_map(map
->p
[i
]);
6780 ISL_F_CLR(map
, ISL_MAP_NORMALIZED
);
6787 __isl_give
struct isl_basic_map
*basic_map_identity(__isl_take isl_space
*dims
)
6789 struct isl_basic_map
*bmap
;
6797 nparam
= dims
->nparam
;
6799 bmap
= isl_basic_map_alloc_space(dims
, 0, dim
, 0);
6803 for (i
= 0; i
< dim
; ++i
) {
6804 int j
= isl_basic_map_alloc_equality(bmap
);
6807 isl_seq_clr(bmap
->eq
[j
], 1 + isl_basic_map_total_dim(bmap
));
6808 isl_int_set_si(bmap
->eq
[j
][1+nparam
+i
], 1);
6809 isl_int_set_si(bmap
->eq
[j
][1+nparam
+dim
+i
], -1);
6811 return isl_basic_map_finalize(bmap
);
6813 isl_basic_map_free(bmap
);
6817 __isl_give isl_basic_map
*isl_basic_map_identity(__isl_take isl_space
*dim
)
6821 if (dim
->n_in
!= dim
->n_out
)
6822 isl_die(dim
->ctx
, isl_error_invalid
,
6823 "number of input and output dimensions needs to be "
6824 "the same", goto error
);
6825 return basic_map_identity(dim
);
6827 isl_space_free(dim
);
6831 struct isl_basic_map
*isl_basic_map_identity_like(struct isl_basic_map
*model
)
6833 if (!model
|| !model
->dim
)
6835 return isl_basic_map_identity(isl_space_copy(model
->dim
));
6838 __isl_give isl_map
*isl_map_identity(__isl_take isl_space
*dim
)
6840 return isl_map_from_basic_map(isl_basic_map_identity(dim
));
6843 struct isl_map
*isl_map_identity_like(struct isl_map
*model
)
6845 if (!model
|| !model
->dim
)
6847 return isl_map_identity(isl_space_copy(model
->dim
));
6850 struct isl_map
*isl_map_identity_like_basic_map(struct isl_basic_map
*model
)
6852 if (!model
|| !model
->dim
)
6854 return isl_map_identity(isl_space_copy(model
->dim
));
6857 __isl_give isl_map
*isl_set_identity(__isl_take isl_set
*set
)
6859 isl_space
*dim
= isl_set_get_space(set
);
6861 id
= isl_map_identity(isl_space_map_from_set(dim
));
6862 return isl_map_intersect_range(id
, set
);
6865 /* Construct a basic set with all set dimensions having only non-negative
6868 __isl_give isl_basic_set
*isl_basic_set_positive_orthant(
6869 __isl_take isl_space
*space
)
6874 struct isl_basic_set
*bset
;
6878 nparam
= space
->nparam
;
6880 bset
= isl_basic_set_alloc_space(space
, 0, 0, dim
);
6883 for (i
= 0; i
< dim
; ++i
) {
6884 int k
= isl_basic_set_alloc_inequality(bset
);
6887 isl_seq_clr(bset
->ineq
[k
], 1 + isl_basic_set_total_dim(bset
));
6888 isl_int_set_si(bset
->ineq
[k
][1 + nparam
+ i
], 1);
6892 isl_basic_set_free(bset
);
6896 /* Construct the half-space x_pos >= 0.
6898 static __isl_give isl_basic_set
*nonneg_halfspace(__isl_take isl_space
*dim
,
6902 isl_basic_set
*nonneg
;
6904 nonneg
= isl_basic_set_alloc_space(dim
, 0, 0, 1);
6905 k
= isl_basic_set_alloc_inequality(nonneg
);
6908 isl_seq_clr(nonneg
->ineq
[k
], 1 + isl_basic_set_total_dim(nonneg
));
6909 isl_int_set_si(nonneg
->ineq
[k
][pos
], 1);
6911 return isl_basic_set_finalize(nonneg
);
6913 isl_basic_set_free(nonneg
);
6917 /* Construct the half-space x_pos <= -1.
6919 static __isl_give isl_basic_set
*neg_halfspace(__isl_take isl_space
*dim
, int pos
)
6924 neg
= isl_basic_set_alloc_space(dim
, 0, 0, 1);
6925 k
= isl_basic_set_alloc_inequality(neg
);
6928 isl_seq_clr(neg
->ineq
[k
], 1 + isl_basic_set_total_dim(neg
));
6929 isl_int_set_si(neg
->ineq
[k
][0], -1);
6930 isl_int_set_si(neg
->ineq
[k
][pos
], -1);
6932 return isl_basic_set_finalize(neg
);
6934 isl_basic_set_free(neg
);
6938 __isl_give isl_set
*isl_set_split_dims(__isl_take isl_set
*set
,
6939 enum isl_dim_type type
, unsigned first
, unsigned n
)
6942 isl_basic_set
*nonneg
;
6950 isl_assert(set
->ctx
, first
+ n
<= isl_set_dim(set
, type
), goto error
);
6952 for (i
= 0; i
< n
; ++i
) {
6953 nonneg
= nonneg_halfspace(isl_set_get_space(set
),
6954 pos(set
->dim
, type
) + first
+ i
);
6955 neg
= neg_halfspace(isl_set_get_space(set
),
6956 pos(set
->dim
, type
) + first
+ i
);
6958 set
= isl_set_intersect(set
, isl_basic_set_union(nonneg
, neg
));
6967 static int foreach_orthant(__isl_take isl_set
*set
, int *signs
, int first
,
6968 int len
, int (*fn
)(__isl_take isl_set
*orthant
, int *signs
, void *user
),
6975 if (isl_set_plain_is_empty(set
)) {
6980 return fn(set
, signs
, user
);
6983 half
= isl_set_from_basic_set(nonneg_halfspace(isl_set_get_space(set
),
6985 half
= isl_set_intersect(half
, isl_set_copy(set
));
6986 if (foreach_orthant(half
, signs
, first
+ 1, len
, fn
, user
) < 0)
6990 half
= isl_set_from_basic_set(neg_halfspace(isl_set_get_space(set
),
6992 half
= isl_set_intersect(half
, set
);
6993 return foreach_orthant(half
, signs
, first
+ 1, len
, fn
, user
);
6999 /* Call "fn" on the intersections of "set" with each of the orthants
7000 * (except for obviously empty intersections). The orthant is identified
7001 * by the signs array, with each entry having value 1 or -1 according
7002 * to the sign of the corresponding variable.
7004 int isl_set_foreach_orthant(__isl_keep isl_set
*set
,
7005 int (*fn
)(__isl_take isl_set
*orthant
, int *signs
, void *user
),
7015 if (isl_set_plain_is_empty(set
))
7018 nparam
= isl_set_dim(set
, isl_dim_param
);
7019 nvar
= isl_set_dim(set
, isl_dim_set
);
7021 signs
= isl_alloc_array(set
->ctx
, int, nparam
+ nvar
);
7023 r
= foreach_orthant(isl_set_copy(set
), signs
, 0, nparam
+ nvar
,
7031 int isl_set_is_equal(struct isl_set
*set1
, struct isl_set
*set2
)
7033 return isl_map_is_equal((struct isl_map
*)set1
, (struct isl_map
*)set2
);
7036 int isl_basic_map_is_subset(
7037 struct isl_basic_map
*bmap1
, struct isl_basic_map
*bmap2
)
7040 struct isl_map
*map1
;
7041 struct isl_map
*map2
;
7043 if (!bmap1
|| !bmap2
)
7046 map1
= isl_map_from_basic_map(isl_basic_map_copy(bmap1
));
7047 map2
= isl_map_from_basic_map(isl_basic_map_copy(bmap2
));
7049 is_subset
= isl_map_is_subset(map1
, map2
);
7057 int isl_basic_set_is_subset(__isl_keep isl_basic_set
*bset1
,
7058 __isl_keep isl_basic_set
*bset2
)
7060 return isl_basic_map_is_subset(bset1
, bset2
);
7063 int isl_basic_map_is_equal(
7064 struct isl_basic_map
*bmap1
, struct isl_basic_map
*bmap2
)
7068 if (!bmap1
|| !bmap2
)
7070 is_subset
= isl_basic_map_is_subset(bmap1
, bmap2
);
7073 is_subset
= isl_basic_map_is_subset(bmap2
, bmap1
);
7077 int isl_basic_set_is_equal(
7078 struct isl_basic_set
*bset1
, struct isl_basic_set
*bset2
)
7080 return isl_basic_map_is_equal(
7081 (struct isl_basic_map
*)bset1
, (struct isl_basic_map
*)bset2
);
7084 int isl_map_is_empty(struct isl_map
*map
)
7091 for (i
= 0; i
< map
->n
; ++i
) {
7092 is_empty
= isl_basic_map_is_empty(map
->p
[i
]);
7101 int isl_map_plain_is_empty(__isl_keep isl_map
*map
)
7103 return map
? map
->n
== 0 : -1;
7106 int isl_map_fast_is_empty(__isl_keep isl_map
*map
)
7108 return isl_map_plain_is_empty(map
);
7111 int isl_set_plain_is_empty(struct isl_set
*set
)
7113 return set
? set
->n
== 0 : -1;
7116 int isl_set_fast_is_empty(__isl_keep isl_set
*set
)
7118 return isl_set_plain_is_empty(set
);
7121 int isl_set_is_empty(struct isl_set
*set
)
7123 return isl_map_is_empty((struct isl_map
*)set
);
7126 int isl_map_has_equal_space(__isl_keep isl_map
*map1
, __isl_keep isl_map
*map2
)
7131 return isl_space_is_equal(map1
->dim
, map2
->dim
);
7134 int isl_set_has_equal_space(__isl_keep isl_set
*set1
, __isl_keep isl_set
*set2
)
7139 return isl_space_is_equal(set1
->dim
, set2
->dim
);
7142 static int map_is_equal(__isl_keep isl_map
*map1
, __isl_keep isl_map
*map2
)
7148 is_subset
= isl_map_is_subset(map1
, map2
);
7151 is_subset
= isl_map_is_subset(map2
, map1
);
7155 int isl_map_is_equal(__isl_keep isl_map
*map1
, __isl_keep isl_map
*map2
)
7157 return isl_map_align_params_map_map_and_test(map1
, map2
, &map_is_equal
);
7160 int isl_basic_map_is_strict_subset(
7161 struct isl_basic_map
*bmap1
, struct isl_basic_map
*bmap2
)
7165 if (!bmap1
|| !bmap2
)
7167 is_subset
= isl_basic_map_is_subset(bmap1
, bmap2
);
7170 is_subset
= isl_basic_map_is_subset(bmap2
, bmap1
);
7171 if (is_subset
== -1)
7176 int isl_map_is_strict_subset(struct isl_map
*map1
, struct isl_map
*map2
)
7182 is_subset
= isl_map_is_subset(map1
, map2
);
7185 is_subset
= isl_map_is_subset(map2
, map1
);
7186 if (is_subset
== -1)
7191 int isl_set_is_strict_subset(__isl_keep isl_set
*set1
, __isl_keep isl_set
*set2
)
7193 return isl_map_is_strict_subset((isl_map
*)set1
, (isl_map
*)set2
);
7196 int isl_basic_map_is_universe(struct isl_basic_map
*bmap
)
7200 return bmap
->n_eq
== 0 && bmap
->n_ineq
== 0;
7203 int isl_basic_set_is_universe(struct isl_basic_set
*bset
)
7207 return bset
->n_eq
== 0 && bset
->n_ineq
== 0;
7210 int isl_map_plain_is_universe(__isl_keep isl_map
*map
)
7217 for (i
= 0; i
< map
->n
; ++i
) {
7218 int r
= isl_basic_map_is_universe(map
->p
[i
]);
7226 int isl_set_plain_is_universe(__isl_keep isl_set
*set
)
7228 return isl_map_plain_is_universe((isl_map
*) set
);
7231 int isl_set_fast_is_universe(__isl_keep isl_set
*set
)
7233 return isl_set_plain_is_universe(set
);
7236 int isl_basic_map_is_empty(struct isl_basic_map
*bmap
)
7238 struct isl_basic_set
*bset
= NULL
;
7239 struct isl_vec
*sample
= NULL
;
7246 if (ISL_F_ISSET(bmap
, ISL_BASIC_MAP_EMPTY
))
7249 if (ISL_F_ISSET(bmap
, ISL_BASIC_MAP_RATIONAL
)) {
7250 struct isl_basic_map
*copy
= isl_basic_map_copy(bmap
);
7251 copy
= isl_basic_map_remove_redundancies(copy
);
7252 empty
= ISL_F_ISSET(copy
, ISL_BASIC_MAP_EMPTY
);
7253 isl_basic_map_free(copy
);
7257 total
= 1 + isl_basic_map_total_dim(bmap
);
7258 if (bmap
->sample
&& bmap
->sample
->size
== total
) {
7259 int contains
= isl_basic_map_contains(bmap
, bmap
->sample
);
7265 isl_vec_free(bmap
->sample
);
7266 bmap
->sample
= NULL
;
7267 bset
= isl_basic_map_underlying_set(isl_basic_map_copy(bmap
));
7270 sample
= isl_basic_set_sample_vec(bset
);
7273 empty
= sample
->size
== 0;
7274 isl_vec_free(bmap
->sample
);
7275 bmap
->sample
= sample
;
7277 ISL_F_SET(bmap
, ISL_BASIC_MAP_EMPTY
);
7282 int isl_basic_map_plain_is_empty(__isl_keep isl_basic_map
*bmap
)
7286 return ISL_F_ISSET(bmap
, ISL_BASIC_MAP_EMPTY
);
7289 int isl_basic_map_fast_is_empty(__isl_keep isl_basic_map
*bmap
)
7291 return isl_basic_map_plain_is_empty(bmap
);
7294 int isl_basic_set_plain_is_empty(__isl_keep isl_basic_set
*bset
)
7298 return ISL_F_ISSET(bset
, ISL_BASIC_SET_EMPTY
);
7301 int isl_basic_set_fast_is_empty(__isl_keep isl_basic_set
*bset
)
7303 return isl_basic_set_plain_is_empty(bset
);
7306 int isl_basic_set_is_empty(struct isl_basic_set
*bset
)
7308 return isl_basic_map_is_empty((struct isl_basic_map
*)bset
);
7311 struct isl_map
*isl_basic_map_union(
7312 struct isl_basic_map
*bmap1
, struct isl_basic_map
*bmap2
)
7314 struct isl_map
*map
;
7315 if (!bmap1
|| !bmap2
)
7318 isl_assert(bmap1
->ctx
, isl_space_is_equal(bmap1
->dim
, bmap2
->dim
), goto error
);
7320 map
= isl_map_alloc_space(isl_space_copy(bmap1
->dim
), 2, 0);
7323 map
= isl_map_add_basic_map(map
, bmap1
);
7324 map
= isl_map_add_basic_map(map
, bmap2
);
7327 isl_basic_map_free(bmap1
);
7328 isl_basic_map_free(bmap2
);
7332 struct isl_set
*isl_basic_set_union(
7333 struct isl_basic_set
*bset1
, struct isl_basic_set
*bset2
)
7335 return (struct isl_set
*)isl_basic_map_union(
7336 (struct isl_basic_map
*)bset1
,
7337 (struct isl_basic_map
*)bset2
);
7340 /* Order divs such that any div only depends on previous divs */
7341 struct isl_basic_map
*isl_basic_map_order_divs(struct isl_basic_map
*bmap
)
7349 off
= isl_space_dim(bmap
->dim
, isl_dim_all
);
7351 for (i
= 0; i
< bmap
->n_div
; ++i
) {
7353 if (isl_int_is_zero(bmap
->div
[i
][0]))
7355 pos
= isl_seq_first_non_zero(bmap
->div
[i
]+1+1+off
+i
,
7359 isl_basic_map_swap_div(bmap
, i
, i
+ pos
);
7365 struct isl_basic_set
*isl_basic_set_order_divs(struct isl_basic_set
*bset
)
7367 return (struct isl_basic_set
*)
7368 isl_basic_map_order_divs((struct isl_basic_map
*)bset
);
7371 __isl_give isl_map
*isl_map_order_divs(__isl_take isl_map
*map
)
7378 for (i
= 0; i
< map
->n
; ++i
) {
7379 map
->p
[i
] = isl_basic_map_order_divs(map
->p
[i
]);
7390 /* Apply the expansion computed by isl_merge_divs.
7391 * The expansion itself is given by "exp" while the resulting
7392 * list of divs is given by "div".
7394 __isl_give isl_basic_set
*isl_basic_set_expand_divs(
7395 __isl_take isl_basic_set
*bset
, __isl_take isl_mat
*div
, int *exp
)
7400 bset
= isl_basic_set_cow(bset
);
7404 if (div
->n_row
< bset
->n_div
)
7405 isl_die(isl_mat_get_ctx(div
), isl_error_invalid
,
7406 "not an expansion", goto error
);
7408 bset
= isl_basic_map_extend_space(bset
, isl_space_copy(bset
->dim
),
7409 div
->n_row
- bset
->n_div
, 0,
7410 2 * (div
->n_row
- bset
->n_div
));
7412 n_div
= bset
->n_div
;
7413 for (i
= n_div
; i
< div
->n_row
; ++i
)
7414 if (isl_basic_set_alloc_div(bset
) < 0)
7418 for (i
= div
->n_row
- 1; i
>= 0; --i
) {
7419 if (j
>= 0 && exp
[j
] == i
) {
7421 isl_basic_map_swap_div(bset
, i
, j
);
7424 isl_seq_cpy(bset
->div
[i
], div
->row
[i
], div
->n_col
);
7425 if (isl_basic_map_add_div_constraints(bset
, i
) < 0)
7433 isl_basic_set_free(bset
);
7438 /* Look for a div in dst that corresponds to the div "div" in src.
7439 * The divs before "div" in src and dst are assumed to be the same.
7441 * Returns -1 if no corresponding div was found and the position
7442 * of the corresponding div in dst otherwise.
7444 static int find_div(struct isl_basic_map
*dst
,
7445 struct isl_basic_map
*src
, unsigned div
)
7449 unsigned total
= isl_space_dim(src
->dim
, isl_dim_all
);
7451 isl_assert(dst
->ctx
, div
<= dst
->n_div
, return -1);
7452 for (i
= div
; i
< dst
->n_div
; ++i
)
7453 if (isl_seq_eq(dst
->div
[i
], src
->div
[div
], 1+1+total
+div
) &&
7454 isl_seq_first_non_zero(dst
->div
[i
]+1+1+total
+div
,
7455 dst
->n_div
- div
) == -1)
7460 struct isl_basic_map
*isl_basic_map_align_divs(
7461 struct isl_basic_map
*dst
, struct isl_basic_map
*src
)
7464 unsigned total
= isl_space_dim(src
->dim
, isl_dim_all
);
7469 if (src
->n_div
== 0)
7472 for (i
= 0; i
< src
->n_div
; ++i
)
7473 isl_assert(src
->ctx
, !isl_int_is_zero(src
->div
[i
][0]), goto error
);
7475 src
= isl_basic_map_order_divs(src
);
7476 dst
= isl_basic_map_cow(dst
);
7477 dst
= isl_basic_map_extend_space(dst
, isl_space_copy(dst
->dim
),
7478 src
->n_div
, 0, 2 * src
->n_div
);
7481 for (i
= 0; i
< src
->n_div
; ++i
) {
7482 int j
= find_div(dst
, src
, i
);
7484 j
= isl_basic_map_alloc_div(dst
);
7487 isl_seq_cpy(dst
->div
[j
], src
->div
[i
], 1+1+total
+i
);
7488 isl_seq_clr(dst
->div
[j
]+1+1+total
+i
, dst
->n_div
- i
);
7489 if (isl_basic_map_add_div_constraints(dst
, j
) < 0)
7493 isl_basic_map_swap_div(dst
, i
, j
);
7497 isl_basic_map_free(dst
);
7501 struct isl_basic_set
*isl_basic_set_align_divs(
7502 struct isl_basic_set
*dst
, struct isl_basic_set
*src
)
7504 return (struct isl_basic_set
*)isl_basic_map_align_divs(
7505 (struct isl_basic_map
*)dst
, (struct isl_basic_map
*)src
);
7508 struct isl_map
*isl_map_align_divs(struct isl_map
*map
)
7516 map
= isl_map_compute_divs(map
);
7517 map
= isl_map_cow(map
);
7521 for (i
= 1; i
< map
->n
; ++i
)
7522 map
->p
[0] = isl_basic_map_align_divs(map
->p
[0], map
->p
[i
]);
7523 for (i
= 1; i
< map
->n
; ++i
)
7524 map
->p
[i
] = isl_basic_map_align_divs(map
->p
[i
], map
->p
[0]);
7526 ISL_F_CLR(map
, ISL_MAP_NORMALIZED
);
7530 struct isl_set
*isl_set_align_divs(struct isl_set
*set
)
7532 return (struct isl_set
*)isl_map_align_divs((struct isl_map
*)set
);
7535 static __isl_give isl_set
*set_apply( __isl_take isl_set
*set
,
7536 __isl_take isl_map
*map
)
7540 isl_assert(set
->ctx
, isl_map_compatible_domain(map
, set
), goto error
);
7541 map
= isl_map_intersect_domain(map
, set
);
7542 set
= isl_map_range(map
);
7550 __isl_give isl_set
*isl_set_apply( __isl_take isl_set
*set
,
7551 __isl_take isl_map
*map
)
7553 return isl_map_align_params_map_map_and(set
, map
, &set_apply
);
7556 /* There is no need to cow as removing empty parts doesn't change
7557 * the meaning of the set.
7559 struct isl_map
*isl_map_remove_empty_parts(struct isl_map
*map
)
7566 for (i
= map
->n
- 1; i
>= 0; --i
)
7567 remove_if_empty(map
, i
);
7572 struct isl_set
*isl_set_remove_empty_parts(struct isl_set
*set
)
7574 return (struct isl_set
*)
7575 isl_map_remove_empty_parts((struct isl_map
*)set
);
7578 struct isl_basic_map
*isl_map_copy_basic_map(struct isl_map
*map
)
7580 struct isl_basic_map
*bmap
;
7581 if (!map
|| map
->n
== 0)
7583 bmap
= map
->p
[map
->n
-1];
7584 isl_assert(map
->ctx
, ISL_F_ISSET(bmap
, ISL_BASIC_SET_FINAL
), return NULL
);
7585 return isl_basic_map_copy(bmap
);
7588 struct isl_basic_set
*isl_set_copy_basic_set(struct isl_set
*set
)
7590 return (struct isl_basic_set
*)
7591 isl_map_copy_basic_map((struct isl_map
*)set
);
7594 __isl_give isl_map
*isl_map_drop_basic_map(__isl_take isl_map
*map
,
7595 __isl_keep isl_basic_map
*bmap
)
7601 for (i
= map
->n
-1; i
>= 0; --i
) {
7602 if (map
->p
[i
] != bmap
)
7604 map
= isl_map_cow(map
);
7607 isl_basic_map_free(map
->p
[i
]);
7608 if (i
!= map
->n
-1) {
7609 ISL_F_CLR(map
, ISL_SET_NORMALIZED
);
7610 map
->p
[i
] = map
->p
[map
->n
-1];
7621 struct isl_set
*isl_set_drop_basic_set(struct isl_set
*set
,
7622 struct isl_basic_set
*bset
)
7624 return (struct isl_set
*)isl_map_drop_basic_map((struct isl_map
*)set
,
7625 (struct isl_basic_map
*)bset
);
7628 /* Given two basic sets bset1 and bset2, compute the maximal difference
7629 * between the values of dimension pos in bset1 and those in bset2
7630 * for any common value of the parameters and dimensions preceding pos.
7632 static enum isl_lp_result
basic_set_maximal_difference_at(
7633 __isl_keep isl_basic_set
*bset1
, __isl_keep isl_basic_set
*bset2
,
7634 int pos
, isl_int
*opt
)
7637 struct isl_basic_map
*bmap1
= NULL
;
7638 struct isl_basic_map
*bmap2
= NULL
;
7639 struct isl_ctx
*ctx
;
7640 struct isl_vec
*obj
;
7643 unsigned dim1
, dim2
;
7644 enum isl_lp_result res
;
7646 if (!bset1
|| !bset2
)
7647 return isl_lp_error
;
7649 nparam
= isl_basic_set_n_param(bset1
);
7650 dim1
= isl_basic_set_n_dim(bset1
);
7651 dim2
= isl_basic_set_n_dim(bset2
);
7652 dims
= isl_space_alloc(bset1
->ctx
, nparam
, pos
, dim1
- pos
);
7653 bmap1
= isl_basic_map_from_basic_set(isl_basic_set_copy(bset1
), dims
);
7654 dims
= isl_space_alloc(bset2
->ctx
, nparam
, pos
, dim2
- pos
);
7655 bmap2
= isl_basic_map_from_basic_set(isl_basic_set_copy(bset2
), dims
);
7656 if (!bmap1
|| !bmap2
)
7658 bmap1
= isl_basic_map_cow(bmap1
);
7659 bmap1
= isl_basic_map_extend(bmap1
, nparam
,
7660 pos
, (dim1
- pos
) + (dim2
- pos
),
7661 bmap2
->n_div
, bmap2
->n_eq
, bmap2
->n_ineq
);
7662 bmap1
= add_constraints(bmap1
, bmap2
, 0, dim1
- pos
);
7665 total
= isl_basic_map_total_dim(bmap1
);
7667 obj
= isl_vec_alloc(ctx
, 1 + total
);
7668 isl_seq_clr(obj
->block
.data
, 1 + total
);
7669 isl_int_set_si(obj
->block
.data
[1+nparam
+pos
], 1);
7670 isl_int_set_si(obj
->block
.data
[1+nparam
+pos
+(dim1
-pos
)], -1);
7673 res
= isl_basic_map_solve_lp(bmap1
, 1, obj
->block
.data
, ctx
->one
,
7675 isl_basic_map_free(bmap1
);
7679 isl_basic_map_free(bmap1
);
7680 isl_basic_map_free(bmap2
);
7681 return isl_lp_error
;
7684 /* Given two _disjoint_ basic sets bset1 and bset2, check whether
7685 * for any common value of the parameters and dimensions preceding pos
7686 * in both basic sets, the values of dimension pos in bset1 are
7687 * smaller or larger than those in bset2.
7690 * 1 if bset1 follows bset2
7691 * -1 if bset1 precedes bset2
7692 * 0 if bset1 and bset2 are incomparable
7693 * -2 if some error occurred.
7695 int isl_basic_set_compare_at(struct isl_basic_set
*bset1
,
7696 struct isl_basic_set
*bset2
, int pos
)
7699 enum isl_lp_result res
;
7704 res
= basic_set_maximal_difference_at(bset1
, bset2
, pos
, &opt
);
7706 if (res
== isl_lp_empty
)
7708 else if ((res
== isl_lp_ok
&& isl_int_is_pos(opt
)) ||
7709 res
== isl_lp_unbounded
)
7711 else if (res
== isl_lp_ok
&& isl_int_is_neg(opt
))
7720 /* Given two basic sets bset1 and bset2, check whether
7721 * for any common value of the parameters and dimensions preceding pos
7722 * there is a value of dimension pos in bset1 that is larger
7723 * than a value of the same dimension in bset2.
7726 * 1 if there exists such a pair
7727 * 0 if there is no such pair, but there is a pair of equal values
7729 * -2 if some error occurred.
7731 int isl_basic_set_follows_at(__isl_keep isl_basic_set
*bset1
,
7732 __isl_keep isl_basic_set
*bset2
, int pos
)
7735 enum isl_lp_result res
;
7740 res
= basic_set_maximal_difference_at(bset1
, bset2
, pos
, &opt
);
7742 if (res
== isl_lp_empty
)
7744 else if ((res
== isl_lp_ok
&& isl_int_is_pos(opt
)) ||
7745 res
== isl_lp_unbounded
)
7747 else if (res
== isl_lp_ok
&& isl_int_is_neg(opt
))
7749 else if (res
== isl_lp_ok
)
7758 /* Given two sets set1 and set2, check whether
7759 * for any common value of the parameters and dimensions preceding pos
7760 * there is a value of dimension pos in set1 that is larger
7761 * than a value of the same dimension in set2.
7764 * 1 if there exists such a pair
7765 * 0 if there is no such pair, but there is a pair of equal values
7767 * -2 if some error occurred.
7769 int isl_set_follows_at(__isl_keep isl_set
*set1
,
7770 __isl_keep isl_set
*set2
, int pos
)
7778 for (i
= 0; i
< set1
->n
; ++i
)
7779 for (j
= 0; j
< set2
->n
; ++j
) {
7781 f
= isl_basic_set_follows_at(set1
->p
[i
], set2
->p
[j
], pos
);
7782 if (f
== 1 || f
== -2)
7791 static int isl_basic_map_plain_has_fixed_var(__isl_keep isl_basic_map
*bmap
,
7792 unsigned pos
, isl_int
*val
)
7800 total
= isl_basic_map_total_dim(bmap
);
7801 for (i
= 0, d
= total
-1; i
< bmap
->n_eq
&& d
+1 > pos
; ++i
) {
7802 for (; d
+1 > pos
; --d
)
7803 if (!isl_int_is_zero(bmap
->eq
[i
][1+d
]))
7807 if (isl_seq_first_non_zero(bmap
->eq
[i
]+1, d
) != -1)
7809 if (isl_seq_first_non_zero(bmap
->eq
[i
]+1+d
+1, total
-d
-1) != -1)
7811 if (!isl_int_is_one(bmap
->eq
[i
][1+d
]))
7814 isl_int_neg(*val
, bmap
->eq
[i
][0]);
7820 static int isl_map_plain_has_fixed_var(__isl_keep isl_map
*map
,
7821 unsigned pos
, isl_int
*val
)
7833 return isl_basic_map_plain_has_fixed_var(map
->p
[0], pos
, val
);
7836 fixed
= isl_basic_map_plain_has_fixed_var(map
->p
[0], pos
, &v
);
7837 for (i
= 1; fixed
== 1 && i
< map
->n
; ++i
) {
7838 fixed
= isl_basic_map_plain_has_fixed_var(map
->p
[i
], pos
, &tmp
);
7839 if (fixed
== 1 && isl_int_ne(tmp
, v
))
7843 isl_int_set(*val
, v
);
7849 static int isl_basic_set_plain_has_fixed_var(__isl_keep isl_basic_set
*bset
,
7850 unsigned pos
, isl_int
*val
)
7852 return isl_basic_map_plain_has_fixed_var((struct isl_basic_map
*)bset
,
7856 static int isl_set_plain_has_fixed_var(__isl_keep isl_set
*set
, unsigned pos
,
7859 return isl_map_plain_has_fixed_var((struct isl_map
*)set
, pos
, val
);
7862 int isl_basic_map_plain_is_fixed(__isl_keep isl_basic_map
*bmap
,
7863 enum isl_dim_type type
, unsigned pos
, isl_int
*val
)
7865 if (pos
>= isl_basic_map_dim(bmap
, type
))
7867 return isl_basic_map_plain_has_fixed_var(bmap
,
7868 isl_basic_map_offset(bmap
, type
) - 1 + pos
, val
);
7871 int isl_map_plain_is_fixed(__isl_keep isl_map
*map
,
7872 enum isl_dim_type type
, unsigned pos
, isl_int
*val
)
7874 if (pos
>= isl_map_dim(map
, type
))
7876 return isl_map_plain_has_fixed_var(map
,
7877 map_offset(map
, type
) - 1 + pos
, val
);
7880 int isl_set_plain_is_fixed(__isl_keep isl_set
*set
,
7881 enum isl_dim_type type
, unsigned pos
, isl_int
*val
)
7883 return isl_map_plain_is_fixed(set
, type
, pos
, val
);
7886 int isl_map_fast_is_fixed(__isl_keep isl_map
*map
,
7887 enum isl_dim_type type
, unsigned pos
, isl_int
*val
)
7889 return isl_map_plain_is_fixed(map
, type
, pos
, val
);
7892 /* Check if dimension dim has fixed value and if so and if val is not NULL,
7893 * then return this fixed value in *val.
7895 int isl_basic_set_plain_dim_is_fixed(__isl_keep isl_basic_set
*bset
,
7896 unsigned dim
, isl_int
*val
)
7898 return isl_basic_set_plain_has_fixed_var(bset
,
7899 isl_basic_set_n_param(bset
) + dim
, val
);
7902 /* Check if dimension dim has fixed value and if so and if val is not NULL,
7903 * then return this fixed value in *val.
7905 int isl_set_plain_dim_is_fixed(__isl_keep isl_set
*set
,
7906 unsigned dim
, isl_int
*val
)
7908 return isl_set_plain_has_fixed_var(set
, isl_set_n_param(set
) + dim
, val
);
7911 int isl_set_fast_dim_is_fixed(__isl_keep isl_set
*set
,
7912 unsigned dim
, isl_int
*val
)
7914 return isl_set_plain_dim_is_fixed(set
, dim
, val
);
7917 /* Check if input variable in has fixed value and if so and if val is not NULL,
7918 * then return this fixed value in *val.
7920 int isl_map_plain_input_is_fixed(__isl_keep isl_map
*map
,
7921 unsigned in
, isl_int
*val
)
7923 return isl_map_plain_has_fixed_var(map
, isl_map_n_param(map
) + in
, val
);
7926 /* Check if dimension dim has an (obvious) fixed lower bound and if so
7927 * and if val is not NULL, then return this lower bound in *val.
7929 int isl_basic_set_plain_dim_has_fixed_lower_bound(
7930 __isl_keep isl_basic_set
*bset
, unsigned dim
, isl_int
*val
)
7932 int i
, i_eq
= -1, i_ineq
= -1;
7939 total
= isl_basic_set_total_dim(bset
);
7940 nparam
= isl_basic_set_n_param(bset
);
7941 for (i
= 0; i
< bset
->n_eq
; ++i
) {
7942 if (isl_int_is_zero(bset
->eq
[i
][1+nparam
+dim
]))
7948 for (i
= 0; i
< bset
->n_ineq
; ++i
) {
7949 if (!isl_int_is_pos(bset
->ineq
[i
][1+nparam
+dim
]))
7951 if (i_eq
!= -1 || i_ineq
!= -1)
7955 if (i_eq
== -1 && i_ineq
== -1)
7957 c
= i_eq
!= -1 ? bset
->eq
[i_eq
] : bset
->ineq
[i_ineq
];
7958 /* The coefficient should always be one due to normalization. */
7959 if (!isl_int_is_one(c
[1+nparam
+dim
]))
7961 if (isl_seq_first_non_zero(c
+1, nparam
+dim
) != -1)
7963 if (isl_seq_first_non_zero(c
+1+nparam
+dim
+1,
7964 total
- nparam
- dim
- 1) != -1)
7967 isl_int_neg(*val
, c
[0]);
7971 int isl_set_plain_dim_has_fixed_lower_bound(__isl_keep isl_set
*set
,
7972 unsigned dim
, isl_int
*val
)
7984 return isl_basic_set_plain_dim_has_fixed_lower_bound(set
->p
[0],
7988 fixed
= isl_basic_set_plain_dim_has_fixed_lower_bound(set
->p
[0],
7990 for (i
= 1; fixed
== 1 && i
< set
->n
; ++i
) {
7991 fixed
= isl_basic_set_plain_dim_has_fixed_lower_bound(set
->p
[i
],
7993 if (fixed
== 1 && isl_int_ne(tmp
, v
))
7997 isl_int_set(*val
, v
);
8008 /* uset_gist depends on constraints without existentially quantified
8009 * variables sorting first.
8011 static int qsort_constraint_cmp(const void *p1
, const void *p2
)
8013 const struct constraint
*c1
= (const struct constraint
*)p1
;
8014 const struct constraint
*c2
= (const struct constraint
*)p2
;
8016 unsigned size
= isl_min(c1
->size
, c2
->size
);
8018 l1
= isl_seq_last_non_zero(c1
->c
, size
);
8019 l2
= isl_seq_last_non_zero(c2
->c
, size
);
8024 return isl_seq_cmp(c1
->c
, c2
->c
, size
);
8027 static struct isl_basic_map
*isl_basic_map_sort_constraints(
8028 struct isl_basic_map
*bmap
)
8031 struct constraint
*c
;
8036 total
= isl_basic_map_total_dim(bmap
);
8037 c
= isl_alloc_array(bmap
->ctx
, struct constraint
, bmap
->n_ineq
);
8040 for (i
= 0; i
< bmap
->n_ineq
; ++i
) {
8042 c
[i
].c
= bmap
->ineq
[i
];
8044 qsort(c
, bmap
->n_ineq
, sizeof(struct constraint
), qsort_constraint_cmp
);
8045 for (i
= 0; i
< bmap
->n_ineq
; ++i
)
8046 bmap
->ineq
[i
] = c
[i
].c
;
8050 isl_basic_map_free(bmap
);
8054 __isl_give isl_basic_set
*isl_basic_set_sort_constraints(
8055 __isl_take isl_basic_set
*bset
)
8057 return (struct isl_basic_set
*)isl_basic_map_sort_constraints(
8058 (struct isl_basic_map
*)bset
);
8061 struct isl_basic_map
*isl_basic_map_normalize(struct isl_basic_map
*bmap
)
8065 if (ISL_F_ISSET(bmap
, ISL_BASIC_MAP_NORMALIZED
))
8067 bmap
= isl_basic_map_remove_redundancies(bmap
);
8068 bmap
= isl_basic_map_sort_constraints(bmap
);
8069 ISL_F_SET(bmap
, ISL_BASIC_MAP_NORMALIZED
);
8073 struct isl_basic_set
*isl_basic_set_normalize(struct isl_basic_set
*bset
)
8075 return (struct isl_basic_set
*)isl_basic_map_normalize(
8076 (struct isl_basic_map
*)bset
);
8079 int isl_basic_map_plain_cmp(const __isl_keep isl_basic_map
*bmap1
,
8080 const __isl_keep isl_basic_map
*bmap2
)
8087 if (ISL_F_ISSET(bmap1
, ISL_BASIC_MAP_RATIONAL
) !=
8088 ISL_F_ISSET(bmap2
, ISL_BASIC_MAP_RATIONAL
))
8089 return ISL_F_ISSET(bmap1
, ISL_BASIC_MAP_RATIONAL
) ? -1 : 1;
8090 if (isl_basic_map_n_param(bmap1
) != isl_basic_map_n_param(bmap2
))
8091 return isl_basic_map_n_param(bmap1
) - isl_basic_map_n_param(bmap2
);
8092 if (isl_basic_map_n_in(bmap1
) != isl_basic_map_n_in(bmap2
))
8093 return isl_basic_map_n_out(bmap1
) - isl_basic_map_n_out(bmap2
);
8094 if (isl_basic_map_n_out(bmap1
) != isl_basic_map_n_out(bmap2
))
8095 return isl_basic_map_n_out(bmap1
) - isl_basic_map_n_out(bmap2
);
8096 if (ISL_F_ISSET(bmap1
, ISL_BASIC_MAP_EMPTY
) &&
8097 ISL_F_ISSET(bmap2
, ISL_BASIC_MAP_EMPTY
))
8099 if (ISL_F_ISSET(bmap1
, ISL_BASIC_MAP_EMPTY
))
8101 if (ISL_F_ISSET(bmap2
, ISL_BASIC_MAP_EMPTY
))
8103 if (bmap1
->n_eq
!= bmap2
->n_eq
)
8104 return bmap1
->n_eq
- bmap2
->n_eq
;
8105 if (bmap1
->n_ineq
!= bmap2
->n_ineq
)
8106 return bmap1
->n_ineq
- bmap2
->n_ineq
;
8107 if (bmap1
->n_div
!= bmap2
->n_div
)
8108 return bmap1
->n_div
- bmap2
->n_div
;
8109 total
= isl_basic_map_total_dim(bmap1
);
8110 for (i
= 0; i
< bmap1
->n_eq
; ++i
) {
8111 cmp
= isl_seq_cmp(bmap1
->eq
[i
], bmap2
->eq
[i
], 1+total
);
8115 for (i
= 0; i
< bmap1
->n_ineq
; ++i
) {
8116 cmp
= isl_seq_cmp(bmap1
->ineq
[i
], bmap2
->ineq
[i
], 1+total
);
8120 for (i
= 0; i
< bmap1
->n_div
; ++i
) {
8121 cmp
= isl_seq_cmp(bmap1
->div
[i
], bmap2
->div
[i
], 1+1+total
);
8128 int isl_basic_set_plain_cmp(const __isl_keep isl_basic_set
*bset1
,
8129 const __isl_keep isl_basic_set
*bset2
)
8131 return isl_basic_map_plain_cmp(bset1
, bset2
);
8134 int isl_set_plain_cmp(const __isl_keep isl_set
*set1
,
8135 const __isl_keep isl_set
*set2
)
8141 if (set1
->n
!= set2
->n
)
8142 return set1
->n
- set2
->n
;
8144 for (i
= 0; i
< set1
->n
; ++i
) {
8145 cmp
= isl_basic_set_plain_cmp(set1
->p
[i
], set2
->p
[i
]);
8153 int isl_basic_map_plain_is_equal(__isl_keep isl_basic_map
*bmap1
,
8154 __isl_keep isl_basic_map
*bmap2
)
8156 return isl_basic_map_plain_cmp(bmap1
, bmap2
) == 0;
8159 int isl_basic_set_plain_is_equal(__isl_keep isl_basic_set
*bset1
,
8160 __isl_keep isl_basic_set
*bset2
)
8162 return isl_basic_map_plain_is_equal((isl_basic_map
*)bset1
,
8163 (isl_basic_map
*)bset2
);
8166 static int qsort_bmap_cmp(const void *p1
, const void *p2
)
8168 const struct isl_basic_map
*bmap1
= *(const struct isl_basic_map
**)p1
;
8169 const struct isl_basic_map
*bmap2
= *(const struct isl_basic_map
**)p2
;
8171 return isl_basic_map_plain_cmp(bmap1
, bmap2
);
8174 /* We normalize in place, but if anything goes wrong we need
8175 * to return NULL, so we need to make sure we don't change the
8176 * meaning of any possible other copies of map.
8178 struct isl_map
*isl_map_normalize(struct isl_map
*map
)
8181 struct isl_basic_map
*bmap
;
8185 if (ISL_F_ISSET(map
, ISL_MAP_NORMALIZED
))
8187 for (i
= 0; i
< map
->n
; ++i
) {
8188 bmap
= isl_basic_map_normalize(isl_basic_map_copy(map
->p
[i
]));
8191 isl_basic_map_free(map
->p
[i
]);
8194 qsort(map
->p
, map
->n
, sizeof(struct isl_basic_map
*), qsort_bmap_cmp
);
8195 ISL_F_SET(map
, ISL_MAP_NORMALIZED
);
8196 map
= isl_map_remove_empty_parts(map
);
8199 for (i
= map
->n
- 1; i
>= 1; --i
) {
8200 if (!isl_basic_map_plain_is_equal(map
->p
[i
-1], map
->p
[i
]))
8202 isl_basic_map_free(map
->p
[i
-1]);
8203 for (j
= i
; j
< map
->n
; ++j
)
8204 map
->p
[j
-1] = map
->p
[j
];
8214 struct isl_set
*isl_set_normalize(struct isl_set
*set
)
8216 return (struct isl_set
*)isl_map_normalize((struct isl_map
*)set
);
8219 int isl_map_plain_is_equal(__isl_keep isl_map
*map1
, __isl_keep isl_map
*map2
)
8229 if (!isl_space_is_equal(map1
->dim
, map2
->dim
))
8232 map1
= isl_map_copy(map1
);
8233 map2
= isl_map_copy(map2
);
8234 map1
= isl_map_normalize(map1
);
8235 map2
= isl_map_normalize(map2
);
8238 equal
= map1
->n
== map2
->n
;
8239 for (i
= 0; equal
&& i
< map1
->n
; ++i
) {
8240 equal
= isl_basic_map_plain_is_equal(map1
->p
[i
], map2
->p
[i
]);
8253 int isl_map_fast_is_equal(__isl_keep isl_map
*map1
, __isl_keep isl_map
*map2
)
8255 return isl_map_plain_is_equal(map1
, map2
);
8258 int isl_set_plain_is_equal(__isl_keep isl_set
*set1
, __isl_keep isl_set
*set2
)
8260 return isl_map_plain_is_equal((struct isl_map
*)set1
,
8261 (struct isl_map
*)set2
);
8264 int isl_set_fast_is_equal(__isl_keep isl_set
*set1
, __isl_keep isl_set
*set2
)
8266 return isl_set_plain_is_equal(set1
, set2
);
8269 /* Return an interval that ranges from min to max (inclusive)
8271 struct isl_basic_set
*isl_basic_set_interval(struct isl_ctx
*ctx
,
8272 isl_int min
, isl_int max
)
8275 struct isl_basic_set
*bset
= NULL
;
8277 bset
= isl_basic_set_alloc(ctx
, 0, 1, 0, 0, 2);
8281 k
= isl_basic_set_alloc_inequality(bset
);
8284 isl_int_set_si(bset
->ineq
[k
][1], 1);
8285 isl_int_neg(bset
->ineq
[k
][0], min
);
8287 k
= isl_basic_set_alloc_inequality(bset
);
8290 isl_int_set_si(bset
->ineq
[k
][1], -1);
8291 isl_int_set(bset
->ineq
[k
][0], max
);
8295 isl_basic_set_free(bset
);
8299 /* Return the Cartesian product of the basic sets in list (in the given order).
8301 __isl_give isl_basic_set
*isl_basic_set_list_product(
8302 __isl_take
struct isl_basic_set_list
*list
)
8310 struct isl_basic_set
*product
= NULL
;
8314 isl_assert(list
->ctx
, list
->n
> 0, goto error
);
8315 isl_assert(list
->ctx
, list
->p
[0], goto error
);
8316 nparam
= isl_basic_set_n_param(list
->p
[0]);
8317 dim
= isl_basic_set_n_dim(list
->p
[0]);
8318 extra
= list
->p
[0]->n_div
;
8319 n_eq
= list
->p
[0]->n_eq
;
8320 n_ineq
= list
->p
[0]->n_ineq
;
8321 for (i
= 1; i
< list
->n
; ++i
) {
8322 isl_assert(list
->ctx
, list
->p
[i
], goto error
);
8323 isl_assert(list
->ctx
,
8324 nparam
== isl_basic_set_n_param(list
->p
[i
]), goto error
);
8325 dim
+= isl_basic_set_n_dim(list
->p
[i
]);
8326 extra
+= list
->p
[i
]->n_div
;
8327 n_eq
+= list
->p
[i
]->n_eq
;
8328 n_ineq
+= list
->p
[i
]->n_ineq
;
8330 product
= isl_basic_set_alloc(list
->ctx
, nparam
, dim
, extra
,
8335 for (i
= 0; i
< list
->n
; ++i
) {
8336 isl_basic_set_add_constraints(product
,
8337 isl_basic_set_copy(list
->p
[i
]), dim
);
8338 dim
+= isl_basic_set_n_dim(list
->p
[i
]);
8340 isl_basic_set_list_free(list
);
8343 isl_basic_set_free(product
);
8344 isl_basic_set_list_free(list
);
8348 struct isl_basic_map
*isl_basic_map_product(
8349 struct isl_basic_map
*bmap1
, struct isl_basic_map
*bmap2
)
8351 isl_space
*dim_result
= NULL
;
8352 struct isl_basic_map
*bmap
;
8353 unsigned in1
, in2
, out1
, out2
, nparam
, total
, pos
;
8354 struct isl_dim_map
*dim_map1
, *dim_map2
;
8356 if (!bmap1
|| !bmap2
)
8359 isl_assert(bmap1
->ctx
, isl_space_match(bmap1
->dim
, isl_dim_param
,
8360 bmap2
->dim
, isl_dim_param
), goto error
);
8361 dim_result
= isl_space_product(isl_space_copy(bmap1
->dim
),
8362 isl_space_copy(bmap2
->dim
));
8364 in1
= isl_basic_map_n_in(bmap1
);
8365 in2
= isl_basic_map_n_in(bmap2
);
8366 out1
= isl_basic_map_n_out(bmap1
);
8367 out2
= isl_basic_map_n_out(bmap2
);
8368 nparam
= isl_basic_map_n_param(bmap1
);
8370 total
= nparam
+ in1
+ in2
+ out1
+ out2
+ bmap1
->n_div
+ bmap2
->n_div
;
8371 dim_map1
= isl_dim_map_alloc(bmap1
->ctx
, total
);
8372 dim_map2
= isl_dim_map_alloc(bmap1
->ctx
, total
);
8373 isl_dim_map_dim(dim_map1
, bmap1
->dim
, isl_dim_param
, pos
= 0);
8374 isl_dim_map_dim(dim_map2
, bmap2
->dim
, isl_dim_param
, pos
= 0);
8375 isl_dim_map_dim(dim_map1
, bmap1
->dim
, isl_dim_in
, pos
+= nparam
);
8376 isl_dim_map_dim(dim_map2
, bmap2
->dim
, isl_dim_in
, pos
+= in1
);
8377 isl_dim_map_dim(dim_map1
, bmap1
->dim
, isl_dim_out
, pos
+= in2
);
8378 isl_dim_map_dim(dim_map2
, bmap2
->dim
, isl_dim_out
, pos
+= out1
);
8379 isl_dim_map_div(dim_map1
, bmap1
, pos
+= out2
);
8380 isl_dim_map_div(dim_map2
, bmap2
, pos
+= bmap1
->n_div
);
8382 bmap
= isl_basic_map_alloc_space(dim_result
,
8383 bmap1
->n_div
+ bmap2
->n_div
,
8384 bmap1
->n_eq
+ bmap2
->n_eq
,
8385 bmap1
->n_ineq
+ bmap2
->n_ineq
);
8386 bmap
= isl_basic_map_add_constraints_dim_map(bmap
, bmap1
, dim_map1
);
8387 bmap
= isl_basic_map_add_constraints_dim_map(bmap
, bmap2
, dim_map2
);
8388 bmap
= isl_basic_map_simplify(bmap
);
8389 return isl_basic_map_finalize(bmap
);
8391 isl_basic_map_free(bmap1
);
8392 isl_basic_map_free(bmap2
);
8396 __isl_give isl_basic_map
*isl_basic_map_flat_product(
8397 __isl_take isl_basic_map
*bmap1
, __isl_take isl_basic_map
*bmap2
)
8399 isl_basic_map
*prod
;
8401 prod
= isl_basic_map_product(bmap1
, bmap2
);
8402 prod
= isl_basic_map_flatten(prod
);
8406 __isl_give isl_basic_set
*isl_basic_set_flat_product(
8407 __isl_take isl_basic_set
*bset1
, __isl_take isl_basic_set
*bset2
)
8409 return isl_basic_map_flat_range_product(bset1
, bset2
);
8412 __isl_give isl_basic_map
*isl_basic_map_domain_product(
8413 __isl_take isl_basic_map
*bmap1
, __isl_take isl_basic_map
*bmap2
)
8415 isl_space
*space_result
= NULL
;
8416 isl_basic_map
*bmap
;
8417 unsigned in1
, in2
, out
, nparam
, total
, pos
;
8418 struct isl_dim_map
*dim_map1
, *dim_map2
;
8420 if (!bmap1
|| !bmap2
)
8423 space_result
= isl_space_domain_product(isl_space_copy(bmap1
->dim
),
8424 isl_space_copy(bmap2
->dim
));
8426 in1
= isl_basic_map_dim(bmap1
, isl_dim_in
);
8427 in2
= isl_basic_map_dim(bmap2
, isl_dim_in
);
8428 out
= isl_basic_map_dim(bmap1
, isl_dim_out
);
8429 nparam
= isl_basic_map_dim(bmap1
, isl_dim_param
);
8431 total
= nparam
+ in1
+ in2
+ out
+ bmap1
->n_div
+ bmap2
->n_div
;
8432 dim_map1
= isl_dim_map_alloc(bmap1
->ctx
, total
);
8433 dim_map2
= isl_dim_map_alloc(bmap1
->ctx
, total
);
8434 isl_dim_map_dim(dim_map1
, bmap1
->dim
, isl_dim_param
, pos
= 0);
8435 isl_dim_map_dim(dim_map2
, bmap2
->dim
, isl_dim_param
, pos
= 0);
8436 isl_dim_map_dim(dim_map1
, bmap1
->dim
, isl_dim_in
, pos
+= nparam
);
8437 isl_dim_map_dim(dim_map2
, bmap2
->dim
, isl_dim_in
, pos
+= in1
);
8438 isl_dim_map_dim(dim_map1
, bmap1
->dim
, isl_dim_out
, pos
+= in2
);
8439 isl_dim_map_dim(dim_map2
, bmap2
->dim
, isl_dim_out
, pos
);
8440 isl_dim_map_div(dim_map1
, bmap1
, pos
+= out
);
8441 isl_dim_map_div(dim_map2
, bmap2
, pos
+= bmap1
->n_div
);
8443 bmap
= isl_basic_map_alloc_space(space_result
,
8444 bmap1
->n_div
+ bmap2
->n_div
,
8445 bmap1
->n_eq
+ bmap2
->n_eq
,
8446 bmap1
->n_ineq
+ bmap2
->n_ineq
);
8447 bmap
= isl_basic_map_add_constraints_dim_map(bmap
, bmap1
, dim_map1
);
8448 bmap
= isl_basic_map_add_constraints_dim_map(bmap
, bmap2
, dim_map2
);
8449 bmap
= isl_basic_map_simplify(bmap
);
8450 return isl_basic_map_finalize(bmap
);
8452 isl_basic_map_free(bmap1
);
8453 isl_basic_map_free(bmap2
);
8457 __isl_give isl_basic_map
*isl_basic_map_range_product(
8458 __isl_take isl_basic_map
*bmap1
, __isl_take isl_basic_map
*bmap2
)
8460 isl_space
*dim_result
= NULL
;
8461 isl_basic_map
*bmap
;
8462 unsigned in
, out1
, out2
, nparam
, total
, pos
;
8463 struct isl_dim_map
*dim_map1
, *dim_map2
;
8465 if (!bmap1
|| !bmap2
)
8468 if (!isl_space_match(bmap1
->dim
, isl_dim_param
,
8469 bmap2
->dim
, isl_dim_param
))
8470 isl_die(isl_basic_map_get_ctx(bmap1
), isl_error_invalid
,
8471 "parameters don't match", goto error
);
8473 dim_result
= isl_space_range_product(isl_space_copy(bmap1
->dim
),
8474 isl_space_copy(bmap2
->dim
));
8476 in
= isl_basic_map_dim(bmap1
, isl_dim_in
);
8477 out1
= isl_basic_map_n_out(bmap1
);
8478 out2
= isl_basic_map_n_out(bmap2
);
8479 nparam
= isl_basic_map_n_param(bmap1
);
8481 total
= nparam
+ in
+ out1
+ out2
+ bmap1
->n_div
+ bmap2
->n_div
;
8482 dim_map1
= isl_dim_map_alloc(bmap1
->ctx
, total
);
8483 dim_map2
= isl_dim_map_alloc(bmap1
->ctx
, total
);
8484 isl_dim_map_dim(dim_map1
, bmap1
->dim
, isl_dim_param
, pos
= 0);
8485 isl_dim_map_dim(dim_map2
, bmap2
->dim
, isl_dim_param
, pos
= 0);
8486 isl_dim_map_dim(dim_map1
, bmap1
->dim
, isl_dim_in
, pos
+= nparam
);
8487 isl_dim_map_dim(dim_map2
, bmap2
->dim
, isl_dim_in
, pos
);
8488 isl_dim_map_dim(dim_map1
, bmap1
->dim
, isl_dim_out
, pos
+= in
);
8489 isl_dim_map_dim(dim_map2
, bmap2
->dim
, isl_dim_out
, pos
+= out1
);
8490 isl_dim_map_div(dim_map1
, bmap1
, pos
+= out2
);
8491 isl_dim_map_div(dim_map2
, bmap2
, pos
+= bmap1
->n_div
);
8493 bmap
= isl_basic_map_alloc_space(dim_result
,
8494 bmap1
->n_div
+ bmap2
->n_div
,
8495 bmap1
->n_eq
+ bmap2
->n_eq
,
8496 bmap1
->n_ineq
+ bmap2
->n_ineq
);
8497 bmap
= isl_basic_map_add_constraints_dim_map(bmap
, bmap1
, dim_map1
);
8498 bmap
= isl_basic_map_add_constraints_dim_map(bmap
, bmap2
, dim_map2
);
8499 bmap
= isl_basic_map_simplify(bmap
);
8500 return isl_basic_map_finalize(bmap
);
8502 isl_basic_map_free(bmap1
);
8503 isl_basic_map_free(bmap2
);
8507 __isl_give isl_basic_map
*isl_basic_map_flat_range_product(
8508 __isl_take isl_basic_map
*bmap1
, __isl_take isl_basic_map
*bmap2
)
8510 isl_basic_map
*prod
;
8512 prod
= isl_basic_map_range_product(bmap1
, bmap2
);
8513 prod
= isl_basic_map_flatten_range(prod
);
8517 static __isl_give isl_map
*map_product(__isl_take isl_map
*map1
,
8518 __isl_take isl_map
*map2
,
8519 __isl_give isl_space
*(*dim_product
)(__isl_take isl_space
*left
,
8520 __isl_take isl_space
*right
),
8521 __isl_give isl_basic_map
*(*basic_map_product
)(
8522 __isl_take isl_basic_map
*left
, __isl_take isl_basic_map
*right
))
8525 struct isl_map
*result
;
8531 isl_assert(map1
->ctx
, isl_space_match(map1
->dim
, isl_dim_param
,
8532 map2
->dim
, isl_dim_param
), goto error
);
8534 if (ISL_F_ISSET(map1
, ISL_MAP_DISJOINT
) &&
8535 ISL_F_ISSET(map2
, ISL_MAP_DISJOINT
))
8536 ISL_FL_SET(flags
, ISL_MAP_DISJOINT
);
8538 result
= isl_map_alloc_space(dim_product(isl_space_copy(map1
->dim
),
8539 isl_space_copy(map2
->dim
)),
8540 map1
->n
* map2
->n
, flags
);
8543 for (i
= 0; i
< map1
->n
; ++i
)
8544 for (j
= 0; j
< map2
->n
; ++j
) {
8545 struct isl_basic_map
*part
;
8546 part
= basic_map_product(isl_basic_map_copy(map1
->p
[i
]),
8547 isl_basic_map_copy(map2
->p
[j
]));
8548 if (isl_basic_map_is_empty(part
))
8549 isl_basic_map_free(part
);
8551 result
= isl_map_add_basic_map(result
, part
);
8564 /* Given two maps A -> B and C -> D, construct a map [A -> C] -> [B -> D]
8566 static __isl_give isl_map
*map_product_aligned(__isl_take isl_map
*map1
,
8567 __isl_take isl_map
*map2
)
8569 return map_product(map1
, map2
, &isl_space_product
, &isl_basic_map_product
);
8572 __isl_give isl_map
*isl_map_product(__isl_take isl_map
*map1
,
8573 __isl_take isl_map
*map2
)
8575 return isl_map_align_params_map_map_and(map1
, map2
, &map_product_aligned
);
8578 /* Given two maps A -> B and C -> D, construct a map (A, C) -> (B, D)
8580 __isl_give isl_map
*isl_map_flat_product(__isl_take isl_map
*map1
,
8581 __isl_take isl_map
*map2
)
8585 prod
= isl_map_product(map1
, map2
);
8586 prod
= isl_map_flatten(prod
);
8590 /* Given two set A and B, construct its Cartesian product A x B.
8592 struct isl_set
*isl_set_product(struct isl_set
*set1
, struct isl_set
*set2
)
8594 return isl_map_range_product(set1
, set2
);
8597 __isl_give isl_set
*isl_set_flat_product(__isl_take isl_set
*set1
,
8598 __isl_take isl_set
*set2
)
8600 return isl_map_flat_range_product(set1
, set2
);
8603 /* Given two maps A -> B and C -> D, construct a map [A -> C] -> (B * D)
8605 static __isl_give isl_map
*map_domain_product_aligned(__isl_take isl_map
*map1
,
8606 __isl_take isl_map
*map2
)
8608 return map_product(map1
, map2
, &isl_space_domain_product
,
8609 &isl_basic_map_domain_product
);
8612 /* Given two maps A -> B and C -> D, construct a map (A * C) -> [B -> D]
8614 static __isl_give isl_map
*map_range_product_aligned(__isl_take isl_map
*map1
,
8615 __isl_take isl_map
*map2
)
8617 return map_product(map1
, map2
, &isl_space_range_product
,
8618 &isl_basic_map_range_product
);
8621 __isl_give isl_map
*isl_map_domain_product(__isl_take isl_map
*map1
,
8622 __isl_take isl_map
*map2
)
8624 return isl_map_align_params_map_map_and(map1
, map2
,
8625 &map_domain_product_aligned
);
8628 __isl_give isl_map
*isl_map_range_product(__isl_take isl_map
*map1
,
8629 __isl_take isl_map
*map2
)
8631 return isl_map_align_params_map_map_and(map1
, map2
,
8632 &map_range_product_aligned
);
8635 /* Given two maps A -> B and C -> D, construct a map (A, C) -> (B * D)
8637 __isl_give isl_map
*isl_map_flat_domain_product(__isl_take isl_map
*map1
,
8638 __isl_take isl_map
*map2
)
8642 prod
= isl_map_domain_product(map1
, map2
);
8643 prod
= isl_map_flatten_domain(prod
);
8647 /* Given two maps A -> B and C -> D, construct a map (A * C) -> (B, D)
8649 __isl_give isl_map
*isl_map_flat_range_product(__isl_take isl_map
*map1
,
8650 __isl_take isl_map
*map2
)
8654 prod
= isl_map_range_product(map1
, map2
);
8655 prod
= isl_map_flatten_range(prod
);
8659 uint32_t isl_basic_map_get_hash(__isl_keep isl_basic_map
*bmap
)
8662 uint32_t hash
= isl_hash_init();
8667 bmap
= isl_basic_map_copy(bmap
);
8668 bmap
= isl_basic_map_normalize(bmap
);
8671 total
= isl_basic_map_total_dim(bmap
);
8672 isl_hash_byte(hash
, bmap
->n_eq
& 0xFF);
8673 for (i
= 0; i
< bmap
->n_eq
; ++i
) {
8675 c_hash
= isl_seq_get_hash(bmap
->eq
[i
], 1 + total
);
8676 isl_hash_hash(hash
, c_hash
);
8678 isl_hash_byte(hash
, bmap
->n_ineq
& 0xFF);
8679 for (i
= 0; i
< bmap
->n_ineq
; ++i
) {
8681 c_hash
= isl_seq_get_hash(bmap
->ineq
[i
], 1 + total
);
8682 isl_hash_hash(hash
, c_hash
);
8684 isl_hash_byte(hash
, bmap
->n_div
& 0xFF);
8685 for (i
= 0; i
< bmap
->n_div
; ++i
) {
8687 if (isl_int_is_zero(bmap
->div
[i
][0]))
8689 isl_hash_byte(hash
, i
& 0xFF);
8690 c_hash
= isl_seq_get_hash(bmap
->div
[i
], 1 + 1 + total
);
8691 isl_hash_hash(hash
, c_hash
);
8693 isl_basic_map_free(bmap
);
8697 uint32_t isl_basic_set_get_hash(__isl_keep isl_basic_set
*bset
)
8699 return isl_basic_map_get_hash((isl_basic_map
*)bset
);
8702 uint32_t isl_map_get_hash(__isl_keep isl_map
*map
)
8709 map
= isl_map_copy(map
);
8710 map
= isl_map_normalize(map
);
8714 hash
= isl_hash_init();
8715 for (i
= 0; i
< map
->n
; ++i
) {
8717 bmap_hash
= isl_basic_map_get_hash(map
->p
[i
]);
8718 isl_hash_hash(hash
, bmap_hash
);
8726 uint32_t isl_set_get_hash(__isl_keep isl_set
*set
)
8728 return isl_map_get_hash((isl_map
*)set
);
8731 /* Check if the value for dimension dim is completely determined
8732 * by the values of the other parameters and variables.
8733 * That is, check if dimension dim is involved in an equality.
8735 int isl_basic_set_dim_is_unique(struct isl_basic_set
*bset
, unsigned dim
)
8742 nparam
= isl_basic_set_n_param(bset
);
8743 for (i
= 0; i
< bset
->n_eq
; ++i
)
8744 if (!isl_int_is_zero(bset
->eq
[i
][1 + nparam
+ dim
]))
8749 /* Check if the value for dimension dim is completely determined
8750 * by the values of the other parameters and variables.
8751 * That is, check if dimension dim is involved in an equality
8752 * for each of the subsets.
8754 int isl_set_dim_is_unique(struct isl_set
*set
, unsigned dim
)
8760 for (i
= 0; i
< set
->n
; ++i
) {
8762 unique
= isl_basic_set_dim_is_unique(set
->p
[i
], dim
);
8769 int isl_set_n_basic_set(__isl_keep isl_set
*set
)
8771 return set
? set
->n
: 0;
8774 int isl_map_foreach_basic_map(__isl_keep isl_map
*map
,
8775 int (*fn
)(__isl_take isl_basic_map
*bmap
, void *user
), void *user
)
8782 for (i
= 0; i
< map
->n
; ++i
)
8783 if (fn(isl_basic_map_copy(map
->p
[i
]), user
) < 0)
8789 int isl_set_foreach_basic_set(__isl_keep isl_set
*set
,
8790 int (*fn
)(__isl_take isl_basic_set
*bset
, void *user
), void *user
)
8797 for (i
= 0; i
< set
->n
; ++i
)
8798 if (fn(isl_basic_set_copy(set
->p
[i
]), user
) < 0)
8804 __isl_give isl_basic_set
*isl_basic_set_lift(__isl_take isl_basic_set
*bset
)
8811 bset
= isl_basic_set_cow(bset
);
8815 dim
= isl_basic_set_get_space(bset
);
8816 dim
= isl_space_lift(dim
, bset
->n_div
);
8819 isl_space_free(bset
->dim
);
8821 bset
->extra
-= bset
->n_div
;
8824 bset
= isl_basic_set_finalize(bset
);
8828 isl_basic_set_free(bset
);
8832 __isl_give isl_set
*isl_set_lift(__isl_take isl_set
*set
)
8838 set
= isl_set_align_divs(set
);
8843 set
= isl_set_cow(set
);
8847 n_div
= set
->p
[0]->n_div
;
8848 dim
= isl_set_get_space(set
);
8849 dim
= isl_space_lift(dim
, n_div
);
8852 isl_space_free(set
->dim
);
8855 for (i
= 0; i
< set
->n
; ++i
) {
8856 set
->p
[i
] = isl_basic_set_lift(set
->p
[i
]);
8867 __isl_give isl_map
*isl_set_lifting(__isl_take isl_set
*set
)
8870 struct isl_basic_map
*bmap
;
8877 set
= isl_set_align_divs(set
);
8882 dim
= isl_set_get_space(set
);
8883 if (set
->n
== 0 || set
->p
[0]->n_div
== 0) {
8885 return isl_map_identity(isl_space_map_from_set(dim
));
8888 n_div
= set
->p
[0]->n_div
;
8889 dim
= isl_space_map_from_set(dim
);
8890 n_param
= isl_space_dim(dim
, isl_dim_param
);
8891 n_set
= isl_space_dim(dim
, isl_dim_in
);
8892 dim
= isl_space_extend(dim
, n_param
, n_set
, n_set
+ n_div
);
8893 bmap
= isl_basic_map_alloc_space(dim
, 0, n_set
, 2 * n_div
);
8894 for (i
= 0; i
< n_set
; ++i
)
8895 bmap
= var_equal(bmap
, i
);
8897 total
= n_param
+ n_set
+ n_set
+ n_div
;
8898 for (i
= 0; i
< n_div
; ++i
) {
8899 k
= isl_basic_map_alloc_inequality(bmap
);
8902 isl_seq_cpy(bmap
->ineq
[k
], set
->p
[0]->div
[i
]+1, 1+n_param
);
8903 isl_seq_clr(bmap
->ineq
[k
]+1+n_param
, n_set
);
8904 isl_seq_cpy(bmap
->ineq
[k
]+1+n_param
+n_set
,
8905 set
->p
[0]->div
[i
]+1+1+n_param
, n_set
+ n_div
);
8906 isl_int_neg(bmap
->ineq
[k
][1+n_param
+n_set
+n_set
+i
],
8907 set
->p
[0]->div
[i
][0]);
8909 l
= isl_basic_map_alloc_inequality(bmap
);
8912 isl_seq_neg(bmap
->ineq
[l
], bmap
->ineq
[k
], 1 + total
);
8913 isl_int_add(bmap
->ineq
[l
][0], bmap
->ineq
[l
][0],
8914 set
->p
[0]->div
[i
][0]);
8915 isl_int_sub_ui(bmap
->ineq
[l
][0], bmap
->ineq
[l
][0], 1);
8919 bmap
= isl_basic_map_simplify(bmap
);
8920 bmap
= isl_basic_map_finalize(bmap
);
8921 return isl_map_from_basic_map(bmap
);
8924 isl_basic_map_free(bmap
);
8928 int isl_basic_set_size(__isl_keep isl_basic_set
*bset
)
8936 dim
= isl_basic_set_total_dim(bset
);
8937 size
+= bset
->n_eq
* (1 + dim
);
8938 size
+= bset
->n_ineq
* (1 + dim
);
8939 size
+= bset
->n_div
* (2 + dim
);
8944 int isl_set_size(__isl_keep isl_set
*set
)
8952 for (i
= 0; i
< set
->n
; ++i
)
8953 size
+= isl_basic_set_size(set
->p
[i
]);
8958 /* Check if there is any lower bound (if lower == 0) and/or upper
8959 * bound (if upper == 0) on the specified dim.
8961 static int basic_map_dim_is_bounded(__isl_keep isl_basic_map
*bmap
,
8962 enum isl_dim_type type
, unsigned pos
, int lower
, int upper
)
8969 isl_assert(bmap
->ctx
, pos
< isl_basic_map_dim(bmap
, type
), return -1);
8971 pos
+= isl_basic_map_offset(bmap
, type
);
8973 for (i
= 0; i
< bmap
->n_div
; ++i
) {
8974 if (isl_int_is_zero(bmap
->div
[i
][0]))
8976 if (!isl_int_is_zero(bmap
->div
[i
][1 + pos
]))
8980 for (i
= 0; i
< bmap
->n_eq
; ++i
)
8981 if (!isl_int_is_zero(bmap
->eq
[i
][pos
]))
8984 for (i
= 0; i
< bmap
->n_ineq
; ++i
) {
8985 int sgn
= isl_int_sgn(bmap
->ineq
[i
][pos
]);
8992 return lower
&& upper
;
8995 int isl_basic_map_dim_is_bounded(__isl_keep isl_basic_map
*bmap
,
8996 enum isl_dim_type type
, unsigned pos
)
8998 return basic_map_dim_is_bounded(bmap
, type
, pos
, 0, 0);
9001 int isl_basic_map_dim_has_lower_bound(__isl_keep isl_basic_map
*bmap
,
9002 enum isl_dim_type type
, unsigned pos
)
9004 return basic_map_dim_is_bounded(bmap
, type
, pos
, 0, 1);
9007 int isl_basic_map_dim_has_upper_bound(__isl_keep isl_basic_map
*bmap
,
9008 enum isl_dim_type type
, unsigned pos
)
9010 return basic_map_dim_is_bounded(bmap
, type
, pos
, 1, 0);
9013 int isl_map_dim_is_bounded(__isl_keep isl_map
*map
,
9014 enum isl_dim_type type
, unsigned pos
)
9021 for (i
= 0; i
< map
->n
; ++i
) {
9023 bounded
= isl_basic_map_dim_is_bounded(map
->p
[i
], type
, pos
);
9024 if (bounded
< 0 || !bounded
)
9031 /* Return 1 if the specified dim is involved in both an upper bound
9032 * and a lower bound.
9034 int isl_set_dim_is_bounded(__isl_keep isl_set
*set
,
9035 enum isl_dim_type type
, unsigned pos
)
9037 return isl_map_dim_is_bounded((isl_map
*)set
, type
, pos
);
9040 static int has_bound(__isl_keep isl_map
*map
,
9041 enum isl_dim_type type
, unsigned pos
,
9042 int (*fn
)(__isl_keep isl_basic_map
*bmap
,
9043 enum isl_dim_type type
, unsigned pos
))
9050 for (i
= 0; i
< map
->n
; ++i
) {
9052 bounded
= fn(map
->p
[i
], type
, pos
);
9053 if (bounded
< 0 || bounded
)
9060 /* Return 1 if the specified dim is involved in any lower bound.
9062 int isl_set_dim_has_lower_bound(__isl_keep isl_set
*set
,
9063 enum isl_dim_type type
, unsigned pos
)
9065 return has_bound(set
, type
, pos
, &isl_basic_map_dim_has_lower_bound
);
9068 /* Return 1 if the specified dim is involved in any upper bound.
9070 int isl_set_dim_has_upper_bound(__isl_keep isl_set
*set
,
9071 enum isl_dim_type type
, unsigned pos
)
9073 return has_bound(set
, type
, pos
, &isl_basic_map_dim_has_upper_bound
);
9076 /* For each of the "n" variables starting at "first", determine
9077 * the sign of the variable and put the results in the first "n"
9078 * elements of the array "signs".
9080 * 1 means that the variable is non-negative
9081 * -1 means that the variable is non-positive
9082 * 0 means the variable attains both positive and negative values.
9084 int isl_basic_set_vars_get_sign(__isl_keep isl_basic_set
*bset
,
9085 unsigned first
, unsigned n
, int *signs
)
9087 isl_vec
*bound
= NULL
;
9088 struct isl_tab
*tab
= NULL
;
9089 struct isl_tab_undo
*snap
;
9092 if (!bset
|| !signs
)
9095 bound
= isl_vec_alloc(bset
->ctx
, 1 + isl_basic_set_total_dim(bset
));
9096 tab
= isl_tab_from_basic_set(bset
, 0);
9100 isl_seq_clr(bound
->el
, bound
->size
);
9101 isl_int_set_si(bound
->el
[0], -1);
9103 snap
= isl_tab_snap(tab
);
9104 for (i
= 0; i
< n
; ++i
) {
9107 isl_int_set_si(bound
->el
[1 + first
+ i
], -1);
9108 if (isl_tab_add_ineq(tab
, bound
->el
) < 0)
9111 isl_int_set_si(bound
->el
[1 + first
+ i
], 0);
9112 if (isl_tab_rollback(tab
, snap
) < 0)
9120 isl_int_set_si(bound
->el
[1 + first
+ i
], 1);
9121 if (isl_tab_add_ineq(tab
, bound
->el
) < 0)
9124 isl_int_set_si(bound
->el
[1 + first
+ i
], 0);
9125 if (isl_tab_rollback(tab
, snap
) < 0)
9128 signs
[i
] = empty
? -1 : 0;
9132 isl_vec_free(bound
);
9136 isl_vec_free(bound
);
9140 int isl_basic_set_dims_get_sign(__isl_keep isl_basic_set
*bset
,
9141 enum isl_dim_type type
, unsigned first
, unsigned n
, int *signs
)
9143 if (!bset
|| !signs
)
9145 isl_assert(bset
->ctx
, first
+ n
<= isl_basic_set_dim(bset
, type
),
9148 first
+= pos(bset
->dim
, type
) - 1;
9149 return isl_basic_set_vars_get_sign(bset
, first
, n
, signs
);
9152 /* Check if the given basic map is obviously single-valued.
9153 * In particular, for each output dimension, check that there is
9154 * an equality that defines the output dimension in terms of
9155 * earlier dimensions.
9157 int isl_basic_map_plain_is_single_valued(__isl_keep isl_basic_map
*bmap
)
9167 total
= 1 + isl_basic_map_total_dim(bmap
);
9168 n_out
= isl_basic_map_dim(bmap
, isl_dim_out
);
9169 o_out
= isl_basic_map_offset(bmap
, isl_dim_out
);
9171 for (i
= 0; i
< n_out
; ++i
) {
9172 for (j
= 0; j
< bmap
->n_eq
; ++j
) {
9173 if (isl_int_is_zero(bmap
->eq
[j
][o_out
+ i
]))
9175 if (isl_seq_first_non_zero(bmap
->eq
[j
] + o_out
+ i
+ 1,
9176 total
- (o_out
+ i
+ 1)) == -1)
9179 if (j
>= bmap
->n_eq
)
9186 /* Check if the given basic map is single-valued.
9191 * and check if the result is a subset of the identity mapping.
9193 int isl_basic_map_is_single_valued(__isl_keep isl_basic_map
*bmap
)
9196 isl_basic_map
*test
;
9200 sv
= isl_basic_map_plain_is_single_valued(bmap
);
9204 test
= isl_basic_map_reverse(isl_basic_map_copy(bmap
));
9205 test
= isl_basic_map_apply_range(test
, isl_basic_map_copy(bmap
));
9207 space
= isl_basic_map_get_space(bmap
);
9208 space
= isl_space_map_from_set(isl_space_range(space
));
9209 id
= isl_basic_map_identity(space
);
9211 sv
= isl_basic_map_is_subset(test
, id
);
9213 isl_basic_map_free(test
);
9214 isl_basic_map_free(id
);
9219 /* Check if the given map is obviously single-valued.
9221 int isl_map_plain_is_single_valued(__isl_keep isl_map
*map
)
9230 return isl_basic_map_plain_is_single_valued(map
->p
[0]);
9233 /* Check if the given map is single-valued.
9238 * and check if the result is a subset of the identity mapping.
9240 int isl_map_is_single_valued(__isl_keep isl_map
*map
)
9247 sv
= isl_map_plain_is_single_valued(map
);
9251 test
= isl_map_reverse(isl_map_copy(map
));
9252 test
= isl_map_apply_range(test
, isl_map_copy(map
));
9254 dim
= isl_space_map_from_set(isl_space_range(isl_map_get_space(map
)));
9255 id
= isl_map_identity(dim
);
9257 sv
= isl_map_is_subset(test
, id
);
9265 int isl_map_is_injective(__isl_keep isl_map
*map
)
9269 map
= isl_map_copy(map
);
9270 map
= isl_map_reverse(map
);
9271 in
= isl_map_is_single_valued(map
);
9277 /* Check if the given map is obviously injective.
9279 int isl_map_plain_is_injective(__isl_keep isl_map
*map
)
9283 map
= isl_map_copy(map
);
9284 map
= isl_map_reverse(map
);
9285 in
= isl_map_plain_is_single_valued(map
);
9291 int isl_map_is_bijective(__isl_keep isl_map
*map
)
9295 sv
= isl_map_is_single_valued(map
);
9299 return isl_map_is_injective(map
);
9302 int isl_set_is_singleton(__isl_keep isl_set
*set
)
9304 return isl_map_is_single_valued((isl_map
*)set
);
9307 int isl_map_is_translation(__isl_keep isl_map
*map
)
9312 delta
= isl_map_deltas(isl_map_copy(map
));
9313 ok
= isl_set_is_singleton(delta
);
9314 isl_set_free(delta
);
9319 static int unique(isl_int
*p
, unsigned pos
, unsigned len
)
9321 if (isl_seq_first_non_zero(p
, pos
) != -1)
9323 if (isl_seq_first_non_zero(p
+ pos
+ 1, len
- pos
- 1) != -1)
9328 int isl_basic_set_is_box(__isl_keep isl_basic_set
*bset
)
9337 if (isl_basic_set_dim(bset
, isl_dim_div
) != 0)
9340 nvar
= isl_basic_set_dim(bset
, isl_dim_set
);
9341 ovar
= isl_space_offset(bset
->dim
, isl_dim_set
);
9342 for (j
= 0; j
< nvar
; ++j
) {
9343 int lower
= 0, upper
= 0;
9344 for (i
= 0; i
< bset
->n_eq
; ++i
) {
9345 if (isl_int_is_zero(bset
->eq
[i
][1 + ovar
+ j
]))
9347 if (!unique(bset
->eq
[i
] + 1 + ovar
, j
, nvar
))
9353 for (i
= 0; i
< bset
->n_ineq
; ++i
) {
9354 if (isl_int_is_zero(bset
->ineq
[i
][1 + ovar
+ j
]))
9356 if (!unique(bset
->ineq
[i
] + 1 + ovar
, j
, nvar
))
9358 if (isl_int_is_pos(bset
->ineq
[i
][1 + ovar
+ j
]))
9363 if (!lower
|| !upper
)
9370 int isl_set_is_box(__isl_keep isl_set
*set
)
9377 return isl_basic_set_is_box(set
->p
[0]);
9380 int isl_basic_set_is_wrapping(__isl_keep isl_basic_set
*bset
)
9385 return isl_space_is_wrapping(bset
->dim
);
9388 int isl_set_is_wrapping(__isl_keep isl_set
*set
)
9393 return isl_space_is_wrapping(set
->dim
);
9396 __isl_give isl_basic_set
*isl_basic_map_wrap(__isl_take isl_basic_map
*bmap
)
9398 bmap
= isl_basic_map_cow(bmap
);
9402 bmap
->dim
= isl_space_wrap(bmap
->dim
);
9406 bmap
= isl_basic_map_finalize(bmap
);
9408 return (isl_basic_set
*)bmap
;
9410 isl_basic_map_free(bmap
);
9414 __isl_give isl_set
*isl_map_wrap(__isl_take isl_map
*map
)
9418 map
= isl_map_cow(map
);
9422 for (i
= 0; i
< map
->n
; ++i
) {
9423 map
->p
[i
] = (isl_basic_map
*)isl_basic_map_wrap(map
->p
[i
]);
9427 map
->dim
= isl_space_wrap(map
->dim
);
9431 return (isl_set
*)map
;
9437 __isl_give isl_basic_map
*isl_basic_set_unwrap(__isl_take isl_basic_set
*bset
)
9439 bset
= isl_basic_set_cow(bset
);
9443 bset
->dim
= isl_space_unwrap(bset
->dim
);
9447 bset
= isl_basic_set_finalize(bset
);
9449 return (isl_basic_map
*)bset
;
9451 isl_basic_set_free(bset
);
9455 __isl_give isl_map
*isl_set_unwrap(__isl_take isl_set
*set
)
9462 if (!isl_set_is_wrapping(set
))
9463 isl_die(set
->ctx
, isl_error_invalid
, "not a wrapping set",
9466 set
= isl_set_cow(set
);
9470 for (i
= 0; i
< set
->n
; ++i
) {
9471 set
->p
[i
] = (isl_basic_set
*)isl_basic_set_unwrap(set
->p
[i
]);
9476 set
->dim
= isl_space_unwrap(set
->dim
);
9480 return (isl_map
*)set
;
9486 __isl_give isl_basic_map
*isl_basic_map_reset(__isl_take isl_basic_map
*bmap
,
9487 enum isl_dim_type type
)
9492 if (!isl_space_is_named_or_nested(bmap
->dim
, type
))
9495 bmap
= isl_basic_map_cow(bmap
);
9499 bmap
->dim
= isl_space_reset(bmap
->dim
, type
);
9503 bmap
= isl_basic_map_finalize(bmap
);
9507 isl_basic_map_free(bmap
);
9511 __isl_give isl_map
*isl_map_reset(__isl_take isl_map
*map
,
9512 enum isl_dim_type type
)
9519 if (!isl_space_is_named_or_nested(map
->dim
, type
))
9522 map
= isl_map_cow(map
);
9526 for (i
= 0; i
< map
->n
; ++i
) {
9527 map
->p
[i
] = isl_basic_map_reset(map
->p
[i
], type
);
9531 map
->dim
= isl_space_reset(map
->dim
, type
);
9541 __isl_give isl_basic_map
*isl_basic_map_flatten(__isl_take isl_basic_map
*bmap
)
9546 if (!bmap
->dim
->nested
[0] && !bmap
->dim
->nested
[1])
9549 bmap
= isl_basic_map_cow(bmap
);
9553 bmap
->dim
= isl_space_flatten(bmap
->dim
);
9557 bmap
= isl_basic_map_finalize(bmap
);
9561 isl_basic_map_free(bmap
);
9565 __isl_give isl_basic_set
*isl_basic_set_flatten(__isl_take isl_basic_set
*bset
)
9567 return (isl_basic_set
*)isl_basic_map_flatten((isl_basic_map
*)bset
);
9570 __isl_give isl_basic_map
*isl_basic_map_flatten_domain(
9571 __isl_take isl_basic_map
*bmap
)
9576 if (!bmap
->dim
->nested
[0])
9579 bmap
= isl_basic_map_cow(bmap
);
9583 bmap
->dim
= isl_space_flatten_domain(bmap
->dim
);
9587 bmap
= isl_basic_map_finalize(bmap
);
9591 isl_basic_map_free(bmap
);
9595 __isl_give isl_basic_map
*isl_basic_map_flatten_range(
9596 __isl_take isl_basic_map
*bmap
)
9601 if (!bmap
->dim
->nested
[1])
9604 bmap
= isl_basic_map_cow(bmap
);
9608 bmap
->dim
= isl_space_flatten_range(bmap
->dim
);
9612 bmap
= isl_basic_map_finalize(bmap
);
9616 isl_basic_map_free(bmap
);
9620 __isl_give isl_map
*isl_map_flatten(__isl_take isl_map
*map
)
9627 if (!map
->dim
->nested
[0] && !map
->dim
->nested
[1])
9630 map
= isl_map_cow(map
);
9634 for (i
= 0; i
< map
->n
; ++i
) {
9635 map
->p
[i
] = isl_basic_map_flatten(map
->p
[i
]);
9639 map
->dim
= isl_space_flatten(map
->dim
);
9649 __isl_give isl_set
*isl_set_flatten(__isl_take isl_set
*set
)
9651 return (isl_set
*)isl_map_flatten((isl_map
*)set
);
9654 __isl_give isl_map
*isl_set_flatten_map(__isl_take isl_set
*set
)
9656 isl_space
*dim
, *flat_dim
;
9659 dim
= isl_set_get_space(set
);
9660 flat_dim
= isl_space_flatten(isl_space_copy(dim
));
9661 map
= isl_map_identity(isl_space_join(isl_space_reverse(dim
), flat_dim
));
9662 map
= isl_map_intersect_domain(map
, set
);
9667 __isl_give isl_map
*isl_map_flatten_domain(__isl_take isl_map
*map
)
9674 if (!map
->dim
->nested
[0])
9677 map
= isl_map_cow(map
);
9681 for (i
= 0; i
< map
->n
; ++i
) {
9682 map
->p
[i
] = isl_basic_map_flatten_domain(map
->p
[i
]);
9686 map
->dim
= isl_space_flatten_domain(map
->dim
);
9696 __isl_give isl_map
*isl_map_flatten_range(__isl_take isl_map
*map
)
9703 if (!map
->dim
->nested
[1])
9706 map
= isl_map_cow(map
);
9710 for (i
= 0; i
< map
->n
; ++i
) {
9711 map
->p
[i
] = isl_basic_map_flatten_range(map
->p
[i
]);
9715 map
->dim
= isl_space_flatten_range(map
->dim
);
9725 /* Reorder the dimensions of "bmap" according to the given dim_map
9726 * and set the dimension specification to "dim".
9728 __isl_give isl_basic_map
*isl_basic_map_realign(__isl_take isl_basic_map
*bmap
,
9729 __isl_take isl_space
*dim
, __isl_take
struct isl_dim_map
*dim_map
)
9734 bmap
= isl_basic_map_cow(bmap
);
9735 if (!bmap
|| !dim
|| !dim_map
)
9738 flags
= bmap
->flags
;
9739 ISL_FL_CLR(flags
, ISL_BASIC_MAP_FINAL
);
9740 ISL_FL_CLR(flags
, ISL_BASIC_MAP_NORMALIZED
);
9741 ISL_FL_CLR(flags
, ISL_BASIC_MAP_NORMALIZED_DIVS
);
9742 res
= isl_basic_map_alloc_space(dim
,
9743 bmap
->n_div
, bmap
->n_eq
, bmap
->n_ineq
);
9744 res
= isl_basic_map_add_constraints_dim_map(res
, bmap
, dim_map
);
9747 res
= isl_basic_map_finalize(res
);
9751 isl_basic_map_free(bmap
);
9752 isl_space_free(dim
);
9756 /* Reorder the dimensions of "map" according to given reordering.
9758 __isl_give isl_map
*isl_map_realign(__isl_take isl_map
*map
,
9759 __isl_take isl_reordering
*r
)
9762 struct isl_dim_map
*dim_map
;
9764 map
= isl_map_cow(map
);
9765 dim_map
= isl_dim_map_from_reordering(r
);
9766 if (!map
|| !r
|| !dim_map
)
9769 for (i
= 0; i
< map
->n
; ++i
) {
9770 struct isl_dim_map
*dim_map_i
;
9772 dim_map_i
= isl_dim_map_extend(dim_map
, map
->p
[i
]);
9774 map
->p
[i
] = isl_basic_map_realign(map
->p
[i
],
9775 isl_space_copy(r
->dim
), dim_map_i
);
9781 map
= isl_map_reset_space(map
, isl_space_copy(r
->dim
));
9783 isl_reordering_free(r
);
9789 isl_reordering_free(r
);
9793 __isl_give isl_set
*isl_set_realign(__isl_take isl_set
*set
,
9794 __isl_take isl_reordering
*r
)
9796 return (isl_set
*)isl_map_realign((isl_map
*)set
, r
);
9799 __isl_give isl_map
*isl_map_align_params(__isl_take isl_map
*map
,
9800 __isl_take isl_space
*model
)
9807 ctx
= isl_space_get_ctx(model
);
9808 if (!isl_space_has_named_params(model
))
9809 isl_die(ctx
, isl_error_invalid
,
9810 "model has unnamed parameters", goto error
);
9811 if (!isl_space_has_named_params(map
->dim
))
9812 isl_die(ctx
, isl_error_invalid
,
9813 "relation has unnamed parameters", goto error
);
9814 if (!isl_space_match(map
->dim
, isl_dim_param
, model
, isl_dim_param
)) {
9815 isl_reordering
*exp
;
9817 model
= isl_space_drop_dims(model
, isl_dim_in
,
9818 0, isl_space_dim(model
, isl_dim_in
));
9819 model
= isl_space_drop_dims(model
, isl_dim_out
,
9820 0, isl_space_dim(model
, isl_dim_out
));
9821 exp
= isl_parameter_alignment_reordering(map
->dim
, model
);
9822 exp
= isl_reordering_extend_space(exp
, isl_map_get_space(map
));
9823 map
= isl_map_realign(map
, exp
);
9826 isl_space_free(model
);
9829 isl_space_free(model
);
9834 __isl_give isl_set
*isl_set_align_params(__isl_take isl_set
*set
,
9835 __isl_take isl_space
*model
)
9837 return isl_map_align_params(set
, model
);
9840 /* Align the parameters of "bmap" to those of "model", introducing
9841 * additional parameters if needed.
9843 __isl_give isl_basic_map
*isl_basic_map_align_params(
9844 __isl_take isl_basic_map
*bmap
, __isl_take isl_space
*model
)
9848 if (!bmap
|| !model
)
9851 ctx
= isl_space_get_ctx(model
);
9852 if (!isl_space_has_named_params(model
))
9853 isl_die(ctx
, isl_error_invalid
,
9854 "model has unnamed parameters", goto error
);
9855 if (!isl_space_has_named_params(bmap
->dim
))
9856 isl_die(ctx
, isl_error_invalid
,
9857 "relation has unnamed parameters", goto error
);
9858 if (!isl_space_match(bmap
->dim
, isl_dim_param
, model
, isl_dim_param
)) {
9859 isl_reordering
*exp
;
9860 struct isl_dim_map
*dim_map
;
9862 model
= isl_space_drop_dims(model
, isl_dim_in
,
9863 0, isl_space_dim(model
, isl_dim_in
));
9864 model
= isl_space_drop_dims(model
, isl_dim_out
,
9865 0, isl_space_dim(model
, isl_dim_out
));
9866 exp
= isl_parameter_alignment_reordering(bmap
->dim
, model
);
9867 exp
= isl_reordering_extend_space(exp
,
9868 isl_basic_map_get_space(bmap
));
9869 dim_map
= isl_dim_map_from_reordering(exp
);
9870 bmap
= isl_basic_map_realign(bmap
,
9871 exp
? isl_space_copy(exp
->dim
) : NULL
,
9872 isl_dim_map_extend(dim_map
, bmap
));
9873 isl_reordering_free(exp
);
9877 isl_space_free(model
);
9880 isl_space_free(model
);
9881 isl_basic_map_free(bmap
);
9885 /* Align the parameters of "bset" to those of "model", introducing
9886 * additional parameters if needed.
9888 __isl_give isl_basic_set
*isl_basic_set_align_params(
9889 __isl_take isl_basic_set
*bset
, __isl_take isl_space
*model
)
9891 return isl_basic_map_align_params(bset
, model
);
9894 __isl_give isl_mat
*isl_basic_map_equalities_matrix(
9895 __isl_keep isl_basic_map
*bmap
, enum isl_dim_type c1
,
9896 enum isl_dim_type c2
, enum isl_dim_type c3
,
9897 enum isl_dim_type c4
, enum isl_dim_type c5
)
9899 enum isl_dim_type c
[5] = { c1
, c2
, c3
, c4
, c5
};
9900 struct isl_mat
*mat
;
9906 mat
= isl_mat_alloc(bmap
->ctx
, bmap
->n_eq
,
9907 isl_basic_map_total_dim(bmap
) + 1);
9910 for (i
= 0; i
< bmap
->n_eq
; ++i
)
9911 for (j
= 0, pos
= 0; j
< 5; ++j
) {
9912 int off
= isl_basic_map_offset(bmap
, c
[j
]);
9913 for (k
= 0; k
< isl_basic_map_dim(bmap
, c
[j
]); ++k
) {
9914 isl_int_set(mat
->row
[i
][pos
],
9915 bmap
->eq
[i
][off
+ k
]);
9923 __isl_give isl_mat
*isl_basic_map_inequalities_matrix(
9924 __isl_keep isl_basic_map
*bmap
, enum isl_dim_type c1
,
9925 enum isl_dim_type c2
, enum isl_dim_type c3
,
9926 enum isl_dim_type c4
, enum isl_dim_type c5
)
9928 enum isl_dim_type c
[5] = { c1
, c2
, c3
, c4
, c5
};
9929 struct isl_mat
*mat
;
9935 mat
= isl_mat_alloc(bmap
->ctx
, bmap
->n_ineq
,
9936 isl_basic_map_total_dim(bmap
) + 1);
9939 for (i
= 0; i
< bmap
->n_ineq
; ++i
)
9940 for (j
= 0, pos
= 0; j
< 5; ++j
) {
9941 int off
= isl_basic_map_offset(bmap
, c
[j
]);
9942 for (k
= 0; k
< isl_basic_map_dim(bmap
, c
[j
]); ++k
) {
9943 isl_int_set(mat
->row
[i
][pos
],
9944 bmap
->ineq
[i
][off
+ k
]);
9952 __isl_give isl_basic_map
*isl_basic_map_from_constraint_matrices(
9953 __isl_take isl_space
*dim
,
9954 __isl_take isl_mat
*eq
, __isl_take isl_mat
*ineq
, enum isl_dim_type c1
,
9955 enum isl_dim_type c2
, enum isl_dim_type c3
,
9956 enum isl_dim_type c4
, enum isl_dim_type c5
)
9958 enum isl_dim_type c
[5] = { c1
, c2
, c3
, c4
, c5
};
9959 isl_basic_map
*bmap
;
9965 if (!dim
|| !eq
|| !ineq
)
9968 if (eq
->n_col
!= ineq
->n_col
)
9969 isl_die(dim
->ctx
, isl_error_invalid
,
9970 "equalities and inequalities matrices should have "
9971 "same number of columns", goto error
);
9973 total
= 1 + isl_space_dim(dim
, isl_dim_all
);
9975 if (eq
->n_col
< total
)
9976 isl_die(dim
->ctx
, isl_error_invalid
,
9977 "number of columns too small", goto error
);
9979 extra
= eq
->n_col
- total
;
9981 bmap
= isl_basic_map_alloc_space(isl_space_copy(dim
), extra
,
9982 eq
->n_row
, ineq
->n_row
);
9985 for (i
= 0; i
< extra
; ++i
) {
9986 k
= isl_basic_map_alloc_div(bmap
);
9989 isl_int_set_si(bmap
->div
[k
][0], 0);
9991 for (i
= 0; i
< eq
->n_row
; ++i
) {
9992 l
= isl_basic_map_alloc_equality(bmap
);
9995 for (j
= 0, pos
= 0; j
< 5; ++j
) {
9996 int off
= isl_basic_map_offset(bmap
, c
[j
]);
9997 for (k
= 0; k
< isl_basic_map_dim(bmap
, c
[j
]); ++k
) {
9998 isl_int_set(bmap
->eq
[l
][off
+ k
],
10004 for (i
= 0; i
< ineq
->n_row
; ++i
) {
10005 l
= isl_basic_map_alloc_inequality(bmap
);
10008 for (j
= 0, pos
= 0; j
< 5; ++j
) {
10009 int off
= isl_basic_map_offset(bmap
, c
[j
]);
10010 for (k
= 0; k
< isl_basic_map_dim(bmap
, c
[j
]); ++k
) {
10011 isl_int_set(bmap
->ineq
[l
][off
+ k
],
10012 ineq
->row
[i
][pos
]);
10018 isl_space_free(dim
);
10020 isl_mat_free(ineq
);
10024 isl_space_free(dim
);
10026 isl_mat_free(ineq
);
10030 __isl_give isl_mat
*isl_basic_set_equalities_matrix(
10031 __isl_keep isl_basic_set
*bset
, enum isl_dim_type c1
,
10032 enum isl_dim_type c2
, enum isl_dim_type c3
, enum isl_dim_type c4
)
10034 return isl_basic_map_equalities_matrix((isl_basic_map
*)bset
,
10035 c1
, c2
, c3
, c4
, isl_dim_in
);
10038 __isl_give isl_mat
*isl_basic_set_inequalities_matrix(
10039 __isl_keep isl_basic_set
*bset
, enum isl_dim_type c1
,
10040 enum isl_dim_type c2
, enum isl_dim_type c3
, enum isl_dim_type c4
)
10042 return isl_basic_map_inequalities_matrix((isl_basic_map
*)bset
,
10043 c1
, c2
, c3
, c4
, isl_dim_in
);
10046 __isl_give isl_basic_set
*isl_basic_set_from_constraint_matrices(
10047 __isl_take isl_space
*dim
,
10048 __isl_take isl_mat
*eq
, __isl_take isl_mat
*ineq
, enum isl_dim_type c1
,
10049 enum isl_dim_type c2
, enum isl_dim_type c3
, enum isl_dim_type c4
)
10051 return (isl_basic_set
*)
10052 isl_basic_map_from_constraint_matrices(dim
, eq
, ineq
,
10053 c1
, c2
, c3
, c4
, isl_dim_in
);
10056 int isl_basic_map_can_zip(__isl_keep isl_basic_map
*bmap
)
10061 return isl_space_can_zip(bmap
->dim
);
10064 int isl_map_can_zip(__isl_keep isl_map
*map
)
10069 return isl_space_can_zip(map
->dim
);
10072 /* Given a basic map (A -> B) -> (C -> D), return the corresponding basic map
10073 * (A -> C) -> (B -> D).
10075 __isl_give isl_basic_map
*isl_basic_map_zip(__isl_take isl_basic_map
*bmap
)
10084 if (!isl_basic_map_can_zip(bmap
))
10085 isl_die(bmap
->ctx
, isl_error_invalid
,
10086 "basic map cannot be zipped", goto error
);
10087 pos
= isl_basic_map_offset(bmap
, isl_dim_in
) +
10088 isl_space_dim(bmap
->dim
->nested
[0], isl_dim_in
);
10089 n1
= isl_space_dim(bmap
->dim
->nested
[0], isl_dim_out
);
10090 n2
= isl_space_dim(bmap
->dim
->nested
[1], isl_dim_in
);
10091 bmap
= isl_basic_map_cow(bmap
);
10092 bmap
= isl_basic_map_swap_vars(bmap
, pos
, n1
, n2
);
10095 bmap
->dim
= isl_space_zip(bmap
->dim
);
10100 isl_basic_map_free(bmap
);
10104 /* Given a map (A -> B) -> (C -> D), return the corresponding map
10105 * (A -> C) -> (B -> D).
10107 __isl_give isl_map
*isl_map_zip(__isl_take isl_map
*map
)
10114 if (!isl_map_can_zip(map
))
10115 isl_die(map
->ctx
, isl_error_invalid
, "map cannot be zipped",
10118 map
= isl_map_cow(map
);
10122 for (i
= 0; i
< map
->n
; ++i
) {
10123 map
->p
[i
] = isl_basic_map_zip(map
->p
[i
]);
10128 map
->dim
= isl_space_zip(map
->dim
);
10138 /* Can we apply isl_basic_map_curry to "bmap"?
10139 * That is, does it have a nested relation in its domain?
10141 int isl_basic_map_can_curry(__isl_keep isl_basic_map
*bmap
)
10146 return isl_space_can_curry(bmap
->dim
);
10149 /* Can we apply isl_map_curry to "map"?
10150 * That is, does it have a nested relation in its domain?
10152 int isl_map_can_curry(__isl_keep isl_map
*map
)
10157 return isl_space_can_curry(map
->dim
);
10160 /* Given a basic map (A -> B) -> C, return the corresponding basic map
10163 __isl_give isl_basic_map
*isl_basic_map_curry(__isl_take isl_basic_map
*bmap
)
10169 if (!isl_basic_map_can_curry(bmap
))
10170 isl_die(bmap
->ctx
, isl_error_invalid
,
10171 "basic map cannot be curried", goto error
);
10172 bmap
->dim
= isl_space_curry(bmap
->dim
);
10177 isl_basic_map_free(bmap
);
10181 /* Given a map (A -> B) -> C, return the corresponding map
10184 __isl_give isl_map
*isl_map_curry(__isl_take isl_map
*map
)
10191 if (!isl_map_can_curry(map
))
10192 isl_die(map
->ctx
, isl_error_invalid
, "map cannot be curried",
10195 map
= isl_map_cow(map
);
10199 for (i
= 0; i
< map
->n
; ++i
) {
10200 map
->p
[i
] = isl_basic_map_curry(map
->p
[i
]);
10205 map
->dim
= isl_space_curry(map
->dim
);
10215 /* Construct a basic map mapping the domain of the affine expression
10216 * to a one-dimensional range prescribed by the affine expression.
10218 __isl_give isl_basic_map
*isl_basic_map_from_aff(__isl_take isl_aff
*aff
)
10222 isl_local_space
*ls
;
10223 isl_basic_map
*bmap
;
10228 ls
= isl_aff_get_local_space(aff
);
10229 bmap
= isl_basic_map_from_local_space(ls
);
10230 bmap
= isl_basic_map_extend_constraints(bmap
, 1, 0);
10231 k
= isl_basic_map_alloc_equality(bmap
);
10235 pos
= isl_basic_map_offset(bmap
, isl_dim_out
);
10236 isl_seq_cpy(bmap
->eq
[k
], aff
->v
->el
+ 1, pos
);
10237 isl_int_neg(bmap
->eq
[k
][pos
], aff
->v
->el
[0]);
10238 isl_seq_cpy(bmap
->eq
[k
] + pos
+ 1, aff
->v
->el
+ 1 + pos
,
10239 aff
->v
->size
- (pos
+ 1));
10242 bmap
= isl_basic_map_finalize(bmap
);
10246 isl_basic_map_free(bmap
);
10250 /* Construct a map mapping the domain of the affine expression
10251 * to a one-dimensional range prescribed by the affine expression.
10253 __isl_give isl_map
*isl_map_from_aff(__isl_take isl_aff
*aff
)
10255 isl_basic_map
*bmap
;
10257 bmap
= isl_basic_map_from_aff(aff
);
10258 return isl_map_from_basic_map(bmap
);
10261 /* Construct a basic map mapping the domain the multi-affine expression
10262 * to its range, with each dimension in the range equated to the
10263 * corresponding affine expression.
10265 __isl_give isl_basic_map
*isl_basic_map_from_multi_aff(
10266 __isl_take isl_multi_aff
*maff
)
10270 isl_basic_map
*bmap
;
10275 if (isl_space_dim(maff
->space
, isl_dim_out
) != maff
->n
)
10276 isl_die(isl_multi_aff_get_ctx(maff
), isl_error_internal
,
10277 "invalid space", return isl_multi_aff_free(maff
));
10279 space
= isl_space_domain(isl_multi_aff_get_space(maff
));
10280 bmap
= isl_basic_map_universe(isl_space_from_domain(space
));
10282 for (i
= 0; i
< maff
->n
; ++i
) {
10284 isl_basic_map
*bmap_i
;
10286 aff
= isl_aff_copy(maff
->p
[i
]);
10287 bmap_i
= isl_basic_map_from_aff(aff
);
10289 bmap
= isl_basic_map_flat_range_product(bmap
, bmap_i
);
10292 bmap
= isl_basic_map_reset_space(bmap
, isl_multi_aff_get_space(maff
));
10294 isl_multi_aff_free(maff
);
10298 /* Construct a map mapping the domain the multi-affine expression
10299 * to its range, with each dimension in the range equated to the
10300 * corresponding affine expression.
10302 __isl_give isl_map
*isl_map_from_multi_aff(__isl_take isl_multi_aff
*maff
)
10304 isl_basic_map
*bmap
;
10306 bmap
= isl_basic_map_from_multi_aff(maff
);
10307 return isl_map_from_basic_map(bmap
);
10310 /* Construct a basic map mapping a domain in the given space to
10311 * to an n-dimensional range, with n the number of elements in the list,
10312 * where each coordinate in the range is prescribed by the
10313 * corresponding affine expression.
10314 * The domains of all affine expressions in the list are assumed to match
10317 __isl_give isl_basic_map
*isl_basic_map_from_aff_list(
10318 __isl_take isl_space
*domain_dim
, __isl_take isl_aff_list
*list
)
10322 isl_basic_map
*bmap
;
10327 dim
= isl_space_from_domain(domain_dim
);
10328 bmap
= isl_basic_map_universe(dim
);
10330 for (i
= 0; i
< list
->n
; ++i
) {
10332 isl_basic_map
*bmap_i
;
10334 aff
= isl_aff_copy(list
->p
[i
]);
10335 bmap_i
= isl_basic_map_from_aff(aff
);
10337 bmap
= isl_basic_map_flat_range_product(bmap
, bmap_i
);
10340 isl_aff_list_free(list
);
10344 __isl_give isl_set
*isl_set_equate(__isl_take isl_set
*set
,
10345 enum isl_dim_type type1
, int pos1
, enum isl_dim_type type2
, int pos2
)
10347 return isl_map_equate(set
, type1
, pos1
, type2
, pos2
);
10350 /* Construct a basic map where the given dimensions are equal to each other.
10352 static __isl_give isl_basic_map
*equator(__isl_take isl_space
*space
,
10353 enum isl_dim_type type1
, int pos1
, enum isl_dim_type type2
, int pos2
)
10355 isl_basic_map
*bmap
= NULL
;
10361 if (pos1
>= isl_space_dim(space
, type1
))
10362 isl_die(isl_space_get_ctx(space
), isl_error_invalid
,
10363 "index out of bounds", goto error
);
10364 if (pos2
>= isl_space_dim(space
, type2
))
10365 isl_die(isl_space_get_ctx(space
), isl_error_invalid
,
10366 "index out of bounds", goto error
);
10368 if (type1
== type2
&& pos1
== pos2
)
10369 return isl_basic_map_universe(space
);
10371 bmap
= isl_basic_map_alloc_space(isl_space_copy(space
), 0, 1, 0);
10372 i
= isl_basic_map_alloc_equality(bmap
);
10375 isl_seq_clr(bmap
->eq
[i
], 1 + isl_basic_map_total_dim(bmap
));
10376 pos1
+= isl_basic_map_offset(bmap
, type1
);
10377 pos2
+= isl_basic_map_offset(bmap
, type2
);
10378 isl_int_set_si(bmap
->eq
[i
][pos1
], -1);
10379 isl_int_set_si(bmap
->eq
[i
][pos2
], 1);
10380 bmap
= isl_basic_map_finalize(bmap
);
10381 isl_space_free(space
);
10384 isl_space_free(space
);
10385 isl_basic_map_free(bmap
);
10389 /* Add a constraint imposing that the given two dimensions are equal.
10391 __isl_give isl_basic_map
*isl_basic_map_equate(__isl_take isl_basic_map
*bmap
,
10392 enum isl_dim_type type1
, int pos1
, enum isl_dim_type type2
, int pos2
)
10396 eq
= equator(isl_basic_map_get_space(bmap
), type1
, pos1
, type2
, pos2
);
10398 bmap
= isl_basic_map_intersect(bmap
, eq
);
10403 /* Add a constraint imposing that the given two dimensions are equal.
10405 __isl_give isl_map
*isl_map_equate(__isl_take isl_map
*map
,
10406 enum isl_dim_type type1
, int pos1
, enum isl_dim_type type2
, int pos2
)
10408 isl_basic_map
*bmap
;
10410 bmap
= equator(isl_map_get_space(map
), type1
, pos1
, type2
, pos2
);
10412 map
= isl_map_intersect(map
, isl_map_from_basic_map(bmap
));
10417 /* Add a constraint imposing that the given two dimensions have opposite values.
10419 __isl_give isl_map
*isl_map_oppose(__isl_take isl_map
*map
,
10420 enum isl_dim_type type1
, int pos1
, enum isl_dim_type type2
, int pos2
)
10422 isl_basic_map
*bmap
= NULL
;
10428 if (pos1
>= isl_map_dim(map
, type1
))
10429 isl_die(map
->ctx
, isl_error_invalid
,
10430 "index out of bounds", goto error
);
10431 if (pos2
>= isl_map_dim(map
, type2
))
10432 isl_die(map
->ctx
, isl_error_invalid
,
10433 "index out of bounds", goto error
);
10435 bmap
= isl_basic_map_alloc_space(isl_map_get_space(map
), 0, 1, 0);
10436 i
= isl_basic_map_alloc_equality(bmap
);
10439 isl_seq_clr(bmap
->eq
[i
], 1 + isl_basic_map_total_dim(bmap
));
10440 pos1
+= isl_basic_map_offset(bmap
, type1
);
10441 pos2
+= isl_basic_map_offset(bmap
, type2
);
10442 isl_int_set_si(bmap
->eq
[i
][pos1
], 1);
10443 isl_int_set_si(bmap
->eq
[i
][pos2
], 1);
10444 bmap
= isl_basic_map_finalize(bmap
);
10446 map
= isl_map_intersect(map
, isl_map_from_basic_map(bmap
));
10450 isl_basic_map_free(bmap
);
10455 /* Add a constraint imposing that the value of the first dimension is
10456 * greater than that of the second.
10458 __isl_give isl_map
*isl_map_order_gt(__isl_take isl_map
*map
,
10459 enum isl_dim_type type1
, int pos1
, enum isl_dim_type type2
, int pos2
)
10461 isl_basic_map
*bmap
= NULL
;
10467 if (pos1
>= isl_map_dim(map
, type1
))
10468 isl_die(map
->ctx
, isl_error_invalid
,
10469 "index out of bounds", goto error
);
10470 if (pos2
>= isl_map_dim(map
, type2
))
10471 isl_die(map
->ctx
, isl_error_invalid
,
10472 "index out of bounds", goto error
);
10474 if (type1
== type2
&& pos1
== pos2
) {
10475 isl_space
*space
= isl_map_get_space(map
);
10477 return isl_map_empty(space
);
10480 bmap
= isl_basic_map_alloc_space(isl_map_get_space(map
), 0, 0, 1);
10481 i
= isl_basic_map_alloc_inequality(bmap
);
10484 isl_seq_clr(bmap
->ineq
[i
], 1 + isl_basic_map_total_dim(bmap
));
10485 pos1
+= isl_basic_map_offset(bmap
, type1
);
10486 pos2
+= isl_basic_map_offset(bmap
, type2
);
10487 isl_int_set_si(bmap
->ineq
[i
][pos1
], 1);
10488 isl_int_set_si(bmap
->ineq
[i
][pos2
], -1);
10489 isl_int_set_si(bmap
->ineq
[i
][0], -1);
10490 bmap
= isl_basic_map_finalize(bmap
);
10492 map
= isl_map_intersect(map
, isl_map_from_basic_map(bmap
));
10496 isl_basic_map_free(bmap
);
10501 /* Add a constraint imposing that the value of the first dimension is
10502 * smaller than that of the second.
10504 __isl_give isl_map
*isl_map_order_lt(__isl_take isl_map
*map
,
10505 enum isl_dim_type type1
, int pos1
, enum isl_dim_type type2
, int pos2
)
10507 return isl_map_order_gt(map
, type2
, pos2
, type1
, pos1
);
10510 __isl_give isl_aff
*isl_basic_map_get_div(__isl_keep isl_basic_map
*bmap
,
10514 isl_local_space
*ls
;
10519 if (!isl_basic_map_divs_known(bmap
))
10520 isl_die(isl_basic_map_get_ctx(bmap
), isl_error_invalid
,
10521 "some divs are unknown", return NULL
);
10523 ls
= isl_basic_map_get_local_space(bmap
);
10524 div
= isl_local_space_get_div(ls
, pos
);
10525 isl_local_space_free(ls
);
10530 __isl_give isl_aff
*isl_basic_set_get_div(__isl_keep isl_basic_set
*bset
,
10533 return isl_basic_map_get_div(bset
, pos
);
10536 /* Plug in "subs" for dimension "type", "pos" of "bset".
10538 * Let i be the dimension to replace and let "subs" be of the form
10542 * Any integer division with a non-zero coefficient for i,
10544 * floor((a i + g)/m)
10548 * floor((a f + d g)/(m d))
10550 * Constraints of the form
10558 __isl_give isl_basic_set
*isl_basic_set_substitute(
10559 __isl_take isl_basic_set
*bset
,
10560 enum isl_dim_type type
, unsigned pos
, __isl_keep isl_aff
*subs
)
10566 if (bset
&& isl_basic_set_plain_is_empty(bset
))
10569 bset
= isl_basic_set_cow(bset
);
10570 if (!bset
|| !subs
)
10573 ctx
= isl_basic_set_get_ctx(bset
);
10574 if (!isl_space_is_equal(bset
->dim
, subs
->ls
->dim
))
10575 isl_die(ctx
, isl_error_invalid
,
10576 "spaces don't match", goto error
);
10577 if (isl_local_space_dim(subs
->ls
, isl_dim_div
) != 0)
10578 isl_die(ctx
, isl_error_unsupported
,
10579 "cannot handle divs yet", goto error
);
10581 pos
+= isl_basic_set_offset(bset
, type
);
10585 for (i
= 0; i
< bset
->n_eq
; ++i
) {
10586 if (isl_int_is_zero(bset
->eq
[i
][pos
]))
10588 isl_int_set(v
, bset
->eq
[i
][pos
]);
10589 isl_int_set_si(bset
->eq
[i
][pos
], 0);
10590 isl_seq_combine(bset
->eq
[i
], subs
->v
->el
[0], bset
->eq
[i
],
10591 v
, subs
->v
->el
+ 1, subs
->v
->size
- 1);
10594 for (i
= 0; i
< bset
->n_ineq
; ++i
) {
10595 if (isl_int_is_zero(bset
->ineq
[i
][pos
]))
10597 isl_int_set(v
, bset
->ineq
[i
][pos
]);
10598 isl_int_set_si(bset
->ineq
[i
][pos
], 0);
10599 isl_seq_combine(bset
->ineq
[i
], subs
->v
->el
[0], bset
->ineq
[i
],
10600 v
, subs
->v
->el
+ 1, subs
->v
->size
- 1);
10603 for (i
= 0; i
< bset
->n_div
; ++i
) {
10604 if (isl_int_is_zero(bset
->div
[i
][1 + pos
]))
10606 isl_int_set(v
, bset
->div
[i
][1 + pos
]);
10607 isl_int_set_si(bset
->div
[i
][1 + pos
], 0);
10608 isl_seq_combine(bset
->div
[i
] + 1,
10609 subs
->v
->el
[0], bset
->div
[i
] + 1,
10610 v
, subs
->v
->el
+ 1, subs
->v
->size
- 1);
10611 isl_int_mul(bset
->div
[i
][0], bset
->div
[i
][0], subs
->v
->el
[0]);
10616 bset
= isl_basic_set_simplify(bset
);
10617 return isl_basic_set_finalize(bset
);
10619 isl_basic_set_free(bset
);
10623 /* Plug in "subs" for dimension "type", "pos" of "set".
10625 __isl_give isl_set
*isl_set_substitute(__isl_take isl_set
*set
,
10626 enum isl_dim_type type
, unsigned pos
, __isl_keep isl_aff
*subs
)
10630 if (set
&& isl_set_plain_is_empty(set
))
10633 set
= isl_set_cow(set
);
10637 for (i
= set
->n
- 1; i
>= 0; --i
) {
10638 set
->p
[i
] = isl_basic_set_substitute(set
->p
[i
], type
, pos
, subs
);
10639 if (remove_if_empty(set
, i
) < 0)