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 isl_basic_map_free_div(bmap
, n
);
205 bmap
->dim
= isl_space_drop_dims(bmap
->dim
, type
, first
, n
);
209 ISL_F_CLR(bmap
, ISL_BASIC_MAP_NORMALIZED
);
210 bmap
= isl_basic_map_simplify(bmap
);
211 return isl_basic_map_finalize(bmap
);
213 isl_basic_map_free(bmap
);
217 __isl_give isl_basic_set
*isl_basic_set_drop(__isl_take isl_basic_set
*bset
,
218 enum isl_dim_type type
, unsigned first
, unsigned n
)
220 return bset_from_bmap(isl_basic_map_drop(bset_to_bmap(bset
),
224 struct isl_basic_map
*isl_basic_map_drop_inputs(
225 struct isl_basic_map
*bmap
, unsigned first
, unsigned n
)
227 return isl_basic_map_drop(bmap
, isl_dim_in
, first
, n
);
230 struct isl_map
*isl_map_drop(struct isl_map
*map
,
231 enum isl_dim_type type
, unsigned first
, unsigned n
)
238 isl_assert(map
->ctx
, first
+ n
<= isl_map_dim(map
, type
), goto error
);
240 if (n
== 0 && !isl_space_get_tuple_name(map
->dim
, type
))
242 map
= isl_map_cow(map
);
245 map
->dim
= isl_space_drop_dims(map
->dim
, type
, first
, n
);
249 for (i
= 0; i
< map
->n
; ++i
) {
250 map
->p
[i
] = isl_basic_map_drop(map
->p
[i
], type
, first
, n
);
254 ISL_F_CLR(map
, ISL_MAP_NORMALIZED
);
262 struct isl_set
*isl_set_drop(struct isl_set
*set
,
263 enum isl_dim_type type
, unsigned first
, unsigned n
)
265 return set_from_map(isl_map_drop(set_to_map(set
), type
, first
, n
));
268 struct isl_map
*isl_map_drop_inputs(
269 struct isl_map
*map
, unsigned first
, unsigned n
)
271 return isl_map_drop(map
, isl_dim_in
, first
, n
);
275 * We don't cow, as the div is assumed to be redundant.
277 __isl_give isl_basic_map
*isl_basic_map_drop_div(
278 __isl_take isl_basic_map
*bmap
, unsigned div
)
286 pos
= 1 + isl_space_dim(bmap
->dim
, isl_dim_all
) + div
;
288 isl_assert(bmap
->ctx
, div
< bmap
->n_div
, goto error
);
290 for (i
= 0; i
< bmap
->n_eq
; ++i
)
291 constraint_drop_vars(bmap
->eq
[i
]+pos
, 1, bmap
->extra
-div
-1);
293 for (i
= 0; i
< bmap
->n_ineq
; ++i
) {
294 if (!isl_int_is_zero(bmap
->ineq
[i
][pos
])) {
295 isl_basic_map_drop_inequality(bmap
, i
);
299 constraint_drop_vars(bmap
->ineq
[i
]+pos
, 1, bmap
->extra
-div
-1);
302 for (i
= 0; i
< bmap
->n_div
; ++i
)
303 constraint_drop_vars(bmap
->div
[i
]+1+pos
, 1, bmap
->extra
-div
-1);
305 if (div
!= bmap
->n_div
- 1) {
307 isl_int
*t
= bmap
->div
[div
];
309 for (j
= div
; j
< bmap
->n_div
- 1; ++j
)
310 bmap
->div
[j
] = bmap
->div
[j
+1];
312 bmap
->div
[bmap
->n_div
- 1] = t
;
314 ISL_F_CLR(bmap
, ISL_BASIC_MAP_NORMALIZED
);
315 isl_basic_map_free_div(bmap
, 1);
319 isl_basic_map_free(bmap
);
323 struct isl_basic_map
*isl_basic_map_normalize_constraints(
324 struct isl_basic_map
*bmap
)
328 unsigned total
= isl_basic_map_total_dim(bmap
);
334 for (i
= bmap
->n_eq
- 1; i
>= 0; --i
) {
335 isl_seq_gcd(bmap
->eq
[i
]+1, total
, &gcd
);
336 if (isl_int_is_zero(gcd
)) {
337 if (!isl_int_is_zero(bmap
->eq
[i
][0])) {
338 bmap
= isl_basic_map_set_to_empty(bmap
);
341 isl_basic_map_drop_equality(bmap
, i
);
344 if (ISL_F_ISSET(bmap
, ISL_BASIC_MAP_RATIONAL
))
345 isl_int_gcd(gcd
, gcd
, bmap
->eq
[i
][0]);
346 if (isl_int_is_one(gcd
))
348 if (!isl_int_is_divisible_by(bmap
->eq
[i
][0], gcd
)) {
349 bmap
= isl_basic_map_set_to_empty(bmap
);
352 isl_seq_scale_down(bmap
->eq
[i
], bmap
->eq
[i
], gcd
, 1+total
);
355 for (i
= bmap
->n_ineq
- 1; i
>= 0; --i
) {
356 isl_seq_gcd(bmap
->ineq
[i
]+1, total
, &gcd
);
357 if (isl_int_is_zero(gcd
)) {
358 if (isl_int_is_neg(bmap
->ineq
[i
][0])) {
359 bmap
= isl_basic_map_set_to_empty(bmap
);
362 isl_basic_map_drop_inequality(bmap
, i
);
365 if (ISL_F_ISSET(bmap
, ISL_BASIC_MAP_RATIONAL
))
366 isl_int_gcd(gcd
, gcd
, bmap
->ineq
[i
][0]);
367 if (isl_int_is_one(gcd
))
369 isl_int_fdiv_q(bmap
->ineq
[i
][0], bmap
->ineq
[i
][0], gcd
);
370 isl_seq_scale_down(bmap
->ineq
[i
]+1, bmap
->ineq
[i
]+1, gcd
, total
);
377 struct isl_basic_set
*isl_basic_set_normalize_constraints(
378 struct isl_basic_set
*bset
)
380 isl_basic_map
*bmap
= bset_to_bmap(bset
);
381 return bset_from_bmap(isl_basic_map_normalize_constraints(bmap
));
384 /* Reduce the coefficient of the variable at position "pos"
385 * in integer division "div", such that it lies in the half-open
386 * interval (1/2,1/2], extracting any excess value from this integer division.
387 * "pos" is as determined by isl_basic_map_offset, i.e., pos == 0
388 * corresponds to the constant term.
390 * That is, the integer division is of the form
392 * floor((... + (c * d + r) * x_pos + ...)/d)
394 * with -d < 2 * r <= d.
397 * floor((... + r * x_pos + ...)/d) + c * x_pos
399 * If 2 * ((c * d + r) % d) <= d, then c = floor((c * d + r)/d).
400 * Otherwise, c = floor((c * d + r)/d) + 1.
402 * This is the same normalization that is performed by isl_aff_floor.
404 static __isl_give isl_basic_map
*reduce_coefficient_in_div(
405 __isl_take isl_basic_map
*bmap
, int div
, int pos
)
411 isl_int_fdiv_r(shift
, bmap
->div
[div
][1 + pos
], bmap
->div
[div
][0]);
412 isl_int_mul_ui(shift
, shift
, 2);
413 add_one
= isl_int_gt(shift
, bmap
->div
[div
][0]);
414 isl_int_fdiv_q(shift
, bmap
->div
[div
][1 + pos
], bmap
->div
[div
][0]);
416 isl_int_add_ui(shift
, shift
, 1);
417 isl_int_neg(shift
, shift
);
418 bmap
= isl_basic_map_shift_div(bmap
, div
, pos
, shift
);
419 isl_int_clear(shift
);
424 /* Does the coefficient of the variable at position "pos"
425 * in integer division "div" need to be reduced?
426 * That is, does it lie outside the half-open interval (1/2,1/2]?
427 * The coefficient c/d lies outside this interval if abs(2 * c) >= d and
430 static isl_bool
needs_reduction(__isl_keep isl_basic_map
*bmap
, int div
,
435 if (isl_int_is_zero(bmap
->div
[div
][1 + pos
]))
436 return isl_bool_false
;
438 isl_int_mul_ui(bmap
->div
[div
][1 + pos
], bmap
->div
[div
][1 + pos
], 2);
439 r
= isl_int_abs_ge(bmap
->div
[div
][1 + pos
], bmap
->div
[div
][0]) &&
440 !isl_int_eq(bmap
->div
[div
][1 + pos
], bmap
->div
[div
][0]);
441 isl_int_divexact_ui(bmap
->div
[div
][1 + pos
],
442 bmap
->div
[div
][1 + pos
], 2);
447 /* Reduce the coefficients (including the constant term) of
448 * integer division "div", if needed.
449 * In particular, make sure all coefficients lie in
450 * the half-open interval (1/2,1/2].
452 static __isl_give isl_basic_map
*reduce_div_coefficients_of_div(
453 __isl_take isl_basic_map
*bmap
, int div
)
456 unsigned total
= 1 + isl_basic_map_total_dim(bmap
);
458 for (i
= 0; i
< total
; ++i
) {
461 reduce
= needs_reduction(bmap
, div
, i
);
463 return isl_basic_map_free(bmap
);
466 bmap
= reduce_coefficient_in_div(bmap
, div
, i
);
474 /* Reduce the coefficients (including the constant term) of
475 * the known integer divisions, if needed
476 * In particular, make sure all coefficients lie in
477 * the half-open interval (1/2,1/2].
479 static __isl_give isl_basic_map
*reduce_div_coefficients(
480 __isl_take isl_basic_map
*bmap
)
486 if (bmap
->n_div
== 0)
489 for (i
= 0; i
< bmap
->n_div
; ++i
) {
490 if (isl_int_is_zero(bmap
->div
[i
][0]))
492 bmap
= reduce_div_coefficients_of_div(bmap
, i
);
500 /* Remove any common factor in numerator and denominator of the div expression,
501 * not taking into account the constant term.
502 * That is, if the div is of the form
504 * floor((a + m f(x))/(m d))
508 * floor((floor(a/m) + f(x))/d)
510 * The difference {a/m}/d in the argument satisfies 0 <= {a/m}/d < 1/d
511 * and can therefore not influence the result of the floor.
513 static void normalize_div_expression(__isl_keep isl_basic_map
*bmap
, int div
)
515 unsigned total
= isl_basic_map_total_dim(bmap
);
516 isl_ctx
*ctx
= bmap
->ctx
;
518 if (isl_int_is_zero(bmap
->div
[div
][0]))
520 isl_seq_gcd(bmap
->div
[div
] + 2, total
, &ctx
->normalize_gcd
);
521 isl_int_gcd(ctx
->normalize_gcd
, ctx
->normalize_gcd
, bmap
->div
[div
][0]);
522 if (isl_int_is_one(ctx
->normalize_gcd
))
524 isl_int_fdiv_q(bmap
->div
[div
][1], bmap
->div
[div
][1],
526 isl_int_divexact(bmap
->div
[div
][0], bmap
->div
[div
][0],
528 isl_seq_scale_down(bmap
->div
[div
] + 2, bmap
->div
[div
] + 2,
529 ctx
->normalize_gcd
, total
);
532 /* Remove any common factor in numerator and denominator of a div expression,
533 * not taking into account the constant term.
534 * That is, look for any div of the form
536 * floor((a + m f(x))/(m d))
540 * floor((floor(a/m) + f(x))/d)
542 * The difference {a/m}/d in the argument satisfies 0 <= {a/m}/d < 1/d
543 * and can therefore not influence the result of the floor.
545 static __isl_give isl_basic_map
*normalize_div_expressions(
546 __isl_take isl_basic_map
*bmap
)
552 if (bmap
->n_div
== 0)
555 for (i
= 0; i
< bmap
->n_div
; ++i
)
556 normalize_div_expression(bmap
, i
);
561 /* Assumes divs have been ordered if keep_divs is set.
563 static void eliminate_var_using_equality(struct isl_basic_map
*bmap
,
564 unsigned pos
, isl_int
*eq
, int keep_divs
, int *progress
)
567 unsigned space_total
;
571 total
= isl_basic_map_total_dim(bmap
);
572 space_total
= isl_space_dim(bmap
->dim
, isl_dim_all
);
573 last_div
= isl_seq_last_non_zero(eq
+ 1 + space_total
, bmap
->n_div
);
574 for (k
= 0; k
< bmap
->n_eq
; ++k
) {
575 if (bmap
->eq
[k
] == eq
)
577 if (isl_int_is_zero(bmap
->eq
[k
][1+pos
]))
581 isl_seq_elim(bmap
->eq
[k
], eq
, 1+pos
, 1+total
, NULL
);
582 isl_seq_normalize(bmap
->ctx
, bmap
->eq
[k
], 1 + total
);
585 for (k
= 0; k
< bmap
->n_ineq
; ++k
) {
586 if (isl_int_is_zero(bmap
->ineq
[k
][1+pos
]))
590 isl_seq_elim(bmap
->ineq
[k
], eq
, 1+pos
, 1+total
, NULL
);
591 isl_seq_normalize(bmap
->ctx
, bmap
->ineq
[k
], 1 + total
);
592 ISL_F_CLR(bmap
, ISL_BASIC_MAP_NORMALIZED
);
595 for (k
= 0; k
< bmap
->n_div
; ++k
) {
596 if (isl_int_is_zero(bmap
->div
[k
][0]))
598 if (isl_int_is_zero(bmap
->div
[k
][1+1+pos
]))
602 /* We need to be careful about circular definitions,
603 * so for now we just remove the definition of div k
604 * if the equality contains any divs.
605 * If keep_divs is set, then the divs have been ordered
606 * and we can keep the definition as long as the result
609 if (last_div
== -1 || (keep_divs
&& last_div
< k
)) {
610 isl_seq_elim(bmap
->div
[k
]+1, eq
,
611 1+pos
, 1+total
, &bmap
->div
[k
][0]);
612 normalize_div_expression(bmap
, k
);
614 isl_seq_clr(bmap
->div
[k
], 1 + total
);
615 ISL_F_CLR(bmap
, ISL_BASIC_MAP_NORMALIZED
);
619 /* Assumes divs have been ordered if keep_divs is set.
621 static __isl_give isl_basic_map
*eliminate_div(__isl_take isl_basic_map
*bmap
,
622 isl_int
*eq
, unsigned div
, int keep_divs
)
624 unsigned pos
= isl_space_dim(bmap
->dim
, isl_dim_all
) + div
;
626 eliminate_var_using_equality(bmap
, pos
, eq
, keep_divs
, NULL
);
628 bmap
= isl_basic_map_drop_div(bmap
, div
);
633 /* Check if elimination of div "div" using equality "eq" would not
634 * result in a div depending on a later div.
636 static int ok_to_eliminate_div(struct isl_basic_map
*bmap
, isl_int
*eq
,
641 unsigned space_total
= isl_space_dim(bmap
->dim
, isl_dim_all
);
642 unsigned pos
= space_total
+ div
;
644 last_div
= isl_seq_last_non_zero(eq
+ 1 + space_total
, bmap
->n_div
);
645 if (last_div
< 0 || last_div
<= div
)
648 for (k
= 0; k
<= last_div
; ++k
) {
649 if (isl_int_is_zero(bmap
->div
[k
][0]))
651 if (!isl_int_is_zero(bmap
->div
[k
][1 + 1 + pos
]))
658 /* Elimininate divs based on equalities
660 static struct isl_basic_map
*eliminate_divs_eq(
661 struct isl_basic_map
*bmap
, int *progress
)
668 bmap
= isl_basic_map_order_divs(bmap
);
673 off
= 1 + isl_space_dim(bmap
->dim
, isl_dim_all
);
675 for (d
= bmap
->n_div
- 1; d
>= 0 ; --d
) {
676 for (i
= 0; i
< bmap
->n_eq
; ++i
) {
677 if (!isl_int_is_one(bmap
->eq
[i
][off
+ d
]) &&
678 !isl_int_is_negone(bmap
->eq
[i
][off
+ d
]))
680 if (!ok_to_eliminate_div(bmap
, bmap
->eq
[i
], d
))
684 bmap
= eliminate_div(bmap
, bmap
->eq
[i
], d
, 1);
685 if (isl_basic_map_drop_equality(bmap
, i
) < 0)
686 return isl_basic_map_free(bmap
);
691 return eliminate_divs_eq(bmap
, progress
);
695 /* Elimininate divs based on inequalities
697 static struct isl_basic_map
*eliminate_divs_ineq(
698 struct isl_basic_map
*bmap
, int *progress
)
709 off
= 1 + isl_space_dim(bmap
->dim
, isl_dim_all
);
711 for (d
= bmap
->n_div
- 1; d
>= 0 ; --d
) {
712 for (i
= 0; i
< bmap
->n_eq
; ++i
)
713 if (!isl_int_is_zero(bmap
->eq
[i
][off
+ d
]))
717 for (i
= 0; i
< bmap
->n_ineq
; ++i
)
718 if (isl_int_abs_gt(bmap
->ineq
[i
][off
+ d
], ctx
->one
))
720 if (i
< bmap
->n_ineq
)
723 bmap
= isl_basic_map_eliminate_vars(bmap
, (off
-1)+d
, 1);
724 if (!bmap
|| ISL_F_ISSET(bmap
, ISL_BASIC_MAP_EMPTY
))
726 bmap
= isl_basic_map_drop_div(bmap
, d
);
733 /* Does the equality constraint at position "eq" in "bmap" involve
734 * any local variables in the range [first, first + n)
735 * that are not marked as having an explicit representation?
737 static isl_bool
bmap_eq_involves_unknown_divs(__isl_keep isl_basic_map
*bmap
,
738 int eq
, unsigned first
, unsigned n
)
744 return isl_bool_error
;
746 o_div
= isl_basic_map_offset(bmap
, isl_dim_div
);
747 for (i
= 0; i
< n
; ++i
) {
750 if (isl_int_is_zero(bmap
->eq
[eq
][o_div
+ first
+ i
]))
752 unknown
= isl_basic_map_div_is_marked_unknown(bmap
, first
+ i
);
754 return isl_bool_error
;
756 return isl_bool_true
;
759 return isl_bool_false
;
762 /* The last local variable involved in the equality constraint
763 * at position "eq" in "bmap" is the local variable at position "div".
764 * It can therefore be used to extract an explicit representation
766 * Do so unless the local variable already has an explicit representation or
767 * the explicit representation would involve any other local variables
768 * that in turn do not have an explicit representation.
769 * An equality constraint involving local variables without an explicit
770 * representation can be used in isl_basic_map_drop_redundant_divs
771 * to separate out an independent local variable. Introducing
772 * an explicit representation here would block this transformation,
773 * while the partial explicit representation in itself is not very useful.
774 * Set *progress if anything is changed.
776 * The equality constraint is of the form
780 * with n a positive number. The explicit representation derived from
785 static __isl_give isl_basic_map
*set_div_from_eq(__isl_take isl_basic_map
*bmap
,
786 int div
, int eq
, int *progress
)
788 unsigned total
, o_div
;
794 if (!isl_int_is_zero(bmap
->div
[div
][0]))
797 involves
= bmap_eq_involves_unknown_divs(bmap
, eq
, 0, div
);
799 return isl_basic_map_free(bmap
);
803 total
= isl_basic_map_dim(bmap
, isl_dim_all
);
804 o_div
= isl_basic_map_offset(bmap
, isl_dim_div
);
805 isl_seq_neg(bmap
->div
[div
] + 1, bmap
->eq
[eq
], 1 + total
);
806 isl_int_set_si(bmap
->div
[div
][1 + o_div
+ div
], 0);
807 isl_int_set(bmap
->div
[div
][0], bmap
->eq
[eq
][o_div
+ div
]);
810 ISL_F_CLR(bmap
, ISL_BASIC_MAP_NORMALIZED
);
815 struct isl_basic_map
*isl_basic_map_gauss(
816 struct isl_basic_map
*bmap
, int *progress
)
824 bmap
= isl_basic_map_order_divs(bmap
);
829 total
= isl_basic_map_total_dim(bmap
);
830 total_var
= total
- bmap
->n_div
;
832 last_var
= total
- 1;
833 for (done
= 0; done
< bmap
->n_eq
; ++done
) {
834 for (; last_var
>= 0; --last_var
) {
835 for (k
= done
; k
< bmap
->n_eq
; ++k
)
836 if (!isl_int_is_zero(bmap
->eq
[k
][1+last_var
]))
844 swap_equality(bmap
, k
, done
);
845 if (isl_int_is_neg(bmap
->eq
[done
][1+last_var
]))
846 isl_seq_neg(bmap
->eq
[done
], bmap
->eq
[done
], 1+total
);
848 eliminate_var_using_equality(bmap
, last_var
, bmap
->eq
[done
], 1,
851 if (last_var
>= total_var
)
852 bmap
= set_div_from_eq(bmap
, last_var
- total_var
,
857 if (done
== bmap
->n_eq
)
859 for (k
= done
; k
< bmap
->n_eq
; ++k
) {
860 if (isl_int_is_zero(bmap
->eq
[k
][0]))
862 return isl_basic_map_set_to_empty(bmap
);
864 isl_basic_map_free_equality(bmap
, bmap
->n_eq
-done
);
868 struct isl_basic_set
*isl_basic_set_gauss(
869 struct isl_basic_set
*bset
, int *progress
)
871 return bset_from_bmap(isl_basic_map_gauss(bset_to_bmap(bset
),
876 static unsigned int round_up(unsigned int v
)
887 /* Hash table of inequalities in a basic map.
888 * "index" is an array of addresses of inequalities in the basic map, some
889 * of which are NULL. The inequalities are hashed on the coefficients
890 * except the constant term.
891 * "size" is the number of elements in the array and is always a power of two
892 * "bits" is the number of bits need to represent an index into the array.
893 * "total" is the total dimension of the basic map.
895 struct isl_constraint_index
{
902 /* Fill in the "ci" data structure for holding the inequalities of "bmap".
904 static isl_stat
create_constraint_index(struct isl_constraint_index
*ci
,
905 __isl_keep isl_basic_map
*bmap
)
911 return isl_stat_error
;
912 ci
->total
= isl_basic_set_total_dim(bmap
);
913 if (bmap
->n_ineq
== 0)
915 ci
->size
= round_up(4 * (bmap
->n_ineq
+ 1) / 3 - 1);
916 ci
->bits
= ffs(ci
->size
) - 1;
917 ctx
= isl_basic_map_get_ctx(bmap
);
918 ci
->index
= isl_calloc_array(ctx
, isl_int
**, ci
->size
);
920 return isl_stat_error
;
925 /* Free the memory allocated by create_constraint_index.
927 static void constraint_index_free(struct isl_constraint_index
*ci
)
932 /* Return the position in ci->index that contains the address of
933 * an inequality that is equal to *ineq up to the constant term,
934 * provided this address is not identical to "ineq".
935 * If there is no such inequality, then return the position where
936 * such an inequality should be inserted.
938 static int hash_index_ineq(struct isl_constraint_index
*ci
, isl_int
**ineq
)
941 uint32_t hash
= isl_seq_get_hash_bits((*ineq
) + 1, ci
->total
, ci
->bits
);
942 for (h
= hash
; ci
->index
[h
]; h
= (h
+1) % ci
->size
)
943 if (ineq
!= ci
->index
[h
] &&
944 isl_seq_eq((*ineq
) + 1, ci
->index
[h
][0]+1, ci
->total
))
949 /* Return the position in ci->index that contains the address of
950 * an inequality that is equal to the k'th inequality of "bmap"
951 * up to the constant term, provided it does not point to the very
953 * If there is no such inequality, then return the position where
954 * such an inequality should be inserted.
956 static int hash_index(struct isl_constraint_index
*ci
,
957 __isl_keep isl_basic_map
*bmap
, int k
)
959 return hash_index_ineq(ci
, &bmap
->ineq
[k
]);
962 static int set_hash_index(struct isl_constraint_index
*ci
,
963 struct isl_basic_set
*bset
, int k
)
965 return hash_index(ci
, bset
, k
);
968 /* Fill in the "ci" data structure with the inequalities of "bset".
970 static isl_stat
setup_constraint_index(struct isl_constraint_index
*ci
,
971 __isl_keep isl_basic_set
*bset
)
975 if (create_constraint_index(ci
, bset
) < 0)
976 return isl_stat_error
;
978 for (k
= 0; k
< bset
->n_ineq
; ++k
) {
979 h
= set_hash_index(ci
, bset
, k
);
980 ci
->index
[h
] = &bset
->ineq
[k
];
986 /* Is the inequality ineq (obviously) redundant with respect
987 * to the constraints in "ci"?
989 * Look for an inequality in "ci" with the same coefficients and then
990 * check if the contant term of "ineq" is greater than or equal
991 * to the constant term of that inequality. If so, "ineq" is clearly
994 * Note that hash_index_ineq ignores a stored constraint if it has
995 * the same address as the passed inequality. It is ok to pass
996 * the address of a local variable here since it will never be
997 * the same as the address of a constraint in "ci".
999 static isl_bool
constraint_index_is_redundant(struct isl_constraint_index
*ci
,
1004 h
= hash_index_ineq(ci
, &ineq
);
1006 return isl_bool_false
;
1007 return isl_int_ge(ineq
[0], (*ci
->index
[h
])[0]);
1010 /* If we can eliminate more than one div, then we need to make
1011 * sure we do it from last div to first div, in order not to
1012 * change the position of the other divs that still need to
1015 static struct isl_basic_map
*remove_duplicate_divs(
1016 struct isl_basic_map
*bmap
, int *progress
)
1026 struct isl_ctx
*ctx
;
1028 bmap
= isl_basic_map_order_divs(bmap
);
1029 if (!bmap
|| bmap
->n_div
<= 1)
1032 total_var
= isl_space_dim(bmap
->dim
, isl_dim_all
);
1033 total
= total_var
+ bmap
->n_div
;
1036 for (k
= bmap
->n_div
- 1; k
>= 0; --k
)
1037 if (!isl_int_is_zero(bmap
->div
[k
][0]))
1042 size
= round_up(4 * bmap
->n_div
/ 3 - 1);
1045 elim_for
= isl_calloc_array(ctx
, int, bmap
->n_div
);
1046 bits
= ffs(size
) - 1;
1047 index
= isl_calloc_array(ctx
, int, size
);
1048 if (!elim_for
|| !index
)
1050 eq
= isl_blk_alloc(ctx
, 1+total
);
1051 if (isl_blk_is_error(eq
))
1054 isl_seq_clr(eq
.data
, 1+total
);
1055 index
[isl_seq_get_hash_bits(bmap
->div
[k
], 2+total
, bits
)] = k
+ 1;
1056 for (--k
; k
>= 0; --k
) {
1059 if (isl_int_is_zero(bmap
->div
[k
][0]))
1062 hash
= isl_seq_get_hash_bits(bmap
->div
[k
], 2+total
, bits
);
1063 for (h
= hash
; index
[h
]; h
= (h
+1) % size
)
1064 if (isl_seq_eq(bmap
->div
[k
],
1065 bmap
->div
[index
[h
]-1], 2+total
))
1070 elim_for
[l
] = k
+ 1;
1074 for (l
= bmap
->n_div
- 1; l
>= 0; --l
) {
1077 k
= elim_for
[l
] - 1;
1078 isl_int_set_si(eq
.data
[1+total_var
+k
], -1);
1079 isl_int_set_si(eq
.data
[1+total_var
+l
], 1);
1080 bmap
= eliminate_div(bmap
, eq
.data
, l
, 1);
1083 isl_int_set_si(eq
.data
[1+total_var
+k
], 0);
1084 isl_int_set_si(eq
.data
[1+total_var
+l
], 0);
1087 isl_blk_free(ctx
, eq
);
1094 static int n_pure_div_eq(struct isl_basic_map
*bmap
)
1099 total
= isl_space_dim(bmap
->dim
, isl_dim_all
);
1100 for (i
= 0, j
= bmap
->n_div
-1; i
< bmap
->n_eq
; ++i
) {
1101 while (j
>= 0 && isl_int_is_zero(bmap
->eq
[i
][1 + total
+ j
]))
1105 if (isl_seq_first_non_zero(bmap
->eq
[i
] + 1 + total
, j
) != -1)
1111 /* Normalize divs that appear in equalities.
1113 * In particular, we assume that bmap contains some equalities
1118 * and we want to replace the set of e_i by a minimal set and
1119 * such that the new e_i have a canonical representation in terms
1121 * If any of the equalities involves more than one divs, then
1122 * we currently simply bail out.
1124 * Let us first additionally assume that all equalities involve
1125 * a div. The equalities then express modulo constraints on the
1126 * remaining variables and we can use "parameter compression"
1127 * to find a minimal set of constraints. The result is a transformation
1129 * x = T(x') = x_0 + G x'
1131 * with G a lower-triangular matrix with all elements below the diagonal
1132 * non-negative and smaller than the diagonal element on the same row.
1133 * We first normalize x_0 by making the same property hold in the affine
1135 * The rows i of G with a 1 on the diagonal do not impose any modulo
1136 * constraint and simply express x_i = x'_i.
1137 * For each of the remaining rows i, we introduce a div and a corresponding
1138 * equality. In particular
1140 * g_ii e_j = x_i - g_i(x')
1142 * where each x'_k is replaced either by x_k (if g_kk = 1) or the
1143 * corresponding div (if g_kk != 1).
1145 * If there are any equalities not involving any div, then we
1146 * first apply a variable compression on the variables x:
1148 * x = C x'' x'' = C_2 x
1150 * and perform the above parameter compression on A C instead of on A.
1151 * The resulting compression is then of the form
1153 * x'' = T(x') = x_0 + G x'
1155 * and in constructing the new divs and the corresponding equalities,
1156 * we have to replace each x'', i.e., the x'_k with (g_kk = 1),
1157 * by the corresponding row from C_2.
1159 static struct isl_basic_map
*normalize_divs(
1160 struct isl_basic_map
*bmap
, int *progress
)
1167 struct isl_mat
*T
= NULL
;
1168 struct isl_mat
*C
= NULL
;
1169 struct isl_mat
*C2
= NULL
;
1172 int dropped
, needed
;
1177 if (bmap
->n_div
== 0)
1180 if (bmap
->n_eq
== 0)
1183 if (ISL_F_ISSET(bmap
, ISL_BASIC_MAP_NORMALIZED_DIVS
))
1186 total
= isl_space_dim(bmap
->dim
, isl_dim_all
);
1187 div_eq
= n_pure_div_eq(bmap
);
1191 if (div_eq
< bmap
->n_eq
) {
1192 B
= isl_mat_sub_alloc6(bmap
->ctx
, bmap
->eq
, div_eq
,
1193 bmap
->n_eq
- div_eq
, 0, 1 + total
);
1194 C
= isl_mat_variable_compression(B
, &C2
);
1197 if (C
->n_col
== 0) {
1198 bmap
= isl_basic_map_set_to_empty(bmap
);
1205 d
= isl_vec_alloc(bmap
->ctx
, div_eq
);
1208 for (i
= 0, j
= bmap
->n_div
-1; i
< div_eq
; ++i
) {
1209 while (j
>= 0 && isl_int_is_zero(bmap
->eq
[i
][1 + total
+ j
]))
1211 isl_int_set(d
->block
.data
[i
], bmap
->eq
[i
][1 + total
+ j
]);
1213 B
= isl_mat_sub_alloc6(bmap
->ctx
, bmap
->eq
, 0, div_eq
, 0, 1 + total
);
1216 B
= isl_mat_product(B
, C
);
1220 T
= isl_mat_parameter_compression(B
, d
);
1223 if (T
->n_col
== 0) {
1224 bmap
= isl_basic_map_set_to_empty(bmap
);
1230 for (i
= 0; i
< T
->n_row
- 1; ++i
) {
1231 isl_int_fdiv_q(v
, T
->row
[1 + i
][0], T
->row
[1 + i
][1 + i
]);
1232 if (isl_int_is_zero(v
))
1234 isl_mat_col_submul(T
, 0, v
, 1 + i
);
1237 pos
= isl_alloc_array(bmap
->ctx
, int, T
->n_row
);
1240 /* We have to be careful because dropping equalities may reorder them */
1242 for (j
= bmap
->n_div
- 1; j
>= 0; --j
) {
1243 for (i
= 0; i
< bmap
->n_eq
; ++i
)
1244 if (!isl_int_is_zero(bmap
->eq
[i
][1 + total
+ j
]))
1246 if (i
< bmap
->n_eq
) {
1247 bmap
= isl_basic_map_drop_div(bmap
, j
);
1248 isl_basic_map_drop_equality(bmap
, i
);
1254 for (i
= 1; i
< T
->n_row
; ++i
) {
1255 if (isl_int_is_one(T
->row
[i
][i
]))
1260 if (needed
> dropped
) {
1261 bmap
= isl_basic_map_extend_space(bmap
, isl_space_copy(bmap
->dim
),
1266 for (i
= 1; i
< T
->n_row
; ++i
) {
1267 if (isl_int_is_one(T
->row
[i
][i
]))
1269 k
= isl_basic_map_alloc_div(bmap
);
1270 pos
[i
] = 1 + total
+ k
;
1271 isl_seq_clr(bmap
->div
[k
] + 1, 1 + total
+ bmap
->n_div
);
1272 isl_int_set(bmap
->div
[k
][0], T
->row
[i
][i
]);
1274 isl_seq_cpy(bmap
->div
[k
] + 1, C2
->row
[i
], 1 + total
);
1276 isl_int_set_si(bmap
->div
[k
][1 + i
], 1);
1277 for (j
= 0; j
< i
; ++j
) {
1278 if (isl_int_is_zero(T
->row
[i
][j
]))
1280 if (pos
[j
] < T
->n_row
&& C2
)
1281 isl_seq_submul(bmap
->div
[k
] + 1, T
->row
[i
][j
],
1282 C2
->row
[pos
[j
]], 1 + total
);
1284 isl_int_neg(bmap
->div
[k
][1 + pos
[j
]],
1287 j
= isl_basic_map_alloc_equality(bmap
);
1288 isl_seq_neg(bmap
->eq
[j
], bmap
->div
[k
]+1, 1+total
+bmap
->n_div
);
1289 isl_int_set(bmap
->eq
[j
][pos
[i
]], bmap
->div
[k
][0]);
1298 ISL_F_SET(bmap
, ISL_BASIC_MAP_NORMALIZED_DIVS
);
1308 static struct isl_basic_map
*set_div_from_lower_bound(
1309 struct isl_basic_map
*bmap
, int div
, int ineq
)
1311 unsigned total
= 1 + isl_space_dim(bmap
->dim
, isl_dim_all
);
1313 isl_seq_neg(bmap
->div
[div
] + 1, bmap
->ineq
[ineq
], total
+ bmap
->n_div
);
1314 isl_int_set(bmap
->div
[div
][0], bmap
->ineq
[ineq
][total
+ div
]);
1315 isl_int_add(bmap
->div
[div
][1], bmap
->div
[div
][1], bmap
->div
[div
][0]);
1316 isl_int_sub_ui(bmap
->div
[div
][1], bmap
->div
[div
][1], 1);
1317 isl_int_set_si(bmap
->div
[div
][1 + total
+ div
], 0);
1322 /* Check whether it is ok to define a div based on an inequality.
1323 * To avoid the introduction of circular definitions of divs, we
1324 * do not allow such a definition if the resulting expression would refer to
1325 * any other undefined divs or if any known div is defined in
1326 * terms of the unknown div.
1328 static int ok_to_set_div_from_bound(struct isl_basic_map
*bmap
,
1332 unsigned total
= 1 + isl_space_dim(bmap
->dim
, isl_dim_all
);
1334 /* Not defined in terms of unknown divs */
1335 for (j
= 0; j
< bmap
->n_div
; ++j
) {
1338 if (isl_int_is_zero(bmap
->ineq
[ineq
][total
+ j
]))
1340 if (isl_int_is_zero(bmap
->div
[j
][0]))
1344 /* No other div defined in terms of this one => avoid loops */
1345 for (j
= 0; j
< bmap
->n_div
; ++j
) {
1348 if (isl_int_is_zero(bmap
->div
[j
][0]))
1350 if (!isl_int_is_zero(bmap
->div
[j
][1 + total
+ div
]))
1357 /* Would an expression for div "div" based on inequality "ineq" of "bmap"
1358 * be a better expression than the current one?
1360 * If we do not have any expression yet, then any expression would be better.
1361 * Otherwise we check if the last variable involved in the inequality
1362 * (disregarding the div that it would define) is in an earlier position
1363 * than the last variable involved in the current div expression.
1365 static int better_div_constraint(__isl_keep isl_basic_map
*bmap
,
1368 unsigned total
= 1 + isl_space_dim(bmap
->dim
, isl_dim_all
);
1372 if (isl_int_is_zero(bmap
->div
[div
][0]))
1375 if (isl_seq_last_non_zero(bmap
->ineq
[ineq
] + total
+ div
+ 1,
1376 bmap
->n_div
- (div
+ 1)) >= 0)
1379 last_ineq
= isl_seq_last_non_zero(bmap
->ineq
[ineq
], total
+ div
);
1380 last_div
= isl_seq_last_non_zero(bmap
->div
[div
] + 1,
1381 total
+ bmap
->n_div
);
1383 return last_ineq
< last_div
;
1386 /* Given two constraints "k" and "l" that are opposite to each other,
1387 * except for the constant term, check if we can use them
1388 * to obtain an expression for one of the hitherto unknown divs or
1389 * a "better" expression for a div for which we already have an expression.
1390 * "sum" is the sum of the constant terms of the constraints.
1391 * If this sum is strictly smaller than the coefficient of one
1392 * of the divs, then this pair can be used define the div.
1393 * To avoid the introduction of circular definitions of divs, we
1394 * do not use the pair if the resulting expression would refer to
1395 * any other undefined divs or if any known div is defined in
1396 * terms of the unknown div.
1398 static struct isl_basic_map
*check_for_div_constraints(
1399 struct isl_basic_map
*bmap
, int k
, int l
, isl_int sum
, int *progress
)
1402 unsigned total
= 1 + isl_space_dim(bmap
->dim
, isl_dim_all
);
1404 for (i
= 0; i
< bmap
->n_div
; ++i
) {
1405 if (isl_int_is_zero(bmap
->ineq
[k
][total
+ i
]))
1407 if (isl_int_abs_ge(sum
, bmap
->ineq
[k
][total
+ i
]))
1409 if (!better_div_constraint(bmap
, i
, k
))
1411 if (!ok_to_set_div_from_bound(bmap
, i
, k
))
1413 if (isl_int_is_pos(bmap
->ineq
[k
][total
+ i
]))
1414 bmap
= set_div_from_lower_bound(bmap
, i
, k
);
1416 bmap
= set_div_from_lower_bound(bmap
, i
, l
);
1424 __isl_give isl_basic_map
*isl_basic_map_remove_duplicate_constraints(
1425 __isl_take isl_basic_map
*bmap
, int *progress
, int detect_divs
)
1427 struct isl_constraint_index ci
;
1429 unsigned total
= isl_basic_map_total_dim(bmap
);
1432 if (!bmap
|| bmap
->n_ineq
<= 1)
1435 if (create_constraint_index(&ci
, bmap
) < 0)
1438 h
= isl_seq_get_hash_bits(bmap
->ineq
[0] + 1, total
, ci
.bits
);
1439 ci
.index
[h
] = &bmap
->ineq
[0];
1440 for (k
= 1; k
< bmap
->n_ineq
; ++k
) {
1441 h
= hash_index(&ci
, bmap
, k
);
1443 ci
.index
[h
] = &bmap
->ineq
[k
];
1448 l
= ci
.index
[h
] - &bmap
->ineq
[0];
1449 if (isl_int_lt(bmap
->ineq
[k
][0], bmap
->ineq
[l
][0]))
1450 swap_inequality(bmap
, k
, l
);
1451 isl_basic_map_drop_inequality(bmap
, k
);
1455 for (k
= 0; k
< bmap
->n_ineq
-1; ++k
) {
1456 isl_seq_neg(bmap
->ineq
[k
]+1, bmap
->ineq
[k
]+1, total
);
1457 h
= hash_index(&ci
, bmap
, k
);
1458 isl_seq_neg(bmap
->ineq
[k
]+1, bmap
->ineq
[k
]+1, total
);
1461 l
= ci
.index
[h
] - &bmap
->ineq
[0];
1462 isl_int_add(sum
, bmap
->ineq
[k
][0], bmap
->ineq
[l
][0]);
1463 if (isl_int_is_pos(sum
)) {
1465 bmap
= check_for_div_constraints(bmap
, k
, l
,
1469 if (isl_int_is_zero(sum
)) {
1470 /* We need to break out of the loop after these
1471 * changes since the contents of the hash
1472 * will no longer be valid.
1473 * Plus, we probably we want to regauss first.
1477 isl_basic_map_drop_inequality(bmap
, l
);
1478 isl_basic_map_inequality_to_equality(bmap
, k
);
1480 bmap
= isl_basic_map_set_to_empty(bmap
);
1485 constraint_index_free(&ci
);
1489 /* Detect all pairs of inequalities that form an equality.
1491 * isl_basic_map_remove_duplicate_constraints detects at most one such pair.
1492 * Call it repeatedly while it is making progress.
1494 __isl_give isl_basic_map
*isl_basic_map_detect_inequality_pairs(
1495 __isl_take isl_basic_map
*bmap
, int *progress
)
1501 bmap
= isl_basic_map_remove_duplicate_constraints(bmap
,
1503 if (progress
&& duplicate
)
1505 } while (duplicate
);
1510 /* Eliminate knowns divs from constraints where they appear with
1511 * a (positive or negative) unit coefficient.
1515 * floor(e/m) + f >= 0
1523 * -floor(e/m) + f >= 0
1527 * -e + m f + m - 1 >= 0
1529 * The first conversion is valid because floor(e/m) >= -f is equivalent
1530 * to e/m >= -f because -f is an integral expression.
1531 * The second conversion follows from the fact that
1533 * -floor(e/m) = ceil(-e/m) = floor((-e + m - 1)/m)
1536 * Note that one of the div constraints may have been eliminated
1537 * due to being redundant with respect to the constraint that is
1538 * being modified by this function. The modified constraint may
1539 * no longer imply this div constraint, so we add it back to make
1540 * sure we do not lose any information.
1542 * We skip integral divs, i.e., those with denominator 1, as we would
1543 * risk eliminating the div from the div constraints. We do not need
1544 * to handle those divs here anyway since the div constraints will turn
1545 * out to form an equality and this equality can then be used to eliminate
1546 * the div from all constraints.
1548 static __isl_give isl_basic_map
*eliminate_unit_divs(
1549 __isl_take isl_basic_map
*bmap
, int *progress
)
1558 ctx
= isl_basic_map_get_ctx(bmap
);
1559 total
= 1 + isl_space_dim(bmap
->dim
, isl_dim_all
);
1561 for (i
= 0; i
< bmap
->n_div
; ++i
) {
1562 if (isl_int_is_zero(bmap
->div
[i
][0]))
1564 if (isl_int_is_one(bmap
->div
[i
][0]))
1566 for (j
= 0; j
< bmap
->n_ineq
; ++j
) {
1569 if (!isl_int_is_one(bmap
->ineq
[j
][total
+ i
]) &&
1570 !isl_int_is_negone(bmap
->ineq
[j
][total
+ i
]))
1575 s
= isl_int_sgn(bmap
->ineq
[j
][total
+ i
]);
1576 isl_int_set_si(bmap
->ineq
[j
][total
+ i
], 0);
1578 isl_seq_combine(bmap
->ineq
[j
],
1579 ctx
->negone
, bmap
->div
[i
] + 1,
1580 bmap
->div
[i
][0], bmap
->ineq
[j
],
1581 total
+ bmap
->n_div
);
1583 isl_seq_combine(bmap
->ineq
[j
],
1584 ctx
->one
, bmap
->div
[i
] + 1,
1585 bmap
->div
[i
][0], bmap
->ineq
[j
],
1586 total
+ bmap
->n_div
);
1588 isl_int_add(bmap
->ineq
[j
][0],
1589 bmap
->ineq
[j
][0], bmap
->div
[i
][0]);
1590 isl_int_sub_ui(bmap
->ineq
[j
][0],
1591 bmap
->ineq
[j
][0], 1);
1594 bmap
= isl_basic_map_extend_constraints(bmap
, 0, 1);
1595 if (isl_basic_map_add_div_constraint(bmap
, i
, s
) < 0)
1596 return isl_basic_map_free(bmap
);
1603 struct isl_basic_map
*isl_basic_map_simplify(struct isl_basic_map
*bmap
)
1612 if (isl_basic_map_plain_is_empty(bmap
))
1614 bmap
= isl_basic_map_normalize_constraints(bmap
);
1615 bmap
= reduce_div_coefficients(bmap
);
1616 bmap
= normalize_div_expressions(bmap
);
1617 bmap
= remove_duplicate_divs(bmap
, &progress
);
1618 bmap
= eliminate_unit_divs(bmap
, &progress
);
1619 bmap
= eliminate_divs_eq(bmap
, &progress
);
1620 bmap
= eliminate_divs_ineq(bmap
, &progress
);
1621 bmap
= isl_basic_map_gauss(bmap
, &progress
);
1622 /* requires equalities in normal form */
1623 bmap
= normalize_divs(bmap
, &progress
);
1624 bmap
= isl_basic_map_remove_duplicate_constraints(bmap
,
1626 if (bmap
&& progress
)
1627 ISL_F_CLR(bmap
, ISL_BASIC_MAP_REDUCED_COEFFICIENTS
);
1632 struct isl_basic_set
*isl_basic_set_simplify(struct isl_basic_set
*bset
)
1634 return bset_from_bmap(isl_basic_map_simplify(bset_to_bmap(bset
)));
1638 int isl_basic_map_is_div_constraint(__isl_keep isl_basic_map
*bmap
,
1639 isl_int
*constraint
, unsigned div
)
1646 pos
= 1 + isl_space_dim(bmap
->dim
, isl_dim_all
) + div
;
1648 if (isl_int_eq(constraint
[pos
], bmap
->div
[div
][0])) {
1650 isl_int_sub(bmap
->div
[div
][1],
1651 bmap
->div
[div
][1], bmap
->div
[div
][0]);
1652 isl_int_add_ui(bmap
->div
[div
][1], bmap
->div
[div
][1], 1);
1653 neg
= isl_seq_is_neg(constraint
, bmap
->div
[div
]+1, pos
);
1654 isl_int_sub_ui(bmap
->div
[div
][1], bmap
->div
[div
][1], 1);
1655 isl_int_add(bmap
->div
[div
][1],
1656 bmap
->div
[div
][1], bmap
->div
[div
][0]);
1659 if (isl_seq_first_non_zero(constraint
+pos
+1,
1660 bmap
->n_div
-div
-1) != -1)
1662 } else if (isl_int_abs_eq(constraint
[pos
], bmap
->div
[div
][0])) {
1663 if (!isl_seq_eq(constraint
, bmap
->div
[div
]+1, pos
))
1665 if (isl_seq_first_non_zero(constraint
+pos
+1,
1666 bmap
->n_div
-div
-1) != -1)
1674 int isl_basic_set_is_div_constraint(__isl_keep isl_basic_set
*bset
,
1675 isl_int
*constraint
, unsigned div
)
1677 return isl_basic_map_is_div_constraint(bset
, constraint
, div
);
1681 /* If the only constraints a div d=floor(f/m)
1682 * appears in are its two defining constraints
1685 * -(f - (m - 1)) + m d >= 0
1687 * then it can safely be removed.
1689 static int div_is_redundant(struct isl_basic_map
*bmap
, int div
)
1692 unsigned pos
= 1 + isl_space_dim(bmap
->dim
, isl_dim_all
) + div
;
1694 for (i
= 0; i
< bmap
->n_eq
; ++i
)
1695 if (!isl_int_is_zero(bmap
->eq
[i
][pos
]))
1698 for (i
= 0; i
< bmap
->n_ineq
; ++i
) {
1699 if (isl_int_is_zero(bmap
->ineq
[i
][pos
]))
1701 if (!isl_basic_map_is_div_constraint(bmap
, bmap
->ineq
[i
], div
))
1705 for (i
= 0; i
< bmap
->n_div
; ++i
) {
1706 if (isl_int_is_zero(bmap
->div
[i
][0]))
1708 if (!isl_int_is_zero(bmap
->div
[i
][1+pos
]))
1716 * Remove divs that don't occur in any of the constraints or other divs.
1717 * These can arise when dropping constraints from a basic map or
1718 * when the divs of a basic map have been temporarily aligned
1719 * with the divs of another basic map.
1721 static struct isl_basic_map
*remove_redundant_divs(struct isl_basic_map
*bmap
)
1728 for (i
= bmap
->n_div
-1; i
>= 0; --i
) {
1729 if (!div_is_redundant(bmap
, i
))
1731 bmap
= isl_basic_map_drop_div(bmap
, i
);
1736 /* Mark "bmap" as final, without checking for obviously redundant
1737 * integer divisions. This function should be used when "bmap"
1738 * is known not to involve any such integer divisions.
1740 __isl_give isl_basic_map
*isl_basic_map_mark_final(
1741 __isl_take isl_basic_map
*bmap
)
1745 ISL_F_SET(bmap
, ISL_BASIC_SET_FINAL
);
1749 /* Mark "bmap" as final, after removing obviously redundant integer divisions.
1751 struct isl_basic_map
*isl_basic_map_finalize(struct isl_basic_map
*bmap
)
1753 bmap
= remove_redundant_divs(bmap
);
1754 bmap
= isl_basic_map_mark_final(bmap
);
1758 struct isl_basic_set
*isl_basic_set_finalize(struct isl_basic_set
*bset
)
1760 return bset_from_bmap(isl_basic_map_finalize(bset_to_bmap(bset
)));
1763 struct isl_set
*isl_set_finalize(struct isl_set
*set
)
1769 for (i
= 0; i
< set
->n
; ++i
) {
1770 set
->p
[i
] = isl_basic_set_finalize(set
->p
[i
]);
1780 struct isl_map
*isl_map_finalize(struct isl_map
*map
)
1786 for (i
= 0; i
< map
->n
; ++i
) {
1787 map
->p
[i
] = isl_basic_map_finalize(map
->p
[i
]);
1791 ISL_F_CLR(map
, ISL_MAP_NORMALIZED
);
1799 /* Remove definition of any div that is defined in terms of the given variable.
1800 * The div itself is not removed. Functions such as
1801 * eliminate_divs_ineq depend on the other divs remaining in place.
1803 static struct isl_basic_map
*remove_dependent_vars(struct isl_basic_map
*bmap
,
1811 for (i
= 0; i
< bmap
->n_div
; ++i
) {
1812 if (isl_int_is_zero(bmap
->div
[i
][0]))
1814 if (isl_int_is_zero(bmap
->div
[i
][1+1+pos
]))
1816 bmap
= isl_basic_map_mark_div_unknown(bmap
, i
);
1823 /* Eliminate the specified variables from the constraints using
1824 * Fourier-Motzkin. The variables themselves are not removed.
1826 struct isl_basic_map
*isl_basic_map_eliminate_vars(
1827 struct isl_basic_map
*bmap
, unsigned pos
, unsigned n
)
1838 total
= isl_basic_map_total_dim(bmap
);
1840 bmap
= isl_basic_map_cow(bmap
);
1841 for (d
= pos
+ n
- 1; d
>= 0 && d
>= pos
; --d
)
1842 bmap
= remove_dependent_vars(bmap
, d
);
1846 for (d
= pos
+ n
- 1;
1847 d
>= 0 && d
>= total
- bmap
->n_div
&& d
>= pos
; --d
)
1848 isl_seq_clr(bmap
->div
[d
-(total
-bmap
->n_div
)], 2+total
);
1849 for (d
= pos
+ n
- 1; d
>= 0 && d
>= pos
; --d
) {
1850 int n_lower
, n_upper
;
1853 for (i
= 0; i
< bmap
->n_eq
; ++i
) {
1854 if (isl_int_is_zero(bmap
->eq
[i
][1+d
]))
1856 eliminate_var_using_equality(bmap
, d
, bmap
->eq
[i
], 0, NULL
);
1857 isl_basic_map_drop_equality(bmap
, i
);
1865 for (i
= 0; i
< bmap
->n_ineq
; ++i
) {
1866 if (isl_int_is_pos(bmap
->ineq
[i
][1+d
]))
1868 else if (isl_int_is_neg(bmap
->ineq
[i
][1+d
]))
1871 bmap
= isl_basic_map_extend_constraints(bmap
,
1872 0, n_lower
* n_upper
);
1875 for (i
= bmap
->n_ineq
- 1; i
>= 0; --i
) {
1877 if (isl_int_is_zero(bmap
->ineq
[i
][1+d
]))
1880 for (j
= 0; j
< i
; ++j
) {
1881 if (isl_int_is_zero(bmap
->ineq
[j
][1+d
]))
1884 if (isl_int_sgn(bmap
->ineq
[i
][1+d
]) ==
1885 isl_int_sgn(bmap
->ineq
[j
][1+d
]))
1887 k
= isl_basic_map_alloc_inequality(bmap
);
1890 isl_seq_cpy(bmap
->ineq
[k
], bmap
->ineq
[i
],
1892 isl_seq_elim(bmap
->ineq
[k
], bmap
->ineq
[j
],
1893 1+d
, 1+total
, NULL
);
1895 isl_basic_map_drop_inequality(bmap
, i
);
1898 if (n_lower
> 0 && n_upper
> 0) {
1899 bmap
= isl_basic_map_normalize_constraints(bmap
);
1900 bmap
= isl_basic_map_remove_duplicate_constraints(bmap
,
1902 bmap
= isl_basic_map_gauss(bmap
, NULL
);
1903 bmap
= isl_basic_map_remove_redundancies(bmap
);
1907 if (ISL_F_ISSET(bmap
, ISL_BASIC_MAP_EMPTY
))
1911 ISL_F_CLR(bmap
, ISL_BASIC_MAP_NORMALIZED
);
1913 bmap
= isl_basic_map_gauss(bmap
, NULL
);
1916 isl_basic_map_free(bmap
);
1920 struct isl_basic_set
*isl_basic_set_eliminate_vars(
1921 struct isl_basic_set
*bset
, unsigned pos
, unsigned n
)
1923 return bset_from_bmap(isl_basic_map_eliminate_vars(bset_to_bmap(bset
),
1927 /* Eliminate the specified n dimensions starting at first from the
1928 * constraints, without removing the dimensions from the space.
1929 * If the set is rational, the dimensions are eliminated using Fourier-Motzkin.
1930 * Otherwise, they are projected out and the original space is restored.
1932 __isl_give isl_basic_map
*isl_basic_map_eliminate(
1933 __isl_take isl_basic_map
*bmap
,
1934 enum isl_dim_type type
, unsigned first
, unsigned n
)
1943 if (first
+ n
> isl_basic_map_dim(bmap
, type
) || first
+ n
< first
)
1944 isl_die(bmap
->ctx
, isl_error_invalid
,
1945 "index out of bounds", goto error
);
1947 if (ISL_F_ISSET(bmap
, ISL_BASIC_MAP_RATIONAL
)) {
1948 first
+= isl_basic_map_offset(bmap
, type
) - 1;
1949 bmap
= isl_basic_map_eliminate_vars(bmap
, first
, n
);
1950 return isl_basic_map_finalize(bmap
);
1953 space
= isl_basic_map_get_space(bmap
);
1954 bmap
= isl_basic_map_project_out(bmap
, type
, first
, n
);
1955 bmap
= isl_basic_map_insert_dims(bmap
, type
, first
, n
);
1956 bmap
= isl_basic_map_reset_space(bmap
, space
);
1959 isl_basic_map_free(bmap
);
1963 __isl_give isl_basic_set
*isl_basic_set_eliminate(
1964 __isl_take isl_basic_set
*bset
,
1965 enum isl_dim_type type
, unsigned first
, unsigned n
)
1967 return isl_basic_map_eliminate(bset
, type
, first
, n
);
1970 /* Remove all constraints from "bmap" that reference any unknown local
1971 * variables (directly or indirectly).
1973 * Dropping all constraints on a local variable will make it redundant,
1974 * so it will get removed implicitly by
1975 * isl_basic_map_drop_constraints_involving_dims. Some other local
1976 * variables may also end up becoming redundant if they only appear
1977 * in constraints together with the unknown local variable.
1978 * Therefore, start over after calling
1979 * isl_basic_map_drop_constraints_involving_dims.
1981 __isl_give isl_basic_map
*isl_basic_map_drop_constraint_involving_unknown_divs(
1982 __isl_take isl_basic_map
*bmap
)
1985 int i
, n_div
, o_div
;
1987 known
= isl_basic_map_divs_known(bmap
);
1989 return isl_basic_map_free(bmap
);
1993 n_div
= isl_basic_map_dim(bmap
, isl_dim_div
);
1994 o_div
= isl_basic_map_offset(bmap
, isl_dim_div
) - 1;
1996 for (i
= 0; i
< n_div
; ++i
) {
1997 known
= isl_basic_map_div_is_known(bmap
, i
);
1999 return isl_basic_map_free(bmap
);
2002 bmap
= remove_dependent_vars(bmap
, o_div
+ i
);
2003 bmap
= isl_basic_map_drop_constraints_involving_dims(bmap
,
2007 n_div
= isl_basic_map_dim(bmap
, isl_dim_div
);
2014 /* Remove all constraints from "map" that reference any unknown local
2015 * variables (directly or indirectly).
2017 * Since constraints may get dropped from the basic maps,
2018 * they may no longer be disjoint from each other.
2020 __isl_give isl_map
*isl_map_drop_constraint_involving_unknown_divs(
2021 __isl_take isl_map
*map
)
2026 known
= isl_map_divs_known(map
);
2028 return isl_map_free(map
);
2032 map
= isl_map_cow(map
);
2036 for (i
= 0; i
< map
->n
; ++i
) {
2038 isl_basic_map_drop_constraint_involving_unknown_divs(
2041 return isl_map_free(map
);
2045 ISL_F_CLR(map
, ISL_MAP_DISJOINT
);
2050 /* Don't assume equalities are in order, because align_divs
2051 * may have changed the order of the divs.
2053 static void compute_elimination_index(struct isl_basic_map
*bmap
, int *elim
)
2058 total
= isl_space_dim(bmap
->dim
, isl_dim_all
);
2059 for (d
= 0; d
< total
; ++d
)
2061 for (i
= 0; i
< bmap
->n_eq
; ++i
) {
2062 for (d
= total
- 1; d
>= 0; --d
) {
2063 if (isl_int_is_zero(bmap
->eq
[i
][1+d
]))
2071 static void set_compute_elimination_index(struct isl_basic_set
*bset
, int *elim
)
2073 compute_elimination_index(bset_to_bmap(bset
), elim
);
2076 static int reduced_using_equalities(isl_int
*dst
, isl_int
*src
,
2077 struct isl_basic_map
*bmap
, int *elim
)
2083 total
= isl_space_dim(bmap
->dim
, isl_dim_all
);
2084 for (d
= total
- 1; d
>= 0; --d
) {
2085 if (isl_int_is_zero(src
[1+d
]))
2090 isl_seq_cpy(dst
, src
, 1 + total
);
2093 isl_seq_elim(dst
, bmap
->eq
[elim
[d
]], 1 + d
, 1 + total
, NULL
);
2098 static int set_reduced_using_equalities(isl_int
*dst
, isl_int
*src
,
2099 struct isl_basic_set
*bset
, int *elim
)
2101 return reduced_using_equalities(dst
, src
,
2102 bset_to_bmap(bset
), elim
);
2105 static struct isl_basic_set
*isl_basic_set_reduce_using_equalities(
2106 struct isl_basic_set
*bset
, struct isl_basic_set
*context
)
2111 if (!bset
|| !context
)
2114 if (context
->n_eq
== 0) {
2115 isl_basic_set_free(context
);
2119 bset
= isl_basic_set_cow(bset
);
2123 elim
= isl_alloc_array(bset
->ctx
, int, isl_basic_set_n_dim(bset
));
2126 set_compute_elimination_index(context
, elim
);
2127 for (i
= 0; i
< bset
->n_eq
; ++i
)
2128 set_reduced_using_equalities(bset
->eq
[i
], bset
->eq
[i
],
2130 for (i
= 0; i
< bset
->n_ineq
; ++i
)
2131 set_reduced_using_equalities(bset
->ineq
[i
], bset
->ineq
[i
],
2133 isl_basic_set_free(context
);
2135 bset
= isl_basic_set_simplify(bset
);
2136 bset
= isl_basic_set_finalize(bset
);
2139 isl_basic_set_free(bset
);
2140 isl_basic_set_free(context
);
2144 /* For each inequality in "ineq" that is a shifted (more relaxed)
2145 * copy of an inequality in "context", mark the corresponding entry
2147 * If an inequality only has a non-negative constant term, then
2150 static isl_stat
mark_shifted_constraints(__isl_keep isl_mat
*ineq
,
2151 __isl_keep isl_basic_set
*context
, int *row
)
2153 struct isl_constraint_index ci
;
2158 if (!ineq
|| !context
)
2159 return isl_stat_error
;
2160 if (context
->n_ineq
== 0)
2162 if (setup_constraint_index(&ci
, context
) < 0)
2163 return isl_stat_error
;
2165 n_ineq
= isl_mat_rows(ineq
);
2166 total
= isl_mat_cols(ineq
) - 1;
2167 for (k
= 0; k
< n_ineq
; ++k
) {
2171 l
= isl_seq_first_non_zero(ineq
->row
[k
] + 1, total
);
2172 if (l
< 0 && isl_int_is_nonneg(ineq
->row
[k
][0])) {
2176 redundant
= constraint_index_is_redundant(&ci
, ineq
->row
[k
]);
2183 constraint_index_free(&ci
);
2186 constraint_index_free(&ci
);
2187 return isl_stat_error
;
2190 static struct isl_basic_set
*remove_shifted_constraints(
2191 struct isl_basic_set
*bset
, struct isl_basic_set
*context
)
2193 struct isl_constraint_index ci
;
2196 if (!bset
|| !context
)
2199 if (context
->n_ineq
== 0)
2201 if (setup_constraint_index(&ci
, context
) < 0)
2204 for (k
= 0; k
< bset
->n_ineq
; ++k
) {
2207 redundant
= constraint_index_is_redundant(&ci
, bset
->ineq
[k
]);
2212 bset
= isl_basic_set_cow(bset
);
2215 isl_basic_set_drop_inequality(bset
, k
);
2218 constraint_index_free(&ci
);
2221 constraint_index_free(&ci
);
2225 /* Remove constraints from "bmap" that are identical to constraints
2226 * in "context" or that are more relaxed (greater constant term).
2228 * We perform the test for shifted copies on the pure constraints
2229 * in remove_shifted_constraints.
2231 static __isl_give isl_basic_map
*isl_basic_map_remove_shifted_constraints(
2232 __isl_take isl_basic_map
*bmap
, __isl_take isl_basic_map
*context
)
2234 isl_basic_set
*bset
, *bset_context
;
2236 if (!bmap
|| !context
)
2239 if (bmap
->n_ineq
== 0 || context
->n_ineq
== 0) {
2240 isl_basic_map_free(context
);
2244 context
= isl_basic_map_align_divs(context
, bmap
);
2245 bmap
= isl_basic_map_align_divs(bmap
, context
);
2247 bset
= isl_basic_map_underlying_set(isl_basic_map_copy(bmap
));
2248 bset_context
= isl_basic_map_underlying_set(context
);
2249 bset
= remove_shifted_constraints(bset
, bset_context
);
2250 isl_basic_set_free(bset_context
);
2252 bmap
= isl_basic_map_overlying_set(bset
, bmap
);
2256 isl_basic_map_free(bmap
);
2257 isl_basic_map_free(context
);
2261 /* Does the (linear part of a) constraint "c" involve any of the "len"
2262 * "relevant" dimensions?
2264 static int is_related(isl_int
*c
, int len
, int *relevant
)
2268 for (i
= 0; i
< len
; ++i
) {
2271 if (!isl_int_is_zero(c
[i
]))
2278 /* Drop constraints from "bmap" that do not involve any of
2279 * the dimensions marked "relevant".
2281 static __isl_give isl_basic_map
*drop_unrelated_constraints(
2282 __isl_take isl_basic_map
*bmap
, int *relevant
)
2286 dim
= isl_basic_map_dim(bmap
, isl_dim_all
);
2287 for (i
= 0; i
< dim
; ++i
)
2293 for (i
= bmap
->n_eq
- 1; i
>= 0; --i
)
2294 if (!is_related(bmap
->eq
[i
] + 1, dim
, relevant
)) {
2295 bmap
= isl_basic_map_cow(bmap
);
2296 if (isl_basic_map_drop_equality(bmap
, i
) < 0)
2297 return isl_basic_map_free(bmap
);
2300 for (i
= bmap
->n_ineq
- 1; i
>= 0; --i
)
2301 if (!is_related(bmap
->ineq
[i
] + 1, dim
, relevant
)) {
2302 bmap
= isl_basic_map_cow(bmap
);
2303 if (isl_basic_map_drop_inequality(bmap
, i
) < 0)
2304 return isl_basic_map_free(bmap
);
2310 /* Update the groups in "group" based on the (linear part of a) constraint "c".
2312 * In particular, for any variable involved in the constraint,
2313 * find the actual group id from before and replace the group
2314 * of the corresponding variable by the minimal group of all
2315 * the variables involved in the constraint considered so far
2316 * (if this minimum is smaller) or replace the minimum by this group
2317 * (if the minimum is larger).
2319 * At the end, all the variables in "c" will (indirectly) point
2320 * to the minimal of the groups that they referred to originally.
2322 static void update_groups(int dim
, int *group
, isl_int
*c
)
2327 for (j
= 0; j
< dim
; ++j
) {
2328 if (isl_int_is_zero(c
[j
]))
2330 while (group
[j
] >= 0 && group
[group
[j
]] != group
[j
])
2331 group
[j
] = group
[group
[j
]];
2332 if (group
[j
] == min
)
2334 if (group
[j
] < min
) {
2335 if (min
>= 0 && min
< dim
)
2336 group
[min
] = group
[j
];
2339 group
[group
[j
]] = min
;
2343 /* Allocate an array of groups of variables, one for each variable
2344 * in "context", initialized to zero.
2346 static int *alloc_groups(__isl_keep isl_basic_set
*context
)
2351 dim
= isl_basic_set_dim(context
, isl_dim_set
);
2352 ctx
= isl_basic_set_get_ctx(context
);
2353 return isl_calloc_array(ctx
, int, dim
);
2356 /* Drop constraints from "bmap" that only involve variables that are
2357 * not related to any of the variables marked with a "-1" in "group".
2359 * We construct groups of variables that collect variables that
2360 * (indirectly) appear in some common constraint of "bmap".
2361 * Each group is identified by the first variable in the group,
2362 * except for the special group of variables that was already identified
2363 * in the input as -1 (or are related to those variables).
2364 * If group[i] is equal to i (or -1), then the group of i is i (or -1),
2365 * otherwise the group of i is the group of group[i].
2367 * We first initialize groups for the remaining variables.
2368 * Then we iterate over the constraints of "bmap" and update the
2369 * group of the variables in the constraint by the smallest group.
2370 * Finally, we resolve indirect references to groups by running over
2373 * After computing the groups, we drop constraints that do not involve
2374 * any variables in the -1 group.
2376 __isl_give isl_basic_map
*isl_basic_map_drop_unrelated_constraints(
2377 __isl_take isl_basic_map
*bmap
, __isl_take
int *group
)
2386 dim
= isl_basic_map_dim(bmap
, isl_dim_all
);
2389 for (i
= 0; i
< dim
; ++i
)
2391 last
= group
[i
] = i
;
2397 for (i
= 0; i
< bmap
->n_eq
; ++i
)
2398 update_groups(dim
, group
, bmap
->eq
[i
] + 1);
2399 for (i
= 0; i
< bmap
->n_ineq
; ++i
)
2400 update_groups(dim
, group
, bmap
->ineq
[i
] + 1);
2402 for (i
= 0; i
< dim
; ++i
)
2404 group
[i
] = group
[group
[i
]];
2406 for (i
= 0; i
< dim
; ++i
)
2407 group
[i
] = group
[i
] == -1;
2409 bmap
= drop_unrelated_constraints(bmap
, group
);
2415 /* Drop constraints from "context" that are irrelevant for computing
2416 * the gist of "bset".
2418 * In particular, drop constraints in variables that are not related
2419 * to any of the variables involved in the constraints of "bset"
2420 * in the sense that there is no sequence of constraints that connects them.
2422 * We first mark all variables that appear in "bset" as belonging
2423 * to a "-1" group and then continue with group_and_drop_irrelevant_constraints.
2425 static __isl_give isl_basic_set
*drop_irrelevant_constraints(
2426 __isl_take isl_basic_set
*context
, __isl_keep isl_basic_set
*bset
)
2432 if (!context
|| !bset
)
2433 return isl_basic_set_free(context
);
2435 group
= alloc_groups(context
);
2438 return isl_basic_set_free(context
);
2440 dim
= isl_basic_set_dim(bset
, isl_dim_set
);
2441 for (i
= 0; i
< dim
; ++i
) {
2442 for (j
= 0; j
< bset
->n_eq
; ++j
)
2443 if (!isl_int_is_zero(bset
->eq
[j
][1 + i
]))
2445 if (j
< bset
->n_eq
) {
2449 for (j
= 0; j
< bset
->n_ineq
; ++j
)
2450 if (!isl_int_is_zero(bset
->ineq
[j
][1 + i
]))
2452 if (j
< bset
->n_ineq
)
2456 return isl_basic_map_drop_unrelated_constraints(context
, group
);
2459 /* Drop constraints from "context" that are irrelevant for computing
2460 * the gist of the inequalities "ineq".
2461 * Inequalities in "ineq" for which the corresponding element of row
2462 * is set to -1 have already been marked for removal and should be ignored.
2464 * In particular, drop constraints in variables that are not related
2465 * to any of the variables involved in "ineq"
2466 * in the sense that there is no sequence of constraints that connects them.
2468 * We first mark all variables that appear in "bset" as belonging
2469 * to a "-1" group and then continue with group_and_drop_irrelevant_constraints.
2471 static __isl_give isl_basic_set
*drop_irrelevant_constraints_marked(
2472 __isl_take isl_basic_set
*context
, __isl_keep isl_mat
*ineq
, int *row
)
2478 if (!context
|| !ineq
)
2479 return isl_basic_set_free(context
);
2481 group
= alloc_groups(context
);
2484 return isl_basic_set_free(context
);
2486 dim
= isl_basic_set_dim(context
, isl_dim_set
);
2487 n
= isl_mat_rows(ineq
);
2488 for (i
= 0; i
< dim
; ++i
) {
2489 for (j
= 0; j
< n
; ++j
) {
2492 if (!isl_int_is_zero(ineq
->row
[j
][1 + i
]))
2499 return isl_basic_map_drop_unrelated_constraints(context
, group
);
2502 /* Do all "n" entries of "row" contain a negative value?
2504 static int all_neg(int *row
, int n
)
2508 for (i
= 0; i
< n
; ++i
)
2515 /* Update the inequalities in "bset" based on the information in "row"
2518 * In particular, the array "row" contains either -1, meaning that
2519 * the corresponding inequality of "bset" is redundant, or the index
2520 * of an inequality in "tab".
2522 * If the row entry is -1, then drop the inequality.
2523 * Otherwise, if the constraint is marked redundant in the tableau,
2524 * then drop the inequality. Similarly, if it is marked as an equality
2525 * in the tableau, then turn the inequality into an equality and
2526 * perform Gaussian elimination.
2528 static __isl_give isl_basic_set
*update_ineq(__isl_take isl_basic_set
*bset
,
2529 __isl_keep
int *row
, struct isl_tab
*tab
)
2534 int found_equality
= 0;
2538 if (tab
&& tab
->empty
)
2539 return isl_basic_set_set_to_empty(bset
);
2541 n_ineq
= bset
->n_ineq
;
2542 for (i
= n_ineq
- 1; i
>= 0; --i
) {
2544 if (isl_basic_set_drop_inequality(bset
, i
) < 0)
2545 return isl_basic_set_free(bset
);
2551 if (isl_tab_is_equality(tab
, n_eq
+ row
[i
])) {
2552 isl_basic_map_inequality_to_equality(bset
, i
);
2554 } else if (isl_tab_is_redundant(tab
, n_eq
+ row
[i
])) {
2555 if (isl_basic_set_drop_inequality(bset
, i
) < 0)
2556 return isl_basic_set_free(bset
);
2561 bset
= isl_basic_set_gauss(bset
, NULL
);
2562 bset
= isl_basic_set_finalize(bset
);
2566 /* Update the inequalities in "bset" based on the information in "row"
2567 * and "tab" and free all arguments (other than "bset").
2569 static __isl_give isl_basic_set
*update_ineq_free(
2570 __isl_take isl_basic_set
*bset
, __isl_take isl_mat
*ineq
,
2571 __isl_take isl_basic_set
*context
, __isl_take
int *row
,
2572 struct isl_tab
*tab
)
2575 isl_basic_set_free(context
);
2577 bset
= update_ineq(bset
, row
, tab
);
2584 /* Remove all information from bset that is redundant in the context
2586 * "ineq" contains the (possibly transformed) inequalities of "bset",
2587 * in the same order.
2588 * The (explicit) equalities of "bset" are assumed to have been taken
2589 * into account by the transformation such that only the inequalities
2591 * "context" is assumed not to be empty.
2593 * "row" keeps track of the constraint index of a "bset" inequality in "tab".
2594 * A value of -1 means that the inequality is obviously redundant and may
2595 * not even appear in "tab".
2597 * We first mark the inequalities of "bset"
2598 * that are obviously redundant with respect to some inequality in "context".
2599 * Then we remove those constraints from "context" that have become
2600 * irrelevant for computing the gist of "bset".
2601 * Note that this removal of constraints cannot be replaced by
2602 * a factorization because factors in "bset" may still be connected
2603 * to each other through constraints in "context".
2605 * If there are any inequalities left, we construct a tableau for
2606 * the context and then add the inequalities of "bset".
2607 * Before adding these inequalities, we freeze all constraints such that
2608 * they won't be considered redundant in terms of the constraints of "bset".
2609 * Then we detect all redundant constraints (among the
2610 * constraints that weren't frozen), first by checking for redundancy in the
2611 * the tableau and then by checking if replacing a constraint by its negation
2612 * would lead to an empty set. This last step is fairly expensive
2613 * and could be optimized by more reuse of the tableau.
2614 * Finally, we update bset according to the results.
2616 static __isl_give isl_basic_set
*uset_gist_full(__isl_take isl_basic_set
*bset
,
2617 __isl_take isl_mat
*ineq
, __isl_take isl_basic_set
*context
)
2622 isl_basic_set
*combined
= NULL
;
2623 struct isl_tab
*tab
= NULL
;
2624 unsigned n_eq
, context_ineq
;
2627 if (!bset
|| !ineq
|| !context
)
2630 if (bset
->n_ineq
== 0 || isl_basic_set_plain_is_universe(context
)) {
2631 isl_basic_set_free(context
);
2636 ctx
= isl_basic_set_get_ctx(context
);
2637 row
= isl_calloc_array(ctx
, int, bset
->n_ineq
);
2641 if (mark_shifted_constraints(ineq
, context
, row
) < 0)
2643 if (all_neg(row
, bset
->n_ineq
))
2644 return update_ineq_free(bset
, ineq
, context
, row
, NULL
);
2646 context
= drop_irrelevant_constraints_marked(context
, ineq
, row
);
2649 if (isl_basic_set_plain_is_universe(context
))
2650 return update_ineq_free(bset
, ineq
, context
, row
, NULL
);
2652 n_eq
= context
->n_eq
;
2653 context_ineq
= context
->n_ineq
;
2654 combined
= isl_basic_set_cow(isl_basic_set_copy(context
));
2655 combined
= isl_basic_set_extend_constraints(combined
, 0, bset
->n_ineq
);
2656 tab
= isl_tab_from_basic_set(combined
, 0);
2657 for (i
= 0; i
< context_ineq
; ++i
)
2658 if (isl_tab_freeze_constraint(tab
, n_eq
+ i
) < 0)
2660 if (isl_tab_extend_cons(tab
, bset
->n_ineq
) < 0)
2663 for (i
= 0; i
< bset
->n_ineq
; ++i
) {
2666 combined
= isl_basic_set_add_ineq(combined
, ineq
->row
[i
]);
2667 if (isl_tab_add_ineq(tab
, ineq
->row
[i
]) < 0)
2671 if (isl_tab_detect_implicit_equalities(tab
) < 0)
2673 if (isl_tab_detect_redundant(tab
) < 0)
2675 total
= isl_basic_set_total_dim(bset
);
2676 for (i
= bset
->n_ineq
- 1; i
>= 0; --i
) {
2677 isl_basic_set
*test
;
2683 if (tab
->con
[n_eq
+ r
].is_redundant
)
2685 test
= isl_basic_set_dup(combined
);
2686 if (isl_inequality_negate(test
, r
) < 0)
2687 test
= isl_basic_set_free(test
);
2688 test
= isl_basic_set_update_from_tab(test
, tab
);
2689 is_empty
= isl_basic_set_is_empty(test
);
2690 isl_basic_set_free(test
);
2694 tab
->con
[n_eq
+ r
].is_redundant
= 1;
2696 bset
= update_ineq_free(bset
, ineq
, context
, row
, tab
);
2698 ISL_F_SET(bset
, ISL_BASIC_SET_NO_IMPLICIT
);
2699 ISL_F_SET(bset
, ISL_BASIC_SET_NO_REDUNDANT
);
2702 isl_basic_set_free(combined
);
2708 isl_basic_set_free(combined
);
2709 isl_basic_set_free(context
);
2710 isl_basic_set_free(bset
);
2714 /* Extract the inequalities of "bset" as an isl_mat.
2716 static __isl_give isl_mat
*extract_ineq(__isl_keep isl_basic_set
*bset
)
2725 ctx
= isl_basic_set_get_ctx(bset
);
2726 total
= isl_basic_set_total_dim(bset
);
2727 ineq
= isl_mat_sub_alloc6(ctx
, bset
->ineq
, 0, bset
->n_ineq
,
2733 /* Remove all information from "bset" that is redundant in the context
2734 * of "context", for the case where both "bset" and "context" are
2737 static __isl_give isl_basic_set
*uset_gist_uncompressed(
2738 __isl_take isl_basic_set
*bset
, __isl_take isl_basic_set
*context
)
2742 ineq
= extract_ineq(bset
);
2743 return uset_gist_full(bset
, ineq
, context
);
2746 /* Remove all information from "bset" that is redundant in the context
2747 * of "context", for the case where the combined equalities of
2748 * "bset" and "context" allow for a compression that can be obtained
2749 * by preapplication of "T".
2751 * "bset" itself is not transformed by "T". Instead, the inequalities
2752 * are extracted from "bset" and those are transformed by "T".
2753 * uset_gist_full then determines which of the transformed inequalities
2754 * are redundant with respect to the transformed "context" and removes
2755 * the corresponding inequalities from "bset".
2757 * After preapplying "T" to the inequalities, any common factor is
2758 * removed from the coefficients. If this results in a tightening
2759 * of the constant term, then the same tightening is applied to
2760 * the corresponding untransformed inequality in "bset".
2761 * That is, if after plugging in T, a constraint f(x) >= 0 is of the form
2765 * with 0 <= r < g, then it is equivalent to
2769 * This means that f(x) >= 0 is equivalent to f(x) - r >= 0 in the affine
2770 * subspace compressed by T since the latter would be transformed to
2774 static __isl_give isl_basic_set
*uset_gist_compressed(
2775 __isl_take isl_basic_set
*bset
, __isl_take isl_basic_set
*context
,
2776 __isl_take isl_mat
*T
)
2780 int i
, n_row
, n_col
;
2783 ineq
= extract_ineq(bset
);
2784 ineq
= isl_mat_product(ineq
, isl_mat_copy(T
));
2785 context
= isl_basic_set_preimage(context
, T
);
2787 if (!ineq
|| !context
)
2789 if (isl_basic_set_plain_is_empty(context
)) {
2791 isl_basic_set_free(context
);
2792 return isl_basic_set_set_to_empty(bset
);
2795 ctx
= isl_mat_get_ctx(ineq
);
2796 n_row
= isl_mat_rows(ineq
);
2797 n_col
= isl_mat_cols(ineq
);
2799 for (i
= 0; i
< n_row
; ++i
) {
2800 isl_seq_gcd(ineq
->row
[i
] + 1, n_col
- 1, &ctx
->normalize_gcd
);
2801 if (isl_int_is_zero(ctx
->normalize_gcd
))
2803 if (isl_int_is_one(ctx
->normalize_gcd
))
2805 isl_seq_scale_down(ineq
->row
[i
] + 1, ineq
->row
[i
] + 1,
2806 ctx
->normalize_gcd
, n_col
- 1);
2807 isl_int_fdiv_r(rem
, ineq
->row
[i
][0], ctx
->normalize_gcd
);
2808 isl_int_fdiv_q(ineq
->row
[i
][0],
2809 ineq
->row
[i
][0], ctx
->normalize_gcd
);
2810 if (isl_int_is_zero(rem
))
2812 bset
= isl_basic_set_cow(bset
);
2815 isl_int_sub(bset
->ineq
[i
][0], bset
->ineq
[i
][0], rem
);
2819 return uset_gist_full(bset
, ineq
, context
);
2822 isl_basic_set_free(context
);
2823 isl_basic_set_free(bset
);
2827 /* Project "bset" onto the variables that are involved in "template".
2829 static __isl_give isl_basic_set
*project_onto_involved(
2830 __isl_take isl_basic_set
*bset
, __isl_keep isl_basic_set
*template)
2834 if (!bset
|| !template)
2835 return isl_basic_set_free(bset
);
2837 n
= isl_basic_set_dim(template, isl_dim_set
);
2839 for (i
= 0; i
< n
; ++i
) {
2842 involved
= isl_basic_set_involves_dims(template,
2845 return isl_basic_set_free(bset
);
2848 bset
= isl_basic_set_eliminate_vars(bset
, i
, 1);
2854 /* Remove all information from bset that is redundant in the context
2855 * of context. In particular, equalities that are linear combinations
2856 * of those in context are removed. Then the inequalities that are
2857 * redundant in the context of the equalities and inequalities of
2858 * context are removed.
2860 * First of all, we drop those constraints from "context"
2861 * that are irrelevant for computing the gist of "bset".
2862 * Alternatively, we could factorize the intersection of "context" and "bset".
2864 * We first compute the intersection of the integer affine hulls
2865 * of "bset" and "context",
2866 * compute the gist inside this intersection and then reduce
2867 * the constraints with respect to the equalities of the context
2868 * that only involve variables already involved in the input.
2870 * If two constraints are mutually redundant, then uset_gist_full
2871 * will remove the second of those constraints. We therefore first
2872 * sort the constraints so that constraints not involving existentially
2873 * quantified variables are given precedence over those that do.
2874 * We have to perform this sorting before the variable compression,
2875 * because that may effect the order of the variables.
2877 static __isl_give isl_basic_set
*uset_gist(__isl_take isl_basic_set
*bset
,
2878 __isl_take isl_basic_set
*context
)
2883 isl_basic_set
*aff_context
;
2886 if (!bset
|| !context
)
2889 context
= drop_irrelevant_constraints(context
, bset
);
2891 bset
= isl_basic_set_detect_equalities(bset
);
2892 aff
= isl_basic_set_copy(bset
);
2893 aff
= isl_basic_set_plain_affine_hull(aff
);
2894 context
= isl_basic_set_detect_equalities(context
);
2895 aff_context
= isl_basic_set_copy(context
);
2896 aff_context
= isl_basic_set_plain_affine_hull(aff_context
);
2897 aff
= isl_basic_set_intersect(aff
, aff_context
);
2900 if (isl_basic_set_plain_is_empty(aff
)) {
2901 isl_basic_set_free(bset
);
2902 isl_basic_set_free(context
);
2905 bset
= isl_basic_set_sort_constraints(bset
);
2906 if (aff
->n_eq
== 0) {
2907 isl_basic_set_free(aff
);
2908 return uset_gist_uncompressed(bset
, context
);
2910 total
= isl_basic_set_total_dim(bset
);
2911 eq
= isl_mat_sub_alloc6(bset
->ctx
, aff
->eq
, 0, aff
->n_eq
, 0, 1 + total
);
2912 eq
= isl_mat_cow(eq
);
2913 T
= isl_mat_variable_compression(eq
, NULL
);
2914 isl_basic_set_free(aff
);
2915 if (T
&& T
->n_col
== 0) {
2917 isl_basic_set_free(context
);
2918 return isl_basic_set_set_to_empty(bset
);
2921 aff_context
= isl_basic_set_affine_hull(isl_basic_set_copy(context
));
2922 aff_context
= project_onto_involved(aff_context
, bset
);
2924 bset
= uset_gist_compressed(bset
, context
, T
);
2925 bset
= isl_basic_set_reduce_using_equalities(bset
, aff_context
);
2928 ISL_F_SET(bset
, ISL_BASIC_SET_NO_IMPLICIT
);
2929 ISL_F_SET(bset
, ISL_BASIC_SET_NO_REDUNDANT
);
2934 isl_basic_set_free(bset
);
2935 isl_basic_set_free(context
);
2939 /* Return the number of equality constraints in "bmap" that involve
2940 * local variables. This function assumes that Gaussian elimination
2941 * has been applied to the equality constraints.
2943 static int n_div_eq(__isl_keep isl_basic_map
*bmap
)
2951 if (bmap
->n_eq
== 0)
2954 total
= isl_basic_map_dim(bmap
, isl_dim_all
);
2955 n_div
= isl_basic_map_dim(bmap
, isl_dim_div
);
2958 for (i
= 0; i
< bmap
->n_eq
; ++i
)
2959 if (isl_seq_first_non_zero(bmap
->eq
[i
] + 1 + total
,
2966 /* Construct a basic map in "space" defined by the equality constraints in "eq".
2967 * The constraints are assumed not to involve any local variables.
2969 static __isl_give isl_basic_map
*basic_map_from_equalities(
2970 __isl_take isl_space
*space
, __isl_take isl_mat
*eq
)
2973 isl_basic_map
*bmap
= NULL
;
2978 if (1 + isl_space_dim(space
, isl_dim_all
) != eq
->n_col
)
2979 isl_die(isl_space_get_ctx(space
), isl_error_internal
,
2980 "unexpected number of columns", goto error
);
2982 bmap
= isl_basic_map_alloc_space(isl_space_copy(space
),
2984 for (i
= 0; i
< eq
->n_row
; ++i
) {
2985 k
= isl_basic_map_alloc_equality(bmap
);
2988 isl_seq_cpy(bmap
->eq
[k
], eq
->row
[i
], eq
->n_col
);
2991 isl_space_free(space
);
2995 isl_space_free(space
);
2997 isl_basic_map_free(bmap
);
3001 /* Construct and return a variable compression based on the equality
3002 * constraints in "bmap1" and "bmap2" that do not involve the local variables.
3003 * "n1" is the number of (initial) equality constraints in "bmap1"
3004 * that do involve local variables.
3005 * "n2" is the number of (initial) equality constraints in "bmap2"
3006 * that do involve local variables.
3007 * "total" is the total number of other variables.
3008 * This function assumes that Gaussian elimination
3009 * has been applied to the equality constraints in both "bmap1" and "bmap2"
3010 * such that the equality constraints not involving local variables
3011 * are those that start at "n1" or "n2".
3013 * If either of "bmap1" and "bmap2" does not have such equality constraints,
3014 * then simply compute the compression based on the equality constraints
3015 * in the other basic map.
3016 * Otherwise, combine the equality constraints from both into a new
3017 * basic map such that Gaussian elimination can be applied to this combination
3018 * and then construct a variable compression from the resulting
3019 * equality constraints.
3021 static __isl_give isl_mat
*combined_variable_compression(
3022 __isl_keep isl_basic_map
*bmap1
, int n1
,
3023 __isl_keep isl_basic_map
*bmap2
, int n2
, int total
)
3026 isl_mat
*E1
, *E2
, *V
;
3027 isl_basic_map
*bmap
;
3029 ctx
= isl_basic_map_get_ctx(bmap1
);
3030 if (bmap1
->n_eq
== n1
) {
3031 E2
= isl_mat_sub_alloc6(ctx
, bmap2
->eq
,
3032 n2
, bmap2
->n_eq
- n2
, 0, 1 + total
);
3033 return isl_mat_variable_compression(E2
, NULL
);
3035 if (bmap2
->n_eq
== n2
) {
3036 E1
= isl_mat_sub_alloc6(ctx
, bmap1
->eq
,
3037 n1
, bmap1
->n_eq
- n1
, 0, 1 + total
);
3038 return isl_mat_variable_compression(E1
, NULL
);
3040 E1
= isl_mat_sub_alloc6(ctx
, bmap1
->eq
,
3041 n1
, bmap1
->n_eq
- n1
, 0, 1 + total
);
3042 E2
= isl_mat_sub_alloc6(ctx
, bmap2
->eq
,
3043 n2
, bmap2
->n_eq
- n2
, 0, 1 + total
);
3044 E1
= isl_mat_concat(E1
, E2
);
3045 bmap
= basic_map_from_equalities(isl_basic_map_get_space(bmap1
), E1
);
3046 bmap
= isl_basic_map_gauss(bmap
, NULL
);
3049 E1
= isl_mat_sub_alloc6(ctx
, bmap
->eq
, 0, bmap
->n_eq
, 0, 1 + total
);
3050 V
= isl_mat_variable_compression(E1
, NULL
);
3051 isl_basic_map_free(bmap
);
3056 /* Extract the stride constraints from "bmap", compressed
3057 * with respect to both the stride constraints in "context" and
3058 * the remaining equality constraints in both "bmap" and "context".
3059 * "bmap_n_eq" is the number of (initial) stride constraints in "bmap".
3060 * "context_n_eq" is the number of (initial) stride constraints in "context".
3062 * Let x be all variables in "bmap" (and "context") other than the local
3063 * variables. First compute a variable compression
3067 * based on the non-stride equality constraints in "bmap" and "context".
3068 * Consider the stride constraints of "context",
3072 * with y the local variables and plug in the variable compression,
3075 * A(V x') + B(y) = 0
3077 * Use these constraints to compute a parameter compression on x'
3081 * Now consider the stride constraints of "bmap"
3085 * and plug in x = V*T x''.
3086 * That is, return A = [C*V*T D].
3088 static __isl_give isl_mat
*extract_compressed_stride_constraints(
3089 __isl_keep isl_basic_map
*bmap
, int bmap_n_eq
,
3090 __isl_keep isl_basic_map
*context
, int context_n_eq
)
3094 isl_mat
*A
, *B
, *T
, *V
;
3096 total
= isl_basic_map_dim(context
, isl_dim_all
);
3097 n_div
= isl_basic_map_dim(context
, isl_dim_div
);
3100 ctx
= isl_basic_map_get_ctx(bmap
);
3102 V
= combined_variable_compression(bmap
, bmap_n_eq
,
3103 context
, context_n_eq
, total
);
3105 A
= isl_mat_sub_alloc6(ctx
, context
->eq
, 0, context_n_eq
, 0, 1 + total
);
3106 B
= isl_mat_sub_alloc6(ctx
, context
->eq
,
3107 0, context_n_eq
, 1 + total
, n_div
);
3108 A
= isl_mat_product(A
, isl_mat_copy(V
));
3109 T
= isl_mat_parameter_compression_ext(A
, B
);
3110 T
= isl_mat_product(V
, T
);
3112 n_div
= isl_basic_map_dim(bmap
, isl_dim_div
);
3113 T
= isl_mat_diagonal(T
, isl_mat_identity(ctx
, n_div
));
3115 A
= isl_mat_sub_alloc6(ctx
, bmap
->eq
,
3116 0, bmap_n_eq
, 0, 1 + total
+ n_div
);
3117 A
= isl_mat_product(A
, T
);
3122 /* Remove the prime factors from *g that have an exponent that
3123 * is strictly smaller than the exponent in "c".
3124 * All exponents in *g are known to be smaller than or equal
3127 * That is, if *g is equal to
3129 * p_1^{e_1} p_2^{e_2} ... p_n^{e_n}
3131 * and "c" is equal to
3133 * p_1^{f_1} p_2^{f_2} ... p_n^{f_n}
3137 * p_1^{e_1 * (e_1 = f_1)} p_2^{e_2 * (e_2 = f_2)} ...
3138 * p_n^{e_n * (e_n = f_n)}
3140 * If e_i = f_i, then c / *g does not have any p_i factors and therefore
3141 * neither does the gcd of *g and c / *g.
3142 * If e_i < f_i, then the gcd of *g and c / *g has a positive
3143 * power min(e_i, s_i) of p_i with s_i = f_i - e_i among its factors.
3144 * Dividing *g by this gcd therefore strictly reduces the exponent
3145 * of the prime factors that need to be removed, while leaving the
3146 * other prime factors untouched.
3147 * Repeating this process until gcd(*g, c / *g) = 1 therefore
3148 * removes all undesired factors, without removing any others.
3150 static void remove_incomplete_powers(isl_int
*g
, isl_int c
)
3156 isl_int_divexact(t
, c
, *g
);
3157 isl_int_gcd(t
, t
, *g
);
3158 if (isl_int_is_one(t
))
3160 isl_int_divexact(*g
, *g
, t
);
3165 /* Reduce the "n" stride constraints in "bmap" based on a copy "A"
3166 * of the same stride constraints in a compressed space that exploits
3167 * all equalities in the context and the other equalities in "bmap".
3169 * If the stride constraints of "bmap" are of the form
3173 * then A is of the form
3177 * If any of these constraints involves only a single local variable y,
3178 * then the constraint appears as
3188 * Let g be the gcd of m and the coefficients of h.
3189 * Then, in particular, g is a divisor of the coefficients of h and
3193 * is known to be a multiple of g.
3194 * If some prime factor in m appears with the same exponent in g,
3195 * then it can be removed from m because f(x) is already known
3196 * to be a multiple of g and therefore in particular of this power
3197 * of the prime factors.
3198 * Prime factors that appear with a smaller exponent in g cannot
3199 * be removed from m.
3200 * Let g' be the divisor of g containing all prime factors that
3201 * appear with the same exponent in m and g, then
3205 * can be replaced by
3207 * f(x) + m/g' y_i' = 0
3209 * Note that (if g' != 1) this changes the explicit representation
3210 * of y_i to that of y_i', so the integer division at position i
3211 * is marked unknown and later recomputed by a call to
3212 * isl_basic_map_gauss.
3214 static __isl_give isl_basic_map
*reduce_stride_constraints(
3215 __isl_take isl_basic_map
*bmap
, int n
, __isl_keep isl_mat
*A
)
3223 return isl_basic_map_free(bmap
);
3225 total
= isl_basic_map_dim(bmap
, isl_dim_all
);
3226 n_div
= isl_basic_map_dim(bmap
, isl_dim_div
);
3230 for (i
= 0; i
< n
; ++i
) {
3233 div
= isl_seq_first_non_zero(bmap
->eq
[i
] + 1 + total
, n_div
);
3235 isl_die(isl_basic_map_get_ctx(bmap
), isl_error_internal
,
3236 "equality constraints modified unexpectedly",
3238 if (isl_seq_first_non_zero(bmap
->eq
[i
] + 1 + total
+ div
+ 1,
3239 n_div
- div
- 1) != -1)
3241 if (isl_mat_row_gcd(A
, i
, &gcd
) < 0)
3243 if (isl_int_is_one(gcd
))
3245 remove_incomplete_powers(&gcd
, bmap
->eq
[i
][1 + total
+ div
]);
3246 if (isl_int_is_one(gcd
))
3248 isl_int_divexact(bmap
->eq
[i
][1 + total
+ div
],
3249 bmap
->eq
[i
][1 + total
+ div
], gcd
);
3250 bmap
= isl_basic_map_mark_div_unknown(bmap
, div
);
3258 bmap
= isl_basic_map_gauss(bmap
, NULL
);
3263 isl_basic_map_free(bmap
);
3267 /* Simplify the stride constraints in "bmap" based on
3268 * the remaining equality constraints in "bmap" and all equality
3269 * constraints in "context".
3270 * Only do this if both "bmap" and "context" have stride constraints.
3272 * First extract a copy of the stride constraints in "bmap" in a compressed
3273 * space exploiting all the other equality constraints and then
3274 * use this compressed copy to simplify the original stride constraints.
3276 static __isl_give isl_basic_map
*gist_strides(__isl_take isl_basic_map
*bmap
,
3277 __isl_keep isl_basic_map
*context
)
3279 int bmap_n_eq
, context_n_eq
;
3282 if (!bmap
|| !context
)
3283 return isl_basic_map_free(bmap
);
3285 bmap_n_eq
= n_div_eq(bmap
);
3286 context_n_eq
= n_div_eq(context
);
3288 if (bmap_n_eq
< 0 || context_n_eq
< 0)
3289 return isl_basic_map_free(bmap
);
3290 if (bmap_n_eq
== 0 || context_n_eq
== 0)
3293 A
= extract_compressed_stride_constraints(bmap
, bmap_n_eq
,
3294 context
, context_n_eq
);
3295 bmap
= reduce_stride_constraints(bmap
, bmap_n_eq
, A
);
3302 /* Return a basic map that has the same intersection with "context" as "bmap"
3303 * and that is as "simple" as possible.
3305 * The core computation is performed on the pure constraints.
3306 * When we add back the meaning of the integer divisions, we need
3307 * to (re)introduce the div constraints. If we happen to have
3308 * discovered that some of these integer divisions are equal to
3309 * some affine combination of other variables, then these div
3310 * constraints may end up getting simplified in terms of the equalities,
3311 * resulting in extra inequalities on the other variables that
3312 * may have been removed already or that may not even have been
3313 * part of the input. We try and remove those constraints of
3314 * this form that are most obviously redundant with respect to
3315 * the context. We also remove those div constraints that are
3316 * redundant with respect to the other constraints in the result.
3318 * The stride constraints among the equality constraints in "bmap" are
3319 * also simplified with respecting to the other equality constraints
3320 * in "bmap" and with respect to all equality constraints in "context".
3322 struct isl_basic_map
*isl_basic_map_gist(struct isl_basic_map
*bmap
,
3323 struct isl_basic_map
*context
)
3325 isl_basic_set
*bset
, *eq
;
3326 isl_basic_map
*eq_bmap
;
3327 unsigned total
, n_div
, extra
, n_eq
, n_ineq
;
3329 if (!bmap
|| !context
)
3332 if (isl_basic_map_plain_is_universe(bmap
)) {
3333 isl_basic_map_free(context
);
3336 if (isl_basic_map_plain_is_empty(context
)) {
3337 isl_space
*space
= isl_basic_map_get_space(bmap
);
3338 isl_basic_map_free(bmap
);
3339 isl_basic_map_free(context
);
3340 return isl_basic_map_universe(space
);
3342 if (isl_basic_map_plain_is_empty(bmap
)) {
3343 isl_basic_map_free(context
);
3347 bmap
= isl_basic_map_remove_redundancies(bmap
);
3348 context
= isl_basic_map_remove_redundancies(context
);
3352 context
= isl_basic_map_align_divs(context
, bmap
);
3353 n_div
= isl_basic_map_dim(context
, isl_dim_div
);
3354 total
= isl_basic_map_dim(bmap
, isl_dim_all
);
3355 extra
= n_div
- isl_basic_map_dim(bmap
, isl_dim_div
);
3357 bset
= isl_basic_map_underlying_set(isl_basic_map_copy(bmap
));
3358 bset
= isl_basic_set_add_dims(bset
, isl_dim_set
, extra
);
3359 bset
= uset_gist(bset
,
3360 isl_basic_map_underlying_set(isl_basic_map_copy(context
)));
3361 bset
= isl_basic_set_project_out(bset
, isl_dim_set
, total
, extra
);
3363 if (!bset
|| bset
->n_eq
== 0 || n_div
== 0 ||
3364 isl_basic_set_plain_is_empty(bset
)) {
3365 isl_basic_map_free(context
);
3366 return isl_basic_map_overlying_set(bset
, bmap
);
3370 n_ineq
= bset
->n_ineq
;
3371 eq
= isl_basic_set_copy(bset
);
3372 eq
= isl_basic_set_cow(eq
);
3373 if (isl_basic_set_free_inequality(eq
, n_ineq
) < 0)
3374 eq
= isl_basic_set_free(eq
);
3375 if (isl_basic_set_free_equality(bset
, n_eq
) < 0)
3376 bset
= isl_basic_set_free(bset
);
3378 eq_bmap
= isl_basic_map_overlying_set(eq
, isl_basic_map_copy(bmap
));
3379 eq_bmap
= gist_strides(eq_bmap
, context
);
3380 eq_bmap
= isl_basic_map_remove_shifted_constraints(eq_bmap
, context
);
3381 bmap
= isl_basic_map_overlying_set(bset
, bmap
);
3382 bmap
= isl_basic_map_intersect(bmap
, eq_bmap
);
3383 bmap
= isl_basic_map_remove_redundancies(bmap
);
3387 isl_basic_map_free(bmap
);
3388 isl_basic_map_free(context
);
3393 * Assumes context has no implicit divs.
3395 __isl_give isl_map
*isl_map_gist_basic_map(__isl_take isl_map
*map
,
3396 __isl_take isl_basic_map
*context
)
3400 if (!map
|| !context
)
3403 if (isl_basic_map_plain_is_empty(context
)) {
3404 isl_space
*space
= isl_map_get_space(map
);
3406 isl_basic_map_free(context
);
3407 return isl_map_universe(space
);
3410 context
= isl_basic_map_remove_redundancies(context
);
3411 map
= isl_map_cow(map
);
3412 if (!map
|| !context
)
3414 isl_assert(map
->ctx
, isl_space_is_equal(map
->dim
, context
->dim
), goto error
);
3415 map
= isl_map_compute_divs(map
);
3418 for (i
= map
->n
- 1; i
>= 0; --i
) {
3419 map
->p
[i
] = isl_basic_map_gist(map
->p
[i
],
3420 isl_basic_map_copy(context
));
3423 if (isl_basic_map_plain_is_empty(map
->p
[i
])) {
3424 isl_basic_map_free(map
->p
[i
]);
3425 if (i
!= map
->n
- 1)
3426 map
->p
[i
] = map
->p
[map
->n
- 1];
3430 isl_basic_map_free(context
);
3431 ISL_F_CLR(map
, ISL_MAP_NORMALIZED
);
3435 isl_basic_map_free(context
);
3439 /* Drop all inequalities from "bmap" that also appear in "context".
3440 * "context" is assumed to have only known local variables and
3441 * the initial local variables of "bmap" are assumed to be the same
3442 * as those of "context".
3443 * The constraints of both "bmap" and "context" are assumed
3444 * to have been sorted using isl_basic_map_sort_constraints.
3446 * Run through the inequality constraints of "bmap" and "context"
3448 * If a constraint of "bmap" involves variables not in "context",
3449 * then it cannot appear in "context".
3450 * If a matching constraint is found, it is removed from "bmap".
3452 static __isl_give isl_basic_map
*drop_inequalities(
3453 __isl_take isl_basic_map
*bmap
, __isl_keep isl_basic_map
*context
)
3456 unsigned total
, extra
;
3458 if (!bmap
|| !context
)
3459 return isl_basic_map_free(bmap
);
3461 total
= isl_basic_map_total_dim(context
);
3462 extra
= isl_basic_map_total_dim(bmap
) - total
;
3464 i1
= bmap
->n_ineq
- 1;
3465 i2
= context
->n_ineq
- 1;
3466 while (bmap
&& i1
>= 0 && i2
>= 0) {
3469 if (isl_seq_first_non_zero(bmap
->ineq
[i1
] + 1 + total
,
3474 cmp
= isl_basic_map_constraint_cmp(context
, bmap
->ineq
[i1
],
3484 if (isl_int_eq(bmap
->ineq
[i1
][0], context
->ineq
[i2
][0])) {
3485 bmap
= isl_basic_map_cow(bmap
);
3486 if (isl_basic_map_drop_inequality(bmap
, i1
) < 0)
3487 bmap
= isl_basic_map_free(bmap
);
3496 /* Drop all equalities from "bmap" that also appear in "context".
3497 * "context" is assumed to have only known local variables and
3498 * the initial local variables of "bmap" are assumed to be the same
3499 * as those of "context".
3501 * Run through the equality constraints of "bmap" and "context"
3503 * If a constraint of "bmap" involves variables not in "context",
3504 * then it cannot appear in "context".
3505 * If a matching constraint is found, it is removed from "bmap".
3507 static __isl_give isl_basic_map
*drop_equalities(
3508 __isl_take isl_basic_map
*bmap
, __isl_keep isl_basic_map
*context
)
3511 unsigned total
, extra
;
3513 if (!bmap
|| !context
)
3514 return isl_basic_map_free(bmap
);
3516 total
= isl_basic_map_total_dim(context
);
3517 extra
= isl_basic_map_total_dim(bmap
) - total
;
3519 i1
= bmap
->n_eq
- 1;
3520 i2
= context
->n_eq
- 1;
3522 while (bmap
&& i1
>= 0 && i2
>= 0) {
3525 if (isl_seq_first_non_zero(bmap
->eq
[i1
] + 1 + total
,
3528 last1
= isl_seq_last_non_zero(bmap
->eq
[i1
] + 1, total
);
3529 last2
= isl_seq_last_non_zero(context
->eq
[i2
] + 1, total
);
3530 if (last1
> last2
) {
3534 if (last1
< last2
) {
3538 if (isl_seq_eq(bmap
->eq
[i1
], context
->eq
[i2
], 1 + total
)) {
3539 bmap
= isl_basic_map_cow(bmap
);
3540 if (isl_basic_map_drop_equality(bmap
, i1
) < 0)
3541 bmap
= isl_basic_map_free(bmap
);
3550 /* Remove the constraints in "context" from "bmap".
3551 * "context" is assumed to have explicit representations
3552 * for all local variables.
3554 * First align the divs of "bmap" to those of "context" and
3555 * sort the constraints. Then drop all constraints from "bmap"
3556 * that appear in "context".
3558 __isl_give isl_basic_map
*isl_basic_map_plain_gist(
3559 __isl_take isl_basic_map
*bmap
, __isl_take isl_basic_map
*context
)
3561 isl_bool done
, known
;
3563 done
= isl_basic_map_plain_is_universe(context
);
3564 if (done
== isl_bool_false
)
3565 done
= isl_basic_map_plain_is_universe(bmap
);
3566 if (done
== isl_bool_false
)
3567 done
= isl_basic_map_plain_is_empty(context
);
3568 if (done
== isl_bool_false
)
3569 done
= isl_basic_map_plain_is_empty(bmap
);
3573 isl_basic_map_free(context
);
3576 known
= isl_basic_map_divs_known(context
);
3580 isl_die(isl_basic_map_get_ctx(bmap
), isl_error_invalid
,
3581 "context has unknown divs", goto error
);
3583 bmap
= isl_basic_map_align_divs(bmap
, context
);
3584 bmap
= isl_basic_map_gauss(bmap
, NULL
);
3585 bmap
= isl_basic_map_sort_constraints(bmap
);
3586 context
= isl_basic_map_sort_constraints(context
);
3588 bmap
= drop_inequalities(bmap
, context
);
3589 bmap
= drop_equalities(bmap
, context
);
3591 isl_basic_map_free(context
);
3592 bmap
= isl_basic_map_finalize(bmap
);
3595 isl_basic_map_free(bmap
);
3596 isl_basic_map_free(context
);
3600 /* Replace "map" by the disjunct at position "pos" and free "context".
3602 static __isl_give isl_map
*replace_by_disjunct(__isl_take isl_map
*map
,
3603 int pos
, __isl_take isl_basic_map
*context
)
3605 isl_basic_map
*bmap
;
3607 bmap
= isl_basic_map_copy(map
->p
[pos
]);
3609 isl_basic_map_free(context
);
3610 return isl_map_from_basic_map(bmap
);
3613 /* Remove the constraints in "context" from "map".
3614 * If any of the disjuncts in the result turns out to be the universe,
3615 * then return this universe.
3616 * "context" is assumed to have explicit representations
3617 * for all local variables.
3619 __isl_give isl_map
*isl_map_plain_gist_basic_map(__isl_take isl_map
*map
,
3620 __isl_take isl_basic_map
*context
)
3623 isl_bool univ
, known
;
3625 univ
= isl_basic_map_plain_is_universe(context
);
3629 isl_basic_map_free(context
);
3632 known
= isl_basic_map_divs_known(context
);
3636 isl_die(isl_map_get_ctx(map
), isl_error_invalid
,
3637 "context has unknown divs", goto error
);
3639 map
= isl_map_cow(map
);
3642 for (i
= 0; i
< map
->n
; ++i
) {
3643 map
->p
[i
] = isl_basic_map_plain_gist(map
->p
[i
],
3644 isl_basic_map_copy(context
));
3645 univ
= isl_basic_map_plain_is_universe(map
->p
[i
]);
3648 if (univ
&& map
->n
> 1)
3649 return replace_by_disjunct(map
, i
, context
);
3652 isl_basic_map_free(context
);
3653 ISL_F_CLR(map
, ISL_MAP_NORMALIZED
);
3655 ISL_F_CLR(map
, ISL_MAP_DISJOINT
);
3659 isl_basic_map_free(context
);
3663 /* Replace "map" by a universe map in the same space and free "drop".
3665 static __isl_give isl_map
*replace_by_universe(__isl_take isl_map
*map
,
3666 __isl_take isl_map
*drop
)
3670 res
= isl_map_universe(isl_map_get_space(map
));
3676 /* Return a map that has the same intersection with "context" as "map"
3677 * and that is as "simple" as possible.
3679 * If "map" is already the universe, then we cannot make it any simpler.
3680 * Similarly, if "context" is the universe, then we cannot exploit it
3682 * If "map" and "context" are identical to each other, then we can
3683 * return the corresponding universe.
3685 * If either "map" or "context" consists of multiple disjuncts,
3686 * then check if "context" happens to be a subset of "map",
3687 * in which case all constraints can be removed.
3688 * In case of multiple disjuncts, the standard procedure
3689 * may not be able to detect that all constraints can be removed.
3691 * If none of these cases apply, we have to work a bit harder.
3692 * During this computation, we make use of a single disjunct context,
3693 * so if the original context consists of more than one disjunct
3694 * then we need to approximate the context by a single disjunct set.
3695 * Simply taking the simple hull may drop constraints that are
3696 * only implicitly available in each disjunct. We therefore also
3697 * look for constraints among those defining "map" that are valid
3698 * for the context. These can then be used to simplify away
3699 * the corresponding constraints in "map".
3701 static __isl_give isl_map
*map_gist(__isl_take isl_map
*map
,
3702 __isl_take isl_map
*context
)
3706 int single_disjunct_map
, single_disjunct_context
;
3708 isl_basic_map
*hull
;
3710 is_universe
= isl_map_plain_is_universe(map
);
3711 if (is_universe
>= 0 && !is_universe
)
3712 is_universe
= isl_map_plain_is_universe(context
);
3713 if (is_universe
< 0)
3716 isl_map_free(context
);
3720 equal
= isl_map_plain_is_equal(map
, context
);
3724 return replace_by_universe(map
, context
);
3726 single_disjunct_map
= isl_map_n_basic_map(map
) == 1;
3727 single_disjunct_context
= isl_map_n_basic_map(context
) == 1;
3728 if (!single_disjunct_map
|| !single_disjunct_context
) {
3729 subset
= isl_map_is_subset(context
, map
);
3733 return replace_by_universe(map
, context
);
3736 context
= isl_map_compute_divs(context
);
3739 if (single_disjunct_context
) {
3740 hull
= isl_map_simple_hull(context
);
3745 ctx
= isl_map_get_ctx(map
);
3746 list
= isl_map_list_alloc(ctx
, 2);
3747 list
= isl_map_list_add(list
, isl_map_copy(context
));
3748 list
= isl_map_list_add(list
, isl_map_copy(map
));
3749 hull
= isl_map_unshifted_simple_hull_from_map_list(context
,
3752 return isl_map_gist_basic_map(map
, hull
);
3755 isl_map_free(context
);
3759 __isl_give isl_map
*isl_map_gist(__isl_take isl_map
*map
,
3760 __isl_take isl_map
*context
)
3762 return isl_map_align_params_map_map_and(map
, context
, &map_gist
);
3765 struct isl_basic_set
*isl_basic_set_gist(struct isl_basic_set
*bset
,
3766 struct isl_basic_set
*context
)
3768 return bset_from_bmap(isl_basic_map_gist(bset_to_bmap(bset
),
3769 bset_to_bmap(context
)));
3772 __isl_give isl_set
*isl_set_gist_basic_set(__isl_take isl_set
*set
,
3773 __isl_take isl_basic_set
*context
)
3775 return set_from_map(isl_map_gist_basic_map(set_to_map(set
),
3776 bset_to_bmap(context
)));
3779 __isl_give isl_set
*isl_set_gist_params_basic_set(__isl_take isl_set
*set
,
3780 __isl_take isl_basic_set
*context
)
3782 isl_space
*space
= isl_set_get_space(set
);
3783 isl_basic_set
*dom_context
= isl_basic_set_universe(space
);
3784 dom_context
= isl_basic_set_intersect_params(dom_context
, context
);
3785 return isl_set_gist_basic_set(set
, dom_context
);
3788 __isl_give isl_set
*isl_set_gist(__isl_take isl_set
*set
,
3789 __isl_take isl_set
*context
)
3791 return set_from_map(isl_map_gist(set_to_map(set
), set_to_map(context
)));
3794 /* Compute the gist of "bmap" with respect to the constraints "context"
3797 __isl_give isl_basic_map
*isl_basic_map_gist_domain(
3798 __isl_take isl_basic_map
*bmap
, __isl_take isl_basic_set
*context
)
3800 isl_space
*space
= isl_basic_map_get_space(bmap
);
3801 isl_basic_map
*bmap_context
= isl_basic_map_universe(space
);
3803 bmap_context
= isl_basic_map_intersect_domain(bmap_context
, context
);
3804 return isl_basic_map_gist(bmap
, bmap_context
);
3807 __isl_give isl_map
*isl_map_gist_domain(__isl_take isl_map
*map
,
3808 __isl_take isl_set
*context
)
3810 isl_map
*map_context
= isl_map_universe(isl_map_get_space(map
));
3811 map_context
= isl_map_intersect_domain(map_context
, context
);
3812 return isl_map_gist(map
, map_context
);
3815 __isl_give isl_map
*isl_map_gist_range(__isl_take isl_map
*map
,
3816 __isl_take isl_set
*context
)
3818 isl_map
*map_context
= isl_map_universe(isl_map_get_space(map
));
3819 map_context
= isl_map_intersect_range(map_context
, context
);
3820 return isl_map_gist(map
, map_context
);
3823 __isl_give isl_map
*isl_map_gist_params(__isl_take isl_map
*map
,
3824 __isl_take isl_set
*context
)
3826 isl_map
*map_context
= isl_map_universe(isl_map_get_space(map
));
3827 map_context
= isl_map_intersect_params(map_context
, context
);
3828 return isl_map_gist(map
, map_context
);
3831 __isl_give isl_set
*isl_set_gist_params(__isl_take isl_set
*set
,
3832 __isl_take isl_set
*context
)
3834 return isl_map_gist_params(set
, context
);
3837 /* Quick check to see if two basic maps are disjoint.
3838 * In particular, we reduce the equalities and inequalities of
3839 * one basic map in the context of the equalities of the other
3840 * basic map and check if we get a contradiction.
3842 isl_bool
isl_basic_map_plain_is_disjoint(__isl_keep isl_basic_map
*bmap1
,
3843 __isl_keep isl_basic_map
*bmap2
)
3845 struct isl_vec
*v
= NULL
;
3850 if (!bmap1
|| !bmap2
)
3851 return isl_bool_error
;
3852 isl_assert(bmap1
->ctx
, isl_space_is_equal(bmap1
->dim
, bmap2
->dim
),
3853 return isl_bool_error
);
3854 if (bmap1
->n_div
|| bmap2
->n_div
)
3855 return isl_bool_false
;
3856 if (!bmap1
->n_eq
&& !bmap2
->n_eq
)
3857 return isl_bool_false
;
3859 total
= isl_space_dim(bmap1
->dim
, isl_dim_all
);
3861 return isl_bool_false
;
3862 v
= isl_vec_alloc(bmap1
->ctx
, 1 + total
);
3865 elim
= isl_alloc_array(bmap1
->ctx
, int, total
);
3868 compute_elimination_index(bmap1
, elim
);
3869 for (i
= 0; i
< bmap2
->n_eq
; ++i
) {
3871 reduced
= reduced_using_equalities(v
->block
.data
, bmap2
->eq
[i
],
3873 if (reduced
&& !isl_int_is_zero(v
->block
.data
[0]) &&
3874 isl_seq_first_non_zero(v
->block
.data
+ 1, total
) == -1)
3877 for (i
= 0; i
< bmap2
->n_ineq
; ++i
) {
3879 reduced
= reduced_using_equalities(v
->block
.data
,
3880 bmap2
->ineq
[i
], bmap1
, elim
);
3881 if (reduced
&& isl_int_is_neg(v
->block
.data
[0]) &&
3882 isl_seq_first_non_zero(v
->block
.data
+ 1, total
) == -1)
3885 compute_elimination_index(bmap2
, elim
);
3886 for (i
= 0; i
< bmap1
->n_ineq
; ++i
) {
3888 reduced
= reduced_using_equalities(v
->block
.data
,
3889 bmap1
->ineq
[i
], bmap2
, elim
);
3890 if (reduced
&& isl_int_is_neg(v
->block
.data
[0]) &&
3891 isl_seq_first_non_zero(v
->block
.data
+ 1, total
) == -1)
3896 return isl_bool_false
;
3900 return isl_bool_true
;
3904 return isl_bool_error
;
3907 int isl_basic_set_plain_is_disjoint(__isl_keep isl_basic_set
*bset1
,
3908 __isl_keep isl_basic_set
*bset2
)
3910 return isl_basic_map_plain_is_disjoint(bset_to_bmap(bset1
),
3911 bset_to_bmap(bset2
));
3914 /* Does "test" hold for all pairs of basic maps in "map1" and "map2"?
3916 static isl_bool
all_pairs(__isl_keep isl_map
*map1
, __isl_keep isl_map
*map2
,
3917 isl_bool (*test
)(__isl_keep isl_basic_map
*bmap1
,
3918 __isl_keep isl_basic_map
*bmap2
))
3923 return isl_bool_error
;
3925 for (i
= 0; i
< map1
->n
; ++i
) {
3926 for (j
= 0; j
< map2
->n
; ++j
) {
3927 isl_bool d
= test(map1
->p
[i
], map2
->p
[j
]);
3928 if (d
!= isl_bool_true
)
3933 return isl_bool_true
;
3936 /* Are "map1" and "map2" obviously disjoint, based on information
3937 * that can be derived without looking at the individual basic maps?
3939 * In particular, if one of them is empty or if they live in different spaces
3940 * (ignoring parameters), then they are clearly disjoint.
3942 static isl_bool
isl_map_plain_is_disjoint_global(__isl_keep isl_map
*map1
,
3943 __isl_keep isl_map
*map2
)
3949 return isl_bool_error
;
3951 disjoint
= isl_map_plain_is_empty(map1
);
3952 if (disjoint
< 0 || disjoint
)
3955 disjoint
= isl_map_plain_is_empty(map2
);
3956 if (disjoint
< 0 || disjoint
)
3959 match
= isl_space_tuple_is_equal(map1
->dim
, isl_dim_in
,
3960 map2
->dim
, isl_dim_in
);
3961 if (match
< 0 || !match
)
3962 return match
< 0 ? isl_bool_error
: isl_bool_true
;
3964 match
= isl_space_tuple_is_equal(map1
->dim
, isl_dim_out
,
3965 map2
->dim
, isl_dim_out
);
3966 if (match
< 0 || !match
)
3967 return match
< 0 ? isl_bool_error
: isl_bool_true
;
3969 return isl_bool_false
;
3972 /* Are "map1" and "map2" obviously disjoint?
3974 * If one of them is empty or if they live in different spaces (ignoring
3975 * parameters), then they are clearly disjoint.
3976 * This is checked by isl_map_plain_is_disjoint_global.
3978 * If they have different parameters, then we skip any further tests.
3980 * If they are obviously equal, but not obviously empty, then we will
3981 * not be able to detect if they are disjoint.
3983 * Otherwise we check if each basic map in "map1" is obviously disjoint
3984 * from each basic map in "map2".
3986 isl_bool
isl_map_plain_is_disjoint(__isl_keep isl_map
*map1
,
3987 __isl_keep isl_map
*map2
)
3993 disjoint
= isl_map_plain_is_disjoint_global(map1
, map2
);
3994 if (disjoint
< 0 || disjoint
)
3997 match
= isl_space_match(map1
->dim
, isl_dim_param
,
3998 map2
->dim
, isl_dim_param
);
3999 if (match
< 0 || !match
)
4000 return match
< 0 ? isl_bool_error
: isl_bool_false
;
4002 intersect
= isl_map_plain_is_equal(map1
, map2
);
4003 if (intersect
< 0 || intersect
)
4004 return intersect
< 0 ? isl_bool_error
: isl_bool_false
;
4006 return all_pairs(map1
, map2
, &isl_basic_map_plain_is_disjoint
);
4009 /* Are "map1" and "map2" disjoint?
4011 * They are disjoint if they are "obviously disjoint" or if one of them
4012 * is empty. Otherwise, they are not disjoint if one of them is universal.
4013 * If the two inputs are (obviously) equal and not empty, then they are
4015 * If none of these cases apply, then check if all pairs of basic maps
4018 isl_bool
isl_map_is_disjoint(__isl_keep isl_map
*map1
, __isl_keep isl_map
*map2
)
4023 disjoint
= isl_map_plain_is_disjoint_global(map1
, map2
);
4024 if (disjoint
< 0 || disjoint
)
4027 disjoint
= isl_map_is_empty(map1
);
4028 if (disjoint
< 0 || disjoint
)
4031 disjoint
= isl_map_is_empty(map2
);
4032 if (disjoint
< 0 || disjoint
)
4035 intersect
= isl_map_plain_is_universe(map1
);
4036 if (intersect
< 0 || intersect
)
4037 return intersect
< 0 ? isl_bool_error
: isl_bool_false
;
4039 intersect
= isl_map_plain_is_universe(map2
);
4040 if (intersect
< 0 || intersect
)
4041 return intersect
< 0 ? isl_bool_error
: isl_bool_false
;
4043 intersect
= isl_map_plain_is_equal(map1
, map2
);
4044 if (intersect
< 0 || intersect
)
4045 return isl_bool_not(intersect
);
4047 return all_pairs(map1
, map2
, &isl_basic_map_is_disjoint
);
4050 /* Are "bmap1" and "bmap2" disjoint?
4052 * They are disjoint if they are "obviously disjoint" or if one of them
4053 * is empty. Otherwise, they are not disjoint if one of them is universal.
4054 * If none of these cases apply, we compute the intersection and see if
4055 * the result is empty.
4057 isl_bool
isl_basic_map_is_disjoint(__isl_keep isl_basic_map
*bmap1
,
4058 __isl_keep isl_basic_map
*bmap2
)
4062 isl_basic_map
*test
;
4064 disjoint
= isl_basic_map_plain_is_disjoint(bmap1
, bmap2
);
4065 if (disjoint
< 0 || disjoint
)
4068 disjoint
= isl_basic_map_is_empty(bmap1
);
4069 if (disjoint
< 0 || disjoint
)
4072 disjoint
= isl_basic_map_is_empty(bmap2
);
4073 if (disjoint
< 0 || disjoint
)
4076 intersect
= isl_basic_map_plain_is_universe(bmap1
);
4077 if (intersect
< 0 || intersect
)
4078 return intersect
< 0 ? isl_bool_error
: isl_bool_false
;
4080 intersect
= isl_basic_map_plain_is_universe(bmap2
);
4081 if (intersect
< 0 || intersect
)
4082 return intersect
< 0 ? isl_bool_error
: isl_bool_false
;
4084 test
= isl_basic_map_intersect(isl_basic_map_copy(bmap1
),
4085 isl_basic_map_copy(bmap2
));
4086 disjoint
= isl_basic_map_is_empty(test
);
4087 isl_basic_map_free(test
);
4092 /* Are "bset1" and "bset2" disjoint?
4094 isl_bool
isl_basic_set_is_disjoint(__isl_keep isl_basic_set
*bset1
,
4095 __isl_keep isl_basic_set
*bset2
)
4097 return isl_basic_map_is_disjoint(bset1
, bset2
);
4100 isl_bool
isl_set_plain_is_disjoint(__isl_keep isl_set
*set1
,
4101 __isl_keep isl_set
*set2
)
4103 return isl_map_plain_is_disjoint(set_to_map(set1
), set_to_map(set2
));
4106 /* Are "set1" and "set2" disjoint?
4108 isl_bool
isl_set_is_disjoint(__isl_keep isl_set
*set1
, __isl_keep isl_set
*set2
)
4110 return isl_map_is_disjoint(set1
, set2
);
4113 /* Is "v" equal to 0, 1 or -1?
4115 static int is_zero_or_one(isl_int v
)
4117 return isl_int_is_zero(v
) || isl_int_is_one(v
) || isl_int_is_negone(v
);
4120 /* Check if we can combine a given div with lower bound l and upper
4121 * bound u with some other div and if so return that other div.
4122 * Otherwise return -1.
4124 * We first check that
4125 * - the bounds are opposites of each other (except for the constant
4127 * - the bounds do not reference any other div
4128 * - no div is defined in terms of this div
4130 * Let m be the size of the range allowed on the div by the bounds.
4131 * That is, the bounds are of the form
4133 * e <= a <= e + m - 1
4135 * with e some expression in the other variables.
4136 * We look for another div b such that no third div is defined in terms
4137 * of this second div b and such that in any constraint that contains
4138 * a (except for the given lower and upper bound), also contains b
4139 * with a coefficient that is m times that of b.
4140 * That is, all constraints (execpt for the lower and upper bound)
4143 * e + f (a + m b) >= 0
4145 * Furthermore, in the constraints that only contain b, the coefficient
4146 * of b should be equal to 1 or -1.
4147 * If so, we return b so that "a + m b" can be replaced by
4148 * a single div "c = a + m b".
4150 static int div_find_coalesce(struct isl_basic_map
*bmap
, int *pairs
,
4151 unsigned div
, unsigned l
, unsigned u
)
4157 if (bmap
->n_div
<= 1)
4159 dim
= isl_space_dim(bmap
->dim
, isl_dim_all
);
4160 if (isl_seq_first_non_zero(bmap
->ineq
[l
] + 1 + dim
, div
) != -1)
4162 if (isl_seq_first_non_zero(bmap
->ineq
[l
] + 1 + dim
+ div
+ 1,
4163 bmap
->n_div
- div
- 1) != -1)
4165 if (!isl_seq_is_neg(bmap
->ineq
[l
] + 1, bmap
->ineq
[u
] + 1,
4169 for (i
= 0; i
< bmap
->n_div
; ++i
) {
4170 if (isl_int_is_zero(bmap
->div
[i
][0]))
4172 if (!isl_int_is_zero(bmap
->div
[i
][1 + 1 + dim
+ div
]))
4176 isl_int_add(bmap
->ineq
[l
][0], bmap
->ineq
[l
][0], bmap
->ineq
[u
][0]);
4177 if (isl_int_is_neg(bmap
->ineq
[l
][0])) {
4178 isl_int_sub(bmap
->ineq
[l
][0],
4179 bmap
->ineq
[l
][0], bmap
->ineq
[u
][0]);
4180 bmap
= isl_basic_map_copy(bmap
);
4181 bmap
= isl_basic_map_set_to_empty(bmap
);
4182 isl_basic_map_free(bmap
);
4185 isl_int_add_ui(bmap
->ineq
[l
][0], bmap
->ineq
[l
][0], 1);
4186 for (i
= 0; i
< bmap
->n_div
; ++i
) {
4191 for (j
= 0; j
< bmap
->n_div
; ++j
) {
4192 if (isl_int_is_zero(bmap
->div
[j
][0]))
4194 if (!isl_int_is_zero(bmap
->div
[j
][1 + 1 + dim
+ i
]))
4197 if (j
< bmap
->n_div
)
4199 for (j
= 0; j
< bmap
->n_ineq
; ++j
) {
4201 if (j
== l
|| j
== u
)
4203 if (isl_int_is_zero(bmap
->ineq
[j
][1 + dim
+ div
])) {
4204 if (is_zero_or_one(bmap
->ineq
[j
][1 + dim
+ i
]))
4208 if (isl_int_is_zero(bmap
->ineq
[j
][1 + dim
+ i
]))
4210 isl_int_mul(bmap
->ineq
[j
][1 + dim
+ div
],
4211 bmap
->ineq
[j
][1 + dim
+ div
],
4213 valid
= isl_int_eq(bmap
->ineq
[j
][1 + dim
+ div
],
4214 bmap
->ineq
[j
][1 + dim
+ i
]);
4215 isl_int_divexact(bmap
->ineq
[j
][1 + dim
+ div
],
4216 bmap
->ineq
[j
][1 + dim
+ div
],
4221 if (j
< bmap
->n_ineq
)
4226 isl_int_sub_ui(bmap
->ineq
[l
][0], bmap
->ineq
[l
][0], 1);
4227 isl_int_sub(bmap
->ineq
[l
][0], bmap
->ineq
[l
][0], bmap
->ineq
[u
][0]);
4231 /* Internal data structure used during the construction and/or evaluation of
4232 * an inequality that ensures that a pair of bounds always allows
4233 * for an integer value.
4235 * "tab" is the tableau in which the inequality is evaluated. It may
4236 * be NULL until it is actually needed.
4237 * "v" contains the inequality coefficients.
4238 * "g", "fl" and "fu" are temporary scalars used during the construction and
4241 struct test_ineq_data
{
4242 struct isl_tab
*tab
;
4249 /* Free all the memory allocated by the fields of "data".
4251 static void test_ineq_data_clear(struct test_ineq_data
*data
)
4253 isl_tab_free(data
->tab
);
4254 isl_vec_free(data
->v
);
4255 isl_int_clear(data
->g
);
4256 isl_int_clear(data
->fl
);
4257 isl_int_clear(data
->fu
);
4260 /* Is the inequality stored in data->v satisfied by "bmap"?
4261 * That is, does it only attain non-negative values?
4262 * data->tab is a tableau corresponding to "bmap".
4264 static isl_bool
test_ineq_is_satisfied(__isl_keep isl_basic_map
*bmap
,
4265 struct test_ineq_data
*data
)
4268 enum isl_lp_result res
;
4270 ctx
= isl_basic_map_get_ctx(bmap
);
4272 data
->tab
= isl_tab_from_basic_map(bmap
, 0);
4273 res
= isl_tab_min(data
->tab
, data
->v
->el
, ctx
->one
, &data
->g
, NULL
, 0);
4274 if (res
== isl_lp_error
)
4275 return isl_bool_error
;
4276 return res
== isl_lp_ok
&& isl_int_is_nonneg(data
->g
);
4279 /* Given a lower and an upper bound on div i, do they always allow
4280 * for an integer value of the given div?
4281 * Determine this property by constructing an inequality
4282 * such that the property is guaranteed when the inequality is nonnegative.
4283 * The lower bound is inequality l, while the upper bound is inequality u.
4284 * The constructed inequality is stored in data->v.
4286 * Let the upper bound be
4290 * and the lower bound
4294 * Let n_u = f_u g and n_l = f_l g, with g = gcd(n_u, n_l).
4297 * - f_u e_l <= f_u f_l g a <= f_l e_u
4299 * Since all variables are integer valued, this is equivalent to
4301 * - f_u e_l - (f_u - 1) <= f_u f_l g a <= f_l e_u + (f_l - 1)
4303 * If this interval is at least f_u f_l g, then it contains at least
4304 * one integer value for a.
4305 * That is, the test constraint is
4307 * f_l e_u + f_u e_l + f_l - 1 + f_u - 1 + 1 >= f_u f_l g
4311 * f_l e_u + f_u e_l + f_l - 1 + f_u - 1 + 1 - f_u f_l g >= 0
4313 * If the coefficients of f_l e_u + f_u e_l have a common divisor g',
4314 * then the constraint can be scaled down by a factor g',
4315 * with the constant term replaced by
4316 * floor((f_l e_{u,0} + f_u e_{l,0} + f_l - 1 + f_u - 1 + 1 - f_u f_l g)/g').
4317 * Note that the result of applying Fourier-Motzkin to this pair
4320 * f_l e_u + f_u e_l >= 0
4322 * If the constant term of the scaled down version of this constraint,
4323 * i.e., floor((f_l e_{u,0} + f_u e_{l,0})/g') is equal to the constant
4324 * term of the scaled down test constraint, then the test constraint
4325 * is known to hold and no explicit evaluation is required.
4326 * This is essentially the Omega test.
4328 * If the test constraint consists of only a constant term, then
4329 * it is sufficient to look at the sign of this constant term.
4331 static isl_bool
int_between_bounds(__isl_keep isl_basic_map
*bmap
, int i
,
4332 int l
, int u
, struct test_ineq_data
*data
)
4334 unsigned offset
, n_div
;
4335 offset
= isl_basic_map_offset(bmap
, isl_dim_div
);
4336 n_div
= isl_basic_map_dim(bmap
, isl_dim_div
);
4338 isl_int_gcd(data
->g
,
4339 bmap
->ineq
[l
][offset
+ i
], bmap
->ineq
[u
][offset
+ i
]);
4340 isl_int_divexact(data
->fl
, bmap
->ineq
[l
][offset
+ i
], data
->g
);
4341 isl_int_divexact(data
->fu
, bmap
->ineq
[u
][offset
+ i
], data
->g
);
4342 isl_int_neg(data
->fu
, data
->fu
);
4343 isl_seq_combine(data
->v
->el
, data
->fl
, bmap
->ineq
[u
],
4344 data
->fu
, bmap
->ineq
[l
], offset
+ n_div
);
4345 isl_int_mul(data
->g
, data
->g
, data
->fl
);
4346 isl_int_mul(data
->g
, data
->g
, data
->fu
);
4347 isl_int_sub(data
->g
, data
->g
, data
->fl
);
4348 isl_int_sub(data
->g
, data
->g
, data
->fu
);
4349 isl_int_add_ui(data
->g
, data
->g
, 1);
4350 isl_int_sub(data
->fl
, data
->v
->el
[0], data
->g
);
4352 isl_seq_gcd(data
->v
->el
+ 1, offset
- 1 + n_div
, &data
->g
);
4353 if (isl_int_is_zero(data
->g
))
4354 return isl_int_is_nonneg(data
->fl
);
4355 if (isl_int_is_one(data
->g
)) {
4356 isl_int_set(data
->v
->el
[0], data
->fl
);
4357 return test_ineq_is_satisfied(bmap
, data
);
4359 isl_int_fdiv_q(data
->fl
, data
->fl
, data
->g
);
4360 isl_int_fdiv_q(data
->v
->el
[0], data
->v
->el
[0], data
->g
);
4361 if (isl_int_eq(data
->fl
, data
->v
->el
[0]))
4362 return isl_bool_true
;
4363 isl_int_set(data
->v
->el
[0], data
->fl
);
4364 isl_seq_scale_down(data
->v
->el
+ 1, data
->v
->el
+ 1, data
->g
,
4365 offset
- 1 + n_div
);
4367 return test_ineq_is_satisfied(bmap
, data
);
4370 /* Remove more kinds of divs that are not strictly needed.
4371 * In particular, if all pairs of lower and upper bounds on a div
4372 * are such that they allow at least one integer value of the div,
4373 * then we can eliminate the div using Fourier-Motzkin without
4374 * introducing any spurious solutions.
4376 * If at least one of the two constraints has a unit coefficient for the div,
4377 * then the presence of such a value is guaranteed so there is no need to check.
4378 * In particular, the value attained by the bound with unit coefficient
4379 * can serve as this intermediate value.
4381 static struct isl_basic_map
*drop_more_redundant_divs(
4382 struct isl_basic_map
*bmap
, int *pairs
, int n
)
4385 struct test_ineq_data data
= { NULL
, NULL
};
4386 unsigned off
, n_div
;
4389 isl_int_init(data
.g
);
4390 isl_int_init(data
.fl
);
4391 isl_int_init(data
.fu
);
4396 ctx
= isl_basic_map_get_ctx(bmap
);
4397 off
= isl_basic_map_offset(bmap
, isl_dim_div
);
4398 n_div
= isl_basic_map_dim(bmap
, isl_dim_div
);
4399 data
.v
= isl_vec_alloc(ctx
, off
+ n_div
);
4408 for (i
= 0; i
< n_div
; ++i
) {
4411 if (best
>= 0 && pairs
[best
] <= pairs
[i
])
4417 for (l
= 0; l
< bmap
->n_ineq
; ++l
) {
4418 if (!isl_int_is_pos(bmap
->ineq
[l
][off
+ i
]))
4420 if (isl_int_is_one(bmap
->ineq
[l
][off
+ i
]))
4422 for (u
= 0; u
< bmap
->n_ineq
; ++u
) {
4423 if (!isl_int_is_neg(bmap
->ineq
[u
][off
+ i
]))
4425 if (isl_int_is_negone(bmap
->ineq
[u
][off
+ i
]))
4427 has_int
= int_between_bounds(bmap
, i
, l
, u
,
4431 if (data
.tab
&& data
.tab
->empty
)
4436 if (u
< bmap
->n_ineq
)
4439 if (data
.tab
&& data
.tab
->empty
) {
4440 bmap
= isl_basic_map_set_to_empty(bmap
);
4443 if (l
== bmap
->n_ineq
) {
4451 test_ineq_data_clear(&data
);
4458 bmap
= isl_basic_map_remove_dims(bmap
, isl_dim_div
, remove
, 1);
4459 return isl_basic_map_drop_redundant_divs(bmap
);
4462 isl_basic_map_free(bmap
);
4463 test_ineq_data_clear(&data
);
4467 /* Given a pair of divs div1 and div2 such that, except for the lower bound l
4468 * and the upper bound u, div1 always occurs together with div2 in the form
4469 * (div1 + m div2), where m is the constant range on the variable div1
4470 * allowed by l and u, replace the pair div1 and div2 by a single
4471 * div that is equal to div1 + m div2.
4473 * The new div will appear in the location that contains div2.
4474 * We need to modify all constraints that contain
4475 * div2 = (div - div1) / m
4476 * The coefficient of div2 is known to be equal to 1 or -1.
4477 * (If a constraint does not contain div2, it will also not contain div1.)
4478 * If the constraint also contains div1, then we know they appear
4479 * as f (div1 + m div2) and we can simply replace (div1 + m div2) by div,
4480 * i.e., the coefficient of div is f.
4482 * Otherwise, we first need to introduce div1 into the constraint.
4491 * A lower bound on div2
4495 * can be replaced by
4497 * m div2 + div1 + m t + f >= 0
4503 * can be replaced by
4505 * -(m div2 + div1) + m t + f' >= 0
4507 * These constraint are those that we would obtain from eliminating
4508 * div1 using Fourier-Motzkin.
4510 * After all constraints have been modified, we drop the lower and upper
4511 * bound and then drop div1.
4513 static struct isl_basic_map
*coalesce_divs(struct isl_basic_map
*bmap
,
4514 unsigned div1
, unsigned div2
, unsigned l
, unsigned u
)
4518 unsigned dim
, total
;
4521 ctx
= isl_basic_map_get_ctx(bmap
);
4523 dim
= isl_space_dim(bmap
->dim
, isl_dim_all
);
4524 total
= 1 + dim
+ bmap
->n_div
;
4527 isl_int_add(m
, bmap
->ineq
[l
][0], bmap
->ineq
[u
][0]);
4528 isl_int_add_ui(m
, m
, 1);
4530 for (i
= 0; i
< bmap
->n_ineq
; ++i
) {
4531 if (i
== l
|| i
== u
)
4533 if (isl_int_is_zero(bmap
->ineq
[i
][1 + dim
+ div2
]))
4535 if (isl_int_is_zero(bmap
->ineq
[i
][1 + dim
+ div1
])) {
4536 if (isl_int_is_pos(bmap
->ineq
[i
][1 + dim
+ div2
]))
4537 isl_seq_combine(bmap
->ineq
[i
], m
, bmap
->ineq
[i
],
4538 ctx
->one
, bmap
->ineq
[l
], total
);
4540 isl_seq_combine(bmap
->ineq
[i
], m
, bmap
->ineq
[i
],
4541 ctx
->one
, bmap
->ineq
[u
], total
);
4543 isl_int_set(bmap
->ineq
[i
][1 + dim
+ div2
],
4544 bmap
->ineq
[i
][1 + dim
+ div1
]);
4545 isl_int_set_si(bmap
->ineq
[i
][1 + dim
+ div1
], 0);
4550 isl_basic_map_drop_inequality(bmap
, l
);
4551 isl_basic_map_drop_inequality(bmap
, u
);
4553 isl_basic_map_drop_inequality(bmap
, u
);
4554 isl_basic_map_drop_inequality(bmap
, l
);
4556 bmap
= isl_basic_map_drop_div(bmap
, div1
);
4560 /* First check if we can coalesce any pair of divs and
4561 * then continue with dropping more redundant divs.
4563 * We loop over all pairs of lower and upper bounds on a div
4564 * with coefficient 1 and -1, respectively, check if there
4565 * is any other div "c" with which we can coalesce the div
4566 * and if so, perform the coalescing.
4568 static struct isl_basic_map
*coalesce_or_drop_more_redundant_divs(
4569 struct isl_basic_map
*bmap
, int *pairs
, int n
)
4574 dim
= isl_space_dim(bmap
->dim
, isl_dim_all
);
4576 for (i
= 0; i
< bmap
->n_div
; ++i
) {
4579 for (l
= 0; l
< bmap
->n_ineq
; ++l
) {
4580 if (!isl_int_is_one(bmap
->ineq
[l
][1 + dim
+ i
]))
4582 for (u
= 0; u
< bmap
->n_ineq
; ++u
) {
4585 if (!isl_int_is_negone(bmap
->ineq
[u
][1+dim
+i
]))
4587 c
= div_find_coalesce(bmap
, pairs
, i
, l
, u
);
4591 bmap
= coalesce_divs(bmap
, i
, c
, l
, u
);
4592 return isl_basic_map_drop_redundant_divs(bmap
);
4597 if (ISL_F_ISSET(bmap
, ISL_BASIC_MAP_EMPTY
))
4600 return drop_more_redundant_divs(bmap
, pairs
, n
);
4603 /* Are the "n" coefficients starting at "first" of inequality constraints
4604 * "i" and "j" of "bmap" equal to each other?
4606 static int is_parallel_part(__isl_keep isl_basic_map
*bmap
, int i
, int j
,
4609 return isl_seq_eq(bmap
->ineq
[i
] + first
, bmap
->ineq
[j
] + first
, n
);
4612 /* Are the "n" coefficients starting at "first" of inequality constraints
4613 * "i" and "j" of "bmap" opposite to each other?
4615 static int is_opposite_part(__isl_keep isl_basic_map
*bmap
, int i
, int j
,
4618 return isl_seq_is_neg(bmap
->ineq
[i
] + first
, bmap
->ineq
[j
] + first
, n
);
4621 /* Are inequality constraints "i" and "j" of "bmap" opposite to each other,
4622 * apart from the constant term?
4624 static int is_opposite(__isl_keep isl_basic_map
*bmap
, int i
, int j
)
4628 total
= isl_basic_map_dim(bmap
, isl_dim_all
);
4629 return is_opposite_part(bmap
, i
, j
, 1, total
);
4632 /* Are inequality constraints "i" and "j" of "bmap" equal to each other,
4633 * apart from the constant term and the coefficient at position "pos"?
4635 static int is_parallel_except(__isl_keep isl_basic_map
*bmap
, int i
, int j
,
4640 total
= isl_basic_map_dim(bmap
, isl_dim_all
);
4641 return is_parallel_part(bmap
, i
, j
, 1, pos
- 1) &&
4642 is_parallel_part(bmap
, i
, j
, pos
+ 1, total
- pos
);
4645 /* Are inequality constraints "i" and "j" of "bmap" opposite to each other,
4646 * apart from the constant term and the coefficient at position "pos"?
4648 static int is_opposite_except(__isl_keep isl_basic_map
*bmap
, int i
, int j
,
4653 total
= isl_basic_map_dim(bmap
, isl_dim_all
);
4654 return is_opposite_part(bmap
, i
, j
, 1, pos
- 1) &&
4655 is_opposite_part(bmap
, i
, j
, pos
+ 1, total
- pos
);
4658 /* Restart isl_basic_map_drop_redundant_divs after "bmap" has
4659 * been modified, simplying it if "simplify" is set.
4660 * Free the temporary data structure "pairs" that was associated
4661 * to the old version of "bmap".
4663 static __isl_give isl_basic_map
*drop_redundant_divs_again(
4664 __isl_take isl_basic_map
*bmap
, __isl_take
int *pairs
, int simplify
)
4667 bmap
= isl_basic_map_simplify(bmap
);
4669 return isl_basic_map_drop_redundant_divs(bmap
);
4672 /* Is "div" the single unknown existentially quantified variable
4673 * in inequality constraint "ineq" of "bmap"?
4674 * "div" is known to have a non-zero coefficient in "ineq".
4676 static int single_unknown(__isl_keep isl_basic_map
*bmap
, int ineq
, int div
)
4679 unsigned n_div
, o_div
;
4681 if (isl_basic_map_div_is_known(bmap
, div
))
4683 n_div
= isl_basic_map_dim(bmap
, isl_dim_div
);
4686 o_div
= isl_basic_map_offset(bmap
, isl_dim_div
);
4687 for (i
= 0; i
< n_div
; ++i
) {
4690 if (isl_int_is_zero(bmap
->ineq
[ineq
][o_div
+ i
]))
4692 if (!isl_basic_map_div_is_known(bmap
, i
))
4699 /* Does integer division "div" have coefficient 1 in inequality constraint
4702 static int has_coef_one(__isl_keep isl_basic_map
*bmap
, int div
, int ineq
)
4706 o_div
= isl_basic_map_offset(bmap
, isl_dim_div
);
4707 if (isl_int_is_one(bmap
->ineq
[ineq
][o_div
+ div
]))
4713 /* Turn inequality constraint "ineq" of "bmap" into an equality and
4714 * then try and drop redundant divs again,
4715 * freeing the temporary data structure "pairs" that was associated
4716 * to the old version of "bmap".
4718 static __isl_give isl_basic_map
*set_eq_and_try_again(
4719 __isl_take isl_basic_map
*bmap
, int ineq
, __isl_take
int *pairs
)
4721 bmap
= isl_basic_map_cow(bmap
);
4722 isl_basic_map_inequality_to_equality(bmap
, ineq
);
4723 return drop_redundant_divs_again(bmap
, pairs
, 1);
4726 /* Drop the integer division at position "div", along with the two
4727 * inequality constraints "ineq1" and "ineq2" in which it appears
4728 * from "bmap" and then try and drop redundant divs again,
4729 * freeing the temporary data structure "pairs" that was associated
4730 * to the old version of "bmap".
4732 static __isl_give isl_basic_map
*drop_div_and_try_again(
4733 __isl_take isl_basic_map
*bmap
, int div
, int ineq1
, int ineq2
,
4734 __isl_take
int *pairs
)
4736 if (ineq1
> ineq2
) {
4737 isl_basic_map_drop_inequality(bmap
, ineq1
);
4738 isl_basic_map_drop_inequality(bmap
, ineq2
);
4740 isl_basic_map_drop_inequality(bmap
, ineq2
);
4741 isl_basic_map_drop_inequality(bmap
, ineq1
);
4743 bmap
= isl_basic_map_drop_div(bmap
, div
);
4744 return drop_redundant_divs_again(bmap
, pairs
, 0);
4747 /* Given two inequality constraints
4749 * f(x) + n d + c >= 0, (ineq)
4751 * with d the variable at position "pos", and
4753 * f(x) + c0 >= 0, (lower)
4755 * compute the maximal value of the lower bound ceil((-f(x) - c)/n)
4756 * determined by the first constraint.
4763 static void lower_bound_from_parallel(__isl_keep isl_basic_map
*bmap
,
4764 int ineq
, int lower
, int pos
, isl_int
*l
)
4766 isl_int_neg(*l
, bmap
->ineq
[ineq
][0]);
4767 isl_int_add(*l
, *l
, bmap
->ineq
[lower
][0]);
4768 isl_int_cdiv_q(*l
, *l
, bmap
->ineq
[ineq
][pos
]);
4771 /* Given two inequality constraints
4773 * f(x) + n d + c >= 0, (ineq)
4775 * with d the variable at position "pos", and
4777 * -f(x) - c0 >= 0, (upper)
4779 * compute the minimal value of the lower bound ceil((-f(x) - c)/n)
4780 * determined by the first constraint.
4787 static void lower_bound_from_opposite(__isl_keep isl_basic_map
*bmap
,
4788 int ineq
, int upper
, int pos
, isl_int
*u
)
4790 isl_int_neg(*u
, bmap
->ineq
[ineq
][0]);
4791 isl_int_sub(*u
, *u
, bmap
->ineq
[upper
][0]);
4792 isl_int_cdiv_q(*u
, *u
, bmap
->ineq
[ineq
][pos
]);
4795 /* Given a lower bound constraint "ineq" on "div" in "bmap",
4796 * does the corresponding lower bound have a fixed value in "bmap"?
4798 * In particular, "ineq" is of the form
4800 * f(x) + n d + c >= 0
4802 * with n > 0, c the constant term and
4803 * d the existentially quantified variable "div".
4804 * That is, the lower bound is
4806 * ceil((-f(x) - c)/n)
4808 * Look for a pair of constraints
4813 * i.e., -c1 <= -f(x) <= c0, that fix ceil((-f(x) - c)/n) to a constant value.
4814 * That is, check that
4816 * ceil((-c1 - c)/n) = ceil((c0 - c)/n)
4818 * If so, return the index of inequality f(x) + c0 >= 0.
4819 * Otherwise, return -1.
4821 static int lower_bound_is_cst(__isl_keep isl_basic_map
*bmap
, int div
, int ineq
)
4824 int lower
= -1, upper
= -1;
4825 unsigned o_div
, n_div
;
4829 n_div
= isl_basic_map_dim(bmap
, isl_dim_div
);
4830 o_div
= isl_basic_map_offset(bmap
, isl_dim_div
);
4831 for (i
= 0; i
< bmap
->n_ineq
&& (lower
< 0 || upper
< 0); ++i
) {
4834 if (!isl_int_is_zero(bmap
->ineq
[i
][o_div
+ div
]))
4837 is_parallel_except(bmap
, ineq
, i
, o_div
+ div
)) {
4842 is_opposite_except(bmap
, ineq
, i
, o_div
+ div
)) {
4847 if (lower
< 0 || upper
< 0)
4853 lower_bound_from_parallel(bmap
, ineq
, lower
, o_div
+ div
, &l
);
4854 lower_bound_from_opposite(bmap
, ineq
, upper
, o_div
+ div
, &u
);
4856 equal
= isl_int_eq(l
, u
);
4861 return equal
? lower
: -1;
4864 /* Given a lower bound constraint "ineq" on the existentially quantified
4865 * variable "div", such that the corresponding lower bound has
4866 * a fixed value in "bmap", assign this fixed value to the variable and
4867 * then try and drop redundant divs again,
4868 * freeing the temporary data structure "pairs" that was associated
4869 * to the old version of "bmap".
4870 * "lower" determines the constant value for the lower bound.
4872 * In particular, "ineq" is of the form
4874 * f(x) + n d + c >= 0,
4876 * while "lower" is of the form
4880 * The lower bound is ceil((-f(x) - c)/n) and its constant value
4881 * is ceil((c0 - c)/n).
4883 static __isl_give isl_basic_map
*fix_cst_lower(__isl_take isl_basic_map
*bmap
,
4884 int div
, int ineq
, int lower
, int *pairs
)
4891 o_div
= isl_basic_map_offset(bmap
, isl_dim_div
);
4892 lower_bound_from_parallel(bmap
, ineq
, lower
, o_div
+ div
, &c
);
4893 bmap
= isl_basic_map_fix(bmap
, isl_dim_div
, div
, c
);
4898 return isl_basic_map_drop_redundant_divs(bmap
);
4901 /* Remove divs that are not strictly needed based on the inequality
4903 * In particular, if a div only occurs positively (or negatively)
4904 * in constraints, then it can simply be dropped.
4905 * Also, if a div occurs in only two constraints and if moreover
4906 * those two constraints are opposite to each other, except for the constant
4907 * term and if the sum of the constant terms is such that for any value
4908 * of the other values, there is always at least one integer value of the
4909 * div, i.e., if one plus this sum is greater than or equal to
4910 * the (absolute value) of the coefficient of the div in the constraints,
4911 * then we can also simply drop the div.
4913 * If an existentially quantified variable does not have an explicit
4914 * representation, appears in only a single lower bound that does not
4915 * involve any other such existentially quantified variables and appears
4916 * in this lower bound with coefficient 1,
4917 * then fix the variable to the value of the lower bound. That is,
4918 * turn the inequality into an equality.
4919 * If for any value of the other variables, there is any value
4920 * for the existentially quantified variable satisfying the constraints,
4921 * then this lower bound also satisfies the constraints.
4922 * It is therefore safe to pick this lower bound.
4924 * The same reasoning holds even if the coefficient is not one.
4925 * However, fixing the variable to the value of the lower bound may
4926 * in general introduce an extra integer division, in which case
4927 * it may be better to pick another value.
4928 * If this integer division has a known constant value, then plugging
4929 * in this constant value removes the existentially quantified variable
4930 * completely. In particular, if the lower bound is of the form
4931 * ceil((-f(x) - c)/n) and there are two constraints, f(x) + c0 >= 0 and
4932 * -f(x) + c1 >= 0 such that ceil((-c1 - c)/n) = ceil((c0 - c)/n),
4933 * then the existentially quantified variable can be assigned this
4936 * We skip divs that appear in equalities or in the definition of other divs.
4937 * Divs that appear in the definition of other divs usually occur in at least
4938 * 4 constraints, but the constraints may have been simplified.
4940 * If any divs are left after these simple checks then we move on
4941 * to more complicated cases in drop_more_redundant_divs.
4943 static __isl_give isl_basic_map
*isl_basic_map_drop_redundant_divs_ineq(
4944 __isl_take isl_basic_map
*bmap
)
4953 if (bmap
->n_div
== 0)
4956 off
= isl_space_dim(bmap
->dim
, isl_dim_all
);
4957 pairs
= isl_calloc_array(bmap
->ctx
, int, bmap
->n_div
);
4961 for (i
= 0; i
< bmap
->n_div
; ++i
) {
4963 int last_pos
, last_neg
;
4967 defined
= !isl_int_is_zero(bmap
->div
[i
][0]);
4968 for (j
= i
; j
< bmap
->n_div
; ++j
)
4969 if (!isl_int_is_zero(bmap
->div
[j
][1 + 1 + off
+ i
]))
4971 if (j
< bmap
->n_div
)
4973 for (j
= 0; j
< bmap
->n_eq
; ++j
)
4974 if (!isl_int_is_zero(bmap
->eq
[j
][1 + off
+ i
]))
4980 for (j
= 0; j
< bmap
->n_ineq
; ++j
) {
4981 if (isl_int_is_pos(bmap
->ineq
[j
][1 + off
+ i
])) {
4985 if (isl_int_is_neg(bmap
->ineq
[j
][1 + off
+ i
])) {
4990 pairs
[i
] = pos
* neg
;
4991 if (pairs
[i
] == 0) {
4992 for (j
= bmap
->n_ineq
- 1; j
>= 0; --j
)
4993 if (!isl_int_is_zero(bmap
->ineq
[j
][1+off
+i
]))
4994 isl_basic_map_drop_inequality(bmap
, j
);
4995 bmap
= isl_basic_map_drop_div(bmap
, i
);
4996 return drop_redundant_divs_again(bmap
, pairs
, 0);
4998 if (pairs
[i
] != 1 || !is_opposite(bmap
, last_pos
, last_neg
)) {
5002 single
= single_unknown(bmap
, last_pos
, i
);
5005 if (has_coef_one(bmap
, i
, last_pos
))
5006 return set_eq_and_try_again(bmap
, last_pos
,
5008 lower
= lower_bound_is_cst(bmap
, i
, last_pos
);
5010 return fix_cst_lower(bmap
, i
, last_pos
, lower
,
5015 isl_int_add(bmap
->ineq
[last_pos
][0],
5016 bmap
->ineq
[last_pos
][0], bmap
->ineq
[last_neg
][0]);
5017 isl_int_add_ui(bmap
->ineq
[last_pos
][0],
5018 bmap
->ineq
[last_pos
][0], 1);
5019 redundant
= isl_int_ge(bmap
->ineq
[last_pos
][0],
5020 bmap
->ineq
[last_pos
][1+off
+i
]);
5021 isl_int_sub_ui(bmap
->ineq
[last_pos
][0],
5022 bmap
->ineq
[last_pos
][0], 1);
5023 isl_int_sub(bmap
->ineq
[last_pos
][0],
5024 bmap
->ineq
[last_pos
][0], bmap
->ineq
[last_neg
][0]);
5026 return drop_div_and_try_again(bmap
, i
,
5027 last_pos
, last_neg
, pairs
);
5028 if (!defined
&& ok_to_set_div_from_bound(bmap
, i
, last_pos
)) {
5029 bmap
= set_div_from_lower_bound(bmap
, i
, last_pos
);
5030 return drop_redundant_divs_again(bmap
, pairs
, 1);
5037 return coalesce_or_drop_more_redundant_divs(bmap
, pairs
, n
);
5043 isl_basic_map_free(bmap
);
5047 /* Consider the coefficients at "c" as a row vector and replace
5048 * them with their product with "T". "T" is assumed to be a square matrix.
5050 static isl_stat
preimage(isl_int
*c
, __isl_keep isl_mat
*T
)
5057 return isl_stat_error
;
5058 n
= isl_mat_rows(T
);
5059 if (isl_seq_first_non_zero(c
, n
) == -1)
5061 ctx
= isl_mat_get_ctx(T
);
5062 v
= isl_vec_alloc(ctx
, n
);
5064 return isl_stat_error
;
5065 isl_seq_swp_or_cpy(v
->el
, c
, n
);
5066 v
= isl_vec_mat_product(v
, isl_mat_copy(T
));
5068 return isl_stat_error
;
5069 isl_seq_swp_or_cpy(c
, v
->el
, n
);
5075 /* Plug in T for the variables in "bmap" starting at "pos".
5076 * T is a linear unimodular matrix, i.e., without constant term.
5078 static __isl_give isl_basic_map
*isl_basic_map_preimage_vars(
5079 __isl_take isl_basic_map
*bmap
, unsigned pos
, __isl_take isl_mat
*T
)
5084 bmap
= isl_basic_map_cow(bmap
);
5088 n
= isl_mat_cols(T
);
5089 if (n
!= isl_mat_rows(T
))
5090 isl_die(isl_mat_get_ctx(T
), isl_error_invalid
,
5091 "expecting square matrix", goto error
);
5093 total
= isl_basic_map_dim(bmap
, isl_dim_all
);
5094 if (pos
+ n
> total
|| pos
+ n
< pos
)
5095 isl_die(isl_mat_get_ctx(T
), isl_error_invalid
,
5096 "invalid range", goto error
);
5098 for (i
= 0; i
< bmap
->n_eq
; ++i
)
5099 if (preimage(bmap
->eq
[i
] + 1 + pos
, T
) < 0)
5101 for (i
= 0; i
< bmap
->n_ineq
; ++i
)
5102 if (preimage(bmap
->ineq
[i
] + 1 + pos
, T
) < 0)
5104 for (i
= 0; i
< bmap
->n_div
; ++i
) {
5105 if (isl_basic_map_div_is_marked_unknown(bmap
, i
))
5107 if (preimage(bmap
->div
[i
] + 1 + 1 + pos
, T
) < 0)
5114 isl_basic_map_free(bmap
);
5119 /* Remove divs that are not strictly needed.
5121 * First look for an equality constraint involving two or more
5122 * existentially quantified variables without an explicit
5123 * representation. Replace the combination that appears
5124 * in the equality constraint by a single existentially quantified
5125 * variable such that the equality can be used to derive
5126 * an explicit representation for the variable.
5127 * If there are no more such equality constraints, then continue
5128 * with isl_basic_map_drop_redundant_divs_ineq.
5130 * In particular, if the equality constraint is of the form
5132 * f(x) + \sum_i c_i a_i = 0
5134 * with a_i existentially quantified variable without explicit
5135 * representation, then apply a transformation on the existentially
5136 * quantified variables to turn the constraint into
5140 * with g the gcd of the c_i.
5141 * In order to easily identify which existentially quantified variables
5142 * have a complete explicit representation, i.e., without being defined
5143 * in terms of other existentially quantified variables without
5144 * an explicit representation, the existentially quantified variables
5147 * The variable transformation is computed by extending the row
5148 * [c_1/g ... c_n/g] to a unimodular matrix, obtaining the transformation
5150 * [a_1'] [c_1/g ... c_n/g] [ a_1 ]
5155 * with [c_1/g ... c_n/g] representing the first row of U.
5156 * The inverse of U is then plugged into the original constraints.
5157 * The call to isl_basic_map_simplify makes sure the explicit
5158 * representation for a_1' is extracted from the equality constraint.
5160 __isl_give isl_basic_map
*isl_basic_map_drop_redundant_divs(
5161 __isl_take isl_basic_map
*bmap
)
5165 unsigned o_div
, n_div
;
5172 if (isl_basic_map_divs_known(bmap
))
5173 return isl_basic_map_drop_redundant_divs_ineq(bmap
);
5174 if (bmap
->n_eq
== 0)
5175 return isl_basic_map_drop_redundant_divs_ineq(bmap
);
5176 bmap
= isl_basic_map_sort_divs(bmap
);
5180 first
= isl_basic_map_first_unknown_div(bmap
);
5182 return isl_basic_map_free(bmap
);
5184 o_div
= isl_basic_map_offset(bmap
, isl_dim_div
);
5185 n_div
= isl_basic_map_dim(bmap
, isl_dim_div
);
5187 for (i
= 0; i
< bmap
->n_eq
; ++i
) {
5188 l
= isl_seq_first_non_zero(bmap
->eq
[i
] + o_div
+ first
,
5193 if (isl_seq_first_non_zero(bmap
->eq
[i
] + o_div
+ l
+ 1,
5194 n_div
- (l
+ 1)) == -1)
5198 if (i
>= bmap
->n_eq
)
5199 return isl_basic_map_drop_redundant_divs_ineq(bmap
);
5201 ctx
= isl_basic_map_get_ctx(bmap
);
5202 T
= isl_mat_alloc(ctx
, n_div
- l
, n_div
- l
);
5204 return isl_basic_map_free(bmap
);
5205 isl_seq_cpy(T
->row
[0], bmap
->eq
[i
] + o_div
+ l
, n_div
- l
);
5206 T
= isl_mat_normalize_row(T
, 0);
5207 T
= isl_mat_unimodular_complete(T
, 1);
5208 T
= isl_mat_right_inverse(T
);
5210 for (i
= l
; i
< n_div
; ++i
)
5211 bmap
= isl_basic_map_mark_div_unknown(bmap
, i
);
5212 bmap
= isl_basic_map_preimage_vars(bmap
, o_div
- 1 + l
, T
);
5213 bmap
= isl_basic_map_simplify(bmap
);
5215 return isl_basic_map_drop_redundant_divs(bmap
);
5218 struct isl_basic_set
*isl_basic_set_drop_redundant_divs(
5219 struct isl_basic_set
*bset
)
5221 isl_basic_map
*bmap
= bset_to_bmap(bset
);
5222 return bset_from_bmap(isl_basic_map_drop_redundant_divs(bmap
));
5225 struct isl_map
*isl_map_drop_redundant_divs(struct isl_map
*map
)
5231 for (i
= 0; i
< map
->n
; ++i
) {
5232 map
->p
[i
] = isl_basic_map_drop_redundant_divs(map
->p
[i
]);
5236 ISL_F_CLR(map
, ISL_MAP_NORMALIZED
);
5243 struct isl_set
*isl_set_drop_redundant_divs(struct isl_set
*set
)
5245 return set_from_map(isl_map_drop_redundant_divs(set_to_map(set
)));
5248 /* Does "bmap" satisfy any equality that involves more than 2 variables
5249 * and/or has coefficients different from -1 and 1?
5251 static int has_multiple_var_equality(__isl_keep isl_basic_map
*bmap
)
5256 total
= isl_basic_map_dim(bmap
, isl_dim_all
);
5258 for (i
= 0; i
< bmap
->n_eq
; ++i
) {
5261 j
= isl_seq_first_non_zero(bmap
->eq
[i
] + 1, total
);
5264 if (!isl_int_is_one(bmap
->eq
[i
][1 + j
]) &&
5265 !isl_int_is_negone(bmap
->eq
[i
][1 + j
]))
5269 k
= isl_seq_first_non_zero(bmap
->eq
[i
] + 1 + j
, total
- j
);
5273 if (!isl_int_is_one(bmap
->eq
[i
][1 + j
]) &&
5274 !isl_int_is_negone(bmap
->eq
[i
][1 + j
]))
5278 k
= isl_seq_first_non_zero(bmap
->eq
[i
] + 1 + j
, total
- j
);
5286 /* Remove any common factor g from the constraint coefficients in "v".
5287 * The constant term is stored in the first position and is replaced
5288 * by floor(c/g). If any common factor is removed and if this results
5289 * in a tightening of the constraint, then set *tightened.
5291 static __isl_give isl_vec
*normalize_constraint(__isl_take isl_vec
*v
,
5298 ctx
= isl_vec_get_ctx(v
);
5299 isl_seq_gcd(v
->el
+ 1, v
->size
- 1, &ctx
->normalize_gcd
);
5300 if (isl_int_is_zero(ctx
->normalize_gcd
))
5302 if (isl_int_is_one(ctx
->normalize_gcd
))
5307 if (tightened
&& !isl_int_is_divisible_by(v
->el
[0], ctx
->normalize_gcd
))
5309 isl_int_fdiv_q(v
->el
[0], v
->el
[0], ctx
->normalize_gcd
);
5310 isl_seq_scale_down(v
->el
+ 1, v
->el
+ 1, ctx
->normalize_gcd
,
5315 /* If "bmap" is an integer set that satisfies any equality involving
5316 * more than 2 variables and/or has coefficients different from -1 and 1,
5317 * then use variable compression to reduce the coefficients by removing
5318 * any (hidden) common factor.
5319 * In particular, apply the variable compression to each constraint,
5320 * factor out any common factor in the non-constant coefficients and
5321 * then apply the inverse of the compression.
5322 * At the end, we mark the basic map as having reduced constants.
5323 * If this flag is still set on the next invocation of this function,
5324 * then we skip the computation.
5326 * Removing a common factor may result in a tightening of some of
5327 * the constraints. If this happens, then we may end up with two
5328 * opposite inequalities that can be replaced by an equality.
5329 * We therefore call isl_basic_map_detect_inequality_pairs,
5330 * which checks for such pairs of inequalities as well as eliminate_divs_eq
5331 * and isl_basic_map_gauss if such a pair was found.
5333 __isl_give isl_basic_map
*isl_basic_map_reduce_coefficients(
5334 __isl_take isl_basic_map
*bmap
)
5339 isl_mat
*eq
, *T
, *T2
;
5345 if (ISL_F_ISSET(bmap
, ISL_BASIC_MAP_REDUCED_COEFFICIENTS
))
5347 if (isl_basic_map_is_rational(bmap
))
5349 if (bmap
->n_eq
== 0)
5351 if (!has_multiple_var_equality(bmap
))
5354 total
= isl_basic_map_dim(bmap
, isl_dim_all
);
5355 ctx
= isl_basic_map_get_ctx(bmap
);
5356 v
= isl_vec_alloc(ctx
, 1 + total
);
5358 return isl_basic_map_free(bmap
);
5360 eq
= isl_mat_sub_alloc6(ctx
, bmap
->eq
, 0, bmap
->n_eq
, 0, 1 + total
);
5361 T
= isl_mat_variable_compression(eq
, &T2
);
5364 if (T
->n_col
== 0) {
5368 return isl_basic_map_set_to_empty(bmap
);
5372 for (i
= 0; i
< bmap
->n_ineq
; ++i
) {
5373 isl_seq_cpy(v
->el
, bmap
->ineq
[i
], 1 + total
);
5374 v
= isl_vec_mat_product(v
, isl_mat_copy(T
));
5375 v
= normalize_constraint(v
, &tightened
);
5376 v
= isl_vec_mat_product(v
, isl_mat_copy(T2
));
5379 isl_seq_cpy(bmap
->ineq
[i
], v
->el
, 1 + total
);
5386 ISL_F_SET(bmap
, ISL_BASIC_MAP_REDUCED_COEFFICIENTS
);
5391 bmap
= isl_basic_map_detect_inequality_pairs(bmap
, &progress
);
5393 bmap
= eliminate_divs_eq(bmap
, &progress
);
5394 bmap
= isl_basic_map_gauss(bmap
, NULL
);
5403 return isl_basic_map_free(bmap
);
5406 /* Shift the integer division at position "div" of "bmap"
5407 * by "shift" times the variable at position "pos".
5408 * "pos" is as determined by isl_basic_map_offset, i.e., pos == 0
5409 * corresponds to the constant term.
5411 * That is, if the integer division has the form
5415 * then replace it by
5417 * floor((f(x) + shift * d * x_pos)/d) - shift * x_pos
5419 __isl_give isl_basic_map
*isl_basic_map_shift_div(
5420 __isl_take isl_basic_map
*bmap
, int div
, int pos
, isl_int shift
)
5425 if (isl_int_is_zero(shift
))
5430 total
= isl_basic_map_dim(bmap
, isl_dim_all
);
5431 total
-= isl_basic_map_dim(bmap
, isl_dim_div
);
5433 isl_int_addmul(bmap
->div
[div
][1 + pos
], shift
, bmap
->div
[div
][0]);
5435 for (i
= 0; i
< bmap
->n_eq
; ++i
) {
5436 if (isl_int_is_zero(bmap
->eq
[i
][1 + total
+ div
]))
5438 isl_int_submul(bmap
->eq
[i
][pos
],
5439 shift
, bmap
->eq
[i
][1 + total
+ div
]);
5441 for (i
= 0; i
< bmap
->n_ineq
; ++i
) {
5442 if (isl_int_is_zero(bmap
->ineq
[i
][1 + total
+ div
]))
5444 isl_int_submul(bmap
->ineq
[i
][pos
],
5445 shift
, bmap
->ineq
[i
][1 + total
+ div
]);
5447 for (i
= 0; i
< bmap
->n_div
; ++i
) {
5448 if (isl_int_is_zero(bmap
->div
[i
][0]))
5450 if (isl_int_is_zero(bmap
->div
[i
][1 + 1 + total
+ div
]))
5452 isl_int_submul(bmap
->div
[i
][1 + pos
],
5453 shift
, bmap
->div
[i
][1 + 1 + total
+ div
]);