2 * Copyright 2008-2009 Katholieke Universiteit Leuven
3 * Copyright 2012-2013 Ecole Normale Superieure
4 * Copyright 2014-2015 INRIA Rocquencourt
6 * Use of this software is governed by the MIT license
8 * Written by Sven Verdoolaege, K.U.Leuven, Departement
9 * Computerwetenschappen, Celestijnenlaan 200A, B-3001 Leuven, Belgium
10 * and Ecole Normale Superieure, 45 rue d’Ulm, 75230 Paris, France
11 * and Inria Paris - Rocquencourt, Domaine de Voluceau - Rocquencourt,
12 * B.P. 105 - 78153 Le Chesnay, France
15 #include <isl_ctx_private.h>
16 #include <isl_map_private.h>
17 #include "isl_equalities.h"
21 #include <isl_space_private.h>
22 #include <isl_mat_private.h>
23 #include <isl_vec_private.h>
25 static void swap_equality(struct isl_basic_map
*bmap
, int a
, int b
)
27 isl_int
*t
= bmap
->eq
[a
];
28 bmap
->eq
[a
] = bmap
->eq
[b
];
32 static void swap_inequality(struct isl_basic_map
*bmap
, int a
, int b
)
35 isl_int
*t
= bmap
->ineq
[a
];
36 bmap
->ineq
[a
] = bmap
->ineq
[b
];
41 static void constraint_drop_vars(isl_int
*c
, unsigned n
, unsigned rem
)
43 isl_seq_cpy(c
, c
+ n
, rem
);
44 isl_seq_clr(c
+ rem
, n
);
47 /* Drop n dimensions starting at first.
49 * In principle, this frees up some extra variables as the number
50 * of columns remains constant, but we would have to extend
51 * the div array too as the number of rows in this array is assumed
52 * to be equal to extra.
54 struct isl_basic_set
*isl_basic_set_drop_dims(
55 struct isl_basic_set
*bset
, unsigned first
, unsigned n
)
62 isl_assert(bset
->ctx
, first
+ n
<= bset
->dim
->n_out
, goto error
);
64 if (n
== 0 && !isl_space_get_tuple_name(bset
->dim
, isl_dim_set
))
67 bset
= isl_basic_set_cow(bset
);
71 for (i
= 0; i
< bset
->n_eq
; ++i
)
72 constraint_drop_vars(bset
->eq
[i
]+1+bset
->dim
->nparam
+first
, n
,
73 (bset
->dim
->n_out
-first
-n
)+bset
->extra
);
75 for (i
= 0; i
< bset
->n_ineq
; ++i
)
76 constraint_drop_vars(bset
->ineq
[i
]+1+bset
->dim
->nparam
+first
, n
,
77 (bset
->dim
->n_out
-first
-n
)+bset
->extra
);
79 for (i
= 0; i
< bset
->n_div
; ++i
)
80 constraint_drop_vars(bset
->div
[i
]+1+1+bset
->dim
->nparam
+first
, n
,
81 (bset
->dim
->n_out
-first
-n
)+bset
->extra
);
83 bset
->dim
= isl_space_drop_outputs(bset
->dim
, first
, n
);
87 ISL_F_CLR(bset
, ISL_BASIC_SET_NORMALIZED
);
88 bset
= isl_basic_set_simplify(bset
);
89 return isl_basic_set_finalize(bset
);
91 isl_basic_set_free(bset
);
95 struct isl_set
*isl_set_drop_dims(
96 struct isl_set
*set
, unsigned first
, unsigned n
)
103 isl_assert(set
->ctx
, first
+ n
<= set
->dim
->n_out
, goto error
);
105 if (n
== 0 && !isl_space_get_tuple_name(set
->dim
, isl_dim_set
))
107 set
= isl_set_cow(set
);
110 set
->dim
= isl_space_drop_outputs(set
->dim
, first
, n
);
114 for (i
= 0; i
< set
->n
; ++i
) {
115 set
->p
[i
] = isl_basic_set_drop_dims(set
->p
[i
], first
, n
);
120 ISL_F_CLR(set
, ISL_SET_NORMALIZED
);
127 /* Move "n" divs starting at "first" to the end of the list of divs.
129 static struct isl_basic_map
*move_divs_last(struct isl_basic_map
*bmap
,
130 unsigned first
, unsigned n
)
135 if (first
+ n
== bmap
->n_div
)
138 div
= isl_alloc_array(bmap
->ctx
, isl_int
*, n
);
141 for (i
= 0; i
< n
; ++i
)
142 div
[i
] = bmap
->div
[first
+ i
];
143 for (i
= 0; i
< bmap
->n_div
- first
- n
; ++i
)
144 bmap
->div
[first
+ i
] = bmap
->div
[first
+ n
+ i
];
145 for (i
= 0; i
< n
; ++i
)
146 bmap
->div
[bmap
->n_div
- n
+ i
] = div
[i
];
150 isl_basic_map_free(bmap
);
154 /* Drop "n" dimensions of type "type" starting at "first".
156 * In principle, this frees up some extra variables as the number
157 * of columns remains constant, but we would have to extend
158 * the div array too as the number of rows in this array is assumed
159 * to be equal to extra.
161 struct isl_basic_map
*isl_basic_map_drop(struct isl_basic_map
*bmap
,
162 enum isl_dim_type type
, unsigned first
, unsigned n
)
172 dim
= isl_basic_map_dim(bmap
, type
);
173 isl_assert(bmap
->ctx
, first
+ n
<= dim
, goto error
);
175 if (n
== 0 && !isl_space_is_named_or_nested(bmap
->dim
, type
))
178 bmap
= isl_basic_map_cow(bmap
);
182 offset
= isl_basic_map_offset(bmap
, type
) + first
;
183 left
= isl_basic_map_total_dim(bmap
) - (offset
- 1) - n
;
184 for (i
= 0; i
< bmap
->n_eq
; ++i
)
185 constraint_drop_vars(bmap
->eq
[i
]+offset
, n
, left
);
187 for (i
= 0; i
< bmap
->n_ineq
; ++i
)
188 constraint_drop_vars(bmap
->ineq
[i
]+offset
, n
, left
);
190 for (i
= 0; i
< bmap
->n_div
; ++i
)
191 constraint_drop_vars(bmap
->div
[i
]+1+offset
, n
, left
);
193 if (type
== isl_dim_div
) {
194 bmap
= move_divs_last(bmap
, first
, n
);
197 isl_basic_map_free_div(bmap
, n
);
199 bmap
->dim
= isl_space_drop_dims(bmap
->dim
, type
, first
, n
);
203 ISL_F_CLR(bmap
, ISL_BASIC_MAP_NORMALIZED
);
204 bmap
= isl_basic_map_simplify(bmap
);
205 return isl_basic_map_finalize(bmap
);
207 isl_basic_map_free(bmap
);
211 __isl_give isl_basic_set
*isl_basic_set_drop(__isl_take isl_basic_set
*bset
,
212 enum isl_dim_type type
, unsigned first
, unsigned n
)
214 return (isl_basic_set
*)isl_basic_map_drop((isl_basic_map
*)bset
,
218 struct isl_basic_map
*isl_basic_map_drop_inputs(
219 struct isl_basic_map
*bmap
, unsigned first
, unsigned n
)
221 return isl_basic_map_drop(bmap
, isl_dim_in
, first
, n
);
224 struct isl_map
*isl_map_drop(struct isl_map
*map
,
225 enum isl_dim_type type
, unsigned first
, unsigned n
)
232 isl_assert(map
->ctx
, first
+ n
<= isl_map_dim(map
, type
), goto error
);
234 if (n
== 0 && !isl_space_get_tuple_name(map
->dim
, type
))
236 map
= isl_map_cow(map
);
239 map
->dim
= isl_space_drop_dims(map
->dim
, type
, first
, n
);
243 for (i
= 0; i
< map
->n
; ++i
) {
244 map
->p
[i
] = isl_basic_map_drop(map
->p
[i
], type
, first
, n
);
248 ISL_F_CLR(map
, ISL_MAP_NORMALIZED
);
256 struct isl_set
*isl_set_drop(struct isl_set
*set
,
257 enum isl_dim_type type
, unsigned first
, unsigned n
)
259 return (isl_set
*)isl_map_drop((isl_map
*)set
, type
, first
, n
);
262 struct isl_map
*isl_map_drop_inputs(
263 struct isl_map
*map
, unsigned first
, unsigned n
)
265 return isl_map_drop(map
, isl_dim_in
, first
, n
);
269 * We don't cow, as the div is assumed to be redundant.
271 static struct isl_basic_map
*isl_basic_map_drop_div(
272 struct isl_basic_map
*bmap
, unsigned div
)
280 pos
= 1 + isl_space_dim(bmap
->dim
, isl_dim_all
) + div
;
282 isl_assert(bmap
->ctx
, div
< bmap
->n_div
, goto error
);
284 for (i
= 0; i
< bmap
->n_eq
; ++i
)
285 constraint_drop_vars(bmap
->eq
[i
]+pos
, 1, bmap
->extra
-div
-1);
287 for (i
= 0; i
< bmap
->n_ineq
; ++i
) {
288 if (!isl_int_is_zero(bmap
->ineq
[i
][pos
])) {
289 isl_basic_map_drop_inequality(bmap
, i
);
293 constraint_drop_vars(bmap
->ineq
[i
]+pos
, 1, bmap
->extra
-div
-1);
296 for (i
= 0; i
< bmap
->n_div
; ++i
)
297 constraint_drop_vars(bmap
->div
[i
]+1+pos
, 1, bmap
->extra
-div
-1);
299 if (div
!= bmap
->n_div
- 1) {
301 isl_int
*t
= bmap
->div
[div
];
303 for (j
= div
; j
< bmap
->n_div
- 1; ++j
)
304 bmap
->div
[j
] = bmap
->div
[j
+1];
306 bmap
->div
[bmap
->n_div
- 1] = t
;
308 ISL_F_CLR(bmap
, ISL_BASIC_MAP_NORMALIZED
);
309 isl_basic_map_free_div(bmap
, 1);
313 isl_basic_map_free(bmap
);
317 struct isl_basic_map
*isl_basic_map_normalize_constraints(
318 struct isl_basic_map
*bmap
)
322 unsigned total
= isl_basic_map_total_dim(bmap
);
328 for (i
= bmap
->n_eq
- 1; i
>= 0; --i
) {
329 isl_seq_gcd(bmap
->eq
[i
]+1, total
, &gcd
);
330 if (isl_int_is_zero(gcd
)) {
331 if (!isl_int_is_zero(bmap
->eq
[i
][0])) {
332 bmap
= isl_basic_map_set_to_empty(bmap
);
335 isl_basic_map_drop_equality(bmap
, i
);
338 if (ISL_F_ISSET(bmap
, ISL_BASIC_MAP_RATIONAL
))
339 isl_int_gcd(gcd
, gcd
, bmap
->eq
[i
][0]);
340 if (isl_int_is_one(gcd
))
342 if (!isl_int_is_divisible_by(bmap
->eq
[i
][0], gcd
)) {
343 bmap
= isl_basic_map_set_to_empty(bmap
);
346 isl_seq_scale_down(bmap
->eq
[i
], bmap
->eq
[i
], gcd
, 1+total
);
349 for (i
= bmap
->n_ineq
- 1; i
>= 0; --i
) {
350 isl_seq_gcd(bmap
->ineq
[i
]+1, total
, &gcd
);
351 if (isl_int_is_zero(gcd
)) {
352 if (isl_int_is_neg(bmap
->ineq
[i
][0])) {
353 bmap
= isl_basic_map_set_to_empty(bmap
);
356 isl_basic_map_drop_inequality(bmap
, i
);
359 if (ISL_F_ISSET(bmap
, ISL_BASIC_MAP_RATIONAL
))
360 isl_int_gcd(gcd
, gcd
, bmap
->ineq
[i
][0]);
361 if (isl_int_is_one(gcd
))
363 isl_int_fdiv_q(bmap
->ineq
[i
][0], bmap
->ineq
[i
][0], gcd
);
364 isl_seq_scale_down(bmap
->ineq
[i
]+1, bmap
->ineq
[i
]+1, gcd
, total
);
371 struct isl_basic_set
*isl_basic_set_normalize_constraints(
372 struct isl_basic_set
*bset
)
374 return (struct isl_basic_set
*)isl_basic_map_normalize_constraints(
375 (struct isl_basic_map
*)bset
);
378 /* Assuming the variable at position "pos" has an integer coefficient
379 * in integer division "div", extract it from this integer division.
380 * "pos" is as determined by isl_basic_map_offset, i.e., pos == 0
381 * corresponds to the constant term.
383 * That is, the integer division is of the form
385 * floor((... + c * d * x_pos + ...)/d)
389 * floor((... + 0 * x_pos + ...)/d) + c * x_pos
391 static __isl_give isl_basic_map
*remove_var_from_div(
392 __isl_take isl_basic_map
*bmap
, int div
, int pos
)
397 isl_int_divexact(shift
, bmap
->div
[div
][1 + pos
], bmap
->div
[div
][0]);
398 isl_int_neg(shift
, shift
);
399 bmap
= isl_basic_map_shift_div(bmap
, div
, pos
, shift
);
400 isl_int_clear(shift
);
405 /* Check if integer division "div" has any integral coefficient
406 * (or constant term). If so, extract them from the integer division.
408 static __isl_give isl_basic_map
*remove_independent_vars_from_div(
409 __isl_take isl_basic_map
*bmap
, int div
)
412 unsigned total
= 1 + isl_basic_map_total_dim(bmap
);
414 for (i
= 0; i
< total
; ++i
) {
415 if (isl_int_is_zero(bmap
->div
[div
][1 + i
]))
417 if (!isl_int_is_divisible_by(bmap
->div
[div
][1 + i
],
420 bmap
= remove_var_from_div(bmap
, div
, i
);
428 /* Check if any known integer division has any integral coefficient
429 * (or constant term). If so, extract them from the integer division.
431 static __isl_give isl_basic_map
*remove_independent_vars_from_divs(
432 __isl_take isl_basic_map
*bmap
)
438 if (bmap
->n_div
== 0)
441 for (i
= 0; i
< bmap
->n_div
; ++i
) {
442 if (isl_int_is_zero(bmap
->div
[i
][0]))
444 bmap
= remove_independent_vars_from_div(bmap
, i
);
452 /* Remove any common factor in numerator and denominator of the div expression,
453 * not taking into account the constant term.
454 * That is, if the div is of the form
456 * floor((a + m f(x))/(m d))
460 * floor((floor(a/m) + f(x))/d)
462 * The difference {a/m}/d in the argument satisfies 0 <= {a/m}/d < 1/d
463 * and can therefore not influence the result of the floor.
465 static void normalize_div_expression(__isl_keep isl_basic_map
*bmap
, int div
)
467 unsigned total
= isl_basic_map_total_dim(bmap
);
468 isl_ctx
*ctx
= bmap
->ctx
;
470 if (isl_int_is_zero(bmap
->div
[div
][0]))
472 isl_seq_gcd(bmap
->div
[div
] + 2, total
, &ctx
->normalize_gcd
);
473 isl_int_gcd(ctx
->normalize_gcd
, ctx
->normalize_gcd
, bmap
->div
[div
][0]);
474 if (isl_int_is_one(ctx
->normalize_gcd
))
476 isl_int_fdiv_q(bmap
->div
[div
][1], bmap
->div
[div
][1],
478 isl_int_divexact(bmap
->div
[div
][0], bmap
->div
[div
][0],
480 isl_seq_scale_down(bmap
->div
[div
] + 2, bmap
->div
[div
] + 2,
481 ctx
->normalize_gcd
, total
);
484 /* Remove any common factor in numerator and denominator of a div expression,
485 * not taking into account the constant term.
486 * That is, look for any div of the form
488 * floor((a + m f(x))/(m d))
492 * floor((floor(a/m) + f(x))/d)
494 * The difference {a/m}/d in the argument satisfies 0 <= {a/m}/d < 1/d
495 * and can therefore not influence the result of the floor.
497 static __isl_give isl_basic_map
*normalize_div_expressions(
498 __isl_take isl_basic_map
*bmap
)
504 if (bmap
->n_div
== 0)
507 for (i
= 0; i
< bmap
->n_div
; ++i
)
508 normalize_div_expression(bmap
, i
);
513 /* Assumes divs have been ordered if keep_divs is set.
515 static void eliminate_var_using_equality(struct isl_basic_map
*bmap
,
516 unsigned pos
, isl_int
*eq
, int keep_divs
, int *progress
)
519 unsigned space_total
;
523 total
= isl_basic_map_total_dim(bmap
);
524 space_total
= isl_space_dim(bmap
->dim
, isl_dim_all
);
525 last_div
= isl_seq_last_non_zero(eq
+ 1 + space_total
, bmap
->n_div
);
526 for (k
= 0; k
< bmap
->n_eq
; ++k
) {
527 if (bmap
->eq
[k
] == eq
)
529 if (isl_int_is_zero(bmap
->eq
[k
][1+pos
]))
533 isl_seq_elim(bmap
->eq
[k
], eq
, 1+pos
, 1+total
, NULL
);
534 isl_seq_normalize(bmap
->ctx
, bmap
->eq
[k
], 1 + total
);
537 for (k
= 0; k
< bmap
->n_ineq
; ++k
) {
538 if (isl_int_is_zero(bmap
->ineq
[k
][1+pos
]))
542 isl_seq_elim(bmap
->ineq
[k
], eq
, 1+pos
, 1+total
, NULL
);
543 isl_seq_normalize(bmap
->ctx
, bmap
->ineq
[k
], 1 + total
);
544 ISL_F_CLR(bmap
, ISL_BASIC_MAP_NORMALIZED
);
547 for (k
= 0; k
< bmap
->n_div
; ++k
) {
548 if (isl_int_is_zero(bmap
->div
[k
][0]))
550 if (isl_int_is_zero(bmap
->div
[k
][1+1+pos
]))
554 /* We need to be careful about circular definitions,
555 * so for now we just remove the definition of div k
556 * if the equality contains any divs.
557 * If keep_divs is set, then the divs have been ordered
558 * and we can keep the definition as long as the result
561 if (last_div
== -1 || (keep_divs
&& last_div
< k
)) {
562 isl_seq_elim(bmap
->div
[k
]+1, eq
,
563 1+pos
, 1+total
, &bmap
->div
[k
][0]);
564 normalize_div_expression(bmap
, k
);
566 isl_seq_clr(bmap
->div
[k
], 1 + total
);
567 ISL_F_CLR(bmap
, ISL_BASIC_MAP_NORMALIZED
);
571 /* Assumes divs have been ordered if keep_divs is set.
573 static __isl_give isl_basic_map
*eliminate_div(__isl_take isl_basic_map
*bmap
,
574 isl_int
*eq
, unsigned div
, int keep_divs
)
576 unsigned pos
= isl_space_dim(bmap
->dim
, isl_dim_all
) + div
;
578 eliminate_var_using_equality(bmap
, pos
, eq
, keep_divs
, NULL
);
580 bmap
= isl_basic_map_drop_div(bmap
, div
);
585 /* Check if elimination of div "div" using equality "eq" would not
586 * result in a div depending on a later div.
588 static int ok_to_eliminate_div(struct isl_basic_map
*bmap
, isl_int
*eq
,
593 unsigned space_total
= isl_space_dim(bmap
->dim
, isl_dim_all
);
594 unsigned pos
= space_total
+ div
;
596 last_div
= isl_seq_last_non_zero(eq
+ 1 + space_total
, bmap
->n_div
);
597 if (last_div
< 0 || last_div
<= div
)
600 for (k
= 0; k
<= last_div
; ++k
) {
601 if (isl_int_is_zero(bmap
->div
[k
][0]))
603 if (!isl_int_is_zero(bmap
->div
[k
][1 + 1 + pos
]))
610 /* Elimininate divs based on equalities
612 static struct isl_basic_map
*eliminate_divs_eq(
613 struct isl_basic_map
*bmap
, int *progress
)
620 bmap
= isl_basic_map_order_divs(bmap
);
625 off
= 1 + isl_space_dim(bmap
->dim
, isl_dim_all
);
627 for (d
= bmap
->n_div
- 1; d
>= 0 ; --d
) {
628 for (i
= 0; i
< bmap
->n_eq
; ++i
) {
629 if (!isl_int_is_one(bmap
->eq
[i
][off
+ d
]) &&
630 !isl_int_is_negone(bmap
->eq
[i
][off
+ d
]))
632 if (!ok_to_eliminate_div(bmap
, bmap
->eq
[i
], d
))
636 bmap
= eliminate_div(bmap
, bmap
->eq
[i
], d
, 1);
637 if (isl_basic_map_drop_equality(bmap
, i
) < 0)
638 return isl_basic_map_free(bmap
);
643 return eliminate_divs_eq(bmap
, progress
);
647 /* Elimininate divs based on inequalities
649 static struct isl_basic_map
*eliminate_divs_ineq(
650 struct isl_basic_map
*bmap
, int *progress
)
661 off
= 1 + isl_space_dim(bmap
->dim
, isl_dim_all
);
663 for (d
= bmap
->n_div
- 1; d
>= 0 ; --d
) {
664 for (i
= 0; i
< bmap
->n_eq
; ++i
)
665 if (!isl_int_is_zero(bmap
->eq
[i
][off
+ d
]))
669 for (i
= 0; i
< bmap
->n_ineq
; ++i
)
670 if (isl_int_abs_gt(bmap
->ineq
[i
][off
+ d
], ctx
->one
))
672 if (i
< bmap
->n_ineq
)
675 bmap
= isl_basic_map_eliminate_vars(bmap
, (off
-1)+d
, 1);
676 if (!bmap
|| ISL_F_ISSET(bmap
, ISL_BASIC_MAP_EMPTY
))
678 bmap
= isl_basic_map_drop_div(bmap
, d
);
685 struct isl_basic_map
*isl_basic_map_gauss(
686 struct isl_basic_map
*bmap
, int *progress
)
694 bmap
= isl_basic_map_order_divs(bmap
);
699 total
= isl_basic_map_total_dim(bmap
);
700 total_var
= total
- bmap
->n_div
;
702 last_var
= total
- 1;
703 for (done
= 0; done
< bmap
->n_eq
; ++done
) {
704 for (; last_var
>= 0; --last_var
) {
705 for (k
= done
; k
< bmap
->n_eq
; ++k
)
706 if (!isl_int_is_zero(bmap
->eq
[k
][1+last_var
]))
714 swap_equality(bmap
, k
, done
);
715 if (isl_int_is_neg(bmap
->eq
[done
][1+last_var
]))
716 isl_seq_neg(bmap
->eq
[done
], bmap
->eq
[done
], 1+total
);
718 eliminate_var_using_equality(bmap
, last_var
, bmap
->eq
[done
], 1,
721 if (last_var
>= total_var
&&
722 isl_int_is_zero(bmap
->div
[last_var
- total_var
][0])) {
723 unsigned div
= last_var
- total_var
;
724 isl_seq_neg(bmap
->div
[div
]+1, bmap
->eq
[done
], 1+total
);
725 isl_int_set_si(bmap
->div
[div
][1+1+last_var
], 0);
726 isl_int_set(bmap
->div
[div
][0],
727 bmap
->eq
[done
][1+last_var
]);
730 ISL_F_CLR(bmap
, ISL_BASIC_MAP_NORMALIZED
);
733 if (done
== bmap
->n_eq
)
735 for (k
= done
; k
< bmap
->n_eq
; ++k
) {
736 if (isl_int_is_zero(bmap
->eq
[k
][0]))
738 return isl_basic_map_set_to_empty(bmap
);
740 isl_basic_map_free_equality(bmap
, bmap
->n_eq
-done
);
744 struct isl_basic_set
*isl_basic_set_gauss(
745 struct isl_basic_set
*bset
, int *progress
)
747 return (struct isl_basic_set
*)isl_basic_map_gauss(
748 (struct isl_basic_map
*)bset
, progress
);
752 static unsigned int round_up(unsigned int v
)
763 /* Hash table of inequalities in a basic map.
764 * "index" is an array of addresses of inequalities in the basic map, some
765 * of which are NULL. The inequalities are hashed on the coefficients
766 * except the constant term.
767 * "size" is the number of elements in the array and is always a power of two
768 * "bits" is the number of bits need to represent an index into the array.
769 * "total" is the total dimension of the basic map.
771 struct isl_constraint_index
{
778 /* Fill in the "ci" data structure for holding the inequalities of "bmap".
780 static isl_stat
create_constraint_index(struct isl_constraint_index
*ci
,
781 __isl_keep isl_basic_map
*bmap
)
787 return isl_stat_error
;
788 ci
->total
= isl_basic_set_total_dim(bmap
);
789 if (bmap
->n_ineq
== 0)
791 ci
->size
= round_up(4 * (bmap
->n_ineq
+ 1) / 3 - 1);
792 ci
->bits
= ffs(ci
->size
) - 1;
793 ctx
= isl_basic_map_get_ctx(bmap
);
794 ci
->index
= isl_calloc_array(ctx
, isl_int
**, ci
->size
);
796 return isl_stat_error
;
801 /* Free the memory allocated by create_constraint_index.
803 static void constraint_index_free(struct isl_constraint_index
*ci
)
808 /* Return the position in ci->index that contains the address of
809 * an inequality that is equal to *ineq up to the constant term,
810 * provided this address is not identical to "ineq".
811 * If there is no such inequality, then return the position where
812 * such an inequality should be inserted.
814 static int hash_index_ineq(struct isl_constraint_index
*ci
, isl_int
**ineq
)
817 uint32_t hash
= isl_seq_get_hash_bits((*ineq
) + 1, ci
->total
, ci
->bits
);
818 for (h
= hash
; ci
->index
[h
]; h
= (h
+1) % ci
->size
)
819 if (ineq
!= ci
->index
[h
] &&
820 isl_seq_eq((*ineq
) + 1, ci
->index
[h
][0]+1, ci
->total
))
825 /* Return the position in ci->index that contains the address of
826 * an inequality that is equal to the k'th inequality of "bmap"
827 * up to the constant term, provided it does not point to the very
829 * If there is no such inequality, then return the position where
830 * such an inequality should be inserted.
832 static int hash_index(struct isl_constraint_index
*ci
,
833 __isl_keep isl_basic_map
*bmap
, int k
)
835 return hash_index_ineq(ci
, &bmap
->ineq
[k
]);
838 static int set_hash_index(struct isl_constraint_index
*ci
,
839 struct isl_basic_set
*bset
, int k
)
841 return hash_index(ci
, bset
, k
);
844 /* Fill in the "ci" data structure with the inequalities of "bset".
846 static isl_stat
setup_constraint_index(struct isl_constraint_index
*ci
,
847 __isl_keep isl_basic_set
*bset
)
851 if (create_constraint_index(ci
, bset
) < 0)
852 return isl_stat_error
;
854 for (k
= 0; k
< bset
->n_ineq
; ++k
) {
855 h
= set_hash_index(ci
, bset
, k
);
856 ci
->index
[h
] = &bset
->ineq
[k
];
862 /* Is the inequality ineq (obviously) redundant with respect
863 * to the constraints in "ci"?
865 * Look for an inequality in "ci" with the same coefficients and then
866 * check if the contant term of "ineq" is greater than or equal
867 * to the constant term of that inequality. If so, "ineq" is clearly
870 * Note that hash_index_ineq ignores a stored constraint if it has
871 * the same address as the passed inequality. It is ok to pass
872 * the address of a local variable here since it will never be
873 * the same as the address of a constraint in "ci".
875 static isl_bool
constraint_index_is_redundant(struct isl_constraint_index
*ci
,
880 h
= hash_index_ineq(ci
, &ineq
);
882 return isl_bool_false
;
883 return isl_int_ge(ineq
[0], (*ci
->index
[h
])[0]);
886 /* If we can eliminate more than one div, then we need to make
887 * sure we do it from last div to first div, in order not to
888 * change the position of the other divs that still need to
891 static struct isl_basic_map
*remove_duplicate_divs(
892 struct isl_basic_map
*bmap
, int *progress
)
904 bmap
= isl_basic_map_order_divs(bmap
);
905 if (!bmap
|| bmap
->n_div
<= 1)
908 total_var
= isl_space_dim(bmap
->dim
, isl_dim_all
);
909 total
= total_var
+ bmap
->n_div
;
912 for (k
= bmap
->n_div
- 1; k
>= 0; --k
)
913 if (!isl_int_is_zero(bmap
->div
[k
][0]))
918 size
= round_up(4 * bmap
->n_div
/ 3 - 1);
921 elim_for
= isl_calloc_array(ctx
, int, bmap
->n_div
);
922 bits
= ffs(size
) - 1;
923 index
= isl_calloc_array(ctx
, int, size
);
924 if (!elim_for
|| !index
)
926 eq
= isl_blk_alloc(ctx
, 1+total
);
927 if (isl_blk_is_error(eq
))
930 isl_seq_clr(eq
.data
, 1+total
);
931 index
[isl_seq_get_hash_bits(bmap
->div
[k
], 2+total
, bits
)] = k
+ 1;
932 for (--k
; k
>= 0; --k
) {
935 if (isl_int_is_zero(bmap
->div
[k
][0]))
938 hash
= isl_seq_get_hash_bits(bmap
->div
[k
], 2+total
, bits
);
939 for (h
= hash
; index
[h
]; h
= (h
+1) % size
)
940 if (isl_seq_eq(bmap
->div
[k
],
941 bmap
->div
[index
[h
]-1], 2+total
))
950 for (l
= bmap
->n_div
- 1; l
>= 0; --l
) {
954 isl_int_set_si(eq
.data
[1+total_var
+k
], -1);
955 isl_int_set_si(eq
.data
[1+total_var
+l
], 1);
956 bmap
= eliminate_div(bmap
, eq
.data
, l
, 1);
959 isl_int_set_si(eq
.data
[1+total_var
+k
], 0);
960 isl_int_set_si(eq
.data
[1+total_var
+l
], 0);
963 isl_blk_free(ctx
, eq
);
970 static int n_pure_div_eq(struct isl_basic_map
*bmap
)
975 total
= isl_space_dim(bmap
->dim
, isl_dim_all
);
976 for (i
= 0, j
= bmap
->n_div
-1; i
< bmap
->n_eq
; ++i
) {
977 while (j
>= 0 && isl_int_is_zero(bmap
->eq
[i
][1 + total
+ j
]))
981 if (isl_seq_first_non_zero(bmap
->eq
[i
] + 1 + total
, j
) != -1)
987 /* Normalize divs that appear in equalities.
989 * In particular, we assume that bmap contains some equalities
994 * and we want to replace the set of e_i by a minimal set and
995 * such that the new e_i have a canonical representation in terms
997 * If any of the equalities involves more than one divs, then
998 * we currently simply bail out.
1000 * Let us first additionally assume that all equalities involve
1001 * a div. The equalities then express modulo constraints on the
1002 * remaining variables and we can use "parameter compression"
1003 * to find a minimal set of constraints. The result is a transformation
1005 * x = T(x') = x_0 + G x'
1007 * with G a lower-triangular matrix with all elements below the diagonal
1008 * non-negative and smaller than the diagonal element on the same row.
1009 * We first normalize x_0 by making the same property hold in the affine
1011 * The rows i of G with a 1 on the diagonal do not impose any modulo
1012 * constraint and simply express x_i = x'_i.
1013 * For each of the remaining rows i, we introduce a div and a corresponding
1014 * equality. In particular
1016 * g_ii e_j = x_i - g_i(x')
1018 * where each x'_k is replaced either by x_k (if g_kk = 1) or the
1019 * corresponding div (if g_kk != 1).
1021 * If there are any equalities not involving any div, then we
1022 * first apply a variable compression on the variables x:
1024 * x = C x'' x'' = C_2 x
1026 * and perform the above parameter compression on A C instead of on A.
1027 * The resulting compression is then of the form
1029 * x'' = T(x') = x_0 + G x'
1031 * and in constructing the new divs and the corresponding equalities,
1032 * we have to replace each x'', i.e., the x'_k with (g_kk = 1),
1033 * by the corresponding row from C_2.
1035 static struct isl_basic_map
*normalize_divs(
1036 struct isl_basic_map
*bmap
, int *progress
)
1043 struct isl_mat
*T
= NULL
;
1044 struct isl_mat
*C
= NULL
;
1045 struct isl_mat
*C2
= NULL
;
1048 int dropped
, needed
;
1053 if (bmap
->n_div
== 0)
1056 if (bmap
->n_eq
== 0)
1059 if (ISL_F_ISSET(bmap
, ISL_BASIC_MAP_NORMALIZED_DIVS
))
1062 total
= isl_space_dim(bmap
->dim
, isl_dim_all
);
1063 div_eq
= n_pure_div_eq(bmap
);
1067 if (div_eq
< bmap
->n_eq
) {
1068 B
= isl_mat_sub_alloc6(bmap
->ctx
, bmap
->eq
, div_eq
,
1069 bmap
->n_eq
- div_eq
, 0, 1 + total
);
1070 C
= isl_mat_variable_compression(B
, &C2
);
1073 if (C
->n_col
== 0) {
1074 bmap
= isl_basic_map_set_to_empty(bmap
);
1081 d
= isl_vec_alloc(bmap
->ctx
, div_eq
);
1084 for (i
= 0, j
= bmap
->n_div
-1; i
< div_eq
; ++i
) {
1085 while (j
>= 0 && isl_int_is_zero(bmap
->eq
[i
][1 + total
+ j
]))
1087 isl_int_set(d
->block
.data
[i
], bmap
->eq
[i
][1 + total
+ j
]);
1089 B
= isl_mat_sub_alloc6(bmap
->ctx
, bmap
->eq
, 0, div_eq
, 0, 1 + total
);
1092 B
= isl_mat_product(B
, C
);
1096 T
= isl_mat_parameter_compression(B
, d
);
1099 if (T
->n_col
== 0) {
1100 bmap
= isl_basic_map_set_to_empty(bmap
);
1106 for (i
= 0; i
< T
->n_row
- 1; ++i
) {
1107 isl_int_fdiv_q(v
, T
->row
[1 + i
][0], T
->row
[1 + i
][1 + i
]);
1108 if (isl_int_is_zero(v
))
1110 isl_mat_col_submul(T
, 0, v
, 1 + i
);
1113 pos
= isl_alloc_array(bmap
->ctx
, int, T
->n_row
);
1116 /* We have to be careful because dropping equalities may reorder them */
1118 for (j
= bmap
->n_div
- 1; j
>= 0; --j
) {
1119 for (i
= 0; i
< bmap
->n_eq
; ++i
)
1120 if (!isl_int_is_zero(bmap
->eq
[i
][1 + total
+ j
]))
1122 if (i
< bmap
->n_eq
) {
1123 bmap
= isl_basic_map_drop_div(bmap
, j
);
1124 isl_basic_map_drop_equality(bmap
, i
);
1130 for (i
= 1; i
< T
->n_row
; ++i
) {
1131 if (isl_int_is_one(T
->row
[i
][i
]))
1136 if (needed
> dropped
) {
1137 bmap
= isl_basic_map_extend_space(bmap
, isl_space_copy(bmap
->dim
),
1142 for (i
= 1; i
< T
->n_row
; ++i
) {
1143 if (isl_int_is_one(T
->row
[i
][i
]))
1145 k
= isl_basic_map_alloc_div(bmap
);
1146 pos
[i
] = 1 + total
+ k
;
1147 isl_seq_clr(bmap
->div
[k
] + 1, 1 + total
+ bmap
->n_div
);
1148 isl_int_set(bmap
->div
[k
][0], T
->row
[i
][i
]);
1150 isl_seq_cpy(bmap
->div
[k
] + 1, C2
->row
[i
], 1 + total
);
1152 isl_int_set_si(bmap
->div
[k
][1 + i
], 1);
1153 for (j
= 0; j
< i
; ++j
) {
1154 if (isl_int_is_zero(T
->row
[i
][j
]))
1156 if (pos
[j
] < T
->n_row
&& C2
)
1157 isl_seq_submul(bmap
->div
[k
] + 1, T
->row
[i
][j
],
1158 C2
->row
[pos
[j
]], 1 + total
);
1160 isl_int_neg(bmap
->div
[k
][1 + pos
[j
]],
1163 j
= isl_basic_map_alloc_equality(bmap
);
1164 isl_seq_neg(bmap
->eq
[j
], bmap
->div
[k
]+1, 1+total
+bmap
->n_div
);
1165 isl_int_set(bmap
->eq
[j
][pos
[i
]], bmap
->div
[k
][0]);
1174 ISL_F_SET(bmap
, ISL_BASIC_MAP_NORMALIZED_DIVS
);
1184 static struct isl_basic_map
*set_div_from_lower_bound(
1185 struct isl_basic_map
*bmap
, int div
, int ineq
)
1187 unsigned total
= 1 + isl_space_dim(bmap
->dim
, isl_dim_all
);
1189 isl_seq_neg(bmap
->div
[div
] + 1, bmap
->ineq
[ineq
], total
+ bmap
->n_div
);
1190 isl_int_set(bmap
->div
[div
][0], bmap
->ineq
[ineq
][total
+ div
]);
1191 isl_int_add(bmap
->div
[div
][1], bmap
->div
[div
][1], bmap
->div
[div
][0]);
1192 isl_int_sub_ui(bmap
->div
[div
][1], bmap
->div
[div
][1], 1);
1193 isl_int_set_si(bmap
->div
[div
][1 + total
+ div
], 0);
1198 /* Check whether it is ok to define a div based on an inequality.
1199 * To avoid the introduction of circular definitions of divs, we
1200 * do not allow such a definition if the resulting expression would refer to
1201 * any other undefined divs or if any known div is defined in
1202 * terms of the unknown div.
1204 static int ok_to_set_div_from_bound(struct isl_basic_map
*bmap
,
1208 unsigned total
= 1 + isl_space_dim(bmap
->dim
, isl_dim_all
);
1210 /* Not defined in terms of unknown divs */
1211 for (j
= 0; j
< bmap
->n_div
; ++j
) {
1214 if (isl_int_is_zero(bmap
->ineq
[ineq
][total
+ j
]))
1216 if (isl_int_is_zero(bmap
->div
[j
][0]))
1220 /* No other div defined in terms of this one => avoid loops */
1221 for (j
= 0; j
< bmap
->n_div
; ++j
) {
1224 if (isl_int_is_zero(bmap
->div
[j
][0]))
1226 if (!isl_int_is_zero(bmap
->div
[j
][1 + total
+ div
]))
1233 /* Would an expression for div "div" based on inequality "ineq" of "bmap"
1234 * be a better expression than the current one?
1236 * If we do not have any expression yet, then any expression would be better.
1237 * Otherwise we check if the last variable involved in the inequality
1238 * (disregarding the div that it would define) is in an earlier position
1239 * than the last variable involved in the current div expression.
1241 static int better_div_constraint(__isl_keep isl_basic_map
*bmap
,
1244 unsigned total
= 1 + isl_space_dim(bmap
->dim
, isl_dim_all
);
1248 if (isl_int_is_zero(bmap
->div
[div
][0]))
1251 if (isl_seq_last_non_zero(bmap
->ineq
[ineq
] + total
+ div
+ 1,
1252 bmap
->n_div
- (div
+ 1)) >= 0)
1255 last_ineq
= isl_seq_last_non_zero(bmap
->ineq
[ineq
], total
+ div
);
1256 last_div
= isl_seq_last_non_zero(bmap
->div
[div
] + 1,
1257 total
+ bmap
->n_div
);
1259 return last_ineq
< last_div
;
1262 /* Given two constraints "k" and "l" that are opposite to each other,
1263 * except for the constant term, check if we can use them
1264 * to obtain an expression for one of the hitherto unknown divs or
1265 * a "better" expression for a div for which we already have an expression.
1266 * "sum" is the sum of the constant terms of the constraints.
1267 * If this sum is strictly smaller than the coefficient of one
1268 * of the divs, then this pair can be used define the div.
1269 * To avoid the introduction of circular definitions of divs, we
1270 * do not use the pair if the resulting expression would refer to
1271 * any other undefined divs or if any known div is defined in
1272 * terms of the unknown div.
1274 static struct isl_basic_map
*check_for_div_constraints(
1275 struct isl_basic_map
*bmap
, int k
, int l
, isl_int sum
, int *progress
)
1278 unsigned total
= 1 + isl_space_dim(bmap
->dim
, isl_dim_all
);
1280 for (i
= 0; i
< bmap
->n_div
; ++i
) {
1281 if (isl_int_is_zero(bmap
->ineq
[k
][total
+ i
]))
1283 if (isl_int_abs_ge(sum
, bmap
->ineq
[k
][total
+ i
]))
1285 if (!better_div_constraint(bmap
, i
, k
))
1287 if (!ok_to_set_div_from_bound(bmap
, i
, k
))
1289 if (isl_int_is_pos(bmap
->ineq
[k
][total
+ i
]))
1290 bmap
= set_div_from_lower_bound(bmap
, i
, k
);
1292 bmap
= set_div_from_lower_bound(bmap
, i
, l
);
1300 __isl_give isl_basic_map
*isl_basic_map_remove_duplicate_constraints(
1301 __isl_take isl_basic_map
*bmap
, int *progress
, int detect_divs
)
1303 struct isl_constraint_index ci
;
1305 unsigned total
= isl_basic_map_total_dim(bmap
);
1308 if (!bmap
|| bmap
->n_ineq
<= 1)
1311 if (create_constraint_index(&ci
, bmap
) < 0)
1314 h
= isl_seq_get_hash_bits(bmap
->ineq
[0] + 1, total
, ci
.bits
);
1315 ci
.index
[h
] = &bmap
->ineq
[0];
1316 for (k
= 1; k
< bmap
->n_ineq
; ++k
) {
1317 h
= hash_index(&ci
, bmap
, k
);
1319 ci
.index
[h
] = &bmap
->ineq
[k
];
1324 l
= ci
.index
[h
] - &bmap
->ineq
[0];
1325 if (isl_int_lt(bmap
->ineq
[k
][0], bmap
->ineq
[l
][0]))
1326 swap_inequality(bmap
, k
, l
);
1327 isl_basic_map_drop_inequality(bmap
, k
);
1331 for (k
= 0; k
< bmap
->n_ineq
-1; ++k
) {
1332 isl_seq_neg(bmap
->ineq
[k
]+1, bmap
->ineq
[k
]+1, total
);
1333 h
= hash_index(&ci
, bmap
, k
);
1334 isl_seq_neg(bmap
->ineq
[k
]+1, bmap
->ineq
[k
]+1, total
);
1337 l
= ci
.index
[h
] - &bmap
->ineq
[0];
1338 isl_int_add(sum
, bmap
->ineq
[k
][0], bmap
->ineq
[l
][0]);
1339 if (isl_int_is_pos(sum
)) {
1341 bmap
= check_for_div_constraints(bmap
, k
, l
,
1345 if (isl_int_is_zero(sum
)) {
1346 /* We need to break out of the loop after these
1347 * changes since the contents of the hash
1348 * will no longer be valid.
1349 * Plus, we probably we want to regauss first.
1353 isl_basic_map_drop_inequality(bmap
, l
);
1354 isl_basic_map_inequality_to_equality(bmap
, k
);
1356 bmap
= isl_basic_map_set_to_empty(bmap
);
1361 constraint_index_free(&ci
);
1365 /* Detect all pairs of inequalities that form an equality.
1367 * isl_basic_map_remove_duplicate_constraints detects at most one such pair.
1368 * Call it repeatedly while it is making progress.
1370 __isl_give isl_basic_map
*isl_basic_map_detect_inequality_pairs(
1371 __isl_take isl_basic_map
*bmap
, int *progress
)
1377 bmap
= isl_basic_map_remove_duplicate_constraints(bmap
,
1379 if (progress
&& duplicate
)
1381 } while (duplicate
);
1386 /* Eliminate knowns divs from constraints where they appear with
1387 * a (positive or negative) unit coefficient.
1391 * floor(e/m) + f >= 0
1399 * -floor(e/m) + f >= 0
1403 * -e + m f + m - 1 >= 0
1405 * The first conversion is valid because floor(e/m) >= -f is equivalent
1406 * to e/m >= -f because -f is an integral expression.
1407 * The second conversion follows from the fact that
1409 * -floor(e/m) = ceil(-e/m) = floor((-e + m - 1)/m)
1412 * Note that one of the div constraints may have been eliminated
1413 * due to being redundant with respect to the constraint that is
1414 * being modified by this function. The modified constraint may
1415 * no longer imply this div constraint, so we add it back to make
1416 * sure we do not lose any information.
1418 * We skip integral divs, i.e., those with denominator 1, as we would
1419 * risk eliminating the div from the div constraints. We do not need
1420 * to handle those divs here anyway since the div constraints will turn
1421 * out to form an equality and this equality can then be use to eliminate
1422 * the div from all constraints.
1424 static __isl_give isl_basic_map
*eliminate_unit_divs(
1425 __isl_take isl_basic_map
*bmap
, int *progress
)
1434 ctx
= isl_basic_map_get_ctx(bmap
);
1435 total
= 1 + isl_space_dim(bmap
->dim
, isl_dim_all
);
1437 for (i
= 0; i
< bmap
->n_div
; ++i
) {
1438 if (isl_int_is_zero(bmap
->div
[i
][0]))
1440 if (isl_int_is_one(bmap
->div
[i
][0]))
1442 for (j
= 0; j
< bmap
->n_ineq
; ++j
) {
1445 if (!isl_int_is_one(bmap
->ineq
[j
][total
+ i
]) &&
1446 !isl_int_is_negone(bmap
->ineq
[j
][total
+ i
]))
1451 s
= isl_int_sgn(bmap
->ineq
[j
][total
+ i
]);
1452 isl_int_set_si(bmap
->ineq
[j
][total
+ i
], 0);
1454 isl_seq_combine(bmap
->ineq
[j
],
1455 ctx
->negone
, bmap
->div
[i
] + 1,
1456 bmap
->div
[i
][0], bmap
->ineq
[j
],
1457 total
+ bmap
->n_div
);
1459 isl_seq_combine(bmap
->ineq
[j
],
1460 ctx
->one
, bmap
->div
[i
] + 1,
1461 bmap
->div
[i
][0], bmap
->ineq
[j
],
1462 total
+ bmap
->n_div
);
1464 isl_int_add(bmap
->ineq
[j
][0],
1465 bmap
->ineq
[j
][0], bmap
->div
[i
][0]);
1466 isl_int_sub_ui(bmap
->ineq
[j
][0],
1467 bmap
->ineq
[j
][0], 1);
1470 bmap
= isl_basic_map_extend_constraints(bmap
, 0, 1);
1471 if (isl_basic_map_add_div_constraint(bmap
, i
, s
) < 0)
1472 return isl_basic_map_free(bmap
);
1479 struct isl_basic_map
*isl_basic_map_simplify(struct isl_basic_map
*bmap
)
1488 if (isl_basic_map_plain_is_empty(bmap
))
1490 bmap
= isl_basic_map_normalize_constraints(bmap
);
1491 bmap
= remove_independent_vars_from_divs(bmap
);
1492 bmap
= normalize_div_expressions(bmap
);
1493 bmap
= remove_duplicate_divs(bmap
, &progress
);
1494 bmap
= eliminate_unit_divs(bmap
, &progress
);
1495 bmap
= eliminate_divs_eq(bmap
, &progress
);
1496 bmap
= eliminate_divs_ineq(bmap
, &progress
);
1497 bmap
= isl_basic_map_gauss(bmap
, &progress
);
1498 /* requires equalities in normal form */
1499 bmap
= normalize_divs(bmap
, &progress
);
1500 bmap
= isl_basic_map_remove_duplicate_constraints(bmap
,
1502 if (bmap
&& progress
)
1503 ISL_F_CLR(bmap
, ISL_BASIC_MAP_REDUCED_COEFFICIENTS
);
1508 struct isl_basic_set
*isl_basic_set_simplify(struct isl_basic_set
*bset
)
1510 return (struct isl_basic_set
*)
1511 isl_basic_map_simplify((struct isl_basic_map
*)bset
);
1515 int isl_basic_map_is_div_constraint(__isl_keep isl_basic_map
*bmap
,
1516 isl_int
*constraint
, unsigned div
)
1523 pos
= 1 + isl_space_dim(bmap
->dim
, isl_dim_all
) + div
;
1525 if (isl_int_eq(constraint
[pos
], bmap
->div
[div
][0])) {
1527 isl_int_sub(bmap
->div
[div
][1],
1528 bmap
->div
[div
][1], bmap
->div
[div
][0]);
1529 isl_int_add_ui(bmap
->div
[div
][1], bmap
->div
[div
][1], 1);
1530 neg
= isl_seq_is_neg(constraint
, bmap
->div
[div
]+1, pos
);
1531 isl_int_sub_ui(bmap
->div
[div
][1], bmap
->div
[div
][1], 1);
1532 isl_int_add(bmap
->div
[div
][1],
1533 bmap
->div
[div
][1], bmap
->div
[div
][0]);
1536 if (isl_seq_first_non_zero(constraint
+pos
+1,
1537 bmap
->n_div
-div
-1) != -1)
1539 } else if (isl_int_abs_eq(constraint
[pos
], bmap
->div
[div
][0])) {
1540 if (!isl_seq_eq(constraint
, bmap
->div
[div
]+1, pos
))
1542 if (isl_seq_first_non_zero(constraint
+pos
+1,
1543 bmap
->n_div
-div
-1) != -1)
1551 int isl_basic_set_is_div_constraint(__isl_keep isl_basic_set
*bset
,
1552 isl_int
*constraint
, unsigned div
)
1554 return isl_basic_map_is_div_constraint(bset
, constraint
, div
);
1558 /* If the only constraints a div d=floor(f/m)
1559 * appears in are its two defining constraints
1562 * -(f - (m - 1)) + m d >= 0
1564 * then it can safely be removed.
1566 static int div_is_redundant(struct isl_basic_map
*bmap
, int div
)
1569 unsigned pos
= 1 + isl_space_dim(bmap
->dim
, isl_dim_all
) + div
;
1571 for (i
= 0; i
< bmap
->n_eq
; ++i
)
1572 if (!isl_int_is_zero(bmap
->eq
[i
][pos
]))
1575 for (i
= 0; i
< bmap
->n_ineq
; ++i
) {
1576 if (isl_int_is_zero(bmap
->ineq
[i
][pos
]))
1578 if (!isl_basic_map_is_div_constraint(bmap
, bmap
->ineq
[i
], div
))
1582 for (i
= 0; i
< bmap
->n_div
; ++i
) {
1583 if (isl_int_is_zero(bmap
->div
[i
][0]))
1585 if (!isl_int_is_zero(bmap
->div
[i
][1+pos
]))
1593 * Remove divs that don't occur in any of the constraints or other divs.
1594 * These can arise when dropping constraints from a basic map or
1595 * when the divs of a basic map have been temporarily aligned
1596 * with the divs of another basic map.
1598 static struct isl_basic_map
*remove_redundant_divs(struct isl_basic_map
*bmap
)
1605 for (i
= bmap
->n_div
-1; i
>= 0; --i
) {
1606 if (!div_is_redundant(bmap
, i
))
1608 bmap
= isl_basic_map_drop_div(bmap
, i
);
1613 /* Mark "bmap" as final, without checking for obviously redundant
1614 * integer divisions. This function should be used when "bmap"
1615 * is known not to involve any such integer divisions.
1617 __isl_give isl_basic_map
*isl_basic_map_mark_final(
1618 __isl_take isl_basic_map
*bmap
)
1622 ISL_F_SET(bmap
, ISL_BASIC_SET_FINAL
);
1626 /* Mark "bmap" as final, after removing obviously redundant integer divisions.
1628 struct isl_basic_map
*isl_basic_map_finalize(struct isl_basic_map
*bmap
)
1630 bmap
= remove_redundant_divs(bmap
);
1631 bmap
= isl_basic_map_mark_final(bmap
);
1635 struct isl_basic_set
*isl_basic_set_finalize(struct isl_basic_set
*bset
)
1637 return (struct isl_basic_set
*)
1638 isl_basic_map_finalize((struct isl_basic_map
*)bset
);
1641 struct isl_set
*isl_set_finalize(struct isl_set
*set
)
1647 for (i
= 0; i
< set
->n
; ++i
) {
1648 set
->p
[i
] = isl_basic_set_finalize(set
->p
[i
]);
1658 struct isl_map
*isl_map_finalize(struct isl_map
*map
)
1664 for (i
= 0; i
< map
->n
; ++i
) {
1665 map
->p
[i
] = isl_basic_map_finalize(map
->p
[i
]);
1669 ISL_F_CLR(map
, ISL_MAP_NORMALIZED
);
1677 /* Remove definition of any div that is defined in terms of the given variable.
1678 * The div itself is not removed. Functions such as
1679 * eliminate_divs_ineq depend on the other divs remaining in place.
1681 static struct isl_basic_map
*remove_dependent_vars(struct isl_basic_map
*bmap
,
1689 for (i
= 0; i
< bmap
->n_div
; ++i
) {
1690 if (isl_int_is_zero(bmap
->div
[i
][0]))
1692 if (isl_int_is_zero(bmap
->div
[i
][1+1+pos
]))
1694 isl_int_set_si(bmap
->div
[i
][0], 0);
1699 /* Eliminate the specified variables from the constraints using
1700 * Fourier-Motzkin. The variables themselves are not removed.
1702 struct isl_basic_map
*isl_basic_map_eliminate_vars(
1703 struct isl_basic_map
*bmap
, unsigned pos
, unsigned n
)
1714 total
= isl_basic_map_total_dim(bmap
);
1716 bmap
= isl_basic_map_cow(bmap
);
1717 for (d
= pos
+ n
- 1; d
>= 0 && d
>= pos
; --d
)
1718 bmap
= remove_dependent_vars(bmap
, d
);
1722 for (d
= pos
+ n
- 1;
1723 d
>= 0 && d
>= total
- bmap
->n_div
&& d
>= pos
; --d
)
1724 isl_seq_clr(bmap
->div
[d
-(total
-bmap
->n_div
)], 2+total
);
1725 for (d
= pos
+ n
- 1; d
>= 0 && d
>= pos
; --d
) {
1726 int n_lower
, n_upper
;
1729 for (i
= 0; i
< bmap
->n_eq
; ++i
) {
1730 if (isl_int_is_zero(bmap
->eq
[i
][1+d
]))
1732 eliminate_var_using_equality(bmap
, d
, bmap
->eq
[i
], 0, NULL
);
1733 isl_basic_map_drop_equality(bmap
, i
);
1741 for (i
= 0; i
< bmap
->n_ineq
; ++i
) {
1742 if (isl_int_is_pos(bmap
->ineq
[i
][1+d
]))
1744 else if (isl_int_is_neg(bmap
->ineq
[i
][1+d
]))
1747 bmap
= isl_basic_map_extend_constraints(bmap
,
1748 0, n_lower
* n_upper
);
1751 for (i
= bmap
->n_ineq
- 1; i
>= 0; --i
) {
1753 if (isl_int_is_zero(bmap
->ineq
[i
][1+d
]))
1756 for (j
= 0; j
< i
; ++j
) {
1757 if (isl_int_is_zero(bmap
->ineq
[j
][1+d
]))
1760 if (isl_int_sgn(bmap
->ineq
[i
][1+d
]) ==
1761 isl_int_sgn(bmap
->ineq
[j
][1+d
]))
1763 k
= isl_basic_map_alloc_inequality(bmap
);
1766 isl_seq_cpy(bmap
->ineq
[k
], bmap
->ineq
[i
],
1768 isl_seq_elim(bmap
->ineq
[k
], bmap
->ineq
[j
],
1769 1+d
, 1+total
, NULL
);
1771 isl_basic_map_drop_inequality(bmap
, i
);
1774 if (n_lower
> 0 && n_upper
> 0) {
1775 bmap
= isl_basic_map_normalize_constraints(bmap
);
1776 bmap
= isl_basic_map_remove_duplicate_constraints(bmap
,
1778 bmap
= isl_basic_map_gauss(bmap
, NULL
);
1779 bmap
= isl_basic_map_remove_redundancies(bmap
);
1783 if (ISL_F_ISSET(bmap
, ISL_BASIC_MAP_EMPTY
))
1787 ISL_F_CLR(bmap
, ISL_BASIC_MAP_NORMALIZED
);
1789 bmap
= isl_basic_map_gauss(bmap
, NULL
);
1792 isl_basic_map_free(bmap
);
1796 struct isl_basic_set
*isl_basic_set_eliminate_vars(
1797 struct isl_basic_set
*bset
, unsigned pos
, unsigned n
)
1799 return (struct isl_basic_set
*)isl_basic_map_eliminate_vars(
1800 (struct isl_basic_map
*)bset
, pos
, n
);
1803 /* Eliminate the specified n dimensions starting at first from the
1804 * constraints, without removing the dimensions from the space.
1805 * If the set is rational, the dimensions are eliminated using Fourier-Motzkin.
1806 * Otherwise, they are projected out and the original space is restored.
1808 __isl_give isl_basic_map
*isl_basic_map_eliminate(
1809 __isl_take isl_basic_map
*bmap
,
1810 enum isl_dim_type type
, unsigned first
, unsigned n
)
1819 if (first
+ n
> isl_basic_map_dim(bmap
, type
) || first
+ n
< first
)
1820 isl_die(bmap
->ctx
, isl_error_invalid
,
1821 "index out of bounds", goto error
);
1823 if (ISL_F_ISSET(bmap
, ISL_BASIC_MAP_RATIONAL
)) {
1824 first
+= isl_basic_map_offset(bmap
, type
) - 1;
1825 bmap
= isl_basic_map_eliminate_vars(bmap
, first
, n
);
1826 return isl_basic_map_finalize(bmap
);
1829 space
= isl_basic_map_get_space(bmap
);
1830 bmap
= isl_basic_map_project_out(bmap
, type
, first
, n
);
1831 bmap
= isl_basic_map_insert_dims(bmap
, type
, first
, n
);
1832 bmap
= isl_basic_map_reset_space(bmap
, space
);
1835 isl_basic_map_free(bmap
);
1839 __isl_give isl_basic_set
*isl_basic_set_eliminate(
1840 __isl_take isl_basic_set
*bset
,
1841 enum isl_dim_type type
, unsigned first
, unsigned n
)
1843 return isl_basic_map_eliminate(bset
, type
, first
, n
);
1846 /* Don't assume equalities are in order, because align_divs
1847 * may have changed the order of the divs.
1849 static void compute_elimination_index(struct isl_basic_map
*bmap
, int *elim
)
1854 total
= isl_space_dim(bmap
->dim
, isl_dim_all
);
1855 for (d
= 0; d
< total
; ++d
)
1857 for (i
= 0; i
< bmap
->n_eq
; ++i
) {
1858 for (d
= total
- 1; d
>= 0; --d
) {
1859 if (isl_int_is_zero(bmap
->eq
[i
][1+d
]))
1867 static void set_compute_elimination_index(struct isl_basic_set
*bset
, int *elim
)
1869 compute_elimination_index((struct isl_basic_map
*)bset
, elim
);
1872 static int reduced_using_equalities(isl_int
*dst
, isl_int
*src
,
1873 struct isl_basic_map
*bmap
, int *elim
)
1879 total
= isl_space_dim(bmap
->dim
, isl_dim_all
);
1880 for (d
= total
- 1; d
>= 0; --d
) {
1881 if (isl_int_is_zero(src
[1+d
]))
1886 isl_seq_cpy(dst
, src
, 1 + total
);
1889 isl_seq_elim(dst
, bmap
->eq
[elim
[d
]], 1 + d
, 1 + total
, NULL
);
1894 static int set_reduced_using_equalities(isl_int
*dst
, isl_int
*src
,
1895 struct isl_basic_set
*bset
, int *elim
)
1897 return reduced_using_equalities(dst
, src
,
1898 (struct isl_basic_map
*)bset
, elim
);
1901 static struct isl_basic_set
*isl_basic_set_reduce_using_equalities(
1902 struct isl_basic_set
*bset
, struct isl_basic_set
*context
)
1907 if (!bset
|| !context
)
1910 if (context
->n_eq
== 0) {
1911 isl_basic_set_free(context
);
1915 bset
= isl_basic_set_cow(bset
);
1919 elim
= isl_alloc_array(bset
->ctx
, int, isl_basic_set_n_dim(bset
));
1922 set_compute_elimination_index(context
, elim
);
1923 for (i
= 0; i
< bset
->n_eq
; ++i
)
1924 set_reduced_using_equalities(bset
->eq
[i
], bset
->eq
[i
],
1926 for (i
= 0; i
< bset
->n_ineq
; ++i
)
1927 set_reduced_using_equalities(bset
->ineq
[i
], bset
->ineq
[i
],
1929 isl_basic_set_free(context
);
1931 bset
= isl_basic_set_simplify(bset
);
1932 bset
= isl_basic_set_finalize(bset
);
1935 isl_basic_set_free(bset
);
1936 isl_basic_set_free(context
);
1940 /* For each inequality in "ineq" that is a shifted (more relaxed)
1941 * copy of an inequality in "context", mark the corresponding entry
1943 * If an inequality only has a non-negative constant term, then
1946 static isl_stat
mark_shifted_constraints(__isl_keep isl_mat
*ineq
,
1947 __isl_keep isl_basic_set
*context
, int *row
)
1949 struct isl_constraint_index ci
;
1954 if (!ineq
|| !context
)
1955 return isl_stat_error
;
1956 if (context
->n_ineq
== 0)
1958 if (setup_constraint_index(&ci
, context
) < 0)
1959 return isl_stat_error
;
1961 n_ineq
= isl_mat_rows(ineq
);
1962 total
= isl_mat_cols(ineq
) - 1;
1963 for (k
= 0; k
< n_ineq
; ++k
) {
1967 l
= isl_seq_first_non_zero(ineq
->row
[k
] + 1, total
);
1968 if (l
< 0 && isl_int_is_nonneg(ineq
->row
[k
][0])) {
1972 redundant
= constraint_index_is_redundant(&ci
, ineq
->row
[k
]);
1979 constraint_index_free(&ci
);
1982 constraint_index_free(&ci
);
1983 return isl_stat_error
;
1986 static struct isl_basic_set
*remove_shifted_constraints(
1987 struct isl_basic_set
*bset
, struct isl_basic_set
*context
)
1989 struct isl_constraint_index ci
;
1992 if (!bset
|| !context
)
1995 if (context
->n_ineq
== 0)
1997 if (setup_constraint_index(&ci
, context
) < 0)
2000 for (k
= 0; k
< bset
->n_ineq
; ++k
) {
2003 redundant
= constraint_index_is_redundant(&ci
, bset
->ineq
[k
]);
2008 bset
= isl_basic_set_cow(bset
);
2011 isl_basic_set_drop_inequality(bset
, k
);
2014 constraint_index_free(&ci
);
2017 constraint_index_free(&ci
);
2021 /* Remove constraints from "bmap" that are identical to constraints
2022 * in "context" or that are more relaxed (greater constant term).
2024 * We perform the test for shifted copies on the pure constraints
2025 * in remove_shifted_constraints.
2027 static __isl_give isl_basic_map
*isl_basic_map_remove_shifted_constraints(
2028 __isl_take isl_basic_map
*bmap
, __isl_take isl_basic_map
*context
)
2030 isl_basic_set
*bset
, *bset_context
;
2032 if (!bmap
|| !context
)
2035 if (bmap
->n_ineq
== 0 || context
->n_ineq
== 0) {
2036 isl_basic_map_free(context
);
2040 context
= isl_basic_map_align_divs(context
, bmap
);
2041 bmap
= isl_basic_map_align_divs(bmap
, context
);
2043 bset
= isl_basic_map_underlying_set(isl_basic_map_copy(bmap
));
2044 bset_context
= isl_basic_map_underlying_set(context
);
2045 bset
= remove_shifted_constraints(bset
, bset_context
);
2046 isl_basic_set_free(bset_context
);
2048 bmap
= isl_basic_map_overlying_set(bset
, bmap
);
2052 isl_basic_map_free(bmap
);
2053 isl_basic_map_free(context
);
2057 /* Does the (linear part of a) constraint "c" involve any of the "len"
2058 * "relevant" dimensions?
2060 static int is_related(isl_int
*c
, int len
, int *relevant
)
2064 for (i
= 0; i
< len
; ++i
) {
2067 if (!isl_int_is_zero(c
[i
]))
2074 /* Drop constraints from "bset" that do not involve any of
2075 * the dimensions marked "relevant".
2077 static __isl_give isl_basic_set
*drop_unrelated_constraints(
2078 __isl_take isl_basic_set
*bset
, int *relevant
)
2082 dim
= isl_basic_set_dim(bset
, isl_dim_set
);
2083 for (i
= 0; i
< dim
; ++i
)
2089 for (i
= bset
->n_eq
- 1; i
>= 0; --i
)
2090 if (!is_related(bset
->eq
[i
] + 1, dim
, relevant
))
2091 isl_basic_set_drop_equality(bset
, i
);
2093 for (i
= bset
->n_ineq
- 1; i
>= 0; --i
)
2094 if (!is_related(bset
->ineq
[i
] + 1, dim
, relevant
))
2095 isl_basic_set_drop_inequality(bset
, i
);
2100 /* Update the groups in "group" based on the (linear part of a) constraint "c".
2102 * In particular, for any variable involved in the constraint,
2103 * find the actual group id from before and replace the group
2104 * of the corresponding variable by the minimal group of all
2105 * the variables involved in the constraint considered so far
2106 * (if this minimum is smaller) or replace the minimum by this group
2107 * (if the minimum is larger).
2109 * At the end, all the variables in "c" will (indirectly) point
2110 * to the minimal of the groups that they referred to originally.
2112 static void update_groups(int dim
, int *group
, isl_int
*c
)
2117 for (j
= 0; j
< dim
; ++j
) {
2118 if (isl_int_is_zero(c
[j
]))
2120 while (group
[j
] >= 0 && group
[group
[j
]] != group
[j
])
2121 group
[j
] = group
[group
[j
]];
2122 if (group
[j
] == min
)
2124 if (group
[j
] < min
) {
2125 if (min
>= 0 && min
< dim
)
2126 group
[min
] = group
[j
];
2129 group
[group
[j
]] = min
;
2133 /* Allocate an array of groups of variables, one for each variable
2134 * in "context", initialized to zero.
2136 static int *alloc_groups(__isl_keep isl_basic_set
*context
)
2141 dim
= isl_basic_set_dim(context
, isl_dim_set
);
2142 ctx
= isl_basic_set_get_ctx(context
);
2143 return isl_calloc_array(ctx
, int, dim
);
2146 /* Drop constraints from "context" that only involve variables that are
2147 * not related to any of the variables marked with a "-1" in "group".
2149 * We construct groups of variables that collect variables that
2150 * (indirectly) appear in some common constraint of "context".
2151 * Each group is identified by the first variable in the group,
2152 * except for the special group of variables that was already identified
2153 * in the input as -1 (or are related to those variables).
2154 * If group[i] is equal to i (or -1), then the group of i is i (or -1),
2155 * otherwise the group of i is the group of group[i].
2157 * We first initialize groups for the remaining variables.
2158 * Then we iterate over the constraints of "context" and update the
2159 * group of the variables in the constraint by the smallest group.
2160 * Finally, we resolve indirect references to groups by running over
2163 * After computing the groups, we drop constraints that do not involve
2164 * any variables in the -1 group.
2166 static __isl_give isl_basic_set
*group_and_drop_irrelevant_constraints(
2167 __isl_take isl_basic_set
*context
, __isl_take
int *group
)
2173 dim
= isl_basic_set_dim(context
, isl_dim_set
);
2176 for (i
= 0; i
< dim
; ++i
)
2178 last
= group
[i
] = i
;
2184 for (i
= 0; i
< context
->n_eq
; ++i
)
2185 update_groups(dim
, group
, context
->eq
[i
] + 1);
2186 for (i
= 0; i
< context
->n_ineq
; ++i
)
2187 update_groups(dim
, group
, context
->ineq
[i
] + 1);
2189 for (i
= 0; i
< dim
; ++i
)
2191 group
[i
] = group
[group
[i
]];
2193 for (i
= 0; i
< dim
; ++i
)
2194 group
[i
] = group
[i
] == -1;
2196 context
= drop_unrelated_constraints(context
, group
);
2202 /* Drop constraints from "context" that are irrelevant for computing
2203 * the gist of "bset".
2205 * In particular, drop constraints in variables that are not related
2206 * to any of the variables involved in the constraints of "bset"
2207 * in the sense that there is no sequence of constraints that connects them.
2209 * We first mark all variables that appear in "bset" as belonging
2210 * to a "-1" group and then continue with group_and_drop_irrelevant_constraints.
2212 static __isl_give isl_basic_set
*drop_irrelevant_constraints(
2213 __isl_take isl_basic_set
*context
, __isl_keep isl_basic_set
*bset
)
2219 if (!context
|| !bset
)
2220 return isl_basic_set_free(context
);
2222 group
= alloc_groups(context
);
2225 return isl_basic_set_free(context
);
2227 dim
= isl_basic_set_dim(bset
, isl_dim_set
);
2228 for (i
= 0; i
< dim
; ++i
) {
2229 for (j
= 0; j
< bset
->n_eq
; ++j
)
2230 if (!isl_int_is_zero(bset
->eq
[j
][1 + i
]))
2232 if (j
< bset
->n_eq
) {
2236 for (j
= 0; j
< bset
->n_ineq
; ++j
)
2237 if (!isl_int_is_zero(bset
->ineq
[j
][1 + i
]))
2239 if (j
< bset
->n_ineq
)
2243 return group_and_drop_irrelevant_constraints(context
, group
);
2246 /* Drop constraints from "context" that are irrelevant for computing
2247 * the gist of the inequalities "ineq".
2248 * Inequalities in "ineq" for which the corresponding element of row
2249 * is set to -1 have already been marked for removal and should be ignored.
2251 * In particular, drop constraints in variables that are not related
2252 * to any of the variables involved in "ineq"
2253 * in the sense that there is no sequence of constraints that connects them.
2255 * We first mark all variables that appear in "bset" as belonging
2256 * to a "-1" group and then continue with group_and_drop_irrelevant_constraints.
2258 static __isl_give isl_basic_set
*drop_irrelevant_constraints_marked(
2259 __isl_take isl_basic_set
*context
, __isl_keep isl_mat
*ineq
, int *row
)
2265 if (!context
|| !ineq
)
2266 return isl_basic_set_free(context
);
2268 group
= alloc_groups(context
);
2271 return isl_basic_set_free(context
);
2273 dim
= isl_basic_set_dim(context
, isl_dim_set
);
2274 n
= isl_mat_rows(ineq
);
2275 for (i
= 0; i
< dim
; ++i
) {
2276 for (j
= 0; j
< n
; ++j
) {
2279 if (!isl_int_is_zero(ineq
->row
[j
][1 + i
]))
2286 return group_and_drop_irrelevant_constraints(context
, group
);
2289 /* Do all "n" entries of "row" contain a negative value?
2291 static int all_neg(int *row
, int n
)
2295 for (i
= 0; i
< n
; ++i
)
2302 /* Update the inequalities in "bset" based on the information in "row"
2305 * In particular, the array "row" contains either -1, meaning that
2306 * the corresponding inequality of "bset" is redundant, or the index
2307 * of an inequality in "tab".
2309 * If the row entry is -1, then drop the inequality.
2310 * Otherwise, if the constraint is marked redundant in the tableau,
2311 * then drop the inequality. Similarly, if it is marked as an equality
2312 * in the tableau, then turn the inequality into an equality and
2313 * perform Gaussian elimination.
2315 static __isl_give isl_basic_set
*update_ineq(__isl_take isl_basic_set
*bset
,
2316 __isl_keep
int *row
, struct isl_tab
*tab
)
2321 int found_equality
= 0;
2325 if (tab
&& tab
->empty
)
2326 return isl_basic_set_set_to_empty(bset
);
2328 n_ineq
= bset
->n_ineq
;
2329 for (i
= n_ineq
- 1; i
>= 0; --i
) {
2331 if (isl_basic_set_drop_inequality(bset
, i
) < 0)
2332 return isl_basic_set_free(bset
);
2338 if (isl_tab_is_equality(tab
, n_eq
+ row
[i
])) {
2339 isl_basic_map_inequality_to_equality(bset
, i
);
2341 } else if (isl_tab_is_redundant(tab
, n_eq
+ row
[i
])) {
2342 if (isl_basic_set_drop_inequality(bset
, i
) < 0)
2343 return isl_basic_set_free(bset
);
2348 bset
= isl_basic_set_gauss(bset
, NULL
);
2349 bset
= isl_basic_set_finalize(bset
);
2353 /* Update the inequalities in "bset" based on the information in "row"
2354 * and "tab" and free all arguments (other than "bset").
2356 static __isl_give isl_basic_set
*update_ineq_free(
2357 __isl_take isl_basic_set
*bset
, __isl_take isl_mat
*ineq
,
2358 __isl_take isl_basic_set
*context
, __isl_take
int *row
,
2359 struct isl_tab
*tab
)
2362 isl_basic_set_free(context
);
2364 bset
= update_ineq(bset
, row
, tab
);
2371 /* Remove all information from bset that is redundant in the context
2373 * "ineq" contains the (possibly transformed) inequalities of "bset",
2374 * in the same order.
2375 * The (explicit) equalities of "bset" are assumed to have been taken
2376 * into account by the transformation such that only the inequalities
2378 * "context" is assumed not to be empty.
2380 * "row" keeps track of the constraint index of a "bset" inequality in "tab".
2381 * A value of -1 means that the inequality is obviously redundant and may
2382 * not even appear in "tab".
2384 * We first mark the inequalities of "bset"
2385 * that are obviously redundant with respect to some inequality in "context".
2386 * Then we remove those constraints from "context" that have become
2387 * irrelevant for computing the gist of "bset".
2388 * Note that this removal of constraints cannot be replaced by
2389 * a factorization because factors in "bset" may still be connected
2390 * to each other through constraints in "context".
2392 * If there are any inequalities left, we construct a tableau for
2393 * the context and then add the inequalities of "bset".
2394 * Before adding these inequalities, we freeze all constraints such that
2395 * they won't be considered redundant in terms of the constraints of "bset".
2396 * Then we detect all redundant constraints (among the
2397 * constraints that weren't frozen), first by checking for redundancy in the
2398 * the tableau and then by checking if replacing a constraint by its negation
2399 * would lead to an empty set. This last step is fairly expensive
2400 * and could be optimized by more reuse of the tableau.
2401 * Finally, we update bset according to the results.
2403 static __isl_give isl_basic_set
*uset_gist_full(__isl_take isl_basic_set
*bset
,
2404 __isl_take isl_mat
*ineq
, __isl_take isl_basic_set
*context
)
2409 isl_basic_set
*combined
= NULL
;
2410 struct isl_tab
*tab
= NULL
;
2411 unsigned n_eq
, context_ineq
;
2414 if (!bset
|| !ineq
|| !context
)
2417 if (bset
->n_ineq
== 0 || isl_basic_set_is_universe(context
)) {
2418 isl_basic_set_free(context
);
2423 ctx
= isl_basic_set_get_ctx(context
);
2424 row
= isl_calloc_array(ctx
, int, bset
->n_ineq
);
2428 if (mark_shifted_constraints(ineq
, context
, row
) < 0)
2430 if (all_neg(row
, bset
->n_ineq
))
2431 return update_ineq_free(bset
, ineq
, context
, row
, NULL
);
2433 context
= drop_irrelevant_constraints_marked(context
, ineq
, row
);
2436 if (isl_basic_set_is_universe(context
))
2437 return update_ineq_free(bset
, ineq
, context
, row
, NULL
);
2439 n_eq
= context
->n_eq
;
2440 context_ineq
= context
->n_ineq
;
2441 combined
= isl_basic_set_cow(isl_basic_set_copy(context
));
2442 combined
= isl_basic_set_extend_constraints(combined
, 0, bset
->n_ineq
);
2443 tab
= isl_tab_from_basic_set(combined
, 0);
2444 for (i
= 0; i
< context_ineq
; ++i
)
2445 if (isl_tab_freeze_constraint(tab
, n_eq
+ i
) < 0)
2447 if (isl_tab_extend_cons(tab
, bset
->n_ineq
) < 0)
2450 for (i
= 0; i
< bset
->n_ineq
; ++i
) {
2453 combined
= isl_basic_set_add_ineq(combined
, ineq
->row
[i
]);
2454 if (isl_tab_add_ineq(tab
, ineq
->row
[i
]) < 0)
2458 if (isl_tab_detect_implicit_equalities(tab
) < 0)
2460 if (isl_tab_detect_redundant(tab
) < 0)
2462 total
= isl_basic_set_total_dim(bset
);
2463 for (i
= bset
->n_ineq
- 1; i
>= 0; --i
) {
2464 isl_basic_set
*test
;
2470 if (tab
->con
[n_eq
+ r
].is_redundant
)
2472 test
= isl_basic_set_dup(combined
);
2473 if (isl_inequality_negate(test
, r
) < 0)
2474 test
= isl_basic_set_free(test
);
2475 test
= isl_basic_set_update_from_tab(test
, tab
);
2476 is_empty
= isl_basic_set_is_empty(test
);
2477 isl_basic_set_free(test
);
2481 tab
->con
[n_eq
+ r
].is_redundant
= 1;
2483 bset
= update_ineq_free(bset
, ineq
, context
, row
, tab
);
2485 ISL_F_SET(bset
, ISL_BASIC_SET_NO_IMPLICIT
);
2486 ISL_F_SET(bset
, ISL_BASIC_SET_NO_REDUNDANT
);
2489 isl_basic_set_free(combined
);
2495 isl_basic_set_free(combined
);
2496 isl_basic_set_free(context
);
2497 isl_basic_set_free(bset
);
2501 /* Extract the inequalities of "bset" as an isl_mat.
2503 static __isl_give isl_mat
*extract_ineq(__isl_keep isl_basic_set
*bset
)
2512 ctx
= isl_basic_set_get_ctx(bset
);
2513 total
= isl_basic_set_total_dim(bset
);
2514 ineq
= isl_mat_sub_alloc6(ctx
, bset
->ineq
, 0, bset
->n_ineq
,
2520 /* Remove all information from "bset" that is redundant in the context
2521 * of "context", for the case where both "bset" and "context" are
2524 static __isl_give isl_basic_set
*uset_gist_uncompressed(
2525 __isl_take isl_basic_set
*bset
, __isl_take isl_basic_set
*context
)
2529 ineq
= extract_ineq(bset
);
2530 return uset_gist_full(bset
, ineq
, context
);
2533 /* Remove all information from "bset" that is redundant in the context
2534 * of "context", for the case where the combined equalities of
2535 * "bset" and "context" allow for a compression that can be obtained
2536 * by preapplication of "T".
2538 * "bset" itself is not transformed by "T". Instead, the inequalities
2539 * are extracted from "bset" and those are transformed by "T".
2540 * uset_gist_full then determines which of the transformed inequalities
2541 * are redundant with respect to the transformed "context" and removes
2542 * the corresponding inequalities from "bset".
2544 * After preapplying "T" to the inequalities, any common factor is
2545 * removed from the coefficients. If this results in a tightening
2546 * of the constant term, then the same tightening is applied to
2547 * the corresponding untransformed inequality in "bset".
2548 * That is, if after plugging in T, a constraint f(x) >= 0 is of the form
2552 * with 0 <= r < g, then it is equivalent to
2556 * This means that f(x) >= 0 is equivalent to f(x) - r >= 0 in the affine
2557 * subspace compressed by T since the latter would be transformed to
2561 static __isl_give isl_basic_set
*uset_gist_compressed(
2562 __isl_take isl_basic_set
*bset
, __isl_take isl_basic_set
*context
,
2563 __isl_take isl_mat
*T
)
2567 int i
, n_row
, n_col
;
2570 ineq
= extract_ineq(bset
);
2571 ineq
= isl_mat_product(ineq
, isl_mat_copy(T
));
2572 context
= isl_basic_set_preimage(context
, T
);
2574 if (!ineq
|| !context
)
2576 if (isl_basic_set_plain_is_empty(context
)) {
2578 isl_basic_set_free(context
);
2579 return isl_basic_set_set_to_empty(bset
);
2582 ctx
= isl_mat_get_ctx(ineq
);
2583 n_row
= isl_mat_rows(ineq
);
2584 n_col
= isl_mat_cols(ineq
);
2586 for (i
= 0; i
< n_row
; ++i
) {
2587 isl_seq_gcd(ineq
->row
[i
] + 1, n_col
- 1, &ctx
->normalize_gcd
);
2588 if (isl_int_is_zero(ctx
->normalize_gcd
))
2590 if (isl_int_is_one(ctx
->normalize_gcd
))
2592 isl_seq_scale_down(ineq
->row
[i
] + 1, ineq
->row
[i
] + 1,
2593 ctx
->normalize_gcd
, n_col
- 1);
2594 isl_int_fdiv_r(rem
, ineq
->row
[i
][0], ctx
->normalize_gcd
);
2595 isl_int_fdiv_q(ineq
->row
[i
][0],
2596 ineq
->row
[i
][0], ctx
->normalize_gcd
);
2597 if (isl_int_is_zero(rem
))
2599 bset
= isl_basic_set_cow(bset
);
2602 isl_int_sub(bset
->ineq
[i
][0], bset
->ineq
[i
][0], rem
);
2606 return uset_gist_full(bset
, ineq
, context
);
2609 isl_basic_set_free(context
);
2610 isl_basic_set_free(bset
);
2614 /* Project "bset" onto the variables that are involved in "template".
2616 static __isl_give isl_basic_set
*project_onto_involved(
2617 __isl_take isl_basic_set
*bset
, __isl_keep isl_basic_set
*template)
2621 if (!bset
|| !template)
2622 return isl_basic_set_free(bset
);
2624 n
= isl_basic_set_dim(template, isl_dim_set
);
2626 for (i
= 0; i
< n
; ++i
) {
2629 involved
= isl_basic_set_involves_dims(template,
2632 return isl_basic_set_free(bset
);
2635 bset
= isl_basic_set_eliminate_vars(bset
, i
, 1);
2641 /* Remove all information from bset that is redundant in the context
2642 * of context. In particular, equalities that are linear combinations
2643 * of those in context are removed. Then the inequalities that are
2644 * redundant in the context of the equalities and inequalities of
2645 * context are removed.
2647 * First of all, we drop those constraints from "context"
2648 * that are irrelevant for computing the gist of "bset".
2649 * Alternatively, we could factorize the intersection of "context" and "bset".
2651 * We first compute the intersection of the integer affine hulls
2652 * of "bset" and "context",
2653 * compute the gist inside this intersection and then reduce
2654 * the constraints with respect to the equalities of the context
2655 * that only involve variables already involved in the input.
2657 * If two constraints are mutually redundant, then uset_gist_full
2658 * will remove the second of those constraints. We therefore first
2659 * sort the constraints so that constraints not involving existentially
2660 * quantified variables are given precedence over those that do.
2661 * We have to perform this sorting before the variable compression,
2662 * because that may effect the order of the variables.
2664 static __isl_give isl_basic_set
*uset_gist(__isl_take isl_basic_set
*bset
,
2665 __isl_take isl_basic_set
*context
)
2670 isl_basic_set
*aff_context
;
2673 if (!bset
|| !context
)
2676 context
= drop_irrelevant_constraints(context
, bset
);
2678 bset
= isl_basic_set_detect_equalities(bset
);
2679 aff
= isl_basic_set_copy(bset
);
2680 aff
= isl_basic_set_plain_affine_hull(aff
);
2681 context
= isl_basic_set_detect_equalities(context
);
2682 aff_context
= isl_basic_set_copy(context
);
2683 aff_context
= isl_basic_set_plain_affine_hull(aff_context
);
2684 aff
= isl_basic_set_intersect(aff
, aff_context
);
2687 if (isl_basic_set_plain_is_empty(aff
)) {
2688 isl_basic_set_free(bset
);
2689 isl_basic_set_free(context
);
2692 bset
= isl_basic_set_sort_constraints(bset
);
2693 if (aff
->n_eq
== 0) {
2694 isl_basic_set_free(aff
);
2695 return uset_gist_uncompressed(bset
, context
);
2697 total
= isl_basic_set_total_dim(bset
);
2698 eq
= isl_mat_sub_alloc6(bset
->ctx
, aff
->eq
, 0, aff
->n_eq
, 0, 1 + total
);
2699 eq
= isl_mat_cow(eq
);
2700 T
= isl_mat_variable_compression(eq
, NULL
);
2701 isl_basic_set_free(aff
);
2702 if (T
&& T
->n_col
== 0) {
2704 isl_basic_set_free(context
);
2705 return isl_basic_set_set_to_empty(bset
);
2708 aff_context
= isl_basic_set_affine_hull(isl_basic_set_copy(context
));
2709 aff_context
= project_onto_involved(aff_context
, bset
);
2711 bset
= uset_gist_compressed(bset
, context
, T
);
2712 bset
= isl_basic_set_reduce_using_equalities(bset
, aff_context
);
2715 ISL_F_SET(bset
, ISL_BASIC_SET_NO_IMPLICIT
);
2716 ISL_F_SET(bset
, ISL_BASIC_SET_NO_REDUNDANT
);
2721 isl_basic_set_free(bset
);
2722 isl_basic_set_free(context
);
2726 /* Return a basic map that has the same intersection with "context" as "bmap"
2727 * and that is as "simple" as possible.
2729 * The core computation is performed on the pure constraints.
2730 * When we add back the meaning of the integer divisions, we need
2731 * to (re)introduce the div constraints. If we happen to have
2732 * discovered that some of these integer divisions are equal to
2733 * some affine combination of other variables, then these div
2734 * constraints may end up getting simplified in terms of the equalities,
2735 * resulting in extra inequalities on the other variables that
2736 * may have been removed already or that may not even have been
2737 * part of the input. We try and remove those constraints of
2738 * this form that are most obviously redundant with respect to
2739 * the context. We also remove those div constraints that are
2740 * redundant with respect to the other constraints in the result.
2742 struct isl_basic_map
*isl_basic_map_gist(struct isl_basic_map
*bmap
,
2743 struct isl_basic_map
*context
)
2745 isl_basic_set
*bset
, *eq
;
2746 isl_basic_map
*eq_bmap
;
2747 unsigned total
, n_div
, extra
, n_eq
, n_ineq
;
2749 if (!bmap
|| !context
)
2752 if (isl_basic_map_is_universe(bmap
)) {
2753 isl_basic_map_free(context
);
2756 if (isl_basic_map_plain_is_empty(context
)) {
2757 isl_space
*space
= isl_basic_map_get_space(bmap
);
2758 isl_basic_map_free(bmap
);
2759 isl_basic_map_free(context
);
2760 return isl_basic_map_universe(space
);
2762 if (isl_basic_map_plain_is_empty(bmap
)) {
2763 isl_basic_map_free(context
);
2767 bmap
= isl_basic_map_remove_redundancies(bmap
);
2768 context
= isl_basic_map_remove_redundancies(context
);
2772 context
= isl_basic_map_align_divs(context
, bmap
);
2773 n_div
= isl_basic_map_dim(context
, isl_dim_div
);
2774 total
= isl_basic_map_dim(bmap
, isl_dim_all
);
2775 extra
= n_div
- isl_basic_map_dim(bmap
, isl_dim_div
);
2777 bset
= isl_basic_map_underlying_set(isl_basic_map_copy(bmap
));
2778 bset
= isl_basic_set_add_dims(bset
, isl_dim_set
, extra
);
2779 bset
= uset_gist(bset
,
2780 isl_basic_map_underlying_set(isl_basic_map_copy(context
)));
2781 bset
= isl_basic_set_project_out(bset
, isl_dim_set
, total
, extra
);
2783 if (!bset
|| bset
->n_eq
== 0 || n_div
== 0 ||
2784 isl_basic_set_plain_is_empty(bset
)) {
2785 isl_basic_map_free(context
);
2786 return isl_basic_map_overlying_set(bset
, bmap
);
2790 n_ineq
= bset
->n_ineq
;
2791 eq
= isl_basic_set_copy(bset
);
2792 eq
= isl_basic_set_cow(eq
);
2793 if (isl_basic_set_free_inequality(eq
, n_ineq
) < 0)
2794 eq
= isl_basic_set_free(eq
);
2795 if (isl_basic_set_free_equality(bset
, n_eq
) < 0)
2796 bset
= isl_basic_set_free(bset
);
2798 eq_bmap
= isl_basic_map_overlying_set(eq
, isl_basic_map_copy(bmap
));
2799 eq_bmap
= isl_basic_map_remove_shifted_constraints(eq_bmap
, context
);
2800 bmap
= isl_basic_map_overlying_set(bset
, bmap
);
2801 bmap
= isl_basic_map_intersect(bmap
, eq_bmap
);
2802 bmap
= isl_basic_map_remove_redundancies(bmap
);
2806 isl_basic_map_free(bmap
);
2807 isl_basic_map_free(context
);
2812 * Assumes context has no implicit divs.
2814 __isl_give isl_map
*isl_map_gist_basic_map(__isl_take isl_map
*map
,
2815 __isl_take isl_basic_map
*context
)
2819 if (!map
|| !context
)
2822 if (isl_basic_map_plain_is_empty(context
)) {
2823 isl_space
*space
= isl_map_get_space(map
);
2825 isl_basic_map_free(context
);
2826 return isl_map_universe(space
);
2829 context
= isl_basic_map_remove_redundancies(context
);
2830 map
= isl_map_cow(map
);
2831 if (!map
|| !context
)
2833 isl_assert(map
->ctx
, isl_space_is_equal(map
->dim
, context
->dim
), goto error
);
2834 map
= isl_map_compute_divs(map
);
2837 for (i
= map
->n
- 1; i
>= 0; --i
) {
2838 map
->p
[i
] = isl_basic_map_gist(map
->p
[i
],
2839 isl_basic_map_copy(context
));
2842 if (isl_basic_map_plain_is_empty(map
->p
[i
])) {
2843 isl_basic_map_free(map
->p
[i
]);
2844 if (i
!= map
->n
- 1)
2845 map
->p
[i
] = map
->p
[map
->n
- 1];
2849 isl_basic_map_free(context
);
2850 ISL_F_CLR(map
, ISL_MAP_NORMALIZED
);
2854 isl_basic_map_free(context
);
2858 /* Return a map that has the same intersection with "context" as "map"
2859 * and that is as "simple" as possible.
2861 * If "map" is already the universe, then we cannot make it any simpler.
2862 * Similarly, if "context" is the universe, then we cannot exploit it
2864 * If "map" and "context" are identical to each other, then we can
2865 * return the corresponding universe.
2867 * If none of these cases apply, we have to work a bit harder.
2868 * During this computation, we make use of a single disjunct context,
2869 * so if the original context consists of more than one disjunct
2870 * then we need to approximate the context by a single disjunct set.
2871 * Simply taking the simple hull may drop constraints that are
2872 * only implicitly available in each disjunct. We therefore also
2873 * look for constraints among those defining "map" that are valid
2874 * for the context. These can then be used to simplify away
2875 * the corresponding constraints in "map".
2877 static __isl_give isl_map
*map_gist(__isl_take isl_map
*map
,
2878 __isl_take isl_map
*context
)
2882 isl_basic_map
*hull
;
2884 is_universe
= isl_map_plain_is_universe(map
);
2885 if (is_universe
>= 0 && !is_universe
)
2886 is_universe
= isl_map_plain_is_universe(context
);
2887 if (is_universe
< 0)
2890 isl_map_free(context
);
2894 equal
= isl_map_plain_is_equal(map
, context
);
2898 isl_map
*res
= isl_map_universe(isl_map_get_space(map
));
2900 isl_map_free(context
);
2904 context
= isl_map_compute_divs(context
);
2907 if (isl_map_n_basic_map(context
) == 1) {
2908 hull
= isl_map_simple_hull(context
);
2913 ctx
= isl_map_get_ctx(map
);
2914 list
= isl_map_list_alloc(ctx
, 2);
2915 list
= isl_map_list_add(list
, isl_map_copy(context
));
2916 list
= isl_map_list_add(list
, isl_map_copy(map
));
2917 hull
= isl_map_unshifted_simple_hull_from_map_list(context
,
2920 return isl_map_gist_basic_map(map
, hull
);
2923 isl_map_free(context
);
2927 __isl_give isl_map
*isl_map_gist(__isl_take isl_map
*map
,
2928 __isl_take isl_map
*context
)
2930 return isl_map_align_params_map_map_and(map
, context
, &map_gist
);
2933 struct isl_basic_set
*isl_basic_set_gist(struct isl_basic_set
*bset
,
2934 struct isl_basic_set
*context
)
2936 return (struct isl_basic_set
*)isl_basic_map_gist(
2937 (struct isl_basic_map
*)bset
, (struct isl_basic_map
*)context
);
2940 __isl_give isl_set
*isl_set_gist_basic_set(__isl_take isl_set
*set
,
2941 __isl_take isl_basic_set
*context
)
2943 return (struct isl_set
*)isl_map_gist_basic_map((struct isl_map
*)set
,
2944 (struct isl_basic_map
*)context
);
2947 __isl_give isl_set
*isl_set_gist_params_basic_set(__isl_take isl_set
*set
,
2948 __isl_take isl_basic_set
*context
)
2950 isl_space
*space
= isl_set_get_space(set
);
2951 isl_basic_set
*dom_context
= isl_basic_set_universe(space
);
2952 dom_context
= isl_basic_set_intersect_params(dom_context
, context
);
2953 return isl_set_gist_basic_set(set
, dom_context
);
2956 __isl_give isl_set
*isl_set_gist(__isl_take isl_set
*set
,
2957 __isl_take isl_set
*context
)
2959 return (struct isl_set
*)isl_map_gist((struct isl_map
*)set
,
2960 (struct isl_map
*)context
);
2963 /* Compute the gist of "bmap" with respect to the constraints "context"
2966 __isl_give isl_basic_map
*isl_basic_map_gist_domain(
2967 __isl_take isl_basic_map
*bmap
, __isl_take isl_basic_set
*context
)
2969 isl_space
*space
= isl_basic_map_get_space(bmap
);
2970 isl_basic_map
*bmap_context
= isl_basic_map_universe(space
);
2972 bmap_context
= isl_basic_map_intersect_domain(bmap_context
, context
);
2973 return isl_basic_map_gist(bmap
, bmap_context
);
2976 __isl_give isl_map
*isl_map_gist_domain(__isl_take isl_map
*map
,
2977 __isl_take isl_set
*context
)
2979 isl_map
*map_context
= isl_map_universe(isl_map_get_space(map
));
2980 map_context
= isl_map_intersect_domain(map_context
, context
);
2981 return isl_map_gist(map
, map_context
);
2984 __isl_give isl_map
*isl_map_gist_range(__isl_take isl_map
*map
,
2985 __isl_take isl_set
*context
)
2987 isl_map
*map_context
= isl_map_universe(isl_map_get_space(map
));
2988 map_context
= isl_map_intersect_range(map_context
, context
);
2989 return isl_map_gist(map
, map_context
);
2992 __isl_give isl_map
*isl_map_gist_params(__isl_take isl_map
*map
,
2993 __isl_take isl_set
*context
)
2995 isl_map
*map_context
= isl_map_universe(isl_map_get_space(map
));
2996 map_context
= isl_map_intersect_params(map_context
, context
);
2997 return isl_map_gist(map
, map_context
);
3000 __isl_give isl_set
*isl_set_gist_params(__isl_take isl_set
*set
,
3001 __isl_take isl_set
*context
)
3003 return isl_map_gist_params(set
, context
);
3006 /* Quick check to see if two basic maps are disjoint.
3007 * In particular, we reduce the equalities and inequalities of
3008 * one basic map in the context of the equalities of the other
3009 * basic map and check if we get a contradiction.
3011 isl_bool
isl_basic_map_plain_is_disjoint(__isl_keep isl_basic_map
*bmap1
,
3012 __isl_keep isl_basic_map
*bmap2
)
3014 struct isl_vec
*v
= NULL
;
3019 if (!bmap1
|| !bmap2
)
3020 return isl_bool_error
;
3021 isl_assert(bmap1
->ctx
, isl_space_is_equal(bmap1
->dim
, bmap2
->dim
),
3022 return isl_bool_error
);
3023 if (bmap1
->n_div
|| bmap2
->n_div
)
3024 return isl_bool_false
;
3025 if (!bmap1
->n_eq
&& !bmap2
->n_eq
)
3026 return isl_bool_false
;
3028 total
= isl_space_dim(bmap1
->dim
, isl_dim_all
);
3030 return isl_bool_false
;
3031 v
= isl_vec_alloc(bmap1
->ctx
, 1 + total
);
3034 elim
= isl_alloc_array(bmap1
->ctx
, int, total
);
3037 compute_elimination_index(bmap1
, elim
);
3038 for (i
= 0; i
< bmap2
->n_eq
; ++i
) {
3040 reduced
= reduced_using_equalities(v
->block
.data
, bmap2
->eq
[i
],
3042 if (reduced
&& !isl_int_is_zero(v
->block
.data
[0]) &&
3043 isl_seq_first_non_zero(v
->block
.data
+ 1, total
) == -1)
3046 for (i
= 0; i
< bmap2
->n_ineq
; ++i
) {
3048 reduced
= reduced_using_equalities(v
->block
.data
,
3049 bmap2
->ineq
[i
], bmap1
, elim
);
3050 if (reduced
&& isl_int_is_neg(v
->block
.data
[0]) &&
3051 isl_seq_first_non_zero(v
->block
.data
+ 1, total
) == -1)
3054 compute_elimination_index(bmap2
, elim
);
3055 for (i
= 0; i
< bmap1
->n_ineq
; ++i
) {
3057 reduced
= reduced_using_equalities(v
->block
.data
,
3058 bmap1
->ineq
[i
], bmap2
, elim
);
3059 if (reduced
&& isl_int_is_neg(v
->block
.data
[0]) &&
3060 isl_seq_first_non_zero(v
->block
.data
+ 1, total
) == -1)
3065 return isl_bool_false
;
3069 return isl_bool_true
;
3073 return isl_bool_error
;
3076 int isl_basic_set_plain_is_disjoint(__isl_keep isl_basic_set
*bset1
,
3077 __isl_keep isl_basic_set
*bset2
)
3079 return isl_basic_map_plain_is_disjoint((struct isl_basic_map
*)bset1
,
3080 (struct isl_basic_map
*)bset2
);
3083 /* Are "map1" and "map2" obviously disjoint?
3085 * If one of them is empty or if they live in different spaces (ignoring
3086 * parameters), then they are clearly disjoint.
3088 * If they have different parameters, then we skip any further tests.
3090 * If they are obviously equal, but not obviously empty, then we will
3091 * not be able to detect if they are disjoint.
3093 * Otherwise we check if each basic map in "map1" is obviously disjoint
3094 * from each basic map in "map2".
3096 isl_bool
isl_map_plain_is_disjoint(__isl_keep isl_map
*map1
,
3097 __isl_keep isl_map
*map2
)
3105 return isl_bool_error
;
3107 disjoint
= isl_map_plain_is_empty(map1
);
3108 if (disjoint
< 0 || disjoint
)
3111 disjoint
= isl_map_plain_is_empty(map2
);
3112 if (disjoint
< 0 || disjoint
)
3115 match
= isl_space_tuple_is_equal(map1
->dim
, isl_dim_in
,
3116 map2
->dim
, isl_dim_in
);
3117 if (match
< 0 || !match
)
3118 return match
< 0 ? isl_bool_error
: isl_bool_true
;
3120 match
= isl_space_tuple_is_equal(map1
->dim
, isl_dim_out
,
3121 map2
->dim
, isl_dim_out
);
3122 if (match
< 0 || !match
)
3123 return match
< 0 ? isl_bool_error
: isl_bool_true
;
3125 match
= isl_space_match(map1
->dim
, isl_dim_param
,
3126 map2
->dim
, isl_dim_param
);
3127 if (match
< 0 || !match
)
3128 return match
< 0 ? isl_bool_error
: isl_bool_false
;
3130 intersect
= isl_map_plain_is_equal(map1
, map2
);
3131 if (intersect
< 0 || intersect
)
3132 return intersect
< 0 ? isl_bool_error
: isl_bool_false
;
3134 for (i
= 0; i
< map1
->n
; ++i
) {
3135 for (j
= 0; j
< map2
->n
; ++j
) {
3136 isl_bool d
= isl_basic_map_plain_is_disjoint(map1
->p
[i
],
3138 if (d
!= isl_bool_true
)
3142 return isl_bool_true
;
3145 /* Are "map1" and "map2" disjoint?
3147 * They are disjoint if they are "obviously disjoint" or if one of them
3148 * is empty. Otherwise, they are not disjoint if one of them is universal.
3149 * If none of these cases apply, we compute the intersection and see if
3150 * the result is empty.
3152 isl_bool
isl_map_is_disjoint(__isl_keep isl_map
*map1
, __isl_keep isl_map
*map2
)
3158 disjoint
= isl_map_plain_is_disjoint(map1
, map2
);
3159 if (disjoint
< 0 || disjoint
)
3162 disjoint
= isl_map_is_empty(map1
);
3163 if (disjoint
< 0 || disjoint
)
3166 disjoint
= isl_map_is_empty(map2
);
3167 if (disjoint
< 0 || disjoint
)
3170 intersect
= isl_map_plain_is_universe(map1
);
3171 if (intersect
< 0 || intersect
)
3172 return intersect
< 0 ? isl_bool_error
: isl_bool_false
;
3174 intersect
= isl_map_plain_is_universe(map2
);
3175 if (intersect
< 0 || intersect
)
3176 return intersect
< 0 ? isl_bool_error
: isl_bool_false
;
3178 test
= isl_map_intersect(isl_map_copy(map1
), isl_map_copy(map2
));
3179 disjoint
= isl_map_is_empty(test
);
3185 /* Are "bmap1" and "bmap2" disjoint?
3187 * They are disjoint if they are "obviously disjoint" or if one of them
3188 * is empty. Otherwise, they are not disjoint if one of them is universal.
3189 * If none of these cases apply, we compute the intersection and see if
3190 * the result is empty.
3192 isl_bool
isl_basic_map_is_disjoint(__isl_keep isl_basic_map
*bmap1
,
3193 __isl_keep isl_basic_map
*bmap2
)
3197 isl_basic_map
*test
;
3199 disjoint
= isl_basic_map_plain_is_disjoint(bmap1
, bmap2
);
3200 if (disjoint
< 0 || disjoint
)
3203 disjoint
= isl_basic_map_is_empty(bmap1
);
3204 if (disjoint
< 0 || disjoint
)
3207 disjoint
= isl_basic_map_is_empty(bmap2
);
3208 if (disjoint
< 0 || disjoint
)
3211 intersect
= isl_basic_map_is_universe(bmap1
);
3212 if (intersect
< 0 || intersect
)
3213 return intersect
< 0 ? isl_bool_error
: isl_bool_false
;
3215 intersect
= isl_basic_map_is_universe(bmap2
);
3216 if (intersect
< 0 || intersect
)
3217 return intersect
< 0 ? isl_bool_error
: isl_bool_false
;
3219 test
= isl_basic_map_intersect(isl_basic_map_copy(bmap1
),
3220 isl_basic_map_copy(bmap2
));
3221 disjoint
= isl_basic_map_is_empty(test
);
3222 isl_basic_map_free(test
);
3227 /* Are "bset1" and "bset2" disjoint?
3229 isl_bool
isl_basic_set_is_disjoint(__isl_keep isl_basic_set
*bset1
,
3230 __isl_keep isl_basic_set
*bset2
)
3232 return isl_basic_map_is_disjoint(bset1
, bset2
);
3235 isl_bool
isl_set_plain_is_disjoint(__isl_keep isl_set
*set1
,
3236 __isl_keep isl_set
*set2
)
3238 return isl_map_plain_is_disjoint((struct isl_map
*)set1
,
3239 (struct isl_map
*)set2
);
3242 /* Are "set1" and "set2" disjoint?
3244 isl_bool
isl_set_is_disjoint(__isl_keep isl_set
*set1
, __isl_keep isl_set
*set2
)
3246 return isl_map_is_disjoint(set1
, set2
);
3249 /* Check if we can combine a given div with lower bound l and upper
3250 * bound u with some other div and if so return that other div.
3251 * Otherwise return -1.
3253 * We first check that
3254 * - the bounds are opposites of each other (except for the constant
3256 * - the bounds do not reference any other div
3257 * - no div is defined in terms of this div
3259 * Let m be the size of the range allowed on the div by the bounds.
3260 * That is, the bounds are of the form
3262 * e <= a <= e + m - 1
3264 * with e some expression in the other variables.
3265 * We look for another div b such that no third div is defined in terms
3266 * of this second div b and such that in any constraint that contains
3267 * a (except for the given lower and upper bound), also contains b
3268 * with a coefficient that is m times that of b.
3269 * That is, all constraints (execpt for the lower and upper bound)
3272 * e + f (a + m b) >= 0
3274 * If so, we return b so that "a + m b" can be replaced by
3275 * a single div "c = a + m b".
3277 static int div_find_coalesce(struct isl_basic_map
*bmap
, int *pairs
,
3278 unsigned div
, unsigned l
, unsigned u
)
3284 if (bmap
->n_div
<= 1)
3286 dim
= isl_space_dim(bmap
->dim
, isl_dim_all
);
3287 if (isl_seq_first_non_zero(bmap
->ineq
[l
] + 1 + dim
, div
) != -1)
3289 if (isl_seq_first_non_zero(bmap
->ineq
[l
] + 1 + dim
+ div
+ 1,
3290 bmap
->n_div
- div
- 1) != -1)
3292 if (!isl_seq_is_neg(bmap
->ineq
[l
] + 1, bmap
->ineq
[u
] + 1,
3296 for (i
= 0; i
< bmap
->n_div
; ++i
) {
3297 if (isl_int_is_zero(bmap
->div
[i
][0]))
3299 if (!isl_int_is_zero(bmap
->div
[i
][1 + 1 + dim
+ div
]))
3303 isl_int_add(bmap
->ineq
[l
][0], bmap
->ineq
[l
][0], bmap
->ineq
[u
][0]);
3304 if (isl_int_is_neg(bmap
->ineq
[l
][0])) {
3305 isl_int_sub(bmap
->ineq
[l
][0],
3306 bmap
->ineq
[l
][0], bmap
->ineq
[u
][0]);
3307 bmap
= isl_basic_map_copy(bmap
);
3308 bmap
= isl_basic_map_set_to_empty(bmap
);
3309 isl_basic_map_free(bmap
);
3312 isl_int_add_ui(bmap
->ineq
[l
][0], bmap
->ineq
[l
][0], 1);
3313 for (i
= 0; i
< bmap
->n_div
; ++i
) {
3318 for (j
= 0; j
< bmap
->n_div
; ++j
) {
3319 if (isl_int_is_zero(bmap
->div
[j
][0]))
3321 if (!isl_int_is_zero(bmap
->div
[j
][1 + 1 + dim
+ i
]))
3324 if (j
< bmap
->n_div
)
3326 for (j
= 0; j
< bmap
->n_ineq
; ++j
) {
3328 if (j
== l
|| j
== u
)
3330 if (isl_int_is_zero(bmap
->ineq
[j
][1 + dim
+ div
]))
3332 if (isl_int_is_zero(bmap
->ineq
[j
][1 + dim
+ i
]))
3334 isl_int_mul(bmap
->ineq
[j
][1 + dim
+ div
],
3335 bmap
->ineq
[j
][1 + dim
+ div
],
3337 valid
= isl_int_eq(bmap
->ineq
[j
][1 + dim
+ div
],
3338 bmap
->ineq
[j
][1 + dim
+ i
]);
3339 isl_int_divexact(bmap
->ineq
[j
][1 + dim
+ div
],
3340 bmap
->ineq
[j
][1 + dim
+ div
],
3345 if (j
< bmap
->n_ineq
)
3350 isl_int_sub_ui(bmap
->ineq
[l
][0], bmap
->ineq
[l
][0], 1);
3351 isl_int_sub(bmap
->ineq
[l
][0], bmap
->ineq
[l
][0], bmap
->ineq
[u
][0]);
3355 /* Given a lower and an upper bound on div i, construct an inequality
3356 * that when nonnegative ensures that this pair of bounds always allows
3357 * for an integer value of the given div.
3358 * The lower bound is inequality l, while the upper bound is inequality u.
3359 * The constructed inequality is stored in ineq.
3360 * g, fl, fu are temporary scalars.
3362 * Let the upper bound be
3366 * and the lower bound
3370 * Let n_u = f_u g and n_l = f_l g, with g = gcd(n_u, n_l).
3373 * - f_u e_l <= f_u f_l g a <= f_l e_u
3375 * Since all variables are integer valued, this is equivalent to
3377 * - f_u e_l - (f_u - 1) <= f_u f_l g a <= f_l e_u + (f_l - 1)
3379 * If this interval is at least f_u f_l g, then it contains at least
3380 * one integer value for a.
3381 * That is, the test constraint is
3383 * f_l e_u + f_u e_l + f_l - 1 + f_u - 1 + 1 >= f_u f_l g
3385 static void construct_test_ineq(struct isl_basic_map
*bmap
, int i
,
3386 int l
, int u
, isl_int
*ineq
, isl_int
*g
, isl_int
*fl
, isl_int
*fu
)
3389 dim
= isl_space_dim(bmap
->dim
, isl_dim_all
);
3391 isl_int_gcd(*g
, bmap
->ineq
[l
][1 + dim
+ i
], bmap
->ineq
[u
][1 + dim
+ i
]);
3392 isl_int_divexact(*fl
, bmap
->ineq
[l
][1 + dim
+ i
], *g
);
3393 isl_int_divexact(*fu
, bmap
->ineq
[u
][1 + dim
+ i
], *g
);
3394 isl_int_neg(*fu
, *fu
);
3395 isl_seq_combine(ineq
, *fl
, bmap
->ineq
[u
], *fu
, bmap
->ineq
[l
],
3396 1 + dim
+ bmap
->n_div
);
3397 isl_int_add(ineq
[0], ineq
[0], *fl
);
3398 isl_int_add(ineq
[0], ineq
[0], *fu
);
3399 isl_int_sub_ui(ineq
[0], ineq
[0], 1);
3400 isl_int_mul(*g
, *g
, *fl
);
3401 isl_int_mul(*g
, *g
, *fu
);
3402 isl_int_sub(ineq
[0], ineq
[0], *g
);
3405 /* Remove more kinds of divs that are not strictly needed.
3406 * In particular, if all pairs of lower and upper bounds on a div
3407 * are such that they allow at least one integer value of the div,
3408 * the we can eliminate the div using Fourier-Motzkin without
3409 * introducing any spurious solutions.
3411 static struct isl_basic_map
*drop_more_redundant_divs(
3412 struct isl_basic_map
*bmap
, int *pairs
, int n
)
3414 struct isl_tab
*tab
= NULL
;
3415 struct isl_vec
*vec
= NULL
;
3427 dim
= isl_space_dim(bmap
->dim
, isl_dim_all
);
3428 vec
= isl_vec_alloc(bmap
->ctx
, 1 + dim
+ bmap
->n_div
);
3432 tab
= isl_tab_from_basic_map(bmap
, 0);
3437 enum isl_lp_result res
;
3439 for (i
= 0; i
< bmap
->n_div
; ++i
) {
3442 if (best
>= 0 && pairs
[best
] <= pairs
[i
])
3448 for (l
= 0; l
< bmap
->n_ineq
; ++l
) {
3449 if (!isl_int_is_pos(bmap
->ineq
[l
][1 + dim
+ i
]))
3451 for (u
= 0; u
< bmap
->n_ineq
; ++u
) {
3452 if (!isl_int_is_neg(bmap
->ineq
[u
][1 + dim
+ i
]))
3454 construct_test_ineq(bmap
, i
, l
, u
,
3455 vec
->el
, &g
, &fl
, &fu
);
3456 res
= isl_tab_min(tab
, vec
->el
,
3457 bmap
->ctx
->one
, &g
, NULL
, 0);
3458 if (res
== isl_lp_error
)
3460 if (res
== isl_lp_empty
) {
3461 bmap
= isl_basic_map_set_to_empty(bmap
);
3464 if (res
!= isl_lp_ok
|| isl_int_is_neg(g
))
3467 if (u
< bmap
->n_ineq
)
3470 if (l
== bmap
->n_ineq
) {
3490 bmap
= isl_basic_map_remove_dims(bmap
, isl_dim_div
, remove
, 1);
3491 return isl_basic_map_drop_redundant_divs(bmap
);
3494 isl_basic_map_free(bmap
);
3503 /* Given a pair of divs div1 and div2 such that, expect for the lower bound l
3504 * and the upper bound u, div1 always occurs together with div2 in the form
3505 * (div1 + m div2), where m is the constant range on the variable div1
3506 * allowed by l and u, replace the pair div1 and div2 by a single
3507 * div that is equal to div1 + m div2.
3509 * The new div will appear in the location that contains div2.
3510 * We need to modify all constraints that contain
3511 * div2 = (div - div1) / m
3512 * (If a constraint does not contain div2, it will also not contain div1.)
3513 * If the constraint also contains div1, then we know they appear
3514 * as f (div1 + m div2) and we can simply replace (div1 + m div2) by div,
3515 * i.e., the coefficient of div is f.
3517 * Otherwise, we first need to introduce div1 into the constraint.
3526 * A lower bound on div2
3530 * can be replaced by
3532 * (n * (m div 2 + div1) + m t + n f)/g >= 0
3534 * with g = gcd(m,n).
3539 * can be replaced by
3541 * (-n * (m div2 + div1) + m t + n f')/g >= 0
3543 * These constraint are those that we would obtain from eliminating
3544 * div1 using Fourier-Motzkin.
3546 * After all constraints have been modified, we drop the lower and upper
3547 * bound and then drop div1.
3549 static struct isl_basic_map
*coalesce_divs(struct isl_basic_map
*bmap
,
3550 unsigned div1
, unsigned div2
, unsigned l
, unsigned u
)
3555 unsigned dim
, total
;
3558 dim
= isl_space_dim(bmap
->dim
, isl_dim_all
);
3559 total
= 1 + dim
+ bmap
->n_div
;
3564 isl_int_add(m
, bmap
->ineq
[l
][0], bmap
->ineq
[u
][0]);
3565 isl_int_add_ui(m
, m
, 1);
3567 for (i
= 0; i
< bmap
->n_ineq
; ++i
) {
3568 if (i
== l
|| i
== u
)
3570 if (isl_int_is_zero(bmap
->ineq
[i
][1 + dim
+ div2
]))
3572 if (isl_int_is_zero(bmap
->ineq
[i
][1 + dim
+ div1
])) {
3573 isl_int_gcd(b
, m
, bmap
->ineq
[i
][1 + dim
+ div2
]);
3574 isl_int_divexact(a
, m
, b
);
3575 isl_int_divexact(b
, bmap
->ineq
[i
][1 + dim
+ div2
], b
);
3576 if (isl_int_is_pos(b
)) {
3577 isl_seq_combine(bmap
->ineq
[i
], a
, bmap
->ineq
[i
],
3578 b
, bmap
->ineq
[l
], total
);
3581 isl_seq_combine(bmap
->ineq
[i
], a
, bmap
->ineq
[i
],
3582 b
, bmap
->ineq
[u
], total
);
3585 isl_int_set(bmap
->ineq
[i
][1 + dim
+ div2
],
3586 bmap
->ineq
[i
][1 + dim
+ div1
]);
3587 isl_int_set_si(bmap
->ineq
[i
][1 + dim
+ div1
], 0);
3594 isl_basic_map_drop_inequality(bmap
, l
);
3595 isl_basic_map_drop_inequality(bmap
, u
);
3597 isl_basic_map_drop_inequality(bmap
, u
);
3598 isl_basic_map_drop_inequality(bmap
, l
);
3600 bmap
= isl_basic_map_drop_div(bmap
, div1
);
3604 /* First check if we can coalesce any pair of divs and
3605 * then continue with dropping more redundant divs.
3607 * We loop over all pairs of lower and upper bounds on a div
3608 * with coefficient 1 and -1, respectively, check if there
3609 * is any other div "c" with which we can coalesce the div
3610 * and if so, perform the coalescing.
3612 static struct isl_basic_map
*coalesce_or_drop_more_redundant_divs(
3613 struct isl_basic_map
*bmap
, int *pairs
, int n
)
3618 dim
= isl_space_dim(bmap
->dim
, isl_dim_all
);
3620 for (i
= 0; i
< bmap
->n_div
; ++i
) {
3623 for (l
= 0; l
< bmap
->n_ineq
; ++l
) {
3624 if (!isl_int_is_one(bmap
->ineq
[l
][1 + dim
+ i
]))
3626 for (u
= 0; u
< bmap
->n_ineq
; ++u
) {
3629 if (!isl_int_is_negone(bmap
->ineq
[u
][1+dim
+i
]))
3631 c
= div_find_coalesce(bmap
, pairs
, i
, l
, u
);
3635 bmap
= coalesce_divs(bmap
, i
, c
, l
, u
);
3636 return isl_basic_map_drop_redundant_divs(bmap
);
3641 if (ISL_F_ISSET(bmap
, ISL_BASIC_MAP_EMPTY
))
3644 return drop_more_redundant_divs(bmap
, pairs
, n
);
3647 /* Remove divs that are not strictly needed.
3648 * In particular, if a div only occurs positively (or negatively)
3649 * in constraints, then it can simply be dropped.
3650 * Also, if a div occurs in only two constraints and if moreover
3651 * those two constraints are opposite to each other, except for the constant
3652 * term and if the sum of the constant terms is such that for any value
3653 * of the other values, there is always at least one integer value of the
3654 * div, i.e., if one plus this sum is greater than or equal to
3655 * the (absolute value) of the coefficent of the div in the constraints,
3656 * then we can also simply drop the div.
3658 * We skip divs that appear in equalities or in the definition of other divs.
3659 * Divs that appear in the definition of other divs usually occur in at least
3660 * 4 constraints, but the constraints may have been simplified.
3662 * If any divs are left after these simple checks then we move on
3663 * to more complicated cases in drop_more_redundant_divs.
3665 struct isl_basic_map
*isl_basic_map_drop_redundant_divs(
3666 struct isl_basic_map
*bmap
)
3675 if (bmap
->n_div
== 0)
3678 off
= isl_space_dim(bmap
->dim
, isl_dim_all
);
3679 pairs
= isl_calloc_array(bmap
->ctx
, int, bmap
->n_div
);
3683 for (i
= 0; i
< bmap
->n_div
; ++i
) {
3685 int last_pos
, last_neg
;
3689 defined
= !isl_int_is_zero(bmap
->div
[i
][0]);
3690 for (j
= i
; j
< bmap
->n_div
; ++j
)
3691 if (!isl_int_is_zero(bmap
->div
[j
][1 + 1 + off
+ i
]))
3693 if (j
< bmap
->n_div
)
3695 for (j
= 0; j
< bmap
->n_eq
; ++j
)
3696 if (!isl_int_is_zero(bmap
->eq
[j
][1 + off
+ i
]))
3702 for (j
= 0; j
< bmap
->n_ineq
; ++j
) {
3703 if (isl_int_is_pos(bmap
->ineq
[j
][1 + off
+ i
])) {
3707 if (isl_int_is_neg(bmap
->ineq
[j
][1 + off
+ i
])) {
3712 pairs
[i
] = pos
* neg
;
3713 if (pairs
[i
] == 0) {
3714 for (j
= bmap
->n_ineq
- 1; j
>= 0; --j
)
3715 if (!isl_int_is_zero(bmap
->ineq
[j
][1+off
+i
]))
3716 isl_basic_map_drop_inequality(bmap
, j
);
3717 bmap
= isl_basic_map_drop_div(bmap
, i
);
3719 return isl_basic_map_drop_redundant_divs(bmap
);
3723 if (!isl_seq_is_neg(bmap
->ineq
[last_pos
] + 1,
3724 bmap
->ineq
[last_neg
] + 1,
3728 isl_int_add(bmap
->ineq
[last_pos
][0],
3729 bmap
->ineq
[last_pos
][0], bmap
->ineq
[last_neg
][0]);
3730 isl_int_add_ui(bmap
->ineq
[last_pos
][0],
3731 bmap
->ineq
[last_pos
][0], 1);
3732 redundant
= isl_int_ge(bmap
->ineq
[last_pos
][0],
3733 bmap
->ineq
[last_pos
][1+off
+i
]);
3734 isl_int_sub_ui(bmap
->ineq
[last_pos
][0],
3735 bmap
->ineq
[last_pos
][0], 1);
3736 isl_int_sub(bmap
->ineq
[last_pos
][0],
3737 bmap
->ineq
[last_pos
][0], bmap
->ineq
[last_neg
][0]);
3740 !ok_to_set_div_from_bound(bmap
, i
, last_pos
)) {
3745 bmap
= set_div_from_lower_bound(bmap
, i
, last_pos
);
3746 bmap
= isl_basic_map_simplify(bmap
);
3748 return isl_basic_map_drop_redundant_divs(bmap
);
3750 if (last_pos
> last_neg
) {
3751 isl_basic_map_drop_inequality(bmap
, last_pos
);
3752 isl_basic_map_drop_inequality(bmap
, last_neg
);
3754 isl_basic_map_drop_inequality(bmap
, last_neg
);
3755 isl_basic_map_drop_inequality(bmap
, last_pos
);
3757 bmap
= isl_basic_map_drop_div(bmap
, i
);
3759 return isl_basic_map_drop_redundant_divs(bmap
);
3763 return coalesce_or_drop_more_redundant_divs(bmap
, pairs
, n
);
3769 isl_basic_map_free(bmap
);
3773 struct isl_basic_set
*isl_basic_set_drop_redundant_divs(
3774 struct isl_basic_set
*bset
)
3776 return (struct isl_basic_set
*)
3777 isl_basic_map_drop_redundant_divs((struct isl_basic_map
*)bset
);
3780 struct isl_map
*isl_map_drop_redundant_divs(struct isl_map
*map
)
3786 for (i
= 0; i
< map
->n
; ++i
) {
3787 map
->p
[i
] = isl_basic_map_drop_redundant_divs(map
->p
[i
]);
3791 ISL_F_CLR(map
, ISL_MAP_NORMALIZED
);
3798 struct isl_set
*isl_set_drop_redundant_divs(struct isl_set
*set
)
3800 return (struct isl_set
*)
3801 isl_map_drop_redundant_divs((struct isl_map
*)set
);
3804 /* Does "bmap" satisfy any equality that involves more than 2 variables
3805 * and/or has coefficients different from -1 and 1?
3807 static int has_multiple_var_equality(__isl_keep isl_basic_map
*bmap
)
3812 total
= isl_basic_map_dim(bmap
, isl_dim_all
);
3814 for (i
= 0; i
< bmap
->n_eq
; ++i
) {
3817 j
= isl_seq_first_non_zero(bmap
->eq
[i
] + 1, total
);
3820 if (!isl_int_is_one(bmap
->eq
[i
][1 + j
]) &&
3821 !isl_int_is_negone(bmap
->eq
[i
][1 + j
]))
3825 k
= isl_seq_first_non_zero(bmap
->eq
[i
] + 1 + j
, total
- j
);
3829 if (!isl_int_is_one(bmap
->eq
[i
][1 + j
]) &&
3830 !isl_int_is_negone(bmap
->eq
[i
][1 + j
]))
3834 k
= isl_seq_first_non_zero(bmap
->eq
[i
] + 1 + j
, total
- j
);
3842 /* Remove any common factor g from the constraint coefficients in "v".
3843 * The constant term is stored in the first position and is replaced
3844 * by floor(c/g). If any common factor is removed and if this results
3845 * in a tightening of the constraint, then set *tightened.
3847 static __isl_give isl_vec
*normalize_constraint(__isl_take isl_vec
*v
,
3854 ctx
= isl_vec_get_ctx(v
);
3855 isl_seq_gcd(v
->el
+ 1, v
->size
- 1, &ctx
->normalize_gcd
);
3856 if (isl_int_is_zero(ctx
->normalize_gcd
))
3858 if (isl_int_is_one(ctx
->normalize_gcd
))
3863 if (tightened
&& !isl_int_is_divisible_by(v
->el
[0], ctx
->normalize_gcd
))
3865 isl_int_fdiv_q(v
->el
[0], v
->el
[0], ctx
->normalize_gcd
);
3866 isl_seq_scale_down(v
->el
+ 1, v
->el
+ 1, ctx
->normalize_gcd
,
3871 /* If "bmap" is an integer set that satisfies any equality involving
3872 * more than 2 variables and/or has coefficients different from -1 and 1,
3873 * then use variable compression to reduce the coefficients by removing
3874 * any (hidden) common factor.
3875 * In particular, apply the variable compression to each constraint,
3876 * factor out any common factor in the non-constant coefficients and
3877 * then apply the inverse of the compression.
3878 * At the end, we mark the basic map as having reduced constants.
3879 * If this flag is still set on the next invocation of this function,
3880 * then we skip the computation.
3882 * Removing a common factor may result in a tightening of some of
3883 * the constraints. If this happens, then we may end up with two
3884 * opposite inequalities that can be replaced by an equality.
3885 * We therefore call isl_basic_map_detect_inequality_pairs,
3886 * which checks for such pairs of inequalities as well as eliminate_divs_eq
3887 * and isl_basic_map_gauss if such a pair was found.
3889 __isl_give isl_basic_map
*isl_basic_map_reduce_coefficients(
3890 __isl_take isl_basic_map
*bmap
)
3895 isl_mat
*eq
, *T
, *T2
;
3901 if (ISL_F_ISSET(bmap
, ISL_BASIC_MAP_REDUCED_COEFFICIENTS
))
3903 if (isl_basic_map_is_rational(bmap
))
3905 if (bmap
->n_eq
== 0)
3907 if (!has_multiple_var_equality(bmap
))
3910 total
= isl_basic_map_dim(bmap
, isl_dim_all
);
3911 ctx
= isl_basic_map_get_ctx(bmap
);
3912 v
= isl_vec_alloc(ctx
, 1 + total
);
3914 return isl_basic_map_free(bmap
);
3916 eq
= isl_mat_sub_alloc6(ctx
, bmap
->eq
, 0, bmap
->n_eq
, 0, 1 + total
);
3917 T
= isl_mat_variable_compression(eq
, &T2
);
3920 if (T
->n_col
== 0) {
3924 return isl_basic_map_set_to_empty(bmap
);
3928 for (i
= 0; i
< bmap
->n_ineq
; ++i
) {
3929 isl_seq_cpy(v
->el
, bmap
->ineq
[i
], 1 + total
);
3930 v
= isl_vec_mat_product(v
, isl_mat_copy(T
));
3931 v
= normalize_constraint(v
, &tightened
);
3932 v
= isl_vec_mat_product(v
, isl_mat_copy(T2
));
3935 isl_seq_cpy(bmap
->ineq
[i
], v
->el
, 1 + total
);
3942 ISL_F_SET(bmap
, ISL_BASIC_MAP_REDUCED_COEFFICIENTS
);
3947 bmap
= isl_basic_map_detect_inequality_pairs(bmap
, &progress
);
3949 bmap
= eliminate_divs_eq(bmap
, &progress
);
3950 bmap
= isl_basic_map_gauss(bmap
, NULL
);
3959 return isl_basic_map_free(bmap
);
3962 /* Shift the integer division at position "div" of "bmap"
3963 * by "shift" times the variable at position "pos".
3964 * "pos" is as determined by isl_basic_map_offset, i.e., pos == 0
3965 * corresponds to the constant term.
3967 * That is, if the integer division has the form
3971 * then replace it by
3973 * floor((f(x) + shift * d * x_pos)/d) - shift * x_pos
3975 __isl_give isl_basic_map
*isl_basic_map_shift_div(
3976 __isl_take isl_basic_map
*bmap
, int div
, int pos
, isl_int shift
)
3984 total
= isl_basic_map_dim(bmap
, isl_dim_all
);
3985 total
-= isl_basic_map_dim(bmap
, isl_dim_div
);
3987 isl_int_addmul(bmap
->div
[div
][1 + pos
], shift
, bmap
->div
[div
][0]);
3989 for (i
= 0; i
< bmap
->n_eq
; ++i
) {
3990 if (isl_int_is_zero(bmap
->eq
[i
][1 + total
+ div
]))
3992 isl_int_submul(bmap
->eq
[i
][pos
],
3993 shift
, bmap
->eq
[i
][1 + total
+ div
]);
3995 for (i
= 0; i
< bmap
->n_ineq
; ++i
) {
3996 if (isl_int_is_zero(bmap
->ineq
[i
][1 + total
+ div
]))
3998 isl_int_submul(bmap
->ineq
[i
][pos
],
3999 shift
, bmap
->ineq
[i
][1 + total
+ div
]);
4001 for (i
= 0; i
< bmap
->n_div
; ++i
) {
4002 if (isl_int_is_zero(bmap
->div
[i
][0]))
4004 if (isl_int_is_zero(bmap
->div
[i
][1 + 1 + total
+ div
]))
4006 isl_int_submul(bmap
->div
[i
][1 + pos
],
4007 shift
, bmap
->div
[i
][1 + 1 + total
+ div
]);