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_get_tuple_name(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 isl_basic_map_free_div(bmap
, n
);
205 bmap
->dim
= isl_space_drop_dims(bmap
->dim
, type
, first
, n
);
209 ISL_F_CLR(bmap
, ISL_BASIC_MAP_NORMALIZED
);
210 bmap
= isl_basic_map_simplify(bmap
);
211 return isl_basic_map_finalize(bmap
);
213 isl_basic_map_free(bmap
);
217 __isl_give isl_basic_set
*isl_basic_set_drop(__isl_take isl_basic_set
*bset
,
218 enum isl_dim_type type
, unsigned first
, unsigned n
)
220 return bset_from_bmap(isl_basic_map_drop(bset_to_bmap(bset
),
224 struct isl_basic_map
*isl_basic_map_drop_inputs(
225 struct isl_basic_map
*bmap
, unsigned first
, unsigned n
)
227 return isl_basic_map_drop(bmap
, isl_dim_in
, first
, n
);
230 struct isl_map
*isl_map_drop(struct isl_map
*map
,
231 enum isl_dim_type type
, unsigned first
, unsigned n
)
238 isl_assert(map
->ctx
, first
+ n
<= isl_map_dim(map
, type
), goto error
);
240 if (n
== 0 && !isl_space_get_tuple_name(map
->dim
, type
))
242 map
= isl_map_cow(map
);
245 map
->dim
= isl_space_drop_dims(map
->dim
, type
, first
, n
);
249 for (i
= 0; i
< map
->n
; ++i
) {
250 map
->p
[i
] = isl_basic_map_drop(map
->p
[i
], type
, first
, n
);
254 ISL_F_CLR(map
, ISL_MAP_NORMALIZED
);
262 struct isl_set
*isl_set_drop(struct isl_set
*set
,
263 enum isl_dim_type type
, unsigned first
, unsigned n
)
265 return set_from_map(isl_map_drop(set_to_map(set
), type
, first
, n
));
268 struct isl_map
*isl_map_drop_inputs(
269 struct isl_map
*map
, unsigned first
, unsigned n
)
271 return isl_map_drop(map
, isl_dim_in
, first
, n
);
275 * We don't cow, as the div is assumed to be redundant.
277 __isl_give isl_basic_map
*isl_basic_map_drop_div(
278 __isl_take isl_basic_map
*bmap
, unsigned div
)
286 pos
= 1 + isl_space_dim(bmap
->dim
, isl_dim_all
) + div
;
288 isl_assert(bmap
->ctx
, div
< bmap
->n_div
, goto error
);
290 for (i
= 0; i
< bmap
->n_eq
; ++i
)
291 constraint_drop_vars(bmap
->eq
[i
]+pos
, 1, bmap
->extra
-div
-1);
293 for (i
= 0; i
< bmap
->n_ineq
; ++i
) {
294 if (!isl_int_is_zero(bmap
->ineq
[i
][pos
])) {
295 isl_basic_map_drop_inequality(bmap
, i
);
299 constraint_drop_vars(bmap
->ineq
[i
]+pos
, 1, bmap
->extra
-div
-1);
302 for (i
= 0; i
< bmap
->n_div
; ++i
)
303 constraint_drop_vars(bmap
->div
[i
]+1+pos
, 1, bmap
->extra
-div
-1);
305 if (div
!= bmap
->n_div
- 1) {
307 isl_int
*t
= bmap
->div
[div
];
309 for (j
= div
; j
< bmap
->n_div
- 1; ++j
)
310 bmap
->div
[j
] = bmap
->div
[j
+1];
312 bmap
->div
[bmap
->n_div
- 1] = t
;
314 ISL_F_CLR(bmap
, ISL_BASIC_MAP_NORMALIZED
);
315 isl_basic_map_free_div(bmap
, 1);
319 isl_basic_map_free(bmap
);
323 struct isl_basic_map
*isl_basic_map_normalize_constraints(
324 struct isl_basic_map
*bmap
)
328 unsigned total
= isl_basic_map_total_dim(bmap
);
334 for (i
= bmap
->n_eq
- 1; i
>= 0; --i
) {
335 isl_seq_gcd(bmap
->eq
[i
]+1, total
, &gcd
);
336 if (isl_int_is_zero(gcd
)) {
337 if (!isl_int_is_zero(bmap
->eq
[i
][0])) {
338 bmap
= isl_basic_map_set_to_empty(bmap
);
341 isl_basic_map_drop_equality(bmap
, i
);
344 if (ISL_F_ISSET(bmap
, ISL_BASIC_MAP_RATIONAL
))
345 isl_int_gcd(gcd
, gcd
, bmap
->eq
[i
][0]);
346 if (isl_int_is_one(gcd
))
348 if (!isl_int_is_divisible_by(bmap
->eq
[i
][0], gcd
)) {
349 bmap
= isl_basic_map_set_to_empty(bmap
);
352 isl_seq_scale_down(bmap
->eq
[i
], bmap
->eq
[i
], gcd
, 1+total
);
355 for (i
= bmap
->n_ineq
- 1; i
>= 0; --i
) {
356 isl_seq_gcd(bmap
->ineq
[i
]+1, total
, &gcd
);
357 if (isl_int_is_zero(gcd
)) {
358 if (isl_int_is_neg(bmap
->ineq
[i
][0])) {
359 bmap
= isl_basic_map_set_to_empty(bmap
);
362 isl_basic_map_drop_inequality(bmap
, i
);
365 if (ISL_F_ISSET(bmap
, ISL_BASIC_MAP_RATIONAL
))
366 isl_int_gcd(gcd
, gcd
, bmap
->ineq
[i
][0]);
367 if (isl_int_is_one(gcd
))
369 isl_int_fdiv_q(bmap
->ineq
[i
][0], bmap
->ineq
[i
][0], gcd
);
370 isl_seq_scale_down(bmap
->ineq
[i
]+1, bmap
->ineq
[i
]+1, gcd
, total
);
377 struct isl_basic_set
*isl_basic_set_normalize_constraints(
378 struct isl_basic_set
*bset
)
380 isl_basic_map
*bmap
= bset_to_bmap(bset
);
381 return bset_from_bmap(isl_basic_map_normalize_constraints(bmap
));
384 /* Assuming the variable at position "pos" has an integer coefficient
385 * in integer division "div", extract it from this integer division.
386 * "pos" is as determined by isl_basic_map_offset, i.e., pos == 0
387 * corresponds to the constant term.
389 * That is, the integer division is of the form
391 * floor((... + c * d * x_pos + ...)/d)
395 * floor((... + 0 * x_pos + ...)/d) + c * x_pos
397 static __isl_give isl_basic_map
*remove_var_from_div(
398 __isl_take isl_basic_map
*bmap
, int div
, int pos
)
403 isl_int_divexact(shift
, bmap
->div
[div
][1 + pos
], bmap
->div
[div
][0]);
404 isl_int_neg(shift
, shift
);
405 bmap
= isl_basic_map_shift_div(bmap
, div
, pos
, shift
);
406 isl_int_clear(shift
);
411 /* Check if integer division "div" has any integral coefficient
412 * (or constant term). If so, extract them from the integer division.
414 static __isl_give isl_basic_map
*remove_independent_vars_from_div(
415 __isl_take isl_basic_map
*bmap
, int div
)
418 unsigned total
= 1 + isl_basic_map_total_dim(bmap
);
420 for (i
= 0; i
< total
; ++i
) {
421 if (isl_int_is_zero(bmap
->div
[div
][1 + i
]))
423 if (!isl_int_is_divisible_by(bmap
->div
[div
][1 + i
],
426 bmap
= remove_var_from_div(bmap
, div
, i
);
434 /* Check if any known integer division has any integral coefficient
435 * (or constant term). If so, extract them from the integer division.
437 static __isl_give isl_basic_map
*remove_independent_vars_from_divs(
438 __isl_take isl_basic_map
*bmap
)
444 if (bmap
->n_div
== 0)
447 for (i
= 0; i
< bmap
->n_div
; ++i
) {
448 if (isl_int_is_zero(bmap
->div
[i
][0]))
450 bmap
= remove_independent_vars_from_div(bmap
, i
);
458 /* Remove any common factor in numerator and denominator of the div expression,
459 * not taking into account the constant term.
460 * That is, if the div is of the form
462 * floor((a + m f(x))/(m d))
466 * floor((floor(a/m) + f(x))/d)
468 * The difference {a/m}/d in the argument satisfies 0 <= {a/m}/d < 1/d
469 * and can therefore not influence the result of the floor.
471 static void normalize_div_expression(__isl_keep isl_basic_map
*bmap
, int div
)
473 unsigned total
= isl_basic_map_total_dim(bmap
);
474 isl_ctx
*ctx
= bmap
->ctx
;
476 if (isl_int_is_zero(bmap
->div
[div
][0]))
478 isl_seq_gcd(bmap
->div
[div
] + 2, total
, &ctx
->normalize_gcd
);
479 isl_int_gcd(ctx
->normalize_gcd
, ctx
->normalize_gcd
, bmap
->div
[div
][0]);
480 if (isl_int_is_one(ctx
->normalize_gcd
))
482 isl_int_fdiv_q(bmap
->div
[div
][1], bmap
->div
[div
][1],
484 isl_int_divexact(bmap
->div
[div
][0], bmap
->div
[div
][0],
486 isl_seq_scale_down(bmap
->div
[div
] + 2, bmap
->div
[div
] + 2,
487 ctx
->normalize_gcd
, total
);
490 /* Remove any common factor in numerator and denominator of a div expression,
491 * not taking into account the constant term.
492 * That is, look for any div of the form
494 * floor((a + m f(x))/(m d))
498 * floor((floor(a/m) + f(x))/d)
500 * The difference {a/m}/d in the argument satisfies 0 <= {a/m}/d < 1/d
501 * and can therefore not influence the result of the floor.
503 static __isl_give isl_basic_map
*normalize_div_expressions(
504 __isl_take isl_basic_map
*bmap
)
510 if (bmap
->n_div
== 0)
513 for (i
= 0; i
< bmap
->n_div
; ++i
)
514 normalize_div_expression(bmap
, i
);
519 /* Assumes divs have been ordered if keep_divs is set.
521 static void eliminate_var_using_equality(struct isl_basic_map
*bmap
,
522 unsigned pos
, isl_int
*eq
, int keep_divs
, int *progress
)
525 unsigned space_total
;
529 total
= isl_basic_map_total_dim(bmap
);
530 space_total
= isl_space_dim(bmap
->dim
, isl_dim_all
);
531 last_div
= isl_seq_last_non_zero(eq
+ 1 + space_total
, bmap
->n_div
);
532 for (k
= 0; k
< bmap
->n_eq
; ++k
) {
533 if (bmap
->eq
[k
] == eq
)
535 if (isl_int_is_zero(bmap
->eq
[k
][1+pos
]))
539 isl_seq_elim(bmap
->eq
[k
], eq
, 1+pos
, 1+total
, NULL
);
540 isl_seq_normalize(bmap
->ctx
, bmap
->eq
[k
], 1 + total
);
543 for (k
= 0; k
< bmap
->n_ineq
; ++k
) {
544 if (isl_int_is_zero(bmap
->ineq
[k
][1+pos
]))
548 isl_seq_elim(bmap
->ineq
[k
], eq
, 1+pos
, 1+total
, NULL
);
549 isl_seq_normalize(bmap
->ctx
, bmap
->ineq
[k
], 1 + total
);
550 ISL_F_CLR(bmap
, ISL_BASIC_MAP_NORMALIZED
);
553 for (k
= 0; k
< bmap
->n_div
; ++k
) {
554 if (isl_int_is_zero(bmap
->div
[k
][0]))
556 if (isl_int_is_zero(bmap
->div
[k
][1+1+pos
]))
560 /* We need to be careful about circular definitions,
561 * so for now we just remove the definition of div k
562 * if the equality contains any divs.
563 * If keep_divs is set, then the divs have been ordered
564 * and we can keep the definition as long as the result
567 if (last_div
== -1 || (keep_divs
&& last_div
< k
)) {
568 isl_seq_elim(bmap
->div
[k
]+1, eq
,
569 1+pos
, 1+total
, &bmap
->div
[k
][0]);
570 normalize_div_expression(bmap
, k
);
572 isl_seq_clr(bmap
->div
[k
], 1 + total
);
573 ISL_F_CLR(bmap
, ISL_BASIC_MAP_NORMALIZED
);
577 /* Assumes divs have been ordered if keep_divs is set.
579 static __isl_give isl_basic_map
*eliminate_div(__isl_take isl_basic_map
*bmap
,
580 isl_int
*eq
, unsigned div
, int keep_divs
)
582 unsigned pos
= isl_space_dim(bmap
->dim
, isl_dim_all
) + div
;
584 eliminate_var_using_equality(bmap
, pos
, eq
, keep_divs
, NULL
);
586 bmap
= isl_basic_map_drop_div(bmap
, div
);
591 /* Check if elimination of div "div" using equality "eq" would not
592 * result in a div depending on a later div.
594 static int ok_to_eliminate_div(struct isl_basic_map
*bmap
, isl_int
*eq
,
599 unsigned space_total
= isl_space_dim(bmap
->dim
, isl_dim_all
);
600 unsigned pos
= space_total
+ div
;
602 last_div
= isl_seq_last_non_zero(eq
+ 1 + space_total
, bmap
->n_div
);
603 if (last_div
< 0 || last_div
<= div
)
606 for (k
= 0; k
<= last_div
; ++k
) {
607 if (isl_int_is_zero(bmap
->div
[k
][0]))
609 if (!isl_int_is_zero(bmap
->div
[k
][1 + 1 + pos
]))
616 /* Elimininate divs based on equalities
618 static struct isl_basic_map
*eliminate_divs_eq(
619 struct isl_basic_map
*bmap
, int *progress
)
626 bmap
= isl_basic_map_order_divs(bmap
);
631 off
= 1 + isl_space_dim(bmap
->dim
, isl_dim_all
);
633 for (d
= bmap
->n_div
- 1; d
>= 0 ; --d
) {
634 for (i
= 0; i
< bmap
->n_eq
; ++i
) {
635 if (!isl_int_is_one(bmap
->eq
[i
][off
+ d
]) &&
636 !isl_int_is_negone(bmap
->eq
[i
][off
+ d
]))
638 if (!ok_to_eliminate_div(bmap
, bmap
->eq
[i
], d
))
642 bmap
= eliminate_div(bmap
, bmap
->eq
[i
], d
, 1);
643 if (isl_basic_map_drop_equality(bmap
, i
) < 0)
644 return isl_basic_map_free(bmap
);
649 return eliminate_divs_eq(bmap
, progress
);
653 /* Elimininate divs based on inequalities
655 static struct isl_basic_map
*eliminate_divs_ineq(
656 struct isl_basic_map
*bmap
, int *progress
)
667 off
= 1 + isl_space_dim(bmap
->dim
, isl_dim_all
);
669 for (d
= bmap
->n_div
- 1; d
>= 0 ; --d
) {
670 for (i
= 0; i
< bmap
->n_eq
; ++i
)
671 if (!isl_int_is_zero(bmap
->eq
[i
][off
+ d
]))
675 for (i
= 0; i
< bmap
->n_ineq
; ++i
)
676 if (isl_int_abs_gt(bmap
->ineq
[i
][off
+ d
], ctx
->one
))
678 if (i
< bmap
->n_ineq
)
681 bmap
= isl_basic_map_eliminate_vars(bmap
, (off
-1)+d
, 1);
682 if (!bmap
|| ISL_F_ISSET(bmap
, ISL_BASIC_MAP_EMPTY
))
684 bmap
= isl_basic_map_drop_div(bmap
, d
);
691 struct isl_basic_map
*isl_basic_map_gauss(
692 struct isl_basic_map
*bmap
, int *progress
)
700 bmap
= isl_basic_map_order_divs(bmap
);
705 total
= isl_basic_map_total_dim(bmap
);
706 total_var
= total
- bmap
->n_div
;
708 last_var
= total
- 1;
709 for (done
= 0; done
< bmap
->n_eq
; ++done
) {
710 for (; last_var
>= 0; --last_var
) {
711 for (k
= done
; k
< bmap
->n_eq
; ++k
)
712 if (!isl_int_is_zero(bmap
->eq
[k
][1+last_var
]))
720 swap_equality(bmap
, k
, done
);
721 if (isl_int_is_neg(bmap
->eq
[done
][1+last_var
]))
722 isl_seq_neg(bmap
->eq
[done
], bmap
->eq
[done
], 1+total
);
724 eliminate_var_using_equality(bmap
, last_var
, bmap
->eq
[done
], 1,
727 if (last_var
>= total_var
&&
728 isl_int_is_zero(bmap
->div
[last_var
- total_var
][0])) {
729 unsigned div
= last_var
- total_var
;
730 isl_seq_neg(bmap
->div
[div
]+1, bmap
->eq
[done
], 1+total
);
731 isl_int_set_si(bmap
->div
[div
][1+1+last_var
], 0);
732 isl_int_set(bmap
->div
[div
][0],
733 bmap
->eq
[done
][1+last_var
]);
736 ISL_F_CLR(bmap
, ISL_BASIC_MAP_NORMALIZED
);
739 if (done
== bmap
->n_eq
)
741 for (k
= done
; k
< bmap
->n_eq
; ++k
) {
742 if (isl_int_is_zero(bmap
->eq
[k
][0]))
744 return isl_basic_map_set_to_empty(bmap
);
746 isl_basic_map_free_equality(bmap
, bmap
->n_eq
-done
);
750 struct isl_basic_set
*isl_basic_set_gauss(
751 struct isl_basic_set
*bset
, int *progress
)
753 return bset_from_bmap(isl_basic_map_gauss(bset_to_bmap(bset
),
758 static unsigned int round_up(unsigned int v
)
769 /* Hash table of inequalities in a basic map.
770 * "index" is an array of addresses of inequalities in the basic map, some
771 * of which are NULL. The inequalities are hashed on the coefficients
772 * except the constant term.
773 * "size" is the number of elements in the array and is always a power of two
774 * "bits" is the number of bits need to represent an index into the array.
775 * "total" is the total dimension of the basic map.
777 struct isl_constraint_index
{
784 /* Fill in the "ci" data structure for holding the inequalities of "bmap".
786 static isl_stat
create_constraint_index(struct isl_constraint_index
*ci
,
787 __isl_keep isl_basic_map
*bmap
)
793 return isl_stat_error
;
794 ci
->total
= isl_basic_set_total_dim(bmap
);
795 if (bmap
->n_ineq
== 0)
797 ci
->size
= round_up(4 * (bmap
->n_ineq
+ 1) / 3 - 1);
798 ci
->bits
= ffs(ci
->size
) - 1;
799 ctx
= isl_basic_map_get_ctx(bmap
);
800 ci
->index
= isl_calloc_array(ctx
, isl_int
**, ci
->size
);
802 return isl_stat_error
;
807 /* Free the memory allocated by create_constraint_index.
809 static void constraint_index_free(struct isl_constraint_index
*ci
)
814 /* Return the position in ci->index that contains the address of
815 * an inequality that is equal to *ineq up to the constant term,
816 * provided this address is not identical to "ineq".
817 * If there is no such inequality, then return the position where
818 * such an inequality should be inserted.
820 static int hash_index_ineq(struct isl_constraint_index
*ci
, isl_int
**ineq
)
823 uint32_t hash
= isl_seq_get_hash_bits((*ineq
) + 1, ci
->total
, ci
->bits
);
824 for (h
= hash
; ci
->index
[h
]; h
= (h
+1) % ci
->size
)
825 if (ineq
!= ci
->index
[h
] &&
826 isl_seq_eq((*ineq
) + 1, ci
->index
[h
][0]+1, ci
->total
))
831 /* Return the position in ci->index that contains the address of
832 * an inequality that is equal to the k'th inequality of "bmap"
833 * up to the constant term, provided it does not point to the very
835 * If there is no such inequality, then return the position where
836 * such an inequality should be inserted.
838 static int hash_index(struct isl_constraint_index
*ci
,
839 __isl_keep isl_basic_map
*bmap
, int k
)
841 return hash_index_ineq(ci
, &bmap
->ineq
[k
]);
844 static int set_hash_index(struct isl_constraint_index
*ci
,
845 struct isl_basic_set
*bset
, int k
)
847 return hash_index(ci
, bset
, k
);
850 /* Fill in the "ci" data structure with the inequalities of "bset".
852 static isl_stat
setup_constraint_index(struct isl_constraint_index
*ci
,
853 __isl_keep isl_basic_set
*bset
)
857 if (create_constraint_index(ci
, bset
) < 0)
858 return isl_stat_error
;
860 for (k
= 0; k
< bset
->n_ineq
; ++k
) {
861 h
= set_hash_index(ci
, bset
, k
);
862 ci
->index
[h
] = &bset
->ineq
[k
];
868 /* Is the inequality ineq (obviously) redundant with respect
869 * to the constraints in "ci"?
871 * Look for an inequality in "ci" with the same coefficients and then
872 * check if the contant term of "ineq" is greater than or equal
873 * to the constant term of that inequality. If so, "ineq" is clearly
876 * Note that hash_index_ineq ignores a stored constraint if it has
877 * the same address as the passed inequality. It is ok to pass
878 * the address of a local variable here since it will never be
879 * the same as the address of a constraint in "ci".
881 static isl_bool
constraint_index_is_redundant(struct isl_constraint_index
*ci
,
886 h
= hash_index_ineq(ci
, &ineq
);
888 return isl_bool_false
;
889 return isl_int_ge(ineq
[0], (*ci
->index
[h
])[0]);
892 /* If we can eliminate more than one div, then we need to make
893 * sure we do it from last div to first div, in order not to
894 * change the position of the other divs that still need to
897 static struct isl_basic_map
*remove_duplicate_divs(
898 struct isl_basic_map
*bmap
, int *progress
)
910 bmap
= isl_basic_map_order_divs(bmap
);
911 if (!bmap
|| bmap
->n_div
<= 1)
914 total_var
= isl_space_dim(bmap
->dim
, isl_dim_all
);
915 total
= total_var
+ bmap
->n_div
;
918 for (k
= bmap
->n_div
- 1; k
>= 0; --k
)
919 if (!isl_int_is_zero(bmap
->div
[k
][0]))
924 size
= round_up(4 * bmap
->n_div
/ 3 - 1);
927 elim_for
= isl_calloc_array(ctx
, int, bmap
->n_div
);
928 bits
= ffs(size
) - 1;
929 index
= isl_calloc_array(ctx
, int, size
);
930 if (!elim_for
|| !index
)
932 eq
= isl_blk_alloc(ctx
, 1+total
);
933 if (isl_blk_is_error(eq
))
936 isl_seq_clr(eq
.data
, 1+total
);
937 index
[isl_seq_get_hash_bits(bmap
->div
[k
], 2+total
, bits
)] = k
+ 1;
938 for (--k
; k
>= 0; --k
) {
941 if (isl_int_is_zero(bmap
->div
[k
][0]))
944 hash
= isl_seq_get_hash_bits(bmap
->div
[k
], 2+total
, bits
);
945 for (h
= hash
; index
[h
]; h
= (h
+1) % size
)
946 if (isl_seq_eq(bmap
->div
[k
],
947 bmap
->div
[index
[h
]-1], 2+total
))
956 for (l
= bmap
->n_div
- 1; l
>= 0; --l
) {
960 isl_int_set_si(eq
.data
[1+total_var
+k
], -1);
961 isl_int_set_si(eq
.data
[1+total_var
+l
], 1);
962 bmap
= eliminate_div(bmap
, eq
.data
, l
, 1);
965 isl_int_set_si(eq
.data
[1+total_var
+k
], 0);
966 isl_int_set_si(eq
.data
[1+total_var
+l
], 0);
969 isl_blk_free(ctx
, eq
);
976 static int n_pure_div_eq(struct isl_basic_map
*bmap
)
981 total
= isl_space_dim(bmap
->dim
, isl_dim_all
);
982 for (i
= 0, j
= bmap
->n_div
-1; i
< bmap
->n_eq
; ++i
) {
983 while (j
>= 0 && isl_int_is_zero(bmap
->eq
[i
][1 + total
+ j
]))
987 if (isl_seq_first_non_zero(bmap
->eq
[i
] + 1 + total
, j
) != -1)
993 /* Normalize divs that appear in equalities.
995 * In particular, we assume that bmap contains some equalities
1000 * and we want to replace the set of e_i by a minimal set and
1001 * such that the new e_i have a canonical representation in terms
1003 * If any of the equalities involves more than one divs, then
1004 * we currently simply bail out.
1006 * Let us first additionally assume that all equalities involve
1007 * a div. The equalities then express modulo constraints on the
1008 * remaining variables and we can use "parameter compression"
1009 * to find a minimal set of constraints. The result is a transformation
1011 * x = T(x') = x_0 + G x'
1013 * with G a lower-triangular matrix with all elements below the diagonal
1014 * non-negative and smaller than the diagonal element on the same row.
1015 * We first normalize x_0 by making the same property hold in the affine
1017 * The rows i of G with a 1 on the diagonal do not impose any modulo
1018 * constraint and simply express x_i = x'_i.
1019 * For each of the remaining rows i, we introduce a div and a corresponding
1020 * equality. In particular
1022 * g_ii e_j = x_i - g_i(x')
1024 * where each x'_k is replaced either by x_k (if g_kk = 1) or the
1025 * corresponding div (if g_kk != 1).
1027 * If there are any equalities not involving any div, then we
1028 * first apply a variable compression on the variables x:
1030 * x = C x'' x'' = C_2 x
1032 * and perform the above parameter compression on A C instead of on A.
1033 * The resulting compression is then of the form
1035 * x'' = T(x') = x_0 + G x'
1037 * and in constructing the new divs and the corresponding equalities,
1038 * we have to replace each x'', i.e., the x'_k with (g_kk = 1),
1039 * by the corresponding row from C_2.
1041 static struct isl_basic_map
*normalize_divs(
1042 struct isl_basic_map
*bmap
, int *progress
)
1049 struct isl_mat
*T
= NULL
;
1050 struct isl_mat
*C
= NULL
;
1051 struct isl_mat
*C2
= NULL
;
1054 int dropped
, needed
;
1059 if (bmap
->n_div
== 0)
1062 if (bmap
->n_eq
== 0)
1065 if (ISL_F_ISSET(bmap
, ISL_BASIC_MAP_NORMALIZED_DIVS
))
1068 total
= isl_space_dim(bmap
->dim
, isl_dim_all
);
1069 div_eq
= n_pure_div_eq(bmap
);
1073 if (div_eq
< bmap
->n_eq
) {
1074 B
= isl_mat_sub_alloc6(bmap
->ctx
, bmap
->eq
, div_eq
,
1075 bmap
->n_eq
- div_eq
, 0, 1 + total
);
1076 C
= isl_mat_variable_compression(B
, &C2
);
1079 if (C
->n_col
== 0) {
1080 bmap
= isl_basic_map_set_to_empty(bmap
);
1087 d
= isl_vec_alloc(bmap
->ctx
, div_eq
);
1090 for (i
= 0, j
= bmap
->n_div
-1; i
< div_eq
; ++i
) {
1091 while (j
>= 0 && isl_int_is_zero(bmap
->eq
[i
][1 + total
+ j
]))
1093 isl_int_set(d
->block
.data
[i
], bmap
->eq
[i
][1 + total
+ j
]);
1095 B
= isl_mat_sub_alloc6(bmap
->ctx
, bmap
->eq
, 0, div_eq
, 0, 1 + total
);
1098 B
= isl_mat_product(B
, C
);
1102 T
= isl_mat_parameter_compression(B
, d
);
1105 if (T
->n_col
== 0) {
1106 bmap
= isl_basic_map_set_to_empty(bmap
);
1112 for (i
= 0; i
< T
->n_row
- 1; ++i
) {
1113 isl_int_fdiv_q(v
, T
->row
[1 + i
][0], T
->row
[1 + i
][1 + i
]);
1114 if (isl_int_is_zero(v
))
1116 isl_mat_col_submul(T
, 0, v
, 1 + i
);
1119 pos
= isl_alloc_array(bmap
->ctx
, int, T
->n_row
);
1122 /* We have to be careful because dropping equalities may reorder them */
1124 for (j
= bmap
->n_div
- 1; j
>= 0; --j
) {
1125 for (i
= 0; i
< bmap
->n_eq
; ++i
)
1126 if (!isl_int_is_zero(bmap
->eq
[i
][1 + total
+ j
]))
1128 if (i
< bmap
->n_eq
) {
1129 bmap
= isl_basic_map_drop_div(bmap
, j
);
1130 isl_basic_map_drop_equality(bmap
, i
);
1136 for (i
= 1; i
< T
->n_row
; ++i
) {
1137 if (isl_int_is_one(T
->row
[i
][i
]))
1142 if (needed
> dropped
) {
1143 bmap
= isl_basic_map_extend_space(bmap
, isl_space_copy(bmap
->dim
),
1148 for (i
= 1; i
< T
->n_row
; ++i
) {
1149 if (isl_int_is_one(T
->row
[i
][i
]))
1151 k
= isl_basic_map_alloc_div(bmap
);
1152 pos
[i
] = 1 + total
+ k
;
1153 isl_seq_clr(bmap
->div
[k
] + 1, 1 + total
+ bmap
->n_div
);
1154 isl_int_set(bmap
->div
[k
][0], T
->row
[i
][i
]);
1156 isl_seq_cpy(bmap
->div
[k
] + 1, C2
->row
[i
], 1 + total
);
1158 isl_int_set_si(bmap
->div
[k
][1 + i
], 1);
1159 for (j
= 0; j
< i
; ++j
) {
1160 if (isl_int_is_zero(T
->row
[i
][j
]))
1162 if (pos
[j
] < T
->n_row
&& C2
)
1163 isl_seq_submul(bmap
->div
[k
] + 1, T
->row
[i
][j
],
1164 C2
->row
[pos
[j
]], 1 + total
);
1166 isl_int_neg(bmap
->div
[k
][1 + pos
[j
]],
1169 j
= isl_basic_map_alloc_equality(bmap
);
1170 isl_seq_neg(bmap
->eq
[j
], bmap
->div
[k
]+1, 1+total
+bmap
->n_div
);
1171 isl_int_set(bmap
->eq
[j
][pos
[i
]], bmap
->div
[k
][0]);
1180 ISL_F_SET(bmap
, ISL_BASIC_MAP_NORMALIZED_DIVS
);
1190 static struct isl_basic_map
*set_div_from_lower_bound(
1191 struct isl_basic_map
*bmap
, int div
, int ineq
)
1193 unsigned total
= 1 + isl_space_dim(bmap
->dim
, isl_dim_all
);
1195 isl_seq_neg(bmap
->div
[div
] + 1, bmap
->ineq
[ineq
], total
+ bmap
->n_div
);
1196 isl_int_set(bmap
->div
[div
][0], bmap
->ineq
[ineq
][total
+ div
]);
1197 isl_int_add(bmap
->div
[div
][1], bmap
->div
[div
][1], bmap
->div
[div
][0]);
1198 isl_int_sub_ui(bmap
->div
[div
][1], bmap
->div
[div
][1], 1);
1199 isl_int_set_si(bmap
->div
[div
][1 + total
+ div
], 0);
1204 /* Check whether it is ok to define a div based on an inequality.
1205 * To avoid the introduction of circular definitions of divs, we
1206 * do not allow such a definition if the resulting expression would refer to
1207 * any other undefined divs or if any known div is defined in
1208 * terms of the unknown div.
1210 static int ok_to_set_div_from_bound(struct isl_basic_map
*bmap
,
1214 unsigned total
= 1 + isl_space_dim(bmap
->dim
, isl_dim_all
);
1216 /* Not defined in terms of unknown divs */
1217 for (j
= 0; j
< bmap
->n_div
; ++j
) {
1220 if (isl_int_is_zero(bmap
->ineq
[ineq
][total
+ j
]))
1222 if (isl_int_is_zero(bmap
->div
[j
][0]))
1226 /* No other div defined in terms of this one => avoid loops */
1227 for (j
= 0; j
< bmap
->n_div
; ++j
) {
1230 if (isl_int_is_zero(bmap
->div
[j
][0]))
1232 if (!isl_int_is_zero(bmap
->div
[j
][1 + total
+ div
]))
1239 /* Would an expression for div "div" based on inequality "ineq" of "bmap"
1240 * be a better expression than the current one?
1242 * If we do not have any expression yet, then any expression would be better.
1243 * Otherwise we check if the last variable involved in the inequality
1244 * (disregarding the div that it would define) is in an earlier position
1245 * than the last variable involved in the current div expression.
1247 static int better_div_constraint(__isl_keep isl_basic_map
*bmap
,
1250 unsigned total
= 1 + isl_space_dim(bmap
->dim
, isl_dim_all
);
1254 if (isl_int_is_zero(bmap
->div
[div
][0]))
1257 if (isl_seq_last_non_zero(bmap
->ineq
[ineq
] + total
+ div
+ 1,
1258 bmap
->n_div
- (div
+ 1)) >= 0)
1261 last_ineq
= isl_seq_last_non_zero(bmap
->ineq
[ineq
], total
+ div
);
1262 last_div
= isl_seq_last_non_zero(bmap
->div
[div
] + 1,
1263 total
+ bmap
->n_div
);
1265 return last_ineq
< last_div
;
1268 /* Given two constraints "k" and "l" that are opposite to each other,
1269 * except for the constant term, check if we can use them
1270 * to obtain an expression for one of the hitherto unknown divs or
1271 * a "better" expression for a div for which we already have an expression.
1272 * "sum" is the sum of the constant terms of the constraints.
1273 * If this sum is strictly smaller than the coefficient of one
1274 * of the divs, then this pair can be used define the div.
1275 * To avoid the introduction of circular definitions of divs, we
1276 * do not use the pair if the resulting expression would refer to
1277 * any other undefined divs or if any known div is defined in
1278 * terms of the unknown div.
1280 static struct isl_basic_map
*check_for_div_constraints(
1281 struct isl_basic_map
*bmap
, int k
, int l
, isl_int sum
, int *progress
)
1284 unsigned total
= 1 + isl_space_dim(bmap
->dim
, isl_dim_all
);
1286 for (i
= 0; i
< bmap
->n_div
; ++i
) {
1287 if (isl_int_is_zero(bmap
->ineq
[k
][total
+ i
]))
1289 if (isl_int_abs_ge(sum
, bmap
->ineq
[k
][total
+ i
]))
1291 if (!better_div_constraint(bmap
, i
, k
))
1293 if (!ok_to_set_div_from_bound(bmap
, i
, k
))
1295 if (isl_int_is_pos(bmap
->ineq
[k
][total
+ i
]))
1296 bmap
= set_div_from_lower_bound(bmap
, i
, k
);
1298 bmap
= set_div_from_lower_bound(bmap
, i
, l
);
1306 __isl_give isl_basic_map
*isl_basic_map_remove_duplicate_constraints(
1307 __isl_take isl_basic_map
*bmap
, int *progress
, int detect_divs
)
1309 struct isl_constraint_index ci
;
1311 unsigned total
= isl_basic_map_total_dim(bmap
);
1314 if (!bmap
|| bmap
->n_ineq
<= 1)
1317 if (create_constraint_index(&ci
, bmap
) < 0)
1320 h
= isl_seq_get_hash_bits(bmap
->ineq
[0] + 1, total
, ci
.bits
);
1321 ci
.index
[h
] = &bmap
->ineq
[0];
1322 for (k
= 1; k
< bmap
->n_ineq
; ++k
) {
1323 h
= hash_index(&ci
, bmap
, k
);
1325 ci
.index
[h
] = &bmap
->ineq
[k
];
1330 l
= ci
.index
[h
] - &bmap
->ineq
[0];
1331 if (isl_int_lt(bmap
->ineq
[k
][0], bmap
->ineq
[l
][0]))
1332 swap_inequality(bmap
, k
, l
);
1333 isl_basic_map_drop_inequality(bmap
, k
);
1337 for (k
= 0; k
< bmap
->n_ineq
-1; ++k
) {
1338 isl_seq_neg(bmap
->ineq
[k
]+1, bmap
->ineq
[k
]+1, total
);
1339 h
= hash_index(&ci
, bmap
, k
);
1340 isl_seq_neg(bmap
->ineq
[k
]+1, bmap
->ineq
[k
]+1, total
);
1343 l
= ci
.index
[h
] - &bmap
->ineq
[0];
1344 isl_int_add(sum
, bmap
->ineq
[k
][0], bmap
->ineq
[l
][0]);
1345 if (isl_int_is_pos(sum
)) {
1347 bmap
= check_for_div_constraints(bmap
, k
, l
,
1351 if (isl_int_is_zero(sum
)) {
1352 /* We need to break out of the loop after these
1353 * changes since the contents of the hash
1354 * will no longer be valid.
1355 * Plus, we probably we want to regauss first.
1359 isl_basic_map_drop_inequality(bmap
, l
);
1360 isl_basic_map_inequality_to_equality(bmap
, k
);
1362 bmap
= isl_basic_map_set_to_empty(bmap
);
1367 constraint_index_free(&ci
);
1371 /* Detect all pairs of inequalities that form an equality.
1373 * isl_basic_map_remove_duplicate_constraints detects at most one such pair.
1374 * Call it repeatedly while it is making progress.
1376 __isl_give isl_basic_map
*isl_basic_map_detect_inequality_pairs(
1377 __isl_take isl_basic_map
*bmap
, int *progress
)
1383 bmap
= isl_basic_map_remove_duplicate_constraints(bmap
,
1385 if (progress
&& duplicate
)
1387 } while (duplicate
);
1392 /* Eliminate knowns divs from constraints where they appear with
1393 * a (positive or negative) unit coefficient.
1397 * floor(e/m) + f >= 0
1405 * -floor(e/m) + f >= 0
1409 * -e + m f + m - 1 >= 0
1411 * The first conversion is valid because floor(e/m) >= -f is equivalent
1412 * to e/m >= -f because -f is an integral expression.
1413 * The second conversion follows from the fact that
1415 * -floor(e/m) = ceil(-e/m) = floor((-e + m - 1)/m)
1418 * Note that one of the div constraints may have been eliminated
1419 * due to being redundant with respect to the constraint that is
1420 * being modified by this function. The modified constraint may
1421 * no longer imply this div constraint, so we add it back to make
1422 * sure we do not lose any information.
1424 * We skip integral divs, i.e., those with denominator 1, as we would
1425 * risk eliminating the div from the div constraints. We do not need
1426 * to handle those divs here anyway since the div constraints will turn
1427 * out to form an equality and this equality can then be used to eliminate
1428 * the div from all constraints.
1430 static __isl_give isl_basic_map
*eliminate_unit_divs(
1431 __isl_take isl_basic_map
*bmap
, int *progress
)
1440 ctx
= isl_basic_map_get_ctx(bmap
);
1441 total
= 1 + isl_space_dim(bmap
->dim
, isl_dim_all
);
1443 for (i
= 0; i
< bmap
->n_div
; ++i
) {
1444 if (isl_int_is_zero(bmap
->div
[i
][0]))
1446 if (isl_int_is_one(bmap
->div
[i
][0]))
1448 for (j
= 0; j
< bmap
->n_ineq
; ++j
) {
1451 if (!isl_int_is_one(bmap
->ineq
[j
][total
+ i
]) &&
1452 !isl_int_is_negone(bmap
->ineq
[j
][total
+ i
]))
1457 s
= isl_int_sgn(bmap
->ineq
[j
][total
+ i
]);
1458 isl_int_set_si(bmap
->ineq
[j
][total
+ i
], 0);
1460 isl_seq_combine(bmap
->ineq
[j
],
1461 ctx
->negone
, bmap
->div
[i
] + 1,
1462 bmap
->div
[i
][0], bmap
->ineq
[j
],
1463 total
+ bmap
->n_div
);
1465 isl_seq_combine(bmap
->ineq
[j
],
1466 ctx
->one
, bmap
->div
[i
] + 1,
1467 bmap
->div
[i
][0], bmap
->ineq
[j
],
1468 total
+ bmap
->n_div
);
1470 isl_int_add(bmap
->ineq
[j
][0],
1471 bmap
->ineq
[j
][0], bmap
->div
[i
][0]);
1472 isl_int_sub_ui(bmap
->ineq
[j
][0],
1473 bmap
->ineq
[j
][0], 1);
1476 bmap
= isl_basic_map_extend_constraints(bmap
, 0, 1);
1477 if (isl_basic_map_add_div_constraint(bmap
, i
, s
) < 0)
1478 return isl_basic_map_free(bmap
);
1485 struct isl_basic_map
*isl_basic_map_simplify(struct isl_basic_map
*bmap
)
1494 if (isl_basic_map_plain_is_empty(bmap
))
1496 bmap
= isl_basic_map_normalize_constraints(bmap
);
1497 bmap
= remove_independent_vars_from_divs(bmap
);
1498 bmap
= normalize_div_expressions(bmap
);
1499 bmap
= remove_duplicate_divs(bmap
, &progress
);
1500 bmap
= eliminate_unit_divs(bmap
, &progress
);
1501 bmap
= eliminate_divs_eq(bmap
, &progress
);
1502 bmap
= eliminate_divs_ineq(bmap
, &progress
);
1503 bmap
= isl_basic_map_gauss(bmap
, &progress
);
1504 /* requires equalities in normal form */
1505 bmap
= normalize_divs(bmap
, &progress
);
1506 bmap
= isl_basic_map_remove_duplicate_constraints(bmap
,
1508 if (bmap
&& progress
)
1509 ISL_F_CLR(bmap
, ISL_BASIC_MAP_REDUCED_COEFFICIENTS
);
1514 struct isl_basic_set
*isl_basic_set_simplify(struct isl_basic_set
*bset
)
1516 return bset_from_bmap(isl_basic_map_simplify(bset_to_bmap(bset
)));
1520 int isl_basic_map_is_div_constraint(__isl_keep isl_basic_map
*bmap
,
1521 isl_int
*constraint
, unsigned div
)
1528 pos
= 1 + isl_space_dim(bmap
->dim
, isl_dim_all
) + div
;
1530 if (isl_int_eq(constraint
[pos
], bmap
->div
[div
][0])) {
1532 isl_int_sub(bmap
->div
[div
][1],
1533 bmap
->div
[div
][1], bmap
->div
[div
][0]);
1534 isl_int_add_ui(bmap
->div
[div
][1], bmap
->div
[div
][1], 1);
1535 neg
= isl_seq_is_neg(constraint
, bmap
->div
[div
]+1, pos
);
1536 isl_int_sub_ui(bmap
->div
[div
][1], bmap
->div
[div
][1], 1);
1537 isl_int_add(bmap
->div
[div
][1],
1538 bmap
->div
[div
][1], bmap
->div
[div
][0]);
1541 if (isl_seq_first_non_zero(constraint
+pos
+1,
1542 bmap
->n_div
-div
-1) != -1)
1544 } else if (isl_int_abs_eq(constraint
[pos
], bmap
->div
[div
][0])) {
1545 if (!isl_seq_eq(constraint
, bmap
->div
[div
]+1, pos
))
1547 if (isl_seq_first_non_zero(constraint
+pos
+1,
1548 bmap
->n_div
-div
-1) != -1)
1556 int isl_basic_set_is_div_constraint(__isl_keep isl_basic_set
*bset
,
1557 isl_int
*constraint
, unsigned div
)
1559 return isl_basic_map_is_div_constraint(bset
, constraint
, div
);
1563 /* If the only constraints a div d=floor(f/m)
1564 * appears in are its two defining constraints
1567 * -(f - (m - 1)) + m d >= 0
1569 * then it can safely be removed.
1571 static int div_is_redundant(struct isl_basic_map
*bmap
, int div
)
1574 unsigned pos
= 1 + isl_space_dim(bmap
->dim
, isl_dim_all
) + div
;
1576 for (i
= 0; i
< bmap
->n_eq
; ++i
)
1577 if (!isl_int_is_zero(bmap
->eq
[i
][pos
]))
1580 for (i
= 0; i
< bmap
->n_ineq
; ++i
) {
1581 if (isl_int_is_zero(bmap
->ineq
[i
][pos
]))
1583 if (!isl_basic_map_is_div_constraint(bmap
, bmap
->ineq
[i
], div
))
1587 for (i
= 0; i
< bmap
->n_div
; ++i
) {
1588 if (isl_int_is_zero(bmap
->div
[i
][0]))
1590 if (!isl_int_is_zero(bmap
->div
[i
][1+pos
]))
1598 * Remove divs that don't occur in any of the constraints or other divs.
1599 * These can arise when dropping constraints from a basic map or
1600 * when the divs of a basic map have been temporarily aligned
1601 * with the divs of another basic map.
1603 static struct isl_basic_map
*remove_redundant_divs(struct isl_basic_map
*bmap
)
1610 for (i
= bmap
->n_div
-1; i
>= 0; --i
) {
1611 if (!div_is_redundant(bmap
, i
))
1613 bmap
= isl_basic_map_drop_div(bmap
, i
);
1618 /* Mark "bmap" as final, without checking for obviously redundant
1619 * integer divisions. This function should be used when "bmap"
1620 * is known not to involve any such integer divisions.
1622 __isl_give isl_basic_map
*isl_basic_map_mark_final(
1623 __isl_take isl_basic_map
*bmap
)
1627 ISL_F_SET(bmap
, ISL_BASIC_SET_FINAL
);
1631 /* Mark "bmap" as final, after removing obviously redundant integer divisions.
1633 struct isl_basic_map
*isl_basic_map_finalize(struct isl_basic_map
*bmap
)
1635 bmap
= remove_redundant_divs(bmap
);
1636 bmap
= isl_basic_map_mark_final(bmap
);
1640 struct isl_basic_set
*isl_basic_set_finalize(struct isl_basic_set
*bset
)
1642 return bset_from_bmap(isl_basic_map_finalize(bset_to_bmap(bset
)));
1645 struct isl_set
*isl_set_finalize(struct isl_set
*set
)
1651 for (i
= 0; i
< set
->n
; ++i
) {
1652 set
->p
[i
] = isl_basic_set_finalize(set
->p
[i
]);
1662 struct isl_map
*isl_map_finalize(struct isl_map
*map
)
1668 for (i
= 0; i
< map
->n
; ++i
) {
1669 map
->p
[i
] = isl_basic_map_finalize(map
->p
[i
]);
1673 ISL_F_CLR(map
, ISL_MAP_NORMALIZED
);
1681 /* Remove definition of any div that is defined in terms of the given variable.
1682 * The div itself is not removed. Functions such as
1683 * eliminate_divs_ineq depend on the other divs remaining in place.
1685 static struct isl_basic_map
*remove_dependent_vars(struct isl_basic_map
*bmap
,
1693 for (i
= 0; i
< bmap
->n_div
; ++i
) {
1694 if (isl_int_is_zero(bmap
->div
[i
][0]))
1696 if (isl_int_is_zero(bmap
->div
[i
][1+1+pos
]))
1698 bmap
= isl_basic_map_mark_div_unknown(bmap
, i
);
1705 /* Eliminate the specified variables from the constraints using
1706 * Fourier-Motzkin. The variables themselves are not removed.
1708 struct isl_basic_map
*isl_basic_map_eliminate_vars(
1709 struct isl_basic_map
*bmap
, unsigned pos
, unsigned n
)
1720 total
= isl_basic_map_total_dim(bmap
);
1722 bmap
= isl_basic_map_cow(bmap
);
1723 for (d
= pos
+ n
- 1; d
>= 0 && d
>= pos
; --d
)
1724 bmap
= remove_dependent_vars(bmap
, d
);
1728 for (d
= pos
+ n
- 1;
1729 d
>= 0 && d
>= total
- bmap
->n_div
&& d
>= pos
; --d
)
1730 isl_seq_clr(bmap
->div
[d
-(total
-bmap
->n_div
)], 2+total
);
1731 for (d
= pos
+ n
- 1; d
>= 0 && d
>= pos
; --d
) {
1732 int n_lower
, n_upper
;
1735 for (i
= 0; i
< bmap
->n_eq
; ++i
) {
1736 if (isl_int_is_zero(bmap
->eq
[i
][1+d
]))
1738 eliminate_var_using_equality(bmap
, d
, bmap
->eq
[i
], 0, NULL
);
1739 isl_basic_map_drop_equality(bmap
, i
);
1747 for (i
= 0; i
< bmap
->n_ineq
; ++i
) {
1748 if (isl_int_is_pos(bmap
->ineq
[i
][1+d
]))
1750 else if (isl_int_is_neg(bmap
->ineq
[i
][1+d
]))
1753 bmap
= isl_basic_map_extend_constraints(bmap
,
1754 0, n_lower
* n_upper
);
1757 for (i
= bmap
->n_ineq
- 1; i
>= 0; --i
) {
1759 if (isl_int_is_zero(bmap
->ineq
[i
][1+d
]))
1762 for (j
= 0; j
< i
; ++j
) {
1763 if (isl_int_is_zero(bmap
->ineq
[j
][1+d
]))
1766 if (isl_int_sgn(bmap
->ineq
[i
][1+d
]) ==
1767 isl_int_sgn(bmap
->ineq
[j
][1+d
]))
1769 k
= isl_basic_map_alloc_inequality(bmap
);
1772 isl_seq_cpy(bmap
->ineq
[k
], bmap
->ineq
[i
],
1774 isl_seq_elim(bmap
->ineq
[k
], bmap
->ineq
[j
],
1775 1+d
, 1+total
, NULL
);
1777 isl_basic_map_drop_inequality(bmap
, i
);
1780 if (n_lower
> 0 && n_upper
> 0) {
1781 bmap
= isl_basic_map_normalize_constraints(bmap
);
1782 bmap
= isl_basic_map_remove_duplicate_constraints(bmap
,
1784 bmap
= isl_basic_map_gauss(bmap
, NULL
);
1785 bmap
= isl_basic_map_remove_redundancies(bmap
);
1789 if (ISL_F_ISSET(bmap
, ISL_BASIC_MAP_EMPTY
))
1793 ISL_F_CLR(bmap
, ISL_BASIC_MAP_NORMALIZED
);
1795 bmap
= isl_basic_map_gauss(bmap
, NULL
);
1798 isl_basic_map_free(bmap
);
1802 struct isl_basic_set
*isl_basic_set_eliminate_vars(
1803 struct isl_basic_set
*bset
, unsigned pos
, unsigned n
)
1805 return bset_from_bmap(isl_basic_map_eliminate_vars(bset_to_bmap(bset
),
1809 /* Eliminate the specified n dimensions starting at first from the
1810 * constraints, without removing the dimensions from the space.
1811 * If the set is rational, the dimensions are eliminated using Fourier-Motzkin.
1812 * Otherwise, they are projected out and the original space is restored.
1814 __isl_give isl_basic_map
*isl_basic_map_eliminate(
1815 __isl_take isl_basic_map
*bmap
,
1816 enum isl_dim_type type
, unsigned first
, unsigned n
)
1825 if (first
+ n
> isl_basic_map_dim(bmap
, type
) || first
+ n
< first
)
1826 isl_die(bmap
->ctx
, isl_error_invalid
,
1827 "index out of bounds", goto error
);
1829 if (ISL_F_ISSET(bmap
, ISL_BASIC_MAP_RATIONAL
)) {
1830 first
+= isl_basic_map_offset(bmap
, type
) - 1;
1831 bmap
= isl_basic_map_eliminate_vars(bmap
, first
, n
);
1832 return isl_basic_map_finalize(bmap
);
1835 space
= isl_basic_map_get_space(bmap
);
1836 bmap
= isl_basic_map_project_out(bmap
, type
, first
, n
);
1837 bmap
= isl_basic_map_insert_dims(bmap
, type
, first
, n
);
1838 bmap
= isl_basic_map_reset_space(bmap
, space
);
1841 isl_basic_map_free(bmap
);
1845 __isl_give isl_basic_set
*isl_basic_set_eliminate(
1846 __isl_take isl_basic_set
*bset
,
1847 enum isl_dim_type type
, unsigned first
, unsigned n
)
1849 return isl_basic_map_eliminate(bset
, type
, first
, n
);
1852 /* Remove all constraints from "bmap" that reference any unknown local
1853 * variables (directly or indirectly).
1855 * Dropping all constraints on a local variable will make it redundant,
1856 * so it will get removed implicitly by
1857 * isl_basic_map_drop_constraints_involving_dims. Some other local
1858 * variables may also end up becoming redundant if they only appear
1859 * in constraints together with the unknown local variable.
1860 * Therefore, start over after calling
1861 * isl_basic_map_drop_constraints_involving_dims.
1863 __isl_give isl_basic_map
*isl_basic_map_drop_constraint_involving_unknown_divs(
1864 __isl_take isl_basic_map
*bmap
)
1867 int i
, n_div
, o_div
;
1869 known
= isl_basic_map_divs_known(bmap
);
1871 return isl_basic_map_free(bmap
);
1875 n_div
= isl_basic_map_dim(bmap
, isl_dim_div
);
1876 o_div
= isl_basic_map_offset(bmap
, isl_dim_div
) - 1;
1878 for (i
= 0; i
< n_div
; ++i
) {
1879 known
= isl_basic_map_div_is_known(bmap
, i
);
1881 return isl_basic_map_free(bmap
);
1884 bmap
= remove_dependent_vars(bmap
, o_div
+ i
);
1885 bmap
= isl_basic_map_drop_constraints_involving_dims(bmap
,
1889 n_div
= isl_basic_map_dim(bmap
, isl_dim_div
);
1896 /* Remove all constraints from "map" that reference any unknown local
1897 * variables (directly or indirectly).
1899 * Since constraints may get dropped from the basic maps,
1900 * they may no longer be disjoint from each other.
1902 __isl_give isl_map
*isl_map_drop_constraint_involving_unknown_divs(
1903 __isl_take isl_map
*map
)
1908 known
= isl_map_divs_known(map
);
1910 return isl_map_free(map
);
1914 map
= isl_map_cow(map
);
1918 for (i
= 0; i
< map
->n
; ++i
) {
1920 isl_basic_map_drop_constraint_involving_unknown_divs(
1923 return isl_map_free(map
);
1927 ISL_F_CLR(map
, ISL_MAP_DISJOINT
);
1932 /* Don't assume equalities are in order, because align_divs
1933 * may have changed the order of the divs.
1935 static void compute_elimination_index(struct isl_basic_map
*bmap
, int *elim
)
1940 total
= isl_space_dim(bmap
->dim
, isl_dim_all
);
1941 for (d
= 0; d
< total
; ++d
)
1943 for (i
= 0; i
< bmap
->n_eq
; ++i
) {
1944 for (d
= total
- 1; d
>= 0; --d
) {
1945 if (isl_int_is_zero(bmap
->eq
[i
][1+d
]))
1953 static void set_compute_elimination_index(struct isl_basic_set
*bset
, int *elim
)
1955 compute_elimination_index(bset_to_bmap(bset
), elim
);
1958 static int reduced_using_equalities(isl_int
*dst
, isl_int
*src
,
1959 struct isl_basic_map
*bmap
, int *elim
)
1965 total
= isl_space_dim(bmap
->dim
, isl_dim_all
);
1966 for (d
= total
- 1; d
>= 0; --d
) {
1967 if (isl_int_is_zero(src
[1+d
]))
1972 isl_seq_cpy(dst
, src
, 1 + total
);
1975 isl_seq_elim(dst
, bmap
->eq
[elim
[d
]], 1 + d
, 1 + total
, NULL
);
1980 static int set_reduced_using_equalities(isl_int
*dst
, isl_int
*src
,
1981 struct isl_basic_set
*bset
, int *elim
)
1983 return reduced_using_equalities(dst
, src
,
1984 bset_to_bmap(bset
), elim
);
1987 static struct isl_basic_set
*isl_basic_set_reduce_using_equalities(
1988 struct isl_basic_set
*bset
, struct isl_basic_set
*context
)
1993 if (!bset
|| !context
)
1996 if (context
->n_eq
== 0) {
1997 isl_basic_set_free(context
);
2001 bset
= isl_basic_set_cow(bset
);
2005 elim
= isl_alloc_array(bset
->ctx
, int, isl_basic_set_n_dim(bset
));
2008 set_compute_elimination_index(context
, elim
);
2009 for (i
= 0; i
< bset
->n_eq
; ++i
)
2010 set_reduced_using_equalities(bset
->eq
[i
], bset
->eq
[i
],
2012 for (i
= 0; i
< bset
->n_ineq
; ++i
)
2013 set_reduced_using_equalities(bset
->ineq
[i
], bset
->ineq
[i
],
2015 isl_basic_set_free(context
);
2017 bset
= isl_basic_set_simplify(bset
);
2018 bset
= isl_basic_set_finalize(bset
);
2021 isl_basic_set_free(bset
);
2022 isl_basic_set_free(context
);
2026 /* For each inequality in "ineq" that is a shifted (more relaxed)
2027 * copy of an inequality in "context", mark the corresponding entry
2029 * If an inequality only has a non-negative constant term, then
2032 static isl_stat
mark_shifted_constraints(__isl_keep isl_mat
*ineq
,
2033 __isl_keep isl_basic_set
*context
, int *row
)
2035 struct isl_constraint_index ci
;
2040 if (!ineq
|| !context
)
2041 return isl_stat_error
;
2042 if (context
->n_ineq
== 0)
2044 if (setup_constraint_index(&ci
, context
) < 0)
2045 return isl_stat_error
;
2047 n_ineq
= isl_mat_rows(ineq
);
2048 total
= isl_mat_cols(ineq
) - 1;
2049 for (k
= 0; k
< n_ineq
; ++k
) {
2053 l
= isl_seq_first_non_zero(ineq
->row
[k
] + 1, total
);
2054 if (l
< 0 && isl_int_is_nonneg(ineq
->row
[k
][0])) {
2058 redundant
= constraint_index_is_redundant(&ci
, ineq
->row
[k
]);
2065 constraint_index_free(&ci
);
2068 constraint_index_free(&ci
);
2069 return isl_stat_error
;
2072 static struct isl_basic_set
*remove_shifted_constraints(
2073 struct isl_basic_set
*bset
, struct isl_basic_set
*context
)
2075 struct isl_constraint_index ci
;
2078 if (!bset
|| !context
)
2081 if (context
->n_ineq
== 0)
2083 if (setup_constraint_index(&ci
, context
) < 0)
2086 for (k
= 0; k
< bset
->n_ineq
; ++k
) {
2089 redundant
= constraint_index_is_redundant(&ci
, bset
->ineq
[k
]);
2094 bset
= isl_basic_set_cow(bset
);
2097 isl_basic_set_drop_inequality(bset
, k
);
2100 constraint_index_free(&ci
);
2103 constraint_index_free(&ci
);
2107 /* Remove constraints from "bmap" that are identical to constraints
2108 * in "context" or that are more relaxed (greater constant term).
2110 * We perform the test for shifted copies on the pure constraints
2111 * in remove_shifted_constraints.
2113 static __isl_give isl_basic_map
*isl_basic_map_remove_shifted_constraints(
2114 __isl_take isl_basic_map
*bmap
, __isl_take isl_basic_map
*context
)
2116 isl_basic_set
*bset
, *bset_context
;
2118 if (!bmap
|| !context
)
2121 if (bmap
->n_ineq
== 0 || context
->n_ineq
== 0) {
2122 isl_basic_map_free(context
);
2126 context
= isl_basic_map_align_divs(context
, bmap
);
2127 bmap
= isl_basic_map_align_divs(bmap
, context
);
2129 bset
= isl_basic_map_underlying_set(isl_basic_map_copy(bmap
));
2130 bset_context
= isl_basic_map_underlying_set(context
);
2131 bset
= remove_shifted_constraints(bset
, bset_context
);
2132 isl_basic_set_free(bset_context
);
2134 bmap
= isl_basic_map_overlying_set(bset
, bmap
);
2138 isl_basic_map_free(bmap
);
2139 isl_basic_map_free(context
);
2143 /* Does the (linear part of a) constraint "c" involve any of the "len"
2144 * "relevant" dimensions?
2146 static int is_related(isl_int
*c
, int len
, int *relevant
)
2150 for (i
= 0; i
< len
; ++i
) {
2153 if (!isl_int_is_zero(c
[i
]))
2160 /* Drop constraints from "bmap" that do not involve any of
2161 * the dimensions marked "relevant".
2163 static __isl_give isl_basic_map
*drop_unrelated_constraints(
2164 __isl_take isl_basic_map
*bmap
, int *relevant
)
2168 dim
= isl_basic_map_dim(bmap
, isl_dim_all
);
2169 for (i
= 0; i
< dim
; ++i
)
2175 for (i
= bmap
->n_eq
- 1; i
>= 0; --i
)
2176 if (!is_related(bmap
->eq
[i
] + 1, dim
, relevant
)) {
2177 bmap
= isl_basic_map_cow(bmap
);
2178 if (isl_basic_map_drop_equality(bmap
, i
) < 0)
2179 return isl_basic_map_free(bmap
);
2182 for (i
= bmap
->n_ineq
- 1; i
>= 0; --i
)
2183 if (!is_related(bmap
->ineq
[i
] + 1, dim
, relevant
)) {
2184 bmap
= isl_basic_map_cow(bmap
);
2185 if (isl_basic_map_drop_inequality(bmap
, i
) < 0)
2186 return isl_basic_map_free(bmap
);
2192 /* Update the groups in "group" based on the (linear part of a) constraint "c".
2194 * In particular, for any variable involved in the constraint,
2195 * find the actual group id from before and replace the group
2196 * of the corresponding variable by the minimal group of all
2197 * the variables involved in the constraint considered so far
2198 * (if this minimum is smaller) or replace the minimum by this group
2199 * (if the minimum is larger).
2201 * At the end, all the variables in "c" will (indirectly) point
2202 * to the minimal of the groups that they referred to originally.
2204 static void update_groups(int dim
, int *group
, isl_int
*c
)
2209 for (j
= 0; j
< dim
; ++j
) {
2210 if (isl_int_is_zero(c
[j
]))
2212 while (group
[j
] >= 0 && group
[group
[j
]] != group
[j
])
2213 group
[j
] = group
[group
[j
]];
2214 if (group
[j
] == min
)
2216 if (group
[j
] < min
) {
2217 if (min
>= 0 && min
< dim
)
2218 group
[min
] = group
[j
];
2221 group
[group
[j
]] = min
;
2225 /* Allocate an array of groups of variables, one for each variable
2226 * in "context", initialized to zero.
2228 static int *alloc_groups(__isl_keep isl_basic_set
*context
)
2233 dim
= isl_basic_set_dim(context
, isl_dim_set
);
2234 ctx
= isl_basic_set_get_ctx(context
);
2235 return isl_calloc_array(ctx
, int, dim
);
2238 /* Drop constraints from "bmap" that only involve variables that are
2239 * not related to any of the variables marked with a "-1" in "group".
2241 * We construct groups of variables that collect variables that
2242 * (indirectly) appear in some common constraint of "bmap".
2243 * Each group is identified by the first variable in the group,
2244 * except for the special group of variables that was already identified
2245 * in the input as -1 (or are related to those variables).
2246 * If group[i] is equal to i (or -1), then the group of i is i (or -1),
2247 * otherwise the group of i is the group of group[i].
2249 * We first initialize groups for the remaining variables.
2250 * Then we iterate over the constraints of "bmap" and update the
2251 * group of the variables in the constraint by the smallest group.
2252 * Finally, we resolve indirect references to groups by running over
2255 * After computing the groups, we drop constraints that do not involve
2256 * any variables in the -1 group.
2258 __isl_give isl_basic_map
*isl_basic_map_drop_unrelated_constraints(
2259 __isl_take isl_basic_map
*bmap
, __isl_take
int *group
)
2268 dim
= isl_basic_map_dim(bmap
, isl_dim_all
);
2271 for (i
= 0; i
< dim
; ++i
)
2273 last
= group
[i
] = i
;
2279 for (i
= 0; i
< bmap
->n_eq
; ++i
)
2280 update_groups(dim
, group
, bmap
->eq
[i
] + 1);
2281 for (i
= 0; i
< bmap
->n_ineq
; ++i
)
2282 update_groups(dim
, group
, bmap
->ineq
[i
] + 1);
2284 for (i
= 0; i
< dim
; ++i
)
2286 group
[i
] = group
[group
[i
]];
2288 for (i
= 0; i
< dim
; ++i
)
2289 group
[i
] = group
[i
] == -1;
2291 bmap
= drop_unrelated_constraints(bmap
, group
);
2297 /* Drop constraints from "context" that are irrelevant for computing
2298 * the gist of "bset".
2300 * In particular, drop constraints in variables that are not related
2301 * to any of the variables involved in the constraints of "bset"
2302 * in the sense that there is no sequence of constraints that connects them.
2304 * We first mark all variables that appear in "bset" as belonging
2305 * to a "-1" group and then continue with group_and_drop_irrelevant_constraints.
2307 static __isl_give isl_basic_set
*drop_irrelevant_constraints(
2308 __isl_take isl_basic_set
*context
, __isl_keep isl_basic_set
*bset
)
2314 if (!context
|| !bset
)
2315 return isl_basic_set_free(context
);
2317 group
= alloc_groups(context
);
2320 return isl_basic_set_free(context
);
2322 dim
= isl_basic_set_dim(bset
, isl_dim_set
);
2323 for (i
= 0; i
< dim
; ++i
) {
2324 for (j
= 0; j
< bset
->n_eq
; ++j
)
2325 if (!isl_int_is_zero(bset
->eq
[j
][1 + i
]))
2327 if (j
< bset
->n_eq
) {
2331 for (j
= 0; j
< bset
->n_ineq
; ++j
)
2332 if (!isl_int_is_zero(bset
->ineq
[j
][1 + i
]))
2334 if (j
< bset
->n_ineq
)
2338 return isl_basic_map_drop_unrelated_constraints(context
, group
);
2341 /* Drop constraints from "context" that are irrelevant for computing
2342 * the gist of the inequalities "ineq".
2343 * Inequalities in "ineq" for which the corresponding element of row
2344 * is set to -1 have already been marked for removal and should be ignored.
2346 * In particular, drop constraints in variables that are not related
2347 * to any of the variables involved in "ineq"
2348 * in the sense that there is no sequence of constraints that connects them.
2350 * We first mark all variables that appear in "bset" as belonging
2351 * to a "-1" group and then continue with group_and_drop_irrelevant_constraints.
2353 static __isl_give isl_basic_set
*drop_irrelevant_constraints_marked(
2354 __isl_take isl_basic_set
*context
, __isl_keep isl_mat
*ineq
, int *row
)
2360 if (!context
|| !ineq
)
2361 return isl_basic_set_free(context
);
2363 group
= alloc_groups(context
);
2366 return isl_basic_set_free(context
);
2368 dim
= isl_basic_set_dim(context
, isl_dim_set
);
2369 n
= isl_mat_rows(ineq
);
2370 for (i
= 0; i
< dim
; ++i
) {
2371 for (j
= 0; j
< n
; ++j
) {
2374 if (!isl_int_is_zero(ineq
->row
[j
][1 + i
]))
2381 return isl_basic_map_drop_unrelated_constraints(context
, group
);
2384 /* Do all "n" entries of "row" contain a negative value?
2386 static int all_neg(int *row
, int n
)
2390 for (i
= 0; i
< n
; ++i
)
2397 /* Update the inequalities in "bset" based on the information in "row"
2400 * In particular, the array "row" contains either -1, meaning that
2401 * the corresponding inequality of "bset" is redundant, or the index
2402 * of an inequality in "tab".
2404 * If the row entry is -1, then drop the inequality.
2405 * Otherwise, if the constraint is marked redundant in the tableau,
2406 * then drop the inequality. Similarly, if it is marked as an equality
2407 * in the tableau, then turn the inequality into an equality and
2408 * perform Gaussian elimination.
2410 static __isl_give isl_basic_set
*update_ineq(__isl_take isl_basic_set
*bset
,
2411 __isl_keep
int *row
, struct isl_tab
*tab
)
2416 int found_equality
= 0;
2420 if (tab
&& tab
->empty
)
2421 return isl_basic_set_set_to_empty(bset
);
2423 n_ineq
= bset
->n_ineq
;
2424 for (i
= n_ineq
- 1; i
>= 0; --i
) {
2426 if (isl_basic_set_drop_inequality(bset
, i
) < 0)
2427 return isl_basic_set_free(bset
);
2433 if (isl_tab_is_equality(tab
, n_eq
+ row
[i
])) {
2434 isl_basic_map_inequality_to_equality(bset
, i
);
2436 } else if (isl_tab_is_redundant(tab
, n_eq
+ row
[i
])) {
2437 if (isl_basic_set_drop_inequality(bset
, i
) < 0)
2438 return isl_basic_set_free(bset
);
2443 bset
= isl_basic_set_gauss(bset
, NULL
);
2444 bset
= isl_basic_set_finalize(bset
);
2448 /* Update the inequalities in "bset" based on the information in "row"
2449 * and "tab" and free all arguments (other than "bset").
2451 static __isl_give isl_basic_set
*update_ineq_free(
2452 __isl_take isl_basic_set
*bset
, __isl_take isl_mat
*ineq
,
2453 __isl_take isl_basic_set
*context
, __isl_take
int *row
,
2454 struct isl_tab
*tab
)
2457 isl_basic_set_free(context
);
2459 bset
= update_ineq(bset
, row
, tab
);
2466 /* Remove all information from bset that is redundant in the context
2468 * "ineq" contains the (possibly transformed) inequalities of "bset",
2469 * in the same order.
2470 * The (explicit) equalities of "bset" are assumed to have been taken
2471 * into account by the transformation such that only the inequalities
2473 * "context" is assumed not to be empty.
2475 * "row" keeps track of the constraint index of a "bset" inequality in "tab".
2476 * A value of -1 means that the inequality is obviously redundant and may
2477 * not even appear in "tab".
2479 * We first mark the inequalities of "bset"
2480 * that are obviously redundant with respect to some inequality in "context".
2481 * Then we remove those constraints from "context" that have become
2482 * irrelevant for computing the gist of "bset".
2483 * Note that this removal of constraints cannot be replaced by
2484 * a factorization because factors in "bset" may still be connected
2485 * to each other through constraints in "context".
2487 * If there are any inequalities left, we construct a tableau for
2488 * the context and then add the inequalities of "bset".
2489 * Before adding these inequalities, we freeze all constraints such that
2490 * they won't be considered redundant in terms of the constraints of "bset".
2491 * Then we detect all redundant constraints (among the
2492 * constraints that weren't frozen), first by checking for redundancy in the
2493 * the tableau and then by checking if replacing a constraint by its negation
2494 * would lead to an empty set. This last step is fairly expensive
2495 * and could be optimized by more reuse of the tableau.
2496 * Finally, we update bset according to the results.
2498 static __isl_give isl_basic_set
*uset_gist_full(__isl_take isl_basic_set
*bset
,
2499 __isl_take isl_mat
*ineq
, __isl_take isl_basic_set
*context
)
2504 isl_basic_set
*combined
= NULL
;
2505 struct isl_tab
*tab
= NULL
;
2506 unsigned n_eq
, context_ineq
;
2509 if (!bset
|| !ineq
|| !context
)
2512 if (bset
->n_ineq
== 0 || isl_basic_set_plain_is_universe(context
)) {
2513 isl_basic_set_free(context
);
2518 ctx
= isl_basic_set_get_ctx(context
);
2519 row
= isl_calloc_array(ctx
, int, bset
->n_ineq
);
2523 if (mark_shifted_constraints(ineq
, context
, row
) < 0)
2525 if (all_neg(row
, bset
->n_ineq
))
2526 return update_ineq_free(bset
, ineq
, context
, row
, NULL
);
2528 context
= drop_irrelevant_constraints_marked(context
, ineq
, row
);
2531 if (isl_basic_set_plain_is_universe(context
))
2532 return update_ineq_free(bset
, ineq
, context
, row
, NULL
);
2534 n_eq
= context
->n_eq
;
2535 context_ineq
= context
->n_ineq
;
2536 combined
= isl_basic_set_cow(isl_basic_set_copy(context
));
2537 combined
= isl_basic_set_extend_constraints(combined
, 0, bset
->n_ineq
);
2538 tab
= isl_tab_from_basic_set(combined
, 0);
2539 for (i
= 0; i
< context_ineq
; ++i
)
2540 if (isl_tab_freeze_constraint(tab
, n_eq
+ i
) < 0)
2542 if (isl_tab_extend_cons(tab
, bset
->n_ineq
) < 0)
2545 for (i
= 0; i
< bset
->n_ineq
; ++i
) {
2548 combined
= isl_basic_set_add_ineq(combined
, ineq
->row
[i
]);
2549 if (isl_tab_add_ineq(tab
, ineq
->row
[i
]) < 0)
2553 if (isl_tab_detect_implicit_equalities(tab
) < 0)
2555 if (isl_tab_detect_redundant(tab
) < 0)
2557 total
= isl_basic_set_total_dim(bset
);
2558 for (i
= bset
->n_ineq
- 1; i
>= 0; --i
) {
2559 isl_basic_set
*test
;
2565 if (tab
->con
[n_eq
+ r
].is_redundant
)
2567 test
= isl_basic_set_dup(combined
);
2568 if (isl_inequality_negate(test
, r
) < 0)
2569 test
= isl_basic_set_free(test
);
2570 test
= isl_basic_set_update_from_tab(test
, tab
);
2571 is_empty
= isl_basic_set_is_empty(test
);
2572 isl_basic_set_free(test
);
2576 tab
->con
[n_eq
+ r
].is_redundant
= 1;
2578 bset
= update_ineq_free(bset
, ineq
, context
, row
, tab
);
2580 ISL_F_SET(bset
, ISL_BASIC_SET_NO_IMPLICIT
);
2581 ISL_F_SET(bset
, ISL_BASIC_SET_NO_REDUNDANT
);
2584 isl_basic_set_free(combined
);
2590 isl_basic_set_free(combined
);
2591 isl_basic_set_free(context
);
2592 isl_basic_set_free(bset
);
2596 /* Extract the inequalities of "bset" as an isl_mat.
2598 static __isl_give isl_mat
*extract_ineq(__isl_keep isl_basic_set
*bset
)
2607 ctx
= isl_basic_set_get_ctx(bset
);
2608 total
= isl_basic_set_total_dim(bset
);
2609 ineq
= isl_mat_sub_alloc6(ctx
, bset
->ineq
, 0, bset
->n_ineq
,
2615 /* Remove all information from "bset" that is redundant in the context
2616 * of "context", for the case where both "bset" and "context" are
2619 static __isl_give isl_basic_set
*uset_gist_uncompressed(
2620 __isl_take isl_basic_set
*bset
, __isl_take isl_basic_set
*context
)
2624 ineq
= extract_ineq(bset
);
2625 return uset_gist_full(bset
, ineq
, context
);
2628 /* Remove all information from "bset" that is redundant in the context
2629 * of "context", for the case where the combined equalities of
2630 * "bset" and "context" allow for a compression that can be obtained
2631 * by preapplication of "T".
2633 * "bset" itself is not transformed by "T". Instead, the inequalities
2634 * are extracted from "bset" and those are transformed by "T".
2635 * uset_gist_full then determines which of the transformed inequalities
2636 * are redundant with respect to the transformed "context" and removes
2637 * the corresponding inequalities from "bset".
2639 * After preapplying "T" to the inequalities, any common factor is
2640 * removed from the coefficients. If this results in a tightening
2641 * of the constant term, then the same tightening is applied to
2642 * the corresponding untransformed inequality in "bset".
2643 * That is, if after plugging in T, a constraint f(x) >= 0 is of the form
2647 * with 0 <= r < g, then it is equivalent to
2651 * This means that f(x) >= 0 is equivalent to f(x) - r >= 0 in the affine
2652 * subspace compressed by T since the latter would be transformed to
2656 static __isl_give isl_basic_set
*uset_gist_compressed(
2657 __isl_take isl_basic_set
*bset
, __isl_take isl_basic_set
*context
,
2658 __isl_take isl_mat
*T
)
2662 int i
, n_row
, n_col
;
2665 ineq
= extract_ineq(bset
);
2666 ineq
= isl_mat_product(ineq
, isl_mat_copy(T
));
2667 context
= isl_basic_set_preimage(context
, T
);
2669 if (!ineq
|| !context
)
2671 if (isl_basic_set_plain_is_empty(context
)) {
2673 isl_basic_set_free(context
);
2674 return isl_basic_set_set_to_empty(bset
);
2677 ctx
= isl_mat_get_ctx(ineq
);
2678 n_row
= isl_mat_rows(ineq
);
2679 n_col
= isl_mat_cols(ineq
);
2681 for (i
= 0; i
< n_row
; ++i
) {
2682 isl_seq_gcd(ineq
->row
[i
] + 1, n_col
- 1, &ctx
->normalize_gcd
);
2683 if (isl_int_is_zero(ctx
->normalize_gcd
))
2685 if (isl_int_is_one(ctx
->normalize_gcd
))
2687 isl_seq_scale_down(ineq
->row
[i
] + 1, ineq
->row
[i
] + 1,
2688 ctx
->normalize_gcd
, n_col
- 1);
2689 isl_int_fdiv_r(rem
, ineq
->row
[i
][0], ctx
->normalize_gcd
);
2690 isl_int_fdiv_q(ineq
->row
[i
][0],
2691 ineq
->row
[i
][0], ctx
->normalize_gcd
);
2692 if (isl_int_is_zero(rem
))
2694 bset
= isl_basic_set_cow(bset
);
2697 isl_int_sub(bset
->ineq
[i
][0], bset
->ineq
[i
][0], rem
);
2701 return uset_gist_full(bset
, ineq
, context
);
2704 isl_basic_set_free(context
);
2705 isl_basic_set_free(bset
);
2709 /* Project "bset" onto the variables that are involved in "template".
2711 static __isl_give isl_basic_set
*project_onto_involved(
2712 __isl_take isl_basic_set
*bset
, __isl_keep isl_basic_set
*template)
2716 if (!bset
|| !template)
2717 return isl_basic_set_free(bset
);
2719 n
= isl_basic_set_dim(template, isl_dim_set
);
2721 for (i
= 0; i
< n
; ++i
) {
2724 involved
= isl_basic_set_involves_dims(template,
2727 return isl_basic_set_free(bset
);
2730 bset
= isl_basic_set_eliminate_vars(bset
, i
, 1);
2736 /* Remove all information from bset that is redundant in the context
2737 * of context. In particular, equalities that are linear combinations
2738 * of those in context are removed. Then the inequalities that are
2739 * redundant in the context of the equalities and inequalities of
2740 * context are removed.
2742 * First of all, we drop those constraints from "context"
2743 * that are irrelevant for computing the gist of "bset".
2744 * Alternatively, we could factorize the intersection of "context" and "bset".
2746 * We first compute the intersection of the integer affine hulls
2747 * of "bset" and "context",
2748 * compute the gist inside this intersection and then reduce
2749 * the constraints with respect to the equalities of the context
2750 * that only involve variables already involved in the input.
2752 * If two constraints are mutually redundant, then uset_gist_full
2753 * will remove the second of those constraints. We therefore first
2754 * sort the constraints so that constraints not involving existentially
2755 * quantified variables are given precedence over those that do.
2756 * We have to perform this sorting before the variable compression,
2757 * because that may effect the order of the variables.
2759 static __isl_give isl_basic_set
*uset_gist(__isl_take isl_basic_set
*bset
,
2760 __isl_take isl_basic_set
*context
)
2765 isl_basic_set
*aff_context
;
2768 if (!bset
|| !context
)
2771 context
= drop_irrelevant_constraints(context
, bset
);
2773 bset
= isl_basic_set_detect_equalities(bset
);
2774 aff
= isl_basic_set_copy(bset
);
2775 aff
= isl_basic_set_plain_affine_hull(aff
);
2776 context
= isl_basic_set_detect_equalities(context
);
2777 aff_context
= isl_basic_set_copy(context
);
2778 aff_context
= isl_basic_set_plain_affine_hull(aff_context
);
2779 aff
= isl_basic_set_intersect(aff
, aff_context
);
2782 if (isl_basic_set_plain_is_empty(aff
)) {
2783 isl_basic_set_free(bset
);
2784 isl_basic_set_free(context
);
2787 bset
= isl_basic_set_sort_constraints(bset
);
2788 if (aff
->n_eq
== 0) {
2789 isl_basic_set_free(aff
);
2790 return uset_gist_uncompressed(bset
, context
);
2792 total
= isl_basic_set_total_dim(bset
);
2793 eq
= isl_mat_sub_alloc6(bset
->ctx
, aff
->eq
, 0, aff
->n_eq
, 0, 1 + total
);
2794 eq
= isl_mat_cow(eq
);
2795 T
= isl_mat_variable_compression(eq
, NULL
);
2796 isl_basic_set_free(aff
);
2797 if (T
&& T
->n_col
== 0) {
2799 isl_basic_set_free(context
);
2800 return isl_basic_set_set_to_empty(bset
);
2803 aff_context
= isl_basic_set_affine_hull(isl_basic_set_copy(context
));
2804 aff_context
= project_onto_involved(aff_context
, bset
);
2806 bset
= uset_gist_compressed(bset
, context
, T
);
2807 bset
= isl_basic_set_reduce_using_equalities(bset
, aff_context
);
2810 ISL_F_SET(bset
, ISL_BASIC_SET_NO_IMPLICIT
);
2811 ISL_F_SET(bset
, ISL_BASIC_SET_NO_REDUNDANT
);
2816 isl_basic_set_free(bset
);
2817 isl_basic_set_free(context
);
2821 /* Return the number of equality constraints in "bmap" that involve
2822 * local variables. This function assumes that Gaussian elimination
2823 * has been applied to the equality constraints.
2825 static int n_div_eq(__isl_keep isl_basic_map
*bmap
)
2833 if (bmap
->n_eq
== 0)
2836 total
= isl_basic_map_dim(bmap
, isl_dim_all
);
2837 n_div
= isl_basic_map_dim(bmap
, isl_dim_div
);
2840 for (i
= 0; i
< bmap
->n_eq
; ++i
)
2841 if (isl_seq_first_non_zero(bmap
->eq
[i
] + 1 + total
,
2848 /* Construct a basic map in "space" defined by the equality constraints in "eq".
2849 * The constraints are assumed not to involve any local variables.
2851 static __isl_give isl_basic_map
*basic_map_from_equalities(
2852 __isl_take isl_space
*space
, __isl_take isl_mat
*eq
)
2855 isl_basic_map
*bmap
= NULL
;
2860 if (1 + isl_space_dim(space
, isl_dim_all
) != eq
->n_col
)
2861 isl_die(isl_space_get_ctx(space
), isl_error_internal
,
2862 "unexpected number of columns", goto error
);
2864 bmap
= isl_basic_map_alloc_space(isl_space_copy(space
),
2866 for (i
= 0; i
< eq
->n_row
; ++i
) {
2867 k
= isl_basic_map_alloc_equality(bmap
);
2870 isl_seq_cpy(bmap
->eq
[k
], eq
->row
[i
], eq
->n_col
);
2873 isl_space_free(space
);
2877 isl_space_free(space
);
2879 isl_basic_map_free(bmap
);
2883 /* Construct and return a variable compression based on the equality
2884 * constraints in "bmap1" and "bmap2" that do not involve the local variables.
2885 * "n1" is the number of (initial) equality constraints in "bmap1"
2886 * that do involve local variables.
2887 * "n2" is the number of (initial) equality constraints in "bmap2"
2888 * that do involve local variables.
2889 * "total" is the total number of other variables.
2890 * This function assumes that Gaussian elimination
2891 * has been applied to the equality constraints in both "bmap1" and "bmap2"
2892 * such that the equality constraints not involving local variables
2893 * are those that start at "n1" or "n2".
2895 * If either of "bmap1" and "bmap2" does not have such equality constraints,
2896 * then simply compute the compression based on the equality constraints
2897 * in the other basic map.
2898 * Otherwise, combine the equality constraints from both into a new
2899 * basic map such that Gaussian elimination can be applied to this combination
2900 * and then construct a variable compression from the resulting
2901 * equality constraints.
2903 static __isl_give isl_mat
*combined_variable_compression(
2904 __isl_keep isl_basic_map
*bmap1
, int n1
,
2905 __isl_keep isl_basic_map
*bmap2
, int n2
, int total
)
2908 isl_mat
*E1
, *E2
, *V
;
2909 isl_basic_map
*bmap
;
2911 ctx
= isl_basic_map_get_ctx(bmap1
);
2912 if (bmap1
->n_eq
== n1
) {
2913 E2
= isl_mat_sub_alloc6(ctx
, bmap2
->eq
,
2914 n2
, bmap2
->n_eq
- n2
, 0, 1 + total
);
2915 return isl_mat_variable_compression(E2
, NULL
);
2917 if (bmap2
->n_eq
== n2
) {
2918 E1
= isl_mat_sub_alloc6(ctx
, bmap1
->eq
,
2919 n1
, bmap1
->n_eq
- n1
, 0, 1 + total
);
2920 return isl_mat_variable_compression(E1
, NULL
);
2922 E1
= isl_mat_sub_alloc6(ctx
, bmap1
->eq
,
2923 n1
, bmap1
->n_eq
- n1
, 0, 1 + total
);
2924 E2
= isl_mat_sub_alloc6(ctx
, bmap2
->eq
,
2925 n2
, bmap2
->n_eq
- n2
, 0, 1 + total
);
2926 E1
= isl_mat_concat(E1
, E2
);
2927 bmap
= basic_map_from_equalities(isl_basic_map_get_space(bmap1
), E1
);
2928 bmap
= isl_basic_map_gauss(bmap
, NULL
);
2931 E1
= isl_mat_sub_alloc6(ctx
, bmap
->eq
, 0, bmap
->n_eq
, 0, 1 + total
);
2932 V
= isl_mat_variable_compression(E1
, NULL
);
2933 isl_basic_map_free(bmap
);
2938 /* Extract the stride constraints from "bmap", compressed
2939 * with respect to both the stride constraints in "context" and
2940 * the remaining equality constraints in both "bmap" and "context".
2941 * "bmap_n_eq" is the number of (initial) stride constraints in "bmap".
2942 * "context_n_eq" is the number of (initial) stride constraints in "context".
2944 * Let x be all variables in "bmap" (and "context") other than the local
2945 * variables. First compute a variable compression
2949 * based on the non-stride equality constraints in "bmap" and "context".
2950 * Consider the stride constraints of "context",
2954 * with y the local variables and plug in the variable compression,
2957 * A(V x') + B(y) = 0
2959 * Use these constraints to compute a parameter compression on x'
2963 * Now consider the stride constraints of "bmap"
2967 * and plug in x = V*T x''.
2968 * That is, return A = [C*V*T D].
2970 static __isl_give isl_mat
*extract_compressed_stride_constraints(
2971 __isl_keep isl_basic_map
*bmap
, int bmap_n_eq
,
2972 __isl_keep isl_basic_map
*context
, int context_n_eq
)
2976 isl_mat
*A
, *B
, *T
, *V
;
2978 total
= isl_basic_map_dim(context
, isl_dim_all
);
2979 n_div
= isl_basic_map_dim(context
, isl_dim_div
);
2982 ctx
= isl_basic_map_get_ctx(bmap
);
2984 V
= combined_variable_compression(bmap
, bmap_n_eq
,
2985 context
, context_n_eq
, total
);
2987 A
= isl_mat_sub_alloc6(ctx
, context
->eq
, 0, context_n_eq
, 0, 1 + total
);
2988 B
= isl_mat_sub_alloc6(ctx
, context
->eq
,
2989 0, context_n_eq
, 1 + total
, n_div
);
2990 A
= isl_mat_product(A
, isl_mat_copy(V
));
2991 T
= isl_mat_parameter_compression_ext(A
, B
);
2992 T
= isl_mat_product(V
, T
);
2994 n_div
= isl_basic_map_dim(bmap
, isl_dim_div
);
2995 T
= isl_mat_diagonal(T
, isl_mat_identity(ctx
, n_div
));
2997 A
= isl_mat_sub_alloc6(ctx
, bmap
->eq
,
2998 0, bmap_n_eq
, 0, 1 + total
+ n_div
);
2999 A
= isl_mat_product(A
, T
);
3004 /* Remove the prime factors from *g that have an exponent that
3005 * is strictly smaller than the exponent in "c".
3006 * All exponents in *g are known to be smaller than or equal
3009 * That is, if *g is equal to
3011 * p_1^{e_1} p_2^{e_2} ... p_n^{e_n}
3013 * and "c" is equal to
3015 * p_1^{f_1} p_2^{f_2} ... p_n^{f_n}
3019 * p_1^{e_1 * (e_1 = f_1)} p_2^{e_2 * (e_2 = f_2)} ...
3020 * p_n^{e_n * (e_n = f_n)}
3022 * If e_i = f_i, then c / *g does not have any p_i factors and therefore
3023 * neither does the gcd of *g and c / *g.
3024 * If e_i < f_i, then the gcd of *g and c / *g has a positive
3025 * power min(e_i, s_i) of p_i with s_i = f_i - e_i among its factors.
3026 * Dividing *g by this gcd therefore strictly reduces the exponent
3027 * of the prime factors that need to be removed, while leaving the
3028 * other prime factors untouched.
3029 * Repeating this process until gcd(*g, c / *g) = 1 therefore
3030 * removes all undesired factors, without removing any others.
3032 static void remove_incomplete_powers(isl_int
*g
, isl_int c
)
3038 isl_int_divexact(t
, c
, *g
);
3039 isl_int_gcd(t
, t
, *g
);
3040 if (isl_int_is_one(t
))
3042 isl_int_divexact(*g
, *g
, t
);
3047 /* Reduce the "n" stride constraints in "bmap" based on a copy "A"
3048 * of the same stride constraints in a compressed space that exploits
3049 * all equalities in the context and the other equalities in "bmap".
3051 * If the stride constraints of "bmap" are of the form
3055 * then A is of the form
3059 * If any of these constraints involves only a single local variable y,
3060 * then the constraint appears as
3070 * Let g be the gcd of m and the coefficients of h.
3071 * Then, in particular, g is a divisor of the coefficients of h and
3075 * is known to be a multiple of g.
3076 * If some prime factor in m appears with the same exponent in g,
3077 * then it can be removed from m because f(x) is already known
3078 * to be a multiple of g and therefore in particular of this power
3079 * of the prime factors.
3080 * Prime factors that appear with a smaller exponent in g cannot
3081 * be removed from m.
3082 * Let g' be the divisor of g containing all prime factors that
3083 * appear with the same exponent in m and g, then
3087 * can be replaced by
3089 * f(x) + m/g' y_i' = 0
3091 * Note that (if g' != 1) this changes the explicit representation
3092 * of y_i to that of y_i', so the integer division at position i
3093 * is marked unknown and later recomputed by a call to
3094 * isl_basic_map_gauss.
3096 static __isl_give isl_basic_map
*reduce_stride_constraints(
3097 __isl_take isl_basic_map
*bmap
, int n
, __isl_keep isl_mat
*A
)
3105 return isl_basic_map_free(bmap
);
3107 total
= isl_basic_map_dim(bmap
, isl_dim_all
);
3108 n_div
= isl_basic_map_dim(bmap
, isl_dim_div
);
3112 for (i
= 0; i
< n
; ++i
) {
3115 div
= isl_seq_first_non_zero(bmap
->eq
[i
] + 1 + total
, n_div
);
3117 isl_die(isl_basic_map_get_ctx(bmap
), isl_error_internal
,
3118 "equality constraints modified unexpectedly",
3120 if (isl_seq_first_non_zero(bmap
->eq
[i
] + 1 + total
+ div
+ 1,
3121 n_div
- div
- 1) != -1)
3123 if (isl_mat_row_gcd(A
, i
, &gcd
) < 0)
3125 if (isl_int_is_one(gcd
))
3127 remove_incomplete_powers(&gcd
, bmap
->eq
[i
][1 + total
+ div
]);
3128 if (isl_int_is_one(gcd
))
3130 isl_int_divexact(bmap
->eq
[i
][1 + total
+ div
],
3131 bmap
->eq
[i
][1 + total
+ div
], gcd
);
3132 bmap
= isl_basic_map_mark_div_unknown(bmap
, div
);
3140 bmap
= isl_basic_map_gauss(bmap
, NULL
);
3145 isl_basic_map_free(bmap
);
3149 /* Simplify the stride constraints in "bmap" based on
3150 * the remaining equality constraints in "bmap" and all equality
3151 * constraints in "context".
3152 * Only do this if both "bmap" and "context" have stride constraints.
3154 * First extract a copy of the stride constraints in "bmap" in a compressed
3155 * space exploiting all the other equality constraints and then
3156 * use this compressed copy to simplify the original stride constraints.
3158 static __isl_give isl_basic_map
*gist_strides(__isl_take isl_basic_map
*bmap
,
3159 __isl_keep isl_basic_map
*context
)
3161 int bmap_n_eq
, context_n_eq
;
3164 if (!bmap
|| !context
)
3165 return isl_basic_map_free(bmap
);
3167 bmap_n_eq
= n_div_eq(bmap
);
3168 context_n_eq
= n_div_eq(context
);
3170 if (bmap_n_eq
< 0 || context_n_eq
< 0)
3171 return isl_basic_map_free(bmap
);
3172 if (bmap_n_eq
== 0 || context_n_eq
== 0)
3175 A
= extract_compressed_stride_constraints(bmap
, bmap_n_eq
,
3176 context
, context_n_eq
);
3177 bmap
= reduce_stride_constraints(bmap
, bmap_n_eq
, A
);
3184 /* Return a basic map that has the same intersection with "context" as "bmap"
3185 * and that is as "simple" as possible.
3187 * The core computation is performed on the pure constraints.
3188 * When we add back the meaning of the integer divisions, we need
3189 * to (re)introduce the div constraints. If we happen to have
3190 * discovered that some of these integer divisions are equal to
3191 * some affine combination of other variables, then these div
3192 * constraints may end up getting simplified in terms of the equalities,
3193 * resulting in extra inequalities on the other variables that
3194 * may have been removed already or that may not even have been
3195 * part of the input. We try and remove those constraints of
3196 * this form that are most obviously redundant with respect to
3197 * the context. We also remove those div constraints that are
3198 * redundant with respect to the other constraints in the result.
3200 * The stride constraints among the equality constraints in "bmap" are
3201 * also simplified with respecting to the other equality constraints
3202 * in "bmap" and with respect to all equality constraints in "context".
3204 struct isl_basic_map
*isl_basic_map_gist(struct isl_basic_map
*bmap
,
3205 struct isl_basic_map
*context
)
3207 isl_basic_set
*bset
, *eq
;
3208 isl_basic_map
*eq_bmap
;
3209 unsigned total
, n_div
, extra
, n_eq
, n_ineq
;
3211 if (!bmap
|| !context
)
3214 if (isl_basic_map_plain_is_universe(bmap
)) {
3215 isl_basic_map_free(context
);
3218 if (isl_basic_map_plain_is_empty(context
)) {
3219 isl_space
*space
= isl_basic_map_get_space(bmap
);
3220 isl_basic_map_free(bmap
);
3221 isl_basic_map_free(context
);
3222 return isl_basic_map_universe(space
);
3224 if (isl_basic_map_plain_is_empty(bmap
)) {
3225 isl_basic_map_free(context
);
3229 bmap
= isl_basic_map_remove_redundancies(bmap
);
3230 context
= isl_basic_map_remove_redundancies(context
);
3234 context
= isl_basic_map_align_divs(context
, bmap
);
3235 n_div
= isl_basic_map_dim(context
, isl_dim_div
);
3236 total
= isl_basic_map_dim(bmap
, isl_dim_all
);
3237 extra
= n_div
- isl_basic_map_dim(bmap
, isl_dim_div
);
3239 bset
= isl_basic_map_underlying_set(isl_basic_map_copy(bmap
));
3240 bset
= isl_basic_set_add_dims(bset
, isl_dim_set
, extra
);
3241 bset
= uset_gist(bset
,
3242 isl_basic_map_underlying_set(isl_basic_map_copy(context
)));
3243 bset
= isl_basic_set_project_out(bset
, isl_dim_set
, total
, extra
);
3245 if (!bset
|| bset
->n_eq
== 0 || n_div
== 0 ||
3246 isl_basic_set_plain_is_empty(bset
)) {
3247 isl_basic_map_free(context
);
3248 return isl_basic_map_overlying_set(bset
, bmap
);
3252 n_ineq
= bset
->n_ineq
;
3253 eq
= isl_basic_set_copy(bset
);
3254 eq
= isl_basic_set_cow(eq
);
3255 if (isl_basic_set_free_inequality(eq
, n_ineq
) < 0)
3256 eq
= isl_basic_set_free(eq
);
3257 if (isl_basic_set_free_equality(bset
, n_eq
) < 0)
3258 bset
= isl_basic_set_free(bset
);
3260 eq_bmap
= isl_basic_map_overlying_set(eq
, isl_basic_map_copy(bmap
));
3261 eq_bmap
= gist_strides(eq_bmap
, context
);
3262 eq_bmap
= isl_basic_map_remove_shifted_constraints(eq_bmap
, context
);
3263 bmap
= isl_basic_map_overlying_set(bset
, bmap
);
3264 bmap
= isl_basic_map_intersect(bmap
, eq_bmap
);
3265 bmap
= isl_basic_map_remove_redundancies(bmap
);
3269 isl_basic_map_free(bmap
);
3270 isl_basic_map_free(context
);
3275 * Assumes context has no implicit divs.
3277 __isl_give isl_map
*isl_map_gist_basic_map(__isl_take isl_map
*map
,
3278 __isl_take isl_basic_map
*context
)
3282 if (!map
|| !context
)
3285 if (isl_basic_map_plain_is_empty(context
)) {
3286 isl_space
*space
= isl_map_get_space(map
);
3288 isl_basic_map_free(context
);
3289 return isl_map_universe(space
);
3292 context
= isl_basic_map_remove_redundancies(context
);
3293 map
= isl_map_cow(map
);
3294 if (!map
|| !context
)
3296 isl_assert(map
->ctx
, isl_space_is_equal(map
->dim
, context
->dim
), goto error
);
3297 map
= isl_map_compute_divs(map
);
3300 for (i
= map
->n
- 1; i
>= 0; --i
) {
3301 map
->p
[i
] = isl_basic_map_gist(map
->p
[i
],
3302 isl_basic_map_copy(context
));
3305 if (isl_basic_map_plain_is_empty(map
->p
[i
])) {
3306 isl_basic_map_free(map
->p
[i
]);
3307 if (i
!= map
->n
- 1)
3308 map
->p
[i
] = map
->p
[map
->n
- 1];
3312 isl_basic_map_free(context
);
3313 ISL_F_CLR(map
, ISL_MAP_NORMALIZED
);
3317 isl_basic_map_free(context
);
3321 /* Drop all inequalities from "bmap" that also appear in "context".
3322 * "context" is assumed to have only known local variables and
3323 * the initial local variables of "bmap" are assumed to be the same
3324 * as those of "context".
3325 * The constraints of both "bmap" and "context" are assumed
3326 * to have been sorted using isl_basic_map_sort_constraints.
3328 * Run through the inequality constraints of "bmap" and "context"
3330 * If a constraint of "bmap" involves variables not in "context",
3331 * then it cannot appear in "context".
3332 * If a matching constraint is found, it is removed from "bmap".
3334 static __isl_give isl_basic_map
*drop_inequalities(
3335 __isl_take isl_basic_map
*bmap
, __isl_keep isl_basic_map
*context
)
3338 unsigned total
, extra
;
3340 if (!bmap
|| !context
)
3341 return isl_basic_map_free(bmap
);
3343 total
= isl_basic_map_total_dim(context
);
3344 extra
= isl_basic_map_total_dim(bmap
) - total
;
3346 i1
= bmap
->n_ineq
- 1;
3347 i2
= context
->n_ineq
- 1;
3348 while (bmap
&& i1
>= 0 && i2
>= 0) {
3351 if (isl_seq_first_non_zero(bmap
->ineq
[i1
] + 1 + total
,
3356 cmp
= isl_basic_map_constraint_cmp(context
, bmap
->ineq
[i1
],
3366 if (isl_int_eq(bmap
->ineq
[i1
][0], context
->ineq
[i2
][0])) {
3367 bmap
= isl_basic_map_cow(bmap
);
3368 if (isl_basic_map_drop_inequality(bmap
, i1
) < 0)
3369 bmap
= isl_basic_map_free(bmap
);
3378 /* Drop all equalities from "bmap" that also appear in "context".
3379 * "context" is assumed to have only known local variables and
3380 * the initial local variables of "bmap" are assumed to be the same
3381 * as those of "context".
3383 * Run through the equality constraints of "bmap" and "context"
3385 * If a constraint of "bmap" involves variables not in "context",
3386 * then it cannot appear in "context".
3387 * If a matching constraint is found, it is removed from "bmap".
3389 static __isl_give isl_basic_map
*drop_equalities(
3390 __isl_take isl_basic_map
*bmap
, __isl_keep isl_basic_map
*context
)
3393 unsigned total
, extra
;
3395 if (!bmap
|| !context
)
3396 return isl_basic_map_free(bmap
);
3398 total
= isl_basic_map_total_dim(context
);
3399 extra
= isl_basic_map_total_dim(bmap
) - total
;
3401 i1
= bmap
->n_eq
- 1;
3402 i2
= context
->n_eq
- 1;
3404 while (bmap
&& i1
>= 0 && i2
>= 0) {
3407 if (isl_seq_first_non_zero(bmap
->eq
[i1
] + 1 + total
,
3410 last1
= isl_seq_last_non_zero(bmap
->eq
[i1
] + 1, total
);
3411 last2
= isl_seq_last_non_zero(context
->eq
[i2
] + 1, total
);
3412 if (last1
> last2
) {
3416 if (last1
< last2
) {
3420 if (isl_seq_eq(bmap
->eq
[i1
], context
->eq
[i2
], 1 + total
)) {
3421 bmap
= isl_basic_map_cow(bmap
);
3422 if (isl_basic_map_drop_equality(bmap
, i1
) < 0)
3423 bmap
= isl_basic_map_free(bmap
);
3432 /* Remove the constraints in "context" from "bmap".
3433 * "context" is assumed to have explicit representations
3434 * for all local variables.
3436 * First align the divs of "bmap" to those of "context" and
3437 * sort the constraints. Then drop all constraints from "bmap"
3438 * that appear in "context".
3440 __isl_give isl_basic_map
*isl_basic_map_plain_gist(
3441 __isl_take isl_basic_map
*bmap
, __isl_take isl_basic_map
*context
)
3443 isl_bool done
, known
;
3445 done
= isl_basic_map_plain_is_universe(context
);
3446 if (done
== isl_bool_false
)
3447 done
= isl_basic_map_plain_is_universe(bmap
);
3448 if (done
== isl_bool_false
)
3449 done
= isl_basic_map_plain_is_empty(context
);
3450 if (done
== isl_bool_false
)
3451 done
= isl_basic_map_plain_is_empty(bmap
);
3455 isl_basic_map_free(context
);
3458 known
= isl_basic_map_divs_known(context
);
3462 isl_die(isl_basic_map_get_ctx(bmap
), isl_error_invalid
,
3463 "context has unknown divs", goto error
);
3465 bmap
= isl_basic_map_align_divs(bmap
, context
);
3466 bmap
= isl_basic_map_gauss(bmap
, NULL
);
3467 bmap
= isl_basic_map_sort_constraints(bmap
);
3468 context
= isl_basic_map_sort_constraints(context
);
3470 bmap
= drop_inequalities(bmap
, context
);
3471 bmap
= drop_equalities(bmap
, context
);
3473 isl_basic_map_free(context
);
3474 bmap
= isl_basic_map_finalize(bmap
);
3477 isl_basic_map_free(bmap
);
3478 isl_basic_map_free(context
);
3482 /* Replace "map" by the disjunct at position "pos" and free "context".
3484 static __isl_give isl_map
*replace_by_disjunct(__isl_take isl_map
*map
,
3485 int pos
, __isl_take isl_basic_map
*context
)
3487 isl_basic_map
*bmap
;
3489 bmap
= isl_basic_map_copy(map
->p
[pos
]);
3491 isl_basic_map_free(context
);
3492 return isl_map_from_basic_map(bmap
);
3495 /* Remove the constraints in "context" from "map".
3496 * If any of the disjuncts in the result turns out to be the universe,
3497 * then return this universe.
3498 * "context" is assumed to have explicit representations
3499 * for all local variables.
3501 __isl_give isl_map
*isl_map_plain_gist_basic_map(__isl_take isl_map
*map
,
3502 __isl_take isl_basic_map
*context
)
3505 isl_bool univ
, known
;
3507 univ
= isl_basic_map_plain_is_universe(context
);
3511 isl_basic_map_free(context
);
3514 known
= isl_basic_map_divs_known(context
);
3518 isl_die(isl_map_get_ctx(map
), isl_error_invalid
,
3519 "context has unknown divs", goto error
);
3521 map
= isl_map_cow(map
);
3524 for (i
= 0; i
< map
->n
; ++i
) {
3525 map
->p
[i
] = isl_basic_map_plain_gist(map
->p
[i
],
3526 isl_basic_map_copy(context
));
3527 univ
= isl_basic_map_plain_is_universe(map
->p
[i
]);
3530 if (univ
&& map
->n
> 1)
3531 return replace_by_disjunct(map
, i
, context
);
3534 isl_basic_map_free(context
);
3535 ISL_F_CLR(map
, ISL_MAP_NORMALIZED
);
3537 ISL_F_CLR(map
, ISL_MAP_DISJOINT
);
3541 isl_basic_map_free(context
);
3545 /* Replace "map" by a universe map in the same space and free "drop".
3547 static __isl_give isl_map
*replace_by_universe(__isl_take isl_map
*map
,
3548 __isl_take isl_map
*drop
)
3552 res
= isl_map_universe(isl_map_get_space(map
));
3558 /* Return a map that has the same intersection with "context" as "map"
3559 * and that is as "simple" as possible.
3561 * If "map" is already the universe, then we cannot make it any simpler.
3562 * Similarly, if "context" is the universe, then we cannot exploit it
3564 * If "map" and "context" are identical to each other, then we can
3565 * return the corresponding universe.
3567 * If either "map" or "context" consists of multiple disjuncts,
3568 * then check if "context" happens to be a subset of "map",
3569 * in which case all constraints can be removed.
3570 * In case of multiple disjuncts, the standard procedure
3571 * may not be able to detect that all constraints can be removed.
3573 * If none of these cases apply, we have to work a bit harder.
3574 * During this computation, we make use of a single disjunct context,
3575 * so if the original context consists of more than one disjunct
3576 * then we need to approximate the context by a single disjunct set.
3577 * Simply taking the simple hull may drop constraints that are
3578 * only implicitly available in each disjunct. We therefore also
3579 * look for constraints among those defining "map" that are valid
3580 * for the context. These can then be used to simplify away
3581 * the corresponding constraints in "map".
3583 static __isl_give isl_map
*map_gist(__isl_take isl_map
*map
,
3584 __isl_take isl_map
*context
)
3588 int single_disjunct_map
, single_disjunct_context
;
3590 isl_basic_map
*hull
;
3592 is_universe
= isl_map_plain_is_universe(map
);
3593 if (is_universe
>= 0 && !is_universe
)
3594 is_universe
= isl_map_plain_is_universe(context
);
3595 if (is_universe
< 0)
3598 isl_map_free(context
);
3602 equal
= isl_map_plain_is_equal(map
, context
);
3606 return replace_by_universe(map
, context
);
3608 single_disjunct_map
= isl_map_n_basic_map(map
) == 1;
3609 single_disjunct_context
= isl_map_n_basic_map(context
) == 1;
3610 if (!single_disjunct_map
|| !single_disjunct_context
) {
3611 subset
= isl_map_is_subset(context
, map
);
3615 return replace_by_universe(map
, context
);
3618 context
= isl_map_compute_divs(context
);
3621 if (single_disjunct_context
) {
3622 hull
= isl_map_simple_hull(context
);
3627 ctx
= isl_map_get_ctx(map
);
3628 list
= isl_map_list_alloc(ctx
, 2);
3629 list
= isl_map_list_add(list
, isl_map_copy(context
));
3630 list
= isl_map_list_add(list
, isl_map_copy(map
));
3631 hull
= isl_map_unshifted_simple_hull_from_map_list(context
,
3634 return isl_map_gist_basic_map(map
, hull
);
3637 isl_map_free(context
);
3641 __isl_give isl_map
*isl_map_gist(__isl_take isl_map
*map
,
3642 __isl_take isl_map
*context
)
3644 return isl_map_align_params_map_map_and(map
, context
, &map_gist
);
3647 struct isl_basic_set
*isl_basic_set_gist(struct isl_basic_set
*bset
,
3648 struct isl_basic_set
*context
)
3650 return bset_from_bmap(isl_basic_map_gist(bset_to_bmap(bset
),
3651 bset_to_bmap(context
)));
3654 __isl_give isl_set
*isl_set_gist_basic_set(__isl_take isl_set
*set
,
3655 __isl_take isl_basic_set
*context
)
3657 return set_from_map(isl_map_gist_basic_map(set_to_map(set
),
3658 bset_to_bmap(context
)));
3661 __isl_give isl_set
*isl_set_gist_params_basic_set(__isl_take isl_set
*set
,
3662 __isl_take isl_basic_set
*context
)
3664 isl_space
*space
= isl_set_get_space(set
);
3665 isl_basic_set
*dom_context
= isl_basic_set_universe(space
);
3666 dom_context
= isl_basic_set_intersect_params(dom_context
, context
);
3667 return isl_set_gist_basic_set(set
, dom_context
);
3670 __isl_give isl_set
*isl_set_gist(__isl_take isl_set
*set
,
3671 __isl_take isl_set
*context
)
3673 return set_from_map(isl_map_gist(set_to_map(set
), set_to_map(context
)));
3676 /* Compute the gist of "bmap" with respect to the constraints "context"
3679 __isl_give isl_basic_map
*isl_basic_map_gist_domain(
3680 __isl_take isl_basic_map
*bmap
, __isl_take isl_basic_set
*context
)
3682 isl_space
*space
= isl_basic_map_get_space(bmap
);
3683 isl_basic_map
*bmap_context
= isl_basic_map_universe(space
);
3685 bmap_context
= isl_basic_map_intersect_domain(bmap_context
, context
);
3686 return isl_basic_map_gist(bmap
, bmap_context
);
3689 __isl_give isl_map
*isl_map_gist_domain(__isl_take isl_map
*map
,
3690 __isl_take isl_set
*context
)
3692 isl_map
*map_context
= isl_map_universe(isl_map_get_space(map
));
3693 map_context
= isl_map_intersect_domain(map_context
, context
);
3694 return isl_map_gist(map
, map_context
);
3697 __isl_give isl_map
*isl_map_gist_range(__isl_take isl_map
*map
,
3698 __isl_take isl_set
*context
)
3700 isl_map
*map_context
= isl_map_universe(isl_map_get_space(map
));
3701 map_context
= isl_map_intersect_range(map_context
, context
);
3702 return isl_map_gist(map
, map_context
);
3705 __isl_give isl_map
*isl_map_gist_params(__isl_take isl_map
*map
,
3706 __isl_take isl_set
*context
)
3708 isl_map
*map_context
= isl_map_universe(isl_map_get_space(map
));
3709 map_context
= isl_map_intersect_params(map_context
, context
);
3710 return isl_map_gist(map
, map_context
);
3713 __isl_give isl_set
*isl_set_gist_params(__isl_take isl_set
*set
,
3714 __isl_take isl_set
*context
)
3716 return isl_map_gist_params(set
, context
);
3719 /* Quick check to see if two basic maps are disjoint.
3720 * In particular, we reduce the equalities and inequalities of
3721 * one basic map in the context of the equalities of the other
3722 * basic map and check if we get a contradiction.
3724 isl_bool
isl_basic_map_plain_is_disjoint(__isl_keep isl_basic_map
*bmap1
,
3725 __isl_keep isl_basic_map
*bmap2
)
3727 struct isl_vec
*v
= NULL
;
3732 if (!bmap1
|| !bmap2
)
3733 return isl_bool_error
;
3734 isl_assert(bmap1
->ctx
, isl_space_is_equal(bmap1
->dim
, bmap2
->dim
),
3735 return isl_bool_error
);
3736 if (bmap1
->n_div
|| bmap2
->n_div
)
3737 return isl_bool_false
;
3738 if (!bmap1
->n_eq
&& !bmap2
->n_eq
)
3739 return isl_bool_false
;
3741 total
= isl_space_dim(bmap1
->dim
, isl_dim_all
);
3743 return isl_bool_false
;
3744 v
= isl_vec_alloc(bmap1
->ctx
, 1 + total
);
3747 elim
= isl_alloc_array(bmap1
->ctx
, int, total
);
3750 compute_elimination_index(bmap1
, elim
);
3751 for (i
= 0; i
< bmap2
->n_eq
; ++i
) {
3753 reduced
= reduced_using_equalities(v
->block
.data
, bmap2
->eq
[i
],
3755 if (reduced
&& !isl_int_is_zero(v
->block
.data
[0]) &&
3756 isl_seq_first_non_zero(v
->block
.data
+ 1, total
) == -1)
3759 for (i
= 0; i
< bmap2
->n_ineq
; ++i
) {
3761 reduced
= reduced_using_equalities(v
->block
.data
,
3762 bmap2
->ineq
[i
], bmap1
, elim
);
3763 if (reduced
&& isl_int_is_neg(v
->block
.data
[0]) &&
3764 isl_seq_first_non_zero(v
->block
.data
+ 1, total
) == -1)
3767 compute_elimination_index(bmap2
, elim
);
3768 for (i
= 0; i
< bmap1
->n_ineq
; ++i
) {
3770 reduced
= reduced_using_equalities(v
->block
.data
,
3771 bmap1
->ineq
[i
], bmap2
, elim
);
3772 if (reduced
&& isl_int_is_neg(v
->block
.data
[0]) &&
3773 isl_seq_first_non_zero(v
->block
.data
+ 1, total
) == -1)
3778 return isl_bool_false
;
3782 return isl_bool_true
;
3786 return isl_bool_error
;
3789 int isl_basic_set_plain_is_disjoint(__isl_keep isl_basic_set
*bset1
,
3790 __isl_keep isl_basic_set
*bset2
)
3792 return isl_basic_map_plain_is_disjoint(bset_to_bmap(bset1
),
3793 bset_to_bmap(bset2
));
3796 /* Does "test" hold for all pairs of basic maps in "map1" and "map2"?
3798 static isl_bool
all_pairs(__isl_keep isl_map
*map1
, __isl_keep isl_map
*map2
,
3799 isl_bool (*test
)(__isl_keep isl_basic_map
*bmap1
,
3800 __isl_keep isl_basic_map
*bmap2
))
3805 return isl_bool_error
;
3807 for (i
= 0; i
< map1
->n
; ++i
) {
3808 for (j
= 0; j
< map2
->n
; ++j
) {
3809 isl_bool d
= test(map1
->p
[i
], map2
->p
[j
]);
3810 if (d
!= isl_bool_true
)
3815 return isl_bool_true
;
3818 /* Are "map1" and "map2" obviously disjoint, based on information
3819 * that can be derived without looking at the individual basic maps?
3821 * In particular, if one of them is empty or if they live in different spaces
3822 * (ignoring parameters), then they are clearly disjoint.
3824 static isl_bool
isl_map_plain_is_disjoint_global(__isl_keep isl_map
*map1
,
3825 __isl_keep isl_map
*map2
)
3831 return isl_bool_error
;
3833 disjoint
= isl_map_plain_is_empty(map1
);
3834 if (disjoint
< 0 || disjoint
)
3837 disjoint
= isl_map_plain_is_empty(map2
);
3838 if (disjoint
< 0 || disjoint
)
3841 match
= isl_space_tuple_is_equal(map1
->dim
, isl_dim_in
,
3842 map2
->dim
, isl_dim_in
);
3843 if (match
< 0 || !match
)
3844 return match
< 0 ? isl_bool_error
: isl_bool_true
;
3846 match
= isl_space_tuple_is_equal(map1
->dim
, isl_dim_out
,
3847 map2
->dim
, isl_dim_out
);
3848 if (match
< 0 || !match
)
3849 return match
< 0 ? isl_bool_error
: isl_bool_true
;
3851 return isl_bool_false
;
3854 /* Are "map1" and "map2" obviously disjoint?
3856 * If one of them is empty or if they live in different spaces (ignoring
3857 * parameters), then they are clearly disjoint.
3858 * This is checked by isl_map_plain_is_disjoint_global.
3860 * If they have different parameters, then we skip any further tests.
3862 * If they are obviously equal, but not obviously empty, then we will
3863 * not be able to detect if they are disjoint.
3865 * Otherwise we check if each basic map in "map1" is obviously disjoint
3866 * from each basic map in "map2".
3868 isl_bool
isl_map_plain_is_disjoint(__isl_keep isl_map
*map1
,
3869 __isl_keep isl_map
*map2
)
3875 disjoint
= isl_map_plain_is_disjoint_global(map1
, map2
);
3876 if (disjoint
< 0 || disjoint
)
3879 match
= isl_space_match(map1
->dim
, isl_dim_param
,
3880 map2
->dim
, isl_dim_param
);
3881 if (match
< 0 || !match
)
3882 return match
< 0 ? isl_bool_error
: isl_bool_false
;
3884 intersect
= isl_map_plain_is_equal(map1
, map2
);
3885 if (intersect
< 0 || intersect
)
3886 return intersect
< 0 ? isl_bool_error
: isl_bool_false
;
3888 return all_pairs(map1
, map2
, &isl_basic_map_plain_is_disjoint
);
3891 /* Are "map1" and "map2" disjoint?
3893 * They are disjoint if they are "obviously disjoint" or if one of them
3894 * is empty. Otherwise, they are not disjoint if one of them is universal.
3895 * If the two inputs are (obviously) equal and not empty, then they are
3897 * If none of these cases apply, then check if all pairs of basic maps
3900 isl_bool
isl_map_is_disjoint(__isl_keep isl_map
*map1
, __isl_keep isl_map
*map2
)
3905 disjoint
= isl_map_plain_is_disjoint_global(map1
, map2
);
3906 if (disjoint
< 0 || disjoint
)
3909 disjoint
= isl_map_is_empty(map1
);
3910 if (disjoint
< 0 || disjoint
)
3913 disjoint
= isl_map_is_empty(map2
);
3914 if (disjoint
< 0 || disjoint
)
3917 intersect
= isl_map_plain_is_universe(map1
);
3918 if (intersect
< 0 || intersect
)
3919 return intersect
< 0 ? isl_bool_error
: isl_bool_false
;
3921 intersect
= isl_map_plain_is_universe(map2
);
3922 if (intersect
< 0 || intersect
)
3923 return intersect
< 0 ? isl_bool_error
: isl_bool_false
;
3925 intersect
= isl_map_plain_is_equal(map1
, map2
);
3926 if (intersect
< 0 || intersect
)
3927 return isl_bool_not(intersect
);
3929 return all_pairs(map1
, map2
, &isl_basic_map_is_disjoint
);
3932 /* Are "bmap1" and "bmap2" disjoint?
3934 * They are disjoint if they are "obviously disjoint" or if one of them
3935 * is empty. Otherwise, they are not disjoint if one of them is universal.
3936 * If none of these cases apply, we compute the intersection and see if
3937 * the result is empty.
3939 isl_bool
isl_basic_map_is_disjoint(__isl_keep isl_basic_map
*bmap1
,
3940 __isl_keep isl_basic_map
*bmap2
)
3944 isl_basic_map
*test
;
3946 disjoint
= isl_basic_map_plain_is_disjoint(bmap1
, bmap2
);
3947 if (disjoint
< 0 || disjoint
)
3950 disjoint
= isl_basic_map_is_empty(bmap1
);
3951 if (disjoint
< 0 || disjoint
)
3954 disjoint
= isl_basic_map_is_empty(bmap2
);
3955 if (disjoint
< 0 || disjoint
)
3958 intersect
= isl_basic_map_plain_is_universe(bmap1
);
3959 if (intersect
< 0 || intersect
)
3960 return intersect
< 0 ? isl_bool_error
: isl_bool_false
;
3962 intersect
= isl_basic_map_plain_is_universe(bmap2
);
3963 if (intersect
< 0 || intersect
)
3964 return intersect
< 0 ? isl_bool_error
: isl_bool_false
;
3966 test
= isl_basic_map_intersect(isl_basic_map_copy(bmap1
),
3967 isl_basic_map_copy(bmap2
));
3968 disjoint
= isl_basic_map_is_empty(test
);
3969 isl_basic_map_free(test
);
3974 /* Are "bset1" and "bset2" disjoint?
3976 isl_bool
isl_basic_set_is_disjoint(__isl_keep isl_basic_set
*bset1
,
3977 __isl_keep isl_basic_set
*bset2
)
3979 return isl_basic_map_is_disjoint(bset1
, bset2
);
3982 isl_bool
isl_set_plain_is_disjoint(__isl_keep isl_set
*set1
,
3983 __isl_keep isl_set
*set2
)
3985 return isl_map_plain_is_disjoint(set_to_map(set1
), set_to_map(set2
));
3988 /* Are "set1" and "set2" disjoint?
3990 isl_bool
isl_set_is_disjoint(__isl_keep isl_set
*set1
, __isl_keep isl_set
*set2
)
3992 return isl_map_is_disjoint(set1
, set2
);
3995 /* Is "v" equal to 0, 1 or -1?
3997 static int is_zero_or_one(isl_int v
)
3999 return isl_int_is_zero(v
) || isl_int_is_one(v
) || isl_int_is_negone(v
);
4002 /* Check if we can combine a given div with lower bound l and upper
4003 * bound u with some other div and if so return that other div.
4004 * Otherwise return -1.
4006 * We first check that
4007 * - the bounds are opposites of each other (except for the constant
4009 * - the bounds do not reference any other div
4010 * - no div is defined in terms of this div
4012 * Let m be the size of the range allowed on the div by the bounds.
4013 * That is, the bounds are of the form
4015 * e <= a <= e + m - 1
4017 * with e some expression in the other variables.
4018 * We look for another div b such that no third div is defined in terms
4019 * of this second div b and such that in any constraint that contains
4020 * a (except for the given lower and upper bound), also contains b
4021 * with a coefficient that is m times that of b.
4022 * That is, all constraints (execpt for the lower and upper bound)
4025 * e + f (a + m b) >= 0
4027 * Furthermore, in the constraints that only contain b, the coefficient
4028 * of b should be equal to 1 or -1.
4029 * If so, we return b so that "a + m b" can be replaced by
4030 * a single div "c = a + m b".
4032 static int div_find_coalesce(struct isl_basic_map
*bmap
, int *pairs
,
4033 unsigned div
, unsigned l
, unsigned u
)
4039 if (bmap
->n_div
<= 1)
4041 dim
= isl_space_dim(bmap
->dim
, isl_dim_all
);
4042 if (isl_seq_first_non_zero(bmap
->ineq
[l
] + 1 + dim
, div
) != -1)
4044 if (isl_seq_first_non_zero(bmap
->ineq
[l
] + 1 + dim
+ div
+ 1,
4045 bmap
->n_div
- div
- 1) != -1)
4047 if (!isl_seq_is_neg(bmap
->ineq
[l
] + 1, bmap
->ineq
[u
] + 1,
4051 for (i
= 0; i
< bmap
->n_div
; ++i
) {
4052 if (isl_int_is_zero(bmap
->div
[i
][0]))
4054 if (!isl_int_is_zero(bmap
->div
[i
][1 + 1 + dim
+ div
]))
4058 isl_int_add(bmap
->ineq
[l
][0], bmap
->ineq
[l
][0], bmap
->ineq
[u
][0]);
4059 if (isl_int_is_neg(bmap
->ineq
[l
][0])) {
4060 isl_int_sub(bmap
->ineq
[l
][0],
4061 bmap
->ineq
[l
][0], bmap
->ineq
[u
][0]);
4062 bmap
= isl_basic_map_copy(bmap
);
4063 bmap
= isl_basic_map_set_to_empty(bmap
);
4064 isl_basic_map_free(bmap
);
4067 isl_int_add_ui(bmap
->ineq
[l
][0], bmap
->ineq
[l
][0], 1);
4068 for (i
= 0; i
< bmap
->n_div
; ++i
) {
4073 for (j
= 0; j
< bmap
->n_div
; ++j
) {
4074 if (isl_int_is_zero(bmap
->div
[j
][0]))
4076 if (!isl_int_is_zero(bmap
->div
[j
][1 + 1 + dim
+ i
]))
4079 if (j
< bmap
->n_div
)
4081 for (j
= 0; j
< bmap
->n_ineq
; ++j
) {
4083 if (j
== l
|| j
== u
)
4085 if (isl_int_is_zero(bmap
->ineq
[j
][1 + dim
+ div
])) {
4086 if (is_zero_or_one(bmap
->ineq
[j
][1 + dim
+ i
]))
4090 if (isl_int_is_zero(bmap
->ineq
[j
][1 + dim
+ i
]))
4092 isl_int_mul(bmap
->ineq
[j
][1 + dim
+ div
],
4093 bmap
->ineq
[j
][1 + dim
+ div
],
4095 valid
= isl_int_eq(bmap
->ineq
[j
][1 + dim
+ div
],
4096 bmap
->ineq
[j
][1 + dim
+ i
]);
4097 isl_int_divexact(bmap
->ineq
[j
][1 + dim
+ div
],
4098 bmap
->ineq
[j
][1 + dim
+ div
],
4103 if (j
< bmap
->n_ineq
)
4108 isl_int_sub_ui(bmap
->ineq
[l
][0], bmap
->ineq
[l
][0], 1);
4109 isl_int_sub(bmap
->ineq
[l
][0], bmap
->ineq
[l
][0], bmap
->ineq
[u
][0]);
4113 /* Internal data structure used during the construction and/or evaluation of
4114 * an inequality that ensures that a pair of bounds always allows
4115 * for an integer value.
4117 * "tab" is the tableau in which the inequality is evaluated. It may
4118 * be NULL until it is actually needed.
4119 * "v" contains the inequality coefficients.
4120 * "g", "fl" and "fu" are temporary scalars used during the construction and
4123 struct test_ineq_data
{
4124 struct isl_tab
*tab
;
4131 /* Free all the memory allocated by the fields of "data".
4133 static void test_ineq_data_clear(struct test_ineq_data
*data
)
4135 isl_tab_free(data
->tab
);
4136 isl_vec_free(data
->v
);
4137 isl_int_clear(data
->g
);
4138 isl_int_clear(data
->fl
);
4139 isl_int_clear(data
->fu
);
4142 /* Is the inequality stored in data->v satisfied by "bmap"?
4143 * That is, does it only attain non-negative values?
4144 * data->tab is a tableau corresponding to "bmap".
4146 static isl_bool
test_ineq_is_satisfied(__isl_keep isl_basic_map
*bmap
,
4147 struct test_ineq_data
*data
)
4150 enum isl_lp_result res
;
4152 ctx
= isl_basic_map_get_ctx(bmap
);
4154 data
->tab
= isl_tab_from_basic_map(bmap
, 0);
4155 res
= isl_tab_min(data
->tab
, data
->v
->el
, ctx
->one
, &data
->g
, NULL
, 0);
4156 if (res
== isl_lp_error
)
4157 return isl_bool_error
;
4158 return res
== isl_lp_ok
&& isl_int_is_nonneg(data
->g
);
4161 /* Given a lower and an upper bound on div i, do they always allow
4162 * for an integer value of the given div?
4163 * Determine this property by constructing an inequality
4164 * such that the property is guaranteed when the inequality is nonnegative.
4165 * The lower bound is inequality l, while the upper bound is inequality u.
4166 * The constructed inequality is stored in data->v.
4168 * Let the upper bound be
4172 * and the lower bound
4176 * Let n_u = f_u g and n_l = f_l g, with g = gcd(n_u, n_l).
4179 * - f_u e_l <= f_u f_l g a <= f_l e_u
4181 * Since all variables are integer valued, this is equivalent to
4183 * - f_u e_l - (f_u - 1) <= f_u f_l g a <= f_l e_u + (f_l - 1)
4185 * If this interval is at least f_u f_l g, then it contains at least
4186 * one integer value for a.
4187 * That is, the test constraint is
4189 * f_l e_u + f_u e_l + f_l - 1 + f_u - 1 + 1 >= f_u f_l g
4193 * f_l e_u + f_u e_l + f_l - 1 + f_u - 1 + 1 - f_u f_l g >= 0
4195 * If the coefficients of f_l e_u + f_u e_l have a common divisor g',
4196 * then the constraint can be scaled down by a factor g',
4197 * with the constant term replaced by
4198 * floor((f_l e_{u,0} + f_u e_{l,0} + f_l - 1 + f_u - 1 + 1 - f_u f_l g)/g').
4199 * Note that the result of applying Fourier-Motzkin to this pair
4202 * f_l e_u + f_u e_l >= 0
4204 * If the constant term of the scaled down version of this constraint,
4205 * i.e., floor((f_l e_{u,0} + f_u e_{l,0})/g') is equal to the constant
4206 * term of the scaled down test constraint, then the test constraint
4207 * is known to hold and no explicit evaluation is required.
4208 * This is essentially the Omega test.
4210 * If the test constraint consists of only a constant term, then
4211 * it is sufficient to look at the sign of this constant term.
4213 static isl_bool
int_between_bounds(__isl_keep isl_basic_map
*bmap
, int i
,
4214 int l
, int u
, struct test_ineq_data
*data
)
4216 unsigned offset
, n_div
;
4217 offset
= isl_basic_map_offset(bmap
, isl_dim_div
);
4218 n_div
= isl_basic_map_dim(bmap
, isl_dim_div
);
4220 isl_int_gcd(data
->g
,
4221 bmap
->ineq
[l
][offset
+ i
], bmap
->ineq
[u
][offset
+ i
]);
4222 isl_int_divexact(data
->fl
, bmap
->ineq
[l
][offset
+ i
], data
->g
);
4223 isl_int_divexact(data
->fu
, bmap
->ineq
[u
][offset
+ i
], data
->g
);
4224 isl_int_neg(data
->fu
, data
->fu
);
4225 isl_seq_combine(data
->v
->el
, data
->fl
, bmap
->ineq
[u
],
4226 data
->fu
, bmap
->ineq
[l
], offset
+ n_div
);
4227 isl_int_mul(data
->g
, data
->g
, data
->fl
);
4228 isl_int_mul(data
->g
, data
->g
, data
->fu
);
4229 isl_int_sub(data
->g
, data
->g
, data
->fl
);
4230 isl_int_sub(data
->g
, data
->g
, data
->fu
);
4231 isl_int_add_ui(data
->g
, data
->g
, 1);
4232 isl_int_sub(data
->fl
, data
->v
->el
[0], data
->g
);
4234 isl_seq_gcd(data
->v
->el
+ 1, offset
- 1 + n_div
, &data
->g
);
4235 if (isl_int_is_zero(data
->g
))
4236 return isl_int_is_nonneg(data
->fl
);
4237 if (isl_int_is_one(data
->g
)) {
4238 isl_int_set(data
->v
->el
[0], data
->fl
);
4239 return test_ineq_is_satisfied(bmap
, data
);
4241 isl_int_fdiv_q(data
->fl
, data
->fl
, data
->g
);
4242 isl_int_fdiv_q(data
->v
->el
[0], data
->v
->el
[0], data
->g
);
4243 if (isl_int_eq(data
->fl
, data
->v
->el
[0]))
4244 return isl_bool_true
;
4245 isl_int_set(data
->v
->el
[0], data
->fl
);
4246 isl_seq_scale_down(data
->v
->el
+ 1, data
->v
->el
+ 1, data
->g
,
4247 offset
- 1 + n_div
);
4249 return test_ineq_is_satisfied(bmap
, data
);
4252 /* Remove more kinds of divs that are not strictly needed.
4253 * In particular, if all pairs of lower and upper bounds on a div
4254 * are such that they allow at least one integer value of the div,
4255 * then we can eliminate the div using Fourier-Motzkin without
4256 * introducing any spurious solutions.
4258 * If at least one of the two constraints has a unit coefficient for the div,
4259 * then the presence of such a value is guaranteed so there is no need to check.
4260 * In particular, the value attained by the bound with unit coefficient
4261 * can serve as this intermediate value.
4263 static struct isl_basic_map
*drop_more_redundant_divs(
4264 struct isl_basic_map
*bmap
, int *pairs
, int n
)
4267 struct test_ineq_data data
= { NULL
, NULL
};
4268 unsigned off
, n_div
;
4271 isl_int_init(data
.g
);
4272 isl_int_init(data
.fl
);
4273 isl_int_init(data
.fu
);
4278 ctx
= isl_basic_map_get_ctx(bmap
);
4279 off
= isl_basic_map_offset(bmap
, isl_dim_div
);
4280 n_div
= isl_basic_map_dim(bmap
, isl_dim_div
);
4281 data
.v
= isl_vec_alloc(ctx
, off
+ n_div
);
4290 for (i
= 0; i
< n_div
; ++i
) {
4293 if (best
>= 0 && pairs
[best
] <= pairs
[i
])
4299 for (l
= 0; l
< bmap
->n_ineq
; ++l
) {
4300 if (!isl_int_is_pos(bmap
->ineq
[l
][off
+ i
]))
4302 if (isl_int_is_one(bmap
->ineq
[l
][off
+ i
]))
4304 for (u
= 0; u
< bmap
->n_ineq
; ++u
) {
4305 if (!isl_int_is_neg(bmap
->ineq
[u
][off
+ i
]))
4307 if (isl_int_is_negone(bmap
->ineq
[u
][off
+ i
]))
4309 has_int
= int_between_bounds(bmap
, i
, l
, u
,
4313 if (data
.tab
&& data
.tab
->empty
)
4318 if (u
< bmap
->n_ineq
)
4321 if (data
.tab
&& data
.tab
->empty
) {
4322 bmap
= isl_basic_map_set_to_empty(bmap
);
4325 if (l
== bmap
->n_ineq
) {
4333 test_ineq_data_clear(&data
);
4340 bmap
= isl_basic_map_remove_dims(bmap
, isl_dim_div
, remove
, 1);
4341 return isl_basic_map_drop_redundant_divs(bmap
);
4344 isl_basic_map_free(bmap
);
4345 test_ineq_data_clear(&data
);
4349 /* Given a pair of divs div1 and div2 such that, except for the lower bound l
4350 * and the upper bound u, div1 always occurs together with div2 in the form
4351 * (div1 + m div2), where m is the constant range on the variable div1
4352 * allowed by l and u, replace the pair div1 and div2 by a single
4353 * div that is equal to div1 + m div2.
4355 * The new div will appear in the location that contains div2.
4356 * We need to modify all constraints that contain
4357 * div2 = (div - div1) / m
4358 * The coefficient of div2 is known to be equal to 1 or -1.
4359 * (If a constraint does not contain div2, it will also not contain div1.)
4360 * If the constraint also contains div1, then we know they appear
4361 * as f (div1 + m div2) and we can simply replace (div1 + m div2) by div,
4362 * i.e., the coefficient of div is f.
4364 * Otherwise, we first need to introduce div1 into the constraint.
4373 * A lower bound on div2
4377 * can be replaced by
4379 * m div2 + div1 + m t + f >= 0
4385 * can be replaced by
4387 * -(m div2 + div1) + m t + f' >= 0
4389 * These constraint are those that we would obtain from eliminating
4390 * div1 using Fourier-Motzkin.
4392 * After all constraints have been modified, we drop the lower and upper
4393 * bound and then drop div1.
4395 static struct isl_basic_map
*coalesce_divs(struct isl_basic_map
*bmap
,
4396 unsigned div1
, unsigned div2
, unsigned l
, unsigned u
)
4400 unsigned dim
, total
;
4403 ctx
= isl_basic_map_get_ctx(bmap
);
4405 dim
= isl_space_dim(bmap
->dim
, isl_dim_all
);
4406 total
= 1 + dim
+ bmap
->n_div
;
4409 isl_int_add(m
, bmap
->ineq
[l
][0], bmap
->ineq
[u
][0]);
4410 isl_int_add_ui(m
, m
, 1);
4412 for (i
= 0; i
< bmap
->n_ineq
; ++i
) {
4413 if (i
== l
|| i
== u
)
4415 if (isl_int_is_zero(bmap
->ineq
[i
][1 + dim
+ div2
]))
4417 if (isl_int_is_zero(bmap
->ineq
[i
][1 + dim
+ div1
])) {
4418 if (isl_int_is_pos(bmap
->ineq
[i
][1 + dim
+ div2
]))
4419 isl_seq_combine(bmap
->ineq
[i
], m
, bmap
->ineq
[i
],
4420 ctx
->one
, bmap
->ineq
[l
], total
);
4422 isl_seq_combine(bmap
->ineq
[i
], m
, bmap
->ineq
[i
],
4423 ctx
->one
, bmap
->ineq
[u
], total
);
4425 isl_int_set(bmap
->ineq
[i
][1 + dim
+ div2
],
4426 bmap
->ineq
[i
][1 + dim
+ div1
]);
4427 isl_int_set_si(bmap
->ineq
[i
][1 + dim
+ div1
], 0);
4432 isl_basic_map_drop_inequality(bmap
, l
);
4433 isl_basic_map_drop_inequality(bmap
, u
);
4435 isl_basic_map_drop_inequality(bmap
, u
);
4436 isl_basic_map_drop_inequality(bmap
, l
);
4438 bmap
= isl_basic_map_drop_div(bmap
, div1
);
4442 /* First check if we can coalesce any pair of divs and
4443 * then continue with dropping more redundant divs.
4445 * We loop over all pairs of lower and upper bounds on a div
4446 * with coefficient 1 and -1, respectively, check if there
4447 * is any other div "c" with which we can coalesce the div
4448 * and if so, perform the coalescing.
4450 static struct isl_basic_map
*coalesce_or_drop_more_redundant_divs(
4451 struct isl_basic_map
*bmap
, int *pairs
, int n
)
4456 dim
= isl_space_dim(bmap
->dim
, isl_dim_all
);
4458 for (i
= 0; i
< bmap
->n_div
; ++i
) {
4461 for (l
= 0; l
< bmap
->n_ineq
; ++l
) {
4462 if (!isl_int_is_one(bmap
->ineq
[l
][1 + dim
+ i
]))
4464 for (u
= 0; u
< bmap
->n_ineq
; ++u
) {
4467 if (!isl_int_is_negone(bmap
->ineq
[u
][1+dim
+i
]))
4469 c
= div_find_coalesce(bmap
, pairs
, i
, l
, u
);
4473 bmap
= coalesce_divs(bmap
, i
, c
, l
, u
);
4474 return isl_basic_map_drop_redundant_divs(bmap
);
4479 if (ISL_F_ISSET(bmap
, ISL_BASIC_MAP_EMPTY
))
4482 return drop_more_redundant_divs(bmap
, pairs
, n
);
4485 /* Are the "n" coefficients starting at "first" of inequality constraints
4486 * "i" and "j" of "bmap" equal to each other?
4488 static int is_parallel_part(__isl_keep isl_basic_map
*bmap
, int i
, int j
,
4491 return isl_seq_eq(bmap
->ineq
[i
] + first
, bmap
->ineq
[j
] + first
, n
);
4494 /* Are the "n" coefficients starting at "first" of inequality constraints
4495 * "i" and "j" of "bmap" opposite to each other?
4497 static int is_opposite_part(__isl_keep isl_basic_map
*bmap
, int i
, int j
,
4500 return isl_seq_is_neg(bmap
->ineq
[i
] + first
, bmap
->ineq
[j
] + first
, n
);
4503 /* Are inequality constraints "i" and "j" of "bmap" opposite to each other,
4504 * apart from the constant term?
4506 static int is_opposite(__isl_keep isl_basic_map
*bmap
, int i
, int j
)
4510 total
= isl_basic_map_dim(bmap
, isl_dim_all
);
4511 return is_opposite_part(bmap
, i
, j
, 1, total
);
4514 /* Are inequality constraints "i" and "j" of "bmap" equal to each other,
4515 * apart from the constant term and the coefficient at position "pos"?
4517 static int is_parallel_except(__isl_keep isl_basic_map
*bmap
, int i
, int j
,
4522 total
= isl_basic_map_dim(bmap
, isl_dim_all
);
4523 return is_parallel_part(bmap
, i
, j
, 1, pos
- 1) &&
4524 is_parallel_part(bmap
, i
, j
, pos
+ 1, total
- pos
);
4527 /* Are inequality constraints "i" and "j" of "bmap" opposite to each other,
4528 * apart from the constant term and the coefficient at position "pos"?
4530 static int is_opposite_except(__isl_keep isl_basic_map
*bmap
, int i
, int j
,
4535 total
= isl_basic_map_dim(bmap
, isl_dim_all
);
4536 return is_opposite_part(bmap
, i
, j
, 1, pos
- 1) &&
4537 is_opposite_part(bmap
, i
, j
, pos
+ 1, total
- pos
);
4540 /* Restart isl_basic_map_drop_redundant_divs after "bmap" has
4541 * been modified, simplying it if "simplify" is set.
4542 * Free the temporary data structure "pairs" that was associated
4543 * to the old version of "bmap".
4545 static __isl_give isl_basic_map
*drop_redundant_divs_again(
4546 __isl_take isl_basic_map
*bmap
, __isl_take
int *pairs
, int simplify
)
4549 bmap
= isl_basic_map_simplify(bmap
);
4551 return isl_basic_map_drop_redundant_divs(bmap
);
4554 /* Is "div" the single unknown existentially quantified variable
4555 * in inequality constraint "ineq" of "bmap"?
4556 * "div" is known to have a non-zero coefficient in "ineq".
4558 static int single_unknown(__isl_keep isl_basic_map
*bmap
, int ineq
, int div
)
4561 unsigned n_div
, o_div
;
4563 if (isl_basic_map_div_is_known(bmap
, div
))
4565 n_div
= isl_basic_map_dim(bmap
, isl_dim_div
);
4568 o_div
= isl_basic_map_offset(bmap
, isl_dim_div
);
4569 for (i
= 0; i
< n_div
; ++i
) {
4572 if (isl_int_is_zero(bmap
->ineq
[ineq
][o_div
+ i
]))
4574 if (!isl_basic_map_div_is_known(bmap
, i
))
4581 /* Does integer division "div" have coefficient 1 in inequality constraint
4584 static int has_coef_one(__isl_keep isl_basic_map
*bmap
, int div
, int ineq
)
4588 o_div
= isl_basic_map_offset(bmap
, isl_dim_div
);
4589 if (isl_int_is_one(bmap
->ineq
[ineq
][o_div
+ div
]))
4595 /* Turn inequality constraint "ineq" of "bmap" into an equality and
4596 * then try and drop redundant divs again,
4597 * freeing the temporary data structure "pairs" that was associated
4598 * to the old version of "bmap".
4600 static __isl_give isl_basic_map
*set_eq_and_try_again(
4601 __isl_take isl_basic_map
*bmap
, int ineq
, __isl_take
int *pairs
)
4603 bmap
= isl_basic_map_cow(bmap
);
4604 isl_basic_map_inequality_to_equality(bmap
, ineq
);
4605 return drop_redundant_divs_again(bmap
, pairs
, 1);
4608 /* Drop the integer division at position "div", along with the two
4609 * inequality constraints "ineq1" and "ineq2" in which it appears
4610 * from "bmap" and then try and drop redundant divs again,
4611 * freeing the temporary data structure "pairs" that was associated
4612 * to the old version of "bmap".
4614 static __isl_give isl_basic_map
*drop_div_and_try_again(
4615 __isl_take isl_basic_map
*bmap
, int div
, int ineq1
, int ineq2
,
4616 __isl_take
int *pairs
)
4618 if (ineq1
> ineq2
) {
4619 isl_basic_map_drop_inequality(bmap
, ineq1
);
4620 isl_basic_map_drop_inequality(bmap
, ineq2
);
4622 isl_basic_map_drop_inequality(bmap
, ineq2
);
4623 isl_basic_map_drop_inequality(bmap
, ineq1
);
4625 bmap
= isl_basic_map_drop_div(bmap
, div
);
4626 return drop_redundant_divs_again(bmap
, pairs
, 0);
4629 /* Given two inequality constraints
4631 * f(x) + n d + c >= 0, (ineq)
4633 * with d the variable at position "pos", and
4635 * f(x) + c0 >= 0, (lower)
4637 * compute the maximal value of the lower bound ceil((-f(x) - c)/n)
4638 * determined by the first constraint.
4645 static void lower_bound_from_parallel(__isl_keep isl_basic_map
*bmap
,
4646 int ineq
, int lower
, int pos
, isl_int
*l
)
4648 isl_int_neg(*l
, bmap
->ineq
[ineq
][0]);
4649 isl_int_add(*l
, *l
, bmap
->ineq
[lower
][0]);
4650 isl_int_cdiv_q(*l
, *l
, bmap
->ineq
[ineq
][pos
]);
4653 /* Given two inequality constraints
4655 * f(x) + n d + c >= 0, (ineq)
4657 * with d the variable at position "pos", and
4659 * -f(x) - c0 >= 0, (upper)
4661 * compute the minimal value of the lower bound ceil((-f(x) - c)/n)
4662 * determined by the first constraint.
4669 static void lower_bound_from_opposite(__isl_keep isl_basic_map
*bmap
,
4670 int ineq
, int upper
, int pos
, isl_int
*u
)
4672 isl_int_neg(*u
, bmap
->ineq
[ineq
][0]);
4673 isl_int_sub(*u
, *u
, bmap
->ineq
[upper
][0]);
4674 isl_int_cdiv_q(*u
, *u
, bmap
->ineq
[ineq
][pos
]);
4677 /* Given a lower bound constraint "ineq" on "div" in "bmap",
4678 * does the corresponding lower bound have a fixed value in "bmap"?
4680 * In particular, "ineq" is of the form
4682 * f(x) + n d + c >= 0
4684 * with n > 0, c the constant term and
4685 * d the existentially quantified variable "div".
4686 * That is, the lower bound is
4688 * ceil((-f(x) - c)/n)
4690 * Look for a pair of constraints
4695 * i.e., -c1 <= -f(x) <= c0, that fix ceil((-f(x) - c)/n) to a constant value.
4696 * That is, check that
4698 * ceil((-c1 - c)/n) = ceil((c0 - c)/n)
4700 * If so, return the index of inequality f(x) + c0 >= 0.
4701 * Otherwise, return -1.
4703 static int lower_bound_is_cst(__isl_keep isl_basic_map
*bmap
, int div
, int ineq
)
4706 int lower
= -1, upper
= -1;
4707 unsigned o_div
, n_div
;
4711 n_div
= isl_basic_map_dim(bmap
, isl_dim_div
);
4712 o_div
= isl_basic_map_offset(bmap
, isl_dim_div
);
4713 for (i
= 0; i
< bmap
->n_ineq
&& (lower
< 0 || upper
< 0); ++i
) {
4716 if (!isl_int_is_zero(bmap
->ineq
[i
][o_div
+ div
]))
4719 is_parallel_except(bmap
, ineq
, i
, o_div
+ div
)) {
4724 is_opposite_except(bmap
, ineq
, i
, o_div
+ div
)) {
4729 if (lower
< 0 || upper
< 0)
4735 lower_bound_from_parallel(bmap
, ineq
, lower
, o_div
+ div
, &l
);
4736 lower_bound_from_opposite(bmap
, ineq
, upper
, o_div
+ div
, &u
);
4738 equal
= isl_int_eq(l
, u
);
4743 return equal
? lower
: -1;
4746 /* Given a lower bound constraint "ineq" on the existentially quantified
4747 * variable "div", such that the corresponding lower bound has
4748 * a fixed value in "bmap", assign this fixed value to the variable and
4749 * then try and drop redundant divs again,
4750 * freeing the temporary data structure "pairs" that was associated
4751 * to the old version of "bmap".
4752 * "lower" determines the constant value for the lower bound.
4754 * In particular, "ineq" is of the form
4756 * f(x) + n d + c >= 0,
4758 * while "lower" is of the form
4762 * The lower bound is ceil((-f(x) - c)/n) and its constant value
4763 * is ceil((c0 - c)/n).
4765 static __isl_give isl_basic_map
*fix_cst_lower(__isl_take isl_basic_map
*bmap
,
4766 int div
, int ineq
, int lower
, int *pairs
)
4773 o_div
= isl_basic_map_offset(bmap
, isl_dim_div
);
4774 lower_bound_from_parallel(bmap
, ineq
, lower
, o_div
+ div
, &c
);
4775 bmap
= isl_basic_map_fix(bmap
, isl_dim_div
, div
, c
);
4780 return isl_basic_map_drop_redundant_divs(bmap
);
4783 /* Remove divs that are not strictly needed based on the inequality
4785 * In particular, if a div only occurs positively (or negatively)
4786 * in constraints, then it can simply be dropped.
4787 * Also, if a div occurs in only two constraints and if moreover
4788 * those two constraints are opposite to each other, except for the constant
4789 * term and if the sum of the constant terms is such that for any value
4790 * of the other values, there is always at least one integer value of the
4791 * div, i.e., if one plus this sum is greater than or equal to
4792 * the (absolute value) of the coefficient of the div in the constraints,
4793 * then we can also simply drop the div.
4795 * If an existentially quantified variable does not have an explicit
4796 * representation, appears in only a single lower bound that does not
4797 * involve any other such existentially quantified variables and appears
4798 * in this lower bound with coefficient 1,
4799 * then fix the variable to the value of the lower bound. That is,
4800 * turn the inequality into an equality.
4801 * If for any value of the other variables, there is any value
4802 * for the existentially quantified variable satisfying the constraints,
4803 * then this lower bound also satisfies the constraints.
4804 * It is therefore safe to pick this lower bound.
4806 * The same reasoning holds even if the coefficient is not one.
4807 * However, fixing the variable to the value of the lower bound may
4808 * in general introduce an extra integer division, in which case
4809 * it may be better to pick another value.
4810 * If this integer division has a known constant value, then plugging
4811 * in this constant value removes the existentially quantified variable
4812 * completely. In particular, if the lower bound is of the form
4813 * ceil((-f(x) - c)/n) and there are two constraints, f(x) + c0 >= 0 and
4814 * -f(x) + c1 >= 0 such that ceil((-c1 - c)/n) = ceil((c0 - c)/n),
4815 * then the existentially quantified variable can be assigned this
4818 * We skip divs that appear in equalities or in the definition of other divs.
4819 * Divs that appear in the definition of other divs usually occur in at least
4820 * 4 constraints, but the constraints may have been simplified.
4822 * If any divs are left after these simple checks then we move on
4823 * to more complicated cases in drop_more_redundant_divs.
4825 static __isl_give isl_basic_map
*isl_basic_map_drop_redundant_divs_ineq(
4826 __isl_take isl_basic_map
*bmap
)
4835 if (bmap
->n_div
== 0)
4838 off
= isl_space_dim(bmap
->dim
, isl_dim_all
);
4839 pairs
= isl_calloc_array(bmap
->ctx
, int, bmap
->n_div
);
4843 for (i
= 0; i
< bmap
->n_div
; ++i
) {
4845 int last_pos
, last_neg
;
4849 defined
= !isl_int_is_zero(bmap
->div
[i
][0]);
4850 for (j
= i
; j
< bmap
->n_div
; ++j
)
4851 if (!isl_int_is_zero(bmap
->div
[j
][1 + 1 + off
+ i
]))
4853 if (j
< bmap
->n_div
)
4855 for (j
= 0; j
< bmap
->n_eq
; ++j
)
4856 if (!isl_int_is_zero(bmap
->eq
[j
][1 + off
+ i
]))
4862 for (j
= 0; j
< bmap
->n_ineq
; ++j
) {
4863 if (isl_int_is_pos(bmap
->ineq
[j
][1 + off
+ i
])) {
4867 if (isl_int_is_neg(bmap
->ineq
[j
][1 + off
+ i
])) {
4872 pairs
[i
] = pos
* neg
;
4873 if (pairs
[i
] == 0) {
4874 for (j
= bmap
->n_ineq
- 1; j
>= 0; --j
)
4875 if (!isl_int_is_zero(bmap
->ineq
[j
][1+off
+i
]))
4876 isl_basic_map_drop_inequality(bmap
, j
);
4877 bmap
= isl_basic_map_drop_div(bmap
, i
);
4878 return drop_redundant_divs_again(bmap
, pairs
, 0);
4880 if (pairs
[i
] != 1 || !is_opposite(bmap
, last_pos
, last_neg
)) {
4884 single
= single_unknown(bmap
, last_pos
, i
);
4887 if (has_coef_one(bmap
, i
, last_pos
))
4888 return set_eq_and_try_again(bmap
, last_pos
,
4890 lower
= lower_bound_is_cst(bmap
, i
, last_pos
);
4892 return fix_cst_lower(bmap
, i
, last_pos
, lower
,
4897 isl_int_add(bmap
->ineq
[last_pos
][0],
4898 bmap
->ineq
[last_pos
][0], bmap
->ineq
[last_neg
][0]);
4899 isl_int_add_ui(bmap
->ineq
[last_pos
][0],
4900 bmap
->ineq
[last_pos
][0], 1);
4901 redundant
= isl_int_ge(bmap
->ineq
[last_pos
][0],
4902 bmap
->ineq
[last_pos
][1+off
+i
]);
4903 isl_int_sub_ui(bmap
->ineq
[last_pos
][0],
4904 bmap
->ineq
[last_pos
][0], 1);
4905 isl_int_sub(bmap
->ineq
[last_pos
][0],
4906 bmap
->ineq
[last_pos
][0], bmap
->ineq
[last_neg
][0]);
4908 return drop_div_and_try_again(bmap
, i
,
4909 last_pos
, last_neg
, pairs
);
4910 if (!defined
&& ok_to_set_div_from_bound(bmap
, i
, last_pos
)) {
4911 bmap
= set_div_from_lower_bound(bmap
, i
, last_pos
);
4912 return drop_redundant_divs_again(bmap
, pairs
, 1);
4919 return coalesce_or_drop_more_redundant_divs(bmap
, pairs
, n
);
4925 isl_basic_map_free(bmap
);
4929 /* Consider the coefficients at "c" as a row vector and replace
4930 * them with their product with "T". "T" is assumed to be a square matrix.
4932 static isl_stat
preimage(isl_int
*c
, __isl_keep isl_mat
*T
)
4939 return isl_stat_error
;
4940 n
= isl_mat_rows(T
);
4941 if (isl_seq_first_non_zero(c
, n
) == -1)
4943 ctx
= isl_mat_get_ctx(T
);
4944 v
= isl_vec_alloc(ctx
, n
);
4946 return isl_stat_error
;
4947 isl_seq_swp_or_cpy(v
->el
, c
, n
);
4948 v
= isl_vec_mat_product(v
, isl_mat_copy(T
));
4950 return isl_stat_error
;
4951 isl_seq_swp_or_cpy(c
, v
->el
, n
);
4957 /* Plug in T for the variables in "bmap" starting at "pos".
4958 * T is a linear unimodular matrix, i.e., without constant term.
4960 static __isl_give isl_basic_map
*isl_basic_map_preimage_vars(
4961 __isl_take isl_basic_map
*bmap
, unsigned pos
, __isl_take isl_mat
*T
)
4966 bmap
= isl_basic_map_cow(bmap
);
4970 n
= isl_mat_cols(T
);
4971 if (n
!= isl_mat_rows(T
))
4972 isl_die(isl_mat_get_ctx(T
), isl_error_invalid
,
4973 "expecting square matrix", goto error
);
4975 total
= isl_basic_map_dim(bmap
, isl_dim_all
);
4976 if (pos
+ n
> total
|| pos
+ n
< pos
)
4977 isl_die(isl_mat_get_ctx(T
), isl_error_invalid
,
4978 "invalid range", goto error
);
4980 for (i
= 0; i
< bmap
->n_eq
; ++i
)
4981 if (preimage(bmap
->eq
[i
] + 1 + pos
, T
) < 0)
4983 for (i
= 0; i
< bmap
->n_ineq
; ++i
)
4984 if (preimage(bmap
->ineq
[i
] + 1 + pos
, T
) < 0)
4986 for (i
= 0; i
< bmap
->n_div
; ++i
) {
4987 if (isl_basic_map_div_is_marked_unknown(bmap
, i
))
4989 if (preimage(bmap
->div
[i
] + 1 + 1 + pos
, T
) < 0)
4996 isl_basic_map_free(bmap
);
5001 /* Remove divs that are not strictly needed.
5003 * First look for an equality constraint involving two or more
5004 * existentially quantified variables without an explicit
5005 * representation. Replace the combination that appears
5006 * in the equality constraint by a single existentially quantified
5007 * variable such that the equality can be used to derive
5008 * an explicit representation for the variable.
5009 * If there are no more such equality constraints, then continue
5010 * with isl_basic_map_drop_redundant_divs_ineq.
5012 * In particular, if the equality constraint is of the form
5014 * f(x) + \sum_i c_i a_i = 0
5016 * with a_i existentially quantified variable without explicit
5017 * representation, then apply a transformation on the existentially
5018 * quantified variables to turn the constraint into
5022 * with g the gcd of the c_i.
5023 * In order to easily identify which existentially quantified variables
5024 * have a complete explicit representation, i.e., without being defined
5025 * in terms of other existentially quantified variables without
5026 * an explicit representation, the existentially quantified variables
5029 * The variable transformation is computed by extending the row
5030 * [c_1/g ... c_n/g] to a unimodular matrix, obtaining the transformation
5032 * [a_1'] [c_1/g ... c_n/g] [ a_1 ]
5037 * with [c_1/g ... c_n/g] representing the first row of U.
5038 * The inverse of U is then plugged into the original constraints.
5039 * The call to isl_basic_map_simplify makes sure the explicit
5040 * representation for a_1' is extracted from the equality constraint.
5042 __isl_give isl_basic_map
*isl_basic_map_drop_redundant_divs(
5043 __isl_take isl_basic_map
*bmap
)
5047 unsigned o_div
, n_div
;
5054 if (isl_basic_map_divs_known(bmap
))
5055 return isl_basic_map_drop_redundant_divs_ineq(bmap
);
5056 if (bmap
->n_eq
== 0)
5057 return isl_basic_map_drop_redundant_divs_ineq(bmap
);
5058 bmap
= isl_basic_map_sort_divs(bmap
);
5062 first
= isl_basic_map_first_unknown_div(bmap
);
5064 return isl_basic_map_free(bmap
);
5066 o_div
= isl_basic_map_offset(bmap
, isl_dim_div
);
5067 n_div
= isl_basic_map_dim(bmap
, isl_dim_div
);
5069 for (i
= 0; i
< bmap
->n_eq
; ++i
) {
5070 l
= isl_seq_first_non_zero(bmap
->eq
[i
] + o_div
+ first
,
5075 if (isl_seq_first_non_zero(bmap
->eq
[i
] + o_div
+ l
+ 1,
5076 n_div
- (l
+ 1)) == -1)
5080 if (i
>= bmap
->n_eq
)
5081 return isl_basic_map_drop_redundant_divs_ineq(bmap
);
5083 ctx
= isl_basic_map_get_ctx(bmap
);
5084 T
= isl_mat_alloc(ctx
, n_div
- l
, n_div
- l
);
5086 return isl_basic_map_free(bmap
);
5087 isl_seq_cpy(T
->row
[0], bmap
->eq
[i
] + o_div
+ l
, n_div
- l
);
5088 T
= isl_mat_normalize_row(T
, 0);
5089 T
= isl_mat_unimodular_complete(T
, 1);
5090 T
= isl_mat_right_inverse(T
);
5092 for (i
= l
; i
< n_div
; ++i
)
5093 bmap
= isl_basic_map_mark_div_unknown(bmap
, i
);
5094 bmap
= isl_basic_map_preimage_vars(bmap
, o_div
- 1 + l
, T
);
5095 bmap
= isl_basic_map_simplify(bmap
);
5097 return isl_basic_map_drop_redundant_divs(bmap
);
5100 struct isl_basic_set
*isl_basic_set_drop_redundant_divs(
5101 struct isl_basic_set
*bset
)
5103 isl_basic_map
*bmap
= bset_to_bmap(bset
);
5104 return bset_from_bmap(isl_basic_map_drop_redundant_divs(bmap
));
5107 struct isl_map
*isl_map_drop_redundant_divs(struct isl_map
*map
)
5113 for (i
= 0; i
< map
->n
; ++i
) {
5114 map
->p
[i
] = isl_basic_map_drop_redundant_divs(map
->p
[i
]);
5118 ISL_F_CLR(map
, ISL_MAP_NORMALIZED
);
5125 struct isl_set
*isl_set_drop_redundant_divs(struct isl_set
*set
)
5127 return set_from_map(isl_map_drop_redundant_divs(set_to_map(set
)));
5130 /* Does "bmap" satisfy any equality that involves more than 2 variables
5131 * and/or has coefficients different from -1 and 1?
5133 static int has_multiple_var_equality(__isl_keep isl_basic_map
*bmap
)
5138 total
= isl_basic_map_dim(bmap
, isl_dim_all
);
5140 for (i
= 0; i
< bmap
->n_eq
; ++i
) {
5143 j
= isl_seq_first_non_zero(bmap
->eq
[i
] + 1, total
);
5146 if (!isl_int_is_one(bmap
->eq
[i
][1 + j
]) &&
5147 !isl_int_is_negone(bmap
->eq
[i
][1 + j
]))
5151 k
= isl_seq_first_non_zero(bmap
->eq
[i
] + 1 + j
, total
- j
);
5155 if (!isl_int_is_one(bmap
->eq
[i
][1 + j
]) &&
5156 !isl_int_is_negone(bmap
->eq
[i
][1 + j
]))
5160 k
= isl_seq_first_non_zero(bmap
->eq
[i
] + 1 + j
, total
- j
);
5168 /* Remove any common factor g from the constraint coefficients in "v".
5169 * The constant term is stored in the first position and is replaced
5170 * by floor(c/g). If any common factor is removed and if this results
5171 * in a tightening of the constraint, then set *tightened.
5173 static __isl_give isl_vec
*normalize_constraint(__isl_take isl_vec
*v
,
5180 ctx
= isl_vec_get_ctx(v
);
5181 isl_seq_gcd(v
->el
+ 1, v
->size
- 1, &ctx
->normalize_gcd
);
5182 if (isl_int_is_zero(ctx
->normalize_gcd
))
5184 if (isl_int_is_one(ctx
->normalize_gcd
))
5189 if (tightened
&& !isl_int_is_divisible_by(v
->el
[0], ctx
->normalize_gcd
))
5191 isl_int_fdiv_q(v
->el
[0], v
->el
[0], ctx
->normalize_gcd
);
5192 isl_seq_scale_down(v
->el
+ 1, v
->el
+ 1, ctx
->normalize_gcd
,
5197 /* If "bmap" is an integer set that satisfies any equality involving
5198 * more than 2 variables and/or has coefficients different from -1 and 1,
5199 * then use variable compression to reduce the coefficients by removing
5200 * any (hidden) common factor.
5201 * In particular, apply the variable compression to each constraint,
5202 * factor out any common factor in the non-constant coefficients and
5203 * then apply the inverse of the compression.
5204 * At the end, we mark the basic map as having reduced constants.
5205 * If this flag is still set on the next invocation of this function,
5206 * then we skip the computation.
5208 * Removing a common factor may result in a tightening of some of
5209 * the constraints. If this happens, then we may end up with two
5210 * opposite inequalities that can be replaced by an equality.
5211 * We therefore call isl_basic_map_detect_inequality_pairs,
5212 * which checks for such pairs of inequalities as well as eliminate_divs_eq
5213 * and isl_basic_map_gauss if such a pair was found.
5215 __isl_give isl_basic_map
*isl_basic_map_reduce_coefficients(
5216 __isl_take isl_basic_map
*bmap
)
5221 isl_mat
*eq
, *T
, *T2
;
5227 if (ISL_F_ISSET(bmap
, ISL_BASIC_MAP_REDUCED_COEFFICIENTS
))
5229 if (isl_basic_map_is_rational(bmap
))
5231 if (bmap
->n_eq
== 0)
5233 if (!has_multiple_var_equality(bmap
))
5236 total
= isl_basic_map_dim(bmap
, isl_dim_all
);
5237 ctx
= isl_basic_map_get_ctx(bmap
);
5238 v
= isl_vec_alloc(ctx
, 1 + total
);
5240 return isl_basic_map_free(bmap
);
5242 eq
= isl_mat_sub_alloc6(ctx
, bmap
->eq
, 0, bmap
->n_eq
, 0, 1 + total
);
5243 T
= isl_mat_variable_compression(eq
, &T2
);
5246 if (T
->n_col
== 0) {
5250 return isl_basic_map_set_to_empty(bmap
);
5254 for (i
= 0; i
< bmap
->n_ineq
; ++i
) {
5255 isl_seq_cpy(v
->el
, bmap
->ineq
[i
], 1 + total
);
5256 v
= isl_vec_mat_product(v
, isl_mat_copy(T
));
5257 v
= normalize_constraint(v
, &tightened
);
5258 v
= isl_vec_mat_product(v
, isl_mat_copy(T2
));
5261 isl_seq_cpy(bmap
->ineq
[i
], v
->el
, 1 + total
);
5268 ISL_F_SET(bmap
, ISL_BASIC_MAP_REDUCED_COEFFICIENTS
);
5273 bmap
= isl_basic_map_detect_inequality_pairs(bmap
, &progress
);
5275 bmap
= eliminate_divs_eq(bmap
, &progress
);
5276 bmap
= isl_basic_map_gauss(bmap
, NULL
);
5285 return isl_basic_map_free(bmap
);
5288 /* Shift the integer division at position "div" of "bmap"
5289 * by "shift" times the variable at position "pos".
5290 * "pos" is as determined by isl_basic_map_offset, i.e., pos == 0
5291 * corresponds to the constant term.
5293 * That is, if the integer division has the form
5297 * then replace it by
5299 * floor((f(x) + shift * d * x_pos)/d) - shift * x_pos
5301 __isl_give isl_basic_map
*isl_basic_map_shift_div(
5302 __isl_take isl_basic_map
*bmap
, int div
, int pos
, isl_int shift
)
5310 total
= isl_basic_map_dim(bmap
, isl_dim_all
);
5311 total
-= isl_basic_map_dim(bmap
, isl_dim_div
);
5313 isl_int_addmul(bmap
->div
[div
][1 + pos
], shift
, bmap
->div
[div
][0]);
5315 for (i
= 0; i
< bmap
->n_eq
; ++i
) {
5316 if (isl_int_is_zero(bmap
->eq
[i
][1 + total
+ div
]))
5318 isl_int_submul(bmap
->eq
[i
][pos
],
5319 shift
, bmap
->eq
[i
][1 + total
+ div
]);
5321 for (i
= 0; i
< bmap
->n_ineq
; ++i
) {
5322 if (isl_int_is_zero(bmap
->ineq
[i
][1 + total
+ div
]))
5324 isl_int_submul(bmap
->ineq
[i
][pos
],
5325 shift
, bmap
->ineq
[i
][1 + total
+ div
]);
5327 for (i
= 0; i
< bmap
->n_div
; ++i
) {
5328 if (isl_int_is_zero(bmap
->div
[i
][0]))
5330 if (isl_int_is_zero(bmap
->div
[i
][1 + 1 + total
+ div
]))
5332 isl_int_submul(bmap
->div
[i
][1 + pos
],
5333 shift
, bmap
->div
[i
][1 + 1 + total
+ div
]);