2 * Copyright 2008-2009 Katholieke Universiteit Leuven
3 * Copyright 2012-2013 Ecole Normale Superieure
4 * Copyright 2014-2015 INRIA Rocquencourt
5 * Copyright 2016 Sven Verdoolaege
7 * Use of this software is governed by the MIT license
9 * Written by Sven Verdoolaege, K.U.Leuven, Departement
10 * Computerwetenschappen, Celestijnenlaan 200A, B-3001 Leuven, Belgium
11 * and Ecole Normale Superieure, 45 rue d’Ulm, 75230 Paris, France
12 * and Inria Paris - Rocquencourt, Domaine de Voluceau - Rocquencourt,
13 * B.P. 105 - 78153 Le Chesnay, France
16 #include <isl_ctx_private.h>
17 #include <isl_map_private.h>
18 #include "isl_equalities.h"
22 #include <isl_space_private.h>
23 #include <isl_mat_private.h>
24 #include <isl_vec_private.h>
26 #include <bset_to_bmap.c>
27 #include <bset_from_bmap.c>
28 #include <set_to_map.c>
29 #include <set_from_map.c>
31 static void swap_equality(struct isl_basic_map
*bmap
, int a
, int b
)
33 isl_int
*t
= bmap
->eq
[a
];
34 bmap
->eq
[a
] = bmap
->eq
[b
];
38 static void swap_inequality(struct isl_basic_map
*bmap
, int a
, int b
)
41 isl_int
*t
= bmap
->ineq
[a
];
42 bmap
->ineq
[a
] = bmap
->ineq
[b
];
47 __isl_give isl_basic_map
*isl_basic_map_normalize_constraints(
48 __isl_take isl_basic_map
*bmap
)
52 isl_size total
= isl_basic_map_dim(bmap
, isl_dim_all
);
55 return isl_basic_map_free(bmap
);
58 for (i
= bmap
->n_eq
- 1; i
>= 0; --i
) {
59 isl_seq_gcd(bmap
->eq
[i
]+1, total
, &gcd
);
60 if (isl_int_is_zero(gcd
)) {
61 if (!isl_int_is_zero(bmap
->eq
[i
][0])) {
62 bmap
= isl_basic_map_set_to_empty(bmap
);
65 isl_basic_map_drop_equality(bmap
, i
);
68 if (ISL_F_ISSET(bmap
, ISL_BASIC_MAP_RATIONAL
))
69 isl_int_gcd(gcd
, gcd
, bmap
->eq
[i
][0]);
70 if (isl_int_is_one(gcd
))
72 if (!isl_int_is_divisible_by(bmap
->eq
[i
][0], gcd
)) {
73 bmap
= isl_basic_map_set_to_empty(bmap
);
76 isl_seq_scale_down(bmap
->eq
[i
], bmap
->eq
[i
], gcd
, 1+total
);
79 for (i
= bmap
->n_ineq
- 1; i
>= 0; --i
) {
80 isl_seq_gcd(bmap
->ineq
[i
]+1, total
, &gcd
);
81 if (isl_int_is_zero(gcd
)) {
82 if (isl_int_is_neg(bmap
->ineq
[i
][0])) {
83 bmap
= isl_basic_map_set_to_empty(bmap
);
86 isl_basic_map_drop_inequality(bmap
, i
);
89 if (ISL_F_ISSET(bmap
, ISL_BASIC_MAP_RATIONAL
))
90 isl_int_gcd(gcd
, gcd
, bmap
->ineq
[i
][0]);
91 if (isl_int_is_one(gcd
))
93 isl_int_fdiv_q(bmap
->ineq
[i
][0], bmap
->ineq
[i
][0], gcd
);
94 isl_seq_scale_down(bmap
->ineq
[i
]+1, bmap
->ineq
[i
]+1, gcd
, total
);
101 __isl_give isl_basic_set
*isl_basic_set_normalize_constraints(
102 __isl_take isl_basic_set
*bset
)
104 isl_basic_map
*bmap
= bset_to_bmap(bset
);
105 return bset_from_bmap(isl_basic_map_normalize_constraints(bmap
));
108 /* Reduce the coefficient of the variable at position "pos"
109 * in integer division "div", such that it lies in the half-open
110 * interval (1/2,1/2], extracting any excess value from this integer division.
111 * "pos" is as determined by isl_basic_map_offset, i.e., pos == 0
112 * corresponds to the constant term.
114 * That is, the integer division is of the form
116 * floor((... + (c * d + r) * x_pos + ...)/d)
118 * with -d < 2 * r <= d.
121 * floor((... + r * x_pos + ...)/d) + c * x_pos
123 * If 2 * ((c * d + r) % d) <= d, then c = floor((c * d + r)/d).
124 * Otherwise, c = floor((c * d + r)/d) + 1.
126 * This is the same normalization that is performed by isl_aff_floor.
128 static __isl_give isl_basic_map
*reduce_coefficient_in_div(
129 __isl_take isl_basic_map
*bmap
, int div
, int pos
)
135 isl_int_fdiv_r(shift
, bmap
->div
[div
][1 + pos
], bmap
->div
[div
][0]);
136 isl_int_mul_ui(shift
, shift
, 2);
137 add_one
= isl_int_gt(shift
, bmap
->div
[div
][0]);
138 isl_int_fdiv_q(shift
, bmap
->div
[div
][1 + pos
], bmap
->div
[div
][0]);
140 isl_int_add_ui(shift
, shift
, 1);
141 isl_int_neg(shift
, shift
);
142 bmap
= isl_basic_map_shift_div(bmap
, div
, pos
, shift
);
143 isl_int_clear(shift
);
148 /* Does the coefficient of the variable at position "pos"
149 * in integer division "div" need to be reduced?
150 * That is, does it lie outside the half-open interval (1/2,1/2]?
151 * The coefficient c/d lies outside this interval if abs(2 * c) >= d and
154 static isl_bool
needs_reduction(__isl_keep isl_basic_map
*bmap
, int div
,
159 if (isl_int_is_zero(bmap
->div
[div
][1 + pos
]))
160 return isl_bool_false
;
162 isl_int_mul_ui(bmap
->div
[div
][1 + pos
], bmap
->div
[div
][1 + pos
], 2);
163 r
= isl_int_abs_ge(bmap
->div
[div
][1 + pos
], bmap
->div
[div
][0]) &&
164 !isl_int_eq(bmap
->div
[div
][1 + pos
], bmap
->div
[div
][0]);
165 isl_int_divexact_ui(bmap
->div
[div
][1 + pos
],
166 bmap
->div
[div
][1 + pos
], 2);
171 /* Reduce the coefficients (including the constant term) of
172 * integer division "div", if needed.
173 * In particular, make sure all coefficients lie in
174 * the half-open interval (1/2,1/2].
176 static __isl_give isl_basic_map
*reduce_div_coefficients_of_div(
177 __isl_take isl_basic_map
*bmap
, int div
)
182 total
= isl_basic_map_dim(bmap
, isl_dim_all
);
184 return isl_basic_map_free(bmap
);
185 for (i
= 0; i
< 1 + total
; ++i
) {
188 reduce
= needs_reduction(bmap
, div
, i
);
190 return isl_basic_map_free(bmap
);
193 bmap
= reduce_coefficient_in_div(bmap
, div
, i
);
201 /* Reduce the coefficients (including the constant term) of
202 * the known integer divisions, if needed
203 * In particular, make sure all coefficients lie in
204 * the half-open interval (1/2,1/2].
206 static __isl_give isl_basic_map
*reduce_div_coefficients(
207 __isl_take isl_basic_map
*bmap
)
213 if (bmap
->n_div
== 0)
216 for (i
= 0; i
< bmap
->n_div
; ++i
) {
217 if (isl_int_is_zero(bmap
->div
[i
][0]))
219 bmap
= reduce_div_coefficients_of_div(bmap
, i
);
227 /* Remove any common factor in numerator and denominator of the div expression,
228 * not taking into account the constant term.
229 * That is, if the div is of the form
231 * floor((a + m f(x))/(m d))
235 * floor((floor(a/m) + f(x))/d)
237 * The difference {a/m}/d in the argument satisfies 0 <= {a/m}/d < 1/d
238 * and can therefore not influence the result of the floor.
240 static __isl_give isl_basic_map
*normalize_div_expression(
241 __isl_take isl_basic_map
*bmap
, int div
)
243 isl_size total
= isl_basic_map_dim(bmap
, isl_dim_all
);
244 isl_ctx
*ctx
= bmap
->ctx
;
247 return isl_basic_map_free(bmap
);
248 if (isl_int_is_zero(bmap
->div
[div
][0]))
250 isl_seq_gcd(bmap
->div
[div
] + 2, total
, &ctx
->normalize_gcd
);
251 isl_int_gcd(ctx
->normalize_gcd
, ctx
->normalize_gcd
, bmap
->div
[div
][0]);
252 if (isl_int_is_one(ctx
->normalize_gcd
))
254 isl_int_fdiv_q(bmap
->div
[div
][1], bmap
->div
[div
][1],
256 isl_int_divexact(bmap
->div
[div
][0], bmap
->div
[div
][0],
258 isl_seq_scale_down(bmap
->div
[div
] + 2, bmap
->div
[div
] + 2,
259 ctx
->normalize_gcd
, total
);
264 /* Remove any common factor in numerator and denominator of a div expression,
265 * not taking into account the constant term.
266 * That is, look for any div of the form
268 * floor((a + m f(x))/(m d))
272 * floor((floor(a/m) + f(x))/d)
274 * The difference {a/m}/d in the argument satisfies 0 <= {a/m}/d < 1/d
275 * and can therefore not influence the result of the floor.
277 static __isl_give isl_basic_map
*normalize_div_expressions(
278 __isl_take isl_basic_map
*bmap
)
284 if (bmap
->n_div
== 0)
287 for (i
= 0; i
< bmap
->n_div
; ++i
)
288 bmap
= normalize_div_expression(bmap
, i
);
293 /* Assumes divs have been ordered if keep_divs is set.
295 static __isl_give isl_basic_map
*eliminate_var_using_equality(
296 __isl_take isl_basic_map
*bmap
,
297 unsigned pos
, isl_int
*eq
, int keep_divs
, int *progress
)
304 total
= isl_basic_map_dim(bmap
, isl_dim_all
);
305 v_div
= isl_basic_map_var_offset(bmap
, isl_dim_div
);
306 if (total
< 0 || v_div
< 0)
307 return isl_basic_map_free(bmap
);
308 last_div
= isl_seq_last_non_zero(eq
+ 1 + v_div
, bmap
->n_div
);
309 for (k
= 0; k
< bmap
->n_eq
; ++k
) {
310 if (bmap
->eq
[k
] == eq
)
312 if (isl_int_is_zero(bmap
->eq
[k
][1+pos
]))
316 isl_seq_elim(bmap
->eq
[k
], eq
, 1+pos
, 1+total
, NULL
);
317 isl_seq_normalize(bmap
->ctx
, bmap
->eq
[k
], 1 + total
);
320 for (k
= 0; k
< bmap
->n_ineq
; ++k
) {
321 if (isl_int_is_zero(bmap
->ineq
[k
][1+pos
]))
325 isl_seq_elim(bmap
->ineq
[k
], eq
, 1+pos
, 1+total
, NULL
);
326 isl_seq_normalize(bmap
->ctx
, bmap
->ineq
[k
], 1 + total
);
327 ISL_F_CLR(bmap
, ISL_BASIC_MAP_NO_REDUNDANT
);
328 ISL_F_CLR(bmap
, ISL_BASIC_MAP_SORTED
);
331 for (k
= 0; k
< bmap
->n_div
; ++k
) {
332 if (isl_int_is_zero(bmap
->div
[k
][0]))
334 if (isl_int_is_zero(bmap
->div
[k
][1+1+pos
]))
338 /* We need to be careful about circular definitions,
339 * so for now we just remove the definition of div k
340 * if the equality contains any divs.
341 * If keep_divs is set, then the divs have been ordered
342 * and we can keep the definition as long as the result
345 if (last_div
== -1 || (keep_divs
&& last_div
< k
)) {
346 isl_seq_elim(bmap
->div
[k
]+1, eq
,
347 1+pos
, 1+total
, &bmap
->div
[k
][0]);
348 bmap
= normalize_div_expression(bmap
, k
);
352 isl_seq_clr(bmap
->div
[k
], 1 + total
);
358 /* Assumes divs have been ordered if keep_divs is set.
360 static __isl_give isl_basic_map
*eliminate_div(__isl_take isl_basic_map
*bmap
,
361 isl_int
*eq
, unsigned div
, int keep_divs
)
366 v_div
= isl_basic_map_var_offset(bmap
, isl_dim_div
);
368 return isl_basic_map_free(bmap
);
370 bmap
= eliminate_var_using_equality(bmap
, pos
, eq
, keep_divs
, NULL
);
372 bmap
= isl_basic_map_drop_div(bmap
, div
);
377 /* Check if elimination of div "div" using equality "eq" would not
378 * result in a div depending on a later div.
380 static isl_bool
ok_to_eliminate_div(__isl_keep isl_basic_map
*bmap
, isl_int
*eq
,
388 v_div
= isl_basic_map_var_offset(bmap
, isl_dim_div
);
390 return isl_bool_error
;
393 last_div
= isl_seq_last_non_zero(eq
+ 1 + v_div
, bmap
->n_div
);
394 if (last_div
< 0 || last_div
<= div
)
395 return isl_bool_true
;
397 for (k
= 0; k
<= last_div
; ++k
) {
398 if (isl_int_is_zero(bmap
->div
[k
][0]))
400 if (!isl_int_is_zero(bmap
->div
[k
][1 + 1 + pos
]))
401 return isl_bool_false
;
404 return isl_bool_true
;
407 /* Eliminate divs based on equalities
409 static __isl_give isl_basic_map
*eliminate_divs_eq(
410 __isl_take isl_basic_map
*bmap
, int *progress
)
417 bmap
= isl_basic_map_order_divs(bmap
);
422 off
= isl_basic_map_offset(bmap
, isl_dim_div
);
424 for (d
= bmap
->n_div
- 1; d
>= 0 ; --d
) {
425 for (i
= 0; i
< bmap
->n_eq
; ++i
) {
428 if (!isl_int_is_one(bmap
->eq
[i
][off
+ d
]) &&
429 !isl_int_is_negone(bmap
->eq
[i
][off
+ d
]))
431 ok
= ok_to_eliminate_div(bmap
, bmap
->eq
[i
], d
);
433 return isl_basic_map_free(bmap
);
438 bmap
= eliminate_div(bmap
, bmap
->eq
[i
], d
, 1);
439 if (isl_basic_map_drop_equality(bmap
, i
) < 0)
440 return isl_basic_map_free(bmap
);
445 return eliminate_divs_eq(bmap
, progress
);
449 /* Eliminate divs based on inequalities
451 static __isl_give isl_basic_map
*eliminate_divs_ineq(
452 __isl_take isl_basic_map
*bmap
, int *progress
)
463 off
= isl_basic_map_offset(bmap
, isl_dim_div
);
465 for (d
= bmap
->n_div
- 1; d
>= 0 ; --d
) {
466 for (i
= 0; i
< bmap
->n_eq
; ++i
)
467 if (!isl_int_is_zero(bmap
->eq
[i
][off
+ d
]))
471 for (i
= 0; i
< bmap
->n_ineq
; ++i
)
472 if (isl_int_abs_gt(bmap
->ineq
[i
][off
+ d
], ctx
->one
))
474 if (i
< bmap
->n_ineq
)
477 bmap
= isl_basic_map_eliminate_vars(bmap
, (off
-1)+d
, 1);
478 if (!bmap
|| ISL_F_ISSET(bmap
, ISL_BASIC_MAP_EMPTY
))
480 bmap
= isl_basic_map_drop_div(bmap
, d
);
487 /* Does the equality constraint at position "eq" in "bmap" involve
488 * any local variables in the range [first, first + n)
489 * that are not marked as having an explicit representation?
491 static isl_bool
bmap_eq_involves_unknown_divs(__isl_keep isl_basic_map
*bmap
,
492 int eq
, unsigned first
, unsigned n
)
498 return isl_bool_error
;
500 o_div
= isl_basic_map_offset(bmap
, isl_dim_div
);
501 for (i
= 0; i
< n
; ++i
) {
504 if (isl_int_is_zero(bmap
->eq
[eq
][o_div
+ first
+ i
]))
506 unknown
= isl_basic_map_div_is_marked_unknown(bmap
, first
+ i
);
508 return isl_bool_error
;
510 return isl_bool_true
;
513 return isl_bool_false
;
516 /* The last local variable involved in the equality constraint
517 * at position "eq" in "bmap" is the local variable at position "div".
518 * It can therefore be used to extract an explicit representation
520 * Do so unless the local variable already has an explicit representation or
521 * the explicit representation would involve any other local variables
522 * that in turn do not have an explicit representation.
523 * An equality constraint involving local variables without an explicit
524 * representation can be used in isl_basic_map_drop_redundant_divs
525 * to separate out an independent local variable. Introducing
526 * an explicit representation here would block this transformation,
527 * while the partial explicit representation in itself is not very useful.
528 * Set *progress if anything is changed.
530 * The equality constraint is of the form
534 * with n a positive number. The explicit representation derived from
539 static __isl_give isl_basic_map
*set_div_from_eq(__isl_take isl_basic_map
*bmap
,
540 int div
, int eq
, int *progress
)
549 if (!isl_int_is_zero(bmap
->div
[div
][0]))
552 involves
= bmap_eq_involves_unknown_divs(bmap
, eq
, 0, div
);
554 return isl_basic_map_free(bmap
);
558 total
= isl_basic_map_dim(bmap
, isl_dim_all
);
560 return isl_basic_map_free(bmap
);
561 o_div
= isl_basic_map_offset(bmap
, isl_dim_div
);
562 isl_seq_neg(bmap
->div
[div
] + 1, bmap
->eq
[eq
], 1 + total
);
563 isl_int_set_si(bmap
->div
[div
][1 + o_div
+ div
], 0);
564 isl_int_set(bmap
->div
[div
][0], bmap
->eq
[eq
][o_div
+ div
]);
571 /* Perform fangcheng (Gaussian elimination) on the equality
572 * constraints of "bmap".
573 * That is, put them into row-echelon form, starting from the last column
574 * backward and use them to eliminate the corresponding coefficients
575 * from all constraints.
577 * If "progress" is not NULL, then it gets set if the elimination
578 * result in any changes.
579 * The elimination process may result in some equality constraints
580 * getting interchanged or removed.
581 * If "swap" or "drop" are not NULL, then they get called when
582 * two equality constraints get interchanged or
583 * when a number of final equality constraints get removed.
584 * As a special case, if the input turns out to be empty,
585 * then drop gets called with the number of removed equality
586 * constraints set to the total number of equality constraints.
587 * If "swap" or "drop" are not NULL, then the local variables (if any)
588 * are assumed to be in a valid order.
590 __isl_give isl_basic_map
*isl_basic_map_gauss5(__isl_take isl_basic_map
*bmap
,
592 isl_stat (*swap
)(unsigned a
, unsigned b
, void *user
),
593 isl_stat (*drop
)(unsigned n
, void *user
), void *user
)
603 bmap
= isl_basic_map_order_divs(bmap
);
605 total
= isl_basic_map_dim(bmap
, isl_dim_all
);
607 return isl_basic_map_free(bmap
);
609 total_var
= total
- bmap
->n_div
;
611 last_var
= total
- 1;
612 for (done
= 0; done
< bmap
->n_eq
; ++done
) {
613 for (; last_var
>= 0; --last_var
) {
614 for (k
= done
; k
< bmap
->n_eq
; ++k
)
615 if (!isl_int_is_zero(bmap
->eq
[k
][1+last_var
]))
623 swap_equality(bmap
, k
, done
);
624 if (swap
&& swap(k
, done
, user
) < 0)
625 return isl_basic_map_free(bmap
);
627 if (isl_int_is_neg(bmap
->eq
[done
][1+last_var
]))
628 isl_seq_neg(bmap
->eq
[done
], bmap
->eq
[done
], 1+total
);
630 bmap
= eliminate_var_using_equality(bmap
, last_var
,
631 bmap
->eq
[done
], 1, progress
);
633 if (last_var
>= total_var
)
634 bmap
= set_div_from_eq(bmap
, last_var
- total_var
,
639 if (done
== bmap
->n_eq
)
641 for (k
= done
; k
< bmap
->n_eq
; ++k
) {
642 if (isl_int_is_zero(bmap
->eq
[k
][0]))
644 if (drop
&& drop(bmap
->n_eq
, user
) < 0)
645 return isl_basic_map_free(bmap
);
646 return isl_basic_map_set_to_empty(bmap
);
648 n_drop
= bmap
->n_eq
- done
;
649 bmap
= isl_basic_map_free_equality(bmap
, n_drop
);
650 if (drop
&& drop(n_drop
, user
) < 0)
651 return isl_basic_map_free(bmap
);
655 __isl_give isl_basic_map
*isl_basic_map_gauss(__isl_take isl_basic_map
*bmap
,
658 return isl_basic_map_gauss5(bmap
, progress
, NULL
, NULL
, NULL
);
661 __isl_give isl_basic_set
*isl_basic_set_gauss(
662 __isl_take isl_basic_set
*bset
, int *progress
)
664 return bset_from_bmap(isl_basic_map_gauss(bset_to_bmap(bset
),
669 static unsigned int round_up(unsigned int v
)
680 /* Hash table of inequalities in a basic map.
681 * "index" is an array of addresses of inequalities in the basic map, some
682 * of which are NULL. The inequalities are hashed on the coefficients
683 * except the constant term.
684 * "size" is the number of elements in the array and is always a power of two
685 * "bits" is the number of bits need to represent an index into the array.
686 * "total" is the total dimension of the basic map.
688 struct isl_constraint_index
{
695 /* Fill in the "ci" data structure for holding the inequalities of "bmap".
697 static isl_stat
create_constraint_index(struct isl_constraint_index
*ci
,
698 __isl_keep isl_basic_map
*bmap
)
704 return isl_stat_error
;
705 ci
->total
= isl_basic_map_dim(bmap
, isl_dim_all
);
707 return isl_stat_error
;
708 if (bmap
->n_ineq
== 0)
710 ci
->size
= round_up(4 * (bmap
->n_ineq
+ 1) / 3 - 1);
711 ci
->bits
= ffs(ci
->size
) - 1;
712 ctx
= isl_basic_map_get_ctx(bmap
);
713 ci
->index
= isl_calloc_array(ctx
, isl_int
**, ci
->size
);
715 return isl_stat_error
;
720 /* Free the memory allocated by create_constraint_index.
722 static void constraint_index_free(struct isl_constraint_index
*ci
)
727 /* Return the position in ci->index that contains the address of
728 * an inequality that is equal to *ineq up to the constant term,
729 * provided this address is not identical to "ineq".
730 * If there is no such inequality, then return the position where
731 * such an inequality should be inserted.
733 static int hash_index_ineq(struct isl_constraint_index
*ci
, isl_int
**ineq
)
736 uint32_t hash
= isl_seq_get_hash_bits((*ineq
) + 1, ci
->total
, ci
->bits
);
737 for (h
= hash
; ci
->index
[h
]; h
= (h
+1) % ci
->size
)
738 if (ineq
!= ci
->index
[h
] &&
739 isl_seq_eq((*ineq
) + 1, ci
->index
[h
][0]+1, ci
->total
))
744 /* Return the position in ci->index that contains the address of
745 * an inequality that is equal to the k'th inequality of "bmap"
746 * up to the constant term, provided it does not point to the very
748 * If there is no such inequality, then return the position where
749 * such an inequality should be inserted.
751 static int hash_index(struct isl_constraint_index
*ci
,
752 __isl_keep isl_basic_map
*bmap
, int k
)
754 return hash_index_ineq(ci
, &bmap
->ineq
[k
]);
757 static int set_hash_index(struct isl_constraint_index
*ci
,
758 __isl_keep isl_basic_set
*bset
, int k
)
760 return hash_index(ci
, bset
, k
);
763 /* Fill in the "ci" data structure with the inequalities of "bset".
765 static isl_stat
setup_constraint_index(struct isl_constraint_index
*ci
,
766 __isl_keep isl_basic_set
*bset
)
770 if (create_constraint_index(ci
, bset
) < 0)
771 return isl_stat_error
;
773 for (k
= 0; k
< bset
->n_ineq
; ++k
) {
774 h
= set_hash_index(ci
, bset
, k
);
775 ci
->index
[h
] = &bset
->ineq
[k
];
781 /* Is the inequality ineq (obviously) redundant with respect
782 * to the constraints in "ci"?
784 * Look for an inequality in "ci" with the same coefficients and then
785 * check if the contant term of "ineq" is greater than or equal
786 * to the constant term of that inequality. If so, "ineq" is clearly
789 * Note that hash_index_ineq ignores a stored constraint if it has
790 * the same address as the passed inequality. It is ok to pass
791 * the address of a local variable here since it will never be
792 * the same as the address of a constraint in "ci".
794 static isl_bool
constraint_index_is_redundant(struct isl_constraint_index
*ci
,
799 h
= hash_index_ineq(ci
, &ineq
);
801 return isl_bool_false
;
802 return isl_int_ge(ineq
[0], (*ci
->index
[h
])[0]);
805 /* If we can eliminate more than one div, then we need to make
806 * sure we do it from last div to first div, in order not to
807 * change the position of the other divs that still need to
810 static __isl_give isl_basic_map
*remove_duplicate_divs(
811 __isl_take isl_basic_map
*bmap
, int *progress
)
823 bmap
= isl_basic_map_order_divs(bmap
);
824 if (!bmap
|| bmap
->n_div
<= 1)
827 v_div
= isl_basic_map_var_offset(bmap
, isl_dim_div
);
829 return isl_basic_map_free(bmap
);
830 total
= v_div
+ bmap
->n_div
;
833 for (k
= bmap
->n_div
- 1; k
>= 0; --k
)
834 if (!isl_int_is_zero(bmap
->div
[k
][0]))
839 size
= round_up(4 * bmap
->n_div
/ 3 - 1);
842 elim_for
= isl_calloc_array(ctx
, int, bmap
->n_div
);
843 bits
= ffs(size
) - 1;
844 index
= isl_calloc_array(ctx
, int, size
);
845 if (!elim_for
|| !index
)
847 eq
= isl_blk_alloc(ctx
, 1+total
);
848 if (isl_blk_is_error(eq
))
851 isl_seq_clr(eq
.data
, 1+total
);
852 index
[isl_seq_get_hash_bits(bmap
->div
[k
], 2+total
, bits
)] = k
+ 1;
853 for (--k
; k
>= 0; --k
) {
856 if (isl_int_is_zero(bmap
->div
[k
][0]))
859 hash
= isl_seq_get_hash_bits(bmap
->div
[k
], 2+total
, bits
);
860 for (h
= hash
; index
[h
]; h
= (h
+1) % size
)
861 if (isl_seq_eq(bmap
->div
[k
],
862 bmap
->div
[index
[h
]-1], 2+total
))
871 for (l
= bmap
->n_div
- 1; l
>= 0; --l
) {
875 isl_int_set_si(eq
.data
[1 + v_div
+ k
], -1);
876 isl_int_set_si(eq
.data
[1 + v_div
+ l
], 1);
877 bmap
= eliminate_div(bmap
, eq
.data
, l
, 1);
880 isl_int_set_si(eq
.data
[1 + v_div
+ k
], 0);
881 isl_int_set_si(eq
.data
[1 + v_div
+ l
], 0);
884 isl_blk_free(ctx
, eq
);
891 static int n_pure_div_eq(struct isl_basic_map
*bmap
)
896 v_div
= isl_basic_map_var_offset(bmap
, isl_dim_div
);
899 for (i
= 0, j
= bmap
->n_div
-1; i
< bmap
->n_eq
; ++i
) {
900 while (j
>= 0 && isl_int_is_zero(bmap
->eq
[i
][1 + v_div
+ j
]))
904 if (isl_seq_first_non_zero(bmap
->eq
[i
] + 1 + v_div
, j
) != -1)
910 /* Normalize divs that appear in equalities.
912 * In particular, we assume that bmap contains some equalities
917 * and we want to replace the set of e_i by a minimal set and
918 * such that the new e_i have a canonical representation in terms
920 * If any of the equalities involves more than one divs, then
921 * we currently simply bail out.
923 * Let us first additionally assume that all equalities involve
924 * a div. The equalities then express modulo constraints on the
925 * remaining variables and we can use "parameter compression"
926 * to find a minimal set of constraints. The result is a transformation
928 * x = T(x') = x_0 + G x'
930 * with G a lower-triangular matrix with all elements below the diagonal
931 * non-negative and smaller than the diagonal element on the same row.
932 * We first normalize x_0 by making the same property hold in the affine
934 * The rows i of G with a 1 on the diagonal do not impose any modulo
935 * constraint and simply express x_i = x'_i.
936 * For each of the remaining rows i, we introduce a div and a corresponding
937 * equality. In particular
939 * g_ii e_j = x_i - g_i(x')
941 * where each x'_k is replaced either by x_k (if g_kk = 1) or the
942 * corresponding div (if g_kk != 1).
944 * If there are any equalities not involving any div, then we
945 * first apply a variable compression on the variables x:
947 * x = C x'' x'' = C_2 x
949 * and perform the above parameter compression on A C instead of on A.
950 * The resulting compression is then of the form
952 * x'' = T(x') = x_0 + G x'
954 * and in constructing the new divs and the corresponding equalities,
955 * we have to replace each x'', i.e., the x'_k with (g_kk = 1),
956 * by the corresponding row from C_2.
958 static __isl_give isl_basic_map
*normalize_divs(__isl_take isl_basic_map
*bmap
,
966 struct isl_mat
*T
= NULL
;
967 struct isl_mat
*C
= NULL
;
968 struct isl_mat
*C2
= NULL
;
976 if (bmap
->n_div
== 0)
982 if (ISL_F_ISSET(bmap
, ISL_BASIC_MAP_NORMALIZED_DIVS
))
985 v_div
= isl_basic_map_var_offset(bmap
, isl_dim_div
);
986 div_eq
= n_pure_div_eq(bmap
);
987 if (v_div
< 0 || div_eq
< 0)
988 return isl_basic_map_free(bmap
);
992 if (div_eq
< bmap
->n_eq
) {
993 B
= isl_mat_sub_alloc6(bmap
->ctx
, bmap
->eq
, div_eq
,
994 bmap
->n_eq
- div_eq
, 0, 1 + v_div
);
995 C
= isl_mat_variable_compression(B
, &C2
);
999 bmap
= isl_basic_map_set_to_empty(bmap
);
1006 d
= isl_vec_alloc(bmap
->ctx
, div_eq
);
1009 for (i
= 0, j
= bmap
->n_div
-1; i
< div_eq
; ++i
) {
1010 while (j
>= 0 && isl_int_is_zero(bmap
->eq
[i
][1 + v_div
+ j
]))
1012 isl_int_set(d
->block
.data
[i
], bmap
->eq
[i
][1 + v_div
+ j
]);
1014 B
= isl_mat_sub_alloc6(bmap
->ctx
, bmap
->eq
, 0, div_eq
, 0, 1 + v_div
);
1017 B
= isl_mat_product(B
, C
);
1021 T
= isl_mat_parameter_compression(B
, d
);
1024 if (T
->n_col
== 0) {
1025 bmap
= isl_basic_map_set_to_empty(bmap
);
1031 for (i
= 0; i
< T
->n_row
- 1; ++i
) {
1032 isl_int_fdiv_q(v
, T
->row
[1 + i
][0], T
->row
[1 + i
][1 + i
]);
1033 if (isl_int_is_zero(v
))
1035 isl_mat_col_submul(T
, 0, v
, 1 + i
);
1038 pos
= isl_alloc_array(bmap
->ctx
, int, T
->n_row
);
1041 /* We have to be careful because dropping equalities may reorder them */
1043 for (j
= bmap
->n_div
- 1; j
>= 0; --j
) {
1044 for (i
= 0; i
< bmap
->n_eq
; ++i
)
1045 if (!isl_int_is_zero(bmap
->eq
[i
][1 + v_div
+ j
]))
1047 if (i
< bmap
->n_eq
) {
1048 bmap
= isl_basic_map_drop_div(bmap
, j
);
1049 isl_basic_map_drop_equality(bmap
, i
);
1055 for (i
= 1; i
< T
->n_row
; ++i
) {
1056 if (isl_int_is_one(T
->row
[i
][i
]))
1061 if (needed
> dropped
) {
1062 bmap
= isl_basic_map_extend_space(bmap
, isl_space_copy(bmap
->dim
),
1067 for (i
= 1; i
< T
->n_row
; ++i
) {
1068 if (isl_int_is_one(T
->row
[i
][i
]))
1070 k
= isl_basic_map_alloc_div(bmap
);
1071 pos
[i
] = 1 + v_div
+ k
;
1072 isl_seq_clr(bmap
->div
[k
] + 1, 1 + v_div
+ bmap
->n_div
);
1073 isl_int_set(bmap
->div
[k
][0], T
->row
[i
][i
]);
1075 isl_seq_cpy(bmap
->div
[k
] + 1, C2
->row
[i
], 1 + v_div
);
1077 isl_int_set_si(bmap
->div
[k
][1 + i
], 1);
1078 for (j
= 0; j
< i
; ++j
) {
1079 if (isl_int_is_zero(T
->row
[i
][j
]))
1081 if (pos
[j
] < T
->n_row
&& C2
)
1082 isl_seq_submul(bmap
->div
[k
] + 1, T
->row
[i
][j
],
1083 C2
->row
[pos
[j
]], 1 + v_div
);
1085 isl_int_neg(bmap
->div
[k
][1 + pos
[j
]],
1088 j
= isl_basic_map_alloc_equality(bmap
);
1089 isl_seq_neg(bmap
->eq
[j
], bmap
->div
[k
]+1, 1+v_div
+bmap
->n_div
);
1090 isl_int_set(bmap
->eq
[j
][pos
[i
]], bmap
->div
[k
][0]);
1099 ISL_F_SET(bmap
, ISL_BASIC_MAP_NORMALIZED_DIVS
);
1110 static __isl_give isl_basic_map
*set_div_from_lower_bound(
1111 __isl_take isl_basic_map
*bmap
, int div
, int ineq
)
1113 unsigned total
= isl_basic_map_offset(bmap
, isl_dim_div
);
1115 isl_seq_neg(bmap
->div
[div
] + 1, bmap
->ineq
[ineq
], total
+ bmap
->n_div
);
1116 isl_int_set(bmap
->div
[div
][0], bmap
->ineq
[ineq
][total
+ div
]);
1117 isl_int_add(bmap
->div
[div
][1], bmap
->div
[div
][1], bmap
->div
[div
][0]);
1118 isl_int_sub_ui(bmap
->div
[div
][1], bmap
->div
[div
][1], 1);
1119 isl_int_set_si(bmap
->div
[div
][1 + total
+ div
], 0);
1124 /* Check whether it is ok to define a div based on an inequality.
1125 * To avoid the introduction of circular definitions of divs, we
1126 * do not allow such a definition if the resulting expression would refer to
1127 * any other undefined divs or if any known div is defined in
1128 * terms of the unknown div.
1130 static isl_bool
ok_to_set_div_from_bound(__isl_keep isl_basic_map
*bmap
,
1134 unsigned total
= isl_basic_map_offset(bmap
, isl_dim_div
);
1136 /* Not defined in terms of unknown divs */
1137 for (j
= 0; j
< bmap
->n_div
; ++j
) {
1140 if (isl_int_is_zero(bmap
->ineq
[ineq
][total
+ j
]))
1142 if (isl_int_is_zero(bmap
->div
[j
][0]))
1143 return isl_bool_false
;
1146 /* No other div defined in terms of this one => avoid loops */
1147 for (j
= 0; j
< bmap
->n_div
; ++j
) {
1150 if (isl_int_is_zero(bmap
->div
[j
][0]))
1152 if (!isl_int_is_zero(bmap
->div
[j
][1 + total
+ div
]))
1153 return isl_bool_false
;
1156 return isl_bool_true
;
1159 /* Would an expression for div "div" based on inequality "ineq" of "bmap"
1160 * be a better expression than the current one?
1162 * If we do not have any expression yet, then any expression would be better.
1163 * Otherwise we check if the last variable involved in the inequality
1164 * (disregarding the div that it would define) is in an earlier position
1165 * than the last variable involved in the current div expression.
1167 static isl_bool
better_div_constraint(__isl_keep isl_basic_map
*bmap
,
1170 unsigned total
= isl_basic_map_offset(bmap
, isl_dim_div
);
1174 if (isl_int_is_zero(bmap
->div
[div
][0]))
1175 return isl_bool_true
;
1177 if (isl_seq_last_non_zero(bmap
->ineq
[ineq
] + total
+ div
+ 1,
1178 bmap
->n_div
- (div
+ 1)) >= 0)
1179 return isl_bool_false
;
1181 last_ineq
= isl_seq_last_non_zero(bmap
->ineq
[ineq
], total
+ div
);
1182 last_div
= isl_seq_last_non_zero(bmap
->div
[div
] + 1,
1183 total
+ bmap
->n_div
);
1185 return last_ineq
< last_div
;
1188 /* Given two constraints "k" and "l" that are opposite to each other,
1189 * except for the constant term, check if we can use them
1190 * to obtain an expression for one of the hitherto unknown divs or
1191 * a "better" expression for a div for which we already have an expression.
1192 * "sum" is the sum of the constant terms of the constraints.
1193 * If this sum is strictly smaller than the coefficient of one
1194 * of the divs, then this pair can be used define the div.
1195 * To avoid the introduction of circular definitions of divs, we
1196 * do not use the pair if the resulting expression would refer to
1197 * any other undefined divs or if any known div is defined in
1198 * terms of the unknown div.
1200 static __isl_give isl_basic_map
*check_for_div_constraints(
1201 __isl_take isl_basic_map
*bmap
, int k
, int l
, isl_int sum
,
1205 unsigned total
= isl_basic_map_offset(bmap
, isl_dim_div
);
1207 for (i
= 0; i
< bmap
->n_div
; ++i
) {
1210 if (isl_int_is_zero(bmap
->ineq
[k
][total
+ i
]))
1212 if (isl_int_abs_ge(sum
, bmap
->ineq
[k
][total
+ i
]))
1214 set_div
= better_div_constraint(bmap
, i
, k
);
1215 if (set_div
>= 0 && set_div
)
1216 set_div
= ok_to_set_div_from_bound(bmap
, i
, k
);
1218 return isl_basic_map_free(bmap
);
1221 if (isl_int_is_pos(bmap
->ineq
[k
][total
+ i
]))
1222 bmap
= set_div_from_lower_bound(bmap
, i
, k
);
1224 bmap
= set_div_from_lower_bound(bmap
, i
, l
);
1232 __isl_give isl_basic_map
*isl_basic_map_remove_duplicate_constraints(
1233 __isl_take isl_basic_map
*bmap
, int *progress
, int detect_divs
)
1235 struct isl_constraint_index ci
;
1237 isl_size total
= isl_basic_map_dim(bmap
, isl_dim_all
);
1240 if (total
< 0 || bmap
->n_ineq
<= 1)
1243 if (create_constraint_index(&ci
, bmap
) < 0)
1246 h
= isl_seq_get_hash_bits(bmap
->ineq
[0] + 1, total
, ci
.bits
);
1247 ci
.index
[h
] = &bmap
->ineq
[0];
1248 for (k
= 1; k
< bmap
->n_ineq
; ++k
) {
1249 h
= hash_index(&ci
, bmap
, k
);
1251 ci
.index
[h
] = &bmap
->ineq
[k
];
1256 l
= ci
.index
[h
] - &bmap
->ineq
[0];
1257 if (isl_int_lt(bmap
->ineq
[k
][0], bmap
->ineq
[l
][0]))
1258 swap_inequality(bmap
, k
, l
);
1259 isl_basic_map_drop_inequality(bmap
, k
);
1263 for (k
= 0; k
< bmap
->n_ineq
-1; ++k
) {
1264 isl_seq_neg(bmap
->ineq
[k
]+1, bmap
->ineq
[k
]+1, total
);
1265 h
= hash_index(&ci
, bmap
, k
);
1266 isl_seq_neg(bmap
->ineq
[k
]+1, bmap
->ineq
[k
]+1, total
);
1269 l
= ci
.index
[h
] - &bmap
->ineq
[0];
1270 isl_int_add(sum
, bmap
->ineq
[k
][0], bmap
->ineq
[l
][0]);
1271 if (isl_int_is_pos(sum
)) {
1273 bmap
= check_for_div_constraints(bmap
, k
, l
,
1277 if (isl_int_is_zero(sum
)) {
1278 /* We need to break out of the loop after these
1279 * changes since the contents of the hash
1280 * will no longer be valid.
1281 * Plus, we probably we want to regauss first.
1285 isl_basic_map_drop_inequality(bmap
, l
);
1286 isl_basic_map_inequality_to_equality(bmap
, k
);
1288 bmap
= isl_basic_map_set_to_empty(bmap
);
1293 constraint_index_free(&ci
);
1297 /* Detect all pairs of inequalities that form an equality.
1299 * isl_basic_map_remove_duplicate_constraints detects at most one such pair.
1300 * Call it repeatedly while it is making progress.
1302 __isl_give isl_basic_map
*isl_basic_map_detect_inequality_pairs(
1303 __isl_take isl_basic_map
*bmap
, int *progress
)
1309 bmap
= isl_basic_map_remove_duplicate_constraints(bmap
,
1311 if (progress
&& duplicate
)
1313 } while (duplicate
);
1318 /* Eliminate knowns divs from constraints where they appear with
1319 * a (positive or negative) unit coefficient.
1323 * floor(e/m) + f >= 0
1331 * -floor(e/m) + f >= 0
1335 * -e + m f + m - 1 >= 0
1337 * The first conversion is valid because floor(e/m) >= -f is equivalent
1338 * to e/m >= -f because -f is an integral expression.
1339 * The second conversion follows from the fact that
1341 * -floor(e/m) = ceil(-e/m) = floor((-e + m - 1)/m)
1344 * Note that one of the div constraints may have been eliminated
1345 * due to being redundant with respect to the constraint that is
1346 * being modified by this function. The modified constraint may
1347 * no longer imply this div constraint, so we add it back to make
1348 * sure we do not lose any information.
1350 * We skip integral divs, i.e., those with denominator 1, as we would
1351 * risk eliminating the div from the div constraints. We do not need
1352 * to handle those divs here anyway since the div constraints will turn
1353 * out to form an equality and this equality can then be used to eliminate
1354 * the div from all constraints.
1356 static __isl_give isl_basic_map
*eliminate_unit_divs(
1357 __isl_take isl_basic_map
*bmap
, int *progress
)
1366 ctx
= isl_basic_map_get_ctx(bmap
);
1367 total
= isl_basic_map_offset(bmap
, isl_dim_div
);
1369 for (i
= 0; i
< bmap
->n_div
; ++i
) {
1370 if (isl_int_is_zero(bmap
->div
[i
][0]))
1372 if (isl_int_is_one(bmap
->div
[i
][0]))
1374 for (j
= 0; j
< bmap
->n_ineq
; ++j
) {
1377 if (!isl_int_is_one(bmap
->ineq
[j
][total
+ i
]) &&
1378 !isl_int_is_negone(bmap
->ineq
[j
][total
+ i
]))
1383 s
= isl_int_sgn(bmap
->ineq
[j
][total
+ i
]);
1384 isl_int_set_si(bmap
->ineq
[j
][total
+ i
], 0);
1386 isl_seq_combine(bmap
->ineq
[j
],
1387 ctx
->negone
, bmap
->div
[i
] + 1,
1388 bmap
->div
[i
][0], bmap
->ineq
[j
],
1389 total
+ bmap
->n_div
);
1391 isl_seq_combine(bmap
->ineq
[j
],
1392 ctx
->one
, bmap
->div
[i
] + 1,
1393 bmap
->div
[i
][0], bmap
->ineq
[j
],
1394 total
+ bmap
->n_div
);
1396 isl_int_add(bmap
->ineq
[j
][0],
1397 bmap
->ineq
[j
][0], bmap
->div
[i
][0]);
1398 isl_int_sub_ui(bmap
->ineq
[j
][0],
1399 bmap
->ineq
[j
][0], 1);
1402 bmap
= isl_basic_map_extend_constraints(bmap
, 0, 1);
1403 bmap
= isl_basic_map_add_div_constraint(bmap
, i
, s
);
1412 __isl_give isl_basic_map
*isl_basic_map_simplify(__isl_take isl_basic_map
*bmap
)
1421 empty
= isl_basic_map_plain_is_empty(bmap
);
1423 return isl_basic_map_free(bmap
);
1426 bmap
= isl_basic_map_normalize_constraints(bmap
);
1427 bmap
= reduce_div_coefficients(bmap
);
1428 bmap
= normalize_div_expressions(bmap
);
1429 bmap
= remove_duplicate_divs(bmap
, &progress
);
1430 bmap
= eliminate_unit_divs(bmap
, &progress
);
1431 bmap
= eliminate_divs_eq(bmap
, &progress
);
1432 bmap
= eliminate_divs_ineq(bmap
, &progress
);
1433 bmap
= isl_basic_map_gauss(bmap
, &progress
);
1434 /* requires equalities in normal form */
1435 bmap
= normalize_divs(bmap
, &progress
);
1436 bmap
= isl_basic_map_remove_duplicate_constraints(bmap
,
1438 if (bmap
&& progress
)
1439 ISL_F_CLR(bmap
, ISL_BASIC_MAP_REDUCED_COEFFICIENTS
);
1444 struct isl_basic_set
*isl_basic_set_simplify(struct isl_basic_set
*bset
)
1446 return bset_from_bmap(isl_basic_map_simplify(bset_to_bmap(bset
)));
1450 isl_bool
isl_basic_map_is_div_constraint(__isl_keep isl_basic_map
*bmap
,
1451 isl_int
*constraint
, unsigned div
)
1456 return isl_bool_error
;
1458 pos
= isl_basic_map_offset(bmap
, isl_dim_div
) + div
;
1460 if (isl_int_eq(constraint
[pos
], bmap
->div
[div
][0])) {
1462 isl_int_sub(bmap
->div
[div
][1],
1463 bmap
->div
[div
][1], bmap
->div
[div
][0]);
1464 isl_int_add_ui(bmap
->div
[div
][1], bmap
->div
[div
][1], 1);
1465 neg
= isl_seq_is_neg(constraint
, bmap
->div
[div
]+1, pos
);
1466 isl_int_sub_ui(bmap
->div
[div
][1], bmap
->div
[div
][1], 1);
1467 isl_int_add(bmap
->div
[div
][1],
1468 bmap
->div
[div
][1], bmap
->div
[div
][0]);
1470 return isl_bool_false
;
1471 if (isl_seq_first_non_zero(constraint
+pos
+1,
1472 bmap
->n_div
-div
-1) != -1)
1473 return isl_bool_false
;
1474 } else if (isl_int_abs_eq(constraint
[pos
], bmap
->div
[div
][0])) {
1475 if (!isl_seq_eq(constraint
, bmap
->div
[div
]+1, pos
))
1476 return isl_bool_false
;
1477 if (isl_seq_first_non_zero(constraint
+pos
+1,
1478 bmap
->n_div
-div
-1) != -1)
1479 return isl_bool_false
;
1481 return isl_bool_false
;
1483 return isl_bool_true
;
1486 isl_bool
isl_basic_set_is_div_constraint(__isl_keep isl_basic_set
*bset
,
1487 isl_int
*constraint
, unsigned div
)
1489 return isl_basic_map_is_div_constraint(bset
, constraint
, div
);
1493 /* If the only constraints a div d=floor(f/m)
1494 * appears in are its two defining constraints
1497 * -(f - (m - 1)) + m d >= 0
1499 * then it can safely be removed.
1501 static isl_bool
div_is_redundant(__isl_keep isl_basic_map
*bmap
, int div
)
1504 unsigned pos
= isl_basic_map_offset(bmap
, isl_dim_div
) + div
;
1506 for (i
= 0; i
< bmap
->n_eq
; ++i
)
1507 if (!isl_int_is_zero(bmap
->eq
[i
][pos
]))
1508 return isl_bool_false
;
1510 for (i
= 0; i
< bmap
->n_ineq
; ++i
) {
1513 if (isl_int_is_zero(bmap
->ineq
[i
][pos
]))
1515 red
= isl_basic_map_is_div_constraint(bmap
, bmap
->ineq
[i
], div
);
1516 if (red
< 0 || !red
)
1520 for (i
= 0; i
< bmap
->n_div
; ++i
) {
1521 if (isl_int_is_zero(bmap
->div
[i
][0]))
1523 if (!isl_int_is_zero(bmap
->div
[i
][1+pos
]))
1524 return isl_bool_false
;
1527 return isl_bool_true
;
1531 * Remove divs that don't occur in any of the constraints or other divs.
1532 * These can arise when dropping constraints from a basic map or
1533 * when the divs of a basic map have been temporarily aligned
1534 * with the divs of another basic map.
1536 static __isl_give isl_basic_map
*remove_redundant_divs(
1537 __isl_take isl_basic_map
*bmap
)
1542 v_div
= isl_basic_map_var_offset(bmap
, isl_dim_div
);
1544 return isl_basic_map_free(bmap
);
1546 for (i
= bmap
->n_div
-1; i
>= 0; --i
) {
1549 redundant
= div_is_redundant(bmap
, i
);
1551 return isl_basic_map_free(bmap
);
1554 bmap
= isl_basic_map_drop_constraints_involving(bmap
,
1556 bmap
= isl_basic_map_drop_div(bmap
, i
);
1561 /* Mark "bmap" as final, without checking for obviously redundant
1562 * integer divisions. This function should be used when "bmap"
1563 * is known not to involve any such integer divisions.
1565 __isl_give isl_basic_map
*isl_basic_map_mark_final(
1566 __isl_take isl_basic_map
*bmap
)
1570 ISL_F_SET(bmap
, ISL_BASIC_SET_FINAL
);
1574 /* Mark "bmap" as final, after removing obviously redundant integer divisions.
1576 __isl_give isl_basic_map
*isl_basic_map_finalize(__isl_take isl_basic_map
*bmap
)
1578 bmap
= remove_redundant_divs(bmap
);
1579 bmap
= isl_basic_map_mark_final(bmap
);
1583 struct isl_basic_set
*isl_basic_set_finalize(struct isl_basic_set
*bset
)
1585 return bset_from_bmap(isl_basic_map_finalize(bset_to_bmap(bset
)));
1588 /* Remove definition of any div that is defined in terms of the given variable.
1589 * The div itself is not removed. Functions such as
1590 * eliminate_divs_ineq depend on the other divs remaining in place.
1592 static __isl_give isl_basic_map
*remove_dependent_vars(
1593 __isl_take isl_basic_map
*bmap
, int pos
)
1600 for (i
= 0; i
< bmap
->n_div
; ++i
) {
1601 if (isl_int_is_zero(bmap
->div
[i
][0]))
1603 if (isl_int_is_zero(bmap
->div
[i
][1+1+pos
]))
1605 bmap
= isl_basic_map_mark_div_unknown(bmap
, i
);
1612 /* Eliminate the specified variables from the constraints using
1613 * Fourier-Motzkin. The variables themselves are not removed.
1615 __isl_give isl_basic_map
*isl_basic_map_eliminate_vars(
1616 __isl_take isl_basic_map
*bmap
, unsigned pos
, unsigned n
)
1625 total
= isl_basic_map_dim(bmap
, isl_dim_all
);
1627 return isl_basic_map_free(bmap
);
1629 bmap
= isl_basic_map_cow(bmap
);
1630 for (d
= pos
+ n
- 1; d
>= 0 && d
>= pos
; --d
)
1631 bmap
= remove_dependent_vars(bmap
, d
);
1635 for (d
= pos
+ n
- 1;
1636 d
>= 0 && d
>= total
- bmap
->n_div
&& d
>= pos
; --d
)
1637 isl_seq_clr(bmap
->div
[d
-(total
-bmap
->n_div
)], 2+total
);
1638 for (d
= pos
+ n
- 1; d
>= 0 && d
>= pos
; --d
) {
1639 int n_lower
, n_upper
;
1642 for (i
= 0; i
< bmap
->n_eq
; ++i
) {
1643 if (isl_int_is_zero(bmap
->eq
[i
][1+d
]))
1645 bmap
= eliminate_var_using_equality(bmap
, d
,
1646 bmap
->eq
[i
], 0, NULL
);
1647 if (isl_basic_map_drop_equality(bmap
, i
) < 0)
1648 return isl_basic_map_free(bmap
);
1656 for (i
= 0; i
< bmap
->n_ineq
; ++i
) {
1657 if (isl_int_is_pos(bmap
->ineq
[i
][1+d
]))
1659 else if (isl_int_is_neg(bmap
->ineq
[i
][1+d
]))
1662 bmap
= isl_basic_map_extend_constraints(bmap
,
1663 0, n_lower
* n_upper
);
1666 for (i
= bmap
->n_ineq
- 1; i
>= 0; --i
) {
1668 if (isl_int_is_zero(bmap
->ineq
[i
][1+d
]))
1671 for (j
= 0; j
< i
; ++j
) {
1672 if (isl_int_is_zero(bmap
->ineq
[j
][1+d
]))
1675 if (isl_int_sgn(bmap
->ineq
[i
][1+d
]) ==
1676 isl_int_sgn(bmap
->ineq
[j
][1+d
]))
1678 k
= isl_basic_map_alloc_inequality(bmap
);
1681 isl_seq_cpy(bmap
->ineq
[k
], bmap
->ineq
[i
],
1683 isl_seq_elim(bmap
->ineq
[k
], bmap
->ineq
[j
],
1684 1+d
, 1+total
, NULL
);
1686 isl_basic_map_drop_inequality(bmap
, i
);
1689 if (n_lower
> 0 && n_upper
> 0) {
1690 bmap
= isl_basic_map_normalize_constraints(bmap
);
1691 bmap
= isl_basic_map_remove_duplicate_constraints(bmap
,
1693 bmap
= isl_basic_map_gauss(bmap
, NULL
);
1694 bmap
= isl_basic_map_remove_redundancies(bmap
);
1698 if (ISL_F_ISSET(bmap
, ISL_BASIC_MAP_EMPTY
))
1703 bmap
= isl_basic_map_gauss(bmap
, NULL
);
1706 isl_basic_map_free(bmap
);
1710 struct isl_basic_set
*isl_basic_set_eliminate_vars(
1711 struct isl_basic_set
*bset
, unsigned pos
, unsigned n
)
1713 return bset_from_bmap(isl_basic_map_eliminate_vars(bset_to_bmap(bset
),
1717 /* Eliminate the specified n dimensions starting at first from the
1718 * constraints, without removing the dimensions from the space.
1719 * If the set is rational, the dimensions are eliminated using Fourier-Motzkin.
1720 * Otherwise, they are projected out and the original space is restored.
1722 __isl_give isl_basic_map
*isl_basic_map_eliminate(
1723 __isl_take isl_basic_map
*bmap
,
1724 enum isl_dim_type type
, unsigned first
, unsigned n
)
1733 if (isl_basic_map_check_range(bmap
, type
, first
, n
) < 0)
1734 return isl_basic_map_free(bmap
);
1736 if (ISL_F_ISSET(bmap
, ISL_BASIC_MAP_RATIONAL
)) {
1737 first
+= isl_basic_map_offset(bmap
, type
) - 1;
1738 bmap
= isl_basic_map_eliminate_vars(bmap
, first
, n
);
1739 return isl_basic_map_finalize(bmap
);
1742 space
= isl_basic_map_get_space(bmap
);
1743 bmap
= isl_basic_map_project_out(bmap
, type
, first
, n
);
1744 bmap
= isl_basic_map_insert_dims(bmap
, type
, first
, n
);
1745 bmap
= isl_basic_map_reset_space(bmap
, space
);
1749 __isl_give isl_basic_set
*isl_basic_set_eliminate(
1750 __isl_take isl_basic_set
*bset
,
1751 enum isl_dim_type type
, unsigned first
, unsigned n
)
1753 return isl_basic_map_eliminate(bset
, type
, first
, n
);
1756 /* Remove all constraints from "bmap" that reference any unknown local
1757 * variables (directly or indirectly).
1759 * Dropping all constraints on a local variable will make it redundant,
1760 * so it will get removed implicitly by
1761 * isl_basic_map_drop_constraints_involving_dims. Some other local
1762 * variables may also end up becoming redundant if they only appear
1763 * in constraints together with the unknown local variable.
1764 * Therefore, start over after calling
1765 * isl_basic_map_drop_constraints_involving_dims.
1767 __isl_give isl_basic_map
*isl_basic_map_drop_constraint_involving_unknown_divs(
1768 __isl_take isl_basic_map
*bmap
)
1774 known
= isl_basic_map_divs_known(bmap
);
1776 return isl_basic_map_free(bmap
);
1780 n_div
= isl_basic_map_dim(bmap
, isl_dim_div
);
1782 return isl_basic_map_free(bmap
);
1783 o_div
= isl_basic_map_offset(bmap
, isl_dim_div
) - 1;
1785 for (i
= 0; i
< n_div
; ++i
) {
1786 known
= isl_basic_map_div_is_known(bmap
, i
);
1788 return isl_basic_map_free(bmap
);
1791 bmap
= remove_dependent_vars(bmap
, o_div
+ i
);
1792 bmap
= isl_basic_map_drop_constraints_involving_dims(bmap
,
1794 n_div
= isl_basic_map_dim(bmap
, isl_dim_div
);
1796 return isl_basic_map_free(bmap
);
1803 /* Remove all constraints from "map" that reference any unknown local
1804 * variables (directly or indirectly).
1806 * Since constraints may get dropped from the basic maps,
1807 * they may no longer be disjoint from each other.
1809 __isl_give isl_map
*isl_map_drop_constraint_involving_unknown_divs(
1810 __isl_take isl_map
*map
)
1815 known
= isl_map_divs_known(map
);
1817 return isl_map_free(map
);
1821 map
= isl_map_cow(map
);
1825 for (i
= 0; i
< map
->n
; ++i
) {
1827 isl_basic_map_drop_constraint_involving_unknown_divs(
1830 return isl_map_free(map
);
1834 ISL_F_CLR(map
, ISL_MAP_DISJOINT
);
1839 /* Don't assume equalities are in order, because align_divs
1840 * may have changed the order of the divs.
1842 static void compute_elimination_index(__isl_keep isl_basic_map
*bmap
, int *elim
)
1847 total
= isl_space_dim(bmap
->dim
, isl_dim_all
);
1848 for (d
= 0; d
< total
; ++d
)
1850 for (i
= 0; i
< bmap
->n_eq
; ++i
) {
1851 for (d
= total
- 1; d
>= 0; --d
) {
1852 if (isl_int_is_zero(bmap
->eq
[i
][1+d
]))
1860 static void set_compute_elimination_index(__isl_keep isl_basic_set
*bset
,
1863 compute_elimination_index(bset_to_bmap(bset
), elim
);
1866 static int reduced_using_equalities(isl_int
*dst
, isl_int
*src
,
1867 __isl_keep isl_basic_map
*bmap
, int *elim
)
1873 total
= isl_space_dim(bmap
->dim
, isl_dim_all
);
1874 for (d
= total
- 1; d
>= 0; --d
) {
1875 if (isl_int_is_zero(src
[1+d
]))
1880 isl_seq_cpy(dst
, src
, 1 + total
);
1883 isl_seq_elim(dst
, bmap
->eq
[elim
[d
]], 1 + d
, 1 + total
, NULL
);
1888 static int set_reduced_using_equalities(isl_int
*dst
, isl_int
*src
,
1889 __isl_keep isl_basic_set
*bset
, int *elim
)
1891 return reduced_using_equalities(dst
, src
,
1892 bset_to_bmap(bset
), elim
);
1895 static __isl_give isl_basic_set
*isl_basic_set_reduce_using_equalities(
1896 __isl_take isl_basic_set
*bset
, __isl_take isl_basic_set
*context
)
1902 if (!bset
|| !context
)
1905 if (context
->n_eq
== 0) {
1906 isl_basic_set_free(context
);
1910 bset
= isl_basic_set_cow(bset
);
1911 dim
= isl_basic_set_dim(bset
, isl_dim_set
);
1915 elim
= isl_alloc_array(bset
->ctx
, int, dim
);
1918 set_compute_elimination_index(context
, elim
);
1919 for (i
= 0; i
< bset
->n_eq
; ++i
)
1920 set_reduced_using_equalities(bset
->eq
[i
], bset
->eq
[i
],
1922 for (i
= 0; i
< bset
->n_ineq
; ++i
)
1923 set_reduced_using_equalities(bset
->ineq
[i
], bset
->ineq
[i
],
1925 isl_basic_set_free(context
);
1927 bset
= isl_basic_set_simplify(bset
);
1928 bset
= isl_basic_set_finalize(bset
);
1931 isl_basic_set_free(bset
);
1932 isl_basic_set_free(context
);
1936 /* For each inequality in "ineq" that is a shifted (more relaxed)
1937 * copy of an inequality in "context", mark the corresponding entry
1939 * If an inequality only has a non-negative constant term, then
1942 static isl_stat
mark_shifted_constraints(__isl_keep isl_mat
*ineq
,
1943 __isl_keep isl_basic_set
*context
, int *row
)
1945 struct isl_constraint_index ci
;
1946 isl_size n_ineq
, cols
;
1950 if (!ineq
|| !context
)
1951 return isl_stat_error
;
1952 if (context
->n_ineq
== 0)
1954 if (setup_constraint_index(&ci
, context
) < 0)
1955 return isl_stat_error
;
1957 n_ineq
= isl_mat_rows(ineq
);
1958 cols
= isl_mat_cols(ineq
);
1959 if (n_ineq
< 0 || cols
< 0)
1960 return isl_stat_error
;
1962 for (k
= 0; k
< n_ineq
; ++k
) {
1966 l
= isl_seq_first_non_zero(ineq
->row
[k
] + 1, total
);
1967 if (l
< 0 && isl_int_is_nonneg(ineq
->row
[k
][0])) {
1971 redundant
= constraint_index_is_redundant(&ci
, ineq
->row
[k
]);
1978 constraint_index_free(&ci
);
1981 constraint_index_free(&ci
);
1982 return isl_stat_error
;
1985 static __isl_give isl_basic_set
*remove_shifted_constraints(
1986 __isl_take isl_basic_set
*bset
, __isl_keep isl_basic_set
*context
)
1988 struct isl_constraint_index ci
;
1991 if (!bset
|| !context
)
1994 if (context
->n_ineq
== 0)
1996 if (setup_constraint_index(&ci
, context
) < 0)
1999 for (k
= 0; k
< bset
->n_ineq
; ++k
) {
2002 redundant
= constraint_index_is_redundant(&ci
, bset
->ineq
[k
]);
2007 bset
= isl_basic_set_cow(bset
);
2010 isl_basic_set_drop_inequality(bset
, k
);
2013 constraint_index_free(&ci
);
2016 constraint_index_free(&ci
);
2020 /* Remove constraints from "bmap" that are identical to constraints
2021 * in "context" or that are more relaxed (greater constant term).
2023 * We perform the test for shifted copies on the pure constraints
2024 * in remove_shifted_constraints.
2026 static __isl_give isl_basic_map
*isl_basic_map_remove_shifted_constraints(
2027 __isl_take isl_basic_map
*bmap
, __isl_take isl_basic_map
*context
)
2029 isl_basic_set
*bset
, *bset_context
;
2031 if (!bmap
|| !context
)
2034 if (bmap
->n_ineq
== 0 || context
->n_ineq
== 0) {
2035 isl_basic_map_free(context
);
2039 context
= isl_basic_map_align_divs(context
, bmap
);
2040 bmap
= isl_basic_map_align_divs(bmap
, context
);
2042 bset
= isl_basic_map_underlying_set(isl_basic_map_copy(bmap
));
2043 bset_context
= isl_basic_map_underlying_set(context
);
2044 bset
= remove_shifted_constraints(bset
, bset_context
);
2045 isl_basic_set_free(bset_context
);
2047 bmap
= isl_basic_map_overlying_set(bset
, bmap
);
2051 isl_basic_map_free(bmap
);
2052 isl_basic_map_free(context
);
2056 /* Does the (linear part of a) constraint "c" involve any of the "len"
2057 * "relevant" dimensions?
2059 static int is_related(isl_int
*c
, int len
, int *relevant
)
2063 for (i
= 0; i
< len
; ++i
) {
2066 if (!isl_int_is_zero(c
[i
]))
2073 /* Drop constraints from "bmap" that do not involve any of
2074 * the dimensions marked "relevant".
2076 static __isl_give isl_basic_map
*drop_unrelated_constraints(
2077 __isl_take isl_basic_map
*bmap
, int *relevant
)
2082 dim
= isl_basic_map_dim(bmap
, isl_dim_all
);
2084 return isl_basic_map_free(bmap
);
2085 for (i
= 0; i
< dim
; ++i
)
2091 for (i
= bmap
->n_eq
- 1; i
>= 0; --i
)
2092 if (!is_related(bmap
->eq
[i
] + 1, dim
, relevant
)) {
2093 bmap
= isl_basic_map_cow(bmap
);
2094 if (isl_basic_map_drop_equality(bmap
, i
) < 0)
2095 return isl_basic_map_free(bmap
);
2098 for (i
= bmap
->n_ineq
- 1; i
>= 0; --i
)
2099 if (!is_related(bmap
->ineq
[i
] + 1, dim
, relevant
)) {
2100 bmap
= isl_basic_map_cow(bmap
);
2101 if (isl_basic_map_drop_inequality(bmap
, i
) < 0)
2102 return isl_basic_map_free(bmap
);
2108 /* Update the groups in "group" based on the (linear part of a) constraint "c".
2110 * In particular, for any variable involved in the constraint,
2111 * find the actual group id from before and replace the group
2112 * of the corresponding variable by the minimal group of all
2113 * the variables involved in the constraint considered so far
2114 * (if this minimum is smaller) or replace the minimum by this group
2115 * (if the minimum is larger).
2117 * At the end, all the variables in "c" will (indirectly) point
2118 * to the minimal of the groups that they referred to originally.
2120 static void update_groups(int dim
, int *group
, isl_int
*c
)
2125 for (j
= 0; j
< dim
; ++j
) {
2126 if (isl_int_is_zero(c
[j
]))
2128 while (group
[j
] >= 0 && group
[group
[j
]] != group
[j
])
2129 group
[j
] = group
[group
[j
]];
2130 if (group
[j
] == min
)
2132 if (group
[j
] < min
) {
2133 if (min
>= 0 && min
< dim
)
2134 group
[min
] = group
[j
];
2137 group
[group
[j
]] = min
;
2141 /* Allocate an array of groups of variables, one for each variable
2142 * in "context", initialized to zero.
2144 static int *alloc_groups(__isl_keep isl_basic_set
*context
)
2149 dim
= isl_basic_set_dim(context
, isl_dim_set
);
2152 ctx
= isl_basic_set_get_ctx(context
);
2153 return isl_calloc_array(ctx
, int, dim
);
2156 /* Drop constraints from "bmap" that only involve variables that are
2157 * not related to any of the variables marked with a "-1" in "group".
2159 * We construct groups of variables that collect variables that
2160 * (indirectly) appear in some common constraint of "bmap".
2161 * Each group is identified by the first variable in the group,
2162 * except for the special group of variables that was already identified
2163 * in the input as -1 (or are related to those variables).
2164 * If group[i] is equal to i (or -1), then the group of i is i (or -1),
2165 * otherwise the group of i is the group of group[i].
2167 * We first initialize groups for the remaining variables.
2168 * Then we iterate over the constraints of "bmap" and update the
2169 * group of the variables in the constraint by the smallest group.
2170 * Finally, we resolve indirect references to groups by running over
2173 * After computing the groups, we drop constraints that do not involve
2174 * any variables in the -1 group.
2176 __isl_give isl_basic_map
*isl_basic_map_drop_unrelated_constraints(
2177 __isl_take isl_basic_map
*bmap
, __isl_take
int *group
)
2183 dim
= isl_basic_map_dim(bmap
, isl_dim_all
);
2185 return isl_basic_map_free(bmap
);
2188 for (i
= 0; i
< dim
; ++i
)
2190 last
= group
[i
] = i
;
2196 for (i
= 0; i
< bmap
->n_eq
; ++i
)
2197 update_groups(dim
, group
, bmap
->eq
[i
] + 1);
2198 for (i
= 0; i
< bmap
->n_ineq
; ++i
)
2199 update_groups(dim
, group
, bmap
->ineq
[i
] + 1);
2201 for (i
= 0; i
< dim
; ++i
)
2203 group
[i
] = group
[group
[i
]];
2205 for (i
= 0; i
< dim
; ++i
)
2206 group
[i
] = group
[i
] == -1;
2208 bmap
= drop_unrelated_constraints(bmap
, group
);
2214 /* Drop constraints from "context" that are irrelevant for computing
2215 * the gist of "bset".
2217 * In particular, drop constraints in variables that are not related
2218 * to any of the variables involved in the constraints of "bset"
2219 * in the sense that there is no sequence of constraints that connects them.
2221 * We first mark all variables that appear in "bset" as belonging
2222 * to a "-1" group and then continue with group_and_drop_irrelevant_constraints.
2224 static __isl_give isl_basic_set
*drop_irrelevant_constraints(
2225 __isl_take isl_basic_set
*context
, __isl_keep isl_basic_set
*bset
)
2231 dim
= isl_basic_set_dim(bset
, isl_dim_set
);
2232 if (!context
|| dim
< 0)
2233 return isl_basic_set_free(context
);
2235 group
= alloc_groups(context
);
2238 return isl_basic_set_free(context
);
2240 for (i
= 0; i
< dim
; ++i
) {
2241 for (j
= 0; j
< bset
->n_eq
; ++j
)
2242 if (!isl_int_is_zero(bset
->eq
[j
][1 + i
]))
2244 if (j
< bset
->n_eq
) {
2248 for (j
= 0; j
< bset
->n_ineq
; ++j
)
2249 if (!isl_int_is_zero(bset
->ineq
[j
][1 + i
]))
2251 if (j
< bset
->n_ineq
)
2255 return isl_basic_map_drop_unrelated_constraints(context
, group
);
2258 /* Drop constraints from "context" that are irrelevant for computing
2259 * the gist of the inequalities "ineq".
2260 * Inequalities in "ineq" for which the corresponding element of row
2261 * is set to -1 have already been marked for removal and should be ignored.
2263 * In particular, drop constraints in variables that are not related
2264 * to any of the variables involved in "ineq"
2265 * in the sense that there is no sequence of constraints that connects them.
2267 * We first mark all variables that appear in "bset" as belonging
2268 * to a "-1" group and then continue with group_and_drop_irrelevant_constraints.
2270 static __isl_give isl_basic_set
*drop_irrelevant_constraints_marked(
2271 __isl_take isl_basic_set
*context
, __isl_keep isl_mat
*ineq
, int *row
)
2278 dim
= isl_basic_set_dim(context
, isl_dim_set
);
2279 n
= isl_mat_rows(ineq
);
2280 if (dim
< 0 || n
< 0)
2281 return isl_basic_set_free(context
);
2283 group
= alloc_groups(context
);
2286 return isl_basic_set_free(context
);
2288 for (i
= 0; i
< dim
; ++i
) {
2289 for (j
= 0; j
< n
; ++j
) {
2292 if (!isl_int_is_zero(ineq
->row
[j
][1 + i
]))
2299 return isl_basic_map_drop_unrelated_constraints(context
, group
);
2302 /* Do all "n" entries of "row" contain a negative value?
2304 static int all_neg(int *row
, int n
)
2308 for (i
= 0; i
< n
; ++i
)
2315 /* Update the inequalities in "bset" based on the information in "row"
2318 * In particular, the array "row" contains either -1, meaning that
2319 * the corresponding inequality of "bset" is redundant, or the index
2320 * of an inequality in "tab".
2322 * If the row entry is -1, then drop the inequality.
2323 * Otherwise, if the constraint is marked redundant in the tableau,
2324 * then drop the inequality. Similarly, if it is marked as an equality
2325 * in the tableau, then turn the inequality into an equality and
2326 * perform Gaussian elimination.
2328 static __isl_give isl_basic_set
*update_ineq(__isl_take isl_basic_set
*bset
,
2329 __isl_keep
int *row
, struct isl_tab
*tab
)
2334 int found_equality
= 0;
2338 if (tab
&& tab
->empty
)
2339 return isl_basic_set_set_to_empty(bset
);
2341 n_ineq
= bset
->n_ineq
;
2342 for (i
= n_ineq
- 1; i
>= 0; --i
) {
2344 if (isl_basic_set_drop_inequality(bset
, i
) < 0)
2345 return isl_basic_set_free(bset
);
2351 if (isl_tab_is_equality(tab
, n_eq
+ row
[i
])) {
2352 isl_basic_map_inequality_to_equality(bset
, i
);
2354 } else if (isl_tab_is_redundant(tab
, n_eq
+ row
[i
])) {
2355 if (isl_basic_set_drop_inequality(bset
, i
) < 0)
2356 return isl_basic_set_free(bset
);
2361 bset
= isl_basic_set_gauss(bset
, NULL
);
2362 bset
= isl_basic_set_finalize(bset
);
2366 /* Update the inequalities in "bset" based on the information in "row"
2367 * and "tab" and free all arguments (other than "bset").
2369 static __isl_give isl_basic_set
*update_ineq_free(
2370 __isl_take isl_basic_set
*bset
, __isl_take isl_mat
*ineq
,
2371 __isl_take isl_basic_set
*context
, __isl_take
int *row
,
2372 struct isl_tab
*tab
)
2375 isl_basic_set_free(context
);
2377 bset
= update_ineq(bset
, row
, tab
);
2384 /* Remove all information from bset that is redundant in the context
2386 * "ineq" contains the (possibly transformed) inequalities of "bset",
2387 * in the same order.
2388 * The (explicit) equalities of "bset" are assumed to have been taken
2389 * into account by the transformation such that only the inequalities
2391 * "context" is assumed not to be empty.
2393 * "row" keeps track of the constraint index of a "bset" inequality in "tab".
2394 * A value of -1 means that the inequality is obviously redundant and may
2395 * not even appear in "tab".
2397 * We first mark the inequalities of "bset"
2398 * that are obviously redundant with respect to some inequality in "context".
2399 * Then we remove those constraints from "context" that have become
2400 * irrelevant for computing the gist of "bset".
2401 * Note that this removal of constraints cannot be replaced by
2402 * a factorization because factors in "bset" may still be connected
2403 * to each other through constraints in "context".
2405 * If there are any inequalities left, we construct a tableau for
2406 * the context and then add the inequalities of "bset".
2407 * Before adding these inequalities, we freeze all constraints such that
2408 * they won't be considered redundant in terms of the constraints of "bset".
2409 * Then we detect all redundant constraints (among the
2410 * constraints that weren't frozen), first by checking for redundancy in the
2411 * the tableau and then by checking if replacing a constraint by its negation
2412 * would lead to an empty set. This last step is fairly expensive
2413 * and could be optimized by more reuse of the tableau.
2414 * Finally, we update bset according to the results.
2416 static __isl_give isl_basic_set
*uset_gist_full(__isl_take isl_basic_set
*bset
,
2417 __isl_take isl_mat
*ineq
, __isl_take isl_basic_set
*context
)
2422 isl_basic_set
*combined
= NULL
;
2423 struct isl_tab
*tab
= NULL
;
2424 unsigned n_eq
, context_ineq
;
2426 if (!bset
|| !ineq
|| !context
)
2429 if (bset
->n_ineq
== 0 || isl_basic_set_plain_is_universe(context
)) {
2430 isl_basic_set_free(context
);
2435 ctx
= isl_basic_set_get_ctx(context
);
2436 row
= isl_calloc_array(ctx
, int, bset
->n_ineq
);
2440 if (mark_shifted_constraints(ineq
, context
, row
) < 0)
2442 if (all_neg(row
, bset
->n_ineq
))
2443 return update_ineq_free(bset
, ineq
, context
, row
, NULL
);
2445 context
= drop_irrelevant_constraints_marked(context
, ineq
, row
);
2448 if (isl_basic_set_plain_is_universe(context
))
2449 return update_ineq_free(bset
, ineq
, context
, row
, NULL
);
2451 n_eq
= context
->n_eq
;
2452 context_ineq
= context
->n_ineq
;
2453 combined
= isl_basic_set_cow(isl_basic_set_copy(context
));
2454 combined
= isl_basic_set_extend_constraints(combined
, 0, bset
->n_ineq
);
2455 tab
= isl_tab_from_basic_set(combined
, 0);
2456 for (i
= 0; i
< context_ineq
; ++i
)
2457 if (isl_tab_freeze_constraint(tab
, n_eq
+ i
) < 0)
2459 if (isl_tab_extend_cons(tab
, bset
->n_ineq
) < 0)
2462 for (i
= 0; i
< bset
->n_ineq
; ++i
) {
2465 combined
= isl_basic_set_add_ineq(combined
, ineq
->row
[i
]);
2466 if (isl_tab_add_ineq(tab
, ineq
->row
[i
]) < 0)
2470 if (isl_tab_detect_implicit_equalities(tab
) < 0)
2472 if (isl_tab_detect_redundant(tab
) < 0)
2474 for (i
= bset
->n_ineq
- 1; i
>= 0; --i
) {
2475 isl_basic_set
*test
;
2481 if (tab
->con
[n_eq
+ r
].is_redundant
)
2483 test
= isl_basic_set_dup(combined
);
2484 test
= isl_inequality_negate(test
, r
);
2485 test
= isl_basic_set_update_from_tab(test
, tab
);
2486 is_empty
= isl_basic_set_is_empty(test
);
2487 isl_basic_set_free(test
);
2491 tab
->con
[n_eq
+ r
].is_redundant
= 1;
2493 bset
= update_ineq_free(bset
, ineq
, context
, row
, tab
);
2495 ISL_F_SET(bset
, ISL_BASIC_SET_NO_IMPLICIT
);
2496 ISL_F_SET(bset
, ISL_BASIC_SET_NO_REDUNDANT
);
2499 isl_basic_set_free(combined
);
2505 isl_basic_set_free(combined
);
2506 isl_basic_set_free(context
);
2507 isl_basic_set_free(bset
);
2511 /* Extract the inequalities of "bset" as an isl_mat.
2513 static __isl_give isl_mat
*extract_ineq(__isl_keep isl_basic_set
*bset
)
2519 total
= isl_basic_set_dim(bset
, isl_dim_all
);
2523 ctx
= isl_basic_set_get_ctx(bset
);
2524 ineq
= isl_mat_sub_alloc6(ctx
, bset
->ineq
, 0, bset
->n_ineq
,
2530 /* Remove all information from "bset" that is redundant in the context
2531 * of "context", for the case where both "bset" and "context" are
2534 static __isl_give isl_basic_set
*uset_gist_uncompressed(
2535 __isl_take isl_basic_set
*bset
, __isl_take isl_basic_set
*context
)
2539 ineq
= extract_ineq(bset
);
2540 return uset_gist_full(bset
, ineq
, context
);
2543 /* Remove all information from "bset" that is redundant in the context
2544 * of "context", for the case where the combined equalities of
2545 * "bset" and "context" allow for a compression that can be obtained
2546 * by preapplication of "T".
2548 * "bset" itself is not transformed by "T". Instead, the inequalities
2549 * are extracted from "bset" and those are transformed by "T".
2550 * uset_gist_full then determines which of the transformed inequalities
2551 * are redundant with respect to the transformed "context" and removes
2552 * the corresponding inequalities from "bset".
2554 * After preapplying "T" to the inequalities, any common factor is
2555 * removed from the coefficients. If this results in a tightening
2556 * of the constant term, then the same tightening is applied to
2557 * the corresponding untransformed inequality in "bset".
2558 * That is, if after plugging in T, a constraint f(x) >= 0 is of the form
2562 * with 0 <= r < g, then it is equivalent to
2566 * This means that f(x) >= 0 is equivalent to f(x) - r >= 0 in the affine
2567 * subspace compressed by T since the latter would be transformed to
2571 static __isl_give isl_basic_set
*uset_gist_compressed(
2572 __isl_take isl_basic_set
*bset
, __isl_take isl_basic_set
*context
,
2573 __isl_take isl_mat
*T
)
2578 isl_size n_row
, n_col
;
2581 ineq
= extract_ineq(bset
);
2582 ineq
= isl_mat_product(ineq
, isl_mat_copy(T
));
2583 context
= isl_basic_set_preimage(context
, T
);
2585 if (!ineq
|| !context
)
2587 if (isl_basic_set_plain_is_empty(context
)) {
2589 isl_basic_set_free(context
);
2590 return isl_basic_set_set_to_empty(bset
);
2593 ctx
= isl_mat_get_ctx(ineq
);
2594 n_row
= isl_mat_rows(ineq
);
2595 n_col
= isl_mat_cols(ineq
);
2596 if (n_row
< 0 || n_col
< 0)
2599 for (i
= 0; i
< n_row
; ++i
) {
2600 isl_seq_gcd(ineq
->row
[i
] + 1, n_col
- 1, &ctx
->normalize_gcd
);
2601 if (isl_int_is_zero(ctx
->normalize_gcd
))
2603 if (isl_int_is_one(ctx
->normalize_gcd
))
2605 isl_seq_scale_down(ineq
->row
[i
] + 1, ineq
->row
[i
] + 1,
2606 ctx
->normalize_gcd
, n_col
- 1);
2607 isl_int_fdiv_r(rem
, ineq
->row
[i
][0], ctx
->normalize_gcd
);
2608 isl_int_fdiv_q(ineq
->row
[i
][0],
2609 ineq
->row
[i
][0], ctx
->normalize_gcd
);
2610 if (isl_int_is_zero(rem
))
2612 bset
= isl_basic_set_cow(bset
);
2615 isl_int_sub(bset
->ineq
[i
][0], bset
->ineq
[i
][0], rem
);
2619 return uset_gist_full(bset
, ineq
, context
);
2622 isl_basic_set_free(context
);
2623 isl_basic_set_free(bset
);
2627 /* Project "bset" onto the variables that are involved in "template".
2629 static __isl_give isl_basic_set
*project_onto_involved(
2630 __isl_take isl_basic_set
*bset
, __isl_keep isl_basic_set
*template)
2635 n
= isl_basic_set_dim(template, isl_dim_set
);
2636 if (n
< 0 || !template)
2637 return isl_basic_set_free(bset
);
2639 for (i
= 0; i
< n
; ++i
) {
2642 involved
= isl_basic_set_involves_dims(template,
2645 return isl_basic_set_free(bset
);
2648 bset
= isl_basic_set_eliminate_vars(bset
, i
, 1);
2654 /* Remove all information from bset that is redundant in the context
2655 * of context. In particular, equalities that are linear combinations
2656 * of those in context are removed. Then the inequalities that are
2657 * redundant in the context of the equalities and inequalities of
2658 * context are removed.
2660 * First of all, we drop those constraints from "context"
2661 * that are irrelevant for computing the gist of "bset".
2662 * Alternatively, we could factorize the intersection of "context" and "bset".
2664 * We first compute the intersection of the integer affine hulls
2665 * of "bset" and "context",
2666 * compute the gist inside this intersection and then reduce
2667 * the constraints with respect to the equalities of the context
2668 * that only involve variables already involved in the input.
2670 * If two constraints are mutually redundant, then uset_gist_full
2671 * will remove the second of those constraints. We therefore first
2672 * sort the constraints so that constraints not involving existentially
2673 * quantified variables are given precedence over those that do.
2674 * We have to perform this sorting before the variable compression,
2675 * because that may effect the order of the variables.
2677 static __isl_give isl_basic_set
*uset_gist(__isl_take isl_basic_set
*bset
,
2678 __isl_take isl_basic_set
*context
)
2683 isl_basic_set
*aff_context
;
2686 total
= isl_basic_set_dim(bset
, isl_dim_all
);
2687 if (total
< 0 || !context
)
2690 context
= drop_irrelevant_constraints(context
, bset
);
2692 bset
= isl_basic_set_detect_equalities(bset
);
2693 aff
= isl_basic_set_copy(bset
);
2694 aff
= isl_basic_set_plain_affine_hull(aff
);
2695 context
= isl_basic_set_detect_equalities(context
);
2696 aff_context
= isl_basic_set_copy(context
);
2697 aff_context
= isl_basic_set_plain_affine_hull(aff_context
);
2698 aff
= isl_basic_set_intersect(aff
, aff_context
);
2701 if (isl_basic_set_plain_is_empty(aff
)) {
2702 isl_basic_set_free(bset
);
2703 isl_basic_set_free(context
);
2706 bset
= isl_basic_set_sort_constraints(bset
);
2707 if (aff
->n_eq
== 0) {
2708 isl_basic_set_free(aff
);
2709 return uset_gist_uncompressed(bset
, context
);
2711 eq
= isl_mat_sub_alloc6(bset
->ctx
, aff
->eq
, 0, aff
->n_eq
, 0, 1 + total
);
2712 eq
= isl_mat_cow(eq
);
2713 T
= isl_mat_variable_compression(eq
, NULL
);
2714 isl_basic_set_free(aff
);
2715 if (T
&& T
->n_col
== 0) {
2717 isl_basic_set_free(context
);
2718 return isl_basic_set_set_to_empty(bset
);
2721 aff_context
= isl_basic_set_affine_hull(isl_basic_set_copy(context
));
2722 aff_context
= project_onto_involved(aff_context
, bset
);
2724 bset
= uset_gist_compressed(bset
, context
, T
);
2725 bset
= isl_basic_set_reduce_using_equalities(bset
, aff_context
);
2728 ISL_F_SET(bset
, ISL_BASIC_SET_NO_IMPLICIT
);
2729 ISL_F_SET(bset
, ISL_BASIC_SET_NO_REDUNDANT
);
2734 isl_basic_set_free(bset
);
2735 isl_basic_set_free(context
);
2739 /* Return the number of equality constraints in "bmap" that involve
2740 * local variables. This function assumes that Gaussian elimination
2741 * has been applied to the equality constraints.
2743 static int n_div_eq(__isl_keep isl_basic_map
*bmap
)
2746 isl_size total
, n_div
;
2751 if (bmap
->n_eq
== 0)
2754 total
= isl_basic_map_dim(bmap
, isl_dim_all
);
2755 n_div
= isl_basic_map_dim(bmap
, isl_dim_div
);
2756 if (total
< 0 || n_div
< 0)
2760 for (i
= 0; i
< bmap
->n_eq
; ++i
)
2761 if (isl_seq_first_non_zero(bmap
->eq
[i
] + 1 + total
,
2768 /* Construct a basic map in "space" defined by the equality constraints in "eq".
2769 * The constraints are assumed not to involve any local variables.
2771 static __isl_give isl_basic_map
*basic_map_from_equalities(
2772 __isl_take isl_space
*space
, __isl_take isl_mat
*eq
)
2776 isl_basic_map
*bmap
= NULL
;
2778 total
= isl_space_dim(space
, isl_dim_all
);
2779 if (total
< 0 || !eq
)
2782 if (1 + total
!= eq
->n_col
)
2783 isl_die(isl_space_get_ctx(space
), isl_error_internal
,
2784 "unexpected number of columns", goto error
);
2786 bmap
= isl_basic_map_alloc_space(isl_space_copy(space
),
2788 for (i
= 0; i
< eq
->n_row
; ++i
) {
2789 k
= isl_basic_map_alloc_equality(bmap
);
2792 isl_seq_cpy(bmap
->eq
[k
], eq
->row
[i
], eq
->n_col
);
2795 isl_space_free(space
);
2799 isl_space_free(space
);
2801 isl_basic_map_free(bmap
);
2805 /* Construct and return a variable compression based on the equality
2806 * constraints in "bmap1" and "bmap2" that do not involve the local variables.
2807 * "n1" is the number of (initial) equality constraints in "bmap1"
2808 * that do involve local variables.
2809 * "n2" is the number of (initial) equality constraints in "bmap2"
2810 * that do involve local variables.
2811 * "total" is the total number of other variables.
2812 * This function assumes that Gaussian elimination
2813 * has been applied to the equality constraints in both "bmap1" and "bmap2"
2814 * such that the equality constraints not involving local variables
2815 * are those that start at "n1" or "n2".
2817 * If either of "bmap1" and "bmap2" does not have such equality constraints,
2818 * then simply compute the compression based on the equality constraints
2819 * in the other basic map.
2820 * Otherwise, combine the equality constraints from both into a new
2821 * basic map such that Gaussian elimination can be applied to this combination
2822 * and then construct a variable compression from the resulting
2823 * equality constraints.
2825 static __isl_give isl_mat
*combined_variable_compression(
2826 __isl_keep isl_basic_map
*bmap1
, int n1
,
2827 __isl_keep isl_basic_map
*bmap2
, int n2
, int total
)
2830 isl_mat
*E1
, *E2
, *V
;
2831 isl_basic_map
*bmap
;
2833 ctx
= isl_basic_map_get_ctx(bmap1
);
2834 if (bmap1
->n_eq
== n1
) {
2835 E2
= isl_mat_sub_alloc6(ctx
, bmap2
->eq
,
2836 n2
, bmap2
->n_eq
- n2
, 0, 1 + total
);
2837 return isl_mat_variable_compression(E2
, NULL
);
2839 if (bmap2
->n_eq
== n2
) {
2840 E1
= isl_mat_sub_alloc6(ctx
, bmap1
->eq
,
2841 n1
, bmap1
->n_eq
- n1
, 0, 1 + total
);
2842 return isl_mat_variable_compression(E1
, NULL
);
2844 E1
= isl_mat_sub_alloc6(ctx
, bmap1
->eq
,
2845 n1
, bmap1
->n_eq
- n1
, 0, 1 + total
);
2846 E2
= isl_mat_sub_alloc6(ctx
, bmap2
->eq
,
2847 n2
, bmap2
->n_eq
- n2
, 0, 1 + total
);
2848 E1
= isl_mat_concat(E1
, E2
);
2849 bmap
= basic_map_from_equalities(isl_basic_map_get_space(bmap1
), E1
);
2850 bmap
= isl_basic_map_gauss(bmap
, NULL
);
2853 E1
= isl_mat_sub_alloc6(ctx
, bmap
->eq
, 0, bmap
->n_eq
, 0, 1 + total
);
2854 V
= isl_mat_variable_compression(E1
, NULL
);
2855 isl_basic_map_free(bmap
);
2860 /* Extract the stride constraints from "bmap", compressed
2861 * with respect to both the stride constraints in "context" and
2862 * the remaining equality constraints in both "bmap" and "context".
2863 * "bmap_n_eq" is the number of (initial) stride constraints in "bmap".
2864 * "context_n_eq" is the number of (initial) stride constraints in "context".
2866 * Let x be all variables in "bmap" (and "context") other than the local
2867 * variables. First compute a variable compression
2871 * based on the non-stride equality constraints in "bmap" and "context".
2872 * Consider the stride constraints of "context",
2876 * with y the local variables and plug in the variable compression,
2879 * A(V x') + B(y) = 0
2881 * Use these constraints to compute a parameter compression on x'
2885 * Now consider the stride constraints of "bmap"
2889 * and plug in x = V*T x''.
2890 * That is, return A = [C*V*T D].
2892 static __isl_give isl_mat
*extract_compressed_stride_constraints(
2893 __isl_keep isl_basic_map
*bmap
, int bmap_n_eq
,
2894 __isl_keep isl_basic_map
*context
, int context_n_eq
)
2896 isl_size total
, n_div
;
2898 isl_mat
*A
, *B
, *T
, *V
;
2900 total
= isl_basic_map_dim(context
, isl_dim_all
);
2901 n_div
= isl_basic_map_dim(context
, isl_dim_div
);
2902 if (total
< 0 || n_div
< 0)
2906 ctx
= isl_basic_map_get_ctx(bmap
);
2908 V
= combined_variable_compression(bmap
, bmap_n_eq
,
2909 context
, context_n_eq
, total
);
2911 A
= isl_mat_sub_alloc6(ctx
, context
->eq
, 0, context_n_eq
, 0, 1 + total
);
2912 B
= isl_mat_sub_alloc6(ctx
, context
->eq
,
2913 0, context_n_eq
, 1 + total
, n_div
);
2914 A
= isl_mat_product(A
, isl_mat_copy(V
));
2915 T
= isl_mat_parameter_compression_ext(A
, B
);
2916 T
= isl_mat_product(V
, T
);
2918 n_div
= isl_basic_map_dim(bmap
, isl_dim_div
);
2920 T
= isl_mat_free(T
);
2922 T
= isl_mat_diagonal(T
, isl_mat_identity(ctx
, n_div
));
2924 A
= isl_mat_sub_alloc6(ctx
, bmap
->eq
,
2925 0, bmap_n_eq
, 0, 1 + total
+ n_div
);
2926 A
= isl_mat_product(A
, T
);
2931 /* Remove the prime factors from *g that have an exponent that
2932 * is strictly smaller than the exponent in "c".
2933 * All exponents in *g are known to be smaller than or equal
2936 * That is, if *g is equal to
2938 * p_1^{e_1} p_2^{e_2} ... p_n^{e_n}
2940 * and "c" is equal to
2942 * p_1^{f_1} p_2^{f_2} ... p_n^{f_n}
2946 * p_1^{e_1 * (e_1 = f_1)} p_2^{e_2 * (e_2 = f_2)} ...
2947 * p_n^{e_n * (e_n = f_n)}
2949 * If e_i = f_i, then c / *g does not have any p_i factors and therefore
2950 * neither does the gcd of *g and c / *g.
2951 * If e_i < f_i, then the gcd of *g and c / *g has a positive
2952 * power min(e_i, s_i) of p_i with s_i = f_i - e_i among its factors.
2953 * Dividing *g by this gcd therefore strictly reduces the exponent
2954 * of the prime factors that need to be removed, while leaving the
2955 * other prime factors untouched.
2956 * Repeating this process until gcd(*g, c / *g) = 1 therefore
2957 * removes all undesired factors, without removing any others.
2959 static void remove_incomplete_powers(isl_int
*g
, isl_int c
)
2965 isl_int_divexact(t
, c
, *g
);
2966 isl_int_gcd(t
, t
, *g
);
2967 if (isl_int_is_one(t
))
2969 isl_int_divexact(*g
, *g
, t
);
2974 /* Reduce the "n" stride constraints in "bmap" based on a copy "A"
2975 * of the same stride constraints in a compressed space that exploits
2976 * all equalities in the context and the other equalities in "bmap".
2978 * If the stride constraints of "bmap" are of the form
2982 * then A is of the form
2986 * If any of these constraints involves only a single local variable y,
2987 * then the constraint appears as
2997 * Let g be the gcd of m and the coefficients of h.
2998 * Then, in particular, g is a divisor of the coefficients of h and
3002 * is known to be a multiple of g.
3003 * If some prime factor in m appears with the same exponent in g,
3004 * then it can be removed from m because f(x) is already known
3005 * to be a multiple of g and therefore in particular of this power
3006 * of the prime factors.
3007 * Prime factors that appear with a smaller exponent in g cannot
3008 * be removed from m.
3009 * Let g' be the divisor of g containing all prime factors that
3010 * appear with the same exponent in m and g, then
3014 * can be replaced by
3016 * f(x) + m/g' y_i' = 0
3018 * Note that (if g' != 1) this changes the explicit representation
3019 * of y_i to that of y_i', so the integer division at position i
3020 * is marked unknown and later recomputed by a call to
3021 * isl_basic_map_gauss.
3023 static __isl_give isl_basic_map
*reduce_stride_constraints(
3024 __isl_take isl_basic_map
*bmap
, int n
, __isl_keep isl_mat
*A
)
3027 isl_size total
, n_div
;
3031 total
= isl_basic_map_dim(bmap
, isl_dim_all
);
3032 n_div
= isl_basic_map_dim(bmap
, isl_dim_div
);
3033 if (total
< 0 || n_div
< 0 || !A
)
3034 return isl_basic_map_free(bmap
);
3038 for (i
= 0; i
< n
; ++i
) {
3041 div
= isl_seq_first_non_zero(bmap
->eq
[i
] + 1 + total
, n_div
);
3043 isl_die(isl_basic_map_get_ctx(bmap
), isl_error_internal
,
3044 "equality constraints modified unexpectedly",
3046 if (isl_seq_first_non_zero(bmap
->eq
[i
] + 1 + total
+ div
+ 1,
3047 n_div
- div
- 1) != -1)
3049 if (isl_mat_row_gcd(A
, i
, &gcd
) < 0)
3051 if (isl_int_is_one(gcd
))
3053 remove_incomplete_powers(&gcd
, bmap
->eq
[i
][1 + total
+ div
]);
3054 if (isl_int_is_one(gcd
))
3056 isl_int_divexact(bmap
->eq
[i
][1 + total
+ div
],
3057 bmap
->eq
[i
][1 + total
+ div
], gcd
);
3058 bmap
= isl_basic_map_mark_div_unknown(bmap
, div
);
3066 bmap
= isl_basic_map_gauss(bmap
, NULL
);
3071 isl_basic_map_free(bmap
);
3075 /* Simplify the stride constraints in "bmap" based on
3076 * the remaining equality constraints in "bmap" and all equality
3077 * constraints in "context".
3078 * Only do this if both "bmap" and "context" have stride constraints.
3080 * First extract a copy of the stride constraints in "bmap" in a compressed
3081 * space exploiting all the other equality constraints and then
3082 * use this compressed copy to simplify the original stride constraints.
3084 static __isl_give isl_basic_map
*gist_strides(__isl_take isl_basic_map
*bmap
,
3085 __isl_keep isl_basic_map
*context
)
3087 int bmap_n_eq
, context_n_eq
;
3090 if (!bmap
|| !context
)
3091 return isl_basic_map_free(bmap
);
3093 bmap_n_eq
= n_div_eq(bmap
);
3094 context_n_eq
= n_div_eq(context
);
3096 if (bmap_n_eq
< 0 || context_n_eq
< 0)
3097 return isl_basic_map_free(bmap
);
3098 if (bmap_n_eq
== 0 || context_n_eq
== 0)
3101 A
= extract_compressed_stride_constraints(bmap
, bmap_n_eq
,
3102 context
, context_n_eq
);
3103 bmap
= reduce_stride_constraints(bmap
, bmap_n_eq
, A
);
3110 /* Return a basic map that has the same intersection with "context" as "bmap"
3111 * and that is as "simple" as possible.
3113 * The core computation is performed on the pure constraints.
3114 * When we add back the meaning of the integer divisions, we need
3115 * to (re)introduce the div constraints. If we happen to have
3116 * discovered that some of these integer divisions are equal to
3117 * some affine combination of other variables, then these div
3118 * constraints may end up getting simplified in terms of the equalities,
3119 * resulting in extra inequalities on the other variables that
3120 * may have been removed already or that may not even have been
3121 * part of the input. We try and remove those constraints of
3122 * this form that are most obviously redundant with respect to
3123 * the context. We also remove those div constraints that are
3124 * redundant with respect to the other constraints in the result.
3126 * The stride constraints among the equality constraints in "bmap" are
3127 * also simplified with respecting to the other equality constraints
3128 * in "bmap" and with respect to all equality constraints in "context".
3130 __isl_give isl_basic_map
*isl_basic_map_gist(__isl_take isl_basic_map
*bmap
,
3131 __isl_take isl_basic_map
*context
)
3133 isl_basic_set
*bset
, *eq
;
3134 isl_basic_map
*eq_bmap
;
3135 isl_size total
, n_div
, n_div_bmap
;
3136 unsigned extra
, n_eq
, n_ineq
;
3138 if (!bmap
|| !context
)
3141 if (isl_basic_map_plain_is_universe(bmap
)) {
3142 isl_basic_map_free(context
);
3145 if (isl_basic_map_plain_is_empty(context
)) {
3146 isl_space
*space
= isl_basic_map_get_space(bmap
);
3147 isl_basic_map_free(bmap
);
3148 isl_basic_map_free(context
);
3149 return isl_basic_map_universe(space
);
3151 if (isl_basic_map_plain_is_empty(bmap
)) {
3152 isl_basic_map_free(context
);
3156 bmap
= isl_basic_map_remove_redundancies(bmap
);
3157 context
= isl_basic_map_remove_redundancies(context
);
3158 context
= isl_basic_map_align_divs(context
, bmap
);
3160 n_div
= isl_basic_map_dim(context
, isl_dim_div
);
3161 total
= isl_basic_map_dim(bmap
, isl_dim_all
);
3162 n_div_bmap
= isl_basic_map_dim(bmap
, isl_dim_div
);
3163 if (n_div
< 0 || total
< 0 || n_div_bmap
< 0)
3165 extra
= n_div
- n_div_bmap
;
3167 bset
= isl_basic_map_underlying_set(isl_basic_map_copy(bmap
));
3168 bset
= isl_basic_set_add_dims(bset
, isl_dim_set
, extra
);
3169 bset
= uset_gist(bset
,
3170 isl_basic_map_underlying_set(isl_basic_map_copy(context
)));
3171 bset
= isl_basic_set_project_out(bset
, isl_dim_set
, total
, extra
);
3173 if (!bset
|| bset
->n_eq
== 0 || n_div
== 0 ||
3174 isl_basic_set_plain_is_empty(bset
)) {
3175 isl_basic_map_free(context
);
3176 return isl_basic_map_overlying_set(bset
, bmap
);
3180 n_ineq
= bset
->n_ineq
;
3181 eq
= isl_basic_set_copy(bset
);
3182 eq
= isl_basic_set_cow(eq
);
3183 eq
= isl_basic_set_free_inequality(eq
, n_ineq
);
3184 bset
= isl_basic_set_free_equality(bset
, n_eq
);
3186 eq_bmap
= isl_basic_map_overlying_set(eq
, isl_basic_map_copy(bmap
));
3187 eq_bmap
= gist_strides(eq_bmap
, context
);
3188 eq_bmap
= isl_basic_map_remove_shifted_constraints(eq_bmap
, context
);
3189 bmap
= isl_basic_map_overlying_set(bset
, bmap
);
3190 bmap
= isl_basic_map_intersect(bmap
, eq_bmap
);
3191 bmap
= isl_basic_map_remove_redundancies(bmap
);
3195 isl_basic_map_free(bmap
);
3196 isl_basic_map_free(context
);
3201 * Assumes context has no implicit divs.
3203 __isl_give isl_map
*isl_map_gist_basic_map(__isl_take isl_map
*map
,
3204 __isl_take isl_basic_map
*context
)
3208 if (!map
|| !context
)
3211 if (isl_basic_map_plain_is_empty(context
)) {
3212 isl_space
*space
= isl_map_get_space(map
);
3214 isl_basic_map_free(context
);
3215 return isl_map_universe(space
);
3218 context
= isl_basic_map_remove_redundancies(context
);
3219 map
= isl_map_cow(map
);
3220 if (!map
|| !context
)
3222 isl_assert(map
->ctx
, isl_space_is_equal(map
->dim
, context
->dim
), goto error
);
3223 map
= isl_map_compute_divs(map
);
3226 for (i
= map
->n
- 1; i
>= 0; --i
) {
3227 map
->p
[i
] = isl_basic_map_gist(map
->p
[i
],
3228 isl_basic_map_copy(context
));
3231 if (isl_basic_map_plain_is_empty(map
->p
[i
])) {
3232 isl_basic_map_free(map
->p
[i
]);
3233 if (i
!= map
->n
- 1)
3234 map
->p
[i
] = map
->p
[map
->n
- 1];
3238 isl_basic_map_free(context
);
3239 ISL_F_CLR(map
, ISL_MAP_NORMALIZED
);
3243 isl_basic_map_free(context
);
3247 /* Drop all inequalities from "bmap" that also appear in "context".
3248 * "context" is assumed to have only known local variables and
3249 * the initial local variables of "bmap" are assumed to be the same
3250 * as those of "context".
3251 * The constraints of both "bmap" and "context" are assumed
3252 * to have been sorted using isl_basic_map_sort_constraints.
3254 * Run through the inequality constraints of "bmap" and "context"
3256 * If a constraint of "bmap" involves variables not in "context",
3257 * then it cannot appear in "context".
3258 * If a matching constraint is found, it is removed from "bmap".
3260 static __isl_give isl_basic_map
*drop_inequalities(
3261 __isl_take isl_basic_map
*bmap
, __isl_keep isl_basic_map
*context
)
3264 isl_size total
, bmap_total
;
3267 total
= isl_basic_map_dim(context
, isl_dim_all
);
3268 bmap_total
= isl_basic_map_dim(bmap
, isl_dim_all
);
3269 if (total
< 0 || bmap_total
< 0)
3270 return isl_basic_map_free(bmap
);
3272 extra
= bmap_total
- total
;
3274 i1
= bmap
->n_ineq
- 1;
3275 i2
= context
->n_ineq
- 1;
3276 while (bmap
&& i1
>= 0 && i2
>= 0) {
3279 if (isl_seq_first_non_zero(bmap
->ineq
[i1
] + 1 + total
,
3284 cmp
= isl_basic_map_constraint_cmp(context
, bmap
->ineq
[i1
],
3294 if (isl_int_eq(bmap
->ineq
[i1
][0], context
->ineq
[i2
][0])) {
3295 bmap
= isl_basic_map_cow(bmap
);
3296 if (isl_basic_map_drop_inequality(bmap
, i1
) < 0)
3297 bmap
= isl_basic_map_free(bmap
);
3306 /* Drop all equalities from "bmap" that also appear in "context".
3307 * "context" is assumed to have only known local variables and
3308 * the initial local variables of "bmap" are assumed to be the same
3309 * as those of "context".
3311 * Run through the equality constraints of "bmap" and "context"
3313 * If a constraint of "bmap" involves variables not in "context",
3314 * then it cannot appear in "context".
3315 * If a matching constraint is found, it is removed from "bmap".
3317 static __isl_give isl_basic_map
*drop_equalities(
3318 __isl_take isl_basic_map
*bmap
, __isl_keep isl_basic_map
*context
)
3321 isl_size total
, bmap_total
;
3324 total
= isl_basic_map_dim(context
, isl_dim_all
);
3325 bmap_total
= isl_basic_map_dim(bmap
, isl_dim_all
);
3326 if (total
< 0 || bmap_total
< 0)
3327 return isl_basic_map_free(bmap
);
3329 extra
= bmap_total
- total
;
3331 i1
= bmap
->n_eq
- 1;
3332 i2
= context
->n_eq
- 1;
3334 while (bmap
&& i1
>= 0 && i2
>= 0) {
3337 if (isl_seq_first_non_zero(bmap
->eq
[i1
] + 1 + total
,
3340 last1
= isl_seq_last_non_zero(bmap
->eq
[i1
] + 1, total
);
3341 last2
= isl_seq_last_non_zero(context
->eq
[i2
] + 1, total
);
3342 if (last1
> last2
) {
3346 if (last1
< last2
) {
3350 if (isl_seq_eq(bmap
->eq
[i1
], context
->eq
[i2
], 1 + total
)) {
3351 bmap
= isl_basic_map_cow(bmap
);
3352 if (isl_basic_map_drop_equality(bmap
, i1
) < 0)
3353 bmap
= isl_basic_map_free(bmap
);
3362 /* Remove the constraints in "context" from "bmap".
3363 * "context" is assumed to have explicit representations
3364 * for all local variables.
3366 * First align the divs of "bmap" to those of "context" and
3367 * sort the constraints. Then drop all constraints from "bmap"
3368 * that appear in "context".
3370 __isl_give isl_basic_map
*isl_basic_map_plain_gist(
3371 __isl_take isl_basic_map
*bmap
, __isl_take isl_basic_map
*context
)
3373 isl_bool done
, known
;
3375 done
= isl_basic_map_plain_is_universe(context
);
3376 if (done
== isl_bool_false
)
3377 done
= isl_basic_map_plain_is_universe(bmap
);
3378 if (done
== isl_bool_false
)
3379 done
= isl_basic_map_plain_is_empty(context
);
3380 if (done
== isl_bool_false
)
3381 done
= isl_basic_map_plain_is_empty(bmap
);
3385 isl_basic_map_free(context
);
3388 known
= isl_basic_map_divs_known(context
);
3392 isl_die(isl_basic_map_get_ctx(bmap
), isl_error_invalid
,
3393 "context has unknown divs", goto error
);
3395 bmap
= isl_basic_map_align_divs(bmap
, context
);
3396 bmap
= isl_basic_map_gauss(bmap
, NULL
);
3397 bmap
= isl_basic_map_sort_constraints(bmap
);
3398 context
= isl_basic_map_sort_constraints(context
);
3400 bmap
= drop_inequalities(bmap
, context
);
3401 bmap
= drop_equalities(bmap
, context
);
3403 isl_basic_map_free(context
);
3404 bmap
= isl_basic_map_finalize(bmap
);
3407 isl_basic_map_free(bmap
);
3408 isl_basic_map_free(context
);
3412 /* Replace "map" by the disjunct at position "pos" and free "context".
3414 static __isl_give isl_map
*replace_by_disjunct(__isl_take isl_map
*map
,
3415 int pos
, __isl_take isl_basic_map
*context
)
3417 isl_basic_map
*bmap
;
3419 bmap
= isl_basic_map_copy(map
->p
[pos
]);
3421 isl_basic_map_free(context
);
3422 return isl_map_from_basic_map(bmap
);
3425 /* Remove the constraints in "context" from "map".
3426 * If any of the disjuncts in the result turns out to be the universe,
3427 * then return this universe.
3428 * "context" is assumed to have explicit representations
3429 * for all local variables.
3431 __isl_give isl_map
*isl_map_plain_gist_basic_map(__isl_take isl_map
*map
,
3432 __isl_take isl_basic_map
*context
)
3435 isl_bool univ
, known
;
3437 univ
= isl_basic_map_plain_is_universe(context
);
3441 isl_basic_map_free(context
);
3444 known
= isl_basic_map_divs_known(context
);
3448 isl_die(isl_map_get_ctx(map
), isl_error_invalid
,
3449 "context has unknown divs", goto error
);
3451 map
= isl_map_cow(map
);
3454 for (i
= 0; i
< map
->n
; ++i
) {
3455 map
->p
[i
] = isl_basic_map_plain_gist(map
->p
[i
],
3456 isl_basic_map_copy(context
));
3457 univ
= isl_basic_map_plain_is_universe(map
->p
[i
]);
3460 if (univ
&& map
->n
> 1)
3461 return replace_by_disjunct(map
, i
, context
);
3464 isl_basic_map_free(context
);
3465 ISL_F_CLR(map
, ISL_MAP_NORMALIZED
);
3467 ISL_F_CLR(map
, ISL_MAP_DISJOINT
);
3471 isl_basic_map_free(context
);
3475 /* Remove the constraints in "context" from "set".
3476 * If any of the disjuncts in the result turns out to be the universe,
3477 * then return this universe.
3478 * "context" is assumed to have explicit representations
3479 * for all local variables.
3481 __isl_give isl_set
*isl_set_plain_gist_basic_set(__isl_take isl_set
*set
,
3482 __isl_take isl_basic_set
*context
)
3484 return set_from_map(isl_map_plain_gist_basic_map(set_to_map(set
),
3485 bset_to_bmap(context
)));
3488 /* Remove the constraints in "context" from "map".
3489 * If any of the disjuncts in the result turns out to be the universe,
3490 * then return this universe.
3491 * "context" is assumed to consist of a single disjunct and
3492 * to have explicit representations for all local variables.
3494 __isl_give isl_map
*isl_map_plain_gist(__isl_take isl_map
*map
,
3495 __isl_take isl_map
*context
)
3497 isl_basic_map
*hull
;
3499 hull
= isl_map_unshifted_simple_hull(context
);
3500 return isl_map_plain_gist_basic_map(map
, hull
);
3503 /* Replace "map" by a universe map in the same space and free "drop".
3505 static __isl_give isl_map
*replace_by_universe(__isl_take isl_map
*map
,
3506 __isl_take isl_map
*drop
)
3510 res
= isl_map_universe(isl_map_get_space(map
));
3516 /* Return a map that has the same intersection with "context" as "map"
3517 * and that is as "simple" as possible.
3519 * If "map" is already the universe, then we cannot make it any simpler.
3520 * Similarly, if "context" is the universe, then we cannot exploit it
3522 * If "map" and "context" are identical to each other, then we can
3523 * return the corresponding universe.
3525 * If either "map" or "context" consists of multiple disjuncts,
3526 * then check if "context" happens to be a subset of "map",
3527 * in which case all constraints can be removed.
3528 * In case of multiple disjuncts, the standard procedure
3529 * may not be able to detect that all constraints can be removed.
3531 * If none of these cases apply, we have to work a bit harder.
3532 * During this computation, we make use of a single disjunct context,
3533 * so if the original context consists of more than one disjunct
3534 * then we need to approximate the context by a single disjunct set.
3535 * Simply taking the simple hull may drop constraints that are
3536 * only implicitly available in each disjunct. We therefore also
3537 * look for constraints among those defining "map" that are valid
3538 * for the context. These can then be used to simplify away
3539 * the corresponding constraints in "map".
3541 static __isl_give isl_map
*map_gist(__isl_take isl_map
*map
,
3542 __isl_take isl_map
*context
)
3546 isl_size n_disjunct_map
, n_disjunct_context
;
3548 isl_basic_map
*hull
;
3550 is_universe
= isl_map_plain_is_universe(map
);
3551 if (is_universe
>= 0 && !is_universe
)
3552 is_universe
= isl_map_plain_is_universe(context
);
3553 if (is_universe
< 0)
3556 isl_map_free(context
);
3560 equal
= isl_map_plain_is_equal(map
, context
);
3564 return replace_by_universe(map
, context
);
3566 n_disjunct_map
= isl_map_n_basic_map(map
);
3567 n_disjunct_context
= isl_map_n_basic_map(context
);
3568 if (n_disjunct_map
< 0 || n_disjunct_context
< 0)
3570 if (n_disjunct_map
!= 1 || n_disjunct_context
!= 1) {
3571 subset
= isl_map_is_subset(context
, map
);
3575 return replace_by_universe(map
, context
);
3578 context
= isl_map_compute_divs(context
);
3581 if (n_disjunct_context
== 1) {
3582 hull
= isl_map_simple_hull(context
);
3587 ctx
= isl_map_get_ctx(map
);
3588 list
= isl_map_list_alloc(ctx
, 2);
3589 list
= isl_map_list_add(list
, isl_map_copy(context
));
3590 list
= isl_map_list_add(list
, isl_map_copy(map
));
3591 hull
= isl_map_unshifted_simple_hull_from_map_list(context
,
3594 return isl_map_gist_basic_map(map
, hull
);
3597 isl_map_free(context
);
3601 __isl_give isl_map
*isl_map_gist(__isl_take isl_map
*map
,
3602 __isl_take isl_map
*context
)
3604 return isl_map_align_params_map_map_and(map
, context
, &map_gist
);
3607 struct isl_basic_set
*isl_basic_set_gist(struct isl_basic_set
*bset
,
3608 struct isl_basic_set
*context
)
3610 return bset_from_bmap(isl_basic_map_gist(bset_to_bmap(bset
),
3611 bset_to_bmap(context
)));
3614 __isl_give isl_set
*isl_set_gist_basic_set(__isl_take isl_set
*set
,
3615 __isl_take isl_basic_set
*context
)
3617 return set_from_map(isl_map_gist_basic_map(set_to_map(set
),
3618 bset_to_bmap(context
)));
3621 __isl_give isl_set
*isl_set_gist_params_basic_set(__isl_take isl_set
*set
,
3622 __isl_take isl_basic_set
*context
)
3624 isl_space
*space
= isl_set_get_space(set
);
3625 isl_basic_set
*dom_context
= isl_basic_set_universe(space
);
3626 dom_context
= isl_basic_set_intersect_params(dom_context
, context
);
3627 return isl_set_gist_basic_set(set
, dom_context
);
3630 __isl_give isl_set
*isl_set_gist(__isl_take isl_set
*set
,
3631 __isl_take isl_set
*context
)
3633 return set_from_map(isl_map_gist(set_to_map(set
), set_to_map(context
)));
3636 /* Compute the gist of "bmap" with respect to the constraints "context"
3639 __isl_give isl_basic_map
*isl_basic_map_gist_domain(
3640 __isl_take isl_basic_map
*bmap
, __isl_take isl_basic_set
*context
)
3642 isl_space
*space
= isl_basic_map_get_space(bmap
);
3643 isl_basic_map
*bmap_context
= isl_basic_map_universe(space
);
3645 bmap_context
= isl_basic_map_intersect_domain(bmap_context
, context
);
3646 return isl_basic_map_gist(bmap
, bmap_context
);
3649 __isl_give isl_map
*isl_map_gist_domain(__isl_take isl_map
*map
,
3650 __isl_take isl_set
*context
)
3652 isl_map
*map_context
= isl_map_universe(isl_map_get_space(map
));
3653 map_context
= isl_map_intersect_domain(map_context
, context
);
3654 return isl_map_gist(map
, map_context
);
3657 __isl_give isl_map
*isl_map_gist_range(__isl_take isl_map
*map
,
3658 __isl_take isl_set
*context
)
3660 isl_map
*map_context
= isl_map_universe(isl_map_get_space(map
));
3661 map_context
= isl_map_intersect_range(map_context
, context
);
3662 return isl_map_gist(map
, map_context
);
3665 __isl_give isl_map
*isl_map_gist_params(__isl_take isl_map
*map
,
3666 __isl_take isl_set
*context
)
3668 isl_map
*map_context
= isl_map_universe(isl_map_get_space(map
));
3669 map_context
= isl_map_intersect_params(map_context
, context
);
3670 return isl_map_gist(map
, map_context
);
3673 __isl_give isl_set
*isl_set_gist_params(__isl_take isl_set
*set
,
3674 __isl_take isl_set
*context
)
3676 return isl_map_gist_params(set
, context
);
3679 /* Quick check to see if two basic maps are disjoint.
3680 * In particular, we reduce the equalities and inequalities of
3681 * one basic map in the context of the equalities of the other
3682 * basic map and check if we get a contradiction.
3684 isl_bool
isl_basic_map_plain_is_disjoint(__isl_keep isl_basic_map
*bmap1
,
3685 __isl_keep isl_basic_map
*bmap2
)
3687 struct isl_vec
*v
= NULL
;
3692 if (!bmap1
|| !bmap2
)
3693 return isl_bool_error
;
3694 isl_assert(bmap1
->ctx
, isl_space_is_equal(bmap1
->dim
, bmap2
->dim
),
3695 return isl_bool_error
);
3696 if (bmap1
->n_div
|| bmap2
->n_div
)
3697 return isl_bool_false
;
3698 if (!bmap1
->n_eq
&& !bmap2
->n_eq
)
3699 return isl_bool_false
;
3701 total
= isl_space_dim(bmap1
->dim
, isl_dim_all
);
3703 return isl_bool_error
;
3705 return isl_bool_false
;
3706 v
= isl_vec_alloc(bmap1
->ctx
, 1 + total
);
3709 elim
= isl_alloc_array(bmap1
->ctx
, int, total
);
3712 compute_elimination_index(bmap1
, elim
);
3713 for (i
= 0; i
< bmap2
->n_eq
; ++i
) {
3715 reduced
= reduced_using_equalities(v
->block
.data
, bmap2
->eq
[i
],
3717 if (reduced
&& !isl_int_is_zero(v
->block
.data
[0]) &&
3718 isl_seq_first_non_zero(v
->block
.data
+ 1, total
) == -1)
3721 for (i
= 0; i
< bmap2
->n_ineq
; ++i
) {
3723 reduced
= reduced_using_equalities(v
->block
.data
,
3724 bmap2
->ineq
[i
], bmap1
, elim
);
3725 if (reduced
&& isl_int_is_neg(v
->block
.data
[0]) &&
3726 isl_seq_first_non_zero(v
->block
.data
+ 1, total
) == -1)
3729 compute_elimination_index(bmap2
, elim
);
3730 for (i
= 0; i
< bmap1
->n_ineq
; ++i
) {
3732 reduced
= reduced_using_equalities(v
->block
.data
,
3733 bmap1
->ineq
[i
], bmap2
, elim
);
3734 if (reduced
&& isl_int_is_neg(v
->block
.data
[0]) &&
3735 isl_seq_first_non_zero(v
->block
.data
+ 1, total
) == -1)
3740 return isl_bool_false
;
3744 return isl_bool_true
;
3748 return isl_bool_error
;
3751 int isl_basic_set_plain_is_disjoint(__isl_keep isl_basic_set
*bset1
,
3752 __isl_keep isl_basic_set
*bset2
)
3754 return isl_basic_map_plain_is_disjoint(bset_to_bmap(bset1
),
3755 bset_to_bmap(bset2
));
3758 /* Does "test" hold for all pairs of basic maps in "map1" and "map2"?
3760 static isl_bool
all_pairs(__isl_keep isl_map
*map1
, __isl_keep isl_map
*map2
,
3761 isl_bool (*test
)(__isl_keep isl_basic_map
*bmap1
,
3762 __isl_keep isl_basic_map
*bmap2
))
3767 return isl_bool_error
;
3769 for (i
= 0; i
< map1
->n
; ++i
) {
3770 for (j
= 0; j
< map2
->n
; ++j
) {
3771 isl_bool d
= test(map1
->p
[i
], map2
->p
[j
]);
3772 if (d
!= isl_bool_true
)
3777 return isl_bool_true
;
3780 /* Are "map1" and "map2" obviously disjoint, based on information
3781 * that can be derived without looking at the individual basic maps?
3783 * In particular, if one of them is empty or if they live in different spaces
3784 * (ignoring parameters), then they are clearly disjoint.
3786 static isl_bool
isl_map_plain_is_disjoint_global(__isl_keep isl_map
*map1
,
3787 __isl_keep isl_map
*map2
)
3793 return isl_bool_error
;
3795 disjoint
= isl_map_plain_is_empty(map1
);
3796 if (disjoint
< 0 || disjoint
)
3799 disjoint
= isl_map_plain_is_empty(map2
);
3800 if (disjoint
< 0 || disjoint
)
3803 match
= isl_space_tuple_is_equal(map1
->dim
, isl_dim_in
,
3804 map2
->dim
, isl_dim_in
);
3805 if (match
< 0 || !match
)
3806 return match
< 0 ? isl_bool_error
: isl_bool_true
;
3808 match
= isl_space_tuple_is_equal(map1
->dim
, isl_dim_out
,
3809 map2
->dim
, isl_dim_out
);
3810 if (match
< 0 || !match
)
3811 return match
< 0 ? isl_bool_error
: isl_bool_true
;
3813 return isl_bool_false
;
3816 /* Are "map1" and "map2" obviously disjoint?
3818 * If one of them is empty or if they live in different spaces (ignoring
3819 * parameters), then they are clearly disjoint.
3820 * This is checked by isl_map_plain_is_disjoint_global.
3822 * If they have different parameters, then we skip any further tests.
3824 * If they are obviously equal, but not obviously empty, then we will
3825 * not be able to detect if they are disjoint.
3827 * Otherwise we check if each basic map in "map1" is obviously disjoint
3828 * from each basic map in "map2".
3830 isl_bool
isl_map_plain_is_disjoint(__isl_keep isl_map
*map1
,
3831 __isl_keep isl_map
*map2
)
3837 disjoint
= isl_map_plain_is_disjoint_global(map1
, map2
);
3838 if (disjoint
< 0 || disjoint
)
3841 match
= isl_map_has_equal_params(map1
, map2
);
3842 if (match
< 0 || !match
)
3843 return match
< 0 ? isl_bool_error
: isl_bool_false
;
3845 intersect
= isl_map_plain_is_equal(map1
, map2
);
3846 if (intersect
< 0 || intersect
)
3847 return intersect
< 0 ? isl_bool_error
: isl_bool_false
;
3849 return all_pairs(map1
, map2
, &isl_basic_map_plain_is_disjoint
);
3852 /* Are "map1" and "map2" disjoint?
3853 * The parameters are assumed to have been aligned.
3855 * In particular, check whether all pairs of basic maps are disjoint.
3857 static isl_bool
isl_map_is_disjoint_aligned(__isl_keep isl_map
*map1
,
3858 __isl_keep isl_map
*map2
)
3860 return all_pairs(map1
, map2
, &isl_basic_map_is_disjoint
);
3863 /* Are "map1" and "map2" disjoint?
3865 * They are disjoint if they are "obviously disjoint" or if one of them
3866 * is empty. Otherwise, they are not disjoint if one of them is universal.
3867 * If the two inputs are (obviously) equal and not empty, then they are
3869 * If none of these cases apply, then check if all pairs of basic maps
3870 * are disjoint after aligning the parameters.
3872 isl_bool
isl_map_is_disjoint(__isl_keep isl_map
*map1
, __isl_keep isl_map
*map2
)
3877 disjoint
= isl_map_plain_is_disjoint_global(map1
, map2
);
3878 if (disjoint
< 0 || disjoint
)
3881 disjoint
= isl_map_is_empty(map1
);
3882 if (disjoint
< 0 || disjoint
)
3885 disjoint
= isl_map_is_empty(map2
);
3886 if (disjoint
< 0 || disjoint
)
3889 intersect
= isl_map_plain_is_universe(map1
);
3890 if (intersect
< 0 || intersect
)
3891 return intersect
< 0 ? isl_bool_error
: isl_bool_false
;
3893 intersect
= isl_map_plain_is_universe(map2
);
3894 if (intersect
< 0 || intersect
)
3895 return intersect
< 0 ? isl_bool_error
: isl_bool_false
;
3897 intersect
= isl_map_plain_is_equal(map1
, map2
);
3898 if (intersect
< 0 || intersect
)
3899 return isl_bool_not(intersect
);
3901 return isl_map_align_params_map_map_and_test(map1
, map2
,
3902 &isl_map_is_disjoint_aligned
);
3905 /* Are "bmap1" and "bmap2" disjoint?
3907 * They are disjoint if they are "obviously disjoint" or if one of them
3908 * is empty. Otherwise, they are not disjoint if one of them is universal.
3909 * If none of these cases apply, we compute the intersection and see if
3910 * the result is empty.
3912 isl_bool
isl_basic_map_is_disjoint(__isl_keep isl_basic_map
*bmap1
,
3913 __isl_keep isl_basic_map
*bmap2
)
3917 isl_basic_map
*test
;
3919 disjoint
= isl_basic_map_plain_is_disjoint(bmap1
, bmap2
);
3920 if (disjoint
< 0 || disjoint
)
3923 disjoint
= isl_basic_map_is_empty(bmap1
);
3924 if (disjoint
< 0 || disjoint
)
3927 disjoint
= isl_basic_map_is_empty(bmap2
);
3928 if (disjoint
< 0 || disjoint
)
3931 intersect
= isl_basic_map_plain_is_universe(bmap1
);
3932 if (intersect
< 0 || intersect
)
3933 return intersect
< 0 ? isl_bool_error
: isl_bool_false
;
3935 intersect
= isl_basic_map_plain_is_universe(bmap2
);
3936 if (intersect
< 0 || intersect
)
3937 return intersect
< 0 ? isl_bool_error
: isl_bool_false
;
3939 test
= isl_basic_map_intersect(isl_basic_map_copy(bmap1
),
3940 isl_basic_map_copy(bmap2
));
3941 disjoint
= isl_basic_map_is_empty(test
);
3942 isl_basic_map_free(test
);
3947 /* Are "bset1" and "bset2" disjoint?
3949 isl_bool
isl_basic_set_is_disjoint(__isl_keep isl_basic_set
*bset1
,
3950 __isl_keep isl_basic_set
*bset2
)
3952 return isl_basic_map_is_disjoint(bset1
, bset2
);
3955 isl_bool
isl_set_plain_is_disjoint(__isl_keep isl_set
*set1
,
3956 __isl_keep isl_set
*set2
)
3958 return isl_map_plain_is_disjoint(set_to_map(set1
), set_to_map(set2
));
3961 /* Are "set1" and "set2" disjoint?
3963 isl_bool
isl_set_is_disjoint(__isl_keep isl_set
*set1
, __isl_keep isl_set
*set2
)
3965 return isl_map_is_disjoint(set1
, set2
);
3968 /* Is "v" equal to 0, 1 or -1?
3970 static int is_zero_or_one(isl_int v
)
3972 return isl_int_is_zero(v
) || isl_int_is_one(v
) || isl_int_is_negone(v
);
3975 /* Are the "n" coefficients starting at "first" of inequality constraints
3976 * "i" and "j" of "bmap" opposite to each other?
3978 static int is_opposite_part(__isl_keep isl_basic_map
*bmap
, int i
, int j
,
3981 return isl_seq_is_neg(bmap
->ineq
[i
] + first
, bmap
->ineq
[j
] + first
, n
);
3984 /* Are inequality constraints "i" and "j" of "bmap" opposite to each other,
3985 * apart from the constant term?
3987 static isl_bool
is_opposite(__isl_keep isl_basic_map
*bmap
, int i
, int j
)
3991 total
= isl_basic_map_dim(bmap
, isl_dim_all
);
3993 return isl_bool_error
;
3994 return is_opposite_part(bmap
, i
, j
, 1, total
);
3997 /* Check if we can combine a given div with lower bound l and upper
3998 * bound u with some other div and if so return that other div.
3999 * Otherwise, return a position beyond the integer divisions.
4000 * Return -1 on error.
4002 * We first check that
4003 * - the bounds are opposites of each other (except for the constant
4005 * - the bounds do not reference any other div
4006 * - no div is defined in terms of this div
4008 * Let m be the size of the range allowed on the div by the bounds.
4009 * That is, the bounds are of the form
4011 * e <= a <= e + m - 1
4013 * with e some expression in the other variables.
4014 * We look for another div b such that no third div is defined in terms
4015 * of this second div b and such that in any constraint that contains
4016 * a (except for the given lower and upper bound), also contains b
4017 * with a coefficient that is m times that of b.
4018 * That is, all constraints (except for the lower and upper bound)
4021 * e + f (a + m b) >= 0
4023 * Furthermore, in the constraints that only contain b, the coefficient
4024 * of b should be equal to 1 or -1.
4025 * If so, we return b so that "a + m b" can be replaced by
4026 * a single div "c = a + m b".
4028 static int div_find_coalesce(__isl_keep isl_basic_map
*bmap
, int *pairs
,
4029 unsigned div
, unsigned l
, unsigned u
)
4037 n_div
= isl_basic_map_dim(bmap
, isl_dim_div
);
4040 v_div
= isl_basic_map_var_offset(bmap
, isl_dim_div
);
4043 if (isl_seq_first_non_zero(bmap
->ineq
[l
] + 1 + v_div
, div
) != -1)
4045 if (isl_seq_first_non_zero(bmap
->ineq
[l
] + 1 + v_div
+ div
+ 1,
4046 n_div
- div
- 1) != -1)
4048 opp
= is_opposite(bmap
, l
, u
);
4049 if (opp
< 0 || !opp
)
4050 return opp
< 0 ? -1 : n_div
;
4052 for (i
= 0; i
< n_div
; ++i
) {
4053 if (isl_int_is_zero(bmap
->div
[i
][0]))
4055 if (!isl_int_is_zero(bmap
->div
[i
][1 + 1 + v_div
+ div
]))
4059 isl_int_add(bmap
->ineq
[l
][0], bmap
->ineq
[l
][0], bmap
->ineq
[u
][0]);
4060 if (isl_int_is_neg(bmap
->ineq
[l
][0])) {
4061 isl_int_sub(bmap
->ineq
[l
][0],
4062 bmap
->ineq
[l
][0], bmap
->ineq
[u
][0]);
4063 bmap
= isl_basic_map_copy(bmap
);
4064 bmap
= isl_basic_map_set_to_empty(bmap
);
4065 isl_basic_map_free(bmap
);
4068 isl_int_add_ui(bmap
->ineq
[l
][0], bmap
->ineq
[l
][0], 1);
4070 for (i
= 0; i
< n_div
; ++i
) {
4075 for (j
= 0; j
< n_div
; ++j
) {
4076 if (isl_int_is_zero(bmap
->div
[j
][0]))
4078 if (!isl_int_is_zero(bmap
->div
[j
][1 + 1 + v_div
+ i
]))
4083 for (j
= 0; j
< bmap
->n_ineq
; ++j
) {
4085 if (j
== l
|| j
== u
)
4087 if (isl_int_is_zero(bmap
->ineq
[j
][1 + v_div
+ div
])) {
4088 if (is_zero_or_one(bmap
->ineq
[j
][1 + v_div
+ i
]))
4092 if (isl_int_is_zero(bmap
->ineq
[j
][1 + v_div
+ i
]))
4094 isl_int_mul(bmap
->ineq
[j
][1 + v_div
+ div
],
4095 bmap
->ineq
[j
][1 + v_div
+ div
],
4097 valid
= isl_int_eq(bmap
->ineq
[j
][1 + v_div
+ div
],
4098 bmap
->ineq
[j
][1 + v_div
+ i
]);
4099 isl_int_divexact(bmap
->ineq
[j
][1 + v_div
+ div
],
4100 bmap
->ineq
[j
][1 + v_div
+ div
],
4105 if (j
< bmap
->n_ineq
)
4110 isl_int_sub_ui(bmap
->ineq
[l
][0], bmap
->ineq
[l
][0], 1);
4111 isl_int_sub(bmap
->ineq
[l
][0], bmap
->ineq
[l
][0], bmap
->ineq
[u
][0]);
4115 /* Internal data structure used during the construction and/or evaluation of
4116 * an inequality that ensures that a pair of bounds always allows
4117 * for an integer value.
4119 * "tab" is the tableau in which the inequality is evaluated. It may
4120 * be NULL until it is actually needed.
4121 * "v" contains the inequality coefficients.
4122 * "g", "fl" and "fu" are temporary scalars used during the construction and
4125 struct test_ineq_data
{
4126 struct isl_tab
*tab
;
4133 /* Free all the memory allocated by the fields of "data".
4135 static void test_ineq_data_clear(struct test_ineq_data
*data
)
4137 isl_tab_free(data
->tab
);
4138 isl_vec_free(data
->v
);
4139 isl_int_clear(data
->g
);
4140 isl_int_clear(data
->fl
);
4141 isl_int_clear(data
->fu
);
4144 /* Is the inequality stored in data->v satisfied by "bmap"?
4145 * That is, does it only attain non-negative values?
4146 * data->tab is a tableau corresponding to "bmap".
4148 static isl_bool
test_ineq_is_satisfied(__isl_keep isl_basic_map
*bmap
,
4149 struct test_ineq_data
*data
)
4152 enum isl_lp_result res
;
4154 ctx
= isl_basic_map_get_ctx(bmap
);
4156 data
->tab
= isl_tab_from_basic_map(bmap
, 0);
4157 res
= isl_tab_min(data
->tab
, data
->v
->el
, ctx
->one
, &data
->g
, NULL
, 0);
4158 if (res
== isl_lp_error
)
4159 return isl_bool_error
;
4160 return res
== isl_lp_ok
&& isl_int_is_nonneg(data
->g
);
4163 /* Given a lower and an upper bound on div i, do they always allow
4164 * for an integer value of the given div?
4165 * Determine this property by constructing an inequality
4166 * such that the property is guaranteed when the inequality is nonnegative.
4167 * The lower bound is inequality l, while the upper bound is inequality u.
4168 * The constructed inequality is stored in data->v.
4170 * Let the upper bound be
4174 * and the lower bound
4178 * Let n_u = f_u g and n_l = f_l g, with g = gcd(n_u, n_l).
4181 * - f_u e_l <= f_u f_l g a <= f_l e_u
4183 * Since all variables are integer valued, this is equivalent to
4185 * - f_u e_l - (f_u - 1) <= f_u f_l g a <= f_l e_u + (f_l - 1)
4187 * If this interval is at least f_u f_l g, then it contains at least
4188 * one integer value for a.
4189 * That is, the test constraint is
4191 * f_l e_u + f_u e_l + f_l - 1 + f_u - 1 + 1 >= f_u f_l g
4195 * f_l e_u + f_u e_l + f_l - 1 + f_u - 1 + 1 - f_u f_l g >= 0
4197 * If the coefficients of f_l e_u + f_u e_l have a common divisor g',
4198 * then the constraint can be scaled down by a factor g',
4199 * with the constant term replaced by
4200 * floor((f_l e_{u,0} + f_u e_{l,0} + f_l - 1 + f_u - 1 + 1 - f_u f_l g)/g').
4201 * Note that the result of applying Fourier-Motzkin to this pair
4204 * f_l e_u + f_u e_l >= 0
4206 * If the constant term of the scaled down version of this constraint,
4207 * i.e., floor((f_l e_{u,0} + f_u e_{l,0})/g') is equal to the constant
4208 * term of the scaled down test constraint, then the test constraint
4209 * is known to hold and no explicit evaluation is required.
4210 * This is essentially the Omega test.
4212 * If the test constraint consists of only a constant term, then
4213 * it is sufficient to look at the sign of this constant term.
4215 static isl_bool
int_between_bounds(__isl_keep isl_basic_map
*bmap
, int i
,
4216 int l
, int u
, struct test_ineq_data
*data
)
4221 offset
= isl_basic_map_offset(bmap
, isl_dim_div
);
4222 n_div
= isl_basic_map_dim(bmap
, isl_dim_div
);
4224 return isl_bool_error
;
4226 isl_int_gcd(data
->g
,
4227 bmap
->ineq
[l
][offset
+ i
], bmap
->ineq
[u
][offset
+ i
]);
4228 isl_int_divexact(data
->fl
, bmap
->ineq
[l
][offset
+ i
], data
->g
);
4229 isl_int_divexact(data
->fu
, bmap
->ineq
[u
][offset
+ i
], data
->g
);
4230 isl_int_neg(data
->fu
, data
->fu
);
4231 isl_seq_combine(data
->v
->el
, data
->fl
, bmap
->ineq
[u
],
4232 data
->fu
, bmap
->ineq
[l
], offset
+ n_div
);
4233 isl_int_mul(data
->g
, data
->g
, data
->fl
);
4234 isl_int_mul(data
->g
, data
->g
, data
->fu
);
4235 isl_int_sub(data
->g
, data
->g
, data
->fl
);
4236 isl_int_sub(data
->g
, data
->g
, data
->fu
);
4237 isl_int_add_ui(data
->g
, data
->g
, 1);
4238 isl_int_sub(data
->fl
, data
->v
->el
[0], data
->g
);
4240 isl_seq_gcd(data
->v
->el
+ 1, offset
- 1 + n_div
, &data
->g
);
4241 if (isl_int_is_zero(data
->g
))
4242 return isl_int_is_nonneg(data
->fl
);
4243 if (isl_int_is_one(data
->g
)) {
4244 isl_int_set(data
->v
->el
[0], data
->fl
);
4245 return test_ineq_is_satisfied(bmap
, data
);
4247 isl_int_fdiv_q(data
->fl
, data
->fl
, data
->g
);
4248 isl_int_fdiv_q(data
->v
->el
[0], data
->v
->el
[0], data
->g
);
4249 if (isl_int_eq(data
->fl
, data
->v
->el
[0]))
4250 return isl_bool_true
;
4251 isl_int_set(data
->v
->el
[0], data
->fl
);
4252 isl_seq_scale_down(data
->v
->el
+ 1, data
->v
->el
+ 1, data
->g
,
4253 offset
- 1 + n_div
);
4255 return test_ineq_is_satisfied(bmap
, data
);
4258 /* Remove more kinds of divs that are not strictly needed.
4259 * In particular, if all pairs of lower and upper bounds on a div
4260 * are such that they allow at least one integer value of the div,
4261 * then we can eliminate the div using Fourier-Motzkin without
4262 * introducing any spurious solutions.
4264 * If at least one of the two constraints has a unit coefficient for the div,
4265 * then the presence of such a value is guaranteed so there is no need to check.
4266 * In particular, the value attained by the bound with unit coefficient
4267 * can serve as this intermediate value.
4269 static __isl_give isl_basic_map
*drop_more_redundant_divs(
4270 __isl_take isl_basic_map
*bmap
, __isl_take
int *pairs
, int n
)
4273 struct test_ineq_data data
= { NULL
, NULL
};
4278 isl_int_init(data
.g
);
4279 isl_int_init(data
.fl
);
4280 isl_int_init(data
.fu
);
4282 n_div
= isl_basic_map_dim(bmap
, isl_dim_div
);
4286 ctx
= isl_basic_map_get_ctx(bmap
);
4287 off
= isl_basic_map_offset(bmap
, isl_dim_div
);
4288 data
.v
= isl_vec_alloc(ctx
, off
+ n_div
);
4297 for (i
= 0; i
< n_div
; ++i
) {
4300 if (best
>= 0 && pairs
[best
] <= pairs
[i
])
4306 for (l
= 0; l
< bmap
->n_ineq
; ++l
) {
4307 if (!isl_int_is_pos(bmap
->ineq
[l
][off
+ i
]))
4309 if (isl_int_is_one(bmap
->ineq
[l
][off
+ i
]))
4311 for (u
= 0; u
< bmap
->n_ineq
; ++u
) {
4312 if (!isl_int_is_neg(bmap
->ineq
[u
][off
+ i
]))
4314 if (isl_int_is_negone(bmap
->ineq
[u
][off
+ i
]))
4316 has_int
= int_between_bounds(bmap
, i
, l
, u
,
4320 if (data
.tab
&& data
.tab
->empty
)
4325 if (u
< bmap
->n_ineq
)
4328 if (data
.tab
&& data
.tab
->empty
) {
4329 bmap
= isl_basic_map_set_to_empty(bmap
);
4332 if (l
== bmap
->n_ineq
) {
4340 test_ineq_data_clear(&data
);
4347 bmap
= isl_basic_map_remove_dims(bmap
, isl_dim_div
, remove
, 1);
4348 return isl_basic_map_drop_redundant_divs(bmap
);
4351 isl_basic_map_free(bmap
);
4352 test_ineq_data_clear(&data
);
4356 /* Given a pair of divs div1 and div2 such that, except for the lower bound l
4357 * and the upper bound u, div1 always occurs together with div2 in the form
4358 * (div1 + m div2), where m is the constant range on the variable div1
4359 * allowed by l and u, replace the pair div1 and div2 by a single
4360 * div that is equal to div1 + m div2.
4362 * The new div will appear in the location that contains div2.
4363 * We need to modify all constraints that contain
4364 * div2 = (div - div1) / m
4365 * The coefficient of div2 is known to be equal to 1 or -1.
4366 * (If a constraint does not contain div2, it will also not contain div1.)
4367 * If the constraint also contains div1, then we know they appear
4368 * as f (div1 + m div2) and we can simply replace (div1 + m div2) by div,
4369 * i.e., the coefficient of div is f.
4371 * Otherwise, we first need to introduce div1 into the constraint.
4380 * A lower bound on div2
4384 * can be replaced by
4386 * m div2 + div1 + m t + f >= 0
4392 * can be replaced by
4394 * -(m div2 + div1) + m t + f' >= 0
4396 * These constraint are those that we would obtain from eliminating
4397 * div1 using Fourier-Motzkin.
4399 * After all constraints have been modified, we drop the lower and upper
4400 * bound and then drop div1.
4401 * Since the new div is only placed in the same location that used
4402 * to store div2, but otherwise has a different meaning, any possible
4403 * explicit representation of the original div2 is removed.
4405 static __isl_give isl_basic_map
*coalesce_divs(__isl_take isl_basic_map
*bmap
,
4406 unsigned div1
, unsigned div2
, unsigned l
, unsigned u
)
4414 ctx
= isl_basic_map_get_ctx(bmap
);
4416 v_div
= isl_basic_map_var_offset(bmap
, isl_dim_div
);
4418 return isl_basic_map_free(bmap
);
4419 total
= 1 + v_div
+ bmap
->n_div
;
4422 isl_int_add(m
, bmap
->ineq
[l
][0], bmap
->ineq
[u
][0]);
4423 isl_int_add_ui(m
, m
, 1);
4425 for (i
= 0; i
< bmap
->n_ineq
; ++i
) {
4426 if (i
== l
|| i
== u
)
4428 if (isl_int_is_zero(bmap
->ineq
[i
][1 + v_div
+ div2
]))
4430 if (isl_int_is_zero(bmap
->ineq
[i
][1 + v_div
+ div1
])) {
4431 if (isl_int_is_pos(bmap
->ineq
[i
][1 + v_div
+ div2
]))
4432 isl_seq_combine(bmap
->ineq
[i
], m
, bmap
->ineq
[i
],
4433 ctx
->one
, bmap
->ineq
[l
], total
);
4435 isl_seq_combine(bmap
->ineq
[i
], m
, bmap
->ineq
[i
],
4436 ctx
->one
, bmap
->ineq
[u
], total
);
4438 isl_int_set(bmap
->ineq
[i
][1 + v_div
+ div2
],
4439 bmap
->ineq
[i
][1 + v_div
+ div1
]);
4440 isl_int_set_si(bmap
->ineq
[i
][1 + v_div
+ div1
], 0);
4445 isl_basic_map_drop_inequality(bmap
, l
);
4446 isl_basic_map_drop_inequality(bmap
, u
);
4448 isl_basic_map_drop_inequality(bmap
, u
);
4449 isl_basic_map_drop_inequality(bmap
, l
);
4451 bmap
= isl_basic_map_mark_div_unknown(bmap
, div2
);
4452 bmap
= isl_basic_map_drop_div(bmap
, div1
);
4456 /* First check if we can coalesce any pair of divs and
4457 * then continue with dropping more redundant divs.
4459 * We loop over all pairs of lower and upper bounds on a div
4460 * with coefficient 1 and -1, respectively, check if there
4461 * is any other div "c" with which we can coalesce the div
4462 * and if so, perform the coalescing.
4464 static __isl_give isl_basic_map
*coalesce_or_drop_more_redundant_divs(
4465 __isl_take isl_basic_map
*bmap
, int *pairs
, int n
)
4471 v_div
= isl_basic_map_var_offset(bmap
, isl_dim_div
);
4472 n_div
= isl_basic_map_dim(bmap
, isl_dim_div
);
4473 if (v_div
< 0 || n_div
< 0)
4474 return isl_basic_map_free(bmap
);
4476 for (i
= 0; i
< n_div
; ++i
) {
4479 for (l
= 0; l
< bmap
->n_ineq
; ++l
) {
4480 if (!isl_int_is_one(bmap
->ineq
[l
][1 + v_div
+ i
]))
4482 for (u
= 0; u
< bmap
->n_ineq
; ++u
) {
4485 if (!isl_int_is_negone(bmap
->ineq
[u
][1+v_div
+i
]))
4487 c
= div_find_coalesce(bmap
, pairs
, i
, l
, u
);
4493 bmap
= coalesce_divs(bmap
, i
, c
, l
, u
);
4494 return isl_basic_map_drop_redundant_divs(bmap
);
4499 if (ISL_F_ISSET(bmap
, ISL_BASIC_MAP_EMPTY
)) {
4504 return drop_more_redundant_divs(bmap
, pairs
, n
);
4507 isl_basic_map_free(bmap
);
4511 /* Are the "n" coefficients starting at "first" of inequality constraints
4512 * "i" and "j" of "bmap" equal to each other?
4514 static int is_parallel_part(__isl_keep isl_basic_map
*bmap
, int i
, int j
,
4517 return isl_seq_eq(bmap
->ineq
[i
] + first
, bmap
->ineq
[j
] + first
, n
);
4520 /* Are inequality constraints "i" and "j" of "bmap" equal to each other,
4521 * apart from the constant term and the coefficient at position "pos"?
4523 static isl_bool
is_parallel_except(__isl_keep isl_basic_map
*bmap
, int i
, int j
,
4528 total
= isl_basic_map_dim(bmap
, isl_dim_all
);
4530 return isl_bool_error
;
4531 return is_parallel_part(bmap
, i
, j
, 1, pos
- 1) &&
4532 is_parallel_part(bmap
, i
, j
, pos
+ 1, total
- pos
);
4535 /* Are inequality constraints "i" and "j" of "bmap" opposite to each other,
4536 * apart from the constant term and the coefficient at position "pos"?
4538 static isl_bool
is_opposite_except(__isl_keep isl_basic_map
*bmap
, int i
, int j
,
4543 total
= isl_basic_map_dim(bmap
, isl_dim_all
);
4545 return isl_bool_error
;
4546 return is_opposite_part(bmap
, i
, j
, 1, pos
- 1) &&
4547 is_opposite_part(bmap
, i
, j
, pos
+ 1, total
- pos
);
4550 /* Restart isl_basic_map_drop_redundant_divs after "bmap" has
4551 * been modified, simplying it if "simplify" is set.
4552 * Free the temporary data structure "pairs" that was associated
4553 * to the old version of "bmap".
4555 static __isl_give isl_basic_map
*drop_redundant_divs_again(
4556 __isl_take isl_basic_map
*bmap
, __isl_take
int *pairs
, int simplify
)
4559 bmap
= isl_basic_map_simplify(bmap
);
4561 return isl_basic_map_drop_redundant_divs(bmap
);
4564 /* Is "div" the single unknown existentially quantified variable
4565 * in inequality constraint "ineq" of "bmap"?
4566 * "div" is known to have a non-zero coefficient in "ineq".
4568 static isl_bool
single_unknown(__isl_keep isl_basic_map
*bmap
, int ineq
,
4576 known
= isl_basic_map_div_is_known(bmap
, div
);
4577 if (known
< 0 || known
)
4578 return isl_bool_not(known
);
4579 n_div
= isl_basic_map_dim(bmap
, isl_dim_div
);
4581 return isl_bool_error
;
4583 return isl_bool_true
;
4584 o_div
= isl_basic_map_offset(bmap
, isl_dim_div
);
4585 for (i
= 0; i
< n_div
; ++i
) {
4590 if (isl_int_is_zero(bmap
->ineq
[ineq
][o_div
+ i
]))
4592 known
= isl_basic_map_div_is_known(bmap
, i
);
4593 if (known
< 0 || !known
)
4597 return isl_bool_true
;
4600 /* Does integer division "div" have coefficient 1 in inequality constraint
4603 static isl_bool
has_coef_one(__isl_keep isl_basic_map
*bmap
, int div
, int ineq
)
4607 o_div
= isl_basic_map_offset(bmap
, isl_dim_div
);
4608 if (isl_int_is_one(bmap
->ineq
[ineq
][o_div
+ div
]))
4609 return isl_bool_true
;
4611 return isl_bool_false
;
4614 /* Turn inequality constraint "ineq" of "bmap" into an equality and
4615 * then try and drop redundant divs again,
4616 * freeing the temporary data structure "pairs" that was associated
4617 * to the old version of "bmap".
4619 static __isl_give isl_basic_map
*set_eq_and_try_again(
4620 __isl_take isl_basic_map
*bmap
, int ineq
, __isl_take
int *pairs
)
4622 bmap
= isl_basic_map_cow(bmap
);
4623 isl_basic_map_inequality_to_equality(bmap
, ineq
);
4624 return drop_redundant_divs_again(bmap
, pairs
, 1);
4627 /* Drop the integer division at position "div", along with the two
4628 * inequality constraints "ineq1" and "ineq2" in which it appears
4629 * from "bmap" and then try and drop redundant divs again,
4630 * freeing the temporary data structure "pairs" that was associated
4631 * to the old version of "bmap".
4633 static __isl_give isl_basic_map
*drop_div_and_try_again(
4634 __isl_take isl_basic_map
*bmap
, int div
, int ineq1
, int ineq2
,
4635 __isl_take
int *pairs
)
4637 if (ineq1
> ineq2
) {
4638 isl_basic_map_drop_inequality(bmap
, ineq1
);
4639 isl_basic_map_drop_inequality(bmap
, ineq2
);
4641 isl_basic_map_drop_inequality(bmap
, ineq2
);
4642 isl_basic_map_drop_inequality(bmap
, ineq1
);
4644 bmap
= isl_basic_map_drop_div(bmap
, div
);
4645 return drop_redundant_divs_again(bmap
, pairs
, 0);
4648 /* Given two inequality constraints
4650 * f(x) + n d + c >= 0, (ineq)
4652 * with d the variable at position "pos", and
4654 * f(x) + c0 >= 0, (lower)
4656 * compute the maximal value of the lower bound ceil((-f(x) - c)/n)
4657 * determined by the first constraint.
4664 static void lower_bound_from_parallel(__isl_keep isl_basic_map
*bmap
,
4665 int ineq
, int lower
, int pos
, isl_int
*l
)
4667 isl_int_neg(*l
, bmap
->ineq
[ineq
][0]);
4668 isl_int_add(*l
, *l
, bmap
->ineq
[lower
][0]);
4669 isl_int_cdiv_q(*l
, *l
, bmap
->ineq
[ineq
][pos
]);
4672 /* Given two inequality constraints
4674 * f(x) + n d + c >= 0, (ineq)
4676 * with d the variable at position "pos", and
4678 * -f(x) - c0 >= 0, (upper)
4680 * compute the minimal value of the lower bound ceil((-f(x) - c)/n)
4681 * determined by the first constraint.
4688 static void lower_bound_from_opposite(__isl_keep isl_basic_map
*bmap
,
4689 int ineq
, int upper
, int pos
, isl_int
*u
)
4691 isl_int_neg(*u
, bmap
->ineq
[ineq
][0]);
4692 isl_int_sub(*u
, *u
, bmap
->ineq
[upper
][0]);
4693 isl_int_cdiv_q(*u
, *u
, bmap
->ineq
[ineq
][pos
]);
4696 /* Given a lower bound constraint "ineq" on "div" in "bmap",
4697 * does the corresponding lower bound have a fixed value in "bmap"?
4699 * In particular, "ineq" is of the form
4701 * f(x) + n d + c >= 0
4703 * with n > 0, c the constant term and
4704 * d the existentially quantified variable "div".
4705 * That is, the lower bound is
4707 * ceil((-f(x) - c)/n)
4709 * Look for a pair of constraints
4714 * i.e., -c1 <= -f(x) <= c0, that fix ceil((-f(x) - c)/n) to a constant value.
4715 * That is, check that
4717 * ceil((-c1 - c)/n) = ceil((c0 - c)/n)
4719 * If so, return the index of inequality f(x) + c0 >= 0.
4720 * Otherwise, return bmap->n_ineq.
4721 * Return -1 on error.
4723 static int lower_bound_is_cst(__isl_keep isl_basic_map
*bmap
, int div
, int ineq
)
4726 int lower
= -1, upper
= -1;
4731 o_div
= isl_basic_map_offset(bmap
, isl_dim_div
);
4732 for (i
= 0; i
< bmap
->n_ineq
&& (lower
< 0 || upper
< 0); ++i
) {
4737 if (!isl_int_is_zero(bmap
->ineq
[i
][o_div
+ div
]))
4739 par
= isl_bool_false
;
4741 par
= is_parallel_except(bmap
, ineq
, i
, o_div
+ div
);
4748 opp
= isl_bool_false
;
4750 opp
= is_opposite_except(bmap
, ineq
, i
, o_div
+ div
);
4757 if (lower
< 0 || upper
< 0)
4758 return bmap
->n_ineq
;
4763 lower_bound_from_parallel(bmap
, ineq
, lower
, o_div
+ div
, &l
);
4764 lower_bound_from_opposite(bmap
, ineq
, upper
, o_div
+ div
, &u
);
4766 equal
= isl_int_eq(l
, u
);
4771 return equal
? lower
: bmap
->n_ineq
;
4774 /* Given a lower bound constraint "ineq" on the existentially quantified
4775 * variable "div", such that the corresponding lower bound has
4776 * a fixed value in "bmap", assign this fixed value to the variable and
4777 * then try and drop redundant divs again,
4778 * freeing the temporary data structure "pairs" that was associated
4779 * to the old version of "bmap".
4780 * "lower" determines the constant value for the lower bound.
4782 * In particular, "ineq" is of the form
4784 * f(x) + n d + c >= 0,
4786 * while "lower" is of the form
4790 * The lower bound is ceil((-f(x) - c)/n) and its constant value
4791 * is ceil((c0 - c)/n).
4793 static __isl_give isl_basic_map
*fix_cst_lower(__isl_take isl_basic_map
*bmap
,
4794 int div
, int ineq
, int lower
, int *pairs
)
4801 o_div
= isl_basic_map_offset(bmap
, isl_dim_div
);
4802 lower_bound_from_parallel(bmap
, ineq
, lower
, o_div
+ div
, &c
);
4803 bmap
= isl_basic_map_fix(bmap
, isl_dim_div
, div
, c
);
4808 return isl_basic_map_drop_redundant_divs(bmap
);
4811 /* Remove divs that are not strictly needed based on the inequality
4813 * In particular, if a div only occurs positively (or negatively)
4814 * in constraints, then it can simply be dropped.
4815 * Also, if a div occurs in only two constraints and if moreover
4816 * those two constraints are opposite to each other, except for the constant
4817 * term and if the sum of the constant terms is such that for any value
4818 * of the other values, there is always at least one integer value of the
4819 * div, i.e., if one plus this sum is greater than or equal to
4820 * the (absolute value) of the coefficient of the div in the constraints,
4821 * then we can also simply drop the div.
4823 * If an existentially quantified variable does not have an explicit
4824 * representation, appears in only a single lower bound that does not
4825 * involve any other such existentially quantified variables and appears
4826 * in this lower bound with coefficient 1,
4827 * then fix the variable to the value of the lower bound. That is,
4828 * turn the inequality into an equality.
4829 * If for any value of the other variables, there is any value
4830 * for the existentially quantified variable satisfying the constraints,
4831 * then this lower bound also satisfies the constraints.
4832 * It is therefore safe to pick this lower bound.
4834 * The same reasoning holds even if the coefficient is not one.
4835 * However, fixing the variable to the value of the lower bound may
4836 * in general introduce an extra integer division, in which case
4837 * it may be better to pick another value.
4838 * If this integer division has a known constant value, then plugging
4839 * in this constant value removes the existentially quantified variable
4840 * completely. In particular, if the lower bound is of the form
4841 * ceil((-f(x) - c)/n) and there are two constraints, f(x) + c0 >= 0 and
4842 * -f(x) + c1 >= 0 such that ceil((-c1 - c)/n) = ceil((c0 - c)/n),
4843 * then the existentially quantified variable can be assigned this
4846 * We skip divs that appear in equalities or in the definition of other divs.
4847 * Divs that appear in the definition of other divs usually occur in at least
4848 * 4 constraints, but the constraints may have been simplified.
4850 * If any divs are left after these simple checks then we move on
4851 * to more complicated cases in drop_more_redundant_divs.
4853 static __isl_give isl_basic_map
*isl_basic_map_drop_redundant_divs_ineq(
4854 __isl_take isl_basic_map
*bmap
)
4864 if (bmap
->n_div
== 0)
4867 off
= isl_basic_map_var_offset(bmap
, isl_dim_div
);
4869 return isl_basic_map_free(bmap
);
4870 pairs
= isl_calloc_array(bmap
->ctx
, int, bmap
->n_div
);
4874 n_ineq
= isl_basic_map_n_inequality(bmap
);
4875 for (i
= 0; i
< bmap
->n_div
; ++i
) {
4877 int last_pos
, last_neg
;
4880 isl_bool opp
, set_div
;
4882 defined
= !isl_int_is_zero(bmap
->div
[i
][0]);
4883 for (j
= i
; j
< bmap
->n_div
; ++j
)
4884 if (!isl_int_is_zero(bmap
->div
[j
][1 + 1 + off
+ i
]))
4886 if (j
< bmap
->n_div
)
4888 for (j
= 0; j
< bmap
->n_eq
; ++j
)
4889 if (!isl_int_is_zero(bmap
->eq
[j
][1 + off
+ i
]))
4895 for (j
= 0; j
< bmap
->n_ineq
; ++j
) {
4896 if (isl_int_is_pos(bmap
->ineq
[j
][1 + off
+ i
])) {
4900 if (isl_int_is_neg(bmap
->ineq
[j
][1 + off
+ i
])) {
4905 pairs
[i
] = pos
* neg
;
4906 if (pairs
[i
] == 0) {
4907 for (j
= bmap
->n_ineq
- 1; j
>= 0; --j
)
4908 if (!isl_int_is_zero(bmap
->ineq
[j
][1+off
+i
]))
4909 isl_basic_map_drop_inequality(bmap
, j
);
4910 bmap
= isl_basic_map_drop_div(bmap
, i
);
4911 return drop_redundant_divs_again(bmap
, pairs
, 0);
4914 opp
= isl_bool_false
;
4916 opp
= is_opposite(bmap
, last_pos
, last_neg
);
4921 isl_bool single
, one
;
4925 single
= single_unknown(bmap
, last_pos
, i
);
4930 one
= has_coef_one(bmap
, i
, last_pos
);
4934 return set_eq_and_try_again(bmap
, last_pos
,
4936 lower
= lower_bound_is_cst(bmap
, i
, last_pos
);
4940 return fix_cst_lower(bmap
, i
, last_pos
, lower
,
4945 isl_int_add(bmap
->ineq
[last_pos
][0],
4946 bmap
->ineq
[last_pos
][0], bmap
->ineq
[last_neg
][0]);
4947 isl_int_add_ui(bmap
->ineq
[last_pos
][0],
4948 bmap
->ineq
[last_pos
][0], 1);
4949 redundant
= isl_int_ge(bmap
->ineq
[last_pos
][0],
4950 bmap
->ineq
[last_pos
][1+off
+i
]);
4951 isl_int_sub_ui(bmap
->ineq
[last_pos
][0],
4952 bmap
->ineq
[last_pos
][0], 1);
4953 isl_int_sub(bmap
->ineq
[last_pos
][0],
4954 bmap
->ineq
[last_pos
][0], bmap
->ineq
[last_neg
][0]);
4956 return drop_div_and_try_again(bmap
, i
,
4957 last_pos
, last_neg
, pairs
);
4959 set_div
= isl_bool_false
;
4961 set_div
= ok_to_set_div_from_bound(bmap
, i
, last_pos
);
4963 return isl_basic_map_free(bmap
);
4965 bmap
= set_div_from_lower_bound(bmap
, i
, last_pos
);
4966 return drop_redundant_divs_again(bmap
, pairs
, 1);
4973 return coalesce_or_drop_more_redundant_divs(bmap
, pairs
, n
);
4979 isl_basic_map_free(bmap
);
4983 /* Consider the coefficients at "c" as a row vector and replace
4984 * them with their product with "T". "T" is assumed to be a square matrix.
4986 static isl_stat
preimage(isl_int
*c
, __isl_keep isl_mat
*T
)
4992 n
= isl_mat_rows(T
);
4994 return isl_stat_error
;
4995 if (isl_seq_first_non_zero(c
, n
) == -1)
4997 ctx
= isl_mat_get_ctx(T
);
4998 v
= isl_vec_alloc(ctx
, n
);
5000 return isl_stat_error
;
5001 isl_seq_swp_or_cpy(v
->el
, c
, n
);
5002 v
= isl_vec_mat_product(v
, isl_mat_copy(T
));
5004 return isl_stat_error
;
5005 isl_seq_swp_or_cpy(c
, v
->el
, n
);
5011 /* Plug in T for the variables in "bmap" starting at "pos".
5012 * T is a linear unimodular matrix, i.e., without constant term.
5014 static __isl_give isl_basic_map
*isl_basic_map_preimage_vars(
5015 __isl_take isl_basic_map
*bmap
, unsigned pos
, __isl_take isl_mat
*T
)
5018 isl_size n_row
, n_col
;
5020 bmap
= isl_basic_map_cow(bmap
);
5021 n_row
= isl_mat_rows(T
);
5022 n_col
= isl_mat_cols(T
);
5023 if (!bmap
|| n_row
< 0 || n_col
< 0)
5027 isl_die(isl_mat_get_ctx(T
), isl_error_invalid
,
5028 "expecting square matrix", goto error
);
5030 if (isl_basic_map_check_range(bmap
, isl_dim_all
, pos
, n_col
) < 0)
5033 for (i
= 0; i
< bmap
->n_eq
; ++i
)
5034 if (preimage(bmap
->eq
[i
] + 1 + pos
, T
) < 0)
5036 for (i
= 0; i
< bmap
->n_ineq
; ++i
)
5037 if (preimage(bmap
->ineq
[i
] + 1 + pos
, T
) < 0)
5039 for (i
= 0; i
< bmap
->n_div
; ++i
) {
5040 if (isl_basic_map_div_is_marked_unknown(bmap
, i
))
5042 if (preimage(bmap
->div
[i
] + 1 + 1 + pos
, T
) < 0)
5049 isl_basic_map_free(bmap
);
5054 /* Remove divs that are not strictly needed.
5056 * First look for an equality constraint involving two or more
5057 * existentially quantified variables without an explicit
5058 * representation. Replace the combination that appears
5059 * in the equality constraint by a single existentially quantified
5060 * variable such that the equality can be used to derive
5061 * an explicit representation for the variable.
5062 * If there are no more such equality constraints, then continue
5063 * with isl_basic_map_drop_redundant_divs_ineq.
5065 * In particular, if the equality constraint is of the form
5067 * f(x) + \sum_i c_i a_i = 0
5069 * with a_i existentially quantified variable without explicit
5070 * representation, then apply a transformation on the existentially
5071 * quantified variables to turn the constraint into
5075 * with g the gcd of the c_i.
5076 * In order to easily identify which existentially quantified variables
5077 * have a complete explicit representation, i.e., without being defined
5078 * in terms of other existentially quantified variables without
5079 * an explicit representation, the existentially quantified variables
5082 * The variable transformation is computed by extending the row
5083 * [c_1/g ... c_n/g] to a unimodular matrix, obtaining the transformation
5085 * [a_1'] [c_1/g ... c_n/g] [ a_1 ]
5090 * with [c_1/g ... c_n/g] representing the first row of U.
5091 * The inverse of U is then plugged into the original constraints.
5092 * The call to isl_basic_map_simplify makes sure the explicit
5093 * representation for a_1' is extracted from the equality constraint.
5095 __isl_give isl_basic_map
*isl_basic_map_drop_redundant_divs(
5096 __isl_take isl_basic_map
*bmap
)
5108 if (isl_basic_map_divs_known(bmap
))
5109 return isl_basic_map_drop_redundant_divs_ineq(bmap
);
5110 if (bmap
->n_eq
== 0)
5111 return isl_basic_map_drop_redundant_divs_ineq(bmap
);
5112 bmap
= isl_basic_map_sort_divs(bmap
);
5116 first
= isl_basic_map_first_unknown_div(bmap
);
5118 return isl_basic_map_free(bmap
);
5120 o_div
= isl_basic_map_offset(bmap
, isl_dim_div
);
5121 n_div
= isl_basic_map_dim(bmap
, isl_dim_div
);
5123 return isl_basic_map_free(bmap
);
5125 for (i
= 0; i
< bmap
->n_eq
; ++i
) {
5126 l
= isl_seq_first_non_zero(bmap
->eq
[i
] + o_div
+ first
,
5131 if (isl_seq_first_non_zero(bmap
->eq
[i
] + o_div
+ l
+ 1,
5132 n_div
- (l
+ 1)) == -1)
5136 if (i
>= bmap
->n_eq
)
5137 return isl_basic_map_drop_redundant_divs_ineq(bmap
);
5139 ctx
= isl_basic_map_get_ctx(bmap
);
5140 T
= isl_mat_alloc(ctx
, n_div
- l
, n_div
- l
);
5142 return isl_basic_map_free(bmap
);
5143 isl_seq_cpy(T
->row
[0], bmap
->eq
[i
] + o_div
+ l
, n_div
- l
);
5144 T
= isl_mat_normalize_row(T
, 0);
5145 T
= isl_mat_unimodular_complete(T
, 1);
5146 T
= isl_mat_right_inverse(T
);
5148 for (i
= l
; i
< n_div
; ++i
)
5149 bmap
= isl_basic_map_mark_div_unknown(bmap
, i
);
5150 bmap
= isl_basic_map_preimage_vars(bmap
, o_div
- 1 + l
, T
);
5151 bmap
= isl_basic_map_simplify(bmap
);
5153 return isl_basic_map_drop_redundant_divs(bmap
);
5156 /* Does "bmap" satisfy any equality that involves more than 2 variables
5157 * and/or has coefficients different from -1 and 1?
5159 static isl_bool
has_multiple_var_equality(__isl_keep isl_basic_map
*bmap
)
5164 total
= isl_basic_map_dim(bmap
, isl_dim_all
);
5166 return isl_bool_error
;
5168 for (i
= 0; i
< bmap
->n_eq
; ++i
) {
5171 j
= isl_seq_first_non_zero(bmap
->eq
[i
] + 1, total
);
5174 if (!isl_int_is_one(bmap
->eq
[i
][1 + j
]) &&
5175 !isl_int_is_negone(bmap
->eq
[i
][1 + j
]))
5176 return isl_bool_true
;
5179 k
= isl_seq_first_non_zero(bmap
->eq
[i
] + 1 + j
, total
- j
);
5183 if (!isl_int_is_one(bmap
->eq
[i
][1 + j
]) &&
5184 !isl_int_is_negone(bmap
->eq
[i
][1 + j
]))
5185 return isl_bool_true
;
5188 k
= isl_seq_first_non_zero(bmap
->eq
[i
] + 1 + j
, total
- j
);
5190 return isl_bool_true
;
5193 return isl_bool_false
;
5196 /* Remove any common factor g from the constraint coefficients in "v".
5197 * The constant term is stored in the first position and is replaced
5198 * by floor(c/g). If any common factor is removed and if this results
5199 * in a tightening of the constraint, then set *tightened.
5201 static __isl_give isl_vec
*normalize_constraint(__isl_take isl_vec
*v
,
5208 ctx
= isl_vec_get_ctx(v
);
5209 isl_seq_gcd(v
->el
+ 1, v
->size
- 1, &ctx
->normalize_gcd
);
5210 if (isl_int_is_zero(ctx
->normalize_gcd
))
5212 if (isl_int_is_one(ctx
->normalize_gcd
))
5217 if (tightened
&& !isl_int_is_divisible_by(v
->el
[0], ctx
->normalize_gcd
))
5219 isl_int_fdiv_q(v
->el
[0], v
->el
[0], ctx
->normalize_gcd
);
5220 isl_seq_scale_down(v
->el
+ 1, v
->el
+ 1, ctx
->normalize_gcd
,
5225 /* If "bmap" is an integer set that satisfies any equality involving
5226 * more than 2 variables and/or has coefficients different from -1 and 1,
5227 * then use variable compression to reduce the coefficients by removing
5228 * any (hidden) common factor.
5229 * In particular, apply the variable compression to each constraint,
5230 * factor out any common factor in the non-constant coefficients and
5231 * then apply the inverse of the compression.
5232 * At the end, we mark the basic map as having reduced constants.
5233 * If this flag is still set on the next invocation of this function,
5234 * then we skip the computation.
5236 * Removing a common factor may result in a tightening of some of
5237 * the constraints. If this happens, then we may end up with two
5238 * opposite inequalities that can be replaced by an equality.
5239 * We therefore call isl_basic_map_detect_inequality_pairs,
5240 * which checks for such pairs of inequalities as well as eliminate_divs_eq
5241 * and isl_basic_map_gauss if such a pair was found.
5243 * Tightening may also result in some other constraints becoming
5244 * (rationally) redundant with respect to the tightened constraint
5245 * (in combination with other constraints). The basic map may
5246 * therefore no longer be assumed to have no redundant constraints.
5248 * Note that this function may leave the result in an inconsistent state.
5249 * In particular, the constraints may not be gaussed.
5250 * Unfortunately, isl_map_coalesce actually depends on this inconsistent state
5251 * for some of the test cases to pass successfully.
5252 * Any potential modification of the representation is therefore only
5253 * performed on a single copy of the basic map.
5255 __isl_give isl_basic_map
*isl_basic_map_reduce_coefficients(
5256 __isl_take isl_basic_map
*bmap
)
5262 isl_mat
*eq
, *T
, *T2
;
5268 if (ISL_F_ISSET(bmap
, ISL_BASIC_MAP_REDUCED_COEFFICIENTS
))
5270 if (isl_basic_map_is_rational(bmap
))
5272 if (bmap
->n_eq
== 0)
5274 multi
= has_multiple_var_equality(bmap
);
5276 return isl_basic_map_free(bmap
);
5280 total
= isl_basic_map_dim(bmap
, isl_dim_all
);
5282 return isl_basic_map_free(bmap
);
5283 ctx
= isl_basic_map_get_ctx(bmap
);
5284 v
= isl_vec_alloc(ctx
, 1 + total
);
5286 return isl_basic_map_free(bmap
);
5288 eq
= isl_mat_sub_alloc6(ctx
, bmap
->eq
, 0, bmap
->n_eq
, 0, 1 + total
);
5289 T
= isl_mat_variable_compression(eq
, &T2
);
5292 if (T
->n_col
== 0) {
5296 return isl_basic_map_set_to_empty(bmap
);
5299 bmap
= isl_basic_map_cow(bmap
);
5304 for (i
= 0; i
< bmap
->n_ineq
; ++i
) {
5305 isl_seq_cpy(v
->el
, bmap
->ineq
[i
], 1 + total
);
5306 v
= isl_vec_mat_product(v
, isl_mat_copy(T
));
5307 v
= normalize_constraint(v
, &tightened
);
5308 v
= isl_vec_mat_product(v
, isl_mat_copy(T2
));
5311 isl_seq_cpy(bmap
->ineq
[i
], v
->el
, 1 + total
);
5318 ISL_F_SET(bmap
, ISL_BASIC_MAP_REDUCED_COEFFICIENTS
);
5323 ISL_F_CLR(bmap
, ISL_BASIC_MAP_NO_REDUNDANT
);
5324 bmap
= isl_basic_map_detect_inequality_pairs(bmap
, &progress
);
5326 bmap
= eliminate_divs_eq(bmap
, &progress
);
5327 bmap
= isl_basic_map_gauss(bmap
, NULL
);
5336 return isl_basic_map_free(bmap
);
5339 /* Shift the integer division at position "div" of "bmap"
5340 * by "shift" times the variable at position "pos".
5341 * "pos" is as determined by isl_basic_map_offset, i.e., pos == 0
5342 * corresponds to the constant term.
5344 * That is, if the integer division has the form
5348 * then replace it by
5350 * floor((f(x) + shift * d * x_pos)/d) - shift * x_pos
5352 __isl_give isl_basic_map
*isl_basic_map_shift_div(
5353 __isl_take isl_basic_map
*bmap
, int div
, int pos
, isl_int shift
)
5356 isl_size total
, n_div
;
5358 if (isl_int_is_zero(shift
))
5360 total
= isl_basic_map_dim(bmap
, isl_dim_all
);
5361 n_div
= isl_basic_map_dim(bmap
, isl_dim_div
);
5363 if (total
< 0 || n_div
< 0)
5364 return isl_basic_map_free(bmap
);
5366 isl_int_addmul(bmap
->div
[div
][1 + pos
], shift
, bmap
->div
[div
][0]);
5368 for (i
= 0; i
< bmap
->n_eq
; ++i
) {
5369 if (isl_int_is_zero(bmap
->eq
[i
][1 + total
+ div
]))
5371 isl_int_submul(bmap
->eq
[i
][pos
],
5372 shift
, bmap
->eq
[i
][1 + total
+ div
]);
5374 for (i
= 0; i
< bmap
->n_ineq
; ++i
) {
5375 if (isl_int_is_zero(bmap
->ineq
[i
][1 + total
+ div
]))
5377 isl_int_submul(bmap
->ineq
[i
][pos
],
5378 shift
, bmap
->ineq
[i
][1 + total
+ div
]);
5380 for (i
= 0; i
< bmap
->n_div
; ++i
) {
5381 if (isl_int_is_zero(bmap
->div
[i
][0]))
5383 if (isl_int_is_zero(bmap
->div
[i
][1 + 1 + total
+ div
]))
5385 isl_int_submul(bmap
->div
[i
][1 + pos
],
5386 shift
, bmap
->div
[i
][1 + 1 + total
+ div
]);