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 __isl_give isl_basic_set
*isl_basic_set_drop(__isl_take isl_basic_set
*bset
,
208 enum isl_dim_type type
, unsigned first
, unsigned n
)
210 return (isl_basic_set
*)isl_basic_map_drop((isl_basic_map
*)bset
,
214 struct isl_basic_map
*isl_basic_map_drop_inputs(
215 struct isl_basic_map
*bmap
, unsigned first
, unsigned n
)
217 return isl_basic_map_drop(bmap
, isl_dim_in
, first
, n
);
220 struct isl_map
*isl_map_drop(struct isl_map
*map
,
221 enum isl_dim_type type
, unsigned first
, unsigned n
)
228 isl_assert(map
->ctx
, first
+ n
<= isl_map_dim(map
, type
), goto error
);
232 map
= isl_map_cow(map
);
235 map
->dim
= isl_dim_drop(map
->dim
, type
, first
, n
);
239 for (i
= 0; i
< map
->n
; ++i
) {
240 map
->p
[i
] = isl_basic_map_drop(map
->p
[i
], type
, first
, n
);
244 ISL_F_CLR(map
, ISL_MAP_NORMALIZED
);
252 struct isl_set
*isl_set_drop(struct isl_set
*set
,
253 enum isl_dim_type type
, unsigned first
, unsigned n
)
255 return (isl_set
*)isl_map_drop((isl_map
*)set
, type
, first
, n
);
258 struct isl_map
*isl_map_drop_inputs(
259 struct isl_map
*map
, unsigned first
, unsigned n
)
261 return isl_map_drop(map
, isl_dim_in
, first
, n
);
265 * We don't cow, as the div is assumed to be redundant.
267 static struct isl_basic_map
*isl_basic_map_drop_div(
268 struct isl_basic_map
*bmap
, unsigned div
)
276 pos
= 1 + isl_dim_total(bmap
->dim
) + div
;
278 isl_assert(bmap
->ctx
, div
< bmap
->n_div
, goto error
);
280 for (i
= 0; i
< bmap
->n_eq
; ++i
)
281 constraint_drop_vars(bmap
->eq
[i
]+pos
, 1, bmap
->extra
-div
-1);
283 for (i
= 0; i
< bmap
->n_ineq
; ++i
) {
284 if (!isl_int_is_zero(bmap
->ineq
[i
][pos
])) {
285 isl_basic_map_drop_inequality(bmap
, i
);
289 constraint_drop_vars(bmap
->ineq
[i
]+pos
, 1, bmap
->extra
-div
-1);
292 for (i
= 0; i
< bmap
->n_div
; ++i
)
293 constraint_drop_vars(bmap
->div
[i
]+1+pos
, 1, bmap
->extra
-div
-1);
295 if (div
!= bmap
->n_div
- 1) {
297 isl_int
*t
= bmap
->div
[div
];
299 for (j
= div
; j
< bmap
->n_div
- 1; ++j
)
300 bmap
->div
[j
] = bmap
->div
[j
+1];
302 bmap
->div
[bmap
->n_div
- 1] = t
;
304 ISL_F_CLR(bmap
, ISL_BASIC_MAP_NORMALIZED
);
305 isl_basic_map_free_div(bmap
, 1);
309 isl_basic_map_free(bmap
);
313 struct isl_basic_map
*isl_basic_map_normalize_constraints(
314 struct isl_basic_map
*bmap
)
318 unsigned total
= isl_basic_map_total_dim(bmap
);
324 for (i
= bmap
->n_eq
- 1; i
>= 0; --i
) {
325 isl_seq_gcd(bmap
->eq
[i
]+1, total
, &gcd
);
326 if (isl_int_is_zero(gcd
)) {
327 if (!isl_int_is_zero(bmap
->eq
[i
][0])) {
328 bmap
= isl_basic_map_set_to_empty(bmap
);
331 isl_basic_map_drop_equality(bmap
, i
);
334 if (ISL_F_ISSET(bmap
, ISL_BASIC_MAP_RATIONAL
))
335 isl_int_gcd(gcd
, gcd
, bmap
->eq
[i
][0]);
336 if (isl_int_is_one(gcd
))
338 if (!isl_int_is_divisible_by(bmap
->eq
[i
][0], gcd
)) {
339 bmap
= isl_basic_map_set_to_empty(bmap
);
342 isl_seq_scale_down(bmap
->eq
[i
], bmap
->eq
[i
], gcd
, 1+total
);
345 for (i
= bmap
->n_ineq
- 1; i
>= 0; --i
) {
346 isl_seq_gcd(bmap
->ineq
[i
]+1, total
, &gcd
);
347 if (isl_int_is_zero(gcd
)) {
348 if (isl_int_is_neg(bmap
->ineq
[i
][0])) {
349 bmap
= isl_basic_map_set_to_empty(bmap
);
352 isl_basic_map_drop_inequality(bmap
, i
);
355 if (ISL_F_ISSET(bmap
, ISL_BASIC_MAP_RATIONAL
))
356 isl_int_gcd(gcd
, gcd
, bmap
->ineq
[i
][0]);
357 if (isl_int_is_one(gcd
))
359 isl_int_fdiv_q(bmap
->ineq
[i
][0], bmap
->ineq
[i
][0], gcd
);
360 isl_seq_scale_down(bmap
->ineq
[i
]+1, bmap
->ineq
[i
]+1, gcd
, total
);
367 struct isl_basic_set
*isl_basic_set_normalize_constraints(
368 struct isl_basic_set
*bset
)
370 return (struct isl_basic_set
*)isl_basic_map_normalize_constraints(
371 (struct isl_basic_map
*)bset
);
374 /* Assumes divs have been ordered if keep_divs is set.
376 static void eliminate_var_using_equality(struct isl_basic_map
*bmap
,
377 unsigned pos
, isl_int
*eq
, int keep_divs
, int *progress
)
383 total
= isl_basic_map_total_dim(bmap
);
384 last_div
= isl_seq_last_non_zero(eq
+ 1 + isl_dim_total(bmap
->dim
),
386 for (k
= 0; k
< bmap
->n_eq
; ++k
) {
387 if (bmap
->eq
[k
] == eq
)
389 if (isl_int_is_zero(bmap
->eq
[k
][1+pos
]))
393 isl_seq_elim(bmap
->eq
[k
], eq
, 1+pos
, 1+total
, NULL
);
396 for (k
= 0; k
< bmap
->n_ineq
; ++k
) {
397 if (isl_int_is_zero(bmap
->ineq
[k
][1+pos
]))
401 isl_seq_elim(bmap
->ineq
[k
], eq
, 1+pos
, 1+total
, NULL
);
402 ISL_F_CLR(bmap
, ISL_BASIC_MAP_NORMALIZED
);
405 for (k
= 0; k
< bmap
->n_div
; ++k
) {
406 if (isl_int_is_zero(bmap
->div
[k
][0]))
408 if (isl_int_is_zero(bmap
->div
[k
][1+1+pos
]))
412 /* We need to be careful about circular definitions,
413 * so for now we just remove the definition of div k
414 * if the equality contains any divs.
415 * If keep_divs is set, then the divs have been ordered
416 * and we can keep the definition as long as the result
419 if (last_div
== -1 || (keep_divs
&& last_div
< k
))
420 isl_seq_elim(bmap
->div
[k
]+1, eq
,
421 1+pos
, 1+total
, &bmap
->div
[k
][0]);
423 isl_seq_clr(bmap
->div
[k
], 1 + total
);
424 ISL_F_CLR(bmap
, ISL_BASIC_MAP_NORMALIZED
);
428 /* Assumes divs have been ordered if keep_divs is set.
430 static void eliminate_div(struct isl_basic_map
*bmap
, isl_int
*eq
,
431 unsigned div
, int keep_divs
)
433 unsigned pos
= isl_dim_total(bmap
->dim
) + div
;
435 eliminate_var_using_equality(bmap
, pos
, eq
, keep_divs
, NULL
);
437 isl_basic_map_drop_div(bmap
, div
);
440 /* Check if elimination of div "div" using equality "eq" would not
441 * result in a div depending on a later div.
443 static int ok_to_eliminate_div(struct isl_basic_map
*bmap
, isl_int
*eq
,
448 unsigned pos
= isl_dim_total(bmap
->dim
) + div
;
450 last_div
= isl_seq_last_non_zero(eq
+ 1 + isl_dim_total(bmap
->dim
),
452 if (last_div
< 0 || last_div
<= div
)
455 for (k
= 0; k
<= last_div
; ++k
) {
456 if (isl_int_is_zero(bmap
->div
[k
][0]))
458 if (!isl_int_is_zero(bmap
->div
[k
][1 + 1 + pos
]))
465 /* Elimininate divs based on equalities
467 static struct isl_basic_map
*eliminate_divs_eq(
468 struct isl_basic_map
*bmap
, int *progress
)
475 bmap
= isl_basic_map_order_divs(bmap
);
480 off
= 1 + isl_dim_total(bmap
->dim
);
482 for (d
= bmap
->n_div
- 1; d
>= 0 ; --d
) {
483 for (i
= 0; i
< bmap
->n_eq
; ++i
) {
484 if (!isl_int_is_one(bmap
->eq
[i
][off
+ d
]) &&
485 !isl_int_is_negone(bmap
->eq
[i
][off
+ d
]))
487 if (!ok_to_eliminate_div(bmap
, bmap
->eq
[i
], d
))
491 eliminate_div(bmap
, bmap
->eq
[i
], d
, 1);
492 isl_basic_map_drop_equality(bmap
, i
);
497 return eliminate_divs_eq(bmap
, progress
);
501 /* Elimininate divs based on inequalities
503 static struct isl_basic_map
*eliminate_divs_ineq(
504 struct isl_basic_map
*bmap
, int *progress
)
515 off
= 1 + isl_dim_total(bmap
->dim
);
517 for (d
= bmap
->n_div
- 1; d
>= 0 ; --d
) {
518 for (i
= 0; i
< bmap
->n_eq
; ++i
)
519 if (!isl_int_is_zero(bmap
->eq
[i
][off
+ d
]))
523 for (i
= 0; i
< bmap
->n_ineq
; ++i
)
524 if (isl_int_abs_gt(bmap
->ineq
[i
][off
+ d
], ctx
->one
))
526 if (i
< bmap
->n_ineq
)
529 bmap
= isl_basic_map_eliminate_vars(bmap
, (off
-1)+d
, 1);
530 if (ISL_F_ISSET(bmap
, ISL_BASIC_MAP_EMPTY
))
532 bmap
= isl_basic_map_drop_div(bmap
, d
);
539 struct isl_basic_map
*isl_basic_map_gauss(
540 struct isl_basic_map
*bmap
, int *progress
)
548 bmap
= isl_basic_map_order_divs(bmap
);
553 total
= isl_basic_map_total_dim(bmap
);
554 total_var
= total
- bmap
->n_div
;
556 last_var
= total
- 1;
557 for (done
= 0; done
< bmap
->n_eq
; ++done
) {
558 for (; last_var
>= 0; --last_var
) {
559 for (k
= done
; k
< bmap
->n_eq
; ++k
)
560 if (!isl_int_is_zero(bmap
->eq
[k
][1+last_var
]))
568 swap_equality(bmap
, k
, done
);
569 if (isl_int_is_neg(bmap
->eq
[done
][1+last_var
]))
570 isl_seq_neg(bmap
->eq
[done
], bmap
->eq
[done
], 1+total
);
572 eliminate_var_using_equality(bmap
, last_var
, bmap
->eq
[done
], 1,
575 if (last_var
>= total_var
&&
576 isl_int_is_zero(bmap
->div
[last_var
- total_var
][0])) {
577 unsigned div
= last_var
- total_var
;
578 isl_seq_neg(bmap
->div
[div
]+1, bmap
->eq
[done
], 1+total
);
579 isl_int_set_si(bmap
->div
[div
][1+1+last_var
], 0);
580 isl_int_set(bmap
->div
[div
][0],
581 bmap
->eq
[done
][1+last_var
]);
582 ISL_F_CLR(bmap
, ISL_BASIC_MAP_NORMALIZED
);
585 if (done
== bmap
->n_eq
)
587 for (k
= done
; k
< bmap
->n_eq
; ++k
) {
588 if (isl_int_is_zero(bmap
->eq
[k
][0]))
590 return isl_basic_map_set_to_empty(bmap
);
592 isl_basic_map_free_equality(bmap
, bmap
->n_eq
-done
);
596 struct isl_basic_set
*isl_basic_set_gauss(
597 struct isl_basic_set
*bset
, int *progress
)
599 return (struct isl_basic_set
*)isl_basic_map_gauss(
600 (struct isl_basic_map
*)bset
, progress
);
604 static unsigned int round_up(unsigned int v
)
615 static int hash_index(isl_int
***index
, unsigned int size
, int bits
,
616 struct isl_basic_map
*bmap
, int k
)
619 unsigned total
= isl_basic_map_total_dim(bmap
);
620 uint32_t hash
= isl_seq_get_hash_bits(bmap
->ineq
[k
]+1, total
, bits
);
621 for (h
= hash
; index
[h
]; h
= (h
+1) % size
)
622 if (&bmap
->ineq
[k
] != index
[h
] &&
623 isl_seq_eq(bmap
->ineq
[k
]+1, index
[h
][0]+1, total
))
628 static int set_hash_index(isl_int
***index
, unsigned int size
, int bits
,
629 struct isl_basic_set
*bset
, int k
)
631 return hash_index(index
, size
, bits
, (struct isl_basic_map
*)bset
, k
);
634 /* If we can eliminate more than one div, then we need to make
635 * sure we do it from last div to first div, in order not to
636 * change the position of the other divs that still need to
639 static struct isl_basic_map
*remove_duplicate_divs(
640 struct isl_basic_map
*bmap
, int *progress
)
648 unsigned total_var
= isl_dim_total(bmap
->dim
);
649 unsigned total
= total_var
+ bmap
->n_div
;
652 if (bmap
->n_div
<= 1)
656 for (k
= bmap
->n_div
- 1; k
>= 0; --k
)
657 if (!isl_int_is_zero(bmap
->div
[k
][0]))
662 elim_for
= isl_calloc_array(ctx
, int, bmap
->n_div
);
663 size
= round_up(4 * bmap
->n_div
/ 3 - 1);
664 bits
= ffs(size
) - 1;
665 index
= isl_calloc_array(ctx
, int, size
);
668 eq
= isl_blk_alloc(ctx
, 1+total
);
669 if (isl_blk_is_error(eq
))
672 isl_seq_clr(eq
.data
, 1+total
);
673 index
[isl_seq_get_hash_bits(bmap
->div
[k
], 2+total
, bits
)] = k
+ 1;
674 for (--k
; k
>= 0; --k
) {
677 if (isl_int_is_zero(bmap
->div
[k
][0]))
680 hash
= isl_seq_get_hash_bits(bmap
->div
[k
], 2+total
, bits
);
681 for (h
= hash
; index
[h
]; h
= (h
+1) % size
)
682 if (isl_seq_eq(bmap
->div
[k
],
683 bmap
->div
[index
[h
]-1], 2+total
))
692 for (l
= bmap
->n_div
- 1; l
>= 0; --l
) {
696 isl_int_set_si(eq
.data
[1+total_var
+k
], -1);
697 isl_int_set_si(eq
.data
[1+total_var
+l
], 1);
698 eliminate_div(bmap
, eq
.data
, l
, 0);
699 isl_int_set_si(eq
.data
[1+total_var
+k
], 0);
700 isl_int_set_si(eq
.data
[1+total_var
+l
], 0);
703 isl_blk_free(ctx
, eq
);
710 static int n_pure_div_eq(struct isl_basic_map
*bmap
)
715 total
= isl_dim_total(bmap
->dim
);
716 for (i
= 0, j
= bmap
->n_div
-1; i
< bmap
->n_eq
; ++i
) {
717 while (j
>= 0 && isl_int_is_zero(bmap
->eq
[i
][1 + total
+ j
]))
721 if (isl_seq_first_non_zero(bmap
->eq
[i
] + 1 + total
, j
) != -1)
727 /* Normalize divs that appear in equalities.
729 * In particular, we assume that bmap contains some equalities
734 * and we want to replace the set of e_i by a minimal set and
735 * such that the new e_i have a canonical representation in terms
737 * If any of the equalities involves more than one divs, then
738 * we currently simply bail out.
740 * Let us first additionally assume that all equalities involve
741 * a div. The equalities then express modulo constraints on the
742 * remaining variables and we can use "parameter compression"
743 * to find a minimal set of constraints. The result is a transformation
745 * x = T(x') = x_0 + G x'
747 * with G a lower-triangular matrix with all elements below the diagonal
748 * non-negative and smaller than the diagonal element on the same row.
749 * We first normalize x_0 by making the same property hold in the affine
751 * The rows i of G with a 1 on the diagonal do not impose any modulo
752 * constraint and simply express x_i = x'_i.
753 * For each of the remaining rows i, we introduce a div and a corresponding
754 * equality. In particular
756 * g_ii e_j = x_i - g_i(x')
758 * where each x'_k is replaced either by x_k (if g_kk = 1) or the
759 * corresponding div (if g_kk != 1).
761 * If there are any equalities not involving any div, then we
762 * first apply a variable compression on the variables x:
764 * x = C x'' x'' = C_2 x
766 * and perform the above parameter compression on A C instead of on A.
767 * The resulting compression is then of the form
769 * x'' = T(x') = x_0 + G x'
771 * and in constructing the new divs and the corresponding equalities,
772 * we have to replace each x'', i.e., the x'_k with (g_kk = 1),
773 * by the corresponding row from C_2.
775 static struct isl_basic_map
*normalize_divs(
776 struct isl_basic_map
*bmap
, int *progress
)
783 struct isl_mat
*T
= NULL
;
784 struct isl_mat
*C
= NULL
;
785 struct isl_mat
*C2
= NULL
;
793 if (bmap
->n_div
== 0)
799 if (ISL_F_ISSET(bmap
, ISL_BASIC_MAP_NORMALIZED_DIVS
))
802 total
= isl_dim_total(bmap
->dim
);
803 div_eq
= n_pure_div_eq(bmap
);
807 if (div_eq
< bmap
->n_eq
) {
808 B
= isl_mat_sub_alloc(bmap
->ctx
, bmap
->eq
, div_eq
,
809 bmap
->n_eq
- div_eq
, 0, 1 + total
);
810 C
= isl_mat_variable_compression(B
, &C2
);
814 bmap
= isl_basic_map_set_to_empty(bmap
);
821 d
= isl_vec_alloc(bmap
->ctx
, div_eq
);
824 for (i
= 0, j
= bmap
->n_div
-1; i
< div_eq
; ++i
) {
825 while (j
>= 0 && isl_int_is_zero(bmap
->eq
[i
][1 + total
+ j
]))
827 isl_int_set(d
->block
.data
[i
], bmap
->eq
[i
][1 + total
+ j
]);
829 B
= isl_mat_sub_alloc(bmap
->ctx
, bmap
->eq
, 0, div_eq
, 0, 1 + total
);
832 B
= isl_mat_product(B
, C
);
836 T
= isl_mat_parameter_compression(B
, d
);
840 bmap
= isl_basic_map_set_to_empty(bmap
);
846 for (i
= 0; i
< T
->n_row
- 1; ++i
) {
847 isl_int_fdiv_q(v
, T
->row
[1 + i
][0], T
->row
[1 + i
][1 + i
]);
848 if (isl_int_is_zero(v
))
850 isl_mat_col_submul(T
, 0, v
, 1 + i
);
853 pos
= isl_alloc_array(bmap
->ctx
, int, T
->n_row
);
854 /* We have to be careful because dropping equalities may reorder them */
856 for (j
= bmap
->n_div
- 1; j
>= 0; --j
) {
857 for (i
= 0; i
< bmap
->n_eq
; ++i
)
858 if (!isl_int_is_zero(bmap
->eq
[i
][1 + total
+ j
]))
860 if (i
< bmap
->n_eq
) {
861 bmap
= isl_basic_map_drop_div(bmap
, j
);
862 isl_basic_map_drop_equality(bmap
, i
);
868 for (i
= 1; i
< T
->n_row
; ++i
) {
869 if (isl_int_is_one(T
->row
[i
][i
]))
874 if (needed
> dropped
) {
875 bmap
= isl_basic_map_extend_dim(bmap
, isl_dim_copy(bmap
->dim
),
880 for (i
= 1; i
< T
->n_row
; ++i
) {
881 if (isl_int_is_one(T
->row
[i
][i
]))
883 k
= isl_basic_map_alloc_div(bmap
);
884 pos
[i
] = 1 + total
+ k
;
885 isl_seq_clr(bmap
->div
[k
] + 1, 1 + total
+ bmap
->n_div
);
886 isl_int_set(bmap
->div
[k
][0], T
->row
[i
][i
]);
888 isl_seq_cpy(bmap
->div
[k
] + 1, C2
->row
[i
], 1 + total
);
890 isl_int_set_si(bmap
->div
[k
][1 + i
], 1);
891 for (j
= 0; j
< i
; ++j
) {
892 if (isl_int_is_zero(T
->row
[i
][j
]))
894 if (pos
[j
] < T
->n_row
&& C2
)
895 isl_seq_submul(bmap
->div
[k
] + 1, T
->row
[i
][j
],
896 C2
->row
[pos
[j
]], 1 + total
);
898 isl_int_neg(bmap
->div
[k
][1 + pos
[j
]],
901 j
= isl_basic_map_alloc_equality(bmap
);
902 isl_seq_neg(bmap
->eq
[j
], bmap
->div
[k
]+1, 1+total
+bmap
->n_div
);
903 isl_int_set(bmap
->eq
[j
][pos
[i
]], bmap
->div
[k
][0]);
912 ISL_F_SET(bmap
, ISL_BASIC_MAP_NORMALIZED_DIVS
);
922 static struct isl_basic_map
*set_div_from_lower_bound(
923 struct isl_basic_map
*bmap
, int div
, int ineq
)
925 unsigned total
= 1 + isl_dim_total(bmap
->dim
);
927 isl_seq_neg(bmap
->div
[div
] + 1, bmap
->ineq
[ineq
], total
+ bmap
->n_div
);
928 isl_int_set(bmap
->div
[div
][0], bmap
->ineq
[ineq
][total
+ div
]);
929 isl_int_add(bmap
->div
[div
][1], bmap
->div
[div
][1], bmap
->div
[div
][0]);
930 isl_int_sub_ui(bmap
->div
[div
][1], bmap
->div
[div
][1], 1);
931 isl_int_set_si(bmap
->div
[div
][1 + total
+ div
], 0);
936 /* Check whether it is ok to define a div based on an inequality.
937 * To avoid the introduction of circular definitions of divs, we
938 * do not allow such a definition if the resulting expression would refer to
939 * any other undefined divs or if any known div is defined in
940 * terms of the unknown div.
942 static int ok_to_set_div_from_bound(struct isl_basic_map
*bmap
,
946 unsigned total
= 1 + isl_dim_total(bmap
->dim
);
948 /* Not defined in terms of unknown divs */
949 for (j
= 0; j
< bmap
->n_div
; ++j
) {
952 if (isl_int_is_zero(bmap
->ineq
[ineq
][total
+ j
]))
954 if (isl_int_is_zero(bmap
->div
[j
][0]))
958 /* No other div defined in terms of this one => avoid loops */
959 for (j
= 0; j
< bmap
->n_div
; ++j
) {
962 if (isl_int_is_zero(bmap
->div
[j
][0]))
964 if (!isl_int_is_zero(bmap
->div
[j
][1 + total
+ div
]))
971 /* Given two constraints "k" and "l" that are opposite to each other,
972 * except for the constant term, check if we can use them
973 * to obtain an expression for one of the hitherto unknown divs.
974 * "sum" is the sum of the constant terms of the constraints.
975 * If this sum is strictly smaller than the coefficient of one
976 * of the divs, then this pair can be used define the div.
977 * To avoid the introduction of circular definitions of divs, we
978 * do not use the pair if the resulting expression would refer to
979 * any other undefined divs or if any known div is defined in
980 * terms of the unknown div.
982 static struct isl_basic_map
*check_for_div_constraints(
983 struct isl_basic_map
*bmap
, int k
, int l
, isl_int sum
, int *progress
)
986 unsigned total
= 1 + isl_dim_total(bmap
->dim
);
988 for (i
= 0; i
< bmap
->n_div
; ++i
) {
989 if (!isl_int_is_zero(bmap
->div
[i
][0]))
991 if (isl_int_is_zero(bmap
->ineq
[k
][total
+ i
]))
993 if (isl_int_abs_ge(sum
, bmap
->ineq
[k
][total
+ i
]))
995 if (!ok_to_set_div_from_bound(bmap
, i
, k
))
997 if (isl_int_is_pos(bmap
->ineq
[k
][total
+ i
]))
998 bmap
= set_div_from_lower_bound(bmap
, i
, k
);
1000 bmap
= set_div_from_lower_bound(bmap
, i
, l
);
1008 static struct isl_basic_map
*remove_duplicate_constraints(
1009 struct isl_basic_map
*bmap
, int *progress
)
1015 unsigned total
= isl_basic_map_total_dim(bmap
);
1018 if (bmap
->n_ineq
<= 1)
1021 size
= round_up(4 * (bmap
->n_ineq
+1) / 3 - 1);
1022 bits
= ffs(size
) - 1;
1023 index
= isl_calloc_array(ctx
, isl_int
**, size
);
1027 index
[isl_seq_get_hash_bits(bmap
->ineq
[0]+1, total
, bits
)] = &bmap
->ineq
[0];
1028 for (k
= 1; k
< bmap
->n_ineq
; ++k
) {
1029 h
= hash_index(index
, size
, bits
, bmap
, k
);
1031 index
[h
] = &bmap
->ineq
[k
];
1036 l
= index
[h
] - &bmap
->ineq
[0];
1037 if (isl_int_lt(bmap
->ineq
[k
][0], bmap
->ineq
[l
][0]))
1038 swap_inequality(bmap
, k
, l
);
1039 isl_basic_map_drop_inequality(bmap
, k
);
1043 for (k
= 0; k
< bmap
->n_ineq
-1; ++k
) {
1044 isl_seq_neg(bmap
->ineq
[k
]+1, bmap
->ineq
[k
]+1, total
);
1045 h
= hash_index(index
, size
, bits
, bmap
, k
);
1046 isl_seq_neg(bmap
->ineq
[k
]+1, bmap
->ineq
[k
]+1, total
);
1049 l
= index
[h
] - &bmap
->ineq
[0];
1050 isl_int_add(sum
, bmap
->ineq
[k
][0], bmap
->ineq
[l
][0]);
1051 if (isl_int_is_pos(sum
)) {
1052 bmap
= check_for_div_constraints(bmap
, k
, l
, sum
,
1056 if (isl_int_is_zero(sum
)) {
1057 /* We need to break out of the loop after these
1058 * changes since the contents of the hash
1059 * will no longer be valid.
1060 * Plus, we probably we want to regauss first.
1064 isl_basic_map_drop_inequality(bmap
, l
);
1065 isl_basic_map_inequality_to_equality(bmap
, k
);
1067 bmap
= isl_basic_map_set_to_empty(bmap
);
1077 struct isl_basic_map
*isl_basic_map_simplify(struct isl_basic_map
*bmap
)
1084 bmap
= isl_basic_map_normalize_constraints(bmap
);
1085 bmap
= remove_duplicate_divs(bmap
, &progress
);
1086 bmap
= eliminate_divs_eq(bmap
, &progress
);
1087 bmap
= eliminate_divs_ineq(bmap
, &progress
);
1088 bmap
= isl_basic_map_gauss(bmap
, &progress
);
1089 /* requires equalities in normal form */
1090 bmap
= normalize_divs(bmap
, &progress
);
1091 bmap
= remove_duplicate_constraints(bmap
, &progress
);
1096 struct isl_basic_set
*isl_basic_set_simplify(struct isl_basic_set
*bset
)
1098 return (struct isl_basic_set
*)
1099 isl_basic_map_simplify((struct isl_basic_map
*)bset
);
1103 /* If the only constraints a div d=floor(f/m)
1104 * appears in are its two defining constraints
1107 * -(f - (m - 1)) + m d >= 0
1109 * then it can safely be removed.
1111 static int div_is_redundant(struct isl_basic_map
*bmap
, int div
)
1114 unsigned pos
= 1 + isl_dim_total(bmap
->dim
) + div
;
1116 for (i
= 0; i
< bmap
->n_eq
; ++i
)
1117 if (!isl_int_is_zero(bmap
->eq
[i
][pos
]))
1120 for (i
= 0; i
< bmap
->n_ineq
; ++i
) {
1121 if (isl_int_is_zero(bmap
->ineq
[i
][pos
]))
1123 if (isl_int_eq(bmap
->ineq
[i
][pos
], bmap
->div
[div
][0])) {
1125 isl_int_sub(bmap
->div
[div
][1],
1126 bmap
->div
[div
][1], bmap
->div
[div
][0]);
1127 isl_int_add_ui(bmap
->div
[div
][1], bmap
->div
[div
][1], 1);
1128 neg
= isl_seq_is_neg(bmap
->ineq
[i
], bmap
->div
[div
]+1, pos
);
1129 isl_int_sub_ui(bmap
->div
[div
][1], bmap
->div
[div
][1], 1);
1130 isl_int_add(bmap
->div
[div
][1],
1131 bmap
->div
[div
][1], bmap
->div
[div
][0]);
1134 if (isl_seq_first_non_zero(bmap
->ineq
[i
]+pos
+1,
1135 bmap
->n_div
-div
-1) != -1)
1137 } else if (isl_int_abs_eq(bmap
->ineq
[i
][pos
], bmap
->div
[div
][0])) {
1138 if (!isl_seq_eq(bmap
->ineq
[i
], bmap
->div
[div
]+1, pos
))
1140 if (isl_seq_first_non_zero(bmap
->ineq
[i
]+pos
+1,
1141 bmap
->n_div
-div
-1) != -1)
1147 for (i
= 0; i
< bmap
->n_div
; ++i
)
1148 if (!isl_int_is_zero(bmap
->div
[i
][1+pos
]))
1155 * Remove divs that don't occur in any of the constraints or other divs.
1156 * These can arise when dropping some of the variables in a quast
1157 * returned by piplib.
1159 static struct isl_basic_map
*remove_redundant_divs(struct isl_basic_map
*bmap
)
1166 for (i
= bmap
->n_div
-1; i
>= 0; --i
) {
1167 if (!div_is_redundant(bmap
, i
))
1169 bmap
= isl_basic_map_drop_div(bmap
, i
);
1174 struct isl_basic_map
*isl_basic_map_finalize(struct isl_basic_map
*bmap
)
1176 bmap
= remove_redundant_divs(bmap
);
1179 ISL_F_SET(bmap
, ISL_BASIC_SET_FINAL
);
1183 struct isl_basic_set
*isl_basic_set_finalize(struct isl_basic_set
*bset
)
1185 return (struct isl_basic_set
*)
1186 isl_basic_map_finalize((struct isl_basic_map
*)bset
);
1189 struct isl_set
*isl_set_finalize(struct isl_set
*set
)
1195 for (i
= 0; i
< set
->n
; ++i
) {
1196 set
->p
[i
] = isl_basic_set_finalize(set
->p
[i
]);
1206 struct isl_map
*isl_map_finalize(struct isl_map
*map
)
1212 for (i
= 0; i
< map
->n
; ++i
) {
1213 map
->p
[i
] = isl_basic_map_finalize(map
->p
[i
]);
1217 ISL_F_CLR(map
, ISL_MAP_NORMALIZED
);
1225 /* Remove definition of any div that is defined in terms of the given variable.
1226 * The div itself is not removed. Functions such as
1227 * eliminate_divs_ineq depend on the other divs remaining in place.
1229 static struct isl_basic_map
*remove_dependent_vars(struct isl_basic_map
*bmap
,
1234 for (i
= 0; i
< bmap
->n_div
; ++i
) {
1235 if (isl_int_is_zero(bmap
->div
[i
][0]))
1237 if (isl_int_is_zero(bmap
->div
[i
][1+1+pos
]))
1239 isl_int_set_si(bmap
->div
[i
][0], 0);
1244 /* Eliminate the specified variables from the constraints using
1245 * Fourier-Motzkin. The variables themselves are not removed.
1247 struct isl_basic_map
*isl_basic_map_eliminate_vars(
1248 struct isl_basic_map
*bmap
, unsigned pos
, unsigned n
)
1258 total
= isl_basic_map_total_dim(bmap
);
1260 bmap
= isl_basic_map_cow(bmap
);
1261 for (d
= pos
+ n
- 1; d
>= 0 && d
>= pos
; --d
)
1262 bmap
= remove_dependent_vars(bmap
, d
);
1264 for (d
= pos
+ n
- 1;
1265 d
>= 0 && d
>= total
- bmap
->n_div
&& d
>= pos
; --d
)
1266 isl_seq_clr(bmap
->div
[d
-(total
-bmap
->n_div
)], 2+total
);
1267 for (d
= pos
+ n
- 1; d
>= 0 && d
>= pos
; --d
) {
1268 int n_lower
, n_upper
;
1271 for (i
= 0; i
< bmap
->n_eq
; ++i
) {
1272 if (isl_int_is_zero(bmap
->eq
[i
][1+d
]))
1274 eliminate_var_using_equality(bmap
, d
, bmap
->eq
[i
], 0, NULL
);
1275 isl_basic_map_drop_equality(bmap
, i
);
1282 for (i
= 0; i
< bmap
->n_ineq
; ++i
) {
1283 if (isl_int_is_pos(bmap
->ineq
[i
][1+d
]))
1285 else if (isl_int_is_neg(bmap
->ineq
[i
][1+d
]))
1288 bmap
= isl_basic_map_extend_constraints(bmap
,
1289 0, n_lower
* n_upper
);
1290 for (i
= bmap
->n_ineq
- 1; i
>= 0; --i
) {
1292 if (isl_int_is_zero(bmap
->ineq
[i
][1+d
]))
1295 for (j
= 0; j
< i
; ++j
) {
1296 if (isl_int_is_zero(bmap
->ineq
[j
][1+d
]))
1299 if (isl_int_sgn(bmap
->ineq
[i
][1+d
]) ==
1300 isl_int_sgn(bmap
->ineq
[j
][1+d
]))
1302 k
= isl_basic_map_alloc_inequality(bmap
);
1305 isl_seq_cpy(bmap
->ineq
[k
], bmap
->ineq
[i
],
1307 isl_seq_elim(bmap
->ineq
[k
], bmap
->ineq
[j
],
1308 1+d
, 1+total
, NULL
);
1310 isl_basic_map_drop_inequality(bmap
, i
);
1313 if (n_lower
> 0 && n_upper
> 0) {
1314 bmap
= isl_basic_map_normalize_constraints(bmap
);
1315 bmap
= remove_duplicate_constraints(bmap
, NULL
);
1316 bmap
= isl_basic_map_gauss(bmap
, NULL
);
1317 bmap
= isl_basic_map_convex_hull(bmap
);
1320 if (ISL_F_ISSET(bmap
, ISL_BASIC_MAP_EMPTY
))
1324 ISL_F_CLR(bmap
, ISL_BASIC_MAP_NORMALIZED
);
1327 isl_basic_map_free(bmap
);
1331 struct isl_basic_set
*isl_basic_set_eliminate_vars(
1332 struct isl_basic_set
*bset
, unsigned pos
, unsigned n
)
1334 return (struct isl_basic_set
*)isl_basic_map_eliminate_vars(
1335 (struct isl_basic_map
*)bset
, pos
, n
);
1338 /* Don't assume equalities are in order, because align_divs
1339 * may have changed the order of the divs.
1341 static void compute_elimination_index(struct isl_basic_map
*bmap
, int *elim
)
1346 total
= isl_dim_total(bmap
->dim
);
1347 for (d
= 0; d
< total
; ++d
)
1349 for (i
= 0; i
< bmap
->n_eq
; ++i
) {
1350 for (d
= total
- 1; d
>= 0; --d
) {
1351 if (isl_int_is_zero(bmap
->eq
[i
][1+d
]))
1359 static void set_compute_elimination_index(struct isl_basic_set
*bset
, int *elim
)
1361 compute_elimination_index((struct isl_basic_map
*)bset
, elim
);
1364 static int reduced_using_equalities(isl_int
*dst
, isl_int
*src
,
1365 struct isl_basic_map
*bmap
, int *elim
)
1371 total
= isl_dim_total(bmap
->dim
);
1372 for (d
= total
- 1; d
>= 0; --d
) {
1373 if (isl_int_is_zero(src
[1+d
]))
1378 isl_seq_cpy(dst
, src
, 1 + total
);
1381 isl_seq_elim(dst
, bmap
->eq
[elim
[d
]], 1 + d
, 1 + total
, NULL
);
1386 static int set_reduced_using_equalities(isl_int
*dst
, isl_int
*src
,
1387 struct isl_basic_set
*bset
, int *elim
)
1389 return reduced_using_equalities(dst
, src
,
1390 (struct isl_basic_map
*)bset
, elim
);
1393 static struct isl_basic_set
*isl_basic_set_reduce_using_equalities(
1394 struct isl_basic_set
*bset
, struct isl_basic_set
*context
)
1399 if (!bset
|| !context
)
1402 if (context
->n_eq
== 0) {
1403 isl_basic_set_free(context
);
1407 bset
= isl_basic_set_cow(bset
);
1411 elim
= isl_alloc_array(bset
->ctx
, int, isl_basic_set_n_dim(bset
));
1414 set_compute_elimination_index(context
, elim
);
1415 for (i
= 0; i
< bset
->n_eq
; ++i
)
1416 set_reduced_using_equalities(bset
->eq
[i
], bset
->eq
[i
],
1418 for (i
= 0; i
< bset
->n_ineq
; ++i
)
1419 set_reduced_using_equalities(bset
->ineq
[i
], bset
->ineq
[i
],
1421 isl_basic_set_free(context
);
1423 bset
= isl_basic_set_simplify(bset
);
1424 bset
= isl_basic_set_finalize(bset
);
1427 isl_basic_set_free(bset
);
1428 isl_basic_set_free(context
);
1432 static struct isl_basic_set
*remove_shifted_constraints(
1433 struct isl_basic_set
*bset
, struct isl_basic_set
*context
)
1443 size
= round_up(4 * (context
->n_ineq
+1) / 3 - 1);
1444 bits
= ffs(size
) - 1;
1445 index
= isl_calloc_array(ctx
, isl_int
**, size
);
1449 for (k
= 0; k
< context
->n_ineq
; ++k
) {
1450 h
= set_hash_index(index
, size
, bits
, context
, k
);
1451 index
[h
] = &context
->ineq
[k
];
1453 for (k
= 0; k
< bset
->n_ineq
; ++k
) {
1454 h
= set_hash_index(index
, size
, bits
, bset
, k
);
1457 l
= index
[h
] - &context
->ineq
[0];
1458 if (isl_int_lt(bset
->ineq
[k
][0], context
->ineq
[l
][0]))
1460 bset
= isl_basic_set_cow(bset
);
1463 isl_basic_set_drop_inequality(bset
, k
);
1473 /* Tighten (decrease) the constant terms of the inequalities based
1474 * on the equalities, without removing any integer points.
1475 * For example, if there is an equality
1483 * then we want to replace the inequality by
1487 * We do this by computing a variable compression and translating
1488 * the constraints to the compressed space.
1489 * If any constraint has coefficients (except the contant term)
1490 * with a common factor "f", then we can replace the constant term "c"
1497 * f * floor(c/f) - c = -fract(c/f)
1499 * and we can add the same value to the original constraint.
1501 * In the example, the compressed space only contains "j",
1502 * and the inequality translates to
1506 * We add -fract(-1/3) = -2 to the original constraint to obtain
1510 static struct isl_basic_set
*normalize_constraints_in_compressed_space(
1511 struct isl_basic_set
*bset
)
1515 struct isl_mat
*B
, *C
;
1521 if (ISL_F_ISSET(bset
, ISL_BASIC_SET_RATIONAL
))
1527 bset
= isl_basic_set_cow(bset
);
1531 total
= isl_basic_set_total_dim(bset
);
1532 B
= isl_mat_sub_alloc(bset
->ctx
, bset
->eq
, 0, bset
->n_eq
, 0, 1 + total
);
1533 C
= isl_mat_variable_compression(B
, NULL
);
1536 if (C
->n_col
== 0) {
1538 return isl_basic_set_set_to_empty(bset
);
1540 B
= isl_mat_sub_alloc(bset
->ctx
, bset
->ineq
,
1541 0, bset
->n_ineq
, 0, 1 + total
);
1542 C
= isl_mat_product(B
, C
);
1547 for (i
= 0; i
< bset
->n_ineq
; ++i
) {
1548 isl_seq_gcd(C
->row
[i
] + 1, C
->n_col
- 1, &gcd
);
1549 if (isl_int_is_one(gcd
))
1551 isl_int_fdiv_r(C
->row
[i
][0], C
->row
[i
][0], gcd
);
1552 isl_int_sub(bset
->ineq
[i
][0], bset
->ineq
[i
][0], C
->row
[i
][0]);
1561 /* Remove all information from bset that is redundant in the context
1562 * of context. Both bset and context are assumed to be full-dimensional.
1564 * We first * remove the inequalities from "bset"
1565 * that are obviously redundant with respect to some inequality in "context".
1567 * If there are any inequalities left, we construct a tableau for
1568 * the context and then add the inequalities of "bset".
1569 * Before adding these inequalities, we freeze all constraints such that
1570 * they won't be considered redundant in terms of the constraints of "bset".
1571 * Then we detect all redundant constraints (among the
1572 * constraints that weren't frozen), first by checking for redundancy in the
1573 * the tableau and then by checking if replacing a constraint by its negation
1574 * would lead to an empty set. This last step is fairly expensive
1575 * and could be optimized by more reuse of the tableau.
1576 * Finally, we update bset according to the results.
1578 static __isl_give isl_basic_set
*uset_gist_full(__isl_take isl_basic_set
*bset
,
1579 __isl_take isl_basic_set
*context
)
1582 isl_basic_set
*combined
= NULL
;
1583 struct isl_tab
*tab
= NULL
;
1584 unsigned context_ineq
;
1587 if (!bset
|| !context
)
1590 if (isl_basic_set_is_universe(bset
)) {
1591 isl_basic_set_free(context
);
1595 if (isl_basic_set_is_universe(context
)) {
1596 isl_basic_set_free(context
);
1600 bset
= remove_shifted_constraints(bset
, context
);
1603 if (bset
->n_ineq
== 0)
1606 context_ineq
= context
->n_ineq
;
1607 combined
= isl_basic_set_cow(isl_basic_set_copy(context
));
1608 combined
= isl_basic_set_extend_constraints(combined
, 0, bset
->n_ineq
);
1609 tab
= isl_tab_from_basic_set(combined
);
1610 for (i
= 0; i
< context_ineq
; ++i
)
1611 if (isl_tab_freeze_constraint(tab
, i
) < 0)
1613 tab
= isl_tab_extend(tab
, bset
->n_ineq
);
1614 for (i
= 0; i
< bset
->n_ineq
; ++i
)
1615 if (isl_tab_add_ineq(tab
, bset
->ineq
[i
]) < 0)
1617 bset
= isl_basic_set_add_constraints(combined
, bset
, 0);
1621 if (isl_tab_detect_redundant(tab
) < 0)
1623 total
= isl_basic_set_total_dim(bset
);
1624 for (i
= context_ineq
; i
< bset
->n_ineq
; ++i
) {
1626 if (tab
->con
[i
].is_redundant
)
1628 tab
->con
[i
].is_redundant
= 1;
1629 combined
= isl_basic_set_dup(bset
);
1630 combined
= isl_basic_set_update_from_tab(combined
, tab
);
1631 combined
= isl_basic_set_extend_constraints(combined
, 0, 1);
1632 k
= isl_basic_set_alloc_inequality(combined
);
1635 isl_seq_neg(combined
->ineq
[k
], bset
->ineq
[i
], 1 + total
);
1636 isl_int_sub_ui(combined
->ineq
[k
][0], combined
->ineq
[k
][0], 1);
1637 is_empty
= isl_basic_set_is_empty(combined
);
1640 isl_basic_set_free(combined
);
1643 tab
->con
[i
].is_redundant
= 0;
1645 for (i
= 0; i
< context_ineq
; ++i
)
1646 tab
->con
[i
].is_redundant
= 1;
1647 bset
= isl_basic_set_update_from_tab(bset
, tab
);
1649 ISL_F_SET(bset
, ISL_BASIC_SET_NO_IMPLICIT
);
1650 ISL_F_SET(bset
, ISL_BASIC_SET_NO_REDUNDANT
);
1655 bset
= isl_basic_set_simplify(bset
);
1656 bset
= isl_basic_set_finalize(bset
);
1657 isl_basic_set_free(context
);
1661 isl_basic_set_free(combined
);
1662 isl_basic_set_free(context
);
1663 isl_basic_set_free(bset
);
1667 /* Remove all information from bset that is redundant in the context
1668 * of context. In particular, equalities that are linear combinations
1669 * of those in context are removed. Then the inequalities that are
1670 * redundant in the context of the equalities and inequalities of
1671 * context are removed.
1673 * We first compute the integer affine hull of the intersection,
1674 * compute the gist inside this affine hull and then add back
1675 * those equalities that are not implied by the context.
1677 static __isl_give isl_basic_set
*uset_gist(__isl_take isl_basic_set
*bset
,
1678 __isl_take isl_basic_set
*context
)
1683 isl_basic_set
*aff_context
;
1686 if (!bset
|| !context
)
1689 bset
= isl_basic_set_intersect(bset
, isl_basic_set_copy(context
));
1690 if (isl_basic_set_fast_is_empty(bset
)) {
1691 isl_basic_set_free(context
);
1694 aff
= isl_basic_set_affine_hull(isl_basic_set_copy(bset
));
1697 if (isl_basic_set_fast_is_empty(aff
)) {
1698 isl_basic_set_free(aff
);
1699 isl_basic_set_free(context
);
1702 if (aff
->n_eq
== 0) {
1703 isl_basic_set_free(aff
);
1704 return uset_gist_full(bset
, context
);
1706 total
= isl_basic_set_total_dim(bset
);
1707 eq
= isl_mat_sub_alloc(bset
->ctx
, aff
->eq
, 0, aff
->n_eq
, 0, 1 + total
);
1708 eq
= isl_mat_cow(eq
);
1709 T
= isl_mat_variable_compression(eq
, &T2
);
1710 if (T
&& T
->n_col
== 0) {
1713 isl_basic_set_free(context
);
1714 isl_basic_set_free(aff
);
1715 return isl_basic_set_set_to_empty(bset
);
1718 aff_context
= isl_basic_set_affine_hull(isl_basic_set_copy(context
));
1720 bset
= isl_basic_set_preimage(bset
, isl_mat_copy(T
));
1721 context
= isl_basic_set_preimage(context
, T
);
1723 bset
= uset_gist_full(bset
, context
);
1724 bset
= isl_basic_set_preimage(bset
, T2
);
1725 bset
= isl_basic_set_intersect(bset
, aff
);
1726 bset
= isl_basic_set_reduce_using_equalities(bset
, aff_context
);
1729 ISL_F_SET(bset
, ISL_BASIC_SET_NO_IMPLICIT
);
1730 ISL_F_SET(bset
, ISL_BASIC_SET_NO_REDUNDANT
);
1735 isl_basic_set_free(bset
);
1736 isl_basic_set_free(context
);
1740 /* Normalize the divs in "bmap" in the context of the equalities in "context".
1741 * We simply add the equalities in context to bmap and then do a regular
1742 * div normalizations. Better results can be obtained by normalizing
1743 * only the divs in bmap than do not also appear in context.
1744 * We need to be careful to reduce the divs using the equalities
1745 * so that later calls to isl_basic_map_overlying_set wouldn't introduce
1746 * spurious constraints.
1748 static struct isl_basic_map
*normalize_divs_in_context(
1749 struct isl_basic_map
*bmap
, struct isl_basic_map
*context
)
1752 unsigned total_context
;
1755 div_eq
= n_pure_div_eq(bmap
);
1759 if (context
->n_div
> 0)
1760 bmap
= isl_basic_map_align_divs(bmap
, context
);
1762 total_context
= isl_basic_map_total_dim(context
);
1763 bmap
= isl_basic_map_extend_constraints(bmap
, context
->n_eq
, 0);
1764 for (i
= 0; i
< context
->n_eq
; ++i
) {
1766 k
= isl_basic_map_alloc_equality(bmap
);
1767 isl_seq_cpy(bmap
->eq
[k
], context
->eq
[i
], 1 + total_context
);
1768 isl_seq_clr(bmap
->eq
[k
] + 1 + total_context
,
1769 isl_basic_map_total_dim(bmap
) - total_context
);
1771 bmap
= isl_basic_map_gauss(bmap
, NULL
);
1772 bmap
= normalize_divs(bmap
, NULL
);
1773 bmap
= isl_basic_map_gauss(bmap
, NULL
);
1777 struct isl_basic_map
*isl_basic_map_gist(struct isl_basic_map
*bmap
,
1778 struct isl_basic_map
*context
)
1780 struct isl_basic_set
*bset
;
1782 if (!bmap
|| !context
)
1785 if (isl_basic_map_is_universe(context
)) {
1786 isl_basic_map_free(context
);
1789 if (isl_basic_map_is_universe(bmap
)) {
1790 isl_basic_map_free(context
);
1793 if (isl_basic_map_fast_is_empty(context
)) {
1794 struct isl_dim
*dim
= isl_dim_copy(bmap
->dim
);
1795 isl_basic_map_free(context
);
1796 isl_basic_map_free(bmap
);
1797 return isl_basic_map_universe(dim
);
1799 if (isl_basic_map_fast_is_empty(bmap
)) {
1800 isl_basic_map_free(context
);
1804 bmap
= isl_basic_map_convex_hull(bmap
);
1805 context
= isl_basic_map_convex_hull(context
);
1808 bmap
= normalize_divs_in_context(bmap
, context
);
1810 context
= isl_basic_map_align_divs(context
, bmap
);
1811 bmap
= isl_basic_map_align_divs(bmap
, context
);
1813 bset
= uset_gist(isl_basic_map_underlying_set(isl_basic_map_copy(bmap
)),
1814 isl_basic_map_underlying_set(context
));
1816 return isl_basic_map_overlying_set(bset
, bmap
);
1818 isl_basic_map_free(bmap
);
1819 isl_basic_map_free(context
);
1824 * Assumes context has no implicit divs.
1826 __isl_give isl_map
*isl_map_gist_basic_map(__isl_take isl_map
*map
,
1827 __isl_take isl_basic_map
*context
)
1831 if (!map
|| !context
)
1834 if (isl_basic_map_is_universe(context
)) {
1835 isl_basic_map_free(context
);
1838 if (isl_basic_map_fast_is_empty(context
)) {
1839 struct isl_dim
*dim
= isl_dim_copy(map
->dim
);
1840 isl_basic_map_free(context
);
1842 return isl_map_universe(dim
);
1845 context
= isl_basic_map_convex_hull(context
);
1846 map
= isl_map_cow(map
);
1847 if (!map
|| !context
)
1849 isl_assert(map
->ctx
, isl_dim_equal(map
->dim
, context
->dim
), goto error
);
1850 map
= isl_map_compute_divs(map
);
1851 for (i
= 0; i
< map
->n
; ++i
)
1852 context
= isl_basic_map_align_divs(context
, map
->p
[i
]);
1853 for (i
= 0; i
< map
->n
; ++i
) {
1854 map
->p
[i
] = isl_basic_map_gist(map
->p
[i
],
1855 isl_basic_map_copy(context
));
1859 isl_basic_map_free(context
);
1860 ISL_F_CLR(map
, ISL_MAP_NORMALIZED
);
1864 isl_basic_map_free(context
);
1868 __isl_give isl_map
*isl_map_gist(__isl_take isl_map
*map
,
1869 __isl_take isl_map
*context
)
1871 return isl_map_gist_basic_map(map
, isl_map_convex_hull(context
));
1874 struct isl_basic_set
*isl_basic_set_gist(struct isl_basic_set
*bset
,
1875 struct isl_basic_set
*context
)
1877 return (struct isl_basic_set
*)isl_basic_map_gist(
1878 (struct isl_basic_map
*)bset
, (struct isl_basic_map
*)context
);
1881 __isl_give isl_set
*isl_set_gist_basic_set(__isl_take isl_set
*set
,
1882 __isl_take isl_basic_set
*context
)
1884 return (struct isl_set
*)isl_map_gist_basic_map((struct isl_map
*)set
,
1885 (struct isl_basic_map
*)context
);
1888 __isl_give isl_set
*isl_set_gist(__isl_take isl_set
*set
,
1889 __isl_take isl_set
*context
)
1891 return (struct isl_set
*)isl_map_gist((struct isl_map
*)set
,
1892 (struct isl_map
*)context
);
1895 /* Quick check to see if two basic maps are disjoint.
1896 * In particular, we reduce the equalities and inequalities of
1897 * one basic map in the context of the equalities of the other
1898 * basic map and check if we get a contradiction.
1900 int isl_basic_map_fast_is_disjoint(struct isl_basic_map
*bmap1
,
1901 struct isl_basic_map
*bmap2
)
1903 struct isl_vec
*v
= NULL
;
1908 if (!bmap1
|| !bmap2
)
1910 isl_assert(bmap1
->ctx
, isl_dim_equal(bmap1
->dim
, bmap2
->dim
),
1912 if (bmap1
->n_div
|| bmap2
->n_div
)
1914 if (!bmap1
->n_eq
&& !bmap2
->n_eq
)
1917 total
= isl_dim_total(bmap1
->dim
);
1920 v
= isl_vec_alloc(bmap1
->ctx
, 1 + total
);
1923 elim
= isl_alloc_array(bmap1
->ctx
, int, total
);
1926 compute_elimination_index(bmap1
, elim
);
1927 for (i
= 0; i
< bmap2
->n_eq
; ++i
) {
1929 reduced
= reduced_using_equalities(v
->block
.data
, bmap2
->eq
[i
],
1931 if (reduced
&& !isl_int_is_zero(v
->block
.data
[0]) &&
1932 isl_seq_first_non_zero(v
->block
.data
+ 1, total
) == -1)
1935 for (i
= 0; i
< bmap2
->n_ineq
; ++i
) {
1937 reduced
= reduced_using_equalities(v
->block
.data
,
1938 bmap2
->ineq
[i
], bmap1
, elim
);
1939 if (reduced
&& isl_int_is_neg(v
->block
.data
[0]) &&
1940 isl_seq_first_non_zero(v
->block
.data
+ 1, total
) == -1)
1943 compute_elimination_index(bmap2
, elim
);
1944 for (i
= 0; i
< bmap1
->n_ineq
; ++i
) {
1946 reduced
= reduced_using_equalities(v
->block
.data
,
1947 bmap1
->ineq
[i
], bmap2
, elim
);
1948 if (reduced
&& isl_int_is_neg(v
->block
.data
[0]) &&
1949 isl_seq_first_non_zero(v
->block
.data
+ 1, total
) == -1)
1965 int isl_basic_set_fast_is_disjoint(struct isl_basic_set
*bset1
,
1966 struct isl_basic_set
*bset2
)
1968 return isl_basic_map_fast_is_disjoint((struct isl_basic_map
*)bset1
,
1969 (struct isl_basic_map
*)bset2
);
1972 int isl_map_fast_is_disjoint(struct isl_map
*map1
, struct isl_map
*map2
)
1979 if (isl_map_fast_is_equal(map1
, map2
))
1982 for (i
= 0; i
< map1
->n
; ++i
) {
1983 for (j
= 0; j
< map2
->n
; ++j
) {
1984 int d
= isl_basic_map_fast_is_disjoint(map1
->p
[i
],
1993 int isl_set_fast_is_disjoint(struct isl_set
*set1
, struct isl_set
*set2
)
1995 return isl_map_fast_is_disjoint((struct isl_map
*)set1
,
1996 (struct isl_map
*)set2
);
1999 /* Check if we can combine a given div with lower bound l and upper
2000 * bound u with some other div and if so return that other div.
2001 * Otherwise return -1.
2003 * We first check that
2004 * - the bounds are opposites of each other (except for the constant
2006 * - the bounds do not reference any other div
2007 * - no div is defined in terms of this div
2009 * Let m be the size of the range allowed on the div by the bounds.
2010 * That is, the bounds are of the form
2012 * e <= a <= e + m - 1
2014 * with e some expression in the other variables.
2015 * We look for another div b such that no third div is defined in terms
2016 * of this second div b and such that in any constraint that contains
2017 * a (except for the given lower and upper bound), also contains b
2018 * with a coefficient that is m times that of b.
2019 * That is, all constraints (execpt for the lower and upper bound)
2022 * e + f (a + m b) >= 0
2024 * If so, we return b so that "a + m b" can be replaced by
2025 * a single div "c = a + m b".
2027 static int div_find_coalesce(struct isl_basic_map
*bmap
, int *pairs
,
2028 unsigned div
, unsigned l
, unsigned u
)
2034 if (bmap
->n_div
<= 1)
2036 dim
= isl_dim_total(bmap
->dim
);
2037 if (isl_seq_first_non_zero(bmap
->ineq
[l
] + 1 + dim
, div
) != -1)
2039 if (isl_seq_first_non_zero(bmap
->ineq
[l
] + 1 + dim
+ div
+ 1,
2040 bmap
->n_div
- div
- 1) != -1)
2042 if (!isl_seq_is_neg(bmap
->ineq
[l
] + 1, bmap
->ineq
[u
] + 1,
2046 for (i
= 0; i
< bmap
->n_div
; ++i
) {
2047 if (isl_int_is_zero(bmap
->div
[i
][0]))
2049 if (!isl_int_is_zero(bmap
->div
[i
][1 + 1 + dim
+ div
]))
2053 isl_int_add(bmap
->ineq
[l
][0], bmap
->ineq
[l
][0], bmap
->ineq
[u
][0]);
2054 if (isl_int_is_neg(bmap
->ineq
[l
][0])) {
2055 isl_int_sub(bmap
->ineq
[l
][0],
2056 bmap
->ineq
[l
][0], bmap
->ineq
[u
][0]);
2057 bmap
= isl_basic_map_copy(bmap
);
2058 bmap
= isl_basic_map_set_to_empty(bmap
);
2059 isl_basic_map_free(bmap
);
2062 isl_int_add_ui(bmap
->ineq
[l
][0], bmap
->ineq
[l
][0], 1);
2063 for (i
= 0; i
< bmap
->n_div
; ++i
) {
2068 for (j
= 0; j
< bmap
->n_div
; ++j
) {
2069 if (isl_int_is_zero(bmap
->div
[j
][0]))
2071 if (!isl_int_is_zero(bmap
->div
[j
][1 + 1 + dim
+ i
]))
2074 if (j
< bmap
->n_div
)
2076 for (j
= 0; j
< bmap
->n_ineq
; ++j
) {
2078 if (j
== l
|| j
== u
)
2080 if (isl_int_is_zero(bmap
->ineq
[j
][1 + dim
+ div
]))
2082 if (isl_int_is_zero(bmap
->ineq
[j
][1 + dim
+ i
]))
2084 isl_int_mul(bmap
->ineq
[j
][1 + dim
+ div
],
2085 bmap
->ineq
[j
][1 + dim
+ div
],
2087 valid
= isl_int_eq(bmap
->ineq
[j
][1 + dim
+ div
],
2088 bmap
->ineq
[j
][1 + dim
+ i
]);
2089 isl_int_divexact(bmap
->ineq
[j
][1 + dim
+ div
],
2090 bmap
->ineq
[j
][1 + dim
+ div
],
2095 if (j
< bmap
->n_ineq
)
2100 isl_int_sub_ui(bmap
->ineq
[l
][0], bmap
->ineq
[l
][0], 1);
2101 isl_int_sub(bmap
->ineq
[l
][0], bmap
->ineq
[l
][0], bmap
->ineq
[u
][0]);
2105 /* Given a lower and an upper bound on div i, construct an inequality
2106 * that when nonnegative ensures that this pair of bounds always allows
2107 * for an integer value of the given div.
2108 * The lower bound is inequality l, while the upper bound is inequality u.
2109 * The constructed inequality is stored in ineq.
2110 * g, fl, fu are temporary scalars.
2112 * Let the upper bound be
2116 * and the lower bound
2120 * Let n_u = f_u g and n_l = f_l g, with g = gcd(n_u, n_l).
2123 * - f_u e_l <= f_u f_l g a <= f_l e_u
2125 * Since all variables are integer valued, this is equivalent to
2127 * - f_u e_l - (f_u - 1) <= f_u f_l g a <= f_l e_u + (f_l - 1)
2129 * If this interval is at least f_u f_l g, then it contains at least
2130 * one integer value for a.
2131 * That is, the test constraint is
2133 * f_l e_u + f_u e_l + f_l - 1 + f_u - 1 + 1 >= f_u f_l g
2135 static void construct_test_ineq(struct isl_basic_map
*bmap
, int i
,
2136 int l
, int u
, isl_int
*ineq
, isl_int g
, isl_int fl
, isl_int fu
)
2139 dim
= isl_dim_total(bmap
->dim
);
2141 isl_int_gcd(g
, bmap
->ineq
[l
][1 + dim
+ i
], bmap
->ineq
[u
][1 + dim
+ i
]);
2142 isl_int_divexact(fl
, bmap
->ineq
[l
][1 + dim
+ i
], g
);
2143 isl_int_divexact(fu
, bmap
->ineq
[u
][1 + dim
+ i
], g
);
2144 isl_int_neg(fu
, fu
);
2145 isl_seq_combine(ineq
, fl
, bmap
->ineq
[u
], fu
, bmap
->ineq
[l
],
2146 1 + dim
+ bmap
->n_div
);
2147 isl_int_add(ineq
[0], ineq
[0], fl
);
2148 isl_int_add(ineq
[0], ineq
[0], fu
);
2149 isl_int_sub_ui(ineq
[0], ineq
[0], 1);
2150 isl_int_mul(g
, g
, fl
);
2151 isl_int_mul(g
, g
, fu
);
2152 isl_int_sub(ineq
[0], ineq
[0], g
);
2155 /* Remove more kinds of divs that are not strictly needed.
2156 * In particular, if all pairs of lower and upper bounds on a div
2157 * are such that they allow at least one integer value of the div,
2158 * the we can eliminate the div using Fourier-Motzkin without
2159 * introducing any spurious solutions.
2161 static struct isl_basic_map
*drop_more_redundant_divs(
2162 struct isl_basic_map
*bmap
, int *pairs
, int n
)
2164 struct isl_tab
*tab
= NULL
;
2165 struct isl_vec
*vec
= NULL
;
2177 dim
= isl_dim_total(bmap
->dim
);
2178 vec
= isl_vec_alloc(bmap
->ctx
, 1 + dim
+ bmap
->n_div
);
2182 tab
= isl_tab_from_basic_map(bmap
);
2187 enum isl_lp_result res
;
2189 for (i
= 0; i
< bmap
->n_div
; ++i
) {
2192 if (best
>= 0 && pairs
[best
] <= pairs
[i
])
2198 for (l
= 0; l
< bmap
->n_ineq
; ++l
) {
2199 if (!isl_int_is_pos(bmap
->ineq
[l
][1 + dim
+ i
]))
2201 for (u
= 0; u
< bmap
->n_ineq
; ++u
) {
2202 if (!isl_int_is_neg(bmap
->ineq
[u
][1 + dim
+ i
]))
2204 construct_test_ineq(bmap
, i
, l
, u
,
2205 vec
->el
, g
, fl
, fu
);
2206 res
= isl_tab_min(tab
, vec
->el
,
2207 bmap
->ctx
->one
, &g
, NULL
, 0);
2208 if (res
== isl_lp_error
)
2210 if (res
== isl_lp_empty
) {
2211 bmap
= isl_basic_map_set_to_empty(bmap
);
2214 if (res
!= isl_lp_ok
|| isl_int_is_neg(g
))
2217 if (u
< bmap
->n_ineq
)
2220 if (l
== bmap
->n_ineq
) {
2240 bmap
= isl_basic_map_remove(bmap
, isl_dim_div
, remove
, 1);
2241 return isl_basic_map_drop_redundant_divs(bmap
);
2244 isl_basic_map_free(bmap
);
2253 /* Given a pair of divs div1 and div2 such that, expect for the lower bound l
2254 * and the upper bound u, div1 always occurs together with div2 in the form
2255 * (div1 + m div2), where m is the constant range on the variable div1
2256 * allowed by l and u, replace the pair div1 and div2 by a single
2257 * div that is equal to div1 + m div2.
2259 * The new div will appear in the location that contains div2.
2260 * We need to modify all constraints that contain
2261 * div2 = (div - div1) / m
2262 * (If a constraint does not contain div2, it will also not contain div1.)
2263 * If the constraint also contains div1, then we know they appear
2264 * as f (div1 + m div2) and we can simply replace (div1 + m div2) by div,
2265 * i.e., the coefficient of div is f.
2267 * Otherwise, we first need to introduce div1 into the constraint.
2276 * A lower bound on div2
2280 * can be replaced by
2282 * (n * (m div 2 + div1) + m t + n f)/g >= 0
2284 * with g = gcd(m,n).
2289 * can be replaced by
2291 * (-n * (m div2 + div1) + m t + n f')/g >= 0
2293 * These constraint are those that we would obtain from eliminating
2294 * div1 using Fourier-Motzkin.
2296 * After all constraints have been modified, we drop the lower and upper
2297 * bound and then drop div1.
2299 static struct isl_basic_map
*coalesce_divs(struct isl_basic_map
*bmap
,
2300 unsigned div1
, unsigned div2
, unsigned l
, unsigned u
)
2305 unsigned dim
, total
;
2308 dim
= isl_dim_total(bmap
->dim
);
2309 total
= 1 + dim
+ bmap
->n_div
;
2314 isl_int_add(m
, bmap
->ineq
[l
][0], bmap
->ineq
[u
][0]);
2315 isl_int_add_ui(m
, m
, 1);
2317 for (i
= 0; i
< bmap
->n_ineq
; ++i
) {
2318 if (i
== l
|| i
== u
)
2320 if (isl_int_is_zero(bmap
->ineq
[i
][1 + dim
+ div2
]))
2322 if (isl_int_is_zero(bmap
->ineq
[i
][1 + dim
+ div1
])) {
2323 isl_int_gcd(b
, m
, bmap
->ineq
[i
][1 + dim
+ div2
]);
2324 isl_int_divexact(a
, m
, b
);
2325 isl_int_divexact(b
, bmap
->ineq
[i
][1 + dim
+ div2
], b
);
2326 if (isl_int_is_pos(b
)) {
2327 isl_seq_combine(bmap
->ineq
[i
], a
, bmap
->ineq
[i
],
2328 b
, bmap
->ineq
[l
], total
);
2331 isl_seq_combine(bmap
->ineq
[i
], a
, bmap
->ineq
[i
],
2332 b
, bmap
->ineq
[u
], total
);
2335 isl_int_set(bmap
->ineq
[i
][1 + dim
+ div2
],
2336 bmap
->ineq
[i
][1 + dim
+ div1
]);
2337 isl_int_set_si(bmap
->ineq
[i
][1 + dim
+ div1
], 0);
2344 isl_basic_map_drop_inequality(bmap
, l
);
2345 isl_basic_map_drop_inequality(bmap
, u
);
2347 isl_basic_map_drop_inequality(bmap
, u
);
2348 isl_basic_map_drop_inequality(bmap
, l
);
2350 bmap
= isl_basic_map_drop_div(bmap
, div1
);
2354 /* First check if we can coalesce any pair of divs and
2355 * then continue with dropping more redundant divs.
2357 * We loop over all pairs of lower and upper bounds on a div
2358 * with coefficient 1 and -1, respectively, check if there
2359 * is any other div "c" with which we can coalesce the div
2360 * and if so, perform the coalescing.
2362 static struct isl_basic_map
*coalesce_or_drop_more_redundant_divs(
2363 struct isl_basic_map
*bmap
, int *pairs
, int n
)
2368 dim
= isl_dim_total(bmap
->dim
);
2370 for (i
= 0; i
< bmap
->n_div
; ++i
) {
2373 for (l
= 0; l
< bmap
->n_ineq
; ++l
) {
2374 if (!isl_int_is_one(bmap
->ineq
[l
][1 + dim
+ i
]))
2376 for (u
= 0; u
< bmap
->n_ineq
; ++u
) {
2379 if (!isl_int_is_negone(bmap
->ineq
[u
][1+dim
+i
]))
2381 c
= div_find_coalesce(bmap
, pairs
, i
, l
, u
);
2385 bmap
= coalesce_divs(bmap
, i
, c
, l
, u
);
2386 return isl_basic_map_drop_redundant_divs(bmap
);
2391 if (ISL_F_ISSET(bmap
, ISL_BASIC_MAP_EMPTY
))
2394 return drop_more_redundant_divs(bmap
, pairs
, n
);
2397 /* Remove divs that are not strictly needed.
2398 * In particular, if a div only occurs positively (or negatively)
2399 * in constraints, then it can simply be dropped.
2400 * Also, if a div occurs only occurs in two constraints and if moreover
2401 * those two constraints are opposite to each other, except for the constant
2402 * term and if the sum of the constant terms is such that for any value
2403 * of the other values, there is always at least one integer value of the
2404 * div, i.e., if one plus this sum is greater than or equal to
2405 * the (absolute value) of the coefficent of the div in the constraints,
2406 * then we can also simply drop the div.
2408 * If any divs are left after these simple checks then we move on
2409 * to more complicated cases in drop_more_redundant_divs.
2411 struct isl_basic_map
*isl_basic_map_drop_redundant_divs(
2412 struct isl_basic_map
*bmap
)
2422 off
= isl_dim_total(bmap
->dim
);
2423 pairs
= isl_calloc_array(bmap
->ctx
, int, bmap
->n_div
);
2427 for (i
= 0; i
< bmap
->n_div
; ++i
) {
2429 int last_pos
, last_neg
;
2433 defined
= !isl_int_is_zero(bmap
->div
[i
][0]);
2434 for (j
= 0; j
< bmap
->n_eq
; ++j
)
2435 if (!isl_int_is_zero(bmap
->eq
[j
][1 + off
+ i
]))
2441 for (j
= 0; j
< bmap
->n_ineq
; ++j
) {
2442 if (isl_int_is_pos(bmap
->ineq
[j
][1 + off
+ i
])) {
2446 if (isl_int_is_neg(bmap
->ineq
[j
][1 + off
+ i
])) {
2451 pairs
[i
] = pos
* neg
;
2452 if (pairs
[i
] == 0) {
2453 for (j
= bmap
->n_ineq
- 1; j
>= 0; --j
)
2454 if (!isl_int_is_zero(bmap
->ineq
[j
][1+off
+i
]))
2455 isl_basic_map_drop_inequality(bmap
, j
);
2456 bmap
= isl_basic_map_drop_div(bmap
, i
);
2458 return isl_basic_map_drop_redundant_divs(bmap
);
2462 if (!isl_seq_is_neg(bmap
->ineq
[last_pos
] + 1,
2463 bmap
->ineq
[last_neg
] + 1,
2467 isl_int_add(bmap
->ineq
[last_pos
][0],
2468 bmap
->ineq
[last_pos
][0], bmap
->ineq
[last_neg
][0]);
2469 isl_int_add_ui(bmap
->ineq
[last_pos
][0],
2470 bmap
->ineq
[last_pos
][0], 1);
2471 redundant
= isl_int_ge(bmap
->ineq
[last_pos
][0],
2472 bmap
->ineq
[last_pos
][1+off
+i
]);
2473 isl_int_sub_ui(bmap
->ineq
[last_pos
][0],
2474 bmap
->ineq
[last_pos
][0], 1);
2475 isl_int_sub(bmap
->ineq
[last_pos
][0],
2476 bmap
->ineq
[last_pos
][0], bmap
->ineq
[last_neg
][0]);
2479 !ok_to_set_div_from_bound(bmap
, i
, last_pos
)) {
2484 bmap
= set_div_from_lower_bound(bmap
, i
, last_pos
);
2485 bmap
= isl_basic_map_simplify(bmap
);
2487 return isl_basic_map_drop_redundant_divs(bmap
);
2489 if (last_pos
> last_neg
) {
2490 isl_basic_map_drop_inequality(bmap
, last_pos
);
2491 isl_basic_map_drop_inequality(bmap
, last_neg
);
2493 isl_basic_map_drop_inequality(bmap
, last_neg
);
2494 isl_basic_map_drop_inequality(bmap
, last_pos
);
2496 bmap
= isl_basic_map_drop_div(bmap
, i
);
2498 return isl_basic_map_drop_redundant_divs(bmap
);
2502 return coalesce_or_drop_more_redundant_divs(bmap
, pairs
, n
);
2508 isl_basic_map_free(bmap
);
2512 struct isl_basic_set
*isl_basic_set_drop_redundant_divs(
2513 struct isl_basic_set
*bset
)
2515 return (struct isl_basic_set
*)
2516 isl_basic_map_drop_redundant_divs((struct isl_basic_map
*)bset
);
2519 struct isl_map
*isl_map_drop_redundant_divs(struct isl_map
*map
)
2525 for (i
= 0; i
< map
->n
; ++i
) {
2526 map
->p
[i
] = isl_basic_map_drop_redundant_divs(map
->p
[i
]);
2530 ISL_F_CLR(map
, ISL_MAP_NORMALIZED
);
2537 struct isl_set
*isl_set_drop_redundant_divs(struct isl_set
*set
)
2539 return (struct isl_set
*)
2540 isl_map_drop_redundant_divs((struct isl_map
*)set
);