2 * Copyright 2008-2009 Katholieke Universiteit Leuven
3 * Copyright 2012-2013 Ecole Normale Superieure
4 * Copyright 2014-2015 INRIA Rocquencourt
5 * Copyright 2016 Sven Verdoolaege
7 * Use of this software is governed by the MIT license
9 * Written by Sven Verdoolaege, K.U.Leuven, Departement
10 * Computerwetenschappen, Celestijnenlaan 200A, B-3001 Leuven, Belgium
11 * and Ecole Normale Superieure, 45 rue d’Ulm, 75230 Paris, France
12 * and Inria Paris - Rocquencourt, Domaine de Voluceau - Rocquencourt,
13 * B.P. 105 - 78153 Le Chesnay, France
16 #include <isl_ctx_private.h>
17 #include <isl_map_private.h>
18 #include "isl_equalities.h"
22 #include <isl_space_private.h>
23 #include <isl_mat_private.h>
24 #include <isl_vec_private.h>
26 static void swap_equality(struct isl_basic_map
*bmap
, int a
, int b
)
28 isl_int
*t
= bmap
->eq
[a
];
29 bmap
->eq
[a
] = bmap
->eq
[b
];
33 static void swap_inequality(struct isl_basic_map
*bmap
, int a
, int b
)
36 isl_int
*t
= bmap
->ineq
[a
];
37 bmap
->ineq
[a
] = bmap
->ineq
[b
];
42 static void constraint_drop_vars(isl_int
*c
, unsigned n
, unsigned rem
)
44 isl_seq_cpy(c
, c
+ n
, rem
);
45 isl_seq_clr(c
+ rem
, n
);
48 /* Drop n dimensions starting at first.
50 * In principle, this frees up some extra variables as the number
51 * of columns remains constant, but we would have to extend
52 * the div array too as the number of rows in this array is assumed
53 * to be equal to extra.
55 struct isl_basic_set
*isl_basic_set_drop_dims(
56 struct isl_basic_set
*bset
, unsigned first
, unsigned n
)
63 isl_assert(bset
->ctx
, first
+ n
<= bset
->dim
->n_out
, goto error
);
65 if (n
== 0 && !isl_space_get_tuple_name(bset
->dim
, isl_dim_set
))
68 bset
= isl_basic_set_cow(bset
);
72 for (i
= 0; i
< bset
->n_eq
; ++i
)
73 constraint_drop_vars(bset
->eq
[i
]+1+bset
->dim
->nparam
+first
, n
,
74 (bset
->dim
->n_out
-first
-n
)+bset
->extra
);
76 for (i
= 0; i
< bset
->n_ineq
; ++i
)
77 constraint_drop_vars(bset
->ineq
[i
]+1+bset
->dim
->nparam
+first
, n
,
78 (bset
->dim
->n_out
-first
-n
)+bset
->extra
);
80 for (i
= 0; i
< bset
->n_div
; ++i
)
81 constraint_drop_vars(bset
->div
[i
]+1+1+bset
->dim
->nparam
+first
, n
,
82 (bset
->dim
->n_out
-first
-n
)+bset
->extra
);
84 bset
->dim
= isl_space_drop_outputs(bset
->dim
, first
, n
);
88 ISL_F_CLR(bset
, ISL_BASIC_SET_NORMALIZED
);
89 bset
= isl_basic_set_simplify(bset
);
90 return isl_basic_set_finalize(bset
);
92 isl_basic_set_free(bset
);
96 struct isl_set
*isl_set_drop_dims(
97 struct isl_set
*set
, unsigned first
, unsigned n
)
104 isl_assert(set
->ctx
, first
+ n
<= set
->dim
->n_out
, goto error
);
106 if (n
== 0 && !isl_space_get_tuple_name(set
->dim
, isl_dim_set
))
108 set
= isl_set_cow(set
);
111 set
->dim
= isl_space_drop_outputs(set
->dim
, first
, n
);
115 for (i
= 0; i
< set
->n
; ++i
) {
116 set
->p
[i
] = isl_basic_set_drop_dims(set
->p
[i
], first
, n
);
121 ISL_F_CLR(set
, ISL_SET_NORMALIZED
);
128 /* Move "n" divs starting at "first" to the end of the list of divs.
130 static struct isl_basic_map
*move_divs_last(struct isl_basic_map
*bmap
,
131 unsigned first
, unsigned n
)
136 if (first
+ n
== bmap
->n_div
)
139 div
= isl_alloc_array(bmap
->ctx
, isl_int
*, n
);
142 for (i
= 0; i
< n
; ++i
)
143 div
[i
] = bmap
->div
[first
+ i
];
144 for (i
= 0; i
< bmap
->n_div
- first
- n
; ++i
)
145 bmap
->div
[first
+ i
] = bmap
->div
[first
+ n
+ i
];
146 for (i
= 0; i
< n
; ++i
)
147 bmap
->div
[bmap
->n_div
- n
+ i
] = div
[i
];
151 isl_basic_map_free(bmap
);
155 /* Drop "n" dimensions of type "type" starting at "first".
157 * In principle, this frees up some extra variables as the number
158 * of columns remains constant, but we would have to extend
159 * the div array too as the number of rows in this array is assumed
160 * to be equal to extra.
162 struct isl_basic_map
*isl_basic_map_drop(struct isl_basic_map
*bmap
,
163 enum isl_dim_type type
, unsigned first
, unsigned n
)
173 dim
= isl_basic_map_dim(bmap
, type
);
174 isl_assert(bmap
->ctx
, first
+ n
<= dim
, goto error
);
176 if (n
== 0 && !isl_space_is_named_or_nested(bmap
->dim
, type
))
179 bmap
= isl_basic_map_cow(bmap
);
183 offset
= isl_basic_map_offset(bmap
, type
) + first
;
184 left
= isl_basic_map_total_dim(bmap
) - (offset
- 1) - n
;
185 for (i
= 0; i
< bmap
->n_eq
; ++i
)
186 constraint_drop_vars(bmap
->eq
[i
]+offset
, n
, left
);
188 for (i
= 0; i
< bmap
->n_ineq
; ++i
)
189 constraint_drop_vars(bmap
->ineq
[i
]+offset
, n
, left
);
191 for (i
= 0; i
< bmap
->n_div
; ++i
)
192 constraint_drop_vars(bmap
->div
[i
]+1+offset
, n
, left
);
194 if (type
== isl_dim_div
) {
195 bmap
= move_divs_last(bmap
, first
, n
);
198 isl_basic_map_free_div(bmap
, n
);
200 bmap
->dim
= isl_space_drop_dims(bmap
->dim
, type
, first
, n
);
204 ISL_F_CLR(bmap
, ISL_BASIC_MAP_NORMALIZED
);
205 bmap
= isl_basic_map_simplify(bmap
);
206 return isl_basic_map_finalize(bmap
);
208 isl_basic_map_free(bmap
);
212 __isl_give isl_basic_set
*isl_basic_set_drop(__isl_take isl_basic_set
*bset
,
213 enum isl_dim_type type
, unsigned first
, unsigned n
)
215 return (isl_basic_set
*)isl_basic_map_drop((isl_basic_map
*)bset
,
219 struct isl_basic_map
*isl_basic_map_drop_inputs(
220 struct isl_basic_map
*bmap
, unsigned first
, unsigned n
)
222 return isl_basic_map_drop(bmap
, isl_dim_in
, first
, n
);
225 struct isl_map
*isl_map_drop(struct isl_map
*map
,
226 enum isl_dim_type type
, unsigned first
, unsigned n
)
233 isl_assert(map
->ctx
, first
+ n
<= isl_map_dim(map
, type
), goto error
);
235 if (n
== 0 && !isl_space_get_tuple_name(map
->dim
, type
))
237 map
= isl_map_cow(map
);
240 map
->dim
= isl_space_drop_dims(map
->dim
, type
, first
, n
);
244 for (i
= 0; i
< map
->n
; ++i
) {
245 map
->p
[i
] = isl_basic_map_drop(map
->p
[i
], type
, first
, n
);
249 ISL_F_CLR(map
, ISL_MAP_NORMALIZED
);
257 struct isl_set
*isl_set_drop(struct isl_set
*set
,
258 enum isl_dim_type type
, unsigned first
, unsigned n
)
260 return (isl_set
*)isl_map_drop((isl_map
*)set
, type
, first
, n
);
263 struct isl_map
*isl_map_drop_inputs(
264 struct isl_map
*map
, unsigned first
, unsigned n
)
266 return isl_map_drop(map
, isl_dim_in
, first
, n
);
270 * We don't cow, as the div is assumed to be redundant.
272 static struct isl_basic_map
*isl_basic_map_drop_div(
273 struct isl_basic_map
*bmap
, unsigned div
)
281 pos
= 1 + isl_space_dim(bmap
->dim
, isl_dim_all
) + div
;
283 isl_assert(bmap
->ctx
, div
< bmap
->n_div
, goto error
);
285 for (i
= 0; i
< bmap
->n_eq
; ++i
)
286 constraint_drop_vars(bmap
->eq
[i
]+pos
, 1, bmap
->extra
-div
-1);
288 for (i
= 0; i
< bmap
->n_ineq
; ++i
) {
289 if (!isl_int_is_zero(bmap
->ineq
[i
][pos
])) {
290 isl_basic_map_drop_inequality(bmap
, i
);
294 constraint_drop_vars(bmap
->ineq
[i
]+pos
, 1, bmap
->extra
-div
-1);
297 for (i
= 0; i
< bmap
->n_div
; ++i
)
298 constraint_drop_vars(bmap
->div
[i
]+1+pos
, 1, bmap
->extra
-div
-1);
300 if (div
!= bmap
->n_div
- 1) {
302 isl_int
*t
= bmap
->div
[div
];
304 for (j
= div
; j
< bmap
->n_div
- 1; ++j
)
305 bmap
->div
[j
] = bmap
->div
[j
+1];
307 bmap
->div
[bmap
->n_div
- 1] = t
;
309 ISL_F_CLR(bmap
, ISL_BASIC_MAP_NORMALIZED
);
310 isl_basic_map_free_div(bmap
, 1);
314 isl_basic_map_free(bmap
);
318 struct isl_basic_map
*isl_basic_map_normalize_constraints(
319 struct isl_basic_map
*bmap
)
323 unsigned total
= isl_basic_map_total_dim(bmap
);
329 for (i
= bmap
->n_eq
- 1; i
>= 0; --i
) {
330 isl_seq_gcd(bmap
->eq
[i
]+1, total
, &gcd
);
331 if (isl_int_is_zero(gcd
)) {
332 if (!isl_int_is_zero(bmap
->eq
[i
][0])) {
333 bmap
= isl_basic_map_set_to_empty(bmap
);
336 isl_basic_map_drop_equality(bmap
, i
);
339 if (ISL_F_ISSET(bmap
, ISL_BASIC_MAP_RATIONAL
))
340 isl_int_gcd(gcd
, gcd
, bmap
->eq
[i
][0]);
341 if (isl_int_is_one(gcd
))
343 if (!isl_int_is_divisible_by(bmap
->eq
[i
][0], gcd
)) {
344 bmap
= isl_basic_map_set_to_empty(bmap
);
347 isl_seq_scale_down(bmap
->eq
[i
], bmap
->eq
[i
], gcd
, 1+total
);
350 for (i
= bmap
->n_ineq
- 1; i
>= 0; --i
) {
351 isl_seq_gcd(bmap
->ineq
[i
]+1, total
, &gcd
);
352 if (isl_int_is_zero(gcd
)) {
353 if (isl_int_is_neg(bmap
->ineq
[i
][0])) {
354 bmap
= isl_basic_map_set_to_empty(bmap
);
357 isl_basic_map_drop_inequality(bmap
, i
);
360 if (ISL_F_ISSET(bmap
, ISL_BASIC_MAP_RATIONAL
))
361 isl_int_gcd(gcd
, gcd
, bmap
->ineq
[i
][0]);
362 if (isl_int_is_one(gcd
))
364 isl_int_fdiv_q(bmap
->ineq
[i
][0], bmap
->ineq
[i
][0], gcd
);
365 isl_seq_scale_down(bmap
->ineq
[i
]+1, bmap
->ineq
[i
]+1, gcd
, total
);
372 struct isl_basic_set
*isl_basic_set_normalize_constraints(
373 struct isl_basic_set
*bset
)
375 return (struct isl_basic_set
*)isl_basic_map_normalize_constraints(
376 (struct isl_basic_map
*)bset
);
379 /* Assuming the variable at position "pos" has an integer coefficient
380 * in integer division "div", extract it from this integer division.
381 * "pos" is as determined by isl_basic_map_offset, i.e., pos == 0
382 * corresponds to the constant term.
384 * That is, the integer division is of the form
386 * floor((... + c * d * x_pos + ...)/d)
390 * floor((... + 0 * x_pos + ...)/d) + c * x_pos
392 static __isl_give isl_basic_map
*remove_var_from_div(
393 __isl_take isl_basic_map
*bmap
, int div
, int pos
)
398 isl_int_divexact(shift
, bmap
->div
[div
][1 + pos
], bmap
->div
[div
][0]);
399 isl_int_neg(shift
, shift
);
400 bmap
= isl_basic_map_shift_div(bmap
, div
, pos
, shift
);
401 isl_int_clear(shift
);
406 /* Check if integer division "div" has any integral coefficient
407 * (or constant term). If so, extract them from the integer division.
409 static __isl_give isl_basic_map
*remove_independent_vars_from_div(
410 __isl_take isl_basic_map
*bmap
, int div
)
413 unsigned total
= 1 + isl_basic_map_total_dim(bmap
);
415 for (i
= 0; i
< total
; ++i
) {
416 if (isl_int_is_zero(bmap
->div
[div
][1 + i
]))
418 if (!isl_int_is_divisible_by(bmap
->div
[div
][1 + i
],
421 bmap
= remove_var_from_div(bmap
, div
, i
);
429 /* Check if any known integer division has any integral coefficient
430 * (or constant term). If so, extract them from the integer division.
432 static __isl_give isl_basic_map
*remove_independent_vars_from_divs(
433 __isl_take isl_basic_map
*bmap
)
439 if (bmap
->n_div
== 0)
442 for (i
= 0; i
< bmap
->n_div
; ++i
) {
443 if (isl_int_is_zero(bmap
->div
[i
][0]))
445 bmap
= remove_independent_vars_from_div(bmap
, i
);
453 /* Remove any common factor in numerator and denominator of the div expression,
454 * not taking into account the constant term.
455 * That is, if the div is of the form
457 * floor((a + m f(x))/(m d))
461 * floor((floor(a/m) + f(x))/d)
463 * The difference {a/m}/d in the argument satisfies 0 <= {a/m}/d < 1/d
464 * and can therefore not influence the result of the floor.
466 static void normalize_div_expression(__isl_keep isl_basic_map
*bmap
, int div
)
468 unsigned total
= isl_basic_map_total_dim(bmap
);
469 isl_ctx
*ctx
= bmap
->ctx
;
471 if (isl_int_is_zero(bmap
->div
[div
][0]))
473 isl_seq_gcd(bmap
->div
[div
] + 2, total
, &ctx
->normalize_gcd
);
474 isl_int_gcd(ctx
->normalize_gcd
, ctx
->normalize_gcd
, bmap
->div
[div
][0]);
475 if (isl_int_is_one(ctx
->normalize_gcd
))
477 isl_int_fdiv_q(bmap
->div
[div
][1], bmap
->div
[div
][1],
479 isl_int_divexact(bmap
->div
[div
][0], bmap
->div
[div
][0],
481 isl_seq_scale_down(bmap
->div
[div
] + 2, bmap
->div
[div
] + 2,
482 ctx
->normalize_gcd
, total
);
485 /* Remove any common factor in numerator and denominator of a div expression,
486 * not taking into account the constant term.
487 * That is, look for any div of the form
489 * floor((a + m f(x))/(m d))
493 * floor((floor(a/m) + f(x))/d)
495 * The difference {a/m}/d in the argument satisfies 0 <= {a/m}/d < 1/d
496 * and can therefore not influence the result of the floor.
498 static __isl_give isl_basic_map
*normalize_div_expressions(
499 __isl_take isl_basic_map
*bmap
)
505 if (bmap
->n_div
== 0)
508 for (i
= 0; i
< bmap
->n_div
; ++i
)
509 normalize_div_expression(bmap
, i
);
514 /* Assumes divs have been ordered if keep_divs is set.
516 static void eliminate_var_using_equality(struct isl_basic_map
*bmap
,
517 unsigned pos
, isl_int
*eq
, int keep_divs
, int *progress
)
520 unsigned space_total
;
524 total
= isl_basic_map_total_dim(bmap
);
525 space_total
= isl_space_dim(bmap
->dim
, isl_dim_all
);
526 last_div
= isl_seq_last_non_zero(eq
+ 1 + space_total
, bmap
->n_div
);
527 for (k
= 0; k
< bmap
->n_eq
; ++k
) {
528 if (bmap
->eq
[k
] == eq
)
530 if (isl_int_is_zero(bmap
->eq
[k
][1+pos
]))
534 isl_seq_elim(bmap
->eq
[k
], eq
, 1+pos
, 1+total
, NULL
);
535 isl_seq_normalize(bmap
->ctx
, bmap
->eq
[k
], 1 + total
);
538 for (k
= 0; k
< bmap
->n_ineq
; ++k
) {
539 if (isl_int_is_zero(bmap
->ineq
[k
][1+pos
]))
543 isl_seq_elim(bmap
->ineq
[k
], eq
, 1+pos
, 1+total
, NULL
);
544 isl_seq_normalize(bmap
->ctx
, bmap
->ineq
[k
], 1 + total
);
545 ISL_F_CLR(bmap
, ISL_BASIC_MAP_NORMALIZED
);
548 for (k
= 0; k
< bmap
->n_div
; ++k
) {
549 if (isl_int_is_zero(bmap
->div
[k
][0]))
551 if (isl_int_is_zero(bmap
->div
[k
][1+1+pos
]))
555 /* We need to be careful about circular definitions,
556 * so for now we just remove the definition of div k
557 * if the equality contains any divs.
558 * If keep_divs is set, then the divs have been ordered
559 * and we can keep the definition as long as the result
562 if (last_div
== -1 || (keep_divs
&& last_div
< k
)) {
563 isl_seq_elim(bmap
->div
[k
]+1, eq
,
564 1+pos
, 1+total
, &bmap
->div
[k
][0]);
565 normalize_div_expression(bmap
, k
);
567 isl_seq_clr(bmap
->div
[k
], 1 + total
);
568 ISL_F_CLR(bmap
, ISL_BASIC_MAP_NORMALIZED
);
572 /* Assumes divs have been ordered if keep_divs is set.
574 static __isl_give isl_basic_map
*eliminate_div(__isl_take isl_basic_map
*bmap
,
575 isl_int
*eq
, unsigned div
, int keep_divs
)
577 unsigned pos
= isl_space_dim(bmap
->dim
, isl_dim_all
) + div
;
579 eliminate_var_using_equality(bmap
, pos
, eq
, keep_divs
, NULL
);
581 bmap
= isl_basic_map_drop_div(bmap
, div
);
586 /* Check if elimination of div "div" using equality "eq" would not
587 * result in a div depending on a later div.
589 static int ok_to_eliminate_div(struct isl_basic_map
*bmap
, isl_int
*eq
,
594 unsigned space_total
= isl_space_dim(bmap
->dim
, isl_dim_all
);
595 unsigned pos
= space_total
+ div
;
597 last_div
= isl_seq_last_non_zero(eq
+ 1 + space_total
, bmap
->n_div
);
598 if (last_div
< 0 || last_div
<= div
)
601 for (k
= 0; k
<= last_div
; ++k
) {
602 if (isl_int_is_zero(bmap
->div
[k
][0]))
604 if (!isl_int_is_zero(bmap
->div
[k
][1 + 1 + pos
]))
611 /* Elimininate divs based on equalities
613 static struct isl_basic_map
*eliminate_divs_eq(
614 struct isl_basic_map
*bmap
, int *progress
)
621 bmap
= isl_basic_map_order_divs(bmap
);
626 off
= 1 + isl_space_dim(bmap
->dim
, isl_dim_all
);
628 for (d
= bmap
->n_div
- 1; d
>= 0 ; --d
) {
629 for (i
= 0; i
< bmap
->n_eq
; ++i
) {
630 if (!isl_int_is_one(bmap
->eq
[i
][off
+ d
]) &&
631 !isl_int_is_negone(bmap
->eq
[i
][off
+ d
]))
633 if (!ok_to_eliminate_div(bmap
, bmap
->eq
[i
], d
))
637 bmap
= eliminate_div(bmap
, bmap
->eq
[i
], d
, 1);
638 if (isl_basic_map_drop_equality(bmap
, i
) < 0)
639 return isl_basic_map_free(bmap
);
644 return eliminate_divs_eq(bmap
, progress
);
648 /* Elimininate divs based on inequalities
650 static struct isl_basic_map
*eliminate_divs_ineq(
651 struct isl_basic_map
*bmap
, int *progress
)
662 off
= 1 + isl_space_dim(bmap
->dim
, isl_dim_all
);
664 for (d
= bmap
->n_div
- 1; d
>= 0 ; --d
) {
665 for (i
= 0; i
< bmap
->n_eq
; ++i
)
666 if (!isl_int_is_zero(bmap
->eq
[i
][off
+ d
]))
670 for (i
= 0; i
< bmap
->n_ineq
; ++i
)
671 if (isl_int_abs_gt(bmap
->ineq
[i
][off
+ d
], ctx
->one
))
673 if (i
< bmap
->n_ineq
)
676 bmap
= isl_basic_map_eliminate_vars(bmap
, (off
-1)+d
, 1);
677 if (!bmap
|| ISL_F_ISSET(bmap
, ISL_BASIC_MAP_EMPTY
))
679 bmap
= isl_basic_map_drop_div(bmap
, d
);
686 struct isl_basic_map
*isl_basic_map_gauss(
687 struct isl_basic_map
*bmap
, int *progress
)
695 bmap
= isl_basic_map_order_divs(bmap
);
700 total
= isl_basic_map_total_dim(bmap
);
701 total_var
= total
- bmap
->n_div
;
703 last_var
= total
- 1;
704 for (done
= 0; done
< bmap
->n_eq
; ++done
) {
705 for (; last_var
>= 0; --last_var
) {
706 for (k
= done
; k
< bmap
->n_eq
; ++k
)
707 if (!isl_int_is_zero(bmap
->eq
[k
][1+last_var
]))
715 swap_equality(bmap
, k
, done
);
716 if (isl_int_is_neg(bmap
->eq
[done
][1+last_var
]))
717 isl_seq_neg(bmap
->eq
[done
], bmap
->eq
[done
], 1+total
);
719 eliminate_var_using_equality(bmap
, last_var
, bmap
->eq
[done
], 1,
722 if (last_var
>= total_var
&&
723 isl_int_is_zero(bmap
->div
[last_var
- total_var
][0])) {
724 unsigned div
= last_var
- total_var
;
725 isl_seq_neg(bmap
->div
[div
]+1, bmap
->eq
[done
], 1+total
);
726 isl_int_set_si(bmap
->div
[div
][1+1+last_var
], 0);
727 isl_int_set(bmap
->div
[div
][0],
728 bmap
->eq
[done
][1+last_var
]);
731 ISL_F_CLR(bmap
, ISL_BASIC_MAP_NORMALIZED
);
734 if (done
== bmap
->n_eq
)
736 for (k
= done
; k
< bmap
->n_eq
; ++k
) {
737 if (isl_int_is_zero(bmap
->eq
[k
][0]))
739 return isl_basic_map_set_to_empty(bmap
);
741 isl_basic_map_free_equality(bmap
, bmap
->n_eq
-done
);
745 struct isl_basic_set
*isl_basic_set_gauss(
746 struct isl_basic_set
*bset
, int *progress
)
748 return (struct isl_basic_set
*)isl_basic_map_gauss(
749 (struct isl_basic_map
*)bset
, progress
);
753 static unsigned int round_up(unsigned int v
)
764 /* Hash table of inequalities in a basic map.
765 * "index" is an array of addresses of inequalities in the basic map, some
766 * of which are NULL. The inequalities are hashed on the coefficients
767 * except the constant term.
768 * "size" is the number of elements in the array and is always a power of two
769 * "bits" is the number of bits need to represent an index into the array.
770 * "total" is the total dimension of the basic map.
772 struct isl_constraint_index
{
779 /* Fill in the "ci" data structure for holding the inequalities of "bmap".
781 static isl_stat
create_constraint_index(struct isl_constraint_index
*ci
,
782 __isl_keep isl_basic_map
*bmap
)
788 return isl_stat_error
;
789 ci
->total
= isl_basic_set_total_dim(bmap
);
790 if (bmap
->n_ineq
== 0)
792 ci
->size
= round_up(4 * (bmap
->n_ineq
+ 1) / 3 - 1);
793 ci
->bits
= ffs(ci
->size
) - 1;
794 ctx
= isl_basic_map_get_ctx(bmap
);
795 ci
->index
= isl_calloc_array(ctx
, isl_int
**, ci
->size
);
797 return isl_stat_error
;
802 /* Free the memory allocated by create_constraint_index.
804 static void constraint_index_free(struct isl_constraint_index
*ci
)
809 /* Return the position in ci->index that contains the address of
810 * an inequality that is equal to *ineq up to the constant term,
811 * provided this address is not identical to "ineq".
812 * If there is no such inequality, then return the position where
813 * such an inequality should be inserted.
815 static int hash_index_ineq(struct isl_constraint_index
*ci
, isl_int
**ineq
)
818 uint32_t hash
= isl_seq_get_hash_bits((*ineq
) + 1, ci
->total
, ci
->bits
);
819 for (h
= hash
; ci
->index
[h
]; h
= (h
+1) % ci
->size
)
820 if (ineq
!= ci
->index
[h
] &&
821 isl_seq_eq((*ineq
) + 1, ci
->index
[h
][0]+1, ci
->total
))
826 /* Return the position in ci->index that contains the address of
827 * an inequality that is equal to the k'th inequality of "bmap"
828 * up to the constant term, provided it does not point to the very
830 * If there is no such inequality, then return the position where
831 * such an inequality should be inserted.
833 static int hash_index(struct isl_constraint_index
*ci
,
834 __isl_keep isl_basic_map
*bmap
, int k
)
836 return hash_index_ineq(ci
, &bmap
->ineq
[k
]);
839 static int set_hash_index(struct isl_constraint_index
*ci
,
840 struct isl_basic_set
*bset
, int k
)
842 return hash_index(ci
, bset
, k
);
845 /* Fill in the "ci" data structure with the inequalities of "bset".
847 static isl_stat
setup_constraint_index(struct isl_constraint_index
*ci
,
848 __isl_keep isl_basic_set
*bset
)
852 if (create_constraint_index(ci
, bset
) < 0)
853 return isl_stat_error
;
855 for (k
= 0; k
< bset
->n_ineq
; ++k
) {
856 h
= set_hash_index(ci
, bset
, k
);
857 ci
->index
[h
] = &bset
->ineq
[k
];
863 /* Is the inequality ineq (obviously) redundant with respect
864 * to the constraints in "ci"?
866 * Look for an inequality in "ci" with the same coefficients and then
867 * check if the contant term of "ineq" is greater than or equal
868 * to the constant term of that inequality. If so, "ineq" is clearly
871 * Note that hash_index_ineq ignores a stored constraint if it has
872 * the same address as the passed inequality. It is ok to pass
873 * the address of a local variable here since it will never be
874 * the same as the address of a constraint in "ci".
876 static isl_bool
constraint_index_is_redundant(struct isl_constraint_index
*ci
,
881 h
= hash_index_ineq(ci
, &ineq
);
883 return isl_bool_false
;
884 return isl_int_ge(ineq
[0], (*ci
->index
[h
])[0]);
887 /* If we can eliminate more than one div, then we need to make
888 * sure we do it from last div to first div, in order not to
889 * change the position of the other divs that still need to
892 static struct isl_basic_map
*remove_duplicate_divs(
893 struct isl_basic_map
*bmap
, int *progress
)
905 bmap
= isl_basic_map_order_divs(bmap
);
906 if (!bmap
|| bmap
->n_div
<= 1)
909 total_var
= isl_space_dim(bmap
->dim
, isl_dim_all
);
910 total
= total_var
+ bmap
->n_div
;
913 for (k
= bmap
->n_div
- 1; k
>= 0; --k
)
914 if (!isl_int_is_zero(bmap
->div
[k
][0]))
919 size
= round_up(4 * bmap
->n_div
/ 3 - 1);
922 elim_for
= isl_calloc_array(ctx
, int, bmap
->n_div
);
923 bits
= ffs(size
) - 1;
924 index
= isl_calloc_array(ctx
, int, size
);
925 if (!elim_for
|| !index
)
927 eq
= isl_blk_alloc(ctx
, 1+total
);
928 if (isl_blk_is_error(eq
))
931 isl_seq_clr(eq
.data
, 1+total
);
932 index
[isl_seq_get_hash_bits(bmap
->div
[k
], 2+total
, bits
)] = k
+ 1;
933 for (--k
; k
>= 0; --k
) {
936 if (isl_int_is_zero(bmap
->div
[k
][0]))
939 hash
= isl_seq_get_hash_bits(bmap
->div
[k
], 2+total
, bits
);
940 for (h
= hash
; index
[h
]; h
= (h
+1) % size
)
941 if (isl_seq_eq(bmap
->div
[k
],
942 bmap
->div
[index
[h
]-1], 2+total
))
951 for (l
= bmap
->n_div
- 1; l
>= 0; --l
) {
955 isl_int_set_si(eq
.data
[1+total_var
+k
], -1);
956 isl_int_set_si(eq
.data
[1+total_var
+l
], 1);
957 bmap
= eliminate_div(bmap
, eq
.data
, l
, 1);
960 isl_int_set_si(eq
.data
[1+total_var
+k
], 0);
961 isl_int_set_si(eq
.data
[1+total_var
+l
], 0);
964 isl_blk_free(ctx
, eq
);
971 static int n_pure_div_eq(struct isl_basic_map
*bmap
)
976 total
= isl_space_dim(bmap
->dim
, isl_dim_all
);
977 for (i
= 0, j
= bmap
->n_div
-1; i
< bmap
->n_eq
; ++i
) {
978 while (j
>= 0 && isl_int_is_zero(bmap
->eq
[i
][1 + total
+ j
]))
982 if (isl_seq_first_non_zero(bmap
->eq
[i
] + 1 + total
, j
) != -1)
988 /* Normalize divs that appear in equalities.
990 * In particular, we assume that bmap contains some equalities
995 * and we want to replace the set of e_i by a minimal set and
996 * such that the new e_i have a canonical representation in terms
998 * If any of the equalities involves more than one divs, then
999 * we currently simply bail out.
1001 * Let us first additionally assume that all equalities involve
1002 * a div. The equalities then express modulo constraints on the
1003 * remaining variables and we can use "parameter compression"
1004 * to find a minimal set of constraints. The result is a transformation
1006 * x = T(x') = x_0 + G x'
1008 * with G a lower-triangular matrix with all elements below the diagonal
1009 * non-negative and smaller than the diagonal element on the same row.
1010 * We first normalize x_0 by making the same property hold in the affine
1012 * The rows i of G with a 1 on the diagonal do not impose any modulo
1013 * constraint and simply express x_i = x'_i.
1014 * For each of the remaining rows i, we introduce a div and a corresponding
1015 * equality. In particular
1017 * g_ii e_j = x_i - g_i(x')
1019 * where each x'_k is replaced either by x_k (if g_kk = 1) or the
1020 * corresponding div (if g_kk != 1).
1022 * If there are any equalities not involving any div, then we
1023 * first apply a variable compression on the variables x:
1025 * x = C x'' x'' = C_2 x
1027 * and perform the above parameter compression on A C instead of on A.
1028 * The resulting compression is then of the form
1030 * x'' = T(x') = x_0 + G x'
1032 * and in constructing the new divs and the corresponding equalities,
1033 * we have to replace each x'', i.e., the x'_k with (g_kk = 1),
1034 * by the corresponding row from C_2.
1036 static struct isl_basic_map
*normalize_divs(
1037 struct isl_basic_map
*bmap
, int *progress
)
1044 struct isl_mat
*T
= NULL
;
1045 struct isl_mat
*C
= NULL
;
1046 struct isl_mat
*C2
= NULL
;
1049 int dropped
, needed
;
1054 if (bmap
->n_div
== 0)
1057 if (bmap
->n_eq
== 0)
1060 if (ISL_F_ISSET(bmap
, ISL_BASIC_MAP_NORMALIZED_DIVS
))
1063 total
= isl_space_dim(bmap
->dim
, isl_dim_all
);
1064 div_eq
= n_pure_div_eq(bmap
);
1068 if (div_eq
< bmap
->n_eq
) {
1069 B
= isl_mat_sub_alloc6(bmap
->ctx
, bmap
->eq
, div_eq
,
1070 bmap
->n_eq
- div_eq
, 0, 1 + total
);
1071 C
= isl_mat_variable_compression(B
, &C2
);
1074 if (C
->n_col
== 0) {
1075 bmap
= isl_basic_map_set_to_empty(bmap
);
1082 d
= isl_vec_alloc(bmap
->ctx
, div_eq
);
1085 for (i
= 0, j
= bmap
->n_div
-1; i
< div_eq
; ++i
) {
1086 while (j
>= 0 && isl_int_is_zero(bmap
->eq
[i
][1 + total
+ j
]))
1088 isl_int_set(d
->block
.data
[i
], bmap
->eq
[i
][1 + total
+ j
]);
1090 B
= isl_mat_sub_alloc6(bmap
->ctx
, bmap
->eq
, 0, div_eq
, 0, 1 + total
);
1093 B
= isl_mat_product(B
, C
);
1097 T
= isl_mat_parameter_compression(B
, d
);
1100 if (T
->n_col
== 0) {
1101 bmap
= isl_basic_map_set_to_empty(bmap
);
1107 for (i
= 0; i
< T
->n_row
- 1; ++i
) {
1108 isl_int_fdiv_q(v
, T
->row
[1 + i
][0], T
->row
[1 + i
][1 + i
]);
1109 if (isl_int_is_zero(v
))
1111 isl_mat_col_submul(T
, 0, v
, 1 + i
);
1114 pos
= isl_alloc_array(bmap
->ctx
, int, T
->n_row
);
1117 /* We have to be careful because dropping equalities may reorder them */
1119 for (j
= bmap
->n_div
- 1; j
>= 0; --j
) {
1120 for (i
= 0; i
< bmap
->n_eq
; ++i
)
1121 if (!isl_int_is_zero(bmap
->eq
[i
][1 + total
+ j
]))
1123 if (i
< bmap
->n_eq
) {
1124 bmap
= isl_basic_map_drop_div(bmap
, j
);
1125 isl_basic_map_drop_equality(bmap
, i
);
1131 for (i
= 1; i
< T
->n_row
; ++i
) {
1132 if (isl_int_is_one(T
->row
[i
][i
]))
1137 if (needed
> dropped
) {
1138 bmap
= isl_basic_map_extend_space(bmap
, isl_space_copy(bmap
->dim
),
1143 for (i
= 1; i
< T
->n_row
; ++i
) {
1144 if (isl_int_is_one(T
->row
[i
][i
]))
1146 k
= isl_basic_map_alloc_div(bmap
);
1147 pos
[i
] = 1 + total
+ k
;
1148 isl_seq_clr(bmap
->div
[k
] + 1, 1 + total
+ bmap
->n_div
);
1149 isl_int_set(bmap
->div
[k
][0], T
->row
[i
][i
]);
1151 isl_seq_cpy(bmap
->div
[k
] + 1, C2
->row
[i
], 1 + total
);
1153 isl_int_set_si(bmap
->div
[k
][1 + i
], 1);
1154 for (j
= 0; j
< i
; ++j
) {
1155 if (isl_int_is_zero(T
->row
[i
][j
]))
1157 if (pos
[j
] < T
->n_row
&& C2
)
1158 isl_seq_submul(bmap
->div
[k
] + 1, T
->row
[i
][j
],
1159 C2
->row
[pos
[j
]], 1 + total
);
1161 isl_int_neg(bmap
->div
[k
][1 + pos
[j
]],
1164 j
= isl_basic_map_alloc_equality(bmap
);
1165 isl_seq_neg(bmap
->eq
[j
], bmap
->div
[k
]+1, 1+total
+bmap
->n_div
);
1166 isl_int_set(bmap
->eq
[j
][pos
[i
]], bmap
->div
[k
][0]);
1175 ISL_F_SET(bmap
, ISL_BASIC_MAP_NORMALIZED_DIVS
);
1185 static struct isl_basic_map
*set_div_from_lower_bound(
1186 struct isl_basic_map
*bmap
, int div
, int ineq
)
1188 unsigned total
= 1 + isl_space_dim(bmap
->dim
, isl_dim_all
);
1190 isl_seq_neg(bmap
->div
[div
] + 1, bmap
->ineq
[ineq
], total
+ bmap
->n_div
);
1191 isl_int_set(bmap
->div
[div
][0], bmap
->ineq
[ineq
][total
+ div
]);
1192 isl_int_add(bmap
->div
[div
][1], bmap
->div
[div
][1], bmap
->div
[div
][0]);
1193 isl_int_sub_ui(bmap
->div
[div
][1], bmap
->div
[div
][1], 1);
1194 isl_int_set_si(bmap
->div
[div
][1 + total
+ div
], 0);
1199 /* Check whether it is ok to define a div based on an inequality.
1200 * To avoid the introduction of circular definitions of divs, we
1201 * do not allow such a definition if the resulting expression would refer to
1202 * any other undefined divs or if any known div is defined in
1203 * terms of the unknown div.
1205 static int ok_to_set_div_from_bound(struct isl_basic_map
*bmap
,
1209 unsigned total
= 1 + isl_space_dim(bmap
->dim
, isl_dim_all
);
1211 /* Not defined in terms of unknown divs */
1212 for (j
= 0; j
< bmap
->n_div
; ++j
) {
1215 if (isl_int_is_zero(bmap
->ineq
[ineq
][total
+ j
]))
1217 if (isl_int_is_zero(bmap
->div
[j
][0]))
1221 /* No other div defined in terms of this one => avoid loops */
1222 for (j
= 0; j
< bmap
->n_div
; ++j
) {
1225 if (isl_int_is_zero(bmap
->div
[j
][0]))
1227 if (!isl_int_is_zero(bmap
->div
[j
][1 + total
+ div
]))
1234 /* Would an expression for div "div" based on inequality "ineq" of "bmap"
1235 * be a better expression than the current one?
1237 * If we do not have any expression yet, then any expression would be better.
1238 * Otherwise we check if the last variable involved in the inequality
1239 * (disregarding the div that it would define) is in an earlier position
1240 * than the last variable involved in the current div expression.
1242 static int better_div_constraint(__isl_keep isl_basic_map
*bmap
,
1245 unsigned total
= 1 + isl_space_dim(bmap
->dim
, isl_dim_all
);
1249 if (isl_int_is_zero(bmap
->div
[div
][0]))
1252 if (isl_seq_last_non_zero(bmap
->ineq
[ineq
] + total
+ div
+ 1,
1253 bmap
->n_div
- (div
+ 1)) >= 0)
1256 last_ineq
= isl_seq_last_non_zero(bmap
->ineq
[ineq
], total
+ div
);
1257 last_div
= isl_seq_last_non_zero(bmap
->div
[div
] + 1,
1258 total
+ bmap
->n_div
);
1260 return last_ineq
< last_div
;
1263 /* Given two constraints "k" and "l" that are opposite to each other,
1264 * except for the constant term, check if we can use them
1265 * to obtain an expression for one of the hitherto unknown divs or
1266 * a "better" expression for a div for which we already have an expression.
1267 * "sum" is the sum of the constant terms of the constraints.
1268 * If this sum is strictly smaller than the coefficient of one
1269 * of the divs, then this pair can be used define the div.
1270 * To avoid the introduction of circular definitions of divs, we
1271 * do not use the pair if the resulting expression would refer to
1272 * any other undefined divs or if any known div is defined in
1273 * terms of the unknown div.
1275 static struct isl_basic_map
*check_for_div_constraints(
1276 struct isl_basic_map
*bmap
, int k
, int l
, isl_int sum
, int *progress
)
1279 unsigned total
= 1 + isl_space_dim(bmap
->dim
, isl_dim_all
);
1281 for (i
= 0; i
< bmap
->n_div
; ++i
) {
1282 if (isl_int_is_zero(bmap
->ineq
[k
][total
+ i
]))
1284 if (isl_int_abs_ge(sum
, bmap
->ineq
[k
][total
+ i
]))
1286 if (!better_div_constraint(bmap
, i
, k
))
1288 if (!ok_to_set_div_from_bound(bmap
, i
, k
))
1290 if (isl_int_is_pos(bmap
->ineq
[k
][total
+ i
]))
1291 bmap
= set_div_from_lower_bound(bmap
, i
, k
);
1293 bmap
= set_div_from_lower_bound(bmap
, i
, l
);
1301 __isl_give isl_basic_map
*isl_basic_map_remove_duplicate_constraints(
1302 __isl_take isl_basic_map
*bmap
, int *progress
, int detect_divs
)
1304 struct isl_constraint_index ci
;
1306 unsigned total
= isl_basic_map_total_dim(bmap
);
1309 if (!bmap
|| bmap
->n_ineq
<= 1)
1312 if (create_constraint_index(&ci
, bmap
) < 0)
1315 h
= isl_seq_get_hash_bits(bmap
->ineq
[0] + 1, total
, ci
.bits
);
1316 ci
.index
[h
] = &bmap
->ineq
[0];
1317 for (k
= 1; k
< bmap
->n_ineq
; ++k
) {
1318 h
= hash_index(&ci
, bmap
, k
);
1320 ci
.index
[h
] = &bmap
->ineq
[k
];
1325 l
= ci
.index
[h
] - &bmap
->ineq
[0];
1326 if (isl_int_lt(bmap
->ineq
[k
][0], bmap
->ineq
[l
][0]))
1327 swap_inequality(bmap
, k
, l
);
1328 isl_basic_map_drop_inequality(bmap
, k
);
1332 for (k
= 0; k
< bmap
->n_ineq
-1; ++k
) {
1333 isl_seq_neg(bmap
->ineq
[k
]+1, bmap
->ineq
[k
]+1, total
);
1334 h
= hash_index(&ci
, bmap
, k
);
1335 isl_seq_neg(bmap
->ineq
[k
]+1, bmap
->ineq
[k
]+1, total
);
1338 l
= ci
.index
[h
] - &bmap
->ineq
[0];
1339 isl_int_add(sum
, bmap
->ineq
[k
][0], bmap
->ineq
[l
][0]);
1340 if (isl_int_is_pos(sum
)) {
1342 bmap
= check_for_div_constraints(bmap
, k
, l
,
1346 if (isl_int_is_zero(sum
)) {
1347 /* We need to break out of the loop after these
1348 * changes since the contents of the hash
1349 * will no longer be valid.
1350 * Plus, we probably we want to regauss first.
1354 isl_basic_map_drop_inequality(bmap
, l
);
1355 isl_basic_map_inequality_to_equality(bmap
, k
);
1357 bmap
= isl_basic_map_set_to_empty(bmap
);
1362 constraint_index_free(&ci
);
1366 /* Detect all pairs of inequalities that form an equality.
1368 * isl_basic_map_remove_duplicate_constraints detects at most one such pair.
1369 * Call it repeatedly while it is making progress.
1371 __isl_give isl_basic_map
*isl_basic_map_detect_inequality_pairs(
1372 __isl_take isl_basic_map
*bmap
, int *progress
)
1378 bmap
= isl_basic_map_remove_duplicate_constraints(bmap
,
1380 if (progress
&& duplicate
)
1382 } while (duplicate
);
1387 /* Eliminate knowns divs from constraints where they appear with
1388 * a (positive or negative) unit coefficient.
1392 * floor(e/m) + f >= 0
1400 * -floor(e/m) + f >= 0
1404 * -e + m f + m - 1 >= 0
1406 * The first conversion is valid because floor(e/m) >= -f is equivalent
1407 * to e/m >= -f because -f is an integral expression.
1408 * The second conversion follows from the fact that
1410 * -floor(e/m) = ceil(-e/m) = floor((-e + m - 1)/m)
1413 * Note that one of the div constraints may have been eliminated
1414 * due to being redundant with respect to the constraint that is
1415 * being modified by this function. The modified constraint may
1416 * no longer imply this div constraint, so we add it back to make
1417 * sure we do not lose any information.
1419 * We skip integral divs, i.e., those with denominator 1, as we would
1420 * risk eliminating the div from the div constraints. We do not need
1421 * to handle those divs here anyway since the div constraints will turn
1422 * out to form an equality and this equality can then be use to eliminate
1423 * the div from all constraints.
1425 static __isl_give isl_basic_map
*eliminate_unit_divs(
1426 __isl_take isl_basic_map
*bmap
, int *progress
)
1435 ctx
= isl_basic_map_get_ctx(bmap
);
1436 total
= 1 + isl_space_dim(bmap
->dim
, isl_dim_all
);
1438 for (i
= 0; i
< bmap
->n_div
; ++i
) {
1439 if (isl_int_is_zero(bmap
->div
[i
][0]))
1441 if (isl_int_is_one(bmap
->div
[i
][0]))
1443 for (j
= 0; j
< bmap
->n_ineq
; ++j
) {
1446 if (!isl_int_is_one(bmap
->ineq
[j
][total
+ i
]) &&
1447 !isl_int_is_negone(bmap
->ineq
[j
][total
+ i
]))
1452 s
= isl_int_sgn(bmap
->ineq
[j
][total
+ i
]);
1453 isl_int_set_si(bmap
->ineq
[j
][total
+ i
], 0);
1455 isl_seq_combine(bmap
->ineq
[j
],
1456 ctx
->negone
, bmap
->div
[i
] + 1,
1457 bmap
->div
[i
][0], bmap
->ineq
[j
],
1458 total
+ bmap
->n_div
);
1460 isl_seq_combine(bmap
->ineq
[j
],
1461 ctx
->one
, bmap
->div
[i
] + 1,
1462 bmap
->div
[i
][0], bmap
->ineq
[j
],
1463 total
+ bmap
->n_div
);
1465 isl_int_add(bmap
->ineq
[j
][0],
1466 bmap
->ineq
[j
][0], bmap
->div
[i
][0]);
1467 isl_int_sub_ui(bmap
->ineq
[j
][0],
1468 bmap
->ineq
[j
][0], 1);
1471 bmap
= isl_basic_map_extend_constraints(bmap
, 0, 1);
1472 if (isl_basic_map_add_div_constraint(bmap
, i
, s
) < 0)
1473 return isl_basic_map_free(bmap
);
1480 struct isl_basic_map
*isl_basic_map_simplify(struct isl_basic_map
*bmap
)
1489 if (isl_basic_map_plain_is_empty(bmap
))
1491 bmap
= isl_basic_map_normalize_constraints(bmap
);
1492 bmap
= remove_independent_vars_from_divs(bmap
);
1493 bmap
= normalize_div_expressions(bmap
);
1494 bmap
= remove_duplicate_divs(bmap
, &progress
);
1495 bmap
= eliminate_unit_divs(bmap
, &progress
);
1496 bmap
= eliminate_divs_eq(bmap
, &progress
);
1497 bmap
= eliminate_divs_ineq(bmap
, &progress
);
1498 bmap
= isl_basic_map_gauss(bmap
, &progress
);
1499 /* requires equalities in normal form */
1500 bmap
= normalize_divs(bmap
, &progress
);
1501 bmap
= isl_basic_map_remove_duplicate_constraints(bmap
,
1503 if (bmap
&& progress
)
1504 ISL_F_CLR(bmap
, ISL_BASIC_MAP_REDUCED_COEFFICIENTS
);
1509 struct isl_basic_set
*isl_basic_set_simplify(struct isl_basic_set
*bset
)
1511 return (struct isl_basic_set
*)
1512 isl_basic_map_simplify((struct isl_basic_map
*)bset
);
1516 int isl_basic_map_is_div_constraint(__isl_keep isl_basic_map
*bmap
,
1517 isl_int
*constraint
, unsigned div
)
1524 pos
= 1 + isl_space_dim(bmap
->dim
, isl_dim_all
) + div
;
1526 if (isl_int_eq(constraint
[pos
], bmap
->div
[div
][0])) {
1528 isl_int_sub(bmap
->div
[div
][1],
1529 bmap
->div
[div
][1], bmap
->div
[div
][0]);
1530 isl_int_add_ui(bmap
->div
[div
][1], bmap
->div
[div
][1], 1);
1531 neg
= isl_seq_is_neg(constraint
, bmap
->div
[div
]+1, pos
);
1532 isl_int_sub_ui(bmap
->div
[div
][1], bmap
->div
[div
][1], 1);
1533 isl_int_add(bmap
->div
[div
][1],
1534 bmap
->div
[div
][1], bmap
->div
[div
][0]);
1537 if (isl_seq_first_non_zero(constraint
+pos
+1,
1538 bmap
->n_div
-div
-1) != -1)
1540 } else if (isl_int_abs_eq(constraint
[pos
], bmap
->div
[div
][0])) {
1541 if (!isl_seq_eq(constraint
, bmap
->div
[div
]+1, pos
))
1543 if (isl_seq_first_non_zero(constraint
+pos
+1,
1544 bmap
->n_div
-div
-1) != -1)
1552 int isl_basic_set_is_div_constraint(__isl_keep isl_basic_set
*bset
,
1553 isl_int
*constraint
, unsigned div
)
1555 return isl_basic_map_is_div_constraint(bset
, constraint
, div
);
1559 /* If the only constraints a div d=floor(f/m)
1560 * appears in are its two defining constraints
1563 * -(f - (m - 1)) + m d >= 0
1565 * then it can safely be removed.
1567 static int div_is_redundant(struct isl_basic_map
*bmap
, int div
)
1570 unsigned pos
= 1 + isl_space_dim(bmap
->dim
, isl_dim_all
) + div
;
1572 for (i
= 0; i
< bmap
->n_eq
; ++i
)
1573 if (!isl_int_is_zero(bmap
->eq
[i
][pos
]))
1576 for (i
= 0; i
< bmap
->n_ineq
; ++i
) {
1577 if (isl_int_is_zero(bmap
->ineq
[i
][pos
]))
1579 if (!isl_basic_map_is_div_constraint(bmap
, bmap
->ineq
[i
], div
))
1583 for (i
= 0; i
< bmap
->n_div
; ++i
) {
1584 if (isl_int_is_zero(bmap
->div
[i
][0]))
1586 if (!isl_int_is_zero(bmap
->div
[i
][1+pos
]))
1594 * Remove divs that don't occur in any of the constraints or other divs.
1595 * These can arise when dropping constraints from a basic map or
1596 * when the divs of a basic map have been temporarily aligned
1597 * with the divs of another basic map.
1599 static struct isl_basic_map
*remove_redundant_divs(struct isl_basic_map
*bmap
)
1606 for (i
= bmap
->n_div
-1; i
>= 0; --i
) {
1607 if (!div_is_redundant(bmap
, i
))
1609 bmap
= isl_basic_map_drop_div(bmap
, i
);
1614 /* Mark "bmap" as final, without checking for obviously redundant
1615 * integer divisions. This function should be used when "bmap"
1616 * is known not to involve any such integer divisions.
1618 __isl_give isl_basic_map
*isl_basic_map_mark_final(
1619 __isl_take isl_basic_map
*bmap
)
1623 ISL_F_SET(bmap
, ISL_BASIC_SET_FINAL
);
1627 /* Mark "bmap" as final, after removing obviously redundant integer divisions.
1629 struct isl_basic_map
*isl_basic_map_finalize(struct isl_basic_map
*bmap
)
1631 bmap
= remove_redundant_divs(bmap
);
1632 bmap
= isl_basic_map_mark_final(bmap
);
1636 struct isl_basic_set
*isl_basic_set_finalize(struct isl_basic_set
*bset
)
1638 return (struct isl_basic_set
*)
1639 isl_basic_map_finalize((struct isl_basic_map
*)bset
);
1642 struct isl_set
*isl_set_finalize(struct isl_set
*set
)
1648 for (i
= 0; i
< set
->n
; ++i
) {
1649 set
->p
[i
] = isl_basic_set_finalize(set
->p
[i
]);
1659 struct isl_map
*isl_map_finalize(struct isl_map
*map
)
1665 for (i
= 0; i
< map
->n
; ++i
) {
1666 map
->p
[i
] = isl_basic_map_finalize(map
->p
[i
]);
1670 ISL_F_CLR(map
, ISL_MAP_NORMALIZED
);
1678 /* Remove definition of any div that is defined in terms of the given variable.
1679 * The div itself is not removed. Functions such as
1680 * eliminate_divs_ineq depend on the other divs remaining in place.
1682 static struct isl_basic_map
*remove_dependent_vars(struct isl_basic_map
*bmap
,
1690 for (i
= 0; i
< bmap
->n_div
; ++i
) {
1691 if (isl_int_is_zero(bmap
->div
[i
][0]))
1693 if (isl_int_is_zero(bmap
->div
[i
][1+1+pos
]))
1695 bmap
= isl_basic_map_mark_div_unknown(bmap
, i
);
1702 /* Eliminate the specified variables from the constraints using
1703 * Fourier-Motzkin. The variables themselves are not removed.
1705 struct isl_basic_map
*isl_basic_map_eliminate_vars(
1706 struct isl_basic_map
*bmap
, unsigned pos
, unsigned n
)
1717 total
= isl_basic_map_total_dim(bmap
);
1719 bmap
= isl_basic_map_cow(bmap
);
1720 for (d
= pos
+ n
- 1; d
>= 0 && d
>= pos
; --d
)
1721 bmap
= remove_dependent_vars(bmap
, d
);
1725 for (d
= pos
+ n
- 1;
1726 d
>= 0 && d
>= total
- bmap
->n_div
&& d
>= pos
; --d
)
1727 isl_seq_clr(bmap
->div
[d
-(total
-bmap
->n_div
)], 2+total
);
1728 for (d
= pos
+ n
- 1; d
>= 0 && d
>= pos
; --d
) {
1729 int n_lower
, n_upper
;
1732 for (i
= 0; i
< bmap
->n_eq
; ++i
) {
1733 if (isl_int_is_zero(bmap
->eq
[i
][1+d
]))
1735 eliminate_var_using_equality(bmap
, d
, bmap
->eq
[i
], 0, NULL
);
1736 isl_basic_map_drop_equality(bmap
, i
);
1744 for (i
= 0; i
< bmap
->n_ineq
; ++i
) {
1745 if (isl_int_is_pos(bmap
->ineq
[i
][1+d
]))
1747 else if (isl_int_is_neg(bmap
->ineq
[i
][1+d
]))
1750 bmap
= isl_basic_map_extend_constraints(bmap
,
1751 0, n_lower
* n_upper
);
1754 for (i
= bmap
->n_ineq
- 1; i
>= 0; --i
) {
1756 if (isl_int_is_zero(bmap
->ineq
[i
][1+d
]))
1759 for (j
= 0; j
< i
; ++j
) {
1760 if (isl_int_is_zero(bmap
->ineq
[j
][1+d
]))
1763 if (isl_int_sgn(bmap
->ineq
[i
][1+d
]) ==
1764 isl_int_sgn(bmap
->ineq
[j
][1+d
]))
1766 k
= isl_basic_map_alloc_inequality(bmap
);
1769 isl_seq_cpy(bmap
->ineq
[k
], bmap
->ineq
[i
],
1771 isl_seq_elim(bmap
->ineq
[k
], bmap
->ineq
[j
],
1772 1+d
, 1+total
, NULL
);
1774 isl_basic_map_drop_inequality(bmap
, i
);
1777 if (n_lower
> 0 && n_upper
> 0) {
1778 bmap
= isl_basic_map_normalize_constraints(bmap
);
1779 bmap
= isl_basic_map_remove_duplicate_constraints(bmap
,
1781 bmap
= isl_basic_map_gauss(bmap
, NULL
);
1782 bmap
= isl_basic_map_remove_redundancies(bmap
);
1786 if (ISL_F_ISSET(bmap
, ISL_BASIC_MAP_EMPTY
))
1790 ISL_F_CLR(bmap
, ISL_BASIC_MAP_NORMALIZED
);
1792 bmap
= isl_basic_map_gauss(bmap
, NULL
);
1795 isl_basic_map_free(bmap
);
1799 struct isl_basic_set
*isl_basic_set_eliminate_vars(
1800 struct isl_basic_set
*bset
, unsigned pos
, unsigned n
)
1802 return (struct isl_basic_set
*)isl_basic_map_eliminate_vars(
1803 (struct isl_basic_map
*)bset
, pos
, n
);
1806 /* Eliminate the specified n dimensions starting at first from the
1807 * constraints, without removing the dimensions from the space.
1808 * If the set is rational, the dimensions are eliminated using Fourier-Motzkin.
1809 * Otherwise, they are projected out and the original space is restored.
1811 __isl_give isl_basic_map
*isl_basic_map_eliminate(
1812 __isl_take isl_basic_map
*bmap
,
1813 enum isl_dim_type type
, unsigned first
, unsigned n
)
1822 if (first
+ n
> isl_basic_map_dim(bmap
, type
) || first
+ n
< first
)
1823 isl_die(bmap
->ctx
, isl_error_invalid
,
1824 "index out of bounds", goto error
);
1826 if (ISL_F_ISSET(bmap
, ISL_BASIC_MAP_RATIONAL
)) {
1827 first
+= isl_basic_map_offset(bmap
, type
) - 1;
1828 bmap
= isl_basic_map_eliminate_vars(bmap
, first
, n
);
1829 return isl_basic_map_finalize(bmap
);
1832 space
= isl_basic_map_get_space(bmap
);
1833 bmap
= isl_basic_map_project_out(bmap
, type
, first
, n
);
1834 bmap
= isl_basic_map_insert_dims(bmap
, type
, first
, n
);
1835 bmap
= isl_basic_map_reset_space(bmap
, space
);
1838 isl_basic_map_free(bmap
);
1842 __isl_give isl_basic_set
*isl_basic_set_eliminate(
1843 __isl_take isl_basic_set
*bset
,
1844 enum isl_dim_type type
, unsigned first
, unsigned n
)
1846 return isl_basic_map_eliminate(bset
, type
, first
, n
);
1849 /* Remove all constraints from "bmap" that reference any unknown local
1850 * variables (directly or indirectly).
1852 * Dropping all constraints on a local variable will make it redundant,
1853 * so it will get removed implicitly by
1854 * isl_basic_map_drop_constraints_involving_dims. Some other local
1855 * variables may also end up becoming redundant if they only appear
1856 * in constraints together with the unknown local variable.
1857 * Therefore, start over after calling
1858 * isl_basic_map_drop_constraints_involving_dims.
1860 __isl_give isl_basic_map
*isl_basic_map_drop_constraint_involving_unknown_divs(
1861 __isl_take isl_basic_map
*bmap
)
1864 int i
, n_div
, o_div
;
1866 known
= isl_basic_map_divs_known(bmap
);
1868 return isl_basic_map_free(bmap
);
1872 n_div
= isl_basic_map_dim(bmap
, isl_dim_div
);
1873 o_div
= isl_basic_map_offset(bmap
, isl_dim_div
) - 1;
1875 for (i
= 0; i
< n_div
; ++i
) {
1876 known
= isl_basic_map_div_is_known(bmap
, i
);
1878 return isl_basic_map_free(bmap
);
1881 bmap
= remove_dependent_vars(bmap
, o_div
+ i
);
1882 bmap
= isl_basic_map_drop_constraints_involving_dims(bmap
,
1886 n_div
= isl_basic_map_dim(bmap
, isl_dim_div
);
1893 /* Remove all constraints from "map" that reference any unknown local
1894 * variables (directly or indirectly).
1896 * Since constraints may get dropped from the basic maps,
1897 * they may no longer be disjoint from each other.
1899 __isl_give isl_map
*isl_map_drop_constraint_involving_unknown_divs(
1900 __isl_take isl_map
*map
)
1905 known
= isl_map_divs_known(map
);
1907 return isl_map_free(map
);
1911 map
= isl_map_cow(map
);
1915 for (i
= 0; i
< map
->n
; ++i
) {
1917 isl_basic_map_drop_constraint_involving_unknown_divs(
1920 return isl_map_free(map
);
1924 ISL_F_CLR(map
, ISL_MAP_DISJOINT
);
1929 /* Don't assume equalities are in order, because align_divs
1930 * may have changed the order of the divs.
1932 static void compute_elimination_index(struct isl_basic_map
*bmap
, int *elim
)
1937 total
= isl_space_dim(bmap
->dim
, isl_dim_all
);
1938 for (d
= 0; d
< total
; ++d
)
1940 for (i
= 0; i
< bmap
->n_eq
; ++i
) {
1941 for (d
= total
- 1; d
>= 0; --d
) {
1942 if (isl_int_is_zero(bmap
->eq
[i
][1+d
]))
1950 static void set_compute_elimination_index(struct isl_basic_set
*bset
, int *elim
)
1952 compute_elimination_index((struct isl_basic_map
*)bset
, elim
);
1955 static int reduced_using_equalities(isl_int
*dst
, isl_int
*src
,
1956 struct isl_basic_map
*bmap
, int *elim
)
1962 total
= isl_space_dim(bmap
->dim
, isl_dim_all
);
1963 for (d
= total
- 1; d
>= 0; --d
) {
1964 if (isl_int_is_zero(src
[1+d
]))
1969 isl_seq_cpy(dst
, src
, 1 + total
);
1972 isl_seq_elim(dst
, bmap
->eq
[elim
[d
]], 1 + d
, 1 + total
, NULL
);
1977 static int set_reduced_using_equalities(isl_int
*dst
, isl_int
*src
,
1978 struct isl_basic_set
*bset
, int *elim
)
1980 return reduced_using_equalities(dst
, src
,
1981 (struct isl_basic_map
*)bset
, elim
);
1984 static struct isl_basic_set
*isl_basic_set_reduce_using_equalities(
1985 struct isl_basic_set
*bset
, struct isl_basic_set
*context
)
1990 if (!bset
|| !context
)
1993 if (context
->n_eq
== 0) {
1994 isl_basic_set_free(context
);
1998 bset
= isl_basic_set_cow(bset
);
2002 elim
= isl_alloc_array(bset
->ctx
, int, isl_basic_set_n_dim(bset
));
2005 set_compute_elimination_index(context
, elim
);
2006 for (i
= 0; i
< bset
->n_eq
; ++i
)
2007 set_reduced_using_equalities(bset
->eq
[i
], bset
->eq
[i
],
2009 for (i
= 0; i
< bset
->n_ineq
; ++i
)
2010 set_reduced_using_equalities(bset
->ineq
[i
], bset
->ineq
[i
],
2012 isl_basic_set_free(context
);
2014 bset
= isl_basic_set_simplify(bset
);
2015 bset
= isl_basic_set_finalize(bset
);
2018 isl_basic_set_free(bset
);
2019 isl_basic_set_free(context
);
2023 /* For each inequality in "ineq" that is a shifted (more relaxed)
2024 * copy of an inequality in "context", mark the corresponding entry
2026 * If an inequality only has a non-negative constant term, then
2029 static isl_stat
mark_shifted_constraints(__isl_keep isl_mat
*ineq
,
2030 __isl_keep isl_basic_set
*context
, int *row
)
2032 struct isl_constraint_index ci
;
2037 if (!ineq
|| !context
)
2038 return isl_stat_error
;
2039 if (context
->n_ineq
== 0)
2041 if (setup_constraint_index(&ci
, context
) < 0)
2042 return isl_stat_error
;
2044 n_ineq
= isl_mat_rows(ineq
);
2045 total
= isl_mat_cols(ineq
) - 1;
2046 for (k
= 0; k
< n_ineq
; ++k
) {
2050 l
= isl_seq_first_non_zero(ineq
->row
[k
] + 1, total
);
2051 if (l
< 0 && isl_int_is_nonneg(ineq
->row
[k
][0])) {
2055 redundant
= constraint_index_is_redundant(&ci
, ineq
->row
[k
]);
2062 constraint_index_free(&ci
);
2065 constraint_index_free(&ci
);
2066 return isl_stat_error
;
2069 static struct isl_basic_set
*remove_shifted_constraints(
2070 struct isl_basic_set
*bset
, struct isl_basic_set
*context
)
2072 struct isl_constraint_index ci
;
2075 if (!bset
|| !context
)
2078 if (context
->n_ineq
== 0)
2080 if (setup_constraint_index(&ci
, context
) < 0)
2083 for (k
= 0; k
< bset
->n_ineq
; ++k
) {
2086 redundant
= constraint_index_is_redundant(&ci
, bset
->ineq
[k
]);
2091 bset
= isl_basic_set_cow(bset
);
2094 isl_basic_set_drop_inequality(bset
, k
);
2097 constraint_index_free(&ci
);
2100 constraint_index_free(&ci
);
2104 /* Remove constraints from "bmap" that are identical to constraints
2105 * in "context" or that are more relaxed (greater constant term).
2107 * We perform the test for shifted copies on the pure constraints
2108 * in remove_shifted_constraints.
2110 static __isl_give isl_basic_map
*isl_basic_map_remove_shifted_constraints(
2111 __isl_take isl_basic_map
*bmap
, __isl_take isl_basic_map
*context
)
2113 isl_basic_set
*bset
, *bset_context
;
2115 if (!bmap
|| !context
)
2118 if (bmap
->n_ineq
== 0 || context
->n_ineq
== 0) {
2119 isl_basic_map_free(context
);
2123 context
= isl_basic_map_align_divs(context
, bmap
);
2124 bmap
= isl_basic_map_align_divs(bmap
, context
);
2126 bset
= isl_basic_map_underlying_set(isl_basic_map_copy(bmap
));
2127 bset_context
= isl_basic_map_underlying_set(context
);
2128 bset
= remove_shifted_constraints(bset
, bset_context
);
2129 isl_basic_set_free(bset_context
);
2131 bmap
= isl_basic_map_overlying_set(bset
, bmap
);
2135 isl_basic_map_free(bmap
);
2136 isl_basic_map_free(context
);
2140 /* Does the (linear part of a) constraint "c" involve any of the "len"
2141 * "relevant" dimensions?
2143 static int is_related(isl_int
*c
, int len
, int *relevant
)
2147 for (i
= 0; i
< len
; ++i
) {
2150 if (!isl_int_is_zero(c
[i
]))
2157 /* Drop constraints from "bmap" that do not involve any of
2158 * the dimensions marked "relevant".
2160 static __isl_give isl_basic_map
*drop_unrelated_constraints(
2161 __isl_take isl_basic_map
*bmap
, int *relevant
)
2165 dim
= isl_basic_map_dim(bmap
, isl_dim_all
);
2166 for (i
= 0; i
< dim
; ++i
)
2172 for (i
= bmap
->n_eq
- 1; i
>= 0; --i
)
2173 if (!is_related(bmap
->eq
[i
] + 1, dim
, relevant
)) {
2174 bmap
= isl_basic_map_cow(bmap
);
2175 if (isl_basic_map_drop_equality(bmap
, i
) < 0)
2176 return isl_basic_map_free(bmap
);
2179 for (i
= bmap
->n_ineq
- 1; i
>= 0; --i
)
2180 if (!is_related(bmap
->ineq
[i
] + 1, dim
, relevant
)) {
2181 bmap
= isl_basic_map_cow(bmap
);
2182 if (isl_basic_map_drop_inequality(bmap
, i
) < 0)
2183 return isl_basic_map_free(bmap
);
2189 /* Update the groups in "group" based on the (linear part of a) constraint "c".
2191 * In particular, for any variable involved in the constraint,
2192 * find the actual group id from before and replace the group
2193 * of the corresponding variable by the minimal group of all
2194 * the variables involved in the constraint considered so far
2195 * (if this minimum is smaller) or replace the minimum by this group
2196 * (if the minimum is larger).
2198 * At the end, all the variables in "c" will (indirectly) point
2199 * to the minimal of the groups that they referred to originally.
2201 static void update_groups(int dim
, int *group
, isl_int
*c
)
2206 for (j
= 0; j
< dim
; ++j
) {
2207 if (isl_int_is_zero(c
[j
]))
2209 while (group
[j
] >= 0 && group
[group
[j
]] != group
[j
])
2210 group
[j
] = group
[group
[j
]];
2211 if (group
[j
] == min
)
2213 if (group
[j
] < min
) {
2214 if (min
>= 0 && min
< dim
)
2215 group
[min
] = group
[j
];
2218 group
[group
[j
]] = min
;
2222 /* Allocate an array of groups of variables, one for each variable
2223 * in "context", initialized to zero.
2225 static int *alloc_groups(__isl_keep isl_basic_set
*context
)
2230 dim
= isl_basic_set_dim(context
, isl_dim_set
);
2231 ctx
= isl_basic_set_get_ctx(context
);
2232 return isl_calloc_array(ctx
, int, dim
);
2235 /* Drop constraints from "bmap" that only involve variables that are
2236 * not related to any of the variables marked with a "-1" in "group".
2238 * We construct groups of variables that collect variables that
2239 * (indirectly) appear in some common constraint of "bmap".
2240 * Each group is identified by the first variable in the group,
2241 * except for the special group of variables that was already identified
2242 * in the input as -1 (or are related to those variables).
2243 * If group[i] is equal to i (or -1), then the group of i is i (or -1),
2244 * otherwise the group of i is the group of group[i].
2246 * We first initialize groups for the remaining variables.
2247 * Then we iterate over the constraints of "bmap" and update the
2248 * group of the variables in the constraint by the smallest group.
2249 * Finally, we resolve indirect references to groups by running over
2252 * After computing the groups, we drop constraints that do not involve
2253 * any variables in the -1 group.
2255 __isl_give isl_basic_map
*isl_basic_map_drop_unrelated_constraints(
2256 __isl_take isl_basic_map
*bmap
, __isl_take
int *group
)
2265 dim
= isl_basic_map_dim(bmap
, isl_dim_all
);
2268 for (i
= 0; i
< dim
; ++i
)
2270 last
= group
[i
] = i
;
2276 for (i
= 0; i
< bmap
->n_eq
; ++i
)
2277 update_groups(dim
, group
, bmap
->eq
[i
] + 1);
2278 for (i
= 0; i
< bmap
->n_ineq
; ++i
)
2279 update_groups(dim
, group
, bmap
->ineq
[i
] + 1);
2281 for (i
= 0; i
< dim
; ++i
)
2283 group
[i
] = group
[group
[i
]];
2285 for (i
= 0; i
< dim
; ++i
)
2286 group
[i
] = group
[i
] == -1;
2288 bmap
= drop_unrelated_constraints(bmap
, group
);
2294 /* Drop constraints from "context" that are irrelevant for computing
2295 * the gist of "bset".
2297 * In particular, drop constraints in variables that are not related
2298 * to any of the variables involved in the constraints of "bset"
2299 * in the sense that there is no sequence of constraints that connects them.
2301 * We first mark all variables that appear in "bset" as belonging
2302 * to a "-1" group and then continue with group_and_drop_irrelevant_constraints.
2304 static __isl_give isl_basic_set
*drop_irrelevant_constraints(
2305 __isl_take isl_basic_set
*context
, __isl_keep isl_basic_set
*bset
)
2311 if (!context
|| !bset
)
2312 return isl_basic_set_free(context
);
2314 group
= alloc_groups(context
);
2317 return isl_basic_set_free(context
);
2319 dim
= isl_basic_set_dim(bset
, isl_dim_set
);
2320 for (i
= 0; i
< dim
; ++i
) {
2321 for (j
= 0; j
< bset
->n_eq
; ++j
)
2322 if (!isl_int_is_zero(bset
->eq
[j
][1 + i
]))
2324 if (j
< bset
->n_eq
) {
2328 for (j
= 0; j
< bset
->n_ineq
; ++j
)
2329 if (!isl_int_is_zero(bset
->ineq
[j
][1 + i
]))
2331 if (j
< bset
->n_ineq
)
2335 return isl_basic_map_drop_unrelated_constraints(context
, group
);
2338 /* Drop constraints from "context" that are irrelevant for computing
2339 * the gist of the inequalities "ineq".
2340 * Inequalities in "ineq" for which the corresponding element of row
2341 * is set to -1 have already been marked for removal and should be ignored.
2343 * In particular, drop constraints in variables that are not related
2344 * to any of the variables involved in "ineq"
2345 * in the sense that there is no sequence of constraints that connects them.
2347 * We first mark all variables that appear in "bset" as belonging
2348 * to a "-1" group and then continue with group_and_drop_irrelevant_constraints.
2350 static __isl_give isl_basic_set
*drop_irrelevant_constraints_marked(
2351 __isl_take isl_basic_set
*context
, __isl_keep isl_mat
*ineq
, int *row
)
2357 if (!context
|| !ineq
)
2358 return isl_basic_set_free(context
);
2360 group
= alloc_groups(context
);
2363 return isl_basic_set_free(context
);
2365 dim
= isl_basic_set_dim(context
, isl_dim_set
);
2366 n
= isl_mat_rows(ineq
);
2367 for (i
= 0; i
< dim
; ++i
) {
2368 for (j
= 0; j
< n
; ++j
) {
2371 if (!isl_int_is_zero(ineq
->row
[j
][1 + i
]))
2378 return isl_basic_map_drop_unrelated_constraints(context
, group
);
2381 /* Do all "n" entries of "row" contain a negative value?
2383 static int all_neg(int *row
, int n
)
2387 for (i
= 0; i
< n
; ++i
)
2394 /* Update the inequalities in "bset" based on the information in "row"
2397 * In particular, the array "row" contains either -1, meaning that
2398 * the corresponding inequality of "bset" is redundant, or the index
2399 * of an inequality in "tab".
2401 * If the row entry is -1, then drop the inequality.
2402 * Otherwise, if the constraint is marked redundant in the tableau,
2403 * then drop the inequality. Similarly, if it is marked as an equality
2404 * in the tableau, then turn the inequality into an equality and
2405 * perform Gaussian elimination.
2407 static __isl_give isl_basic_set
*update_ineq(__isl_take isl_basic_set
*bset
,
2408 __isl_keep
int *row
, struct isl_tab
*tab
)
2413 int found_equality
= 0;
2417 if (tab
&& tab
->empty
)
2418 return isl_basic_set_set_to_empty(bset
);
2420 n_ineq
= bset
->n_ineq
;
2421 for (i
= n_ineq
- 1; i
>= 0; --i
) {
2423 if (isl_basic_set_drop_inequality(bset
, i
) < 0)
2424 return isl_basic_set_free(bset
);
2430 if (isl_tab_is_equality(tab
, n_eq
+ row
[i
])) {
2431 isl_basic_map_inequality_to_equality(bset
, i
);
2433 } else if (isl_tab_is_redundant(tab
, n_eq
+ row
[i
])) {
2434 if (isl_basic_set_drop_inequality(bset
, i
) < 0)
2435 return isl_basic_set_free(bset
);
2440 bset
= isl_basic_set_gauss(bset
, NULL
);
2441 bset
= isl_basic_set_finalize(bset
);
2445 /* Update the inequalities in "bset" based on the information in "row"
2446 * and "tab" and free all arguments (other than "bset").
2448 static __isl_give isl_basic_set
*update_ineq_free(
2449 __isl_take isl_basic_set
*bset
, __isl_take isl_mat
*ineq
,
2450 __isl_take isl_basic_set
*context
, __isl_take
int *row
,
2451 struct isl_tab
*tab
)
2454 isl_basic_set_free(context
);
2456 bset
= update_ineq(bset
, row
, tab
);
2463 /* Remove all information from bset that is redundant in the context
2465 * "ineq" contains the (possibly transformed) inequalities of "bset",
2466 * in the same order.
2467 * The (explicit) equalities of "bset" are assumed to have been taken
2468 * into account by the transformation such that only the inequalities
2470 * "context" is assumed not to be empty.
2472 * "row" keeps track of the constraint index of a "bset" inequality in "tab".
2473 * A value of -1 means that the inequality is obviously redundant and may
2474 * not even appear in "tab".
2476 * We first mark the inequalities of "bset"
2477 * that are obviously redundant with respect to some inequality in "context".
2478 * Then we remove those constraints from "context" that have become
2479 * irrelevant for computing the gist of "bset".
2480 * Note that this removal of constraints cannot be replaced by
2481 * a factorization because factors in "bset" may still be connected
2482 * to each other through constraints in "context".
2484 * If there are any inequalities left, we construct a tableau for
2485 * the context and then add the inequalities of "bset".
2486 * Before adding these inequalities, we freeze all constraints such that
2487 * they won't be considered redundant in terms of the constraints of "bset".
2488 * Then we detect all redundant constraints (among the
2489 * constraints that weren't frozen), first by checking for redundancy in the
2490 * the tableau and then by checking if replacing a constraint by its negation
2491 * would lead to an empty set. This last step is fairly expensive
2492 * and could be optimized by more reuse of the tableau.
2493 * Finally, we update bset according to the results.
2495 static __isl_give isl_basic_set
*uset_gist_full(__isl_take isl_basic_set
*bset
,
2496 __isl_take isl_mat
*ineq
, __isl_take isl_basic_set
*context
)
2501 isl_basic_set
*combined
= NULL
;
2502 struct isl_tab
*tab
= NULL
;
2503 unsigned n_eq
, context_ineq
;
2506 if (!bset
|| !ineq
|| !context
)
2509 if (bset
->n_ineq
== 0 || isl_basic_set_plain_is_universe(context
)) {
2510 isl_basic_set_free(context
);
2515 ctx
= isl_basic_set_get_ctx(context
);
2516 row
= isl_calloc_array(ctx
, int, bset
->n_ineq
);
2520 if (mark_shifted_constraints(ineq
, context
, row
) < 0)
2522 if (all_neg(row
, bset
->n_ineq
))
2523 return update_ineq_free(bset
, ineq
, context
, row
, NULL
);
2525 context
= drop_irrelevant_constraints_marked(context
, ineq
, row
);
2528 if (isl_basic_set_plain_is_universe(context
))
2529 return update_ineq_free(bset
, ineq
, context
, row
, NULL
);
2531 n_eq
= context
->n_eq
;
2532 context_ineq
= context
->n_ineq
;
2533 combined
= isl_basic_set_cow(isl_basic_set_copy(context
));
2534 combined
= isl_basic_set_extend_constraints(combined
, 0, bset
->n_ineq
);
2535 tab
= isl_tab_from_basic_set(combined
, 0);
2536 for (i
= 0; i
< context_ineq
; ++i
)
2537 if (isl_tab_freeze_constraint(tab
, n_eq
+ i
) < 0)
2539 if (isl_tab_extend_cons(tab
, bset
->n_ineq
) < 0)
2542 for (i
= 0; i
< bset
->n_ineq
; ++i
) {
2545 combined
= isl_basic_set_add_ineq(combined
, ineq
->row
[i
]);
2546 if (isl_tab_add_ineq(tab
, ineq
->row
[i
]) < 0)
2550 if (isl_tab_detect_implicit_equalities(tab
) < 0)
2552 if (isl_tab_detect_redundant(tab
) < 0)
2554 total
= isl_basic_set_total_dim(bset
);
2555 for (i
= bset
->n_ineq
- 1; i
>= 0; --i
) {
2556 isl_basic_set
*test
;
2562 if (tab
->con
[n_eq
+ r
].is_redundant
)
2564 test
= isl_basic_set_dup(combined
);
2565 if (isl_inequality_negate(test
, r
) < 0)
2566 test
= isl_basic_set_free(test
);
2567 test
= isl_basic_set_update_from_tab(test
, tab
);
2568 is_empty
= isl_basic_set_is_empty(test
);
2569 isl_basic_set_free(test
);
2573 tab
->con
[n_eq
+ r
].is_redundant
= 1;
2575 bset
= update_ineq_free(bset
, ineq
, context
, row
, tab
);
2577 ISL_F_SET(bset
, ISL_BASIC_SET_NO_IMPLICIT
);
2578 ISL_F_SET(bset
, ISL_BASIC_SET_NO_REDUNDANT
);
2581 isl_basic_set_free(combined
);
2587 isl_basic_set_free(combined
);
2588 isl_basic_set_free(context
);
2589 isl_basic_set_free(bset
);
2593 /* Extract the inequalities of "bset" as an isl_mat.
2595 static __isl_give isl_mat
*extract_ineq(__isl_keep isl_basic_set
*bset
)
2604 ctx
= isl_basic_set_get_ctx(bset
);
2605 total
= isl_basic_set_total_dim(bset
);
2606 ineq
= isl_mat_sub_alloc6(ctx
, bset
->ineq
, 0, bset
->n_ineq
,
2612 /* Remove all information from "bset" that is redundant in the context
2613 * of "context", for the case where both "bset" and "context" are
2616 static __isl_give isl_basic_set
*uset_gist_uncompressed(
2617 __isl_take isl_basic_set
*bset
, __isl_take isl_basic_set
*context
)
2621 ineq
= extract_ineq(bset
);
2622 return uset_gist_full(bset
, ineq
, context
);
2625 /* Remove all information from "bset" that is redundant in the context
2626 * of "context", for the case where the combined equalities of
2627 * "bset" and "context" allow for a compression that can be obtained
2628 * by preapplication of "T".
2630 * "bset" itself is not transformed by "T". Instead, the inequalities
2631 * are extracted from "bset" and those are transformed by "T".
2632 * uset_gist_full then determines which of the transformed inequalities
2633 * are redundant with respect to the transformed "context" and removes
2634 * the corresponding inequalities from "bset".
2636 * After preapplying "T" to the inequalities, any common factor is
2637 * removed from the coefficients. If this results in a tightening
2638 * of the constant term, then the same tightening is applied to
2639 * the corresponding untransformed inequality in "bset".
2640 * That is, if after plugging in T, a constraint f(x) >= 0 is of the form
2644 * with 0 <= r < g, then it is equivalent to
2648 * This means that f(x) >= 0 is equivalent to f(x) - r >= 0 in the affine
2649 * subspace compressed by T since the latter would be transformed to
2653 static __isl_give isl_basic_set
*uset_gist_compressed(
2654 __isl_take isl_basic_set
*bset
, __isl_take isl_basic_set
*context
,
2655 __isl_take isl_mat
*T
)
2659 int i
, n_row
, n_col
;
2662 ineq
= extract_ineq(bset
);
2663 ineq
= isl_mat_product(ineq
, isl_mat_copy(T
));
2664 context
= isl_basic_set_preimage(context
, T
);
2666 if (!ineq
|| !context
)
2668 if (isl_basic_set_plain_is_empty(context
)) {
2670 isl_basic_set_free(context
);
2671 return isl_basic_set_set_to_empty(bset
);
2674 ctx
= isl_mat_get_ctx(ineq
);
2675 n_row
= isl_mat_rows(ineq
);
2676 n_col
= isl_mat_cols(ineq
);
2678 for (i
= 0; i
< n_row
; ++i
) {
2679 isl_seq_gcd(ineq
->row
[i
] + 1, n_col
- 1, &ctx
->normalize_gcd
);
2680 if (isl_int_is_zero(ctx
->normalize_gcd
))
2682 if (isl_int_is_one(ctx
->normalize_gcd
))
2684 isl_seq_scale_down(ineq
->row
[i
] + 1, ineq
->row
[i
] + 1,
2685 ctx
->normalize_gcd
, n_col
- 1);
2686 isl_int_fdiv_r(rem
, ineq
->row
[i
][0], ctx
->normalize_gcd
);
2687 isl_int_fdiv_q(ineq
->row
[i
][0],
2688 ineq
->row
[i
][0], ctx
->normalize_gcd
);
2689 if (isl_int_is_zero(rem
))
2691 bset
= isl_basic_set_cow(bset
);
2694 isl_int_sub(bset
->ineq
[i
][0], bset
->ineq
[i
][0], rem
);
2698 return uset_gist_full(bset
, ineq
, context
);
2701 isl_basic_set_free(context
);
2702 isl_basic_set_free(bset
);
2706 /* Project "bset" onto the variables that are involved in "template".
2708 static __isl_give isl_basic_set
*project_onto_involved(
2709 __isl_take isl_basic_set
*bset
, __isl_keep isl_basic_set
*template)
2713 if (!bset
|| !template)
2714 return isl_basic_set_free(bset
);
2716 n
= isl_basic_set_dim(template, isl_dim_set
);
2718 for (i
= 0; i
< n
; ++i
) {
2721 involved
= isl_basic_set_involves_dims(template,
2724 return isl_basic_set_free(bset
);
2727 bset
= isl_basic_set_eliminate_vars(bset
, i
, 1);
2733 /* Remove all information from bset that is redundant in the context
2734 * of context. In particular, equalities that are linear combinations
2735 * of those in context are removed. Then the inequalities that are
2736 * redundant in the context of the equalities and inequalities of
2737 * context are removed.
2739 * First of all, we drop those constraints from "context"
2740 * that are irrelevant for computing the gist of "bset".
2741 * Alternatively, we could factorize the intersection of "context" and "bset".
2743 * We first compute the intersection of the integer affine hulls
2744 * of "bset" and "context",
2745 * compute the gist inside this intersection and then reduce
2746 * the constraints with respect to the equalities of the context
2747 * that only involve variables already involved in the input.
2749 * If two constraints are mutually redundant, then uset_gist_full
2750 * will remove the second of those constraints. We therefore first
2751 * sort the constraints so that constraints not involving existentially
2752 * quantified variables are given precedence over those that do.
2753 * We have to perform this sorting before the variable compression,
2754 * because that may effect the order of the variables.
2756 static __isl_give isl_basic_set
*uset_gist(__isl_take isl_basic_set
*bset
,
2757 __isl_take isl_basic_set
*context
)
2762 isl_basic_set
*aff_context
;
2765 if (!bset
|| !context
)
2768 context
= drop_irrelevant_constraints(context
, bset
);
2770 bset
= isl_basic_set_detect_equalities(bset
);
2771 aff
= isl_basic_set_copy(bset
);
2772 aff
= isl_basic_set_plain_affine_hull(aff
);
2773 context
= isl_basic_set_detect_equalities(context
);
2774 aff_context
= isl_basic_set_copy(context
);
2775 aff_context
= isl_basic_set_plain_affine_hull(aff_context
);
2776 aff
= isl_basic_set_intersect(aff
, aff_context
);
2779 if (isl_basic_set_plain_is_empty(aff
)) {
2780 isl_basic_set_free(bset
);
2781 isl_basic_set_free(context
);
2784 bset
= isl_basic_set_sort_constraints(bset
);
2785 if (aff
->n_eq
== 0) {
2786 isl_basic_set_free(aff
);
2787 return uset_gist_uncompressed(bset
, context
);
2789 total
= isl_basic_set_total_dim(bset
);
2790 eq
= isl_mat_sub_alloc6(bset
->ctx
, aff
->eq
, 0, aff
->n_eq
, 0, 1 + total
);
2791 eq
= isl_mat_cow(eq
);
2792 T
= isl_mat_variable_compression(eq
, NULL
);
2793 isl_basic_set_free(aff
);
2794 if (T
&& T
->n_col
== 0) {
2796 isl_basic_set_free(context
);
2797 return isl_basic_set_set_to_empty(bset
);
2800 aff_context
= isl_basic_set_affine_hull(isl_basic_set_copy(context
));
2801 aff_context
= project_onto_involved(aff_context
, bset
);
2803 bset
= uset_gist_compressed(bset
, context
, T
);
2804 bset
= isl_basic_set_reduce_using_equalities(bset
, aff_context
);
2807 ISL_F_SET(bset
, ISL_BASIC_SET_NO_IMPLICIT
);
2808 ISL_F_SET(bset
, ISL_BASIC_SET_NO_REDUNDANT
);
2813 isl_basic_set_free(bset
);
2814 isl_basic_set_free(context
);
2818 /* Return a basic map that has the same intersection with "context" as "bmap"
2819 * and that is as "simple" as possible.
2821 * The core computation is performed on the pure constraints.
2822 * When we add back the meaning of the integer divisions, we need
2823 * to (re)introduce the div constraints. If we happen to have
2824 * discovered that some of these integer divisions are equal to
2825 * some affine combination of other variables, then these div
2826 * constraints may end up getting simplified in terms of the equalities,
2827 * resulting in extra inequalities on the other variables that
2828 * may have been removed already or that may not even have been
2829 * part of the input. We try and remove those constraints of
2830 * this form that are most obviously redundant with respect to
2831 * the context. We also remove those div constraints that are
2832 * redundant with respect to the other constraints in the result.
2834 struct isl_basic_map
*isl_basic_map_gist(struct isl_basic_map
*bmap
,
2835 struct isl_basic_map
*context
)
2837 isl_basic_set
*bset
, *eq
;
2838 isl_basic_map
*eq_bmap
;
2839 unsigned total
, n_div
, extra
, n_eq
, n_ineq
;
2841 if (!bmap
|| !context
)
2844 if (isl_basic_map_plain_is_universe(bmap
)) {
2845 isl_basic_map_free(context
);
2848 if (isl_basic_map_plain_is_empty(context
)) {
2849 isl_space
*space
= isl_basic_map_get_space(bmap
);
2850 isl_basic_map_free(bmap
);
2851 isl_basic_map_free(context
);
2852 return isl_basic_map_universe(space
);
2854 if (isl_basic_map_plain_is_empty(bmap
)) {
2855 isl_basic_map_free(context
);
2859 bmap
= isl_basic_map_remove_redundancies(bmap
);
2860 context
= isl_basic_map_remove_redundancies(context
);
2864 context
= isl_basic_map_align_divs(context
, bmap
);
2865 n_div
= isl_basic_map_dim(context
, isl_dim_div
);
2866 total
= isl_basic_map_dim(bmap
, isl_dim_all
);
2867 extra
= n_div
- isl_basic_map_dim(bmap
, isl_dim_div
);
2869 bset
= isl_basic_map_underlying_set(isl_basic_map_copy(bmap
));
2870 bset
= isl_basic_set_add_dims(bset
, isl_dim_set
, extra
);
2871 bset
= uset_gist(bset
,
2872 isl_basic_map_underlying_set(isl_basic_map_copy(context
)));
2873 bset
= isl_basic_set_project_out(bset
, isl_dim_set
, total
, extra
);
2875 if (!bset
|| bset
->n_eq
== 0 || n_div
== 0 ||
2876 isl_basic_set_plain_is_empty(bset
)) {
2877 isl_basic_map_free(context
);
2878 return isl_basic_map_overlying_set(bset
, bmap
);
2882 n_ineq
= bset
->n_ineq
;
2883 eq
= isl_basic_set_copy(bset
);
2884 eq
= isl_basic_set_cow(eq
);
2885 if (isl_basic_set_free_inequality(eq
, n_ineq
) < 0)
2886 eq
= isl_basic_set_free(eq
);
2887 if (isl_basic_set_free_equality(bset
, n_eq
) < 0)
2888 bset
= isl_basic_set_free(bset
);
2890 eq_bmap
= isl_basic_map_overlying_set(eq
, isl_basic_map_copy(bmap
));
2891 eq_bmap
= isl_basic_map_remove_shifted_constraints(eq_bmap
, context
);
2892 bmap
= isl_basic_map_overlying_set(bset
, bmap
);
2893 bmap
= isl_basic_map_intersect(bmap
, eq_bmap
);
2894 bmap
= isl_basic_map_remove_redundancies(bmap
);
2898 isl_basic_map_free(bmap
);
2899 isl_basic_map_free(context
);
2904 * Assumes context has no implicit divs.
2906 __isl_give isl_map
*isl_map_gist_basic_map(__isl_take isl_map
*map
,
2907 __isl_take isl_basic_map
*context
)
2911 if (!map
|| !context
)
2914 if (isl_basic_map_plain_is_empty(context
)) {
2915 isl_space
*space
= isl_map_get_space(map
);
2917 isl_basic_map_free(context
);
2918 return isl_map_universe(space
);
2921 context
= isl_basic_map_remove_redundancies(context
);
2922 map
= isl_map_cow(map
);
2923 if (!map
|| !context
)
2925 isl_assert(map
->ctx
, isl_space_is_equal(map
->dim
, context
->dim
), goto error
);
2926 map
= isl_map_compute_divs(map
);
2929 for (i
= map
->n
- 1; i
>= 0; --i
) {
2930 map
->p
[i
] = isl_basic_map_gist(map
->p
[i
],
2931 isl_basic_map_copy(context
));
2934 if (isl_basic_map_plain_is_empty(map
->p
[i
])) {
2935 isl_basic_map_free(map
->p
[i
]);
2936 if (i
!= map
->n
- 1)
2937 map
->p
[i
] = map
->p
[map
->n
- 1];
2941 isl_basic_map_free(context
);
2942 ISL_F_CLR(map
, ISL_MAP_NORMALIZED
);
2946 isl_basic_map_free(context
);
2950 /* Drop all inequalities from "bmap" that also appear in "context".
2951 * "context" is assumed to have only known local variables and
2952 * the initial local variables of "bmap" are assumed to be the same
2953 * as those of "context".
2954 * The constraints of both "bmap" and "context" are assumed
2955 * to have been sorted using isl_basic_map_sort_constraints.
2957 * Run through the inequality constraints of "bmap" and "context"
2959 * If a constraint of "bmap" involves variables not in "context",
2960 * then it cannot appear in "context".
2961 * If a matching constraint is found, it is removed from "bmap".
2963 static __isl_give isl_basic_map
*drop_inequalities(
2964 __isl_take isl_basic_map
*bmap
, __isl_keep isl_basic_map
*context
)
2967 unsigned total
, extra
;
2969 if (!bmap
|| !context
)
2970 return isl_basic_map_free(bmap
);
2972 total
= isl_basic_map_total_dim(context
);
2973 extra
= isl_basic_map_total_dim(bmap
) - total
;
2975 i1
= bmap
->n_ineq
- 1;
2976 i2
= context
->n_ineq
- 1;
2977 while (bmap
&& i1
>= 0 && i2
>= 0) {
2980 if (isl_seq_first_non_zero(bmap
->ineq
[i1
] + 1 + total
,
2985 cmp
= isl_basic_map_constraint_cmp(context
, bmap
->ineq
[i1
],
2995 if (isl_int_eq(bmap
->ineq
[i1
][0], context
->ineq
[i2
][0])) {
2996 bmap
= isl_basic_map_cow(bmap
);
2997 if (isl_basic_map_drop_inequality(bmap
, i1
) < 0)
2998 bmap
= isl_basic_map_free(bmap
);
3007 /* Drop all equalities from "bmap" that also appear in "context".
3008 * "context" is assumed to have only known local variables and
3009 * the initial local variables of "bmap" are assumed to be the same
3010 * as those of "context".
3012 * Run through the equality constraints of "bmap" and "context"
3014 * If a constraint of "bmap" involves variables not in "context",
3015 * then it cannot appear in "context".
3016 * If a matching constraint is found, it is removed from "bmap".
3018 static __isl_give isl_basic_map
*drop_equalities(
3019 __isl_take isl_basic_map
*bmap
, __isl_keep isl_basic_map
*context
)
3022 unsigned total
, extra
;
3024 if (!bmap
|| !context
)
3025 return isl_basic_map_free(bmap
);
3027 total
= isl_basic_map_total_dim(context
);
3028 extra
= isl_basic_map_total_dim(bmap
) - total
;
3030 i1
= bmap
->n_eq
- 1;
3031 i2
= context
->n_eq
- 1;
3033 while (bmap
&& i1
>= 0 && i2
>= 0) {
3036 if (isl_seq_first_non_zero(bmap
->eq
[i1
] + 1 + total
,
3039 last1
= isl_seq_last_non_zero(bmap
->eq
[i1
] + 1, total
);
3040 last2
= isl_seq_last_non_zero(context
->eq
[i2
] + 1, total
);
3041 if (last1
> last2
) {
3045 if (last1
< last2
) {
3049 if (isl_seq_eq(bmap
->eq
[i1
], context
->eq
[i2
], 1 + total
)) {
3050 bmap
= isl_basic_map_cow(bmap
);
3051 if (isl_basic_map_drop_equality(bmap
, i1
) < 0)
3052 bmap
= isl_basic_map_free(bmap
);
3061 /* Remove the constraints in "context" from "bmap".
3062 * "context" is assumed to have explicit representations
3063 * for all local variables.
3065 * First align the divs of "bmap" to those of "context" and
3066 * sort the constraints. Then drop all constraints from "bmap"
3067 * that appear in "context".
3069 __isl_give isl_basic_map
*isl_basic_map_plain_gist(
3070 __isl_take isl_basic_map
*bmap
, __isl_take isl_basic_map
*context
)
3072 isl_bool done
, known
;
3074 done
= isl_basic_map_plain_is_universe(context
);
3075 if (done
== isl_bool_false
)
3076 done
= isl_basic_map_plain_is_universe(bmap
);
3077 if (done
== isl_bool_false
)
3078 done
= isl_basic_map_plain_is_empty(context
);
3079 if (done
== isl_bool_false
)
3080 done
= isl_basic_map_plain_is_empty(bmap
);
3084 isl_basic_map_free(context
);
3087 known
= isl_basic_map_divs_known(context
);
3091 isl_die(isl_basic_map_get_ctx(bmap
), isl_error_invalid
,
3092 "context has unknown divs", goto error
);
3094 bmap
= isl_basic_map_align_divs(bmap
, context
);
3095 bmap
= isl_basic_map_gauss(bmap
, NULL
);
3096 bmap
= isl_basic_map_sort_constraints(bmap
);
3097 context
= isl_basic_map_sort_constraints(context
);
3099 bmap
= drop_inequalities(bmap
, context
);
3100 bmap
= drop_equalities(bmap
, context
);
3102 isl_basic_map_free(context
);
3103 bmap
= isl_basic_map_finalize(bmap
);
3106 isl_basic_map_free(bmap
);
3107 isl_basic_map_free(context
);
3111 /* Replace "map" by the disjunct at position "pos" and free "context".
3113 static __isl_give isl_map
*replace_by_disjunct(__isl_take isl_map
*map
,
3114 int pos
, __isl_take isl_basic_map
*context
)
3116 isl_basic_map
*bmap
;
3118 bmap
= isl_basic_map_copy(map
->p
[pos
]);
3120 isl_basic_map_free(context
);
3121 return isl_map_from_basic_map(bmap
);
3124 /* Remove the constraints in "context" from "map".
3125 * If any of the disjuncts in the result turns out to be the universe,
3126 * the return this universe.
3127 * "context" is assumed to have explicit representations
3128 * for all local variables.
3130 __isl_give isl_map
*isl_map_plain_gist_basic_map(__isl_take isl_map
*map
,
3131 __isl_take isl_basic_map
*context
)
3134 isl_bool univ
, known
;
3136 univ
= isl_basic_map_plain_is_universe(context
);
3140 isl_basic_map_free(context
);
3143 known
= isl_basic_map_divs_known(context
);
3147 isl_die(isl_map_get_ctx(map
), isl_error_invalid
,
3148 "context has unknown divs", goto error
);
3150 map
= isl_map_cow(map
);
3153 for (i
= 0; i
< map
->n
; ++i
) {
3154 map
->p
[i
] = isl_basic_map_plain_gist(map
->p
[i
],
3155 isl_basic_map_copy(context
));
3156 univ
= isl_basic_map_plain_is_universe(map
->p
[i
]);
3159 if (univ
&& map
->n
> 1)
3160 return replace_by_disjunct(map
, i
, context
);
3163 isl_basic_map_free(context
);
3164 ISL_F_CLR(map
, ISL_MAP_NORMALIZED
);
3166 ISL_F_CLR(map
, ISL_MAP_DISJOINT
);
3170 isl_basic_map_free(context
);
3174 /* Return a map that has the same intersection with "context" as "map"
3175 * and that is as "simple" as possible.
3177 * If "map" is already the universe, then we cannot make it any simpler.
3178 * Similarly, if "context" is the universe, then we cannot exploit it
3180 * If "map" and "context" are identical to each other, then we can
3181 * return the corresponding universe.
3183 * If none of these cases apply, we have to work a bit harder.
3184 * During this computation, we make use of a single disjunct context,
3185 * so if the original context consists of more than one disjunct
3186 * then we need to approximate the context by a single disjunct set.
3187 * Simply taking the simple hull may drop constraints that are
3188 * only implicitly available in each disjunct. We therefore also
3189 * look for constraints among those defining "map" that are valid
3190 * for the context. These can then be used to simplify away
3191 * the corresponding constraints in "map".
3193 static __isl_give isl_map
*map_gist(__isl_take isl_map
*map
,
3194 __isl_take isl_map
*context
)
3198 isl_basic_map
*hull
;
3200 is_universe
= isl_map_plain_is_universe(map
);
3201 if (is_universe
>= 0 && !is_universe
)
3202 is_universe
= isl_map_plain_is_universe(context
);
3203 if (is_universe
< 0)
3206 isl_map_free(context
);
3210 equal
= isl_map_plain_is_equal(map
, context
);
3214 isl_map
*res
= isl_map_universe(isl_map_get_space(map
));
3216 isl_map_free(context
);
3220 context
= isl_map_compute_divs(context
);
3223 if (isl_map_n_basic_map(context
) == 1) {
3224 hull
= isl_map_simple_hull(context
);
3229 ctx
= isl_map_get_ctx(map
);
3230 list
= isl_map_list_alloc(ctx
, 2);
3231 list
= isl_map_list_add(list
, isl_map_copy(context
));
3232 list
= isl_map_list_add(list
, isl_map_copy(map
));
3233 hull
= isl_map_unshifted_simple_hull_from_map_list(context
,
3236 return isl_map_gist_basic_map(map
, hull
);
3239 isl_map_free(context
);
3243 __isl_give isl_map
*isl_map_gist(__isl_take isl_map
*map
,
3244 __isl_take isl_map
*context
)
3246 return isl_map_align_params_map_map_and(map
, context
, &map_gist
);
3249 struct isl_basic_set
*isl_basic_set_gist(struct isl_basic_set
*bset
,
3250 struct isl_basic_set
*context
)
3252 return (struct isl_basic_set
*)isl_basic_map_gist(
3253 (struct isl_basic_map
*)bset
, (struct isl_basic_map
*)context
);
3256 __isl_give isl_set
*isl_set_gist_basic_set(__isl_take isl_set
*set
,
3257 __isl_take isl_basic_set
*context
)
3259 return (struct isl_set
*)isl_map_gist_basic_map((struct isl_map
*)set
,
3260 (struct isl_basic_map
*)context
);
3263 __isl_give isl_set
*isl_set_gist_params_basic_set(__isl_take isl_set
*set
,
3264 __isl_take isl_basic_set
*context
)
3266 isl_space
*space
= isl_set_get_space(set
);
3267 isl_basic_set
*dom_context
= isl_basic_set_universe(space
);
3268 dom_context
= isl_basic_set_intersect_params(dom_context
, context
);
3269 return isl_set_gist_basic_set(set
, dom_context
);
3272 __isl_give isl_set
*isl_set_gist(__isl_take isl_set
*set
,
3273 __isl_take isl_set
*context
)
3275 return (struct isl_set
*)isl_map_gist((struct isl_map
*)set
,
3276 (struct isl_map
*)context
);
3279 /* Compute the gist of "bmap" with respect to the constraints "context"
3282 __isl_give isl_basic_map
*isl_basic_map_gist_domain(
3283 __isl_take isl_basic_map
*bmap
, __isl_take isl_basic_set
*context
)
3285 isl_space
*space
= isl_basic_map_get_space(bmap
);
3286 isl_basic_map
*bmap_context
= isl_basic_map_universe(space
);
3288 bmap_context
= isl_basic_map_intersect_domain(bmap_context
, context
);
3289 return isl_basic_map_gist(bmap
, bmap_context
);
3292 __isl_give isl_map
*isl_map_gist_domain(__isl_take isl_map
*map
,
3293 __isl_take isl_set
*context
)
3295 isl_map
*map_context
= isl_map_universe(isl_map_get_space(map
));
3296 map_context
= isl_map_intersect_domain(map_context
, context
);
3297 return isl_map_gist(map
, map_context
);
3300 __isl_give isl_map
*isl_map_gist_range(__isl_take isl_map
*map
,
3301 __isl_take isl_set
*context
)
3303 isl_map
*map_context
= isl_map_universe(isl_map_get_space(map
));
3304 map_context
= isl_map_intersect_range(map_context
, context
);
3305 return isl_map_gist(map
, map_context
);
3308 __isl_give isl_map
*isl_map_gist_params(__isl_take isl_map
*map
,
3309 __isl_take isl_set
*context
)
3311 isl_map
*map_context
= isl_map_universe(isl_map_get_space(map
));
3312 map_context
= isl_map_intersect_params(map_context
, context
);
3313 return isl_map_gist(map
, map_context
);
3316 __isl_give isl_set
*isl_set_gist_params(__isl_take isl_set
*set
,
3317 __isl_take isl_set
*context
)
3319 return isl_map_gist_params(set
, context
);
3322 /* Quick check to see if two basic maps are disjoint.
3323 * In particular, we reduce the equalities and inequalities of
3324 * one basic map in the context of the equalities of the other
3325 * basic map and check if we get a contradiction.
3327 isl_bool
isl_basic_map_plain_is_disjoint(__isl_keep isl_basic_map
*bmap1
,
3328 __isl_keep isl_basic_map
*bmap2
)
3330 struct isl_vec
*v
= NULL
;
3335 if (!bmap1
|| !bmap2
)
3336 return isl_bool_error
;
3337 isl_assert(bmap1
->ctx
, isl_space_is_equal(bmap1
->dim
, bmap2
->dim
),
3338 return isl_bool_error
);
3339 if (bmap1
->n_div
|| bmap2
->n_div
)
3340 return isl_bool_false
;
3341 if (!bmap1
->n_eq
&& !bmap2
->n_eq
)
3342 return isl_bool_false
;
3344 total
= isl_space_dim(bmap1
->dim
, isl_dim_all
);
3346 return isl_bool_false
;
3347 v
= isl_vec_alloc(bmap1
->ctx
, 1 + total
);
3350 elim
= isl_alloc_array(bmap1
->ctx
, int, total
);
3353 compute_elimination_index(bmap1
, elim
);
3354 for (i
= 0; i
< bmap2
->n_eq
; ++i
) {
3356 reduced
= reduced_using_equalities(v
->block
.data
, bmap2
->eq
[i
],
3358 if (reduced
&& !isl_int_is_zero(v
->block
.data
[0]) &&
3359 isl_seq_first_non_zero(v
->block
.data
+ 1, total
) == -1)
3362 for (i
= 0; i
< bmap2
->n_ineq
; ++i
) {
3364 reduced
= reduced_using_equalities(v
->block
.data
,
3365 bmap2
->ineq
[i
], bmap1
, elim
);
3366 if (reduced
&& isl_int_is_neg(v
->block
.data
[0]) &&
3367 isl_seq_first_non_zero(v
->block
.data
+ 1, total
) == -1)
3370 compute_elimination_index(bmap2
, elim
);
3371 for (i
= 0; i
< bmap1
->n_ineq
; ++i
) {
3373 reduced
= reduced_using_equalities(v
->block
.data
,
3374 bmap1
->ineq
[i
], bmap2
, elim
);
3375 if (reduced
&& isl_int_is_neg(v
->block
.data
[0]) &&
3376 isl_seq_first_non_zero(v
->block
.data
+ 1, total
) == -1)
3381 return isl_bool_false
;
3385 return isl_bool_true
;
3389 return isl_bool_error
;
3392 int isl_basic_set_plain_is_disjoint(__isl_keep isl_basic_set
*bset1
,
3393 __isl_keep isl_basic_set
*bset2
)
3395 return isl_basic_map_plain_is_disjoint((struct isl_basic_map
*)bset1
,
3396 (struct isl_basic_map
*)bset2
);
3399 /* Does "test" hold for all pairs of basic maps in "map1" and "map2"?
3401 static isl_bool
all_pairs(__isl_keep isl_map
*map1
, __isl_keep isl_map
*map2
,
3402 isl_bool (*test
)(__isl_keep isl_basic_map
*bmap1
,
3403 __isl_keep isl_basic_map
*bmap2
))
3408 return isl_bool_error
;
3410 for (i
= 0; i
< map1
->n
; ++i
) {
3411 for (j
= 0; j
< map2
->n
; ++j
) {
3412 isl_bool d
= test(map1
->p
[i
], map2
->p
[j
]);
3413 if (d
!= isl_bool_true
)
3418 return isl_bool_true
;
3421 /* Are "map1" and "map2" obviously disjoint, based on information
3422 * that can be derived without looking at the individual basic maps?
3424 * In particular, if one of them is empty or if they live in different spaces
3425 * (ignoring parameters), then they are clearly disjoint.
3427 static isl_bool
isl_map_plain_is_disjoint_global(__isl_keep isl_map
*map1
,
3428 __isl_keep isl_map
*map2
)
3434 return isl_bool_error
;
3436 disjoint
= isl_map_plain_is_empty(map1
);
3437 if (disjoint
< 0 || disjoint
)
3440 disjoint
= isl_map_plain_is_empty(map2
);
3441 if (disjoint
< 0 || disjoint
)
3444 match
= isl_space_tuple_is_equal(map1
->dim
, isl_dim_in
,
3445 map2
->dim
, isl_dim_in
);
3446 if (match
< 0 || !match
)
3447 return match
< 0 ? isl_bool_error
: isl_bool_true
;
3449 match
= isl_space_tuple_is_equal(map1
->dim
, isl_dim_out
,
3450 map2
->dim
, isl_dim_out
);
3451 if (match
< 0 || !match
)
3452 return match
< 0 ? isl_bool_error
: isl_bool_true
;
3454 return isl_bool_false
;
3457 /* Are "map1" and "map2" obviously disjoint?
3459 * If one of them is empty or if they live in different spaces (ignoring
3460 * parameters), then they are clearly disjoint.
3461 * This is checked by isl_map_plain_is_disjoint_global.
3463 * If they have different parameters, then we skip any further tests.
3465 * If they are obviously equal, but not obviously empty, then we will
3466 * not be able to detect if they are disjoint.
3468 * Otherwise we check if each basic map in "map1" is obviously disjoint
3469 * from each basic map in "map2".
3471 isl_bool
isl_map_plain_is_disjoint(__isl_keep isl_map
*map1
,
3472 __isl_keep isl_map
*map2
)
3478 disjoint
= isl_map_plain_is_disjoint_global(map1
, map2
);
3479 if (disjoint
< 0 || disjoint
)
3482 match
= isl_space_match(map1
->dim
, isl_dim_param
,
3483 map2
->dim
, isl_dim_param
);
3484 if (match
< 0 || !match
)
3485 return match
< 0 ? isl_bool_error
: isl_bool_false
;
3487 intersect
= isl_map_plain_is_equal(map1
, map2
);
3488 if (intersect
< 0 || intersect
)
3489 return intersect
< 0 ? isl_bool_error
: isl_bool_false
;
3491 return all_pairs(map1
, map2
, &isl_basic_map_plain_is_disjoint
);
3494 /* Are "map1" and "map2" disjoint?
3496 * They are disjoint if they are "obviously disjoint" or if one of them
3497 * is empty. Otherwise, they are not disjoint if one of them is universal.
3498 * If the two inputs are (obviously) equal and not empty, then they are
3500 * If none of these cases apply, then check if all pairs of basic maps
3503 isl_bool
isl_map_is_disjoint(__isl_keep isl_map
*map1
, __isl_keep isl_map
*map2
)
3508 disjoint
= isl_map_plain_is_disjoint_global(map1
, map2
);
3509 if (disjoint
< 0 || disjoint
)
3512 disjoint
= isl_map_is_empty(map1
);
3513 if (disjoint
< 0 || disjoint
)
3516 disjoint
= isl_map_is_empty(map2
);
3517 if (disjoint
< 0 || disjoint
)
3520 intersect
= isl_map_plain_is_universe(map1
);
3521 if (intersect
< 0 || intersect
)
3522 return intersect
< 0 ? isl_bool_error
: isl_bool_false
;
3524 intersect
= isl_map_plain_is_universe(map2
);
3525 if (intersect
< 0 || intersect
)
3526 return intersect
< 0 ? isl_bool_error
: isl_bool_false
;
3528 intersect
= isl_map_plain_is_equal(map1
, map2
);
3529 if (intersect
< 0 || intersect
)
3530 return isl_bool_not(intersect
);
3532 return all_pairs(map1
, map2
, &isl_basic_map_is_disjoint
);
3535 /* Are "bmap1" and "bmap2" disjoint?
3537 * They are disjoint if they are "obviously disjoint" or if one of them
3538 * is empty. Otherwise, they are not disjoint if one of them is universal.
3539 * If none of these cases apply, we compute the intersection and see if
3540 * the result is empty.
3542 isl_bool
isl_basic_map_is_disjoint(__isl_keep isl_basic_map
*bmap1
,
3543 __isl_keep isl_basic_map
*bmap2
)
3547 isl_basic_map
*test
;
3549 disjoint
= isl_basic_map_plain_is_disjoint(bmap1
, bmap2
);
3550 if (disjoint
< 0 || disjoint
)
3553 disjoint
= isl_basic_map_is_empty(bmap1
);
3554 if (disjoint
< 0 || disjoint
)
3557 disjoint
= isl_basic_map_is_empty(bmap2
);
3558 if (disjoint
< 0 || disjoint
)
3561 intersect
= isl_basic_map_plain_is_universe(bmap1
);
3562 if (intersect
< 0 || intersect
)
3563 return intersect
< 0 ? isl_bool_error
: isl_bool_false
;
3565 intersect
= isl_basic_map_plain_is_universe(bmap2
);
3566 if (intersect
< 0 || intersect
)
3567 return intersect
< 0 ? isl_bool_error
: isl_bool_false
;
3569 test
= isl_basic_map_intersect(isl_basic_map_copy(bmap1
),
3570 isl_basic_map_copy(bmap2
));
3571 disjoint
= isl_basic_map_is_empty(test
);
3572 isl_basic_map_free(test
);
3577 /* Are "bset1" and "bset2" disjoint?
3579 isl_bool
isl_basic_set_is_disjoint(__isl_keep isl_basic_set
*bset1
,
3580 __isl_keep isl_basic_set
*bset2
)
3582 return isl_basic_map_is_disjoint(bset1
, bset2
);
3585 isl_bool
isl_set_plain_is_disjoint(__isl_keep isl_set
*set1
,
3586 __isl_keep isl_set
*set2
)
3588 return isl_map_plain_is_disjoint((struct isl_map
*)set1
,
3589 (struct isl_map
*)set2
);
3592 /* Are "set1" and "set2" disjoint?
3594 isl_bool
isl_set_is_disjoint(__isl_keep isl_set
*set1
, __isl_keep isl_set
*set2
)
3596 return isl_map_is_disjoint(set1
, set2
);
3599 /* Is "v" equal to 0, 1 or -1?
3601 static int is_zero_or_one(isl_int v
)
3603 return isl_int_is_zero(v
) || isl_int_is_one(v
) || isl_int_is_negone(v
);
3606 /* Check if we can combine a given div with lower bound l and upper
3607 * bound u with some other div and if so return that other div.
3608 * Otherwise return -1.
3610 * We first check that
3611 * - the bounds are opposites of each other (except for the constant
3613 * - the bounds do not reference any other div
3614 * - no div is defined in terms of this div
3616 * Let m be the size of the range allowed on the div by the bounds.
3617 * That is, the bounds are of the form
3619 * e <= a <= e + m - 1
3621 * with e some expression in the other variables.
3622 * We look for another div b such that no third div is defined in terms
3623 * of this second div b and such that in any constraint that contains
3624 * a (except for the given lower and upper bound), also contains b
3625 * with a coefficient that is m times that of b.
3626 * That is, all constraints (execpt for the lower and upper bound)
3629 * e + f (a + m b) >= 0
3631 * Furthermore, in the constraints that only contain b, the coefficient
3632 * of b should be equal to 1 or -1.
3633 * If so, we return b so that "a + m b" can be replaced by
3634 * a single div "c = a + m b".
3636 static int div_find_coalesce(struct isl_basic_map
*bmap
, int *pairs
,
3637 unsigned div
, unsigned l
, unsigned u
)
3643 if (bmap
->n_div
<= 1)
3645 dim
= isl_space_dim(bmap
->dim
, isl_dim_all
);
3646 if (isl_seq_first_non_zero(bmap
->ineq
[l
] + 1 + dim
, div
) != -1)
3648 if (isl_seq_first_non_zero(bmap
->ineq
[l
] + 1 + dim
+ div
+ 1,
3649 bmap
->n_div
- div
- 1) != -1)
3651 if (!isl_seq_is_neg(bmap
->ineq
[l
] + 1, bmap
->ineq
[u
] + 1,
3655 for (i
= 0; i
< bmap
->n_div
; ++i
) {
3656 if (isl_int_is_zero(bmap
->div
[i
][0]))
3658 if (!isl_int_is_zero(bmap
->div
[i
][1 + 1 + dim
+ div
]))
3662 isl_int_add(bmap
->ineq
[l
][0], bmap
->ineq
[l
][0], bmap
->ineq
[u
][0]);
3663 if (isl_int_is_neg(bmap
->ineq
[l
][0])) {
3664 isl_int_sub(bmap
->ineq
[l
][0],
3665 bmap
->ineq
[l
][0], bmap
->ineq
[u
][0]);
3666 bmap
= isl_basic_map_copy(bmap
);
3667 bmap
= isl_basic_map_set_to_empty(bmap
);
3668 isl_basic_map_free(bmap
);
3671 isl_int_add_ui(bmap
->ineq
[l
][0], bmap
->ineq
[l
][0], 1);
3672 for (i
= 0; i
< bmap
->n_div
; ++i
) {
3677 for (j
= 0; j
< bmap
->n_div
; ++j
) {
3678 if (isl_int_is_zero(bmap
->div
[j
][0]))
3680 if (!isl_int_is_zero(bmap
->div
[j
][1 + 1 + dim
+ i
]))
3683 if (j
< bmap
->n_div
)
3685 for (j
= 0; j
< bmap
->n_ineq
; ++j
) {
3687 if (j
== l
|| j
== u
)
3689 if (isl_int_is_zero(bmap
->ineq
[j
][1 + dim
+ div
])) {
3690 if (is_zero_or_one(bmap
->ineq
[j
][1 + dim
+ i
]))
3694 if (isl_int_is_zero(bmap
->ineq
[j
][1 + dim
+ i
]))
3696 isl_int_mul(bmap
->ineq
[j
][1 + dim
+ div
],
3697 bmap
->ineq
[j
][1 + dim
+ div
],
3699 valid
= isl_int_eq(bmap
->ineq
[j
][1 + dim
+ div
],
3700 bmap
->ineq
[j
][1 + dim
+ i
]);
3701 isl_int_divexact(bmap
->ineq
[j
][1 + dim
+ div
],
3702 bmap
->ineq
[j
][1 + dim
+ div
],
3707 if (j
< bmap
->n_ineq
)
3712 isl_int_sub_ui(bmap
->ineq
[l
][0], bmap
->ineq
[l
][0], 1);
3713 isl_int_sub(bmap
->ineq
[l
][0], bmap
->ineq
[l
][0], bmap
->ineq
[u
][0]);
3717 /* Given a lower and an upper bound on div i, construct an inequality
3718 * that when nonnegative ensures that this pair of bounds always allows
3719 * for an integer value of the given div.
3720 * The lower bound is inequality l, while the upper bound is inequality u.
3721 * The constructed inequality is stored in ineq.
3722 * g, fl, fu are temporary scalars.
3724 * Let the upper bound be
3728 * and the lower bound
3732 * Let n_u = f_u g and n_l = f_l g, with g = gcd(n_u, n_l).
3735 * - f_u e_l <= f_u f_l g a <= f_l e_u
3737 * Since all variables are integer valued, this is equivalent to
3739 * - f_u e_l - (f_u - 1) <= f_u f_l g a <= f_l e_u + (f_l - 1)
3741 * If this interval is at least f_u f_l g, then it contains at least
3742 * one integer value for a.
3743 * That is, the test constraint is
3745 * f_l e_u + f_u e_l + f_l - 1 + f_u - 1 + 1 >= f_u f_l g
3747 static void construct_test_ineq(struct isl_basic_map
*bmap
, int i
,
3748 int l
, int u
, isl_int
*ineq
, isl_int
*g
, isl_int
*fl
, isl_int
*fu
)
3751 dim
= isl_space_dim(bmap
->dim
, isl_dim_all
);
3753 isl_int_gcd(*g
, bmap
->ineq
[l
][1 + dim
+ i
], bmap
->ineq
[u
][1 + dim
+ i
]);
3754 isl_int_divexact(*fl
, bmap
->ineq
[l
][1 + dim
+ i
], *g
);
3755 isl_int_divexact(*fu
, bmap
->ineq
[u
][1 + dim
+ i
], *g
);
3756 isl_int_neg(*fu
, *fu
);
3757 isl_seq_combine(ineq
, *fl
, bmap
->ineq
[u
], *fu
, bmap
->ineq
[l
],
3758 1 + dim
+ bmap
->n_div
);
3759 isl_int_add(ineq
[0], ineq
[0], *fl
);
3760 isl_int_add(ineq
[0], ineq
[0], *fu
);
3761 isl_int_sub_ui(ineq
[0], ineq
[0], 1);
3762 isl_int_mul(*g
, *g
, *fl
);
3763 isl_int_mul(*g
, *g
, *fu
);
3764 isl_int_sub(ineq
[0], ineq
[0], *g
);
3767 /* Remove more kinds of divs that are not strictly needed.
3768 * In particular, if all pairs of lower and upper bounds on a div
3769 * are such that they allow at least one integer value of the div,
3770 * the we can eliminate the div using Fourier-Motzkin without
3771 * introducing any spurious solutions.
3773 static struct isl_basic_map
*drop_more_redundant_divs(
3774 struct isl_basic_map
*bmap
, int *pairs
, int n
)
3776 struct isl_tab
*tab
= NULL
;
3777 struct isl_vec
*vec
= NULL
;
3789 dim
= isl_space_dim(bmap
->dim
, isl_dim_all
);
3790 vec
= isl_vec_alloc(bmap
->ctx
, 1 + dim
+ bmap
->n_div
);
3794 tab
= isl_tab_from_basic_map(bmap
, 0);
3799 enum isl_lp_result res
;
3801 for (i
= 0; i
< bmap
->n_div
; ++i
) {
3804 if (best
>= 0 && pairs
[best
] <= pairs
[i
])
3810 for (l
= 0; l
< bmap
->n_ineq
; ++l
) {
3811 if (!isl_int_is_pos(bmap
->ineq
[l
][1 + dim
+ i
]))
3813 for (u
= 0; u
< bmap
->n_ineq
; ++u
) {
3814 if (!isl_int_is_neg(bmap
->ineq
[u
][1 + dim
+ i
]))
3816 construct_test_ineq(bmap
, i
, l
, u
,
3817 vec
->el
, &g
, &fl
, &fu
);
3818 res
= isl_tab_min(tab
, vec
->el
,
3819 bmap
->ctx
->one
, &g
, NULL
, 0);
3820 if (res
== isl_lp_error
)
3822 if (res
== isl_lp_empty
) {
3823 bmap
= isl_basic_map_set_to_empty(bmap
);
3826 if (res
!= isl_lp_ok
|| isl_int_is_neg(g
))
3829 if (u
< bmap
->n_ineq
)
3832 if (l
== bmap
->n_ineq
) {
3852 bmap
= isl_basic_map_remove_dims(bmap
, isl_dim_div
, remove
, 1);
3853 return isl_basic_map_drop_redundant_divs(bmap
);
3856 isl_basic_map_free(bmap
);
3865 /* Given a pair of divs div1 and div2 such that, except for the lower bound l
3866 * and the upper bound u, div1 always occurs together with div2 in the form
3867 * (div1 + m div2), where m is the constant range on the variable div1
3868 * allowed by l and u, replace the pair div1 and div2 by a single
3869 * div that is equal to div1 + m div2.
3871 * The new div will appear in the location that contains div2.
3872 * We need to modify all constraints that contain
3873 * div2 = (div - div1) / m
3874 * The coefficient of div2 is known to be equal to 1 or -1.
3875 * (If a constraint does not contain div2, it will also not contain div1.)
3876 * If the constraint also contains div1, then we know they appear
3877 * as f (div1 + m div2) and we can simply replace (div1 + m div2) by div,
3878 * i.e., the coefficient of div is f.
3880 * Otherwise, we first need to introduce div1 into the constraint.
3889 * A lower bound on div2
3893 * can be replaced by
3895 * m div2 + div1 + m t + f >= 0
3901 * can be replaced by
3903 * -(m div2 + div1) + m t + f' >= 0
3905 * These constraint are those that we would obtain from eliminating
3906 * div1 using Fourier-Motzkin.
3908 * After all constraints have been modified, we drop the lower and upper
3909 * bound and then drop div1.
3911 static struct isl_basic_map
*coalesce_divs(struct isl_basic_map
*bmap
,
3912 unsigned div1
, unsigned div2
, unsigned l
, unsigned u
)
3916 unsigned dim
, total
;
3919 ctx
= isl_basic_map_get_ctx(bmap
);
3921 dim
= isl_space_dim(bmap
->dim
, isl_dim_all
);
3922 total
= 1 + dim
+ bmap
->n_div
;
3925 isl_int_add(m
, bmap
->ineq
[l
][0], bmap
->ineq
[u
][0]);
3926 isl_int_add_ui(m
, m
, 1);
3928 for (i
= 0; i
< bmap
->n_ineq
; ++i
) {
3929 if (i
== l
|| i
== u
)
3931 if (isl_int_is_zero(bmap
->ineq
[i
][1 + dim
+ div2
]))
3933 if (isl_int_is_zero(bmap
->ineq
[i
][1 + dim
+ div1
])) {
3934 if (isl_int_is_pos(bmap
->ineq
[i
][1 + dim
+ div2
]))
3935 isl_seq_combine(bmap
->ineq
[i
], m
, bmap
->ineq
[i
],
3936 ctx
->one
, bmap
->ineq
[l
], total
);
3938 isl_seq_combine(bmap
->ineq
[i
], m
, bmap
->ineq
[i
],
3939 ctx
->one
, bmap
->ineq
[u
], total
);
3941 isl_int_set(bmap
->ineq
[i
][1 + dim
+ div2
],
3942 bmap
->ineq
[i
][1 + dim
+ div1
]);
3943 isl_int_set_si(bmap
->ineq
[i
][1 + dim
+ div1
], 0);
3948 isl_basic_map_drop_inequality(bmap
, l
);
3949 isl_basic_map_drop_inequality(bmap
, u
);
3951 isl_basic_map_drop_inequality(bmap
, u
);
3952 isl_basic_map_drop_inequality(bmap
, l
);
3954 bmap
= isl_basic_map_drop_div(bmap
, div1
);
3958 /* First check if we can coalesce any pair of divs and
3959 * then continue with dropping more redundant divs.
3961 * We loop over all pairs of lower and upper bounds on a div
3962 * with coefficient 1 and -1, respectively, check if there
3963 * is any other div "c" with which we can coalesce the div
3964 * and if so, perform the coalescing.
3966 static struct isl_basic_map
*coalesce_or_drop_more_redundant_divs(
3967 struct isl_basic_map
*bmap
, int *pairs
, int n
)
3972 dim
= isl_space_dim(bmap
->dim
, isl_dim_all
);
3974 for (i
= 0; i
< bmap
->n_div
; ++i
) {
3977 for (l
= 0; l
< bmap
->n_ineq
; ++l
) {
3978 if (!isl_int_is_one(bmap
->ineq
[l
][1 + dim
+ i
]))
3980 for (u
= 0; u
< bmap
->n_ineq
; ++u
) {
3983 if (!isl_int_is_negone(bmap
->ineq
[u
][1+dim
+i
]))
3985 c
= div_find_coalesce(bmap
, pairs
, i
, l
, u
);
3989 bmap
= coalesce_divs(bmap
, i
, c
, l
, u
);
3990 return isl_basic_map_drop_redundant_divs(bmap
);
3995 if (ISL_F_ISSET(bmap
, ISL_BASIC_MAP_EMPTY
))
3998 return drop_more_redundant_divs(bmap
, pairs
, n
);
4001 /* Are the "n" coefficients starting at "first" of inequality constraints
4002 * "i" and "j" of "bmap" equal to each other?
4004 static int is_parallel_part(__isl_keep isl_basic_map
*bmap
, int i
, int j
,
4007 return isl_seq_eq(bmap
->ineq
[i
] + first
, bmap
->ineq
[j
] + first
, n
);
4010 /* Are the "n" coefficients starting at "first" of inequality constraints
4011 * "i" and "j" of "bmap" opposite to each other?
4013 static int is_opposite_part(__isl_keep isl_basic_map
*bmap
, int i
, int j
,
4016 return isl_seq_is_neg(bmap
->ineq
[i
] + first
, bmap
->ineq
[j
] + first
, n
);
4019 /* Are inequality constraints "i" and "j" of "bmap" opposite to each other,
4020 * apart from the constant term?
4022 static int is_opposite(__isl_keep isl_basic_map
*bmap
, int i
, int j
)
4026 total
= isl_basic_map_dim(bmap
, isl_dim_all
);
4027 return is_opposite_part(bmap
, i
, j
, 1, total
);
4030 /* Are inequality constraints "i" and "j" of "bmap" equal to each other,
4031 * apart from the constant term and the coefficient at position "pos"?
4033 static int is_parallel_except(__isl_keep isl_basic_map
*bmap
, int i
, int j
,
4038 total
= isl_basic_map_dim(bmap
, isl_dim_all
);
4039 return is_parallel_part(bmap
, i
, j
, 1, pos
- 1) &&
4040 is_parallel_part(bmap
, i
, j
, pos
+ 1, total
- pos
);
4043 /* Are inequality constraints "i" and "j" of "bmap" opposite to each other,
4044 * apart from the constant term and the coefficient at position "pos"?
4046 static int is_opposite_except(__isl_keep isl_basic_map
*bmap
, int i
, int j
,
4051 total
= isl_basic_map_dim(bmap
, isl_dim_all
);
4052 return is_opposite_part(bmap
, i
, j
, 1, pos
- 1) &&
4053 is_opposite_part(bmap
, i
, j
, pos
+ 1, total
- pos
);
4056 /* Restart isl_basic_map_drop_redundant_divs after "bmap" has
4057 * been modified, simplying it if "simplify" is set.
4058 * Free the temporary data structure "pairs" that was associated
4059 * to the old version of "bmap".
4061 static __isl_give isl_basic_map
*drop_redundant_divs_again(
4062 __isl_take isl_basic_map
*bmap
, __isl_take
int *pairs
, int simplify
)
4065 bmap
= isl_basic_map_simplify(bmap
);
4067 return isl_basic_map_drop_redundant_divs(bmap
);
4070 /* Is "div" the single unknown existentially quantified variable
4071 * in inequality constraint "ineq" of "bmap"?
4072 * "div" is known to have a non-zero coefficient in "ineq".
4074 static int single_unknown(__isl_keep isl_basic_map
*bmap
, int ineq
, int div
)
4077 unsigned n_div
, o_div
;
4079 if (isl_basic_map_div_is_known(bmap
, div
))
4081 n_div
= isl_basic_map_dim(bmap
, isl_dim_div
);
4084 o_div
= isl_basic_map_offset(bmap
, isl_dim_div
);
4085 for (i
= 0; i
< n_div
; ++i
) {
4088 if (isl_int_is_zero(bmap
->ineq
[ineq
][o_div
+ i
]))
4090 if (!isl_basic_map_div_is_known(bmap
, i
))
4097 /* Does integer division "div" have coefficient 1 in inequality constraint
4100 static int has_coef_one(__isl_keep isl_basic_map
*bmap
, int div
, int ineq
)
4104 o_div
= isl_basic_map_offset(bmap
, isl_dim_div
);
4105 if (isl_int_is_one(bmap
->ineq
[ineq
][o_div
+ div
]))
4111 /* Turn inequality constraint "ineq" of "bmap" into an equality and
4112 * then try and drop redundant divs again,
4113 * freeing the temporary data structure "pairs" that was associated
4114 * to the old version of "bmap".
4116 static __isl_give isl_basic_map
*set_eq_and_try_again(
4117 __isl_take isl_basic_map
*bmap
, int ineq
, __isl_take
int *pairs
)
4119 bmap
= isl_basic_map_cow(bmap
);
4120 isl_basic_map_inequality_to_equality(bmap
, ineq
);
4121 return drop_redundant_divs_again(bmap
, pairs
, 1);
4124 /* Given two inequality constraints
4126 * f(x) + n d + c >= 0, (ineq)
4128 * with d the variable at position "pos", and
4130 * f(x) + c0 >= 0, (lower)
4132 * compute the maximal value of the lower bound ceil((-f(x) - c)/n)
4133 * determined by the first constraint.
4140 static void lower_bound_from_parallel(__isl_keep isl_basic_map
*bmap
,
4141 int ineq
, int lower
, int pos
, isl_int
*l
)
4143 isl_int_neg(*l
, bmap
->ineq
[ineq
][0]);
4144 isl_int_add(*l
, *l
, bmap
->ineq
[lower
][0]);
4145 isl_int_cdiv_q(*l
, *l
, bmap
->ineq
[ineq
][pos
]);
4148 /* Given two inequality constraints
4150 * f(x) + n d + c >= 0, (ineq)
4152 * with d the variable at position "pos", and
4154 * -f(x) - c0 >= 0, (upper)
4156 * compute the minimal value of the lower bound ceil((-f(x) - c)/n)
4157 * determined by the first constraint.
4164 static void lower_bound_from_opposite(__isl_keep isl_basic_map
*bmap
,
4165 int ineq
, int upper
, int pos
, isl_int
*u
)
4167 isl_int_neg(*u
, bmap
->ineq
[ineq
][0]);
4168 isl_int_sub(*u
, *u
, bmap
->ineq
[upper
][0]);
4169 isl_int_cdiv_q(*u
, *u
, bmap
->ineq
[ineq
][pos
]);
4172 /* Given a lower bound constraint "ineq" on "div" in "bmap",
4173 * does the corresponding lower bound have a fixed value in "bmap"?
4175 * In particular, "ineq" is of the form
4177 * f(x) + n d + c >= 0
4179 * with n > 0, c the constant term and
4180 * d the existentially quantified variable "div".
4181 * That is, the lower bound is
4183 * ceil((-f(x) - c)/n)
4185 * Look for a pair of constraints
4190 * i.e., -c1 <= -f(x) <= c0, that fix ceil((-f(x) - c)/n) to a constant value.
4191 * That is, check that
4193 * ceil((-c1 - c)/n) = ceil((c0 - c)/n)
4195 * If so, return the index of inequality f(x) + c0 >= 0.
4196 * Otherwise, return -1.
4198 static int lower_bound_is_cst(__isl_keep isl_basic_map
*bmap
, int div
, int ineq
)
4201 int lower
= -1, upper
= -1;
4202 unsigned o_div
, n_div
;
4206 n_div
= isl_basic_map_dim(bmap
, isl_dim_div
);
4207 o_div
= isl_basic_map_offset(bmap
, isl_dim_div
);
4208 for (i
= 0; i
< bmap
->n_ineq
&& (lower
< 0 || upper
< 0); ++i
) {
4211 if (!isl_int_is_zero(bmap
->ineq
[i
][o_div
+ div
]))
4214 is_parallel_except(bmap
, ineq
, i
, o_div
+ div
)) {
4219 is_opposite_except(bmap
, ineq
, i
, o_div
+ div
)) {
4224 if (lower
< 0 || upper
< 0)
4230 lower_bound_from_parallel(bmap
, ineq
, lower
, o_div
+ div
, &l
);
4231 lower_bound_from_opposite(bmap
, ineq
, upper
, o_div
+ div
, &u
);
4233 equal
= isl_int_eq(l
, u
);
4238 return equal
? lower
: -1;
4241 /* Given a lower bound constraint "ineq" on the existentially quantified
4242 * variable "div", such that the corresponding lower bound has
4243 * a fixed value in "bmap", assign this fixed value to the variable and
4244 * then try and drop redundant divs again,
4245 * freeing the temporary data structure "pairs" that was associated
4246 * to the old version of "bmap".
4247 * "lower" determines the constant value for the lower bound.
4249 * In particular, "ineq" is of the form
4251 * f(x) + n d + c >= 0,
4253 * while "lower" is of the form
4257 * The lower bound is ceil((-f(x) - c)/n) and its constant value
4258 * is ceil((c0 - c)/n).
4260 static __isl_give isl_basic_map
*fix_cst_lower(__isl_take isl_basic_map
*bmap
,
4261 int div
, int ineq
, int lower
, int *pairs
)
4268 o_div
= isl_basic_map_offset(bmap
, isl_dim_div
);
4269 lower_bound_from_parallel(bmap
, ineq
, lower
, o_div
+ div
, &c
);
4270 bmap
= isl_basic_map_fix(bmap
, isl_dim_div
, div
, c
);
4275 return isl_basic_map_drop_redundant_divs(bmap
);
4278 /* Remove divs that are not strictly needed based on the inequality
4280 * In particular, if a div only occurs positively (or negatively)
4281 * in constraints, then it can simply be dropped.
4282 * Also, if a div occurs in only two constraints and if moreover
4283 * those two constraints are opposite to each other, except for the constant
4284 * term and if the sum of the constant terms is such that for any value
4285 * of the other values, there is always at least one integer value of the
4286 * div, i.e., if one plus this sum is greater than or equal to
4287 * the (absolute value) of the coefficient of the div in the constraints,
4288 * then we can also simply drop the div.
4290 * If an existentially quantified variable does not have an explicit
4291 * representation, appears in only a single lower bound that does not
4292 * involve any other such existentially quantified variables and appears
4293 * in this lower bound with coefficient 1,
4294 * then fix the variable to the value of the lower bound. That is,
4295 * turn the inequality into an equality.
4296 * If for any value of the other variables, there is any value
4297 * for the existentially quantified variable satisfying the constraints,
4298 * then this lower bound also satisfies the constraints.
4299 * It is therefore safe to pick this lower bound.
4301 * The same reasoning holds even if the coefficient is not one.
4302 * However, fixing the variable to the value of the lower bound may
4303 * in general introduce an extra integer division, in which case
4304 * it may be better to pick another value.
4305 * If this integer division has a known constant value, then plugging
4306 * in this constant value removes the existentially quantified variable
4307 * completely. In particular, if the lower bound is of the form
4308 * ceil((-f(x) - c)/n) and there are two constraints, f(x) + c0 >= 0 and
4309 * -f(x) + c1 >= 0 such that ceil((-c1 - c)/n) = ceil((c0 - c)/n),
4310 * then the existentially quantified variable can be assigned this
4313 * We skip divs that appear in equalities or in the definition of other divs.
4314 * Divs that appear in the definition of other divs usually occur in at least
4315 * 4 constraints, but the constraints may have been simplified.
4317 * If any divs are left after these simple checks then we move on
4318 * to more complicated cases in drop_more_redundant_divs.
4320 static __isl_give isl_basic_map
*isl_basic_map_drop_redundant_divs_ineq(
4321 __isl_take isl_basic_map
*bmap
)
4330 if (bmap
->n_div
== 0)
4333 off
= isl_space_dim(bmap
->dim
, isl_dim_all
);
4334 pairs
= isl_calloc_array(bmap
->ctx
, int, bmap
->n_div
);
4338 for (i
= 0; i
< bmap
->n_div
; ++i
) {
4340 int last_pos
, last_neg
;
4344 defined
= !isl_int_is_zero(bmap
->div
[i
][0]);
4345 for (j
= i
; j
< bmap
->n_div
; ++j
)
4346 if (!isl_int_is_zero(bmap
->div
[j
][1 + 1 + off
+ i
]))
4348 if (j
< bmap
->n_div
)
4350 for (j
= 0; j
< bmap
->n_eq
; ++j
)
4351 if (!isl_int_is_zero(bmap
->eq
[j
][1 + off
+ i
]))
4357 for (j
= 0; j
< bmap
->n_ineq
; ++j
) {
4358 if (isl_int_is_pos(bmap
->ineq
[j
][1 + off
+ i
])) {
4362 if (isl_int_is_neg(bmap
->ineq
[j
][1 + off
+ i
])) {
4367 pairs
[i
] = pos
* neg
;
4368 if (pairs
[i
] == 0) {
4369 for (j
= bmap
->n_ineq
- 1; j
>= 0; --j
)
4370 if (!isl_int_is_zero(bmap
->ineq
[j
][1+off
+i
]))
4371 isl_basic_map_drop_inequality(bmap
, j
);
4372 bmap
= isl_basic_map_drop_div(bmap
, i
);
4373 return drop_redundant_divs_again(bmap
, pairs
, 0);
4375 if (pairs
[i
] != 1 || !is_opposite(bmap
, last_pos
, last_neg
)) {
4379 single
= single_unknown(bmap
, last_pos
, i
);
4382 if (has_coef_one(bmap
, i
, last_pos
))
4383 return set_eq_and_try_again(bmap
, last_pos
,
4385 lower
= lower_bound_is_cst(bmap
, i
, last_pos
);
4387 return fix_cst_lower(bmap
, i
, last_pos
, lower
,
4392 isl_int_add(bmap
->ineq
[last_pos
][0],
4393 bmap
->ineq
[last_pos
][0], bmap
->ineq
[last_neg
][0]);
4394 isl_int_add_ui(bmap
->ineq
[last_pos
][0],
4395 bmap
->ineq
[last_pos
][0], 1);
4396 redundant
= isl_int_ge(bmap
->ineq
[last_pos
][0],
4397 bmap
->ineq
[last_pos
][1+off
+i
]);
4398 isl_int_sub_ui(bmap
->ineq
[last_pos
][0],
4399 bmap
->ineq
[last_pos
][0], 1);
4400 isl_int_sub(bmap
->ineq
[last_pos
][0],
4401 bmap
->ineq
[last_pos
][0], bmap
->ineq
[last_neg
][0]);
4404 !ok_to_set_div_from_bound(bmap
, i
, last_pos
)) {
4409 bmap
= set_div_from_lower_bound(bmap
, i
, last_pos
);
4410 return drop_redundant_divs_again(bmap
, pairs
, 1);
4412 if (last_pos
> last_neg
) {
4413 isl_basic_map_drop_inequality(bmap
, last_pos
);
4414 isl_basic_map_drop_inequality(bmap
, last_neg
);
4416 isl_basic_map_drop_inequality(bmap
, last_neg
);
4417 isl_basic_map_drop_inequality(bmap
, last_pos
);
4419 bmap
= isl_basic_map_drop_div(bmap
, i
);
4420 return drop_redundant_divs_again(bmap
, pairs
, 0);
4424 return coalesce_or_drop_more_redundant_divs(bmap
, pairs
, n
);
4430 isl_basic_map_free(bmap
);
4434 /* Consider the coefficients at "c" as a row vector and replace
4435 * them with their product with "T". "T" is assumed to be a square matrix.
4437 static isl_stat
preimage(isl_int
*c
, __isl_keep isl_mat
*T
)
4444 return isl_stat_error
;
4445 n
= isl_mat_rows(T
);
4446 if (isl_seq_first_non_zero(c
, n
) == -1)
4448 ctx
= isl_mat_get_ctx(T
);
4449 v
= isl_vec_alloc(ctx
, n
);
4451 return isl_stat_error
;
4452 isl_seq_swp_or_cpy(v
->el
, c
, n
);
4453 v
= isl_vec_mat_product(v
, isl_mat_copy(T
));
4455 return isl_stat_error
;
4456 isl_seq_swp_or_cpy(c
, v
->el
, n
);
4462 /* Plug in T for the variables in "bmap" starting at "pos".
4463 * T is a linear unimodular matrix, i.e., without constant term.
4465 static __isl_give isl_basic_map
*isl_basic_map_preimage_vars(
4466 __isl_take isl_basic_map
*bmap
, unsigned pos
, __isl_take isl_mat
*T
)
4471 bmap
= isl_basic_map_cow(bmap
);
4475 n
= isl_mat_cols(T
);
4476 if (n
!= isl_mat_rows(T
))
4477 isl_die(isl_mat_get_ctx(T
), isl_error_invalid
,
4478 "expecting square matrix", goto error
);
4480 total
= isl_basic_map_dim(bmap
, isl_dim_all
);
4481 if (pos
+ n
> total
|| pos
+ n
< pos
)
4482 isl_die(isl_mat_get_ctx(T
), isl_error_invalid
,
4483 "invalid range", goto error
);
4485 for (i
= 0; i
< bmap
->n_eq
; ++i
)
4486 if (preimage(bmap
->eq
[i
] + 1 + pos
, T
) < 0)
4488 for (i
= 0; i
< bmap
->n_ineq
; ++i
)
4489 if (preimage(bmap
->ineq
[i
] + 1 + pos
, T
) < 0)
4491 for (i
= 0; i
< bmap
->n_div
; ++i
) {
4492 if (!isl_basic_map_div_is_known(bmap
, i
))
4494 if (preimage(bmap
->div
[i
] + 1 + 1 + pos
, T
) < 0)
4501 isl_basic_map_free(bmap
);
4506 /* Remove divs that are not strictly needed.
4508 * First look for an equality constraint involving two or more
4509 * existentially quantified variables without an explicit
4510 * representation. Replace the combination that appears
4511 * in the equality constraint by a single existentially quantified
4512 * variable such that the equality can be used to derive
4513 * an explicit representation for the variable.
4514 * If there are no more such equality constraints, then continue
4515 * with isl_basic_map_drop_redundant_divs_ineq.
4517 * In particular, if the equality constraint is of the form
4519 * f(x) + \sum_i c_i a_i = 0
4521 * with a_i existentially quantified variable without explicit
4522 * representation, then apply a transformation on the existentially
4523 * quantified variables to turn the constraint into
4527 * with g the gcd of the c_i.
4528 * In order to easily identify which existentially quantified variables
4529 * have a complete explicit representation, i.e., without being defined
4530 * in terms of other existentially quantified variables without
4531 * an explicit representation, the existentially quantified variables
4534 * The variable transformation is computed by extending the row
4535 * [c_1/g ... c_n/g] to a unimodular matrix, obtaining the transformation
4537 * [a_1'] [c_1/g ... c_n/g] [ a_1 ]
4542 * with [c_1/g ... c_n/g] representing the first row of U.
4543 * The inverse of U is then plugged into the original constraints.
4544 * The call to isl_basic_map_simplify makes sure the explicit
4545 * representation for a_1' is extracted from the equality constraint.
4547 __isl_give isl_basic_map
*isl_basic_map_drop_redundant_divs(
4548 __isl_take isl_basic_map
*bmap
)
4552 unsigned o_div
, n_div
;
4559 if (isl_basic_map_divs_known(bmap
))
4560 return isl_basic_map_drop_redundant_divs_ineq(bmap
);
4561 if (bmap
->n_eq
== 0)
4562 return isl_basic_map_drop_redundant_divs_ineq(bmap
);
4563 bmap
= isl_basic_map_sort_divs(bmap
);
4567 first
= isl_basic_map_first_unknown_div(bmap
);
4569 return isl_basic_map_free(bmap
);
4571 o_div
= isl_basic_map_offset(bmap
, isl_dim_div
);
4572 n_div
= isl_basic_map_dim(bmap
, isl_dim_div
);
4574 for (i
= 0; i
< bmap
->n_eq
; ++i
) {
4575 l
= isl_seq_first_non_zero(bmap
->eq
[i
] + o_div
+ first
,
4580 if (isl_seq_first_non_zero(bmap
->eq
[i
] + o_div
+ l
+ 1,
4581 n_div
- (l
+ 1)) == -1)
4585 if (i
>= bmap
->n_eq
)
4586 return isl_basic_map_drop_redundant_divs_ineq(bmap
);
4588 ctx
= isl_basic_map_get_ctx(bmap
);
4589 T
= isl_mat_alloc(ctx
, n_div
- l
, n_div
- l
);
4591 return isl_basic_map_free(bmap
);
4592 isl_seq_cpy(T
->row
[0], bmap
->eq
[i
] + o_div
+ l
, n_div
- l
);
4593 T
= isl_mat_normalize_row(T
, 0);
4594 T
= isl_mat_unimodular_complete(T
, 1);
4595 T
= isl_mat_right_inverse(T
);
4597 for (i
= l
; i
< n_div
; ++i
)
4598 bmap
= isl_basic_map_mark_div_unknown(bmap
, i
);
4599 bmap
= isl_basic_map_preimage_vars(bmap
, o_div
- 1 + l
, T
);
4600 bmap
= isl_basic_map_simplify(bmap
);
4602 return isl_basic_map_drop_redundant_divs(bmap
);
4605 struct isl_basic_set
*isl_basic_set_drop_redundant_divs(
4606 struct isl_basic_set
*bset
)
4608 return (struct isl_basic_set
*)
4609 isl_basic_map_drop_redundant_divs((struct isl_basic_map
*)bset
);
4612 struct isl_map
*isl_map_drop_redundant_divs(struct isl_map
*map
)
4618 for (i
= 0; i
< map
->n
; ++i
) {
4619 map
->p
[i
] = isl_basic_map_drop_redundant_divs(map
->p
[i
]);
4623 ISL_F_CLR(map
, ISL_MAP_NORMALIZED
);
4630 struct isl_set
*isl_set_drop_redundant_divs(struct isl_set
*set
)
4632 return (struct isl_set
*)
4633 isl_map_drop_redundant_divs((struct isl_map
*)set
);
4636 /* Does "bmap" satisfy any equality that involves more than 2 variables
4637 * and/or has coefficients different from -1 and 1?
4639 static int has_multiple_var_equality(__isl_keep isl_basic_map
*bmap
)
4644 total
= isl_basic_map_dim(bmap
, isl_dim_all
);
4646 for (i
= 0; i
< bmap
->n_eq
; ++i
) {
4649 j
= isl_seq_first_non_zero(bmap
->eq
[i
] + 1, total
);
4652 if (!isl_int_is_one(bmap
->eq
[i
][1 + j
]) &&
4653 !isl_int_is_negone(bmap
->eq
[i
][1 + j
]))
4657 k
= isl_seq_first_non_zero(bmap
->eq
[i
] + 1 + j
, total
- j
);
4661 if (!isl_int_is_one(bmap
->eq
[i
][1 + j
]) &&
4662 !isl_int_is_negone(bmap
->eq
[i
][1 + j
]))
4666 k
= isl_seq_first_non_zero(bmap
->eq
[i
] + 1 + j
, total
- j
);
4674 /* Remove any common factor g from the constraint coefficients in "v".
4675 * The constant term is stored in the first position and is replaced
4676 * by floor(c/g). If any common factor is removed and if this results
4677 * in a tightening of the constraint, then set *tightened.
4679 static __isl_give isl_vec
*normalize_constraint(__isl_take isl_vec
*v
,
4686 ctx
= isl_vec_get_ctx(v
);
4687 isl_seq_gcd(v
->el
+ 1, v
->size
- 1, &ctx
->normalize_gcd
);
4688 if (isl_int_is_zero(ctx
->normalize_gcd
))
4690 if (isl_int_is_one(ctx
->normalize_gcd
))
4695 if (tightened
&& !isl_int_is_divisible_by(v
->el
[0], ctx
->normalize_gcd
))
4697 isl_int_fdiv_q(v
->el
[0], v
->el
[0], ctx
->normalize_gcd
);
4698 isl_seq_scale_down(v
->el
+ 1, v
->el
+ 1, ctx
->normalize_gcd
,
4703 /* If "bmap" is an integer set that satisfies any equality involving
4704 * more than 2 variables and/or has coefficients different from -1 and 1,
4705 * then use variable compression to reduce the coefficients by removing
4706 * any (hidden) common factor.
4707 * In particular, apply the variable compression to each constraint,
4708 * factor out any common factor in the non-constant coefficients and
4709 * then apply the inverse of the compression.
4710 * At the end, we mark the basic map as having reduced constants.
4711 * If this flag is still set on the next invocation of this function,
4712 * then we skip the computation.
4714 * Removing a common factor may result in a tightening of some of
4715 * the constraints. If this happens, then we may end up with two
4716 * opposite inequalities that can be replaced by an equality.
4717 * We therefore call isl_basic_map_detect_inequality_pairs,
4718 * which checks for such pairs of inequalities as well as eliminate_divs_eq
4719 * and isl_basic_map_gauss if such a pair was found.
4721 __isl_give isl_basic_map
*isl_basic_map_reduce_coefficients(
4722 __isl_take isl_basic_map
*bmap
)
4727 isl_mat
*eq
, *T
, *T2
;
4733 if (ISL_F_ISSET(bmap
, ISL_BASIC_MAP_REDUCED_COEFFICIENTS
))
4735 if (isl_basic_map_is_rational(bmap
))
4737 if (bmap
->n_eq
== 0)
4739 if (!has_multiple_var_equality(bmap
))
4742 total
= isl_basic_map_dim(bmap
, isl_dim_all
);
4743 ctx
= isl_basic_map_get_ctx(bmap
);
4744 v
= isl_vec_alloc(ctx
, 1 + total
);
4746 return isl_basic_map_free(bmap
);
4748 eq
= isl_mat_sub_alloc6(ctx
, bmap
->eq
, 0, bmap
->n_eq
, 0, 1 + total
);
4749 T
= isl_mat_variable_compression(eq
, &T2
);
4752 if (T
->n_col
== 0) {
4756 return isl_basic_map_set_to_empty(bmap
);
4760 for (i
= 0; i
< bmap
->n_ineq
; ++i
) {
4761 isl_seq_cpy(v
->el
, bmap
->ineq
[i
], 1 + total
);
4762 v
= isl_vec_mat_product(v
, isl_mat_copy(T
));
4763 v
= normalize_constraint(v
, &tightened
);
4764 v
= isl_vec_mat_product(v
, isl_mat_copy(T2
));
4767 isl_seq_cpy(bmap
->ineq
[i
], v
->el
, 1 + total
);
4774 ISL_F_SET(bmap
, ISL_BASIC_MAP_REDUCED_COEFFICIENTS
);
4779 bmap
= isl_basic_map_detect_inequality_pairs(bmap
, &progress
);
4781 bmap
= eliminate_divs_eq(bmap
, &progress
);
4782 bmap
= isl_basic_map_gauss(bmap
, NULL
);
4791 return isl_basic_map_free(bmap
);
4794 /* Shift the integer division at position "div" of "bmap"
4795 * by "shift" times the variable at position "pos".
4796 * "pos" is as determined by isl_basic_map_offset, i.e., pos == 0
4797 * corresponds to the constant term.
4799 * That is, if the integer division has the form
4803 * then replace it by
4805 * floor((f(x) + shift * d * x_pos)/d) - shift * x_pos
4807 __isl_give isl_basic_map
*isl_basic_map_shift_div(
4808 __isl_take isl_basic_map
*bmap
, int div
, int pos
, isl_int shift
)
4816 total
= isl_basic_map_dim(bmap
, isl_dim_all
);
4817 total
-= isl_basic_map_dim(bmap
, isl_dim_div
);
4819 isl_int_addmul(bmap
->div
[div
][1 + pos
], shift
, bmap
->div
[div
][0]);
4821 for (i
= 0; i
< bmap
->n_eq
; ++i
) {
4822 if (isl_int_is_zero(bmap
->eq
[i
][1 + total
+ div
]))
4824 isl_int_submul(bmap
->eq
[i
][pos
],
4825 shift
, bmap
->eq
[i
][1 + total
+ div
]);
4827 for (i
= 0; i
< bmap
->n_ineq
; ++i
) {
4828 if (isl_int_is_zero(bmap
->ineq
[i
][1 + total
+ div
]))
4830 isl_int_submul(bmap
->ineq
[i
][pos
],
4831 shift
, bmap
->ineq
[i
][1 + total
+ div
]);
4833 for (i
= 0; i
< bmap
->n_div
; ++i
) {
4834 if (isl_int_is_zero(bmap
->div
[i
][0]))
4836 if (isl_int_is_zero(bmap
->div
[i
][1 + 1 + total
+ div
]))
4838 isl_int_submul(bmap
->div
[i
][1 + pos
],
4839 shift
, bmap
->div
[i
][1 + 1 + total
+ div
]);