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_ctx_private.h>
11 #include <isl_map_private.h>
12 #include "isl_equalities.h"
16 #include <isl_dim_private.h>
17 #include <isl_mat_private.h>
19 static void swap_equality(struct isl_basic_map
*bmap
, int a
, int b
)
21 isl_int
*t
= bmap
->eq
[a
];
22 bmap
->eq
[a
] = bmap
->eq
[b
];
26 static void swap_inequality(struct isl_basic_map
*bmap
, int a
, int b
)
29 isl_int
*t
= bmap
->ineq
[a
];
30 bmap
->ineq
[a
] = bmap
->ineq
[b
];
35 static void set_swap_inequality(struct isl_basic_set
*bset
, int a
, int b
)
37 swap_inequality((struct isl_basic_map
*)bset
, a
, b
);
40 static void constraint_drop_vars(isl_int
*c
, unsigned n
, unsigned rem
)
42 isl_seq_cpy(c
, c
+ n
, rem
);
43 isl_seq_clr(c
+ rem
, n
);
46 /* Drop n dimensions starting at first.
48 * In principle, this frees up some extra variables as the number
49 * of columns remains constant, but we would have to extend
50 * the div array too as the number of rows in this array is assumed
51 * to be equal to extra.
53 struct isl_basic_set
*isl_basic_set_drop_dims(
54 struct isl_basic_set
*bset
, unsigned first
, unsigned n
)
61 isl_assert(bset
->ctx
, first
+ n
<= bset
->dim
->n_out
, goto error
);
63 if (n
== 0 && !isl_dim_get_tuple_name(bset
->dim
, isl_dim_set
))
66 bset
= isl_basic_set_cow(bset
);
70 for (i
= 0; i
< bset
->n_eq
; ++i
)
71 constraint_drop_vars(bset
->eq
[i
]+1+bset
->dim
->nparam
+first
, n
,
72 (bset
->dim
->n_out
-first
-n
)+bset
->extra
);
74 for (i
= 0; i
< bset
->n_ineq
; ++i
)
75 constraint_drop_vars(bset
->ineq
[i
]+1+bset
->dim
->nparam
+first
, n
,
76 (bset
->dim
->n_out
-first
-n
)+bset
->extra
);
78 for (i
= 0; i
< bset
->n_div
; ++i
)
79 constraint_drop_vars(bset
->div
[i
]+1+1+bset
->dim
->nparam
+first
, n
,
80 (bset
->dim
->n_out
-first
-n
)+bset
->extra
);
82 bset
->dim
= isl_dim_drop_outputs(bset
->dim
, first
, n
);
86 ISL_F_CLR(bset
, ISL_BASIC_SET_NORMALIZED
);
87 bset
= isl_basic_set_simplify(bset
);
88 return isl_basic_set_finalize(bset
);
90 isl_basic_set_free(bset
);
94 struct isl_set
*isl_set_drop_dims(
95 struct isl_set
*set
, unsigned first
, unsigned n
)
102 isl_assert(set
->ctx
, first
+ n
<= set
->dim
->n_out
, goto error
);
104 if (n
== 0 && !isl_dim_get_tuple_name(set
->dim
, isl_dim_set
))
106 set
= isl_set_cow(set
);
109 set
->dim
= isl_dim_drop_outputs(set
->dim
, first
, n
);
113 for (i
= 0; i
< set
->n
; ++i
) {
114 set
->p
[i
] = isl_basic_set_drop_dims(set
->p
[i
], first
, n
);
119 ISL_F_CLR(set
, ISL_SET_NORMALIZED
);
126 /* Move "n" divs starting at "first" to the end of the list of divs.
128 static struct isl_basic_map
*move_divs_last(struct isl_basic_map
*bmap
,
129 unsigned first
, unsigned n
)
134 if (first
+ n
== bmap
->n_div
)
137 div
= isl_alloc_array(bmap
->ctx
, isl_int
*, n
);
140 for (i
= 0; i
< n
; ++i
)
141 div
[i
] = bmap
->div
[first
+ i
];
142 for (i
= 0; i
< bmap
->n_div
- first
- n
; ++i
)
143 bmap
->div
[first
+ i
] = bmap
->div
[first
+ n
+ i
];
144 for (i
= 0; i
< n
; ++i
)
145 bmap
->div
[bmap
->n_div
- n
+ i
] = div
[i
];
149 isl_basic_map_free(bmap
);
153 /* Drop "n" dimensions of type "type" starting at "first".
155 * In principle, this frees up some extra variables as the number
156 * of columns remains constant, but we would have to extend
157 * the div array too as the number of rows in this array is assumed
158 * to be equal to extra.
160 struct isl_basic_map
*isl_basic_map_drop(struct isl_basic_map
*bmap
,
161 enum isl_dim_type type
, unsigned first
, unsigned n
)
171 dim
= isl_basic_map_dim(bmap
, type
);
172 isl_assert(bmap
->ctx
, first
+ n
<= dim
, goto error
);
174 if (n
== 0 && !isl_dim_get_tuple_name(bmap
->dim
, type
))
177 bmap
= isl_basic_map_cow(bmap
);
181 offset
= isl_basic_map_offset(bmap
, type
) + first
;
182 left
= isl_basic_map_total_dim(bmap
) - (offset
- 1) - n
;
183 for (i
= 0; i
< bmap
->n_eq
; ++i
)
184 constraint_drop_vars(bmap
->eq
[i
]+offset
, n
, left
);
186 for (i
= 0; i
< bmap
->n_ineq
; ++i
)
187 constraint_drop_vars(bmap
->ineq
[i
]+offset
, n
, left
);
189 for (i
= 0; i
< bmap
->n_div
; ++i
)
190 constraint_drop_vars(bmap
->div
[i
]+1+offset
, n
, left
);
192 if (type
== isl_dim_div
) {
193 bmap
= move_divs_last(bmap
, first
, n
);
196 isl_basic_map_free_div(bmap
, n
);
198 bmap
->dim
= isl_dim_drop(bmap
->dim
, type
, first
, n
);
202 ISL_F_CLR(bmap
, ISL_BASIC_MAP_NORMALIZED
);
203 bmap
= isl_basic_map_simplify(bmap
);
204 return isl_basic_map_finalize(bmap
);
206 isl_basic_map_free(bmap
);
210 __isl_give isl_basic_set
*isl_basic_set_drop(__isl_take isl_basic_set
*bset
,
211 enum isl_dim_type type
, unsigned first
, unsigned n
)
213 return (isl_basic_set
*)isl_basic_map_drop((isl_basic_map
*)bset
,
217 struct isl_basic_map
*isl_basic_map_drop_inputs(
218 struct isl_basic_map
*bmap
, unsigned first
, unsigned n
)
220 return isl_basic_map_drop(bmap
, isl_dim_in
, first
, n
);
223 struct isl_map
*isl_map_drop(struct isl_map
*map
,
224 enum isl_dim_type type
, unsigned first
, unsigned n
)
231 isl_assert(map
->ctx
, first
+ n
<= isl_map_dim(map
, type
), goto error
);
233 if (n
== 0 && !isl_dim_get_tuple_name(map
->dim
, type
))
235 map
= isl_map_cow(map
);
238 map
->dim
= isl_dim_drop(map
->dim
, type
, first
, n
);
242 for (i
= 0; i
< map
->n
; ++i
) {
243 map
->p
[i
] = isl_basic_map_drop(map
->p
[i
], type
, first
, n
);
247 ISL_F_CLR(map
, ISL_MAP_NORMALIZED
);
255 struct isl_set
*isl_set_drop(struct isl_set
*set
,
256 enum isl_dim_type type
, unsigned first
, unsigned n
)
258 return (isl_set
*)isl_map_drop((isl_map
*)set
, type
, first
, n
);
261 struct isl_map
*isl_map_drop_inputs(
262 struct isl_map
*map
, unsigned first
, unsigned n
)
264 return isl_map_drop(map
, isl_dim_in
, first
, n
);
268 * We don't cow, as the div is assumed to be redundant.
270 static struct isl_basic_map
*isl_basic_map_drop_div(
271 struct isl_basic_map
*bmap
, unsigned div
)
279 pos
= 1 + isl_dim_total(bmap
->dim
) + div
;
281 isl_assert(bmap
->ctx
, div
< bmap
->n_div
, goto error
);
283 for (i
= 0; i
< bmap
->n_eq
; ++i
)
284 constraint_drop_vars(bmap
->eq
[i
]+pos
, 1, bmap
->extra
-div
-1);
286 for (i
= 0; i
< bmap
->n_ineq
; ++i
) {
287 if (!isl_int_is_zero(bmap
->ineq
[i
][pos
])) {
288 isl_basic_map_drop_inequality(bmap
, i
);
292 constraint_drop_vars(bmap
->ineq
[i
]+pos
, 1, bmap
->extra
-div
-1);
295 for (i
= 0; i
< bmap
->n_div
; ++i
)
296 constraint_drop_vars(bmap
->div
[i
]+1+pos
, 1, bmap
->extra
-div
-1);
298 if (div
!= bmap
->n_div
- 1) {
300 isl_int
*t
= bmap
->div
[div
];
302 for (j
= div
; j
< bmap
->n_div
- 1; ++j
)
303 bmap
->div
[j
] = bmap
->div
[j
+1];
305 bmap
->div
[bmap
->n_div
- 1] = t
;
307 ISL_F_CLR(bmap
, ISL_BASIC_MAP_NORMALIZED
);
308 isl_basic_map_free_div(bmap
, 1);
312 isl_basic_map_free(bmap
);
316 struct isl_basic_map
*isl_basic_map_normalize_constraints(
317 struct isl_basic_map
*bmap
)
321 unsigned total
= isl_basic_map_total_dim(bmap
);
327 for (i
= bmap
->n_eq
- 1; i
>= 0; --i
) {
328 isl_seq_gcd(bmap
->eq
[i
]+1, total
, &gcd
);
329 if (isl_int_is_zero(gcd
)) {
330 if (!isl_int_is_zero(bmap
->eq
[i
][0])) {
331 bmap
= isl_basic_map_set_to_empty(bmap
);
334 isl_basic_map_drop_equality(bmap
, i
);
337 if (ISL_F_ISSET(bmap
, ISL_BASIC_MAP_RATIONAL
))
338 isl_int_gcd(gcd
, gcd
, bmap
->eq
[i
][0]);
339 if (isl_int_is_one(gcd
))
341 if (!isl_int_is_divisible_by(bmap
->eq
[i
][0], gcd
)) {
342 bmap
= isl_basic_map_set_to_empty(bmap
);
345 isl_seq_scale_down(bmap
->eq
[i
], bmap
->eq
[i
], gcd
, 1+total
);
348 for (i
= bmap
->n_ineq
- 1; i
>= 0; --i
) {
349 isl_seq_gcd(bmap
->ineq
[i
]+1, total
, &gcd
);
350 if (isl_int_is_zero(gcd
)) {
351 if (isl_int_is_neg(bmap
->ineq
[i
][0])) {
352 bmap
= isl_basic_map_set_to_empty(bmap
);
355 isl_basic_map_drop_inequality(bmap
, i
);
358 if (ISL_F_ISSET(bmap
, ISL_BASIC_MAP_RATIONAL
))
359 isl_int_gcd(gcd
, gcd
, bmap
->ineq
[i
][0]);
360 if (isl_int_is_one(gcd
))
362 isl_int_fdiv_q(bmap
->ineq
[i
][0], bmap
->ineq
[i
][0], gcd
);
363 isl_seq_scale_down(bmap
->ineq
[i
]+1, bmap
->ineq
[i
]+1, gcd
, total
);
370 struct isl_basic_set
*isl_basic_set_normalize_constraints(
371 struct isl_basic_set
*bset
)
373 return (struct isl_basic_set
*)isl_basic_map_normalize_constraints(
374 (struct isl_basic_map
*)bset
);
377 /* Assumes divs have been ordered if keep_divs is set.
379 static void eliminate_var_using_equality(struct isl_basic_map
*bmap
,
380 unsigned pos
, isl_int
*eq
, int keep_divs
, int *progress
)
386 total
= isl_basic_map_total_dim(bmap
);
387 last_div
= isl_seq_last_non_zero(eq
+ 1 + isl_dim_total(bmap
->dim
),
389 for (k
= 0; k
< bmap
->n_eq
; ++k
) {
390 if (bmap
->eq
[k
] == eq
)
392 if (isl_int_is_zero(bmap
->eq
[k
][1+pos
]))
396 isl_seq_elim(bmap
->eq
[k
], eq
, 1+pos
, 1+total
, NULL
);
397 isl_seq_normalize(bmap
->ctx
, bmap
->eq
[k
], 1 + total
);
400 for (k
= 0; k
< bmap
->n_ineq
; ++k
) {
401 if (isl_int_is_zero(bmap
->ineq
[k
][1+pos
]))
405 isl_seq_elim(bmap
->ineq
[k
], eq
, 1+pos
, 1+total
, NULL
);
406 isl_seq_normalize(bmap
->ctx
, bmap
->ineq
[k
], 1 + total
);
407 ISL_F_CLR(bmap
, ISL_BASIC_MAP_NORMALIZED
);
410 for (k
= 0; k
< bmap
->n_div
; ++k
) {
411 if (isl_int_is_zero(bmap
->div
[k
][0]))
413 if (isl_int_is_zero(bmap
->div
[k
][1+1+pos
]))
417 /* We need to be careful about circular definitions,
418 * so for now we just remove the definition of div k
419 * if the equality contains any divs.
420 * If keep_divs is set, then the divs have been ordered
421 * and we can keep the definition as long as the result
424 if (last_div
== -1 || (keep_divs
&& last_div
< k
))
425 isl_seq_elim(bmap
->div
[k
]+1, eq
,
426 1+pos
, 1+total
, &bmap
->div
[k
][0]);
428 isl_seq_clr(bmap
->div
[k
], 1 + total
);
429 ISL_F_CLR(bmap
, ISL_BASIC_MAP_NORMALIZED
);
433 /* Assumes divs have been ordered if keep_divs is set.
435 static void eliminate_div(struct isl_basic_map
*bmap
, isl_int
*eq
,
436 unsigned div
, int keep_divs
)
438 unsigned pos
= isl_dim_total(bmap
->dim
) + div
;
440 eliminate_var_using_equality(bmap
, pos
, eq
, keep_divs
, NULL
);
442 isl_basic_map_drop_div(bmap
, div
);
445 /* Check if elimination of div "div" using equality "eq" would not
446 * result in a div depending on a later div.
448 static int ok_to_eliminate_div(struct isl_basic_map
*bmap
, isl_int
*eq
,
453 unsigned pos
= isl_dim_total(bmap
->dim
) + div
;
455 last_div
= isl_seq_last_non_zero(eq
+ 1 + isl_dim_total(bmap
->dim
),
457 if (last_div
< 0 || last_div
<= div
)
460 for (k
= 0; k
<= last_div
; ++k
) {
461 if (isl_int_is_zero(bmap
->div
[k
][0]))
463 if (!isl_int_is_zero(bmap
->div
[k
][1 + 1 + pos
]))
470 /* Elimininate divs based on equalities
472 static struct isl_basic_map
*eliminate_divs_eq(
473 struct isl_basic_map
*bmap
, int *progress
)
480 bmap
= isl_basic_map_order_divs(bmap
);
485 off
= 1 + isl_dim_total(bmap
->dim
);
487 for (d
= bmap
->n_div
- 1; d
>= 0 ; --d
) {
488 for (i
= 0; i
< bmap
->n_eq
; ++i
) {
489 if (!isl_int_is_one(bmap
->eq
[i
][off
+ d
]) &&
490 !isl_int_is_negone(bmap
->eq
[i
][off
+ d
]))
492 if (!ok_to_eliminate_div(bmap
, bmap
->eq
[i
], d
))
496 eliminate_div(bmap
, bmap
->eq
[i
], d
, 1);
497 isl_basic_map_drop_equality(bmap
, i
);
502 return eliminate_divs_eq(bmap
, progress
);
506 /* Elimininate divs based on inequalities
508 static struct isl_basic_map
*eliminate_divs_ineq(
509 struct isl_basic_map
*bmap
, int *progress
)
520 off
= 1 + isl_dim_total(bmap
->dim
);
522 for (d
= bmap
->n_div
- 1; d
>= 0 ; --d
) {
523 for (i
= 0; i
< bmap
->n_eq
; ++i
)
524 if (!isl_int_is_zero(bmap
->eq
[i
][off
+ d
]))
528 for (i
= 0; i
< bmap
->n_ineq
; ++i
)
529 if (isl_int_abs_gt(bmap
->ineq
[i
][off
+ d
], ctx
->one
))
531 if (i
< bmap
->n_ineq
)
534 bmap
= isl_basic_map_eliminate_vars(bmap
, (off
-1)+d
, 1);
535 if (!bmap
|| ISL_F_ISSET(bmap
, ISL_BASIC_MAP_EMPTY
))
537 bmap
= isl_basic_map_drop_div(bmap
, d
);
544 struct isl_basic_map
*isl_basic_map_gauss(
545 struct isl_basic_map
*bmap
, int *progress
)
553 bmap
= isl_basic_map_order_divs(bmap
);
558 total
= isl_basic_map_total_dim(bmap
);
559 total_var
= total
- bmap
->n_div
;
561 last_var
= total
- 1;
562 for (done
= 0; done
< bmap
->n_eq
; ++done
) {
563 for (; last_var
>= 0; --last_var
) {
564 for (k
= done
; k
< bmap
->n_eq
; ++k
)
565 if (!isl_int_is_zero(bmap
->eq
[k
][1+last_var
]))
573 swap_equality(bmap
, k
, done
);
574 if (isl_int_is_neg(bmap
->eq
[done
][1+last_var
]))
575 isl_seq_neg(bmap
->eq
[done
], bmap
->eq
[done
], 1+total
);
577 eliminate_var_using_equality(bmap
, last_var
, bmap
->eq
[done
], 1,
580 if (last_var
>= total_var
&&
581 isl_int_is_zero(bmap
->div
[last_var
- total_var
][0])) {
582 unsigned div
= last_var
- total_var
;
583 isl_seq_neg(bmap
->div
[div
]+1, bmap
->eq
[done
], 1+total
);
584 isl_int_set_si(bmap
->div
[div
][1+1+last_var
], 0);
585 isl_int_set(bmap
->div
[div
][0],
586 bmap
->eq
[done
][1+last_var
]);
587 ISL_F_CLR(bmap
, ISL_BASIC_MAP_NORMALIZED
);
590 if (done
== bmap
->n_eq
)
592 for (k
= done
; k
< bmap
->n_eq
; ++k
) {
593 if (isl_int_is_zero(bmap
->eq
[k
][0]))
595 return isl_basic_map_set_to_empty(bmap
);
597 isl_basic_map_free_equality(bmap
, bmap
->n_eq
-done
);
601 struct isl_basic_set
*isl_basic_set_gauss(
602 struct isl_basic_set
*bset
, int *progress
)
604 return (struct isl_basic_set
*)isl_basic_map_gauss(
605 (struct isl_basic_map
*)bset
, progress
);
609 static unsigned int round_up(unsigned int v
)
620 static int hash_index(isl_int
***index
, unsigned int size
, int bits
,
621 struct isl_basic_map
*bmap
, int k
)
624 unsigned total
= isl_basic_map_total_dim(bmap
);
625 uint32_t hash
= isl_seq_get_hash_bits(bmap
->ineq
[k
]+1, total
, bits
);
626 for (h
= hash
; index
[h
]; h
= (h
+1) % size
)
627 if (&bmap
->ineq
[k
] != index
[h
] &&
628 isl_seq_eq(bmap
->ineq
[k
]+1, index
[h
][0]+1, total
))
633 static int set_hash_index(isl_int
***index
, unsigned int size
, int bits
,
634 struct isl_basic_set
*bset
, int k
)
636 return hash_index(index
, size
, bits
, (struct isl_basic_map
*)bset
, k
);
639 /* If we can eliminate more than one div, then we need to make
640 * sure we do it from last div to first div, in order not to
641 * change the position of the other divs that still need to
644 static struct isl_basic_map
*remove_duplicate_divs(
645 struct isl_basic_map
*bmap
, int *progress
)
657 if (!bmap
|| bmap
->n_div
<= 1)
660 total_var
= isl_dim_total(bmap
->dim
);
661 total
= total_var
+ bmap
->n_div
;
664 for (k
= bmap
->n_div
- 1; k
>= 0; --k
)
665 if (!isl_int_is_zero(bmap
->div
[k
][0]))
670 elim_for
= isl_calloc_array(ctx
, int, bmap
->n_div
);
671 size
= round_up(4 * bmap
->n_div
/ 3 - 1);
672 bits
= ffs(size
) - 1;
673 index
= isl_calloc_array(ctx
, int, size
);
676 eq
= isl_blk_alloc(ctx
, 1+total
);
677 if (isl_blk_is_error(eq
))
680 isl_seq_clr(eq
.data
, 1+total
);
681 index
[isl_seq_get_hash_bits(bmap
->div
[k
], 2+total
, bits
)] = k
+ 1;
682 for (--k
; k
>= 0; --k
) {
685 if (isl_int_is_zero(bmap
->div
[k
][0]))
688 hash
= isl_seq_get_hash_bits(bmap
->div
[k
], 2+total
, bits
);
689 for (h
= hash
; index
[h
]; h
= (h
+1) % size
)
690 if (isl_seq_eq(bmap
->div
[k
],
691 bmap
->div
[index
[h
]-1], 2+total
))
700 for (l
= bmap
->n_div
- 1; l
>= 0; --l
) {
704 isl_int_set_si(eq
.data
[1+total_var
+k
], -1);
705 isl_int_set_si(eq
.data
[1+total_var
+l
], 1);
706 eliminate_div(bmap
, eq
.data
, l
, 0);
707 isl_int_set_si(eq
.data
[1+total_var
+k
], 0);
708 isl_int_set_si(eq
.data
[1+total_var
+l
], 0);
711 isl_blk_free(ctx
, eq
);
718 static int n_pure_div_eq(struct isl_basic_map
*bmap
)
723 total
= isl_dim_total(bmap
->dim
);
724 for (i
= 0, j
= bmap
->n_div
-1; i
< bmap
->n_eq
; ++i
) {
725 while (j
>= 0 && isl_int_is_zero(bmap
->eq
[i
][1 + total
+ j
]))
729 if (isl_seq_first_non_zero(bmap
->eq
[i
] + 1 + total
, j
) != -1)
735 /* Normalize divs that appear in equalities.
737 * In particular, we assume that bmap contains some equalities
742 * and we want to replace the set of e_i by a minimal set and
743 * such that the new e_i have a canonical representation in terms
745 * If any of the equalities involves more than one divs, then
746 * we currently simply bail out.
748 * Let us first additionally assume that all equalities involve
749 * a div. The equalities then express modulo constraints on the
750 * remaining variables and we can use "parameter compression"
751 * to find a minimal set of constraints. The result is a transformation
753 * x = T(x') = x_0 + G x'
755 * with G a lower-triangular matrix with all elements below the diagonal
756 * non-negative and smaller than the diagonal element on the same row.
757 * We first normalize x_0 by making the same property hold in the affine
759 * The rows i of G with a 1 on the diagonal do not impose any modulo
760 * constraint and simply express x_i = x'_i.
761 * For each of the remaining rows i, we introduce a div and a corresponding
762 * equality. In particular
764 * g_ii e_j = x_i - g_i(x')
766 * where each x'_k is replaced either by x_k (if g_kk = 1) or the
767 * corresponding div (if g_kk != 1).
769 * If there are any equalities not involving any div, then we
770 * first apply a variable compression on the variables x:
772 * x = C x'' x'' = C_2 x
774 * and perform the above parameter compression on A C instead of on A.
775 * The resulting compression is then of the form
777 * x'' = T(x') = x_0 + G x'
779 * and in constructing the new divs and the corresponding equalities,
780 * we have to replace each x'', i.e., the x'_k with (g_kk = 1),
781 * by the corresponding row from C_2.
783 static struct isl_basic_map
*normalize_divs(
784 struct isl_basic_map
*bmap
, int *progress
)
791 struct isl_mat
*T
= NULL
;
792 struct isl_mat
*C
= NULL
;
793 struct isl_mat
*C2
= NULL
;
801 if (bmap
->n_div
== 0)
807 if (ISL_F_ISSET(bmap
, ISL_BASIC_MAP_NORMALIZED_DIVS
))
810 total
= isl_dim_total(bmap
->dim
);
811 div_eq
= n_pure_div_eq(bmap
);
815 if (div_eq
< bmap
->n_eq
) {
816 B
= isl_mat_sub_alloc6(bmap
->ctx
, bmap
->eq
, div_eq
,
817 bmap
->n_eq
- div_eq
, 0, 1 + total
);
818 C
= isl_mat_variable_compression(B
, &C2
);
822 bmap
= isl_basic_map_set_to_empty(bmap
);
829 d
= isl_vec_alloc(bmap
->ctx
, div_eq
);
832 for (i
= 0, j
= bmap
->n_div
-1; i
< div_eq
; ++i
) {
833 while (j
>= 0 && isl_int_is_zero(bmap
->eq
[i
][1 + total
+ j
]))
835 isl_int_set(d
->block
.data
[i
], bmap
->eq
[i
][1 + total
+ j
]);
837 B
= isl_mat_sub_alloc6(bmap
->ctx
, bmap
->eq
, 0, div_eq
, 0, 1 + total
);
840 B
= isl_mat_product(B
, C
);
844 T
= isl_mat_parameter_compression(B
, d
);
848 bmap
= isl_basic_map_set_to_empty(bmap
);
854 for (i
= 0; i
< T
->n_row
- 1; ++i
) {
855 isl_int_fdiv_q(v
, T
->row
[1 + i
][0], T
->row
[1 + i
][1 + i
]);
856 if (isl_int_is_zero(v
))
858 isl_mat_col_submul(T
, 0, v
, 1 + i
);
861 pos
= isl_alloc_array(bmap
->ctx
, int, T
->n_row
);
864 /* We have to be careful because dropping equalities may reorder them */
866 for (j
= bmap
->n_div
- 1; j
>= 0; --j
) {
867 for (i
= 0; i
< bmap
->n_eq
; ++i
)
868 if (!isl_int_is_zero(bmap
->eq
[i
][1 + total
+ j
]))
870 if (i
< bmap
->n_eq
) {
871 bmap
= isl_basic_map_drop_div(bmap
, j
);
872 isl_basic_map_drop_equality(bmap
, i
);
878 for (i
= 1; i
< T
->n_row
; ++i
) {
879 if (isl_int_is_one(T
->row
[i
][i
]))
884 if (needed
> dropped
) {
885 bmap
= isl_basic_map_extend_dim(bmap
, isl_dim_copy(bmap
->dim
),
890 for (i
= 1; i
< T
->n_row
; ++i
) {
891 if (isl_int_is_one(T
->row
[i
][i
]))
893 k
= isl_basic_map_alloc_div(bmap
);
894 pos
[i
] = 1 + total
+ k
;
895 isl_seq_clr(bmap
->div
[k
] + 1, 1 + total
+ bmap
->n_div
);
896 isl_int_set(bmap
->div
[k
][0], T
->row
[i
][i
]);
898 isl_seq_cpy(bmap
->div
[k
] + 1, C2
->row
[i
], 1 + total
);
900 isl_int_set_si(bmap
->div
[k
][1 + i
], 1);
901 for (j
= 0; j
< i
; ++j
) {
902 if (isl_int_is_zero(T
->row
[i
][j
]))
904 if (pos
[j
] < T
->n_row
&& C2
)
905 isl_seq_submul(bmap
->div
[k
] + 1, T
->row
[i
][j
],
906 C2
->row
[pos
[j
]], 1 + total
);
908 isl_int_neg(bmap
->div
[k
][1 + pos
[j
]],
911 j
= isl_basic_map_alloc_equality(bmap
);
912 isl_seq_neg(bmap
->eq
[j
], bmap
->div
[k
]+1, 1+total
+bmap
->n_div
);
913 isl_int_set(bmap
->eq
[j
][pos
[i
]], bmap
->div
[k
][0]);
922 ISL_F_SET(bmap
, ISL_BASIC_MAP_NORMALIZED_DIVS
);
932 static struct isl_basic_map
*set_div_from_lower_bound(
933 struct isl_basic_map
*bmap
, int div
, int ineq
)
935 unsigned total
= 1 + isl_dim_total(bmap
->dim
);
937 isl_seq_neg(bmap
->div
[div
] + 1, bmap
->ineq
[ineq
], total
+ bmap
->n_div
);
938 isl_int_set(bmap
->div
[div
][0], bmap
->ineq
[ineq
][total
+ div
]);
939 isl_int_add(bmap
->div
[div
][1], bmap
->div
[div
][1], bmap
->div
[div
][0]);
940 isl_int_sub_ui(bmap
->div
[div
][1], bmap
->div
[div
][1], 1);
941 isl_int_set_si(bmap
->div
[div
][1 + total
+ div
], 0);
946 /* Check whether it is ok to define a div based on an inequality.
947 * To avoid the introduction of circular definitions of divs, we
948 * do not allow such a definition if the resulting expression would refer to
949 * any other undefined divs or if any known div is defined in
950 * terms of the unknown div.
952 static int ok_to_set_div_from_bound(struct isl_basic_map
*bmap
,
956 unsigned total
= 1 + isl_dim_total(bmap
->dim
);
958 /* Not defined in terms of unknown divs */
959 for (j
= 0; j
< bmap
->n_div
; ++j
) {
962 if (isl_int_is_zero(bmap
->ineq
[ineq
][total
+ j
]))
964 if (isl_int_is_zero(bmap
->div
[j
][0]))
968 /* No other div defined in terms of this one => avoid loops */
969 for (j
= 0; j
< bmap
->n_div
; ++j
) {
972 if (isl_int_is_zero(bmap
->div
[j
][0]))
974 if (!isl_int_is_zero(bmap
->div
[j
][1 + total
+ div
]))
981 /* Given two constraints "k" and "l" that are opposite to each other,
982 * except for the constant term, check if we can use them
983 * to obtain an expression for one of the hitherto unknown divs.
984 * "sum" is the sum of the constant terms of the constraints.
985 * If this sum is strictly smaller than the coefficient of one
986 * of the divs, then this pair can be used define the div.
987 * To avoid the introduction of circular definitions of divs, we
988 * do not use the pair if the resulting expression would refer to
989 * any other undefined divs or if any known div is defined in
990 * terms of the unknown div.
992 static struct isl_basic_map
*check_for_div_constraints(
993 struct isl_basic_map
*bmap
, int k
, int l
, isl_int sum
, int *progress
)
996 unsigned total
= 1 + isl_dim_total(bmap
->dim
);
998 for (i
= 0; i
< bmap
->n_div
; ++i
) {
999 if (!isl_int_is_zero(bmap
->div
[i
][0]))
1001 if (isl_int_is_zero(bmap
->ineq
[k
][total
+ i
]))
1003 if (isl_int_abs_ge(sum
, bmap
->ineq
[k
][total
+ i
]))
1005 if (!ok_to_set_div_from_bound(bmap
, i
, k
))
1007 if (isl_int_is_pos(bmap
->ineq
[k
][total
+ i
]))
1008 bmap
= set_div_from_lower_bound(bmap
, i
, k
);
1010 bmap
= set_div_from_lower_bound(bmap
, i
, l
);
1018 static struct isl_basic_map
*remove_duplicate_constraints(
1019 struct isl_basic_map
*bmap
, int *progress
, int detect_divs
)
1025 unsigned total
= isl_basic_map_total_dim(bmap
);
1028 if (!bmap
|| bmap
->n_ineq
<= 1)
1031 size
= round_up(4 * (bmap
->n_ineq
+1) / 3 - 1);
1032 bits
= ffs(size
) - 1;
1033 index
= isl_calloc_array(ctx
, isl_int
**, size
);
1037 index
[isl_seq_get_hash_bits(bmap
->ineq
[0]+1, total
, bits
)] = &bmap
->ineq
[0];
1038 for (k
= 1; k
< bmap
->n_ineq
; ++k
) {
1039 h
= hash_index(index
, size
, bits
, bmap
, k
);
1041 index
[h
] = &bmap
->ineq
[k
];
1046 l
= index
[h
] - &bmap
->ineq
[0];
1047 if (isl_int_lt(bmap
->ineq
[k
][0], bmap
->ineq
[l
][0]))
1048 swap_inequality(bmap
, k
, l
);
1049 isl_basic_map_drop_inequality(bmap
, k
);
1053 for (k
= 0; k
< bmap
->n_ineq
-1; ++k
) {
1054 isl_seq_neg(bmap
->ineq
[k
]+1, bmap
->ineq
[k
]+1, total
);
1055 h
= hash_index(index
, size
, bits
, bmap
, k
);
1056 isl_seq_neg(bmap
->ineq
[k
]+1, bmap
->ineq
[k
]+1, total
);
1059 l
= index
[h
] - &bmap
->ineq
[0];
1060 isl_int_add(sum
, bmap
->ineq
[k
][0], bmap
->ineq
[l
][0]);
1061 if (isl_int_is_pos(sum
)) {
1063 bmap
= check_for_div_constraints(bmap
, k
, l
,
1067 if (isl_int_is_zero(sum
)) {
1068 /* We need to break out of the loop after these
1069 * changes since the contents of the hash
1070 * will no longer be valid.
1071 * Plus, we probably we want to regauss first.
1075 isl_basic_map_drop_inequality(bmap
, l
);
1076 isl_basic_map_inequality_to_equality(bmap
, k
);
1078 bmap
= isl_basic_map_set_to_empty(bmap
);
1088 struct isl_basic_map
*isl_basic_map_simplify(struct isl_basic_map
*bmap
)
1095 bmap
= isl_basic_map_normalize_constraints(bmap
);
1096 bmap
= remove_duplicate_divs(bmap
, &progress
);
1097 bmap
= eliminate_divs_eq(bmap
, &progress
);
1098 bmap
= eliminate_divs_ineq(bmap
, &progress
);
1099 bmap
= isl_basic_map_gauss(bmap
, &progress
);
1100 /* requires equalities in normal form */
1101 bmap
= normalize_divs(bmap
, &progress
);
1102 bmap
= remove_duplicate_constraints(bmap
, &progress
, 1);
1107 struct isl_basic_set
*isl_basic_set_simplify(struct isl_basic_set
*bset
)
1109 return (struct isl_basic_set
*)
1110 isl_basic_map_simplify((struct isl_basic_map
*)bset
);
1114 int isl_basic_map_is_div_constraint(__isl_keep isl_basic_map
*bmap
,
1115 isl_int
*constraint
, unsigned div
)
1122 pos
= 1 + isl_dim_total(bmap
->dim
) + div
;
1124 if (isl_int_eq(constraint
[pos
], bmap
->div
[div
][0])) {
1126 isl_int_sub(bmap
->div
[div
][1],
1127 bmap
->div
[div
][1], bmap
->div
[div
][0]);
1128 isl_int_add_ui(bmap
->div
[div
][1], bmap
->div
[div
][1], 1);
1129 neg
= isl_seq_is_neg(constraint
, bmap
->div
[div
]+1, pos
);
1130 isl_int_sub_ui(bmap
->div
[div
][1], bmap
->div
[div
][1], 1);
1131 isl_int_add(bmap
->div
[div
][1],
1132 bmap
->div
[div
][1], bmap
->div
[div
][0]);
1135 if (isl_seq_first_non_zero(constraint
+pos
+1,
1136 bmap
->n_div
-div
-1) != -1)
1138 } else if (isl_int_abs_eq(constraint
[pos
], bmap
->div
[div
][0])) {
1139 if (!isl_seq_eq(constraint
, bmap
->div
[div
]+1, pos
))
1141 if (isl_seq_first_non_zero(constraint
+pos
+1,
1142 bmap
->n_div
-div
-1) != -1)
1151 /* If the only constraints a div d=floor(f/m)
1152 * appears in are its two defining constraints
1155 * -(f - (m - 1)) + m d >= 0
1157 * then it can safely be removed.
1159 static int div_is_redundant(struct isl_basic_map
*bmap
, int div
)
1162 unsigned pos
= 1 + isl_dim_total(bmap
->dim
) + div
;
1164 for (i
= 0; i
< bmap
->n_eq
; ++i
)
1165 if (!isl_int_is_zero(bmap
->eq
[i
][pos
]))
1168 for (i
= 0; i
< bmap
->n_ineq
; ++i
) {
1169 if (isl_int_is_zero(bmap
->ineq
[i
][pos
]))
1171 if (!isl_basic_map_is_div_constraint(bmap
, bmap
->ineq
[i
], div
))
1175 for (i
= 0; i
< bmap
->n_div
; ++i
)
1176 if (!isl_int_is_zero(bmap
->div
[i
][1+pos
]))
1183 * Remove divs that don't occur in any of the constraints or other divs.
1184 * These can arise when dropping some of the variables in a quast
1185 * returned by piplib.
1187 static struct isl_basic_map
*remove_redundant_divs(struct isl_basic_map
*bmap
)
1194 for (i
= bmap
->n_div
-1; i
>= 0; --i
) {
1195 if (!div_is_redundant(bmap
, i
))
1197 bmap
= isl_basic_map_drop_div(bmap
, i
);
1202 struct isl_basic_map
*isl_basic_map_finalize(struct isl_basic_map
*bmap
)
1204 bmap
= remove_redundant_divs(bmap
);
1207 ISL_F_SET(bmap
, ISL_BASIC_SET_FINAL
);
1211 struct isl_basic_set
*isl_basic_set_finalize(struct isl_basic_set
*bset
)
1213 return (struct isl_basic_set
*)
1214 isl_basic_map_finalize((struct isl_basic_map
*)bset
);
1217 struct isl_set
*isl_set_finalize(struct isl_set
*set
)
1223 for (i
= 0; i
< set
->n
; ++i
) {
1224 set
->p
[i
] = isl_basic_set_finalize(set
->p
[i
]);
1234 struct isl_map
*isl_map_finalize(struct isl_map
*map
)
1240 for (i
= 0; i
< map
->n
; ++i
) {
1241 map
->p
[i
] = isl_basic_map_finalize(map
->p
[i
]);
1245 ISL_F_CLR(map
, ISL_MAP_NORMALIZED
);
1253 /* Remove definition of any div that is defined in terms of the given variable.
1254 * The div itself is not removed. Functions such as
1255 * eliminate_divs_ineq depend on the other divs remaining in place.
1257 static struct isl_basic_map
*remove_dependent_vars(struct isl_basic_map
*bmap
,
1262 for (i
= 0; i
< bmap
->n_div
; ++i
) {
1263 if (isl_int_is_zero(bmap
->div
[i
][0]))
1265 if (isl_int_is_zero(bmap
->div
[i
][1+1+pos
]))
1267 isl_int_set_si(bmap
->div
[i
][0], 0);
1272 /* Eliminate the specified variables from the constraints using
1273 * Fourier-Motzkin. The variables themselves are not removed.
1275 struct isl_basic_map
*isl_basic_map_eliminate_vars(
1276 struct isl_basic_map
*bmap
, unsigned pos
, unsigned n
)
1286 total
= isl_basic_map_total_dim(bmap
);
1288 bmap
= isl_basic_map_cow(bmap
);
1289 for (d
= pos
+ n
- 1; d
>= 0 && d
>= pos
; --d
)
1290 bmap
= remove_dependent_vars(bmap
, d
);
1292 for (d
= pos
+ n
- 1;
1293 d
>= 0 && d
>= total
- bmap
->n_div
&& d
>= pos
; --d
)
1294 isl_seq_clr(bmap
->div
[d
-(total
-bmap
->n_div
)], 2+total
);
1295 for (d
= pos
+ n
- 1; d
>= 0 && d
>= pos
; --d
) {
1296 int n_lower
, n_upper
;
1299 for (i
= 0; i
< bmap
->n_eq
; ++i
) {
1300 if (isl_int_is_zero(bmap
->eq
[i
][1+d
]))
1302 eliminate_var_using_equality(bmap
, d
, bmap
->eq
[i
], 0, NULL
);
1303 isl_basic_map_drop_equality(bmap
, i
);
1310 for (i
= 0; i
< bmap
->n_ineq
; ++i
) {
1311 if (isl_int_is_pos(bmap
->ineq
[i
][1+d
]))
1313 else if (isl_int_is_neg(bmap
->ineq
[i
][1+d
]))
1316 bmap
= isl_basic_map_extend_constraints(bmap
,
1317 0, n_lower
* n_upper
);
1320 for (i
= bmap
->n_ineq
- 1; i
>= 0; --i
) {
1322 if (isl_int_is_zero(bmap
->ineq
[i
][1+d
]))
1325 for (j
= 0; j
< i
; ++j
) {
1326 if (isl_int_is_zero(bmap
->ineq
[j
][1+d
]))
1329 if (isl_int_sgn(bmap
->ineq
[i
][1+d
]) ==
1330 isl_int_sgn(bmap
->ineq
[j
][1+d
]))
1332 k
= isl_basic_map_alloc_inequality(bmap
);
1335 isl_seq_cpy(bmap
->ineq
[k
], bmap
->ineq
[i
],
1337 isl_seq_elim(bmap
->ineq
[k
], bmap
->ineq
[j
],
1338 1+d
, 1+total
, NULL
);
1340 isl_basic_map_drop_inequality(bmap
, i
);
1343 if (n_lower
> 0 && n_upper
> 0) {
1344 bmap
= isl_basic_map_normalize_constraints(bmap
);
1345 bmap
= remove_duplicate_constraints(bmap
, NULL
, 0);
1346 bmap
= isl_basic_map_gauss(bmap
, NULL
);
1347 bmap
= isl_basic_map_remove_redundancies(bmap
);
1350 if (ISL_F_ISSET(bmap
, ISL_BASIC_MAP_EMPTY
))
1354 ISL_F_CLR(bmap
, ISL_BASIC_MAP_NORMALIZED
);
1357 isl_basic_map_free(bmap
);
1361 struct isl_basic_set
*isl_basic_set_eliminate_vars(
1362 struct isl_basic_set
*bset
, unsigned pos
, unsigned n
)
1364 return (struct isl_basic_set
*)isl_basic_map_eliminate_vars(
1365 (struct isl_basic_map
*)bset
, pos
, n
);
1368 /* Don't assume equalities are in order, because align_divs
1369 * may have changed the order of the divs.
1371 static void compute_elimination_index(struct isl_basic_map
*bmap
, int *elim
)
1376 total
= isl_dim_total(bmap
->dim
);
1377 for (d
= 0; d
< total
; ++d
)
1379 for (i
= 0; i
< bmap
->n_eq
; ++i
) {
1380 for (d
= total
- 1; d
>= 0; --d
) {
1381 if (isl_int_is_zero(bmap
->eq
[i
][1+d
]))
1389 static void set_compute_elimination_index(struct isl_basic_set
*bset
, int *elim
)
1391 compute_elimination_index((struct isl_basic_map
*)bset
, elim
);
1394 static int reduced_using_equalities(isl_int
*dst
, isl_int
*src
,
1395 struct isl_basic_map
*bmap
, int *elim
)
1401 total
= isl_dim_total(bmap
->dim
);
1402 for (d
= total
- 1; d
>= 0; --d
) {
1403 if (isl_int_is_zero(src
[1+d
]))
1408 isl_seq_cpy(dst
, src
, 1 + total
);
1411 isl_seq_elim(dst
, bmap
->eq
[elim
[d
]], 1 + d
, 1 + total
, NULL
);
1416 static int set_reduced_using_equalities(isl_int
*dst
, isl_int
*src
,
1417 struct isl_basic_set
*bset
, int *elim
)
1419 return reduced_using_equalities(dst
, src
,
1420 (struct isl_basic_map
*)bset
, elim
);
1423 static struct isl_basic_set
*isl_basic_set_reduce_using_equalities(
1424 struct isl_basic_set
*bset
, struct isl_basic_set
*context
)
1429 if (!bset
|| !context
)
1432 if (context
->n_eq
== 0) {
1433 isl_basic_set_free(context
);
1437 bset
= isl_basic_set_cow(bset
);
1441 elim
= isl_alloc_array(bset
->ctx
, int, isl_basic_set_n_dim(bset
));
1444 set_compute_elimination_index(context
, elim
);
1445 for (i
= 0; i
< bset
->n_eq
; ++i
)
1446 set_reduced_using_equalities(bset
->eq
[i
], bset
->eq
[i
],
1448 for (i
= 0; i
< bset
->n_ineq
; ++i
)
1449 set_reduced_using_equalities(bset
->ineq
[i
], bset
->ineq
[i
],
1451 isl_basic_set_free(context
);
1453 bset
= isl_basic_set_simplify(bset
);
1454 bset
= isl_basic_set_finalize(bset
);
1457 isl_basic_set_free(bset
);
1458 isl_basic_set_free(context
);
1462 static struct isl_basic_set
*remove_shifted_constraints(
1463 struct isl_basic_set
*bset
, struct isl_basic_set
*context
)
1473 size
= round_up(4 * (context
->n_ineq
+1) / 3 - 1);
1474 bits
= ffs(size
) - 1;
1475 index
= isl_calloc_array(ctx
, isl_int
**, size
);
1479 for (k
= 0; k
< context
->n_ineq
; ++k
) {
1480 h
= set_hash_index(index
, size
, bits
, context
, k
);
1481 index
[h
] = &context
->ineq
[k
];
1483 for (k
= 0; k
< bset
->n_ineq
; ++k
) {
1484 h
= set_hash_index(index
, size
, bits
, bset
, k
);
1487 l
= index
[h
] - &context
->ineq
[0];
1488 if (isl_int_lt(bset
->ineq
[k
][0], context
->ineq
[l
][0]))
1490 bset
= isl_basic_set_cow(bset
);
1493 isl_basic_set_drop_inequality(bset
, k
);
1503 /* Tighten (decrease) the constant terms of the inequalities based
1504 * on the equalities, without removing any integer points.
1505 * For example, if there is an equality
1513 * then we want to replace the inequality by
1517 * We do this by computing a variable compression and translating
1518 * the constraints to the compressed space.
1519 * If any constraint has coefficients (except the contant term)
1520 * with a common factor "f", then we can replace the constant term "c"
1527 * f * floor(c/f) - c = -fract(c/f)
1529 * and we can add the same value to the original constraint.
1531 * In the example, the compressed space only contains "j",
1532 * and the inequality translates to
1536 * We add -fract(-1/3) = -2 to the original constraint to obtain
1540 static struct isl_basic_set
*normalize_constraints_in_compressed_space(
1541 struct isl_basic_set
*bset
)
1545 struct isl_mat
*B
, *C
;
1551 if (ISL_F_ISSET(bset
, ISL_BASIC_SET_RATIONAL
))
1557 bset
= isl_basic_set_cow(bset
);
1561 total
= isl_basic_set_total_dim(bset
);
1562 B
= isl_mat_sub_alloc6(bset
->ctx
, bset
->eq
, 0, bset
->n_eq
, 0, 1 + total
);
1563 C
= isl_mat_variable_compression(B
, NULL
);
1566 if (C
->n_col
== 0) {
1568 return isl_basic_set_set_to_empty(bset
);
1570 B
= isl_mat_sub_alloc6(bset
->ctx
, bset
->ineq
,
1571 0, bset
->n_ineq
, 0, 1 + total
);
1572 C
= isl_mat_product(B
, C
);
1577 for (i
= 0; i
< bset
->n_ineq
; ++i
) {
1578 isl_seq_gcd(C
->row
[i
] + 1, C
->n_col
- 1, &gcd
);
1579 if (isl_int_is_one(gcd
))
1581 isl_int_fdiv_r(C
->row
[i
][0], C
->row
[i
][0], gcd
);
1582 isl_int_sub(bset
->ineq
[i
][0], bset
->ineq
[i
][0], C
->row
[i
][0]);
1591 /* Remove all information from bset that is redundant in the context
1592 * of context. Both bset and context are assumed to be full-dimensional.
1594 * We first * remove the inequalities from "bset"
1595 * that are obviously redundant with respect to some inequality in "context".
1597 * If there are any inequalities left, we construct a tableau for
1598 * the context and then add the inequalities of "bset".
1599 * Before adding these inequalities, we freeze all constraints such that
1600 * they won't be considered redundant in terms of the constraints of "bset".
1601 * Then we detect all redundant constraints (among the
1602 * constraints that weren't frozen), first by checking for redundancy in the
1603 * the tableau and then by checking if replacing a constraint by its negation
1604 * would lead to an empty set. This last step is fairly expensive
1605 * and could be optimized by more reuse of the tableau.
1606 * Finally, we update bset according to the results.
1608 static __isl_give isl_basic_set
*uset_gist_full(__isl_take isl_basic_set
*bset
,
1609 __isl_take isl_basic_set
*context
)
1612 isl_basic_set
*combined
= NULL
;
1613 struct isl_tab
*tab
= NULL
;
1614 unsigned context_ineq
;
1617 if (!bset
|| !context
)
1620 if (isl_basic_set_is_universe(bset
)) {
1621 isl_basic_set_free(context
);
1625 if (isl_basic_set_is_universe(context
)) {
1626 isl_basic_set_free(context
);
1630 bset
= remove_shifted_constraints(bset
, context
);
1633 if (bset
->n_ineq
== 0)
1636 context_ineq
= context
->n_ineq
;
1637 combined
= isl_basic_set_cow(isl_basic_set_copy(context
));
1638 combined
= isl_basic_set_extend_constraints(combined
, 0, bset
->n_ineq
);
1639 tab
= isl_tab_from_basic_set(combined
);
1640 for (i
= 0; i
< context_ineq
; ++i
)
1641 if (isl_tab_freeze_constraint(tab
, i
) < 0)
1643 tab
= isl_tab_extend(tab
, bset
->n_ineq
);
1644 for (i
= 0; i
< bset
->n_ineq
; ++i
)
1645 if (isl_tab_add_ineq(tab
, bset
->ineq
[i
]) < 0)
1647 bset
= isl_basic_set_add_constraints(combined
, bset
, 0);
1651 if (isl_tab_detect_redundant(tab
) < 0)
1653 total
= isl_basic_set_total_dim(bset
);
1654 for (i
= context_ineq
; i
< bset
->n_ineq
; ++i
) {
1656 if (tab
->con
[i
].is_redundant
)
1658 tab
->con
[i
].is_redundant
= 1;
1659 combined
= isl_basic_set_dup(bset
);
1660 combined
= isl_basic_set_update_from_tab(combined
, tab
);
1661 combined
= isl_basic_set_extend_constraints(combined
, 0, 1);
1662 k
= isl_basic_set_alloc_inequality(combined
);
1665 isl_seq_neg(combined
->ineq
[k
], bset
->ineq
[i
], 1 + total
);
1666 isl_int_sub_ui(combined
->ineq
[k
][0], combined
->ineq
[k
][0], 1);
1667 is_empty
= isl_basic_set_is_empty(combined
);
1670 isl_basic_set_free(combined
);
1673 tab
->con
[i
].is_redundant
= 0;
1675 for (i
= 0; i
< context_ineq
; ++i
)
1676 tab
->con
[i
].is_redundant
= 1;
1677 bset
= isl_basic_set_update_from_tab(bset
, tab
);
1679 ISL_F_SET(bset
, ISL_BASIC_SET_NO_IMPLICIT
);
1680 ISL_F_SET(bset
, ISL_BASIC_SET_NO_REDUNDANT
);
1685 bset
= isl_basic_set_simplify(bset
);
1686 bset
= isl_basic_set_finalize(bset
);
1687 isl_basic_set_free(context
);
1691 isl_basic_set_free(combined
);
1692 isl_basic_set_free(context
);
1693 isl_basic_set_free(bset
);
1697 /* Remove all information from bset that is redundant in the context
1698 * of context. In particular, equalities that are linear combinations
1699 * of those in context are removed. Then the inequalities that are
1700 * redundant in the context of the equalities and inequalities of
1701 * context are removed.
1703 * We first compute the integer affine hull of the intersection,
1704 * compute the gist inside this affine hull and then add back
1705 * those equalities that are not implied by the context.
1707 * If two constraints are mutually redundant, then uset_gist_full
1708 * will remove the second of those constraints. We therefore first
1709 * sort the constraints so that constraints not involving existentially
1710 * quantified variables are given precedence over those that do.
1711 * We have to perform this sorting before the variable compression,
1712 * because that may effect the order of the variables.
1714 static __isl_give isl_basic_set
*uset_gist(__isl_take isl_basic_set
*bset
,
1715 __isl_take isl_basic_set
*context
)
1720 isl_basic_set
*aff_context
;
1723 if (!bset
|| !context
)
1726 bset
= isl_basic_set_intersect(bset
, isl_basic_set_copy(context
));
1727 if (isl_basic_set_plain_is_empty(bset
)) {
1728 isl_basic_set_free(context
);
1731 bset
= isl_basic_set_sort_constraints(bset
);
1732 aff
= isl_basic_set_affine_hull(isl_basic_set_copy(bset
));
1735 if (isl_basic_set_plain_is_empty(aff
)) {
1736 isl_basic_set_free(aff
);
1737 isl_basic_set_free(context
);
1740 if (aff
->n_eq
== 0) {
1741 isl_basic_set_free(aff
);
1742 return uset_gist_full(bset
, context
);
1744 total
= isl_basic_set_total_dim(bset
);
1745 eq
= isl_mat_sub_alloc6(bset
->ctx
, aff
->eq
, 0, aff
->n_eq
, 0, 1 + total
);
1746 eq
= isl_mat_cow(eq
);
1747 T
= isl_mat_variable_compression(eq
, &T2
);
1748 if (T
&& T
->n_col
== 0) {
1751 isl_basic_set_free(context
);
1752 isl_basic_set_free(aff
);
1753 return isl_basic_set_set_to_empty(bset
);
1756 aff_context
= isl_basic_set_affine_hull(isl_basic_set_copy(context
));
1758 bset
= isl_basic_set_preimage(bset
, isl_mat_copy(T
));
1759 context
= isl_basic_set_preimage(context
, T
);
1761 bset
= uset_gist_full(bset
, context
);
1762 bset
= isl_basic_set_preimage(bset
, T2
);
1763 bset
= isl_basic_set_intersect(bset
, aff
);
1764 bset
= isl_basic_set_reduce_using_equalities(bset
, aff_context
);
1767 ISL_F_SET(bset
, ISL_BASIC_SET_NO_IMPLICIT
);
1768 ISL_F_SET(bset
, ISL_BASIC_SET_NO_REDUNDANT
);
1773 isl_basic_set_free(bset
);
1774 isl_basic_set_free(context
);
1778 /* Normalize the divs in "bmap" in the context of the equalities in "context".
1779 * We simply add the equalities in context to bmap and then do a regular
1780 * div normalizations. Better results can be obtained by normalizing
1781 * only the divs in bmap than do not also appear in context.
1782 * We need to be careful to reduce the divs using the equalities
1783 * so that later calls to isl_basic_map_overlying_set wouldn't introduce
1784 * spurious constraints.
1786 static struct isl_basic_map
*normalize_divs_in_context(
1787 struct isl_basic_map
*bmap
, struct isl_basic_map
*context
)
1790 unsigned total_context
;
1793 div_eq
= n_pure_div_eq(bmap
);
1797 if (context
->n_div
> 0)
1798 bmap
= isl_basic_map_align_divs(bmap
, context
);
1800 total_context
= isl_basic_map_total_dim(context
);
1801 bmap
= isl_basic_map_extend_constraints(bmap
, context
->n_eq
, 0);
1802 for (i
= 0; i
< context
->n_eq
; ++i
) {
1804 k
= isl_basic_map_alloc_equality(bmap
);
1805 isl_seq_cpy(bmap
->eq
[k
], context
->eq
[i
], 1 + total_context
);
1806 isl_seq_clr(bmap
->eq
[k
] + 1 + total_context
,
1807 isl_basic_map_total_dim(bmap
) - total_context
);
1809 bmap
= isl_basic_map_gauss(bmap
, NULL
);
1810 bmap
= normalize_divs(bmap
, NULL
);
1811 bmap
= isl_basic_map_gauss(bmap
, NULL
);
1815 struct isl_basic_map
*isl_basic_map_gist(struct isl_basic_map
*bmap
,
1816 struct isl_basic_map
*context
)
1818 struct isl_basic_set
*bset
;
1820 if (!bmap
|| !context
)
1823 if (isl_basic_map_is_universe(bmap
)) {
1824 isl_basic_map_free(context
);
1827 if (isl_basic_map_plain_is_empty(context
)) {
1828 struct isl_dim
*dim
= isl_dim_copy(bmap
->dim
);
1829 isl_basic_map_free(context
);
1830 isl_basic_map_free(bmap
);
1831 return isl_basic_map_universe(dim
);
1833 if (isl_basic_map_plain_is_empty(bmap
)) {
1834 isl_basic_map_free(context
);
1838 bmap
= isl_basic_map_remove_redundancies(bmap
);
1839 context
= isl_basic_map_remove_redundancies(context
);
1842 bmap
= normalize_divs_in_context(bmap
, context
);
1844 context
= isl_basic_map_align_divs(context
, bmap
);
1845 bmap
= isl_basic_map_align_divs(bmap
, context
);
1847 bset
= uset_gist(isl_basic_map_underlying_set(isl_basic_map_copy(bmap
)),
1848 isl_basic_map_underlying_set(context
));
1850 return isl_basic_map_overlying_set(bset
, bmap
);
1852 isl_basic_map_free(bmap
);
1853 isl_basic_map_free(context
);
1858 * Assumes context has no implicit divs.
1860 __isl_give isl_map
*isl_map_gist_basic_map(__isl_take isl_map
*map
,
1861 __isl_take isl_basic_map
*context
)
1865 if (!map
|| !context
)
1868 if (isl_basic_map_plain_is_empty(context
)) {
1869 struct isl_dim
*dim
= isl_dim_copy(map
->dim
);
1870 isl_basic_map_free(context
);
1872 return isl_map_universe(dim
);
1875 context
= isl_basic_map_remove_redundancies(context
);
1876 map
= isl_map_cow(map
);
1877 if (!map
|| !context
)
1879 isl_assert(map
->ctx
, isl_dim_equal(map
->dim
, context
->dim
), goto error
);
1880 map
= isl_map_compute_divs(map
);
1881 for (i
= 0; i
< map
->n
; ++i
)
1882 context
= isl_basic_map_align_divs(context
, map
->p
[i
]);
1883 for (i
= map
->n
- 1; i
>= 0; --i
) {
1884 map
->p
[i
] = isl_basic_map_gist(map
->p
[i
],
1885 isl_basic_map_copy(context
));
1888 if (isl_basic_map_plain_is_empty(map
->p
[i
])) {
1889 isl_basic_map_free(map
->p
[i
]);
1890 if (i
!= map
->n
- 1)
1891 map
->p
[i
] = map
->p
[map
->n
- 1];
1895 isl_basic_map_free(context
);
1896 ISL_F_CLR(map
, ISL_MAP_NORMALIZED
);
1900 isl_basic_map_free(context
);
1904 __isl_give isl_map
*isl_map_gist(__isl_take isl_map
*map
,
1905 __isl_take isl_map
*context
)
1907 context
= isl_map_compute_divs(context
);
1908 return isl_map_gist_basic_map(map
, isl_map_simple_hull(context
));
1911 struct isl_basic_set
*isl_basic_set_gist(struct isl_basic_set
*bset
,
1912 struct isl_basic_set
*context
)
1914 return (struct isl_basic_set
*)isl_basic_map_gist(
1915 (struct isl_basic_map
*)bset
, (struct isl_basic_map
*)context
);
1918 __isl_give isl_set
*isl_set_gist_basic_set(__isl_take isl_set
*set
,
1919 __isl_take isl_basic_set
*context
)
1921 return (struct isl_set
*)isl_map_gist_basic_map((struct isl_map
*)set
,
1922 (struct isl_basic_map
*)context
);
1925 __isl_give isl_set
*isl_set_gist(__isl_take isl_set
*set
,
1926 __isl_take isl_set
*context
)
1928 return (struct isl_set
*)isl_map_gist((struct isl_map
*)set
,
1929 (struct isl_map
*)context
);
1932 /* Quick check to see if two basic maps are disjoint.
1933 * In particular, we reduce the equalities and inequalities of
1934 * one basic map in the context of the equalities of the other
1935 * basic map and check if we get a contradiction.
1937 int isl_basic_map_plain_is_disjoint(__isl_keep isl_basic_map
*bmap1
,
1938 __isl_keep isl_basic_map
*bmap2
)
1940 struct isl_vec
*v
= NULL
;
1945 if (!bmap1
|| !bmap2
)
1947 isl_assert(bmap1
->ctx
, isl_dim_equal(bmap1
->dim
, bmap2
->dim
),
1949 if (bmap1
->n_div
|| bmap2
->n_div
)
1951 if (!bmap1
->n_eq
&& !bmap2
->n_eq
)
1954 total
= isl_dim_total(bmap1
->dim
);
1957 v
= isl_vec_alloc(bmap1
->ctx
, 1 + total
);
1960 elim
= isl_alloc_array(bmap1
->ctx
, int, total
);
1963 compute_elimination_index(bmap1
, elim
);
1964 for (i
= 0; i
< bmap2
->n_eq
; ++i
) {
1966 reduced
= reduced_using_equalities(v
->block
.data
, bmap2
->eq
[i
],
1968 if (reduced
&& !isl_int_is_zero(v
->block
.data
[0]) &&
1969 isl_seq_first_non_zero(v
->block
.data
+ 1, total
) == -1)
1972 for (i
= 0; i
< bmap2
->n_ineq
; ++i
) {
1974 reduced
= reduced_using_equalities(v
->block
.data
,
1975 bmap2
->ineq
[i
], bmap1
, elim
);
1976 if (reduced
&& isl_int_is_neg(v
->block
.data
[0]) &&
1977 isl_seq_first_non_zero(v
->block
.data
+ 1, total
) == -1)
1980 compute_elimination_index(bmap2
, elim
);
1981 for (i
= 0; i
< bmap1
->n_ineq
; ++i
) {
1983 reduced
= reduced_using_equalities(v
->block
.data
,
1984 bmap1
->ineq
[i
], bmap2
, elim
);
1985 if (reduced
&& isl_int_is_neg(v
->block
.data
[0]) &&
1986 isl_seq_first_non_zero(v
->block
.data
+ 1, total
) == -1)
2002 int isl_basic_set_plain_is_disjoint(__isl_keep isl_basic_set
*bset1
,
2003 __isl_keep isl_basic_set
*bset2
)
2005 return isl_basic_map_plain_is_disjoint((struct isl_basic_map
*)bset1
,
2006 (struct isl_basic_map
*)bset2
);
2009 int isl_map_plain_is_disjoint(__isl_keep isl_map
*map1
,
2010 __isl_keep isl_map
*map2
)
2017 if (isl_map_plain_is_equal(map1
, map2
))
2020 for (i
= 0; i
< map1
->n
; ++i
) {
2021 for (j
= 0; j
< map2
->n
; ++j
) {
2022 int d
= isl_basic_map_plain_is_disjoint(map1
->p
[i
],
2031 int isl_set_plain_is_disjoint(__isl_keep isl_set
*set1
,
2032 __isl_keep isl_set
*set2
)
2034 return isl_map_plain_is_disjoint((struct isl_map
*)set1
,
2035 (struct isl_map
*)set2
);
2038 int isl_set_fast_is_disjoint(__isl_keep isl_set
*set1
, __isl_keep isl_set
*set2
)
2040 return isl_set_plain_is_disjoint(set1
, set2
);
2043 /* Check if we can combine a given div with lower bound l and upper
2044 * bound u with some other div and if so return that other div.
2045 * Otherwise return -1.
2047 * We first check that
2048 * - the bounds are opposites of each other (except for the constant
2050 * - the bounds do not reference any other div
2051 * - no div is defined in terms of this div
2053 * Let m be the size of the range allowed on the div by the bounds.
2054 * That is, the bounds are of the form
2056 * e <= a <= e + m - 1
2058 * with e some expression in the other variables.
2059 * We look for another div b such that no third div is defined in terms
2060 * of this second div b and such that in any constraint that contains
2061 * a (except for the given lower and upper bound), also contains b
2062 * with a coefficient that is m times that of b.
2063 * That is, all constraints (execpt for the lower and upper bound)
2066 * e + f (a + m b) >= 0
2068 * If so, we return b so that "a + m b" can be replaced by
2069 * a single div "c = a + m b".
2071 static int div_find_coalesce(struct isl_basic_map
*bmap
, int *pairs
,
2072 unsigned div
, unsigned l
, unsigned u
)
2078 if (bmap
->n_div
<= 1)
2080 dim
= isl_dim_total(bmap
->dim
);
2081 if (isl_seq_first_non_zero(bmap
->ineq
[l
] + 1 + dim
, div
) != -1)
2083 if (isl_seq_first_non_zero(bmap
->ineq
[l
] + 1 + dim
+ div
+ 1,
2084 bmap
->n_div
- div
- 1) != -1)
2086 if (!isl_seq_is_neg(bmap
->ineq
[l
] + 1, bmap
->ineq
[u
] + 1,
2090 for (i
= 0; i
< bmap
->n_div
; ++i
) {
2091 if (isl_int_is_zero(bmap
->div
[i
][0]))
2093 if (!isl_int_is_zero(bmap
->div
[i
][1 + 1 + dim
+ div
]))
2097 isl_int_add(bmap
->ineq
[l
][0], bmap
->ineq
[l
][0], bmap
->ineq
[u
][0]);
2098 if (isl_int_is_neg(bmap
->ineq
[l
][0])) {
2099 isl_int_sub(bmap
->ineq
[l
][0],
2100 bmap
->ineq
[l
][0], bmap
->ineq
[u
][0]);
2101 bmap
= isl_basic_map_copy(bmap
);
2102 bmap
= isl_basic_map_set_to_empty(bmap
);
2103 isl_basic_map_free(bmap
);
2106 isl_int_add_ui(bmap
->ineq
[l
][0], bmap
->ineq
[l
][0], 1);
2107 for (i
= 0; i
< bmap
->n_div
; ++i
) {
2112 for (j
= 0; j
< bmap
->n_div
; ++j
) {
2113 if (isl_int_is_zero(bmap
->div
[j
][0]))
2115 if (!isl_int_is_zero(bmap
->div
[j
][1 + 1 + dim
+ i
]))
2118 if (j
< bmap
->n_div
)
2120 for (j
= 0; j
< bmap
->n_ineq
; ++j
) {
2122 if (j
== l
|| j
== u
)
2124 if (isl_int_is_zero(bmap
->ineq
[j
][1 + dim
+ div
]))
2126 if (isl_int_is_zero(bmap
->ineq
[j
][1 + dim
+ i
]))
2128 isl_int_mul(bmap
->ineq
[j
][1 + dim
+ div
],
2129 bmap
->ineq
[j
][1 + dim
+ div
],
2131 valid
= isl_int_eq(bmap
->ineq
[j
][1 + dim
+ div
],
2132 bmap
->ineq
[j
][1 + dim
+ i
]);
2133 isl_int_divexact(bmap
->ineq
[j
][1 + dim
+ div
],
2134 bmap
->ineq
[j
][1 + dim
+ div
],
2139 if (j
< bmap
->n_ineq
)
2144 isl_int_sub_ui(bmap
->ineq
[l
][0], bmap
->ineq
[l
][0], 1);
2145 isl_int_sub(bmap
->ineq
[l
][0], bmap
->ineq
[l
][0], bmap
->ineq
[u
][0]);
2149 /* Given a lower and an upper bound on div i, construct an inequality
2150 * that when nonnegative ensures that this pair of bounds always allows
2151 * for an integer value of the given div.
2152 * The lower bound is inequality l, while the upper bound is inequality u.
2153 * The constructed inequality is stored in ineq.
2154 * g, fl, fu are temporary scalars.
2156 * Let the upper bound be
2160 * and the lower bound
2164 * Let n_u = f_u g and n_l = f_l g, with g = gcd(n_u, n_l).
2167 * - f_u e_l <= f_u f_l g a <= f_l e_u
2169 * Since all variables are integer valued, this is equivalent to
2171 * - f_u e_l - (f_u - 1) <= f_u f_l g a <= f_l e_u + (f_l - 1)
2173 * If this interval is at least f_u f_l g, then it contains at least
2174 * one integer value for a.
2175 * That is, the test constraint is
2177 * f_l e_u + f_u e_l + f_l - 1 + f_u - 1 + 1 >= f_u f_l g
2179 static void construct_test_ineq(struct isl_basic_map
*bmap
, int i
,
2180 int l
, int u
, isl_int
*ineq
, isl_int g
, isl_int fl
, isl_int fu
)
2183 dim
= isl_dim_total(bmap
->dim
);
2185 isl_int_gcd(g
, bmap
->ineq
[l
][1 + dim
+ i
], bmap
->ineq
[u
][1 + dim
+ i
]);
2186 isl_int_divexact(fl
, bmap
->ineq
[l
][1 + dim
+ i
], g
);
2187 isl_int_divexact(fu
, bmap
->ineq
[u
][1 + dim
+ i
], g
);
2188 isl_int_neg(fu
, fu
);
2189 isl_seq_combine(ineq
, fl
, bmap
->ineq
[u
], fu
, bmap
->ineq
[l
],
2190 1 + dim
+ bmap
->n_div
);
2191 isl_int_add(ineq
[0], ineq
[0], fl
);
2192 isl_int_add(ineq
[0], ineq
[0], fu
);
2193 isl_int_sub_ui(ineq
[0], ineq
[0], 1);
2194 isl_int_mul(g
, g
, fl
);
2195 isl_int_mul(g
, g
, fu
);
2196 isl_int_sub(ineq
[0], ineq
[0], g
);
2199 /* Remove more kinds of divs that are not strictly needed.
2200 * In particular, if all pairs of lower and upper bounds on a div
2201 * are such that they allow at least one integer value of the div,
2202 * the we can eliminate the div using Fourier-Motzkin without
2203 * introducing any spurious solutions.
2205 static struct isl_basic_map
*drop_more_redundant_divs(
2206 struct isl_basic_map
*bmap
, int *pairs
, int n
)
2208 struct isl_tab
*tab
= NULL
;
2209 struct isl_vec
*vec
= NULL
;
2221 dim
= isl_dim_total(bmap
->dim
);
2222 vec
= isl_vec_alloc(bmap
->ctx
, 1 + dim
+ bmap
->n_div
);
2226 tab
= isl_tab_from_basic_map(bmap
);
2231 enum isl_lp_result res
;
2233 for (i
= 0; i
< bmap
->n_div
; ++i
) {
2236 if (best
>= 0 && pairs
[best
] <= pairs
[i
])
2242 for (l
= 0; l
< bmap
->n_ineq
; ++l
) {
2243 if (!isl_int_is_pos(bmap
->ineq
[l
][1 + dim
+ i
]))
2245 for (u
= 0; u
< bmap
->n_ineq
; ++u
) {
2246 if (!isl_int_is_neg(bmap
->ineq
[u
][1 + dim
+ i
]))
2248 construct_test_ineq(bmap
, i
, l
, u
,
2249 vec
->el
, g
, fl
, fu
);
2250 res
= isl_tab_min(tab
, vec
->el
,
2251 bmap
->ctx
->one
, &g
, NULL
, 0);
2252 if (res
== isl_lp_error
)
2254 if (res
== isl_lp_empty
) {
2255 bmap
= isl_basic_map_set_to_empty(bmap
);
2258 if (res
!= isl_lp_ok
|| isl_int_is_neg(g
))
2261 if (u
< bmap
->n_ineq
)
2264 if (l
== bmap
->n_ineq
) {
2284 bmap
= isl_basic_map_remove_dims(bmap
, isl_dim_div
, remove
, 1);
2285 return isl_basic_map_drop_redundant_divs(bmap
);
2288 isl_basic_map_free(bmap
);
2297 /* Given a pair of divs div1 and div2 such that, expect for the lower bound l
2298 * and the upper bound u, div1 always occurs together with div2 in the form
2299 * (div1 + m div2), where m is the constant range on the variable div1
2300 * allowed by l and u, replace the pair div1 and div2 by a single
2301 * div that is equal to div1 + m div2.
2303 * The new div will appear in the location that contains div2.
2304 * We need to modify all constraints that contain
2305 * div2 = (div - div1) / m
2306 * (If a constraint does not contain div2, it will also not contain div1.)
2307 * If the constraint also contains div1, then we know they appear
2308 * as f (div1 + m div2) and we can simply replace (div1 + m div2) by div,
2309 * i.e., the coefficient of div is f.
2311 * Otherwise, we first need to introduce div1 into the constraint.
2320 * A lower bound on div2
2324 * can be replaced by
2326 * (n * (m div 2 + div1) + m t + n f)/g >= 0
2328 * with g = gcd(m,n).
2333 * can be replaced by
2335 * (-n * (m div2 + div1) + m t + n f')/g >= 0
2337 * These constraint are those that we would obtain from eliminating
2338 * div1 using Fourier-Motzkin.
2340 * After all constraints have been modified, we drop the lower and upper
2341 * bound and then drop div1.
2343 static struct isl_basic_map
*coalesce_divs(struct isl_basic_map
*bmap
,
2344 unsigned div1
, unsigned div2
, unsigned l
, unsigned u
)
2349 unsigned dim
, total
;
2352 dim
= isl_dim_total(bmap
->dim
);
2353 total
= 1 + dim
+ bmap
->n_div
;
2358 isl_int_add(m
, bmap
->ineq
[l
][0], bmap
->ineq
[u
][0]);
2359 isl_int_add_ui(m
, m
, 1);
2361 for (i
= 0; i
< bmap
->n_ineq
; ++i
) {
2362 if (i
== l
|| i
== u
)
2364 if (isl_int_is_zero(bmap
->ineq
[i
][1 + dim
+ div2
]))
2366 if (isl_int_is_zero(bmap
->ineq
[i
][1 + dim
+ div1
])) {
2367 isl_int_gcd(b
, m
, bmap
->ineq
[i
][1 + dim
+ div2
]);
2368 isl_int_divexact(a
, m
, b
);
2369 isl_int_divexact(b
, bmap
->ineq
[i
][1 + dim
+ div2
], b
);
2370 if (isl_int_is_pos(b
)) {
2371 isl_seq_combine(bmap
->ineq
[i
], a
, bmap
->ineq
[i
],
2372 b
, bmap
->ineq
[l
], total
);
2375 isl_seq_combine(bmap
->ineq
[i
], a
, bmap
->ineq
[i
],
2376 b
, bmap
->ineq
[u
], total
);
2379 isl_int_set(bmap
->ineq
[i
][1 + dim
+ div2
],
2380 bmap
->ineq
[i
][1 + dim
+ div1
]);
2381 isl_int_set_si(bmap
->ineq
[i
][1 + dim
+ div1
], 0);
2388 isl_basic_map_drop_inequality(bmap
, l
);
2389 isl_basic_map_drop_inequality(bmap
, u
);
2391 isl_basic_map_drop_inequality(bmap
, u
);
2392 isl_basic_map_drop_inequality(bmap
, l
);
2394 bmap
= isl_basic_map_drop_div(bmap
, div1
);
2398 /* First check if we can coalesce any pair of divs and
2399 * then continue with dropping more redundant divs.
2401 * We loop over all pairs of lower and upper bounds on a div
2402 * with coefficient 1 and -1, respectively, check if there
2403 * is any other div "c" with which we can coalesce the div
2404 * and if so, perform the coalescing.
2406 static struct isl_basic_map
*coalesce_or_drop_more_redundant_divs(
2407 struct isl_basic_map
*bmap
, int *pairs
, int n
)
2412 dim
= isl_dim_total(bmap
->dim
);
2414 for (i
= 0; i
< bmap
->n_div
; ++i
) {
2417 for (l
= 0; l
< bmap
->n_ineq
; ++l
) {
2418 if (!isl_int_is_one(bmap
->ineq
[l
][1 + dim
+ i
]))
2420 for (u
= 0; u
< bmap
->n_ineq
; ++u
) {
2423 if (!isl_int_is_negone(bmap
->ineq
[u
][1+dim
+i
]))
2425 c
= div_find_coalesce(bmap
, pairs
, i
, l
, u
);
2429 bmap
= coalesce_divs(bmap
, i
, c
, l
, u
);
2430 return isl_basic_map_drop_redundant_divs(bmap
);
2435 if (ISL_F_ISSET(bmap
, ISL_BASIC_MAP_EMPTY
))
2438 return drop_more_redundant_divs(bmap
, pairs
, n
);
2441 /* Remove divs that are not strictly needed.
2442 * In particular, if a div only occurs positively (or negatively)
2443 * in constraints, then it can simply be dropped.
2444 * Also, if a div occurs only occurs in two constraints and if moreover
2445 * those two constraints are opposite to each other, except for the constant
2446 * term and if the sum of the constant terms is such that for any value
2447 * of the other values, there is always at least one integer value of the
2448 * div, i.e., if one plus this sum is greater than or equal to
2449 * the (absolute value) of the coefficent of the div in the constraints,
2450 * then we can also simply drop the div.
2452 * If any divs are left after these simple checks then we move on
2453 * to more complicated cases in drop_more_redundant_divs.
2455 struct isl_basic_map
*isl_basic_map_drop_redundant_divs(
2456 struct isl_basic_map
*bmap
)
2466 off
= isl_dim_total(bmap
->dim
);
2467 pairs
= isl_calloc_array(bmap
->ctx
, int, bmap
->n_div
);
2471 for (i
= 0; i
< bmap
->n_div
; ++i
) {
2473 int last_pos
, last_neg
;
2477 defined
= !isl_int_is_zero(bmap
->div
[i
][0]);
2478 for (j
= 0; j
< bmap
->n_eq
; ++j
)
2479 if (!isl_int_is_zero(bmap
->eq
[j
][1 + off
+ i
]))
2485 for (j
= 0; j
< bmap
->n_ineq
; ++j
) {
2486 if (isl_int_is_pos(bmap
->ineq
[j
][1 + off
+ i
])) {
2490 if (isl_int_is_neg(bmap
->ineq
[j
][1 + off
+ i
])) {
2495 pairs
[i
] = pos
* neg
;
2496 if (pairs
[i
] == 0) {
2497 for (j
= bmap
->n_ineq
- 1; j
>= 0; --j
)
2498 if (!isl_int_is_zero(bmap
->ineq
[j
][1+off
+i
]))
2499 isl_basic_map_drop_inequality(bmap
, j
);
2500 bmap
= isl_basic_map_drop_div(bmap
, i
);
2502 return isl_basic_map_drop_redundant_divs(bmap
);
2506 if (!isl_seq_is_neg(bmap
->ineq
[last_pos
] + 1,
2507 bmap
->ineq
[last_neg
] + 1,
2511 isl_int_add(bmap
->ineq
[last_pos
][0],
2512 bmap
->ineq
[last_pos
][0], bmap
->ineq
[last_neg
][0]);
2513 isl_int_add_ui(bmap
->ineq
[last_pos
][0],
2514 bmap
->ineq
[last_pos
][0], 1);
2515 redundant
= isl_int_ge(bmap
->ineq
[last_pos
][0],
2516 bmap
->ineq
[last_pos
][1+off
+i
]);
2517 isl_int_sub_ui(bmap
->ineq
[last_pos
][0],
2518 bmap
->ineq
[last_pos
][0], 1);
2519 isl_int_sub(bmap
->ineq
[last_pos
][0],
2520 bmap
->ineq
[last_pos
][0], bmap
->ineq
[last_neg
][0]);
2523 !ok_to_set_div_from_bound(bmap
, i
, last_pos
)) {
2528 bmap
= set_div_from_lower_bound(bmap
, i
, last_pos
);
2529 bmap
= isl_basic_map_simplify(bmap
);
2531 return isl_basic_map_drop_redundant_divs(bmap
);
2533 if (last_pos
> last_neg
) {
2534 isl_basic_map_drop_inequality(bmap
, last_pos
);
2535 isl_basic_map_drop_inequality(bmap
, last_neg
);
2537 isl_basic_map_drop_inequality(bmap
, last_neg
);
2538 isl_basic_map_drop_inequality(bmap
, last_pos
);
2540 bmap
= isl_basic_map_drop_div(bmap
, i
);
2542 return isl_basic_map_drop_redundant_divs(bmap
);
2546 return coalesce_or_drop_more_redundant_divs(bmap
, pairs
, n
);
2552 isl_basic_map_free(bmap
);
2556 struct isl_basic_set
*isl_basic_set_drop_redundant_divs(
2557 struct isl_basic_set
*bset
)
2559 return (struct isl_basic_set
*)
2560 isl_basic_map_drop_redundant_divs((struct isl_basic_map
*)bset
);
2563 struct isl_map
*isl_map_drop_redundant_divs(struct isl_map
*map
)
2569 for (i
= 0; i
< map
->n
; ++i
) {
2570 map
->p
[i
] = isl_basic_map_drop_redundant_divs(map
->p
[i
]);
2574 ISL_F_CLR(map
, ISL_MAP_NORMALIZED
);
2581 struct isl_set
*isl_set_drop_redundant_divs(struct isl_set
*set
)
2583 return (struct isl_set
*)
2584 isl_map_drop_redundant_divs((struct isl_map
*)set
);