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_is_named_or_nested(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 /* Move "n" divs starting at "first" to the end of the list of divs.
103 static struct isl_basic_map
*move_divs_last(struct isl_basic_map
*bmap
,
104 unsigned first
, unsigned n
)
109 if (first
+ n
== bmap
->n_div
)
112 div
= isl_alloc_array(bmap
->ctx
, isl_int
*, n
);
115 for (i
= 0; i
< n
; ++i
)
116 div
[i
] = bmap
->div
[first
+ i
];
117 for (i
= 0; i
< bmap
->n_div
- first
- n
; ++i
)
118 bmap
->div
[first
+ i
] = bmap
->div
[first
+ n
+ i
];
119 for (i
= 0; i
< n
; ++i
)
120 bmap
->div
[bmap
->n_div
- n
+ i
] = div
[i
];
124 isl_basic_map_free(bmap
);
128 /* Drop "n" dimensions of type "type" starting at "first".
130 * In principle, this frees up some extra variables as the number
131 * of columns remains constant, but we would have to extend
132 * the div array too as the number of rows in this array is assumed
133 * to be equal to extra.
135 struct isl_basic_map
*isl_basic_map_drop(struct isl_basic_map
*bmap
,
136 enum isl_dim_type type
, unsigned first
, unsigned n
)
146 dim
= isl_basic_map_dim(bmap
, type
);
147 isl_assert(bmap
->ctx
, first
+ n
<= dim
, goto error
);
149 if (n
== 0 && !isl_space_is_named_or_nested(bmap
->dim
, type
))
152 bmap
= isl_basic_map_cow(bmap
);
156 offset
= isl_basic_map_offset(bmap
, type
) + first
;
157 left
= isl_basic_map_total_dim(bmap
) - (offset
- 1) - n
;
158 for (i
= 0; i
< bmap
->n_eq
; ++i
)
159 constraint_drop_vars(bmap
->eq
[i
]+offset
, n
, left
);
161 for (i
= 0; i
< bmap
->n_ineq
; ++i
)
162 constraint_drop_vars(bmap
->ineq
[i
]+offset
, n
, left
);
164 for (i
= 0; i
< bmap
->n_div
; ++i
)
165 constraint_drop_vars(bmap
->div
[i
]+1+offset
, n
, left
);
167 if (type
== isl_dim_div
) {
168 bmap
= move_divs_last(bmap
, first
, n
);
171 if (isl_basic_map_free_div(bmap
, n
) < 0)
172 return isl_basic_map_free(bmap
);
174 bmap
->dim
= isl_space_drop_dims(bmap
->dim
, type
, first
, n
);
178 ISL_F_CLR(bmap
, ISL_BASIC_MAP_NORMALIZED
);
179 bmap
= isl_basic_map_simplify(bmap
);
180 return isl_basic_map_finalize(bmap
);
182 isl_basic_map_free(bmap
);
186 __isl_give isl_basic_set
*isl_basic_set_drop(__isl_take isl_basic_set
*bset
,
187 enum isl_dim_type type
, unsigned first
, unsigned n
)
189 return bset_from_bmap(isl_basic_map_drop(bset_to_bmap(bset
),
193 struct isl_map
*isl_map_drop(struct isl_map
*map
,
194 enum isl_dim_type type
, unsigned first
, unsigned n
)
201 isl_assert(map
->ctx
, first
+ n
<= isl_map_dim(map
, type
), goto error
);
203 if (n
== 0 && !isl_space_is_named_or_nested(map
->dim
, type
))
205 map
= isl_map_cow(map
);
208 map
->dim
= isl_space_drop_dims(map
->dim
, type
, first
, n
);
212 for (i
= 0; i
< map
->n
; ++i
) {
213 map
->p
[i
] = isl_basic_map_drop(map
->p
[i
], type
, first
, n
);
217 ISL_F_CLR(map
, ISL_MAP_NORMALIZED
);
225 struct isl_set
*isl_set_drop(struct isl_set
*set
,
226 enum isl_dim_type type
, unsigned first
, unsigned n
)
228 return set_from_map(isl_map_drop(set_to_map(set
), type
, first
, n
));
232 * We don't cow, as the div is assumed to be redundant.
234 __isl_give isl_basic_map
*isl_basic_map_drop_div(
235 __isl_take isl_basic_map
*bmap
, unsigned div
)
243 pos
= 1 + isl_space_dim(bmap
->dim
, isl_dim_all
) + div
;
245 isl_assert(bmap
->ctx
, div
< bmap
->n_div
, goto error
);
247 for (i
= 0; i
< bmap
->n_eq
; ++i
)
248 constraint_drop_vars(bmap
->eq
[i
]+pos
, 1, bmap
->extra
-div
-1);
250 for (i
= 0; i
< bmap
->n_ineq
; ++i
) {
251 if (!isl_int_is_zero(bmap
->ineq
[i
][pos
])) {
252 isl_basic_map_drop_inequality(bmap
, i
);
256 constraint_drop_vars(bmap
->ineq
[i
]+pos
, 1, bmap
->extra
-div
-1);
259 for (i
= 0; i
< bmap
->n_div
; ++i
)
260 constraint_drop_vars(bmap
->div
[i
]+1+pos
, 1, bmap
->extra
-div
-1);
262 if (div
!= bmap
->n_div
- 1) {
264 isl_int
*t
= bmap
->div
[div
];
266 for (j
= div
; j
< bmap
->n_div
- 1; ++j
)
267 bmap
->div
[j
] = bmap
->div
[j
+1];
269 bmap
->div
[bmap
->n_div
- 1] = t
;
271 ISL_F_CLR(bmap
, ISL_BASIC_MAP_NORMALIZED
);
272 if (isl_basic_map_free_div(bmap
, 1) < 0)
273 return isl_basic_map_free(bmap
);
277 isl_basic_map_free(bmap
);
281 struct isl_basic_map
*isl_basic_map_normalize_constraints(
282 struct isl_basic_map
*bmap
)
286 unsigned total
= isl_basic_map_total_dim(bmap
);
292 for (i
= bmap
->n_eq
- 1; i
>= 0; --i
) {
293 isl_seq_gcd(bmap
->eq
[i
]+1, total
, &gcd
);
294 if (isl_int_is_zero(gcd
)) {
295 if (!isl_int_is_zero(bmap
->eq
[i
][0])) {
296 bmap
= isl_basic_map_set_to_empty(bmap
);
299 isl_basic_map_drop_equality(bmap
, i
);
302 if (ISL_F_ISSET(bmap
, ISL_BASIC_MAP_RATIONAL
))
303 isl_int_gcd(gcd
, gcd
, bmap
->eq
[i
][0]);
304 if (isl_int_is_one(gcd
))
306 if (!isl_int_is_divisible_by(bmap
->eq
[i
][0], gcd
)) {
307 bmap
= isl_basic_map_set_to_empty(bmap
);
310 isl_seq_scale_down(bmap
->eq
[i
], bmap
->eq
[i
], gcd
, 1+total
);
313 for (i
= bmap
->n_ineq
- 1; i
>= 0; --i
) {
314 isl_seq_gcd(bmap
->ineq
[i
]+1, total
, &gcd
);
315 if (isl_int_is_zero(gcd
)) {
316 if (isl_int_is_neg(bmap
->ineq
[i
][0])) {
317 bmap
= isl_basic_map_set_to_empty(bmap
);
320 isl_basic_map_drop_inequality(bmap
, i
);
323 if (ISL_F_ISSET(bmap
, ISL_BASIC_MAP_RATIONAL
))
324 isl_int_gcd(gcd
, gcd
, bmap
->ineq
[i
][0]);
325 if (isl_int_is_one(gcd
))
327 isl_int_fdiv_q(bmap
->ineq
[i
][0], bmap
->ineq
[i
][0], gcd
);
328 isl_seq_scale_down(bmap
->ineq
[i
]+1, bmap
->ineq
[i
]+1, gcd
, total
);
335 struct isl_basic_set
*isl_basic_set_normalize_constraints(
336 struct isl_basic_set
*bset
)
338 isl_basic_map
*bmap
= bset_to_bmap(bset
);
339 return bset_from_bmap(isl_basic_map_normalize_constraints(bmap
));
342 /* Reduce the coefficient of the variable at position "pos"
343 * in integer division "div", such that it lies in the half-open
344 * interval (1/2,1/2], extracting any excess value from this integer division.
345 * "pos" is as determined by isl_basic_map_offset, i.e., pos == 0
346 * corresponds to the constant term.
348 * That is, the integer division is of the form
350 * floor((... + (c * d + r) * x_pos + ...)/d)
352 * with -d < 2 * r <= d.
355 * floor((... + r * x_pos + ...)/d) + c * x_pos
357 * If 2 * ((c * d + r) % d) <= d, then c = floor((c * d + r)/d).
358 * Otherwise, c = floor((c * d + r)/d) + 1.
360 * This is the same normalization that is performed by isl_aff_floor.
362 static __isl_give isl_basic_map
*reduce_coefficient_in_div(
363 __isl_take isl_basic_map
*bmap
, int div
, int pos
)
369 isl_int_fdiv_r(shift
, bmap
->div
[div
][1 + pos
], bmap
->div
[div
][0]);
370 isl_int_mul_ui(shift
, shift
, 2);
371 add_one
= isl_int_gt(shift
, bmap
->div
[div
][0]);
372 isl_int_fdiv_q(shift
, bmap
->div
[div
][1 + pos
], bmap
->div
[div
][0]);
374 isl_int_add_ui(shift
, shift
, 1);
375 isl_int_neg(shift
, shift
);
376 bmap
= isl_basic_map_shift_div(bmap
, div
, pos
, shift
);
377 isl_int_clear(shift
);
382 /* Does the coefficient of the variable at position "pos"
383 * in integer division "div" need to be reduced?
384 * That is, does it lie outside the half-open interval (1/2,1/2]?
385 * The coefficient c/d lies outside this interval if abs(2 * c) >= d and
388 static isl_bool
needs_reduction(__isl_keep isl_basic_map
*bmap
, int div
,
393 if (isl_int_is_zero(bmap
->div
[div
][1 + pos
]))
394 return isl_bool_false
;
396 isl_int_mul_ui(bmap
->div
[div
][1 + pos
], bmap
->div
[div
][1 + pos
], 2);
397 r
= isl_int_abs_ge(bmap
->div
[div
][1 + pos
], bmap
->div
[div
][0]) &&
398 !isl_int_eq(bmap
->div
[div
][1 + pos
], bmap
->div
[div
][0]);
399 isl_int_divexact_ui(bmap
->div
[div
][1 + pos
],
400 bmap
->div
[div
][1 + pos
], 2);
405 /* Reduce the coefficients (including the constant term) of
406 * integer division "div", if needed.
407 * In particular, make sure all coefficients lie in
408 * the half-open interval (1/2,1/2].
410 static __isl_give isl_basic_map
*reduce_div_coefficients_of_div(
411 __isl_take isl_basic_map
*bmap
, int div
)
414 unsigned total
= 1 + isl_basic_map_total_dim(bmap
);
416 for (i
= 0; i
< total
; ++i
) {
419 reduce
= needs_reduction(bmap
, div
, i
);
421 return isl_basic_map_free(bmap
);
424 bmap
= reduce_coefficient_in_div(bmap
, div
, i
);
432 /* Reduce the coefficients (including the constant term) of
433 * the known integer divisions, if needed
434 * In particular, make sure all coefficients lie in
435 * the half-open interval (1/2,1/2].
437 static __isl_give isl_basic_map
*reduce_div_coefficients(
438 __isl_take isl_basic_map
*bmap
)
444 if (bmap
->n_div
== 0)
447 for (i
= 0; i
< bmap
->n_div
; ++i
) {
448 if (isl_int_is_zero(bmap
->div
[i
][0]))
450 bmap
= reduce_div_coefficients_of_div(bmap
, i
);
458 /* Remove any common factor in numerator and denominator of the div expression,
459 * not taking into account the constant term.
460 * That is, if the div is of the form
462 * floor((a + m f(x))/(m d))
466 * floor((floor(a/m) + f(x))/d)
468 * The difference {a/m}/d in the argument satisfies 0 <= {a/m}/d < 1/d
469 * and can therefore not influence the result of the floor.
471 static void normalize_div_expression(__isl_keep isl_basic_map
*bmap
, int div
)
473 unsigned total
= isl_basic_map_total_dim(bmap
);
474 isl_ctx
*ctx
= bmap
->ctx
;
476 if (isl_int_is_zero(bmap
->div
[div
][0]))
478 isl_seq_gcd(bmap
->div
[div
] + 2, total
, &ctx
->normalize_gcd
);
479 isl_int_gcd(ctx
->normalize_gcd
, ctx
->normalize_gcd
, bmap
->div
[div
][0]);
480 if (isl_int_is_one(ctx
->normalize_gcd
))
482 isl_int_fdiv_q(bmap
->div
[div
][1], bmap
->div
[div
][1],
484 isl_int_divexact(bmap
->div
[div
][0], bmap
->div
[div
][0],
486 isl_seq_scale_down(bmap
->div
[div
] + 2, bmap
->div
[div
] + 2,
487 ctx
->normalize_gcd
, total
);
490 /* Remove any common factor in numerator and denominator of a div expression,
491 * not taking into account the constant term.
492 * That is, look for any div of the form
494 * floor((a + m f(x))/(m d))
498 * floor((floor(a/m) + f(x))/d)
500 * The difference {a/m}/d in the argument satisfies 0 <= {a/m}/d < 1/d
501 * and can therefore not influence the result of the floor.
503 static __isl_give isl_basic_map
*normalize_div_expressions(
504 __isl_take isl_basic_map
*bmap
)
510 if (bmap
->n_div
== 0)
513 for (i
= 0; i
< bmap
->n_div
; ++i
)
514 normalize_div_expression(bmap
, i
);
519 /* Assumes divs have been ordered if keep_divs is set.
521 static void eliminate_var_using_equality(struct isl_basic_map
*bmap
,
522 unsigned pos
, isl_int
*eq
, int keep_divs
, int *progress
)
525 unsigned space_total
;
529 total
= isl_basic_map_total_dim(bmap
);
530 space_total
= isl_space_dim(bmap
->dim
, isl_dim_all
);
531 last_div
= isl_seq_last_non_zero(eq
+ 1 + space_total
, bmap
->n_div
);
532 for (k
= 0; k
< bmap
->n_eq
; ++k
) {
533 if (bmap
->eq
[k
] == eq
)
535 if (isl_int_is_zero(bmap
->eq
[k
][1+pos
]))
539 isl_seq_elim(bmap
->eq
[k
], eq
, 1+pos
, 1+total
, NULL
);
540 isl_seq_normalize(bmap
->ctx
, bmap
->eq
[k
], 1 + total
);
543 for (k
= 0; k
< bmap
->n_ineq
; ++k
) {
544 if (isl_int_is_zero(bmap
->ineq
[k
][1+pos
]))
548 isl_seq_elim(bmap
->ineq
[k
], eq
, 1+pos
, 1+total
, NULL
);
549 isl_seq_normalize(bmap
->ctx
, bmap
->ineq
[k
], 1 + total
);
550 ISL_F_CLR(bmap
, ISL_BASIC_MAP_NORMALIZED
);
553 for (k
= 0; k
< bmap
->n_div
; ++k
) {
554 if (isl_int_is_zero(bmap
->div
[k
][0]))
556 if (isl_int_is_zero(bmap
->div
[k
][1+1+pos
]))
560 /* We need to be careful about circular definitions,
561 * so for now we just remove the definition of div k
562 * if the equality contains any divs.
563 * If keep_divs is set, then the divs have been ordered
564 * and we can keep the definition as long as the result
567 if (last_div
== -1 || (keep_divs
&& last_div
< k
)) {
568 isl_seq_elim(bmap
->div
[k
]+1, eq
,
569 1+pos
, 1+total
, &bmap
->div
[k
][0]);
570 normalize_div_expression(bmap
, k
);
572 isl_seq_clr(bmap
->div
[k
], 1 + total
);
573 ISL_F_CLR(bmap
, ISL_BASIC_MAP_NORMALIZED
);
577 /* Assumes divs have been ordered if keep_divs is set.
579 static __isl_give isl_basic_map
*eliminate_div(__isl_take isl_basic_map
*bmap
,
580 isl_int
*eq
, unsigned div
, int keep_divs
)
582 unsigned pos
= isl_space_dim(bmap
->dim
, isl_dim_all
) + div
;
584 eliminate_var_using_equality(bmap
, pos
, eq
, keep_divs
, NULL
);
586 bmap
= isl_basic_map_drop_div(bmap
, div
);
591 /* Check if elimination of div "div" using equality "eq" would not
592 * result in a div depending on a later div.
594 static isl_bool
ok_to_eliminate_div(struct isl_basic_map
*bmap
, isl_int
*eq
,
599 unsigned space_total
= isl_space_dim(bmap
->dim
, isl_dim_all
);
600 unsigned pos
= space_total
+ div
;
602 last_div
= isl_seq_last_non_zero(eq
+ 1 + space_total
, bmap
->n_div
);
603 if (last_div
< 0 || last_div
<= div
)
604 return isl_bool_true
;
606 for (k
= 0; k
<= last_div
; ++k
) {
607 if (isl_int_is_zero(bmap
->div
[k
][0]))
609 if (!isl_int_is_zero(bmap
->div
[k
][1 + 1 + pos
]))
610 return isl_bool_false
;
613 return isl_bool_true
;
616 /* Eliminate divs based on equalities
618 static struct isl_basic_map
*eliminate_divs_eq(
619 struct isl_basic_map
*bmap
, int *progress
)
626 bmap
= isl_basic_map_order_divs(bmap
);
631 off
= 1 + isl_space_dim(bmap
->dim
, isl_dim_all
);
633 for (d
= bmap
->n_div
- 1; d
>= 0 ; --d
) {
634 for (i
= 0; i
< bmap
->n_eq
; ++i
) {
637 if (!isl_int_is_one(bmap
->eq
[i
][off
+ d
]) &&
638 !isl_int_is_negone(bmap
->eq
[i
][off
+ d
]))
640 ok
= ok_to_eliminate_div(bmap
, bmap
->eq
[i
], d
);
642 return isl_basic_map_free(bmap
);
647 bmap
= eliminate_div(bmap
, bmap
->eq
[i
], d
, 1);
648 if (isl_basic_map_drop_equality(bmap
, i
) < 0)
649 return isl_basic_map_free(bmap
);
654 return eliminate_divs_eq(bmap
, progress
);
658 /* Elimininate divs based on inequalities
660 static struct isl_basic_map
*eliminate_divs_ineq(
661 struct isl_basic_map
*bmap
, int *progress
)
672 off
= 1 + isl_space_dim(bmap
->dim
, isl_dim_all
);
674 for (d
= bmap
->n_div
- 1; d
>= 0 ; --d
) {
675 for (i
= 0; i
< bmap
->n_eq
; ++i
)
676 if (!isl_int_is_zero(bmap
->eq
[i
][off
+ d
]))
680 for (i
= 0; i
< bmap
->n_ineq
; ++i
)
681 if (isl_int_abs_gt(bmap
->ineq
[i
][off
+ d
], ctx
->one
))
683 if (i
< bmap
->n_ineq
)
686 bmap
= isl_basic_map_eliminate_vars(bmap
, (off
-1)+d
, 1);
687 if (!bmap
|| ISL_F_ISSET(bmap
, ISL_BASIC_MAP_EMPTY
))
689 bmap
= isl_basic_map_drop_div(bmap
, d
);
696 /* Does the equality constraint at position "eq" in "bmap" involve
697 * any local variables in the range [first, first + n)
698 * that are not marked as having an explicit representation?
700 static isl_bool
bmap_eq_involves_unknown_divs(__isl_keep isl_basic_map
*bmap
,
701 int eq
, unsigned first
, unsigned n
)
707 return isl_bool_error
;
709 o_div
= isl_basic_map_offset(bmap
, isl_dim_div
);
710 for (i
= 0; i
< n
; ++i
) {
713 if (isl_int_is_zero(bmap
->eq
[eq
][o_div
+ first
+ i
]))
715 unknown
= isl_basic_map_div_is_marked_unknown(bmap
, first
+ i
);
717 return isl_bool_error
;
719 return isl_bool_true
;
722 return isl_bool_false
;
725 /* The last local variable involved in the equality constraint
726 * at position "eq" in "bmap" is the local variable at position "div".
727 * It can therefore be used to extract an explicit representation
729 * Do so unless the local variable already has an explicit representation or
730 * the explicit representation would involve any other local variables
731 * that in turn do not have an explicit representation.
732 * An equality constraint involving local variables without an explicit
733 * representation can be used in isl_basic_map_drop_redundant_divs
734 * to separate out an independent local variable. Introducing
735 * an explicit representation here would block this transformation,
736 * while the partial explicit representation in itself is not very useful.
737 * Set *progress if anything is changed.
739 * The equality constraint is of the form
743 * with n a positive number. The explicit representation derived from
748 static __isl_give isl_basic_map
*set_div_from_eq(__isl_take isl_basic_map
*bmap
,
749 int div
, int eq
, int *progress
)
751 unsigned total
, o_div
;
757 if (!isl_int_is_zero(bmap
->div
[div
][0]))
760 involves
= bmap_eq_involves_unknown_divs(bmap
, eq
, 0, div
);
762 return isl_basic_map_free(bmap
);
766 total
= isl_basic_map_dim(bmap
, isl_dim_all
);
767 o_div
= isl_basic_map_offset(bmap
, isl_dim_div
);
768 isl_seq_neg(bmap
->div
[div
] + 1, bmap
->eq
[eq
], 1 + total
);
769 isl_int_set_si(bmap
->div
[div
][1 + o_div
+ div
], 0);
770 isl_int_set(bmap
->div
[div
][0], bmap
->eq
[eq
][o_div
+ div
]);
773 ISL_F_CLR(bmap
, ISL_BASIC_MAP_NORMALIZED
);
778 struct isl_basic_map
*isl_basic_map_gauss(
779 struct isl_basic_map
*bmap
, int *progress
)
787 bmap
= isl_basic_map_order_divs(bmap
);
792 total
= isl_basic_map_total_dim(bmap
);
793 total_var
= total
- bmap
->n_div
;
795 last_var
= total
- 1;
796 for (done
= 0; done
< bmap
->n_eq
; ++done
) {
797 for (; last_var
>= 0; --last_var
) {
798 for (k
= done
; k
< bmap
->n_eq
; ++k
)
799 if (!isl_int_is_zero(bmap
->eq
[k
][1+last_var
]))
807 swap_equality(bmap
, k
, done
);
808 if (isl_int_is_neg(bmap
->eq
[done
][1+last_var
]))
809 isl_seq_neg(bmap
->eq
[done
], bmap
->eq
[done
], 1+total
);
811 eliminate_var_using_equality(bmap
, last_var
, bmap
->eq
[done
], 1,
814 if (last_var
>= total_var
)
815 bmap
= set_div_from_eq(bmap
, last_var
- total_var
,
820 if (done
== bmap
->n_eq
)
822 for (k
= done
; k
< bmap
->n_eq
; ++k
) {
823 if (isl_int_is_zero(bmap
->eq
[k
][0]))
825 return isl_basic_map_set_to_empty(bmap
);
827 isl_basic_map_free_equality(bmap
, bmap
->n_eq
-done
);
831 struct isl_basic_set
*isl_basic_set_gauss(
832 struct isl_basic_set
*bset
, int *progress
)
834 return bset_from_bmap(isl_basic_map_gauss(bset_to_bmap(bset
),
839 static unsigned int round_up(unsigned int v
)
850 /* Hash table of inequalities in a basic map.
851 * "index" is an array of addresses of inequalities in the basic map, some
852 * of which are NULL. The inequalities are hashed on the coefficients
853 * except the constant term.
854 * "size" is the number of elements in the array and is always a power of two
855 * "bits" is the number of bits need to represent an index into the array.
856 * "total" is the total dimension of the basic map.
858 struct isl_constraint_index
{
865 /* Fill in the "ci" data structure for holding the inequalities of "bmap".
867 static isl_stat
create_constraint_index(struct isl_constraint_index
*ci
,
868 __isl_keep isl_basic_map
*bmap
)
874 return isl_stat_error
;
875 ci
->total
= isl_basic_set_total_dim(bmap
);
876 if (bmap
->n_ineq
== 0)
878 ci
->size
= round_up(4 * (bmap
->n_ineq
+ 1) / 3 - 1);
879 ci
->bits
= ffs(ci
->size
) - 1;
880 ctx
= isl_basic_map_get_ctx(bmap
);
881 ci
->index
= isl_calloc_array(ctx
, isl_int
**, ci
->size
);
883 return isl_stat_error
;
888 /* Free the memory allocated by create_constraint_index.
890 static void constraint_index_free(struct isl_constraint_index
*ci
)
895 /* Return the position in ci->index that contains the address of
896 * an inequality that is equal to *ineq up to the constant term,
897 * provided this address is not identical to "ineq".
898 * If there is no such inequality, then return the position where
899 * such an inequality should be inserted.
901 static int hash_index_ineq(struct isl_constraint_index
*ci
, isl_int
**ineq
)
904 uint32_t hash
= isl_seq_get_hash_bits((*ineq
) + 1, ci
->total
, ci
->bits
);
905 for (h
= hash
; ci
->index
[h
]; h
= (h
+1) % ci
->size
)
906 if (ineq
!= ci
->index
[h
] &&
907 isl_seq_eq((*ineq
) + 1, ci
->index
[h
][0]+1, ci
->total
))
912 /* Return the position in ci->index that contains the address of
913 * an inequality that is equal to the k'th inequality of "bmap"
914 * up to the constant term, provided it does not point to the very
916 * If there is no such inequality, then return the position where
917 * such an inequality should be inserted.
919 static int hash_index(struct isl_constraint_index
*ci
,
920 __isl_keep isl_basic_map
*bmap
, int k
)
922 return hash_index_ineq(ci
, &bmap
->ineq
[k
]);
925 static int set_hash_index(struct isl_constraint_index
*ci
,
926 struct isl_basic_set
*bset
, int k
)
928 return hash_index(ci
, bset
, k
);
931 /* Fill in the "ci" data structure with the inequalities of "bset".
933 static isl_stat
setup_constraint_index(struct isl_constraint_index
*ci
,
934 __isl_keep isl_basic_set
*bset
)
938 if (create_constraint_index(ci
, bset
) < 0)
939 return isl_stat_error
;
941 for (k
= 0; k
< bset
->n_ineq
; ++k
) {
942 h
= set_hash_index(ci
, bset
, k
);
943 ci
->index
[h
] = &bset
->ineq
[k
];
949 /* Is the inequality ineq (obviously) redundant with respect
950 * to the constraints in "ci"?
952 * Look for an inequality in "ci" with the same coefficients and then
953 * check if the contant term of "ineq" is greater than or equal
954 * to the constant term of that inequality. If so, "ineq" is clearly
957 * Note that hash_index_ineq ignores a stored constraint if it has
958 * the same address as the passed inequality. It is ok to pass
959 * the address of a local variable here since it will never be
960 * the same as the address of a constraint in "ci".
962 static isl_bool
constraint_index_is_redundant(struct isl_constraint_index
*ci
,
967 h
= hash_index_ineq(ci
, &ineq
);
969 return isl_bool_false
;
970 return isl_int_ge(ineq
[0], (*ci
->index
[h
])[0]);
973 /* If we can eliminate more than one div, then we need to make
974 * sure we do it from last div to first div, in order not to
975 * change the position of the other divs that still need to
978 static struct isl_basic_map
*remove_duplicate_divs(
979 struct isl_basic_map
*bmap
, int *progress
)
991 bmap
= isl_basic_map_order_divs(bmap
);
992 if (!bmap
|| bmap
->n_div
<= 1)
995 total_var
= isl_space_dim(bmap
->dim
, isl_dim_all
);
996 total
= total_var
+ bmap
->n_div
;
999 for (k
= bmap
->n_div
- 1; k
>= 0; --k
)
1000 if (!isl_int_is_zero(bmap
->div
[k
][0]))
1005 size
= round_up(4 * bmap
->n_div
/ 3 - 1);
1008 elim_for
= isl_calloc_array(ctx
, int, bmap
->n_div
);
1009 bits
= ffs(size
) - 1;
1010 index
= isl_calloc_array(ctx
, int, size
);
1011 if (!elim_for
|| !index
)
1013 eq
= isl_blk_alloc(ctx
, 1+total
);
1014 if (isl_blk_is_error(eq
))
1017 isl_seq_clr(eq
.data
, 1+total
);
1018 index
[isl_seq_get_hash_bits(bmap
->div
[k
], 2+total
, bits
)] = k
+ 1;
1019 for (--k
; k
>= 0; --k
) {
1022 if (isl_int_is_zero(bmap
->div
[k
][0]))
1025 hash
= isl_seq_get_hash_bits(bmap
->div
[k
], 2+total
, bits
);
1026 for (h
= hash
; index
[h
]; h
= (h
+1) % size
)
1027 if (isl_seq_eq(bmap
->div
[k
],
1028 bmap
->div
[index
[h
]-1], 2+total
))
1033 elim_for
[l
] = k
+ 1;
1037 for (l
= bmap
->n_div
- 1; l
>= 0; --l
) {
1040 k
= elim_for
[l
] - 1;
1041 isl_int_set_si(eq
.data
[1+total_var
+k
], -1);
1042 isl_int_set_si(eq
.data
[1+total_var
+l
], 1);
1043 bmap
= eliminate_div(bmap
, eq
.data
, l
, 1);
1046 isl_int_set_si(eq
.data
[1+total_var
+k
], 0);
1047 isl_int_set_si(eq
.data
[1+total_var
+l
], 0);
1050 isl_blk_free(ctx
, eq
);
1057 static int n_pure_div_eq(struct isl_basic_map
*bmap
)
1062 total
= isl_space_dim(bmap
->dim
, isl_dim_all
);
1063 for (i
= 0, j
= bmap
->n_div
-1; i
< bmap
->n_eq
; ++i
) {
1064 while (j
>= 0 && isl_int_is_zero(bmap
->eq
[i
][1 + total
+ j
]))
1068 if (isl_seq_first_non_zero(bmap
->eq
[i
] + 1 + total
, j
) != -1)
1074 /* Normalize divs that appear in equalities.
1076 * In particular, we assume that bmap contains some equalities
1081 * and we want to replace the set of e_i by a minimal set and
1082 * such that the new e_i have a canonical representation in terms
1084 * If any of the equalities involves more than one divs, then
1085 * we currently simply bail out.
1087 * Let us first additionally assume that all equalities involve
1088 * a div. The equalities then express modulo constraints on the
1089 * remaining variables and we can use "parameter compression"
1090 * to find a minimal set of constraints. The result is a transformation
1092 * x = T(x') = x_0 + G x'
1094 * with G a lower-triangular matrix with all elements below the diagonal
1095 * non-negative and smaller than the diagonal element on the same row.
1096 * We first normalize x_0 by making the same property hold in the affine
1098 * The rows i of G with a 1 on the diagonal do not impose any modulo
1099 * constraint and simply express x_i = x'_i.
1100 * For each of the remaining rows i, we introduce a div and a corresponding
1101 * equality. In particular
1103 * g_ii e_j = x_i - g_i(x')
1105 * where each x'_k is replaced either by x_k (if g_kk = 1) or the
1106 * corresponding div (if g_kk != 1).
1108 * If there are any equalities not involving any div, then we
1109 * first apply a variable compression on the variables x:
1111 * x = C x'' x'' = C_2 x
1113 * and perform the above parameter compression on A C instead of on A.
1114 * The resulting compression is then of the form
1116 * x'' = T(x') = x_0 + G x'
1118 * and in constructing the new divs and the corresponding equalities,
1119 * we have to replace each x'', i.e., the x'_k with (g_kk = 1),
1120 * by the corresponding row from C_2.
1122 static struct isl_basic_map
*normalize_divs(
1123 struct isl_basic_map
*bmap
, int *progress
)
1130 struct isl_mat
*T
= NULL
;
1131 struct isl_mat
*C
= NULL
;
1132 struct isl_mat
*C2
= NULL
;
1135 int dropped
, needed
;
1140 if (bmap
->n_div
== 0)
1143 if (bmap
->n_eq
== 0)
1146 if (ISL_F_ISSET(bmap
, ISL_BASIC_MAP_NORMALIZED_DIVS
))
1149 total
= isl_space_dim(bmap
->dim
, isl_dim_all
);
1150 div_eq
= n_pure_div_eq(bmap
);
1154 if (div_eq
< bmap
->n_eq
) {
1155 B
= isl_mat_sub_alloc6(bmap
->ctx
, bmap
->eq
, div_eq
,
1156 bmap
->n_eq
- div_eq
, 0, 1 + total
);
1157 C
= isl_mat_variable_compression(B
, &C2
);
1160 if (C
->n_col
== 0) {
1161 bmap
= isl_basic_map_set_to_empty(bmap
);
1168 d
= isl_vec_alloc(bmap
->ctx
, div_eq
);
1171 for (i
= 0, j
= bmap
->n_div
-1; i
< div_eq
; ++i
) {
1172 while (j
>= 0 && isl_int_is_zero(bmap
->eq
[i
][1 + total
+ j
]))
1174 isl_int_set(d
->block
.data
[i
], bmap
->eq
[i
][1 + total
+ j
]);
1176 B
= isl_mat_sub_alloc6(bmap
->ctx
, bmap
->eq
, 0, div_eq
, 0, 1 + total
);
1179 B
= isl_mat_product(B
, C
);
1183 T
= isl_mat_parameter_compression(B
, d
);
1186 if (T
->n_col
== 0) {
1187 bmap
= isl_basic_map_set_to_empty(bmap
);
1193 for (i
= 0; i
< T
->n_row
- 1; ++i
) {
1194 isl_int_fdiv_q(v
, T
->row
[1 + i
][0], T
->row
[1 + i
][1 + i
]);
1195 if (isl_int_is_zero(v
))
1197 isl_mat_col_submul(T
, 0, v
, 1 + i
);
1200 pos
= isl_alloc_array(bmap
->ctx
, int, T
->n_row
);
1203 /* We have to be careful because dropping equalities may reorder them */
1205 for (j
= bmap
->n_div
- 1; j
>= 0; --j
) {
1206 for (i
= 0; i
< bmap
->n_eq
; ++i
)
1207 if (!isl_int_is_zero(bmap
->eq
[i
][1 + total
+ j
]))
1209 if (i
< bmap
->n_eq
) {
1210 bmap
= isl_basic_map_drop_div(bmap
, j
);
1211 isl_basic_map_drop_equality(bmap
, i
);
1217 for (i
= 1; i
< T
->n_row
; ++i
) {
1218 if (isl_int_is_one(T
->row
[i
][i
]))
1223 if (needed
> dropped
) {
1224 bmap
= isl_basic_map_extend_space(bmap
, isl_space_copy(bmap
->dim
),
1229 for (i
= 1; i
< T
->n_row
; ++i
) {
1230 if (isl_int_is_one(T
->row
[i
][i
]))
1232 k
= isl_basic_map_alloc_div(bmap
);
1233 pos
[i
] = 1 + total
+ k
;
1234 isl_seq_clr(bmap
->div
[k
] + 1, 1 + total
+ bmap
->n_div
);
1235 isl_int_set(bmap
->div
[k
][0], T
->row
[i
][i
]);
1237 isl_seq_cpy(bmap
->div
[k
] + 1, C2
->row
[i
], 1 + total
);
1239 isl_int_set_si(bmap
->div
[k
][1 + i
], 1);
1240 for (j
= 0; j
< i
; ++j
) {
1241 if (isl_int_is_zero(T
->row
[i
][j
]))
1243 if (pos
[j
] < T
->n_row
&& C2
)
1244 isl_seq_submul(bmap
->div
[k
] + 1, T
->row
[i
][j
],
1245 C2
->row
[pos
[j
]], 1 + total
);
1247 isl_int_neg(bmap
->div
[k
][1 + pos
[j
]],
1250 j
= isl_basic_map_alloc_equality(bmap
);
1251 isl_seq_neg(bmap
->eq
[j
], bmap
->div
[k
]+1, 1+total
+bmap
->n_div
);
1252 isl_int_set(bmap
->eq
[j
][pos
[i
]], bmap
->div
[k
][0]);
1261 ISL_F_SET(bmap
, ISL_BASIC_MAP_NORMALIZED_DIVS
);
1272 static struct isl_basic_map
*set_div_from_lower_bound(
1273 struct isl_basic_map
*bmap
, int div
, int ineq
)
1275 unsigned total
= 1 + isl_space_dim(bmap
->dim
, isl_dim_all
);
1277 isl_seq_neg(bmap
->div
[div
] + 1, bmap
->ineq
[ineq
], total
+ bmap
->n_div
);
1278 isl_int_set(bmap
->div
[div
][0], bmap
->ineq
[ineq
][total
+ div
]);
1279 isl_int_add(bmap
->div
[div
][1], bmap
->div
[div
][1], bmap
->div
[div
][0]);
1280 isl_int_sub_ui(bmap
->div
[div
][1], bmap
->div
[div
][1], 1);
1281 isl_int_set_si(bmap
->div
[div
][1 + total
+ div
], 0);
1286 /* Check whether it is ok to define a div based on an inequality.
1287 * To avoid the introduction of circular definitions of divs, we
1288 * do not allow such a definition if the resulting expression would refer to
1289 * any other undefined divs or if any known div is defined in
1290 * terms of the unknown div.
1292 static isl_bool
ok_to_set_div_from_bound(struct isl_basic_map
*bmap
,
1296 unsigned total
= 1 + isl_space_dim(bmap
->dim
, isl_dim_all
);
1298 /* Not defined in terms of unknown divs */
1299 for (j
= 0; j
< bmap
->n_div
; ++j
) {
1302 if (isl_int_is_zero(bmap
->ineq
[ineq
][total
+ j
]))
1304 if (isl_int_is_zero(bmap
->div
[j
][0]))
1305 return isl_bool_false
;
1308 /* No other div defined in terms of this one => avoid loops */
1309 for (j
= 0; j
< bmap
->n_div
; ++j
) {
1312 if (isl_int_is_zero(bmap
->div
[j
][0]))
1314 if (!isl_int_is_zero(bmap
->div
[j
][1 + total
+ div
]))
1315 return isl_bool_false
;
1318 return isl_bool_true
;
1321 /* Would an expression for div "div" based on inequality "ineq" of "bmap"
1322 * be a better expression than the current one?
1324 * If we do not have any expression yet, then any expression would be better.
1325 * Otherwise we check if the last variable involved in the inequality
1326 * (disregarding the div that it would define) is in an earlier position
1327 * than the last variable involved in the current div expression.
1329 static isl_bool
better_div_constraint(__isl_keep isl_basic_map
*bmap
,
1332 unsigned total
= 1 + isl_space_dim(bmap
->dim
, isl_dim_all
);
1336 if (isl_int_is_zero(bmap
->div
[div
][0]))
1337 return isl_bool_true
;
1339 if (isl_seq_last_non_zero(bmap
->ineq
[ineq
] + total
+ div
+ 1,
1340 bmap
->n_div
- (div
+ 1)) >= 0)
1341 return isl_bool_false
;
1343 last_ineq
= isl_seq_last_non_zero(bmap
->ineq
[ineq
], total
+ div
);
1344 last_div
= isl_seq_last_non_zero(bmap
->div
[div
] + 1,
1345 total
+ bmap
->n_div
);
1347 return last_ineq
< last_div
;
1350 /* Given two constraints "k" and "l" that are opposite to each other,
1351 * except for the constant term, check if we can use them
1352 * to obtain an expression for one of the hitherto unknown divs or
1353 * a "better" expression for a div for which we already have an expression.
1354 * "sum" is the sum of the constant terms of the constraints.
1355 * If this sum is strictly smaller than the coefficient of one
1356 * of the divs, then this pair can be used define the div.
1357 * To avoid the introduction of circular definitions of divs, we
1358 * do not use the pair if the resulting expression would refer to
1359 * any other undefined divs or if any known div is defined in
1360 * terms of the unknown div.
1362 static struct isl_basic_map
*check_for_div_constraints(
1363 struct isl_basic_map
*bmap
, int k
, int l
, isl_int sum
, int *progress
)
1366 unsigned total
= 1 + isl_space_dim(bmap
->dim
, isl_dim_all
);
1368 for (i
= 0; i
< bmap
->n_div
; ++i
) {
1371 if (isl_int_is_zero(bmap
->ineq
[k
][total
+ i
]))
1373 if (isl_int_abs_ge(sum
, bmap
->ineq
[k
][total
+ i
]))
1375 set_div
= better_div_constraint(bmap
, i
, k
);
1376 if (set_div
>= 0 && set_div
)
1377 set_div
= ok_to_set_div_from_bound(bmap
, i
, k
);
1379 return isl_basic_map_free(bmap
);
1382 if (isl_int_is_pos(bmap
->ineq
[k
][total
+ i
]))
1383 bmap
= set_div_from_lower_bound(bmap
, i
, k
);
1385 bmap
= set_div_from_lower_bound(bmap
, i
, l
);
1393 __isl_give isl_basic_map
*isl_basic_map_remove_duplicate_constraints(
1394 __isl_take isl_basic_map
*bmap
, int *progress
, int detect_divs
)
1396 struct isl_constraint_index ci
;
1398 unsigned total
= isl_basic_map_total_dim(bmap
);
1401 if (!bmap
|| bmap
->n_ineq
<= 1)
1404 if (create_constraint_index(&ci
, bmap
) < 0)
1407 h
= isl_seq_get_hash_bits(bmap
->ineq
[0] + 1, total
, ci
.bits
);
1408 ci
.index
[h
] = &bmap
->ineq
[0];
1409 for (k
= 1; k
< bmap
->n_ineq
; ++k
) {
1410 h
= hash_index(&ci
, bmap
, k
);
1412 ci
.index
[h
] = &bmap
->ineq
[k
];
1417 l
= ci
.index
[h
] - &bmap
->ineq
[0];
1418 if (isl_int_lt(bmap
->ineq
[k
][0], bmap
->ineq
[l
][0]))
1419 swap_inequality(bmap
, k
, l
);
1420 isl_basic_map_drop_inequality(bmap
, k
);
1424 for (k
= 0; k
< bmap
->n_ineq
-1; ++k
) {
1425 isl_seq_neg(bmap
->ineq
[k
]+1, bmap
->ineq
[k
]+1, total
);
1426 h
= hash_index(&ci
, bmap
, k
);
1427 isl_seq_neg(bmap
->ineq
[k
]+1, bmap
->ineq
[k
]+1, total
);
1430 l
= ci
.index
[h
] - &bmap
->ineq
[0];
1431 isl_int_add(sum
, bmap
->ineq
[k
][0], bmap
->ineq
[l
][0]);
1432 if (isl_int_is_pos(sum
)) {
1434 bmap
= check_for_div_constraints(bmap
, k
, l
,
1438 if (isl_int_is_zero(sum
)) {
1439 /* We need to break out of the loop after these
1440 * changes since the contents of the hash
1441 * will no longer be valid.
1442 * Plus, we probably we want to regauss first.
1446 isl_basic_map_drop_inequality(bmap
, l
);
1447 isl_basic_map_inequality_to_equality(bmap
, k
);
1449 bmap
= isl_basic_map_set_to_empty(bmap
);
1454 constraint_index_free(&ci
);
1458 /* Detect all pairs of inequalities that form an equality.
1460 * isl_basic_map_remove_duplicate_constraints detects at most one such pair.
1461 * Call it repeatedly while it is making progress.
1463 __isl_give isl_basic_map
*isl_basic_map_detect_inequality_pairs(
1464 __isl_take isl_basic_map
*bmap
, int *progress
)
1470 bmap
= isl_basic_map_remove_duplicate_constraints(bmap
,
1472 if (progress
&& duplicate
)
1474 } while (duplicate
);
1479 /* Eliminate knowns divs from constraints where they appear with
1480 * a (positive or negative) unit coefficient.
1484 * floor(e/m) + f >= 0
1492 * -floor(e/m) + f >= 0
1496 * -e + m f + m - 1 >= 0
1498 * The first conversion is valid because floor(e/m) >= -f is equivalent
1499 * to e/m >= -f because -f is an integral expression.
1500 * The second conversion follows from the fact that
1502 * -floor(e/m) = ceil(-e/m) = floor((-e + m - 1)/m)
1505 * Note that one of the div constraints may have been eliminated
1506 * due to being redundant with respect to the constraint that is
1507 * being modified by this function. The modified constraint may
1508 * no longer imply this div constraint, so we add it back to make
1509 * sure we do not lose any information.
1511 * We skip integral divs, i.e., those with denominator 1, as we would
1512 * risk eliminating the div from the div constraints. We do not need
1513 * to handle those divs here anyway since the div constraints will turn
1514 * out to form an equality and this equality can then be used to eliminate
1515 * the div from all constraints.
1517 static __isl_give isl_basic_map
*eliminate_unit_divs(
1518 __isl_take isl_basic_map
*bmap
, int *progress
)
1527 ctx
= isl_basic_map_get_ctx(bmap
);
1528 total
= 1 + isl_space_dim(bmap
->dim
, isl_dim_all
);
1530 for (i
= 0; i
< bmap
->n_div
; ++i
) {
1531 if (isl_int_is_zero(bmap
->div
[i
][0]))
1533 if (isl_int_is_one(bmap
->div
[i
][0]))
1535 for (j
= 0; j
< bmap
->n_ineq
; ++j
) {
1538 if (!isl_int_is_one(bmap
->ineq
[j
][total
+ i
]) &&
1539 !isl_int_is_negone(bmap
->ineq
[j
][total
+ i
]))
1544 s
= isl_int_sgn(bmap
->ineq
[j
][total
+ i
]);
1545 isl_int_set_si(bmap
->ineq
[j
][total
+ i
], 0);
1547 isl_seq_combine(bmap
->ineq
[j
],
1548 ctx
->negone
, bmap
->div
[i
] + 1,
1549 bmap
->div
[i
][0], bmap
->ineq
[j
],
1550 total
+ bmap
->n_div
);
1552 isl_seq_combine(bmap
->ineq
[j
],
1553 ctx
->one
, bmap
->div
[i
] + 1,
1554 bmap
->div
[i
][0], bmap
->ineq
[j
],
1555 total
+ bmap
->n_div
);
1557 isl_int_add(bmap
->ineq
[j
][0],
1558 bmap
->ineq
[j
][0], bmap
->div
[i
][0]);
1559 isl_int_sub_ui(bmap
->ineq
[j
][0],
1560 bmap
->ineq
[j
][0], 1);
1563 bmap
= isl_basic_map_extend_constraints(bmap
, 0, 1);
1564 if (isl_basic_map_add_div_constraint(bmap
, i
, s
) < 0)
1565 return isl_basic_map_free(bmap
);
1572 struct isl_basic_map
*isl_basic_map_simplify(struct isl_basic_map
*bmap
)
1581 empty
= isl_basic_map_plain_is_empty(bmap
);
1583 return isl_basic_map_free(bmap
);
1586 bmap
= isl_basic_map_normalize_constraints(bmap
);
1587 bmap
= reduce_div_coefficients(bmap
);
1588 bmap
= normalize_div_expressions(bmap
);
1589 bmap
= remove_duplicate_divs(bmap
, &progress
);
1590 bmap
= eliminate_unit_divs(bmap
, &progress
);
1591 bmap
= eliminate_divs_eq(bmap
, &progress
);
1592 bmap
= eliminate_divs_ineq(bmap
, &progress
);
1593 bmap
= isl_basic_map_gauss(bmap
, &progress
);
1594 /* requires equalities in normal form */
1595 bmap
= normalize_divs(bmap
, &progress
);
1596 bmap
= isl_basic_map_remove_duplicate_constraints(bmap
,
1598 if (bmap
&& progress
)
1599 ISL_F_CLR(bmap
, ISL_BASIC_MAP_REDUCED_COEFFICIENTS
);
1604 struct isl_basic_set
*isl_basic_set_simplify(struct isl_basic_set
*bset
)
1606 return bset_from_bmap(isl_basic_map_simplify(bset_to_bmap(bset
)));
1610 isl_bool
isl_basic_map_is_div_constraint(__isl_keep isl_basic_map
*bmap
,
1611 isl_int
*constraint
, unsigned div
)
1616 return isl_bool_error
;
1618 pos
= 1 + isl_space_dim(bmap
->dim
, isl_dim_all
) + div
;
1620 if (isl_int_eq(constraint
[pos
], bmap
->div
[div
][0])) {
1622 isl_int_sub(bmap
->div
[div
][1],
1623 bmap
->div
[div
][1], bmap
->div
[div
][0]);
1624 isl_int_add_ui(bmap
->div
[div
][1], bmap
->div
[div
][1], 1);
1625 neg
= isl_seq_is_neg(constraint
, bmap
->div
[div
]+1, pos
);
1626 isl_int_sub_ui(bmap
->div
[div
][1], bmap
->div
[div
][1], 1);
1627 isl_int_add(bmap
->div
[div
][1],
1628 bmap
->div
[div
][1], bmap
->div
[div
][0]);
1630 return isl_bool_false
;
1631 if (isl_seq_first_non_zero(constraint
+pos
+1,
1632 bmap
->n_div
-div
-1) != -1)
1633 return isl_bool_false
;
1634 } else if (isl_int_abs_eq(constraint
[pos
], bmap
->div
[div
][0])) {
1635 if (!isl_seq_eq(constraint
, bmap
->div
[div
]+1, pos
))
1636 return isl_bool_false
;
1637 if (isl_seq_first_non_zero(constraint
+pos
+1,
1638 bmap
->n_div
-div
-1) != -1)
1639 return isl_bool_false
;
1641 return isl_bool_false
;
1643 return isl_bool_true
;
1646 isl_bool
isl_basic_set_is_div_constraint(__isl_keep isl_basic_set
*bset
,
1647 isl_int
*constraint
, unsigned div
)
1649 return isl_basic_map_is_div_constraint(bset
, constraint
, div
);
1653 /* If the only constraints a div d=floor(f/m)
1654 * appears in are its two defining constraints
1657 * -(f - (m - 1)) + m d >= 0
1659 * then it can safely be removed.
1661 static isl_bool
div_is_redundant(struct isl_basic_map
*bmap
, int div
)
1664 unsigned pos
= 1 + isl_space_dim(bmap
->dim
, isl_dim_all
) + div
;
1666 for (i
= 0; i
< bmap
->n_eq
; ++i
)
1667 if (!isl_int_is_zero(bmap
->eq
[i
][pos
]))
1668 return isl_bool_false
;
1670 for (i
= 0; i
< bmap
->n_ineq
; ++i
) {
1673 if (isl_int_is_zero(bmap
->ineq
[i
][pos
]))
1675 red
= isl_basic_map_is_div_constraint(bmap
, bmap
->ineq
[i
], div
);
1676 if (red
< 0 || !red
)
1680 for (i
= 0; i
< bmap
->n_div
; ++i
) {
1681 if (isl_int_is_zero(bmap
->div
[i
][0]))
1683 if (!isl_int_is_zero(bmap
->div
[i
][1+pos
]))
1684 return isl_bool_false
;
1687 return isl_bool_true
;
1691 * Remove divs that don't occur in any of the constraints or other divs.
1692 * These can arise when dropping constraints from a basic map or
1693 * when the divs of a basic map have been temporarily aligned
1694 * with the divs of another basic map.
1696 static struct isl_basic_map
*remove_redundant_divs(struct isl_basic_map
*bmap
)
1703 for (i
= bmap
->n_div
-1; i
>= 0; --i
) {
1706 redundant
= div_is_redundant(bmap
, i
);
1708 return isl_basic_map_free(bmap
);
1711 bmap
= isl_basic_map_drop_div(bmap
, i
);
1716 /* Mark "bmap" as final, without checking for obviously redundant
1717 * integer divisions. This function should be used when "bmap"
1718 * is known not to involve any such integer divisions.
1720 __isl_give isl_basic_map
*isl_basic_map_mark_final(
1721 __isl_take isl_basic_map
*bmap
)
1725 ISL_F_SET(bmap
, ISL_BASIC_SET_FINAL
);
1729 /* Mark "bmap" as final, after removing obviously redundant integer divisions.
1731 struct isl_basic_map
*isl_basic_map_finalize(struct isl_basic_map
*bmap
)
1733 bmap
= remove_redundant_divs(bmap
);
1734 bmap
= isl_basic_map_mark_final(bmap
);
1738 struct isl_basic_set
*isl_basic_set_finalize(struct isl_basic_set
*bset
)
1740 return bset_from_bmap(isl_basic_map_finalize(bset_to_bmap(bset
)));
1743 /* Remove definition of any div that is defined in terms of the given variable.
1744 * The div itself is not removed. Functions such as
1745 * eliminate_divs_ineq depend on the other divs remaining in place.
1747 static struct isl_basic_map
*remove_dependent_vars(struct isl_basic_map
*bmap
,
1755 for (i
= 0; i
< bmap
->n_div
; ++i
) {
1756 if (isl_int_is_zero(bmap
->div
[i
][0]))
1758 if (isl_int_is_zero(bmap
->div
[i
][1+1+pos
]))
1760 bmap
= isl_basic_map_mark_div_unknown(bmap
, i
);
1767 /* Eliminate the specified variables from the constraints using
1768 * Fourier-Motzkin. The variables themselves are not removed.
1770 struct isl_basic_map
*isl_basic_map_eliminate_vars(
1771 struct isl_basic_map
*bmap
, unsigned pos
, unsigned n
)
1782 total
= isl_basic_map_total_dim(bmap
);
1784 bmap
= isl_basic_map_cow(bmap
);
1785 for (d
= pos
+ n
- 1; d
>= 0 && d
>= pos
; --d
)
1786 bmap
= remove_dependent_vars(bmap
, d
);
1790 for (d
= pos
+ n
- 1;
1791 d
>= 0 && d
>= total
- bmap
->n_div
&& d
>= pos
; --d
)
1792 isl_seq_clr(bmap
->div
[d
-(total
-bmap
->n_div
)], 2+total
);
1793 for (d
= pos
+ n
- 1; d
>= 0 && d
>= pos
; --d
) {
1794 int n_lower
, n_upper
;
1797 for (i
= 0; i
< bmap
->n_eq
; ++i
) {
1798 if (isl_int_is_zero(bmap
->eq
[i
][1+d
]))
1800 eliminate_var_using_equality(bmap
, d
, bmap
->eq
[i
], 0, NULL
);
1801 isl_basic_map_drop_equality(bmap
, i
);
1809 for (i
= 0; i
< bmap
->n_ineq
; ++i
) {
1810 if (isl_int_is_pos(bmap
->ineq
[i
][1+d
]))
1812 else if (isl_int_is_neg(bmap
->ineq
[i
][1+d
]))
1815 bmap
= isl_basic_map_extend_constraints(bmap
,
1816 0, n_lower
* n_upper
);
1819 for (i
= bmap
->n_ineq
- 1; i
>= 0; --i
) {
1821 if (isl_int_is_zero(bmap
->ineq
[i
][1+d
]))
1824 for (j
= 0; j
< i
; ++j
) {
1825 if (isl_int_is_zero(bmap
->ineq
[j
][1+d
]))
1828 if (isl_int_sgn(bmap
->ineq
[i
][1+d
]) ==
1829 isl_int_sgn(bmap
->ineq
[j
][1+d
]))
1831 k
= isl_basic_map_alloc_inequality(bmap
);
1834 isl_seq_cpy(bmap
->ineq
[k
], bmap
->ineq
[i
],
1836 isl_seq_elim(bmap
->ineq
[k
], bmap
->ineq
[j
],
1837 1+d
, 1+total
, NULL
);
1839 isl_basic_map_drop_inequality(bmap
, i
);
1842 if (n_lower
> 0 && n_upper
> 0) {
1843 bmap
= isl_basic_map_normalize_constraints(bmap
);
1844 bmap
= isl_basic_map_remove_duplicate_constraints(bmap
,
1846 bmap
= isl_basic_map_gauss(bmap
, NULL
);
1847 bmap
= isl_basic_map_remove_redundancies(bmap
);
1851 if (ISL_F_ISSET(bmap
, ISL_BASIC_MAP_EMPTY
))
1855 ISL_F_CLR(bmap
, ISL_BASIC_MAP_NORMALIZED
);
1857 bmap
= isl_basic_map_gauss(bmap
, NULL
);
1860 isl_basic_map_free(bmap
);
1864 struct isl_basic_set
*isl_basic_set_eliminate_vars(
1865 struct isl_basic_set
*bset
, unsigned pos
, unsigned n
)
1867 return bset_from_bmap(isl_basic_map_eliminate_vars(bset_to_bmap(bset
),
1871 /* Eliminate the specified n dimensions starting at first from the
1872 * constraints, without removing the dimensions from the space.
1873 * If the set is rational, the dimensions are eliminated using Fourier-Motzkin.
1874 * Otherwise, they are projected out and the original space is restored.
1876 __isl_give isl_basic_map
*isl_basic_map_eliminate(
1877 __isl_take isl_basic_map
*bmap
,
1878 enum isl_dim_type type
, unsigned first
, unsigned n
)
1887 if (first
+ n
> isl_basic_map_dim(bmap
, type
) || first
+ n
< first
)
1888 isl_die(bmap
->ctx
, isl_error_invalid
,
1889 "index out of bounds", goto error
);
1891 if (ISL_F_ISSET(bmap
, ISL_BASIC_MAP_RATIONAL
)) {
1892 first
+= isl_basic_map_offset(bmap
, type
) - 1;
1893 bmap
= isl_basic_map_eliminate_vars(bmap
, first
, n
);
1894 return isl_basic_map_finalize(bmap
);
1897 space
= isl_basic_map_get_space(bmap
);
1898 bmap
= isl_basic_map_project_out(bmap
, type
, first
, n
);
1899 bmap
= isl_basic_map_insert_dims(bmap
, type
, first
, n
);
1900 bmap
= isl_basic_map_reset_space(bmap
, space
);
1903 isl_basic_map_free(bmap
);
1907 __isl_give isl_basic_set
*isl_basic_set_eliminate(
1908 __isl_take isl_basic_set
*bset
,
1909 enum isl_dim_type type
, unsigned first
, unsigned n
)
1911 return isl_basic_map_eliminate(bset
, type
, first
, n
);
1914 /* Remove all constraints from "bmap" that reference any unknown local
1915 * variables (directly or indirectly).
1917 * Dropping all constraints on a local variable will make it redundant,
1918 * so it will get removed implicitly by
1919 * isl_basic_map_drop_constraints_involving_dims. Some other local
1920 * variables may also end up becoming redundant if they only appear
1921 * in constraints together with the unknown local variable.
1922 * Therefore, start over after calling
1923 * isl_basic_map_drop_constraints_involving_dims.
1925 __isl_give isl_basic_map
*isl_basic_map_drop_constraint_involving_unknown_divs(
1926 __isl_take isl_basic_map
*bmap
)
1929 int i
, n_div
, o_div
;
1931 known
= isl_basic_map_divs_known(bmap
);
1933 return isl_basic_map_free(bmap
);
1937 n_div
= isl_basic_map_dim(bmap
, isl_dim_div
);
1938 o_div
= isl_basic_map_offset(bmap
, isl_dim_div
) - 1;
1940 for (i
= 0; i
< n_div
; ++i
) {
1941 known
= isl_basic_map_div_is_known(bmap
, i
);
1943 return isl_basic_map_free(bmap
);
1946 bmap
= remove_dependent_vars(bmap
, o_div
+ i
);
1947 bmap
= isl_basic_map_drop_constraints_involving_dims(bmap
,
1951 n_div
= isl_basic_map_dim(bmap
, isl_dim_div
);
1958 /* Remove all constraints from "map" that reference any unknown local
1959 * variables (directly or indirectly).
1961 * Since constraints may get dropped from the basic maps,
1962 * they may no longer be disjoint from each other.
1964 __isl_give isl_map
*isl_map_drop_constraint_involving_unknown_divs(
1965 __isl_take isl_map
*map
)
1970 known
= isl_map_divs_known(map
);
1972 return isl_map_free(map
);
1976 map
= isl_map_cow(map
);
1980 for (i
= 0; i
< map
->n
; ++i
) {
1982 isl_basic_map_drop_constraint_involving_unknown_divs(
1985 return isl_map_free(map
);
1989 ISL_F_CLR(map
, ISL_MAP_DISJOINT
);
1994 /* Don't assume equalities are in order, because align_divs
1995 * may have changed the order of the divs.
1997 static void compute_elimination_index(struct isl_basic_map
*bmap
, int *elim
)
2002 total
= isl_space_dim(bmap
->dim
, isl_dim_all
);
2003 for (d
= 0; d
< total
; ++d
)
2005 for (i
= 0; i
< bmap
->n_eq
; ++i
) {
2006 for (d
= total
- 1; d
>= 0; --d
) {
2007 if (isl_int_is_zero(bmap
->eq
[i
][1+d
]))
2015 static void set_compute_elimination_index(struct isl_basic_set
*bset
, int *elim
)
2017 compute_elimination_index(bset_to_bmap(bset
), elim
);
2020 static int reduced_using_equalities(isl_int
*dst
, isl_int
*src
,
2021 struct isl_basic_map
*bmap
, int *elim
)
2027 total
= isl_space_dim(bmap
->dim
, isl_dim_all
);
2028 for (d
= total
- 1; d
>= 0; --d
) {
2029 if (isl_int_is_zero(src
[1+d
]))
2034 isl_seq_cpy(dst
, src
, 1 + total
);
2037 isl_seq_elim(dst
, bmap
->eq
[elim
[d
]], 1 + d
, 1 + total
, NULL
);
2042 static int set_reduced_using_equalities(isl_int
*dst
, isl_int
*src
,
2043 struct isl_basic_set
*bset
, int *elim
)
2045 return reduced_using_equalities(dst
, src
,
2046 bset_to_bmap(bset
), elim
);
2049 static struct isl_basic_set
*isl_basic_set_reduce_using_equalities(
2050 struct isl_basic_set
*bset
, struct isl_basic_set
*context
)
2055 if (!bset
|| !context
)
2058 if (context
->n_eq
== 0) {
2059 isl_basic_set_free(context
);
2063 bset
= isl_basic_set_cow(bset
);
2067 elim
= isl_alloc_array(bset
->ctx
, int, isl_basic_set_n_dim(bset
));
2070 set_compute_elimination_index(context
, elim
);
2071 for (i
= 0; i
< bset
->n_eq
; ++i
)
2072 set_reduced_using_equalities(bset
->eq
[i
], bset
->eq
[i
],
2074 for (i
= 0; i
< bset
->n_ineq
; ++i
)
2075 set_reduced_using_equalities(bset
->ineq
[i
], bset
->ineq
[i
],
2077 isl_basic_set_free(context
);
2079 bset
= isl_basic_set_simplify(bset
);
2080 bset
= isl_basic_set_finalize(bset
);
2083 isl_basic_set_free(bset
);
2084 isl_basic_set_free(context
);
2088 /* For each inequality in "ineq" that is a shifted (more relaxed)
2089 * copy of an inequality in "context", mark the corresponding entry
2091 * If an inequality only has a non-negative constant term, then
2094 static isl_stat
mark_shifted_constraints(__isl_keep isl_mat
*ineq
,
2095 __isl_keep isl_basic_set
*context
, int *row
)
2097 struct isl_constraint_index ci
;
2102 if (!ineq
|| !context
)
2103 return isl_stat_error
;
2104 if (context
->n_ineq
== 0)
2106 if (setup_constraint_index(&ci
, context
) < 0)
2107 return isl_stat_error
;
2109 n_ineq
= isl_mat_rows(ineq
);
2110 total
= isl_mat_cols(ineq
) - 1;
2111 for (k
= 0; k
< n_ineq
; ++k
) {
2115 l
= isl_seq_first_non_zero(ineq
->row
[k
] + 1, total
);
2116 if (l
< 0 && isl_int_is_nonneg(ineq
->row
[k
][0])) {
2120 redundant
= constraint_index_is_redundant(&ci
, ineq
->row
[k
]);
2127 constraint_index_free(&ci
);
2130 constraint_index_free(&ci
);
2131 return isl_stat_error
;
2134 static struct isl_basic_set
*remove_shifted_constraints(
2135 struct isl_basic_set
*bset
, struct isl_basic_set
*context
)
2137 struct isl_constraint_index ci
;
2140 if (!bset
|| !context
)
2143 if (context
->n_ineq
== 0)
2145 if (setup_constraint_index(&ci
, context
) < 0)
2148 for (k
= 0; k
< bset
->n_ineq
; ++k
) {
2151 redundant
= constraint_index_is_redundant(&ci
, bset
->ineq
[k
]);
2156 bset
= isl_basic_set_cow(bset
);
2159 isl_basic_set_drop_inequality(bset
, k
);
2162 constraint_index_free(&ci
);
2165 constraint_index_free(&ci
);
2169 /* Remove constraints from "bmap" that are identical to constraints
2170 * in "context" or that are more relaxed (greater constant term).
2172 * We perform the test for shifted copies on the pure constraints
2173 * in remove_shifted_constraints.
2175 static __isl_give isl_basic_map
*isl_basic_map_remove_shifted_constraints(
2176 __isl_take isl_basic_map
*bmap
, __isl_take isl_basic_map
*context
)
2178 isl_basic_set
*bset
, *bset_context
;
2180 if (!bmap
|| !context
)
2183 if (bmap
->n_ineq
== 0 || context
->n_ineq
== 0) {
2184 isl_basic_map_free(context
);
2188 context
= isl_basic_map_align_divs(context
, bmap
);
2189 bmap
= isl_basic_map_align_divs(bmap
, context
);
2191 bset
= isl_basic_map_underlying_set(isl_basic_map_copy(bmap
));
2192 bset_context
= isl_basic_map_underlying_set(context
);
2193 bset
= remove_shifted_constraints(bset
, bset_context
);
2194 isl_basic_set_free(bset_context
);
2196 bmap
= isl_basic_map_overlying_set(bset
, bmap
);
2200 isl_basic_map_free(bmap
);
2201 isl_basic_map_free(context
);
2205 /* Does the (linear part of a) constraint "c" involve any of the "len"
2206 * "relevant" dimensions?
2208 static int is_related(isl_int
*c
, int len
, int *relevant
)
2212 for (i
= 0; i
< len
; ++i
) {
2215 if (!isl_int_is_zero(c
[i
]))
2222 /* Drop constraints from "bmap" that do not involve any of
2223 * the dimensions marked "relevant".
2225 static __isl_give isl_basic_map
*drop_unrelated_constraints(
2226 __isl_take isl_basic_map
*bmap
, int *relevant
)
2230 dim
= isl_basic_map_dim(bmap
, isl_dim_all
);
2231 for (i
= 0; i
< dim
; ++i
)
2237 for (i
= bmap
->n_eq
- 1; i
>= 0; --i
)
2238 if (!is_related(bmap
->eq
[i
] + 1, dim
, relevant
)) {
2239 bmap
= isl_basic_map_cow(bmap
);
2240 if (isl_basic_map_drop_equality(bmap
, i
) < 0)
2241 return isl_basic_map_free(bmap
);
2244 for (i
= bmap
->n_ineq
- 1; i
>= 0; --i
)
2245 if (!is_related(bmap
->ineq
[i
] + 1, dim
, relevant
)) {
2246 bmap
= isl_basic_map_cow(bmap
);
2247 if (isl_basic_map_drop_inequality(bmap
, i
) < 0)
2248 return isl_basic_map_free(bmap
);
2254 /* Update the groups in "group" based on the (linear part of a) constraint "c".
2256 * In particular, for any variable involved in the constraint,
2257 * find the actual group id from before and replace the group
2258 * of the corresponding variable by the minimal group of all
2259 * the variables involved in the constraint considered so far
2260 * (if this minimum is smaller) or replace the minimum by this group
2261 * (if the minimum is larger).
2263 * At the end, all the variables in "c" will (indirectly) point
2264 * to the minimal of the groups that they referred to originally.
2266 static void update_groups(int dim
, int *group
, isl_int
*c
)
2271 for (j
= 0; j
< dim
; ++j
) {
2272 if (isl_int_is_zero(c
[j
]))
2274 while (group
[j
] >= 0 && group
[group
[j
]] != group
[j
])
2275 group
[j
] = group
[group
[j
]];
2276 if (group
[j
] == min
)
2278 if (group
[j
] < min
) {
2279 if (min
>= 0 && min
< dim
)
2280 group
[min
] = group
[j
];
2283 group
[group
[j
]] = min
;
2287 /* Allocate an array of groups of variables, one for each variable
2288 * in "context", initialized to zero.
2290 static int *alloc_groups(__isl_keep isl_basic_set
*context
)
2295 dim
= isl_basic_set_dim(context
, isl_dim_set
);
2296 ctx
= isl_basic_set_get_ctx(context
);
2297 return isl_calloc_array(ctx
, int, dim
);
2300 /* Drop constraints from "bmap" that only involve variables that are
2301 * not related to any of the variables marked with a "-1" in "group".
2303 * We construct groups of variables that collect variables that
2304 * (indirectly) appear in some common constraint of "bmap".
2305 * Each group is identified by the first variable in the group,
2306 * except for the special group of variables that was already identified
2307 * in the input as -1 (or are related to those variables).
2308 * If group[i] is equal to i (or -1), then the group of i is i (or -1),
2309 * otherwise the group of i is the group of group[i].
2311 * We first initialize groups for the remaining variables.
2312 * Then we iterate over the constraints of "bmap" and update the
2313 * group of the variables in the constraint by the smallest group.
2314 * Finally, we resolve indirect references to groups by running over
2317 * After computing the groups, we drop constraints that do not involve
2318 * any variables in the -1 group.
2320 __isl_give isl_basic_map
*isl_basic_map_drop_unrelated_constraints(
2321 __isl_take isl_basic_map
*bmap
, __isl_take
int *group
)
2330 dim
= isl_basic_map_dim(bmap
, isl_dim_all
);
2333 for (i
= 0; i
< dim
; ++i
)
2335 last
= group
[i
] = i
;
2341 for (i
= 0; i
< bmap
->n_eq
; ++i
)
2342 update_groups(dim
, group
, bmap
->eq
[i
] + 1);
2343 for (i
= 0; i
< bmap
->n_ineq
; ++i
)
2344 update_groups(dim
, group
, bmap
->ineq
[i
] + 1);
2346 for (i
= 0; i
< dim
; ++i
)
2348 group
[i
] = group
[group
[i
]];
2350 for (i
= 0; i
< dim
; ++i
)
2351 group
[i
] = group
[i
] == -1;
2353 bmap
= drop_unrelated_constraints(bmap
, group
);
2359 /* Drop constraints from "context" that are irrelevant for computing
2360 * the gist of "bset".
2362 * In particular, drop constraints in variables that are not related
2363 * to any of the variables involved in the constraints of "bset"
2364 * in the sense that there is no sequence of constraints that connects them.
2366 * We first mark all variables that appear in "bset" as belonging
2367 * to a "-1" group and then continue with group_and_drop_irrelevant_constraints.
2369 static __isl_give isl_basic_set
*drop_irrelevant_constraints(
2370 __isl_take isl_basic_set
*context
, __isl_keep isl_basic_set
*bset
)
2376 if (!context
|| !bset
)
2377 return isl_basic_set_free(context
);
2379 group
= alloc_groups(context
);
2382 return isl_basic_set_free(context
);
2384 dim
= isl_basic_set_dim(bset
, isl_dim_set
);
2385 for (i
= 0; i
< dim
; ++i
) {
2386 for (j
= 0; j
< bset
->n_eq
; ++j
)
2387 if (!isl_int_is_zero(bset
->eq
[j
][1 + i
]))
2389 if (j
< bset
->n_eq
) {
2393 for (j
= 0; j
< bset
->n_ineq
; ++j
)
2394 if (!isl_int_is_zero(bset
->ineq
[j
][1 + i
]))
2396 if (j
< bset
->n_ineq
)
2400 return isl_basic_map_drop_unrelated_constraints(context
, group
);
2403 /* Drop constraints from "context" that are irrelevant for computing
2404 * the gist of the inequalities "ineq".
2405 * Inequalities in "ineq" for which the corresponding element of row
2406 * is set to -1 have already been marked for removal and should be ignored.
2408 * In particular, drop constraints in variables that are not related
2409 * to any of the variables involved in "ineq"
2410 * in the sense that there is no sequence of constraints that connects them.
2412 * We first mark all variables that appear in "bset" as belonging
2413 * to a "-1" group and then continue with group_and_drop_irrelevant_constraints.
2415 static __isl_give isl_basic_set
*drop_irrelevant_constraints_marked(
2416 __isl_take isl_basic_set
*context
, __isl_keep isl_mat
*ineq
, int *row
)
2422 if (!context
|| !ineq
)
2423 return isl_basic_set_free(context
);
2425 group
= alloc_groups(context
);
2428 return isl_basic_set_free(context
);
2430 dim
= isl_basic_set_dim(context
, isl_dim_set
);
2431 n
= isl_mat_rows(ineq
);
2432 for (i
= 0; i
< dim
; ++i
) {
2433 for (j
= 0; j
< n
; ++j
) {
2436 if (!isl_int_is_zero(ineq
->row
[j
][1 + i
]))
2443 return isl_basic_map_drop_unrelated_constraints(context
, group
);
2446 /* Do all "n" entries of "row" contain a negative value?
2448 static int all_neg(int *row
, int n
)
2452 for (i
= 0; i
< n
; ++i
)
2459 /* Update the inequalities in "bset" based on the information in "row"
2462 * In particular, the array "row" contains either -1, meaning that
2463 * the corresponding inequality of "bset" is redundant, or the index
2464 * of an inequality in "tab".
2466 * If the row entry is -1, then drop the inequality.
2467 * Otherwise, if the constraint is marked redundant in the tableau,
2468 * then drop the inequality. Similarly, if it is marked as an equality
2469 * in the tableau, then turn the inequality into an equality and
2470 * perform Gaussian elimination.
2472 static __isl_give isl_basic_set
*update_ineq(__isl_take isl_basic_set
*bset
,
2473 __isl_keep
int *row
, struct isl_tab
*tab
)
2478 int found_equality
= 0;
2482 if (tab
&& tab
->empty
)
2483 return isl_basic_set_set_to_empty(bset
);
2485 n_ineq
= bset
->n_ineq
;
2486 for (i
= n_ineq
- 1; i
>= 0; --i
) {
2488 if (isl_basic_set_drop_inequality(bset
, i
) < 0)
2489 return isl_basic_set_free(bset
);
2495 if (isl_tab_is_equality(tab
, n_eq
+ row
[i
])) {
2496 isl_basic_map_inequality_to_equality(bset
, i
);
2498 } else if (isl_tab_is_redundant(tab
, n_eq
+ row
[i
])) {
2499 if (isl_basic_set_drop_inequality(bset
, i
) < 0)
2500 return isl_basic_set_free(bset
);
2505 bset
= isl_basic_set_gauss(bset
, NULL
);
2506 bset
= isl_basic_set_finalize(bset
);
2510 /* Update the inequalities in "bset" based on the information in "row"
2511 * and "tab" and free all arguments (other than "bset").
2513 static __isl_give isl_basic_set
*update_ineq_free(
2514 __isl_take isl_basic_set
*bset
, __isl_take isl_mat
*ineq
,
2515 __isl_take isl_basic_set
*context
, __isl_take
int *row
,
2516 struct isl_tab
*tab
)
2519 isl_basic_set_free(context
);
2521 bset
= update_ineq(bset
, row
, tab
);
2528 /* Remove all information from bset that is redundant in the context
2530 * "ineq" contains the (possibly transformed) inequalities of "bset",
2531 * in the same order.
2532 * The (explicit) equalities of "bset" are assumed to have been taken
2533 * into account by the transformation such that only the inequalities
2535 * "context" is assumed not to be empty.
2537 * "row" keeps track of the constraint index of a "bset" inequality in "tab".
2538 * A value of -1 means that the inequality is obviously redundant and may
2539 * not even appear in "tab".
2541 * We first mark the inequalities of "bset"
2542 * that are obviously redundant with respect to some inequality in "context".
2543 * Then we remove those constraints from "context" that have become
2544 * irrelevant for computing the gist of "bset".
2545 * Note that this removal of constraints cannot be replaced by
2546 * a factorization because factors in "bset" may still be connected
2547 * to each other through constraints in "context".
2549 * If there are any inequalities left, we construct a tableau for
2550 * the context and then add the inequalities of "bset".
2551 * Before adding these inequalities, we freeze all constraints such that
2552 * they won't be considered redundant in terms of the constraints of "bset".
2553 * Then we detect all redundant constraints (among the
2554 * constraints that weren't frozen), first by checking for redundancy in the
2555 * the tableau and then by checking if replacing a constraint by its negation
2556 * would lead to an empty set. This last step is fairly expensive
2557 * and could be optimized by more reuse of the tableau.
2558 * Finally, we update bset according to the results.
2560 static __isl_give isl_basic_set
*uset_gist_full(__isl_take isl_basic_set
*bset
,
2561 __isl_take isl_mat
*ineq
, __isl_take isl_basic_set
*context
)
2566 isl_basic_set
*combined
= NULL
;
2567 struct isl_tab
*tab
= NULL
;
2568 unsigned n_eq
, context_ineq
;
2570 if (!bset
|| !ineq
|| !context
)
2573 if (bset
->n_ineq
== 0 || isl_basic_set_plain_is_universe(context
)) {
2574 isl_basic_set_free(context
);
2579 ctx
= isl_basic_set_get_ctx(context
);
2580 row
= isl_calloc_array(ctx
, int, bset
->n_ineq
);
2584 if (mark_shifted_constraints(ineq
, context
, row
) < 0)
2586 if (all_neg(row
, bset
->n_ineq
))
2587 return update_ineq_free(bset
, ineq
, context
, row
, NULL
);
2589 context
= drop_irrelevant_constraints_marked(context
, ineq
, row
);
2592 if (isl_basic_set_plain_is_universe(context
))
2593 return update_ineq_free(bset
, ineq
, context
, row
, NULL
);
2595 n_eq
= context
->n_eq
;
2596 context_ineq
= context
->n_ineq
;
2597 combined
= isl_basic_set_cow(isl_basic_set_copy(context
));
2598 combined
= isl_basic_set_extend_constraints(combined
, 0, bset
->n_ineq
);
2599 tab
= isl_tab_from_basic_set(combined
, 0);
2600 for (i
= 0; i
< context_ineq
; ++i
)
2601 if (isl_tab_freeze_constraint(tab
, n_eq
+ i
) < 0)
2603 if (isl_tab_extend_cons(tab
, bset
->n_ineq
) < 0)
2606 for (i
= 0; i
< bset
->n_ineq
; ++i
) {
2609 combined
= isl_basic_set_add_ineq(combined
, ineq
->row
[i
]);
2610 if (isl_tab_add_ineq(tab
, ineq
->row
[i
]) < 0)
2614 if (isl_tab_detect_implicit_equalities(tab
) < 0)
2616 if (isl_tab_detect_redundant(tab
) < 0)
2618 for (i
= bset
->n_ineq
- 1; i
>= 0; --i
) {
2619 isl_basic_set
*test
;
2625 if (tab
->con
[n_eq
+ r
].is_redundant
)
2627 test
= isl_basic_set_dup(combined
);
2628 if (isl_inequality_negate(test
, r
) < 0)
2629 test
= isl_basic_set_free(test
);
2630 test
= isl_basic_set_update_from_tab(test
, tab
);
2631 is_empty
= isl_basic_set_is_empty(test
);
2632 isl_basic_set_free(test
);
2636 tab
->con
[n_eq
+ r
].is_redundant
= 1;
2638 bset
= update_ineq_free(bset
, ineq
, context
, row
, tab
);
2640 ISL_F_SET(bset
, ISL_BASIC_SET_NO_IMPLICIT
);
2641 ISL_F_SET(bset
, ISL_BASIC_SET_NO_REDUNDANT
);
2644 isl_basic_set_free(combined
);
2650 isl_basic_set_free(combined
);
2651 isl_basic_set_free(context
);
2652 isl_basic_set_free(bset
);
2656 /* Extract the inequalities of "bset" as an isl_mat.
2658 static __isl_give isl_mat
*extract_ineq(__isl_keep isl_basic_set
*bset
)
2667 ctx
= isl_basic_set_get_ctx(bset
);
2668 total
= isl_basic_set_total_dim(bset
);
2669 ineq
= isl_mat_sub_alloc6(ctx
, bset
->ineq
, 0, bset
->n_ineq
,
2675 /* Remove all information from "bset" that is redundant in the context
2676 * of "context", for the case where both "bset" and "context" are
2679 static __isl_give isl_basic_set
*uset_gist_uncompressed(
2680 __isl_take isl_basic_set
*bset
, __isl_take isl_basic_set
*context
)
2684 ineq
= extract_ineq(bset
);
2685 return uset_gist_full(bset
, ineq
, context
);
2688 /* Remove all information from "bset" that is redundant in the context
2689 * of "context", for the case where the combined equalities of
2690 * "bset" and "context" allow for a compression that can be obtained
2691 * by preapplication of "T".
2693 * "bset" itself is not transformed by "T". Instead, the inequalities
2694 * are extracted from "bset" and those are transformed by "T".
2695 * uset_gist_full then determines which of the transformed inequalities
2696 * are redundant with respect to the transformed "context" and removes
2697 * the corresponding inequalities from "bset".
2699 * After preapplying "T" to the inequalities, any common factor is
2700 * removed from the coefficients. If this results in a tightening
2701 * of the constant term, then the same tightening is applied to
2702 * the corresponding untransformed inequality in "bset".
2703 * That is, if after plugging in T, a constraint f(x) >= 0 is of the form
2707 * with 0 <= r < g, then it is equivalent to
2711 * This means that f(x) >= 0 is equivalent to f(x) - r >= 0 in the affine
2712 * subspace compressed by T since the latter would be transformed to
2716 static __isl_give isl_basic_set
*uset_gist_compressed(
2717 __isl_take isl_basic_set
*bset
, __isl_take isl_basic_set
*context
,
2718 __isl_take isl_mat
*T
)
2722 int i
, n_row
, n_col
;
2725 ineq
= extract_ineq(bset
);
2726 ineq
= isl_mat_product(ineq
, isl_mat_copy(T
));
2727 context
= isl_basic_set_preimage(context
, T
);
2729 if (!ineq
|| !context
)
2731 if (isl_basic_set_plain_is_empty(context
)) {
2733 isl_basic_set_free(context
);
2734 return isl_basic_set_set_to_empty(bset
);
2737 ctx
= isl_mat_get_ctx(ineq
);
2738 n_row
= isl_mat_rows(ineq
);
2739 n_col
= isl_mat_cols(ineq
);
2741 for (i
= 0; i
< n_row
; ++i
) {
2742 isl_seq_gcd(ineq
->row
[i
] + 1, n_col
- 1, &ctx
->normalize_gcd
);
2743 if (isl_int_is_zero(ctx
->normalize_gcd
))
2745 if (isl_int_is_one(ctx
->normalize_gcd
))
2747 isl_seq_scale_down(ineq
->row
[i
] + 1, ineq
->row
[i
] + 1,
2748 ctx
->normalize_gcd
, n_col
- 1);
2749 isl_int_fdiv_r(rem
, ineq
->row
[i
][0], ctx
->normalize_gcd
);
2750 isl_int_fdiv_q(ineq
->row
[i
][0],
2751 ineq
->row
[i
][0], ctx
->normalize_gcd
);
2752 if (isl_int_is_zero(rem
))
2754 bset
= isl_basic_set_cow(bset
);
2757 isl_int_sub(bset
->ineq
[i
][0], bset
->ineq
[i
][0], rem
);
2761 return uset_gist_full(bset
, ineq
, context
);
2764 isl_basic_set_free(context
);
2765 isl_basic_set_free(bset
);
2769 /* Project "bset" onto the variables that are involved in "template".
2771 static __isl_give isl_basic_set
*project_onto_involved(
2772 __isl_take isl_basic_set
*bset
, __isl_keep isl_basic_set
*template)
2776 if (!bset
|| !template)
2777 return isl_basic_set_free(bset
);
2779 n
= isl_basic_set_dim(template, isl_dim_set
);
2781 for (i
= 0; i
< n
; ++i
) {
2784 involved
= isl_basic_set_involves_dims(template,
2787 return isl_basic_set_free(bset
);
2790 bset
= isl_basic_set_eliminate_vars(bset
, i
, 1);
2796 /* Remove all information from bset that is redundant in the context
2797 * of context. In particular, equalities that are linear combinations
2798 * of those in context are removed. Then the inequalities that are
2799 * redundant in the context of the equalities and inequalities of
2800 * context are removed.
2802 * First of all, we drop those constraints from "context"
2803 * that are irrelevant for computing the gist of "bset".
2804 * Alternatively, we could factorize the intersection of "context" and "bset".
2806 * We first compute the intersection of the integer affine hulls
2807 * of "bset" and "context",
2808 * compute the gist inside this intersection and then reduce
2809 * the constraints with respect to the equalities of the context
2810 * that only involve variables already involved in the input.
2812 * If two constraints are mutually redundant, then uset_gist_full
2813 * will remove the second of those constraints. We therefore first
2814 * sort the constraints so that constraints not involving existentially
2815 * quantified variables are given precedence over those that do.
2816 * We have to perform this sorting before the variable compression,
2817 * because that may effect the order of the variables.
2819 static __isl_give isl_basic_set
*uset_gist(__isl_take isl_basic_set
*bset
,
2820 __isl_take isl_basic_set
*context
)
2825 isl_basic_set
*aff_context
;
2828 if (!bset
|| !context
)
2831 context
= drop_irrelevant_constraints(context
, bset
);
2833 bset
= isl_basic_set_detect_equalities(bset
);
2834 aff
= isl_basic_set_copy(bset
);
2835 aff
= isl_basic_set_plain_affine_hull(aff
);
2836 context
= isl_basic_set_detect_equalities(context
);
2837 aff_context
= isl_basic_set_copy(context
);
2838 aff_context
= isl_basic_set_plain_affine_hull(aff_context
);
2839 aff
= isl_basic_set_intersect(aff
, aff_context
);
2842 if (isl_basic_set_plain_is_empty(aff
)) {
2843 isl_basic_set_free(bset
);
2844 isl_basic_set_free(context
);
2847 bset
= isl_basic_set_sort_constraints(bset
);
2848 if (aff
->n_eq
== 0) {
2849 isl_basic_set_free(aff
);
2850 return uset_gist_uncompressed(bset
, context
);
2852 total
= isl_basic_set_total_dim(bset
);
2853 eq
= isl_mat_sub_alloc6(bset
->ctx
, aff
->eq
, 0, aff
->n_eq
, 0, 1 + total
);
2854 eq
= isl_mat_cow(eq
);
2855 T
= isl_mat_variable_compression(eq
, NULL
);
2856 isl_basic_set_free(aff
);
2857 if (T
&& T
->n_col
== 0) {
2859 isl_basic_set_free(context
);
2860 return isl_basic_set_set_to_empty(bset
);
2863 aff_context
= isl_basic_set_affine_hull(isl_basic_set_copy(context
));
2864 aff_context
= project_onto_involved(aff_context
, bset
);
2866 bset
= uset_gist_compressed(bset
, context
, T
);
2867 bset
= isl_basic_set_reduce_using_equalities(bset
, aff_context
);
2870 ISL_F_SET(bset
, ISL_BASIC_SET_NO_IMPLICIT
);
2871 ISL_F_SET(bset
, ISL_BASIC_SET_NO_REDUNDANT
);
2876 isl_basic_set_free(bset
);
2877 isl_basic_set_free(context
);
2881 /* Return the number of equality constraints in "bmap" that involve
2882 * local variables. This function assumes that Gaussian elimination
2883 * has been applied to the equality constraints.
2885 static int n_div_eq(__isl_keep isl_basic_map
*bmap
)
2893 if (bmap
->n_eq
== 0)
2896 total
= isl_basic_map_dim(bmap
, isl_dim_all
);
2897 n_div
= isl_basic_map_dim(bmap
, isl_dim_div
);
2900 for (i
= 0; i
< bmap
->n_eq
; ++i
)
2901 if (isl_seq_first_non_zero(bmap
->eq
[i
] + 1 + total
,
2908 /* Construct a basic map in "space" defined by the equality constraints in "eq".
2909 * The constraints are assumed not to involve any local variables.
2911 static __isl_give isl_basic_map
*basic_map_from_equalities(
2912 __isl_take isl_space
*space
, __isl_take isl_mat
*eq
)
2915 isl_basic_map
*bmap
= NULL
;
2920 if (1 + isl_space_dim(space
, isl_dim_all
) != eq
->n_col
)
2921 isl_die(isl_space_get_ctx(space
), isl_error_internal
,
2922 "unexpected number of columns", goto error
);
2924 bmap
= isl_basic_map_alloc_space(isl_space_copy(space
),
2926 for (i
= 0; i
< eq
->n_row
; ++i
) {
2927 k
= isl_basic_map_alloc_equality(bmap
);
2930 isl_seq_cpy(bmap
->eq
[k
], eq
->row
[i
], eq
->n_col
);
2933 isl_space_free(space
);
2937 isl_space_free(space
);
2939 isl_basic_map_free(bmap
);
2943 /* Construct and return a variable compression based on the equality
2944 * constraints in "bmap1" and "bmap2" that do not involve the local variables.
2945 * "n1" is the number of (initial) equality constraints in "bmap1"
2946 * that do involve local variables.
2947 * "n2" is the number of (initial) equality constraints in "bmap2"
2948 * that do involve local variables.
2949 * "total" is the total number of other variables.
2950 * This function assumes that Gaussian elimination
2951 * has been applied to the equality constraints in both "bmap1" and "bmap2"
2952 * such that the equality constraints not involving local variables
2953 * are those that start at "n1" or "n2".
2955 * If either of "bmap1" and "bmap2" does not have such equality constraints,
2956 * then simply compute the compression based on the equality constraints
2957 * in the other basic map.
2958 * Otherwise, combine the equality constraints from both into a new
2959 * basic map such that Gaussian elimination can be applied to this combination
2960 * and then construct a variable compression from the resulting
2961 * equality constraints.
2963 static __isl_give isl_mat
*combined_variable_compression(
2964 __isl_keep isl_basic_map
*bmap1
, int n1
,
2965 __isl_keep isl_basic_map
*bmap2
, int n2
, int total
)
2968 isl_mat
*E1
, *E2
, *V
;
2969 isl_basic_map
*bmap
;
2971 ctx
= isl_basic_map_get_ctx(bmap1
);
2972 if (bmap1
->n_eq
== n1
) {
2973 E2
= isl_mat_sub_alloc6(ctx
, bmap2
->eq
,
2974 n2
, bmap2
->n_eq
- n2
, 0, 1 + total
);
2975 return isl_mat_variable_compression(E2
, NULL
);
2977 if (bmap2
->n_eq
== n2
) {
2978 E1
= isl_mat_sub_alloc6(ctx
, bmap1
->eq
,
2979 n1
, bmap1
->n_eq
- n1
, 0, 1 + total
);
2980 return isl_mat_variable_compression(E1
, NULL
);
2982 E1
= isl_mat_sub_alloc6(ctx
, bmap1
->eq
,
2983 n1
, bmap1
->n_eq
- n1
, 0, 1 + total
);
2984 E2
= isl_mat_sub_alloc6(ctx
, bmap2
->eq
,
2985 n2
, bmap2
->n_eq
- n2
, 0, 1 + total
);
2986 E1
= isl_mat_concat(E1
, E2
);
2987 bmap
= basic_map_from_equalities(isl_basic_map_get_space(bmap1
), E1
);
2988 bmap
= isl_basic_map_gauss(bmap
, NULL
);
2991 E1
= isl_mat_sub_alloc6(ctx
, bmap
->eq
, 0, bmap
->n_eq
, 0, 1 + total
);
2992 V
= isl_mat_variable_compression(E1
, NULL
);
2993 isl_basic_map_free(bmap
);
2998 /* Extract the stride constraints from "bmap", compressed
2999 * with respect to both the stride constraints in "context" and
3000 * the remaining equality constraints in both "bmap" and "context".
3001 * "bmap_n_eq" is the number of (initial) stride constraints in "bmap".
3002 * "context_n_eq" is the number of (initial) stride constraints in "context".
3004 * Let x be all variables in "bmap" (and "context") other than the local
3005 * variables. First compute a variable compression
3009 * based on the non-stride equality constraints in "bmap" and "context".
3010 * Consider the stride constraints of "context",
3014 * with y the local variables and plug in the variable compression,
3017 * A(V x') + B(y) = 0
3019 * Use these constraints to compute a parameter compression on x'
3023 * Now consider the stride constraints of "bmap"
3027 * and plug in x = V*T x''.
3028 * That is, return A = [C*V*T D].
3030 static __isl_give isl_mat
*extract_compressed_stride_constraints(
3031 __isl_keep isl_basic_map
*bmap
, int bmap_n_eq
,
3032 __isl_keep isl_basic_map
*context
, int context_n_eq
)
3036 isl_mat
*A
, *B
, *T
, *V
;
3038 total
= isl_basic_map_dim(context
, isl_dim_all
);
3039 n_div
= isl_basic_map_dim(context
, isl_dim_div
);
3042 ctx
= isl_basic_map_get_ctx(bmap
);
3044 V
= combined_variable_compression(bmap
, bmap_n_eq
,
3045 context
, context_n_eq
, total
);
3047 A
= isl_mat_sub_alloc6(ctx
, context
->eq
, 0, context_n_eq
, 0, 1 + total
);
3048 B
= isl_mat_sub_alloc6(ctx
, context
->eq
,
3049 0, context_n_eq
, 1 + total
, n_div
);
3050 A
= isl_mat_product(A
, isl_mat_copy(V
));
3051 T
= isl_mat_parameter_compression_ext(A
, B
);
3052 T
= isl_mat_product(V
, T
);
3054 n_div
= isl_basic_map_dim(bmap
, isl_dim_div
);
3055 T
= isl_mat_diagonal(T
, isl_mat_identity(ctx
, n_div
));
3057 A
= isl_mat_sub_alloc6(ctx
, bmap
->eq
,
3058 0, bmap_n_eq
, 0, 1 + total
+ n_div
);
3059 A
= isl_mat_product(A
, T
);
3064 /* Remove the prime factors from *g that have an exponent that
3065 * is strictly smaller than the exponent in "c".
3066 * All exponents in *g are known to be smaller than or equal
3069 * That is, if *g is equal to
3071 * p_1^{e_1} p_2^{e_2} ... p_n^{e_n}
3073 * and "c" is equal to
3075 * p_1^{f_1} p_2^{f_2} ... p_n^{f_n}
3079 * p_1^{e_1 * (e_1 = f_1)} p_2^{e_2 * (e_2 = f_2)} ...
3080 * p_n^{e_n * (e_n = f_n)}
3082 * If e_i = f_i, then c / *g does not have any p_i factors and therefore
3083 * neither does the gcd of *g and c / *g.
3084 * If e_i < f_i, then the gcd of *g and c / *g has a positive
3085 * power min(e_i, s_i) of p_i with s_i = f_i - e_i among its factors.
3086 * Dividing *g by this gcd therefore strictly reduces the exponent
3087 * of the prime factors that need to be removed, while leaving the
3088 * other prime factors untouched.
3089 * Repeating this process until gcd(*g, c / *g) = 1 therefore
3090 * removes all undesired factors, without removing any others.
3092 static void remove_incomplete_powers(isl_int
*g
, isl_int c
)
3098 isl_int_divexact(t
, c
, *g
);
3099 isl_int_gcd(t
, t
, *g
);
3100 if (isl_int_is_one(t
))
3102 isl_int_divexact(*g
, *g
, t
);
3107 /* Reduce the "n" stride constraints in "bmap" based on a copy "A"
3108 * of the same stride constraints in a compressed space that exploits
3109 * all equalities in the context and the other equalities in "bmap".
3111 * If the stride constraints of "bmap" are of the form
3115 * then A is of the form
3119 * If any of these constraints involves only a single local variable y,
3120 * then the constraint appears as
3130 * Let g be the gcd of m and the coefficients of h.
3131 * Then, in particular, g is a divisor of the coefficients of h and
3135 * is known to be a multiple of g.
3136 * If some prime factor in m appears with the same exponent in g,
3137 * then it can be removed from m because f(x) is already known
3138 * to be a multiple of g and therefore in particular of this power
3139 * of the prime factors.
3140 * Prime factors that appear with a smaller exponent in g cannot
3141 * be removed from m.
3142 * Let g' be the divisor of g containing all prime factors that
3143 * appear with the same exponent in m and g, then
3147 * can be replaced by
3149 * f(x) + m/g' y_i' = 0
3151 * Note that (if g' != 1) this changes the explicit representation
3152 * of y_i to that of y_i', so the integer division at position i
3153 * is marked unknown and later recomputed by a call to
3154 * isl_basic_map_gauss.
3156 static __isl_give isl_basic_map
*reduce_stride_constraints(
3157 __isl_take isl_basic_map
*bmap
, int n
, __isl_keep isl_mat
*A
)
3165 return isl_basic_map_free(bmap
);
3167 total
= isl_basic_map_dim(bmap
, isl_dim_all
);
3168 n_div
= isl_basic_map_dim(bmap
, isl_dim_div
);
3172 for (i
= 0; i
< n
; ++i
) {
3175 div
= isl_seq_first_non_zero(bmap
->eq
[i
] + 1 + total
, n_div
);
3177 isl_die(isl_basic_map_get_ctx(bmap
), isl_error_internal
,
3178 "equality constraints modified unexpectedly",
3180 if (isl_seq_first_non_zero(bmap
->eq
[i
] + 1 + total
+ div
+ 1,
3181 n_div
- div
- 1) != -1)
3183 if (isl_mat_row_gcd(A
, i
, &gcd
) < 0)
3185 if (isl_int_is_one(gcd
))
3187 remove_incomplete_powers(&gcd
, bmap
->eq
[i
][1 + total
+ div
]);
3188 if (isl_int_is_one(gcd
))
3190 isl_int_divexact(bmap
->eq
[i
][1 + total
+ div
],
3191 bmap
->eq
[i
][1 + total
+ div
], gcd
);
3192 bmap
= isl_basic_map_mark_div_unknown(bmap
, div
);
3200 bmap
= isl_basic_map_gauss(bmap
, NULL
);
3205 isl_basic_map_free(bmap
);
3209 /* Simplify the stride constraints in "bmap" based on
3210 * the remaining equality constraints in "bmap" and all equality
3211 * constraints in "context".
3212 * Only do this if both "bmap" and "context" have stride constraints.
3214 * First extract a copy of the stride constraints in "bmap" in a compressed
3215 * space exploiting all the other equality constraints and then
3216 * use this compressed copy to simplify the original stride constraints.
3218 static __isl_give isl_basic_map
*gist_strides(__isl_take isl_basic_map
*bmap
,
3219 __isl_keep isl_basic_map
*context
)
3221 int bmap_n_eq
, context_n_eq
;
3224 if (!bmap
|| !context
)
3225 return isl_basic_map_free(bmap
);
3227 bmap_n_eq
= n_div_eq(bmap
);
3228 context_n_eq
= n_div_eq(context
);
3230 if (bmap_n_eq
< 0 || context_n_eq
< 0)
3231 return isl_basic_map_free(bmap
);
3232 if (bmap_n_eq
== 0 || context_n_eq
== 0)
3235 A
= extract_compressed_stride_constraints(bmap
, bmap_n_eq
,
3236 context
, context_n_eq
);
3237 bmap
= reduce_stride_constraints(bmap
, bmap_n_eq
, A
);
3244 /* Return a basic map that has the same intersection with "context" as "bmap"
3245 * and that is as "simple" as possible.
3247 * The core computation is performed on the pure constraints.
3248 * When we add back the meaning of the integer divisions, we need
3249 * to (re)introduce the div constraints. If we happen to have
3250 * discovered that some of these integer divisions are equal to
3251 * some affine combination of other variables, then these div
3252 * constraints may end up getting simplified in terms of the equalities,
3253 * resulting in extra inequalities on the other variables that
3254 * may have been removed already or that may not even have been
3255 * part of the input. We try and remove those constraints of
3256 * this form that are most obviously redundant with respect to
3257 * the context. We also remove those div constraints that are
3258 * redundant with respect to the other constraints in the result.
3260 * The stride constraints among the equality constraints in "bmap" are
3261 * also simplified with respecting to the other equality constraints
3262 * in "bmap" and with respect to all equality constraints in "context".
3264 struct isl_basic_map
*isl_basic_map_gist(struct isl_basic_map
*bmap
,
3265 struct isl_basic_map
*context
)
3267 isl_basic_set
*bset
, *eq
;
3268 isl_basic_map
*eq_bmap
;
3269 unsigned total
, n_div
, extra
, n_eq
, n_ineq
;
3271 if (!bmap
|| !context
)
3274 if (isl_basic_map_plain_is_universe(bmap
)) {
3275 isl_basic_map_free(context
);
3278 if (isl_basic_map_plain_is_empty(context
)) {
3279 isl_space
*space
= isl_basic_map_get_space(bmap
);
3280 isl_basic_map_free(bmap
);
3281 isl_basic_map_free(context
);
3282 return isl_basic_map_universe(space
);
3284 if (isl_basic_map_plain_is_empty(bmap
)) {
3285 isl_basic_map_free(context
);
3289 bmap
= isl_basic_map_remove_redundancies(bmap
);
3290 context
= isl_basic_map_remove_redundancies(context
);
3294 context
= isl_basic_map_align_divs(context
, bmap
);
3295 n_div
= isl_basic_map_dim(context
, isl_dim_div
);
3296 total
= isl_basic_map_dim(bmap
, isl_dim_all
);
3297 extra
= n_div
- isl_basic_map_dim(bmap
, isl_dim_div
);
3299 bset
= isl_basic_map_underlying_set(isl_basic_map_copy(bmap
));
3300 bset
= isl_basic_set_add_dims(bset
, isl_dim_set
, extra
);
3301 bset
= uset_gist(bset
,
3302 isl_basic_map_underlying_set(isl_basic_map_copy(context
)));
3303 bset
= isl_basic_set_project_out(bset
, isl_dim_set
, total
, extra
);
3305 if (!bset
|| bset
->n_eq
== 0 || n_div
== 0 ||
3306 isl_basic_set_plain_is_empty(bset
)) {
3307 isl_basic_map_free(context
);
3308 return isl_basic_map_overlying_set(bset
, bmap
);
3312 n_ineq
= bset
->n_ineq
;
3313 eq
= isl_basic_set_copy(bset
);
3314 eq
= isl_basic_set_cow(eq
);
3315 if (isl_basic_set_free_inequality(eq
, n_ineq
) < 0)
3316 eq
= isl_basic_set_free(eq
);
3317 if (isl_basic_set_free_equality(bset
, n_eq
) < 0)
3318 bset
= isl_basic_set_free(bset
);
3320 eq_bmap
= isl_basic_map_overlying_set(eq
, isl_basic_map_copy(bmap
));
3321 eq_bmap
= gist_strides(eq_bmap
, context
);
3322 eq_bmap
= isl_basic_map_remove_shifted_constraints(eq_bmap
, context
);
3323 bmap
= isl_basic_map_overlying_set(bset
, bmap
);
3324 bmap
= isl_basic_map_intersect(bmap
, eq_bmap
);
3325 bmap
= isl_basic_map_remove_redundancies(bmap
);
3329 isl_basic_map_free(bmap
);
3330 isl_basic_map_free(context
);
3335 * Assumes context has no implicit divs.
3337 __isl_give isl_map
*isl_map_gist_basic_map(__isl_take isl_map
*map
,
3338 __isl_take isl_basic_map
*context
)
3342 if (!map
|| !context
)
3345 if (isl_basic_map_plain_is_empty(context
)) {
3346 isl_space
*space
= isl_map_get_space(map
);
3348 isl_basic_map_free(context
);
3349 return isl_map_universe(space
);
3352 context
= isl_basic_map_remove_redundancies(context
);
3353 map
= isl_map_cow(map
);
3354 if (!map
|| !context
)
3356 isl_assert(map
->ctx
, isl_space_is_equal(map
->dim
, context
->dim
), goto error
);
3357 map
= isl_map_compute_divs(map
);
3360 for (i
= map
->n
- 1; i
>= 0; --i
) {
3361 map
->p
[i
] = isl_basic_map_gist(map
->p
[i
],
3362 isl_basic_map_copy(context
));
3365 if (isl_basic_map_plain_is_empty(map
->p
[i
])) {
3366 isl_basic_map_free(map
->p
[i
]);
3367 if (i
!= map
->n
- 1)
3368 map
->p
[i
] = map
->p
[map
->n
- 1];
3372 isl_basic_map_free(context
);
3373 ISL_F_CLR(map
, ISL_MAP_NORMALIZED
);
3377 isl_basic_map_free(context
);
3381 /* Drop all inequalities from "bmap" that also appear in "context".
3382 * "context" is assumed to have only known local variables and
3383 * the initial local variables of "bmap" are assumed to be the same
3384 * as those of "context".
3385 * The constraints of both "bmap" and "context" are assumed
3386 * to have been sorted using isl_basic_map_sort_constraints.
3388 * Run through the inequality constraints of "bmap" and "context"
3390 * If a constraint of "bmap" involves variables not in "context",
3391 * then it cannot appear in "context".
3392 * If a matching constraint is found, it is removed from "bmap".
3394 static __isl_give isl_basic_map
*drop_inequalities(
3395 __isl_take isl_basic_map
*bmap
, __isl_keep isl_basic_map
*context
)
3398 unsigned total
, extra
;
3400 if (!bmap
|| !context
)
3401 return isl_basic_map_free(bmap
);
3403 total
= isl_basic_map_total_dim(context
);
3404 extra
= isl_basic_map_total_dim(bmap
) - total
;
3406 i1
= bmap
->n_ineq
- 1;
3407 i2
= context
->n_ineq
- 1;
3408 while (bmap
&& i1
>= 0 && i2
>= 0) {
3411 if (isl_seq_first_non_zero(bmap
->ineq
[i1
] + 1 + total
,
3416 cmp
= isl_basic_map_constraint_cmp(context
, bmap
->ineq
[i1
],
3426 if (isl_int_eq(bmap
->ineq
[i1
][0], context
->ineq
[i2
][0])) {
3427 bmap
= isl_basic_map_cow(bmap
);
3428 if (isl_basic_map_drop_inequality(bmap
, i1
) < 0)
3429 bmap
= isl_basic_map_free(bmap
);
3438 /* Drop all equalities from "bmap" that also appear in "context".
3439 * "context" is assumed to have only known local variables and
3440 * the initial local variables of "bmap" are assumed to be the same
3441 * as those of "context".
3443 * Run through the equality constraints of "bmap" and "context"
3445 * If a constraint of "bmap" involves variables not in "context",
3446 * then it cannot appear in "context".
3447 * If a matching constraint is found, it is removed from "bmap".
3449 static __isl_give isl_basic_map
*drop_equalities(
3450 __isl_take isl_basic_map
*bmap
, __isl_keep isl_basic_map
*context
)
3453 unsigned total
, extra
;
3455 if (!bmap
|| !context
)
3456 return isl_basic_map_free(bmap
);
3458 total
= isl_basic_map_total_dim(context
);
3459 extra
= isl_basic_map_total_dim(bmap
) - total
;
3461 i1
= bmap
->n_eq
- 1;
3462 i2
= context
->n_eq
- 1;
3464 while (bmap
&& i1
>= 0 && i2
>= 0) {
3467 if (isl_seq_first_non_zero(bmap
->eq
[i1
] + 1 + total
,
3470 last1
= isl_seq_last_non_zero(bmap
->eq
[i1
] + 1, total
);
3471 last2
= isl_seq_last_non_zero(context
->eq
[i2
] + 1, total
);
3472 if (last1
> last2
) {
3476 if (last1
< last2
) {
3480 if (isl_seq_eq(bmap
->eq
[i1
], context
->eq
[i2
], 1 + total
)) {
3481 bmap
= isl_basic_map_cow(bmap
);
3482 if (isl_basic_map_drop_equality(bmap
, i1
) < 0)
3483 bmap
= isl_basic_map_free(bmap
);
3492 /* Remove the constraints in "context" from "bmap".
3493 * "context" is assumed to have explicit representations
3494 * for all local variables.
3496 * First align the divs of "bmap" to those of "context" and
3497 * sort the constraints. Then drop all constraints from "bmap"
3498 * that appear in "context".
3500 __isl_give isl_basic_map
*isl_basic_map_plain_gist(
3501 __isl_take isl_basic_map
*bmap
, __isl_take isl_basic_map
*context
)
3503 isl_bool done
, known
;
3505 done
= isl_basic_map_plain_is_universe(context
);
3506 if (done
== isl_bool_false
)
3507 done
= isl_basic_map_plain_is_universe(bmap
);
3508 if (done
== isl_bool_false
)
3509 done
= isl_basic_map_plain_is_empty(context
);
3510 if (done
== isl_bool_false
)
3511 done
= isl_basic_map_plain_is_empty(bmap
);
3515 isl_basic_map_free(context
);
3518 known
= isl_basic_map_divs_known(context
);
3522 isl_die(isl_basic_map_get_ctx(bmap
), isl_error_invalid
,
3523 "context has unknown divs", goto error
);
3525 bmap
= isl_basic_map_align_divs(bmap
, context
);
3526 bmap
= isl_basic_map_gauss(bmap
, NULL
);
3527 bmap
= isl_basic_map_sort_constraints(bmap
);
3528 context
= isl_basic_map_sort_constraints(context
);
3530 bmap
= drop_inequalities(bmap
, context
);
3531 bmap
= drop_equalities(bmap
, context
);
3533 isl_basic_map_free(context
);
3534 bmap
= isl_basic_map_finalize(bmap
);
3537 isl_basic_map_free(bmap
);
3538 isl_basic_map_free(context
);
3542 /* Replace "map" by the disjunct at position "pos" and free "context".
3544 static __isl_give isl_map
*replace_by_disjunct(__isl_take isl_map
*map
,
3545 int pos
, __isl_take isl_basic_map
*context
)
3547 isl_basic_map
*bmap
;
3549 bmap
= isl_basic_map_copy(map
->p
[pos
]);
3551 isl_basic_map_free(context
);
3552 return isl_map_from_basic_map(bmap
);
3555 /* Remove the constraints in "context" from "map".
3556 * If any of the disjuncts in the result turns out to be the universe,
3557 * then return this universe.
3558 * "context" is assumed to have explicit representations
3559 * for all local variables.
3561 __isl_give isl_map
*isl_map_plain_gist_basic_map(__isl_take isl_map
*map
,
3562 __isl_take isl_basic_map
*context
)
3565 isl_bool univ
, known
;
3567 univ
= isl_basic_map_plain_is_universe(context
);
3571 isl_basic_map_free(context
);
3574 known
= isl_basic_map_divs_known(context
);
3578 isl_die(isl_map_get_ctx(map
), isl_error_invalid
,
3579 "context has unknown divs", goto error
);
3581 map
= isl_map_cow(map
);
3584 for (i
= 0; i
< map
->n
; ++i
) {
3585 map
->p
[i
] = isl_basic_map_plain_gist(map
->p
[i
],
3586 isl_basic_map_copy(context
));
3587 univ
= isl_basic_map_plain_is_universe(map
->p
[i
]);
3590 if (univ
&& map
->n
> 1)
3591 return replace_by_disjunct(map
, i
, context
);
3594 isl_basic_map_free(context
);
3595 ISL_F_CLR(map
, ISL_MAP_NORMALIZED
);
3597 ISL_F_CLR(map
, ISL_MAP_DISJOINT
);
3601 isl_basic_map_free(context
);
3605 /* Replace "map" by a universe map in the same space and free "drop".
3607 static __isl_give isl_map
*replace_by_universe(__isl_take isl_map
*map
,
3608 __isl_take isl_map
*drop
)
3612 res
= isl_map_universe(isl_map_get_space(map
));
3618 /* Return a map that has the same intersection with "context" as "map"
3619 * and that is as "simple" as possible.
3621 * If "map" is already the universe, then we cannot make it any simpler.
3622 * Similarly, if "context" is the universe, then we cannot exploit it
3624 * If "map" and "context" are identical to each other, then we can
3625 * return the corresponding universe.
3627 * If either "map" or "context" consists of multiple disjuncts,
3628 * then check if "context" happens to be a subset of "map",
3629 * in which case all constraints can be removed.
3630 * In case of multiple disjuncts, the standard procedure
3631 * may not be able to detect that all constraints can be removed.
3633 * If none of these cases apply, we have to work a bit harder.
3634 * During this computation, we make use of a single disjunct context,
3635 * so if the original context consists of more than one disjunct
3636 * then we need to approximate the context by a single disjunct set.
3637 * Simply taking the simple hull may drop constraints that are
3638 * only implicitly available in each disjunct. We therefore also
3639 * look for constraints among those defining "map" that are valid
3640 * for the context. These can then be used to simplify away
3641 * the corresponding constraints in "map".
3643 static __isl_give isl_map
*map_gist(__isl_take isl_map
*map
,
3644 __isl_take isl_map
*context
)
3648 int single_disjunct_map
, single_disjunct_context
;
3650 isl_basic_map
*hull
;
3652 is_universe
= isl_map_plain_is_universe(map
);
3653 if (is_universe
>= 0 && !is_universe
)
3654 is_universe
= isl_map_plain_is_universe(context
);
3655 if (is_universe
< 0)
3658 isl_map_free(context
);
3662 equal
= isl_map_plain_is_equal(map
, context
);
3666 return replace_by_universe(map
, context
);
3668 single_disjunct_map
= isl_map_n_basic_map(map
) == 1;
3669 single_disjunct_context
= isl_map_n_basic_map(context
) == 1;
3670 if (!single_disjunct_map
|| !single_disjunct_context
) {
3671 subset
= isl_map_is_subset(context
, map
);
3675 return replace_by_universe(map
, context
);
3678 context
= isl_map_compute_divs(context
);
3681 if (single_disjunct_context
) {
3682 hull
= isl_map_simple_hull(context
);
3687 ctx
= isl_map_get_ctx(map
);
3688 list
= isl_map_list_alloc(ctx
, 2);
3689 list
= isl_map_list_add(list
, isl_map_copy(context
));
3690 list
= isl_map_list_add(list
, isl_map_copy(map
));
3691 hull
= isl_map_unshifted_simple_hull_from_map_list(context
,
3694 return isl_map_gist_basic_map(map
, hull
);
3697 isl_map_free(context
);
3701 __isl_give isl_map
*isl_map_gist(__isl_take isl_map
*map
,
3702 __isl_take isl_map
*context
)
3704 return isl_map_align_params_map_map_and(map
, context
, &map_gist
);
3707 struct isl_basic_set
*isl_basic_set_gist(struct isl_basic_set
*bset
,
3708 struct isl_basic_set
*context
)
3710 return bset_from_bmap(isl_basic_map_gist(bset_to_bmap(bset
),
3711 bset_to_bmap(context
)));
3714 __isl_give isl_set
*isl_set_gist_basic_set(__isl_take isl_set
*set
,
3715 __isl_take isl_basic_set
*context
)
3717 return set_from_map(isl_map_gist_basic_map(set_to_map(set
),
3718 bset_to_bmap(context
)));
3721 __isl_give isl_set
*isl_set_gist_params_basic_set(__isl_take isl_set
*set
,
3722 __isl_take isl_basic_set
*context
)
3724 isl_space
*space
= isl_set_get_space(set
);
3725 isl_basic_set
*dom_context
= isl_basic_set_universe(space
);
3726 dom_context
= isl_basic_set_intersect_params(dom_context
, context
);
3727 return isl_set_gist_basic_set(set
, dom_context
);
3730 __isl_give isl_set
*isl_set_gist(__isl_take isl_set
*set
,
3731 __isl_take isl_set
*context
)
3733 return set_from_map(isl_map_gist(set_to_map(set
), set_to_map(context
)));
3736 /* Compute the gist of "bmap" with respect to the constraints "context"
3739 __isl_give isl_basic_map
*isl_basic_map_gist_domain(
3740 __isl_take isl_basic_map
*bmap
, __isl_take isl_basic_set
*context
)
3742 isl_space
*space
= isl_basic_map_get_space(bmap
);
3743 isl_basic_map
*bmap_context
= isl_basic_map_universe(space
);
3745 bmap_context
= isl_basic_map_intersect_domain(bmap_context
, context
);
3746 return isl_basic_map_gist(bmap
, bmap_context
);
3749 __isl_give isl_map
*isl_map_gist_domain(__isl_take isl_map
*map
,
3750 __isl_take isl_set
*context
)
3752 isl_map
*map_context
= isl_map_universe(isl_map_get_space(map
));
3753 map_context
= isl_map_intersect_domain(map_context
, context
);
3754 return isl_map_gist(map
, map_context
);
3757 __isl_give isl_map
*isl_map_gist_range(__isl_take isl_map
*map
,
3758 __isl_take isl_set
*context
)
3760 isl_map
*map_context
= isl_map_universe(isl_map_get_space(map
));
3761 map_context
= isl_map_intersect_range(map_context
, context
);
3762 return isl_map_gist(map
, map_context
);
3765 __isl_give isl_map
*isl_map_gist_params(__isl_take isl_map
*map
,
3766 __isl_take isl_set
*context
)
3768 isl_map
*map_context
= isl_map_universe(isl_map_get_space(map
));
3769 map_context
= isl_map_intersect_params(map_context
, context
);
3770 return isl_map_gist(map
, map_context
);
3773 __isl_give isl_set
*isl_set_gist_params(__isl_take isl_set
*set
,
3774 __isl_take isl_set
*context
)
3776 return isl_map_gist_params(set
, context
);
3779 /* Quick check to see if two basic maps are disjoint.
3780 * In particular, we reduce the equalities and inequalities of
3781 * one basic map in the context of the equalities of the other
3782 * basic map and check if we get a contradiction.
3784 isl_bool
isl_basic_map_plain_is_disjoint(__isl_keep isl_basic_map
*bmap1
,
3785 __isl_keep isl_basic_map
*bmap2
)
3787 struct isl_vec
*v
= NULL
;
3792 if (!bmap1
|| !bmap2
)
3793 return isl_bool_error
;
3794 isl_assert(bmap1
->ctx
, isl_space_is_equal(bmap1
->dim
, bmap2
->dim
),
3795 return isl_bool_error
);
3796 if (bmap1
->n_div
|| bmap2
->n_div
)
3797 return isl_bool_false
;
3798 if (!bmap1
->n_eq
&& !bmap2
->n_eq
)
3799 return isl_bool_false
;
3801 total
= isl_space_dim(bmap1
->dim
, isl_dim_all
);
3803 return isl_bool_false
;
3804 v
= isl_vec_alloc(bmap1
->ctx
, 1 + total
);
3807 elim
= isl_alloc_array(bmap1
->ctx
, int, total
);
3810 compute_elimination_index(bmap1
, elim
);
3811 for (i
= 0; i
< bmap2
->n_eq
; ++i
) {
3813 reduced
= reduced_using_equalities(v
->block
.data
, bmap2
->eq
[i
],
3815 if (reduced
&& !isl_int_is_zero(v
->block
.data
[0]) &&
3816 isl_seq_first_non_zero(v
->block
.data
+ 1, total
) == -1)
3819 for (i
= 0; i
< bmap2
->n_ineq
; ++i
) {
3821 reduced
= reduced_using_equalities(v
->block
.data
,
3822 bmap2
->ineq
[i
], bmap1
, elim
);
3823 if (reduced
&& isl_int_is_neg(v
->block
.data
[0]) &&
3824 isl_seq_first_non_zero(v
->block
.data
+ 1, total
) == -1)
3827 compute_elimination_index(bmap2
, elim
);
3828 for (i
= 0; i
< bmap1
->n_ineq
; ++i
) {
3830 reduced
= reduced_using_equalities(v
->block
.data
,
3831 bmap1
->ineq
[i
], bmap2
, elim
);
3832 if (reduced
&& isl_int_is_neg(v
->block
.data
[0]) &&
3833 isl_seq_first_non_zero(v
->block
.data
+ 1, total
) == -1)
3838 return isl_bool_false
;
3842 return isl_bool_true
;
3846 return isl_bool_error
;
3849 int isl_basic_set_plain_is_disjoint(__isl_keep isl_basic_set
*bset1
,
3850 __isl_keep isl_basic_set
*bset2
)
3852 return isl_basic_map_plain_is_disjoint(bset_to_bmap(bset1
),
3853 bset_to_bmap(bset2
));
3856 /* Does "test" hold for all pairs of basic maps in "map1" and "map2"?
3858 static isl_bool
all_pairs(__isl_keep isl_map
*map1
, __isl_keep isl_map
*map2
,
3859 isl_bool (*test
)(__isl_keep isl_basic_map
*bmap1
,
3860 __isl_keep isl_basic_map
*bmap2
))
3865 return isl_bool_error
;
3867 for (i
= 0; i
< map1
->n
; ++i
) {
3868 for (j
= 0; j
< map2
->n
; ++j
) {
3869 isl_bool d
= test(map1
->p
[i
], map2
->p
[j
]);
3870 if (d
!= isl_bool_true
)
3875 return isl_bool_true
;
3878 /* Are "map1" and "map2" obviously disjoint, based on information
3879 * that can be derived without looking at the individual basic maps?
3881 * In particular, if one of them is empty or if they live in different spaces
3882 * (ignoring parameters), then they are clearly disjoint.
3884 static isl_bool
isl_map_plain_is_disjoint_global(__isl_keep isl_map
*map1
,
3885 __isl_keep isl_map
*map2
)
3891 return isl_bool_error
;
3893 disjoint
= isl_map_plain_is_empty(map1
);
3894 if (disjoint
< 0 || disjoint
)
3897 disjoint
= isl_map_plain_is_empty(map2
);
3898 if (disjoint
< 0 || disjoint
)
3901 match
= isl_space_tuple_is_equal(map1
->dim
, isl_dim_in
,
3902 map2
->dim
, isl_dim_in
);
3903 if (match
< 0 || !match
)
3904 return match
< 0 ? isl_bool_error
: isl_bool_true
;
3906 match
= isl_space_tuple_is_equal(map1
->dim
, isl_dim_out
,
3907 map2
->dim
, isl_dim_out
);
3908 if (match
< 0 || !match
)
3909 return match
< 0 ? isl_bool_error
: isl_bool_true
;
3911 return isl_bool_false
;
3914 /* Are "map1" and "map2" obviously disjoint?
3916 * If one of them is empty or if they live in different spaces (ignoring
3917 * parameters), then they are clearly disjoint.
3918 * This is checked by isl_map_plain_is_disjoint_global.
3920 * If they have different parameters, then we skip any further tests.
3922 * If they are obviously equal, but not obviously empty, then we will
3923 * not be able to detect if they are disjoint.
3925 * Otherwise we check if each basic map in "map1" is obviously disjoint
3926 * from each basic map in "map2".
3928 isl_bool
isl_map_plain_is_disjoint(__isl_keep isl_map
*map1
,
3929 __isl_keep isl_map
*map2
)
3935 disjoint
= isl_map_plain_is_disjoint_global(map1
, map2
);
3936 if (disjoint
< 0 || disjoint
)
3939 match
= isl_space_match(map1
->dim
, isl_dim_param
,
3940 map2
->dim
, isl_dim_param
);
3941 if (match
< 0 || !match
)
3942 return match
< 0 ? isl_bool_error
: isl_bool_false
;
3944 intersect
= isl_map_plain_is_equal(map1
, map2
);
3945 if (intersect
< 0 || intersect
)
3946 return intersect
< 0 ? isl_bool_error
: isl_bool_false
;
3948 return all_pairs(map1
, map2
, &isl_basic_map_plain_is_disjoint
);
3951 /* Are "map1" and "map2" disjoint?
3953 * They are disjoint if they are "obviously disjoint" or if one of them
3954 * is empty. Otherwise, they are not disjoint if one of them is universal.
3955 * If the two inputs are (obviously) equal and not empty, then they are
3957 * If none of these cases apply, then check if all pairs of basic maps
3960 isl_bool
isl_map_is_disjoint(__isl_keep isl_map
*map1
, __isl_keep isl_map
*map2
)
3965 disjoint
= isl_map_plain_is_disjoint_global(map1
, map2
);
3966 if (disjoint
< 0 || disjoint
)
3969 disjoint
= isl_map_is_empty(map1
);
3970 if (disjoint
< 0 || disjoint
)
3973 disjoint
= isl_map_is_empty(map2
);
3974 if (disjoint
< 0 || disjoint
)
3977 intersect
= isl_map_plain_is_universe(map1
);
3978 if (intersect
< 0 || intersect
)
3979 return intersect
< 0 ? isl_bool_error
: isl_bool_false
;
3981 intersect
= isl_map_plain_is_universe(map2
);
3982 if (intersect
< 0 || intersect
)
3983 return intersect
< 0 ? isl_bool_error
: isl_bool_false
;
3985 intersect
= isl_map_plain_is_equal(map1
, map2
);
3986 if (intersect
< 0 || intersect
)
3987 return isl_bool_not(intersect
);
3989 return all_pairs(map1
, map2
, &isl_basic_map_is_disjoint
);
3992 /* Are "bmap1" and "bmap2" disjoint?
3994 * They are disjoint if they are "obviously disjoint" or if one of them
3995 * is empty. Otherwise, they are not disjoint if one of them is universal.
3996 * If none of these cases apply, we compute the intersection and see if
3997 * the result is empty.
3999 isl_bool
isl_basic_map_is_disjoint(__isl_keep isl_basic_map
*bmap1
,
4000 __isl_keep isl_basic_map
*bmap2
)
4004 isl_basic_map
*test
;
4006 disjoint
= isl_basic_map_plain_is_disjoint(bmap1
, bmap2
);
4007 if (disjoint
< 0 || disjoint
)
4010 disjoint
= isl_basic_map_is_empty(bmap1
);
4011 if (disjoint
< 0 || disjoint
)
4014 disjoint
= isl_basic_map_is_empty(bmap2
);
4015 if (disjoint
< 0 || disjoint
)
4018 intersect
= isl_basic_map_plain_is_universe(bmap1
);
4019 if (intersect
< 0 || intersect
)
4020 return intersect
< 0 ? isl_bool_error
: isl_bool_false
;
4022 intersect
= isl_basic_map_plain_is_universe(bmap2
);
4023 if (intersect
< 0 || intersect
)
4024 return intersect
< 0 ? isl_bool_error
: isl_bool_false
;
4026 test
= isl_basic_map_intersect(isl_basic_map_copy(bmap1
),
4027 isl_basic_map_copy(bmap2
));
4028 disjoint
= isl_basic_map_is_empty(test
);
4029 isl_basic_map_free(test
);
4034 /* Are "bset1" and "bset2" disjoint?
4036 isl_bool
isl_basic_set_is_disjoint(__isl_keep isl_basic_set
*bset1
,
4037 __isl_keep isl_basic_set
*bset2
)
4039 return isl_basic_map_is_disjoint(bset1
, bset2
);
4042 isl_bool
isl_set_plain_is_disjoint(__isl_keep isl_set
*set1
,
4043 __isl_keep isl_set
*set2
)
4045 return isl_map_plain_is_disjoint(set_to_map(set1
), set_to_map(set2
));
4048 /* Are "set1" and "set2" disjoint?
4050 isl_bool
isl_set_is_disjoint(__isl_keep isl_set
*set1
, __isl_keep isl_set
*set2
)
4052 return isl_map_is_disjoint(set1
, set2
);
4055 /* Is "v" equal to 0, 1 or -1?
4057 static int is_zero_or_one(isl_int v
)
4059 return isl_int_is_zero(v
) || isl_int_is_one(v
) || isl_int_is_negone(v
);
4062 /* Check if we can combine a given div with lower bound l and upper
4063 * bound u with some other div and if so return that other div.
4064 * Otherwise return -1.
4066 * We first check that
4067 * - the bounds are opposites of each other (except for the constant
4069 * - the bounds do not reference any other div
4070 * - no div is defined in terms of this div
4072 * Let m be the size of the range allowed on the div by the bounds.
4073 * That is, the bounds are of the form
4075 * e <= a <= e + m - 1
4077 * with e some expression in the other variables.
4078 * We look for another div b such that no third div is defined in terms
4079 * of this second div b and such that in any constraint that contains
4080 * a (except for the given lower and upper bound), also contains b
4081 * with a coefficient that is m times that of b.
4082 * That is, all constraints (execpt for the lower and upper bound)
4085 * e + f (a + m b) >= 0
4087 * Furthermore, in the constraints that only contain b, the coefficient
4088 * of b should be equal to 1 or -1.
4089 * If so, we return b so that "a + m b" can be replaced by
4090 * a single div "c = a + m b".
4092 static int div_find_coalesce(struct isl_basic_map
*bmap
, int *pairs
,
4093 unsigned div
, unsigned l
, unsigned u
)
4099 if (bmap
->n_div
<= 1)
4101 dim
= isl_space_dim(bmap
->dim
, isl_dim_all
);
4102 if (isl_seq_first_non_zero(bmap
->ineq
[l
] + 1 + dim
, div
) != -1)
4104 if (isl_seq_first_non_zero(bmap
->ineq
[l
] + 1 + dim
+ div
+ 1,
4105 bmap
->n_div
- div
- 1) != -1)
4107 if (!isl_seq_is_neg(bmap
->ineq
[l
] + 1, bmap
->ineq
[u
] + 1,
4111 for (i
= 0; i
< bmap
->n_div
; ++i
) {
4112 if (isl_int_is_zero(bmap
->div
[i
][0]))
4114 if (!isl_int_is_zero(bmap
->div
[i
][1 + 1 + dim
+ div
]))
4118 isl_int_add(bmap
->ineq
[l
][0], bmap
->ineq
[l
][0], bmap
->ineq
[u
][0]);
4119 if (isl_int_is_neg(bmap
->ineq
[l
][0])) {
4120 isl_int_sub(bmap
->ineq
[l
][0],
4121 bmap
->ineq
[l
][0], bmap
->ineq
[u
][0]);
4122 bmap
= isl_basic_map_copy(bmap
);
4123 bmap
= isl_basic_map_set_to_empty(bmap
);
4124 isl_basic_map_free(bmap
);
4127 isl_int_add_ui(bmap
->ineq
[l
][0], bmap
->ineq
[l
][0], 1);
4128 for (i
= 0; i
< bmap
->n_div
; ++i
) {
4133 for (j
= 0; j
< bmap
->n_div
; ++j
) {
4134 if (isl_int_is_zero(bmap
->div
[j
][0]))
4136 if (!isl_int_is_zero(bmap
->div
[j
][1 + 1 + dim
+ i
]))
4139 if (j
< bmap
->n_div
)
4141 for (j
= 0; j
< bmap
->n_ineq
; ++j
) {
4143 if (j
== l
|| j
== u
)
4145 if (isl_int_is_zero(bmap
->ineq
[j
][1 + dim
+ div
])) {
4146 if (is_zero_or_one(bmap
->ineq
[j
][1 + dim
+ i
]))
4150 if (isl_int_is_zero(bmap
->ineq
[j
][1 + dim
+ i
]))
4152 isl_int_mul(bmap
->ineq
[j
][1 + dim
+ div
],
4153 bmap
->ineq
[j
][1 + dim
+ div
],
4155 valid
= isl_int_eq(bmap
->ineq
[j
][1 + dim
+ div
],
4156 bmap
->ineq
[j
][1 + dim
+ i
]);
4157 isl_int_divexact(bmap
->ineq
[j
][1 + dim
+ div
],
4158 bmap
->ineq
[j
][1 + dim
+ div
],
4163 if (j
< bmap
->n_ineq
)
4168 isl_int_sub_ui(bmap
->ineq
[l
][0], bmap
->ineq
[l
][0], 1);
4169 isl_int_sub(bmap
->ineq
[l
][0], bmap
->ineq
[l
][0], bmap
->ineq
[u
][0]);
4173 /* Internal data structure used during the construction and/or evaluation of
4174 * an inequality that ensures that a pair of bounds always allows
4175 * for an integer value.
4177 * "tab" is the tableau in which the inequality is evaluated. It may
4178 * be NULL until it is actually needed.
4179 * "v" contains the inequality coefficients.
4180 * "g", "fl" and "fu" are temporary scalars used during the construction and
4183 struct test_ineq_data
{
4184 struct isl_tab
*tab
;
4191 /* Free all the memory allocated by the fields of "data".
4193 static void test_ineq_data_clear(struct test_ineq_data
*data
)
4195 isl_tab_free(data
->tab
);
4196 isl_vec_free(data
->v
);
4197 isl_int_clear(data
->g
);
4198 isl_int_clear(data
->fl
);
4199 isl_int_clear(data
->fu
);
4202 /* Is the inequality stored in data->v satisfied by "bmap"?
4203 * That is, does it only attain non-negative values?
4204 * data->tab is a tableau corresponding to "bmap".
4206 static isl_bool
test_ineq_is_satisfied(__isl_keep isl_basic_map
*bmap
,
4207 struct test_ineq_data
*data
)
4210 enum isl_lp_result res
;
4212 ctx
= isl_basic_map_get_ctx(bmap
);
4214 data
->tab
= isl_tab_from_basic_map(bmap
, 0);
4215 res
= isl_tab_min(data
->tab
, data
->v
->el
, ctx
->one
, &data
->g
, NULL
, 0);
4216 if (res
== isl_lp_error
)
4217 return isl_bool_error
;
4218 return res
== isl_lp_ok
&& isl_int_is_nonneg(data
->g
);
4221 /* Given a lower and an upper bound on div i, do they always allow
4222 * for an integer value of the given div?
4223 * Determine this property by constructing an inequality
4224 * such that the property is guaranteed when the inequality is nonnegative.
4225 * The lower bound is inequality l, while the upper bound is inequality u.
4226 * The constructed inequality is stored in data->v.
4228 * Let the upper bound be
4232 * and the lower bound
4236 * Let n_u = f_u g and n_l = f_l g, with g = gcd(n_u, n_l).
4239 * - f_u e_l <= f_u f_l g a <= f_l e_u
4241 * Since all variables are integer valued, this is equivalent to
4243 * - f_u e_l - (f_u - 1) <= f_u f_l g a <= f_l e_u + (f_l - 1)
4245 * If this interval is at least f_u f_l g, then it contains at least
4246 * one integer value for a.
4247 * That is, the test constraint is
4249 * f_l e_u + f_u e_l + f_l - 1 + f_u - 1 + 1 >= f_u f_l g
4253 * f_l e_u + f_u e_l + f_l - 1 + f_u - 1 + 1 - f_u f_l g >= 0
4255 * If the coefficients of f_l e_u + f_u e_l have a common divisor g',
4256 * then the constraint can be scaled down by a factor g',
4257 * with the constant term replaced by
4258 * floor((f_l e_{u,0} + f_u e_{l,0} + f_l - 1 + f_u - 1 + 1 - f_u f_l g)/g').
4259 * Note that the result of applying Fourier-Motzkin to this pair
4262 * f_l e_u + f_u e_l >= 0
4264 * If the constant term of the scaled down version of this constraint,
4265 * i.e., floor((f_l e_{u,0} + f_u e_{l,0})/g') is equal to the constant
4266 * term of the scaled down test constraint, then the test constraint
4267 * is known to hold and no explicit evaluation is required.
4268 * This is essentially the Omega test.
4270 * If the test constraint consists of only a constant term, then
4271 * it is sufficient to look at the sign of this constant term.
4273 static isl_bool
int_between_bounds(__isl_keep isl_basic_map
*bmap
, int i
,
4274 int l
, int u
, struct test_ineq_data
*data
)
4276 unsigned offset
, n_div
;
4277 offset
= isl_basic_map_offset(bmap
, isl_dim_div
);
4278 n_div
= isl_basic_map_dim(bmap
, isl_dim_div
);
4280 isl_int_gcd(data
->g
,
4281 bmap
->ineq
[l
][offset
+ i
], bmap
->ineq
[u
][offset
+ i
]);
4282 isl_int_divexact(data
->fl
, bmap
->ineq
[l
][offset
+ i
], data
->g
);
4283 isl_int_divexact(data
->fu
, bmap
->ineq
[u
][offset
+ i
], data
->g
);
4284 isl_int_neg(data
->fu
, data
->fu
);
4285 isl_seq_combine(data
->v
->el
, data
->fl
, bmap
->ineq
[u
],
4286 data
->fu
, bmap
->ineq
[l
], offset
+ n_div
);
4287 isl_int_mul(data
->g
, data
->g
, data
->fl
);
4288 isl_int_mul(data
->g
, data
->g
, data
->fu
);
4289 isl_int_sub(data
->g
, data
->g
, data
->fl
);
4290 isl_int_sub(data
->g
, data
->g
, data
->fu
);
4291 isl_int_add_ui(data
->g
, data
->g
, 1);
4292 isl_int_sub(data
->fl
, data
->v
->el
[0], data
->g
);
4294 isl_seq_gcd(data
->v
->el
+ 1, offset
- 1 + n_div
, &data
->g
);
4295 if (isl_int_is_zero(data
->g
))
4296 return isl_int_is_nonneg(data
->fl
);
4297 if (isl_int_is_one(data
->g
)) {
4298 isl_int_set(data
->v
->el
[0], data
->fl
);
4299 return test_ineq_is_satisfied(bmap
, data
);
4301 isl_int_fdiv_q(data
->fl
, data
->fl
, data
->g
);
4302 isl_int_fdiv_q(data
->v
->el
[0], data
->v
->el
[0], data
->g
);
4303 if (isl_int_eq(data
->fl
, data
->v
->el
[0]))
4304 return isl_bool_true
;
4305 isl_int_set(data
->v
->el
[0], data
->fl
);
4306 isl_seq_scale_down(data
->v
->el
+ 1, data
->v
->el
+ 1, data
->g
,
4307 offset
- 1 + n_div
);
4309 return test_ineq_is_satisfied(bmap
, data
);
4312 /* Remove more kinds of divs that are not strictly needed.
4313 * In particular, if all pairs of lower and upper bounds on a div
4314 * are such that they allow at least one integer value of the div,
4315 * then we can eliminate the div using Fourier-Motzkin without
4316 * introducing any spurious solutions.
4318 * If at least one of the two constraints has a unit coefficient for the div,
4319 * then the presence of such a value is guaranteed so there is no need to check.
4320 * In particular, the value attained by the bound with unit coefficient
4321 * can serve as this intermediate value.
4323 static struct isl_basic_map
*drop_more_redundant_divs(
4324 struct isl_basic_map
*bmap
, int *pairs
, int n
)
4327 struct test_ineq_data data
= { NULL
, NULL
};
4328 unsigned off
, n_div
;
4331 isl_int_init(data
.g
);
4332 isl_int_init(data
.fl
);
4333 isl_int_init(data
.fu
);
4338 ctx
= isl_basic_map_get_ctx(bmap
);
4339 off
= isl_basic_map_offset(bmap
, isl_dim_div
);
4340 n_div
= isl_basic_map_dim(bmap
, isl_dim_div
);
4341 data
.v
= isl_vec_alloc(ctx
, off
+ n_div
);
4350 for (i
= 0; i
< n_div
; ++i
) {
4353 if (best
>= 0 && pairs
[best
] <= pairs
[i
])
4359 for (l
= 0; l
< bmap
->n_ineq
; ++l
) {
4360 if (!isl_int_is_pos(bmap
->ineq
[l
][off
+ i
]))
4362 if (isl_int_is_one(bmap
->ineq
[l
][off
+ i
]))
4364 for (u
= 0; u
< bmap
->n_ineq
; ++u
) {
4365 if (!isl_int_is_neg(bmap
->ineq
[u
][off
+ i
]))
4367 if (isl_int_is_negone(bmap
->ineq
[u
][off
+ i
]))
4369 has_int
= int_between_bounds(bmap
, i
, l
, u
,
4373 if (data
.tab
&& data
.tab
->empty
)
4378 if (u
< bmap
->n_ineq
)
4381 if (data
.tab
&& data
.tab
->empty
) {
4382 bmap
= isl_basic_map_set_to_empty(bmap
);
4385 if (l
== bmap
->n_ineq
) {
4393 test_ineq_data_clear(&data
);
4400 bmap
= isl_basic_map_remove_dims(bmap
, isl_dim_div
, remove
, 1);
4401 return isl_basic_map_drop_redundant_divs(bmap
);
4404 isl_basic_map_free(bmap
);
4405 test_ineq_data_clear(&data
);
4409 /* Given a pair of divs div1 and div2 such that, except for the lower bound l
4410 * and the upper bound u, div1 always occurs together with div2 in the form
4411 * (div1 + m div2), where m is the constant range on the variable div1
4412 * allowed by l and u, replace the pair div1 and div2 by a single
4413 * div that is equal to div1 + m div2.
4415 * The new div will appear in the location that contains div2.
4416 * We need to modify all constraints that contain
4417 * div2 = (div - div1) / m
4418 * The coefficient of div2 is known to be equal to 1 or -1.
4419 * (If a constraint does not contain div2, it will also not contain div1.)
4420 * If the constraint also contains div1, then we know they appear
4421 * as f (div1 + m div2) and we can simply replace (div1 + m div2) by div,
4422 * i.e., the coefficient of div is f.
4424 * Otherwise, we first need to introduce div1 into the constraint.
4433 * A lower bound on div2
4437 * can be replaced by
4439 * m div2 + div1 + m t + f >= 0
4445 * can be replaced by
4447 * -(m div2 + div1) + m t + f' >= 0
4449 * These constraint are those that we would obtain from eliminating
4450 * div1 using Fourier-Motzkin.
4452 * After all constraints have been modified, we drop the lower and upper
4453 * bound and then drop div1.
4455 static struct isl_basic_map
*coalesce_divs(struct isl_basic_map
*bmap
,
4456 unsigned div1
, unsigned div2
, unsigned l
, unsigned u
)
4460 unsigned dim
, total
;
4463 ctx
= isl_basic_map_get_ctx(bmap
);
4465 dim
= isl_space_dim(bmap
->dim
, isl_dim_all
);
4466 total
= 1 + dim
+ bmap
->n_div
;
4469 isl_int_add(m
, bmap
->ineq
[l
][0], bmap
->ineq
[u
][0]);
4470 isl_int_add_ui(m
, m
, 1);
4472 for (i
= 0; i
< bmap
->n_ineq
; ++i
) {
4473 if (i
== l
|| i
== u
)
4475 if (isl_int_is_zero(bmap
->ineq
[i
][1 + dim
+ div2
]))
4477 if (isl_int_is_zero(bmap
->ineq
[i
][1 + dim
+ div1
])) {
4478 if (isl_int_is_pos(bmap
->ineq
[i
][1 + dim
+ div2
]))
4479 isl_seq_combine(bmap
->ineq
[i
], m
, bmap
->ineq
[i
],
4480 ctx
->one
, bmap
->ineq
[l
], total
);
4482 isl_seq_combine(bmap
->ineq
[i
], m
, bmap
->ineq
[i
],
4483 ctx
->one
, bmap
->ineq
[u
], total
);
4485 isl_int_set(bmap
->ineq
[i
][1 + dim
+ div2
],
4486 bmap
->ineq
[i
][1 + dim
+ div1
]);
4487 isl_int_set_si(bmap
->ineq
[i
][1 + dim
+ div1
], 0);
4492 isl_basic_map_drop_inequality(bmap
, l
);
4493 isl_basic_map_drop_inequality(bmap
, u
);
4495 isl_basic_map_drop_inequality(bmap
, u
);
4496 isl_basic_map_drop_inequality(bmap
, l
);
4498 bmap
= isl_basic_map_drop_div(bmap
, div1
);
4502 /* First check if we can coalesce any pair of divs and
4503 * then continue with dropping more redundant divs.
4505 * We loop over all pairs of lower and upper bounds on a div
4506 * with coefficient 1 and -1, respectively, check if there
4507 * is any other div "c" with which we can coalesce the div
4508 * and if so, perform the coalescing.
4510 static struct isl_basic_map
*coalesce_or_drop_more_redundant_divs(
4511 struct isl_basic_map
*bmap
, int *pairs
, int n
)
4516 dim
= isl_space_dim(bmap
->dim
, isl_dim_all
);
4518 for (i
= 0; i
< bmap
->n_div
; ++i
) {
4521 for (l
= 0; l
< bmap
->n_ineq
; ++l
) {
4522 if (!isl_int_is_one(bmap
->ineq
[l
][1 + dim
+ i
]))
4524 for (u
= 0; u
< bmap
->n_ineq
; ++u
) {
4527 if (!isl_int_is_negone(bmap
->ineq
[u
][1+dim
+i
]))
4529 c
= div_find_coalesce(bmap
, pairs
, i
, l
, u
);
4533 bmap
= coalesce_divs(bmap
, i
, c
, l
, u
);
4534 return isl_basic_map_drop_redundant_divs(bmap
);
4539 if (ISL_F_ISSET(bmap
, ISL_BASIC_MAP_EMPTY
)) {
4544 return drop_more_redundant_divs(bmap
, pairs
, n
);
4547 /* Are the "n" coefficients starting at "first" of inequality constraints
4548 * "i" and "j" of "bmap" equal to each other?
4550 static int is_parallel_part(__isl_keep isl_basic_map
*bmap
, int i
, int j
,
4553 return isl_seq_eq(bmap
->ineq
[i
] + first
, bmap
->ineq
[j
] + first
, n
);
4556 /* Are the "n" coefficients starting at "first" of inequality constraints
4557 * "i" and "j" of "bmap" opposite to each other?
4559 static int is_opposite_part(__isl_keep isl_basic_map
*bmap
, int i
, int j
,
4562 return isl_seq_is_neg(bmap
->ineq
[i
] + first
, bmap
->ineq
[j
] + first
, n
);
4565 /* Are inequality constraints "i" and "j" of "bmap" opposite to each other,
4566 * apart from the constant term?
4568 static isl_bool
is_opposite(__isl_keep isl_basic_map
*bmap
, int i
, int j
)
4572 total
= isl_basic_map_dim(bmap
, isl_dim_all
);
4573 return is_opposite_part(bmap
, i
, j
, 1, total
);
4576 /* Are inequality constraints "i" and "j" of "bmap" equal to each other,
4577 * apart from the constant term and the coefficient at position "pos"?
4579 static int is_parallel_except(__isl_keep isl_basic_map
*bmap
, int i
, int j
,
4584 total
= isl_basic_map_dim(bmap
, isl_dim_all
);
4585 return is_parallel_part(bmap
, i
, j
, 1, pos
- 1) &&
4586 is_parallel_part(bmap
, i
, j
, pos
+ 1, total
- pos
);
4589 /* Are inequality constraints "i" and "j" of "bmap" opposite to each other,
4590 * apart from the constant term and the coefficient at position "pos"?
4592 static int is_opposite_except(__isl_keep isl_basic_map
*bmap
, int i
, int j
,
4597 total
= isl_basic_map_dim(bmap
, isl_dim_all
);
4598 return is_opposite_part(bmap
, i
, j
, 1, pos
- 1) &&
4599 is_opposite_part(bmap
, i
, j
, pos
+ 1, total
- pos
);
4602 /* Restart isl_basic_map_drop_redundant_divs after "bmap" has
4603 * been modified, simplying it if "simplify" is set.
4604 * Free the temporary data structure "pairs" that was associated
4605 * to the old version of "bmap".
4607 static __isl_give isl_basic_map
*drop_redundant_divs_again(
4608 __isl_take isl_basic_map
*bmap
, __isl_take
int *pairs
, int simplify
)
4611 bmap
= isl_basic_map_simplify(bmap
);
4613 return isl_basic_map_drop_redundant_divs(bmap
);
4616 /* Is "div" the single unknown existentially quantified variable
4617 * in inequality constraint "ineq" of "bmap"?
4618 * "div" is known to have a non-zero coefficient in "ineq".
4620 static isl_bool
single_unknown(__isl_keep isl_basic_map
*bmap
, int ineq
,
4624 unsigned n_div
, o_div
;
4627 known
= isl_basic_map_div_is_known(bmap
, div
);
4628 if (known
< 0 || known
)
4629 return isl_bool_not(known
);
4630 n_div
= isl_basic_map_dim(bmap
, isl_dim_div
);
4632 return isl_bool_true
;
4633 o_div
= isl_basic_map_offset(bmap
, isl_dim_div
);
4634 for (i
= 0; i
< n_div
; ++i
) {
4639 if (isl_int_is_zero(bmap
->ineq
[ineq
][o_div
+ i
]))
4641 known
= isl_basic_map_div_is_known(bmap
, i
);
4642 if (known
< 0 || !known
)
4646 return isl_bool_true
;
4649 /* Does integer division "div" have coefficient 1 in inequality constraint
4652 static isl_bool
has_coef_one(__isl_keep isl_basic_map
*bmap
, int div
, int ineq
)
4656 o_div
= isl_basic_map_offset(bmap
, isl_dim_div
);
4657 if (isl_int_is_one(bmap
->ineq
[ineq
][o_div
+ div
]))
4658 return isl_bool_true
;
4660 return isl_bool_false
;
4663 /* Turn inequality constraint "ineq" of "bmap" into an equality and
4664 * then try and drop redundant divs again,
4665 * freeing the temporary data structure "pairs" that was associated
4666 * to the old version of "bmap".
4668 static __isl_give isl_basic_map
*set_eq_and_try_again(
4669 __isl_take isl_basic_map
*bmap
, int ineq
, __isl_take
int *pairs
)
4671 bmap
= isl_basic_map_cow(bmap
);
4672 isl_basic_map_inequality_to_equality(bmap
, ineq
);
4673 return drop_redundant_divs_again(bmap
, pairs
, 1);
4676 /* Drop the integer division at position "div", along with the two
4677 * inequality constraints "ineq1" and "ineq2" in which it appears
4678 * from "bmap" and then try and drop redundant divs again,
4679 * freeing the temporary data structure "pairs" that was associated
4680 * to the old version of "bmap".
4682 static __isl_give isl_basic_map
*drop_div_and_try_again(
4683 __isl_take isl_basic_map
*bmap
, int div
, int ineq1
, int ineq2
,
4684 __isl_take
int *pairs
)
4686 if (ineq1
> ineq2
) {
4687 isl_basic_map_drop_inequality(bmap
, ineq1
);
4688 isl_basic_map_drop_inequality(bmap
, ineq2
);
4690 isl_basic_map_drop_inequality(bmap
, ineq2
);
4691 isl_basic_map_drop_inequality(bmap
, ineq1
);
4693 bmap
= isl_basic_map_drop_div(bmap
, div
);
4694 return drop_redundant_divs_again(bmap
, pairs
, 0);
4697 /* Given two inequality constraints
4699 * f(x) + n d + c >= 0, (ineq)
4701 * with d the variable at position "pos", and
4703 * f(x) + c0 >= 0, (lower)
4705 * compute the maximal value of the lower bound ceil((-f(x) - c)/n)
4706 * determined by the first constraint.
4713 static void lower_bound_from_parallel(__isl_keep isl_basic_map
*bmap
,
4714 int ineq
, int lower
, int pos
, isl_int
*l
)
4716 isl_int_neg(*l
, bmap
->ineq
[ineq
][0]);
4717 isl_int_add(*l
, *l
, bmap
->ineq
[lower
][0]);
4718 isl_int_cdiv_q(*l
, *l
, bmap
->ineq
[ineq
][pos
]);
4721 /* Given two inequality constraints
4723 * f(x) + n d + c >= 0, (ineq)
4725 * with d the variable at position "pos", and
4727 * -f(x) - c0 >= 0, (upper)
4729 * compute the minimal value of the lower bound ceil((-f(x) - c)/n)
4730 * determined by the first constraint.
4737 static void lower_bound_from_opposite(__isl_keep isl_basic_map
*bmap
,
4738 int ineq
, int upper
, int pos
, isl_int
*u
)
4740 isl_int_neg(*u
, bmap
->ineq
[ineq
][0]);
4741 isl_int_sub(*u
, *u
, bmap
->ineq
[upper
][0]);
4742 isl_int_cdiv_q(*u
, *u
, bmap
->ineq
[ineq
][pos
]);
4745 /* Given a lower bound constraint "ineq" on "div" in "bmap",
4746 * does the corresponding lower bound have a fixed value in "bmap"?
4748 * In particular, "ineq" is of the form
4750 * f(x) + n d + c >= 0
4752 * with n > 0, c the constant term and
4753 * d the existentially quantified variable "div".
4754 * That is, the lower bound is
4756 * ceil((-f(x) - c)/n)
4758 * Look for a pair of constraints
4763 * i.e., -c1 <= -f(x) <= c0, that fix ceil((-f(x) - c)/n) to a constant value.
4764 * That is, check that
4766 * ceil((-c1 - c)/n) = ceil((c0 - c)/n)
4768 * If so, return the index of inequality f(x) + c0 >= 0.
4769 * Otherwise, return -1.
4771 static int lower_bound_is_cst(__isl_keep isl_basic_map
*bmap
, int div
, int ineq
)
4774 int lower
= -1, upper
= -1;
4779 o_div
= isl_basic_map_offset(bmap
, isl_dim_div
);
4780 for (i
= 0; i
< bmap
->n_ineq
&& (lower
< 0 || upper
< 0); ++i
) {
4783 if (!isl_int_is_zero(bmap
->ineq
[i
][o_div
+ div
]))
4786 is_parallel_except(bmap
, ineq
, i
, o_div
+ div
)) {
4791 is_opposite_except(bmap
, ineq
, i
, o_div
+ div
)) {
4796 if (lower
< 0 || upper
< 0)
4802 lower_bound_from_parallel(bmap
, ineq
, lower
, o_div
+ div
, &l
);
4803 lower_bound_from_opposite(bmap
, ineq
, upper
, o_div
+ div
, &u
);
4805 equal
= isl_int_eq(l
, u
);
4810 return equal
? lower
: -1;
4813 /* Given a lower bound constraint "ineq" on the existentially quantified
4814 * variable "div", such that the corresponding lower bound has
4815 * a fixed value in "bmap", assign this fixed value to the variable and
4816 * then try and drop redundant divs again,
4817 * freeing the temporary data structure "pairs" that was associated
4818 * to the old version of "bmap".
4819 * "lower" determines the constant value for the lower bound.
4821 * In particular, "ineq" is of the form
4823 * f(x) + n d + c >= 0,
4825 * while "lower" is of the form
4829 * The lower bound is ceil((-f(x) - c)/n) and its constant value
4830 * is ceil((c0 - c)/n).
4832 static __isl_give isl_basic_map
*fix_cst_lower(__isl_take isl_basic_map
*bmap
,
4833 int div
, int ineq
, int lower
, int *pairs
)
4840 o_div
= isl_basic_map_offset(bmap
, isl_dim_div
);
4841 lower_bound_from_parallel(bmap
, ineq
, lower
, o_div
+ div
, &c
);
4842 bmap
= isl_basic_map_fix(bmap
, isl_dim_div
, div
, c
);
4847 return isl_basic_map_drop_redundant_divs(bmap
);
4850 /* Remove divs that are not strictly needed based on the inequality
4852 * In particular, if a div only occurs positively (or negatively)
4853 * in constraints, then it can simply be dropped.
4854 * Also, if a div occurs in only two constraints and if moreover
4855 * those two constraints are opposite to each other, except for the constant
4856 * term and if the sum of the constant terms is such that for any value
4857 * of the other values, there is always at least one integer value of the
4858 * div, i.e., if one plus this sum is greater than or equal to
4859 * the (absolute value) of the coefficient of the div in the constraints,
4860 * then we can also simply drop the div.
4862 * If an existentially quantified variable does not have an explicit
4863 * representation, appears in only a single lower bound that does not
4864 * involve any other such existentially quantified variables and appears
4865 * in this lower bound with coefficient 1,
4866 * then fix the variable to the value of the lower bound. That is,
4867 * turn the inequality into an equality.
4868 * If for any value of the other variables, there is any value
4869 * for the existentially quantified variable satisfying the constraints,
4870 * then this lower bound also satisfies the constraints.
4871 * It is therefore safe to pick this lower bound.
4873 * The same reasoning holds even if the coefficient is not one.
4874 * However, fixing the variable to the value of the lower bound may
4875 * in general introduce an extra integer division, in which case
4876 * it may be better to pick another value.
4877 * If this integer division has a known constant value, then plugging
4878 * in this constant value removes the existentially quantified variable
4879 * completely. In particular, if the lower bound is of the form
4880 * ceil((-f(x) - c)/n) and there are two constraints, f(x) + c0 >= 0 and
4881 * -f(x) + c1 >= 0 such that ceil((-c1 - c)/n) = ceil((c0 - c)/n),
4882 * then the existentially quantified variable can be assigned this
4885 * We skip divs that appear in equalities or in the definition of other divs.
4886 * Divs that appear in the definition of other divs usually occur in at least
4887 * 4 constraints, but the constraints may have been simplified.
4889 * If any divs are left after these simple checks then we move on
4890 * to more complicated cases in drop_more_redundant_divs.
4892 static __isl_give isl_basic_map
*isl_basic_map_drop_redundant_divs_ineq(
4893 __isl_take isl_basic_map
*bmap
)
4902 if (bmap
->n_div
== 0)
4905 off
= isl_space_dim(bmap
->dim
, isl_dim_all
);
4906 pairs
= isl_calloc_array(bmap
->ctx
, int, bmap
->n_div
);
4910 for (i
= 0; i
< bmap
->n_div
; ++i
) {
4912 int last_pos
, last_neg
;
4915 isl_bool opp
, set_div
;
4917 defined
= !isl_int_is_zero(bmap
->div
[i
][0]);
4918 for (j
= i
; j
< bmap
->n_div
; ++j
)
4919 if (!isl_int_is_zero(bmap
->div
[j
][1 + 1 + off
+ i
]))
4921 if (j
< bmap
->n_div
)
4923 for (j
= 0; j
< bmap
->n_eq
; ++j
)
4924 if (!isl_int_is_zero(bmap
->eq
[j
][1 + off
+ i
]))
4930 for (j
= 0; j
< bmap
->n_ineq
; ++j
) {
4931 if (isl_int_is_pos(bmap
->ineq
[j
][1 + off
+ i
])) {
4935 if (isl_int_is_neg(bmap
->ineq
[j
][1 + off
+ i
])) {
4940 pairs
[i
] = pos
* neg
;
4941 if (pairs
[i
] == 0) {
4942 for (j
= bmap
->n_ineq
- 1; j
>= 0; --j
)
4943 if (!isl_int_is_zero(bmap
->ineq
[j
][1+off
+i
]))
4944 isl_basic_map_drop_inequality(bmap
, j
);
4945 bmap
= isl_basic_map_drop_div(bmap
, i
);
4946 return drop_redundant_divs_again(bmap
, pairs
, 0);
4949 opp
= isl_bool_false
;
4951 opp
= is_opposite(bmap
, last_pos
, last_neg
);
4956 isl_bool single
, one
;
4960 single
= single_unknown(bmap
, last_pos
, i
);
4965 one
= has_coef_one(bmap
, i
, last_pos
);
4969 return set_eq_and_try_again(bmap
, last_pos
,
4971 lower
= lower_bound_is_cst(bmap
, i
, last_pos
);
4973 return fix_cst_lower(bmap
, i
, last_pos
, lower
,
4978 isl_int_add(bmap
->ineq
[last_pos
][0],
4979 bmap
->ineq
[last_pos
][0], bmap
->ineq
[last_neg
][0]);
4980 isl_int_add_ui(bmap
->ineq
[last_pos
][0],
4981 bmap
->ineq
[last_pos
][0], 1);
4982 redundant
= isl_int_ge(bmap
->ineq
[last_pos
][0],
4983 bmap
->ineq
[last_pos
][1+off
+i
]);
4984 isl_int_sub_ui(bmap
->ineq
[last_pos
][0],
4985 bmap
->ineq
[last_pos
][0], 1);
4986 isl_int_sub(bmap
->ineq
[last_pos
][0],
4987 bmap
->ineq
[last_pos
][0], bmap
->ineq
[last_neg
][0]);
4989 return drop_div_and_try_again(bmap
, i
,
4990 last_pos
, last_neg
, pairs
);
4992 set_div
= isl_bool_false
;
4994 set_div
= ok_to_set_div_from_bound(bmap
, i
, last_pos
);
4996 return isl_basic_map_free(bmap
);
4998 bmap
= set_div_from_lower_bound(bmap
, i
, last_pos
);
4999 return drop_redundant_divs_again(bmap
, pairs
, 1);
5006 return coalesce_or_drop_more_redundant_divs(bmap
, pairs
, n
);
5012 isl_basic_map_free(bmap
);
5016 /* Consider the coefficients at "c" as a row vector and replace
5017 * them with their product with "T". "T" is assumed to be a square matrix.
5019 static isl_stat
preimage(isl_int
*c
, __isl_keep isl_mat
*T
)
5026 return isl_stat_error
;
5027 n
= isl_mat_rows(T
);
5028 if (isl_seq_first_non_zero(c
, n
) == -1)
5030 ctx
= isl_mat_get_ctx(T
);
5031 v
= isl_vec_alloc(ctx
, n
);
5033 return isl_stat_error
;
5034 isl_seq_swp_or_cpy(v
->el
, c
, n
);
5035 v
= isl_vec_mat_product(v
, isl_mat_copy(T
));
5037 return isl_stat_error
;
5038 isl_seq_swp_or_cpy(c
, v
->el
, n
);
5044 /* Plug in T for the variables in "bmap" starting at "pos".
5045 * T is a linear unimodular matrix, i.e., without constant term.
5047 static __isl_give isl_basic_map
*isl_basic_map_preimage_vars(
5048 __isl_take isl_basic_map
*bmap
, unsigned pos
, __isl_take isl_mat
*T
)
5053 bmap
= isl_basic_map_cow(bmap
);
5057 n
= isl_mat_cols(T
);
5058 if (n
!= isl_mat_rows(T
))
5059 isl_die(isl_mat_get_ctx(T
), isl_error_invalid
,
5060 "expecting square matrix", goto error
);
5062 total
= isl_basic_map_dim(bmap
, isl_dim_all
);
5063 if (pos
+ n
> total
|| pos
+ n
< pos
)
5064 isl_die(isl_mat_get_ctx(T
), isl_error_invalid
,
5065 "invalid range", goto error
);
5067 for (i
= 0; i
< bmap
->n_eq
; ++i
)
5068 if (preimage(bmap
->eq
[i
] + 1 + pos
, T
) < 0)
5070 for (i
= 0; i
< bmap
->n_ineq
; ++i
)
5071 if (preimage(bmap
->ineq
[i
] + 1 + pos
, T
) < 0)
5073 for (i
= 0; i
< bmap
->n_div
; ++i
) {
5074 if (isl_basic_map_div_is_marked_unknown(bmap
, i
))
5076 if (preimage(bmap
->div
[i
] + 1 + 1 + pos
, T
) < 0)
5083 isl_basic_map_free(bmap
);
5088 /* Remove divs that are not strictly needed.
5090 * First look for an equality constraint involving two or more
5091 * existentially quantified variables without an explicit
5092 * representation. Replace the combination that appears
5093 * in the equality constraint by a single existentially quantified
5094 * variable such that the equality can be used to derive
5095 * an explicit representation for the variable.
5096 * If there are no more such equality constraints, then continue
5097 * with isl_basic_map_drop_redundant_divs_ineq.
5099 * In particular, if the equality constraint is of the form
5101 * f(x) + \sum_i c_i a_i = 0
5103 * with a_i existentially quantified variable without explicit
5104 * representation, then apply a transformation on the existentially
5105 * quantified variables to turn the constraint into
5109 * with g the gcd of the c_i.
5110 * In order to easily identify which existentially quantified variables
5111 * have a complete explicit representation, i.e., without being defined
5112 * in terms of other existentially quantified variables without
5113 * an explicit representation, the existentially quantified variables
5116 * The variable transformation is computed by extending the row
5117 * [c_1/g ... c_n/g] to a unimodular matrix, obtaining the transformation
5119 * [a_1'] [c_1/g ... c_n/g] [ a_1 ]
5124 * with [c_1/g ... c_n/g] representing the first row of U.
5125 * The inverse of U is then plugged into the original constraints.
5126 * The call to isl_basic_map_simplify makes sure the explicit
5127 * representation for a_1' is extracted from the equality constraint.
5129 __isl_give isl_basic_map
*isl_basic_map_drop_redundant_divs(
5130 __isl_take isl_basic_map
*bmap
)
5134 unsigned o_div
, n_div
;
5141 if (isl_basic_map_divs_known(bmap
))
5142 return isl_basic_map_drop_redundant_divs_ineq(bmap
);
5143 if (bmap
->n_eq
== 0)
5144 return isl_basic_map_drop_redundant_divs_ineq(bmap
);
5145 bmap
= isl_basic_map_sort_divs(bmap
);
5149 first
= isl_basic_map_first_unknown_div(bmap
);
5151 return isl_basic_map_free(bmap
);
5153 o_div
= isl_basic_map_offset(bmap
, isl_dim_div
);
5154 n_div
= isl_basic_map_dim(bmap
, isl_dim_div
);
5156 for (i
= 0; i
< bmap
->n_eq
; ++i
) {
5157 l
= isl_seq_first_non_zero(bmap
->eq
[i
] + o_div
+ first
,
5162 if (isl_seq_first_non_zero(bmap
->eq
[i
] + o_div
+ l
+ 1,
5163 n_div
- (l
+ 1)) == -1)
5167 if (i
>= bmap
->n_eq
)
5168 return isl_basic_map_drop_redundant_divs_ineq(bmap
);
5170 ctx
= isl_basic_map_get_ctx(bmap
);
5171 T
= isl_mat_alloc(ctx
, n_div
- l
, n_div
- l
);
5173 return isl_basic_map_free(bmap
);
5174 isl_seq_cpy(T
->row
[0], bmap
->eq
[i
] + o_div
+ l
, n_div
- l
);
5175 T
= isl_mat_normalize_row(T
, 0);
5176 T
= isl_mat_unimodular_complete(T
, 1);
5177 T
= isl_mat_right_inverse(T
);
5179 for (i
= l
; i
< n_div
; ++i
)
5180 bmap
= isl_basic_map_mark_div_unknown(bmap
, i
);
5181 bmap
= isl_basic_map_preimage_vars(bmap
, o_div
- 1 + l
, T
);
5182 bmap
= isl_basic_map_simplify(bmap
);
5184 return isl_basic_map_drop_redundant_divs(bmap
);
5187 struct isl_basic_set
*isl_basic_set_drop_redundant_divs(
5188 struct isl_basic_set
*bset
)
5190 isl_basic_map
*bmap
= bset_to_bmap(bset
);
5191 return bset_from_bmap(isl_basic_map_drop_redundant_divs(bmap
));
5194 /* Does "bmap" satisfy any equality that involves more than 2 variables
5195 * and/or has coefficients different from -1 and 1?
5197 static int has_multiple_var_equality(__isl_keep isl_basic_map
*bmap
)
5202 total
= isl_basic_map_dim(bmap
, isl_dim_all
);
5204 for (i
= 0; i
< bmap
->n_eq
; ++i
) {
5207 j
= isl_seq_first_non_zero(bmap
->eq
[i
] + 1, total
);
5210 if (!isl_int_is_one(bmap
->eq
[i
][1 + j
]) &&
5211 !isl_int_is_negone(bmap
->eq
[i
][1 + j
]))
5215 k
= isl_seq_first_non_zero(bmap
->eq
[i
] + 1 + j
, total
- j
);
5219 if (!isl_int_is_one(bmap
->eq
[i
][1 + j
]) &&
5220 !isl_int_is_negone(bmap
->eq
[i
][1 + j
]))
5224 k
= isl_seq_first_non_zero(bmap
->eq
[i
] + 1 + j
, total
- j
);
5232 /* Remove any common factor g from the constraint coefficients in "v".
5233 * The constant term is stored in the first position and is replaced
5234 * by floor(c/g). If any common factor is removed and if this results
5235 * in a tightening of the constraint, then set *tightened.
5237 static __isl_give isl_vec
*normalize_constraint(__isl_take isl_vec
*v
,
5244 ctx
= isl_vec_get_ctx(v
);
5245 isl_seq_gcd(v
->el
+ 1, v
->size
- 1, &ctx
->normalize_gcd
);
5246 if (isl_int_is_zero(ctx
->normalize_gcd
))
5248 if (isl_int_is_one(ctx
->normalize_gcd
))
5253 if (tightened
&& !isl_int_is_divisible_by(v
->el
[0], ctx
->normalize_gcd
))
5255 isl_int_fdiv_q(v
->el
[0], v
->el
[0], ctx
->normalize_gcd
);
5256 isl_seq_scale_down(v
->el
+ 1, v
->el
+ 1, ctx
->normalize_gcd
,
5261 /* If "bmap" is an integer set that satisfies any equality involving
5262 * more than 2 variables and/or has coefficients different from -1 and 1,
5263 * then use variable compression to reduce the coefficients by removing
5264 * any (hidden) common factor.
5265 * In particular, apply the variable compression to each constraint,
5266 * factor out any common factor in the non-constant coefficients and
5267 * then apply the inverse of the compression.
5268 * At the end, we mark the basic map as having reduced constants.
5269 * If this flag is still set on the next invocation of this function,
5270 * then we skip the computation.
5272 * Removing a common factor may result in a tightening of some of
5273 * the constraints. If this happens, then we may end up with two
5274 * opposite inequalities that can be replaced by an equality.
5275 * We therefore call isl_basic_map_detect_inequality_pairs,
5276 * which checks for such pairs of inequalities as well as eliminate_divs_eq
5277 * and isl_basic_map_gauss if such a pair was found.
5279 __isl_give isl_basic_map
*isl_basic_map_reduce_coefficients(
5280 __isl_take isl_basic_map
*bmap
)
5285 isl_mat
*eq
, *T
, *T2
;
5291 if (ISL_F_ISSET(bmap
, ISL_BASIC_MAP_REDUCED_COEFFICIENTS
))
5293 if (isl_basic_map_is_rational(bmap
))
5295 if (bmap
->n_eq
== 0)
5297 if (!has_multiple_var_equality(bmap
))
5300 total
= isl_basic_map_dim(bmap
, isl_dim_all
);
5301 ctx
= isl_basic_map_get_ctx(bmap
);
5302 v
= isl_vec_alloc(ctx
, 1 + total
);
5304 return isl_basic_map_free(bmap
);
5306 eq
= isl_mat_sub_alloc6(ctx
, bmap
->eq
, 0, bmap
->n_eq
, 0, 1 + total
);
5307 T
= isl_mat_variable_compression(eq
, &T2
);
5310 if (T
->n_col
== 0) {
5314 return isl_basic_map_set_to_empty(bmap
);
5318 for (i
= 0; i
< bmap
->n_ineq
; ++i
) {
5319 isl_seq_cpy(v
->el
, bmap
->ineq
[i
], 1 + total
);
5320 v
= isl_vec_mat_product(v
, isl_mat_copy(T
));
5321 v
= normalize_constraint(v
, &tightened
);
5322 v
= isl_vec_mat_product(v
, isl_mat_copy(T2
));
5325 isl_seq_cpy(bmap
->ineq
[i
], v
->el
, 1 + total
);
5332 ISL_F_SET(bmap
, ISL_BASIC_MAP_REDUCED_COEFFICIENTS
);
5337 bmap
= isl_basic_map_detect_inequality_pairs(bmap
, &progress
);
5339 bmap
= eliminate_divs_eq(bmap
, &progress
);
5340 bmap
= isl_basic_map_gauss(bmap
, NULL
);
5349 return isl_basic_map_free(bmap
);
5352 /* Shift the integer division at position "div" of "bmap"
5353 * by "shift" times the variable at position "pos".
5354 * "pos" is as determined by isl_basic_map_offset, i.e., pos == 0
5355 * corresponds to the constant term.
5357 * That is, if the integer division has the form
5361 * then replace it by
5363 * floor((f(x) + shift * d * x_pos)/d) - shift * x_pos
5365 __isl_give isl_basic_map
*isl_basic_map_shift_div(
5366 __isl_take isl_basic_map
*bmap
, int div
, int pos
, isl_int shift
)
5371 if (isl_int_is_zero(shift
))
5376 total
= isl_basic_map_dim(bmap
, isl_dim_all
);
5377 total
-= isl_basic_map_dim(bmap
, isl_dim_div
);
5379 isl_int_addmul(bmap
->div
[div
][1 + pos
], shift
, bmap
->div
[div
][0]);
5381 for (i
= 0; i
< bmap
->n_eq
; ++i
) {
5382 if (isl_int_is_zero(bmap
->eq
[i
][1 + total
+ div
]))
5384 isl_int_submul(bmap
->eq
[i
][pos
],
5385 shift
, bmap
->eq
[i
][1 + total
+ div
]);
5387 for (i
= 0; i
< bmap
->n_ineq
; ++i
) {
5388 if (isl_int_is_zero(bmap
->ineq
[i
][1 + total
+ div
]))
5390 isl_int_submul(bmap
->ineq
[i
][pos
],
5391 shift
, bmap
->ineq
[i
][1 + total
+ div
]);
5393 for (i
= 0; i
< bmap
->n_div
; ++i
) {
5394 if (isl_int_is_zero(bmap
->div
[i
][0]))
5396 if (isl_int_is_zero(bmap
->div
[i
][1 + 1 + total
+ div
]))
5398 isl_int_submul(bmap
->div
[i
][1 + pos
],
5399 shift
, bmap
->div
[i
][1 + 1 + total
+ div
]);