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 /* Mark "bmap" as having one or more inequality constraints modified.
32 * If "equivalent" is set, then this modification was done based
33 * on an equality constraint already available in "bmap".
35 * Any modification may result in the constraints no longer being sorted and
36 * may also undo the effect of reduce_coefficients.
38 * A modification that uses extra information may also result
39 * in the modified constraint(s) becoming redundant or
40 * turning into an implicit equality constraint.
42 static __isl_give isl_basic_map
*isl_basic_map_modify_inequality(
43 __isl_take isl_basic_map
*bmap
, int equivalent
)
47 ISL_F_CLR(bmap
, ISL_BASIC_MAP_SORTED
);
48 ISL_F_CLR(bmap
, ISL_BASIC_MAP_REDUCED_COEFFICIENTS
);
51 ISL_F_CLR(bmap
, ISL_BASIC_MAP_NO_REDUNDANT
);
52 ISL_F_CLR(bmap
, ISL_BASIC_MAP_NO_IMPLICIT
);
56 static void swap_equality(__isl_keep isl_basic_map
*bmap
, int a
, int b
)
58 isl_int
*t
= bmap
->eq
[a
];
59 bmap
->eq
[a
] = bmap
->eq
[b
];
63 static void swap_inequality(__isl_keep isl_basic_map
*bmap
, int a
, int b
)
66 isl_int
*t
= bmap
->ineq
[a
];
67 bmap
->ineq
[a
] = bmap
->ineq
[b
];
72 /* Scale down the inequality constraint "ineq" of length "len"
74 * All the coefficients, except the constant term,
75 * are assumed to be multiples of "f".
77 * If the factor is 0 or 1, then no scaling needs to be performed.
79 * If scaling is performed then take into account that the constraint
80 * is modified (not simply based on an equality constraint).
82 static __isl_give isl_basic_map
*scale_down_inequality(
83 __isl_take isl_basic_map
*bmap
, int ineq
, isl_int f
, unsigned len
)
88 if (isl_int_is_zero(f
) || isl_int_is_one(f
))
91 isl_int_fdiv_q(bmap
->ineq
[ineq
][0], bmap
->ineq
[ineq
][0], f
);
92 isl_seq_scale_down(bmap
->ineq
[ineq
] + 1, bmap
->ineq
[ineq
] + 1, f
, len
);
94 bmap
= isl_basic_map_modify_inequality(bmap
, 0);
99 __isl_give isl_basic_map
*isl_basic_map_normalize_constraints(
100 __isl_take isl_basic_map
*bmap
)
104 isl_size total
= isl_basic_map_dim(bmap
, isl_dim_all
);
107 return isl_basic_map_free(bmap
);
110 for (i
= bmap
->n_eq
- 1; i
>= 0; --i
) {
111 isl_seq_gcd(bmap
->eq
[i
]+1, total
, &gcd
);
112 if (isl_int_is_zero(gcd
)) {
113 if (!isl_int_is_zero(bmap
->eq
[i
][0])) {
114 bmap
= isl_basic_map_set_to_empty(bmap
);
117 if (isl_basic_map_drop_equality(bmap
, i
) < 0)
121 if (ISL_F_ISSET(bmap
, ISL_BASIC_MAP_RATIONAL
))
122 isl_int_gcd(gcd
, gcd
, bmap
->eq
[i
][0]);
123 if (isl_int_is_one(gcd
))
125 if (!isl_int_is_divisible_by(bmap
->eq
[i
][0], gcd
)) {
126 bmap
= isl_basic_map_set_to_empty(bmap
);
129 isl_seq_scale_down(bmap
->eq
[i
], bmap
->eq
[i
], gcd
, 1+total
);
132 for (i
= bmap
->n_ineq
- 1; i
>= 0; --i
) {
133 isl_seq_gcd(bmap
->ineq
[i
]+1, total
, &gcd
);
134 if (isl_int_is_zero(gcd
)) {
135 if (isl_int_is_neg(bmap
->ineq
[i
][0])) {
136 bmap
= isl_basic_map_set_to_empty(bmap
);
139 if (isl_basic_map_drop_inequality(bmap
, i
) < 0)
143 if (ISL_F_ISSET(bmap
, ISL_BASIC_MAP_RATIONAL
))
144 isl_int_gcd(gcd
, gcd
, bmap
->ineq
[i
][0]);
145 bmap
= scale_down_inequality(bmap
, i
, gcd
, total
);
154 isl_basic_map_free(bmap
);
158 __isl_give isl_basic_set
*isl_basic_set_normalize_constraints(
159 __isl_take isl_basic_set
*bset
)
161 isl_basic_map
*bmap
= bset_to_bmap(bset
);
162 return bset_from_bmap(isl_basic_map_normalize_constraints(bmap
));
165 /* Reduce the coefficient of the variable at position "pos"
166 * in integer division "div", such that it lies in the half-open
167 * interval (1/2,1/2], extracting any excess value from this integer division.
168 * "pos" is as determined by isl_basic_map_offset, i.e., pos == 0
169 * corresponds to the constant term.
171 * That is, the integer division is of the form
173 * floor((... + (c * d + r) * x_pos + ...)/d)
175 * with -d < 2 * r <= d.
178 * floor((... + r * x_pos + ...)/d) + c * x_pos
180 * If 2 * ((c * d + r) % d) <= d, then c = floor((c * d + r)/d).
181 * Otherwise, c = floor((c * d + r)/d) + 1.
183 * This is the same normalization that is performed by isl_aff_floor.
185 static __isl_give isl_basic_map
*reduce_coefficient_in_div(
186 __isl_take isl_basic_map
*bmap
, int div
, int pos
)
192 isl_int_fdiv_r(shift
, bmap
->div
[div
][1 + pos
], bmap
->div
[div
][0]);
193 isl_int_mul_ui(shift
, shift
, 2);
194 add_one
= isl_int_gt(shift
, bmap
->div
[div
][0]);
195 isl_int_fdiv_q(shift
, bmap
->div
[div
][1 + pos
], bmap
->div
[div
][0]);
197 isl_int_add_ui(shift
, shift
, 1);
198 isl_int_neg(shift
, shift
);
199 bmap
= isl_basic_map_shift_div(bmap
, div
, pos
, shift
);
200 isl_int_clear(shift
);
205 /* Does the coefficient of the variable at position "pos"
206 * in integer division "div" need to be reduced?
207 * That is, does it lie outside the half-open interval (1/2,1/2]?
208 * The coefficient c/d lies outside this interval if abs(2 * c) >= d and
211 static isl_bool
needs_reduction(__isl_keep isl_basic_map
*bmap
, int div
,
216 if (isl_int_is_zero(bmap
->div
[div
][1 + pos
]))
217 return isl_bool_false
;
219 isl_int_mul_ui(bmap
->div
[div
][1 + pos
], bmap
->div
[div
][1 + pos
], 2);
220 r
= isl_int_abs_ge(bmap
->div
[div
][1 + pos
], bmap
->div
[div
][0]) &&
221 !isl_int_eq(bmap
->div
[div
][1 + pos
], bmap
->div
[div
][0]);
222 isl_int_divexact_ui(bmap
->div
[div
][1 + pos
],
223 bmap
->div
[div
][1 + pos
], 2);
228 /* Reduce the coefficients (including the constant term) of
229 * integer division "div", if needed.
230 * In particular, make sure all coefficients lie in
231 * the half-open interval (1/2,1/2].
233 static __isl_give isl_basic_map
*reduce_div_coefficients_of_div(
234 __isl_take isl_basic_map
*bmap
, int div
)
239 total
= isl_basic_map_dim(bmap
, isl_dim_all
);
241 return isl_basic_map_free(bmap
);
242 for (i
= 0; i
< 1 + total
; ++i
) {
245 reduce
= needs_reduction(bmap
, div
, i
);
247 return isl_basic_map_free(bmap
);
250 bmap
= reduce_coefficient_in_div(bmap
, div
, i
);
258 /* Reduce the coefficients (including the constant term) of
259 * the known integer divisions, if needed
260 * In particular, make sure all coefficients lie in
261 * the half-open interval (1/2,1/2].
263 static __isl_give isl_basic_map
*reduce_div_coefficients(
264 __isl_take isl_basic_map
*bmap
)
270 if (bmap
->n_div
== 0)
273 for (i
= 0; i
< bmap
->n_div
; ++i
) {
274 if (isl_int_is_zero(bmap
->div
[i
][0]))
276 bmap
= reduce_div_coefficients_of_div(bmap
, i
);
284 /* Remove any common factor in numerator and denominator of the div expression,
285 * not taking into account the constant term.
286 * That is, if the div is of the form
288 * floor((a + m f(x))/(m d))
292 * floor((floor(a/m) + f(x))/d)
294 * The difference {a/m}/d in the argument satisfies 0 <= {a/m}/d < 1/d
295 * and can therefore not influence the result of the floor.
297 static __isl_give isl_basic_map
*normalize_div_expression(
298 __isl_take isl_basic_map
*bmap
, int div
)
300 isl_size total
= isl_basic_map_dim(bmap
, isl_dim_all
);
301 isl_ctx
*ctx
= bmap
->ctx
;
304 return isl_basic_map_free(bmap
);
305 if (isl_int_is_zero(bmap
->div
[div
][0]))
307 isl_seq_gcd(bmap
->div
[div
] + 2, total
, &ctx
->normalize_gcd
);
308 isl_int_gcd(ctx
->normalize_gcd
, ctx
->normalize_gcd
, bmap
->div
[div
][0]);
309 if (isl_int_is_one(ctx
->normalize_gcd
))
311 isl_int_fdiv_q(bmap
->div
[div
][1], bmap
->div
[div
][1],
313 isl_int_divexact(bmap
->div
[div
][0], bmap
->div
[div
][0],
315 isl_seq_scale_down(bmap
->div
[div
] + 2, bmap
->div
[div
] + 2,
316 ctx
->normalize_gcd
, total
);
321 /* Remove any common factor in numerator and denominator of a div expression,
322 * not taking into account the constant term.
323 * That is, look for any div of the form
325 * floor((a + m f(x))/(m d))
329 * floor((floor(a/m) + f(x))/d)
331 * The difference {a/m}/d in the argument satisfies 0 <= {a/m}/d < 1/d
332 * and can therefore not influence the result of the floor.
334 static __isl_give isl_basic_map
*normalize_div_expressions(
335 __isl_take isl_basic_map
*bmap
)
341 if (bmap
->n_div
== 0)
344 for (i
= 0; i
< bmap
->n_div
; ++i
)
345 bmap
= normalize_div_expression(bmap
, i
);
350 /* Eliminate the variable at position "pos" from the constraints of "bmap"
351 * using the equality constraint "eq".
352 * If "keep_divs" is set, then try and preserve
353 * the integer division expressions. In this case, these expressions
354 * are assumed to have been ordered.
355 * If "equivalent" is set, then the elimination is performed
356 * using an equality constraint of "bmap", meaning that the meaning
357 * of the constraints is preserved.
359 static __isl_give isl_basic_map
*eliminate_var_using_equality(
360 __isl_take isl_basic_map
*bmap
,
361 unsigned pos
, isl_int
*eq
, int keep_divs
, int equivalent
, int *progress
)
369 total
= isl_basic_map_dim(bmap
, isl_dim_all
);
370 v_div
= isl_basic_map_var_offset(bmap
, isl_dim_div
);
371 if (total
< 0 || v_div
< 0)
372 return isl_basic_map_free(bmap
);
373 ctx
= isl_basic_map_get_ctx(bmap
);
374 last_div
= isl_seq_last_non_zero(eq
+ 1 + v_div
, bmap
->n_div
);
375 for (k
= 0; k
< bmap
->n_eq
; ++k
) {
376 if (bmap
->eq
[k
] == eq
)
378 if (isl_int_is_zero(bmap
->eq
[k
][1+pos
]))
382 isl_seq_elim(bmap
->eq
[k
], eq
, 1+pos
, 1+total
, NULL
);
383 isl_seq_normalize(ctx
, bmap
->eq
[k
], 1 + total
);
386 for (k
= 0; k
< bmap
->n_ineq
; ++k
) {
387 if (isl_int_is_zero(bmap
->ineq
[k
][1+pos
]))
391 isl_seq_elim(bmap
->ineq
[k
], eq
, 1+pos
, 1+total
, NULL
);
392 isl_seq_gcd(bmap
->ineq
[k
], 1 + total
, &ctx
->normalize_gcd
);
393 bmap
= scale_down_inequality(bmap
, k
, ctx
->normalize_gcd
,
395 bmap
= isl_basic_map_modify_inequality(bmap
, equivalent
);
400 for (k
= 0; k
< bmap
->n_div
; ++k
) {
401 if (isl_int_is_zero(bmap
->div
[k
][0]))
403 if (isl_int_is_zero(bmap
->div
[k
][1+1+pos
]))
407 /* We need to be careful about circular definitions,
408 * so for now we just remove the definition of div k
409 * if the equality contains any divs.
410 * If keep_divs is set, then the divs have been ordered
411 * and we can keep the definition as long as the result
414 if (last_div
== -1 || (keep_divs
&& last_div
< k
)) {
415 isl_seq_elim(bmap
->div
[k
]+1, eq
,
416 1+pos
, 1+total
, &bmap
->div
[k
][0]);
417 bmap
= normalize_div_expression(bmap
, k
);
421 isl_seq_clr(bmap
->div
[k
], 1 + total
);
427 /* Eliminate and remove the local variable at position "pos" of "bmap"
428 * using the equality constraint "eq".
429 * If "keep_divs" is set, then try and preserve
430 * the integer division expressions. In this case, these expressions
431 * are assumed to have been ordered.
432 * If "equivalent" is set, then the elimination is performed
433 * using an equality constraint of "bmap", meaning that the meaning
434 * of the constraints is preserved.
436 static __isl_give isl_basic_map
*eliminate_div(__isl_take isl_basic_map
*bmap
,
437 isl_int
*eq
, unsigned div
, int keep_divs
, int equivalent
)
442 v_div
= isl_basic_map_var_offset(bmap
, isl_dim_div
);
444 return isl_basic_map_free(bmap
);
446 bmap
= eliminate_var_using_equality(bmap
, pos
, eq
, keep_divs
,
449 bmap
= isl_basic_map_drop_div(bmap
, div
);
454 /* Check if elimination of div "div" using equality "eq" would not
455 * result in a div depending on a later div.
457 static isl_bool
ok_to_eliminate_div(__isl_keep isl_basic_map
*bmap
, isl_int
*eq
,
465 v_div
= isl_basic_map_var_offset(bmap
, isl_dim_div
);
467 return isl_bool_error
;
470 last_div
= isl_seq_last_non_zero(eq
+ 1 + v_div
, bmap
->n_div
);
471 if (last_div
< 0 || last_div
<= div
)
472 return isl_bool_true
;
474 for (k
= 0; k
<= last_div
; ++k
) {
475 if (isl_int_is_zero(bmap
->div
[k
][0]))
477 if (!isl_int_is_zero(bmap
->div
[k
][1 + 1 + pos
]))
478 return isl_bool_false
;
481 return isl_bool_true
;
484 /* Eliminate divs based on equalities
486 static __isl_give isl_basic_map
*eliminate_divs_eq(
487 __isl_take isl_basic_map
*bmap
, int *progress
)
494 bmap
= isl_basic_map_order_divs(bmap
);
499 off
= isl_basic_map_offset(bmap
, isl_dim_div
);
501 for (d
= bmap
->n_div
- 1; d
>= 0 ; --d
) {
502 for (i
= 0; i
< bmap
->n_eq
; ++i
) {
505 if (!isl_int_is_one(bmap
->eq
[i
][off
+ d
]) &&
506 !isl_int_is_negone(bmap
->eq
[i
][off
+ d
]))
508 ok
= ok_to_eliminate_div(bmap
, bmap
->eq
[i
], d
);
510 return isl_basic_map_free(bmap
);
515 bmap
= eliminate_div(bmap
, bmap
->eq
[i
], d
, 1, 1);
516 if (isl_basic_map_drop_equality(bmap
, i
) < 0)
517 return isl_basic_map_free(bmap
);
522 return eliminate_divs_eq(bmap
, progress
);
526 /* Eliminate divs based on inequalities
528 static __isl_give isl_basic_map
*eliminate_divs_ineq(
529 __isl_take isl_basic_map
*bmap
, int *progress
)
540 off
= isl_basic_map_offset(bmap
, isl_dim_div
);
542 for (d
= bmap
->n_div
- 1; d
>= 0 ; --d
) {
543 for (i
= 0; i
< bmap
->n_eq
; ++i
)
544 if (!isl_int_is_zero(bmap
->eq
[i
][off
+ d
]))
548 for (i
= 0; i
< bmap
->n_ineq
; ++i
)
549 if (isl_int_abs_gt(bmap
->ineq
[i
][off
+ d
], ctx
->one
))
551 if (i
< bmap
->n_ineq
)
554 bmap
= isl_basic_map_eliminate_vars(bmap
, (off
-1)+d
, 1);
555 if (!bmap
|| ISL_F_ISSET(bmap
, ISL_BASIC_MAP_EMPTY
))
557 bmap
= isl_basic_map_drop_div(bmap
, d
);
564 /* Does the equality constraint at position "eq" in "bmap" involve
565 * any local variables in the range [first, first + n)
566 * that are not marked as having an explicit representation?
568 static isl_bool
bmap_eq_involves_unknown_divs(__isl_keep isl_basic_map
*bmap
,
569 int eq
, unsigned first
, unsigned n
)
575 return isl_bool_error
;
577 o_div
= isl_basic_map_offset(bmap
, isl_dim_div
);
578 for (i
= 0; i
< n
; ++i
) {
581 if (isl_int_is_zero(bmap
->eq
[eq
][o_div
+ first
+ i
]))
583 unknown
= isl_basic_map_div_is_marked_unknown(bmap
, first
+ i
);
585 return isl_bool_error
;
587 return isl_bool_true
;
590 return isl_bool_false
;
593 /* The last local variable involved in the equality constraint
594 * at position "eq" in "bmap" is the local variable at position "div".
595 * It can therefore be used to extract an explicit representation
597 * Do so unless the local variable already has an explicit representation or
598 * the explicit representation would involve any other local variables
599 * that in turn do not have an explicit representation.
600 * An equality constraint involving local variables without an explicit
601 * representation can be used in isl_basic_map_drop_redundant_divs
602 * to separate out an independent local variable. Introducing
603 * an explicit representation here would block this transformation,
604 * while the partial explicit representation in itself is not very useful.
605 * Set *progress if anything is changed.
607 * The equality constraint is of the form
611 * with n a positive number. The explicit representation derived from
616 static __isl_give isl_basic_map
*set_div_from_eq(__isl_take isl_basic_map
*bmap
,
617 int div
, int eq
, int *progress
)
626 if (!isl_int_is_zero(bmap
->div
[div
][0]))
629 involves
= bmap_eq_involves_unknown_divs(bmap
, eq
, 0, div
);
631 return isl_basic_map_free(bmap
);
635 total
= isl_basic_map_dim(bmap
, isl_dim_all
);
637 return isl_basic_map_free(bmap
);
638 o_div
= isl_basic_map_offset(bmap
, isl_dim_div
);
639 isl_seq_neg(bmap
->div
[div
] + 1, bmap
->eq
[eq
], 1 + total
);
640 isl_int_set_si(bmap
->div
[div
][1 + o_div
+ div
], 0);
641 isl_int_set(bmap
->div
[div
][0], bmap
->eq
[eq
][o_div
+ div
]);
648 /* Perform fangcheng (Gaussian elimination) on the equality
649 * constraints of "bmap".
650 * That is, put them into row-echelon form, starting from the last column
651 * backward and use them to eliminate the corresponding coefficients
652 * from all constraints.
654 * If "progress" is not NULL, then it gets set if the elimination
655 * results in any changes.
656 * The elimination process may result in some equality constraints
657 * getting interchanged or removed.
658 * If "swap" or "drop" are not NULL, then they get called when
659 * two equality constraints get interchanged or
660 * when a number of final equality constraints get removed.
661 * As a special case, if the input turns out to be empty,
662 * then drop gets called with the number of removed equality
663 * constraints set to the total number of equality constraints.
664 * If "swap" or "drop" are not NULL, then the local variables (if any)
665 * are assumed to be in a valid order.
667 __isl_give isl_basic_map
*isl_basic_map_gauss5(__isl_take isl_basic_map
*bmap
,
669 isl_stat (*swap
)(unsigned a
, unsigned b
, void *user
),
670 isl_stat (*drop
)(unsigned n
, void *user
), void *user
)
680 bmap
= isl_basic_map_order_divs(bmap
);
682 total
= isl_basic_map_dim(bmap
, isl_dim_all
);
684 return isl_basic_map_free(bmap
);
686 total_var
= total
- bmap
->n_div
;
688 last_var
= total
- 1;
689 for (done
= 0; done
< bmap
->n_eq
; ++done
) {
690 for (; last_var
>= 0; --last_var
) {
691 for (k
= done
; k
< bmap
->n_eq
; ++k
)
692 if (!isl_int_is_zero(bmap
->eq
[k
][1+last_var
]))
700 swap_equality(bmap
, k
, done
);
701 if (swap
&& swap(k
, done
, user
) < 0)
702 return isl_basic_map_free(bmap
);
704 if (isl_int_is_neg(bmap
->eq
[done
][1+last_var
]))
705 isl_seq_neg(bmap
->eq
[done
], bmap
->eq
[done
], 1+total
);
707 bmap
= eliminate_var_using_equality(bmap
, last_var
,
708 bmap
->eq
[done
], 1, 1, progress
);
710 if (last_var
>= total_var
)
711 bmap
= set_div_from_eq(bmap
, last_var
- total_var
,
716 if (done
== bmap
->n_eq
)
718 for (k
= done
; k
< bmap
->n_eq
; ++k
) {
719 if (isl_int_is_zero(bmap
->eq
[k
][0]))
721 if (drop
&& drop(bmap
->n_eq
, user
) < 0)
722 return isl_basic_map_free(bmap
);
723 return isl_basic_map_set_to_empty(bmap
);
725 n_drop
= bmap
->n_eq
- done
;
726 bmap
= isl_basic_map_free_equality(bmap
, n_drop
);
727 if (drop
&& drop(n_drop
, user
) < 0)
728 return isl_basic_map_free(bmap
);
732 __isl_give isl_basic_map
*isl_basic_map_gauss(__isl_take isl_basic_map
*bmap
,
735 return isl_basic_map_gauss5(bmap
, progress
, NULL
, NULL
, NULL
);
738 __isl_give isl_basic_set
*isl_basic_set_gauss(
739 __isl_take isl_basic_set
*bset
, int *progress
)
741 return bset_from_bmap(isl_basic_map_gauss(bset_to_bmap(bset
),
746 static unsigned int round_up(unsigned int v
)
757 /* Hash table of inequalities in a basic map.
758 * "index" is an array of addresses of inequalities in the basic map, some
759 * of which are NULL. The inequalities are hashed on the coefficients
760 * except the constant term.
761 * "size" is the number of elements in the array and is always a power of two
762 * "bits" is the number of bits need to represent an index into the array.
763 * "total" is the total dimension of the basic map.
765 struct isl_constraint_index
{
772 /* Fill in the "ci" data structure for holding the inequalities of "bmap".
774 static isl_stat
create_constraint_index(struct isl_constraint_index
*ci
,
775 __isl_keep isl_basic_map
*bmap
)
781 return isl_stat_error
;
782 ci
->total
= isl_basic_map_dim(bmap
, isl_dim_all
);
784 return isl_stat_error
;
785 if (bmap
->n_ineq
== 0)
787 ci
->size
= round_up(4 * (bmap
->n_ineq
+ 1) / 3 - 1);
788 ci
->bits
= ffs(ci
->size
) - 1;
789 ctx
= isl_basic_map_get_ctx(bmap
);
790 ci
->index
= isl_calloc_array(ctx
, isl_int
**, ci
->size
);
792 return isl_stat_error
;
797 /* Free the memory allocated by create_constraint_index.
799 static void constraint_index_free(struct isl_constraint_index
*ci
)
804 /* Return the position in ci->index that contains the address of
805 * an inequality that is equal to *ineq up to the constant term,
806 * provided this address is not identical to "ineq".
807 * If there is no such inequality, then return the position where
808 * such an inequality should be inserted.
810 static int hash_index_ineq(struct isl_constraint_index
*ci
, isl_int
**ineq
)
813 uint32_t hash
= isl_seq_get_hash_bits((*ineq
) + 1, ci
->total
, ci
->bits
);
814 for (h
= hash
; ci
->index
[h
]; h
= (h
+1) % ci
->size
)
815 if (ineq
!= ci
->index
[h
] &&
816 isl_seq_eq((*ineq
) + 1, ci
->index
[h
][0]+1, ci
->total
))
821 /* Return the position in ci->index that contains the address of
822 * an inequality that is equal to the k'th inequality of "bmap"
823 * up to the constant term, provided it does not point to the very
825 * If there is no such inequality, then return the position where
826 * such an inequality should be inserted.
828 static int hash_index(struct isl_constraint_index
*ci
,
829 __isl_keep isl_basic_map
*bmap
, int k
)
831 return hash_index_ineq(ci
, &bmap
->ineq
[k
]);
834 static int set_hash_index(struct isl_constraint_index
*ci
,
835 __isl_keep isl_basic_set
*bset
, int k
)
837 return hash_index(ci
, bset
, k
);
840 /* Fill in the "ci" data structure with the inequalities of "bset".
842 static isl_stat
setup_constraint_index(struct isl_constraint_index
*ci
,
843 __isl_keep isl_basic_set
*bset
)
847 if (create_constraint_index(ci
, bset
) < 0)
848 return isl_stat_error
;
850 for (k
= 0; k
< bset
->n_ineq
; ++k
) {
851 h
= set_hash_index(ci
, bset
, k
);
852 ci
->index
[h
] = &bset
->ineq
[k
];
858 /* Is the inequality ineq (obviously) redundant with respect
859 * to the constraints in "ci"?
861 * Look for an inequality in "ci" with the same coefficients and then
862 * check if the contant term of "ineq" is greater than or equal
863 * to the constant term of that inequality. If so, "ineq" is clearly
866 * Note that hash_index_ineq ignores a stored constraint if it has
867 * the same address as the passed inequality. It is ok to pass
868 * the address of a local variable here since it will never be
869 * the same as the address of a constraint in "ci".
871 static isl_bool
constraint_index_is_redundant(struct isl_constraint_index
*ci
,
876 h
= hash_index_ineq(ci
, &ineq
);
878 return isl_bool_false
;
879 return isl_int_ge(ineq
[0], (*ci
->index
[h
])[0]);
882 /* If we can eliminate more than one div, then we need to make
883 * sure we do it from last div to first div, in order not to
884 * change the position of the other divs that still need to
887 static __isl_give isl_basic_map
*remove_duplicate_divs(
888 __isl_take isl_basic_map
*bmap
, int *progress
)
900 bmap
= isl_basic_map_order_divs(bmap
);
901 if (!bmap
|| bmap
->n_div
<= 1)
904 v_div
= isl_basic_map_var_offset(bmap
, isl_dim_div
);
906 return isl_basic_map_free(bmap
);
907 total
= v_div
+ bmap
->n_div
;
910 for (k
= bmap
->n_div
- 1; k
>= 0; --k
)
911 if (!isl_int_is_zero(bmap
->div
[k
][0]))
916 size
= round_up(4 * bmap
->n_div
/ 3 - 1);
919 elim_for
= isl_calloc_array(ctx
, int, bmap
->n_div
);
920 bits
= ffs(size
) - 1;
921 index
= isl_calloc_array(ctx
, int, size
);
922 if (!elim_for
|| !index
)
924 eq
= isl_blk_alloc(ctx
, 1+total
);
925 if (isl_blk_is_error(eq
))
928 isl_seq_clr(eq
.data
, 1+total
);
929 index
[isl_seq_get_hash_bits(bmap
->div
[k
], 2+total
, bits
)] = k
+ 1;
930 for (--k
; k
>= 0; --k
) {
933 if (isl_int_is_zero(bmap
->div
[k
][0]))
936 hash
= isl_seq_get_hash_bits(bmap
->div
[k
], 2+total
, bits
);
937 for (h
= hash
; index
[h
]; h
= (h
+1) % size
)
938 if (isl_seq_eq(bmap
->div
[k
],
939 bmap
->div
[index
[h
]-1], 2+total
))
948 for (l
= bmap
->n_div
- 1; l
>= 0; --l
) {
952 isl_int_set_si(eq
.data
[1 + v_div
+ k
], -1);
953 isl_int_set_si(eq
.data
[1 + v_div
+ l
], 1);
954 bmap
= eliminate_div(bmap
, eq
.data
, l
, 1, 0);
957 isl_int_set_si(eq
.data
[1 + v_div
+ k
], 0);
958 isl_int_set_si(eq
.data
[1 + v_div
+ l
], 0);
961 isl_blk_free(ctx
, eq
);
968 static int n_pure_div_eq(__isl_keep isl_basic_map
*bmap
)
973 v_div
= isl_basic_map_var_offset(bmap
, isl_dim_div
);
976 for (i
= 0, j
= bmap
->n_div
-1; i
< bmap
->n_eq
; ++i
) {
977 while (j
>= 0 && isl_int_is_zero(bmap
->eq
[i
][1 + v_div
+ j
]))
981 if (isl_seq_first_non_zero(bmap
->eq
[i
] + 1 + v_div
, j
) != -1)
987 /* Normalize divs that appear in equalities.
989 * In particular, we assume that bmap contains some equalities
994 * and we want to replace the set of e_i by a minimal set and
995 * such that the new e_i have a canonical representation in terms
997 * If any of the equalities involves more than one divs, then
998 * we currently simply bail out.
1000 * Let us first additionally assume that all equalities involve
1001 * a div. The equalities then express modulo constraints on the
1002 * remaining variables and we can use "parameter compression"
1003 * to find a minimal set of constraints. The result is a transformation
1005 * x = T(x') = x_0 + G x'
1007 * with G a lower-triangular matrix with all elements below the diagonal
1008 * non-negative and smaller than the diagonal element on the same row.
1009 * We first normalize x_0 by making the same property hold in the affine
1011 * The rows i of G with a 1 on the diagonal do not impose any modulo
1012 * constraint and simply express x_i = x'_i.
1013 * For each of the remaining rows i, we introduce a div and a corresponding
1014 * equality. In particular
1016 * g_ii e_j = x_i - g_i(x')
1018 * where each x'_k is replaced either by x_k (if g_kk = 1) or the
1019 * corresponding div (if g_kk != 1).
1021 * If there are any equalities not involving any div, then we
1022 * first apply a variable compression on the variables x:
1024 * x = C x'' x'' = C_2 x
1026 * and perform the above parameter compression on A C instead of on A.
1027 * The resulting compression is then of the form
1029 * x'' = T(x') = x_0 + G x'
1031 * and in constructing the new divs and the corresponding equalities,
1032 * we have to replace each x'', i.e., the x'_k with (g_kk = 1),
1033 * by the corresponding row from C_2.
1035 static __isl_give isl_basic_map
*normalize_divs(__isl_take isl_basic_map
*bmap
,
1043 struct isl_mat
*T
= NULL
;
1044 struct isl_mat
*C
= NULL
;
1045 struct isl_mat
*C2
= NULL
;
1048 int dropped
, needed
;
1053 if (bmap
->n_div
== 0)
1056 if (bmap
->n_eq
== 0)
1059 if (ISL_F_ISSET(bmap
, ISL_BASIC_MAP_NORMALIZED_DIVS
))
1062 v_div
= isl_basic_map_var_offset(bmap
, isl_dim_div
);
1063 div_eq
= n_pure_div_eq(bmap
);
1064 if (v_div
< 0 || div_eq
< 0)
1065 return isl_basic_map_free(bmap
);
1069 if (div_eq
< bmap
->n_eq
) {
1070 B
= isl_mat_sub_alloc6(bmap
->ctx
, bmap
->eq
, div_eq
,
1071 bmap
->n_eq
- div_eq
, 0, 1 + v_div
);
1072 C
= isl_mat_variable_compression(B
, &C2
);
1075 if (C
->n_col
== 0) {
1076 bmap
= isl_basic_map_set_to_empty(bmap
);
1083 d
= isl_vec_alloc(bmap
->ctx
, div_eq
);
1086 for (i
= 0, j
= bmap
->n_div
-1; i
< div_eq
; ++i
) {
1087 while (j
>= 0 && isl_int_is_zero(bmap
->eq
[i
][1 + v_div
+ j
]))
1089 isl_int_set(d
->block
.data
[i
], bmap
->eq
[i
][1 + v_div
+ j
]);
1091 B
= isl_mat_sub_alloc6(bmap
->ctx
, bmap
->eq
, 0, div_eq
, 0, 1 + v_div
);
1094 B
= isl_mat_product(B
, C
);
1098 T
= isl_mat_parameter_compression(B
, d
);
1101 if (T
->n_col
== 0) {
1102 bmap
= isl_basic_map_set_to_empty(bmap
);
1108 for (i
= 0; i
< T
->n_row
- 1; ++i
) {
1109 isl_int_fdiv_q(v
, T
->row
[1 + i
][0], T
->row
[1 + i
][1 + i
]);
1110 if (isl_int_is_zero(v
))
1112 isl_mat_col_submul(T
, 0, v
, 1 + i
);
1115 pos
= isl_alloc_array(bmap
->ctx
, int, T
->n_row
);
1118 /* We have to be careful because dropping equalities may reorder them */
1120 for (j
= bmap
->n_div
- 1; j
>= 0; --j
) {
1121 for (i
= 0; i
< bmap
->n_eq
; ++i
)
1122 if (!isl_int_is_zero(bmap
->eq
[i
][1 + v_div
+ j
]))
1124 if (i
< bmap
->n_eq
) {
1125 bmap
= isl_basic_map_drop_div(bmap
, j
);
1126 if (isl_basic_map_drop_equality(bmap
, i
) < 0)
1133 for (i
= 1; i
< T
->n_row
; ++i
) {
1134 if (isl_int_is_one(T
->row
[i
][i
]))
1139 if (needed
> dropped
) {
1140 bmap
= isl_basic_map_extend(bmap
, needed
, needed
, 0);
1144 for (i
= 1; i
< T
->n_row
; ++i
) {
1145 if (isl_int_is_one(T
->row
[i
][i
]))
1147 k
= isl_basic_map_alloc_div(bmap
);
1148 pos
[i
] = 1 + v_div
+ k
;
1149 isl_seq_clr(bmap
->div
[k
] + 1, 1 + v_div
+ bmap
->n_div
);
1150 isl_int_set(bmap
->div
[k
][0], T
->row
[i
][i
]);
1152 isl_seq_cpy(bmap
->div
[k
] + 1, C2
->row
[i
], 1 + v_div
);
1154 isl_int_set_si(bmap
->div
[k
][1 + i
], 1);
1155 for (j
= 0; j
< i
; ++j
) {
1156 if (isl_int_is_zero(T
->row
[i
][j
]))
1158 if (pos
[j
] < T
->n_row
&& C2
)
1159 isl_seq_submul(bmap
->div
[k
] + 1, T
->row
[i
][j
],
1160 C2
->row
[pos
[j
]], 1 + v_div
);
1162 isl_int_neg(bmap
->div
[k
][1 + pos
[j
]],
1165 j
= isl_basic_map_alloc_equality(bmap
);
1166 isl_seq_neg(bmap
->eq
[j
], bmap
->div
[k
]+1, 1+v_div
+bmap
->n_div
);
1167 isl_int_set(bmap
->eq
[j
][pos
[i
]], bmap
->div
[k
][0]);
1176 ISL_F_SET(bmap
, ISL_BASIC_MAP_NORMALIZED_DIVS
);
1184 isl_basic_map_free(bmap
);
1188 static __isl_give isl_basic_map
*set_div_from_lower_bound(
1189 __isl_take isl_basic_map
*bmap
, int div
, int ineq
)
1191 unsigned total
= isl_basic_map_offset(bmap
, isl_dim_div
);
1193 isl_seq_neg(bmap
->div
[div
] + 1, bmap
->ineq
[ineq
], total
+ bmap
->n_div
);
1194 isl_int_set(bmap
->div
[div
][0], bmap
->ineq
[ineq
][total
+ div
]);
1195 isl_int_add(bmap
->div
[div
][1], bmap
->div
[div
][1], bmap
->div
[div
][0]);
1196 isl_int_sub_ui(bmap
->div
[div
][1], bmap
->div
[div
][1], 1);
1197 isl_int_set_si(bmap
->div
[div
][1 + total
+ div
], 0);
1202 /* Check whether it is ok to define a div based on an inequality.
1203 * To avoid the introduction of circular definitions of divs, we
1204 * do not allow such a definition 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_bool
ok_to_set_div_from_bound(__isl_keep isl_basic_map
*bmap
,
1212 unsigned total
= isl_basic_map_offset(bmap
, isl_dim_div
);
1214 /* Not defined in terms of unknown divs */
1215 for (j
= 0; j
< bmap
->n_div
; ++j
) {
1218 if (isl_int_is_zero(bmap
->ineq
[ineq
][total
+ j
]))
1220 if (isl_int_is_zero(bmap
->div
[j
][0]))
1221 return isl_bool_false
;
1224 /* No other div defined in terms of this one => avoid loops */
1225 for (j
= 0; j
< bmap
->n_div
; ++j
) {
1228 if (isl_int_is_zero(bmap
->div
[j
][0]))
1230 if (!isl_int_is_zero(bmap
->div
[j
][1 + total
+ div
]))
1231 return isl_bool_false
;
1234 return isl_bool_true
;
1237 /* Would an expression for div "div" based on inequality "ineq" of "bmap"
1238 * be a better expression than the current one?
1240 * If we do not have any expression yet, then any expression would be better.
1241 * Otherwise we check if the last variable involved in the inequality
1242 * (disregarding the div that it would define) is in an earlier position
1243 * than the last variable involved in the current div expression.
1245 static isl_bool
better_div_constraint(__isl_keep isl_basic_map
*bmap
,
1248 unsigned total
= isl_basic_map_offset(bmap
, isl_dim_div
);
1252 if (isl_int_is_zero(bmap
->div
[div
][0]))
1253 return isl_bool_true
;
1255 if (isl_seq_last_non_zero(bmap
->ineq
[ineq
] + total
+ div
+ 1,
1256 bmap
->n_div
- (div
+ 1)) >= 0)
1257 return isl_bool_false
;
1259 last_ineq
= isl_seq_last_non_zero(bmap
->ineq
[ineq
], total
+ div
);
1260 last_div
= isl_seq_last_non_zero(bmap
->div
[div
] + 1,
1261 total
+ bmap
->n_div
);
1263 return last_ineq
< last_div
;
1266 /* Given two constraints "k" and "l" that are opposite to each other,
1267 * except for the constant term, check if we can use them
1268 * to obtain an expression for one of the hitherto unknown divs or
1269 * a "better" expression for a div for which we already have an expression.
1270 * "sum" is the sum of the constant terms of the constraints.
1271 * If this sum is strictly smaller than the coefficient of one
1272 * of the divs, then this pair can be used to define the div.
1273 * To avoid the introduction of circular definitions of divs, we
1274 * do not use the pair if the resulting expression would refer to
1275 * any other undefined divs or if any known div is defined in
1276 * terms of the unknown div.
1278 static __isl_give isl_basic_map
*check_for_div_constraints(
1279 __isl_take isl_basic_map
*bmap
, int k
, int l
, isl_int sum
,
1283 unsigned total
= isl_basic_map_offset(bmap
, isl_dim_div
);
1285 for (i
= 0; i
< bmap
->n_div
; ++i
) {
1288 if (isl_int_is_zero(bmap
->ineq
[k
][total
+ i
]))
1290 if (isl_int_abs_ge(sum
, bmap
->ineq
[k
][total
+ i
]))
1292 set_div
= better_div_constraint(bmap
, i
, k
);
1293 if (set_div
>= 0 && set_div
)
1294 set_div
= ok_to_set_div_from_bound(bmap
, i
, k
);
1296 return isl_basic_map_free(bmap
);
1299 if (isl_int_is_pos(bmap
->ineq
[k
][total
+ i
]))
1300 bmap
= set_div_from_lower_bound(bmap
, i
, k
);
1302 bmap
= set_div_from_lower_bound(bmap
, i
, l
);
1310 __isl_give isl_basic_map
*isl_basic_map_remove_duplicate_constraints(
1311 __isl_take isl_basic_map
*bmap
, int *progress
, int detect_divs
)
1313 struct isl_constraint_index ci
;
1315 isl_size total
= isl_basic_map_dim(bmap
, isl_dim_all
);
1318 if (total
< 0 || bmap
->n_ineq
<= 1)
1321 if (create_constraint_index(&ci
, bmap
) < 0)
1324 h
= isl_seq_get_hash_bits(bmap
->ineq
[0] + 1, total
, ci
.bits
);
1325 ci
.index
[h
] = &bmap
->ineq
[0];
1326 for (k
= 1; k
< bmap
->n_ineq
; ++k
) {
1327 h
= hash_index(&ci
, bmap
, k
);
1329 ci
.index
[h
] = &bmap
->ineq
[k
];
1334 l
= ci
.index
[h
] - &bmap
->ineq
[0];
1335 if (isl_int_lt(bmap
->ineq
[k
][0], bmap
->ineq
[l
][0]))
1336 swap_inequality(bmap
, k
, l
);
1337 isl_basic_map_drop_inequality(bmap
, k
);
1341 for (k
= 0; bmap
&& k
< bmap
->n_ineq
-1; ++k
) {
1342 isl_seq_neg(bmap
->ineq
[k
]+1, bmap
->ineq
[k
]+1, total
);
1343 h
= hash_index(&ci
, bmap
, k
);
1344 isl_seq_neg(bmap
->ineq
[k
]+1, bmap
->ineq
[k
]+1, total
);
1347 l
= ci
.index
[h
] - &bmap
->ineq
[0];
1348 isl_int_add(sum
, bmap
->ineq
[k
][0], bmap
->ineq
[l
][0]);
1349 if (isl_int_is_pos(sum
)) {
1351 bmap
= check_for_div_constraints(bmap
, k
, l
,
1355 if (isl_int_is_zero(sum
)) {
1356 /* We need to break out of the loop after these
1357 * changes since the contents of the hash
1358 * will no longer be valid.
1359 * Plus, we probably we want to regauss first.
1363 isl_basic_map_drop_inequality(bmap
, l
);
1364 isl_basic_map_inequality_to_equality(bmap
, k
);
1366 bmap
= isl_basic_map_set_to_empty(bmap
);
1371 constraint_index_free(&ci
);
1375 /* Detect all pairs of inequalities that form an equality.
1377 * isl_basic_map_remove_duplicate_constraints detects at most one such pair.
1378 * Call it repeatedly while it is making progress.
1380 __isl_give isl_basic_map
*isl_basic_map_detect_inequality_pairs(
1381 __isl_take isl_basic_map
*bmap
, int *progress
)
1387 bmap
= isl_basic_map_remove_duplicate_constraints(bmap
,
1389 if (progress
&& duplicate
)
1391 } while (duplicate
);
1396 /* Given a known integer division "div" that is not integral
1397 * (with denominator 1), eliminate it from the constraints in "bmap"
1398 * where it appears with a (positive or negative) unit coefficient.
1399 * If "progress" is not NULL, then it gets set if the elimination
1400 * results in any changes.
1404 * floor(e/m) + f >= 0
1412 * -floor(e/m) + f >= 0
1416 * -e + m f + m - 1 >= 0
1418 * The first conversion is valid because floor(e/m) >= -f is equivalent
1419 * to e/m >= -f because -f is an integral expression.
1420 * The second conversion follows from the fact that
1422 * -floor(e/m) = ceil(-e/m) = floor((-e + m - 1)/m)
1425 * Note that one of the div constraints may have been eliminated
1426 * due to being redundant with respect to the constraint that is
1427 * being modified by this function. The modified constraint may
1428 * no longer imply this div constraint, so we add it back to make
1429 * sure we do not lose any information.
1431 static __isl_give isl_basic_map
*eliminate_unit_div(
1432 __isl_take isl_basic_map
*bmap
, int div
, int *progress
)
1435 isl_size v_div
, dim
;
1438 v_div
= isl_basic_map_var_offset(bmap
, isl_dim_div
);
1439 dim
= isl_basic_map_dim(bmap
, isl_dim_all
);
1440 if (v_div
< 0 || dim
< 0)
1441 return isl_basic_map_free(bmap
);
1443 ctx
= isl_basic_map_get_ctx(bmap
);
1445 for (j
= 0; j
< bmap
->n_ineq
; ++j
) {
1448 if (!isl_int_is_one(bmap
->ineq
[j
][1 + v_div
+ div
]) &&
1449 !isl_int_is_negone(bmap
->ineq
[j
][1 + v_div
+ div
]))
1455 s
= isl_int_sgn(bmap
->ineq
[j
][1 + v_div
+ div
]);
1456 isl_int_set_si(bmap
->ineq
[j
][1 + v_div
+ div
], 0);
1458 isl_seq_combine(bmap
->ineq
[j
],
1459 ctx
->negone
, bmap
->div
[div
] + 1,
1460 bmap
->div
[div
][0], bmap
->ineq
[j
], 1 + dim
);
1462 isl_seq_combine(bmap
->ineq
[j
],
1463 ctx
->one
, bmap
->div
[div
] + 1,
1464 bmap
->div
[div
][0], bmap
->ineq
[j
], 1 + dim
);
1466 isl_int_add(bmap
->ineq
[j
][0],
1467 bmap
->ineq
[j
][0], bmap
->div
[div
][0]);
1468 isl_int_sub_ui(bmap
->ineq
[j
][0],
1469 bmap
->ineq
[j
][0], 1);
1472 bmap
= isl_basic_map_extend_constraints(bmap
, 0, 1);
1473 bmap
= isl_basic_map_add_div_constraint(bmap
, div
, s
);
1481 /* Eliminate selected known divs from constraints where they appear with
1482 * a (positive or negative) unit coefficient.
1483 * In particular, only handle those for which "select" returns isl_bool_true.
1484 * If "progress" is not NULL, then it gets set if the elimination
1485 * results in any changes.
1487 * We skip integral divs, i.e., those with denominator 1, as we would
1488 * risk eliminating the div from the div constraints. We do not need
1489 * to handle those divs here anyway since the div constraints will turn
1490 * out to form an equality and this equality can then be used to eliminate
1491 * the div from all constraints.
1493 static __isl_give isl_basic_map
*eliminate_selected_unit_divs(
1494 __isl_take isl_basic_map
*bmap
,
1495 isl_bool (*select
)(__isl_keep isl_basic_map
*bmap
, int div
),
1501 n_div
= isl_basic_map_dim(bmap
, isl_dim_div
);
1503 return isl_basic_map_free(bmap
);
1505 for (i
= 0; i
< n_div
; ++i
) {
1509 skip
= isl_basic_map_div_is_marked_unknown(bmap
, i
);
1511 return isl_basic_map_free(bmap
);
1514 if (isl_int_is_one(bmap
->div
[i
][0]))
1516 selected
= select(bmap
, i
);
1518 return isl_basic_map_free(bmap
);
1521 bmap
= eliminate_unit_div(bmap
, i
, progress
);
1529 /* eliminate_selected_unit_divs callback that selects every
1532 static isl_bool
is_any_div(__isl_keep isl_basic_map
*bmap
, int div
)
1534 return isl_bool_true
;
1537 /* Eliminate known divs from constraints where they appear with
1538 * a (positive or negative) unit coefficient.
1539 * If "progress" is not NULL, then it gets set if the elimination
1540 * results in any changes.
1542 static __isl_give isl_basic_map
*eliminate_unit_divs(
1543 __isl_take isl_basic_map
*bmap
, int *progress
)
1545 return eliminate_selected_unit_divs(bmap
, &is_any_div
, progress
);
1548 /* eliminate_selected_unit_divs callback that selects
1549 * integer divisions that only appear with
1550 * a (positive or negative) unit coefficient
1551 * (outside their div constraints).
1553 static isl_bool
is_pure_unit_div(__isl_keep isl_basic_map
*bmap
, int div
)
1556 isl_size v_div
, n_ineq
;
1558 v_div
= isl_basic_map_var_offset(bmap
, isl_dim_div
);
1559 n_ineq
= isl_basic_map_n_inequality(bmap
);
1560 if (v_div
< 0 || n_ineq
< 0)
1561 return isl_bool_error
;
1563 for (i
= 0; i
< n_ineq
; ++i
) {
1566 if (isl_int_is_zero(bmap
->ineq
[i
][1 + v_div
+ div
]))
1568 skip
= isl_basic_map_is_div_constraint(bmap
,
1569 bmap
->ineq
[i
], div
);
1571 return isl_bool_error
;
1574 if (!isl_int_is_one(bmap
->ineq
[i
][1 + v_div
+ div
]) &&
1575 !isl_int_is_negone(bmap
->ineq
[i
][1 + v_div
+ div
]))
1576 return isl_bool_false
;
1579 return isl_bool_true
;
1582 /* Eliminate known divs from constraints where they appear with
1583 * a (positive or negative) unit coefficient,
1584 * but only if they do not appear in any other constraints
1585 * (other than the div constraints).
1587 __isl_give isl_basic_map
*isl_basic_map_eliminate_pure_unit_divs(
1588 __isl_take isl_basic_map
*bmap
)
1590 return eliminate_selected_unit_divs(bmap
, &is_pure_unit_div
, NULL
);
1593 __isl_give isl_basic_map
*isl_basic_map_simplify(__isl_take isl_basic_map
*bmap
)
1602 empty
= isl_basic_map_plain_is_empty(bmap
);
1604 return isl_basic_map_free(bmap
);
1607 bmap
= isl_basic_map_normalize_constraints(bmap
);
1608 bmap
= reduce_div_coefficients(bmap
);
1609 bmap
= normalize_div_expressions(bmap
);
1610 bmap
= remove_duplicate_divs(bmap
, &progress
);
1611 bmap
= eliminate_unit_divs(bmap
, &progress
);
1612 bmap
= eliminate_divs_eq(bmap
, &progress
);
1613 bmap
= eliminate_divs_ineq(bmap
, &progress
);
1614 bmap
= isl_basic_map_gauss(bmap
, &progress
);
1615 /* requires equalities in normal form */
1616 bmap
= normalize_divs(bmap
, &progress
);
1617 bmap
= isl_basic_map_remove_duplicate_constraints(bmap
,
1623 __isl_give isl_basic_set
*isl_basic_set_simplify(
1624 __isl_take isl_basic_set
*bset
)
1626 return bset_from_bmap(isl_basic_map_simplify(bset_to_bmap(bset
)));
1630 isl_bool
isl_basic_map_is_div_constraint(__isl_keep isl_basic_map
*bmap
,
1631 isl_int
*constraint
, unsigned div
)
1636 return isl_bool_error
;
1638 pos
= isl_basic_map_offset(bmap
, isl_dim_div
) + div
;
1640 if (isl_int_eq(constraint
[pos
], bmap
->div
[div
][0])) {
1642 isl_int_sub(bmap
->div
[div
][1],
1643 bmap
->div
[div
][1], bmap
->div
[div
][0]);
1644 isl_int_add_ui(bmap
->div
[div
][1], bmap
->div
[div
][1], 1);
1645 neg
= isl_seq_is_neg(constraint
, bmap
->div
[div
]+1, pos
);
1646 isl_int_sub_ui(bmap
->div
[div
][1], bmap
->div
[div
][1], 1);
1647 isl_int_add(bmap
->div
[div
][1],
1648 bmap
->div
[div
][1], bmap
->div
[div
][0]);
1650 return isl_bool_false
;
1651 if (isl_seq_first_non_zero(constraint
+pos
+1,
1652 bmap
->n_div
-div
-1) != -1)
1653 return isl_bool_false
;
1654 } else if (isl_int_abs_eq(constraint
[pos
], bmap
->div
[div
][0])) {
1655 if (!isl_seq_eq(constraint
, bmap
->div
[div
]+1, pos
))
1656 return isl_bool_false
;
1657 if (isl_seq_first_non_zero(constraint
+pos
+1,
1658 bmap
->n_div
-div
-1) != -1)
1659 return isl_bool_false
;
1661 return isl_bool_false
;
1663 return isl_bool_true
;
1666 /* If the only constraints a div d=floor(f/m)
1667 * appears in are its two defining constraints
1670 * -(f - (m - 1)) + m d >= 0
1672 * then it can safely be removed.
1674 static isl_bool
div_is_redundant(__isl_keep isl_basic_map
*bmap
, int div
)
1677 isl_size v_div
= isl_basic_map_var_offset(bmap
, isl_dim_div
);
1678 unsigned pos
= 1 + v_div
+ div
;
1681 return isl_bool_error
;
1683 for (i
= 0; i
< bmap
->n_eq
; ++i
)
1684 if (!isl_int_is_zero(bmap
->eq
[i
][pos
]))
1685 return isl_bool_false
;
1687 for (i
= 0; i
< bmap
->n_ineq
; ++i
) {
1690 if (isl_int_is_zero(bmap
->ineq
[i
][pos
]))
1692 red
= isl_basic_map_is_div_constraint(bmap
, bmap
->ineq
[i
], div
);
1693 if (red
< 0 || !red
)
1697 for (i
= 0; i
< bmap
->n_div
; ++i
) {
1698 if (isl_int_is_zero(bmap
->div
[i
][0]))
1700 if (!isl_int_is_zero(bmap
->div
[i
][1+pos
]))
1701 return isl_bool_false
;
1704 return isl_bool_true
;
1708 * Remove divs that don't occur in any of the constraints or other divs.
1709 * These can arise when dropping constraints from a basic map or
1710 * when the divs of a basic map have been temporarily aligned
1711 * with the divs of another basic map.
1713 static __isl_give isl_basic_map
*remove_redundant_divs(
1714 __isl_take isl_basic_map
*bmap
)
1719 v_div
= isl_basic_map_var_offset(bmap
, isl_dim_div
);
1721 return isl_basic_map_free(bmap
);
1723 for (i
= bmap
->n_div
-1; i
>= 0; --i
) {
1726 redundant
= div_is_redundant(bmap
, i
);
1728 return isl_basic_map_free(bmap
);
1731 bmap
= isl_basic_map_drop_constraints_involving(bmap
,
1733 bmap
= isl_basic_map_drop_div(bmap
, i
);
1738 /* Mark "bmap" as final, without checking for obviously redundant
1739 * integer divisions. This function should be used when "bmap"
1740 * is known not to involve any such integer divisions.
1742 __isl_give isl_basic_map
*isl_basic_map_mark_final(
1743 __isl_take isl_basic_map
*bmap
)
1747 ISL_F_SET(bmap
, ISL_BASIC_SET_FINAL
);
1751 /* Mark "bmap" as final, after removing obviously redundant integer divisions.
1753 __isl_give isl_basic_map
*isl_basic_map_finalize(__isl_take isl_basic_map
*bmap
)
1755 bmap
= remove_redundant_divs(bmap
);
1756 bmap
= isl_basic_map_mark_final(bmap
);
1760 __isl_give isl_basic_set
*isl_basic_set_finalize(
1761 __isl_take isl_basic_set
*bset
)
1763 return bset_from_bmap(isl_basic_map_finalize(bset_to_bmap(bset
)));
1766 /* Remove definition of any div that is defined in terms of the given variable.
1767 * The div itself is not removed. Functions such as
1768 * eliminate_divs_ineq depend on the other divs remaining in place.
1770 static __isl_give isl_basic_map
*remove_dependent_vars(
1771 __isl_take isl_basic_map
*bmap
, int pos
)
1778 for (i
= 0; i
< bmap
->n_div
; ++i
) {
1779 if (isl_int_is_zero(bmap
->div
[i
][0]))
1781 if (isl_int_is_zero(bmap
->div
[i
][1+1+pos
]))
1783 bmap
= isl_basic_map_mark_div_unknown(bmap
, i
);
1790 /* Eliminate the specified variables from the constraints using
1791 * Fourier-Motzkin. The variables themselves are not removed.
1793 __isl_give isl_basic_map
*isl_basic_map_eliminate_vars(
1794 __isl_take isl_basic_map
*bmap
, unsigned pos
, unsigned n
)
1803 total
= isl_basic_map_dim(bmap
, isl_dim_all
);
1805 return isl_basic_map_free(bmap
);
1807 bmap
= isl_basic_map_cow(bmap
);
1808 for (d
= pos
+ n
- 1; d
>= 0 && d
>= pos
; --d
)
1809 bmap
= remove_dependent_vars(bmap
, d
);
1813 for (d
= pos
+ n
- 1;
1814 d
>= 0 && d
>= total
- bmap
->n_div
&& d
>= pos
; --d
)
1815 isl_seq_clr(bmap
->div
[d
-(total
-bmap
->n_div
)], 2+total
);
1816 for (d
= pos
+ n
- 1; d
>= 0 && d
>= pos
; --d
) {
1817 int n_lower
, n_upper
;
1820 for (i
= 0; i
< bmap
->n_eq
; ++i
) {
1821 if (isl_int_is_zero(bmap
->eq
[i
][1+d
]))
1823 bmap
= eliminate_var_using_equality(bmap
, d
,
1824 bmap
->eq
[i
], 0, 1, NULL
);
1825 if (isl_basic_map_drop_equality(bmap
, i
) < 0)
1826 return isl_basic_map_free(bmap
);
1834 for (i
= 0; i
< bmap
->n_ineq
; ++i
) {
1835 if (isl_int_is_pos(bmap
->ineq
[i
][1+d
]))
1837 else if (isl_int_is_neg(bmap
->ineq
[i
][1+d
]))
1840 bmap
= isl_basic_map_extend_constraints(bmap
,
1841 0, n_lower
* n_upper
);
1844 for (i
= bmap
->n_ineq
- 1; i
>= 0; --i
) {
1846 if (isl_int_is_zero(bmap
->ineq
[i
][1+d
]))
1849 for (j
= 0; j
< i
; ++j
) {
1850 if (isl_int_is_zero(bmap
->ineq
[j
][1+d
]))
1853 if (isl_int_sgn(bmap
->ineq
[i
][1+d
]) ==
1854 isl_int_sgn(bmap
->ineq
[j
][1+d
]))
1856 k
= isl_basic_map_alloc_inequality(bmap
);
1859 isl_seq_cpy(bmap
->ineq
[k
], bmap
->ineq
[i
],
1861 isl_seq_elim(bmap
->ineq
[k
], bmap
->ineq
[j
],
1862 1+d
, 1+total
, NULL
);
1864 isl_basic_map_drop_inequality(bmap
, i
);
1867 if (n_lower
> 0 && n_upper
> 0) {
1868 bmap
= isl_basic_map_normalize_constraints(bmap
);
1869 bmap
= isl_basic_map_remove_duplicate_constraints(bmap
,
1871 bmap
= isl_basic_map_gauss(bmap
, NULL
);
1872 bmap
= isl_basic_map_remove_redundancies(bmap
);
1876 if (ISL_F_ISSET(bmap
, ISL_BASIC_MAP_EMPTY
))
1881 bmap
= isl_basic_map_gauss(bmap
, NULL
);
1884 isl_basic_map_free(bmap
);
1888 __isl_give isl_basic_set
*isl_basic_set_eliminate_vars(
1889 __isl_take isl_basic_set
*bset
, unsigned pos
, unsigned n
)
1891 return bset_from_bmap(isl_basic_map_eliminate_vars(bset_to_bmap(bset
),
1895 /* Eliminate the specified n dimensions starting at first from the
1896 * constraints, without removing the dimensions from the space.
1897 * If the set is rational, the dimensions are eliminated using Fourier-Motzkin.
1898 * Otherwise, they are projected out and the original space is restored.
1900 __isl_give isl_basic_map
*isl_basic_map_eliminate(
1901 __isl_take isl_basic_map
*bmap
,
1902 enum isl_dim_type type
, unsigned first
, unsigned n
)
1911 if (isl_basic_map_check_range(bmap
, type
, first
, n
) < 0)
1912 return isl_basic_map_free(bmap
);
1914 if (ISL_F_ISSET(bmap
, ISL_BASIC_MAP_RATIONAL
)) {
1915 first
+= isl_basic_map_offset(bmap
, type
) - 1;
1916 bmap
= isl_basic_map_eliminate_vars(bmap
, first
, n
);
1917 return isl_basic_map_finalize(bmap
);
1920 space
= isl_basic_map_get_space(bmap
);
1921 bmap
= isl_basic_map_project_out(bmap
, type
, first
, n
);
1922 bmap
= isl_basic_map_insert_dims(bmap
, type
, first
, n
);
1923 bmap
= isl_basic_map_reset_space(bmap
, space
);
1927 __isl_give isl_basic_set
*isl_basic_set_eliminate(
1928 __isl_take isl_basic_set
*bset
,
1929 enum isl_dim_type type
, unsigned first
, unsigned n
)
1931 return isl_basic_map_eliminate(bset
, type
, first
, n
);
1934 /* Remove all constraints from "bmap" that reference any unknown local
1935 * variables (directly or indirectly).
1937 * Dropping all constraints on a local variable will make it redundant,
1938 * so it will get removed implicitly by
1939 * isl_basic_map_drop_constraints_involving_dims. Some other local
1940 * variables may also end up becoming redundant if they only appear
1941 * in constraints together with the unknown local variable.
1942 * Therefore, start over after calling
1943 * isl_basic_map_drop_constraints_involving_dims.
1945 __isl_give isl_basic_map
*isl_basic_map_drop_constraints_involving_unknown_divs(
1946 __isl_take isl_basic_map
*bmap
)
1952 known
= isl_basic_map_divs_known(bmap
);
1954 return isl_basic_map_free(bmap
);
1958 n_div
= isl_basic_map_dim(bmap
, isl_dim_div
);
1960 return isl_basic_map_free(bmap
);
1961 o_div
= isl_basic_map_offset(bmap
, isl_dim_div
) - 1;
1963 for (i
= 0; i
< n_div
; ++i
) {
1964 known
= isl_basic_map_div_is_known(bmap
, i
);
1966 return isl_basic_map_free(bmap
);
1969 bmap
= remove_dependent_vars(bmap
, o_div
+ i
);
1970 bmap
= isl_basic_map_drop_constraints_involving_dims(bmap
,
1972 n_div
= isl_basic_map_dim(bmap
, isl_dim_div
);
1974 return isl_basic_map_free(bmap
);
1981 /* Remove all constraints from "bset" that reference any unknown local
1982 * variables (directly or indirectly).
1984 __isl_give isl_basic_set
*isl_basic_set_drop_constraints_involving_unknown_divs(
1985 __isl_take isl_basic_set
*bset
)
1987 isl_basic_map
*bmap
;
1989 bmap
= bset_to_bmap(bset
);
1990 bmap
= isl_basic_map_drop_constraints_involving_unknown_divs(bmap
);
1991 return bset_from_bmap(bmap
);
1994 /* Remove all constraints from "map" that reference any unknown local
1995 * variables (directly or indirectly).
1997 * Since constraints may get dropped from the basic maps,
1998 * they may no longer be disjoint from each other.
2000 __isl_give isl_map
*isl_map_drop_constraints_involving_unknown_divs(
2001 __isl_take isl_map
*map
)
2006 known
= isl_map_divs_known(map
);
2008 return isl_map_free(map
);
2012 map
= isl_map_cow(map
);
2016 for (i
= 0; i
< map
->n
; ++i
) {
2018 isl_basic_map_drop_constraints_involving_unknown_divs(
2021 return isl_map_free(map
);
2025 ISL_F_CLR(map
, ISL_MAP_DISJOINT
);
2030 /* Don't assume equalities are in order, because align_divs
2031 * may have changed the order of the divs.
2033 static void compute_elimination_index(__isl_keep isl_basic_map
*bmap
, int *elim
,
2038 for (d
= 0; d
< len
; ++d
)
2040 for (i
= 0; i
< bmap
->n_eq
; ++i
) {
2041 for (d
= len
- 1; d
>= 0; --d
) {
2042 if (isl_int_is_zero(bmap
->eq
[i
][1+d
]))
2050 static void set_compute_elimination_index(__isl_keep isl_basic_set
*bset
,
2051 int *elim
, unsigned len
)
2053 compute_elimination_index(bset_to_bmap(bset
), elim
, len
);
2056 static int reduced_using_equalities(isl_int
*dst
, isl_int
*src
,
2057 __isl_keep isl_basic_map
*bmap
, int *elim
, unsigned total
)
2062 for (d
= total
- 1; d
>= 0; --d
) {
2063 if (isl_int_is_zero(src
[1+d
]))
2068 isl_seq_cpy(dst
, src
, 1 + total
);
2071 isl_seq_elim(dst
, bmap
->eq
[elim
[d
]], 1 + d
, 1 + total
, NULL
);
2076 static int set_reduced_using_equalities(isl_int
*dst
, isl_int
*src
,
2077 __isl_keep isl_basic_set
*bset
, int *elim
, unsigned total
)
2079 return reduced_using_equalities(dst
, src
,
2080 bset_to_bmap(bset
), elim
, total
);
2083 static __isl_give isl_basic_set
*isl_basic_set_reduce_using_equalities(
2084 __isl_take isl_basic_set
*bset
, __isl_take isl_basic_set
*context
)
2090 if (!bset
|| !context
)
2093 if (context
->n_eq
== 0) {
2094 isl_basic_set_free(context
);
2098 bset
= isl_basic_set_cow(bset
);
2099 dim
= isl_basic_set_dim(bset
, isl_dim_set
);
2103 elim
= isl_alloc_array(bset
->ctx
, int, dim
);
2106 set_compute_elimination_index(context
, elim
, dim
);
2107 for (i
= 0; i
< bset
->n_eq
; ++i
)
2108 set_reduced_using_equalities(bset
->eq
[i
], bset
->eq
[i
],
2109 context
, elim
, dim
);
2110 for (i
= 0; i
< bset
->n_ineq
; ++i
)
2111 set_reduced_using_equalities(bset
->ineq
[i
], bset
->ineq
[i
],
2112 context
, elim
, dim
);
2113 isl_basic_set_free(context
);
2115 bset
= isl_basic_set_simplify(bset
);
2116 bset
= isl_basic_set_finalize(bset
);
2119 isl_basic_set_free(bset
);
2120 isl_basic_set_free(context
);
2124 /* For each inequality in "ineq" that is a shifted (more relaxed)
2125 * copy of an inequality in "context", mark the corresponding entry
2127 * If an inequality only has a non-negative constant term, then
2130 static isl_stat
mark_shifted_constraints(__isl_keep isl_mat
*ineq
,
2131 __isl_keep isl_basic_set
*context
, int *row
)
2133 struct isl_constraint_index ci
;
2134 isl_size n_ineq
, cols
;
2138 if (!ineq
|| !context
)
2139 return isl_stat_error
;
2140 if (context
->n_ineq
== 0)
2142 if (setup_constraint_index(&ci
, context
) < 0)
2143 return isl_stat_error
;
2145 n_ineq
= isl_mat_rows(ineq
);
2146 cols
= isl_mat_cols(ineq
);
2147 if (n_ineq
< 0 || cols
< 0)
2148 return isl_stat_error
;
2150 for (k
= 0; k
< n_ineq
; ++k
) {
2154 l
= isl_seq_first_non_zero(ineq
->row
[k
] + 1, total
);
2155 if (l
< 0 && isl_int_is_nonneg(ineq
->row
[k
][0])) {
2159 redundant
= constraint_index_is_redundant(&ci
, ineq
->row
[k
]);
2166 constraint_index_free(&ci
);
2169 constraint_index_free(&ci
);
2170 return isl_stat_error
;
2173 static __isl_give isl_basic_set
*remove_shifted_constraints(
2174 __isl_take isl_basic_set
*bset
, __isl_keep isl_basic_set
*context
)
2176 struct isl_constraint_index ci
;
2179 if (!bset
|| !context
)
2182 if (context
->n_ineq
== 0)
2184 if (setup_constraint_index(&ci
, context
) < 0)
2187 for (k
= 0; k
< bset
->n_ineq
; ++k
) {
2190 redundant
= constraint_index_is_redundant(&ci
, bset
->ineq
[k
]);
2195 bset
= isl_basic_set_cow(bset
);
2198 isl_basic_set_drop_inequality(bset
, k
);
2201 constraint_index_free(&ci
);
2204 constraint_index_free(&ci
);
2208 /* Remove constraints from "bmap" that are identical to constraints
2209 * in "context" or that are more relaxed (greater constant term).
2211 * We perform the test for shifted copies on the pure constraints
2212 * in remove_shifted_constraints.
2214 static __isl_give isl_basic_map
*isl_basic_map_remove_shifted_constraints(
2215 __isl_take isl_basic_map
*bmap
, __isl_take isl_basic_map
*context
)
2217 isl_basic_set
*bset
, *bset_context
;
2219 if (!bmap
|| !context
)
2222 if (bmap
->n_ineq
== 0 || context
->n_ineq
== 0) {
2223 isl_basic_map_free(context
);
2227 bmap
= isl_basic_map_order_divs(bmap
);
2228 context
= isl_basic_map_align_divs(context
, bmap
);
2229 bmap
= isl_basic_map_align_divs(bmap
, context
);
2231 bset
= isl_basic_map_underlying_set(isl_basic_map_copy(bmap
));
2232 bset_context
= isl_basic_map_underlying_set(context
);
2233 bset
= remove_shifted_constraints(bset
, bset_context
);
2234 isl_basic_set_free(bset_context
);
2236 bmap
= isl_basic_map_overlying_set(bset
, bmap
);
2240 isl_basic_map_free(bmap
);
2241 isl_basic_map_free(context
);
2245 /* Does the (linear part of a) constraint "c" involve any of the "len"
2246 * "relevant" dimensions?
2248 static int is_related(isl_int
*c
, int len
, int *relevant
)
2252 for (i
= 0; i
< len
; ++i
) {
2255 if (!isl_int_is_zero(c
[i
]))
2262 /* Drop constraints from "bmap" that do not involve any of
2263 * the dimensions marked "relevant".
2265 static __isl_give isl_basic_map
*drop_unrelated_constraints(
2266 __isl_take isl_basic_map
*bmap
, int *relevant
)
2271 dim
= isl_basic_map_dim(bmap
, isl_dim_all
);
2273 return isl_basic_map_free(bmap
);
2274 for (i
= 0; i
< dim
; ++i
)
2280 for (i
= bmap
->n_eq
- 1; i
>= 0; --i
)
2281 if (!is_related(bmap
->eq
[i
] + 1, dim
, relevant
)) {
2282 bmap
= isl_basic_map_cow(bmap
);
2283 if (isl_basic_map_drop_equality(bmap
, i
) < 0)
2284 return isl_basic_map_free(bmap
);
2287 for (i
= bmap
->n_ineq
- 1; i
>= 0; --i
)
2288 if (!is_related(bmap
->ineq
[i
] + 1, dim
, relevant
)) {
2289 bmap
= isl_basic_map_cow(bmap
);
2290 if (isl_basic_map_drop_inequality(bmap
, i
) < 0)
2291 return isl_basic_map_free(bmap
);
2297 /* Update the groups in "group" based on the (linear part of a) constraint "c".
2299 * In particular, for any variable involved in the constraint,
2300 * find the actual group id from before and replace the group
2301 * of the corresponding variable by the minimal group of all
2302 * the variables involved in the constraint considered so far
2303 * (if this minimum is smaller) or replace the minimum by this group
2304 * (if the minimum is larger).
2306 * At the end, all the variables in "c" will (indirectly) point
2307 * to the minimal of the groups that they referred to originally.
2309 static void update_groups(int dim
, int *group
, isl_int
*c
)
2314 for (j
= 0; j
< dim
; ++j
) {
2315 if (isl_int_is_zero(c
[j
]))
2317 while (group
[j
] >= 0 && group
[group
[j
]] != group
[j
])
2318 group
[j
] = group
[group
[j
]];
2319 if (group
[j
] == min
)
2321 if (group
[j
] < min
) {
2322 if (min
>= 0 && min
< dim
)
2323 group
[min
] = group
[j
];
2326 group
[group
[j
]] = min
;
2330 /* Allocate an array of groups of variables, one for each variable
2331 * in "context", initialized to zero.
2333 static int *alloc_groups(__isl_keep isl_basic_set
*context
)
2338 dim
= isl_basic_set_dim(context
, isl_dim_set
);
2341 ctx
= isl_basic_set_get_ctx(context
);
2342 return isl_calloc_array(ctx
, int, dim
);
2345 /* Drop constraints from "bmap" that only involve variables that are
2346 * not related to any of the variables marked with a "-1" in "group".
2348 * We construct groups of variables that collect variables that
2349 * (indirectly) appear in some common constraint of "bmap".
2350 * Each group is identified by the first variable in the group,
2351 * except for the special group of variables that was already identified
2352 * in the input as -1 (or are related to those variables).
2353 * If group[i] is equal to i (or -1), then the group of i is i (or -1),
2354 * otherwise the group of i is the group of group[i].
2356 * We first initialize groups for the remaining variables.
2357 * Then we iterate over the constraints of "bmap" and update the
2358 * group of the variables in the constraint by the smallest group.
2359 * Finally, we resolve indirect references to groups by running over
2362 * After computing the groups, we drop constraints that do not involve
2363 * any variables in the -1 group.
2365 __isl_give isl_basic_map
*isl_basic_map_drop_unrelated_constraints(
2366 __isl_take isl_basic_map
*bmap
, __isl_take
int *group
)
2372 dim
= isl_basic_map_dim(bmap
, isl_dim_all
);
2374 return isl_basic_map_free(bmap
);
2377 for (i
= 0; i
< dim
; ++i
)
2379 last
= group
[i
] = i
;
2385 for (i
= 0; i
< bmap
->n_eq
; ++i
)
2386 update_groups(dim
, group
, bmap
->eq
[i
] + 1);
2387 for (i
= 0; i
< bmap
->n_ineq
; ++i
)
2388 update_groups(dim
, group
, bmap
->ineq
[i
] + 1);
2390 for (i
= 0; i
< dim
; ++i
)
2392 group
[i
] = group
[group
[i
]];
2394 for (i
= 0; i
< dim
; ++i
)
2395 group
[i
] = group
[i
] == -1;
2397 bmap
= drop_unrelated_constraints(bmap
, group
);
2403 /* Drop constraints from "context" that are irrelevant for computing
2404 * the gist of "bset".
2406 * In particular, drop constraints in variables that are not related
2407 * to any of the variables involved in the constraints of "bset"
2408 * in the sense that there is no sequence of constraints that connects them.
2410 * We first mark all variables that appear in "bset" as belonging
2411 * to a "-1" group and then continue with group_and_drop_irrelevant_constraints.
2413 static __isl_give isl_basic_set
*drop_irrelevant_constraints(
2414 __isl_take isl_basic_set
*context
, __isl_keep isl_basic_set
*bset
)
2420 dim
= isl_basic_set_dim(bset
, isl_dim_set
);
2421 if (!context
|| dim
< 0)
2422 return isl_basic_set_free(context
);
2424 group
= alloc_groups(context
);
2427 return isl_basic_set_free(context
);
2429 for (i
= 0; i
< dim
; ++i
) {
2430 for (j
= 0; j
< bset
->n_eq
; ++j
)
2431 if (!isl_int_is_zero(bset
->eq
[j
][1 + i
]))
2433 if (j
< bset
->n_eq
) {
2437 for (j
= 0; j
< bset
->n_ineq
; ++j
)
2438 if (!isl_int_is_zero(bset
->ineq
[j
][1 + i
]))
2440 if (j
< bset
->n_ineq
)
2444 return isl_basic_map_drop_unrelated_constraints(context
, group
);
2447 /* Drop constraints from "context" that are irrelevant for computing
2448 * the gist of the inequalities "ineq".
2449 * Inequalities in "ineq" for which the corresponding element of row
2450 * is set to -1 have already been marked for removal and should be ignored.
2452 * In particular, drop constraints in variables that are not related
2453 * to any of the variables involved in "ineq"
2454 * in the sense that there is no sequence of constraints that connects them.
2456 * We first mark all variables that appear in "bset" as belonging
2457 * to a "-1" group and then continue with group_and_drop_irrelevant_constraints.
2459 static __isl_give isl_basic_set
*drop_irrelevant_constraints_marked(
2460 __isl_take isl_basic_set
*context
, __isl_keep isl_mat
*ineq
, int *row
)
2467 dim
= isl_basic_set_dim(context
, isl_dim_set
);
2468 n
= isl_mat_rows(ineq
);
2469 if (dim
< 0 || n
< 0)
2470 return isl_basic_set_free(context
);
2472 group
= alloc_groups(context
);
2475 return isl_basic_set_free(context
);
2477 for (i
= 0; i
< dim
; ++i
) {
2478 for (j
= 0; j
< n
; ++j
) {
2481 if (!isl_int_is_zero(ineq
->row
[j
][1 + i
]))
2488 return isl_basic_map_drop_unrelated_constraints(context
, group
);
2491 /* Do all "n" entries of "row" contain a negative value?
2493 static int all_neg(int *row
, int n
)
2497 for (i
= 0; i
< n
; ++i
)
2504 /* Update the inequalities in "bset" based on the information in "row"
2507 * In particular, the array "row" contains either -1, meaning that
2508 * the corresponding inequality of "bset" is redundant, or the index
2509 * of an inequality in "tab".
2511 * If the row entry is -1, then drop the inequality.
2512 * Otherwise, if the constraint is marked redundant in the tableau,
2513 * then drop the inequality. Similarly, if it is marked as an equality
2514 * in the tableau, then turn the inequality into an equality and
2515 * perform Gaussian elimination.
2517 static __isl_give isl_basic_set
*update_ineq(__isl_take isl_basic_set
*bset
,
2518 __isl_keep
int *row
, struct isl_tab
*tab
)
2523 int found_equality
= 0;
2527 if (tab
&& tab
->empty
)
2528 return isl_basic_set_set_to_empty(bset
);
2530 n_ineq
= bset
->n_ineq
;
2531 for (i
= n_ineq
- 1; i
>= 0; --i
) {
2533 if (isl_basic_set_drop_inequality(bset
, i
) < 0)
2534 return isl_basic_set_free(bset
);
2540 if (isl_tab_is_equality(tab
, n_eq
+ row
[i
])) {
2541 isl_basic_map_inequality_to_equality(bset
, i
);
2543 } else if (isl_tab_is_redundant(tab
, n_eq
+ row
[i
])) {
2544 if (isl_basic_set_drop_inequality(bset
, i
) < 0)
2545 return isl_basic_set_free(bset
);
2550 bset
= isl_basic_set_gauss(bset
, NULL
);
2551 bset
= isl_basic_set_finalize(bset
);
2555 /* Update the inequalities in "bset" based on the information in "row"
2556 * and "tab" and free all arguments (other than "bset").
2558 static __isl_give isl_basic_set
*update_ineq_free(
2559 __isl_take isl_basic_set
*bset
, __isl_take isl_mat
*ineq
,
2560 __isl_take isl_basic_set
*context
, __isl_take
int *row
,
2561 struct isl_tab
*tab
)
2564 isl_basic_set_free(context
);
2566 bset
= update_ineq(bset
, row
, tab
);
2573 /* Remove all information from bset that is redundant in the context
2575 * "ineq" contains the (possibly transformed) inequalities of "bset",
2576 * in the same order.
2577 * The (explicit) equalities of "bset" are assumed to have been taken
2578 * into account by the transformation such that only the inequalities
2580 * "context" is assumed not to be empty.
2582 * "row" keeps track of the constraint index of a "bset" inequality in "tab".
2583 * A value of -1 means that the inequality is obviously redundant and may
2584 * not even appear in "tab".
2586 * We first mark the inequalities of "bset"
2587 * that are obviously redundant with respect to some inequality in "context".
2588 * Then we remove those constraints from "context" that have become
2589 * irrelevant for computing the gist of "bset".
2590 * Note that this removal of constraints cannot be replaced by
2591 * a factorization because factors in "bset" may still be connected
2592 * to each other through constraints in "context".
2594 * If there are any inequalities left, we construct a tableau for
2595 * the context and then add the inequalities of "bset".
2596 * Before adding these inequalities, we freeze all constraints such that
2597 * they won't be considered redundant in terms of the constraints of "bset".
2598 * Then we detect all redundant constraints (among the
2599 * constraints that weren't frozen), first by checking for redundancy in the
2600 * the tableau and then by checking if replacing a constraint by its negation
2601 * would lead to an empty set. This last step is fairly expensive
2602 * and could be optimized by more reuse of the tableau.
2603 * Finally, we update bset according to the results.
2605 static __isl_give isl_basic_set
*uset_gist_full(__isl_take isl_basic_set
*bset
,
2606 __isl_take isl_mat
*ineq
, __isl_take isl_basic_set
*context
)
2611 isl_basic_set
*combined
= NULL
;
2612 struct isl_tab
*tab
= NULL
;
2613 unsigned n_eq
, context_ineq
;
2615 if (!bset
|| !ineq
|| !context
)
2618 if (bset
->n_ineq
== 0 || isl_basic_set_plain_is_universe(context
)) {
2619 isl_basic_set_free(context
);
2624 ctx
= isl_basic_set_get_ctx(context
);
2625 row
= isl_calloc_array(ctx
, int, bset
->n_ineq
);
2629 if (mark_shifted_constraints(ineq
, context
, row
) < 0)
2631 if (all_neg(row
, bset
->n_ineq
))
2632 return update_ineq_free(bset
, ineq
, context
, row
, NULL
);
2634 context
= drop_irrelevant_constraints_marked(context
, ineq
, row
);
2637 if (isl_basic_set_plain_is_universe(context
))
2638 return update_ineq_free(bset
, ineq
, context
, row
, NULL
);
2640 n_eq
= context
->n_eq
;
2641 context_ineq
= context
->n_ineq
;
2642 combined
= isl_basic_set_cow(isl_basic_set_copy(context
));
2643 combined
= isl_basic_set_extend_constraints(combined
, 0, bset
->n_ineq
);
2644 tab
= isl_tab_from_basic_set(combined
, 0);
2645 for (i
= 0; i
< context_ineq
; ++i
)
2646 if (isl_tab_freeze_constraint(tab
, n_eq
+ i
) < 0)
2648 if (isl_tab_extend_cons(tab
, bset
->n_ineq
) < 0)
2651 for (i
= 0; i
< bset
->n_ineq
; ++i
) {
2654 combined
= isl_basic_set_add_ineq(combined
, ineq
->row
[i
]);
2655 if (isl_tab_add_ineq(tab
, ineq
->row
[i
]) < 0)
2659 if (isl_tab_detect_implicit_equalities(tab
) < 0)
2661 if (isl_tab_detect_redundant(tab
) < 0)
2663 for (i
= bset
->n_ineq
- 1; i
>= 0; --i
) {
2664 isl_basic_set
*test
;
2670 if (tab
->con
[n_eq
+ r
].is_redundant
)
2672 test
= isl_basic_set_dup(combined
);
2673 test
= isl_inequality_negate(test
, r
);
2674 test
= isl_basic_set_update_from_tab(test
, tab
);
2675 is_empty
= isl_basic_set_is_empty(test
);
2676 isl_basic_set_free(test
);
2680 tab
->con
[n_eq
+ r
].is_redundant
= 1;
2682 bset
= update_ineq_free(bset
, ineq
, context
, row
, tab
);
2684 ISL_F_SET(bset
, ISL_BASIC_SET_NO_IMPLICIT
);
2685 ISL_F_SET(bset
, ISL_BASIC_SET_NO_REDUNDANT
);
2688 isl_basic_set_free(combined
);
2694 isl_basic_set_free(combined
);
2695 isl_basic_set_free(context
);
2696 isl_basic_set_free(bset
);
2700 /* Extract the inequalities of "bset" as an isl_mat.
2702 static __isl_give isl_mat
*extract_ineq(__isl_keep isl_basic_set
*bset
)
2708 total
= isl_basic_set_dim(bset
, isl_dim_all
);
2712 ctx
= isl_basic_set_get_ctx(bset
);
2713 ineq
= isl_mat_sub_alloc6(ctx
, bset
->ineq
, 0, bset
->n_ineq
,
2719 /* Remove all information from "bset" that is redundant in the context
2720 * of "context", for the case where both "bset" and "context" are
2723 static __isl_give isl_basic_set
*uset_gist_uncompressed(
2724 __isl_take isl_basic_set
*bset
, __isl_take isl_basic_set
*context
)
2728 ineq
= extract_ineq(bset
);
2729 return uset_gist_full(bset
, ineq
, context
);
2732 /* Replace "bset" by an empty basic set in the same space.
2734 static __isl_give isl_basic_set
*replace_by_empty(
2735 __isl_take isl_basic_set
*bset
)
2739 space
= isl_basic_set_get_space(bset
);
2740 isl_basic_set_free(bset
);
2741 return isl_basic_set_empty(space
);
2744 /* Remove all information from "bset" that is redundant in the context
2745 * of "context", for the case where the combined equalities of
2746 * "bset" and "context" allow for a compression that can be obtained
2747 * by preapplication of "T".
2748 * If the compression of "context" is empty, meaning that "bset" and
2749 * "context" do not intersect, then return the empty set.
2751 * "bset" itself is not transformed by "T". Instead, the inequalities
2752 * are extracted from "bset" and those are transformed by "T".
2753 * uset_gist_full then determines which of the transformed inequalities
2754 * are redundant with respect to the transformed "context" and removes
2755 * the corresponding inequalities from "bset".
2757 * After preapplying "T" to the inequalities, any common factor is
2758 * removed from the coefficients. If this results in a tightening
2759 * of the constant term, then the same tightening is applied to
2760 * the corresponding untransformed inequality in "bset".
2761 * That is, if after plugging in T, a constraint f(x) >= 0 is of the form
2765 * with 0 <= r < g, then it is equivalent to
2769 * This means that f(x) >= 0 is equivalent to f(x) - r >= 0 in the affine
2770 * subspace compressed by T since the latter would be transformed to
2774 static __isl_give isl_basic_set
*uset_gist_compressed(
2775 __isl_take isl_basic_set
*bset
, __isl_take isl_basic_set
*context
,
2776 __isl_take isl_mat
*T
)
2781 isl_size n_row
, n_col
;
2784 ineq
= extract_ineq(bset
);
2785 ineq
= isl_mat_product(ineq
, isl_mat_copy(T
));
2786 context
= isl_basic_set_preimage(context
, T
);
2788 if (!ineq
|| !context
)
2790 if (isl_basic_set_plain_is_empty(context
)) {
2792 isl_basic_set_free(context
);
2793 return replace_by_empty(bset
);
2796 ctx
= isl_mat_get_ctx(ineq
);
2797 n_row
= isl_mat_rows(ineq
);
2798 n_col
= isl_mat_cols(ineq
);
2799 if (n_row
< 0 || n_col
< 0)
2802 for (i
= 0; i
< n_row
; ++i
) {
2803 isl_seq_gcd(ineq
->row
[i
] + 1, n_col
- 1, &ctx
->normalize_gcd
);
2804 if (isl_int_is_zero(ctx
->normalize_gcd
))
2806 if (isl_int_is_one(ctx
->normalize_gcd
))
2808 isl_seq_scale_down(ineq
->row
[i
] + 1, ineq
->row
[i
] + 1,
2809 ctx
->normalize_gcd
, n_col
- 1);
2810 isl_int_fdiv_r(rem
, ineq
->row
[i
][0], ctx
->normalize_gcd
);
2811 isl_int_fdiv_q(ineq
->row
[i
][0],
2812 ineq
->row
[i
][0], ctx
->normalize_gcd
);
2813 if (isl_int_is_zero(rem
))
2815 bset
= isl_basic_set_cow(bset
);
2818 isl_int_sub(bset
->ineq
[i
][0], bset
->ineq
[i
][0], rem
);
2822 return uset_gist_full(bset
, ineq
, context
);
2825 isl_basic_set_free(context
);
2826 isl_basic_set_free(bset
);
2830 /* Project "bset" onto the variables that are involved in "template".
2832 static __isl_give isl_basic_set
*project_onto_involved(
2833 __isl_take isl_basic_set
*bset
, __isl_keep isl_basic_set
*template)
2838 n
= isl_basic_set_dim(template, isl_dim_set
);
2839 if (n
< 0 || !template)
2840 return isl_basic_set_free(bset
);
2842 for (i
= 0; i
< n
; ++i
) {
2845 involved
= isl_basic_set_involves_dims(template,
2848 return isl_basic_set_free(bset
);
2851 bset
= isl_basic_set_eliminate_vars(bset
, i
, 1);
2857 /* Remove all information from bset that is redundant in the context
2858 * of context. In particular, equalities that are linear combinations
2859 * of those in context are removed. Then the inequalities that are
2860 * redundant in the context of the equalities and inequalities of
2861 * context are removed.
2863 * First of all, we drop those constraints from "context"
2864 * that are irrelevant for computing the gist of "bset".
2865 * Alternatively, we could factorize the intersection of "context" and "bset".
2867 * We first compute the intersection of the integer affine hulls
2868 * of "bset" and "context",
2869 * compute the gist inside this intersection and then reduce
2870 * the constraints with respect to the equalities of the context
2871 * that only involve variables already involved in the input.
2872 * If the intersection of the affine hulls turns out to be empty,
2873 * then return the empty set.
2875 * If two constraints are mutually redundant, then uset_gist_full
2876 * will remove the second of those constraints. We therefore first
2877 * sort the constraints so that constraints not involving existentially
2878 * quantified variables are given precedence over those that do.
2879 * We have to perform this sorting before the variable compression,
2880 * because that may effect the order of the variables.
2882 static __isl_give isl_basic_set
*uset_gist(__isl_take isl_basic_set
*bset
,
2883 __isl_take isl_basic_set
*context
)
2888 isl_basic_set
*aff_context
;
2891 total
= isl_basic_set_dim(bset
, isl_dim_all
);
2892 if (total
< 0 || !context
)
2895 context
= drop_irrelevant_constraints(context
, bset
);
2897 bset
= isl_basic_set_detect_equalities(bset
);
2898 aff
= isl_basic_set_copy(bset
);
2899 aff
= isl_basic_set_plain_affine_hull(aff
);
2900 context
= isl_basic_set_detect_equalities(context
);
2901 aff_context
= isl_basic_set_copy(context
);
2902 aff_context
= isl_basic_set_plain_affine_hull(aff_context
);
2903 aff
= isl_basic_set_intersect(aff
, aff_context
);
2906 if (isl_basic_set_plain_is_empty(aff
)) {
2907 isl_basic_set_free(bset
);
2908 isl_basic_set_free(context
);
2911 bset
= isl_basic_set_sort_constraints(bset
);
2912 if (aff
->n_eq
== 0) {
2913 isl_basic_set_free(aff
);
2914 return uset_gist_uncompressed(bset
, context
);
2916 eq
= isl_mat_sub_alloc6(bset
->ctx
, aff
->eq
, 0, aff
->n_eq
, 0, 1 + total
);
2917 eq
= isl_mat_cow(eq
);
2918 T
= isl_mat_variable_compression(eq
, NULL
);
2919 isl_basic_set_free(aff
);
2920 if (T
&& T
->n_col
== 0) {
2922 isl_basic_set_free(context
);
2923 return replace_by_empty(bset
);
2926 aff_context
= isl_basic_set_affine_hull(isl_basic_set_copy(context
));
2927 aff_context
= project_onto_involved(aff_context
, bset
);
2929 bset
= uset_gist_compressed(bset
, context
, T
);
2930 bset
= isl_basic_set_reduce_using_equalities(bset
, aff_context
);
2933 ISL_F_SET(bset
, ISL_BASIC_SET_NO_IMPLICIT
);
2934 ISL_F_SET(bset
, ISL_BASIC_SET_NO_REDUNDANT
);
2939 isl_basic_set_free(bset
);
2940 isl_basic_set_free(context
);
2944 /* Return the number of equality constraints in "bmap" that involve
2945 * local variables. This function assumes that Gaussian elimination
2946 * has been applied to the equality constraints.
2948 static int n_div_eq(__isl_keep isl_basic_map
*bmap
)
2951 isl_size total
, n_div
;
2956 if (bmap
->n_eq
== 0)
2959 total
= isl_basic_map_dim(bmap
, isl_dim_all
);
2960 n_div
= isl_basic_map_dim(bmap
, isl_dim_div
);
2961 if (total
< 0 || n_div
< 0)
2965 for (i
= 0; i
< bmap
->n_eq
; ++i
)
2966 if (isl_seq_first_non_zero(bmap
->eq
[i
] + 1 + total
,
2973 /* Construct a basic map in "space" defined by the equality constraints in "eq".
2974 * The constraints are assumed not to involve any local variables.
2976 static __isl_give isl_basic_map
*basic_map_from_equalities(
2977 __isl_take isl_space
*space
, __isl_take isl_mat
*eq
)
2981 isl_basic_map
*bmap
= NULL
;
2983 total
= isl_space_dim(space
, isl_dim_all
);
2984 if (total
< 0 || !eq
)
2987 if (1 + total
!= eq
->n_col
)
2988 isl_die(isl_space_get_ctx(space
), isl_error_internal
,
2989 "unexpected number of columns", goto error
);
2991 bmap
= isl_basic_map_alloc_space(isl_space_copy(space
),
2993 for (i
= 0; i
< eq
->n_row
; ++i
) {
2994 k
= isl_basic_map_alloc_equality(bmap
);
2997 isl_seq_cpy(bmap
->eq
[k
], eq
->row
[i
], eq
->n_col
);
3000 isl_space_free(space
);
3004 isl_space_free(space
);
3006 isl_basic_map_free(bmap
);
3010 /* Construct and return a variable compression based on the equality
3011 * constraints in "bmap1" and "bmap2" that do not involve the local variables.
3012 * "n1" is the number of (initial) equality constraints in "bmap1"
3013 * that do involve local variables.
3014 * "n2" is the number of (initial) equality constraints in "bmap2"
3015 * that do involve local variables.
3016 * "total" is the total number of other variables.
3017 * This function assumes that Gaussian elimination
3018 * has been applied to the equality constraints in both "bmap1" and "bmap2"
3019 * such that the equality constraints not involving local variables
3020 * are those that start at "n1" or "n2".
3022 * If either of "bmap1" and "bmap2" does not have such equality constraints,
3023 * then simply compute the compression based on the equality constraints
3024 * in the other basic map.
3025 * Otherwise, combine the equality constraints from both into a new
3026 * basic map such that Gaussian elimination can be applied to this combination
3027 * and then construct a variable compression from the resulting
3028 * equality constraints.
3030 static __isl_give isl_mat
*combined_variable_compression(
3031 __isl_keep isl_basic_map
*bmap1
, int n1
,
3032 __isl_keep isl_basic_map
*bmap2
, int n2
, int total
)
3035 isl_mat
*E1
, *E2
, *V
;
3036 isl_basic_map
*bmap
;
3038 ctx
= isl_basic_map_get_ctx(bmap1
);
3039 if (bmap1
->n_eq
== n1
) {
3040 E2
= isl_mat_sub_alloc6(ctx
, bmap2
->eq
,
3041 n2
, bmap2
->n_eq
- n2
, 0, 1 + total
);
3042 return isl_mat_variable_compression(E2
, NULL
);
3044 if (bmap2
->n_eq
== n2
) {
3045 E1
= isl_mat_sub_alloc6(ctx
, bmap1
->eq
,
3046 n1
, bmap1
->n_eq
- n1
, 0, 1 + total
);
3047 return isl_mat_variable_compression(E1
, NULL
);
3049 E1
= isl_mat_sub_alloc6(ctx
, bmap1
->eq
,
3050 n1
, bmap1
->n_eq
- n1
, 0, 1 + total
);
3051 E2
= isl_mat_sub_alloc6(ctx
, bmap2
->eq
,
3052 n2
, bmap2
->n_eq
- n2
, 0, 1 + total
);
3053 E1
= isl_mat_concat(E1
, E2
);
3054 bmap
= basic_map_from_equalities(isl_basic_map_get_space(bmap1
), E1
);
3055 bmap
= isl_basic_map_gauss(bmap
, NULL
);
3058 E1
= isl_mat_sub_alloc6(ctx
, bmap
->eq
, 0, bmap
->n_eq
, 0, 1 + total
);
3059 V
= isl_mat_variable_compression(E1
, NULL
);
3060 isl_basic_map_free(bmap
);
3065 /* Extract the stride constraints from "bmap", compressed
3066 * with respect to both the stride constraints in "context" and
3067 * the remaining equality constraints in both "bmap" and "context".
3068 * "bmap_n_eq" is the number of (initial) stride constraints in "bmap".
3069 * "context_n_eq" is the number of (initial) stride constraints in "context".
3071 * Let x be all variables in "bmap" (and "context") other than the local
3072 * variables. First compute a variable compression
3076 * based on the non-stride equality constraints in "bmap" and "context".
3077 * Consider the stride constraints of "context",
3081 * with y the local variables and plug in the variable compression,
3084 * A(V x') + B(y) = 0
3086 * Use these constraints to compute a parameter compression on x'
3090 * Now consider the stride constraints of "bmap"
3094 * and plug in x = V*T x''.
3095 * That is, return A = [C*V*T D].
3097 static __isl_give isl_mat
*extract_compressed_stride_constraints(
3098 __isl_keep isl_basic_map
*bmap
, int bmap_n_eq
,
3099 __isl_keep isl_basic_map
*context
, int context_n_eq
)
3101 isl_size total
, n_div
;
3103 isl_mat
*A
, *B
, *T
, *V
;
3105 total
= isl_basic_map_dim(context
, isl_dim_all
);
3106 n_div
= isl_basic_map_dim(context
, isl_dim_div
);
3107 if (total
< 0 || n_div
< 0)
3111 ctx
= isl_basic_map_get_ctx(bmap
);
3113 V
= combined_variable_compression(bmap
, bmap_n_eq
,
3114 context
, context_n_eq
, total
);
3116 A
= isl_mat_sub_alloc6(ctx
, context
->eq
, 0, context_n_eq
, 0, 1 + total
);
3117 B
= isl_mat_sub_alloc6(ctx
, context
->eq
,
3118 0, context_n_eq
, 1 + total
, n_div
);
3119 A
= isl_mat_product(A
, isl_mat_copy(V
));
3120 T
= isl_mat_parameter_compression_ext(A
, B
);
3121 T
= isl_mat_product(V
, T
);
3123 n_div
= isl_basic_map_dim(bmap
, isl_dim_div
);
3125 T
= isl_mat_free(T
);
3127 T
= isl_mat_diagonal(T
, isl_mat_identity(ctx
, n_div
));
3129 A
= isl_mat_sub_alloc6(ctx
, bmap
->eq
,
3130 0, bmap_n_eq
, 0, 1 + total
+ n_div
);
3131 A
= isl_mat_product(A
, T
);
3136 /* Remove the prime factors from *g that have an exponent that
3137 * is strictly smaller than the exponent in "c".
3138 * All exponents in *g are known to be smaller than or equal
3141 * That is, if *g is equal to
3143 * p_1^{e_1} p_2^{e_2} ... p_n^{e_n}
3145 * and "c" is equal to
3147 * p_1^{f_1} p_2^{f_2} ... p_n^{f_n}
3151 * p_1^{e_1 * (e_1 = f_1)} p_2^{e_2 * (e_2 = f_2)} ...
3152 * p_n^{e_n * (e_n = f_n)}
3154 * If e_i = f_i, then c / *g does not have any p_i factors and therefore
3155 * neither does the gcd of *g and c / *g.
3156 * If e_i < f_i, then the gcd of *g and c / *g has a positive
3157 * power min(e_i, s_i) of p_i with s_i = f_i - e_i among its factors.
3158 * Dividing *g by this gcd therefore strictly reduces the exponent
3159 * of the prime factors that need to be removed, while leaving the
3160 * other prime factors untouched.
3161 * Repeating this process until gcd(*g, c / *g) = 1 therefore
3162 * removes all undesired factors, without removing any others.
3164 static void remove_incomplete_powers(isl_int
*g
, isl_int c
)
3170 isl_int_divexact(t
, c
, *g
);
3171 isl_int_gcd(t
, t
, *g
);
3172 if (isl_int_is_one(t
))
3174 isl_int_divexact(*g
, *g
, t
);
3179 /* Reduce the "n" stride constraints in "bmap" based on a copy "A"
3180 * of the same stride constraints in a compressed space that exploits
3181 * all equalities in the context and the other equalities in "bmap".
3183 * If the stride constraints of "bmap" are of the form
3187 * then A is of the form
3191 * If any of these constraints involves only a single local variable y,
3192 * then the constraint appears as
3202 * Let g be the gcd of m and the coefficients of h.
3203 * Then, in particular, g is a divisor of the coefficients of h and
3207 * is known to be a multiple of g.
3208 * If some prime factor in m appears with the same exponent in g,
3209 * then it can be removed from m because f(x) is already known
3210 * to be a multiple of g and therefore in particular of this power
3211 * of the prime factors.
3212 * Prime factors that appear with a smaller exponent in g cannot
3213 * be removed from m.
3214 * Let g' be the divisor of g containing all prime factors that
3215 * appear with the same exponent in m and g, then
3219 * can be replaced by
3221 * f(x) + m/g' y_i' = 0
3223 * Note that (if g' != 1) this changes the explicit representation
3224 * of y_i to that of y_i', so the integer division at position i
3225 * is marked unknown and later recomputed by a call to
3226 * isl_basic_map_gauss.
3228 static __isl_give isl_basic_map
*reduce_stride_constraints(
3229 __isl_take isl_basic_map
*bmap
, int n
, __isl_keep isl_mat
*A
)
3232 isl_size total
, n_div
;
3236 total
= isl_basic_map_dim(bmap
, isl_dim_all
);
3237 n_div
= isl_basic_map_dim(bmap
, isl_dim_div
);
3238 if (total
< 0 || n_div
< 0 || !A
)
3239 return isl_basic_map_free(bmap
);
3243 for (i
= 0; i
< n
; ++i
) {
3246 div
= isl_seq_first_non_zero(bmap
->eq
[i
] + 1 + total
, n_div
);
3248 isl_die(isl_basic_map_get_ctx(bmap
), isl_error_internal
,
3249 "equality constraints modified unexpectedly",
3251 if (isl_seq_first_non_zero(bmap
->eq
[i
] + 1 + total
+ div
+ 1,
3252 n_div
- div
- 1) != -1)
3254 if (isl_mat_row_gcd(A
, i
, &gcd
) < 0)
3256 if (isl_int_is_one(gcd
))
3258 remove_incomplete_powers(&gcd
, bmap
->eq
[i
][1 + total
+ div
]);
3259 if (isl_int_is_one(gcd
))
3261 isl_int_divexact(bmap
->eq
[i
][1 + total
+ div
],
3262 bmap
->eq
[i
][1 + total
+ div
], gcd
);
3263 bmap
= isl_basic_map_mark_div_unknown(bmap
, div
);
3271 bmap
= isl_basic_map_gauss(bmap
, NULL
);
3276 isl_basic_map_free(bmap
);
3280 /* Simplify the stride constraints in "bmap" based on
3281 * the remaining equality constraints in "bmap" and all equality
3282 * constraints in "context".
3283 * Only do this if both "bmap" and "context" have stride constraints.
3285 * First extract a copy of the stride constraints in "bmap" in a compressed
3286 * space exploiting all the other equality constraints and then
3287 * use this compressed copy to simplify the original stride constraints.
3289 static __isl_give isl_basic_map
*gist_strides(__isl_take isl_basic_map
*bmap
,
3290 __isl_keep isl_basic_map
*context
)
3292 int bmap_n_eq
, context_n_eq
;
3295 if (!bmap
|| !context
)
3296 return isl_basic_map_free(bmap
);
3298 bmap_n_eq
= n_div_eq(bmap
);
3299 context_n_eq
= n_div_eq(context
);
3301 if (bmap_n_eq
< 0 || context_n_eq
< 0)
3302 return isl_basic_map_free(bmap
);
3303 if (bmap_n_eq
== 0 || context_n_eq
== 0)
3306 A
= extract_compressed_stride_constraints(bmap
, bmap_n_eq
,
3307 context
, context_n_eq
);
3308 bmap
= reduce_stride_constraints(bmap
, bmap_n_eq
, A
);
3315 /* Return a basic map that has the same intersection with "context" as "bmap"
3316 * and that is as "simple" as possible.
3318 * The core computation is performed on the pure constraints.
3319 * When we add back the meaning of the integer divisions, we need
3320 * to (re)introduce the div constraints. If we happen to have
3321 * discovered that some of these integer divisions are equal to
3322 * some affine combination of other variables, then these div
3323 * constraints may end up getting simplified in terms of the equalities,
3324 * resulting in extra inequalities on the other variables that
3325 * may have been removed already or that may not even have been
3326 * part of the input. We try and remove those constraints of
3327 * this form that are most obviously redundant with respect to
3328 * the context. We also remove those div constraints that are
3329 * redundant with respect to the other constraints in the result.
3331 * The stride constraints among the equality constraints in "bmap" are
3332 * also simplified with respecting to the other equality constraints
3333 * in "bmap" and with respect to all equality constraints in "context".
3335 __isl_give isl_basic_map
*isl_basic_map_gist(__isl_take isl_basic_map
*bmap
,
3336 __isl_take isl_basic_map
*context
)
3338 isl_basic_set
*bset
, *eq
;
3339 isl_basic_map
*eq_bmap
;
3340 isl_size total
, n_div
, n_div_bmap
;
3341 unsigned extra
, n_eq
, n_ineq
;
3343 if (!bmap
|| !context
)
3346 if (isl_basic_map_plain_is_universe(bmap
)) {
3347 isl_basic_map_free(context
);
3350 if (isl_basic_map_plain_is_empty(context
)) {
3351 isl_space
*space
= isl_basic_map_get_space(bmap
);
3352 isl_basic_map_free(bmap
);
3353 isl_basic_map_free(context
);
3354 return isl_basic_map_universe(space
);
3356 if (isl_basic_map_plain_is_empty(bmap
)) {
3357 isl_basic_map_free(context
);
3361 bmap
= isl_basic_map_remove_redundancies(bmap
);
3362 context
= isl_basic_map_remove_redundancies(context
);
3363 bmap
= isl_basic_map_order_divs(bmap
);
3364 context
= isl_basic_map_align_divs(context
, bmap
);
3366 n_div
= isl_basic_map_dim(context
, isl_dim_div
);
3367 total
= isl_basic_map_dim(bmap
, isl_dim_all
);
3368 n_div_bmap
= isl_basic_map_dim(bmap
, isl_dim_div
);
3369 if (n_div
< 0 || total
< 0 || n_div_bmap
< 0)
3371 extra
= n_div
- n_div_bmap
;
3373 bset
= isl_basic_map_underlying_set(isl_basic_map_copy(bmap
));
3374 bset
= isl_basic_set_add_dims(bset
, isl_dim_set
, extra
);
3375 bset
= uset_gist(bset
,
3376 isl_basic_map_underlying_set(isl_basic_map_copy(context
)));
3377 bset
= isl_basic_set_project_out(bset
, isl_dim_set
, total
, extra
);
3379 if (!bset
|| bset
->n_eq
== 0 || n_div
== 0 ||
3380 isl_basic_set_plain_is_empty(bset
)) {
3381 isl_basic_map_free(context
);
3382 return isl_basic_map_overlying_set(bset
, bmap
);
3386 n_ineq
= bset
->n_ineq
;
3387 eq
= isl_basic_set_copy(bset
);
3388 eq
= isl_basic_set_cow(eq
);
3389 eq
= isl_basic_set_free_inequality(eq
, n_ineq
);
3390 bset
= isl_basic_set_free_equality(bset
, n_eq
);
3392 eq_bmap
= isl_basic_map_overlying_set(eq
, isl_basic_map_copy(bmap
));
3393 eq_bmap
= gist_strides(eq_bmap
, context
);
3394 eq_bmap
= isl_basic_map_remove_shifted_constraints(eq_bmap
, context
);
3395 bmap
= isl_basic_map_overlying_set(bset
, bmap
);
3396 bmap
= isl_basic_map_intersect(bmap
, eq_bmap
);
3397 bmap
= isl_basic_map_remove_redundancies(bmap
);
3401 isl_basic_map_free(bmap
);
3402 isl_basic_map_free(context
);
3407 * Assumes context has no implicit divs.
3409 __isl_give isl_map
*isl_map_gist_basic_map(__isl_take isl_map
*map
,
3410 __isl_take isl_basic_map
*context
)
3414 if (!map
|| !context
)
3417 if (isl_basic_map_plain_is_empty(context
)) {
3418 isl_space
*space
= isl_map_get_space(map
);
3420 isl_basic_map_free(context
);
3421 return isl_map_universe(space
);
3424 context
= isl_basic_map_remove_redundancies(context
);
3425 map
= isl_map_cow(map
);
3426 if (isl_map_basic_map_check_equal_space(map
, context
) < 0)
3428 map
= isl_map_compute_divs(map
);
3431 for (i
= map
->n
- 1; i
>= 0; --i
) {
3432 map
->p
[i
] = isl_basic_map_gist(map
->p
[i
],
3433 isl_basic_map_copy(context
));
3436 if (isl_basic_map_plain_is_empty(map
->p
[i
])) {
3437 isl_basic_map_free(map
->p
[i
]);
3438 if (i
!= map
->n
- 1)
3439 map
->p
[i
] = map
->p
[map
->n
- 1];
3443 isl_basic_map_free(context
);
3444 ISL_F_CLR(map
, ISL_MAP_NORMALIZED
);
3448 isl_basic_map_free(context
);
3452 /* Drop all inequalities from "bmap" that also appear in "context".
3453 * "context" is assumed to have only known local variables and
3454 * the initial local variables of "bmap" are assumed to be the same
3455 * as those of "context".
3456 * The constraints of both "bmap" and "context" are assumed
3457 * to have been sorted using isl_basic_map_sort_constraints.
3459 * Run through the inequality constraints of "bmap" and "context"
3461 * If a constraint of "bmap" involves variables not in "context",
3462 * then it cannot appear in "context".
3463 * If a matching constraint is found, it is removed from "bmap".
3465 static __isl_give isl_basic_map
*drop_inequalities(
3466 __isl_take isl_basic_map
*bmap
, __isl_keep isl_basic_map
*context
)
3469 isl_size total
, bmap_total
;
3472 total
= isl_basic_map_dim(context
, isl_dim_all
);
3473 bmap_total
= isl_basic_map_dim(bmap
, isl_dim_all
);
3474 if (total
< 0 || bmap_total
< 0)
3475 return isl_basic_map_free(bmap
);
3477 extra
= bmap_total
- total
;
3479 i1
= bmap
->n_ineq
- 1;
3480 i2
= context
->n_ineq
- 1;
3481 while (bmap
&& i1
>= 0 && i2
>= 0) {
3484 if (isl_seq_first_non_zero(bmap
->ineq
[i1
] + 1 + total
,
3489 cmp
= isl_basic_map_constraint_cmp(context
, bmap
->ineq
[i1
],
3499 if (isl_int_eq(bmap
->ineq
[i1
][0], context
->ineq
[i2
][0])) {
3500 bmap
= isl_basic_map_cow(bmap
);
3501 if (isl_basic_map_drop_inequality(bmap
, i1
) < 0)
3502 bmap
= isl_basic_map_free(bmap
);
3511 /* Drop all equalities from "bmap" that also appear in "context".
3512 * "context" is assumed to have only known local variables and
3513 * the initial local variables of "bmap" are assumed to be the same
3514 * as those of "context".
3516 * Run through the equality constraints of "bmap" and "context"
3518 * If a constraint of "bmap" involves variables not in "context",
3519 * then it cannot appear in "context".
3520 * If a matching constraint is found, it is removed from "bmap".
3522 static __isl_give isl_basic_map
*drop_equalities(
3523 __isl_take isl_basic_map
*bmap
, __isl_keep isl_basic_map
*context
)
3526 isl_size total
, bmap_total
;
3529 total
= isl_basic_map_dim(context
, isl_dim_all
);
3530 bmap_total
= isl_basic_map_dim(bmap
, isl_dim_all
);
3531 if (total
< 0 || bmap_total
< 0)
3532 return isl_basic_map_free(bmap
);
3534 extra
= bmap_total
- total
;
3536 i1
= bmap
->n_eq
- 1;
3537 i2
= context
->n_eq
- 1;
3539 while (bmap
&& i1
>= 0 && i2
>= 0) {
3542 if (isl_seq_first_non_zero(bmap
->eq
[i1
] + 1 + total
,
3545 last1
= isl_seq_last_non_zero(bmap
->eq
[i1
] + 1, total
);
3546 last2
= isl_seq_last_non_zero(context
->eq
[i2
] + 1, total
);
3547 if (last1
> last2
) {
3551 if (last1
< last2
) {
3555 if (isl_seq_eq(bmap
->eq
[i1
], context
->eq
[i2
], 1 + total
)) {
3556 bmap
= isl_basic_map_cow(bmap
);
3557 if (isl_basic_map_drop_equality(bmap
, i1
) < 0)
3558 bmap
= isl_basic_map_free(bmap
);
3567 /* Remove the constraints in "context" from "bmap".
3568 * "context" is assumed to have explicit representations
3569 * for all local variables.
3571 * First align the divs of "bmap" to those of "context" and
3572 * sort the constraints. Then drop all constraints from "bmap"
3573 * that appear in "context".
3575 __isl_give isl_basic_map
*isl_basic_map_plain_gist(
3576 __isl_take isl_basic_map
*bmap
, __isl_take isl_basic_map
*context
)
3578 isl_bool done
, known
;
3580 done
= isl_basic_map_plain_is_universe(context
);
3581 if (done
== isl_bool_false
)
3582 done
= isl_basic_map_plain_is_universe(bmap
);
3583 if (done
== isl_bool_false
)
3584 done
= isl_basic_map_plain_is_empty(context
);
3585 if (done
== isl_bool_false
)
3586 done
= isl_basic_map_plain_is_empty(bmap
);
3590 isl_basic_map_free(context
);
3593 known
= isl_basic_map_divs_known(context
);
3597 isl_die(isl_basic_map_get_ctx(bmap
), isl_error_invalid
,
3598 "context has unknown divs", goto error
);
3600 context
= isl_basic_map_order_divs(context
);
3601 bmap
= isl_basic_map_align_divs(bmap
, context
);
3602 bmap
= isl_basic_map_gauss(bmap
, NULL
);
3603 bmap
= isl_basic_map_sort_constraints(bmap
);
3604 context
= isl_basic_map_sort_constraints(context
);
3606 bmap
= drop_inequalities(bmap
, context
);
3607 bmap
= drop_equalities(bmap
, context
);
3609 isl_basic_map_free(context
);
3610 bmap
= isl_basic_map_finalize(bmap
);
3613 isl_basic_map_free(bmap
);
3614 isl_basic_map_free(context
);
3618 /* Replace "map" by the disjunct at position "pos" and free "context".
3620 static __isl_give isl_map
*replace_by_disjunct(__isl_take isl_map
*map
,
3621 int pos
, __isl_take isl_basic_map
*context
)
3623 isl_basic_map
*bmap
;
3625 bmap
= isl_basic_map_copy(map
->p
[pos
]);
3627 isl_basic_map_free(context
);
3628 return isl_map_from_basic_map(bmap
);
3631 /* Remove the constraints in "context" from "map".
3632 * If any of the disjuncts in the result turns out to be the universe,
3633 * then return this universe.
3634 * "context" is assumed to have explicit representations
3635 * for all local variables.
3637 __isl_give isl_map
*isl_map_plain_gist_basic_map(__isl_take isl_map
*map
,
3638 __isl_take isl_basic_map
*context
)
3641 isl_bool univ
, known
;
3643 univ
= isl_basic_map_plain_is_universe(context
);
3647 isl_basic_map_free(context
);
3650 known
= isl_basic_map_divs_known(context
);
3654 isl_die(isl_map_get_ctx(map
), isl_error_invalid
,
3655 "context has unknown divs", goto error
);
3657 map
= isl_map_cow(map
);
3660 for (i
= 0; i
< map
->n
; ++i
) {
3661 map
->p
[i
] = isl_basic_map_plain_gist(map
->p
[i
],
3662 isl_basic_map_copy(context
));
3663 univ
= isl_basic_map_plain_is_universe(map
->p
[i
]);
3666 if (univ
&& map
->n
> 1)
3667 return replace_by_disjunct(map
, i
, context
);
3670 isl_basic_map_free(context
);
3671 ISL_F_CLR(map
, ISL_MAP_NORMALIZED
);
3673 ISL_F_CLR(map
, ISL_MAP_DISJOINT
);
3677 isl_basic_map_free(context
);
3681 /* Remove the constraints in "context" from "set".
3682 * If any of the disjuncts in the result turns out to be the universe,
3683 * then return this universe.
3684 * "context" is assumed to have explicit representations
3685 * for all local variables.
3687 __isl_give isl_set
*isl_set_plain_gist_basic_set(__isl_take isl_set
*set
,
3688 __isl_take isl_basic_set
*context
)
3690 return set_from_map(isl_map_plain_gist_basic_map(set_to_map(set
),
3691 bset_to_bmap(context
)));
3694 /* Remove the constraints in "context" from "map".
3695 * If any of the disjuncts in the result turns out to be the universe,
3696 * then return this universe.
3697 * "context" is assumed to consist of a single disjunct and
3698 * to have explicit representations for all local variables.
3700 __isl_give isl_map
*isl_map_plain_gist(__isl_take isl_map
*map
,
3701 __isl_take isl_map
*context
)
3703 isl_basic_map
*hull
;
3705 hull
= isl_map_unshifted_simple_hull(context
);
3706 return isl_map_plain_gist_basic_map(map
, hull
);
3709 /* Replace "map" by a universe map in the same space and free "drop".
3711 static __isl_give isl_map
*replace_by_universe(__isl_take isl_map
*map
,
3712 __isl_take isl_map
*drop
)
3716 res
= isl_map_universe(isl_map_get_space(map
));
3722 /* Return a map that has the same intersection with "context" as "map"
3723 * and that is as "simple" as possible.
3725 * If "map" is already the universe, then we cannot make it any simpler.
3726 * Similarly, if "context" is the universe, then we cannot exploit it
3728 * If "map" and "context" are identical to each other, then we can
3729 * return the corresponding universe.
3731 * If either "map" or "context" consists of multiple disjuncts,
3732 * then check if "context" happens to be a subset of "map",
3733 * in which case all constraints can be removed.
3734 * In case of multiple disjuncts, the standard procedure
3735 * may not be able to detect that all constraints can be removed.
3737 * If none of these cases apply, we have to work a bit harder.
3738 * During this computation, we make use of a single disjunct context,
3739 * so if the original context consists of more than one disjunct
3740 * then we need to approximate the context by a single disjunct set.
3741 * Simply taking the simple hull may drop constraints that are
3742 * only implicitly available in each disjunct. We therefore also
3743 * look for constraints among those defining "map" that are valid
3744 * for the context. These can then be used to simplify away
3745 * the corresponding constraints in "map".
3747 __isl_give isl_map
*isl_map_gist(__isl_take isl_map
*map
,
3748 __isl_take isl_map
*context
)
3752 isl_size n_disjunct_map
, n_disjunct_context
;
3754 isl_basic_map
*hull
;
3756 is_universe
= isl_map_plain_is_universe(map
);
3757 if (is_universe
>= 0 && !is_universe
)
3758 is_universe
= isl_map_plain_is_universe(context
);
3759 if (is_universe
< 0)
3762 isl_map_free(context
);
3766 isl_map_align_params_bin(&map
, &context
);
3767 equal
= isl_map_plain_is_equal(map
, context
);
3771 return replace_by_universe(map
, context
);
3773 n_disjunct_map
= isl_map_n_basic_map(map
);
3774 n_disjunct_context
= isl_map_n_basic_map(context
);
3775 if (n_disjunct_map
< 0 || n_disjunct_context
< 0)
3777 if (n_disjunct_map
!= 1 || n_disjunct_context
!= 1) {
3778 subset
= isl_map_is_subset(context
, map
);
3782 return replace_by_universe(map
, context
);
3785 context
= isl_map_compute_divs(context
);
3788 if (n_disjunct_context
== 1) {
3789 hull
= isl_map_simple_hull(context
);
3794 ctx
= isl_map_get_ctx(map
);
3795 list
= isl_map_list_alloc(ctx
, 2);
3796 list
= isl_map_list_add(list
, isl_map_copy(context
));
3797 list
= isl_map_list_add(list
, isl_map_copy(map
));
3798 hull
= isl_map_unshifted_simple_hull_from_map_list(context
,
3801 return isl_map_gist_basic_map(map
, hull
);
3804 isl_map_free(context
);
3808 __isl_give isl_basic_set
*isl_basic_set_gist(__isl_take isl_basic_set
*bset
,
3809 __isl_take isl_basic_set
*context
)
3811 return bset_from_bmap(isl_basic_map_gist(bset_to_bmap(bset
),
3812 bset_to_bmap(context
)));
3815 __isl_give isl_set
*isl_set_gist_basic_set(__isl_take isl_set
*set
,
3816 __isl_take isl_basic_set
*context
)
3818 return set_from_map(isl_map_gist_basic_map(set_to_map(set
),
3819 bset_to_bmap(context
)));
3822 __isl_give isl_set
*isl_set_gist_params_basic_set(__isl_take isl_set
*set
,
3823 __isl_take isl_basic_set
*context
)
3825 isl_space
*space
= isl_set_get_space(set
);
3826 isl_basic_set
*dom_context
= isl_basic_set_universe(space
);
3827 dom_context
= isl_basic_set_intersect_params(dom_context
, context
);
3828 return isl_set_gist_basic_set(set
, dom_context
);
3831 __isl_give isl_set
*isl_set_gist(__isl_take isl_set
*set
,
3832 __isl_take isl_set
*context
)
3834 return set_from_map(isl_map_gist(set_to_map(set
), set_to_map(context
)));
3837 /* Compute the gist of "bmap" with respect to the constraints "context"
3840 __isl_give isl_basic_map
*isl_basic_map_gist_domain(
3841 __isl_take isl_basic_map
*bmap
, __isl_take isl_basic_set
*context
)
3843 isl_space
*space
= isl_basic_map_get_space(bmap
);
3844 isl_basic_map
*bmap_context
= isl_basic_map_universe(space
);
3846 bmap_context
= isl_basic_map_intersect_domain(bmap_context
, context
);
3847 return isl_basic_map_gist(bmap
, bmap_context
);
3850 __isl_give isl_map
*isl_map_gist_domain(__isl_take isl_map
*map
,
3851 __isl_take isl_set
*context
)
3853 isl_map
*map_context
= isl_map_universe(isl_map_get_space(map
));
3854 map_context
= isl_map_intersect_domain(map_context
, context
);
3855 return isl_map_gist(map
, map_context
);
3858 __isl_give isl_map
*isl_map_gist_range(__isl_take isl_map
*map
,
3859 __isl_take isl_set
*context
)
3861 isl_map
*map_context
= isl_map_universe(isl_map_get_space(map
));
3862 map_context
= isl_map_intersect_range(map_context
, context
);
3863 return isl_map_gist(map
, map_context
);
3866 __isl_give isl_map
*isl_map_gist_params(__isl_take isl_map
*map
,
3867 __isl_take isl_set
*context
)
3869 isl_map
*map_context
= isl_map_universe(isl_map_get_space(map
));
3870 map_context
= isl_map_intersect_params(map_context
, context
);
3871 return isl_map_gist(map
, map_context
);
3874 __isl_give isl_set
*isl_set_gist_params(__isl_take isl_set
*set
,
3875 __isl_take isl_set
*context
)
3877 return isl_map_gist_params(set
, context
);
3880 /* Quick check to see if two basic maps are disjoint.
3881 * In particular, we reduce the equalities and inequalities of
3882 * one basic map in the context of the equalities of the other
3883 * basic map and check if we get a contradiction.
3885 isl_bool
isl_basic_map_plain_is_disjoint(__isl_keep isl_basic_map
*bmap1
,
3886 __isl_keep isl_basic_map
*bmap2
)
3888 struct isl_vec
*v
= NULL
;
3893 if (isl_basic_map_check_equal_space(bmap1
, bmap2
) < 0)
3894 return isl_bool_error
;
3895 if (bmap1
->n_div
|| bmap2
->n_div
)
3896 return isl_bool_false
;
3897 if (!bmap1
->n_eq
&& !bmap2
->n_eq
)
3898 return isl_bool_false
;
3900 total
= isl_space_dim(bmap1
->dim
, isl_dim_all
);
3902 return isl_bool_error
;
3904 return isl_bool_false
;
3905 v
= isl_vec_alloc(bmap1
->ctx
, 1 + total
);
3908 elim
= isl_alloc_array(bmap1
->ctx
, int, total
);
3911 compute_elimination_index(bmap1
, elim
, total
);
3912 for (i
= 0; i
< bmap2
->n_eq
; ++i
) {
3914 reduced
= reduced_using_equalities(v
->block
.data
, bmap2
->eq
[i
],
3915 bmap1
, elim
, total
);
3916 if (reduced
&& !isl_int_is_zero(v
->block
.data
[0]) &&
3917 isl_seq_first_non_zero(v
->block
.data
+ 1, total
) == -1)
3920 for (i
= 0; i
< bmap2
->n_ineq
; ++i
) {
3922 reduced
= reduced_using_equalities(v
->block
.data
,
3923 bmap2
->ineq
[i
], bmap1
, elim
, total
);
3924 if (reduced
&& isl_int_is_neg(v
->block
.data
[0]) &&
3925 isl_seq_first_non_zero(v
->block
.data
+ 1, total
) == -1)
3928 compute_elimination_index(bmap2
, elim
, total
);
3929 for (i
= 0; i
< bmap1
->n_ineq
; ++i
) {
3931 reduced
= reduced_using_equalities(v
->block
.data
,
3932 bmap1
->ineq
[i
], bmap2
, elim
, total
);
3933 if (reduced
&& isl_int_is_neg(v
->block
.data
[0]) &&
3934 isl_seq_first_non_zero(v
->block
.data
+ 1, total
) == -1)
3939 return isl_bool_false
;
3943 return isl_bool_true
;
3947 return isl_bool_error
;
3950 int isl_basic_set_plain_is_disjoint(__isl_keep isl_basic_set
*bset1
,
3951 __isl_keep isl_basic_set
*bset2
)
3953 return isl_basic_map_plain_is_disjoint(bset_to_bmap(bset1
),
3954 bset_to_bmap(bset2
));
3957 /* Does "test" hold for all pairs of basic maps in "map1" and "map2"?
3959 static isl_bool
all_pairs(__isl_keep isl_map
*map1
, __isl_keep isl_map
*map2
,
3960 isl_bool (*test
)(__isl_keep isl_basic_map
*bmap1
,
3961 __isl_keep isl_basic_map
*bmap2
))
3966 return isl_bool_error
;
3968 for (i
= 0; i
< map1
->n
; ++i
) {
3969 for (j
= 0; j
< map2
->n
; ++j
) {
3970 isl_bool d
= test(map1
->p
[i
], map2
->p
[j
]);
3971 if (d
!= isl_bool_true
)
3976 return isl_bool_true
;
3979 /* Are "map1" and "map2" obviously disjoint, based on information
3980 * that can be derived without looking at the individual basic maps?
3982 * In particular, if one of them is empty or if they live in different spaces
3983 * (ignoring parameters), then they are clearly disjoint.
3985 static isl_bool
isl_map_plain_is_disjoint_global(__isl_keep isl_map
*map1
,
3986 __isl_keep isl_map
*map2
)
3992 return isl_bool_error
;
3994 disjoint
= isl_map_plain_is_empty(map1
);
3995 if (disjoint
< 0 || disjoint
)
3998 disjoint
= isl_map_plain_is_empty(map2
);
3999 if (disjoint
< 0 || disjoint
)
4002 match
= isl_map_tuple_is_equal(map1
, isl_dim_in
, map2
, isl_dim_in
);
4003 if (match
< 0 || !match
)
4004 return match
< 0 ? isl_bool_error
: isl_bool_true
;
4006 match
= isl_map_tuple_is_equal(map1
, isl_dim_out
, map2
, isl_dim_out
);
4007 if (match
< 0 || !match
)
4008 return match
< 0 ? isl_bool_error
: isl_bool_true
;
4010 return isl_bool_false
;
4013 /* Are "map1" and "map2" obviously disjoint?
4015 * If one of them is empty or if they live in different spaces (ignoring
4016 * parameters), then they are clearly disjoint.
4017 * This is checked by isl_map_plain_is_disjoint_global.
4019 * If they have different parameters, then we skip any further tests.
4021 * If they are obviously equal, but not obviously empty, then we will
4022 * not be able to detect if they are disjoint.
4024 * Otherwise we check if each basic map in "map1" is obviously disjoint
4025 * from each basic map in "map2".
4027 isl_bool
isl_map_plain_is_disjoint(__isl_keep isl_map
*map1
,
4028 __isl_keep isl_map
*map2
)
4034 disjoint
= isl_map_plain_is_disjoint_global(map1
, map2
);
4035 if (disjoint
< 0 || disjoint
)
4038 match
= isl_map_has_equal_params(map1
, map2
);
4039 if (match
< 0 || !match
)
4040 return match
< 0 ? isl_bool_error
: isl_bool_false
;
4042 intersect
= isl_map_plain_is_equal(map1
, map2
);
4043 if (intersect
< 0 || intersect
)
4044 return intersect
< 0 ? isl_bool_error
: isl_bool_false
;
4046 return all_pairs(map1
, map2
, &isl_basic_map_plain_is_disjoint
);
4049 /* Are "map1" and "map2" disjoint?
4050 * The parameters are assumed to have been aligned.
4052 * In particular, check whether all pairs of basic maps are disjoint.
4054 static isl_bool
isl_map_is_disjoint_aligned(__isl_keep isl_map
*map1
,
4055 __isl_keep isl_map
*map2
)
4057 return all_pairs(map1
, map2
, &isl_basic_map_is_disjoint
);
4060 /* Are "map1" and "map2" disjoint?
4062 * They are disjoint if they are "obviously disjoint" or if one of them
4063 * is empty. Otherwise, they are not disjoint if one of them is universal.
4064 * If the two inputs are (obviously) equal and not empty, then they are
4066 * If none of these cases apply, then check if all pairs of basic maps
4067 * are disjoint after aligning the parameters.
4069 isl_bool
isl_map_is_disjoint(__isl_keep isl_map
*map1
, __isl_keep isl_map
*map2
)
4074 disjoint
= isl_map_plain_is_disjoint_global(map1
, map2
);
4075 if (disjoint
< 0 || disjoint
)
4078 disjoint
= isl_map_is_empty(map1
);
4079 if (disjoint
< 0 || disjoint
)
4082 disjoint
= isl_map_is_empty(map2
);
4083 if (disjoint
< 0 || disjoint
)
4086 intersect
= isl_map_plain_is_universe(map1
);
4087 if (intersect
< 0 || intersect
)
4088 return isl_bool_not(intersect
);
4090 intersect
= isl_map_plain_is_universe(map2
);
4091 if (intersect
< 0 || intersect
)
4092 return isl_bool_not(intersect
);
4094 intersect
= isl_map_plain_is_equal(map1
, map2
);
4095 if (intersect
< 0 || intersect
)
4096 return isl_bool_not(intersect
);
4098 return isl_map_align_params_map_map_and_test(map1
, map2
,
4099 &isl_map_is_disjoint_aligned
);
4102 /* Are "bmap1" and "bmap2" disjoint?
4104 * They are disjoint if they are "obviously disjoint" or if one of them
4105 * is empty. Otherwise, they are not disjoint if one of them is universal.
4106 * If none of these cases apply, we compute the intersection and see if
4107 * the result is empty.
4109 isl_bool
isl_basic_map_is_disjoint(__isl_keep isl_basic_map
*bmap1
,
4110 __isl_keep isl_basic_map
*bmap2
)
4114 isl_basic_map
*test
;
4116 disjoint
= isl_basic_map_plain_is_disjoint(bmap1
, bmap2
);
4117 if (disjoint
< 0 || disjoint
)
4120 disjoint
= isl_basic_map_is_empty(bmap1
);
4121 if (disjoint
< 0 || disjoint
)
4124 disjoint
= isl_basic_map_is_empty(bmap2
);
4125 if (disjoint
< 0 || disjoint
)
4128 intersect
= isl_basic_map_plain_is_universe(bmap1
);
4129 if (intersect
< 0 || intersect
)
4130 return isl_bool_not(intersect
);
4132 intersect
= isl_basic_map_plain_is_universe(bmap2
);
4133 if (intersect
< 0 || intersect
)
4134 return isl_bool_not(intersect
);
4136 test
= isl_basic_map_intersect(isl_basic_map_copy(bmap1
),
4137 isl_basic_map_copy(bmap2
));
4138 disjoint
= isl_basic_map_is_empty(test
);
4139 isl_basic_map_free(test
);
4144 /* Are "bset1" and "bset2" disjoint?
4146 isl_bool
isl_basic_set_is_disjoint(__isl_keep isl_basic_set
*bset1
,
4147 __isl_keep isl_basic_set
*bset2
)
4149 return isl_basic_map_is_disjoint(bset1
, bset2
);
4152 isl_bool
isl_set_plain_is_disjoint(__isl_keep isl_set
*set1
,
4153 __isl_keep isl_set
*set2
)
4155 return isl_map_plain_is_disjoint(set_to_map(set1
), set_to_map(set2
));
4158 /* Are "set1" and "set2" disjoint?
4160 isl_bool
isl_set_is_disjoint(__isl_keep isl_set
*set1
, __isl_keep isl_set
*set2
)
4162 return isl_map_is_disjoint(set1
, set2
);
4165 /* Is "v" equal to 0, 1 or -1?
4167 static int is_zero_or_one(isl_int v
)
4169 return isl_int_is_zero(v
) || isl_int_is_one(v
) || isl_int_is_negone(v
);
4172 /* Are the "n" coefficients starting at "first" of inequality constraints
4173 * "i" and "j" of "bmap" opposite to each other?
4175 static int is_opposite_part(__isl_keep isl_basic_map
*bmap
, int i
, int j
,
4178 return isl_seq_is_neg(bmap
->ineq
[i
] + first
, bmap
->ineq
[j
] + first
, n
);
4181 /* Are inequality constraints "i" and "j" of "bmap" opposite to each other,
4182 * apart from the constant term?
4184 static isl_bool
is_opposite(__isl_keep isl_basic_map
*bmap
, int i
, int j
)
4188 total
= isl_basic_map_dim(bmap
, isl_dim_all
);
4190 return isl_bool_error
;
4191 return is_opposite_part(bmap
, i
, j
, 1, total
);
4194 /* Check if we can combine a given div with lower bound l and upper
4195 * bound u with some other div and if so return that other div.
4196 * Otherwise, return a position beyond the integer divisions.
4197 * Return -1 on error.
4199 * We first check that
4200 * - the bounds are opposites of each other (except for the constant
4202 * - the bounds do not reference any other div
4203 * - no div is defined in terms of this div
4205 * Let m be the size of the range allowed on the div by the bounds.
4206 * That is, the bounds are of the form
4208 * e <= a <= e + m - 1
4210 * with e some expression in the other variables.
4211 * We look for another div b such that no third div is defined in terms
4212 * of this second div b and such that in any constraint that contains
4213 * a (except for the given lower and upper bound), also contains b
4214 * with a coefficient that is m times that of b.
4215 * That is, all constraints (except for the lower and upper bound)
4218 * e + f (a + m b) >= 0
4220 * Furthermore, in the constraints that only contain b, the coefficient
4221 * of b should be equal to 1 or -1.
4222 * If so, we return b so that "a + m b" can be replaced by
4223 * a single div "c = a + m b".
4225 static int div_find_coalesce(__isl_keep isl_basic_map
*bmap
, int *pairs
,
4226 unsigned div
, unsigned l
, unsigned u
)
4234 n_div
= isl_basic_map_dim(bmap
, isl_dim_div
);
4237 v_div
= isl_basic_map_var_offset(bmap
, isl_dim_div
);
4240 if (isl_seq_first_non_zero(bmap
->ineq
[l
] + 1 + v_div
, div
) != -1)
4242 if (isl_seq_first_non_zero(bmap
->ineq
[l
] + 1 + v_div
+ div
+ 1,
4243 n_div
- div
- 1) != -1)
4245 opp
= is_opposite(bmap
, l
, u
);
4246 if (opp
< 0 || !opp
)
4247 return opp
< 0 ? -1 : n_div
;
4249 for (i
= 0; i
< n_div
; ++i
) {
4250 if (isl_int_is_zero(bmap
->div
[i
][0]))
4252 if (!isl_int_is_zero(bmap
->div
[i
][1 + 1 + v_div
+ div
]))
4256 isl_int_add(bmap
->ineq
[l
][0], bmap
->ineq
[l
][0], bmap
->ineq
[u
][0]);
4257 if (isl_int_is_neg(bmap
->ineq
[l
][0])) {
4258 isl_int_sub(bmap
->ineq
[l
][0],
4259 bmap
->ineq
[l
][0], bmap
->ineq
[u
][0]);
4260 bmap
= isl_basic_map_copy(bmap
);
4261 bmap
= isl_basic_map_set_to_empty(bmap
);
4262 isl_basic_map_free(bmap
);
4265 isl_int_add_ui(bmap
->ineq
[l
][0], bmap
->ineq
[l
][0], 1);
4267 for (i
= 0; i
< n_div
; ++i
) {
4272 for (j
= 0; j
< n_div
; ++j
) {
4273 if (isl_int_is_zero(bmap
->div
[j
][0]))
4275 if (!isl_int_is_zero(bmap
->div
[j
][1 + 1 + v_div
+ i
]))
4280 for (j
= 0; j
< bmap
->n_ineq
; ++j
) {
4282 if (j
== l
|| j
== u
)
4284 if (isl_int_is_zero(bmap
->ineq
[j
][1 + v_div
+ div
])) {
4285 if (is_zero_or_one(bmap
->ineq
[j
][1 + v_div
+ i
]))
4289 if (isl_int_is_zero(bmap
->ineq
[j
][1 + v_div
+ i
]))
4291 isl_int_mul(bmap
->ineq
[j
][1 + v_div
+ div
],
4292 bmap
->ineq
[j
][1 + v_div
+ div
],
4294 valid
= isl_int_eq(bmap
->ineq
[j
][1 + v_div
+ div
],
4295 bmap
->ineq
[j
][1 + v_div
+ i
]);
4296 isl_int_divexact(bmap
->ineq
[j
][1 + v_div
+ div
],
4297 bmap
->ineq
[j
][1 + v_div
+ div
],
4302 if (j
< bmap
->n_ineq
)
4307 isl_int_sub_ui(bmap
->ineq
[l
][0], bmap
->ineq
[l
][0], 1);
4308 isl_int_sub(bmap
->ineq
[l
][0], bmap
->ineq
[l
][0], bmap
->ineq
[u
][0]);
4312 /* Internal data structure used during the construction and/or evaluation of
4313 * an inequality that ensures that a pair of bounds always allows
4314 * for an integer value.
4316 * "tab" is the tableau in which the inequality is evaluated. It may
4317 * be NULL until it is actually needed.
4318 * "v" contains the inequality coefficients.
4319 * "g", "fl" and "fu" are temporary scalars used during the construction and
4322 struct test_ineq_data
{
4323 struct isl_tab
*tab
;
4330 /* Free all the memory allocated by the fields of "data".
4332 static void test_ineq_data_clear(struct test_ineq_data
*data
)
4334 isl_tab_free(data
->tab
);
4335 isl_vec_free(data
->v
);
4336 isl_int_clear(data
->g
);
4337 isl_int_clear(data
->fl
);
4338 isl_int_clear(data
->fu
);
4341 /* Is the inequality stored in data->v satisfied by "bmap"?
4342 * That is, does it only attain non-negative values?
4343 * data->tab is a tableau corresponding to "bmap".
4345 static isl_bool
test_ineq_is_satisfied(__isl_keep isl_basic_map
*bmap
,
4346 struct test_ineq_data
*data
)
4349 enum isl_lp_result res
;
4351 ctx
= isl_basic_map_get_ctx(bmap
);
4353 data
->tab
= isl_tab_from_basic_map(bmap
, 0);
4354 res
= isl_tab_min(data
->tab
, data
->v
->el
, ctx
->one
, &data
->g
, NULL
, 0);
4355 if (res
== isl_lp_error
)
4356 return isl_bool_error
;
4357 return res
== isl_lp_ok
&& isl_int_is_nonneg(data
->g
);
4360 /* Given a lower and an upper bound on div i, do they always allow
4361 * for an integer value of the given div?
4362 * Determine this property by constructing an inequality
4363 * such that the property is guaranteed when the inequality is nonnegative.
4364 * The lower bound is inequality l, while the upper bound is inequality u.
4365 * The constructed inequality is stored in data->v.
4367 * Let the upper bound be
4371 * and the lower bound
4375 * Let n_u = f_u g and n_l = f_l g, with g = gcd(n_u, n_l).
4378 * - f_u e_l <= f_u f_l g a <= f_l e_u
4380 * Since all variables are integer valued, this is equivalent to
4382 * - f_u e_l - (f_u - 1) <= f_u f_l g a <= f_l e_u + (f_l - 1)
4384 * If this interval is at least f_u f_l g, then it contains at least
4385 * one integer value for a.
4386 * That is, the test constraint is
4388 * f_l e_u + f_u e_l + f_l - 1 + f_u - 1 + 1 >= f_u f_l g
4392 * f_l e_u + f_u e_l + f_l - 1 + f_u - 1 + 1 - f_u f_l g >= 0
4394 * If the coefficients of f_l e_u + f_u e_l have a common divisor g',
4395 * then the constraint can be scaled down by a factor g',
4396 * with the constant term replaced by
4397 * floor((f_l e_{u,0} + f_u e_{l,0} + f_l - 1 + f_u - 1 + 1 - f_u f_l g)/g').
4398 * Note that the result of applying Fourier-Motzkin to this pair
4401 * f_l e_u + f_u e_l >= 0
4403 * If the constant term of the scaled down version of this constraint,
4404 * i.e., floor((f_l e_{u,0} + f_u e_{l,0})/g') is equal to the constant
4405 * term of the scaled down test constraint, then the test constraint
4406 * is known to hold and no explicit evaluation is required.
4407 * This is essentially the Omega test.
4409 * If the test constraint consists of only a constant term, then
4410 * it is sufficient to look at the sign of this constant term.
4412 static isl_bool
int_between_bounds(__isl_keep isl_basic_map
*bmap
, int i
,
4413 int l
, int u
, struct test_ineq_data
*data
)
4418 offset
= isl_basic_map_offset(bmap
, isl_dim_div
);
4419 n_div
= isl_basic_map_dim(bmap
, isl_dim_div
);
4421 return isl_bool_error
;
4423 isl_int_gcd(data
->g
,
4424 bmap
->ineq
[l
][offset
+ i
], bmap
->ineq
[u
][offset
+ i
]);
4425 isl_int_divexact(data
->fl
, bmap
->ineq
[l
][offset
+ i
], data
->g
);
4426 isl_int_divexact(data
->fu
, bmap
->ineq
[u
][offset
+ i
], data
->g
);
4427 isl_int_neg(data
->fu
, data
->fu
);
4428 isl_seq_combine(data
->v
->el
, data
->fl
, bmap
->ineq
[u
],
4429 data
->fu
, bmap
->ineq
[l
], offset
+ n_div
);
4430 isl_int_mul(data
->g
, data
->g
, data
->fl
);
4431 isl_int_mul(data
->g
, data
->g
, data
->fu
);
4432 isl_int_sub(data
->g
, data
->g
, data
->fl
);
4433 isl_int_sub(data
->g
, data
->g
, data
->fu
);
4434 isl_int_add_ui(data
->g
, data
->g
, 1);
4435 isl_int_sub(data
->fl
, data
->v
->el
[0], data
->g
);
4437 isl_seq_gcd(data
->v
->el
+ 1, offset
- 1 + n_div
, &data
->g
);
4438 if (isl_int_is_zero(data
->g
))
4439 return isl_int_is_nonneg(data
->fl
);
4440 if (isl_int_is_one(data
->g
)) {
4441 isl_int_set(data
->v
->el
[0], data
->fl
);
4442 return test_ineq_is_satisfied(bmap
, data
);
4444 isl_int_fdiv_q(data
->fl
, data
->fl
, data
->g
);
4445 isl_int_fdiv_q(data
->v
->el
[0], data
->v
->el
[0], data
->g
);
4446 if (isl_int_eq(data
->fl
, data
->v
->el
[0]))
4447 return isl_bool_true
;
4448 isl_int_set(data
->v
->el
[0], data
->fl
);
4449 isl_seq_scale_down(data
->v
->el
+ 1, data
->v
->el
+ 1, data
->g
,
4450 offset
- 1 + n_div
);
4452 return test_ineq_is_satisfied(bmap
, data
);
4455 /* Remove more kinds of divs that are not strictly needed.
4456 * In particular, if all pairs of lower and upper bounds on a div
4457 * are such that they allow at least one integer value of the div,
4458 * then we can eliminate the div using Fourier-Motzkin without
4459 * introducing any spurious solutions.
4461 * If at least one of the two constraints has a unit coefficient for the div,
4462 * then the presence of such a value is guaranteed so there is no need to check.
4463 * In particular, the value attained by the bound with unit coefficient
4464 * can serve as this intermediate value.
4466 static __isl_give isl_basic_map
*drop_more_redundant_divs(
4467 __isl_take isl_basic_map
*bmap
, __isl_take
int *pairs
, int n
)
4470 struct test_ineq_data data
= { NULL
, NULL
};
4475 isl_int_init(data
.g
);
4476 isl_int_init(data
.fl
);
4477 isl_int_init(data
.fu
);
4479 n_div
= isl_basic_map_dim(bmap
, isl_dim_div
);
4483 ctx
= isl_basic_map_get_ctx(bmap
);
4484 off
= isl_basic_map_offset(bmap
, isl_dim_div
);
4485 data
.v
= isl_vec_alloc(ctx
, off
+ n_div
);
4494 for (i
= 0; i
< n_div
; ++i
) {
4497 if (best
>= 0 && pairs
[best
] <= pairs
[i
])
4503 for (l
= 0; l
< bmap
->n_ineq
; ++l
) {
4504 if (!isl_int_is_pos(bmap
->ineq
[l
][off
+ i
]))
4506 if (isl_int_is_one(bmap
->ineq
[l
][off
+ i
]))
4508 for (u
= 0; u
< bmap
->n_ineq
; ++u
) {
4509 if (!isl_int_is_neg(bmap
->ineq
[u
][off
+ i
]))
4511 if (isl_int_is_negone(bmap
->ineq
[u
][off
+ i
]))
4513 has_int
= int_between_bounds(bmap
, i
, l
, u
,
4517 if (data
.tab
&& data
.tab
->empty
)
4522 if (u
< bmap
->n_ineq
)
4525 if (data
.tab
&& data
.tab
->empty
) {
4526 bmap
= isl_basic_map_set_to_empty(bmap
);
4529 if (l
== bmap
->n_ineq
) {
4537 test_ineq_data_clear(&data
);
4544 bmap
= isl_basic_map_remove_dims(bmap
, isl_dim_div
, remove
, 1);
4545 return isl_basic_map_drop_redundant_divs(bmap
);
4548 isl_basic_map_free(bmap
);
4549 test_ineq_data_clear(&data
);
4553 /* Given a pair of divs div1 and div2 such that, except for the lower bound l
4554 * and the upper bound u, div1 always occurs together with div2 in the form
4555 * (div1 + m div2), where m is the constant range on the variable div1
4556 * allowed by l and u, replace the pair div1 and div2 by a single
4557 * div that is equal to div1 + m div2.
4559 * The new div will appear in the location that contains div2.
4560 * We need to modify all constraints that contain
4561 * div2 = (div - div1) / m
4562 * The coefficient of div2 is known to be equal to 1 or -1.
4563 * (If a constraint does not contain div2, it will also not contain div1.)
4564 * If the constraint also contains div1, then we know they appear
4565 * as f (div1 + m div2) and we can simply replace (div1 + m div2) by div,
4566 * i.e., the coefficient of div is f.
4568 * Otherwise, we first need to introduce div1 into the constraint.
4577 * A lower bound on div2
4581 * can be replaced by
4583 * m div2 + div1 + m t + f >= 0
4589 * can be replaced by
4591 * -(m div2 + div1) + m t + f' >= 0
4593 * These constraint are those that we would obtain from eliminating
4594 * div1 using Fourier-Motzkin.
4596 * After all constraints have been modified, we drop the lower and upper
4597 * bound and then drop div1.
4598 * Since the new div is only placed in the same location that used
4599 * to store div2, but otherwise has a different meaning, any possible
4600 * explicit representation of the original div2 is removed.
4602 static __isl_give isl_basic_map
*coalesce_divs(__isl_take isl_basic_map
*bmap
,
4603 unsigned div1
, unsigned div2
, unsigned l
, unsigned u
)
4611 ctx
= isl_basic_map_get_ctx(bmap
);
4613 v_div
= isl_basic_map_var_offset(bmap
, isl_dim_div
);
4615 return isl_basic_map_free(bmap
);
4616 total
= 1 + v_div
+ bmap
->n_div
;
4619 isl_int_add(m
, bmap
->ineq
[l
][0], bmap
->ineq
[u
][0]);
4620 isl_int_add_ui(m
, m
, 1);
4622 for (i
= 0; i
< bmap
->n_ineq
; ++i
) {
4623 if (i
== l
|| i
== u
)
4625 if (isl_int_is_zero(bmap
->ineq
[i
][1 + v_div
+ div2
]))
4627 if (isl_int_is_zero(bmap
->ineq
[i
][1 + v_div
+ div1
])) {
4628 if (isl_int_is_pos(bmap
->ineq
[i
][1 + v_div
+ div2
]))
4629 isl_seq_combine(bmap
->ineq
[i
], m
, bmap
->ineq
[i
],
4630 ctx
->one
, bmap
->ineq
[l
], total
);
4632 isl_seq_combine(bmap
->ineq
[i
], m
, bmap
->ineq
[i
],
4633 ctx
->one
, bmap
->ineq
[u
], total
);
4635 isl_int_set(bmap
->ineq
[i
][1 + v_div
+ div2
],
4636 bmap
->ineq
[i
][1 + v_div
+ div1
]);
4637 isl_int_set_si(bmap
->ineq
[i
][1 + v_div
+ div1
], 0);
4642 isl_basic_map_drop_inequality(bmap
, l
);
4643 isl_basic_map_drop_inequality(bmap
, u
);
4645 isl_basic_map_drop_inequality(bmap
, u
);
4646 isl_basic_map_drop_inequality(bmap
, l
);
4648 bmap
= isl_basic_map_mark_div_unknown(bmap
, div2
);
4649 bmap
= isl_basic_map_drop_div(bmap
, div1
);
4653 /* First check if we can coalesce any pair of divs and
4654 * then continue with dropping more redundant divs.
4656 * We loop over all pairs of lower and upper bounds on a div
4657 * with coefficient 1 and -1, respectively, check if there
4658 * is any other div "c" with which we can coalesce the div
4659 * and if so, perform the coalescing.
4661 static __isl_give isl_basic_map
*coalesce_or_drop_more_redundant_divs(
4662 __isl_take isl_basic_map
*bmap
, int *pairs
, int n
)
4668 v_div
= isl_basic_map_var_offset(bmap
, isl_dim_div
);
4669 n_div
= isl_basic_map_dim(bmap
, isl_dim_div
);
4670 if (v_div
< 0 || n_div
< 0)
4671 return isl_basic_map_free(bmap
);
4673 for (i
= 0; i
< n_div
; ++i
) {
4676 for (l
= 0; l
< bmap
->n_ineq
; ++l
) {
4677 if (!isl_int_is_one(bmap
->ineq
[l
][1 + v_div
+ i
]))
4679 for (u
= 0; u
< bmap
->n_ineq
; ++u
) {
4682 if (!isl_int_is_negone(bmap
->ineq
[u
][1+v_div
+i
]))
4684 c
= div_find_coalesce(bmap
, pairs
, i
, l
, u
);
4690 bmap
= coalesce_divs(bmap
, i
, c
, l
, u
);
4691 return isl_basic_map_drop_redundant_divs(bmap
);
4696 if (ISL_F_ISSET(bmap
, ISL_BASIC_MAP_EMPTY
)) {
4701 return drop_more_redundant_divs(bmap
, pairs
, n
);
4704 isl_basic_map_free(bmap
);
4708 /* Are the "n" coefficients starting at "first" of inequality constraints
4709 * "i" and "j" of "bmap" equal to each other?
4711 static int is_parallel_part(__isl_keep isl_basic_map
*bmap
, int i
, int j
,
4714 return isl_seq_eq(bmap
->ineq
[i
] + first
, bmap
->ineq
[j
] + first
, n
);
4717 /* Are inequality constraints "i" and "j" of "bmap" equal to each other,
4718 * apart from the constant term and the coefficient at position "pos"?
4720 static isl_bool
is_parallel_except(__isl_keep isl_basic_map
*bmap
, int i
, int j
,
4725 total
= isl_basic_map_dim(bmap
, isl_dim_all
);
4727 return isl_bool_error
;
4728 return is_parallel_part(bmap
, i
, j
, 1, pos
- 1) &&
4729 is_parallel_part(bmap
, i
, j
, pos
+ 1, total
- pos
);
4732 /* Are inequality constraints "i" and "j" of "bmap" opposite to each other,
4733 * apart from the constant term and the coefficient at position "pos"?
4735 static isl_bool
is_opposite_except(__isl_keep isl_basic_map
*bmap
, int i
, int j
,
4740 total
= isl_basic_map_dim(bmap
, isl_dim_all
);
4742 return isl_bool_error
;
4743 return is_opposite_part(bmap
, i
, j
, 1, pos
- 1) &&
4744 is_opposite_part(bmap
, i
, j
, pos
+ 1, total
- pos
);
4747 /* Restart isl_basic_map_drop_redundant_divs after "bmap" has
4748 * been modified, simplying it if "simplify" is set.
4749 * Free the temporary data structure "pairs" that was associated
4750 * to the old version of "bmap".
4752 static __isl_give isl_basic_map
*drop_redundant_divs_again(
4753 __isl_take isl_basic_map
*bmap
, __isl_take
int *pairs
, int simplify
)
4756 bmap
= isl_basic_map_simplify(bmap
);
4758 return isl_basic_map_drop_redundant_divs(bmap
);
4761 /* Is "div" the single unknown existentially quantified variable
4762 * in inequality constraint "ineq" of "bmap"?
4763 * "div" is known to have a non-zero coefficient in "ineq".
4765 static isl_bool
single_unknown(__isl_keep isl_basic_map
*bmap
, int ineq
,
4773 known
= isl_basic_map_div_is_known(bmap
, div
);
4774 if (known
< 0 || known
)
4775 return isl_bool_not(known
);
4776 n_div
= isl_basic_map_dim(bmap
, isl_dim_div
);
4778 return isl_bool_error
;
4780 return isl_bool_true
;
4781 o_div
= isl_basic_map_offset(bmap
, isl_dim_div
);
4782 for (i
= 0; i
< n_div
; ++i
) {
4787 if (isl_int_is_zero(bmap
->ineq
[ineq
][o_div
+ i
]))
4789 known
= isl_basic_map_div_is_known(bmap
, i
);
4790 if (known
< 0 || !known
)
4794 return isl_bool_true
;
4797 /* Does integer division "div" have coefficient 1 in inequality constraint
4800 static isl_bool
has_coef_one(__isl_keep isl_basic_map
*bmap
, int div
, int ineq
)
4804 o_div
= isl_basic_map_offset(bmap
, isl_dim_div
);
4805 if (isl_int_is_one(bmap
->ineq
[ineq
][o_div
+ div
]))
4806 return isl_bool_true
;
4808 return isl_bool_false
;
4811 /* Turn inequality constraint "ineq" of "bmap" into an equality and
4812 * then try and drop redundant divs again,
4813 * freeing the temporary data structure "pairs" that was associated
4814 * to the old version of "bmap".
4816 static __isl_give isl_basic_map
*set_eq_and_try_again(
4817 __isl_take isl_basic_map
*bmap
, int ineq
, __isl_take
int *pairs
)
4819 bmap
= isl_basic_map_cow(bmap
);
4820 isl_basic_map_inequality_to_equality(bmap
, ineq
);
4821 return drop_redundant_divs_again(bmap
, pairs
, 1);
4824 /* Drop the integer division at position "div", along with the two
4825 * inequality constraints "ineq1" and "ineq2" in which it appears
4826 * from "bmap" and then try and drop redundant divs again,
4827 * freeing the temporary data structure "pairs" that was associated
4828 * to the old version of "bmap".
4830 static __isl_give isl_basic_map
*drop_div_and_try_again(
4831 __isl_take isl_basic_map
*bmap
, int div
, int ineq1
, int ineq2
,
4832 __isl_take
int *pairs
)
4834 if (ineq1
> ineq2
) {
4835 isl_basic_map_drop_inequality(bmap
, ineq1
);
4836 isl_basic_map_drop_inequality(bmap
, ineq2
);
4838 isl_basic_map_drop_inequality(bmap
, ineq2
);
4839 isl_basic_map_drop_inequality(bmap
, ineq1
);
4841 bmap
= isl_basic_map_drop_div(bmap
, div
);
4842 return drop_redundant_divs_again(bmap
, pairs
, 0);
4845 /* Given two inequality constraints
4847 * f(x) + n d + c >= 0, (ineq)
4849 * with d the variable at position "pos", and
4851 * f(x) + c0 >= 0, (lower)
4853 * compute the maximal value of the lower bound ceil((-f(x) - c)/n)
4854 * determined by the first constraint.
4861 static void lower_bound_from_parallel(__isl_keep isl_basic_map
*bmap
,
4862 int ineq
, int lower
, int pos
, isl_int
*l
)
4864 isl_int_neg(*l
, bmap
->ineq
[ineq
][0]);
4865 isl_int_add(*l
, *l
, bmap
->ineq
[lower
][0]);
4866 isl_int_cdiv_q(*l
, *l
, bmap
->ineq
[ineq
][pos
]);
4869 /* Given two inequality constraints
4871 * f(x) + n d + c >= 0, (ineq)
4873 * with d the variable at position "pos", and
4875 * -f(x) - c0 >= 0, (upper)
4877 * compute the minimal value of the lower bound ceil((-f(x) - c)/n)
4878 * determined by the first constraint.
4885 static void lower_bound_from_opposite(__isl_keep isl_basic_map
*bmap
,
4886 int ineq
, int upper
, int pos
, isl_int
*u
)
4888 isl_int_neg(*u
, bmap
->ineq
[ineq
][0]);
4889 isl_int_sub(*u
, *u
, bmap
->ineq
[upper
][0]);
4890 isl_int_cdiv_q(*u
, *u
, bmap
->ineq
[ineq
][pos
]);
4893 /* Given a lower bound constraint "ineq" on "div" in "bmap",
4894 * does the corresponding lower bound have a fixed value in "bmap"?
4896 * In particular, "ineq" is of the form
4898 * f(x) + n d + c >= 0
4900 * with n > 0, c the constant term and
4901 * d the existentially quantified variable "div".
4902 * That is, the lower bound is
4904 * ceil((-f(x) - c)/n)
4906 * Look for a pair of constraints
4911 * i.e., -c1 <= -f(x) <= c0, that fix ceil((-f(x) - c)/n) to a constant value.
4912 * That is, check that
4914 * ceil((-c1 - c)/n) = ceil((c0 - c)/n)
4916 * If so, return the index of inequality f(x) + c0 >= 0.
4917 * Otherwise, return bmap->n_ineq.
4918 * Return -1 on error.
4920 static int lower_bound_is_cst(__isl_keep isl_basic_map
*bmap
, int div
, int ineq
)
4923 int lower
= -1, upper
= -1;
4928 o_div
= isl_basic_map_offset(bmap
, isl_dim_div
);
4929 for (i
= 0; i
< bmap
->n_ineq
&& (lower
< 0 || upper
< 0); ++i
) {
4934 if (!isl_int_is_zero(bmap
->ineq
[i
][o_div
+ div
]))
4936 par
= isl_bool_false
;
4938 par
= is_parallel_except(bmap
, ineq
, i
, o_div
+ div
);
4945 opp
= isl_bool_false
;
4947 opp
= is_opposite_except(bmap
, ineq
, i
, o_div
+ div
);
4954 if (lower
< 0 || upper
< 0)
4955 return bmap
->n_ineq
;
4960 lower_bound_from_parallel(bmap
, ineq
, lower
, o_div
+ div
, &l
);
4961 lower_bound_from_opposite(bmap
, ineq
, upper
, o_div
+ div
, &u
);
4963 equal
= isl_int_eq(l
, u
);
4968 return equal
? lower
: bmap
->n_ineq
;
4971 /* Given a lower bound constraint "ineq" on the existentially quantified
4972 * variable "div", such that the corresponding lower bound has
4973 * a fixed value in "bmap", assign this fixed value to the variable and
4974 * then try and drop redundant divs again,
4975 * freeing the temporary data structure "pairs" that was associated
4976 * to the old version of "bmap".
4977 * "lower" determines the constant value for the lower bound.
4979 * In particular, "ineq" is of the form
4981 * f(x) + n d + c >= 0,
4983 * while "lower" is of the form
4987 * The lower bound is ceil((-f(x) - c)/n) and its constant value
4988 * is ceil((c0 - c)/n).
4990 static __isl_give isl_basic_map
*fix_cst_lower(__isl_take isl_basic_map
*bmap
,
4991 int div
, int ineq
, int lower
, int *pairs
)
4998 o_div
= isl_basic_map_offset(bmap
, isl_dim_div
);
4999 lower_bound_from_parallel(bmap
, ineq
, lower
, o_div
+ div
, &c
);
5000 bmap
= isl_basic_map_fix(bmap
, isl_dim_div
, div
, c
);
5005 return isl_basic_map_drop_redundant_divs(bmap
);
5008 /* Do any of the integer divisions of "bmap" involve integer division "div"?
5010 * The integer division "div" could only ever appear in any later
5011 * integer division (with an explicit representation).
5013 static isl_bool
any_div_involves_div(__isl_keep isl_basic_map
*bmap
, int div
)
5016 isl_size v_div
, n_div
;
5018 v_div
= isl_basic_map_var_offset(bmap
, isl_dim_div
);
5019 n_div
= isl_basic_map_dim(bmap
, isl_dim_div
);
5020 if (v_div
< 0 || n_div
< 0)
5021 return isl_bool_error
;
5023 for (i
= div
+ 1; i
< n_div
; ++i
) {
5026 unknown
= isl_basic_map_div_is_marked_unknown(bmap
, i
);
5028 return isl_bool_error
;
5031 if (!isl_int_is_zero(bmap
->div
[i
][1 + 1 + v_div
+ div
]))
5032 return isl_bool_true
;
5035 return isl_bool_false
;
5038 /* Remove divs that are not strictly needed based on the inequality
5040 * In particular, if a div only occurs positively (or negatively)
5041 * in constraints, then it can simply be dropped.
5042 * Also, if a div occurs in only two constraints and if moreover
5043 * those two constraints are opposite to each other, except for the constant
5044 * term and if the sum of the constant terms is such that for any value
5045 * of the other values, there is always at least one integer value of the
5046 * div, i.e., if one plus this sum is greater than or equal to
5047 * the (absolute value) of the coefficient of the div in the constraints,
5048 * then we can also simply drop the div.
5050 * If an existentially quantified variable does not have an explicit
5051 * representation, appears in only a single lower bound that does not
5052 * involve any other such existentially quantified variables and appears
5053 * in this lower bound with coefficient 1,
5054 * then fix the variable to the value of the lower bound. That is,
5055 * turn the inequality into an equality.
5056 * If for any value of the other variables, there is any value
5057 * for the existentially quantified variable satisfying the constraints,
5058 * then this lower bound also satisfies the constraints.
5059 * It is therefore safe to pick this lower bound.
5061 * The same reasoning holds even if the coefficient is not one.
5062 * However, fixing the variable to the value of the lower bound may
5063 * in general introduce an extra integer division, in which case
5064 * it may be better to pick another value.
5065 * If this integer division has a known constant value, then plugging
5066 * in this constant value removes the existentially quantified variable
5067 * completely. In particular, if the lower bound is of the form
5068 * ceil((-f(x) - c)/n) and there are two constraints, f(x) + c0 >= 0 and
5069 * -f(x) + c1 >= 0 such that ceil((-c1 - c)/n) = ceil((c0 - c)/n),
5070 * then the existentially quantified variable can be assigned this
5073 * We skip divs that appear in equalities or in the definition of other divs.
5074 * Divs that appear in the definition of other divs usually occur in at least
5075 * 4 constraints, but the constraints may have been simplified.
5077 * If any divs are left after these simple checks then we move on
5078 * to more complicated cases in drop_more_redundant_divs.
5080 static __isl_give isl_basic_map
*isl_basic_map_drop_redundant_divs_ineq(
5081 __isl_take isl_basic_map
*bmap
)
5091 if (bmap
->n_div
== 0)
5094 off
= isl_basic_map_var_offset(bmap
, isl_dim_div
);
5096 return isl_basic_map_free(bmap
);
5097 pairs
= isl_calloc_array(bmap
->ctx
, int, bmap
->n_div
);
5101 n_ineq
= isl_basic_map_n_inequality(bmap
);
5104 for (i
= 0; i
< bmap
->n_div
; ++i
) {
5106 int last_pos
, last_neg
;
5109 isl_bool involves
, opp
, set_div
;
5111 defined
= !isl_int_is_zero(bmap
->div
[i
][0]);
5112 involves
= any_div_involves_div(bmap
, i
);
5117 for (j
= 0; j
< bmap
->n_eq
; ++j
)
5118 if (!isl_int_is_zero(bmap
->eq
[j
][1 + off
+ i
]))
5124 for (j
= 0; j
< bmap
->n_ineq
; ++j
) {
5125 if (isl_int_is_pos(bmap
->ineq
[j
][1 + off
+ i
])) {
5129 if (isl_int_is_neg(bmap
->ineq
[j
][1 + off
+ i
])) {
5134 pairs
[i
] = pos
* neg
;
5135 if (pairs
[i
] == 0) {
5136 for (j
= bmap
->n_ineq
- 1; j
>= 0; --j
)
5137 if (!isl_int_is_zero(bmap
->ineq
[j
][1+off
+i
]))
5138 isl_basic_map_drop_inequality(bmap
, j
);
5139 bmap
= isl_basic_map_drop_div(bmap
, i
);
5140 return drop_redundant_divs_again(bmap
, pairs
, 0);
5143 opp
= isl_bool_false
;
5145 opp
= is_opposite(bmap
, last_pos
, last_neg
);
5150 isl_bool single
, one
;
5154 single
= single_unknown(bmap
, last_pos
, i
);
5159 one
= has_coef_one(bmap
, i
, last_pos
);
5163 return set_eq_and_try_again(bmap
, last_pos
,
5165 lower
= lower_bound_is_cst(bmap
, i
, last_pos
);
5169 return fix_cst_lower(bmap
, i
, last_pos
, lower
,
5174 isl_int_add(bmap
->ineq
[last_pos
][0],
5175 bmap
->ineq
[last_pos
][0], bmap
->ineq
[last_neg
][0]);
5176 isl_int_add_ui(bmap
->ineq
[last_pos
][0],
5177 bmap
->ineq
[last_pos
][0], 1);
5178 redundant
= isl_int_ge(bmap
->ineq
[last_pos
][0],
5179 bmap
->ineq
[last_pos
][1+off
+i
]);
5180 isl_int_sub_ui(bmap
->ineq
[last_pos
][0],
5181 bmap
->ineq
[last_pos
][0], 1);
5182 isl_int_sub(bmap
->ineq
[last_pos
][0],
5183 bmap
->ineq
[last_pos
][0], bmap
->ineq
[last_neg
][0]);
5185 return drop_div_and_try_again(bmap
, i
,
5186 last_pos
, last_neg
, pairs
);
5188 set_div
= isl_bool_false
;
5190 set_div
= ok_to_set_div_from_bound(bmap
, i
, last_pos
);
5192 return isl_basic_map_free(bmap
);
5194 bmap
= set_div_from_lower_bound(bmap
, i
, last_pos
);
5195 return drop_redundant_divs_again(bmap
, pairs
, 1);
5202 return coalesce_or_drop_more_redundant_divs(bmap
, pairs
, n
);
5208 isl_basic_map_free(bmap
);
5212 /* Consider the coefficients at "c" as a row vector and replace
5213 * them with their product with "T". "T" is assumed to be a square matrix.
5215 static isl_stat
preimage(isl_int
*c
, __isl_keep isl_mat
*T
)
5221 n
= isl_mat_rows(T
);
5223 return isl_stat_error
;
5224 if (isl_seq_first_non_zero(c
, n
) == -1)
5226 ctx
= isl_mat_get_ctx(T
);
5227 v
= isl_vec_alloc(ctx
, n
);
5229 return isl_stat_error
;
5230 isl_seq_swp_or_cpy(v
->el
, c
, n
);
5231 v
= isl_vec_mat_product(v
, isl_mat_copy(T
));
5233 return isl_stat_error
;
5234 isl_seq_swp_or_cpy(c
, v
->el
, n
);
5240 /* Plug in T for the variables in "bmap" starting at "pos".
5241 * T is a linear unimodular matrix, i.e., without constant term.
5243 static __isl_give isl_basic_map
*isl_basic_map_preimage_vars(
5244 __isl_take isl_basic_map
*bmap
, unsigned pos
, __isl_take isl_mat
*T
)
5247 isl_size n_row
, n_col
;
5249 bmap
= isl_basic_map_cow(bmap
);
5250 n_row
= isl_mat_rows(T
);
5251 n_col
= isl_mat_cols(T
);
5252 if (!bmap
|| n_row
< 0 || n_col
< 0)
5256 isl_die(isl_mat_get_ctx(T
), isl_error_invalid
,
5257 "expecting square matrix", goto error
);
5259 if (isl_basic_map_check_range(bmap
, isl_dim_all
, pos
, n_col
) < 0)
5262 for (i
= 0; i
< bmap
->n_eq
; ++i
)
5263 if (preimage(bmap
->eq
[i
] + 1 + pos
, T
) < 0)
5265 for (i
= 0; i
< bmap
->n_ineq
; ++i
)
5266 if (preimage(bmap
->ineq
[i
] + 1 + pos
, T
) < 0)
5268 for (i
= 0; i
< bmap
->n_div
; ++i
) {
5269 if (isl_basic_map_div_is_marked_unknown(bmap
, i
))
5271 if (preimage(bmap
->div
[i
] + 1 + 1 + pos
, T
) < 0)
5278 isl_basic_map_free(bmap
);
5283 /* Remove divs that are not strictly needed.
5285 * First look for an equality constraint involving two or more
5286 * existentially quantified variables without an explicit
5287 * representation. Replace the combination that appears
5288 * in the equality constraint by a single existentially quantified
5289 * variable such that the equality can be used to derive
5290 * an explicit representation for the variable.
5291 * If there are no more such equality constraints, then continue
5292 * with isl_basic_map_drop_redundant_divs_ineq.
5294 * In particular, if the equality constraint is of the form
5296 * f(x) + \sum_i c_i a_i = 0
5298 * with a_i existentially quantified variable without explicit
5299 * representation, then apply a transformation on the existentially
5300 * quantified variables to turn the constraint into
5304 * with g the gcd of the c_i.
5305 * In order to easily identify which existentially quantified variables
5306 * have a complete explicit representation, i.e., without being defined
5307 * in terms of other existentially quantified variables without
5308 * an explicit representation, the existentially quantified variables
5311 * The variable transformation is computed by extending the row
5312 * [c_1/g ... c_n/g] to a unimodular matrix, obtaining the transformation
5314 * [a_1'] [c_1/g ... c_n/g] [ a_1 ]
5319 * with [c_1/g ... c_n/g] representing the first row of U.
5320 * The inverse of U is then plugged into the original constraints.
5321 * The call to isl_basic_map_simplify makes sure the explicit
5322 * representation for a_1' is extracted from the equality constraint.
5324 __isl_give isl_basic_map
*isl_basic_map_drop_redundant_divs(
5325 __isl_take isl_basic_map
*bmap
)
5337 if (isl_basic_map_divs_known(bmap
))
5338 return isl_basic_map_drop_redundant_divs_ineq(bmap
);
5339 if (bmap
->n_eq
== 0)
5340 return isl_basic_map_drop_redundant_divs_ineq(bmap
);
5341 bmap
= isl_basic_map_sort_divs(bmap
);
5345 first
= isl_basic_map_first_unknown_div(bmap
);
5347 return isl_basic_map_free(bmap
);
5349 o_div
= isl_basic_map_offset(bmap
, isl_dim_div
);
5350 n_div
= isl_basic_map_dim(bmap
, isl_dim_div
);
5352 return isl_basic_map_free(bmap
);
5354 for (i
= 0; i
< bmap
->n_eq
; ++i
) {
5355 l
= isl_seq_first_non_zero(bmap
->eq
[i
] + o_div
+ first
,
5360 if (isl_seq_first_non_zero(bmap
->eq
[i
] + o_div
+ l
+ 1,
5361 n_div
- (l
+ 1)) == -1)
5365 if (i
>= bmap
->n_eq
)
5366 return isl_basic_map_drop_redundant_divs_ineq(bmap
);
5368 ctx
= isl_basic_map_get_ctx(bmap
);
5369 T
= isl_mat_alloc(ctx
, n_div
- l
, n_div
- l
);
5371 return isl_basic_map_free(bmap
);
5372 isl_seq_cpy(T
->row
[0], bmap
->eq
[i
] + o_div
+ l
, n_div
- l
);
5373 T
= isl_mat_normalize_row(T
, 0);
5374 T
= isl_mat_unimodular_complete(T
, 1);
5375 T
= isl_mat_right_inverse(T
);
5377 for (i
= l
; i
< n_div
; ++i
)
5378 bmap
= isl_basic_map_mark_div_unknown(bmap
, i
);
5379 bmap
= isl_basic_map_preimage_vars(bmap
, o_div
- 1 + l
, T
);
5380 bmap
= isl_basic_map_simplify(bmap
);
5382 return isl_basic_map_drop_redundant_divs(bmap
);
5385 /* Does "bmap" satisfy any equality that involves more than 2 variables
5386 * and/or has coefficients different from -1 and 1?
5388 static isl_bool
has_multiple_var_equality(__isl_keep isl_basic_map
*bmap
)
5393 total
= isl_basic_map_dim(bmap
, isl_dim_all
);
5395 return isl_bool_error
;
5397 for (i
= 0; i
< bmap
->n_eq
; ++i
) {
5400 j
= isl_seq_first_non_zero(bmap
->eq
[i
] + 1, total
);
5403 if (!isl_int_is_one(bmap
->eq
[i
][1 + j
]) &&
5404 !isl_int_is_negone(bmap
->eq
[i
][1 + j
]))
5405 return isl_bool_true
;
5408 k
= isl_seq_first_non_zero(bmap
->eq
[i
] + 1 + j
, total
- j
);
5412 if (!isl_int_is_one(bmap
->eq
[i
][1 + j
]) &&
5413 !isl_int_is_negone(bmap
->eq
[i
][1 + j
]))
5414 return isl_bool_true
;
5417 k
= isl_seq_first_non_zero(bmap
->eq
[i
] + 1 + j
, total
- j
);
5419 return isl_bool_true
;
5422 return isl_bool_false
;
5425 /* Remove any common factor g from the constraint coefficients in "v".
5426 * The constant term is stored in the first position and is replaced
5427 * by floor(c/g). If any common factor is removed and if this results
5428 * in a tightening of the constraint, then set *tightened.
5430 static __isl_give isl_vec
*normalize_constraint(__isl_take isl_vec
*v
,
5437 ctx
= isl_vec_get_ctx(v
);
5438 isl_seq_gcd(v
->el
+ 1, v
->size
- 1, &ctx
->normalize_gcd
);
5439 if (isl_int_is_zero(ctx
->normalize_gcd
))
5441 if (isl_int_is_one(ctx
->normalize_gcd
))
5446 if (tightened
&& !isl_int_is_divisible_by(v
->el
[0], ctx
->normalize_gcd
))
5448 isl_int_fdiv_q(v
->el
[0], v
->el
[0], ctx
->normalize_gcd
);
5449 isl_seq_scale_down(v
->el
+ 1, v
->el
+ 1, ctx
->normalize_gcd
,
5454 /* Internal representation used by isl_basic_map_reduce_coefficients.
5456 * "total" is the total dimensionality of the original basic map.
5457 * "v" is a temporary vector of size 1 + total that can be used
5458 * to store constraint coefficients.
5459 * "T" is the variable compression.
5460 * "T2" is the inverse transformation.
5461 * "tightened" is set if any constant term got tightened
5462 * while reducing the coefficients.
5464 struct isl_reduce_coefficients_data
{
5472 /* Free all memory allocated in "data".
5474 static void isl_reduce_coefficients_data_clear(
5475 struct isl_reduce_coefficients_data
*data
)
5477 data
->T
= isl_mat_free(data
->T
);
5478 data
->T2
= isl_mat_free(data
->T2
);
5479 data
->v
= isl_vec_free(data
->v
);
5482 /* Initialize "data" for "bmap", freeing all allocated memory
5483 * if anything goes wrong.
5485 * In particular, construct a variable compression
5486 * from the equality constraints of "bmap" and
5487 * allocate a temporary vector.
5489 static isl_stat
isl_reduce_coefficients_data_init(
5490 __isl_keep isl_basic_map
*bmap
,
5491 struct isl_reduce_coefficients_data
*data
)
5499 data
->tightened
= 0;
5501 data
->total
= isl_basic_map_dim(bmap
, isl_dim_all
);
5502 if (data
->total
< 0)
5503 return isl_stat_error
;
5504 ctx
= isl_basic_map_get_ctx(bmap
);
5505 data
->v
= isl_vec_alloc(ctx
, 1 + data
->total
);
5507 return isl_stat_error
;
5509 eq
= isl_mat_sub_alloc6(ctx
, bmap
->eq
, 0, bmap
->n_eq
,
5510 0, 1 + data
->total
);
5511 data
->T
= isl_mat_variable_compression(eq
, &data
->T2
);
5512 if (!data
->T
|| !data
->T2
)
5517 isl_reduce_coefficients_data_clear(data
);
5518 return isl_stat_error
;
5521 /* Reduce the coefficients of "bmap" by applying the variable compression
5523 * In particular, apply the variable compression to each constraint,
5524 * factor out any common factor in the non-constant coefficients and
5525 * then apply the inverse of the compression.
5527 * Only apply the reduction on a single copy of the basic map
5528 * since the reduction may leave the result in an inconsistent state.
5529 * In particular, the constraints may not be gaussed.
5531 static __isl_give isl_basic_map
*reduce_coefficients(
5532 __isl_take isl_basic_map
*bmap
,
5533 struct isl_reduce_coefficients_data
*data
)
5538 total
= isl_basic_map_dim(bmap
, isl_dim_all
);
5540 return isl_basic_map_free(bmap
);
5541 if (total
!= data
->total
)
5542 isl_die(isl_basic_map_get_ctx(bmap
), isl_error_internal
,
5543 "total dimensionality changed unexpectedly",
5544 return isl_basic_map_free(bmap
));
5546 bmap
= isl_basic_map_cow(bmap
);
5550 for (i
= 0; i
< bmap
->n_ineq
; ++i
) {
5551 isl_seq_cpy(data
->v
->el
, bmap
->ineq
[i
], 1 + data
->total
);
5552 data
->v
= isl_vec_mat_product(data
->v
, isl_mat_copy(data
->T
));
5553 data
->v
= normalize_constraint(data
->v
, &data
->tightened
);
5554 data
->v
= isl_vec_mat_product(data
->v
, isl_mat_copy(data
->T2
));
5556 return isl_basic_map_free(bmap
);
5557 isl_seq_cpy(bmap
->ineq
[i
], data
->v
->el
, 1 + data
->total
);
5560 ISL_F_SET(bmap
, ISL_BASIC_MAP_REDUCED_COEFFICIENTS
);
5565 /* If "bmap" is an integer set that satisfies any equality involving
5566 * more than 2 variables and/or has coefficients different from -1 and 1,
5567 * then use variable compression to reduce the coefficients by removing
5568 * any (hidden) common factor.
5569 * In particular, apply the variable compression to each constraint,
5570 * factor out any common factor in the non-constant coefficients and
5571 * then apply the inverse of the compression.
5572 * At the end, we mark the basic map as having reduced constants.
5573 * If this flag is still set on the next invocation of this function,
5574 * then we skip the computation.
5576 * Removing a common factor may result in a tightening of some of
5577 * the constraints. If this happens, then we may end up with two
5578 * opposite inequalities that can be replaced by an equality.
5579 * We therefore call isl_basic_map_detect_inequality_pairs,
5580 * which checks for such pairs of inequalities as well as eliminate_divs_eq
5581 * and isl_basic_map_gauss if such a pair was found.
5582 * This call to isl_basic_map_gauss may undo much of the effect
5583 * of the reduction on which isl_map_coalesce depends.
5584 * In particular, constraints in terms of (compressed) local variables
5585 * get reformulated in terms of the set variables again.
5586 * The reduction is therefore applied again afterwards.
5587 * This has to be done before the call to eliminate_divs_eq, however,
5588 * since that may remove some local variables, while
5589 * the data used during the reduction is formulated in terms
5590 * of the original variables.
5592 * Tightening may also result in some other constraints becoming
5593 * (rationally) redundant with respect to the tightened constraint
5594 * (in combination with other constraints). The basic map may
5595 * therefore no longer be assumed to have no redundant constraints.
5597 * Note that this function may leave the result in an inconsistent state.
5598 * In particular, the constraints may not be gaussed.
5599 * Unfortunately, isl_map_coalesce actually depends on this inconsistent state
5600 * for some of the test cases to pass successfully.
5601 * Any potential modification of the representation is therefore only
5602 * performed on a single copy of the basic map.
5604 __isl_give isl_basic_map
*isl_basic_map_reduce_coefficients(
5605 __isl_take isl_basic_map
*bmap
)
5607 struct isl_reduce_coefficients_data data
;
5612 if (ISL_F_ISSET(bmap
, ISL_BASIC_MAP_REDUCED_COEFFICIENTS
))
5614 if (isl_basic_map_is_rational(bmap
))
5616 if (bmap
->n_eq
== 0)
5618 multi
= has_multiple_var_equality(bmap
);
5620 return isl_basic_map_free(bmap
);
5624 if (isl_reduce_coefficients_data_init(bmap
, &data
) < 0)
5625 return isl_basic_map_free(bmap
);
5627 if (data
.T
->n_col
== 0) {
5628 isl_reduce_coefficients_data_clear(&data
);
5629 return isl_basic_map_set_to_empty(bmap
);
5632 bmap
= reduce_coefficients(bmap
, &data
);
5636 if (data
.tightened
) {
5639 ISL_F_CLR(bmap
, ISL_BASIC_MAP_NO_REDUNDANT
);
5640 bmap
= isl_basic_map_detect_inequality_pairs(bmap
, &progress
);
5642 bmap
= isl_basic_map_gauss(bmap
, NULL
);
5643 bmap
= reduce_coefficients(bmap
, &data
);
5644 bmap
= eliminate_divs_eq(bmap
, &progress
);
5648 isl_reduce_coefficients_data_clear(&data
);
5652 isl_reduce_coefficients_data_clear(&data
);
5653 return isl_basic_map_free(bmap
);
5656 /* Shift the integer division at position "div" of "bmap"
5657 * by "shift" times the variable at position "pos".
5658 * "pos" is as determined by isl_basic_map_offset, i.e., pos == 0
5659 * corresponds to the constant term.
5661 * That is, if the integer division has the form
5665 * then replace it by
5667 * floor((f(x) + shift * d * x_pos)/d) - shift * x_pos
5669 __isl_give isl_basic_map
*isl_basic_map_shift_div(
5670 __isl_take isl_basic_map
*bmap
, int div
, int pos
, isl_int shift
)
5673 isl_size total
, n_div
;
5675 if (isl_int_is_zero(shift
))
5677 total
= isl_basic_map_dim(bmap
, isl_dim_all
);
5678 n_div
= isl_basic_map_dim(bmap
, isl_dim_div
);
5680 if (total
< 0 || n_div
< 0)
5681 return isl_basic_map_free(bmap
);
5683 isl_int_addmul(bmap
->div
[div
][1 + pos
], shift
, bmap
->div
[div
][0]);
5685 for (i
= 0; i
< bmap
->n_eq
; ++i
) {
5686 if (isl_int_is_zero(bmap
->eq
[i
][1 + total
+ div
]))
5688 isl_int_submul(bmap
->eq
[i
][pos
],
5689 shift
, bmap
->eq
[i
][1 + total
+ div
]);
5691 for (i
= 0; i
< bmap
->n_ineq
; ++i
) {
5692 if (isl_int_is_zero(bmap
->ineq
[i
][1 + total
+ div
]))
5694 isl_int_submul(bmap
->ineq
[i
][pos
],
5695 shift
, bmap
->ineq
[i
][1 + total
+ div
]);
5697 for (i
= 0; i
< bmap
->n_div
; ++i
) {
5698 if (isl_int_is_zero(bmap
->div
[i
][0]))
5700 if (isl_int_is_zero(bmap
->div
[i
][1 + 1 + total
+ div
]))
5702 isl_int_submul(bmap
->div
[i
][1 + pos
],
5703 shift
, bmap
->div
[i
][1 + 1 + total
+ div
]);