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
;
1909 isl_size n_ineq
, cols
;
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 cols
= isl_mat_cols(ineq
);
1922 if (n_ineq
< 0 || cols
< 0)
1923 return isl_stat_error
;
1925 for (k
= 0; k
< n_ineq
; ++k
) {
1929 l
= isl_seq_first_non_zero(ineq
->row
[k
] + 1, total
);
1930 if (l
< 0 && isl_int_is_nonneg(ineq
->row
[k
][0])) {
1934 redundant
= constraint_index_is_redundant(&ci
, ineq
->row
[k
]);
1941 constraint_index_free(&ci
);
1944 constraint_index_free(&ci
);
1945 return isl_stat_error
;
1948 static __isl_give isl_basic_set
*remove_shifted_constraints(
1949 __isl_take isl_basic_set
*bset
, __isl_keep isl_basic_set
*context
)
1951 struct isl_constraint_index ci
;
1954 if (!bset
|| !context
)
1957 if (context
->n_ineq
== 0)
1959 if (setup_constraint_index(&ci
, context
) < 0)
1962 for (k
= 0; k
< bset
->n_ineq
; ++k
) {
1965 redundant
= constraint_index_is_redundant(&ci
, bset
->ineq
[k
]);
1970 bset
= isl_basic_set_cow(bset
);
1973 isl_basic_set_drop_inequality(bset
, k
);
1976 constraint_index_free(&ci
);
1979 constraint_index_free(&ci
);
1983 /* Remove constraints from "bmap" that are identical to constraints
1984 * in "context" or that are more relaxed (greater constant term).
1986 * We perform the test for shifted copies on the pure constraints
1987 * in remove_shifted_constraints.
1989 static __isl_give isl_basic_map
*isl_basic_map_remove_shifted_constraints(
1990 __isl_take isl_basic_map
*bmap
, __isl_take isl_basic_map
*context
)
1992 isl_basic_set
*bset
, *bset_context
;
1994 if (!bmap
|| !context
)
1997 if (bmap
->n_ineq
== 0 || context
->n_ineq
== 0) {
1998 isl_basic_map_free(context
);
2002 context
= isl_basic_map_align_divs(context
, bmap
);
2003 bmap
= isl_basic_map_align_divs(bmap
, context
);
2005 bset
= isl_basic_map_underlying_set(isl_basic_map_copy(bmap
));
2006 bset_context
= isl_basic_map_underlying_set(context
);
2007 bset
= remove_shifted_constraints(bset
, bset_context
);
2008 isl_basic_set_free(bset_context
);
2010 bmap
= isl_basic_map_overlying_set(bset
, bmap
);
2014 isl_basic_map_free(bmap
);
2015 isl_basic_map_free(context
);
2019 /* Does the (linear part of a) constraint "c" involve any of the "len"
2020 * "relevant" dimensions?
2022 static int is_related(isl_int
*c
, int len
, int *relevant
)
2026 for (i
= 0; i
< len
; ++i
) {
2029 if (!isl_int_is_zero(c
[i
]))
2036 /* Drop constraints from "bmap" that do not involve any of
2037 * the dimensions marked "relevant".
2039 static __isl_give isl_basic_map
*drop_unrelated_constraints(
2040 __isl_take isl_basic_map
*bmap
, int *relevant
)
2045 dim
= isl_basic_map_dim(bmap
, isl_dim_all
);
2047 return isl_basic_map_free(bmap
);
2048 for (i
= 0; i
< dim
; ++i
)
2054 for (i
= bmap
->n_eq
- 1; i
>= 0; --i
)
2055 if (!is_related(bmap
->eq
[i
] + 1, dim
, relevant
)) {
2056 bmap
= isl_basic_map_cow(bmap
);
2057 if (isl_basic_map_drop_equality(bmap
, i
) < 0)
2058 return isl_basic_map_free(bmap
);
2061 for (i
= bmap
->n_ineq
- 1; i
>= 0; --i
)
2062 if (!is_related(bmap
->ineq
[i
] + 1, dim
, relevant
)) {
2063 bmap
= isl_basic_map_cow(bmap
);
2064 if (isl_basic_map_drop_inequality(bmap
, i
) < 0)
2065 return isl_basic_map_free(bmap
);
2071 /* Update the groups in "group" based on the (linear part of a) constraint "c".
2073 * In particular, for any variable involved in the constraint,
2074 * find the actual group id from before and replace the group
2075 * of the corresponding variable by the minimal group of all
2076 * the variables involved in the constraint considered so far
2077 * (if this minimum is smaller) or replace the minimum by this group
2078 * (if the minimum is larger).
2080 * At the end, all the variables in "c" will (indirectly) point
2081 * to the minimal of the groups that they referred to originally.
2083 static void update_groups(int dim
, int *group
, isl_int
*c
)
2088 for (j
= 0; j
< dim
; ++j
) {
2089 if (isl_int_is_zero(c
[j
]))
2091 while (group
[j
] >= 0 && group
[group
[j
]] != group
[j
])
2092 group
[j
] = group
[group
[j
]];
2093 if (group
[j
] == min
)
2095 if (group
[j
] < min
) {
2096 if (min
>= 0 && min
< dim
)
2097 group
[min
] = group
[j
];
2100 group
[group
[j
]] = min
;
2104 /* Allocate an array of groups of variables, one for each variable
2105 * in "context", initialized to zero.
2107 static int *alloc_groups(__isl_keep isl_basic_set
*context
)
2112 dim
= isl_basic_set_dim(context
, isl_dim_set
);
2115 ctx
= isl_basic_set_get_ctx(context
);
2116 return isl_calloc_array(ctx
, int, dim
);
2119 /* Drop constraints from "bmap" that only involve variables that are
2120 * not related to any of the variables marked with a "-1" in "group".
2122 * We construct groups of variables that collect variables that
2123 * (indirectly) appear in some common constraint of "bmap".
2124 * Each group is identified by the first variable in the group,
2125 * except for the special group of variables that was already identified
2126 * in the input as -1 (or are related to those variables).
2127 * If group[i] is equal to i (or -1), then the group of i is i (or -1),
2128 * otherwise the group of i is the group of group[i].
2130 * We first initialize groups for the remaining variables.
2131 * Then we iterate over the constraints of "bmap" and update the
2132 * group of the variables in the constraint by the smallest group.
2133 * Finally, we resolve indirect references to groups by running over
2136 * After computing the groups, we drop constraints that do not involve
2137 * any variables in the -1 group.
2139 __isl_give isl_basic_map
*isl_basic_map_drop_unrelated_constraints(
2140 __isl_take isl_basic_map
*bmap
, __isl_take
int *group
)
2146 dim
= isl_basic_map_dim(bmap
, isl_dim_all
);
2148 return isl_basic_map_free(bmap
);
2151 for (i
= 0; i
< dim
; ++i
)
2153 last
= group
[i
] = i
;
2159 for (i
= 0; i
< bmap
->n_eq
; ++i
)
2160 update_groups(dim
, group
, bmap
->eq
[i
] + 1);
2161 for (i
= 0; i
< bmap
->n_ineq
; ++i
)
2162 update_groups(dim
, group
, bmap
->ineq
[i
] + 1);
2164 for (i
= 0; i
< dim
; ++i
)
2166 group
[i
] = group
[group
[i
]];
2168 for (i
= 0; i
< dim
; ++i
)
2169 group
[i
] = group
[i
] == -1;
2171 bmap
= drop_unrelated_constraints(bmap
, group
);
2177 /* Drop constraints from "context" that are irrelevant for computing
2178 * the gist of "bset".
2180 * In particular, drop constraints in variables that are not related
2181 * to any of the variables involved in the constraints of "bset"
2182 * in the sense that there is no sequence of constraints that connects them.
2184 * We first mark all variables that appear in "bset" as belonging
2185 * to a "-1" group and then continue with group_and_drop_irrelevant_constraints.
2187 static __isl_give isl_basic_set
*drop_irrelevant_constraints(
2188 __isl_take isl_basic_set
*context
, __isl_keep isl_basic_set
*bset
)
2194 dim
= isl_basic_set_dim(bset
, isl_dim_set
);
2195 if (!context
|| dim
< 0)
2196 return isl_basic_set_free(context
);
2198 group
= alloc_groups(context
);
2201 return isl_basic_set_free(context
);
2203 for (i
= 0; i
< dim
; ++i
) {
2204 for (j
= 0; j
< bset
->n_eq
; ++j
)
2205 if (!isl_int_is_zero(bset
->eq
[j
][1 + i
]))
2207 if (j
< bset
->n_eq
) {
2211 for (j
= 0; j
< bset
->n_ineq
; ++j
)
2212 if (!isl_int_is_zero(bset
->ineq
[j
][1 + i
]))
2214 if (j
< bset
->n_ineq
)
2218 return isl_basic_map_drop_unrelated_constraints(context
, group
);
2221 /* Drop constraints from "context" that are irrelevant for computing
2222 * the gist of the inequalities "ineq".
2223 * Inequalities in "ineq" for which the corresponding element of row
2224 * is set to -1 have already been marked for removal and should be ignored.
2226 * In particular, drop constraints in variables that are not related
2227 * to any of the variables involved in "ineq"
2228 * in the sense that there is no sequence of constraints that connects them.
2230 * We first mark all variables that appear in "bset" as belonging
2231 * to a "-1" group and then continue with group_and_drop_irrelevant_constraints.
2233 static __isl_give isl_basic_set
*drop_irrelevant_constraints_marked(
2234 __isl_take isl_basic_set
*context
, __isl_keep isl_mat
*ineq
, int *row
)
2241 dim
= isl_basic_set_dim(context
, isl_dim_set
);
2242 n
= isl_mat_rows(ineq
);
2243 if (dim
< 0 || n
< 0)
2244 return isl_basic_set_free(context
);
2246 group
= alloc_groups(context
);
2249 return isl_basic_set_free(context
);
2251 for (i
= 0; i
< dim
; ++i
) {
2252 for (j
= 0; j
< n
; ++j
) {
2255 if (!isl_int_is_zero(ineq
->row
[j
][1 + i
]))
2262 return isl_basic_map_drop_unrelated_constraints(context
, group
);
2265 /* Do all "n" entries of "row" contain a negative value?
2267 static int all_neg(int *row
, int n
)
2271 for (i
= 0; i
< n
; ++i
)
2278 /* Update the inequalities in "bset" based on the information in "row"
2281 * In particular, the array "row" contains either -1, meaning that
2282 * the corresponding inequality of "bset" is redundant, or the index
2283 * of an inequality in "tab".
2285 * If the row entry is -1, then drop the inequality.
2286 * Otherwise, if the constraint is marked redundant in the tableau,
2287 * then drop the inequality. Similarly, if it is marked as an equality
2288 * in the tableau, then turn the inequality into an equality and
2289 * perform Gaussian elimination.
2291 static __isl_give isl_basic_set
*update_ineq(__isl_take isl_basic_set
*bset
,
2292 __isl_keep
int *row
, struct isl_tab
*tab
)
2297 int found_equality
= 0;
2301 if (tab
&& tab
->empty
)
2302 return isl_basic_set_set_to_empty(bset
);
2304 n_ineq
= bset
->n_ineq
;
2305 for (i
= n_ineq
- 1; i
>= 0; --i
) {
2307 if (isl_basic_set_drop_inequality(bset
, i
) < 0)
2308 return isl_basic_set_free(bset
);
2314 if (isl_tab_is_equality(tab
, n_eq
+ row
[i
])) {
2315 isl_basic_map_inequality_to_equality(bset
, i
);
2317 } else if (isl_tab_is_redundant(tab
, n_eq
+ row
[i
])) {
2318 if (isl_basic_set_drop_inequality(bset
, i
) < 0)
2319 return isl_basic_set_free(bset
);
2324 bset
= isl_basic_set_gauss(bset
, NULL
);
2325 bset
= isl_basic_set_finalize(bset
);
2329 /* Update the inequalities in "bset" based on the information in "row"
2330 * and "tab" and free all arguments (other than "bset").
2332 static __isl_give isl_basic_set
*update_ineq_free(
2333 __isl_take isl_basic_set
*bset
, __isl_take isl_mat
*ineq
,
2334 __isl_take isl_basic_set
*context
, __isl_take
int *row
,
2335 struct isl_tab
*tab
)
2338 isl_basic_set_free(context
);
2340 bset
= update_ineq(bset
, row
, tab
);
2347 /* Remove all information from bset that is redundant in the context
2349 * "ineq" contains the (possibly transformed) inequalities of "bset",
2350 * in the same order.
2351 * The (explicit) equalities of "bset" are assumed to have been taken
2352 * into account by the transformation such that only the inequalities
2354 * "context" is assumed not to be empty.
2356 * "row" keeps track of the constraint index of a "bset" inequality in "tab".
2357 * A value of -1 means that the inequality is obviously redundant and may
2358 * not even appear in "tab".
2360 * We first mark the inequalities of "bset"
2361 * that are obviously redundant with respect to some inequality in "context".
2362 * Then we remove those constraints from "context" that have become
2363 * irrelevant for computing the gist of "bset".
2364 * Note that this removal of constraints cannot be replaced by
2365 * a factorization because factors in "bset" may still be connected
2366 * to each other through constraints in "context".
2368 * If there are any inequalities left, we construct a tableau for
2369 * the context and then add the inequalities of "bset".
2370 * Before adding these inequalities, we freeze all constraints such that
2371 * they won't be considered redundant in terms of the constraints of "bset".
2372 * Then we detect all redundant constraints (among the
2373 * constraints that weren't frozen), first by checking for redundancy in the
2374 * the tableau and then by checking if replacing a constraint by its negation
2375 * would lead to an empty set. This last step is fairly expensive
2376 * and could be optimized by more reuse of the tableau.
2377 * Finally, we update bset according to the results.
2379 static __isl_give isl_basic_set
*uset_gist_full(__isl_take isl_basic_set
*bset
,
2380 __isl_take isl_mat
*ineq
, __isl_take isl_basic_set
*context
)
2385 isl_basic_set
*combined
= NULL
;
2386 struct isl_tab
*tab
= NULL
;
2387 unsigned n_eq
, context_ineq
;
2389 if (!bset
|| !ineq
|| !context
)
2392 if (bset
->n_ineq
== 0 || isl_basic_set_plain_is_universe(context
)) {
2393 isl_basic_set_free(context
);
2398 ctx
= isl_basic_set_get_ctx(context
);
2399 row
= isl_calloc_array(ctx
, int, bset
->n_ineq
);
2403 if (mark_shifted_constraints(ineq
, context
, row
) < 0)
2405 if (all_neg(row
, bset
->n_ineq
))
2406 return update_ineq_free(bset
, ineq
, context
, row
, NULL
);
2408 context
= drop_irrelevant_constraints_marked(context
, ineq
, row
);
2411 if (isl_basic_set_plain_is_universe(context
))
2412 return update_ineq_free(bset
, ineq
, context
, row
, NULL
);
2414 n_eq
= context
->n_eq
;
2415 context_ineq
= context
->n_ineq
;
2416 combined
= isl_basic_set_cow(isl_basic_set_copy(context
));
2417 combined
= isl_basic_set_extend_constraints(combined
, 0, bset
->n_ineq
);
2418 tab
= isl_tab_from_basic_set(combined
, 0);
2419 for (i
= 0; i
< context_ineq
; ++i
)
2420 if (isl_tab_freeze_constraint(tab
, n_eq
+ i
) < 0)
2422 if (isl_tab_extend_cons(tab
, bset
->n_ineq
) < 0)
2425 for (i
= 0; i
< bset
->n_ineq
; ++i
) {
2428 combined
= isl_basic_set_add_ineq(combined
, ineq
->row
[i
]);
2429 if (isl_tab_add_ineq(tab
, ineq
->row
[i
]) < 0)
2433 if (isl_tab_detect_implicit_equalities(tab
) < 0)
2435 if (isl_tab_detect_redundant(tab
) < 0)
2437 for (i
= bset
->n_ineq
- 1; i
>= 0; --i
) {
2438 isl_basic_set
*test
;
2444 if (tab
->con
[n_eq
+ r
].is_redundant
)
2446 test
= isl_basic_set_dup(combined
);
2447 test
= isl_inequality_negate(test
, r
);
2448 test
= isl_basic_set_update_from_tab(test
, tab
);
2449 is_empty
= isl_basic_set_is_empty(test
);
2450 isl_basic_set_free(test
);
2454 tab
->con
[n_eq
+ r
].is_redundant
= 1;
2456 bset
= update_ineq_free(bset
, ineq
, context
, row
, tab
);
2458 ISL_F_SET(bset
, ISL_BASIC_SET_NO_IMPLICIT
);
2459 ISL_F_SET(bset
, ISL_BASIC_SET_NO_REDUNDANT
);
2462 isl_basic_set_free(combined
);
2468 isl_basic_set_free(combined
);
2469 isl_basic_set_free(context
);
2470 isl_basic_set_free(bset
);
2474 /* Extract the inequalities of "bset" as an isl_mat.
2476 static __isl_give isl_mat
*extract_ineq(__isl_keep isl_basic_set
*bset
)
2482 total
= isl_basic_set_dim(bset
, isl_dim_all
);
2486 ctx
= isl_basic_set_get_ctx(bset
);
2487 ineq
= isl_mat_sub_alloc6(ctx
, bset
->ineq
, 0, bset
->n_ineq
,
2493 /* Remove all information from "bset" that is redundant in the context
2494 * of "context", for the case where both "bset" and "context" are
2497 static __isl_give isl_basic_set
*uset_gist_uncompressed(
2498 __isl_take isl_basic_set
*bset
, __isl_take isl_basic_set
*context
)
2502 ineq
= extract_ineq(bset
);
2503 return uset_gist_full(bset
, ineq
, context
);
2506 /* Remove all information from "bset" that is redundant in the context
2507 * of "context", for the case where the combined equalities of
2508 * "bset" and "context" allow for a compression that can be obtained
2509 * by preapplication of "T".
2511 * "bset" itself is not transformed by "T". Instead, the inequalities
2512 * are extracted from "bset" and those are transformed by "T".
2513 * uset_gist_full then determines which of the transformed inequalities
2514 * are redundant with respect to the transformed "context" and removes
2515 * the corresponding inequalities from "bset".
2517 * After preapplying "T" to the inequalities, any common factor is
2518 * removed from the coefficients. If this results in a tightening
2519 * of the constant term, then the same tightening is applied to
2520 * the corresponding untransformed inequality in "bset".
2521 * That is, if after plugging in T, a constraint f(x) >= 0 is of the form
2525 * with 0 <= r < g, then it is equivalent to
2529 * This means that f(x) >= 0 is equivalent to f(x) - r >= 0 in the affine
2530 * subspace compressed by T since the latter would be transformed to
2534 static __isl_give isl_basic_set
*uset_gist_compressed(
2535 __isl_take isl_basic_set
*bset
, __isl_take isl_basic_set
*context
,
2536 __isl_take isl_mat
*T
)
2541 isl_size n_row
, n_col
;
2544 ineq
= extract_ineq(bset
);
2545 ineq
= isl_mat_product(ineq
, isl_mat_copy(T
));
2546 context
= isl_basic_set_preimage(context
, T
);
2548 if (!ineq
|| !context
)
2550 if (isl_basic_set_plain_is_empty(context
)) {
2552 isl_basic_set_free(context
);
2553 return isl_basic_set_set_to_empty(bset
);
2556 ctx
= isl_mat_get_ctx(ineq
);
2557 n_row
= isl_mat_rows(ineq
);
2558 n_col
= isl_mat_cols(ineq
);
2559 if (n_row
< 0 || n_col
< 0)
2562 for (i
= 0; i
< n_row
; ++i
) {
2563 isl_seq_gcd(ineq
->row
[i
] + 1, n_col
- 1, &ctx
->normalize_gcd
);
2564 if (isl_int_is_zero(ctx
->normalize_gcd
))
2566 if (isl_int_is_one(ctx
->normalize_gcd
))
2568 isl_seq_scale_down(ineq
->row
[i
] + 1, ineq
->row
[i
] + 1,
2569 ctx
->normalize_gcd
, n_col
- 1);
2570 isl_int_fdiv_r(rem
, ineq
->row
[i
][0], ctx
->normalize_gcd
);
2571 isl_int_fdiv_q(ineq
->row
[i
][0],
2572 ineq
->row
[i
][0], ctx
->normalize_gcd
);
2573 if (isl_int_is_zero(rem
))
2575 bset
= isl_basic_set_cow(bset
);
2578 isl_int_sub(bset
->ineq
[i
][0], bset
->ineq
[i
][0], rem
);
2582 return uset_gist_full(bset
, ineq
, context
);
2585 isl_basic_set_free(context
);
2586 isl_basic_set_free(bset
);
2590 /* Project "bset" onto the variables that are involved in "template".
2592 static __isl_give isl_basic_set
*project_onto_involved(
2593 __isl_take isl_basic_set
*bset
, __isl_keep isl_basic_set
*template)
2598 n
= isl_basic_set_dim(template, isl_dim_set
);
2599 if (n
< 0 || !template)
2600 return isl_basic_set_free(bset
);
2602 for (i
= 0; i
< n
; ++i
) {
2605 involved
= isl_basic_set_involves_dims(template,
2608 return isl_basic_set_free(bset
);
2611 bset
= isl_basic_set_eliminate_vars(bset
, i
, 1);
2617 /* Remove all information from bset that is redundant in the context
2618 * of context. In particular, equalities that are linear combinations
2619 * of those in context are removed. Then the inequalities that are
2620 * redundant in the context of the equalities and inequalities of
2621 * context are removed.
2623 * First of all, we drop those constraints from "context"
2624 * that are irrelevant for computing the gist of "bset".
2625 * Alternatively, we could factorize the intersection of "context" and "bset".
2627 * We first compute the intersection of the integer affine hulls
2628 * of "bset" and "context",
2629 * compute the gist inside this intersection and then reduce
2630 * the constraints with respect to the equalities of the context
2631 * that only involve variables already involved in the input.
2633 * If two constraints are mutually redundant, then uset_gist_full
2634 * will remove the second of those constraints. We therefore first
2635 * sort the constraints so that constraints not involving existentially
2636 * quantified variables are given precedence over those that do.
2637 * We have to perform this sorting before the variable compression,
2638 * because that may effect the order of the variables.
2640 static __isl_give isl_basic_set
*uset_gist(__isl_take isl_basic_set
*bset
,
2641 __isl_take isl_basic_set
*context
)
2646 isl_basic_set
*aff_context
;
2649 total
= isl_basic_set_dim(bset
, isl_dim_all
);
2650 if (total
< 0 || !context
)
2653 context
= drop_irrelevant_constraints(context
, bset
);
2655 bset
= isl_basic_set_detect_equalities(bset
);
2656 aff
= isl_basic_set_copy(bset
);
2657 aff
= isl_basic_set_plain_affine_hull(aff
);
2658 context
= isl_basic_set_detect_equalities(context
);
2659 aff_context
= isl_basic_set_copy(context
);
2660 aff_context
= isl_basic_set_plain_affine_hull(aff_context
);
2661 aff
= isl_basic_set_intersect(aff
, aff_context
);
2664 if (isl_basic_set_plain_is_empty(aff
)) {
2665 isl_basic_set_free(bset
);
2666 isl_basic_set_free(context
);
2669 bset
= isl_basic_set_sort_constraints(bset
);
2670 if (aff
->n_eq
== 0) {
2671 isl_basic_set_free(aff
);
2672 return uset_gist_uncompressed(bset
, context
);
2674 eq
= isl_mat_sub_alloc6(bset
->ctx
, aff
->eq
, 0, aff
->n_eq
, 0, 1 + total
);
2675 eq
= isl_mat_cow(eq
);
2676 T
= isl_mat_variable_compression(eq
, NULL
);
2677 isl_basic_set_free(aff
);
2678 if (T
&& T
->n_col
== 0) {
2680 isl_basic_set_free(context
);
2681 return isl_basic_set_set_to_empty(bset
);
2684 aff_context
= isl_basic_set_affine_hull(isl_basic_set_copy(context
));
2685 aff_context
= project_onto_involved(aff_context
, bset
);
2687 bset
= uset_gist_compressed(bset
, context
, T
);
2688 bset
= isl_basic_set_reduce_using_equalities(bset
, aff_context
);
2691 ISL_F_SET(bset
, ISL_BASIC_SET_NO_IMPLICIT
);
2692 ISL_F_SET(bset
, ISL_BASIC_SET_NO_REDUNDANT
);
2697 isl_basic_set_free(bset
);
2698 isl_basic_set_free(context
);
2702 /* Return the number of equality constraints in "bmap" that involve
2703 * local variables. This function assumes that Gaussian elimination
2704 * has been applied to the equality constraints.
2706 static int n_div_eq(__isl_keep isl_basic_map
*bmap
)
2709 isl_size total
, n_div
;
2714 if (bmap
->n_eq
== 0)
2717 total
= isl_basic_map_dim(bmap
, isl_dim_all
);
2718 n_div
= isl_basic_map_dim(bmap
, isl_dim_div
);
2719 if (total
< 0 || n_div
< 0)
2723 for (i
= 0; i
< bmap
->n_eq
; ++i
)
2724 if (isl_seq_first_non_zero(bmap
->eq
[i
] + 1 + total
,
2731 /* Construct a basic map in "space" defined by the equality constraints in "eq".
2732 * The constraints are assumed not to involve any local variables.
2734 static __isl_give isl_basic_map
*basic_map_from_equalities(
2735 __isl_take isl_space
*space
, __isl_take isl_mat
*eq
)
2739 isl_basic_map
*bmap
= NULL
;
2741 total
= isl_space_dim(space
, isl_dim_all
);
2742 if (total
< 0 || !eq
)
2745 if (1 + total
!= eq
->n_col
)
2746 isl_die(isl_space_get_ctx(space
), isl_error_internal
,
2747 "unexpected number of columns", goto error
);
2749 bmap
= isl_basic_map_alloc_space(isl_space_copy(space
),
2751 for (i
= 0; i
< eq
->n_row
; ++i
) {
2752 k
= isl_basic_map_alloc_equality(bmap
);
2755 isl_seq_cpy(bmap
->eq
[k
], eq
->row
[i
], eq
->n_col
);
2758 isl_space_free(space
);
2762 isl_space_free(space
);
2764 isl_basic_map_free(bmap
);
2768 /* Construct and return a variable compression based on the equality
2769 * constraints in "bmap1" and "bmap2" that do not involve the local variables.
2770 * "n1" is the number of (initial) equality constraints in "bmap1"
2771 * that do involve local variables.
2772 * "n2" is the number of (initial) equality constraints in "bmap2"
2773 * that do involve local variables.
2774 * "total" is the total number of other variables.
2775 * This function assumes that Gaussian elimination
2776 * has been applied to the equality constraints in both "bmap1" and "bmap2"
2777 * such that the equality constraints not involving local variables
2778 * are those that start at "n1" or "n2".
2780 * If either of "bmap1" and "bmap2" does not have such equality constraints,
2781 * then simply compute the compression based on the equality constraints
2782 * in the other basic map.
2783 * Otherwise, combine the equality constraints from both into a new
2784 * basic map such that Gaussian elimination can be applied to this combination
2785 * and then construct a variable compression from the resulting
2786 * equality constraints.
2788 static __isl_give isl_mat
*combined_variable_compression(
2789 __isl_keep isl_basic_map
*bmap1
, int n1
,
2790 __isl_keep isl_basic_map
*bmap2
, int n2
, int total
)
2793 isl_mat
*E1
, *E2
, *V
;
2794 isl_basic_map
*bmap
;
2796 ctx
= isl_basic_map_get_ctx(bmap1
);
2797 if (bmap1
->n_eq
== n1
) {
2798 E2
= isl_mat_sub_alloc6(ctx
, bmap2
->eq
,
2799 n2
, bmap2
->n_eq
- n2
, 0, 1 + total
);
2800 return isl_mat_variable_compression(E2
, NULL
);
2802 if (bmap2
->n_eq
== n2
) {
2803 E1
= isl_mat_sub_alloc6(ctx
, bmap1
->eq
,
2804 n1
, bmap1
->n_eq
- n1
, 0, 1 + total
);
2805 return isl_mat_variable_compression(E1
, NULL
);
2807 E1
= isl_mat_sub_alloc6(ctx
, bmap1
->eq
,
2808 n1
, bmap1
->n_eq
- n1
, 0, 1 + total
);
2809 E2
= isl_mat_sub_alloc6(ctx
, bmap2
->eq
,
2810 n2
, bmap2
->n_eq
- n2
, 0, 1 + total
);
2811 E1
= isl_mat_concat(E1
, E2
);
2812 bmap
= basic_map_from_equalities(isl_basic_map_get_space(bmap1
), E1
);
2813 bmap
= isl_basic_map_gauss(bmap
, NULL
);
2816 E1
= isl_mat_sub_alloc6(ctx
, bmap
->eq
, 0, bmap
->n_eq
, 0, 1 + total
);
2817 V
= isl_mat_variable_compression(E1
, NULL
);
2818 isl_basic_map_free(bmap
);
2823 /* Extract the stride constraints from "bmap", compressed
2824 * with respect to both the stride constraints in "context" and
2825 * the remaining equality constraints in both "bmap" and "context".
2826 * "bmap_n_eq" is the number of (initial) stride constraints in "bmap".
2827 * "context_n_eq" is the number of (initial) stride constraints in "context".
2829 * Let x be all variables in "bmap" (and "context") other than the local
2830 * variables. First compute a variable compression
2834 * based on the non-stride equality constraints in "bmap" and "context".
2835 * Consider the stride constraints of "context",
2839 * with y the local variables and plug in the variable compression,
2842 * A(V x') + B(y) = 0
2844 * Use these constraints to compute a parameter compression on x'
2848 * Now consider the stride constraints of "bmap"
2852 * and plug in x = V*T x''.
2853 * That is, return A = [C*V*T D].
2855 static __isl_give isl_mat
*extract_compressed_stride_constraints(
2856 __isl_keep isl_basic_map
*bmap
, int bmap_n_eq
,
2857 __isl_keep isl_basic_map
*context
, int context_n_eq
)
2859 isl_size total
, n_div
;
2861 isl_mat
*A
, *B
, *T
, *V
;
2863 total
= isl_basic_map_dim(context
, isl_dim_all
);
2864 n_div
= isl_basic_map_dim(context
, isl_dim_div
);
2865 if (total
< 0 || n_div
< 0)
2869 ctx
= isl_basic_map_get_ctx(bmap
);
2871 V
= combined_variable_compression(bmap
, bmap_n_eq
,
2872 context
, context_n_eq
, total
);
2874 A
= isl_mat_sub_alloc6(ctx
, context
->eq
, 0, context_n_eq
, 0, 1 + total
);
2875 B
= isl_mat_sub_alloc6(ctx
, context
->eq
,
2876 0, context_n_eq
, 1 + total
, n_div
);
2877 A
= isl_mat_product(A
, isl_mat_copy(V
));
2878 T
= isl_mat_parameter_compression_ext(A
, B
);
2879 T
= isl_mat_product(V
, T
);
2881 n_div
= isl_basic_map_dim(bmap
, isl_dim_div
);
2883 T
= isl_mat_free(T
);
2885 T
= isl_mat_diagonal(T
, isl_mat_identity(ctx
, n_div
));
2887 A
= isl_mat_sub_alloc6(ctx
, bmap
->eq
,
2888 0, bmap_n_eq
, 0, 1 + total
+ n_div
);
2889 A
= isl_mat_product(A
, T
);
2894 /* Remove the prime factors from *g that have an exponent that
2895 * is strictly smaller than the exponent in "c".
2896 * All exponents in *g are known to be smaller than or equal
2899 * That is, if *g is equal to
2901 * p_1^{e_1} p_2^{e_2} ... p_n^{e_n}
2903 * and "c" is equal to
2905 * p_1^{f_1} p_2^{f_2} ... p_n^{f_n}
2909 * p_1^{e_1 * (e_1 = f_1)} p_2^{e_2 * (e_2 = f_2)} ...
2910 * p_n^{e_n * (e_n = f_n)}
2912 * If e_i = f_i, then c / *g does not have any p_i factors and therefore
2913 * neither does the gcd of *g and c / *g.
2914 * If e_i < f_i, then the gcd of *g and c / *g has a positive
2915 * power min(e_i, s_i) of p_i with s_i = f_i - e_i among its factors.
2916 * Dividing *g by this gcd therefore strictly reduces the exponent
2917 * of the prime factors that need to be removed, while leaving the
2918 * other prime factors untouched.
2919 * Repeating this process until gcd(*g, c / *g) = 1 therefore
2920 * removes all undesired factors, without removing any others.
2922 static void remove_incomplete_powers(isl_int
*g
, isl_int c
)
2928 isl_int_divexact(t
, c
, *g
);
2929 isl_int_gcd(t
, t
, *g
);
2930 if (isl_int_is_one(t
))
2932 isl_int_divexact(*g
, *g
, t
);
2937 /* Reduce the "n" stride constraints in "bmap" based on a copy "A"
2938 * of the same stride constraints in a compressed space that exploits
2939 * all equalities in the context and the other equalities in "bmap".
2941 * If the stride constraints of "bmap" are of the form
2945 * then A is of the form
2949 * If any of these constraints involves only a single local variable y,
2950 * then the constraint appears as
2960 * Let g be the gcd of m and the coefficients of h.
2961 * Then, in particular, g is a divisor of the coefficients of h and
2965 * is known to be a multiple of g.
2966 * If some prime factor in m appears with the same exponent in g,
2967 * then it can be removed from m because f(x) is already known
2968 * to be a multiple of g and therefore in particular of this power
2969 * of the prime factors.
2970 * Prime factors that appear with a smaller exponent in g cannot
2971 * be removed from m.
2972 * Let g' be the divisor of g containing all prime factors that
2973 * appear with the same exponent in m and g, then
2977 * can be replaced by
2979 * f(x) + m/g' y_i' = 0
2981 * Note that (if g' != 1) this changes the explicit representation
2982 * of y_i to that of y_i', so the integer division at position i
2983 * is marked unknown and later recomputed by a call to
2984 * isl_basic_map_gauss.
2986 static __isl_give isl_basic_map
*reduce_stride_constraints(
2987 __isl_take isl_basic_map
*bmap
, int n
, __isl_keep isl_mat
*A
)
2990 isl_size total
, n_div
;
2994 total
= isl_basic_map_dim(bmap
, isl_dim_all
);
2995 n_div
= isl_basic_map_dim(bmap
, isl_dim_div
);
2996 if (total
< 0 || n_div
< 0 || !A
)
2997 return isl_basic_map_free(bmap
);
3001 for (i
= 0; i
< n
; ++i
) {
3004 div
= isl_seq_first_non_zero(bmap
->eq
[i
] + 1 + total
, n_div
);
3006 isl_die(isl_basic_map_get_ctx(bmap
), isl_error_internal
,
3007 "equality constraints modified unexpectedly",
3009 if (isl_seq_first_non_zero(bmap
->eq
[i
] + 1 + total
+ div
+ 1,
3010 n_div
- div
- 1) != -1)
3012 if (isl_mat_row_gcd(A
, i
, &gcd
) < 0)
3014 if (isl_int_is_one(gcd
))
3016 remove_incomplete_powers(&gcd
, bmap
->eq
[i
][1 + total
+ div
]);
3017 if (isl_int_is_one(gcd
))
3019 isl_int_divexact(bmap
->eq
[i
][1 + total
+ div
],
3020 bmap
->eq
[i
][1 + total
+ div
], gcd
);
3021 bmap
= isl_basic_map_mark_div_unknown(bmap
, div
);
3029 bmap
= isl_basic_map_gauss(bmap
, NULL
);
3034 isl_basic_map_free(bmap
);
3038 /* Simplify the stride constraints in "bmap" based on
3039 * the remaining equality constraints in "bmap" and all equality
3040 * constraints in "context".
3041 * Only do this if both "bmap" and "context" have stride constraints.
3043 * First extract a copy of the stride constraints in "bmap" in a compressed
3044 * space exploiting all the other equality constraints and then
3045 * use this compressed copy to simplify the original stride constraints.
3047 static __isl_give isl_basic_map
*gist_strides(__isl_take isl_basic_map
*bmap
,
3048 __isl_keep isl_basic_map
*context
)
3050 int bmap_n_eq
, context_n_eq
;
3053 if (!bmap
|| !context
)
3054 return isl_basic_map_free(bmap
);
3056 bmap_n_eq
= n_div_eq(bmap
);
3057 context_n_eq
= n_div_eq(context
);
3059 if (bmap_n_eq
< 0 || context_n_eq
< 0)
3060 return isl_basic_map_free(bmap
);
3061 if (bmap_n_eq
== 0 || context_n_eq
== 0)
3064 A
= extract_compressed_stride_constraints(bmap
, bmap_n_eq
,
3065 context
, context_n_eq
);
3066 bmap
= reduce_stride_constraints(bmap
, bmap_n_eq
, A
);
3073 /* Return a basic map that has the same intersection with "context" as "bmap"
3074 * and that is as "simple" as possible.
3076 * The core computation is performed on the pure constraints.
3077 * When we add back the meaning of the integer divisions, we need
3078 * to (re)introduce the div constraints. If we happen to have
3079 * discovered that some of these integer divisions are equal to
3080 * some affine combination of other variables, then these div
3081 * constraints may end up getting simplified in terms of the equalities,
3082 * resulting in extra inequalities on the other variables that
3083 * may have been removed already or that may not even have been
3084 * part of the input. We try and remove those constraints of
3085 * this form that are most obviously redundant with respect to
3086 * the context. We also remove those div constraints that are
3087 * redundant with respect to the other constraints in the result.
3089 * The stride constraints among the equality constraints in "bmap" are
3090 * also simplified with respecting to the other equality constraints
3091 * in "bmap" and with respect to all equality constraints in "context".
3093 __isl_give isl_basic_map
*isl_basic_map_gist(__isl_take isl_basic_map
*bmap
,
3094 __isl_take isl_basic_map
*context
)
3096 isl_basic_set
*bset
, *eq
;
3097 isl_basic_map
*eq_bmap
;
3098 isl_size total
, n_div
, n_div_bmap
;
3099 unsigned extra
, n_eq
, n_ineq
;
3101 if (!bmap
|| !context
)
3104 if (isl_basic_map_plain_is_universe(bmap
)) {
3105 isl_basic_map_free(context
);
3108 if (isl_basic_map_plain_is_empty(context
)) {
3109 isl_space
*space
= isl_basic_map_get_space(bmap
);
3110 isl_basic_map_free(bmap
);
3111 isl_basic_map_free(context
);
3112 return isl_basic_map_universe(space
);
3114 if (isl_basic_map_plain_is_empty(bmap
)) {
3115 isl_basic_map_free(context
);
3119 bmap
= isl_basic_map_remove_redundancies(bmap
);
3120 context
= isl_basic_map_remove_redundancies(context
);
3121 context
= isl_basic_map_align_divs(context
, bmap
);
3123 n_div
= isl_basic_map_dim(context
, isl_dim_div
);
3124 total
= isl_basic_map_dim(bmap
, isl_dim_all
);
3125 n_div_bmap
= isl_basic_map_dim(bmap
, isl_dim_div
);
3126 if (n_div
< 0 || total
< 0 || n_div_bmap
< 0)
3128 extra
= n_div
- n_div_bmap
;
3130 bset
= isl_basic_map_underlying_set(isl_basic_map_copy(bmap
));
3131 bset
= isl_basic_set_add_dims(bset
, isl_dim_set
, extra
);
3132 bset
= uset_gist(bset
,
3133 isl_basic_map_underlying_set(isl_basic_map_copy(context
)));
3134 bset
= isl_basic_set_project_out(bset
, isl_dim_set
, total
, extra
);
3136 if (!bset
|| bset
->n_eq
== 0 || n_div
== 0 ||
3137 isl_basic_set_plain_is_empty(bset
)) {
3138 isl_basic_map_free(context
);
3139 return isl_basic_map_overlying_set(bset
, bmap
);
3143 n_ineq
= bset
->n_ineq
;
3144 eq
= isl_basic_set_copy(bset
);
3145 eq
= isl_basic_set_cow(eq
);
3146 if (isl_basic_set_free_inequality(eq
, n_ineq
) < 0)
3147 eq
= isl_basic_set_free(eq
);
3148 if (isl_basic_set_free_equality(bset
, n_eq
) < 0)
3149 bset
= isl_basic_set_free(bset
);
3151 eq_bmap
= isl_basic_map_overlying_set(eq
, isl_basic_map_copy(bmap
));
3152 eq_bmap
= gist_strides(eq_bmap
, context
);
3153 eq_bmap
= isl_basic_map_remove_shifted_constraints(eq_bmap
, context
);
3154 bmap
= isl_basic_map_overlying_set(bset
, bmap
);
3155 bmap
= isl_basic_map_intersect(bmap
, eq_bmap
);
3156 bmap
= isl_basic_map_remove_redundancies(bmap
);
3160 isl_basic_map_free(bmap
);
3161 isl_basic_map_free(context
);
3166 * Assumes context has no implicit divs.
3168 __isl_give isl_map
*isl_map_gist_basic_map(__isl_take isl_map
*map
,
3169 __isl_take isl_basic_map
*context
)
3173 if (!map
|| !context
)
3176 if (isl_basic_map_plain_is_empty(context
)) {
3177 isl_space
*space
= isl_map_get_space(map
);
3179 isl_basic_map_free(context
);
3180 return isl_map_universe(space
);
3183 context
= isl_basic_map_remove_redundancies(context
);
3184 map
= isl_map_cow(map
);
3185 if (!map
|| !context
)
3187 isl_assert(map
->ctx
, isl_space_is_equal(map
->dim
, context
->dim
), goto error
);
3188 map
= isl_map_compute_divs(map
);
3191 for (i
= map
->n
- 1; i
>= 0; --i
) {
3192 map
->p
[i
] = isl_basic_map_gist(map
->p
[i
],
3193 isl_basic_map_copy(context
));
3196 if (isl_basic_map_plain_is_empty(map
->p
[i
])) {
3197 isl_basic_map_free(map
->p
[i
]);
3198 if (i
!= map
->n
- 1)
3199 map
->p
[i
] = map
->p
[map
->n
- 1];
3203 isl_basic_map_free(context
);
3204 ISL_F_CLR(map
, ISL_MAP_NORMALIZED
);
3208 isl_basic_map_free(context
);
3212 /* Drop all inequalities from "bmap" that also appear in "context".
3213 * "context" is assumed to have only known local variables and
3214 * the initial local variables of "bmap" are assumed to be the same
3215 * as those of "context".
3216 * The constraints of both "bmap" and "context" are assumed
3217 * to have been sorted using isl_basic_map_sort_constraints.
3219 * Run through the inequality constraints of "bmap" and "context"
3221 * If a constraint of "bmap" involves variables not in "context",
3222 * then it cannot appear in "context".
3223 * If a matching constraint is found, it is removed from "bmap".
3225 static __isl_give isl_basic_map
*drop_inequalities(
3226 __isl_take isl_basic_map
*bmap
, __isl_keep isl_basic_map
*context
)
3229 isl_size total
, bmap_total
;
3232 total
= isl_basic_map_dim(context
, isl_dim_all
);
3233 bmap_total
= isl_basic_map_dim(bmap
, isl_dim_all
);
3234 if (total
< 0 || bmap_total
< 0)
3235 return isl_basic_map_free(bmap
);
3237 extra
= bmap_total
- total
;
3239 i1
= bmap
->n_ineq
- 1;
3240 i2
= context
->n_ineq
- 1;
3241 while (bmap
&& i1
>= 0 && i2
>= 0) {
3244 if (isl_seq_first_non_zero(bmap
->ineq
[i1
] + 1 + total
,
3249 cmp
= isl_basic_map_constraint_cmp(context
, bmap
->ineq
[i1
],
3259 if (isl_int_eq(bmap
->ineq
[i1
][0], context
->ineq
[i2
][0])) {
3260 bmap
= isl_basic_map_cow(bmap
);
3261 if (isl_basic_map_drop_inequality(bmap
, i1
) < 0)
3262 bmap
= isl_basic_map_free(bmap
);
3271 /* Drop all equalities from "bmap" that also appear in "context".
3272 * "context" is assumed to have only known local variables and
3273 * the initial local variables of "bmap" are assumed to be the same
3274 * as those of "context".
3276 * Run through the equality constraints of "bmap" and "context"
3278 * If a constraint of "bmap" involves variables not in "context",
3279 * then it cannot appear in "context".
3280 * If a matching constraint is found, it is removed from "bmap".
3282 static __isl_give isl_basic_map
*drop_equalities(
3283 __isl_take isl_basic_map
*bmap
, __isl_keep isl_basic_map
*context
)
3286 isl_size total
, bmap_total
;
3289 total
= isl_basic_map_dim(context
, isl_dim_all
);
3290 bmap_total
= isl_basic_map_dim(bmap
, isl_dim_all
);
3291 if (total
< 0 || bmap_total
< 0)
3292 return isl_basic_map_free(bmap
);
3294 extra
= bmap_total
- total
;
3296 i1
= bmap
->n_eq
- 1;
3297 i2
= context
->n_eq
- 1;
3299 while (bmap
&& i1
>= 0 && i2
>= 0) {
3302 if (isl_seq_first_non_zero(bmap
->eq
[i1
] + 1 + total
,
3305 last1
= isl_seq_last_non_zero(bmap
->eq
[i1
] + 1, total
);
3306 last2
= isl_seq_last_non_zero(context
->eq
[i2
] + 1, total
);
3307 if (last1
> last2
) {
3311 if (last1
< last2
) {
3315 if (isl_seq_eq(bmap
->eq
[i1
], context
->eq
[i2
], 1 + total
)) {
3316 bmap
= isl_basic_map_cow(bmap
);
3317 if (isl_basic_map_drop_equality(bmap
, i1
) < 0)
3318 bmap
= isl_basic_map_free(bmap
);
3327 /* Remove the constraints in "context" from "bmap".
3328 * "context" is assumed to have explicit representations
3329 * for all local variables.
3331 * First align the divs of "bmap" to those of "context" and
3332 * sort the constraints. Then drop all constraints from "bmap"
3333 * that appear in "context".
3335 __isl_give isl_basic_map
*isl_basic_map_plain_gist(
3336 __isl_take isl_basic_map
*bmap
, __isl_take isl_basic_map
*context
)
3338 isl_bool done
, known
;
3340 done
= isl_basic_map_plain_is_universe(context
);
3341 if (done
== isl_bool_false
)
3342 done
= isl_basic_map_plain_is_universe(bmap
);
3343 if (done
== isl_bool_false
)
3344 done
= isl_basic_map_plain_is_empty(context
);
3345 if (done
== isl_bool_false
)
3346 done
= isl_basic_map_plain_is_empty(bmap
);
3350 isl_basic_map_free(context
);
3353 known
= isl_basic_map_divs_known(context
);
3357 isl_die(isl_basic_map_get_ctx(bmap
), isl_error_invalid
,
3358 "context has unknown divs", goto error
);
3360 bmap
= isl_basic_map_align_divs(bmap
, context
);
3361 bmap
= isl_basic_map_gauss(bmap
, NULL
);
3362 bmap
= isl_basic_map_sort_constraints(bmap
);
3363 context
= isl_basic_map_sort_constraints(context
);
3365 bmap
= drop_inequalities(bmap
, context
);
3366 bmap
= drop_equalities(bmap
, context
);
3368 isl_basic_map_free(context
);
3369 bmap
= isl_basic_map_finalize(bmap
);
3372 isl_basic_map_free(bmap
);
3373 isl_basic_map_free(context
);
3377 /* Replace "map" by the disjunct at position "pos" and free "context".
3379 static __isl_give isl_map
*replace_by_disjunct(__isl_take isl_map
*map
,
3380 int pos
, __isl_take isl_basic_map
*context
)
3382 isl_basic_map
*bmap
;
3384 bmap
= isl_basic_map_copy(map
->p
[pos
]);
3386 isl_basic_map_free(context
);
3387 return isl_map_from_basic_map(bmap
);
3390 /* Remove the constraints in "context" from "map".
3391 * If any of the disjuncts in the result turns out to be the universe,
3392 * then return this universe.
3393 * "context" is assumed to have explicit representations
3394 * for all local variables.
3396 __isl_give isl_map
*isl_map_plain_gist_basic_map(__isl_take isl_map
*map
,
3397 __isl_take isl_basic_map
*context
)
3400 isl_bool univ
, known
;
3402 univ
= isl_basic_map_plain_is_universe(context
);
3406 isl_basic_map_free(context
);
3409 known
= isl_basic_map_divs_known(context
);
3413 isl_die(isl_map_get_ctx(map
), isl_error_invalid
,
3414 "context has unknown divs", goto error
);
3416 map
= isl_map_cow(map
);
3419 for (i
= 0; i
< map
->n
; ++i
) {
3420 map
->p
[i
] = isl_basic_map_plain_gist(map
->p
[i
],
3421 isl_basic_map_copy(context
));
3422 univ
= isl_basic_map_plain_is_universe(map
->p
[i
]);
3425 if (univ
&& map
->n
> 1)
3426 return replace_by_disjunct(map
, i
, context
);
3429 isl_basic_map_free(context
);
3430 ISL_F_CLR(map
, ISL_MAP_NORMALIZED
);
3432 ISL_F_CLR(map
, ISL_MAP_DISJOINT
);
3436 isl_basic_map_free(context
);
3440 /* Remove the constraints in "context" from "set".
3441 * If any of the disjuncts in the result turns out to be the universe,
3442 * then return this universe.
3443 * "context" is assumed to have explicit representations
3444 * for all local variables.
3446 __isl_give isl_set
*isl_set_plain_gist_basic_set(__isl_take isl_set
*set
,
3447 __isl_take isl_basic_set
*context
)
3449 return set_from_map(isl_map_plain_gist_basic_map(set_to_map(set
),
3450 bset_to_bmap(context
)));
3453 /* Remove the constraints in "context" from "map".
3454 * If any of the disjuncts in the result turns out to be the universe,
3455 * then return this universe.
3456 * "context" is assumed to consist of a single disjunct and
3457 * to have explicit representations for all local variables.
3459 __isl_give isl_map
*isl_map_plain_gist(__isl_take isl_map
*map
,
3460 __isl_take isl_map
*context
)
3462 isl_basic_map
*hull
;
3464 hull
= isl_map_unshifted_simple_hull(context
);
3465 return isl_map_plain_gist_basic_map(map
, hull
);
3468 /* Replace "map" by a universe map in the same space and free "drop".
3470 static __isl_give isl_map
*replace_by_universe(__isl_take isl_map
*map
,
3471 __isl_take isl_map
*drop
)
3475 res
= isl_map_universe(isl_map_get_space(map
));
3481 /* Return a map that has the same intersection with "context" as "map"
3482 * and that is as "simple" as possible.
3484 * If "map" is already the universe, then we cannot make it any simpler.
3485 * Similarly, if "context" is the universe, then we cannot exploit it
3487 * If "map" and "context" are identical to each other, then we can
3488 * return the corresponding universe.
3490 * If either "map" or "context" consists of multiple disjuncts,
3491 * then check if "context" happens to be a subset of "map",
3492 * in which case all constraints can be removed.
3493 * In case of multiple disjuncts, the standard procedure
3494 * may not be able to detect that all constraints can be removed.
3496 * If none of these cases apply, we have to work a bit harder.
3497 * During this computation, we make use of a single disjunct context,
3498 * so if the original context consists of more than one disjunct
3499 * then we need to approximate the context by a single disjunct set.
3500 * Simply taking the simple hull may drop constraints that are
3501 * only implicitly available in each disjunct. We therefore also
3502 * look for constraints among those defining "map" that are valid
3503 * for the context. These can then be used to simplify away
3504 * the corresponding constraints in "map".
3506 static __isl_give isl_map
*map_gist(__isl_take isl_map
*map
,
3507 __isl_take isl_map
*context
)
3511 int single_disjunct_map
, single_disjunct_context
;
3513 isl_basic_map
*hull
;
3515 is_universe
= isl_map_plain_is_universe(map
);
3516 if (is_universe
>= 0 && !is_universe
)
3517 is_universe
= isl_map_plain_is_universe(context
);
3518 if (is_universe
< 0)
3521 isl_map_free(context
);
3525 equal
= isl_map_plain_is_equal(map
, context
);
3529 return replace_by_universe(map
, context
);
3531 single_disjunct_map
= isl_map_n_basic_map(map
) == 1;
3532 single_disjunct_context
= isl_map_n_basic_map(context
) == 1;
3533 if (!single_disjunct_map
|| !single_disjunct_context
) {
3534 subset
= isl_map_is_subset(context
, map
);
3538 return replace_by_universe(map
, context
);
3541 context
= isl_map_compute_divs(context
);
3544 if (single_disjunct_context
) {
3545 hull
= isl_map_simple_hull(context
);
3550 ctx
= isl_map_get_ctx(map
);
3551 list
= isl_map_list_alloc(ctx
, 2);
3552 list
= isl_map_list_add(list
, isl_map_copy(context
));
3553 list
= isl_map_list_add(list
, isl_map_copy(map
));
3554 hull
= isl_map_unshifted_simple_hull_from_map_list(context
,
3557 return isl_map_gist_basic_map(map
, hull
);
3560 isl_map_free(context
);
3564 __isl_give isl_map
*isl_map_gist(__isl_take isl_map
*map
,
3565 __isl_take isl_map
*context
)
3567 return isl_map_align_params_map_map_and(map
, context
, &map_gist
);
3570 struct isl_basic_set
*isl_basic_set_gist(struct isl_basic_set
*bset
,
3571 struct isl_basic_set
*context
)
3573 return bset_from_bmap(isl_basic_map_gist(bset_to_bmap(bset
),
3574 bset_to_bmap(context
)));
3577 __isl_give isl_set
*isl_set_gist_basic_set(__isl_take isl_set
*set
,
3578 __isl_take isl_basic_set
*context
)
3580 return set_from_map(isl_map_gist_basic_map(set_to_map(set
),
3581 bset_to_bmap(context
)));
3584 __isl_give isl_set
*isl_set_gist_params_basic_set(__isl_take isl_set
*set
,
3585 __isl_take isl_basic_set
*context
)
3587 isl_space
*space
= isl_set_get_space(set
);
3588 isl_basic_set
*dom_context
= isl_basic_set_universe(space
);
3589 dom_context
= isl_basic_set_intersect_params(dom_context
, context
);
3590 return isl_set_gist_basic_set(set
, dom_context
);
3593 __isl_give isl_set
*isl_set_gist(__isl_take isl_set
*set
,
3594 __isl_take isl_set
*context
)
3596 return set_from_map(isl_map_gist(set_to_map(set
), set_to_map(context
)));
3599 /* Compute the gist of "bmap" with respect to the constraints "context"
3602 __isl_give isl_basic_map
*isl_basic_map_gist_domain(
3603 __isl_take isl_basic_map
*bmap
, __isl_take isl_basic_set
*context
)
3605 isl_space
*space
= isl_basic_map_get_space(bmap
);
3606 isl_basic_map
*bmap_context
= isl_basic_map_universe(space
);
3608 bmap_context
= isl_basic_map_intersect_domain(bmap_context
, context
);
3609 return isl_basic_map_gist(bmap
, bmap_context
);
3612 __isl_give isl_map
*isl_map_gist_domain(__isl_take isl_map
*map
,
3613 __isl_take isl_set
*context
)
3615 isl_map
*map_context
= isl_map_universe(isl_map_get_space(map
));
3616 map_context
= isl_map_intersect_domain(map_context
, context
);
3617 return isl_map_gist(map
, map_context
);
3620 __isl_give isl_map
*isl_map_gist_range(__isl_take isl_map
*map
,
3621 __isl_take isl_set
*context
)
3623 isl_map
*map_context
= isl_map_universe(isl_map_get_space(map
));
3624 map_context
= isl_map_intersect_range(map_context
, context
);
3625 return isl_map_gist(map
, map_context
);
3628 __isl_give isl_map
*isl_map_gist_params(__isl_take isl_map
*map
,
3629 __isl_take isl_set
*context
)
3631 isl_map
*map_context
= isl_map_universe(isl_map_get_space(map
));
3632 map_context
= isl_map_intersect_params(map_context
, context
);
3633 return isl_map_gist(map
, map_context
);
3636 __isl_give isl_set
*isl_set_gist_params(__isl_take isl_set
*set
,
3637 __isl_take isl_set
*context
)
3639 return isl_map_gist_params(set
, context
);
3642 /* Quick check to see if two basic maps are disjoint.
3643 * In particular, we reduce the equalities and inequalities of
3644 * one basic map in the context of the equalities of the other
3645 * basic map and check if we get a contradiction.
3647 isl_bool
isl_basic_map_plain_is_disjoint(__isl_keep isl_basic_map
*bmap1
,
3648 __isl_keep isl_basic_map
*bmap2
)
3650 struct isl_vec
*v
= NULL
;
3655 if (!bmap1
|| !bmap2
)
3656 return isl_bool_error
;
3657 isl_assert(bmap1
->ctx
, isl_space_is_equal(bmap1
->dim
, bmap2
->dim
),
3658 return isl_bool_error
);
3659 if (bmap1
->n_div
|| bmap2
->n_div
)
3660 return isl_bool_false
;
3661 if (!bmap1
->n_eq
&& !bmap2
->n_eq
)
3662 return isl_bool_false
;
3664 total
= isl_space_dim(bmap1
->dim
, isl_dim_all
);
3666 return isl_bool_error
;
3668 return isl_bool_false
;
3669 v
= isl_vec_alloc(bmap1
->ctx
, 1 + total
);
3672 elim
= isl_alloc_array(bmap1
->ctx
, int, total
);
3675 compute_elimination_index(bmap1
, elim
);
3676 for (i
= 0; i
< bmap2
->n_eq
; ++i
) {
3678 reduced
= reduced_using_equalities(v
->block
.data
, bmap2
->eq
[i
],
3680 if (reduced
&& !isl_int_is_zero(v
->block
.data
[0]) &&
3681 isl_seq_first_non_zero(v
->block
.data
+ 1, total
) == -1)
3684 for (i
= 0; i
< bmap2
->n_ineq
; ++i
) {
3686 reduced
= reduced_using_equalities(v
->block
.data
,
3687 bmap2
->ineq
[i
], bmap1
, elim
);
3688 if (reduced
&& isl_int_is_neg(v
->block
.data
[0]) &&
3689 isl_seq_first_non_zero(v
->block
.data
+ 1, total
) == -1)
3692 compute_elimination_index(bmap2
, elim
);
3693 for (i
= 0; i
< bmap1
->n_ineq
; ++i
) {
3695 reduced
= reduced_using_equalities(v
->block
.data
,
3696 bmap1
->ineq
[i
], bmap2
, elim
);
3697 if (reduced
&& isl_int_is_neg(v
->block
.data
[0]) &&
3698 isl_seq_first_non_zero(v
->block
.data
+ 1, total
) == -1)
3703 return isl_bool_false
;
3707 return isl_bool_true
;
3711 return isl_bool_error
;
3714 int isl_basic_set_plain_is_disjoint(__isl_keep isl_basic_set
*bset1
,
3715 __isl_keep isl_basic_set
*bset2
)
3717 return isl_basic_map_plain_is_disjoint(bset_to_bmap(bset1
),
3718 bset_to_bmap(bset2
));
3721 /* Does "test" hold for all pairs of basic maps in "map1" and "map2"?
3723 static isl_bool
all_pairs(__isl_keep isl_map
*map1
, __isl_keep isl_map
*map2
,
3724 isl_bool (*test
)(__isl_keep isl_basic_map
*bmap1
,
3725 __isl_keep isl_basic_map
*bmap2
))
3730 return isl_bool_error
;
3732 for (i
= 0; i
< map1
->n
; ++i
) {
3733 for (j
= 0; j
< map2
->n
; ++j
) {
3734 isl_bool d
= test(map1
->p
[i
], map2
->p
[j
]);
3735 if (d
!= isl_bool_true
)
3740 return isl_bool_true
;
3743 /* Are "map1" and "map2" obviously disjoint, based on information
3744 * that can be derived without looking at the individual basic maps?
3746 * In particular, if one of them is empty or if they live in different spaces
3747 * (ignoring parameters), then they are clearly disjoint.
3749 static isl_bool
isl_map_plain_is_disjoint_global(__isl_keep isl_map
*map1
,
3750 __isl_keep isl_map
*map2
)
3756 return isl_bool_error
;
3758 disjoint
= isl_map_plain_is_empty(map1
);
3759 if (disjoint
< 0 || disjoint
)
3762 disjoint
= isl_map_plain_is_empty(map2
);
3763 if (disjoint
< 0 || disjoint
)
3766 match
= isl_space_tuple_is_equal(map1
->dim
, isl_dim_in
,
3767 map2
->dim
, isl_dim_in
);
3768 if (match
< 0 || !match
)
3769 return match
< 0 ? isl_bool_error
: isl_bool_true
;
3771 match
= isl_space_tuple_is_equal(map1
->dim
, isl_dim_out
,
3772 map2
->dim
, isl_dim_out
);
3773 if (match
< 0 || !match
)
3774 return match
< 0 ? isl_bool_error
: isl_bool_true
;
3776 return isl_bool_false
;
3779 /* Are "map1" and "map2" obviously disjoint?
3781 * If one of them is empty or if they live in different spaces (ignoring
3782 * parameters), then they are clearly disjoint.
3783 * This is checked by isl_map_plain_is_disjoint_global.
3785 * If they have different parameters, then we skip any further tests.
3787 * If they are obviously equal, but not obviously empty, then we will
3788 * not be able to detect if they are disjoint.
3790 * Otherwise we check if each basic map in "map1" is obviously disjoint
3791 * from each basic map in "map2".
3793 isl_bool
isl_map_plain_is_disjoint(__isl_keep isl_map
*map1
,
3794 __isl_keep isl_map
*map2
)
3800 disjoint
= isl_map_plain_is_disjoint_global(map1
, map2
);
3801 if (disjoint
< 0 || disjoint
)
3804 match
= isl_map_has_equal_params(map1
, map2
);
3805 if (match
< 0 || !match
)
3806 return match
< 0 ? isl_bool_error
: isl_bool_false
;
3808 intersect
= isl_map_plain_is_equal(map1
, map2
);
3809 if (intersect
< 0 || intersect
)
3810 return intersect
< 0 ? isl_bool_error
: isl_bool_false
;
3812 return all_pairs(map1
, map2
, &isl_basic_map_plain_is_disjoint
);
3815 /* Are "map1" and "map2" disjoint?
3816 * The parameters are assumed to have been aligned.
3818 * In particular, check whether all pairs of basic maps are disjoint.
3820 static isl_bool
isl_map_is_disjoint_aligned(__isl_keep isl_map
*map1
,
3821 __isl_keep isl_map
*map2
)
3823 return all_pairs(map1
, map2
, &isl_basic_map_is_disjoint
);
3826 /* Are "map1" and "map2" disjoint?
3828 * They are disjoint if they are "obviously disjoint" or if one of them
3829 * is empty. Otherwise, they are not disjoint if one of them is universal.
3830 * If the two inputs are (obviously) equal and not empty, then they are
3832 * If none of these cases apply, then check if all pairs of basic maps
3833 * are disjoint after aligning the parameters.
3835 isl_bool
isl_map_is_disjoint(__isl_keep isl_map
*map1
, __isl_keep isl_map
*map2
)
3840 disjoint
= isl_map_plain_is_disjoint_global(map1
, map2
);
3841 if (disjoint
< 0 || disjoint
)
3844 disjoint
= isl_map_is_empty(map1
);
3845 if (disjoint
< 0 || disjoint
)
3848 disjoint
= isl_map_is_empty(map2
);
3849 if (disjoint
< 0 || disjoint
)
3852 intersect
= isl_map_plain_is_universe(map1
);
3853 if (intersect
< 0 || intersect
)
3854 return intersect
< 0 ? isl_bool_error
: isl_bool_false
;
3856 intersect
= isl_map_plain_is_universe(map2
);
3857 if (intersect
< 0 || intersect
)
3858 return intersect
< 0 ? isl_bool_error
: isl_bool_false
;
3860 intersect
= isl_map_plain_is_equal(map1
, map2
);
3861 if (intersect
< 0 || intersect
)
3862 return isl_bool_not(intersect
);
3864 return isl_map_align_params_map_map_and_test(map1
, map2
,
3865 &isl_map_is_disjoint_aligned
);
3868 /* Are "bmap1" and "bmap2" disjoint?
3870 * They are disjoint if they are "obviously disjoint" or if one of them
3871 * is empty. Otherwise, they are not disjoint if one of them is universal.
3872 * If none of these cases apply, we compute the intersection and see if
3873 * the result is empty.
3875 isl_bool
isl_basic_map_is_disjoint(__isl_keep isl_basic_map
*bmap1
,
3876 __isl_keep isl_basic_map
*bmap2
)
3880 isl_basic_map
*test
;
3882 disjoint
= isl_basic_map_plain_is_disjoint(bmap1
, bmap2
);
3883 if (disjoint
< 0 || disjoint
)
3886 disjoint
= isl_basic_map_is_empty(bmap1
);
3887 if (disjoint
< 0 || disjoint
)
3890 disjoint
= isl_basic_map_is_empty(bmap2
);
3891 if (disjoint
< 0 || disjoint
)
3894 intersect
= isl_basic_map_plain_is_universe(bmap1
);
3895 if (intersect
< 0 || intersect
)
3896 return intersect
< 0 ? isl_bool_error
: isl_bool_false
;
3898 intersect
= isl_basic_map_plain_is_universe(bmap2
);
3899 if (intersect
< 0 || intersect
)
3900 return intersect
< 0 ? isl_bool_error
: isl_bool_false
;
3902 test
= isl_basic_map_intersect(isl_basic_map_copy(bmap1
),
3903 isl_basic_map_copy(bmap2
));
3904 disjoint
= isl_basic_map_is_empty(test
);
3905 isl_basic_map_free(test
);
3910 /* Are "bset1" and "bset2" disjoint?
3912 isl_bool
isl_basic_set_is_disjoint(__isl_keep isl_basic_set
*bset1
,
3913 __isl_keep isl_basic_set
*bset2
)
3915 return isl_basic_map_is_disjoint(bset1
, bset2
);
3918 isl_bool
isl_set_plain_is_disjoint(__isl_keep isl_set
*set1
,
3919 __isl_keep isl_set
*set2
)
3921 return isl_map_plain_is_disjoint(set_to_map(set1
), set_to_map(set2
));
3924 /* Are "set1" and "set2" disjoint?
3926 isl_bool
isl_set_is_disjoint(__isl_keep isl_set
*set1
, __isl_keep isl_set
*set2
)
3928 return isl_map_is_disjoint(set1
, set2
);
3931 /* Is "v" equal to 0, 1 or -1?
3933 static int is_zero_or_one(isl_int v
)
3935 return isl_int_is_zero(v
) || isl_int_is_one(v
) || isl_int_is_negone(v
);
3938 /* Are the "n" coefficients starting at "first" of inequality constraints
3939 * "i" and "j" of "bmap" opposite to each other?
3941 static int is_opposite_part(__isl_keep isl_basic_map
*bmap
, int i
, int j
,
3944 return isl_seq_is_neg(bmap
->ineq
[i
] + first
, bmap
->ineq
[j
] + first
, n
);
3947 /* Are inequality constraints "i" and "j" of "bmap" opposite to each other,
3948 * apart from the constant term?
3950 static isl_bool
is_opposite(__isl_keep isl_basic_map
*bmap
, int i
, int j
)
3954 total
= isl_basic_map_dim(bmap
, isl_dim_all
);
3956 return isl_bool_error
;
3957 return is_opposite_part(bmap
, i
, j
, 1, total
);
3960 /* Check if we can combine a given div with lower bound l and upper
3961 * bound u with some other div and if so return that other div.
3962 * Otherwise, return a position beyond the integer divisions.
3963 * Return -1 on error.
3965 * We first check that
3966 * - the bounds are opposites of each other (except for the constant
3968 * - the bounds do not reference any other div
3969 * - no div is defined in terms of this div
3971 * Let m be the size of the range allowed on the div by the bounds.
3972 * That is, the bounds are of the form
3974 * e <= a <= e + m - 1
3976 * with e some expression in the other variables.
3977 * We look for another div b such that no third div is defined in terms
3978 * of this second div b and such that in any constraint that contains
3979 * a (except for the given lower and upper bound), also contains b
3980 * with a coefficient that is m times that of b.
3981 * That is, all constraints (except for the lower and upper bound)
3984 * e + f (a + m b) >= 0
3986 * Furthermore, in the constraints that only contain b, the coefficient
3987 * of b should be equal to 1 or -1.
3988 * If so, we return b so that "a + m b" can be replaced by
3989 * a single div "c = a + m b".
3991 static int div_find_coalesce(__isl_keep isl_basic_map
*bmap
, int *pairs
,
3992 unsigned div
, unsigned l
, unsigned u
)
4000 n_div
= isl_basic_map_dim(bmap
, isl_dim_div
);
4003 v_div
= isl_basic_map_var_offset(bmap
, isl_dim_div
);
4006 if (isl_seq_first_non_zero(bmap
->ineq
[l
] + 1 + v_div
, div
) != -1)
4008 if (isl_seq_first_non_zero(bmap
->ineq
[l
] + 1 + v_div
+ div
+ 1,
4009 n_div
- div
- 1) != -1)
4011 opp
= is_opposite(bmap
, l
, u
);
4012 if (opp
< 0 || !opp
)
4013 return opp
< 0 ? -1 : n_div
;
4015 for (i
= 0; i
< n_div
; ++i
) {
4016 if (isl_int_is_zero(bmap
->div
[i
][0]))
4018 if (!isl_int_is_zero(bmap
->div
[i
][1 + 1 + v_div
+ div
]))
4022 isl_int_add(bmap
->ineq
[l
][0], bmap
->ineq
[l
][0], bmap
->ineq
[u
][0]);
4023 if (isl_int_is_neg(bmap
->ineq
[l
][0])) {
4024 isl_int_sub(bmap
->ineq
[l
][0],
4025 bmap
->ineq
[l
][0], bmap
->ineq
[u
][0]);
4026 bmap
= isl_basic_map_copy(bmap
);
4027 bmap
= isl_basic_map_set_to_empty(bmap
);
4028 isl_basic_map_free(bmap
);
4031 isl_int_add_ui(bmap
->ineq
[l
][0], bmap
->ineq
[l
][0], 1);
4033 for (i
= 0; i
< n_div
; ++i
) {
4038 for (j
= 0; j
< n_div
; ++j
) {
4039 if (isl_int_is_zero(bmap
->div
[j
][0]))
4041 if (!isl_int_is_zero(bmap
->div
[j
][1 + 1 + v_div
+ i
]))
4046 for (j
= 0; j
< bmap
->n_ineq
; ++j
) {
4048 if (j
== l
|| j
== u
)
4050 if (isl_int_is_zero(bmap
->ineq
[j
][1 + v_div
+ div
])) {
4051 if (is_zero_or_one(bmap
->ineq
[j
][1 + v_div
+ i
]))
4055 if (isl_int_is_zero(bmap
->ineq
[j
][1 + v_div
+ i
]))
4057 isl_int_mul(bmap
->ineq
[j
][1 + v_div
+ div
],
4058 bmap
->ineq
[j
][1 + v_div
+ div
],
4060 valid
= isl_int_eq(bmap
->ineq
[j
][1 + v_div
+ div
],
4061 bmap
->ineq
[j
][1 + v_div
+ i
]);
4062 isl_int_divexact(bmap
->ineq
[j
][1 + v_div
+ div
],
4063 bmap
->ineq
[j
][1 + v_div
+ div
],
4068 if (j
< bmap
->n_ineq
)
4073 isl_int_sub_ui(bmap
->ineq
[l
][0], bmap
->ineq
[l
][0], 1);
4074 isl_int_sub(bmap
->ineq
[l
][0], bmap
->ineq
[l
][0], bmap
->ineq
[u
][0]);
4078 /* Internal data structure used during the construction and/or evaluation of
4079 * an inequality that ensures that a pair of bounds always allows
4080 * for an integer value.
4082 * "tab" is the tableau in which the inequality is evaluated. It may
4083 * be NULL until it is actually needed.
4084 * "v" contains the inequality coefficients.
4085 * "g", "fl" and "fu" are temporary scalars used during the construction and
4088 struct test_ineq_data
{
4089 struct isl_tab
*tab
;
4096 /* Free all the memory allocated by the fields of "data".
4098 static void test_ineq_data_clear(struct test_ineq_data
*data
)
4100 isl_tab_free(data
->tab
);
4101 isl_vec_free(data
->v
);
4102 isl_int_clear(data
->g
);
4103 isl_int_clear(data
->fl
);
4104 isl_int_clear(data
->fu
);
4107 /* Is the inequality stored in data->v satisfied by "bmap"?
4108 * That is, does it only attain non-negative values?
4109 * data->tab is a tableau corresponding to "bmap".
4111 static isl_bool
test_ineq_is_satisfied(__isl_keep isl_basic_map
*bmap
,
4112 struct test_ineq_data
*data
)
4115 enum isl_lp_result res
;
4117 ctx
= isl_basic_map_get_ctx(bmap
);
4119 data
->tab
= isl_tab_from_basic_map(bmap
, 0);
4120 res
= isl_tab_min(data
->tab
, data
->v
->el
, ctx
->one
, &data
->g
, NULL
, 0);
4121 if (res
== isl_lp_error
)
4122 return isl_bool_error
;
4123 return res
== isl_lp_ok
&& isl_int_is_nonneg(data
->g
);
4126 /* Given a lower and an upper bound on div i, do they always allow
4127 * for an integer value of the given div?
4128 * Determine this property by constructing an inequality
4129 * such that the property is guaranteed when the inequality is nonnegative.
4130 * The lower bound is inequality l, while the upper bound is inequality u.
4131 * The constructed inequality is stored in data->v.
4133 * Let the upper bound be
4137 * and the lower bound
4141 * Let n_u = f_u g and n_l = f_l g, with g = gcd(n_u, n_l).
4144 * - f_u e_l <= f_u f_l g a <= f_l e_u
4146 * Since all variables are integer valued, this is equivalent to
4148 * - f_u e_l - (f_u - 1) <= f_u f_l g a <= f_l e_u + (f_l - 1)
4150 * If this interval is at least f_u f_l g, then it contains at least
4151 * one integer value for a.
4152 * That is, the test constraint is
4154 * f_l e_u + f_u e_l + f_l - 1 + f_u - 1 + 1 >= f_u f_l g
4158 * f_l e_u + f_u e_l + f_l - 1 + f_u - 1 + 1 - f_u f_l g >= 0
4160 * If the coefficients of f_l e_u + f_u e_l have a common divisor g',
4161 * then the constraint can be scaled down by a factor g',
4162 * with the constant term replaced by
4163 * floor((f_l e_{u,0} + f_u e_{l,0} + f_l - 1 + f_u - 1 + 1 - f_u f_l g)/g').
4164 * Note that the result of applying Fourier-Motzkin to this pair
4167 * f_l e_u + f_u e_l >= 0
4169 * If the constant term of the scaled down version of this constraint,
4170 * i.e., floor((f_l e_{u,0} + f_u e_{l,0})/g') is equal to the constant
4171 * term of the scaled down test constraint, then the test constraint
4172 * is known to hold and no explicit evaluation is required.
4173 * This is essentially the Omega test.
4175 * If the test constraint consists of only a constant term, then
4176 * it is sufficient to look at the sign of this constant term.
4178 static isl_bool
int_between_bounds(__isl_keep isl_basic_map
*bmap
, int i
,
4179 int l
, int u
, struct test_ineq_data
*data
)
4184 offset
= isl_basic_map_offset(bmap
, isl_dim_div
);
4185 n_div
= isl_basic_map_dim(bmap
, isl_dim_div
);
4187 return isl_bool_error
;
4189 isl_int_gcd(data
->g
,
4190 bmap
->ineq
[l
][offset
+ i
], bmap
->ineq
[u
][offset
+ i
]);
4191 isl_int_divexact(data
->fl
, bmap
->ineq
[l
][offset
+ i
], data
->g
);
4192 isl_int_divexact(data
->fu
, bmap
->ineq
[u
][offset
+ i
], data
->g
);
4193 isl_int_neg(data
->fu
, data
->fu
);
4194 isl_seq_combine(data
->v
->el
, data
->fl
, bmap
->ineq
[u
],
4195 data
->fu
, bmap
->ineq
[l
], offset
+ n_div
);
4196 isl_int_mul(data
->g
, data
->g
, data
->fl
);
4197 isl_int_mul(data
->g
, data
->g
, data
->fu
);
4198 isl_int_sub(data
->g
, data
->g
, data
->fl
);
4199 isl_int_sub(data
->g
, data
->g
, data
->fu
);
4200 isl_int_add_ui(data
->g
, data
->g
, 1);
4201 isl_int_sub(data
->fl
, data
->v
->el
[0], data
->g
);
4203 isl_seq_gcd(data
->v
->el
+ 1, offset
- 1 + n_div
, &data
->g
);
4204 if (isl_int_is_zero(data
->g
))
4205 return isl_int_is_nonneg(data
->fl
);
4206 if (isl_int_is_one(data
->g
)) {
4207 isl_int_set(data
->v
->el
[0], data
->fl
);
4208 return test_ineq_is_satisfied(bmap
, data
);
4210 isl_int_fdiv_q(data
->fl
, data
->fl
, data
->g
);
4211 isl_int_fdiv_q(data
->v
->el
[0], data
->v
->el
[0], data
->g
);
4212 if (isl_int_eq(data
->fl
, data
->v
->el
[0]))
4213 return isl_bool_true
;
4214 isl_int_set(data
->v
->el
[0], data
->fl
);
4215 isl_seq_scale_down(data
->v
->el
+ 1, data
->v
->el
+ 1, data
->g
,
4216 offset
- 1 + n_div
);
4218 return test_ineq_is_satisfied(bmap
, data
);
4221 /* Remove more kinds of divs that are not strictly needed.
4222 * In particular, if all pairs of lower and upper bounds on a div
4223 * are such that they allow at least one integer value of the div,
4224 * then we can eliminate the div using Fourier-Motzkin without
4225 * introducing any spurious solutions.
4227 * If at least one of the two constraints has a unit coefficient for the div,
4228 * then the presence of such a value is guaranteed so there is no need to check.
4229 * In particular, the value attained by the bound with unit coefficient
4230 * can serve as this intermediate value.
4232 static __isl_give isl_basic_map
*drop_more_redundant_divs(
4233 __isl_take isl_basic_map
*bmap
, __isl_take
int *pairs
, int n
)
4236 struct test_ineq_data data
= { NULL
, NULL
};
4241 isl_int_init(data
.g
);
4242 isl_int_init(data
.fl
);
4243 isl_int_init(data
.fu
);
4245 n_div
= isl_basic_map_dim(bmap
, isl_dim_div
);
4249 ctx
= isl_basic_map_get_ctx(bmap
);
4250 off
= isl_basic_map_offset(bmap
, isl_dim_div
);
4251 data
.v
= isl_vec_alloc(ctx
, off
+ n_div
);
4260 for (i
= 0; i
< n_div
; ++i
) {
4263 if (best
>= 0 && pairs
[best
] <= pairs
[i
])
4269 for (l
= 0; l
< bmap
->n_ineq
; ++l
) {
4270 if (!isl_int_is_pos(bmap
->ineq
[l
][off
+ i
]))
4272 if (isl_int_is_one(bmap
->ineq
[l
][off
+ i
]))
4274 for (u
= 0; u
< bmap
->n_ineq
; ++u
) {
4275 if (!isl_int_is_neg(bmap
->ineq
[u
][off
+ i
]))
4277 if (isl_int_is_negone(bmap
->ineq
[u
][off
+ i
]))
4279 has_int
= int_between_bounds(bmap
, i
, l
, u
,
4283 if (data
.tab
&& data
.tab
->empty
)
4288 if (u
< bmap
->n_ineq
)
4291 if (data
.tab
&& data
.tab
->empty
) {
4292 bmap
= isl_basic_map_set_to_empty(bmap
);
4295 if (l
== bmap
->n_ineq
) {
4303 test_ineq_data_clear(&data
);
4310 bmap
= isl_basic_map_remove_dims(bmap
, isl_dim_div
, remove
, 1);
4311 return isl_basic_map_drop_redundant_divs(bmap
);
4314 isl_basic_map_free(bmap
);
4315 test_ineq_data_clear(&data
);
4319 /* Given a pair of divs div1 and div2 such that, except for the lower bound l
4320 * and the upper bound u, div1 always occurs together with div2 in the form
4321 * (div1 + m div2), where m is the constant range on the variable div1
4322 * allowed by l and u, replace the pair div1 and div2 by a single
4323 * div that is equal to div1 + m div2.
4325 * The new div will appear in the location that contains div2.
4326 * We need to modify all constraints that contain
4327 * div2 = (div - div1) / m
4328 * The coefficient of div2 is known to be equal to 1 or -1.
4329 * (If a constraint does not contain div2, it will also not contain div1.)
4330 * If the constraint also contains div1, then we know they appear
4331 * as f (div1 + m div2) and we can simply replace (div1 + m div2) by div,
4332 * i.e., the coefficient of div is f.
4334 * Otherwise, we first need to introduce div1 into the constraint.
4343 * A lower bound on div2
4347 * can be replaced by
4349 * m div2 + div1 + m t + f >= 0
4355 * can be replaced by
4357 * -(m div2 + div1) + m t + f' >= 0
4359 * These constraint are those that we would obtain from eliminating
4360 * div1 using Fourier-Motzkin.
4362 * After all constraints have been modified, we drop the lower and upper
4363 * bound and then drop div1.
4364 * Since the new div is only placed in the same location that used
4365 * to store div2, but otherwise has a different meaning, any possible
4366 * explicit representation of the original div2 is removed.
4368 static __isl_give isl_basic_map
*coalesce_divs(__isl_take isl_basic_map
*bmap
,
4369 unsigned div1
, unsigned div2
, unsigned l
, unsigned u
)
4377 ctx
= isl_basic_map_get_ctx(bmap
);
4379 v_div
= isl_basic_map_var_offset(bmap
, isl_dim_div
);
4381 return isl_basic_map_free(bmap
);
4382 total
= 1 + v_div
+ bmap
->n_div
;
4385 isl_int_add(m
, bmap
->ineq
[l
][0], bmap
->ineq
[u
][0]);
4386 isl_int_add_ui(m
, m
, 1);
4388 for (i
= 0; i
< bmap
->n_ineq
; ++i
) {
4389 if (i
== l
|| i
== u
)
4391 if (isl_int_is_zero(bmap
->ineq
[i
][1 + v_div
+ div2
]))
4393 if (isl_int_is_zero(bmap
->ineq
[i
][1 + v_div
+ div1
])) {
4394 if (isl_int_is_pos(bmap
->ineq
[i
][1 + v_div
+ div2
]))
4395 isl_seq_combine(bmap
->ineq
[i
], m
, bmap
->ineq
[i
],
4396 ctx
->one
, bmap
->ineq
[l
], total
);
4398 isl_seq_combine(bmap
->ineq
[i
], m
, bmap
->ineq
[i
],
4399 ctx
->one
, bmap
->ineq
[u
], total
);
4401 isl_int_set(bmap
->ineq
[i
][1 + v_div
+ div2
],
4402 bmap
->ineq
[i
][1 + v_div
+ div1
]);
4403 isl_int_set_si(bmap
->ineq
[i
][1 + v_div
+ div1
], 0);
4408 isl_basic_map_drop_inequality(bmap
, l
);
4409 isl_basic_map_drop_inequality(bmap
, u
);
4411 isl_basic_map_drop_inequality(bmap
, u
);
4412 isl_basic_map_drop_inequality(bmap
, l
);
4414 bmap
= isl_basic_map_mark_div_unknown(bmap
, div2
);
4415 bmap
= isl_basic_map_drop_div(bmap
, div1
);
4419 /* First check if we can coalesce any pair of divs and
4420 * then continue with dropping more redundant divs.
4422 * We loop over all pairs of lower and upper bounds on a div
4423 * with coefficient 1 and -1, respectively, check if there
4424 * is any other div "c" with which we can coalesce the div
4425 * and if so, perform the coalescing.
4427 static __isl_give isl_basic_map
*coalesce_or_drop_more_redundant_divs(
4428 __isl_take isl_basic_map
*bmap
, int *pairs
, int n
)
4434 v_div
= isl_basic_map_var_offset(bmap
, isl_dim_div
);
4435 n_div
= isl_basic_map_dim(bmap
, isl_dim_div
);
4436 if (v_div
< 0 || n_div
< 0)
4437 return isl_basic_map_free(bmap
);
4439 for (i
= 0; i
< n_div
; ++i
) {
4442 for (l
= 0; l
< bmap
->n_ineq
; ++l
) {
4443 if (!isl_int_is_one(bmap
->ineq
[l
][1 + v_div
+ i
]))
4445 for (u
= 0; u
< bmap
->n_ineq
; ++u
) {
4448 if (!isl_int_is_negone(bmap
->ineq
[u
][1+v_div
+i
]))
4450 c
= div_find_coalesce(bmap
, pairs
, i
, l
, u
);
4456 bmap
= coalesce_divs(bmap
, i
, c
, l
, u
);
4457 return isl_basic_map_drop_redundant_divs(bmap
);
4462 if (ISL_F_ISSET(bmap
, ISL_BASIC_MAP_EMPTY
)) {
4467 return drop_more_redundant_divs(bmap
, pairs
, n
);
4470 isl_basic_map_free(bmap
);
4474 /* Are the "n" coefficients starting at "first" of inequality constraints
4475 * "i" and "j" of "bmap" equal to each other?
4477 static int is_parallel_part(__isl_keep isl_basic_map
*bmap
, int i
, int j
,
4480 return isl_seq_eq(bmap
->ineq
[i
] + first
, bmap
->ineq
[j
] + first
, n
);
4483 /* Are inequality constraints "i" and "j" of "bmap" equal to each other,
4484 * apart from the constant term and the coefficient at position "pos"?
4486 static isl_bool
is_parallel_except(__isl_keep isl_basic_map
*bmap
, int i
, int j
,
4491 total
= isl_basic_map_dim(bmap
, isl_dim_all
);
4493 return isl_bool_error
;
4494 return is_parallel_part(bmap
, i
, j
, 1, pos
- 1) &&
4495 is_parallel_part(bmap
, i
, j
, pos
+ 1, total
- pos
);
4498 /* Are inequality constraints "i" and "j" of "bmap" opposite to each other,
4499 * apart from the constant term and the coefficient at position "pos"?
4501 static isl_bool
is_opposite_except(__isl_keep isl_basic_map
*bmap
, int i
, int j
,
4506 total
= isl_basic_map_dim(bmap
, isl_dim_all
);
4508 return isl_bool_error
;
4509 return is_opposite_part(bmap
, i
, j
, 1, pos
- 1) &&
4510 is_opposite_part(bmap
, i
, j
, pos
+ 1, total
- pos
);
4513 /* Restart isl_basic_map_drop_redundant_divs after "bmap" has
4514 * been modified, simplying it if "simplify" is set.
4515 * Free the temporary data structure "pairs" that was associated
4516 * to the old version of "bmap".
4518 static __isl_give isl_basic_map
*drop_redundant_divs_again(
4519 __isl_take isl_basic_map
*bmap
, __isl_take
int *pairs
, int simplify
)
4522 bmap
= isl_basic_map_simplify(bmap
);
4524 return isl_basic_map_drop_redundant_divs(bmap
);
4527 /* Is "div" the single unknown existentially quantified variable
4528 * in inequality constraint "ineq" of "bmap"?
4529 * "div" is known to have a non-zero coefficient in "ineq".
4531 static isl_bool
single_unknown(__isl_keep isl_basic_map
*bmap
, int ineq
,
4539 known
= isl_basic_map_div_is_known(bmap
, div
);
4540 if (known
< 0 || known
)
4541 return isl_bool_not(known
);
4542 n_div
= isl_basic_map_dim(bmap
, isl_dim_div
);
4544 return isl_bool_error
;
4546 return isl_bool_true
;
4547 o_div
= isl_basic_map_offset(bmap
, isl_dim_div
);
4548 for (i
= 0; i
< n_div
; ++i
) {
4553 if (isl_int_is_zero(bmap
->ineq
[ineq
][o_div
+ i
]))
4555 known
= isl_basic_map_div_is_known(bmap
, i
);
4556 if (known
< 0 || !known
)
4560 return isl_bool_true
;
4563 /* Does integer division "div" have coefficient 1 in inequality constraint
4566 static isl_bool
has_coef_one(__isl_keep isl_basic_map
*bmap
, int div
, int ineq
)
4570 o_div
= isl_basic_map_offset(bmap
, isl_dim_div
);
4571 if (isl_int_is_one(bmap
->ineq
[ineq
][o_div
+ div
]))
4572 return isl_bool_true
;
4574 return isl_bool_false
;
4577 /* Turn inequality constraint "ineq" of "bmap" into an equality and
4578 * then try and drop redundant divs again,
4579 * freeing the temporary data structure "pairs" that was associated
4580 * to the old version of "bmap".
4582 static __isl_give isl_basic_map
*set_eq_and_try_again(
4583 __isl_take isl_basic_map
*bmap
, int ineq
, __isl_take
int *pairs
)
4585 bmap
= isl_basic_map_cow(bmap
);
4586 isl_basic_map_inequality_to_equality(bmap
, ineq
);
4587 return drop_redundant_divs_again(bmap
, pairs
, 1);
4590 /* Drop the integer division at position "div", along with the two
4591 * inequality constraints "ineq1" and "ineq2" in which it appears
4592 * from "bmap" and then try and drop redundant divs again,
4593 * freeing the temporary data structure "pairs" that was associated
4594 * to the old version of "bmap".
4596 static __isl_give isl_basic_map
*drop_div_and_try_again(
4597 __isl_take isl_basic_map
*bmap
, int div
, int ineq1
, int ineq2
,
4598 __isl_take
int *pairs
)
4600 if (ineq1
> ineq2
) {
4601 isl_basic_map_drop_inequality(bmap
, ineq1
);
4602 isl_basic_map_drop_inequality(bmap
, ineq2
);
4604 isl_basic_map_drop_inequality(bmap
, ineq2
);
4605 isl_basic_map_drop_inequality(bmap
, ineq1
);
4607 bmap
= isl_basic_map_drop_div(bmap
, div
);
4608 return drop_redundant_divs_again(bmap
, pairs
, 0);
4611 /* Given two inequality constraints
4613 * f(x) + n d + c >= 0, (ineq)
4615 * with d the variable at position "pos", and
4617 * f(x) + c0 >= 0, (lower)
4619 * compute the maximal value of the lower bound ceil((-f(x) - c)/n)
4620 * determined by the first constraint.
4627 static void lower_bound_from_parallel(__isl_keep isl_basic_map
*bmap
,
4628 int ineq
, int lower
, int pos
, isl_int
*l
)
4630 isl_int_neg(*l
, bmap
->ineq
[ineq
][0]);
4631 isl_int_add(*l
, *l
, bmap
->ineq
[lower
][0]);
4632 isl_int_cdiv_q(*l
, *l
, bmap
->ineq
[ineq
][pos
]);
4635 /* Given two inequality constraints
4637 * f(x) + n d + c >= 0, (ineq)
4639 * with d the variable at position "pos", and
4641 * -f(x) - c0 >= 0, (upper)
4643 * compute the minimal value of the lower bound ceil((-f(x) - c)/n)
4644 * determined by the first constraint.
4651 static void lower_bound_from_opposite(__isl_keep isl_basic_map
*bmap
,
4652 int ineq
, int upper
, int pos
, isl_int
*u
)
4654 isl_int_neg(*u
, bmap
->ineq
[ineq
][0]);
4655 isl_int_sub(*u
, *u
, bmap
->ineq
[upper
][0]);
4656 isl_int_cdiv_q(*u
, *u
, bmap
->ineq
[ineq
][pos
]);
4659 /* Given a lower bound constraint "ineq" on "div" in "bmap",
4660 * does the corresponding lower bound have a fixed value in "bmap"?
4662 * In particular, "ineq" is of the form
4664 * f(x) + n d + c >= 0
4666 * with n > 0, c the constant term and
4667 * d the existentially quantified variable "div".
4668 * That is, the lower bound is
4670 * ceil((-f(x) - c)/n)
4672 * Look for a pair of constraints
4677 * i.e., -c1 <= -f(x) <= c0, that fix ceil((-f(x) - c)/n) to a constant value.
4678 * That is, check that
4680 * ceil((-c1 - c)/n) = ceil((c0 - c)/n)
4682 * If so, return the index of inequality f(x) + c0 >= 0.
4683 * Otherwise, return bmap->n_ineq.
4684 * Return -1 on error.
4686 static int lower_bound_is_cst(__isl_keep isl_basic_map
*bmap
, int div
, int ineq
)
4689 int lower
= -1, upper
= -1;
4694 o_div
= isl_basic_map_offset(bmap
, isl_dim_div
);
4695 for (i
= 0; i
< bmap
->n_ineq
&& (lower
< 0 || upper
< 0); ++i
) {
4700 if (!isl_int_is_zero(bmap
->ineq
[i
][o_div
+ div
]))
4702 par
= isl_bool_false
;
4704 par
= is_parallel_except(bmap
, ineq
, i
, o_div
+ div
);
4711 opp
= isl_bool_false
;
4713 opp
= is_opposite_except(bmap
, ineq
, i
, o_div
+ div
);
4720 if (lower
< 0 || upper
< 0)
4721 return bmap
->n_ineq
;
4726 lower_bound_from_parallel(bmap
, ineq
, lower
, o_div
+ div
, &l
);
4727 lower_bound_from_opposite(bmap
, ineq
, upper
, o_div
+ div
, &u
);
4729 equal
= isl_int_eq(l
, u
);
4734 return equal
? lower
: bmap
->n_ineq
;
4737 /* Given a lower bound constraint "ineq" on the existentially quantified
4738 * variable "div", such that the corresponding lower bound has
4739 * a fixed value in "bmap", assign this fixed value to the variable and
4740 * then try and drop redundant divs again,
4741 * freeing the temporary data structure "pairs" that was associated
4742 * to the old version of "bmap".
4743 * "lower" determines the constant value for the lower bound.
4745 * In particular, "ineq" is of the form
4747 * f(x) + n d + c >= 0,
4749 * while "lower" is of the form
4753 * The lower bound is ceil((-f(x) - c)/n) and its constant value
4754 * is ceil((c0 - c)/n).
4756 static __isl_give isl_basic_map
*fix_cst_lower(__isl_take isl_basic_map
*bmap
,
4757 int div
, int ineq
, int lower
, int *pairs
)
4764 o_div
= isl_basic_map_offset(bmap
, isl_dim_div
);
4765 lower_bound_from_parallel(bmap
, ineq
, lower
, o_div
+ div
, &c
);
4766 bmap
= isl_basic_map_fix(bmap
, isl_dim_div
, div
, c
);
4771 return isl_basic_map_drop_redundant_divs(bmap
);
4774 /* Remove divs that are not strictly needed based on the inequality
4776 * In particular, if a div only occurs positively (or negatively)
4777 * in constraints, then it can simply be dropped.
4778 * Also, if a div occurs in only two constraints and if moreover
4779 * those two constraints are opposite to each other, except for the constant
4780 * term and if the sum of the constant terms is such that for any value
4781 * of the other values, there is always at least one integer value of the
4782 * div, i.e., if one plus this sum is greater than or equal to
4783 * the (absolute value) of the coefficient of the div in the constraints,
4784 * then we can also simply drop the div.
4786 * If an existentially quantified variable does not have an explicit
4787 * representation, appears in only a single lower bound that does not
4788 * involve any other such existentially quantified variables and appears
4789 * in this lower bound with coefficient 1,
4790 * then fix the variable to the value of the lower bound. That is,
4791 * turn the inequality into an equality.
4792 * If for any value of the other variables, there is any value
4793 * for the existentially quantified variable satisfying the constraints,
4794 * then this lower bound also satisfies the constraints.
4795 * It is therefore safe to pick this lower bound.
4797 * The same reasoning holds even if the coefficient is not one.
4798 * However, fixing the variable to the value of the lower bound may
4799 * in general introduce an extra integer division, in which case
4800 * it may be better to pick another value.
4801 * If this integer division has a known constant value, then plugging
4802 * in this constant value removes the existentially quantified variable
4803 * completely. In particular, if the lower bound is of the form
4804 * ceil((-f(x) - c)/n) and there are two constraints, f(x) + c0 >= 0 and
4805 * -f(x) + c1 >= 0 such that ceil((-c1 - c)/n) = ceil((c0 - c)/n),
4806 * then the existentially quantified variable can be assigned this
4809 * We skip divs that appear in equalities or in the definition of other divs.
4810 * Divs that appear in the definition of other divs usually occur in at least
4811 * 4 constraints, but the constraints may have been simplified.
4813 * If any divs are left after these simple checks then we move on
4814 * to more complicated cases in drop_more_redundant_divs.
4816 static __isl_give isl_basic_map
*isl_basic_map_drop_redundant_divs_ineq(
4817 __isl_take isl_basic_map
*bmap
)
4827 if (bmap
->n_div
== 0)
4830 off
= isl_basic_map_var_offset(bmap
, isl_dim_div
);
4832 return isl_basic_map_free(bmap
);
4833 pairs
= isl_calloc_array(bmap
->ctx
, int, bmap
->n_div
);
4837 n_ineq
= isl_basic_map_n_inequality(bmap
);
4838 for (i
= 0; i
< bmap
->n_div
; ++i
) {
4840 int last_pos
, last_neg
;
4843 isl_bool opp
, set_div
;
4845 defined
= !isl_int_is_zero(bmap
->div
[i
][0]);
4846 for (j
= i
; j
< bmap
->n_div
; ++j
)
4847 if (!isl_int_is_zero(bmap
->div
[j
][1 + 1 + off
+ i
]))
4849 if (j
< bmap
->n_div
)
4851 for (j
= 0; j
< bmap
->n_eq
; ++j
)
4852 if (!isl_int_is_zero(bmap
->eq
[j
][1 + off
+ i
]))
4858 for (j
= 0; j
< bmap
->n_ineq
; ++j
) {
4859 if (isl_int_is_pos(bmap
->ineq
[j
][1 + off
+ i
])) {
4863 if (isl_int_is_neg(bmap
->ineq
[j
][1 + off
+ i
])) {
4868 pairs
[i
] = pos
* neg
;
4869 if (pairs
[i
] == 0) {
4870 for (j
= bmap
->n_ineq
- 1; j
>= 0; --j
)
4871 if (!isl_int_is_zero(bmap
->ineq
[j
][1+off
+i
]))
4872 isl_basic_map_drop_inequality(bmap
, j
);
4873 bmap
= isl_basic_map_drop_div(bmap
, i
);
4874 return drop_redundant_divs_again(bmap
, pairs
, 0);
4877 opp
= isl_bool_false
;
4879 opp
= is_opposite(bmap
, last_pos
, last_neg
);
4884 isl_bool single
, one
;
4888 single
= single_unknown(bmap
, last_pos
, i
);
4893 one
= has_coef_one(bmap
, i
, last_pos
);
4897 return set_eq_and_try_again(bmap
, last_pos
,
4899 lower
= lower_bound_is_cst(bmap
, i
, last_pos
);
4903 return fix_cst_lower(bmap
, i
, last_pos
, lower
,
4908 isl_int_add(bmap
->ineq
[last_pos
][0],
4909 bmap
->ineq
[last_pos
][0], bmap
->ineq
[last_neg
][0]);
4910 isl_int_add_ui(bmap
->ineq
[last_pos
][0],
4911 bmap
->ineq
[last_pos
][0], 1);
4912 redundant
= isl_int_ge(bmap
->ineq
[last_pos
][0],
4913 bmap
->ineq
[last_pos
][1+off
+i
]);
4914 isl_int_sub_ui(bmap
->ineq
[last_pos
][0],
4915 bmap
->ineq
[last_pos
][0], 1);
4916 isl_int_sub(bmap
->ineq
[last_pos
][0],
4917 bmap
->ineq
[last_pos
][0], bmap
->ineq
[last_neg
][0]);
4919 return drop_div_and_try_again(bmap
, i
,
4920 last_pos
, last_neg
, pairs
);
4922 set_div
= isl_bool_false
;
4924 set_div
= ok_to_set_div_from_bound(bmap
, i
, last_pos
);
4926 return isl_basic_map_free(bmap
);
4928 bmap
= set_div_from_lower_bound(bmap
, i
, last_pos
);
4929 return drop_redundant_divs_again(bmap
, pairs
, 1);
4936 return coalesce_or_drop_more_redundant_divs(bmap
, pairs
, n
);
4942 isl_basic_map_free(bmap
);
4946 /* Consider the coefficients at "c" as a row vector and replace
4947 * them with their product with "T". "T" is assumed to be a square matrix.
4949 static isl_stat
preimage(isl_int
*c
, __isl_keep isl_mat
*T
)
4955 n
= isl_mat_rows(T
);
4957 return isl_stat_error
;
4958 if (isl_seq_first_non_zero(c
, n
) == -1)
4960 ctx
= isl_mat_get_ctx(T
);
4961 v
= isl_vec_alloc(ctx
, n
);
4963 return isl_stat_error
;
4964 isl_seq_swp_or_cpy(v
->el
, c
, n
);
4965 v
= isl_vec_mat_product(v
, isl_mat_copy(T
));
4967 return isl_stat_error
;
4968 isl_seq_swp_or_cpy(c
, v
->el
, n
);
4974 /* Plug in T for the variables in "bmap" starting at "pos".
4975 * T is a linear unimodular matrix, i.e., without constant term.
4977 static __isl_give isl_basic_map
*isl_basic_map_preimage_vars(
4978 __isl_take isl_basic_map
*bmap
, unsigned pos
, __isl_take isl_mat
*T
)
4981 isl_size n_row
, n_col
;
4983 bmap
= isl_basic_map_cow(bmap
);
4984 n_row
= isl_mat_rows(T
);
4985 n_col
= isl_mat_cols(T
);
4986 if (!bmap
|| n_row
< 0 || n_col
< 0)
4990 isl_die(isl_mat_get_ctx(T
), isl_error_invalid
,
4991 "expecting square matrix", goto error
);
4993 if (isl_basic_map_check_range(bmap
, isl_dim_all
, pos
, n_col
) < 0)
4996 for (i
= 0; i
< bmap
->n_eq
; ++i
)
4997 if (preimage(bmap
->eq
[i
] + 1 + pos
, T
) < 0)
4999 for (i
= 0; i
< bmap
->n_ineq
; ++i
)
5000 if (preimage(bmap
->ineq
[i
] + 1 + pos
, T
) < 0)
5002 for (i
= 0; i
< bmap
->n_div
; ++i
) {
5003 if (isl_basic_map_div_is_marked_unknown(bmap
, i
))
5005 if (preimage(bmap
->div
[i
] + 1 + 1 + pos
, T
) < 0)
5012 isl_basic_map_free(bmap
);
5017 /* Remove divs that are not strictly needed.
5019 * First look for an equality constraint involving two or more
5020 * existentially quantified variables without an explicit
5021 * representation. Replace the combination that appears
5022 * in the equality constraint by a single existentially quantified
5023 * variable such that the equality can be used to derive
5024 * an explicit representation for the variable.
5025 * If there are no more such equality constraints, then continue
5026 * with isl_basic_map_drop_redundant_divs_ineq.
5028 * In particular, if the equality constraint is of the form
5030 * f(x) + \sum_i c_i a_i = 0
5032 * with a_i existentially quantified variable without explicit
5033 * representation, then apply a transformation on the existentially
5034 * quantified variables to turn the constraint into
5038 * with g the gcd of the c_i.
5039 * In order to easily identify which existentially quantified variables
5040 * have a complete explicit representation, i.e., without being defined
5041 * in terms of other existentially quantified variables without
5042 * an explicit representation, the existentially quantified variables
5045 * The variable transformation is computed by extending the row
5046 * [c_1/g ... c_n/g] to a unimodular matrix, obtaining the transformation
5048 * [a_1'] [c_1/g ... c_n/g] [ a_1 ]
5053 * with [c_1/g ... c_n/g] representing the first row of U.
5054 * The inverse of U is then plugged into the original constraints.
5055 * The call to isl_basic_map_simplify makes sure the explicit
5056 * representation for a_1' is extracted from the equality constraint.
5058 __isl_give isl_basic_map
*isl_basic_map_drop_redundant_divs(
5059 __isl_take isl_basic_map
*bmap
)
5071 if (isl_basic_map_divs_known(bmap
))
5072 return isl_basic_map_drop_redundant_divs_ineq(bmap
);
5073 if (bmap
->n_eq
== 0)
5074 return isl_basic_map_drop_redundant_divs_ineq(bmap
);
5075 bmap
= isl_basic_map_sort_divs(bmap
);
5079 first
= isl_basic_map_first_unknown_div(bmap
);
5081 return isl_basic_map_free(bmap
);
5083 o_div
= isl_basic_map_offset(bmap
, isl_dim_div
);
5084 n_div
= isl_basic_map_dim(bmap
, isl_dim_div
);
5086 return isl_basic_map_free(bmap
);
5088 for (i
= 0; i
< bmap
->n_eq
; ++i
) {
5089 l
= isl_seq_first_non_zero(bmap
->eq
[i
] + o_div
+ first
,
5094 if (isl_seq_first_non_zero(bmap
->eq
[i
] + o_div
+ l
+ 1,
5095 n_div
- (l
+ 1)) == -1)
5099 if (i
>= bmap
->n_eq
)
5100 return isl_basic_map_drop_redundant_divs_ineq(bmap
);
5102 ctx
= isl_basic_map_get_ctx(bmap
);
5103 T
= isl_mat_alloc(ctx
, n_div
- l
, n_div
- l
);
5105 return isl_basic_map_free(bmap
);
5106 isl_seq_cpy(T
->row
[0], bmap
->eq
[i
] + o_div
+ l
, n_div
- l
);
5107 T
= isl_mat_normalize_row(T
, 0);
5108 T
= isl_mat_unimodular_complete(T
, 1);
5109 T
= isl_mat_right_inverse(T
);
5111 for (i
= l
; i
< n_div
; ++i
)
5112 bmap
= isl_basic_map_mark_div_unknown(bmap
, i
);
5113 bmap
= isl_basic_map_preimage_vars(bmap
, o_div
- 1 + l
, T
);
5114 bmap
= isl_basic_map_simplify(bmap
);
5116 return isl_basic_map_drop_redundant_divs(bmap
);
5119 /* Does "bmap" satisfy any equality that involves more than 2 variables
5120 * and/or has coefficients different from -1 and 1?
5122 static isl_bool
has_multiple_var_equality(__isl_keep isl_basic_map
*bmap
)
5127 total
= isl_basic_map_dim(bmap
, isl_dim_all
);
5129 return isl_bool_error
;
5131 for (i
= 0; i
< bmap
->n_eq
; ++i
) {
5134 j
= isl_seq_first_non_zero(bmap
->eq
[i
] + 1, total
);
5137 if (!isl_int_is_one(bmap
->eq
[i
][1 + j
]) &&
5138 !isl_int_is_negone(bmap
->eq
[i
][1 + j
]))
5139 return isl_bool_true
;
5142 k
= isl_seq_first_non_zero(bmap
->eq
[i
] + 1 + j
, total
- j
);
5146 if (!isl_int_is_one(bmap
->eq
[i
][1 + j
]) &&
5147 !isl_int_is_negone(bmap
->eq
[i
][1 + j
]))
5148 return isl_bool_true
;
5151 k
= isl_seq_first_non_zero(bmap
->eq
[i
] + 1 + j
, total
- j
);
5153 return isl_bool_true
;
5156 return isl_bool_false
;
5159 /* Remove any common factor g from the constraint coefficients in "v".
5160 * The constant term is stored in the first position and is replaced
5161 * by floor(c/g). If any common factor is removed and if this results
5162 * in a tightening of the constraint, then set *tightened.
5164 static __isl_give isl_vec
*normalize_constraint(__isl_take isl_vec
*v
,
5171 ctx
= isl_vec_get_ctx(v
);
5172 isl_seq_gcd(v
->el
+ 1, v
->size
- 1, &ctx
->normalize_gcd
);
5173 if (isl_int_is_zero(ctx
->normalize_gcd
))
5175 if (isl_int_is_one(ctx
->normalize_gcd
))
5180 if (tightened
&& !isl_int_is_divisible_by(v
->el
[0], ctx
->normalize_gcd
))
5182 isl_int_fdiv_q(v
->el
[0], v
->el
[0], ctx
->normalize_gcd
);
5183 isl_seq_scale_down(v
->el
+ 1, v
->el
+ 1, ctx
->normalize_gcd
,
5188 /* If "bmap" is an integer set that satisfies any equality involving
5189 * more than 2 variables and/or has coefficients different from -1 and 1,
5190 * then use variable compression to reduce the coefficients by removing
5191 * any (hidden) common factor.
5192 * In particular, apply the variable compression to each constraint,
5193 * factor out any common factor in the non-constant coefficients and
5194 * then apply the inverse of the compression.
5195 * At the end, we mark the basic map as having reduced constants.
5196 * If this flag is still set on the next invocation of this function,
5197 * then we skip the computation.
5199 * Removing a common factor may result in a tightening of some of
5200 * the constraints. If this happens, then we may end up with two
5201 * opposite inequalities that can be replaced by an equality.
5202 * We therefore call isl_basic_map_detect_inequality_pairs,
5203 * which checks for such pairs of inequalities as well as eliminate_divs_eq
5204 * and isl_basic_map_gauss if such a pair was found.
5206 * Note that this function may leave the result in an inconsistent state.
5207 * In particular, the constraints may not be gaussed.
5208 * Unfortunately, isl_map_coalesce actually depends on this inconsistent state
5209 * for some of the test cases to pass successfully.
5210 * Any potential modification of the representation is therefore only
5211 * performed on a single copy of the basic map.
5213 __isl_give isl_basic_map
*isl_basic_map_reduce_coefficients(
5214 __isl_take isl_basic_map
*bmap
)
5220 isl_mat
*eq
, *T
, *T2
;
5226 if (ISL_F_ISSET(bmap
, ISL_BASIC_MAP_REDUCED_COEFFICIENTS
))
5228 if (isl_basic_map_is_rational(bmap
))
5230 if (bmap
->n_eq
== 0)
5232 multi
= has_multiple_var_equality(bmap
);
5234 return isl_basic_map_free(bmap
);
5238 total
= isl_basic_map_dim(bmap
, isl_dim_all
);
5240 return isl_basic_map_free(bmap
);
5241 ctx
= isl_basic_map_get_ctx(bmap
);
5242 v
= isl_vec_alloc(ctx
, 1 + total
);
5244 return isl_basic_map_free(bmap
);
5246 eq
= isl_mat_sub_alloc6(ctx
, bmap
->eq
, 0, bmap
->n_eq
, 0, 1 + total
);
5247 T
= isl_mat_variable_compression(eq
, &T2
);
5250 if (T
->n_col
== 0) {
5254 return isl_basic_map_set_to_empty(bmap
);
5257 bmap
= isl_basic_map_cow(bmap
);
5262 for (i
= 0; i
< bmap
->n_ineq
; ++i
) {
5263 isl_seq_cpy(v
->el
, bmap
->ineq
[i
], 1 + total
);
5264 v
= isl_vec_mat_product(v
, isl_mat_copy(T
));
5265 v
= normalize_constraint(v
, &tightened
);
5266 v
= isl_vec_mat_product(v
, isl_mat_copy(T2
));
5269 isl_seq_cpy(bmap
->ineq
[i
], v
->el
, 1 + total
);
5276 ISL_F_SET(bmap
, ISL_BASIC_MAP_REDUCED_COEFFICIENTS
);
5281 bmap
= isl_basic_map_detect_inequality_pairs(bmap
, &progress
);
5283 bmap
= eliminate_divs_eq(bmap
, &progress
);
5284 bmap
= isl_basic_map_gauss(bmap
, NULL
);
5293 return isl_basic_map_free(bmap
);
5296 /* Shift the integer division at position "div" of "bmap"
5297 * by "shift" times the variable at position "pos".
5298 * "pos" is as determined by isl_basic_map_offset, i.e., pos == 0
5299 * corresponds to the constant term.
5301 * That is, if the integer division has the form
5305 * then replace it by
5307 * floor((f(x) + shift * d * x_pos)/d) - shift * x_pos
5309 __isl_give isl_basic_map
*isl_basic_map_shift_div(
5310 __isl_take isl_basic_map
*bmap
, int div
, int pos
, isl_int shift
)
5313 isl_size total
, n_div
;
5315 if (isl_int_is_zero(shift
))
5317 total
= isl_basic_map_dim(bmap
, isl_dim_all
);
5318 n_div
= isl_basic_map_dim(bmap
, isl_dim_div
);
5320 if (total
< 0 || n_div
< 0)
5321 return isl_basic_map_free(bmap
);
5323 isl_int_addmul(bmap
->div
[div
][1 + pos
], shift
, bmap
->div
[div
][0]);
5325 for (i
= 0; i
< bmap
->n_eq
; ++i
) {
5326 if (isl_int_is_zero(bmap
->eq
[i
][1 + total
+ div
]))
5328 isl_int_submul(bmap
->eq
[i
][pos
],
5329 shift
, bmap
->eq
[i
][1 + total
+ div
]);
5331 for (i
= 0; i
< bmap
->n_ineq
; ++i
) {
5332 if (isl_int_is_zero(bmap
->ineq
[i
][1 + total
+ div
]))
5334 isl_int_submul(bmap
->ineq
[i
][pos
],
5335 shift
, bmap
->ineq
[i
][1 + total
+ div
]);
5337 for (i
= 0; i
< bmap
->n_div
; ++i
) {
5338 if (isl_int_is_zero(bmap
->div
[i
][0]))
5340 if (isl_int_is_zero(bmap
->div
[i
][1 + 1 + total
+ div
]))
5342 isl_int_submul(bmap
->div
[i
][1 + pos
],
5343 shift
, bmap
->div
[i
][1 + 1 + total
+ div
]);