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 the number of equality constraints in "bmap" that involve
2819 * local variables. This function assumes that Gaussian elimination
2820 * has been applied to the equality constraints.
2822 static int n_div_eq(__isl_keep isl_basic_map
*bmap
)
2830 if (bmap
->n_eq
== 0)
2833 total
= isl_basic_map_dim(bmap
, isl_dim_all
);
2834 n_div
= isl_basic_map_dim(bmap
, isl_dim_div
);
2837 for (i
= 0; i
< bmap
->n_eq
; ++i
)
2838 if (isl_seq_first_non_zero(bmap
->eq
[i
] + 1 + total
,
2845 /* Construct a basic map in "space" defined by the equality constraints in "eq".
2846 * The constraints are assumed not to involve any local variables.
2848 static __isl_give isl_basic_map
*basic_map_from_equalities(
2849 __isl_take isl_space
*space
, __isl_take isl_mat
*eq
)
2852 isl_basic_map
*bmap
= NULL
;
2857 if (1 + isl_space_dim(space
, isl_dim_all
) != eq
->n_col
)
2858 isl_die(isl_space_get_ctx(space
), isl_error_internal
,
2859 "unexpected number of columns", goto error
);
2861 bmap
= isl_basic_map_alloc_space(isl_space_copy(space
),
2863 for (i
= 0; i
< eq
->n_row
; ++i
) {
2864 k
= isl_basic_map_alloc_equality(bmap
);
2867 isl_seq_cpy(bmap
->eq
[k
], eq
->row
[i
], eq
->n_col
);
2870 isl_space_free(space
);
2874 isl_space_free(space
);
2876 isl_basic_map_free(bmap
);
2880 /* Construct and return a variable compression based on the equality
2881 * constraints in "bmap1" and "bmap2" that do not involve the local variables.
2882 * "n1" is the number of (initial) equality constraints in "bmap1"
2883 * that do involve local variables.
2884 * "n2" is the number of (initial) equality constraints in "bmap2"
2885 * that do involve local variables.
2886 * "total" is the total number of other variables.
2887 * This function assumes that Gaussian elimination
2888 * has been applied to the equality constraints in both "bmap1" and "bmap2"
2889 * such that the equality constraints not involving local variables
2890 * are those that start at "n1" or "n2".
2892 * If either of "bmap1" and "bmap2" does not have such equality constraints,
2893 * then simply compute the compression based on the equality constraints
2894 * in the other basic map.
2895 * Otherwise, combine the equality constraints from both into a new
2896 * basic map such that Gaussian elimination can be applied to this combination
2897 * and then construct a variable compression from the resulting
2898 * equality constraints.
2900 static __isl_give isl_mat
*combined_variable_compression(
2901 __isl_keep isl_basic_map
*bmap1
, int n1
,
2902 __isl_keep isl_basic_map
*bmap2
, int n2
, int total
)
2905 isl_mat
*E1
, *E2
, *V
;
2906 isl_basic_map
*bmap
;
2908 ctx
= isl_basic_map_get_ctx(bmap1
);
2909 if (bmap1
->n_eq
== n1
) {
2910 E2
= isl_mat_sub_alloc6(ctx
, bmap2
->eq
,
2911 n2
, bmap2
->n_eq
- n2
, 0, 1 + total
);
2912 return isl_mat_variable_compression(E2
, NULL
);
2914 if (bmap2
->n_eq
== n2
) {
2915 E1
= isl_mat_sub_alloc6(ctx
, bmap1
->eq
,
2916 n1
, bmap1
->n_eq
- n1
, 0, 1 + total
);
2917 return isl_mat_variable_compression(E1
, NULL
);
2919 E1
= isl_mat_sub_alloc6(ctx
, bmap1
->eq
,
2920 n1
, bmap1
->n_eq
- n1
, 0, 1 + total
);
2921 E2
= isl_mat_sub_alloc6(ctx
, bmap2
->eq
,
2922 n2
, bmap2
->n_eq
- n2
, 0, 1 + total
);
2923 E1
= isl_mat_concat(E1
, E2
);
2924 bmap
= basic_map_from_equalities(isl_basic_map_get_space(bmap1
), E1
);
2925 bmap
= isl_basic_map_gauss(bmap
, NULL
);
2928 E1
= isl_mat_sub_alloc6(ctx
, bmap
->eq
, 0, bmap
->n_eq
, 0, 1 + total
);
2929 V
= isl_mat_variable_compression(E1
, NULL
);
2930 isl_basic_map_free(bmap
);
2935 /* Extract the stride constraints from "bmap", compressed
2936 * with respect to both the stride constraints in "context" and
2937 * the remaining equality constraints in both "bmap" and "context".
2938 * "bmap_n_eq" is the number of (initial) stride constraints in "bmap".
2939 * "context_n_eq" is the number of (initial) stride constraints in "context".
2941 * Let x be all variables in "bmap" (and "context") other than the local
2942 * variables. First compute a variable compression
2946 * based on the non-stride equality constraints in "bmap" and "context".
2947 * Consider the stride constraints of "context",
2951 * with y the local variables and plug in the variable compression,
2954 * A(V x') + B(y) = 0
2956 * Use these constraints to compute a parameter compression on x'
2960 * Now consider the stride constraints of "bmap"
2964 * and plug in x = V*T x''.
2965 * That is, return A = [C*V*T D].
2967 static __isl_give isl_mat
*extract_compressed_stride_constraints(
2968 __isl_keep isl_basic_map
*bmap
, int bmap_n_eq
,
2969 __isl_keep isl_basic_map
*context
, int context_n_eq
)
2973 isl_mat
*A
, *B
, *T
, *V
;
2975 total
= isl_basic_map_dim(context
, isl_dim_all
);
2976 n_div
= isl_basic_map_dim(context
, isl_dim_div
);
2979 ctx
= isl_basic_map_get_ctx(bmap
);
2981 V
= combined_variable_compression(bmap
, bmap_n_eq
,
2982 context
, context_n_eq
, total
);
2984 A
= isl_mat_sub_alloc6(ctx
, context
->eq
, 0, context_n_eq
, 0, 1 + total
);
2985 B
= isl_mat_sub_alloc6(ctx
, context
->eq
,
2986 0, context_n_eq
, 1 + total
, n_div
);
2987 A
= isl_mat_product(A
, isl_mat_copy(V
));
2988 T
= isl_mat_parameter_compression_ext(A
, B
);
2989 T
= isl_mat_product(V
, T
);
2991 n_div
= isl_basic_map_dim(bmap
, isl_dim_div
);
2992 T
= isl_mat_diagonal(T
, isl_mat_identity(ctx
, n_div
));
2994 A
= isl_mat_sub_alloc6(ctx
, bmap
->eq
,
2995 0, bmap_n_eq
, 0, 1 + total
+ n_div
);
2996 A
= isl_mat_product(A
, T
);
3001 /* Remove the prime factors from *g that have an exponent that
3002 * is strictly smaller than the exponent in "c".
3003 * All exponents in *g are known to be smaller than or equal
3006 * That is, if *g is equal to
3008 * p_1^{e_1} p_2^{e_2} ... p_n^{e_n}
3010 * and "c" is equal to
3012 * p_1^{f_1} p_2^{f_2} ... p_n^{f_n}
3016 * p_1^{e_1 * (e_1 = f_1)} p_2^{e_2 * (e_2 = f_2)} ...
3017 * p_n^{e_n * (e_n = f_n)}
3019 * If e_i = f_i, then c / *g does not have any p_i factors and therefore
3020 * neither does the gcd of *g and c / *g.
3021 * If e_i < f_i, then the gcd of *g and c / *g has a positive
3022 * power min(e_i, s_i) of p_i with s_i = f_i - e_i among its factors.
3023 * Dividing *g by this gcd therefore strictly reduces the exponent
3024 * of the prime factors that need to be removed, while leaving the
3025 * other prime factors untouched.
3026 * Repeating this process until gcd(*g, c / *g) = 1 therefore
3027 * removes all undesired factors, without removing any others.
3029 static void remove_incomplete_powers(isl_int
*g
, isl_int c
)
3035 isl_int_divexact(t
, c
, *g
);
3036 isl_int_gcd(t
, t
, *g
);
3037 if (isl_int_is_one(t
))
3039 isl_int_divexact(*g
, *g
, t
);
3044 /* Reduce the "n" stride constraints in "bmap" based on a copy "A"
3045 * of the same stride constraints in a compressed space that exploits
3046 * all equalities in the context and the other equalities in "bmap".
3048 * If the stride constraints of "bmap" are of the form
3052 * then A is of the form
3056 * If any of these constraints involves only a single local variable y,
3057 * then the constraint appears as
3067 * Let g be the gcd of m and the coefficients of h.
3068 * Then, in particular, g is a divisor of the coefficients of h and
3072 * is known to be a multiple of g.
3073 * If some prime factor in m appears with the same exponent in g,
3074 * then it can be removed from m because f(x) is already known
3075 * to be a multiple of g and therefore in particular of this power
3076 * of the prime factors.
3077 * Prime factors that appear with a smaller exponent in g cannot
3078 * be removed from m.
3079 * Let g' be the divisor of g containing all prime factors that
3080 * appear with the same exponent in m and g, then
3084 * can be replaced by
3086 * f(x) + m/g' y_i' = 0
3088 * Note that (if g' != 1) this changes the explicit representation
3089 * of y_i to that of y_i', so the integer division at position i
3090 * is marked unknown and later recomputed by a call to
3091 * isl_basic_map_gauss.
3093 static __isl_give isl_basic_map
*reduce_stride_constraints(
3094 __isl_take isl_basic_map
*bmap
, int n
, __isl_keep isl_mat
*A
)
3102 return isl_basic_map_free(bmap
);
3104 total
= isl_basic_map_dim(bmap
, isl_dim_all
);
3105 n_div
= isl_basic_map_dim(bmap
, isl_dim_div
);
3109 for (i
= 0; i
< n
; ++i
) {
3112 div
= isl_seq_first_non_zero(bmap
->eq
[i
] + 1 + total
, n_div
);
3114 isl_die(isl_basic_map_get_ctx(bmap
), isl_error_internal
,
3115 "equality constraints modified unexpectedly",
3117 if (isl_seq_first_non_zero(bmap
->eq
[i
] + 1 + total
+ div
+ 1,
3118 n_div
- div
- 1) != -1)
3120 if (isl_mat_row_gcd(A
, i
, &gcd
) < 0)
3122 if (isl_int_is_one(gcd
))
3124 remove_incomplete_powers(&gcd
, bmap
->eq
[i
][1 + total
+ div
]);
3125 if (isl_int_is_one(gcd
))
3127 isl_int_divexact(bmap
->eq
[i
][1 + total
+ div
],
3128 bmap
->eq
[i
][1 + total
+ div
], gcd
);
3129 bmap
= isl_basic_map_mark_div_unknown(bmap
, div
);
3137 bmap
= isl_basic_map_gauss(bmap
, NULL
);
3142 isl_basic_map_free(bmap
);
3146 /* Simplify the stride constraints in "bmap" based on
3147 * the remaining equality constraints in "bmap" and all equality
3148 * constraints in "context".
3149 * Only do this if both "bmap" and "context" have stride constraints.
3151 * First extract a copy of the stride constraints in "bmap" in a compressed
3152 * space exploiting all the other equality constraints and then
3153 * use this compressed copy to simplify the original stride constraints.
3155 static __isl_give isl_basic_map
*gist_strides(__isl_take isl_basic_map
*bmap
,
3156 __isl_keep isl_basic_map
*context
)
3158 int bmap_n_eq
, context_n_eq
;
3161 if (!bmap
|| !context
)
3162 return isl_basic_map_free(bmap
);
3164 bmap_n_eq
= n_div_eq(bmap
);
3165 context_n_eq
= n_div_eq(context
);
3167 if (bmap_n_eq
< 0 || context_n_eq
< 0)
3168 return isl_basic_map_free(bmap
);
3169 if (bmap_n_eq
== 0 || context_n_eq
== 0)
3172 A
= extract_compressed_stride_constraints(bmap
, bmap_n_eq
,
3173 context
, context_n_eq
);
3174 bmap
= reduce_stride_constraints(bmap
, bmap_n_eq
, A
);
3181 /* Return a basic map that has the same intersection with "context" as "bmap"
3182 * and that is as "simple" as possible.
3184 * The core computation is performed on the pure constraints.
3185 * When we add back the meaning of the integer divisions, we need
3186 * to (re)introduce the div constraints. If we happen to have
3187 * discovered that some of these integer divisions are equal to
3188 * some affine combination of other variables, then these div
3189 * constraints may end up getting simplified in terms of the equalities,
3190 * resulting in extra inequalities on the other variables that
3191 * may have been removed already or that may not even have been
3192 * part of the input. We try and remove those constraints of
3193 * this form that are most obviously redundant with respect to
3194 * the context. We also remove those div constraints that are
3195 * redundant with respect to the other constraints in the result.
3197 * The stride constraints among the equality constraints in "bmap" are
3198 * also simplified with respecting to the other equality constraints
3199 * in "bmap" and with respect to all equality constraints in "context".
3201 struct isl_basic_map
*isl_basic_map_gist(struct isl_basic_map
*bmap
,
3202 struct isl_basic_map
*context
)
3204 isl_basic_set
*bset
, *eq
;
3205 isl_basic_map
*eq_bmap
;
3206 unsigned total
, n_div
, extra
, n_eq
, n_ineq
;
3208 if (!bmap
|| !context
)
3211 if (isl_basic_map_plain_is_universe(bmap
)) {
3212 isl_basic_map_free(context
);
3215 if (isl_basic_map_plain_is_empty(context
)) {
3216 isl_space
*space
= isl_basic_map_get_space(bmap
);
3217 isl_basic_map_free(bmap
);
3218 isl_basic_map_free(context
);
3219 return isl_basic_map_universe(space
);
3221 if (isl_basic_map_plain_is_empty(bmap
)) {
3222 isl_basic_map_free(context
);
3226 bmap
= isl_basic_map_remove_redundancies(bmap
);
3227 context
= isl_basic_map_remove_redundancies(context
);
3231 context
= isl_basic_map_align_divs(context
, bmap
);
3232 n_div
= isl_basic_map_dim(context
, isl_dim_div
);
3233 total
= isl_basic_map_dim(bmap
, isl_dim_all
);
3234 extra
= n_div
- isl_basic_map_dim(bmap
, isl_dim_div
);
3236 bset
= isl_basic_map_underlying_set(isl_basic_map_copy(bmap
));
3237 bset
= isl_basic_set_add_dims(bset
, isl_dim_set
, extra
);
3238 bset
= uset_gist(bset
,
3239 isl_basic_map_underlying_set(isl_basic_map_copy(context
)));
3240 bset
= isl_basic_set_project_out(bset
, isl_dim_set
, total
, extra
);
3242 if (!bset
|| bset
->n_eq
== 0 || n_div
== 0 ||
3243 isl_basic_set_plain_is_empty(bset
)) {
3244 isl_basic_map_free(context
);
3245 return isl_basic_map_overlying_set(bset
, bmap
);
3249 n_ineq
= bset
->n_ineq
;
3250 eq
= isl_basic_set_copy(bset
);
3251 eq
= isl_basic_set_cow(eq
);
3252 if (isl_basic_set_free_inequality(eq
, n_ineq
) < 0)
3253 eq
= isl_basic_set_free(eq
);
3254 if (isl_basic_set_free_equality(bset
, n_eq
) < 0)
3255 bset
= isl_basic_set_free(bset
);
3257 eq_bmap
= isl_basic_map_overlying_set(eq
, isl_basic_map_copy(bmap
));
3258 eq_bmap
= gist_strides(eq_bmap
, context
);
3259 eq_bmap
= isl_basic_map_remove_shifted_constraints(eq_bmap
, context
);
3260 bmap
= isl_basic_map_overlying_set(bset
, bmap
);
3261 bmap
= isl_basic_map_intersect(bmap
, eq_bmap
);
3262 bmap
= isl_basic_map_remove_redundancies(bmap
);
3266 isl_basic_map_free(bmap
);
3267 isl_basic_map_free(context
);
3272 * Assumes context has no implicit divs.
3274 __isl_give isl_map
*isl_map_gist_basic_map(__isl_take isl_map
*map
,
3275 __isl_take isl_basic_map
*context
)
3279 if (!map
|| !context
)
3282 if (isl_basic_map_plain_is_empty(context
)) {
3283 isl_space
*space
= isl_map_get_space(map
);
3285 isl_basic_map_free(context
);
3286 return isl_map_universe(space
);
3289 context
= isl_basic_map_remove_redundancies(context
);
3290 map
= isl_map_cow(map
);
3291 if (!map
|| !context
)
3293 isl_assert(map
->ctx
, isl_space_is_equal(map
->dim
, context
->dim
), goto error
);
3294 map
= isl_map_compute_divs(map
);
3297 for (i
= map
->n
- 1; i
>= 0; --i
) {
3298 map
->p
[i
] = isl_basic_map_gist(map
->p
[i
],
3299 isl_basic_map_copy(context
));
3302 if (isl_basic_map_plain_is_empty(map
->p
[i
])) {
3303 isl_basic_map_free(map
->p
[i
]);
3304 if (i
!= map
->n
- 1)
3305 map
->p
[i
] = map
->p
[map
->n
- 1];
3309 isl_basic_map_free(context
);
3310 ISL_F_CLR(map
, ISL_MAP_NORMALIZED
);
3314 isl_basic_map_free(context
);
3318 /* Drop all inequalities from "bmap" that also appear in "context".
3319 * "context" is assumed to have only known local variables and
3320 * the initial local variables of "bmap" are assumed to be the same
3321 * as those of "context".
3322 * The constraints of both "bmap" and "context" are assumed
3323 * to have been sorted using isl_basic_map_sort_constraints.
3325 * Run through the inequality constraints of "bmap" and "context"
3327 * If a constraint of "bmap" involves variables not in "context",
3328 * then it cannot appear in "context".
3329 * If a matching constraint is found, it is removed from "bmap".
3331 static __isl_give isl_basic_map
*drop_inequalities(
3332 __isl_take isl_basic_map
*bmap
, __isl_keep isl_basic_map
*context
)
3335 unsigned total
, extra
;
3337 if (!bmap
|| !context
)
3338 return isl_basic_map_free(bmap
);
3340 total
= isl_basic_map_total_dim(context
);
3341 extra
= isl_basic_map_total_dim(bmap
) - total
;
3343 i1
= bmap
->n_ineq
- 1;
3344 i2
= context
->n_ineq
- 1;
3345 while (bmap
&& i1
>= 0 && i2
>= 0) {
3348 if (isl_seq_first_non_zero(bmap
->ineq
[i1
] + 1 + total
,
3353 cmp
= isl_basic_map_constraint_cmp(context
, bmap
->ineq
[i1
],
3363 if (isl_int_eq(bmap
->ineq
[i1
][0], context
->ineq
[i2
][0])) {
3364 bmap
= isl_basic_map_cow(bmap
);
3365 if (isl_basic_map_drop_inequality(bmap
, i1
) < 0)
3366 bmap
= isl_basic_map_free(bmap
);
3375 /* Drop all equalities from "bmap" that also appear in "context".
3376 * "context" is assumed to have only known local variables and
3377 * the initial local variables of "bmap" are assumed to be the same
3378 * as those of "context".
3380 * Run through the equality constraints of "bmap" and "context"
3382 * If a constraint of "bmap" involves variables not in "context",
3383 * then it cannot appear in "context".
3384 * If a matching constraint is found, it is removed from "bmap".
3386 static __isl_give isl_basic_map
*drop_equalities(
3387 __isl_take isl_basic_map
*bmap
, __isl_keep isl_basic_map
*context
)
3390 unsigned total
, extra
;
3392 if (!bmap
|| !context
)
3393 return isl_basic_map_free(bmap
);
3395 total
= isl_basic_map_total_dim(context
);
3396 extra
= isl_basic_map_total_dim(bmap
) - total
;
3398 i1
= bmap
->n_eq
- 1;
3399 i2
= context
->n_eq
- 1;
3401 while (bmap
&& i1
>= 0 && i2
>= 0) {
3404 if (isl_seq_first_non_zero(bmap
->eq
[i1
] + 1 + total
,
3407 last1
= isl_seq_last_non_zero(bmap
->eq
[i1
] + 1, total
);
3408 last2
= isl_seq_last_non_zero(context
->eq
[i2
] + 1, total
);
3409 if (last1
> last2
) {
3413 if (last1
< last2
) {
3417 if (isl_seq_eq(bmap
->eq
[i1
], context
->eq
[i2
], 1 + total
)) {
3418 bmap
= isl_basic_map_cow(bmap
);
3419 if (isl_basic_map_drop_equality(bmap
, i1
) < 0)
3420 bmap
= isl_basic_map_free(bmap
);
3429 /* Remove the constraints in "context" from "bmap".
3430 * "context" is assumed to have explicit representations
3431 * for all local variables.
3433 * First align the divs of "bmap" to those of "context" and
3434 * sort the constraints. Then drop all constraints from "bmap"
3435 * that appear in "context".
3437 __isl_give isl_basic_map
*isl_basic_map_plain_gist(
3438 __isl_take isl_basic_map
*bmap
, __isl_take isl_basic_map
*context
)
3440 isl_bool done
, known
;
3442 done
= isl_basic_map_plain_is_universe(context
);
3443 if (done
== isl_bool_false
)
3444 done
= isl_basic_map_plain_is_universe(bmap
);
3445 if (done
== isl_bool_false
)
3446 done
= isl_basic_map_plain_is_empty(context
);
3447 if (done
== isl_bool_false
)
3448 done
= isl_basic_map_plain_is_empty(bmap
);
3452 isl_basic_map_free(context
);
3455 known
= isl_basic_map_divs_known(context
);
3459 isl_die(isl_basic_map_get_ctx(bmap
), isl_error_invalid
,
3460 "context has unknown divs", goto error
);
3462 bmap
= isl_basic_map_align_divs(bmap
, context
);
3463 bmap
= isl_basic_map_gauss(bmap
, NULL
);
3464 bmap
= isl_basic_map_sort_constraints(bmap
);
3465 context
= isl_basic_map_sort_constraints(context
);
3467 bmap
= drop_inequalities(bmap
, context
);
3468 bmap
= drop_equalities(bmap
, context
);
3470 isl_basic_map_free(context
);
3471 bmap
= isl_basic_map_finalize(bmap
);
3474 isl_basic_map_free(bmap
);
3475 isl_basic_map_free(context
);
3479 /* Replace "map" by the disjunct at position "pos" and free "context".
3481 static __isl_give isl_map
*replace_by_disjunct(__isl_take isl_map
*map
,
3482 int pos
, __isl_take isl_basic_map
*context
)
3484 isl_basic_map
*bmap
;
3486 bmap
= isl_basic_map_copy(map
->p
[pos
]);
3488 isl_basic_map_free(context
);
3489 return isl_map_from_basic_map(bmap
);
3492 /* Remove the constraints in "context" from "map".
3493 * If any of the disjuncts in the result turns out to be the universe,
3494 * then return this universe.
3495 * "context" is assumed to have explicit representations
3496 * for all local variables.
3498 __isl_give isl_map
*isl_map_plain_gist_basic_map(__isl_take isl_map
*map
,
3499 __isl_take isl_basic_map
*context
)
3502 isl_bool univ
, known
;
3504 univ
= isl_basic_map_plain_is_universe(context
);
3508 isl_basic_map_free(context
);
3511 known
= isl_basic_map_divs_known(context
);
3515 isl_die(isl_map_get_ctx(map
), isl_error_invalid
,
3516 "context has unknown divs", goto error
);
3518 map
= isl_map_cow(map
);
3521 for (i
= 0; i
< map
->n
; ++i
) {
3522 map
->p
[i
] = isl_basic_map_plain_gist(map
->p
[i
],
3523 isl_basic_map_copy(context
));
3524 univ
= isl_basic_map_plain_is_universe(map
->p
[i
]);
3527 if (univ
&& map
->n
> 1)
3528 return replace_by_disjunct(map
, i
, context
);
3531 isl_basic_map_free(context
);
3532 ISL_F_CLR(map
, ISL_MAP_NORMALIZED
);
3534 ISL_F_CLR(map
, ISL_MAP_DISJOINT
);
3538 isl_basic_map_free(context
);
3542 /* Replace "map" by a universe map in the same space and free "drop".
3544 static __isl_give isl_map
*replace_by_universe(__isl_take isl_map
*map
,
3545 __isl_take isl_map
*drop
)
3549 res
= isl_map_universe(isl_map_get_space(map
));
3555 /* Return a map that has the same intersection with "context" as "map"
3556 * and that is as "simple" as possible.
3558 * If "map" is already the universe, then we cannot make it any simpler.
3559 * Similarly, if "context" is the universe, then we cannot exploit it
3561 * If "map" and "context" are identical to each other, then we can
3562 * return the corresponding universe.
3564 * If either "map" or "context" consists of multiple disjuncts,
3565 * then check if "context" happens to be a subset of "map",
3566 * in which case all constraints can be removed.
3567 * In case of multiple disjuncts, the standard procedure
3568 * may not be able to detect that all constraints can be removed.
3570 * If none of these cases apply, we have to work a bit harder.
3571 * During this computation, we make use of a single disjunct context,
3572 * so if the original context consists of more than one disjunct
3573 * then we need to approximate the context by a single disjunct set.
3574 * Simply taking the simple hull may drop constraints that are
3575 * only implicitly available in each disjunct. We therefore also
3576 * look for constraints among those defining "map" that are valid
3577 * for the context. These can then be used to simplify away
3578 * the corresponding constraints in "map".
3580 static __isl_give isl_map
*map_gist(__isl_take isl_map
*map
,
3581 __isl_take isl_map
*context
)
3585 int single_disjunct_map
, single_disjunct_context
;
3587 isl_basic_map
*hull
;
3589 is_universe
= isl_map_plain_is_universe(map
);
3590 if (is_universe
>= 0 && !is_universe
)
3591 is_universe
= isl_map_plain_is_universe(context
);
3592 if (is_universe
< 0)
3595 isl_map_free(context
);
3599 equal
= isl_map_plain_is_equal(map
, context
);
3603 return replace_by_universe(map
, context
);
3605 single_disjunct_map
= isl_map_n_basic_map(map
) == 1;
3606 single_disjunct_context
= isl_map_n_basic_map(context
) == 1;
3607 if (!single_disjunct_map
|| !single_disjunct_context
) {
3608 subset
= isl_map_is_subset(context
, map
);
3612 return replace_by_universe(map
, context
);
3615 context
= isl_map_compute_divs(context
);
3618 if (single_disjunct_context
) {
3619 hull
= isl_map_simple_hull(context
);
3624 ctx
= isl_map_get_ctx(map
);
3625 list
= isl_map_list_alloc(ctx
, 2);
3626 list
= isl_map_list_add(list
, isl_map_copy(context
));
3627 list
= isl_map_list_add(list
, isl_map_copy(map
));
3628 hull
= isl_map_unshifted_simple_hull_from_map_list(context
,
3631 return isl_map_gist_basic_map(map
, hull
);
3634 isl_map_free(context
);
3638 __isl_give isl_map
*isl_map_gist(__isl_take isl_map
*map
,
3639 __isl_take isl_map
*context
)
3641 return isl_map_align_params_map_map_and(map
, context
, &map_gist
);
3644 struct isl_basic_set
*isl_basic_set_gist(struct isl_basic_set
*bset
,
3645 struct isl_basic_set
*context
)
3647 return (struct isl_basic_set
*)isl_basic_map_gist(
3648 (struct isl_basic_map
*)bset
, (struct isl_basic_map
*)context
);
3651 __isl_give isl_set
*isl_set_gist_basic_set(__isl_take isl_set
*set
,
3652 __isl_take isl_basic_set
*context
)
3654 return (struct isl_set
*)isl_map_gist_basic_map((struct isl_map
*)set
,
3655 (struct isl_basic_map
*)context
);
3658 __isl_give isl_set
*isl_set_gist_params_basic_set(__isl_take isl_set
*set
,
3659 __isl_take isl_basic_set
*context
)
3661 isl_space
*space
= isl_set_get_space(set
);
3662 isl_basic_set
*dom_context
= isl_basic_set_universe(space
);
3663 dom_context
= isl_basic_set_intersect_params(dom_context
, context
);
3664 return isl_set_gist_basic_set(set
, dom_context
);
3667 __isl_give isl_set
*isl_set_gist(__isl_take isl_set
*set
,
3668 __isl_take isl_set
*context
)
3670 return (struct isl_set
*)isl_map_gist((struct isl_map
*)set
,
3671 (struct isl_map
*)context
);
3674 /* Compute the gist of "bmap" with respect to the constraints "context"
3677 __isl_give isl_basic_map
*isl_basic_map_gist_domain(
3678 __isl_take isl_basic_map
*bmap
, __isl_take isl_basic_set
*context
)
3680 isl_space
*space
= isl_basic_map_get_space(bmap
);
3681 isl_basic_map
*bmap_context
= isl_basic_map_universe(space
);
3683 bmap_context
= isl_basic_map_intersect_domain(bmap_context
, context
);
3684 return isl_basic_map_gist(bmap
, bmap_context
);
3687 __isl_give isl_map
*isl_map_gist_domain(__isl_take isl_map
*map
,
3688 __isl_take isl_set
*context
)
3690 isl_map
*map_context
= isl_map_universe(isl_map_get_space(map
));
3691 map_context
= isl_map_intersect_domain(map_context
, context
);
3692 return isl_map_gist(map
, map_context
);
3695 __isl_give isl_map
*isl_map_gist_range(__isl_take isl_map
*map
,
3696 __isl_take isl_set
*context
)
3698 isl_map
*map_context
= isl_map_universe(isl_map_get_space(map
));
3699 map_context
= isl_map_intersect_range(map_context
, context
);
3700 return isl_map_gist(map
, map_context
);
3703 __isl_give isl_map
*isl_map_gist_params(__isl_take isl_map
*map
,
3704 __isl_take isl_set
*context
)
3706 isl_map
*map_context
= isl_map_universe(isl_map_get_space(map
));
3707 map_context
= isl_map_intersect_params(map_context
, context
);
3708 return isl_map_gist(map
, map_context
);
3711 __isl_give isl_set
*isl_set_gist_params(__isl_take isl_set
*set
,
3712 __isl_take isl_set
*context
)
3714 return isl_map_gist_params(set
, context
);
3717 /* Quick check to see if two basic maps are disjoint.
3718 * In particular, we reduce the equalities and inequalities of
3719 * one basic map in the context of the equalities of the other
3720 * basic map and check if we get a contradiction.
3722 isl_bool
isl_basic_map_plain_is_disjoint(__isl_keep isl_basic_map
*bmap1
,
3723 __isl_keep isl_basic_map
*bmap2
)
3725 struct isl_vec
*v
= NULL
;
3730 if (!bmap1
|| !bmap2
)
3731 return isl_bool_error
;
3732 isl_assert(bmap1
->ctx
, isl_space_is_equal(bmap1
->dim
, bmap2
->dim
),
3733 return isl_bool_error
);
3734 if (bmap1
->n_div
|| bmap2
->n_div
)
3735 return isl_bool_false
;
3736 if (!bmap1
->n_eq
&& !bmap2
->n_eq
)
3737 return isl_bool_false
;
3739 total
= isl_space_dim(bmap1
->dim
, isl_dim_all
);
3741 return isl_bool_false
;
3742 v
= isl_vec_alloc(bmap1
->ctx
, 1 + total
);
3745 elim
= isl_alloc_array(bmap1
->ctx
, int, total
);
3748 compute_elimination_index(bmap1
, elim
);
3749 for (i
= 0; i
< bmap2
->n_eq
; ++i
) {
3751 reduced
= reduced_using_equalities(v
->block
.data
, bmap2
->eq
[i
],
3753 if (reduced
&& !isl_int_is_zero(v
->block
.data
[0]) &&
3754 isl_seq_first_non_zero(v
->block
.data
+ 1, total
) == -1)
3757 for (i
= 0; i
< bmap2
->n_ineq
; ++i
) {
3759 reduced
= reduced_using_equalities(v
->block
.data
,
3760 bmap2
->ineq
[i
], bmap1
, elim
);
3761 if (reduced
&& isl_int_is_neg(v
->block
.data
[0]) &&
3762 isl_seq_first_non_zero(v
->block
.data
+ 1, total
) == -1)
3765 compute_elimination_index(bmap2
, elim
);
3766 for (i
= 0; i
< bmap1
->n_ineq
; ++i
) {
3768 reduced
= reduced_using_equalities(v
->block
.data
,
3769 bmap1
->ineq
[i
], bmap2
, elim
);
3770 if (reduced
&& isl_int_is_neg(v
->block
.data
[0]) &&
3771 isl_seq_first_non_zero(v
->block
.data
+ 1, total
) == -1)
3776 return isl_bool_false
;
3780 return isl_bool_true
;
3784 return isl_bool_error
;
3787 int isl_basic_set_plain_is_disjoint(__isl_keep isl_basic_set
*bset1
,
3788 __isl_keep isl_basic_set
*bset2
)
3790 return isl_basic_map_plain_is_disjoint((struct isl_basic_map
*)bset1
,
3791 (struct isl_basic_map
*)bset2
);
3794 /* Does "test" hold for all pairs of basic maps in "map1" and "map2"?
3796 static isl_bool
all_pairs(__isl_keep isl_map
*map1
, __isl_keep isl_map
*map2
,
3797 isl_bool (*test
)(__isl_keep isl_basic_map
*bmap1
,
3798 __isl_keep isl_basic_map
*bmap2
))
3803 return isl_bool_error
;
3805 for (i
= 0; i
< map1
->n
; ++i
) {
3806 for (j
= 0; j
< map2
->n
; ++j
) {
3807 isl_bool d
= test(map1
->p
[i
], map2
->p
[j
]);
3808 if (d
!= isl_bool_true
)
3813 return isl_bool_true
;
3816 /* Are "map1" and "map2" obviously disjoint, based on information
3817 * that can be derived without looking at the individual basic maps?
3819 * In particular, if one of them is empty or if they live in different spaces
3820 * (ignoring parameters), then they are clearly disjoint.
3822 static isl_bool
isl_map_plain_is_disjoint_global(__isl_keep isl_map
*map1
,
3823 __isl_keep isl_map
*map2
)
3829 return isl_bool_error
;
3831 disjoint
= isl_map_plain_is_empty(map1
);
3832 if (disjoint
< 0 || disjoint
)
3835 disjoint
= isl_map_plain_is_empty(map2
);
3836 if (disjoint
< 0 || disjoint
)
3839 match
= isl_space_tuple_is_equal(map1
->dim
, isl_dim_in
,
3840 map2
->dim
, isl_dim_in
);
3841 if (match
< 0 || !match
)
3842 return match
< 0 ? isl_bool_error
: isl_bool_true
;
3844 match
= isl_space_tuple_is_equal(map1
->dim
, isl_dim_out
,
3845 map2
->dim
, isl_dim_out
);
3846 if (match
< 0 || !match
)
3847 return match
< 0 ? isl_bool_error
: isl_bool_true
;
3849 return isl_bool_false
;
3852 /* Are "map1" and "map2" obviously disjoint?
3854 * If one of them is empty or if they live in different spaces (ignoring
3855 * parameters), then they are clearly disjoint.
3856 * This is checked by isl_map_plain_is_disjoint_global.
3858 * If they have different parameters, then we skip any further tests.
3860 * If they are obviously equal, but not obviously empty, then we will
3861 * not be able to detect if they are disjoint.
3863 * Otherwise we check if each basic map in "map1" is obviously disjoint
3864 * from each basic map in "map2".
3866 isl_bool
isl_map_plain_is_disjoint(__isl_keep isl_map
*map1
,
3867 __isl_keep isl_map
*map2
)
3873 disjoint
= isl_map_plain_is_disjoint_global(map1
, map2
);
3874 if (disjoint
< 0 || disjoint
)
3877 match
= isl_space_match(map1
->dim
, isl_dim_param
,
3878 map2
->dim
, isl_dim_param
);
3879 if (match
< 0 || !match
)
3880 return match
< 0 ? isl_bool_error
: isl_bool_false
;
3882 intersect
= isl_map_plain_is_equal(map1
, map2
);
3883 if (intersect
< 0 || intersect
)
3884 return intersect
< 0 ? isl_bool_error
: isl_bool_false
;
3886 return all_pairs(map1
, map2
, &isl_basic_map_plain_is_disjoint
);
3889 /* Are "map1" and "map2" disjoint?
3891 * They are disjoint if they are "obviously disjoint" or if one of them
3892 * is empty. Otherwise, they are not disjoint if one of them is universal.
3893 * If the two inputs are (obviously) equal and not empty, then they are
3895 * If none of these cases apply, then check if all pairs of basic maps
3898 isl_bool
isl_map_is_disjoint(__isl_keep isl_map
*map1
, __isl_keep isl_map
*map2
)
3903 disjoint
= isl_map_plain_is_disjoint_global(map1
, map2
);
3904 if (disjoint
< 0 || disjoint
)
3907 disjoint
= isl_map_is_empty(map1
);
3908 if (disjoint
< 0 || disjoint
)
3911 disjoint
= isl_map_is_empty(map2
);
3912 if (disjoint
< 0 || disjoint
)
3915 intersect
= isl_map_plain_is_universe(map1
);
3916 if (intersect
< 0 || intersect
)
3917 return intersect
< 0 ? isl_bool_error
: isl_bool_false
;
3919 intersect
= isl_map_plain_is_universe(map2
);
3920 if (intersect
< 0 || intersect
)
3921 return intersect
< 0 ? isl_bool_error
: isl_bool_false
;
3923 intersect
= isl_map_plain_is_equal(map1
, map2
);
3924 if (intersect
< 0 || intersect
)
3925 return isl_bool_not(intersect
);
3927 return all_pairs(map1
, map2
, &isl_basic_map_is_disjoint
);
3930 /* Are "bmap1" and "bmap2" disjoint?
3932 * They are disjoint if they are "obviously disjoint" or if one of them
3933 * is empty. Otherwise, they are not disjoint if one of them is universal.
3934 * If none of these cases apply, we compute the intersection and see if
3935 * the result is empty.
3937 isl_bool
isl_basic_map_is_disjoint(__isl_keep isl_basic_map
*bmap1
,
3938 __isl_keep isl_basic_map
*bmap2
)
3942 isl_basic_map
*test
;
3944 disjoint
= isl_basic_map_plain_is_disjoint(bmap1
, bmap2
);
3945 if (disjoint
< 0 || disjoint
)
3948 disjoint
= isl_basic_map_is_empty(bmap1
);
3949 if (disjoint
< 0 || disjoint
)
3952 disjoint
= isl_basic_map_is_empty(bmap2
);
3953 if (disjoint
< 0 || disjoint
)
3956 intersect
= isl_basic_map_plain_is_universe(bmap1
);
3957 if (intersect
< 0 || intersect
)
3958 return intersect
< 0 ? isl_bool_error
: isl_bool_false
;
3960 intersect
= isl_basic_map_plain_is_universe(bmap2
);
3961 if (intersect
< 0 || intersect
)
3962 return intersect
< 0 ? isl_bool_error
: isl_bool_false
;
3964 test
= isl_basic_map_intersect(isl_basic_map_copy(bmap1
),
3965 isl_basic_map_copy(bmap2
));
3966 disjoint
= isl_basic_map_is_empty(test
);
3967 isl_basic_map_free(test
);
3972 /* Are "bset1" and "bset2" disjoint?
3974 isl_bool
isl_basic_set_is_disjoint(__isl_keep isl_basic_set
*bset1
,
3975 __isl_keep isl_basic_set
*bset2
)
3977 return isl_basic_map_is_disjoint(bset1
, bset2
);
3980 isl_bool
isl_set_plain_is_disjoint(__isl_keep isl_set
*set1
,
3981 __isl_keep isl_set
*set2
)
3983 return isl_map_plain_is_disjoint((struct isl_map
*)set1
,
3984 (struct isl_map
*)set2
);
3987 /* Are "set1" and "set2" disjoint?
3989 isl_bool
isl_set_is_disjoint(__isl_keep isl_set
*set1
, __isl_keep isl_set
*set2
)
3991 return isl_map_is_disjoint(set1
, set2
);
3994 /* Is "v" equal to 0, 1 or -1?
3996 static int is_zero_or_one(isl_int v
)
3998 return isl_int_is_zero(v
) || isl_int_is_one(v
) || isl_int_is_negone(v
);
4001 /* Check if we can combine a given div with lower bound l and upper
4002 * bound u with some other div and if so return that other div.
4003 * Otherwise return -1.
4005 * We first check that
4006 * - the bounds are opposites of each other (except for the constant
4008 * - the bounds do not reference any other div
4009 * - no div is defined in terms of this div
4011 * Let m be the size of the range allowed on the div by the bounds.
4012 * That is, the bounds are of the form
4014 * e <= a <= e + m - 1
4016 * with e some expression in the other variables.
4017 * We look for another div b such that no third div is defined in terms
4018 * of this second div b and such that in any constraint that contains
4019 * a (except for the given lower and upper bound), also contains b
4020 * with a coefficient that is m times that of b.
4021 * That is, all constraints (execpt for the lower and upper bound)
4024 * e + f (a + m b) >= 0
4026 * Furthermore, in the constraints that only contain b, the coefficient
4027 * of b should be equal to 1 or -1.
4028 * If so, we return b so that "a + m b" can be replaced by
4029 * a single div "c = a + m b".
4031 static int div_find_coalesce(struct isl_basic_map
*bmap
, int *pairs
,
4032 unsigned div
, unsigned l
, unsigned u
)
4038 if (bmap
->n_div
<= 1)
4040 dim
= isl_space_dim(bmap
->dim
, isl_dim_all
);
4041 if (isl_seq_first_non_zero(bmap
->ineq
[l
] + 1 + dim
, div
) != -1)
4043 if (isl_seq_first_non_zero(bmap
->ineq
[l
] + 1 + dim
+ div
+ 1,
4044 bmap
->n_div
- div
- 1) != -1)
4046 if (!isl_seq_is_neg(bmap
->ineq
[l
] + 1, bmap
->ineq
[u
] + 1,
4050 for (i
= 0; i
< bmap
->n_div
; ++i
) {
4051 if (isl_int_is_zero(bmap
->div
[i
][0]))
4053 if (!isl_int_is_zero(bmap
->div
[i
][1 + 1 + dim
+ div
]))
4057 isl_int_add(bmap
->ineq
[l
][0], bmap
->ineq
[l
][0], bmap
->ineq
[u
][0]);
4058 if (isl_int_is_neg(bmap
->ineq
[l
][0])) {
4059 isl_int_sub(bmap
->ineq
[l
][0],
4060 bmap
->ineq
[l
][0], bmap
->ineq
[u
][0]);
4061 bmap
= isl_basic_map_copy(bmap
);
4062 bmap
= isl_basic_map_set_to_empty(bmap
);
4063 isl_basic_map_free(bmap
);
4066 isl_int_add_ui(bmap
->ineq
[l
][0], bmap
->ineq
[l
][0], 1);
4067 for (i
= 0; i
< bmap
->n_div
; ++i
) {
4072 for (j
= 0; j
< bmap
->n_div
; ++j
) {
4073 if (isl_int_is_zero(bmap
->div
[j
][0]))
4075 if (!isl_int_is_zero(bmap
->div
[j
][1 + 1 + dim
+ i
]))
4078 if (j
< bmap
->n_div
)
4080 for (j
= 0; j
< bmap
->n_ineq
; ++j
) {
4082 if (j
== l
|| j
== u
)
4084 if (isl_int_is_zero(bmap
->ineq
[j
][1 + dim
+ div
])) {
4085 if (is_zero_or_one(bmap
->ineq
[j
][1 + dim
+ i
]))
4089 if (isl_int_is_zero(bmap
->ineq
[j
][1 + dim
+ i
]))
4091 isl_int_mul(bmap
->ineq
[j
][1 + dim
+ div
],
4092 bmap
->ineq
[j
][1 + dim
+ div
],
4094 valid
= isl_int_eq(bmap
->ineq
[j
][1 + dim
+ div
],
4095 bmap
->ineq
[j
][1 + dim
+ i
]);
4096 isl_int_divexact(bmap
->ineq
[j
][1 + dim
+ div
],
4097 bmap
->ineq
[j
][1 + dim
+ div
],
4102 if (j
< bmap
->n_ineq
)
4107 isl_int_sub_ui(bmap
->ineq
[l
][0], bmap
->ineq
[l
][0], 1);
4108 isl_int_sub(bmap
->ineq
[l
][0], bmap
->ineq
[l
][0], bmap
->ineq
[u
][0]);
4112 /* Given a lower and an upper bound on div i, construct an inequality
4113 * that when nonnegative ensures that this pair of bounds always allows
4114 * for an integer value of the given div.
4115 * The lower bound is inequality l, while the upper bound is inequality u.
4116 * The constructed inequality is stored in ineq.
4117 * g, fl, fu are temporary scalars.
4119 * Let the upper bound be
4123 * and the lower bound
4127 * Let n_u = f_u g and n_l = f_l g, with g = gcd(n_u, n_l).
4130 * - f_u e_l <= f_u f_l g a <= f_l e_u
4132 * Since all variables are integer valued, this is equivalent to
4134 * - f_u e_l - (f_u - 1) <= f_u f_l g a <= f_l e_u + (f_l - 1)
4136 * If this interval is at least f_u f_l g, then it contains at least
4137 * one integer value for a.
4138 * That is, the test constraint is
4140 * f_l e_u + f_u e_l + f_l - 1 + f_u - 1 + 1 >= f_u f_l g
4142 static void construct_test_ineq(struct isl_basic_map
*bmap
, int i
,
4143 int l
, int u
, isl_int
*ineq
, isl_int
*g
, isl_int
*fl
, isl_int
*fu
)
4146 dim
= isl_space_dim(bmap
->dim
, isl_dim_all
);
4148 isl_int_gcd(*g
, bmap
->ineq
[l
][1 + dim
+ i
], bmap
->ineq
[u
][1 + dim
+ i
]);
4149 isl_int_divexact(*fl
, bmap
->ineq
[l
][1 + dim
+ i
], *g
);
4150 isl_int_divexact(*fu
, bmap
->ineq
[u
][1 + dim
+ i
], *g
);
4151 isl_int_neg(*fu
, *fu
);
4152 isl_seq_combine(ineq
, *fl
, bmap
->ineq
[u
], *fu
, bmap
->ineq
[l
],
4153 1 + dim
+ bmap
->n_div
);
4154 isl_int_add(ineq
[0], ineq
[0], *fl
);
4155 isl_int_add(ineq
[0], ineq
[0], *fu
);
4156 isl_int_sub_ui(ineq
[0], ineq
[0], 1);
4157 isl_int_mul(*g
, *g
, *fl
);
4158 isl_int_mul(*g
, *g
, *fu
);
4159 isl_int_sub(ineq
[0], ineq
[0], *g
);
4162 /* Remove more kinds of divs that are not strictly needed.
4163 * In particular, if all pairs of lower and upper bounds on a div
4164 * are such that they allow at least one integer value of the div,
4165 * the we can eliminate the div using Fourier-Motzkin without
4166 * introducing any spurious solutions.
4168 static struct isl_basic_map
*drop_more_redundant_divs(
4169 struct isl_basic_map
*bmap
, int *pairs
, int n
)
4171 struct isl_tab
*tab
= NULL
;
4172 struct isl_vec
*vec
= NULL
;
4184 dim
= isl_space_dim(bmap
->dim
, isl_dim_all
);
4185 vec
= isl_vec_alloc(bmap
->ctx
, 1 + dim
+ bmap
->n_div
);
4189 tab
= isl_tab_from_basic_map(bmap
, 0);
4194 enum isl_lp_result res
;
4196 for (i
= 0; i
< bmap
->n_div
; ++i
) {
4199 if (best
>= 0 && pairs
[best
] <= pairs
[i
])
4205 for (l
= 0; l
< bmap
->n_ineq
; ++l
) {
4206 if (!isl_int_is_pos(bmap
->ineq
[l
][1 + dim
+ i
]))
4208 for (u
= 0; u
< bmap
->n_ineq
; ++u
) {
4209 if (!isl_int_is_neg(bmap
->ineq
[u
][1 + dim
+ i
]))
4211 construct_test_ineq(bmap
, i
, l
, u
,
4212 vec
->el
, &g
, &fl
, &fu
);
4213 res
= isl_tab_min(tab
, vec
->el
,
4214 bmap
->ctx
->one
, &g
, NULL
, 0);
4215 if (res
== isl_lp_error
)
4217 if (res
== isl_lp_empty
) {
4218 bmap
= isl_basic_map_set_to_empty(bmap
);
4221 if (res
!= isl_lp_ok
|| isl_int_is_neg(g
))
4224 if (u
< bmap
->n_ineq
)
4227 if (l
== bmap
->n_ineq
) {
4247 bmap
= isl_basic_map_remove_dims(bmap
, isl_dim_div
, remove
, 1);
4248 return isl_basic_map_drop_redundant_divs(bmap
);
4251 isl_basic_map_free(bmap
);
4260 /* Given a pair of divs div1 and div2 such that, except for the lower bound l
4261 * and the upper bound u, div1 always occurs together with div2 in the form
4262 * (div1 + m div2), where m is the constant range on the variable div1
4263 * allowed by l and u, replace the pair div1 and div2 by a single
4264 * div that is equal to div1 + m div2.
4266 * The new div will appear in the location that contains div2.
4267 * We need to modify all constraints that contain
4268 * div2 = (div - div1) / m
4269 * The coefficient of div2 is known to be equal to 1 or -1.
4270 * (If a constraint does not contain div2, it will also not contain div1.)
4271 * If the constraint also contains div1, then we know they appear
4272 * as f (div1 + m div2) and we can simply replace (div1 + m div2) by div,
4273 * i.e., the coefficient of div is f.
4275 * Otherwise, we first need to introduce div1 into the constraint.
4284 * A lower bound on div2
4288 * can be replaced by
4290 * m div2 + div1 + m t + f >= 0
4296 * can be replaced by
4298 * -(m div2 + div1) + m t + f' >= 0
4300 * These constraint are those that we would obtain from eliminating
4301 * div1 using Fourier-Motzkin.
4303 * After all constraints have been modified, we drop the lower and upper
4304 * bound and then drop div1.
4306 static struct isl_basic_map
*coalesce_divs(struct isl_basic_map
*bmap
,
4307 unsigned div1
, unsigned div2
, unsigned l
, unsigned u
)
4311 unsigned dim
, total
;
4314 ctx
= isl_basic_map_get_ctx(bmap
);
4316 dim
= isl_space_dim(bmap
->dim
, isl_dim_all
);
4317 total
= 1 + dim
+ bmap
->n_div
;
4320 isl_int_add(m
, bmap
->ineq
[l
][0], bmap
->ineq
[u
][0]);
4321 isl_int_add_ui(m
, m
, 1);
4323 for (i
= 0; i
< bmap
->n_ineq
; ++i
) {
4324 if (i
== l
|| i
== u
)
4326 if (isl_int_is_zero(bmap
->ineq
[i
][1 + dim
+ div2
]))
4328 if (isl_int_is_zero(bmap
->ineq
[i
][1 + dim
+ div1
])) {
4329 if (isl_int_is_pos(bmap
->ineq
[i
][1 + dim
+ div2
]))
4330 isl_seq_combine(bmap
->ineq
[i
], m
, bmap
->ineq
[i
],
4331 ctx
->one
, bmap
->ineq
[l
], total
);
4333 isl_seq_combine(bmap
->ineq
[i
], m
, bmap
->ineq
[i
],
4334 ctx
->one
, bmap
->ineq
[u
], total
);
4336 isl_int_set(bmap
->ineq
[i
][1 + dim
+ div2
],
4337 bmap
->ineq
[i
][1 + dim
+ div1
]);
4338 isl_int_set_si(bmap
->ineq
[i
][1 + dim
+ div1
], 0);
4343 isl_basic_map_drop_inequality(bmap
, l
);
4344 isl_basic_map_drop_inequality(bmap
, u
);
4346 isl_basic_map_drop_inequality(bmap
, u
);
4347 isl_basic_map_drop_inequality(bmap
, l
);
4349 bmap
= isl_basic_map_drop_div(bmap
, div1
);
4353 /* First check if we can coalesce any pair of divs and
4354 * then continue with dropping more redundant divs.
4356 * We loop over all pairs of lower and upper bounds on a div
4357 * with coefficient 1 and -1, respectively, check if there
4358 * is any other div "c" with which we can coalesce the div
4359 * and if so, perform the coalescing.
4361 static struct isl_basic_map
*coalesce_or_drop_more_redundant_divs(
4362 struct isl_basic_map
*bmap
, int *pairs
, int n
)
4367 dim
= isl_space_dim(bmap
->dim
, isl_dim_all
);
4369 for (i
= 0; i
< bmap
->n_div
; ++i
) {
4372 for (l
= 0; l
< bmap
->n_ineq
; ++l
) {
4373 if (!isl_int_is_one(bmap
->ineq
[l
][1 + dim
+ i
]))
4375 for (u
= 0; u
< bmap
->n_ineq
; ++u
) {
4378 if (!isl_int_is_negone(bmap
->ineq
[u
][1+dim
+i
]))
4380 c
= div_find_coalesce(bmap
, pairs
, i
, l
, u
);
4384 bmap
= coalesce_divs(bmap
, i
, c
, l
, u
);
4385 return isl_basic_map_drop_redundant_divs(bmap
);
4390 if (ISL_F_ISSET(bmap
, ISL_BASIC_MAP_EMPTY
))
4393 return drop_more_redundant_divs(bmap
, pairs
, n
);
4396 /* Are the "n" coefficients starting at "first" of inequality constraints
4397 * "i" and "j" of "bmap" equal to each other?
4399 static int is_parallel_part(__isl_keep isl_basic_map
*bmap
, int i
, int j
,
4402 return isl_seq_eq(bmap
->ineq
[i
] + first
, bmap
->ineq
[j
] + first
, n
);
4405 /* Are the "n" coefficients starting at "first" of inequality constraints
4406 * "i" and "j" of "bmap" opposite to each other?
4408 static int is_opposite_part(__isl_keep isl_basic_map
*bmap
, int i
, int j
,
4411 return isl_seq_is_neg(bmap
->ineq
[i
] + first
, bmap
->ineq
[j
] + first
, n
);
4414 /* Are inequality constraints "i" and "j" of "bmap" opposite to each other,
4415 * apart from the constant term?
4417 static int is_opposite(__isl_keep isl_basic_map
*bmap
, int i
, int j
)
4421 total
= isl_basic_map_dim(bmap
, isl_dim_all
);
4422 return is_opposite_part(bmap
, i
, j
, 1, total
);
4425 /* Are inequality constraints "i" and "j" of "bmap" equal to each other,
4426 * apart from the constant term and the coefficient at position "pos"?
4428 static int is_parallel_except(__isl_keep isl_basic_map
*bmap
, int i
, int j
,
4433 total
= isl_basic_map_dim(bmap
, isl_dim_all
);
4434 return is_parallel_part(bmap
, i
, j
, 1, pos
- 1) &&
4435 is_parallel_part(bmap
, i
, j
, pos
+ 1, total
- pos
);
4438 /* Are inequality constraints "i" and "j" of "bmap" opposite to each other,
4439 * apart from the constant term and the coefficient at position "pos"?
4441 static int is_opposite_except(__isl_keep isl_basic_map
*bmap
, int i
, int j
,
4446 total
= isl_basic_map_dim(bmap
, isl_dim_all
);
4447 return is_opposite_part(bmap
, i
, j
, 1, pos
- 1) &&
4448 is_opposite_part(bmap
, i
, j
, pos
+ 1, total
- pos
);
4451 /* Restart isl_basic_map_drop_redundant_divs after "bmap" has
4452 * been modified, simplying it if "simplify" is set.
4453 * Free the temporary data structure "pairs" that was associated
4454 * to the old version of "bmap".
4456 static __isl_give isl_basic_map
*drop_redundant_divs_again(
4457 __isl_take isl_basic_map
*bmap
, __isl_take
int *pairs
, int simplify
)
4460 bmap
= isl_basic_map_simplify(bmap
);
4462 return isl_basic_map_drop_redundant_divs(bmap
);
4465 /* Is "div" the single unknown existentially quantified variable
4466 * in inequality constraint "ineq" of "bmap"?
4467 * "div" is known to have a non-zero coefficient in "ineq".
4469 static int single_unknown(__isl_keep isl_basic_map
*bmap
, int ineq
, int div
)
4472 unsigned n_div
, o_div
;
4474 if (isl_basic_map_div_is_known(bmap
, div
))
4476 n_div
= isl_basic_map_dim(bmap
, isl_dim_div
);
4479 o_div
= isl_basic_map_offset(bmap
, isl_dim_div
);
4480 for (i
= 0; i
< n_div
; ++i
) {
4483 if (isl_int_is_zero(bmap
->ineq
[ineq
][o_div
+ i
]))
4485 if (!isl_basic_map_div_is_known(bmap
, i
))
4492 /* Does integer division "div" have coefficient 1 in inequality constraint
4495 static int has_coef_one(__isl_keep isl_basic_map
*bmap
, int div
, int ineq
)
4499 o_div
= isl_basic_map_offset(bmap
, isl_dim_div
);
4500 if (isl_int_is_one(bmap
->ineq
[ineq
][o_div
+ div
]))
4506 /* Turn inequality constraint "ineq" of "bmap" into an equality and
4507 * then try and drop redundant divs again,
4508 * freeing the temporary data structure "pairs" that was associated
4509 * to the old version of "bmap".
4511 static __isl_give isl_basic_map
*set_eq_and_try_again(
4512 __isl_take isl_basic_map
*bmap
, int ineq
, __isl_take
int *pairs
)
4514 bmap
= isl_basic_map_cow(bmap
);
4515 isl_basic_map_inequality_to_equality(bmap
, ineq
);
4516 return drop_redundant_divs_again(bmap
, pairs
, 1);
4519 /* Given two inequality constraints
4521 * f(x) + n d + c >= 0, (ineq)
4523 * with d the variable at position "pos", and
4525 * f(x) + c0 >= 0, (lower)
4527 * compute the maximal value of the lower bound ceil((-f(x) - c)/n)
4528 * determined by the first constraint.
4535 static void lower_bound_from_parallel(__isl_keep isl_basic_map
*bmap
,
4536 int ineq
, int lower
, int pos
, isl_int
*l
)
4538 isl_int_neg(*l
, bmap
->ineq
[ineq
][0]);
4539 isl_int_add(*l
, *l
, bmap
->ineq
[lower
][0]);
4540 isl_int_cdiv_q(*l
, *l
, bmap
->ineq
[ineq
][pos
]);
4543 /* Given two inequality constraints
4545 * f(x) + n d + c >= 0, (ineq)
4547 * with d the variable at position "pos", and
4549 * -f(x) - c0 >= 0, (upper)
4551 * compute the minimal value of the lower bound ceil((-f(x) - c)/n)
4552 * determined by the first constraint.
4559 static void lower_bound_from_opposite(__isl_keep isl_basic_map
*bmap
,
4560 int ineq
, int upper
, int pos
, isl_int
*u
)
4562 isl_int_neg(*u
, bmap
->ineq
[ineq
][0]);
4563 isl_int_sub(*u
, *u
, bmap
->ineq
[upper
][0]);
4564 isl_int_cdiv_q(*u
, *u
, bmap
->ineq
[ineq
][pos
]);
4567 /* Given a lower bound constraint "ineq" on "div" in "bmap",
4568 * does the corresponding lower bound have a fixed value in "bmap"?
4570 * In particular, "ineq" is of the form
4572 * f(x) + n d + c >= 0
4574 * with n > 0, c the constant term and
4575 * d the existentially quantified variable "div".
4576 * That is, the lower bound is
4578 * ceil((-f(x) - c)/n)
4580 * Look for a pair of constraints
4585 * i.e., -c1 <= -f(x) <= c0, that fix ceil((-f(x) - c)/n) to a constant value.
4586 * That is, check that
4588 * ceil((-c1 - c)/n) = ceil((c0 - c)/n)
4590 * If so, return the index of inequality f(x) + c0 >= 0.
4591 * Otherwise, return -1.
4593 static int lower_bound_is_cst(__isl_keep isl_basic_map
*bmap
, int div
, int ineq
)
4596 int lower
= -1, upper
= -1;
4597 unsigned o_div
, n_div
;
4601 n_div
= isl_basic_map_dim(bmap
, isl_dim_div
);
4602 o_div
= isl_basic_map_offset(bmap
, isl_dim_div
);
4603 for (i
= 0; i
< bmap
->n_ineq
&& (lower
< 0 || upper
< 0); ++i
) {
4606 if (!isl_int_is_zero(bmap
->ineq
[i
][o_div
+ div
]))
4609 is_parallel_except(bmap
, ineq
, i
, o_div
+ div
)) {
4614 is_opposite_except(bmap
, ineq
, i
, o_div
+ div
)) {
4619 if (lower
< 0 || upper
< 0)
4625 lower_bound_from_parallel(bmap
, ineq
, lower
, o_div
+ div
, &l
);
4626 lower_bound_from_opposite(bmap
, ineq
, upper
, o_div
+ div
, &u
);
4628 equal
= isl_int_eq(l
, u
);
4633 return equal
? lower
: -1;
4636 /* Given a lower bound constraint "ineq" on the existentially quantified
4637 * variable "div", such that the corresponding lower bound has
4638 * a fixed value in "bmap", assign this fixed value to the variable and
4639 * then try and drop redundant divs again,
4640 * freeing the temporary data structure "pairs" that was associated
4641 * to the old version of "bmap".
4642 * "lower" determines the constant value for the lower bound.
4644 * In particular, "ineq" is of the form
4646 * f(x) + n d + c >= 0,
4648 * while "lower" is of the form
4652 * The lower bound is ceil((-f(x) - c)/n) and its constant value
4653 * is ceil((c0 - c)/n).
4655 static __isl_give isl_basic_map
*fix_cst_lower(__isl_take isl_basic_map
*bmap
,
4656 int div
, int ineq
, int lower
, int *pairs
)
4663 o_div
= isl_basic_map_offset(bmap
, isl_dim_div
);
4664 lower_bound_from_parallel(bmap
, ineq
, lower
, o_div
+ div
, &c
);
4665 bmap
= isl_basic_map_fix(bmap
, isl_dim_div
, div
, c
);
4670 return isl_basic_map_drop_redundant_divs(bmap
);
4673 /* Remove divs that are not strictly needed based on the inequality
4675 * In particular, if a div only occurs positively (or negatively)
4676 * in constraints, then it can simply be dropped.
4677 * Also, if a div occurs in only two constraints and if moreover
4678 * those two constraints are opposite to each other, except for the constant
4679 * term and if the sum of the constant terms is such that for any value
4680 * of the other values, there is always at least one integer value of the
4681 * div, i.e., if one plus this sum is greater than or equal to
4682 * the (absolute value) of the coefficient of the div in the constraints,
4683 * then we can also simply drop the div.
4685 * If an existentially quantified variable does not have an explicit
4686 * representation, appears in only a single lower bound that does not
4687 * involve any other such existentially quantified variables and appears
4688 * in this lower bound with coefficient 1,
4689 * then fix the variable to the value of the lower bound. That is,
4690 * turn the inequality into an equality.
4691 * If for any value of the other variables, there is any value
4692 * for the existentially quantified variable satisfying the constraints,
4693 * then this lower bound also satisfies the constraints.
4694 * It is therefore safe to pick this lower bound.
4696 * The same reasoning holds even if the coefficient is not one.
4697 * However, fixing the variable to the value of the lower bound may
4698 * in general introduce an extra integer division, in which case
4699 * it may be better to pick another value.
4700 * If this integer division has a known constant value, then plugging
4701 * in this constant value removes the existentially quantified variable
4702 * completely. In particular, if the lower bound is of the form
4703 * ceil((-f(x) - c)/n) and there are two constraints, f(x) + c0 >= 0 and
4704 * -f(x) + c1 >= 0 such that ceil((-c1 - c)/n) = ceil((c0 - c)/n),
4705 * then the existentially quantified variable can be assigned this
4708 * We skip divs that appear in equalities or in the definition of other divs.
4709 * Divs that appear in the definition of other divs usually occur in at least
4710 * 4 constraints, but the constraints may have been simplified.
4712 * If any divs are left after these simple checks then we move on
4713 * to more complicated cases in drop_more_redundant_divs.
4715 static __isl_give isl_basic_map
*isl_basic_map_drop_redundant_divs_ineq(
4716 __isl_take isl_basic_map
*bmap
)
4725 if (bmap
->n_div
== 0)
4728 off
= isl_space_dim(bmap
->dim
, isl_dim_all
);
4729 pairs
= isl_calloc_array(bmap
->ctx
, int, bmap
->n_div
);
4733 for (i
= 0; i
< bmap
->n_div
; ++i
) {
4735 int last_pos
, last_neg
;
4739 defined
= !isl_int_is_zero(bmap
->div
[i
][0]);
4740 for (j
= i
; j
< bmap
->n_div
; ++j
)
4741 if (!isl_int_is_zero(bmap
->div
[j
][1 + 1 + off
+ i
]))
4743 if (j
< bmap
->n_div
)
4745 for (j
= 0; j
< bmap
->n_eq
; ++j
)
4746 if (!isl_int_is_zero(bmap
->eq
[j
][1 + off
+ i
]))
4752 for (j
= 0; j
< bmap
->n_ineq
; ++j
) {
4753 if (isl_int_is_pos(bmap
->ineq
[j
][1 + off
+ i
])) {
4757 if (isl_int_is_neg(bmap
->ineq
[j
][1 + off
+ i
])) {
4762 pairs
[i
] = pos
* neg
;
4763 if (pairs
[i
] == 0) {
4764 for (j
= bmap
->n_ineq
- 1; j
>= 0; --j
)
4765 if (!isl_int_is_zero(bmap
->ineq
[j
][1+off
+i
]))
4766 isl_basic_map_drop_inequality(bmap
, j
);
4767 bmap
= isl_basic_map_drop_div(bmap
, i
);
4768 return drop_redundant_divs_again(bmap
, pairs
, 0);
4770 if (pairs
[i
] != 1 || !is_opposite(bmap
, last_pos
, last_neg
)) {
4774 single
= single_unknown(bmap
, last_pos
, i
);
4777 if (has_coef_one(bmap
, i
, last_pos
))
4778 return set_eq_and_try_again(bmap
, last_pos
,
4780 lower
= lower_bound_is_cst(bmap
, i
, last_pos
);
4782 return fix_cst_lower(bmap
, i
, last_pos
, lower
,
4787 isl_int_add(bmap
->ineq
[last_pos
][0],
4788 bmap
->ineq
[last_pos
][0], bmap
->ineq
[last_neg
][0]);
4789 isl_int_add_ui(bmap
->ineq
[last_pos
][0],
4790 bmap
->ineq
[last_pos
][0], 1);
4791 redundant
= isl_int_ge(bmap
->ineq
[last_pos
][0],
4792 bmap
->ineq
[last_pos
][1+off
+i
]);
4793 isl_int_sub_ui(bmap
->ineq
[last_pos
][0],
4794 bmap
->ineq
[last_pos
][0], 1);
4795 isl_int_sub(bmap
->ineq
[last_pos
][0],
4796 bmap
->ineq
[last_pos
][0], bmap
->ineq
[last_neg
][0]);
4799 !ok_to_set_div_from_bound(bmap
, i
, last_pos
)) {
4804 bmap
= set_div_from_lower_bound(bmap
, i
, last_pos
);
4805 return drop_redundant_divs_again(bmap
, pairs
, 1);
4807 if (last_pos
> last_neg
) {
4808 isl_basic_map_drop_inequality(bmap
, last_pos
);
4809 isl_basic_map_drop_inequality(bmap
, last_neg
);
4811 isl_basic_map_drop_inequality(bmap
, last_neg
);
4812 isl_basic_map_drop_inequality(bmap
, last_pos
);
4814 bmap
= isl_basic_map_drop_div(bmap
, i
);
4815 return drop_redundant_divs_again(bmap
, pairs
, 0);
4819 return coalesce_or_drop_more_redundant_divs(bmap
, pairs
, n
);
4825 isl_basic_map_free(bmap
);
4829 /* Consider the coefficients at "c" as a row vector and replace
4830 * them with their product with "T". "T" is assumed to be a square matrix.
4832 static isl_stat
preimage(isl_int
*c
, __isl_keep isl_mat
*T
)
4839 return isl_stat_error
;
4840 n
= isl_mat_rows(T
);
4841 if (isl_seq_first_non_zero(c
, n
) == -1)
4843 ctx
= isl_mat_get_ctx(T
);
4844 v
= isl_vec_alloc(ctx
, n
);
4846 return isl_stat_error
;
4847 isl_seq_swp_or_cpy(v
->el
, c
, n
);
4848 v
= isl_vec_mat_product(v
, isl_mat_copy(T
));
4850 return isl_stat_error
;
4851 isl_seq_swp_or_cpy(c
, v
->el
, n
);
4857 /* Plug in T for the variables in "bmap" starting at "pos".
4858 * T is a linear unimodular matrix, i.e., without constant term.
4860 static __isl_give isl_basic_map
*isl_basic_map_preimage_vars(
4861 __isl_take isl_basic_map
*bmap
, unsigned pos
, __isl_take isl_mat
*T
)
4866 bmap
= isl_basic_map_cow(bmap
);
4870 n
= isl_mat_cols(T
);
4871 if (n
!= isl_mat_rows(T
))
4872 isl_die(isl_mat_get_ctx(T
), isl_error_invalid
,
4873 "expecting square matrix", goto error
);
4875 total
= isl_basic_map_dim(bmap
, isl_dim_all
);
4876 if (pos
+ n
> total
|| pos
+ n
< pos
)
4877 isl_die(isl_mat_get_ctx(T
), isl_error_invalid
,
4878 "invalid range", goto error
);
4880 for (i
= 0; i
< bmap
->n_eq
; ++i
)
4881 if (preimage(bmap
->eq
[i
] + 1 + pos
, T
) < 0)
4883 for (i
= 0; i
< bmap
->n_ineq
; ++i
)
4884 if (preimage(bmap
->ineq
[i
] + 1 + pos
, T
) < 0)
4886 for (i
= 0; i
< bmap
->n_div
; ++i
) {
4887 if (!isl_basic_map_div_is_known(bmap
, i
))
4889 if (preimage(bmap
->div
[i
] + 1 + 1 + pos
, T
) < 0)
4896 isl_basic_map_free(bmap
);
4901 /* Remove divs that are not strictly needed.
4903 * First look for an equality constraint involving two or more
4904 * existentially quantified variables without an explicit
4905 * representation. Replace the combination that appears
4906 * in the equality constraint by a single existentially quantified
4907 * variable such that the equality can be used to derive
4908 * an explicit representation for the variable.
4909 * If there are no more such equality constraints, then continue
4910 * with isl_basic_map_drop_redundant_divs_ineq.
4912 * In particular, if the equality constraint is of the form
4914 * f(x) + \sum_i c_i a_i = 0
4916 * with a_i existentially quantified variable without explicit
4917 * representation, then apply a transformation on the existentially
4918 * quantified variables to turn the constraint into
4922 * with g the gcd of the c_i.
4923 * In order to easily identify which existentially quantified variables
4924 * have a complete explicit representation, i.e., without being defined
4925 * in terms of other existentially quantified variables without
4926 * an explicit representation, the existentially quantified variables
4929 * The variable transformation is computed by extending the row
4930 * [c_1/g ... c_n/g] to a unimodular matrix, obtaining the transformation
4932 * [a_1'] [c_1/g ... c_n/g] [ a_1 ]
4937 * with [c_1/g ... c_n/g] representing the first row of U.
4938 * The inverse of U is then plugged into the original constraints.
4939 * The call to isl_basic_map_simplify makes sure the explicit
4940 * representation for a_1' is extracted from the equality constraint.
4942 __isl_give isl_basic_map
*isl_basic_map_drop_redundant_divs(
4943 __isl_take isl_basic_map
*bmap
)
4947 unsigned o_div
, n_div
;
4954 if (isl_basic_map_divs_known(bmap
))
4955 return isl_basic_map_drop_redundant_divs_ineq(bmap
);
4956 if (bmap
->n_eq
== 0)
4957 return isl_basic_map_drop_redundant_divs_ineq(bmap
);
4958 bmap
= isl_basic_map_sort_divs(bmap
);
4962 first
= isl_basic_map_first_unknown_div(bmap
);
4964 return isl_basic_map_free(bmap
);
4966 o_div
= isl_basic_map_offset(bmap
, isl_dim_div
);
4967 n_div
= isl_basic_map_dim(bmap
, isl_dim_div
);
4969 for (i
= 0; i
< bmap
->n_eq
; ++i
) {
4970 l
= isl_seq_first_non_zero(bmap
->eq
[i
] + o_div
+ first
,
4975 if (isl_seq_first_non_zero(bmap
->eq
[i
] + o_div
+ l
+ 1,
4976 n_div
- (l
+ 1)) == -1)
4980 if (i
>= bmap
->n_eq
)
4981 return isl_basic_map_drop_redundant_divs_ineq(bmap
);
4983 ctx
= isl_basic_map_get_ctx(bmap
);
4984 T
= isl_mat_alloc(ctx
, n_div
- l
, n_div
- l
);
4986 return isl_basic_map_free(bmap
);
4987 isl_seq_cpy(T
->row
[0], bmap
->eq
[i
] + o_div
+ l
, n_div
- l
);
4988 T
= isl_mat_normalize_row(T
, 0);
4989 T
= isl_mat_unimodular_complete(T
, 1);
4990 T
= isl_mat_right_inverse(T
);
4992 for (i
= l
; i
< n_div
; ++i
)
4993 bmap
= isl_basic_map_mark_div_unknown(bmap
, i
);
4994 bmap
= isl_basic_map_preimage_vars(bmap
, o_div
- 1 + l
, T
);
4995 bmap
= isl_basic_map_simplify(bmap
);
4997 return isl_basic_map_drop_redundant_divs(bmap
);
5000 struct isl_basic_set
*isl_basic_set_drop_redundant_divs(
5001 struct isl_basic_set
*bset
)
5003 return (struct isl_basic_set
*)
5004 isl_basic_map_drop_redundant_divs((struct isl_basic_map
*)bset
);
5007 struct isl_map
*isl_map_drop_redundant_divs(struct isl_map
*map
)
5013 for (i
= 0; i
< map
->n
; ++i
) {
5014 map
->p
[i
] = isl_basic_map_drop_redundant_divs(map
->p
[i
]);
5018 ISL_F_CLR(map
, ISL_MAP_NORMALIZED
);
5025 struct isl_set
*isl_set_drop_redundant_divs(struct isl_set
*set
)
5027 return (struct isl_set
*)
5028 isl_map_drop_redundant_divs((struct isl_map
*)set
);
5031 /* Does "bmap" satisfy any equality that involves more than 2 variables
5032 * and/or has coefficients different from -1 and 1?
5034 static int has_multiple_var_equality(__isl_keep isl_basic_map
*bmap
)
5039 total
= isl_basic_map_dim(bmap
, isl_dim_all
);
5041 for (i
= 0; i
< bmap
->n_eq
; ++i
) {
5044 j
= isl_seq_first_non_zero(bmap
->eq
[i
] + 1, total
);
5047 if (!isl_int_is_one(bmap
->eq
[i
][1 + j
]) &&
5048 !isl_int_is_negone(bmap
->eq
[i
][1 + j
]))
5052 k
= isl_seq_first_non_zero(bmap
->eq
[i
] + 1 + j
, total
- j
);
5056 if (!isl_int_is_one(bmap
->eq
[i
][1 + j
]) &&
5057 !isl_int_is_negone(bmap
->eq
[i
][1 + j
]))
5061 k
= isl_seq_first_non_zero(bmap
->eq
[i
] + 1 + j
, total
- j
);
5069 /* Remove any common factor g from the constraint coefficients in "v".
5070 * The constant term is stored in the first position and is replaced
5071 * by floor(c/g). If any common factor is removed and if this results
5072 * in a tightening of the constraint, then set *tightened.
5074 static __isl_give isl_vec
*normalize_constraint(__isl_take isl_vec
*v
,
5081 ctx
= isl_vec_get_ctx(v
);
5082 isl_seq_gcd(v
->el
+ 1, v
->size
- 1, &ctx
->normalize_gcd
);
5083 if (isl_int_is_zero(ctx
->normalize_gcd
))
5085 if (isl_int_is_one(ctx
->normalize_gcd
))
5090 if (tightened
&& !isl_int_is_divisible_by(v
->el
[0], ctx
->normalize_gcd
))
5092 isl_int_fdiv_q(v
->el
[0], v
->el
[0], ctx
->normalize_gcd
);
5093 isl_seq_scale_down(v
->el
+ 1, v
->el
+ 1, ctx
->normalize_gcd
,
5098 /* If "bmap" is an integer set that satisfies any equality involving
5099 * more than 2 variables and/or has coefficients different from -1 and 1,
5100 * then use variable compression to reduce the coefficients by removing
5101 * any (hidden) common factor.
5102 * In particular, apply the variable compression to each constraint,
5103 * factor out any common factor in the non-constant coefficients and
5104 * then apply the inverse of the compression.
5105 * At the end, we mark the basic map as having reduced constants.
5106 * If this flag is still set on the next invocation of this function,
5107 * then we skip the computation.
5109 * Removing a common factor may result in a tightening of some of
5110 * the constraints. If this happens, then we may end up with two
5111 * opposite inequalities that can be replaced by an equality.
5112 * We therefore call isl_basic_map_detect_inequality_pairs,
5113 * which checks for such pairs of inequalities as well as eliminate_divs_eq
5114 * and isl_basic_map_gauss if such a pair was found.
5116 __isl_give isl_basic_map
*isl_basic_map_reduce_coefficients(
5117 __isl_take isl_basic_map
*bmap
)
5122 isl_mat
*eq
, *T
, *T2
;
5128 if (ISL_F_ISSET(bmap
, ISL_BASIC_MAP_REDUCED_COEFFICIENTS
))
5130 if (isl_basic_map_is_rational(bmap
))
5132 if (bmap
->n_eq
== 0)
5134 if (!has_multiple_var_equality(bmap
))
5137 total
= isl_basic_map_dim(bmap
, isl_dim_all
);
5138 ctx
= isl_basic_map_get_ctx(bmap
);
5139 v
= isl_vec_alloc(ctx
, 1 + total
);
5141 return isl_basic_map_free(bmap
);
5143 eq
= isl_mat_sub_alloc6(ctx
, bmap
->eq
, 0, bmap
->n_eq
, 0, 1 + total
);
5144 T
= isl_mat_variable_compression(eq
, &T2
);
5147 if (T
->n_col
== 0) {
5151 return isl_basic_map_set_to_empty(bmap
);
5155 for (i
= 0; i
< bmap
->n_ineq
; ++i
) {
5156 isl_seq_cpy(v
->el
, bmap
->ineq
[i
], 1 + total
);
5157 v
= isl_vec_mat_product(v
, isl_mat_copy(T
));
5158 v
= normalize_constraint(v
, &tightened
);
5159 v
= isl_vec_mat_product(v
, isl_mat_copy(T2
));
5162 isl_seq_cpy(bmap
->ineq
[i
], v
->el
, 1 + total
);
5169 ISL_F_SET(bmap
, ISL_BASIC_MAP_REDUCED_COEFFICIENTS
);
5174 bmap
= isl_basic_map_detect_inequality_pairs(bmap
, &progress
);
5176 bmap
= eliminate_divs_eq(bmap
, &progress
);
5177 bmap
= isl_basic_map_gauss(bmap
, NULL
);
5186 return isl_basic_map_free(bmap
);
5189 /* Shift the integer division at position "div" of "bmap"
5190 * by "shift" times the variable at position "pos".
5191 * "pos" is as determined by isl_basic_map_offset, i.e., pos == 0
5192 * corresponds to the constant term.
5194 * That is, if the integer division has the form
5198 * then replace it by
5200 * floor((f(x) + shift * d * x_pos)/d) - shift * x_pos
5202 __isl_give isl_basic_map
*isl_basic_map_shift_div(
5203 __isl_take isl_basic_map
*bmap
, int div
, int pos
, isl_int shift
)
5211 total
= isl_basic_map_dim(bmap
, isl_dim_all
);
5212 total
-= isl_basic_map_dim(bmap
, isl_dim_div
);
5214 isl_int_addmul(bmap
->div
[div
][1 + pos
], shift
, bmap
->div
[div
][0]);
5216 for (i
= 0; i
< bmap
->n_eq
; ++i
) {
5217 if (isl_int_is_zero(bmap
->eq
[i
][1 + total
+ div
]))
5219 isl_int_submul(bmap
->eq
[i
][pos
],
5220 shift
, bmap
->eq
[i
][1 + total
+ div
]);
5222 for (i
= 0; i
< bmap
->n_ineq
; ++i
) {
5223 if (isl_int_is_zero(bmap
->ineq
[i
][1 + total
+ div
]))
5225 isl_int_submul(bmap
->ineq
[i
][pos
],
5226 shift
, bmap
->ineq
[i
][1 + total
+ div
]);
5228 for (i
= 0; i
< bmap
->n_div
; ++i
) {
5229 if (isl_int_is_zero(bmap
->div
[i
][0]))
5231 if (isl_int_is_zero(bmap
->div
[i
][1 + 1 + total
+ div
]))
5233 isl_int_submul(bmap
->div
[i
][1 + pos
],
5234 shift
, bmap
->div
[i
][1 + 1 + total
+ div
]);