2 * Copyright 2008-2009 Katholieke Universiteit Leuven
4 * Use of this software is governed by the GNU LGPLv2.1 license
6 * Written by Sven Verdoolaege, K.U.Leuven, Departement
7 * Computerwetenschappen, Celestijnenlaan 200A, B-3001 Leuven, Belgium
10 #include "isl_equalities.h"
12 #include "isl_map_private.h"
16 static void swap_equality(struct isl_basic_map
*bmap
, int a
, int b
)
18 isl_int
*t
= bmap
->eq
[a
];
19 bmap
->eq
[a
] = bmap
->eq
[b
];
23 static void swap_inequality(struct isl_basic_map
*bmap
, int a
, int b
)
26 isl_int
*t
= bmap
->ineq
[a
];
27 bmap
->ineq
[a
] = bmap
->ineq
[b
];
32 static void set_swap_inequality(struct isl_basic_set
*bset
, int a
, int b
)
34 swap_inequality((struct isl_basic_map
*)bset
, a
, b
);
37 static void constraint_drop_vars(isl_int
*c
, unsigned n
, unsigned rem
)
39 isl_seq_cpy(c
, c
+ n
, rem
);
40 isl_seq_clr(c
+ rem
, n
);
43 /* Drop n dimensions starting at first.
45 * In principle, this frees up some extra variables as the number
46 * of columns remains constant, but we would have to extend
47 * the div array too as the number of rows in this array is assumed
48 * to be equal to extra.
50 struct isl_basic_set
*isl_basic_set_drop_dims(
51 struct isl_basic_set
*bset
, unsigned first
, unsigned n
)
58 isl_assert(bset
->ctx
, first
+ n
<= bset
->dim
->n_out
, goto error
);
63 bset
= isl_basic_set_cow(bset
);
67 for (i
= 0; i
< bset
->n_eq
; ++i
)
68 constraint_drop_vars(bset
->eq
[i
]+1+bset
->dim
->nparam
+first
, n
,
69 (bset
->dim
->n_out
-first
-n
)+bset
->extra
);
71 for (i
= 0; i
< bset
->n_ineq
; ++i
)
72 constraint_drop_vars(bset
->ineq
[i
]+1+bset
->dim
->nparam
+first
, n
,
73 (bset
->dim
->n_out
-first
-n
)+bset
->extra
);
75 for (i
= 0; i
< bset
->n_div
; ++i
)
76 constraint_drop_vars(bset
->div
[i
]+1+1+bset
->dim
->nparam
+first
, n
,
77 (bset
->dim
->n_out
-first
-n
)+bset
->extra
);
79 bset
->dim
= isl_dim_drop_outputs(bset
->dim
, first
, n
);
83 ISL_F_CLR(bset
, ISL_BASIC_SET_NORMALIZED
);
84 bset
= isl_basic_set_simplify(bset
);
85 return isl_basic_set_finalize(bset
);
87 isl_basic_set_free(bset
);
91 struct isl_set
*isl_set_drop_dims(
92 struct isl_set
*set
, unsigned first
, unsigned n
)
99 isl_assert(set
->ctx
, first
+ n
<= set
->dim
->n_out
, goto error
);
103 set
= isl_set_cow(set
);
106 set
->dim
= isl_dim_drop_outputs(set
->dim
, first
, n
);
110 for (i
= 0; i
< set
->n
; ++i
) {
111 set
->p
[i
] = isl_basic_set_drop_dims(set
->p
[i
], first
, n
);
116 ISL_F_CLR(set
, ISL_SET_NORMALIZED
);
123 /* Move "n" divs starting at "first" to the end of the list of divs.
125 static struct isl_basic_map
*move_divs_last(struct isl_basic_map
*bmap
,
126 unsigned first
, unsigned n
)
131 if (first
+ n
== bmap
->n_div
)
134 div
= isl_alloc_array(bmap
->ctx
, isl_int
*, n
);
137 for (i
= 0; i
< n
; ++i
)
138 div
[i
] = bmap
->div
[first
+ i
];
139 for (i
= 0; i
< bmap
->n_div
- first
- n
; ++i
)
140 bmap
->div
[first
+ i
] = bmap
->div
[first
+ n
+ i
];
141 for (i
= 0; i
< n
; ++i
)
142 bmap
->div
[bmap
->n_div
- n
+ i
] = div
[i
];
146 isl_basic_map_free(bmap
);
150 /* Drop "n" dimensions of type "type" starting at "first".
152 * In principle, this frees up some extra variables as the number
153 * of columns remains constant, but we would have to extend
154 * the div array too as the number of rows in this array is assumed
155 * to be equal to extra.
157 struct isl_basic_map
*isl_basic_map_drop(struct isl_basic_map
*bmap
,
158 enum isl_dim_type type
, unsigned first
, unsigned n
)
168 dim
= isl_basic_map_dim(bmap
, type
);
169 isl_assert(bmap
->ctx
, first
+ n
<= dim
, goto error
);
174 bmap
= isl_basic_map_cow(bmap
);
178 offset
= isl_basic_map_offset(bmap
, type
) + first
;
179 left
= isl_basic_map_total_dim(bmap
) - (offset
- 1) - n
;
180 for (i
= 0; i
< bmap
->n_eq
; ++i
)
181 constraint_drop_vars(bmap
->eq
[i
]+offset
, n
, left
);
183 for (i
= 0; i
< bmap
->n_ineq
; ++i
)
184 constraint_drop_vars(bmap
->ineq
[i
]+offset
, n
, left
);
186 for (i
= 0; i
< bmap
->n_div
; ++i
)
187 constraint_drop_vars(bmap
->div
[i
]+1+offset
, n
, left
);
189 if (type
== isl_dim_div
) {
190 bmap
= move_divs_last(bmap
, first
, n
);
193 isl_basic_map_free_div(bmap
, n
);
195 bmap
->dim
= isl_dim_drop(bmap
->dim
, type
, first
, n
);
199 ISL_F_CLR(bmap
, ISL_BASIC_MAP_NORMALIZED
);
200 bmap
= isl_basic_map_simplify(bmap
);
201 return isl_basic_map_finalize(bmap
);
203 isl_basic_map_free(bmap
);
207 struct isl_basic_map
*isl_basic_map_drop_inputs(
208 struct isl_basic_map
*bmap
, unsigned first
, unsigned n
)
210 return isl_basic_map_drop(bmap
, isl_dim_in
, first
, n
);
213 struct isl_map
*isl_map_drop(struct isl_map
*map
,
214 enum isl_dim_type type
, unsigned first
, unsigned n
)
221 isl_assert(map
->ctx
, first
+ n
<= isl_map_dim(map
, type
), goto error
);
225 map
= isl_map_cow(map
);
228 map
->dim
= isl_dim_drop(map
->dim
, type
, first
, n
);
232 for (i
= 0; i
< map
->n
; ++i
) {
233 map
->p
[i
] = isl_basic_map_drop(map
->p
[i
], type
, first
, n
);
237 ISL_F_CLR(map
, ISL_MAP_NORMALIZED
);
245 struct isl_set
*isl_set_drop(struct isl_set
*set
,
246 enum isl_dim_type type
, unsigned first
, unsigned n
)
248 return (isl_set
*)isl_map_drop((isl_map
*)set
, type
, first
, n
);
251 struct isl_map
*isl_map_drop_inputs(
252 struct isl_map
*map
, unsigned first
, unsigned n
)
254 return isl_map_drop(map
, isl_dim_in
, first
, n
);
258 * We don't cow, as the div is assumed to be redundant.
260 static struct isl_basic_map
*isl_basic_map_drop_div(
261 struct isl_basic_map
*bmap
, unsigned div
)
269 pos
= 1 + isl_dim_total(bmap
->dim
) + div
;
271 isl_assert(bmap
->ctx
, div
< bmap
->n_div
, goto error
);
273 for (i
= 0; i
< bmap
->n_eq
; ++i
)
274 constraint_drop_vars(bmap
->eq
[i
]+pos
, 1, bmap
->extra
-div
-1);
276 for (i
= 0; i
< bmap
->n_ineq
; ++i
) {
277 if (!isl_int_is_zero(bmap
->ineq
[i
][pos
])) {
278 isl_basic_map_drop_inequality(bmap
, i
);
282 constraint_drop_vars(bmap
->ineq
[i
]+pos
, 1, bmap
->extra
-div
-1);
285 for (i
= 0; i
< bmap
->n_div
; ++i
)
286 constraint_drop_vars(bmap
->div
[i
]+1+pos
, 1, bmap
->extra
-div
-1);
288 if (div
!= bmap
->n_div
- 1) {
290 isl_int
*t
= bmap
->div
[div
];
292 for (j
= div
; j
< bmap
->n_div
- 1; ++j
)
293 bmap
->div
[j
] = bmap
->div
[j
+1];
295 bmap
->div
[bmap
->n_div
- 1] = t
;
297 ISL_F_CLR(bmap
, ISL_BASIC_MAP_NORMALIZED
);
298 isl_basic_map_free_div(bmap
, 1);
302 isl_basic_map_free(bmap
);
306 struct isl_basic_map
*isl_basic_map_normalize_constraints(
307 struct isl_basic_map
*bmap
)
311 unsigned total
= isl_basic_map_total_dim(bmap
);
314 for (i
= bmap
->n_eq
- 1; i
>= 0; --i
) {
315 isl_seq_gcd(bmap
->eq
[i
]+1, total
, &gcd
);
316 if (isl_int_is_zero(gcd
)) {
317 if (!isl_int_is_zero(bmap
->eq
[i
][0])) {
318 bmap
= isl_basic_map_set_to_empty(bmap
);
321 isl_basic_map_drop_equality(bmap
, i
);
324 if (ISL_F_ISSET(bmap
, ISL_BASIC_MAP_RATIONAL
))
325 isl_int_gcd(gcd
, gcd
, bmap
->eq
[i
][0]);
326 if (isl_int_is_one(gcd
))
328 if (!isl_int_is_divisible_by(bmap
->eq
[i
][0], gcd
)) {
329 bmap
= isl_basic_map_set_to_empty(bmap
);
332 isl_seq_scale_down(bmap
->eq
[i
], bmap
->eq
[i
], gcd
, 1+total
);
335 for (i
= bmap
->n_ineq
- 1; i
>= 0; --i
) {
336 isl_seq_gcd(bmap
->ineq
[i
]+1, total
, &gcd
);
337 if (isl_int_is_zero(gcd
)) {
338 if (isl_int_is_neg(bmap
->ineq
[i
][0])) {
339 bmap
= isl_basic_map_set_to_empty(bmap
);
342 isl_basic_map_drop_inequality(bmap
, i
);
345 if (ISL_F_ISSET(bmap
, ISL_BASIC_MAP_RATIONAL
))
346 isl_int_gcd(gcd
, gcd
, bmap
->ineq
[i
][0]);
347 if (isl_int_is_one(gcd
))
349 isl_int_fdiv_q(bmap
->ineq
[i
][0], bmap
->ineq
[i
][0], gcd
);
350 isl_seq_scale_down(bmap
->ineq
[i
]+1, bmap
->ineq
[i
]+1, gcd
, total
);
357 struct isl_basic_set
*isl_basic_set_normalize_constraints(
358 struct isl_basic_set
*bset
)
360 return (struct isl_basic_set
*)isl_basic_map_normalize_constraints(
361 (struct isl_basic_map
*)bset
);
364 /* Assumes divs have been ordered if keep_divs is set.
366 static void eliminate_var_using_equality(struct isl_basic_map
*bmap
,
367 unsigned pos
, isl_int
*eq
, int keep_divs
, int *progress
)
373 total
= isl_basic_map_total_dim(bmap
);
374 last_div
= isl_seq_last_non_zero(eq
+ 1 + isl_dim_total(bmap
->dim
),
376 for (k
= 0; k
< bmap
->n_eq
; ++k
) {
377 if (bmap
->eq
[k
] == eq
)
379 if (isl_int_is_zero(bmap
->eq
[k
][1+pos
]))
383 isl_seq_elim(bmap
->eq
[k
], eq
, 1+pos
, 1+total
, NULL
);
386 for (k
= 0; k
< bmap
->n_ineq
; ++k
) {
387 if (isl_int_is_zero(bmap
->ineq
[k
][1+pos
]))
391 isl_seq_elim(bmap
->ineq
[k
], eq
, 1+pos
, 1+total
, NULL
);
392 ISL_F_CLR(bmap
, ISL_BASIC_MAP_NORMALIZED
);
395 for (k
= 0; k
< bmap
->n_div
; ++k
) {
396 if (isl_int_is_zero(bmap
->div
[k
][0]))
398 if (isl_int_is_zero(bmap
->div
[k
][1+1+pos
]))
402 /* We need to be careful about circular definitions,
403 * so for now we just remove the definition of div k
404 * if the equality contains any divs.
405 * If keep_divs is set, then the divs have been ordered
406 * and we can keep the definition as long as the result
409 if (last_div
== -1 || (keep_divs
&& last_div
< k
))
410 isl_seq_elim(bmap
->div
[k
]+1, eq
,
411 1+pos
, 1+total
, &bmap
->div
[k
][0]);
413 isl_seq_clr(bmap
->div
[k
], 1 + total
);
414 ISL_F_CLR(bmap
, ISL_BASIC_MAP_NORMALIZED
);
418 /* Assumes divs have been ordered if keep_divs is set.
420 static void eliminate_div(struct isl_basic_map
*bmap
, isl_int
*eq
,
421 unsigned div
, int keep_divs
)
423 unsigned pos
= isl_dim_total(bmap
->dim
) + div
;
425 eliminate_var_using_equality(bmap
, pos
, eq
, keep_divs
, NULL
);
427 isl_basic_map_drop_div(bmap
, div
);
430 /* Check if elimination of div "div" using equality "eq" would not
431 * result in a div depending on a later div.
433 static int ok_to_eliminate_div(struct isl_basic_map
*bmap
, isl_int
*eq
,
438 unsigned pos
= isl_dim_total(bmap
->dim
) + div
;
440 last_div
= isl_seq_last_non_zero(eq
+ 1 + isl_dim_total(bmap
->dim
),
442 if (last_div
< 0 || last_div
<= div
)
445 for (k
= 0; k
<= last_div
; ++k
) {
446 if (isl_int_is_zero(bmap
->div
[k
][0]))
448 if (!isl_int_is_zero(bmap
->div
[k
][1 + 1 + pos
]))
455 /* Elimininate divs based on equalities
457 static struct isl_basic_map
*eliminate_divs_eq(
458 struct isl_basic_map
*bmap
, int *progress
)
465 bmap
= isl_basic_map_order_divs(bmap
);
470 off
= 1 + isl_dim_total(bmap
->dim
);
472 for (d
= bmap
->n_div
- 1; d
>= 0 ; --d
) {
473 for (i
= 0; i
< bmap
->n_eq
; ++i
) {
474 if (!isl_int_is_one(bmap
->eq
[i
][off
+ d
]) &&
475 !isl_int_is_negone(bmap
->eq
[i
][off
+ d
]))
477 if (!ok_to_eliminate_div(bmap
, bmap
->eq
[i
], d
))
481 eliminate_div(bmap
, bmap
->eq
[i
], d
, 1);
482 isl_basic_map_drop_equality(bmap
, i
);
487 return eliminate_divs_eq(bmap
, progress
);
491 /* Elimininate divs based on inequalities
493 static struct isl_basic_map
*eliminate_divs_ineq(
494 struct isl_basic_map
*bmap
, int *progress
)
505 off
= 1 + isl_dim_total(bmap
->dim
);
507 for (d
= bmap
->n_div
- 1; d
>= 0 ; --d
) {
508 for (i
= 0; i
< bmap
->n_eq
; ++i
)
509 if (!isl_int_is_zero(bmap
->eq
[i
][off
+ d
]))
513 for (i
= 0; i
< bmap
->n_ineq
; ++i
)
514 if (isl_int_abs_gt(bmap
->ineq
[i
][off
+ d
], ctx
->one
))
516 if (i
< bmap
->n_ineq
)
519 bmap
= isl_basic_map_eliminate_vars(bmap
, (off
-1)+d
, 1);
520 if (ISL_F_ISSET(bmap
, ISL_BASIC_MAP_EMPTY
))
522 bmap
= isl_basic_map_drop_div(bmap
, d
);
529 struct isl_basic_map
*isl_basic_map_gauss(
530 struct isl_basic_map
*bmap
, int *progress
)
538 bmap
= isl_basic_map_order_divs(bmap
);
543 total
= isl_basic_map_total_dim(bmap
);
544 total_var
= total
- bmap
->n_div
;
546 last_var
= total
- 1;
547 for (done
= 0; done
< bmap
->n_eq
; ++done
) {
548 for (; last_var
>= 0; --last_var
) {
549 for (k
= done
; k
< bmap
->n_eq
; ++k
)
550 if (!isl_int_is_zero(bmap
->eq
[k
][1+last_var
]))
558 swap_equality(bmap
, k
, done
);
559 if (isl_int_is_neg(bmap
->eq
[done
][1+last_var
]))
560 isl_seq_neg(bmap
->eq
[done
], bmap
->eq
[done
], 1+total
);
562 eliminate_var_using_equality(bmap
, last_var
, bmap
->eq
[done
], 1,
565 if (last_var
>= total_var
&&
566 isl_int_is_zero(bmap
->div
[last_var
- total_var
][0])) {
567 unsigned div
= last_var
- total_var
;
568 isl_seq_neg(bmap
->div
[div
]+1, bmap
->eq
[done
], 1+total
);
569 isl_int_set_si(bmap
->div
[div
][1+1+last_var
], 0);
570 isl_int_set(bmap
->div
[div
][0],
571 bmap
->eq
[done
][1+last_var
]);
572 ISL_F_CLR(bmap
, ISL_BASIC_MAP_NORMALIZED
);
575 if (done
== bmap
->n_eq
)
577 for (k
= done
; k
< bmap
->n_eq
; ++k
) {
578 if (isl_int_is_zero(bmap
->eq
[k
][0]))
580 return isl_basic_map_set_to_empty(bmap
);
582 isl_basic_map_free_equality(bmap
, bmap
->n_eq
-done
);
586 struct isl_basic_set
*isl_basic_set_gauss(
587 struct isl_basic_set
*bset
, int *progress
)
589 return (struct isl_basic_set
*)isl_basic_map_gauss(
590 (struct isl_basic_map
*)bset
, progress
);
594 static unsigned int round_up(unsigned int v
)
605 static int hash_index(isl_int
***index
, unsigned int size
, int bits
,
606 struct isl_basic_map
*bmap
, int k
)
609 unsigned total
= isl_basic_map_total_dim(bmap
);
610 uint32_t hash
= isl_seq_get_hash_bits(bmap
->ineq
[k
]+1, total
, bits
);
611 for (h
= hash
; index
[h
]; h
= (h
+1) % size
)
612 if (&bmap
->ineq
[k
] != index
[h
] &&
613 isl_seq_eq(bmap
->ineq
[k
]+1, index
[h
][0]+1, total
))
618 static int set_hash_index(isl_int
***index
, unsigned int size
, int bits
,
619 struct isl_basic_set
*bset
, int k
)
621 return hash_index(index
, size
, bits
, (struct isl_basic_map
*)bset
, k
);
624 /* If we can eliminate more than one div, then we need to make
625 * sure we do it from last div to first div, in order not to
626 * change the position of the other divs that still need to
629 static struct isl_basic_map
*remove_duplicate_divs(
630 struct isl_basic_map
*bmap
, int *progress
)
638 unsigned total_var
= isl_dim_total(bmap
->dim
);
639 unsigned total
= total_var
+ bmap
->n_div
;
642 if (bmap
->n_div
<= 1)
646 for (k
= bmap
->n_div
- 1; k
>= 0; --k
)
647 if (!isl_int_is_zero(bmap
->div
[k
][0]))
652 elim_for
= isl_calloc_array(ctx
, int, bmap
->n_div
);
653 size
= round_up(4 * bmap
->n_div
/ 3 - 1);
654 bits
= ffs(size
) - 1;
655 index
= isl_calloc_array(ctx
, int, size
);
658 eq
= isl_blk_alloc(ctx
, 1+total
);
659 if (isl_blk_is_error(eq
))
662 isl_seq_clr(eq
.data
, 1+total
);
663 index
[isl_seq_get_hash_bits(bmap
->div
[k
], 2+total
, bits
)] = k
+ 1;
664 for (--k
; k
>= 0; --k
) {
667 if (isl_int_is_zero(bmap
->div
[k
][0]))
670 hash
= isl_seq_get_hash_bits(bmap
->div
[k
], 2+total
, bits
);
671 for (h
= hash
; index
[h
]; h
= (h
+1) % size
)
672 if (isl_seq_eq(bmap
->div
[k
],
673 bmap
->div
[index
[h
]-1], 2+total
))
682 for (l
= bmap
->n_div
- 1; l
>= 0; --l
) {
686 isl_int_set_si(eq
.data
[1+total_var
+k
], -1);
687 isl_int_set_si(eq
.data
[1+total_var
+l
], 1);
688 eliminate_div(bmap
, eq
.data
, l
, 0);
689 isl_int_set_si(eq
.data
[1+total_var
+k
], 0);
690 isl_int_set_si(eq
.data
[1+total_var
+l
], 0);
693 isl_blk_free(ctx
, eq
);
700 static int n_pure_div_eq(struct isl_basic_map
*bmap
)
705 total
= isl_dim_total(bmap
->dim
);
706 for (i
= 0, j
= bmap
->n_div
-1; i
< bmap
->n_eq
; ++i
) {
707 while (j
>= 0 && isl_int_is_zero(bmap
->eq
[i
][1 + total
+ j
]))
711 if (isl_seq_first_non_zero(bmap
->eq
[i
] + 1 + total
, j
) != -1)
717 /* Normalize divs that appear in equalities.
719 * In particular, we assume that bmap contains some equalities
724 * and we want to replace the set of e_i by a minimal set and
725 * such that the new e_i have a canonical representation in terms
727 * If any of the equalities involves more than one divs, then
728 * we currently simply bail out.
730 * Let us first additionally assume that all equalities involve
731 * a div. The equalities then express modulo constraints on the
732 * remaining variables and we can use "parameter compression"
733 * to find a minimal set of constraints. The result is a transformation
735 * x = T(x') = x_0 + G x'
737 * with G a lower-triangular matrix with all elements below the diagonal
738 * non-negative and smaller than the diagonal element on the same row.
739 * We first normalize x_0 by making the same property hold in the affine
741 * The rows i of G with a 1 on the diagonal do not impose any modulo
742 * constraint and simply express x_i = x'_i.
743 * For each of the remaining rows i, we introduce a div and a corresponding
744 * equality. In particular
746 * g_ii e_j = x_i - g_i(x')
748 * where each x'_k is replaced either by x_k (if g_kk = 1) or the
749 * corresponding div (if g_kk != 1).
751 * If there are any equalities not involving any div, then we
752 * first apply a variable compression on the variables x:
754 * x = C x'' x'' = C_2 x
756 * and perform the above parameter compression on A C instead of on A.
757 * The resulting compression is then of the form
759 * x'' = T(x') = x_0 + G x'
761 * and in constructing the new divs and the corresponding equalities,
762 * we have to replace each x'', i.e., the x'_k with (g_kk = 1),
763 * by the corresponding row from C_2.
765 static struct isl_basic_map
*normalize_divs(
766 struct isl_basic_map
*bmap
, int *progress
)
773 struct isl_mat
*T
= NULL
;
774 struct isl_mat
*C
= NULL
;
775 struct isl_mat
*C2
= NULL
;
783 if (bmap
->n_div
== 0)
789 if (ISL_F_ISSET(bmap
, ISL_BASIC_MAP_NORMALIZED_DIVS
))
792 total
= isl_dim_total(bmap
->dim
);
793 div_eq
= n_pure_div_eq(bmap
);
797 if (div_eq
< bmap
->n_eq
) {
798 B
= isl_mat_sub_alloc(bmap
->ctx
, bmap
->eq
, div_eq
,
799 bmap
->n_eq
- div_eq
, 0, 1 + total
);
800 C
= isl_mat_variable_compression(B
, &C2
);
804 bmap
= isl_basic_map_set_to_empty(bmap
);
811 d
= isl_vec_alloc(bmap
->ctx
, div_eq
);
814 for (i
= 0, j
= bmap
->n_div
-1; i
< div_eq
; ++i
) {
815 while (j
>= 0 && isl_int_is_zero(bmap
->eq
[i
][1 + total
+ j
]))
817 isl_int_set(d
->block
.data
[i
], bmap
->eq
[i
][1 + total
+ j
]);
819 B
= isl_mat_sub_alloc(bmap
->ctx
, bmap
->eq
, 0, div_eq
, 0, 1 + total
);
822 B
= isl_mat_product(B
, C
);
826 T
= isl_mat_parameter_compression(B
, d
);
830 bmap
= isl_basic_map_set_to_empty(bmap
);
836 for (i
= 0; i
< T
->n_row
- 1; ++i
) {
837 isl_int_fdiv_q(v
, T
->row
[1 + i
][0], T
->row
[1 + i
][1 + i
]);
838 if (isl_int_is_zero(v
))
840 isl_mat_col_submul(T
, 0, v
, 1 + i
);
843 pos
= isl_alloc_array(bmap
->ctx
, int, T
->n_row
);
844 /* We have to be careful because dropping equalities may reorder them */
846 for (j
= bmap
->n_div
- 1; j
>= 0; --j
) {
847 for (i
= 0; i
< bmap
->n_eq
; ++i
)
848 if (!isl_int_is_zero(bmap
->eq
[i
][1 + total
+ j
]))
850 if (i
< bmap
->n_eq
) {
851 bmap
= isl_basic_map_drop_div(bmap
, j
);
852 isl_basic_map_drop_equality(bmap
, i
);
858 for (i
= 1; i
< T
->n_row
; ++i
) {
859 if (isl_int_is_one(T
->row
[i
][i
]))
864 if (needed
> dropped
) {
865 bmap
= isl_basic_map_extend_dim(bmap
, isl_dim_copy(bmap
->dim
),
870 for (i
= 1; i
< T
->n_row
; ++i
) {
871 if (isl_int_is_one(T
->row
[i
][i
]))
873 k
= isl_basic_map_alloc_div(bmap
);
874 pos
[i
] = 1 + total
+ k
;
875 isl_seq_clr(bmap
->div
[k
] + 1, 1 + total
+ bmap
->n_div
);
876 isl_int_set(bmap
->div
[k
][0], T
->row
[i
][i
]);
878 isl_seq_cpy(bmap
->div
[k
] + 1, C2
->row
[i
], 1 + total
);
880 isl_int_set_si(bmap
->div
[k
][1 + i
], 1);
881 for (j
= 0; j
< i
; ++j
) {
882 if (isl_int_is_zero(T
->row
[i
][j
]))
884 if (pos
[j
] < T
->n_row
&& C2
)
885 isl_seq_submul(bmap
->div
[k
] + 1, T
->row
[i
][j
],
886 C2
->row
[pos
[j
]], 1 + total
);
888 isl_int_neg(bmap
->div
[k
][1 + pos
[j
]],
891 j
= isl_basic_map_alloc_equality(bmap
);
892 isl_seq_neg(bmap
->eq
[j
], bmap
->div
[k
]+1, 1+total
+bmap
->n_div
);
893 isl_int_set(bmap
->eq
[j
][pos
[i
]], bmap
->div
[k
][0]);
902 ISL_F_SET(bmap
, ISL_BASIC_MAP_NORMALIZED_DIVS
);
912 static struct isl_basic_map
*set_div_from_lower_bound(
913 struct isl_basic_map
*bmap
, int div
, int ineq
)
915 unsigned total
= 1 + isl_dim_total(bmap
->dim
);
917 isl_seq_neg(bmap
->div
[div
] + 1, bmap
->ineq
[ineq
], total
+ bmap
->n_div
);
918 isl_int_set(bmap
->div
[div
][0], bmap
->ineq
[ineq
][total
+ div
]);
919 isl_int_add(bmap
->div
[div
][1], bmap
->div
[div
][1], bmap
->div
[div
][0]);
920 isl_int_sub_ui(bmap
->div
[div
][1], bmap
->div
[div
][1], 1);
921 isl_int_set_si(bmap
->div
[div
][1 + total
+ div
], 0);
926 /* Check whether it is ok to define a div based on an inequality.
927 * To avoid the introduction of circular definitions of divs, we
928 * do not allow such a definition if the resulting expression would refer to
929 * any other undefined divs or if any known div is defined in
930 * terms of the unknown div.
932 static int ok_to_set_div_from_bound(struct isl_basic_map
*bmap
,
936 unsigned total
= 1 + isl_dim_total(bmap
->dim
);
938 /* Not defined in terms of unknown divs */
939 for (j
= 0; j
< bmap
->n_div
; ++j
) {
942 if (isl_int_is_zero(bmap
->ineq
[ineq
][total
+ j
]))
944 if (isl_int_is_zero(bmap
->div
[j
][0]))
948 /* No other div defined in terms of this one => avoid loops */
949 for (j
= 0; j
< bmap
->n_div
; ++j
) {
952 if (isl_int_is_zero(bmap
->div
[j
][0]))
954 if (!isl_int_is_zero(bmap
->div
[j
][1 + total
+ div
]))
961 /* Given two constraints "k" and "l" that are opposite to each other,
962 * except for the constant term, check if we can use them
963 * to obtain an expression for one of the hitherto unknown divs.
964 * "sum" is the sum of the constant terms of the constraints.
965 * If this sum is strictly smaller than the coefficient of one
966 * of the divs, then this pair can be used define the div.
967 * To avoid the introduction of circular definitions of divs, we
968 * do not use the pair if the resulting expression would refer to
969 * any other undefined divs or if any known div is defined in
970 * terms of the unknown div.
972 static struct isl_basic_map
*check_for_div_constraints(
973 struct isl_basic_map
*bmap
, int k
, int l
, isl_int sum
, int *progress
)
976 unsigned total
= 1 + isl_dim_total(bmap
->dim
);
978 for (i
= 0; i
< bmap
->n_div
; ++i
) {
979 if (!isl_int_is_zero(bmap
->div
[i
][0]))
981 if (isl_int_is_zero(bmap
->ineq
[k
][total
+ i
]))
983 if (isl_int_abs_ge(sum
, bmap
->ineq
[k
][total
+ i
]))
985 if (!ok_to_set_div_from_bound(bmap
, i
, k
))
987 if (isl_int_is_pos(bmap
->ineq
[k
][total
+ i
]))
988 bmap
= set_div_from_lower_bound(bmap
, i
, k
);
990 bmap
= set_div_from_lower_bound(bmap
, i
, l
);
998 static struct isl_basic_map
*remove_duplicate_constraints(
999 struct isl_basic_map
*bmap
, int *progress
)
1005 unsigned total
= isl_basic_map_total_dim(bmap
);
1008 if (bmap
->n_ineq
<= 1)
1011 size
= round_up(4 * (bmap
->n_ineq
+1) / 3 - 1);
1012 bits
= ffs(size
) - 1;
1013 index
= isl_calloc_array(ctx
, isl_int
**, size
);
1017 index
[isl_seq_get_hash_bits(bmap
->ineq
[0]+1, total
, bits
)] = &bmap
->ineq
[0];
1018 for (k
= 1; k
< bmap
->n_ineq
; ++k
) {
1019 h
= hash_index(index
, size
, bits
, bmap
, k
);
1021 index
[h
] = &bmap
->ineq
[k
];
1026 l
= index
[h
] - &bmap
->ineq
[0];
1027 if (isl_int_lt(bmap
->ineq
[k
][0], bmap
->ineq
[l
][0]))
1028 swap_inequality(bmap
, k
, l
);
1029 isl_basic_map_drop_inequality(bmap
, k
);
1033 for (k
= 0; k
< bmap
->n_ineq
-1; ++k
) {
1034 isl_seq_neg(bmap
->ineq
[k
]+1, bmap
->ineq
[k
]+1, total
);
1035 h
= hash_index(index
, size
, bits
, bmap
, k
);
1036 isl_seq_neg(bmap
->ineq
[k
]+1, bmap
->ineq
[k
]+1, total
);
1039 l
= index
[h
] - &bmap
->ineq
[0];
1040 isl_int_add(sum
, bmap
->ineq
[k
][0], bmap
->ineq
[l
][0]);
1041 if (isl_int_is_pos(sum
)) {
1042 bmap
= check_for_div_constraints(bmap
, k
, l
, sum
,
1046 if (isl_int_is_zero(sum
)) {
1047 /* We need to break out of the loop after these
1048 * changes since the contents of the hash
1049 * will no longer be valid.
1050 * Plus, we probably we want to regauss first.
1054 isl_basic_map_drop_inequality(bmap
, l
);
1055 isl_basic_map_inequality_to_equality(bmap
, k
);
1057 bmap
= isl_basic_map_set_to_empty(bmap
);
1067 struct isl_basic_map
*isl_basic_map_simplify(struct isl_basic_map
*bmap
)
1074 bmap
= isl_basic_map_normalize_constraints(bmap
);
1075 bmap
= remove_duplicate_divs(bmap
, &progress
);
1076 bmap
= eliminate_divs_eq(bmap
, &progress
);
1077 bmap
= eliminate_divs_ineq(bmap
, &progress
);
1078 bmap
= isl_basic_map_gauss(bmap
, &progress
);
1079 /* requires equalities in normal form */
1080 bmap
= normalize_divs(bmap
, &progress
);
1081 bmap
= remove_duplicate_constraints(bmap
, &progress
);
1086 struct isl_basic_set
*isl_basic_set_simplify(struct isl_basic_set
*bset
)
1088 return (struct isl_basic_set
*)
1089 isl_basic_map_simplify((struct isl_basic_map
*)bset
);
1093 /* If the only constraints a div d=floor(f/m)
1094 * appears in are its two defining constraints
1097 * -(f - (m - 1)) + m d >= 0
1099 * then it can safely be removed.
1101 static int div_is_redundant(struct isl_basic_map
*bmap
, int div
)
1104 unsigned pos
= 1 + isl_dim_total(bmap
->dim
) + div
;
1106 for (i
= 0; i
< bmap
->n_eq
; ++i
)
1107 if (!isl_int_is_zero(bmap
->eq
[i
][pos
]))
1110 for (i
= 0; i
< bmap
->n_ineq
; ++i
) {
1111 if (isl_int_is_zero(bmap
->ineq
[i
][pos
]))
1113 if (isl_int_eq(bmap
->ineq
[i
][pos
], bmap
->div
[div
][0])) {
1115 isl_int_sub(bmap
->div
[div
][1],
1116 bmap
->div
[div
][1], bmap
->div
[div
][0]);
1117 isl_int_add_ui(bmap
->div
[div
][1], bmap
->div
[div
][1], 1);
1118 neg
= isl_seq_is_neg(bmap
->ineq
[i
], bmap
->div
[div
]+1, pos
);
1119 isl_int_sub_ui(bmap
->div
[div
][1], bmap
->div
[div
][1], 1);
1120 isl_int_add(bmap
->div
[div
][1],
1121 bmap
->div
[div
][1], bmap
->div
[div
][0]);
1124 if (isl_seq_first_non_zero(bmap
->ineq
[i
]+pos
+1,
1125 bmap
->n_div
-div
-1) != -1)
1127 } else if (isl_int_abs_eq(bmap
->ineq
[i
][pos
], bmap
->div
[div
][0])) {
1128 if (!isl_seq_eq(bmap
->ineq
[i
], bmap
->div
[div
]+1, pos
))
1130 if (isl_seq_first_non_zero(bmap
->ineq
[i
]+pos
+1,
1131 bmap
->n_div
-div
-1) != -1)
1137 for (i
= 0; i
< bmap
->n_div
; ++i
)
1138 if (!isl_int_is_zero(bmap
->div
[i
][1+pos
]))
1145 * Remove divs that don't occur in any of the constraints or other divs.
1146 * These can arise when dropping some of the variables in a quast
1147 * returned by piplib.
1149 static struct isl_basic_map
*remove_redundant_divs(struct isl_basic_map
*bmap
)
1156 for (i
= bmap
->n_div
-1; i
>= 0; --i
) {
1157 if (!div_is_redundant(bmap
, i
))
1159 bmap
= isl_basic_map_drop_div(bmap
, i
);
1164 struct isl_basic_map
*isl_basic_map_finalize(struct isl_basic_map
*bmap
)
1166 bmap
= remove_redundant_divs(bmap
);
1169 ISL_F_SET(bmap
, ISL_BASIC_SET_FINAL
);
1173 struct isl_basic_set
*isl_basic_set_finalize(struct isl_basic_set
*bset
)
1175 return (struct isl_basic_set
*)
1176 isl_basic_map_finalize((struct isl_basic_map
*)bset
);
1179 struct isl_set
*isl_set_finalize(struct isl_set
*set
)
1185 for (i
= 0; i
< set
->n
; ++i
) {
1186 set
->p
[i
] = isl_basic_set_finalize(set
->p
[i
]);
1196 struct isl_map
*isl_map_finalize(struct isl_map
*map
)
1202 for (i
= 0; i
< map
->n
; ++i
) {
1203 map
->p
[i
] = isl_basic_map_finalize(map
->p
[i
]);
1207 ISL_F_CLR(map
, ISL_MAP_NORMALIZED
);
1215 /* Remove definition of any div that is defined in terms of the given variable.
1216 * The div itself is not removed. Functions such as
1217 * eliminate_divs_ineq depend on the other divs remaining in place.
1219 static struct isl_basic_map
*remove_dependent_vars(struct isl_basic_map
*bmap
,
1224 for (i
= 0; i
< bmap
->n_div
; ++i
) {
1225 if (isl_int_is_zero(bmap
->div
[i
][0]))
1227 if (isl_int_is_zero(bmap
->div
[i
][1+1+pos
]))
1229 isl_int_set_si(bmap
->div
[i
][0], 0);
1234 /* Eliminate the specified variables from the constraints using
1235 * Fourier-Motzkin. The variables themselves are not removed.
1237 struct isl_basic_map
*isl_basic_map_eliminate_vars(
1238 struct isl_basic_map
*bmap
, unsigned pos
, unsigned n
)
1248 total
= isl_basic_map_total_dim(bmap
);
1250 bmap
= isl_basic_map_cow(bmap
);
1251 for (d
= pos
+ n
- 1; d
>= 0 && d
>= pos
; --d
)
1252 bmap
= remove_dependent_vars(bmap
, d
);
1254 for (d
= pos
+ n
- 1;
1255 d
>= 0 && d
>= total
- bmap
->n_div
&& d
>= pos
; --d
)
1256 isl_seq_clr(bmap
->div
[d
-(total
-bmap
->n_div
)], 2+total
);
1257 for (d
= pos
+ n
- 1; d
>= 0 && d
>= pos
; --d
) {
1258 int n_lower
, n_upper
;
1261 for (i
= 0; i
< bmap
->n_eq
; ++i
) {
1262 if (isl_int_is_zero(bmap
->eq
[i
][1+d
]))
1264 eliminate_var_using_equality(bmap
, d
, bmap
->eq
[i
], 0, NULL
);
1265 isl_basic_map_drop_equality(bmap
, i
);
1272 for (i
= 0; i
< bmap
->n_ineq
; ++i
) {
1273 if (isl_int_is_pos(bmap
->ineq
[i
][1+d
]))
1275 else if (isl_int_is_neg(bmap
->ineq
[i
][1+d
]))
1278 bmap
= isl_basic_map_extend_constraints(bmap
,
1279 0, n_lower
* n_upper
);
1280 for (i
= bmap
->n_ineq
- 1; i
>= 0; --i
) {
1282 if (isl_int_is_zero(bmap
->ineq
[i
][1+d
]))
1285 for (j
= 0; j
< i
; ++j
) {
1286 if (isl_int_is_zero(bmap
->ineq
[j
][1+d
]))
1289 if (isl_int_sgn(bmap
->ineq
[i
][1+d
]) ==
1290 isl_int_sgn(bmap
->ineq
[j
][1+d
]))
1292 k
= isl_basic_map_alloc_inequality(bmap
);
1295 isl_seq_cpy(bmap
->ineq
[k
], bmap
->ineq
[i
],
1297 isl_seq_elim(bmap
->ineq
[k
], bmap
->ineq
[j
],
1298 1+d
, 1+total
, NULL
);
1300 isl_basic_map_drop_inequality(bmap
, i
);
1303 if (n_lower
> 0 && n_upper
> 0) {
1304 bmap
= isl_basic_map_normalize_constraints(bmap
);
1305 bmap
= remove_duplicate_constraints(bmap
, NULL
);
1306 bmap
= isl_basic_map_gauss(bmap
, NULL
);
1307 bmap
= isl_basic_map_convex_hull(bmap
);
1310 if (ISL_F_ISSET(bmap
, ISL_BASIC_MAP_EMPTY
))
1314 ISL_F_CLR(bmap
, ISL_BASIC_MAP_NORMALIZED
);
1317 isl_basic_map_free(bmap
);
1321 struct isl_basic_set
*isl_basic_set_eliminate_vars(
1322 struct isl_basic_set
*bset
, unsigned pos
, unsigned n
)
1324 return (struct isl_basic_set
*)isl_basic_map_eliminate_vars(
1325 (struct isl_basic_map
*)bset
, pos
, n
);
1328 /* Don't assume equalities are in order, because align_divs
1329 * may have changed the order of the divs.
1331 static void compute_elimination_index(struct isl_basic_map
*bmap
, int *elim
)
1336 total
= isl_dim_total(bmap
->dim
);
1337 for (d
= 0; d
< total
; ++d
)
1339 for (i
= 0; i
< bmap
->n_eq
; ++i
) {
1340 for (d
= total
- 1; d
>= 0; --d
) {
1341 if (isl_int_is_zero(bmap
->eq
[i
][1+d
]))
1349 static void set_compute_elimination_index(struct isl_basic_set
*bset
, int *elim
)
1351 compute_elimination_index((struct isl_basic_map
*)bset
, elim
);
1354 static int reduced_using_equalities(isl_int
*dst
, isl_int
*src
,
1355 struct isl_basic_map
*bmap
, int *elim
)
1361 total
= isl_dim_total(bmap
->dim
);
1362 for (d
= total
- 1; d
>= 0; --d
) {
1363 if (isl_int_is_zero(src
[1+d
]))
1368 isl_seq_cpy(dst
, src
, 1 + total
);
1371 isl_seq_elim(dst
, bmap
->eq
[elim
[d
]], 1 + d
, 1 + total
, NULL
);
1376 static int set_reduced_using_equalities(isl_int
*dst
, isl_int
*src
,
1377 struct isl_basic_set
*bset
, int *elim
)
1379 return reduced_using_equalities(dst
, src
,
1380 (struct isl_basic_map
*)bset
, elim
);
1383 static struct isl_basic_set
*isl_basic_set_reduce_using_equalities(
1384 struct isl_basic_set
*bset
, struct isl_basic_set
*context
)
1389 if (!bset
|| !context
)
1392 bset
= isl_basic_set_cow(bset
);
1396 elim
= isl_alloc_array(ctx
, int, isl_basic_set_n_dim(bset
));
1399 set_compute_elimination_index(context
, elim
);
1400 for (i
= 0; i
< bset
->n_eq
; ++i
)
1401 set_reduced_using_equalities(bset
->eq
[i
], bset
->eq
[i
],
1403 for (i
= 0; i
< bset
->n_ineq
; ++i
)
1404 set_reduced_using_equalities(bset
->ineq
[i
], bset
->ineq
[i
],
1406 isl_basic_set_free(context
);
1408 bset
= isl_basic_set_simplify(bset
);
1409 bset
= isl_basic_set_finalize(bset
);
1412 isl_basic_set_free(bset
);
1413 isl_basic_set_free(context
);
1417 static struct isl_basic_set
*remove_shifted_constraints(
1418 struct isl_basic_set
*bset
, struct isl_basic_set
*context
)
1428 size
= round_up(4 * (context
->n_ineq
+1) / 3 - 1);
1429 bits
= ffs(size
) - 1;
1430 index
= isl_calloc_array(ctx
, isl_int
**, size
);
1434 for (k
= 0; k
< context
->n_ineq
; ++k
) {
1435 h
= set_hash_index(index
, size
, bits
, context
, k
);
1436 index
[h
] = &context
->ineq
[k
];
1438 for (k
= 0; k
< bset
->n_ineq
; ++k
) {
1439 h
= set_hash_index(index
, size
, bits
, bset
, k
);
1442 l
= index
[h
] - &context
->ineq
[0];
1443 if (isl_int_lt(bset
->ineq
[k
][0], context
->ineq
[l
][0]))
1445 bset
= isl_basic_set_cow(bset
);
1448 isl_basic_set_drop_inequality(bset
, k
);
1458 /* Tighten (decrease) the constant terms of the inequalities based
1459 * on the equalities, without removing any integer points.
1460 * For example, if there is an equality
1468 * then we want to replace the inequality by
1472 * We do this by computing a variable compression and translating
1473 * the constraints to the compressed space.
1474 * If any constraint has coefficients (except the contant term)
1475 * with a common factor "f", then we can replace the constant term "c"
1482 * f * floor(c/f) - c = -fract(c/f)
1484 * and we can add the same value to the original constraint.
1486 * In the example, the compressed space only contains "j",
1487 * and the inequality translates to
1491 * We add -fract(-1/3) = -2 to the original constraint to obtain
1495 static struct isl_basic_set
*normalize_constraints_in_compressed_space(
1496 struct isl_basic_set
*bset
)
1500 struct isl_mat
*B
, *C
;
1506 if (ISL_F_ISSET(bset
, ISL_BASIC_SET_RATIONAL
))
1512 bset
= isl_basic_set_cow(bset
);
1516 total
= isl_basic_set_total_dim(bset
);
1517 B
= isl_mat_sub_alloc(bset
->ctx
, bset
->eq
, 0, bset
->n_eq
, 0, 1 + total
);
1518 C
= isl_mat_variable_compression(B
, NULL
);
1521 if (C
->n_col
== 0) {
1523 return isl_basic_set_set_to_empty(bset
);
1525 B
= isl_mat_sub_alloc(bset
->ctx
, bset
->ineq
,
1526 0, bset
->n_ineq
, 0, 1 + total
);
1527 C
= isl_mat_product(B
, C
);
1532 for (i
= 0; i
< bset
->n_ineq
; ++i
) {
1533 isl_seq_gcd(C
->row
[i
] + 1, C
->n_col
- 1, &gcd
);
1534 if (isl_int_is_one(gcd
))
1536 isl_int_fdiv_r(C
->row
[i
][0], C
->row
[i
][0], gcd
);
1537 isl_int_sub(bset
->ineq
[i
][0], bset
->ineq
[i
][0], C
->row
[i
][0]);
1546 /* Remove all information from bset that is redundant in the context
1547 * of context. In particular, equalities that are linear combinations
1548 * of those in context are removed. Then the inequalities that are
1549 * redundant in the context of the equalities and inequalities of
1550 * context are removed.
1552 * We first simplify the constraints of "bset" in the context of the
1553 * equalities of "context".
1554 * Then we simplify the inequalities of the context in the context
1555 * of the equalities of bset and remove the inequalities from "bset"
1556 * that are obviously redundant with respect to some inequality in "context".
1558 * If there are any inequalities left, we construct a tableau for
1559 * the context and then add the inequalities of "bset".
1560 * Before adding these equalities, we freeze all constraints such that
1561 * they won't be considered redundant in terms of the constraints of "bset".
1562 * Then we detect all equalities and redundant constraints (among the
1563 * constraints that weren't frozen) and update bset according to the results.
1564 * We have to be careful here because we don't want any of the context
1565 * constraints to remain and because we haven't added the equalities of "bset"
1566 * to the tableau so we temporarily have to pretend that there were no
1569 static struct isl_basic_set
*uset_gist(struct isl_basic_set
*bset
,
1570 struct isl_basic_set
*context
)
1573 struct isl_tab
*tab
;
1574 unsigned context_ineq
;
1575 struct isl_basic_set
*combined
= NULL
;
1577 if (!context
|| !bset
)
1580 if (context
->n_eq
> 0)
1581 bset
= isl_basic_set_reduce_using_equalities(bset
,
1582 isl_basic_set_copy(context
));
1585 if (isl_basic_set_fast_is_empty(bset
))
1590 if (bset
->n_eq
> 0) {
1591 struct isl_basic_set
*affine_hull
;
1592 affine_hull
= isl_basic_set_copy(bset
);
1593 affine_hull
= isl_basic_set_cow(affine_hull
);
1596 isl_basic_set_free_inequality(affine_hull
, affine_hull
->n_ineq
);
1597 context
= isl_basic_set_intersect(context
, affine_hull
);
1598 context
= isl_basic_set_gauss(context
, NULL
);
1599 context
= normalize_constraints_in_compressed_space(context
);
1603 if (ISL_F_ISSET(context
, ISL_BASIC_SET_EMPTY
)) {
1604 isl_basic_set_free(bset
);
1607 if (!context
->n_ineq
)
1609 bset
= remove_shifted_constraints(bset
, context
);
1612 context_ineq
= context
->n_ineq
;
1613 combined
= isl_basic_set_cow(isl_basic_set_copy(context
));
1614 if (isl_basic_set_free_equality(combined
, context
->n_eq
) < 0)
1616 combined
= isl_basic_set_extend_constraints(combined
,
1617 bset
->n_eq
, bset
->n_ineq
);
1618 tab
= isl_tab_from_basic_set(combined
);
1621 for (i
= 0; i
< context_ineq
; ++i
)
1622 if (isl_tab_freeze_constraint(tab
, i
) < 0)
1624 tab
= isl_tab_extend(tab
, bset
->n_ineq
);
1627 for (i
= 0; i
< bset
->n_ineq
; ++i
)
1628 if (isl_tab_add_ineq(tab
, bset
->ineq
[i
]) < 0)
1630 bset
= isl_basic_set_add_constraints(combined
, bset
, 0);
1631 tab
= isl_tab_detect_implicit_equalities(tab
);
1632 if (isl_tab_detect_redundant(tab
) < 0) {
1636 for (i
= 0; i
< context_ineq
; ++i
) {
1637 tab
->con
[i
].is_zero
= 0;
1638 tab
->con
[i
].is_redundant
= 1;
1640 bset
= isl_basic_set_update_from_tab(bset
, tab
);
1642 ISL_F_SET(bset
, ISL_BASIC_SET_NO_IMPLICIT
);
1643 ISL_F_SET(bset
, ISL_BASIC_SET_NO_REDUNDANT
);
1645 bset
= isl_basic_set_simplify(bset
);
1646 bset
= isl_basic_set_finalize(bset
);
1647 isl_basic_set_free(context
);
1650 isl_basic_set_free(combined
);
1652 isl_basic_set_free(bset
);
1653 isl_basic_set_free(context
);
1657 /* Normalize the divs in "bmap" in the context of the equalities in "context".
1658 * We simply add the equalities in context to bmap and then do a regular
1659 * div normalizations. Better results can be obtained by normalizing
1660 * only the divs in bmap than do not also appear in context.
1661 * We need to be careful to reduce the divs using the equalities
1662 * so that later calls to isl_basic_map_overlying_set wouldn't introduce
1663 * spurious constraints.
1665 static struct isl_basic_map
*normalize_divs_in_context(
1666 struct isl_basic_map
*bmap
, struct isl_basic_map
*context
)
1669 unsigned total_context
;
1672 div_eq
= n_pure_div_eq(bmap
);
1676 if (context
->n_div
> 0)
1677 bmap
= isl_basic_map_align_divs(bmap
, context
);
1679 total_context
= isl_basic_map_total_dim(context
);
1680 bmap
= isl_basic_map_extend_constraints(bmap
, context
->n_eq
, 0);
1681 for (i
= 0; i
< context
->n_eq
; ++i
) {
1683 k
= isl_basic_map_alloc_equality(bmap
);
1684 isl_seq_cpy(bmap
->eq
[k
], context
->eq
[i
], 1 + total_context
);
1685 isl_seq_clr(bmap
->eq
[k
] + 1 + total_context
,
1686 isl_basic_map_total_dim(bmap
) - total_context
);
1688 bmap
= isl_basic_map_gauss(bmap
, NULL
);
1689 bmap
= normalize_divs(bmap
, NULL
);
1690 bmap
= isl_basic_map_gauss(bmap
, NULL
);
1694 struct isl_basic_map
*isl_basic_map_gist(struct isl_basic_map
*bmap
,
1695 struct isl_basic_map
*context
)
1697 struct isl_basic_set
*bset
;
1699 if (!bmap
|| !context
)
1702 if (isl_basic_map_is_universe(context
)) {
1703 isl_basic_map_free(context
);
1706 if (isl_basic_map_is_universe(bmap
)) {
1707 isl_basic_map_free(context
);
1710 if (isl_basic_map_fast_is_empty(context
)) {
1711 struct isl_dim
*dim
= isl_dim_copy(bmap
->dim
);
1712 isl_basic_map_free(context
);
1713 isl_basic_map_free(bmap
);
1714 return isl_basic_map_universe(dim
);
1716 if (isl_basic_map_fast_is_empty(bmap
)) {
1717 isl_basic_map_free(context
);
1721 bmap
= isl_basic_map_convex_hull(bmap
);
1722 context
= isl_basic_map_convex_hull(context
);
1725 bmap
= normalize_divs_in_context(bmap
, context
);
1727 context
= isl_basic_map_align_divs(context
, bmap
);
1728 bmap
= isl_basic_map_align_divs(bmap
, context
);
1730 bset
= uset_gist(isl_basic_map_underlying_set(isl_basic_map_copy(bmap
)),
1731 isl_basic_map_underlying_set(context
));
1733 return isl_basic_map_overlying_set(bset
, bmap
);
1735 isl_basic_map_free(bmap
);
1736 isl_basic_map_free(context
);
1741 * Assumes context has no implicit divs.
1743 __isl_give isl_map
*isl_map_gist_basic_map(__isl_take isl_map
*map
,
1744 __isl_take isl_basic_map
*context
)
1748 if (!map
|| !context
)
1751 if (isl_basic_map_is_universe(context
)) {
1752 isl_basic_map_free(context
);
1755 if (isl_basic_map_fast_is_empty(context
)) {
1756 struct isl_dim
*dim
= isl_dim_copy(map
->dim
);
1757 isl_basic_map_free(context
);
1759 return isl_map_universe(dim
);
1762 context
= isl_basic_map_convex_hull(context
);
1763 map
= isl_map_cow(map
);
1764 if (!map
|| !context
)
1766 isl_assert(map
->ctx
, isl_dim_equal(map
->dim
, context
->dim
), goto error
);
1767 map
= isl_map_compute_divs(map
);
1768 for (i
= 0; i
< map
->n
; ++i
)
1769 context
= isl_basic_map_align_divs(context
, map
->p
[i
]);
1770 for (i
= 0; i
< map
->n
; ++i
) {
1771 map
->p
[i
] = isl_basic_map_gist(map
->p
[i
],
1772 isl_basic_map_copy(context
));
1776 isl_basic_map_free(context
);
1777 ISL_F_CLR(map
, ISL_MAP_NORMALIZED
);
1781 isl_basic_map_free(context
);
1785 __isl_give isl_map
*isl_map_gist(__isl_take isl_map
*map
,
1786 __isl_take isl_map
*context
)
1788 return isl_map_gist_basic_map(map
, isl_map_convex_hull(context
));
1791 struct isl_basic_set
*isl_basic_set_gist(struct isl_basic_set
*bset
,
1792 struct isl_basic_set
*context
)
1794 return (struct isl_basic_set
*)isl_basic_map_gist(
1795 (struct isl_basic_map
*)bset
, (struct isl_basic_map
*)context
);
1798 __isl_give isl_set
*isl_set_gist_basic_set(__isl_take isl_set
*set
,
1799 __isl_take isl_basic_set
*context
)
1801 return (struct isl_set
*)isl_map_gist_basic_map((struct isl_map
*)set
,
1802 (struct isl_basic_map
*)context
);
1805 __isl_give isl_set
*isl_set_gist(__isl_take isl_set
*set
,
1806 __isl_take isl_set
*context
)
1808 return (struct isl_set
*)isl_map_gist((struct isl_map
*)set
,
1809 (struct isl_map
*)context
);
1812 /* Quick check to see if two basic maps are disjoint.
1813 * In particular, we reduce the equalities and inequalities of
1814 * one basic map in the context of the equalities of the other
1815 * basic map and check if we get a contradiction.
1817 int isl_basic_map_fast_is_disjoint(struct isl_basic_map
*bmap1
,
1818 struct isl_basic_map
*bmap2
)
1820 struct isl_vec
*v
= NULL
;
1825 if (!bmap1
|| !bmap2
)
1827 isl_assert(bmap1
->ctx
, isl_dim_equal(bmap1
->dim
, bmap2
->dim
),
1829 if (bmap1
->n_div
|| bmap2
->n_div
)
1831 if (!bmap1
->n_eq
&& !bmap2
->n_eq
)
1834 total
= isl_dim_total(bmap1
->dim
);
1837 v
= isl_vec_alloc(bmap1
->ctx
, 1 + total
);
1840 elim
= isl_alloc_array(bmap1
->ctx
, int, total
);
1843 compute_elimination_index(bmap1
, elim
);
1844 for (i
= 0; i
< bmap2
->n_eq
; ++i
) {
1846 reduced
= reduced_using_equalities(v
->block
.data
, bmap2
->eq
[i
],
1848 if (reduced
&& !isl_int_is_zero(v
->block
.data
[0]) &&
1849 isl_seq_first_non_zero(v
->block
.data
+ 1, total
) == -1)
1852 for (i
= 0; i
< bmap2
->n_ineq
; ++i
) {
1854 reduced
= reduced_using_equalities(v
->block
.data
,
1855 bmap2
->ineq
[i
], bmap1
, elim
);
1856 if (reduced
&& isl_int_is_neg(v
->block
.data
[0]) &&
1857 isl_seq_first_non_zero(v
->block
.data
+ 1, total
) == -1)
1860 compute_elimination_index(bmap2
, elim
);
1861 for (i
= 0; i
< bmap1
->n_ineq
; ++i
) {
1863 reduced
= reduced_using_equalities(v
->block
.data
,
1864 bmap1
->ineq
[i
], bmap2
, elim
);
1865 if (reduced
&& isl_int_is_neg(v
->block
.data
[0]) &&
1866 isl_seq_first_non_zero(v
->block
.data
+ 1, total
) == -1)
1882 int isl_basic_set_fast_is_disjoint(struct isl_basic_set
*bset1
,
1883 struct isl_basic_set
*bset2
)
1885 return isl_basic_map_fast_is_disjoint((struct isl_basic_map
*)bset1
,
1886 (struct isl_basic_map
*)bset2
);
1889 int isl_map_fast_is_disjoint(struct isl_map
*map1
, struct isl_map
*map2
)
1896 if (isl_map_fast_is_equal(map1
, map2
))
1899 for (i
= 0; i
< map1
->n
; ++i
) {
1900 for (j
= 0; j
< map2
->n
; ++j
) {
1901 int d
= isl_basic_map_fast_is_disjoint(map1
->p
[i
],
1910 int isl_set_fast_is_disjoint(struct isl_set
*set1
, struct isl_set
*set2
)
1912 return isl_map_fast_is_disjoint((struct isl_map
*)set1
,
1913 (struct isl_map
*)set2
);
1916 /* Check if we can combine a given div with lower bound l and upper
1917 * bound u with some other div and if so return that other div.
1918 * Otherwise return -1.
1920 * We first check that
1921 * - the bounds are opposites of each other (except for the constant
1923 * - the bounds do not reference any other div
1924 * - no div is defined in terms of this div
1926 * Let m be the size of the range allowed on the div by the bounds.
1927 * That is, the bounds are of the form
1929 * e <= a <= e + m - 1
1931 * with e some expression in the other variables.
1932 * We look for another div b such that no third div is defined in terms
1933 * of this second div b and such that in any constraint that contains
1934 * a (except for the given lower and upper bound), also contains b
1935 * with a coefficient that is m times that of b.
1936 * That is, all constraints (execpt for the lower and upper bound)
1939 * e + f (a + m b) >= 0
1941 * If so, we return b so that "a + m b" can be replaced by
1942 * a single div "c = a + m b".
1944 static int div_find_coalesce(struct isl_basic_map
*bmap
, int *pairs
,
1945 unsigned div
, unsigned l
, unsigned u
)
1951 if (bmap
->n_div
<= 1)
1953 dim
= isl_dim_total(bmap
->dim
);
1954 if (isl_seq_first_non_zero(bmap
->ineq
[l
] + 1 + dim
, div
) != -1)
1956 if (isl_seq_first_non_zero(bmap
->ineq
[l
] + 1 + dim
+ div
+ 1,
1957 bmap
->n_div
- div
- 1) != -1)
1959 if (!isl_seq_is_neg(bmap
->ineq
[l
] + 1, bmap
->ineq
[u
] + 1,
1963 for (i
= 0; i
< bmap
->n_div
; ++i
) {
1964 if (isl_int_is_zero(bmap
->div
[i
][0]))
1966 if (!isl_int_is_zero(bmap
->div
[i
][1 + 1 + dim
+ div
]))
1970 isl_int_add(bmap
->ineq
[l
][0], bmap
->ineq
[l
][0], bmap
->ineq
[u
][0]);
1971 if (isl_int_is_neg(bmap
->ineq
[l
][0])) {
1972 isl_int_sub(bmap
->ineq
[l
][0],
1973 bmap
->ineq
[l
][0], bmap
->ineq
[u
][0]);
1974 bmap
= isl_basic_map_copy(bmap
);
1975 bmap
= isl_basic_map_set_to_empty(bmap
);
1976 isl_basic_map_free(bmap
);
1979 isl_int_add_ui(bmap
->ineq
[l
][0], bmap
->ineq
[l
][0], 1);
1980 for (i
= 0; i
< bmap
->n_div
; ++i
) {
1985 for (j
= 0; j
< bmap
->n_div
; ++j
) {
1986 if (isl_int_is_zero(bmap
->div
[j
][0]))
1988 if (!isl_int_is_zero(bmap
->div
[j
][1 + 1 + dim
+ i
]))
1991 if (j
< bmap
->n_div
)
1993 for (j
= 0; j
< bmap
->n_ineq
; ++j
) {
1995 if (j
== l
|| j
== u
)
1997 if (isl_int_is_zero(bmap
->ineq
[j
][1 + dim
+ div
]))
1999 if (isl_int_is_zero(bmap
->ineq
[j
][1 + dim
+ i
]))
2001 isl_int_mul(bmap
->ineq
[j
][1 + dim
+ div
],
2002 bmap
->ineq
[j
][1 + dim
+ div
],
2004 valid
= isl_int_eq(bmap
->ineq
[j
][1 + dim
+ div
],
2005 bmap
->ineq
[j
][1 + dim
+ i
]);
2006 isl_int_divexact(bmap
->ineq
[j
][1 + dim
+ div
],
2007 bmap
->ineq
[j
][1 + dim
+ div
],
2012 if (j
< bmap
->n_ineq
)
2017 isl_int_sub_ui(bmap
->ineq
[l
][0], bmap
->ineq
[l
][0], 1);
2018 isl_int_sub(bmap
->ineq
[l
][0], bmap
->ineq
[l
][0], bmap
->ineq
[u
][0]);
2022 /* Given a lower and an upper bound on div i, construct an inequality
2023 * that when nonnegative ensures that this pair of bounds always allows
2024 * for an integer value of the given div.
2025 * The lower bound is inequality l, while the upper bound is inequality u.
2026 * The constructed inequality is stored in ineq.
2027 * g, fl, fu are temporary scalars.
2029 * Let the upper bound be
2033 * and the lower bound
2037 * Let n_u = f_u g and n_l = f_l g, with g = gcd(n_u, n_l).
2040 * - f_u e_l <= f_u f_l g a <= f_l e_u
2042 * Since all variables are integer valued, this is equivalent to
2044 * - f_u e_l - (f_u - 1) <= f_u f_l g a <= f_l e_u + (f_l - 1)
2046 * If this interval is at least f_u f_l g, then it contains at least
2047 * one integer value for a.
2048 * That is, the test constraint is
2050 * f_l e_u + f_u e_l + f_l - 1 + f_u - 1 + 1 >= f_u f_l g
2052 static void construct_test_ineq(struct isl_basic_map
*bmap
, int i
,
2053 int l
, int u
, isl_int
*ineq
, isl_int g
, isl_int fl
, isl_int fu
)
2056 dim
= isl_dim_total(bmap
->dim
);
2058 isl_int_gcd(g
, bmap
->ineq
[l
][1 + dim
+ i
], bmap
->ineq
[u
][1 + dim
+ i
]);
2059 isl_int_divexact(fl
, bmap
->ineq
[l
][1 + dim
+ i
], g
);
2060 isl_int_divexact(fu
, bmap
->ineq
[u
][1 + dim
+ i
], g
);
2061 isl_int_neg(fu
, fu
);
2062 isl_seq_combine(ineq
, fl
, bmap
->ineq
[u
], fu
, bmap
->ineq
[l
],
2063 1 + dim
+ bmap
->n_div
);
2064 isl_int_add(ineq
[0], ineq
[0], fl
);
2065 isl_int_add(ineq
[0], ineq
[0], fu
);
2066 isl_int_sub_ui(ineq
[0], ineq
[0], 1);
2067 isl_int_mul(g
, g
, fl
);
2068 isl_int_mul(g
, g
, fu
);
2069 isl_int_sub(ineq
[0], ineq
[0], g
);
2072 /* Remove more kinds of divs that are not strictly needed.
2073 * In particular, if all pairs of lower and upper bounds on a div
2074 * are such that they allow at least one integer value of the div,
2075 * the we can eliminate the div using Fourier-Motzkin without
2076 * introducing any spurious solutions.
2078 static struct isl_basic_map
*drop_more_redundant_divs(
2079 struct isl_basic_map
*bmap
, int *pairs
, int n
)
2081 struct isl_tab
*tab
= NULL
;
2082 struct isl_vec
*vec
= NULL
;
2094 dim
= isl_dim_total(bmap
->dim
);
2095 vec
= isl_vec_alloc(bmap
->ctx
, 1 + dim
+ bmap
->n_div
);
2099 tab
= isl_tab_from_basic_map(bmap
);
2104 enum isl_lp_result res
;
2106 for (i
= 0; i
< bmap
->n_div
; ++i
) {
2109 if (best
>= 0 && pairs
[best
] <= pairs
[i
])
2115 for (l
= 0; l
< bmap
->n_ineq
; ++l
) {
2116 if (!isl_int_is_pos(bmap
->ineq
[l
][1 + dim
+ i
]))
2118 for (u
= 0; u
< bmap
->n_ineq
; ++u
) {
2119 if (!isl_int_is_neg(bmap
->ineq
[u
][1 + dim
+ i
]))
2121 construct_test_ineq(bmap
, i
, l
, u
,
2122 vec
->el
, g
, fl
, fu
);
2123 res
= isl_tab_min(tab
, vec
->el
,
2124 bmap
->ctx
->one
, &g
, NULL
, 0);
2125 if (res
== isl_lp_error
)
2127 if (res
== isl_lp_empty
) {
2128 bmap
= isl_basic_map_set_to_empty(bmap
);
2131 if (res
!= isl_lp_ok
|| isl_int_is_neg(g
))
2134 if (u
< bmap
->n_ineq
)
2137 if (l
== bmap
->n_ineq
) {
2157 bmap
= isl_basic_map_remove(bmap
, isl_dim_div
, remove
, 1);
2158 return isl_basic_map_drop_redundant_divs(bmap
);
2161 isl_basic_map_free(bmap
);
2170 /* Given a pair of divs div1 and div2 such that, expect for the lower bound l
2171 * and the upper bound u, div1 always occurs together with div2 in the form
2172 * (div1 + m div2), where m is the constant range on the variable div1
2173 * allowed by l and u, replace the pair div1 and div2 by a single
2174 * div that is equal to div1 + m div2.
2176 * The new div will appear in the location that contains div2.
2177 * We need to modify all constraints that contain
2178 * div2 = (div - div1) / m
2179 * (If a constraint does not contain div2, it will also not contain div1.)
2180 * If the constraint also contains div1, then we know they appear
2181 * as f (div1 + m div2) and we can simply replace (div1 + m div2) by div,
2182 * i.e., the coefficient of div is f.
2184 * Otherwise, we first need to introduce div1 into the constraint.
2193 * A lower bound on div2
2197 * can be replaced by
2199 * (n * (m div 2 + div1) + m t + n f)/g >= 0
2201 * with g = gcd(m,n).
2206 * can be replaced by
2208 * (-n * (m div2 + div1) + m t + n f')/g >= 0
2210 * These constraint are those that we would obtain from eliminating
2211 * div1 using Fourier-Motzkin.
2213 * After all constraints have been modified, we drop the lower and upper
2214 * bound and then drop div1.
2216 static struct isl_basic_map
*coalesce_divs(struct isl_basic_map
*bmap
,
2217 unsigned div1
, unsigned div2
, unsigned l
, unsigned u
)
2222 unsigned dim
, total
;
2225 dim
= isl_dim_total(bmap
->dim
);
2226 total
= 1 + dim
+ bmap
->n_div
;
2231 isl_int_add(m
, bmap
->ineq
[l
][0], bmap
->ineq
[u
][0]);
2232 isl_int_add_ui(m
, m
, 1);
2234 for (i
= 0; i
< bmap
->n_ineq
; ++i
) {
2235 if (i
== l
|| i
== u
)
2237 if (isl_int_is_zero(bmap
->ineq
[i
][1 + dim
+ div2
]))
2239 if (isl_int_is_zero(bmap
->ineq
[i
][1 + dim
+ div1
])) {
2240 isl_int_gcd(b
, m
, bmap
->ineq
[i
][1 + dim
+ div2
]);
2241 isl_int_divexact(a
, m
, b
);
2242 isl_int_divexact(b
, bmap
->ineq
[i
][1 + dim
+ div2
], b
);
2243 if (isl_int_is_pos(b
)) {
2244 isl_seq_combine(bmap
->ineq
[i
], a
, bmap
->ineq
[i
],
2245 b
, bmap
->ineq
[l
], total
);
2248 isl_seq_combine(bmap
->ineq
[i
], a
, bmap
->ineq
[i
],
2249 b
, bmap
->ineq
[u
], total
);
2252 isl_int_set(bmap
->ineq
[i
][1 + dim
+ div2
],
2253 bmap
->ineq
[i
][1 + dim
+ div1
]);
2254 isl_int_set_si(bmap
->ineq
[i
][1 + dim
+ div1
], 0);
2261 isl_basic_map_drop_inequality(bmap
, l
);
2262 isl_basic_map_drop_inequality(bmap
, u
);
2264 isl_basic_map_drop_inequality(bmap
, u
);
2265 isl_basic_map_drop_inequality(bmap
, l
);
2267 bmap
= isl_basic_map_drop_div(bmap
, div1
);
2271 /* First check if we can coalesce any pair of divs and
2272 * then continue with dropping more redundant divs.
2274 * We loop over all pairs of lower and upper bounds on a div
2275 * with coefficient 1 and -1, respectively, check if there
2276 * is any other div "c" with which we can coalesce the div
2277 * and if so, perform the coalescing.
2279 static struct isl_basic_map
*coalesce_or_drop_more_redundant_divs(
2280 struct isl_basic_map
*bmap
, int *pairs
, int n
)
2285 dim
= isl_dim_total(bmap
->dim
);
2287 for (i
= 0; i
< bmap
->n_div
; ++i
) {
2290 for (l
= 0; l
< bmap
->n_ineq
; ++l
) {
2291 if (!isl_int_is_one(bmap
->ineq
[l
][1 + dim
+ i
]))
2293 for (u
= 0; u
< bmap
->n_ineq
; ++u
) {
2296 if (!isl_int_is_negone(bmap
->ineq
[u
][1+dim
+i
]))
2298 c
= div_find_coalesce(bmap
, pairs
, i
, l
, u
);
2302 bmap
= coalesce_divs(bmap
, i
, c
, l
, u
);
2303 return isl_basic_map_drop_redundant_divs(bmap
);
2308 if (ISL_F_ISSET(bmap
, ISL_BASIC_MAP_EMPTY
))
2311 return drop_more_redundant_divs(bmap
, pairs
, n
);
2314 /* Remove divs that are not strictly needed.
2315 * In particular, if a div only occurs positively (or negatively)
2316 * in constraints, then it can simply be dropped.
2317 * Also, if a div occurs only occurs in two constraints and if moreover
2318 * those two constraints are opposite to each other, except for the constant
2319 * term and if the sum of the constant terms is such that for any value
2320 * of the other values, there is always at least one integer value of the
2321 * div, i.e., if one plus this sum is greater than or equal to
2322 * the (absolute value) of the coefficent of the div in the constraints,
2323 * then we can also simply drop the div.
2325 * If any divs are left after these simple checks then we move on
2326 * to more complicated cases in drop_more_redundant_divs.
2328 struct isl_basic_map
*isl_basic_map_drop_redundant_divs(
2329 struct isl_basic_map
*bmap
)
2339 off
= isl_dim_total(bmap
->dim
);
2340 pairs
= isl_calloc_array(bmap
->ctx
, int, bmap
->n_div
);
2344 for (i
= 0; i
< bmap
->n_div
; ++i
) {
2346 int last_pos
, last_neg
;
2350 defined
= !isl_int_is_zero(bmap
->div
[i
][0]);
2351 for (j
= 0; j
< bmap
->n_eq
; ++j
)
2352 if (!isl_int_is_zero(bmap
->eq
[j
][1 + off
+ i
]))
2358 for (j
= 0; j
< bmap
->n_ineq
; ++j
) {
2359 if (isl_int_is_pos(bmap
->ineq
[j
][1 + off
+ i
])) {
2363 if (isl_int_is_neg(bmap
->ineq
[j
][1 + off
+ i
])) {
2368 pairs
[i
] = pos
* neg
;
2369 if (pairs
[i
] == 0) {
2370 for (j
= bmap
->n_ineq
- 1; j
>= 0; --j
)
2371 if (!isl_int_is_zero(bmap
->ineq
[j
][1+off
+i
]))
2372 isl_basic_map_drop_inequality(bmap
, j
);
2373 bmap
= isl_basic_map_drop_div(bmap
, i
);
2375 return isl_basic_map_drop_redundant_divs(bmap
);
2379 if (!isl_seq_is_neg(bmap
->ineq
[last_pos
] + 1,
2380 bmap
->ineq
[last_neg
] + 1,
2384 isl_int_add(bmap
->ineq
[last_pos
][0],
2385 bmap
->ineq
[last_pos
][0], bmap
->ineq
[last_neg
][0]);
2386 isl_int_add_ui(bmap
->ineq
[last_pos
][0],
2387 bmap
->ineq
[last_pos
][0], 1);
2388 redundant
= isl_int_ge(bmap
->ineq
[last_pos
][0],
2389 bmap
->ineq
[last_pos
][1+off
+i
]);
2390 isl_int_sub_ui(bmap
->ineq
[last_pos
][0],
2391 bmap
->ineq
[last_pos
][0], 1);
2392 isl_int_sub(bmap
->ineq
[last_pos
][0],
2393 bmap
->ineq
[last_pos
][0], bmap
->ineq
[last_neg
][0]);
2396 !ok_to_set_div_from_bound(bmap
, i
, last_pos
)) {
2401 bmap
= set_div_from_lower_bound(bmap
, i
, last_pos
);
2402 bmap
= isl_basic_map_simplify(bmap
);
2404 return isl_basic_map_drop_redundant_divs(bmap
);
2406 if (last_pos
> last_neg
) {
2407 isl_basic_map_drop_inequality(bmap
, last_pos
);
2408 isl_basic_map_drop_inequality(bmap
, last_neg
);
2410 isl_basic_map_drop_inequality(bmap
, last_neg
);
2411 isl_basic_map_drop_inequality(bmap
, last_pos
);
2413 bmap
= isl_basic_map_drop_div(bmap
, i
);
2415 return isl_basic_map_drop_redundant_divs(bmap
);
2419 return coalesce_or_drop_more_redundant_divs(bmap
, pairs
, n
);
2425 isl_basic_map_free(bmap
);
2429 struct isl_basic_set
*isl_basic_set_drop_redundant_divs(
2430 struct isl_basic_set
*bset
)
2432 return (struct isl_basic_set
*)
2433 isl_basic_map_drop_redundant_divs((struct isl_basic_map
*)bset
);
2436 struct isl_map
*isl_map_drop_redundant_divs(struct isl_map
*map
)
2442 for (i
= 0; i
< map
->n
; ++i
) {
2443 map
->p
[i
] = isl_basic_map_drop_redundant_divs(map
->p
[i
]);
2447 ISL_F_CLR(map
, ISL_MAP_NORMALIZED
);
2454 struct isl_set
*isl_set_drop_redundant_divs(struct isl_set
*set
)
2456 return (struct isl_set
*)
2457 isl_map_drop_redundant_divs((struct isl_map
*)set
);