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
6 * Copyright 2023 Cerebras Systems
8 * Use of this software is governed by the MIT license
10 * Written by Sven Verdoolaege, K.U.Leuven, Departement
11 * Computerwetenschappen, Celestijnenlaan 200A, B-3001 Leuven, Belgium
12 * and Ecole Normale Superieure, 45 rue d’Ulm, 75230 Paris, France
13 * and Inria Paris - Rocquencourt, Domaine de Voluceau - Rocquencourt,
14 * B.P. 105 - 78153 Le Chesnay, France
15 * and Cerebras Systems, 1237 E Arques Ave, Sunnyvale, CA, USA
18 #include <isl_ctx_private.h>
19 #include <isl_map_private.h>
20 #include "isl_equalities.h"
24 #include <isl_space_private.h>
25 #include <isl_mat_private.h>
26 #include <isl_vec_private.h>
28 #include <bset_to_bmap.c>
29 #include <bset_from_bmap.c>
30 #include <set_to_map.c>
31 #include <set_from_map.c>
33 /* Mark "bmap" as having one or more inequality constraints modified.
34 * If "equivalent" is set, then this modification was done based
35 * on an equality constraint already available in "bmap".
37 * Any modification may result in the constraints no longer being sorted and
38 * may also undo the effect of reduce_coefficients.
40 * A modification that uses extra information may also result
41 * in the modified constraint(s) becoming redundant or
42 * turning into an implicit equality constraint.
44 static __isl_give isl_basic_map
*isl_basic_map_modify_inequality(
45 __isl_take isl_basic_map
*bmap
, int equivalent
)
49 ISL_F_CLR(bmap
, ISL_BASIC_MAP_SORTED
);
50 ISL_F_CLR(bmap
, ISL_BASIC_MAP_REDUCED_COEFFICIENTS
);
53 ISL_F_CLR(bmap
, ISL_BASIC_MAP_NO_REDUNDANT
);
54 ISL_F_CLR(bmap
, ISL_BASIC_MAP_NO_IMPLICIT
);
58 static void swap_equality(__isl_keep isl_basic_map
*bmap
, int a
, int b
)
60 isl_int
*t
= bmap
->eq
[a
];
61 bmap
->eq
[a
] = bmap
->eq
[b
];
65 static void swap_inequality(__isl_keep isl_basic_map
*bmap
, int a
, int b
)
68 isl_int
*t
= bmap
->ineq
[a
];
69 bmap
->ineq
[a
] = bmap
->ineq
[b
];
74 /* Scale down the inequality constraint "ineq" of length "len"
76 * All the coefficients, except the constant term,
77 * are assumed to be multiples of "f".
79 * If the factor is 0 or 1, then no scaling needs to be performed.
81 * If scaling is performed then take into account that the constraint
82 * is modified (not simply based on an equality constraint).
84 static __isl_give isl_basic_map
*scale_down_inequality(
85 __isl_take isl_basic_map
*bmap
, int ineq
, isl_int f
, unsigned len
)
90 if (isl_int_is_zero(f
) || isl_int_is_one(f
))
93 isl_int_fdiv_q(bmap
->ineq
[ineq
][0], bmap
->ineq
[ineq
][0], f
);
94 isl_seq_scale_down(bmap
->ineq
[ineq
] + 1, bmap
->ineq
[ineq
] + 1, f
, len
);
96 bmap
= isl_basic_map_modify_inequality(bmap
, 0);
101 __isl_give isl_basic_map
*isl_basic_map_normalize_constraints(
102 __isl_take isl_basic_map
*bmap
)
106 isl_size total
= isl_basic_map_dim(bmap
, isl_dim_all
);
109 return isl_basic_map_free(bmap
);
112 for (i
= bmap
->n_eq
- 1; i
>= 0; --i
) {
113 isl_seq_gcd(bmap
->eq
[i
]+1, total
, &gcd
);
114 if (isl_int_is_zero(gcd
)) {
115 if (!isl_int_is_zero(bmap
->eq
[i
][0])) {
116 bmap
= isl_basic_map_set_to_empty(bmap
);
119 if (isl_basic_map_drop_equality(bmap
, i
) < 0)
123 if (ISL_F_ISSET(bmap
, ISL_BASIC_MAP_RATIONAL
))
124 isl_int_gcd(gcd
, gcd
, bmap
->eq
[i
][0]);
125 if (isl_int_is_one(gcd
))
127 if (!isl_int_is_divisible_by(bmap
->eq
[i
][0], gcd
)) {
128 bmap
= isl_basic_map_set_to_empty(bmap
);
131 isl_seq_scale_down(bmap
->eq
[i
], bmap
->eq
[i
], gcd
, 1+total
);
134 for (i
= bmap
->n_ineq
- 1; i
>= 0; --i
) {
135 isl_seq_gcd(bmap
->ineq
[i
]+1, total
, &gcd
);
136 if (isl_int_is_zero(gcd
)) {
137 if (isl_int_is_neg(bmap
->ineq
[i
][0])) {
138 bmap
= isl_basic_map_set_to_empty(bmap
);
141 if (isl_basic_map_drop_inequality(bmap
, i
) < 0)
145 if (ISL_F_ISSET(bmap
, ISL_BASIC_MAP_RATIONAL
))
146 isl_int_gcd(gcd
, gcd
, bmap
->ineq
[i
][0]);
147 bmap
= scale_down_inequality(bmap
, i
, gcd
, total
);
156 isl_basic_map_free(bmap
);
160 __isl_give isl_basic_set
*isl_basic_set_normalize_constraints(
161 __isl_take isl_basic_set
*bset
)
163 isl_basic_map
*bmap
= bset_to_bmap(bset
);
164 return bset_from_bmap(isl_basic_map_normalize_constraints(bmap
));
167 /* Reduce the coefficient of the variable at position "pos"
168 * in integer division "div", such that it lies in the half-open
169 * interval (1/2,1/2], extracting any excess value from this integer division.
170 * "pos" is as determined by isl_basic_map_offset, i.e., pos == 0
171 * corresponds to the constant term.
173 * That is, the integer division is of the form
175 * floor((... + (c * d + r) * x_pos + ...)/d)
177 * with -d < 2 * r <= d.
180 * floor((... + r * x_pos + ...)/d) + c * x_pos
182 * If 2 * ((c * d + r) % d) <= d, then c = floor((c * d + r)/d).
183 * Otherwise, c = floor((c * d + r)/d) + 1.
185 * This is the same normalization that is performed by isl_aff_floor.
187 static __isl_give isl_basic_map
*reduce_coefficient_in_div(
188 __isl_take isl_basic_map
*bmap
, int div
, int pos
)
194 isl_int_fdiv_r(shift
, bmap
->div
[div
][1 + pos
], bmap
->div
[div
][0]);
195 isl_int_mul_ui(shift
, shift
, 2);
196 add_one
= isl_int_gt(shift
, bmap
->div
[div
][0]);
197 isl_int_fdiv_q(shift
, bmap
->div
[div
][1 + pos
], bmap
->div
[div
][0]);
199 isl_int_add_ui(shift
, shift
, 1);
200 isl_int_neg(shift
, shift
);
201 bmap
= isl_basic_map_shift_div(bmap
, div
, pos
, shift
);
202 isl_int_clear(shift
);
207 /* Does the coefficient of the variable at position "pos"
208 * in integer division "div" need to be reduced?
209 * That is, does it lie outside the half-open interval (1/2,1/2]?
210 * The coefficient c/d lies outside this interval if abs(2 * c) >= d and
213 static isl_bool
needs_reduction(__isl_keep isl_basic_map
*bmap
, int div
,
218 if (isl_int_is_zero(bmap
->div
[div
][1 + pos
]))
219 return isl_bool_false
;
221 isl_int_mul_ui(bmap
->div
[div
][1 + pos
], bmap
->div
[div
][1 + pos
], 2);
222 r
= isl_int_abs_ge(bmap
->div
[div
][1 + pos
], bmap
->div
[div
][0]) &&
223 !isl_int_eq(bmap
->div
[div
][1 + pos
], bmap
->div
[div
][0]);
224 isl_int_divexact_ui(bmap
->div
[div
][1 + pos
],
225 bmap
->div
[div
][1 + pos
], 2);
230 /* Reduce the coefficients (including the constant term) of
231 * integer division "div", if needed.
232 * In particular, make sure all coefficients lie in
233 * the half-open interval (1/2,1/2].
235 static __isl_give isl_basic_map
*reduce_div_coefficients_of_div(
236 __isl_take isl_basic_map
*bmap
, int div
)
241 total
= isl_basic_map_dim(bmap
, isl_dim_all
);
243 return isl_basic_map_free(bmap
);
244 for (i
= 0; i
< 1 + total
; ++i
) {
247 reduce
= needs_reduction(bmap
, div
, i
);
249 return isl_basic_map_free(bmap
);
252 bmap
= reduce_coefficient_in_div(bmap
, div
, i
);
260 /* Reduce the coefficients (including the constant term) of
261 * the known integer divisions, if needed
262 * In particular, make sure all coefficients lie in
263 * the half-open interval (1/2,1/2].
265 static __isl_give isl_basic_map
*reduce_div_coefficients(
266 __isl_take isl_basic_map
*bmap
)
272 if (bmap
->n_div
== 0)
275 for (i
= 0; i
< bmap
->n_div
; ++i
) {
276 if (isl_int_is_zero(bmap
->div
[i
][0]))
278 bmap
= reduce_div_coefficients_of_div(bmap
, i
);
286 /* Remove any common factor in numerator and denominator of the div expression,
287 * not taking into account the constant term.
288 * That is, if the div is of the form
290 * floor((a + m f(x))/(m d))
294 * floor((floor(a/m) + f(x))/d)
296 * The difference {a/m}/d in the argument satisfies 0 <= {a/m}/d < 1/d
297 * and can therefore not influence the result of the floor.
299 static __isl_give isl_basic_map
*normalize_div_expression(
300 __isl_take isl_basic_map
*bmap
, int div
)
302 isl_size total
= isl_basic_map_dim(bmap
, isl_dim_all
);
303 isl_ctx
*ctx
= bmap
->ctx
;
306 return isl_basic_map_free(bmap
);
307 if (isl_int_is_zero(bmap
->div
[div
][0]))
309 isl_seq_gcd(bmap
->div
[div
] + 2, total
, &ctx
->normalize_gcd
);
310 isl_int_gcd(ctx
->normalize_gcd
, ctx
->normalize_gcd
, bmap
->div
[div
][0]);
311 if (isl_int_is_one(ctx
->normalize_gcd
))
313 isl_int_fdiv_q(bmap
->div
[div
][1], bmap
->div
[div
][1],
315 isl_int_divexact(bmap
->div
[div
][0], bmap
->div
[div
][0],
317 isl_seq_scale_down(bmap
->div
[div
] + 2, bmap
->div
[div
] + 2,
318 ctx
->normalize_gcd
, total
);
323 /* Remove any common factor in numerator and denominator of a div expression,
324 * not taking into account the constant term.
325 * That is, look for any div of the form
327 * floor((a + m f(x))/(m d))
331 * floor((floor(a/m) + f(x))/d)
333 * The difference {a/m}/d in the argument satisfies 0 <= {a/m}/d < 1/d
334 * and can therefore not influence the result of the floor.
336 static __isl_give isl_basic_map
*normalize_div_expressions(
337 __isl_take isl_basic_map
*bmap
)
343 if (bmap
->n_div
== 0)
346 for (i
= 0; i
< bmap
->n_div
; ++i
)
347 bmap
= normalize_div_expression(bmap
, i
);
352 /* Some progress has been made.
353 * Set *progress if "progress" is not NULL.
355 static void mark_progress(int *progress
)
361 /* Eliminate the variable at position "pos" from the constraints of "bmap"
362 * using the equality constraint "eq".
363 * If "keep_divs" is set, then try and preserve
364 * the integer division expressions. In this case, these expressions
365 * are assumed to have been ordered.
366 * If "equivalent" is set, then the elimination is performed
367 * using an equality constraint of "bmap", meaning that the meaning
368 * of the constraints is preserved.
370 static __isl_give isl_basic_map
*eliminate_var_using_equality(
371 __isl_take isl_basic_map
*bmap
,
372 unsigned pos
, isl_int
*eq
, int keep_divs
, int equivalent
, int *progress
)
380 total
= isl_basic_map_dim(bmap
, isl_dim_all
);
381 v_div
= isl_basic_map_var_offset(bmap
, isl_dim_div
);
382 if (total
< 0 || v_div
< 0)
383 return isl_basic_map_free(bmap
);
384 ctx
= isl_basic_map_get_ctx(bmap
);
385 last_div
= isl_seq_last_non_zero(eq
+ 1 + v_div
, bmap
->n_div
);
386 for (k
= 0; k
< bmap
->n_eq
; ++k
) {
387 if (bmap
->eq
[k
] == eq
)
389 if (isl_int_is_zero(bmap
->eq
[k
][1+pos
]))
391 mark_progress(progress
);
392 isl_seq_elim(bmap
->eq
[k
], eq
, 1+pos
, 1+total
, NULL
);
393 isl_seq_normalize(ctx
, bmap
->eq
[k
], 1 + total
);
396 for (k
= 0; k
< bmap
->n_ineq
; ++k
) {
397 if (isl_int_is_zero(bmap
->ineq
[k
][1+pos
]))
399 mark_progress(progress
);
400 isl_seq_elim(bmap
->ineq
[k
], eq
, 1+pos
, 1+total
, NULL
);
401 isl_seq_gcd(bmap
->ineq
[k
], 1 + total
, &ctx
->normalize_gcd
);
402 bmap
= scale_down_inequality(bmap
, k
, ctx
->normalize_gcd
,
404 bmap
= isl_basic_map_modify_inequality(bmap
, equivalent
);
409 for (k
= 0; k
< bmap
->n_div
; ++k
) {
410 if (isl_int_is_zero(bmap
->div
[k
][0]))
412 if (isl_int_is_zero(bmap
->div
[k
][1+1+pos
]))
414 mark_progress(progress
);
415 /* We need to be careful about circular definitions,
416 * so for now we just remove the definition of div k
417 * if the equality contains any divs.
418 * If keep_divs is set, then the divs have been ordered
419 * and we can keep the definition as long as the result
422 if (last_div
== -1 || (keep_divs
&& last_div
< k
)) {
423 isl_seq_elim(bmap
->div
[k
]+1, eq
,
424 1+pos
, 1+total
, &bmap
->div
[k
][0]);
425 bmap
= normalize_div_expression(bmap
, k
);
429 isl_seq_clr(bmap
->div
[k
], 1 + total
);
435 /* Eliminate and remove the local variable at position "pos" of "bmap"
436 * using the equality constraint "eq".
437 * If "keep_divs" is set, then try and preserve
438 * the integer division expressions. In this case, these expressions
439 * are assumed to have been ordered.
440 * If "equivalent" is set, then the elimination is performed
441 * using an equality constraint of "bmap", meaning that the meaning
442 * of the constraints is preserved.
444 static __isl_give isl_basic_map
*eliminate_div(__isl_take isl_basic_map
*bmap
,
445 isl_int
*eq
, unsigned div
, int keep_divs
, int equivalent
)
450 v_div
= isl_basic_map_var_offset(bmap
, isl_dim_div
);
452 return isl_basic_map_free(bmap
);
454 bmap
= eliminate_var_using_equality(bmap
, pos
, eq
, keep_divs
,
457 bmap
= isl_basic_map_drop_div(bmap
, div
);
462 /* Check if elimination of div "div" using equality "eq" would not
463 * result in a div depending on a later div.
465 static isl_bool
ok_to_eliminate_div(__isl_keep isl_basic_map
*bmap
, isl_int
*eq
,
473 v_div
= isl_basic_map_var_offset(bmap
, isl_dim_div
);
475 return isl_bool_error
;
478 last_div
= isl_seq_last_non_zero(eq
+ 1 + v_div
, bmap
->n_div
);
479 if (last_div
< 0 || last_div
<= div
)
480 return isl_bool_true
;
482 for (k
= 0; k
<= last_div
; ++k
) {
483 if (isl_int_is_zero(bmap
->div
[k
][0]))
485 if (!isl_int_is_zero(bmap
->div
[k
][1 + 1 + pos
]))
486 return isl_bool_false
;
489 return isl_bool_true
;
492 /* Eliminate divs based on equalities
494 static __isl_give isl_basic_map
*eliminate_divs_eq(
495 __isl_take isl_basic_map
*bmap
, int *progress
)
502 bmap
= isl_basic_map_order_divs(bmap
);
507 off
= isl_basic_map_offset(bmap
, isl_dim_div
);
509 for (d
= bmap
->n_div
- 1; d
>= 0 ; --d
) {
510 for (i
= 0; i
< bmap
->n_eq
; ++i
) {
513 if (!isl_int_is_one(bmap
->eq
[i
][off
+ d
]) &&
514 !isl_int_is_negone(bmap
->eq
[i
][off
+ d
]))
516 ok
= ok_to_eliminate_div(bmap
, bmap
->eq
[i
], d
);
518 return isl_basic_map_free(bmap
);
522 mark_progress(progress
);
523 bmap
= eliminate_div(bmap
, bmap
->eq
[i
], d
, 1, 1);
524 if (isl_basic_map_drop_equality(bmap
, i
) < 0)
525 return isl_basic_map_free(bmap
);
530 return eliminate_divs_eq(bmap
, progress
);
534 /* Eliminate divs based on inequalities
536 static __isl_give isl_basic_map
*eliminate_divs_ineq(
537 __isl_take isl_basic_map
*bmap
, int *progress
)
548 off
= isl_basic_map_offset(bmap
, isl_dim_div
);
550 for (d
= bmap
->n_div
- 1; d
>= 0 ; --d
) {
551 for (i
= 0; i
< bmap
->n_eq
; ++i
)
552 if (!isl_int_is_zero(bmap
->eq
[i
][off
+ d
]))
556 for (i
= 0; i
< bmap
->n_ineq
; ++i
)
557 if (isl_int_abs_gt(bmap
->ineq
[i
][off
+ d
], ctx
->one
))
559 if (i
< bmap
->n_ineq
)
561 mark_progress(progress
);
562 bmap
= isl_basic_map_eliminate_vars(bmap
, (off
-1)+d
, 1);
563 if (!bmap
|| ISL_F_ISSET(bmap
, ISL_BASIC_MAP_EMPTY
))
565 bmap
= isl_basic_map_drop_div(bmap
, d
);
572 /* Does the equality constraint at position "eq" in "bmap" involve
573 * any local variables in the range [first, first + n)
574 * that are not marked as having an explicit representation?
576 static isl_bool
bmap_eq_involves_unknown_divs(__isl_keep isl_basic_map
*bmap
,
577 int eq
, unsigned first
, unsigned n
)
583 return isl_bool_error
;
585 o_div
= isl_basic_map_offset(bmap
, isl_dim_div
);
586 for (i
= 0; i
< n
; ++i
) {
589 if (isl_int_is_zero(bmap
->eq
[eq
][o_div
+ first
+ i
]))
591 unknown
= isl_basic_map_div_is_marked_unknown(bmap
, first
+ i
);
593 return isl_bool_error
;
595 return isl_bool_true
;
598 return isl_bool_false
;
601 /* The last local variable involved in the equality constraint
602 * at position "eq" in "bmap" is the local variable at position "div".
603 * It can therefore be used to extract an explicit representation
605 * Do so unless the local variable already has an explicit representation or
606 * the explicit representation would involve any other local variables
607 * that in turn do not have an explicit representation.
608 * An equality constraint involving local variables without an explicit
609 * representation can be used in isl_basic_map_drop_redundant_divs
610 * to separate out an independent local variable. Introducing
611 * an explicit representation here would block this transformation,
612 * while the partial explicit representation in itself is not very useful.
613 * Set *progress if anything is changed.
615 * The equality constraint is of the form
619 * with n a positive number. The explicit representation derived from
624 static __isl_give isl_basic_map
*set_div_from_eq(__isl_take isl_basic_map
*bmap
,
625 int div
, int eq
, int *progress
)
634 if (!isl_int_is_zero(bmap
->div
[div
][0]))
637 involves
= bmap_eq_involves_unknown_divs(bmap
, eq
, 0, div
);
639 return isl_basic_map_free(bmap
);
643 total
= isl_basic_map_dim(bmap
, isl_dim_all
);
645 return isl_basic_map_free(bmap
);
646 o_div
= isl_basic_map_offset(bmap
, isl_dim_div
);
647 isl_seq_neg(bmap
->div
[div
] + 1, bmap
->eq
[eq
], 1 + total
);
648 isl_int_set_si(bmap
->div
[div
][1 + o_div
+ div
], 0);
649 isl_int_set(bmap
->div
[div
][0], bmap
->eq
[eq
][o_div
+ div
]);
650 mark_progress(progress
);
655 /* Perform fangcheng (Gaussian elimination) on the equality
656 * constraints of "bmap".
657 * That is, put them into row-echelon form, starting from the last column
658 * backward and use them to eliminate the corresponding coefficients
659 * from all constraints.
661 * If "progress" is not NULL, then it gets set if the elimination
662 * results in any changes.
663 * The elimination process may result in some equality constraints
664 * getting interchanged or removed.
665 * If "swap" or "drop" are not NULL, then they get called when
666 * two equality constraints get interchanged or
667 * when a number of final equality constraints get removed.
668 * As a special case, if the input turns out to be empty,
669 * then drop gets called with the number of removed equality
670 * constraints set to the total number of equality constraints.
671 * If "swap" or "drop" are not NULL, then the local variables (if any)
672 * are assumed to be in a valid order.
674 __isl_give isl_basic_map
*isl_basic_map_gauss5(__isl_take isl_basic_map
*bmap
,
676 isl_stat (*swap
)(unsigned a
, unsigned b
, void *user
),
677 isl_stat (*drop
)(unsigned n
, void *user
), void *user
)
687 bmap
= isl_basic_map_order_divs(bmap
);
689 total
= isl_basic_map_dim(bmap
, isl_dim_all
);
691 return isl_basic_map_free(bmap
);
693 total_var
= total
- bmap
->n_div
;
695 last_var
= total
- 1;
696 for (done
= 0; done
< bmap
->n_eq
; ++done
) {
697 for (; last_var
>= 0; --last_var
) {
698 for (k
= done
; k
< bmap
->n_eq
; ++k
)
699 if (!isl_int_is_zero(bmap
->eq
[k
][1+last_var
]))
707 swap_equality(bmap
, k
, done
);
708 if (swap
&& swap(k
, done
, user
) < 0)
709 return isl_basic_map_free(bmap
);
711 if (isl_int_is_neg(bmap
->eq
[done
][1+last_var
]))
712 isl_seq_neg(bmap
->eq
[done
], bmap
->eq
[done
], 1+total
);
714 bmap
= eliminate_var_using_equality(bmap
, last_var
,
715 bmap
->eq
[done
], 1, 1, progress
);
717 if (last_var
>= total_var
)
718 bmap
= set_div_from_eq(bmap
, last_var
- total_var
,
723 if (done
== bmap
->n_eq
)
725 for (k
= done
; k
< bmap
->n_eq
; ++k
) {
726 if (isl_int_is_zero(bmap
->eq
[k
][0]))
728 if (drop
&& drop(bmap
->n_eq
, user
) < 0)
729 return isl_basic_map_free(bmap
);
730 return isl_basic_map_set_to_empty(bmap
);
732 n_drop
= bmap
->n_eq
- done
;
733 bmap
= isl_basic_map_free_equality(bmap
, n_drop
);
734 if (drop
&& drop(n_drop
, user
) < 0)
735 return isl_basic_map_free(bmap
);
739 __isl_give isl_basic_map
*isl_basic_map_gauss(__isl_take isl_basic_map
*bmap
,
742 return isl_basic_map_gauss5(bmap
, progress
, NULL
, NULL
, NULL
);
745 __isl_give isl_basic_set
*isl_basic_set_gauss(
746 __isl_take isl_basic_set
*bset
, int *progress
)
748 return bset_from_bmap(isl_basic_map_gauss(bset_to_bmap(bset
),
753 static unsigned int round_up(unsigned int v
)
764 /* Hash table of inequalities in a basic map.
765 * "index" is an array of addresses of inequalities in the basic map, some
766 * of which are NULL. The inequalities are hashed on the coefficients
767 * except the constant term.
768 * "size" is the number of elements in the array and is always a power of two
769 * "bits" is the number of bits need to represent an index into the array.
770 * "total" is the total dimension of the basic map.
772 struct isl_constraint_index
{
779 /* Fill in the "ci" data structure for holding the inequalities of "bmap".
781 static isl_stat
create_constraint_index(struct isl_constraint_index
*ci
,
782 __isl_keep isl_basic_map
*bmap
)
788 return isl_stat_error
;
789 ci
->total
= isl_basic_map_dim(bmap
, isl_dim_all
);
791 return isl_stat_error
;
792 if (bmap
->n_ineq
== 0)
794 ci
->size
= round_up(4 * (bmap
->n_ineq
+ 1) / 3 - 1);
795 ci
->bits
= ffs(ci
->size
) - 1;
796 ctx
= isl_basic_map_get_ctx(bmap
);
797 ci
->index
= isl_calloc_array(ctx
, isl_int
**, ci
->size
);
799 return isl_stat_error
;
804 /* Free the memory allocated by create_constraint_index.
806 static void constraint_index_free(struct isl_constraint_index
*ci
)
811 /* Return the position in ci->index that contains the address of
812 * an inequality that is equal to *ineq up to the constant term,
813 * provided this address is not identical to "ineq".
814 * If there is no such inequality, then return the position where
815 * such an inequality should be inserted.
817 static int hash_index_ineq(struct isl_constraint_index
*ci
, isl_int
**ineq
)
820 uint32_t hash
= isl_seq_get_hash_bits((*ineq
) + 1, ci
->total
, ci
->bits
);
821 for (h
= hash
; ci
->index
[h
]; h
= (h
+1) % ci
->size
)
822 if (ineq
!= ci
->index
[h
] &&
823 isl_seq_eq((*ineq
) + 1, ci
->index
[h
][0]+1, ci
->total
))
828 /* Return the position in ci->index that contains the address of
829 * an inequality that is equal to the k'th inequality of "bmap"
830 * up to the constant term, provided it does not point to the very
832 * If there is no such inequality, then return the position where
833 * such an inequality should be inserted.
835 static int hash_index(struct isl_constraint_index
*ci
,
836 __isl_keep isl_basic_map
*bmap
, int k
)
838 return hash_index_ineq(ci
, &bmap
->ineq
[k
]);
841 static int set_hash_index(struct isl_constraint_index
*ci
,
842 __isl_keep isl_basic_set
*bset
, int k
)
844 return hash_index(ci
, bset
, k
);
847 /* Fill in the "ci" data structure with the inequalities of "bset".
849 static isl_stat
setup_constraint_index(struct isl_constraint_index
*ci
,
850 __isl_keep isl_basic_set
*bset
)
854 if (create_constraint_index(ci
, bset
) < 0)
855 return isl_stat_error
;
857 for (k
= 0; k
< bset
->n_ineq
; ++k
) {
858 h
= set_hash_index(ci
, bset
, k
);
859 ci
->index
[h
] = &bset
->ineq
[k
];
865 /* Is the inequality ineq (obviously) redundant with respect
866 * to the constraints in "ci"?
868 * Look for an inequality in "ci" with the same coefficients and then
869 * check if the contant term of "ineq" is greater than or equal
870 * to the constant term of that inequality. If so, "ineq" is clearly
873 * Note that hash_index_ineq ignores a stored constraint if it has
874 * the same address as the passed inequality. It is ok to pass
875 * the address of a local variable here since it will never be
876 * the same as the address of a constraint in "ci".
878 static isl_bool
constraint_index_is_redundant(struct isl_constraint_index
*ci
,
883 h
= hash_index_ineq(ci
, &ineq
);
885 return isl_bool_false
;
886 return isl_int_ge(ineq
[0], (*ci
->index
[h
])[0]);
889 /* If we can eliminate more than one div, then we need to make
890 * sure we do it from last div to first div, in order not to
891 * change the position of the other divs that still need to
894 static __isl_give isl_basic_map
*remove_duplicate_divs(
895 __isl_take isl_basic_map
*bmap
, int *progress
)
907 bmap
= isl_basic_map_order_divs(bmap
);
908 if (!bmap
|| bmap
->n_div
<= 1)
911 v_div
= isl_basic_map_var_offset(bmap
, isl_dim_div
);
913 return isl_basic_map_free(bmap
);
914 total
= v_div
+ bmap
->n_div
;
917 for (k
= bmap
->n_div
- 1; k
>= 0; --k
)
918 if (!isl_int_is_zero(bmap
->div
[k
][0]))
923 size
= round_up(4 * bmap
->n_div
/ 3 - 1);
926 elim_for
= isl_calloc_array(ctx
, int, bmap
->n_div
);
927 bits
= ffs(size
) - 1;
928 index
= isl_calloc_array(ctx
, int, size
);
929 if (!elim_for
|| !index
)
931 eq
= isl_blk_alloc(ctx
, 1+total
);
932 if (isl_blk_is_error(eq
))
935 isl_seq_clr(eq
.data
, 1+total
);
936 index
[isl_seq_get_hash_bits(bmap
->div
[k
], 2+total
, bits
)] = k
+ 1;
937 for (--k
; k
>= 0; --k
) {
940 if (isl_int_is_zero(bmap
->div
[k
][0]))
943 hash
= isl_seq_get_hash_bits(bmap
->div
[k
], 2+total
, bits
);
944 for (h
= hash
; index
[h
]; h
= (h
+1) % size
)
945 if (isl_seq_eq(bmap
->div
[k
],
946 bmap
->div
[index
[h
]-1], 2+total
))
949 mark_progress(progress
);
955 for (l
= bmap
->n_div
- 1; l
>= 0; --l
) {
959 isl_int_set_si(eq
.data
[1 + v_div
+ k
], -1);
960 isl_int_set_si(eq
.data
[1 + v_div
+ l
], 1);
961 bmap
= eliminate_div(bmap
, eq
.data
, l
, 1, 0);
964 isl_int_set_si(eq
.data
[1 + v_div
+ k
], 0);
965 isl_int_set_si(eq
.data
[1 + v_div
+ l
], 0);
968 isl_blk_free(ctx
, eq
);
975 /* Is the local variable at position "div" of "bmap"
976 * an integral integer division?
978 static isl_bool
is_known_integral_div(__isl_keep isl_basic_map
*bmap
, int div
)
982 unknown
= isl_basic_map_div_is_marked_unknown(bmap
, div
);
983 if (unknown
< 0 || unknown
)
984 return isl_bool_not(unknown
);
985 return isl_basic_map_div_is_integral(bmap
, div
);
988 /* Eliminate local variable "div" from "bmap", given
989 * that it represents an integer division with denominator 1.
991 * Construct an equality constraint that equates the local variable
992 * to the argument of the integer division and use that to eliminate
993 * the local variable.
995 static __isl_give isl_basic_map
*eliminate_integral_div(
996 __isl_take isl_basic_map
*bmap
, int div
)
998 isl_size total
, v_div
;
1001 v_div
= isl_basic_map_var_offset(bmap
, isl_dim_div
);
1002 total
= isl_basic_map_dim(bmap
, isl_dim_all
);
1003 if (v_div
< 0 || total
< 0)
1004 return isl_basic_map_free(bmap
);
1005 v
= isl_vec_alloc(isl_basic_map_get_ctx(bmap
), 1 + total
);
1007 return isl_basic_map_free(bmap
);
1008 isl_seq_cpy(v
->el
, bmap
->div
[div
] + 1, 1 + total
);
1009 isl_int_set_si(v
->el
[1 + v_div
+ div
], -1);
1010 bmap
= eliminate_div(bmap
, v
->el
, div
, 1, 0);
1016 /* Eliminate all integer divisions with denominator 1.
1018 static __isl_give isl_basic_map
*eliminate_integral_divs(
1019 __isl_take isl_basic_map
*bmap
, int *progress
)
1024 n_div
= isl_basic_map_dim(bmap
, isl_dim_div
);
1026 return isl_basic_map_free(bmap
);
1028 for (i
= 0; i
< n_div
; ++i
) {
1031 eliminate
= is_known_integral_div(bmap
, i
);
1033 return isl_basic_map_free(bmap
);
1037 bmap
= eliminate_integral_div(bmap
, i
);
1038 mark_progress(progress
);
1046 static int n_pure_div_eq(__isl_keep isl_basic_map
*bmap
)
1051 v_div
= isl_basic_map_var_offset(bmap
, isl_dim_div
);
1054 for (i
= 0, j
= bmap
->n_div
-1; i
< bmap
->n_eq
; ++i
) {
1055 while (j
>= 0 && isl_int_is_zero(bmap
->eq
[i
][1 + v_div
+ j
]))
1059 if (isl_seq_first_non_zero(bmap
->eq
[i
] + 1 + v_div
, j
) != -1)
1065 /* Normalize divs that appear in equalities.
1067 * In particular, we assume that bmap contains some equalities
1072 * and we want to replace the set of e_i by a minimal set and
1073 * such that the new e_i have a canonical representation in terms
1075 * If any of the equalities involves more than one divs, then
1076 * we currently simply bail out.
1078 * Let us first additionally assume that all equalities involve
1079 * a div. The equalities then express modulo constraints on the
1080 * remaining variables and we can use "parameter compression"
1081 * to find a minimal set of constraints. The result is a transformation
1083 * x = T(x') = x_0 + G x'
1085 * with G a lower-triangular matrix with all elements below the diagonal
1086 * non-negative and smaller than the diagonal element on the same row.
1087 * We first normalize x_0 by making the same property hold in the affine
1089 * The rows i of G with a 1 on the diagonal do not impose any modulo
1090 * constraint and simply express x_i = x'_i.
1091 * For each of the remaining rows i, we introduce a div and a corresponding
1092 * equality. In particular
1094 * g_ii e_j = x_i - g_i(x')
1096 * where each x'_k is replaced either by x_k (if g_kk = 1) or the
1097 * corresponding div (if g_kk != 1).
1099 * If there are any equalities not involving any div, then we
1100 * first apply a variable compression on the variables x:
1102 * x = C x'' x'' = C_2 x
1104 * and perform the above parameter compression on A C instead of on A.
1105 * The resulting compression is then of the form
1107 * x'' = T(x') = x_0 + G x'
1109 * and in constructing the new divs and the corresponding equalities,
1110 * we have to replace each x'', i.e., the x'_k with (g_kk = 1),
1111 * by the corresponding row from C_2.
1113 static __isl_give isl_basic_map
*normalize_divs(__isl_take isl_basic_map
*bmap
,
1121 struct isl_mat
*T
= NULL
;
1122 struct isl_mat
*C
= NULL
;
1123 struct isl_mat
*C2
= NULL
;
1126 int dropped
, needed
;
1131 if (bmap
->n_div
== 0)
1134 if (bmap
->n_eq
== 0)
1137 if (ISL_F_ISSET(bmap
, ISL_BASIC_MAP_NORMALIZED_DIVS
))
1140 v_div
= isl_basic_map_var_offset(bmap
, isl_dim_div
);
1141 div_eq
= n_pure_div_eq(bmap
);
1142 if (v_div
< 0 || div_eq
< 0)
1143 return isl_basic_map_free(bmap
);
1147 if (div_eq
< bmap
->n_eq
) {
1148 B
= isl_mat_sub_alloc6(bmap
->ctx
, bmap
->eq
, div_eq
,
1149 bmap
->n_eq
- div_eq
, 0, 1 + v_div
);
1150 C
= isl_mat_variable_compression(B
, &C2
);
1153 if (C
->n_col
== 0) {
1154 bmap
= isl_basic_map_set_to_empty(bmap
);
1161 d
= isl_vec_alloc(bmap
->ctx
, div_eq
);
1164 for (i
= 0, j
= bmap
->n_div
-1; i
< div_eq
; ++i
) {
1165 while (j
>= 0 && isl_int_is_zero(bmap
->eq
[i
][1 + v_div
+ j
]))
1167 isl_int_set(d
->block
.data
[i
], bmap
->eq
[i
][1 + v_div
+ j
]);
1169 B
= isl_mat_sub_alloc6(bmap
->ctx
, bmap
->eq
, 0, div_eq
, 0, 1 + v_div
);
1172 B
= isl_mat_product(B
, C
);
1176 T
= isl_mat_parameter_compression(B
, d
);
1179 if (T
->n_col
== 0) {
1180 bmap
= isl_basic_map_set_to_empty(bmap
);
1186 for (i
= 0; i
< T
->n_row
- 1; ++i
) {
1187 isl_int_fdiv_q(v
, T
->row
[1 + i
][0], T
->row
[1 + i
][1 + i
]);
1188 if (isl_int_is_zero(v
))
1190 isl_mat_col_submul(T
, 0, v
, 1 + i
);
1193 pos
= isl_alloc_array(bmap
->ctx
, int, T
->n_row
);
1196 /* We have to be careful because dropping equalities may reorder them */
1198 for (j
= bmap
->n_div
- 1; j
>= 0; --j
) {
1199 for (i
= 0; i
< bmap
->n_eq
; ++i
)
1200 if (!isl_int_is_zero(bmap
->eq
[i
][1 + v_div
+ j
]))
1202 if (i
< bmap
->n_eq
) {
1203 bmap
= isl_basic_map_drop_div(bmap
, j
);
1204 if (isl_basic_map_drop_equality(bmap
, i
) < 0)
1211 for (i
= 1; i
< T
->n_row
; ++i
) {
1212 if (isl_int_is_one(T
->row
[i
][i
]))
1217 if (needed
> dropped
) {
1218 bmap
= isl_basic_map_extend(bmap
, needed
, needed
, 0);
1222 for (i
= 1; i
< T
->n_row
; ++i
) {
1223 if (isl_int_is_one(T
->row
[i
][i
]))
1225 k
= isl_basic_map_alloc_div(bmap
);
1226 pos
[i
] = 1 + v_div
+ k
;
1227 isl_seq_clr(bmap
->div
[k
] + 1, 1 + v_div
+ bmap
->n_div
);
1228 isl_int_set(bmap
->div
[k
][0], T
->row
[i
][i
]);
1230 isl_seq_cpy(bmap
->div
[k
] + 1, C2
->row
[i
], 1 + v_div
);
1232 isl_int_set_si(bmap
->div
[k
][1 + i
], 1);
1233 for (j
= 0; j
< i
; ++j
) {
1234 if (isl_int_is_zero(T
->row
[i
][j
]))
1236 if (pos
[j
] < T
->n_row
&& C2
)
1237 isl_seq_submul(bmap
->div
[k
] + 1, T
->row
[i
][j
],
1238 C2
->row
[pos
[j
]], 1 + v_div
);
1240 isl_int_neg(bmap
->div
[k
][1 + pos
[j
]],
1243 j
= isl_basic_map_alloc_equality(bmap
);
1244 isl_seq_neg(bmap
->eq
[j
], bmap
->div
[k
]+1, 1+v_div
+bmap
->n_div
);
1245 isl_int_set(bmap
->eq
[j
][pos
[i
]], bmap
->div
[k
][0]);
1251 mark_progress(progress
);
1253 ISL_F_SET(bmap
, ISL_BASIC_MAP_NORMALIZED_DIVS
);
1261 isl_basic_map_free(bmap
);
1265 static __isl_give isl_basic_map
*set_div_from_lower_bound(
1266 __isl_take isl_basic_map
*bmap
, int div
, int ineq
)
1268 unsigned total
= isl_basic_map_offset(bmap
, isl_dim_div
);
1270 isl_seq_neg(bmap
->div
[div
] + 1, bmap
->ineq
[ineq
], total
+ bmap
->n_div
);
1271 isl_int_set(bmap
->div
[div
][0], bmap
->ineq
[ineq
][total
+ div
]);
1272 isl_int_add(bmap
->div
[div
][1], bmap
->div
[div
][1], bmap
->div
[div
][0]);
1273 isl_int_sub_ui(bmap
->div
[div
][1], bmap
->div
[div
][1], 1);
1274 isl_int_set_si(bmap
->div
[div
][1 + total
+ div
], 0);
1279 /* Check whether it is ok to define a div based on an inequality.
1280 * To avoid the introduction of circular definitions of divs, we
1281 * do not allow such a definition if the resulting expression would refer to
1282 * any other undefined divs or if any known div is defined in
1283 * terms of the unknown div.
1285 static isl_bool
ok_to_set_div_from_bound(__isl_keep isl_basic_map
*bmap
,
1289 unsigned total
= isl_basic_map_offset(bmap
, isl_dim_div
);
1291 /* Not defined in terms of unknown divs */
1292 for (j
= 0; j
< bmap
->n_div
; ++j
) {
1295 if (isl_int_is_zero(bmap
->ineq
[ineq
][total
+ j
]))
1297 if (isl_int_is_zero(bmap
->div
[j
][0]))
1298 return isl_bool_false
;
1301 /* No other div defined in terms of this one => avoid loops */
1302 for (j
= 0; j
< bmap
->n_div
; ++j
) {
1305 if (isl_int_is_zero(bmap
->div
[j
][0]))
1307 if (!isl_int_is_zero(bmap
->div
[j
][1 + total
+ div
]))
1308 return isl_bool_false
;
1311 return isl_bool_true
;
1314 /* Would an expression for div "div" based on inequality "ineq" of "bmap"
1315 * be a better expression than the current one?
1317 * If we do not have any expression yet, then any expression would be better.
1318 * Otherwise we check if the last variable involved in the inequality
1319 * (disregarding the div that it would define) is in an earlier position
1320 * than the last variable involved in the current div expression.
1322 static isl_bool
better_div_constraint(__isl_keep isl_basic_map
*bmap
,
1325 unsigned total
= isl_basic_map_offset(bmap
, isl_dim_div
);
1329 if (isl_int_is_zero(bmap
->div
[div
][0]))
1330 return isl_bool_true
;
1332 if (isl_seq_last_non_zero(bmap
->ineq
[ineq
] + total
+ div
+ 1,
1333 bmap
->n_div
- (div
+ 1)) >= 0)
1334 return isl_bool_false
;
1336 last_ineq
= isl_seq_last_non_zero(bmap
->ineq
[ineq
], total
+ div
);
1337 last_div
= isl_seq_last_non_zero(bmap
->div
[div
] + 1,
1338 total
+ bmap
->n_div
);
1340 return last_ineq
< last_div
;
1343 /* Given two constraints "k" and "l" that are opposite to each other,
1344 * except for the constant term, check if we can use them
1345 * to obtain an expression for one of the hitherto unknown divs or
1346 * a "better" expression for a div for which we already have an expression.
1347 * "sum" is the sum of the constant terms of the constraints.
1348 * If this sum is strictly smaller than the coefficient of one
1349 * of the divs, then this pair can be used to define the div.
1350 * To avoid the introduction of circular definitions of divs, we
1351 * do not use the pair if the resulting expression would refer to
1352 * any other undefined divs or if any known div is defined in
1353 * terms of the unknown div.
1355 static __isl_give isl_basic_map
*check_for_div_constraints(
1356 __isl_take isl_basic_map
*bmap
, int k
, int l
, isl_int sum
,
1360 unsigned total
= isl_basic_map_offset(bmap
, isl_dim_div
);
1362 for (i
= 0; i
< bmap
->n_div
; ++i
) {
1365 if (isl_int_is_zero(bmap
->ineq
[k
][total
+ i
]))
1367 if (isl_int_abs_ge(sum
, bmap
->ineq
[k
][total
+ i
]))
1369 set_div
= better_div_constraint(bmap
, i
, k
);
1370 if (set_div
>= 0 && set_div
)
1371 set_div
= ok_to_set_div_from_bound(bmap
, i
, k
);
1373 return isl_basic_map_free(bmap
);
1376 if (isl_int_is_pos(bmap
->ineq
[k
][total
+ i
]))
1377 bmap
= set_div_from_lower_bound(bmap
, i
, k
);
1379 bmap
= set_div_from_lower_bound(bmap
, i
, l
);
1380 mark_progress(progress
);
1386 __isl_give isl_basic_map
*isl_basic_map_remove_duplicate_constraints(
1387 __isl_take isl_basic_map
*bmap
, int *progress
, int detect_divs
)
1389 struct isl_constraint_index ci
;
1391 isl_size total
= isl_basic_map_dim(bmap
, isl_dim_all
);
1394 if (total
< 0 || bmap
->n_ineq
<= 1)
1397 if (create_constraint_index(&ci
, bmap
) < 0)
1400 h
= isl_seq_get_hash_bits(bmap
->ineq
[0] + 1, total
, ci
.bits
);
1401 ci
.index
[h
] = &bmap
->ineq
[0];
1402 for (k
= 1; k
< bmap
->n_ineq
; ++k
) {
1403 h
= hash_index(&ci
, bmap
, k
);
1405 ci
.index
[h
] = &bmap
->ineq
[k
];
1408 l
= ci
.index
[h
] - &bmap
->ineq
[0];
1409 if (isl_int_lt(bmap
->ineq
[k
][0], bmap
->ineq
[l
][0]))
1410 swap_inequality(bmap
, k
, l
);
1411 isl_basic_map_drop_inequality(bmap
, k
);
1415 for (k
= 0; bmap
&& k
< bmap
->n_ineq
-1; ++k
) {
1416 isl_seq_neg(bmap
->ineq
[k
]+1, bmap
->ineq
[k
]+1, total
);
1417 h
= hash_index(&ci
, bmap
, k
);
1418 isl_seq_neg(bmap
->ineq
[k
]+1, bmap
->ineq
[k
]+1, total
);
1421 l
= ci
.index
[h
] - &bmap
->ineq
[0];
1422 isl_int_add(sum
, bmap
->ineq
[k
][0], bmap
->ineq
[l
][0]);
1423 if (isl_int_is_pos(sum
)) {
1425 bmap
= check_for_div_constraints(bmap
, k
, l
,
1429 if (isl_int_is_zero(sum
)) {
1430 /* We need to break out of the loop after these
1431 * changes since the contents of the hash
1432 * will no longer be valid.
1433 * Plus, we probably we want to regauss first.
1435 mark_progress(progress
);
1436 isl_basic_map_drop_inequality(bmap
, l
);
1437 isl_basic_map_inequality_to_equality(bmap
, k
);
1439 bmap
= isl_basic_map_set_to_empty(bmap
);
1444 constraint_index_free(&ci
);
1448 /* Detect all pairs of inequalities that form an equality.
1450 * isl_basic_map_remove_duplicate_constraints detects at most one such pair.
1451 * Call it repeatedly while it is making progress.
1453 __isl_give isl_basic_map
*isl_basic_map_detect_inequality_pairs(
1454 __isl_take isl_basic_map
*bmap
, int *progress
)
1460 bmap
= isl_basic_map_remove_duplicate_constraints(bmap
,
1463 mark_progress(progress
);
1464 } while (duplicate
);
1469 /* Given a known integer division "div" that is not integral
1470 * (with denominator 1), eliminate it from the constraints in "bmap"
1471 * where it appears with a (positive or negative) unit coefficient.
1472 * If "progress" is not NULL, then it gets set if the elimination
1473 * results in any changes.
1477 * floor(e/m) + f >= 0
1485 * -floor(e/m) + f >= 0
1489 * -e + m f + m - 1 >= 0
1491 * The first conversion is valid because floor(e/m) >= -f is equivalent
1492 * to e/m >= -f because -f is an integral expression.
1493 * The second conversion follows from the fact that
1495 * -floor(e/m) = ceil(-e/m) = floor((-e + m - 1)/m)
1498 * Note that one of the div constraints may have been eliminated
1499 * due to being redundant with respect to the constraint that is
1500 * being modified by this function. The modified constraint may
1501 * no longer imply this div constraint, so we add it back to make
1502 * sure we do not lose any information.
1504 static __isl_give isl_basic_map
*eliminate_unit_div(
1505 __isl_take isl_basic_map
*bmap
, int div
, int *progress
)
1508 isl_size v_div
, dim
;
1511 v_div
= isl_basic_map_var_offset(bmap
, isl_dim_div
);
1512 dim
= isl_basic_map_dim(bmap
, isl_dim_all
);
1513 if (v_div
< 0 || dim
< 0)
1514 return isl_basic_map_free(bmap
);
1516 ctx
= isl_basic_map_get_ctx(bmap
);
1518 for (j
= 0; j
< bmap
->n_ineq
; ++j
) {
1521 if (!isl_int_is_one(bmap
->ineq
[j
][1 + v_div
+ div
]) &&
1522 !isl_int_is_negone(bmap
->ineq
[j
][1 + v_div
+ div
]))
1525 mark_progress(progress
);
1527 s
= isl_int_sgn(bmap
->ineq
[j
][1 + v_div
+ div
]);
1528 isl_int_set_si(bmap
->ineq
[j
][1 + v_div
+ div
], 0);
1530 isl_seq_combine(bmap
->ineq
[j
],
1531 ctx
->negone
, bmap
->div
[div
] + 1,
1532 bmap
->div
[div
][0], bmap
->ineq
[j
], 1 + dim
);
1534 isl_seq_combine(bmap
->ineq
[j
],
1535 ctx
->one
, bmap
->div
[div
] + 1,
1536 bmap
->div
[div
][0], bmap
->ineq
[j
], 1 + dim
);
1538 isl_int_add(bmap
->ineq
[j
][0],
1539 bmap
->ineq
[j
][0], bmap
->div
[div
][0]);
1540 isl_int_sub_ui(bmap
->ineq
[j
][0],
1541 bmap
->ineq
[j
][0], 1);
1544 bmap
= isl_basic_map_extend_constraints(bmap
, 0, 1);
1545 bmap
= isl_basic_map_add_div_constraint(bmap
, div
, s
);
1553 /* Eliminate selected known divs from constraints where they appear with
1554 * a (positive or negative) unit coefficient.
1555 * In particular, only handle those for which "select" returns isl_bool_true.
1556 * If "progress" is not NULL, then it gets set if the elimination
1557 * results in any changes.
1559 * We skip integral divs, i.e., those with denominator 1, as we would
1560 * risk eliminating the div from the div constraints.
1561 * They are eliminated in eliminate_integral_divs instead.
1563 static __isl_give isl_basic_map
*eliminate_selected_unit_divs(
1564 __isl_take isl_basic_map
*bmap
,
1565 isl_bool (*select
)(__isl_keep isl_basic_map
*bmap
, int div
),
1571 n_div
= isl_basic_map_dim(bmap
, isl_dim_div
);
1573 return isl_basic_map_free(bmap
);
1575 for (i
= 0; i
< n_div
; ++i
) {
1579 skip
= isl_basic_map_div_is_marked_unknown(bmap
, i
);
1580 if (skip
>= 0 && !skip
)
1581 skip
= isl_basic_map_div_is_integral(bmap
, i
);
1583 return isl_basic_map_free(bmap
);
1586 selected
= select(bmap
, i
);
1588 return isl_basic_map_free(bmap
);
1591 bmap
= eliminate_unit_div(bmap
, i
, progress
);
1599 /* eliminate_selected_unit_divs callback that selects every
1602 static isl_bool
is_any_div(__isl_keep isl_basic_map
*bmap
, int div
)
1604 return isl_bool_true
;
1607 /* Eliminate known divs from constraints where they appear with
1608 * a (positive or negative) unit coefficient.
1609 * If "progress" is not NULL, then it gets set if the elimination
1610 * results in any changes.
1612 static __isl_give isl_basic_map
*eliminate_unit_divs(
1613 __isl_take isl_basic_map
*bmap
, int *progress
)
1615 return eliminate_selected_unit_divs(bmap
, &is_any_div
, progress
);
1618 /* eliminate_selected_unit_divs callback that selects
1619 * integer divisions that only appear with
1620 * a (positive or negative) unit coefficient
1621 * (outside their div constraints).
1623 static isl_bool
is_pure_unit_div(__isl_keep isl_basic_map
*bmap
, int div
)
1626 isl_size v_div
, n_ineq
;
1628 v_div
= isl_basic_map_var_offset(bmap
, isl_dim_div
);
1629 n_ineq
= isl_basic_map_n_inequality(bmap
);
1630 if (v_div
< 0 || n_ineq
< 0)
1631 return isl_bool_error
;
1633 for (i
= 0; i
< n_ineq
; ++i
) {
1636 if (isl_int_is_zero(bmap
->ineq
[i
][1 + v_div
+ div
]))
1638 skip
= isl_basic_map_is_div_constraint(bmap
,
1639 bmap
->ineq
[i
], div
);
1641 return isl_bool_error
;
1644 if (!isl_int_is_one(bmap
->ineq
[i
][1 + v_div
+ div
]) &&
1645 !isl_int_is_negone(bmap
->ineq
[i
][1 + v_div
+ div
]))
1646 return isl_bool_false
;
1649 return isl_bool_true
;
1652 /* Eliminate known divs from constraints where they appear with
1653 * a (positive or negative) unit coefficient,
1654 * but only if they do not appear in any other constraints
1655 * (other than the div constraints).
1657 __isl_give isl_basic_map
*isl_basic_map_eliminate_pure_unit_divs(
1658 __isl_take isl_basic_map
*bmap
)
1660 return eliminate_selected_unit_divs(bmap
, &is_pure_unit_div
, NULL
);
1663 __isl_give isl_basic_map
*isl_basic_map_simplify(__isl_take isl_basic_map
*bmap
)
1672 empty
= isl_basic_map_plain_is_empty(bmap
);
1674 return isl_basic_map_free(bmap
);
1677 bmap
= isl_basic_map_normalize_constraints(bmap
);
1678 bmap
= reduce_div_coefficients(bmap
);
1679 bmap
= normalize_div_expressions(bmap
);
1680 bmap
= remove_duplicate_divs(bmap
, &progress
);
1681 bmap
= eliminate_unit_divs(bmap
, &progress
);
1682 bmap
= eliminate_divs_eq(bmap
, &progress
);
1683 bmap
= eliminate_divs_ineq(bmap
, &progress
);
1684 bmap
= eliminate_integral_divs(bmap
, &progress
);
1685 bmap
= isl_basic_map_gauss(bmap
, &progress
);
1686 /* requires equalities in normal form */
1687 bmap
= normalize_divs(bmap
, &progress
);
1688 bmap
= isl_basic_map_remove_duplicate_constraints(bmap
,
1694 __isl_give isl_basic_set
*isl_basic_set_simplify(
1695 __isl_take isl_basic_set
*bset
)
1697 return bset_from_bmap(isl_basic_map_simplify(bset_to_bmap(bset
)));
1701 isl_bool
isl_basic_map_is_div_constraint(__isl_keep isl_basic_map
*bmap
,
1702 isl_int
*constraint
, unsigned div
)
1707 return isl_bool_error
;
1709 pos
= isl_basic_map_offset(bmap
, isl_dim_div
) + div
;
1711 if (isl_int_eq(constraint
[pos
], bmap
->div
[div
][0])) {
1713 isl_int_sub(bmap
->div
[div
][1],
1714 bmap
->div
[div
][1], bmap
->div
[div
][0]);
1715 isl_int_add_ui(bmap
->div
[div
][1], bmap
->div
[div
][1], 1);
1716 neg
= isl_seq_is_neg(constraint
, bmap
->div
[div
]+1, pos
);
1717 isl_int_sub_ui(bmap
->div
[div
][1], bmap
->div
[div
][1], 1);
1718 isl_int_add(bmap
->div
[div
][1],
1719 bmap
->div
[div
][1], bmap
->div
[div
][0]);
1721 return isl_bool_false
;
1722 if (isl_seq_first_non_zero(constraint
+pos
+1,
1723 bmap
->n_div
-div
-1) != -1)
1724 return isl_bool_false
;
1725 } else if (isl_int_abs_eq(constraint
[pos
], bmap
->div
[div
][0])) {
1726 if (!isl_seq_eq(constraint
, bmap
->div
[div
]+1, pos
))
1727 return isl_bool_false
;
1728 if (isl_seq_first_non_zero(constraint
+pos
+1,
1729 bmap
->n_div
-div
-1) != -1)
1730 return isl_bool_false
;
1732 return isl_bool_false
;
1734 return isl_bool_true
;
1737 /* If the only constraints a div d=floor(f/m)
1738 * appears in are its two defining constraints
1741 * -(f - (m - 1)) + m d >= 0
1743 * then it can safely be removed.
1745 static isl_bool
div_is_redundant(__isl_keep isl_basic_map
*bmap
, int div
)
1748 isl_size v_div
= isl_basic_map_var_offset(bmap
, isl_dim_div
);
1749 unsigned pos
= 1 + v_div
+ div
;
1752 return isl_bool_error
;
1754 for (i
= 0; i
< bmap
->n_eq
; ++i
)
1755 if (!isl_int_is_zero(bmap
->eq
[i
][pos
]))
1756 return isl_bool_false
;
1758 for (i
= 0; i
< bmap
->n_ineq
; ++i
) {
1761 if (isl_int_is_zero(bmap
->ineq
[i
][pos
]))
1763 red
= isl_basic_map_is_div_constraint(bmap
, bmap
->ineq
[i
], div
);
1764 if (red
< 0 || !red
)
1768 for (i
= 0; i
< bmap
->n_div
; ++i
) {
1769 if (isl_int_is_zero(bmap
->div
[i
][0]))
1771 if (!isl_int_is_zero(bmap
->div
[i
][1+pos
]))
1772 return isl_bool_false
;
1775 return isl_bool_true
;
1779 * Remove divs that don't occur in any of the constraints or other divs.
1780 * These can arise when dropping constraints from a basic map or
1781 * when the divs of a basic map have been temporarily aligned
1782 * with the divs of another basic map.
1784 static __isl_give isl_basic_map
*remove_redundant_divs(
1785 __isl_take isl_basic_map
*bmap
)
1790 v_div
= isl_basic_map_var_offset(bmap
, isl_dim_div
);
1792 return isl_basic_map_free(bmap
);
1794 for (i
= bmap
->n_div
-1; i
>= 0; --i
) {
1797 redundant
= div_is_redundant(bmap
, i
);
1799 return isl_basic_map_free(bmap
);
1802 bmap
= isl_basic_map_drop_constraints_involving(bmap
,
1804 bmap
= isl_basic_map_drop_div(bmap
, i
);
1809 /* Mark "bmap" as final, without checking for obviously redundant
1810 * integer divisions. This function should be used when "bmap"
1811 * is known not to involve any such integer divisions.
1813 __isl_give isl_basic_map
*isl_basic_map_mark_final(
1814 __isl_take isl_basic_map
*bmap
)
1818 ISL_F_SET(bmap
, ISL_BASIC_SET_FINAL
);
1822 /* Mark "bmap" as final, after removing obviously redundant integer divisions.
1824 __isl_give isl_basic_map
*isl_basic_map_finalize(__isl_take isl_basic_map
*bmap
)
1826 bmap
= remove_redundant_divs(bmap
);
1827 bmap
= isl_basic_map_mark_final(bmap
);
1831 __isl_give isl_basic_set
*isl_basic_set_finalize(
1832 __isl_take isl_basic_set
*bset
)
1834 return bset_from_bmap(isl_basic_map_finalize(bset_to_bmap(bset
)));
1837 /* Remove definition of any div that is defined in terms of the given variable.
1838 * The div itself is not removed. Functions such as
1839 * eliminate_divs_ineq depend on the other divs remaining in place.
1841 static __isl_give isl_basic_map
*remove_dependent_vars(
1842 __isl_take isl_basic_map
*bmap
, int pos
)
1849 for (i
= 0; i
< bmap
->n_div
; ++i
) {
1850 if (isl_int_is_zero(bmap
->div
[i
][0]))
1852 if (isl_int_is_zero(bmap
->div
[i
][1+1+pos
]))
1854 bmap
= isl_basic_map_mark_div_unknown(bmap
, i
);
1861 /* Eliminate the specified variables from the constraints using
1862 * Fourier-Motzkin. The variables themselves are not removed.
1864 __isl_give isl_basic_map
*isl_basic_map_eliminate_vars(
1865 __isl_take isl_basic_map
*bmap
, unsigned pos
, unsigned n
)
1874 total
= isl_basic_map_dim(bmap
, isl_dim_all
);
1876 return isl_basic_map_free(bmap
);
1878 bmap
= isl_basic_map_cow(bmap
);
1879 for (d
= pos
+ n
- 1; d
>= 0 && d
>= pos
; --d
)
1880 bmap
= remove_dependent_vars(bmap
, d
);
1884 for (d
= pos
+ n
- 1;
1885 d
>= 0 && d
>= total
- bmap
->n_div
&& d
>= pos
; --d
)
1886 isl_seq_clr(bmap
->div
[d
-(total
-bmap
->n_div
)], 2+total
);
1887 for (d
= pos
+ n
- 1; d
>= 0 && d
>= pos
; --d
) {
1888 int n_lower
, n_upper
;
1891 for (i
= 0; i
< bmap
->n_eq
; ++i
) {
1892 if (isl_int_is_zero(bmap
->eq
[i
][1+d
]))
1894 bmap
= eliminate_var_using_equality(bmap
, d
,
1895 bmap
->eq
[i
], 0, 1, NULL
);
1896 if (isl_basic_map_drop_equality(bmap
, i
) < 0)
1897 return isl_basic_map_free(bmap
);
1905 for (i
= 0; i
< bmap
->n_ineq
; ++i
) {
1906 if (isl_int_is_pos(bmap
->ineq
[i
][1+d
]))
1908 else if (isl_int_is_neg(bmap
->ineq
[i
][1+d
]))
1911 bmap
= isl_basic_map_extend_constraints(bmap
,
1912 0, n_lower
* n_upper
);
1915 for (i
= bmap
->n_ineq
- 1; i
>= 0; --i
) {
1917 if (isl_int_is_zero(bmap
->ineq
[i
][1+d
]))
1920 for (j
= 0; j
< i
; ++j
) {
1921 if (isl_int_is_zero(bmap
->ineq
[j
][1+d
]))
1924 if (isl_int_sgn(bmap
->ineq
[i
][1+d
]) ==
1925 isl_int_sgn(bmap
->ineq
[j
][1+d
]))
1927 k
= isl_basic_map_alloc_inequality(bmap
);
1930 isl_seq_cpy(bmap
->ineq
[k
], bmap
->ineq
[i
],
1932 isl_seq_elim(bmap
->ineq
[k
], bmap
->ineq
[j
],
1933 1+d
, 1+total
, NULL
);
1935 isl_basic_map_drop_inequality(bmap
, i
);
1938 if (n_lower
> 0 && n_upper
> 0) {
1939 bmap
= isl_basic_map_normalize_constraints(bmap
);
1940 bmap
= isl_basic_map_remove_duplicate_constraints(bmap
,
1942 bmap
= isl_basic_map_gauss(bmap
, NULL
);
1943 bmap
= isl_basic_map_remove_redundancies(bmap
);
1947 if (ISL_F_ISSET(bmap
, ISL_BASIC_MAP_EMPTY
))
1952 bmap
= isl_basic_map_gauss(bmap
, NULL
);
1955 isl_basic_map_free(bmap
);
1959 __isl_give isl_basic_set
*isl_basic_set_eliminate_vars(
1960 __isl_take isl_basic_set
*bset
, unsigned pos
, unsigned n
)
1962 return bset_from_bmap(isl_basic_map_eliminate_vars(bset_to_bmap(bset
),
1966 /* Eliminate the specified n dimensions starting at first from the
1967 * constraints, without removing the dimensions from the space.
1968 * If the set is rational, the dimensions are eliminated using Fourier-Motzkin.
1969 * Otherwise, they are projected out and the original space is restored.
1971 __isl_give isl_basic_map
*isl_basic_map_eliminate(
1972 __isl_take isl_basic_map
*bmap
,
1973 enum isl_dim_type type
, unsigned first
, unsigned n
)
1982 if (isl_basic_map_check_range(bmap
, type
, first
, n
) < 0)
1983 return isl_basic_map_free(bmap
);
1985 if (ISL_F_ISSET(bmap
, ISL_BASIC_MAP_RATIONAL
)) {
1986 first
+= isl_basic_map_offset(bmap
, type
) - 1;
1987 bmap
= isl_basic_map_eliminate_vars(bmap
, first
, n
);
1988 return isl_basic_map_finalize(bmap
);
1991 space
= isl_basic_map_get_space(bmap
);
1992 bmap
= isl_basic_map_project_out(bmap
, type
, first
, n
);
1993 bmap
= isl_basic_map_insert_dims(bmap
, type
, first
, n
);
1994 bmap
= isl_basic_map_reset_space(bmap
, space
);
1998 __isl_give isl_basic_set
*isl_basic_set_eliminate(
1999 __isl_take isl_basic_set
*bset
,
2000 enum isl_dim_type type
, unsigned first
, unsigned n
)
2002 return isl_basic_map_eliminate(bset
, type
, first
, n
);
2005 /* Remove all constraints from "bmap" that reference any unknown local
2006 * variables (directly or indirectly).
2008 * Dropping all constraints on a local variable will make it redundant,
2009 * so it will get removed implicitly by
2010 * isl_basic_map_drop_constraints_involving_dims. Some other local
2011 * variables may also end up becoming redundant if they only appear
2012 * in constraints together with the unknown local variable.
2013 * Therefore, start over after calling
2014 * isl_basic_map_drop_constraints_involving_dims.
2016 __isl_give isl_basic_map
*isl_basic_map_drop_constraints_involving_unknown_divs(
2017 __isl_take isl_basic_map
*bmap
)
2023 known
= isl_basic_map_divs_known(bmap
);
2025 return isl_basic_map_free(bmap
);
2029 n_div
= isl_basic_map_dim(bmap
, isl_dim_div
);
2031 return isl_basic_map_free(bmap
);
2032 o_div
= isl_basic_map_offset(bmap
, isl_dim_div
) - 1;
2034 for (i
= 0; i
< n_div
; ++i
) {
2035 known
= isl_basic_map_div_is_known(bmap
, i
);
2037 return isl_basic_map_free(bmap
);
2040 bmap
= remove_dependent_vars(bmap
, o_div
+ i
);
2041 bmap
= isl_basic_map_drop_constraints_involving_dims(bmap
,
2043 n_div
= isl_basic_map_dim(bmap
, isl_dim_div
);
2045 return isl_basic_map_free(bmap
);
2052 /* Remove all constraints from "bset" that reference any unknown local
2053 * variables (directly or indirectly).
2055 __isl_give isl_basic_set
*isl_basic_set_drop_constraints_involving_unknown_divs(
2056 __isl_take isl_basic_set
*bset
)
2058 isl_basic_map
*bmap
;
2060 bmap
= bset_to_bmap(bset
);
2061 bmap
= isl_basic_map_drop_constraints_involving_unknown_divs(bmap
);
2062 return bset_from_bmap(bmap
);
2065 /* Remove all constraints from "map" that reference any unknown local
2066 * variables (directly or indirectly).
2068 * Since constraints may get dropped from the basic maps,
2069 * they may no longer be disjoint from each other.
2071 __isl_give isl_map
*isl_map_drop_constraints_involving_unknown_divs(
2072 __isl_take isl_map
*map
)
2077 known
= isl_map_divs_known(map
);
2079 return isl_map_free(map
);
2083 map
= isl_map_cow(map
);
2087 for (i
= 0; i
< map
->n
; ++i
) {
2089 isl_basic_map_drop_constraints_involving_unknown_divs(
2092 return isl_map_free(map
);
2096 ISL_F_CLR(map
, ISL_MAP_DISJOINT
);
2101 /* Don't assume equalities are in order, because align_divs
2102 * may have changed the order of the divs.
2104 static void compute_elimination_index(__isl_keep isl_basic_map
*bmap
, int *elim
,
2109 for (d
= 0; d
< len
; ++d
)
2111 for (i
= 0; i
< bmap
->n_eq
; ++i
) {
2112 for (d
= len
- 1; d
>= 0; --d
) {
2113 if (isl_int_is_zero(bmap
->eq
[i
][1+d
]))
2121 static void set_compute_elimination_index(__isl_keep isl_basic_set
*bset
,
2122 int *elim
, unsigned len
)
2124 compute_elimination_index(bset_to_bmap(bset
), elim
, len
);
2127 static int reduced_using_equalities(isl_int
*dst
, isl_int
*src
,
2128 __isl_keep isl_basic_map
*bmap
, int *elim
, unsigned total
)
2133 for (d
= total
- 1; d
>= 0; --d
) {
2134 if (isl_int_is_zero(src
[1+d
]))
2139 isl_seq_cpy(dst
, src
, 1 + total
);
2142 isl_seq_elim(dst
, bmap
->eq
[elim
[d
]], 1 + d
, 1 + total
, NULL
);
2147 static int set_reduced_using_equalities(isl_int
*dst
, isl_int
*src
,
2148 __isl_keep isl_basic_set
*bset
, int *elim
, unsigned total
)
2150 return reduced_using_equalities(dst
, src
,
2151 bset_to_bmap(bset
), elim
, total
);
2154 static __isl_give isl_basic_set
*isl_basic_set_reduce_using_equalities(
2155 __isl_take isl_basic_set
*bset
, __isl_take isl_basic_set
*context
)
2161 if (!bset
|| !context
)
2164 if (context
->n_eq
== 0) {
2165 isl_basic_set_free(context
);
2169 bset
= isl_basic_set_cow(bset
);
2170 dim
= isl_basic_set_dim(bset
, isl_dim_set
);
2174 elim
= isl_alloc_array(bset
->ctx
, int, dim
);
2177 set_compute_elimination_index(context
, elim
, dim
);
2178 for (i
= 0; i
< bset
->n_eq
; ++i
)
2179 set_reduced_using_equalities(bset
->eq
[i
], bset
->eq
[i
],
2180 context
, elim
, dim
);
2181 for (i
= 0; i
< bset
->n_ineq
; ++i
)
2182 set_reduced_using_equalities(bset
->ineq
[i
], bset
->ineq
[i
],
2183 context
, elim
, dim
);
2184 isl_basic_set_free(context
);
2186 bset
= isl_basic_set_simplify(bset
);
2187 bset
= isl_basic_set_finalize(bset
);
2190 isl_basic_set_free(bset
);
2191 isl_basic_set_free(context
);
2195 /* For each inequality in "ineq" that is a shifted (more relaxed)
2196 * copy of an inequality in "context", mark the corresponding entry
2198 * If an inequality only has a non-negative constant term, then
2201 static isl_stat
mark_shifted_constraints(__isl_keep isl_mat
*ineq
,
2202 __isl_keep isl_basic_set
*context
, int *row
)
2204 struct isl_constraint_index ci
;
2205 isl_size n_ineq
, cols
;
2209 if (!ineq
|| !context
)
2210 return isl_stat_error
;
2211 if (context
->n_ineq
== 0)
2213 if (setup_constraint_index(&ci
, context
) < 0)
2214 return isl_stat_error
;
2216 n_ineq
= isl_mat_rows(ineq
);
2217 cols
= isl_mat_cols(ineq
);
2218 if (n_ineq
< 0 || cols
< 0)
2219 return isl_stat_error
;
2221 for (k
= 0; k
< n_ineq
; ++k
) {
2225 l
= isl_seq_first_non_zero(ineq
->row
[k
] + 1, total
);
2226 if (l
< 0 && isl_int_is_nonneg(ineq
->row
[k
][0])) {
2230 redundant
= constraint_index_is_redundant(&ci
, ineq
->row
[k
]);
2237 constraint_index_free(&ci
);
2240 constraint_index_free(&ci
);
2241 return isl_stat_error
;
2244 static __isl_give isl_basic_set
*remove_shifted_constraints(
2245 __isl_take isl_basic_set
*bset
, __isl_keep isl_basic_set
*context
)
2247 struct isl_constraint_index ci
;
2250 if (!bset
|| !context
)
2253 if (context
->n_ineq
== 0)
2255 if (setup_constraint_index(&ci
, context
) < 0)
2258 for (k
= 0; k
< bset
->n_ineq
; ++k
) {
2261 redundant
= constraint_index_is_redundant(&ci
, bset
->ineq
[k
]);
2266 bset
= isl_basic_set_cow(bset
);
2269 isl_basic_set_drop_inequality(bset
, k
);
2272 constraint_index_free(&ci
);
2275 constraint_index_free(&ci
);
2279 /* Remove constraints from "bmap" that are identical to constraints
2280 * in "context" or that are more relaxed (greater constant term).
2282 * We perform the test for shifted copies on the pure constraints
2283 * in remove_shifted_constraints.
2285 static __isl_give isl_basic_map
*isl_basic_map_remove_shifted_constraints(
2286 __isl_take isl_basic_map
*bmap
, __isl_take isl_basic_map
*context
)
2288 isl_basic_set
*bset
, *bset_context
;
2290 if (!bmap
|| !context
)
2293 if (bmap
->n_ineq
== 0 || context
->n_ineq
== 0) {
2294 isl_basic_map_free(context
);
2298 bmap
= isl_basic_map_order_divs(bmap
);
2299 context
= isl_basic_map_align_divs(context
, bmap
);
2300 bmap
= isl_basic_map_align_divs(bmap
, context
);
2302 bset
= isl_basic_map_underlying_set(isl_basic_map_copy(bmap
));
2303 bset_context
= isl_basic_map_underlying_set(context
);
2304 bset
= remove_shifted_constraints(bset
, bset_context
);
2305 isl_basic_set_free(bset_context
);
2307 bmap
= isl_basic_map_overlying_set(bset
, bmap
);
2311 isl_basic_map_free(bmap
);
2312 isl_basic_map_free(context
);
2316 /* Does the (linear part of a) constraint "c" involve any of the "len"
2317 * "relevant" dimensions?
2319 static int is_related(isl_int
*c
, int len
, int *relevant
)
2323 for (i
= 0; i
< len
; ++i
) {
2326 if (!isl_int_is_zero(c
[i
]))
2333 /* Drop constraints from "bmap" that do not involve any of
2334 * the dimensions marked "relevant".
2336 static __isl_give isl_basic_map
*drop_unrelated_constraints(
2337 __isl_take isl_basic_map
*bmap
, int *relevant
)
2342 dim
= isl_basic_map_dim(bmap
, isl_dim_all
);
2344 return isl_basic_map_free(bmap
);
2345 for (i
= 0; i
< dim
; ++i
)
2351 for (i
= bmap
->n_eq
- 1; i
>= 0; --i
)
2352 if (!is_related(bmap
->eq
[i
] + 1, dim
, relevant
)) {
2353 bmap
= isl_basic_map_cow(bmap
);
2354 if (isl_basic_map_drop_equality(bmap
, i
) < 0)
2355 return isl_basic_map_free(bmap
);
2358 for (i
= bmap
->n_ineq
- 1; i
>= 0; --i
)
2359 if (!is_related(bmap
->ineq
[i
] + 1, dim
, relevant
)) {
2360 bmap
= isl_basic_map_cow(bmap
);
2361 if (isl_basic_map_drop_inequality(bmap
, i
) < 0)
2362 return isl_basic_map_free(bmap
);
2368 /* Update the groups in "group" based on the (linear part of a) constraint "c".
2370 * In particular, for any variable involved in the constraint,
2371 * find the actual group id from before and replace the group
2372 * of the corresponding variable by the minimal group of all
2373 * the variables involved in the constraint considered so far
2374 * (if this minimum is smaller) or replace the minimum by this group
2375 * (if the minimum is larger).
2377 * At the end, all the variables in "c" will (indirectly) point
2378 * to the minimal of the groups that they referred to originally.
2380 static void update_groups(int dim
, int *group
, isl_int
*c
)
2385 for (j
= 0; j
< dim
; ++j
) {
2386 if (isl_int_is_zero(c
[j
]))
2388 while (group
[j
] >= 0 && group
[group
[j
]] != group
[j
])
2389 group
[j
] = group
[group
[j
]];
2390 if (group
[j
] == min
)
2392 if (group
[j
] < min
) {
2393 if (min
>= 0 && min
< dim
)
2394 group
[min
] = group
[j
];
2397 group
[group
[j
]] = min
;
2401 /* Allocate an array of groups of variables, one for each variable
2402 * in "context", initialized to zero.
2404 static int *alloc_groups(__isl_keep isl_basic_set
*context
)
2409 dim
= isl_basic_set_dim(context
, isl_dim_set
);
2412 ctx
= isl_basic_set_get_ctx(context
);
2413 return isl_calloc_array(ctx
, int, dim
);
2416 /* Drop constraints from "bmap" that only involve variables that are
2417 * not related to any of the variables marked with a "-1" in "group".
2419 * We construct groups of variables that collect variables that
2420 * (indirectly) appear in some common constraint of "bmap".
2421 * Each group is identified by the first variable in the group,
2422 * except for the special group of variables that was already identified
2423 * in the input as -1 (or are related to those variables).
2424 * If group[i] is equal to i (or -1), then the group of i is i (or -1),
2425 * otherwise the group of i is the group of group[i].
2427 * We first initialize groups for the remaining variables.
2428 * Then we iterate over the constraints of "bmap" and update the
2429 * group of the variables in the constraint by the smallest group.
2430 * Finally, we resolve indirect references to groups by running over
2433 * After computing the groups, we drop constraints that do not involve
2434 * any variables in the -1 group.
2436 __isl_give isl_basic_map
*isl_basic_map_drop_unrelated_constraints(
2437 __isl_take isl_basic_map
*bmap
, __isl_take
int *group
)
2443 dim
= isl_basic_map_dim(bmap
, isl_dim_all
);
2445 return isl_basic_map_free(bmap
);
2448 for (i
= 0; i
< dim
; ++i
)
2450 last
= group
[i
] = i
;
2456 for (i
= 0; i
< bmap
->n_eq
; ++i
)
2457 update_groups(dim
, group
, bmap
->eq
[i
] + 1);
2458 for (i
= 0; i
< bmap
->n_ineq
; ++i
)
2459 update_groups(dim
, group
, bmap
->ineq
[i
] + 1);
2461 for (i
= 0; i
< dim
; ++i
)
2463 group
[i
] = group
[group
[i
]];
2465 for (i
= 0; i
< dim
; ++i
)
2466 group
[i
] = group
[i
] == -1;
2468 bmap
= drop_unrelated_constraints(bmap
, group
);
2474 /* Drop constraints from "context" that are irrelevant for computing
2475 * the gist of "bset".
2477 * In particular, drop constraints in variables that are not related
2478 * to any of the variables involved in the constraints of "bset"
2479 * in the sense that there is no sequence of constraints that connects them.
2481 * We first mark all variables that appear in "bset" as belonging
2482 * to a "-1" group and then continue with group_and_drop_irrelevant_constraints.
2484 static __isl_give isl_basic_set
*drop_irrelevant_constraints(
2485 __isl_take isl_basic_set
*context
, __isl_keep isl_basic_set
*bset
)
2491 dim
= isl_basic_set_dim(bset
, isl_dim_set
);
2492 if (!context
|| dim
< 0)
2493 return isl_basic_set_free(context
);
2495 group
= alloc_groups(context
);
2498 return isl_basic_set_free(context
);
2500 for (i
= 0; i
< dim
; ++i
) {
2501 for (j
= 0; j
< bset
->n_eq
; ++j
)
2502 if (!isl_int_is_zero(bset
->eq
[j
][1 + i
]))
2504 if (j
< bset
->n_eq
) {
2508 for (j
= 0; j
< bset
->n_ineq
; ++j
)
2509 if (!isl_int_is_zero(bset
->ineq
[j
][1 + i
]))
2511 if (j
< bset
->n_ineq
)
2515 return isl_basic_map_drop_unrelated_constraints(context
, group
);
2518 /* Drop constraints from "context" that are irrelevant for computing
2519 * the gist of the inequalities "ineq".
2520 * Inequalities in "ineq" for which the corresponding element of row
2521 * is set to -1 have already been marked for removal and should be ignored.
2523 * In particular, drop constraints in variables that are not related
2524 * to any of the variables involved in "ineq"
2525 * in the sense that there is no sequence of constraints that connects them.
2527 * We first mark all variables that appear in "bset" as belonging
2528 * to a "-1" group and then continue with group_and_drop_irrelevant_constraints.
2530 static __isl_give isl_basic_set
*drop_irrelevant_constraints_marked(
2531 __isl_take isl_basic_set
*context
, __isl_keep isl_mat
*ineq
, int *row
)
2538 dim
= isl_basic_set_dim(context
, isl_dim_set
);
2539 n
= isl_mat_rows(ineq
);
2540 if (dim
< 0 || n
< 0)
2541 return isl_basic_set_free(context
);
2543 group
= alloc_groups(context
);
2546 return isl_basic_set_free(context
);
2548 for (i
= 0; i
< dim
; ++i
) {
2549 for (j
= 0; j
< n
; ++j
) {
2552 if (!isl_int_is_zero(ineq
->row
[j
][1 + i
]))
2559 return isl_basic_map_drop_unrelated_constraints(context
, group
);
2562 /* Do all "n" entries of "row" contain a negative value?
2564 static int all_neg(int *row
, int n
)
2568 for (i
= 0; i
< n
; ++i
)
2575 /* Update the inequalities in "bset" based on the information in "row"
2578 * In particular, the array "row" contains either -1, meaning that
2579 * the corresponding inequality of "bset" is redundant, or the index
2580 * of an inequality in "tab".
2582 * If the row entry is -1, then drop the inequality.
2583 * Otherwise, if the constraint is marked redundant in the tableau,
2584 * then drop the inequality. Similarly, if it is marked as an equality
2585 * in the tableau, then turn the inequality into an equality and
2586 * perform Gaussian elimination.
2588 static __isl_give isl_basic_set
*update_ineq(__isl_take isl_basic_set
*bset
,
2589 __isl_keep
int *row
, struct isl_tab
*tab
)
2594 int found_equality
= 0;
2598 if (tab
&& tab
->empty
)
2599 return isl_basic_set_set_to_empty(bset
);
2601 n_ineq
= bset
->n_ineq
;
2602 for (i
= n_ineq
- 1; i
>= 0; --i
) {
2604 if (isl_basic_set_drop_inequality(bset
, i
) < 0)
2605 return isl_basic_set_free(bset
);
2611 if (isl_tab_is_equality(tab
, n_eq
+ row
[i
])) {
2612 isl_basic_map_inequality_to_equality(bset
, i
);
2614 } else if (isl_tab_is_redundant(tab
, n_eq
+ row
[i
])) {
2615 if (isl_basic_set_drop_inequality(bset
, i
) < 0)
2616 return isl_basic_set_free(bset
);
2621 bset
= isl_basic_set_gauss(bset
, NULL
);
2622 bset
= isl_basic_set_finalize(bset
);
2626 /* Update the inequalities in "bset" based on the information in "row"
2627 * and "tab" and free all arguments (other than "bset").
2629 static __isl_give isl_basic_set
*update_ineq_free(
2630 __isl_take isl_basic_set
*bset
, __isl_take isl_mat
*ineq
,
2631 __isl_take isl_basic_set
*context
, __isl_take
int *row
,
2632 struct isl_tab
*tab
)
2635 isl_basic_set_free(context
);
2637 bset
= update_ineq(bset
, row
, tab
);
2644 /* Remove all information from bset that is redundant in the context
2646 * "ineq" contains the (possibly transformed) inequalities of "bset",
2647 * in the same order.
2648 * The (explicit) equalities of "bset" are assumed to have been taken
2649 * into account by the transformation such that only the inequalities
2651 * "context" is assumed not to be empty.
2653 * "row" keeps track of the constraint index of a "bset" inequality in "tab".
2654 * A value of -1 means that the inequality is obviously redundant and may
2655 * not even appear in "tab".
2657 * We first mark the inequalities of "bset"
2658 * that are obviously redundant with respect to some inequality in "context".
2659 * Then we remove those constraints from "context" that have become
2660 * irrelevant for computing the gist of "bset".
2661 * Note that this removal of constraints cannot be replaced by
2662 * a factorization because factors in "bset" may still be connected
2663 * to each other through constraints in "context".
2665 * If there are any inequalities left, we construct a tableau for
2666 * the context and then add the inequalities of "bset".
2667 * Before adding these inequalities, we freeze all constraints such that
2668 * they won't be considered redundant in terms of the constraints of "bset".
2669 * Then we detect all redundant constraints (among the
2670 * constraints that weren't frozen), first by checking for redundancy in the
2671 * the tableau and then by checking if replacing a constraint by its negation
2672 * would lead to an empty set. This last step is fairly expensive
2673 * and could be optimized by more reuse of the tableau.
2674 * Finally, we update bset according to the results.
2676 static __isl_give isl_basic_set
*uset_gist_full(__isl_take isl_basic_set
*bset
,
2677 __isl_take isl_mat
*ineq
, __isl_take isl_basic_set
*context
)
2682 isl_basic_set
*combined
= NULL
;
2683 struct isl_tab
*tab
= NULL
;
2684 unsigned n_eq
, context_ineq
;
2686 if (!bset
|| !ineq
|| !context
)
2689 if (bset
->n_ineq
== 0 || isl_basic_set_plain_is_universe(context
)) {
2690 isl_basic_set_free(context
);
2695 ctx
= isl_basic_set_get_ctx(context
);
2696 row
= isl_calloc_array(ctx
, int, bset
->n_ineq
);
2700 if (mark_shifted_constraints(ineq
, context
, row
) < 0)
2702 if (all_neg(row
, bset
->n_ineq
))
2703 return update_ineq_free(bset
, ineq
, context
, row
, NULL
);
2705 context
= drop_irrelevant_constraints_marked(context
, ineq
, row
);
2708 if (isl_basic_set_plain_is_universe(context
))
2709 return update_ineq_free(bset
, ineq
, context
, row
, NULL
);
2711 n_eq
= context
->n_eq
;
2712 context_ineq
= context
->n_ineq
;
2713 combined
= isl_basic_set_cow(isl_basic_set_copy(context
));
2714 combined
= isl_basic_set_extend_constraints(combined
, 0, bset
->n_ineq
);
2715 tab
= isl_tab_from_basic_set(combined
, 0);
2716 for (i
= 0; i
< context_ineq
; ++i
)
2717 if (isl_tab_freeze_constraint(tab
, n_eq
+ i
) < 0)
2719 if (isl_tab_extend_cons(tab
, bset
->n_ineq
) < 0)
2722 for (i
= 0; i
< bset
->n_ineq
; ++i
) {
2725 combined
= isl_basic_set_add_ineq(combined
, ineq
->row
[i
]);
2726 if (isl_tab_add_ineq(tab
, ineq
->row
[i
]) < 0)
2730 if (isl_tab_detect_implicit_equalities(tab
) < 0)
2732 if (isl_tab_detect_redundant(tab
) < 0)
2734 for (i
= bset
->n_ineq
- 1; i
>= 0; --i
) {
2735 isl_basic_set
*test
;
2741 if (tab
->con
[n_eq
+ r
].is_redundant
)
2743 test
= isl_basic_set_dup(combined
);
2744 test
= isl_inequality_negate(test
, r
);
2745 test
= isl_basic_set_update_from_tab(test
, tab
);
2746 is_empty
= isl_basic_set_is_empty(test
);
2747 isl_basic_set_free(test
);
2751 tab
->con
[n_eq
+ r
].is_redundant
= 1;
2753 bset
= update_ineq_free(bset
, ineq
, context
, row
, tab
);
2755 ISL_F_SET(bset
, ISL_BASIC_SET_NO_IMPLICIT
);
2756 ISL_F_SET(bset
, ISL_BASIC_SET_NO_REDUNDANT
);
2759 isl_basic_set_free(combined
);
2765 isl_basic_set_free(combined
);
2766 isl_basic_set_free(context
);
2767 isl_basic_set_free(bset
);
2771 /* Extract the inequalities of "bset" as an isl_mat.
2773 static __isl_give isl_mat
*extract_ineq(__isl_keep isl_basic_set
*bset
)
2779 total
= isl_basic_set_dim(bset
, isl_dim_all
);
2783 ctx
= isl_basic_set_get_ctx(bset
);
2784 ineq
= isl_mat_sub_alloc6(ctx
, bset
->ineq
, 0, bset
->n_ineq
,
2790 /* Remove all information from "bset" that is redundant in the context
2791 * of "context", for the case where both "bset" and "context" are
2794 static __isl_give isl_basic_set
*uset_gist_uncompressed(
2795 __isl_take isl_basic_set
*bset
, __isl_take isl_basic_set
*context
)
2799 ineq
= extract_ineq(bset
);
2800 return uset_gist_full(bset
, ineq
, context
);
2803 /* Replace "bset" by an empty basic set in the same space.
2805 static __isl_give isl_basic_set
*replace_by_empty(
2806 __isl_take isl_basic_set
*bset
)
2810 space
= isl_basic_set_get_space(bset
);
2811 isl_basic_set_free(bset
);
2812 return isl_basic_set_empty(space
);
2815 /* Remove all information from "bset" that is redundant in the context
2816 * of "context", for the case where the combined equalities of
2817 * "bset" and "context" allow for a compression that can be obtained
2818 * by preapplication of "T".
2819 * If the compression of "context" is empty, meaning that "bset" and
2820 * "context" do not intersect, then return the empty set.
2822 * "bset" itself is not transformed by "T". Instead, the inequalities
2823 * are extracted from "bset" and those are transformed by "T".
2824 * uset_gist_full then determines which of the transformed inequalities
2825 * are redundant with respect to the transformed "context" and removes
2826 * the corresponding inequalities from "bset".
2828 * After preapplying "T" to the inequalities, any common factor is
2829 * removed from the coefficients. If this results in a tightening
2830 * of the constant term, then the same tightening is applied to
2831 * the corresponding untransformed inequality in "bset".
2832 * That is, if after plugging in T, a constraint f(x) >= 0 is of the form
2836 * with 0 <= r < g, then it is equivalent to
2840 * This means that f(x) >= 0 is equivalent to f(x) - r >= 0 in the affine
2841 * subspace compressed by T since the latter would be transformed to
2845 static __isl_give isl_basic_set
*uset_gist_compressed(
2846 __isl_take isl_basic_set
*bset
, __isl_take isl_basic_set
*context
,
2847 __isl_take isl_mat
*T
)
2852 isl_size n_row
, n_col
;
2855 ineq
= extract_ineq(bset
);
2856 ineq
= isl_mat_product(ineq
, isl_mat_copy(T
));
2857 context
= isl_basic_set_preimage(context
, T
);
2859 if (!ineq
|| !context
)
2861 if (isl_basic_set_plain_is_empty(context
)) {
2863 isl_basic_set_free(context
);
2864 return replace_by_empty(bset
);
2867 ctx
= isl_mat_get_ctx(ineq
);
2868 n_row
= isl_mat_rows(ineq
);
2869 n_col
= isl_mat_cols(ineq
);
2870 if (n_row
< 0 || n_col
< 0)
2873 for (i
= 0; i
< n_row
; ++i
) {
2874 isl_seq_gcd(ineq
->row
[i
] + 1, n_col
- 1, &ctx
->normalize_gcd
);
2875 if (isl_int_is_zero(ctx
->normalize_gcd
))
2877 if (isl_int_is_one(ctx
->normalize_gcd
))
2879 isl_seq_scale_down(ineq
->row
[i
] + 1, ineq
->row
[i
] + 1,
2880 ctx
->normalize_gcd
, n_col
- 1);
2881 isl_int_fdiv_r(rem
, ineq
->row
[i
][0], ctx
->normalize_gcd
);
2882 isl_int_fdiv_q(ineq
->row
[i
][0],
2883 ineq
->row
[i
][0], ctx
->normalize_gcd
);
2884 if (isl_int_is_zero(rem
))
2886 bset
= isl_basic_set_cow(bset
);
2889 isl_int_sub(bset
->ineq
[i
][0], bset
->ineq
[i
][0], rem
);
2893 return uset_gist_full(bset
, ineq
, context
);
2896 isl_basic_set_free(context
);
2897 isl_basic_set_free(bset
);
2901 /* Project "bset" onto the variables that are involved in "template".
2903 static __isl_give isl_basic_set
*project_onto_involved(
2904 __isl_take isl_basic_set
*bset
, __isl_keep isl_basic_set
*template)
2909 n
= isl_basic_set_dim(template, isl_dim_set
);
2910 if (n
< 0 || !template)
2911 return isl_basic_set_free(bset
);
2913 for (i
= 0; i
< n
; ++i
) {
2916 involved
= isl_basic_set_involves_dims(template,
2919 return isl_basic_set_free(bset
);
2922 bset
= isl_basic_set_eliminate_vars(bset
, i
, 1);
2928 /* Remove all information from bset that is redundant in the context
2929 * of context. In particular, equalities that are linear combinations
2930 * of those in context are removed. Then the inequalities that are
2931 * redundant in the context of the equalities and inequalities of
2932 * context are removed.
2934 * First of all, we drop those constraints from "context"
2935 * that are irrelevant for computing the gist of "bset".
2936 * Alternatively, we could factorize the intersection of "context" and "bset".
2938 * We first compute the intersection of the integer affine hulls
2939 * of "bset" and "context",
2940 * compute the gist inside this intersection and then reduce
2941 * the constraints with respect to the equalities of the context
2942 * that only involve variables already involved in the input.
2943 * If the intersection of the affine hulls turns out to be empty,
2944 * then return the empty set.
2946 * If two constraints are mutually redundant, then uset_gist_full
2947 * will remove the second of those constraints. We therefore first
2948 * sort the constraints so that constraints not involving existentially
2949 * quantified variables are given precedence over those that do.
2950 * We have to perform this sorting before the variable compression,
2951 * because that may effect the order of the variables.
2953 static __isl_give isl_basic_set
*uset_gist(__isl_take isl_basic_set
*bset
,
2954 __isl_take isl_basic_set
*context
)
2959 isl_basic_set
*aff_context
;
2962 total
= isl_basic_set_dim(bset
, isl_dim_all
);
2963 if (total
< 0 || !context
)
2966 context
= drop_irrelevant_constraints(context
, bset
);
2968 bset
= isl_basic_set_detect_equalities(bset
);
2969 aff
= isl_basic_set_copy(bset
);
2970 aff
= isl_basic_set_plain_affine_hull(aff
);
2971 context
= isl_basic_set_detect_equalities(context
);
2972 aff_context
= isl_basic_set_copy(context
);
2973 aff_context
= isl_basic_set_plain_affine_hull(aff_context
);
2974 aff
= isl_basic_set_intersect(aff
, aff_context
);
2977 if (isl_basic_set_plain_is_empty(aff
)) {
2978 isl_basic_set_free(bset
);
2979 isl_basic_set_free(context
);
2982 bset
= isl_basic_set_sort_constraints(bset
);
2983 if (aff
->n_eq
== 0) {
2984 isl_basic_set_free(aff
);
2985 return uset_gist_uncompressed(bset
, context
);
2987 eq
= isl_mat_sub_alloc6(bset
->ctx
, aff
->eq
, 0, aff
->n_eq
, 0, 1 + total
);
2988 eq
= isl_mat_cow(eq
);
2989 T
= isl_mat_variable_compression(eq
, NULL
);
2990 isl_basic_set_free(aff
);
2991 if (T
&& T
->n_col
== 0) {
2993 isl_basic_set_free(context
);
2994 return replace_by_empty(bset
);
2997 aff_context
= isl_basic_set_affine_hull(isl_basic_set_copy(context
));
2998 aff_context
= project_onto_involved(aff_context
, bset
);
3000 bset
= uset_gist_compressed(bset
, context
, T
);
3001 bset
= isl_basic_set_reduce_using_equalities(bset
, aff_context
);
3004 ISL_F_SET(bset
, ISL_BASIC_SET_NO_IMPLICIT
);
3005 ISL_F_SET(bset
, ISL_BASIC_SET_NO_REDUNDANT
);
3010 isl_basic_set_free(bset
);
3011 isl_basic_set_free(context
);
3015 /* Return the number of equality constraints in "bmap" that involve
3016 * local variables. This function assumes that Gaussian elimination
3017 * has been applied to the equality constraints.
3019 static int n_div_eq(__isl_keep isl_basic_map
*bmap
)
3022 isl_size total
, n_div
;
3027 if (bmap
->n_eq
== 0)
3030 total
= isl_basic_map_dim(bmap
, isl_dim_all
);
3031 n_div
= isl_basic_map_dim(bmap
, isl_dim_div
);
3032 if (total
< 0 || n_div
< 0)
3036 for (i
= 0; i
< bmap
->n_eq
; ++i
)
3037 if (isl_seq_first_non_zero(bmap
->eq
[i
] + 1 + total
,
3044 /* Construct a basic map in "space" defined by the equality constraints in "eq".
3045 * The constraints are assumed not to involve any local variables.
3047 static __isl_give isl_basic_map
*basic_map_from_equalities(
3048 __isl_take isl_space
*space
, __isl_take isl_mat
*eq
)
3052 isl_basic_map
*bmap
= NULL
;
3054 total
= isl_space_dim(space
, isl_dim_all
);
3055 if (total
< 0 || !eq
)
3058 if (1 + total
!= eq
->n_col
)
3059 isl_die(isl_space_get_ctx(space
), isl_error_internal
,
3060 "unexpected number of columns", goto error
);
3062 bmap
= isl_basic_map_alloc_space(isl_space_copy(space
),
3064 for (i
= 0; i
< eq
->n_row
; ++i
) {
3065 k
= isl_basic_map_alloc_equality(bmap
);
3068 isl_seq_cpy(bmap
->eq
[k
], eq
->row
[i
], eq
->n_col
);
3071 isl_space_free(space
);
3075 isl_space_free(space
);
3077 isl_basic_map_free(bmap
);
3081 /* Construct and return a variable compression based on the equality
3082 * constraints in "bmap1" and "bmap2" that do not involve the local variables.
3083 * "n1" is the number of (initial) equality constraints in "bmap1"
3084 * that do involve local variables.
3085 * "n2" is the number of (initial) equality constraints in "bmap2"
3086 * that do involve local variables.
3087 * "total" is the total number of other variables.
3088 * This function assumes that Gaussian elimination
3089 * has been applied to the equality constraints in both "bmap1" and "bmap2"
3090 * such that the equality constraints not involving local variables
3091 * are those that start at "n1" or "n2".
3093 * If either of "bmap1" and "bmap2" does not have such equality constraints,
3094 * then simply compute the compression based on the equality constraints
3095 * in the other basic map.
3096 * Otherwise, combine the equality constraints from both into a new
3097 * basic map such that Gaussian elimination can be applied to this combination
3098 * and then construct a variable compression from the resulting
3099 * equality constraints.
3101 static __isl_give isl_mat
*combined_variable_compression(
3102 __isl_keep isl_basic_map
*bmap1
, int n1
,
3103 __isl_keep isl_basic_map
*bmap2
, int n2
, int total
)
3106 isl_mat
*E1
, *E2
, *V
;
3107 isl_basic_map
*bmap
;
3109 ctx
= isl_basic_map_get_ctx(bmap1
);
3110 if (bmap1
->n_eq
== n1
) {
3111 E2
= isl_mat_sub_alloc6(ctx
, bmap2
->eq
,
3112 n2
, bmap2
->n_eq
- n2
, 0, 1 + total
);
3113 return isl_mat_variable_compression(E2
, NULL
);
3115 if (bmap2
->n_eq
== n2
) {
3116 E1
= isl_mat_sub_alloc6(ctx
, bmap1
->eq
,
3117 n1
, bmap1
->n_eq
- n1
, 0, 1 + total
);
3118 return isl_mat_variable_compression(E1
, NULL
);
3120 E1
= isl_mat_sub_alloc6(ctx
, bmap1
->eq
,
3121 n1
, bmap1
->n_eq
- n1
, 0, 1 + total
);
3122 E2
= isl_mat_sub_alloc6(ctx
, bmap2
->eq
,
3123 n2
, bmap2
->n_eq
- n2
, 0, 1 + total
);
3124 E1
= isl_mat_concat(E1
, E2
);
3125 bmap
= basic_map_from_equalities(isl_basic_map_get_space(bmap1
), E1
);
3126 bmap
= isl_basic_map_gauss(bmap
, NULL
);
3129 E1
= isl_mat_sub_alloc6(ctx
, bmap
->eq
, 0, bmap
->n_eq
, 0, 1 + total
);
3130 V
= isl_mat_variable_compression(E1
, NULL
);
3131 isl_basic_map_free(bmap
);
3136 /* Extract the stride constraints from "bmap", compressed
3137 * with respect to both the stride constraints in "context" and
3138 * the remaining equality constraints in both "bmap" and "context".
3139 * "bmap_n_eq" is the number of (initial) stride constraints in "bmap".
3140 * "context_n_eq" is the number of (initial) stride constraints in "context".
3142 * Let x be all variables in "bmap" (and "context") other than the local
3143 * variables. First compute a variable compression
3147 * based on the non-stride equality constraints in "bmap" and "context".
3148 * Consider the stride constraints of "context",
3152 * with y the local variables and plug in the variable compression,
3155 * A(V x') + B(y) = 0
3157 * Use these constraints to compute a parameter compression on x'
3161 * Now consider the stride constraints of "bmap"
3165 * and plug in x = V*T x''.
3166 * That is, return A = [C*V*T D].
3168 static __isl_give isl_mat
*extract_compressed_stride_constraints(
3169 __isl_keep isl_basic_map
*bmap
, int bmap_n_eq
,
3170 __isl_keep isl_basic_map
*context
, int context_n_eq
)
3172 isl_size total
, n_div
;
3174 isl_mat
*A
, *B
, *T
, *V
;
3176 total
= isl_basic_map_dim(context
, isl_dim_all
);
3177 n_div
= isl_basic_map_dim(context
, isl_dim_div
);
3178 if (total
< 0 || n_div
< 0)
3182 ctx
= isl_basic_map_get_ctx(bmap
);
3184 V
= combined_variable_compression(bmap
, bmap_n_eq
,
3185 context
, context_n_eq
, total
);
3187 A
= isl_mat_sub_alloc6(ctx
, context
->eq
, 0, context_n_eq
, 0, 1 + total
);
3188 B
= isl_mat_sub_alloc6(ctx
, context
->eq
,
3189 0, context_n_eq
, 1 + total
, n_div
);
3190 A
= isl_mat_product(A
, isl_mat_copy(V
));
3191 T
= isl_mat_parameter_compression_ext(A
, B
);
3192 T
= isl_mat_product(V
, T
);
3194 n_div
= isl_basic_map_dim(bmap
, isl_dim_div
);
3196 T
= isl_mat_free(T
);
3198 T
= isl_mat_diagonal(T
, isl_mat_identity(ctx
, n_div
));
3200 A
= isl_mat_sub_alloc6(ctx
, bmap
->eq
,
3201 0, bmap_n_eq
, 0, 1 + total
+ n_div
);
3202 A
= isl_mat_product(A
, T
);
3207 /* Remove the prime factors from *g that have an exponent that
3208 * is strictly smaller than the exponent in "c".
3209 * All exponents in *g are known to be smaller than or equal
3212 * That is, if *g is equal to
3214 * p_1^{e_1} p_2^{e_2} ... p_n^{e_n}
3216 * and "c" is equal to
3218 * p_1^{f_1} p_2^{f_2} ... p_n^{f_n}
3222 * p_1^{e_1 * (e_1 = f_1)} p_2^{e_2 * (e_2 = f_2)} ...
3223 * p_n^{e_n * (e_n = f_n)}
3225 * If e_i = f_i, then c / *g does not have any p_i factors and therefore
3226 * neither does the gcd of *g and c / *g.
3227 * If e_i < f_i, then the gcd of *g and c / *g has a positive
3228 * power min(e_i, s_i) of p_i with s_i = f_i - e_i among its factors.
3229 * Dividing *g by this gcd therefore strictly reduces the exponent
3230 * of the prime factors that need to be removed, while leaving the
3231 * other prime factors untouched.
3232 * Repeating this process until gcd(*g, c / *g) = 1 therefore
3233 * removes all undesired factors, without removing any others.
3235 static void remove_incomplete_powers(isl_int
*g
, isl_int c
)
3241 isl_int_divexact(t
, c
, *g
);
3242 isl_int_gcd(t
, t
, *g
);
3243 if (isl_int_is_one(t
))
3245 isl_int_divexact(*g
, *g
, t
);
3250 /* Reduce the "n" stride constraints in "bmap" based on a copy "A"
3251 * of the same stride constraints in a compressed space that exploits
3252 * all equalities in the context and the other equalities in "bmap".
3254 * If the stride constraints of "bmap" are of the form
3258 * then A is of the form
3262 * If any of these constraints involves only a single local variable y,
3263 * then the constraint appears as
3273 * Let g be the gcd of m and the coefficients of h.
3274 * Then, in particular, g is a divisor of the coefficients of h and
3278 * is known to be a multiple of g.
3279 * If some prime factor in m appears with the same exponent in g,
3280 * then it can be removed from m because f(x) is already known
3281 * to be a multiple of g and therefore in particular of this power
3282 * of the prime factors.
3283 * Prime factors that appear with a smaller exponent in g cannot
3284 * be removed from m.
3285 * Let g' be the divisor of g containing all prime factors that
3286 * appear with the same exponent in m and g, then
3290 * can be replaced by
3292 * f(x) + m/g' y_i' = 0
3294 * Note that (if g' != 1) this changes the explicit representation
3295 * of y_i to that of y_i', so the integer division at position i
3296 * is marked unknown and later recomputed by a call to
3297 * isl_basic_map_gauss.
3299 static __isl_give isl_basic_map
*reduce_stride_constraints(
3300 __isl_take isl_basic_map
*bmap
, int n
, __isl_keep isl_mat
*A
)
3303 isl_size total
, n_div
;
3307 total
= isl_basic_map_dim(bmap
, isl_dim_all
);
3308 n_div
= isl_basic_map_dim(bmap
, isl_dim_div
);
3309 if (total
< 0 || n_div
< 0 || !A
)
3310 return isl_basic_map_free(bmap
);
3314 for (i
= 0; i
< n
; ++i
) {
3317 div
= isl_seq_first_non_zero(bmap
->eq
[i
] + 1 + total
, n_div
);
3319 isl_die(isl_basic_map_get_ctx(bmap
), isl_error_internal
,
3320 "equality constraints modified unexpectedly",
3322 if (isl_seq_first_non_zero(bmap
->eq
[i
] + 1 + total
+ div
+ 1,
3323 n_div
- div
- 1) != -1)
3325 if (isl_mat_row_gcd(A
, i
, &gcd
) < 0)
3327 if (isl_int_is_one(gcd
))
3329 remove_incomplete_powers(&gcd
, bmap
->eq
[i
][1 + total
+ div
]);
3330 if (isl_int_is_one(gcd
))
3332 isl_int_divexact(bmap
->eq
[i
][1 + total
+ div
],
3333 bmap
->eq
[i
][1 + total
+ div
], gcd
);
3334 bmap
= isl_basic_map_mark_div_unknown(bmap
, div
);
3342 bmap
= isl_basic_map_gauss(bmap
, NULL
);
3347 isl_basic_map_free(bmap
);
3351 /* Simplify the stride constraints in "bmap" based on
3352 * the remaining equality constraints in "bmap" and all equality
3353 * constraints in "context".
3354 * Only do this if both "bmap" and "context" have stride constraints.
3356 * First extract a copy of the stride constraints in "bmap" in a compressed
3357 * space exploiting all the other equality constraints and then
3358 * use this compressed copy to simplify the original stride constraints.
3360 static __isl_give isl_basic_map
*gist_strides(__isl_take isl_basic_map
*bmap
,
3361 __isl_keep isl_basic_map
*context
)
3363 int bmap_n_eq
, context_n_eq
;
3366 if (!bmap
|| !context
)
3367 return isl_basic_map_free(bmap
);
3369 bmap_n_eq
= n_div_eq(bmap
);
3370 context_n_eq
= n_div_eq(context
);
3372 if (bmap_n_eq
< 0 || context_n_eq
< 0)
3373 return isl_basic_map_free(bmap
);
3374 if (bmap_n_eq
== 0 || context_n_eq
== 0)
3377 A
= extract_compressed_stride_constraints(bmap
, bmap_n_eq
,
3378 context
, context_n_eq
);
3379 bmap
= reduce_stride_constraints(bmap
, bmap_n_eq
, A
);
3386 /* Return a basic map that has the same intersection with "context" as "bmap"
3387 * and that is as "simple" as possible.
3389 * The core computation is performed on the pure constraints.
3390 * When we add back the meaning of the integer divisions, we need
3391 * to (re)introduce the div constraints. If we happen to have
3392 * discovered that some of these integer divisions are equal to
3393 * some affine combination of other variables, then these div
3394 * constraints may end up getting simplified in terms of the equalities,
3395 * resulting in extra inequalities on the other variables that
3396 * may have been removed already or that may not even have been
3397 * part of the input. We try and remove those constraints of
3398 * this form that are most obviously redundant with respect to
3399 * the context. We also remove those div constraints that are
3400 * redundant with respect to the other constraints in the result.
3402 * The stride constraints among the equality constraints in "bmap" are
3403 * also simplified with respecting to the other equality constraints
3404 * in "bmap" and with respect to all equality constraints in "context".
3406 __isl_give isl_basic_map
*isl_basic_map_gist(__isl_take isl_basic_map
*bmap
,
3407 __isl_take isl_basic_map
*context
)
3409 isl_basic_set
*bset
, *eq
;
3410 isl_basic_map
*eq_bmap
;
3411 isl_size total
, n_div
, n_div_bmap
;
3412 unsigned extra
, n_eq
, n_ineq
;
3414 if (!bmap
|| !context
)
3417 if (isl_basic_map_plain_is_universe(bmap
)) {
3418 isl_basic_map_free(context
);
3421 if (isl_basic_map_plain_is_empty(context
)) {
3422 isl_space
*space
= isl_basic_map_get_space(bmap
);
3423 isl_basic_map_free(bmap
);
3424 isl_basic_map_free(context
);
3425 return isl_basic_map_universe(space
);
3427 if (isl_basic_map_plain_is_empty(bmap
)) {
3428 isl_basic_map_free(context
);
3432 bmap
= isl_basic_map_remove_redundancies(bmap
);
3433 context
= isl_basic_map_remove_redundancies(context
);
3434 bmap
= isl_basic_map_order_divs(bmap
);
3435 context
= isl_basic_map_align_divs(context
, bmap
);
3437 n_div
= isl_basic_map_dim(context
, isl_dim_div
);
3438 total
= isl_basic_map_dim(bmap
, isl_dim_all
);
3439 n_div_bmap
= isl_basic_map_dim(bmap
, isl_dim_div
);
3440 if (n_div
< 0 || total
< 0 || n_div_bmap
< 0)
3442 extra
= n_div
- n_div_bmap
;
3444 bset
= isl_basic_map_underlying_set(isl_basic_map_copy(bmap
));
3445 bset
= isl_basic_set_add_dims(bset
, isl_dim_set
, extra
);
3446 bset
= uset_gist(bset
,
3447 isl_basic_map_underlying_set(isl_basic_map_copy(context
)));
3448 bset
= isl_basic_set_project_out(bset
, isl_dim_set
, total
, extra
);
3450 if (!bset
|| bset
->n_eq
== 0 || n_div
== 0 ||
3451 isl_basic_set_plain_is_empty(bset
)) {
3452 isl_basic_map_free(context
);
3453 return isl_basic_map_overlying_set(bset
, bmap
);
3457 n_ineq
= bset
->n_ineq
;
3458 eq
= isl_basic_set_copy(bset
);
3459 eq
= isl_basic_set_cow(eq
);
3460 eq
= isl_basic_set_free_inequality(eq
, n_ineq
);
3461 bset
= isl_basic_set_free_equality(bset
, n_eq
);
3463 eq_bmap
= isl_basic_map_overlying_set(eq
, isl_basic_map_copy(bmap
));
3464 eq_bmap
= gist_strides(eq_bmap
, context
);
3465 eq_bmap
= isl_basic_map_remove_shifted_constraints(eq_bmap
, context
);
3466 bmap
= isl_basic_map_overlying_set(bset
, bmap
);
3467 bmap
= isl_basic_map_intersect(bmap
, eq_bmap
);
3468 bmap
= isl_basic_map_remove_redundancies(bmap
);
3472 isl_basic_map_free(bmap
);
3473 isl_basic_map_free(context
);
3478 * Assumes context has no implicit divs.
3480 __isl_give isl_map
*isl_map_gist_basic_map(__isl_take isl_map
*map
,
3481 __isl_take isl_basic_map
*context
)
3485 if (!map
|| !context
)
3488 if (isl_basic_map_plain_is_empty(context
)) {
3489 isl_space
*space
= isl_map_get_space(map
);
3491 isl_basic_map_free(context
);
3492 return isl_map_universe(space
);
3495 context
= isl_basic_map_remove_redundancies(context
);
3496 map
= isl_map_cow(map
);
3497 if (isl_map_basic_map_check_equal_space(map
, context
) < 0)
3499 map
= isl_map_compute_divs(map
);
3502 for (i
= map
->n
- 1; i
>= 0; --i
) {
3503 map
->p
[i
] = isl_basic_map_gist(map
->p
[i
],
3504 isl_basic_map_copy(context
));
3507 if (isl_basic_map_plain_is_empty(map
->p
[i
])) {
3508 isl_basic_map_free(map
->p
[i
]);
3509 if (i
!= map
->n
- 1)
3510 map
->p
[i
] = map
->p
[map
->n
- 1];
3514 isl_basic_map_free(context
);
3515 ISL_F_CLR(map
, ISL_MAP_NORMALIZED
);
3519 isl_basic_map_free(context
);
3523 /* Drop all inequalities from "bmap" that also appear in "context".
3524 * "context" is assumed to have only known local variables and
3525 * the initial local variables of "bmap" are assumed to be the same
3526 * as those of "context".
3527 * The constraints of both "bmap" and "context" are assumed
3528 * to have been sorted using isl_basic_map_sort_constraints.
3530 * Run through the inequality constraints of "bmap" and "context"
3532 * If a constraint of "bmap" involves variables not in "context",
3533 * then it cannot appear in "context".
3534 * If a matching constraint is found, it is removed from "bmap".
3536 static __isl_give isl_basic_map
*drop_inequalities(
3537 __isl_take isl_basic_map
*bmap
, __isl_keep isl_basic_map
*context
)
3540 isl_size total
, bmap_total
;
3543 total
= isl_basic_map_dim(context
, isl_dim_all
);
3544 bmap_total
= isl_basic_map_dim(bmap
, isl_dim_all
);
3545 if (total
< 0 || bmap_total
< 0)
3546 return isl_basic_map_free(bmap
);
3548 extra
= bmap_total
- total
;
3550 i1
= bmap
->n_ineq
- 1;
3551 i2
= context
->n_ineq
- 1;
3552 while (bmap
&& i1
>= 0 && i2
>= 0) {
3555 if (isl_seq_first_non_zero(bmap
->ineq
[i1
] + 1 + total
,
3560 cmp
= isl_basic_map_constraint_cmp(context
, bmap
->ineq
[i1
],
3570 if (isl_int_eq(bmap
->ineq
[i1
][0], context
->ineq
[i2
][0])) {
3571 bmap
= isl_basic_map_cow(bmap
);
3572 if (isl_basic_map_drop_inequality(bmap
, i1
) < 0)
3573 bmap
= isl_basic_map_free(bmap
);
3582 /* Drop all equalities from "bmap" that also appear in "context".
3583 * "context" is assumed to have only known local variables and
3584 * the initial local variables of "bmap" are assumed to be the same
3585 * as those of "context".
3587 * Run through the equality constraints of "bmap" and "context"
3589 * If a constraint of "bmap" involves variables not in "context",
3590 * then it cannot appear in "context".
3591 * If a matching constraint is found, it is removed from "bmap".
3593 static __isl_give isl_basic_map
*drop_equalities(
3594 __isl_take isl_basic_map
*bmap
, __isl_keep isl_basic_map
*context
)
3597 isl_size total
, bmap_total
;
3600 total
= isl_basic_map_dim(context
, isl_dim_all
);
3601 bmap_total
= isl_basic_map_dim(bmap
, isl_dim_all
);
3602 if (total
< 0 || bmap_total
< 0)
3603 return isl_basic_map_free(bmap
);
3605 extra
= bmap_total
- total
;
3607 i1
= bmap
->n_eq
- 1;
3608 i2
= context
->n_eq
- 1;
3610 while (bmap
&& i1
>= 0 && i2
>= 0) {
3613 if (isl_seq_first_non_zero(bmap
->eq
[i1
] + 1 + total
,
3616 last1
= isl_seq_last_non_zero(bmap
->eq
[i1
] + 1, total
);
3617 last2
= isl_seq_last_non_zero(context
->eq
[i2
] + 1, total
);
3618 if (last1
> last2
) {
3622 if (last1
< last2
) {
3626 if (isl_seq_eq(bmap
->eq
[i1
], context
->eq
[i2
], 1 + total
)) {
3627 bmap
= isl_basic_map_cow(bmap
);
3628 if (isl_basic_map_drop_equality(bmap
, i1
) < 0)
3629 bmap
= isl_basic_map_free(bmap
);
3638 /* Remove the constraints in "context" from "bmap".
3639 * "context" is assumed to have explicit representations
3640 * for all local variables.
3642 * First align the divs of "bmap" to those of "context" and
3643 * sort the constraints. Then drop all constraints from "bmap"
3644 * that appear in "context".
3646 __isl_give isl_basic_map
*isl_basic_map_plain_gist(
3647 __isl_take isl_basic_map
*bmap
, __isl_take isl_basic_map
*context
)
3649 isl_bool done
, known
;
3651 done
= isl_basic_map_plain_is_universe(context
);
3652 if (done
== isl_bool_false
)
3653 done
= isl_basic_map_plain_is_universe(bmap
);
3654 if (done
== isl_bool_false
)
3655 done
= isl_basic_map_plain_is_empty(context
);
3656 if (done
== isl_bool_false
)
3657 done
= isl_basic_map_plain_is_empty(bmap
);
3661 isl_basic_map_free(context
);
3664 known
= isl_basic_map_divs_known(context
);
3668 isl_die(isl_basic_map_get_ctx(bmap
), isl_error_invalid
,
3669 "context has unknown divs", goto error
);
3671 context
= isl_basic_map_order_divs(context
);
3672 bmap
= isl_basic_map_align_divs(bmap
, context
);
3673 bmap
= isl_basic_map_gauss(bmap
, NULL
);
3674 bmap
= isl_basic_map_sort_constraints(bmap
);
3675 context
= isl_basic_map_sort_constraints(context
);
3677 bmap
= drop_inequalities(bmap
, context
);
3678 bmap
= drop_equalities(bmap
, context
);
3680 isl_basic_map_free(context
);
3681 bmap
= isl_basic_map_finalize(bmap
);
3684 isl_basic_map_free(bmap
);
3685 isl_basic_map_free(context
);
3689 /* Replace "map" by the disjunct at position "pos" and free "context".
3691 static __isl_give isl_map
*replace_by_disjunct(__isl_take isl_map
*map
,
3692 int pos
, __isl_take isl_basic_map
*context
)
3694 isl_basic_map
*bmap
;
3696 bmap
= isl_basic_map_copy(map
->p
[pos
]);
3698 isl_basic_map_free(context
);
3699 return isl_map_from_basic_map(bmap
);
3702 /* Remove the constraints in "context" from "map".
3703 * If any of the disjuncts in the result turns out to be the universe,
3704 * then return this universe.
3705 * "context" is assumed to have explicit representations
3706 * for all local variables.
3708 __isl_give isl_map
*isl_map_plain_gist_basic_map(__isl_take isl_map
*map
,
3709 __isl_take isl_basic_map
*context
)
3712 isl_bool univ
, known
;
3714 univ
= isl_basic_map_plain_is_universe(context
);
3718 isl_basic_map_free(context
);
3721 known
= isl_basic_map_divs_known(context
);
3725 isl_die(isl_map_get_ctx(map
), isl_error_invalid
,
3726 "context has unknown divs", goto error
);
3728 map
= isl_map_cow(map
);
3731 for (i
= 0; i
< map
->n
; ++i
) {
3732 map
->p
[i
] = isl_basic_map_plain_gist(map
->p
[i
],
3733 isl_basic_map_copy(context
));
3734 univ
= isl_basic_map_plain_is_universe(map
->p
[i
]);
3737 if (univ
&& map
->n
> 1)
3738 return replace_by_disjunct(map
, i
, context
);
3741 isl_basic_map_free(context
);
3742 ISL_F_CLR(map
, ISL_MAP_NORMALIZED
);
3744 ISL_F_CLR(map
, ISL_MAP_DISJOINT
);
3748 isl_basic_map_free(context
);
3752 /* Remove the constraints in "context" from "set".
3753 * If any of the disjuncts in the result turns out to be the universe,
3754 * then return this universe.
3755 * "context" is assumed to have explicit representations
3756 * for all local variables.
3758 __isl_give isl_set
*isl_set_plain_gist_basic_set(__isl_take isl_set
*set
,
3759 __isl_take isl_basic_set
*context
)
3761 return set_from_map(isl_map_plain_gist_basic_map(set_to_map(set
),
3762 bset_to_bmap(context
)));
3765 /* Remove the constraints in "context" from "map".
3766 * If any of the disjuncts in the result turns out to be the universe,
3767 * then return this universe.
3768 * "context" is assumed to consist of a single disjunct and
3769 * to have explicit representations for all local variables.
3771 __isl_give isl_map
*isl_map_plain_gist(__isl_take isl_map
*map
,
3772 __isl_take isl_map
*context
)
3774 isl_basic_map
*hull
;
3776 hull
= isl_map_unshifted_simple_hull(context
);
3777 return isl_map_plain_gist_basic_map(map
, hull
);
3780 /* Replace "map" by a universe map in the same space and free "drop".
3782 static __isl_give isl_map
*replace_by_universe(__isl_take isl_map
*map
,
3783 __isl_take isl_map
*drop
)
3787 res
= isl_map_universe(isl_map_get_space(map
));
3793 /* Return a map that has the same intersection with "context" as "map"
3794 * and that is as "simple" as possible.
3796 * If "map" is already the universe, then we cannot make it any simpler.
3797 * Similarly, if "context" is the universe, then we cannot exploit it
3799 * If "map" and "context" are identical to each other, then we can
3800 * return the corresponding universe.
3802 * If either "map" or "context" consists of multiple disjuncts,
3803 * then check if "context" happens to be a subset of "map",
3804 * in which case all constraints can be removed.
3805 * In case of multiple disjuncts, the standard procedure
3806 * may not be able to detect that all constraints can be removed.
3808 * If none of these cases apply, we have to work a bit harder.
3809 * During this computation, we make use of a single disjunct context,
3810 * so if the original context consists of more than one disjunct
3811 * then we need to approximate the context by a single disjunct set.
3812 * Simply taking the simple hull may drop constraints that are
3813 * only implicitly available in each disjunct. We therefore also
3814 * look for constraints among those defining "map" that are valid
3815 * for the context. These can then be used to simplify away
3816 * the corresponding constraints in "map".
3818 __isl_give isl_map
*isl_map_gist(__isl_take isl_map
*map
,
3819 __isl_take isl_map
*context
)
3823 isl_size n_disjunct_map
, n_disjunct_context
;
3825 isl_basic_map
*hull
;
3827 is_universe
= isl_map_plain_is_universe(map
);
3828 if (is_universe
>= 0 && !is_universe
)
3829 is_universe
= isl_map_plain_is_universe(context
);
3830 if (is_universe
< 0)
3833 isl_map_free(context
);
3837 isl_map_align_params_bin(&map
, &context
);
3838 equal
= isl_map_plain_is_equal(map
, context
);
3842 return replace_by_universe(map
, context
);
3844 n_disjunct_map
= isl_map_n_basic_map(map
);
3845 n_disjunct_context
= isl_map_n_basic_map(context
);
3846 if (n_disjunct_map
< 0 || n_disjunct_context
< 0)
3848 if (n_disjunct_map
!= 1 || n_disjunct_context
!= 1) {
3849 subset
= isl_map_is_subset(context
, map
);
3853 return replace_by_universe(map
, context
);
3856 context
= isl_map_compute_divs(context
);
3859 if (n_disjunct_context
== 1) {
3860 hull
= isl_map_simple_hull(context
);
3865 ctx
= isl_map_get_ctx(map
);
3866 list
= isl_map_list_alloc(ctx
, 2);
3867 list
= isl_map_list_add(list
, isl_map_copy(context
));
3868 list
= isl_map_list_add(list
, isl_map_copy(map
));
3869 hull
= isl_map_unshifted_simple_hull_from_map_list(context
,
3872 return isl_map_gist_basic_map(map
, hull
);
3875 isl_map_free(context
);
3879 __isl_give isl_basic_set
*isl_basic_set_gist(__isl_take isl_basic_set
*bset
,
3880 __isl_take isl_basic_set
*context
)
3882 return bset_from_bmap(isl_basic_map_gist(bset_to_bmap(bset
),
3883 bset_to_bmap(context
)));
3886 __isl_give isl_set
*isl_set_gist_basic_set(__isl_take isl_set
*set
,
3887 __isl_take isl_basic_set
*context
)
3889 return set_from_map(isl_map_gist_basic_map(set_to_map(set
),
3890 bset_to_bmap(context
)));
3893 __isl_give isl_set
*isl_set_gist_params_basic_set(__isl_take isl_set
*set
,
3894 __isl_take isl_basic_set
*context
)
3896 isl_space
*space
= isl_set_get_space(set
);
3897 isl_basic_set
*dom_context
= isl_basic_set_universe(space
);
3898 dom_context
= isl_basic_set_intersect_params(dom_context
, context
);
3899 return isl_set_gist_basic_set(set
, dom_context
);
3902 __isl_give isl_set
*isl_set_gist(__isl_take isl_set
*set
,
3903 __isl_take isl_set
*context
)
3905 return set_from_map(isl_map_gist(set_to_map(set
), set_to_map(context
)));
3908 /* Compute the gist of "bmap" with respect to the constraints "context"
3911 __isl_give isl_basic_map
*isl_basic_map_gist_domain(
3912 __isl_take isl_basic_map
*bmap
, __isl_take isl_basic_set
*context
)
3914 isl_space
*space
= isl_basic_map_get_space(bmap
);
3915 isl_basic_map
*bmap_context
= isl_basic_map_universe(space
);
3917 bmap_context
= isl_basic_map_intersect_domain(bmap_context
, context
);
3918 return isl_basic_map_gist(bmap
, bmap_context
);
3921 __isl_give isl_map
*isl_map_gist_domain(__isl_take isl_map
*map
,
3922 __isl_take isl_set
*context
)
3924 isl_map
*map_context
= isl_map_universe(isl_map_get_space(map
));
3925 map_context
= isl_map_intersect_domain(map_context
, context
);
3926 return isl_map_gist(map
, map_context
);
3929 __isl_give isl_map
*isl_map_gist_range(__isl_take isl_map
*map
,
3930 __isl_take isl_set
*context
)
3932 isl_map
*map_context
= isl_map_universe(isl_map_get_space(map
));
3933 map_context
= isl_map_intersect_range(map_context
, context
);
3934 return isl_map_gist(map
, map_context
);
3937 __isl_give isl_map
*isl_map_gist_params(__isl_take isl_map
*map
,
3938 __isl_take isl_set
*context
)
3940 isl_map
*map_context
= isl_map_universe(isl_map_get_space(map
));
3941 map_context
= isl_map_intersect_params(map_context
, context
);
3942 return isl_map_gist(map
, map_context
);
3945 __isl_give isl_set
*isl_set_gist_params(__isl_take isl_set
*set
,
3946 __isl_take isl_set
*context
)
3948 return isl_map_gist_params(set
, context
);
3951 /* Quick check to see if two basic maps are disjoint.
3952 * In particular, we reduce the equalities and inequalities of
3953 * one basic map in the context of the equalities of the other
3954 * basic map and check if we get a contradiction.
3956 isl_bool
isl_basic_map_plain_is_disjoint(__isl_keep isl_basic_map
*bmap1
,
3957 __isl_keep isl_basic_map
*bmap2
)
3959 struct isl_vec
*v
= NULL
;
3964 if (isl_basic_map_check_equal_space(bmap1
, bmap2
) < 0)
3965 return isl_bool_error
;
3966 if (bmap1
->n_div
|| bmap2
->n_div
)
3967 return isl_bool_false
;
3968 if (!bmap1
->n_eq
&& !bmap2
->n_eq
)
3969 return isl_bool_false
;
3971 total
= isl_space_dim(bmap1
->dim
, isl_dim_all
);
3973 return isl_bool_error
;
3975 return isl_bool_false
;
3976 v
= isl_vec_alloc(bmap1
->ctx
, 1 + total
);
3979 elim
= isl_alloc_array(bmap1
->ctx
, int, total
);
3982 compute_elimination_index(bmap1
, elim
, total
);
3983 for (i
= 0; i
< bmap2
->n_eq
; ++i
) {
3985 reduced
= reduced_using_equalities(v
->block
.data
, bmap2
->eq
[i
],
3986 bmap1
, elim
, total
);
3987 if (reduced
&& !isl_int_is_zero(v
->block
.data
[0]) &&
3988 isl_seq_first_non_zero(v
->block
.data
+ 1, total
) == -1)
3991 for (i
= 0; i
< bmap2
->n_ineq
; ++i
) {
3993 reduced
= reduced_using_equalities(v
->block
.data
,
3994 bmap2
->ineq
[i
], bmap1
, elim
, total
);
3995 if (reduced
&& isl_int_is_neg(v
->block
.data
[0]) &&
3996 isl_seq_first_non_zero(v
->block
.data
+ 1, total
) == -1)
3999 compute_elimination_index(bmap2
, elim
, total
);
4000 for (i
= 0; i
< bmap1
->n_ineq
; ++i
) {
4002 reduced
= reduced_using_equalities(v
->block
.data
,
4003 bmap1
->ineq
[i
], bmap2
, elim
, total
);
4004 if (reduced
&& isl_int_is_neg(v
->block
.data
[0]) &&
4005 isl_seq_first_non_zero(v
->block
.data
+ 1, total
) == -1)
4010 return isl_bool_false
;
4014 return isl_bool_true
;
4018 return isl_bool_error
;
4021 int isl_basic_set_plain_is_disjoint(__isl_keep isl_basic_set
*bset1
,
4022 __isl_keep isl_basic_set
*bset2
)
4024 return isl_basic_map_plain_is_disjoint(bset_to_bmap(bset1
),
4025 bset_to_bmap(bset2
));
4028 /* Does "test" hold for all pairs of basic maps in "map1" and "map2"?
4030 static isl_bool
all_pairs(__isl_keep isl_map
*map1
, __isl_keep isl_map
*map2
,
4031 isl_bool (*test
)(__isl_keep isl_basic_map
*bmap1
,
4032 __isl_keep isl_basic_map
*bmap2
))
4037 return isl_bool_error
;
4039 for (i
= 0; i
< map1
->n
; ++i
) {
4040 for (j
= 0; j
< map2
->n
; ++j
) {
4041 isl_bool d
= test(map1
->p
[i
], map2
->p
[j
]);
4042 if (d
!= isl_bool_true
)
4047 return isl_bool_true
;
4050 /* Are "map1" and "map2" obviously disjoint, based on information
4051 * that can be derived without looking at the individual basic maps?
4053 * In particular, if one of them is empty or if they live in different spaces
4054 * (ignoring parameters), then they are clearly disjoint.
4056 static isl_bool
isl_map_plain_is_disjoint_global(__isl_keep isl_map
*map1
,
4057 __isl_keep isl_map
*map2
)
4063 return isl_bool_error
;
4065 disjoint
= isl_map_plain_is_empty(map1
);
4066 if (disjoint
< 0 || disjoint
)
4069 disjoint
= isl_map_plain_is_empty(map2
);
4070 if (disjoint
< 0 || disjoint
)
4073 match
= isl_map_tuple_is_equal(map1
, isl_dim_in
, map2
, isl_dim_in
);
4074 if (match
< 0 || !match
)
4075 return match
< 0 ? isl_bool_error
: isl_bool_true
;
4077 match
= isl_map_tuple_is_equal(map1
, isl_dim_out
, map2
, isl_dim_out
);
4078 if (match
< 0 || !match
)
4079 return match
< 0 ? isl_bool_error
: isl_bool_true
;
4081 return isl_bool_false
;
4084 /* Are "map1" and "map2" obviously disjoint?
4086 * If one of them is empty or if they live in different spaces (ignoring
4087 * parameters), then they are clearly disjoint.
4088 * This is checked by isl_map_plain_is_disjoint_global.
4090 * If they have different parameters, then we skip any further tests.
4092 * If they are obviously equal, but not obviously empty, then we will
4093 * not be able to detect if they are disjoint.
4095 * Otherwise we check if each basic map in "map1" is obviously disjoint
4096 * from each basic map in "map2".
4098 isl_bool
isl_map_plain_is_disjoint(__isl_keep isl_map
*map1
,
4099 __isl_keep isl_map
*map2
)
4105 disjoint
= isl_map_plain_is_disjoint_global(map1
, map2
);
4106 if (disjoint
< 0 || disjoint
)
4109 match
= isl_map_has_equal_params(map1
, map2
);
4110 if (match
< 0 || !match
)
4111 return match
< 0 ? isl_bool_error
: isl_bool_false
;
4113 intersect
= isl_map_plain_is_equal(map1
, map2
);
4114 if (intersect
< 0 || intersect
)
4115 return intersect
< 0 ? isl_bool_error
: isl_bool_false
;
4117 return all_pairs(map1
, map2
, &isl_basic_map_plain_is_disjoint
);
4120 /* Are "map1" and "map2" disjoint?
4121 * The parameters are assumed to have been aligned.
4123 * In particular, check whether all pairs of basic maps are disjoint.
4125 static isl_bool
isl_map_is_disjoint_aligned(__isl_keep isl_map
*map1
,
4126 __isl_keep isl_map
*map2
)
4128 return all_pairs(map1
, map2
, &isl_basic_map_is_disjoint
);
4131 /* Are "map1" and "map2" disjoint?
4133 * They are disjoint if they are "obviously disjoint" or if one of them
4134 * is empty. Otherwise, they are not disjoint if one of them is universal.
4135 * If the two inputs are (obviously) equal and not empty, then they are
4137 * If none of these cases apply, then check if all pairs of basic maps
4138 * are disjoint after aligning the parameters.
4140 isl_bool
isl_map_is_disjoint(__isl_keep isl_map
*map1
, __isl_keep isl_map
*map2
)
4145 disjoint
= isl_map_plain_is_disjoint_global(map1
, map2
);
4146 if (disjoint
< 0 || disjoint
)
4149 disjoint
= isl_map_is_empty(map1
);
4150 if (disjoint
< 0 || disjoint
)
4153 disjoint
= isl_map_is_empty(map2
);
4154 if (disjoint
< 0 || disjoint
)
4157 intersect
= isl_map_plain_is_universe(map1
);
4158 if (intersect
< 0 || intersect
)
4159 return isl_bool_not(intersect
);
4161 intersect
= isl_map_plain_is_universe(map2
);
4162 if (intersect
< 0 || intersect
)
4163 return isl_bool_not(intersect
);
4165 intersect
= isl_map_plain_is_equal(map1
, map2
);
4166 if (intersect
< 0 || intersect
)
4167 return isl_bool_not(intersect
);
4169 return isl_map_align_params_map_map_and_test(map1
, map2
,
4170 &isl_map_is_disjoint_aligned
);
4173 /* Are "bmap1" and "bmap2" disjoint?
4175 * They are disjoint if they are "obviously disjoint" or if one of them
4176 * is empty. Otherwise, they are not disjoint if one of them is universal.
4177 * If none of these cases apply, we compute the intersection and see if
4178 * the result is empty.
4180 isl_bool
isl_basic_map_is_disjoint(__isl_keep isl_basic_map
*bmap1
,
4181 __isl_keep isl_basic_map
*bmap2
)
4185 isl_basic_map
*test
;
4187 disjoint
= isl_basic_map_plain_is_disjoint(bmap1
, bmap2
);
4188 if (disjoint
< 0 || disjoint
)
4191 disjoint
= isl_basic_map_is_empty(bmap1
);
4192 if (disjoint
< 0 || disjoint
)
4195 disjoint
= isl_basic_map_is_empty(bmap2
);
4196 if (disjoint
< 0 || disjoint
)
4199 intersect
= isl_basic_map_plain_is_universe(bmap1
);
4200 if (intersect
< 0 || intersect
)
4201 return isl_bool_not(intersect
);
4203 intersect
= isl_basic_map_plain_is_universe(bmap2
);
4204 if (intersect
< 0 || intersect
)
4205 return isl_bool_not(intersect
);
4207 test
= isl_basic_map_intersect(isl_basic_map_copy(bmap1
),
4208 isl_basic_map_copy(bmap2
));
4209 disjoint
= isl_basic_map_is_empty(test
);
4210 isl_basic_map_free(test
);
4215 /* Are "bset1" and "bset2" disjoint?
4217 isl_bool
isl_basic_set_is_disjoint(__isl_keep isl_basic_set
*bset1
,
4218 __isl_keep isl_basic_set
*bset2
)
4220 return isl_basic_map_is_disjoint(bset1
, bset2
);
4223 isl_bool
isl_set_plain_is_disjoint(__isl_keep isl_set
*set1
,
4224 __isl_keep isl_set
*set2
)
4226 return isl_map_plain_is_disjoint(set_to_map(set1
), set_to_map(set2
));
4229 /* Are "set1" and "set2" disjoint?
4231 isl_bool
isl_set_is_disjoint(__isl_keep isl_set
*set1
, __isl_keep isl_set
*set2
)
4233 return isl_map_is_disjoint(set1
, set2
);
4236 /* Is "v" equal to 0, 1 or -1?
4238 static int is_zero_or_one(isl_int v
)
4240 return isl_int_is_zero(v
) || isl_int_is_one(v
) || isl_int_is_negone(v
);
4243 /* Are the "n" coefficients starting at "first" of inequality constraints
4244 * "i" and "j" of "bmap" opposite to each other?
4246 static int is_opposite_part(__isl_keep isl_basic_map
*bmap
, int i
, int j
,
4249 return isl_seq_is_neg(bmap
->ineq
[i
] + first
, bmap
->ineq
[j
] + first
, n
);
4252 /* Are inequality constraints "i" and "j" of "bmap" opposite to each other,
4253 * apart from the constant term?
4255 static isl_bool
is_opposite(__isl_keep isl_basic_map
*bmap
, int i
, int j
)
4259 total
= isl_basic_map_dim(bmap
, isl_dim_all
);
4261 return isl_bool_error
;
4262 return is_opposite_part(bmap
, i
, j
, 1, total
);
4265 /* Check if we can combine a given div with lower bound l and upper
4266 * bound u with some other div and if so return that other div.
4267 * Otherwise, return a position beyond the integer divisions.
4268 * Return -1 on error.
4270 * We first check that
4271 * - the bounds are opposites of each other (except for the constant
4273 * - the bounds do not reference any other div
4274 * - no div is defined in terms of this div
4276 * Let m be the size of the range allowed on the div by the bounds.
4277 * That is, the bounds are of the form
4279 * e <= a <= e + m - 1
4281 * with e some expression in the other variables.
4282 * We look for another div b such that no third div is defined in terms
4283 * of this second div b and such that in any constraint that contains
4284 * a (except for the given lower and upper bound), also contains b
4285 * with a coefficient that is m times that of b.
4286 * That is, all constraints (except for the lower and upper bound)
4289 * e + f (a + m b) >= 0
4291 * Furthermore, in the constraints that only contain b, the coefficient
4292 * of b should be equal to 1 or -1.
4293 * If so, we return b so that "a + m b" can be replaced by
4294 * a single div "c = a + m b".
4296 static int div_find_coalesce(__isl_keep isl_basic_map
*bmap
, int *pairs
,
4297 unsigned div
, unsigned l
, unsigned u
)
4305 n_div
= isl_basic_map_dim(bmap
, isl_dim_div
);
4308 v_div
= isl_basic_map_var_offset(bmap
, isl_dim_div
);
4311 if (isl_seq_first_non_zero(bmap
->ineq
[l
] + 1 + v_div
, div
) != -1)
4313 if (isl_seq_first_non_zero(bmap
->ineq
[l
] + 1 + v_div
+ div
+ 1,
4314 n_div
- div
- 1) != -1)
4316 opp
= is_opposite(bmap
, l
, u
);
4317 if (opp
< 0 || !opp
)
4318 return opp
< 0 ? -1 : n_div
;
4320 for (i
= 0; i
< n_div
; ++i
) {
4321 if (isl_int_is_zero(bmap
->div
[i
][0]))
4323 if (!isl_int_is_zero(bmap
->div
[i
][1 + 1 + v_div
+ div
]))
4327 isl_int_add(bmap
->ineq
[l
][0], bmap
->ineq
[l
][0], bmap
->ineq
[u
][0]);
4328 if (isl_int_is_neg(bmap
->ineq
[l
][0])) {
4329 isl_int_sub(bmap
->ineq
[l
][0],
4330 bmap
->ineq
[l
][0], bmap
->ineq
[u
][0]);
4331 bmap
= isl_basic_map_copy(bmap
);
4332 bmap
= isl_basic_map_set_to_empty(bmap
);
4333 isl_basic_map_free(bmap
);
4336 isl_int_add_ui(bmap
->ineq
[l
][0], bmap
->ineq
[l
][0], 1);
4338 for (i
= 0; i
< n_div
; ++i
) {
4343 for (j
= 0; j
< n_div
; ++j
) {
4344 if (isl_int_is_zero(bmap
->div
[j
][0]))
4346 if (!isl_int_is_zero(bmap
->div
[j
][1 + 1 + v_div
+ i
]))
4351 for (j
= 0; j
< bmap
->n_ineq
; ++j
) {
4353 if (j
== l
|| j
== u
)
4355 if (isl_int_is_zero(bmap
->ineq
[j
][1 + v_div
+ div
])) {
4356 if (is_zero_or_one(bmap
->ineq
[j
][1 + v_div
+ i
]))
4360 if (isl_int_is_zero(bmap
->ineq
[j
][1 + v_div
+ i
]))
4362 isl_int_mul(bmap
->ineq
[j
][1 + v_div
+ div
],
4363 bmap
->ineq
[j
][1 + v_div
+ div
],
4365 valid
= isl_int_eq(bmap
->ineq
[j
][1 + v_div
+ div
],
4366 bmap
->ineq
[j
][1 + v_div
+ i
]);
4367 isl_int_divexact(bmap
->ineq
[j
][1 + v_div
+ div
],
4368 bmap
->ineq
[j
][1 + v_div
+ div
],
4373 if (j
< bmap
->n_ineq
)
4378 isl_int_sub_ui(bmap
->ineq
[l
][0], bmap
->ineq
[l
][0], 1);
4379 isl_int_sub(bmap
->ineq
[l
][0], bmap
->ineq
[l
][0], bmap
->ineq
[u
][0]);
4383 /* Internal data structure used during the construction and/or evaluation of
4384 * an inequality that ensures that a pair of bounds always allows
4385 * for an integer value.
4387 * "tab" is the tableau in which the inequality is evaluated. It may
4388 * be NULL until it is actually needed.
4389 * "v" contains the inequality coefficients.
4390 * "g", "fl" and "fu" are temporary scalars used during the construction and
4393 struct test_ineq_data
{
4394 struct isl_tab
*tab
;
4401 /* Free all the memory allocated by the fields of "data".
4403 static void test_ineq_data_clear(struct test_ineq_data
*data
)
4405 isl_tab_free(data
->tab
);
4406 isl_vec_free(data
->v
);
4407 isl_int_clear(data
->g
);
4408 isl_int_clear(data
->fl
);
4409 isl_int_clear(data
->fu
);
4412 /* Is the inequality stored in data->v satisfied by "bmap"?
4413 * That is, does it only attain non-negative values?
4414 * data->tab is a tableau corresponding to "bmap".
4416 static isl_bool
test_ineq_is_satisfied(__isl_keep isl_basic_map
*bmap
,
4417 struct test_ineq_data
*data
)
4420 enum isl_lp_result res
;
4422 ctx
= isl_basic_map_get_ctx(bmap
);
4424 data
->tab
= isl_tab_from_basic_map(bmap
, 0);
4425 res
= isl_tab_min(data
->tab
, data
->v
->el
, ctx
->one
, &data
->g
, NULL
, 0);
4426 if (res
== isl_lp_error
)
4427 return isl_bool_error
;
4428 return res
== isl_lp_ok
&& isl_int_is_nonneg(data
->g
);
4431 /* Given a lower and an upper bound on div i, do they always allow
4432 * for an integer value of the given div?
4433 * Determine this property by constructing an inequality
4434 * such that the property is guaranteed when the inequality is nonnegative.
4435 * The lower bound is inequality l, while the upper bound is inequality u.
4436 * The constructed inequality is stored in data->v.
4438 * Let the upper bound be
4442 * and the lower bound
4446 * Let n_u = f_u g and n_l = f_l g, with g = gcd(n_u, n_l).
4449 * - f_u e_l <= f_u f_l g a <= f_l e_u
4451 * Since all variables are integer valued, this is equivalent to
4453 * - f_u e_l - (f_u - 1) <= f_u f_l g a <= f_l e_u + (f_l - 1)
4455 * If this interval is at least f_u f_l g, then it contains at least
4456 * one integer value for a.
4457 * That is, the test constraint is
4459 * f_l e_u + f_u e_l + f_l - 1 + f_u - 1 + 1 >= f_u f_l g
4463 * f_l e_u + f_u e_l + f_l - 1 + f_u - 1 + 1 - f_u f_l g >= 0
4465 * If the coefficients of f_l e_u + f_u e_l have a common divisor g',
4466 * then the constraint can be scaled down by a factor g',
4467 * with the constant term replaced by
4468 * floor((f_l e_{u,0} + f_u e_{l,0} + f_l - 1 + f_u - 1 + 1 - f_u f_l g)/g').
4469 * Note that the result of applying Fourier-Motzkin to this pair
4472 * f_l e_u + f_u e_l >= 0
4474 * If the constant term of the scaled down version of this constraint,
4475 * i.e., floor((f_l e_{u,0} + f_u e_{l,0})/g') is equal to the constant
4476 * term of the scaled down test constraint, then the test constraint
4477 * is known to hold and no explicit evaluation is required.
4478 * This is essentially the Omega test.
4480 * If the test constraint consists of only a constant term, then
4481 * it is sufficient to look at the sign of this constant term.
4483 static isl_bool
int_between_bounds(__isl_keep isl_basic_map
*bmap
, int i
,
4484 int l
, int u
, struct test_ineq_data
*data
)
4489 offset
= isl_basic_map_offset(bmap
, isl_dim_div
);
4490 n_div
= isl_basic_map_dim(bmap
, isl_dim_div
);
4492 return isl_bool_error
;
4494 isl_int_gcd(data
->g
,
4495 bmap
->ineq
[l
][offset
+ i
], bmap
->ineq
[u
][offset
+ i
]);
4496 isl_int_divexact(data
->fl
, bmap
->ineq
[l
][offset
+ i
], data
->g
);
4497 isl_int_divexact(data
->fu
, bmap
->ineq
[u
][offset
+ i
], data
->g
);
4498 isl_int_neg(data
->fu
, data
->fu
);
4499 isl_seq_combine(data
->v
->el
, data
->fl
, bmap
->ineq
[u
],
4500 data
->fu
, bmap
->ineq
[l
], offset
+ n_div
);
4501 isl_int_mul(data
->g
, data
->g
, data
->fl
);
4502 isl_int_mul(data
->g
, data
->g
, data
->fu
);
4503 isl_int_sub(data
->g
, data
->g
, data
->fl
);
4504 isl_int_sub(data
->g
, data
->g
, data
->fu
);
4505 isl_int_add_ui(data
->g
, data
->g
, 1);
4506 isl_int_sub(data
->fl
, data
->v
->el
[0], data
->g
);
4508 isl_seq_gcd(data
->v
->el
+ 1, offset
- 1 + n_div
, &data
->g
);
4509 if (isl_int_is_zero(data
->g
))
4510 return isl_int_is_nonneg(data
->fl
);
4511 if (isl_int_is_one(data
->g
)) {
4512 isl_int_set(data
->v
->el
[0], data
->fl
);
4513 return test_ineq_is_satisfied(bmap
, data
);
4515 isl_int_fdiv_q(data
->fl
, data
->fl
, data
->g
);
4516 isl_int_fdiv_q(data
->v
->el
[0], data
->v
->el
[0], data
->g
);
4517 if (isl_int_eq(data
->fl
, data
->v
->el
[0]))
4518 return isl_bool_true
;
4519 isl_int_set(data
->v
->el
[0], data
->fl
);
4520 isl_seq_scale_down(data
->v
->el
+ 1, data
->v
->el
+ 1, data
->g
,
4521 offset
- 1 + n_div
);
4523 return test_ineq_is_satisfied(bmap
, data
);
4526 /* Remove more kinds of divs that are not strictly needed.
4527 * In particular, if all pairs of lower and upper bounds on a div
4528 * are such that they allow at least one integer value of the div,
4529 * then we can eliminate the div using Fourier-Motzkin without
4530 * introducing any spurious solutions.
4532 * If at least one of the two constraints has a unit coefficient for the div,
4533 * then the presence of such a value is guaranteed so there is no need to check.
4534 * In particular, the value attained by the bound with unit coefficient
4535 * can serve as this intermediate value.
4537 static __isl_give isl_basic_map
*drop_more_redundant_divs(
4538 __isl_take isl_basic_map
*bmap
, __isl_take
int *pairs
, int n
)
4541 struct test_ineq_data data
= { NULL
, NULL
};
4546 isl_int_init(data
.g
);
4547 isl_int_init(data
.fl
);
4548 isl_int_init(data
.fu
);
4550 n_div
= isl_basic_map_dim(bmap
, isl_dim_div
);
4554 ctx
= isl_basic_map_get_ctx(bmap
);
4555 off
= isl_basic_map_offset(bmap
, isl_dim_div
);
4556 data
.v
= isl_vec_alloc(ctx
, off
+ n_div
);
4565 for (i
= 0; i
< n_div
; ++i
) {
4568 if (best
>= 0 && pairs
[best
] <= pairs
[i
])
4574 for (l
= 0; l
< bmap
->n_ineq
; ++l
) {
4575 if (!isl_int_is_pos(bmap
->ineq
[l
][off
+ i
]))
4577 if (isl_int_is_one(bmap
->ineq
[l
][off
+ i
]))
4579 for (u
= 0; u
< bmap
->n_ineq
; ++u
) {
4580 if (!isl_int_is_neg(bmap
->ineq
[u
][off
+ i
]))
4582 if (isl_int_is_negone(bmap
->ineq
[u
][off
+ i
]))
4584 has_int
= int_between_bounds(bmap
, i
, l
, u
,
4588 if (data
.tab
&& data
.tab
->empty
)
4593 if (u
< bmap
->n_ineq
)
4596 if (data
.tab
&& data
.tab
->empty
) {
4597 bmap
= isl_basic_map_set_to_empty(bmap
);
4600 if (l
== bmap
->n_ineq
) {
4608 test_ineq_data_clear(&data
);
4615 bmap
= isl_basic_map_remove_dims(bmap
, isl_dim_div
, remove
, 1);
4616 return isl_basic_map_drop_redundant_divs(bmap
);
4619 isl_basic_map_free(bmap
);
4620 test_ineq_data_clear(&data
);
4624 /* Given a pair of divs div1 and div2 such that, except for the lower bound l
4625 * and the upper bound u, div1 always occurs together with div2 in the form
4626 * (div1 + m div2), where m is the constant range on the variable div1
4627 * allowed by l and u, replace the pair div1 and div2 by a single
4628 * div that is equal to div1 + m div2.
4630 * The new div will appear in the location that contains div2.
4631 * We need to modify all constraints that contain
4632 * div2 = (div - div1) / m
4633 * The coefficient of div2 is known to be equal to 1 or -1.
4634 * (If a constraint does not contain div2, it will also not contain div1.)
4635 * If the constraint also contains div1, then we know they appear
4636 * as f (div1 + m div2) and we can simply replace (div1 + m div2) by div,
4637 * i.e., the coefficient of div is f.
4639 * Otherwise, we first need to introduce div1 into the constraint.
4648 * A lower bound on div2
4652 * can be replaced by
4654 * m div2 + div1 + m t + f >= 0
4660 * can be replaced by
4662 * -(m div2 + div1) + m t + f' >= 0
4664 * These constraint are those that we would obtain from eliminating
4665 * div1 using Fourier-Motzkin.
4667 * After all constraints have been modified, we drop the lower and upper
4668 * bound and then drop div1.
4669 * Since the new div is only placed in the same location that used
4670 * to store div2, but otherwise has a different meaning, any possible
4671 * explicit representation of the original div2 is removed.
4673 static __isl_give isl_basic_map
*coalesce_divs(__isl_take isl_basic_map
*bmap
,
4674 unsigned div1
, unsigned div2
, unsigned l
, unsigned u
)
4682 ctx
= isl_basic_map_get_ctx(bmap
);
4684 v_div
= isl_basic_map_var_offset(bmap
, isl_dim_div
);
4686 return isl_basic_map_free(bmap
);
4687 total
= 1 + v_div
+ bmap
->n_div
;
4690 isl_int_add(m
, bmap
->ineq
[l
][0], bmap
->ineq
[u
][0]);
4691 isl_int_add_ui(m
, m
, 1);
4693 for (i
= 0; i
< bmap
->n_ineq
; ++i
) {
4694 if (i
== l
|| i
== u
)
4696 if (isl_int_is_zero(bmap
->ineq
[i
][1 + v_div
+ div2
]))
4698 if (isl_int_is_zero(bmap
->ineq
[i
][1 + v_div
+ div1
])) {
4699 if (isl_int_is_pos(bmap
->ineq
[i
][1 + v_div
+ div2
]))
4700 isl_seq_combine(bmap
->ineq
[i
], m
, bmap
->ineq
[i
],
4701 ctx
->one
, bmap
->ineq
[l
], total
);
4703 isl_seq_combine(bmap
->ineq
[i
], m
, bmap
->ineq
[i
],
4704 ctx
->one
, bmap
->ineq
[u
], total
);
4706 isl_int_set(bmap
->ineq
[i
][1 + v_div
+ div2
],
4707 bmap
->ineq
[i
][1 + v_div
+ div1
]);
4708 isl_int_set_si(bmap
->ineq
[i
][1 + v_div
+ div1
], 0);
4713 isl_basic_map_drop_inequality(bmap
, l
);
4714 isl_basic_map_drop_inequality(bmap
, u
);
4716 isl_basic_map_drop_inequality(bmap
, u
);
4717 isl_basic_map_drop_inequality(bmap
, l
);
4719 bmap
= isl_basic_map_mark_div_unknown(bmap
, div2
);
4720 bmap
= isl_basic_map_drop_div(bmap
, div1
);
4724 /* First check if we can coalesce any pair of divs and
4725 * then continue with dropping more redundant divs.
4727 * We loop over all pairs of lower and upper bounds on a div
4728 * with coefficient 1 and -1, respectively, check if there
4729 * is any other div "c" with which we can coalesce the div
4730 * and if so, perform the coalescing.
4732 static __isl_give isl_basic_map
*coalesce_or_drop_more_redundant_divs(
4733 __isl_take isl_basic_map
*bmap
, int *pairs
, int n
)
4739 v_div
= isl_basic_map_var_offset(bmap
, isl_dim_div
);
4740 n_div
= isl_basic_map_dim(bmap
, isl_dim_div
);
4741 if (v_div
< 0 || n_div
< 0)
4742 return isl_basic_map_free(bmap
);
4744 for (i
= 0; i
< n_div
; ++i
) {
4747 for (l
= 0; l
< bmap
->n_ineq
; ++l
) {
4748 if (!isl_int_is_one(bmap
->ineq
[l
][1 + v_div
+ i
]))
4750 for (u
= 0; u
< bmap
->n_ineq
; ++u
) {
4753 if (!isl_int_is_negone(bmap
->ineq
[u
][1+v_div
+i
]))
4755 c
= div_find_coalesce(bmap
, pairs
, i
, l
, u
);
4761 bmap
= coalesce_divs(bmap
, i
, c
, l
, u
);
4762 return isl_basic_map_drop_redundant_divs(bmap
);
4767 if (ISL_F_ISSET(bmap
, ISL_BASIC_MAP_EMPTY
)) {
4772 return drop_more_redundant_divs(bmap
, pairs
, n
);
4775 isl_basic_map_free(bmap
);
4779 /* Are the "n" coefficients starting at "first" of inequality constraints
4780 * "i" and "j" of "bmap" equal to each other?
4782 static int is_parallel_part(__isl_keep isl_basic_map
*bmap
, int i
, int j
,
4785 return isl_seq_eq(bmap
->ineq
[i
] + first
, bmap
->ineq
[j
] + first
, n
);
4788 /* Are inequality constraints "i" and "j" of "bmap" equal to each other,
4789 * apart from the constant term and the coefficient at position "pos"?
4791 static isl_bool
is_parallel_except(__isl_keep isl_basic_map
*bmap
, int i
, int j
,
4796 total
= isl_basic_map_dim(bmap
, isl_dim_all
);
4798 return isl_bool_error
;
4799 return is_parallel_part(bmap
, i
, j
, 1, pos
- 1) &&
4800 is_parallel_part(bmap
, i
, j
, pos
+ 1, total
- pos
);
4803 /* Are inequality constraints "i" and "j" of "bmap" opposite to each other,
4804 * apart from the constant term and the coefficient at position "pos"?
4806 static isl_bool
is_opposite_except(__isl_keep isl_basic_map
*bmap
, int i
, int j
,
4811 total
= isl_basic_map_dim(bmap
, isl_dim_all
);
4813 return isl_bool_error
;
4814 return is_opposite_part(bmap
, i
, j
, 1, pos
- 1) &&
4815 is_opposite_part(bmap
, i
, j
, pos
+ 1, total
- pos
);
4818 /* Restart isl_basic_map_drop_redundant_divs after "bmap" has
4819 * been modified, simplying it if "simplify" is set.
4820 * Free the temporary data structure "pairs" that was associated
4821 * to the old version of "bmap".
4823 static __isl_give isl_basic_map
*drop_redundant_divs_again(
4824 __isl_take isl_basic_map
*bmap
, __isl_take
int *pairs
, int simplify
)
4827 bmap
= isl_basic_map_simplify(bmap
);
4829 return isl_basic_map_drop_redundant_divs(bmap
);
4832 /* Is "div" the single unknown existentially quantified variable
4833 * in inequality constraint "ineq" of "bmap"?
4834 * "div" is known to have a non-zero coefficient in "ineq".
4836 static isl_bool
single_unknown(__isl_keep isl_basic_map
*bmap
, int ineq
,
4844 known
= isl_basic_map_div_is_known(bmap
, div
);
4845 if (known
< 0 || known
)
4846 return isl_bool_not(known
);
4847 n_div
= isl_basic_map_dim(bmap
, isl_dim_div
);
4849 return isl_bool_error
;
4851 return isl_bool_true
;
4852 o_div
= isl_basic_map_offset(bmap
, isl_dim_div
);
4853 for (i
= 0; i
< n_div
; ++i
) {
4858 if (isl_int_is_zero(bmap
->ineq
[ineq
][o_div
+ i
]))
4860 known
= isl_basic_map_div_is_known(bmap
, i
);
4861 if (known
< 0 || !known
)
4865 return isl_bool_true
;
4868 /* Does integer division "div" have coefficient 1 in inequality constraint
4871 static isl_bool
has_coef_one(__isl_keep isl_basic_map
*bmap
, int div
, int ineq
)
4875 o_div
= isl_basic_map_offset(bmap
, isl_dim_div
);
4876 if (isl_int_is_one(bmap
->ineq
[ineq
][o_div
+ div
]))
4877 return isl_bool_true
;
4879 return isl_bool_false
;
4882 /* Turn inequality constraint "ineq" of "bmap" into an equality and
4883 * then try and drop redundant divs again,
4884 * freeing the temporary data structure "pairs" that was associated
4885 * to the old version of "bmap".
4887 static __isl_give isl_basic_map
*set_eq_and_try_again(
4888 __isl_take isl_basic_map
*bmap
, int ineq
, __isl_take
int *pairs
)
4890 bmap
= isl_basic_map_cow(bmap
);
4891 isl_basic_map_inequality_to_equality(bmap
, ineq
);
4892 return drop_redundant_divs_again(bmap
, pairs
, 1);
4895 /* Drop the integer division at position "div", along with the two
4896 * inequality constraints "ineq1" and "ineq2" in which it appears
4897 * from "bmap" and then try and drop redundant divs again,
4898 * freeing the temporary data structure "pairs" that was associated
4899 * to the old version of "bmap".
4901 static __isl_give isl_basic_map
*drop_div_and_try_again(
4902 __isl_take isl_basic_map
*bmap
, int div
, int ineq1
, int ineq2
,
4903 __isl_take
int *pairs
)
4905 if (ineq1
> ineq2
) {
4906 isl_basic_map_drop_inequality(bmap
, ineq1
);
4907 isl_basic_map_drop_inequality(bmap
, ineq2
);
4909 isl_basic_map_drop_inequality(bmap
, ineq2
);
4910 isl_basic_map_drop_inequality(bmap
, ineq1
);
4912 bmap
= isl_basic_map_drop_div(bmap
, div
);
4913 return drop_redundant_divs_again(bmap
, pairs
, 0);
4916 /* Given two inequality constraints
4918 * f(x) + n d + c >= 0, (ineq)
4920 * with d the variable at position "pos", and
4922 * f(x) + c0 >= 0, (lower)
4924 * compute the maximal value of the lower bound ceil((-f(x) - c)/n)
4925 * determined by the first constraint.
4932 static void lower_bound_from_parallel(__isl_keep isl_basic_map
*bmap
,
4933 int ineq
, int lower
, int pos
, isl_int
*l
)
4935 isl_int_neg(*l
, bmap
->ineq
[ineq
][0]);
4936 isl_int_add(*l
, *l
, bmap
->ineq
[lower
][0]);
4937 isl_int_cdiv_q(*l
, *l
, bmap
->ineq
[ineq
][pos
]);
4940 /* Given two inequality constraints
4942 * f(x) + n d + c >= 0, (ineq)
4944 * with d the variable at position "pos", and
4946 * -f(x) - c0 >= 0, (upper)
4948 * compute the minimal value of the lower bound ceil((-f(x) - c)/n)
4949 * determined by the first constraint.
4956 static void lower_bound_from_opposite(__isl_keep isl_basic_map
*bmap
,
4957 int ineq
, int upper
, int pos
, isl_int
*u
)
4959 isl_int_neg(*u
, bmap
->ineq
[ineq
][0]);
4960 isl_int_sub(*u
, *u
, bmap
->ineq
[upper
][0]);
4961 isl_int_cdiv_q(*u
, *u
, bmap
->ineq
[ineq
][pos
]);
4964 /* Given a lower bound constraint "ineq" on "div" in "bmap",
4965 * does the corresponding lower bound have a fixed value in "bmap"?
4967 * In particular, "ineq" is of the form
4969 * f(x) + n d + c >= 0
4971 * with n > 0, c the constant term and
4972 * d the existentially quantified variable "div".
4973 * That is, the lower bound is
4975 * ceil((-f(x) - c)/n)
4977 * Look for a pair of constraints
4982 * i.e., -c1 <= -f(x) <= c0, that fix ceil((-f(x) - c)/n) to a constant value.
4983 * That is, check that
4985 * ceil((-c1 - c)/n) = ceil((c0 - c)/n)
4987 * If so, return the index of inequality f(x) + c0 >= 0.
4988 * Otherwise, return bmap->n_ineq.
4989 * Return -1 on error.
4991 static int lower_bound_is_cst(__isl_keep isl_basic_map
*bmap
, int div
, int ineq
)
4994 int lower
= -1, upper
= -1;
4999 o_div
= isl_basic_map_offset(bmap
, isl_dim_div
);
5000 for (i
= 0; i
< bmap
->n_ineq
&& (lower
< 0 || upper
< 0); ++i
) {
5005 if (!isl_int_is_zero(bmap
->ineq
[i
][o_div
+ div
]))
5007 par
= isl_bool_false
;
5009 par
= is_parallel_except(bmap
, ineq
, i
, o_div
+ div
);
5016 opp
= isl_bool_false
;
5018 opp
= is_opposite_except(bmap
, ineq
, i
, o_div
+ div
);
5025 if (lower
< 0 || upper
< 0)
5026 return bmap
->n_ineq
;
5031 lower_bound_from_parallel(bmap
, ineq
, lower
, o_div
+ div
, &l
);
5032 lower_bound_from_opposite(bmap
, ineq
, upper
, o_div
+ div
, &u
);
5034 equal
= isl_int_eq(l
, u
);
5039 return equal
? lower
: bmap
->n_ineq
;
5042 /* Given a lower bound constraint "ineq" on the existentially quantified
5043 * variable "div", such that the corresponding lower bound has
5044 * a fixed value in "bmap", assign this fixed value to the variable and
5045 * then try and drop redundant divs again,
5046 * freeing the temporary data structure "pairs" that was associated
5047 * to the old version of "bmap".
5048 * "lower" determines the constant value for the lower bound.
5050 * In particular, "ineq" is of the form
5052 * f(x) + n d + c >= 0,
5054 * while "lower" is of the form
5058 * The lower bound is ceil((-f(x) - c)/n) and its constant value
5059 * is ceil((c0 - c)/n).
5061 static __isl_give isl_basic_map
*fix_cst_lower(__isl_take isl_basic_map
*bmap
,
5062 int div
, int ineq
, int lower
, int *pairs
)
5069 o_div
= isl_basic_map_offset(bmap
, isl_dim_div
);
5070 lower_bound_from_parallel(bmap
, ineq
, lower
, o_div
+ div
, &c
);
5071 bmap
= isl_basic_map_fix(bmap
, isl_dim_div
, div
, c
);
5076 return isl_basic_map_drop_redundant_divs(bmap
);
5079 /* Do any of the integer divisions of "bmap" involve integer division "div"?
5081 * The integer division "div" could only ever appear in any later
5082 * integer division (with an explicit representation).
5084 static isl_bool
any_div_involves_div(__isl_keep isl_basic_map
*bmap
, int div
)
5087 isl_size v_div
, n_div
;
5089 v_div
= isl_basic_map_var_offset(bmap
, isl_dim_div
);
5090 n_div
= isl_basic_map_dim(bmap
, isl_dim_div
);
5091 if (v_div
< 0 || n_div
< 0)
5092 return isl_bool_error
;
5094 for (i
= div
+ 1; i
< n_div
; ++i
) {
5097 unknown
= isl_basic_map_div_is_marked_unknown(bmap
, i
);
5099 return isl_bool_error
;
5102 if (!isl_int_is_zero(bmap
->div
[i
][1 + 1 + v_div
+ div
]))
5103 return isl_bool_true
;
5106 return isl_bool_false
;
5109 /* Remove divs that are not strictly needed based on the inequality
5111 * In particular, if a div only occurs positively (or negatively)
5112 * in constraints, then it can simply be dropped.
5113 * Also, if a div occurs in only two constraints and if moreover
5114 * those two constraints are opposite to each other, except for the constant
5115 * term and if the sum of the constant terms is such that for any value
5116 * of the other values, there is always at least one integer value of the
5117 * div, i.e., if one plus this sum is greater than or equal to
5118 * the (absolute value) of the coefficient of the div in the constraints,
5119 * then we can also simply drop the div.
5121 * If an existentially quantified variable does not have an explicit
5122 * representation, appears in only a single lower bound that does not
5123 * involve any other such existentially quantified variables and appears
5124 * in this lower bound with coefficient 1,
5125 * then fix the variable to the value of the lower bound. That is,
5126 * turn the inequality into an equality.
5127 * If for any value of the other variables, there is any value
5128 * for the existentially quantified variable satisfying the constraints,
5129 * then this lower bound also satisfies the constraints.
5130 * It is therefore safe to pick this lower bound.
5132 * The same reasoning holds even if the coefficient is not one.
5133 * However, fixing the variable to the value of the lower bound may
5134 * in general introduce an extra integer division, in which case
5135 * it may be better to pick another value.
5136 * If this integer division has a known constant value, then plugging
5137 * in this constant value removes the existentially quantified variable
5138 * completely. In particular, if the lower bound is of the form
5139 * ceil((-f(x) - c)/n) and there are two constraints, f(x) + c0 >= 0 and
5140 * -f(x) + c1 >= 0 such that ceil((-c1 - c)/n) = ceil((c0 - c)/n),
5141 * then the existentially quantified variable can be assigned this
5144 * We skip divs that appear in equalities or in the definition of other divs.
5145 * Divs that appear in the definition of other divs usually occur in at least
5146 * 4 constraints, but the constraints may have been simplified.
5148 * If any divs are left after these simple checks then we move on
5149 * to more complicated cases in drop_more_redundant_divs.
5151 static __isl_give isl_basic_map
*isl_basic_map_drop_redundant_divs_ineq(
5152 __isl_take isl_basic_map
*bmap
)
5162 if (bmap
->n_div
== 0)
5165 off
= isl_basic_map_var_offset(bmap
, isl_dim_div
);
5167 return isl_basic_map_free(bmap
);
5168 pairs
= isl_calloc_array(bmap
->ctx
, int, bmap
->n_div
);
5172 n_ineq
= isl_basic_map_n_inequality(bmap
);
5175 for (i
= 0; i
< bmap
->n_div
; ++i
) {
5177 int last_pos
, last_neg
;
5180 isl_bool involves
, opp
, set_div
;
5182 defined
= !isl_int_is_zero(bmap
->div
[i
][0]);
5183 involves
= any_div_involves_div(bmap
, i
);
5188 for (j
= 0; j
< bmap
->n_eq
; ++j
)
5189 if (!isl_int_is_zero(bmap
->eq
[j
][1 + off
+ i
]))
5195 for (j
= 0; j
< bmap
->n_ineq
; ++j
) {
5196 if (isl_int_is_pos(bmap
->ineq
[j
][1 + off
+ i
])) {
5200 if (isl_int_is_neg(bmap
->ineq
[j
][1 + off
+ i
])) {
5205 pairs
[i
] = pos
* neg
;
5206 if (pairs
[i
] == 0) {
5207 for (j
= bmap
->n_ineq
- 1; j
>= 0; --j
)
5208 if (!isl_int_is_zero(bmap
->ineq
[j
][1+off
+i
]))
5209 isl_basic_map_drop_inequality(bmap
, j
);
5210 bmap
= isl_basic_map_drop_div(bmap
, i
);
5211 return drop_redundant_divs_again(bmap
, pairs
, 0);
5214 opp
= isl_bool_false
;
5216 opp
= is_opposite(bmap
, last_pos
, last_neg
);
5221 isl_bool single
, one
;
5225 single
= single_unknown(bmap
, last_pos
, i
);
5230 one
= has_coef_one(bmap
, i
, last_pos
);
5234 return set_eq_and_try_again(bmap
, last_pos
,
5236 lower
= lower_bound_is_cst(bmap
, i
, last_pos
);
5240 return fix_cst_lower(bmap
, i
, last_pos
, lower
,
5245 isl_int_add(bmap
->ineq
[last_pos
][0],
5246 bmap
->ineq
[last_pos
][0], bmap
->ineq
[last_neg
][0]);
5247 isl_int_add_ui(bmap
->ineq
[last_pos
][0],
5248 bmap
->ineq
[last_pos
][0], 1);
5249 redundant
= isl_int_ge(bmap
->ineq
[last_pos
][0],
5250 bmap
->ineq
[last_pos
][1+off
+i
]);
5251 isl_int_sub_ui(bmap
->ineq
[last_pos
][0],
5252 bmap
->ineq
[last_pos
][0], 1);
5253 isl_int_sub(bmap
->ineq
[last_pos
][0],
5254 bmap
->ineq
[last_pos
][0], bmap
->ineq
[last_neg
][0]);
5256 return drop_div_and_try_again(bmap
, i
,
5257 last_pos
, last_neg
, pairs
);
5259 set_div
= isl_bool_false
;
5261 set_div
= ok_to_set_div_from_bound(bmap
, i
, last_pos
);
5263 return isl_basic_map_free(bmap
);
5265 bmap
= set_div_from_lower_bound(bmap
, i
, last_pos
);
5266 return drop_redundant_divs_again(bmap
, pairs
, 1);
5273 return coalesce_or_drop_more_redundant_divs(bmap
, pairs
, n
);
5279 isl_basic_map_free(bmap
);
5283 /* Consider the coefficients at "c" as a row vector and replace
5284 * them with their product with "T". "T" is assumed to be a square matrix.
5286 static isl_stat
preimage(isl_int
*c
, __isl_keep isl_mat
*T
)
5292 n
= isl_mat_rows(T
);
5294 return isl_stat_error
;
5295 if (isl_seq_first_non_zero(c
, n
) == -1)
5297 ctx
= isl_mat_get_ctx(T
);
5298 v
= isl_vec_alloc(ctx
, n
);
5300 return isl_stat_error
;
5301 isl_seq_swp_or_cpy(v
->el
, c
, n
);
5302 v
= isl_vec_mat_product(v
, isl_mat_copy(T
));
5304 return isl_stat_error
;
5305 isl_seq_swp_or_cpy(c
, v
->el
, n
);
5311 /* Plug in T for the variables in "bmap" starting at "pos".
5312 * T is a linear unimodular matrix, i.e., without constant term.
5314 static __isl_give isl_basic_map
*isl_basic_map_preimage_vars(
5315 __isl_take isl_basic_map
*bmap
, unsigned pos
, __isl_take isl_mat
*T
)
5318 isl_size n_row
, n_col
;
5320 bmap
= isl_basic_map_cow(bmap
);
5321 n_row
= isl_mat_rows(T
);
5322 n_col
= isl_mat_cols(T
);
5323 if (!bmap
|| n_row
< 0 || n_col
< 0)
5327 isl_die(isl_mat_get_ctx(T
), isl_error_invalid
,
5328 "expecting square matrix", goto error
);
5330 if (isl_basic_map_check_range(bmap
, isl_dim_all
, pos
, n_col
) < 0)
5333 for (i
= 0; i
< bmap
->n_eq
; ++i
)
5334 if (preimage(bmap
->eq
[i
] + 1 + pos
, T
) < 0)
5336 for (i
= 0; i
< bmap
->n_ineq
; ++i
)
5337 if (preimage(bmap
->ineq
[i
] + 1 + pos
, T
) < 0)
5339 for (i
= 0; i
< bmap
->n_div
; ++i
) {
5340 if (isl_basic_map_div_is_marked_unknown(bmap
, i
))
5342 if (preimage(bmap
->div
[i
] + 1 + 1 + pos
, T
) < 0)
5349 isl_basic_map_free(bmap
);
5354 /* Remove divs that are not strictly needed.
5356 * First look for an equality constraint involving two or more
5357 * existentially quantified variables without an explicit
5358 * representation. Replace the combination that appears
5359 * in the equality constraint by a single existentially quantified
5360 * variable such that the equality can be used to derive
5361 * an explicit representation for the variable.
5362 * If there are no more such equality constraints, then continue
5363 * with isl_basic_map_drop_redundant_divs_ineq.
5365 * In particular, if the equality constraint is of the form
5367 * f(x) + \sum_i c_i a_i = 0
5369 * with a_i existentially quantified variable without explicit
5370 * representation, then apply a transformation on the existentially
5371 * quantified variables to turn the constraint into
5375 * with g the gcd of the c_i.
5376 * In order to easily identify which existentially quantified variables
5377 * have a complete explicit representation, i.e., without being defined
5378 * in terms of other existentially quantified variables without
5379 * an explicit representation, the existentially quantified variables
5382 * The variable transformation is computed by extending the row
5383 * [c_1/g ... c_n/g] to a unimodular matrix, obtaining the transformation
5385 * [a_1'] [c_1/g ... c_n/g] [ a_1 ]
5390 * with [c_1/g ... c_n/g] representing the first row of U.
5391 * The inverse of U is then plugged into the original constraints.
5392 * The call to isl_basic_map_simplify makes sure the explicit
5393 * representation for a_1' is extracted from the equality constraint.
5395 __isl_give isl_basic_map
*isl_basic_map_drop_redundant_divs(
5396 __isl_take isl_basic_map
*bmap
)
5408 if (isl_basic_map_divs_known(bmap
))
5409 return isl_basic_map_drop_redundant_divs_ineq(bmap
);
5410 if (bmap
->n_eq
== 0)
5411 return isl_basic_map_drop_redundant_divs_ineq(bmap
);
5412 bmap
= isl_basic_map_sort_divs(bmap
);
5416 first
= isl_basic_map_first_unknown_div(bmap
);
5418 return isl_basic_map_free(bmap
);
5420 o_div
= isl_basic_map_offset(bmap
, isl_dim_div
);
5421 n_div
= isl_basic_map_dim(bmap
, isl_dim_div
);
5423 return isl_basic_map_free(bmap
);
5425 for (i
= 0; i
< bmap
->n_eq
; ++i
) {
5426 l
= isl_seq_first_non_zero(bmap
->eq
[i
] + o_div
+ first
,
5431 if (isl_seq_first_non_zero(bmap
->eq
[i
] + o_div
+ l
+ 1,
5432 n_div
- (l
+ 1)) == -1)
5436 if (i
>= bmap
->n_eq
)
5437 return isl_basic_map_drop_redundant_divs_ineq(bmap
);
5439 ctx
= isl_basic_map_get_ctx(bmap
);
5440 T
= isl_mat_alloc(ctx
, n_div
- l
, n_div
- l
);
5442 return isl_basic_map_free(bmap
);
5443 isl_seq_cpy(T
->row
[0], bmap
->eq
[i
] + o_div
+ l
, n_div
- l
);
5444 T
= isl_mat_normalize_row(T
, 0);
5445 T
= isl_mat_unimodular_complete(T
, 1);
5446 T
= isl_mat_right_inverse(T
);
5448 for (i
= l
; i
< n_div
; ++i
)
5449 bmap
= isl_basic_map_mark_div_unknown(bmap
, i
);
5450 bmap
= isl_basic_map_preimage_vars(bmap
, o_div
- 1 + l
, T
);
5451 bmap
= isl_basic_map_simplify(bmap
);
5453 return isl_basic_map_drop_redundant_divs(bmap
);
5456 /* Does "bmap" satisfy any equality that involves more than 2 variables
5457 * and/or has coefficients different from -1 and 1?
5459 static isl_bool
has_multiple_var_equality(__isl_keep isl_basic_map
*bmap
)
5464 total
= isl_basic_map_dim(bmap
, isl_dim_all
);
5466 return isl_bool_error
;
5468 for (i
= 0; i
< bmap
->n_eq
; ++i
) {
5471 j
= isl_seq_first_non_zero(bmap
->eq
[i
] + 1, total
);
5474 if (!isl_int_is_one(bmap
->eq
[i
][1 + j
]) &&
5475 !isl_int_is_negone(bmap
->eq
[i
][1 + j
]))
5476 return isl_bool_true
;
5479 k
= isl_seq_first_non_zero(bmap
->eq
[i
] + 1 + j
, total
- j
);
5483 if (!isl_int_is_one(bmap
->eq
[i
][1 + j
]) &&
5484 !isl_int_is_negone(bmap
->eq
[i
][1 + j
]))
5485 return isl_bool_true
;
5488 k
= isl_seq_first_non_zero(bmap
->eq
[i
] + 1 + j
, total
- j
);
5490 return isl_bool_true
;
5493 return isl_bool_false
;
5496 /* Remove any common factor g from the constraint coefficients in "v".
5497 * The constant term is stored in the first position and is replaced
5498 * by floor(c/g). If any common factor is removed and if this results
5499 * in a tightening of the constraint, then set *tightened.
5501 static __isl_give isl_vec
*normalize_constraint(__isl_take isl_vec
*v
,
5508 ctx
= isl_vec_get_ctx(v
);
5509 isl_seq_gcd(v
->el
+ 1, v
->size
- 1, &ctx
->normalize_gcd
);
5510 if (isl_int_is_zero(ctx
->normalize_gcd
))
5512 if (isl_int_is_one(ctx
->normalize_gcd
))
5517 if (tightened
&& !isl_int_is_divisible_by(v
->el
[0], ctx
->normalize_gcd
))
5519 isl_int_fdiv_q(v
->el
[0], v
->el
[0], ctx
->normalize_gcd
);
5520 isl_seq_scale_down(v
->el
+ 1, v
->el
+ 1, ctx
->normalize_gcd
,
5525 /* Internal representation used by isl_basic_map_reduce_coefficients.
5527 * "total" is the total dimensionality of the original basic map.
5528 * "v" is a temporary vector of size 1 + total that can be used
5529 * to store constraint coefficients.
5530 * "T" is the variable compression.
5531 * "T2" is the inverse transformation.
5532 * "tightened" is set if any constant term got tightened
5533 * while reducing the coefficients.
5535 struct isl_reduce_coefficients_data
{
5543 /* Free all memory allocated in "data".
5545 static void isl_reduce_coefficients_data_clear(
5546 struct isl_reduce_coefficients_data
*data
)
5548 data
->T
= isl_mat_free(data
->T
);
5549 data
->T2
= isl_mat_free(data
->T2
);
5550 data
->v
= isl_vec_free(data
->v
);
5553 /* Initialize "data" for "bmap", freeing all allocated memory
5554 * if anything goes wrong.
5556 * In particular, construct a variable compression
5557 * from the equality constraints of "bmap" and
5558 * allocate a temporary vector.
5560 static isl_stat
isl_reduce_coefficients_data_init(
5561 __isl_keep isl_basic_map
*bmap
,
5562 struct isl_reduce_coefficients_data
*data
)
5570 data
->tightened
= 0;
5572 data
->total
= isl_basic_map_dim(bmap
, isl_dim_all
);
5573 if (data
->total
< 0)
5574 return isl_stat_error
;
5575 ctx
= isl_basic_map_get_ctx(bmap
);
5576 data
->v
= isl_vec_alloc(ctx
, 1 + data
->total
);
5578 return isl_stat_error
;
5580 eq
= isl_mat_sub_alloc6(ctx
, bmap
->eq
, 0, bmap
->n_eq
,
5581 0, 1 + data
->total
);
5582 data
->T
= isl_mat_variable_compression(eq
, &data
->T2
);
5583 if (!data
->T
|| !data
->T2
)
5588 isl_reduce_coefficients_data_clear(data
);
5589 return isl_stat_error
;
5592 /* Reduce the coefficients of "bmap" by applying the variable compression
5594 * In particular, apply the variable compression to each constraint,
5595 * factor out any common factor in the non-constant coefficients and
5596 * then apply the inverse of the compression.
5598 * Only apply the reduction on a single copy of the basic map
5599 * since the reduction may leave the result in an inconsistent state.
5600 * In particular, the constraints may not be gaussed.
5602 static __isl_give isl_basic_map
*reduce_coefficients(
5603 __isl_take isl_basic_map
*bmap
,
5604 struct isl_reduce_coefficients_data
*data
)
5609 total
= isl_basic_map_dim(bmap
, isl_dim_all
);
5611 return isl_basic_map_free(bmap
);
5612 if (total
!= data
->total
)
5613 isl_die(isl_basic_map_get_ctx(bmap
), isl_error_internal
,
5614 "total dimensionality changed unexpectedly",
5615 return isl_basic_map_free(bmap
));
5617 bmap
= isl_basic_map_cow(bmap
);
5621 for (i
= 0; i
< bmap
->n_ineq
; ++i
) {
5622 isl_seq_cpy(data
->v
->el
, bmap
->ineq
[i
], 1 + data
->total
);
5623 data
->v
= isl_vec_mat_product(data
->v
, isl_mat_copy(data
->T
));
5624 data
->v
= normalize_constraint(data
->v
, &data
->tightened
);
5625 data
->v
= isl_vec_mat_product(data
->v
, isl_mat_copy(data
->T2
));
5627 return isl_basic_map_free(bmap
);
5628 isl_seq_cpy(bmap
->ineq
[i
], data
->v
->el
, 1 + data
->total
);
5631 ISL_F_SET(bmap
, ISL_BASIC_MAP_REDUCED_COEFFICIENTS
);
5636 /* If "bmap" is an integer set that satisfies any equality involving
5637 * more than 2 variables and/or has coefficients different from -1 and 1,
5638 * then use variable compression to reduce the coefficients by removing
5639 * any (hidden) common factor.
5640 * In particular, apply the variable compression to each constraint,
5641 * factor out any common factor in the non-constant coefficients and
5642 * then apply the inverse of the compression.
5643 * At the end, we mark the basic map as having reduced constants.
5644 * If this flag is still set on the next invocation of this function,
5645 * then we skip the computation.
5647 * Removing a common factor may result in a tightening of some of
5648 * the constraints. If this happens, then we may end up with two
5649 * opposite inequalities that can be replaced by an equality.
5650 * We therefore call isl_basic_map_detect_inequality_pairs,
5651 * which checks for such pairs of inequalities as well as eliminate_divs_eq
5652 * and isl_basic_map_gauss if such a pair was found.
5653 * This call to isl_basic_map_gauss may undo much of the effect
5654 * of the reduction on which isl_map_coalesce depends.
5655 * In particular, constraints in terms of (compressed) local variables
5656 * get reformulated in terms of the set variables again.
5657 * The reduction is therefore applied again afterwards.
5658 * This has to be done before the call to eliminate_divs_eq, however,
5659 * since that may remove some local variables, while
5660 * the data used during the reduction is formulated in terms
5661 * of the original variables.
5663 * Tightening may also result in some other constraints becoming
5664 * (rationally) redundant with respect to the tightened constraint
5665 * (in combination with other constraints). The basic map may
5666 * therefore no longer be assumed to have no redundant constraints.
5668 * Note that this function may leave the result in an inconsistent state.
5669 * In particular, the constraints may not be gaussed.
5670 * Unfortunately, isl_map_coalesce actually depends on this inconsistent state
5671 * for some of the test cases to pass successfully.
5672 * Any potential modification of the representation is therefore only
5673 * performed on a single copy of the basic map.
5675 __isl_give isl_basic_map
*isl_basic_map_reduce_coefficients(
5676 __isl_take isl_basic_map
*bmap
)
5678 struct isl_reduce_coefficients_data data
;
5683 if (ISL_F_ISSET(bmap
, ISL_BASIC_MAP_REDUCED_COEFFICIENTS
))
5685 if (isl_basic_map_is_rational(bmap
))
5687 if (bmap
->n_eq
== 0)
5689 multi
= has_multiple_var_equality(bmap
);
5691 return isl_basic_map_free(bmap
);
5695 if (isl_reduce_coefficients_data_init(bmap
, &data
) < 0)
5696 return isl_basic_map_free(bmap
);
5698 if (data
.T
->n_col
== 0) {
5699 isl_reduce_coefficients_data_clear(&data
);
5700 return isl_basic_map_set_to_empty(bmap
);
5703 bmap
= reduce_coefficients(bmap
, &data
);
5707 if (data
.tightened
) {
5710 ISL_F_CLR(bmap
, ISL_BASIC_MAP_NO_REDUNDANT
);
5711 bmap
= isl_basic_map_detect_inequality_pairs(bmap
, &progress
);
5713 bmap
= isl_basic_map_gauss(bmap
, NULL
);
5714 bmap
= reduce_coefficients(bmap
, &data
);
5715 bmap
= eliminate_divs_eq(bmap
, &progress
);
5719 isl_reduce_coefficients_data_clear(&data
);
5723 isl_reduce_coefficients_data_clear(&data
);
5724 return isl_basic_map_free(bmap
);
5727 /* Shift the integer division at position "div" of "bmap"
5728 * by "shift" times the variable at position "pos".
5729 * "pos" is as determined by isl_basic_map_offset, i.e., pos == 0
5730 * corresponds to the constant term.
5732 * That is, if the integer division has the form
5736 * then replace it by
5738 * floor((f(x) + shift * d * x_pos)/d) - shift * x_pos
5740 __isl_give isl_basic_map
*isl_basic_map_shift_div(
5741 __isl_take isl_basic_map
*bmap
, int div
, int pos
, isl_int shift
)
5744 isl_size total
, n_div
;
5746 if (isl_int_is_zero(shift
))
5748 total
= isl_basic_map_dim(bmap
, isl_dim_all
);
5749 n_div
= isl_basic_map_dim(bmap
, isl_dim_div
);
5751 if (total
< 0 || n_div
< 0)
5752 return isl_basic_map_free(bmap
);
5754 isl_int_addmul(bmap
->div
[div
][1 + pos
], shift
, bmap
->div
[div
][0]);
5756 for (i
= 0; i
< bmap
->n_eq
; ++i
) {
5757 if (isl_int_is_zero(bmap
->eq
[i
][1 + total
+ div
]))
5759 isl_int_submul(bmap
->eq
[i
][pos
],
5760 shift
, bmap
->eq
[i
][1 + total
+ div
]);
5762 for (i
= 0; i
< bmap
->n_ineq
; ++i
) {
5763 if (isl_int_is_zero(bmap
->ineq
[i
][1 + total
+ div
]))
5765 isl_int_submul(bmap
->ineq
[i
][pos
],
5766 shift
, bmap
->ineq
[i
][1 + total
+ div
]);
5768 for (i
= 0; i
< bmap
->n_div
; ++i
) {
5769 if (isl_int_is_zero(bmap
->div
[i
][0]))
5771 if (isl_int_is_zero(bmap
->div
[i
][1 + 1 + total
+ div
]))
5773 isl_int_submul(bmap
->div
[i
][1 + pos
],
5774 shift
, bmap
->div
[i
][1 + 1 + total
+ div
]);