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(__isl_keep 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(__isl_keep 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 if (isl_basic_map_drop_equality(bmap
, i
) < 0)
69 if (ISL_F_ISSET(bmap
, ISL_BASIC_MAP_RATIONAL
))
70 isl_int_gcd(gcd
, gcd
, bmap
->eq
[i
][0]);
71 if (isl_int_is_one(gcd
))
73 if (!isl_int_is_divisible_by(bmap
->eq
[i
][0], gcd
)) {
74 bmap
= isl_basic_map_set_to_empty(bmap
);
77 isl_seq_scale_down(bmap
->eq
[i
], bmap
->eq
[i
], gcd
, 1+total
);
80 for (i
= bmap
->n_ineq
- 1; i
>= 0; --i
) {
81 isl_seq_gcd(bmap
->ineq
[i
]+1, total
, &gcd
);
82 if (isl_int_is_zero(gcd
)) {
83 if (isl_int_is_neg(bmap
->ineq
[i
][0])) {
84 bmap
= isl_basic_map_set_to_empty(bmap
);
87 if (isl_basic_map_drop_inequality(bmap
, i
) < 0)
91 if (ISL_F_ISSET(bmap
, ISL_BASIC_MAP_RATIONAL
))
92 isl_int_gcd(gcd
, gcd
, bmap
->ineq
[i
][0]);
93 if (isl_int_is_one(gcd
))
95 isl_int_fdiv_q(bmap
->ineq
[i
][0], bmap
->ineq
[i
][0], gcd
);
96 isl_seq_scale_down(bmap
->ineq
[i
]+1, bmap
->ineq
[i
]+1, gcd
, total
);
103 isl_basic_map_free(bmap
);
107 __isl_give isl_basic_set
*isl_basic_set_normalize_constraints(
108 __isl_take isl_basic_set
*bset
)
110 isl_basic_map
*bmap
= bset_to_bmap(bset
);
111 return bset_from_bmap(isl_basic_map_normalize_constraints(bmap
));
114 /* Reduce the coefficient of the variable at position "pos"
115 * in integer division "div", such that it lies in the half-open
116 * interval (1/2,1/2], extracting any excess value from this integer division.
117 * "pos" is as determined by isl_basic_map_offset, i.e., pos == 0
118 * corresponds to the constant term.
120 * That is, the integer division is of the form
122 * floor((... + (c * d + r) * x_pos + ...)/d)
124 * with -d < 2 * r <= d.
127 * floor((... + r * x_pos + ...)/d) + c * x_pos
129 * If 2 * ((c * d + r) % d) <= d, then c = floor((c * d + r)/d).
130 * Otherwise, c = floor((c * d + r)/d) + 1.
132 * This is the same normalization that is performed by isl_aff_floor.
134 static __isl_give isl_basic_map
*reduce_coefficient_in_div(
135 __isl_take isl_basic_map
*bmap
, int div
, int pos
)
141 isl_int_fdiv_r(shift
, bmap
->div
[div
][1 + pos
], bmap
->div
[div
][0]);
142 isl_int_mul_ui(shift
, shift
, 2);
143 add_one
= isl_int_gt(shift
, bmap
->div
[div
][0]);
144 isl_int_fdiv_q(shift
, bmap
->div
[div
][1 + pos
], bmap
->div
[div
][0]);
146 isl_int_add_ui(shift
, shift
, 1);
147 isl_int_neg(shift
, shift
);
148 bmap
= isl_basic_map_shift_div(bmap
, div
, pos
, shift
);
149 isl_int_clear(shift
);
154 /* Does the coefficient of the variable at position "pos"
155 * in integer division "div" need to be reduced?
156 * That is, does it lie outside the half-open interval (1/2,1/2]?
157 * The coefficient c/d lies outside this interval if abs(2 * c) >= d and
160 static isl_bool
needs_reduction(__isl_keep isl_basic_map
*bmap
, int div
,
165 if (isl_int_is_zero(bmap
->div
[div
][1 + pos
]))
166 return isl_bool_false
;
168 isl_int_mul_ui(bmap
->div
[div
][1 + pos
], bmap
->div
[div
][1 + pos
], 2);
169 r
= isl_int_abs_ge(bmap
->div
[div
][1 + pos
], bmap
->div
[div
][0]) &&
170 !isl_int_eq(bmap
->div
[div
][1 + pos
], bmap
->div
[div
][0]);
171 isl_int_divexact_ui(bmap
->div
[div
][1 + pos
],
172 bmap
->div
[div
][1 + pos
], 2);
177 /* Reduce the coefficients (including the constant term) of
178 * integer division "div", if needed.
179 * In particular, make sure all coefficients lie in
180 * the half-open interval (1/2,1/2].
182 static __isl_give isl_basic_map
*reduce_div_coefficients_of_div(
183 __isl_take isl_basic_map
*bmap
, int div
)
188 total
= isl_basic_map_dim(bmap
, isl_dim_all
);
190 return isl_basic_map_free(bmap
);
191 for (i
= 0; i
< 1 + total
; ++i
) {
194 reduce
= needs_reduction(bmap
, div
, i
);
196 return isl_basic_map_free(bmap
);
199 bmap
= reduce_coefficient_in_div(bmap
, div
, i
);
207 /* Reduce the coefficients (including the constant term) of
208 * the known integer divisions, if needed
209 * In particular, make sure all coefficients lie in
210 * the half-open interval (1/2,1/2].
212 static __isl_give isl_basic_map
*reduce_div_coefficients(
213 __isl_take isl_basic_map
*bmap
)
219 if (bmap
->n_div
== 0)
222 for (i
= 0; i
< bmap
->n_div
; ++i
) {
223 if (isl_int_is_zero(bmap
->div
[i
][0]))
225 bmap
= reduce_div_coefficients_of_div(bmap
, i
);
233 /* Remove any common factor in numerator and denominator of the div expression,
234 * not taking into account the constant term.
235 * That is, if the div is of the form
237 * floor((a + m f(x))/(m d))
241 * floor((floor(a/m) + f(x))/d)
243 * The difference {a/m}/d in the argument satisfies 0 <= {a/m}/d < 1/d
244 * and can therefore not influence the result of the floor.
246 static __isl_give isl_basic_map
*normalize_div_expression(
247 __isl_take isl_basic_map
*bmap
, int div
)
249 isl_size total
= isl_basic_map_dim(bmap
, isl_dim_all
);
250 isl_ctx
*ctx
= bmap
->ctx
;
253 return isl_basic_map_free(bmap
);
254 if (isl_int_is_zero(bmap
->div
[div
][0]))
256 isl_seq_gcd(bmap
->div
[div
] + 2, total
, &ctx
->normalize_gcd
);
257 isl_int_gcd(ctx
->normalize_gcd
, ctx
->normalize_gcd
, bmap
->div
[div
][0]);
258 if (isl_int_is_one(ctx
->normalize_gcd
))
260 isl_int_fdiv_q(bmap
->div
[div
][1], bmap
->div
[div
][1],
262 isl_int_divexact(bmap
->div
[div
][0], bmap
->div
[div
][0],
264 isl_seq_scale_down(bmap
->div
[div
] + 2, bmap
->div
[div
] + 2,
265 ctx
->normalize_gcd
, total
);
270 /* Remove any common factor in numerator and denominator of a div expression,
271 * not taking into account the constant term.
272 * That is, look for any div of the form
274 * floor((a + m f(x))/(m d))
278 * floor((floor(a/m) + f(x))/d)
280 * The difference {a/m}/d in the argument satisfies 0 <= {a/m}/d < 1/d
281 * and can therefore not influence the result of the floor.
283 static __isl_give isl_basic_map
*normalize_div_expressions(
284 __isl_take isl_basic_map
*bmap
)
290 if (bmap
->n_div
== 0)
293 for (i
= 0; i
< bmap
->n_div
; ++i
)
294 bmap
= normalize_div_expression(bmap
, i
);
299 /* Assumes divs have been ordered if keep_divs is set.
301 static __isl_give isl_basic_map
*eliminate_var_using_equality(
302 __isl_take isl_basic_map
*bmap
,
303 unsigned pos
, isl_int
*eq
, int keep_divs
, int *progress
)
310 total
= isl_basic_map_dim(bmap
, isl_dim_all
);
311 v_div
= isl_basic_map_var_offset(bmap
, isl_dim_div
);
312 if (total
< 0 || v_div
< 0)
313 return isl_basic_map_free(bmap
);
314 last_div
= isl_seq_last_non_zero(eq
+ 1 + v_div
, bmap
->n_div
);
315 for (k
= 0; k
< bmap
->n_eq
; ++k
) {
316 if (bmap
->eq
[k
] == eq
)
318 if (isl_int_is_zero(bmap
->eq
[k
][1+pos
]))
322 isl_seq_elim(bmap
->eq
[k
], eq
, 1+pos
, 1+total
, NULL
);
323 isl_seq_normalize(bmap
->ctx
, bmap
->eq
[k
], 1 + total
);
326 for (k
= 0; k
< bmap
->n_ineq
; ++k
) {
327 if (isl_int_is_zero(bmap
->ineq
[k
][1+pos
]))
331 isl_seq_elim(bmap
->ineq
[k
], eq
, 1+pos
, 1+total
, NULL
);
332 isl_seq_normalize(bmap
->ctx
, bmap
->ineq
[k
], 1 + total
);
333 ISL_F_CLR(bmap
, ISL_BASIC_MAP_NO_REDUNDANT
);
334 ISL_F_CLR(bmap
, ISL_BASIC_MAP_SORTED
);
335 ISL_F_CLR(bmap
, ISL_BASIC_MAP_REDUCED_COEFFICIENTS
);
338 for (k
= 0; k
< bmap
->n_div
; ++k
) {
339 if (isl_int_is_zero(bmap
->div
[k
][0]))
341 if (isl_int_is_zero(bmap
->div
[k
][1+1+pos
]))
345 /* We need to be careful about circular definitions,
346 * so for now we just remove the definition of div k
347 * if the equality contains any divs.
348 * If keep_divs is set, then the divs have been ordered
349 * and we can keep the definition as long as the result
352 if (last_div
== -1 || (keep_divs
&& last_div
< k
)) {
353 isl_seq_elim(bmap
->div
[k
]+1, eq
,
354 1+pos
, 1+total
, &bmap
->div
[k
][0]);
355 bmap
= normalize_div_expression(bmap
, k
);
359 isl_seq_clr(bmap
->div
[k
], 1 + total
);
365 /* Assumes divs have been ordered if keep_divs is set.
367 static __isl_give isl_basic_map
*eliminate_div(__isl_take isl_basic_map
*bmap
,
368 isl_int
*eq
, unsigned div
, int keep_divs
)
373 v_div
= isl_basic_map_var_offset(bmap
, isl_dim_div
);
375 return isl_basic_map_free(bmap
);
377 bmap
= eliminate_var_using_equality(bmap
, pos
, eq
, keep_divs
, NULL
);
379 bmap
= isl_basic_map_drop_div(bmap
, div
);
384 /* Check if elimination of div "div" using equality "eq" would not
385 * result in a div depending on a later div.
387 static isl_bool
ok_to_eliminate_div(__isl_keep isl_basic_map
*bmap
, isl_int
*eq
,
395 v_div
= isl_basic_map_var_offset(bmap
, isl_dim_div
);
397 return isl_bool_error
;
400 last_div
= isl_seq_last_non_zero(eq
+ 1 + v_div
, bmap
->n_div
);
401 if (last_div
< 0 || last_div
<= div
)
402 return isl_bool_true
;
404 for (k
= 0; k
<= last_div
; ++k
) {
405 if (isl_int_is_zero(bmap
->div
[k
][0]))
407 if (!isl_int_is_zero(bmap
->div
[k
][1 + 1 + pos
]))
408 return isl_bool_false
;
411 return isl_bool_true
;
414 /* Eliminate divs based on equalities
416 static __isl_give isl_basic_map
*eliminate_divs_eq(
417 __isl_take isl_basic_map
*bmap
, int *progress
)
424 bmap
= isl_basic_map_order_divs(bmap
);
429 off
= isl_basic_map_offset(bmap
, isl_dim_div
);
431 for (d
= bmap
->n_div
- 1; d
>= 0 ; --d
) {
432 for (i
= 0; i
< bmap
->n_eq
; ++i
) {
435 if (!isl_int_is_one(bmap
->eq
[i
][off
+ d
]) &&
436 !isl_int_is_negone(bmap
->eq
[i
][off
+ d
]))
438 ok
= ok_to_eliminate_div(bmap
, bmap
->eq
[i
], d
);
440 return isl_basic_map_free(bmap
);
445 bmap
= eliminate_div(bmap
, bmap
->eq
[i
], d
, 1);
446 if (isl_basic_map_drop_equality(bmap
, i
) < 0)
447 return isl_basic_map_free(bmap
);
452 return eliminate_divs_eq(bmap
, progress
);
456 /* Eliminate divs based on inequalities
458 static __isl_give isl_basic_map
*eliminate_divs_ineq(
459 __isl_take isl_basic_map
*bmap
, int *progress
)
470 off
= isl_basic_map_offset(bmap
, isl_dim_div
);
472 for (d
= bmap
->n_div
- 1; d
>= 0 ; --d
) {
473 for (i
= 0; i
< bmap
->n_eq
; ++i
)
474 if (!isl_int_is_zero(bmap
->eq
[i
][off
+ d
]))
478 for (i
= 0; i
< bmap
->n_ineq
; ++i
)
479 if (isl_int_abs_gt(bmap
->ineq
[i
][off
+ d
], ctx
->one
))
481 if (i
< bmap
->n_ineq
)
484 bmap
= isl_basic_map_eliminate_vars(bmap
, (off
-1)+d
, 1);
485 if (!bmap
|| ISL_F_ISSET(bmap
, ISL_BASIC_MAP_EMPTY
))
487 bmap
= isl_basic_map_drop_div(bmap
, d
);
494 /* Does the equality constraint at position "eq" in "bmap" involve
495 * any local variables in the range [first, first + n)
496 * that are not marked as having an explicit representation?
498 static isl_bool
bmap_eq_involves_unknown_divs(__isl_keep isl_basic_map
*bmap
,
499 int eq
, unsigned first
, unsigned n
)
505 return isl_bool_error
;
507 o_div
= isl_basic_map_offset(bmap
, isl_dim_div
);
508 for (i
= 0; i
< n
; ++i
) {
511 if (isl_int_is_zero(bmap
->eq
[eq
][o_div
+ first
+ i
]))
513 unknown
= isl_basic_map_div_is_marked_unknown(bmap
, first
+ i
);
515 return isl_bool_error
;
517 return isl_bool_true
;
520 return isl_bool_false
;
523 /* The last local variable involved in the equality constraint
524 * at position "eq" in "bmap" is the local variable at position "div".
525 * It can therefore be used to extract an explicit representation
527 * Do so unless the local variable already has an explicit representation or
528 * the explicit representation would involve any other local variables
529 * that in turn do not have an explicit representation.
530 * An equality constraint involving local variables without an explicit
531 * representation can be used in isl_basic_map_drop_redundant_divs
532 * to separate out an independent local variable. Introducing
533 * an explicit representation here would block this transformation,
534 * while the partial explicit representation in itself is not very useful.
535 * Set *progress if anything is changed.
537 * The equality constraint is of the form
541 * with n a positive number. The explicit representation derived from
546 static __isl_give isl_basic_map
*set_div_from_eq(__isl_take isl_basic_map
*bmap
,
547 int div
, int eq
, int *progress
)
556 if (!isl_int_is_zero(bmap
->div
[div
][0]))
559 involves
= bmap_eq_involves_unknown_divs(bmap
, eq
, 0, div
);
561 return isl_basic_map_free(bmap
);
565 total
= isl_basic_map_dim(bmap
, isl_dim_all
);
567 return isl_basic_map_free(bmap
);
568 o_div
= isl_basic_map_offset(bmap
, isl_dim_div
);
569 isl_seq_neg(bmap
->div
[div
] + 1, bmap
->eq
[eq
], 1 + total
);
570 isl_int_set_si(bmap
->div
[div
][1 + o_div
+ div
], 0);
571 isl_int_set(bmap
->div
[div
][0], bmap
->eq
[eq
][o_div
+ div
]);
578 /* Perform fangcheng (Gaussian elimination) on the equality
579 * constraints of "bmap".
580 * That is, put them into row-echelon form, starting from the last column
581 * backward and use them to eliminate the corresponding coefficients
582 * from all constraints.
584 * If "progress" is not NULL, then it gets set if the elimination
585 * results in any changes.
586 * The elimination process may result in some equality constraints
587 * getting interchanged or removed.
588 * If "swap" or "drop" are not NULL, then they get called when
589 * two equality constraints get interchanged or
590 * when a number of final equality constraints get removed.
591 * As a special case, if the input turns out to be empty,
592 * then drop gets called with the number of removed equality
593 * constraints set to the total number of equality constraints.
594 * If "swap" or "drop" are not NULL, then the local variables (if any)
595 * are assumed to be in a valid order.
597 __isl_give isl_basic_map
*isl_basic_map_gauss5(__isl_take isl_basic_map
*bmap
,
599 isl_stat (*swap
)(unsigned a
, unsigned b
, void *user
),
600 isl_stat (*drop
)(unsigned n
, void *user
), void *user
)
610 bmap
= isl_basic_map_order_divs(bmap
);
612 total
= isl_basic_map_dim(bmap
, isl_dim_all
);
614 return isl_basic_map_free(bmap
);
616 total_var
= total
- bmap
->n_div
;
618 last_var
= total
- 1;
619 for (done
= 0; done
< bmap
->n_eq
; ++done
) {
620 for (; last_var
>= 0; --last_var
) {
621 for (k
= done
; k
< bmap
->n_eq
; ++k
)
622 if (!isl_int_is_zero(bmap
->eq
[k
][1+last_var
]))
630 swap_equality(bmap
, k
, done
);
631 if (swap
&& swap(k
, done
, user
) < 0)
632 return isl_basic_map_free(bmap
);
634 if (isl_int_is_neg(bmap
->eq
[done
][1+last_var
]))
635 isl_seq_neg(bmap
->eq
[done
], bmap
->eq
[done
], 1+total
);
637 bmap
= eliminate_var_using_equality(bmap
, last_var
,
638 bmap
->eq
[done
], 1, progress
);
640 if (last_var
>= total_var
)
641 bmap
= set_div_from_eq(bmap
, last_var
- total_var
,
646 if (done
== bmap
->n_eq
)
648 for (k
= done
; k
< bmap
->n_eq
; ++k
) {
649 if (isl_int_is_zero(bmap
->eq
[k
][0]))
651 if (drop
&& drop(bmap
->n_eq
, user
) < 0)
652 return isl_basic_map_free(bmap
);
653 return isl_basic_map_set_to_empty(bmap
);
655 n_drop
= bmap
->n_eq
- done
;
656 bmap
= isl_basic_map_free_equality(bmap
, n_drop
);
657 if (drop
&& drop(n_drop
, user
) < 0)
658 return isl_basic_map_free(bmap
);
662 __isl_give isl_basic_map
*isl_basic_map_gauss(__isl_take isl_basic_map
*bmap
,
665 return isl_basic_map_gauss5(bmap
, progress
, NULL
, NULL
, NULL
);
668 __isl_give isl_basic_set
*isl_basic_set_gauss(
669 __isl_take isl_basic_set
*bset
, int *progress
)
671 return bset_from_bmap(isl_basic_map_gauss(bset_to_bmap(bset
),
676 static unsigned int round_up(unsigned int v
)
687 /* Hash table of inequalities in a basic map.
688 * "index" is an array of addresses of inequalities in the basic map, some
689 * of which are NULL. The inequalities are hashed on the coefficients
690 * except the constant term.
691 * "size" is the number of elements in the array and is always a power of two
692 * "bits" is the number of bits need to represent an index into the array.
693 * "total" is the total dimension of the basic map.
695 struct isl_constraint_index
{
702 /* Fill in the "ci" data structure for holding the inequalities of "bmap".
704 static isl_stat
create_constraint_index(struct isl_constraint_index
*ci
,
705 __isl_keep isl_basic_map
*bmap
)
711 return isl_stat_error
;
712 ci
->total
= isl_basic_map_dim(bmap
, isl_dim_all
);
714 return isl_stat_error
;
715 if (bmap
->n_ineq
== 0)
717 ci
->size
= round_up(4 * (bmap
->n_ineq
+ 1) / 3 - 1);
718 ci
->bits
= ffs(ci
->size
) - 1;
719 ctx
= isl_basic_map_get_ctx(bmap
);
720 ci
->index
= isl_calloc_array(ctx
, isl_int
**, ci
->size
);
722 return isl_stat_error
;
727 /* Free the memory allocated by create_constraint_index.
729 static void constraint_index_free(struct isl_constraint_index
*ci
)
734 /* Return the position in ci->index that contains the address of
735 * an inequality that is equal to *ineq up to the constant term,
736 * provided this address is not identical to "ineq".
737 * If there is no such inequality, then return the position where
738 * such an inequality should be inserted.
740 static int hash_index_ineq(struct isl_constraint_index
*ci
, isl_int
**ineq
)
743 uint32_t hash
= isl_seq_get_hash_bits((*ineq
) + 1, ci
->total
, ci
->bits
);
744 for (h
= hash
; ci
->index
[h
]; h
= (h
+1) % ci
->size
)
745 if (ineq
!= ci
->index
[h
] &&
746 isl_seq_eq((*ineq
) + 1, ci
->index
[h
][0]+1, ci
->total
))
751 /* Return the position in ci->index that contains the address of
752 * an inequality that is equal to the k'th inequality of "bmap"
753 * up to the constant term, provided it does not point to the very
755 * If there is no such inequality, then return the position where
756 * such an inequality should be inserted.
758 static int hash_index(struct isl_constraint_index
*ci
,
759 __isl_keep isl_basic_map
*bmap
, int k
)
761 return hash_index_ineq(ci
, &bmap
->ineq
[k
]);
764 static int set_hash_index(struct isl_constraint_index
*ci
,
765 __isl_keep isl_basic_set
*bset
, int k
)
767 return hash_index(ci
, bset
, k
);
770 /* Fill in the "ci" data structure with the inequalities of "bset".
772 static isl_stat
setup_constraint_index(struct isl_constraint_index
*ci
,
773 __isl_keep isl_basic_set
*bset
)
777 if (create_constraint_index(ci
, bset
) < 0)
778 return isl_stat_error
;
780 for (k
= 0; k
< bset
->n_ineq
; ++k
) {
781 h
= set_hash_index(ci
, bset
, k
);
782 ci
->index
[h
] = &bset
->ineq
[k
];
788 /* Is the inequality ineq (obviously) redundant with respect
789 * to the constraints in "ci"?
791 * Look for an inequality in "ci" with the same coefficients and then
792 * check if the contant term of "ineq" is greater than or equal
793 * to the constant term of that inequality. If so, "ineq" is clearly
796 * Note that hash_index_ineq ignores a stored constraint if it has
797 * the same address as the passed inequality. It is ok to pass
798 * the address of a local variable here since it will never be
799 * the same as the address of a constraint in "ci".
801 static isl_bool
constraint_index_is_redundant(struct isl_constraint_index
*ci
,
806 h
= hash_index_ineq(ci
, &ineq
);
808 return isl_bool_false
;
809 return isl_int_ge(ineq
[0], (*ci
->index
[h
])[0]);
812 /* If we can eliminate more than one div, then we need to make
813 * sure we do it from last div to first div, in order not to
814 * change the position of the other divs that still need to
817 static __isl_give isl_basic_map
*remove_duplicate_divs(
818 __isl_take isl_basic_map
*bmap
, int *progress
)
830 bmap
= isl_basic_map_order_divs(bmap
);
831 if (!bmap
|| bmap
->n_div
<= 1)
834 v_div
= isl_basic_map_var_offset(bmap
, isl_dim_div
);
836 return isl_basic_map_free(bmap
);
837 total
= v_div
+ bmap
->n_div
;
840 for (k
= bmap
->n_div
- 1; k
>= 0; --k
)
841 if (!isl_int_is_zero(bmap
->div
[k
][0]))
846 size
= round_up(4 * bmap
->n_div
/ 3 - 1);
849 elim_for
= isl_calloc_array(ctx
, int, bmap
->n_div
);
850 bits
= ffs(size
) - 1;
851 index
= isl_calloc_array(ctx
, int, size
);
852 if (!elim_for
|| !index
)
854 eq
= isl_blk_alloc(ctx
, 1+total
);
855 if (isl_blk_is_error(eq
))
858 isl_seq_clr(eq
.data
, 1+total
);
859 index
[isl_seq_get_hash_bits(bmap
->div
[k
], 2+total
, bits
)] = k
+ 1;
860 for (--k
; k
>= 0; --k
) {
863 if (isl_int_is_zero(bmap
->div
[k
][0]))
866 hash
= isl_seq_get_hash_bits(bmap
->div
[k
], 2+total
, bits
);
867 for (h
= hash
; index
[h
]; h
= (h
+1) % size
)
868 if (isl_seq_eq(bmap
->div
[k
],
869 bmap
->div
[index
[h
]-1], 2+total
))
878 for (l
= bmap
->n_div
- 1; l
>= 0; --l
) {
882 isl_int_set_si(eq
.data
[1 + v_div
+ k
], -1);
883 isl_int_set_si(eq
.data
[1 + v_div
+ l
], 1);
884 bmap
= eliminate_div(bmap
, eq
.data
, l
, 1);
887 isl_int_set_si(eq
.data
[1 + v_div
+ k
], 0);
888 isl_int_set_si(eq
.data
[1 + v_div
+ l
], 0);
891 isl_blk_free(ctx
, eq
);
898 static int n_pure_div_eq(__isl_keep isl_basic_map
*bmap
)
903 v_div
= isl_basic_map_var_offset(bmap
, isl_dim_div
);
906 for (i
= 0, j
= bmap
->n_div
-1; i
< bmap
->n_eq
; ++i
) {
907 while (j
>= 0 && isl_int_is_zero(bmap
->eq
[i
][1 + v_div
+ j
]))
911 if (isl_seq_first_non_zero(bmap
->eq
[i
] + 1 + v_div
, j
) != -1)
917 /* Normalize divs that appear in equalities.
919 * In particular, we assume that bmap contains some equalities
924 * and we want to replace the set of e_i by a minimal set and
925 * such that the new e_i have a canonical representation in terms
927 * If any of the equalities involves more than one divs, then
928 * we currently simply bail out.
930 * Let us first additionally assume that all equalities involve
931 * a div. The equalities then express modulo constraints on the
932 * remaining variables and we can use "parameter compression"
933 * to find a minimal set of constraints. The result is a transformation
935 * x = T(x') = x_0 + G x'
937 * with G a lower-triangular matrix with all elements below the diagonal
938 * non-negative and smaller than the diagonal element on the same row.
939 * We first normalize x_0 by making the same property hold in the affine
941 * The rows i of G with a 1 on the diagonal do not impose any modulo
942 * constraint and simply express x_i = x'_i.
943 * For each of the remaining rows i, we introduce a div and a corresponding
944 * equality. In particular
946 * g_ii e_j = x_i - g_i(x')
948 * where each x'_k is replaced either by x_k (if g_kk = 1) or the
949 * corresponding div (if g_kk != 1).
951 * If there are any equalities not involving any div, then we
952 * first apply a variable compression on the variables x:
954 * x = C x'' x'' = C_2 x
956 * and perform the above parameter compression on A C instead of on A.
957 * The resulting compression is then of the form
959 * x'' = T(x') = x_0 + G x'
961 * and in constructing the new divs and the corresponding equalities,
962 * we have to replace each x'', i.e., the x'_k with (g_kk = 1),
963 * by the corresponding row from C_2.
965 static __isl_give isl_basic_map
*normalize_divs(__isl_take isl_basic_map
*bmap
,
973 struct isl_mat
*T
= NULL
;
974 struct isl_mat
*C
= NULL
;
975 struct isl_mat
*C2
= NULL
;
983 if (bmap
->n_div
== 0)
989 if (ISL_F_ISSET(bmap
, ISL_BASIC_MAP_NORMALIZED_DIVS
))
992 v_div
= isl_basic_map_var_offset(bmap
, isl_dim_div
);
993 div_eq
= n_pure_div_eq(bmap
);
994 if (v_div
< 0 || div_eq
< 0)
995 return isl_basic_map_free(bmap
);
999 if (div_eq
< bmap
->n_eq
) {
1000 B
= isl_mat_sub_alloc6(bmap
->ctx
, bmap
->eq
, div_eq
,
1001 bmap
->n_eq
- div_eq
, 0, 1 + v_div
);
1002 C
= isl_mat_variable_compression(B
, &C2
);
1005 if (C
->n_col
== 0) {
1006 bmap
= isl_basic_map_set_to_empty(bmap
);
1013 d
= isl_vec_alloc(bmap
->ctx
, div_eq
);
1016 for (i
= 0, j
= bmap
->n_div
-1; i
< div_eq
; ++i
) {
1017 while (j
>= 0 && isl_int_is_zero(bmap
->eq
[i
][1 + v_div
+ j
]))
1019 isl_int_set(d
->block
.data
[i
], bmap
->eq
[i
][1 + v_div
+ j
]);
1021 B
= isl_mat_sub_alloc6(bmap
->ctx
, bmap
->eq
, 0, div_eq
, 0, 1 + v_div
);
1024 B
= isl_mat_product(B
, C
);
1028 T
= isl_mat_parameter_compression(B
, d
);
1031 if (T
->n_col
== 0) {
1032 bmap
= isl_basic_map_set_to_empty(bmap
);
1038 for (i
= 0; i
< T
->n_row
- 1; ++i
) {
1039 isl_int_fdiv_q(v
, T
->row
[1 + i
][0], T
->row
[1 + i
][1 + i
]);
1040 if (isl_int_is_zero(v
))
1042 isl_mat_col_submul(T
, 0, v
, 1 + i
);
1045 pos
= isl_alloc_array(bmap
->ctx
, int, T
->n_row
);
1048 /* We have to be careful because dropping equalities may reorder them */
1050 for (j
= bmap
->n_div
- 1; j
>= 0; --j
) {
1051 for (i
= 0; i
< bmap
->n_eq
; ++i
)
1052 if (!isl_int_is_zero(bmap
->eq
[i
][1 + v_div
+ j
]))
1054 if (i
< bmap
->n_eq
) {
1055 bmap
= isl_basic_map_drop_div(bmap
, j
);
1056 if (isl_basic_map_drop_equality(bmap
, i
) < 0)
1063 for (i
= 1; i
< T
->n_row
; ++i
) {
1064 if (isl_int_is_one(T
->row
[i
][i
]))
1069 if (needed
> dropped
) {
1070 bmap
= isl_basic_map_extend(bmap
, needed
, needed
, 0);
1074 for (i
= 1; i
< T
->n_row
; ++i
) {
1075 if (isl_int_is_one(T
->row
[i
][i
]))
1077 k
= isl_basic_map_alloc_div(bmap
);
1078 pos
[i
] = 1 + v_div
+ k
;
1079 isl_seq_clr(bmap
->div
[k
] + 1, 1 + v_div
+ bmap
->n_div
);
1080 isl_int_set(bmap
->div
[k
][0], T
->row
[i
][i
]);
1082 isl_seq_cpy(bmap
->div
[k
] + 1, C2
->row
[i
], 1 + v_div
);
1084 isl_int_set_si(bmap
->div
[k
][1 + i
], 1);
1085 for (j
= 0; j
< i
; ++j
) {
1086 if (isl_int_is_zero(T
->row
[i
][j
]))
1088 if (pos
[j
] < T
->n_row
&& C2
)
1089 isl_seq_submul(bmap
->div
[k
] + 1, T
->row
[i
][j
],
1090 C2
->row
[pos
[j
]], 1 + v_div
);
1092 isl_int_neg(bmap
->div
[k
][1 + pos
[j
]],
1095 j
= isl_basic_map_alloc_equality(bmap
);
1096 isl_seq_neg(bmap
->eq
[j
], bmap
->div
[k
]+1, 1+v_div
+bmap
->n_div
);
1097 isl_int_set(bmap
->eq
[j
][pos
[i
]], bmap
->div
[k
][0]);
1106 ISL_F_SET(bmap
, ISL_BASIC_MAP_NORMALIZED_DIVS
);
1114 isl_basic_map_free(bmap
);
1118 static __isl_give isl_basic_map
*set_div_from_lower_bound(
1119 __isl_take isl_basic_map
*bmap
, int div
, int ineq
)
1121 unsigned total
= isl_basic_map_offset(bmap
, isl_dim_div
);
1123 isl_seq_neg(bmap
->div
[div
] + 1, bmap
->ineq
[ineq
], total
+ bmap
->n_div
);
1124 isl_int_set(bmap
->div
[div
][0], bmap
->ineq
[ineq
][total
+ div
]);
1125 isl_int_add(bmap
->div
[div
][1], bmap
->div
[div
][1], bmap
->div
[div
][0]);
1126 isl_int_sub_ui(bmap
->div
[div
][1], bmap
->div
[div
][1], 1);
1127 isl_int_set_si(bmap
->div
[div
][1 + total
+ div
], 0);
1132 /* Check whether it is ok to define a div based on an inequality.
1133 * To avoid the introduction of circular definitions of divs, we
1134 * do not allow such a definition if the resulting expression would refer to
1135 * any other undefined divs or if any known div is defined in
1136 * terms of the unknown div.
1138 static isl_bool
ok_to_set_div_from_bound(__isl_keep isl_basic_map
*bmap
,
1142 unsigned total
= isl_basic_map_offset(bmap
, isl_dim_div
);
1144 /* Not defined in terms of unknown divs */
1145 for (j
= 0; j
< bmap
->n_div
; ++j
) {
1148 if (isl_int_is_zero(bmap
->ineq
[ineq
][total
+ j
]))
1150 if (isl_int_is_zero(bmap
->div
[j
][0]))
1151 return isl_bool_false
;
1154 /* No other div defined in terms of this one => avoid loops */
1155 for (j
= 0; j
< bmap
->n_div
; ++j
) {
1158 if (isl_int_is_zero(bmap
->div
[j
][0]))
1160 if (!isl_int_is_zero(bmap
->div
[j
][1 + total
+ div
]))
1161 return isl_bool_false
;
1164 return isl_bool_true
;
1167 /* Would an expression for div "div" based on inequality "ineq" of "bmap"
1168 * be a better expression than the current one?
1170 * If we do not have any expression yet, then any expression would be better.
1171 * Otherwise we check if the last variable involved in the inequality
1172 * (disregarding the div that it would define) is in an earlier position
1173 * than the last variable involved in the current div expression.
1175 static isl_bool
better_div_constraint(__isl_keep isl_basic_map
*bmap
,
1178 unsigned total
= isl_basic_map_offset(bmap
, isl_dim_div
);
1182 if (isl_int_is_zero(bmap
->div
[div
][0]))
1183 return isl_bool_true
;
1185 if (isl_seq_last_non_zero(bmap
->ineq
[ineq
] + total
+ div
+ 1,
1186 bmap
->n_div
- (div
+ 1)) >= 0)
1187 return isl_bool_false
;
1189 last_ineq
= isl_seq_last_non_zero(bmap
->ineq
[ineq
], total
+ div
);
1190 last_div
= isl_seq_last_non_zero(bmap
->div
[div
] + 1,
1191 total
+ bmap
->n_div
);
1193 return last_ineq
< last_div
;
1196 /* Given two constraints "k" and "l" that are opposite to each other,
1197 * except for the constant term, check if we can use them
1198 * to obtain an expression for one of the hitherto unknown divs or
1199 * a "better" expression for a div for which we already have an expression.
1200 * "sum" is the sum of the constant terms of the constraints.
1201 * If this sum is strictly smaller than the coefficient of one
1202 * of the divs, then this pair can be used to define the div.
1203 * To avoid the introduction of circular definitions of divs, we
1204 * do not use the pair if the resulting expression would refer to
1205 * any other undefined divs or if any known div is defined in
1206 * terms of the unknown div.
1208 static __isl_give isl_basic_map
*check_for_div_constraints(
1209 __isl_take isl_basic_map
*bmap
, int k
, int l
, isl_int sum
,
1213 unsigned total
= isl_basic_map_offset(bmap
, isl_dim_div
);
1215 for (i
= 0; i
< bmap
->n_div
; ++i
) {
1218 if (isl_int_is_zero(bmap
->ineq
[k
][total
+ i
]))
1220 if (isl_int_abs_ge(sum
, bmap
->ineq
[k
][total
+ i
]))
1222 set_div
= better_div_constraint(bmap
, i
, k
);
1223 if (set_div
>= 0 && set_div
)
1224 set_div
= ok_to_set_div_from_bound(bmap
, i
, k
);
1226 return isl_basic_map_free(bmap
);
1229 if (isl_int_is_pos(bmap
->ineq
[k
][total
+ i
]))
1230 bmap
= set_div_from_lower_bound(bmap
, i
, k
);
1232 bmap
= set_div_from_lower_bound(bmap
, i
, l
);
1240 __isl_give isl_basic_map
*isl_basic_map_remove_duplicate_constraints(
1241 __isl_take isl_basic_map
*bmap
, int *progress
, int detect_divs
)
1243 struct isl_constraint_index ci
;
1245 isl_size total
= isl_basic_map_dim(bmap
, isl_dim_all
);
1248 if (total
< 0 || bmap
->n_ineq
<= 1)
1251 if (create_constraint_index(&ci
, bmap
) < 0)
1254 h
= isl_seq_get_hash_bits(bmap
->ineq
[0] + 1, total
, ci
.bits
);
1255 ci
.index
[h
] = &bmap
->ineq
[0];
1256 for (k
= 1; k
< bmap
->n_ineq
; ++k
) {
1257 h
= hash_index(&ci
, bmap
, k
);
1259 ci
.index
[h
] = &bmap
->ineq
[k
];
1264 l
= ci
.index
[h
] - &bmap
->ineq
[0];
1265 if (isl_int_lt(bmap
->ineq
[k
][0], bmap
->ineq
[l
][0]))
1266 swap_inequality(bmap
, k
, l
);
1267 isl_basic_map_drop_inequality(bmap
, k
);
1271 for (k
= 0; bmap
&& k
< bmap
->n_ineq
-1; ++k
) {
1272 isl_seq_neg(bmap
->ineq
[k
]+1, bmap
->ineq
[k
]+1, total
);
1273 h
= hash_index(&ci
, bmap
, k
);
1274 isl_seq_neg(bmap
->ineq
[k
]+1, bmap
->ineq
[k
]+1, total
);
1277 l
= ci
.index
[h
] - &bmap
->ineq
[0];
1278 isl_int_add(sum
, bmap
->ineq
[k
][0], bmap
->ineq
[l
][0]);
1279 if (isl_int_is_pos(sum
)) {
1281 bmap
= check_for_div_constraints(bmap
, k
, l
,
1285 if (isl_int_is_zero(sum
)) {
1286 /* We need to break out of the loop after these
1287 * changes since the contents of the hash
1288 * will no longer be valid.
1289 * Plus, we probably we want to regauss first.
1293 isl_basic_map_drop_inequality(bmap
, l
);
1294 isl_basic_map_inequality_to_equality(bmap
, k
);
1296 bmap
= isl_basic_map_set_to_empty(bmap
);
1301 constraint_index_free(&ci
);
1305 /* Detect all pairs of inequalities that form an equality.
1307 * isl_basic_map_remove_duplicate_constraints detects at most one such pair.
1308 * Call it repeatedly while it is making progress.
1310 __isl_give isl_basic_map
*isl_basic_map_detect_inequality_pairs(
1311 __isl_take isl_basic_map
*bmap
, int *progress
)
1317 bmap
= isl_basic_map_remove_duplicate_constraints(bmap
,
1319 if (progress
&& duplicate
)
1321 } while (duplicate
);
1326 /* Given a known integer division "div" that is not integral
1327 * (with denominator 1), eliminate it from the constraints in "bmap"
1328 * where it appears with a (positive or negative) unit coefficient.
1329 * If "progress" is not NULL, then it gets set if the elimination
1330 * results in any changes.
1334 * floor(e/m) + f >= 0
1342 * -floor(e/m) + f >= 0
1346 * -e + m f + m - 1 >= 0
1348 * The first conversion is valid because floor(e/m) >= -f is equivalent
1349 * to e/m >= -f because -f is an integral expression.
1350 * The second conversion follows from the fact that
1352 * -floor(e/m) = ceil(-e/m) = floor((-e + m - 1)/m)
1355 * Note that one of the div constraints may have been eliminated
1356 * due to being redundant with respect to the constraint that is
1357 * being modified by this function. The modified constraint may
1358 * no longer imply this div constraint, so we add it back to make
1359 * sure we do not lose any information.
1361 static __isl_give isl_basic_map
*eliminate_unit_div(
1362 __isl_take isl_basic_map
*bmap
, int div
, int *progress
)
1365 isl_size v_div
, dim
;
1368 v_div
= isl_basic_map_var_offset(bmap
, isl_dim_div
);
1369 dim
= isl_basic_map_dim(bmap
, isl_dim_all
);
1370 if (v_div
< 0 || dim
< 0)
1371 return isl_basic_map_free(bmap
);
1373 ctx
= isl_basic_map_get_ctx(bmap
);
1375 for (j
= 0; j
< bmap
->n_ineq
; ++j
) {
1378 if (!isl_int_is_one(bmap
->ineq
[j
][1 + v_div
+ div
]) &&
1379 !isl_int_is_negone(bmap
->ineq
[j
][1 + v_div
+ div
]))
1385 s
= isl_int_sgn(bmap
->ineq
[j
][1 + v_div
+ div
]);
1386 isl_int_set_si(bmap
->ineq
[j
][1 + v_div
+ div
], 0);
1388 isl_seq_combine(bmap
->ineq
[j
],
1389 ctx
->negone
, bmap
->div
[div
] + 1,
1390 bmap
->div
[div
][0], bmap
->ineq
[j
], 1 + dim
);
1392 isl_seq_combine(bmap
->ineq
[j
],
1393 ctx
->one
, bmap
->div
[div
] + 1,
1394 bmap
->div
[div
][0], bmap
->ineq
[j
], 1 + dim
);
1396 isl_int_add(bmap
->ineq
[j
][0],
1397 bmap
->ineq
[j
][0], bmap
->div
[div
][0]);
1398 isl_int_sub_ui(bmap
->ineq
[j
][0],
1399 bmap
->ineq
[j
][0], 1);
1402 bmap
= isl_basic_map_extend_constraints(bmap
, 0, 1);
1403 bmap
= isl_basic_map_add_div_constraint(bmap
, div
, s
);
1411 /* Eliminate selected known divs from constraints where they appear with
1412 * a (positive or negative) unit coefficient.
1413 * In particular, only handle those for which "select" returns isl_bool_true.
1414 * If "progress" is not NULL, then it gets set if the elimination
1415 * results in any changes.
1417 * We skip integral divs, i.e., those with denominator 1, as we would
1418 * risk eliminating the div from the div constraints. We do not need
1419 * to handle those divs here anyway since the div constraints will turn
1420 * out to form an equality and this equality can then be used to eliminate
1421 * the div from all constraints.
1423 static __isl_give isl_basic_map
*eliminate_selected_unit_divs(
1424 __isl_take isl_basic_map
*bmap
,
1425 isl_bool (*select
)(__isl_keep isl_basic_map
*bmap
, int div
),
1433 for (i
= 0; i
< bmap
->n_div
; ++i
) {
1436 if (isl_int_is_zero(bmap
->div
[i
][0]))
1438 if (isl_int_is_one(bmap
->div
[i
][0]))
1440 selected
= select(bmap
, i
);
1442 return isl_basic_map_free(bmap
);
1445 bmap
= eliminate_unit_div(bmap
, i
, progress
);
1453 /* eliminate_selected_unit_divs callback that selects every
1456 static isl_bool
is_any_div(__isl_keep isl_basic_map
*bmap
, int div
)
1458 return isl_bool_true
;
1461 /* Eliminate known divs from constraints where they appear with
1462 * a (positive or negative) unit coefficient.
1463 * If "progress" is not NULL, then it gets set if the elimination
1464 * results in any changes.
1466 static __isl_give isl_basic_map
*eliminate_unit_divs(
1467 __isl_take isl_basic_map
*bmap
, int *progress
)
1469 return eliminate_selected_unit_divs(bmap
, &is_any_div
, progress
);
1472 /* eliminate_selected_unit_divs callback that selects
1473 * integer divisions that only appear with
1474 * a (positive or negative) unit coefficient
1475 * (outside their div constraints).
1477 static isl_bool
is_pure_unit_div(__isl_keep isl_basic_map
*bmap
, int div
)
1480 isl_size v_div
, n_ineq
;
1482 v_div
= isl_basic_map_var_offset(bmap
, isl_dim_div
);
1483 n_ineq
= isl_basic_map_n_inequality(bmap
);
1484 if (v_div
< 0 || n_ineq
< 0)
1485 return isl_bool_error
;
1487 for (i
= 0; i
< n_ineq
; ++i
) {
1490 if (isl_int_is_zero(bmap
->ineq
[i
][1 + v_div
+ div
]))
1492 skip
= isl_basic_map_is_div_constraint(bmap
,
1493 bmap
->ineq
[i
], div
);
1495 return isl_bool_error
;
1498 if (!isl_int_is_one(bmap
->ineq
[i
][1 + v_div
+ div
]) &&
1499 !isl_int_is_negone(bmap
->ineq
[i
][1 + v_div
+ div
]))
1500 return isl_bool_false
;
1503 return isl_bool_true
;
1506 /* Eliminate known divs from constraints where they appear with
1507 * a (positive or negative) unit coefficient,
1508 * but only if they do not appear in any other constraints
1509 * (other than the div constraints).
1511 __isl_give isl_basic_map
*isl_basic_map_eliminate_pure_unit_divs(
1512 __isl_take isl_basic_map
*bmap
)
1514 return eliminate_selected_unit_divs(bmap
, &is_pure_unit_div
, NULL
);
1517 __isl_give isl_basic_map
*isl_basic_map_simplify(__isl_take isl_basic_map
*bmap
)
1526 empty
= isl_basic_map_plain_is_empty(bmap
);
1528 return isl_basic_map_free(bmap
);
1531 bmap
= isl_basic_map_normalize_constraints(bmap
);
1532 bmap
= reduce_div_coefficients(bmap
);
1533 bmap
= normalize_div_expressions(bmap
);
1534 bmap
= remove_duplicate_divs(bmap
, &progress
);
1535 bmap
= eliminate_unit_divs(bmap
, &progress
);
1536 bmap
= eliminate_divs_eq(bmap
, &progress
);
1537 bmap
= eliminate_divs_ineq(bmap
, &progress
);
1538 bmap
= isl_basic_map_gauss(bmap
, &progress
);
1539 /* requires equalities in normal form */
1540 bmap
= normalize_divs(bmap
, &progress
);
1541 bmap
= isl_basic_map_remove_duplicate_constraints(bmap
,
1547 __isl_give isl_basic_set
*isl_basic_set_simplify(
1548 __isl_take isl_basic_set
*bset
)
1550 return bset_from_bmap(isl_basic_map_simplify(bset_to_bmap(bset
)));
1554 isl_bool
isl_basic_map_is_div_constraint(__isl_keep isl_basic_map
*bmap
,
1555 isl_int
*constraint
, unsigned div
)
1560 return isl_bool_error
;
1562 pos
= isl_basic_map_offset(bmap
, isl_dim_div
) + div
;
1564 if (isl_int_eq(constraint
[pos
], bmap
->div
[div
][0])) {
1566 isl_int_sub(bmap
->div
[div
][1],
1567 bmap
->div
[div
][1], bmap
->div
[div
][0]);
1568 isl_int_add_ui(bmap
->div
[div
][1], bmap
->div
[div
][1], 1);
1569 neg
= isl_seq_is_neg(constraint
, bmap
->div
[div
]+1, pos
);
1570 isl_int_sub_ui(bmap
->div
[div
][1], bmap
->div
[div
][1], 1);
1571 isl_int_add(bmap
->div
[div
][1],
1572 bmap
->div
[div
][1], bmap
->div
[div
][0]);
1574 return isl_bool_false
;
1575 if (isl_seq_first_non_zero(constraint
+pos
+1,
1576 bmap
->n_div
-div
-1) != -1)
1577 return isl_bool_false
;
1578 } else if (isl_int_abs_eq(constraint
[pos
], bmap
->div
[div
][0])) {
1579 if (!isl_seq_eq(constraint
, bmap
->div
[div
]+1, pos
))
1580 return isl_bool_false
;
1581 if (isl_seq_first_non_zero(constraint
+pos
+1,
1582 bmap
->n_div
-div
-1) != -1)
1583 return isl_bool_false
;
1585 return isl_bool_false
;
1587 return isl_bool_true
;
1590 /* If the only constraints a div d=floor(f/m)
1591 * appears in are its two defining constraints
1594 * -(f - (m - 1)) + m d >= 0
1596 * then it can safely be removed.
1598 static isl_bool
div_is_redundant(__isl_keep isl_basic_map
*bmap
, int div
)
1601 isl_size v_div
= isl_basic_map_var_offset(bmap
, isl_dim_div
);
1602 unsigned pos
= 1 + v_div
+ div
;
1605 return isl_bool_error
;
1607 for (i
= 0; i
< bmap
->n_eq
; ++i
)
1608 if (!isl_int_is_zero(bmap
->eq
[i
][pos
]))
1609 return isl_bool_false
;
1611 for (i
= 0; i
< bmap
->n_ineq
; ++i
) {
1614 if (isl_int_is_zero(bmap
->ineq
[i
][pos
]))
1616 red
= isl_basic_map_is_div_constraint(bmap
, bmap
->ineq
[i
], div
);
1617 if (red
< 0 || !red
)
1621 for (i
= 0; i
< bmap
->n_div
; ++i
) {
1622 if (isl_int_is_zero(bmap
->div
[i
][0]))
1624 if (!isl_int_is_zero(bmap
->div
[i
][1+pos
]))
1625 return isl_bool_false
;
1628 return isl_bool_true
;
1632 * Remove divs that don't occur in any of the constraints or other divs.
1633 * These can arise when dropping constraints from a basic map or
1634 * when the divs of a basic map have been temporarily aligned
1635 * with the divs of another basic map.
1637 static __isl_give isl_basic_map
*remove_redundant_divs(
1638 __isl_take isl_basic_map
*bmap
)
1643 v_div
= isl_basic_map_var_offset(bmap
, isl_dim_div
);
1645 return isl_basic_map_free(bmap
);
1647 for (i
= bmap
->n_div
-1; i
>= 0; --i
) {
1650 redundant
= div_is_redundant(bmap
, i
);
1652 return isl_basic_map_free(bmap
);
1655 bmap
= isl_basic_map_drop_constraints_involving(bmap
,
1657 bmap
= isl_basic_map_drop_div(bmap
, i
);
1662 /* Mark "bmap" as final, without checking for obviously redundant
1663 * integer divisions. This function should be used when "bmap"
1664 * is known not to involve any such integer divisions.
1666 __isl_give isl_basic_map
*isl_basic_map_mark_final(
1667 __isl_take isl_basic_map
*bmap
)
1671 ISL_F_SET(bmap
, ISL_BASIC_SET_FINAL
);
1675 /* Mark "bmap" as final, after removing obviously redundant integer divisions.
1677 __isl_give isl_basic_map
*isl_basic_map_finalize(__isl_take isl_basic_map
*bmap
)
1679 bmap
= remove_redundant_divs(bmap
);
1680 bmap
= isl_basic_map_mark_final(bmap
);
1684 __isl_give isl_basic_set
*isl_basic_set_finalize(
1685 __isl_take isl_basic_set
*bset
)
1687 return bset_from_bmap(isl_basic_map_finalize(bset_to_bmap(bset
)));
1690 /* Remove definition of any div that is defined in terms of the given variable.
1691 * The div itself is not removed. Functions such as
1692 * eliminate_divs_ineq depend on the other divs remaining in place.
1694 static __isl_give isl_basic_map
*remove_dependent_vars(
1695 __isl_take isl_basic_map
*bmap
, int pos
)
1702 for (i
= 0; i
< bmap
->n_div
; ++i
) {
1703 if (isl_int_is_zero(bmap
->div
[i
][0]))
1705 if (isl_int_is_zero(bmap
->div
[i
][1+1+pos
]))
1707 bmap
= isl_basic_map_mark_div_unknown(bmap
, i
);
1714 /* Eliminate the specified variables from the constraints using
1715 * Fourier-Motzkin. The variables themselves are not removed.
1717 __isl_give isl_basic_map
*isl_basic_map_eliminate_vars(
1718 __isl_take isl_basic_map
*bmap
, unsigned pos
, unsigned n
)
1727 total
= isl_basic_map_dim(bmap
, isl_dim_all
);
1729 return isl_basic_map_free(bmap
);
1731 bmap
= isl_basic_map_cow(bmap
);
1732 for (d
= pos
+ n
- 1; d
>= 0 && d
>= pos
; --d
)
1733 bmap
= remove_dependent_vars(bmap
, d
);
1737 for (d
= pos
+ n
- 1;
1738 d
>= 0 && d
>= total
- bmap
->n_div
&& d
>= pos
; --d
)
1739 isl_seq_clr(bmap
->div
[d
-(total
-bmap
->n_div
)], 2+total
);
1740 for (d
= pos
+ n
- 1; d
>= 0 && d
>= pos
; --d
) {
1741 int n_lower
, n_upper
;
1744 for (i
= 0; i
< bmap
->n_eq
; ++i
) {
1745 if (isl_int_is_zero(bmap
->eq
[i
][1+d
]))
1747 bmap
= eliminate_var_using_equality(bmap
, d
,
1748 bmap
->eq
[i
], 0, NULL
);
1749 if (isl_basic_map_drop_equality(bmap
, i
) < 0)
1750 return isl_basic_map_free(bmap
);
1758 for (i
= 0; i
< bmap
->n_ineq
; ++i
) {
1759 if (isl_int_is_pos(bmap
->ineq
[i
][1+d
]))
1761 else if (isl_int_is_neg(bmap
->ineq
[i
][1+d
]))
1764 bmap
= isl_basic_map_extend_constraints(bmap
,
1765 0, n_lower
* n_upper
);
1768 for (i
= bmap
->n_ineq
- 1; i
>= 0; --i
) {
1770 if (isl_int_is_zero(bmap
->ineq
[i
][1+d
]))
1773 for (j
= 0; j
< i
; ++j
) {
1774 if (isl_int_is_zero(bmap
->ineq
[j
][1+d
]))
1777 if (isl_int_sgn(bmap
->ineq
[i
][1+d
]) ==
1778 isl_int_sgn(bmap
->ineq
[j
][1+d
]))
1780 k
= isl_basic_map_alloc_inequality(bmap
);
1783 isl_seq_cpy(bmap
->ineq
[k
], bmap
->ineq
[i
],
1785 isl_seq_elim(bmap
->ineq
[k
], bmap
->ineq
[j
],
1786 1+d
, 1+total
, NULL
);
1788 isl_basic_map_drop_inequality(bmap
, i
);
1791 if (n_lower
> 0 && n_upper
> 0) {
1792 bmap
= isl_basic_map_normalize_constraints(bmap
);
1793 bmap
= isl_basic_map_remove_duplicate_constraints(bmap
,
1795 bmap
= isl_basic_map_gauss(bmap
, NULL
);
1796 bmap
= isl_basic_map_remove_redundancies(bmap
);
1800 if (ISL_F_ISSET(bmap
, ISL_BASIC_MAP_EMPTY
))
1805 bmap
= isl_basic_map_gauss(bmap
, NULL
);
1808 isl_basic_map_free(bmap
);
1812 __isl_give isl_basic_set
*isl_basic_set_eliminate_vars(
1813 __isl_take isl_basic_set
*bset
, unsigned pos
, unsigned n
)
1815 return bset_from_bmap(isl_basic_map_eliminate_vars(bset_to_bmap(bset
),
1819 /* Eliminate the specified n dimensions starting at first from the
1820 * constraints, without removing the dimensions from the space.
1821 * If the set is rational, the dimensions are eliminated using Fourier-Motzkin.
1822 * Otherwise, they are projected out and the original space is restored.
1824 __isl_give isl_basic_map
*isl_basic_map_eliminate(
1825 __isl_take isl_basic_map
*bmap
,
1826 enum isl_dim_type type
, unsigned first
, unsigned n
)
1835 if (isl_basic_map_check_range(bmap
, type
, first
, n
) < 0)
1836 return isl_basic_map_free(bmap
);
1838 if (ISL_F_ISSET(bmap
, ISL_BASIC_MAP_RATIONAL
)) {
1839 first
+= isl_basic_map_offset(bmap
, type
) - 1;
1840 bmap
= isl_basic_map_eliminate_vars(bmap
, first
, n
);
1841 return isl_basic_map_finalize(bmap
);
1844 space
= isl_basic_map_get_space(bmap
);
1845 bmap
= isl_basic_map_project_out(bmap
, type
, first
, n
);
1846 bmap
= isl_basic_map_insert_dims(bmap
, type
, first
, n
);
1847 bmap
= isl_basic_map_reset_space(bmap
, space
);
1851 __isl_give isl_basic_set
*isl_basic_set_eliminate(
1852 __isl_take isl_basic_set
*bset
,
1853 enum isl_dim_type type
, unsigned first
, unsigned n
)
1855 return isl_basic_map_eliminate(bset
, type
, first
, n
);
1858 /* Remove all constraints from "bmap" that reference any unknown local
1859 * variables (directly or indirectly).
1861 * Dropping all constraints on a local variable will make it redundant,
1862 * so it will get removed implicitly by
1863 * isl_basic_map_drop_constraints_involving_dims. Some other local
1864 * variables may also end up becoming redundant if they only appear
1865 * in constraints together with the unknown local variable.
1866 * Therefore, start over after calling
1867 * isl_basic_map_drop_constraints_involving_dims.
1869 __isl_give isl_basic_map
*isl_basic_map_drop_constraints_involving_unknown_divs(
1870 __isl_take isl_basic_map
*bmap
)
1876 known
= isl_basic_map_divs_known(bmap
);
1878 return isl_basic_map_free(bmap
);
1882 n_div
= isl_basic_map_dim(bmap
, isl_dim_div
);
1884 return isl_basic_map_free(bmap
);
1885 o_div
= isl_basic_map_offset(bmap
, isl_dim_div
) - 1;
1887 for (i
= 0; i
< n_div
; ++i
) {
1888 known
= isl_basic_map_div_is_known(bmap
, i
);
1890 return isl_basic_map_free(bmap
);
1893 bmap
= remove_dependent_vars(bmap
, o_div
+ i
);
1894 bmap
= isl_basic_map_drop_constraints_involving_dims(bmap
,
1896 n_div
= isl_basic_map_dim(bmap
, isl_dim_div
);
1898 return isl_basic_map_free(bmap
);
1905 /* Remove all constraints from "bset" that reference any unknown local
1906 * variables (directly or indirectly).
1908 __isl_give isl_basic_set
*isl_basic_set_drop_constraints_involving_unknown_divs(
1909 __isl_take isl_basic_set
*bset
)
1911 isl_basic_map
*bmap
;
1913 bmap
= bset_to_bmap(bset
);
1914 bmap
= isl_basic_map_drop_constraints_involving_unknown_divs(bmap
);
1915 return bset_from_bmap(bmap
);
1918 /* Remove all constraints from "map" that reference any unknown local
1919 * variables (directly or indirectly).
1921 * Since constraints may get dropped from the basic maps,
1922 * they may no longer be disjoint from each other.
1924 __isl_give isl_map
*isl_map_drop_constraints_involving_unknown_divs(
1925 __isl_take isl_map
*map
)
1930 known
= isl_map_divs_known(map
);
1932 return isl_map_free(map
);
1936 map
= isl_map_cow(map
);
1940 for (i
= 0; i
< map
->n
; ++i
) {
1942 isl_basic_map_drop_constraints_involving_unknown_divs(
1945 return isl_map_free(map
);
1949 ISL_F_CLR(map
, ISL_MAP_DISJOINT
);
1954 /* Don't assume equalities are in order, because align_divs
1955 * may have changed the order of the divs.
1957 static void compute_elimination_index(__isl_keep isl_basic_map
*bmap
, int *elim
,
1962 for (d
= 0; d
< len
; ++d
)
1964 for (i
= 0; i
< bmap
->n_eq
; ++i
) {
1965 for (d
= len
- 1; d
>= 0; --d
) {
1966 if (isl_int_is_zero(bmap
->eq
[i
][1+d
]))
1974 static void set_compute_elimination_index(__isl_keep isl_basic_set
*bset
,
1975 int *elim
, unsigned len
)
1977 compute_elimination_index(bset_to_bmap(bset
), elim
, len
);
1980 static int reduced_using_equalities(isl_int
*dst
, isl_int
*src
,
1981 __isl_keep isl_basic_map
*bmap
, int *elim
, unsigned total
)
1986 for (d
= total
- 1; d
>= 0; --d
) {
1987 if (isl_int_is_zero(src
[1+d
]))
1992 isl_seq_cpy(dst
, src
, 1 + total
);
1995 isl_seq_elim(dst
, bmap
->eq
[elim
[d
]], 1 + d
, 1 + total
, NULL
);
2000 static int set_reduced_using_equalities(isl_int
*dst
, isl_int
*src
,
2001 __isl_keep isl_basic_set
*bset
, int *elim
, unsigned total
)
2003 return reduced_using_equalities(dst
, src
,
2004 bset_to_bmap(bset
), elim
, total
);
2007 static __isl_give isl_basic_set
*isl_basic_set_reduce_using_equalities(
2008 __isl_take isl_basic_set
*bset
, __isl_take isl_basic_set
*context
)
2014 if (!bset
|| !context
)
2017 if (context
->n_eq
== 0) {
2018 isl_basic_set_free(context
);
2022 bset
= isl_basic_set_cow(bset
);
2023 dim
= isl_basic_set_dim(bset
, isl_dim_set
);
2027 elim
= isl_alloc_array(bset
->ctx
, int, dim
);
2030 set_compute_elimination_index(context
, elim
, dim
);
2031 for (i
= 0; i
< bset
->n_eq
; ++i
)
2032 set_reduced_using_equalities(bset
->eq
[i
], bset
->eq
[i
],
2033 context
, elim
, dim
);
2034 for (i
= 0; i
< bset
->n_ineq
; ++i
)
2035 set_reduced_using_equalities(bset
->ineq
[i
], bset
->ineq
[i
],
2036 context
, elim
, dim
);
2037 isl_basic_set_free(context
);
2039 bset
= isl_basic_set_simplify(bset
);
2040 bset
= isl_basic_set_finalize(bset
);
2043 isl_basic_set_free(bset
);
2044 isl_basic_set_free(context
);
2048 /* For each inequality in "ineq" that is a shifted (more relaxed)
2049 * copy of an inequality in "context", mark the corresponding entry
2051 * If an inequality only has a non-negative constant term, then
2054 static isl_stat
mark_shifted_constraints(__isl_keep isl_mat
*ineq
,
2055 __isl_keep isl_basic_set
*context
, int *row
)
2057 struct isl_constraint_index ci
;
2058 isl_size n_ineq
, cols
;
2062 if (!ineq
|| !context
)
2063 return isl_stat_error
;
2064 if (context
->n_ineq
== 0)
2066 if (setup_constraint_index(&ci
, context
) < 0)
2067 return isl_stat_error
;
2069 n_ineq
= isl_mat_rows(ineq
);
2070 cols
= isl_mat_cols(ineq
);
2071 if (n_ineq
< 0 || cols
< 0)
2072 return isl_stat_error
;
2074 for (k
= 0; k
< n_ineq
; ++k
) {
2078 l
= isl_seq_first_non_zero(ineq
->row
[k
] + 1, total
);
2079 if (l
< 0 && isl_int_is_nonneg(ineq
->row
[k
][0])) {
2083 redundant
= constraint_index_is_redundant(&ci
, ineq
->row
[k
]);
2090 constraint_index_free(&ci
);
2093 constraint_index_free(&ci
);
2094 return isl_stat_error
;
2097 static __isl_give isl_basic_set
*remove_shifted_constraints(
2098 __isl_take isl_basic_set
*bset
, __isl_keep isl_basic_set
*context
)
2100 struct isl_constraint_index ci
;
2103 if (!bset
|| !context
)
2106 if (context
->n_ineq
== 0)
2108 if (setup_constraint_index(&ci
, context
) < 0)
2111 for (k
= 0; k
< bset
->n_ineq
; ++k
) {
2114 redundant
= constraint_index_is_redundant(&ci
, bset
->ineq
[k
]);
2119 bset
= isl_basic_set_cow(bset
);
2122 isl_basic_set_drop_inequality(bset
, k
);
2125 constraint_index_free(&ci
);
2128 constraint_index_free(&ci
);
2132 /* Remove constraints from "bmap" that are identical to constraints
2133 * in "context" or that are more relaxed (greater constant term).
2135 * We perform the test for shifted copies on the pure constraints
2136 * in remove_shifted_constraints.
2138 static __isl_give isl_basic_map
*isl_basic_map_remove_shifted_constraints(
2139 __isl_take isl_basic_map
*bmap
, __isl_take isl_basic_map
*context
)
2141 isl_basic_set
*bset
, *bset_context
;
2143 if (!bmap
|| !context
)
2146 if (bmap
->n_ineq
== 0 || context
->n_ineq
== 0) {
2147 isl_basic_map_free(context
);
2151 bmap
= isl_basic_map_order_divs(bmap
);
2152 context
= isl_basic_map_align_divs(context
, bmap
);
2153 bmap
= isl_basic_map_align_divs(bmap
, context
);
2155 bset
= isl_basic_map_underlying_set(isl_basic_map_copy(bmap
));
2156 bset_context
= isl_basic_map_underlying_set(context
);
2157 bset
= remove_shifted_constraints(bset
, bset_context
);
2158 isl_basic_set_free(bset_context
);
2160 bmap
= isl_basic_map_overlying_set(bset
, bmap
);
2164 isl_basic_map_free(bmap
);
2165 isl_basic_map_free(context
);
2169 /* Does the (linear part of a) constraint "c" involve any of the "len"
2170 * "relevant" dimensions?
2172 static int is_related(isl_int
*c
, int len
, int *relevant
)
2176 for (i
= 0; i
< len
; ++i
) {
2179 if (!isl_int_is_zero(c
[i
]))
2186 /* Drop constraints from "bmap" that do not involve any of
2187 * the dimensions marked "relevant".
2189 static __isl_give isl_basic_map
*drop_unrelated_constraints(
2190 __isl_take isl_basic_map
*bmap
, int *relevant
)
2195 dim
= isl_basic_map_dim(bmap
, isl_dim_all
);
2197 return isl_basic_map_free(bmap
);
2198 for (i
= 0; i
< dim
; ++i
)
2204 for (i
= bmap
->n_eq
- 1; i
>= 0; --i
)
2205 if (!is_related(bmap
->eq
[i
] + 1, dim
, relevant
)) {
2206 bmap
= isl_basic_map_cow(bmap
);
2207 if (isl_basic_map_drop_equality(bmap
, i
) < 0)
2208 return isl_basic_map_free(bmap
);
2211 for (i
= bmap
->n_ineq
- 1; i
>= 0; --i
)
2212 if (!is_related(bmap
->ineq
[i
] + 1, dim
, relevant
)) {
2213 bmap
= isl_basic_map_cow(bmap
);
2214 if (isl_basic_map_drop_inequality(bmap
, i
) < 0)
2215 return isl_basic_map_free(bmap
);
2221 /* Update the groups in "group" based on the (linear part of a) constraint "c".
2223 * In particular, for any variable involved in the constraint,
2224 * find the actual group id from before and replace the group
2225 * of the corresponding variable by the minimal group of all
2226 * the variables involved in the constraint considered so far
2227 * (if this minimum is smaller) or replace the minimum by this group
2228 * (if the minimum is larger).
2230 * At the end, all the variables in "c" will (indirectly) point
2231 * to the minimal of the groups that they referred to originally.
2233 static void update_groups(int dim
, int *group
, isl_int
*c
)
2238 for (j
= 0; j
< dim
; ++j
) {
2239 if (isl_int_is_zero(c
[j
]))
2241 while (group
[j
] >= 0 && group
[group
[j
]] != group
[j
])
2242 group
[j
] = group
[group
[j
]];
2243 if (group
[j
] == min
)
2245 if (group
[j
] < min
) {
2246 if (min
>= 0 && min
< dim
)
2247 group
[min
] = group
[j
];
2250 group
[group
[j
]] = min
;
2254 /* Allocate an array of groups of variables, one for each variable
2255 * in "context", initialized to zero.
2257 static int *alloc_groups(__isl_keep isl_basic_set
*context
)
2262 dim
= isl_basic_set_dim(context
, isl_dim_set
);
2265 ctx
= isl_basic_set_get_ctx(context
);
2266 return isl_calloc_array(ctx
, int, dim
);
2269 /* Drop constraints from "bmap" that only involve variables that are
2270 * not related to any of the variables marked with a "-1" in "group".
2272 * We construct groups of variables that collect variables that
2273 * (indirectly) appear in some common constraint of "bmap".
2274 * Each group is identified by the first variable in the group,
2275 * except for the special group of variables that was already identified
2276 * in the input as -1 (or are related to those variables).
2277 * If group[i] is equal to i (or -1), then the group of i is i (or -1),
2278 * otherwise the group of i is the group of group[i].
2280 * We first initialize groups for the remaining variables.
2281 * Then we iterate over the constraints of "bmap" and update the
2282 * group of the variables in the constraint by the smallest group.
2283 * Finally, we resolve indirect references to groups by running over
2286 * After computing the groups, we drop constraints that do not involve
2287 * any variables in the -1 group.
2289 __isl_give isl_basic_map
*isl_basic_map_drop_unrelated_constraints(
2290 __isl_take isl_basic_map
*bmap
, __isl_take
int *group
)
2296 dim
= isl_basic_map_dim(bmap
, isl_dim_all
);
2298 return isl_basic_map_free(bmap
);
2301 for (i
= 0; i
< dim
; ++i
)
2303 last
= group
[i
] = i
;
2309 for (i
= 0; i
< bmap
->n_eq
; ++i
)
2310 update_groups(dim
, group
, bmap
->eq
[i
] + 1);
2311 for (i
= 0; i
< bmap
->n_ineq
; ++i
)
2312 update_groups(dim
, group
, bmap
->ineq
[i
] + 1);
2314 for (i
= 0; i
< dim
; ++i
)
2316 group
[i
] = group
[group
[i
]];
2318 for (i
= 0; i
< dim
; ++i
)
2319 group
[i
] = group
[i
] == -1;
2321 bmap
= drop_unrelated_constraints(bmap
, group
);
2327 /* Drop constraints from "context" that are irrelevant for computing
2328 * the gist of "bset".
2330 * In particular, drop constraints in variables that are not related
2331 * to any of the variables involved in the constraints of "bset"
2332 * in the sense that there is no sequence of constraints that connects them.
2334 * We first mark all variables that appear in "bset" as belonging
2335 * to a "-1" group and then continue with group_and_drop_irrelevant_constraints.
2337 static __isl_give isl_basic_set
*drop_irrelevant_constraints(
2338 __isl_take isl_basic_set
*context
, __isl_keep isl_basic_set
*bset
)
2344 dim
= isl_basic_set_dim(bset
, isl_dim_set
);
2345 if (!context
|| dim
< 0)
2346 return isl_basic_set_free(context
);
2348 group
= alloc_groups(context
);
2351 return isl_basic_set_free(context
);
2353 for (i
= 0; i
< dim
; ++i
) {
2354 for (j
= 0; j
< bset
->n_eq
; ++j
)
2355 if (!isl_int_is_zero(bset
->eq
[j
][1 + i
]))
2357 if (j
< bset
->n_eq
) {
2361 for (j
= 0; j
< bset
->n_ineq
; ++j
)
2362 if (!isl_int_is_zero(bset
->ineq
[j
][1 + i
]))
2364 if (j
< bset
->n_ineq
)
2368 return isl_basic_map_drop_unrelated_constraints(context
, group
);
2371 /* Drop constraints from "context" that are irrelevant for computing
2372 * the gist of the inequalities "ineq".
2373 * Inequalities in "ineq" for which the corresponding element of row
2374 * is set to -1 have already been marked for removal and should be ignored.
2376 * In particular, drop constraints in variables that are not related
2377 * to any of the variables involved in "ineq"
2378 * in the sense that there is no sequence of constraints that connects them.
2380 * We first mark all variables that appear in "bset" as belonging
2381 * to a "-1" group and then continue with group_and_drop_irrelevant_constraints.
2383 static __isl_give isl_basic_set
*drop_irrelevant_constraints_marked(
2384 __isl_take isl_basic_set
*context
, __isl_keep isl_mat
*ineq
, int *row
)
2391 dim
= isl_basic_set_dim(context
, isl_dim_set
);
2392 n
= isl_mat_rows(ineq
);
2393 if (dim
< 0 || n
< 0)
2394 return isl_basic_set_free(context
);
2396 group
= alloc_groups(context
);
2399 return isl_basic_set_free(context
);
2401 for (i
= 0; i
< dim
; ++i
) {
2402 for (j
= 0; j
< n
; ++j
) {
2405 if (!isl_int_is_zero(ineq
->row
[j
][1 + i
]))
2412 return isl_basic_map_drop_unrelated_constraints(context
, group
);
2415 /* Do all "n" entries of "row" contain a negative value?
2417 static int all_neg(int *row
, int n
)
2421 for (i
= 0; i
< n
; ++i
)
2428 /* Update the inequalities in "bset" based on the information in "row"
2431 * In particular, the array "row" contains either -1, meaning that
2432 * the corresponding inequality of "bset" is redundant, or the index
2433 * of an inequality in "tab".
2435 * If the row entry is -1, then drop the inequality.
2436 * Otherwise, if the constraint is marked redundant in the tableau,
2437 * then drop the inequality. Similarly, if it is marked as an equality
2438 * in the tableau, then turn the inequality into an equality and
2439 * perform Gaussian elimination.
2441 static __isl_give isl_basic_set
*update_ineq(__isl_take isl_basic_set
*bset
,
2442 __isl_keep
int *row
, struct isl_tab
*tab
)
2447 int found_equality
= 0;
2451 if (tab
&& tab
->empty
)
2452 return isl_basic_set_set_to_empty(bset
);
2454 n_ineq
= bset
->n_ineq
;
2455 for (i
= n_ineq
- 1; i
>= 0; --i
) {
2457 if (isl_basic_set_drop_inequality(bset
, i
) < 0)
2458 return isl_basic_set_free(bset
);
2464 if (isl_tab_is_equality(tab
, n_eq
+ row
[i
])) {
2465 isl_basic_map_inequality_to_equality(bset
, i
);
2467 } else if (isl_tab_is_redundant(tab
, n_eq
+ row
[i
])) {
2468 if (isl_basic_set_drop_inequality(bset
, i
) < 0)
2469 return isl_basic_set_free(bset
);
2474 bset
= isl_basic_set_gauss(bset
, NULL
);
2475 bset
= isl_basic_set_finalize(bset
);
2479 /* Update the inequalities in "bset" based on the information in "row"
2480 * and "tab" and free all arguments (other than "bset").
2482 static __isl_give isl_basic_set
*update_ineq_free(
2483 __isl_take isl_basic_set
*bset
, __isl_take isl_mat
*ineq
,
2484 __isl_take isl_basic_set
*context
, __isl_take
int *row
,
2485 struct isl_tab
*tab
)
2488 isl_basic_set_free(context
);
2490 bset
= update_ineq(bset
, row
, tab
);
2497 /* Remove all information from bset that is redundant in the context
2499 * "ineq" contains the (possibly transformed) inequalities of "bset",
2500 * in the same order.
2501 * The (explicit) equalities of "bset" are assumed to have been taken
2502 * into account by the transformation such that only the inequalities
2504 * "context" is assumed not to be empty.
2506 * "row" keeps track of the constraint index of a "bset" inequality in "tab".
2507 * A value of -1 means that the inequality is obviously redundant and may
2508 * not even appear in "tab".
2510 * We first mark the inequalities of "bset"
2511 * that are obviously redundant with respect to some inequality in "context".
2512 * Then we remove those constraints from "context" that have become
2513 * irrelevant for computing the gist of "bset".
2514 * Note that this removal of constraints cannot be replaced by
2515 * a factorization because factors in "bset" may still be connected
2516 * to each other through constraints in "context".
2518 * If there are any inequalities left, we construct a tableau for
2519 * the context and then add the inequalities of "bset".
2520 * Before adding these inequalities, we freeze all constraints such that
2521 * they won't be considered redundant in terms of the constraints of "bset".
2522 * Then we detect all redundant constraints (among the
2523 * constraints that weren't frozen), first by checking for redundancy in the
2524 * the tableau and then by checking if replacing a constraint by its negation
2525 * would lead to an empty set. This last step is fairly expensive
2526 * and could be optimized by more reuse of the tableau.
2527 * Finally, we update bset according to the results.
2529 static __isl_give isl_basic_set
*uset_gist_full(__isl_take isl_basic_set
*bset
,
2530 __isl_take isl_mat
*ineq
, __isl_take isl_basic_set
*context
)
2535 isl_basic_set
*combined
= NULL
;
2536 struct isl_tab
*tab
= NULL
;
2537 unsigned n_eq
, context_ineq
;
2539 if (!bset
|| !ineq
|| !context
)
2542 if (bset
->n_ineq
== 0 || isl_basic_set_plain_is_universe(context
)) {
2543 isl_basic_set_free(context
);
2548 ctx
= isl_basic_set_get_ctx(context
);
2549 row
= isl_calloc_array(ctx
, int, bset
->n_ineq
);
2553 if (mark_shifted_constraints(ineq
, context
, row
) < 0)
2555 if (all_neg(row
, bset
->n_ineq
))
2556 return update_ineq_free(bset
, ineq
, context
, row
, NULL
);
2558 context
= drop_irrelevant_constraints_marked(context
, ineq
, row
);
2561 if (isl_basic_set_plain_is_universe(context
))
2562 return update_ineq_free(bset
, ineq
, context
, row
, NULL
);
2564 n_eq
= context
->n_eq
;
2565 context_ineq
= context
->n_ineq
;
2566 combined
= isl_basic_set_cow(isl_basic_set_copy(context
));
2567 combined
= isl_basic_set_extend_constraints(combined
, 0, bset
->n_ineq
);
2568 tab
= isl_tab_from_basic_set(combined
, 0);
2569 for (i
= 0; i
< context_ineq
; ++i
)
2570 if (isl_tab_freeze_constraint(tab
, n_eq
+ i
) < 0)
2572 if (isl_tab_extend_cons(tab
, bset
->n_ineq
) < 0)
2575 for (i
= 0; i
< bset
->n_ineq
; ++i
) {
2578 combined
= isl_basic_set_add_ineq(combined
, ineq
->row
[i
]);
2579 if (isl_tab_add_ineq(tab
, ineq
->row
[i
]) < 0)
2583 if (isl_tab_detect_implicit_equalities(tab
) < 0)
2585 if (isl_tab_detect_redundant(tab
) < 0)
2587 for (i
= bset
->n_ineq
- 1; i
>= 0; --i
) {
2588 isl_basic_set
*test
;
2594 if (tab
->con
[n_eq
+ r
].is_redundant
)
2596 test
= isl_basic_set_dup(combined
);
2597 test
= isl_inequality_negate(test
, r
);
2598 test
= isl_basic_set_update_from_tab(test
, tab
);
2599 is_empty
= isl_basic_set_is_empty(test
);
2600 isl_basic_set_free(test
);
2604 tab
->con
[n_eq
+ r
].is_redundant
= 1;
2606 bset
= update_ineq_free(bset
, ineq
, context
, row
, tab
);
2608 ISL_F_SET(bset
, ISL_BASIC_SET_NO_IMPLICIT
);
2609 ISL_F_SET(bset
, ISL_BASIC_SET_NO_REDUNDANT
);
2612 isl_basic_set_free(combined
);
2618 isl_basic_set_free(combined
);
2619 isl_basic_set_free(context
);
2620 isl_basic_set_free(bset
);
2624 /* Extract the inequalities of "bset" as an isl_mat.
2626 static __isl_give isl_mat
*extract_ineq(__isl_keep isl_basic_set
*bset
)
2632 total
= isl_basic_set_dim(bset
, isl_dim_all
);
2636 ctx
= isl_basic_set_get_ctx(bset
);
2637 ineq
= isl_mat_sub_alloc6(ctx
, bset
->ineq
, 0, bset
->n_ineq
,
2643 /* Remove all information from "bset" that is redundant in the context
2644 * of "context", for the case where both "bset" and "context" are
2647 static __isl_give isl_basic_set
*uset_gist_uncompressed(
2648 __isl_take isl_basic_set
*bset
, __isl_take isl_basic_set
*context
)
2652 ineq
= extract_ineq(bset
);
2653 return uset_gist_full(bset
, ineq
, context
);
2656 /* Replace "bset" by an empty basic set in the same space.
2658 static __isl_give isl_basic_set
*replace_by_empty(
2659 __isl_take isl_basic_set
*bset
)
2663 space
= isl_basic_set_get_space(bset
);
2664 isl_basic_set_free(bset
);
2665 return isl_basic_set_empty(space
);
2668 /* Remove all information from "bset" that is redundant in the context
2669 * of "context", for the case where the combined equalities of
2670 * "bset" and "context" allow for a compression that can be obtained
2671 * by preapplication of "T".
2672 * If the compression of "context" is empty, meaning that "bset" and
2673 * "context" do not intersect, then return the empty set.
2675 * "bset" itself is not transformed by "T". Instead, the inequalities
2676 * are extracted from "bset" and those are transformed by "T".
2677 * uset_gist_full then determines which of the transformed inequalities
2678 * are redundant with respect to the transformed "context" and removes
2679 * the corresponding inequalities from "bset".
2681 * After preapplying "T" to the inequalities, any common factor is
2682 * removed from the coefficients. If this results in a tightening
2683 * of the constant term, then the same tightening is applied to
2684 * the corresponding untransformed inequality in "bset".
2685 * That is, if after plugging in T, a constraint f(x) >= 0 is of the form
2689 * with 0 <= r < g, then it is equivalent to
2693 * This means that f(x) >= 0 is equivalent to f(x) - r >= 0 in the affine
2694 * subspace compressed by T since the latter would be transformed to
2698 static __isl_give isl_basic_set
*uset_gist_compressed(
2699 __isl_take isl_basic_set
*bset
, __isl_take isl_basic_set
*context
,
2700 __isl_take isl_mat
*T
)
2705 isl_size n_row
, n_col
;
2708 ineq
= extract_ineq(bset
);
2709 ineq
= isl_mat_product(ineq
, isl_mat_copy(T
));
2710 context
= isl_basic_set_preimage(context
, T
);
2712 if (!ineq
|| !context
)
2714 if (isl_basic_set_plain_is_empty(context
)) {
2716 isl_basic_set_free(context
);
2717 return replace_by_empty(bset
);
2720 ctx
= isl_mat_get_ctx(ineq
);
2721 n_row
= isl_mat_rows(ineq
);
2722 n_col
= isl_mat_cols(ineq
);
2723 if (n_row
< 0 || n_col
< 0)
2726 for (i
= 0; i
< n_row
; ++i
) {
2727 isl_seq_gcd(ineq
->row
[i
] + 1, n_col
- 1, &ctx
->normalize_gcd
);
2728 if (isl_int_is_zero(ctx
->normalize_gcd
))
2730 if (isl_int_is_one(ctx
->normalize_gcd
))
2732 isl_seq_scale_down(ineq
->row
[i
] + 1, ineq
->row
[i
] + 1,
2733 ctx
->normalize_gcd
, n_col
- 1);
2734 isl_int_fdiv_r(rem
, ineq
->row
[i
][0], ctx
->normalize_gcd
);
2735 isl_int_fdiv_q(ineq
->row
[i
][0],
2736 ineq
->row
[i
][0], ctx
->normalize_gcd
);
2737 if (isl_int_is_zero(rem
))
2739 bset
= isl_basic_set_cow(bset
);
2742 isl_int_sub(bset
->ineq
[i
][0], bset
->ineq
[i
][0], rem
);
2746 return uset_gist_full(bset
, ineq
, context
);
2749 isl_basic_set_free(context
);
2750 isl_basic_set_free(bset
);
2754 /* Project "bset" onto the variables that are involved in "template".
2756 static __isl_give isl_basic_set
*project_onto_involved(
2757 __isl_take isl_basic_set
*bset
, __isl_keep isl_basic_set
*template)
2762 n
= isl_basic_set_dim(template, isl_dim_set
);
2763 if (n
< 0 || !template)
2764 return isl_basic_set_free(bset
);
2766 for (i
= 0; i
< n
; ++i
) {
2769 involved
= isl_basic_set_involves_dims(template,
2772 return isl_basic_set_free(bset
);
2775 bset
= isl_basic_set_eliminate_vars(bset
, i
, 1);
2781 /* Remove all information from bset that is redundant in the context
2782 * of context. In particular, equalities that are linear combinations
2783 * of those in context are removed. Then the inequalities that are
2784 * redundant in the context of the equalities and inequalities of
2785 * context are removed.
2787 * First of all, we drop those constraints from "context"
2788 * that are irrelevant for computing the gist of "bset".
2789 * Alternatively, we could factorize the intersection of "context" and "bset".
2791 * We first compute the intersection of the integer affine hulls
2792 * of "bset" and "context",
2793 * compute the gist inside this intersection and then reduce
2794 * the constraints with respect to the equalities of the context
2795 * that only involve variables already involved in the input.
2796 * If the intersection of the affine hulls turns out to be empty,
2797 * then return the empty set.
2799 * If two constraints are mutually redundant, then uset_gist_full
2800 * will remove the second of those constraints. We therefore first
2801 * sort the constraints so that constraints not involving existentially
2802 * quantified variables are given precedence over those that do.
2803 * We have to perform this sorting before the variable compression,
2804 * because that may effect the order of the variables.
2806 static __isl_give isl_basic_set
*uset_gist(__isl_take isl_basic_set
*bset
,
2807 __isl_take isl_basic_set
*context
)
2812 isl_basic_set
*aff_context
;
2815 total
= isl_basic_set_dim(bset
, isl_dim_all
);
2816 if (total
< 0 || !context
)
2819 context
= drop_irrelevant_constraints(context
, bset
);
2821 bset
= isl_basic_set_detect_equalities(bset
);
2822 aff
= isl_basic_set_copy(bset
);
2823 aff
= isl_basic_set_plain_affine_hull(aff
);
2824 context
= isl_basic_set_detect_equalities(context
);
2825 aff_context
= isl_basic_set_copy(context
);
2826 aff_context
= isl_basic_set_plain_affine_hull(aff_context
);
2827 aff
= isl_basic_set_intersect(aff
, aff_context
);
2830 if (isl_basic_set_plain_is_empty(aff
)) {
2831 isl_basic_set_free(bset
);
2832 isl_basic_set_free(context
);
2835 bset
= isl_basic_set_sort_constraints(bset
);
2836 if (aff
->n_eq
== 0) {
2837 isl_basic_set_free(aff
);
2838 return uset_gist_uncompressed(bset
, context
);
2840 eq
= isl_mat_sub_alloc6(bset
->ctx
, aff
->eq
, 0, aff
->n_eq
, 0, 1 + total
);
2841 eq
= isl_mat_cow(eq
);
2842 T
= isl_mat_variable_compression(eq
, NULL
);
2843 isl_basic_set_free(aff
);
2844 if (T
&& T
->n_col
== 0) {
2846 isl_basic_set_free(context
);
2847 return replace_by_empty(bset
);
2850 aff_context
= isl_basic_set_affine_hull(isl_basic_set_copy(context
));
2851 aff_context
= project_onto_involved(aff_context
, bset
);
2853 bset
= uset_gist_compressed(bset
, context
, T
);
2854 bset
= isl_basic_set_reduce_using_equalities(bset
, aff_context
);
2857 ISL_F_SET(bset
, ISL_BASIC_SET_NO_IMPLICIT
);
2858 ISL_F_SET(bset
, ISL_BASIC_SET_NO_REDUNDANT
);
2863 isl_basic_set_free(bset
);
2864 isl_basic_set_free(context
);
2868 /* Return the number of equality constraints in "bmap" that involve
2869 * local variables. This function assumes that Gaussian elimination
2870 * has been applied to the equality constraints.
2872 static int n_div_eq(__isl_keep isl_basic_map
*bmap
)
2875 isl_size total
, n_div
;
2880 if (bmap
->n_eq
== 0)
2883 total
= isl_basic_map_dim(bmap
, isl_dim_all
);
2884 n_div
= isl_basic_map_dim(bmap
, isl_dim_div
);
2885 if (total
< 0 || n_div
< 0)
2889 for (i
= 0; i
< bmap
->n_eq
; ++i
)
2890 if (isl_seq_first_non_zero(bmap
->eq
[i
] + 1 + total
,
2897 /* Construct a basic map in "space" defined by the equality constraints in "eq".
2898 * The constraints are assumed not to involve any local variables.
2900 static __isl_give isl_basic_map
*basic_map_from_equalities(
2901 __isl_take isl_space
*space
, __isl_take isl_mat
*eq
)
2905 isl_basic_map
*bmap
= NULL
;
2907 total
= isl_space_dim(space
, isl_dim_all
);
2908 if (total
< 0 || !eq
)
2911 if (1 + total
!= eq
->n_col
)
2912 isl_die(isl_space_get_ctx(space
), isl_error_internal
,
2913 "unexpected number of columns", goto error
);
2915 bmap
= isl_basic_map_alloc_space(isl_space_copy(space
),
2917 for (i
= 0; i
< eq
->n_row
; ++i
) {
2918 k
= isl_basic_map_alloc_equality(bmap
);
2921 isl_seq_cpy(bmap
->eq
[k
], eq
->row
[i
], eq
->n_col
);
2924 isl_space_free(space
);
2928 isl_space_free(space
);
2930 isl_basic_map_free(bmap
);
2934 /* Construct and return a variable compression based on the equality
2935 * constraints in "bmap1" and "bmap2" that do not involve the local variables.
2936 * "n1" is the number of (initial) equality constraints in "bmap1"
2937 * that do involve local variables.
2938 * "n2" is the number of (initial) equality constraints in "bmap2"
2939 * that do involve local variables.
2940 * "total" is the total number of other variables.
2941 * This function assumes that Gaussian elimination
2942 * has been applied to the equality constraints in both "bmap1" and "bmap2"
2943 * such that the equality constraints not involving local variables
2944 * are those that start at "n1" or "n2".
2946 * If either of "bmap1" and "bmap2" does not have such equality constraints,
2947 * then simply compute the compression based on the equality constraints
2948 * in the other basic map.
2949 * Otherwise, combine the equality constraints from both into a new
2950 * basic map such that Gaussian elimination can be applied to this combination
2951 * and then construct a variable compression from the resulting
2952 * equality constraints.
2954 static __isl_give isl_mat
*combined_variable_compression(
2955 __isl_keep isl_basic_map
*bmap1
, int n1
,
2956 __isl_keep isl_basic_map
*bmap2
, int n2
, int total
)
2959 isl_mat
*E1
, *E2
, *V
;
2960 isl_basic_map
*bmap
;
2962 ctx
= isl_basic_map_get_ctx(bmap1
);
2963 if (bmap1
->n_eq
== n1
) {
2964 E2
= isl_mat_sub_alloc6(ctx
, bmap2
->eq
,
2965 n2
, bmap2
->n_eq
- n2
, 0, 1 + total
);
2966 return isl_mat_variable_compression(E2
, NULL
);
2968 if (bmap2
->n_eq
== n2
) {
2969 E1
= isl_mat_sub_alloc6(ctx
, bmap1
->eq
,
2970 n1
, bmap1
->n_eq
- n1
, 0, 1 + total
);
2971 return isl_mat_variable_compression(E1
, NULL
);
2973 E1
= isl_mat_sub_alloc6(ctx
, bmap1
->eq
,
2974 n1
, bmap1
->n_eq
- n1
, 0, 1 + total
);
2975 E2
= isl_mat_sub_alloc6(ctx
, bmap2
->eq
,
2976 n2
, bmap2
->n_eq
- n2
, 0, 1 + total
);
2977 E1
= isl_mat_concat(E1
, E2
);
2978 bmap
= basic_map_from_equalities(isl_basic_map_get_space(bmap1
), E1
);
2979 bmap
= isl_basic_map_gauss(bmap
, NULL
);
2982 E1
= isl_mat_sub_alloc6(ctx
, bmap
->eq
, 0, bmap
->n_eq
, 0, 1 + total
);
2983 V
= isl_mat_variable_compression(E1
, NULL
);
2984 isl_basic_map_free(bmap
);
2989 /* Extract the stride constraints from "bmap", compressed
2990 * with respect to both the stride constraints in "context" and
2991 * the remaining equality constraints in both "bmap" and "context".
2992 * "bmap_n_eq" is the number of (initial) stride constraints in "bmap".
2993 * "context_n_eq" is the number of (initial) stride constraints in "context".
2995 * Let x be all variables in "bmap" (and "context") other than the local
2996 * variables. First compute a variable compression
3000 * based on the non-stride equality constraints in "bmap" and "context".
3001 * Consider the stride constraints of "context",
3005 * with y the local variables and plug in the variable compression,
3008 * A(V x') + B(y) = 0
3010 * Use these constraints to compute a parameter compression on x'
3014 * Now consider the stride constraints of "bmap"
3018 * and plug in x = V*T x''.
3019 * That is, return A = [C*V*T D].
3021 static __isl_give isl_mat
*extract_compressed_stride_constraints(
3022 __isl_keep isl_basic_map
*bmap
, int bmap_n_eq
,
3023 __isl_keep isl_basic_map
*context
, int context_n_eq
)
3025 isl_size total
, n_div
;
3027 isl_mat
*A
, *B
, *T
, *V
;
3029 total
= isl_basic_map_dim(context
, isl_dim_all
);
3030 n_div
= isl_basic_map_dim(context
, isl_dim_div
);
3031 if (total
< 0 || n_div
< 0)
3035 ctx
= isl_basic_map_get_ctx(bmap
);
3037 V
= combined_variable_compression(bmap
, bmap_n_eq
,
3038 context
, context_n_eq
, total
);
3040 A
= isl_mat_sub_alloc6(ctx
, context
->eq
, 0, context_n_eq
, 0, 1 + total
);
3041 B
= isl_mat_sub_alloc6(ctx
, context
->eq
,
3042 0, context_n_eq
, 1 + total
, n_div
);
3043 A
= isl_mat_product(A
, isl_mat_copy(V
));
3044 T
= isl_mat_parameter_compression_ext(A
, B
);
3045 T
= isl_mat_product(V
, T
);
3047 n_div
= isl_basic_map_dim(bmap
, isl_dim_div
);
3049 T
= isl_mat_free(T
);
3051 T
= isl_mat_diagonal(T
, isl_mat_identity(ctx
, n_div
));
3053 A
= isl_mat_sub_alloc6(ctx
, bmap
->eq
,
3054 0, bmap_n_eq
, 0, 1 + total
+ n_div
);
3055 A
= isl_mat_product(A
, T
);
3060 /* Remove the prime factors from *g that have an exponent that
3061 * is strictly smaller than the exponent in "c".
3062 * All exponents in *g are known to be smaller than or equal
3065 * That is, if *g is equal to
3067 * p_1^{e_1} p_2^{e_2} ... p_n^{e_n}
3069 * and "c" is equal to
3071 * p_1^{f_1} p_2^{f_2} ... p_n^{f_n}
3075 * p_1^{e_1 * (e_1 = f_1)} p_2^{e_2 * (e_2 = f_2)} ...
3076 * p_n^{e_n * (e_n = f_n)}
3078 * If e_i = f_i, then c / *g does not have any p_i factors and therefore
3079 * neither does the gcd of *g and c / *g.
3080 * If e_i < f_i, then the gcd of *g and c / *g has a positive
3081 * power min(e_i, s_i) of p_i with s_i = f_i - e_i among its factors.
3082 * Dividing *g by this gcd therefore strictly reduces the exponent
3083 * of the prime factors that need to be removed, while leaving the
3084 * other prime factors untouched.
3085 * Repeating this process until gcd(*g, c / *g) = 1 therefore
3086 * removes all undesired factors, without removing any others.
3088 static void remove_incomplete_powers(isl_int
*g
, isl_int c
)
3094 isl_int_divexact(t
, c
, *g
);
3095 isl_int_gcd(t
, t
, *g
);
3096 if (isl_int_is_one(t
))
3098 isl_int_divexact(*g
, *g
, t
);
3103 /* Reduce the "n" stride constraints in "bmap" based on a copy "A"
3104 * of the same stride constraints in a compressed space that exploits
3105 * all equalities in the context and the other equalities in "bmap".
3107 * If the stride constraints of "bmap" are of the form
3111 * then A is of the form
3115 * If any of these constraints involves only a single local variable y,
3116 * then the constraint appears as
3126 * Let g be the gcd of m and the coefficients of h.
3127 * Then, in particular, g is a divisor of the coefficients of h and
3131 * is known to be a multiple of g.
3132 * If some prime factor in m appears with the same exponent in g,
3133 * then it can be removed from m because f(x) is already known
3134 * to be a multiple of g and therefore in particular of this power
3135 * of the prime factors.
3136 * Prime factors that appear with a smaller exponent in g cannot
3137 * be removed from m.
3138 * Let g' be the divisor of g containing all prime factors that
3139 * appear with the same exponent in m and g, then
3143 * can be replaced by
3145 * f(x) + m/g' y_i' = 0
3147 * Note that (if g' != 1) this changes the explicit representation
3148 * of y_i to that of y_i', so the integer division at position i
3149 * is marked unknown and later recomputed by a call to
3150 * isl_basic_map_gauss.
3152 static __isl_give isl_basic_map
*reduce_stride_constraints(
3153 __isl_take isl_basic_map
*bmap
, int n
, __isl_keep isl_mat
*A
)
3156 isl_size total
, n_div
;
3160 total
= isl_basic_map_dim(bmap
, isl_dim_all
);
3161 n_div
= isl_basic_map_dim(bmap
, isl_dim_div
);
3162 if (total
< 0 || n_div
< 0 || !A
)
3163 return isl_basic_map_free(bmap
);
3167 for (i
= 0; i
< n
; ++i
) {
3170 div
= isl_seq_first_non_zero(bmap
->eq
[i
] + 1 + total
, n_div
);
3172 isl_die(isl_basic_map_get_ctx(bmap
), isl_error_internal
,
3173 "equality constraints modified unexpectedly",
3175 if (isl_seq_first_non_zero(bmap
->eq
[i
] + 1 + total
+ div
+ 1,
3176 n_div
- div
- 1) != -1)
3178 if (isl_mat_row_gcd(A
, i
, &gcd
) < 0)
3180 if (isl_int_is_one(gcd
))
3182 remove_incomplete_powers(&gcd
, bmap
->eq
[i
][1 + total
+ div
]);
3183 if (isl_int_is_one(gcd
))
3185 isl_int_divexact(bmap
->eq
[i
][1 + total
+ div
],
3186 bmap
->eq
[i
][1 + total
+ div
], gcd
);
3187 bmap
= isl_basic_map_mark_div_unknown(bmap
, div
);
3195 bmap
= isl_basic_map_gauss(bmap
, NULL
);
3200 isl_basic_map_free(bmap
);
3204 /* Simplify the stride constraints in "bmap" based on
3205 * the remaining equality constraints in "bmap" and all equality
3206 * constraints in "context".
3207 * Only do this if both "bmap" and "context" have stride constraints.
3209 * First extract a copy of the stride constraints in "bmap" in a compressed
3210 * space exploiting all the other equality constraints and then
3211 * use this compressed copy to simplify the original stride constraints.
3213 static __isl_give isl_basic_map
*gist_strides(__isl_take isl_basic_map
*bmap
,
3214 __isl_keep isl_basic_map
*context
)
3216 int bmap_n_eq
, context_n_eq
;
3219 if (!bmap
|| !context
)
3220 return isl_basic_map_free(bmap
);
3222 bmap_n_eq
= n_div_eq(bmap
);
3223 context_n_eq
= n_div_eq(context
);
3225 if (bmap_n_eq
< 0 || context_n_eq
< 0)
3226 return isl_basic_map_free(bmap
);
3227 if (bmap_n_eq
== 0 || context_n_eq
== 0)
3230 A
= extract_compressed_stride_constraints(bmap
, bmap_n_eq
,
3231 context
, context_n_eq
);
3232 bmap
= reduce_stride_constraints(bmap
, bmap_n_eq
, A
);
3239 /* Return a basic map that has the same intersection with "context" as "bmap"
3240 * and that is as "simple" as possible.
3242 * The core computation is performed on the pure constraints.
3243 * When we add back the meaning of the integer divisions, we need
3244 * to (re)introduce the div constraints. If we happen to have
3245 * discovered that some of these integer divisions are equal to
3246 * some affine combination of other variables, then these div
3247 * constraints may end up getting simplified in terms of the equalities,
3248 * resulting in extra inequalities on the other variables that
3249 * may have been removed already or that may not even have been
3250 * part of the input. We try and remove those constraints of
3251 * this form that are most obviously redundant with respect to
3252 * the context. We also remove those div constraints that are
3253 * redundant with respect to the other constraints in the result.
3255 * The stride constraints among the equality constraints in "bmap" are
3256 * also simplified with respecting to the other equality constraints
3257 * in "bmap" and with respect to all equality constraints in "context".
3259 __isl_give isl_basic_map
*isl_basic_map_gist(__isl_take isl_basic_map
*bmap
,
3260 __isl_take isl_basic_map
*context
)
3262 isl_basic_set
*bset
, *eq
;
3263 isl_basic_map
*eq_bmap
;
3264 isl_size total
, n_div
, n_div_bmap
;
3265 unsigned extra
, n_eq
, n_ineq
;
3267 if (!bmap
|| !context
)
3270 if (isl_basic_map_plain_is_universe(bmap
)) {
3271 isl_basic_map_free(context
);
3274 if (isl_basic_map_plain_is_empty(context
)) {
3275 isl_space
*space
= isl_basic_map_get_space(bmap
);
3276 isl_basic_map_free(bmap
);
3277 isl_basic_map_free(context
);
3278 return isl_basic_map_universe(space
);
3280 if (isl_basic_map_plain_is_empty(bmap
)) {
3281 isl_basic_map_free(context
);
3285 bmap
= isl_basic_map_remove_redundancies(bmap
);
3286 context
= isl_basic_map_remove_redundancies(context
);
3287 bmap
= isl_basic_map_order_divs(bmap
);
3288 context
= isl_basic_map_align_divs(context
, bmap
);
3290 n_div
= isl_basic_map_dim(context
, isl_dim_div
);
3291 total
= isl_basic_map_dim(bmap
, isl_dim_all
);
3292 n_div_bmap
= isl_basic_map_dim(bmap
, isl_dim_div
);
3293 if (n_div
< 0 || total
< 0 || n_div_bmap
< 0)
3295 extra
= n_div
- n_div_bmap
;
3297 bset
= isl_basic_map_underlying_set(isl_basic_map_copy(bmap
));
3298 bset
= isl_basic_set_add_dims(bset
, isl_dim_set
, extra
);
3299 bset
= uset_gist(bset
,
3300 isl_basic_map_underlying_set(isl_basic_map_copy(context
)));
3301 bset
= isl_basic_set_project_out(bset
, isl_dim_set
, total
, extra
);
3303 if (!bset
|| bset
->n_eq
== 0 || n_div
== 0 ||
3304 isl_basic_set_plain_is_empty(bset
)) {
3305 isl_basic_map_free(context
);
3306 return isl_basic_map_overlying_set(bset
, bmap
);
3310 n_ineq
= bset
->n_ineq
;
3311 eq
= isl_basic_set_copy(bset
);
3312 eq
= isl_basic_set_cow(eq
);
3313 eq
= isl_basic_set_free_inequality(eq
, n_ineq
);
3314 bset
= isl_basic_set_free_equality(bset
, n_eq
);
3316 eq_bmap
= isl_basic_map_overlying_set(eq
, isl_basic_map_copy(bmap
));
3317 eq_bmap
= gist_strides(eq_bmap
, context
);
3318 eq_bmap
= isl_basic_map_remove_shifted_constraints(eq_bmap
, context
);
3319 bmap
= isl_basic_map_overlying_set(bset
, bmap
);
3320 bmap
= isl_basic_map_intersect(bmap
, eq_bmap
);
3321 bmap
= isl_basic_map_remove_redundancies(bmap
);
3325 isl_basic_map_free(bmap
);
3326 isl_basic_map_free(context
);
3331 * Assumes context has no implicit divs.
3333 __isl_give isl_map
*isl_map_gist_basic_map(__isl_take isl_map
*map
,
3334 __isl_take isl_basic_map
*context
)
3338 if (!map
|| !context
)
3341 if (isl_basic_map_plain_is_empty(context
)) {
3342 isl_space
*space
= isl_map_get_space(map
);
3344 isl_basic_map_free(context
);
3345 return isl_map_universe(space
);
3348 context
= isl_basic_map_remove_redundancies(context
);
3349 map
= isl_map_cow(map
);
3350 if (isl_map_basic_map_check_equal_space(map
, context
) < 0)
3352 map
= isl_map_compute_divs(map
);
3355 for (i
= map
->n
- 1; i
>= 0; --i
) {
3356 map
->p
[i
] = isl_basic_map_gist(map
->p
[i
],
3357 isl_basic_map_copy(context
));
3360 if (isl_basic_map_plain_is_empty(map
->p
[i
])) {
3361 isl_basic_map_free(map
->p
[i
]);
3362 if (i
!= map
->n
- 1)
3363 map
->p
[i
] = map
->p
[map
->n
- 1];
3367 isl_basic_map_free(context
);
3368 ISL_F_CLR(map
, ISL_MAP_NORMALIZED
);
3372 isl_basic_map_free(context
);
3376 /* Drop all inequalities from "bmap" that also appear in "context".
3377 * "context" is assumed to have only known local variables and
3378 * the initial local variables of "bmap" are assumed to be the same
3379 * as those of "context".
3380 * The constraints of both "bmap" and "context" are assumed
3381 * to have been sorted using isl_basic_map_sort_constraints.
3383 * Run through the inequality constraints of "bmap" and "context"
3385 * If a constraint of "bmap" involves variables not in "context",
3386 * then it cannot appear in "context".
3387 * If a matching constraint is found, it is removed from "bmap".
3389 static __isl_give isl_basic_map
*drop_inequalities(
3390 __isl_take isl_basic_map
*bmap
, __isl_keep isl_basic_map
*context
)
3393 isl_size total
, bmap_total
;
3396 total
= isl_basic_map_dim(context
, isl_dim_all
);
3397 bmap_total
= isl_basic_map_dim(bmap
, isl_dim_all
);
3398 if (total
< 0 || bmap_total
< 0)
3399 return isl_basic_map_free(bmap
);
3401 extra
= bmap_total
- total
;
3403 i1
= bmap
->n_ineq
- 1;
3404 i2
= context
->n_ineq
- 1;
3405 while (bmap
&& i1
>= 0 && i2
>= 0) {
3408 if (isl_seq_first_non_zero(bmap
->ineq
[i1
] + 1 + total
,
3413 cmp
= isl_basic_map_constraint_cmp(context
, bmap
->ineq
[i1
],
3423 if (isl_int_eq(bmap
->ineq
[i1
][0], context
->ineq
[i2
][0])) {
3424 bmap
= isl_basic_map_cow(bmap
);
3425 if (isl_basic_map_drop_inequality(bmap
, i1
) < 0)
3426 bmap
= isl_basic_map_free(bmap
);
3435 /* Drop all equalities from "bmap" that also appear in "context".
3436 * "context" is assumed to have only known local variables and
3437 * the initial local variables of "bmap" are assumed to be the same
3438 * as those of "context".
3440 * Run through the equality constraints of "bmap" and "context"
3442 * If a constraint of "bmap" involves variables not in "context",
3443 * then it cannot appear in "context".
3444 * If a matching constraint is found, it is removed from "bmap".
3446 static __isl_give isl_basic_map
*drop_equalities(
3447 __isl_take isl_basic_map
*bmap
, __isl_keep isl_basic_map
*context
)
3450 isl_size total
, bmap_total
;
3453 total
= isl_basic_map_dim(context
, isl_dim_all
);
3454 bmap_total
= isl_basic_map_dim(bmap
, isl_dim_all
);
3455 if (total
< 0 || bmap_total
< 0)
3456 return isl_basic_map_free(bmap
);
3458 extra
= bmap_total
- total
;
3460 i1
= bmap
->n_eq
- 1;
3461 i2
= context
->n_eq
- 1;
3463 while (bmap
&& i1
>= 0 && i2
>= 0) {
3466 if (isl_seq_first_non_zero(bmap
->eq
[i1
] + 1 + total
,
3469 last1
= isl_seq_last_non_zero(bmap
->eq
[i1
] + 1, total
);
3470 last2
= isl_seq_last_non_zero(context
->eq
[i2
] + 1, total
);
3471 if (last1
> last2
) {
3475 if (last1
< last2
) {
3479 if (isl_seq_eq(bmap
->eq
[i1
], context
->eq
[i2
], 1 + total
)) {
3480 bmap
= isl_basic_map_cow(bmap
);
3481 if (isl_basic_map_drop_equality(bmap
, i1
) < 0)
3482 bmap
= isl_basic_map_free(bmap
);
3491 /* Remove the constraints in "context" from "bmap".
3492 * "context" is assumed to have explicit representations
3493 * for all local variables.
3495 * First align the divs of "bmap" to those of "context" and
3496 * sort the constraints. Then drop all constraints from "bmap"
3497 * that appear in "context".
3499 __isl_give isl_basic_map
*isl_basic_map_plain_gist(
3500 __isl_take isl_basic_map
*bmap
, __isl_take isl_basic_map
*context
)
3502 isl_bool done
, known
;
3504 done
= isl_basic_map_plain_is_universe(context
);
3505 if (done
== isl_bool_false
)
3506 done
= isl_basic_map_plain_is_universe(bmap
);
3507 if (done
== isl_bool_false
)
3508 done
= isl_basic_map_plain_is_empty(context
);
3509 if (done
== isl_bool_false
)
3510 done
= isl_basic_map_plain_is_empty(bmap
);
3514 isl_basic_map_free(context
);
3517 known
= isl_basic_map_divs_known(context
);
3521 isl_die(isl_basic_map_get_ctx(bmap
), isl_error_invalid
,
3522 "context has unknown divs", goto error
);
3524 context
= isl_basic_map_order_divs(context
);
3525 bmap
= isl_basic_map_align_divs(bmap
, context
);
3526 bmap
= isl_basic_map_gauss(bmap
, NULL
);
3527 bmap
= isl_basic_map_sort_constraints(bmap
);
3528 context
= isl_basic_map_sort_constraints(context
);
3530 bmap
= drop_inequalities(bmap
, context
);
3531 bmap
= drop_equalities(bmap
, context
);
3533 isl_basic_map_free(context
);
3534 bmap
= isl_basic_map_finalize(bmap
);
3537 isl_basic_map_free(bmap
);
3538 isl_basic_map_free(context
);
3542 /* Replace "map" by the disjunct at position "pos" and free "context".
3544 static __isl_give isl_map
*replace_by_disjunct(__isl_take isl_map
*map
,
3545 int pos
, __isl_take isl_basic_map
*context
)
3547 isl_basic_map
*bmap
;
3549 bmap
= isl_basic_map_copy(map
->p
[pos
]);
3551 isl_basic_map_free(context
);
3552 return isl_map_from_basic_map(bmap
);
3555 /* Remove the constraints in "context" from "map".
3556 * If any of the disjuncts in the result turns out to be the universe,
3557 * then return this universe.
3558 * "context" is assumed to have explicit representations
3559 * for all local variables.
3561 __isl_give isl_map
*isl_map_plain_gist_basic_map(__isl_take isl_map
*map
,
3562 __isl_take isl_basic_map
*context
)
3565 isl_bool univ
, known
;
3567 univ
= isl_basic_map_plain_is_universe(context
);
3571 isl_basic_map_free(context
);
3574 known
= isl_basic_map_divs_known(context
);
3578 isl_die(isl_map_get_ctx(map
), isl_error_invalid
,
3579 "context has unknown divs", goto error
);
3581 map
= isl_map_cow(map
);
3584 for (i
= 0; i
< map
->n
; ++i
) {
3585 map
->p
[i
] = isl_basic_map_plain_gist(map
->p
[i
],
3586 isl_basic_map_copy(context
));
3587 univ
= isl_basic_map_plain_is_universe(map
->p
[i
]);
3590 if (univ
&& map
->n
> 1)
3591 return replace_by_disjunct(map
, i
, context
);
3594 isl_basic_map_free(context
);
3595 ISL_F_CLR(map
, ISL_MAP_NORMALIZED
);
3597 ISL_F_CLR(map
, ISL_MAP_DISJOINT
);
3601 isl_basic_map_free(context
);
3605 /* Remove the constraints in "context" from "set".
3606 * If any of the disjuncts in the result turns out to be the universe,
3607 * then return this universe.
3608 * "context" is assumed to have explicit representations
3609 * for all local variables.
3611 __isl_give isl_set
*isl_set_plain_gist_basic_set(__isl_take isl_set
*set
,
3612 __isl_take isl_basic_set
*context
)
3614 return set_from_map(isl_map_plain_gist_basic_map(set_to_map(set
),
3615 bset_to_bmap(context
)));
3618 /* Remove the constraints in "context" from "map".
3619 * If any of the disjuncts in the result turns out to be the universe,
3620 * then return this universe.
3621 * "context" is assumed to consist of a single disjunct and
3622 * to have explicit representations for all local variables.
3624 __isl_give isl_map
*isl_map_plain_gist(__isl_take isl_map
*map
,
3625 __isl_take isl_map
*context
)
3627 isl_basic_map
*hull
;
3629 hull
= isl_map_unshifted_simple_hull(context
);
3630 return isl_map_plain_gist_basic_map(map
, hull
);
3633 /* Replace "map" by a universe map in the same space and free "drop".
3635 static __isl_give isl_map
*replace_by_universe(__isl_take isl_map
*map
,
3636 __isl_take isl_map
*drop
)
3640 res
= isl_map_universe(isl_map_get_space(map
));
3646 /* Return a map that has the same intersection with "context" as "map"
3647 * and that is as "simple" as possible.
3649 * If "map" is already the universe, then we cannot make it any simpler.
3650 * Similarly, if "context" is the universe, then we cannot exploit it
3652 * If "map" and "context" are identical to each other, then we can
3653 * return the corresponding universe.
3655 * If either "map" or "context" consists of multiple disjuncts,
3656 * then check if "context" happens to be a subset of "map",
3657 * in which case all constraints can be removed.
3658 * In case of multiple disjuncts, the standard procedure
3659 * may not be able to detect that all constraints can be removed.
3661 * If none of these cases apply, we have to work a bit harder.
3662 * During this computation, we make use of a single disjunct context,
3663 * so if the original context consists of more than one disjunct
3664 * then we need to approximate the context by a single disjunct set.
3665 * Simply taking the simple hull may drop constraints that are
3666 * only implicitly available in each disjunct. We therefore also
3667 * look for constraints among those defining "map" that are valid
3668 * for the context. These can then be used to simplify away
3669 * the corresponding constraints in "map".
3671 __isl_give isl_map
*isl_map_gist(__isl_take isl_map
*map
,
3672 __isl_take isl_map
*context
)
3676 isl_size n_disjunct_map
, n_disjunct_context
;
3678 isl_basic_map
*hull
;
3680 is_universe
= isl_map_plain_is_universe(map
);
3681 if (is_universe
>= 0 && !is_universe
)
3682 is_universe
= isl_map_plain_is_universe(context
);
3683 if (is_universe
< 0)
3686 isl_map_free(context
);
3690 isl_map_align_params_bin(&map
, &context
);
3691 equal
= isl_map_plain_is_equal(map
, context
);
3695 return replace_by_universe(map
, context
);
3697 n_disjunct_map
= isl_map_n_basic_map(map
);
3698 n_disjunct_context
= isl_map_n_basic_map(context
);
3699 if (n_disjunct_map
< 0 || n_disjunct_context
< 0)
3701 if (n_disjunct_map
!= 1 || n_disjunct_context
!= 1) {
3702 subset
= isl_map_is_subset(context
, map
);
3706 return replace_by_universe(map
, context
);
3709 context
= isl_map_compute_divs(context
);
3712 if (n_disjunct_context
== 1) {
3713 hull
= isl_map_simple_hull(context
);
3718 ctx
= isl_map_get_ctx(map
);
3719 list
= isl_map_list_alloc(ctx
, 2);
3720 list
= isl_map_list_add(list
, isl_map_copy(context
));
3721 list
= isl_map_list_add(list
, isl_map_copy(map
));
3722 hull
= isl_map_unshifted_simple_hull_from_map_list(context
,
3725 return isl_map_gist_basic_map(map
, hull
);
3728 isl_map_free(context
);
3732 __isl_give isl_basic_set
*isl_basic_set_gist(__isl_take isl_basic_set
*bset
,
3733 __isl_take isl_basic_set
*context
)
3735 return bset_from_bmap(isl_basic_map_gist(bset_to_bmap(bset
),
3736 bset_to_bmap(context
)));
3739 __isl_give isl_set
*isl_set_gist_basic_set(__isl_take isl_set
*set
,
3740 __isl_take isl_basic_set
*context
)
3742 return set_from_map(isl_map_gist_basic_map(set_to_map(set
),
3743 bset_to_bmap(context
)));
3746 __isl_give isl_set
*isl_set_gist_params_basic_set(__isl_take isl_set
*set
,
3747 __isl_take isl_basic_set
*context
)
3749 isl_space
*space
= isl_set_get_space(set
);
3750 isl_basic_set
*dom_context
= isl_basic_set_universe(space
);
3751 dom_context
= isl_basic_set_intersect_params(dom_context
, context
);
3752 return isl_set_gist_basic_set(set
, dom_context
);
3755 __isl_give isl_set
*isl_set_gist(__isl_take isl_set
*set
,
3756 __isl_take isl_set
*context
)
3758 return set_from_map(isl_map_gist(set_to_map(set
), set_to_map(context
)));
3761 /* Compute the gist of "bmap" with respect to the constraints "context"
3764 __isl_give isl_basic_map
*isl_basic_map_gist_domain(
3765 __isl_take isl_basic_map
*bmap
, __isl_take isl_basic_set
*context
)
3767 isl_space
*space
= isl_basic_map_get_space(bmap
);
3768 isl_basic_map
*bmap_context
= isl_basic_map_universe(space
);
3770 bmap_context
= isl_basic_map_intersect_domain(bmap_context
, context
);
3771 return isl_basic_map_gist(bmap
, bmap_context
);
3774 __isl_give isl_map
*isl_map_gist_domain(__isl_take isl_map
*map
,
3775 __isl_take isl_set
*context
)
3777 isl_map
*map_context
= isl_map_universe(isl_map_get_space(map
));
3778 map_context
= isl_map_intersect_domain(map_context
, context
);
3779 return isl_map_gist(map
, map_context
);
3782 __isl_give isl_map
*isl_map_gist_range(__isl_take isl_map
*map
,
3783 __isl_take isl_set
*context
)
3785 isl_map
*map_context
= isl_map_universe(isl_map_get_space(map
));
3786 map_context
= isl_map_intersect_range(map_context
, context
);
3787 return isl_map_gist(map
, map_context
);
3790 __isl_give isl_map
*isl_map_gist_params(__isl_take isl_map
*map
,
3791 __isl_take isl_set
*context
)
3793 isl_map
*map_context
= isl_map_universe(isl_map_get_space(map
));
3794 map_context
= isl_map_intersect_params(map_context
, context
);
3795 return isl_map_gist(map
, map_context
);
3798 __isl_give isl_set
*isl_set_gist_params(__isl_take isl_set
*set
,
3799 __isl_take isl_set
*context
)
3801 return isl_map_gist_params(set
, context
);
3804 /* Quick check to see if two basic maps are disjoint.
3805 * In particular, we reduce the equalities and inequalities of
3806 * one basic map in the context of the equalities of the other
3807 * basic map and check if we get a contradiction.
3809 isl_bool
isl_basic_map_plain_is_disjoint(__isl_keep isl_basic_map
*bmap1
,
3810 __isl_keep isl_basic_map
*bmap2
)
3812 struct isl_vec
*v
= NULL
;
3817 if (isl_basic_map_check_equal_space(bmap1
, bmap2
) < 0)
3818 return isl_bool_error
;
3819 if (bmap1
->n_div
|| bmap2
->n_div
)
3820 return isl_bool_false
;
3821 if (!bmap1
->n_eq
&& !bmap2
->n_eq
)
3822 return isl_bool_false
;
3824 total
= isl_space_dim(bmap1
->dim
, isl_dim_all
);
3826 return isl_bool_error
;
3828 return isl_bool_false
;
3829 v
= isl_vec_alloc(bmap1
->ctx
, 1 + total
);
3832 elim
= isl_alloc_array(bmap1
->ctx
, int, total
);
3835 compute_elimination_index(bmap1
, elim
, total
);
3836 for (i
= 0; i
< bmap2
->n_eq
; ++i
) {
3838 reduced
= reduced_using_equalities(v
->block
.data
, bmap2
->eq
[i
],
3839 bmap1
, elim
, total
);
3840 if (reduced
&& !isl_int_is_zero(v
->block
.data
[0]) &&
3841 isl_seq_first_non_zero(v
->block
.data
+ 1, total
) == -1)
3844 for (i
= 0; i
< bmap2
->n_ineq
; ++i
) {
3846 reduced
= reduced_using_equalities(v
->block
.data
,
3847 bmap2
->ineq
[i
], bmap1
, elim
, total
);
3848 if (reduced
&& isl_int_is_neg(v
->block
.data
[0]) &&
3849 isl_seq_first_non_zero(v
->block
.data
+ 1, total
) == -1)
3852 compute_elimination_index(bmap2
, elim
, total
);
3853 for (i
= 0; i
< bmap1
->n_ineq
; ++i
) {
3855 reduced
= reduced_using_equalities(v
->block
.data
,
3856 bmap1
->ineq
[i
], bmap2
, elim
, total
);
3857 if (reduced
&& isl_int_is_neg(v
->block
.data
[0]) &&
3858 isl_seq_first_non_zero(v
->block
.data
+ 1, total
) == -1)
3863 return isl_bool_false
;
3867 return isl_bool_true
;
3871 return isl_bool_error
;
3874 int isl_basic_set_plain_is_disjoint(__isl_keep isl_basic_set
*bset1
,
3875 __isl_keep isl_basic_set
*bset2
)
3877 return isl_basic_map_plain_is_disjoint(bset_to_bmap(bset1
),
3878 bset_to_bmap(bset2
));
3881 /* Does "test" hold for all pairs of basic maps in "map1" and "map2"?
3883 static isl_bool
all_pairs(__isl_keep isl_map
*map1
, __isl_keep isl_map
*map2
,
3884 isl_bool (*test
)(__isl_keep isl_basic_map
*bmap1
,
3885 __isl_keep isl_basic_map
*bmap2
))
3890 return isl_bool_error
;
3892 for (i
= 0; i
< map1
->n
; ++i
) {
3893 for (j
= 0; j
< map2
->n
; ++j
) {
3894 isl_bool d
= test(map1
->p
[i
], map2
->p
[j
]);
3895 if (d
!= isl_bool_true
)
3900 return isl_bool_true
;
3903 /* Are "map1" and "map2" obviously disjoint, based on information
3904 * that can be derived without looking at the individual basic maps?
3906 * In particular, if one of them is empty or if they live in different spaces
3907 * (ignoring parameters), then they are clearly disjoint.
3909 static isl_bool
isl_map_plain_is_disjoint_global(__isl_keep isl_map
*map1
,
3910 __isl_keep isl_map
*map2
)
3916 return isl_bool_error
;
3918 disjoint
= isl_map_plain_is_empty(map1
);
3919 if (disjoint
< 0 || disjoint
)
3922 disjoint
= isl_map_plain_is_empty(map2
);
3923 if (disjoint
< 0 || disjoint
)
3926 match
= isl_map_tuple_is_equal(map1
, isl_dim_in
, map2
, isl_dim_in
);
3927 if (match
< 0 || !match
)
3928 return match
< 0 ? isl_bool_error
: isl_bool_true
;
3930 match
= isl_map_tuple_is_equal(map1
, isl_dim_out
, map2
, isl_dim_out
);
3931 if (match
< 0 || !match
)
3932 return match
< 0 ? isl_bool_error
: isl_bool_true
;
3934 return isl_bool_false
;
3937 /* Are "map1" and "map2" obviously disjoint?
3939 * If one of them is empty or if they live in different spaces (ignoring
3940 * parameters), then they are clearly disjoint.
3941 * This is checked by isl_map_plain_is_disjoint_global.
3943 * If they have different parameters, then we skip any further tests.
3945 * If they are obviously equal, but not obviously empty, then we will
3946 * not be able to detect if they are disjoint.
3948 * Otherwise we check if each basic map in "map1" is obviously disjoint
3949 * from each basic map in "map2".
3951 isl_bool
isl_map_plain_is_disjoint(__isl_keep isl_map
*map1
,
3952 __isl_keep isl_map
*map2
)
3958 disjoint
= isl_map_plain_is_disjoint_global(map1
, map2
);
3959 if (disjoint
< 0 || disjoint
)
3962 match
= isl_map_has_equal_params(map1
, map2
);
3963 if (match
< 0 || !match
)
3964 return match
< 0 ? isl_bool_error
: isl_bool_false
;
3966 intersect
= isl_map_plain_is_equal(map1
, map2
);
3967 if (intersect
< 0 || intersect
)
3968 return intersect
< 0 ? isl_bool_error
: isl_bool_false
;
3970 return all_pairs(map1
, map2
, &isl_basic_map_plain_is_disjoint
);
3973 /* Are "map1" and "map2" disjoint?
3974 * The parameters are assumed to have been aligned.
3976 * In particular, check whether all pairs of basic maps are disjoint.
3978 static isl_bool
isl_map_is_disjoint_aligned(__isl_keep isl_map
*map1
,
3979 __isl_keep isl_map
*map2
)
3981 return all_pairs(map1
, map2
, &isl_basic_map_is_disjoint
);
3984 /* Are "map1" and "map2" disjoint?
3986 * They are disjoint if they are "obviously disjoint" or if one of them
3987 * is empty. Otherwise, they are not disjoint if one of them is universal.
3988 * If the two inputs are (obviously) equal and not empty, then they are
3990 * If none of these cases apply, then check if all pairs of basic maps
3991 * are disjoint after aligning the parameters.
3993 isl_bool
isl_map_is_disjoint(__isl_keep isl_map
*map1
, __isl_keep isl_map
*map2
)
3998 disjoint
= isl_map_plain_is_disjoint_global(map1
, map2
);
3999 if (disjoint
< 0 || disjoint
)
4002 disjoint
= isl_map_is_empty(map1
);
4003 if (disjoint
< 0 || disjoint
)
4006 disjoint
= isl_map_is_empty(map2
);
4007 if (disjoint
< 0 || disjoint
)
4010 intersect
= isl_map_plain_is_universe(map1
);
4011 if (intersect
< 0 || intersect
)
4012 return isl_bool_not(intersect
);
4014 intersect
= isl_map_plain_is_universe(map2
);
4015 if (intersect
< 0 || intersect
)
4016 return isl_bool_not(intersect
);
4018 intersect
= isl_map_plain_is_equal(map1
, map2
);
4019 if (intersect
< 0 || intersect
)
4020 return isl_bool_not(intersect
);
4022 return isl_map_align_params_map_map_and_test(map1
, map2
,
4023 &isl_map_is_disjoint_aligned
);
4026 /* Are "bmap1" and "bmap2" disjoint?
4028 * They are disjoint if they are "obviously disjoint" or if one of them
4029 * is empty. Otherwise, they are not disjoint if one of them is universal.
4030 * If none of these cases apply, we compute the intersection and see if
4031 * the result is empty.
4033 isl_bool
isl_basic_map_is_disjoint(__isl_keep isl_basic_map
*bmap1
,
4034 __isl_keep isl_basic_map
*bmap2
)
4038 isl_basic_map
*test
;
4040 disjoint
= isl_basic_map_plain_is_disjoint(bmap1
, bmap2
);
4041 if (disjoint
< 0 || disjoint
)
4044 disjoint
= isl_basic_map_is_empty(bmap1
);
4045 if (disjoint
< 0 || disjoint
)
4048 disjoint
= isl_basic_map_is_empty(bmap2
);
4049 if (disjoint
< 0 || disjoint
)
4052 intersect
= isl_basic_map_plain_is_universe(bmap1
);
4053 if (intersect
< 0 || intersect
)
4054 return isl_bool_not(intersect
);
4056 intersect
= isl_basic_map_plain_is_universe(bmap2
);
4057 if (intersect
< 0 || intersect
)
4058 return isl_bool_not(intersect
);
4060 test
= isl_basic_map_intersect(isl_basic_map_copy(bmap1
),
4061 isl_basic_map_copy(bmap2
));
4062 disjoint
= isl_basic_map_is_empty(test
);
4063 isl_basic_map_free(test
);
4068 /* Are "bset1" and "bset2" disjoint?
4070 isl_bool
isl_basic_set_is_disjoint(__isl_keep isl_basic_set
*bset1
,
4071 __isl_keep isl_basic_set
*bset2
)
4073 return isl_basic_map_is_disjoint(bset1
, bset2
);
4076 isl_bool
isl_set_plain_is_disjoint(__isl_keep isl_set
*set1
,
4077 __isl_keep isl_set
*set2
)
4079 return isl_map_plain_is_disjoint(set_to_map(set1
), set_to_map(set2
));
4082 /* Are "set1" and "set2" disjoint?
4084 isl_bool
isl_set_is_disjoint(__isl_keep isl_set
*set1
, __isl_keep isl_set
*set2
)
4086 return isl_map_is_disjoint(set1
, set2
);
4089 /* Is "v" equal to 0, 1 or -1?
4091 static int is_zero_or_one(isl_int v
)
4093 return isl_int_is_zero(v
) || isl_int_is_one(v
) || isl_int_is_negone(v
);
4096 /* Are the "n" coefficients starting at "first" of inequality constraints
4097 * "i" and "j" of "bmap" opposite to each other?
4099 static int is_opposite_part(__isl_keep isl_basic_map
*bmap
, int i
, int j
,
4102 return isl_seq_is_neg(bmap
->ineq
[i
] + first
, bmap
->ineq
[j
] + first
, n
);
4105 /* Are inequality constraints "i" and "j" of "bmap" opposite to each other,
4106 * apart from the constant term?
4108 static isl_bool
is_opposite(__isl_keep isl_basic_map
*bmap
, int i
, int j
)
4112 total
= isl_basic_map_dim(bmap
, isl_dim_all
);
4114 return isl_bool_error
;
4115 return is_opposite_part(bmap
, i
, j
, 1, total
);
4118 /* Check if we can combine a given div with lower bound l and upper
4119 * bound u with some other div and if so return that other div.
4120 * Otherwise, return a position beyond the integer divisions.
4121 * Return -1 on error.
4123 * We first check that
4124 * - the bounds are opposites of each other (except for the constant
4126 * - the bounds do not reference any other div
4127 * - no div is defined in terms of this div
4129 * Let m be the size of the range allowed on the div by the bounds.
4130 * That is, the bounds are of the form
4132 * e <= a <= e + m - 1
4134 * with e some expression in the other variables.
4135 * We look for another div b such that no third div is defined in terms
4136 * of this second div b and such that in any constraint that contains
4137 * a (except for the given lower and upper bound), also contains b
4138 * with a coefficient that is m times that of b.
4139 * That is, all constraints (except for the lower and upper bound)
4142 * e + f (a + m b) >= 0
4144 * Furthermore, in the constraints that only contain b, the coefficient
4145 * of b should be equal to 1 or -1.
4146 * If so, we return b so that "a + m b" can be replaced by
4147 * a single div "c = a + m b".
4149 static int div_find_coalesce(__isl_keep isl_basic_map
*bmap
, int *pairs
,
4150 unsigned div
, unsigned l
, unsigned u
)
4158 n_div
= isl_basic_map_dim(bmap
, isl_dim_div
);
4161 v_div
= isl_basic_map_var_offset(bmap
, isl_dim_div
);
4164 if (isl_seq_first_non_zero(bmap
->ineq
[l
] + 1 + v_div
, div
) != -1)
4166 if (isl_seq_first_non_zero(bmap
->ineq
[l
] + 1 + v_div
+ div
+ 1,
4167 n_div
- div
- 1) != -1)
4169 opp
= is_opposite(bmap
, l
, u
);
4170 if (opp
< 0 || !opp
)
4171 return opp
< 0 ? -1 : n_div
;
4173 for (i
= 0; i
< n_div
; ++i
) {
4174 if (isl_int_is_zero(bmap
->div
[i
][0]))
4176 if (!isl_int_is_zero(bmap
->div
[i
][1 + 1 + v_div
+ div
]))
4180 isl_int_add(bmap
->ineq
[l
][0], bmap
->ineq
[l
][0], bmap
->ineq
[u
][0]);
4181 if (isl_int_is_neg(bmap
->ineq
[l
][0])) {
4182 isl_int_sub(bmap
->ineq
[l
][0],
4183 bmap
->ineq
[l
][0], bmap
->ineq
[u
][0]);
4184 bmap
= isl_basic_map_copy(bmap
);
4185 bmap
= isl_basic_map_set_to_empty(bmap
);
4186 isl_basic_map_free(bmap
);
4189 isl_int_add_ui(bmap
->ineq
[l
][0], bmap
->ineq
[l
][0], 1);
4191 for (i
= 0; i
< n_div
; ++i
) {
4196 for (j
= 0; j
< n_div
; ++j
) {
4197 if (isl_int_is_zero(bmap
->div
[j
][0]))
4199 if (!isl_int_is_zero(bmap
->div
[j
][1 + 1 + v_div
+ i
]))
4204 for (j
= 0; j
< bmap
->n_ineq
; ++j
) {
4206 if (j
== l
|| j
== u
)
4208 if (isl_int_is_zero(bmap
->ineq
[j
][1 + v_div
+ div
])) {
4209 if (is_zero_or_one(bmap
->ineq
[j
][1 + v_div
+ i
]))
4213 if (isl_int_is_zero(bmap
->ineq
[j
][1 + v_div
+ i
]))
4215 isl_int_mul(bmap
->ineq
[j
][1 + v_div
+ div
],
4216 bmap
->ineq
[j
][1 + v_div
+ div
],
4218 valid
= isl_int_eq(bmap
->ineq
[j
][1 + v_div
+ div
],
4219 bmap
->ineq
[j
][1 + v_div
+ i
]);
4220 isl_int_divexact(bmap
->ineq
[j
][1 + v_div
+ div
],
4221 bmap
->ineq
[j
][1 + v_div
+ div
],
4226 if (j
< bmap
->n_ineq
)
4231 isl_int_sub_ui(bmap
->ineq
[l
][0], bmap
->ineq
[l
][0], 1);
4232 isl_int_sub(bmap
->ineq
[l
][0], bmap
->ineq
[l
][0], bmap
->ineq
[u
][0]);
4236 /* Internal data structure used during the construction and/or evaluation of
4237 * an inequality that ensures that a pair of bounds always allows
4238 * for an integer value.
4240 * "tab" is the tableau in which the inequality is evaluated. It may
4241 * be NULL until it is actually needed.
4242 * "v" contains the inequality coefficients.
4243 * "g", "fl" and "fu" are temporary scalars used during the construction and
4246 struct test_ineq_data
{
4247 struct isl_tab
*tab
;
4254 /* Free all the memory allocated by the fields of "data".
4256 static void test_ineq_data_clear(struct test_ineq_data
*data
)
4258 isl_tab_free(data
->tab
);
4259 isl_vec_free(data
->v
);
4260 isl_int_clear(data
->g
);
4261 isl_int_clear(data
->fl
);
4262 isl_int_clear(data
->fu
);
4265 /* Is the inequality stored in data->v satisfied by "bmap"?
4266 * That is, does it only attain non-negative values?
4267 * data->tab is a tableau corresponding to "bmap".
4269 static isl_bool
test_ineq_is_satisfied(__isl_keep isl_basic_map
*bmap
,
4270 struct test_ineq_data
*data
)
4273 enum isl_lp_result res
;
4275 ctx
= isl_basic_map_get_ctx(bmap
);
4277 data
->tab
= isl_tab_from_basic_map(bmap
, 0);
4278 res
= isl_tab_min(data
->tab
, data
->v
->el
, ctx
->one
, &data
->g
, NULL
, 0);
4279 if (res
== isl_lp_error
)
4280 return isl_bool_error
;
4281 return res
== isl_lp_ok
&& isl_int_is_nonneg(data
->g
);
4284 /* Given a lower and an upper bound on div i, do they always allow
4285 * for an integer value of the given div?
4286 * Determine this property by constructing an inequality
4287 * such that the property is guaranteed when the inequality is nonnegative.
4288 * The lower bound is inequality l, while the upper bound is inequality u.
4289 * The constructed inequality is stored in data->v.
4291 * Let the upper bound be
4295 * and the lower bound
4299 * Let n_u = f_u g and n_l = f_l g, with g = gcd(n_u, n_l).
4302 * - f_u e_l <= f_u f_l g a <= f_l e_u
4304 * Since all variables are integer valued, this is equivalent to
4306 * - f_u e_l - (f_u - 1) <= f_u f_l g a <= f_l e_u + (f_l - 1)
4308 * If this interval is at least f_u f_l g, then it contains at least
4309 * one integer value for a.
4310 * That is, the test constraint is
4312 * f_l e_u + f_u e_l + f_l - 1 + f_u - 1 + 1 >= f_u f_l g
4316 * f_l e_u + f_u e_l + f_l - 1 + f_u - 1 + 1 - f_u f_l g >= 0
4318 * If the coefficients of f_l e_u + f_u e_l have a common divisor g',
4319 * then the constraint can be scaled down by a factor g',
4320 * with the constant term replaced by
4321 * floor((f_l e_{u,0} + f_u e_{l,0} + f_l - 1 + f_u - 1 + 1 - f_u f_l g)/g').
4322 * Note that the result of applying Fourier-Motzkin to this pair
4325 * f_l e_u + f_u e_l >= 0
4327 * If the constant term of the scaled down version of this constraint,
4328 * i.e., floor((f_l e_{u,0} + f_u e_{l,0})/g') is equal to the constant
4329 * term of the scaled down test constraint, then the test constraint
4330 * is known to hold and no explicit evaluation is required.
4331 * This is essentially the Omega test.
4333 * If the test constraint consists of only a constant term, then
4334 * it is sufficient to look at the sign of this constant term.
4336 static isl_bool
int_between_bounds(__isl_keep isl_basic_map
*bmap
, int i
,
4337 int l
, int u
, struct test_ineq_data
*data
)
4342 offset
= isl_basic_map_offset(bmap
, isl_dim_div
);
4343 n_div
= isl_basic_map_dim(bmap
, isl_dim_div
);
4345 return isl_bool_error
;
4347 isl_int_gcd(data
->g
,
4348 bmap
->ineq
[l
][offset
+ i
], bmap
->ineq
[u
][offset
+ i
]);
4349 isl_int_divexact(data
->fl
, bmap
->ineq
[l
][offset
+ i
], data
->g
);
4350 isl_int_divexact(data
->fu
, bmap
->ineq
[u
][offset
+ i
], data
->g
);
4351 isl_int_neg(data
->fu
, data
->fu
);
4352 isl_seq_combine(data
->v
->el
, data
->fl
, bmap
->ineq
[u
],
4353 data
->fu
, bmap
->ineq
[l
], offset
+ n_div
);
4354 isl_int_mul(data
->g
, data
->g
, data
->fl
);
4355 isl_int_mul(data
->g
, data
->g
, data
->fu
);
4356 isl_int_sub(data
->g
, data
->g
, data
->fl
);
4357 isl_int_sub(data
->g
, data
->g
, data
->fu
);
4358 isl_int_add_ui(data
->g
, data
->g
, 1);
4359 isl_int_sub(data
->fl
, data
->v
->el
[0], data
->g
);
4361 isl_seq_gcd(data
->v
->el
+ 1, offset
- 1 + n_div
, &data
->g
);
4362 if (isl_int_is_zero(data
->g
))
4363 return isl_int_is_nonneg(data
->fl
);
4364 if (isl_int_is_one(data
->g
)) {
4365 isl_int_set(data
->v
->el
[0], data
->fl
);
4366 return test_ineq_is_satisfied(bmap
, data
);
4368 isl_int_fdiv_q(data
->fl
, data
->fl
, data
->g
);
4369 isl_int_fdiv_q(data
->v
->el
[0], data
->v
->el
[0], data
->g
);
4370 if (isl_int_eq(data
->fl
, data
->v
->el
[0]))
4371 return isl_bool_true
;
4372 isl_int_set(data
->v
->el
[0], data
->fl
);
4373 isl_seq_scale_down(data
->v
->el
+ 1, data
->v
->el
+ 1, data
->g
,
4374 offset
- 1 + n_div
);
4376 return test_ineq_is_satisfied(bmap
, data
);
4379 /* Remove more kinds of divs that are not strictly needed.
4380 * In particular, if all pairs of lower and upper bounds on a div
4381 * are such that they allow at least one integer value of the div,
4382 * then we can eliminate the div using Fourier-Motzkin without
4383 * introducing any spurious solutions.
4385 * If at least one of the two constraints has a unit coefficient for the div,
4386 * then the presence of such a value is guaranteed so there is no need to check.
4387 * In particular, the value attained by the bound with unit coefficient
4388 * can serve as this intermediate value.
4390 static __isl_give isl_basic_map
*drop_more_redundant_divs(
4391 __isl_take isl_basic_map
*bmap
, __isl_take
int *pairs
, int n
)
4394 struct test_ineq_data data
= { NULL
, NULL
};
4399 isl_int_init(data
.g
);
4400 isl_int_init(data
.fl
);
4401 isl_int_init(data
.fu
);
4403 n_div
= isl_basic_map_dim(bmap
, isl_dim_div
);
4407 ctx
= isl_basic_map_get_ctx(bmap
);
4408 off
= isl_basic_map_offset(bmap
, isl_dim_div
);
4409 data
.v
= isl_vec_alloc(ctx
, off
+ n_div
);
4418 for (i
= 0; i
< n_div
; ++i
) {
4421 if (best
>= 0 && pairs
[best
] <= pairs
[i
])
4427 for (l
= 0; l
< bmap
->n_ineq
; ++l
) {
4428 if (!isl_int_is_pos(bmap
->ineq
[l
][off
+ i
]))
4430 if (isl_int_is_one(bmap
->ineq
[l
][off
+ i
]))
4432 for (u
= 0; u
< bmap
->n_ineq
; ++u
) {
4433 if (!isl_int_is_neg(bmap
->ineq
[u
][off
+ i
]))
4435 if (isl_int_is_negone(bmap
->ineq
[u
][off
+ i
]))
4437 has_int
= int_between_bounds(bmap
, i
, l
, u
,
4441 if (data
.tab
&& data
.tab
->empty
)
4446 if (u
< bmap
->n_ineq
)
4449 if (data
.tab
&& data
.tab
->empty
) {
4450 bmap
= isl_basic_map_set_to_empty(bmap
);
4453 if (l
== bmap
->n_ineq
) {
4461 test_ineq_data_clear(&data
);
4468 bmap
= isl_basic_map_remove_dims(bmap
, isl_dim_div
, remove
, 1);
4469 return isl_basic_map_drop_redundant_divs(bmap
);
4472 isl_basic_map_free(bmap
);
4473 test_ineq_data_clear(&data
);
4477 /* Given a pair of divs div1 and div2 such that, except for the lower bound l
4478 * and the upper bound u, div1 always occurs together with div2 in the form
4479 * (div1 + m div2), where m is the constant range on the variable div1
4480 * allowed by l and u, replace the pair div1 and div2 by a single
4481 * div that is equal to div1 + m div2.
4483 * The new div will appear in the location that contains div2.
4484 * We need to modify all constraints that contain
4485 * div2 = (div - div1) / m
4486 * The coefficient of div2 is known to be equal to 1 or -1.
4487 * (If a constraint does not contain div2, it will also not contain div1.)
4488 * If the constraint also contains div1, then we know they appear
4489 * as f (div1 + m div2) and we can simply replace (div1 + m div2) by div,
4490 * i.e., the coefficient of div is f.
4492 * Otherwise, we first need to introduce div1 into the constraint.
4501 * A lower bound on div2
4505 * can be replaced by
4507 * m div2 + div1 + m t + f >= 0
4513 * can be replaced by
4515 * -(m div2 + div1) + m t + f' >= 0
4517 * These constraint are those that we would obtain from eliminating
4518 * div1 using Fourier-Motzkin.
4520 * After all constraints have been modified, we drop the lower and upper
4521 * bound and then drop div1.
4522 * Since the new div is only placed in the same location that used
4523 * to store div2, but otherwise has a different meaning, any possible
4524 * explicit representation of the original div2 is removed.
4526 static __isl_give isl_basic_map
*coalesce_divs(__isl_take isl_basic_map
*bmap
,
4527 unsigned div1
, unsigned div2
, unsigned l
, unsigned u
)
4535 ctx
= isl_basic_map_get_ctx(bmap
);
4537 v_div
= isl_basic_map_var_offset(bmap
, isl_dim_div
);
4539 return isl_basic_map_free(bmap
);
4540 total
= 1 + v_div
+ bmap
->n_div
;
4543 isl_int_add(m
, bmap
->ineq
[l
][0], bmap
->ineq
[u
][0]);
4544 isl_int_add_ui(m
, m
, 1);
4546 for (i
= 0; i
< bmap
->n_ineq
; ++i
) {
4547 if (i
== l
|| i
== u
)
4549 if (isl_int_is_zero(bmap
->ineq
[i
][1 + v_div
+ div2
]))
4551 if (isl_int_is_zero(bmap
->ineq
[i
][1 + v_div
+ div1
])) {
4552 if (isl_int_is_pos(bmap
->ineq
[i
][1 + v_div
+ div2
]))
4553 isl_seq_combine(bmap
->ineq
[i
], m
, bmap
->ineq
[i
],
4554 ctx
->one
, bmap
->ineq
[l
], total
);
4556 isl_seq_combine(bmap
->ineq
[i
], m
, bmap
->ineq
[i
],
4557 ctx
->one
, bmap
->ineq
[u
], total
);
4559 isl_int_set(bmap
->ineq
[i
][1 + v_div
+ div2
],
4560 bmap
->ineq
[i
][1 + v_div
+ div1
]);
4561 isl_int_set_si(bmap
->ineq
[i
][1 + v_div
+ div1
], 0);
4566 isl_basic_map_drop_inequality(bmap
, l
);
4567 isl_basic_map_drop_inequality(bmap
, u
);
4569 isl_basic_map_drop_inequality(bmap
, u
);
4570 isl_basic_map_drop_inequality(bmap
, l
);
4572 bmap
= isl_basic_map_mark_div_unknown(bmap
, div2
);
4573 bmap
= isl_basic_map_drop_div(bmap
, div1
);
4577 /* First check if we can coalesce any pair of divs and
4578 * then continue with dropping more redundant divs.
4580 * We loop over all pairs of lower and upper bounds on a div
4581 * with coefficient 1 and -1, respectively, check if there
4582 * is any other div "c" with which we can coalesce the div
4583 * and if so, perform the coalescing.
4585 static __isl_give isl_basic_map
*coalesce_or_drop_more_redundant_divs(
4586 __isl_take isl_basic_map
*bmap
, int *pairs
, int n
)
4592 v_div
= isl_basic_map_var_offset(bmap
, isl_dim_div
);
4593 n_div
= isl_basic_map_dim(bmap
, isl_dim_div
);
4594 if (v_div
< 0 || n_div
< 0)
4595 return isl_basic_map_free(bmap
);
4597 for (i
= 0; i
< n_div
; ++i
) {
4600 for (l
= 0; l
< bmap
->n_ineq
; ++l
) {
4601 if (!isl_int_is_one(bmap
->ineq
[l
][1 + v_div
+ i
]))
4603 for (u
= 0; u
< bmap
->n_ineq
; ++u
) {
4606 if (!isl_int_is_negone(bmap
->ineq
[u
][1+v_div
+i
]))
4608 c
= div_find_coalesce(bmap
, pairs
, i
, l
, u
);
4614 bmap
= coalesce_divs(bmap
, i
, c
, l
, u
);
4615 return isl_basic_map_drop_redundant_divs(bmap
);
4620 if (ISL_F_ISSET(bmap
, ISL_BASIC_MAP_EMPTY
)) {
4625 return drop_more_redundant_divs(bmap
, pairs
, n
);
4628 isl_basic_map_free(bmap
);
4632 /* Are the "n" coefficients starting at "first" of inequality constraints
4633 * "i" and "j" of "bmap" equal to each other?
4635 static int is_parallel_part(__isl_keep isl_basic_map
*bmap
, int i
, int j
,
4638 return isl_seq_eq(bmap
->ineq
[i
] + first
, bmap
->ineq
[j
] + first
, n
);
4641 /* Are inequality constraints "i" and "j" of "bmap" equal to each other,
4642 * apart from the constant term and the coefficient at position "pos"?
4644 static isl_bool
is_parallel_except(__isl_keep isl_basic_map
*bmap
, int i
, int j
,
4649 total
= isl_basic_map_dim(bmap
, isl_dim_all
);
4651 return isl_bool_error
;
4652 return is_parallel_part(bmap
, i
, j
, 1, pos
- 1) &&
4653 is_parallel_part(bmap
, i
, j
, pos
+ 1, total
- pos
);
4656 /* Are inequality constraints "i" and "j" of "bmap" opposite to each other,
4657 * apart from the constant term and the coefficient at position "pos"?
4659 static isl_bool
is_opposite_except(__isl_keep isl_basic_map
*bmap
, int i
, int j
,
4664 total
= isl_basic_map_dim(bmap
, isl_dim_all
);
4666 return isl_bool_error
;
4667 return is_opposite_part(bmap
, i
, j
, 1, pos
- 1) &&
4668 is_opposite_part(bmap
, i
, j
, pos
+ 1, total
- pos
);
4671 /* Restart isl_basic_map_drop_redundant_divs after "bmap" has
4672 * been modified, simplying it if "simplify" is set.
4673 * Free the temporary data structure "pairs" that was associated
4674 * to the old version of "bmap".
4676 static __isl_give isl_basic_map
*drop_redundant_divs_again(
4677 __isl_take isl_basic_map
*bmap
, __isl_take
int *pairs
, int simplify
)
4680 bmap
= isl_basic_map_simplify(bmap
);
4682 return isl_basic_map_drop_redundant_divs(bmap
);
4685 /* Is "div" the single unknown existentially quantified variable
4686 * in inequality constraint "ineq" of "bmap"?
4687 * "div" is known to have a non-zero coefficient in "ineq".
4689 static isl_bool
single_unknown(__isl_keep isl_basic_map
*bmap
, int ineq
,
4697 known
= isl_basic_map_div_is_known(bmap
, div
);
4698 if (known
< 0 || known
)
4699 return isl_bool_not(known
);
4700 n_div
= isl_basic_map_dim(bmap
, isl_dim_div
);
4702 return isl_bool_error
;
4704 return isl_bool_true
;
4705 o_div
= isl_basic_map_offset(bmap
, isl_dim_div
);
4706 for (i
= 0; i
< n_div
; ++i
) {
4711 if (isl_int_is_zero(bmap
->ineq
[ineq
][o_div
+ i
]))
4713 known
= isl_basic_map_div_is_known(bmap
, i
);
4714 if (known
< 0 || !known
)
4718 return isl_bool_true
;
4721 /* Does integer division "div" have coefficient 1 in inequality constraint
4724 static isl_bool
has_coef_one(__isl_keep isl_basic_map
*bmap
, int div
, int ineq
)
4728 o_div
= isl_basic_map_offset(bmap
, isl_dim_div
);
4729 if (isl_int_is_one(bmap
->ineq
[ineq
][o_div
+ div
]))
4730 return isl_bool_true
;
4732 return isl_bool_false
;
4735 /* Turn inequality constraint "ineq" of "bmap" into an equality and
4736 * then try and drop redundant divs again,
4737 * freeing the temporary data structure "pairs" that was associated
4738 * to the old version of "bmap".
4740 static __isl_give isl_basic_map
*set_eq_and_try_again(
4741 __isl_take isl_basic_map
*bmap
, int ineq
, __isl_take
int *pairs
)
4743 bmap
= isl_basic_map_cow(bmap
);
4744 isl_basic_map_inequality_to_equality(bmap
, ineq
);
4745 return drop_redundant_divs_again(bmap
, pairs
, 1);
4748 /* Drop the integer division at position "div", along with the two
4749 * inequality constraints "ineq1" and "ineq2" in which it appears
4750 * from "bmap" and then try and drop redundant divs again,
4751 * freeing the temporary data structure "pairs" that was associated
4752 * to the old version of "bmap".
4754 static __isl_give isl_basic_map
*drop_div_and_try_again(
4755 __isl_take isl_basic_map
*bmap
, int div
, int ineq1
, int ineq2
,
4756 __isl_take
int *pairs
)
4758 if (ineq1
> ineq2
) {
4759 isl_basic_map_drop_inequality(bmap
, ineq1
);
4760 isl_basic_map_drop_inequality(bmap
, ineq2
);
4762 isl_basic_map_drop_inequality(bmap
, ineq2
);
4763 isl_basic_map_drop_inequality(bmap
, ineq1
);
4765 bmap
= isl_basic_map_drop_div(bmap
, div
);
4766 return drop_redundant_divs_again(bmap
, pairs
, 0);
4769 /* Given two inequality constraints
4771 * f(x) + n d + c >= 0, (ineq)
4773 * with d the variable at position "pos", and
4775 * f(x) + c0 >= 0, (lower)
4777 * compute the maximal value of the lower bound ceil((-f(x) - c)/n)
4778 * determined by the first constraint.
4785 static void lower_bound_from_parallel(__isl_keep isl_basic_map
*bmap
,
4786 int ineq
, int lower
, int pos
, isl_int
*l
)
4788 isl_int_neg(*l
, bmap
->ineq
[ineq
][0]);
4789 isl_int_add(*l
, *l
, bmap
->ineq
[lower
][0]);
4790 isl_int_cdiv_q(*l
, *l
, bmap
->ineq
[ineq
][pos
]);
4793 /* Given two inequality constraints
4795 * f(x) + n d + c >= 0, (ineq)
4797 * with d the variable at position "pos", and
4799 * -f(x) - c0 >= 0, (upper)
4801 * compute the minimal value of the lower bound ceil((-f(x) - c)/n)
4802 * determined by the first constraint.
4809 static void lower_bound_from_opposite(__isl_keep isl_basic_map
*bmap
,
4810 int ineq
, int upper
, int pos
, isl_int
*u
)
4812 isl_int_neg(*u
, bmap
->ineq
[ineq
][0]);
4813 isl_int_sub(*u
, *u
, bmap
->ineq
[upper
][0]);
4814 isl_int_cdiv_q(*u
, *u
, bmap
->ineq
[ineq
][pos
]);
4817 /* Given a lower bound constraint "ineq" on "div" in "bmap",
4818 * does the corresponding lower bound have a fixed value in "bmap"?
4820 * In particular, "ineq" is of the form
4822 * f(x) + n d + c >= 0
4824 * with n > 0, c the constant term and
4825 * d the existentially quantified variable "div".
4826 * That is, the lower bound is
4828 * ceil((-f(x) - c)/n)
4830 * Look for a pair of constraints
4835 * i.e., -c1 <= -f(x) <= c0, that fix ceil((-f(x) - c)/n) to a constant value.
4836 * That is, check that
4838 * ceil((-c1 - c)/n) = ceil((c0 - c)/n)
4840 * If so, return the index of inequality f(x) + c0 >= 0.
4841 * Otherwise, return bmap->n_ineq.
4842 * Return -1 on error.
4844 static int lower_bound_is_cst(__isl_keep isl_basic_map
*bmap
, int div
, int ineq
)
4847 int lower
= -1, upper
= -1;
4852 o_div
= isl_basic_map_offset(bmap
, isl_dim_div
);
4853 for (i
= 0; i
< bmap
->n_ineq
&& (lower
< 0 || upper
< 0); ++i
) {
4858 if (!isl_int_is_zero(bmap
->ineq
[i
][o_div
+ div
]))
4860 par
= isl_bool_false
;
4862 par
= is_parallel_except(bmap
, ineq
, i
, o_div
+ div
);
4869 opp
= isl_bool_false
;
4871 opp
= is_opposite_except(bmap
, ineq
, i
, o_div
+ div
);
4878 if (lower
< 0 || upper
< 0)
4879 return bmap
->n_ineq
;
4884 lower_bound_from_parallel(bmap
, ineq
, lower
, o_div
+ div
, &l
);
4885 lower_bound_from_opposite(bmap
, ineq
, upper
, o_div
+ div
, &u
);
4887 equal
= isl_int_eq(l
, u
);
4892 return equal
? lower
: bmap
->n_ineq
;
4895 /* Given a lower bound constraint "ineq" on the existentially quantified
4896 * variable "div", such that the corresponding lower bound has
4897 * a fixed value in "bmap", assign this fixed value to the variable and
4898 * then try and drop redundant divs again,
4899 * freeing the temporary data structure "pairs" that was associated
4900 * to the old version of "bmap".
4901 * "lower" determines the constant value for the lower bound.
4903 * In particular, "ineq" is of the form
4905 * f(x) + n d + c >= 0,
4907 * while "lower" is of the form
4911 * The lower bound is ceil((-f(x) - c)/n) and its constant value
4912 * is ceil((c0 - c)/n).
4914 static __isl_give isl_basic_map
*fix_cst_lower(__isl_take isl_basic_map
*bmap
,
4915 int div
, int ineq
, int lower
, int *pairs
)
4922 o_div
= isl_basic_map_offset(bmap
, isl_dim_div
);
4923 lower_bound_from_parallel(bmap
, ineq
, lower
, o_div
+ div
, &c
);
4924 bmap
= isl_basic_map_fix(bmap
, isl_dim_div
, div
, c
);
4929 return isl_basic_map_drop_redundant_divs(bmap
);
4932 /* Do any of the integer divisions of "bmap" involve integer division "div"?
4934 * The integer division "div" could only ever appear in any later
4935 * integer division (with an explicit representation).
4937 static isl_bool
any_div_involves_div(__isl_keep isl_basic_map
*bmap
, int div
)
4940 isl_size v_div
, n_div
;
4942 v_div
= isl_basic_map_var_offset(bmap
, isl_dim_div
);
4943 n_div
= isl_basic_map_dim(bmap
, isl_dim_div
);
4944 if (v_div
< 0 || n_div
< 0)
4945 return isl_bool_error
;
4947 for (i
= div
+ 1; i
< n_div
; ++i
) {
4950 unknown
= isl_basic_map_div_is_marked_unknown(bmap
, i
);
4952 return isl_bool_error
;
4955 if (!isl_int_is_zero(bmap
->div
[i
][1 + 1 + v_div
+ div
]))
4956 return isl_bool_true
;
4959 return isl_bool_false
;
4962 /* Remove divs that are not strictly needed based on the inequality
4964 * In particular, if a div only occurs positively (or negatively)
4965 * in constraints, then it can simply be dropped.
4966 * Also, if a div occurs in only two constraints and if moreover
4967 * those two constraints are opposite to each other, except for the constant
4968 * term and if the sum of the constant terms is such that for any value
4969 * of the other values, there is always at least one integer value of the
4970 * div, i.e., if one plus this sum is greater than or equal to
4971 * the (absolute value) of the coefficient of the div in the constraints,
4972 * then we can also simply drop the div.
4974 * If an existentially quantified variable does not have an explicit
4975 * representation, appears in only a single lower bound that does not
4976 * involve any other such existentially quantified variables and appears
4977 * in this lower bound with coefficient 1,
4978 * then fix the variable to the value of the lower bound. That is,
4979 * turn the inequality into an equality.
4980 * If for any value of the other variables, there is any value
4981 * for the existentially quantified variable satisfying the constraints,
4982 * then this lower bound also satisfies the constraints.
4983 * It is therefore safe to pick this lower bound.
4985 * The same reasoning holds even if the coefficient is not one.
4986 * However, fixing the variable to the value of the lower bound may
4987 * in general introduce an extra integer division, in which case
4988 * it may be better to pick another value.
4989 * If this integer division has a known constant value, then plugging
4990 * in this constant value removes the existentially quantified variable
4991 * completely. In particular, if the lower bound is of the form
4992 * ceil((-f(x) - c)/n) and there are two constraints, f(x) + c0 >= 0 and
4993 * -f(x) + c1 >= 0 such that ceil((-c1 - c)/n) = ceil((c0 - c)/n),
4994 * then the existentially quantified variable can be assigned this
4997 * We skip divs that appear in equalities or in the definition of other divs.
4998 * Divs that appear in the definition of other divs usually occur in at least
4999 * 4 constraints, but the constraints may have been simplified.
5001 * If any divs are left after these simple checks then we move on
5002 * to more complicated cases in drop_more_redundant_divs.
5004 static __isl_give isl_basic_map
*isl_basic_map_drop_redundant_divs_ineq(
5005 __isl_take isl_basic_map
*bmap
)
5015 if (bmap
->n_div
== 0)
5018 off
= isl_basic_map_var_offset(bmap
, isl_dim_div
);
5020 return isl_basic_map_free(bmap
);
5021 pairs
= isl_calloc_array(bmap
->ctx
, int, bmap
->n_div
);
5025 n_ineq
= isl_basic_map_n_inequality(bmap
);
5028 for (i
= 0; i
< bmap
->n_div
; ++i
) {
5030 int last_pos
, last_neg
;
5033 isl_bool involves
, opp
, set_div
;
5035 defined
= !isl_int_is_zero(bmap
->div
[i
][0]);
5036 involves
= any_div_involves_div(bmap
, i
);
5041 for (j
= 0; j
< bmap
->n_eq
; ++j
)
5042 if (!isl_int_is_zero(bmap
->eq
[j
][1 + off
+ i
]))
5048 for (j
= 0; j
< bmap
->n_ineq
; ++j
) {
5049 if (isl_int_is_pos(bmap
->ineq
[j
][1 + off
+ i
])) {
5053 if (isl_int_is_neg(bmap
->ineq
[j
][1 + off
+ i
])) {
5058 pairs
[i
] = pos
* neg
;
5059 if (pairs
[i
] == 0) {
5060 for (j
= bmap
->n_ineq
- 1; j
>= 0; --j
)
5061 if (!isl_int_is_zero(bmap
->ineq
[j
][1+off
+i
]))
5062 isl_basic_map_drop_inequality(bmap
, j
);
5063 bmap
= isl_basic_map_drop_div(bmap
, i
);
5064 return drop_redundant_divs_again(bmap
, pairs
, 0);
5067 opp
= isl_bool_false
;
5069 opp
= is_opposite(bmap
, last_pos
, last_neg
);
5074 isl_bool single
, one
;
5078 single
= single_unknown(bmap
, last_pos
, i
);
5083 one
= has_coef_one(bmap
, i
, last_pos
);
5087 return set_eq_and_try_again(bmap
, last_pos
,
5089 lower
= lower_bound_is_cst(bmap
, i
, last_pos
);
5093 return fix_cst_lower(bmap
, i
, last_pos
, lower
,
5098 isl_int_add(bmap
->ineq
[last_pos
][0],
5099 bmap
->ineq
[last_pos
][0], bmap
->ineq
[last_neg
][0]);
5100 isl_int_add_ui(bmap
->ineq
[last_pos
][0],
5101 bmap
->ineq
[last_pos
][0], 1);
5102 redundant
= isl_int_ge(bmap
->ineq
[last_pos
][0],
5103 bmap
->ineq
[last_pos
][1+off
+i
]);
5104 isl_int_sub_ui(bmap
->ineq
[last_pos
][0],
5105 bmap
->ineq
[last_pos
][0], 1);
5106 isl_int_sub(bmap
->ineq
[last_pos
][0],
5107 bmap
->ineq
[last_pos
][0], bmap
->ineq
[last_neg
][0]);
5109 return drop_div_and_try_again(bmap
, i
,
5110 last_pos
, last_neg
, pairs
);
5112 set_div
= isl_bool_false
;
5114 set_div
= ok_to_set_div_from_bound(bmap
, i
, last_pos
);
5116 return isl_basic_map_free(bmap
);
5118 bmap
= set_div_from_lower_bound(bmap
, i
, last_pos
);
5119 return drop_redundant_divs_again(bmap
, pairs
, 1);
5126 return coalesce_or_drop_more_redundant_divs(bmap
, pairs
, n
);
5132 isl_basic_map_free(bmap
);
5136 /* Consider the coefficients at "c" as a row vector and replace
5137 * them with their product with "T". "T" is assumed to be a square matrix.
5139 static isl_stat
preimage(isl_int
*c
, __isl_keep isl_mat
*T
)
5145 n
= isl_mat_rows(T
);
5147 return isl_stat_error
;
5148 if (isl_seq_first_non_zero(c
, n
) == -1)
5150 ctx
= isl_mat_get_ctx(T
);
5151 v
= isl_vec_alloc(ctx
, n
);
5153 return isl_stat_error
;
5154 isl_seq_swp_or_cpy(v
->el
, c
, n
);
5155 v
= isl_vec_mat_product(v
, isl_mat_copy(T
));
5157 return isl_stat_error
;
5158 isl_seq_swp_or_cpy(c
, v
->el
, n
);
5164 /* Plug in T for the variables in "bmap" starting at "pos".
5165 * T is a linear unimodular matrix, i.e., without constant term.
5167 static __isl_give isl_basic_map
*isl_basic_map_preimage_vars(
5168 __isl_take isl_basic_map
*bmap
, unsigned pos
, __isl_take isl_mat
*T
)
5171 isl_size n_row
, n_col
;
5173 bmap
= isl_basic_map_cow(bmap
);
5174 n_row
= isl_mat_rows(T
);
5175 n_col
= isl_mat_cols(T
);
5176 if (!bmap
|| n_row
< 0 || n_col
< 0)
5180 isl_die(isl_mat_get_ctx(T
), isl_error_invalid
,
5181 "expecting square matrix", goto error
);
5183 if (isl_basic_map_check_range(bmap
, isl_dim_all
, pos
, n_col
) < 0)
5186 for (i
= 0; i
< bmap
->n_eq
; ++i
)
5187 if (preimage(bmap
->eq
[i
] + 1 + pos
, T
) < 0)
5189 for (i
= 0; i
< bmap
->n_ineq
; ++i
)
5190 if (preimage(bmap
->ineq
[i
] + 1 + pos
, T
) < 0)
5192 for (i
= 0; i
< bmap
->n_div
; ++i
) {
5193 if (isl_basic_map_div_is_marked_unknown(bmap
, i
))
5195 if (preimage(bmap
->div
[i
] + 1 + 1 + pos
, T
) < 0)
5202 isl_basic_map_free(bmap
);
5207 /* Remove divs that are not strictly needed.
5209 * First look for an equality constraint involving two or more
5210 * existentially quantified variables without an explicit
5211 * representation. Replace the combination that appears
5212 * in the equality constraint by a single existentially quantified
5213 * variable such that the equality can be used to derive
5214 * an explicit representation for the variable.
5215 * If there are no more such equality constraints, then continue
5216 * with isl_basic_map_drop_redundant_divs_ineq.
5218 * In particular, if the equality constraint is of the form
5220 * f(x) + \sum_i c_i a_i = 0
5222 * with a_i existentially quantified variable without explicit
5223 * representation, then apply a transformation on the existentially
5224 * quantified variables to turn the constraint into
5228 * with g the gcd of the c_i.
5229 * In order to easily identify which existentially quantified variables
5230 * have a complete explicit representation, i.e., without being defined
5231 * in terms of other existentially quantified variables without
5232 * an explicit representation, the existentially quantified variables
5235 * The variable transformation is computed by extending the row
5236 * [c_1/g ... c_n/g] to a unimodular matrix, obtaining the transformation
5238 * [a_1'] [c_1/g ... c_n/g] [ a_1 ]
5243 * with [c_1/g ... c_n/g] representing the first row of U.
5244 * The inverse of U is then plugged into the original constraints.
5245 * The call to isl_basic_map_simplify makes sure the explicit
5246 * representation for a_1' is extracted from the equality constraint.
5248 __isl_give isl_basic_map
*isl_basic_map_drop_redundant_divs(
5249 __isl_take isl_basic_map
*bmap
)
5261 if (isl_basic_map_divs_known(bmap
))
5262 return isl_basic_map_drop_redundant_divs_ineq(bmap
);
5263 if (bmap
->n_eq
== 0)
5264 return isl_basic_map_drop_redundant_divs_ineq(bmap
);
5265 bmap
= isl_basic_map_sort_divs(bmap
);
5269 first
= isl_basic_map_first_unknown_div(bmap
);
5271 return isl_basic_map_free(bmap
);
5273 o_div
= isl_basic_map_offset(bmap
, isl_dim_div
);
5274 n_div
= isl_basic_map_dim(bmap
, isl_dim_div
);
5276 return isl_basic_map_free(bmap
);
5278 for (i
= 0; i
< bmap
->n_eq
; ++i
) {
5279 l
= isl_seq_first_non_zero(bmap
->eq
[i
] + o_div
+ first
,
5284 if (isl_seq_first_non_zero(bmap
->eq
[i
] + o_div
+ l
+ 1,
5285 n_div
- (l
+ 1)) == -1)
5289 if (i
>= bmap
->n_eq
)
5290 return isl_basic_map_drop_redundant_divs_ineq(bmap
);
5292 ctx
= isl_basic_map_get_ctx(bmap
);
5293 T
= isl_mat_alloc(ctx
, n_div
- l
, n_div
- l
);
5295 return isl_basic_map_free(bmap
);
5296 isl_seq_cpy(T
->row
[0], bmap
->eq
[i
] + o_div
+ l
, n_div
- l
);
5297 T
= isl_mat_normalize_row(T
, 0);
5298 T
= isl_mat_unimodular_complete(T
, 1);
5299 T
= isl_mat_right_inverse(T
);
5301 for (i
= l
; i
< n_div
; ++i
)
5302 bmap
= isl_basic_map_mark_div_unknown(bmap
, i
);
5303 bmap
= isl_basic_map_preimage_vars(bmap
, o_div
- 1 + l
, T
);
5304 bmap
= isl_basic_map_simplify(bmap
);
5306 return isl_basic_map_drop_redundant_divs(bmap
);
5309 /* Does "bmap" satisfy any equality that involves more than 2 variables
5310 * and/or has coefficients different from -1 and 1?
5312 static isl_bool
has_multiple_var_equality(__isl_keep isl_basic_map
*bmap
)
5317 total
= isl_basic_map_dim(bmap
, isl_dim_all
);
5319 return isl_bool_error
;
5321 for (i
= 0; i
< bmap
->n_eq
; ++i
) {
5324 j
= isl_seq_first_non_zero(bmap
->eq
[i
] + 1, total
);
5327 if (!isl_int_is_one(bmap
->eq
[i
][1 + j
]) &&
5328 !isl_int_is_negone(bmap
->eq
[i
][1 + j
]))
5329 return isl_bool_true
;
5332 k
= isl_seq_first_non_zero(bmap
->eq
[i
] + 1 + j
, total
- j
);
5336 if (!isl_int_is_one(bmap
->eq
[i
][1 + j
]) &&
5337 !isl_int_is_negone(bmap
->eq
[i
][1 + j
]))
5338 return isl_bool_true
;
5341 k
= isl_seq_first_non_zero(bmap
->eq
[i
] + 1 + j
, total
- j
);
5343 return isl_bool_true
;
5346 return isl_bool_false
;
5349 /* Remove any common factor g from the constraint coefficients in "v".
5350 * The constant term is stored in the first position and is replaced
5351 * by floor(c/g). If any common factor is removed and if this results
5352 * in a tightening of the constraint, then set *tightened.
5354 static __isl_give isl_vec
*normalize_constraint(__isl_take isl_vec
*v
,
5361 ctx
= isl_vec_get_ctx(v
);
5362 isl_seq_gcd(v
->el
+ 1, v
->size
- 1, &ctx
->normalize_gcd
);
5363 if (isl_int_is_zero(ctx
->normalize_gcd
))
5365 if (isl_int_is_one(ctx
->normalize_gcd
))
5370 if (tightened
&& !isl_int_is_divisible_by(v
->el
[0], ctx
->normalize_gcd
))
5372 isl_int_fdiv_q(v
->el
[0], v
->el
[0], ctx
->normalize_gcd
);
5373 isl_seq_scale_down(v
->el
+ 1, v
->el
+ 1, ctx
->normalize_gcd
,
5378 /* Internal representation used by isl_basic_map_reduce_coefficients.
5380 * "total" is the total dimensionality of the original basic map.
5381 * "v" is a temporary vector of size 1 + total that can be used
5382 * to store constraint coefficients.
5383 * "T" is the variable compression.
5384 * "T2" is the inverse transformation.
5385 * "tightened" is set if any constant term got tightened
5386 * while reducing the coefficients.
5388 struct isl_reduce_coefficients_data
{
5396 /* Free all memory allocated in "data".
5398 static void isl_reduce_coefficients_data_clear(
5399 struct isl_reduce_coefficients_data
*data
)
5401 data
->T
= isl_mat_free(data
->T
);
5402 data
->T2
= isl_mat_free(data
->T2
);
5403 data
->v
= isl_vec_free(data
->v
);
5406 /* Initialize "data" for "bmap", freeing all allocated memory
5407 * if anything goes wrong.
5409 * In particular, construct a variable compression
5410 * from the equality constraints of "bmap" and
5411 * allocate a temporary vector.
5413 static isl_stat
isl_reduce_coefficients_data_init(
5414 __isl_keep isl_basic_map
*bmap
,
5415 struct isl_reduce_coefficients_data
*data
)
5423 data
->tightened
= 0;
5425 data
->total
= isl_basic_map_dim(bmap
, isl_dim_all
);
5426 if (data
->total
< 0)
5427 return isl_stat_error
;
5428 ctx
= isl_basic_map_get_ctx(bmap
);
5429 data
->v
= isl_vec_alloc(ctx
, 1 + data
->total
);
5431 return isl_stat_error
;
5433 eq
= isl_mat_sub_alloc6(ctx
, bmap
->eq
, 0, bmap
->n_eq
,
5434 0, 1 + data
->total
);
5435 data
->T
= isl_mat_variable_compression(eq
, &data
->T2
);
5436 if (!data
->T
|| !data
->T2
)
5441 isl_reduce_coefficients_data_clear(data
);
5442 return isl_stat_error
;
5445 /* Reduce the coefficients of "bmap" by applying the variable compression
5447 * In particular, apply the variable compression to each constraint,
5448 * factor out any common factor in the non-constant coefficients and
5449 * then apply the inverse of the compression.
5451 * Only apply the reduction on a single copy of the basic map
5452 * since the reduction may leave the result in an inconsistent state.
5453 * In particular, the constraints may not be gaussed.
5455 static __isl_give isl_basic_map
*reduce_coefficients(
5456 __isl_take isl_basic_map
*bmap
,
5457 struct isl_reduce_coefficients_data
*data
)
5462 total
= isl_basic_map_dim(bmap
, isl_dim_all
);
5464 return isl_basic_map_free(bmap
);
5465 if (total
!= data
->total
)
5466 isl_die(isl_basic_map_get_ctx(bmap
), isl_error_internal
,
5467 "total dimensionality changed unexpectedly",
5468 return isl_basic_map_free(bmap
));
5470 bmap
= isl_basic_map_cow(bmap
);
5474 for (i
= 0; i
< bmap
->n_ineq
; ++i
) {
5475 isl_seq_cpy(data
->v
->el
, bmap
->ineq
[i
], 1 + data
->total
);
5476 data
->v
= isl_vec_mat_product(data
->v
, isl_mat_copy(data
->T
));
5477 data
->v
= normalize_constraint(data
->v
, &data
->tightened
);
5478 data
->v
= isl_vec_mat_product(data
->v
, isl_mat_copy(data
->T2
));
5480 return isl_basic_map_free(bmap
);
5481 isl_seq_cpy(bmap
->ineq
[i
], data
->v
->el
, 1 + data
->total
);
5484 ISL_F_SET(bmap
, ISL_BASIC_MAP_REDUCED_COEFFICIENTS
);
5489 /* If "bmap" is an integer set that satisfies any equality involving
5490 * more than 2 variables and/or has coefficients different from -1 and 1,
5491 * then use variable compression to reduce the coefficients by removing
5492 * any (hidden) common factor.
5493 * In particular, apply the variable compression to each constraint,
5494 * factor out any common factor in the non-constant coefficients and
5495 * then apply the inverse of the compression.
5496 * At the end, we mark the basic map as having reduced constants.
5497 * If this flag is still set on the next invocation of this function,
5498 * then we skip the computation.
5500 * Removing a common factor may result in a tightening of some of
5501 * the constraints. If this happens, then we may end up with two
5502 * opposite inequalities that can be replaced by an equality.
5503 * We therefore call isl_basic_map_detect_inequality_pairs,
5504 * which checks for such pairs of inequalities as well as eliminate_divs_eq
5505 * and isl_basic_map_gauss if such a pair was found.
5507 * Tightening may also result in some other constraints becoming
5508 * (rationally) redundant with respect to the tightened constraint
5509 * (in combination with other constraints). The basic map may
5510 * therefore no longer be assumed to have no redundant constraints.
5512 * Note that this function may leave the result in an inconsistent state.
5513 * In particular, the constraints may not be gaussed.
5514 * Unfortunately, isl_map_coalesce actually depends on this inconsistent state
5515 * for some of the test cases to pass successfully.
5516 * Any potential modification of the representation is therefore only
5517 * performed on a single copy of the basic map.
5519 __isl_give isl_basic_map
*isl_basic_map_reduce_coefficients(
5520 __isl_take isl_basic_map
*bmap
)
5522 struct isl_reduce_coefficients_data data
;
5527 if (ISL_F_ISSET(bmap
, ISL_BASIC_MAP_REDUCED_COEFFICIENTS
))
5529 if (isl_basic_map_is_rational(bmap
))
5531 if (bmap
->n_eq
== 0)
5533 multi
= has_multiple_var_equality(bmap
);
5535 return isl_basic_map_free(bmap
);
5539 if (isl_reduce_coefficients_data_init(bmap
, &data
) < 0)
5540 return isl_basic_map_free(bmap
);
5542 if (data
.T
->n_col
== 0) {
5543 isl_reduce_coefficients_data_clear(&data
);
5544 return isl_basic_map_set_to_empty(bmap
);
5547 bmap
= reduce_coefficients(bmap
, &data
);
5551 isl_reduce_coefficients_data_clear(&data
);
5553 if (data
.tightened
) {
5556 ISL_F_CLR(bmap
, ISL_BASIC_MAP_NO_REDUNDANT
);
5557 bmap
= isl_basic_map_detect_inequality_pairs(bmap
, &progress
);
5559 bmap
= isl_basic_map_gauss(bmap
, NULL
);
5560 bmap
= eliminate_divs_eq(bmap
, &progress
);
5566 isl_reduce_coefficients_data_clear(&data
);
5567 return isl_basic_map_free(bmap
);
5570 /* Shift the integer division at position "div" of "bmap"
5571 * by "shift" times the variable at position "pos".
5572 * "pos" is as determined by isl_basic_map_offset, i.e., pos == 0
5573 * corresponds to the constant term.
5575 * That is, if the integer division has the form
5579 * then replace it by
5581 * floor((f(x) + shift * d * x_pos)/d) - shift * x_pos
5583 __isl_give isl_basic_map
*isl_basic_map_shift_div(
5584 __isl_take isl_basic_map
*bmap
, int div
, int pos
, isl_int shift
)
5587 isl_size total
, n_div
;
5589 if (isl_int_is_zero(shift
))
5591 total
= isl_basic_map_dim(bmap
, isl_dim_all
);
5592 n_div
= isl_basic_map_dim(bmap
, isl_dim_div
);
5594 if (total
< 0 || n_div
< 0)
5595 return isl_basic_map_free(bmap
);
5597 isl_int_addmul(bmap
->div
[div
][1 + pos
], shift
, bmap
->div
[div
][0]);
5599 for (i
= 0; i
< bmap
->n_eq
; ++i
) {
5600 if (isl_int_is_zero(bmap
->eq
[i
][1 + total
+ div
]))
5602 isl_int_submul(bmap
->eq
[i
][pos
],
5603 shift
, bmap
->eq
[i
][1 + total
+ div
]);
5605 for (i
= 0; i
< bmap
->n_ineq
; ++i
) {
5606 if (isl_int_is_zero(bmap
->ineq
[i
][1 + total
+ div
]))
5608 isl_int_submul(bmap
->ineq
[i
][pos
],
5609 shift
, bmap
->ineq
[i
][1 + total
+ div
]);
5611 for (i
= 0; i
< bmap
->n_div
; ++i
) {
5612 if (isl_int_is_zero(bmap
->div
[i
][0]))
5614 if (isl_int_is_zero(bmap
->div
[i
][1 + 1 + total
+ div
]))
5616 isl_int_submul(bmap
->div
[i
][1 + pos
],
5617 shift
, bmap
->div
[i
][1 + 1 + total
+ div
]);