2 * Copyright 2008-2009 Katholieke Universiteit Leuven
3 * Copyright 2012-2013 Ecole Normale Superieure
4 * Copyright 2014 INRIA Rocquencourt
6 * Use of this software is governed by the MIT license
8 * Written by Sven Verdoolaege, K.U.Leuven, Departement
9 * Computerwetenschappen, Celestijnenlaan 200A, B-3001 Leuven, Belgium
10 * and Ecole Normale Superieure, 45 rue d’Ulm, 75230 Paris, France
11 * and Inria Paris - Rocquencourt, Domaine de Voluceau - Rocquencourt,
12 * B.P. 105 - 78153 Le Chesnay, France
15 #include <isl_ctx_private.h>
16 #include <isl_map_private.h>
17 #include "isl_equalities.h"
21 #include <isl_space_private.h>
22 #include <isl_mat_private.h>
23 #include <isl_vec_private.h>
25 static void swap_equality(struct isl_basic_map
*bmap
, int a
, int b
)
27 isl_int
*t
= bmap
->eq
[a
];
28 bmap
->eq
[a
] = bmap
->eq
[b
];
32 static void swap_inequality(struct isl_basic_map
*bmap
, int a
, int b
)
35 isl_int
*t
= bmap
->ineq
[a
];
36 bmap
->ineq
[a
] = bmap
->ineq
[b
];
41 static void constraint_drop_vars(isl_int
*c
, unsigned n
, unsigned rem
)
43 isl_seq_cpy(c
, c
+ n
, rem
);
44 isl_seq_clr(c
+ rem
, n
);
47 /* Drop n dimensions starting at first.
49 * In principle, this frees up some extra variables as the number
50 * of columns remains constant, but we would have to extend
51 * the div array too as the number of rows in this array is assumed
52 * to be equal to extra.
54 struct isl_basic_set
*isl_basic_set_drop_dims(
55 struct isl_basic_set
*bset
, unsigned first
, unsigned n
)
62 isl_assert(bset
->ctx
, first
+ n
<= bset
->dim
->n_out
, goto error
);
64 if (n
== 0 && !isl_space_get_tuple_name(bset
->dim
, isl_dim_set
))
67 bset
= isl_basic_set_cow(bset
);
71 for (i
= 0; i
< bset
->n_eq
; ++i
)
72 constraint_drop_vars(bset
->eq
[i
]+1+bset
->dim
->nparam
+first
, n
,
73 (bset
->dim
->n_out
-first
-n
)+bset
->extra
);
75 for (i
= 0; i
< bset
->n_ineq
; ++i
)
76 constraint_drop_vars(bset
->ineq
[i
]+1+bset
->dim
->nparam
+first
, n
,
77 (bset
->dim
->n_out
-first
-n
)+bset
->extra
);
79 for (i
= 0; i
< bset
->n_div
; ++i
)
80 constraint_drop_vars(bset
->div
[i
]+1+1+bset
->dim
->nparam
+first
, n
,
81 (bset
->dim
->n_out
-first
-n
)+bset
->extra
);
83 bset
->dim
= isl_space_drop_outputs(bset
->dim
, first
, n
);
87 ISL_F_CLR(bset
, ISL_BASIC_SET_NORMALIZED
);
88 bset
= isl_basic_set_simplify(bset
);
89 return isl_basic_set_finalize(bset
);
91 isl_basic_set_free(bset
);
95 struct isl_set
*isl_set_drop_dims(
96 struct isl_set
*set
, unsigned first
, unsigned n
)
103 isl_assert(set
->ctx
, first
+ n
<= set
->dim
->n_out
, goto error
);
105 if (n
== 0 && !isl_space_get_tuple_name(set
->dim
, isl_dim_set
))
107 set
= isl_set_cow(set
);
110 set
->dim
= isl_space_drop_outputs(set
->dim
, first
, n
);
114 for (i
= 0; i
< set
->n
; ++i
) {
115 set
->p
[i
] = isl_basic_set_drop_dims(set
->p
[i
], first
, n
);
120 ISL_F_CLR(set
, ISL_SET_NORMALIZED
);
127 /* Move "n" divs starting at "first" to the end of the list of divs.
129 static struct isl_basic_map
*move_divs_last(struct isl_basic_map
*bmap
,
130 unsigned first
, unsigned n
)
135 if (first
+ n
== bmap
->n_div
)
138 div
= isl_alloc_array(bmap
->ctx
, isl_int
*, n
);
141 for (i
= 0; i
< n
; ++i
)
142 div
[i
] = bmap
->div
[first
+ i
];
143 for (i
= 0; i
< bmap
->n_div
- first
- n
; ++i
)
144 bmap
->div
[first
+ i
] = bmap
->div
[first
+ n
+ i
];
145 for (i
= 0; i
< n
; ++i
)
146 bmap
->div
[bmap
->n_div
- n
+ i
] = div
[i
];
150 isl_basic_map_free(bmap
);
154 /* Drop "n" dimensions of type "type" starting at "first".
156 * In principle, this frees up some extra variables as the number
157 * of columns remains constant, but we would have to extend
158 * the div array too as the number of rows in this array is assumed
159 * to be equal to extra.
161 struct isl_basic_map
*isl_basic_map_drop(struct isl_basic_map
*bmap
,
162 enum isl_dim_type type
, unsigned first
, unsigned n
)
172 dim
= isl_basic_map_dim(bmap
, type
);
173 isl_assert(bmap
->ctx
, first
+ n
<= dim
, goto error
);
175 if (n
== 0 && !isl_space_is_named_or_nested(bmap
->dim
, type
))
178 bmap
= isl_basic_map_cow(bmap
);
182 offset
= isl_basic_map_offset(bmap
, type
) + first
;
183 left
= isl_basic_map_total_dim(bmap
) - (offset
- 1) - n
;
184 for (i
= 0; i
< bmap
->n_eq
; ++i
)
185 constraint_drop_vars(bmap
->eq
[i
]+offset
, n
, left
);
187 for (i
= 0; i
< bmap
->n_ineq
; ++i
)
188 constraint_drop_vars(bmap
->ineq
[i
]+offset
, n
, left
);
190 for (i
= 0; i
< bmap
->n_div
; ++i
)
191 constraint_drop_vars(bmap
->div
[i
]+1+offset
, n
, left
);
193 if (type
== isl_dim_div
) {
194 bmap
= move_divs_last(bmap
, first
, n
);
197 isl_basic_map_free_div(bmap
, n
);
199 bmap
->dim
= isl_space_drop_dims(bmap
->dim
, type
, first
, n
);
203 ISL_F_CLR(bmap
, ISL_BASIC_MAP_NORMALIZED
);
204 bmap
= isl_basic_map_simplify(bmap
);
205 return isl_basic_map_finalize(bmap
);
207 isl_basic_map_free(bmap
);
211 __isl_give isl_basic_set
*isl_basic_set_drop(__isl_take isl_basic_set
*bset
,
212 enum isl_dim_type type
, unsigned first
, unsigned n
)
214 return (isl_basic_set
*)isl_basic_map_drop((isl_basic_map
*)bset
,
218 struct isl_basic_map
*isl_basic_map_drop_inputs(
219 struct isl_basic_map
*bmap
, unsigned first
, unsigned n
)
221 return isl_basic_map_drop(bmap
, isl_dim_in
, first
, n
);
224 struct isl_map
*isl_map_drop(struct isl_map
*map
,
225 enum isl_dim_type type
, unsigned first
, unsigned n
)
232 isl_assert(map
->ctx
, first
+ n
<= isl_map_dim(map
, type
), goto error
);
234 if (n
== 0 && !isl_space_get_tuple_name(map
->dim
, type
))
236 map
= isl_map_cow(map
);
239 map
->dim
= isl_space_drop_dims(map
->dim
, type
, first
, n
);
243 for (i
= 0; i
< map
->n
; ++i
) {
244 map
->p
[i
] = isl_basic_map_drop(map
->p
[i
], type
, first
, n
);
248 ISL_F_CLR(map
, ISL_MAP_NORMALIZED
);
256 struct isl_set
*isl_set_drop(struct isl_set
*set
,
257 enum isl_dim_type type
, unsigned first
, unsigned n
)
259 return (isl_set
*)isl_map_drop((isl_map
*)set
, type
, first
, n
);
262 struct isl_map
*isl_map_drop_inputs(
263 struct isl_map
*map
, unsigned first
, unsigned n
)
265 return isl_map_drop(map
, isl_dim_in
, first
, n
);
269 * We don't cow, as the div is assumed to be redundant.
271 static struct isl_basic_map
*isl_basic_map_drop_div(
272 struct isl_basic_map
*bmap
, unsigned div
)
280 pos
= 1 + isl_space_dim(bmap
->dim
, isl_dim_all
) + div
;
282 isl_assert(bmap
->ctx
, div
< bmap
->n_div
, goto error
);
284 for (i
= 0; i
< bmap
->n_eq
; ++i
)
285 constraint_drop_vars(bmap
->eq
[i
]+pos
, 1, bmap
->extra
-div
-1);
287 for (i
= 0; i
< bmap
->n_ineq
; ++i
) {
288 if (!isl_int_is_zero(bmap
->ineq
[i
][pos
])) {
289 isl_basic_map_drop_inequality(bmap
, i
);
293 constraint_drop_vars(bmap
->ineq
[i
]+pos
, 1, bmap
->extra
-div
-1);
296 for (i
= 0; i
< bmap
->n_div
; ++i
)
297 constraint_drop_vars(bmap
->div
[i
]+1+pos
, 1, bmap
->extra
-div
-1);
299 if (div
!= bmap
->n_div
- 1) {
301 isl_int
*t
= bmap
->div
[div
];
303 for (j
= div
; j
< bmap
->n_div
- 1; ++j
)
304 bmap
->div
[j
] = bmap
->div
[j
+1];
306 bmap
->div
[bmap
->n_div
- 1] = t
;
308 ISL_F_CLR(bmap
, ISL_BASIC_MAP_NORMALIZED
);
309 isl_basic_map_free_div(bmap
, 1);
313 isl_basic_map_free(bmap
);
317 struct isl_basic_map
*isl_basic_map_normalize_constraints(
318 struct isl_basic_map
*bmap
)
322 unsigned total
= isl_basic_map_total_dim(bmap
);
328 for (i
= bmap
->n_eq
- 1; i
>= 0; --i
) {
329 isl_seq_gcd(bmap
->eq
[i
]+1, total
, &gcd
);
330 if (isl_int_is_zero(gcd
)) {
331 if (!isl_int_is_zero(bmap
->eq
[i
][0])) {
332 bmap
= isl_basic_map_set_to_empty(bmap
);
335 isl_basic_map_drop_equality(bmap
, i
);
338 if (ISL_F_ISSET(bmap
, ISL_BASIC_MAP_RATIONAL
))
339 isl_int_gcd(gcd
, gcd
, bmap
->eq
[i
][0]);
340 if (isl_int_is_one(gcd
))
342 if (!isl_int_is_divisible_by(bmap
->eq
[i
][0], gcd
)) {
343 bmap
= isl_basic_map_set_to_empty(bmap
);
346 isl_seq_scale_down(bmap
->eq
[i
], bmap
->eq
[i
], gcd
, 1+total
);
349 for (i
= bmap
->n_ineq
- 1; i
>= 0; --i
) {
350 isl_seq_gcd(bmap
->ineq
[i
]+1, total
, &gcd
);
351 if (isl_int_is_zero(gcd
)) {
352 if (isl_int_is_neg(bmap
->ineq
[i
][0])) {
353 bmap
= isl_basic_map_set_to_empty(bmap
);
356 isl_basic_map_drop_inequality(bmap
, i
);
359 if (ISL_F_ISSET(bmap
, ISL_BASIC_MAP_RATIONAL
))
360 isl_int_gcd(gcd
, gcd
, bmap
->ineq
[i
][0]);
361 if (isl_int_is_one(gcd
))
363 isl_int_fdiv_q(bmap
->ineq
[i
][0], bmap
->ineq
[i
][0], gcd
);
364 isl_seq_scale_down(bmap
->ineq
[i
]+1, bmap
->ineq
[i
]+1, gcd
, total
);
371 struct isl_basic_set
*isl_basic_set_normalize_constraints(
372 struct isl_basic_set
*bset
)
374 return (struct isl_basic_set
*)isl_basic_map_normalize_constraints(
375 (struct isl_basic_map
*)bset
);
378 /* Assuming the variable at position "pos" has an integer coefficient
379 * in integer division "div", extract it from this integer division.
380 * "pos" is as determined by isl_basic_map_offset, i.e., pos == 0
381 * corresponds to the constant term.
383 * That is, the integer division is of the form
385 * floor((... + c * d * x_pos + ...)/d)
389 * floor((... + 0 * x_pos + ...)/d) + c * x_pos
391 static __isl_give isl_basic_map
*remove_var_from_div(
392 __isl_take isl_basic_map
*bmap
, int div
, int pos
)
397 isl_int_divexact(shift
, bmap
->div
[div
][1 + pos
], bmap
->div
[div
][0]);
398 isl_int_neg(shift
, shift
);
399 bmap
= isl_basic_map_shift_div(bmap
, div
, pos
, shift
);
400 isl_int_clear(shift
);
405 /* Check if integer division "div" has any integral coefficient
406 * (or constant term). If so, extract them from the integer division.
408 static __isl_give isl_basic_map
*remove_independent_vars_from_div(
409 __isl_take isl_basic_map
*bmap
, int div
)
412 unsigned total
= 1 + isl_basic_map_total_dim(bmap
);
414 for (i
= 0; i
< total
; ++i
) {
415 if (isl_int_is_zero(bmap
->div
[div
][1 + i
]))
417 if (!isl_int_is_divisible_by(bmap
->div
[div
][1 + i
],
420 bmap
= remove_var_from_div(bmap
, div
, i
);
428 /* Check if any known integer division has any integral coefficient
429 * (or constant term). If so, extract them from the integer division.
431 static __isl_give isl_basic_map
*remove_independent_vars_from_divs(
432 __isl_take isl_basic_map
*bmap
)
438 if (bmap
->n_div
== 0)
441 for (i
= 0; i
< bmap
->n_div
; ++i
) {
442 if (isl_int_is_zero(bmap
->div
[i
][0]))
444 bmap
= remove_independent_vars_from_div(bmap
, i
);
452 /* Remove any common factor in numerator and denominator of the div expression,
453 * not taking into account the constant term.
454 * That is, if the div is of the form
456 * floor((a + m f(x))/(m d))
460 * floor((floor(a/m) + f(x))/d)
462 * The difference {a/m}/d in the argument satisfies 0 <= {a/m}/d < 1/d
463 * and can therefore not influence the result of the floor.
465 static void normalize_div_expression(__isl_keep isl_basic_map
*bmap
, int div
)
467 unsigned total
= isl_basic_map_total_dim(bmap
);
468 isl_ctx
*ctx
= bmap
->ctx
;
470 if (isl_int_is_zero(bmap
->div
[div
][0]))
472 isl_seq_gcd(bmap
->div
[div
] + 2, total
, &ctx
->normalize_gcd
);
473 isl_int_gcd(ctx
->normalize_gcd
, ctx
->normalize_gcd
, bmap
->div
[div
][0]);
474 if (isl_int_is_one(ctx
->normalize_gcd
))
476 isl_int_fdiv_q(bmap
->div
[div
][1], bmap
->div
[div
][1],
478 isl_int_divexact(bmap
->div
[div
][0], bmap
->div
[div
][0],
480 isl_seq_scale_down(bmap
->div
[div
] + 2, bmap
->div
[div
] + 2,
481 ctx
->normalize_gcd
, total
);
484 /* Remove any common factor in numerator and denominator of a div expression,
485 * not taking into account the constant term.
486 * That is, look for any div of the form
488 * floor((a + m f(x))/(m d))
492 * floor((floor(a/m) + f(x))/d)
494 * The difference {a/m}/d in the argument satisfies 0 <= {a/m}/d < 1/d
495 * and can therefore not influence the result of the floor.
497 static __isl_give isl_basic_map
*normalize_div_expressions(
498 __isl_take isl_basic_map
*bmap
)
504 if (bmap
->n_div
== 0)
507 for (i
= 0; i
< bmap
->n_div
; ++i
)
508 normalize_div_expression(bmap
, i
);
513 /* Assumes divs have been ordered if keep_divs is set.
515 static void eliminate_var_using_equality(struct isl_basic_map
*bmap
,
516 unsigned pos
, isl_int
*eq
, int keep_divs
, int *progress
)
519 unsigned space_total
;
523 total
= isl_basic_map_total_dim(bmap
);
524 space_total
= isl_space_dim(bmap
->dim
, isl_dim_all
);
525 last_div
= isl_seq_last_non_zero(eq
+ 1 + space_total
, bmap
->n_div
);
526 for (k
= 0; k
< bmap
->n_eq
; ++k
) {
527 if (bmap
->eq
[k
] == eq
)
529 if (isl_int_is_zero(bmap
->eq
[k
][1+pos
]))
533 isl_seq_elim(bmap
->eq
[k
], eq
, 1+pos
, 1+total
, NULL
);
534 isl_seq_normalize(bmap
->ctx
, bmap
->eq
[k
], 1 + total
);
537 for (k
= 0; k
< bmap
->n_ineq
; ++k
) {
538 if (isl_int_is_zero(bmap
->ineq
[k
][1+pos
]))
542 isl_seq_elim(bmap
->ineq
[k
], eq
, 1+pos
, 1+total
, NULL
);
543 isl_seq_normalize(bmap
->ctx
, bmap
->ineq
[k
], 1 + total
);
544 ISL_F_CLR(bmap
, ISL_BASIC_MAP_NORMALIZED
);
547 for (k
= 0; k
< bmap
->n_div
; ++k
) {
548 if (isl_int_is_zero(bmap
->div
[k
][0]))
550 if (isl_int_is_zero(bmap
->div
[k
][1+1+pos
]))
554 /* We need to be careful about circular definitions,
555 * so for now we just remove the definition of div k
556 * if the equality contains any divs.
557 * If keep_divs is set, then the divs have been ordered
558 * and we can keep the definition as long as the result
561 if (last_div
== -1 || (keep_divs
&& last_div
< k
)) {
562 isl_seq_elim(bmap
->div
[k
]+1, eq
,
563 1+pos
, 1+total
, &bmap
->div
[k
][0]);
564 normalize_div_expression(bmap
, k
);
566 isl_seq_clr(bmap
->div
[k
], 1 + total
);
567 ISL_F_CLR(bmap
, ISL_BASIC_MAP_NORMALIZED
);
571 /* Assumes divs have been ordered if keep_divs is set.
573 static __isl_give isl_basic_map
*eliminate_div(__isl_take isl_basic_map
*bmap
,
574 isl_int
*eq
, unsigned div
, int keep_divs
)
576 unsigned pos
= isl_space_dim(bmap
->dim
, isl_dim_all
) + div
;
578 eliminate_var_using_equality(bmap
, pos
, eq
, keep_divs
, NULL
);
580 bmap
= isl_basic_map_drop_div(bmap
, div
);
585 /* Check if elimination of div "div" using equality "eq" would not
586 * result in a div depending on a later div.
588 static int ok_to_eliminate_div(struct isl_basic_map
*bmap
, isl_int
*eq
,
593 unsigned space_total
= isl_space_dim(bmap
->dim
, isl_dim_all
);
594 unsigned pos
= space_total
+ div
;
596 last_div
= isl_seq_last_non_zero(eq
+ 1 + space_total
, bmap
->n_div
);
597 if (last_div
< 0 || last_div
<= div
)
600 for (k
= 0; k
<= last_div
; ++k
) {
601 if (isl_int_is_zero(bmap
->div
[k
][0]))
603 if (!isl_int_is_zero(bmap
->div
[k
][1 + 1 + pos
]))
610 /* Elimininate divs based on equalities
612 static struct isl_basic_map
*eliminate_divs_eq(
613 struct isl_basic_map
*bmap
, int *progress
)
620 bmap
= isl_basic_map_order_divs(bmap
);
625 off
= 1 + isl_space_dim(bmap
->dim
, isl_dim_all
);
627 for (d
= bmap
->n_div
- 1; d
>= 0 ; --d
) {
628 for (i
= 0; i
< bmap
->n_eq
; ++i
) {
629 if (!isl_int_is_one(bmap
->eq
[i
][off
+ d
]) &&
630 !isl_int_is_negone(bmap
->eq
[i
][off
+ d
]))
632 if (!ok_to_eliminate_div(bmap
, bmap
->eq
[i
], d
))
636 bmap
= eliminate_div(bmap
, bmap
->eq
[i
], d
, 1);
637 if (isl_basic_map_drop_equality(bmap
, i
) < 0)
638 return isl_basic_map_free(bmap
);
643 return eliminate_divs_eq(bmap
, progress
);
647 /* Elimininate divs based on inequalities
649 static struct isl_basic_map
*eliminate_divs_ineq(
650 struct isl_basic_map
*bmap
, int *progress
)
661 off
= 1 + isl_space_dim(bmap
->dim
, isl_dim_all
);
663 for (d
= bmap
->n_div
- 1; d
>= 0 ; --d
) {
664 for (i
= 0; i
< bmap
->n_eq
; ++i
)
665 if (!isl_int_is_zero(bmap
->eq
[i
][off
+ d
]))
669 for (i
= 0; i
< bmap
->n_ineq
; ++i
)
670 if (isl_int_abs_gt(bmap
->ineq
[i
][off
+ d
], ctx
->one
))
672 if (i
< bmap
->n_ineq
)
675 bmap
= isl_basic_map_eliminate_vars(bmap
, (off
-1)+d
, 1);
676 if (!bmap
|| ISL_F_ISSET(bmap
, ISL_BASIC_MAP_EMPTY
))
678 bmap
= isl_basic_map_drop_div(bmap
, d
);
685 struct isl_basic_map
*isl_basic_map_gauss(
686 struct isl_basic_map
*bmap
, int *progress
)
694 bmap
= isl_basic_map_order_divs(bmap
);
699 total
= isl_basic_map_total_dim(bmap
);
700 total_var
= total
- bmap
->n_div
;
702 last_var
= total
- 1;
703 for (done
= 0; done
< bmap
->n_eq
; ++done
) {
704 for (; last_var
>= 0; --last_var
) {
705 for (k
= done
; k
< bmap
->n_eq
; ++k
)
706 if (!isl_int_is_zero(bmap
->eq
[k
][1+last_var
]))
714 swap_equality(bmap
, k
, done
);
715 if (isl_int_is_neg(bmap
->eq
[done
][1+last_var
]))
716 isl_seq_neg(bmap
->eq
[done
], bmap
->eq
[done
], 1+total
);
718 eliminate_var_using_equality(bmap
, last_var
, bmap
->eq
[done
], 1,
721 if (last_var
>= total_var
&&
722 isl_int_is_zero(bmap
->div
[last_var
- total_var
][0])) {
723 unsigned div
= last_var
- total_var
;
724 isl_seq_neg(bmap
->div
[div
]+1, bmap
->eq
[done
], 1+total
);
725 isl_int_set_si(bmap
->div
[div
][1+1+last_var
], 0);
726 isl_int_set(bmap
->div
[div
][0],
727 bmap
->eq
[done
][1+last_var
]);
730 ISL_F_CLR(bmap
, ISL_BASIC_MAP_NORMALIZED
);
733 if (done
== bmap
->n_eq
)
735 for (k
= done
; k
< bmap
->n_eq
; ++k
) {
736 if (isl_int_is_zero(bmap
->eq
[k
][0]))
738 return isl_basic_map_set_to_empty(bmap
);
740 isl_basic_map_free_equality(bmap
, bmap
->n_eq
-done
);
744 struct isl_basic_set
*isl_basic_set_gauss(
745 struct isl_basic_set
*bset
, int *progress
)
747 return (struct isl_basic_set
*)isl_basic_map_gauss(
748 (struct isl_basic_map
*)bset
, progress
);
752 static unsigned int round_up(unsigned int v
)
763 static int hash_index(isl_int
***index
, unsigned int size
, int bits
,
764 struct isl_basic_map
*bmap
, int k
)
767 unsigned total
= isl_basic_map_total_dim(bmap
);
768 uint32_t hash
= isl_seq_get_hash_bits(bmap
->ineq
[k
]+1, total
, bits
);
769 for (h
= hash
; index
[h
]; h
= (h
+1) % size
)
770 if (&bmap
->ineq
[k
] != index
[h
] &&
771 isl_seq_eq(bmap
->ineq
[k
]+1, index
[h
][0]+1, total
))
776 static int set_hash_index(isl_int
***index
, unsigned int size
, int bits
,
777 struct isl_basic_set
*bset
, int k
)
779 return hash_index(index
, size
, bits
, (struct isl_basic_map
*)bset
, k
);
782 /* If we can eliminate more than one div, then we need to make
783 * sure we do it from last div to first div, in order not to
784 * change the position of the other divs that still need to
787 static struct isl_basic_map
*remove_duplicate_divs(
788 struct isl_basic_map
*bmap
, int *progress
)
800 bmap
= isl_basic_map_order_divs(bmap
);
801 if (!bmap
|| bmap
->n_div
<= 1)
804 total_var
= isl_space_dim(bmap
->dim
, isl_dim_all
);
805 total
= total_var
+ bmap
->n_div
;
808 for (k
= bmap
->n_div
- 1; k
>= 0; --k
)
809 if (!isl_int_is_zero(bmap
->div
[k
][0]))
814 size
= round_up(4 * bmap
->n_div
/ 3 - 1);
817 elim_for
= isl_calloc_array(ctx
, int, bmap
->n_div
);
818 bits
= ffs(size
) - 1;
819 index
= isl_calloc_array(ctx
, int, size
);
820 if (!elim_for
|| !index
)
822 eq
= isl_blk_alloc(ctx
, 1+total
);
823 if (isl_blk_is_error(eq
))
826 isl_seq_clr(eq
.data
, 1+total
);
827 index
[isl_seq_get_hash_bits(bmap
->div
[k
], 2+total
, bits
)] = k
+ 1;
828 for (--k
; k
>= 0; --k
) {
831 if (isl_int_is_zero(bmap
->div
[k
][0]))
834 hash
= isl_seq_get_hash_bits(bmap
->div
[k
], 2+total
, bits
);
835 for (h
= hash
; index
[h
]; h
= (h
+1) % size
)
836 if (isl_seq_eq(bmap
->div
[k
],
837 bmap
->div
[index
[h
]-1], 2+total
))
846 for (l
= bmap
->n_div
- 1; l
>= 0; --l
) {
850 isl_int_set_si(eq
.data
[1+total_var
+k
], -1);
851 isl_int_set_si(eq
.data
[1+total_var
+l
], 1);
852 bmap
= eliminate_div(bmap
, eq
.data
, l
, 1);
855 isl_int_set_si(eq
.data
[1+total_var
+k
], 0);
856 isl_int_set_si(eq
.data
[1+total_var
+l
], 0);
859 isl_blk_free(ctx
, eq
);
866 static int n_pure_div_eq(struct isl_basic_map
*bmap
)
871 total
= isl_space_dim(bmap
->dim
, isl_dim_all
);
872 for (i
= 0, j
= bmap
->n_div
-1; i
< bmap
->n_eq
; ++i
) {
873 while (j
>= 0 && isl_int_is_zero(bmap
->eq
[i
][1 + total
+ j
]))
877 if (isl_seq_first_non_zero(bmap
->eq
[i
] + 1 + total
, j
) != -1)
883 /* Normalize divs that appear in equalities.
885 * In particular, we assume that bmap contains some equalities
890 * and we want to replace the set of e_i by a minimal set and
891 * such that the new e_i have a canonical representation in terms
893 * If any of the equalities involves more than one divs, then
894 * we currently simply bail out.
896 * Let us first additionally assume that all equalities involve
897 * a div. The equalities then express modulo constraints on the
898 * remaining variables and we can use "parameter compression"
899 * to find a minimal set of constraints. The result is a transformation
901 * x = T(x') = x_0 + G x'
903 * with G a lower-triangular matrix with all elements below the diagonal
904 * non-negative and smaller than the diagonal element on the same row.
905 * We first normalize x_0 by making the same property hold in the affine
907 * The rows i of G with a 1 on the diagonal do not impose any modulo
908 * constraint and simply express x_i = x'_i.
909 * For each of the remaining rows i, we introduce a div and a corresponding
910 * equality. In particular
912 * g_ii e_j = x_i - g_i(x')
914 * where each x'_k is replaced either by x_k (if g_kk = 1) or the
915 * corresponding div (if g_kk != 1).
917 * If there are any equalities not involving any div, then we
918 * first apply a variable compression on the variables x:
920 * x = C x'' x'' = C_2 x
922 * and perform the above parameter compression on A C instead of on A.
923 * The resulting compression is then of the form
925 * x'' = T(x') = x_0 + G x'
927 * and in constructing the new divs and the corresponding equalities,
928 * we have to replace each x'', i.e., the x'_k with (g_kk = 1),
929 * by the corresponding row from C_2.
931 static struct isl_basic_map
*normalize_divs(
932 struct isl_basic_map
*bmap
, int *progress
)
939 struct isl_mat
*T
= NULL
;
940 struct isl_mat
*C
= NULL
;
941 struct isl_mat
*C2
= NULL
;
949 if (bmap
->n_div
== 0)
955 if (ISL_F_ISSET(bmap
, ISL_BASIC_MAP_NORMALIZED_DIVS
))
958 total
= isl_space_dim(bmap
->dim
, isl_dim_all
);
959 div_eq
= n_pure_div_eq(bmap
);
963 if (div_eq
< bmap
->n_eq
) {
964 B
= isl_mat_sub_alloc6(bmap
->ctx
, bmap
->eq
, div_eq
,
965 bmap
->n_eq
- div_eq
, 0, 1 + total
);
966 C
= isl_mat_variable_compression(B
, &C2
);
970 bmap
= isl_basic_map_set_to_empty(bmap
);
977 d
= isl_vec_alloc(bmap
->ctx
, div_eq
);
980 for (i
= 0, j
= bmap
->n_div
-1; i
< div_eq
; ++i
) {
981 while (j
>= 0 && isl_int_is_zero(bmap
->eq
[i
][1 + total
+ j
]))
983 isl_int_set(d
->block
.data
[i
], bmap
->eq
[i
][1 + total
+ j
]);
985 B
= isl_mat_sub_alloc6(bmap
->ctx
, bmap
->eq
, 0, div_eq
, 0, 1 + total
);
988 B
= isl_mat_product(B
, C
);
992 T
= isl_mat_parameter_compression(B
, d
);
996 bmap
= isl_basic_map_set_to_empty(bmap
);
1002 for (i
= 0; i
< T
->n_row
- 1; ++i
) {
1003 isl_int_fdiv_q(v
, T
->row
[1 + i
][0], T
->row
[1 + i
][1 + i
]);
1004 if (isl_int_is_zero(v
))
1006 isl_mat_col_submul(T
, 0, v
, 1 + i
);
1009 pos
= isl_alloc_array(bmap
->ctx
, int, T
->n_row
);
1012 /* We have to be careful because dropping equalities may reorder them */
1014 for (j
= bmap
->n_div
- 1; j
>= 0; --j
) {
1015 for (i
= 0; i
< bmap
->n_eq
; ++i
)
1016 if (!isl_int_is_zero(bmap
->eq
[i
][1 + total
+ j
]))
1018 if (i
< bmap
->n_eq
) {
1019 bmap
= isl_basic_map_drop_div(bmap
, j
);
1020 isl_basic_map_drop_equality(bmap
, i
);
1026 for (i
= 1; i
< T
->n_row
; ++i
) {
1027 if (isl_int_is_one(T
->row
[i
][i
]))
1032 if (needed
> dropped
) {
1033 bmap
= isl_basic_map_extend_space(bmap
, isl_space_copy(bmap
->dim
),
1038 for (i
= 1; i
< T
->n_row
; ++i
) {
1039 if (isl_int_is_one(T
->row
[i
][i
]))
1041 k
= isl_basic_map_alloc_div(bmap
);
1042 pos
[i
] = 1 + total
+ k
;
1043 isl_seq_clr(bmap
->div
[k
] + 1, 1 + total
+ bmap
->n_div
);
1044 isl_int_set(bmap
->div
[k
][0], T
->row
[i
][i
]);
1046 isl_seq_cpy(bmap
->div
[k
] + 1, C2
->row
[i
], 1 + total
);
1048 isl_int_set_si(bmap
->div
[k
][1 + i
], 1);
1049 for (j
= 0; j
< i
; ++j
) {
1050 if (isl_int_is_zero(T
->row
[i
][j
]))
1052 if (pos
[j
] < T
->n_row
&& C2
)
1053 isl_seq_submul(bmap
->div
[k
] + 1, T
->row
[i
][j
],
1054 C2
->row
[pos
[j
]], 1 + total
);
1056 isl_int_neg(bmap
->div
[k
][1 + pos
[j
]],
1059 j
= isl_basic_map_alloc_equality(bmap
);
1060 isl_seq_neg(bmap
->eq
[j
], bmap
->div
[k
]+1, 1+total
+bmap
->n_div
);
1061 isl_int_set(bmap
->eq
[j
][pos
[i
]], bmap
->div
[k
][0]);
1070 ISL_F_SET(bmap
, ISL_BASIC_MAP_NORMALIZED_DIVS
);
1080 static struct isl_basic_map
*set_div_from_lower_bound(
1081 struct isl_basic_map
*bmap
, int div
, int ineq
)
1083 unsigned total
= 1 + isl_space_dim(bmap
->dim
, isl_dim_all
);
1085 isl_seq_neg(bmap
->div
[div
] + 1, bmap
->ineq
[ineq
], total
+ bmap
->n_div
);
1086 isl_int_set(bmap
->div
[div
][0], bmap
->ineq
[ineq
][total
+ div
]);
1087 isl_int_add(bmap
->div
[div
][1], bmap
->div
[div
][1], bmap
->div
[div
][0]);
1088 isl_int_sub_ui(bmap
->div
[div
][1], bmap
->div
[div
][1], 1);
1089 isl_int_set_si(bmap
->div
[div
][1 + total
+ div
], 0);
1094 /* Check whether it is ok to define a div based on an inequality.
1095 * To avoid the introduction of circular definitions of divs, we
1096 * do not allow such a definition if the resulting expression would refer to
1097 * any other undefined divs or if any known div is defined in
1098 * terms of the unknown div.
1100 static int ok_to_set_div_from_bound(struct isl_basic_map
*bmap
,
1104 unsigned total
= 1 + isl_space_dim(bmap
->dim
, isl_dim_all
);
1106 /* Not defined in terms of unknown divs */
1107 for (j
= 0; j
< bmap
->n_div
; ++j
) {
1110 if (isl_int_is_zero(bmap
->ineq
[ineq
][total
+ j
]))
1112 if (isl_int_is_zero(bmap
->div
[j
][0]))
1116 /* No other div defined in terms of this one => avoid loops */
1117 for (j
= 0; j
< bmap
->n_div
; ++j
) {
1120 if (isl_int_is_zero(bmap
->div
[j
][0]))
1122 if (!isl_int_is_zero(bmap
->div
[j
][1 + total
+ div
]))
1129 /* Would an expression for div "div" based on inequality "ineq" of "bmap"
1130 * be a better expression than the current one?
1132 * If we do not have any expression yet, then any expression would be better.
1133 * Otherwise we check if the last variable involved in the inequality
1134 * (disregarding the div that it would define) is in an earlier position
1135 * than the last variable involved in the current div expression.
1137 static int better_div_constraint(__isl_keep isl_basic_map
*bmap
,
1140 unsigned total
= 1 + isl_space_dim(bmap
->dim
, isl_dim_all
);
1144 if (isl_int_is_zero(bmap
->div
[div
][0]))
1147 if (isl_seq_last_non_zero(bmap
->ineq
[ineq
] + total
+ div
+ 1,
1148 bmap
->n_div
- (div
+ 1)) >= 0)
1151 last_ineq
= isl_seq_last_non_zero(bmap
->ineq
[ineq
], total
+ div
);
1152 last_div
= isl_seq_last_non_zero(bmap
->div
[div
] + 1,
1153 total
+ bmap
->n_div
);
1155 return last_ineq
< last_div
;
1158 /* Given two constraints "k" and "l" that are opposite to each other,
1159 * except for the constant term, check if we can use them
1160 * to obtain an expression for one of the hitherto unknown divs or
1161 * a "better" expression for a div for which we already have an expression.
1162 * "sum" is the sum of the constant terms of the constraints.
1163 * If this sum is strictly smaller than the coefficient of one
1164 * of the divs, then this pair can be used define the div.
1165 * To avoid the introduction of circular definitions of divs, we
1166 * do not use the pair if the resulting expression would refer to
1167 * any other undefined divs or if any known div is defined in
1168 * terms of the unknown div.
1170 static struct isl_basic_map
*check_for_div_constraints(
1171 struct isl_basic_map
*bmap
, int k
, int l
, isl_int sum
, int *progress
)
1174 unsigned total
= 1 + isl_space_dim(bmap
->dim
, isl_dim_all
);
1176 for (i
= 0; i
< bmap
->n_div
; ++i
) {
1177 if (isl_int_is_zero(bmap
->ineq
[k
][total
+ i
]))
1179 if (isl_int_abs_ge(sum
, bmap
->ineq
[k
][total
+ i
]))
1181 if (!better_div_constraint(bmap
, i
, k
))
1183 if (!ok_to_set_div_from_bound(bmap
, i
, k
))
1185 if (isl_int_is_pos(bmap
->ineq
[k
][total
+ i
]))
1186 bmap
= set_div_from_lower_bound(bmap
, i
, k
);
1188 bmap
= set_div_from_lower_bound(bmap
, i
, l
);
1196 __isl_give isl_basic_map
*isl_basic_map_remove_duplicate_constraints(
1197 __isl_take isl_basic_map
*bmap
, int *progress
, int detect_divs
)
1203 unsigned total
= isl_basic_map_total_dim(bmap
);
1207 if (!bmap
|| bmap
->n_ineq
<= 1)
1210 size
= round_up(4 * (bmap
->n_ineq
+1) / 3 - 1);
1213 bits
= ffs(size
) - 1;
1214 ctx
= isl_basic_map_get_ctx(bmap
);
1215 index
= isl_calloc_array(ctx
, isl_int
**, size
);
1219 index
[isl_seq_get_hash_bits(bmap
->ineq
[0]+1, total
, bits
)] = &bmap
->ineq
[0];
1220 for (k
= 1; k
< bmap
->n_ineq
; ++k
) {
1221 h
= hash_index(index
, size
, bits
, bmap
, k
);
1223 index
[h
] = &bmap
->ineq
[k
];
1228 l
= index
[h
] - &bmap
->ineq
[0];
1229 if (isl_int_lt(bmap
->ineq
[k
][0], bmap
->ineq
[l
][0]))
1230 swap_inequality(bmap
, k
, l
);
1231 isl_basic_map_drop_inequality(bmap
, k
);
1235 for (k
= 0; k
< bmap
->n_ineq
-1; ++k
) {
1236 isl_seq_neg(bmap
->ineq
[k
]+1, bmap
->ineq
[k
]+1, total
);
1237 h
= hash_index(index
, size
, bits
, bmap
, k
);
1238 isl_seq_neg(bmap
->ineq
[k
]+1, bmap
->ineq
[k
]+1, total
);
1241 l
= index
[h
] - &bmap
->ineq
[0];
1242 isl_int_add(sum
, bmap
->ineq
[k
][0], bmap
->ineq
[l
][0]);
1243 if (isl_int_is_pos(sum
)) {
1245 bmap
= check_for_div_constraints(bmap
, k
, l
,
1249 if (isl_int_is_zero(sum
)) {
1250 /* We need to break out of the loop after these
1251 * changes since the contents of the hash
1252 * will no longer be valid.
1253 * Plus, we probably we want to regauss first.
1257 isl_basic_map_drop_inequality(bmap
, l
);
1258 isl_basic_map_inequality_to_equality(bmap
, k
);
1260 bmap
= isl_basic_map_set_to_empty(bmap
);
1269 /* Detect all pairs of inequalities that form an equality.
1271 * isl_basic_map_remove_duplicate_constraints detects at most one such pair.
1272 * Call it repeatedly while it is making progress.
1274 __isl_give isl_basic_map
*isl_basic_map_detect_inequality_pairs(
1275 __isl_take isl_basic_map
*bmap
, int *progress
)
1281 bmap
= isl_basic_map_remove_duplicate_constraints(bmap
,
1283 if (progress
&& duplicate
)
1285 } while (duplicate
);
1290 /* Eliminate knowns divs from constraints where they appear with
1291 * a (positive or negative) unit coefficient.
1295 * floor(e/m) + f >= 0
1303 * -floor(e/m) + f >= 0
1307 * -e + m f + m - 1 >= 0
1309 * The first conversion is valid because floor(e/m) >= -f is equivalent
1310 * to e/m >= -f because -f is an integral expression.
1311 * The second conversion follows from the fact that
1313 * -floor(e/m) = ceil(-e/m) = floor((-e + m - 1)/m)
1316 * Note that one of the div constraints may have been eliminated
1317 * due to being redundant with respect to the constraint that is
1318 * being modified by this function. The modified constraint may
1319 * no longer imply this div constraint, so we add it back to make
1320 * sure we do not lose any information.
1322 * We skip integral divs, i.e., those with denominator 1, as we would
1323 * risk eliminating the div from the div constraints. We do not need
1324 * to handle those divs here anyway since the div constraints will turn
1325 * out to form an equality and this equality can then be use to eliminate
1326 * the div from all constraints.
1328 static __isl_give isl_basic_map
*eliminate_unit_divs(
1329 __isl_take isl_basic_map
*bmap
, int *progress
)
1338 ctx
= isl_basic_map_get_ctx(bmap
);
1339 total
= 1 + isl_space_dim(bmap
->dim
, isl_dim_all
);
1341 for (i
= 0; i
< bmap
->n_div
; ++i
) {
1342 if (isl_int_is_zero(bmap
->div
[i
][0]))
1344 if (isl_int_is_one(bmap
->div
[i
][0]))
1346 for (j
= 0; j
< bmap
->n_ineq
; ++j
) {
1349 if (!isl_int_is_one(bmap
->ineq
[j
][total
+ i
]) &&
1350 !isl_int_is_negone(bmap
->ineq
[j
][total
+ i
]))
1355 s
= isl_int_sgn(bmap
->ineq
[j
][total
+ i
]);
1356 isl_int_set_si(bmap
->ineq
[j
][total
+ i
], 0);
1358 isl_seq_combine(bmap
->ineq
[j
],
1359 ctx
->negone
, bmap
->div
[i
] + 1,
1360 bmap
->div
[i
][0], bmap
->ineq
[j
],
1361 total
+ bmap
->n_div
);
1363 isl_seq_combine(bmap
->ineq
[j
],
1364 ctx
->one
, bmap
->div
[i
] + 1,
1365 bmap
->div
[i
][0], bmap
->ineq
[j
],
1366 total
+ bmap
->n_div
);
1368 isl_int_add(bmap
->ineq
[j
][0],
1369 bmap
->ineq
[j
][0], bmap
->div
[i
][0]);
1370 isl_int_sub_ui(bmap
->ineq
[j
][0],
1371 bmap
->ineq
[j
][0], 1);
1374 bmap
= isl_basic_map_extend_constraints(bmap
, 0, 1);
1375 if (isl_basic_map_add_div_constraint(bmap
, i
, s
) < 0)
1376 return isl_basic_map_free(bmap
);
1383 struct isl_basic_map
*isl_basic_map_simplify(struct isl_basic_map
*bmap
)
1392 if (isl_basic_map_plain_is_empty(bmap
))
1394 bmap
= isl_basic_map_normalize_constraints(bmap
);
1395 bmap
= remove_independent_vars_from_divs(bmap
);
1396 bmap
= normalize_div_expressions(bmap
);
1397 bmap
= remove_duplicate_divs(bmap
, &progress
);
1398 bmap
= eliminate_unit_divs(bmap
, &progress
);
1399 bmap
= eliminate_divs_eq(bmap
, &progress
);
1400 bmap
= eliminate_divs_ineq(bmap
, &progress
);
1401 bmap
= isl_basic_map_gauss(bmap
, &progress
);
1402 /* requires equalities in normal form */
1403 bmap
= normalize_divs(bmap
, &progress
);
1404 bmap
= isl_basic_map_remove_duplicate_constraints(bmap
,
1406 if (bmap
&& progress
)
1407 ISL_F_CLR(bmap
, ISL_BASIC_MAP_REDUCED_COEFFICIENTS
);
1412 struct isl_basic_set
*isl_basic_set_simplify(struct isl_basic_set
*bset
)
1414 return (struct isl_basic_set
*)
1415 isl_basic_map_simplify((struct isl_basic_map
*)bset
);
1419 int isl_basic_map_is_div_constraint(__isl_keep isl_basic_map
*bmap
,
1420 isl_int
*constraint
, unsigned div
)
1427 pos
= 1 + isl_space_dim(bmap
->dim
, isl_dim_all
) + div
;
1429 if (isl_int_eq(constraint
[pos
], bmap
->div
[div
][0])) {
1431 isl_int_sub(bmap
->div
[div
][1],
1432 bmap
->div
[div
][1], bmap
->div
[div
][0]);
1433 isl_int_add_ui(bmap
->div
[div
][1], bmap
->div
[div
][1], 1);
1434 neg
= isl_seq_is_neg(constraint
, bmap
->div
[div
]+1, pos
);
1435 isl_int_sub_ui(bmap
->div
[div
][1], bmap
->div
[div
][1], 1);
1436 isl_int_add(bmap
->div
[div
][1],
1437 bmap
->div
[div
][1], bmap
->div
[div
][0]);
1440 if (isl_seq_first_non_zero(constraint
+pos
+1,
1441 bmap
->n_div
-div
-1) != -1)
1443 } else if (isl_int_abs_eq(constraint
[pos
], bmap
->div
[div
][0])) {
1444 if (!isl_seq_eq(constraint
, bmap
->div
[div
]+1, pos
))
1446 if (isl_seq_first_non_zero(constraint
+pos
+1,
1447 bmap
->n_div
-div
-1) != -1)
1455 int isl_basic_set_is_div_constraint(__isl_keep isl_basic_set
*bset
,
1456 isl_int
*constraint
, unsigned div
)
1458 return isl_basic_map_is_div_constraint(bset
, constraint
, div
);
1462 /* If the only constraints a div d=floor(f/m)
1463 * appears in are its two defining constraints
1466 * -(f - (m - 1)) + m d >= 0
1468 * then it can safely be removed.
1470 static int div_is_redundant(struct isl_basic_map
*bmap
, int div
)
1473 unsigned pos
= 1 + isl_space_dim(bmap
->dim
, isl_dim_all
) + div
;
1475 for (i
= 0; i
< bmap
->n_eq
; ++i
)
1476 if (!isl_int_is_zero(bmap
->eq
[i
][pos
]))
1479 for (i
= 0; i
< bmap
->n_ineq
; ++i
) {
1480 if (isl_int_is_zero(bmap
->ineq
[i
][pos
]))
1482 if (!isl_basic_map_is_div_constraint(bmap
, bmap
->ineq
[i
], div
))
1486 for (i
= 0; i
< bmap
->n_div
; ++i
) {
1487 if (isl_int_is_zero(bmap
->div
[i
][0]))
1489 if (!isl_int_is_zero(bmap
->div
[i
][1+pos
]))
1497 * Remove divs that don't occur in any of the constraints or other divs.
1498 * These can arise when dropping constraints from a basic map or
1499 * when the divs of a basic map have been temporarily aligned
1500 * with the divs of another basic map.
1502 static struct isl_basic_map
*remove_redundant_divs(struct isl_basic_map
*bmap
)
1509 for (i
= bmap
->n_div
-1; i
>= 0; --i
) {
1510 if (!div_is_redundant(bmap
, i
))
1512 bmap
= isl_basic_map_drop_div(bmap
, i
);
1517 struct isl_basic_map
*isl_basic_map_finalize(struct isl_basic_map
*bmap
)
1519 bmap
= remove_redundant_divs(bmap
);
1522 ISL_F_SET(bmap
, ISL_BASIC_SET_FINAL
);
1526 struct isl_basic_set
*isl_basic_set_finalize(struct isl_basic_set
*bset
)
1528 return (struct isl_basic_set
*)
1529 isl_basic_map_finalize((struct isl_basic_map
*)bset
);
1532 struct isl_set
*isl_set_finalize(struct isl_set
*set
)
1538 for (i
= 0; i
< set
->n
; ++i
) {
1539 set
->p
[i
] = isl_basic_set_finalize(set
->p
[i
]);
1549 struct isl_map
*isl_map_finalize(struct isl_map
*map
)
1555 for (i
= 0; i
< map
->n
; ++i
) {
1556 map
->p
[i
] = isl_basic_map_finalize(map
->p
[i
]);
1560 ISL_F_CLR(map
, ISL_MAP_NORMALIZED
);
1568 /* Remove definition of any div that is defined in terms of the given variable.
1569 * The div itself is not removed. Functions such as
1570 * eliminate_divs_ineq depend on the other divs remaining in place.
1572 static struct isl_basic_map
*remove_dependent_vars(struct isl_basic_map
*bmap
,
1580 for (i
= 0; i
< bmap
->n_div
; ++i
) {
1581 if (isl_int_is_zero(bmap
->div
[i
][0]))
1583 if (isl_int_is_zero(bmap
->div
[i
][1+1+pos
]))
1585 isl_int_set_si(bmap
->div
[i
][0], 0);
1590 /* Eliminate the specified variables from the constraints using
1591 * Fourier-Motzkin. The variables themselves are not removed.
1593 struct isl_basic_map
*isl_basic_map_eliminate_vars(
1594 struct isl_basic_map
*bmap
, unsigned pos
, unsigned n
)
1605 total
= isl_basic_map_total_dim(bmap
);
1607 bmap
= isl_basic_map_cow(bmap
);
1608 for (d
= pos
+ n
- 1; d
>= 0 && d
>= pos
; --d
)
1609 bmap
= remove_dependent_vars(bmap
, d
);
1613 for (d
= pos
+ n
- 1;
1614 d
>= 0 && d
>= total
- bmap
->n_div
&& d
>= pos
; --d
)
1615 isl_seq_clr(bmap
->div
[d
-(total
-bmap
->n_div
)], 2+total
);
1616 for (d
= pos
+ n
- 1; d
>= 0 && d
>= pos
; --d
) {
1617 int n_lower
, n_upper
;
1620 for (i
= 0; i
< bmap
->n_eq
; ++i
) {
1621 if (isl_int_is_zero(bmap
->eq
[i
][1+d
]))
1623 eliminate_var_using_equality(bmap
, d
, bmap
->eq
[i
], 0, NULL
);
1624 isl_basic_map_drop_equality(bmap
, i
);
1632 for (i
= 0; i
< bmap
->n_ineq
; ++i
) {
1633 if (isl_int_is_pos(bmap
->ineq
[i
][1+d
]))
1635 else if (isl_int_is_neg(bmap
->ineq
[i
][1+d
]))
1638 bmap
= isl_basic_map_extend_constraints(bmap
,
1639 0, n_lower
* n_upper
);
1642 for (i
= bmap
->n_ineq
- 1; i
>= 0; --i
) {
1644 if (isl_int_is_zero(bmap
->ineq
[i
][1+d
]))
1647 for (j
= 0; j
< i
; ++j
) {
1648 if (isl_int_is_zero(bmap
->ineq
[j
][1+d
]))
1651 if (isl_int_sgn(bmap
->ineq
[i
][1+d
]) ==
1652 isl_int_sgn(bmap
->ineq
[j
][1+d
]))
1654 k
= isl_basic_map_alloc_inequality(bmap
);
1657 isl_seq_cpy(bmap
->ineq
[k
], bmap
->ineq
[i
],
1659 isl_seq_elim(bmap
->ineq
[k
], bmap
->ineq
[j
],
1660 1+d
, 1+total
, NULL
);
1662 isl_basic_map_drop_inequality(bmap
, i
);
1665 if (n_lower
> 0 && n_upper
> 0) {
1666 bmap
= isl_basic_map_normalize_constraints(bmap
);
1667 bmap
= isl_basic_map_remove_duplicate_constraints(bmap
,
1669 bmap
= isl_basic_map_gauss(bmap
, NULL
);
1670 bmap
= isl_basic_map_remove_redundancies(bmap
);
1674 if (ISL_F_ISSET(bmap
, ISL_BASIC_MAP_EMPTY
))
1678 ISL_F_CLR(bmap
, ISL_BASIC_MAP_NORMALIZED
);
1680 bmap
= isl_basic_map_gauss(bmap
, NULL
);
1683 isl_basic_map_free(bmap
);
1687 struct isl_basic_set
*isl_basic_set_eliminate_vars(
1688 struct isl_basic_set
*bset
, unsigned pos
, unsigned n
)
1690 return (struct isl_basic_set
*)isl_basic_map_eliminate_vars(
1691 (struct isl_basic_map
*)bset
, pos
, n
);
1694 /* Eliminate the specified n dimensions starting at first from the
1695 * constraints, without removing the dimensions from the space.
1696 * If the set is rational, the dimensions are eliminated using Fourier-Motzkin.
1697 * Otherwise, they are projected out and the original space is restored.
1699 __isl_give isl_basic_map
*isl_basic_map_eliminate(
1700 __isl_take isl_basic_map
*bmap
,
1701 enum isl_dim_type type
, unsigned first
, unsigned n
)
1710 if (first
+ n
> isl_basic_map_dim(bmap
, type
) || first
+ n
< first
)
1711 isl_die(bmap
->ctx
, isl_error_invalid
,
1712 "index out of bounds", goto error
);
1714 if (ISL_F_ISSET(bmap
, ISL_BASIC_MAP_RATIONAL
)) {
1715 first
+= isl_basic_map_offset(bmap
, type
) - 1;
1716 bmap
= isl_basic_map_eliminate_vars(bmap
, first
, n
);
1717 return isl_basic_map_finalize(bmap
);
1720 space
= isl_basic_map_get_space(bmap
);
1721 bmap
= isl_basic_map_project_out(bmap
, type
, first
, n
);
1722 bmap
= isl_basic_map_insert_dims(bmap
, type
, first
, n
);
1723 bmap
= isl_basic_map_reset_space(bmap
, space
);
1726 isl_basic_map_free(bmap
);
1730 __isl_give isl_basic_set
*isl_basic_set_eliminate(
1731 __isl_take isl_basic_set
*bset
,
1732 enum isl_dim_type type
, unsigned first
, unsigned n
)
1734 return isl_basic_map_eliminate(bset
, type
, first
, n
);
1737 /* Don't assume equalities are in order, because align_divs
1738 * may have changed the order of the divs.
1740 static void compute_elimination_index(struct isl_basic_map
*bmap
, int *elim
)
1745 total
= isl_space_dim(bmap
->dim
, isl_dim_all
);
1746 for (d
= 0; d
< total
; ++d
)
1748 for (i
= 0; i
< bmap
->n_eq
; ++i
) {
1749 for (d
= total
- 1; d
>= 0; --d
) {
1750 if (isl_int_is_zero(bmap
->eq
[i
][1+d
]))
1758 static void set_compute_elimination_index(struct isl_basic_set
*bset
, int *elim
)
1760 compute_elimination_index((struct isl_basic_map
*)bset
, elim
);
1763 static int reduced_using_equalities(isl_int
*dst
, isl_int
*src
,
1764 struct isl_basic_map
*bmap
, int *elim
)
1770 total
= isl_space_dim(bmap
->dim
, isl_dim_all
);
1771 for (d
= total
- 1; d
>= 0; --d
) {
1772 if (isl_int_is_zero(src
[1+d
]))
1777 isl_seq_cpy(dst
, src
, 1 + total
);
1780 isl_seq_elim(dst
, bmap
->eq
[elim
[d
]], 1 + d
, 1 + total
, NULL
);
1785 static int set_reduced_using_equalities(isl_int
*dst
, isl_int
*src
,
1786 struct isl_basic_set
*bset
, int *elim
)
1788 return reduced_using_equalities(dst
, src
,
1789 (struct isl_basic_map
*)bset
, elim
);
1792 static struct isl_basic_set
*isl_basic_set_reduce_using_equalities(
1793 struct isl_basic_set
*bset
, struct isl_basic_set
*context
)
1798 if (!bset
|| !context
)
1801 if (context
->n_eq
== 0) {
1802 isl_basic_set_free(context
);
1806 bset
= isl_basic_set_cow(bset
);
1810 elim
= isl_alloc_array(bset
->ctx
, int, isl_basic_set_n_dim(bset
));
1813 set_compute_elimination_index(context
, elim
);
1814 for (i
= 0; i
< bset
->n_eq
; ++i
)
1815 set_reduced_using_equalities(bset
->eq
[i
], bset
->eq
[i
],
1817 for (i
= 0; i
< bset
->n_ineq
; ++i
)
1818 set_reduced_using_equalities(bset
->ineq
[i
], bset
->ineq
[i
],
1820 isl_basic_set_free(context
);
1822 bset
= isl_basic_set_simplify(bset
);
1823 bset
= isl_basic_set_finalize(bset
);
1826 isl_basic_set_free(bset
);
1827 isl_basic_set_free(context
);
1831 static struct isl_basic_set
*remove_shifted_constraints(
1832 struct isl_basic_set
*bset
, struct isl_basic_set
*context
)
1840 if (!bset
|| !context
)
1843 size
= round_up(4 * (context
->n_ineq
+1) / 3 - 1);
1846 bits
= ffs(size
) - 1;
1847 ctx
= isl_basic_set_get_ctx(bset
);
1848 index
= isl_calloc_array(ctx
, isl_int
**, size
);
1852 for (k
= 0; k
< context
->n_ineq
; ++k
) {
1853 h
= set_hash_index(index
, size
, bits
, context
, k
);
1854 index
[h
] = &context
->ineq
[k
];
1856 for (k
= 0; k
< bset
->n_ineq
; ++k
) {
1857 h
= set_hash_index(index
, size
, bits
, bset
, k
);
1860 l
= index
[h
] - &context
->ineq
[0];
1861 if (isl_int_lt(bset
->ineq
[k
][0], context
->ineq
[l
][0]))
1863 bset
= isl_basic_set_cow(bset
);
1866 isl_basic_set_drop_inequality(bset
, k
);
1876 /* Remove constraints from "bmap" that are identical to constraints
1877 * in "context" or that are more relaxed (greater constant term).
1879 * We perform the test for shifted copies on the pure constraints
1880 * in remove_shifted_constraints.
1882 static __isl_give isl_basic_map
*isl_basic_map_remove_shifted_constraints(
1883 __isl_take isl_basic_map
*bmap
, __isl_take isl_basic_map
*context
)
1885 isl_basic_set
*bset
, *bset_context
;
1887 if (!bmap
|| !context
)
1890 if (bmap
->n_ineq
== 0 || context
->n_ineq
== 0) {
1891 isl_basic_map_free(context
);
1895 context
= isl_basic_map_align_divs(context
, bmap
);
1896 bmap
= isl_basic_map_align_divs(bmap
, context
);
1898 bset
= isl_basic_map_underlying_set(isl_basic_map_copy(bmap
));
1899 bset_context
= isl_basic_map_underlying_set(context
);
1900 bset
= remove_shifted_constraints(bset
, bset_context
);
1901 isl_basic_set_free(bset_context
);
1903 bmap
= isl_basic_map_overlying_set(bset
, bmap
);
1907 isl_basic_map_free(bmap
);
1908 isl_basic_map_free(context
);
1912 /* Does the (linear part of a) constraint "c" involve any of the "len"
1913 * "relevant" dimensions?
1915 static int is_related(isl_int
*c
, int len
, int *relevant
)
1919 for (i
= 0; i
< len
; ++i
) {
1922 if (!isl_int_is_zero(c
[i
]))
1929 /* Drop constraints from "bset" that do not involve any of
1930 * the dimensions marked "relevant".
1932 static __isl_give isl_basic_set
*drop_unrelated_constraints(
1933 __isl_take isl_basic_set
*bset
, int *relevant
)
1937 dim
= isl_basic_set_dim(bset
, isl_dim_set
);
1938 for (i
= 0; i
< dim
; ++i
)
1944 for (i
= bset
->n_eq
- 1; i
>= 0; --i
)
1945 if (!is_related(bset
->eq
[i
] + 1, dim
, relevant
))
1946 isl_basic_set_drop_equality(bset
, i
);
1948 for (i
= bset
->n_ineq
- 1; i
>= 0; --i
)
1949 if (!is_related(bset
->ineq
[i
] + 1, dim
, relevant
))
1950 isl_basic_set_drop_inequality(bset
, i
);
1955 /* Update the groups in "group" based on the (linear part of a) constraint "c".
1957 * In particular, for any variable involved in the constraint,
1958 * find the actual group id from before and replace the group
1959 * of the corresponding variable by the minimal group of all
1960 * the variables involved in the constraint considered so far
1961 * (if this minimum is smaller) or replace the minimum by this group
1962 * (if the minimum is larger).
1964 * At the end, all the variables in "c" will (indirectly) point
1965 * to the minimal of the groups that they referred to originally.
1967 static void update_groups(int dim
, int *group
, isl_int
*c
)
1972 for (j
= 0; j
< dim
; ++j
) {
1973 if (isl_int_is_zero(c
[j
]))
1975 while (group
[j
] >= 0 && group
[group
[j
]] != group
[j
])
1976 group
[j
] = group
[group
[j
]];
1977 if (group
[j
] == min
)
1979 if (group
[j
] < min
) {
1980 if (min
>= 0 && min
< dim
)
1981 group
[min
] = group
[j
];
1984 group
[group
[j
]] = min
;
1988 /* Drop constraints from "context" that are irrelevant for computing
1989 * the gist of "bset".
1991 * In particular, drop constraints in variables that are not related
1992 * to any of the variables involved in the constraints of "bset"
1993 * in the sense that there is no sequence of constraints that connects them.
1995 * We construct groups of variables that collect variables that
1996 * (indirectly) appear in some common constraint of "context".
1997 * Each group is identified by the first variable in the group,
1998 * except for the special group of variables that appear in "bset"
1999 * (or are related to those variables), which is identified by -1.
2000 * If group[i] is equal to i (or -1), then the group of i is i (or -1),
2001 * otherwise the group of i is the group of group[i].
2003 * We first initialize the -1 group with the variables that appear in "bset".
2004 * Then we initialize groups for the remaining variables.
2005 * Then we iterate over the constraints of "context" and update the
2006 * group of the variables in the constraint by the smallest group.
2007 * Finally, we resolve indirect references to groups by running over
2010 * After computing the groups, we drop constraints that do not involve
2011 * any variables in the -1 group.
2013 static __isl_give isl_basic_set
*drop_irrelevant_constraints(
2014 __isl_take isl_basic_set
*context
, __isl_keep isl_basic_set
*bset
)
2022 if (!context
|| !bset
)
2023 return isl_basic_set_free(context
);
2025 dim
= isl_basic_set_dim(bset
, isl_dim_set
);
2026 ctx
= isl_basic_set_get_ctx(bset
);
2027 group
= isl_calloc_array(ctx
, int, dim
);
2032 for (i
= 0; i
< dim
; ++i
) {
2033 for (j
= 0; j
< bset
->n_eq
; ++j
)
2034 if (!isl_int_is_zero(bset
->eq
[j
][1 + i
]))
2036 if (j
< bset
->n_eq
) {
2040 for (j
= 0; j
< bset
->n_ineq
; ++j
)
2041 if (!isl_int_is_zero(bset
->ineq
[j
][1 + i
]))
2043 if (j
< bset
->n_ineq
)
2048 for (i
= 0; i
< dim
; ++i
)
2050 last
= group
[i
] = i
;
2056 for (i
= 0; i
< context
->n_eq
; ++i
)
2057 update_groups(dim
, group
, context
->eq
[i
] + 1);
2058 for (i
= 0; i
< context
->n_ineq
; ++i
)
2059 update_groups(dim
, group
, context
->ineq
[i
] + 1);
2061 for (i
= 0; i
< dim
; ++i
)
2063 group
[i
] = group
[group
[i
]];
2065 for (i
= 0; i
< dim
; ++i
)
2066 group
[i
] = group
[i
] == -1;
2068 context
= drop_unrelated_constraints(context
, group
);
2074 return isl_basic_set_free(context
);
2077 /* Remove all information from bset that is redundant in the context
2078 * of context. Both bset and context are assumed to be full-dimensional.
2080 * We first remove the inequalities from "bset"
2081 * that are obviously redundant with respect to some inequality in "context".
2082 * Then we remove those constraints from "context" that have become
2083 * irrelevant for computing the gist of "bset".
2084 * Note that this removal of constraints cannot be replaced by
2085 * a factorization because factors in "bset" may still be connected
2086 * to each other through constraints in "context".
2088 * If there are any inequalities left, we construct a tableau for
2089 * the context and then add the inequalities of "bset".
2090 * Before adding these inequalities, we freeze all constraints such that
2091 * they won't be considered redundant in terms of the constraints of "bset".
2092 * Then we detect all redundant constraints (among the
2093 * constraints that weren't frozen), first by checking for redundancy in the
2094 * the tableau and then by checking if replacing a constraint by its negation
2095 * would lead to an empty set. This last step is fairly expensive
2096 * and could be optimized by more reuse of the tableau.
2097 * Finally, we update bset according to the results.
2099 static __isl_give isl_basic_set
*uset_gist_full(__isl_take isl_basic_set
*bset
,
2100 __isl_take isl_basic_set
*context
)
2103 isl_basic_set
*combined
= NULL
;
2104 struct isl_tab
*tab
= NULL
;
2105 unsigned context_ineq
;
2108 if (!bset
|| !context
)
2111 if (isl_basic_set_is_universe(bset
)) {
2112 isl_basic_set_free(context
);
2116 if (isl_basic_set_is_universe(context
)) {
2117 isl_basic_set_free(context
);
2121 bset
= remove_shifted_constraints(bset
, context
);
2124 if (bset
->n_ineq
== 0)
2127 context
= drop_irrelevant_constraints(context
, bset
);
2130 if (isl_basic_set_is_universe(context
)) {
2131 isl_basic_set_free(context
);
2135 context_ineq
= context
->n_ineq
;
2136 combined
= isl_basic_set_cow(isl_basic_set_copy(context
));
2137 combined
= isl_basic_set_extend_constraints(combined
, 0, bset
->n_ineq
);
2138 tab
= isl_tab_from_basic_set(combined
, 0);
2139 for (i
= 0; i
< context_ineq
; ++i
)
2140 if (isl_tab_freeze_constraint(tab
, i
) < 0)
2142 if (isl_tab_extend_cons(tab
, bset
->n_ineq
) < 0)
2144 for (i
= 0; i
< bset
->n_ineq
; ++i
)
2145 if (isl_tab_add_ineq(tab
, bset
->ineq
[i
]) < 0)
2147 bset
= isl_basic_set_add_constraints(combined
, bset
, 0);
2151 if (isl_tab_detect_redundant(tab
) < 0)
2153 total
= isl_basic_set_total_dim(bset
);
2154 for (i
= context_ineq
; i
< bset
->n_ineq
; ++i
) {
2156 if (tab
->con
[i
].is_redundant
)
2158 tab
->con
[i
].is_redundant
= 1;
2159 combined
= isl_basic_set_dup(bset
);
2160 combined
= isl_basic_set_update_from_tab(combined
, tab
);
2161 combined
= isl_basic_set_extend_constraints(combined
, 0, 1);
2162 k
= isl_basic_set_alloc_inequality(combined
);
2165 isl_seq_neg(combined
->ineq
[k
], bset
->ineq
[i
], 1 + total
);
2166 isl_int_sub_ui(combined
->ineq
[k
][0], combined
->ineq
[k
][0], 1);
2167 is_empty
= isl_basic_set_is_empty(combined
);
2170 isl_basic_set_free(combined
);
2173 tab
->con
[i
].is_redundant
= 0;
2175 for (i
= 0; i
< context_ineq
; ++i
)
2176 tab
->con
[i
].is_redundant
= 1;
2177 bset
= isl_basic_set_update_from_tab(bset
, tab
);
2179 ISL_F_SET(bset
, ISL_BASIC_SET_NO_IMPLICIT
);
2180 ISL_F_SET(bset
, ISL_BASIC_SET_NO_REDUNDANT
);
2185 bset
= isl_basic_set_simplify(bset
);
2186 bset
= isl_basic_set_finalize(bset
);
2187 isl_basic_set_free(context
);
2191 isl_basic_set_free(combined
);
2192 isl_basic_set_free(context
);
2193 isl_basic_set_free(bset
);
2197 /* Remove all information from bset that is redundant in the context
2198 * of context. In particular, equalities that are linear combinations
2199 * of those in context are removed. Then the inequalities that are
2200 * redundant in the context of the equalities and inequalities of
2201 * context are removed.
2203 * First of all, we drop those constraints from "context"
2204 * that are irrelevant for computing the gist of "bset".
2205 * Alternatively, we could factorize the intersection of "context" and "bset".
2207 * We first compute the integer affine hull of the intersection,
2208 * compute the gist inside this affine hull and then add back
2209 * those equalities that are not implied by the context.
2211 * If two constraints are mutually redundant, then uset_gist_full
2212 * will remove the second of those constraints. We therefore first
2213 * sort the constraints so that constraints not involving existentially
2214 * quantified variables are given precedence over those that do.
2215 * We have to perform this sorting before the variable compression,
2216 * because that may effect the order of the variables.
2218 static __isl_give isl_basic_set
*uset_gist(__isl_take isl_basic_set
*bset
,
2219 __isl_take isl_basic_set
*context
)
2224 isl_basic_set
*aff_context
;
2227 if (!bset
|| !context
)
2230 context
= drop_irrelevant_constraints(context
, bset
);
2232 aff
= isl_basic_set_copy(bset
);
2233 aff
= isl_basic_set_intersect(aff
, isl_basic_set_copy(context
));
2234 aff
= isl_basic_set_affine_hull(aff
);
2237 if (isl_basic_set_plain_is_empty(aff
)) {
2238 isl_basic_set_free(bset
);
2239 isl_basic_set_free(context
);
2242 bset
= isl_basic_set_sort_constraints(bset
);
2243 if (aff
->n_eq
== 0) {
2244 isl_basic_set_free(aff
);
2245 return uset_gist_full(bset
, context
);
2247 total
= isl_basic_set_total_dim(bset
);
2248 eq
= isl_mat_sub_alloc6(bset
->ctx
, aff
->eq
, 0, aff
->n_eq
, 0, 1 + total
);
2249 eq
= isl_mat_cow(eq
);
2250 T
= isl_mat_variable_compression(eq
, &T2
);
2251 if (T
&& T
->n_col
== 0) {
2254 isl_basic_set_free(context
);
2255 isl_basic_set_free(aff
);
2256 return isl_basic_set_set_to_empty(bset
);
2259 aff_context
= isl_basic_set_affine_hull(isl_basic_set_copy(context
));
2261 bset
= isl_basic_set_preimage(bset
, isl_mat_copy(T
));
2262 context
= isl_basic_set_preimage(context
, T
);
2264 bset
= uset_gist_full(bset
, context
);
2265 bset
= isl_basic_set_preimage(bset
, T2
);
2266 bset
= isl_basic_set_intersect(bset
, aff
);
2267 bset
= isl_basic_set_reduce_using_equalities(bset
, aff_context
);
2270 ISL_F_SET(bset
, ISL_BASIC_SET_NO_IMPLICIT
);
2271 ISL_F_SET(bset
, ISL_BASIC_SET_NO_REDUNDANT
);
2276 isl_basic_set_free(bset
);
2277 isl_basic_set_free(context
);
2281 /* Normalize the divs in "bmap" in the context of the equalities in "context".
2282 * We simply add the equalities in context to bmap and then do a regular
2283 * div normalizations. Better results can be obtained by normalizing
2284 * only the divs in bmap than do not also appear in context.
2285 * We need to be careful to reduce the divs using the equalities
2286 * so that later calls to isl_basic_map_overlying_set wouldn't introduce
2287 * spurious constraints.
2289 static struct isl_basic_map
*normalize_divs_in_context(
2290 struct isl_basic_map
*bmap
, struct isl_basic_map
*context
)
2293 unsigned total_context
;
2296 div_eq
= n_pure_div_eq(bmap
);
2300 bmap
= isl_basic_map_cow(bmap
);
2301 if (context
->n_div
> 0)
2302 bmap
= isl_basic_map_align_divs(bmap
, context
);
2304 total_context
= isl_basic_map_total_dim(context
);
2305 bmap
= isl_basic_map_extend_constraints(bmap
, context
->n_eq
, 0);
2306 for (i
= 0; i
< context
->n_eq
; ++i
) {
2308 k
= isl_basic_map_alloc_equality(bmap
);
2310 return isl_basic_map_free(bmap
);
2311 isl_seq_cpy(bmap
->eq
[k
], context
->eq
[i
], 1 + total_context
);
2312 isl_seq_clr(bmap
->eq
[k
] + 1 + total_context
,
2313 isl_basic_map_total_dim(bmap
) - total_context
);
2315 bmap
= isl_basic_map_gauss(bmap
, NULL
);
2316 bmap
= normalize_divs(bmap
, NULL
);
2317 bmap
= isl_basic_map_gauss(bmap
, NULL
);
2321 /* Return a basic map that has the same intersection with "context" as "bmap"
2322 * and that is as "simple" as possible.
2324 * The core computation is performed on the pure constraints.
2325 * When we add back the meaning of the integer divisions, we need
2326 * to (re)introduce the div constraints. If we happen to have
2327 * discovered that some of these integer divisions are equal to
2328 * some affine combination of other variables, then these div
2329 * constraints may end up getting simplified in terms of the equalities,
2330 * resulting in extra inequalities on the other variables that
2331 * may have been removed already or that may not even have been
2332 * part of the input. We try and remove those constraints of
2333 * this form that are most obviously redundant with respect to
2334 * the context. We also remove those div constraints that are
2335 * redundant with respect to the other constraints in the result.
2337 struct isl_basic_map
*isl_basic_map_gist(struct isl_basic_map
*bmap
,
2338 struct isl_basic_map
*context
)
2340 isl_basic_set
*bset
, *eq
;
2341 isl_basic_map
*eq_bmap
;
2342 unsigned n_div
, n_eq
, n_ineq
;
2344 if (!bmap
|| !context
)
2347 if (isl_basic_map_is_universe(bmap
)) {
2348 isl_basic_map_free(context
);
2351 if (isl_basic_map_plain_is_empty(context
)) {
2352 isl_space
*space
= isl_basic_map_get_space(bmap
);
2353 isl_basic_map_free(bmap
);
2354 isl_basic_map_free(context
);
2355 return isl_basic_map_universe(space
);
2357 if (isl_basic_map_plain_is_empty(bmap
)) {
2358 isl_basic_map_free(context
);
2362 bmap
= isl_basic_map_remove_redundancies(bmap
);
2363 context
= isl_basic_map_remove_redundancies(context
);
2368 bmap
= normalize_divs_in_context(bmap
, context
);
2370 context
= isl_basic_map_align_divs(context
, bmap
);
2371 bmap
= isl_basic_map_align_divs(bmap
, context
);
2372 n_div
= isl_basic_map_dim(bmap
, isl_dim_div
);
2374 bset
= uset_gist(isl_basic_map_underlying_set(isl_basic_map_copy(bmap
)),
2375 isl_basic_map_underlying_set(isl_basic_map_copy(context
)));
2377 if (!bset
|| bset
->n_eq
== 0 || n_div
== 0 ||
2378 isl_basic_set_plain_is_empty(bset
)) {
2379 isl_basic_map_free(context
);
2380 return isl_basic_map_overlying_set(bset
, bmap
);
2384 n_ineq
= bset
->n_ineq
;
2385 eq
= isl_basic_set_copy(bset
);
2386 eq
= isl_basic_set_cow(eq
);
2387 if (isl_basic_set_free_inequality(eq
, n_ineq
) < 0)
2388 eq
= isl_basic_set_free(eq
);
2389 if (isl_basic_set_free_equality(bset
, n_eq
) < 0)
2390 bset
= isl_basic_set_free(bset
);
2392 eq_bmap
= isl_basic_map_overlying_set(eq
, isl_basic_map_copy(bmap
));
2393 eq_bmap
= isl_basic_map_remove_shifted_constraints(eq_bmap
, context
);
2394 bmap
= isl_basic_map_overlying_set(bset
, bmap
);
2395 bmap
= isl_basic_map_intersect(bmap
, eq_bmap
);
2396 bmap
= isl_basic_map_remove_redundancies(bmap
);
2400 isl_basic_map_free(bmap
);
2401 isl_basic_map_free(context
);
2406 * Assumes context has no implicit divs.
2408 __isl_give isl_map
*isl_map_gist_basic_map(__isl_take isl_map
*map
,
2409 __isl_take isl_basic_map
*context
)
2413 if (!map
|| !context
)
2416 if (isl_basic_map_plain_is_empty(context
)) {
2417 isl_space
*space
= isl_map_get_space(map
);
2419 isl_basic_map_free(context
);
2420 return isl_map_universe(space
);
2423 context
= isl_basic_map_remove_redundancies(context
);
2424 map
= isl_map_cow(map
);
2425 if (!map
|| !context
)
2427 isl_assert(map
->ctx
, isl_space_is_equal(map
->dim
, context
->dim
), goto error
);
2428 map
= isl_map_compute_divs(map
);
2431 for (i
= map
->n
- 1; i
>= 0; --i
) {
2432 map
->p
[i
] = isl_basic_map_gist(map
->p
[i
],
2433 isl_basic_map_copy(context
));
2436 if (isl_basic_map_plain_is_empty(map
->p
[i
])) {
2437 isl_basic_map_free(map
->p
[i
]);
2438 if (i
!= map
->n
- 1)
2439 map
->p
[i
] = map
->p
[map
->n
- 1];
2443 isl_basic_map_free(context
);
2444 ISL_F_CLR(map
, ISL_MAP_NORMALIZED
);
2448 isl_basic_map_free(context
);
2452 /* Return a map that has the same intersection with "context" as "map"
2453 * and that is as "simple" as possible.
2455 * If "map" is already the universe, then we cannot make it any simpler.
2456 * Similarly, if "context" is the universe, then we cannot exploit it
2458 * If "map" and "context" are identical to each other, then we can
2459 * return the corresponding universe.
2461 * If none of these cases apply, we have to work a bit harder.
2462 * During this computation, we make use of a single disjunct context,
2463 * so if the original context consists of more than one disjunct
2464 * then we need to approximate the context by a single disjunct set.
2465 * Simply taking the simple hull may drop constraints that are
2466 * only implicitly available in each disjunct. We therefore also
2467 * look for constraints among those defining "map" that are valid
2468 * for the context. These can then be used to simplify away
2469 * the corresponding constraints in "map".
2471 static __isl_give isl_map
*map_gist(__isl_take isl_map
*map
,
2472 __isl_take isl_map
*context
)
2476 isl_basic_map
*hull
;
2478 is_universe
= isl_map_plain_is_universe(map
);
2479 if (is_universe
>= 0 && !is_universe
)
2480 is_universe
= isl_map_plain_is_universe(context
);
2481 if (is_universe
< 0)
2484 isl_map_free(context
);
2488 equal
= isl_map_plain_is_equal(map
, context
);
2492 isl_map
*res
= isl_map_universe(isl_map_get_space(map
));
2494 isl_map_free(context
);
2498 context
= isl_map_compute_divs(context
);
2501 if (isl_map_n_basic_map(context
) == 1) {
2502 hull
= isl_map_simple_hull(context
);
2507 ctx
= isl_map_get_ctx(map
);
2508 list
= isl_map_list_alloc(ctx
, 2);
2509 list
= isl_map_list_add(list
, isl_map_copy(context
));
2510 list
= isl_map_list_add(list
, isl_map_copy(map
));
2511 hull
= isl_map_unshifted_simple_hull_from_map_list(context
,
2514 return isl_map_gist_basic_map(map
, hull
);
2517 isl_map_free(context
);
2521 __isl_give isl_map
*isl_map_gist(__isl_take isl_map
*map
,
2522 __isl_take isl_map
*context
)
2524 return isl_map_align_params_map_map_and(map
, context
, &map_gist
);
2527 struct isl_basic_set
*isl_basic_set_gist(struct isl_basic_set
*bset
,
2528 struct isl_basic_set
*context
)
2530 return (struct isl_basic_set
*)isl_basic_map_gist(
2531 (struct isl_basic_map
*)bset
, (struct isl_basic_map
*)context
);
2534 __isl_give isl_set
*isl_set_gist_basic_set(__isl_take isl_set
*set
,
2535 __isl_take isl_basic_set
*context
)
2537 return (struct isl_set
*)isl_map_gist_basic_map((struct isl_map
*)set
,
2538 (struct isl_basic_map
*)context
);
2541 __isl_give isl_set
*isl_set_gist_params_basic_set(__isl_take isl_set
*set
,
2542 __isl_take isl_basic_set
*context
)
2544 isl_space
*space
= isl_set_get_space(set
);
2545 isl_basic_set
*dom_context
= isl_basic_set_universe(space
);
2546 dom_context
= isl_basic_set_intersect_params(dom_context
, context
);
2547 return isl_set_gist_basic_set(set
, dom_context
);
2550 __isl_give isl_set
*isl_set_gist(__isl_take isl_set
*set
,
2551 __isl_take isl_set
*context
)
2553 return (struct isl_set
*)isl_map_gist((struct isl_map
*)set
,
2554 (struct isl_map
*)context
);
2557 /* Compute the gist of "bmap" with respect to the constraints "context"
2560 __isl_give isl_basic_map
*isl_basic_map_gist_domain(
2561 __isl_take isl_basic_map
*bmap
, __isl_take isl_basic_set
*context
)
2563 isl_space
*space
= isl_basic_map_get_space(bmap
);
2564 isl_basic_map
*bmap_context
= isl_basic_map_universe(space
);
2566 bmap_context
= isl_basic_map_intersect_domain(bmap_context
, context
);
2567 return isl_basic_map_gist(bmap
, bmap_context
);
2570 __isl_give isl_map
*isl_map_gist_domain(__isl_take isl_map
*map
,
2571 __isl_take isl_set
*context
)
2573 isl_map
*map_context
= isl_map_universe(isl_map_get_space(map
));
2574 map_context
= isl_map_intersect_domain(map_context
, context
);
2575 return isl_map_gist(map
, map_context
);
2578 __isl_give isl_map
*isl_map_gist_range(__isl_take isl_map
*map
,
2579 __isl_take isl_set
*context
)
2581 isl_map
*map_context
= isl_map_universe(isl_map_get_space(map
));
2582 map_context
= isl_map_intersect_range(map_context
, context
);
2583 return isl_map_gist(map
, map_context
);
2586 __isl_give isl_map
*isl_map_gist_params(__isl_take isl_map
*map
,
2587 __isl_take isl_set
*context
)
2589 isl_map
*map_context
= isl_map_universe(isl_map_get_space(map
));
2590 map_context
= isl_map_intersect_params(map_context
, context
);
2591 return isl_map_gist(map
, map_context
);
2594 __isl_give isl_set
*isl_set_gist_params(__isl_take isl_set
*set
,
2595 __isl_take isl_set
*context
)
2597 return isl_map_gist_params(set
, context
);
2600 /* Quick check to see if two basic maps are disjoint.
2601 * In particular, we reduce the equalities and inequalities of
2602 * one basic map in the context of the equalities of the other
2603 * basic map and check if we get a contradiction.
2605 isl_bool
isl_basic_map_plain_is_disjoint(__isl_keep isl_basic_map
*bmap1
,
2606 __isl_keep isl_basic_map
*bmap2
)
2608 struct isl_vec
*v
= NULL
;
2613 if (!bmap1
|| !bmap2
)
2614 return isl_bool_error
;
2615 isl_assert(bmap1
->ctx
, isl_space_is_equal(bmap1
->dim
, bmap2
->dim
),
2616 return isl_bool_error
);
2617 if (bmap1
->n_div
|| bmap2
->n_div
)
2618 return isl_bool_false
;
2619 if (!bmap1
->n_eq
&& !bmap2
->n_eq
)
2620 return isl_bool_false
;
2622 total
= isl_space_dim(bmap1
->dim
, isl_dim_all
);
2624 return isl_bool_false
;
2625 v
= isl_vec_alloc(bmap1
->ctx
, 1 + total
);
2628 elim
= isl_alloc_array(bmap1
->ctx
, int, total
);
2631 compute_elimination_index(bmap1
, elim
);
2632 for (i
= 0; i
< bmap2
->n_eq
; ++i
) {
2634 reduced
= reduced_using_equalities(v
->block
.data
, bmap2
->eq
[i
],
2636 if (reduced
&& !isl_int_is_zero(v
->block
.data
[0]) &&
2637 isl_seq_first_non_zero(v
->block
.data
+ 1, total
) == -1)
2640 for (i
= 0; i
< bmap2
->n_ineq
; ++i
) {
2642 reduced
= reduced_using_equalities(v
->block
.data
,
2643 bmap2
->ineq
[i
], bmap1
, elim
);
2644 if (reduced
&& isl_int_is_neg(v
->block
.data
[0]) &&
2645 isl_seq_first_non_zero(v
->block
.data
+ 1, total
) == -1)
2648 compute_elimination_index(bmap2
, elim
);
2649 for (i
= 0; i
< bmap1
->n_ineq
; ++i
) {
2651 reduced
= reduced_using_equalities(v
->block
.data
,
2652 bmap1
->ineq
[i
], bmap2
, elim
);
2653 if (reduced
&& isl_int_is_neg(v
->block
.data
[0]) &&
2654 isl_seq_first_non_zero(v
->block
.data
+ 1, total
) == -1)
2659 return isl_bool_false
;
2663 return isl_bool_true
;
2667 return isl_bool_error
;
2670 int isl_basic_set_plain_is_disjoint(__isl_keep isl_basic_set
*bset1
,
2671 __isl_keep isl_basic_set
*bset2
)
2673 return isl_basic_map_plain_is_disjoint((struct isl_basic_map
*)bset1
,
2674 (struct isl_basic_map
*)bset2
);
2677 /* Are "map1" and "map2" obviously disjoint?
2679 * If one of them is empty or if they live in different spaces (ignoring
2680 * parameters), then they are clearly disjoint.
2682 * If they have different parameters, then we skip any further tests.
2684 * If they are obviously equal, but not obviously empty, then we will
2685 * not be able to detect if they are disjoint.
2687 * Otherwise we check if each basic map in "map1" is obviously disjoint
2688 * from each basic map in "map2".
2690 isl_bool
isl_map_plain_is_disjoint(__isl_keep isl_map
*map1
,
2691 __isl_keep isl_map
*map2
)
2699 return isl_bool_error
;
2701 disjoint
= isl_map_plain_is_empty(map1
);
2702 if (disjoint
< 0 || disjoint
)
2705 disjoint
= isl_map_plain_is_empty(map2
);
2706 if (disjoint
< 0 || disjoint
)
2709 match
= isl_space_tuple_is_equal(map1
->dim
, isl_dim_in
,
2710 map2
->dim
, isl_dim_in
);
2711 if (match
< 0 || !match
)
2712 return match
< 0 ? isl_bool_error
: isl_bool_true
;
2714 match
= isl_space_tuple_is_equal(map1
->dim
, isl_dim_out
,
2715 map2
->dim
, isl_dim_out
);
2716 if (match
< 0 || !match
)
2717 return match
< 0 ? isl_bool_error
: isl_bool_true
;
2719 match
= isl_space_match(map1
->dim
, isl_dim_param
,
2720 map2
->dim
, isl_dim_param
);
2721 if (match
< 0 || !match
)
2722 return match
< 0 ? isl_bool_error
: isl_bool_false
;
2724 intersect
= isl_map_plain_is_equal(map1
, map2
);
2725 if (intersect
< 0 || intersect
)
2726 return intersect
< 0 ? isl_bool_error
: isl_bool_false
;
2728 for (i
= 0; i
< map1
->n
; ++i
) {
2729 for (j
= 0; j
< map2
->n
; ++j
) {
2730 isl_bool d
= isl_basic_map_plain_is_disjoint(map1
->p
[i
],
2732 if (d
!= isl_bool_true
)
2736 return isl_bool_true
;
2739 /* Are "map1" and "map2" disjoint?
2741 * They are disjoint if they are "obviously disjoint" or if one of them
2742 * is empty. Otherwise, they are not disjoint if one of them is universal.
2743 * If none of these cases apply, we compute the intersection and see if
2744 * the result is empty.
2746 isl_bool
isl_map_is_disjoint(__isl_keep isl_map
*map1
, __isl_keep isl_map
*map2
)
2752 disjoint
= isl_map_plain_is_disjoint(map1
, map2
);
2753 if (disjoint
< 0 || disjoint
)
2756 disjoint
= isl_map_is_empty(map1
);
2757 if (disjoint
< 0 || disjoint
)
2760 disjoint
= isl_map_is_empty(map2
);
2761 if (disjoint
< 0 || disjoint
)
2764 intersect
= isl_map_plain_is_universe(map1
);
2765 if (intersect
< 0 || intersect
)
2766 return intersect
< 0 ? isl_bool_error
: isl_bool_false
;
2768 intersect
= isl_map_plain_is_universe(map2
);
2769 if (intersect
< 0 || intersect
)
2770 return intersect
< 0 ? isl_bool_error
: isl_bool_false
;
2772 test
= isl_map_intersect(isl_map_copy(map1
), isl_map_copy(map2
));
2773 disjoint
= isl_map_is_empty(test
);
2779 /* Are "bmap1" and "bmap2" disjoint?
2781 * They are disjoint if they are "obviously disjoint" or if one of them
2782 * is empty. Otherwise, they are not disjoint if one of them is universal.
2783 * If none of these cases apply, we compute the intersection and see if
2784 * the result is empty.
2786 isl_bool
isl_basic_map_is_disjoint(__isl_keep isl_basic_map
*bmap1
,
2787 __isl_keep isl_basic_map
*bmap2
)
2791 isl_basic_map
*test
;
2793 disjoint
= isl_basic_map_plain_is_disjoint(bmap1
, bmap2
);
2794 if (disjoint
< 0 || disjoint
)
2797 disjoint
= isl_basic_map_is_empty(bmap1
);
2798 if (disjoint
< 0 || disjoint
)
2801 disjoint
= isl_basic_map_is_empty(bmap2
);
2802 if (disjoint
< 0 || disjoint
)
2805 intersect
= isl_basic_map_is_universe(bmap1
);
2806 if (intersect
< 0 || intersect
)
2807 return intersect
< 0 ? isl_bool_error
: isl_bool_false
;
2809 intersect
= isl_basic_map_is_universe(bmap2
);
2810 if (intersect
< 0 || intersect
)
2811 return intersect
< 0 ? isl_bool_error
: isl_bool_false
;
2813 test
= isl_basic_map_intersect(isl_basic_map_copy(bmap1
),
2814 isl_basic_map_copy(bmap2
));
2815 disjoint
= isl_basic_map_is_empty(test
);
2816 isl_basic_map_free(test
);
2821 /* Are "bset1" and "bset2" disjoint?
2823 isl_bool
isl_basic_set_is_disjoint(__isl_keep isl_basic_set
*bset1
,
2824 __isl_keep isl_basic_set
*bset2
)
2826 return isl_basic_map_is_disjoint(bset1
, bset2
);
2829 isl_bool
isl_set_plain_is_disjoint(__isl_keep isl_set
*set1
,
2830 __isl_keep isl_set
*set2
)
2832 return isl_map_plain_is_disjoint((struct isl_map
*)set1
,
2833 (struct isl_map
*)set2
);
2836 /* Are "set1" and "set2" disjoint?
2838 isl_bool
isl_set_is_disjoint(__isl_keep isl_set
*set1
, __isl_keep isl_set
*set2
)
2840 return isl_map_is_disjoint(set1
, set2
);
2843 /* Check if we can combine a given div with lower bound l and upper
2844 * bound u with some other div and if so return that other div.
2845 * Otherwise return -1.
2847 * We first check that
2848 * - the bounds are opposites of each other (except for the constant
2850 * - the bounds do not reference any other div
2851 * - no div is defined in terms of this div
2853 * Let m be the size of the range allowed on the div by the bounds.
2854 * That is, the bounds are of the form
2856 * e <= a <= e + m - 1
2858 * with e some expression in the other variables.
2859 * We look for another div b such that no third div is defined in terms
2860 * of this second div b and such that in any constraint that contains
2861 * a (except for the given lower and upper bound), also contains b
2862 * with a coefficient that is m times that of b.
2863 * That is, all constraints (execpt for the lower and upper bound)
2866 * e + f (a + m b) >= 0
2868 * If so, we return b so that "a + m b" can be replaced by
2869 * a single div "c = a + m b".
2871 static int div_find_coalesce(struct isl_basic_map
*bmap
, int *pairs
,
2872 unsigned div
, unsigned l
, unsigned u
)
2878 if (bmap
->n_div
<= 1)
2880 dim
= isl_space_dim(bmap
->dim
, isl_dim_all
);
2881 if (isl_seq_first_non_zero(bmap
->ineq
[l
] + 1 + dim
, div
) != -1)
2883 if (isl_seq_first_non_zero(bmap
->ineq
[l
] + 1 + dim
+ div
+ 1,
2884 bmap
->n_div
- div
- 1) != -1)
2886 if (!isl_seq_is_neg(bmap
->ineq
[l
] + 1, bmap
->ineq
[u
] + 1,
2890 for (i
= 0; i
< bmap
->n_div
; ++i
) {
2891 if (isl_int_is_zero(bmap
->div
[i
][0]))
2893 if (!isl_int_is_zero(bmap
->div
[i
][1 + 1 + dim
+ div
]))
2897 isl_int_add(bmap
->ineq
[l
][0], bmap
->ineq
[l
][0], bmap
->ineq
[u
][0]);
2898 if (isl_int_is_neg(bmap
->ineq
[l
][0])) {
2899 isl_int_sub(bmap
->ineq
[l
][0],
2900 bmap
->ineq
[l
][0], bmap
->ineq
[u
][0]);
2901 bmap
= isl_basic_map_copy(bmap
);
2902 bmap
= isl_basic_map_set_to_empty(bmap
);
2903 isl_basic_map_free(bmap
);
2906 isl_int_add_ui(bmap
->ineq
[l
][0], bmap
->ineq
[l
][0], 1);
2907 for (i
= 0; i
< bmap
->n_div
; ++i
) {
2912 for (j
= 0; j
< bmap
->n_div
; ++j
) {
2913 if (isl_int_is_zero(bmap
->div
[j
][0]))
2915 if (!isl_int_is_zero(bmap
->div
[j
][1 + 1 + dim
+ i
]))
2918 if (j
< bmap
->n_div
)
2920 for (j
= 0; j
< bmap
->n_ineq
; ++j
) {
2922 if (j
== l
|| j
== u
)
2924 if (isl_int_is_zero(bmap
->ineq
[j
][1 + dim
+ div
]))
2926 if (isl_int_is_zero(bmap
->ineq
[j
][1 + dim
+ i
]))
2928 isl_int_mul(bmap
->ineq
[j
][1 + dim
+ div
],
2929 bmap
->ineq
[j
][1 + dim
+ div
],
2931 valid
= isl_int_eq(bmap
->ineq
[j
][1 + dim
+ div
],
2932 bmap
->ineq
[j
][1 + dim
+ i
]);
2933 isl_int_divexact(bmap
->ineq
[j
][1 + dim
+ div
],
2934 bmap
->ineq
[j
][1 + dim
+ div
],
2939 if (j
< bmap
->n_ineq
)
2944 isl_int_sub_ui(bmap
->ineq
[l
][0], bmap
->ineq
[l
][0], 1);
2945 isl_int_sub(bmap
->ineq
[l
][0], bmap
->ineq
[l
][0], bmap
->ineq
[u
][0]);
2949 /* Given a lower and an upper bound on div i, construct an inequality
2950 * that when nonnegative ensures that this pair of bounds always allows
2951 * for an integer value of the given div.
2952 * The lower bound is inequality l, while the upper bound is inequality u.
2953 * The constructed inequality is stored in ineq.
2954 * g, fl, fu are temporary scalars.
2956 * Let the upper bound be
2960 * and the lower bound
2964 * Let n_u = f_u g and n_l = f_l g, with g = gcd(n_u, n_l).
2967 * - f_u e_l <= f_u f_l g a <= f_l e_u
2969 * Since all variables are integer valued, this is equivalent to
2971 * - f_u e_l - (f_u - 1) <= f_u f_l g a <= f_l e_u + (f_l - 1)
2973 * If this interval is at least f_u f_l g, then it contains at least
2974 * one integer value for a.
2975 * That is, the test constraint is
2977 * f_l e_u + f_u e_l + f_l - 1 + f_u - 1 + 1 >= f_u f_l g
2979 static void construct_test_ineq(struct isl_basic_map
*bmap
, int i
,
2980 int l
, int u
, isl_int
*ineq
, isl_int g
, isl_int fl
, isl_int fu
)
2983 dim
= isl_space_dim(bmap
->dim
, isl_dim_all
);
2985 isl_int_gcd(g
, bmap
->ineq
[l
][1 + dim
+ i
], bmap
->ineq
[u
][1 + dim
+ i
]);
2986 isl_int_divexact(fl
, bmap
->ineq
[l
][1 + dim
+ i
], g
);
2987 isl_int_divexact(fu
, bmap
->ineq
[u
][1 + dim
+ i
], g
);
2988 isl_int_neg(fu
, fu
);
2989 isl_seq_combine(ineq
, fl
, bmap
->ineq
[u
], fu
, bmap
->ineq
[l
],
2990 1 + dim
+ bmap
->n_div
);
2991 isl_int_add(ineq
[0], ineq
[0], fl
);
2992 isl_int_add(ineq
[0], ineq
[0], fu
);
2993 isl_int_sub_ui(ineq
[0], ineq
[0], 1);
2994 isl_int_mul(g
, g
, fl
);
2995 isl_int_mul(g
, g
, fu
);
2996 isl_int_sub(ineq
[0], ineq
[0], g
);
2999 /* Remove more kinds of divs that are not strictly needed.
3000 * In particular, if all pairs of lower and upper bounds on a div
3001 * are such that they allow at least one integer value of the div,
3002 * the we can eliminate the div using Fourier-Motzkin without
3003 * introducing any spurious solutions.
3005 static struct isl_basic_map
*drop_more_redundant_divs(
3006 struct isl_basic_map
*bmap
, int *pairs
, int n
)
3008 struct isl_tab
*tab
= NULL
;
3009 struct isl_vec
*vec
= NULL
;
3021 dim
= isl_space_dim(bmap
->dim
, isl_dim_all
);
3022 vec
= isl_vec_alloc(bmap
->ctx
, 1 + dim
+ bmap
->n_div
);
3026 tab
= isl_tab_from_basic_map(bmap
, 0);
3031 enum isl_lp_result res
;
3033 for (i
= 0; i
< bmap
->n_div
; ++i
) {
3036 if (best
>= 0 && pairs
[best
] <= pairs
[i
])
3042 for (l
= 0; l
< bmap
->n_ineq
; ++l
) {
3043 if (!isl_int_is_pos(bmap
->ineq
[l
][1 + dim
+ i
]))
3045 for (u
= 0; u
< bmap
->n_ineq
; ++u
) {
3046 if (!isl_int_is_neg(bmap
->ineq
[u
][1 + dim
+ i
]))
3048 construct_test_ineq(bmap
, i
, l
, u
,
3049 vec
->el
, g
, fl
, fu
);
3050 res
= isl_tab_min(tab
, vec
->el
,
3051 bmap
->ctx
->one
, &g
, NULL
, 0);
3052 if (res
== isl_lp_error
)
3054 if (res
== isl_lp_empty
) {
3055 bmap
= isl_basic_map_set_to_empty(bmap
);
3058 if (res
!= isl_lp_ok
|| isl_int_is_neg(g
))
3061 if (u
< bmap
->n_ineq
)
3064 if (l
== bmap
->n_ineq
) {
3084 bmap
= isl_basic_map_remove_dims(bmap
, isl_dim_div
, remove
, 1);
3085 return isl_basic_map_drop_redundant_divs(bmap
);
3088 isl_basic_map_free(bmap
);
3097 /* Given a pair of divs div1 and div2 such that, expect for the lower bound l
3098 * and the upper bound u, div1 always occurs together with div2 in the form
3099 * (div1 + m div2), where m is the constant range on the variable div1
3100 * allowed by l and u, replace the pair div1 and div2 by a single
3101 * div that is equal to div1 + m div2.
3103 * The new div will appear in the location that contains div2.
3104 * We need to modify all constraints that contain
3105 * div2 = (div - div1) / m
3106 * (If a constraint does not contain div2, it will also not contain div1.)
3107 * If the constraint also contains div1, then we know they appear
3108 * as f (div1 + m div2) and we can simply replace (div1 + m div2) by div,
3109 * i.e., the coefficient of div is f.
3111 * Otherwise, we first need to introduce div1 into the constraint.
3120 * A lower bound on div2
3124 * can be replaced by
3126 * (n * (m div 2 + div1) + m t + n f)/g >= 0
3128 * with g = gcd(m,n).
3133 * can be replaced by
3135 * (-n * (m div2 + div1) + m t + n f')/g >= 0
3137 * These constraint are those that we would obtain from eliminating
3138 * div1 using Fourier-Motzkin.
3140 * After all constraints have been modified, we drop the lower and upper
3141 * bound and then drop div1.
3143 static struct isl_basic_map
*coalesce_divs(struct isl_basic_map
*bmap
,
3144 unsigned div1
, unsigned div2
, unsigned l
, unsigned u
)
3149 unsigned dim
, total
;
3152 dim
= isl_space_dim(bmap
->dim
, isl_dim_all
);
3153 total
= 1 + dim
+ bmap
->n_div
;
3158 isl_int_add(m
, bmap
->ineq
[l
][0], bmap
->ineq
[u
][0]);
3159 isl_int_add_ui(m
, m
, 1);
3161 for (i
= 0; i
< bmap
->n_ineq
; ++i
) {
3162 if (i
== l
|| i
== u
)
3164 if (isl_int_is_zero(bmap
->ineq
[i
][1 + dim
+ div2
]))
3166 if (isl_int_is_zero(bmap
->ineq
[i
][1 + dim
+ div1
])) {
3167 isl_int_gcd(b
, m
, bmap
->ineq
[i
][1 + dim
+ div2
]);
3168 isl_int_divexact(a
, m
, b
);
3169 isl_int_divexact(b
, bmap
->ineq
[i
][1 + dim
+ div2
], b
);
3170 if (isl_int_is_pos(b
)) {
3171 isl_seq_combine(bmap
->ineq
[i
], a
, bmap
->ineq
[i
],
3172 b
, bmap
->ineq
[l
], total
);
3175 isl_seq_combine(bmap
->ineq
[i
], a
, bmap
->ineq
[i
],
3176 b
, bmap
->ineq
[u
], total
);
3179 isl_int_set(bmap
->ineq
[i
][1 + dim
+ div2
],
3180 bmap
->ineq
[i
][1 + dim
+ div1
]);
3181 isl_int_set_si(bmap
->ineq
[i
][1 + dim
+ div1
], 0);
3188 isl_basic_map_drop_inequality(bmap
, l
);
3189 isl_basic_map_drop_inequality(bmap
, u
);
3191 isl_basic_map_drop_inequality(bmap
, u
);
3192 isl_basic_map_drop_inequality(bmap
, l
);
3194 bmap
= isl_basic_map_drop_div(bmap
, div1
);
3198 /* First check if we can coalesce any pair of divs and
3199 * then continue with dropping more redundant divs.
3201 * We loop over all pairs of lower and upper bounds on a div
3202 * with coefficient 1 and -1, respectively, check if there
3203 * is any other div "c" with which we can coalesce the div
3204 * and if so, perform the coalescing.
3206 static struct isl_basic_map
*coalesce_or_drop_more_redundant_divs(
3207 struct isl_basic_map
*bmap
, int *pairs
, int n
)
3212 dim
= isl_space_dim(bmap
->dim
, isl_dim_all
);
3214 for (i
= 0; i
< bmap
->n_div
; ++i
) {
3217 for (l
= 0; l
< bmap
->n_ineq
; ++l
) {
3218 if (!isl_int_is_one(bmap
->ineq
[l
][1 + dim
+ i
]))
3220 for (u
= 0; u
< bmap
->n_ineq
; ++u
) {
3223 if (!isl_int_is_negone(bmap
->ineq
[u
][1+dim
+i
]))
3225 c
= div_find_coalesce(bmap
, pairs
, i
, l
, u
);
3229 bmap
= coalesce_divs(bmap
, i
, c
, l
, u
);
3230 return isl_basic_map_drop_redundant_divs(bmap
);
3235 if (ISL_F_ISSET(bmap
, ISL_BASIC_MAP_EMPTY
))
3238 return drop_more_redundant_divs(bmap
, pairs
, n
);
3241 /* Remove divs that are not strictly needed.
3242 * In particular, if a div only occurs positively (or negatively)
3243 * in constraints, then it can simply be dropped.
3244 * Also, if a div occurs in only two constraints and if moreover
3245 * those two constraints are opposite to each other, except for the constant
3246 * term and if the sum of the constant terms is such that for any value
3247 * of the other values, there is always at least one integer value of the
3248 * div, i.e., if one plus this sum is greater than or equal to
3249 * the (absolute value) of the coefficent of the div in the constraints,
3250 * then we can also simply drop the div.
3252 * We skip divs that appear in equalities or in the definition of other divs.
3253 * Divs that appear in the definition of other divs usually occur in at least
3254 * 4 constraints, but the constraints may have been simplified.
3256 * If any divs are left after these simple checks then we move on
3257 * to more complicated cases in drop_more_redundant_divs.
3259 struct isl_basic_map
*isl_basic_map_drop_redundant_divs(
3260 struct isl_basic_map
*bmap
)
3269 if (bmap
->n_div
== 0)
3272 off
= isl_space_dim(bmap
->dim
, isl_dim_all
);
3273 pairs
= isl_calloc_array(bmap
->ctx
, int, bmap
->n_div
);
3277 for (i
= 0; i
< bmap
->n_div
; ++i
) {
3279 int last_pos
, last_neg
;
3283 defined
= !isl_int_is_zero(bmap
->div
[i
][0]);
3284 for (j
= i
; j
< bmap
->n_div
; ++j
)
3285 if (!isl_int_is_zero(bmap
->div
[j
][1 + 1 + off
+ i
]))
3287 if (j
< bmap
->n_div
)
3289 for (j
= 0; j
< bmap
->n_eq
; ++j
)
3290 if (!isl_int_is_zero(bmap
->eq
[j
][1 + off
+ i
]))
3296 for (j
= 0; j
< bmap
->n_ineq
; ++j
) {
3297 if (isl_int_is_pos(bmap
->ineq
[j
][1 + off
+ i
])) {
3301 if (isl_int_is_neg(bmap
->ineq
[j
][1 + off
+ i
])) {
3306 pairs
[i
] = pos
* neg
;
3307 if (pairs
[i
] == 0) {
3308 for (j
= bmap
->n_ineq
- 1; j
>= 0; --j
)
3309 if (!isl_int_is_zero(bmap
->ineq
[j
][1+off
+i
]))
3310 isl_basic_map_drop_inequality(bmap
, j
);
3311 bmap
= isl_basic_map_drop_div(bmap
, i
);
3313 return isl_basic_map_drop_redundant_divs(bmap
);
3317 if (!isl_seq_is_neg(bmap
->ineq
[last_pos
] + 1,
3318 bmap
->ineq
[last_neg
] + 1,
3322 isl_int_add(bmap
->ineq
[last_pos
][0],
3323 bmap
->ineq
[last_pos
][0], bmap
->ineq
[last_neg
][0]);
3324 isl_int_add_ui(bmap
->ineq
[last_pos
][0],
3325 bmap
->ineq
[last_pos
][0], 1);
3326 redundant
= isl_int_ge(bmap
->ineq
[last_pos
][0],
3327 bmap
->ineq
[last_pos
][1+off
+i
]);
3328 isl_int_sub_ui(bmap
->ineq
[last_pos
][0],
3329 bmap
->ineq
[last_pos
][0], 1);
3330 isl_int_sub(bmap
->ineq
[last_pos
][0],
3331 bmap
->ineq
[last_pos
][0], bmap
->ineq
[last_neg
][0]);
3334 !ok_to_set_div_from_bound(bmap
, i
, last_pos
)) {
3339 bmap
= set_div_from_lower_bound(bmap
, i
, last_pos
);
3340 bmap
= isl_basic_map_simplify(bmap
);
3342 return isl_basic_map_drop_redundant_divs(bmap
);
3344 if (last_pos
> last_neg
) {
3345 isl_basic_map_drop_inequality(bmap
, last_pos
);
3346 isl_basic_map_drop_inequality(bmap
, last_neg
);
3348 isl_basic_map_drop_inequality(bmap
, last_neg
);
3349 isl_basic_map_drop_inequality(bmap
, last_pos
);
3351 bmap
= isl_basic_map_drop_div(bmap
, i
);
3353 return isl_basic_map_drop_redundant_divs(bmap
);
3357 return coalesce_or_drop_more_redundant_divs(bmap
, pairs
, n
);
3363 isl_basic_map_free(bmap
);
3367 struct isl_basic_set
*isl_basic_set_drop_redundant_divs(
3368 struct isl_basic_set
*bset
)
3370 return (struct isl_basic_set
*)
3371 isl_basic_map_drop_redundant_divs((struct isl_basic_map
*)bset
);
3374 struct isl_map
*isl_map_drop_redundant_divs(struct isl_map
*map
)
3380 for (i
= 0; i
< map
->n
; ++i
) {
3381 map
->p
[i
] = isl_basic_map_drop_redundant_divs(map
->p
[i
]);
3385 ISL_F_CLR(map
, ISL_MAP_NORMALIZED
);
3392 struct isl_set
*isl_set_drop_redundant_divs(struct isl_set
*set
)
3394 return (struct isl_set
*)
3395 isl_map_drop_redundant_divs((struct isl_map
*)set
);
3398 /* Does "bmap" satisfy any equality that involves more than 2 variables
3399 * and/or has coefficients different from -1 and 1?
3401 static int has_multiple_var_equality(__isl_keep isl_basic_map
*bmap
)
3406 total
= isl_basic_map_dim(bmap
, isl_dim_all
);
3408 for (i
= 0; i
< bmap
->n_eq
; ++i
) {
3411 j
= isl_seq_first_non_zero(bmap
->eq
[i
] + 1, total
);
3414 if (!isl_int_is_one(bmap
->eq
[i
][1 + j
]) &&
3415 !isl_int_is_negone(bmap
->eq
[i
][1 + j
]))
3419 k
= isl_seq_first_non_zero(bmap
->eq
[i
] + 1 + j
, total
- j
);
3423 if (!isl_int_is_one(bmap
->eq
[i
][1 + j
]) &&
3424 !isl_int_is_negone(bmap
->eq
[i
][1 + j
]))
3428 k
= isl_seq_first_non_zero(bmap
->eq
[i
] + 1 + j
, total
- j
);
3436 /* Remove any common factor g from the constraint coefficients in "v".
3437 * The constant term is stored in the first position and is replaced
3438 * by floor(c/g). If any common factor is removed and if this results
3439 * in a tightening of the constraint, then set *tightened.
3441 static __isl_give isl_vec
*normalize_constraint(__isl_take isl_vec
*v
,
3448 ctx
= isl_vec_get_ctx(v
);
3449 isl_seq_gcd(v
->el
+ 1, v
->size
- 1, &ctx
->normalize_gcd
);
3450 if (isl_int_is_zero(ctx
->normalize_gcd
))
3452 if (isl_int_is_one(ctx
->normalize_gcd
))
3457 if (tightened
&& !isl_int_is_divisible_by(v
->el
[0], ctx
->normalize_gcd
))
3459 isl_int_fdiv_q(v
->el
[0], v
->el
[0], ctx
->normalize_gcd
);
3460 isl_seq_scale_down(v
->el
+ 1, v
->el
+ 1, ctx
->normalize_gcd
,
3465 /* If "bmap" is an integer set that satisfies any equality involving
3466 * more than 2 variables and/or has coefficients different from -1 and 1,
3467 * then use variable compression to reduce the coefficients by removing
3468 * any (hidden) common factor.
3469 * In particular, apply the variable compression to each constraint,
3470 * factor out any common factor in the non-constant coefficients and
3471 * then apply the inverse of the compression.
3472 * At the end, we mark the basic map as having reduced constants.
3473 * If this flag is still set on the next invocation of this function,
3474 * then we skip the computation.
3476 * Removing a common factor may result in a tightening of some of
3477 * the constraints. If this happens, then we may end up with two
3478 * opposite inequalities that can be replaced by an equality.
3479 * We therefore call isl_basic_map_detect_inequality_pairs,
3480 * which checks for such pairs of inequalities as well as eliminate_divs_eq
3481 * and isl_basic_map_gauss if such a pair was found.
3483 __isl_give isl_basic_map
*isl_basic_map_reduce_coefficients(
3484 __isl_take isl_basic_map
*bmap
)
3489 isl_mat
*eq
, *T
, *T2
;
3495 if (ISL_F_ISSET(bmap
, ISL_BASIC_MAP_REDUCED_COEFFICIENTS
))
3497 if (isl_basic_map_is_rational(bmap
))
3499 if (bmap
->n_eq
== 0)
3501 if (!has_multiple_var_equality(bmap
))
3504 total
= isl_basic_map_dim(bmap
, isl_dim_all
);
3505 ctx
= isl_basic_map_get_ctx(bmap
);
3506 v
= isl_vec_alloc(ctx
, 1 + total
);
3508 return isl_basic_map_free(bmap
);
3510 eq
= isl_mat_sub_alloc6(ctx
, bmap
->eq
, 0, bmap
->n_eq
, 0, 1 + total
);
3511 T
= isl_mat_variable_compression(eq
, &T2
);
3514 if (T
->n_col
== 0) {
3518 return isl_basic_map_set_to_empty(bmap
);
3522 for (i
= 0; i
< bmap
->n_ineq
; ++i
) {
3523 isl_seq_cpy(v
->el
, bmap
->ineq
[i
], 1 + total
);
3524 v
= isl_vec_mat_product(v
, isl_mat_copy(T
));
3525 v
= normalize_constraint(v
, &tightened
);
3526 v
= isl_vec_mat_product(v
, isl_mat_copy(T2
));
3529 isl_seq_cpy(bmap
->ineq
[i
], v
->el
, 1 + total
);
3536 ISL_F_SET(bmap
, ISL_BASIC_MAP_REDUCED_COEFFICIENTS
);
3541 bmap
= isl_basic_map_detect_inequality_pairs(bmap
, &progress
);
3543 bmap
= eliminate_divs_eq(bmap
, &progress
);
3544 bmap
= isl_basic_map_gauss(bmap
, NULL
);
3553 return isl_basic_map_free(bmap
);
3556 /* Shift the integer division at position "div" of "bmap"
3557 * by "shift" times the variable at position "pos".
3558 * "pos" is as determined by isl_basic_map_offset, i.e., pos == 0
3559 * corresponds to the constant term.
3561 * That is, if the integer division has the form
3565 * then replace it by
3567 * floor((f(x) + shift * d * x_pos)/d) - shift * x_pos
3569 __isl_give isl_basic_map
*isl_basic_map_shift_div(
3570 __isl_take isl_basic_map
*bmap
, int div
, int pos
, isl_int shift
)
3578 total
= isl_basic_map_dim(bmap
, isl_dim_all
);
3579 total
-= isl_basic_map_dim(bmap
, isl_dim_div
);
3581 isl_int_addmul(bmap
->div
[div
][1 + pos
], shift
, bmap
->div
[div
][0]);
3583 for (i
= 0; i
< bmap
->n_eq
; ++i
) {
3584 if (isl_int_is_zero(bmap
->eq
[i
][1 + total
+ div
]))
3586 isl_int_submul(bmap
->eq
[i
][pos
],
3587 shift
, bmap
->eq
[i
][1 + total
+ div
]);
3589 for (i
= 0; i
< bmap
->n_ineq
; ++i
) {
3590 if (isl_int_is_zero(bmap
->ineq
[i
][1 + total
+ div
]))
3592 isl_int_submul(bmap
->ineq
[i
][pos
],
3593 shift
, bmap
->ineq
[i
][1 + total
+ div
]);
3595 for (i
= 0; i
< bmap
->n_div
; ++i
) {
3596 if (isl_int_is_zero(bmap
->div
[i
][0]))
3598 if (isl_int_is_zero(bmap
->div
[i
][1 + 1 + total
+ div
]))
3600 isl_int_submul(bmap
->div
[i
][1 + pos
],
3601 shift
, bmap
->div
[i
][1 + 1 + total
+ div
]);