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
)
1749 isl_size v_div
= isl_basic_map_var_offset(bmap
, isl_dim_div
);
1750 unsigned pos
= 1 + v_div
+ div
;
1753 return isl_bool_error
;
1755 for (i
= 0; i
< bmap
->n_eq
; ++i
)
1756 if (!isl_int_is_zero(bmap
->eq
[i
][pos
]))
1757 return isl_bool_false
;
1759 for (i
= 0; i
< bmap
->n_ineq
; ++i
) {
1762 if (isl_int_is_zero(bmap
->ineq
[i
][pos
]))
1764 red
= isl_basic_map_is_div_constraint(bmap
, bmap
->ineq
[i
], div
);
1765 if (red
< 0 || !red
)
1769 involves
= isl_basic_map_any_div_involves_vars(bmap
, v_div
+ div
, 1);
1770 if (involves
< 0 || involves
)
1771 return isl_bool_not(involves
);
1773 return isl_bool_true
;
1777 * Remove divs that don't occur in any of the constraints or other divs.
1778 * These can arise when dropping constraints from a basic map or
1779 * when the divs of a basic map have been temporarily aligned
1780 * with the divs of another basic map.
1782 static __isl_give isl_basic_map
*remove_redundant_divs(
1783 __isl_take isl_basic_map
*bmap
)
1788 v_div
= isl_basic_map_var_offset(bmap
, isl_dim_div
);
1790 return isl_basic_map_free(bmap
);
1792 for (i
= bmap
->n_div
-1; i
>= 0; --i
) {
1795 redundant
= div_is_redundant(bmap
, i
);
1797 return isl_basic_map_free(bmap
);
1800 bmap
= isl_basic_map_drop_constraints_involving(bmap
,
1802 bmap
= isl_basic_map_drop_div(bmap
, i
);
1807 /* Mark "bmap" as final, without checking for obviously redundant
1808 * integer divisions. This function should be used when "bmap"
1809 * is known not to involve any such integer divisions.
1811 __isl_give isl_basic_map
*isl_basic_map_mark_final(
1812 __isl_take isl_basic_map
*bmap
)
1816 ISL_F_SET(bmap
, ISL_BASIC_SET_FINAL
);
1820 /* Mark "bmap" as final, after removing obviously redundant integer divisions.
1822 __isl_give isl_basic_map
*isl_basic_map_finalize(__isl_take isl_basic_map
*bmap
)
1824 bmap
= remove_redundant_divs(bmap
);
1825 bmap
= isl_basic_map_mark_final(bmap
);
1829 __isl_give isl_basic_set
*isl_basic_set_finalize(
1830 __isl_take isl_basic_set
*bset
)
1832 return bset_from_bmap(isl_basic_map_finalize(bset_to_bmap(bset
)));
1835 /* Remove definition of any div that is defined in terms of the given variable.
1836 * The div itself is not removed. Functions such as
1837 * eliminate_divs_ineq depend on the other divs remaining in place.
1839 static __isl_give isl_basic_map
*remove_dependent_vars(
1840 __isl_take isl_basic_map
*bmap
, int pos
)
1847 for (i
= 0; i
< bmap
->n_div
; ++i
) {
1848 if (isl_int_is_zero(bmap
->div
[i
][0]))
1850 if (isl_int_is_zero(bmap
->div
[i
][1+1+pos
]))
1852 bmap
= isl_basic_map_mark_div_unknown(bmap
, i
);
1859 /* Eliminate the specified variables from the constraints using
1860 * Fourier-Motzkin. The variables themselves are not removed.
1862 __isl_give isl_basic_map
*isl_basic_map_eliminate_vars(
1863 __isl_take isl_basic_map
*bmap
, unsigned pos
, unsigned n
)
1872 total
= isl_basic_map_dim(bmap
, isl_dim_all
);
1874 return isl_basic_map_free(bmap
);
1876 bmap
= isl_basic_map_cow(bmap
);
1877 for (d
= pos
+ n
- 1; d
>= 0 && d
>= pos
; --d
)
1878 bmap
= remove_dependent_vars(bmap
, d
);
1882 for (d
= pos
+ n
- 1;
1883 d
>= 0 && d
>= total
- bmap
->n_div
&& d
>= pos
; --d
)
1884 isl_seq_clr(bmap
->div
[d
-(total
-bmap
->n_div
)], 2+total
);
1885 for (d
= pos
+ n
- 1; d
>= 0 && d
>= pos
; --d
) {
1886 int n_lower
, n_upper
;
1889 for (i
= 0; i
< bmap
->n_eq
; ++i
) {
1890 if (isl_int_is_zero(bmap
->eq
[i
][1+d
]))
1892 bmap
= eliminate_var_using_equality(bmap
, d
,
1893 bmap
->eq
[i
], 0, 1, NULL
);
1894 if (isl_basic_map_drop_equality(bmap
, i
) < 0)
1895 return isl_basic_map_free(bmap
);
1903 for (i
= 0; i
< bmap
->n_ineq
; ++i
) {
1904 if (isl_int_is_pos(bmap
->ineq
[i
][1+d
]))
1906 else if (isl_int_is_neg(bmap
->ineq
[i
][1+d
]))
1909 bmap
= isl_basic_map_extend_constraints(bmap
,
1910 0, n_lower
* n_upper
);
1913 for (i
= bmap
->n_ineq
- 1; i
>= 0; --i
) {
1915 if (isl_int_is_zero(bmap
->ineq
[i
][1+d
]))
1918 for (j
= 0; j
< i
; ++j
) {
1919 if (isl_int_is_zero(bmap
->ineq
[j
][1+d
]))
1922 if (isl_int_sgn(bmap
->ineq
[i
][1+d
]) ==
1923 isl_int_sgn(bmap
->ineq
[j
][1+d
]))
1925 k
= isl_basic_map_alloc_inequality(bmap
);
1928 isl_seq_cpy(bmap
->ineq
[k
], bmap
->ineq
[i
],
1930 isl_seq_elim(bmap
->ineq
[k
], bmap
->ineq
[j
],
1931 1+d
, 1+total
, NULL
);
1933 isl_basic_map_drop_inequality(bmap
, i
);
1936 if (n_lower
> 0 && n_upper
> 0) {
1937 bmap
= isl_basic_map_normalize_constraints(bmap
);
1938 bmap
= isl_basic_map_remove_duplicate_constraints(bmap
,
1940 bmap
= isl_basic_map_gauss(bmap
, NULL
);
1941 bmap
= isl_basic_map_remove_redundancies(bmap
);
1945 if (ISL_F_ISSET(bmap
, ISL_BASIC_MAP_EMPTY
))
1950 bmap
= isl_basic_map_gauss(bmap
, NULL
);
1953 isl_basic_map_free(bmap
);
1957 __isl_give isl_basic_set
*isl_basic_set_eliminate_vars(
1958 __isl_take isl_basic_set
*bset
, unsigned pos
, unsigned n
)
1960 return bset_from_bmap(isl_basic_map_eliminate_vars(bset_to_bmap(bset
),
1964 /* Eliminate the specified n dimensions starting at first from the
1965 * constraints, without removing the dimensions from the space.
1966 * If the set is rational, the dimensions are eliminated using Fourier-Motzkin.
1967 * Otherwise, they are projected out and the original space is restored.
1969 __isl_give isl_basic_map
*isl_basic_map_eliminate(
1970 __isl_take isl_basic_map
*bmap
,
1971 enum isl_dim_type type
, unsigned first
, unsigned n
)
1980 if (isl_basic_map_check_range(bmap
, type
, first
, n
) < 0)
1981 return isl_basic_map_free(bmap
);
1983 if (ISL_F_ISSET(bmap
, ISL_BASIC_MAP_RATIONAL
)) {
1984 first
+= isl_basic_map_offset(bmap
, type
) - 1;
1985 bmap
= isl_basic_map_eliminate_vars(bmap
, first
, n
);
1986 return isl_basic_map_finalize(bmap
);
1989 space
= isl_basic_map_get_space(bmap
);
1990 bmap
= isl_basic_map_project_out(bmap
, type
, first
, n
);
1991 bmap
= isl_basic_map_insert_dims(bmap
, type
, first
, n
);
1992 bmap
= isl_basic_map_reset_space(bmap
, space
);
1996 __isl_give isl_basic_set
*isl_basic_set_eliminate(
1997 __isl_take isl_basic_set
*bset
,
1998 enum isl_dim_type type
, unsigned first
, unsigned n
)
2000 return isl_basic_map_eliminate(bset
, type
, first
, n
);
2003 /* Remove all constraints from "bmap" that reference any unknown local
2004 * variables (directly or indirectly).
2006 * Dropping all constraints on a local variable will make it redundant,
2007 * so it will get removed implicitly by
2008 * isl_basic_map_drop_constraints_involving_dims. Some other local
2009 * variables may also end up becoming redundant if they only appear
2010 * in constraints together with the unknown local variable.
2011 * Therefore, start over after calling
2012 * isl_basic_map_drop_constraints_involving_dims.
2014 __isl_give isl_basic_map
*isl_basic_map_drop_constraints_involving_unknown_divs(
2015 __isl_take isl_basic_map
*bmap
)
2021 known
= isl_basic_map_divs_known(bmap
);
2023 return isl_basic_map_free(bmap
);
2027 n_div
= isl_basic_map_dim(bmap
, isl_dim_div
);
2029 return isl_basic_map_free(bmap
);
2030 o_div
= isl_basic_map_offset(bmap
, isl_dim_div
) - 1;
2032 for (i
= 0; i
< n_div
; ++i
) {
2033 known
= isl_basic_map_div_is_known(bmap
, i
);
2035 return isl_basic_map_free(bmap
);
2038 bmap
= remove_dependent_vars(bmap
, o_div
+ i
);
2039 bmap
= isl_basic_map_drop_constraints_involving_dims(bmap
,
2041 n_div
= isl_basic_map_dim(bmap
, isl_dim_div
);
2043 return isl_basic_map_free(bmap
);
2050 /* Remove all constraints from "bset" that reference any unknown local
2051 * variables (directly or indirectly).
2053 __isl_give isl_basic_set
*isl_basic_set_drop_constraints_involving_unknown_divs(
2054 __isl_take isl_basic_set
*bset
)
2056 isl_basic_map
*bmap
;
2058 bmap
= bset_to_bmap(bset
);
2059 bmap
= isl_basic_map_drop_constraints_involving_unknown_divs(bmap
);
2060 return bset_from_bmap(bmap
);
2063 /* Remove all constraints from "map" that reference any unknown local
2064 * variables (directly or indirectly).
2066 * Since constraints may get dropped from the basic maps,
2067 * they may no longer be disjoint from each other.
2069 __isl_give isl_map
*isl_map_drop_constraints_involving_unknown_divs(
2070 __isl_take isl_map
*map
)
2075 known
= isl_map_divs_known(map
);
2077 return isl_map_free(map
);
2081 map
= isl_map_cow(map
);
2085 for (i
= 0; i
< map
->n
; ++i
) {
2087 isl_basic_map_drop_constraints_involving_unknown_divs(
2090 return isl_map_free(map
);
2094 ISL_F_CLR(map
, ISL_MAP_DISJOINT
);
2099 /* Don't assume equalities are in order, because align_divs
2100 * may have changed the order of the divs.
2102 static void compute_elimination_index(__isl_keep isl_basic_map
*bmap
, int *elim
,
2107 for (d
= 0; d
< len
; ++d
)
2109 for (i
= 0; i
< bmap
->n_eq
; ++i
) {
2110 for (d
= len
- 1; d
>= 0; --d
) {
2111 if (isl_int_is_zero(bmap
->eq
[i
][1+d
]))
2119 static void set_compute_elimination_index(__isl_keep isl_basic_set
*bset
,
2120 int *elim
, unsigned len
)
2122 compute_elimination_index(bset_to_bmap(bset
), elim
, len
);
2125 static int reduced_using_equalities(isl_int
*dst
, isl_int
*src
,
2126 __isl_keep isl_basic_map
*bmap
, int *elim
, unsigned total
)
2131 for (d
= total
- 1; d
>= 0; --d
) {
2132 if (isl_int_is_zero(src
[1+d
]))
2137 isl_seq_cpy(dst
, src
, 1 + total
);
2140 isl_seq_elim(dst
, bmap
->eq
[elim
[d
]], 1 + d
, 1 + total
, NULL
);
2145 static int set_reduced_using_equalities(isl_int
*dst
, isl_int
*src
,
2146 __isl_keep isl_basic_set
*bset
, int *elim
, unsigned total
)
2148 return reduced_using_equalities(dst
, src
,
2149 bset_to_bmap(bset
), elim
, total
);
2152 static __isl_give isl_basic_set
*isl_basic_set_reduce_using_equalities(
2153 __isl_take isl_basic_set
*bset
, __isl_take isl_basic_set
*context
)
2159 if (!bset
|| !context
)
2162 if (context
->n_eq
== 0) {
2163 isl_basic_set_free(context
);
2167 bset
= isl_basic_set_cow(bset
);
2168 dim
= isl_basic_set_dim(bset
, isl_dim_set
);
2172 elim
= isl_alloc_array(bset
->ctx
, int, dim
);
2175 set_compute_elimination_index(context
, elim
, dim
);
2176 for (i
= 0; i
< bset
->n_eq
; ++i
)
2177 set_reduced_using_equalities(bset
->eq
[i
], bset
->eq
[i
],
2178 context
, elim
, dim
);
2179 for (i
= 0; i
< bset
->n_ineq
; ++i
)
2180 set_reduced_using_equalities(bset
->ineq
[i
], bset
->ineq
[i
],
2181 context
, elim
, dim
);
2182 isl_basic_set_free(context
);
2184 bset
= isl_basic_set_simplify(bset
);
2185 bset
= isl_basic_set_finalize(bset
);
2188 isl_basic_set_free(bset
);
2189 isl_basic_set_free(context
);
2193 /* For each inequality in "ineq" that is a shifted (more relaxed)
2194 * copy of an inequality in "context", mark the corresponding entry
2196 * If an inequality only has a non-negative constant term, then
2199 static isl_stat
mark_shifted_constraints(__isl_keep isl_mat
*ineq
,
2200 __isl_keep isl_basic_set
*context
, int *row
)
2202 struct isl_constraint_index ci
;
2203 isl_size n_ineq
, cols
;
2207 if (!ineq
|| !context
)
2208 return isl_stat_error
;
2209 if (context
->n_ineq
== 0)
2211 if (setup_constraint_index(&ci
, context
) < 0)
2212 return isl_stat_error
;
2214 n_ineq
= isl_mat_rows(ineq
);
2215 cols
= isl_mat_cols(ineq
);
2216 if (n_ineq
< 0 || cols
< 0)
2217 return isl_stat_error
;
2219 for (k
= 0; k
< n_ineq
; ++k
) {
2223 l
= isl_seq_first_non_zero(ineq
->row
[k
] + 1, total
);
2224 if (l
< 0 && isl_int_is_nonneg(ineq
->row
[k
][0])) {
2228 redundant
= constraint_index_is_redundant(&ci
, ineq
->row
[k
]);
2235 constraint_index_free(&ci
);
2238 constraint_index_free(&ci
);
2239 return isl_stat_error
;
2242 static __isl_give isl_basic_set
*remove_shifted_constraints(
2243 __isl_take isl_basic_set
*bset
, __isl_keep isl_basic_set
*context
)
2245 struct isl_constraint_index ci
;
2248 if (!bset
|| !context
)
2251 if (context
->n_ineq
== 0)
2253 if (setup_constraint_index(&ci
, context
) < 0)
2256 for (k
= 0; k
< bset
->n_ineq
; ++k
) {
2259 redundant
= constraint_index_is_redundant(&ci
, bset
->ineq
[k
]);
2264 bset
= isl_basic_set_cow(bset
);
2267 isl_basic_set_drop_inequality(bset
, k
);
2270 constraint_index_free(&ci
);
2273 constraint_index_free(&ci
);
2277 /* Remove constraints from "bmap" that are identical to constraints
2278 * in "context" or that are more relaxed (greater constant term).
2280 * We perform the test for shifted copies on the pure constraints
2281 * in remove_shifted_constraints.
2283 static __isl_give isl_basic_map
*isl_basic_map_remove_shifted_constraints(
2284 __isl_take isl_basic_map
*bmap
, __isl_take isl_basic_map
*context
)
2286 isl_basic_set
*bset
, *bset_context
;
2288 if (!bmap
|| !context
)
2291 if (bmap
->n_ineq
== 0 || context
->n_ineq
== 0) {
2292 isl_basic_map_free(context
);
2296 bmap
= isl_basic_map_order_divs(bmap
);
2297 context
= isl_basic_map_align_divs(context
, bmap
);
2298 bmap
= isl_basic_map_align_divs(bmap
, context
);
2300 bset
= isl_basic_map_underlying_set(isl_basic_map_copy(bmap
));
2301 bset_context
= isl_basic_map_underlying_set(context
);
2302 bset
= remove_shifted_constraints(bset
, bset_context
);
2303 isl_basic_set_free(bset_context
);
2305 bmap
= isl_basic_map_overlying_set(bset
, bmap
);
2309 isl_basic_map_free(bmap
);
2310 isl_basic_map_free(context
);
2314 /* Does the (linear part of a) constraint "c" involve any of the "len"
2315 * "relevant" dimensions?
2317 static int is_related(isl_int
*c
, int len
, int *relevant
)
2321 for (i
= 0; i
< len
; ++i
) {
2324 if (!isl_int_is_zero(c
[i
]))
2331 /* Drop constraints from "bmap" that do not involve any of
2332 * the dimensions marked "relevant".
2334 static __isl_give isl_basic_map
*drop_unrelated_constraints(
2335 __isl_take isl_basic_map
*bmap
, int *relevant
)
2340 dim
= isl_basic_map_dim(bmap
, isl_dim_all
);
2342 return isl_basic_map_free(bmap
);
2343 for (i
= 0; i
< dim
; ++i
)
2349 for (i
= bmap
->n_eq
- 1; i
>= 0; --i
)
2350 if (!is_related(bmap
->eq
[i
] + 1, dim
, relevant
)) {
2351 bmap
= isl_basic_map_cow(bmap
);
2352 if (isl_basic_map_drop_equality(bmap
, i
) < 0)
2353 return isl_basic_map_free(bmap
);
2356 for (i
= bmap
->n_ineq
- 1; i
>= 0; --i
)
2357 if (!is_related(bmap
->ineq
[i
] + 1, dim
, relevant
)) {
2358 bmap
= isl_basic_map_cow(bmap
);
2359 if (isl_basic_map_drop_inequality(bmap
, i
) < 0)
2360 return isl_basic_map_free(bmap
);
2366 /* Update the groups in "group" based on the (linear part of a) constraint "c".
2368 * In particular, for any variable involved in the constraint,
2369 * find the actual group id from before and replace the group
2370 * of the corresponding variable by the minimal group of all
2371 * the variables involved in the constraint considered so far
2372 * (if this minimum is smaller) or replace the minimum by this group
2373 * (if the minimum is larger).
2375 * At the end, all the variables in "c" will (indirectly) point
2376 * to the minimal of the groups that they referred to originally.
2378 static void update_groups(int dim
, int *group
, isl_int
*c
)
2383 for (j
= 0; j
< dim
; ++j
) {
2384 if (isl_int_is_zero(c
[j
]))
2386 while (group
[j
] >= 0 && group
[group
[j
]] != group
[j
])
2387 group
[j
] = group
[group
[j
]];
2388 if (group
[j
] == min
)
2390 if (group
[j
] < min
) {
2391 if (min
>= 0 && min
< dim
)
2392 group
[min
] = group
[j
];
2395 group
[group
[j
]] = min
;
2399 /* Allocate an array of groups of variables, one for each variable
2400 * in "context", initialized to zero.
2402 static int *alloc_groups(__isl_keep isl_basic_set
*context
)
2407 dim
= isl_basic_set_dim(context
, isl_dim_set
);
2410 ctx
= isl_basic_set_get_ctx(context
);
2411 return isl_calloc_array(ctx
, int, dim
);
2414 /* Drop constraints from "bmap" that only involve variables that are
2415 * not related to any of the variables marked with a "-1" in "group".
2417 * We construct groups of variables that collect variables that
2418 * (indirectly) appear in some common constraint of "bmap".
2419 * Each group is identified by the first variable in the group,
2420 * except for the special group of variables that was already identified
2421 * in the input as -1 (or are related to those variables).
2422 * If group[i] is equal to i (or -1), then the group of i is i (or -1),
2423 * otherwise the group of i is the group of group[i].
2425 * We first initialize groups for the remaining variables.
2426 * Then we iterate over the constraints of "bmap" and update the
2427 * group of the variables in the constraint by the smallest group.
2428 * Finally, we resolve indirect references to groups by running over
2431 * After computing the groups, we drop constraints that do not involve
2432 * any variables in the -1 group.
2434 __isl_give isl_basic_map
*isl_basic_map_drop_unrelated_constraints(
2435 __isl_take isl_basic_map
*bmap
, __isl_take
int *group
)
2441 dim
= isl_basic_map_dim(bmap
, isl_dim_all
);
2443 return isl_basic_map_free(bmap
);
2446 for (i
= 0; i
< dim
; ++i
)
2448 last
= group
[i
] = i
;
2454 for (i
= 0; i
< bmap
->n_eq
; ++i
)
2455 update_groups(dim
, group
, bmap
->eq
[i
] + 1);
2456 for (i
= 0; i
< bmap
->n_ineq
; ++i
)
2457 update_groups(dim
, group
, bmap
->ineq
[i
] + 1);
2459 for (i
= 0; i
< dim
; ++i
)
2461 group
[i
] = group
[group
[i
]];
2463 for (i
= 0; i
< dim
; ++i
)
2464 group
[i
] = group
[i
] == -1;
2466 bmap
= drop_unrelated_constraints(bmap
, group
);
2472 /* Drop constraints from "context" that are irrelevant for computing
2473 * the gist of "bset".
2475 * In particular, drop constraints in variables that are not related
2476 * to any of the variables involved in the constraints of "bset"
2477 * in the sense that there is no sequence of constraints that connects them.
2479 * We first mark all variables that appear in "bset" as belonging
2480 * to a "-1" group and then continue with group_and_drop_irrelevant_constraints.
2482 static __isl_give isl_basic_set
*drop_irrelevant_constraints(
2483 __isl_take isl_basic_set
*context
, __isl_keep isl_basic_set
*bset
)
2489 dim
= isl_basic_set_dim(bset
, isl_dim_set
);
2490 if (!context
|| dim
< 0)
2491 return isl_basic_set_free(context
);
2493 group
= alloc_groups(context
);
2496 return isl_basic_set_free(context
);
2498 for (i
= 0; i
< dim
; ++i
) {
2499 for (j
= 0; j
< bset
->n_eq
; ++j
)
2500 if (!isl_int_is_zero(bset
->eq
[j
][1 + i
]))
2502 if (j
< bset
->n_eq
) {
2506 for (j
= 0; j
< bset
->n_ineq
; ++j
)
2507 if (!isl_int_is_zero(bset
->ineq
[j
][1 + i
]))
2509 if (j
< bset
->n_ineq
)
2513 return isl_basic_map_drop_unrelated_constraints(context
, group
);
2516 /* Drop constraints from "context" that are irrelevant for computing
2517 * the gist of the inequalities "ineq".
2518 * Inequalities in "ineq" for which the corresponding element of row
2519 * is set to -1 have already been marked for removal and should be ignored.
2521 * In particular, drop constraints in variables that are not related
2522 * to any of the variables involved in "ineq"
2523 * in the sense that there is no sequence of constraints that connects them.
2525 * We first mark all variables that appear in "bset" as belonging
2526 * to a "-1" group and then continue with group_and_drop_irrelevant_constraints.
2528 static __isl_give isl_basic_set
*drop_irrelevant_constraints_marked(
2529 __isl_take isl_basic_set
*context
, __isl_keep isl_mat
*ineq
, int *row
)
2536 dim
= isl_basic_set_dim(context
, isl_dim_set
);
2537 n
= isl_mat_rows(ineq
);
2538 if (dim
< 0 || n
< 0)
2539 return isl_basic_set_free(context
);
2541 group
= alloc_groups(context
);
2544 return isl_basic_set_free(context
);
2546 for (i
= 0; i
< dim
; ++i
) {
2547 for (j
= 0; j
< n
; ++j
) {
2550 if (!isl_int_is_zero(ineq
->row
[j
][1 + i
]))
2557 return isl_basic_map_drop_unrelated_constraints(context
, group
);
2560 /* Do all "n" entries of "row" contain a negative value?
2562 static int all_neg(int *row
, int n
)
2566 for (i
= 0; i
< n
; ++i
)
2573 /* Update the inequalities in "bset" based on the information in "row"
2576 * In particular, the array "row" contains either -1, meaning that
2577 * the corresponding inequality of "bset" is redundant, or the index
2578 * of an inequality in "tab".
2580 * If the row entry is -1, then drop the inequality.
2581 * Otherwise, if the constraint is marked redundant in the tableau,
2582 * then drop the inequality. Similarly, if it is marked as an equality
2583 * in the tableau, then turn the inequality into an equality and
2584 * perform Gaussian elimination.
2586 static __isl_give isl_basic_set
*update_ineq(__isl_take isl_basic_set
*bset
,
2587 __isl_keep
int *row
, struct isl_tab
*tab
)
2592 int found_equality
= 0;
2596 if (tab
&& tab
->empty
)
2597 return isl_basic_set_set_to_empty(bset
);
2599 n_ineq
= bset
->n_ineq
;
2600 for (i
= n_ineq
- 1; i
>= 0; --i
) {
2602 if (isl_basic_set_drop_inequality(bset
, i
) < 0)
2603 return isl_basic_set_free(bset
);
2609 if (isl_tab_is_equality(tab
, n_eq
+ row
[i
])) {
2610 isl_basic_map_inequality_to_equality(bset
, i
);
2612 } else if (isl_tab_is_redundant(tab
, n_eq
+ row
[i
])) {
2613 if (isl_basic_set_drop_inequality(bset
, i
) < 0)
2614 return isl_basic_set_free(bset
);
2619 bset
= isl_basic_set_gauss(bset
, NULL
);
2620 bset
= isl_basic_set_finalize(bset
);
2624 /* Update the inequalities in "bset" based on the information in "row"
2625 * and "tab" and free all arguments (other than "bset").
2627 static __isl_give isl_basic_set
*update_ineq_free(
2628 __isl_take isl_basic_set
*bset
, __isl_take isl_mat
*ineq
,
2629 __isl_take isl_basic_set
*context
, __isl_take
int *row
,
2630 struct isl_tab
*tab
)
2633 isl_basic_set_free(context
);
2635 bset
= update_ineq(bset
, row
, tab
);
2642 /* Remove all information from bset that is redundant in the context
2644 * "ineq" contains the (possibly transformed) inequalities of "bset",
2645 * in the same order.
2646 * The (explicit) equalities of "bset" are assumed to have been taken
2647 * into account by the transformation such that only the inequalities
2649 * "context" is assumed not to be empty.
2651 * "row" keeps track of the constraint index of a "bset" inequality in "tab".
2652 * A value of -1 means that the inequality is obviously redundant and may
2653 * not even appear in "tab".
2655 * We first mark the inequalities of "bset"
2656 * that are obviously redundant with respect to some inequality in "context".
2657 * Then we remove those constraints from "context" that have become
2658 * irrelevant for computing the gist of "bset".
2659 * Note that this removal of constraints cannot be replaced by
2660 * a factorization because factors in "bset" may still be connected
2661 * to each other through constraints in "context".
2663 * If there are any inequalities left, we construct a tableau for
2664 * the context and then add the inequalities of "bset".
2665 * Before adding these inequalities, we freeze all constraints such that
2666 * they won't be considered redundant in terms of the constraints of "bset".
2667 * Then we detect all redundant constraints (among the
2668 * constraints that weren't frozen), first by checking for redundancy in the
2669 * the tableau and then by checking if replacing a constraint by its negation
2670 * would lead to an empty set. This last step is fairly expensive
2671 * and could be optimized by more reuse of the tableau.
2672 * Finally, we update bset according to the results.
2674 static __isl_give isl_basic_set
*uset_gist_full(__isl_take isl_basic_set
*bset
,
2675 __isl_take isl_mat
*ineq
, __isl_take isl_basic_set
*context
)
2680 isl_basic_set
*combined
= NULL
;
2681 struct isl_tab
*tab
= NULL
;
2682 unsigned n_eq
, context_ineq
;
2684 if (!bset
|| !ineq
|| !context
)
2687 if (bset
->n_ineq
== 0 || isl_basic_set_plain_is_universe(context
)) {
2688 isl_basic_set_free(context
);
2693 ctx
= isl_basic_set_get_ctx(context
);
2694 row
= isl_calloc_array(ctx
, int, bset
->n_ineq
);
2698 if (mark_shifted_constraints(ineq
, context
, row
) < 0)
2700 if (all_neg(row
, bset
->n_ineq
))
2701 return update_ineq_free(bset
, ineq
, context
, row
, NULL
);
2703 context
= drop_irrelevant_constraints_marked(context
, ineq
, row
);
2706 if (isl_basic_set_plain_is_universe(context
))
2707 return update_ineq_free(bset
, ineq
, context
, row
, NULL
);
2709 n_eq
= context
->n_eq
;
2710 context_ineq
= context
->n_ineq
;
2711 combined
= isl_basic_set_cow(isl_basic_set_copy(context
));
2712 combined
= isl_basic_set_extend_constraints(combined
, 0, bset
->n_ineq
);
2713 tab
= isl_tab_from_basic_set(combined
, 0);
2714 for (i
= 0; i
< context_ineq
; ++i
)
2715 if (isl_tab_freeze_constraint(tab
, n_eq
+ i
) < 0)
2717 if (isl_tab_extend_cons(tab
, bset
->n_ineq
) < 0)
2720 for (i
= 0; i
< bset
->n_ineq
; ++i
) {
2723 combined
= isl_basic_set_add_ineq(combined
, ineq
->row
[i
]);
2724 if (isl_tab_add_ineq(tab
, ineq
->row
[i
]) < 0)
2728 if (isl_tab_detect_implicit_equalities(tab
) < 0)
2730 if (isl_tab_detect_redundant(tab
) < 0)
2732 for (i
= bset
->n_ineq
- 1; i
>= 0; --i
) {
2733 isl_basic_set
*test
;
2739 if (tab
->con
[n_eq
+ r
].is_redundant
)
2741 test
= isl_basic_set_dup(combined
);
2742 test
= isl_inequality_negate(test
, r
);
2743 test
= isl_basic_set_update_from_tab(test
, tab
);
2744 is_empty
= isl_basic_set_is_empty(test
);
2745 isl_basic_set_free(test
);
2749 tab
->con
[n_eq
+ r
].is_redundant
= 1;
2751 bset
= update_ineq_free(bset
, ineq
, context
, row
, tab
);
2753 ISL_F_SET(bset
, ISL_BASIC_SET_NO_IMPLICIT
);
2754 ISL_F_SET(bset
, ISL_BASIC_SET_NO_REDUNDANT
);
2757 isl_basic_set_free(combined
);
2763 isl_basic_set_free(combined
);
2764 isl_basic_set_free(context
);
2765 isl_basic_set_free(bset
);
2769 /* Extract the inequalities of "bset" as an isl_mat.
2771 static __isl_give isl_mat
*extract_ineq(__isl_keep isl_basic_set
*bset
)
2777 total
= isl_basic_set_dim(bset
, isl_dim_all
);
2781 ctx
= isl_basic_set_get_ctx(bset
);
2782 ineq
= isl_mat_sub_alloc6(ctx
, bset
->ineq
, 0, bset
->n_ineq
,
2788 /* Remove all information from "bset" that is redundant in the context
2789 * of "context", for the case where both "bset" and "context" are
2792 static __isl_give isl_basic_set
*uset_gist_uncompressed(
2793 __isl_take isl_basic_set
*bset
, __isl_take isl_basic_set
*context
)
2797 ineq
= extract_ineq(bset
);
2798 return uset_gist_full(bset
, ineq
, context
);
2801 /* Replace "bset" by an empty basic set in the same space.
2803 static __isl_give isl_basic_set
*replace_by_empty(
2804 __isl_take isl_basic_set
*bset
)
2808 space
= isl_basic_set_get_space(bset
);
2809 isl_basic_set_free(bset
);
2810 return isl_basic_set_empty(space
);
2813 /* Remove all information from "bset" that is redundant in the context
2814 * of "context", for the case where the combined equalities of
2815 * "bset" and "context" allow for a compression that can be obtained
2816 * by preapplication of "T".
2817 * If the compression of "context" is empty, meaning that "bset" and
2818 * "context" do not intersect, then return the empty set.
2820 * "bset" itself is not transformed by "T". Instead, the inequalities
2821 * are extracted from "bset" and those are transformed by "T".
2822 * uset_gist_full then determines which of the transformed inequalities
2823 * are redundant with respect to the transformed "context" and removes
2824 * the corresponding inequalities from "bset".
2826 * After preapplying "T" to the inequalities, any common factor is
2827 * removed from the coefficients. If this results in a tightening
2828 * of the constant term, then the same tightening is applied to
2829 * the corresponding untransformed inequality in "bset".
2830 * That is, if after plugging in T, a constraint f(x) >= 0 is of the form
2834 * with 0 <= r < g, then it is equivalent to
2838 * This means that f(x) >= 0 is equivalent to f(x) - r >= 0 in the affine
2839 * subspace compressed by T since the latter would be transformed to
2843 static __isl_give isl_basic_set
*uset_gist_compressed(
2844 __isl_take isl_basic_set
*bset
, __isl_take isl_basic_set
*context
,
2845 __isl_take isl_mat
*T
)
2850 isl_size n_row
, n_col
;
2853 ineq
= extract_ineq(bset
);
2854 ineq
= isl_mat_product(ineq
, isl_mat_copy(T
));
2855 context
= isl_basic_set_preimage(context
, T
);
2857 if (!ineq
|| !context
)
2859 if (isl_basic_set_plain_is_empty(context
)) {
2861 isl_basic_set_free(context
);
2862 return replace_by_empty(bset
);
2865 ctx
= isl_mat_get_ctx(ineq
);
2866 n_row
= isl_mat_rows(ineq
);
2867 n_col
= isl_mat_cols(ineq
);
2868 if (n_row
< 0 || n_col
< 0)
2871 for (i
= 0; i
< n_row
; ++i
) {
2872 isl_seq_gcd(ineq
->row
[i
] + 1, n_col
- 1, &ctx
->normalize_gcd
);
2873 if (isl_int_is_zero(ctx
->normalize_gcd
))
2875 if (isl_int_is_one(ctx
->normalize_gcd
))
2877 isl_seq_scale_down(ineq
->row
[i
] + 1, ineq
->row
[i
] + 1,
2878 ctx
->normalize_gcd
, n_col
- 1);
2879 isl_int_fdiv_r(rem
, ineq
->row
[i
][0], ctx
->normalize_gcd
);
2880 isl_int_fdiv_q(ineq
->row
[i
][0],
2881 ineq
->row
[i
][0], ctx
->normalize_gcd
);
2882 if (isl_int_is_zero(rem
))
2884 bset
= isl_basic_set_cow(bset
);
2887 isl_int_sub(bset
->ineq
[i
][0], bset
->ineq
[i
][0], rem
);
2891 return uset_gist_full(bset
, ineq
, context
);
2894 isl_basic_set_free(context
);
2895 isl_basic_set_free(bset
);
2899 /* Project "bset" onto the variables that are involved in "template".
2901 static __isl_give isl_basic_set
*project_onto_involved(
2902 __isl_take isl_basic_set
*bset
, __isl_keep isl_basic_set
*template)
2907 n
= isl_basic_set_dim(template, isl_dim_set
);
2908 if (n
< 0 || !template)
2909 return isl_basic_set_free(bset
);
2911 for (i
= 0; i
< n
; ++i
) {
2914 involved
= isl_basic_set_involves_dims(template,
2917 return isl_basic_set_free(bset
);
2920 bset
= isl_basic_set_eliminate_vars(bset
, i
, 1);
2926 /* Remove all information from bset that is redundant in the context
2927 * of context. In particular, equalities that are linear combinations
2928 * of those in context are removed. Then the inequalities that are
2929 * redundant in the context of the equalities and inequalities of
2930 * context are removed.
2932 * First of all, we drop those constraints from "context"
2933 * that are irrelevant for computing the gist of "bset".
2934 * Alternatively, we could factorize the intersection of "context" and "bset".
2936 * We first compute the intersection of the integer affine hulls
2937 * of "bset" and "context",
2938 * compute the gist inside this intersection and then reduce
2939 * the constraints with respect to the equalities of the context
2940 * that only involve variables already involved in the input.
2941 * If the intersection of the affine hulls turns out to be empty,
2942 * then return the empty set.
2944 * If two constraints are mutually redundant, then uset_gist_full
2945 * will remove the second of those constraints. We therefore first
2946 * sort the constraints so that constraints not involving existentially
2947 * quantified variables are given precedence over those that do.
2948 * We have to perform this sorting before the variable compression,
2949 * because that may effect the order of the variables.
2951 static __isl_give isl_basic_set
*uset_gist(__isl_take isl_basic_set
*bset
,
2952 __isl_take isl_basic_set
*context
)
2957 isl_basic_set
*aff_context
;
2960 total
= isl_basic_set_dim(bset
, isl_dim_all
);
2961 if (total
< 0 || !context
)
2964 context
= drop_irrelevant_constraints(context
, bset
);
2966 bset
= isl_basic_set_detect_equalities(bset
);
2967 aff
= isl_basic_set_copy(bset
);
2968 aff
= isl_basic_set_plain_affine_hull(aff
);
2969 context
= isl_basic_set_detect_equalities(context
);
2970 aff_context
= isl_basic_set_copy(context
);
2971 aff_context
= isl_basic_set_plain_affine_hull(aff_context
);
2972 aff
= isl_basic_set_intersect(aff
, aff_context
);
2975 if (isl_basic_set_plain_is_empty(aff
)) {
2976 isl_basic_set_free(bset
);
2977 isl_basic_set_free(context
);
2980 bset
= isl_basic_set_sort_constraints(bset
);
2981 if (aff
->n_eq
== 0) {
2982 isl_basic_set_free(aff
);
2983 return uset_gist_uncompressed(bset
, context
);
2985 eq
= isl_mat_sub_alloc6(bset
->ctx
, aff
->eq
, 0, aff
->n_eq
, 0, 1 + total
);
2986 eq
= isl_mat_cow(eq
);
2987 T
= isl_mat_variable_compression(eq
, NULL
);
2988 isl_basic_set_free(aff
);
2989 if (T
&& T
->n_col
== 0) {
2991 isl_basic_set_free(context
);
2992 return replace_by_empty(bset
);
2995 aff_context
= isl_basic_set_affine_hull(isl_basic_set_copy(context
));
2996 aff_context
= project_onto_involved(aff_context
, bset
);
2998 bset
= uset_gist_compressed(bset
, context
, T
);
2999 bset
= isl_basic_set_reduce_using_equalities(bset
, aff_context
);
3002 ISL_F_SET(bset
, ISL_BASIC_SET_NO_IMPLICIT
);
3003 ISL_F_SET(bset
, ISL_BASIC_SET_NO_REDUNDANT
);
3008 isl_basic_set_free(bset
);
3009 isl_basic_set_free(context
);
3013 /* Return the number of equality constraints in "bmap" that involve
3014 * local variables. This function assumes that Gaussian elimination
3015 * has been applied to the equality constraints.
3017 static int n_div_eq(__isl_keep isl_basic_map
*bmap
)
3020 isl_size total
, n_div
;
3025 if (bmap
->n_eq
== 0)
3028 total
= isl_basic_map_dim(bmap
, isl_dim_all
);
3029 n_div
= isl_basic_map_dim(bmap
, isl_dim_div
);
3030 if (total
< 0 || n_div
< 0)
3034 for (i
= 0; i
< bmap
->n_eq
; ++i
)
3035 if (isl_seq_first_non_zero(bmap
->eq
[i
] + 1 + total
,
3042 /* Construct a basic map in "space" defined by the equality constraints in "eq".
3043 * The constraints are assumed not to involve any local variables.
3045 static __isl_give isl_basic_map
*basic_map_from_equalities(
3046 __isl_take isl_space
*space
, __isl_take isl_mat
*eq
)
3050 isl_basic_map
*bmap
= NULL
;
3052 total
= isl_space_dim(space
, isl_dim_all
);
3053 if (total
< 0 || !eq
)
3056 if (1 + total
!= eq
->n_col
)
3057 isl_die(isl_space_get_ctx(space
), isl_error_internal
,
3058 "unexpected number of columns", goto error
);
3060 bmap
= isl_basic_map_alloc_space(isl_space_copy(space
),
3062 for (i
= 0; i
< eq
->n_row
; ++i
) {
3063 k
= isl_basic_map_alloc_equality(bmap
);
3066 isl_seq_cpy(bmap
->eq
[k
], eq
->row
[i
], eq
->n_col
);
3069 isl_space_free(space
);
3073 isl_space_free(space
);
3075 isl_basic_map_free(bmap
);
3079 /* Construct and return a variable compression based on the equality
3080 * constraints in "bmap1" and "bmap2" that do not involve the local variables.
3081 * "n1" is the number of (initial) equality constraints in "bmap1"
3082 * that do involve local variables.
3083 * "n2" is the number of (initial) equality constraints in "bmap2"
3084 * that do involve local variables.
3085 * "total" is the total number of other variables.
3086 * This function assumes that Gaussian elimination
3087 * has been applied to the equality constraints in both "bmap1" and "bmap2"
3088 * such that the equality constraints not involving local variables
3089 * are those that start at "n1" or "n2".
3091 * If either of "bmap1" and "bmap2" does not have such equality constraints,
3092 * then simply compute the compression based on the equality constraints
3093 * in the other basic map.
3094 * Otherwise, combine the equality constraints from both into a new
3095 * basic map such that Gaussian elimination can be applied to this combination
3096 * and then construct a variable compression from the resulting
3097 * equality constraints.
3099 static __isl_give isl_mat
*combined_variable_compression(
3100 __isl_keep isl_basic_map
*bmap1
, int n1
,
3101 __isl_keep isl_basic_map
*bmap2
, int n2
, int total
)
3104 isl_mat
*E1
, *E2
, *V
;
3105 isl_basic_map
*bmap
;
3107 ctx
= isl_basic_map_get_ctx(bmap1
);
3108 if (bmap1
->n_eq
== n1
) {
3109 E2
= isl_mat_sub_alloc6(ctx
, bmap2
->eq
,
3110 n2
, bmap2
->n_eq
- n2
, 0, 1 + total
);
3111 return isl_mat_variable_compression(E2
, NULL
);
3113 if (bmap2
->n_eq
== n2
) {
3114 E1
= isl_mat_sub_alloc6(ctx
, bmap1
->eq
,
3115 n1
, bmap1
->n_eq
- n1
, 0, 1 + total
);
3116 return isl_mat_variable_compression(E1
, NULL
);
3118 E1
= isl_mat_sub_alloc6(ctx
, bmap1
->eq
,
3119 n1
, bmap1
->n_eq
- n1
, 0, 1 + total
);
3120 E2
= isl_mat_sub_alloc6(ctx
, bmap2
->eq
,
3121 n2
, bmap2
->n_eq
- n2
, 0, 1 + total
);
3122 E1
= isl_mat_concat(E1
, E2
);
3123 bmap
= basic_map_from_equalities(isl_basic_map_get_space(bmap1
), E1
);
3124 bmap
= isl_basic_map_gauss(bmap
, NULL
);
3127 E1
= isl_mat_sub_alloc6(ctx
, bmap
->eq
, 0, bmap
->n_eq
, 0, 1 + total
);
3128 V
= isl_mat_variable_compression(E1
, NULL
);
3129 isl_basic_map_free(bmap
);
3134 /* Extract the stride constraints from "bmap", compressed
3135 * with respect to both the stride constraints in "context" and
3136 * the remaining equality constraints in both "bmap" and "context".
3137 * "bmap_n_eq" is the number of (initial) stride constraints in "bmap".
3138 * "context_n_eq" is the number of (initial) stride constraints in "context".
3140 * Let x be all variables in "bmap" (and "context") other than the local
3141 * variables. First compute a variable compression
3145 * based on the non-stride equality constraints in "bmap" and "context".
3146 * Consider the stride constraints of "context",
3150 * with y the local variables and plug in the variable compression,
3153 * A(V x') + B(y) = 0
3155 * Use these constraints to compute a parameter compression on x'
3159 * Now consider the stride constraints of "bmap"
3163 * and plug in x = V*T x''.
3164 * That is, return A = [C*V*T D].
3166 static __isl_give isl_mat
*extract_compressed_stride_constraints(
3167 __isl_keep isl_basic_map
*bmap
, int bmap_n_eq
,
3168 __isl_keep isl_basic_map
*context
, int context_n_eq
)
3170 isl_size total
, n_div
;
3172 isl_mat
*A
, *B
, *T
, *V
;
3174 total
= isl_basic_map_dim(context
, isl_dim_all
);
3175 n_div
= isl_basic_map_dim(context
, isl_dim_div
);
3176 if (total
< 0 || n_div
< 0)
3180 ctx
= isl_basic_map_get_ctx(bmap
);
3182 V
= combined_variable_compression(bmap
, bmap_n_eq
,
3183 context
, context_n_eq
, total
);
3185 A
= isl_mat_sub_alloc6(ctx
, context
->eq
, 0, context_n_eq
, 0, 1 + total
);
3186 B
= isl_mat_sub_alloc6(ctx
, context
->eq
,
3187 0, context_n_eq
, 1 + total
, n_div
);
3188 A
= isl_mat_product(A
, isl_mat_copy(V
));
3189 T
= isl_mat_parameter_compression_ext(A
, B
);
3190 T
= isl_mat_product(V
, T
);
3192 n_div
= isl_basic_map_dim(bmap
, isl_dim_div
);
3194 T
= isl_mat_free(T
);
3196 T
= isl_mat_diagonal(T
, isl_mat_identity(ctx
, n_div
));
3198 A
= isl_mat_sub_alloc6(ctx
, bmap
->eq
,
3199 0, bmap_n_eq
, 0, 1 + total
+ n_div
);
3200 A
= isl_mat_product(A
, T
);
3205 /* Remove the prime factors from *g that have an exponent that
3206 * is strictly smaller than the exponent in "c".
3207 * All exponents in *g are known to be smaller than or equal
3210 * That is, if *g is equal to
3212 * p_1^{e_1} p_2^{e_2} ... p_n^{e_n}
3214 * and "c" is equal to
3216 * p_1^{f_1} p_2^{f_2} ... p_n^{f_n}
3220 * p_1^{e_1 * (e_1 = f_1)} p_2^{e_2 * (e_2 = f_2)} ...
3221 * p_n^{e_n * (e_n = f_n)}
3223 * If e_i = f_i, then c / *g does not have any p_i factors and therefore
3224 * neither does the gcd of *g and c / *g.
3225 * If e_i < f_i, then the gcd of *g and c / *g has a positive
3226 * power min(e_i, s_i) of p_i with s_i = f_i - e_i among its factors.
3227 * Dividing *g by this gcd therefore strictly reduces the exponent
3228 * of the prime factors that need to be removed, while leaving the
3229 * other prime factors untouched.
3230 * Repeating this process until gcd(*g, c / *g) = 1 therefore
3231 * removes all undesired factors, without removing any others.
3233 static void remove_incomplete_powers(isl_int
*g
, isl_int c
)
3239 isl_int_divexact(t
, c
, *g
);
3240 isl_int_gcd(t
, t
, *g
);
3241 if (isl_int_is_one(t
))
3243 isl_int_divexact(*g
, *g
, t
);
3248 /* Reduce the "n" stride constraints in "bmap" based on a copy "A"
3249 * of the same stride constraints in a compressed space that exploits
3250 * all equalities in the context and the other equalities in "bmap".
3252 * If the stride constraints of "bmap" are of the form
3256 * then A is of the form
3260 * If any of these constraints involves only a single local variable y,
3261 * then the constraint appears as
3271 * Let g be the gcd of m and the coefficients of h.
3272 * Then, in particular, g is a divisor of the coefficients of h and
3276 * is known to be a multiple of g.
3277 * If some prime factor in m appears with the same exponent in g,
3278 * then it can be removed from m because f(x) is already known
3279 * to be a multiple of g and therefore in particular of this power
3280 * of the prime factors.
3281 * Prime factors that appear with a smaller exponent in g cannot
3282 * be removed from m.
3283 * Let g' be the divisor of g containing all prime factors that
3284 * appear with the same exponent in m and g, then
3288 * can be replaced by
3290 * f(x) + m/g' y_i' = 0
3292 * Note that (if g' != 1) this changes the explicit representation
3293 * of y_i to that of y_i', so the integer division at position i
3294 * is marked unknown and later recomputed by a call to
3295 * isl_basic_map_gauss.
3297 static __isl_give isl_basic_map
*reduce_stride_constraints(
3298 __isl_take isl_basic_map
*bmap
, int n
, __isl_keep isl_mat
*A
)
3301 isl_size total
, n_div
;
3305 total
= isl_basic_map_dim(bmap
, isl_dim_all
);
3306 n_div
= isl_basic_map_dim(bmap
, isl_dim_div
);
3307 if (total
< 0 || n_div
< 0 || !A
)
3308 return isl_basic_map_free(bmap
);
3312 for (i
= 0; i
< n
; ++i
) {
3315 div
= isl_seq_first_non_zero(bmap
->eq
[i
] + 1 + total
, n_div
);
3317 isl_die(isl_basic_map_get_ctx(bmap
), isl_error_internal
,
3318 "equality constraints modified unexpectedly",
3320 if (isl_seq_first_non_zero(bmap
->eq
[i
] + 1 + total
+ div
+ 1,
3321 n_div
- div
- 1) != -1)
3323 if (isl_mat_row_gcd(A
, i
, &gcd
) < 0)
3325 if (isl_int_is_one(gcd
))
3327 remove_incomplete_powers(&gcd
, bmap
->eq
[i
][1 + total
+ div
]);
3328 if (isl_int_is_one(gcd
))
3330 isl_int_divexact(bmap
->eq
[i
][1 + total
+ div
],
3331 bmap
->eq
[i
][1 + total
+ div
], gcd
);
3332 bmap
= isl_basic_map_mark_div_unknown(bmap
, div
);
3340 bmap
= isl_basic_map_gauss(bmap
, NULL
);
3345 isl_basic_map_free(bmap
);
3349 /* Simplify the stride constraints in "bmap" based on
3350 * the remaining equality constraints in "bmap" and all equality
3351 * constraints in "context".
3352 * Only do this if both "bmap" and "context" have stride constraints.
3354 * First extract a copy of the stride constraints in "bmap" in a compressed
3355 * space exploiting all the other equality constraints and then
3356 * use this compressed copy to simplify the original stride constraints.
3358 static __isl_give isl_basic_map
*gist_strides(__isl_take isl_basic_map
*bmap
,
3359 __isl_keep isl_basic_map
*context
)
3361 int bmap_n_eq
, context_n_eq
;
3364 if (!bmap
|| !context
)
3365 return isl_basic_map_free(bmap
);
3367 bmap_n_eq
= n_div_eq(bmap
);
3368 context_n_eq
= n_div_eq(context
);
3370 if (bmap_n_eq
< 0 || context_n_eq
< 0)
3371 return isl_basic_map_free(bmap
);
3372 if (bmap_n_eq
== 0 || context_n_eq
== 0)
3375 A
= extract_compressed_stride_constraints(bmap
, bmap_n_eq
,
3376 context
, context_n_eq
);
3377 bmap
= reduce_stride_constraints(bmap
, bmap_n_eq
, A
);
3384 /* Return a basic map that has the same intersection with "context" as "bmap"
3385 * and that is as "simple" as possible.
3387 * The core computation is performed on the pure constraints.
3388 * When we add back the meaning of the integer divisions, we need
3389 * to (re)introduce the div constraints. If we happen to have
3390 * discovered that some of these integer divisions are equal to
3391 * some affine combination of other variables, then these div
3392 * constraints may end up getting simplified in terms of the equalities,
3393 * resulting in extra inequalities on the other variables that
3394 * may have been removed already or that may not even have been
3395 * part of the input. We try and remove those constraints of
3396 * this form that are most obviously redundant with respect to
3397 * the context. We also remove those div constraints that are
3398 * redundant with respect to the other constraints in the result.
3400 * The stride constraints among the equality constraints in "bmap" are
3401 * also simplified with respecting to the other equality constraints
3402 * in "bmap" and with respect to all equality constraints in "context".
3404 __isl_give isl_basic_map
*isl_basic_map_gist(__isl_take isl_basic_map
*bmap
,
3405 __isl_take isl_basic_map
*context
)
3407 isl_basic_set
*bset
, *eq
;
3408 isl_basic_map
*eq_bmap
;
3409 isl_size total
, n_div
, n_div_bmap
;
3410 unsigned extra
, n_eq
, n_ineq
;
3412 if (!bmap
|| !context
)
3415 if (isl_basic_map_plain_is_universe(bmap
)) {
3416 isl_basic_map_free(context
);
3419 if (isl_basic_map_plain_is_empty(context
)) {
3420 isl_space
*space
= isl_basic_map_get_space(bmap
);
3421 isl_basic_map_free(bmap
);
3422 isl_basic_map_free(context
);
3423 return isl_basic_map_universe(space
);
3425 if (isl_basic_map_plain_is_empty(bmap
)) {
3426 isl_basic_map_free(context
);
3430 bmap
= isl_basic_map_remove_redundancies(bmap
);
3431 context
= isl_basic_map_remove_redundancies(context
);
3432 bmap
= isl_basic_map_order_divs(bmap
);
3433 context
= isl_basic_map_align_divs(context
, bmap
);
3435 n_div
= isl_basic_map_dim(context
, isl_dim_div
);
3436 total
= isl_basic_map_dim(bmap
, isl_dim_all
);
3437 n_div_bmap
= isl_basic_map_dim(bmap
, isl_dim_div
);
3438 if (n_div
< 0 || total
< 0 || n_div_bmap
< 0)
3440 extra
= n_div
- n_div_bmap
;
3442 bset
= isl_basic_map_underlying_set(isl_basic_map_copy(bmap
));
3443 bset
= isl_basic_set_add_dims(bset
, isl_dim_set
, extra
);
3444 bset
= uset_gist(bset
,
3445 isl_basic_map_underlying_set(isl_basic_map_copy(context
)));
3446 bset
= isl_basic_set_project_out(bset
, isl_dim_set
, total
, extra
);
3448 if (!bset
|| bset
->n_eq
== 0 || n_div
== 0 ||
3449 isl_basic_set_plain_is_empty(bset
)) {
3450 isl_basic_map_free(context
);
3451 return isl_basic_map_overlying_set(bset
, bmap
);
3455 n_ineq
= bset
->n_ineq
;
3456 eq
= isl_basic_set_copy(bset
);
3457 eq
= isl_basic_set_cow(eq
);
3458 eq
= isl_basic_set_free_inequality(eq
, n_ineq
);
3459 bset
= isl_basic_set_free_equality(bset
, n_eq
);
3461 eq_bmap
= isl_basic_map_overlying_set(eq
, isl_basic_map_copy(bmap
));
3462 eq_bmap
= gist_strides(eq_bmap
, context
);
3463 eq_bmap
= isl_basic_map_remove_shifted_constraints(eq_bmap
, context
);
3464 bmap
= isl_basic_map_overlying_set(bset
, bmap
);
3465 bmap
= isl_basic_map_intersect(bmap
, eq_bmap
);
3466 bmap
= isl_basic_map_remove_redundancies(bmap
);
3470 isl_basic_map_free(bmap
);
3471 isl_basic_map_free(context
);
3476 * Assumes context has no implicit divs.
3478 __isl_give isl_map
*isl_map_gist_basic_map(__isl_take isl_map
*map
,
3479 __isl_take isl_basic_map
*context
)
3483 if (!map
|| !context
)
3486 if (isl_basic_map_plain_is_empty(context
)) {
3487 isl_space
*space
= isl_map_get_space(map
);
3489 isl_basic_map_free(context
);
3490 return isl_map_universe(space
);
3493 context
= isl_basic_map_remove_redundancies(context
);
3494 map
= isl_map_cow(map
);
3495 if (isl_map_basic_map_check_equal_space(map
, context
) < 0)
3497 map
= isl_map_compute_divs(map
);
3500 for (i
= map
->n
- 1; i
>= 0; --i
) {
3501 map
->p
[i
] = isl_basic_map_gist(map
->p
[i
],
3502 isl_basic_map_copy(context
));
3505 if (isl_basic_map_plain_is_empty(map
->p
[i
])) {
3506 isl_basic_map_free(map
->p
[i
]);
3507 if (i
!= map
->n
- 1)
3508 map
->p
[i
] = map
->p
[map
->n
- 1];
3512 isl_basic_map_free(context
);
3513 ISL_F_CLR(map
, ISL_MAP_NORMALIZED
);
3517 isl_basic_map_free(context
);
3521 /* Drop all inequalities from "bmap" that also appear in "context".
3522 * "context" is assumed to have only known local variables and
3523 * the initial local variables of "bmap" are assumed to be the same
3524 * as those of "context".
3525 * The constraints of both "bmap" and "context" are assumed
3526 * to have been sorted using isl_basic_map_sort_constraints.
3528 * Run through the inequality constraints of "bmap" and "context"
3530 * If a constraint of "bmap" involves variables not in "context",
3531 * then it cannot appear in "context".
3532 * If a matching constraint is found, it is removed from "bmap".
3534 static __isl_give isl_basic_map
*drop_inequalities(
3535 __isl_take isl_basic_map
*bmap
, __isl_keep isl_basic_map
*context
)
3538 isl_size total
, bmap_total
;
3541 total
= isl_basic_map_dim(context
, isl_dim_all
);
3542 bmap_total
= isl_basic_map_dim(bmap
, isl_dim_all
);
3543 if (total
< 0 || bmap_total
< 0)
3544 return isl_basic_map_free(bmap
);
3546 extra
= bmap_total
- total
;
3548 i1
= bmap
->n_ineq
- 1;
3549 i2
= context
->n_ineq
- 1;
3550 while (bmap
&& i1
>= 0 && i2
>= 0) {
3553 if (isl_seq_first_non_zero(bmap
->ineq
[i1
] + 1 + total
,
3558 cmp
= isl_basic_map_constraint_cmp(context
, bmap
->ineq
[i1
],
3568 if (isl_int_eq(bmap
->ineq
[i1
][0], context
->ineq
[i2
][0])) {
3569 bmap
= isl_basic_map_cow(bmap
);
3570 if (isl_basic_map_drop_inequality(bmap
, i1
) < 0)
3571 bmap
= isl_basic_map_free(bmap
);
3580 /* Drop all equalities from "bmap" that also appear in "context".
3581 * "context" is assumed to have only known local variables and
3582 * the initial local variables of "bmap" are assumed to be the same
3583 * as those of "context".
3585 * Run through the equality constraints of "bmap" and "context"
3587 * If a constraint of "bmap" involves variables not in "context",
3588 * then it cannot appear in "context".
3589 * If a matching constraint is found, it is removed from "bmap".
3591 static __isl_give isl_basic_map
*drop_equalities(
3592 __isl_take isl_basic_map
*bmap
, __isl_keep isl_basic_map
*context
)
3595 isl_size total
, bmap_total
;
3598 total
= isl_basic_map_dim(context
, isl_dim_all
);
3599 bmap_total
= isl_basic_map_dim(bmap
, isl_dim_all
);
3600 if (total
< 0 || bmap_total
< 0)
3601 return isl_basic_map_free(bmap
);
3603 extra
= bmap_total
- total
;
3605 i1
= bmap
->n_eq
- 1;
3606 i2
= context
->n_eq
- 1;
3608 while (bmap
&& i1
>= 0 && i2
>= 0) {
3611 if (isl_seq_first_non_zero(bmap
->eq
[i1
] + 1 + total
,
3614 last1
= isl_seq_last_non_zero(bmap
->eq
[i1
] + 1, total
);
3615 last2
= isl_seq_last_non_zero(context
->eq
[i2
] + 1, total
);
3616 if (last1
> last2
) {
3620 if (last1
< last2
) {
3624 if (isl_seq_eq(bmap
->eq
[i1
], context
->eq
[i2
], 1 + total
)) {
3625 bmap
= isl_basic_map_cow(bmap
);
3626 if (isl_basic_map_drop_equality(bmap
, i1
) < 0)
3627 bmap
= isl_basic_map_free(bmap
);
3636 /* Remove the constraints in "context" from "bmap".
3637 * "context" is assumed to have explicit representations
3638 * for all local variables.
3640 * First align the divs of "bmap" to those of "context" and
3641 * sort the constraints. Then drop all constraints from "bmap"
3642 * that appear in "context".
3644 __isl_give isl_basic_map
*isl_basic_map_plain_gist(
3645 __isl_take isl_basic_map
*bmap
, __isl_take isl_basic_map
*context
)
3647 isl_bool done
, known
;
3649 done
= isl_basic_map_plain_is_universe(context
);
3650 if (done
== isl_bool_false
)
3651 done
= isl_basic_map_plain_is_universe(bmap
);
3652 if (done
== isl_bool_false
)
3653 done
= isl_basic_map_plain_is_empty(context
);
3654 if (done
== isl_bool_false
)
3655 done
= isl_basic_map_plain_is_empty(bmap
);
3659 isl_basic_map_free(context
);
3662 known
= isl_basic_map_divs_known(context
);
3666 isl_die(isl_basic_map_get_ctx(bmap
), isl_error_invalid
,
3667 "context has unknown divs", goto error
);
3669 context
= isl_basic_map_order_divs(context
);
3670 bmap
= isl_basic_map_align_divs(bmap
, context
);
3671 bmap
= isl_basic_map_gauss(bmap
, NULL
);
3672 bmap
= isl_basic_map_sort_constraints(bmap
);
3673 context
= isl_basic_map_sort_constraints(context
);
3675 bmap
= drop_inequalities(bmap
, context
);
3676 bmap
= drop_equalities(bmap
, context
);
3678 isl_basic_map_free(context
);
3679 bmap
= isl_basic_map_finalize(bmap
);
3682 isl_basic_map_free(bmap
);
3683 isl_basic_map_free(context
);
3687 /* Replace "map" by the disjunct at position "pos" and free "context".
3689 static __isl_give isl_map
*replace_by_disjunct(__isl_take isl_map
*map
,
3690 int pos
, __isl_take isl_basic_map
*context
)
3692 isl_basic_map
*bmap
;
3694 bmap
= isl_basic_map_copy(map
->p
[pos
]);
3696 isl_basic_map_free(context
);
3697 return isl_map_from_basic_map(bmap
);
3700 /* Remove the constraints in "context" from "map".
3701 * If any of the disjuncts in the result turns out to be the universe,
3702 * then return this universe.
3703 * "context" is assumed to have explicit representations
3704 * for all local variables.
3706 __isl_give isl_map
*isl_map_plain_gist_basic_map(__isl_take isl_map
*map
,
3707 __isl_take isl_basic_map
*context
)
3710 isl_bool univ
, known
;
3712 univ
= isl_basic_map_plain_is_universe(context
);
3716 isl_basic_map_free(context
);
3719 known
= isl_basic_map_divs_known(context
);
3723 isl_die(isl_map_get_ctx(map
), isl_error_invalid
,
3724 "context has unknown divs", goto error
);
3726 map
= isl_map_cow(map
);
3729 for (i
= 0; i
< map
->n
; ++i
) {
3730 map
->p
[i
] = isl_basic_map_plain_gist(map
->p
[i
],
3731 isl_basic_map_copy(context
));
3732 univ
= isl_basic_map_plain_is_universe(map
->p
[i
]);
3735 if (univ
&& map
->n
> 1)
3736 return replace_by_disjunct(map
, i
, context
);
3739 isl_basic_map_free(context
);
3740 ISL_F_CLR(map
, ISL_MAP_NORMALIZED
);
3742 ISL_F_CLR(map
, ISL_MAP_DISJOINT
);
3746 isl_basic_map_free(context
);
3750 /* Remove the constraints in "context" from "set".
3751 * If any of the disjuncts in the result turns out to be the universe,
3752 * then return this universe.
3753 * "context" is assumed to have explicit representations
3754 * for all local variables.
3756 __isl_give isl_set
*isl_set_plain_gist_basic_set(__isl_take isl_set
*set
,
3757 __isl_take isl_basic_set
*context
)
3759 return set_from_map(isl_map_plain_gist_basic_map(set_to_map(set
),
3760 bset_to_bmap(context
)));
3763 /* Remove the constraints in "context" from "map".
3764 * If any of the disjuncts in the result turns out to be the universe,
3765 * then return this universe.
3766 * "context" is assumed to consist of a single disjunct and
3767 * to have explicit representations for all local variables.
3769 __isl_give isl_map
*isl_map_plain_gist(__isl_take isl_map
*map
,
3770 __isl_take isl_map
*context
)
3772 isl_basic_map
*hull
;
3774 hull
= isl_map_unshifted_simple_hull(context
);
3775 return isl_map_plain_gist_basic_map(map
, hull
);
3778 /* Replace "map" by a universe map in the same space and free "drop".
3780 static __isl_give isl_map
*replace_by_universe(__isl_take isl_map
*map
,
3781 __isl_take isl_map
*drop
)
3785 res
= isl_map_universe(isl_map_get_space(map
));
3791 /* Return a map that has the same intersection with "context" as "map"
3792 * and that is as "simple" as possible.
3794 * If "map" is already the universe, then we cannot make it any simpler.
3795 * Similarly, if "context" is the universe, then we cannot exploit it
3797 * If "map" and "context" are identical to each other, then we can
3798 * return the corresponding universe.
3800 * If either "map" or "context" consists of multiple disjuncts,
3801 * then check if "context" happens to be a subset of "map",
3802 * in which case all constraints can be removed.
3803 * In case of multiple disjuncts, the standard procedure
3804 * may not be able to detect that all constraints can be removed.
3806 * If none of these cases apply, we have to work a bit harder.
3807 * During this computation, we make use of a single disjunct context,
3808 * so if the original context consists of more than one disjunct
3809 * then we need to approximate the context by a single disjunct set.
3810 * Simply taking the simple hull may drop constraints that are
3811 * only implicitly available in each disjunct. We therefore also
3812 * look for constraints among those defining "map" that are valid
3813 * for the context. These can then be used to simplify away
3814 * the corresponding constraints in "map".
3816 __isl_give isl_map
*isl_map_gist(__isl_take isl_map
*map
,
3817 __isl_take isl_map
*context
)
3821 isl_size n_disjunct_map
, n_disjunct_context
;
3823 isl_basic_map
*hull
;
3825 is_universe
= isl_map_plain_is_universe(map
);
3826 if (is_universe
>= 0 && !is_universe
)
3827 is_universe
= isl_map_plain_is_universe(context
);
3828 if (is_universe
< 0)
3831 isl_map_free(context
);
3835 isl_map_align_params_bin(&map
, &context
);
3836 equal
= isl_map_plain_is_equal(map
, context
);
3840 return replace_by_universe(map
, context
);
3842 n_disjunct_map
= isl_map_n_basic_map(map
);
3843 n_disjunct_context
= isl_map_n_basic_map(context
);
3844 if (n_disjunct_map
< 0 || n_disjunct_context
< 0)
3846 if (n_disjunct_map
!= 1 || n_disjunct_context
!= 1) {
3847 subset
= isl_map_is_subset(context
, map
);
3851 return replace_by_universe(map
, context
);
3854 context
= isl_map_compute_divs(context
);
3857 if (n_disjunct_context
== 1) {
3858 hull
= isl_map_simple_hull(context
);
3863 ctx
= isl_map_get_ctx(map
);
3864 list
= isl_map_list_alloc(ctx
, 2);
3865 list
= isl_map_list_add(list
, isl_map_copy(context
));
3866 list
= isl_map_list_add(list
, isl_map_copy(map
));
3867 hull
= isl_map_unshifted_simple_hull_from_map_list(context
,
3870 return isl_map_gist_basic_map(map
, hull
);
3873 isl_map_free(context
);
3877 __isl_give isl_basic_set
*isl_basic_set_gist(__isl_take isl_basic_set
*bset
,
3878 __isl_take isl_basic_set
*context
)
3880 return bset_from_bmap(isl_basic_map_gist(bset_to_bmap(bset
),
3881 bset_to_bmap(context
)));
3884 __isl_give isl_set
*isl_set_gist_basic_set(__isl_take isl_set
*set
,
3885 __isl_take isl_basic_set
*context
)
3887 return set_from_map(isl_map_gist_basic_map(set_to_map(set
),
3888 bset_to_bmap(context
)));
3891 __isl_give isl_set
*isl_set_gist_params_basic_set(__isl_take isl_set
*set
,
3892 __isl_take isl_basic_set
*context
)
3894 isl_space
*space
= isl_set_get_space(set
);
3895 isl_basic_set
*dom_context
= isl_basic_set_universe(space
);
3896 dom_context
= isl_basic_set_intersect_params(dom_context
, context
);
3897 return isl_set_gist_basic_set(set
, dom_context
);
3900 __isl_give isl_set
*isl_set_gist(__isl_take isl_set
*set
,
3901 __isl_take isl_set
*context
)
3903 return set_from_map(isl_map_gist(set_to_map(set
), set_to_map(context
)));
3906 /* Compute the gist of "bmap" with respect to the constraints "context"
3909 __isl_give isl_basic_map
*isl_basic_map_gist_domain(
3910 __isl_take isl_basic_map
*bmap
, __isl_take isl_basic_set
*context
)
3912 isl_space
*space
= isl_basic_map_get_space(bmap
);
3913 isl_basic_map
*bmap_context
= isl_basic_map_universe(space
);
3915 bmap_context
= isl_basic_map_intersect_domain(bmap_context
, context
);
3916 return isl_basic_map_gist(bmap
, bmap_context
);
3919 __isl_give isl_map
*isl_map_gist_domain(__isl_take isl_map
*map
,
3920 __isl_take isl_set
*context
)
3922 isl_map
*map_context
= isl_map_universe(isl_map_get_space(map
));
3923 map_context
= isl_map_intersect_domain(map_context
, context
);
3924 return isl_map_gist(map
, map_context
);
3927 __isl_give isl_map
*isl_map_gist_range(__isl_take isl_map
*map
,
3928 __isl_take isl_set
*context
)
3930 isl_map
*map_context
= isl_map_universe(isl_map_get_space(map
));
3931 map_context
= isl_map_intersect_range(map_context
, context
);
3932 return isl_map_gist(map
, map_context
);
3935 __isl_give isl_map
*isl_map_gist_params(__isl_take isl_map
*map
,
3936 __isl_take isl_set
*context
)
3938 isl_map
*map_context
= isl_map_universe(isl_map_get_space(map
));
3939 map_context
= isl_map_intersect_params(map_context
, context
);
3940 return isl_map_gist(map
, map_context
);
3943 __isl_give isl_set
*isl_set_gist_params(__isl_take isl_set
*set
,
3944 __isl_take isl_set
*context
)
3946 return isl_map_gist_params(set
, context
);
3949 /* Quick check to see if two basic maps are disjoint.
3950 * In particular, we reduce the equalities and inequalities of
3951 * one basic map in the context of the equalities of the other
3952 * basic map and check if we get a contradiction.
3954 isl_bool
isl_basic_map_plain_is_disjoint(__isl_keep isl_basic_map
*bmap1
,
3955 __isl_keep isl_basic_map
*bmap2
)
3957 struct isl_vec
*v
= NULL
;
3962 if (isl_basic_map_check_equal_space(bmap1
, bmap2
) < 0)
3963 return isl_bool_error
;
3964 if (bmap1
->n_div
|| bmap2
->n_div
)
3965 return isl_bool_false
;
3966 if (!bmap1
->n_eq
&& !bmap2
->n_eq
)
3967 return isl_bool_false
;
3969 total
= isl_space_dim(bmap1
->dim
, isl_dim_all
);
3971 return isl_bool_error
;
3973 return isl_bool_false
;
3974 v
= isl_vec_alloc(bmap1
->ctx
, 1 + total
);
3977 elim
= isl_alloc_array(bmap1
->ctx
, int, total
);
3980 compute_elimination_index(bmap1
, elim
, total
);
3981 for (i
= 0; i
< bmap2
->n_eq
; ++i
) {
3983 reduced
= reduced_using_equalities(v
->block
.data
, bmap2
->eq
[i
],
3984 bmap1
, elim
, total
);
3985 if (reduced
&& !isl_int_is_zero(v
->block
.data
[0]) &&
3986 isl_seq_first_non_zero(v
->block
.data
+ 1, total
) == -1)
3989 for (i
= 0; i
< bmap2
->n_ineq
; ++i
) {
3991 reduced
= reduced_using_equalities(v
->block
.data
,
3992 bmap2
->ineq
[i
], bmap1
, elim
, total
);
3993 if (reduced
&& isl_int_is_neg(v
->block
.data
[0]) &&
3994 isl_seq_first_non_zero(v
->block
.data
+ 1, total
) == -1)
3997 compute_elimination_index(bmap2
, elim
, total
);
3998 for (i
= 0; i
< bmap1
->n_ineq
; ++i
) {
4000 reduced
= reduced_using_equalities(v
->block
.data
,
4001 bmap1
->ineq
[i
], bmap2
, elim
, total
);
4002 if (reduced
&& isl_int_is_neg(v
->block
.data
[0]) &&
4003 isl_seq_first_non_zero(v
->block
.data
+ 1, total
) == -1)
4008 return isl_bool_false
;
4012 return isl_bool_true
;
4016 return isl_bool_error
;
4019 int isl_basic_set_plain_is_disjoint(__isl_keep isl_basic_set
*bset1
,
4020 __isl_keep isl_basic_set
*bset2
)
4022 return isl_basic_map_plain_is_disjoint(bset_to_bmap(bset1
),
4023 bset_to_bmap(bset2
));
4026 /* Does "test" hold for all pairs of basic maps in "map1" and "map2"?
4028 static isl_bool
all_pairs(__isl_keep isl_map
*map1
, __isl_keep isl_map
*map2
,
4029 isl_bool (*test
)(__isl_keep isl_basic_map
*bmap1
,
4030 __isl_keep isl_basic_map
*bmap2
))
4035 return isl_bool_error
;
4037 for (i
= 0; i
< map1
->n
; ++i
) {
4038 for (j
= 0; j
< map2
->n
; ++j
) {
4039 isl_bool d
= test(map1
->p
[i
], map2
->p
[j
]);
4040 if (d
!= isl_bool_true
)
4045 return isl_bool_true
;
4048 /* Are "map1" and "map2" obviously disjoint, based on information
4049 * that can be derived without looking at the individual basic maps?
4051 * In particular, if one of them is empty or if they live in different spaces
4052 * (ignoring parameters), then they are clearly disjoint.
4054 static isl_bool
isl_map_plain_is_disjoint_global(__isl_keep isl_map
*map1
,
4055 __isl_keep isl_map
*map2
)
4061 return isl_bool_error
;
4063 disjoint
= isl_map_plain_is_empty(map1
);
4064 if (disjoint
< 0 || disjoint
)
4067 disjoint
= isl_map_plain_is_empty(map2
);
4068 if (disjoint
< 0 || disjoint
)
4071 match
= isl_map_tuple_is_equal(map1
, isl_dim_in
, map2
, isl_dim_in
);
4072 if (match
< 0 || !match
)
4073 return match
< 0 ? isl_bool_error
: isl_bool_true
;
4075 match
= isl_map_tuple_is_equal(map1
, isl_dim_out
, map2
, isl_dim_out
);
4076 if (match
< 0 || !match
)
4077 return match
< 0 ? isl_bool_error
: isl_bool_true
;
4079 return isl_bool_false
;
4082 /* Are "map1" and "map2" obviously disjoint?
4084 * If one of them is empty or if they live in different spaces (ignoring
4085 * parameters), then they are clearly disjoint.
4086 * This is checked by isl_map_plain_is_disjoint_global.
4088 * If they have different parameters, then we skip any further tests.
4090 * If they are obviously equal, but not obviously empty, then we will
4091 * not be able to detect if they are disjoint.
4093 * Otherwise we check if each basic map in "map1" is obviously disjoint
4094 * from each basic map in "map2".
4096 isl_bool
isl_map_plain_is_disjoint(__isl_keep isl_map
*map1
,
4097 __isl_keep isl_map
*map2
)
4103 disjoint
= isl_map_plain_is_disjoint_global(map1
, map2
);
4104 if (disjoint
< 0 || disjoint
)
4107 match
= isl_map_has_equal_params(map1
, map2
);
4108 if (match
< 0 || !match
)
4109 return match
< 0 ? isl_bool_error
: isl_bool_false
;
4111 intersect
= isl_map_plain_is_equal(map1
, map2
);
4112 if (intersect
< 0 || intersect
)
4113 return intersect
< 0 ? isl_bool_error
: isl_bool_false
;
4115 return all_pairs(map1
, map2
, &isl_basic_map_plain_is_disjoint
);
4118 /* Are "map1" and "map2" disjoint?
4119 * The parameters are assumed to have been aligned.
4121 * In particular, check whether all pairs of basic maps are disjoint.
4123 static isl_bool
isl_map_is_disjoint_aligned(__isl_keep isl_map
*map1
,
4124 __isl_keep isl_map
*map2
)
4126 return all_pairs(map1
, map2
, &isl_basic_map_is_disjoint
);
4129 /* Are "map1" and "map2" disjoint?
4131 * They are disjoint if they are "obviously disjoint" or if one of them
4132 * is empty. Otherwise, they are not disjoint if one of them is universal.
4133 * If the two inputs are (obviously) equal and not empty, then they are
4135 * If none of these cases apply, then check if all pairs of basic maps
4136 * are disjoint after aligning the parameters.
4138 isl_bool
isl_map_is_disjoint(__isl_keep isl_map
*map1
, __isl_keep isl_map
*map2
)
4143 disjoint
= isl_map_plain_is_disjoint_global(map1
, map2
);
4144 if (disjoint
< 0 || disjoint
)
4147 disjoint
= isl_map_is_empty(map1
);
4148 if (disjoint
< 0 || disjoint
)
4151 disjoint
= isl_map_is_empty(map2
);
4152 if (disjoint
< 0 || disjoint
)
4155 intersect
= isl_map_plain_is_universe(map1
);
4156 if (intersect
< 0 || intersect
)
4157 return isl_bool_not(intersect
);
4159 intersect
= isl_map_plain_is_universe(map2
);
4160 if (intersect
< 0 || intersect
)
4161 return isl_bool_not(intersect
);
4163 intersect
= isl_map_plain_is_equal(map1
, map2
);
4164 if (intersect
< 0 || intersect
)
4165 return isl_bool_not(intersect
);
4167 return isl_map_align_params_map_map_and_test(map1
, map2
,
4168 &isl_map_is_disjoint_aligned
);
4171 /* Are "bmap1" and "bmap2" disjoint?
4173 * They are disjoint if they are "obviously disjoint" or if one of them
4174 * is empty. Otherwise, they are not disjoint if one of them is universal.
4175 * If none of these cases apply, we compute the intersection and see if
4176 * the result is empty.
4178 isl_bool
isl_basic_map_is_disjoint(__isl_keep isl_basic_map
*bmap1
,
4179 __isl_keep isl_basic_map
*bmap2
)
4183 isl_basic_map
*test
;
4185 disjoint
= isl_basic_map_plain_is_disjoint(bmap1
, bmap2
);
4186 if (disjoint
< 0 || disjoint
)
4189 disjoint
= isl_basic_map_is_empty(bmap1
);
4190 if (disjoint
< 0 || disjoint
)
4193 disjoint
= isl_basic_map_is_empty(bmap2
);
4194 if (disjoint
< 0 || disjoint
)
4197 intersect
= isl_basic_map_plain_is_universe(bmap1
);
4198 if (intersect
< 0 || intersect
)
4199 return isl_bool_not(intersect
);
4201 intersect
= isl_basic_map_plain_is_universe(bmap2
);
4202 if (intersect
< 0 || intersect
)
4203 return isl_bool_not(intersect
);
4205 test
= isl_basic_map_intersect(isl_basic_map_copy(bmap1
),
4206 isl_basic_map_copy(bmap2
));
4207 disjoint
= isl_basic_map_is_empty(test
);
4208 isl_basic_map_free(test
);
4213 /* Are "bset1" and "bset2" disjoint?
4215 isl_bool
isl_basic_set_is_disjoint(__isl_keep isl_basic_set
*bset1
,
4216 __isl_keep isl_basic_set
*bset2
)
4218 return isl_basic_map_is_disjoint(bset1
, bset2
);
4221 isl_bool
isl_set_plain_is_disjoint(__isl_keep isl_set
*set1
,
4222 __isl_keep isl_set
*set2
)
4224 return isl_map_plain_is_disjoint(set_to_map(set1
), set_to_map(set2
));
4227 /* Are "set1" and "set2" disjoint?
4229 isl_bool
isl_set_is_disjoint(__isl_keep isl_set
*set1
, __isl_keep isl_set
*set2
)
4231 return isl_map_is_disjoint(set1
, set2
);
4234 /* Is "v" equal to 0, 1 or -1?
4236 static int is_zero_or_one(isl_int v
)
4238 return isl_int_is_zero(v
) || isl_int_is_one(v
) || isl_int_is_negone(v
);
4241 /* Are the "n" coefficients starting at "first" of inequality constraints
4242 * "i" and "j" of "bmap" opposite to each other?
4244 static int is_opposite_part(__isl_keep isl_basic_map
*bmap
, int i
, int j
,
4247 return isl_seq_is_neg(bmap
->ineq
[i
] + first
, bmap
->ineq
[j
] + first
, n
);
4250 /* Are inequality constraints "i" and "j" of "bmap" opposite to each other,
4251 * apart from the constant term?
4253 static isl_bool
is_opposite(__isl_keep isl_basic_map
*bmap
, int i
, int j
)
4257 total
= isl_basic_map_dim(bmap
, isl_dim_all
);
4259 return isl_bool_error
;
4260 return is_opposite_part(bmap
, i
, j
, 1, total
);
4263 /* Check if we can combine a given div with lower bound l and upper
4264 * bound u with some other div and if so return that other div.
4265 * Otherwise, return a position beyond the integer divisions.
4266 * Return isl_size_error on error.
4268 * We first check that
4269 * - the bounds are opposites of each other (except for the constant
4271 * - the bounds do not reference any other div
4272 * - no div is defined in terms of this div
4274 * Let m be the size of the range allowed on the div by the bounds.
4275 * That is, the bounds are of the form
4277 * e <= a <= e + m - 1
4279 * with e some expression in the other variables.
4280 * We look for another div b such that no third div is defined in terms
4281 * of this second div b and such that in any constraint that contains
4282 * a (except for the given lower and upper bound), also contains b
4283 * with a coefficient that is m times that of b.
4284 * That is, all constraints (except for the lower and upper bound)
4287 * e + f (a + m b) >= 0
4289 * Furthermore, in the constraints that only contain b, the coefficient
4290 * of b should be equal to 1 or -1.
4291 * If so, we return b so that "a + m b" can be replaced by
4292 * a single div "c = a + m b".
4294 static isl_size
div_find_coalesce(__isl_keep isl_basic_map
*bmap
, int *pairs
,
4295 unsigned div
, unsigned l
, unsigned u
)
4301 isl_bool involves
, opp
;
4303 n_div
= isl_basic_map_dim(bmap
, isl_dim_div
);
4306 v_div
= isl_basic_map_var_offset(bmap
, isl_dim_div
);
4308 return isl_size_error
;
4309 if (isl_seq_first_non_zero(bmap
->ineq
[l
] + 1 + v_div
, div
) != -1)
4311 if (isl_seq_first_non_zero(bmap
->ineq
[l
] + 1 + v_div
+ div
+ 1,
4312 n_div
- div
- 1) != -1)
4314 opp
= is_opposite(bmap
, l
, u
);
4315 if (opp
< 0 || !opp
)
4316 return opp
< 0 ? isl_size_error
: n_div
;
4318 involves
= isl_basic_map_any_div_involves_vars(bmap
, v_div
+ div
, 1);
4319 if (involves
< 0 || involves
)
4320 return involves
< 0 ? isl_size_error
: n_div
;
4322 isl_int_add(bmap
->ineq
[l
][0], bmap
->ineq
[l
][0], bmap
->ineq
[u
][0]);
4323 if (isl_int_is_neg(bmap
->ineq
[l
][0])) {
4324 isl_int_sub(bmap
->ineq
[l
][0],
4325 bmap
->ineq
[l
][0], bmap
->ineq
[u
][0]);
4326 bmap
= isl_basic_map_copy(bmap
);
4327 bmap
= isl_basic_map_set_to_empty(bmap
);
4328 isl_basic_map_free(bmap
);
4331 isl_int_add_ui(bmap
->ineq
[l
][0], bmap
->ineq
[l
][0], 1);
4333 for (i
= 0; i
< n_div
; ++i
) {
4338 involves
= isl_basic_map_any_div_involves_vars(bmap
,
4344 for (j
= 0; j
< bmap
->n_ineq
; ++j
) {
4346 if (j
== l
|| j
== u
)
4348 if (isl_int_is_zero(bmap
->ineq
[j
][1 + v_div
+ div
])) {
4349 if (is_zero_or_one(bmap
->ineq
[j
][1 + v_div
+ i
]))
4353 if (isl_int_is_zero(bmap
->ineq
[j
][1 + v_div
+ i
]))
4355 isl_int_mul(bmap
->ineq
[j
][1 + v_div
+ div
],
4356 bmap
->ineq
[j
][1 + v_div
+ div
],
4358 valid
= isl_int_eq(bmap
->ineq
[j
][1 + v_div
+ div
],
4359 bmap
->ineq
[j
][1 + v_div
+ i
]);
4360 isl_int_divexact(bmap
->ineq
[j
][1 + v_div
+ div
],
4361 bmap
->ineq
[j
][1 + v_div
+ div
],
4366 if (j
< bmap
->n_ineq
)
4372 error
: coalesce
= isl_size_error
;
4373 isl_int_sub_ui(bmap
->ineq
[l
][0], bmap
->ineq
[l
][0], 1);
4374 isl_int_sub(bmap
->ineq
[l
][0], bmap
->ineq
[l
][0], bmap
->ineq
[u
][0]);
4378 /* Internal data structure used during the construction and/or evaluation of
4379 * an inequality that ensures that a pair of bounds always allows
4380 * for an integer value.
4382 * "tab" is the tableau in which the inequality is evaluated. It may
4383 * be NULL until it is actually needed.
4384 * "v" contains the inequality coefficients.
4385 * "g", "fl" and "fu" are temporary scalars used during the construction and
4388 struct test_ineq_data
{
4389 struct isl_tab
*tab
;
4396 /* Free all the memory allocated by the fields of "data".
4398 static void test_ineq_data_clear(struct test_ineq_data
*data
)
4400 isl_tab_free(data
->tab
);
4401 isl_vec_free(data
->v
);
4402 isl_int_clear(data
->g
);
4403 isl_int_clear(data
->fl
);
4404 isl_int_clear(data
->fu
);
4407 /* Is the inequality stored in data->v satisfied by "bmap"?
4408 * That is, does it only attain non-negative values?
4409 * data->tab is a tableau corresponding to "bmap".
4411 static isl_bool
test_ineq_is_satisfied(__isl_keep isl_basic_map
*bmap
,
4412 struct test_ineq_data
*data
)
4415 enum isl_lp_result res
;
4417 ctx
= isl_basic_map_get_ctx(bmap
);
4419 data
->tab
= isl_tab_from_basic_map(bmap
, 0);
4420 res
= isl_tab_min(data
->tab
, data
->v
->el
, ctx
->one
, &data
->g
, NULL
, 0);
4421 if (res
== isl_lp_error
)
4422 return isl_bool_error
;
4423 return res
== isl_lp_ok
&& isl_int_is_nonneg(data
->g
);
4426 /* Given a lower and an upper bound on div i, do they always allow
4427 * for an integer value of the given div?
4428 * Determine this property by constructing an inequality
4429 * such that the property is guaranteed when the inequality is nonnegative.
4430 * The lower bound is inequality l, while the upper bound is inequality u.
4431 * The constructed inequality is stored in data->v.
4433 * Let the upper bound be
4437 * and the lower bound
4441 * Let n_u = f_u g and n_l = f_l g, with g = gcd(n_u, n_l).
4444 * - f_u e_l <= f_u f_l g a <= f_l e_u
4446 * Since all variables are integer valued, this is equivalent to
4448 * - f_u e_l - (f_u - 1) <= f_u f_l g a <= f_l e_u + (f_l - 1)
4450 * If this interval is at least f_u f_l g, then it contains at least
4451 * one integer value for a.
4452 * That is, the test constraint is
4454 * f_l e_u + f_u e_l + f_l - 1 + f_u - 1 + 1 >= f_u f_l g
4458 * f_l e_u + f_u e_l + f_l - 1 + f_u - 1 + 1 - f_u f_l g >= 0
4460 * If the coefficients of f_l e_u + f_u e_l have a common divisor g',
4461 * then the constraint can be scaled down by a factor g',
4462 * with the constant term replaced by
4463 * floor((f_l e_{u,0} + f_u e_{l,0} + f_l - 1 + f_u - 1 + 1 - f_u f_l g)/g').
4464 * Note that the result of applying Fourier-Motzkin to this pair
4467 * f_l e_u + f_u e_l >= 0
4469 * If the constant term of the scaled down version of this constraint,
4470 * i.e., floor((f_l e_{u,0} + f_u e_{l,0})/g') is equal to the constant
4471 * term of the scaled down test constraint, then the test constraint
4472 * is known to hold and no explicit evaluation is required.
4473 * This is essentially the Omega test.
4475 * If the test constraint consists of only a constant term, then
4476 * it is sufficient to look at the sign of this constant term.
4478 static isl_bool
int_between_bounds(__isl_keep isl_basic_map
*bmap
, int i
,
4479 int l
, int u
, struct test_ineq_data
*data
)
4484 offset
= isl_basic_map_offset(bmap
, isl_dim_div
);
4485 n_div
= isl_basic_map_dim(bmap
, isl_dim_div
);
4487 return isl_bool_error
;
4489 isl_int_gcd(data
->g
,
4490 bmap
->ineq
[l
][offset
+ i
], bmap
->ineq
[u
][offset
+ i
]);
4491 isl_int_divexact(data
->fl
, bmap
->ineq
[l
][offset
+ i
], data
->g
);
4492 isl_int_divexact(data
->fu
, bmap
->ineq
[u
][offset
+ i
], data
->g
);
4493 isl_int_neg(data
->fu
, data
->fu
);
4494 isl_seq_combine(data
->v
->el
, data
->fl
, bmap
->ineq
[u
],
4495 data
->fu
, bmap
->ineq
[l
], offset
+ n_div
);
4496 isl_int_mul(data
->g
, data
->g
, data
->fl
);
4497 isl_int_mul(data
->g
, data
->g
, data
->fu
);
4498 isl_int_sub(data
->g
, data
->g
, data
->fl
);
4499 isl_int_sub(data
->g
, data
->g
, data
->fu
);
4500 isl_int_add_ui(data
->g
, data
->g
, 1);
4501 isl_int_sub(data
->fl
, data
->v
->el
[0], data
->g
);
4503 isl_seq_gcd(data
->v
->el
+ 1, offset
- 1 + n_div
, &data
->g
);
4504 if (isl_int_is_zero(data
->g
))
4505 return isl_int_is_nonneg(data
->fl
);
4506 if (isl_int_is_one(data
->g
)) {
4507 isl_int_set(data
->v
->el
[0], data
->fl
);
4508 return test_ineq_is_satisfied(bmap
, data
);
4510 isl_int_fdiv_q(data
->fl
, data
->fl
, data
->g
);
4511 isl_int_fdiv_q(data
->v
->el
[0], data
->v
->el
[0], data
->g
);
4512 if (isl_int_eq(data
->fl
, data
->v
->el
[0]))
4513 return isl_bool_true
;
4514 isl_int_set(data
->v
->el
[0], data
->fl
);
4515 isl_seq_scale_down(data
->v
->el
+ 1, data
->v
->el
+ 1, data
->g
,
4516 offset
- 1 + n_div
);
4518 return test_ineq_is_satisfied(bmap
, data
);
4521 /* Remove more kinds of divs that are not strictly needed.
4522 * In particular, if all pairs of lower and upper bounds on a div
4523 * are such that they allow at least one integer value of the div,
4524 * then we can eliminate the div using Fourier-Motzkin without
4525 * introducing any spurious solutions.
4527 * If at least one of the two constraints has a unit coefficient for the div,
4528 * then the presence of such a value is guaranteed so there is no need to check.
4529 * In particular, the value attained by the bound with unit coefficient
4530 * can serve as this intermediate value.
4532 static __isl_give isl_basic_map
*drop_more_redundant_divs(
4533 __isl_take isl_basic_map
*bmap
, __isl_take
int *pairs
, int n
)
4536 struct test_ineq_data data
= { NULL
, NULL
};
4541 isl_int_init(data
.g
);
4542 isl_int_init(data
.fl
);
4543 isl_int_init(data
.fu
);
4545 n_div
= isl_basic_map_dim(bmap
, isl_dim_div
);
4549 ctx
= isl_basic_map_get_ctx(bmap
);
4550 off
= isl_basic_map_offset(bmap
, isl_dim_div
);
4551 data
.v
= isl_vec_alloc(ctx
, off
+ n_div
);
4560 for (i
= 0; i
< n_div
; ++i
) {
4563 if (best
>= 0 && pairs
[best
] <= pairs
[i
])
4569 for (l
= 0; l
< bmap
->n_ineq
; ++l
) {
4570 if (!isl_int_is_pos(bmap
->ineq
[l
][off
+ i
]))
4572 if (isl_int_is_one(bmap
->ineq
[l
][off
+ i
]))
4574 for (u
= 0; u
< bmap
->n_ineq
; ++u
) {
4575 if (!isl_int_is_neg(bmap
->ineq
[u
][off
+ i
]))
4577 if (isl_int_is_negone(bmap
->ineq
[u
][off
+ i
]))
4579 has_int
= int_between_bounds(bmap
, i
, l
, u
,
4583 if (data
.tab
&& data
.tab
->empty
)
4588 if (u
< bmap
->n_ineq
)
4591 if (data
.tab
&& data
.tab
->empty
) {
4592 bmap
= isl_basic_map_set_to_empty(bmap
);
4595 if (l
== bmap
->n_ineq
) {
4603 test_ineq_data_clear(&data
);
4610 bmap
= isl_basic_map_remove_dims(bmap
, isl_dim_div
, remove
, 1);
4611 return isl_basic_map_drop_redundant_divs(bmap
);
4614 isl_basic_map_free(bmap
);
4615 test_ineq_data_clear(&data
);
4619 /* Given a pair of divs div1 and div2 such that, except for the lower bound l
4620 * and the upper bound u, div1 always occurs together with div2 in the form
4621 * (div1 + m div2), where m is the constant range on the variable div1
4622 * allowed by l and u, replace the pair div1 and div2 by a single
4623 * div that is equal to div1 + m div2.
4625 * The new div will appear in the location that contains div2.
4626 * We need to modify all constraints that contain
4627 * div2 = (div - div1) / m
4628 * The coefficient of div2 is known to be equal to 1 or -1.
4629 * (If a constraint does not contain div2, it will also not contain div1.)
4630 * If the constraint also contains div1, then we know they appear
4631 * as f (div1 + m div2) and we can simply replace (div1 + m div2) by div,
4632 * i.e., the coefficient of div is f.
4634 * Otherwise, we first need to introduce div1 into the constraint.
4643 * A lower bound on div2
4647 * can be replaced by
4649 * m div2 + div1 + m t + f >= 0
4655 * can be replaced by
4657 * -(m div2 + div1) + m t + f' >= 0
4659 * These constraint are those that we would obtain from eliminating
4660 * div1 using Fourier-Motzkin.
4662 * After all constraints have been modified, we drop the lower and upper
4663 * bound and then drop div1.
4664 * Since the new div is only placed in the same location that used
4665 * to store div2, but otherwise has a different meaning, any possible
4666 * explicit representation of the original div2 is removed.
4668 static __isl_give isl_basic_map
*coalesce_divs(__isl_take isl_basic_map
*bmap
,
4669 unsigned div1
, unsigned div2
, unsigned l
, unsigned u
)
4677 ctx
= isl_basic_map_get_ctx(bmap
);
4679 v_div
= isl_basic_map_var_offset(bmap
, isl_dim_div
);
4681 return isl_basic_map_free(bmap
);
4682 total
= 1 + v_div
+ bmap
->n_div
;
4685 isl_int_add(m
, bmap
->ineq
[l
][0], bmap
->ineq
[u
][0]);
4686 isl_int_add_ui(m
, m
, 1);
4688 for (i
= 0; i
< bmap
->n_ineq
; ++i
) {
4689 if (i
== l
|| i
== u
)
4691 if (isl_int_is_zero(bmap
->ineq
[i
][1 + v_div
+ div2
]))
4693 if (isl_int_is_zero(bmap
->ineq
[i
][1 + v_div
+ div1
])) {
4694 if (isl_int_is_pos(bmap
->ineq
[i
][1 + v_div
+ div2
]))
4695 isl_seq_combine(bmap
->ineq
[i
], m
, bmap
->ineq
[i
],
4696 ctx
->one
, bmap
->ineq
[l
], total
);
4698 isl_seq_combine(bmap
->ineq
[i
], m
, bmap
->ineq
[i
],
4699 ctx
->one
, bmap
->ineq
[u
], total
);
4701 isl_int_set(bmap
->ineq
[i
][1 + v_div
+ div2
],
4702 bmap
->ineq
[i
][1 + v_div
+ div1
]);
4703 isl_int_set_si(bmap
->ineq
[i
][1 + v_div
+ div1
], 0);
4708 isl_basic_map_drop_inequality(bmap
, l
);
4709 isl_basic_map_drop_inequality(bmap
, u
);
4711 isl_basic_map_drop_inequality(bmap
, u
);
4712 isl_basic_map_drop_inequality(bmap
, l
);
4714 bmap
= isl_basic_map_mark_div_unknown(bmap
, div2
);
4715 bmap
= isl_basic_map_drop_div(bmap
, div1
);
4719 /* First check if we can coalesce any pair of divs and
4720 * then continue with dropping more redundant divs.
4722 * We loop over all pairs of lower and upper bounds on a div
4723 * with coefficient 1 and -1, respectively, check if there
4724 * is any other div "c" with which we can coalesce the div
4725 * and if so, perform the coalescing.
4727 static __isl_give isl_basic_map
*coalesce_or_drop_more_redundant_divs(
4728 __isl_take isl_basic_map
*bmap
, int *pairs
, int n
)
4734 v_div
= isl_basic_map_var_offset(bmap
, isl_dim_div
);
4735 n_div
= isl_basic_map_dim(bmap
, isl_dim_div
);
4736 if (v_div
< 0 || n_div
< 0)
4737 return isl_basic_map_free(bmap
);
4739 for (i
= 0; i
< n_div
; ++i
) {
4742 for (l
= 0; l
< bmap
->n_ineq
; ++l
) {
4743 if (!isl_int_is_one(bmap
->ineq
[l
][1 + v_div
+ i
]))
4745 for (u
= 0; u
< bmap
->n_ineq
; ++u
) {
4748 if (!isl_int_is_negone(bmap
->ineq
[u
][1+v_div
+i
]))
4750 c
= div_find_coalesce(bmap
, pairs
, i
, l
, u
);
4756 bmap
= coalesce_divs(bmap
, i
, c
, l
, u
);
4757 return isl_basic_map_drop_redundant_divs(bmap
);
4762 if (ISL_F_ISSET(bmap
, ISL_BASIC_MAP_EMPTY
)) {
4767 return drop_more_redundant_divs(bmap
, pairs
, n
);
4770 isl_basic_map_free(bmap
);
4774 /* Are the "n" coefficients starting at "first" of inequality constraints
4775 * "i" and "j" of "bmap" equal to each other?
4777 static int is_parallel_part(__isl_keep isl_basic_map
*bmap
, int i
, int j
,
4780 return isl_seq_eq(bmap
->ineq
[i
] + first
, bmap
->ineq
[j
] + first
, n
);
4783 /* Are inequality constraints "i" and "j" of "bmap" equal to each other,
4784 * apart from the constant term and the coefficient at position "pos"?
4786 static isl_bool
is_parallel_except(__isl_keep isl_basic_map
*bmap
, int i
, int j
,
4791 total
= isl_basic_map_dim(bmap
, isl_dim_all
);
4793 return isl_bool_error
;
4794 return is_parallel_part(bmap
, i
, j
, 1, pos
- 1) &&
4795 is_parallel_part(bmap
, i
, j
, pos
+ 1, total
- pos
);
4798 /* Are inequality constraints "i" and "j" of "bmap" opposite to each other,
4799 * apart from the constant term and the coefficient at position "pos"?
4801 static isl_bool
is_opposite_except(__isl_keep isl_basic_map
*bmap
, int i
, int j
,
4806 total
= isl_basic_map_dim(bmap
, isl_dim_all
);
4808 return isl_bool_error
;
4809 return is_opposite_part(bmap
, i
, j
, 1, pos
- 1) &&
4810 is_opposite_part(bmap
, i
, j
, pos
+ 1, total
- pos
);
4813 /* Restart isl_basic_map_drop_redundant_divs after "bmap" has
4814 * been modified, simplying it if "simplify" is set.
4815 * Free the temporary data structure "pairs" that was associated
4816 * to the old version of "bmap".
4818 static __isl_give isl_basic_map
*drop_redundant_divs_again(
4819 __isl_take isl_basic_map
*bmap
, __isl_take
int *pairs
, int simplify
)
4822 bmap
= isl_basic_map_simplify(bmap
);
4824 return isl_basic_map_drop_redundant_divs(bmap
);
4827 /* Is "div" the single unknown existentially quantified variable
4828 * in inequality constraint "ineq" of "bmap"?
4829 * "div" is known to have a non-zero coefficient in "ineq".
4831 static isl_bool
single_unknown(__isl_keep isl_basic_map
*bmap
, int ineq
,
4839 known
= isl_basic_map_div_is_known(bmap
, div
);
4840 if (known
< 0 || known
)
4841 return isl_bool_not(known
);
4842 n_div
= isl_basic_map_dim(bmap
, isl_dim_div
);
4844 return isl_bool_error
;
4846 return isl_bool_true
;
4847 o_div
= isl_basic_map_offset(bmap
, isl_dim_div
);
4848 for (i
= 0; i
< n_div
; ++i
) {
4853 if (isl_int_is_zero(bmap
->ineq
[ineq
][o_div
+ i
]))
4855 known
= isl_basic_map_div_is_known(bmap
, i
);
4856 if (known
< 0 || !known
)
4860 return isl_bool_true
;
4863 /* Does integer division "div" have coefficient 1 in inequality constraint
4866 static isl_bool
has_coef_one(__isl_keep isl_basic_map
*bmap
, int div
, int ineq
)
4870 o_div
= isl_basic_map_offset(bmap
, isl_dim_div
);
4871 if (isl_int_is_one(bmap
->ineq
[ineq
][o_div
+ div
]))
4872 return isl_bool_true
;
4874 return isl_bool_false
;
4877 /* Turn inequality constraint "ineq" of "bmap" into an equality and
4878 * then try and drop redundant divs again,
4879 * freeing the temporary data structure "pairs" that was associated
4880 * to the old version of "bmap".
4882 static __isl_give isl_basic_map
*set_eq_and_try_again(
4883 __isl_take isl_basic_map
*bmap
, int ineq
, __isl_take
int *pairs
)
4885 bmap
= isl_basic_map_cow(bmap
);
4886 isl_basic_map_inequality_to_equality(bmap
, ineq
);
4887 return drop_redundant_divs_again(bmap
, pairs
, 1);
4890 /* Drop the integer division at position "div", along with the two
4891 * inequality constraints "ineq1" and "ineq2" in which it appears
4892 * from "bmap" and then try and drop redundant divs again,
4893 * freeing the temporary data structure "pairs" that was associated
4894 * to the old version of "bmap".
4896 static __isl_give isl_basic_map
*drop_div_and_try_again(
4897 __isl_take isl_basic_map
*bmap
, int div
, int ineq1
, int ineq2
,
4898 __isl_take
int *pairs
)
4900 if (ineq1
> ineq2
) {
4901 isl_basic_map_drop_inequality(bmap
, ineq1
);
4902 isl_basic_map_drop_inequality(bmap
, ineq2
);
4904 isl_basic_map_drop_inequality(bmap
, ineq2
);
4905 isl_basic_map_drop_inequality(bmap
, ineq1
);
4907 bmap
= isl_basic_map_drop_div(bmap
, div
);
4908 return drop_redundant_divs_again(bmap
, pairs
, 0);
4911 /* Given two inequality constraints
4913 * f(x) + n d + c >= 0, (ineq)
4915 * with d the variable at position "pos", and
4917 * f(x) + c0 >= 0, (lower)
4919 * compute the maximal value of the lower bound ceil((-f(x) - c)/n)
4920 * determined by the first constraint.
4927 static void lower_bound_from_parallel(__isl_keep isl_basic_map
*bmap
,
4928 int ineq
, int lower
, int pos
, isl_int
*l
)
4930 isl_int_neg(*l
, bmap
->ineq
[ineq
][0]);
4931 isl_int_add(*l
, *l
, bmap
->ineq
[lower
][0]);
4932 isl_int_cdiv_q(*l
, *l
, bmap
->ineq
[ineq
][pos
]);
4935 /* Given two inequality constraints
4937 * f(x) + n d + c >= 0, (ineq)
4939 * with d the variable at position "pos", and
4941 * -f(x) - c0 >= 0, (upper)
4943 * compute the minimal value of the lower bound ceil((-f(x) - c)/n)
4944 * determined by the first constraint.
4951 static void lower_bound_from_opposite(__isl_keep isl_basic_map
*bmap
,
4952 int ineq
, int upper
, int pos
, isl_int
*u
)
4954 isl_int_neg(*u
, bmap
->ineq
[ineq
][0]);
4955 isl_int_sub(*u
, *u
, bmap
->ineq
[upper
][0]);
4956 isl_int_cdiv_q(*u
, *u
, bmap
->ineq
[ineq
][pos
]);
4959 /* Given a lower bound constraint "ineq" on "div" in "bmap",
4960 * does the corresponding lower bound have a fixed value in "bmap"?
4962 * In particular, "ineq" is of the form
4964 * f(x) + n d + c >= 0
4966 * with n > 0, c the constant term and
4967 * d the existentially quantified variable "div".
4968 * That is, the lower bound is
4970 * ceil((-f(x) - c)/n)
4972 * Look for a pair of constraints
4977 * i.e., -c1 <= -f(x) <= c0, that fix ceil((-f(x) - c)/n) to a constant value.
4978 * That is, check that
4980 * ceil((-c1 - c)/n) = ceil((c0 - c)/n)
4982 * If so, return the index of inequality f(x) + c0 >= 0.
4983 * Otherwise, return bmap->n_ineq.
4984 * Return -1 on error.
4986 static int lower_bound_is_cst(__isl_keep isl_basic_map
*bmap
, int div
, int ineq
)
4989 int lower
= -1, upper
= -1;
4994 o_div
= isl_basic_map_offset(bmap
, isl_dim_div
);
4995 for (i
= 0; i
< bmap
->n_ineq
&& (lower
< 0 || upper
< 0); ++i
) {
5000 if (!isl_int_is_zero(bmap
->ineq
[i
][o_div
+ div
]))
5002 par
= isl_bool_false
;
5004 par
= is_parallel_except(bmap
, ineq
, i
, o_div
+ div
);
5011 opp
= isl_bool_false
;
5013 opp
= is_opposite_except(bmap
, ineq
, i
, o_div
+ div
);
5020 if (lower
< 0 || upper
< 0)
5021 return bmap
->n_ineq
;
5026 lower_bound_from_parallel(bmap
, ineq
, lower
, o_div
+ div
, &l
);
5027 lower_bound_from_opposite(bmap
, ineq
, upper
, o_div
+ div
, &u
);
5029 equal
= isl_int_eq(l
, u
);
5034 return equal
? lower
: bmap
->n_ineq
;
5037 /* Given a lower bound constraint "ineq" on the existentially quantified
5038 * variable "div", such that the corresponding lower bound has
5039 * a fixed value in "bmap", assign this fixed value to the variable and
5040 * then try and drop redundant divs again,
5041 * freeing the temporary data structure "pairs" that was associated
5042 * to the old version of "bmap".
5043 * "lower" determines the constant value for the lower bound.
5045 * In particular, "ineq" is of the form
5047 * f(x) + n d + c >= 0,
5049 * while "lower" is of the form
5053 * The lower bound is ceil((-f(x) - c)/n) and its constant value
5054 * is ceil((c0 - c)/n).
5056 static __isl_give isl_basic_map
*fix_cst_lower(__isl_take isl_basic_map
*bmap
,
5057 int div
, int ineq
, int lower
, int *pairs
)
5064 o_div
= isl_basic_map_offset(bmap
, isl_dim_div
);
5065 lower_bound_from_parallel(bmap
, ineq
, lower
, o_div
+ div
, &c
);
5066 bmap
= isl_basic_map_fix(bmap
, isl_dim_div
, div
, c
);
5071 return isl_basic_map_drop_redundant_divs(bmap
);
5074 /* Do any of the integer divisions of "bmap" involve integer division "div"?
5076 * The integer division "div" could only ever appear in any later
5077 * integer division (with an explicit representation).
5079 static isl_bool
any_div_involves_div(__isl_keep isl_basic_map
*bmap
, int div
)
5082 isl_size v_div
, n_div
;
5084 v_div
= isl_basic_map_var_offset(bmap
, isl_dim_div
);
5085 n_div
= isl_basic_map_dim(bmap
, isl_dim_div
);
5086 if (v_div
< 0 || n_div
< 0)
5087 return isl_bool_error
;
5089 for (i
= div
+ 1; i
< n_div
; ++i
) {
5092 involves
= isl_basic_map_div_expr_involves_vars(bmap
, i
,
5094 if (involves
< 0 || involves
)
5098 return isl_bool_false
;
5101 /* Remove divs that are not strictly needed based on the inequality
5103 * In particular, if a div only occurs positively (or negatively)
5104 * in constraints, then it can simply be dropped.
5105 * Also, if a div occurs in only two constraints and if moreover
5106 * those two constraints are opposite to each other, except for the constant
5107 * term and if the sum of the constant terms is such that for any value
5108 * of the other values, there is always at least one integer value of the
5109 * div, i.e., if one plus this sum is greater than or equal to
5110 * the (absolute value) of the coefficient of the div in the constraints,
5111 * then we can also simply drop the div.
5113 * If an existentially quantified variable does not have an explicit
5114 * representation, appears in only a single lower bound that does not
5115 * involve any other such existentially quantified variables and appears
5116 * in this lower bound with coefficient 1,
5117 * then fix the variable to the value of the lower bound. That is,
5118 * turn the inequality into an equality.
5119 * If for any value of the other variables, there is any value
5120 * for the existentially quantified variable satisfying the constraints,
5121 * then this lower bound also satisfies the constraints.
5122 * It is therefore safe to pick this lower bound.
5124 * The same reasoning holds even if the coefficient is not one.
5125 * However, fixing the variable to the value of the lower bound may
5126 * in general introduce an extra integer division, in which case
5127 * it may be better to pick another value.
5128 * If this integer division has a known constant value, then plugging
5129 * in this constant value removes the existentially quantified variable
5130 * completely. In particular, if the lower bound is of the form
5131 * ceil((-f(x) - c)/n) and there are two constraints, f(x) + c0 >= 0 and
5132 * -f(x) + c1 >= 0 such that ceil((-c1 - c)/n) = ceil((c0 - c)/n),
5133 * then the existentially quantified variable can be assigned this
5136 * We skip divs that appear in equalities or in the definition of other divs.
5137 * Divs that appear in the definition of other divs usually occur in at least
5138 * 4 constraints, but the constraints may have been simplified.
5140 * If any divs are left after these simple checks then we move on
5141 * to more complicated cases in drop_more_redundant_divs.
5143 static __isl_give isl_basic_map
*isl_basic_map_drop_redundant_divs_ineq(
5144 __isl_take isl_basic_map
*bmap
)
5154 if (bmap
->n_div
== 0)
5157 off
= isl_basic_map_var_offset(bmap
, isl_dim_div
);
5159 return isl_basic_map_free(bmap
);
5160 pairs
= isl_calloc_array(bmap
->ctx
, int, bmap
->n_div
);
5164 n_ineq
= isl_basic_map_n_inequality(bmap
);
5167 for (i
= 0; i
< bmap
->n_div
; ++i
) {
5169 int last_pos
, last_neg
;
5172 isl_bool involves
, opp
, set_div
;
5174 defined
= !isl_int_is_zero(bmap
->div
[i
][0]);
5175 involves
= any_div_involves_div(bmap
, i
);
5180 for (j
= 0; j
< bmap
->n_eq
; ++j
)
5181 if (!isl_int_is_zero(bmap
->eq
[j
][1 + off
+ i
]))
5187 for (j
= 0; j
< bmap
->n_ineq
; ++j
) {
5188 if (isl_int_is_pos(bmap
->ineq
[j
][1 + off
+ i
])) {
5192 if (isl_int_is_neg(bmap
->ineq
[j
][1 + off
+ i
])) {
5197 pairs
[i
] = pos
* neg
;
5198 if (pairs
[i
] == 0) {
5199 for (j
= bmap
->n_ineq
- 1; j
>= 0; --j
)
5200 if (!isl_int_is_zero(bmap
->ineq
[j
][1+off
+i
]))
5201 isl_basic_map_drop_inequality(bmap
, j
);
5202 bmap
= isl_basic_map_drop_div(bmap
, i
);
5203 return drop_redundant_divs_again(bmap
, pairs
, 0);
5206 opp
= isl_bool_false
;
5208 opp
= is_opposite(bmap
, last_pos
, last_neg
);
5213 isl_bool single
, one
;
5217 single
= single_unknown(bmap
, last_pos
, i
);
5222 one
= has_coef_one(bmap
, i
, last_pos
);
5226 return set_eq_and_try_again(bmap
, last_pos
,
5228 lower
= lower_bound_is_cst(bmap
, i
, last_pos
);
5232 return fix_cst_lower(bmap
, i
, last_pos
, lower
,
5237 isl_int_add(bmap
->ineq
[last_pos
][0],
5238 bmap
->ineq
[last_pos
][0], bmap
->ineq
[last_neg
][0]);
5239 isl_int_add_ui(bmap
->ineq
[last_pos
][0],
5240 bmap
->ineq
[last_pos
][0], 1);
5241 redundant
= isl_int_ge(bmap
->ineq
[last_pos
][0],
5242 bmap
->ineq
[last_pos
][1+off
+i
]);
5243 isl_int_sub_ui(bmap
->ineq
[last_pos
][0],
5244 bmap
->ineq
[last_pos
][0], 1);
5245 isl_int_sub(bmap
->ineq
[last_pos
][0],
5246 bmap
->ineq
[last_pos
][0], bmap
->ineq
[last_neg
][0]);
5248 return drop_div_and_try_again(bmap
, i
,
5249 last_pos
, last_neg
, pairs
);
5251 set_div
= isl_bool_false
;
5253 set_div
= ok_to_set_div_from_bound(bmap
, i
, last_pos
);
5255 return isl_basic_map_free(bmap
);
5257 bmap
= set_div_from_lower_bound(bmap
, i
, last_pos
);
5258 return drop_redundant_divs_again(bmap
, pairs
, 1);
5265 return coalesce_or_drop_more_redundant_divs(bmap
, pairs
, n
);
5271 isl_basic_map_free(bmap
);
5275 /* Consider the coefficients at "c" as a row vector and replace
5276 * them with their product with "T". "T" is assumed to be a square matrix.
5278 static isl_stat
preimage(isl_int
*c
, __isl_keep isl_mat
*T
)
5284 n
= isl_mat_rows(T
);
5286 return isl_stat_error
;
5287 if (isl_seq_first_non_zero(c
, n
) == -1)
5289 ctx
= isl_mat_get_ctx(T
);
5290 v
= isl_vec_alloc(ctx
, n
);
5292 return isl_stat_error
;
5293 isl_seq_swp_or_cpy(v
->el
, c
, n
);
5294 v
= isl_vec_mat_product(v
, isl_mat_copy(T
));
5296 return isl_stat_error
;
5297 isl_seq_swp_or_cpy(c
, v
->el
, n
);
5303 /* Plug in T for the variables in "bmap" starting at "pos".
5304 * T is a linear unimodular matrix, i.e., without constant term.
5306 static __isl_give isl_basic_map
*isl_basic_map_preimage_vars(
5307 __isl_take isl_basic_map
*bmap
, unsigned pos
, __isl_take isl_mat
*T
)
5310 isl_size n_row
, n_col
;
5312 bmap
= isl_basic_map_cow(bmap
);
5313 n_row
= isl_mat_rows(T
);
5314 n_col
= isl_mat_cols(T
);
5315 if (!bmap
|| n_row
< 0 || n_col
< 0)
5319 isl_die(isl_mat_get_ctx(T
), isl_error_invalid
,
5320 "expecting square matrix", goto error
);
5322 if (isl_basic_map_check_range(bmap
, isl_dim_all
, pos
, n_col
) < 0)
5325 for (i
= 0; i
< bmap
->n_eq
; ++i
)
5326 if (preimage(bmap
->eq
[i
] + 1 + pos
, T
) < 0)
5328 for (i
= 0; i
< bmap
->n_ineq
; ++i
)
5329 if (preimage(bmap
->ineq
[i
] + 1 + pos
, T
) < 0)
5331 for (i
= 0; i
< bmap
->n_div
; ++i
) {
5332 if (isl_basic_map_div_is_marked_unknown(bmap
, i
))
5334 if (preimage(bmap
->div
[i
] + 1 + 1 + pos
, T
) < 0)
5341 isl_basic_map_free(bmap
);
5346 /* Remove divs that are not strictly needed.
5348 * First look for an equality constraint involving two or more
5349 * existentially quantified variables without an explicit
5350 * representation. Replace the combination that appears
5351 * in the equality constraint by a single existentially quantified
5352 * variable such that the equality can be used to derive
5353 * an explicit representation for the variable.
5354 * If there are no more such equality constraints, then continue
5355 * with isl_basic_map_drop_redundant_divs_ineq.
5357 * In particular, if the equality constraint is of the form
5359 * f(x) + \sum_i c_i a_i = 0
5361 * with a_i existentially quantified variable without explicit
5362 * representation, then apply a transformation on the existentially
5363 * quantified variables to turn the constraint into
5367 * with g the gcd of the c_i.
5368 * In order to easily identify which existentially quantified variables
5369 * have a complete explicit representation, i.e., without being defined
5370 * in terms of other existentially quantified variables without
5371 * an explicit representation, the existentially quantified variables
5374 * The variable transformation is computed by extending the row
5375 * [c_1/g ... c_n/g] to a unimodular matrix, obtaining the transformation
5377 * [a_1'] [c_1/g ... c_n/g] [ a_1 ]
5382 * with [c_1/g ... c_n/g] representing the first row of U.
5383 * The inverse of U is then plugged into the original constraints.
5384 * The call to isl_basic_map_simplify makes sure the explicit
5385 * representation for a_1' is extracted from the equality constraint.
5387 __isl_give isl_basic_map
*isl_basic_map_drop_redundant_divs(
5388 __isl_take isl_basic_map
*bmap
)
5400 if (isl_basic_map_divs_known(bmap
))
5401 return isl_basic_map_drop_redundant_divs_ineq(bmap
);
5402 if (bmap
->n_eq
== 0)
5403 return isl_basic_map_drop_redundant_divs_ineq(bmap
);
5404 bmap
= isl_basic_map_sort_divs(bmap
);
5408 first
= isl_basic_map_first_unknown_div(bmap
);
5410 return isl_basic_map_free(bmap
);
5412 o_div
= isl_basic_map_offset(bmap
, isl_dim_div
);
5413 n_div
= isl_basic_map_dim(bmap
, isl_dim_div
);
5415 return isl_basic_map_free(bmap
);
5417 for (i
= 0; i
< bmap
->n_eq
; ++i
) {
5418 l
= isl_seq_first_non_zero(bmap
->eq
[i
] + o_div
+ first
,
5423 if (isl_seq_first_non_zero(bmap
->eq
[i
] + o_div
+ l
+ 1,
5424 n_div
- (l
+ 1)) == -1)
5428 if (i
>= bmap
->n_eq
)
5429 return isl_basic_map_drop_redundant_divs_ineq(bmap
);
5431 ctx
= isl_basic_map_get_ctx(bmap
);
5432 T
= isl_mat_alloc(ctx
, n_div
- l
, n_div
- l
);
5434 return isl_basic_map_free(bmap
);
5435 isl_seq_cpy(T
->row
[0], bmap
->eq
[i
] + o_div
+ l
, n_div
- l
);
5436 T
= isl_mat_normalize_row(T
, 0);
5437 T
= isl_mat_unimodular_complete(T
, 1);
5438 T
= isl_mat_right_inverse(T
);
5440 for (i
= l
; i
< n_div
; ++i
)
5441 bmap
= isl_basic_map_mark_div_unknown(bmap
, i
);
5442 bmap
= isl_basic_map_preimage_vars(bmap
, o_div
- 1 + l
, T
);
5443 bmap
= isl_basic_map_simplify(bmap
);
5445 return isl_basic_map_drop_redundant_divs(bmap
);
5448 /* Does "bmap" satisfy any equality that involves more than 2 variables
5449 * and/or has coefficients different from -1 and 1?
5451 static isl_bool
has_multiple_var_equality(__isl_keep isl_basic_map
*bmap
)
5456 total
= isl_basic_map_dim(bmap
, isl_dim_all
);
5458 return isl_bool_error
;
5460 for (i
= 0; i
< bmap
->n_eq
; ++i
) {
5463 j
= isl_seq_first_non_zero(bmap
->eq
[i
] + 1, total
);
5466 if (!isl_int_is_one(bmap
->eq
[i
][1 + j
]) &&
5467 !isl_int_is_negone(bmap
->eq
[i
][1 + j
]))
5468 return isl_bool_true
;
5471 k
= isl_seq_first_non_zero(bmap
->eq
[i
] + 1 + j
, total
- j
);
5475 if (!isl_int_is_one(bmap
->eq
[i
][1 + j
]) &&
5476 !isl_int_is_negone(bmap
->eq
[i
][1 + j
]))
5477 return isl_bool_true
;
5480 k
= isl_seq_first_non_zero(bmap
->eq
[i
] + 1 + j
, total
- j
);
5482 return isl_bool_true
;
5485 return isl_bool_false
;
5488 /* Remove any common factor g from the constraint coefficients in "v".
5489 * The constant term is stored in the first position and is replaced
5490 * by floor(c/g). If any common factor is removed and if this results
5491 * in a tightening of the constraint, then set *tightened.
5493 static __isl_give isl_vec
*normalize_constraint(__isl_take isl_vec
*v
,
5500 ctx
= isl_vec_get_ctx(v
);
5501 isl_seq_gcd(v
->el
+ 1, v
->size
- 1, &ctx
->normalize_gcd
);
5502 if (isl_int_is_zero(ctx
->normalize_gcd
))
5504 if (isl_int_is_one(ctx
->normalize_gcd
))
5509 if (tightened
&& !isl_int_is_divisible_by(v
->el
[0], ctx
->normalize_gcd
))
5511 isl_int_fdiv_q(v
->el
[0], v
->el
[0], ctx
->normalize_gcd
);
5512 isl_seq_scale_down(v
->el
+ 1, v
->el
+ 1, ctx
->normalize_gcd
,
5517 /* Internal representation used by isl_basic_map_reduce_coefficients.
5519 * "total" is the total dimensionality of the original basic map.
5520 * "v" is a temporary vector of size 1 + total that can be used
5521 * to store constraint coefficients.
5522 * "T" is the variable compression.
5523 * "T2" is the inverse transformation.
5524 * "tightened" is set if any constant term got tightened
5525 * while reducing the coefficients.
5527 struct isl_reduce_coefficients_data
{
5535 /* Free all memory allocated in "data".
5537 static void isl_reduce_coefficients_data_clear(
5538 struct isl_reduce_coefficients_data
*data
)
5540 data
->T
= isl_mat_free(data
->T
);
5541 data
->T2
= isl_mat_free(data
->T2
);
5542 data
->v
= isl_vec_free(data
->v
);
5545 /* Initialize "data" for "bmap", freeing all allocated memory
5546 * if anything goes wrong.
5548 * In particular, construct a variable compression
5549 * from the equality constraints of "bmap" and
5550 * allocate a temporary vector.
5552 static isl_stat
isl_reduce_coefficients_data_init(
5553 __isl_keep isl_basic_map
*bmap
,
5554 struct isl_reduce_coefficients_data
*data
)
5562 data
->tightened
= 0;
5564 data
->total
= isl_basic_map_dim(bmap
, isl_dim_all
);
5565 if (data
->total
< 0)
5566 return isl_stat_error
;
5567 ctx
= isl_basic_map_get_ctx(bmap
);
5568 data
->v
= isl_vec_alloc(ctx
, 1 + data
->total
);
5570 return isl_stat_error
;
5572 eq
= isl_mat_sub_alloc6(ctx
, bmap
->eq
, 0, bmap
->n_eq
,
5573 0, 1 + data
->total
);
5574 data
->T
= isl_mat_variable_compression(eq
, &data
->T2
);
5575 if (!data
->T
|| !data
->T2
)
5580 isl_reduce_coefficients_data_clear(data
);
5581 return isl_stat_error
;
5584 /* Reduce the coefficients of "bmap" by applying the variable compression
5586 * In particular, apply the variable compression to each constraint,
5587 * factor out any common factor in the non-constant coefficients and
5588 * then apply the inverse of the compression.
5590 * Only apply the reduction on a single copy of the basic map
5591 * since the reduction may leave the result in an inconsistent state.
5592 * In particular, the constraints may not be gaussed.
5594 static __isl_give isl_basic_map
*reduce_coefficients(
5595 __isl_take isl_basic_map
*bmap
,
5596 struct isl_reduce_coefficients_data
*data
)
5601 total
= isl_basic_map_dim(bmap
, isl_dim_all
);
5603 return isl_basic_map_free(bmap
);
5604 if (total
!= data
->total
)
5605 isl_die(isl_basic_map_get_ctx(bmap
), isl_error_internal
,
5606 "total dimensionality changed unexpectedly",
5607 return isl_basic_map_free(bmap
));
5609 bmap
= isl_basic_map_cow(bmap
);
5613 for (i
= 0; i
< bmap
->n_ineq
; ++i
) {
5614 isl_seq_cpy(data
->v
->el
, bmap
->ineq
[i
], 1 + data
->total
);
5615 data
->v
= isl_vec_mat_product(data
->v
, isl_mat_copy(data
->T
));
5616 data
->v
= normalize_constraint(data
->v
, &data
->tightened
);
5617 data
->v
= isl_vec_mat_product(data
->v
, isl_mat_copy(data
->T2
));
5619 return isl_basic_map_free(bmap
);
5620 isl_seq_cpy(bmap
->ineq
[i
], data
->v
->el
, 1 + data
->total
);
5623 ISL_F_SET(bmap
, ISL_BASIC_MAP_REDUCED_COEFFICIENTS
);
5628 /* If "bmap" is an integer set that satisfies any equality involving
5629 * more than 2 variables and/or has coefficients different from -1 and 1,
5630 * then use variable compression to reduce the coefficients by removing
5631 * any (hidden) common factor.
5632 * In particular, apply the variable compression to each constraint,
5633 * factor out any common factor in the non-constant coefficients and
5634 * then apply the inverse of the compression.
5635 * At the end, we mark the basic map as having reduced constants.
5636 * If this flag is still set on the next invocation of this function,
5637 * then we skip the computation.
5639 * Removing a common factor may result in a tightening of some of
5640 * the constraints. If this happens, then we may end up with two
5641 * opposite inequalities that can be replaced by an equality.
5642 * We therefore call isl_basic_map_detect_inequality_pairs,
5643 * which checks for such pairs of inequalities as well as eliminate_divs_eq
5644 * and isl_basic_map_gauss if such a pair was found.
5645 * This call to isl_basic_map_gauss may undo much of the effect
5646 * of the reduction on which isl_map_coalesce depends.
5647 * In particular, constraints in terms of (compressed) local variables
5648 * get reformulated in terms of the set variables again.
5649 * The reduction is therefore applied again afterwards.
5650 * This has to be done before the call to eliminate_divs_eq, however,
5651 * since that may remove some local variables, while
5652 * the data used during the reduction is formulated in terms
5653 * of the original variables.
5655 * Tightening may also result in some other constraints becoming
5656 * (rationally) redundant with respect to the tightened constraint
5657 * (in combination with other constraints). The basic map may
5658 * therefore no longer be assumed to have no redundant constraints.
5660 * Note that this function may leave the result in an inconsistent state.
5661 * In particular, the constraints may not be gaussed.
5662 * Unfortunately, isl_map_coalesce actually depends on this inconsistent state
5663 * for some of the test cases to pass successfully.
5664 * Any potential modification of the representation is therefore only
5665 * performed on a single copy of the basic map.
5667 __isl_give isl_basic_map
*isl_basic_map_reduce_coefficients(
5668 __isl_take isl_basic_map
*bmap
)
5670 struct isl_reduce_coefficients_data data
;
5675 if (ISL_F_ISSET(bmap
, ISL_BASIC_MAP_REDUCED_COEFFICIENTS
))
5677 if (isl_basic_map_is_rational(bmap
))
5679 if (bmap
->n_eq
== 0)
5681 multi
= has_multiple_var_equality(bmap
);
5683 return isl_basic_map_free(bmap
);
5687 if (isl_reduce_coefficients_data_init(bmap
, &data
) < 0)
5688 return isl_basic_map_free(bmap
);
5690 if (data
.T
->n_col
== 0) {
5691 isl_reduce_coefficients_data_clear(&data
);
5692 return isl_basic_map_set_to_empty(bmap
);
5695 bmap
= reduce_coefficients(bmap
, &data
);
5699 if (data
.tightened
) {
5702 ISL_F_CLR(bmap
, ISL_BASIC_MAP_NO_REDUNDANT
);
5703 bmap
= isl_basic_map_detect_inequality_pairs(bmap
, &progress
);
5705 bmap
= isl_basic_map_gauss(bmap
, NULL
);
5706 bmap
= reduce_coefficients(bmap
, &data
);
5707 bmap
= eliminate_divs_eq(bmap
, &progress
);
5711 isl_reduce_coefficients_data_clear(&data
);
5715 isl_reduce_coefficients_data_clear(&data
);
5716 return isl_basic_map_free(bmap
);
5719 /* Shift the integer division at position "div" of "bmap"
5720 * by "shift" times the variable at position "pos".
5721 * "pos" is as determined by isl_basic_map_offset, i.e., pos == 0
5722 * corresponds to the constant term.
5724 * That is, if the integer division has the form
5728 * then replace it by
5730 * floor((f(x) + shift * d * x_pos)/d) - shift * x_pos
5732 __isl_give isl_basic_map
*isl_basic_map_shift_div(
5733 __isl_take isl_basic_map
*bmap
, int div
, int pos
, isl_int shift
)
5736 isl_size total
, n_div
;
5738 if (isl_int_is_zero(shift
))
5740 total
= isl_basic_map_dim(bmap
, isl_dim_all
);
5741 n_div
= isl_basic_map_dim(bmap
, isl_dim_div
);
5743 if (total
< 0 || n_div
< 0)
5744 return isl_basic_map_free(bmap
);
5746 isl_int_addmul(bmap
->div
[div
][1 + pos
], shift
, bmap
->div
[div
][0]);
5748 for (i
= 0; i
< bmap
->n_eq
; ++i
) {
5749 if (isl_int_is_zero(bmap
->eq
[i
][1 + total
+ div
]))
5751 isl_int_submul(bmap
->eq
[i
][pos
],
5752 shift
, bmap
->eq
[i
][1 + total
+ div
]);
5754 for (i
= 0; i
< bmap
->n_ineq
; ++i
) {
5755 if (isl_int_is_zero(bmap
->ineq
[i
][1 + total
+ div
]))
5757 isl_int_submul(bmap
->ineq
[i
][pos
],
5758 shift
, bmap
->ineq
[i
][1 + total
+ div
]);
5760 for (i
= 0; i
< bmap
->n_div
; ++i
) {
5761 if (isl_int_is_zero(bmap
->div
[i
][0]))
5763 if (isl_int_is_zero(bmap
->div
[i
][1 + 1 + total
+ div
]))
5765 isl_int_submul(bmap
->div
[i
][1 + pos
],
5766 shift
, bmap
->div
[i
][1 + 1 + total
+ div
]);