2 * Copyright 2008-2009 Katholieke Universiteit Leuven
3 * Copyright 2012-2013 Ecole Normale Superieure
4 * Copyright 2014-2015 INRIA Rocquencourt
5 * Copyright 2016 Sven Verdoolaege
7 * Use of this software is governed by the MIT license
9 * Written by Sven Verdoolaege, K.U.Leuven, Departement
10 * Computerwetenschappen, Celestijnenlaan 200A, B-3001 Leuven, Belgium
11 * and Ecole Normale Superieure, 45 rue d’Ulm, 75230 Paris, France
12 * and Inria Paris - Rocquencourt, Domaine de Voluceau - Rocquencourt,
13 * B.P. 105 - 78153 Le Chesnay, France
16 #include <isl_ctx_private.h>
17 #include <isl_map_private.h>
18 #include "isl_equalities.h"
22 #include <isl_space_private.h>
23 #include <isl_mat_private.h>
24 #include <isl_vec_private.h>
26 #include <bset_to_bmap.c>
27 #include <bset_from_bmap.c>
28 #include <set_to_map.c>
29 #include <set_from_map.c>
31 static void swap_equality(struct isl_basic_map
*bmap
, int a
, int b
)
33 isl_int
*t
= bmap
->eq
[a
];
34 bmap
->eq
[a
] = bmap
->eq
[b
];
38 static void swap_inequality(struct isl_basic_map
*bmap
, int a
, int b
)
41 isl_int
*t
= bmap
->ineq
[a
];
42 bmap
->ineq
[a
] = bmap
->ineq
[b
];
47 static void constraint_drop_vars(isl_int
*c
, unsigned n
, unsigned rem
)
49 isl_seq_cpy(c
, c
+ n
, rem
);
50 isl_seq_clr(c
+ rem
, n
);
53 /* Drop n dimensions starting at first.
55 * In principle, this frees up some extra variables as the number
56 * of columns remains constant, but we would have to extend
57 * the div array too as the number of rows in this array is assumed
58 * to be equal to extra.
60 struct isl_basic_set
*isl_basic_set_drop_dims(
61 struct isl_basic_set
*bset
, unsigned first
, unsigned n
)
68 isl_assert(bset
->ctx
, first
+ n
<= bset
->dim
->n_out
, goto error
);
70 if (n
== 0 && !isl_space_is_named_or_nested(bset
->dim
, isl_dim_set
))
73 bset
= isl_basic_set_cow(bset
);
77 for (i
= 0; i
< bset
->n_eq
; ++i
)
78 constraint_drop_vars(bset
->eq
[i
]+1+bset
->dim
->nparam
+first
, n
,
79 (bset
->dim
->n_out
-first
-n
)+bset
->extra
);
81 for (i
= 0; i
< bset
->n_ineq
; ++i
)
82 constraint_drop_vars(bset
->ineq
[i
]+1+bset
->dim
->nparam
+first
, n
,
83 (bset
->dim
->n_out
-first
-n
)+bset
->extra
);
85 for (i
= 0; i
< bset
->n_div
; ++i
)
86 constraint_drop_vars(bset
->div
[i
]+1+1+bset
->dim
->nparam
+first
, n
,
87 (bset
->dim
->n_out
-first
-n
)+bset
->extra
);
89 bset
->dim
= isl_space_drop_outputs(bset
->dim
, first
, n
);
93 ISL_F_CLR(bset
, ISL_BASIC_SET_NORMALIZED
);
94 bset
= isl_basic_set_simplify(bset
);
95 return isl_basic_set_finalize(bset
);
97 isl_basic_set_free(bset
);
101 struct isl_set
*isl_set_drop_dims(
102 struct isl_set
*set
, unsigned first
, unsigned n
)
109 isl_assert(set
->ctx
, first
+ n
<= set
->dim
->n_out
, goto error
);
111 if (n
== 0 && !isl_space_get_tuple_name(set
->dim
, isl_dim_set
))
113 set
= isl_set_cow(set
);
116 set
->dim
= isl_space_drop_outputs(set
->dim
, first
, n
);
120 for (i
= 0; i
< set
->n
; ++i
) {
121 set
->p
[i
] = isl_basic_set_drop_dims(set
->p
[i
], first
, n
);
126 ISL_F_CLR(set
, ISL_SET_NORMALIZED
);
133 /* Move "n" divs starting at "first" to the end of the list of divs.
135 static struct isl_basic_map
*move_divs_last(struct isl_basic_map
*bmap
,
136 unsigned first
, unsigned n
)
141 if (first
+ n
== bmap
->n_div
)
144 div
= isl_alloc_array(bmap
->ctx
, isl_int
*, n
);
147 for (i
= 0; i
< n
; ++i
)
148 div
[i
] = bmap
->div
[first
+ i
];
149 for (i
= 0; i
< bmap
->n_div
- first
- n
; ++i
)
150 bmap
->div
[first
+ i
] = bmap
->div
[first
+ n
+ i
];
151 for (i
= 0; i
< n
; ++i
)
152 bmap
->div
[bmap
->n_div
- n
+ i
] = div
[i
];
156 isl_basic_map_free(bmap
);
160 /* Drop "n" dimensions of type "type" starting at "first".
162 * In principle, this frees up some extra variables as the number
163 * of columns remains constant, but we would have to extend
164 * the div array too as the number of rows in this array is assumed
165 * to be equal to extra.
167 struct isl_basic_map
*isl_basic_map_drop(struct isl_basic_map
*bmap
,
168 enum isl_dim_type type
, unsigned first
, unsigned n
)
178 dim
= isl_basic_map_dim(bmap
, type
);
179 isl_assert(bmap
->ctx
, first
+ n
<= dim
, goto error
);
181 if (n
== 0 && !isl_space_is_named_or_nested(bmap
->dim
, type
))
184 bmap
= isl_basic_map_cow(bmap
);
188 offset
= isl_basic_map_offset(bmap
, type
) + first
;
189 left
= isl_basic_map_total_dim(bmap
) - (offset
- 1) - n
;
190 for (i
= 0; i
< bmap
->n_eq
; ++i
)
191 constraint_drop_vars(bmap
->eq
[i
]+offset
, n
, left
);
193 for (i
= 0; i
< bmap
->n_ineq
; ++i
)
194 constraint_drop_vars(bmap
->ineq
[i
]+offset
, n
, left
);
196 for (i
= 0; i
< bmap
->n_div
; ++i
)
197 constraint_drop_vars(bmap
->div
[i
]+1+offset
, n
, left
);
199 if (type
== isl_dim_div
) {
200 bmap
= move_divs_last(bmap
, first
, n
);
203 if (isl_basic_map_free_div(bmap
, n
) < 0)
204 return isl_basic_map_free(bmap
);
206 bmap
->dim
= isl_space_drop_dims(bmap
->dim
, type
, first
, n
);
210 ISL_F_CLR(bmap
, ISL_BASIC_MAP_NORMALIZED
);
211 bmap
= isl_basic_map_simplify(bmap
);
212 return isl_basic_map_finalize(bmap
);
214 isl_basic_map_free(bmap
);
218 __isl_give isl_basic_set
*isl_basic_set_drop(__isl_take isl_basic_set
*bset
,
219 enum isl_dim_type type
, unsigned first
, unsigned n
)
221 return bset_from_bmap(isl_basic_map_drop(bset_to_bmap(bset
),
225 struct isl_basic_map
*isl_basic_map_drop_inputs(
226 struct isl_basic_map
*bmap
, unsigned first
, unsigned n
)
228 return isl_basic_map_drop(bmap
, isl_dim_in
, first
, n
);
231 struct isl_map
*isl_map_drop(struct isl_map
*map
,
232 enum isl_dim_type type
, unsigned first
, unsigned n
)
239 isl_assert(map
->ctx
, first
+ n
<= isl_map_dim(map
, type
), goto error
);
241 if (n
== 0 && !isl_space_is_named_or_nested(map
->dim
, type
))
243 map
= isl_map_cow(map
);
246 map
->dim
= isl_space_drop_dims(map
->dim
, type
, first
, n
);
250 for (i
= 0; i
< map
->n
; ++i
) {
251 map
->p
[i
] = isl_basic_map_drop(map
->p
[i
], type
, first
, n
);
255 ISL_F_CLR(map
, ISL_MAP_NORMALIZED
);
263 struct isl_set
*isl_set_drop(struct isl_set
*set
,
264 enum isl_dim_type type
, unsigned first
, unsigned n
)
266 return set_from_map(isl_map_drop(set_to_map(set
), type
, first
, n
));
269 struct isl_map
*isl_map_drop_inputs(
270 struct isl_map
*map
, unsigned first
, unsigned n
)
272 return isl_map_drop(map
, isl_dim_in
, first
, n
);
276 * We don't cow, as the div is assumed to be redundant.
278 __isl_give isl_basic_map
*isl_basic_map_drop_div(
279 __isl_take isl_basic_map
*bmap
, unsigned div
)
287 pos
= 1 + isl_space_dim(bmap
->dim
, isl_dim_all
) + div
;
289 isl_assert(bmap
->ctx
, div
< bmap
->n_div
, goto error
);
291 for (i
= 0; i
< bmap
->n_eq
; ++i
)
292 constraint_drop_vars(bmap
->eq
[i
]+pos
, 1, bmap
->extra
-div
-1);
294 for (i
= 0; i
< bmap
->n_ineq
; ++i
) {
295 if (!isl_int_is_zero(bmap
->ineq
[i
][pos
])) {
296 isl_basic_map_drop_inequality(bmap
, i
);
300 constraint_drop_vars(bmap
->ineq
[i
]+pos
, 1, bmap
->extra
-div
-1);
303 for (i
= 0; i
< bmap
->n_div
; ++i
)
304 constraint_drop_vars(bmap
->div
[i
]+1+pos
, 1, bmap
->extra
-div
-1);
306 if (div
!= bmap
->n_div
- 1) {
308 isl_int
*t
= bmap
->div
[div
];
310 for (j
= div
; j
< bmap
->n_div
- 1; ++j
)
311 bmap
->div
[j
] = bmap
->div
[j
+1];
313 bmap
->div
[bmap
->n_div
- 1] = t
;
315 ISL_F_CLR(bmap
, ISL_BASIC_MAP_NORMALIZED
);
316 isl_basic_map_free_div(bmap
, 1);
320 isl_basic_map_free(bmap
);
324 struct isl_basic_map
*isl_basic_map_normalize_constraints(
325 struct isl_basic_map
*bmap
)
329 unsigned total
= isl_basic_map_total_dim(bmap
);
335 for (i
= bmap
->n_eq
- 1; i
>= 0; --i
) {
336 isl_seq_gcd(bmap
->eq
[i
]+1, total
, &gcd
);
337 if (isl_int_is_zero(gcd
)) {
338 if (!isl_int_is_zero(bmap
->eq
[i
][0])) {
339 bmap
= isl_basic_map_set_to_empty(bmap
);
342 isl_basic_map_drop_equality(bmap
, i
);
345 if (ISL_F_ISSET(bmap
, ISL_BASIC_MAP_RATIONAL
))
346 isl_int_gcd(gcd
, gcd
, bmap
->eq
[i
][0]);
347 if (isl_int_is_one(gcd
))
349 if (!isl_int_is_divisible_by(bmap
->eq
[i
][0], gcd
)) {
350 bmap
= isl_basic_map_set_to_empty(bmap
);
353 isl_seq_scale_down(bmap
->eq
[i
], bmap
->eq
[i
], gcd
, 1+total
);
356 for (i
= bmap
->n_ineq
- 1; i
>= 0; --i
) {
357 isl_seq_gcd(bmap
->ineq
[i
]+1, total
, &gcd
);
358 if (isl_int_is_zero(gcd
)) {
359 if (isl_int_is_neg(bmap
->ineq
[i
][0])) {
360 bmap
= isl_basic_map_set_to_empty(bmap
);
363 isl_basic_map_drop_inequality(bmap
, i
);
366 if (ISL_F_ISSET(bmap
, ISL_BASIC_MAP_RATIONAL
))
367 isl_int_gcd(gcd
, gcd
, bmap
->ineq
[i
][0]);
368 if (isl_int_is_one(gcd
))
370 isl_int_fdiv_q(bmap
->ineq
[i
][0], bmap
->ineq
[i
][0], gcd
);
371 isl_seq_scale_down(bmap
->ineq
[i
]+1, bmap
->ineq
[i
]+1, gcd
, total
);
378 struct isl_basic_set
*isl_basic_set_normalize_constraints(
379 struct isl_basic_set
*bset
)
381 isl_basic_map
*bmap
= bset_to_bmap(bset
);
382 return bset_from_bmap(isl_basic_map_normalize_constraints(bmap
));
385 /* Assuming the variable at position "pos" has an integer coefficient
386 * in integer division "div", extract it from this integer division.
387 * "pos" is as determined by isl_basic_map_offset, i.e., pos == 0
388 * corresponds to the constant term.
390 * That is, the integer division is of the form
392 * floor((... + c * d * x_pos + ...)/d)
396 * floor((... + 0 * x_pos + ...)/d) + c * x_pos
398 static __isl_give isl_basic_map
*remove_var_from_div(
399 __isl_take isl_basic_map
*bmap
, int div
, int pos
)
404 isl_int_divexact(shift
, bmap
->div
[div
][1 + pos
], bmap
->div
[div
][0]);
405 isl_int_neg(shift
, shift
);
406 bmap
= isl_basic_map_shift_div(bmap
, div
, pos
, shift
);
407 isl_int_clear(shift
);
412 /* Check if integer division "div" has any integral coefficient
413 * (or constant term). If so, extract them from the integer division.
415 static __isl_give isl_basic_map
*remove_independent_vars_from_div(
416 __isl_take isl_basic_map
*bmap
, int div
)
419 unsigned total
= 1 + isl_basic_map_total_dim(bmap
);
421 for (i
= 0; i
< total
; ++i
) {
422 if (isl_int_is_zero(bmap
->div
[div
][1 + i
]))
424 if (!isl_int_is_divisible_by(bmap
->div
[div
][1 + i
],
427 bmap
= remove_var_from_div(bmap
, div
, i
);
435 /* Check if any known integer division has any integral coefficient
436 * (or constant term). If so, extract them from the integer division.
438 static __isl_give isl_basic_map
*remove_independent_vars_from_divs(
439 __isl_take isl_basic_map
*bmap
)
445 if (bmap
->n_div
== 0)
448 for (i
= 0; i
< bmap
->n_div
; ++i
) {
449 if (isl_int_is_zero(bmap
->div
[i
][0]))
451 bmap
= remove_independent_vars_from_div(bmap
, i
);
459 /* Remove any common factor in numerator and denominator of the div expression,
460 * not taking into account the constant term.
461 * That is, if the div is of the form
463 * floor((a + m f(x))/(m d))
467 * floor((floor(a/m) + f(x))/d)
469 * The difference {a/m}/d in the argument satisfies 0 <= {a/m}/d < 1/d
470 * and can therefore not influence the result of the floor.
472 static void normalize_div_expression(__isl_keep isl_basic_map
*bmap
, int div
)
474 unsigned total
= isl_basic_map_total_dim(bmap
);
475 isl_ctx
*ctx
= bmap
->ctx
;
477 if (isl_int_is_zero(bmap
->div
[div
][0]))
479 isl_seq_gcd(bmap
->div
[div
] + 2, total
, &ctx
->normalize_gcd
);
480 isl_int_gcd(ctx
->normalize_gcd
, ctx
->normalize_gcd
, bmap
->div
[div
][0]);
481 if (isl_int_is_one(ctx
->normalize_gcd
))
483 isl_int_fdiv_q(bmap
->div
[div
][1], bmap
->div
[div
][1],
485 isl_int_divexact(bmap
->div
[div
][0], bmap
->div
[div
][0],
487 isl_seq_scale_down(bmap
->div
[div
] + 2, bmap
->div
[div
] + 2,
488 ctx
->normalize_gcd
, total
);
491 /* Remove any common factor in numerator and denominator of a div expression,
492 * not taking into account the constant term.
493 * That is, look for any div of the form
495 * floor((a + m f(x))/(m d))
499 * floor((floor(a/m) + f(x))/d)
501 * The difference {a/m}/d in the argument satisfies 0 <= {a/m}/d < 1/d
502 * and can therefore not influence the result of the floor.
504 static __isl_give isl_basic_map
*normalize_div_expressions(
505 __isl_take isl_basic_map
*bmap
)
511 if (bmap
->n_div
== 0)
514 for (i
= 0; i
< bmap
->n_div
; ++i
)
515 normalize_div_expression(bmap
, i
);
520 /* Assumes divs have been ordered if keep_divs is set.
522 static void eliminate_var_using_equality(struct isl_basic_map
*bmap
,
523 unsigned pos
, isl_int
*eq
, int keep_divs
, int *progress
)
526 unsigned space_total
;
530 total
= isl_basic_map_total_dim(bmap
);
531 space_total
= isl_space_dim(bmap
->dim
, isl_dim_all
);
532 last_div
= isl_seq_last_non_zero(eq
+ 1 + space_total
, bmap
->n_div
);
533 for (k
= 0; k
< bmap
->n_eq
; ++k
) {
534 if (bmap
->eq
[k
] == eq
)
536 if (isl_int_is_zero(bmap
->eq
[k
][1+pos
]))
540 isl_seq_elim(bmap
->eq
[k
], eq
, 1+pos
, 1+total
, NULL
);
541 isl_seq_normalize(bmap
->ctx
, bmap
->eq
[k
], 1 + total
);
544 for (k
= 0; k
< bmap
->n_ineq
; ++k
) {
545 if (isl_int_is_zero(bmap
->ineq
[k
][1+pos
]))
549 isl_seq_elim(bmap
->ineq
[k
], eq
, 1+pos
, 1+total
, NULL
);
550 isl_seq_normalize(bmap
->ctx
, bmap
->ineq
[k
], 1 + total
);
551 ISL_F_CLR(bmap
, ISL_BASIC_MAP_NORMALIZED
);
554 for (k
= 0; k
< bmap
->n_div
; ++k
) {
555 if (isl_int_is_zero(bmap
->div
[k
][0]))
557 if (isl_int_is_zero(bmap
->div
[k
][1+1+pos
]))
561 /* We need to be careful about circular definitions,
562 * so for now we just remove the definition of div k
563 * if the equality contains any divs.
564 * If keep_divs is set, then the divs have been ordered
565 * and we can keep the definition as long as the result
568 if (last_div
== -1 || (keep_divs
&& last_div
< k
)) {
569 isl_seq_elim(bmap
->div
[k
]+1, eq
,
570 1+pos
, 1+total
, &bmap
->div
[k
][0]);
571 normalize_div_expression(bmap
, k
);
573 isl_seq_clr(bmap
->div
[k
], 1 + total
);
574 ISL_F_CLR(bmap
, ISL_BASIC_MAP_NORMALIZED
);
578 /* Assumes divs have been ordered if keep_divs is set.
580 static __isl_give isl_basic_map
*eliminate_div(__isl_take isl_basic_map
*bmap
,
581 isl_int
*eq
, unsigned div
, int keep_divs
)
583 unsigned pos
= isl_space_dim(bmap
->dim
, isl_dim_all
) + div
;
585 eliminate_var_using_equality(bmap
, pos
, eq
, keep_divs
, NULL
);
587 bmap
= isl_basic_map_drop_div(bmap
, div
);
592 /* Check if elimination of div "div" using equality "eq" would not
593 * result in a div depending on a later div.
595 static int ok_to_eliminate_div(struct isl_basic_map
*bmap
, isl_int
*eq
,
600 unsigned space_total
= isl_space_dim(bmap
->dim
, isl_dim_all
);
601 unsigned pos
= space_total
+ div
;
603 last_div
= isl_seq_last_non_zero(eq
+ 1 + space_total
, bmap
->n_div
);
604 if (last_div
< 0 || last_div
<= div
)
607 for (k
= 0; k
<= last_div
; ++k
) {
608 if (isl_int_is_zero(bmap
->div
[k
][0]))
610 if (!isl_int_is_zero(bmap
->div
[k
][1 + 1 + pos
]))
617 /* Elimininate divs based on equalities
619 static struct isl_basic_map
*eliminate_divs_eq(
620 struct isl_basic_map
*bmap
, int *progress
)
627 bmap
= isl_basic_map_order_divs(bmap
);
632 off
= 1 + isl_space_dim(bmap
->dim
, isl_dim_all
);
634 for (d
= bmap
->n_div
- 1; d
>= 0 ; --d
) {
635 for (i
= 0; i
< bmap
->n_eq
; ++i
) {
636 if (!isl_int_is_one(bmap
->eq
[i
][off
+ d
]) &&
637 !isl_int_is_negone(bmap
->eq
[i
][off
+ d
]))
639 if (!ok_to_eliminate_div(bmap
, bmap
->eq
[i
], d
))
643 bmap
= eliminate_div(bmap
, bmap
->eq
[i
], d
, 1);
644 if (isl_basic_map_drop_equality(bmap
, i
) < 0)
645 return isl_basic_map_free(bmap
);
650 return eliminate_divs_eq(bmap
, progress
);
654 /* Elimininate divs based on inequalities
656 static struct isl_basic_map
*eliminate_divs_ineq(
657 struct isl_basic_map
*bmap
, int *progress
)
668 off
= 1 + isl_space_dim(bmap
->dim
, isl_dim_all
);
670 for (d
= bmap
->n_div
- 1; d
>= 0 ; --d
) {
671 for (i
= 0; i
< bmap
->n_eq
; ++i
)
672 if (!isl_int_is_zero(bmap
->eq
[i
][off
+ d
]))
676 for (i
= 0; i
< bmap
->n_ineq
; ++i
)
677 if (isl_int_abs_gt(bmap
->ineq
[i
][off
+ d
], ctx
->one
))
679 if (i
< bmap
->n_ineq
)
682 bmap
= isl_basic_map_eliminate_vars(bmap
, (off
-1)+d
, 1);
683 if (!bmap
|| ISL_F_ISSET(bmap
, ISL_BASIC_MAP_EMPTY
))
685 bmap
= isl_basic_map_drop_div(bmap
, d
);
692 struct isl_basic_map
*isl_basic_map_gauss(
693 struct isl_basic_map
*bmap
, int *progress
)
701 bmap
= isl_basic_map_order_divs(bmap
);
706 total
= isl_basic_map_total_dim(bmap
);
707 total_var
= total
- bmap
->n_div
;
709 last_var
= total
- 1;
710 for (done
= 0; done
< bmap
->n_eq
; ++done
) {
711 for (; last_var
>= 0; --last_var
) {
712 for (k
= done
; k
< bmap
->n_eq
; ++k
)
713 if (!isl_int_is_zero(bmap
->eq
[k
][1+last_var
]))
721 swap_equality(bmap
, k
, done
);
722 if (isl_int_is_neg(bmap
->eq
[done
][1+last_var
]))
723 isl_seq_neg(bmap
->eq
[done
], bmap
->eq
[done
], 1+total
);
725 eliminate_var_using_equality(bmap
, last_var
, bmap
->eq
[done
], 1,
728 if (last_var
>= total_var
&&
729 isl_int_is_zero(bmap
->div
[last_var
- total_var
][0])) {
730 unsigned div
= last_var
- total_var
;
731 isl_seq_neg(bmap
->div
[div
]+1, bmap
->eq
[done
], 1+total
);
732 isl_int_set_si(bmap
->div
[div
][1+1+last_var
], 0);
733 isl_int_set(bmap
->div
[div
][0],
734 bmap
->eq
[done
][1+last_var
]);
737 ISL_F_CLR(bmap
, ISL_BASIC_MAP_NORMALIZED
);
740 if (done
== bmap
->n_eq
)
742 for (k
= done
; k
< bmap
->n_eq
; ++k
) {
743 if (isl_int_is_zero(bmap
->eq
[k
][0]))
745 return isl_basic_map_set_to_empty(bmap
);
747 isl_basic_map_free_equality(bmap
, bmap
->n_eq
-done
);
751 struct isl_basic_set
*isl_basic_set_gauss(
752 struct isl_basic_set
*bset
, int *progress
)
754 return bset_from_bmap(isl_basic_map_gauss(bset_to_bmap(bset
),
759 static unsigned int round_up(unsigned int v
)
770 /* Hash table of inequalities in a basic map.
771 * "index" is an array of addresses of inequalities in the basic map, some
772 * of which are NULL. The inequalities are hashed on the coefficients
773 * except the constant term.
774 * "size" is the number of elements in the array and is always a power of two
775 * "bits" is the number of bits need to represent an index into the array.
776 * "total" is the total dimension of the basic map.
778 struct isl_constraint_index
{
785 /* Fill in the "ci" data structure for holding the inequalities of "bmap".
787 static isl_stat
create_constraint_index(struct isl_constraint_index
*ci
,
788 __isl_keep isl_basic_map
*bmap
)
794 return isl_stat_error
;
795 ci
->total
= isl_basic_set_total_dim(bmap
);
796 if (bmap
->n_ineq
== 0)
798 ci
->size
= round_up(4 * (bmap
->n_ineq
+ 1) / 3 - 1);
799 ci
->bits
= ffs(ci
->size
) - 1;
800 ctx
= isl_basic_map_get_ctx(bmap
);
801 ci
->index
= isl_calloc_array(ctx
, isl_int
**, ci
->size
);
803 return isl_stat_error
;
808 /* Free the memory allocated by create_constraint_index.
810 static void constraint_index_free(struct isl_constraint_index
*ci
)
815 /* Return the position in ci->index that contains the address of
816 * an inequality that is equal to *ineq up to the constant term,
817 * provided this address is not identical to "ineq".
818 * If there is no such inequality, then return the position where
819 * such an inequality should be inserted.
821 static int hash_index_ineq(struct isl_constraint_index
*ci
, isl_int
**ineq
)
824 uint32_t hash
= isl_seq_get_hash_bits((*ineq
) + 1, ci
->total
, ci
->bits
);
825 for (h
= hash
; ci
->index
[h
]; h
= (h
+1) % ci
->size
)
826 if (ineq
!= ci
->index
[h
] &&
827 isl_seq_eq((*ineq
) + 1, ci
->index
[h
][0]+1, ci
->total
))
832 /* Return the position in ci->index that contains the address of
833 * an inequality that is equal to the k'th inequality of "bmap"
834 * up to the constant term, provided it does not point to the very
836 * If there is no such inequality, then return the position where
837 * such an inequality should be inserted.
839 static int hash_index(struct isl_constraint_index
*ci
,
840 __isl_keep isl_basic_map
*bmap
, int k
)
842 return hash_index_ineq(ci
, &bmap
->ineq
[k
]);
845 static int set_hash_index(struct isl_constraint_index
*ci
,
846 struct isl_basic_set
*bset
, int k
)
848 return hash_index(ci
, bset
, k
);
851 /* Fill in the "ci" data structure with the inequalities of "bset".
853 static isl_stat
setup_constraint_index(struct isl_constraint_index
*ci
,
854 __isl_keep isl_basic_set
*bset
)
858 if (create_constraint_index(ci
, bset
) < 0)
859 return isl_stat_error
;
861 for (k
= 0; k
< bset
->n_ineq
; ++k
) {
862 h
= set_hash_index(ci
, bset
, k
);
863 ci
->index
[h
] = &bset
->ineq
[k
];
869 /* Is the inequality ineq (obviously) redundant with respect
870 * to the constraints in "ci"?
872 * Look for an inequality in "ci" with the same coefficients and then
873 * check if the contant term of "ineq" is greater than or equal
874 * to the constant term of that inequality. If so, "ineq" is clearly
877 * Note that hash_index_ineq ignores a stored constraint if it has
878 * the same address as the passed inequality. It is ok to pass
879 * the address of a local variable here since it will never be
880 * the same as the address of a constraint in "ci".
882 static isl_bool
constraint_index_is_redundant(struct isl_constraint_index
*ci
,
887 h
= hash_index_ineq(ci
, &ineq
);
889 return isl_bool_false
;
890 return isl_int_ge(ineq
[0], (*ci
->index
[h
])[0]);
893 /* If we can eliminate more than one div, then we need to make
894 * sure we do it from last div to first div, in order not to
895 * change the position of the other divs that still need to
898 static struct isl_basic_map
*remove_duplicate_divs(
899 struct isl_basic_map
*bmap
, int *progress
)
911 bmap
= isl_basic_map_order_divs(bmap
);
912 if (!bmap
|| bmap
->n_div
<= 1)
915 total_var
= isl_space_dim(bmap
->dim
, isl_dim_all
);
916 total
= total_var
+ bmap
->n_div
;
919 for (k
= bmap
->n_div
- 1; k
>= 0; --k
)
920 if (!isl_int_is_zero(bmap
->div
[k
][0]))
925 size
= round_up(4 * bmap
->n_div
/ 3 - 1);
928 elim_for
= isl_calloc_array(ctx
, int, bmap
->n_div
);
929 bits
= ffs(size
) - 1;
930 index
= isl_calloc_array(ctx
, int, size
);
931 if (!elim_for
|| !index
)
933 eq
= isl_blk_alloc(ctx
, 1+total
);
934 if (isl_blk_is_error(eq
))
937 isl_seq_clr(eq
.data
, 1+total
);
938 index
[isl_seq_get_hash_bits(bmap
->div
[k
], 2+total
, bits
)] = k
+ 1;
939 for (--k
; k
>= 0; --k
) {
942 if (isl_int_is_zero(bmap
->div
[k
][0]))
945 hash
= isl_seq_get_hash_bits(bmap
->div
[k
], 2+total
, bits
);
946 for (h
= hash
; index
[h
]; h
= (h
+1) % size
)
947 if (isl_seq_eq(bmap
->div
[k
],
948 bmap
->div
[index
[h
]-1], 2+total
))
957 for (l
= bmap
->n_div
- 1; l
>= 0; --l
) {
961 isl_int_set_si(eq
.data
[1+total_var
+k
], -1);
962 isl_int_set_si(eq
.data
[1+total_var
+l
], 1);
963 bmap
= eliminate_div(bmap
, eq
.data
, l
, 1);
966 isl_int_set_si(eq
.data
[1+total_var
+k
], 0);
967 isl_int_set_si(eq
.data
[1+total_var
+l
], 0);
970 isl_blk_free(ctx
, eq
);
977 static int n_pure_div_eq(struct isl_basic_map
*bmap
)
982 total
= isl_space_dim(bmap
->dim
, isl_dim_all
);
983 for (i
= 0, j
= bmap
->n_div
-1; i
< bmap
->n_eq
; ++i
) {
984 while (j
>= 0 && isl_int_is_zero(bmap
->eq
[i
][1 + total
+ j
]))
988 if (isl_seq_first_non_zero(bmap
->eq
[i
] + 1 + total
, j
) != -1)
994 /* Normalize divs that appear in equalities.
996 * In particular, we assume that bmap contains some equalities
1001 * and we want to replace the set of e_i by a minimal set and
1002 * such that the new e_i have a canonical representation in terms
1004 * If any of the equalities involves more than one divs, then
1005 * we currently simply bail out.
1007 * Let us first additionally assume that all equalities involve
1008 * a div. The equalities then express modulo constraints on the
1009 * remaining variables and we can use "parameter compression"
1010 * to find a minimal set of constraints. The result is a transformation
1012 * x = T(x') = x_0 + G x'
1014 * with G a lower-triangular matrix with all elements below the diagonal
1015 * non-negative and smaller than the diagonal element on the same row.
1016 * We first normalize x_0 by making the same property hold in the affine
1018 * The rows i of G with a 1 on the diagonal do not impose any modulo
1019 * constraint and simply express x_i = x'_i.
1020 * For each of the remaining rows i, we introduce a div and a corresponding
1021 * equality. In particular
1023 * g_ii e_j = x_i - g_i(x')
1025 * where each x'_k is replaced either by x_k (if g_kk = 1) or the
1026 * corresponding div (if g_kk != 1).
1028 * If there are any equalities not involving any div, then we
1029 * first apply a variable compression on the variables x:
1031 * x = C x'' x'' = C_2 x
1033 * and perform the above parameter compression on A C instead of on A.
1034 * The resulting compression is then of the form
1036 * x'' = T(x') = x_0 + G x'
1038 * and in constructing the new divs and the corresponding equalities,
1039 * we have to replace each x'', i.e., the x'_k with (g_kk = 1),
1040 * by the corresponding row from C_2.
1042 static struct isl_basic_map
*normalize_divs(
1043 struct isl_basic_map
*bmap
, int *progress
)
1050 struct isl_mat
*T
= NULL
;
1051 struct isl_mat
*C
= NULL
;
1052 struct isl_mat
*C2
= NULL
;
1055 int dropped
, needed
;
1060 if (bmap
->n_div
== 0)
1063 if (bmap
->n_eq
== 0)
1066 if (ISL_F_ISSET(bmap
, ISL_BASIC_MAP_NORMALIZED_DIVS
))
1069 total
= isl_space_dim(bmap
->dim
, isl_dim_all
);
1070 div_eq
= n_pure_div_eq(bmap
);
1074 if (div_eq
< bmap
->n_eq
) {
1075 B
= isl_mat_sub_alloc6(bmap
->ctx
, bmap
->eq
, div_eq
,
1076 bmap
->n_eq
- div_eq
, 0, 1 + total
);
1077 C
= isl_mat_variable_compression(B
, &C2
);
1080 if (C
->n_col
== 0) {
1081 bmap
= isl_basic_map_set_to_empty(bmap
);
1088 d
= isl_vec_alloc(bmap
->ctx
, div_eq
);
1091 for (i
= 0, j
= bmap
->n_div
-1; i
< div_eq
; ++i
) {
1092 while (j
>= 0 && isl_int_is_zero(bmap
->eq
[i
][1 + total
+ j
]))
1094 isl_int_set(d
->block
.data
[i
], bmap
->eq
[i
][1 + total
+ j
]);
1096 B
= isl_mat_sub_alloc6(bmap
->ctx
, bmap
->eq
, 0, div_eq
, 0, 1 + total
);
1099 B
= isl_mat_product(B
, C
);
1103 T
= isl_mat_parameter_compression(B
, d
);
1106 if (T
->n_col
== 0) {
1107 bmap
= isl_basic_map_set_to_empty(bmap
);
1113 for (i
= 0; i
< T
->n_row
- 1; ++i
) {
1114 isl_int_fdiv_q(v
, T
->row
[1 + i
][0], T
->row
[1 + i
][1 + i
]);
1115 if (isl_int_is_zero(v
))
1117 isl_mat_col_submul(T
, 0, v
, 1 + i
);
1120 pos
= isl_alloc_array(bmap
->ctx
, int, T
->n_row
);
1123 /* We have to be careful because dropping equalities may reorder them */
1125 for (j
= bmap
->n_div
- 1; j
>= 0; --j
) {
1126 for (i
= 0; i
< bmap
->n_eq
; ++i
)
1127 if (!isl_int_is_zero(bmap
->eq
[i
][1 + total
+ j
]))
1129 if (i
< bmap
->n_eq
) {
1130 bmap
= isl_basic_map_drop_div(bmap
, j
);
1131 isl_basic_map_drop_equality(bmap
, i
);
1137 for (i
= 1; i
< T
->n_row
; ++i
) {
1138 if (isl_int_is_one(T
->row
[i
][i
]))
1143 if (needed
> dropped
) {
1144 bmap
= isl_basic_map_extend_space(bmap
, isl_space_copy(bmap
->dim
),
1149 for (i
= 1; i
< T
->n_row
; ++i
) {
1150 if (isl_int_is_one(T
->row
[i
][i
]))
1152 k
= isl_basic_map_alloc_div(bmap
);
1153 pos
[i
] = 1 + total
+ k
;
1154 isl_seq_clr(bmap
->div
[k
] + 1, 1 + total
+ bmap
->n_div
);
1155 isl_int_set(bmap
->div
[k
][0], T
->row
[i
][i
]);
1157 isl_seq_cpy(bmap
->div
[k
] + 1, C2
->row
[i
], 1 + total
);
1159 isl_int_set_si(bmap
->div
[k
][1 + i
], 1);
1160 for (j
= 0; j
< i
; ++j
) {
1161 if (isl_int_is_zero(T
->row
[i
][j
]))
1163 if (pos
[j
] < T
->n_row
&& C2
)
1164 isl_seq_submul(bmap
->div
[k
] + 1, T
->row
[i
][j
],
1165 C2
->row
[pos
[j
]], 1 + total
);
1167 isl_int_neg(bmap
->div
[k
][1 + pos
[j
]],
1170 j
= isl_basic_map_alloc_equality(bmap
);
1171 isl_seq_neg(bmap
->eq
[j
], bmap
->div
[k
]+1, 1+total
+bmap
->n_div
);
1172 isl_int_set(bmap
->eq
[j
][pos
[i
]], bmap
->div
[k
][0]);
1181 ISL_F_SET(bmap
, ISL_BASIC_MAP_NORMALIZED_DIVS
);
1192 static struct isl_basic_map
*set_div_from_lower_bound(
1193 struct isl_basic_map
*bmap
, int div
, int ineq
)
1195 unsigned total
= 1 + isl_space_dim(bmap
->dim
, isl_dim_all
);
1197 isl_seq_neg(bmap
->div
[div
] + 1, bmap
->ineq
[ineq
], total
+ bmap
->n_div
);
1198 isl_int_set(bmap
->div
[div
][0], bmap
->ineq
[ineq
][total
+ div
]);
1199 isl_int_add(bmap
->div
[div
][1], bmap
->div
[div
][1], bmap
->div
[div
][0]);
1200 isl_int_sub_ui(bmap
->div
[div
][1], bmap
->div
[div
][1], 1);
1201 isl_int_set_si(bmap
->div
[div
][1 + total
+ div
], 0);
1206 /* Check whether it is ok to define a div based on an inequality.
1207 * To avoid the introduction of circular definitions of divs, we
1208 * do not allow such a definition if the resulting expression would refer to
1209 * any other undefined divs or if any known div is defined in
1210 * terms of the unknown div.
1212 static int ok_to_set_div_from_bound(struct isl_basic_map
*bmap
,
1216 unsigned total
= 1 + isl_space_dim(bmap
->dim
, isl_dim_all
);
1218 /* Not defined in terms of unknown divs */
1219 for (j
= 0; j
< bmap
->n_div
; ++j
) {
1222 if (isl_int_is_zero(bmap
->ineq
[ineq
][total
+ j
]))
1224 if (isl_int_is_zero(bmap
->div
[j
][0]))
1228 /* No other div defined in terms of this one => avoid loops */
1229 for (j
= 0; j
< bmap
->n_div
; ++j
) {
1232 if (isl_int_is_zero(bmap
->div
[j
][0]))
1234 if (!isl_int_is_zero(bmap
->div
[j
][1 + total
+ div
]))
1241 /* Would an expression for div "div" based on inequality "ineq" of "bmap"
1242 * be a better expression than the current one?
1244 * If we do not have any expression yet, then any expression would be better.
1245 * Otherwise we check if the last variable involved in the inequality
1246 * (disregarding the div that it would define) is in an earlier position
1247 * than the last variable involved in the current div expression.
1249 static int better_div_constraint(__isl_keep isl_basic_map
*bmap
,
1252 unsigned total
= 1 + isl_space_dim(bmap
->dim
, isl_dim_all
);
1256 if (isl_int_is_zero(bmap
->div
[div
][0]))
1259 if (isl_seq_last_non_zero(bmap
->ineq
[ineq
] + total
+ div
+ 1,
1260 bmap
->n_div
- (div
+ 1)) >= 0)
1263 last_ineq
= isl_seq_last_non_zero(bmap
->ineq
[ineq
], total
+ div
);
1264 last_div
= isl_seq_last_non_zero(bmap
->div
[div
] + 1,
1265 total
+ bmap
->n_div
);
1267 return last_ineq
< last_div
;
1270 /* Given two constraints "k" and "l" that are opposite to each other,
1271 * except for the constant term, check if we can use them
1272 * to obtain an expression for one of the hitherto unknown divs or
1273 * a "better" expression for a div for which we already have an expression.
1274 * "sum" is the sum of the constant terms of the constraints.
1275 * If this sum is strictly smaller than the coefficient of one
1276 * of the divs, then this pair can be used define the div.
1277 * To avoid the introduction of circular definitions of divs, we
1278 * do not use the pair if the resulting expression would refer to
1279 * any other undefined divs or if any known div is defined in
1280 * terms of the unknown div.
1282 static struct isl_basic_map
*check_for_div_constraints(
1283 struct isl_basic_map
*bmap
, int k
, int l
, isl_int sum
, int *progress
)
1286 unsigned total
= 1 + isl_space_dim(bmap
->dim
, isl_dim_all
);
1288 for (i
= 0; i
< bmap
->n_div
; ++i
) {
1289 if (isl_int_is_zero(bmap
->ineq
[k
][total
+ i
]))
1291 if (isl_int_abs_ge(sum
, bmap
->ineq
[k
][total
+ i
]))
1293 if (!better_div_constraint(bmap
, i
, k
))
1295 if (!ok_to_set_div_from_bound(bmap
, i
, k
))
1297 if (isl_int_is_pos(bmap
->ineq
[k
][total
+ i
]))
1298 bmap
= set_div_from_lower_bound(bmap
, i
, k
);
1300 bmap
= set_div_from_lower_bound(bmap
, i
, l
);
1308 __isl_give isl_basic_map
*isl_basic_map_remove_duplicate_constraints(
1309 __isl_take isl_basic_map
*bmap
, int *progress
, int detect_divs
)
1311 struct isl_constraint_index ci
;
1313 unsigned total
= isl_basic_map_total_dim(bmap
);
1316 if (!bmap
|| bmap
->n_ineq
<= 1)
1319 if (create_constraint_index(&ci
, bmap
) < 0)
1322 h
= isl_seq_get_hash_bits(bmap
->ineq
[0] + 1, total
, ci
.bits
);
1323 ci
.index
[h
] = &bmap
->ineq
[0];
1324 for (k
= 1; k
< bmap
->n_ineq
; ++k
) {
1325 h
= hash_index(&ci
, bmap
, k
);
1327 ci
.index
[h
] = &bmap
->ineq
[k
];
1332 l
= ci
.index
[h
] - &bmap
->ineq
[0];
1333 if (isl_int_lt(bmap
->ineq
[k
][0], bmap
->ineq
[l
][0]))
1334 swap_inequality(bmap
, k
, l
);
1335 isl_basic_map_drop_inequality(bmap
, k
);
1339 for (k
= 0; k
< bmap
->n_ineq
-1; ++k
) {
1340 isl_seq_neg(bmap
->ineq
[k
]+1, bmap
->ineq
[k
]+1, total
);
1341 h
= hash_index(&ci
, bmap
, k
);
1342 isl_seq_neg(bmap
->ineq
[k
]+1, bmap
->ineq
[k
]+1, total
);
1345 l
= ci
.index
[h
] - &bmap
->ineq
[0];
1346 isl_int_add(sum
, bmap
->ineq
[k
][0], bmap
->ineq
[l
][0]);
1347 if (isl_int_is_pos(sum
)) {
1349 bmap
= check_for_div_constraints(bmap
, k
, l
,
1353 if (isl_int_is_zero(sum
)) {
1354 /* We need to break out of the loop after these
1355 * changes since the contents of the hash
1356 * will no longer be valid.
1357 * Plus, we probably we want to regauss first.
1361 isl_basic_map_drop_inequality(bmap
, l
);
1362 isl_basic_map_inequality_to_equality(bmap
, k
);
1364 bmap
= isl_basic_map_set_to_empty(bmap
);
1369 constraint_index_free(&ci
);
1373 /* Detect all pairs of inequalities that form an equality.
1375 * isl_basic_map_remove_duplicate_constraints detects at most one such pair.
1376 * Call it repeatedly while it is making progress.
1378 __isl_give isl_basic_map
*isl_basic_map_detect_inequality_pairs(
1379 __isl_take isl_basic_map
*bmap
, int *progress
)
1385 bmap
= isl_basic_map_remove_duplicate_constraints(bmap
,
1387 if (progress
&& duplicate
)
1389 } while (duplicate
);
1394 /* Eliminate knowns divs from constraints where they appear with
1395 * a (positive or negative) unit coefficient.
1399 * floor(e/m) + f >= 0
1407 * -floor(e/m) + f >= 0
1411 * -e + m f + m - 1 >= 0
1413 * The first conversion is valid because floor(e/m) >= -f is equivalent
1414 * to e/m >= -f because -f is an integral expression.
1415 * The second conversion follows from the fact that
1417 * -floor(e/m) = ceil(-e/m) = floor((-e + m - 1)/m)
1420 * Note that one of the div constraints may have been eliminated
1421 * due to being redundant with respect to the constraint that is
1422 * being modified by this function. The modified constraint may
1423 * no longer imply this div constraint, so we add it back to make
1424 * sure we do not lose any information.
1426 * We skip integral divs, i.e., those with denominator 1, as we would
1427 * risk eliminating the div from the div constraints. We do not need
1428 * to handle those divs here anyway since the div constraints will turn
1429 * out to form an equality and this equality can then be used to eliminate
1430 * the div from all constraints.
1432 static __isl_give isl_basic_map
*eliminate_unit_divs(
1433 __isl_take isl_basic_map
*bmap
, int *progress
)
1442 ctx
= isl_basic_map_get_ctx(bmap
);
1443 total
= 1 + isl_space_dim(bmap
->dim
, isl_dim_all
);
1445 for (i
= 0; i
< bmap
->n_div
; ++i
) {
1446 if (isl_int_is_zero(bmap
->div
[i
][0]))
1448 if (isl_int_is_one(bmap
->div
[i
][0]))
1450 for (j
= 0; j
< bmap
->n_ineq
; ++j
) {
1453 if (!isl_int_is_one(bmap
->ineq
[j
][total
+ i
]) &&
1454 !isl_int_is_negone(bmap
->ineq
[j
][total
+ i
]))
1459 s
= isl_int_sgn(bmap
->ineq
[j
][total
+ i
]);
1460 isl_int_set_si(bmap
->ineq
[j
][total
+ i
], 0);
1462 isl_seq_combine(bmap
->ineq
[j
],
1463 ctx
->negone
, bmap
->div
[i
] + 1,
1464 bmap
->div
[i
][0], bmap
->ineq
[j
],
1465 total
+ bmap
->n_div
);
1467 isl_seq_combine(bmap
->ineq
[j
],
1468 ctx
->one
, bmap
->div
[i
] + 1,
1469 bmap
->div
[i
][0], bmap
->ineq
[j
],
1470 total
+ bmap
->n_div
);
1472 isl_int_add(bmap
->ineq
[j
][0],
1473 bmap
->ineq
[j
][0], bmap
->div
[i
][0]);
1474 isl_int_sub_ui(bmap
->ineq
[j
][0],
1475 bmap
->ineq
[j
][0], 1);
1478 bmap
= isl_basic_map_extend_constraints(bmap
, 0, 1);
1479 if (isl_basic_map_add_div_constraint(bmap
, i
, s
) < 0)
1480 return isl_basic_map_free(bmap
);
1487 struct isl_basic_map
*isl_basic_map_simplify(struct isl_basic_map
*bmap
)
1496 empty
= isl_basic_map_plain_is_empty(bmap
);
1498 return isl_basic_map_free(bmap
);
1501 bmap
= isl_basic_map_normalize_constraints(bmap
);
1502 bmap
= remove_independent_vars_from_divs(bmap
);
1503 bmap
= normalize_div_expressions(bmap
);
1504 bmap
= remove_duplicate_divs(bmap
, &progress
);
1505 bmap
= eliminate_unit_divs(bmap
, &progress
);
1506 bmap
= eliminate_divs_eq(bmap
, &progress
);
1507 bmap
= eliminate_divs_ineq(bmap
, &progress
);
1508 bmap
= isl_basic_map_gauss(bmap
, &progress
);
1509 /* requires equalities in normal form */
1510 bmap
= normalize_divs(bmap
, &progress
);
1511 bmap
= isl_basic_map_remove_duplicate_constraints(bmap
,
1513 if (bmap
&& progress
)
1514 ISL_F_CLR(bmap
, ISL_BASIC_MAP_REDUCED_COEFFICIENTS
);
1519 struct isl_basic_set
*isl_basic_set_simplify(struct isl_basic_set
*bset
)
1521 return bset_from_bmap(isl_basic_map_simplify(bset_to_bmap(bset
)));
1525 int isl_basic_map_is_div_constraint(__isl_keep isl_basic_map
*bmap
,
1526 isl_int
*constraint
, unsigned div
)
1533 pos
= 1 + isl_space_dim(bmap
->dim
, isl_dim_all
) + div
;
1535 if (isl_int_eq(constraint
[pos
], bmap
->div
[div
][0])) {
1537 isl_int_sub(bmap
->div
[div
][1],
1538 bmap
->div
[div
][1], bmap
->div
[div
][0]);
1539 isl_int_add_ui(bmap
->div
[div
][1], bmap
->div
[div
][1], 1);
1540 neg
= isl_seq_is_neg(constraint
, bmap
->div
[div
]+1, pos
);
1541 isl_int_sub_ui(bmap
->div
[div
][1], bmap
->div
[div
][1], 1);
1542 isl_int_add(bmap
->div
[div
][1],
1543 bmap
->div
[div
][1], bmap
->div
[div
][0]);
1546 if (isl_seq_first_non_zero(constraint
+pos
+1,
1547 bmap
->n_div
-div
-1) != -1)
1549 } else if (isl_int_abs_eq(constraint
[pos
], bmap
->div
[div
][0])) {
1550 if (!isl_seq_eq(constraint
, bmap
->div
[div
]+1, pos
))
1552 if (isl_seq_first_non_zero(constraint
+pos
+1,
1553 bmap
->n_div
-div
-1) != -1)
1561 int isl_basic_set_is_div_constraint(__isl_keep isl_basic_set
*bset
,
1562 isl_int
*constraint
, unsigned div
)
1564 return isl_basic_map_is_div_constraint(bset
, constraint
, div
);
1568 /* If the only constraints a div d=floor(f/m)
1569 * appears in are its two defining constraints
1572 * -(f - (m - 1)) + m d >= 0
1574 * then it can safely be removed.
1576 static int div_is_redundant(struct isl_basic_map
*bmap
, int div
)
1579 unsigned pos
= 1 + isl_space_dim(bmap
->dim
, isl_dim_all
) + div
;
1581 for (i
= 0; i
< bmap
->n_eq
; ++i
)
1582 if (!isl_int_is_zero(bmap
->eq
[i
][pos
]))
1585 for (i
= 0; i
< bmap
->n_ineq
; ++i
) {
1586 if (isl_int_is_zero(bmap
->ineq
[i
][pos
]))
1588 if (!isl_basic_map_is_div_constraint(bmap
, bmap
->ineq
[i
], div
))
1592 for (i
= 0; i
< bmap
->n_div
; ++i
) {
1593 if (isl_int_is_zero(bmap
->div
[i
][0]))
1595 if (!isl_int_is_zero(bmap
->div
[i
][1+pos
]))
1603 * Remove divs that don't occur in any of the constraints or other divs.
1604 * These can arise when dropping constraints from a basic map or
1605 * when the divs of a basic map have been temporarily aligned
1606 * with the divs of another basic map.
1608 static struct isl_basic_map
*remove_redundant_divs(struct isl_basic_map
*bmap
)
1615 for (i
= bmap
->n_div
-1; i
>= 0; --i
) {
1616 if (!div_is_redundant(bmap
, i
))
1618 bmap
= isl_basic_map_drop_div(bmap
, i
);
1623 /* Mark "bmap" as final, without checking for obviously redundant
1624 * integer divisions. This function should be used when "bmap"
1625 * is known not to involve any such integer divisions.
1627 __isl_give isl_basic_map
*isl_basic_map_mark_final(
1628 __isl_take isl_basic_map
*bmap
)
1632 ISL_F_SET(bmap
, ISL_BASIC_SET_FINAL
);
1636 /* Mark "bmap" as final, after removing obviously redundant integer divisions.
1638 struct isl_basic_map
*isl_basic_map_finalize(struct isl_basic_map
*bmap
)
1640 bmap
= remove_redundant_divs(bmap
);
1641 bmap
= isl_basic_map_mark_final(bmap
);
1645 struct isl_basic_set
*isl_basic_set_finalize(struct isl_basic_set
*bset
)
1647 return bset_from_bmap(isl_basic_map_finalize(bset_to_bmap(bset
)));
1650 struct isl_set
*isl_set_finalize(struct isl_set
*set
)
1656 for (i
= 0; i
< set
->n
; ++i
) {
1657 set
->p
[i
] = isl_basic_set_finalize(set
->p
[i
]);
1667 struct isl_map
*isl_map_finalize(struct isl_map
*map
)
1673 for (i
= 0; i
< map
->n
; ++i
) {
1674 map
->p
[i
] = isl_basic_map_finalize(map
->p
[i
]);
1678 ISL_F_CLR(map
, ISL_MAP_NORMALIZED
);
1686 /* Remove definition of any div that is defined in terms of the given variable.
1687 * The div itself is not removed. Functions such as
1688 * eliminate_divs_ineq depend on the other divs remaining in place.
1690 static struct isl_basic_map
*remove_dependent_vars(struct isl_basic_map
*bmap
,
1698 for (i
= 0; i
< bmap
->n_div
; ++i
) {
1699 if (isl_int_is_zero(bmap
->div
[i
][0]))
1701 if (isl_int_is_zero(bmap
->div
[i
][1+1+pos
]))
1703 bmap
= isl_basic_map_mark_div_unknown(bmap
, i
);
1710 /* Eliminate the specified variables from the constraints using
1711 * Fourier-Motzkin. The variables themselves are not removed.
1713 struct isl_basic_map
*isl_basic_map_eliminate_vars(
1714 struct isl_basic_map
*bmap
, unsigned pos
, unsigned n
)
1725 total
= isl_basic_map_total_dim(bmap
);
1727 bmap
= isl_basic_map_cow(bmap
);
1728 for (d
= pos
+ n
- 1; d
>= 0 && d
>= pos
; --d
)
1729 bmap
= remove_dependent_vars(bmap
, d
);
1733 for (d
= pos
+ n
- 1;
1734 d
>= 0 && d
>= total
- bmap
->n_div
&& d
>= pos
; --d
)
1735 isl_seq_clr(bmap
->div
[d
-(total
-bmap
->n_div
)], 2+total
);
1736 for (d
= pos
+ n
- 1; d
>= 0 && d
>= pos
; --d
) {
1737 int n_lower
, n_upper
;
1740 for (i
= 0; i
< bmap
->n_eq
; ++i
) {
1741 if (isl_int_is_zero(bmap
->eq
[i
][1+d
]))
1743 eliminate_var_using_equality(bmap
, d
, bmap
->eq
[i
], 0, NULL
);
1744 isl_basic_map_drop_equality(bmap
, i
);
1752 for (i
= 0; i
< bmap
->n_ineq
; ++i
) {
1753 if (isl_int_is_pos(bmap
->ineq
[i
][1+d
]))
1755 else if (isl_int_is_neg(bmap
->ineq
[i
][1+d
]))
1758 bmap
= isl_basic_map_extend_constraints(bmap
,
1759 0, n_lower
* n_upper
);
1762 for (i
= bmap
->n_ineq
- 1; i
>= 0; --i
) {
1764 if (isl_int_is_zero(bmap
->ineq
[i
][1+d
]))
1767 for (j
= 0; j
< i
; ++j
) {
1768 if (isl_int_is_zero(bmap
->ineq
[j
][1+d
]))
1771 if (isl_int_sgn(bmap
->ineq
[i
][1+d
]) ==
1772 isl_int_sgn(bmap
->ineq
[j
][1+d
]))
1774 k
= isl_basic_map_alloc_inequality(bmap
);
1777 isl_seq_cpy(bmap
->ineq
[k
], bmap
->ineq
[i
],
1779 isl_seq_elim(bmap
->ineq
[k
], bmap
->ineq
[j
],
1780 1+d
, 1+total
, NULL
);
1782 isl_basic_map_drop_inequality(bmap
, i
);
1785 if (n_lower
> 0 && n_upper
> 0) {
1786 bmap
= isl_basic_map_normalize_constraints(bmap
);
1787 bmap
= isl_basic_map_remove_duplicate_constraints(bmap
,
1789 bmap
= isl_basic_map_gauss(bmap
, NULL
);
1790 bmap
= isl_basic_map_remove_redundancies(bmap
);
1794 if (ISL_F_ISSET(bmap
, ISL_BASIC_MAP_EMPTY
))
1798 ISL_F_CLR(bmap
, ISL_BASIC_MAP_NORMALIZED
);
1800 bmap
= isl_basic_map_gauss(bmap
, NULL
);
1803 isl_basic_map_free(bmap
);
1807 struct isl_basic_set
*isl_basic_set_eliminate_vars(
1808 struct isl_basic_set
*bset
, unsigned pos
, unsigned n
)
1810 return bset_from_bmap(isl_basic_map_eliminate_vars(bset_to_bmap(bset
),
1814 /* Eliminate the specified n dimensions starting at first from the
1815 * constraints, without removing the dimensions from the space.
1816 * If the set is rational, the dimensions are eliminated using Fourier-Motzkin.
1817 * Otherwise, they are projected out and the original space is restored.
1819 __isl_give isl_basic_map
*isl_basic_map_eliminate(
1820 __isl_take isl_basic_map
*bmap
,
1821 enum isl_dim_type type
, unsigned first
, unsigned n
)
1830 if (first
+ n
> isl_basic_map_dim(bmap
, type
) || first
+ n
< first
)
1831 isl_die(bmap
->ctx
, isl_error_invalid
,
1832 "index out of bounds", goto error
);
1834 if (ISL_F_ISSET(bmap
, ISL_BASIC_MAP_RATIONAL
)) {
1835 first
+= isl_basic_map_offset(bmap
, type
) - 1;
1836 bmap
= isl_basic_map_eliminate_vars(bmap
, first
, n
);
1837 return isl_basic_map_finalize(bmap
);
1840 space
= isl_basic_map_get_space(bmap
);
1841 bmap
= isl_basic_map_project_out(bmap
, type
, first
, n
);
1842 bmap
= isl_basic_map_insert_dims(bmap
, type
, first
, n
);
1843 bmap
= isl_basic_map_reset_space(bmap
, space
);
1846 isl_basic_map_free(bmap
);
1850 __isl_give isl_basic_set
*isl_basic_set_eliminate(
1851 __isl_take isl_basic_set
*bset
,
1852 enum isl_dim_type type
, unsigned first
, unsigned n
)
1854 return isl_basic_map_eliminate(bset
, type
, first
, n
);
1857 /* Remove all constraints from "bmap" that reference any unknown local
1858 * variables (directly or indirectly).
1860 * Dropping all constraints on a local variable will make it redundant,
1861 * so it will get removed implicitly by
1862 * isl_basic_map_drop_constraints_involving_dims. Some other local
1863 * variables may also end up becoming redundant if they only appear
1864 * in constraints together with the unknown local variable.
1865 * Therefore, start over after calling
1866 * isl_basic_map_drop_constraints_involving_dims.
1868 __isl_give isl_basic_map
*isl_basic_map_drop_constraint_involving_unknown_divs(
1869 __isl_take isl_basic_map
*bmap
)
1872 int i
, n_div
, o_div
;
1874 known
= isl_basic_map_divs_known(bmap
);
1876 return isl_basic_map_free(bmap
);
1880 n_div
= isl_basic_map_dim(bmap
, isl_dim_div
);
1881 o_div
= isl_basic_map_offset(bmap
, isl_dim_div
) - 1;
1883 for (i
= 0; i
< n_div
; ++i
) {
1884 known
= isl_basic_map_div_is_known(bmap
, i
);
1886 return isl_basic_map_free(bmap
);
1889 bmap
= remove_dependent_vars(bmap
, o_div
+ i
);
1890 bmap
= isl_basic_map_drop_constraints_involving_dims(bmap
,
1894 n_div
= isl_basic_map_dim(bmap
, isl_dim_div
);
1901 /* Remove all constraints from "map" that reference any unknown local
1902 * variables (directly or indirectly).
1904 * Since constraints may get dropped from the basic maps,
1905 * they may no longer be disjoint from each other.
1907 __isl_give isl_map
*isl_map_drop_constraint_involving_unknown_divs(
1908 __isl_take isl_map
*map
)
1913 known
= isl_map_divs_known(map
);
1915 return isl_map_free(map
);
1919 map
= isl_map_cow(map
);
1923 for (i
= 0; i
< map
->n
; ++i
) {
1925 isl_basic_map_drop_constraint_involving_unknown_divs(
1928 return isl_map_free(map
);
1932 ISL_F_CLR(map
, ISL_MAP_DISJOINT
);
1937 /* Don't assume equalities are in order, because align_divs
1938 * may have changed the order of the divs.
1940 static void compute_elimination_index(struct isl_basic_map
*bmap
, int *elim
)
1945 total
= isl_space_dim(bmap
->dim
, isl_dim_all
);
1946 for (d
= 0; d
< total
; ++d
)
1948 for (i
= 0; i
< bmap
->n_eq
; ++i
) {
1949 for (d
= total
- 1; d
>= 0; --d
) {
1950 if (isl_int_is_zero(bmap
->eq
[i
][1+d
]))
1958 static void set_compute_elimination_index(struct isl_basic_set
*bset
, int *elim
)
1960 compute_elimination_index(bset_to_bmap(bset
), elim
);
1963 static int reduced_using_equalities(isl_int
*dst
, isl_int
*src
,
1964 struct isl_basic_map
*bmap
, int *elim
)
1970 total
= isl_space_dim(bmap
->dim
, isl_dim_all
);
1971 for (d
= total
- 1; d
>= 0; --d
) {
1972 if (isl_int_is_zero(src
[1+d
]))
1977 isl_seq_cpy(dst
, src
, 1 + total
);
1980 isl_seq_elim(dst
, bmap
->eq
[elim
[d
]], 1 + d
, 1 + total
, NULL
);
1985 static int set_reduced_using_equalities(isl_int
*dst
, isl_int
*src
,
1986 struct isl_basic_set
*bset
, int *elim
)
1988 return reduced_using_equalities(dst
, src
,
1989 bset_to_bmap(bset
), elim
);
1992 static struct isl_basic_set
*isl_basic_set_reduce_using_equalities(
1993 struct isl_basic_set
*bset
, struct isl_basic_set
*context
)
1998 if (!bset
|| !context
)
2001 if (context
->n_eq
== 0) {
2002 isl_basic_set_free(context
);
2006 bset
= isl_basic_set_cow(bset
);
2010 elim
= isl_alloc_array(bset
->ctx
, int, isl_basic_set_n_dim(bset
));
2013 set_compute_elimination_index(context
, elim
);
2014 for (i
= 0; i
< bset
->n_eq
; ++i
)
2015 set_reduced_using_equalities(bset
->eq
[i
], bset
->eq
[i
],
2017 for (i
= 0; i
< bset
->n_ineq
; ++i
)
2018 set_reduced_using_equalities(bset
->ineq
[i
], bset
->ineq
[i
],
2020 isl_basic_set_free(context
);
2022 bset
= isl_basic_set_simplify(bset
);
2023 bset
= isl_basic_set_finalize(bset
);
2026 isl_basic_set_free(bset
);
2027 isl_basic_set_free(context
);
2031 /* For each inequality in "ineq" that is a shifted (more relaxed)
2032 * copy of an inequality in "context", mark the corresponding entry
2034 * If an inequality only has a non-negative constant term, then
2037 static isl_stat
mark_shifted_constraints(__isl_keep isl_mat
*ineq
,
2038 __isl_keep isl_basic_set
*context
, int *row
)
2040 struct isl_constraint_index ci
;
2045 if (!ineq
|| !context
)
2046 return isl_stat_error
;
2047 if (context
->n_ineq
== 0)
2049 if (setup_constraint_index(&ci
, context
) < 0)
2050 return isl_stat_error
;
2052 n_ineq
= isl_mat_rows(ineq
);
2053 total
= isl_mat_cols(ineq
) - 1;
2054 for (k
= 0; k
< n_ineq
; ++k
) {
2058 l
= isl_seq_first_non_zero(ineq
->row
[k
] + 1, total
);
2059 if (l
< 0 && isl_int_is_nonneg(ineq
->row
[k
][0])) {
2063 redundant
= constraint_index_is_redundant(&ci
, ineq
->row
[k
]);
2070 constraint_index_free(&ci
);
2073 constraint_index_free(&ci
);
2074 return isl_stat_error
;
2077 static struct isl_basic_set
*remove_shifted_constraints(
2078 struct isl_basic_set
*bset
, struct isl_basic_set
*context
)
2080 struct isl_constraint_index ci
;
2083 if (!bset
|| !context
)
2086 if (context
->n_ineq
== 0)
2088 if (setup_constraint_index(&ci
, context
) < 0)
2091 for (k
= 0; k
< bset
->n_ineq
; ++k
) {
2094 redundant
= constraint_index_is_redundant(&ci
, bset
->ineq
[k
]);
2099 bset
= isl_basic_set_cow(bset
);
2102 isl_basic_set_drop_inequality(bset
, k
);
2105 constraint_index_free(&ci
);
2108 constraint_index_free(&ci
);
2112 /* Remove constraints from "bmap" that are identical to constraints
2113 * in "context" or that are more relaxed (greater constant term).
2115 * We perform the test for shifted copies on the pure constraints
2116 * in remove_shifted_constraints.
2118 static __isl_give isl_basic_map
*isl_basic_map_remove_shifted_constraints(
2119 __isl_take isl_basic_map
*bmap
, __isl_take isl_basic_map
*context
)
2121 isl_basic_set
*bset
, *bset_context
;
2123 if (!bmap
|| !context
)
2126 if (bmap
->n_ineq
== 0 || context
->n_ineq
== 0) {
2127 isl_basic_map_free(context
);
2131 context
= isl_basic_map_align_divs(context
, bmap
);
2132 bmap
= isl_basic_map_align_divs(bmap
, context
);
2134 bset
= isl_basic_map_underlying_set(isl_basic_map_copy(bmap
));
2135 bset_context
= isl_basic_map_underlying_set(context
);
2136 bset
= remove_shifted_constraints(bset
, bset_context
);
2137 isl_basic_set_free(bset_context
);
2139 bmap
= isl_basic_map_overlying_set(bset
, bmap
);
2143 isl_basic_map_free(bmap
);
2144 isl_basic_map_free(context
);
2148 /* Does the (linear part of a) constraint "c" involve any of the "len"
2149 * "relevant" dimensions?
2151 static int is_related(isl_int
*c
, int len
, int *relevant
)
2155 for (i
= 0; i
< len
; ++i
) {
2158 if (!isl_int_is_zero(c
[i
]))
2165 /* Drop constraints from "bmap" that do not involve any of
2166 * the dimensions marked "relevant".
2168 static __isl_give isl_basic_map
*drop_unrelated_constraints(
2169 __isl_take isl_basic_map
*bmap
, int *relevant
)
2173 dim
= isl_basic_map_dim(bmap
, isl_dim_all
);
2174 for (i
= 0; i
< dim
; ++i
)
2180 for (i
= bmap
->n_eq
- 1; i
>= 0; --i
)
2181 if (!is_related(bmap
->eq
[i
] + 1, dim
, relevant
)) {
2182 bmap
= isl_basic_map_cow(bmap
);
2183 if (isl_basic_map_drop_equality(bmap
, i
) < 0)
2184 return isl_basic_map_free(bmap
);
2187 for (i
= bmap
->n_ineq
- 1; i
>= 0; --i
)
2188 if (!is_related(bmap
->ineq
[i
] + 1, dim
, relevant
)) {
2189 bmap
= isl_basic_map_cow(bmap
);
2190 if (isl_basic_map_drop_inequality(bmap
, i
) < 0)
2191 return isl_basic_map_free(bmap
);
2197 /* Update the groups in "group" based on the (linear part of a) constraint "c".
2199 * In particular, for any variable involved in the constraint,
2200 * find the actual group id from before and replace the group
2201 * of the corresponding variable by the minimal group of all
2202 * the variables involved in the constraint considered so far
2203 * (if this minimum is smaller) or replace the minimum by this group
2204 * (if the minimum is larger).
2206 * At the end, all the variables in "c" will (indirectly) point
2207 * to the minimal of the groups that they referred to originally.
2209 static void update_groups(int dim
, int *group
, isl_int
*c
)
2214 for (j
= 0; j
< dim
; ++j
) {
2215 if (isl_int_is_zero(c
[j
]))
2217 while (group
[j
] >= 0 && group
[group
[j
]] != group
[j
])
2218 group
[j
] = group
[group
[j
]];
2219 if (group
[j
] == min
)
2221 if (group
[j
] < min
) {
2222 if (min
>= 0 && min
< dim
)
2223 group
[min
] = group
[j
];
2226 group
[group
[j
]] = min
;
2230 /* Allocate an array of groups of variables, one for each variable
2231 * in "context", initialized to zero.
2233 static int *alloc_groups(__isl_keep isl_basic_set
*context
)
2238 dim
= isl_basic_set_dim(context
, isl_dim_set
);
2239 ctx
= isl_basic_set_get_ctx(context
);
2240 return isl_calloc_array(ctx
, int, dim
);
2243 /* Drop constraints from "bmap" that only involve variables that are
2244 * not related to any of the variables marked with a "-1" in "group".
2246 * We construct groups of variables that collect variables that
2247 * (indirectly) appear in some common constraint of "bmap".
2248 * Each group is identified by the first variable in the group,
2249 * except for the special group of variables that was already identified
2250 * in the input as -1 (or are related to those variables).
2251 * If group[i] is equal to i (or -1), then the group of i is i (or -1),
2252 * otherwise the group of i is the group of group[i].
2254 * We first initialize groups for the remaining variables.
2255 * Then we iterate over the constraints of "bmap" and update the
2256 * group of the variables in the constraint by the smallest group.
2257 * Finally, we resolve indirect references to groups by running over
2260 * After computing the groups, we drop constraints that do not involve
2261 * any variables in the -1 group.
2263 __isl_give isl_basic_map
*isl_basic_map_drop_unrelated_constraints(
2264 __isl_take isl_basic_map
*bmap
, __isl_take
int *group
)
2273 dim
= isl_basic_map_dim(bmap
, isl_dim_all
);
2276 for (i
= 0; i
< dim
; ++i
)
2278 last
= group
[i
] = i
;
2284 for (i
= 0; i
< bmap
->n_eq
; ++i
)
2285 update_groups(dim
, group
, bmap
->eq
[i
] + 1);
2286 for (i
= 0; i
< bmap
->n_ineq
; ++i
)
2287 update_groups(dim
, group
, bmap
->ineq
[i
] + 1);
2289 for (i
= 0; i
< dim
; ++i
)
2291 group
[i
] = group
[group
[i
]];
2293 for (i
= 0; i
< dim
; ++i
)
2294 group
[i
] = group
[i
] == -1;
2296 bmap
= drop_unrelated_constraints(bmap
, group
);
2302 /* Drop constraints from "context" that are irrelevant for computing
2303 * the gist of "bset".
2305 * In particular, drop constraints in variables that are not related
2306 * to any of the variables involved in the constraints of "bset"
2307 * in the sense that there is no sequence of constraints that connects them.
2309 * We first mark all variables that appear in "bset" as belonging
2310 * to a "-1" group and then continue with group_and_drop_irrelevant_constraints.
2312 static __isl_give isl_basic_set
*drop_irrelevant_constraints(
2313 __isl_take isl_basic_set
*context
, __isl_keep isl_basic_set
*bset
)
2319 if (!context
|| !bset
)
2320 return isl_basic_set_free(context
);
2322 group
= alloc_groups(context
);
2325 return isl_basic_set_free(context
);
2327 dim
= isl_basic_set_dim(bset
, isl_dim_set
);
2328 for (i
= 0; i
< dim
; ++i
) {
2329 for (j
= 0; j
< bset
->n_eq
; ++j
)
2330 if (!isl_int_is_zero(bset
->eq
[j
][1 + i
]))
2332 if (j
< bset
->n_eq
) {
2336 for (j
= 0; j
< bset
->n_ineq
; ++j
)
2337 if (!isl_int_is_zero(bset
->ineq
[j
][1 + i
]))
2339 if (j
< bset
->n_ineq
)
2343 return isl_basic_map_drop_unrelated_constraints(context
, group
);
2346 /* Drop constraints from "context" that are irrelevant for computing
2347 * the gist of the inequalities "ineq".
2348 * Inequalities in "ineq" for which the corresponding element of row
2349 * is set to -1 have already been marked for removal and should be ignored.
2351 * In particular, drop constraints in variables that are not related
2352 * to any of the variables involved in "ineq"
2353 * in the sense that there is no sequence of constraints that connects them.
2355 * We first mark all variables that appear in "bset" as belonging
2356 * to a "-1" group and then continue with group_and_drop_irrelevant_constraints.
2358 static __isl_give isl_basic_set
*drop_irrelevant_constraints_marked(
2359 __isl_take isl_basic_set
*context
, __isl_keep isl_mat
*ineq
, int *row
)
2365 if (!context
|| !ineq
)
2366 return isl_basic_set_free(context
);
2368 group
= alloc_groups(context
);
2371 return isl_basic_set_free(context
);
2373 dim
= isl_basic_set_dim(context
, isl_dim_set
);
2374 n
= isl_mat_rows(ineq
);
2375 for (i
= 0; i
< dim
; ++i
) {
2376 for (j
= 0; j
< n
; ++j
) {
2379 if (!isl_int_is_zero(ineq
->row
[j
][1 + i
]))
2386 return isl_basic_map_drop_unrelated_constraints(context
, group
);
2389 /* Do all "n" entries of "row" contain a negative value?
2391 static int all_neg(int *row
, int n
)
2395 for (i
= 0; i
< n
; ++i
)
2402 /* Update the inequalities in "bset" based on the information in "row"
2405 * In particular, the array "row" contains either -1, meaning that
2406 * the corresponding inequality of "bset" is redundant, or the index
2407 * of an inequality in "tab".
2409 * If the row entry is -1, then drop the inequality.
2410 * Otherwise, if the constraint is marked redundant in the tableau,
2411 * then drop the inequality. Similarly, if it is marked as an equality
2412 * in the tableau, then turn the inequality into an equality and
2413 * perform Gaussian elimination.
2415 static __isl_give isl_basic_set
*update_ineq(__isl_take isl_basic_set
*bset
,
2416 __isl_keep
int *row
, struct isl_tab
*tab
)
2421 int found_equality
= 0;
2425 if (tab
&& tab
->empty
)
2426 return isl_basic_set_set_to_empty(bset
);
2428 n_ineq
= bset
->n_ineq
;
2429 for (i
= n_ineq
- 1; i
>= 0; --i
) {
2431 if (isl_basic_set_drop_inequality(bset
, i
) < 0)
2432 return isl_basic_set_free(bset
);
2438 if (isl_tab_is_equality(tab
, n_eq
+ row
[i
])) {
2439 isl_basic_map_inequality_to_equality(bset
, i
);
2441 } else if (isl_tab_is_redundant(tab
, n_eq
+ row
[i
])) {
2442 if (isl_basic_set_drop_inequality(bset
, i
) < 0)
2443 return isl_basic_set_free(bset
);
2448 bset
= isl_basic_set_gauss(bset
, NULL
);
2449 bset
= isl_basic_set_finalize(bset
);
2453 /* Update the inequalities in "bset" based on the information in "row"
2454 * and "tab" and free all arguments (other than "bset").
2456 static __isl_give isl_basic_set
*update_ineq_free(
2457 __isl_take isl_basic_set
*bset
, __isl_take isl_mat
*ineq
,
2458 __isl_take isl_basic_set
*context
, __isl_take
int *row
,
2459 struct isl_tab
*tab
)
2462 isl_basic_set_free(context
);
2464 bset
= update_ineq(bset
, row
, tab
);
2471 /* Remove all information from bset that is redundant in the context
2473 * "ineq" contains the (possibly transformed) inequalities of "bset",
2474 * in the same order.
2475 * The (explicit) equalities of "bset" are assumed to have been taken
2476 * into account by the transformation such that only the inequalities
2478 * "context" is assumed not to be empty.
2480 * "row" keeps track of the constraint index of a "bset" inequality in "tab".
2481 * A value of -1 means that the inequality is obviously redundant and may
2482 * not even appear in "tab".
2484 * We first mark the inequalities of "bset"
2485 * that are obviously redundant with respect to some inequality in "context".
2486 * Then we remove those constraints from "context" that have become
2487 * irrelevant for computing the gist of "bset".
2488 * Note that this removal of constraints cannot be replaced by
2489 * a factorization because factors in "bset" may still be connected
2490 * to each other through constraints in "context".
2492 * If there are any inequalities left, we construct a tableau for
2493 * the context and then add the inequalities of "bset".
2494 * Before adding these inequalities, we freeze all constraints such that
2495 * they won't be considered redundant in terms of the constraints of "bset".
2496 * Then we detect all redundant constraints (among the
2497 * constraints that weren't frozen), first by checking for redundancy in the
2498 * the tableau and then by checking if replacing a constraint by its negation
2499 * would lead to an empty set. This last step is fairly expensive
2500 * and could be optimized by more reuse of the tableau.
2501 * Finally, we update bset according to the results.
2503 static __isl_give isl_basic_set
*uset_gist_full(__isl_take isl_basic_set
*bset
,
2504 __isl_take isl_mat
*ineq
, __isl_take isl_basic_set
*context
)
2509 isl_basic_set
*combined
= NULL
;
2510 struct isl_tab
*tab
= NULL
;
2511 unsigned n_eq
, context_ineq
;
2514 if (!bset
|| !ineq
|| !context
)
2517 if (bset
->n_ineq
== 0 || isl_basic_set_plain_is_universe(context
)) {
2518 isl_basic_set_free(context
);
2523 ctx
= isl_basic_set_get_ctx(context
);
2524 row
= isl_calloc_array(ctx
, int, bset
->n_ineq
);
2528 if (mark_shifted_constraints(ineq
, context
, row
) < 0)
2530 if (all_neg(row
, bset
->n_ineq
))
2531 return update_ineq_free(bset
, ineq
, context
, row
, NULL
);
2533 context
= drop_irrelevant_constraints_marked(context
, ineq
, row
);
2536 if (isl_basic_set_plain_is_universe(context
))
2537 return update_ineq_free(bset
, ineq
, context
, row
, NULL
);
2539 n_eq
= context
->n_eq
;
2540 context_ineq
= context
->n_ineq
;
2541 combined
= isl_basic_set_cow(isl_basic_set_copy(context
));
2542 combined
= isl_basic_set_extend_constraints(combined
, 0, bset
->n_ineq
);
2543 tab
= isl_tab_from_basic_set(combined
, 0);
2544 for (i
= 0; i
< context_ineq
; ++i
)
2545 if (isl_tab_freeze_constraint(tab
, n_eq
+ i
) < 0)
2547 if (isl_tab_extend_cons(tab
, bset
->n_ineq
) < 0)
2550 for (i
= 0; i
< bset
->n_ineq
; ++i
) {
2553 combined
= isl_basic_set_add_ineq(combined
, ineq
->row
[i
]);
2554 if (isl_tab_add_ineq(tab
, ineq
->row
[i
]) < 0)
2558 if (isl_tab_detect_implicit_equalities(tab
) < 0)
2560 if (isl_tab_detect_redundant(tab
) < 0)
2562 total
= isl_basic_set_total_dim(bset
);
2563 for (i
= bset
->n_ineq
- 1; i
>= 0; --i
) {
2564 isl_basic_set
*test
;
2570 if (tab
->con
[n_eq
+ r
].is_redundant
)
2572 test
= isl_basic_set_dup(combined
);
2573 if (isl_inequality_negate(test
, r
) < 0)
2574 test
= isl_basic_set_free(test
);
2575 test
= isl_basic_set_update_from_tab(test
, tab
);
2576 is_empty
= isl_basic_set_is_empty(test
);
2577 isl_basic_set_free(test
);
2581 tab
->con
[n_eq
+ r
].is_redundant
= 1;
2583 bset
= update_ineq_free(bset
, ineq
, context
, row
, tab
);
2585 ISL_F_SET(bset
, ISL_BASIC_SET_NO_IMPLICIT
);
2586 ISL_F_SET(bset
, ISL_BASIC_SET_NO_REDUNDANT
);
2589 isl_basic_set_free(combined
);
2595 isl_basic_set_free(combined
);
2596 isl_basic_set_free(context
);
2597 isl_basic_set_free(bset
);
2601 /* Extract the inequalities of "bset" as an isl_mat.
2603 static __isl_give isl_mat
*extract_ineq(__isl_keep isl_basic_set
*bset
)
2612 ctx
= isl_basic_set_get_ctx(bset
);
2613 total
= isl_basic_set_total_dim(bset
);
2614 ineq
= isl_mat_sub_alloc6(ctx
, bset
->ineq
, 0, bset
->n_ineq
,
2620 /* Remove all information from "bset" that is redundant in the context
2621 * of "context", for the case where both "bset" and "context" are
2624 static __isl_give isl_basic_set
*uset_gist_uncompressed(
2625 __isl_take isl_basic_set
*bset
, __isl_take isl_basic_set
*context
)
2629 ineq
= extract_ineq(bset
);
2630 return uset_gist_full(bset
, ineq
, context
);
2633 /* Remove all information from "bset" that is redundant in the context
2634 * of "context", for the case where the combined equalities of
2635 * "bset" and "context" allow for a compression that can be obtained
2636 * by preapplication of "T".
2638 * "bset" itself is not transformed by "T". Instead, the inequalities
2639 * are extracted from "bset" and those are transformed by "T".
2640 * uset_gist_full then determines which of the transformed inequalities
2641 * are redundant with respect to the transformed "context" and removes
2642 * the corresponding inequalities from "bset".
2644 * After preapplying "T" to the inequalities, any common factor is
2645 * removed from the coefficients. If this results in a tightening
2646 * of the constant term, then the same tightening is applied to
2647 * the corresponding untransformed inequality in "bset".
2648 * That is, if after plugging in T, a constraint f(x) >= 0 is of the form
2652 * with 0 <= r < g, then it is equivalent to
2656 * This means that f(x) >= 0 is equivalent to f(x) - r >= 0 in the affine
2657 * subspace compressed by T since the latter would be transformed to
2661 static __isl_give isl_basic_set
*uset_gist_compressed(
2662 __isl_take isl_basic_set
*bset
, __isl_take isl_basic_set
*context
,
2663 __isl_take isl_mat
*T
)
2667 int i
, n_row
, n_col
;
2670 ineq
= extract_ineq(bset
);
2671 ineq
= isl_mat_product(ineq
, isl_mat_copy(T
));
2672 context
= isl_basic_set_preimage(context
, T
);
2674 if (!ineq
|| !context
)
2676 if (isl_basic_set_plain_is_empty(context
)) {
2678 isl_basic_set_free(context
);
2679 return isl_basic_set_set_to_empty(bset
);
2682 ctx
= isl_mat_get_ctx(ineq
);
2683 n_row
= isl_mat_rows(ineq
);
2684 n_col
= isl_mat_cols(ineq
);
2686 for (i
= 0; i
< n_row
; ++i
) {
2687 isl_seq_gcd(ineq
->row
[i
] + 1, n_col
- 1, &ctx
->normalize_gcd
);
2688 if (isl_int_is_zero(ctx
->normalize_gcd
))
2690 if (isl_int_is_one(ctx
->normalize_gcd
))
2692 isl_seq_scale_down(ineq
->row
[i
] + 1, ineq
->row
[i
] + 1,
2693 ctx
->normalize_gcd
, n_col
- 1);
2694 isl_int_fdiv_r(rem
, ineq
->row
[i
][0], ctx
->normalize_gcd
);
2695 isl_int_fdiv_q(ineq
->row
[i
][0],
2696 ineq
->row
[i
][0], ctx
->normalize_gcd
);
2697 if (isl_int_is_zero(rem
))
2699 bset
= isl_basic_set_cow(bset
);
2702 isl_int_sub(bset
->ineq
[i
][0], bset
->ineq
[i
][0], rem
);
2706 return uset_gist_full(bset
, ineq
, context
);
2709 isl_basic_set_free(context
);
2710 isl_basic_set_free(bset
);
2714 /* Project "bset" onto the variables that are involved in "template".
2716 static __isl_give isl_basic_set
*project_onto_involved(
2717 __isl_take isl_basic_set
*bset
, __isl_keep isl_basic_set
*template)
2721 if (!bset
|| !template)
2722 return isl_basic_set_free(bset
);
2724 n
= isl_basic_set_dim(template, isl_dim_set
);
2726 for (i
= 0; i
< n
; ++i
) {
2729 involved
= isl_basic_set_involves_dims(template,
2732 return isl_basic_set_free(bset
);
2735 bset
= isl_basic_set_eliminate_vars(bset
, i
, 1);
2741 /* Remove all information from bset that is redundant in the context
2742 * of context. In particular, equalities that are linear combinations
2743 * of those in context are removed. Then the inequalities that are
2744 * redundant in the context of the equalities and inequalities of
2745 * context are removed.
2747 * First of all, we drop those constraints from "context"
2748 * that are irrelevant for computing the gist of "bset".
2749 * Alternatively, we could factorize the intersection of "context" and "bset".
2751 * We first compute the intersection of the integer affine hulls
2752 * of "bset" and "context",
2753 * compute the gist inside this intersection and then reduce
2754 * the constraints with respect to the equalities of the context
2755 * that only involve variables already involved in the input.
2757 * If two constraints are mutually redundant, then uset_gist_full
2758 * will remove the second of those constraints. We therefore first
2759 * sort the constraints so that constraints not involving existentially
2760 * quantified variables are given precedence over those that do.
2761 * We have to perform this sorting before the variable compression,
2762 * because that may effect the order of the variables.
2764 static __isl_give isl_basic_set
*uset_gist(__isl_take isl_basic_set
*bset
,
2765 __isl_take isl_basic_set
*context
)
2770 isl_basic_set
*aff_context
;
2773 if (!bset
|| !context
)
2776 context
= drop_irrelevant_constraints(context
, bset
);
2778 bset
= isl_basic_set_detect_equalities(bset
);
2779 aff
= isl_basic_set_copy(bset
);
2780 aff
= isl_basic_set_plain_affine_hull(aff
);
2781 context
= isl_basic_set_detect_equalities(context
);
2782 aff_context
= isl_basic_set_copy(context
);
2783 aff_context
= isl_basic_set_plain_affine_hull(aff_context
);
2784 aff
= isl_basic_set_intersect(aff
, aff_context
);
2787 if (isl_basic_set_plain_is_empty(aff
)) {
2788 isl_basic_set_free(bset
);
2789 isl_basic_set_free(context
);
2792 bset
= isl_basic_set_sort_constraints(bset
);
2793 if (aff
->n_eq
== 0) {
2794 isl_basic_set_free(aff
);
2795 return uset_gist_uncompressed(bset
, context
);
2797 total
= isl_basic_set_total_dim(bset
);
2798 eq
= isl_mat_sub_alloc6(bset
->ctx
, aff
->eq
, 0, aff
->n_eq
, 0, 1 + total
);
2799 eq
= isl_mat_cow(eq
);
2800 T
= isl_mat_variable_compression(eq
, NULL
);
2801 isl_basic_set_free(aff
);
2802 if (T
&& T
->n_col
== 0) {
2804 isl_basic_set_free(context
);
2805 return isl_basic_set_set_to_empty(bset
);
2808 aff_context
= isl_basic_set_affine_hull(isl_basic_set_copy(context
));
2809 aff_context
= project_onto_involved(aff_context
, bset
);
2811 bset
= uset_gist_compressed(bset
, context
, T
);
2812 bset
= isl_basic_set_reduce_using_equalities(bset
, aff_context
);
2815 ISL_F_SET(bset
, ISL_BASIC_SET_NO_IMPLICIT
);
2816 ISL_F_SET(bset
, ISL_BASIC_SET_NO_REDUNDANT
);
2821 isl_basic_set_free(bset
);
2822 isl_basic_set_free(context
);
2826 /* Return the number of equality constraints in "bmap" that involve
2827 * local variables. This function assumes that Gaussian elimination
2828 * has been applied to the equality constraints.
2830 static int n_div_eq(__isl_keep isl_basic_map
*bmap
)
2838 if (bmap
->n_eq
== 0)
2841 total
= isl_basic_map_dim(bmap
, isl_dim_all
);
2842 n_div
= isl_basic_map_dim(bmap
, isl_dim_div
);
2845 for (i
= 0; i
< bmap
->n_eq
; ++i
)
2846 if (isl_seq_first_non_zero(bmap
->eq
[i
] + 1 + total
,
2853 /* Construct a basic map in "space" defined by the equality constraints in "eq".
2854 * The constraints are assumed not to involve any local variables.
2856 static __isl_give isl_basic_map
*basic_map_from_equalities(
2857 __isl_take isl_space
*space
, __isl_take isl_mat
*eq
)
2860 isl_basic_map
*bmap
= NULL
;
2865 if (1 + isl_space_dim(space
, isl_dim_all
) != eq
->n_col
)
2866 isl_die(isl_space_get_ctx(space
), isl_error_internal
,
2867 "unexpected number of columns", goto error
);
2869 bmap
= isl_basic_map_alloc_space(isl_space_copy(space
),
2871 for (i
= 0; i
< eq
->n_row
; ++i
) {
2872 k
= isl_basic_map_alloc_equality(bmap
);
2875 isl_seq_cpy(bmap
->eq
[k
], eq
->row
[i
], eq
->n_col
);
2878 isl_space_free(space
);
2882 isl_space_free(space
);
2884 isl_basic_map_free(bmap
);
2888 /* Construct and return a variable compression based on the equality
2889 * constraints in "bmap1" and "bmap2" that do not involve the local variables.
2890 * "n1" is the number of (initial) equality constraints in "bmap1"
2891 * that do involve local variables.
2892 * "n2" is the number of (initial) equality constraints in "bmap2"
2893 * that do involve local variables.
2894 * "total" is the total number of other variables.
2895 * This function assumes that Gaussian elimination
2896 * has been applied to the equality constraints in both "bmap1" and "bmap2"
2897 * such that the equality constraints not involving local variables
2898 * are those that start at "n1" or "n2".
2900 * If either of "bmap1" and "bmap2" does not have such equality constraints,
2901 * then simply compute the compression based on the equality constraints
2902 * in the other basic map.
2903 * Otherwise, combine the equality constraints from both into a new
2904 * basic map such that Gaussian elimination can be applied to this combination
2905 * and then construct a variable compression from the resulting
2906 * equality constraints.
2908 static __isl_give isl_mat
*combined_variable_compression(
2909 __isl_keep isl_basic_map
*bmap1
, int n1
,
2910 __isl_keep isl_basic_map
*bmap2
, int n2
, int total
)
2913 isl_mat
*E1
, *E2
, *V
;
2914 isl_basic_map
*bmap
;
2916 ctx
= isl_basic_map_get_ctx(bmap1
);
2917 if (bmap1
->n_eq
== n1
) {
2918 E2
= isl_mat_sub_alloc6(ctx
, bmap2
->eq
,
2919 n2
, bmap2
->n_eq
- n2
, 0, 1 + total
);
2920 return isl_mat_variable_compression(E2
, NULL
);
2922 if (bmap2
->n_eq
== n2
) {
2923 E1
= isl_mat_sub_alloc6(ctx
, bmap1
->eq
,
2924 n1
, bmap1
->n_eq
- n1
, 0, 1 + total
);
2925 return isl_mat_variable_compression(E1
, NULL
);
2927 E1
= isl_mat_sub_alloc6(ctx
, bmap1
->eq
,
2928 n1
, bmap1
->n_eq
- n1
, 0, 1 + total
);
2929 E2
= isl_mat_sub_alloc6(ctx
, bmap2
->eq
,
2930 n2
, bmap2
->n_eq
- n2
, 0, 1 + total
);
2931 E1
= isl_mat_concat(E1
, E2
);
2932 bmap
= basic_map_from_equalities(isl_basic_map_get_space(bmap1
), E1
);
2933 bmap
= isl_basic_map_gauss(bmap
, NULL
);
2936 E1
= isl_mat_sub_alloc6(ctx
, bmap
->eq
, 0, bmap
->n_eq
, 0, 1 + total
);
2937 V
= isl_mat_variable_compression(E1
, NULL
);
2938 isl_basic_map_free(bmap
);
2943 /* Extract the stride constraints from "bmap", compressed
2944 * with respect to both the stride constraints in "context" and
2945 * the remaining equality constraints in both "bmap" and "context".
2946 * "bmap_n_eq" is the number of (initial) stride constraints in "bmap".
2947 * "context_n_eq" is the number of (initial) stride constraints in "context".
2949 * Let x be all variables in "bmap" (and "context") other than the local
2950 * variables. First compute a variable compression
2954 * based on the non-stride equality constraints in "bmap" and "context".
2955 * Consider the stride constraints of "context",
2959 * with y the local variables and plug in the variable compression,
2962 * A(V x') + B(y) = 0
2964 * Use these constraints to compute a parameter compression on x'
2968 * Now consider the stride constraints of "bmap"
2972 * and plug in x = V*T x''.
2973 * That is, return A = [C*V*T D].
2975 static __isl_give isl_mat
*extract_compressed_stride_constraints(
2976 __isl_keep isl_basic_map
*bmap
, int bmap_n_eq
,
2977 __isl_keep isl_basic_map
*context
, int context_n_eq
)
2981 isl_mat
*A
, *B
, *T
, *V
;
2983 total
= isl_basic_map_dim(context
, isl_dim_all
);
2984 n_div
= isl_basic_map_dim(context
, isl_dim_div
);
2987 ctx
= isl_basic_map_get_ctx(bmap
);
2989 V
= combined_variable_compression(bmap
, bmap_n_eq
,
2990 context
, context_n_eq
, total
);
2992 A
= isl_mat_sub_alloc6(ctx
, context
->eq
, 0, context_n_eq
, 0, 1 + total
);
2993 B
= isl_mat_sub_alloc6(ctx
, context
->eq
,
2994 0, context_n_eq
, 1 + total
, n_div
);
2995 A
= isl_mat_product(A
, isl_mat_copy(V
));
2996 T
= isl_mat_parameter_compression_ext(A
, B
);
2997 T
= isl_mat_product(V
, T
);
2999 n_div
= isl_basic_map_dim(bmap
, isl_dim_div
);
3000 T
= isl_mat_diagonal(T
, isl_mat_identity(ctx
, n_div
));
3002 A
= isl_mat_sub_alloc6(ctx
, bmap
->eq
,
3003 0, bmap_n_eq
, 0, 1 + total
+ n_div
);
3004 A
= isl_mat_product(A
, T
);
3009 /* Remove the prime factors from *g that have an exponent that
3010 * is strictly smaller than the exponent in "c".
3011 * All exponents in *g are known to be smaller than or equal
3014 * That is, if *g is equal to
3016 * p_1^{e_1} p_2^{e_2} ... p_n^{e_n}
3018 * and "c" is equal to
3020 * p_1^{f_1} p_2^{f_2} ... p_n^{f_n}
3024 * p_1^{e_1 * (e_1 = f_1)} p_2^{e_2 * (e_2 = f_2)} ...
3025 * p_n^{e_n * (e_n = f_n)}
3027 * If e_i = f_i, then c / *g does not have any p_i factors and therefore
3028 * neither does the gcd of *g and c / *g.
3029 * If e_i < f_i, then the gcd of *g and c / *g has a positive
3030 * power min(e_i, s_i) of p_i with s_i = f_i - e_i among its factors.
3031 * Dividing *g by this gcd therefore strictly reduces the exponent
3032 * of the prime factors that need to be removed, while leaving the
3033 * other prime factors untouched.
3034 * Repeating this process until gcd(*g, c / *g) = 1 therefore
3035 * removes all undesired factors, without removing any others.
3037 static void remove_incomplete_powers(isl_int
*g
, isl_int c
)
3043 isl_int_divexact(t
, c
, *g
);
3044 isl_int_gcd(t
, t
, *g
);
3045 if (isl_int_is_one(t
))
3047 isl_int_divexact(*g
, *g
, t
);
3052 /* Reduce the "n" stride constraints in "bmap" based on a copy "A"
3053 * of the same stride constraints in a compressed space that exploits
3054 * all equalities in the context and the other equalities in "bmap".
3056 * If the stride constraints of "bmap" are of the form
3060 * then A is of the form
3064 * If any of these constraints involves only a single local variable y,
3065 * then the constraint appears as
3075 * Let g be the gcd of m and the coefficients of h.
3076 * Then, in particular, g is a divisor of the coefficients of h and
3080 * is known to be a multiple of g.
3081 * If some prime factor in m appears with the same exponent in g,
3082 * then it can be removed from m because f(x) is already known
3083 * to be a multiple of g and therefore in particular of this power
3084 * of the prime factors.
3085 * Prime factors that appear with a smaller exponent in g cannot
3086 * be removed from m.
3087 * Let g' be the divisor of g containing all prime factors that
3088 * appear with the same exponent in m and g, then
3092 * can be replaced by
3094 * f(x) + m/g' y_i' = 0
3096 * Note that (if g' != 1) this changes the explicit representation
3097 * of y_i to that of y_i', so the integer division at position i
3098 * is marked unknown and later recomputed by a call to
3099 * isl_basic_map_gauss.
3101 static __isl_give isl_basic_map
*reduce_stride_constraints(
3102 __isl_take isl_basic_map
*bmap
, int n
, __isl_keep isl_mat
*A
)
3110 return isl_basic_map_free(bmap
);
3112 total
= isl_basic_map_dim(bmap
, isl_dim_all
);
3113 n_div
= isl_basic_map_dim(bmap
, isl_dim_div
);
3117 for (i
= 0; i
< n
; ++i
) {
3120 div
= isl_seq_first_non_zero(bmap
->eq
[i
] + 1 + total
, n_div
);
3122 isl_die(isl_basic_map_get_ctx(bmap
), isl_error_internal
,
3123 "equality constraints modified unexpectedly",
3125 if (isl_seq_first_non_zero(bmap
->eq
[i
] + 1 + total
+ div
+ 1,
3126 n_div
- div
- 1) != -1)
3128 if (isl_mat_row_gcd(A
, i
, &gcd
) < 0)
3130 if (isl_int_is_one(gcd
))
3132 remove_incomplete_powers(&gcd
, bmap
->eq
[i
][1 + total
+ div
]);
3133 if (isl_int_is_one(gcd
))
3135 isl_int_divexact(bmap
->eq
[i
][1 + total
+ div
],
3136 bmap
->eq
[i
][1 + total
+ div
], gcd
);
3137 bmap
= isl_basic_map_mark_div_unknown(bmap
, div
);
3145 bmap
= isl_basic_map_gauss(bmap
, NULL
);
3150 isl_basic_map_free(bmap
);
3154 /* Simplify the stride constraints in "bmap" based on
3155 * the remaining equality constraints in "bmap" and all equality
3156 * constraints in "context".
3157 * Only do this if both "bmap" and "context" have stride constraints.
3159 * First extract a copy of the stride constraints in "bmap" in a compressed
3160 * space exploiting all the other equality constraints and then
3161 * use this compressed copy to simplify the original stride constraints.
3163 static __isl_give isl_basic_map
*gist_strides(__isl_take isl_basic_map
*bmap
,
3164 __isl_keep isl_basic_map
*context
)
3166 int bmap_n_eq
, context_n_eq
;
3169 if (!bmap
|| !context
)
3170 return isl_basic_map_free(bmap
);
3172 bmap_n_eq
= n_div_eq(bmap
);
3173 context_n_eq
= n_div_eq(context
);
3175 if (bmap_n_eq
< 0 || context_n_eq
< 0)
3176 return isl_basic_map_free(bmap
);
3177 if (bmap_n_eq
== 0 || context_n_eq
== 0)
3180 A
= extract_compressed_stride_constraints(bmap
, bmap_n_eq
,
3181 context
, context_n_eq
);
3182 bmap
= reduce_stride_constraints(bmap
, bmap_n_eq
, A
);
3189 /* Return a basic map that has the same intersection with "context" as "bmap"
3190 * and that is as "simple" as possible.
3192 * The core computation is performed on the pure constraints.
3193 * When we add back the meaning of the integer divisions, we need
3194 * to (re)introduce the div constraints. If we happen to have
3195 * discovered that some of these integer divisions are equal to
3196 * some affine combination of other variables, then these div
3197 * constraints may end up getting simplified in terms of the equalities,
3198 * resulting in extra inequalities on the other variables that
3199 * may have been removed already or that may not even have been
3200 * part of the input. We try and remove those constraints of
3201 * this form that are most obviously redundant with respect to
3202 * the context. We also remove those div constraints that are
3203 * redundant with respect to the other constraints in the result.
3205 * The stride constraints among the equality constraints in "bmap" are
3206 * also simplified with respecting to the other equality constraints
3207 * in "bmap" and with respect to all equality constraints in "context".
3209 struct isl_basic_map
*isl_basic_map_gist(struct isl_basic_map
*bmap
,
3210 struct isl_basic_map
*context
)
3212 isl_basic_set
*bset
, *eq
;
3213 isl_basic_map
*eq_bmap
;
3214 unsigned total
, n_div
, extra
, n_eq
, n_ineq
;
3216 if (!bmap
|| !context
)
3219 if (isl_basic_map_plain_is_universe(bmap
)) {
3220 isl_basic_map_free(context
);
3223 if (isl_basic_map_plain_is_empty(context
)) {
3224 isl_space
*space
= isl_basic_map_get_space(bmap
);
3225 isl_basic_map_free(bmap
);
3226 isl_basic_map_free(context
);
3227 return isl_basic_map_universe(space
);
3229 if (isl_basic_map_plain_is_empty(bmap
)) {
3230 isl_basic_map_free(context
);
3234 bmap
= isl_basic_map_remove_redundancies(bmap
);
3235 context
= isl_basic_map_remove_redundancies(context
);
3239 context
= isl_basic_map_align_divs(context
, bmap
);
3240 n_div
= isl_basic_map_dim(context
, isl_dim_div
);
3241 total
= isl_basic_map_dim(bmap
, isl_dim_all
);
3242 extra
= n_div
- isl_basic_map_dim(bmap
, isl_dim_div
);
3244 bset
= isl_basic_map_underlying_set(isl_basic_map_copy(bmap
));
3245 bset
= isl_basic_set_add_dims(bset
, isl_dim_set
, extra
);
3246 bset
= uset_gist(bset
,
3247 isl_basic_map_underlying_set(isl_basic_map_copy(context
)));
3248 bset
= isl_basic_set_project_out(bset
, isl_dim_set
, total
, extra
);
3250 if (!bset
|| bset
->n_eq
== 0 || n_div
== 0 ||
3251 isl_basic_set_plain_is_empty(bset
)) {
3252 isl_basic_map_free(context
);
3253 return isl_basic_map_overlying_set(bset
, bmap
);
3257 n_ineq
= bset
->n_ineq
;
3258 eq
= isl_basic_set_copy(bset
);
3259 eq
= isl_basic_set_cow(eq
);
3260 if (isl_basic_set_free_inequality(eq
, n_ineq
) < 0)
3261 eq
= isl_basic_set_free(eq
);
3262 if (isl_basic_set_free_equality(bset
, n_eq
) < 0)
3263 bset
= isl_basic_set_free(bset
);
3265 eq_bmap
= isl_basic_map_overlying_set(eq
, isl_basic_map_copy(bmap
));
3266 eq_bmap
= gist_strides(eq_bmap
, context
);
3267 eq_bmap
= isl_basic_map_remove_shifted_constraints(eq_bmap
, context
);
3268 bmap
= isl_basic_map_overlying_set(bset
, bmap
);
3269 bmap
= isl_basic_map_intersect(bmap
, eq_bmap
);
3270 bmap
= isl_basic_map_remove_redundancies(bmap
);
3274 isl_basic_map_free(bmap
);
3275 isl_basic_map_free(context
);
3280 * Assumes context has no implicit divs.
3282 __isl_give isl_map
*isl_map_gist_basic_map(__isl_take isl_map
*map
,
3283 __isl_take isl_basic_map
*context
)
3287 if (!map
|| !context
)
3290 if (isl_basic_map_plain_is_empty(context
)) {
3291 isl_space
*space
= isl_map_get_space(map
);
3293 isl_basic_map_free(context
);
3294 return isl_map_universe(space
);
3297 context
= isl_basic_map_remove_redundancies(context
);
3298 map
= isl_map_cow(map
);
3299 if (!map
|| !context
)
3301 isl_assert(map
->ctx
, isl_space_is_equal(map
->dim
, context
->dim
), goto error
);
3302 map
= isl_map_compute_divs(map
);
3305 for (i
= map
->n
- 1; i
>= 0; --i
) {
3306 map
->p
[i
] = isl_basic_map_gist(map
->p
[i
],
3307 isl_basic_map_copy(context
));
3310 if (isl_basic_map_plain_is_empty(map
->p
[i
])) {
3311 isl_basic_map_free(map
->p
[i
]);
3312 if (i
!= map
->n
- 1)
3313 map
->p
[i
] = map
->p
[map
->n
- 1];
3317 isl_basic_map_free(context
);
3318 ISL_F_CLR(map
, ISL_MAP_NORMALIZED
);
3322 isl_basic_map_free(context
);
3326 /* Drop all inequalities from "bmap" that also appear in "context".
3327 * "context" is assumed to have only known local variables and
3328 * the initial local variables of "bmap" are assumed to be the same
3329 * as those of "context".
3330 * The constraints of both "bmap" and "context" are assumed
3331 * to have been sorted using isl_basic_map_sort_constraints.
3333 * Run through the inequality constraints of "bmap" and "context"
3335 * If a constraint of "bmap" involves variables not in "context",
3336 * then it cannot appear in "context".
3337 * If a matching constraint is found, it is removed from "bmap".
3339 static __isl_give isl_basic_map
*drop_inequalities(
3340 __isl_take isl_basic_map
*bmap
, __isl_keep isl_basic_map
*context
)
3343 unsigned total
, extra
;
3345 if (!bmap
|| !context
)
3346 return isl_basic_map_free(bmap
);
3348 total
= isl_basic_map_total_dim(context
);
3349 extra
= isl_basic_map_total_dim(bmap
) - total
;
3351 i1
= bmap
->n_ineq
- 1;
3352 i2
= context
->n_ineq
- 1;
3353 while (bmap
&& i1
>= 0 && i2
>= 0) {
3356 if (isl_seq_first_non_zero(bmap
->ineq
[i1
] + 1 + total
,
3361 cmp
= isl_basic_map_constraint_cmp(context
, bmap
->ineq
[i1
],
3371 if (isl_int_eq(bmap
->ineq
[i1
][0], context
->ineq
[i2
][0])) {
3372 bmap
= isl_basic_map_cow(bmap
);
3373 if (isl_basic_map_drop_inequality(bmap
, i1
) < 0)
3374 bmap
= isl_basic_map_free(bmap
);
3383 /* Drop all equalities from "bmap" that also appear in "context".
3384 * "context" is assumed to have only known local variables and
3385 * the initial local variables of "bmap" are assumed to be the same
3386 * as those of "context".
3388 * Run through the equality constraints of "bmap" and "context"
3390 * If a constraint of "bmap" involves variables not in "context",
3391 * then it cannot appear in "context".
3392 * If a matching constraint is found, it is removed from "bmap".
3394 static __isl_give isl_basic_map
*drop_equalities(
3395 __isl_take isl_basic_map
*bmap
, __isl_keep isl_basic_map
*context
)
3398 unsigned total
, extra
;
3400 if (!bmap
|| !context
)
3401 return isl_basic_map_free(bmap
);
3403 total
= isl_basic_map_total_dim(context
);
3404 extra
= isl_basic_map_total_dim(bmap
) - total
;
3406 i1
= bmap
->n_eq
- 1;
3407 i2
= context
->n_eq
- 1;
3409 while (bmap
&& i1
>= 0 && i2
>= 0) {
3412 if (isl_seq_first_non_zero(bmap
->eq
[i1
] + 1 + total
,
3415 last1
= isl_seq_last_non_zero(bmap
->eq
[i1
] + 1, total
);
3416 last2
= isl_seq_last_non_zero(context
->eq
[i2
] + 1, total
);
3417 if (last1
> last2
) {
3421 if (last1
< last2
) {
3425 if (isl_seq_eq(bmap
->eq
[i1
], context
->eq
[i2
], 1 + total
)) {
3426 bmap
= isl_basic_map_cow(bmap
);
3427 if (isl_basic_map_drop_equality(bmap
, i1
) < 0)
3428 bmap
= isl_basic_map_free(bmap
);
3437 /* Remove the constraints in "context" from "bmap".
3438 * "context" is assumed to have explicit representations
3439 * for all local variables.
3441 * First align the divs of "bmap" to those of "context" and
3442 * sort the constraints. Then drop all constraints from "bmap"
3443 * that appear in "context".
3445 __isl_give isl_basic_map
*isl_basic_map_plain_gist(
3446 __isl_take isl_basic_map
*bmap
, __isl_take isl_basic_map
*context
)
3448 isl_bool done
, known
;
3450 done
= isl_basic_map_plain_is_universe(context
);
3451 if (done
== isl_bool_false
)
3452 done
= isl_basic_map_plain_is_universe(bmap
);
3453 if (done
== isl_bool_false
)
3454 done
= isl_basic_map_plain_is_empty(context
);
3455 if (done
== isl_bool_false
)
3456 done
= isl_basic_map_plain_is_empty(bmap
);
3460 isl_basic_map_free(context
);
3463 known
= isl_basic_map_divs_known(context
);
3467 isl_die(isl_basic_map_get_ctx(bmap
), isl_error_invalid
,
3468 "context has unknown divs", goto error
);
3470 bmap
= isl_basic_map_align_divs(bmap
, context
);
3471 bmap
= isl_basic_map_gauss(bmap
, NULL
);
3472 bmap
= isl_basic_map_sort_constraints(bmap
);
3473 context
= isl_basic_map_sort_constraints(context
);
3475 bmap
= drop_inequalities(bmap
, context
);
3476 bmap
= drop_equalities(bmap
, context
);
3478 isl_basic_map_free(context
);
3479 bmap
= isl_basic_map_finalize(bmap
);
3482 isl_basic_map_free(bmap
);
3483 isl_basic_map_free(context
);
3487 /* Replace "map" by the disjunct at position "pos" and free "context".
3489 static __isl_give isl_map
*replace_by_disjunct(__isl_take isl_map
*map
,
3490 int pos
, __isl_take isl_basic_map
*context
)
3492 isl_basic_map
*bmap
;
3494 bmap
= isl_basic_map_copy(map
->p
[pos
]);
3496 isl_basic_map_free(context
);
3497 return isl_map_from_basic_map(bmap
);
3500 /* Remove the constraints in "context" from "map".
3501 * If any of the disjuncts in the result turns out to be the universe,
3502 * then return this universe.
3503 * "context" is assumed to have explicit representations
3504 * for all local variables.
3506 __isl_give isl_map
*isl_map_plain_gist_basic_map(__isl_take isl_map
*map
,
3507 __isl_take isl_basic_map
*context
)
3510 isl_bool univ
, known
;
3512 univ
= isl_basic_map_plain_is_universe(context
);
3516 isl_basic_map_free(context
);
3519 known
= isl_basic_map_divs_known(context
);
3523 isl_die(isl_map_get_ctx(map
), isl_error_invalid
,
3524 "context has unknown divs", goto error
);
3526 map
= isl_map_cow(map
);
3529 for (i
= 0; i
< map
->n
; ++i
) {
3530 map
->p
[i
] = isl_basic_map_plain_gist(map
->p
[i
],
3531 isl_basic_map_copy(context
));
3532 univ
= isl_basic_map_plain_is_universe(map
->p
[i
]);
3535 if (univ
&& map
->n
> 1)
3536 return replace_by_disjunct(map
, i
, context
);
3539 isl_basic_map_free(context
);
3540 ISL_F_CLR(map
, ISL_MAP_NORMALIZED
);
3542 ISL_F_CLR(map
, ISL_MAP_DISJOINT
);
3546 isl_basic_map_free(context
);
3550 /* Replace "map" by a universe map in the same space and free "drop".
3552 static __isl_give isl_map
*replace_by_universe(__isl_take isl_map
*map
,
3553 __isl_take isl_map
*drop
)
3557 res
= isl_map_universe(isl_map_get_space(map
));
3563 /* Return a map that has the same intersection with "context" as "map"
3564 * and that is as "simple" as possible.
3566 * If "map" is already the universe, then we cannot make it any simpler.
3567 * Similarly, if "context" is the universe, then we cannot exploit it
3569 * If "map" and "context" are identical to each other, then we can
3570 * return the corresponding universe.
3572 * If either "map" or "context" consists of multiple disjuncts,
3573 * then check if "context" happens to be a subset of "map",
3574 * in which case all constraints can be removed.
3575 * In case of multiple disjuncts, the standard procedure
3576 * may not be able to detect that all constraints can be removed.
3578 * If none of these cases apply, we have to work a bit harder.
3579 * During this computation, we make use of a single disjunct context,
3580 * so if the original context consists of more than one disjunct
3581 * then we need to approximate the context by a single disjunct set.
3582 * Simply taking the simple hull may drop constraints that are
3583 * only implicitly available in each disjunct. We therefore also
3584 * look for constraints among those defining "map" that are valid
3585 * for the context. These can then be used to simplify away
3586 * the corresponding constraints in "map".
3588 static __isl_give isl_map
*map_gist(__isl_take isl_map
*map
,
3589 __isl_take isl_map
*context
)
3593 int single_disjunct_map
, single_disjunct_context
;
3595 isl_basic_map
*hull
;
3597 is_universe
= isl_map_plain_is_universe(map
);
3598 if (is_universe
>= 0 && !is_universe
)
3599 is_universe
= isl_map_plain_is_universe(context
);
3600 if (is_universe
< 0)
3603 isl_map_free(context
);
3607 equal
= isl_map_plain_is_equal(map
, context
);
3611 return replace_by_universe(map
, context
);
3613 single_disjunct_map
= isl_map_n_basic_map(map
) == 1;
3614 single_disjunct_context
= isl_map_n_basic_map(context
) == 1;
3615 if (!single_disjunct_map
|| !single_disjunct_context
) {
3616 subset
= isl_map_is_subset(context
, map
);
3620 return replace_by_universe(map
, context
);
3623 context
= isl_map_compute_divs(context
);
3626 if (single_disjunct_context
) {
3627 hull
= isl_map_simple_hull(context
);
3632 ctx
= isl_map_get_ctx(map
);
3633 list
= isl_map_list_alloc(ctx
, 2);
3634 list
= isl_map_list_add(list
, isl_map_copy(context
));
3635 list
= isl_map_list_add(list
, isl_map_copy(map
));
3636 hull
= isl_map_unshifted_simple_hull_from_map_list(context
,
3639 return isl_map_gist_basic_map(map
, hull
);
3642 isl_map_free(context
);
3646 __isl_give isl_map
*isl_map_gist(__isl_take isl_map
*map
,
3647 __isl_take isl_map
*context
)
3649 return isl_map_align_params_map_map_and(map
, context
, &map_gist
);
3652 struct isl_basic_set
*isl_basic_set_gist(struct isl_basic_set
*bset
,
3653 struct isl_basic_set
*context
)
3655 return bset_from_bmap(isl_basic_map_gist(bset_to_bmap(bset
),
3656 bset_to_bmap(context
)));
3659 __isl_give isl_set
*isl_set_gist_basic_set(__isl_take isl_set
*set
,
3660 __isl_take isl_basic_set
*context
)
3662 return set_from_map(isl_map_gist_basic_map(set_to_map(set
),
3663 bset_to_bmap(context
)));
3666 __isl_give isl_set
*isl_set_gist_params_basic_set(__isl_take isl_set
*set
,
3667 __isl_take isl_basic_set
*context
)
3669 isl_space
*space
= isl_set_get_space(set
);
3670 isl_basic_set
*dom_context
= isl_basic_set_universe(space
);
3671 dom_context
= isl_basic_set_intersect_params(dom_context
, context
);
3672 return isl_set_gist_basic_set(set
, dom_context
);
3675 __isl_give isl_set
*isl_set_gist(__isl_take isl_set
*set
,
3676 __isl_take isl_set
*context
)
3678 return set_from_map(isl_map_gist(set_to_map(set
), set_to_map(context
)));
3681 /* Compute the gist of "bmap" with respect to the constraints "context"
3684 __isl_give isl_basic_map
*isl_basic_map_gist_domain(
3685 __isl_take isl_basic_map
*bmap
, __isl_take isl_basic_set
*context
)
3687 isl_space
*space
= isl_basic_map_get_space(bmap
);
3688 isl_basic_map
*bmap_context
= isl_basic_map_universe(space
);
3690 bmap_context
= isl_basic_map_intersect_domain(bmap_context
, context
);
3691 return isl_basic_map_gist(bmap
, bmap_context
);
3694 __isl_give isl_map
*isl_map_gist_domain(__isl_take isl_map
*map
,
3695 __isl_take isl_set
*context
)
3697 isl_map
*map_context
= isl_map_universe(isl_map_get_space(map
));
3698 map_context
= isl_map_intersect_domain(map_context
, context
);
3699 return isl_map_gist(map
, map_context
);
3702 __isl_give isl_map
*isl_map_gist_range(__isl_take isl_map
*map
,
3703 __isl_take isl_set
*context
)
3705 isl_map
*map_context
= isl_map_universe(isl_map_get_space(map
));
3706 map_context
= isl_map_intersect_range(map_context
, context
);
3707 return isl_map_gist(map
, map_context
);
3710 __isl_give isl_map
*isl_map_gist_params(__isl_take isl_map
*map
,
3711 __isl_take isl_set
*context
)
3713 isl_map
*map_context
= isl_map_universe(isl_map_get_space(map
));
3714 map_context
= isl_map_intersect_params(map_context
, context
);
3715 return isl_map_gist(map
, map_context
);
3718 __isl_give isl_set
*isl_set_gist_params(__isl_take isl_set
*set
,
3719 __isl_take isl_set
*context
)
3721 return isl_map_gist_params(set
, context
);
3724 /* Quick check to see if two basic maps are disjoint.
3725 * In particular, we reduce the equalities and inequalities of
3726 * one basic map in the context of the equalities of the other
3727 * basic map and check if we get a contradiction.
3729 isl_bool
isl_basic_map_plain_is_disjoint(__isl_keep isl_basic_map
*bmap1
,
3730 __isl_keep isl_basic_map
*bmap2
)
3732 struct isl_vec
*v
= NULL
;
3737 if (!bmap1
|| !bmap2
)
3738 return isl_bool_error
;
3739 isl_assert(bmap1
->ctx
, isl_space_is_equal(bmap1
->dim
, bmap2
->dim
),
3740 return isl_bool_error
);
3741 if (bmap1
->n_div
|| bmap2
->n_div
)
3742 return isl_bool_false
;
3743 if (!bmap1
->n_eq
&& !bmap2
->n_eq
)
3744 return isl_bool_false
;
3746 total
= isl_space_dim(bmap1
->dim
, isl_dim_all
);
3748 return isl_bool_false
;
3749 v
= isl_vec_alloc(bmap1
->ctx
, 1 + total
);
3752 elim
= isl_alloc_array(bmap1
->ctx
, int, total
);
3755 compute_elimination_index(bmap1
, elim
);
3756 for (i
= 0; i
< bmap2
->n_eq
; ++i
) {
3758 reduced
= reduced_using_equalities(v
->block
.data
, bmap2
->eq
[i
],
3760 if (reduced
&& !isl_int_is_zero(v
->block
.data
[0]) &&
3761 isl_seq_first_non_zero(v
->block
.data
+ 1, total
) == -1)
3764 for (i
= 0; i
< bmap2
->n_ineq
; ++i
) {
3766 reduced
= reduced_using_equalities(v
->block
.data
,
3767 bmap2
->ineq
[i
], bmap1
, elim
);
3768 if (reduced
&& isl_int_is_neg(v
->block
.data
[0]) &&
3769 isl_seq_first_non_zero(v
->block
.data
+ 1, total
) == -1)
3772 compute_elimination_index(bmap2
, elim
);
3773 for (i
= 0; i
< bmap1
->n_ineq
; ++i
) {
3775 reduced
= reduced_using_equalities(v
->block
.data
,
3776 bmap1
->ineq
[i
], bmap2
, elim
);
3777 if (reduced
&& isl_int_is_neg(v
->block
.data
[0]) &&
3778 isl_seq_first_non_zero(v
->block
.data
+ 1, total
) == -1)
3783 return isl_bool_false
;
3787 return isl_bool_true
;
3791 return isl_bool_error
;
3794 int isl_basic_set_plain_is_disjoint(__isl_keep isl_basic_set
*bset1
,
3795 __isl_keep isl_basic_set
*bset2
)
3797 return isl_basic_map_plain_is_disjoint(bset_to_bmap(bset1
),
3798 bset_to_bmap(bset2
));
3801 /* Does "test" hold for all pairs of basic maps in "map1" and "map2"?
3803 static isl_bool
all_pairs(__isl_keep isl_map
*map1
, __isl_keep isl_map
*map2
,
3804 isl_bool (*test
)(__isl_keep isl_basic_map
*bmap1
,
3805 __isl_keep isl_basic_map
*bmap2
))
3810 return isl_bool_error
;
3812 for (i
= 0; i
< map1
->n
; ++i
) {
3813 for (j
= 0; j
< map2
->n
; ++j
) {
3814 isl_bool d
= test(map1
->p
[i
], map2
->p
[j
]);
3815 if (d
!= isl_bool_true
)
3820 return isl_bool_true
;
3823 /* Are "map1" and "map2" obviously disjoint, based on information
3824 * that can be derived without looking at the individual basic maps?
3826 * In particular, if one of them is empty or if they live in different spaces
3827 * (ignoring parameters), then they are clearly disjoint.
3829 static isl_bool
isl_map_plain_is_disjoint_global(__isl_keep isl_map
*map1
,
3830 __isl_keep isl_map
*map2
)
3836 return isl_bool_error
;
3838 disjoint
= isl_map_plain_is_empty(map1
);
3839 if (disjoint
< 0 || disjoint
)
3842 disjoint
= isl_map_plain_is_empty(map2
);
3843 if (disjoint
< 0 || disjoint
)
3846 match
= isl_space_tuple_is_equal(map1
->dim
, isl_dim_in
,
3847 map2
->dim
, isl_dim_in
);
3848 if (match
< 0 || !match
)
3849 return match
< 0 ? isl_bool_error
: isl_bool_true
;
3851 match
= isl_space_tuple_is_equal(map1
->dim
, isl_dim_out
,
3852 map2
->dim
, isl_dim_out
);
3853 if (match
< 0 || !match
)
3854 return match
< 0 ? isl_bool_error
: isl_bool_true
;
3856 return isl_bool_false
;
3859 /* Are "map1" and "map2" obviously disjoint?
3861 * If one of them is empty or if they live in different spaces (ignoring
3862 * parameters), then they are clearly disjoint.
3863 * This is checked by isl_map_plain_is_disjoint_global.
3865 * If they have different parameters, then we skip any further tests.
3867 * If they are obviously equal, but not obviously empty, then we will
3868 * not be able to detect if they are disjoint.
3870 * Otherwise we check if each basic map in "map1" is obviously disjoint
3871 * from each basic map in "map2".
3873 isl_bool
isl_map_plain_is_disjoint(__isl_keep isl_map
*map1
,
3874 __isl_keep isl_map
*map2
)
3880 disjoint
= isl_map_plain_is_disjoint_global(map1
, map2
);
3881 if (disjoint
< 0 || disjoint
)
3884 match
= isl_space_match(map1
->dim
, isl_dim_param
,
3885 map2
->dim
, isl_dim_param
);
3886 if (match
< 0 || !match
)
3887 return match
< 0 ? isl_bool_error
: isl_bool_false
;
3889 intersect
= isl_map_plain_is_equal(map1
, map2
);
3890 if (intersect
< 0 || intersect
)
3891 return intersect
< 0 ? isl_bool_error
: isl_bool_false
;
3893 return all_pairs(map1
, map2
, &isl_basic_map_plain_is_disjoint
);
3896 /* Are "map1" and "map2" disjoint?
3898 * They are disjoint if they are "obviously disjoint" or if one of them
3899 * is empty. Otherwise, they are not disjoint if one of them is universal.
3900 * If the two inputs are (obviously) equal and not empty, then they are
3902 * If none of these cases apply, then check if all pairs of basic maps
3905 isl_bool
isl_map_is_disjoint(__isl_keep isl_map
*map1
, __isl_keep isl_map
*map2
)
3910 disjoint
= isl_map_plain_is_disjoint_global(map1
, map2
);
3911 if (disjoint
< 0 || disjoint
)
3914 disjoint
= isl_map_is_empty(map1
);
3915 if (disjoint
< 0 || disjoint
)
3918 disjoint
= isl_map_is_empty(map2
);
3919 if (disjoint
< 0 || disjoint
)
3922 intersect
= isl_map_plain_is_universe(map1
);
3923 if (intersect
< 0 || intersect
)
3924 return intersect
< 0 ? isl_bool_error
: isl_bool_false
;
3926 intersect
= isl_map_plain_is_universe(map2
);
3927 if (intersect
< 0 || intersect
)
3928 return intersect
< 0 ? isl_bool_error
: isl_bool_false
;
3930 intersect
= isl_map_plain_is_equal(map1
, map2
);
3931 if (intersect
< 0 || intersect
)
3932 return isl_bool_not(intersect
);
3934 return all_pairs(map1
, map2
, &isl_basic_map_is_disjoint
);
3937 /* Are "bmap1" and "bmap2" disjoint?
3939 * They are disjoint if they are "obviously disjoint" or if one of them
3940 * is empty. Otherwise, they are not disjoint if one of them is universal.
3941 * If none of these cases apply, we compute the intersection and see if
3942 * the result is empty.
3944 isl_bool
isl_basic_map_is_disjoint(__isl_keep isl_basic_map
*bmap1
,
3945 __isl_keep isl_basic_map
*bmap2
)
3949 isl_basic_map
*test
;
3951 disjoint
= isl_basic_map_plain_is_disjoint(bmap1
, bmap2
);
3952 if (disjoint
< 0 || disjoint
)
3955 disjoint
= isl_basic_map_is_empty(bmap1
);
3956 if (disjoint
< 0 || disjoint
)
3959 disjoint
= isl_basic_map_is_empty(bmap2
);
3960 if (disjoint
< 0 || disjoint
)
3963 intersect
= isl_basic_map_plain_is_universe(bmap1
);
3964 if (intersect
< 0 || intersect
)
3965 return intersect
< 0 ? isl_bool_error
: isl_bool_false
;
3967 intersect
= isl_basic_map_plain_is_universe(bmap2
);
3968 if (intersect
< 0 || intersect
)
3969 return intersect
< 0 ? isl_bool_error
: isl_bool_false
;
3971 test
= isl_basic_map_intersect(isl_basic_map_copy(bmap1
),
3972 isl_basic_map_copy(bmap2
));
3973 disjoint
= isl_basic_map_is_empty(test
);
3974 isl_basic_map_free(test
);
3979 /* Are "bset1" and "bset2" disjoint?
3981 isl_bool
isl_basic_set_is_disjoint(__isl_keep isl_basic_set
*bset1
,
3982 __isl_keep isl_basic_set
*bset2
)
3984 return isl_basic_map_is_disjoint(bset1
, bset2
);
3987 isl_bool
isl_set_plain_is_disjoint(__isl_keep isl_set
*set1
,
3988 __isl_keep isl_set
*set2
)
3990 return isl_map_plain_is_disjoint(set_to_map(set1
), set_to_map(set2
));
3993 /* Are "set1" and "set2" disjoint?
3995 isl_bool
isl_set_is_disjoint(__isl_keep isl_set
*set1
, __isl_keep isl_set
*set2
)
3997 return isl_map_is_disjoint(set1
, set2
);
4000 /* Is "v" equal to 0, 1 or -1?
4002 static int is_zero_or_one(isl_int v
)
4004 return isl_int_is_zero(v
) || isl_int_is_one(v
) || isl_int_is_negone(v
);
4007 /* Check if we can combine a given div with lower bound l and upper
4008 * bound u with some other div and if so return that other div.
4009 * Otherwise return -1.
4011 * We first check that
4012 * - the bounds are opposites of each other (except for the constant
4014 * - the bounds do not reference any other div
4015 * - no div is defined in terms of this div
4017 * Let m be the size of the range allowed on the div by the bounds.
4018 * That is, the bounds are of the form
4020 * e <= a <= e + m - 1
4022 * with e some expression in the other variables.
4023 * We look for another div b such that no third div is defined in terms
4024 * of this second div b and such that in any constraint that contains
4025 * a (except for the given lower and upper bound), also contains b
4026 * with a coefficient that is m times that of b.
4027 * That is, all constraints (execpt for the lower and upper bound)
4030 * e + f (a + m b) >= 0
4032 * Furthermore, in the constraints that only contain b, the coefficient
4033 * of b should be equal to 1 or -1.
4034 * If so, we return b so that "a + m b" can be replaced by
4035 * a single div "c = a + m b".
4037 static int div_find_coalesce(struct isl_basic_map
*bmap
, int *pairs
,
4038 unsigned div
, unsigned l
, unsigned u
)
4044 if (bmap
->n_div
<= 1)
4046 dim
= isl_space_dim(bmap
->dim
, isl_dim_all
);
4047 if (isl_seq_first_non_zero(bmap
->ineq
[l
] + 1 + dim
, div
) != -1)
4049 if (isl_seq_first_non_zero(bmap
->ineq
[l
] + 1 + dim
+ div
+ 1,
4050 bmap
->n_div
- div
- 1) != -1)
4052 if (!isl_seq_is_neg(bmap
->ineq
[l
] + 1, bmap
->ineq
[u
] + 1,
4056 for (i
= 0; i
< bmap
->n_div
; ++i
) {
4057 if (isl_int_is_zero(bmap
->div
[i
][0]))
4059 if (!isl_int_is_zero(bmap
->div
[i
][1 + 1 + dim
+ div
]))
4063 isl_int_add(bmap
->ineq
[l
][0], bmap
->ineq
[l
][0], bmap
->ineq
[u
][0]);
4064 if (isl_int_is_neg(bmap
->ineq
[l
][0])) {
4065 isl_int_sub(bmap
->ineq
[l
][0],
4066 bmap
->ineq
[l
][0], bmap
->ineq
[u
][0]);
4067 bmap
= isl_basic_map_copy(bmap
);
4068 bmap
= isl_basic_map_set_to_empty(bmap
);
4069 isl_basic_map_free(bmap
);
4072 isl_int_add_ui(bmap
->ineq
[l
][0], bmap
->ineq
[l
][0], 1);
4073 for (i
= 0; i
< bmap
->n_div
; ++i
) {
4078 for (j
= 0; j
< bmap
->n_div
; ++j
) {
4079 if (isl_int_is_zero(bmap
->div
[j
][0]))
4081 if (!isl_int_is_zero(bmap
->div
[j
][1 + 1 + dim
+ i
]))
4084 if (j
< bmap
->n_div
)
4086 for (j
= 0; j
< bmap
->n_ineq
; ++j
) {
4088 if (j
== l
|| j
== u
)
4090 if (isl_int_is_zero(bmap
->ineq
[j
][1 + dim
+ div
])) {
4091 if (is_zero_or_one(bmap
->ineq
[j
][1 + dim
+ i
]))
4095 if (isl_int_is_zero(bmap
->ineq
[j
][1 + dim
+ i
]))
4097 isl_int_mul(bmap
->ineq
[j
][1 + dim
+ div
],
4098 bmap
->ineq
[j
][1 + dim
+ div
],
4100 valid
= isl_int_eq(bmap
->ineq
[j
][1 + dim
+ div
],
4101 bmap
->ineq
[j
][1 + dim
+ i
]);
4102 isl_int_divexact(bmap
->ineq
[j
][1 + dim
+ div
],
4103 bmap
->ineq
[j
][1 + dim
+ div
],
4108 if (j
< bmap
->n_ineq
)
4113 isl_int_sub_ui(bmap
->ineq
[l
][0], bmap
->ineq
[l
][0], 1);
4114 isl_int_sub(bmap
->ineq
[l
][0], bmap
->ineq
[l
][0], bmap
->ineq
[u
][0]);
4118 /* Internal data structure used during the construction and/or evaluation of
4119 * an inequality that ensures that a pair of bounds always allows
4120 * for an integer value.
4122 * "tab" is the tableau in which the inequality is evaluated. It may
4123 * be NULL until it is actually needed.
4124 * "v" contains the inequality coefficients.
4125 * "g", "fl" and "fu" are temporary scalars used during the construction and
4128 struct test_ineq_data
{
4129 struct isl_tab
*tab
;
4136 /* Free all the memory allocated by the fields of "data".
4138 static void test_ineq_data_clear(struct test_ineq_data
*data
)
4140 isl_tab_free(data
->tab
);
4141 isl_vec_free(data
->v
);
4142 isl_int_clear(data
->g
);
4143 isl_int_clear(data
->fl
);
4144 isl_int_clear(data
->fu
);
4147 /* Is the inequality stored in data->v satisfied by "bmap"?
4148 * That is, does it only attain non-negative values?
4149 * data->tab is a tableau corresponding to "bmap".
4151 static isl_bool
test_ineq_is_satisfied(__isl_keep isl_basic_map
*bmap
,
4152 struct test_ineq_data
*data
)
4155 enum isl_lp_result res
;
4157 ctx
= isl_basic_map_get_ctx(bmap
);
4159 data
->tab
= isl_tab_from_basic_map(bmap
, 0);
4160 res
= isl_tab_min(data
->tab
, data
->v
->el
, ctx
->one
, &data
->g
, NULL
, 0);
4161 if (res
== isl_lp_error
)
4162 return isl_bool_error
;
4163 return res
== isl_lp_ok
&& isl_int_is_nonneg(data
->g
);
4166 /* Given a lower and an upper bound on div i, do they always allow
4167 * for an integer value of the given div?
4168 * Determine this property by constructing an inequality
4169 * such that the property is guaranteed when the inequality is nonnegative.
4170 * The lower bound is inequality l, while the upper bound is inequality u.
4171 * The constructed inequality is stored in data->v.
4173 * Let the upper bound be
4177 * and the lower bound
4181 * Let n_u = f_u g and n_l = f_l g, with g = gcd(n_u, n_l).
4184 * - f_u e_l <= f_u f_l g a <= f_l e_u
4186 * Since all variables are integer valued, this is equivalent to
4188 * - f_u e_l - (f_u - 1) <= f_u f_l g a <= f_l e_u + (f_l - 1)
4190 * If this interval is at least f_u f_l g, then it contains at least
4191 * one integer value for a.
4192 * That is, the test constraint is
4194 * f_l e_u + f_u e_l + f_l - 1 + f_u - 1 + 1 >= f_u f_l g
4198 * f_l e_u + f_u e_l + f_l - 1 + f_u - 1 + 1 - f_u f_l g >= 0
4200 * If the coefficients of f_l e_u + f_u e_l have a common divisor g',
4201 * then the constraint can be scaled down by a factor g',
4202 * with the constant term replaced by
4203 * floor((f_l e_{u,0} + f_u e_{l,0} + f_l - 1 + f_u - 1 + 1 - f_u f_l g)/g').
4204 * Note that the result of applying Fourier-Motzkin to this pair
4207 * f_l e_u + f_u e_l >= 0
4209 * If the constant term of the scaled down version of this constraint,
4210 * i.e., floor((f_l e_{u,0} + f_u e_{l,0})/g') is equal to the constant
4211 * term of the scaled down test constraint, then the test constraint
4212 * is known to hold and no explicit evaluation is required.
4213 * This is essentially the Omega test.
4215 * If the test constraint consists of only a constant term, then
4216 * it is sufficient to look at the sign of this constant term.
4218 static isl_bool
int_between_bounds(__isl_keep isl_basic_map
*bmap
, int i
,
4219 int l
, int u
, struct test_ineq_data
*data
)
4221 unsigned offset
, n_div
;
4222 offset
= isl_basic_map_offset(bmap
, isl_dim_div
);
4223 n_div
= isl_basic_map_dim(bmap
, isl_dim_div
);
4225 isl_int_gcd(data
->g
,
4226 bmap
->ineq
[l
][offset
+ i
], bmap
->ineq
[u
][offset
+ i
]);
4227 isl_int_divexact(data
->fl
, bmap
->ineq
[l
][offset
+ i
], data
->g
);
4228 isl_int_divexact(data
->fu
, bmap
->ineq
[u
][offset
+ i
], data
->g
);
4229 isl_int_neg(data
->fu
, data
->fu
);
4230 isl_seq_combine(data
->v
->el
, data
->fl
, bmap
->ineq
[u
],
4231 data
->fu
, bmap
->ineq
[l
], offset
+ n_div
);
4232 isl_int_mul(data
->g
, data
->g
, data
->fl
);
4233 isl_int_mul(data
->g
, data
->g
, data
->fu
);
4234 isl_int_sub(data
->g
, data
->g
, data
->fl
);
4235 isl_int_sub(data
->g
, data
->g
, data
->fu
);
4236 isl_int_add_ui(data
->g
, data
->g
, 1);
4237 isl_int_sub(data
->fl
, data
->v
->el
[0], data
->g
);
4239 isl_seq_gcd(data
->v
->el
+ 1, offset
- 1 + n_div
, &data
->g
);
4240 if (isl_int_is_zero(data
->g
))
4241 return isl_int_is_nonneg(data
->fl
);
4242 if (isl_int_is_one(data
->g
)) {
4243 isl_int_set(data
->v
->el
[0], data
->fl
);
4244 return test_ineq_is_satisfied(bmap
, data
);
4246 isl_int_fdiv_q(data
->fl
, data
->fl
, data
->g
);
4247 isl_int_fdiv_q(data
->v
->el
[0], data
->v
->el
[0], data
->g
);
4248 if (isl_int_eq(data
->fl
, data
->v
->el
[0]))
4249 return isl_bool_true
;
4250 isl_int_set(data
->v
->el
[0], data
->fl
);
4251 isl_seq_scale_down(data
->v
->el
+ 1, data
->v
->el
+ 1, data
->g
,
4252 offset
- 1 + n_div
);
4254 return test_ineq_is_satisfied(bmap
, data
);
4257 /* Remove more kinds of divs that are not strictly needed.
4258 * In particular, if all pairs of lower and upper bounds on a div
4259 * are such that they allow at least one integer value of the div,
4260 * then we can eliminate the div using Fourier-Motzkin without
4261 * introducing any spurious solutions.
4263 * If at least one of the two constraints has a unit coefficient for the div,
4264 * then the presence of such a value is guaranteed so there is no need to check.
4265 * In particular, the value attained by the bound with unit coefficient
4266 * can serve as this intermediate value.
4268 static struct isl_basic_map
*drop_more_redundant_divs(
4269 struct isl_basic_map
*bmap
, int *pairs
, int n
)
4272 struct test_ineq_data data
= { NULL
, NULL
};
4273 unsigned off
, n_div
;
4276 isl_int_init(data
.g
);
4277 isl_int_init(data
.fl
);
4278 isl_int_init(data
.fu
);
4283 ctx
= isl_basic_map_get_ctx(bmap
);
4284 off
= isl_basic_map_offset(bmap
, isl_dim_div
);
4285 n_div
= isl_basic_map_dim(bmap
, isl_dim_div
);
4286 data
.v
= isl_vec_alloc(ctx
, off
+ n_div
);
4295 for (i
= 0; i
< n_div
; ++i
) {
4298 if (best
>= 0 && pairs
[best
] <= pairs
[i
])
4304 for (l
= 0; l
< bmap
->n_ineq
; ++l
) {
4305 if (!isl_int_is_pos(bmap
->ineq
[l
][off
+ i
]))
4307 if (isl_int_is_one(bmap
->ineq
[l
][off
+ i
]))
4309 for (u
= 0; u
< bmap
->n_ineq
; ++u
) {
4310 if (!isl_int_is_neg(bmap
->ineq
[u
][off
+ i
]))
4312 if (isl_int_is_negone(bmap
->ineq
[u
][off
+ i
]))
4314 has_int
= int_between_bounds(bmap
, i
, l
, u
,
4318 if (data
.tab
&& data
.tab
->empty
)
4323 if (u
< bmap
->n_ineq
)
4326 if (data
.tab
&& data
.tab
->empty
) {
4327 bmap
= isl_basic_map_set_to_empty(bmap
);
4330 if (l
== bmap
->n_ineq
) {
4338 test_ineq_data_clear(&data
);
4345 bmap
= isl_basic_map_remove_dims(bmap
, isl_dim_div
, remove
, 1);
4346 return isl_basic_map_drop_redundant_divs(bmap
);
4349 isl_basic_map_free(bmap
);
4350 test_ineq_data_clear(&data
);
4354 /* Given a pair of divs div1 and div2 such that, except for the lower bound l
4355 * and the upper bound u, div1 always occurs together with div2 in the form
4356 * (div1 + m div2), where m is the constant range on the variable div1
4357 * allowed by l and u, replace the pair div1 and div2 by a single
4358 * div that is equal to div1 + m div2.
4360 * The new div will appear in the location that contains div2.
4361 * We need to modify all constraints that contain
4362 * div2 = (div - div1) / m
4363 * The coefficient of div2 is known to be equal to 1 or -1.
4364 * (If a constraint does not contain div2, it will also not contain div1.)
4365 * If the constraint also contains div1, then we know they appear
4366 * as f (div1 + m div2) and we can simply replace (div1 + m div2) by div,
4367 * i.e., the coefficient of div is f.
4369 * Otherwise, we first need to introduce div1 into the constraint.
4378 * A lower bound on div2
4382 * can be replaced by
4384 * m div2 + div1 + m t + f >= 0
4390 * can be replaced by
4392 * -(m div2 + div1) + m t + f' >= 0
4394 * These constraint are those that we would obtain from eliminating
4395 * div1 using Fourier-Motzkin.
4397 * After all constraints have been modified, we drop the lower and upper
4398 * bound and then drop div1.
4400 static struct isl_basic_map
*coalesce_divs(struct isl_basic_map
*bmap
,
4401 unsigned div1
, unsigned div2
, unsigned l
, unsigned u
)
4405 unsigned dim
, total
;
4408 ctx
= isl_basic_map_get_ctx(bmap
);
4410 dim
= isl_space_dim(bmap
->dim
, isl_dim_all
);
4411 total
= 1 + dim
+ bmap
->n_div
;
4414 isl_int_add(m
, bmap
->ineq
[l
][0], bmap
->ineq
[u
][0]);
4415 isl_int_add_ui(m
, m
, 1);
4417 for (i
= 0; i
< bmap
->n_ineq
; ++i
) {
4418 if (i
== l
|| i
== u
)
4420 if (isl_int_is_zero(bmap
->ineq
[i
][1 + dim
+ div2
]))
4422 if (isl_int_is_zero(bmap
->ineq
[i
][1 + dim
+ div1
])) {
4423 if (isl_int_is_pos(bmap
->ineq
[i
][1 + dim
+ div2
]))
4424 isl_seq_combine(bmap
->ineq
[i
], m
, bmap
->ineq
[i
],
4425 ctx
->one
, bmap
->ineq
[l
], total
);
4427 isl_seq_combine(bmap
->ineq
[i
], m
, bmap
->ineq
[i
],
4428 ctx
->one
, bmap
->ineq
[u
], total
);
4430 isl_int_set(bmap
->ineq
[i
][1 + dim
+ div2
],
4431 bmap
->ineq
[i
][1 + dim
+ div1
]);
4432 isl_int_set_si(bmap
->ineq
[i
][1 + dim
+ div1
], 0);
4437 isl_basic_map_drop_inequality(bmap
, l
);
4438 isl_basic_map_drop_inequality(bmap
, u
);
4440 isl_basic_map_drop_inequality(bmap
, u
);
4441 isl_basic_map_drop_inequality(bmap
, l
);
4443 bmap
= isl_basic_map_drop_div(bmap
, div1
);
4447 /* First check if we can coalesce any pair of divs and
4448 * then continue with dropping more redundant divs.
4450 * We loop over all pairs of lower and upper bounds on a div
4451 * with coefficient 1 and -1, respectively, check if there
4452 * is any other div "c" with which we can coalesce the div
4453 * and if so, perform the coalescing.
4455 static struct isl_basic_map
*coalesce_or_drop_more_redundant_divs(
4456 struct isl_basic_map
*bmap
, int *pairs
, int n
)
4461 dim
= isl_space_dim(bmap
->dim
, isl_dim_all
);
4463 for (i
= 0; i
< bmap
->n_div
; ++i
) {
4466 for (l
= 0; l
< bmap
->n_ineq
; ++l
) {
4467 if (!isl_int_is_one(bmap
->ineq
[l
][1 + dim
+ i
]))
4469 for (u
= 0; u
< bmap
->n_ineq
; ++u
) {
4472 if (!isl_int_is_negone(bmap
->ineq
[u
][1+dim
+i
]))
4474 c
= div_find_coalesce(bmap
, pairs
, i
, l
, u
);
4478 bmap
= coalesce_divs(bmap
, i
, c
, l
, u
);
4479 return isl_basic_map_drop_redundant_divs(bmap
);
4484 if (ISL_F_ISSET(bmap
, ISL_BASIC_MAP_EMPTY
)) {
4489 return drop_more_redundant_divs(bmap
, pairs
, n
);
4492 /* Are the "n" coefficients starting at "first" of inequality constraints
4493 * "i" and "j" of "bmap" equal to each other?
4495 static int is_parallel_part(__isl_keep isl_basic_map
*bmap
, int i
, int j
,
4498 return isl_seq_eq(bmap
->ineq
[i
] + first
, bmap
->ineq
[j
] + first
, n
);
4501 /* Are the "n" coefficients starting at "first" of inequality constraints
4502 * "i" and "j" of "bmap" opposite to each other?
4504 static int is_opposite_part(__isl_keep isl_basic_map
*bmap
, int i
, int j
,
4507 return isl_seq_is_neg(bmap
->ineq
[i
] + first
, bmap
->ineq
[j
] + first
, n
);
4510 /* Are inequality constraints "i" and "j" of "bmap" opposite to each other,
4511 * apart from the constant term?
4513 static int is_opposite(__isl_keep isl_basic_map
*bmap
, int i
, int j
)
4517 total
= isl_basic_map_dim(bmap
, isl_dim_all
);
4518 return is_opposite_part(bmap
, i
, j
, 1, total
);
4521 /* Are inequality constraints "i" and "j" of "bmap" equal to each other,
4522 * apart from the constant term and the coefficient at position "pos"?
4524 static int is_parallel_except(__isl_keep isl_basic_map
*bmap
, int i
, int j
,
4529 total
= isl_basic_map_dim(bmap
, isl_dim_all
);
4530 return is_parallel_part(bmap
, i
, j
, 1, pos
- 1) &&
4531 is_parallel_part(bmap
, i
, j
, pos
+ 1, total
- pos
);
4534 /* Are inequality constraints "i" and "j" of "bmap" opposite to each other,
4535 * apart from the constant term and the coefficient at position "pos"?
4537 static int is_opposite_except(__isl_keep isl_basic_map
*bmap
, int i
, int j
,
4542 total
= isl_basic_map_dim(bmap
, isl_dim_all
);
4543 return is_opposite_part(bmap
, i
, j
, 1, pos
- 1) &&
4544 is_opposite_part(bmap
, i
, j
, pos
+ 1, total
- pos
);
4547 /* Restart isl_basic_map_drop_redundant_divs after "bmap" has
4548 * been modified, simplying it if "simplify" is set.
4549 * Free the temporary data structure "pairs" that was associated
4550 * to the old version of "bmap".
4552 static __isl_give isl_basic_map
*drop_redundant_divs_again(
4553 __isl_take isl_basic_map
*bmap
, __isl_take
int *pairs
, int simplify
)
4556 bmap
= isl_basic_map_simplify(bmap
);
4558 return isl_basic_map_drop_redundant_divs(bmap
);
4561 /* Is "div" the single unknown existentially quantified variable
4562 * in inequality constraint "ineq" of "bmap"?
4563 * "div" is known to have a non-zero coefficient in "ineq".
4565 static int single_unknown(__isl_keep isl_basic_map
*bmap
, int ineq
, int div
)
4568 unsigned n_div
, o_div
;
4570 if (isl_basic_map_div_is_known(bmap
, div
))
4572 n_div
= isl_basic_map_dim(bmap
, isl_dim_div
);
4575 o_div
= isl_basic_map_offset(bmap
, isl_dim_div
);
4576 for (i
= 0; i
< n_div
; ++i
) {
4579 if (isl_int_is_zero(bmap
->ineq
[ineq
][o_div
+ i
]))
4581 if (!isl_basic_map_div_is_known(bmap
, i
))
4588 /* Does integer division "div" have coefficient 1 in inequality constraint
4591 static int has_coef_one(__isl_keep isl_basic_map
*bmap
, int div
, int ineq
)
4595 o_div
= isl_basic_map_offset(bmap
, isl_dim_div
);
4596 if (isl_int_is_one(bmap
->ineq
[ineq
][o_div
+ div
]))
4602 /* Turn inequality constraint "ineq" of "bmap" into an equality and
4603 * then try and drop redundant divs again,
4604 * freeing the temporary data structure "pairs" that was associated
4605 * to the old version of "bmap".
4607 static __isl_give isl_basic_map
*set_eq_and_try_again(
4608 __isl_take isl_basic_map
*bmap
, int ineq
, __isl_take
int *pairs
)
4610 bmap
= isl_basic_map_cow(bmap
);
4611 isl_basic_map_inequality_to_equality(bmap
, ineq
);
4612 return drop_redundant_divs_again(bmap
, pairs
, 1);
4615 /* Drop the integer division at position "div", along with the two
4616 * inequality constraints "ineq1" and "ineq2" in which it appears
4617 * from "bmap" and then try and drop redundant divs again,
4618 * freeing the temporary data structure "pairs" that was associated
4619 * to the old version of "bmap".
4621 static __isl_give isl_basic_map
*drop_div_and_try_again(
4622 __isl_take isl_basic_map
*bmap
, int div
, int ineq1
, int ineq2
,
4623 __isl_take
int *pairs
)
4625 if (ineq1
> ineq2
) {
4626 isl_basic_map_drop_inequality(bmap
, ineq1
);
4627 isl_basic_map_drop_inequality(bmap
, ineq2
);
4629 isl_basic_map_drop_inequality(bmap
, ineq2
);
4630 isl_basic_map_drop_inequality(bmap
, ineq1
);
4632 bmap
= isl_basic_map_drop_div(bmap
, div
);
4633 return drop_redundant_divs_again(bmap
, pairs
, 0);
4636 /* Given two inequality constraints
4638 * f(x) + n d + c >= 0, (ineq)
4640 * with d the variable at position "pos", and
4642 * f(x) + c0 >= 0, (lower)
4644 * compute the maximal value of the lower bound ceil((-f(x) - c)/n)
4645 * determined by the first constraint.
4652 static void lower_bound_from_parallel(__isl_keep isl_basic_map
*bmap
,
4653 int ineq
, int lower
, int pos
, isl_int
*l
)
4655 isl_int_neg(*l
, bmap
->ineq
[ineq
][0]);
4656 isl_int_add(*l
, *l
, bmap
->ineq
[lower
][0]);
4657 isl_int_cdiv_q(*l
, *l
, bmap
->ineq
[ineq
][pos
]);
4660 /* Given two inequality constraints
4662 * f(x) + n d + c >= 0, (ineq)
4664 * with d the variable at position "pos", and
4666 * -f(x) - c0 >= 0, (upper)
4668 * compute the minimal value of the lower bound ceil((-f(x) - c)/n)
4669 * determined by the first constraint.
4676 static void lower_bound_from_opposite(__isl_keep isl_basic_map
*bmap
,
4677 int ineq
, int upper
, int pos
, isl_int
*u
)
4679 isl_int_neg(*u
, bmap
->ineq
[ineq
][0]);
4680 isl_int_sub(*u
, *u
, bmap
->ineq
[upper
][0]);
4681 isl_int_cdiv_q(*u
, *u
, bmap
->ineq
[ineq
][pos
]);
4684 /* Given a lower bound constraint "ineq" on "div" in "bmap",
4685 * does the corresponding lower bound have a fixed value in "bmap"?
4687 * In particular, "ineq" is of the form
4689 * f(x) + n d + c >= 0
4691 * with n > 0, c the constant term and
4692 * d the existentially quantified variable "div".
4693 * That is, the lower bound is
4695 * ceil((-f(x) - c)/n)
4697 * Look for a pair of constraints
4702 * i.e., -c1 <= -f(x) <= c0, that fix ceil((-f(x) - c)/n) to a constant value.
4703 * That is, check that
4705 * ceil((-c1 - c)/n) = ceil((c0 - c)/n)
4707 * If so, return the index of inequality f(x) + c0 >= 0.
4708 * Otherwise, return -1.
4710 static int lower_bound_is_cst(__isl_keep isl_basic_map
*bmap
, int div
, int ineq
)
4713 int lower
= -1, upper
= -1;
4714 unsigned o_div
, n_div
;
4718 n_div
= isl_basic_map_dim(bmap
, isl_dim_div
);
4719 o_div
= isl_basic_map_offset(bmap
, isl_dim_div
);
4720 for (i
= 0; i
< bmap
->n_ineq
&& (lower
< 0 || upper
< 0); ++i
) {
4723 if (!isl_int_is_zero(bmap
->ineq
[i
][o_div
+ div
]))
4726 is_parallel_except(bmap
, ineq
, i
, o_div
+ div
)) {
4731 is_opposite_except(bmap
, ineq
, i
, o_div
+ div
)) {
4736 if (lower
< 0 || upper
< 0)
4742 lower_bound_from_parallel(bmap
, ineq
, lower
, o_div
+ div
, &l
);
4743 lower_bound_from_opposite(bmap
, ineq
, upper
, o_div
+ div
, &u
);
4745 equal
= isl_int_eq(l
, u
);
4750 return equal
? lower
: -1;
4753 /* Given a lower bound constraint "ineq" on the existentially quantified
4754 * variable "div", such that the corresponding lower bound has
4755 * a fixed value in "bmap", assign this fixed value to the variable and
4756 * then try and drop redundant divs again,
4757 * freeing the temporary data structure "pairs" that was associated
4758 * to the old version of "bmap".
4759 * "lower" determines the constant value for the lower bound.
4761 * In particular, "ineq" is of the form
4763 * f(x) + n d + c >= 0,
4765 * while "lower" is of the form
4769 * The lower bound is ceil((-f(x) - c)/n) and its constant value
4770 * is ceil((c0 - c)/n).
4772 static __isl_give isl_basic_map
*fix_cst_lower(__isl_take isl_basic_map
*bmap
,
4773 int div
, int ineq
, int lower
, int *pairs
)
4780 o_div
= isl_basic_map_offset(bmap
, isl_dim_div
);
4781 lower_bound_from_parallel(bmap
, ineq
, lower
, o_div
+ div
, &c
);
4782 bmap
= isl_basic_map_fix(bmap
, isl_dim_div
, div
, c
);
4787 return isl_basic_map_drop_redundant_divs(bmap
);
4790 /* Remove divs that are not strictly needed based on the inequality
4792 * In particular, if a div only occurs positively (or negatively)
4793 * in constraints, then it can simply be dropped.
4794 * Also, if a div occurs in only two constraints and if moreover
4795 * those two constraints are opposite to each other, except for the constant
4796 * term and if the sum of the constant terms is such that for any value
4797 * of the other values, there is always at least one integer value of the
4798 * div, i.e., if one plus this sum is greater than or equal to
4799 * the (absolute value) of the coefficient of the div in the constraints,
4800 * then we can also simply drop the div.
4802 * If an existentially quantified variable does not have an explicit
4803 * representation, appears in only a single lower bound that does not
4804 * involve any other such existentially quantified variables and appears
4805 * in this lower bound with coefficient 1,
4806 * then fix the variable to the value of the lower bound. That is,
4807 * turn the inequality into an equality.
4808 * If for any value of the other variables, there is any value
4809 * for the existentially quantified variable satisfying the constraints,
4810 * then this lower bound also satisfies the constraints.
4811 * It is therefore safe to pick this lower bound.
4813 * The same reasoning holds even if the coefficient is not one.
4814 * However, fixing the variable to the value of the lower bound may
4815 * in general introduce an extra integer division, in which case
4816 * it may be better to pick another value.
4817 * If this integer division has a known constant value, then plugging
4818 * in this constant value removes the existentially quantified variable
4819 * completely. In particular, if the lower bound is of the form
4820 * ceil((-f(x) - c)/n) and there are two constraints, f(x) + c0 >= 0 and
4821 * -f(x) + c1 >= 0 such that ceil((-c1 - c)/n) = ceil((c0 - c)/n),
4822 * then the existentially quantified variable can be assigned this
4825 * We skip divs that appear in equalities or in the definition of other divs.
4826 * Divs that appear in the definition of other divs usually occur in at least
4827 * 4 constraints, but the constraints may have been simplified.
4829 * If any divs are left after these simple checks then we move on
4830 * to more complicated cases in drop_more_redundant_divs.
4832 static __isl_give isl_basic_map
*isl_basic_map_drop_redundant_divs_ineq(
4833 __isl_take isl_basic_map
*bmap
)
4842 if (bmap
->n_div
== 0)
4845 off
= isl_space_dim(bmap
->dim
, isl_dim_all
);
4846 pairs
= isl_calloc_array(bmap
->ctx
, int, bmap
->n_div
);
4850 for (i
= 0; i
< bmap
->n_div
; ++i
) {
4852 int last_pos
, last_neg
;
4856 defined
= !isl_int_is_zero(bmap
->div
[i
][0]);
4857 for (j
= i
; j
< bmap
->n_div
; ++j
)
4858 if (!isl_int_is_zero(bmap
->div
[j
][1 + 1 + off
+ i
]))
4860 if (j
< bmap
->n_div
)
4862 for (j
= 0; j
< bmap
->n_eq
; ++j
)
4863 if (!isl_int_is_zero(bmap
->eq
[j
][1 + off
+ i
]))
4869 for (j
= 0; j
< bmap
->n_ineq
; ++j
) {
4870 if (isl_int_is_pos(bmap
->ineq
[j
][1 + off
+ i
])) {
4874 if (isl_int_is_neg(bmap
->ineq
[j
][1 + off
+ i
])) {
4879 pairs
[i
] = pos
* neg
;
4880 if (pairs
[i
] == 0) {
4881 for (j
= bmap
->n_ineq
- 1; j
>= 0; --j
)
4882 if (!isl_int_is_zero(bmap
->ineq
[j
][1+off
+i
]))
4883 isl_basic_map_drop_inequality(bmap
, j
);
4884 bmap
= isl_basic_map_drop_div(bmap
, i
);
4885 return drop_redundant_divs_again(bmap
, pairs
, 0);
4887 if (pairs
[i
] != 1 || !is_opposite(bmap
, last_pos
, last_neg
)) {
4891 single
= single_unknown(bmap
, last_pos
, i
);
4894 if (has_coef_one(bmap
, i
, last_pos
))
4895 return set_eq_and_try_again(bmap
, last_pos
,
4897 lower
= lower_bound_is_cst(bmap
, i
, last_pos
);
4899 return fix_cst_lower(bmap
, i
, last_pos
, lower
,
4904 isl_int_add(bmap
->ineq
[last_pos
][0],
4905 bmap
->ineq
[last_pos
][0], bmap
->ineq
[last_neg
][0]);
4906 isl_int_add_ui(bmap
->ineq
[last_pos
][0],
4907 bmap
->ineq
[last_pos
][0], 1);
4908 redundant
= isl_int_ge(bmap
->ineq
[last_pos
][0],
4909 bmap
->ineq
[last_pos
][1+off
+i
]);
4910 isl_int_sub_ui(bmap
->ineq
[last_pos
][0],
4911 bmap
->ineq
[last_pos
][0], 1);
4912 isl_int_sub(bmap
->ineq
[last_pos
][0],
4913 bmap
->ineq
[last_pos
][0], bmap
->ineq
[last_neg
][0]);
4915 return drop_div_and_try_again(bmap
, i
,
4916 last_pos
, last_neg
, pairs
);
4917 if (!defined
&& ok_to_set_div_from_bound(bmap
, i
, last_pos
)) {
4918 bmap
= set_div_from_lower_bound(bmap
, i
, last_pos
);
4919 return drop_redundant_divs_again(bmap
, pairs
, 1);
4926 return coalesce_or_drop_more_redundant_divs(bmap
, pairs
, n
);
4932 isl_basic_map_free(bmap
);
4936 /* Consider the coefficients at "c" as a row vector and replace
4937 * them with their product with "T". "T" is assumed to be a square matrix.
4939 static isl_stat
preimage(isl_int
*c
, __isl_keep isl_mat
*T
)
4946 return isl_stat_error
;
4947 n
= isl_mat_rows(T
);
4948 if (isl_seq_first_non_zero(c
, n
) == -1)
4950 ctx
= isl_mat_get_ctx(T
);
4951 v
= isl_vec_alloc(ctx
, n
);
4953 return isl_stat_error
;
4954 isl_seq_swp_or_cpy(v
->el
, c
, n
);
4955 v
= isl_vec_mat_product(v
, isl_mat_copy(T
));
4957 return isl_stat_error
;
4958 isl_seq_swp_or_cpy(c
, v
->el
, n
);
4964 /* Plug in T for the variables in "bmap" starting at "pos".
4965 * T is a linear unimodular matrix, i.e., without constant term.
4967 static __isl_give isl_basic_map
*isl_basic_map_preimage_vars(
4968 __isl_take isl_basic_map
*bmap
, unsigned pos
, __isl_take isl_mat
*T
)
4973 bmap
= isl_basic_map_cow(bmap
);
4977 n
= isl_mat_cols(T
);
4978 if (n
!= isl_mat_rows(T
))
4979 isl_die(isl_mat_get_ctx(T
), isl_error_invalid
,
4980 "expecting square matrix", goto error
);
4982 total
= isl_basic_map_dim(bmap
, isl_dim_all
);
4983 if (pos
+ n
> total
|| pos
+ n
< pos
)
4984 isl_die(isl_mat_get_ctx(T
), isl_error_invalid
,
4985 "invalid range", goto error
);
4987 for (i
= 0; i
< bmap
->n_eq
; ++i
)
4988 if (preimage(bmap
->eq
[i
] + 1 + pos
, T
) < 0)
4990 for (i
= 0; i
< bmap
->n_ineq
; ++i
)
4991 if (preimage(bmap
->ineq
[i
] + 1 + pos
, T
) < 0)
4993 for (i
= 0; i
< bmap
->n_div
; ++i
) {
4994 if (isl_basic_map_div_is_marked_unknown(bmap
, i
))
4996 if (preimage(bmap
->div
[i
] + 1 + 1 + pos
, T
) < 0)
5003 isl_basic_map_free(bmap
);
5008 /* Remove divs that are not strictly needed.
5010 * First look for an equality constraint involving two or more
5011 * existentially quantified variables without an explicit
5012 * representation. Replace the combination that appears
5013 * in the equality constraint by a single existentially quantified
5014 * variable such that the equality can be used to derive
5015 * an explicit representation for the variable.
5016 * If there are no more such equality constraints, then continue
5017 * with isl_basic_map_drop_redundant_divs_ineq.
5019 * In particular, if the equality constraint is of the form
5021 * f(x) + \sum_i c_i a_i = 0
5023 * with a_i existentially quantified variable without explicit
5024 * representation, then apply a transformation on the existentially
5025 * quantified variables to turn the constraint into
5029 * with g the gcd of the c_i.
5030 * In order to easily identify which existentially quantified variables
5031 * have a complete explicit representation, i.e., without being defined
5032 * in terms of other existentially quantified variables without
5033 * an explicit representation, the existentially quantified variables
5036 * The variable transformation is computed by extending the row
5037 * [c_1/g ... c_n/g] to a unimodular matrix, obtaining the transformation
5039 * [a_1'] [c_1/g ... c_n/g] [ a_1 ]
5044 * with [c_1/g ... c_n/g] representing the first row of U.
5045 * The inverse of U is then plugged into the original constraints.
5046 * The call to isl_basic_map_simplify makes sure the explicit
5047 * representation for a_1' is extracted from the equality constraint.
5049 __isl_give isl_basic_map
*isl_basic_map_drop_redundant_divs(
5050 __isl_take isl_basic_map
*bmap
)
5054 unsigned o_div
, n_div
;
5061 if (isl_basic_map_divs_known(bmap
))
5062 return isl_basic_map_drop_redundant_divs_ineq(bmap
);
5063 if (bmap
->n_eq
== 0)
5064 return isl_basic_map_drop_redundant_divs_ineq(bmap
);
5065 bmap
= isl_basic_map_sort_divs(bmap
);
5069 first
= isl_basic_map_first_unknown_div(bmap
);
5071 return isl_basic_map_free(bmap
);
5073 o_div
= isl_basic_map_offset(bmap
, isl_dim_div
);
5074 n_div
= isl_basic_map_dim(bmap
, isl_dim_div
);
5076 for (i
= 0; i
< bmap
->n_eq
; ++i
) {
5077 l
= isl_seq_first_non_zero(bmap
->eq
[i
] + o_div
+ first
,
5082 if (isl_seq_first_non_zero(bmap
->eq
[i
] + o_div
+ l
+ 1,
5083 n_div
- (l
+ 1)) == -1)
5087 if (i
>= bmap
->n_eq
)
5088 return isl_basic_map_drop_redundant_divs_ineq(bmap
);
5090 ctx
= isl_basic_map_get_ctx(bmap
);
5091 T
= isl_mat_alloc(ctx
, n_div
- l
, n_div
- l
);
5093 return isl_basic_map_free(bmap
);
5094 isl_seq_cpy(T
->row
[0], bmap
->eq
[i
] + o_div
+ l
, n_div
- l
);
5095 T
= isl_mat_normalize_row(T
, 0);
5096 T
= isl_mat_unimodular_complete(T
, 1);
5097 T
= isl_mat_right_inverse(T
);
5099 for (i
= l
; i
< n_div
; ++i
)
5100 bmap
= isl_basic_map_mark_div_unknown(bmap
, i
);
5101 bmap
= isl_basic_map_preimage_vars(bmap
, o_div
- 1 + l
, T
);
5102 bmap
= isl_basic_map_simplify(bmap
);
5104 return isl_basic_map_drop_redundant_divs(bmap
);
5107 struct isl_basic_set
*isl_basic_set_drop_redundant_divs(
5108 struct isl_basic_set
*bset
)
5110 isl_basic_map
*bmap
= bset_to_bmap(bset
);
5111 return bset_from_bmap(isl_basic_map_drop_redundant_divs(bmap
));
5114 struct isl_map
*isl_map_drop_redundant_divs(struct isl_map
*map
)
5120 for (i
= 0; i
< map
->n
; ++i
) {
5121 map
->p
[i
] = isl_basic_map_drop_redundant_divs(map
->p
[i
]);
5125 ISL_F_CLR(map
, ISL_MAP_NORMALIZED
);
5132 struct isl_set
*isl_set_drop_redundant_divs(struct isl_set
*set
)
5134 return set_from_map(isl_map_drop_redundant_divs(set_to_map(set
)));
5137 /* Does "bmap" satisfy any equality that involves more than 2 variables
5138 * and/or has coefficients different from -1 and 1?
5140 static int has_multiple_var_equality(__isl_keep isl_basic_map
*bmap
)
5145 total
= isl_basic_map_dim(bmap
, isl_dim_all
);
5147 for (i
= 0; i
< bmap
->n_eq
; ++i
) {
5150 j
= isl_seq_first_non_zero(bmap
->eq
[i
] + 1, total
);
5153 if (!isl_int_is_one(bmap
->eq
[i
][1 + j
]) &&
5154 !isl_int_is_negone(bmap
->eq
[i
][1 + j
]))
5158 k
= isl_seq_first_non_zero(bmap
->eq
[i
] + 1 + j
, total
- j
);
5162 if (!isl_int_is_one(bmap
->eq
[i
][1 + j
]) &&
5163 !isl_int_is_negone(bmap
->eq
[i
][1 + j
]))
5167 k
= isl_seq_first_non_zero(bmap
->eq
[i
] + 1 + j
, total
- j
);
5175 /* Remove any common factor g from the constraint coefficients in "v".
5176 * The constant term is stored in the first position and is replaced
5177 * by floor(c/g). If any common factor is removed and if this results
5178 * in a tightening of the constraint, then set *tightened.
5180 static __isl_give isl_vec
*normalize_constraint(__isl_take isl_vec
*v
,
5187 ctx
= isl_vec_get_ctx(v
);
5188 isl_seq_gcd(v
->el
+ 1, v
->size
- 1, &ctx
->normalize_gcd
);
5189 if (isl_int_is_zero(ctx
->normalize_gcd
))
5191 if (isl_int_is_one(ctx
->normalize_gcd
))
5196 if (tightened
&& !isl_int_is_divisible_by(v
->el
[0], ctx
->normalize_gcd
))
5198 isl_int_fdiv_q(v
->el
[0], v
->el
[0], ctx
->normalize_gcd
);
5199 isl_seq_scale_down(v
->el
+ 1, v
->el
+ 1, ctx
->normalize_gcd
,
5204 /* If "bmap" is an integer set that satisfies any equality involving
5205 * more than 2 variables and/or has coefficients different from -1 and 1,
5206 * then use variable compression to reduce the coefficients by removing
5207 * any (hidden) common factor.
5208 * In particular, apply the variable compression to each constraint,
5209 * factor out any common factor in the non-constant coefficients and
5210 * then apply the inverse of the compression.
5211 * At the end, we mark the basic map as having reduced constants.
5212 * If this flag is still set on the next invocation of this function,
5213 * then we skip the computation.
5215 * Removing a common factor may result in a tightening of some of
5216 * the constraints. If this happens, then we may end up with two
5217 * opposite inequalities that can be replaced by an equality.
5218 * We therefore call isl_basic_map_detect_inequality_pairs,
5219 * which checks for such pairs of inequalities as well as eliminate_divs_eq
5220 * and isl_basic_map_gauss if such a pair was found.
5222 __isl_give isl_basic_map
*isl_basic_map_reduce_coefficients(
5223 __isl_take isl_basic_map
*bmap
)
5228 isl_mat
*eq
, *T
, *T2
;
5234 if (ISL_F_ISSET(bmap
, ISL_BASIC_MAP_REDUCED_COEFFICIENTS
))
5236 if (isl_basic_map_is_rational(bmap
))
5238 if (bmap
->n_eq
== 0)
5240 if (!has_multiple_var_equality(bmap
))
5243 total
= isl_basic_map_dim(bmap
, isl_dim_all
);
5244 ctx
= isl_basic_map_get_ctx(bmap
);
5245 v
= isl_vec_alloc(ctx
, 1 + total
);
5247 return isl_basic_map_free(bmap
);
5249 eq
= isl_mat_sub_alloc6(ctx
, bmap
->eq
, 0, bmap
->n_eq
, 0, 1 + total
);
5250 T
= isl_mat_variable_compression(eq
, &T2
);
5253 if (T
->n_col
== 0) {
5257 return isl_basic_map_set_to_empty(bmap
);
5261 for (i
= 0; i
< bmap
->n_ineq
; ++i
) {
5262 isl_seq_cpy(v
->el
, bmap
->ineq
[i
], 1 + total
);
5263 v
= isl_vec_mat_product(v
, isl_mat_copy(T
));
5264 v
= normalize_constraint(v
, &tightened
);
5265 v
= isl_vec_mat_product(v
, isl_mat_copy(T2
));
5268 isl_seq_cpy(bmap
->ineq
[i
], v
->el
, 1 + total
);
5275 ISL_F_SET(bmap
, ISL_BASIC_MAP_REDUCED_COEFFICIENTS
);
5280 bmap
= isl_basic_map_detect_inequality_pairs(bmap
, &progress
);
5282 bmap
= eliminate_divs_eq(bmap
, &progress
);
5283 bmap
= isl_basic_map_gauss(bmap
, NULL
);
5292 return isl_basic_map_free(bmap
);
5295 /* Shift the integer division at position "div" of "bmap"
5296 * by "shift" times the variable at position "pos".
5297 * "pos" is as determined by isl_basic_map_offset, i.e., pos == 0
5298 * corresponds to the constant term.
5300 * That is, if the integer division has the form
5304 * then replace it by
5306 * floor((f(x) + shift * d * x_pos)/d) - shift * x_pos
5308 __isl_give isl_basic_map
*isl_basic_map_shift_div(
5309 __isl_take isl_basic_map
*bmap
, int div
, int pos
, isl_int shift
)
5317 total
= isl_basic_map_dim(bmap
, isl_dim_all
);
5318 total
-= isl_basic_map_dim(bmap
, isl_dim_div
);
5320 isl_int_addmul(bmap
->div
[div
][1 + pos
], shift
, bmap
->div
[div
][0]);
5322 for (i
= 0; i
< bmap
->n_eq
; ++i
) {
5323 if (isl_int_is_zero(bmap
->eq
[i
][1 + total
+ div
]))
5325 isl_int_submul(bmap
->eq
[i
][pos
],
5326 shift
, bmap
->eq
[i
][1 + total
+ div
]);
5328 for (i
= 0; i
< bmap
->n_ineq
; ++i
) {
5329 if (isl_int_is_zero(bmap
->ineq
[i
][1 + total
+ div
]))
5331 isl_int_submul(bmap
->ineq
[i
][pos
],
5332 shift
, bmap
->ineq
[i
][1 + total
+ div
]);
5334 for (i
= 0; i
< bmap
->n_div
; ++i
) {
5335 if (isl_int_is_zero(bmap
->div
[i
][0]))
5337 if (isl_int_is_zero(bmap
->div
[i
][1 + 1 + total
+ div
]))
5339 isl_int_submul(bmap
->div
[i
][1 + pos
],
5340 shift
, bmap
->div
[i
][1 + 1 + total
+ div
]);