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 __isl_give isl_basic_map
*isl_basic_map_normalize_constraints(
48 __isl_take isl_basic_map
*bmap
)
52 isl_size total
= isl_basic_map_dim(bmap
, isl_dim_all
);
55 return isl_basic_map_free(bmap
);
58 for (i
= bmap
->n_eq
- 1; i
>= 0; --i
) {
59 isl_seq_gcd(bmap
->eq
[i
]+1, total
, &gcd
);
60 if (isl_int_is_zero(gcd
)) {
61 if (!isl_int_is_zero(bmap
->eq
[i
][0])) {
62 bmap
= isl_basic_map_set_to_empty(bmap
);
65 isl_basic_map_drop_equality(bmap
, i
);
68 if (ISL_F_ISSET(bmap
, ISL_BASIC_MAP_RATIONAL
))
69 isl_int_gcd(gcd
, gcd
, bmap
->eq
[i
][0]);
70 if (isl_int_is_one(gcd
))
72 if (!isl_int_is_divisible_by(bmap
->eq
[i
][0], gcd
)) {
73 bmap
= isl_basic_map_set_to_empty(bmap
);
76 isl_seq_scale_down(bmap
->eq
[i
], bmap
->eq
[i
], gcd
, 1+total
);
79 for (i
= bmap
->n_ineq
- 1; i
>= 0; --i
) {
80 isl_seq_gcd(bmap
->ineq
[i
]+1, total
, &gcd
);
81 if (isl_int_is_zero(gcd
)) {
82 if (isl_int_is_neg(bmap
->ineq
[i
][0])) {
83 bmap
= isl_basic_map_set_to_empty(bmap
);
86 isl_basic_map_drop_inequality(bmap
, i
);
89 if (ISL_F_ISSET(bmap
, ISL_BASIC_MAP_RATIONAL
))
90 isl_int_gcd(gcd
, gcd
, bmap
->ineq
[i
][0]);
91 if (isl_int_is_one(gcd
))
93 isl_int_fdiv_q(bmap
->ineq
[i
][0], bmap
->ineq
[i
][0], gcd
);
94 isl_seq_scale_down(bmap
->ineq
[i
]+1, bmap
->ineq
[i
]+1, gcd
, total
);
101 __isl_give isl_basic_set
*isl_basic_set_normalize_constraints(
102 __isl_take isl_basic_set
*bset
)
104 isl_basic_map
*bmap
= bset_to_bmap(bset
);
105 return bset_from_bmap(isl_basic_map_normalize_constraints(bmap
));
108 /* Reduce the coefficient of the variable at position "pos"
109 * in integer division "div", such that it lies in the half-open
110 * interval (1/2,1/2], extracting any excess value from this integer division.
111 * "pos" is as determined by isl_basic_map_offset, i.e., pos == 0
112 * corresponds to the constant term.
114 * That is, the integer division is of the form
116 * floor((... + (c * d + r) * x_pos + ...)/d)
118 * with -d < 2 * r <= d.
121 * floor((... + r * x_pos + ...)/d) + c * x_pos
123 * If 2 * ((c * d + r) % d) <= d, then c = floor((c * d + r)/d).
124 * Otherwise, c = floor((c * d + r)/d) + 1.
126 * This is the same normalization that is performed by isl_aff_floor.
128 static __isl_give isl_basic_map
*reduce_coefficient_in_div(
129 __isl_take isl_basic_map
*bmap
, int div
, int pos
)
135 isl_int_fdiv_r(shift
, bmap
->div
[div
][1 + pos
], bmap
->div
[div
][0]);
136 isl_int_mul_ui(shift
, shift
, 2);
137 add_one
= isl_int_gt(shift
, bmap
->div
[div
][0]);
138 isl_int_fdiv_q(shift
, bmap
->div
[div
][1 + pos
], bmap
->div
[div
][0]);
140 isl_int_add_ui(shift
, shift
, 1);
141 isl_int_neg(shift
, shift
);
142 bmap
= isl_basic_map_shift_div(bmap
, div
, pos
, shift
);
143 isl_int_clear(shift
);
148 /* Does the coefficient of the variable at position "pos"
149 * in integer division "div" need to be reduced?
150 * That is, does it lie outside the half-open interval (1/2,1/2]?
151 * The coefficient c/d lies outside this interval if abs(2 * c) >= d and
154 static isl_bool
needs_reduction(__isl_keep isl_basic_map
*bmap
, int div
,
159 if (isl_int_is_zero(bmap
->div
[div
][1 + pos
]))
160 return isl_bool_false
;
162 isl_int_mul_ui(bmap
->div
[div
][1 + pos
], bmap
->div
[div
][1 + pos
], 2);
163 r
= isl_int_abs_ge(bmap
->div
[div
][1 + pos
], bmap
->div
[div
][0]) &&
164 !isl_int_eq(bmap
->div
[div
][1 + pos
], bmap
->div
[div
][0]);
165 isl_int_divexact_ui(bmap
->div
[div
][1 + pos
],
166 bmap
->div
[div
][1 + pos
], 2);
171 /* Reduce the coefficients (including the constant term) of
172 * integer division "div", if needed.
173 * In particular, make sure all coefficients lie in
174 * the half-open interval (1/2,1/2].
176 static __isl_give isl_basic_map
*reduce_div_coefficients_of_div(
177 __isl_take isl_basic_map
*bmap
, int div
)
182 total
= isl_basic_map_dim(bmap
, isl_dim_all
);
184 return isl_basic_map_free(bmap
);
185 for (i
= 0; i
< 1 + total
; ++i
) {
188 reduce
= needs_reduction(bmap
, div
, i
);
190 return isl_basic_map_free(bmap
);
193 bmap
= reduce_coefficient_in_div(bmap
, div
, i
);
201 /* Reduce the coefficients (including the constant term) of
202 * the known integer divisions, if needed
203 * In particular, make sure all coefficients lie in
204 * the half-open interval (1/2,1/2].
206 static __isl_give isl_basic_map
*reduce_div_coefficients(
207 __isl_take isl_basic_map
*bmap
)
213 if (bmap
->n_div
== 0)
216 for (i
= 0; i
< bmap
->n_div
; ++i
) {
217 if (isl_int_is_zero(bmap
->div
[i
][0]))
219 bmap
= reduce_div_coefficients_of_div(bmap
, i
);
227 /* Remove any common factor in numerator and denominator of the div expression,
228 * not taking into account the constant term.
229 * That is, if the div is of the form
231 * floor((a + m f(x))/(m d))
235 * floor((floor(a/m) + f(x))/d)
237 * The difference {a/m}/d in the argument satisfies 0 <= {a/m}/d < 1/d
238 * and can therefore not influence the result of the floor.
240 static __isl_give isl_basic_map
*normalize_div_expression(
241 __isl_take isl_basic_map
*bmap
, int div
)
243 isl_size total
= isl_basic_map_dim(bmap
, isl_dim_all
);
244 isl_ctx
*ctx
= bmap
->ctx
;
247 return isl_basic_map_free(bmap
);
248 if (isl_int_is_zero(bmap
->div
[div
][0]))
250 isl_seq_gcd(bmap
->div
[div
] + 2, total
, &ctx
->normalize_gcd
);
251 isl_int_gcd(ctx
->normalize_gcd
, ctx
->normalize_gcd
, bmap
->div
[div
][0]);
252 if (isl_int_is_one(ctx
->normalize_gcd
))
254 isl_int_fdiv_q(bmap
->div
[div
][1], bmap
->div
[div
][1],
256 isl_int_divexact(bmap
->div
[div
][0], bmap
->div
[div
][0],
258 isl_seq_scale_down(bmap
->div
[div
] + 2, bmap
->div
[div
] + 2,
259 ctx
->normalize_gcd
, total
);
264 /* Remove any common factor in numerator and denominator of a div expression,
265 * not taking into account the constant term.
266 * That is, look for any div of the form
268 * floor((a + m f(x))/(m d))
272 * floor((floor(a/m) + f(x))/d)
274 * The difference {a/m}/d in the argument satisfies 0 <= {a/m}/d < 1/d
275 * and can therefore not influence the result of the floor.
277 static __isl_give isl_basic_map
*normalize_div_expressions(
278 __isl_take isl_basic_map
*bmap
)
284 if (bmap
->n_div
== 0)
287 for (i
= 0; i
< bmap
->n_div
; ++i
)
288 bmap
= normalize_div_expression(bmap
, i
);
293 /* Assumes divs have been ordered if keep_divs is set.
295 static __isl_give isl_basic_map
*eliminate_var_using_equality(
296 __isl_take isl_basic_map
*bmap
,
297 unsigned pos
, isl_int
*eq
, int keep_divs
, int *progress
)
304 total
= isl_basic_map_dim(bmap
, isl_dim_all
);
305 v_div
= isl_basic_map_var_offset(bmap
, isl_dim_div
);
306 if (total
< 0 || v_div
< 0)
307 return isl_basic_map_free(bmap
);
308 last_div
= isl_seq_last_non_zero(eq
+ 1 + v_div
, bmap
->n_div
);
309 for (k
= 0; k
< bmap
->n_eq
; ++k
) {
310 if (bmap
->eq
[k
] == eq
)
312 if (isl_int_is_zero(bmap
->eq
[k
][1+pos
]))
316 isl_seq_elim(bmap
->eq
[k
], eq
, 1+pos
, 1+total
, NULL
);
317 isl_seq_normalize(bmap
->ctx
, bmap
->eq
[k
], 1 + total
);
320 for (k
= 0; k
< bmap
->n_ineq
; ++k
) {
321 if (isl_int_is_zero(bmap
->ineq
[k
][1+pos
]))
325 isl_seq_elim(bmap
->ineq
[k
], eq
, 1+pos
, 1+total
, NULL
);
326 isl_seq_normalize(bmap
->ctx
, bmap
->ineq
[k
], 1 + total
);
327 ISL_F_CLR(bmap
, ISL_BASIC_MAP_NO_REDUNDANT
);
328 ISL_F_CLR(bmap
, ISL_BASIC_MAP_SORTED
);
331 for (k
= 0; k
< bmap
->n_div
; ++k
) {
332 if (isl_int_is_zero(bmap
->div
[k
][0]))
334 if (isl_int_is_zero(bmap
->div
[k
][1+1+pos
]))
338 /* We need to be careful about circular definitions,
339 * so for now we just remove the definition of div k
340 * if the equality contains any divs.
341 * If keep_divs is set, then the divs have been ordered
342 * and we can keep the definition as long as the result
345 if (last_div
== -1 || (keep_divs
&& last_div
< k
)) {
346 isl_seq_elim(bmap
->div
[k
]+1, eq
,
347 1+pos
, 1+total
, &bmap
->div
[k
][0]);
348 bmap
= normalize_div_expression(bmap
, k
);
352 isl_seq_clr(bmap
->div
[k
], 1 + total
);
358 /* Assumes divs have been ordered if keep_divs is set.
360 static __isl_give isl_basic_map
*eliminate_div(__isl_take isl_basic_map
*bmap
,
361 isl_int
*eq
, unsigned div
, int keep_divs
)
366 v_div
= isl_basic_map_var_offset(bmap
, isl_dim_div
);
368 return isl_basic_map_free(bmap
);
370 bmap
= eliminate_var_using_equality(bmap
, pos
, eq
, keep_divs
, NULL
);
372 bmap
= isl_basic_map_drop_div(bmap
, div
);
377 /* Check if elimination of div "div" using equality "eq" would not
378 * result in a div depending on a later div.
380 static isl_bool
ok_to_eliminate_div(__isl_keep isl_basic_map
*bmap
, isl_int
*eq
,
388 v_div
= isl_basic_map_var_offset(bmap
, isl_dim_div
);
390 return isl_bool_error
;
393 last_div
= isl_seq_last_non_zero(eq
+ 1 + v_div
, bmap
->n_div
);
394 if (last_div
< 0 || last_div
<= div
)
395 return isl_bool_true
;
397 for (k
= 0; k
<= last_div
; ++k
) {
398 if (isl_int_is_zero(bmap
->div
[k
][0]))
400 if (!isl_int_is_zero(bmap
->div
[k
][1 + 1 + pos
]))
401 return isl_bool_false
;
404 return isl_bool_true
;
407 /* Eliminate divs based on equalities
409 static __isl_give isl_basic_map
*eliminate_divs_eq(
410 __isl_take isl_basic_map
*bmap
, int *progress
)
417 bmap
= isl_basic_map_order_divs(bmap
);
422 off
= isl_basic_map_offset(bmap
, isl_dim_div
);
424 for (d
= bmap
->n_div
- 1; d
>= 0 ; --d
) {
425 for (i
= 0; i
< bmap
->n_eq
; ++i
) {
428 if (!isl_int_is_one(bmap
->eq
[i
][off
+ d
]) &&
429 !isl_int_is_negone(bmap
->eq
[i
][off
+ d
]))
431 ok
= ok_to_eliminate_div(bmap
, bmap
->eq
[i
], d
);
433 return isl_basic_map_free(bmap
);
438 bmap
= eliminate_div(bmap
, bmap
->eq
[i
], d
, 1);
439 if (isl_basic_map_drop_equality(bmap
, i
) < 0)
440 return isl_basic_map_free(bmap
);
445 return eliminate_divs_eq(bmap
, progress
);
449 /* Eliminate divs based on inequalities
451 static __isl_give isl_basic_map
*eliminate_divs_ineq(
452 __isl_take isl_basic_map
*bmap
, int *progress
)
463 off
= isl_basic_map_offset(bmap
, isl_dim_div
);
465 for (d
= bmap
->n_div
- 1; d
>= 0 ; --d
) {
466 for (i
= 0; i
< bmap
->n_eq
; ++i
)
467 if (!isl_int_is_zero(bmap
->eq
[i
][off
+ d
]))
471 for (i
= 0; i
< bmap
->n_ineq
; ++i
)
472 if (isl_int_abs_gt(bmap
->ineq
[i
][off
+ d
], ctx
->one
))
474 if (i
< bmap
->n_ineq
)
477 bmap
= isl_basic_map_eliminate_vars(bmap
, (off
-1)+d
, 1);
478 if (!bmap
|| ISL_F_ISSET(bmap
, ISL_BASIC_MAP_EMPTY
))
480 bmap
= isl_basic_map_drop_div(bmap
, d
);
487 /* Does the equality constraint at position "eq" in "bmap" involve
488 * any local variables in the range [first, first + n)
489 * that are not marked as having an explicit representation?
491 static isl_bool
bmap_eq_involves_unknown_divs(__isl_keep isl_basic_map
*bmap
,
492 int eq
, unsigned first
, unsigned n
)
498 return isl_bool_error
;
500 o_div
= isl_basic_map_offset(bmap
, isl_dim_div
);
501 for (i
= 0; i
< n
; ++i
) {
504 if (isl_int_is_zero(bmap
->eq
[eq
][o_div
+ first
+ i
]))
506 unknown
= isl_basic_map_div_is_marked_unknown(bmap
, first
+ i
);
508 return isl_bool_error
;
510 return isl_bool_true
;
513 return isl_bool_false
;
516 /* The last local variable involved in the equality constraint
517 * at position "eq" in "bmap" is the local variable at position "div".
518 * It can therefore be used to extract an explicit representation
520 * Do so unless the local variable already has an explicit representation or
521 * the explicit representation would involve any other local variables
522 * that in turn do not have an explicit representation.
523 * An equality constraint involving local variables without an explicit
524 * representation can be used in isl_basic_map_drop_redundant_divs
525 * to separate out an independent local variable. Introducing
526 * an explicit representation here would block this transformation,
527 * while the partial explicit representation in itself is not very useful.
528 * Set *progress if anything is changed.
530 * The equality constraint is of the form
534 * with n a positive number. The explicit representation derived from
539 static __isl_give isl_basic_map
*set_div_from_eq(__isl_take isl_basic_map
*bmap
,
540 int div
, int eq
, int *progress
)
549 if (!isl_int_is_zero(bmap
->div
[div
][0]))
552 involves
= bmap_eq_involves_unknown_divs(bmap
, eq
, 0, div
);
554 return isl_basic_map_free(bmap
);
558 total
= isl_basic_map_dim(bmap
, isl_dim_all
);
560 return isl_basic_map_free(bmap
);
561 o_div
= isl_basic_map_offset(bmap
, isl_dim_div
);
562 isl_seq_neg(bmap
->div
[div
] + 1, bmap
->eq
[eq
], 1 + total
);
563 isl_int_set_si(bmap
->div
[div
][1 + o_div
+ div
], 0);
564 isl_int_set(bmap
->div
[div
][0], bmap
->eq
[eq
][o_div
+ div
]);
571 __isl_give isl_basic_map
*isl_basic_map_gauss(__isl_take isl_basic_map
*bmap
,
580 bmap
= isl_basic_map_order_divs(bmap
);
582 total
= isl_basic_map_dim(bmap
, isl_dim_all
);
584 return isl_basic_map_free(bmap
);
586 total_var
= total
- bmap
->n_div
;
588 last_var
= total
- 1;
589 for (done
= 0; done
< bmap
->n_eq
; ++done
) {
590 for (; last_var
>= 0; --last_var
) {
591 for (k
= done
; k
< bmap
->n_eq
; ++k
)
592 if (!isl_int_is_zero(bmap
->eq
[k
][1+last_var
]))
600 swap_equality(bmap
, k
, done
);
601 if (isl_int_is_neg(bmap
->eq
[done
][1+last_var
]))
602 isl_seq_neg(bmap
->eq
[done
], bmap
->eq
[done
], 1+total
);
604 bmap
= eliminate_var_using_equality(bmap
, last_var
,
605 bmap
->eq
[done
], 1, progress
);
607 if (last_var
>= total_var
)
608 bmap
= set_div_from_eq(bmap
, last_var
- total_var
,
613 if (done
== bmap
->n_eq
)
615 for (k
= done
; k
< bmap
->n_eq
; ++k
) {
616 if (isl_int_is_zero(bmap
->eq
[k
][0]))
618 return isl_basic_map_set_to_empty(bmap
);
620 isl_basic_map_free_equality(bmap
, bmap
->n_eq
-done
);
624 __isl_give isl_basic_set
*isl_basic_set_gauss(
625 __isl_take isl_basic_set
*bset
, int *progress
)
627 return bset_from_bmap(isl_basic_map_gauss(bset_to_bmap(bset
),
632 static unsigned int round_up(unsigned int v
)
643 /* Hash table of inequalities in a basic map.
644 * "index" is an array of addresses of inequalities in the basic map, some
645 * of which are NULL. The inequalities are hashed on the coefficients
646 * except the constant term.
647 * "size" is the number of elements in the array and is always a power of two
648 * "bits" is the number of bits need to represent an index into the array.
649 * "total" is the total dimension of the basic map.
651 struct isl_constraint_index
{
658 /* Fill in the "ci" data structure for holding the inequalities of "bmap".
660 static isl_stat
create_constraint_index(struct isl_constraint_index
*ci
,
661 __isl_keep isl_basic_map
*bmap
)
667 return isl_stat_error
;
668 ci
->total
= isl_basic_map_dim(bmap
, isl_dim_all
);
670 return isl_stat_error
;
671 if (bmap
->n_ineq
== 0)
673 ci
->size
= round_up(4 * (bmap
->n_ineq
+ 1) / 3 - 1);
674 ci
->bits
= ffs(ci
->size
) - 1;
675 ctx
= isl_basic_map_get_ctx(bmap
);
676 ci
->index
= isl_calloc_array(ctx
, isl_int
**, ci
->size
);
678 return isl_stat_error
;
683 /* Free the memory allocated by create_constraint_index.
685 static void constraint_index_free(struct isl_constraint_index
*ci
)
690 /* Return the position in ci->index that contains the address of
691 * an inequality that is equal to *ineq up to the constant term,
692 * provided this address is not identical to "ineq".
693 * If there is no such inequality, then return the position where
694 * such an inequality should be inserted.
696 static int hash_index_ineq(struct isl_constraint_index
*ci
, isl_int
**ineq
)
699 uint32_t hash
= isl_seq_get_hash_bits((*ineq
) + 1, ci
->total
, ci
->bits
);
700 for (h
= hash
; ci
->index
[h
]; h
= (h
+1) % ci
->size
)
701 if (ineq
!= ci
->index
[h
] &&
702 isl_seq_eq((*ineq
) + 1, ci
->index
[h
][0]+1, ci
->total
))
707 /* Return the position in ci->index that contains the address of
708 * an inequality that is equal to the k'th inequality of "bmap"
709 * up to the constant term, provided it does not point to the very
711 * If there is no such inequality, then return the position where
712 * such an inequality should be inserted.
714 static int hash_index(struct isl_constraint_index
*ci
,
715 __isl_keep isl_basic_map
*bmap
, int k
)
717 return hash_index_ineq(ci
, &bmap
->ineq
[k
]);
720 static int set_hash_index(struct isl_constraint_index
*ci
,
721 __isl_keep isl_basic_set
*bset
, int k
)
723 return hash_index(ci
, bset
, k
);
726 /* Fill in the "ci" data structure with the inequalities of "bset".
728 static isl_stat
setup_constraint_index(struct isl_constraint_index
*ci
,
729 __isl_keep isl_basic_set
*bset
)
733 if (create_constraint_index(ci
, bset
) < 0)
734 return isl_stat_error
;
736 for (k
= 0; k
< bset
->n_ineq
; ++k
) {
737 h
= set_hash_index(ci
, bset
, k
);
738 ci
->index
[h
] = &bset
->ineq
[k
];
744 /* Is the inequality ineq (obviously) redundant with respect
745 * to the constraints in "ci"?
747 * Look for an inequality in "ci" with the same coefficients and then
748 * check if the contant term of "ineq" is greater than or equal
749 * to the constant term of that inequality. If so, "ineq" is clearly
752 * Note that hash_index_ineq ignores a stored constraint if it has
753 * the same address as the passed inequality. It is ok to pass
754 * the address of a local variable here since it will never be
755 * the same as the address of a constraint in "ci".
757 static isl_bool
constraint_index_is_redundant(struct isl_constraint_index
*ci
,
762 h
= hash_index_ineq(ci
, &ineq
);
764 return isl_bool_false
;
765 return isl_int_ge(ineq
[0], (*ci
->index
[h
])[0]);
768 /* If we can eliminate more than one div, then we need to make
769 * sure we do it from last div to first div, in order not to
770 * change the position of the other divs that still need to
773 static __isl_give isl_basic_map
*remove_duplicate_divs(
774 __isl_take isl_basic_map
*bmap
, int *progress
)
786 bmap
= isl_basic_map_order_divs(bmap
);
787 if (!bmap
|| bmap
->n_div
<= 1)
790 v_div
= isl_basic_map_var_offset(bmap
, isl_dim_div
);
792 return isl_basic_map_free(bmap
);
793 total
= v_div
+ bmap
->n_div
;
796 for (k
= bmap
->n_div
- 1; k
>= 0; --k
)
797 if (!isl_int_is_zero(bmap
->div
[k
][0]))
802 size
= round_up(4 * bmap
->n_div
/ 3 - 1);
805 elim_for
= isl_calloc_array(ctx
, int, bmap
->n_div
);
806 bits
= ffs(size
) - 1;
807 index
= isl_calloc_array(ctx
, int, size
);
808 if (!elim_for
|| !index
)
810 eq
= isl_blk_alloc(ctx
, 1+total
);
811 if (isl_blk_is_error(eq
))
814 isl_seq_clr(eq
.data
, 1+total
);
815 index
[isl_seq_get_hash_bits(bmap
->div
[k
], 2+total
, bits
)] = k
+ 1;
816 for (--k
; k
>= 0; --k
) {
819 if (isl_int_is_zero(bmap
->div
[k
][0]))
822 hash
= isl_seq_get_hash_bits(bmap
->div
[k
], 2+total
, bits
);
823 for (h
= hash
; index
[h
]; h
= (h
+1) % size
)
824 if (isl_seq_eq(bmap
->div
[k
],
825 bmap
->div
[index
[h
]-1], 2+total
))
834 for (l
= bmap
->n_div
- 1; l
>= 0; --l
) {
838 isl_int_set_si(eq
.data
[1 + v_div
+ k
], -1);
839 isl_int_set_si(eq
.data
[1 + v_div
+ l
], 1);
840 bmap
= eliminate_div(bmap
, eq
.data
, l
, 1);
843 isl_int_set_si(eq
.data
[1 + v_div
+ k
], 0);
844 isl_int_set_si(eq
.data
[1 + v_div
+ l
], 0);
847 isl_blk_free(ctx
, eq
);
854 static int n_pure_div_eq(struct isl_basic_map
*bmap
)
859 v_div
= isl_basic_map_var_offset(bmap
, isl_dim_div
);
862 for (i
= 0, j
= bmap
->n_div
-1; i
< bmap
->n_eq
; ++i
) {
863 while (j
>= 0 && isl_int_is_zero(bmap
->eq
[i
][1 + v_div
+ j
]))
867 if (isl_seq_first_non_zero(bmap
->eq
[i
] + 1 + v_div
, j
) != -1)
873 /* Normalize divs that appear in equalities.
875 * In particular, we assume that bmap contains some equalities
880 * and we want to replace the set of e_i by a minimal set and
881 * such that the new e_i have a canonical representation in terms
883 * If any of the equalities involves more than one divs, then
884 * we currently simply bail out.
886 * Let us first additionally assume that all equalities involve
887 * a div. The equalities then express modulo constraints on the
888 * remaining variables and we can use "parameter compression"
889 * to find a minimal set of constraints. The result is a transformation
891 * x = T(x') = x_0 + G x'
893 * with G a lower-triangular matrix with all elements below the diagonal
894 * non-negative and smaller than the diagonal element on the same row.
895 * We first normalize x_0 by making the same property hold in the affine
897 * The rows i of G with a 1 on the diagonal do not impose any modulo
898 * constraint and simply express x_i = x'_i.
899 * For each of the remaining rows i, we introduce a div and a corresponding
900 * equality. In particular
902 * g_ii e_j = x_i - g_i(x')
904 * where each x'_k is replaced either by x_k (if g_kk = 1) or the
905 * corresponding div (if g_kk != 1).
907 * If there are any equalities not involving any div, then we
908 * first apply a variable compression on the variables x:
910 * x = C x'' x'' = C_2 x
912 * and perform the above parameter compression on A C instead of on A.
913 * The resulting compression is then of the form
915 * x'' = T(x') = x_0 + G x'
917 * and in constructing the new divs and the corresponding equalities,
918 * we have to replace each x'', i.e., the x'_k with (g_kk = 1),
919 * by the corresponding row from C_2.
921 static __isl_give isl_basic_map
*normalize_divs(__isl_take isl_basic_map
*bmap
,
929 struct isl_mat
*T
= NULL
;
930 struct isl_mat
*C
= NULL
;
931 struct isl_mat
*C2
= NULL
;
939 if (bmap
->n_div
== 0)
945 if (ISL_F_ISSET(bmap
, ISL_BASIC_MAP_NORMALIZED_DIVS
))
948 v_div
= isl_basic_map_var_offset(bmap
, isl_dim_div
);
949 div_eq
= n_pure_div_eq(bmap
);
950 if (v_div
< 0 || div_eq
< 0)
951 return isl_basic_map_free(bmap
);
955 if (div_eq
< bmap
->n_eq
) {
956 B
= isl_mat_sub_alloc6(bmap
->ctx
, bmap
->eq
, div_eq
,
957 bmap
->n_eq
- div_eq
, 0, 1 + v_div
);
958 C
= isl_mat_variable_compression(B
, &C2
);
962 bmap
= isl_basic_map_set_to_empty(bmap
);
969 d
= isl_vec_alloc(bmap
->ctx
, div_eq
);
972 for (i
= 0, j
= bmap
->n_div
-1; i
< div_eq
; ++i
) {
973 while (j
>= 0 && isl_int_is_zero(bmap
->eq
[i
][1 + v_div
+ j
]))
975 isl_int_set(d
->block
.data
[i
], bmap
->eq
[i
][1 + v_div
+ j
]);
977 B
= isl_mat_sub_alloc6(bmap
->ctx
, bmap
->eq
, 0, div_eq
, 0, 1 + v_div
);
980 B
= isl_mat_product(B
, C
);
984 T
= isl_mat_parameter_compression(B
, d
);
988 bmap
= isl_basic_map_set_to_empty(bmap
);
994 for (i
= 0; i
< T
->n_row
- 1; ++i
) {
995 isl_int_fdiv_q(v
, T
->row
[1 + i
][0], T
->row
[1 + i
][1 + i
]);
996 if (isl_int_is_zero(v
))
998 isl_mat_col_submul(T
, 0, v
, 1 + i
);
1001 pos
= isl_alloc_array(bmap
->ctx
, int, T
->n_row
);
1004 /* We have to be careful because dropping equalities may reorder them */
1006 for (j
= bmap
->n_div
- 1; j
>= 0; --j
) {
1007 for (i
= 0; i
< bmap
->n_eq
; ++i
)
1008 if (!isl_int_is_zero(bmap
->eq
[i
][1 + v_div
+ j
]))
1010 if (i
< bmap
->n_eq
) {
1011 bmap
= isl_basic_map_drop_div(bmap
, j
);
1012 isl_basic_map_drop_equality(bmap
, i
);
1018 for (i
= 1; i
< T
->n_row
; ++i
) {
1019 if (isl_int_is_one(T
->row
[i
][i
]))
1024 if (needed
> dropped
) {
1025 bmap
= isl_basic_map_extend_space(bmap
, isl_space_copy(bmap
->dim
),
1030 for (i
= 1; i
< T
->n_row
; ++i
) {
1031 if (isl_int_is_one(T
->row
[i
][i
]))
1033 k
= isl_basic_map_alloc_div(bmap
);
1034 pos
[i
] = 1 + v_div
+ k
;
1035 isl_seq_clr(bmap
->div
[k
] + 1, 1 + v_div
+ bmap
->n_div
);
1036 isl_int_set(bmap
->div
[k
][0], T
->row
[i
][i
]);
1038 isl_seq_cpy(bmap
->div
[k
] + 1, C2
->row
[i
], 1 + v_div
);
1040 isl_int_set_si(bmap
->div
[k
][1 + i
], 1);
1041 for (j
= 0; j
< i
; ++j
) {
1042 if (isl_int_is_zero(T
->row
[i
][j
]))
1044 if (pos
[j
] < T
->n_row
&& C2
)
1045 isl_seq_submul(bmap
->div
[k
] + 1, T
->row
[i
][j
],
1046 C2
->row
[pos
[j
]], 1 + v_div
);
1048 isl_int_neg(bmap
->div
[k
][1 + pos
[j
]],
1051 j
= isl_basic_map_alloc_equality(bmap
);
1052 isl_seq_neg(bmap
->eq
[j
], bmap
->div
[k
]+1, 1+v_div
+bmap
->n_div
);
1053 isl_int_set(bmap
->eq
[j
][pos
[i
]], bmap
->div
[k
][0]);
1062 ISL_F_SET(bmap
, ISL_BASIC_MAP_NORMALIZED_DIVS
);
1073 static __isl_give isl_basic_map
*set_div_from_lower_bound(
1074 __isl_take isl_basic_map
*bmap
, int div
, int ineq
)
1076 unsigned total
= isl_basic_map_offset(bmap
, isl_dim_div
);
1078 isl_seq_neg(bmap
->div
[div
] + 1, bmap
->ineq
[ineq
], total
+ bmap
->n_div
);
1079 isl_int_set(bmap
->div
[div
][0], bmap
->ineq
[ineq
][total
+ div
]);
1080 isl_int_add(bmap
->div
[div
][1], bmap
->div
[div
][1], bmap
->div
[div
][0]);
1081 isl_int_sub_ui(bmap
->div
[div
][1], bmap
->div
[div
][1], 1);
1082 isl_int_set_si(bmap
->div
[div
][1 + total
+ div
], 0);
1087 /* Check whether it is ok to define a div based on an inequality.
1088 * To avoid the introduction of circular definitions of divs, we
1089 * do not allow such a definition if the resulting expression would refer to
1090 * any other undefined divs or if any known div is defined in
1091 * terms of the unknown div.
1093 static isl_bool
ok_to_set_div_from_bound(__isl_keep isl_basic_map
*bmap
,
1097 unsigned total
= isl_basic_map_offset(bmap
, isl_dim_div
);
1099 /* Not defined in terms of unknown divs */
1100 for (j
= 0; j
< bmap
->n_div
; ++j
) {
1103 if (isl_int_is_zero(bmap
->ineq
[ineq
][total
+ j
]))
1105 if (isl_int_is_zero(bmap
->div
[j
][0]))
1106 return isl_bool_false
;
1109 /* No other div defined in terms of this one => avoid loops */
1110 for (j
= 0; j
< bmap
->n_div
; ++j
) {
1113 if (isl_int_is_zero(bmap
->div
[j
][0]))
1115 if (!isl_int_is_zero(bmap
->div
[j
][1 + total
+ div
]))
1116 return isl_bool_false
;
1119 return isl_bool_true
;
1122 /* Would an expression for div "div" based on inequality "ineq" of "bmap"
1123 * be a better expression than the current one?
1125 * If we do not have any expression yet, then any expression would be better.
1126 * Otherwise we check if the last variable involved in the inequality
1127 * (disregarding the div that it would define) is in an earlier position
1128 * than the last variable involved in the current div expression.
1130 static isl_bool
better_div_constraint(__isl_keep isl_basic_map
*bmap
,
1133 unsigned total
= isl_basic_map_offset(bmap
, isl_dim_div
);
1137 if (isl_int_is_zero(bmap
->div
[div
][0]))
1138 return isl_bool_true
;
1140 if (isl_seq_last_non_zero(bmap
->ineq
[ineq
] + total
+ div
+ 1,
1141 bmap
->n_div
- (div
+ 1)) >= 0)
1142 return isl_bool_false
;
1144 last_ineq
= isl_seq_last_non_zero(bmap
->ineq
[ineq
], total
+ div
);
1145 last_div
= isl_seq_last_non_zero(bmap
->div
[div
] + 1,
1146 total
+ bmap
->n_div
);
1148 return last_ineq
< last_div
;
1151 /* Given two constraints "k" and "l" that are opposite to each other,
1152 * except for the constant term, check if we can use them
1153 * to obtain an expression for one of the hitherto unknown divs or
1154 * a "better" expression for a div for which we already have an expression.
1155 * "sum" is the sum of the constant terms of the constraints.
1156 * If this sum is strictly smaller than the coefficient of one
1157 * of the divs, then this pair can be used define the div.
1158 * To avoid the introduction of circular definitions of divs, we
1159 * do not use the pair if the resulting expression would refer to
1160 * any other undefined divs or if any known div is defined in
1161 * terms of the unknown div.
1163 static __isl_give isl_basic_map
*check_for_div_constraints(
1164 __isl_take isl_basic_map
*bmap
, int k
, int l
, isl_int sum
,
1168 unsigned total
= isl_basic_map_offset(bmap
, isl_dim_div
);
1170 for (i
= 0; i
< bmap
->n_div
; ++i
) {
1173 if (isl_int_is_zero(bmap
->ineq
[k
][total
+ i
]))
1175 if (isl_int_abs_ge(sum
, bmap
->ineq
[k
][total
+ i
]))
1177 set_div
= better_div_constraint(bmap
, i
, k
);
1178 if (set_div
>= 0 && set_div
)
1179 set_div
= ok_to_set_div_from_bound(bmap
, i
, k
);
1181 return isl_basic_map_free(bmap
);
1184 if (isl_int_is_pos(bmap
->ineq
[k
][total
+ i
]))
1185 bmap
= set_div_from_lower_bound(bmap
, i
, k
);
1187 bmap
= set_div_from_lower_bound(bmap
, i
, l
);
1195 __isl_give isl_basic_map
*isl_basic_map_remove_duplicate_constraints(
1196 __isl_take isl_basic_map
*bmap
, int *progress
, int detect_divs
)
1198 struct isl_constraint_index ci
;
1200 isl_size total
= isl_basic_map_dim(bmap
, isl_dim_all
);
1203 if (total
< 0 || bmap
->n_ineq
<= 1)
1206 if (create_constraint_index(&ci
, bmap
) < 0)
1209 h
= isl_seq_get_hash_bits(bmap
->ineq
[0] + 1, total
, ci
.bits
);
1210 ci
.index
[h
] = &bmap
->ineq
[0];
1211 for (k
= 1; k
< bmap
->n_ineq
; ++k
) {
1212 h
= hash_index(&ci
, bmap
, k
);
1214 ci
.index
[h
] = &bmap
->ineq
[k
];
1219 l
= ci
.index
[h
] - &bmap
->ineq
[0];
1220 if (isl_int_lt(bmap
->ineq
[k
][0], bmap
->ineq
[l
][0]))
1221 swap_inequality(bmap
, k
, l
);
1222 isl_basic_map_drop_inequality(bmap
, k
);
1226 for (k
= 0; k
< bmap
->n_ineq
-1; ++k
) {
1227 isl_seq_neg(bmap
->ineq
[k
]+1, bmap
->ineq
[k
]+1, total
);
1228 h
= hash_index(&ci
, bmap
, k
);
1229 isl_seq_neg(bmap
->ineq
[k
]+1, bmap
->ineq
[k
]+1, total
);
1232 l
= ci
.index
[h
] - &bmap
->ineq
[0];
1233 isl_int_add(sum
, bmap
->ineq
[k
][0], bmap
->ineq
[l
][0]);
1234 if (isl_int_is_pos(sum
)) {
1236 bmap
= check_for_div_constraints(bmap
, k
, l
,
1240 if (isl_int_is_zero(sum
)) {
1241 /* We need to break out of the loop after these
1242 * changes since the contents of the hash
1243 * will no longer be valid.
1244 * Plus, we probably we want to regauss first.
1248 isl_basic_map_drop_inequality(bmap
, l
);
1249 isl_basic_map_inequality_to_equality(bmap
, k
);
1251 bmap
= isl_basic_map_set_to_empty(bmap
);
1256 constraint_index_free(&ci
);
1260 /* Detect all pairs of inequalities that form an equality.
1262 * isl_basic_map_remove_duplicate_constraints detects at most one such pair.
1263 * Call it repeatedly while it is making progress.
1265 __isl_give isl_basic_map
*isl_basic_map_detect_inequality_pairs(
1266 __isl_take isl_basic_map
*bmap
, int *progress
)
1272 bmap
= isl_basic_map_remove_duplicate_constraints(bmap
,
1274 if (progress
&& duplicate
)
1276 } while (duplicate
);
1281 /* Eliminate knowns divs from constraints where they appear with
1282 * a (positive or negative) unit coefficient.
1286 * floor(e/m) + f >= 0
1294 * -floor(e/m) + f >= 0
1298 * -e + m f + m - 1 >= 0
1300 * The first conversion is valid because floor(e/m) >= -f is equivalent
1301 * to e/m >= -f because -f is an integral expression.
1302 * The second conversion follows from the fact that
1304 * -floor(e/m) = ceil(-e/m) = floor((-e + m - 1)/m)
1307 * Note that one of the div constraints may have been eliminated
1308 * due to being redundant with respect to the constraint that is
1309 * being modified by this function. The modified constraint may
1310 * no longer imply this div constraint, so we add it back to make
1311 * sure we do not lose any information.
1313 * We skip integral divs, i.e., those with denominator 1, as we would
1314 * risk eliminating the div from the div constraints. We do not need
1315 * to handle those divs here anyway since the div constraints will turn
1316 * out to form an equality and this equality can then be used to eliminate
1317 * the div from all constraints.
1319 static __isl_give isl_basic_map
*eliminate_unit_divs(
1320 __isl_take isl_basic_map
*bmap
, int *progress
)
1329 ctx
= isl_basic_map_get_ctx(bmap
);
1330 total
= isl_basic_map_offset(bmap
, isl_dim_div
);
1332 for (i
= 0; i
< bmap
->n_div
; ++i
) {
1333 if (isl_int_is_zero(bmap
->div
[i
][0]))
1335 if (isl_int_is_one(bmap
->div
[i
][0]))
1337 for (j
= 0; j
< bmap
->n_ineq
; ++j
) {
1340 if (!isl_int_is_one(bmap
->ineq
[j
][total
+ i
]) &&
1341 !isl_int_is_negone(bmap
->ineq
[j
][total
+ i
]))
1346 s
= isl_int_sgn(bmap
->ineq
[j
][total
+ i
]);
1347 isl_int_set_si(bmap
->ineq
[j
][total
+ i
], 0);
1349 isl_seq_combine(bmap
->ineq
[j
],
1350 ctx
->negone
, bmap
->div
[i
] + 1,
1351 bmap
->div
[i
][0], bmap
->ineq
[j
],
1352 total
+ bmap
->n_div
);
1354 isl_seq_combine(bmap
->ineq
[j
],
1355 ctx
->one
, bmap
->div
[i
] + 1,
1356 bmap
->div
[i
][0], bmap
->ineq
[j
],
1357 total
+ bmap
->n_div
);
1359 isl_int_add(bmap
->ineq
[j
][0],
1360 bmap
->ineq
[j
][0], bmap
->div
[i
][0]);
1361 isl_int_sub_ui(bmap
->ineq
[j
][0],
1362 bmap
->ineq
[j
][0], 1);
1365 bmap
= isl_basic_map_extend_constraints(bmap
, 0, 1);
1366 bmap
= isl_basic_map_add_div_constraint(bmap
, i
, s
);
1375 __isl_give isl_basic_map
*isl_basic_map_simplify(__isl_take isl_basic_map
*bmap
)
1384 empty
= isl_basic_map_plain_is_empty(bmap
);
1386 return isl_basic_map_free(bmap
);
1389 bmap
= isl_basic_map_normalize_constraints(bmap
);
1390 bmap
= reduce_div_coefficients(bmap
);
1391 bmap
= normalize_div_expressions(bmap
);
1392 bmap
= remove_duplicate_divs(bmap
, &progress
);
1393 bmap
= eliminate_unit_divs(bmap
, &progress
);
1394 bmap
= eliminate_divs_eq(bmap
, &progress
);
1395 bmap
= eliminate_divs_ineq(bmap
, &progress
);
1396 bmap
= isl_basic_map_gauss(bmap
, &progress
);
1397 /* requires equalities in normal form */
1398 bmap
= normalize_divs(bmap
, &progress
);
1399 bmap
= isl_basic_map_remove_duplicate_constraints(bmap
,
1401 if (bmap
&& progress
)
1402 ISL_F_CLR(bmap
, ISL_BASIC_MAP_REDUCED_COEFFICIENTS
);
1407 struct isl_basic_set
*isl_basic_set_simplify(struct isl_basic_set
*bset
)
1409 return bset_from_bmap(isl_basic_map_simplify(bset_to_bmap(bset
)));
1413 isl_bool
isl_basic_map_is_div_constraint(__isl_keep isl_basic_map
*bmap
,
1414 isl_int
*constraint
, unsigned div
)
1419 return isl_bool_error
;
1421 pos
= isl_basic_map_offset(bmap
, isl_dim_div
) + div
;
1423 if (isl_int_eq(constraint
[pos
], bmap
->div
[div
][0])) {
1425 isl_int_sub(bmap
->div
[div
][1],
1426 bmap
->div
[div
][1], bmap
->div
[div
][0]);
1427 isl_int_add_ui(bmap
->div
[div
][1], bmap
->div
[div
][1], 1);
1428 neg
= isl_seq_is_neg(constraint
, bmap
->div
[div
]+1, pos
);
1429 isl_int_sub_ui(bmap
->div
[div
][1], bmap
->div
[div
][1], 1);
1430 isl_int_add(bmap
->div
[div
][1],
1431 bmap
->div
[div
][1], bmap
->div
[div
][0]);
1433 return isl_bool_false
;
1434 if (isl_seq_first_non_zero(constraint
+pos
+1,
1435 bmap
->n_div
-div
-1) != -1)
1436 return isl_bool_false
;
1437 } else if (isl_int_abs_eq(constraint
[pos
], bmap
->div
[div
][0])) {
1438 if (!isl_seq_eq(constraint
, bmap
->div
[div
]+1, pos
))
1439 return isl_bool_false
;
1440 if (isl_seq_first_non_zero(constraint
+pos
+1,
1441 bmap
->n_div
-div
-1) != -1)
1442 return isl_bool_false
;
1444 return isl_bool_false
;
1446 return isl_bool_true
;
1449 isl_bool
isl_basic_set_is_div_constraint(__isl_keep isl_basic_set
*bset
,
1450 isl_int
*constraint
, unsigned div
)
1452 return isl_basic_map_is_div_constraint(bset
, constraint
, div
);
1456 /* If the only constraints a div d=floor(f/m)
1457 * appears in are its two defining constraints
1460 * -(f - (m - 1)) + m d >= 0
1462 * then it can safely be removed.
1464 static isl_bool
div_is_redundant(__isl_keep isl_basic_map
*bmap
, int div
)
1467 unsigned pos
= isl_basic_map_offset(bmap
, isl_dim_div
) + div
;
1469 for (i
= 0; i
< bmap
->n_eq
; ++i
)
1470 if (!isl_int_is_zero(bmap
->eq
[i
][pos
]))
1471 return isl_bool_false
;
1473 for (i
= 0; i
< bmap
->n_ineq
; ++i
) {
1476 if (isl_int_is_zero(bmap
->ineq
[i
][pos
]))
1478 red
= isl_basic_map_is_div_constraint(bmap
, bmap
->ineq
[i
], div
);
1479 if (red
< 0 || !red
)
1483 for (i
= 0; i
< bmap
->n_div
; ++i
) {
1484 if (isl_int_is_zero(bmap
->div
[i
][0]))
1486 if (!isl_int_is_zero(bmap
->div
[i
][1+pos
]))
1487 return isl_bool_false
;
1490 return isl_bool_true
;
1494 * Remove divs that don't occur in any of the constraints or other divs.
1495 * These can arise when dropping constraints from a basic map or
1496 * when the divs of a basic map have been temporarily aligned
1497 * with the divs of another basic map.
1499 static __isl_give isl_basic_map
*remove_redundant_divs(
1500 __isl_take isl_basic_map
*bmap
)
1505 v_div
= isl_basic_map_var_offset(bmap
, isl_dim_div
);
1507 return isl_basic_map_free(bmap
);
1509 for (i
= bmap
->n_div
-1; i
>= 0; --i
) {
1512 redundant
= div_is_redundant(bmap
, i
);
1514 return isl_basic_map_free(bmap
);
1517 bmap
= isl_basic_map_drop_constraints_involving(bmap
,
1519 bmap
= isl_basic_map_drop_div(bmap
, i
);
1524 /* Mark "bmap" as final, without checking for obviously redundant
1525 * integer divisions. This function should be used when "bmap"
1526 * is known not to involve any such integer divisions.
1528 __isl_give isl_basic_map
*isl_basic_map_mark_final(
1529 __isl_take isl_basic_map
*bmap
)
1533 ISL_F_SET(bmap
, ISL_BASIC_SET_FINAL
);
1537 /* Mark "bmap" as final, after removing obviously redundant integer divisions.
1539 __isl_give isl_basic_map
*isl_basic_map_finalize(__isl_take isl_basic_map
*bmap
)
1541 bmap
= remove_redundant_divs(bmap
);
1542 bmap
= isl_basic_map_mark_final(bmap
);
1546 struct isl_basic_set
*isl_basic_set_finalize(struct isl_basic_set
*bset
)
1548 return bset_from_bmap(isl_basic_map_finalize(bset_to_bmap(bset
)));
1551 /* Remove definition of any div that is defined in terms of the given variable.
1552 * The div itself is not removed. Functions such as
1553 * eliminate_divs_ineq depend on the other divs remaining in place.
1555 static __isl_give isl_basic_map
*remove_dependent_vars(
1556 __isl_take isl_basic_map
*bmap
, int pos
)
1563 for (i
= 0; i
< bmap
->n_div
; ++i
) {
1564 if (isl_int_is_zero(bmap
->div
[i
][0]))
1566 if (isl_int_is_zero(bmap
->div
[i
][1+1+pos
]))
1568 bmap
= isl_basic_map_mark_div_unknown(bmap
, i
);
1575 /* Eliminate the specified variables from the constraints using
1576 * Fourier-Motzkin. The variables themselves are not removed.
1578 __isl_give isl_basic_map
*isl_basic_map_eliminate_vars(
1579 __isl_take isl_basic_map
*bmap
, unsigned pos
, unsigned n
)
1588 total
= isl_basic_map_dim(bmap
, isl_dim_all
);
1590 return isl_basic_map_free(bmap
);
1592 bmap
= isl_basic_map_cow(bmap
);
1593 for (d
= pos
+ n
- 1; d
>= 0 && d
>= pos
; --d
)
1594 bmap
= remove_dependent_vars(bmap
, d
);
1598 for (d
= pos
+ n
- 1;
1599 d
>= 0 && d
>= total
- bmap
->n_div
&& d
>= pos
; --d
)
1600 isl_seq_clr(bmap
->div
[d
-(total
-bmap
->n_div
)], 2+total
);
1601 for (d
= pos
+ n
- 1; d
>= 0 && d
>= pos
; --d
) {
1602 int n_lower
, n_upper
;
1605 for (i
= 0; i
< bmap
->n_eq
; ++i
) {
1606 if (isl_int_is_zero(bmap
->eq
[i
][1+d
]))
1608 bmap
= eliminate_var_using_equality(bmap
, d
,
1609 bmap
->eq
[i
], 0, NULL
);
1610 if (isl_basic_map_drop_equality(bmap
, i
) < 0)
1611 return isl_basic_map_free(bmap
);
1619 for (i
= 0; i
< bmap
->n_ineq
; ++i
) {
1620 if (isl_int_is_pos(bmap
->ineq
[i
][1+d
]))
1622 else if (isl_int_is_neg(bmap
->ineq
[i
][1+d
]))
1625 bmap
= isl_basic_map_extend_constraints(bmap
,
1626 0, n_lower
* n_upper
);
1629 for (i
= bmap
->n_ineq
- 1; i
>= 0; --i
) {
1631 if (isl_int_is_zero(bmap
->ineq
[i
][1+d
]))
1634 for (j
= 0; j
< i
; ++j
) {
1635 if (isl_int_is_zero(bmap
->ineq
[j
][1+d
]))
1638 if (isl_int_sgn(bmap
->ineq
[i
][1+d
]) ==
1639 isl_int_sgn(bmap
->ineq
[j
][1+d
]))
1641 k
= isl_basic_map_alloc_inequality(bmap
);
1644 isl_seq_cpy(bmap
->ineq
[k
], bmap
->ineq
[i
],
1646 isl_seq_elim(bmap
->ineq
[k
], bmap
->ineq
[j
],
1647 1+d
, 1+total
, NULL
);
1649 isl_basic_map_drop_inequality(bmap
, i
);
1652 if (n_lower
> 0 && n_upper
> 0) {
1653 bmap
= isl_basic_map_normalize_constraints(bmap
);
1654 bmap
= isl_basic_map_remove_duplicate_constraints(bmap
,
1656 bmap
= isl_basic_map_gauss(bmap
, NULL
);
1657 bmap
= isl_basic_map_remove_redundancies(bmap
);
1661 if (ISL_F_ISSET(bmap
, ISL_BASIC_MAP_EMPTY
))
1666 bmap
= isl_basic_map_gauss(bmap
, NULL
);
1669 isl_basic_map_free(bmap
);
1673 struct isl_basic_set
*isl_basic_set_eliminate_vars(
1674 struct isl_basic_set
*bset
, unsigned pos
, unsigned n
)
1676 return bset_from_bmap(isl_basic_map_eliminate_vars(bset_to_bmap(bset
),
1680 /* Eliminate the specified n dimensions starting at first from the
1681 * constraints, without removing the dimensions from the space.
1682 * If the set is rational, the dimensions are eliminated using Fourier-Motzkin.
1683 * Otherwise, they are projected out and the original space is restored.
1685 __isl_give isl_basic_map
*isl_basic_map_eliminate(
1686 __isl_take isl_basic_map
*bmap
,
1687 enum isl_dim_type type
, unsigned first
, unsigned n
)
1696 if (isl_basic_map_check_range(bmap
, type
, first
, n
) < 0)
1697 return isl_basic_map_free(bmap
);
1699 if (ISL_F_ISSET(bmap
, ISL_BASIC_MAP_RATIONAL
)) {
1700 first
+= isl_basic_map_offset(bmap
, type
) - 1;
1701 bmap
= isl_basic_map_eliminate_vars(bmap
, first
, n
);
1702 return isl_basic_map_finalize(bmap
);
1705 space
= isl_basic_map_get_space(bmap
);
1706 bmap
= isl_basic_map_project_out(bmap
, type
, first
, n
);
1707 bmap
= isl_basic_map_insert_dims(bmap
, type
, first
, n
);
1708 bmap
= isl_basic_map_reset_space(bmap
, space
);
1712 __isl_give isl_basic_set
*isl_basic_set_eliminate(
1713 __isl_take isl_basic_set
*bset
,
1714 enum isl_dim_type type
, unsigned first
, unsigned n
)
1716 return isl_basic_map_eliminate(bset
, type
, first
, n
);
1719 /* Remove all constraints from "bmap" that reference any unknown local
1720 * variables (directly or indirectly).
1722 * Dropping all constraints on a local variable will make it redundant,
1723 * so it will get removed implicitly by
1724 * isl_basic_map_drop_constraints_involving_dims. Some other local
1725 * variables may also end up becoming redundant if they only appear
1726 * in constraints together with the unknown local variable.
1727 * Therefore, start over after calling
1728 * isl_basic_map_drop_constraints_involving_dims.
1730 __isl_give isl_basic_map
*isl_basic_map_drop_constraint_involving_unknown_divs(
1731 __isl_take isl_basic_map
*bmap
)
1737 known
= isl_basic_map_divs_known(bmap
);
1739 return isl_basic_map_free(bmap
);
1743 n_div
= isl_basic_map_dim(bmap
, isl_dim_div
);
1745 return isl_basic_map_free(bmap
);
1746 o_div
= isl_basic_map_offset(bmap
, isl_dim_div
) - 1;
1748 for (i
= 0; i
< n_div
; ++i
) {
1749 known
= isl_basic_map_div_is_known(bmap
, i
);
1751 return isl_basic_map_free(bmap
);
1754 bmap
= remove_dependent_vars(bmap
, o_div
+ i
);
1755 bmap
= isl_basic_map_drop_constraints_involving_dims(bmap
,
1757 n_div
= isl_basic_map_dim(bmap
, isl_dim_div
);
1759 return isl_basic_map_free(bmap
);
1766 /* Remove all constraints from "map" that reference any unknown local
1767 * variables (directly or indirectly).
1769 * Since constraints may get dropped from the basic maps,
1770 * they may no longer be disjoint from each other.
1772 __isl_give isl_map
*isl_map_drop_constraint_involving_unknown_divs(
1773 __isl_take isl_map
*map
)
1778 known
= isl_map_divs_known(map
);
1780 return isl_map_free(map
);
1784 map
= isl_map_cow(map
);
1788 for (i
= 0; i
< map
->n
; ++i
) {
1790 isl_basic_map_drop_constraint_involving_unknown_divs(
1793 return isl_map_free(map
);
1797 ISL_F_CLR(map
, ISL_MAP_DISJOINT
);
1802 /* Don't assume equalities are in order, because align_divs
1803 * may have changed the order of the divs.
1805 static void compute_elimination_index(__isl_keep isl_basic_map
*bmap
, int *elim
)
1810 total
= isl_space_dim(bmap
->dim
, isl_dim_all
);
1811 for (d
= 0; d
< total
; ++d
)
1813 for (i
= 0; i
< bmap
->n_eq
; ++i
) {
1814 for (d
= total
- 1; d
>= 0; --d
) {
1815 if (isl_int_is_zero(bmap
->eq
[i
][1+d
]))
1823 static void set_compute_elimination_index(__isl_keep isl_basic_set
*bset
,
1826 compute_elimination_index(bset_to_bmap(bset
), elim
);
1829 static int reduced_using_equalities(isl_int
*dst
, isl_int
*src
,
1830 __isl_keep isl_basic_map
*bmap
, int *elim
)
1836 total
= isl_space_dim(bmap
->dim
, isl_dim_all
);
1837 for (d
= total
- 1; d
>= 0; --d
) {
1838 if (isl_int_is_zero(src
[1+d
]))
1843 isl_seq_cpy(dst
, src
, 1 + total
);
1846 isl_seq_elim(dst
, bmap
->eq
[elim
[d
]], 1 + d
, 1 + total
, NULL
);
1851 static int set_reduced_using_equalities(isl_int
*dst
, isl_int
*src
,
1852 __isl_keep isl_basic_set
*bset
, int *elim
)
1854 return reduced_using_equalities(dst
, src
,
1855 bset_to_bmap(bset
), elim
);
1858 static __isl_give isl_basic_set
*isl_basic_set_reduce_using_equalities(
1859 __isl_take isl_basic_set
*bset
, __isl_take isl_basic_set
*context
)
1865 if (!bset
|| !context
)
1868 if (context
->n_eq
== 0) {
1869 isl_basic_set_free(context
);
1873 bset
= isl_basic_set_cow(bset
);
1874 dim
= isl_basic_set_dim(bset
, isl_dim_set
);
1878 elim
= isl_alloc_array(bset
->ctx
, int, dim
);
1881 set_compute_elimination_index(context
, elim
);
1882 for (i
= 0; i
< bset
->n_eq
; ++i
)
1883 set_reduced_using_equalities(bset
->eq
[i
], bset
->eq
[i
],
1885 for (i
= 0; i
< bset
->n_ineq
; ++i
)
1886 set_reduced_using_equalities(bset
->ineq
[i
], bset
->ineq
[i
],
1888 isl_basic_set_free(context
);
1890 bset
= isl_basic_set_simplify(bset
);
1891 bset
= isl_basic_set_finalize(bset
);
1894 isl_basic_set_free(bset
);
1895 isl_basic_set_free(context
);
1899 /* For each inequality in "ineq" that is a shifted (more relaxed)
1900 * copy of an inequality in "context", mark the corresponding entry
1902 * If an inequality only has a non-negative constant term, then
1905 static isl_stat
mark_shifted_constraints(__isl_keep isl_mat
*ineq
,
1906 __isl_keep isl_basic_set
*context
, int *row
)
1908 struct isl_constraint_index ci
;
1913 if (!ineq
|| !context
)
1914 return isl_stat_error
;
1915 if (context
->n_ineq
== 0)
1917 if (setup_constraint_index(&ci
, context
) < 0)
1918 return isl_stat_error
;
1920 n_ineq
= isl_mat_rows(ineq
);
1921 total
= isl_mat_cols(ineq
) - 1;
1922 for (k
= 0; k
< n_ineq
; ++k
) {
1926 l
= isl_seq_first_non_zero(ineq
->row
[k
] + 1, total
);
1927 if (l
< 0 && isl_int_is_nonneg(ineq
->row
[k
][0])) {
1931 redundant
= constraint_index_is_redundant(&ci
, ineq
->row
[k
]);
1938 constraint_index_free(&ci
);
1941 constraint_index_free(&ci
);
1942 return isl_stat_error
;
1945 static __isl_give isl_basic_set
*remove_shifted_constraints(
1946 __isl_take isl_basic_set
*bset
, __isl_keep isl_basic_set
*context
)
1948 struct isl_constraint_index ci
;
1951 if (!bset
|| !context
)
1954 if (context
->n_ineq
== 0)
1956 if (setup_constraint_index(&ci
, context
) < 0)
1959 for (k
= 0; k
< bset
->n_ineq
; ++k
) {
1962 redundant
= constraint_index_is_redundant(&ci
, bset
->ineq
[k
]);
1967 bset
= isl_basic_set_cow(bset
);
1970 isl_basic_set_drop_inequality(bset
, k
);
1973 constraint_index_free(&ci
);
1976 constraint_index_free(&ci
);
1980 /* Remove constraints from "bmap" that are identical to constraints
1981 * in "context" or that are more relaxed (greater constant term).
1983 * We perform the test for shifted copies on the pure constraints
1984 * in remove_shifted_constraints.
1986 static __isl_give isl_basic_map
*isl_basic_map_remove_shifted_constraints(
1987 __isl_take isl_basic_map
*bmap
, __isl_take isl_basic_map
*context
)
1989 isl_basic_set
*bset
, *bset_context
;
1991 if (!bmap
|| !context
)
1994 if (bmap
->n_ineq
== 0 || context
->n_ineq
== 0) {
1995 isl_basic_map_free(context
);
1999 context
= isl_basic_map_align_divs(context
, bmap
);
2000 bmap
= isl_basic_map_align_divs(bmap
, context
);
2002 bset
= isl_basic_map_underlying_set(isl_basic_map_copy(bmap
));
2003 bset_context
= isl_basic_map_underlying_set(context
);
2004 bset
= remove_shifted_constraints(bset
, bset_context
);
2005 isl_basic_set_free(bset_context
);
2007 bmap
= isl_basic_map_overlying_set(bset
, bmap
);
2011 isl_basic_map_free(bmap
);
2012 isl_basic_map_free(context
);
2016 /* Does the (linear part of a) constraint "c" involve any of the "len"
2017 * "relevant" dimensions?
2019 static int is_related(isl_int
*c
, int len
, int *relevant
)
2023 for (i
= 0; i
< len
; ++i
) {
2026 if (!isl_int_is_zero(c
[i
]))
2033 /* Drop constraints from "bmap" that do not involve any of
2034 * the dimensions marked "relevant".
2036 static __isl_give isl_basic_map
*drop_unrelated_constraints(
2037 __isl_take isl_basic_map
*bmap
, int *relevant
)
2042 dim
= isl_basic_map_dim(bmap
, isl_dim_all
);
2044 return isl_basic_map_free(bmap
);
2045 for (i
= 0; i
< dim
; ++i
)
2051 for (i
= bmap
->n_eq
- 1; i
>= 0; --i
)
2052 if (!is_related(bmap
->eq
[i
] + 1, dim
, relevant
)) {
2053 bmap
= isl_basic_map_cow(bmap
);
2054 if (isl_basic_map_drop_equality(bmap
, i
) < 0)
2055 return isl_basic_map_free(bmap
);
2058 for (i
= bmap
->n_ineq
- 1; i
>= 0; --i
)
2059 if (!is_related(bmap
->ineq
[i
] + 1, dim
, relevant
)) {
2060 bmap
= isl_basic_map_cow(bmap
);
2061 if (isl_basic_map_drop_inequality(bmap
, i
) < 0)
2062 return isl_basic_map_free(bmap
);
2068 /* Update the groups in "group" based on the (linear part of a) constraint "c".
2070 * In particular, for any variable involved in the constraint,
2071 * find the actual group id from before and replace the group
2072 * of the corresponding variable by the minimal group of all
2073 * the variables involved in the constraint considered so far
2074 * (if this minimum is smaller) or replace the minimum by this group
2075 * (if the minimum is larger).
2077 * At the end, all the variables in "c" will (indirectly) point
2078 * to the minimal of the groups that they referred to originally.
2080 static void update_groups(int dim
, int *group
, isl_int
*c
)
2085 for (j
= 0; j
< dim
; ++j
) {
2086 if (isl_int_is_zero(c
[j
]))
2088 while (group
[j
] >= 0 && group
[group
[j
]] != group
[j
])
2089 group
[j
] = group
[group
[j
]];
2090 if (group
[j
] == min
)
2092 if (group
[j
] < min
) {
2093 if (min
>= 0 && min
< dim
)
2094 group
[min
] = group
[j
];
2097 group
[group
[j
]] = min
;
2101 /* Allocate an array of groups of variables, one for each variable
2102 * in "context", initialized to zero.
2104 static int *alloc_groups(__isl_keep isl_basic_set
*context
)
2109 dim
= isl_basic_set_dim(context
, isl_dim_set
);
2112 ctx
= isl_basic_set_get_ctx(context
);
2113 return isl_calloc_array(ctx
, int, dim
);
2116 /* Drop constraints from "bmap" that only involve variables that are
2117 * not related to any of the variables marked with a "-1" in "group".
2119 * We construct groups of variables that collect variables that
2120 * (indirectly) appear in some common constraint of "bmap".
2121 * Each group is identified by the first variable in the group,
2122 * except for the special group of variables that was already identified
2123 * in the input as -1 (or are related to those variables).
2124 * If group[i] is equal to i (or -1), then the group of i is i (or -1),
2125 * otherwise the group of i is the group of group[i].
2127 * We first initialize groups for the remaining variables.
2128 * Then we iterate over the constraints of "bmap" and update the
2129 * group of the variables in the constraint by the smallest group.
2130 * Finally, we resolve indirect references to groups by running over
2133 * After computing the groups, we drop constraints that do not involve
2134 * any variables in the -1 group.
2136 __isl_give isl_basic_map
*isl_basic_map_drop_unrelated_constraints(
2137 __isl_take isl_basic_map
*bmap
, __isl_take
int *group
)
2143 dim
= isl_basic_map_dim(bmap
, isl_dim_all
);
2145 return isl_basic_map_free(bmap
);
2148 for (i
= 0; i
< dim
; ++i
)
2150 last
= group
[i
] = i
;
2156 for (i
= 0; i
< bmap
->n_eq
; ++i
)
2157 update_groups(dim
, group
, bmap
->eq
[i
] + 1);
2158 for (i
= 0; i
< bmap
->n_ineq
; ++i
)
2159 update_groups(dim
, group
, bmap
->ineq
[i
] + 1);
2161 for (i
= 0; i
< dim
; ++i
)
2163 group
[i
] = group
[group
[i
]];
2165 for (i
= 0; i
< dim
; ++i
)
2166 group
[i
] = group
[i
] == -1;
2168 bmap
= drop_unrelated_constraints(bmap
, group
);
2174 /* Drop constraints from "context" that are irrelevant for computing
2175 * the gist of "bset".
2177 * In particular, drop constraints in variables that are not related
2178 * to any of the variables involved in the constraints of "bset"
2179 * in the sense that there is no sequence of constraints that connects them.
2181 * We first mark all variables that appear in "bset" as belonging
2182 * to a "-1" group and then continue with group_and_drop_irrelevant_constraints.
2184 static __isl_give isl_basic_set
*drop_irrelevant_constraints(
2185 __isl_take isl_basic_set
*context
, __isl_keep isl_basic_set
*bset
)
2191 dim
= isl_basic_set_dim(bset
, isl_dim_set
);
2192 if (!context
|| dim
< 0)
2193 return isl_basic_set_free(context
);
2195 group
= alloc_groups(context
);
2198 return isl_basic_set_free(context
);
2200 for (i
= 0; i
< dim
; ++i
) {
2201 for (j
= 0; j
< bset
->n_eq
; ++j
)
2202 if (!isl_int_is_zero(bset
->eq
[j
][1 + i
]))
2204 if (j
< bset
->n_eq
) {
2208 for (j
= 0; j
< bset
->n_ineq
; ++j
)
2209 if (!isl_int_is_zero(bset
->ineq
[j
][1 + i
]))
2211 if (j
< bset
->n_ineq
)
2215 return isl_basic_map_drop_unrelated_constraints(context
, group
);
2218 /* Drop constraints from "context" that are irrelevant for computing
2219 * the gist of the inequalities "ineq".
2220 * Inequalities in "ineq" for which the corresponding element of row
2221 * is set to -1 have already been marked for removal and should be ignored.
2223 * In particular, drop constraints in variables that are not related
2224 * to any of the variables involved in "ineq"
2225 * in the sense that there is no sequence of constraints that connects them.
2227 * We first mark all variables that appear in "bset" as belonging
2228 * to a "-1" group and then continue with group_and_drop_irrelevant_constraints.
2230 static __isl_give isl_basic_set
*drop_irrelevant_constraints_marked(
2231 __isl_take isl_basic_set
*context
, __isl_keep isl_mat
*ineq
, int *row
)
2237 dim
= isl_basic_set_dim(context
, isl_dim_set
);
2238 if (dim
< 0 || !ineq
)
2239 return isl_basic_set_free(context
);
2241 group
= alloc_groups(context
);
2244 return isl_basic_set_free(context
);
2246 n
= isl_mat_rows(ineq
);
2247 for (i
= 0; i
< dim
; ++i
) {
2248 for (j
= 0; j
< n
; ++j
) {
2251 if (!isl_int_is_zero(ineq
->row
[j
][1 + i
]))
2258 return isl_basic_map_drop_unrelated_constraints(context
, group
);
2261 /* Do all "n" entries of "row" contain a negative value?
2263 static int all_neg(int *row
, int n
)
2267 for (i
= 0; i
< n
; ++i
)
2274 /* Update the inequalities in "bset" based on the information in "row"
2277 * In particular, the array "row" contains either -1, meaning that
2278 * the corresponding inequality of "bset" is redundant, or the index
2279 * of an inequality in "tab".
2281 * If the row entry is -1, then drop the inequality.
2282 * Otherwise, if the constraint is marked redundant in the tableau,
2283 * then drop the inequality. Similarly, if it is marked as an equality
2284 * in the tableau, then turn the inequality into an equality and
2285 * perform Gaussian elimination.
2287 static __isl_give isl_basic_set
*update_ineq(__isl_take isl_basic_set
*bset
,
2288 __isl_keep
int *row
, struct isl_tab
*tab
)
2293 int found_equality
= 0;
2297 if (tab
&& tab
->empty
)
2298 return isl_basic_set_set_to_empty(bset
);
2300 n_ineq
= bset
->n_ineq
;
2301 for (i
= n_ineq
- 1; i
>= 0; --i
) {
2303 if (isl_basic_set_drop_inequality(bset
, i
) < 0)
2304 return isl_basic_set_free(bset
);
2310 if (isl_tab_is_equality(tab
, n_eq
+ row
[i
])) {
2311 isl_basic_map_inequality_to_equality(bset
, i
);
2313 } else if (isl_tab_is_redundant(tab
, n_eq
+ row
[i
])) {
2314 if (isl_basic_set_drop_inequality(bset
, i
) < 0)
2315 return isl_basic_set_free(bset
);
2320 bset
= isl_basic_set_gauss(bset
, NULL
);
2321 bset
= isl_basic_set_finalize(bset
);
2325 /* Update the inequalities in "bset" based on the information in "row"
2326 * and "tab" and free all arguments (other than "bset").
2328 static __isl_give isl_basic_set
*update_ineq_free(
2329 __isl_take isl_basic_set
*bset
, __isl_take isl_mat
*ineq
,
2330 __isl_take isl_basic_set
*context
, __isl_take
int *row
,
2331 struct isl_tab
*tab
)
2334 isl_basic_set_free(context
);
2336 bset
= update_ineq(bset
, row
, tab
);
2343 /* Remove all information from bset that is redundant in the context
2345 * "ineq" contains the (possibly transformed) inequalities of "bset",
2346 * in the same order.
2347 * The (explicit) equalities of "bset" are assumed to have been taken
2348 * into account by the transformation such that only the inequalities
2350 * "context" is assumed not to be empty.
2352 * "row" keeps track of the constraint index of a "bset" inequality in "tab".
2353 * A value of -1 means that the inequality is obviously redundant and may
2354 * not even appear in "tab".
2356 * We first mark the inequalities of "bset"
2357 * that are obviously redundant with respect to some inequality in "context".
2358 * Then we remove those constraints from "context" that have become
2359 * irrelevant for computing the gist of "bset".
2360 * Note that this removal of constraints cannot be replaced by
2361 * a factorization because factors in "bset" may still be connected
2362 * to each other through constraints in "context".
2364 * If there are any inequalities left, we construct a tableau for
2365 * the context and then add the inequalities of "bset".
2366 * Before adding these inequalities, we freeze all constraints such that
2367 * they won't be considered redundant in terms of the constraints of "bset".
2368 * Then we detect all redundant constraints (among the
2369 * constraints that weren't frozen), first by checking for redundancy in the
2370 * the tableau and then by checking if replacing a constraint by its negation
2371 * would lead to an empty set. This last step is fairly expensive
2372 * and could be optimized by more reuse of the tableau.
2373 * Finally, we update bset according to the results.
2375 static __isl_give isl_basic_set
*uset_gist_full(__isl_take isl_basic_set
*bset
,
2376 __isl_take isl_mat
*ineq
, __isl_take isl_basic_set
*context
)
2381 isl_basic_set
*combined
= NULL
;
2382 struct isl_tab
*tab
= NULL
;
2383 unsigned n_eq
, context_ineq
;
2385 if (!bset
|| !ineq
|| !context
)
2388 if (bset
->n_ineq
== 0 || isl_basic_set_plain_is_universe(context
)) {
2389 isl_basic_set_free(context
);
2394 ctx
= isl_basic_set_get_ctx(context
);
2395 row
= isl_calloc_array(ctx
, int, bset
->n_ineq
);
2399 if (mark_shifted_constraints(ineq
, context
, row
) < 0)
2401 if (all_neg(row
, bset
->n_ineq
))
2402 return update_ineq_free(bset
, ineq
, context
, row
, NULL
);
2404 context
= drop_irrelevant_constraints_marked(context
, ineq
, row
);
2407 if (isl_basic_set_plain_is_universe(context
))
2408 return update_ineq_free(bset
, ineq
, context
, row
, NULL
);
2410 n_eq
= context
->n_eq
;
2411 context_ineq
= context
->n_ineq
;
2412 combined
= isl_basic_set_cow(isl_basic_set_copy(context
));
2413 combined
= isl_basic_set_extend_constraints(combined
, 0, bset
->n_ineq
);
2414 tab
= isl_tab_from_basic_set(combined
, 0);
2415 for (i
= 0; i
< context_ineq
; ++i
)
2416 if (isl_tab_freeze_constraint(tab
, n_eq
+ i
) < 0)
2418 if (isl_tab_extend_cons(tab
, bset
->n_ineq
) < 0)
2421 for (i
= 0; i
< bset
->n_ineq
; ++i
) {
2424 combined
= isl_basic_set_add_ineq(combined
, ineq
->row
[i
]);
2425 if (isl_tab_add_ineq(tab
, ineq
->row
[i
]) < 0)
2429 if (isl_tab_detect_implicit_equalities(tab
) < 0)
2431 if (isl_tab_detect_redundant(tab
) < 0)
2433 for (i
= bset
->n_ineq
- 1; i
>= 0; --i
) {
2434 isl_basic_set
*test
;
2440 if (tab
->con
[n_eq
+ r
].is_redundant
)
2442 test
= isl_basic_set_dup(combined
);
2443 test
= isl_inequality_negate(test
, r
);
2444 test
= isl_basic_set_update_from_tab(test
, tab
);
2445 is_empty
= isl_basic_set_is_empty(test
);
2446 isl_basic_set_free(test
);
2450 tab
->con
[n_eq
+ r
].is_redundant
= 1;
2452 bset
= update_ineq_free(bset
, ineq
, context
, row
, tab
);
2454 ISL_F_SET(bset
, ISL_BASIC_SET_NO_IMPLICIT
);
2455 ISL_F_SET(bset
, ISL_BASIC_SET_NO_REDUNDANT
);
2458 isl_basic_set_free(combined
);
2464 isl_basic_set_free(combined
);
2465 isl_basic_set_free(context
);
2466 isl_basic_set_free(bset
);
2470 /* Extract the inequalities of "bset" as an isl_mat.
2472 static __isl_give isl_mat
*extract_ineq(__isl_keep isl_basic_set
*bset
)
2478 total
= isl_basic_set_dim(bset
, isl_dim_all
);
2482 ctx
= isl_basic_set_get_ctx(bset
);
2483 ineq
= isl_mat_sub_alloc6(ctx
, bset
->ineq
, 0, bset
->n_ineq
,
2489 /* Remove all information from "bset" that is redundant in the context
2490 * of "context", for the case where both "bset" and "context" are
2493 static __isl_give isl_basic_set
*uset_gist_uncompressed(
2494 __isl_take isl_basic_set
*bset
, __isl_take isl_basic_set
*context
)
2498 ineq
= extract_ineq(bset
);
2499 return uset_gist_full(bset
, ineq
, context
);
2502 /* Remove all information from "bset" that is redundant in the context
2503 * of "context", for the case where the combined equalities of
2504 * "bset" and "context" allow for a compression that can be obtained
2505 * by preapplication of "T".
2507 * "bset" itself is not transformed by "T". Instead, the inequalities
2508 * are extracted from "bset" and those are transformed by "T".
2509 * uset_gist_full then determines which of the transformed inequalities
2510 * are redundant with respect to the transformed "context" and removes
2511 * the corresponding inequalities from "bset".
2513 * After preapplying "T" to the inequalities, any common factor is
2514 * removed from the coefficients. If this results in a tightening
2515 * of the constant term, then the same tightening is applied to
2516 * the corresponding untransformed inequality in "bset".
2517 * That is, if after plugging in T, a constraint f(x) >= 0 is of the form
2521 * with 0 <= r < g, then it is equivalent to
2525 * This means that f(x) >= 0 is equivalent to f(x) - r >= 0 in the affine
2526 * subspace compressed by T since the latter would be transformed to
2530 static __isl_give isl_basic_set
*uset_gist_compressed(
2531 __isl_take isl_basic_set
*bset
, __isl_take isl_basic_set
*context
,
2532 __isl_take isl_mat
*T
)
2536 int i
, n_row
, n_col
;
2539 ineq
= extract_ineq(bset
);
2540 ineq
= isl_mat_product(ineq
, isl_mat_copy(T
));
2541 context
= isl_basic_set_preimage(context
, T
);
2543 if (!ineq
|| !context
)
2545 if (isl_basic_set_plain_is_empty(context
)) {
2547 isl_basic_set_free(context
);
2548 return isl_basic_set_set_to_empty(bset
);
2551 ctx
= isl_mat_get_ctx(ineq
);
2552 n_row
= isl_mat_rows(ineq
);
2553 n_col
= isl_mat_cols(ineq
);
2555 for (i
= 0; i
< n_row
; ++i
) {
2556 isl_seq_gcd(ineq
->row
[i
] + 1, n_col
- 1, &ctx
->normalize_gcd
);
2557 if (isl_int_is_zero(ctx
->normalize_gcd
))
2559 if (isl_int_is_one(ctx
->normalize_gcd
))
2561 isl_seq_scale_down(ineq
->row
[i
] + 1, ineq
->row
[i
] + 1,
2562 ctx
->normalize_gcd
, n_col
- 1);
2563 isl_int_fdiv_r(rem
, ineq
->row
[i
][0], ctx
->normalize_gcd
);
2564 isl_int_fdiv_q(ineq
->row
[i
][0],
2565 ineq
->row
[i
][0], ctx
->normalize_gcd
);
2566 if (isl_int_is_zero(rem
))
2568 bset
= isl_basic_set_cow(bset
);
2571 isl_int_sub(bset
->ineq
[i
][0], bset
->ineq
[i
][0], rem
);
2575 return uset_gist_full(bset
, ineq
, context
);
2578 isl_basic_set_free(context
);
2579 isl_basic_set_free(bset
);
2583 /* Project "bset" onto the variables that are involved in "template".
2585 static __isl_give isl_basic_set
*project_onto_involved(
2586 __isl_take isl_basic_set
*bset
, __isl_keep isl_basic_set
*template)
2591 n
= isl_basic_set_dim(template, isl_dim_set
);
2592 if (n
< 0 || !template)
2593 return isl_basic_set_free(bset
);
2595 for (i
= 0; i
< n
; ++i
) {
2598 involved
= isl_basic_set_involves_dims(template,
2601 return isl_basic_set_free(bset
);
2604 bset
= isl_basic_set_eliminate_vars(bset
, i
, 1);
2610 /* Remove all information from bset that is redundant in the context
2611 * of context. In particular, equalities that are linear combinations
2612 * of those in context are removed. Then the inequalities that are
2613 * redundant in the context of the equalities and inequalities of
2614 * context are removed.
2616 * First of all, we drop those constraints from "context"
2617 * that are irrelevant for computing the gist of "bset".
2618 * Alternatively, we could factorize the intersection of "context" and "bset".
2620 * We first compute the intersection of the integer affine hulls
2621 * of "bset" and "context",
2622 * compute the gist inside this intersection and then reduce
2623 * the constraints with respect to the equalities of the context
2624 * that only involve variables already involved in the input.
2626 * If two constraints are mutually redundant, then uset_gist_full
2627 * will remove the second of those constraints. We therefore first
2628 * sort the constraints so that constraints not involving existentially
2629 * quantified variables are given precedence over those that do.
2630 * We have to perform this sorting before the variable compression,
2631 * because that may effect the order of the variables.
2633 static __isl_give isl_basic_set
*uset_gist(__isl_take isl_basic_set
*bset
,
2634 __isl_take isl_basic_set
*context
)
2639 isl_basic_set
*aff_context
;
2642 total
= isl_basic_set_dim(bset
, isl_dim_all
);
2643 if (total
< 0 || !context
)
2646 context
= drop_irrelevant_constraints(context
, bset
);
2648 bset
= isl_basic_set_detect_equalities(bset
);
2649 aff
= isl_basic_set_copy(bset
);
2650 aff
= isl_basic_set_plain_affine_hull(aff
);
2651 context
= isl_basic_set_detect_equalities(context
);
2652 aff_context
= isl_basic_set_copy(context
);
2653 aff_context
= isl_basic_set_plain_affine_hull(aff_context
);
2654 aff
= isl_basic_set_intersect(aff
, aff_context
);
2657 if (isl_basic_set_plain_is_empty(aff
)) {
2658 isl_basic_set_free(bset
);
2659 isl_basic_set_free(context
);
2662 bset
= isl_basic_set_sort_constraints(bset
);
2663 if (aff
->n_eq
== 0) {
2664 isl_basic_set_free(aff
);
2665 return uset_gist_uncompressed(bset
, context
);
2667 eq
= isl_mat_sub_alloc6(bset
->ctx
, aff
->eq
, 0, aff
->n_eq
, 0, 1 + total
);
2668 eq
= isl_mat_cow(eq
);
2669 T
= isl_mat_variable_compression(eq
, NULL
);
2670 isl_basic_set_free(aff
);
2671 if (T
&& T
->n_col
== 0) {
2673 isl_basic_set_free(context
);
2674 return isl_basic_set_set_to_empty(bset
);
2677 aff_context
= isl_basic_set_affine_hull(isl_basic_set_copy(context
));
2678 aff_context
= project_onto_involved(aff_context
, bset
);
2680 bset
= uset_gist_compressed(bset
, context
, T
);
2681 bset
= isl_basic_set_reduce_using_equalities(bset
, aff_context
);
2684 ISL_F_SET(bset
, ISL_BASIC_SET_NO_IMPLICIT
);
2685 ISL_F_SET(bset
, ISL_BASIC_SET_NO_REDUNDANT
);
2690 isl_basic_set_free(bset
);
2691 isl_basic_set_free(context
);
2695 /* Return the number of equality constraints in "bmap" that involve
2696 * local variables. This function assumes that Gaussian elimination
2697 * has been applied to the equality constraints.
2699 static int n_div_eq(__isl_keep isl_basic_map
*bmap
)
2702 isl_size total
, n_div
;
2707 if (bmap
->n_eq
== 0)
2710 total
= isl_basic_map_dim(bmap
, isl_dim_all
);
2711 n_div
= isl_basic_map_dim(bmap
, isl_dim_div
);
2712 if (total
< 0 || n_div
< 0)
2716 for (i
= 0; i
< bmap
->n_eq
; ++i
)
2717 if (isl_seq_first_non_zero(bmap
->eq
[i
] + 1 + total
,
2724 /* Construct a basic map in "space" defined by the equality constraints in "eq".
2725 * The constraints are assumed not to involve any local variables.
2727 static __isl_give isl_basic_map
*basic_map_from_equalities(
2728 __isl_take isl_space
*space
, __isl_take isl_mat
*eq
)
2732 isl_basic_map
*bmap
= NULL
;
2734 total
= isl_space_dim(space
, isl_dim_all
);
2735 if (total
< 0 || !eq
)
2738 if (1 + total
!= eq
->n_col
)
2739 isl_die(isl_space_get_ctx(space
), isl_error_internal
,
2740 "unexpected number of columns", goto error
);
2742 bmap
= isl_basic_map_alloc_space(isl_space_copy(space
),
2744 for (i
= 0; i
< eq
->n_row
; ++i
) {
2745 k
= isl_basic_map_alloc_equality(bmap
);
2748 isl_seq_cpy(bmap
->eq
[k
], eq
->row
[i
], eq
->n_col
);
2751 isl_space_free(space
);
2755 isl_space_free(space
);
2757 isl_basic_map_free(bmap
);
2761 /* Construct and return a variable compression based on the equality
2762 * constraints in "bmap1" and "bmap2" that do not involve the local variables.
2763 * "n1" is the number of (initial) equality constraints in "bmap1"
2764 * that do involve local variables.
2765 * "n2" is the number of (initial) equality constraints in "bmap2"
2766 * that do involve local variables.
2767 * "total" is the total number of other variables.
2768 * This function assumes that Gaussian elimination
2769 * has been applied to the equality constraints in both "bmap1" and "bmap2"
2770 * such that the equality constraints not involving local variables
2771 * are those that start at "n1" or "n2".
2773 * If either of "bmap1" and "bmap2" does not have such equality constraints,
2774 * then simply compute the compression based on the equality constraints
2775 * in the other basic map.
2776 * Otherwise, combine the equality constraints from both into a new
2777 * basic map such that Gaussian elimination can be applied to this combination
2778 * and then construct a variable compression from the resulting
2779 * equality constraints.
2781 static __isl_give isl_mat
*combined_variable_compression(
2782 __isl_keep isl_basic_map
*bmap1
, int n1
,
2783 __isl_keep isl_basic_map
*bmap2
, int n2
, int total
)
2786 isl_mat
*E1
, *E2
, *V
;
2787 isl_basic_map
*bmap
;
2789 ctx
= isl_basic_map_get_ctx(bmap1
);
2790 if (bmap1
->n_eq
== n1
) {
2791 E2
= isl_mat_sub_alloc6(ctx
, bmap2
->eq
,
2792 n2
, bmap2
->n_eq
- n2
, 0, 1 + total
);
2793 return isl_mat_variable_compression(E2
, NULL
);
2795 if (bmap2
->n_eq
== n2
) {
2796 E1
= isl_mat_sub_alloc6(ctx
, bmap1
->eq
,
2797 n1
, bmap1
->n_eq
- n1
, 0, 1 + total
);
2798 return isl_mat_variable_compression(E1
, NULL
);
2800 E1
= isl_mat_sub_alloc6(ctx
, bmap1
->eq
,
2801 n1
, bmap1
->n_eq
- n1
, 0, 1 + total
);
2802 E2
= isl_mat_sub_alloc6(ctx
, bmap2
->eq
,
2803 n2
, bmap2
->n_eq
- n2
, 0, 1 + total
);
2804 E1
= isl_mat_concat(E1
, E2
);
2805 bmap
= basic_map_from_equalities(isl_basic_map_get_space(bmap1
), E1
);
2806 bmap
= isl_basic_map_gauss(bmap
, NULL
);
2809 E1
= isl_mat_sub_alloc6(ctx
, bmap
->eq
, 0, bmap
->n_eq
, 0, 1 + total
);
2810 V
= isl_mat_variable_compression(E1
, NULL
);
2811 isl_basic_map_free(bmap
);
2816 /* Extract the stride constraints from "bmap", compressed
2817 * with respect to both the stride constraints in "context" and
2818 * the remaining equality constraints in both "bmap" and "context".
2819 * "bmap_n_eq" is the number of (initial) stride constraints in "bmap".
2820 * "context_n_eq" is the number of (initial) stride constraints in "context".
2822 * Let x be all variables in "bmap" (and "context") other than the local
2823 * variables. First compute a variable compression
2827 * based on the non-stride equality constraints in "bmap" and "context".
2828 * Consider the stride constraints of "context",
2832 * with y the local variables and plug in the variable compression,
2835 * A(V x') + B(y) = 0
2837 * Use these constraints to compute a parameter compression on x'
2841 * Now consider the stride constraints of "bmap"
2845 * and plug in x = V*T x''.
2846 * That is, return A = [C*V*T D].
2848 static __isl_give isl_mat
*extract_compressed_stride_constraints(
2849 __isl_keep isl_basic_map
*bmap
, int bmap_n_eq
,
2850 __isl_keep isl_basic_map
*context
, int context_n_eq
)
2852 isl_size total
, n_div
;
2854 isl_mat
*A
, *B
, *T
, *V
;
2856 total
= isl_basic_map_dim(context
, isl_dim_all
);
2857 n_div
= isl_basic_map_dim(context
, isl_dim_div
);
2858 if (total
< 0 || n_div
< 0)
2862 ctx
= isl_basic_map_get_ctx(bmap
);
2864 V
= combined_variable_compression(bmap
, bmap_n_eq
,
2865 context
, context_n_eq
, total
);
2867 A
= isl_mat_sub_alloc6(ctx
, context
->eq
, 0, context_n_eq
, 0, 1 + total
);
2868 B
= isl_mat_sub_alloc6(ctx
, context
->eq
,
2869 0, context_n_eq
, 1 + total
, n_div
);
2870 A
= isl_mat_product(A
, isl_mat_copy(V
));
2871 T
= isl_mat_parameter_compression_ext(A
, B
);
2872 T
= isl_mat_product(V
, T
);
2874 n_div
= isl_basic_map_dim(bmap
, isl_dim_div
);
2876 T
= isl_mat_free(T
);
2878 T
= isl_mat_diagonal(T
, isl_mat_identity(ctx
, n_div
));
2880 A
= isl_mat_sub_alloc6(ctx
, bmap
->eq
,
2881 0, bmap_n_eq
, 0, 1 + total
+ n_div
);
2882 A
= isl_mat_product(A
, T
);
2887 /* Remove the prime factors from *g that have an exponent that
2888 * is strictly smaller than the exponent in "c".
2889 * All exponents in *g are known to be smaller than or equal
2892 * That is, if *g is equal to
2894 * p_1^{e_1} p_2^{e_2} ... p_n^{e_n}
2896 * and "c" is equal to
2898 * p_1^{f_1} p_2^{f_2} ... p_n^{f_n}
2902 * p_1^{e_1 * (e_1 = f_1)} p_2^{e_2 * (e_2 = f_2)} ...
2903 * p_n^{e_n * (e_n = f_n)}
2905 * If e_i = f_i, then c / *g does not have any p_i factors and therefore
2906 * neither does the gcd of *g and c / *g.
2907 * If e_i < f_i, then the gcd of *g and c / *g has a positive
2908 * power min(e_i, s_i) of p_i with s_i = f_i - e_i among its factors.
2909 * Dividing *g by this gcd therefore strictly reduces the exponent
2910 * of the prime factors that need to be removed, while leaving the
2911 * other prime factors untouched.
2912 * Repeating this process until gcd(*g, c / *g) = 1 therefore
2913 * removes all undesired factors, without removing any others.
2915 static void remove_incomplete_powers(isl_int
*g
, isl_int c
)
2921 isl_int_divexact(t
, c
, *g
);
2922 isl_int_gcd(t
, t
, *g
);
2923 if (isl_int_is_one(t
))
2925 isl_int_divexact(*g
, *g
, t
);
2930 /* Reduce the "n" stride constraints in "bmap" based on a copy "A"
2931 * of the same stride constraints in a compressed space that exploits
2932 * all equalities in the context and the other equalities in "bmap".
2934 * If the stride constraints of "bmap" are of the form
2938 * then A is of the form
2942 * If any of these constraints involves only a single local variable y,
2943 * then the constraint appears as
2953 * Let g be the gcd of m and the coefficients of h.
2954 * Then, in particular, g is a divisor of the coefficients of h and
2958 * is known to be a multiple of g.
2959 * If some prime factor in m appears with the same exponent in g,
2960 * then it can be removed from m because f(x) is already known
2961 * to be a multiple of g and therefore in particular of this power
2962 * of the prime factors.
2963 * Prime factors that appear with a smaller exponent in g cannot
2964 * be removed from m.
2965 * Let g' be the divisor of g containing all prime factors that
2966 * appear with the same exponent in m and g, then
2970 * can be replaced by
2972 * f(x) + m/g' y_i' = 0
2974 * Note that (if g' != 1) this changes the explicit representation
2975 * of y_i to that of y_i', so the integer division at position i
2976 * is marked unknown and later recomputed by a call to
2977 * isl_basic_map_gauss.
2979 static __isl_give isl_basic_map
*reduce_stride_constraints(
2980 __isl_take isl_basic_map
*bmap
, int n
, __isl_keep isl_mat
*A
)
2983 isl_size total
, n_div
;
2987 total
= isl_basic_map_dim(bmap
, isl_dim_all
);
2988 n_div
= isl_basic_map_dim(bmap
, isl_dim_div
);
2989 if (total
< 0 || n_div
< 0 || !A
)
2990 return isl_basic_map_free(bmap
);
2994 for (i
= 0; i
< n
; ++i
) {
2997 div
= isl_seq_first_non_zero(bmap
->eq
[i
] + 1 + total
, n_div
);
2999 isl_die(isl_basic_map_get_ctx(bmap
), isl_error_internal
,
3000 "equality constraints modified unexpectedly",
3002 if (isl_seq_first_non_zero(bmap
->eq
[i
] + 1 + total
+ div
+ 1,
3003 n_div
- div
- 1) != -1)
3005 if (isl_mat_row_gcd(A
, i
, &gcd
) < 0)
3007 if (isl_int_is_one(gcd
))
3009 remove_incomplete_powers(&gcd
, bmap
->eq
[i
][1 + total
+ div
]);
3010 if (isl_int_is_one(gcd
))
3012 isl_int_divexact(bmap
->eq
[i
][1 + total
+ div
],
3013 bmap
->eq
[i
][1 + total
+ div
], gcd
);
3014 bmap
= isl_basic_map_mark_div_unknown(bmap
, div
);
3022 bmap
= isl_basic_map_gauss(bmap
, NULL
);
3027 isl_basic_map_free(bmap
);
3031 /* Simplify the stride constraints in "bmap" based on
3032 * the remaining equality constraints in "bmap" and all equality
3033 * constraints in "context".
3034 * Only do this if both "bmap" and "context" have stride constraints.
3036 * First extract a copy of the stride constraints in "bmap" in a compressed
3037 * space exploiting all the other equality constraints and then
3038 * use this compressed copy to simplify the original stride constraints.
3040 static __isl_give isl_basic_map
*gist_strides(__isl_take isl_basic_map
*bmap
,
3041 __isl_keep isl_basic_map
*context
)
3043 int bmap_n_eq
, context_n_eq
;
3046 if (!bmap
|| !context
)
3047 return isl_basic_map_free(bmap
);
3049 bmap_n_eq
= n_div_eq(bmap
);
3050 context_n_eq
= n_div_eq(context
);
3052 if (bmap_n_eq
< 0 || context_n_eq
< 0)
3053 return isl_basic_map_free(bmap
);
3054 if (bmap_n_eq
== 0 || context_n_eq
== 0)
3057 A
= extract_compressed_stride_constraints(bmap
, bmap_n_eq
,
3058 context
, context_n_eq
);
3059 bmap
= reduce_stride_constraints(bmap
, bmap_n_eq
, A
);
3066 /* Return a basic map that has the same intersection with "context" as "bmap"
3067 * and that is as "simple" as possible.
3069 * The core computation is performed on the pure constraints.
3070 * When we add back the meaning of the integer divisions, we need
3071 * to (re)introduce the div constraints. If we happen to have
3072 * discovered that some of these integer divisions are equal to
3073 * some affine combination of other variables, then these div
3074 * constraints may end up getting simplified in terms of the equalities,
3075 * resulting in extra inequalities on the other variables that
3076 * may have been removed already or that may not even have been
3077 * part of the input. We try and remove those constraints of
3078 * this form that are most obviously redundant with respect to
3079 * the context. We also remove those div constraints that are
3080 * redundant with respect to the other constraints in the result.
3082 * The stride constraints among the equality constraints in "bmap" are
3083 * also simplified with respecting to the other equality constraints
3084 * in "bmap" and with respect to all equality constraints in "context".
3086 __isl_give isl_basic_map
*isl_basic_map_gist(__isl_take isl_basic_map
*bmap
,
3087 __isl_take isl_basic_map
*context
)
3089 isl_basic_set
*bset
, *eq
;
3090 isl_basic_map
*eq_bmap
;
3091 isl_size total
, n_div
, n_div_bmap
;
3092 unsigned extra
, n_eq
, n_ineq
;
3094 if (!bmap
|| !context
)
3097 if (isl_basic_map_plain_is_universe(bmap
)) {
3098 isl_basic_map_free(context
);
3101 if (isl_basic_map_plain_is_empty(context
)) {
3102 isl_space
*space
= isl_basic_map_get_space(bmap
);
3103 isl_basic_map_free(bmap
);
3104 isl_basic_map_free(context
);
3105 return isl_basic_map_universe(space
);
3107 if (isl_basic_map_plain_is_empty(bmap
)) {
3108 isl_basic_map_free(context
);
3112 bmap
= isl_basic_map_remove_redundancies(bmap
);
3113 context
= isl_basic_map_remove_redundancies(context
);
3114 context
= isl_basic_map_align_divs(context
, bmap
);
3116 n_div
= isl_basic_map_dim(context
, isl_dim_div
);
3117 total
= isl_basic_map_dim(bmap
, isl_dim_all
);
3118 n_div_bmap
= isl_basic_map_dim(bmap
, isl_dim_div
);
3119 if (n_div
< 0 || total
< 0 || n_div_bmap
< 0)
3121 extra
= n_div
- n_div_bmap
;
3123 bset
= isl_basic_map_underlying_set(isl_basic_map_copy(bmap
));
3124 bset
= isl_basic_set_add_dims(bset
, isl_dim_set
, extra
);
3125 bset
= uset_gist(bset
,
3126 isl_basic_map_underlying_set(isl_basic_map_copy(context
)));
3127 bset
= isl_basic_set_project_out(bset
, isl_dim_set
, total
, extra
);
3129 if (!bset
|| bset
->n_eq
== 0 || n_div
== 0 ||
3130 isl_basic_set_plain_is_empty(bset
)) {
3131 isl_basic_map_free(context
);
3132 return isl_basic_map_overlying_set(bset
, bmap
);
3136 n_ineq
= bset
->n_ineq
;
3137 eq
= isl_basic_set_copy(bset
);
3138 eq
= isl_basic_set_cow(eq
);
3139 if (isl_basic_set_free_inequality(eq
, n_ineq
) < 0)
3140 eq
= isl_basic_set_free(eq
);
3141 if (isl_basic_set_free_equality(bset
, n_eq
) < 0)
3142 bset
= isl_basic_set_free(bset
);
3144 eq_bmap
= isl_basic_map_overlying_set(eq
, isl_basic_map_copy(bmap
));
3145 eq_bmap
= gist_strides(eq_bmap
, context
);
3146 eq_bmap
= isl_basic_map_remove_shifted_constraints(eq_bmap
, context
);
3147 bmap
= isl_basic_map_overlying_set(bset
, bmap
);
3148 bmap
= isl_basic_map_intersect(bmap
, eq_bmap
);
3149 bmap
= isl_basic_map_remove_redundancies(bmap
);
3153 isl_basic_map_free(bmap
);
3154 isl_basic_map_free(context
);
3159 * Assumes context has no implicit divs.
3161 __isl_give isl_map
*isl_map_gist_basic_map(__isl_take isl_map
*map
,
3162 __isl_take isl_basic_map
*context
)
3166 if (!map
|| !context
)
3169 if (isl_basic_map_plain_is_empty(context
)) {
3170 isl_space
*space
= isl_map_get_space(map
);
3172 isl_basic_map_free(context
);
3173 return isl_map_universe(space
);
3176 context
= isl_basic_map_remove_redundancies(context
);
3177 map
= isl_map_cow(map
);
3178 if (!map
|| !context
)
3180 isl_assert(map
->ctx
, isl_space_is_equal(map
->dim
, context
->dim
), goto error
);
3181 map
= isl_map_compute_divs(map
);
3184 for (i
= map
->n
- 1; i
>= 0; --i
) {
3185 map
->p
[i
] = isl_basic_map_gist(map
->p
[i
],
3186 isl_basic_map_copy(context
));
3189 if (isl_basic_map_plain_is_empty(map
->p
[i
])) {
3190 isl_basic_map_free(map
->p
[i
]);
3191 if (i
!= map
->n
- 1)
3192 map
->p
[i
] = map
->p
[map
->n
- 1];
3196 isl_basic_map_free(context
);
3197 ISL_F_CLR(map
, ISL_MAP_NORMALIZED
);
3201 isl_basic_map_free(context
);
3205 /* Drop all inequalities from "bmap" that also appear in "context".
3206 * "context" is assumed to have only known local variables and
3207 * the initial local variables of "bmap" are assumed to be the same
3208 * as those of "context".
3209 * The constraints of both "bmap" and "context" are assumed
3210 * to have been sorted using isl_basic_map_sort_constraints.
3212 * Run through the inequality constraints of "bmap" and "context"
3214 * If a constraint of "bmap" involves variables not in "context",
3215 * then it cannot appear in "context".
3216 * If a matching constraint is found, it is removed from "bmap".
3218 static __isl_give isl_basic_map
*drop_inequalities(
3219 __isl_take isl_basic_map
*bmap
, __isl_keep isl_basic_map
*context
)
3222 isl_size total
, bmap_total
;
3225 total
= isl_basic_map_dim(context
, isl_dim_all
);
3226 bmap_total
= isl_basic_map_dim(bmap
, isl_dim_all
);
3227 if (total
< 0 || bmap_total
< 0)
3228 return isl_basic_map_free(bmap
);
3230 extra
= bmap_total
- total
;
3232 i1
= bmap
->n_ineq
- 1;
3233 i2
= context
->n_ineq
- 1;
3234 while (bmap
&& i1
>= 0 && i2
>= 0) {
3237 if (isl_seq_first_non_zero(bmap
->ineq
[i1
] + 1 + total
,
3242 cmp
= isl_basic_map_constraint_cmp(context
, bmap
->ineq
[i1
],
3252 if (isl_int_eq(bmap
->ineq
[i1
][0], context
->ineq
[i2
][0])) {
3253 bmap
= isl_basic_map_cow(bmap
);
3254 if (isl_basic_map_drop_inequality(bmap
, i1
) < 0)
3255 bmap
= isl_basic_map_free(bmap
);
3264 /* Drop all equalities from "bmap" that also appear in "context".
3265 * "context" is assumed to have only known local variables and
3266 * the initial local variables of "bmap" are assumed to be the same
3267 * as those of "context".
3269 * Run through the equality constraints of "bmap" and "context"
3271 * If a constraint of "bmap" involves variables not in "context",
3272 * then it cannot appear in "context".
3273 * If a matching constraint is found, it is removed from "bmap".
3275 static __isl_give isl_basic_map
*drop_equalities(
3276 __isl_take isl_basic_map
*bmap
, __isl_keep isl_basic_map
*context
)
3279 isl_size total
, bmap_total
;
3282 total
= isl_basic_map_dim(context
, isl_dim_all
);
3283 bmap_total
= isl_basic_map_dim(bmap
, isl_dim_all
);
3284 if (total
< 0 || bmap_total
< 0)
3285 return isl_basic_map_free(bmap
);
3287 extra
= bmap_total
- total
;
3289 i1
= bmap
->n_eq
- 1;
3290 i2
= context
->n_eq
- 1;
3292 while (bmap
&& i1
>= 0 && i2
>= 0) {
3295 if (isl_seq_first_non_zero(bmap
->eq
[i1
] + 1 + total
,
3298 last1
= isl_seq_last_non_zero(bmap
->eq
[i1
] + 1, total
);
3299 last2
= isl_seq_last_non_zero(context
->eq
[i2
] + 1, total
);
3300 if (last1
> last2
) {
3304 if (last1
< last2
) {
3308 if (isl_seq_eq(bmap
->eq
[i1
], context
->eq
[i2
], 1 + total
)) {
3309 bmap
= isl_basic_map_cow(bmap
);
3310 if (isl_basic_map_drop_equality(bmap
, i1
) < 0)
3311 bmap
= isl_basic_map_free(bmap
);
3320 /* Remove the constraints in "context" from "bmap".
3321 * "context" is assumed to have explicit representations
3322 * for all local variables.
3324 * First align the divs of "bmap" to those of "context" and
3325 * sort the constraints. Then drop all constraints from "bmap"
3326 * that appear in "context".
3328 __isl_give isl_basic_map
*isl_basic_map_plain_gist(
3329 __isl_take isl_basic_map
*bmap
, __isl_take isl_basic_map
*context
)
3331 isl_bool done
, known
;
3333 done
= isl_basic_map_plain_is_universe(context
);
3334 if (done
== isl_bool_false
)
3335 done
= isl_basic_map_plain_is_universe(bmap
);
3336 if (done
== isl_bool_false
)
3337 done
= isl_basic_map_plain_is_empty(context
);
3338 if (done
== isl_bool_false
)
3339 done
= isl_basic_map_plain_is_empty(bmap
);
3343 isl_basic_map_free(context
);
3346 known
= isl_basic_map_divs_known(context
);
3350 isl_die(isl_basic_map_get_ctx(bmap
), isl_error_invalid
,
3351 "context has unknown divs", goto error
);
3353 bmap
= isl_basic_map_align_divs(bmap
, context
);
3354 bmap
= isl_basic_map_gauss(bmap
, NULL
);
3355 bmap
= isl_basic_map_sort_constraints(bmap
);
3356 context
= isl_basic_map_sort_constraints(context
);
3358 bmap
= drop_inequalities(bmap
, context
);
3359 bmap
= drop_equalities(bmap
, context
);
3361 isl_basic_map_free(context
);
3362 bmap
= isl_basic_map_finalize(bmap
);
3365 isl_basic_map_free(bmap
);
3366 isl_basic_map_free(context
);
3370 /* Replace "map" by the disjunct at position "pos" and free "context".
3372 static __isl_give isl_map
*replace_by_disjunct(__isl_take isl_map
*map
,
3373 int pos
, __isl_take isl_basic_map
*context
)
3375 isl_basic_map
*bmap
;
3377 bmap
= isl_basic_map_copy(map
->p
[pos
]);
3379 isl_basic_map_free(context
);
3380 return isl_map_from_basic_map(bmap
);
3383 /* Remove the constraints in "context" from "map".
3384 * If any of the disjuncts in the result turns out to be the universe,
3385 * then return this universe.
3386 * "context" is assumed to have explicit representations
3387 * for all local variables.
3389 __isl_give isl_map
*isl_map_plain_gist_basic_map(__isl_take isl_map
*map
,
3390 __isl_take isl_basic_map
*context
)
3393 isl_bool univ
, known
;
3395 univ
= isl_basic_map_plain_is_universe(context
);
3399 isl_basic_map_free(context
);
3402 known
= isl_basic_map_divs_known(context
);
3406 isl_die(isl_map_get_ctx(map
), isl_error_invalid
,
3407 "context has unknown divs", goto error
);
3409 map
= isl_map_cow(map
);
3412 for (i
= 0; i
< map
->n
; ++i
) {
3413 map
->p
[i
] = isl_basic_map_plain_gist(map
->p
[i
],
3414 isl_basic_map_copy(context
));
3415 univ
= isl_basic_map_plain_is_universe(map
->p
[i
]);
3418 if (univ
&& map
->n
> 1)
3419 return replace_by_disjunct(map
, i
, context
);
3422 isl_basic_map_free(context
);
3423 ISL_F_CLR(map
, ISL_MAP_NORMALIZED
);
3425 ISL_F_CLR(map
, ISL_MAP_DISJOINT
);
3429 isl_basic_map_free(context
);
3433 /* Remove the constraints in "context" from "set".
3434 * If any of the disjuncts in the result turns out to be the universe,
3435 * then return this universe.
3436 * "context" is assumed to have explicit representations
3437 * for all local variables.
3439 __isl_give isl_set
*isl_set_plain_gist_basic_set(__isl_take isl_set
*set
,
3440 __isl_take isl_basic_set
*context
)
3442 return set_from_map(isl_map_plain_gist_basic_map(set_to_map(set
),
3443 bset_to_bmap(context
)));
3446 /* Remove the constraints in "context" from "map".
3447 * If any of the disjuncts in the result turns out to be the universe,
3448 * then return this universe.
3449 * "context" is assumed to consist of a single disjunct and
3450 * to have explicit representations for all local variables.
3452 __isl_give isl_map
*isl_map_plain_gist(__isl_take isl_map
*map
,
3453 __isl_take isl_map
*context
)
3455 isl_basic_map
*hull
;
3457 hull
= isl_map_unshifted_simple_hull(context
);
3458 return isl_map_plain_gist_basic_map(map
, hull
);
3461 /* Replace "map" by a universe map in the same space and free "drop".
3463 static __isl_give isl_map
*replace_by_universe(__isl_take isl_map
*map
,
3464 __isl_take isl_map
*drop
)
3468 res
= isl_map_universe(isl_map_get_space(map
));
3474 /* Return a map that has the same intersection with "context" as "map"
3475 * and that is as "simple" as possible.
3477 * If "map" is already the universe, then we cannot make it any simpler.
3478 * Similarly, if "context" is the universe, then we cannot exploit it
3480 * If "map" and "context" are identical to each other, then we can
3481 * return the corresponding universe.
3483 * If either "map" or "context" consists of multiple disjuncts,
3484 * then check if "context" happens to be a subset of "map",
3485 * in which case all constraints can be removed.
3486 * In case of multiple disjuncts, the standard procedure
3487 * may not be able to detect that all constraints can be removed.
3489 * If none of these cases apply, we have to work a bit harder.
3490 * During this computation, we make use of a single disjunct context,
3491 * so if the original context consists of more than one disjunct
3492 * then we need to approximate the context by a single disjunct set.
3493 * Simply taking the simple hull may drop constraints that are
3494 * only implicitly available in each disjunct. We therefore also
3495 * look for constraints among those defining "map" that are valid
3496 * for the context. These can then be used to simplify away
3497 * the corresponding constraints in "map".
3499 static __isl_give isl_map
*map_gist(__isl_take isl_map
*map
,
3500 __isl_take isl_map
*context
)
3504 int single_disjunct_map
, single_disjunct_context
;
3506 isl_basic_map
*hull
;
3508 is_universe
= isl_map_plain_is_universe(map
);
3509 if (is_universe
>= 0 && !is_universe
)
3510 is_universe
= isl_map_plain_is_universe(context
);
3511 if (is_universe
< 0)
3514 isl_map_free(context
);
3518 equal
= isl_map_plain_is_equal(map
, context
);
3522 return replace_by_universe(map
, context
);
3524 single_disjunct_map
= isl_map_n_basic_map(map
) == 1;
3525 single_disjunct_context
= isl_map_n_basic_map(context
) == 1;
3526 if (!single_disjunct_map
|| !single_disjunct_context
) {
3527 subset
= isl_map_is_subset(context
, map
);
3531 return replace_by_universe(map
, context
);
3534 context
= isl_map_compute_divs(context
);
3537 if (single_disjunct_context
) {
3538 hull
= isl_map_simple_hull(context
);
3543 ctx
= isl_map_get_ctx(map
);
3544 list
= isl_map_list_alloc(ctx
, 2);
3545 list
= isl_map_list_add(list
, isl_map_copy(context
));
3546 list
= isl_map_list_add(list
, isl_map_copy(map
));
3547 hull
= isl_map_unshifted_simple_hull_from_map_list(context
,
3550 return isl_map_gist_basic_map(map
, hull
);
3553 isl_map_free(context
);
3557 __isl_give isl_map
*isl_map_gist(__isl_take isl_map
*map
,
3558 __isl_take isl_map
*context
)
3560 return isl_map_align_params_map_map_and(map
, context
, &map_gist
);
3563 struct isl_basic_set
*isl_basic_set_gist(struct isl_basic_set
*bset
,
3564 struct isl_basic_set
*context
)
3566 return bset_from_bmap(isl_basic_map_gist(bset_to_bmap(bset
),
3567 bset_to_bmap(context
)));
3570 __isl_give isl_set
*isl_set_gist_basic_set(__isl_take isl_set
*set
,
3571 __isl_take isl_basic_set
*context
)
3573 return set_from_map(isl_map_gist_basic_map(set_to_map(set
),
3574 bset_to_bmap(context
)));
3577 __isl_give isl_set
*isl_set_gist_params_basic_set(__isl_take isl_set
*set
,
3578 __isl_take isl_basic_set
*context
)
3580 isl_space
*space
= isl_set_get_space(set
);
3581 isl_basic_set
*dom_context
= isl_basic_set_universe(space
);
3582 dom_context
= isl_basic_set_intersect_params(dom_context
, context
);
3583 return isl_set_gist_basic_set(set
, dom_context
);
3586 __isl_give isl_set
*isl_set_gist(__isl_take isl_set
*set
,
3587 __isl_take isl_set
*context
)
3589 return set_from_map(isl_map_gist(set_to_map(set
), set_to_map(context
)));
3592 /* Compute the gist of "bmap" with respect to the constraints "context"
3595 __isl_give isl_basic_map
*isl_basic_map_gist_domain(
3596 __isl_take isl_basic_map
*bmap
, __isl_take isl_basic_set
*context
)
3598 isl_space
*space
= isl_basic_map_get_space(bmap
);
3599 isl_basic_map
*bmap_context
= isl_basic_map_universe(space
);
3601 bmap_context
= isl_basic_map_intersect_domain(bmap_context
, context
);
3602 return isl_basic_map_gist(bmap
, bmap_context
);
3605 __isl_give isl_map
*isl_map_gist_domain(__isl_take isl_map
*map
,
3606 __isl_take isl_set
*context
)
3608 isl_map
*map_context
= isl_map_universe(isl_map_get_space(map
));
3609 map_context
= isl_map_intersect_domain(map_context
, context
);
3610 return isl_map_gist(map
, map_context
);
3613 __isl_give isl_map
*isl_map_gist_range(__isl_take isl_map
*map
,
3614 __isl_take isl_set
*context
)
3616 isl_map
*map_context
= isl_map_universe(isl_map_get_space(map
));
3617 map_context
= isl_map_intersect_range(map_context
, context
);
3618 return isl_map_gist(map
, map_context
);
3621 __isl_give isl_map
*isl_map_gist_params(__isl_take isl_map
*map
,
3622 __isl_take isl_set
*context
)
3624 isl_map
*map_context
= isl_map_universe(isl_map_get_space(map
));
3625 map_context
= isl_map_intersect_params(map_context
, context
);
3626 return isl_map_gist(map
, map_context
);
3629 __isl_give isl_set
*isl_set_gist_params(__isl_take isl_set
*set
,
3630 __isl_take isl_set
*context
)
3632 return isl_map_gist_params(set
, context
);
3635 /* Quick check to see if two basic maps are disjoint.
3636 * In particular, we reduce the equalities and inequalities of
3637 * one basic map in the context of the equalities of the other
3638 * basic map and check if we get a contradiction.
3640 isl_bool
isl_basic_map_plain_is_disjoint(__isl_keep isl_basic_map
*bmap1
,
3641 __isl_keep isl_basic_map
*bmap2
)
3643 struct isl_vec
*v
= NULL
;
3648 if (!bmap1
|| !bmap2
)
3649 return isl_bool_error
;
3650 isl_assert(bmap1
->ctx
, isl_space_is_equal(bmap1
->dim
, bmap2
->dim
),
3651 return isl_bool_error
);
3652 if (bmap1
->n_div
|| bmap2
->n_div
)
3653 return isl_bool_false
;
3654 if (!bmap1
->n_eq
&& !bmap2
->n_eq
)
3655 return isl_bool_false
;
3657 total
= isl_space_dim(bmap1
->dim
, isl_dim_all
);
3659 return isl_bool_error
;
3661 return isl_bool_false
;
3662 v
= isl_vec_alloc(bmap1
->ctx
, 1 + total
);
3665 elim
= isl_alloc_array(bmap1
->ctx
, int, total
);
3668 compute_elimination_index(bmap1
, elim
);
3669 for (i
= 0; i
< bmap2
->n_eq
; ++i
) {
3671 reduced
= reduced_using_equalities(v
->block
.data
, bmap2
->eq
[i
],
3673 if (reduced
&& !isl_int_is_zero(v
->block
.data
[0]) &&
3674 isl_seq_first_non_zero(v
->block
.data
+ 1, total
) == -1)
3677 for (i
= 0; i
< bmap2
->n_ineq
; ++i
) {
3679 reduced
= reduced_using_equalities(v
->block
.data
,
3680 bmap2
->ineq
[i
], bmap1
, elim
);
3681 if (reduced
&& isl_int_is_neg(v
->block
.data
[0]) &&
3682 isl_seq_first_non_zero(v
->block
.data
+ 1, total
) == -1)
3685 compute_elimination_index(bmap2
, elim
);
3686 for (i
= 0; i
< bmap1
->n_ineq
; ++i
) {
3688 reduced
= reduced_using_equalities(v
->block
.data
,
3689 bmap1
->ineq
[i
], bmap2
, elim
);
3690 if (reduced
&& isl_int_is_neg(v
->block
.data
[0]) &&
3691 isl_seq_first_non_zero(v
->block
.data
+ 1, total
) == -1)
3696 return isl_bool_false
;
3700 return isl_bool_true
;
3704 return isl_bool_error
;
3707 int isl_basic_set_plain_is_disjoint(__isl_keep isl_basic_set
*bset1
,
3708 __isl_keep isl_basic_set
*bset2
)
3710 return isl_basic_map_plain_is_disjoint(bset_to_bmap(bset1
),
3711 bset_to_bmap(bset2
));
3714 /* Does "test" hold for all pairs of basic maps in "map1" and "map2"?
3716 static isl_bool
all_pairs(__isl_keep isl_map
*map1
, __isl_keep isl_map
*map2
,
3717 isl_bool (*test
)(__isl_keep isl_basic_map
*bmap1
,
3718 __isl_keep isl_basic_map
*bmap2
))
3723 return isl_bool_error
;
3725 for (i
= 0; i
< map1
->n
; ++i
) {
3726 for (j
= 0; j
< map2
->n
; ++j
) {
3727 isl_bool d
= test(map1
->p
[i
], map2
->p
[j
]);
3728 if (d
!= isl_bool_true
)
3733 return isl_bool_true
;
3736 /* Are "map1" and "map2" obviously disjoint, based on information
3737 * that can be derived without looking at the individual basic maps?
3739 * In particular, if one of them is empty or if they live in different spaces
3740 * (ignoring parameters), then they are clearly disjoint.
3742 static isl_bool
isl_map_plain_is_disjoint_global(__isl_keep isl_map
*map1
,
3743 __isl_keep isl_map
*map2
)
3749 return isl_bool_error
;
3751 disjoint
= isl_map_plain_is_empty(map1
);
3752 if (disjoint
< 0 || disjoint
)
3755 disjoint
= isl_map_plain_is_empty(map2
);
3756 if (disjoint
< 0 || disjoint
)
3759 match
= isl_space_tuple_is_equal(map1
->dim
, isl_dim_in
,
3760 map2
->dim
, isl_dim_in
);
3761 if (match
< 0 || !match
)
3762 return match
< 0 ? isl_bool_error
: isl_bool_true
;
3764 match
= isl_space_tuple_is_equal(map1
->dim
, isl_dim_out
,
3765 map2
->dim
, isl_dim_out
);
3766 if (match
< 0 || !match
)
3767 return match
< 0 ? isl_bool_error
: isl_bool_true
;
3769 return isl_bool_false
;
3772 /* Are "map1" and "map2" obviously disjoint?
3774 * If one of them is empty or if they live in different spaces (ignoring
3775 * parameters), then they are clearly disjoint.
3776 * This is checked by isl_map_plain_is_disjoint_global.
3778 * If they have different parameters, then we skip any further tests.
3780 * If they are obviously equal, but not obviously empty, then we will
3781 * not be able to detect if they are disjoint.
3783 * Otherwise we check if each basic map in "map1" is obviously disjoint
3784 * from each basic map in "map2".
3786 isl_bool
isl_map_plain_is_disjoint(__isl_keep isl_map
*map1
,
3787 __isl_keep isl_map
*map2
)
3793 disjoint
= isl_map_plain_is_disjoint_global(map1
, map2
);
3794 if (disjoint
< 0 || disjoint
)
3797 match
= isl_map_has_equal_params(map1
, map2
);
3798 if (match
< 0 || !match
)
3799 return match
< 0 ? isl_bool_error
: isl_bool_false
;
3801 intersect
= isl_map_plain_is_equal(map1
, map2
);
3802 if (intersect
< 0 || intersect
)
3803 return intersect
< 0 ? isl_bool_error
: isl_bool_false
;
3805 return all_pairs(map1
, map2
, &isl_basic_map_plain_is_disjoint
);
3808 /* Are "map1" and "map2" disjoint?
3809 * The parameters are assumed to have been aligned.
3811 * In particular, check whether all pairs of basic maps are disjoint.
3813 static isl_bool
isl_map_is_disjoint_aligned(__isl_keep isl_map
*map1
,
3814 __isl_keep isl_map
*map2
)
3816 return all_pairs(map1
, map2
, &isl_basic_map_is_disjoint
);
3819 /* Are "map1" and "map2" disjoint?
3821 * They are disjoint if they are "obviously disjoint" or if one of them
3822 * is empty. Otherwise, they are not disjoint if one of them is universal.
3823 * If the two inputs are (obviously) equal and not empty, then they are
3825 * If none of these cases apply, then check if all pairs of basic maps
3826 * are disjoint after aligning the parameters.
3828 isl_bool
isl_map_is_disjoint(__isl_keep isl_map
*map1
, __isl_keep isl_map
*map2
)
3833 disjoint
= isl_map_plain_is_disjoint_global(map1
, map2
);
3834 if (disjoint
< 0 || disjoint
)
3837 disjoint
= isl_map_is_empty(map1
);
3838 if (disjoint
< 0 || disjoint
)
3841 disjoint
= isl_map_is_empty(map2
);
3842 if (disjoint
< 0 || disjoint
)
3845 intersect
= isl_map_plain_is_universe(map1
);
3846 if (intersect
< 0 || intersect
)
3847 return intersect
< 0 ? isl_bool_error
: isl_bool_false
;
3849 intersect
= isl_map_plain_is_universe(map2
);
3850 if (intersect
< 0 || intersect
)
3851 return intersect
< 0 ? isl_bool_error
: isl_bool_false
;
3853 intersect
= isl_map_plain_is_equal(map1
, map2
);
3854 if (intersect
< 0 || intersect
)
3855 return isl_bool_not(intersect
);
3857 return isl_map_align_params_map_map_and_test(map1
, map2
,
3858 &isl_map_is_disjoint_aligned
);
3861 /* Are "bmap1" and "bmap2" disjoint?
3863 * They are disjoint if they are "obviously disjoint" or if one of them
3864 * is empty. Otherwise, they are not disjoint if one of them is universal.
3865 * If none of these cases apply, we compute the intersection and see if
3866 * the result is empty.
3868 isl_bool
isl_basic_map_is_disjoint(__isl_keep isl_basic_map
*bmap1
,
3869 __isl_keep isl_basic_map
*bmap2
)
3873 isl_basic_map
*test
;
3875 disjoint
= isl_basic_map_plain_is_disjoint(bmap1
, bmap2
);
3876 if (disjoint
< 0 || disjoint
)
3879 disjoint
= isl_basic_map_is_empty(bmap1
);
3880 if (disjoint
< 0 || disjoint
)
3883 disjoint
= isl_basic_map_is_empty(bmap2
);
3884 if (disjoint
< 0 || disjoint
)
3887 intersect
= isl_basic_map_plain_is_universe(bmap1
);
3888 if (intersect
< 0 || intersect
)
3889 return intersect
< 0 ? isl_bool_error
: isl_bool_false
;
3891 intersect
= isl_basic_map_plain_is_universe(bmap2
);
3892 if (intersect
< 0 || intersect
)
3893 return intersect
< 0 ? isl_bool_error
: isl_bool_false
;
3895 test
= isl_basic_map_intersect(isl_basic_map_copy(bmap1
),
3896 isl_basic_map_copy(bmap2
));
3897 disjoint
= isl_basic_map_is_empty(test
);
3898 isl_basic_map_free(test
);
3903 /* Are "bset1" and "bset2" disjoint?
3905 isl_bool
isl_basic_set_is_disjoint(__isl_keep isl_basic_set
*bset1
,
3906 __isl_keep isl_basic_set
*bset2
)
3908 return isl_basic_map_is_disjoint(bset1
, bset2
);
3911 isl_bool
isl_set_plain_is_disjoint(__isl_keep isl_set
*set1
,
3912 __isl_keep isl_set
*set2
)
3914 return isl_map_plain_is_disjoint(set_to_map(set1
), set_to_map(set2
));
3917 /* Are "set1" and "set2" disjoint?
3919 isl_bool
isl_set_is_disjoint(__isl_keep isl_set
*set1
, __isl_keep isl_set
*set2
)
3921 return isl_map_is_disjoint(set1
, set2
);
3924 /* Is "v" equal to 0, 1 or -1?
3926 static int is_zero_or_one(isl_int v
)
3928 return isl_int_is_zero(v
) || isl_int_is_one(v
) || isl_int_is_negone(v
);
3931 /* Are the "n" coefficients starting at "first" of inequality constraints
3932 * "i" and "j" of "bmap" opposite to each other?
3934 static int is_opposite_part(__isl_keep isl_basic_map
*bmap
, int i
, int j
,
3937 return isl_seq_is_neg(bmap
->ineq
[i
] + first
, bmap
->ineq
[j
] + first
, n
);
3940 /* Are inequality constraints "i" and "j" of "bmap" opposite to each other,
3941 * apart from the constant term?
3943 static isl_bool
is_opposite(__isl_keep isl_basic_map
*bmap
, int i
, int j
)
3947 total
= isl_basic_map_dim(bmap
, isl_dim_all
);
3949 return isl_bool_error
;
3950 return is_opposite_part(bmap
, i
, j
, 1, total
);
3953 /* Check if we can combine a given div with lower bound l and upper
3954 * bound u with some other div and if so return that other div.
3955 * Otherwise, return a position beyond the integer divisions.
3956 * Return -1 on error.
3958 * We first check that
3959 * - the bounds are opposites of each other (except for the constant
3961 * - the bounds do not reference any other div
3962 * - no div is defined in terms of this div
3964 * Let m be the size of the range allowed on the div by the bounds.
3965 * That is, the bounds are of the form
3967 * e <= a <= e + m - 1
3969 * with e some expression in the other variables.
3970 * We look for another div b such that no third div is defined in terms
3971 * of this second div b and such that in any constraint that contains
3972 * a (except for the given lower and upper bound), also contains b
3973 * with a coefficient that is m times that of b.
3974 * That is, all constraints (except for the lower and upper bound)
3977 * e + f (a + m b) >= 0
3979 * Furthermore, in the constraints that only contain b, the coefficient
3980 * of b should be equal to 1 or -1.
3981 * If so, we return b so that "a + m b" can be replaced by
3982 * a single div "c = a + m b".
3984 static int div_find_coalesce(__isl_keep isl_basic_map
*bmap
, int *pairs
,
3985 unsigned div
, unsigned l
, unsigned u
)
3993 n_div
= isl_basic_map_dim(bmap
, isl_dim_div
);
3996 v_div
= isl_basic_map_var_offset(bmap
, isl_dim_div
);
3999 if (isl_seq_first_non_zero(bmap
->ineq
[l
] + 1 + v_div
, div
) != -1)
4001 if (isl_seq_first_non_zero(bmap
->ineq
[l
] + 1 + v_div
+ div
+ 1,
4002 n_div
- div
- 1) != -1)
4004 opp
= is_opposite(bmap
, l
, u
);
4005 if (opp
< 0 || !opp
)
4006 return opp
< 0 ? -1 : n_div
;
4008 for (i
= 0; i
< n_div
; ++i
) {
4009 if (isl_int_is_zero(bmap
->div
[i
][0]))
4011 if (!isl_int_is_zero(bmap
->div
[i
][1 + 1 + v_div
+ div
]))
4015 isl_int_add(bmap
->ineq
[l
][0], bmap
->ineq
[l
][0], bmap
->ineq
[u
][0]);
4016 if (isl_int_is_neg(bmap
->ineq
[l
][0])) {
4017 isl_int_sub(bmap
->ineq
[l
][0],
4018 bmap
->ineq
[l
][0], bmap
->ineq
[u
][0]);
4019 bmap
= isl_basic_map_copy(bmap
);
4020 bmap
= isl_basic_map_set_to_empty(bmap
);
4021 isl_basic_map_free(bmap
);
4024 isl_int_add_ui(bmap
->ineq
[l
][0], bmap
->ineq
[l
][0], 1);
4026 for (i
= 0; i
< n_div
; ++i
) {
4031 for (j
= 0; j
< n_div
; ++j
) {
4032 if (isl_int_is_zero(bmap
->div
[j
][0]))
4034 if (!isl_int_is_zero(bmap
->div
[j
][1 + 1 + v_div
+ i
]))
4039 for (j
= 0; j
< bmap
->n_ineq
; ++j
) {
4041 if (j
== l
|| j
== u
)
4043 if (isl_int_is_zero(bmap
->ineq
[j
][1 + v_div
+ div
])) {
4044 if (is_zero_or_one(bmap
->ineq
[j
][1 + v_div
+ i
]))
4048 if (isl_int_is_zero(bmap
->ineq
[j
][1 + v_div
+ i
]))
4050 isl_int_mul(bmap
->ineq
[j
][1 + v_div
+ div
],
4051 bmap
->ineq
[j
][1 + v_div
+ div
],
4053 valid
= isl_int_eq(bmap
->ineq
[j
][1 + v_div
+ div
],
4054 bmap
->ineq
[j
][1 + v_div
+ i
]);
4055 isl_int_divexact(bmap
->ineq
[j
][1 + v_div
+ div
],
4056 bmap
->ineq
[j
][1 + v_div
+ div
],
4061 if (j
< bmap
->n_ineq
)
4066 isl_int_sub_ui(bmap
->ineq
[l
][0], bmap
->ineq
[l
][0], 1);
4067 isl_int_sub(bmap
->ineq
[l
][0], bmap
->ineq
[l
][0], bmap
->ineq
[u
][0]);
4071 /* Internal data structure used during the construction and/or evaluation of
4072 * an inequality that ensures that a pair of bounds always allows
4073 * for an integer value.
4075 * "tab" is the tableau in which the inequality is evaluated. It may
4076 * be NULL until it is actually needed.
4077 * "v" contains the inequality coefficients.
4078 * "g", "fl" and "fu" are temporary scalars used during the construction and
4081 struct test_ineq_data
{
4082 struct isl_tab
*tab
;
4089 /* Free all the memory allocated by the fields of "data".
4091 static void test_ineq_data_clear(struct test_ineq_data
*data
)
4093 isl_tab_free(data
->tab
);
4094 isl_vec_free(data
->v
);
4095 isl_int_clear(data
->g
);
4096 isl_int_clear(data
->fl
);
4097 isl_int_clear(data
->fu
);
4100 /* Is the inequality stored in data->v satisfied by "bmap"?
4101 * That is, does it only attain non-negative values?
4102 * data->tab is a tableau corresponding to "bmap".
4104 static isl_bool
test_ineq_is_satisfied(__isl_keep isl_basic_map
*bmap
,
4105 struct test_ineq_data
*data
)
4108 enum isl_lp_result res
;
4110 ctx
= isl_basic_map_get_ctx(bmap
);
4112 data
->tab
= isl_tab_from_basic_map(bmap
, 0);
4113 res
= isl_tab_min(data
->tab
, data
->v
->el
, ctx
->one
, &data
->g
, NULL
, 0);
4114 if (res
== isl_lp_error
)
4115 return isl_bool_error
;
4116 return res
== isl_lp_ok
&& isl_int_is_nonneg(data
->g
);
4119 /* Given a lower and an upper bound on div i, do they always allow
4120 * for an integer value of the given div?
4121 * Determine this property by constructing an inequality
4122 * such that the property is guaranteed when the inequality is nonnegative.
4123 * The lower bound is inequality l, while the upper bound is inequality u.
4124 * The constructed inequality is stored in data->v.
4126 * Let the upper bound be
4130 * and the lower bound
4134 * Let n_u = f_u g and n_l = f_l g, with g = gcd(n_u, n_l).
4137 * - f_u e_l <= f_u f_l g a <= f_l e_u
4139 * Since all variables are integer valued, this is equivalent to
4141 * - f_u e_l - (f_u - 1) <= f_u f_l g a <= f_l e_u + (f_l - 1)
4143 * If this interval is at least f_u f_l g, then it contains at least
4144 * one integer value for a.
4145 * That is, the test constraint is
4147 * f_l e_u + f_u e_l + f_l - 1 + f_u - 1 + 1 >= f_u f_l g
4151 * f_l e_u + f_u e_l + f_l - 1 + f_u - 1 + 1 - f_u f_l g >= 0
4153 * If the coefficients of f_l e_u + f_u e_l have a common divisor g',
4154 * then the constraint can be scaled down by a factor g',
4155 * with the constant term replaced by
4156 * floor((f_l e_{u,0} + f_u e_{l,0} + f_l - 1 + f_u - 1 + 1 - f_u f_l g)/g').
4157 * Note that the result of applying Fourier-Motzkin to this pair
4160 * f_l e_u + f_u e_l >= 0
4162 * If the constant term of the scaled down version of this constraint,
4163 * i.e., floor((f_l e_{u,0} + f_u e_{l,0})/g') is equal to the constant
4164 * term of the scaled down test constraint, then the test constraint
4165 * is known to hold and no explicit evaluation is required.
4166 * This is essentially the Omega test.
4168 * If the test constraint consists of only a constant term, then
4169 * it is sufficient to look at the sign of this constant term.
4171 static isl_bool
int_between_bounds(__isl_keep isl_basic_map
*bmap
, int i
,
4172 int l
, int u
, struct test_ineq_data
*data
)
4177 offset
= isl_basic_map_offset(bmap
, isl_dim_div
);
4178 n_div
= isl_basic_map_dim(bmap
, isl_dim_div
);
4180 return isl_bool_error
;
4182 isl_int_gcd(data
->g
,
4183 bmap
->ineq
[l
][offset
+ i
], bmap
->ineq
[u
][offset
+ i
]);
4184 isl_int_divexact(data
->fl
, bmap
->ineq
[l
][offset
+ i
], data
->g
);
4185 isl_int_divexact(data
->fu
, bmap
->ineq
[u
][offset
+ i
], data
->g
);
4186 isl_int_neg(data
->fu
, data
->fu
);
4187 isl_seq_combine(data
->v
->el
, data
->fl
, bmap
->ineq
[u
],
4188 data
->fu
, bmap
->ineq
[l
], offset
+ n_div
);
4189 isl_int_mul(data
->g
, data
->g
, data
->fl
);
4190 isl_int_mul(data
->g
, data
->g
, data
->fu
);
4191 isl_int_sub(data
->g
, data
->g
, data
->fl
);
4192 isl_int_sub(data
->g
, data
->g
, data
->fu
);
4193 isl_int_add_ui(data
->g
, data
->g
, 1);
4194 isl_int_sub(data
->fl
, data
->v
->el
[0], data
->g
);
4196 isl_seq_gcd(data
->v
->el
+ 1, offset
- 1 + n_div
, &data
->g
);
4197 if (isl_int_is_zero(data
->g
))
4198 return isl_int_is_nonneg(data
->fl
);
4199 if (isl_int_is_one(data
->g
)) {
4200 isl_int_set(data
->v
->el
[0], data
->fl
);
4201 return test_ineq_is_satisfied(bmap
, data
);
4203 isl_int_fdiv_q(data
->fl
, data
->fl
, data
->g
);
4204 isl_int_fdiv_q(data
->v
->el
[0], data
->v
->el
[0], data
->g
);
4205 if (isl_int_eq(data
->fl
, data
->v
->el
[0]))
4206 return isl_bool_true
;
4207 isl_int_set(data
->v
->el
[0], data
->fl
);
4208 isl_seq_scale_down(data
->v
->el
+ 1, data
->v
->el
+ 1, data
->g
,
4209 offset
- 1 + n_div
);
4211 return test_ineq_is_satisfied(bmap
, data
);
4214 /* Remove more kinds of divs that are not strictly needed.
4215 * In particular, if all pairs of lower and upper bounds on a div
4216 * are such that they allow at least one integer value of the div,
4217 * then we can eliminate the div using Fourier-Motzkin without
4218 * introducing any spurious solutions.
4220 * If at least one of the two constraints has a unit coefficient for the div,
4221 * then the presence of such a value is guaranteed so there is no need to check.
4222 * In particular, the value attained by the bound with unit coefficient
4223 * can serve as this intermediate value.
4225 static __isl_give isl_basic_map
*drop_more_redundant_divs(
4226 __isl_take isl_basic_map
*bmap
, __isl_take
int *pairs
, int n
)
4229 struct test_ineq_data data
= { NULL
, NULL
};
4234 isl_int_init(data
.g
);
4235 isl_int_init(data
.fl
);
4236 isl_int_init(data
.fu
);
4238 n_div
= isl_basic_map_dim(bmap
, isl_dim_div
);
4242 ctx
= isl_basic_map_get_ctx(bmap
);
4243 off
= isl_basic_map_offset(bmap
, isl_dim_div
);
4244 data
.v
= isl_vec_alloc(ctx
, off
+ n_div
);
4253 for (i
= 0; i
< n_div
; ++i
) {
4256 if (best
>= 0 && pairs
[best
] <= pairs
[i
])
4262 for (l
= 0; l
< bmap
->n_ineq
; ++l
) {
4263 if (!isl_int_is_pos(bmap
->ineq
[l
][off
+ i
]))
4265 if (isl_int_is_one(bmap
->ineq
[l
][off
+ i
]))
4267 for (u
= 0; u
< bmap
->n_ineq
; ++u
) {
4268 if (!isl_int_is_neg(bmap
->ineq
[u
][off
+ i
]))
4270 if (isl_int_is_negone(bmap
->ineq
[u
][off
+ i
]))
4272 has_int
= int_between_bounds(bmap
, i
, l
, u
,
4276 if (data
.tab
&& data
.tab
->empty
)
4281 if (u
< bmap
->n_ineq
)
4284 if (data
.tab
&& data
.tab
->empty
) {
4285 bmap
= isl_basic_map_set_to_empty(bmap
);
4288 if (l
== bmap
->n_ineq
) {
4296 test_ineq_data_clear(&data
);
4303 bmap
= isl_basic_map_remove_dims(bmap
, isl_dim_div
, remove
, 1);
4304 return isl_basic_map_drop_redundant_divs(bmap
);
4307 isl_basic_map_free(bmap
);
4308 test_ineq_data_clear(&data
);
4312 /* Given a pair of divs div1 and div2 such that, except for the lower bound l
4313 * and the upper bound u, div1 always occurs together with div2 in the form
4314 * (div1 + m div2), where m is the constant range on the variable div1
4315 * allowed by l and u, replace the pair div1 and div2 by a single
4316 * div that is equal to div1 + m div2.
4318 * The new div will appear in the location that contains div2.
4319 * We need to modify all constraints that contain
4320 * div2 = (div - div1) / m
4321 * The coefficient of div2 is known to be equal to 1 or -1.
4322 * (If a constraint does not contain div2, it will also not contain div1.)
4323 * If the constraint also contains div1, then we know they appear
4324 * as f (div1 + m div2) and we can simply replace (div1 + m div2) by div,
4325 * i.e., the coefficient of div is f.
4327 * Otherwise, we first need to introduce div1 into the constraint.
4336 * A lower bound on div2
4340 * can be replaced by
4342 * m div2 + div1 + m t + f >= 0
4348 * can be replaced by
4350 * -(m div2 + div1) + m t + f' >= 0
4352 * These constraint are those that we would obtain from eliminating
4353 * div1 using Fourier-Motzkin.
4355 * After all constraints have been modified, we drop the lower and upper
4356 * bound and then drop div1.
4357 * Since the new div is only placed in the same location that used
4358 * to store div2, but otherwise has a different meaning, any possible
4359 * explicit representation of the original div2 is removed.
4361 static __isl_give isl_basic_map
*coalesce_divs(__isl_take isl_basic_map
*bmap
,
4362 unsigned div1
, unsigned div2
, unsigned l
, unsigned u
)
4370 ctx
= isl_basic_map_get_ctx(bmap
);
4372 v_div
= isl_basic_map_var_offset(bmap
, isl_dim_div
);
4374 return isl_basic_map_free(bmap
);
4375 total
= 1 + v_div
+ bmap
->n_div
;
4378 isl_int_add(m
, bmap
->ineq
[l
][0], bmap
->ineq
[u
][0]);
4379 isl_int_add_ui(m
, m
, 1);
4381 for (i
= 0; i
< bmap
->n_ineq
; ++i
) {
4382 if (i
== l
|| i
== u
)
4384 if (isl_int_is_zero(bmap
->ineq
[i
][1 + v_div
+ div2
]))
4386 if (isl_int_is_zero(bmap
->ineq
[i
][1 + v_div
+ div1
])) {
4387 if (isl_int_is_pos(bmap
->ineq
[i
][1 + v_div
+ div2
]))
4388 isl_seq_combine(bmap
->ineq
[i
], m
, bmap
->ineq
[i
],
4389 ctx
->one
, bmap
->ineq
[l
], total
);
4391 isl_seq_combine(bmap
->ineq
[i
], m
, bmap
->ineq
[i
],
4392 ctx
->one
, bmap
->ineq
[u
], total
);
4394 isl_int_set(bmap
->ineq
[i
][1 + v_div
+ div2
],
4395 bmap
->ineq
[i
][1 + v_div
+ div1
]);
4396 isl_int_set_si(bmap
->ineq
[i
][1 + v_div
+ div1
], 0);
4401 isl_basic_map_drop_inequality(bmap
, l
);
4402 isl_basic_map_drop_inequality(bmap
, u
);
4404 isl_basic_map_drop_inequality(bmap
, u
);
4405 isl_basic_map_drop_inequality(bmap
, l
);
4407 bmap
= isl_basic_map_mark_div_unknown(bmap
, div2
);
4408 bmap
= isl_basic_map_drop_div(bmap
, div1
);
4412 /* First check if we can coalesce any pair of divs and
4413 * then continue with dropping more redundant divs.
4415 * We loop over all pairs of lower and upper bounds on a div
4416 * with coefficient 1 and -1, respectively, check if there
4417 * is any other div "c" with which we can coalesce the div
4418 * and if so, perform the coalescing.
4420 static __isl_give isl_basic_map
*coalesce_or_drop_more_redundant_divs(
4421 __isl_take isl_basic_map
*bmap
, int *pairs
, int n
)
4427 v_div
= isl_basic_map_var_offset(bmap
, isl_dim_div
);
4428 n_div
= isl_basic_map_dim(bmap
, isl_dim_div
);
4429 if (v_div
< 0 || n_div
< 0)
4430 return isl_basic_map_free(bmap
);
4432 for (i
= 0; i
< n_div
; ++i
) {
4435 for (l
= 0; l
< bmap
->n_ineq
; ++l
) {
4436 if (!isl_int_is_one(bmap
->ineq
[l
][1 + v_div
+ i
]))
4438 for (u
= 0; u
< bmap
->n_ineq
; ++u
) {
4441 if (!isl_int_is_negone(bmap
->ineq
[u
][1+v_div
+i
]))
4443 c
= div_find_coalesce(bmap
, pairs
, i
, l
, u
);
4449 bmap
= coalesce_divs(bmap
, i
, c
, l
, u
);
4450 return isl_basic_map_drop_redundant_divs(bmap
);
4455 if (ISL_F_ISSET(bmap
, ISL_BASIC_MAP_EMPTY
)) {
4460 return drop_more_redundant_divs(bmap
, pairs
, n
);
4463 isl_basic_map_free(bmap
);
4467 /* Are the "n" coefficients starting at "first" of inequality constraints
4468 * "i" and "j" of "bmap" equal to each other?
4470 static int is_parallel_part(__isl_keep isl_basic_map
*bmap
, int i
, int j
,
4473 return isl_seq_eq(bmap
->ineq
[i
] + first
, bmap
->ineq
[j
] + first
, n
);
4476 /* Are inequality constraints "i" and "j" of "bmap" equal to each other,
4477 * apart from the constant term and the coefficient at position "pos"?
4479 static isl_bool
is_parallel_except(__isl_keep isl_basic_map
*bmap
, int i
, int j
,
4484 total
= isl_basic_map_dim(bmap
, isl_dim_all
);
4486 return isl_bool_error
;
4487 return is_parallel_part(bmap
, i
, j
, 1, pos
- 1) &&
4488 is_parallel_part(bmap
, i
, j
, pos
+ 1, total
- pos
);
4491 /* Are inequality constraints "i" and "j" of "bmap" opposite to each other,
4492 * apart from the constant term and the coefficient at position "pos"?
4494 static isl_bool
is_opposite_except(__isl_keep isl_basic_map
*bmap
, int i
, int j
,
4499 total
= isl_basic_map_dim(bmap
, isl_dim_all
);
4501 return isl_bool_error
;
4502 return is_opposite_part(bmap
, i
, j
, 1, pos
- 1) &&
4503 is_opposite_part(bmap
, i
, j
, pos
+ 1, total
- pos
);
4506 /* Restart isl_basic_map_drop_redundant_divs after "bmap" has
4507 * been modified, simplying it if "simplify" is set.
4508 * Free the temporary data structure "pairs" that was associated
4509 * to the old version of "bmap".
4511 static __isl_give isl_basic_map
*drop_redundant_divs_again(
4512 __isl_take isl_basic_map
*bmap
, __isl_take
int *pairs
, int simplify
)
4515 bmap
= isl_basic_map_simplify(bmap
);
4517 return isl_basic_map_drop_redundant_divs(bmap
);
4520 /* Is "div" the single unknown existentially quantified variable
4521 * in inequality constraint "ineq" of "bmap"?
4522 * "div" is known to have a non-zero coefficient in "ineq".
4524 static isl_bool
single_unknown(__isl_keep isl_basic_map
*bmap
, int ineq
,
4532 known
= isl_basic_map_div_is_known(bmap
, div
);
4533 if (known
< 0 || known
)
4534 return isl_bool_not(known
);
4535 n_div
= isl_basic_map_dim(bmap
, isl_dim_div
);
4537 return isl_bool_error
;
4539 return isl_bool_true
;
4540 o_div
= isl_basic_map_offset(bmap
, isl_dim_div
);
4541 for (i
= 0; i
< n_div
; ++i
) {
4546 if (isl_int_is_zero(bmap
->ineq
[ineq
][o_div
+ i
]))
4548 known
= isl_basic_map_div_is_known(bmap
, i
);
4549 if (known
< 0 || !known
)
4553 return isl_bool_true
;
4556 /* Does integer division "div" have coefficient 1 in inequality constraint
4559 static isl_bool
has_coef_one(__isl_keep isl_basic_map
*bmap
, int div
, int ineq
)
4563 o_div
= isl_basic_map_offset(bmap
, isl_dim_div
);
4564 if (isl_int_is_one(bmap
->ineq
[ineq
][o_div
+ div
]))
4565 return isl_bool_true
;
4567 return isl_bool_false
;
4570 /* Turn inequality constraint "ineq" of "bmap" into an equality and
4571 * then try and drop redundant divs again,
4572 * freeing the temporary data structure "pairs" that was associated
4573 * to the old version of "bmap".
4575 static __isl_give isl_basic_map
*set_eq_and_try_again(
4576 __isl_take isl_basic_map
*bmap
, int ineq
, __isl_take
int *pairs
)
4578 bmap
= isl_basic_map_cow(bmap
);
4579 isl_basic_map_inequality_to_equality(bmap
, ineq
);
4580 return drop_redundant_divs_again(bmap
, pairs
, 1);
4583 /* Drop the integer division at position "div", along with the two
4584 * inequality constraints "ineq1" and "ineq2" in which it appears
4585 * from "bmap" and then try and drop redundant divs again,
4586 * freeing the temporary data structure "pairs" that was associated
4587 * to the old version of "bmap".
4589 static __isl_give isl_basic_map
*drop_div_and_try_again(
4590 __isl_take isl_basic_map
*bmap
, int div
, int ineq1
, int ineq2
,
4591 __isl_take
int *pairs
)
4593 if (ineq1
> ineq2
) {
4594 isl_basic_map_drop_inequality(bmap
, ineq1
);
4595 isl_basic_map_drop_inequality(bmap
, ineq2
);
4597 isl_basic_map_drop_inequality(bmap
, ineq2
);
4598 isl_basic_map_drop_inequality(bmap
, ineq1
);
4600 bmap
= isl_basic_map_drop_div(bmap
, div
);
4601 return drop_redundant_divs_again(bmap
, pairs
, 0);
4604 /* Given two inequality constraints
4606 * f(x) + n d + c >= 0, (ineq)
4608 * with d the variable at position "pos", and
4610 * f(x) + c0 >= 0, (lower)
4612 * compute the maximal value of the lower bound ceil((-f(x) - c)/n)
4613 * determined by the first constraint.
4620 static void lower_bound_from_parallel(__isl_keep isl_basic_map
*bmap
,
4621 int ineq
, int lower
, int pos
, isl_int
*l
)
4623 isl_int_neg(*l
, bmap
->ineq
[ineq
][0]);
4624 isl_int_add(*l
, *l
, bmap
->ineq
[lower
][0]);
4625 isl_int_cdiv_q(*l
, *l
, bmap
->ineq
[ineq
][pos
]);
4628 /* Given two inequality constraints
4630 * f(x) + n d + c >= 0, (ineq)
4632 * with d the variable at position "pos", and
4634 * -f(x) - c0 >= 0, (upper)
4636 * compute the minimal value of the lower bound ceil((-f(x) - c)/n)
4637 * determined by the first constraint.
4644 static void lower_bound_from_opposite(__isl_keep isl_basic_map
*bmap
,
4645 int ineq
, int upper
, int pos
, isl_int
*u
)
4647 isl_int_neg(*u
, bmap
->ineq
[ineq
][0]);
4648 isl_int_sub(*u
, *u
, bmap
->ineq
[upper
][0]);
4649 isl_int_cdiv_q(*u
, *u
, bmap
->ineq
[ineq
][pos
]);
4652 /* Given a lower bound constraint "ineq" on "div" in "bmap",
4653 * does the corresponding lower bound have a fixed value in "bmap"?
4655 * In particular, "ineq" is of the form
4657 * f(x) + n d + c >= 0
4659 * with n > 0, c the constant term and
4660 * d the existentially quantified variable "div".
4661 * That is, the lower bound is
4663 * ceil((-f(x) - c)/n)
4665 * Look for a pair of constraints
4670 * i.e., -c1 <= -f(x) <= c0, that fix ceil((-f(x) - c)/n) to a constant value.
4671 * That is, check that
4673 * ceil((-c1 - c)/n) = ceil((c0 - c)/n)
4675 * If so, return the index of inequality f(x) + c0 >= 0.
4676 * Otherwise, return bmap->n_ineq.
4677 * Return -1 on error.
4679 static int lower_bound_is_cst(__isl_keep isl_basic_map
*bmap
, int div
, int ineq
)
4682 int lower
= -1, upper
= -1;
4687 o_div
= isl_basic_map_offset(bmap
, isl_dim_div
);
4688 for (i
= 0; i
< bmap
->n_ineq
&& (lower
< 0 || upper
< 0); ++i
) {
4693 if (!isl_int_is_zero(bmap
->ineq
[i
][o_div
+ div
]))
4695 par
= isl_bool_false
;
4697 par
= is_parallel_except(bmap
, ineq
, i
, o_div
+ div
);
4704 opp
= isl_bool_false
;
4706 opp
= is_opposite_except(bmap
, ineq
, i
, o_div
+ div
);
4713 if (lower
< 0 || upper
< 0)
4714 return bmap
->n_ineq
;
4719 lower_bound_from_parallel(bmap
, ineq
, lower
, o_div
+ div
, &l
);
4720 lower_bound_from_opposite(bmap
, ineq
, upper
, o_div
+ div
, &u
);
4722 equal
= isl_int_eq(l
, u
);
4727 return equal
? lower
: bmap
->n_ineq
;
4730 /* Given a lower bound constraint "ineq" on the existentially quantified
4731 * variable "div", such that the corresponding lower bound has
4732 * a fixed value in "bmap", assign this fixed value to the variable and
4733 * then try and drop redundant divs again,
4734 * freeing the temporary data structure "pairs" that was associated
4735 * to the old version of "bmap".
4736 * "lower" determines the constant value for the lower bound.
4738 * In particular, "ineq" is of the form
4740 * f(x) + n d + c >= 0,
4742 * while "lower" is of the form
4746 * The lower bound is ceil((-f(x) - c)/n) and its constant value
4747 * is ceil((c0 - c)/n).
4749 static __isl_give isl_basic_map
*fix_cst_lower(__isl_take isl_basic_map
*bmap
,
4750 int div
, int ineq
, int lower
, int *pairs
)
4757 o_div
= isl_basic_map_offset(bmap
, isl_dim_div
);
4758 lower_bound_from_parallel(bmap
, ineq
, lower
, o_div
+ div
, &c
);
4759 bmap
= isl_basic_map_fix(bmap
, isl_dim_div
, div
, c
);
4764 return isl_basic_map_drop_redundant_divs(bmap
);
4767 /* Remove divs that are not strictly needed based on the inequality
4769 * In particular, if a div only occurs positively (or negatively)
4770 * in constraints, then it can simply be dropped.
4771 * Also, if a div occurs in only two constraints and if moreover
4772 * those two constraints are opposite to each other, except for the constant
4773 * term and if the sum of the constant terms is such that for any value
4774 * of the other values, there is always at least one integer value of the
4775 * div, i.e., if one plus this sum is greater than or equal to
4776 * the (absolute value) of the coefficient of the div in the constraints,
4777 * then we can also simply drop the div.
4779 * If an existentially quantified variable does not have an explicit
4780 * representation, appears in only a single lower bound that does not
4781 * involve any other such existentially quantified variables and appears
4782 * in this lower bound with coefficient 1,
4783 * then fix the variable to the value of the lower bound. That is,
4784 * turn the inequality into an equality.
4785 * If for any value of the other variables, there is any value
4786 * for the existentially quantified variable satisfying the constraints,
4787 * then this lower bound also satisfies the constraints.
4788 * It is therefore safe to pick this lower bound.
4790 * The same reasoning holds even if the coefficient is not one.
4791 * However, fixing the variable to the value of the lower bound may
4792 * in general introduce an extra integer division, in which case
4793 * it may be better to pick another value.
4794 * If this integer division has a known constant value, then plugging
4795 * in this constant value removes the existentially quantified variable
4796 * completely. In particular, if the lower bound is of the form
4797 * ceil((-f(x) - c)/n) and there are two constraints, f(x) + c0 >= 0 and
4798 * -f(x) + c1 >= 0 such that ceil((-c1 - c)/n) = ceil((c0 - c)/n),
4799 * then the existentially quantified variable can be assigned this
4802 * We skip divs that appear in equalities or in the definition of other divs.
4803 * Divs that appear in the definition of other divs usually occur in at least
4804 * 4 constraints, but the constraints may have been simplified.
4806 * If any divs are left after these simple checks then we move on
4807 * to more complicated cases in drop_more_redundant_divs.
4809 static __isl_give isl_basic_map
*isl_basic_map_drop_redundant_divs_ineq(
4810 __isl_take isl_basic_map
*bmap
)
4820 if (bmap
->n_div
== 0)
4823 off
= isl_basic_map_var_offset(bmap
, isl_dim_div
);
4825 return isl_basic_map_free(bmap
);
4826 pairs
= isl_calloc_array(bmap
->ctx
, int, bmap
->n_div
);
4830 n_ineq
= isl_basic_map_n_inequality(bmap
);
4831 for (i
= 0; i
< bmap
->n_div
; ++i
) {
4833 int last_pos
, last_neg
;
4836 isl_bool opp
, set_div
;
4838 defined
= !isl_int_is_zero(bmap
->div
[i
][0]);
4839 for (j
= i
; j
< bmap
->n_div
; ++j
)
4840 if (!isl_int_is_zero(bmap
->div
[j
][1 + 1 + off
+ i
]))
4842 if (j
< bmap
->n_div
)
4844 for (j
= 0; j
< bmap
->n_eq
; ++j
)
4845 if (!isl_int_is_zero(bmap
->eq
[j
][1 + off
+ i
]))
4851 for (j
= 0; j
< bmap
->n_ineq
; ++j
) {
4852 if (isl_int_is_pos(bmap
->ineq
[j
][1 + off
+ i
])) {
4856 if (isl_int_is_neg(bmap
->ineq
[j
][1 + off
+ i
])) {
4861 pairs
[i
] = pos
* neg
;
4862 if (pairs
[i
] == 0) {
4863 for (j
= bmap
->n_ineq
- 1; j
>= 0; --j
)
4864 if (!isl_int_is_zero(bmap
->ineq
[j
][1+off
+i
]))
4865 isl_basic_map_drop_inequality(bmap
, j
);
4866 bmap
= isl_basic_map_drop_div(bmap
, i
);
4867 return drop_redundant_divs_again(bmap
, pairs
, 0);
4870 opp
= isl_bool_false
;
4872 opp
= is_opposite(bmap
, last_pos
, last_neg
);
4877 isl_bool single
, one
;
4881 single
= single_unknown(bmap
, last_pos
, i
);
4886 one
= has_coef_one(bmap
, i
, last_pos
);
4890 return set_eq_and_try_again(bmap
, last_pos
,
4892 lower
= lower_bound_is_cst(bmap
, i
, last_pos
);
4896 return fix_cst_lower(bmap
, i
, last_pos
, lower
,
4901 isl_int_add(bmap
->ineq
[last_pos
][0],
4902 bmap
->ineq
[last_pos
][0], bmap
->ineq
[last_neg
][0]);
4903 isl_int_add_ui(bmap
->ineq
[last_pos
][0],
4904 bmap
->ineq
[last_pos
][0], 1);
4905 redundant
= isl_int_ge(bmap
->ineq
[last_pos
][0],
4906 bmap
->ineq
[last_pos
][1+off
+i
]);
4907 isl_int_sub_ui(bmap
->ineq
[last_pos
][0],
4908 bmap
->ineq
[last_pos
][0], 1);
4909 isl_int_sub(bmap
->ineq
[last_pos
][0],
4910 bmap
->ineq
[last_pos
][0], bmap
->ineq
[last_neg
][0]);
4912 return drop_div_and_try_again(bmap
, i
,
4913 last_pos
, last_neg
, pairs
);
4915 set_div
= isl_bool_false
;
4917 set_div
= ok_to_set_div_from_bound(bmap
, i
, last_pos
);
4919 return isl_basic_map_free(bmap
);
4921 bmap
= set_div_from_lower_bound(bmap
, i
, last_pos
);
4922 return drop_redundant_divs_again(bmap
, pairs
, 1);
4929 return coalesce_or_drop_more_redundant_divs(bmap
, pairs
, n
);
4935 isl_basic_map_free(bmap
);
4939 /* Consider the coefficients at "c" as a row vector and replace
4940 * them with their product with "T". "T" is assumed to be a square matrix.
4942 static isl_stat
preimage(isl_int
*c
, __isl_keep isl_mat
*T
)
4949 return isl_stat_error
;
4950 n
= isl_mat_rows(T
);
4951 if (isl_seq_first_non_zero(c
, n
) == -1)
4953 ctx
= isl_mat_get_ctx(T
);
4954 v
= isl_vec_alloc(ctx
, n
);
4956 return isl_stat_error
;
4957 isl_seq_swp_or_cpy(v
->el
, c
, n
);
4958 v
= isl_vec_mat_product(v
, isl_mat_copy(T
));
4960 return isl_stat_error
;
4961 isl_seq_swp_or_cpy(c
, v
->el
, n
);
4967 /* Plug in T for the variables in "bmap" starting at "pos".
4968 * T is a linear unimodular matrix, i.e., without constant term.
4970 static __isl_give isl_basic_map
*isl_basic_map_preimage_vars(
4971 __isl_take isl_basic_map
*bmap
, unsigned pos
, __isl_take isl_mat
*T
)
4976 bmap
= isl_basic_map_cow(bmap
);
4980 n
= isl_mat_cols(T
);
4981 if (n
!= isl_mat_rows(T
))
4982 isl_die(isl_mat_get_ctx(T
), isl_error_invalid
,
4983 "expecting square matrix", goto error
);
4985 if (isl_basic_map_check_range(bmap
, isl_dim_all
, pos
, n
) < 0)
4988 for (i
= 0; i
< bmap
->n_eq
; ++i
)
4989 if (preimage(bmap
->eq
[i
] + 1 + pos
, T
) < 0)
4991 for (i
= 0; i
< bmap
->n_ineq
; ++i
)
4992 if (preimage(bmap
->ineq
[i
] + 1 + pos
, T
) < 0)
4994 for (i
= 0; i
< bmap
->n_div
; ++i
) {
4995 if (isl_basic_map_div_is_marked_unknown(bmap
, i
))
4997 if (preimage(bmap
->div
[i
] + 1 + 1 + pos
, T
) < 0)
5004 isl_basic_map_free(bmap
);
5009 /* Remove divs that are not strictly needed.
5011 * First look for an equality constraint involving two or more
5012 * existentially quantified variables without an explicit
5013 * representation. Replace the combination that appears
5014 * in the equality constraint by a single existentially quantified
5015 * variable such that the equality can be used to derive
5016 * an explicit representation for the variable.
5017 * If there are no more such equality constraints, then continue
5018 * with isl_basic_map_drop_redundant_divs_ineq.
5020 * In particular, if the equality constraint is of the form
5022 * f(x) + \sum_i c_i a_i = 0
5024 * with a_i existentially quantified variable without explicit
5025 * representation, then apply a transformation on the existentially
5026 * quantified variables to turn the constraint into
5030 * with g the gcd of the c_i.
5031 * In order to easily identify which existentially quantified variables
5032 * have a complete explicit representation, i.e., without being defined
5033 * in terms of other existentially quantified variables without
5034 * an explicit representation, the existentially quantified variables
5037 * The variable transformation is computed by extending the row
5038 * [c_1/g ... c_n/g] to a unimodular matrix, obtaining the transformation
5040 * [a_1'] [c_1/g ... c_n/g] [ a_1 ]
5045 * with [c_1/g ... c_n/g] representing the first row of U.
5046 * The inverse of U is then plugged into the original constraints.
5047 * The call to isl_basic_map_simplify makes sure the explicit
5048 * representation for a_1' is extracted from the equality constraint.
5050 __isl_give isl_basic_map
*isl_basic_map_drop_redundant_divs(
5051 __isl_take isl_basic_map
*bmap
)
5063 if (isl_basic_map_divs_known(bmap
))
5064 return isl_basic_map_drop_redundant_divs_ineq(bmap
);
5065 if (bmap
->n_eq
== 0)
5066 return isl_basic_map_drop_redundant_divs_ineq(bmap
);
5067 bmap
= isl_basic_map_sort_divs(bmap
);
5071 first
= isl_basic_map_first_unknown_div(bmap
);
5073 return isl_basic_map_free(bmap
);
5075 o_div
= isl_basic_map_offset(bmap
, isl_dim_div
);
5076 n_div
= isl_basic_map_dim(bmap
, isl_dim_div
);
5078 return isl_basic_map_free(bmap
);
5080 for (i
= 0; i
< bmap
->n_eq
; ++i
) {
5081 l
= isl_seq_first_non_zero(bmap
->eq
[i
] + o_div
+ first
,
5086 if (isl_seq_first_non_zero(bmap
->eq
[i
] + o_div
+ l
+ 1,
5087 n_div
- (l
+ 1)) == -1)
5091 if (i
>= bmap
->n_eq
)
5092 return isl_basic_map_drop_redundant_divs_ineq(bmap
);
5094 ctx
= isl_basic_map_get_ctx(bmap
);
5095 T
= isl_mat_alloc(ctx
, n_div
- l
, n_div
- l
);
5097 return isl_basic_map_free(bmap
);
5098 isl_seq_cpy(T
->row
[0], bmap
->eq
[i
] + o_div
+ l
, n_div
- l
);
5099 T
= isl_mat_normalize_row(T
, 0);
5100 T
= isl_mat_unimodular_complete(T
, 1);
5101 T
= isl_mat_right_inverse(T
);
5103 for (i
= l
; i
< n_div
; ++i
)
5104 bmap
= isl_basic_map_mark_div_unknown(bmap
, i
);
5105 bmap
= isl_basic_map_preimage_vars(bmap
, o_div
- 1 + l
, T
);
5106 bmap
= isl_basic_map_simplify(bmap
);
5108 return isl_basic_map_drop_redundant_divs(bmap
);
5111 /* Does "bmap" satisfy any equality that involves more than 2 variables
5112 * and/or has coefficients different from -1 and 1?
5114 static isl_bool
has_multiple_var_equality(__isl_keep isl_basic_map
*bmap
)
5119 total
= isl_basic_map_dim(bmap
, isl_dim_all
);
5121 return isl_bool_error
;
5123 for (i
= 0; i
< bmap
->n_eq
; ++i
) {
5126 j
= isl_seq_first_non_zero(bmap
->eq
[i
] + 1, total
);
5129 if (!isl_int_is_one(bmap
->eq
[i
][1 + j
]) &&
5130 !isl_int_is_negone(bmap
->eq
[i
][1 + j
]))
5131 return isl_bool_true
;
5134 k
= isl_seq_first_non_zero(bmap
->eq
[i
] + 1 + j
, total
- j
);
5138 if (!isl_int_is_one(bmap
->eq
[i
][1 + j
]) &&
5139 !isl_int_is_negone(bmap
->eq
[i
][1 + j
]))
5140 return isl_bool_true
;
5143 k
= isl_seq_first_non_zero(bmap
->eq
[i
] + 1 + j
, total
- j
);
5145 return isl_bool_true
;
5148 return isl_bool_false
;
5151 /* Remove any common factor g from the constraint coefficients in "v".
5152 * The constant term is stored in the first position and is replaced
5153 * by floor(c/g). If any common factor is removed and if this results
5154 * in a tightening of the constraint, then set *tightened.
5156 static __isl_give isl_vec
*normalize_constraint(__isl_take isl_vec
*v
,
5163 ctx
= isl_vec_get_ctx(v
);
5164 isl_seq_gcd(v
->el
+ 1, v
->size
- 1, &ctx
->normalize_gcd
);
5165 if (isl_int_is_zero(ctx
->normalize_gcd
))
5167 if (isl_int_is_one(ctx
->normalize_gcd
))
5172 if (tightened
&& !isl_int_is_divisible_by(v
->el
[0], ctx
->normalize_gcd
))
5174 isl_int_fdiv_q(v
->el
[0], v
->el
[0], ctx
->normalize_gcd
);
5175 isl_seq_scale_down(v
->el
+ 1, v
->el
+ 1, ctx
->normalize_gcd
,
5180 /* If "bmap" is an integer set that satisfies any equality involving
5181 * more than 2 variables and/or has coefficients different from -1 and 1,
5182 * then use variable compression to reduce the coefficients by removing
5183 * any (hidden) common factor.
5184 * In particular, apply the variable compression to each constraint,
5185 * factor out any common factor in the non-constant coefficients and
5186 * then apply the inverse of the compression.
5187 * At the end, we mark the basic map as having reduced constants.
5188 * If this flag is still set on the next invocation of this function,
5189 * then we skip the computation.
5191 * Removing a common factor may result in a tightening of some of
5192 * the constraints. If this happens, then we may end up with two
5193 * opposite inequalities that can be replaced by an equality.
5194 * We therefore call isl_basic_map_detect_inequality_pairs,
5195 * which checks for such pairs of inequalities as well as eliminate_divs_eq
5196 * and isl_basic_map_gauss if such a pair was found.
5198 * Note that this function may leave the result in an inconsistent state.
5199 * In particular, the constraints may not be gaussed.
5200 * Unfortunately, isl_map_coalesce actually depends on this inconsistent state
5201 * for some of the test cases to pass successfully.
5202 * Any potential modification of the representation is therefore only
5203 * performed on a single copy of the basic map.
5205 __isl_give isl_basic_map
*isl_basic_map_reduce_coefficients(
5206 __isl_take isl_basic_map
*bmap
)
5212 isl_mat
*eq
, *T
, *T2
;
5218 if (ISL_F_ISSET(bmap
, ISL_BASIC_MAP_REDUCED_COEFFICIENTS
))
5220 if (isl_basic_map_is_rational(bmap
))
5222 if (bmap
->n_eq
== 0)
5224 multi
= has_multiple_var_equality(bmap
);
5226 return isl_basic_map_free(bmap
);
5230 total
= isl_basic_map_dim(bmap
, isl_dim_all
);
5232 return isl_basic_map_free(bmap
);
5233 ctx
= isl_basic_map_get_ctx(bmap
);
5234 v
= isl_vec_alloc(ctx
, 1 + total
);
5236 return isl_basic_map_free(bmap
);
5238 eq
= isl_mat_sub_alloc6(ctx
, bmap
->eq
, 0, bmap
->n_eq
, 0, 1 + total
);
5239 T
= isl_mat_variable_compression(eq
, &T2
);
5242 if (T
->n_col
== 0) {
5246 return isl_basic_map_set_to_empty(bmap
);
5249 bmap
= isl_basic_map_cow(bmap
);
5254 for (i
= 0; i
< bmap
->n_ineq
; ++i
) {
5255 isl_seq_cpy(v
->el
, bmap
->ineq
[i
], 1 + total
);
5256 v
= isl_vec_mat_product(v
, isl_mat_copy(T
));
5257 v
= normalize_constraint(v
, &tightened
);
5258 v
= isl_vec_mat_product(v
, isl_mat_copy(T2
));
5261 isl_seq_cpy(bmap
->ineq
[i
], v
->el
, 1 + total
);
5268 ISL_F_SET(bmap
, ISL_BASIC_MAP_REDUCED_COEFFICIENTS
);
5273 bmap
= isl_basic_map_detect_inequality_pairs(bmap
, &progress
);
5275 bmap
= eliminate_divs_eq(bmap
, &progress
);
5276 bmap
= isl_basic_map_gauss(bmap
, NULL
);
5285 return isl_basic_map_free(bmap
);
5288 /* Shift the integer division at position "div" of "bmap"
5289 * by "shift" times the variable at position "pos".
5290 * "pos" is as determined by isl_basic_map_offset, i.e., pos == 0
5291 * corresponds to the constant term.
5293 * That is, if the integer division has the form
5297 * then replace it by
5299 * floor((f(x) + shift * d * x_pos)/d) - shift * x_pos
5301 __isl_give isl_basic_map
*isl_basic_map_shift_div(
5302 __isl_take isl_basic_map
*bmap
, int div
, int pos
, isl_int shift
)
5305 isl_size total
, n_div
;
5307 if (isl_int_is_zero(shift
))
5309 total
= isl_basic_map_dim(bmap
, isl_dim_all
);
5310 n_div
= isl_basic_map_dim(bmap
, isl_dim_div
);
5312 if (total
< 0 || n_div
< 0)
5313 return isl_basic_map_free(bmap
);
5315 isl_int_addmul(bmap
->div
[div
][1 + pos
], shift
, bmap
->div
[div
][0]);
5317 for (i
= 0; i
< bmap
->n_eq
; ++i
) {
5318 if (isl_int_is_zero(bmap
->eq
[i
][1 + total
+ div
]))
5320 isl_int_submul(bmap
->eq
[i
][pos
],
5321 shift
, bmap
->eq
[i
][1 + total
+ div
]);
5323 for (i
= 0; i
< bmap
->n_ineq
; ++i
) {
5324 if (isl_int_is_zero(bmap
->ineq
[i
][1 + total
+ div
]))
5326 isl_int_submul(bmap
->ineq
[i
][pos
],
5327 shift
, bmap
->ineq
[i
][1 + total
+ div
]);
5329 for (i
= 0; i
< bmap
->n_div
; ++i
) {
5330 if (isl_int_is_zero(bmap
->div
[i
][0]))
5332 if (isl_int_is_zero(bmap
->div
[i
][1 + 1 + total
+ div
]))
5334 isl_int_submul(bmap
->div
[i
][1 + pos
],
5335 shift
, bmap
->div
[i
][1 + 1 + total
+ div
]);