2 * Copyright 2008-2009 Katholieke Universiteit Leuven
3 * Copyright 2012-2013 Ecole Normale Superieure
4 * Copyright 2014-2015 INRIA Rocquencourt
5 * Copyright 2016 Sven Verdoolaege
7 * Use of this software is governed by the MIT license
9 * Written by Sven Verdoolaege, K.U.Leuven, Departement
10 * Computerwetenschappen, Celestijnenlaan 200A, B-3001 Leuven, Belgium
11 * and Ecole Normale Superieure, 45 rue d’Ulm, 75230 Paris, France
12 * and Inria Paris - Rocquencourt, Domaine de Voluceau - Rocquencourt,
13 * B.P. 105 - 78153 Le Chesnay, France
16 #include <isl_ctx_private.h>
17 #include <isl_map_private.h>
18 #include "isl_equalities.h"
22 #include <isl_space_private.h>
23 #include <isl_mat_private.h>
24 #include <isl_vec_private.h>
26 #include <bset_to_bmap.c>
27 #include <bset_from_bmap.c>
28 #include <set_to_map.c>
30 static void swap_equality(struct isl_basic_map
*bmap
, int a
, int b
)
32 isl_int
*t
= bmap
->eq
[a
];
33 bmap
->eq
[a
] = bmap
->eq
[b
];
37 static void swap_inequality(struct isl_basic_map
*bmap
, int a
, int b
)
40 isl_int
*t
= bmap
->ineq
[a
];
41 bmap
->ineq
[a
] = bmap
->ineq
[b
];
46 static void constraint_drop_vars(isl_int
*c
, unsigned n
, unsigned rem
)
48 isl_seq_cpy(c
, c
+ n
, rem
);
49 isl_seq_clr(c
+ rem
, n
);
52 /* Drop n dimensions starting at first.
54 * In principle, this frees up some extra variables as the number
55 * of columns remains constant, but we would have to extend
56 * the div array too as the number of rows in this array is assumed
57 * to be equal to extra.
59 struct isl_basic_set
*isl_basic_set_drop_dims(
60 struct isl_basic_set
*bset
, unsigned first
, unsigned n
)
67 isl_assert(bset
->ctx
, first
+ n
<= bset
->dim
->n_out
, goto error
);
69 if (n
== 0 && !isl_space_get_tuple_name(bset
->dim
, isl_dim_set
))
72 bset
= isl_basic_set_cow(bset
);
76 for (i
= 0; i
< bset
->n_eq
; ++i
)
77 constraint_drop_vars(bset
->eq
[i
]+1+bset
->dim
->nparam
+first
, n
,
78 (bset
->dim
->n_out
-first
-n
)+bset
->extra
);
80 for (i
= 0; i
< bset
->n_ineq
; ++i
)
81 constraint_drop_vars(bset
->ineq
[i
]+1+bset
->dim
->nparam
+first
, n
,
82 (bset
->dim
->n_out
-first
-n
)+bset
->extra
);
84 for (i
= 0; i
< bset
->n_div
; ++i
)
85 constraint_drop_vars(bset
->div
[i
]+1+1+bset
->dim
->nparam
+first
, n
,
86 (bset
->dim
->n_out
-first
-n
)+bset
->extra
);
88 bset
->dim
= isl_space_drop_outputs(bset
->dim
, first
, n
);
92 ISL_F_CLR(bset
, ISL_BASIC_SET_NORMALIZED
);
93 bset
= isl_basic_set_simplify(bset
);
94 return isl_basic_set_finalize(bset
);
96 isl_basic_set_free(bset
);
100 struct isl_set
*isl_set_drop_dims(
101 struct isl_set
*set
, unsigned first
, unsigned n
)
108 isl_assert(set
->ctx
, first
+ n
<= set
->dim
->n_out
, goto error
);
110 if (n
== 0 && !isl_space_get_tuple_name(set
->dim
, isl_dim_set
))
112 set
= isl_set_cow(set
);
115 set
->dim
= isl_space_drop_outputs(set
->dim
, first
, n
);
119 for (i
= 0; i
< set
->n
; ++i
) {
120 set
->p
[i
] = isl_basic_set_drop_dims(set
->p
[i
], first
, n
);
125 ISL_F_CLR(set
, ISL_SET_NORMALIZED
);
132 /* Move "n" divs starting at "first" to the end of the list of divs.
134 static struct isl_basic_map
*move_divs_last(struct isl_basic_map
*bmap
,
135 unsigned first
, unsigned n
)
140 if (first
+ n
== bmap
->n_div
)
143 div
= isl_alloc_array(bmap
->ctx
, isl_int
*, n
);
146 for (i
= 0; i
< n
; ++i
)
147 div
[i
] = bmap
->div
[first
+ i
];
148 for (i
= 0; i
< bmap
->n_div
- first
- n
; ++i
)
149 bmap
->div
[first
+ i
] = bmap
->div
[first
+ n
+ i
];
150 for (i
= 0; i
< n
; ++i
)
151 bmap
->div
[bmap
->n_div
- n
+ i
] = div
[i
];
155 isl_basic_map_free(bmap
);
159 /* Drop "n" dimensions of type "type" starting at "first".
161 * In principle, this frees up some extra variables as the number
162 * of columns remains constant, but we would have to extend
163 * the div array too as the number of rows in this array is assumed
164 * to be equal to extra.
166 struct isl_basic_map
*isl_basic_map_drop(struct isl_basic_map
*bmap
,
167 enum isl_dim_type type
, unsigned first
, unsigned n
)
177 dim
= isl_basic_map_dim(bmap
, type
);
178 isl_assert(bmap
->ctx
, first
+ n
<= dim
, goto error
);
180 if (n
== 0 && !isl_space_is_named_or_nested(bmap
->dim
, type
))
183 bmap
= isl_basic_map_cow(bmap
);
187 offset
= isl_basic_map_offset(bmap
, type
) + first
;
188 left
= isl_basic_map_total_dim(bmap
) - (offset
- 1) - n
;
189 for (i
= 0; i
< bmap
->n_eq
; ++i
)
190 constraint_drop_vars(bmap
->eq
[i
]+offset
, n
, left
);
192 for (i
= 0; i
< bmap
->n_ineq
; ++i
)
193 constraint_drop_vars(bmap
->ineq
[i
]+offset
, n
, left
);
195 for (i
= 0; i
< bmap
->n_div
; ++i
)
196 constraint_drop_vars(bmap
->div
[i
]+1+offset
, n
, left
);
198 if (type
== isl_dim_div
) {
199 bmap
= move_divs_last(bmap
, first
, n
);
202 isl_basic_map_free_div(bmap
, n
);
204 bmap
->dim
= isl_space_drop_dims(bmap
->dim
, type
, first
, n
);
208 ISL_F_CLR(bmap
, ISL_BASIC_MAP_NORMALIZED
);
209 bmap
= isl_basic_map_simplify(bmap
);
210 return isl_basic_map_finalize(bmap
);
212 isl_basic_map_free(bmap
);
216 __isl_give isl_basic_set
*isl_basic_set_drop(__isl_take isl_basic_set
*bset
,
217 enum isl_dim_type type
, unsigned first
, unsigned n
)
219 return bset_from_bmap(isl_basic_map_drop(bset_to_bmap(bset
),
223 struct isl_basic_map
*isl_basic_map_drop_inputs(
224 struct isl_basic_map
*bmap
, unsigned first
, unsigned n
)
226 return isl_basic_map_drop(bmap
, isl_dim_in
, first
, n
);
229 struct isl_map
*isl_map_drop(struct isl_map
*map
,
230 enum isl_dim_type type
, unsigned first
, unsigned n
)
237 isl_assert(map
->ctx
, first
+ n
<= isl_map_dim(map
, type
), goto error
);
239 if (n
== 0 && !isl_space_get_tuple_name(map
->dim
, type
))
241 map
= isl_map_cow(map
);
244 map
->dim
= isl_space_drop_dims(map
->dim
, type
, first
, n
);
248 for (i
= 0; i
< map
->n
; ++i
) {
249 map
->p
[i
] = isl_basic_map_drop(map
->p
[i
], type
, first
, n
);
253 ISL_F_CLR(map
, ISL_MAP_NORMALIZED
);
261 struct isl_set
*isl_set_drop(struct isl_set
*set
,
262 enum isl_dim_type type
, unsigned first
, unsigned n
)
264 return (isl_set
*)isl_map_drop(set_to_map(set
), type
, first
, n
);
267 struct isl_map
*isl_map_drop_inputs(
268 struct isl_map
*map
, unsigned first
, unsigned n
)
270 return isl_map_drop(map
, isl_dim_in
, first
, n
);
274 * We don't cow, as the div is assumed to be redundant.
276 __isl_give isl_basic_map
*isl_basic_map_drop_div(
277 __isl_take isl_basic_map
*bmap
, unsigned div
)
285 pos
= 1 + isl_space_dim(bmap
->dim
, isl_dim_all
) + div
;
287 isl_assert(bmap
->ctx
, div
< bmap
->n_div
, goto error
);
289 for (i
= 0; i
< bmap
->n_eq
; ++i
)
290 constraint_drop_vars(bmap
->eq
[i
]+pos
, 1, bmap
->extra
-div
-1);
292 for (i
= 0; i
< bmap
->n_ineq
; ++i
) {
293 if (!isl_int_is_zero(bmap
->ineq
[i
][pos
])) {
294 isl_basic_map_drop_inequality(bmap
, i
);
298 constraint_drop_vars(bmap
->ineq
[i
]+pos
, 1, bmap
->extra
-div
-1);
301 for (i
= 0; i
< bmap
->n_div
; ++i
)
302 constraint_drop_vars(bmap
->div
[i
]+1+pos
, 1, bmap
->extra
-div
-1);
304 if (div
!= bmap
->n_div
- 1) {
306 isl_int
*t
= bmap
->div
[div
];
308 for (j
= div
; j
< bmap
->n_div
- 1; ++j
)
309 bmap
->div
[j
] = bmap
->div
[j
+1];
311 bmap
->div
[bmap
->n_div
- 1] = t
;
313 ISL_F_CLR(bmap
, ISL_BASIC_MAP_NORMALIZED
);
314 isl_basic_map_free_div(bmap
, 1);
318 isl_basic_map_free(bmap
);
322 struct isl_basic_map
*isl_basic_map_normalize_constraints(
323 struct isl_basic_map
*bmap
)
327 unsigned total
= isl_basic_map_total_dim(bmap
);
333 for (i
= bmap
->n_eq
- 1; i
>= 0; --i
) {
334 isl_seq_gcd(bmap
->eq
[i
]+1, total
, &gcd
);
335 if (isl_int_is_zero(gcd
)) {
336 if (!isl_int_is_zero(bmap
->eq
[i
][0])) {
337 bmap
= isl_basic_map_set_to_empty(bmap
);
340 isl_basic_map_drop_equality(bmap
, i
);
343 if (ISL_F_ISSET(bmap
, ISL_BASIC_MAP_RATIONAL
))
344 isl_int_gcd(gcd
, gcd
, bmap
->eq
[i
][0]);
345 if (isl_int_is_one(gcd
))
347 if (!isl_int_is_divisible_by(bmap
->eq
[i
][0], gcd
)) {
348 bmap
= isl_basic_map_set_to_empty(bmap
);
351 isl_seq_scale_down(bmap
->eq
[i
], bmap
->eq
[i
], gcd
, 1+total
);
354 for (i
= bmap
->n_ineq
- 1; i
>= 0; --i
) {
355 isl_seq_gcd(bmap
->ineq
[i
]+1, total
, &gcd
);
356 if (isl_int_is_zero(gcd
)) {
357 if (isl_int_is_neg(bmap
->ineq
[i
][0])) {
358 bmap
= isl_basic_map_set_to_empty(bmap
);
361 isl_basic_map_drop_inequality(bmap
, i
);
364 if (ISL_F_ISSET(bmap
, ISL_BASIC_MAP_RATIONAL
))
365 isl_int_gcd(gcd
, gcd
, bmap
->ineq
[i
][0]);
366 if (isl_int_is_one(gcd
))
368 isl_int_fdiv_q(bmap
->ineq
[i
][0], bmap
->ineq
[i
][0], gcd
);
369 isl_seq_scale_down(bmap
->ineq
[i
]+1, bmap
->ineq
[i
]+1, gcd
, total
);
376 struct isl_basic_set
*isl_basic_set_normalize_constraints(
377 struct isl_basic_set
*bset
)
379 isl_basic_map
*bmap
= bset_to_bmap(bset
);
380 return bset_from_bmap(isl_basic_map_normalize_constraints(bmap
));
383 /* Assuming the variable at position "pos" has an integer coefficient
384 * in integer division "div", extract it from this integer division.
385 * "pos" is as determined by isl_basic_map_offset, i.e., pos == 0
386 * corresponds to the constant term.
388 * That is, the integer division is of the form
390 * floor((... + c * d * x_pos + ...)/d)
394 * floor((... + 0 * x_pos + ...)/d) + c * x_pos
396 static __isl_give isl_basic_map
*remove_var_from_div(
397 __isl_take isl_basic_map
*bmap
, int div
, int pos
)
402 isl_int_divexact(shift
, bmap
->div
[div
][1 + pos
], bmap
->div
[div
][0]);
403 isl_int_neg(shift
, shift
);
404 bmap
= isl_basic_map_shift_div(bmap
, div
, pos
, shift
);
405 isl_int_clear(shift
);
410 /* Check if integer division "div" has any integral coefficient
411 * (or constant term). If so, extract them from the integer division.
413 static __isl_give isl_basic_map
*remove_independent_vars_from_div(
414 __isl_take isl_basic_map
*bmap
, int div
)
417 unsigned total
= 1 + isl_basic_map_total_dim(bmap
);
419 for (i
= 0; i
< total
; ++i
) {
420 if (isl_int_is_zero(bmap
->div
[div
][1 + i
]))
422 if (!isl_int_is_divisible_by(bmap
->div
[div
][1 + i
],
425 bmap
= remove_var_from_div(bmap
, div
, i
);
433 /* Check if any known integer division has any integral coefficient
434 * (or constant term). If so, extract them from the integer division.
436 static __isl_give isl_basic_map
*remove_independent_vars_from_divs(
437 __isl_take isl_basic_map
*bmap
)
443 if (bmap
->n_div
== 0)
446 for (i
= 0; i
< bmap
->n_div
; ++i
) {
447 if (isl_int_is_zero(bmap
->div
[i
][0]))
449 bmap
= remove_independent_vars_from_div(bmap
, i
);
457 /* Remove any common factor in numerator and denominator of the div expression,
458 * not taking into account the constant term.
459 * That is, if the div is of the form
461 * floor((a + m f(x))/(m d))
465 * floor((floor(a/m) + f(x))/d)
467 * The difference {a/m}/d in the argument satisfies 0 <= {a/m}/d < 1/d
468 * and can therefore not influence the result of the floor.
470 static void normalize_div_expression(__isl_keep isl_basic_map
*bmap
, int div
)
472 unsigned total
= isl_basic_map_total_dim(bmap
);
473 isl_ctx
*ctx
= bmap
->ctx
;
475 if (isl_int_is_zero(bmap
->div
[div
][0]))
477 isl_seq_gcd(bmap
->div
[div
] + 2, total
, &ctx
->normalize_gcd
);
478 isl_int_gcd(ctx
->normalize_gcd
, ctx
->normalize_gcd
, bmap
->div
[div
][0]);
479 if (isl_int_is_one(ctx
->normalize_gcd
))
481 isl_int_fdiv_q(bmap
->div
[div
][1], bmap
->div
[div
][1],
483 isl_int_divexact(bmap
->div
[div
][0], bmap
->div
[div
][0],
485 isl_seq_scale_down(bmap
->div
[div
] + 2, bmap
->div
[div
] + 2,
486 ctx
->normalize_gcd
, total
);
489 /* Remove any common factor in numerator and denominator of a div expression,
490 * not taking into account the constant term.
491 * That is, look for any div of the form
493 * floor((a + m f(x))/(m d))
497 * floor((floor(a/m) + f(x))/d)
499 * The difference {a/m}/d in the argument satisfies 0 <= {a/m}/d < 1/d
500 * and can therefore not influence the result of the floor.
502 static __isl_give isl_basic_map
*normalize_div_expressions(
503 __isl_take isl_basic_map
*bmap
)
509 if (bmap
->n_div
== 0)
512 for (i
= 0; i
< bmap
->n_div
; ++i
)
513 normalize_div_expression(bmap
, i
);
518 /* Assumes divs have been ordered if keep_divs is set.
520 static void eliminate_var_using_equality(struct isl_basic_map
*bmap
,
521 unsigned pos
, isl_int
*eq
, int keep_divs
, int *progress
)
524 unsigned space_total
;
528 total
= isl_basic_map_total_dim(bmap
);
529 space_total
= isl_space_dim(bmap
->dim
, isl_dim_all
);
530 last_div
= isl_seq_last_non_zero(eq
+ 1 + space_total
, bmap
->n_div
);
531 for (k
= 0; k
< bmap
->n_eq
; ++k
) {
532 if (bmap
->eq
[k
] == eq
)
534 if (isl_int_is_zero(bmap
->eq
[k
][1+pos
]))
538 isl_seq_elim(bmap
->eq
[k
], eq
, 1+pos
, 1+total
, NULL
);
539 isl_seq_normalize(bmap
->ctx
, bmap
->eq
[k
], 1 + total
);
542 for (k
= 0; k
< bmap
->n_ineq
; ++k
) {
543 if (isl_int_is_zero(bmap
->ineq
[k
][1+pos
]))
547 isl_seq_elim(bmap
->ineq
[k
], eq
, 1+pos
, 1+total
, NULL
);
548 isl_seq_normalize(bmap
->ctx
, bmap
->ineq
[k
], 1 + total
);
549 ISL_F_CLR(bmap
, ISL_BASIC_MAP_NORMALIZED
);
552 for (k
= 0; k
< bmap
->n_div
; ++k
) {
553 if (isl_int_is_zero(bmap
->div
[k
][0]))
555 if (isl_int_is_zero(bmap
->div
[k
][1+1+pos
]))
559 /* We need to be careful about circular definitions,
560 * so for now we just remove the definition of div k
561 * if the equality contains any divs.
562 * If keep_divs is set, then the divs have been ordered
563 * and we can keep the definition as long as the result
566 if (last_div
== -1 || (keep_divs
&& last_div
< k
)) {
567 isl_seq_elim(bmap
->div
[k
]+1, eq
,
568 1+pos
, 1+total
, &bmap
->div
[k
][0]);
569 normalize_div_expression(bmap
, k
);
571 isl_seq_clr(bmap
->div
[k
], 1 + total
);
572 ISL_F_CLR(bmap
, ISL_BASIC_MAP_NORMALIZED
);
576 /* Assumes divs have been ordered if keep_divs is set.
578 static __isl_give isl_basic_map
*eliminate_div(__isl_take isl_basic_map
*bmap
,
579 isl_int
*eq
, unsigned div
, int keep_divs
)
581 unsigned pos
= isl_space_dim(bmap
->dim
, isl_dim_all
) + div
;
583 eliminate_var_using_equality(bmap
, pos
, eq
, keep_divs
, NULL
);
585 bmap
= isl_basic_map_drop_div(bmap
, div
);
590 /* Check if elimination of div "div" using equality "eq" would not
591 * result in a div depending on a later div.
593 static int ok_to_eliminate_div(struct isl_basic_map
*bmap
, isl_int
*eq
,
598 unsigned space_total
= isl_space_dim(bmap
->dim
, isl_dim_all
);
599 unsigned pos
= space_total
+ div
;
601 last_div
= isl_seq_last_non_zero(eq
+ 1 + space_total
, bmap
->n_div
);
602 if (last_div
< 0 || last_div
<= div
)
605 for (k
= 0; k
<= last_div
; ++k
) {
606 if (isl_int_is_zero(bmap
->div
[k
][0]))
608 if (!isl_int_is_zero(bmap
->div
[k
][1 + 1 + pos
]))
615 /* Elimininate divs based on equalities
617 static struct isl_basic_map
*eliminate_divs_eq(
618 struct isl_basic_map
*bmap
, int *progress
)
625 bmap
= isl_basic_map_order_divs(bmap
);
630 off
= 1 + isl_space_dim(bmap
->dim
, isl_dim_all
);
632 for (d
= bmap
->n_div
- 1; d
>= 0 ; --d
) {
633 for (i
= 0; i
< bmap
->n_eq
; ++i
) {
634 if (!isl_int_is_one(bmap
->eq
[i
][off
+ d
]) &&
635 !isl_int_is_negone(bmap
->eq
[i
][off
+ d
]))
637 if (!ok_to_eliminate_div(bmap
, bmap
->eq
[i
], d
))
641 bmap
= eliminate_div(bmap
, bmap
->eq
[i
], d
, 1);
642 if (isl_basic_map_drop_equality(bmap
, i
) < 0)
643 return isl_basic_map_free(bmap
);
648 return eliminate_divs_eq(bmap
, progress
);
652 /* Elimininate divs based on inequalities
654 static struct isl_basic_map
*eliminate_divs_ineq(
655 struct isl_basic_map
*bmap
, int *progress
)
666 off
= 1 + isl_space_dim(bmap
->dim
, isl_dim_all
);
668 for (d
= bmap
->n_div
- 1; d
>= 0 ; --d
) {
669 for (i
= 0; i
< bmap
->n_eq
; ++i
)
670 if (!isl_int_is_zero(bmap
->eq
[i
][off
+ d
]))
674 for (i
= 0; i
< bmap
->n_ineq
; ++i
)
675 if (isl_int_abs_gt(bmap
->ineq
[i
][off
+ d
], ctx
->one
))
677 if (i
< bmap
->n_ineq
)
680 bmap
= isl_basic_map_eliminate_vars(bmap
, (off
-1)+d
, 1);
681 if (!bmap
|| ISL_F_ISSET(bmap
, ISL_BASIC_MAP_EMPTY
))
683 bmap
= isl_basic_map_drop_div(bmap
, d
);
690 struct isl_basic_map
*isl_basic_map_gauss(
691 struct isl_basic_map
*bmap
, int *progress
)
699 bmap
= isl_basic_map_order_divs(bmap
);
704 total
= isl_basic_map_total_dim(bmap
);
705 total_var
= total
- bmap
->n_div
;
707 last_var
= total
- 1;
708 for (done
= 0; done
< bmap
->n_eq
; ++done
) {
709 for (; last_var
>= 0; --last_var
) {
710 for (k
= done
; k
< bmap
->n_eq
; ++k
)
711 if (!isl_int_is_zero(bmap
->eq
[k
][1+last_var
]))
719 swap_equality(bmap
, k
, done
);
720 if (isl_int_is_neg(bmap
->eq
[done
][1+last_var
]))
721 isl_seq_neg(bmap
->eq
[done
], bmap
->eq
[done
], 1+total
);
723 eliminate_var_using_equality(bmap
, last_var
, bmap
->eq
[done
], 1,
726 if (last_var
>= total_var
&&
727 isl_int_is_zero(bmap
->div
[last_var
- total_var
][0])) {
728 unsigned div
= last_var
- total_var
;
729 isl_seq_neg(bmap
->div
[div
]+1, bmap
->eq
[done
], 1+total
);
730 isl_int_set_si(bmap
->div
[div
][1+1+last_var
], 0);
731 isl_int_set(bmap
->div
[div
][0],
732 bmap
->eq
[done
][1+last_var
]);
735 ISL_F_CLR(bmap
, ISL_BASIC_MAP_NORMALIZED
);
738 if (done
== bmap
->n_eq
)
740 for (k
= done
; k
< bmap
->n_eq
; ++k
) {
741 if (isl_int_is_zero(bmap
->eq
[k
][0]))
743 return isl_basic_map_set_to_empty(bmap
);
745 isl_basic_map_free_equality(bmap
, bmap
->n_eq
-done
);
749 struct isl_basic_set
*isl_basic_set_gauss(
750 struct isl_basic_set
*bset
, int *progress
)
752 return bset_from_bmap(isl_basic_map_gauss(bset_to_bmap(bset
),
757 static unsigned int round_up(unsigned int v
)
768 /* Hash table of inequalities in a basic map.
769 * "index" is an array of addresses of inequalities in the basic map, some
770 * of which are NULL. The inequalities are hashed on the coefficients
771 * except the constant term.
772 * "size" is the number of elements in the array and is always a power of two
773 * "bits" is the number of bits need to represent an index into the array.
774 * "total" is the total dimension of the basic map.
776 struct isl_constraint_index
{
783 /* Fill in the "ci" data structure for holding the inequalities of "bmap".
785 static isl_stat
create_constraint_index(struct isl_constraint_index
*ci
,
786 __isl_keep isl_basic_map
*bmap
)
792 return isl_stat_error
;
793 ci
->total
= isl_basic_set_total_dim(bmap
);
794 if (bmap
->n_ineq
== 0)
796 ci
->size
= round_up(4 * (bmap
->n_ineq
+ 1) / 3 - 1);
797 ci
->bits
= ffs(ci
->size
) - 1;
798 ctx
= isl_basic_map_get_ctx(bmap
);
799 ci
->index
= isl_calloc_array(ctx
, isl_int
**, ci
->size
);
801 return isl_stat_error
;
806 /* Free the memory allocated by create_constraint_index.
808 static void constraint_index_free(struct isl_constraint_index
*ci
)
813 /* Return the position in ci->index that contains the address of
814 * an inequality that is equal to *ineq up to the constant term,
815 * provided this address is not identical to "ineq".
816 * If there is no such inequality, then return the position where
817 * such an inequality should be inserted.
819 static int hash_index_ineq(struct isl_constraint_index
*ci
, isl_int
**ineq
)
822 uint32_t hash
= isl_seq_get_hash_bits((*ineq
) + 1, ci
->total
, ci
->bits
);
823 for (h
= hash
; ci
->index
[h
]; h
= (h
+1) % ci
->size
)
824 if (ineq
!= ci
->index
[h
] &&
825 isl_seq_eq((*ineq
) + 1, ci
->index
[h
][0]+1, ci
->total
))
830 /* Return the position in ci->index that contains the address of
831 * an inequality that is equal to the k'th inequality of "bmap"
832 * up to the constant term, provided it does not point to the very
834 * If there is no such inequality, then return the position where
835 * such an inequality should be inserted.
837 static int hash_index(struct isl_constraint_index
*ci
,
838 __isl_keep isl_basic_map
*bmap
, int k
)
840 return hash_index_ineq(ci
, &bmap
->ineq
[k
]);
843 static int set_hash_index(struct isl_constraint_index
*ci
,
844 struct isl_basic_set
*bset
, int k
)
846 return hash_index(ci
, bset
, k
);
849 /* Fill in the "ci" data structure with the inequalities of "bset".
851 static isl_stat
setup_constraint_index(struct isl_constraint_index
*ci
,
852 __isl_keep isl_basic_set
*bset
)
856 if (create_constraint_index(ci
, bset
) < 0)
857 return isl_stat_error
;
859 for (k
= 0; k
< bset
->n_ineq
; ++k
) {
860 h
= set_hash_index(ci
, bset
, k
);
861 ci
->index
[h
] = &bset
->ineq
[k
];
867 /* Is the inequality ineq (obviously) redundant with respect
868 * to the constraints in "ci"?
870 * Look for an inequality in "ci" with the same coefficients and then
871 * check if the contant term of "ineq" is greater than or equal
872 * to the constant term of that inequality. If so, "ineq" is clearly
875 * Note that hash_index_ineq ignores a stored constraint if it has
876 * the same address as the passed inequality. It is ok to pass
877 * the address of a local variable here since it will never be
878 * the same as the address of a constraint in "ci".
880 static isl_bool
constraint_index_is_redundant(struct isl_constraint_index
*ci
,
885 h
= hash_index_ineq(ci
, &ineq
);
887 return isl_bool_false
;
888 return isl_int_ge(ineq
[0], (*ci
->index
[h
])[0]);
891 /* If we can eliminate more than one div, then we need to make
892 * sure we do it from last div to first div, in order not to
893 * change the position of the other divs that still need to
896 static struct isl_basic_map
*remove_duplicate_divs(
897 struct isl_basic_map
*bmap
, int *progress
)
909 bmap
= isl_basic_map_order_divs(bmap
);
910 if (!bmap
|| bmap
->n_div
<= 1)
913 total_var
= isl_space_dim(bmap
->dim
, isl_dim_all
);
914 total
= total_var
+ 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
))
955 for (l
= bmap
->n_div
- 1; l
>= 0; --l
) {
959 isl_int_set_si(eq
.data
[1+total_var
+k
], -1);
960 isl_int_set_si(eq
.data
[1+total_var
+l
], 1);
961 bmap
= eliminate_div(bmap
, eq
.data
, l
, 1);
964 isl_int_set_si(eq
.data
[1+total_var
+k
], 0);
965 isl_int_set_si(eq
.data
[1+total_var
+l
], 0);
968 isl_blk_free(ctx
, eq
);
975 static int n_pure_div_eq(struct isl_basic_map
*bmap
)
980 total
= isl_space_dim(bmap
->dim
, isl_dim_all
);
981 for (i
= 0, j
= bmap
->n_div
-1; i
< bmap
->n_eq
; ++i
) {
982 while (j
>= 0 && isl_int_is_zero(bmap
->eq
[i
][1 + total
+ j
]))
986 if (isl_seq_first_non_zero(bmap
->eq
[i
] + 1 + total
, j
) != -1)
992 /* Normalize divs that appear in equalities.
994 * In particular, we assume that bmap contains some equalities
999 * and we want to replace the set of e_i by a minimal set and
1000 * such that the new e_i have a canonical representation in terms
1002 * If any of the equalities involves more than one divs, then
1003 * we currently simply bail out.
1005 * Let us first additionally assume that all equalities involve
1006 * a div. The equalities then express modulo constraints on the
1007 * remaining variables and we can use "parameter compression"
1008 * to find a minimal set of constraints. The result is a transformation
1010 * x = T(x') = x_0 + G x'
1012 * with G a lower-triangular matrix with all elements below the diagonal
1013 * non-negative and smaller than the diagonal element on the same row.
1014 * We first normalize x_0 by making the same property hold in the affine
1016 * The rows i of G with a 1 on the diagonal do not impose any modulo
1017 * constraint and simply express x_i = x'_i.
1018 * For each of the remaining rows i, we introduce a div and a corresponding
1019 * equality. In particular
1021 * g_ii e_j = x_i - g_i(x')
1023 * where each x'_k is replaced either by x_k (if g_kk = 1) or the
1024 * corresponding div (if g_kk != 1).
1026 * If there are any equalities not involving any div, then we
1027 * first apply a variable compression on the variables x:
1029 * x = C x'' x'' = C_2 x
1031 * and perform the above parameter compression on A C instead of on A.
1032 * The resulting compression is then of the form
1034 * x'' = T(x') = x_0 + G x'
1036 * and in constructing the new divs and the corresponding equalities,
1037 * we have to replace each x'', i.e., the x'_k with (g_kk = 1),
1038 * by the corresponding row from C_2.
1040 static struct isl_basic_map
*normalize_divs(
1041 struct isl_basic_map
*bmap
, int *progress
)
1048 struct isl_mat
*T
= NULL
;
1049 struct isl_mat
*C
= NULL
;
1050 struct isl_mat
*C2
= NULL
;
1053 int dropped
, needed
;
1058 if (bmap
->n_div
== 0)
1061 if (bmap
->n_eq
== 0)
1064 if (ISL_F_ISSET(bmap
, ISL_BASIC_MAP_NORMALIZED_DIVS
))
1067 total
= isl_space_dim(bmap
->dim
, isl_dim_all
);
1068 div_eq
= n_pure_div_eq(bmap
);
1072 if (div_eq
< bmap
->n_eq
) {
1073 B
= isl_mat_sub_alloc6(bmap
->ctx
, bmap
->eq
, div_eq
,
1074 bmap
->n_eq
- div_eq
, 0, 1 + total
);
1075 C
= isl_mat_variable_compression(B
, &C2
);
1078 if (C
->n_col
== 0) {
1079 bmap
= isl_basic_map_set_to_empty(bmap
);
1086 d
= isl_vec_alloc(bmap
->ctx
, div_eq
);
1089 for (i
= 0, j
= bmap
->n_div
-1; i
< div_eq
; ++i
) {
1090 while (j
>= 0 && isl_int_is_zero(bmap
->eq
[i
][1 + total
+ j
]))
1092 isl_int_set(d
->block
.data
[i
], bmap
->eq
[i
][1 + total
+ j
]);
1094 B
= isl_mat_sub_alloc6(bmap
->ctx
, bmap
->eq
, 0, div_eq
, 0, 1 + total
);
1097 B
= isl_mat_product(B
, C
);
1101 T
= isl_mat_parameter_compression(B
, d
);
1104 if (T
->n_col
== 0) {
1105 bmap
= isl_basic_map_set_to_empty(bmap
);
1111 for (i
= 0; i
< T
->n_row
- 1; ++i
) {
1112 isl_int_fdiv_q(v
, T
->row
[1 + i
][0], T
->row
[1 + i
][1 + i
]);
1113 if (isl_int_is_zero(v
))
1115 isl_mat_col_submul(T
, 0, v
, 1 + i
);
1118 pos
= isl_alloc_array(bmap
->ctx
, int, T
->n_row
);
1121 /* We have to be careful because dropping equalities may reorder them */
1123 for (j
= bmap
->n_div
- 1; j
>= 0; --j
) {
1124 for (i
= 0; i
< bmap
->n_eq
; ++i
)
1125 if (!isl_int_is_zero(bmap
->eq
[i
][1 + total
+ j
]))
1127 if (i
< bmap
->n_eq
) {
1128 bmap
= isl_basic_map_drop_div(bmap
, j
);
1129 isl_basic_map_drop_equality(bmap
, i
);
1135 for (i
= 1; i
< T
->n_row
; ++i
) {
1136 if (isl_int_is_one(T
->row
[i
][i
]))
1141 if (needed
> dropped
) {
1142 bmap
= isl_basic_map_extend_space(bmap
, isl_space_copy(bmap
->dim
),
1147 for (i
= 1; i
< T
->n_row
; ++i
) {
1148 if (isl_int_is_one(T
->row
[i
][i
]))
1150 k
= isl_basic_map_alloc_div(bmap
);
1151 pos
[i
] = 1 + total
+ k
;
1152 isl_seq_clr(bmap
->div
[k
] + 1, 1 + total
+ bmap
->n_div
);
1153 isl_int_set(bmap
->div
[k
][0], T
->row
[i
][i
]);
1155 isl_seq_cpy(bmap
->div
[k
] + 1, C2
->row
[i
], 1 + total
);
1157 isl_int_set_si(bmap
->div
[k
][1 + i
], 1);
1158 for (j
= 0; j
< i
; ++j
) {
1159 if (isl_int_is_zero(T
->row
[i
][j
]))
1161 if (pos
[j
] < T
->n_row
&& C2
)
1162 isl_seq_submul(bmap
->div
[k
] + 1, T
->row
[i
][j
],
1163 C2
->row
[pos
[j
]], 1 + total
);
1165 isl_int_neg(bmap
->div
[k
][1 + pos
[j
]],
1168 j
= isl_basic_map_alloc_equality(bmap
);
1169 isl_seq_neg(bmap
->eq
[j
], bmap
->div
[k
]+1, 1+total
+bmap
->n_div
);
1170 isl_int_set(bmap
->eq
[j
][pos
[i
]], bmap
->div
[k
][0]);
1179 ISL_F_SET(bmap
, ISL_BASIC_MAP_NORMALIZED_DIVS
);
1189 static struct isl_basic_map
*set_div_from_lower_bound(
1190 struct isl_basic_map
*bmap
, int div
, int ineq
)
1192 unsigned total
= 1 + isl_space_dim(bmap
->dim
, isl_dim_all
);
1194 isl_seq_neg(bmap
->div
[div
] + 1, bmap
->ineq
[ineq
], total
+ bmap
->n_div
);
1195 isl_int_set(bmap
->div
[div
][0], bmap
->ineq
[ineq
][total
+ div
]);
1196 isl_int_add(bmap
->div
[div
][1], bmap
->div
[div
][1], bmap
->div
[div
][0]);
1197 isl_int_sub_ui(bmap
->div
[div
][1], bmap
->div
[div
][1], 1);
1198 isl_int_set_si(bmap
->div
[div
][1 + total
+ div
], 0);
1203 /* Check whether it is ok to define a div based on an inequality.
1204 * To avoid the introduction of circular definitions of divs, we
1205 * do not allow such a definition if the resulting expression would refer to
1206 * any other undefined divs or if any known div is defined in
1207 * terms of the unknown div.
1209 static int ok_to_set_div_from_bound(struct isl_basic_map
*bmap
,
1213 unsigned total
= 1 + isl_space_dim(bmap
->dim
, isl_dim_all
);
1215 /* Not defined in terms of unknown divs */
1216 for (j
= 0; j
< bmap
->n_div
; ++j
) {
1219 if (isl_int_is_zero(bmap
->ineq
[ineq
][total
+ j
]))
1221 if (isl_int_is_zero(bmap
->div
[j
][0]))
1225 /* No other div defined in terms of this one => avoid loops */
1226 for (j
= 0; j
< bmap
->n_div
; ++j
) {
1229 if (isl_int_is_zero(bmap
->div
[j
][0]))
1231 if (!isl_int_is_zero(bmap
->div
[j
][1 + total
+ div
]))
1238 /* Would an expression for div "div" based on inequality "ineq" of "bmap"
1239 * be a better expression than the current one?
1241 * If we do not have any expression yet, then any expression would be better.
1242 * Otherwise we check if the last variable involved in the inequality
1243 * (disregarding the div that it would define) is in an earlier position
1244 * than the last variable involved in the current div expression.
1246 static int better_div_constraint(__isl_keep isl_basic_map
*bmap
,
1249 unsigned total
= 1 + isl_space_dim(bmap
->dim
, isl_dim_all
);
1253 if (isl_int_is_zero(bmap
->div
[div
][0]))
1256 if (isl_seq_last_non_zero(bmap
->ineq
[ineq
] + total
+ div
+ 1,
1257 bmap
->n_div
- (div
+ 1)) >= 0)
1260 last_ineq
= isl_seq_last_non_zero(bmap
->ineq
[ineq
], total
+ div
);
1261 last_div
= isl_seq_last_non_zero(bmap
->div
[div
] + 1,
1262 total
+ bmap
->n_div
);
1264 return last_ineq
< last_div
;
1267 /* Given two constraints "k" and "l" that are opposite to each other,
1268 * except for the constant term, check if we can use them
1269 * to obtain an expression for one of the hitherto unknown divs or
1270 * a "better" expression for a div for which we already have an expression.
1271 * "sum" is the sum of the constant terms of the constraints.
1272 * If this sum is strictly smaller than the coefficient of one
1273 * of the divs, then this pair can be used define the div.
1274 * To avoid the introduction of circular definitions of divs, we
1275 * do not use the pair if the resulting expression would refer to
1276 * any other undefined divs or if any known div is defined in
1277 * terms of the unknown div.
1279 static struct isl_basic_map
*check_for_div_constraints(
1280 struct isl_basic_map
*bmap
, int k
, int l
, isl_int sum
, int *progress
)
1283 unsigned total
= 1 + isl_space_dim(bmap
->dim
, isl_dim_all
);
1285 for (i
= 0; i
< bmap
->n_div
; ++i
) {
1286 if (isl_int_is_zero(bmap
->ineq
[k
][total
+ i
]))
1288 if (isl_int_abs_ge(sum
, bmap
->ineq
[k
][total
+ i
]))
1290 if (!better_div_constraint(bmap
, i
, k
))
1292 if (!ok_to_set_div_from_bound(bmap
, i
, k
))
1294 if (isl_int_is_pos(bmap
->ineq
[k
][total
+ i
]))
1295 bmap
= set_div_from_lower_bound(bmap
, i
, k
);
1297 bmap
= set_div_from_lower_bound(bmap
, i
, l
);
1305 __isl_give isl_basic_map
*isl_basic_map_remove_duplicate_constraints(
1306 __isl_take isl_basic_map
*bmap
, int *progress
, int detect_divs
)
1308 struct isl_constraint_index ci
;
1310 unsigned total
= isl_basic_map_total_dim(bmap
);
1313 if (!bmap
|| bmap
->n_ineq
<= 1)
1316 if (create_constraint_index(&ci
, bmap
) < 0)
1319 h
= isl_seq_get_hash_bits(bmap
->ineq
[0] + 1, total
, ci
.bits
);
1320 ci
.index
[h
] = &bmap
->ineq
[0];
1321 for (k
= 1; k
< bmap
->n_ineq
; ++k
) {
1322 h
= hash_index(&ci
, bmap
, k
);
1324 ci
.index
[h
] = &bmap
->ineq
[k
];
1329 l
= ci
.index
[h
] - &bmap
->ineq
[0];
1330 if (isl_int_lt(bmap
->ineq
[k
][0], bmap
->ineq
[l
][0]))
1331 swap_inequality(bmap
, k
, l
);
1332 isl_basic_map_drop_inequality(bmap
, k
);
1336 for (k
= 0; k
< bmap
->n_ineq
-1; ++k
) {
1337 isl_seq_neg(bmap
->ineq
[k
]+1, bmap
->ineq
[k
]+1, total
);
1338 h
= hash_index(&ci
, bmap
, k
);
1339 isl_seq_neg(bmap
->ineq
[k
]+1, bmap
->ineq
[k
]+1, total
);
1342 l
= ci
.index
[h
] - &bmap
->ineq
[0];
1343 isl_int_add(sum
, bmap
->ineq
[k
][0], bmap
->ineq
[l
][0]);
1344 if (isl_int_is_pos(sum
)) {
1346 bmap
= check_for_div_constraints(bmap
, k
, l
,
1350 if (isl_int_is_zero(sum
)) {
1351 /* We need to break out of the loop after these
1352 * changes since the contents of the hash
1353 * will no longer be valid.
1354 * Plus, we probably we want to regauss first.
1358 isl_basic_map_drop_inequality(bmap
, l
);
1359 isl_basic_map_inequality_to_equality(bmap
, k
);
1361 bmap
= isl_basic_map_set_to_empty(bmap
);
1366 constraint_index_free(&ci
);
1370 /* Detect all pairs of inequalities that form an equality.
1372 * isl_basic_map_remove_duplicate_constraints detects at most one such pair.
1373 * Call it repeatedly while it is making progress.
1375 __isl_give isl_basic_map
*isl_basic_map_detect_inequality_pairs(
1376 __isl_take isl_basic_map
*bmap
, int *progress
)
1382 bmap
= isl_basic_map_remove_duplicate_constraints(bmap
,
1384 if (progress
&& duplicate
)
1386 } while (duplicate
);
1391 /* Eliminate knowns divs from constraints where they appear with
1392 * a (positive or negative) unit coefficient.
1396 * floor(e/m) + f >= 0
1404 * -floor(e/m) + f >= 0
1408 * -e + m f + m - 1 >= 0
1410 * The first conversion is valid because floor(e/m) >= -f is equivalent
1411 * to e/m >= -f because -f is an integral expression.
1412 * The second conversion follows from the fact that
1414 * -floor(e/m) = ceil(-e/m) = floor((-e + m - 1)/m)
1417 * Note that one of the div constraints may have been eliminated
1418 * due to being redundant with respect to the constraint that is
1419 * being modified by this function. The modified constraint may
1420 * no longer imply this div constraint, so we add it back to make
1421 * sure we do not lose any information.
1423 * We skip integral divs, i.e., those with denominator 1, as we would
1424 * risk eliminating the div from the div constraints. We do not need
1425 * to handle those divs here anyway since the div constraints will turn
1426 * out to form an equality and this equality can then be use to eliminate
1427 * the div from all constraints.
1429 static __isl_give isl_basic_map
*eliminate_unit_divs(
1430 __isl_take isl_basic_map
*bmap
, int *progress
)
1439 ctx
= isl_basic_map_get_ctx(bmap
);
1440 total
= 1 + isl_space_dim(bmap
->dim
, isl_dim_all
);
1442 for (i
= 0; i
< bmap
->n_div
; ++i
) {
1443 if (isl_int_is_zero(bmap
->div
[i
][0]))
1445 if (isl_int_is_one(bmap
->div
[i
][0]))
1447 for (j
= 0; j
< bmap
->n_ineq
; ++j
) {
1450 if (!isl_int_is_one(bmap
->ineq
[j
][total
+ i
]) &&
1451 !isl_int_is_negone(bmap
->ineq
[j
][total
+ i
]))
1456 s
= isl_int_sgn(bmap
->ineq
[j
][total
+ i
]);
1457 isl_int_set_si(bmap
->ineq
[j
][total
+ i
], 0);
1459 isl_seq_combine(bmap
->ineq
[j
],
1460 ctx
->negone
, bmap
->div
[i
] + 1,
1461 bmap
->div
[i
][0], bmap
->ineq
[j
],
1462 total
+ bmap
->n_div
);
1464 isl_seq_combine(bmap
->ineq
[j
],
1465 ctx
->one
, bmap
->div
[i
] + 1,
1466 bmap
->div
[i
][0], bmap
->ineq
[j
],
1467 total
+ bmap
->n_div
);
1469 isl_int_add(bmap
->ineq
[j
][0],
1470 bmap
->ineq
[j
][0], bmap
->div
[i
][0]);
1471 isl_int_sub_ui(bmap
->ineq
[j
][0],
1472 bmap
->ineq
[j
][0], 1);
1475 bmap
= isl_basic_map_extend_constraints(bmap
, 0, 1);
1476 if (isl_basic_map_add_div_constraint(bmap
, i
, s
) < 0)
1477 return isl_basic_map_free(bmap
);
1484 struct isl_basic_map
*isl_basic_map_simplify(struct isl_basic_map
*bmap
)
1493 if (isl_basic_map_plain_is_empty(bmap
))
1495 bmap
= isl_basic_map_normalize_constraints(bmap
);
1496 bmap
= remove_independent_vars_from_divs(bmap
);
1497 bmap
= normalize_div_expressions(bmap
);
1498 bmap
= remove_duplicate_divs(bmap
, &progress
);
1499 bmap
= eliminate_unit_divs(bmap
, &progress
);
1500 bmap
= eliminate_divs_eq(bmap
, &progress
);
1501 bmap
= eliminate_divs_ineq(bmap
, &progress
);
1502 bmap
= isl_basic_map_gauss(bmap
, &progress
);
1503 /* requires equalities in normal form */
1504 bmap
= normalize_divs(bmap
, &progress
);
1505 bmap
= isl_basic_map_remove_duplicate_constraints(bmap
,
1507 if (bmap
&& progress
)
1508 ISL_F_CLR(bmap
, ISL_BASIC_MAP_REDUCED_COEFFICIENTS
);
1513 struct isl_basic_set
*isl_basic_set_simplify(struct isl_basic_set
*bset
)
1515 return bset_from_bmap(isl_basic_map_simplify(bset_to_bmap(bset
)));
1519 int isl_basic_map_is_div_constraint(__isl_keep isl_basic_map
*bmap
,
1520 isl_int
*constraint
, unsigned div
)
1527 pos
= 1 + isl_space_dim(bmap
->dim
, isl_dim_all
) + div
;
1529 if (isl_int_eq(constraint
[pos
], bmap
->div
[div
][0])) {
1531 isl_int_sub(bmap
->div
[div
][1],
1532 bmap
->div
[div
][1], bmap
->div
[div
][0]);
1533 isl_int_add_ui(bmap
->div
[div
][1], bmap
->div
[div
][1], 1);
1534 neg
= isl_seq_is_neg(constraint
, bmap
->div
[div
]+1, pos
);
1535 isl_int_sub_ui(bmap
->div
[div
][1], bmap
->div
[div
][1], 1);
1536 isl_int_add(bmap
->div
[div
][1],
1537 bmap
->div
[div
][1], bmap
->div
[div
][0]);
1540 if (isl_seq_first_non_zero(constraint
+pos
+1,
1541 bmap
->n_div
-div
-1) != -1)
1543 } else if (isl_int_abs_eq(constraint
[pos
], bmap
->div
[div
][0])) {
1544 if (!isl_seq_eq(constraint
, bmap
->div
[div
]+1, pos
))
1546 if (isl_seq_first_non_zero(constraint
+pos
+1,
1547 bmap
->n_div
-div
-1) != -1)
1555 int isl_basic_set_is_div_constraint(__isl_keep isl_basic_set
*bset
,
1556 isl_int
*constraint
, unsigned div
)
1558 return isl_basic_map_is_div_constraint(bset
, constraint
, div
);
1562 /* If the only constraints a div d=floor(f/m)
1563 * appears in are its two defining constraints
1566 * -(f - (m - 1)) + m d >= 0
1568 * then it can safely be removed.
1570 static int div_is_redundant(struct isl_basic_map
*bmap
, int div
)
1573 unsigned pos
= 1 + isl_space_dim(bmap
->dim
, isl_dim_all
) + div
;
1575 for (i
= 0; i
< bmap
->n_eq
; ++i
)
1576 if (!isl_int_is_zero(bmap
->eq
[i
][pos
]))
1579 for (i
= 0; i
< bmap
->n_ineq
; ++i
) {
1580 if (isl_int_is_zero(bmap
->ineq
[i
][pos
]))
1582 if (!isl_basic_map_is_div_constraint(bmap
, bmap
->ineq
[i
], div
))
1586 for (i
= 0; i
< bmap
->n_div
; ++i
) {
1587 if (isl_int_is_zero(bmap
->div
[i
][0]))
1589 if (!isl_int_is_zero(bmap
->div
[i
][1+pos
]))
1597 * Remove divs that don't occur in any of the constraints or other divs.
1598 * These can arise when dropping constraints from a basic map or
1599 * when the divs of a basic map have been temporarily aligned
1600 * with the divs of another basic map.
1602 static struct isl_basic_map
*remove_redundant_divs(struct isl_basic_map
*bmap
)
1609 for (i
= bmap
->n_div
-1; i
>= 0; --i
) {
1610 if (!div_is_redundant(bmap
, i
))
1612 bmap
= isl_basic_map_drop_div(bmap
, i
);
1617 /* Mark "bmap" as final, without checking for obviously redundant
1618 * integer divisions. This function should be used when "bmap"
1619 * is known not to involve any such integer divisions.
1621 __isl_give isl_basic_map
*isl_basic_map_mark_final(
1622 __isl_take isl_basic_map
*bmap
)
1626 ISL_F_SET(bmap
, ISL_BASIC_SET_FINAL
);
1630 /* Mark "bmap" as final, after removing obviously redundant integer divisions.
1632 struct isl_basic_map
*isl_basic_map_finalize(struct isl_basic_map
*bmap
)
1634 bmap
= remove_redundant_divs(bmap
);
1635 bmap
= isl_basic_map_mark_final(bmap
);
1639 struct isl_basic_set
*isl_basic_set_finalize(struct isl_basic_set
*bset
)
1641 return bset_from_bmap(isl_basic_map_finalize(bset_to_bmap(bset
)));
1644 struct isl_set
*isl_set_finalize(struct isl_set
*set
)
1650 for (i
= 0; i
< set
->n
; ++i
) {
1651 set
->p
[i
] = isl_basic_set_finalize(set
->p
[i
]);
1661 struct isl_map
*isl_map_finalize(struct isl_map
*map
)
1667 for (i
= 0; i
< map
->n
; ++i
) {
1668 map
->p
[i
] = isl_basic_map_finalize(map
->p
[i
]);
1672 ISL_F_CLR(map
, ISL_MAP_NORMALIZED
);
1680 /* Remove definition of any div that is defined in terms of the given variable.
1681 * The div itself is not removed. Functions such as
1682 * eliminate_divs_ineq depend on the other divs remaining in place.
1684 static struct isl_basic_map
*remove_dependent_vars(struct isl_basic_map
*bmap
,
1692 for (i
= 0; i
< bmap
->n_div
; ++i
) {
1693 if (isl_int_is_zero(bmap
->div
[i
][0]))
1695 if (isl_int_is_zero(bmap
->div
[i
][1+1+pos
]))
1697 bmap
= isl_basic_map_mark_div_unknown(bmap
, i
);
1704 /* Eliminate the specified variables from the constraints using
1705 * Fourier-Motzkin. The variables themselves are not removed.
1707 struct isl_basic_map
*isl_basic_map_eliminate_vars(
1708 struct isl_basic_map
*bmap
, unsigned pos
, unsigned n
)
1719 total
= isl_basic_map_total_dim(bmap
);
1721 bmap
= isl_basic_map_cow(bmap
);
1722 for (d
= pos
+ n
- 1; d
>= 0 && d
>= pos
; --d
)
1723 bmap
= remove_dependent_vars(bmap
, d
);
1727 for (d
= pos
+ n
- 1;
1728 d
>= 0 && d
>= total
- bmap
->n_div
&& d
>= pos
; --d
)
1729 isl_seq_clr(bmap
->div
[d
-(total
-bmap
->n_div
)], 2+total
);
1730 for (d
= pos
+ n
- 1; d
>= 0 && d
>= pos
; --d
) {
1731 int n_lower
, n_upper
;
1734 for (i
= 0; i
< bmap
->n_eq
; ++i
) {
1735 if (isl_int_is_zero(bmap
->eq
[i
][1+d
]))
1737 eliminate_var_using_equality(bmap
, d
, bmap
->eq
[i
], 0, NULL
);
1738 isl_basic_map_drop_equality(bmap
, i
);
1746 for (i
= 0; i
< bmap
->n_ineq
; ++i
) {
1747 if (isl_int_is_pos(bmap
->ineq
[i
][1+d
]))
1749 else if (isl_int_is_neg(bmap
->ineq
[i
][1+d
]))
1752 bmap
= isl_basic_map_extend_constraints(bmap
,
1753 0, n_lower
* n_upper
);
1756 for (i
= bmap
->n_ineq
- 1; i
>= 0; --i
) {
1758 if (isl_int_is_zero(bmap
->ineq
[i
][1+d
]))
1761 for (j
= 0; j
< i
; ++j
) {
1762 if (isl_int_is_zero(bmap
->ineq
[j
][1+d
]))
1765 if (isl_int_sgn(bmap
->ineq
[i
][1+d
]) ==
1766 isl_int_sgn(bmap
->ineq
[j
][1+d
]))
1768 k
= isl_basic_map_alloc_inequality(bmap
);
1771 isl_seq_cpy(bmap
->ineq
[k
], bmap
->ineq
[i
],
1773 isl_seq_elim(bmap
->ineq
[k
], bmap
->ineq
[j
],
1774 1+d
, 1+total
, NULL
);
1776 isl_basic_map_drop_inequality(bmap
, i
);
1779 if (n_lower
> 0 && n_upper
> 0) {
1780 bmap
= isl_basic_map_normalize_constraints(bmap
);
1781 bmap
= isl_basic_map_remove_duplicate_constraints(bmap
,
1783 bmap
= isl_basic_map_gauss(bmap
, NULL
);
1784 bmap
= isl_basic_map_remove_redundancies(bmap
);
1788 if (ISL_F_ISSET(bmap
, ISL_BASIC_MAP_EMPTY
))
1792 ISL_F_CLR(bmap
, ISL_BASIC_MAP_NORMALIZED
);
1794 bmap
= isl_basic_map_gauss(bmap
, NULL
);
1797 isl_basic_map_free(bmap
);
1801 struct isl_basic_set
*isl_basic_set_eliminate_vars(
1802 struct isl_basic_set
*bset
, unsigned pos
, unsigned n
)
1804 return bset_from_bmap(isl_basic_map_eliminate_vars(bset_to_bmap(bset
),
1808 /* Eliminate the specified n dimensions starting at first from the
1809 * constraints, without removing the dimensions from the space.
1810 * If the set is rational, the dimensions are eliminated using Fourier-Motzkin.
1811 * Otherwise, they are projected out and the original space is restored.
1813 __isl_give isl_basic_map
*isl_basic_map_eliminate(
1814 __isl_take isl_basic_map
*bmap
,
1815 enum isl_dim_type type
, unsigned first
, unsigned n
)
1824 if (first
+ n
> isl_basic_map_dim(bmap
, type
) || first
+ n
< first
)
1825 isl_die(bmap
->ctx
, isl_error_invalid
,
1826 "index out of bounds", goto error
);
1828 if (ISL_F_ISSET(bmap
, ISL_BASIC_MAP_RATIONAL
)) {
1829 first
+= isl_basic_map_offset(bmap
, type
) - 1;
1830 bmap
= isl_basic_map_eliminate_vars(bmap
, first
, n
);
1831 return isl_basic_map_finalize(bmap
);
1834 space
= isl_basic_map_get_space(bmap
);
1835 bmap
= isl_basic_map_project_out(bmap
, type
, first
, n
);
1836 bmap
= isl_basic_map_insert_dims(bmap
, type
, first
, n
);
1837 bmap
= isl_basic_map_reset_space(bmap
, space
);
1840 isl_basic_map_free(bmap
);
1844 __isl_give isl_basic_set
*isl_basic_set_eliminate(
1845 __isl_take isl_basic_set
*bset
,
1846 enum isl_dim_type type
, unsigned first
, unsigned n
)
1848 return isl_basic_map_eliminate(bset
, type
, first
, n
);
1851 /* Remove all constraints from "bmap" that reference any unknown local
1852 * variables (directly or indirectly).
1854 * Dropping all constraints on a local variable will make it redundant,
1855 * so it will get removed implicitly by
1856 * isl_basic_map_drop_constraints_involving_dims. Some other local
1857 * variables may also end up becoming redundant if they only appear
1858 * in constraints together with the unknown local variable.
1859 * Therefore, start over after calling
1860 * isl_basic_map_drop_constraints_involving_dims.
1862 __isl_give isl_basic_map
*isl_basic_map_drop_constraint_involving_unknown_divs(
1863 __isl_take isl_basic_map
*bmap
)
1866 int i
, n_div
, o_div
;
1868 known
= isl_basic_map_divs_known(bmap
);
1870 return isl_basic_map_free(bmap
);
1874 n_div
= isl_basic_map_dim(bmap
, isl_dim_div
);
1875 o_div
= isl_basic_map_offset(bmap
, isl_dim_div
) - 1;
1877 for (i
= 0; i
< n_div
; ++i
) {
1878 known
= isl_basic_map_div_is_known(bmap
, i
);
1880 return isl_basic_map_free(bmap
);
1883 bmap
= remove_dependent_vars(bmap
, o_div
+ i
);
1884 bmap
= isl_basic_map_drop_constraints_involving_dims(bmap
,
1888 n_div
= isl_basic_map_dim(bmap
, isl_dim_div
);
1895 /* Remove all constraints from "map" that reference any unknown local
1896 * variables (directly or indirectly).
1898 * Since constraints may get dropped from the basic maps,
1899 * they may no longer be disjoint from each other.
1901 __isl_give isl_map
*isl_map_drop_constraint_involving_unknown_divs(
1902 __isl_take isl_map
*map
)
1907 known
= isl_map_divs_known(map
);
1909 return isl_map_free(map
);
1913 map
= isl_map_cow(map
);
1917 for (i
= 0; i
< map
->n
; ++i
) {
1919 isl_basic_map_drop_constraint_involving_unknown_divs(
1922 return isl_map_free(map
);
1926 ISL_F_CLR(map
, ISL_MAP_DISJOINT
);
1931 /* Don't assume equalities are in order, because align_divs
1932 * may have changed the order of the divs.
1934 static void compute_elimination_index(struct isl_basic_map
*bmap
, int *elim
)
1939 total
= isl_space_dim(bmap
->dim
, isl_dim_all
);
1940 for (d
= 0; d
< total
; ++d
)
1942 for (i
= 0; i
< bmap
->n_eq
; ++i
) {
1943 for (d
= total
- 1; d
>= 0; --d
) {
1944 if (isl_int_is_zero(bmap
->eq
[i
][1+d
]))
1952 static void set_compute_elimination_index(struct isl_basic_set
*bset
, int *elim
)
1954 compute_elimination_index(bset_to_bmap(bset
), elim
);
1957 static int reduced_using_equalities(isl_int
*dst
, isl_int
*src
,
1958 struct isl_basic_map
*bmap
, int *elim
)
1964 total
= isl_space_dim(bmap
->dim
, isl_dim_all
);
1965 for (d
= total
- 1; d
>= 0; --d
) {
1966 if (isl_int_is_zero(src
[1+d
]))
1971 isl_seq_cpy(dst
, src
, 1 + total
);
1974 isl_seq_elim(dst
, bmap
->eq
[elim
[d
]], 1 + d
, 1 + total
, NULL
);
1979 static int set_reduced_using_equalities(isl_int
*dst
, isl_int
*src
,
1980 struct isl_basic_set
*bset
, int *elim
)
1982 return reduced_using_equalities(dst
, src
,
1983 bset_to_bmap(bset
), elim
);
1986 static struct isl_basic_set
*isl_basic_set_reduce_using_equalities(
1987 struct isl_basic_set
*bset
, struct isl_basic_set
*context
)
1992 if (!bset
|| !context
)
1995 if (context
->n_eq
== 0) {
1996 isl_basic_set_free(context
);
2000 bset
= isl_basic_set_cow(bset
);
2004 elim
= isl_alloc_array(bset
->ctx
, int, isl_basic_set_n_dim(bset
));
2007 set_compute_elimination_index(context
, elim
);
2008 for (i
= 0; i
< bset
->n_eq
; ++i
)
2009 set_reduced_using_equalities(bset
->eq
[i
], bset
->eq
[i
],
2011 for (i
= 0; i
< bset
->n_ineq
; ++i
)
2012 set_reduced_using_equalities(bset
->ineq
[i
], bset
->ineq
[i
],
2014 isl_basic_set_free(context
);
2016 bset
= isl_basic_set_simplify(bset
);
2017 bset
= isl_basic_set_finalize(bset
);
2020 isl_basic_set_free(bset
);
2021 isl_basic_set_free(context
);
2025 /* For each inequality in "ineq" that is a shifted (more relaxed)
2026 * copy of an inequality in "context", mark the corresponding entry
2028 * If an inequality only has a non-negative constant term, then
2031 static isl_stat
mark_shifted_constraints(__isl_keep isl_mat
*ineq
,
2032 __isl_keep isl_basic_set
*context
, int *row
)
2034 struct isl_constraint_index ci
;
2039 if (!ineq
|| !context
)
2040 return isl_stat_error
;
2041 if (context
->n_ineq
== 0)
2043 if (setup_constraint_index(&ci
, context
) < 0)
2044 return isl_stat_error
;
2046 n_ineq
= isl_mat_rows(ineq
);
2047 total
= isl_mat_cols(ineq
) - 1;
2048 for (k
= 0; k
< n_ineq
; ++k
) {
2052 l
= isl_seq_first_non_zero(ineq
->row
[k
] + 1, total
);
2053 if (l
< 0 && isl_int_is_nonneg(ineq
->row
[k
][0])) {
2057 redundant
= constraint_index_is_redundant(&ci
, ineq
->row
[k
]);
2064 constraint_index_free(&ci
);
2067 constraint_index_free(&ci
);
2068 return isl_stat_error
;
2071 static struct isl_basic_set
*remove_shifted_constraints(
2072 struct isl_basic_set
*bset
, struct isl_basic_set
*context
)
2074 struct isl_constraint_index ci
;
2077 if (!bset
|| !context
)
2080 if (context
->n_ineq
== 0)
2082 if (setup_constraint_index(&ci
, context
) < 0)
2085 for (k
= 0; k
< bset
->n_ineq
; ++k
) {
2088 redundant
= constraint_index_is_redundant(&ci
, bset
->ineq
[k
]);
2093 bset
= isl_basic_set_cow(bset
);
2096 isl_basic_set_drop_inequality(bset
, k
);
2099 constraint_index_free(&ci
);
2102 constraint_index_free(&ci
);
2106 /* Remove constraints from "bmap" that are identical to constraints
2107 * in "context" or that are more relaxed (greater constant term).
2109 * We perform the test for shifted copies on the pure constraints
2110 * in remove_shifted_constraints.
2112 static __isl_give isl_basic_map
*isl_basic_map_remove_shifted_constraints(
2113 __isl_take isl_basic_map
*bmap
, __isl_take isl_basic_map
*context
)
2115 isl_basic_set
*bset
, *bset_context
;
2117 if (!bmap
|| !context
)
2120 if (bmap
->n_ineq
== 0 || context
->n_ineq
== 0) {
2121 isl_basic_map_free(context
);
2125 context
= isl_basic_map_align_divs(context
, bmap
);
2126 bmap
= isl_basic_map_align_divs(bmap
, context
);
2128 bset
= isl_basic_map_underlying_set(isl_basic_map_copy(bmap
));
2129 bset_context
= isl_basic_map_underlying_set(context
);
2130 bset
= remove_shifted_constraints(bset
, bset_context
);
2131 isl_basic_set_free(bset_context
);
2133 bmap
= isl_basic_map_overlying_set(bset
, bmap
);
2137 isl_basic_map_free(bmap
);
2138 isl_basic_map_free(context
);
2142 /* Does the (linear part of a) constraint "c" involve any of the "len"
2143 * "relevant" dimensions?
2145 static int is_related(isl_int
*c
, int len
, int *relevant
)
2149 for (i
= 0; i
< len
; ++i
) {
2152 if (!isl_int_is_zero(c
[i
]))
2159 /* Drop constraints from "bmap" that do not involve any of
2160 * the dimensions marked "relevant".
2162 static __isl_give isl_basic_map
*drop_unrelated_constraints(
2163 __isl_take isl_basic_map
*bmap
, int *relevant
)
2167 dim
= isl_basic_map_dim(bmap
, isl_dim_all
);
2168 for (i
= 0; i
< dim
; ++i
)
2174 for (i
= bmap
->n_eq
- 1; i
>= 0; --i
)
2175 if (!is_related(bmap
->eq
[i
] + 1, dim
, relevant
)) {
2176 bmap
= isl_basic_map_cow(bmap
);
2177 if (isl_basic_map_drop_equality(bmap
, i
) < 0)
2178 return isl_basic_map_free(bmap
);
2181 for (i
= bmap
->n_ineq
- 1; i
>= 0; --i
)
2182 if (!is_related(bmap
->ineq
[i
] + 1, dim
, relevant
)) {
2183 bmap
= isl_basic_map_cow(bmap
);
2184 if (isl_basic_map_drop_inequality(bmap
, i
) < 0)
2185 return isl_basic_map_free(bmap
);
2191 /* Update the groups in "group" based on the (linear part of a) constraint "c".
2193 * In particular, for any variable involved in the constraint,
2194 * find the actual group id from before and replace the group
2195 * of the corresponding variable by the minimal group of all
2196 * the variables involved in the constraint considered so far
2197 * (if this minimum is smaller) or replace the minimum by this group
2198 * (if the minimum is larger).
2200 * At the end, all the variables in "c" will (indirectly) point
2201 * to the minimal of the groups that they referred to originally.
2203 static void update_groups(int dim
, int *group
, isl_int
*c
)
2208 for (j
= 0; j
< dim
; ++j
) {
2209 if (isl_int_is_zero(c
[j
]))
2211 while (group
[j
] >= 0 && group
[group
[j
]] != group
[j
])
2212 group
[j
] = group
[group
[j
]];
2213 if (group
[j
] == min
)
2215 if (group
[j
] < min
) {
2216 if (min
>= 0 && min
< dim
)
2217 group
[min
] = group
[j
];
2220 group
[group
[j
]] = min
;
2224 /* Allocate an array of groups of variables, one for each variable
2225 * in "context", initialized to zero.
2227 static int *alloc_groups(__isl_keep isl_basic_set
*context
)
2232 dim
= isl_basic_set_dim(context
, isl_dim_set
);
2233 ctx
= isl_basic_set_get_ctx(context
);
2234 return isl_calloc_array(ctx
, int, dim
);
2237 /* Drop constraints from "bmap" that only involve variables that are
2238 * not related to any of the variables marked with a "-1" in "group".
2240 * We construct groups of variables that collect variables that
2241 * (indirectly) appear in some common constraint of "bmap".
2242 * Each group is identified by the first variable in the group,
2243 * except for the special group of variables that was already identified
2244 * in the input as -1 (or are related to those variables).
2245 * If group[i] is equal to i (or -1), then the group of i is i (or -1),
2246 * otherwise the group of i is the group of group[i].
2248 * We first initialize groups for the remaining variables.
2249 * Then we iterate over the constraints of "bmap" and update the
2250 * group of the variables in the constraint by the smallest group.
2251 * Finally, we resolve indirect references to groups by running over
2254 * After computing the groups, we drop constraints that do not involve
2255 * any variables in the -1 group.
2257 __isl_give isl_basic_map
*isl_basic_map_drop_unrelated_constraints(
2258 __isl_take isl_basic_map
*bmap
, __isl_take
int *group
)
2267 dim
= isl_basic_map_dim(bmap
, isl_dim_all
);
2270 for (i
= 0; i
< dim
; ++i
)
2272 last
= group
[i
] = i
;
2278 for (i
= 0; i
< bmap
->n_eq
; ++i
)
2279 update_groups(dim
, group
, bmap
->eq
[i
] + 1);
2280 for (i
= 0; i
< bmap
->n_ineq
; ++i
)
2281 update_groups(dim
, group
, bmap
->ineq
[i
] + 1);
2283 for (i
= 0; i
< dim
; ++i
)
2285 group
[i
] = group
[group
[i
]];
2287 for (i
= 0; i
< dim
; ++i
)
2288 group
[i
] = group
[i
] == -1;
2290 bmap
= drop_unrelated_constraints(bmap
, group
);
2296 /* Drop constraints from "context" that are irrelevant for computing
2297 * the gist of "bset".
2299 * In particular, drop constraints in variables that are not related
2300 * to any of the variables involved in the constraints of "bset"
2301 * in the sense that there is no sequence of constraints that connects them.
2303 * We first mark all variables that appear in "bset" as belonging
2304 * to a "-1" group and then continue with group_and_drop_irrelevant_constraints.
2306 static __isl_give isl_basic_set
*drop_irrelevant_constraints(
2307 __isl_take isl_basic_set
*context
, __isl_keep isl_basic_set
*bset
)
2313 if (!context
|| !bset
)
2314 return isl_basic_set_free(context
);
2316 group
= alloc_groups(context
);
2319 return isl_basic_set_free(context
);
2321 dim
= isl_basic_set_dim(bset
, isl_dim_set
);
2322 for (i
= 0; i
< dim
; ++i
) {
2323 for (j
= 0; j
< bset
->n_eq
; ++j
)
2324 if (!isl_int_is_zero(bset
->eq
[j
][1 + i
]))
2326 if (j
< bset
->n_eq
) {
2330 for (j
= 0; j
< bset
->n_ineq
; ++j
)
2331 if (!isl_int_is_zero(bset
->ineq
[j
][1 + i
]))
2333 if (j
< bset
->n_ineq
)
2337 return isl_basic_map_drop_unrelated_constraints(context
, group
);
2340 /* Drop constraints from "context" that are irrelevant for computing
2341 * the gist of the inequalities "ineq".
2342 * Inequalities in "ineq" for which the corresponding element of row
2343 * is set to -1 have already been marked for removal and should be ignored.
2345 * In particular, drop constraints in variables that are not related
2346 * to any of the variables involved in "ineq"
2347 * in the sense that there is no sequence of constraints that connects them.
2349 * We first mark all variables that appear in "bset" as belonging
2350 * to a "-1" group and then continue with group_and_drop_irrelevant_constraints.
2352 static __isl_give isl_basic_set
*drop_irrelevant_constraints_marked(
2353 __isl_take isl_basic_set
*context
, __isl_keep isl_mat
*ineq
, int *row
)
2359 if (!context
|| !ineq
)
2360 return isl_basic_set_free(context
);
2362 group
= alloc_groups(context
);
2365 return isl_basic_set_free(context
);
2367 dim
= isl_basic_set_dim(context
, isl_dim_set
);
2368 n
= isl_mat_rows(ineq
);
2369 for (i
= 0; i
< dim
; ++i
) {
2370 for (j
= 0; j
< n
; ++j
) {
2373 if (!isl_int_is_zero(ineq
->row
[j
][1 + i
]))
2380 return isl_basic_map_drop_unrelated_constraints(context
, group
);
2383 /* Do all "n" entries of "row" contain a negative value?
2385 static int all_neg(int *row
, int n
)
2389 for (i
= 0; i
< n
; ++i
)
2396 /* Update the inequalities in "bset" based on the information in "row"
2399 * In particular, the array "row" contains either -1, meaning that
2400 * the corresponding inequality of "bset" is redundant, or the index
2401 * of an inequality in "tab".
2403 * If the row entry is -1, then drop the inequality.
2404 * Otherwise, if the constraint is marked redundant in the tableau,
2405 * then drop the inequality. Similarly, if it is marked as an equality
2406 * in the tableau, then turn the inequality into an equality and
2407 * perform Gaussian elimination.
2409 static __isl_give isl_basic_set
*update_ineq(__isl_take isl_basic_set
*bset
,
2410 __isl_keep
int *row
, struct isl_tab
*tab
)
2415 int found_equality
= 0;
2419 if (tab
&& tab
->empty
)
2420 return isl_basic_set_set_to_empty(bset
);
2422 n_ineq
= bset
->n_ineq
;
2423 for (i
= n_ineq
- 1; i
>= 0; --i
) {
2425 if (isl_basic_set_drop_inequality(bset
, i
) < 0)
2426 return isl_basic_set_free(bset
);
2432 if (isl_tab_is_equality(tab
, n_eq
+ row
[i
])) {
2433 isl_basic_map_inequality_to_equality(bset
, i
);
2435 } else if (isl_tab_is_redundant(tab
, n_eq
+ row
[i
])) {
2436 if (isl_basic_set_drop_inequality(bset
, i
) < 0)
2437 return isl_basic_set_free(bset
);
2442 bset
= isl_basic_set_gauss(bset
, NULL
);
2443 bset
= isl_basic_set_finalize(bset
);
2447 /* Update the inequalities in "bset" based on the information in "row"
2448 * and "tab" and free all arguments (other than "bset").
2450 static __isl_give isl_basic_set
*update_ineq_free(
2451 __isl_take isl_basic_set
*bset
, __isl_take isl_mat
*ineq
,
2452 __isl_take isl_basic_set
*context
, __isl_take
int *row
,
2453 struct isl_tab
*tab
)
2456 isl_basic_set_free(context
);
2458 bset
= update_ineq(bset
, row
, tab
);
2465 /* Remove all information from bset that is redundant in the context
2467 * "ineq" contains the (possibly transformed) inequalities of "bset",
2468 * in the same order.
2469 * The (explicit) equalities of "bset" are assumed to have been taken
2470 * into account by the transformation such that only the inequalities
2472 * "context" is assumed not to be empty.
2474 * "row" keeps track of the constraint index of a "bset" inequality in "tab".
2475 * A value of -1 means that the inequality is obviously redundant and may
2476 * not even appear in "tab".
2478 * We first mark the inequalities of "bset"
2479 * that are obviously redundant with respect to some inequality in "context".
2480 * Then we remove those constraints from "context" that have become
2481 * irrelevant for computing the gist of "bset".
2482 * Note that this removal of constraints cannot be replaced by
2483 * a factorization because factors in "bset" may still be connected
2484 * to each other through constraints in "context".
2486 * If there are any inequalities left, we construct a tableau for
2487 * the context and then add the inequalities of "bset".
2488 * Before adding these inequalities, we freeze all constraints such that
2489 * they won't be considered redundant in terms of the constraints of "bset".
2490 * Then we detect all redundant constraints (among the
2491 * constraints that weren't frozen), first by checking for redundancy in the
2492 * the tableau and then by checking if replacing a constraint by its negation
2493 * would lead to an empty set. This last step is fairly expensive
2494 * and could be optimized by more reuse of the tableau.
2495 * Finally, we update bset according to the results.
2497 static __isl_give isl_basic_set
*uset_gist_full(__isl_take isl_basic_set
*bset
,
2498 __isl_take isl_mat
*ineq
, __isl_take isl_basic_set
*context
)
2503 isl_basic_set
*combined
= NULL
;
2504 struct isl_tab
*tab
= NULL
;
2505 unsigned n_eq
, context_ineq
;
2508 if (!bset
|| !ineq
|| !context
)
2511 if (bset
->n_ineq
== 0 || isl_basic_set_plain_is_universe(context
)) {
2512 isl_basic_set_free(context
);
2517 ctx
= isl_basic_set_get_ctx(context
);
2518 row
= isl_calloc_array(ctx
, int, bset
->n_ineq
);
2522 if (mark_shifted_constraints(ineq
, context
, row
) < 0)
2524 if (all_neg(row
, bset
->n_ineq
))
2525 return update_ineq_free(bset
, ineq
, context
, row
, NULL
);
2527 context
= drop_irrelevant_constraints_marked(context
, ineq
, row
);
2530 if (isl_basic_set_plain_is_universe(context
))
2531 return update_ineq_free(bset
, ineq
, context
, row
, NULL
);
2533 n_eq
= context
->n_eq
;
2534 context_ineq
= context
->n_ineq
;
2535 combined
= isl_basic_set_cow(isl_basic_set_copy(context
));
2536 combined
= isl_basic_set_extend_constraints(combined
, 0, bset
->n_ineq
);
2537 tab
= isl_tab_from_basic_set(combined
, 0);
2538 for (i
= 0; i
< context_ineq
; ++i
)
2539 if (isl_tab_freeze_constraint(tab
, n_eq
+ i
) < 0)
2541 if (isl_tab_extend_cons(tab
, bset
->n_ineq
) < 0)
2544 for (i
= 0; i
< bset
->n_ineq
; ++i
) {
2547 combined
= isl_basic_set_add_ineq(combined
, ineq
->row
[i
]);
2548 if (isl_tab_add_ineq(tab
, ineq
->row
[i
]) < 0)
2552 if (isl_tab_detect_implicit_equalities(tab
) < 0)
2554 if (isl_tab_detect_redundant(tab
) < 0)
2556 total
= isl_basic_set_total_dim(bset
);
2557 for (i
= bset
->n_ineq
- 1; i
>= 0; --i
) {
2558 isl_basic_set
*test
;
2564 if (tab
->con
[n_eq
+ r
].is_redundant
)
2566 test
= isl_basic_set_dup(combined
);
2567 if (isl_inequality_negate(test
, r
) < 0)
2568 test
= isl_basic_set_free(test
);
2569 test
= isl_basic_set_update_from_tab(test
, tab
);
2570 is_empty
= isl_basic_set_is_empty(test
);
2571 isl_basic_set_free(test
);
2575 tab
->con
[n_eq
+ r
].is_redundant
= 1;
2577 bset
= update_ineq_free(bset
, ineq
, context
, row
, tab
);
2579 ISL_F_SET(bset
, ISL_BASIC_SET_NO_IMPLICIT
);
2580 ISL_F_SET(bset
, ISL_BASIC_SET_NO_REDUNDANT
);
2583 isl_basic_set_free(combined
);
2589 isl_basic_set_free(combined
);
2590 isl_basic_set_free(context
);
2591 isl_basic_set_free(bset
);
2595 /* Extract the inequalities of "bset" as an isl_mat.
2597 static __isl_give isl_mat
*extract_ineq(__isl_keep isl_basic_set
*bset
)
2606 ctx
= isl_basic_set_get_ctx(bset
);
2607 total
= isl_basic_set_total_dim(bset
);
2608 ineq
= isl_mat_sub_alloc6(ctx
, bset
->ineq
, 0, bset
->n_ineq
,
2614 /* Remove all information from "bset" that is redundant in the context
2615 * of "context", for the case where both "bset" and "context" are
2618 static __isl_give isl_basic_set
*uset_gist_uncompressed(
2619 __isl_take isl_basic_set
*bset
, __isl_take isl_basic_set
*context
)
2623 ineq
= extract_ineq(bset
);
2624 return uset_gist_full(bset
, ineq
, context
);
2627 /* Remove all information from "bset" that is redundant in the context
2628 * of "context", for the case where the combined equalities of
2629 * "bset" and "context" allow for a compression that can be obtained
2630 * by preapplication of "T".
2632 * "bset" itself is not transformed by "T". Instead, the inequalities
2633 * are extracted from "bset" and those are transformed by "T".
2634 * uset_gist_full then determines which of the transformed inequalities
2635 * are redundant with respect to the transformed "context" and removes
2636 * the corresponding inequalities from "bset".
2638 * After preapplying "T" to the inequalities, any common factor is
2639 * removed from the coefficients. If this results in a tightening
2640 * of the constant term, then the same tightening is applied to
2641 * the corresponding untransformed inequality in "bset".
2642 * That is, if after plugging in T, a constraint f(x) >= 0 is of the form
2646 * with 0 <= r < g, then it is equivalent to
2650 * This means that f(x) >= 0 is equivalent to f(x) - r >= 0 in the affine
2651 * subspace compressed by T since the latter would be transformed to
2655 static __isl_give isl_basic_set
*uset_gist_compressed(
2656 __isl_take isl_basic_set
*bset
, __isl_take isl_basic_set
*context
,
2657 __isl_take isl_mat
*T
)
2661 int i
, n_row
, n_col
;
2664 ineq
= extract_ineq(bset
);
2665 ineq
= isl_mat_product(ineq
, isl_mat_copy(T
));
2666 context
= isl_basic_set_preimage(context
, T
);
2668 if (!ineq
|| !context
)
2670 if (isl_basic_set_plain_is_empty(context
)) {
2672 isl_basic_set_free(context
);
2673 return isl_basic_set_set_to_empty(bset
);
2676 ctx
= isl_mat_get_ctx(ineq
);
2677 n_row
= isl_mat_rows(ineq
);
2678 n_col
= isl_mat_cols(ineq
);
2680 for (i
= 0; i
< n_row
; ++i
) {
2681 isl_seq_gcd(ineq
->row
[i
] + 1, n_col
- 1, &ctx
->normalize_gcd
);
2682 if (isl_int_is_zero(ctx
->normalize_gcd
))
2684 if (isl_int_is_one(ctx
->normalize_gcd
))
2686 isl_seq_scale_down(ineq
->row
[i
] + 1, ineq
->row
[i
] + 1,
2687 ctx
->normalize_gcd
, n_col
- 1);
2688 isl_int_fdiv_r(rem
, ineq
->row
[i
][0], ctx
->normalize_gcd
);
2689 isl_int_fdiv_q(ineq
->row
[i
][0],
2690 ineq
->row
[i
][0], ctx
->normalize_gcd
);
2691 if (isl_int_is_zero(rem
))
2693 bset
= isl_basic_set_cow(bset
);
2696 isl_int_sub(bset
->ineq
[i
][0], bset
->ineq
[i
][0], rem
);
2700 return uset_gist_full(bset
, ineq
, context
);
2703 isl_basic_set_free(context
);
2704 isl_basic_set_free(bset
);
2708 /* Project "bset" onto the variables that are involved in "template".
2710 static __isl_give isl_basic_set
*project_onto_involved(
2711 __isl_take isl_basic_set
*bset
, __isl_keep isl_basic_set
*template)
2715 if (!bset
|| !template)
2716 return isl_basic_set_free(bset
);
2718 n
= isl_basic_set_dim(template, isl_dim_set
);
2720 for (i
= 0; i
< n
; ++i
) {
2723 involved
= isl_basic_set_involves_dims(template,
2726 return isl_basic_set_free(bset
);
2729 bset
= isl_basic_set_eliminate_vars(bset
, i
, 1);
2735 /* Remove all information from bset that is redundant in the context
2736 * of context. In particular, equalities that are linear combinations
2737 * of those in context are removed. Then the inequalities that are
2738 * redundant in the context of the equalities and inequalities of
2739 * context are removed.
2741 * First of all, we drop those constraints from "context"
2742 * that are irrelevant for computing the gist of "bset".
2743 * Alternatively, we could factorize the intersection of "context" and "bset".
2745 * We first compute the intersection of the integer affine hulls
2746 * of "bset" and "context",
2747 * compute the gist inside this intersection and then reduce
2748 * the constraints with respect to the equalities of the context
2749 * that only involve variables already involved in the input.
2751 * If two constraints are mutually redundant, then uset_gist_full
2752 * will remove the second of those constraints. We therefore first
2753 * sort the constraints so that constraints not involving existentially
2754 * quantified variables are given precedence over those that do.
2755 * We have to perform this sorting before the variable compression,
2756 * because that may effect the order of the variables.
2758 static __isl_give isl_basic_set
*uset_gist(__isl_take isl_basic_set
*bset
,
2759 __isl_take isl_basic_set
*context
)
2764 isl_basic_set
*aff_context
;
2767 if (!bset
|| !context
)
2770 context
= drop_irrelevant_constraints(context
, bset
);
2772 bset
= isl_basic_set_detect_equalities(bset
);
2773 aff
= isl_basic_set_copy(bset
);
2774 aff
= isl_basic_set_plain_affine_hull(aff
);
2775 context
= isl_basic_set_detect_equalities(context
);
2776 aff_context
= isl_basic_set_copy(context
);
2777 aff_context
= isl_basic_set_plain_affine_hull(aff_context
);
2778 aff
= isl_basic_set_intersect(aff
, aff_context
);
2781 if (isl_basic_set_plain_is_empty(aff
)) {
2782 isl_basic_set_free(bset
);
2783 isl_basic_set_free(context
);
2786 bset
= isl_basic_set_sort_constraints(bset
);
2787 if (aff
->n_eq
== 0) {
2788 isl_basic_set_free(aff
);
2789 return uset_gist_uncompressed(bset
, context
);
2791 total
= isl_basic_set_total_dim(bset
);
2792 eq
= isl_mat_sub_alloc6(bset
->ctx
, aff
->eq
, 0, aff
->n_eq
, 0, 1 + total
);
2793 eq
= isl_mat_cow(eq
);
2794 T
= isl_mat_variable_compression(eq
, NULL
);
2795 isl_basic_set_free(aff
);
2796 if (T
&& T
->n_col
== 0) {
2798 isl_basic_set_free(context
);
2799 return isl_basic_set_set_to_empty(bset
);
2802 aff_context
= isl_basic_set_affine_hull(isl_basic_set_copy(context
));
2803 aff_context
= project_onto_involved(aff_context
, bset
);
2805 bset
= uset_gist_compressed(bset
, context
, T
);
2806 bset
= isl_basic_set_reduce_using_equalities(bset
, aff_context
);
2809 ISL_F_SET(bset
, ISL_BASIC_SET_NO_IMPLICIT
);
2810 ISL_F_SET(bset
, ISL_BASIC_SET_NO_REDUNDANT
);
2815 isl_basic_set_free(bset
);
2816 isl_basic_set_free(context
);
2820 /* Return the number of equality constraints in "bmap" that involve
2821 * local variables. This function assumes that Gaussian elimination
2822 * has been applied to the equality constraints.
2824 static int n_div_eq(__isl_keep isl_basic_map
*bmap
)
2832 if (bmap
->n_eq
== 0)
2835 total
= isl_basic_map_dim(bmap
, isl_dim_all
);
2836 n_div
= isl_basic_map_dim(bmap
, isl_dim_div
);
2839 for (i
= 0; i
< bmap
->n_eq
; ++i
)
2840 if (isl_seq_first_non_zero(bmap
->eq
[i
] + 1 + total
,
2847 /* Construct a basic map in "space" defined by the equality constraints in "eq".
2848 * The constraints are assumed not to involve any local variables.
2850 static __isl_give isl_basic_map
*basic_map_from_equalities(
2851 __isl_take isl_space
*space
, __isl_take isl_mat
*eq
)
2854 isl_basic_map
*bmap
= NULL
;
2859 if (1 + isl_space_dim(space
, isl_dim_all
) != eq
->n_col
)
2860 isl_die(isl_space_get_ctx(space
), isl_error_internal
,
2861 "unexpected number of columns", goto error
);
2863 bmap
= isl_basic_map_alloc_space(isl_space_copy(space
),
2865 for (i
= 0; i
< eq
->n_row
; ++i
) {
2866 k
= isl_basic_map_alloc_equality(bmap
);
2869 isl_seq_cpy(bmap
->eq
[k
], eq
->row
[i
], eq
->n_col
);
2872 isl_space_free(space
);
2876 isl_space_free(space
);
2878 isl_basic_map_free(bmap
);
2882 /* Construct and return a variable compression based on the equality
2883 * constraints in "bmap1" and "bmap2" that do not involve the local variables.
2884 * "n1" is the number of (initial) equality constraints in "bmap1"
2885 * that do involve local variables.
2886 * "n2" is the number of (initial) equality constraints in "bmap2"
2887 * that do involve local variables.
2888 * "total" is the total number of other variables.
2889 * This function assumes that Gaussian elimination
2890 * has been applied to the equality constraints in both "bmap1" and "bmap2"
2891 * such that the equality constraints not involving local variables
2892 * are those that start at "n1" or "n2".
2894 * If either of "bmap1" and "bmap2" does not have such equality constraints,
2895 * then simply compute the compression based on the equality constraints
2896 * in the other basic map.
2897 * Otherwise, combine the equality constraints from both into a new
2898 * basic map such that Gaussian elimination can be applied to this combination
2899 * and then construct a variable compression from the resulting
2900 * equality constraints.
2902 static __isl_give isl_mat
*combined_variable_compression(
2903 __isl_keep isl_basic_map
*bmap1
, int n1
,
2904 __isl_keep isl_basic_map
*bmap2
, int n2
, int total
)
2907 isl_mat
*E1
, *E2
, *V
;
2908 isl_basic_map
*bmap
;
2910 ctx
= isl_basic_map_get_ctx(bmap1
);
2911 if (bmap1
->n_eq
== n1
) {
2912 E2
= isl_mat_sub_alloc6(ctx
, bmap2
->eq
,
2913 n2
, bmap2
->n_eq
- n2
, 0, 1 + total
);
2914 return isl_mat_variable_compression(E2
, NULL
);
2916 if (bmap2
->n_eq
== n2
) {
2917 E1
= isl_mat_sub_alloc6(ctx
, bmap1
->eq
,
2918 n1
, bmap1
->n_eq
- n1
, 0, 1 + total
);
2919 return isl_mat_variable_compression(E1
, NULL
);
2921 E1
= isl_mat_sub_alloc6(ctx
, bmap1
->eq
,
2922 n1
, bmap1
->n_eq
- n1
, 0, 1 + total
);
2923 E2
= isl_mat_sub_alloc6(ctx
, bmap2
->eq
,
2924 n2
, bmap2
->n_eq
- n2
, 0, 1 + total
);
2925 E1
= isl_mat_concat(E1
, E2
);
2926 bmap
= basic_map_from_equalities(isl_basic_map_get_space(bmap1
), E1
);
2927 bmap
= isl_basic_map_gauss(bmap
, NULL
);
2930 E1
= isl_mat_sub_alloc6(ctx
, bmap
->eq
, 0, bmap
->n_eq
, 0, 1 + total
);
2931 V
= isl_mat_variable_compression(E1
, NULL
);
2932 isl_basic_map_free(bmap
);
2937 /* Extract the stride constraints from "bmap", compressed
2938 * with respect to both the stride constraints in "context" and
2939 * the remaining equality constraints in both "bmap" and "context".
2940 * "bmap_n_eq" is the number of (initial) stride constraints in "bmap".
2941 * "context_n_eq" is the number of (initial) stride constraints in "context".
2943 * Let x be all variables in "bmap" (and "context") other than the local
2944 * variables. First compute a variable compression
2948 * based on the non-stride equality constraints in "bmap" and "context".
2949 * Consider the stride constraints of "context",
2953 * with y the local variables and plug in the variable compression,
2956 * A(V x') + B(y) = 0
2958 * Use these constraints to compute a parameter compression on x'
2962 * Now consider the stride constraints of "bmap"
2966 * and plug in x = V*T x''.
2967 * That is, return A = [C*V*T D].
2969 static __isl_give isl_mat
*extract_compressed_stride_constraints(
2970 __isl_keep isl_basic_map
*bmap
, int bmap_n_eq
,
2971 __isl_keep isl_basic_map
*context
, int context_n_eq
)
2975 isl_mat
*A
, *B
, *T
, *V
;
2977 total
= isl_basic_map_dim(context
, isl_dim_all
);
2978 n_div
= isl_basic_map_dim(context
, isl_dim_div
);
2981 ctx
= isl_basic_map_get_ctx(bmap
);
2983 V
= combined_variable_compression(bmap
, bmap_n_eq
,
2984 context
, context_n_eq
, total
);
2986 A
= isl_mat_sub_alloc6(ctx
, context
->eq
, 0, context_n_eq
, 0, 1 + total
);
2987 B
= isl_mat_sub_alloc6(ctx
, context
->eq
,
2988 0, context_n_eq
, 1 + total
, n_div
);
2989 A
= isl_mat_product(A
, isl_mat_copy(V
));
2990 T
= isl_mat_parameter_compression_ext(A
, B
);
2991 T
= isl_mat_product(V
, T
);
2993 n_div
= isl_basic_map_dim(bmap
, isl_dim_div
);
2994 T
= isl_mat_diagonal(T
, isl_mat_identity(ctx
, n_div
));
2996 A
= isl_mat_sub_alloc6(ctx
, bmap
->eq
,
2997 0, bmap_n_eq
, 0, 1 + total
+ n_div
);
2998 A
= isl_mat_product(A
, T
);
3003 /* Remove the prime factors from *g that have an exponent that
3004 * is strictly smaller than the exponent in "c".
3005 * All exponents in *g are known to be smaller than or equal
3008 * That is, if *g is equal to
3010 * p_1^{e_1} p_2^{e_2} ... p_n^{e_n}
3012 * and "c" is equal to
3014 * p_1^{f_1} p_2^{f_2} ... p_n^{f_n}
3018 * p_1^{e_1 * (e_1 = f_1)} p_2^{e_2 * (e_2 = f_2)} ...
3019 * p_n^{e_n * (e_n = f_n)}
3021 * If e_i = f_i, then c / *g does not have any p_i factors and therefore
3022 * neither does the gcd of *g and c / *g.
3023 * If e_i < f_i, then the gcd of *g and c / *g has a positive
3024 * power min(e_i, s_i) of p_i with s_i = f_i - e_i among its factors.
3025 * Dividing *g by this gcd therefore strictly reduces the exponent
3026 * of the prime factors that need to be removed, while leaving the
3027 * other prime factors untouched.
3028 * Repeating this process until gcd(*g, c / *g) = 1 therefore
3029 * removes all undesired factors, without removing any others.
3031 static void remove_incomplete_powers(isl_int
*g
, isl_int c
)
3037 isl_int_divexact(t
, c
, *g
);
3038 isl_int_gcd(t
, t
, *g
);
3039 if (isl_int_is_one(t
))
3041 isl_int_divexact(*g
, *g
, t
);
3046 /* Reduce the "n" stride constraints in "bmap" based on a copy "A"
3047 * of the same stride constraints in a compressed space that exploits
3048 * all equalities in the context and the other equalities in "bmap".
3050 * If the stride constraints of "bmap" are of the form
3054 * then A is of the form
3058 * If any of these constraints involves only a single local variable y,
3059 * then the constraint appears as
3069 * Let g be the gcd of m and the coefficients of h.
3070 * Then, in particular, g is a divisor of the coefficients of h and
3074 * is known to be a multiple of g.
3075 * If some prime factor in m appears with the same exponent in g,
3076 * then it can be removed from m because f(x) is already known
3077 * to be a multiple of g and therefore in particular of this power
3078 * of the prime factors.
3079 * Prime factors that appear with a smaller exponent in g cannot
3080 * be removed from m.
3081 * Let g' be the divisor of g containing all prime factors that
3082 * appear with the same exponent in m and g, then
3086 * can be replaced by
3088 * f(x) + m/g' y_i' = 0
3090 * Note that (if g' != 1) this changes the explicit representation
3091 * of y_i to that of y_i', so the integer division at position i
3092 * is marked unknown and later recomputed by a call to
3093 * isl_basic_map_gauss.
3095 static __isl_give isl_basic_map
*reduce_stride_constraints(
3096 __isl_take isl_basic_map
*bmap
, int n
, __isl_keep isl_mat
*A
)
3104 return isl_basic_map_free(bmap
);
3106 total
= isl_basic_map_dim(bmap
, isl_dim_all
);
3107 n_div
= isl_basic_map_dim(bmap
, isl_dim_div
);
3111 for (i
= 0; i
< n
; ++i
) {
3114 div
= isl_seq_first_non_zero(bmap
->eq
[i
] + 1 + total
, n_div
);
3116 isl_die(isl_basic_map_get_ctx(bmap
), isl_error_internal
,
3117 "equality constraints modified unexpectedly",
3119 if (isl_seq_first_non_zero(bmap
->eq
[i
] + 1 + total
+ div
+ 1,
3120 n_div
- div
- 1) != -1)
3122 if (isl_mat_row_gcd(A
, i
, &gcd
) < 0)
3124 if (isl_int_is_one(gcd
))
3126 remove_incomplete_powers(&gcd
, bmap
->eq
[i
][1 + total
+ div
]);
3127 if (isl_int_is_one(gcd
))
3129 isl_int_divexact(bmap
->eq
[i
][1 + total
+ div
],
3130 bmap
->eq
[i
][1 + total
+ div
], gcd
);
3131 bmap
= isl_basic_map_mark_div_unknown(bmap
, div
);
3139 bmap
= isl_basic_map_gauss(bmap
, NULL
);
3144 isl_basic_map_free(bmap
);
3148 /* Simplify the stride constraints in "bmap" based on
3149 * the remaining equality constraints in "bmap" and all equality
3150 * constraints in "context".
3151 * Only do this if both "bmap" and "context" have stride constraints.
3153 * First extract a copy of the stride constraints in "bmap" in a compressed
3154 * space exploiting all the other equality constraints and then
3155 * use this compressed copy to simplify the original stride constraints.
3157 static __isl_give isl_basic_map
*gist_strides(__isl_take isl_basic_map
*bmap
,
3158 __isl_keep isl_basic_map
*context
)
3160 int bmap_n_eq
, context_n_eq
;
3163 if (!bmap
|| !context
)
3164 return isl_basic_map_free(bmap
);
3166 bmap_n_eq
= n_div_eq(bmap
);
3167 context_n_eq
= n_div_eq(context
);
3169 if (bmap_n_eq
< 0 || context_n_eq
< 0)
3170 return isl_basic_map_free(bmap
);
3171 if (bmap_n_eq
== 0 || context_n_eq
== 0)
3174 A
= extract_compressed_stride_constraints(bmap
, bmap_n_eq
,
3175 context
, context_n_eq
);
3176 bmap
= reduce_stride_constraints(bmap
, bmap_n_eq
, A
);
3183 /* Return a basic map that has the same intersection with "context" as "bmap"
3184 * and that is as "simple" as possible.
3186 * The core computation is performed on the pure constraints.
3187 * When we add back the meaning of the integer divisions, we need
3188 * to (re)introduce the div constraints. If we happen to have
3189 * discovered that some of these integer divisions are equal to
3190 * some affine combination of other variables, then these div
3191 * constraints may end up getting simplified in terms of the equalities,
3192 * resulting in extra inequalities on the other variables that
3193 * may have been removed already or that may not even have been
3194 * part of the input. We try and remove those constraints of
3195 * this form that are most obviously redundant with respect to
3196 * the context. We also remove those div constraints that are
3197 * redundant with respect to the other constraints in the result.
3199 * The stride constraints among the equality constraints in "bmap" are
3200 * also simplified with respecting to the other equality constraints
3201 * in "bmap" and with respect to all equality constraints in "context".
3203 struct isl_basic_map
*isl_basic_map_gist(struct isl_basic_map
*bmap
,
3204 struct isl_basic_map
*context
)
3206 isl_basic_set
*bset
, *eq
;
3207 isl_basic_map
*eq_bmap
;
3208 unsigned total
, n_div
, extra
, n_eq
, n_ineq
;
3210 if (!bmap
|| !context
)
3213 if (isl_basic_map_plain_is_universe(bmap
)) {
3214 isl_basic_map_free(context
);
3217 if (isl_basic_map_plain_is_empty(context
)) {
3218 isl_space
*space
= isl_basic_map_get_space(bmap
);
3219 isl_basic_map_free(bmap
);
3220 isl_basic_map_free(context
);
3221 return isl_basic_map_universe(space
);
3223 if (isl_basic_map_plain_is_empty(bmap
)) {
3224 isl_basic_map_free(context
);
3228 bmap
= isl_basic_map_remove_redundancies(bmap
);
3229 context
= isl_basic_map_remove_redundancies(context
);
3233 context
= isl_basic_map_align_divs(context
, bmap
);
3234 n_div
= isl_basic_map_dim(context
, isl_dim_div
);
3235 total
= isl_basic_map_dim(bmap
, isl_dim_all
);
3236 extra
= n_div
- isl_basic_map_dim(bmap
, isl_dim_div
);
3238 bset
= isl_basic_map_underlying_set(isl_basic_map_copy(bmap
));
3239 bset
= isl_basic_set_add_dims(bset
, isl_dim_set
, extra
);
3240 bset
= uset_gist(bset
,
3241 isl_basic_map_underlying_set(isl_basic_map_copy(context
)));
3242 bset
= isl_basic_set_project_out(bset
, isl_dim_set
, total
, extra
);
3244 if (!bset
|| bset
->n_eq
== 0 || n_div
== 0 ||
3245 isl_basic_set_plain_is_empty(bset
)) {
3246 isl_basic_map_free(context
);
3247 return isl_basic_map_overlying_set(bset
, bmap
);
3251 n_ineq
= bset
->n_ineq
;
3252 eq
= isl_basic_set_copy(bset
);
3253 eq
= isl_basic_set_cow(eq
);
3254 if (isl_basic_set_free_inequality(eq
, n_ineq
) < 0)
3255 eq
= isl_basic_set_free(eq
);
3256 if (isl_basic_set_free_equality(bset
, n_eq
) < 0)
3257 bset
= isl_basic_set_free(bset
);
3259 eq_bmap
= isl_basic_map_overlying_set(eq
, isl_basic_map_copy(bmap
));
3260 eq_bmap
= gist_strides(eq_bmap
, context
);
3261 eq_bmap
= isl_basic_map_remove_shifted_constraints(eq_bmap
, context
);
3262 bmap
= isl_basic_map_overlying_set(bset
, bmap
);
3263 bmap
= isl_basic_map_intersect(bmap
, eq_bmap
);
3264 bmap
= isl_basic_map_remove_redundancies(bmap
);
3268 isl_basic_map_free(bmap
);
3269 isl_basic_map_free(context
);
3274 * Assumes context has no implicit divs.
3276 __isl_give isl_map
*isl_map_gist_basic_map(__isl_take isl_map
*map
,
3277 __isl_take isl_basic_map
*context
)
3281 if (!map
|| !context
)
3284 if (isl_basic_map_plain_is_empty(context
)) {
3285 isl_space
*space
= isl_map_get_space(map
);
3287 isl_basic_map_free(context
);
3288 return isl_map_universe(space
);
3291 context
= isl_basic_map_remove_redundancies(context
);
3292 map
= isl_map_cow(map
);
3293 if (!map
|| !context
)
3295 isl_assert(map
->ctx
, isl_space_is_equal(map
->dim
, context
->dim
), goto error
);
3296 map
= isl_map_compute_divs(map
);
3299 for (i
= map
->n
- 1; i
>= 0; --i
) {
3300 map
->p
[i
] = isl_basic_map_gist(map
->p
[i
],
3301 isl_basic_map_copy(context
));
3304 if (isl_basic_map_plain_is_empty(map
->p
[i
])) {
3305 isl_basic_map_free(map
->p
[i
]);
3306 if (i
!= map
->n
- 1)
3307 map
->p
[i
] = map
->p
[map
->n
- 1];
3311 isl_basic_map_free(context
);
3312 ISL_F_CLR(map
, ISL_MAP_NORMALIZED
);
3316 isl_basic_map_free(context
);
3320 /* Drop all inequalities from "bmap" that also appear in "context".
3321 * "context" is assumed to have only known local variables and
3322 * the initial local variables of "bmap" are assumed to be the same
3323 * as those of "context".
3324 * The constraints of both "bmap" and "context" are assumed
3325 * to have been sorted using isl_basic_map_sort_constraints.
3327 * Run through the inequality constraints of "bmap" and "context"
3329 * If a constraint of "bmap" involves variables not in "context",
3330 * then it cannot appear in "context".
3331 * If a matching constraint is found, it is removed from "bmap".
3333 static __isl_give isl_basic_map
*drop_inequalities(
3334 __isl_take isl_basic_map
*bmap
, __isl_keep isl_basic_map
*context
)
3337 unsigned total
, extra
;
3339 if (!bmap
|| !context
)
3340 return isl_basic_map_free(bmap
);
3342 total
= isl_basic_map_total_dim(context
);
3343 extra
= isl_basic_map_total_dim(bmap
) - total
;
3345 i1
= bmap
->n_ineq
- 1;
3346 i2
= context
->n_ineq
- 1;
3347 while (bmap
&& i1
>= 0 && i2
>= 0) {
3350 if (isl_seq_first_non_zero(bmap
->ineq
[i1
] + 1 + total
,
3355 cmp
= isl_basic_map_constraint_cmp(context
, bmap
->ineq
[i1
],
3365 if (isl_int_eq(bmap
->ineq
[i1
][0], context
->ineq
[i2
][0])) {
3366 bmap
= isl_basic_map_cow(bmap
);
3367 if (isl_basic_map_drop_inequality(bmap
, i1
) < 0)
3368 bmap
= isl_basic_map_free(bmap
);
3377 /* Drop all equalities from "bmap" that also appear in "context".
3378 * "context" is assumed to have only known local variables and
3379 * the initial local variables of "bmap" are assumed to be the same
3380 * as those of "context".
3382 * Run through the equality constraints of "bmap" and "context"
3384 * If a constraint of "bmap" involves variables not in "context",
3385 * then it cannot appear in "context".
3386 * If a matching constraint is found, it is removed from "bmap".
3388 static __isl_give isl_basic_map
*drop_equalities(
3389 __isl_take isl_basic_map
*bmap
, __isl_keep isl_basic_map
*context
)
3392 unsigned total
, extra
;
3394 if (!bmap
|| !context
)
3395 return isl_basic_map_free(bmap
);
3397 total
= isl_basic_map_total_dim(context
);
3398 extra
= isl_basic_map_total_dim(bmap
) - total
;
3400 i1
= bmap
->n_eq
- 1;
3401 i2
= context
->n_eq
- 1;
3403 while (bmap
&& i1
>= 0 && i2
>= 0) {
3406 if (isl_seq_first_non_zero(bmap
->eq
[i1
] + 1 + total
,
3409 last1
= isl_seq_last_non_zero(bmap
->eq
[i1
] + 1, total
);
3410 last2
= isl_seq_last_non_zero(context
->eq
[i2
] + 1, total
);
3411 if (last1
> last2
) {
3415 if (last1
< last2
) {
3419 if (isl_seq_eq(bmap
->eq
[i1
], context
->eq
[i2
], 1 + total
)) {
3420 bmap
= isl_basic_map_cow(bmap
);
3421 if (isl_basic_map_drop_equality(bmap
, i1
) < 0)
3422 bmap
= isl_basic_map_free(bmap
);
3431 /* Remove the constraints in "context" from "bmap".
3432 * "context" is assumed to have explicit representations
3433 * for all local variables.
3435 * First align the divs of "bmap" to those of "context" and
3436 * sort the constraints. Then drop all constraints from "bmap"
3437 * that appear in "context".
3439 __isl_give isl_basic_map
*isl_basic_map_plain_gist(
3440 __isl_take isl_basic_map
*bmap
, __isl_take isl_basic_map
*context
)
3442 isl_bool done
, known
;
3444 done
= isl_basic_map_plain_is_universe(context
);
3445 if (done
== isl_bool_false
)
3446 done
= isl_basic_map_plain_is_universe(bmap
);
3447 if (done
== isl_bool_false
)
3448 done
= isl_basic_map_plain_is_empty(context
);
3449 if (done
== isl_bool_false
)
3450 done
= isl_basic_map_plain_is_empty(bmap
);
3454 isl_basic_map_free(context
);
3457 known
= isl_basic_map_divs_known(context
);
3461 isl_die(isl_basic_map_get_ctx(bmap
), isl_error_invalid
,
3462 "context has unknown divs", goto error
);
3464 bmap
= isl_basic_map_align_divs(bmap
, context
);
3465 bmap
= isl_basic_map_gauss(bmap
, NULL
);
3466 bmap
= isl_basic_map_sort_constraints(bmap
);
3467 context
= isl_basic_map_sort_constraints(context
);
3469 bmap
= drop_inequalities(bmap
, context
);
3470 bmap
= drop_equalities(bmap
, context
);
3472 isl_basic_map_free(context
);
3473 bmap
= isl_basic_map_finalize(bmap
);
3476 isl_basic_map_free(bmap
);
3477 isl_basic_map_free(context
);
3481 /* Replace "map" by the disjunct at position "pos" and free "context".
3483 static __isl_give isl_map
*replace_by_disjunct(__isl_take isl_map
*map
,
3484 int pos
, __isl_take isl_basic_map
*context
)
3486 isl_basic_map
*bmap
;
3488 bmap
= isl_basic_map_copy(map
->p
[pos
]);
3490 isl_basic_map_free(context
);
3491 return isl_map_from_basic_map(bmap
);
3494 /* Remove the constraints in "context" from "map".
3495 * If any of the disjuncts in the result turns out to be the universe,
3496 * then return this universe.
3497 * "context" is assumed to have explicit representations
3498 * for all local variables.
3500 __isl_give isl_map
*isl_map_plain_gist_basic_map(__isl_take isl_map
*map
,
3501 __isl_take isl_basic_map
*context
)
3504 isl_bool univ
, known
;
3506 univ
= isl_basic_map_plain_is_universe(context
);
3510 isl_basic_map_free(context
);
3513 known
= isl_basic_map_divs_known(context
);
3517 isl_die(isl_map_get_ctx(map
), isl_error_invalid
,
3518 "context has unknown divs", goto error
);
3520 map
= isl_map_cow(map
);
3523 for (i
= 0; i
< map
->n
; ++i
) {
3524 map
->p
[i
] = isl_basic_map_plain_gist(map
->p
[i
],
3525 isl_basic_map_copy(context
));
3526 univ
= isl_basic_map_plain_is_universe(map
->p
[i
]);
3529 if (univ
&& map
->n
> 1)
3530 return replace_by_disjunct(map
, i
, context
);
3533 isl_basic_map_free(context
);
3534 ISL_F_CLR(map
, ISL_MAP_NORMALIZED
);
3536 ISL_F_CLR(map
, ISL_MAP_DISJOINT
);
3540 isl_basic_map_free(context
);
3544 /* Replace "map" by a universe map in the same space and free "drop".
3546 static __isl_give isl_map
*replace_by_universe(__isl_take isl_map
*map
,
3547 __isl_take isl_map
*drop
)
3551 res
= isl_map_universe(isl_map_get_space(map
));
3557 /* Return a map that has the same intersection with "context" as "map"
3558 * and that is as "simple" as possible.
3560 * If "map" is already the universe, then we cannot make it any simpler.
3561 * Similarly, if "context" is the universe, then we cannot exploit it
3563 * If "map" and "context" are identical to each other, then we can
3564 * return the corresponding universe.
3566 * If either "map" or "context" consists of multiple disjuncts,
3567 * then check if "context" happens to be a subset of "map",
3568 * in which case all constraints can be removed.
3569 * In case of multiple disjuncts, the standard procedure
3570 * may not be able to detect that all constraints can be removed.
3572 * If none of these cases apply, we have to work a bit harder.
3573 * During this computation, we make use of a single disjunct context,
3574 * so if the original context consists of more than one disjunct
3575 * then we need to approximate the context by a single disjunct set.
3576 * Simply taking the simple hull may drop constraints that are
3577 * only implicitly available in each disjunct. We therefore also
3578 * look for constraints among those defining "map" that are valid
3579 * for the context. These can then be used to simplify away
3580 * the corresponding constraints in "map".
3582 static __isl_give isl_map
*map_gist(__isl_take isl_map
*map
,
3583 __isl_take isl_map
*context
)
3587 int single_disjunct_map
, single_disjunct_context
;
3589 isl_basic_map
*hull
;
3591 is_universe
= isl_map_plain_is_universe(map
);
3592 if (is_universe
>= 0 && !is_universe
)
3593 is_universe
= isl_map_plain_is_universe(context
);
3594 if (is_universe
< 0)
3597 isl_map_free(context
);
3601 equal
= isl_map_plain_is_equal(map
, context
);
3605 return replace_by_universe(map
, context
);
3607 single_disjunct_map
= isl_map_n_basic_map(map
) == 1;
3608 single_disjunct_context
= isl_map_n_basic_map(context
) == 1;
3609 if (!single_disjunct_map
|| !single_disjunct_context
) {
3610 subset
= isl_map_is_subset(context
, map
);
3614 return replace_by_universe(map
, context
);
3617 context
= isl_map_compute_divs(context
);
3620 if (single_disjunct_context
) {
3621 hull
= isl_map_simple_hull(context
);
3626 ctx
= isl_map_get_ctx(map
);
3627 list
= isl_map_list_alloc(ctx
, 2);
3628 list
= isl_map_list_add(list
, isl_map_copy(context
));
3629 list
= isl_map_list_add(list
, isl_map_copy(map
));
3630 hull
= isl_map_unshifted_simple_hull_from_map_list(context
,
3633 return isl_map_gist_basic_map(map
, hull
);
3636 isl_map_free(context
);
3640 __isl_give isl_map
*isl_map_gist(__isl_take isl_map
*map
,
3641 __isl_take isl_map
*context
)
3643 return isl_map_align_params_map_map_and(map
, context
, &map_gist
);
3646 struct isl_basic_set
*isl_basic_set_gist(struct isl_basic_set
*bset
,
3647 struct isl_basic_set
*context
)
3649 return bset_from_bmap(isl_basic_map_gist(bset_to_bmap(bset
),
3650 bset_to_bmap(context
)));
3653 __isl_give isl_set
*isl_set_gist_basic_set(__isl_take isl_set
*set
,
3654 __isl_take isl_basic_set
*context
)
3656 return (struct isl_set
*)isl_map_gist_basic_map(set_to_map(set
),
3657 bset_to_bmap(context
));
3660 __isl_give isl_set
*isl_set_gist_params_basic_set(__isl_take isl_set
*set
,
3661 __isl_take isl_basic_set
*context
)
3663 isl_space
*space
= isl_set_get_space(set
);
3664 isl_basic_set
*dom_context
= isl_basic_set_universe(space
);
3665 dom_context
= isl_basic_set_intersect_params(dom_context
, context
);
3666 return isl_set_gist_basic_set(set
, dom_context
);
3669 __isl_give isl_set
*isl_set_gist(__isl_take isl_set
*set
,
3670 __isl_take isl_set
*context
)
3672 return (struct isl_set
*)isl_map_gist(set_to_map(set
),
3673 set_to_map(context
));
3676 /* Compute the gist of "bmap" with respect to the constraints "context"
3679 __isl_give isl_basic_map
*isl_basic_map_gist_domain(
3680 __isl_take isl_basic_map
*bmap
, __isl_take isl_basic_set
*context
)
3682 isl_space
*space
= isl_basic_map_get_space(bmap
);
3683 isl_basic_map
*bmap_context
= isl_basic_map_universe(space
);
3685 bmap_context
= isl_basic_map_intersect_domain(bmap_context
, context
);
3686 return isl_basic_map_gist(bmap
, bmap_context
);
3689 __isl_give isl_map
*isl_map_gist_domain(__isl_take isl_map
*map
,
3690 __isl_take isl_set
*context
)
3692 isl_map
*map_context
= isl_map_universe(isl_map_get_space(map
));
3693 map_context
= isl_map_intersect_domain(map_context
, context
);
3694 return isl_map_gist(map
, map_context
);
3697 __isl_give isl_map
*isl_map_gist_range(__isl_take isl_map
*map
,
3698 __isl_take isl_set
*context
)
3700 isl_map
*map_context
= isl_map_universe(isl_map_get_space(map
));
3701 map_context
= isl_map_intersect_range(map_context
, context
);
3702 return isl_map_gist(map
, map_context
);
3705 __isl_give isl_map
*isl_map_gist_params(__isl_take isl_map
*map
,
3706 __isl_take isl_set
*context
)
3708 isl_map
*map_context
= isl_map_universe(isl_map_get_space(map
));
3709 map_context
= isl_map_intersect_params(map_context
, context
);
3710 return isl_map_gist(map
, map_context
);
3713 __isl_give isl_set
*isl_set_gist_params(__isl_take isl_set
*set
,
3714 __isl_take isl_set
*context
)
3716 return isl_map_gist_params(set
, context
);
3719 /* Quick check to see if two basic maps are disjoint.
3720 * In particular, we reduce the equalities and inequalities of
3721 * one basic map in the context of the equalities of the other
3722 * basic map and check if we get a contradiction.
3724 isl_bool
isl_basic_map_plain_is_disjoint(__isl_keep isl_basic_map
*bmap1
,
3725 __isl_keep isl_basic_map
*bmap2
)
3727 struct isl_vec
*v
= NULL
;
3732 if (!bmap1
|| !bmap2
)
3733 return isl_bool_error
;
3734 isl_assert(bmap1
->ctx
, isl_space_is_equal(bmap1
->dim
, bmap2
->dim
),
3735 return isl_bool_error
);
3736 if (bmap1
->n_div
|| bmap2
->n_div
)
3737 return isl_bool_false
;
3738 if (!bmap1
->n_eq
&& !bmap2
->n_eq
)
3739 return isl_bool_false
;
3741 total
= isl_space_dim(bmap1
->dim
, isl_dim_all
);
3743 return isl_bool_false
;
3744 v
= isl_vec_alloc(bmap1
->ctx
, 1 + total
);
3747 elim
= isl_alloc_array(bmap1
->ctx
, int, total
);
3750 compute_elimination_index(bmap1
, elim
);
3751 for (i
= 0; i
< bmap2
->n_eq
; ++i
) {
3753 reduced
= reduced_using_equalities(v
->block
.data
, bmap2
->eq
[i
],
3755 if (reduced
&& !isl_int_is_zero(v
->block
.data
[0]) &&
3756 isl_seq_first_non_zero(v
->block
.data
+ 1, total
) == -1)
3759 for (i
= 0; i
< bmap2
->n_ineq
; ++i
) {
3761 reduced
= reduced_using_equalities(v
->block
.data
,
3762 bmap2
->ineq
[i
], bmap1
, elim
);
3763 if (reduced
&& isl_int_is_neg(v
->block
.data
[0]) &&
3764 isl_seq_first_non_zero(v
->block
.data
+ 1, total
) == -1)
3767 compute_elimination_index(bmap2
, elim
);
3768 for (i
= 0; i
< bmap1
->n_ineq
; ++i
) {
3770 reduced
= reduced_using_equalities(v
->block
.data
,
3771 bmap1
->ineq
[i
], bmap2
, elim
);
3772 if (reduced
&& isl_int_is_neg(v
->block
.data
[0]) &&
3773 isl_seq_first_non_zero(v
->block
.data
+ 1, total
) == -1)
3778 return isl_bool_false
;
3782 return isl_bool_true
;
3786 return isl_bool_error
;
3789 int isl_basic_set_plain_is_disjoint(__isl_keep isl_basic_set
*bset1
,
3790 __isl_keep isl_basic_set
*bset2
)
3792 return isl_basic_map_plain_is_disjoint(bset_to_bmap(bset1
),
3793 bset_to_bmap(bset2
));
3796 /* Does "test" hold for all pairs of basic maps in "map1" and "map2"?
3798 static isl_bool
all_pairs(__isl_keep isl_map
*map1
, __isl_keep isl_map
*map2
,
3799 isl_bool (*test
)(__isl_keep isl_basic_map
*bmap1
,
3800 __isl_keep isl_basic_map
*bmap2
))
3805 return isl_bool_error
;
3807 for (i
= 0; i
< map1
->n
; ++i
) {
3808 for (j
= 0; j
< map2
->n
; ++j
) {
3809 isl_bool d
= test(map1
->p
[i
], map2
->p
[j
]);
3810 if (d
!= isl_bool_true
)
3815 return isl_bool_true
;
3818 /* Are "map1" and "map2" obviously disjoint, based on information
3819 * that can be derived without looking at the individual basic maps?
3821 * In particular, if one of them is empty or if they live in different spaces
3822 * (ignoring parameters), then they are clearly disjoint.
3824 static isl_bool
isl_map_plain_is_disjoint_global(__isl_keep isl_map
*map1
,
3825 __isl_keep isl_map
*map2
)
3831 return isl_bool_error
;
3833 disjoint
= isl_map_plain_is_empty(map1
);
3834 if (disjoint
< 0 || disjoint
)
3837 disjoint
= isl_map_plain_is_empty(map2
);
3838 if (disjoint
< 0 || disjoint
)
3841 match
= isl_space_tuple_is_equal(map1
->dim
, isl_dim_in
,
3842 map2
->dim
, isl_dim_in
);
3843 if (match
< 0 || !match
)
3844 return match
< 0 ? isl_bool_error
: isl_bool_true
;
3846 match
= isl_space_tuple_is_equal(map1
->dim
, isl_dim_out
,
3847 map2
->dim
, isl_dim_out
);
3848 if (match
< 0 || !match
)
3849 return match
< 0 ? isl_bool_error
: isl_bool_true
;
3851 return isl_bool_false
;
3854 /* Are "map1" and "map2" obviously disjoint?
3856 * If one of them is empty or if they live in different spaces (ignoring
3857 * parameters), then they are clearly disjoint.
3858 * This is checked by isl_map_plain_is_disjoint_global.
3860 * If they have different parameters, then we skip any further tests.
3862 * If they are obviously equal, but not obviously empty, then we will
3863 * not be able to detect if they are disjoint.
3865 * Otherwise we check if each basic map in "map1" is obviously disjoint
3866 * from each basic map in "map2".
3868 isl_bool
isl_map_plain_is_disjoint(__isl_keep isl_map
*map1
,
3869 __isl_keep isl_map
*map2
)
3875 disjoint
= isl_map_plain_is_disjoint_global(map1
, map2
);
3876 if (disjoint
< 0 || disjoint
)
3879 match
= isl_space_match(map1
->dim
, isl_dim_param
,
3880 map2
->dim
, isl_dim_param
);
3881 if (match
< 0 || !match
)
3882 return match
< 0 ? isl_bool_error
: isl_bool_false
;
3884 intersect
= isl_map_plain_is_equal(map1
, map2
);
3885 if (intersect
< 0 || intersect
)
3886 return intersect
< 0 ? isl_bool_error
: isl_bool_false
;
3888 return all_pairs(map1
, map2
, &isl_basic_map_plain_is_disjoint
);
3891 /* Are "map1" and "map2" disjoint?
3893 * They are disjoint if they are "obviously disjoint" or if one of them
3894 * is empty. Otherwise, they are not disjoint if one of them is universal.
3895 * If the two inputs are (obviously) equal and not empty, then they are
3897 * If none of these cases apply, then check if all pairs of basic maps
3900 isl_bool
isl_map_is_disjoint(__isl_keep isl_map
*map1
, __isl_keep isl_map
*map2
)
3905 disjoint
= isl_map_plain_is_disjoint_global(map1
, map2
);
3906 if (disjoint
< 0 || disjoint
)
3909 disjoint
= isl_map_is_empty(map1
);
3910 if (disjoint
< 0 || disjoint
)
3913 disjoint
= isl_map_is_empty(map2
);
3914 if (disjoint
< 0 || disjoint
)
3917 intersect
= isl_map_plain_is_universe(map1
);
3918 if (intersect
< 0 || intersect
)
3919 return intersect
< 0 ? isl_bool_error
: isl_bool_false
;
3921 intersect
= isl_map_plain_is_universe(map2
);
3922 if (intersect
< 0 || intersect
)
3923 return intersect
< 0 ? isl_bool_error
: isl_bool_false
;
3925 intersect
= isl_map_plain_is_equal(map1
, map2
);
3926 if (intersect
< 0 || intersect
)
3927 return isl_bool_not(intersect
);
3929 return all_pairs(map1
, map2
, &isl_basic_map_is_disjoint
);
3932 /* Are "bmap1" and "bmap2" disjoint?
3934 * They are disjoint if they are "obviously disjoint" or if one of them
3935 * is empty. Otherwise, they are not disjoint if one of them is universal.
3936 * If none of these cases apply, we compute the intersection and see if
3937 * the result is empty.
3939 isl_bool
isl_basic_map_is_disjoint(__isl_keep isl_basic_map
*bmap1
,
3940 __isl_keep isl_basic_map
*bmap2
)
3944 isl_basic_map
*test
;
3946 disjoint
= isl_basic_map_plain_is_disjoint(bmap1
, bmap2
);
3947 if (disjoint
< 0 || disjoint
)
3950 disjoint
= isl_basic_map_is_empty(bmap1
);
3951 if (disjoint
< 0 || disjoint
)
3954 disjoint
= isl_basic_map_is_empty(bmap2
);
3955 if (disjoint
< 0 || disjoint
)
3958 intersect
= isl_basic_map_plain_is_universe(bmap1
);
3959 if (intersect
< 0 || intersect
)
3960 return intersect
< 0 ? isl_bool_error
: isl_bool_false
;
3962 intersect
= isl_basic_map_plain_is_universe(bmap2
);
3963 if (intersect
< 0 || intersect
)
3964 return intersect
< 0 ? isl_bool_error
: isl_bool_false
;
3966 test
= isl_basic_map_intersect(isl_basic_map_copy(bmap1
),
3967 isl_basic_map_copy(bmap2
));
3968 disjoint
= isl_basic_map_is_empty(test
);
3969 isl_basic_map_free(test
);
3974 /* Are "bset1" and "bset2" disjoint?
3976 isl_bool
isl_basic_set_is_disjoint(__isl_keep isl_basic_set
*bset1
,
3977 __isl_keep isl_basic_set
*bset2
)
3979 return isl_basic_map_is_disjoint(bset1
, bset2
);
3982 isl_bool
isl_set_plain_is_disjoint(__isl_keep isl_set
*set1
,
3983 __isl_keep isl_set
*set2
)
3985 return isl_map_plain_is_disjoint(set_to_map(set1
), set_to_map(set2
));
3988 /* Are "set1" and "set2" disjoint?
3990 isl_bool
isl_set_is_disjoint(__isl_keep isl_set
*set1
, __isl_keep isl_set
*set2
)
3992 return isl_map_is_disjoint(set1
, set2
);
3995 /* Is "v" equal to 0, 1 or -1?
3997 static int is_zero_or_one(isl_int v
)
3999 return isl_int_is_zero(v
) || isl_int_is_one(v
) || isl_int_is_negone(v
);
4002 /* Check if we can combine a given div with lower bound l and upper
4003 * bound u with some other div and if so return that other div.
4004 * Otherwise return -1.
4006 * We first check that
4007 * - the bounds are opposites of each other (except for the constant
4009 * - the bounds do not reference any other div
4010 * - no div is defined in terms of this div
4012 * Let m be the size of the range allowed on the div by the bounds.
4013 * That is, the bounds are of the form
4015 * e <= a <= e + m - 1
4017 * with e some expression in the other variables.
4018 * We look for another div b such that no third div is defined in terms
4019 * of this second div b and such that in any constraint that contains
4020 * a (except for the given lower and upper bound), also contains b
4021 * with a coefficient that is m times that of b.
4022 * That is, all constraints (execpt for the lower and upper bound)
4025 * e + f (a + m b) >= 0
4027 * Furthermore, in the constraints that only contain b, the coefficient
4028 * of b should be equal to 1 or -1.
4029 * If so, we return b so that "a + m b" can be replaced by
4030 * a single div "c = a + m b".
4032 static int div_find_coalesce(struct isl_basic_map
*bmap
, int *pairs
,
4033 unsigned div
, unsigned l
, unsigned u
)
4039 if (bmap
->n_div
<= 1)
4041 dim
= isl_space_dim(bmap
->dim
, isl_dim_all
);
4042 if (isl_seq_first_non_zero(bmap
->ineq
[l
] + 1 + dim
, div
) != -1)
4044 if (isl_seq_first_non_zero(bmap
->ineq
[l
] + 1 + dim
+ div
+ 1,
4045 bmap
->n_div
- div
- 1) != -1)
4047 if (!isl_seq_is_neg(bmap
->ineq
[l
] + 1, bmap
->ineq
[u
] + 1,
4051 for (i
= 0; i
< bmap
->n_div
; ++i
) {
4052 if (isl_int_is_zero(bmap
->div
[i
][0]))
4054 if (!isl_int_is_zero(bmap
->div
[i
][1 + 1 + dim
+ div
]))
4058 isl_int_add(bmap
->ineq
[l
][0], bmap
->ineq
[l
][0], bmap
->ineq
[u
][0]);
4059 if (isl_int_is_neg(bmap
->ineq
[l
][0])) {
4060 isl_int_sub(bmap
->ineq
[l
][0],
4061 bmap
->ineq
[l
][0], bmap
->ineq
[u
][0]);
4062 bmap
= isl_basic_map_copy(bmap
);
4063 bmap
= isl_basic_map_set_to_empty(bmap
);
4064 isl_basic_map_free(bmap
);
4067 isl_int_add_ui(bmap
->ineq
[l
][0], bmap
->ineq
[l
][0], 1);
4068 for (i
= 0; i
< bmap
->n_div
; ++i
) {
4073 for (j
= 0; j
< bmap
->n_div
; ++j
) {
4074 if (isl_int_is_zero(bmap
->div
[j
][0]))
4076 if (!isl_int_is_zero(bmap
->div
[j
][1 + 1 + dim
+ i
]))
4079 if (j
< bmap
->n_div
)
4081 for (j
= 0; j
< bmap
->n_ineq
; ++j
) {
4083 if (j
== l
|| j
== u
)
4085 if (isl_int_is_zero(bmap
->ineq
[j
][1 + dim
+ div
])) {
4086 if (is_zero_or_one(bmap
->ineq
[j
][1 + dim
+ i
]))
4090 if (isl_int_is_zero(bmap
->ineq
[j
][1 + dim
+ i
]))
4092 isl_int_mul(bmap
->ineq
[j
][1 + dim
+ div
],
4093 bmap
->ineq
[j
][1 + dim
+ div
],
4095 valid
= isl_int_eq(bmap
->ineq
[j
][1 + dim
+ div
],
4096 bmap
->ineq
[j
][1 + dim
+ i
]);
4097 isl_int_divexact(bmap
->ineq
[j
][1 + dim
+ div
],
4098 bmap
->ineq
[j
][1 + dim
+ div
],
4103 if (j
< bmap
->n_ineq
)
4108 isl_int_sub_ui(bmap
->ineq
[l
][0], bmap
->ineq
[l
][0], 1);
4109 isl_int_sub(bmap
->ineq
[l
][0], bmap
->ineq
[l
][0], bmap
->ineq
[u
][0]);
4113 /* Internal data structure used during the construction and/or evaluation of
4114 * an inequality that ensures that a pair of bounds always allows
4115 * for an integer value.
4117 * "tab" is the tableau in which the inequality is evaluated. It may
4118 * be NULL until it is actually needed.
4119 * "v" contains the inequality coefficients.
4120 * "g", "fl" and "fu" are temporary scalars used during the construction and
4123 struct test_ineq_data
{
4124 struct isl_tab
*tab
;
4131 /* Free all the memory allocated by the fields of "data".
4133 static void test_ineq_data_clear(struct test_ineq_data
*data
)
4135 isl_tab_free(data
->tab
);
4136 isl_vec_free(data
->v
);
4137 isl_int_clear(data
->g
);
4138 isl_int_clear(data
->fl
);
4139 isl_int_clear(data
->fu
);
4142 /* Is the inequality stored in data->v satisfied by "bmap"?
4143 * That is, does it only attain non-negative values?
4144 * data->tab is a tableau corresponding to "bmap".
4146 static isl_bool
test_ineq_is_satisfied(__isl_keep isl_basic_map
*bmap
,
4147 struct test_ineq_data
*data
)
4150 enum isl_lp_result res
;
4152 ctx
= isl_basic_map_get_ctx(bmap
);
4154 data
->tab
= isl_tab_from_basic_map(bmap
, 0);
4155 res
= isl_tab_min(data
->tab
, data
->v
->el
, ctx
->one
, &data
->g
, NULL
, 0);
4156 if (res
== isl_lp_error
)
4157 return isl_bool_error
;
4158 return res
== isl_lp_ok
&& isl_int_is_nonneg(data
->g
);
4161 /* Given a lower and an upper bound on div i, do they always allow
4162 * for an integer value of the given div?
4163 * Determine this property by constructing an inequality
4164 * such that the property is guaranteed when the inequality is nonnegative.
4165 * The lower bound is inequality l, while the upper bound is inequality u.
4166 * The constructed inequality is stored in data->v.
4168 * Let the upper bound be
4172 * and the lower bound
4176 * Let n_u = f_u g and n_l = f_l g, with g = gcd(n_u, n_l).
4179 * - f_u e_l <= f_u f_l g a <= f_l e_u
4181 * Since all variables are integer valued, this is equivalent to
4183 * - f_u e_l - (f_u - 1) <= f_u f_l g a <= f_l e_u + (f_l - 1)
4185 * If this interval is at least f_u f_l g, then it contains at least
4186 * one integer value for a.
4187 * That is, the test constraint is
4189 * f_l e_u + f_u e_l + f_l - 1 + f_u - 1 + 1 >= f_u f_l g
4193 * f_l e_u + f_u e_l + f_l - 1 + f_u - 1 + 1 - f_u f_l g >= 0
4195 * If the coefficients of f_l e_u + f_u e_l have a common divisor g',
4196 * then the constraint can be scaled down by a factor g',
4197 * with the constant term replaced by
4198 * floor((f_l e_{u,0} + f_u e_{l,0} + f_l - 1 + f_u - 1 + 1 - f_u f_l g)/g').
4199 * Note that the result of applying Fourier-Motzkin to this pair
4202 * f_l e_u + f_u e_l >= 0
4204 * If the constant term of the scaled down version of this constraint,
4205 * i.e., floor((f_l e_{u,0} + f_u e_{l,0})/g') is equal to the constant
4206 * term of the scaled down test constraint, then the test constraint
4207 * is known to hold and no explicit evaluation is required.
4208 * This is essentially the Omega test.
4210 * If the test constraint consists of only a constant term, then
4211 * it is sufficient to look at the sign of this constant term.
4213 static isl_bool
int_between_bounds(__isl_keep isl_basic_map
*bmap
, int i
,
4214 int l
, int u
, struct test_ineq_data
*data
)
4216 unsigned offset
, n_div
;
4217 offset
= isl_basic_map_offset(bmap
, isl_dim_div
);
4218 n_div
= isl_basic_map_dim(bmap
, isl_dim_div
);
4220 isl_int_gcd(data
->g
,
4221 bmap
->ineq
[l
][offset
+ i
], bmap
->ineq
[u
][offset
+ i
]);
4222 isl_int_divexact(data
->fl
, bmap
->ineq
[l
][offset
+ i
], data
->g
);
4223 isl_int_divexact(data
->fu
, bmap
->ineq
[u
][offset
+ i
], data
->g
);
4224 isl_int_neg(data
->fu
, data
->fu
);
4225 isl_seq_combine(data
->v
->el
, data
->fl
, bmap
->ineq
[u
],
4226 data
->fu
, bmap
->ineq
[l
], offset
+ n_div
);
4227 isl_int_mul(data
->g
, data
->g
, data
->fl
);
4228 isl_int_mul(data
->g
, data
->g
, data
->fu
);
4229 isl_int_sub(data
->g
, data
->g
, data
->fl
);
4230 isl_int_sub(data
->g
, data
->g
, data
->fu
);
4231 isl_int_add_ui(data
->g
, data
->g
, 1);
4232 isl_int_sub(data
->fl
, data
->v
->el
[0], data
->g
);
4234 isl_seq_gcd(data
->v
->el
+ 1, offset
- 1 + n_div
, &data
->g
);
4235 if (isl_int_is_zero(data
->g
))
4236 return isl_int_is_nonneg(data
->fl
);
4237 if (isl_int_is_one(data
->g
)) {
4238 isl_int_set(data
->v
->el
[0], data
->fl
);
4239 return test_ineq_is_satisfied(bmap
, data
);
4241 isl_int_fdiv_q(data
->fl
, data
->fl
, data
->g
);
4242 isl_int_fdiv_q(data
->v
->el
[0], data
->v
->el
[0], data
->g
);
4243 if (isl_int_eq(data
->fl
, data
->v
->el
[0]))
4244 return isl_bool_true
;
4245 isl_int_set(data
->v
->el
[0], data
->fl
);
4246 isl_seq_scale_down(data
->v
->el
+ 1, data
->v
->el
+ 1, data
->g
,
4247 offset
- 1 + n_div
);
4249 return test_ineq_is_satisfied(bmap
, data
);
4252 /* Remove more kinds of divs that are not strictly needed.
4253 * In particular, if all pairs of lower and upper bounds on a div
4254 * are such that they allow at least one integer value of the div,
4255 * then we can eliminate the div using Fourier-Motzkin without
4256 * introducing any spurious solutions.
4258 * If at least one of the two constraints has a unit coefficient for the div,
4259 * then the presence of such a value is guaranteed so there is no need to check.
4260 * In particular, the value attained by the bound with unit coefficient
4261 * can serve as this intermediate value.
4263 static struct isl_basic_map
*drop_more_redundant_divs(
4264 struct isl_basic_map
*bmap
, int *pairs
, int n
)
4267 struct test_ineq_data data
= { NULL
, NULL
};
4268 unsigned off
, n_div
;
4271 isl_int_init(data
.g
);
4272 isl_int_init(data
.fl
);
4273 isl_int_init(data
.fu
);
4278 ctx
= isl_basic_map_get_ctx(bmap
);
4279 off
= isl_basic_map_offset(bmap
, isl_dim_div
);
4280 n_div
= isl_basic_map_dim(bmap
, isl_dim_div
);
4281 data
.v
= isl_vec_alloc(ctx
, off
+ n_div
);
4290 for (i
= 0; i
< n_div
; ++i
) {
4293 if (best
>= 0 && pairs
[best
] <= pairs
[i
])
4299 for (l
= 0; l
< bmap
->n_ineq
; ++l
) {
4300 if (!isl_int_is_pos(bmap
->ineq
[l
][off
+ i
]))
4302 if (isl_int_is_one(bmap
->ineq
[l
][off
+ i
]))
4304 for (u
= 0; u
< bmap
->n_ineq
; ++u
) {
4305 if (!isl_int_is_neg(bmap
->ineq
[u
][off
+ i
]))
4307 if (isl_int_is_negone(bmap
->ineq
[u
][off
+ i
]))
4309 has_int
= int_between_bounds(bmap
, i
, l
, u
,
4313 if (data
.tab
&& data
.tab
->empty
)
4318 if (u
< bmap
->n_ineq
)
4321 if (data
.tab
&& data
.tab
->empty
) {
4322 bmap
= isl_basic_map_set_to_empty(bmap
);
4325 if (l
== bmap
->n_ineq
) {
4333 test_ineq_data_clear(&data
);
4340 bmap
= isl_basic_map_remove_dims(bmap
, isl_dim_div
, remove
, 1);
4341 return isl_basic_map_drop_redundant_divs(bmap
);
4344 isl_basic_map_free(bmap
);
4345 test_ineq_data_clear(&data
);
4349 /* Given a pair of divs div1 and div2 such that, except for the lower bound l
4350 * and the upper bound u, div1 always occurs together with div2 in the form
4351 * (div1 + m div2), where m is the constant range on the variable div1
4352 * allowed by l and u, replace the pair div1 and div2 by a single
4353 * div that is equal to div1 + m div2.
4355 * The new div will appear in the location that contains div2.
4356 * We need to modify all constraints that contain
4357 * div2 = (div - div1) / m
4358 * The coefficient of div2 is known to be equal to 1 or -1.
4359 * (If a constraint does not contain div2, it will also not contain div1.)
4360 * If the constraint also contains div1, then we know they appear
4361 * as f (div1 + m div2) and we can simply replace (div1 + m div2) by div,
4362 * i.e., the coefficient of div is f.
4364 * Otherwise, we first need to introduce div1 into the constraint.
4373 * A lower bound on div2
4377 * can be replaced by
4379 * m div2 + div1 + m t + f >= 0
4385 * can be replaced by
4387 * -(m div2 + div1) + m t + f' >= 0
4389 * These constraint are those that we would obtain from eliminating
4390 * div1 using Fourier-Motzkin.
4392 * After all constraints have been modified, we drop the lower and upper
4393 * bound and then drop div1.
4395 static struct isl_basic_map
*coalesce_divs(struct isl_basic_map
*bmap
,
4396 unsigned div1
, unsigned div2
, unsigned l
, unsigned u
)
4400 unsigned dim
, total
;
4403 ctx
= isl_basic_map_get_ctx(bmap
);
4405 dim
= isl_space_dim(bmap
->dim
, isl_dim_all
);
4406 total
= 1 + dim
+ bmap
->n_div
;
4409 isl_int_add(m
, bmap
->ineq
[l
][0], bmap
->ineq
[u
][0]);
4410 isl_int_add_ui(m
, m
, 1);
4412 for (i
= 0; i
< bmap
->n_ineq
; ++i
) {
4413 if (i
== l
|| i
== u
)
4415 if (isl_int_is_zero(bmap
->ineq
[i
][1 + dim
+ div2
]))
4417 if (isl_int_is_zero(bmap
->ineq
[i
][1 + dim
+ div1
])) {
4418 if (isl_int_is_pos(bmap
->ineq
[i
][1 + dim
+ div2
]))
4419 isl_seq_combine(bmap
->ineq
[i
], m
, bmap
->ineq
[i
],
4420 ctx
->one
, bmap
->ineq
[l
], total
);
4422 isl_seq_combine(bmap
->ineq
[i
], m
, bmap
->ineq
[i
],
4423 ctx
->one
, bmap
->ineq
[u
], total
);
4425 isl_int_set(bmap
->ineq
[i
][1 + dim
+ div2
],
4426 bmap
->ineq
[i
][1 + dim
+ div1
]);
4427 isl_int_set_si(bmap
->ineq
[i
][1 + dim
+ div1
], 0);
4432 isl_basic_map_drop_inequality(bmap
, l
);
4433 isl_basic_map_drop_inequality(bmap
, u
);
4435 isl_basic_map_drop_inequality(bmap
, u
);
4436 isl_basic_map_drop_inequality(bmap
, l
);
4438 bmap
= isl_basic_map_drop_div(bmap
, div1
);
4442 /* First check if we can coalesce any pair of divs and
4443 * then continue with dropping more redundant divs.
4445 * We loop over all pairs of lower and upper bounds on a div
4446 * with coefficient 1 and -1, respectively, check if there
4447 * is any other div "c" with which we can coalesce the div
4448 * and if so, perform the coalescing.
4450 static struct isl_basic_map
*coalesce_or_drop_more_redundant_divs(
4451 struct isl_basic_map
*bmap
, int *pairs
, int n
)
4456 dim
= isl_space_dim(bmap
->dim
, isl_dim_all
);
4458 for (i
= 0; i
< bmap
->n_div
; ++i
) {
4461 for (l
= 0; l
< bmap
->n_ineq
; ++l
) {
4462 if (!isl_int_is_one(bmap
->ineq
[l
][1 + dim
+ i
]))
4464 for (u
= 0; u
< bmap
->n_ineq
; ++u
) {
4467 if (!isl_int_is_negone(bmap
->ineq
[u
][1+dim
+i
]))
4469 c
= div_find_coalesce(bmap
, pairs
, i
, l
, u
);
4473 bmap
= coalesce_divs(bmap
, i
, c
, l
, u
);
4474 return isl_basic_map_drop_redundant_divs(bmap
);
4479 if (ISL_F_ISSET(bmap
, ISL_BASIC_MAP_EMPTY
))
4482 return drop_more_redundant_divs(bmap
, pairs
, n
);
4485 /* Are the "n" coefficients starting at "first" of inequality constraints
4486 * "i" and "j" of "bmap" equal to each other?
4488 static int is_parallel_part(__isl_keep isl_basic_map
*bmap
, int i
, int j
,
4491 return isl_seq_eq(bmap
->ineq
[i
] + first
, bmap
->ineq
[j
] + first
, n
);
4494 /* Are the "n" coefficients starting at "first" of inequality constraints
4495 * "i" and "j" of "bmap" opposite to each other?
4497 static int is_opposite_part(__isl_keep isl_basic_map
*bmap
, int i
, int j
,
4500 return isl_seq_is_neg(bmap
->ineq
[i
] + first
, bmap
->ineq
[j
] + first
, n
);
4503 /* Are inequality constraints "i" and "j" of "bmap" opposite to each other,
4504 * apart from the constant term?
4506 static int is_opposite(__isl_keep isl_basic_map
*bmap
, int i
, int j
)
4510 total
= isl_basic_map_dim(bmap
, isl_dim_all
);
4511 return is_opposite_part(bmap
, i
, j
, 1, total
);
4514 /* Are inequality constraints "i" and "j" of "bmap" equal to each other,
4515 * apart from the constant term and the coefficient at position "pos"?
4517 static int is_parallel_except(__isl_keep isl_basic_map
*bmap
, int i
, int j
,
4522 total
= isl_basic_map_dim(bmap
, isl_dim_all
);
4523 return is_parallel_part(bmap
, i
, j
, 1, pos
- 1) &&
4524 is_parallel_part(bmap
, i
, j
, pos
+ 1, total
- pos
);
4527 /* Are inequality constraints "i" and "j" of "bmap" opposite to each other,
4528 * apart from the constant term and the coefficient at position "pos"?
4530 static int is_opposite_except(__isl_keep isl_basic_map
*bmap
, int i
, int j
,
4535 total
= isl_basic_map_dim(bmap
, isl_dim_all
);
4536 return is_opposite_part(bmap
, i
, j
, 1, pos
- 1) &&
4537 is_opposite_part(bmap
, i
, j
, pos
+ 1, total
- pos
);
4540 /* Restart isl_basic_map_drop_redundant_divs after "bmap" has
4541 * been modified, simplying it if "simplify" is set.
4542 * Free the temporary data structure "pairs" that was associated
4543 * to the old version of "bmap".
4545 static __isl_give isl_basic_map
*drop_redundant_divs_again(
4546 __isl_take isl_basic_map
*bmap
, __isl_take
int *pairs
, int simplify
)
4549 bmap
= isl_basic_map_simplify(bmap
);
4551 return isl_basic_map_drop_redundant_divs(bmap
);
4554 /* Is "div" the single unknown existentially quantified variable
4555 * in inequality constraint "ineq" of "bmap"?
4556 * "div" is known to have a non-zero coefficient in "ineq".
4558 static int single_unknown(__isl_keep isl_basic_map
*bmap
, int ineq
, int div
)
4561 unsigned n_div
, o_div
;
4563 if (isl_basic_map_div_is_known(bmap
, div
))
4565 n_div
= isl_basic_map_dim(bmap
, isl_dim_div
);
4568 o_div
= isl_basic_map_offset(bmap
, isl_dim_div
);
4569 for (i
= 0; i
< n_div
; ++i
) {
4572 if (isl_int_is_zero(bmap
->ineq
[ineq
][o_div
+ i
]))
4574 if (!isl_basic_map_div_is_known(bmap
, i
))
4581 /* Does integer division "div" have coefficient 1 in inequality constraint
4584 static int has_coef_one(__isl_keep isl_basic_map
*bmap
, int div
, int ineq
)
4588 o_div
= isl_basic_map_offset(bmap
, isl_dim_div
);
4589 if (isl_int_is_one(bmap
->ineq
[ineq
][o_div
+ div
]))
4595 /* Turn inequality constraint "ineq" of "bmap" into an equality and
4596 * then try and drop redundant divs again,
4597 * freeing the temporary data structure "pairs" that was associated
4598 * to the old version of "bmap".
4600 static __isl_give isl_basic_map
*set_eq_and_try_again(
4601 __isl_take isl_basic_map
*bmap
, int ineq
, __isl_take
int *pairs
)
4603 bmap
= isl_basic_map_cow(bmap
);
4604 isl_basic_map_inequality_to_equality(bmap
, ineq
);
4605 return drop_redundant_divs_again(bmap
, pairs
, 1);
4608 /* Drop the integer division at position "div", along with the two
4609 * inequality constraints "ineq1" and "ineq2" in which it appears
4610 * from "bmap" and then try and drop redundant divs again,
4611 * freeing the temporary data structure "pairs" that was associated
4612 * to the old version of "bmap".
4614 static __isl_give isl_basic_map
*drop_div_and_try_again(
4615 __isl_take isl_basic_map
*bmap
, int div
, int ineq1
, int ineq2
,
4616 __isl_take
int *pairs
)
4618 if (ineq1
> ineq2
) {
4619 isl_basic_map_drop_inequality(bmap
, ineq1
);
4620 isl_basic_map_drop_inequality(bmap
, ineq2
);
4622 isl_basic_map_drop_inequality(bmap
, ineq2
);
4623 isl_basic_map_drop_inequality(bmap
, ineq1
);
4625 bmap
= isl_basic_map_drop_div(bmap
, div
);
4626 return drop_redundant_divs_again(bmap
, pairs
, 0);
4629 /* Given two inequality constraints
4631 * f(x) + n d + c >= 0, (ineq)
4633 * with d the variable at position "pos", and
4635 * f(x) + c0 >= 0, (lower)
4637 * compute the maximal value of the lower bound ceil((-f(x) - c)/n)
4638 * determined by the first constraint.
4645 static void lower_bound_from_parallel(__isl_keep isl_basic_map
*bmap
,
4646 int ineq
, int lower
, int pos
, isl_int
*l
)
4648 isl_int_neg(*l
, bmap
->ineq
[ineq
][0]);
4649 isl_int_add(*l
, *l
, bmap
->ineq
[lower
][0]);
4650 isl_int_cdiv_q(*l
, *l
, bmap
->ineq
[ineq
][pos
]);
4653 /* Given two inequality constraints
4655 * f(x) + n d + c >= 0, (ineq)
4657 * with d the variable at position "pos", and
4659 * -f(x) - c0 >= 0, (upper)
4661 * compute the minimal value of the lower bound ceil((-f(x) - c)/n)
4662 * determined by the first constraint.
4669 static void lower_bound_from_opposite(__isl_keep isl_basic_map
*bmap
,
4670 int ineq
, int upper
, int pos
, isl_int
*u
)
4672 isl_int_neg(*u
, bmap
->ineq
[ineq
][0]);
4673 isl_int_sub(*u
, *u
, bmap
->ineq
[upper
][0]);
4674 isl_int_cdiv_q(*u
, *u
, bmap
->ineq
[ineq
][pos
]);
4677 /* Given a lower bound constraint "ineq" on "div" in "bmap",
4678 * does the corresponding lower bound have a fixed value in "bmap"?
4680 * In particular, "ineq" is of the form
4682 * f(x) + n d + c >= 0
4684 * with n > 0, c the constant term and
4685 * d the existentially quantified variable "div".
4686 * That is, the lower bound is
4688 * ceil((-f(x) - c)/n)
4690 * Look for a pair of constraints
4695 * i.e., -c1 <= -f(x) <= c0, that fix ceil((-f(x) - c)/n) to a constant value.
4696 * That is, check that
4698 * ceil((-c1 - c)/n) = ceil((c0 - c)/n)
4700 * If so, return the index of inequality f(x) + c0 >= 0.
4701 * Otherwise, return -1.
4703 static int lower_bound_is_cst(__isl_keep isl_basic_map
*bmap
, int div
, int ineq
)
4706 int lower
= -1, upper
= -1;
4707 unsigned o_div
, n_div
;
4711 n_div
= isl_basic_map_dim(bmap
, isl_dim_div
);
4712 o_div
= isl_basic_map_offset(bmap
, isl_dim_div
);
4713 for (i
= 0; i
< bmap
->n_ineq
&& (lower
< 0 || upper
< 0); ++i
) {
4716 if (!isl_int_is_zero(bmap
->ineq
[i
][o_div
+ div
]))
4719 is_parallel_except(bmap
, ineq
, i
, o_div
+ div
)) {
4724 is_opposite_except(bmap
, ineq
, i
, o_div
+ div
)) {
4729 if (lower
< 0 || upper
< 0)
4735 lower_bound_from_parallel(bmap
, ineq
, lower
, o_div
+ div
, &l
);
4736 lower_bound_from_opposite(bmap
, ineq
, upper
, o_div
+ div
, &u
);
4738 equal
= isl_int_eq(l
, u
);
4743 return equal
? lower
: -1;
4746 /* Given a lower bound constraint "ineq" on the existentially quantified
4747 * variable "div", such that the corresponding lower bound has
4748 * a fixed value in "bmap", assign this fixed value to the variable and
4749 * then try and drop redundant divs again,
4750 * freeing the temporary data structure "pairs" that was associated
4751 * to the old version of "bmap".
4752 * "lower" determines the constant value for the lower bound.
4754 * In particular, "ineq" is of the form
4756 * f(x) + n d + c >= 0,
4758 * while "lower" is of the form
4762 * The lower bound is ceil((-f(x) - c)/n) and its constant value
4763 * is ceil((c0 - c)/n).
4765 static __isl_give isl_basic_map
*fix_cst_lower(__isl_take isl_basic_map
*bmap
,
4766 int div
, int ineq
, int lower
, int *pairs
)
4773 o_div
= isl_basic_map_offset(bmap
, isl_dim_div
);
4774 lower_bound_from_parallel(bmap
, ineq
, lower
, o_div
+ div
, &c
);
4775 bmap
= isl_basic_map_fix(bmap
, isl_dim_div
, div
, c
);
4780 return isl_basic_map_drop_redundant_divs(bmap
);
4783 /* Remove divs that are not strictly needed based on the inequality
4785 * In particular, if a div only occurs positively (or negatively)
4786 * in constraints, then it can simply be dropped.
4787 * Also, if a div occurs in only two constraints and if moreover
4788 * those two constraints are opposite to each other, except for the constant
4789 * term and if the sum of the constant terms is such that for any value
4790 * of the other values, there is always at least one integer value of the
4791 * div, i.e., if one plus this sum is greater than or equal to
4792 * the (absolute value) of the coefficient of the div in the constraints,
4793 * then we can also simply drop the div.
4795 * If an existentially quantified variable does not have an explicit
4796 * representation, appears in only a single lower bound that does not
4797 * involve any other such existentially quantified variables and appears
4798 * in this lower bound with coefficient 1,
4799 * then fix the variable to the value of the lower bound. That is,
4800 * turn the inequality into an equality.
4801 * If for any value of the other variables, there is any value
4802 * for the existentially quantified variable satisfying the constraints,
4803 * then this lower bound also satisfies the constraints.
4804 * It is therefore safe to pick this lower bound.
4806 * The same reasoning holds even if the coefficient is not one.
4807 * However, fixing the variable to the value of the lower bound may
4808 * in general introduce an extra integer division, in which case
4809 * it may be better to pick another value.
4810 * If this integer division has a known constant value, then plugging
4811 * in this constant value removes the existentially quantified variable
4812 * completely. In particular, if the lower bound is of the form
4813 * ceil((-f(x) - c)/n) and there are two constraints, f(x) + c0 >= 0 and
4814 * -f(x) + c1 >= 0 such that ceil((-c1 - c)/n) = ceil((c0 - c)/n),
4815 * then the existentially quantified variable can be assigned this
4818 * We skip divs that appear in equalities or in the definition of other divs.
4819 * Divs that appear in the definition of other divs usually occur in at least
4820 * 4 constraints, but the constraints may have been simplified.
4822 * If any divs are left after these simple checks then we move on
4823 * to more complicated cases in drop_more_redundant_divs.
4825 static __isl_give isl_basic_map
*isl_basic_map_drop_redundant_divs_ineq(
4826 __isl_take isl_basic_map
*bmap
)
4835 if (bmap
->n_div
== 0)
4838 off
= isl_space_dim(bmap
->dim
, isl_dim_all
);
4839 pairs
= isl_calloc_array(bmap
->ctx
, int, bmap
->n_div
);
4843 for (i
= 0; i
< bmap
->n_div
; ++i
) {
4845 int last_pos
, last_neg
;
4849 defined
= !isl_int_is_zero(bmap
->div
[i
][0]);
4850 for (j
= i
; j
< bmap
->n_div
; ++j
)
4851 if (!isl_int_is_zero(bmap
->div
[j
][1 + 1 + off
+ i
]))
4853 if (j
< bmap
->n_div
)
4855 for (j
= 0; j
< bmap
->n_eq
; ++j
)
4856 if (!isl_int_is_zero(bmap
->eq
[j
][1 + off
+ i
]))
4862 for (j
= 0; j
< bmap
->n_ineq
; ++j
) {
4863 if (isl_int_is_pos(bmap
->ineq
[j
][1 + off
+ i
])) {
4867 if (isl_int_is_neg(bmap
->ineq
[j
][1 + off
+ i
])) {
4872 pairs
[i
] = pos
* neg
;
4873 if (pairs
[i
] == 0) {
4874 for (j
= bmap
->n_ineq
- 1; j
>= 0; --j
)
4875 if (!isl_int_is_zero(bmap
->ineq
[j
][1+off
+i
]))
4876 isl_basic_map_drop_inequality(bmap
, j
);
4877 bmap
= isl_basic_map_drop_div(bmap
, i
);
4878 return drop_redundant_divs_again(bmap
, pairs
, 0);
4880 if (pairs
[i
] != 1 || !is_opposite(bmap
, last_pos
, last_neg
)) {
4884 single
= single_unknown(bmap
, last_pos
, i
);
4887 if (has_coef_one(bmap
, i
, last_pos
))
4888 return set_eq_and_try_again(bmap
, last_pos
,
4890 lower
= lower_bound_is_cst(bmap
, i
, last_pos
);
4892 return fix_cst_lower(bmap
, i
, last_pos
, lower
,
4897 isl_int_add(bmap
->ineq
[last_pos
][0],
4898 bmap
->ineq
[last_pos
][0], bmap
->ineq
[last_neg
][0]);
4899 isl_int_add_ui(bmap
->ineq
[last_pos
][0],
4900 bmap
->ineq
[last_pos
][0], 1);
4901 redundant
= isl_int_ge(bmap
->ineq
[last_pos
][0],
4902 bmap
->ineq
[last_pos
][1+off
+i
]);
4903 isl_int_sub_ui(bmap
->ineq
[last_pos
][0],
4904 bmap
->ineq
[last_pos
][0], 1);
4905 isl_int_sub(bmap
->ineq
[last_pos
][0],
4906 bmap
->ineq
[last_pos
][0], bmap
->ineq
[last_neg
][0]);
4908 return drop_div_and_try_again(bmap
, i
,
4909 last_pos
, last_neg
, pairs
);
4910 if (!defined
&& ok_to_set_div_from_bound(bmap
, i
, last_pos
)) {
4911 bmap
= set_div_from_lower_bound(bmap
, i
, last_pos
);
4912 return drop_redundant_divs_again(bmap
, pairs
, 1);
4919 return coalesce_or_drop_more_redundant_divs(bmap
, pairs
, n
);
4925 isl_basic_map_free(bmap
);
4929 /* Consider the coefficients at "c" as a row vector and replace
4930 * them with their product with "T". "T" is assumed to be a square matrix.
4932 static isl_stat
preimage(isl_int
*c
, __isl_keep isl_mat
*T
)
4939 return isl_stat_error
;
4940 n
= isl_mat_rows(T
);
4941 if (isl_seq_first_non_zero(c
, n
) == -1)
4943 ctx
= isl_mat_get_ctx(T
);
4944 v
= isl_vec_alloc(ctx
, n
);
4946 return isl_stat_error
;
4947 isl_seq_swp_or_cpy(v
->el
, c
, n
);
4948 v
= isl_vec_mat_product(v
, isl_mat_copy(T
));
4950 return isl_stat_error
;
4951 isl_seq_swp_or_cpy(c
, v
->el
, n
);
4957 /* Plug in T for the variables in "bmap" starting at "pos".
4958 * T is a linear unimodular matrix, i.e., without constant term.
4960 static __isl_give isl_basic_map
*isl_basic_map_preimage_vars(
4961 __isl_take isl_basic_map
*bmap
, unsigned pos
, __isl_take isl_mat
*T
)
4966 bmap
= isl_basic_map_cow(bmap
);
4970 n
= isl_mat_cols(T
);
4971 if (n
!= isl_mat_rows(T
))
4972 isl_die(isl_mat_get_ctx(T
), isl_error_invalid
,
4973 "expecting square matrix", goto error
);
4975 total
= isl_basic_map_dim(bmap
, isl_dim_all
);
4976 if (pos
+ n
> total
|| pos
+ n
< pos
)
4977 isl_die(isl_mat_get_ctx(T
), isl_error_invalid
,
4978 "invalid range", goto error
);
4980 for (i
= 0; i
< bmap
->n_eq
; ++i
)
4981 if (preimage(bmap
->eq
[i
] + 1 + pos
, T
) < 0)
4983 for (i
= 0; i
< bmap
->n_ineq
; ++i
)
4984 if (preimage(bmap
->ineq
[i
] + 1 + pos
, T
) < 0)
4986 for (i
= 0; i
< bmap
->n_div
; ++i
) {
4987 if (isl_basic_map_div_is_marked_unknown(bmap
, i
))
4989 if (preimage(bmap
->div
[i
] + 1 + 1 + pos
, T
) < 0)
4996 isl_basic_map_free(bmap
);
5001 /* Remove divs that are not strictly needed.
5003 * First look for an equality constraint involving two or more
5004 * existentially quantified variables without an explicit
5005 * representation. Replace the combination that appears
5006 * in the equality constraint by a single existentially quantified
5007 * variable such that the equality can be used to derive
5008 * an explicit representation for the variable.
5009 * If there are no more such equality constraints, then continue
5010 * with isl_basic_map_drop_redundant_divs_ineq.
5012 * In particular, if the equality constraint is of the form
5014 * f(x) + \sum_i c_i a_i = 0
5016 * with a_i existentially quantified variable without explicit
5017 * representation, then apply a transformation on the existentially
5018 * quantified variables to turn the constraint into
5022 * with g the gcd of the c_i.
5023 * In order to easily identify which existentially quantified variables
5024 * have a complete explicit representation, i.e., without being defined
5025 * in terms of other existentially quantified variables without
5026 * an explicit representation, the existentially quantified variables
5029 * The variable transformation is computed by extending the row
5030 * [c_1/g ... c_n/g] to a unimodular matrix, obtaining the transformation
5032 * [a_1'] [c_1/g ... c_n/g] [ a_1 ]
5037 * with [c_1/g ... c_n/g] representing the first row of U.
5038 * The inverse of U is then plugged into the original constraints.
5039 * The call to isl_basic_map_simplify makes sure the explicit
5040 * representation for a_1' is extracted from the equality constraint.
5042 __isl_give isl_basic_map
*isl_basic_map_drop_redundant_divs(
5043 __isl_take isl_basic_map
*bmap
)
5047 unsigned o_div
, n_div
;
5054 if (isl_basic_map_divs_known(bmap
))
5055 return isl_basic_map_drop_redundant_divs_ineq(bmap
);
5056 if (bmap
->n_eq
== 0)
5057 return isl_basic_map_drop_redundant_divs_ineq(bmap
);
5058 bmap
= isl_basic_map_sort_divs(bmap
);
5062 first
= isl_basic_map_first_unknown_div(bmap
);
5064 return isl_basic_map_free(bmap
);
5066 o_div
= isl_basic_map_offset(bmap
, isl_dim_div
);
5067 n_div
= isl_basic_map_dim(bmap
, isl_dim_div
);
5069 for (i
= 0; i
< bmap
->n_eq
; ++i
) {
5070 l
= isl_seq_first_non_zero(bmap
->eq
[i
] + o_div
+ first
,
5075 if (isl_seq_first_non_zero(bmap
->eq
[i
] + o_div
+ l
+ 1,
5076 n_div
- (l
+ 1)) == -1)
5080 if (i
>= bmap
->n_eq
)
5081 return isl_basic_map_drop_redundant_divs_ineq(bmap
);
5083 ctx
= isl_basic_map_get_ctx(bmap
);
5084 T
= isl_mat_alloc(ctx
, n_div
- l
, n_div
- l
);
5086 return isl_basic_map_free(bmap
);
5087 isl_seq_cpy(T
->row
[0], bmap
->eq
[i
] + o_div
+ l
, n_div
- l
);
5088 T
= isl_mat_normalize_row(T
, 0);
5089 T
= isl_mat_unimodular_complete(T
, 1);
5090 T
= isl_mat_right_inverse(T
);
5092 for (i
= l
; i
< n_div
; ++i
)
5093 bmap
= isl_basic_map_mark_div_unknown(bmap
, i
);
5094 bmap
= isl_basic_map_preimage_vars(bmap
, o_div
- 1 + l
, T
);
5095 bmap
= isl_basic_map_simplify(bmap
);
5097 return isl_basic_map_drop_redundant_divs(bmap
);
5100 struct isl_basic_set
*isl_basic_set_drop_redundant_divs(
5101 struct isl_basic_set
*bset
)
5103 isl_basic_map
*bmap
= bset_to_bmap(bset
);
5104 return bset_from_bmap(isl_basic_map_drop_redundant_divs(bmap
));
5107 struct isl_map
*isl_map_drop_redundant_divs(struct isl_map
*map
)
5113 for (i
= 0; i
< map
->n
; ++i
) {
5114 map
->p
[i
] = isl_basic_map_drop_redundant_divs(map
->p
[i
]);
5118 ISL_F_CLR(map
, ISL_MAP_NORMALIZED
);
5125 struct isl_set
*isl_set_drop_redundant_divs(struct isl_set
*set
)
5127 return (struct isl_set
*)
5128 isl_map_drop_redundant_divs(set_to_map(set
));
5131 /* Does "bmap" satisfy any equality that involves more than 2 variables
5132 * and/or has coefficients different from -1 and 1?
5134 static int has_multiple_var_equality(__isl_keep isl_basic_map
*bmap
)
5139 total
= isl_basic_map_dim(bmap
, isl_dim_all
);
5141 for (i
= 0; i
< bmap
->n_eq
; ++i
) {
5144 j
= isl_seq_first_non_zero(bmap
->eq
[i
] + 1, total
);
5147 if (!isl_int_is_one(bmap
->eq
[i
][1 + j
]) &&
5148 !isl_int_is_negone(bmap
->eq
[i
][1 + j
]))
5152 k
= isl_seq_first_non_zero(bmap
->eq
[i
] + 1 + j
, total
- j
);
5156 if (!isl_int_is_one(bmap
->eq
[i
][1 + j
]) &&
5157 !isl_int_is_negone(bmap
->eq
[i
][1 + j
]))
5161 k
= isl_seq_first_non_zero(bmap
->eq
[i
] + 1 + j
, total
- j
);
5169 /* Remove any common factor g from the constraint coefficients in "v".
5170 * The constant term is stored in the first position and is replaced
5171 * by floor(c/g). If any common factor is removed and if this results
5172 * in a tightening of the constraint, then set *tightened.
5174 static __isl_give isl_vec
*normalize_constraint(__isl_take isl_vec
*v
,
5181 ctx
= isl_vec_get_ctx(v
);
5182 isl_seq_gcd(v
->el
+ 1, v
->size
- 1, &ctx
->normalize_gcd
);
5183 if (isl_int_is_zero(ctx
->normalize_gcd
))
5185 if (isl_int_is_one(ctx
->normalize_gcd
))
5190 if (tightened
&& !isl_int_is_divisible_by(v
->el
[0], ctx
->normalize_gcd
))
5192 isl_int_fdiv_q(v
->el
[0], v
->el
[0], ctx
->normalize_gcd
);
5193 isl_seq_scale_down(v
->el
+ 1, v
->el
+ 1, ctx
->normalize_gcd
,
5198 /* If "bmap" is an integer set that satisfies any equality involving
5199 * more than 2 variables and/or has coefficients different from -1 and 1,
5200 * then use variable compression to reduce the coefficients by removing
5201 * any (hidden) common factor.
5202 * In particular, apply the variable compression to each constraint,
5203 * factor out any common factor in the non-constant coefficients and
5204 * then apply the inverse of the compression.
5205 * At the end, we mark the basic map as having reduced constants.
5206 * If this flag is still set on the next invocation of this function,
5207 * then we skip the computation.
5209 * Removing a common factor may result in a tightening of some of
5210 * the constraints. If this happens, then we may end up with two
5211 * opposite inequalities that can be replaced by an equality.
5212 * We therefore call isl_basic_map_detect_inequality_pairs,
5213 * which checks for such pairs of inequalities as well as eliminate_divs_eq
5214 * and isl_basic_map_gauss if such a pair was found.
5216 __isl_give isl_basic_map
*isl_basic_map_reduce_coefficients(
5217 __isl_take isl_basic_map
*bmap
)
5222 isl_mat
*eq
, *T
, *T2
;
5228 if (ISL_F_ISSET(bmap
, ISL_BASIC_MAP_REDUCED_COEFFICIENTS
))
5230 if (isl_basic_map_is_rational(bmap
))
5232 if (bmap
->n_eq
== 0)
5234 if (!has_multiple_var_equality(bmap
))
5237 total
= isl_basic_map_dim(bmap
, isl_dim_all
);
5238 ctx
= isl_basic_map_get_ctx(bmap
);
5239 v
= isl_vec_alloc(ctx
, 1 + total
);
5241 return isl_basic_map_free(bmap
);
5243 eq
= isl_mat_sub_alloc6(ctx
, bmap
->eq
, 0, bmap
->n_eq
, 0, 1 + total
);
5244 T
= isl_mat_variable_compression(eq
, &T2
);
5247 if (T
->n_col
== 0) {
5251 return isl_basic_map_set_to_empty(bmap
);
5255 for (i
= 0; i
< bmap
->n_ineq
; ++i
) {
5256 isl_seq_cpy(v
->el
, bmap
->ineq
[i
], 1 + total
);
5257 v
= isl_vec_mat_product(v
, isl_mat_copy(T
));
5258 v
= normalize_constraint(v
, &tightened
);
5259 v
= isl_vec_mat_product(v
, isl_mat_copy(T2
));
5262 isl_seq_cpy(bmap
->ineq
[i
], v
->el
, 1 + total
);
5269 ISL_F_SET(bmap
, ISL_BASIC_MAP_REDUCED_COEFFICIENTS
);
5274 bmap
= isl_basic_map_detect_inequality_pairs(bmap
, &progress
);
5276 bmap
= eliminate_divs_eq(bmap
, &progress
);
5277 bmap
= isl_basic_map_gauss(bmap
, NULL
);
5286 return isl_basic_map_free(bmap
);
5289 /* Shift the integer division at position "div" of "bmap"
5290 * by "shift" times the variable at position "pos".
5291 * "pos" is as determined by isl_basic_map_offset, i.e., pos == 0
5292 * corresponds to the constant term.
5294 * That is, if the integer division has the form
5298 * then replace it by
5300 * floor((f(x) + shift * d * x_pos)/d) - shift * x_pos
5302 __isl_give isl_basic_map
*isl_basic_map_shift_div(
5303 __isl_take isl_basic_map
*bmap
, int div
, int pos
, isl_int shift
)
5311 total
= isl_basic_map_dim(bmap
, isl_dim_all
);
5312 total
-= isl_basic_map_dim(bmap
, isl_dim_div
);
5314 isl_int_addmul(bmap
->div
[div
][1 + pos
], shift
, bmap
->div
[div
][0]);
5316 for (i
= 0; i
< bmap
->n_eq
; ++i
) {
5317 if (isl_int_is_zero(bmap
->eq
[i
][1 + total
+ div
]))
5319 isl_int_submul(bmap
->eq
[i
][pos
],
5320 shift
, bmap
->eq
[i
][1 + total
+ div
]);
5322 for (i
= 0; i
< bmap
->n_ineq
; ++i
) {
5323 if (isl_int_is_zero(bmap
->ineq
[i
][1 + total
+ div
]))
5325 isl_int_submul(bmap
->ineq
[i
][pos
],
5326 shift
, bmap
->ineq
[i
][1 + total
+ div
]);
5328 for (i
= 0; i
< bmap
->n_div
; ++i
) {
5329 if (isl_int_is_zero(bmap
->div
[i
][0]))
5331 if (isl_int_is_zero(bmap
->div
[i
][1 + 1 + total
+ div
]))
5333 isl_int_submul(bmap
->div
[i
][1 + pos
],
5334 shift
, bmap
->div
[i
][1 + 1 + total
+ div
]);