2 * Copyright 2008-2009 Katholieke Universiteit Leuven
3 * Copyright 2012-2013 Ecole Normale Superieure
4 * Copyright 2014-2015 INRIA Rocquencourt
5 * Copyright 2016 Sven Verdoolaege
7 * Use of this software is governed by the MIT license
9 * Written by Sven Verdoolaege, K.U.Leuven, Departement
10 * Computerwetenschappen, Celestijnenlaan 200A, B-3001 Leuven, Belgium
11 * and Ecole Normale Superieure, 45 rue d’Ulm, 75230 Paris, France
12 * and Inria Paris - Rocquencourt, Domaine de Voluceau - Rocquencourt,
13 * B.P. 105 - 78153 Le Chesnay, France
16 #include <isl_ctx_private.h>
17 #include <isl_map_private.h>
18 #include "isl_equalities.h"
22 #include <isl_space_private.h>
23 #include <isl_mat_private.h>
24 #include <isl_vec_private.h>
26 #include <bset_to_bmap.c>
27 #include <bset_from_bmap.c>
28 #include <set_to_map.c>
29 #include <set_from_map.c>
31 static void swap_equality(struct isl_basic_map
*bmap
, int a
, int b
)
33 isl_int
*t
= bmap
->eq
[a
];
34 bmap
->eq
[a
] = bmap
->eq
[b
];
38 static void swap_inequality(struct isl_basic_map
*bmap
, int a
, int b
)
41 isl_int
*t
= bmap
->ineq
[a
];
42 bmap
->ineq
[a
] = bmap
->ineq
[b
];
47 static void constraint_drop_vars(isl_int
*c
, unsigned n
, unsigned rem
)
49 isl_seq_cpy(c
, c
+ n
, rem
);
50 isl_seq_clr(c
+ rem
, n
);
53 /* Drop n dimensions starting at first.
55 * In principle, this frees up some extra variables as the number
56 * of columns remains constant, but we would have to extend
57 * the div array too as the number of rows in this array is assumed
58 * to be equal to extra.
60 struct isl_basic_set
*isl_basic_set_drop_dims(
61 struct isl_basic_set
*bset
, unsigned first
, unsigned n
)
68 isl_assert(bset
->ctx
, first
+ n
<= bset
->dim
->n_out
, goto error
);
70 if (n
== 0 && !isl_space_get_tuple_name(bset
->dim
, isl_dim_set
))
73 bset
= isl_basic_set_cow(bset
);
77 for (i
= 0; i
< bset
->n_eq
; ++i
)
78 constraint_drop_vars(bset
->eq
[i
]+1+bset
->dim
->nparam
+first
, n
,
79 (bset
->dim
->n_out
-first
-n
)+bset
->extra
);
81 for (i
= 0; i
< bset
->n_ineq
; ++i
)
82 constraint_drop_vars(bset
->ineq
[i
]+1+bset
->dim
->nparam
+first
, n
,
83 (bset
->dim
->n_out
-first
-n
)+bset
->extra
);
85 for (i
= 0; i
< bset
->n_div
; ++i
)
86 constraint_drop_vars(bset
->div
[i
]+1+1+bset
->dim
->nparam
+first
, n
,
87 (bset
->dim
->n_out
-first
-n
)+bset
->extra
);
89 bset
->dim
= isl_space_drop_outputs(bset
->dim
, first
, n
);
93 ISL_F_CLR(bset
, ISL_BASIC_SET_NORMALIZED
);
94 bset
= isl_basic_set_simplify(bset
);
95 return isl_basic_set_finalize(bset
);
97 isl_basic_set_free(bset
);
101 struct isl_set
*isl_set_drop_dims(
102 struct isl_set
*set
, unsigned first
, unsigned n
)
109 isl_assert(set
->ctx
, first
+ n
<= set
->dim
->n_out
, goto error
);
111 if (n
== 0 && !isl_space_get_tuple_name(set
->dim
, isl_dim_set
))
113 set
= isl_set_cow(set
);
116 set
->dim
= isl_space_drop_outputs(set
->dim
, first
, n
);
120 for (i
= 0; i
< set
->n
; ++i
) {
121 set
->p
[i
] = isl_basic_set_drop_dims(set
->p
[i
], first
, n
);
126 ISL_F_CLR(set
, ISL_SET_NORMALIZED
);
133 /* Move "n" divs starting at "first" to the end of the list of divs.
135 static struct isl_basic_map
*move_divs_last(struct isl_basic_map
*bmap
,
136 unsigned first
, unsigned n
)
141 if (first
+ n
== bmap
->n_div
)
144 div
= isl_alloc_array(bmap
->ctx
, isl_int
*, n
);
147 for (i
= 0; i
< n
; ++i
)
148 div
[i
] = bmap
->div
[first
+ i
];
149 for (i
= 0; i
< bmap
->n_div
- first
- n
; ++i
)
150 bmap
->div
[first
+ i
] = bmap
->div
[first
+ n
+ i
];
151 for (i
= 0; i
< n
; ++i
)
152 bmap
->div
[bmap
->n_div
- n
+ i
] = div
[i
];
156 isl_basic_map_free(bmap
);
160 /* Drop "n" dimensions of type "type" starting at "first".
162 * In principle, this frees up some extra variables as the number
163 * of columns remains constant, but we would have to extend
164 * the div array too as the number of rows in this array is assumed
165 * to be equal to extra.
167 struct isl_basic_map
*isl_basic_map_drop(struct isl_basic_map
*bmap
,
168 enum isl_dim_type type
, unsigned first
, unsigned n
)
178 dim
= isl_basic_map_dim(bmap
, type
);
179 isl_assert(bmap
->ctx
, first
+ n
<= dim
, goto error
);
181 if (n
== 0 && !isl_space_is_named_or_nested(bmap
->dim
, type
))
184 bmap
= isl_basic_map_cow(bmap
);
188 offset
= isl_basic_map_offset(bmap
, type
) + first
;
189 left
= isl_basic_map_total_dim(bmap
) - (offset
- 1) - n
;
190 for (i
= 0; i
< bmap
->n_eq
; ++i
)
191 constraint_drop_vars(bmap
->eq
[i
]+offset
, n
, left
);
193 for (i
= 0; i
< bmap
->n_ineq
; ++i
)
194 constraint_drop_vars(bmap
->ineq
[i
]+offset
, n
, left
);
196 for (i
= 0; i
< bmap
->n_div
; ++i
)
197 constraint_drop_vars(bmap
->div
[i
]+1+offset
, n
, left
);
199 if (type
== isl_dim_div
) {
200 bmap
= move_divs_last(bmap
, first
, n
);
203 if (isl_basic_map_free_div(bmap
, n
) < 0)
204 return isl_basic_map_free(bmap
);
206 bmap
->dim
= isl_space_drop_dims(bmap
->dim
, type
, first
, n
);
210 ISL_F_CLR(bmap
, ISL_BASIC_MAP_NORMALIZED
);
211 bmap
= isl_basic_map_simplify(bmap
);
212 return isl_basic_map_finalize(bmap
);
214 isl_basic_map_free(bmap
);
218 __isl_give isl_basic_set
*isl_basic_set_drop(__isl_take isl_basic_set
*bset
,
219 enum isl_dim_type type
, unsigned first
, unsigned n
)
221 return bset_from_bmap(isl_basic_map_drop(bset_to_bmap(bset
),
225 struct isl_basic_map
*isl_basic_map_drop_inputs(
226 struct isl_basic_map
*bmap
, unsigned first
, unsigned n
)
228 return isl_basic_map_drop(bmap
, isl_dim_in
, first
, n
);
231 struct isl_map
*isl_map_drop(struct isl_map
*map
,
232 enum isl_dim_type type
, unsigned first
, unsigned n
)
239 isl_assert(map
->ctx
, first
+ n
<= isl_map_dim(map
, type
), goto error
);
241 if (n
== 0 && !isl_space_get_tuple_name(map
->dim
, type
))
243 map
= isl_map_cow(map
);
246 map
->dim
= isl_space_drop_dims(map
->dim
, type
, first
, n
);
250 for (i
= 0; i
< map
->n
; ++i
) {
251 map
->p
[i
] = isl_basic_map_drop(map
->p
[i
], type
, first
, n
);
255 ISL_F_CLR(map
, ISL_MAP_NORMALIZED
);
263 struct isl_set
*isl_set_drop(struct isl_set
*set
,
264 enum isl_dim_type type
, unsigned first
, unsigned n
)
266 return set_from_map(isl_map_drop(set_to_map(set
), type
, first
, n
));
269 struct isl_map
*isl_map_drop_inputs(
270 struct isl_map
*map
, unsigned first
, unsigned n
)
272 return isl_map_drop(map
, isl_dim_in
, first
, n
);
276 * We don't cow, as the div is assumed to be redundant.
278 __isl_give isl_basic_map
*isl_basic_map_drop_div(
279 __isl_take isl_basic_map
*bmap
, unsigned div
)
287 pos
= 1 + isl_space_dim(bmap
->dim
, isl_dim_all
) + div
;
289 isl_assert(bmap
->ctx
, div
< bmap
->n_div
, goto error
);
291 for (i
= 0; i
< bmap
->n_eq
; ++i
)
292 constraint_drop_vars(bmap
->eq
[i
]+pos
, 1, bmap
->extra
-div
-1);
294 for (i
= 0; i
< bmap
->n_ineq
; ++i
) {
295 if (!isl_int_is_zero(bmap
->ineq
[i
][pos
])) {
296 isl_basic_map_drop_inequality(bmap
, i
);
300 constraint_drop_vars(bmap
->ineq
[i
]+pos
, 1, bmap
->extra
-div
-1);
303 for (i
= 0; i
< bmap
->n_div
; ++i
)
304 constraint_drop_vars(bmap
->div
[i
]+1+pos
, 1, bmap
->extra
-div
-1);
306 if (div
!= bmap
->n_div
- 1) {
308 isl_int
*t
= bmap
->div
[div
];
310 for (j
= div
; j
< bmap
->n_div
- 1; ++j
)
311 bmap
->div
[j
] = bmap
->div
[j
+1];
313 bmap
->div
[bmap
->n_div
- 1] = t
;
315 ISL_F_CLR(bmap
, ISL_BASIC_MAP_NORMALIZED
);
316 isl_basic_map_free_div(bmap
, 1);
320 isl_basic_map_free(bmap
);
324 struct isl_basic_map
*isl_basic_map_normalize_constraints(
325 struct isl_basic_map
*bmap
)
329 unsigned total
= isl_basic_map_total_dim(bmap
);
335 for (i
= bmap
->n_eq
- 1; i
>= 0; --i
) {
336 isl_seq_gcd(bmap
->eq
[i
]+1, total
, &gcd
);
337 if (isl_int_is_zero(gcd
)) {
338 if (!isl_int_is_zero(bmap
->eq
[i
][0])) {
339 bmap
= isl_basic_map_set_to_empty(bmap
);
342 isl_basic_map_drop_equality(bmap
, i
);
345 if (ISL_F_ISSET(bmap
, ISL_BASIC_MAP_RATIONAL
))
346 isl_int_gcd(gcd
, gcd
, bmap
->eq
[i
][0]);
347 if (isl_int_is_one(gcd
))
349 if (!isl_int_is_divisible_by(bmap
->eq
[i
][0], gcd
)) {
350 bmap
= isl_basic_map_set_to_empty(bmap
);
353 isl_seq_scale_down(bmap
->eq
[i
], bmap
->eq
[i
], gcd
, 1+total
);
356 for (i
= bmap
->n_ineq
- 1; i
>= 0; --i
) {
357 isl_seq_gcd(bmap
->ineq
[i
]+1, total
, &gcd
);
358 if (isl_int_is_zero(gcd
)) {
359 if (isl_int_is_neg(bmap
->ineq
[i
][0])) {
360 bmap
= isl_basic_map_set_to_empty(bmap
);
363 isl_basic_map_drop_inequality(bmap
, i
);
366 if (ISL_F_ISSET(bmap
, ISL_BASIC_MAP_RATIONAL
))
367 isl_int_gcd(gcd
, gcd
, bmap
->ineq
[i
][0]);
368 if (isl_int_is_one(gcd
))
370 isl_int_fdiv_q(bmap
->ineq
[i
][0], bmap
->ineq
[i
][0], gcd
);
371 isl_seq_scale_down(bmap
->ineq
[i
]+1, bmap
->ineq
[i
]+1, gcd
, total
);
378 struct isl_basic_set
*isl_basic_set_normalize_constraints(
379 struct isl_basic_set
*bset
)
381 isl_basic_map
*bmap
= bset_to_bmap(bset
);
382 return bset_from_bmap(isl_basic_map_normalize_constraints(bmap
));
385 /* Reduce the coefficient of the variable at position "pos"
386 * in integer division "div", such that it lies in the half-open
387 * interval (1/2,1/2], extracting any excess value from this integer division.
388 * "pos" is as determined by isl_basic_map_offset, i.e., pos == 0
389 * corresponds to the constant term.
391 * That is, the integer division is of the form
393 * floor((... + (c * d + r) * x_pos + ...)/d)
395 * with -d < 2 * r <= d.
398 * floor((... + r * x_pos + ...)/d) + c * x_pos
400 * If 2 * ((c * d + r) % d) <= d, then c = floor((c * d + r)/d).
401 * Otherwise, c = floor((c * d + r)/d) + 1.
403 * This is the same normalization that is performed by isl_aff_floor.
405 static __isl_give isl_basic_map
*reduce_coefficient_in_div(
406 __isl_take isl_basic_map
*bmap
, int div
, int pos
)
412 isl_int_fdiv_r(shift
, bmap
->div
[div
][1 + pos
], bmap
->div
[div
][0]);
413 isl_int_mul_ui(shift
, shift
, 2);
414 add_one
= isl_int_gt(shift
, bmap
->div
[div
][0]);
415 isl_int_fdiv_q(shift
, bmap
->div
[div
][1 + pos
], bmap
->div
[div
][0]);
417 isl_int_add_ui(shift
, shift
, 1);
418 isl_int_neg(shift
, shift
);
419 bmap
= isl_basic_map_shift_div(bmap
, div
, pos
, shift
);
420 isl_int_clear(shift
);
425 /* Does the coefficient of the variable at position "pos"
426 * in integer division "div" need to be reduced?
427 * That is, does it lie outside the half-open interval (1/2,1/2]?
428 * The coefficient c/d lies outside this interval if abs(2 * c) >= d and
431 static isl_bool
needs_reduction(__isl_keep isl_basic_map
*bmap
, int div
,
436 if (isl_int_is_zero(bmap
->div
[div
][1 + pos
]))
437 return isl_bool_false
;
439 isl_int_mul_ui(bmap
->div
[div
][1 + pos
], bmap
->div
[div
][1 + pos
], 2);
440 r
= isl_int_abs_ge(bmap
->div
[div
][1 + pos
], bmap
->div
[div
][0]) &&
441 !isl_int_eq(bmap
->div
[div
][1 + pos
], bmap
->div
[div
][0]);
442 isl_int_divexact_ui(bmap
->div
[div
][1 + pos
],
443 bmap
->div
[div
][1 + pos
], 2);
448 /* Reduce the coefficients (including the constant term) of
449 * integer division "div", if needed.
450 * In particular, make sure all coefficients lie in
451 * the half-open interval (1/2,1/2].
453 static __isl_give isl_basic_map
*reduce_div_coefficients_of_div(
454 __isl_take isl_basic_map
*bmap
, int div
)
457 unsigned total
= 1 + isl_basic_map_total_dim(bmap
);
459 for (i
= 0; i
< total
; ++i
) {
462 reduce
= needs_reduction(bmap
, div
, i
);
464 return isl_basic_map_free(bmap
);
467 bmap
= reduce_coefficient_in_div(bmap
, div
, i
);
475 /* Reduce the coefficients (including the constant term) of
476 * the known integer divisions, if needed
477 * In particular, make sure all coefficients lie in
478 * the half-open interval (1/2,1/2].
480 static __isl_give isl_basic_map
*reduce_div_coefficients(
481 __isl_take isl_basic_map
*bmap
)
487 if (bmap
->n_div
== 0)
490 for (i
= 0; i
< bmap
->n_div
; ++i
) {
491 if (isl_int_is_zero(bmap
->div
[i
][0]))
493 bmap
= reduce_div_coefficients_of_div(bmap
, i
);
501 /* Remove any common factor in numerator and denominator of the div expression,
502 * not taking into account the constant term.
503 * That is, if the div is of the form
505 * floor((a + m f(x))/(m d))
509 * floor((floor(a/m) + f(x))/d)
511 * The difference {a/m}/d in the argument satisfies 0 <= {a/m}/d < 1/d
512 * and can therefore not influence the result of the floor.
514 static void normalize_div_expression(__isl_keep isl_basic_map
*bmap
, int div
)
516 unsigned total
= isl_basic_map_total_dim(bmap
);
517 isl_ctx
*ctx
= bmap
->ctx
;
519 if (isl_int_is_zero(bmap
->div
[div
][0]))
521 isl_seq_gcd(bmap
->div
[div
] + 2, total
, &ctx
->normalize_gcd
);
522 isl_int_gcd(ctx
->normalize_gcd
, ctx
->normalize_gcd
, bmap
->div
[div
][0]);
523 if (isl_int_is_one(ctx
->normalize_gcd
))
525 isl_int_fdiv_q(bmap
->div
[div
][1], bmap
->div
[div
][1],
527 isl_int_divexact(bmap
->div
[div
][0], bmap
->div
[div
][0],
529 isl_seq_scale_down(bmap
->div
[div
] + 2, bmap
->div
[div
] + 2,
530 ctx
->normalize_gcd
, total
);
533 /* Remove any common factor in numerator and denominator of a div expression,
534 * not taking into account the constant term.
535 * That is, look for any div of the form
537 * floor((a + m f(x))/(m d))
541 * floor((floor(a/m) + f(x))/d)
543 * The difference {a/m}/d in the argument satisfies 0 <= {a/m}/d < 1/d
544 * and can therefore not influence the result of the floor.
546 static __isl_give isl_basic_map
*normalize_div_expressions(
547 __isl_take isl_basic_map
*bmap
)
553 if (bmap
->n_div
== 0)
556 for (i
= 0; i
< bmap
->n_div
; ++i
)
557 normalize_div_expression(bmap
, i
);
562 /* Assumes divs have been ordered if keep_divs is set.
564 static void eliminate_var_using_equality(struct isl_basic_map
*bmap
,
565 unsigned pos
, isl_int
*eq
, int keep_divs
, int *progress
)
568 unsigned space_total
;
572 total
= isl_basic_map_total_dim(bmap
);
573 space_total
= isl_space_dim(bmap
->dim
, isl_dim_all
);
574 last_div
= isl_seq_last_non_zero(eq
+ 1 + space_total
, bmap
->n_div
);
575 for (k
= 0; k
< bmap
->n_eq
; ++k
) {
576 if (bmap
->eq
[k
] == eq
)
578 if (isl_int_is_zero(bmap
->eq
[k
][1+pos
]))
582 isl_seq_elim(bmap
->eq
[k
], eq
, 1+pos
, 1+total
, NULL
);
583 isl_seq_normalize(bmap
->ctx
, bmap
->eq
[k
], 1 + total
);
586 for (k
= 0; k
< bmap
->n_ineq
; ++k
) {
587 if (isl_int_is_zero(bmap
->ineq
[k
][1+pos
]))
591 isl_seq_elim(bmap
->ineq
[k
], eq
, 1+pos
, 1+total
, NULL
);
592 isl_seq_normalize(bmap
->ctx
, bmap
->ineq
[k
], 1 + total
);
593 ISL_F_CLR(bmap
, ISL_BASIC_MAP_NORMALIZED
);
596 for (k
= 0; k
< bmap
->n_div
; ++k
) {
597 if (isl_int_is_zero(bmap
->div
[k
][0]))
599 if (isl_int_is_zero(bmap
->div
[k
][1+1+pos
]))
603 /* We need to be careful about circular definitions,
604 * so for now we just remove the definition of div k
605 * if the equality contains any divs.
606 * If keep_divs is set, then the divs have been ordered
607 * and we can keep the definition as long as the result
610 if (last_div
== -1 || (keep_divs
&& last_div
< k
)) {
611 isl_seq_elim(bmap
->div
[k
]+1, eq
,
612 1+pos
, 1+total
, &bmap
->div
[k
][0]);
613 normalize_div_expression(bmap
, k
);
615 isl_seq_clr(bmap
->div
[k
], 1 + total
);
616 ISL_F_CLR(bmap
, ISL_BASIC_MAP_NORMALIZED
);
620 /* Assumes divs have been ordered if keep_divs is set.
622 static __isl_give isl_basic_map
*eliminate_div(__isl_take isl_basic_map
*bmap
,
623 isl_int
*eq
, unsigned div
, int keep_divs
)
625 unsigned pos
= isl_space_dim(bmap
->dim
, isl_dim_all
) + div
;
627 eliminate_var_using_equality(bmap
, pos
, eq
, keep_divs
, NULL
);
629 bmap
= isl_basic_map_drop_div(bmap
, div
);
634 /* Check if elimination of div "div" using equality "eq" would not
635 * result in a div depending on a later div.
637 static int ok_to_eliminate_div(struct isl_basic_map
*bmap
, isl_int
*eq
,
642 unsigned space_total
= isl_space_dim(bmap
->dim
, isl_dim_all
);
643 unsigned pos
= space_total
+ div
;
645 last_div
= isl_seq_last_non_zero(eq
+ 1 + space_total
, bmap
->n_div
);
646 if (last_div
< 0 || last_div
<= div
)
649 for (k
= 0; k
<= last_div
; ++k
) {
650 if (isl_int_is_zero(bmap
->div
[k
][0]))
652 if (!isl_int_is_zero(bmap
->div
[k
][1 + 1 + pos
]))
659 /* Elimininate divs based on equalities
661 static struct isl_basic_map
*eliminate_divs_eq(
662 struct isl_basic_map
*bmap
, int *progress
)
669 bmap
= isl_basic_map_order_divs(bmap
);
674 off
= 1 + isl_space_dim(bmap
->dim
, isl_dim_all
);
676 for (d
= bmap
->n_div
- 1; d
>= 0 ; --d
) {
677 for (i
= 0; i
< bmap
->n_eq
; ++i
) {
678 if (!isl_int_is_one(bmap
->eq
[i
][off
+ d
]) &&
679 !isl_int_is_negone(bmap
->eq
[i
][off
+ d
]))
681 if (!ok_to_eliminate_div(bmap
, bmap
->eq
[i
], d
))
685 bmap
= eliminate_div(bmap
, bmap
->eq
[i
], d
, 1);
686 if (isl_basic_map_drop_equality(bmap
, i
) < 0)
687 return isl_basic_map_free(bmap
);
692 return eliminate_divs_eq(bmap
, progress
);
696 /* Elimininate divs based on inequalities
698 static struct isl_basic_map
*eliminate_divs_ineq(
699 struct isl_basic_map
*bmap
, int *progress
)
710 off
= 1 + isl_space_dim(bmap
->dim
, isl_dim_all
);
712 for (d
= bmap
->n_div
- 1; d
>= 0 ; --d
) {
713 for (i
= 0; i
< bmap
->n_eq
; ++i
)
714 if (!isl_int_is_zero(bmap
->eq
[i
][off
+ d
]))
718 for (i
= 0; i
< bmap
->n_ineq
; ++i
)
719 if (isl_int_abs_gt(bmap
->ineq
[i
][off
+ d
], ctx
->one
))
721 if (i
< bmap
->n_ineq
)
724 bmap
= isl_basic_map_eliminate_vars(bmap
, (off
-1)+d
, 1);
725 if (!bmap
|| ISL_F_ISSET(bmap
, ISL_BASIC_MAP_EMPTY
))
727 bmap
= isl_basic_map_drop_div(bmap
, d
);
734 /* Does the equality constraint at position "eq" in "bmap" involve
735 * any local variables in the range [first, first + n)
736 * that are not marked as having an explicit representation?
738 static isl_bool
bmap_eq_involves_unknown_divs(__isl_keep isl_basic_map
*bmap
,
739 int eq
, unsigned first
, unsigned n
)
745 return isl_bool_error
;
747 o_div
= isl_basic_map_offset(bmap
, isl_dim_div
);
748 for (i
= 0; i
< n
; ++i
) {
751 if (isl_int_is_zero(bmap
->eq
[eq
][o_div
+ first
+ i
]))
753 unknown
= isl_basic_map_div_is_marked_unknown(bmap
, first
+ i
);
755 return isl_bool_error
;
757 return isl_bool_true
;
760 return isl_bool_false
;
763 /* The last local variable involved in the equality constraint
764 * at position "eq" in "bmap" is the local variable at position "div".
765 * It can therefore be used to extract an explicit representation
767 * Do so unless the local variable already has an explicit representation or
768 * the explicit representation would involve any other local variables
769 * that in turn do not have an explicit representation.
770 * An equality constraint involving local variables without an explicit
771 * representation can be used in isl_basic_map_drop_redundant_divs
772 * to separate out an independent local variable. Introducing
773 * an explicit representation here would block this transformation,
774 * while the partial explicit representation in itself is not very useful.
775 * Set *progress if anything is changed.
777 * The equality constraint is of the form
781 * with n a positive number. The explicit representation derived from
786 static __isl_give isl_basic_map
*set_div_from_eq(__isl_take isl_basic_map
*bmap
,
787 int div
, int eq
, int *progress
)
789 unsigned total
, o_div
;
795 if (!isl_int_is_zero(bmap
->div
[div
][0]))
798 involves
= bmap_eq_involves_unknown_divs(bmap
, eq
, 0, div
);
800 return isl_basic_map_free(bmap
);
804 total
= isl_basic_map_dim(bmap
, isl_dim_all
);
805 o_div
= isl_basic_map_offset(bmap
, isl_dim_div
);
806 isl_seq_neg(bmap
->div
[div
] + 1, bmap
->eq
[eq
], 1 + total
);
807 isl_int_set_si(bmap
->div
[div
][1 + o_div
+ div
], 0);
808 isl_int_set(bmap
->div
[div
][0], bmap
->eq
[eq
][o_div
+ div
]);
811 ISL_F_CLR(bmap
, ISL_BASIC_MAP_NORMALIZED
);
816 struct isl_basic_map
*isl_basic_map_gauss(
817 struct isl_basic_map
*bmap
, int *progress
)
825 bmap
= isl_basic_map_order_divs(bmap
);
830 total
= isl_basic_map_total_dim(bmap
);
831 total_var
= total
- bmap
->n_div
;
833 last_var
= total
- 1;
834 for (done
= 0; done
< bmap
->n_eq
; ++done
) {
835 for (; last_var
>= 0; --last_var
) {
836 for (k
= done
; k
< bmap
->n_eq
; ++k
)
837 if (!isl_int_is_zero(bmap
->eq
[k
][1+last_var
]))
845 swap_equality(bmap
, k
, done
);
846 if (isl_int_is_neg(bmap
->eq
[done
][1+last_var
]))
847 isl_seq_neg(bmap
->eq
[done
], bmap
->eq
[done
], 1+total
);
849 eliminate_var_using_equality(bmap
, last_var
, bmap
->eq
[done
], 1,
852 if (last_var
>= total_var
)
853 bmap
= set_div_from_eq(bmap
, last_var
- total_var
,
858 if (done
== bmap
->n_eq
)
860 for (k
= done
; k
< bmap
->n_eq
; ++k
) {
861 if (isl_int_is_zero(bmap
->eq
[k
][0]))
863 return isl_basic_map_set_to_empty(bmap
);
865 isl_basic_map_free_equality(bmap
, bmap
->n_eq
-done
);
869 struct isl_basic_set
*isl_basic_set_gauss(
870 struct isl_basic_set
*bset
, int *progress
)
872 return bset_from_bmap(isl_basic_map_gauss(bset_to_bmap(bset
),
877 static unsigned int round_up(unsigned int v
)
888 /* Hash table of inequalities in a basic map.
889 * "index" is an array of addresses of inequalities in the basic map, some
890 * of which are NULL. The inequalities are hashed on the coefficients
891 * except the constant term.
892 * "size" is the number of elements in the array and is always a power of two
893 * "bits" is the number of bits need to represent an index into the array.
894 * "total" is the total dimension of the basic map.
896 struct isl_constraint_index
{
903 /* Fill in the "ci" data structure for holding the inequalities of "bmap".
905 static isl_stat
create_constraint_index(struct isl_constraint_index
*ci
,
906 __isl_keep isl_basic_map
*bmap
)
912 return isl_stat_error
;
913 ci
->total
= isl_basic_set_total_dim(bmap
);
914 if (bmap
->n_ineq
== 0)
916 ci
->size
= round_up(4 * (bmap
->n_ineq
+ 1) / 3 - 1);
917 ci
->bits
= ffs(ci
->size
) - 1;
918 ctx
= isl_basic_map_get_ctx(bmap
);
919 ci
->index
= isl_calloc_array(ctx
, isl_int
**, ci
->size
);
921 return isl_stat_error
;
926 /* Free the memory allocated by create_constraint_index.
928 static void constraint_index_free(struct isl_constraint_index
*ci
)
933 /* Return the position in ci->index that contains the address of
934 * an inequality that is equal to *ineq up to the constant term,
935 * provided this address is not identical to "ineq".
936 * If there is no such inequality, then return the position where
937 * such an inequality should be inserted.
939 static int hash_index_ineq(struct isl_constraint_index
*ci
, isl_int
**ineq
)
942 uint32_t hash
= isl_seq_get_hash_bits((*ineq
) + 1, ci
->total
, ci
->bits
);
943 for (h
= hash
; ci
->index
[h
]; h
= (h
+1) % ci
->size
)
944 if (ineq
!= ci
->index
[h
] &&
945 isl_seq_eq((*ineq
) + 1, ci
->index
[h
][0]+1, ci
->total
))
950 /* Return the position in ci->index that contains the address of
951 * an inequality that is equal to the k'th inequality of "bmap"
952 * up to the constant term, provided it does not point to the very
954 * If there is no such inequality, then return the position where
955 * such an inequality should be inserted.
957 static int hash_index(struct isl_constraint_index
*ci
,
958 __isl_keep isl_basic_map
*bmap
, int k
)
960 return hash_index_ineq(ci
, &bmap
->ineq
[k
]);
963 static int set_hash_index(struct isl_constraint_index
*ci
,
964 struct isl_basic_set
*bset
, int k
)
966 return hash_index(ci
, bset
, k
);
969 /* Fill in the "ci" data structure with the inequalities of "bset".
971 static isl_stat
setup_constraint_index(struct isl_constraint_index
*ci
,
972 __isl_keep isl_basic_set
*bset
)
976 if (create_constraint_index(ci
, bset
) < 0)
977 return isl_stat_error
;
979 for (k
= 0; k
< bset
->n_ineq
; ++k
) {
980 h
= set_hash_index(ci
, bset
, k
);
981 ci
->index
[h
] = &bset
->ineq
[k
];
987 /* Is the inequality ineq (obviously) redundant with respect
988 * to the constraints in "ci"?
990 * Look for an inequality in "ci" with the same coefficients and then
991 * check if the contant term of "ineq" is greater than or equal
992 * to the constant term of that inequality. If so, "ineq" is clearly
995 * Note that hash_index_ineq ignores a stored constraint if it has
996 * the same address as the passed inequality. It is ok to pass
997 * the address of a local variable here since it will never be
998 * the same as the address of a constraint in "ci".
1000 static isl_bool
constraint_index_is_redundant(struct isl_constraint_index
*ci
,
1005 h
= hash_index_ineq(ci
, &ineq
);
1007 return isl_bool_false
;
1008 return isl_int_ge(ineq
[0], (*ci
->index
[h
])[0]);
1011 /* If we can eliminate more than one div, then we need to make
1012 * sure we do it from last div to first div, in order not to
1013 * change the position of the other divs that still need to
1016 static struct isl_basic_map
*remove_duplicate_divs(
1017 struct isl_basic_map
*bmap
, int *progress
)
1027 struct isl_ctx
*ctx
;
1029 bmap
= isl_basic_map_order_divs(bmap
);
1030 if (!bmap
|| bmap
->n_div
<= 1)
1033 total_var
= isl_space_dim(bmap
->dim
, isl_dim_all
);
1034 total
= total_var
+ bmap
->n_div
;
1037 for (k
= bmap
->n_div
- 1; k
>= 0; --k
)
1038 if (!isl_int_is_zero(bmap
->div
[k
][0]))
1043 size
= round_up(4 * bmap
->n_div
/ 3 - 1);
1046 elim_for
= isl_calloc_array(ctx
, int, bmap
->n_div
);
1047 bits
= ffs(size
) - 1;
1048 index
= isl_calloc_array(ctx
, int, size
);
1049 if (!elim_for
|| !index
)
1051 eq
= isl_blk_alloc(ctx
, 1+total
);
1052 if (isl_blk_is_error(eq
))
1055 isl_seq_clr(eq
.data
, 1+total
);
1056 index
[isl_seq_get_hash_bits(bmap
->div
[k
], 2+total
, bits
)] = k
+ 1;
1057 for (--k
; k
>= 0; --k
) {
1060 if (isl_int_is_zero(bmap
->div
[k
][0]))
1063 hash
= isl_seq_get_hash_bits(bmap
->div
[k
], 2+total
, bits
);
1064 for (h
= hash
; index
[h
]; h
= (h
+1) % size
)
1065 if (isl_seq_eq(bmap
->div
[k
],
1066 bmap
->div
[index
[h
]-1], 2+total
))
1071 elim_for
[l
] = k
+ 1;
1075 for (l
= bmap
->n_div
- 1; l
>= 0; --l
) {
1078 k
= elim_for
[l
] - 1;
1079 isl_int_set_si(eq
.data
[1+total_var
+k
], -1);
1080 isl_int_set_si(eq
.data
[1+total_var
+l
], 1);
1081 bmap
= eliminate_div(bmap
, eq
.data
, l
, 1);
1084 isl_int_set_si(eq
.data
[1+total_var
+k
], 0);
1085 isl_int_set_si(eq
.data
[1+total_var
+l
], 0);
1088 isl_blk_free(ctx
, eq
);
1095 static int n_pure_div_eq(struct isl_basic_map
*bmap
)
1100 total
= isl_space_dim(bmap
->dim
, isl_dim_all
);
1101 for (i
= 0, j
= bmap
->n_div
-1; i
< bmap
->n_eq
; ++i
) {
1102 while (j
>= 0 && isl_int_is_zero(bmap
->eq
[i
][1 + total
+ j
]))
1106 if (isl_seq_first_non_zero(bmap
->eq
[i
] + 1 + total
, j
) != -1)
1112 /* Normalize divs that appear in equalities.
1114 * In particular, we assume that bmap contains some equalities
1119 * and we want to replace the set of e_i by a minimal set and
1120 * such that the new e_i have a canonical representation in terms
1122 * If any of the equalities involves more than one divs, then
1123 * we currently simply bail out.
1125 * Let us first additionally assume that all equalities involve
1126 * a div. The equalities then express modulo constraints on the
1127 * remaining variables and we can use "parameter compression"
1128 * to find a minimal set of constraints. The result is a transformation
1130 * x = T(x') = x_0 + G x'
1132 * with G a lower-triangular matrix with all elements below the diagonal
1133 * non-negative and smaller than the diagonal element on the same row.
1134 * We first normalize x_0 by making the same property hold in the affine
1136 * The rows i of G with a 1 on the diagonal do not impose any modulo
1137 * constraint and simply express x_i = x'_i.
1138 * For each of the remaining rows i, we introduce a div and a corresponding
1139 * equality. In particular
1141 * g_ii e_j = x_i - g_i(x')
1143 * where each x'_k is replaced either by x_k (if g_kk = 1) or the
1144 * corresponding div (if g_kk != 1).
1146 * If there are any equalities not involving any div, then we
1147 * first apply a variable compression on the variables x:
1149 * x = C x'' x'' = C_2 x
1151 * and perform the above parameter compression on A C instead of on A.
1152 * The resulting compression is then of the form
1154 * x'' = T(x') = x_0 + G x'
1156 * and in constructing the new divs and the corresponding equalities,
1157 * we have to replace each x'', i.e., the x'_k with (g_kk = 1),
1158 * by the corresponding row from C_2.
1160 static struct isl_basic_map
*normalize_divs(
1161 struct isl_basic_map
*bmap
, int *progress
)
1168 struct isl_mat
*T
= NULL
;
1169 struct isl_mat
*C
= NULL
;
1170 struct isl_mat
*C2
= NULL
;
1173 int dropped
, needed
;
1178 if (bmap
->n_div
== 0)
1181 if (bmap
->n_eq
== 0)
1184 if (ISL_F_ISSET(bmap
, ISL_BASIC_MAP_NORMALIZED_DIVS
))
1187 total
= isl_space_dim(bmap
->dim
, isl_dim_all
);
1188 div_eq
= n_pure_div_eq(bmap
);
1192 if (div_eq
< bmap
->n_eq
) {
1193 B
= isl_mat_sub_alloc6(bmap
->ctx
, bmap
->eq
, div_eq
,
1194 bmap
->n_eq
- div_eq
, 0, 1 + total
);
1195 C
= isl_mat_variable_compression(B
, &C2
);
1198 if (C
->n_col
== 0) {
1199 bmap
= isl_basic_map_set_to_empty(bmap
);
1206 d
= isl_vec_alloc(bmap
->ctx
, div_eq
);
1209 for (i
= 0, j
= bmap
->n_div
-1; i
< div_eq
; ++i
) {
1210 while (j
>= 0 && isl_int_is_zero(bmap
->eq
[i
][1 + total
+ j
]))
1212 isl_int_set(d
->block
.data
[i
], bmap
->eq
[i
][1 + total
+ j
]);
1214 B
= isl_mat_sub_alloc6(bmap
->ctx
, bmap
->eq
, 0, div_eq
, 0, 1 + total
);
1217 B
= isl_mat_product(B
, C
);
1221 T
= isl_mat_parameter_compression(B
, d
);
1224 if (T
->n_col
== 0) {
1225 bmap
= isl_basic_map_set_to_empty(bmap
);
1231 for (i
= 0; i
< T
->n_row
- 1; ++i
) {
1232 isl_int_fdiv_q(v
, T
->row
[1 + i
][0], T
->row
[1 + i
][1 + i
]);
1233 if (isl_int_is_zero(v
))
1235 isl_mat_col_submul(T
, 0, v
, 1 + i
);
1238 pos
= isl_alloc_array(bmap
->ctx
, int, T
->n_row
);
1241 /* We have to be careful because dropping equalities may reorder them */
1243 for (j
= bmap
->n_div
- 1; j
>= 0; --j
) {
1244 for (i
= 0; i
< bmap
->n_eq
; ++i
)
1245 if (!isl_int_is_zero(bmap
->eq
[i
][1 + total
+ j
]))
1247 if (i
< bmap
->n_eq
) {
1248 bmap
= isl_basic_map_drop_div(bmap
, j
);
1249 isl_basic_map_drop_equality(bmap
, i
);
1255 for (i
= 1; i
< T
->n_row
; ++i
) {
1256 if (isl_int_is_one(T
->row
[i
][i
]))
1261 if (needed
> dropped
) {
1262 bmap
= isl_basic_map_extend_space(bmap
, isl_space_copy(bmap
->dim
),
1267 for (i
= 1; i
< T
->n_row
; ++i
) {
1268 if (isl_int_is_one(T
->row
[i
][i
]))
1270 k
= isl_basic_map_alloc_div(bmap
);
1271 pos
[i
] = 1 + total
+ k
;
1272 isl_seq_clr(bmap
->div
[k
] + 1, 1 + total
+ bmap
->n_div
);
1273 isl_int_set(bmap
->div
[k
][0], T
->row
[i
][i
]);
1275 isl_seq_cpy(bmap
->div
[k
] + 1, C2
->row
[i
], 1 + total
);
1277 isl_int_set_si(bmap
->div
[k
][1 + i
], 1);
1278 for (j
= 0; j
< i
; ++j
) {
1279 if (isl_int_is_zero(T
->row
[i
][j
]))
1281 if (pos
[j
] < T
->n_row
&& C2
)
1282 isl_seq_submul(bmap
->div
[k
] + 1, T
->row
[i
][j
],
1283 C2
->row
[pos
[j
]], 1 + total
);
1285 isl_int_neg(bmap
->div
[k
][1 + pos
[j
]],
1288 j
= isl_basic_map_alloc_equality(bmap
);
1289 isl_seq_neg(bmap
->eq
[j
], bmap
->div
[k
]+1, 1+total
+bmap
->n_div
);
1290 isl_int_set(bmap
->eq
[j
][pos
[i
]], bmap
->div
[k
][0]);
1299 ISL_F_SET(bmap
, ISL_BASIC_MAP_NORMALIZED_DIVS
);
1310 static struct isl_basic_map
*set_div_from_lower_bound(
1311 struct isl_basic_map
*bmap
, int div
, int ineq
)
1313 unsigned total
= 1 + isl_space_dim(bmap
->dim
, isl_dim_all
);
1315 isl_seq_neg(bmap
->div
[div
] + 1, bmap
->ineq
[ineq
], total
+ bmap
->n_div
);
1316 isl_int_set(bmap
->div
[div
][0], bmap
->ineq
[ineq
][total
+ div
]);
1317 isl_int_add(bmap
->div
[div
][1], bmap
->div
[div
][1], bmap
->div
[div
][0]);
1318 isl_int_sub_ui(bmap
->div
[div
][1], bmap
->div
[div
][1], 1);
1319 isl_int_set_si(bmap
->div
[div
][1 + total
+ div
], 0);
1324 /* Check whether it is ok to define a div based on an inequality.
1325 * To avoid the introduction of circular definitions of divs, we
1326 * do not allow such a definition if the resulting expression would refer to
1327 * any other undefined divs or if any known div is defined in
1328 * terms of the unknown div.
1330 static int ok_to_set_div_from_bound(struct isl_basic_map
*bmap
,
1334 unsigned total
= 1 + isl_space_dim(bmap
->dim
, isl_dim_all
);
1336 /* Not defined in terms of unknown divs */
1337 for (j
= 0; j
< bmap
->n_div
; ++j
) {
1340 if (isl_int_is_zero(bmap
->ineq
[ineq
][total
+ j
]))
1342 if (isl_int_is_zero(bmap
->div
[j
][0]))
1346 /* No other div defined in terms of this one => avoid loops */
1347 for (j
= 0; j
< bmap
->n_div
; ++j
) {
1350 if (isl_int_is_zero(bmap
->div
[j
][0]))
1352 if (!isl_int_is_zero(bmap
->div
[j
][1 + total
+ div
]))
1359 /* Would an expression for div "div" based on inequality "ineq" of "bmap"
1360 * be a better expression than the current one?
1362 * If we do not have any expression yet, then any expression would be better.
1363 * Otherwise we check if the last variable involved in the inequality
1364 * (disregarding the div that it would define) is in an earlier position
1365 * than the last variable involved in the current div expression.
1367 static int better_div_constraint(__isl_keep isl_basic_map
*bmap
,
1370 unsigned total
= 1 + isl_space_dim(bmap
->dim
, isl_dim_all
);
1374 if (isl_int_is_zero(bmap
->div
[div
][0]))
1377 if (isl_seq_last_non_zero(bmap
->ineq
[ineq
] + total
+ div
+ 1,
1378 bmap
->n_div
- (div
+ 1)) >= 0)
1381 last_ineq
= isl_seq_last_non_zero(bmap
->ineq
[ineq
], total
+ div
);
1382 last_div
= isl_seq_last_non_zero(bmap
->div
[div
] + 1,
1383 total
+ bmap
->n_div
);
1385 return last_ineq
< last_div
;
1388 /* Given two constraints "k" and "l" that are opposite to each other,
1389 * except for the constant term, check if we can use them
1390 * to obtain an expression for one of the hitherto unknown divs or
1391 * a "better" expression for a div for which we already have an expression.
1392 * "sum" is the sum of the constant terms of the constraints.
1393 * If this sum is strictly smaller than the coefficient of one
1394 * of the divs, then this pair can be used define the div.
1395 * To avoid the introduction of circular definitions of divs, we
1396 * do not use the pair if the resulting expression would refer to
1397 * any other undefined divs or if any known div is defined in
1398 * terms of the unknown div.
1400 static struct isl_basic_map
*check_for_div_constraints(
1401 struct isl_basic_map
*bmap
, int k
, int l
, isl_int sum
, int *progress
)
1404 unsigned total
= 1 + isl_space_dim(bmap
->dim
, isl_dim_all
);
1406 for (i
= 0; i
< bmap
->n_div
; ++i
) {
1407 if (isl_int_is_zero(bmap
->ineq
[k
][total
+ i
]))
1409 if (isl_int_abs_ge(sum
, bmap
->ineq
[k
][total
+ i
]))
1411 if (!better_div_constraint(bmap
, i
, k
))
1413 if (!ok_to_set_div_from_bound(bmap
, i
, k
))
1415 if (isl_int_is_pos(bmap
->ineq
[k
][total
+ i
]))
1416 bmap
= set_div_from_lower_bound(bmap
, i
, k
);
1418 bmap
= set_div_from_lower_bound(bmap
, i
, l
);
1426 __isl_give isl_basic_map
*isl_basic_map_remove_duplicate_constraints(
1427 __isl_take isl_basic_map
*bmap
, int *progress
, int detect_divs
)
1429 struct isl_constraint_index ci
;
1431 unsigned total
= isl_basic_map_total_dim(bmap
);
1434 if (!bmap
|| bmap
->n_ineq
<= 1)
1437 if (create_constraint_index(&ci
, bmap
) < 0)
1440 h
= isl_seq_get_hash_bits(bmap
->ineq
[0] + 1, total
, ci
.bits
);
1441 ci
.index
[h
] = &bmap
->ineq
[0];
1442 for (k
= 1; k
< bmap
->n_ineq
; ++k
) {
1443 h
= hash_index(&ci
, bmap
, k
);
1445 ci
.index
[h
] = &bmap
->ineq
[k
];
1450 l
= ci
.index
[h
] - &bmap
->ineq
[0];
1451 if (isl_int_lt(bmap
->ineq
[k
][0], bmap
->ineq
[l
][0]))
1452 swap_inequality(bmap
, k
, l
);
1453 isl_basic_map_drop_inequality(bmap
, k
);
1457 for (k
= 0; k
< bmap
->n_ineq
-1; ++k
) {
1458 isl_seq_neg(bmap
->ineq
[k
]+1, bmap
->ineq
[k
]+1, total
);
1459 h
= hash_index(&ci
, bmap
, k
);
1460 isl_seq_neg(bmap
->ineq
[k
]+1, bmap
->ineq
[k
]+1, total
);
1463 l
= ci
.index
[h
] - &bmap
->ineq
[0];
1464 isl_int_add(sum
, bmap
->ineq
[k
][0], bmap
->ineq
[l
][0]);
1465 if (isl_int_is_pos(sum
)) {
1467 bmap
= check_for_div_constraints(bmap
, k
, l
,
1471 if (isl_int_is_zero(sum
)) {
1472 /* We need to break out of the loop after these
1473 * changes since the contents of the hash
1474 * will no longer be valid.
1475 * Plus, we probably we want to regauss first.
1479 isl_basic_map_drop_inequality(bmap
, l
);
1480 isl_basic_map_inequality_to_equality(bmap
, k
);
1482 bmap
= isl_basic_map_set_to_empty(bmap
);
1487 constraint_index_free(&ci
);
1491 /* Detect all pairs of inequalities that form an equality.
1493 * isl_basic_map_remove_duplicate_constraints detects at most one such pair.
1494 * Call it repeatedly while it is making progress.
1496 __isl_give isl_basic_map
*isl_basic_map_detect_inequality_pairs(
1497 __isl_take isl_basic_map
*bmap
, int *progress
)
1503 bmap
= isl_basic_map_remove_duplicate_constraints(bmap
,
1505 if (progress
&& duplicate
)
1507 } while (duplicate
);
1512 /* Eliminate knowns divs from constraints where they appear with
1513 * a (positive or negative) unit coefficient.
1517 * floor(e/m) + f >= 0
1525 * -floor(e/m) + f >= 0
1529 * -e + m f + m - 1 >= 0
1531 * The first conversion is valid because floor(e/m) >= -f is equivalent
1532 * to e/m >= -f because -f is an integral expression.
1533 * The second conversion follows from the fact that
1535 * -floor(e/m) = ceil(-e/m) = floor((-e + m - 1)/m)
1538 * Note that one of the div constraints may have been eliminated
1539 * due to being redundant with respect to the constraint that is
1540 * being modified by this function. The modified constraint may
1541 * no longer imply this div constraint, so we add it back to make
1542 * sure we do not lose any information.
1544 * We skip integral divs, i.e., those with denominator 1, as we would
1545 * risk eliminating the div from the div constraints. We do not need
1546 * to handle those divs here anyway since the div constraints will turn
1547 * out to form an equality and this equality can then be used to eliminate
1548 * the div from all constraints.
1550 static __isl_give isl_basic_map
*eliminate_unit_divs(
1551 __isl_take isl_basic_map
*bmap
, int *progress
)
1560 ctx
= isl_basic_map_get_ctx(bmap
);
1561 total
= 1 + isl_space_dim(bmap
->dim
, isl_dim_all
);
1563 for (i
= 0; i
< bmap
->n_div
; ++i
) {
1564 if (isl_int_is_zero(bmap
->div
[i
][0]))
1566 if (isl_int_is_one(bmap
->div
[i
][0]))
1568 for (j
= 0; j
< bmap
->n_ineq
; ++j
) {
1571 if (!isl_int_is_one(bmap
->ineq
[j
][total
+ i
]) &&
1572 !isl_int_is_negone(bmap
->ineq
[j
][total
+ i
]))
1577 s
= isl_int_sgn(bmap
->ineq
[j
][total
+ i
]);
1578 isl_int_set_si(bmap
->ineq
[j
][total
+ i
], 0);
1580 isl_seq_combine(bmap
->ineq
[j
],
1581 ctx
->negone
, bmap
->div
[i
] + 1,
1582 bmap
->div
[i
][0], bmap
->ineq
[j
],
1583 total
+ bmap
->n_div
);
1585 isl_seq_combine(bmap
->ineq
[j
],
1586 ctx
->one
, bmap
->div
[i
] + 1,
1587 bmap
->div
[i
][0], bmap
->ineq
[j
],
1588 total
+ bmap
->n_div
);
1590 isl_int_add(bmap
->ineq
[j
][0],
1591 bmap
->ineq
[j
][0], bmap
->div
[i
][0]);
1592 isl_int_sub_ui(bmap
->ineq
[j
][0],
1593 bmap
->ineq
[j
][0], 1);
1596 bmap
= isl_basic_map_extend_constraints(bmap
, 0, 1);
1597 if (isl_basic_map_add_div_constraint(bmap
, i
, s
) < 0)
1598 return isl_basic_map_free(bmap
);
1605 struct isl_basic_map
*isl_basic_map_simplify(struct isl_basic_map
*bmap
)
1614 empty
= isl_basic_map_plain_is_empty(bmap
);
1616 return isl_basic_map_free(bmap
);
1619 bmap
= isl_basic_map_normalize_constraints(bmap
);
1620 bmap
= reduce_div_coefficients(bmap
);
1621 bmap
= normalize_div_expressions(bmap
);
1622 bmap
= remove_duplicate_divs(bmap
, &progress
);
1623 bmap
= eliminate_unit_divs(bmap
, &progress
);
1624 bmap
= eliminate_divs_eq(bmap
, &progress
);
1625 bmap
= eliminate_divs_ineq(bmap
, &progress
);
1626 bmap
= isl_basic_map_gauss(bmap
, &progress
);
1627 /* requires equalities in normal form */
1628 bmap
= normalize_divs(bmap
, &progress
);
1629 bmap
= isl_basic_map_remove_duplicate_constraints(bmap
,
1631 if (bmap
&& progress
)
1632 ISL_F_CLR(bmap
, ISL_BASIC_MAP_REDUCED_COEFFICIENTS
);
1637 struct isl_basic_set
*isl_basic_set_simplify(struct isl_basic_set
*bset
)
1639 return bset_from_bmap(isl_basic_map_simplify(bset_to_bmap(bset
)));
1643 int isl_basic_map_is_div_constraint(__isl_keep isl_basic_map
*bmap
,
1644 isl_int
*constraint
, unsigned div
)
1651 pos
= 1 + isl_space_dim(bmap
->dim
, isl_dim_all
) + div
;
1653 if (isl_int_eq(constraint
[pos
], bmap
->div
[div
][0])) {
1655 isl_int_sub(bmap
->div
[div
][1],
1656 bmap
->div
[div
][1], bmap
->div
[div
][0]);
1657 isl_int_add_ui(bmap
->div
[div
][1], bmap
->div
[div
][1], 1);
1658 neg
= isl_seq_is_neg(constraint
, bmap
->div
[div
]+1, pos
);
1659 isl_int_sub_ui(bmap
->div
[div
][1], bmap
->div
[div
][1], 1);
1660 isl_int_add(bmap
->div
[div
][1],
1661 bmap
->div
[div
][1], bmap
->div
[div
][0]);
1664 if (isl_seq_first_non_zero(constraint
+pos
+1,
1665 bmap
->n_div
-div
-1) != -1)
1667 } else if (isl_int_abs_eq(constraint
[pos
], bmap
->div
[div
][0])) {
1668 if (!isl_seq_eq(constraint
, bmap
->div
[div
]+1, pos
))
1670 if (isl_seq_first_non_zero(constraint
+pos
+1,
1671 bmap
->n_div
-div
-1) != -1)
1679 int isl_basic_set_is_div_constraint(__isl_keep isl_basic_set
*bset
,
1680 isl_int
*constraint
, unsigned div
)
1682 return isl_basic_map_is_div_constraint(bset
, constraint
, div
);
1686 /* If the only constraints a div d=floor(f/m)
1687 * appears in are its two defining constraints
1690 * -(f - (m - 1)) + m d >= 0
1692 * then it can safely be removed.
1694 static int div_is_redundant(struct isl_basic_map
*bmap
, int div
)
1697 unsigned pos
= 1 + isl_space_dim(bmap
->dim
, isl_dim_all
) + div
;
1699 for (i
= 0; i
< bmap
->n_eq
; ++i
)
1700 if (!isl_int_is_zero(bmap
->eq
[i
][pos
]))
1703 for (i
= 0; i
< bmap
->n_ineq
; ++i
) {
1704 if (isl_int_is_zero(bmap
->ineq
[i
][pos
]))
1706 if (!isl_basic_map_is_div_constraint(bmap
, bmap
->ineq
[i
], div
))
1710 for (i
= 0; i
< bmap
->n_div
; ++i
) {
1711 if (isl_int_is_zero(bmap
->div
[i
][0]))
1713 if (!isl_int_is_zero(bmap
->div
[i
][1+pos
]))
1721 * Remove divs that don't occur in any of the constraints or other divs.
1722 * These can arise when dropping constraints from a basic map or
1723 * when the divs of a basic map have been temporarily aligned
1724 * with the divs of another basic map.
1726 static struct isl_basic_map
*remove_redundant_divs(struct isl_basic_map
*bmap
)
1733 for (i
= bmap
->n_div
-1; i
>= 0; --i
) {
1734 if (!div_is_redundant(bmap
, i
))
1736 bmap
= isl_basic_map_drop_div(bmap
, i
);
1741 /* Mark "bmap" as final, without checking for obviously redundant
1742 * integer divisions. This function should be used when "bmap"
1743 * is known not to involve any such integer divisions.
1745 __isl_give isl_basic_map
*isl_basic_map_mark_final(
1746 __isl_take isl_basic_map
*bmap
)
1750 ISL_F_SET(bmap
, ISL_BASIC_SET_FINAL
);
1754 /* Mark "bmap" as final, after removing obviously redundant integer divisions.
1756 struct isl_basic_map
*isl_basic_map_finalize(struct isl_basic_map
*bmap
)
1758 bmap
= remove_redundant_divs(bmap
);
1759 bmap
= isl_basic_map_mark_final(bmap
);
1763 struct isl_basic_set
*isl_basic_set_finalize(struct isl_basic_set
*bset
)
1765 return bset_from_bmap(isl_basic_map_finalize(bset_to_bmap(bset
)));
1768 struct isl_set
*isl_set_finalize(struct isl_set
*set
)
1774 for (i
= 0; i
< set
->n
; ++i
) {
1775 set
->p
[i
] = isl_basic_set_finalize(set
->p
[i
]);
1785 struct isl_map
*isl_map_finalize(struct isl_map
*map
)
1791 for (i
= 0; i
< map
->n
; ++i
) {
1792 map
->p
[i
] = isl_basic_map_finalize(map
->p
[i
]);
1796 ISL_F_CLR(map
, ISL_MAP_NORMALIZED
);
1804 /* Remove definition of any div that is defined in terms of the given variable.
1805 * The div itself is not removed. Functions such as
1806 * eliminate_divs_ineq depend on the other divs remaining in place.
1808 static struct isl_basic_map
*remove_dependent_vars(struct isl_basic_map
*bmap
,
1816 for (i
= 0; i
< bmap
->n_div
; ++i
) {
1817 if (isl_int_is_zero(bmap
->div
[i
][0]))
1819 if (isl_int_is_zero(bmap
->div
[i
][1+1+pos
]))
1821 bmap
= isl_basic_map_mark_div_unknown(bmap
, i
);
1828 /* Eliminate the specified variables from the constraints using
1829 * Fourier-Motzkin. The variables themselves are not removed.
1831 struct isl_basic_map
*isl_basic_map_eliminate_vars(
1832 struct isl_basic_map
*bmap
, unsigned pos
, unsigned n
)
1843 total
= isl_basic_map_total_dim(bmap
);
1845 bmap
= isl_basic_map_cow(bmap
);
1846 for (d
= pos
+ n
- 1; d
>= 0 && d
>= pos
; --d
)
1847 bmap
= remove_dependent_vars(bmap
, d
);
1851 for (d
= pos
+ n
- 1;
1852 d
>= 0 && d
>= total
- bmap
->n_div
&& d
>= pos
; --d
)
1853 isl_seq_clr(bmap
->div
[d
-(total
-bmap
->n_div
)], 2+total
);
1854 for (d
= pos
+ n
- 1; d
>= 0 && d
>= pos
; --d
) {
1855 int n_lower
, n_upper
;
1858 for (i
= 0; i
< bmap
->n_eq
; ++i
) {
1859 if (isl_int_is_zero(bmap
->eq
[i
][1+d
]))
1861 eliminate_var_using_equality(bmap
, d
, bmap
->eq
[i
], 0, NULL
);
1862 isl_basic_map_drop_equality(bmap
, i
);
1870 for (i
= 0; i
< bmap
->n_ineq
; ++i
) {
1871 if (isl_int_is_pos(bmap
->ineq
[i
][1+d
]))
1873 else if (isl_int_is_neg(bmap
->ineq
[i
][1+d
]))
1876 bmap
= isl_basic_map_extend_constraints(bmap
,
1877 0, n_lower
* n_upper
);
1880 for (i
= bmap
->n_ineq
- 1; i
>= 0; --i
) {
1882 if (isl_int_is_zero(bmap
->ineq
[i
][1+d
]))
1885 for (j
= 0; j
< i
; ++j
) {
1886 if (isl_int_is_zero(bmap
->ineq
[j
][1+d
]))
1889 if (isl_int_sgn(bmap
->ineq
[i
][1+d
]) ==
1890 isl_int_sgn(bmap
->ineq
[j
][1+d
]))
1892 k
= isl_basic_map_alloc_inequality(bmap
);
1895 isl_seq_cpy(bmap
->ineq
[k
], bmap
->ineq
[i
],
1897 isl_seq_elim(bmap
->ineq
[k
], bmap
->ineq
[j
],
1898 1+d
, 1+total
, NULL
);
1900 isl_basic_map_drop_inequality(bmap
, i
);
1903 if (n_lower
> 0 && n_upper
> 0) {
1904 bmap
= isl_basic_map_normalize_constraints(bmap
);
1905 bmap
= isl_basic_map_remove_duplicate_constraints(bmap
,
1907 bmap
= isl_basic_map_gauss(bmap
, NULL
);
1908 bmap
= isl_basic_map_remove_redundancies(bmap
);
1912 if (ISL_F_ISSET(bmap
, ISL_BASIC_MAP_EMPTY
))
1916 ISL_F_CLR(bmap
, ISL_BASIC_MAP_NORMALIZED
);
1918 bmap
= isl_basic_map_gauss(bmap
, NULL
);
1921 isl_basic_map_free(bmap
);
1925 struct isl_basic_set
*isl_basic_set_eliminate_vars(
1926 struct isl_basic_set
*bset
, unsigned pos
, unsigned n
)
1928 return bset_from_bmap(isl_basic_map_eliminate_vars(bset_to_bmap(bset
),
1932 /* Eliminate the specified n dimensions starting at first from the
1933 * constraints, without removing the dimensions from the space.
1934 * If the set is rational, the dimensions are eliminated using Fourier-Motzkin.
1935 * Otherwise, they are projected out and the original space is restored.
1937 __isl_give isl_basic_map
*isl_basic_map_eliminate(
1938 __isl_take isl_basic_map
*bmap
,
1939 enum isl_dim_type type
, unsigned first
, unsigned n
)
1948 if (first
+ n
> isl_basic_map_dim(bmap
, type
) || first
+ n
< first
)
1949 isl_die(bmap
->ctx
, isl_error_invalid
,
1950 "index out of bounds", goto error
);
1952 if (ISL_F_ISSET(bmap
, ISL_BASIC_MAP_RATIONAL
)) {
1953 first
+= isl_basic_map_offset(bmap
, type
) - 1;
1954 bmap
= isl_basic_map_eliminate_vars(bmap
, first
, n
);
1955 return isl_basic_map_finalize(bmap
);
1958 space
= isl_basic_map_get_space(bmap
);
1959 bmap
= isl_basic_map_project_out(bmap
, type
, first
, n
);
1960 bmap
= isl_basic_map_insert_dims(bmap
, type
, first
, n
);
1961 bmap
= isl_basic_map_reset_space(bmap
, space
);
1964 isl_basic_map_free(bmap
);
1968 __isl_give isl_basic_set
*isl_basic_set_eliminate(
1969 __isl_take isl_basic_set
*bset
,
1970 enum isl_dim_type type
, unsigned first
, unsigned n
)
1972 return isl_basic_map_eliminate(bset
, type
, first
, n
);
1975 /* Remove all constraints from "bmap" that reference any unknown local
1976 * variables (directly or indirectly).
1978 * Dropping all constraints on a local variable will make it redundant,
1979 * so it will get removed implicitly by
1980 * isl_basic_map_drop_constraints_involving_dims. Some other local
1981 * variables may also end up becoming redundant if they only appear
1982 * in constraints together with the unknown local variable.
1983 * Therefore, start over after calling
1984 * isl_basic_map_drop_constraints_involving_dims.
1986 __isl_give isl_basic_map
*isl_basic_map_drop_constraint_involving_unknown_divs(
1987 __isl_take isl_basic_map
*bmap
)
1990 int i
, n_div
, o_div
;
1992 known
= isl_basic_map_divs_known(bmap
);
1994 return isl_basic_map_free(bmap
);
1998 n_div
= isl_basic_map_dim(bmap
, isl_dim_div
);
1999 o_div
= isl_basic_map_offset(bmap
, isl_dim_div
) - 1;
2001 for (i
= 0; i
< n_div
; ++i
) {
2002 known
= isl_basic_map_div_is_known(bmap
, i
);
2004 return isl_basic_map_free(bmap
);
2007 bmap
= remove_dependent_vars(bmap
, o_div
+ i
);
2008 bmap
= isl_basic_map_drop_constraints_involving_dims(bmap
,
2012 n_div
= isl_basic_map_dim(bmap
, isl_dim_div
);
2019 /* Remove all constraints from "map" that reference any unknown local
2020 * variables (directly or indirectly).
2022 * Since constraints may get dropped from the basic maps,
2023 * they may no longer be disjoint from each other.
2025 __isl_give isl_map
*isl_map_drop_constraint_involving_unknown_divs(
2026 __isl_take isl_map
*map
)
2031 known
= isl_map_divs_known(map
);
2033 return isl_map_free(map
);
2037 map
= isl_map_cow(map
);
2041 for (i
= 0; i
< map
->n
; ++i
) {
2043 isl_basic_map_drop_constraint_involving_unknown_divs(
2046 return isl_map_free(map
);
2050 ISL_F_CLR(map
, ISL_MAP_DISJOINT
);
2055 /* Don't assume equalities are in order, because align_divs
2056 * may have changed the order of the divs.
2058 static void compute_elimination_index(struct isl_basic_map
*bmap
, int *elim
)
2063 total
= isl_space_dim(bmap
->dim
, isl_dim_all
);
2064 for (d
= 0; d
< total
; ++d
)
2066 for (i
= 0; i
< bmap
->n_eq
; ++i
) {
2067 for (d
= total
- 1; d
>= 0; --d
) {
2068 if (isl_int_is_zero(bmap
->eq
[i
][1+d
]))
2076 static void set_compute_elimination_index(struct isl_basic_set
*bset
, int *elim
)
2078 compute_elimination_index(bset_to_bmap(bset
), elim
);
2081 static int reduced_using_equalities(isl_int
*dst
, isl_int
*src
,
2082 struct isl_basic_map
*bmap
, int *elim
)
2088 total
= isl_space_dim(bmap
->dim
, isl_dim_all
);
2089 for (d
= total
- 1; d
>= 0; --d
) {
2090 if (isl_int_is_zero(src
[1+d
]))
2095 isl_seq_cpy(dst
, src
, 1 + total
);
2098 isl_seq_elim(dst
, bmap
->eq
[elim
[d
]], 1 + d
, 1 + total
, NULL
);
2103 static int set_reduced_using_equalities(isl_int
*dst
, isl_int
*src
,
2104 struct isl_basic_set
*bset
, int *elim
)
2106 return reduced_using_equalities(dst
, src
,
2107 bset_to_bmap(bset
), elim
);
2110 static struct isl_basic_set
*isl_basic_set_reduce_using_equalities(
2111 struct isl_basic_set
*bset
, struct isl_basic_set
*context
)
2116 if (!bset
|| !context
)
2119 if (context
->n_eq
== 0) {
2120 isl_basic_set_free(context
);
2124 bset
= isl_basic_set_cow(bset
);
2128 elim
= isl_alloc_array(bset
->ctx
, int, isl_basic_set_n_dim(bset
));
2131 set_compute_elimination_index(context
, elim
);
2132 for (i
= 0; i
< bset
->n_eq
; ++i
)
2133 set_reduced_using_equalities(bset
->eq
[i
], bset
->eq
[i
],
2135 for (i
= 0; i
< bset
->n_ineq
; ++i
)
2136 set_reduced_using_equalities(bset
->ineq
[i
], bset
->ineq
[i
],
2138 isl_basic_set_free(context
);
2140 bset
= isl_basic_set_simplify(bset
);
2141 bset
= isl_basic_set_finalize(bset
);
2144 isl_basic_set_free(bset
);
2145 isl_basic_set_free(context
);
2149 /* For each inequality in "ineq" that is a shifted (more relaxed)
2150 * copy of an inequality in "context", mark the corresponding entry
2152 * If an inequality only has a non-negative constant term, then
2155 static isl_stat
mark_shifted_constraints(__isl_keep isl_mat
*ineq
,
2156 __isl_keep isl_basic_set
*context
, int *row
)
2158 struct isl_constraint_index ci
;
2163 if (!ineq
|| !context
)
2164 return isl_stat_error
;
2165 if (context
->n_ineq
== 0)
2167 if (setup_constraint_index(&ci
, context
) < 0)
2168 return isl_stat_error
;
2170 n_ineq
= isl_mat_rows(ineq
);
2171 total
= isl_mat_cols(ineq
) - 1;
2172 for (k
= 0; k
< n_ineq
; ++k
) {
2176 l
= isl_seq_first_non_zero(ineq
->row
[k
] + 1, total
);
2177 if (l
< 0 && isl_int_is_nonneg(ineq
->row
[k
][0])) {
2181 redundant
= constraint_index_is_redundant(&ci
, ineq
->row
[k
]);
2188 constraint_index_free(&ci
);
2191 constraint_index_free(&ci
);
2192 return isl_stat_error
;
2195 static struct isl_basic_set
*remove_shifted_constraints(
2196 struct isl_basic_set
*bset
, struct isl_basic_set
*context
)
2198 struct isl_constraint_index ci
;
2201 if (!bset
|| !context
)
2204 if (context
->n_ineq
== 0)
2206 if (setup_constraint_index(&ci
, context
) < 0)
2209 for (k
= 0; k
< bset
->n_ineq
; ++k
) {
2212 redundant
= constraint_index_is_redundant(&ci
, bset
->ineq
[k
]);
2217 bset
= isl_basic_set_cow(bset
);
2220 isl_basic_set_drop_inequality(bset
, k
);
2223 constraint_index_free(&ci
);
2226 constraint_index_free(&ci
);
2230 /* Remove constraints from "bmap" that are identical to constraints
2231 * in "context" or that are more relaxed (greater constant term).
2233 * We perform the test for shifted copies on the pure constraints
2234 * in remove_shifted_constraints.
2236 static __isl_give isl_basic_map
*isl_basic_map_remove_shifted_constraints(
2237 __isl_take isl_basic_map
*bmap
, __isl_take isl_basic_map
*context
)
2239 isl_basic_set
*bset
, *bset_context
;
2241 if (!bmap
|| !context
)
2244 if (bmap
->n_ineq
== 0 || context
->n_ineq
== 0) {
2245 isl_basic_map_free(context
);
2249 context
= isl_basic_map_align_divs(context
, bmap
);
2250 bmap
= isl_basic_map_align_divs(bmap
, context
);
2252 bset
= isl_basic_map_underlying_set(isl_basic_map_copy(bmap
));
2253 bset_context
= isl_basic_map_underlying_set(context
);
2254 bset
= remove_shifted_constraints(bset
, bset_context
);
2255 isl_basic_set_free(bset_context
);
2257 bmap
= isl_basic_map_overlying_set(bset
, bmap
);
2261 isl_basic_map_free(bmap
);
2262 isl_basic_map_free(context
);
2266 /* Does the (linear part of a) constraint "c" involve any of the "len"
2267 * "relevant" dimensions?
2269 static int is_related(isl_int
*c
, int len
, int *relevant
)
2273 for (i
= 0; i
< len
; ++i
) {
2276 if (!isl_int_is_zero(c
[i
]))
2283 /* Drop constraints from "bmap" that do not involve any of
2284 * the dimensions marked "relevant".
2286 static __isl_give isl_basic_map
*drop_unrelated_constraints(
2287 __isl_take isl_basic_map
*bmap
, int *relevant
)
2291 dim
= isl_basic_map_dim(bmap
, isl_dim_all
);
2292 for (i
= 0; i
< dim
; ++i
)
2298 for (i
= bmap
->n_eq
- 1; i
>= 0; --i
)
2299 if (!is_related(bmap
->eq
[i
] + 1, dim
, relevant
)) {
2300 bmap
= isl_basic_map_cow(bmap
);
2301 if (isl_basic_map_drop_equality(bmap
, i
) < 0)
2302 return isl_basic_map_free(bmap
);
2305 for (i
= bmap
->n_ineq
- 1; i
>= 0; --i
)
2306 if (!is_related(bmap
->ineq
[i
] + 1, dim
, relevant
)) {
2307 bmap
= isl_basic_map_cow(bmap
);
2308 if (isl_basic_map_drop_inequality(bmap
, i
) < 0)
2309 return isl_basic_map_free(bmap
);
2315 /* Update the groups in "group" based on the (linear part of a) constraint "c".
2317 * In particular, for any variable involved in the constraint,
2318 * find the actual group id from before and replace the group
2319 * of the corresponding variable by the minimal group of all
2320 * the variables involved in the constraint considered so far
2321 * (if this minimum is smaller) or replace the minimum by this group
2322 * (if the minimum is larger).
2324 * At the end, all the variables in "c" will (indirectly) point
2325 * to the minimal of the groups that they referred to originally.
2327 static void update_groups(int dim
, int *group
, isl_int
*c
)
2332 for (j
= 0; j
< dim
; ++j
) {
2333 if (isl_int_is_zero(c
[j
]))
2335 while (group
[j
] >= 0 && group
[group
[j
]] != group
[j
])
2336 group
[j
] = group
[group
[j
]];
2337 if (group
[j
] == min
)
2339 if (group
[j
] < min
) {
2340 if (min
>= 0 && min
< dim
)
2341 group
[min
] = group
[j
];
2344 group
[group
[j
]] = min
;
2348 /* Allocate an array of groups of variables, one for each variable
2349 * in "context", initialized to zero.
2351 static int *alloc_groups(__isl_keep isl_basic_set
*context
)
2356 dim
= isl_basic_set_dim(context
, isl_dim_set
);
2357 ctx
= isl_basic_set_get_ctx(context
);
2358 return isl_calloc_array(ctx
, int, dim
);
2361 /* Drop constraints from "bmap" that only involve variables that are
2362 * not related to any of the variables marked with a "-1" in "group".
2364 * We construct groups of variables that collect variables that
2365 * (indirectly) appear in some common constraint of "bmap".
2366 * Each group is identified by the first variable in the group,
2367 * except for the special group of variables that was already identified
2368 * in the input as -1 (or are related to those variables).
2369 * If group[i] is equal to i (or -1), then the group of i is i (or -1),
2370 * otherwise the group of i is the group of group[i].
2372 * We first initialize groups for the remaining variables.
2373 * Then we iterate over the constraints of "bmap" and update the
2374 * group of the variables in the constraint by the smallest group.
2375 * Finally, we resolve indirect references to groups by running over
2378 * After computing the groups, we drop constraints that do not involve
2379 * any variables in the -1 group.
2381 __isl_give isl_basic_map
*isl_basic_map_drop_unrelated_constraints(
2382 __isl_take isl_basic_map
*bmap
, __isl_take
int *group
)
2391 dim
= isl_basic_map_dim(bmap
, isl_dim_all
);
2394 for (i
= 0; i
< dim
; ++i
)
2396 last
= group
[i
] = i
;
2402 for (i
= 0; i
< bmap
->n_eq
; ++i
)
2403 update_groups(dim
, group
, bmap
->eq
[i
] + 1);
2404 for (i
= 0; i
< bmap
->n_ineq
; ++i
)
2405 update_groups(dim
, group
, bmap
->ineq
[i
] + 1);
2407 for (i
= 0; i
< dim
; ++i
)
2409 group
[i
] = group
[group
[i
]];
2411 for (i
= 0; i
< dim
; ++i
)
2412 group
[i
] = group
[i
] == -1;
2414 bmap
= drop_unrelated_constraints(bmap
, group
);
2420 /* Drop constraints from "context" that are irrelevant for computing
2421 * the gist of "bset".
2423 * In particular, drop constraints in variables that are not related
2424 * to any of the variables involved in the constraints of "bset"
2425 * in the sense that there is no sequence of constraints that connects them.
2427 * We first mark all variables that appear in "bset" as belonging
2428 * to a "-1" group and then continue with group_and_drop_irrelevant_constraints.
2430 static __isl_give isl_basic_set
*drop_irrelevant_constraints(
2431 __isl_take isl_basic_set
*context
, __isl_keep isl_basic_set
*bset
)
2437 if (!context
|| !bset
)
2438 return isl_basic_set_free(context
);
2440 group
= alloc_groups(context
);
2443 return isl_basic_set_free(context
);
2445 dim
= isl_basic_set_dim(bset
, isl_dim_set
);
2446 for (i
= 0; i
< dim
; ++i
) {
2447 for (j
= 0; j
< bset
->n_eq
; ++j
)
2448 if (!isl_int_is_zero(bset
->eq
[j
][1 + i
]))
2450 if (j
< bset
->n_eq
) {
2454 for (j
= 0; j
< bset
->n_ineq
; ++j
)
2455 if (!isl_int_is_zero(bset
->ineq
[j
][1 + i
]))
2457 if (j
< bset
->n_ineq
)
2461 return isl_basic_map_drop_unrelated_constraints(context
, group
);
2464 /* Drop constraints from "context" that are irrelevant for computing
2465 * the gist of the inequalities "ineq".
2466 * Inequalities in "ineq" for which the corresponding element of row
2467 * is set to -1 have already been marked for removal and should be ignored.
2469 * In particular, drop constraints in variables that are not related
2470 * to any of the variables involved in "ineq"
2471 * in the sense that there is no sequence of constraints that connects them.
2473 * We first mark all variables that appear in "bset" as belonging
2474 * to a "-1" group and then continue with group_and_drop_irrelevant_constraints.
2476 static __isl_give isl_basic_set
*drop_irrelevant_constraints_marked(
2477 __isl_take isl_basic_set
*context
, __isl_keep isl_mat
*ineq
, int *row
)
2483 if (!context
|| !ineq
)
2484 return isl_basic_set_free(context
);
2486 group
= alloc_groups(context
);
2489 return isl_basic_set_free(context
);
2491 dim
= isl_basic_set_dim(context
, isl_dim_set
);
2492 n
= isl_mat_rows(ineq
);
2493 for (i
= 0; i
< dim
; ++i
) {
2494 for (j
= 0; j
< n
; ++j
) {
2497 if (!isl_int_is_zero(ineq
->row
[j
][1 + i
]))
2504 return isl_basic_map_drop_unrelated_constraints(context
, group
);
2507 /* Do all "n" entries of "row" contain a negative value?
2509 static int all_neg(int *row
, int n
)
2513 for (i
= 0; i
< n
; ++i
)
2520 /* Update the inequalities in "bset" based on the information in "row"
2523 * In particular, the array "row" contains either -1, meaning that
2524 * the corresponding inequality of "bset" is redundant, or the index
2525 * of an inequality in "tab".
2527 * If the row entry is -1, then drop the inequality.
2528 * Otherwise, if the constraint is marked redundant in the tableau,
2529 * then drop the inequality. Similarly, if it is marked as an equality
2530 * in the tableau, then turn the inequality into an equality and
2531 * perform Gaussian elimination.
2533 static __isl_give isl_basic_set
*update_ineq(__isl_take isl_basic_set
*bset
,
2534 __isl_keep
int *row
, struct isl_tab
*tab
)
2539 int found_equality
= 0;
2543 if (tab
&& tab
->empty
)
2544 return isl_basic_set_set_to_empty(bset
);
2546 n_ineq
= bset
->n_ineq
;
2547 for (i
= n_ineq
- 1; i
>= 0; --i
) {
2549 if (isl_basic_set_drop_inequality(bset
, i
) < 0)
2550 return isl_basic_set_free(bset
);
2556 if (isl_tab_is_equality(tab
, n_eq
+ row
[i
])) {
2557 isl_basic_map_inequality_to_equality(bset
, i
);
2559 } else if (isl_tab_is_redundant(tab
, n_eq
+ row
[i
])) {
2560 if (isl_basic_set_drop_inequality(bset
, i
) < 0)
2561 return isl_basic_set_free(bset
);
2566 bset
= isl_basic_set_gauss(bset
, NULL
);
2567 bset
= isl_basic_set_finalize(bset
);
2571 /* Update the inequalities in "bset" based on the information in "row"
2572 * and "tab" and free all arguments (other than "bset").
2574 static __isl_give isl_basic_set
*update_ineq_free(
2575 __isl_take isl_basic_set
*bset
, __isl_take isl_mat
*ineq
,
2576 __isl_take isl_basic_set
*context
, __isl_take
int *row
,
2577 struct isl_tab
*tab
)
2580 isl_basic_set_free(context
);
2582 bset
= update_ineq(bset
, row
, tab
);
2589 /* Remove all information from bset that is redundant in the context
2591 * "ineq" contains the (possibly transformed) inequalities of "bset",
2592 * in the same order.
2593 * The (explicit) equalities of "bset" are assumed to have been taken
2594 * into account by the transformation such that only the inequalities
2596 * "context" is assumed not to be empty.
2598 * "row" keeps track of the constraint index of a "bset" inequality in "tab".
2599 * A value of -1 means that the inequality is obviously redundant and may
2600 * not even appear in "tab".
2602 * We first mark the inequalities of "bset"
2603 * that are obviously redundant with respect to some inequality in "context".
2604 * Then we remove those constraints from "context" that have become
2605 * irrelevant for computing the gist of "bset".
2606 * Note that this removal of constraints cannot be replaced by
2607 * a factorization because factors in "bset" may still be connected
2608 * to each other through constraints in "context".
2610 * If there are any inequalities left, we construct a tableau for
2611 * the context and then add the inequalities of "bset".
2612 * Before adding these inequalities, we freeze all constraints such that
2613 * they won't be considered redundant in terms of the constraints of "bset".
2614 * Then we detect all redundant constraints (among the
2615 * constraints that weren't frozen), first by checking for redundancy in the
2616 * the tableau and then by checking if replacing a constraint by its negation
2617 * would lead to an empty set. This last step is fairly expensive
2618 * and could be optimized by more reuse of the tableau.
2619 * Finally, we update bset according to the results.
2621 static __isl_give isl_basic_set
*uset_gist_full(__isl_take isl_basic_set
*bset
,
2622 __isl_take isl_mat
*ineq
, __isl_take isl_basic_set
*context
)
2627 isl_basic_set
*combined
= NULL
;
2628 struct isl_tab
*tab
= NULL
;
2629 unsigned n_eq
, context_ineq
;
2632 if (!bset
|| !ineq
|| !context
)
2635 if (bset
->n_ineq
== 0 || isl_basic_set_plain_is_universe(context
)) {
2636 isl_basic_set_free(context
);
2641 ctx
= isl_basic_set_get_ctx(context
);
2642 row
= isl_calloc_array(ctx
, int, bset
->n_ineq
);
2646 if (mark_shifted_constraints(ineq
, context
, row
) < 0)
2648 if (all_neg(row
, bset
->n_ineq
))
2649 return update_ineq_free(bset
, ineq
, context
, row
, NULL
);
2651 context
= drop_irrelevant_constraints_marked(context
, ineq
, row
);
2654 if (isl_basic_set_plain_is_universe(context
))
2655 return update_ineq_free(bset
, ineq
, context
, row
, NULL
);
2657 n_eq
= context
->n_eq
;
2658 context_ineq
= context
->n_ineq
;
2659 combined
= isl_basic_set_cow(isl_basic_set_copy(context
));
2660 combined
= isl_basic_set_extend_constraints(combined
, 0, bset
->n_ineq
);
2661 tab
= isl_tab_from_basic_set(combined
, 0);
2662 for (i
= 0; i
< context_ineq
; ++i
)
2663 if (isl_tab_freeze_constraint(tab
, n_eq
+ i
) < 0)
2665 if (isl_tab_extend_cons(tab
, bset
->n_ineq
) < 0)
2668 for (i
= 0; i
< bset
->n_ineq
; ++i
) {
2671 combined
= isl_basic_set_add_ineq(combined
, ineq
->row
[i
]);
2672 if (isl_tab_add_ineq(tab
, ineq
->row
[i
]) < 0)
2676 if (isl_tab_detect_implicit_equalities(tab
) < 0)
2678 if (isl_tab_detect_redundant(tab
) < 0)
2680 total
= isl_basic_set_total_dim(bset
);
2681 for (i
= bset
->n_ineq
- 1; i
>= 0; --i
) {
2682 isl_basic_set
*test
;
2688 if (tab
->con
[n_eq
+ r
].is_redundant
)
2690 test
= isl_basic_set_dup(combined
);
2691 if (isl_inequality_negate(test
, r
) < 0)
2692 test
= isl_basic_set_free(test
);
2693 test
= isl_basic_set_update_from_tab(test
, tab
);
2694 is_empty
= isl_basic_set_is_empty(test
);
2695 isl_basic_set_free(test
);
2699 tab
->con
[n_eq
+ r
].is_redundant
= 1;
2701 bset
= update_ineq_free(bset
, ineq
, context
, row
, tab
);
2703 ISL_F_SET(bset
, ISL_BASIC_SET_NO_IMPLICIT
);
2704 ISL_F_SET(bset
, ISL_BASIC_SET_NO_REDUNDANT
);
2707 isl_basic_set_free(combined
);
2713 isl_basic_set_free(combined
);
2714 isl_basic_set_free(context
);
2715 isl_basic_set_free(bset
);
2719 /* Extract the inequalities of "bset" as an isl_mat.
2721 static __isl_give isl_mat
*extract_ineq(__isl_keep isl_basic_set
*bset
)
2730 ctx
= isl_basic_set_get_ctx(bset
);
2731 total
= isl_basic_set_total_dim(bset
);
2732 ineq
= isl_mat_sub_alloc6(ctx
, bset
->ineq
, 0, bset
->n_ineq
,
2738 /* Remove all information from "bset" that is redundant in the context
2739 * of "context", for the case where both "bset" and "context" are
2742 static __isl_give isl_basic_set
*uset_gist_uncompressed(
2743 __isl_take isl_basic_set
*bset
, __isl_take isl_basic_set
*context
)
2747 ineq
= extract_ineq(bset
);
2748 return uset_gist_full(bset
, ineq
, context
);
2751 /* Remove all information from "bset" that is redundant in the context
2752 * of "context", for the case where the combined equalities of
2753 * "bset" and "context" allow for a compression that can be obtained
2754 * by preapplication of "T".
2756 * "bset" itself is not transformed by "T". Instead, the inequalities
2757 * are extracted from "bset" and those are transformed by "T".
2758 * uset_gist_full then determines which of the transformed inequalities
2759 * are redundant with respect to the transformed "context" and removes
2760 * the corresponding inequalities from "bset".
2762 * After preapplying "T" to the inequalities, any common factor is
2763 * removed from the coefficients. If this results in a tightening
2764 * of the constant term, then the same tightening is applied to
2765 * the corresponding untransformed inequality in "bset".
2766 * That is, if after plugging in T, a constraint f(x) >= 0 is of the form
2770 * with 0 <= r < g, then it is equivalent to
2774 * This means that f(x) >= 0 is equivalent to f(x) - r >= 0 in the affine
2775 * subspace compressed by T since the latter would be transformed to
2779 static __isl_give isl_basic_set
*uset_gist_compressed(
2780 __isl_take isl_basic_set
*bset
, __isl_take isl_basic_set
*context
,
2781 __isl_take isl_mat
*T
)
2785 int i
, n_row
, n_col
;
2788 ineq
= extract_ineq(bset
);
2789 ineq
= isl_mat_product(ineq
, isl_mat_copy(T
));
2790 context
= isl_basic_set_preimage(context
, T
);
2792 if (!ineq
|| !context
)
2794 if (isl_basic_set_plain_is_empty(context
)) {
2796 isl_basic_set_free(context
);
2797 return isl_basic_set_set_to_empty(bset
);
2800 ctx
= isl_mat_get_ctx(ineq
);
2801 n_row
= isl_mat_rows(ineq
);
2802 n_col
= isl_mat_cols(ineq
);
2804 for (i
= 0; i
< n_row
; ++i
) {
2805 isl_seq_gcd(ineq
->row
[i
] + 1, n_col
- 1, &ctx
->normalize_gcd
);
2806 if (isl_int_is_zero(ctx
->normalize_gcd
))
2808 if (isl_int_is_one(ctx
->normalize_gcd
))
2810 isl_seq_scale_down(ineq
->row
[i
] + 1, ineq
->row
[i
] + 1,
2811 ctx
->normalize_gcd
, n_col
- 1);
2812 isl_int_fdiv_r(rem
, ineq
->row
[i
][0], ctx
->normalize_gcd
);
2813 isl_int_fdiv_q(ineq
->row
[i
][0],
2814 ineq
->row
[i
][0], ctx
->normalize_gcd
);
2815 if (isl_int_is_zero(rem
))
2817 bset
= isl_basic_set_cow(bset
);
2820 isl_int_sub(bset
->ineq
[i
][0], bset
->ineq
[i
][0], rem
);
2824 return uset_gist_full(bset
, ineq
, context
);
2827 isl_basic_set_free(context
);
2828 isl_basic_set_free(bset
);
2832 /* Project "bset" onto the variables that are involved in "template".
2834 static __isl_give isl_basic_set
*project_onto_involved(
2835 __isl_take isl_basic_set
*bset
, __isl_keep isl_basic_set
*template)
2839 if (!bset
|| !template)
2840 return isl_basic_set_free(bset
);
2842 n
= isl_basic_set_dim(template, isl_dim_set
);
2844 for (i
= 0; i
< n
; ++i
) {
2847 involved
= isl_basic_set_involves_dims(template,
2850 return isl_basic_set_free(bset
);
2853 bset
= isl_basic_set_eliminate_vars(bset
, i
, 1);
2859 /* Remove all information from bset that is redundant in the context
2860 * of context. In particular, equalities that are linear combinations
2861 * of those in context are removed. Then the inequalities that are
2862 * redundant in the context of the equalities and inequalities of
2863 * context are removed.
2865 * First of all, we drop those constraints from "context"
2866 * that are irrelevant for computing the gist of "bset".
2867 * Alternatively, we could factorize the intersection of "context" and "bset".
2869 * We first compute the intersection of the integer affine hulls
2870 * of "bset" and "context",
2871 * compute the gist inside this intersection and then reduce
2872 * the constraints with respect to the equalities of the context
2873 * that only involve variables already involved in the input.
2875 * If two constraints are mutually redundant, then uset_gist_full
2876 * will remove the second of those constraints. We therefore first
2877 * sort the constraints so that constraints not involving existentially
2878 * quantified variables are given precedence over those that do.
2879 * We have to perform this sorting before the variable compression,
2880 * because that may effect the order of the variables.
2882 static __isl_give isl_basic_set
*uset_gist(__isl_take isl_basic_set
*bset
,
2883 __isl_take isl_basic_set
*context
)
2888 isl_basic_set
*aff_context
;
2891 if (!bset
|| !context
)
2894 context
= drop_irrelevant_constraints(context
, bset
);
2896 bset
= isl_basic_set_detect_equalities(bset
);
2897 aff
= isl_basic_set_copy(bset
);
2898 aff
= isl_basic_set_plain_affine_hull(aff
);
2899 context
= isl_basic_set_detect_equalities(context
);
2900 aff_context
= isl_basic_set_copy(context
);
2901 aff_context
= isl_basic_set_plain_affine_hull(aff_context
);
2902 aff
= isl_basic_set_intersect(aff
, aff_context
);
2905 if (isl_basic_set_plain_is_empty(aff
)) {
2906 isl_basic_set_free(bset
);
2907 isl_basic_set_free(context
);
2910 bset
= isl_basic_set_sort_constraints(bset
);
2911 if (aff
->n_eq
== 0) {
2912 isl_basic_set_free(aff
);
2913 return uset_gist_uncompressed(bset
, context
);
2915 total
= isl_basic_set_total_dim(bset
);
2916 eq
= isl_mat_sub_alloc6(bset
->ctx
, aff
->eq
, 0, aff
->n_eq
, 0, 1 + total
);
2917 eq
= isl_mat_cow(eq
);
2918 T
= isl_mat_variable_compression(eq
, NULL
);
2919 isl_basic_set_free(aff
);
2920 if (T
&& T
->n_col
== 0) {
2922 isl_basic_set_free(context
);
2923 return isl_basic_set_set_to_empty(bset
);
2926 aff_context
= isl_basic_set_affine_hull(isl_basic_set_copy(context
));
2927 aff_context
= project_onto_involved(aff_context
, bset
);
2929 bset
= uset_gist_compressed(bset
, context
, T
);
2930 bset
= isl_basic_set_reduce_using_equalities(bset
, aff_context
);
2933 ISL_F_SET(bset
, ISL_BASIC_SET_NO_IMPLICIT
);
2934 ISL_F_SET(bset
, ISL_BASIC_SET_NO_REDUNDANT
);
2939 isl_basic_set_free(bset
);
2940 isl_basic_set_free(context
);
2944 /* Return the number of equality constraints in "bmap" that involve
2945 * local variables. This function assumes that Gaussian elimination
2946 * has been applied to the equality constraints.
2948 static int n_div_eq(__isl_keep isl_basic_map
*bmap
)
2956 if (bmap
->n_eq
== 0)
2959 total
= isl_basic_map_dim(bmap
, isl_dim_all
);
2960 n_div
= isl_basic_map_dim(bmap
, isl_dim_div
);
2963 for (i
= 0; i
< bmap
->n_eq
; ++i
)
2964 if (isl_seq_first_non_zero(bmap
->eq
[i
] + 1 + total
,
2971 /* Construct a basic map in "space" defined by the equality constraints in "eq".
2972 * The constraints are assumed not to involve any local variables.
2974 static __isl_give isl_basic_map
*basic_map_from_equalities(
2975 __isl_take isl_space
*space
, __isl_take isl_mat
*eq
)
2978 isl_basic_map
*bmap
= NULL
;
2983 if (1 + isl_space_dim(space
, isl_dim_all
) != eq
->n_col
)
2984 isl_die(isl_space_get_ctx(space
), isl_error_internal
,
2985 "unexpected number of columns", goto error
);
2987 bmap
= isl_basic_map_alloc_space(isl_space_copy(space
),
2989 for (i
= 0; i
< eq
->n_row
; ++i
) {
2990 k
= isl_basic_map_alloc_equality(bmap
);
2993 isl_seq_cpy(bmap
->eq
[k
], eq
->row
[i
], eq
->n_col
);
2996 isl_space_free(space
);
3000 isl_space_free(space
);
3002 isl_basic_map_free(bmap
);
3006 /* Construct and return a variable compression based on the equality
3007 * constraints in "bmap1" and "bmap2" that do not involve the local variables.
3008 * "n1" is the number of (initial) equality constraints in "bmap1"
3009 * that do involve local variables.
3010 * "n2" is the number of (initial) equality constraints in "bmap2"
3011 * that do involve local variables.
3012 * "total" is the total number of other variables.
3013 * This function assumes that Gaussian elimination
3014 * has been applied to the equality constraints in both "bmap1" and "bmap2"
3015 * such that the equality constraints not involving local variables
3016 * are those that start at "n1" or "n2".
3018 * If either of "bmap1" and "bmap2" does not have such equality constraints,
3019 * then simply compute the compression based on the equality constraints
3020 * in the other basic map.
3021 * Otherwise, combine the equality constraints from both into a new
3022 * basic map such that Gaussian elimination can be applied to this combination
3023 * and then construct a variable compression from the resulting
3024 * equality constraints.
3026 static __isl_give isl_mat
*combined_variable_compression(
3027 __isl_keep isl_basic_map
*bmap1
, int n1
,
3028 __isl_keep isl_basic_map
*bmap2
, int n2
, int total
)
3031 isl_mat
*E1
, *E2
, *V
;
3032 isl_basic_map
*bmap
;
3034 ctx
= isl_basic_map_get_ctx(bmap1
);
3035 if (bmap1
->n_eq
== n1
) {
3036 E2
= isl_mat_sub_alloc6(ctx
, bmap2
->eq
,
3037 n2
, bmap2
->n_eq
- n2
, 0, 1 + total
);
3038 return isl_mat_variable_compression(E2
, NULL
);
3040 if (bmap2
->n_eq
== n2
) {
3041 E1
= isl_mat_sub_alloc6(ctx
, bmap1
->eq
,
3042 n1
, bmap1
->n_eq
- n1
, 0, 1 + total
);
3043 return isl_mat_variable_compression(E1
, NULL
);
3045 E1
= isl_mat_sub_alloc6(ctx
, bmap1
->eq
,
3046 n1
, bmap1
->n_eq
- n1
, 0, 1 + total
);
3047 E2
= isl_mat_sub_alloc6(ctx
, bmap2
->eq
,
3048 n2
, bmap2
->n_eq
- n2
, 0, 1 + total
);
3049 E1
= isl_mat_concat(E1
, E2
);
3050 bmap
= basic_map_from_equalities(isl_basic_map_get_space(bmap1
), E1
);
3051 bmap
= isl_basic_map_gauss(bmap
, NULL
);
3054 E1
= isl_mat_sub_alloc6(ctx
, bmap
->eq
, 0, bmap
->n_eq
, 0, 1 + total
);
3055 V
= isl_mat_variable_compression(E1
, NULL
);
3056 isl_basic_map_free(bmap
);
3061 /* Extract the stride constraints from "bmap", compressed
3062 * with respect to both the stride constraints in "context" and
3063 * the remaining equality constraints in both "bmap" and "context".
3064 * "bmap_n_eq" is the number of (initial) stride constraints in "bmap".
3065 * "context_n_eq" is the number of (initial) stride constraints in "context".
3067 * Let x be all variables in "bmap" (and "context") other than the local
3068 * variables. First compute a variable compression
3072 * based on the non-stride equality constraints in "bmap" and "context".
3073 * Consider the stride constraints of "context",
3077 * with y the local variables and plug in the variable compression,
3080 * A(V x') + B(y) = 0
3082 * Use these constraints to compute a parameter compression on x'
3086 * Now consider the stride constraints of "bmap"
3090 * and plug in x = V*T x''.
3091 * That is, return A = [C*V*T D].
3093 static __isl_give isl_mat
*extract_compressed_stride_constraints(
3094 __isl_keep isl_basic_map
*bmap
, int bmap_n_eq
,
3095 __isl_keep isl_basic_map
*context
, int context_n_eq
)
3099 isl_mat
*A
, *B
, *T
, *V
;
3101 total
= isl_basic_map_dim(context
, isl_dim_all
);
3102 n_div
= isl_basic_map_dim(context
, isl_dim_div
);
3105 ctx
= isl_basic_map_get_ctx(bmap
);
3107 V
= combined_variable_compression(bmap
, bmap_n_eq
,
3108 context
, context_n_eq
, total
);
3110 A
= isl_mat_sub_alloc6(ctx
, context
->eq
, 0, context_n_eq
, 0, 1 + total
);
3111 B
= isl_mat_sub_alloc6(ctx
, context
->eq
,
3112 0, context_n_eq
, 1 + total
, n_div
);
3113 A
= isl_mat_product(A
, isl_mat_copy(V
));
3114 T
= isl_mat_parameter_compression_ext(A
, B
);
3115 T
= isl_mat_product(V
, T
);
3117 n_div
= isl_basic_map_dim(bmap
, isl_dim_div
);
3118 T
= isl_mat_diagonal(T
, isl_mat_identity(ctx
, n_div
));
3120 A
= isl_mat_sub_alloc6(ctx
, bmap
->eq
,
3121 0, bmap_n_eq
, 0, 1 + total
+ n_div
);
3122 A
= isl_mat_product(A
, T
);
3127 /* Remove the prime factors from *g that have an exponent that
3128 * is strictly smaller than the exponent in "c".
3129 * All exponents in *g are known to be smaller than or equal
3132 * That is, if *g is equal to
3134 * p_1^{e_1} p_2^{e_2} ... p_n^{e_n}
3136 * and "c" is equal to
3138 * p_1^{f_1} p_2^{f_2} ... p_n^{f_n}
3142 * p_1^{e_1 * (e_1 = f_1)} p_2^{e_2 * (e_2 = f_2)} ...
3143 * p_n^{e_n * (e_n = f_n)}
3145 * If e_i = f_i, then c / *g does not have any p_i factors and therefore
3146 * neither does the gcd of *g and c / *g.
3147 * If e_i < f_i, then the gcd of *g and c / *g has a positive
3148 * power min(e_i, s_i) of p_i with s_i = f_i - e_i among its factors.
3149 * Dividing *g by this gcd therefore strictly reduces the exponent
3150 * of the prime factors that need to be removed, while leaving the
3151 * other prime factors untouched.
3152 * Repeating this process until gcd(*g, c / *g) = 1 therefore
3153 * removes all undesired factors, without removing any others.
3155 static void remove_incomplete_powers(isl_int
*g
, isl_int c
)
3161 isl_int_divexact(t
, c
, *g
);
3162 isl_int_gcd(t
, t
, *g
);
3163 if (isl_int_is_one(t
))
3165 isl_int_divexact(*g
, *g
, t
);
3170 /* Reduce the "n" stride constraints in "bmap" based on a copy "A"
3171 * of the same stride constraints in a compressed space that exploits
3172 * all equalities in the context and the other equalities in "bmap".
3174 * If the stride constraints of "bmap" are of the form
3178 * then A is of the form
3182 * If any of these constraints involves only a single local variable y,
3183 * then the constraint appears as
3193 * Let g be the gcd of m and the coefficients of h.
3194 * Then, in particular, g is a divisor of the coefficients of h and
3198 * is known to be a multiple of g.
3199 * If some prime factor in m appears with the same exponent in g,
3200 * then it can be removed from m because f(x) is already known
3201 * to be a multiple of g and therefore in particular of this power
3202 * of the prime factors.
3203 * Prime factors that appear with a smaller exponent in g cannot
3204 * be removed from m.
3205 * Let g' be the divisor of g containing all prime factors that
3206 * appear with the same exponent in m and g, then
3210 * can be replaced by
3212 * f(x) + m/g' y_i' = 0
3214 * Note that (if g' != 1) this changes the explicit representation
3215 * of y_i to that of y_i', so the integer division at position i
3216 * is marked unknown and later recomputed by a call to
3217 * isl_basic_map_gauss.
3219 static __isl_give isl_basic_map
*reduce_stride_constraints(
3220 __isl_take isl_basic_map
*bmap
, int n
, __isl_keep isl_mat
*A
)
3228 return isl_basic_map_free(bmap
);
3230 total
= isl_basic_map_dim(bmap
, isl_dim_all
);
3231 n_div
= isl_basic_map_dim(bmap
, isl_dim_div
);
3235 for (i
= 0; i
< n
; ++i
) {
3238 div
= isl_seq_first_non_zero(bmap
->eq
[i
] + 1 + total
, n_div
);
3240 isl_die(isl_basic_map_get_ctx(bmap
), isl_error_internal
,
3241 "equality constraints modified unexpectedly",
3243 if (isl_seq_first_non_zero(bmap
->eq
[i
] + 1 + total
+ div
+ 1,
3244 n_div
- div
- 1) != -1)
3246 if (isl_mat_row_gcd(A
, i
, &gcd
) < 0)
3248 if (isl_int_is_one(gcd
))
3250 remove_incomplete_powers(&gcd
, bmap
->eq
[i
][1 + total
+ div
]);
3251 if (isl_int_is_one(gcd
))
3253 isl_int_divexact(bmap
->eq
[i
][1 + total
+ div
],
3254 bmap
->eq
[i
][1 + total
+ div
], gcd
);
3255 bmap
= isl_basic_map_mark_div_unknown(bmap
, div
);
3263 bmap
= isl_basic_map_gauss(bmap
, NULL
);
3268 isl_basic_map_free(bmap
);
3272 /* Simplify the stride constraints in "bmap" based on
3273 * the remaining equality constraints in "bmap" and all equality
3274 * constraints in "context".
3275 * Only do this if both "bmap" and "context" have stride constraints.
3277 * First extract a copy of the stride constraints in "bmap" in a compressed
3278 * space exploiting all the other equality constraints and then
3279 * use this compressed copy to simplify the original stride constraints.
3281 static __isl_give isl_basic_map
*gist_strides(__isl_take isl_basic_map
*bmap
,
3282 __isl_keep isl_basic_map
*context
)
3284 int bmap_n_eq
, context_n_eq
;
3287 if (!bmap
|| !context
)
3288 return isl_basic_map_free(bmap
);
3290 bmap_n_eq
= n_div_eq(bmap
);
3291 context_n_eq
= n_div_eq(context
);
3293 if (bmap_n_eq
< 0 || context_n_eq
< 0)
3294 return isl_basic_map_free(bmap
);
3295 if (bmap_n_eq
== 0 || context_n_eq
== 0)
3298 A
= extract_compressed_stride_constraints(bmap
, bmap_n_eq
,
3299 context
, context_n_eq
);
3300 bmap
= reduce_stride_constraints(bmap
, bmap_n_eq
, A
);
3307 /* Return a basic map that has the same intersection with "context" as "bmap"
3308 * and that is as "simple" as possible.
3310 * The core computation is performed on the pure constraints.
3311 * When we add back the meaning of the integer divisions, we need
3312 * to (re)introduce the div constraints. If we happen to have
3313 * discovered that some of these integer divisions are equal to
3314 * some affine combination of other variables, then these div
3315 * constraints may end up getting simplified in terms of the equalities,
3316 * resulting in extra inequalities on the other variables that
3317 * may have been removed already or that may not even have been
3318 * part of the input. We try and remove those constraints of
3319 * this form that are most obviously redundant with respect to
3320 * the context. We also remove those div constraints that are
3321 * redundant with respect to the other constraints in the result.
3323 * The stride constraints among the equality constraints in "bmap" are
3324 * also simplified with respecting to the other equality constraints
3325 * in "bmap" and with respect to all equality constraints in "context".
3327 struct isl_basic_map
*isl_basic_map_gist(struct isl_basic_map
*bmap
,
3328 struct isl_basic_map
*context
)
3330 isl_basic_set
*bset
, *eq
;
3331 isl_basic_map
*eq_bmap
;
3332 unsigned total
, n_div
, extra
, n_eq
, n_ineq
;
3334 if (!bmap
|| !context
)
3337 if (isl_basic_map_plain_is_universe(bmap
)) {
3338 isl_basic_map_free(context
);
3341 if (isl_basic_map_plain_is_empty(context
)) {
3342 isl_space
*space
= isl_basic_map_get_space(bmap
);
3343 isl_basic_map_free(bmap
);
3344 isl_basic_map_free(context
);
3345 return isl_basic_map_universe(space
);
3347 if (isl_basic_map_plain_is_empty(bmap
)) {
3348 isl_basic_map_free(context
);
3352 bmap
= isl_basic_map_remove_redundancies(bmap
);
3353 context
= isl_basic_map_remove_redundancies(context
);
3357 context
= isl_basic_map_align_divs(context
, bmap
);
3358 n_div
= isl_basic_map_dim(context
, isl_dim_div
);
3359 total
= isl_basic_map_dim(bmap
, isl_dim_all
);
3360 extra
= n_div
- isl_basic_map_dim(bmap
, isl_dim_div
);
3362 bset
= isl_basic_map_underlying_set(isl_basic_map_copy(bmap
));
3363 bset
= isl_basic_set_add_dims(bset
, isl_dim_set
, extra
);
3364 bset
= uset_gist(bset
,
3365 isl_basic_map_underlying_set(isl_basic_map_copy(context
)));
3366 bset
= isl_basic_set_project_out(bset
, isl_dim_set
, total
, extra
);
3368 if (!bset
|| bset
->n_eq
== 0 || n_div
== 0 ||
3369 isl_basic_set_plain_is_empty(bset
)) {
3370 isl_basic_map_free(context
);
3371 return isl_basic_map_overlying_set(bset
, bmap
);
3375 n_ineq
= bset
->n_ineq
;
3376 eq
= isl_basic_set_copy(bset
);
3377 eq
= isl_basic_set_cow(eq
);
3378 if (isl_basic_set_free_inequality(eq
, n_ineq
) < 0)
3379 eq
= isl_basic_set_free(eq
);
3380 if (isl_basic_set_free_equality(bset
, n_eq
) < 0)
3381 bset
= isl_basic_set_free(bset
);
3383 eq_bmap
= isl_basic_map_overlying_set(eq
, isl_basic_map_copy(bmap
));
3384 eq_bmap
= gist_strides(eq_bmap
, context
);
3385 eq_bmap
= isl_basic_map_remove_shifted_constraints(eq_bmap
, context
);
3386 bmap
= isl_basic_map_overlying_set(bset
, bmap
);
3387 bmap
= isl_basic_map_intersect(bmap
, eq_bmap
);
3388 bmap
= isl_basic_map_remove_redundancies(bmap
);
3392 isl_basic_map_free(bmap
);
3393 isl_basic_map_free(context
);
3398 * Assumes context has no implicit divs.
3400 __isl_give isl_map
*isl_map_gist_basic_map(__isl_take isl_map
*map
,
3401 __isl_take isl_basic_map
*context
)
3405 if (!map
|| !context
)
3408 if (isl_basic_map_plain_is_empty(context
)) {
3409 isl_space
*space
= isl_map_get_space(map
);
3411 isl_basic_map_free(context
);
3412 return isl_map_universe(space
);
3415 context
= isl_basic_map_remove_redundancies(context
);
3416 map
= isl_map_cow(map
);
3417 if (!map
|| !context
)
3419 isl_assert(map
->ctx
, isl_space_is_equal(map
->dim
, context
->dim
), goto error
);
3420 map
= isl_map_compute_divs(map
);
3423 for (i
= map
->n
- 1; i
>= 0; --i
) {
3424 map
->p
[i
] = isl_basic_map_gist(map
->p
[i
],
3425 isl_basic_map_copy(context
));
3428 if (isl_basic_map_plain_is_empty(map
->p
[i
])) {
3429 isl_basic_map_free(map
->p
[i
]);
3430 if (i
!= map
->n
- 1)
3431 map
->p
[i
] = map
->p
[map
->n
- 1];
3435 isl_basic_map_free(context
);
3436 ISL_F_CLR(map
, ISL_MAP_NORMALIZED
);
3440 isl_basic_map_free(context
);
3444 /* Drop all inequalities from "bmap" that also appear in "context".
3445 * "context" is assumed to have only known local variables and
3446 * the initial local variables of "bmap" are assumed to be the same
3447 * as those of "context".
3448 * The constraints of both "bmap" and "context" are assumed
3449 * to have been sorted using isl_basic_map_sort_constraints.
3451 * Run through the inequality constraints of "bmap" and "context"
3453 * If a constraint of "bmap" involves variables not in "context",
3454 * then it cannot appear in "context".
3455 * If a matching constraint is found, it is removed from "bmap".
3457 static __isl_give isl_basic_map
*drop_inequalities(
3458 __isl_take isl_basic_map
*bmap
, __isl_keep isl_basic_map
*context
)
3461 unsigned total
, extra
;
3463 if (!bmap
|| !context
)
3464 return isl_basic_map_free(bmap
);
3466 total
= isl_basic_map_total_dim(context
);
3467 extra
= isl_basic_map_total_dim(bmap
) - total
;
3469 i1
= bmap
->n_ineq
- 1;
3470 i2
= context
->n_ineq
- 1;
3471 while (bmap
&& i1
>= 0 && i2
>= 0) {
3474 if (isl_seq_first_non_zero(bmap
->ineq
[i1
] + 1 + total
,
3479 cmp
= isl_basic_map_constraint_cmp(context
, bmap
->ineq
[i1
],
3489 if (isl_int_eq(bmap
->ineq
[i1
][0], context
->ineq
[i2
][0])) {
3490 bmap
= isl_basic_map_cow(bmap
);
3491 if (isl_basic_map_drop_inequality(bmap
, i1
) < 0)
3492 bmap
= isl_basic_map_free(bmap
);
3501 /* Drop all equalities from "bmap" that also appear in "context".
3502 * "context" is assumed to have only known local variables and
3503 * the initial local variables of "bmap" are assumed to be the same
3504 * as those of "context".
3506 * Run through the equality constraints of "bmap" and "context"
3508 * If a constraint of "bmap" involves variables not in "context",
3509 * then it cannot appear in "context".
3510 * If a matching constraint is found, it is removed from "bmap".
3512 static __isl_give isl_basic_map
*drop_equalities(
3513 __isl_take isl_basic_map
*bmap
, __isl_keep isl_basic_map
*context
)
3516 unsigned total
, extra
;
3518 if (!bmap
|| !context
)
3519 return isl_basic_map_free(bmap
);
3521 total
= isl_basic_map_total_dim(context
);
3522 extra
= isl_basic_map_total_dim(bmap
) - total
;
3524 i1
= bmap
->n_eq
- 1;
3525 i2
= context
->n_eq
- 1;
3527 while (bmap
&& i1
>= 0 && i2
>= 0) {
3530 if (isl_seq_first_non_zero(bmap
->eq
[i1
] + 1 + total
,
3533 last1
= isl_seq_last_non_zero(bmap
->eq
[i1
] + 1, total
);
3534 last2
= isl_seq_last_non_zero(context
->eq
[i2
] + 1, total
);
3535 if (last1
> last2
) {
3539 if (last1
< last2
) {
3543 if (isl_seq_eq(bmap
->eq
[i1
], context
->eq
[i2
], 1 + total
)) {
3544 bmap
= isl_basic_map_cow(bmap
);
3545 if (isl_basic_map_drop_equality(bmap
, i1
) < 0)
3546 bmap
= isl_basic_map_free(bmap
);
3555 /* Remove the constraints in "context" from "bmap".
3556 * "context" is assumed to have explicit representations
3557 * for all local variables.
3559 * First align the divs of "bmap" to those of "context" and
3560 * sort the constraints. Then drop all constraints from "bmap"
3561 * that appear in "context".
3563 __isl_give isl_basic_map
*isl_basic_map_plain_gist(
3564 __isl_take isl_basic_map
*bmap
, __isl_take isl_basic_map
*context
)
3566 isl_bool done
, known
;
3568 done
= isl_basic_map_plain_is_universe(context
);
3569 if (done
== isl_bool_false
)
3570 done
= isl_basic_map_plain_is_universe(bmap
);
3571 if (done
== isl_bool_false
)
3572 done
= isl_basic_map_plain_is_empty(context
);
3573 if (done
== isl_bool_false
)
3574 done
= isl_basic_map_plain_is_empty(bmap
);
3578 isl_basic_map_free(context
);
3581 known
= isl_basic_map_divs_known(context
);
3585 isl_die(isl_basic_map_get_ctx(bmap
), isl_error_invalid
,
3586 "context has unknown divs", goto error
);
3588 bmap
= isl_basic_map_align_divs(bmap
, context
);
3589 bmap
= isl_basic_map_gauss(bmap
, NULL
);
3590 bmap
= isl_basic_map_sort_constraints(bmap
);
3591 context
= isl_basic_map_sort_constraints(context
);
3593 bmap
= drop_inequalities(bmap
, context
);
3594 bmap
= drop_equalities(bmap
, context
);
3596 isl_basic_map_free(context
);
3597 bmap
= isl_basic_map_finalize(bmap
);
3600 isl_basic_map_free(bmap
);
3601 isl_basic_map_free(context
);
3605 /* Replace "map" by the disjunct at position "pos" and free "context".
3607 static __isl_give isl_map
*replace_by_disjunct(__isl_take isl_map
*map
,
3608 int pos
, __isl_take isl_basic_map
*context
)
3610 isl_basic_map
*bmap
;
3612 bmap
= isl_basic_map_copy(map
->p
[pos
]);
3614 isl_basic_map_free(context
);
3615 return isl_map_from_basic_map(bmap
);
3618 /* Remove the constraints in "context" from "map".
3619 * If any of the disjuncts in the result turns out to be the universe,
3620 * then return this universe.
3621 * "context" is assumed to have explicit representations
3622 * for all local variables.
3624 __isl_give isl_map
*isl_map_plain_gist_basic_map(__isl_take isl_map
*map
,
3625 __isl_take isl_basic_map
*context
)
3628 isl_bool univ
, known
;
3630 univ
= isl_basic_map_plain_is_universe(context
);
3634 isl_basic_map_free(context
);
3637 known
= isl_basic_map_divs_known(context
);
3641 isl_die(isl_map_get_ctx(map
), isl_error_invalid
,
3642 "context has unknown divs", goto error
);
3644 map
= isl_map_cow(map
);
3647 for (i
= 0; i
< map
->n
; ++i
) {
3648 map
->p
[i
] = isl_basic_map_plain_gist(map
->p
[i
],
3649 isl_basic_map_copy(context
));
3650 univ
= isl_basic_map_plain_is_universe(map
->p
[i
]);
3653 if (univ
&& map
->n
> 1)
3654 return replace_by_disjunct(map
, i
, context
);
3657 isl_basic_map_free(context
);
3658 ISL_F_CLR(map
, ISL_MAP_NORMALIZED
);
3660 ISL_F_CLR(map
, ISL_MAP_DISJOINT
);
3664 isl_basic_map_free(context
);
3668 /* Replace "map" by a universe map in the same space and free "drop".
3670 static __isl_give isl_map
*replace_by_universe(__isl_take isl_map
*map
,
3671 __isl_take isl_map
*drop
)
3675 res
= isl_map_universe(isl_map_get_space(map
));
3681 /* Return a map that has the same intersection with "context" as "map"
3682 * and that is as "simple" as possible.
3684 * If "map" is already the universe, then we cannot make it any simpler.
3685 * Similarly, if "context" is the universe, then we cannot exploit it
3687 * If "map" and "context" are identical to each other, then we can
3688 * return the corresponding universe.
3690 * If either "map" or "context" consists of multiple disjuncts,
3691 * then check if "context" happens to be a subset of "map",
3692 * in which case all constraints can be removed.
3693 * In case of multiple disjuncts, the standard procedure
3694 * may not be able to detect that all constraints can be removed.
3696 * If none of these cases apply, we have to work a bit harder.
3697 * During this computation, we make use of a single disjunct context,
3698 * so if the original context consists of more than one disjunct
3699 * then we need to approximate the context by a single disjunct set.
3700 * Simply taking the simple hull may drop constraints that are
3701 * only implicitly available in each disjunct. We therefore also
3702 * look for constraints among those defining "map" that are valid
3703 * for the context. These can then be used to simplify away
3704 * the corresponding constraints in "map".
3706 static __isl_give isl_map
*map_gist(__isl_take isl_map
*map
,
3707 __isl_take isl_map
*context
)
3711 int single_disjunct_map
, single_disjunct_context
;
3713 isl_basic_map
*hull
;
3715 is_universe
= isl_map_plain_is_universe(map
);
3716 if (is_universe
>= 0 && !is_universe
)
3717 is_universe
= isl_map_plain_is_universe(context
);
3718 if (is_universe
< 0)
3721 isl_map_free(context
);
3725 equal
= isl_map_plain_is_equal(map
, context
);
3729 return replace_by_universe(map
, context
);
3731 single_disjunct_map
= isl_map_n_basic_map(map
) == 1;
3732 single_disjunct_context
= isl_map_n_basic_map(context
) == 1;
3733 if (!single_disjunct_map
|| !single_disjunct_context
) {
3734 subset
= isl_map_is_subset(context
, map
);
3738 return replace_by_universe(map
, context
);
3741 context
= isl_map_compute_divs(context
);
3744 if (single_disjunct_context
) {
3745 hull
= isl_map_simple_hull(context
);
3750 ctx
= isl_map_get_ctx(map
);
3751 list
= isl_map_list_alloc(ctx
, 2);
3752 list
= isl_map_list_add(list
, isl_map_copy(context
));
3753 list
= isl_map_list_add(list
, isl_map_copy(map
));
3754 hull
= isl_map_unshifted_simple_hull_from_map_list(context
,
3757 return isl_map_gist_basic_map(map
, hull
);
3760 isl_map_free(context
);
3764 __isl_give isl_map
*isl_map_gist(__isl_take isl_map
*map
,
3765 __isl_take isl_map
*context
)
3767 return isl_map_align_params_map_map_and(map
, context
, &map_gist
);
3770 struct isl_basic_set
*isl_basic_set_gist(struct isl_basic_set
*bset
,
3771 struct isl_basic_set
*context
)
3773 return bset_from_bmap(isl_basic_map_gist(bset_to_bmap(bset
),
3774 bset_to_bmap(context
)));
3777 __isl_give isl_set
*isl_set_gist_basic_set(__isl_take isl_set
*set
,
3778 __isl_take isl_basic_set
*context
)
3780 return set_from_map(isl_map_gist_basic_map(set_to_map(set
),
3781 bset_to_bmap(context
)));
3784 __isl_give isl_set
*isl_set_gist_params_basic_set(__isl_take isl_set
*set
,
3785 __isl_take isl_basic_set
*context
)
3787 isl_space
*space
= isl_set_get_space(set
);
3788 isl_basic_set
*dom_context
= isl_basic_set_universe(space
);
3789 dom_context
= isl_basic_set_intersect_params(dom_context
, context
);
3790 return isl_set_gist_basic_set(set
, dom_context
);
3793 __isl_give isl_set
*isl_set_gist(__isl_take isl_set
*set
,
3794 __isl_take isl_set
*context
)
3796 return set_from_map(isl_map_gist(set_to_map(set
), set_to_map(context
)));
3799 /* Compute the gist of "bmap" with respect to the constraints "context"
3802 __isl_give isl_basic_map
*isl_basic_map_gist_domain(
3803 __isl_take isl_basic_map
*bmap
, __isl_take isl_basic_set
*context
)
3805 isl_space
*space
= isl_basic_map_get_space(bmap
);
3806 isl_basic_map
*bmap_context
= isl_basic_map_universe(space
);
3808 bmap_context
= isl_basic_map_intersect_domain(bmap_context
, context
);
3809 return isl_basic_map_gist(bmap
, bmap_context
);
3812 __isl_give isl_map
*isl_map_gist_domain(__isl_take isl_map
*map
,
3813 __isl_take isl_set
*context
)
3815 isl_map
*map_context
= isl_map_universe(isl_map_get_space(map
));
3816 map_context
= isl_map_intersect_domain(map_context
, context
);
3817 return isl_map_gist(map
, map_context
);
3820 __isl_give isl_map
*isl_map_gist_range(__isl_take isl_map
*map
,
3821 __isl_take isl_set
*context
)
3823 isl_map
*map_context
= isl_map_universe(isl_map_get_space(map
));
3824 map_context
= isl_map_intersect_range(map_context
, context
);
3825 return isl_map_gist(map
, map_context
);
3828 __isl_give isl_map
*isl_map_gist_params(__isl_take isl_map
*map
,
3829 __isl_take isl_set
*context
)
3831 isl_map
*map_context
= isl_map_universe(isl_map_get_space(map
));
3832 map_context
= isl_map_intersect_params(map_context
, context
);
3833 return isl_map_gist(map
, map_context
);
3836 __isl_give isl_set
*isl_set_gist_params(__isl_take isl_set
*set
,
3837 __isl_take isl_set
*context
)
3839 return isl_map_gist_params(set
, context
);
3842 /* Quick check to see if two basic maps are disjoint.
3843 * In particular, we reduce the equalities and inequalities of
3844 * one basic map in the context of the equalities of the other
3845 * basic map and check if we get a contradiction.
3847 isl_bool
isl_basic_map_plain_is_disjoint(__isl_keep isl_basic_map
*bmap1
,
3848 __isl_keep isl_basic_map
*bmap2
)
3850 struct isl_vec
*v
= NULL
;
3855 if (!bmap1
|| !bmap2
)
3856 return isl_bool_error
;
3857 isl_assert(bmap1
->ctx
, isl_space_is_equal(bmap1
->dim
, bmap2
->dim
),
3858 return isl_bool_error
);
3859 if (bmap1
->n_div
|| bmap2
->n_div
)
3860 return isl_bool_false
;
3861 if (!bmap1
->n_eq
&& !bmap2
->n_eq
)
3862 return isl_bool_false
;
3864 total
= isl_space_dim(bmap1
->dim
, isl_dim_all
);
3866 return isl_bool_false
;
3867 v
= isl_vec_alloc(bmap1
->ctx
, 1 + total
);
3870 elim
= isl_alloc_array(bmap1
->ctx
, int, total
);
3873 compute_elimination_index(bmap1
, elim
);
3874 for (i
= 0; i
< bmap2
->n_eq
; ++i
) {
3876 reduced
= reduced_using_equalities(v
->block
.data
, bmap2
->eq
[i
],
3878 if (reduced
&& !isl_int_is_zero(v
->block
.data
[0]) &&
3879 isl_seq_first_non_zero(v
->block
.data
+ 1, total
) == -1)
3882 for (i
= 0; i
< bmap2
->n_ineq
; ++i
) {
3884 reduced
= reduced_using_equalities(v
->block
.data
,
3885 bmap2
->ineq
[i
], bmap1
, elim
);
3886 if (reduced
&& isl_int_is_neg(v
->block
.data
[0]) &&
3887 isl_seq_first_non_zero(v
->block
.data
+ 1, total
) == -1)
3890 compute_elimination_index(bmap2
, elim
);
3891 for (i
= 0; i
< bmap1
->n_ineq
; ++i
) {
3893 reduced
= reduced_using_equalities(v
->block
.data
,
3894 bmap1
->ineq
[i
], bmap2
, elim
);
3895 if (reduced
&& isl_int_is_neg(v
->block
.data
[0]) &&
3896 isl_seq_first_non_zero(v
->block
.data
+ 1, total
) == -1)
3901 return isl_bool_false
;
3905 return isl_bool_true
;
3909 return isl_bool_error
;
3912 int isl_basic_set_plain_is_disjoint(__isl_keep isl_basic_set
*bset1
,
3913 __isl_keep isl_basic_set
*bset2
)
3915 return isl_basic_map_plain_is_disjoint(bset_to_bmap(bset1
),
3916 bset_to_bmap(bset2
));
3919 /* Does "test" hold for all pairs of basic maps in "map1" and "map2"?
3921 static isl_bool
all_pairs(__isl_keep isl_map
*map1
, __isl_keep isl_map
*map2
,
3922 isl_bool (*test
)(__isl_keep isl_basic_map
*bmap1
,
3923 __isl_keep isl_basic_map
*bmap2
))
3928 return isl_bool_error
;
3930 for (i
= 0; i
< map1
->n
; ++i
) {
3931 for (j
= 0; j
< map2
->n
; ++j
) {
3932 isl_bool d
= test(map1
->p
[i
], map2
->p
[j
]);
3933 if (d
!= isl_bool_true
)
3938 return isl_bool_true
;
3941 /* Are "map1" and "map2" obviously disjoint, based on information
3942 * that can be derived without looking at the individual basic maps?
3944 * In particular, if one of them is empty or if they live in different spaces
3945 * (ignoring parameters), then they are clearly disjoint.
3947 static isl_bool
isl_map_plain_is_disjoint_global(__isl_keep isl_map
*map1
,
3948 __isl_keep isl_map
*map2
)
3954 return isl_bool_error
;
3956 disjoint
= isl_map_plain_is_empty(map1
);
3957 if (disjoint
< 0 || disjoint
)
3960 disjoint
= isl_map_plain_is_empty(map2
);
3961 if (disjoint
< 0 || disjoint
)
3964 match
= isl_space_tuple_is_equal(map1
->dim
, isl_dim_in
,
3965 map2
->dim
, isl_dim_in
);
3966 if (match
< 0 || !match
)
3967 return match
< 0 ? isl_bool_error
: isl_bool_true
;
3969 match
= isl_space_tuple_is_equal(map1
->dim
, isl_dim_out
,
3970 map2
->dim
, isl_dim_out
);
3971 if (match
< 0 || !match
)
3972 return match
< 0 ? isl_bool_error
: isl_bool_true
;
3974 return isl_bool_false
;
3977 /* Are "map1" and "map2" obviously disjoint?
3979 * If one of them is empty or if they live in different spaces (ignoring
3980 * parameters), then they are clearly disjoint.
3981 * This is checked by isl_map_plain_is_disjoint_global.
3983 * If they have different parameters, then we skip any further tests.
3985 * If they are obviously equal, but not obviously empty, then we will
3986 * not be able to detect if they are disjoint.
3988 * Otherwise we check if each basic map in "map1" is obviously disjoint
3989 * from each basic map in "map2".
3991 isl_bool
isl_map_plain_is_disjoint(__isl_keep isl_map
*map1
,
3992 __isl_keep isl_map
*map2
)
3998 disjoint
= isl_map_plain_is_disjoint_global(map1
, map2
);
3999 if (disjoint
< 0 || disjoint
)
4002 match
= isl_space_match(map1
->dim
, isl_dim_param
,
4003 map2
->dim
, isl_dim_param
);
4004 if (match
< 0 || !match
)
4005 return match
< 0 ? isl_bool_error
: isl_bool_false
;
4007 intersect
= isl_map_plain_is_equal(map1
, map2
);
4008 if (intersect
< 0 || intersect
)
4009 return intersect
< 0 ? isl_bool_error
: isl_bool_false
;
4011 return all_pairs(map1
, map2
, &isl_basic_map_plain_is_disjoint
);
4014 /* Are "map1" and "map2" disjoint?
4016 * They are disjoint if they are "obviously disjoint" or if one of them
4017 * is empty. Otherwise, they are not disjoint if one of them is universal.
4018 * If the two inputs are (obviously) equal and not empty, then they are
4020 * If none of these cases apply, then check if all pairs of basic maps
4023 isl_bool
isl_map_is_disjoint(__isl_keep isl_map
*map1
, __isl_keep isl_map
*map2
)
4028 disjoint
= isl_map_plain_is_disjoint_global(map1
, map2
);
4029 if (disjoint
< 0 || disjoint
)
4032 disjoint
= isl_map_is_empty(map1
);
4033 if (disjoint
< 0 || disjoint
)
4036 disjoint
= isl_map_is_empty(map2
);
4037 if (disjoint
< 0 || disjoint
)
4040 intersect
= isl_map_plain_is_universe(map1
);
4041 if (intersect
< 0 || intersect
)
4042 return intersect
< 0 ? isl_bool_error
: isl_bool_false
;
4044 intersect
= isl_map_plain_is_universe(map2
);
4045 if (intersect
< 0 || intersect
)
4046 return intersect
< 0 ? isl_bool_error
: isl_bool_false
;
4048 intersect
= isl_map_plain_is_equal(map1
, map2
);
4049 if (intersect
< 0 || intersect
)
4050 return isl_bool_not(intersect
);
4052 return all_pairs(map1
, map2
, &isl_basic_map_is_disjoint
);
4055 /* Are "bmap1" and "bmap2" disjoint?
4057 * They are disjoint if they are "obviously disjoint" or if one of them
4058 * is empty. Otherwise, they are not disjoint if one of them is universal.
4059 * If none of these cases apply, we compute the intersection and see if
4060 * the result is empty.
4062 isl_bool
isl_basic_map_is_disjoint(__isl_keep isl_basic_map
*bmap1
,
4063 __isl_keep isl_basic_map
*bmap2
)
4067 isl_basic_map
*test
;
4069 disjoint
= isl_basic_map_plain_is_disjoint(bmap1
, bmap2
);
4070 if (disjoint
< 0 || disjoint
)
4073 disjoint
= isl_basic_map_is_empty(bmap1
);
4074 if (disjoint
< 0 || disjoint
)
4077 disjoint
= isl_basic_map_is_empty(bmap2
);
4078 if (disjoint
< 0 || disjoint
)
4081 intersect
= isl_basic_map_plain_is_universe(bmap1
);
4082 if (intersect
< 0 || intersect
)
4083 return intersect
< 0 ? isl_bool_error
: isl_bool_false
;
4085 intersect
= isl_basic_map_plain_is_universe(bmap2
);
4086 if (intersect
< 0 || intersect
)
4087 return intersect
< 0 ? isl_bool_error
: isl_bool_false
;
4089 test
= isl_basic_map_intersect(isl_basic_map_copy(bmap1
),
4090 isl_basic_map_copy(bmap2
));
4091 disjoint
= isl_basic_map_is_empty(test
);
4092 isl_basic_map_free(test
);
4097 /* Are "bset1" and "bset2" disjoint?
4099 isl_bool
isl_basic_set_is_disjoint(__isl_keep isl_basic_set
*bset1
,
4100 __isl_keep isl_basic_set
*bset2
)
4102 return isl_basic_map_is_disjoint(bset1
, bset2
);
4105 isl_bool
isl_set_plain_is_disjoint(__isl_keep isl_set
*set1
,
4106 __isl_keep isl_set
*set2
)
4108 return isl_map_plain_is_disjoint(set_to_map(set1
), set_to_map(set2
));
4111 /* Are "set1" and "set2" disjoint?
4113 isl_bool
isl_set_is_disjoint(__isl_keep isl_set
*set1
, __isl_keep isl_set
*set2
)
4115 return isl_map_is_disjoint(set1
, set2
);
4118 /* Is "v" equal to 0, 1 or -1?
4120 static int is_zero_or_one(isl_int v
)
4122 return isl_int_is_zero(v
) || isl_int_is_one(v
) || isl_int_is_negone(v
);
4125 /* Check if we can combine a given div with lower bound l and upper
4126 * bound u with some other div and if so return that other div.
4127 * Otherwise return -1.
4129 * We first check that
4130 * - the bounds are opposites of each other (except for the constant
4132 * - the bounds do not reference any other div
4133 * - no div is defined in terms of this div
4135 * Let m be the size of the range allowed on the div by the bounds.
4136 * That is, the bounds are of the form
4138 * e <= a <= e + m - 1
4140 * with e some expression in the other variables.
4141 * We look for another div b such that no third div is defined in terms
4142 * of this second div b and such that in any constraint that contains
4143 * a (except for the given lower and upper bound), also contains b
4144 * with a coefficient that is m times that of b.
4145 * That is, all constraints (execpt for the lower and upper bound)
4148 * e + f (a + m b) >= 0
4150 * Furthermore, in the constraints that only contain b, the coefficient
4151 * of b should be equal to 1 or -1.
4152 * If so, we return b so that "a + m b" can be replaced by
4153 * a single div "c = a + m b".
4155 static int div_find_coalesce(struct isl_basic_map
*bmap
, int *pairs
,
4156 unsigned div
, unsigned l
, unsigned u
)
4162 if (bmap
->n_div
<= 1)
4164 dim
= isl_space_dim(bmap
->dim
, isl_dim_all
);
4165 if (isl_seq_first_non_zero(bmap
->ineq
[l
] + 1 + dim
, div
) != -1)
4167 if (isl_seq_first_non_zero(bmap
->ineq
[l
] + 1 + dim
+ div
+ 1,
4168 bmap
->n_div
- div
- 1) != -1)
4170 if (!isl_seq_is_neg(bmap
->ineq
[l
] + 1, bmap
->ineq
[u
] + 1,
4174 for (i
= 0; i
< bmap
->n_div
; ++i
) {
4175 if (isl_int_is_zero(bmap
->div
[i
][0]))
4177 if (!isl_int_is_zero(bmap
->div
[i
][1 + 1 + dim
+ div
]))
4181 isl_int_add(bmap
->ineq
[l
][0], bmap
->ineq
[l
][0], bmap
->ineq
[u
][0]);
4182 if (isl_int_is_neg(bmap
->ineq
[l
][0])) {
4183 isl_int_sub(bmap
->ineq
[l
][0],
4184 bmap
->ineq
[l
][0], bmap
->ineq
[u
][0]);
4185 bmap
= isl_basic_map_copy(bmap
);
4186 bmap
= isl_basic_map_set_to_empty(bmap
);
4187 isl_basic_map_free(bmap
);
4190 isl_int_add_ui(bmap
->ineq
[l
][0], bmap
->ineq
[l
][0], 1);
4191 for (i
= 0; i
< bmap
->n_div
; ++i
) {
4196 for (j
= 0; j
< bmap
->n_div
; ++j
) {
4197 if (isl_int_is_zero(bmap
->div
[j
][0]))
4199 if (!isl_int_is_zero(bmap
->div
[j
][1 + 1 + dim
+ i
]))
4202 if (j
< bmap
->n_div
)
4204 for (j
= 0; j
< bmap
->n_ineq
; ++j
) {
4206 if (j
== l
|| j
== u
)
4208 if (isl_int_is_zero(bmap
->ineq
[j
][1 + dim
+ div
])) {
4209 if (is_zero_or_one(bmap
->ineq
[j
][1 + dim
+ i
]))
4213 if (isl_int_is_zero(bmap
->ineq
[j
][1 + dim
+ i
]))
4215 isl_int_mul(bmap
->ineq
[j
][1 + dim
+ div
],
4216 bmap
->ineq
[j
][1 + dim
+ div
],
4218 valid
= isl_int_eq(bmap
->ineq
[j
][1 + dim
+ div
],
4219 bmap
->ineq
[j
][1 + dim
+ i
]);
4220 isl_int_divexact(bmap
->ineq
[j
][1 + dim
+ div
],
4221 bmap
->ineq
[j
][1 + dim
+ div
],
4226 if (j
< bmap
->n_ineq
)
4231 isl_int_sub_ui(bmap
->ineq
[l
][0], bmap
->ineq
[l
][0], 1);
4232 isl_int_sub(bmap
->ineq
[l
][0], bmap
->ineq
[l
][0], bmap
->ineq
[u
][0]);
4236 /* Internal data structure used during the construction and/or evaluation of
4237 * an inequality that ensures that a pair of bounds always allows
4238 * for an integer value.
4240 * "tab" is the tableau in which the inequality is evaluated. It may
4241 * be NULL until it is actually needed.
4242 * "v" contains the inequality coefficients.
4243 * "g", "fl" and "fu" are temporary scalars used during the construction and
4246 struct test_ineq_data
{
4247 struct isl_tab
*tab
;
4254 /* Free all the memory allocated by the fields of "data".
4256 static void test_ineq_data_clear(struct test_ineq_data
*data
)
4258 isl_tab_free(data
->tab
);
4259 isl_vec_free(data
->v
);
4260 isl_int_clear(data
->g
);
4261 isl_int_clear(data
->fl
);
4262 isl_int_clear(data
->fu
);
4265 /* Is the inequality stored in data->v satisfied by "bmap"?
4266 * That is, does it only attain non-negative values?
4267 * data->tab is a tableau corresponding to "bmap".
4269 static isl_bool
test_ineq_is_satisfied(__isl_keep isl_basic_map
*bmap
,
4270 struct test_ineq_data
*data
)
4273 enum isl_lp_result res
;
4275 ctx
= isl_basic_map_get_ctx(bmap
);
4277 data
->tab
= isl_tab_from_basic_map(bmap
, 0);
4278 res
= isl_tab_min(data
->tab
, data
->v
->el
, ctx
->one
, &data
->g
, NULL
, 0);
4279 if (res
== isl_lp_error
)
4280 return isl_bool_error
;
4281 return res
== isl_lp_ok
&& isl_int_is_nonneg(data
->g
);
4284 /* Given a lower and an upper bound on div i, do they always allow
4285 * for an integer value of the given div?
4286 * Determine this property by constructing an inequality
4287 * such that the property is guaranteed when the inequality is nonnegative.
4288 * The lower bound is inequality l, while the upper bound is inequality u.
4289 * The constructed inequality is stored in data->v.
4291 * Let the upper bound be
4295 * and the lower bound
4299 * Let n_u = f_u g and n_l = f_l g, with g = gcd(n_u, n_l).
4302 * - f_u e_l <= f_u f_l g a <= f_l e_u
4304 * Since all variables are integer valued, this is equivalent to
4306 * - f_u e_l - (f_u - 1) <= f_u f_l g a <= f_l e_u + (f_l - 1)
4308 * If this interval is at least f_u f_l g, then it contains at least
4309 * one integer value for a.
4310 * That is, the test constraint is
4312 * f_l e_u + f_u e_l + f_l - 1 + f_u - 1 + 1 >= f_u f_l g
4316 * f_l e_u + f_u e_l + f_l - 1 + f_u - 1 + 1 - f_u f_l g >= 0
4318 * If the coefficients of f_l e_u + f_u e_l have a common divisor g',
4319 * then the constraint can be scaled down by a factor g',
4320 * with the constant term replaced by
4321 * floor((f_l e_{u,0} + f_u e_{l,0} + f_l - 1 + f_u - 1 + 1 - f_u f_l g)/g').
4322 * Note that the result of applying Fourier-Motzkin to this pair
4325 * f_l e_u + f_u e_l >= 0
4327 * If the constant term of the scaled down version of this constraint,
4328 * i.e., floor((f_l e_{u,0} + f_u e_{l,0})/g') is equal to the constant
4329 * term of the scaled down test constraint, then the test constraint
4330 * is known to hold and no explicit evaluation is required.
4331 * This is essentially the Omega test.
4333 * If the test constraint consists of only a constant term, then
4334 * it is sufficient to look at the sign of this constant term.
4336 static isl_bool
int_between_bounds(__isl_keep isl_basic_map
*bmap
, int i
,
4337 int l
, int u
, struct test_ineq_data
*data
)
4339 unsigned offset
, n_div
;
4340 offset
= isl_basic_map_offset(bmap
, isl_dim_div
);
4341 n_div
= isl_basic_map_dim(bmap
, isl_dim_div
);
4343 isl_int_gcd(data
->g
,
4344 bmap
->ineq
[l
][offset
+ i
], bmap
->ineq
[u
][offset
+ i
]);
4345 isl_int_divexact(data
->fl
, bmap
->ineq
[l
][offset
+ i
], data
->g
);
4346 isl_int_divexact(data
->fu
, bmap
->ineq
[u
][offset
+ i
], data
->g
);
4347 isl_int_neg(data
->fu
, data
->fu
);
4348 isl_seq_combine(data
->v
->el
, data
->fl
, bmap
->ineq
[u
],
4349 data
->fu
, bmap
->ineq
[l
], offset
+ n_div
);
4350 isl_int_mul(data
->g
, data
->g
, data
->fl
);
4351 isl_int_mul(data
->g
, data
->g
, data
->fu
);
4352 isl_int_sub(data
->g
, data
->g
, data
->fl
);
4353 isl_int_sub(data
->g
, data
->g
, data
->fu
);
4354 isl_int_add_ui(data
->g
, data
->g
, 1);
4355 isl_int_sub(data
->fl
, data
->v
->el
[0], data
->g
);
4357 isl_seq_gcd(data
->v
->el
+ 1, offset
- 1 + n_div
, &data
->g
);
4358 if (isl_int_is_zero(data
->g
))
4359 return isl_int_is_nonneg(data
->fl
);
4360 if (isl_int_is_one(data
->g
)) {
4361 isl_int_set(data
->v
->el
[0], data
->fl
);
4362 return test_ineq_is_satisfied(bmap
, data
);
4364 isl_int_fdiv_q(data
->fl
, data
->fl
, data
->g
);
4365 isl_int_fdiv_q(data
->v
->el
[0], data
->v
->el
[0], data
->g
);
4366 if (isl_int_eq(data
->fl
, data
->v
->el
[0]))
4367 return isl_bool_true
;
4368 isl_int_set(data
->v
->el
[0], data
->fl
);
4369 isl_seq_scale_down(data
->v
->el
+ 1, data
->v
->el
+ 1, data
->g
,
4370 offset
- 1 + n_div
);
4372 return test_ineq_is_satisfied(bmap
, data
);
4375 /* Remove more kinds of divs that are not strictly needed.
4376 * In particular, if all pairs of lower and upper bounds on a div
4377 * are such that they allow at least one integer value of the div,
4378 * then we can eliminate the div using Fourier-Motzkin without
4379 * introducing any spurious solutions.
4381 * If at least one of the two constraints has a unit coefficient for the div,
4382 * then the presence of such a value is guaranteed so there is no need to check.
4383 * In particular, the value attained by the bound with unit coefficient
4384 * can serve as this intermediate value.
4386 static struct isl_basic_map
*drop_more_redundant_divs(
4387 struct isl_basic_map
*bmap
, int *pairs
, int n
)
4390 struct test_ineq_data data
= { NULL
, NULL
};
4391 unsigned off
, n_div
;
4394 isl_int_init(data
.g
);
4395 isl_int_init(data
.fl
);
4396 isl_int_init(data
.fu
);
4401 ctx
= isl_basic_map_get_ctx(bmap
);
4402 off
= isl_basic_map_offset(bmap
, isl_dim_div
);
4403 n_div
= isl_basic_map_dim(bmap
, isl_dim_div
);
4404 data
.v
= isl_vec_alloc(ctx
, off
+ n_div
);
4413 for (i
= 0; i
< n_div
; ++i
) {
4416 if (best
>= 0 && pairs
[best
] <= pairs
[i
])
4422 for (l
= 0; l
< bmap
->n_ineq
; ++l
) {
4423 if (!isl_int_is_pos(bmap
->ineq
[l
][off
+ i
]))
4425 if (isl_int_is_one(bmap
->ineq
[l
][off
+ i
]))
4427 for (u
= 0; u
< bmap
->n_ineq
; ++u
) {
4428 if (!isl_int_is_neg(bmap
->ineq
[u
][off
+ i
]))
4430 if (isl_int_is_negone(bmap
->ineq
[u
][off
+ i
]))
4432 has_int
= int_between_bounds(bmap
, i
, l
, u
,
4436 if (data
.tab
&& data
.tab
->empty
)
4441 if (u
< bmap
->n_ineq
)
4444 if (data
.tab
&& data
.tab
->empty
) {
4445 bmap
= isl_basic_map_set_to_empty(bmap
);
4448 if (l
== bmap
->n_ineq
) {
4456 test_ineq_data_clear(&data
);
4463 bmap
= isl_basic_map_remove_dims(bmap
, isl_dim_div
, remove
, 1);
4464 return isl_basic_map_drop_redundant_divs(bmap
);
4467 isl_basic_map_free(bmap
);
4468 test_ineq_data_clear(&data
);
4472 /* Given a pair of divs div1 and div2 such that, except for the lower bound l
4473 * and the upper bound u, div1 always occurs together with div2 in the form
4474 * (div1 + m div2), where m is the constant range on the variable div1
4475 * allowed by l and u, replace the pair div1 and div2 by a single
4476 * div that is equal to div1 + m div2.
4478 * The new div will appear in the location that contains div2.
4479 * We need to modify all constraints that contain
4480 * div2 = (div - div1) / m
4481 * The coefficient of div2 is known to be equal to 1 or -1.
4482 * (If a constraint does not contain div2, it will also not contain div1.)
4483 * If the constraint also contains div1, then we know they appear
4484 * as f (div1 + m div2) and we can simply replace (div1 + m div2) by div,
4485 * i.e., the coefficient of div is f.
4487 * Otherwise, we first need to introduce div1 into the constraint.
4496 * A lower bound on div2
4500 * can be replaced by
4502 * m div2 + div1 + m t + f >= 0
4508 * can be replaced by
4510 * -(m div2 + div1) + m t + f' >= 0
4512 * These constraint are those that we would obtain from eliminating
4513 * div1 using Fourier-Motzkin.
4515 * After all constraints have been modified, we drop the lower and upper
4516 * bound and then drop div1.
4518 static struct isl_basic_map
*coalesce_divs(struct isl_basic_map
*bmap
,
4519 unsigned div1
, unsigned div2
, unsigned l
, unsigned u
)
4523 unsigned dim
, total
;
4526 ctx
= isl_basic_map_get_ctx(bmap
);
4528 dim
= isl_space_dim(bmap
->dim
, isl_dim_all
);
4529 total
= 1 + dim
+ bmap
->n_div
;
4532 isl_int_add(m
, bmap
->ineq
[l
][0], bmap
->ineq
[u
][0]);
4533 isl_int_add_ui(m
, m
, 1);
4535 for (i
= 0; i
< bmap
->n_ineq
; ++i
) {
4536 if (i
== l
|| i
== u
)
4538 if (isl_int_is_zero(bmap
->ineq
[i
][1 + dim
+ div2
]))
4540 if (isl_int_is_zero(bmap
->ineq
[i
][1 + dim
+ div1
])) {
4541 if (isl_int_is_pos(bmap
->ineq
[i
][1 + dim
+ div2
]))
4542 isl_seq_combine(bmap
->ineq
[i
], m
, bmap
->ineq
[i
],
4543 ctx
->one
, bmap
->ineq
[l
], total
);
4545 isl_seq_combine(bmap
->ineq
[i
], m
, bmap
->ineq
[i
],
4546 ctx
->one
, bmap
->ineq
[u
], total
);
4548 isl_int_set(bmap
->ineq
[i
][1 + dim
+ div2
],
4549 bmap
->ineq
[i
][1 + dim
+ div1
]);
4550 isl_int_set_si(bmap
->ineq
[i
][1 + dim
+ div1
], 0);
4555 isl_basic_map_drop_inequality(bmap
, l
);
4556 isl_basic_map_drop_inequality(bmap
, u
);
4558 isl_basic_map_drop_inequality(bmap
, u
);
4559 isl_basic_map_drop_inequality(bmap
, l
);
4561 bmap
= isl_basic_map_drop_div(bmap
, div1
);
4565 /* First check if we can coalesce any pair of divs and
4566 * then continue with dropping more redundant divs.
4568 * We loop over all pairs of lower and upper bounds on a div
4569 * with coefficient 1 and -1, respectively, check if there
4570 * is any other div "c" with which we can coalesce the div
4571 * and if so, perform the coalescing.
4573 static struct isl_basic_map
*coalesce_or_drop_more_redundant_divs(
4574 struct isl_basic_map
*bmap
, int *pairs
, int n
)
4579 dim
= isl_space_dim(bmap
->dim
, isl_dim_all
);
4581 for (i
= 0; i
< bmap
->n_div
; ++i
) {
4584 for (l
= 0; l
< bmap
->n_ineq
; ++l
) {
4585 if (!isl_int_is_one(bmap
->ineq
[l
][1 + dim
+ i
]))
4587 for (u
= 0; u
< bmap
->n_ineq
; ++u
) {
4590 if (!isl_int_is_negone(bmap
->ineq
[u
][1+dim
+i
]))
4592 c
= div_find_coalesce(bmap
, pairs
, i
, l
, u
);
4596 bmap
= coalesce_divs(bmap
, i
, c
, l
, u
);
4597 return isl_basic_map_drop_redundant_divs(bmap
);
4602 if (ISL_F_ISSET(bmap
, ISL_BASIC_MAP_EMPTY
)) {
4607 return drop_more_redundant_divs(bmap
, pairs
, n
);
4610 /* Are the "n" coefficients starting at "first" of inequality constraints
4611 * "i" and "j" of "bmap" equal to each other?
4613 static int is_parallel_part(__isl_keep isl_basic_map
*bmap
, int i
, int j
,
4616 return isl_seq_eq(bmap
->ineq
[i
] + first
, bmap
->ineq
[j
] + first
, n
);
4619 /* Are the "n" coefficients starting at "first" of inequality constraints
4620 * "i" and "j" of "bmap" opposite to each other?
4622 static int is_opposite_part(__isl_keep isl_basic_map
*bmap
, int i
, int j
,
4625 return isl_seq_is_neg(bmap
->ineq
[i
] + first
, bmap
->ineq
[j
] + first
, n
);
4628 /* Are inequality constraints "i" and "j" of "bmap" opposite to each other,
4629 * apart from the constant term?
4631 static int is_opposite(__isl_keep isl_basic_map
*bmap
, int i
, int j
)
4635 total
= isl_basic_map_dim(bmap
, isl_dim_all
);
4636 return is_opposite_part(bmap
, i
, j
, 1, total
);
4639 /* Are inequality constraints "i" and "j" of "bmap" equal to each other,
4640 * apart from the constant term and the coefficient at position "pos"?
4642 static int is_parallel_except(__isl_keep isl_basic_map
*bmap
, int i
, int j
,
4647 total
= isl_basic_map_dim(bmap
, isl_dim_all
);
4648 return is_parallel_part(bmap
, i
, j
, 1, pos
- 1) &&
4649 is_parallel_part(bmap
, i
, j
, pos
+ 1, total
- pos
);
4652 /* Are inequality constraints "i" and "j" of "bmap" opposite to each other,
4653 * apart from the constant term and the coefficient at position "pos"?
4655 static int is_opposite_except(__isl_keep isl_basic_map
*bmap
, int i
, int j
,
4660 total
= isl_basic_map_dim(bmap
, isl_dim_all
);
4661 return is_opposite_part(bmap
, i
, j
, 1, pos
- 1) &&
4662 is_opposite_part(bmap
, i
, j
, pos
+ 1, total
- pos
);
4665 /* Restart isl_basic_map_drop_redundant_divs after "bmap" has
4666 * been modified, simplying it if "simplify" is set.
4667 * Free the temporary data structure "pairs" that was associated
4668 * to the old version of "bmap".
4670 static __isl_give isl_basic_map
*drop_redundant_divs_again(
4671 __isl_take isl_basic_map
*bmap
, __isl_take
int *pairs
, int simplify
)
4674 bmap
= isl_basic_map_simplify(bmap
);
4676 return isl_basic_map_drop_redundant_divs(bmap
);
4679 /* Is "div" the single unknown existentially quantified variable
4680 * in inequality constraint "ineq" of "bmap"?
4681 * "div" is known to have a non-zero coefficient in "ineq".
4683 static int single_unknown(__isl_keep isl_basic_map
*bmap
, int ineq
, int div
)
4686 unsigned n_div
, o_div
;
4688 if (isl_basic_map_div_is_known(bmap
, div
))
4690 n_div
= isl_basic_map_dim(bmap
, isl_dim_div
);
4693 o_div
= isl_basic_map_offset(bmap
, isl_dim_div
);
4694 for (i
= 0; i
< n_div
; ++i
) {
4697 if (isl_int_is_zero(bmap
->ineq
[ineq
][o_div
+ i
]))
4699 if (!isl_basic_map_div_is_known(bmap
, i
))
4706 /* Does integer division "div" have coefficient 1 in inequality constraint
4709 static int has_coef_one(__isl_keep isl_basic_map
*bmap
, int div
, int ineq
)
4713 o_div
= isl_basic_map_offset(bmap
, isl_dim_div
);
4714 if (isl_int_is_one(bmap
->ineq
[ineq
][o_div
+ div
]))
4720 /* Turn inequality constraint "ineq" of "bmap" into an equality and
4721 * then try and drop redundant divs again,
4722 * freeing the temporary data structure "pairs" that was associated
4723 * to the old version of "bmap".
4725 static __isl_give isl_basic_map
*set_eq_and_try_again(
4726 __isl_take isl_basic_map
*bmap
, int ineq
, __isl_take
int *pairs
)
4728 bmap
= isl_basic_map_cow(bmap
);
4729 isl_basic_map_inequality_to_equality(bmap
, ineq
);
4730 return drop_redundant_divs_again(bmap
, pairs
, 1);
4733 /* Drop the integer division at position "div", along with the two
4734 * inequality constraints "ineq1" and "ineq2" in which it appears
4735 * from "bmap" and then try and drop redundant divs again,
4736 * freeing the temporary data structure "pairs" that was associated
4737 * to the old version of "bmap".
4739 static __isl_give isl_basic_map
*drop_div_and_try_again(
4740 __isl_take isl_basic_map
*bmap
, int div
, int ineq1
, int ineq2
,
4741 __isl_take
int *pairs
)
4743 if (ineq1
> ineq2
) {
4744 isl_basic_map_drop_inequality(bmap
, ineq1
);
4745 isl_basic_map_drop_inequality(bmap
, ineq2
);
4747 isl_basic_map_drop_inequality(bmap
, ineq2
);
4748 isl_basic_map_drop_inequality(bmap
, ineq1
);
4750 bmap
= isl_basic_map_drop_div(bmap
, div
);
4751 return drop_redundant_divs_again(bmap
, pairs
, 0);
4754 /* Given two inequality constraints
4756 * f(x) + n d + c >= 0, (ineq)
4758 * with d the variable at position "pos", and
4760 * f(x) + c0 >= 0, (lower)
4762 * compute the maximal value of the lower bound ceil((-f(x) - c)/n)
4763 * determined by the first constraint.
4770 static void lower_bound_from_parallel(__isl_keep isl_basic_map
*bmap
,
4771 int ineq
, int lower
, int pos
, isl_int
*l
)
4773 isl_int_neg(*l
, bmap
->ineq
[ineq
][0]);
4774 isl_int_add(*l
, *l
, bmap
->ineq
[lower
][0]);
4775 isl_int_cdiv_q(*l
, *l
, bmap
->ineq
[ineq
][pos
]);
4778 /* Given two inequality constraints
4780 * f(x) + n d + c >= 0, (ineq)
4782 * with d the variable at position "pos", and
4784 * -f(x) - c0 >= 0, (upper)
4786 * compute the minimal value of the lower bound ceil((-f(x) - c)/n)
4787 * determined by the first constraint.
4794 static void lower_bound_from_opposite(__isl_keep isl_basic_map
*bmap
,
4795 int ineq
, int upper
, int pos
, isl_int
*u
)
4797 isl_int_neg(*u
, bmap
->ineq
[ineq
][0]);
4798 isl_int_sub(*u
, *u
, bmap
->ineq
[upper
][0]);
4799 isl_int_cdiv_q(*u
, *u
, bmap
->ineq
[ineq
][pos
]);
4802 /* Given a lower bound constraint "ineq" on "div" in "bmap",
4803 * does the corresponding lower bound have a fixed value in "bmap"?
4805 * In particular, "ineq" is of the form
4807 * f(x) + n d + c >= 0
4809 * with n > 0, c the constant term and
4810 * d the existentially quantified variable "div".
4811 * That is, the lower bound is
4813 * ceil((-f(x) - c)/n)
4815 * Look for a pair of constraints
4820 * i.e., -c1 <= -f(x) <= c0, that fix ceil((-f(x) - c)/n) to a constant value.
4821 * That is, check that
4823 * ceil((-c1 - c)/n) = ceil((c0 - c)/n)
4825 * If so, return the index of inequality f(x) + c0 >= 0.
4826 * Otherwise, return -1.
4828 static int lower_bound_is_cst(__isl_keep isl_basic_map
*bmap
, int div
, int ineq
)
4831 int lower
= -1, upper
= -1;
4832 unsigned o_div
, n_div
;
4836 n_div
= isl_basic_map_dim(bmap
, isl_dim_div
);
4837 o_div
= isl_basic_map_offset(bmap
, isl_dim_div
);
4838 for (i
= 0; i
< bmap
->n_ineq
&& (lower
< 0 || upper
< 0); ++i
) {
4841 if (!isl_int_is_zero(bmap
->ineq
[i
][o_div
+ div
]))
4844 is_parallel_except(bmap
, ineq
, i
, o_div
+ div
)) {
4849 is_opposite_except(bmap
, ineq
, i
, o_div
+ div
)) {
4854 if (lower
< 0 || upper
< 0)
4860 lower_bound_from_parallel(bmap
, ineq
, lower
, o_div
+ div
, &l
);
4861 lower_bound_from_opposite(bmap
, ineq
, upper
, o_div
+ div
, &u
);
4863 equal
= isl_int_eq(l
, u
);
4868 return equal
? lower
: -1;
4871 /* Given a lower bound constraint "ineq" on the existentially quantified
4872 * variable "div", such that the corresponding lower bound has
4873 * a fixed value in "bmap", assign this fixed value to the variable and
4874 * then try and drop redundant divs again,
4875 * freeing the temporary data structure "pairs" that was associated
4876 * to the old version of "bmap".
4877 * "lower" determines the constant value for the lower bound.
4879 * In particular, "ineq" is of the form
4881 * f(x) + n d + c >= 0,
4883 * while "lower" is of the form
4887 * The lower bound is ceil((-f(x) - c)/n) and its constant value
4888 * is ceil((c0 - c)/n).
4890 static __isl_give isl_basic_map
*fix_cst_lower(__isl_take isl_basic_map
*bmap
,
4891 int div
, int ineq
, int lower
, int *pairs
)
4898 o_div
= isl_basic_map_offset(bmap
, isl_dim_div
);
4899 lower_bound_from_parallel(bmap
, ineq
, lower
, o_div
+ div
, &c
);
4900 bmap
= isl_basic_map_fix(bmap
, isl_dim_div
, div
, c
);
4905 return isl_basic_map_drop_redundant_divs(bmap
);
4908 /* Remove divs that are not strictly needed based on the inequality
4910 * In particular, if a div only occurs positively (or negatively)
4911 * in constraints, then it can simply be dropped.
4912 * Also, if a div occurs in only two constraints and if moreover
4913 * those two constraints are opposite to each other, except for the constant
4914 * term and if the sum of the constant terms is such that for any value
4915 * of the other values, there is always at least one integer value of the
4916 * div, i.e., if one plus this sum is greater than or equal to
4917 * the (absolute value) of the coefficient of the div in the constraints,
4918 * then we can also simply drop the div.
4920 * If an existentially quantified variable does not have an explicit
4921 * representation, appears in only a single lower bound that does not
4922 * involve any other such existentially quantified variables and appears
4923 * in this lower bound with coefficient 1,
4924 * then fix the variable to the value of the lower bound. That is,
4925 * turn the inequality into an equality.
4926 * If for any value of the other variables, there is any value
4927 * for the existentially quantified variable satisfying the constraints,
4928 * then this lower bound also satisfies the constraints.
4929 * It is therefore safe to pick this lower bound.
4931 * The same reasoning holds even if the coefficient is not one.
4932 * However, fixing the variable to the value of the lower bound may
4933 * in general introduce an extra integer division, in which case
4934 * it may be better to pick another value.
4935 * If this integer division has a known constant value, then plugging
4936 * in this constant value removes the existentially quantified variable
4937 * completely. In particular, if the lower bound is of the form
4938 * ceil((-f(x) - c)/n) and there are two constraints, f(x) + c0 >= 0 and
4939 * -f(x) + c1 >= 0 such that ceil((-c1 - c)/n) = ceil((c0 - c)/n),
4940 * then the existentially quantified variable can be assigned this
4943 * We skip divs that appear in equalities or in the definition of other divs.
4944 * Divs that appear in the definition of other divs usually occur in at least
4945 * 4 constraints, but the constraints may have been simplified.
4947 * If any divs are left after these simple checks then we move on
4948 * to more complicated cases in drop_more_redundant_divs.
4950 static __isl_give isl_basic_map
*isl_basic_map_drop_redundant_divs_ineq(
4951 __isl_take isl_basic_map
*bmap
)
4960 if (bmap
->n_div
== 0)
4963 off
= isl_space_dim(bmap
->dim
, isl_dim_all
);
4964 pairs
= isl_calloc_array(bmap
->ctx
, int, bmap
->n_div
);
4968 for (i
= 0; i
< bmap
->n_div
; ++i
) {
4970 int last_pos
, last_neg
;
4974 defined
= !isl_int_is_zero(bmap
->div
[i
][0]);
4975 for (j
= i
; j
< bmap
->n_div
; ++j
)
4976 if (!isl_int_is_zero(bmap
->div
[j
][1 + 1 + off
+ i
]))
4978 if (j
< bmap
->n_div
)
4980 for (j
= 0; j
< bmap
->n_eq
; ++j
)
4981 if (!isl_int_is_zero(bmap
->eq
[j
][1 + off
+ i
]))
4987 for (j
= 0; j
< bmap
->n_ineq
; ++j
) {
4988 if (isl_int_is_pos(bmap
->ineq
[j
][1 + off
+ i
])) {
4992 if (isl_int_is_neg(bmap
->ineq
[j
][1 + off
+ i
])) {
4997 pairs
[i
] = pos
* neg
;
4998 if (pairs
[i
] == 0) {
4999 for (j
= bmap
->n_ineq
- 1; j
>= 0; --j
)
5000 if (!isl_int_is_zero(bmap
->ineq
[j
][1+off
+i
]))
5001 isl_basic_map_drop_inequality(bmap
, j
);
5002 bmap
= isl_basic_map_drop_div(bmap
, i
);
5003 return drop_redundant_divs_again(bmap
, pairs
, 0);
5005 if (pairs
[i
] != 1 || !is_opposite(bmap
, last_pos
, last_neg
)) {
5009 single
= single_unknown(bmap
, last_pos
, i
);
5012 if (has_coef_one(bmap
, i
, last_pos
))
5013 return set_eq_and_try_again(bmap
, last_pos
,
5015 lower
= lower_bound_is_cst(bmap
, i
, last_pos
);
5017 return fix_cst_lower(bmap
, i
, last_pos
, lower
,
5022 isl_int_add(bmap
->ineq
[last_pos
][0],
5023 bmap
->ineq
[last_pos
][0], bmap
->ineq
[last_neg
][0]);
5024 isl_int_add_ui(bmap
->ineq
[last_pos
][0],
5025 bmap
->ineq
[last_pos
][0], 1);
5026 redundant
= isl_int_ge(bmap
->ineq
[last_pos
][0],
5027 bmap
->ineq
[last_pos
][1+off
+i
]);
5028 isl_int_sub_ui(bmap
->ineq
[last_pos
][0],
5029 bmap
->ineq
[last_pos
][0], 1);
5030 isl_int_sub(bmap
->ineq
[last_pos
][0],
5031 bmap
->ineq
[last_pos
][0], bmap
->ineq
[last_neg
][0]);
5033 return drop_div_and_try_again(bmap
, i
,
5034 last_pos
, last_neg
, pairs
);
5035 if (!defined
&& ok_to_set_div_from_bound(bmap
, i
, last_pos
)) {
5036 bmap
= set_div_from_lower_bound(bmap
, i
, last_pos
);
5037 return drop_redundant_divs_again(bmap
, pairs
, 1);
5044 return coalesce_or_drop_more_redundant_divs(bmap
, pairs
, n
);
5050 isl_basic_map_free(bmap
);
5054 /* Consider the coefficients at "c" as a row vector and replace
5055 * them with their product with "T". "T" is assumed to be a square matrix.
5057 static isl_stat
preimage(isl_int
*c
, __isl_keep isl_mat
*T
)
5064 return isl_stat_error
;
5065 n
= isl_mat_rows(T
);
5066 if (isl_seq_first_non_zero(c
, n
) == -1)
5068 ctx
= isl_mat_get_ctx(T
);
5069 v
= isl_vec_alloc(ctx
, n
);
5071 return isl_stat_error
;
5072 isl_seq_swp_or_cpy(v
->el
, c
, n
);
5073 v
= isl_vec_mat_product(v
, isl_mat_copy(T
));
5075 return isl_stat_error
;
5076 isl_seq_swp_or_cpy(c
, v
->el
, n
);
5082 /* Plug in T for the variables in "bmap" starting at "pos".
5083 * T is a linear unimodular matrix, i.e., without constant term.
5085 static __isl_give isl_basic_map
*isl_basic_map_preimage_vars(
5086 __isl_take isl_basic_map
*bmap
, unsigned pos
, __isl_take isl_mat
*T
)
5091 bmap
= isl_basic_map_cow(bmap
);
5095 n
= isl_mat_cols(T
);
5096 if (n
!= isl_mat_rows(T
))
5097 isl_die(isl_mat_get_ctx(T
), isl_error_invalid
,
5098 "expecting square matrix", goto error
);
5100 total
= isl_basic_map_dim(bmap
, isl_dim_all
);
5101 if (pos
+ n
> total
|| pos
+ n
< pos
)
5102 isl_die(isl_mat_get_ctx(T
), isl_error_invalid
,
5103 "invalid range", goto error
);
5105 for (i
= 0; i
< bmap
->n_eq
; ++i
)
5106 if (preimage(bmap
->eq
[i
] + 1 + pos
, T
) < 0)
5108 for (i
= 0; i
< bmap
->n_ineq
; ++i
)
5109 if (preimage(bmap
->ineq
[i
] + 1 + pos
, T
) < 0)
5111 for (i
= 0; i
< bmap
->n_div
; ++i
) {
5112 if (isl_basic_map_div_is_marked_unknown(bmap
, i
))
5114 if (preimage(bmap
->div
[i
] + 1 + 1 + pos
, T
) < 0)
5121 isl_basic_map_free(bmap
);
5126 /* Remove divs that are not strictly needed.
5128 * First look for an equality constraint involving two or more
5129 * existentially quantified variables without an explicit
5130 * representation. Replace the combination that appears
5131 * in the equality constraint by a single existentially quantified
5132 * variable such that the equality can be used to derive
5133 * an explicit representation for the variable.
5134 * If there are no more such equality constraints, then continue
5135 * with isl_basic_map_drop_redundant_divs_ineq.
5137 * In particular, if the equality constraint is of the form
5139 * f(x) + \sum_i c_i a_i = 0
5141 * with a_i existentially quantified variable without explicit
5142 * representation, then apply a transformation on the existentially
5143 * quantified variables to turn the constraint into
5147 * with g the gcd of the c_i.
5148 * In order to easily identify which existentially quantified variables
5149 * have a complete explicit representation, i.e., without being defined
5150 * in terms of other existentially quantified variables without
5151 * an explicit representation, the existentially quantified variables
5154 * The variable transformation is computed by extending the row
5155 * [c_1/g ... c_n/g] to a unimodular matrix, obtaining the transformation
5157 * [a_1'] [c_1/g ... c_n/g] [ a_1 ]
5162 * with [c_1/g ... c_n/g] representing the first row of U.
5163 * The inverse of U is then plugged into the original constraints.
5164 * The call to isl_basic_map_simplify makes sure the explicit
5165 * representation for a_1' is extracted from the equality constraint.
5167 __isl_give isl_basic_map
*isl_basic_map_drop_redundant_divs(
5168 __isl_take isl_basic_map
*bmap
)
5172 unsigned o_div
, n_div
;
5179 if (isl_basic_map_divs_known(bmap
))
5180 return isl_basic_map_drop_redundant_divs_ineq(bmap
);
5181 if (bmap
->n_eq
== 0)
5182 return isl_basic_map_drop_redundant_divs_ineq(bmap
);
5183 bmap
= isl_basic_map_sort_divs(bmap
);
5187 first
= isl_basic_map_first_unknown_div(bmap
);
5189 return isl_basic_map_free(bmap
);
5191 o_div
= isl_basic_map_offset(bmap
, isl_dim_div
);
5192 n_div
= isl_basic_map_dim(bmap
, isl_dim_div
);
5194 for (i
= 0; i
< bmap
->n_eq
; ++i
) {
5195 l
= isl_seq_first_non_zero(bmap
->eq
[i
] + o_div
+ first
,
5200 if (isl_seq_first_non_zero(bmap
->eq
[i
] + o_div
+ l
+ 1,
5201 n_div
- (l
+ 1)) == -1)
5205 if (i
>= bmap
->n_eq
)
5206 return isl_basic_map_drop_redundant_divs_ineq(bmap
);
5208 ctx
= isl_basic_map_get_ctx(bmap
);
5209 T
= isl_mat_alloc(ctx
, n_div
- l
, n_div
- l
);
5211 return isl_basic_map_free(bmap
);
5212 isl_seq_cpy(T
->row
[0], bmap
->eq
[i
] + o_div
+ l
, n_div
- l
);
5213 T
= isl_mat_normalize_row(T
, 0);
5214 T
= isl_mat_unimodular_complete(T
, 1);
5215 T
= isl_mat_right_inverse(T
);
5217 for (i
= l
; i
< n_div
; ++i
)
5218 bmap
= isl_basic_map_mark_div_unknown(bmap
, i
);
5219 bmap
= isl_basic_map_preimage_vars(bmap
, o_div
- 1 + l
, T
);
5220 bmap
= isl_basic_map_simplify(bmap
);
5222 return isl_basic_map_drop_redundant_divs(bmap
);
5225 struct isl_basic_set
*isl_basic_set_drop_redundant_divs(
5226 struct isl_basic_set
*bset
)
5228 isl_basic_map
*bmap
= bset_to_bmap(bset
);
5229 return bset_from_bmap(isl_basic_map_drop_redundant_divs(bmap
));
5232 struct isl_map
*isl_map_drop_redundant_divs(struct isl_map
*map
)
5238 for (i
= 0; i
< map
->n
; ++i
) {
5239 map
->p
[i
] = isl_basic_map_drop_redundant_divs(map
->p
[i
]);
5243 ISL_F_CLR(map
, ISL_MAP_NORMALIZED
);
5250 struct isl_set
*isl_set_drop_redundant_divs(struct isl_set
*set
)
5252 return set_from_map(isl_map_drop_redundant_divs(set_to_map(set
)));
5255 /* Does "bmap" satisfy any equality that involves more than 2 variables
5256 * and/or has coefficients different from -1 and 1?
5258 static int has_multiple_var_equality(__isl_keep isl_basic_map
*bmap
)
5263 total
= isl_basic_map_dim(bmap
, isl_dim_all
);
5265 for (i
= 0; i
< bmap
->n_eq
; ++i
) {
5268 j
= isl_seq_first_non_zero(bmap
->eq
[i
] + 1, total
);
5271 if (!isl_int_is_one(bmap
->eq
[i
][1 + j
]) &&
5272 !isl_int_is_negone(bmap
->eq
[i
][1 + j
]))
5276 k
= isl_seq_first_non_zero(bmap
->eq
[i
] + 1 + j
, total
- j
);
5280 if (!isl_int_is_one(bmap
->eq
[i
][1 + j
]) &&
5281 !isl_int_is_negone(bmap
->eq
[i
][1 + j
]))
5285 k
= isl_seq_first_non_zero(bmap
->eq
[i
] + 1 + j
, total
- j
);
5293 /* Remove any common factor g from the constraint coefficients in "v".
5294 * The constant term is stored in the first position and is replaced
5295 * by floor(c/g). If any common factor is removed and if this results
5296 * in a tightening of the constraint, then set *tightened.
5298 static __isl_give isl_vec
*normalize_constraint(__isl_take isl_vec
*v
,
5305 ctx
= isl_vec_get_ctx(v
);
5306 isl_seq_gcd(v
->el
+ 1, v
->size
- 1, &ctx
->normalize_gcd
);
5307 if (isl_int_is_zero(ctx
->normalize_gcd
))
5309 if (isl_int_is_one(ctx
->normalize_gcd
))
5314 if (tightened
&& !isl_int_is_divisible_by(v
->el
[0], ctx
->normalize_gcd
))
5316 isl_int_fdiv_q(v
->el
[0], v
->el
[0], ctx
->normalize_gcd
);
5317 isl_seq_scale_down(v
->el
+ 1, v
->el
+ 1, ctx
->normalize_gcd
,
5322 /* If "bmap" is an integer set that satisfies any equality involving
5323 * more than 2 variables and/or has coefficients different from -1 and 1,
5324 * then use variable compression to reduce the coefficients by removing
5325 * any (hidden) common factor.
5326 * In particular, apply the variable compression to each constraint,
5327 * factor out any common factor in the non-constant coefficients and
5328 * then apply the inverse of the compression.
5329 * At the end, we mark the basic map as having reduced constants.
5330 * If this flag is still set on the next invocation of this function,
5331 * then we skip the computation.
5333 * Removing a common factor may result in a tightening of some of
5334 * the constraints. If this happens, then we may end up with two
5335 * opposite inequalities that can be replaced by an equality.
5336 * We therefore call isl_basic_map_detect_inequality_pairs,
5337 * which checks for such pairs of inequalities as well as eliminate_divs_eq
5338 * and isl_basic_map_gauss if such a pair was found.
5340 __isl_give isl_basic_map
*isl_basic_map_reduce_coefficients(
5341 __isl_take isl_basic_map
*bmap
)
5346 isl_mat
*eq
, *T
, *T2
;
5352 if (ISL_F_ISSET(bmap
, ISL_BASIC_MAP_REDUCED_COEFFICIENTS
))
5354 if (isl_basic_map_is_rational(bmap
))
5356 if (bmap
->n_eq
== 0)
5358 if (!has_multiple_var_equality(bmap
))
5361 total
= isl_basic_map_dim(bmap
, isl_dim_all
);
5362 ctx
= isl_basic_map_get_ctx(bmap
);
5363 v
= isl_vec_alloc(ctx
, 1 + total
);
5365 return isl_basic_map_free(bmap
);
5367 eq
= isl_mat_sub_alloc6(ctx
, bmap
->eq
, 0, bmap
->n_eq
, 0, 1 + total
);
5368 T
= isl_mat_variable_compression(eq
, &T2
);
5371 if (T
->n_col
== 0) {
5375 return isl_basic_map_set_to_empty(bmap
);
5379 for (i
= 0; i
< bmap
->n_ineq
; ++i
) {
5380 isl_seq_cpy(v
->el
, bmap
->ineq
[i
], 1 + total
);
5381 v
= isl_vec_mat_product(v
, isl_mat_copy(T
));
5382 v
= normalize_constraint(v
, &tightened
);
5383 v
= isl_vec_mat_product(v
, isl_mat_copy(T2
));
5386 isl_seq_cpy(bmap
->ineq
[i
], v
->el
, 1 + total
);
5393 ISL_F_SET(bmap
, ISL_BASIC_MAP_REDUCED_COEFFICIENTS
);
5398 bmap
= isl_basic_map_detect_inequality_pairs(bmap
, &progress
);
5400 bmap
= eliminate_divs_eq(bmap
, &progress
);
5401 bmap
= isl_basic_map_gauss(bmap
, NULL
);
5410 return isl_basic_map_free(bmap
);
5413 /* Shift the integer division at position "div" of "bmap"
5414 * by "shift" times the variable at position "pos".
5415 * "pos" is as determined by isl_basic_map_offset, i.e., pos == 0
5416 * corresponds to the constant term.
5418 * That is, if the integer division has the form
5422 * then replace it by
5424 * floor((f(x) + shift * d * x_pos)/d) - shift * x_pos
5426 __isl_give isl_basic_map
*isl_basic_map_shift_div(
5427 __isl_take isl_basic_map
*bmap
, int div
, int pos
, isl_int shift
)
5432 if (isl_int_is_zero(shift
))
5437 total
= isl_basic_map_dim(bmap
, isl_dim_all
);
5438 total
-= isl_basic_map_dim(bmap
, isl_dim_div
);
5440 isl_int_addmul(bmap
->div
[div
][1 + pos
], shift
, bmap
->div
[div
][0]);
5442 for (i
= 0; i
< bmap
->n_eq
; ++i
) {
5443 if (isl_int_is_zero(bmap
->eq
[i
][1 + total
+ div
]))
5445 isl_int_submul(bmap
->eq
[i
][pos
],
5446 shift
, bmap
->eq
[i
][1 + total
+ div
]);
5448 for (i
= 0; i
< bmap
->n_ineq
; ++i
) {
5449 if (isl_int_is_zero(bmap
->ineq
[i
][1 + total
+ div
]))
5451 isl_int_submul(bmap
->ineq
[i
][pos
],
5452 shift
, bmap
->ineq
[i
][1 + total
+ div
]);
5454 for (i
= 0; i
< bmap
->n_div
; ++i
) {
5455 if (isl_int_is_zero(bmap
->div
[i
][0]))
5457 if (isl_int_is_zero(bmap
->div
[i
][1 + 1 + total
+ div
]))
5459 isl_int_submul(bmap
->div
[i
][1 + pos
],
5460 shift
, bmap
->div
[i
][1 + 1 + total
+ div
]);