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 unsigned total
= isl_basic_map_total_dim(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
)
180 unsigned total
= 1 + isl_basic_map_total_dim(bmap
);
182 for (i
= 0; i
< total
; ++i
) {
185 reduce
= needs_reduction(bmap
, div
, i
);
187 return isl_basic_map_free(bmap
);
190 bmap
= reduce_coefficient_in_div(bmap
, div
, i
);
198 /* Reduce the coefficients (including the constant term) of
199 * the known integer divisions, if needed
200 * In particular, make sure all coefficients lie in
201 * the half-open interval (1/2,1/2].
203 static __isl_give isl_basic_map
*reduce_div_coefficients(
204 __isl_take isl_basic_map
*bmap
)
210 if (bmap
->n_div
== 0)
213 for (i
= 0; i
< bmap
->n_div
; ++i
) {
214 if (isl_int_is_zero(bmap
->div
[i
][0]))
216 bmap
= reduce_div_coefficients_of_div(bmap
, i
);
224 /* Remove any common factor in numerator and denominator of the div expression,
225 * not taking into account the constant term.
226 * That is, if the div is of the form
228 * floor((a + m f(x))/(m d))
232 * floor((floor(a/m) + f(x))/d)
234 * The difference {a/m}/d in the argument satisfies 0 <= {a/m}/d < 1/d
235 * and can therefore not influence the result of the floor.
237 static void normalize_div_expression(__isl_keep isl_basic_map
*bmap
, int div
)
239 unsigned total
= isl_basic_map_total_dim(bmap
);
240 isl_ctx
*ctx
= bmap
->ctx
;
242 if (isl_int_is_zero(bmap
->div
[div
][0]))
244 isl_seq_gcd(bmap
->div
[div
] + 2, total
, &ctx
->normalize_gcd
);
245 isl_int_gcd(ctx
->normalize_gcd
, ctx
->normalize_gcd
, bmap
->div
[div
][0]);
246 if (isl_int_is_one(ctx
->normalize_gcd
))
248 isl_int_fdiv_q(bmap
->div
[div
][1], bmap
->div
[div
][1],
250 isl_int_divexact(bmap
->div
[div
][0], bmap
->div
[div
][0],
252 isl_seq_scale_down(bmap
->div
[div
] + 2, bmap
->div
[div
] + 2,
253 ctx
->normalize_gcd
, total
);
256 /* Remove any common factor in numerator and denominator of a div expression,
257 * not taking into account the constant term.
258 * That is, look for any div of the form
260 * floor((a + m f(x))/(m d))
264 * floor((floor(a/m) + f(x))/d)
266 * The difference {a/m}/d in the argument satisfies 0 <= {a/m}/d < 1/d
267 * and can therefore not influence the result of the floor.
269 static __isl_give isl_basic_map
*normalize_div_expressions(
270 __isl_take isl_basic_map
*bmap
)
276 if (bmap
->n_div
== 0)
279 for (i
= 0; i
< bmap
->n_div
; ++i
)
280 normalize_div_expression(bmap
, i
);
285 /* Assumes divs have been ordered if keep_divs is set.
287 static void eliminate_var_using_equality(struct isl_basic_map
*bmap
,
288 unsigned pos
, isl_int
*eq
, int keep_divs
, int *progress
)
291 unsigned space_total
;
295 total
= isl_basic_map_total_dim(bmap
);
296 space_total
= isl_space_dim(bmap
->dim
, isl_dim_all
);
297 last_div
= isl_seq_last_non_zero(eq
+ 1 + space_total
, bmap
->n_div
);
298 for (k
= 0; k
< bmap
->n_eq
; ++k
) {
299 if (bmap
->eq
[k
] == eq
)
301 if (isl_int_is_zero(bmap
->eq
[k
][1+pos
]))
305 isl_seq_elim(bmap
->eq
[k
], eq
, 1+pos
, 1+total
, NULL
);
306 isl_seq_normalize(bmap
->ctx
, bmap
->eq
[k
], 1 + total
);
309 for (k
= 0; k
< bmap
->n_ineq
; ++k
) {
310 if (isl_int_is_zero(bmap
->ineq
[k
][1+pos
]))
314 isl_seq_elim(bmap
->ineq
[k
], eq
, 1+pos
, 1+total
, NULL
);
315 isl_seq_normalize(bmap
->ctx
, bmap
->ineq
[k
], 1 + total
);
316 ISL_F_CLR(bmap
, ISL_BASIC_MAP_NO_REDUNDANT
);
317 ISL_F_CLR(bmap
, ISL_BASIC_MAP_SORTED
);
320 for (k
= 0; k
< bmap
->n_div
; ++k
) {
321 if (isl_int_is_zero(bmap
->div
[k
][0]))
323 if (isl_int_is_zero(bmap
->div
[k
][1+1+pos
]))
327 /* We need to be careful about circular definitions,
328 * so for now we just remove the definition of div k
329 * if the equality contains any divs.
330 * If keep_divs is set, then the divs have been ordered
331 * and we can keep the definition as long as the result
334 if (last_div
== -1 || (keep_divs
&& last_div
< k
)) {
335 isl_seq_elim(bmap
->div
[k
]+1, eq
,
336 1+pos
, 1+total
, &bmap
->div
[k
][0]);
337 normalize_div_expression(bmap
, k
);
339 isl_seq_clr(bmap
->div
[k
], 1 + total
);
343 /* Assumes divs have been ordered if keep_divs is set.
345 static __isl_give isl_basic_map
*eliminate_div(__isl_take isl_basic_map
*bmap
,
346 isl_int
*eq
, unsigned div
, int keep_divs
)
348 unsigned pos
= isl_space_dim(bmap
->dim
, isl_dim_all
) + div
;
350 eliminate_var_using_equality(bmap
, pos
, eq
, keep_divs
, NULL
);
352 bmap
= isl_basic_map_drop_div(bmap
, div
);
357 /* Check if elimination of div "div" using equality "eq" would not
358 * result in a div depending on a later div.
360 static isl_bool
ok_to_eliminate_div(__isl_keep isl_basic_map
*bmap
, isl_int
*eq
,
365 unsigned space_total
= isl_space_dim(bmap
->dim
, isl_dim_all
);
366 unsigned pos
= space_total
+ div
;
368 last_div
= isl_seq_last_non_zero(eq
+ 1 + space_total
, bmap
->n_div
);
369 if (last_div
< 0 || last_div
<= div
)
370 return isl_bool_true
;
372 for (k
= 0; k
<= last_div
; ++k
) {
373 if (isl_int_is_zero(bmap
->div
[k
][0]))
375 if (!isl_int_is_zero(bmap
->div
[k
][1 + 1 + pos
]))
376 return isl_bool_false
;
379 return isl_bool_true
;
382 /* Eliminate divs based on equalities
384 static __isl_give isl_basic_map
*eliminate_divs_eq(
385 __isl_take isl_basic_map
*bmap
, int *progress
)
392 bmap
= isl_basic_map_order_divs(bmap
);
397 off
= 1 + isl_space_dim(bmap
->dim
, isl_dim_all
);
399 for (d
= bmap
->n_div
- 1; d
>= 0 ; --d
) {
400 for (i
= 0; i
< bmap
->n_eq
; ++i
) {
403 if (!isl_int_is_one(bmap
->eq
[i
][off
+ d
]) &&
404 !isl_int_is_negone(bmap
->eq
[i
][off
+ d
]))
406 ok
= ok_to_eliminate_div(bmap
, bmap
->eq
[i
], d
);
408 return isl_basic_map_free(bmap
);
413 bmap
= eliminate_div(bmap
, bmap
->eq
[i
], d
, 1);
414 if (isl_basic_map_drop_equality(bmap
, i
) < 0)
415 return isl_basic_map_free(bmap
);
420 return eliminate_divs_eq(bmap
, progress
);
424 /* Eliminate divs based on inequalities
426 static __isl_give isl_basic_map
*eliminate_divs_ineq(
427 __isl_take isl_basic_map
*bmap
, int *progress
)
438 off
= 1 + isl_space_dim(bmap
->dim
, isl_dim_all
);
440 for (d
= bmap
->n_div
- 1; d
>= 0 ; --d
) {
441 for (i
= 0; i
< bmap
->n_eq
; ++i
)
442 if (!isl_int_is_zero(bmap
->eq
[i
][off
+ d
]))
446 for (i
= 0; i
< bmap
->n_ineq
; ++i
)
447 if (isl_int_abs_gt(bmap
->ineq
[i
][off
+ d
], ctx
->one
))
449 if (i
< bmap
->n_ineq
)
452 bmap
= isl_basic_map_eliminate_vars(bmap
, (off
-1)+d
, 1);
453 if (!bmap
|| ISL_F_ISSET(bmap
, ISL_BASIC_MAP_EMPTY
))
455 bmap
= isl_basic_map_drop_div(bmap
, d
);
462 /* Does the equality constraint at position "eq" in "bmap" involve
463 * any local variables in the range [first, first + n)
464 * that are not marked as having an explicit representation?
466 static isl_bool
bmap_eq_involves_unknown_divs(__isl_keep isl_basic_map
*bmap
,
467 int eq
, unsigned first
, unsigned n
)
473 return isl_bool_error
;
475 o_div
= isl_basic_map_offset(bmap
, isl_dim_div
);
476 for (i
= 0; i
< n
; ++i
) {
479 if (isl_int_is_zero(bmap
->eq
[eq
][o_div
+ first
+ i
]))
481 unknown
= isl_basic_map_div_is_marked_unknown(bmap
, first
+ i
);
483 return isl_bool_error
;
485 return isl_bool_true
;
488 return isl_bool_false
;
491 /* The last local variable involved in the equality constraint
492 * at position "eq" in "bmap" is the local variable at position "div".
493 * It can therefore be used to extract an explicit representation
495 * Do so unless the local variable already has an explicit representation or
496 * the explicit representation would involve any other local variables
497 * that in turn do not have an explicit representation.
498 * An equality constraint involving local variables without an explicit
499 * representation can be used in isl_basic_map_drop_redundant_divs
500 * to separate out an independent local variable. Introducing
501 * an explicit representation here would block this transformation,
502 * while the partial explicit representation in itself is not very useful.
503 * Set *progress if anything is changed.
505 * The equality constraint is of the form
509 * with n a positive number. The explicit representation derived from
514 static __isl_give isl_basic_map
*set_div_from_eq(__isl_take isl_basic_map
*bmap
,
515 int div
, int eq
, int *progress
)
517 unsigned total
, o_div
;
523 if (!isl_int_is_zero(bmap
->div
[div
][0]))
526 involves
= bmap_eq_involves_unknown_divs(bmap
, eq
, 0, div
);
528 return isl_basic_map_free(bmap
);
532 total
= isl_basic_map_dim(bmap
, isl_dim_all
);
533 o_div
= isl_basic_map_offset(bmap
, isl_dim_div
);
534 isl_seq_neg(bmap
->div
[div
] + 1, bmap
->eq
[eq
], 1 + total
);
535 isl_int_set_si(bmap
->div
[div
][1 + o_div
+ div
], 0);
536 isl_int_set(bmap
->div
[div
][0], bmap
->eq
[eq
][o_div
+ div
]);
543 __isl_give isl_basic_map
*isl_basic_map_gauss(__isl_take isl_basic_map
*bmap
,
552 bmap
= isl_basic_map_order_divs(bmap
);
557 total
= isl_basic_map_total_dim(bmap
);
558 total_var
= total
- bmap
->n_div
;
560 last_var
= total
- 1;
561 for (done
= 0; done
< bmap
->n_eq
; ++done
) {
562 for (; last_var
>= 0; --last_var
) {
563 for (k
= done
; k
< bmap
->n_eq
; ++k
)
564 if (!isl_int_is_zero(bmap
->eq
[k
][1+last_var
]))
572 swap_equality(bmap
, k
, done
);
573 if (isl_int_is_neg(bmap
->eq
[done
][1+last_var
]))
574 isl_seq_neg(bmap
->eq
[done
], bmap
->eq
[done
], 1+total
);
576 eliminate_var_using_equality(bmap
, last_var
, bmap
->eq
[done
], 1,
579 if (last_var
>= total_var
)
580 bmap
= set_div_from_eq(bmap
, last_var
- total_var
,
585 if (done
== bmap
->n_eq
)
587 for (k
= done
; k
< bmap
->n_eq
; ++k
) {
588 if (isl_int_is_zero(bmap
->eq
[k
][0]))
590 return isl_basic_map_set_to_empty(bmap
);
592 isl_basic_map_free_equality(bmap
, bmap
->n_eq
-done
);
596 __isl_give isl_basic_set
*isl_basic_set_gauss(
597 __isl_take isl_basic_set
*bset
, int *progress
)
599 return bset_from_bmap(isl_basic_map_gauss(bset_to_bmap(bset
),
604 static unsigned int round_up(unsigned int v
)
615 /* Hash table of inequalities in a basic map.
616 * "index" is an array of addresses of inequalities in the basic map, some
617 * of which are NULL. The inequalities are hashed on the coefficients
618 * except the constant term.
619 * "size" is the number of elements in the array and is always a power of two
620 * "bits" is the number of bits need to represent an index into the array.
621 * "total" is the total dimension of the basic map.
623 struct isl_constraint_index
{
630 /* Fill in the "ci" data structure for holding the inequalities of "bmap".
632 static isl_stat
create_constraint_index(struct isl_constraint_index
*ci
,
633 __isl_keep isl_basic_map
*bmap
)
639 return isl_stat_error
;
640 ci
->total
= isl_basic_set_total_dim(bmap
);
641 if (bmap
->n_ineq
== 0)
643 ci
->size
= round_up(4 * (bmap
->n_ineq
+ 1) / 3 - 1);
644 ci
->bits
= ffs(ci
->size
) - 1;
645 ctx
= isl_basic_map_get_ctx(bmap
);
646 ci
->index
= isl_calloc_array(ctx
, isl_int
**, ci
->size
);
648 return isl_stat_error
;
653 /* Free the memory allocated by create_constraint_index.
655 static void constraint_index_free(struct isl_constraint_index
*ci
)
660 /* Return the position in ci->index that contains the address of
661 * an inequality that is equal to *ineq up to the constant term,
662 * provided this address is not identical to "ineq".
663 * If there is no such inequality, then return the position where
664 * such an inequality should be inserted.
666 static int hash_index_ineq(struct isl_constraint_index
*ci
, isl_int
**ineq
)
669 uint32_t hash
= isl_seq_get_hash_bits((*ineq
) + 1, ci
->total
, ci
->bits
);
670 for (h
= hash
; ci
->index
[h
]; h
= (h
+1) % ci
->size
)
671 if (ineq
!= ci
->index
[h
] &&
672 isl_seq_eq((*ineq
) + 1, ci
->index
[h
][0]+1, ci
->total
))
677 /* Return the position in ci->index that contains the address of
678 * an inequality that is equal to the k'th inequality of "bmap"
679 * up to the constant term, provided it does not point to the very
681 * If there is no such inequality, then return the position where
682 * such an inequality should be inserted.
684 static int hash_index(struct isl_constraint_index
*ci
,
685 __isl_keep isl_basic_map
*bmap
, int k
)
687 return hash_index_ineq(ci
, &bmap
->ineq
[k
]);
690 static int set_hash_index(struct isl_constraint_index
*ci
,
691 __isl_keep isl_basic_set
*bset
, int k
)
693 return hash_index(ci
, bset
, k
);
696 /* Fill in the "ci" data structure with the inequalities of "bset".
698 static isl_stat
setup_constraint_index(struct isl_constraint_index
*ci
,
699 __isl_keep isl_basic_set
*bset
)
703 if (create_constraint_index(ci
, bset
) < 0)
704 return isl_stat_error
;
706 for (k
= 0; k
< bset
->n_ineq
; ++k
) {
707 h
= set_hash_index(ci
, bset
, k
);
708 ci
->index
[h
] = &bset
->ineq
[k
];
714 /* Is the inequality ineq (obviously) redundant with respect
715 * to the constraints in "ci"?
717 * Look for an inequality in "ci" with the same coefficients and then
718 * check if the contant term of "ineq" is greater than or equal
719 * to the constant term of that inequality. If so, "ineq" is clearly
722 * Note that hash_index_ineq ignores a stored constraint if it has
723 * the same address as the passed inequality. It is ok to pass
724 * the address of a local variable here since it will never be
725 * the same as the address of a constraint in "ci".
727 static isl_bool
constraint_index_is_redundant(struct isl_constraint_index
*ci
,
732 h
= hash_index_ineq(ci
, &ineq
);
734 return isl_bool_false
;
735 return isl_int_ge(ineq
[0], (*ci
->index
[h
])[0]);
738 /* If we can eliminate more than one div, then we need to make
739 * sure we do it from last div to first div, in order not to
740 * change the position of the other divs that still need to
743 static __isl_give isl_basic_map
*remove_duplicate_divs(
744 __isl_take isl_basic_map
*bmap
, int *progress
)
756 bmap
= isl_basic_map_order_divs(bmap
);
757 if (!bmap
|| bmap
->n_div
<= 1)
760 total_var
= isl_space_dim(bmap
->dim
, isl_dim_all
);
761 total
= total_var
+ bmap
->n_div
;
764 for (k
= bmap
->n_div
- 1; k
>= 0; --k
)
765 if (!isl_int_is_zero(bmap
->div
[k
][0]))
770 size
= round_up(4 * bmap
->n_div
/ 3 - 1);
773 elim_for
= isl_calloc_array(ctx
, int, bmap
->n_div
);
774 bits
= ffs(size
) - 1;
775 index
= isl_calloc_array(ctx
, int, size
);
776 if (!elim_for
|| !index
)
778 eq
= isl_blk_alloc(ctx
, 1+total
);
779 if (isl_blk_is_error(eq
))
782 isl_seq_clr(eq
.data
, 1+total
);
783 index
[isl_seq_get_hash_bits(bmap
->div
[k
], 2+total
, bits
)] = k
+ 1;
784 for (--k
; k
>= 0; --k
) {
787 if (isl_int_is_zero(bmap
->div
[k
][0]))
790 hash
= isl_seq_get_hash_bits(bmap
->div
[k
], 2+total
, bits
);
791 for (h
= hash
; index
[h
]; h
= (h
+1) % size
)
792 if (isl_seq_eq(bmap
->div
[k
],
793 bmap
->div
[index
[h
]-1], 2+total
))
802 for (l
= bmap
->n_div
- 1; l
>= 0; --l
) {
806 isl_int_set_si(eq
.data
[1+total_var
+k
], -1);
807 isl_int_set_si(eq
.data
[1+total_var
+l
], 1);
808 bmap
= eliminate_div(bmap
, eq
.data
, l
, 1);
811 isl_int_set_si(eq
.data
[1+total_var
+k
], 0);
812 isl_int_set_si(eq
.data
[1+total_var
+l
], 0);
815 isl_blk_free(ctx
, eq
);
822 static int n_pure_div_eq(struct isl_basic_map
*bmap
)
827 total
= isl_space_dim(bmap
->dim
, isl_dim_all
);
828 for (i
= 0, j
= bmap
->n_div
-1; i
< bmap
->n_eq
; ++i
) {
829 while (j
>= 0 && isl_int_is_zero(bmap
->eq
[i
][1 + total
+ j
]))
833 if (isl_seq_first_non_zero(bmap
->eq
[i
] + 1 + total
, j
) != -1)
839 /* Normalize divs that appear in equalities.
841 * In particular, we assume that bmap contains some equalities
846 * and we want to replace the set of e_i by a minimal set and
847 * such that the new e_i have a canonical representation in terms
849 * If any of the equalities involves more than one divs, then
850 * we currently simply bail out.
852 * Let us first additionally assume that all equalities involve
853 * a div. The equalities then express modulo constraints on the
854 * remaining variables and we can use "parameter compression"
855 * to find a minimal set of constraints. The result is a transformation
857 * x = T(x') = x_0 + G x'
859 * with G a lower-triangular matrix with all elements below the diagonal
860 * non-negative and smaller than the diagonal element on the same row.
861 * We first normalize x_0 by making the same property hold in the affine
863 * The rows i of G with a 1 on the diagonal do not impose any modulo
864 * constraint and simply express x_i = x'_i.
865 * For each of the remaining rows i, we introduce a div and a corresponding
866 * equality. In particular
868 * g_ii e_j = x_i - g_i(x')
870 * where each x'_k is replaced either by x_k (if g_kk = 1) or the
871 * corresponding div (if g_kk != 1).
873 * If there are any equalities not involving any div, then we
874 * first apply a variable compression on the variables x:
876 * x = C x'' x'' = C_2 x
878 * and perform the above parameter compression on A C instead of on A.
879 * The resulting compression is then of the form
881 * x'' = T(x') = x_0 + G x'
883 * and in constructing the new divs and the corresponding equalities,
884 * we have to replace each x'', i.e., the x'_k with (g_kk = 1),
885 * by the corresponding row from C_2.
887 static __isl_give isl_basic_map
*normalize_divs(__isl_take isl_basic_map
*bmap
,
895 struct isl_mat
*T
= NULL
;
896 struct isl_mat
*C
= NULL
;
897 struct isl_mat
*C2
= NULL
;
905 if (bmap
->n_div
== 0)
911 if (ISL_F_ISSET(bmap
, ISL_BASIC_MAP_NORMALIZED_DIVS
))
914 total
= isl_space_dim(bmap
->dim
, isl_dim_all
);
915 div_eq
= n_pure_div_eq(bmap
);
919 if (div_eq
< bmap
->n_eq
) {
920 B
= isl_mat_sub_alloc6(bmap
->ctx
, bmap
->eq
, div_eq
,
921 bmap
->n_eq
- div_eq
, 0, 1 + total
);
922 C
= isl_mat_variable_compression(B
, &C2
);
926 bmap
= isl_basic_map_set_to_empty(bmap
);
933 d
= isl_vec_alloc(bmap
->ctx
, div_eq
);
936 for (i
= 0, j
= bmap
->n_div
-1; i
< div_eq
; ++i
) {
937 while (j
>= 0 && isl_int_is_zero(bmap
->eq
[i
][1 + total
+ j
]))
939 isl_int_set(d
->block
.data
[i
], bmap
->eq
[i
][1 + total
+ j
]);
941 B
= isl_mat_sub_alloc6(bmap
->ctx
, bmap
->eq
, 0, div_eq
, 0, 1 + total
);
944 B
= isl_mat_product(B
, C
);
948 T
= isl_mat_parameter_compression(B
, d
);
952 bmap
= isl_basic_map_set_to_empty(bmap
);
958 for (i
= 0; i
< T
->n_row
- 1; ++i
) {
959 isl_int_fdiv_q(v
, T
->row
[1 + i
][0], T
->row
[1 + i
][1 + i
]);
960 if (isl_int_is_zero(v
))
962 isl_mat_col_submul(T
, 0, v
, 1 + i
);
965 pos
= isl_alloc_array(bmap
->ctx
, int, T
->n_row
);
968 /* We have to be careful because dropping equalities may reorder them */
970 for (j
= bmap
->n_div
- 1; j
>= 0; --j
) {
971 for (i
= 0; i
< bmap
->n_eq
; ++i
)
972 if (!isl_int_is_zero(bmap
->eq
[i
][1 + total
+ j
]))
974 if (i
< bmap
->n_eq
) {
975 bmap
= isl_basic_map_drop_div(bmap
, j
);
976 isl_basic_map_drop_equality(bmap
, i
);
982 for (i
= 1; i
< T
->n_row
; ++i
) {
983 if (isl_int_is_one(T
->row
[i
][i
]))
988 if (needed
> dropped
) {
989 bmap
= isl_basic_map_extend_space(bmap
, isl_space_copy(bmap
->dim
),
994 for (i
= 1; i
< T
->n_row
; ++i
) {
995 if (isl_int_is_one(T
->row
[i
][i
]))
997 k
= isl_basic_map_alloc_div(bmap
);
998 pos
[i
] = 1 + total
+ k
;
999 isl_seq_clr(bmap
->div
[k
] + 1, 1 + total
+ bmap
->n_div
);
1000 isl_int_set(bmap
->div
[k
][0], T
->row
[i
][i
]);
1002 isl_seq_cpy(bmap
->div
[k
] + 1, C2
->row
[i
], 1 + total
);
1004 isl_int_set_si(bmap
->div
[k
][1 + i
], 1);
1005 for (j
= 0; j
< i
; ++j
) {
1006 if (isl_int_is_zero(T
->row
[i
][j
]))
1008 if (pos
[j
] < T
->n_row
&& C2
)
1009 isl_seq_submul(bmap
->div
[k
] + 1, T
->row
[i
][j
],
1010 C2
->row
[pos
[j
]], 1 + total
);
1012 isl_int_neg(bmap
->div
[k
][1 + pos
[j
]],
1015 j
= isl_basic_map_alloc_equality(bmap
);
1016 isl_seq_neg(bmap
->eq
[j
], bmap
->div
[k
]+1, 1+total
+bmap
->n_div
);
1017 isl_int_set(bmap
->eq
[j
][pos
[i
]], bmap
->div
[k
][0]);
1026 ISL_F_SET(bmap
, ISL_BASIC_MAP_NORMALIZED_DIVS
);
1037 static __isl_give isl_basic_map
*set_div_from_lower_bound(
1038 __isl_take isl_basic_map
*bmap
, int div
, int ineq
)
1040 unsigned total
= 1 + isl_space_dim(bmap
->dim
, isl_dim_all
);
1042 isl_seq_neg(bmap
->div
[div
] + 1, bmap
->ineq
[ineq
], total
+ bmap
->n_div
);
1043 isl_int_set(bmap
->div
[div
][0], bmap
->ineq
[ineq
][total
+ div
]);
1044 isl_int_add(bmap
->div
[div
][1], bmap
->div
[div
][1], bmap
->div
[div
][0]);
1045 isl_int_sub_ui(bmap
->div
[div
][1], bmap
->div
[div
][1], 1);
1046 isl_int_set_si(bmap
->div
[div
][1 + total
+ div
], 0);
1051 /* Check whether it is ok to define a div based on an inequality.
1052 * To avoid the introduction of circular definitions of divs, we
1053 * do not allow such a definition if the resulting expression would refer to
1054 * any other undefined divs or if any known div is defined in
1055 * terms of the unknown div.
1057 static isl_bool
ok_to_set_div_from_bound(__isl_keep isl_basic_map
*bmap
,
1061 unsigned total
= 1 + isl_space_dim(bmap
->dim
, isl_dim_all
);
1063 /* Not defined in terms of unknown divs */
1064 for (j
= 0; j
< bmap
->n_div
; ++j
) {
1067 if (isl_int_is_zero(bmap
->ineq
[ineq
][total
+ j
]))
1069 if (isl_int_is_zero(bmap
->div
[j
][0]))
1070 return isl_bool_false
;
1073 /* No other div defined in terms of this one => avoid loops */
1074 for (j
= 0; j
< bmap
->n_div
; ++j
) {
1077 if (isl_int_is_zero(bmap
->div
[j
][0]))
1079 if (!isl_int_is_zero(bmap
->div
[j
][1 + total
+ div
]))
1080 return isl_bool_false
;
1083 return isl_bool_true
;
1086 /* Would an expression for div "div" based on inequality "ineq" of "bmap"
1087 * be a better expression than the current one?
1089 * If we do not have any expression yet, then any expression would be better.
1090 * Otherwise we check if the last variable involved in the inequality
1091 * (disregarding the div that it would define) is in an earlier position
1092 * than the last variable involved in the current div expression.
1094 static isl_bool
better_div_constraint(__isl_keep isl_basic_map
*bmap
,
1097 unsigned total
= 1 + isl_space_dim(bmap
->dim
, isl_dim_all
);
1101 if (isl_int_is_zero(bmap
->div
[div
][0]))
1102 return isl_bool_true
;
1104 if (isl_seq_last_non_zero(bmap
->ineq
[ineq
] + total
+ div
+ 1,
1105 bmap
->n_div
- (div
+ 1)) >= 0)
1106 return isl_bool_false
;
1108 last_ineq
= isl_seq_last_non_zero(bmap
->ineq
[ineq
], total
+ div
);
1109 last_div
= isl_seq_last_non_zero(bmap
->div
[div
] + 1,
1110 total
+ bmap
->n_div
);
1112 return last_ineq
< last_div
;
1115 /* Given two constraints "k" and "l" that are opposite to each other,
1116 * except for the constant term, check if we can use them
1117 * to obtain an expression for one of the hitherto unknown divs or
1118 * a "better" expression for a div for which we already have an expression.
1119 * "sum" is the sum of the constant terms of the constraints.
1120 * If this sum is strictly smaller than the coefficient of one
1121 * of the divs, then this pair can be used define the div.
1122 * To avoid the introduction of circular definitions of divs, we
1123 * do not use the pair if the resulting expression would refer to
1124 * any other undefined divs or if any known div is defined in
1125 * terms of the unknown div.
1127 static __isl_give isl_basic_map
*check_for_div_constraints(
1128 __isl_take isl_basic_map
*bmap
, int k
, int l
, isl_int sum
,
1132 unsigned total
= 1 + isl_space_dim(bmap
->dim
, isl_dim_all
);
1134 for (i
= 0; i
< bmap
->n_div
; ++i
) {
1137 if (isl_int_is_zero(bmap
->ineq
[k
][total
+ i
]))
1139 if (isl_int_abs_ge(sum
, bmap
->ineq
[k
][total
+ i
]))
1141 set_div
= better_div_constraint(bmap
, i
, k
);
1142 if (set_div
>= 0 && set_div
)
1143 set_div
= ok_to_set_div_from_bound(bmap
, i
, k
);
1145 return isl_basic_map_free(bmap
);
1148 if (isl_int_is_pos(bmap
->ineq
[k
][total
+ i
]))
1149 bmap
= set_div_from_lower_bound(bmap
, i
, k
);
1151 bmap
= set_div_from_lower_bound(bmap
, i
, l
);
1159 __isl_give isl_basic_map
*isl_basic_map_remove_duplicate_constraints(
1160 __isl_take isl_basic_map
*bmap
, int *progress
, int detect_divs
)
1162 struct isl_constraint_index ci
;
1164 unsigned total
= isl_basic_map_total_dim(bmap
);
1167 if (!bmap
|| bmap
->n_ineq
<= 1)
1170 if (create_constraint_index(&ci
, bmap
) < 0)
1173 h
= isl_seq_get_hash_bits(bmap
->ineq
[0] + 1, total
, ci
.bits
);
1174 ci
.index
[h
] = &bmap
->ineq
[0];
1175 for (k
= 1; k
< bmap
->n_ineq
; ++k
) {
1176 h
= hash_index(&ci
, bmap
, k
);
1178 ci
.index
[h
] = &bmap
->ineq
[k
];
1183 l
= ci
.index
[h
] - &bmap
->ineq
[0];
1184 if (isl_int_lt(bmap
->ineq
[k
][0], bmap
->ineq
[l
][0]))
1185 swap_inequality(bmap
, k
, l
);
1186 isl_basic_map_drop_inequality(bmap
, k
);
1190 for (k
= 0; k
< bmap
->n_ineq
-1; ++k
) {
1191 isl_seq_neg(bmap
->ineq
[k
]+1, bmap
->ineq
[k
]+1, total
);
1192 h
= hash_index(&ci
, bmap
, k
);
1193 isl_seq_neg(bmap
->ineq
[k
]+1, bmap
->ineq
[k
]+1, total
);
1196 l
= ci
.index
[h
] - &bmap
->ineq
[0];
1197 isl_int_add(sum
, bmap
->ineq
[k
][0], bmap
->ineq
[l
][0]);
1198 if (isl_int_is_pos(sum
)) {
1200 bmap
= check_for_div_constraints(bmap
, k
, l
,
1204 if (isl_int_is_zero(sum
)) {
1205 /* We need to break out of the loop after these
1206 * changes since the contents of the hash
1207 * will no longer be valid.
1208 * Plus, we probably we want to regauss first.
1212 isl_basic_map_drop_inequality(bmap
, l
);
1213 isl_basic_map_inequality_to_equality(bmap
, k
);
1215 bmap
= isl_basic_map_set_to_empty(bmap
);
1220 constraint_index_free(&ci
);
1224 /* Detect all pairs of inequalities that form an equality.
1226 * isl_basic_map_remove_duplicate_constraints detects at most one such pair.
1227 * Call it repeatedly while it is making progress.
1229 __isl_give isl_basic_map
*isl_basic_map_detect_inequality_pairs(
1230 __isl_take isl_basic_map
*bmap
, int *progress
)
1236 bmap
= isl_basic_map_remove_duplicate_constraints(bmap
,
1238 if (progress
&& duplicate
)
1240 } while (duplicate
);
1245 /* Eliminate knowns divs from constraints where they appear with
1246 * a (positive or negative) unit coefficient.
1250 * floor(e/m) + f >= 0
1258 * -floor(e/m) + f >= 0
1262 * -e + m f + m - 1 >= 0
1264 * The first conversion is valid because floor(e/m) >= -f is equivalent
1265 * to e/m >= -f because -f is an integral expression.
1266 * The second conversion follows from the fact that
1268 * -floor(e/m) = ceil(-e/m) = floor((-e + m - 1)/m)
1271 * Note that one of the div constraints may have been eliminated
1272 * due to being redundant with respect to the constraint that is
1273 * being modified by this function. The modified constraint may
1274 * no longer imply this div constraint, so we add it back to make
1275 * sure we do not lose any information.
1277 * We skip integral divs, i.e., those with denominator 1, as we would
1278 * risk eliminating the div from the div constraints. We do not need
1279 * to handle those divs here anyway since the div constraints will turn
1280 * out to form an equality and this equality can then be used to eliminate
1281 * the div from all constraints.
1283 static __isl_give isl_basic_map
*eliminate_unit_divs(
1284 __isl_take isl_basic_map
*bmap
, int *progress
)
1293 ctx
= isl_basic_map_get_ctx(bmap
);
1294 total
= 1 + isl_space_dim(bmap
->dim
, isl_dim_all
);
1296 for (i
= 0; i
< bmap
->n_div
; ++i
) {
1297 if (isl_int_is_zero(bmap
->div
[i
][0]))
1299 if (isl_int_is_one(bmap
->div
[i
][0]))
1301 for (j
= 0; j
< bmap
->n_ineq
; ++j
) {
1304 if (!isl_int_is_one(bmap
->ineq
[j
][total
+ i
]) &&
1305 !isl_int_is_negone(bmap
->ineq
[j
][total
+ i
]))
1310 s
= isl_int_sgn(bmap
->ineq
[j
][total
+ i
]);
1311 isl_int_set_si(bmap
->ineq
[j
][total
+ i
], 0);
1313 isl_seq_combine(bmap
->ineq
[j
],
1314 ctx
->negone
, bmap
->div
[i
] + 1,
1315 bmap
->div
[i
][0], bmap
->ineq
[j
],
1316 total
+ bmap
->n_div
);
1318 isl_seq_combine(bmap
->ineq
[j
],
1319 ctx
->one
, bmap
->div
[i
] + 1,
1320 bmap
->div
[i
][0], bmap
->ineq
[j
],
1321 total
+ bmap
->n_div
);
1323 isl_int_add(bmap
->ineq
[j
][0],
1324 bmap
->ineq
[j
][0], bmap
->div
[i
][0]);
1325 isl_int_sub_ui(bmap
->ineq
[j
][0],
1326 bmap
->ineq
[j
][0], 1);
1329 bmap
= isl_basic_map_extend_constraints(bmap
, 0, 1);
1330 if (isl_basic_map_add_div_constraint(bmap
, i
, s
) < 0)
1331 return isl_basic_map_free(bmap
);
1338 __isl_give isl_basic_map
*isl_basic_map_simplify(__isl_take isl_basic_map
*bmap
)
1347 empty
= isl_basic_map_plain_is_empty(bmap
);
1349 return isl_basic_map_free(bmap
);
1352 bmap
= isl_basic_map_normalize_constraints(bmap
);
1353 bmap
= reduce_div_coefficients(bmap
);
1354 bmap
= normalize_div_expressions(bmap
);
1355 bmap
= remove_duplicate_divs(bmap
, &progress
);
1356 bmap
= eliminate_unit_divs(bmap
, &progress
);
1357 bmap
= eliminate_divs_eq(bmap
, &progress
);
1358 bmap
= eliminate_divs_ineq(bmap
, &progress
);
1359 bmap
= isl_basic_map_gauss(bmap
, &progress
);
1360 /* requires equalities in normal form */
1361 bmap
= normalize_divs(bmap
, &progress
);
1362 bmap
= isl_basic_map_remove_duplicate_constraints(bmap
,
1364 if (bmap
&& progress
)
1365 ISL_F_CLR(bmap
, ISL_BASIC_MAP_REDUCED_COEFFICIENTS
);
1370 struct isl_basic_set
*isl_basic_set_simplify(struct isl_basic_set
*bset
)
1372 return bset_from_bmap(isl_basic_map_simplify(bset_to_bmap(bset
)));
1376 isl_bool
isl_basic_map_is_div_constraint(__isl_keep isl_basic_map
*bmap
,
1377 isl_int
*constraint
, unsigned div
)
1382 return isl_bool_error
;
1384 pos
= 1 + isl_space_dim(bmap
->dim
, isl_dim_all
) + div
;
1386 if (isl_int_eq(constraint
[pos
], bmap
->div
[div
][0])) {
1388 isl_int_sub(bmap
->div
[div
][1],
1389 bmap
->div
[div
][1], bmap
->div
[div
][0]);
1390 isl_int_add_ui(bmap
->div
[div
][1], bmap
->div
[div
][1], 1);
1391 neg
= isl_seq_is_neg(constraint
, bmap
->div
[div
]+1, pos
);
1392 isl_int_sub_ui(bmap
->div
[div
][1], bmap
->div
[div
][1], 1);
1393 isl_int_add(bmap
->div
[div
][1],
1394 bmap
->div
[div
][1], bmap
->div
[div
][0]);
1396 return isl_bool_false
;
1397 if (isl_seq_first_non_zero(constraint
+pos
+1,
1398 bmap
->n_div
-div
-1) != -1)
1399 return isl_bool_false
;
1400 } else if (isl_int_abs_eq(constraint
[pos
], bmap
->div
[div
][0])) {
1401 if (!isl_seq_eq(constraint
, bmap
->div
[div
]+1, pos
))
1402 return isl_bool_false
;
1403 if (isl_seq_first_non_zero(constraint
+pos
+1,
1404 bmap
->n_div
-div
-1) != -1)
1405 return isl_bool_false
;
1407 return isl_bool_false
;
1409 return isl_bool_true
;
1412 isl_bool
isl_basic_set_is_div_constraint(__isl_keep isl_basic_set
*bset
,
1413 isl_int
*constraint
, unsigned div
)
1415 return isl_basic_map_is_div_constraint(bset
, constraint
, div
);
1419 /* If the only constraints a div d=floor(f/m)
1420 * appears in are its two defining constraints
1423 * -(f - (m - 1)) + m d >= 0
1425 * then it can safely be removed.
1427 static isl_bool
div_is_redundant(__isl_keep isl_basic_map
*bmap
, int div
)
1430 unsigned pos
= 1 + isl_space_dim(bmap
->dim
, isl_dim_all
) + div
;
1432 for (i
= 0; i
< bmap
->n_eq
; ++i
)
1433 if (!isl_int_is_zero(bmap
->eq
[i
][pos
]))
1434 return isl_bool_false
;
1436 for (i
= 0; i
< bmap
->n_ineq
; ++i
) {
1439 if (isl_int_is_zero(bmap
->ineq
[i
][pos
]))
1441 red
= isl_basic_map_is_div_constraint(bmap
, bmap
->ineq
[i
], div
);
1442 if (red
< 0 || !red
)
1446 for (i
= 0; i
< bmap
->n_div
; ++i
) {
1447 if (isl_int_is_zero(bmap
->div
[i
][0]))
1449 if (!isl_int_is_zero(bmap
->div
[i
][1+pos
]))
1450 return isl_bool_false
;
1453 return isl_bool_true
;
1457 * Remove divs that don't occur in any of the constraints or other divs.
1458 * These can arise when dropping constraints from a basic map or
1459 * when the divs of a basic map have been temporarily aligned
1460 * with the divs of another basic map.
1462 static __isl_give isl_basic_map
*remove_redundant_divs(
1463 __isl_take isl_basic_map
*bmap
)
1470 for (i
= bmap
->n_div
-1; i
>= 0; --i
) {
1473 redundant
= div_is_redundant(bmap
, i
);
1475 return isl_basic_map_free(bmap
);
1478 bmap
= isl_basic_map_drop_div(bmap
, i
);
1483 /* Mark "bmap" as final, without checking for obviously redundant
1484 * integer divisions. This function should be used when "bmap"
1485 * is known not to involve any such integer divisions.
1487 __isl_give isl_basic_map
*isl_basic_map_mark_final(
1488 __isl_take isl_basic_map
*bmap
)
1492 ISL_F_SET(bmap
, ISL_BASIC_SET_FINAL
);
1496 /* Mark "bmap" as final, after removing obviously redundant integer divisions.
1498 __isl_give isl_basic_map
*isl_basic_map_finalize(__isl_take isl_basic_map
*bmap
)
1500 bmap
= remove_redundant_divs(bmap
);
1501 bmap
= isl_basic_map_mark_final(bmap
);
1505 struct isl_basic_set
*isl_basic_set_finalize(struct isl_basic_set
*bset
)
1507 return bset_from_bmap(isl_basic_map_finalize(bset_to_bmap(bset
)));
1510 /* Remove definition of any div that is defined in terms of the given variable.
1511 * The div itself is not removed. Functions such as
1512 * eliminate_divs_ineq depend on the other divs remaining in place.
1514 static __isl_give isl_basic_map
*remove_dependent_vars(
1515 __isl_take isl_basic_map
*bmap
, int pos
)
1522 for (i
= 0; i
< bmap
->n_div
; ++i
) {
1523 if (isl_int_is_zero(bmap
->div
[i
][0]))
1525 if (isl_int_is_zero(bmap
->div
[i
][1+1+pos
]))
1527 bmap
= isl_basic_map_mark_div_unknown(bmap
, i
);
1534 /* Eliminate the specified variables from the constraints using
1535 * Fourier-Motzkin. The variables themselves are not removed.
1537 __isl_give isl_basic_map
*isl_basic_map_eliminate_vars(
1538 __isl_take isl_basic_map
*bmap
, unsigned pos
, unsigned n
)
1549 total
= isl_basic_map_total_dim(bmap
);
1551 bmap
= isl_basic_map_cow(bmap
);
1552 for (d
= pos
+ n
- 1; d
>= 0 && d
>= pos
; --d
)
1553 bmap
= remove_dependent_vars(bmap
, d
);
1557 for (d
= pos
+ n
- 1;
1558 d
>= 0 && d
>= total
- bmap
->n_div
&& d
>= pos
; --d
)
1559 isl_seq_clr(bmap
->div
[d
-(total
-bmap
->n_div
)], 2+total
);
1560 for (d
= pos
+ n
- 1; d
>= 0 && d
>= pos
; --d
) {
1561 int n_lower
, n_upper
;
1564 for (i
= 0; i
< bmap
->n_eq
; ++i
) {
1565 if (isl_int_is_zero(bmap
->eq
[i
][1+d
]))
1567 eliminate_var_using_equality(bmap
, d
, bmap
->eq
[i
], 0, NULL
);
1568 isl_basic_map_drop_equality(bmap
, i
);
1576 for (i
= 0; i
< bmap
->n_ineq
; ++i
) {
1577 if (isl_int_is_pos(bmap
->ineq
[i
][1+d
]))
1579 else if (isl_int_is_neg(bmap
->ineq
[i
][1+d
]))
1582 bmap
= isl_basic_map_extend_constraints(bmap
,
1583 0, n_lower
* n_upper
);
1586 for (i
= bmap
->n_ineq
- 1; i
>= 0; --i
) {
1588 if (isl_int_is_zero(bmap
->ineq
[i
][1+d
]))
1591 for (j
= 0; j
< i
; ++j
) {
1592 if (isl_int_is_zero(bmap
->ineq
[j
][1+d
]))
1595 if (isl_int_sgn(bmap
->ineq
[i
][1+d
]) ==
1596 isl_int_sgn(bmap
->ineq
[j
][1+d
]))
1598 k
= isl_basic_map_alloc_inequality(bmap
);
1601 isl_seq_cpy(bmap
->ineq
[k
], bmap
->ineq
[i
],
1603 isl_seq_elim(bmap
->ineq
[k
], bmap
->ineq
[j
],
1604 1+d
, 1+total
, NULL
);
1606 isl_basic_map_drop_inequality(bmap
, i
);
1609 if (n_lower
> 0 && n_upper
> 0) {
1610 bmap
= isl_basic_map_normalize_constraints(bmap
);
1611 bmap
= isl_basic_map_remove_duplicate_constraints(bmap
,
1613 bmap
= isl_basic_map_gauss(bmap
, NULL
);
1614 bmap
= isl_basic_map_remove_redundancies(bmap
);
1618 if (ISL_F_ISSET(bmap
, ISL_BASIC_MAP_EMPTY
))
1623 bmap
= isl_basic_map_gauss(bmap
, NULL
);
1626 isl_basic_map_free(bmap
);
1630 struct isl_basic_set
*isl_basic_set_eliminate_vars(
1631 struct isl_basic_set
*bset
, unsigned pos
, unsigned n
)
1633 return bset_from_bmap(isl_basic_map_eliminate_vars(bset_to_bmap(bset
),
1637 /* Eliminate the specified n dimensions starting at first from the
1638 * constraints, without removing the dimensions from the space.
1639 * If the set is rational, the dimensions are eliminated using Fourier-Motzkin.
1640 * Otherwise, they are projected out and the original space is restored.
1642 __isl_give isl_basic_map
*isl_basic_map_eliminate(
1643 __isl_take isl_basic_map
*bmap
,
1644 enum isl_dim_type type
, unsigned first
, unsigned n
)
1653 if (first
+ n
> isl_basic_map_dim(bmap
, type
) || first
+ n
< first
)
1654 isl_die(bmap
->ctx
, isl_error_invalid
,
1655 "index out of bounds", goto error
);
1657 if (ISL_F_ISSET(bmap
, ISL_BASIC_MAP_RATIONAL
)) {
1658 first
+= isl_basic_map_offset(bmap
, type
) - 1;
1659 bmap
= isl_basic_map_eliminate_vars(bmap
, first
, n
);
1660 return isl_basic_map_finalize(bmap
);
1663 space
= isl_basic_map_get_space(bmap
);
1664 bmap
= isl_basic_map_project_out(bmap
, type
, first
, n
);
1665 bmap
= isl_basic_map_insert_dims(bmap
, type
, first
, n
);
1666 bmap
= isl_basic_map_reset_space(bmap
, space
);
1669 isl_basic_map_free(bmap
);
1673 __isl_give isl_basic_set
*isl_basic_set_eliminate(
1674 __isl_take isl_basic_set
*bset
,
1675 enum isl_dim_type type
, unsigned first
, unsigned n
)
1677 return isl_basic_map_eliminate(bset
, type
, first
, n
);
1680 /* Remove all constraints from "bmap" that reference any unknown local
1681 * variables (directly or indirectly).
1683 * Dropping all constraints on a local variable will make it redundant,
1684 * so it will get removed implicitly by
1685 * isl_basic_map_drop_constraints_involving_dims. Some other local
1686 * variables may also end up becoming redundant if they only appear
1687 * in constraints together with the unknown local variable.
1688 * Therefore, start over after calling
1689 * isl_basic_map_drop_constraints_involving_dims.
1691 __isl_give isl_basic_map
*isl_basic_map_drop_constraint_involving_unknown_divs(
1692 __isl_take isl_basic_map
*bmap
)
1695 int i
, n_div
, o_div
;
1697 known
= isl_basic_map_divs_known(bmap
);
1699 return isl_basic_map_free(bmap
);
1703 n_div
= isl_basic_map_dim(bmap
, isl_dim_div
);
1704 o_div
= isl_basic_map_offset(bmap
, isl_dim_div
) - 1;
1706 for (i
= 0; i
< n_div
; ++i
) {
1707 known
= isl_basic_map_div_is_known(bmap
, i
);
1709 return isl_basic_map_free(bmap
);
1712 bmap
= remove_dependent_vars(bmap
, o_div
+ i
);
1713 bmap
= isl_basic_map_drop_constraints_involving_dims(bmap
,
1717 n_div
= isl_basic_map_dim(bmap
, isl_dim_div
);
1724 /* Remove all constraints from "map" that reference any unknown local
1725 * variables (directly or indirectly).
1727 * Since constraints may get dropped from the basic maps,
1728 * they may no longer be disjoint from each other.
1730 __isl_give isl_map
*isl_map_drop_constraint_involving_unknown_divs(
1731 __isl_take isl_map
*map
)
1736 known
= isl_map_divs_known(map
);
1738 return isl_map_free(map
);
1742 map
= isl_map_cow(map
);
1746 for (i
= 0; i
< map
->n
; ++i
) {
1748 isl_basic_map_drop_constraint_involving_unknown_divs(
1751 return isl_map_free(map
);
1755 ISL_F_CLR(map
, ISL_MAP_DISJOINT
);
1760 /* Don't assume equalities are in order, because align_divs
1761 * may have changed the order of the divs.
1763 static void compute_elimination_index(__isl_keep isl_basic_map
*bmap
, int *elim
)
1768 total
= isl_space_dim(bmap
->dim
, isl_dim_all
);
1769 for (d
= 0; d
< total
; ++d
)
1771 for (i
= 0; i
< bmap
->n_eq
; ++i
) {
1772 for (d
= total
- 1; d
>= 0; --d
) {
1773 if (isl_int_is_zero(bmap
->eq
[i
][1+d
]))
1781 static void set_compute_elimination_index(__isl_keep isl_basic_set
*bset
,
1784 compute_elimination_index(bset_to_bmap(bset
), elim
);
1787 static int reduced_using_equalities(isl_int
*dst
, isl_int
*src
,
1788 __isl_keep isl_basic_map
*bmap
, int *elim
)
1794 total
= isl_space_dim(bmap
->dim
, isl_dim_all
);
1795 for (d
= total
- 1; d
>= 0; --d
) {
1796 if (isl_int_is_zero(src
[1+d
]))
1801 isl_seq_cpy(dst
, src
, 1 + total
);
1804 isl_seq_elim(dst
, bmap
->eq
[elim
[d
]], 1 + d
, 1 + total
, NULL
);
1809 static int set_reduced_using_equalities(isl_int
*dst
, isl_int
*src
,
1810 __isl_keep isl_basic_set
*bset
, int *elim
)
1812 return reduced_using_equalities(dst
, src
,
1813 bset_to_bmap(bset
), elim
);
1816 static __isl_give isl_basic_set
*isl_basic_set_reduce_using_equalities(
1817 __isl_take isl_basic_set
*bset
, __isl_take isl_basic_set
*context
)
1822 if (!bset
|| !context
)
1825 if (context
->n_eq
== 0) {
1826 isl_basic_set_free(context
);
1830 bset
= isl_basic_set_cow(bset
);
1834 elim
= isl_alloc_array(bset
->ctx
, int, isl_basic_set_n_dim(bset
));
1837 set_compute_elimination_index(context
, elim
);
1838 for (i
= 0; i
< bset
->n_eq
; ++i
)
1839 set_reduced_using_equalities(bset
->eq
[i
], bset
->eq
[i
],
1841 for (i
= 0; i
< bset
->n_ineq
; ++i
)
1842 set_reduced_using_equalities(bset
->ineq
[i
], bset
->ineq
[i
],
1844 isl_basic_set_free(context
);
1846 bset
= isl_basic_set_simplify(bset
);
1847 bset
= isl_basic_set_finalize(bset
);
1850 isl_basic_set_free(bset
);
1851 isl_basic_set_free(context
);
1855 /* For each inequality in "ineq" that is a shifted (more relaxed)
1856 * copy of an inequality in "context", mark the corresponding entry
1858 * If an inequality only has a non-negative constant term, then
1861 static isl_stat
mark_shifted_constraints(__isl_keep isl_mat
*ineq
,
1862 __isl_keep isl_basic_set
*context
, int *row
)
1864 struct isl_constraint_index ci
;
1869 if (!ineq
|| !context
)
1870 return isl_stat_error
;
1871 if (context
->n_ineq
== 0)
1873 if (setup_constraint_index(&ci
, context
) < 0)
1874 return isl_stat_error
;
1876 n_ineq
= isl_mat_rows(ineq
);
1877 total
= isl_mat_cols(ineq
) - 1;
1878 for (k
= 0; k
< n_ineq
; ++k
) {
1882 l
= isl_seq_first_non_zero(ineq
->row
[k
] + 1, total
);
1883 if (l
< 0 && isl_int_is_nonneg(ineq
->row
[k
][0])) {
1887 redundant
= constraint_index_is_redundant(&ci
, ineq
->row
[k
]);
1894 constraint_index_free(&ci
);
1897 constraint_index_free(&ci
);
1898 return isl_stat_error
;
1901 static __isl_give isl_basic_set
*remove_shifted_constraints(
1902 __isl_take isl_basic_set
*bset
, __isl_keep isl_basic_set
*context
)
1904 struct isl_constraint_index ci
;
1907 if (!bset
|| !context
)
1910 if (context
->n_ineq
== 0)
1912 if (setup_constraint_index(&ci
, context
) < 0)
1915 for (k
= 0; k
< bset
->n_ineq
; ++k
) {
1918 redundant
= constraint_index_is_redundant(&ci
, bset
->ineq
[k
]);
1923 bset
= isl_basic_set_cow(bset
);
1926 isl_basic_set_drop_inequality(bset
, k
);
1929 constraint_index_free(&ci
);
1932 constraint_index_free(&ci
);
1936 /* Remove constraints from "bmap" that are identical to constraints
1937 * in "context" or that are more relaxed (greater constant term).
1939 * We perform the test for shifted copies on the pure constraints
1940 * in remove_shifted_constraints.
1942 static __isl_give isl_basic_map
*isl_basic_map_remove_shifted_constraints(
1943 __isl_take isl_basic_map
*bmap
, __isl_take isl_basic_map
*context
)
1945 isl_basic_set
*bset
, *bset_context
;
1947 if (!bmap
|| !context
)
1950 if (bmap
->n_ineq
== 0 || context
->n_ineq
== 0) {
1951 isl_basic_map_free(context
);
1955 context
= isl_basic_map_align_divs(context
, bmap
);
1956 bmap
= isl_basic_map_align_divs(bmap
, context
);
1958 bset
= isl_basic_map_underlying_set(isl_basic_map_copy(bmap
));
1959 bset_context
= isl_basic_map_underlying_set(context
);
1960 bset
= remove_shifted_constraints(bset
, bset_context
);
1961 isl_basic_set_free(bset_context
);
1963 bmap
= isl_basic_map_overlying_set(bset
, bmap
);
1967 isl_basic_map_free(bmap
);
1968 isl_basic_map_free(context
);
1972 /* Does the (linear part of a) constraint "c" involve any of the "len"
1973 * "relevant" dimensions?
1975 static int is_related(isl_int
*c
, int len
, int *relevant
)
1979 for (i
= 0; i
< len
; ++i
) {
1982 if (!isl_int_is_zero(c
[i
]))
1989 /* Drop constraints from "bmap" that do not involve any of
1990 * the dimensions marked "relevant".
1992 static __isl_give isl_basic_map
*drop_unrelated_constraints(
1993 __isl_take isl_basic_map
*bmap
, int *relevant
)
1997 dim
= isl_basic_map_dim(bmap
, isl_dim_all
);
1998 for (i
= 0; i
< dim
; ++i
)
2004 for (i
= bmap
->n_eq
- 1; i
>= 0; --i
)
2005 if (!is_related(bmap
->eq
[i
] + 1, dim
, relevant
)) {
2006 bmap
= isl_basic_map_cow(bmap
);
2007 if (isl_basic_map_drop_equality(bmap
, i
) < 0)
2008 return isl_basic_map_free(bmap
);
2011 for (i
= bmap
->n_ineq
- 1; i
>= 0; --i
)
2012 if (!is_related(bmap
->ineq
[i
] + 1, dim
, relevant
)) {
2013 bmap
= isl_basic_map_cow(bmap
);
2014 if (isl_basic_map_drop_inequality(bmap
, i
) < 0)
2015 return isl_basic_map_free(bmap
);
2021 /* Update the groups in "group" based on the (linear part of a) constraint "c".
2023 * In particular, for any variable involved in the constraint,
2024 * find the actual group id from before and replace the group
2025 * of the corresponding variable by the minimal group of all
2026 * the variables involved in the constraint considered so far
2027 * (if this minimum is smaller) or replace the minimum by this group
2028 * (if the minimum is larger).
2030 * At the end, all the variables in "c" will (indirectly) point
2031 * to the minimal of the groups that they referred to originally.
2033 static void update_groups(int dim
, int *group
, isl_int
*c
)
2038 for (j
= 0; j
< dim
; ++j
) {
2039 if (isl_int_is_zero(c
[j
]))
2041 while (group
[j
] >= 0 && group
[group
[j
]] != group
[j
])
2042 group
[j
] = group
[group
[j
]];
2043 if (group
[j
] == min
)
2045 if (group
[j
] < min
) {
2046 if (min
>= 0 && min
< dim
)
2047 group
[min
] = group
[j
];
2050 group
[group
[j
]] = min
;
2054 /* Allocate an array of groups of variables, one for each variable
2055 * in "context", initialized to zero.
2057 static int *alloc_groups(__isl_keep isl_basic_set
*context
)
2062 dim
= isl_basic_set_dim(context
, isl_dim_set
);
2063 ctx
= isl_basic_set_get_ctx(context
);
2064 return isl_calloc_array(ctx
, int, dim
);
2067 /* Drop constraints from "bmap" that only involve variables that are
2068 * not related to any of the variables marked with a "-1" in "group".
2070 * We construct groups of variables that collect variables that
2071 * (indirectly) appear in some common constraint of "bmap".
2072 * Each group is identified by the first variable in the group,
2073 * except for the special group of variables that was already identified
2074 * in the input as -1 (or are related to those variables).
2075 * If group[i] is equal to i (or -1), then the group of i is i (or -1),
2076 * otherwise the group of i is the group of group[i].
2078 * We first initialize groups for the remaining variables.
2079 * Then we iterate over the constraints of "bmap" and update the
2080 * group of the variables in the constraint by the smallest group.
2081 * Finally, we resolve indirect references to groups by running over
2084 * After computing the groups, we drop constraints that do not involve
2085 * any variables in the -1 group.
2087 __isl_give isl_basic_map
*isl_basic_map_drop_unrelated_constraints(
2088 __isl_take isl_basic_map
*bmap
, __isl_take
int *group
)
2097 dim
= isl_basic_map_dim(bmap
, isl_dim_all
);
2100 for (i
= 0; i
< dim
; ++i
)
2102 last
= group
[i
] = i
;
2108 for (i
= 0; i
< bmap
->n_eq
; ++i
)
2109 update_groups(dim
, group
, bmap
->eq
[i
] + 1);
2110 for (i
= 0; i
< bmap
->n_ineq
; ++i
)
2111 update_groups(dim
, group
, bmap
->ineq
[i
] + 1);
2113 for (i
= 0; i
< dim
; ++i
)
2115 group
[i
] = group
[group
[i
]];
2117 for (i
= 0; i
< dim
; ++i
)
2118 group
[i
] = group
[i
] == -1;
2120 bmap
= drop_unrelated_constraints(bmap
, group
);
2126 /* Drop constraints from "context" that are irrelevant for computing
2127 * the gist of "bset".
2129 * In particular, drop constraints in variables that are not related
2130 * to any of the variables involved in the constraints of "bset"
2131 * in the sense that there is no sequence of constraints that connects them.
2133 * We first mark all variables that appear in "bset" as belonging
2134 * to a "-1" group and then continue with group_and_drop_irrelevant_constraints.
2136 static __isl_give isl_basic_set
*drop_irrelevant_constraints(
2137 __isl_take isl_basic_set
*context
, __isl_keep isl_basic_set
*bset
)
2143 if (!context
|| !bset
)
2144 return isl_basic_set_free(context
);
2146 group
= alloc_groups(context
);
2149 return isl_basic_set_free(context
);
2151 dim
= isl_basic_set_dim(bset
, isl_dim_set
);
2152 for (i
= 0; i
< dim
; ++i
) {
2153 for (j
= 0; j
< bset
->n_eq
; ++j
)
2154 if (!isl_int_is_zero(bset
->eq
[j
][1 + i
]))
2156 if (j
< bset
->n_eq
) {
2160 for (j
= 0; j
< bset
->n_ineq
; ++j
)
2161 if (!isl_int_is_zero(bset
->ineq
[j
][1 + i
]))
2163 if (j
< bset
->n_ineq
)
2167 return isl_basic_map_drop_unrelated_constraints(context
, group
);
2170 /* Drop constraints from "context" that are irrelevant for computing
2171 * the gist of the inequalities "ineq".
2172 * Inequalities in "ineq" for which the corresponding element of row
2173 * is set to -1 have already been marked for removal and should be ignored.
2175 * In particular, drop constraints in variables that are not related
2176 * to any of the variables involved in "ineq"
2177 * in the sense that there is no sequence of constraints that connects them.
2179 * We first mark all variables that appear in "bset" as belonging
2180 * to a "-1" group and then continue with group_and_drop_irrelevant_constraints.
2182 static __isl_give isl_basic_set
*drop_irrelevant_constraints_marked(
2183 __isl_take isl_basic_set
*context
, __isl_keep isl_mat
*ineq
, int *row
)
2189 if (!context
|| !ineq
)
2190 return isl_basic_set_free(context
);
2192 group
= alloc_groups(context
);
2195 return isl_basic_set_free(context
);
2197 dim
= isl_basic_set_dim(context
, isl_dim_set
);
2198 n
= isl_mat_rows(ineq
);
2199 for (i
= 0; i
< dim
; ++i
) {
2200 for (j
= 0; j
< n
; ++j
) {
2203 if (!isl_int_is_zero(ineq
->row
[j
][1 + i
]))
2210 return isl_basic_map_drop_unrelated_constraints(context
, group
);
2213 /* Do all "n" entries of "row" contain a negative value?
2215 static int all_neg(int *row
, int n
)
2219 for (i
= 0; i
< n
; ++i
)
2226 /* Update the inequalities in "bset" based on the information in "row"
2229 * In particular, the array "row" contains either -1, meaning that
2230 * the corresponding inequality of "bset" is redundant, or the index
2231 * of an inequality in "tab".
2233 * If the row entry is -1, then drop the inequality.
2234 * Otherwise, if the constraint is marked redundant in the tableau,
2235 * then drop the inequality. Similarly, if it is marked as an equality
2236 * in the tableau, then turn the inequality into an equality and
2237 * perform Gaussian elimination.
2239 static __isl_give isl_basic_set
*update_ineq(__isl_take isl_basic_set
*bset
,
2240 __isl_keep
int *row
, struct isl_tab
*tab
)
2245 int found_equality
= 0;
2249 if (tab
&& tab
->empty
)
2250 return isl_basic_set_set_to_empty(bset
);
2252 n_ineq
= bset
->n_ineq
;
2253 for (i
= n_ineq
- 1; i
>= 0; --i
) {
2255 if (isl_basic_set_drop_inequality(bset
, i
) < 0)
2256 return isl_basic_set_free(bset
);
2262 if (isl_tab_is_equality(tab
, n_eq
+ row
[i
])) {
2263 isl_basic_map_inequality_to_equality(bset
, i
);
2265 } else if (isl_tab_is_redundant(tab
, n_eq
+ row
[i
])) {
2266 if (isl_basic_set_drop_inequality(bset
, i
) < 0)
2267 return isl_basic_set_free(bset
);
2272 bset
= isl_basic_set_gauss(bset
, NULL
);
2273 bset
= isl_basic_set_finalize(bset
);
2277 /* Update the inequalities in "bset" based on the information in "row"
2278 * and "tab" and free all arguments (other than "bset").
2280 static __isl_give isl_basic_set
*update_ineq_free(
2281 __isl_take isl_basic_set
*bset
, __isl_take isl_mat
*ineq
,
2282 __isl_take isl_basic_set
*context
, __isl_take
int *row
,
2283 struct isl_tab
*tab
)
2286 isl_basic_set_free(context
);
2288 bset
= update_ineq(bset
, row
, tab
);
2295 /* Remove all information from bset that is redundant in the context
2297 * "ineq" contains the (possibly transformed) inequalities of "bset",
2298 * in the same order.
2299 * The (explicit) equalities of "bset" are assumed to have been taken
2300 * into account by the transformation such that only the inequalities
2302 * "context" is assumed not to be empty.
2304 * "row" keeps track of the constraint index of a "bset" inequality in "tab".
2305 * A value of -1 means that the inequality is obviously redundant and may
2306 * not even appear in "tab".
2308 * We first mark the inequalities of "bset"
2309 * that are obviously redundant with respect to some inequality in "context".
2310 * Then we remove those constraints from "context" that have become
2311 * irrelevant for computing the gist of "bset".
2312 * Note that this removal of constraints cannot be replaced by
2313 * a factorization because factors in "bset" may still be connected
2314 * to each other through constraints in "context".
2316 * If there are any inequalities left, we construct a tableau for
2317 * the context and then add the inequalities of "bset".
2318 * Before adding these inequalities, we freeze all constraints such that
2319 * they won't be considered redundant in terms of the constraints of "bset".
2320 * Then we detect all redundant constraints (among the
2321 * constraints that weren't frozen), first by checking for redundancy in the
2322 * the tableau and then by checking if replacing a constraint by its negation
2323 * would lead to an empty set. This last step is fairly expensive
2324 * and could be optimized by more reuse of the tableau.
2325 * Finally, we update bset according to the results.
2327 static __isl_give isl_basic_set
*uset_gist_full(__isl_take isl_basic_set
*bset
,
2328 __isl_take isl_mat
*ineq
, __isl_take isl_basic_set
*context
)
2333 isl_basic_set
*combined
= NULL
;
2334 struct isl_tab
*tab
= NULL
;
2335 unsigned n_eq
, context_ineq
;
2337 if (!bset
|| !ineq
|| !context
)
2340 if (bset
->n_ineq
== 0 || isl_basic_set_plain_is_universe(context
)) {
2341 isl_basic_set_free(context
);
2346 ctx
= isl_basic_set_get_ctx(context
);
2347 row
= isl_calloc_array(ctx
, int, bset
->n_ineq
);
2351 if (mark_shifted_constraints(ineq
, context
, row
) < 0)
2353 if (all_neg(row
, bset
->n_ineq
))
2354 return update_ineq_free(bset
, ineq
, context
, row
, NULL
);
2356 context
= drop_irrelevant_constraints_marked(context
, ineq
, row
);
2359 if (isl_basic_set_plain_is_universe(context
))
2360 return update_ineq_free(bset
, ineq
, context
, row
, NULL
);
2362 n_eq
= context
->n_eq
;
2363 context_ineq
= context
->n_ineq
;
2364 combined
= isl_basic_set_cow(isl_basic_set_copy(context
));
2365 combined
= isl_basic_set_extend_constraints(combined
, 0, bset
->n_ineq
);
2366 tab
= isl_tab_from_basic_set(combined
, 0);
2367 for (i
= 0; i
< context_ineq
; ++i
)
2368 if (isl_tab_freeze_constraint(tab
, n_eq
+ i
) < 0)
2370 if (isl_tab_extend_cons(tab
, bset
->n_ineq
) < 0)
2373 for (i
= 0; i
< bset
->n_ineq
; ++i
) {
2376 combined
= isl_basic_set_add_ineq(combined
, ineq
->row
[i
]);
2377 if (isl_tab_add_ineq(tab
, ineq
->row
[i
]) < 0)
2381 if (isl_tab_detect_implicit_equalities(tab
) < 0)
2383 if (isl_tab_detect_redundant(tab
) < 0)
2385 for (i
= bset
->n_ineq
- 1; i
>= 0; --i
) {
2386 isl_basic_set
*test
;
2392 if (tab
->con
[n_eq
+ r
].is_redundant
)
2394 test
= isl_basic_set_dup(combined
);
2395 if (isl_inequality_negate(test
, r
) < 0)
2396 test
= isl_basic_set_free(test
);
2397 test
= isl_basic_set_update_from_tab(test
, tab
);
2398 is_empty
= isl_basic_set_is_empty(test
);
2399 isl_basic_set_free(test
);
2403 tab
->con
[n_eq
+ r
].is_redundant
= 1;
2405 bset
= update_ineq_free(bset
, ineq
, context
, row
, tab
);
2407 ISL_F_SET(bset
, ISL_BASIC_SET_NO_IMPLICIT
);
2408 ISL_F_SET(bset
, ISL_BASIC_SET_NO_REDUNDANT
);
2411 isl_basic_set_free(combined
);
2417 isl_basic_set_free(combined
);
2418 isl_basic_set_free(context
);
2419 isl_basic_set_free(bset
);
2423 /* Extract the inequalities of "bset" as an isl_mat.
2425 static __isl_give isl_mat
*extract_ineq(__isl_keep isl_basic_set
*bset
)
2434 ctx
= isl_basic_set_get_ctx(bset
);
2435 total
= isl_basic_set_total_dim(bset
);
2436 ineq
= isl_mat_sub_alloc6(ctx
, bset
->ineq
, 0, bset
->n_ineq
,
2442 /* Remove all information from "bset" that is redundant in the context
2443 * of "context", for the case where both "bset" and "context" are
2446 static __isl_give isl_basic_set
*uset_gist_uncompressed(
2447 __isl_take isl_basic_set
*bset
, __isl_take isl_basic_set
*context
)
2451 ineq
= extract_ineq(bset
);
2452 return uset_gist_full(bset
, ineq
, context
);
2455 /* Remove all information from "bset" that is redundant in the context
2456 * of "context", for the case where the combined equalities of
2457 * "bset" and "context" allow for a compression that can be obtained
2458 * by preapplication of "T".
2460 * "bset" itself is not transformed by "T". Instead, the inequalities
2461 * are extracted from "bset" and those are transformed by "T".
2462 * uset_gist_full then determines which of the transformed inequalities
2463 * are redundant with respect to the transformed "context" and removes
2464 * the corresponding inequalities from "bset".
2466 * After preapplying "T" to the inequalities, any common factor is
2467 * removed from the coefficients. If this results in a tightening
2468 * of the constant term, then the same tightening is applied to
2469 * the corresponding untransformed inequality in "bset".
2470 * That is, if after plugging in T, a constraint f(x) >= 0 is of the form
2474 * with 0 <= r < g, then it is equivalent to
2478 * This means that f(x) >= 0 is equivalent to f(x) - r >= 0 in the affine
2479 * subspace compressed by T since the latter would be transformed to
2483 static __isl_give isl_basic_set
*uset_gist_compressed(
2484 __isl_take isl_basic_set
*bset
, __isl_take isl_basic_set
*context
,
2485 __isl_take isl_mat
*T
)
2489 int i
, n_row
, n_col
;
2492 ineq
= extract_ineq(bset
);
2493 ineq
= isl_mat_product(ineq
, isl_mat_copy(T
));
2494 context
= isl_basic_set_preimage(context
, T
);
2496 if (!ineq
|| !context
)
2498 if (isl_basic_set_plain_is_empty(context
)) {
2500 isl_basic_set_free(context
);
2501 return isl_basic_set_set_to_empty(bset
);
2504 ctx
= isl_mat_get_ctx(ineq
);
2505 n_row
= isl_mat_rows(ineq
);
2506 n_col
= isl_mat_cols(ineq
);
2508 for (i
= 0; i
< n_row
; ++i
) {
2509 isl_seq_gcd(ineq
->row
[i
] + 1, n_col
- 1, &ctx
->normalize_gcd
);
2510 if (isl_int_is_zero(ctx
->normalize_gcd
))
2512 if (isl_int_is_one(ctx
->normalize_gcd
))
2514 isl_seq_scale_down(ineq
->row
[i
] + 1, ineq
->row
[i
] + 1,
2515 ctx
->normalize_gcd
, n_col
- 1);
2516 isl_int_fdiv_r(rem
, ineq
->row
[i
][0], ctx
->normalize_gcd
);
2517 isl_int_fdiv_q(ineq
->row
[i
][0],
2518 ineq
->row
[i
][0], ctx
->normalize_gcd
);
2519 if (isl_int_is_zero(rem
))
2521 bset
= isl_basic_set_cow(bset
);
2524 isl_int_sub(bset
->ineq
[i
][0], bset
->ineq
[i
][0], rem
);
2528 return uset_gist_full(bset
, ineq
, context
);
2531 isl_basic_set_free(context
);
2532 isl_basic_set_free(bset
);
2536 /* Project "bset" onto the variables that are involved in "template".
2538 static __isl_give isl_basic_set
*project_onto_involved(
2539 __isl_take isl_basic_set
*bset
, __isl_keep isl_basic_set
*template)
2543 if (!bset
|| !template)
2544 return isl_basic_set_free(bset
);
2546 n
= isl_basic_set_dim(template, isl_dim_set
);
2548 for (i
= 0; i
< n
; ++i
) {
2551 involved
= isl_basic_set_involves_dims(template,
2554 return isl_basic_set_free(bset
);
2557 bset
= isl_basic_set_eliminate_vars(bset
, i
, 1);
2563 /* Remove all information from bset that is redundant in the context
2564 * of context. In particular, equalities that are linear combinations
2565 * of those in context are removed. Then the inequalities that are
2566 * redundant in the context of the equalities and inequalities of
2567 * context are removed.
2569 * First of all, we drop those constraints from "context"
2570 * that are irrelevant for computing the gist of "bset".
2571 * Alternatively, we could factorize the intersection of "context" and "bset".
2573 * We first compute the intersection of the integer affine hulls
2574 * of "bset" and "context",
2575 * compute the gist inside this intersection and then reduce
2576 * the constraints with respect to the equalities of the context
2577 * that only involve variables already involved in the input.
2579 * If two constraints are mutually redundant, then uset_gist_full
2580 * will remove the second of those constraints. We therefore first
2581 * sort the constraints so that constraints not involving existentially
2582 * quantified variables are given precedence over those that do.
2583 * We have to perform this sorting before the variable compression,
2584 * because that may effect the order of the variables.
2586 static __isl_give isl_basic_set
*uset_gist(__isl_take isl_basic_set
*bset
,
2587 __isl_take isl_basic_set
*context
)
2592 isl_basic_set
*aff_context
;
2595 if (!bset
|| !context
)
2598 context
= drop_irrelevant_constraints(context
, bset
);
2600 bset
= isl_basic_set_detect_equalities(bset
);
2601 aff
= isl_basic_set_copy(bset
);
2602 aff
= isl_basic_set_plain_affine_hull(aff
);
2603 context
= isl_basic_set_detect_equalities(context
);
2604 aff_context
= isl_basic_set_copy(context
);
2605 aff_context
= isl_basic_set_plain_affine_hull(aff_context
);
2606 aff
= isl_basic_set_intersect(aff
, aff_context
);
2609 if (isl_basic_set_plain_is_empty(aff
)) {
2610 isl_basic_set_free(bset
);
2611 isl_basic_set_free(context
);
2614 bset
= isl_basic_set_sort_constraints(bset
);
2615 if (aff
->n_eq
== 0) {
2616 isl_basic_set_free(aff
);
2617 return uset_gist_uncompressed(bset
, context
);
2619 total
= isl_basic_set_total_dim(bset
);
2620 eq
= isl_mat_sub_alloc6(bset
->ctx
, aff
->eq
, 0, aff
->n_eq
, 0, 1 + total
);
2621 eq
= isl_mat_cow(eq
);
2622 T
= isl_mat_variable_compression(eq
, NULL
);
2623 isl_basic_set_free(aff
);
2624 if (T
&& T
->n_col
== 0) {
2626 isl_basic_set_free(context
);
2627 return isl_basic_set_set_to_empty(bset
);
2630 aff_context
= isl_basic_set_affine_hull(isl_basic_set_copy(context
));
2631 aff_context
= project_onto_involved(aff_context
, bset
);
2633 bset
= uset_gist_compressed(bset
, context
, T
);
2634 bset
= isl_basic_set_reduce_using_equalities(bset
, aff_context
);
2637 ISL_F_SET(bset
, ISL_BASIC_SET_NO_IMPLICIT
);
2638 ISL_F_SET(bset
, ISL_BASIC_SET_NO_REDUNDANT
);
2643 isl_basic_set_free(bset
);
2644 isl_basic_set_free(context
);
2648 /* Return the number of equality constraints in "bmap" that involve
2649 * local variables. This function assumes that Gaussian elimination
2650 * has been applied to the equality constraints.
2652 static int n_div_eq(__isl_keep isl_basic_map
*bmap
)
2660 if (bmap
->n_eq
== 0)
2663 total
= isl_basic_map_dim(bmap
, isl_dim_all
);
2664 n_div
= isl_basic_map_dim(bmap
, isl_dim_div
);
2667 for (i
= 0; i
< bmap
->n_eq
; ++i
)
2668 if (isl_seq_first_non_zero(bmap
->eq
[i
] + 1 + total
,
2675 /* Construct a basic map in "space" defined by the equality constraints in "eq".
2676 * The constraints are assumed not to involve any local variables.
2678 static __isl_give isl_basic_map
*basic_map_from_equalities(
2679 __isl_take isl_space
*space
, __isl_take isl_mat
*eq
)
2682 isl_basic_map
*bmap
= NULL
;
2687 if (1 + isl_space_dim(space
, isl_dim_all
) != eq
->n_col
)
2688 isl_die(isl_space_get_ctx(space
), isl_error_internal
,
2689 "unexpected number of columns", goto error
);
2691 bmap
= isl_basic_map_alloc_space(isl_space_copy(space
),
2693 for (i
= 0; i
< eq
->n_row
; ++i
) {
2694 k
= isl_basic_map_alloc_equality(bmap
);
2697 isl_seq_cpy(bmap
->eq
[k
], eq
->row
[i
], eq
->n_col
);
2700 isl_space_free(space
);
2704 isl_space_free(space
);
2706 isl_basic_map_free(bmap
);
2710 /* Construct and return a variable compression based on the equality
2711 * constraints in "bmap1" and "bmap2" that do not involve the local variables.
2712 * "n1" is the number of (initial) equality constraints in "bmap1"
2713 * that do involve local variables.
2714 * "n2" is the number of (initial) equality constraints in "bmap2"
2715 * that do involve local variables.
2716 * "total" is the total number of other variables.
2717 * This function assumes that Gaussian elimination
2718 * has been applied to the equality constraints in both "bmap1" and "bmap2"
2719 * such that the equality constraints not involving local variables
2720 * are those that start at "n1" or "n2".
2722 * If either of "bmap1" and "bmap2" does not have such equality constraints,
2723 * then simply compute the compression based on the equality constraints
2724 * in the other basic map.
2725 * Otherwise, combine the equality constraints from both into a new
2726 * basic map such that Gaussian elimination can be applied to this combination
2727 * and then construct a variable compression from the resulting
2728 * equality constraints.
2730 static __isl_give isl_mat
*combined_variable_compression(
2731 __isl_keep isl_basic_map
*bmap1
, int n1
,
2732 __isl_keep isl_basic_map
*bmap2
, int n2
, int total
)
2735 isl_mat
*E1
, *E2
, *V
;
2736 isl_basic_map
*bmap
;
2738 ctx
= isl_basic_map_get_ctx(bmap1
);
2739 if (bmap1
->n_eq
== n1
) {
2740 E2
= isl_mat_sub_alloc6(ctx
, bmap2
->eq
,
2741 n2
, bmap2
->n_eq
- n2
, 0, 1 + total
);
2742 return isl_mat_variable_compression(E2
, NULL
);
2744 if (bmap2
->n_eq
== n2
) {
2745 E1
= isl_mat_sub_alloc6(ctx
, bmap1
->eq
,
2746 n1
, bmap1
->n_eq
- n1
, 0, 1 + total
);
2747 return isl_mat_variable_compression(E1
, NULL
);
2749 E1
= isl_mat_sub_alloc6(ctx
, bmap1
->eq
,
2750 n1
, bmap1
->n_eq
- n1
, 0, 1 + total
);
2751 E2
= isl_mat_sub_alloc6(ctx
, bmap2
->eq
,
2752 n2
, bmap2
->n_eq
- n2
, 0, 1 + total
);
2753 E1
= isl_mat_concat(E1
, E2
);
2754 bmap
= basic_map_from_equalities(isl_basic_map_get_space(bmap1
), E1
);
2755 bmap
= isl_basic_map_gauss(bmap
, NULL
);
2758 E1
= isl_mat_sub_alloc6(ctx
, bmap
->eq
, 0, bmap
->n_eq
, 0, 1 + total
);
2759 V
= isl_mat_variable_compression(E1
, NULL
);
2760 isl_basic_map_free(bmap
);
2765 /* Extract the stride constraints from "bmap", compressed
2766 * with respect to both the stride constraints in "context" and
2767 * the remaining equality constraints in both "bmap" and "context".
2768 * "bmap_n_eq" is the number of (initial) stride constraints in "bmap".
2769 * "context_n_eq" is the number of (initial) stride constraints in "context".
2771 * Let x be all variables in "bmap" (and "context") other than the local
2772 * variables. First compute a variable compression
2776 * based on the non-stride equality constraints in "bmap" and "context".
2777 * Consider the stride constraints of "context",
2781 * with y the local variables and plug in the variable compression,
2784 * A(V x') + B(y) = 0
2786 * Use these constraints to compute a parameter compression on x'
2790 * Now consider the stride constraints of "bmap"
2794 * and plug in x = V*T x''.
2795 * That is, return A = [C*V*T D].
2797 static __isl_give isl_mat
*extract_compressed_stride_constraints(
2798 __isl_keep isl_basic_map
*bmap
, int bmap_n_eq
,
2799 __isl_keep isl_basic_map
*context
, int context_n_eq
)
2803 isl_mat
*A
, *B
, *T
, *V
;
2805 total
= isl_basic_map_dim(context
, isl_dim_all
);
2806 n_div
= isl_basic_map_dim(context
, isl_dim_div
);
2809 ctx
= isl_basic_map_get_ctx(bmap
);
2811 V
= combined_variable_compression(bmap
, bmap_n_eq
,
2812 context
, context_n_eq
, total
);
2814 A
= isl_mat_sub_alloc6(ctx
, context
->eq
, 0, context_n_eq
, 0, 1 + total
);
2815 B
= isl_mat_sub_alloc6(ctx
, context
->eq
,
2816 0, context_n_eq
, 1 + total
, n_div
);
2817 A
= isl_mat_product(A
, isl_mat_copy(V
));
2818 T
= isl_mat_parameter_compression_ext(A
, B
);
2819 T
= isl_mat_product(V
, T
);
2821 n_div
= isl_basic_map_dim(bmap
, isl_dim_div
);
2822 T
= isl_mat_diagonal(T
, isl_mat_identity(ctx
, n_div
));
2824 A
= isl_mat_sub_alloc6(ctx
, bmap
->eq
,
2825 0, bmap_n_eq
, 0, 1 + total
+ n_div
);
2826 A
= isl_mat_product(A
, T
);
2831 /* Remove the prime factors from *g that have an exponent that
2832 * is strictly smaller than the exponent in "c".
2833 * All exponents in *g are known to be smaller than or equal
2836 * That is, if *g is equal to
2838 * p_1^{e_1} p_2^{e_2} ... p_n^{e_n}
2840 * and "c" is equal to
2842 * p_1^{f_1} p_2^{f_2} ... p_n^{f_n}
2846 * p_1^{e_1 * (e_1 = f_1)} p_2^{e_2 * (e_2 = f_2)} ...
2847 * p_n^{e_n * (e_n = f_n)}
2849 * If e_i = f_i, then c / *g does not have any p_i factors and therefore
2850 * neither does the gcd of *g and c / *g.
2851 * If e_i < f_i, then the gcd of *g and c / *g has a positive
2852 * power min(e_i, s_i) of p_i with s_i = f_i - e_i among its factors.
2853 * Dividing *g by this gcd therefore strictly reduces the exponent
2854 * of the prime factors that need to be removed, while leaving the
2855 * other prime factors untouched.
2856 * Repeating this process until gcd(*g, c / *g) = 1 therefore
2857 * removes all undesired factors, without removing any others.
2859 static void remove_incomplete_powers(isl_int
*g
, isl_int c
)
2865 isl_int_divexact(t
, c
, *g
);
2866 isl_int_gcd(t
, t
, *g
);
2867 if (isl_int_is_one(t
))
2869 isl_int_divexact(*g
, *g
, t
);
2874 /* Reduce the "n" stride constraints in "bmap" based on a copy "A"
2875 * of the same stride constraints in a compressed space that exploits
2876 * all equalities in the context and the other equalities in "bmap".
2878 * If the stride constraints of "bmap" are of the form
2882 * then A is of the form
2886 * If any of these constraints involves only a single local variable y,
2887 * then the constraint appears as
2897 * Let g be the gcd of m and the coefficients of h.
2898 * Then, in particular, g is a divisor of the coefficients of h and
2902 * is known to be a multiple of g.
2903 * If some prime factor in m appears with the same exponent in g,
2904 * then it can be removed from m because f(x) is already known
2905 * to be a multiple of g and therefore in particular of this power
2906 * of the prime factors.
2907 * Prime factors that appear with a smaller exponent in g cannot
2908 * be removed from m.
2909 * Let g' be the divisor of g containing all prime factors that
2910 * appear with the same exponent in m and g, then
2914 * can be replaced by
2916 * f(x) + m/g' y_i' = 0
2918 * Note that (if g' != 1) this changes the explicit representation
2919 * of y_i to that of y_i', so the integer division at position i
2920 * is marked unknown and later recomputed by a call to
2921 * isl_basic_map_gauss.
2923 static __isl_give isl_basic_map
*reduce_stride_constraints(
2924 __isl_take isl_basic_map
*bmap
, int n
, __isl_keep isl_mat
*A
)
2932 return isl_basic_map_free(bmap
);
2934 total
= isl_basic_map_dim(bmap
, isl_dim_all
);
2935 n_div
= isl_basic_map_dim(bmap
, isl_dim_div
);
2939 for (i
= 0; i
< n
; ++i
) {
2942 div
= isl_seq_first_non_zero(bmap
->eq
[i
] + 1 + total
, n_div
);
2944 isl_die(isl_basic_map_get_ctx(bmap
), isl_error_internal
,
2945 "equality constraints modified unexpectedly",
2947 if (isl_seq_first_non_zero(bmap
->eq
[i
] + 1 + total
+ div
+ 1,
2948 n_div
- div
- 1) != -1)
2950 if (isl_mat_row_gcd(A
, i
, &gcd
) < 0)
2952 if (isl_int_is_one(gcd
))
2954 remove_incomplete_powers(&gcd
, bmap
->eq
[i
][1 + total
+ div
]);
2955 if (isl_int_is_one(gcd
))
2957 isl_int_divexact(bmap
->eq
[i
][1 + total
+ div
],
2958 bmap
->eq
[i
][1 + total
+ div
], gcd
);
2959 bmap
= isl_basic_map_mark_div_unknown(bmap
, div
);
2967 bmap
= isl_basic_map_gauss(bmap
, NULL
);
2972 isl_basic_map_free(bmap
);
2976 /* Simplify the stride constraints in "bmap" based on
2977 * the remaining equality constraints in "bmap" and all equality
2978 * constraints in "context".
2979 * Only do this if both "bmap" and "context" have stride constraints.
2981 * First extract a copy of the stride constraints in "bmap" in a compressed
2982 * space exploiting all the other equality constraints and then
2983 * use this compressed copy to simplify the original stride constraints.
2985 static __isl_give isl_basic_map
*gist_strides(__isl_take isl_basic_map
*bmap
,
2986 __isl_keep isl_basic_map
*context
)
2988 int bmap_n_eq
, context_n_eq
;
2991 if (!bmap
|| !context
)
2992 return isl_basic_map_free(bmap
);
2994 bmap_n_eq
= n_div_eq(bmap
);
2995 context_n_eq
= n_div_eq(context
);
2997 if (bmap_n_eq
< 0 || context_n_eq
< 0)
2998 return isl_basic_map_free(bmap
);
2999 if (bmap_n_eq
== 0 || context_n_eq
== 0)
3002 A
= extract_compressed_stride_constraints(bmap
, bmap_n_eq
,
3003 context
, context_n_eq
);
3004 bmap
= reduce_stride_constraints(bmap
, bmap_n_eq
, A
);
3011 /* Return a basic map that has the same intersection with "context" as "bmap"
3012 * and that is as "simple" as possible.
3014 * The core computation is performed on the pure constraints.
3015 * When we add back the meaning of the integer divisions, we need
3016 * to (re)introduce the div constraints. If we happen to have
3017 * discovered that some of these integer divisions are equal to
3018 * some affine combination of other variables, then these div
3019 * constraints may end up getting simplified in terms of the equalities,
3020 * resulting in extra inequalities on the other variables that
3021 * may have been removed already or that may not even have been
3022 * part of the input. We try and remove those constraints of
3023 * this form that are most obviously redundant with respect to
3024 * the context. We also remove those div constraints that are
3025 * redundant with respect to the other constraints in the result.
3027 * The stride constraints among the equality constraints in "bmap" are
3028 * also simplified with respecting to the other equality constraints
3029 * in "bmap" and with respect to all equality constraints in "context".
3031 __isl_give isl_basic_map
*isl_basic_map_gist(__isl_take isl_basic_map
*bmap
,
3032 __isl_take isl_basic_map
*context
)
3034 isl_basic_set
*bset
, *eq
;
3035 isl_basic_map
*eq_bmap
;
3036 unsigned total
, n_div
, extra
, n_eq
, n_ineq
;
3038 if (!bmap
|| !context
)
3041 if (isl_basic_map_plain_is_universe(bmap
)) {
3042 isl_basic_map_free(context
);
3045 if (isl_basic_map_plain_is_empty(context
)) {
3046 isl_space
*space
= isl_basic_map_get_space(bmap
);
3047 isl_basic_map_free(bmap
);
3048 isl_basic_map_free(context
);
3049 return isl_basic_map_universe(space
);
3051 if (isl_basic_map_plain_is_empty(bmap
)) {
3052 isl_basic_map_free(context
);
3056 bmap
= isl_basic_map_remove_redundancies(bmap
);
3057 context
= isl_basic_map_remove_redundancies(context
);
3058 context
= isl_basic_map_align_divs(context
, bmap
);
3062 n_div
= isl_basic_map_dim(context
, isl_dim_div
);
3063 total
= isl_basic_map_dim(bmap
, isl_dim_all
);
3064 extra
= n_div
- isl_basic_map_dim(bmap
, isl_dim_div
);
3066 bset
= isl_basic_map_underlying_set(isl_basic_map_copy(bmap
));
3067 bset
= isl_basic_set_add_dims(bset
, isl_dim_set
, extra
);
3068 bset
= uset_gist(bset
,
3069 isl_basic_map_underlying_set(isl_basic_map_copy(context
)));
3070 bset
= isl_basic_set_project_out(bset
, isl_dim_set
, total
, extra
);
3072 if (!bset
|| bset
->n_eq
== 0 || n_div
== 0 ||
3073 isl_basic_set_plain_is_empty(bset
)) {
3074 isl_basic_map_free(context
);
3075 return isl_basic_map_overlying_set(bset
, bmap
);
3079 n_ineq
= bset
->n_ineq
;
3080 eq
= isl_basic_set_copy(bset
);
3081 eq
= isl_basic_set_cow(eq
);
3082 if (isl_basic_set_free_inequality(eq
, n_ineq
) < 0)
3083 eq
= isl_basic_set_free(eq
);
3084 if (isl_basic_set_free_equality(bset
, n_eq
) < 0)
3085 bset
= isl_basic_set_free(bset
);
3087 eq_bmap
= isl_basic_map_overlying_set(eq
, isl_basic_map_copy(bmap
));
3088 eq_bmap
= gist_strides(eq_bmap
, context
);
3089 eq_bmap
= isl_basic_map_remove_shifted_constraints(eq_bmap
, context
);
3090 bmap
= isl_basic_map_overlying_set(bset
, bmap
);
3091 bmap
= isl_basic_map_intersect(bmap
, eq_bmap
);
3092 bmap
= isl_basic_map_remove_redundancies(bmap
);
3096 isl_basic_map_free(bmap
);
3097 isl_basic_map_free(context
);
3102 * Assumes context has no implicit divs.
3104 __isl_give isl_map
*isl_map_gist_basic_map(__isl_take isl_map
*map
,
3105 __isl_take isl_basic_map
*context
)
3109 if (!map
|| !context
)
3112 if (isl_basic_map_plain_is_empty(context
)) {
3113 isl_space
*space
= isl_map_get_space(map
);
3115 isl_basic_map_free(context
);
3116 return isl_map_universe(space
);
3119 context
= isl_basic_map_remove_redundancies(context
);
3120 map
= isl_map_cow(map
);
3121 if (!map
|| !context
)
3123 isl_assert(map
->ctx
, isl_space_is_equal(map
->dim
, context
->dim
), goto error
);
3124 map
= isl_map_compute_divs(map
);
3127 for (i
= map
->n
- 1; i
>= 0; --i
) {
3128 map
->p
[i
] = isl_basic_map_gist(map
->p
[i
],
3129 isl_basic_map_copy(context
));
3132 if (isl_basic_map_plain_is_empty(map
->p
[i
])) {
3133 isl_basic_map_free(map
->p
[i
]);
3134 if (i
!= map
->n
- 1)
3135 map
->p
[i
] = map
->p
[map
->n
- 1];
3139 isl_basic_map_free(context
);
3140 ISL_F_CLR(map
, ISL_MAP_NORMALIZED
);
3144 isl_basic_map_free(context
);
3148 /* Drop all inequalities from "bmap" that also appear in "context".
3149 * "context" is assumed to have only known local variables and
3150 * the initial local variables of "bmap" are assumed to be the same
3151 * as those of "context".
3152 * The constraints of both "bmap" and "context" are assumed
3153 * to have been sorted using isl_basic_map_sort_constraints.
3155 * Run through the inequality constraints of "bmap" and "context"
3157 * If a constraint of "bmap" involves variables not in "context",
3158 * then it cannot appear in "context".
3159 * If a matching constraint is found, it is removed from "bmap".
3161 static __isl_give isl_basic_map
*drop_inequalities(
3162 __isl_take isl_basic_map
*bmap
, __isl_keep isl_basic_map
*context
)
3165 unsigned total
, extra
;
3167 if (!bmap
|| !context
)
3168 return isl_basic_map_free(bmap
);
3170 total
= isl_basic_map_total_dim(context
);
3171 extra
= isl_basic_map_total_dim(bmap
) - total
;
3173 i1
= bmap
->n_ineq
- 1;
3174 i2
= context
->n_ineq
- 1;
3175 while (bmap
&& i1
>= 0 && i2
>= 0) {
3178 if (isl_seq_first_non_zero(bmap
->ineq
[i1
] + 1 + total
,
3183 cmp
= isl_basic_map_constraint_cmp(context
, bmap
->ineq
[i1
],
3193 if (isl_int_eq(bmap
->ineq
[i1
][0], context
->ineq
[i2
][0])) {
3194 bmap
= isl_basic_map_cow(bmap
);
3195 if (isl_basic_map_drop_inequality(bmap
, i1
) < 0)
3196 bmap
= isl_basic_map_free(bmap
);
3205 /* Drop all equalities from "bmap" that also appear in "context".
3206 * "context" is assumed to have only known local variables and
3207 * the initial local variables of "bmap" are assumed to be the same
3208 * as those of "context".
3210 * Run through the equality constraints of "bmap" and "context"
3212 * If a constraint of "bmap" involves variables not in "context",
3213 * then it cannot appear in "context".
3214 * If a matching constraint is found, it is removed from "bmap".
3216 static __isl_give isl_basic_map
*drop_equalities(
3217 __isl_take isl_basic_map
*bmap
, __isl_keep isl_basic_map
*context
)
3220 unsigned total
, extra
;
3222 if (!bmap
|| !context
)
3223 return isl_basic_map_free(bmap
);
3225 total
= isl_basic_map_total_dim(context
);
3226 extra
= isl_basic_map_total_dim(bmap
) - total
;
3228 i1
= bmap
->n_eq
- 1;
3229 i2
= context
->n_eq
- 1;
3231 while (bmap
&& i1
>= 0 && i2
>= 0) {
3234 if (isl_seq_first_non_zero(bmap
->eq
[i1
] + 1 + total
,
3237 last1
= isl_seq_last_non_zero(bmap
->eq
[i1
] + 1, total
);
3238 last2
= isl_seq_last_non_zero(context
->eq
[i2
] + 1, total
);
3239 if (last1
> last2
) {
3243 if (last1
< last2
) {
3247 if (isl_seq_eq(bmap
->eq
[i1
], context
->eq
[i2
], 1 + total
)) {
3248 bmap
= isl_basic_map_cow(bmap
);
3249 if (isl_basic_map_drop_equality(bmap
, i1
) < 0)
3250 bmap
= isl_basic_map_free(bmap
);
3259 /* Remove the constraints in "context" from "bmap".
3260 * "context" is assumed to have explicit representations
3261 * for all local variables.
3263 * First align the divs of "bmap" to those of "context" and
3264 * sort the constraints. Then drop all constraints from "bmap"
3265 * that appear in "context".
3267 __isl_give isl_basic_map
*isl_basic_map_plain_gist(
3268 __isl_take isl_basic_map
*bmap
, __isl_take isl_basic_map
*context
)
3270 isl_bool done
, known
;
3272 done
= isl_basic_map_plain_is_universe(context
);
3273 if (done
== isl_bool_false
)
3274 done
= isl_basic_map_plain_is_universe(bmap
);
3275 if (done
== isl_bool_false
)
3276 done
= isl_basic_map_plain_is_empty(context
);
3277 if (done
== isl_bool_false
)
3278 done
= isl_basic_map_plain_is_empty(bmap
);
3282 isl_basic_map_free(context
);
3285 known
= isl_basic_map_divs_known(context
);
3289 isl_die(isl_basic_map_get_ctx(bmap
), isl_error_invalid
,
3290 "context has unknown divs", goto error
);
3292 bmap
= isl_basic_map_align_divs(bmap
, context
);
3293 bmap
= isl_basic_map_gauss(bmap
, NULL
);
3294 bmap
= isl_basic_map_sort_constraints(bmap
);
3295 context
= isl_basic_map_sort_constraints(context
);
3297 bmap
= drop_inequalities(bmap
, context
);
3298 bmap
= drop_equalities(bmap
, context
);
3300 isl_basic_map_free(context
);
3301 bmap
= isl_basic_map_finalize(bmap
);
3304 isl_basic_map_free(bmap
);
3305 isl_basic_map_free(context
);
3309 /* Replace "map" by the disjunct at position "pos" and free "context".
3311 static __isl_give isl_map
*replace_by_disjunct(__isl_take isl_map
*map
,
3312 int pos
, __isl_take isl_basic_map
*context
)
3314 isl_basic_map
*bmap
;
3316 bmap
= isl_basic_map_copy(map
->p
[pos
]);
3318 isl_basic_map_free(context
);
3319 return isl_map_from_basic_map(bmap
);
3322 /* Remove the constraints in "context" from "map".
3323 * If any of the disjuncts in the result turns out to be the universe,
3324 * then return this universe.
3325 * "context" is assumed to have explicit representations
3326 * for all local variables.
3328 __isl_give isl_map
*isl_map_plain_gist_basic_map(__isl_take isl_map
*map
,
3329 __isl_take isl_basic_map
*context
)
3332 isl_bool univ
, known
;
3334 univ
= isl_basic_map_plain_is_universe(context
);
3338 isl_basic_map_free(context
);
3341 known
= isl_basic_map_divs_known(context
);
3345 isl_die(isl_map_get_ctx(map
), isl_error_invalid
,
3346 "context has unknown divs", goto error
);
3348 map
= isl_map_cow(map
);
3351 for (i
= 0; i
< map
->n
; ++i
) {
3352 map
->p
[i
] = isl_basic_map_plain_gist(map
->p
[i
],
3353 isl_basic_map_copy(context
));
3354 univ
= isl_basic_map_plain_is_universe(map
->p
[i
]);
3357 if (univ
&& map
->n
> 1)
3358 return replace_by_disjunct(map
, i
, context
);
3361 isl_basic_map_free(context
);
3362 ISL_F_CLR(map
, ISL_MAP_NORMALIZED
);
3364 ISL_F_CLR(map
, ISL_MAP_DISJOINT
);
3368 isl_basic_map_free(context
);
3372 /* Remove the constraints in "context" from "set".
3373 * If any of the disjuncts in the result turns out to be the universe,
3374 * then return this universe.
3375 * "context" is assumed to have explicit representations
3376 * for all local variables.
3378 __isl_give isl_set
*isl_set_plain_gist_basic_set(__isl_take isl_set
*set
,
3379 __isl_take isl_basic_set
*context
)
3381 return set_from_map(isl_map_plain_gist_basic_map(set_to_map(set
),
3382 bset_to_bmap(context
)));
3385 /* Remove the constraints in "context" from "map".
3386 * If any of the disjuncts in the result turns out to be the universe,
3387 * then return this universe.
3388 * "context" is assumed to consist of a single disjunct and
3389 * to have explicit representations for all local variables.
3391 __isl_give isl_map
*isl_map_plain_gist(__isl_take isl_map
*map
,
3392 __isl_take isl_map
*context
)
3394 isl_basic_map
*hull
;
3396 hull
= isl_map_unshifted_simple_hull(context
);
3397 return isl_map_plain_gist_basic_map(map
, hull
);
3400 /* Replace "map" by a universe map in the same space and free "drop".
3402 static __isl_give isl_map
*replace_by_universe(__isl_take isl_map
*map
,
3403 __isl_take isl_map
*drop
)
3407 res
= isl_map_universe(isl_map_get_space(map
));
3413 /* Return a map that has the same intersection with "context" as "map"
3414 * and that is as "simple" as possible.
3416 * If "map" is already the universe, then we cannot make it any simpler.
3417 * Similarly, if "context" is the universe, then we cannot exploit it
3419 * If "map" and "context" are identical to each other, then we can
3420 * return the corresponding universe.
3422 * If either "map" or "context" consists of multiple disjuncts,
3423 * then check if "context" happens to be a subset of "map",
3424 * in which case all constraints can be removed.
3425 * In case of multiple disjuncts, the standard procedure
3426 * may not be able to detect that all constraints can be removed.
3428 * If none of these cases apply, we have to work a bit harder.
3429 * During this computation, we make use of a single disjunct context,
3430 * so if the original context consists of more than one disjunct
3431 * then we need to approximate the context by a single disjunct set.
3432 * Simply taking the simple hull may drop constraints that are
3433 * only implicitly available in each disjunct. We therefore also
3434 * look for constraints among those defining "map" that are valid
3435 * for the context. These can then be used to simplify away
3436 * the corresponding constraints in "map".
3438 static __isl_give isl_map
*map_gist(__isl_take isl_map
*map
,
3439 __isl_take isl_map
*context
)
3443 int single_disjunct_map
, single_disjunct_context
;
3445 isl_basic_map
*hull
;
3447 is_universe
= isl_map_plain_is_universe(map
);
3448 if (is_universe
>= 0 && !is_universe
)
3449 is_universe
= isl_map_plain_is_universe(context
);
3450 if (is_universe
< 0)
3453 isl_map_free(context
);
3457 equal
= isl_map_plain_is_equal(map
, context
);
3461 return replace_by_universe(map
, context
);
3463 single_disjunct_map
= isl_map_n_basic_map(map
) == 1;
3464 single_disjunct_context
= isl_map_n_basic_map(context
) == 1;
3465 if (!single_disjunct_map
|| !single_disjunct_context
) {
3466 subset
= isl_map_is_subset(context
, map
);
3470 return replace_by_universe(map
, context
);
3473 context
= isl_map_compute_divs(context
);
3476 if (single_disjunct_context
) {
3477 hull
= isl_map_simple_hull(context
);
3482 ctx
= isl_map_get_ctx(map
);
3483 list
= isl_map_list_alloc(ctx
, 2);
3484 list
= isl_map_list_add(list
, isl_map_copy(context
));
3485 list
= isl_map_list_add(list
, isl_map_copy(map
));
3486 hull
= isl_map_unshifted_simple_hull_from_map_list(context
,
3489 return isl_map_gist_basic_map(map
, hull
);
3492 isl_map_free(context
);
3496 __isl_give isl_map
*isl_map_gist(__isl_take isl_map
*map
,
3497 __isl_take isl_map
*context
)
3499 return isl_map_align_params_map_map_and(map
, context
, &map_gist
);
3502 struct isl_basic_set
*isl_basic_set_gist(struct isl_basic_set
*bset
,
3503 struct isl_basic_set
*context
)
3505 return bset_from_bmap(isl_basic_map_gist(bset_to_bmap(bset
),
3506 bset_to_bmap(context
)));
3509 __isl_give isl_set
*isl_set_gist_basic_set(__isl_take isl_set
*set
,
3510 __isl_take isl_basic_set
*context
)
3512 return set_from_map(isl_map_gist_basic_map(set_to_map(set
),
3513 bset_to_bmap(context
)));
3516 __isl_give isl_set
*isl_set_gist_params_basic_set(__isl_take isl_set
*set
,
3517 __isl_take isl_basic_set
*context
)
3519 isl_space
*space
= isl_set_get_space(set
);
3520 isl_basic_set
*dom_context
= isl_basic_set_universe(space
);
3521 dom_context
= isl_basic_set_intersect_params(dom_context
, context
);
3522 return isl_set_gist_basic_set(set
, dom_context
);
3525 __isl_give isl_set
*isl_set_gist(__isl_take isl_set
*set
,
3526 __isl_take isl_set
*context
)
3528 return set_from_map(isl_map_gist(set_to_map(set
), set_to_map(context
)));
3531 /* Compute the gist of "bmap" with respect to the constraints "context"
3534 __isl_give isl_basic_map
*isl_basic_map_gist_domain(
3535 __isl_take isl_basic_map
*bmap
, __isl_take isl_basic_set
*context
)
3537 isl_space
*space
= isl_basic_map_get_space(bmap
);
3538 isl_basic_map
*bmap_context
= isl_basic_map_universe(space
);
3540 bmap_context
= isl_basic_map_intersect_domain(bmap_context
, context
);
3541 return isl_basic_map_gist(bmap
, bmap_context
);
3544 __isl_give isl_map
*isl_map_gist_domain(__isl_take isl_map
*map
,
3545 __isl_take isl_set
*context
)
3547 isl_map
*map_context
= isl_map_universe(isl_map_get_space(map
));
3548 map_context
= isl_map_intersect_domain(map_context
, context
);
3549 return isl_map_gist(map
, map_context
);
3552 __isl_give isl_map
*isl_map_gist_range(__isl_take isl_map
*map
,
3553 __isl_take isl_set
*context
)
3555 isl_map
*map_context
= isl_map_universe(isl_map_get_space(map
));
3556 map_context
= isl_map_intersect_range(map_context
, context
);
3557 return isl_map_gist(map
, map_context
);
3560 __isl_give isl_map
*isl_map_gist_params(__isl_take isl_map
*map
,
3561 __isl_take isl_set
*context
)
3563 isl_map
*map_context
= isl_map_universe(isl_map_get_space(map
));
3564 map_context
= isl_map_intersect_params(map_context
, context
);
3565 return isl_map_gist(map
, map_context
);
3568 __isl_give isl_set
*isl_set_gist_params(__isl_take isl_set
*set
,
3569 __isl_take isl_set
*context
)
3571 return isl_map_gist_params(set
, context
);
3574 /* Quick check to see if two basic maps are disjoint.
3575 * In particular, we reduce the equalities and inequalities of
3576 * one basic map in the context of the equalities of the other
3577 * basic map and check if we get a contradiction.
3579 isl_bool
isl_basic_map_plain_is_disjoint(__isl_keep isl_basic_map
*bmap1
,
3580 __isl_keep isl_basic_map
*bmap2
)
3582 struct isl_vec
*v
= NULL
;
3587 if (!bmap1
|| !bmap2
)
3588 return isl_bool_error
;
3589 isl_assert(bmap1
->ctx
, isl_space_is_equal(bmap1
->dim
, bmap2
->dim
),
3590 return isl_bool_error
);
3591 if (bmap1
->n_div
|| bmap2
->n_div
)
3592 return isl_bool_false
;
3593 if (!bmap1
->n_eq
&& !bmap2
->n_eq
)
3594 return isl_bool_false
;
3596 total
= isl_space_dim(bmap1
->dim
, isl_dim_all
);
3598 return isl_bool_false
;
3599 v
= isl_vec_alloc(bmap1
->ctx
, 1 + total
);
3602 elim
= isl_alloc_array(bmap1
->ctx
, int, total
);
3605 compute_elimination_index(bmap1
, elim
);
3606 for (i
= 0; i
< bmap2
->n_eq
; ++i
) {
3608 reduced
= reduced_using_equalities(v
->block
.data
, bmap2
->eq
[i
],
3610 if (reduced
&& !isl_int_is_zero(v
->block
.data
[0]) &&
3611 isl_seq_first_non_zero(v
->block
.data
+ 1, total
) == -1)
3614 for (i
= 0; i
< bmap2
->n_ineq
; ++i
) {
3616 reduced
= reduced_using_equalities(v
->block
.data
,
3617 bmap2
->ineq
[i
], bmap1
, elim
);
3618 if (reduced
&& isl_int_is_neg(v
->block
.data
[0]) &&
3619 isl_seq_first_non_zero(v
->block
.data
+ 1, total
) == -1)
3622 compute_elimination_index(bmap2
, elim
);
3623 for (i
= 0; i
< bmap1
->n_ineq
; ++i
) {
3625 reduced
= reduced_using_equalities(v
->block
.data
,
3626 bmap1
->ineq
[i
], bmap2
, elim
);
3627 if (reduced
&& isl_int_is_neg(v
->block
.data
[0]) &&
3628 isl_seq_first_non_zero(v
->block
.data
+ 1, total
) == -1)
3633 return isl_bool_false
;
3637 return isl_bool_true
;
3641 return isl_bool_error
;
3644 int isl_basic_set_plain_is_disjoint(__isl_keep isl_basic_set
*bset1
,
3645 __isl_keep isl_basic_set
*bset2
)
3647 return isl_basic_map_plain_is_disjoint(bset_to_bmap(bset1
),
3648 bset_to_bmap(bset2
));
3651 /* Does "test" hold for all pairs of basic maps in "map1" and "map2"?
3653 static isl_bool
all_pairs(__isl_keep isl_map
*map1
, __isl_keep isl_map
*map2
,
3654 isl_bool (*test
)(__isl_keep isl_basic_map
*bmap1
,
3655 __isl_keep isl_basic_map
*bmap2
))
3660 return isl_bool_error
;
3662 for (i
= 0; i
< map1
->n
; ++i
) {
3663 for (j
= 0; j
< map2
->n
; ++j
) {
3664 isl_bool d
= test(map1
->p
[i
], map2
->p
[j
]);
3665 if (d
!= isl_bool_true
)
3670 return isl_bool_true
;
3673 /* Are "map1" and "map2" obviously disjoint, based on information
3674 * that can be derived without looking at the individual basic maps?
3676 * In particular, if one of them is empty or if they live in different spaces
3677 * (ignoring parameters), then they are clearly disjoint.
3679 static isl_bool
isl_map_plain_is_disjoint_global(__isl_keep isl_map
*map1
,
3680 __isl_keep isl_map
*map2
)
3686 return isl_bool_error
;
3688 disjoint
= isl_map_plain_is_empty(map1
);
3689 if (disjoint
< 0 || disjoint
)
3692 disjoint
= isl_map_plain_is_empty(map2
);
3693 if (disjoint
< 0 || disjoint
)
3696 match
= isl_space_tuple_is_equal(map1
->dim
, isl_dim_in
,
3697 map2
->dim
, isl_dim_in
);
3698 if (match
< 0 || !match
)
3699 return match
< 0 ? isl_bool_error
: isl_bool_true
;
3701 match
= isl_space_tuple_is_equal(map1
->dim
, isl_dim_out
,
3702 map2
->dim
, isl_dim_out
);
3703 if (match
< 0 || !match
)
3704 return match
< 0 ? isl_bool_error
: isl_bool_true
;
3706 return isl_bool_false
;
3709 /* Are "map1" and "map2" obviously disjoint?
3711 * If one of them is empty or if they live in different spaces (ignoring
3712 * parameters), then they are clearly disjoint.
3713 * This is checked by isl_map_plain_is_disjoint_global.
3715 * If they have different parameters, then we skip any further tests.
3717 * If they are obviously equal, but not obviously empty, then we will
3718 * not be able to detect if they are disjoint.
3720 * Otherwise we check if each basic map in "map1" is obviously disjoint
3721 * from each basic map in "map2".
3723 isl_bool
isl_map_plain_is_disjoint(__isl_keep isl_map
*map1
,
3724 __isl_keep isl_map
*map2
)
3730 disjoint
= isl_map_plain_is_disjoint_global(map1
, map2
);
3731 if (disjoint
< 0 || disjoint
)
3734 match
= isl_map_has_equal_params(map1
, map2
);
3735 if (match
< 0 || !match
)
3736 return match
< 0 ? isl_bool_error
: isl_bool_false
;
3738 intersect
= isl_map_plain_is_equal(map1
, map2
);
3739 if (intersect
< 0 || intersect
)
3740 return intersect
< 0 ? isl_bool_error
: isl_bool_false
;
3742 return all_pairs(map1
, map2
, &isl_basic_map_plain_is_disjoint
);
3745 /* Are "map1" and "map2" disjoint?
3746 * The parameters are assumed to have been aligned.
3748 * In particular, check whether all pairs of basic maps are disjoint.
3750 static isl_bool
isl_map_is_disjoint_aligned(__isl_keep isl_map
*map1
,
3751 __isl_keep isl_map
*map2
)
3753 return all_pairs(map1
, map2
, &isl_basic_map_is_disjoint
);
3756 /* Are "map1" and "map2" disjoint?
3758 * They are disjoint if they are "obviously disjoint" or if one of them
3759 * is empty. Otherwise, they are not disjoint if one of them is universal.
3760 * If the two inputs are (obviously) equal and not empty, then they are
3762 * If none of these cases apply, then check if all pairs of basic maps
3763 * are disjoint after aligning the parameters.
3765 isl_bool
isl_map_is_disjoint(__isl_keep isl_map
*map1
, __isl_keep isl_map
*map2
)
3770 disjoint
= isl_map_plain_is_disjoint_global(map1
, map2
);
3771 if (disjoint
< 0 || disjoint
)
3774 disjoint
= isl_map_is_empty(map1
);
3775 if (disjoint
< 0 || disjoint
)
3778 disjoint
= isl_map_is_empty(map2
);
3779 if (disjoint
< 0 || disjoint
)
3782 intersect
= isl_map_plain_is_universe(map1
);
3783 if (intersect
< 0 || intersect
)
3784 return intersect
< 0 ? isl_bool_error
: isl_bool_false
;
3786 intersect
= isl_map_plain_is_universe(map2
);
3787 if (intersect
< 0 || intersect
)
3788 return intersect
< 0 ? isl_bool_error
: isl_bool_false
;
3790 intersect
= isl_map_plain_is_equal(map1
, map2
);
3791 if (intersect
< 0 || intersect
)
3792 return isl_bool_not(intersect
);
3794 return isl_map_align_params_map_map_and_test(map1
, map2
,
3795 &isl_map_is_disjoint_aligned
);
3798 /* Are "bmap1" and "bmap2" disjoint?
3800 * They are disjoint if they are "obviously disjoint" or if one of them
3801 * is empty. Otherwise, they are not disjoint if one of them is universal.
3802 * If none of these cases apply, we compute the intersection and see if
3803 * the result is empty.
3805 isl_bool
isl_basic_map_is_disjoint(__isl_keep isl_basic_map
*bmap1
,
3806 __isl_keep isl_basic_map
*bmap2
)
3810 isl_basic_map
*test
;
3812 disjoint
= isl_basic_map_plain_is_disjoint(bmap1
, bmap2
);
3813 if (disjoint
< 0 || disjoint
)
3816 disjoint
= isl_basic_map_is_empty(bmap1
);
3817 if (disjoint
< 0 || disjoint
)
3820 disjoint
= isl_basic_map_is_empty(bmap2
);
3821 if (disjoint
< 0 || disjoint
)
3824 intersect
= isl_basic_map_plain_is_universe(bmap1
);
3825 if (intersect
< 0 || intersect
)
3826 return intersect
< 0 ? isl_bool_error
: isl_bool_false
;
3828 intersect
= isl_basic_map_plain_is_universe(bmap2
);
3829 if (intersect
< 0 || intersect
)
3830 return intersect
< 0 ? isl_bool_error
: isl_bool_false
;
3832 test
= isl_basic_map_intersect(isl_basic_map_copy(bmap1
),
3833 isl_basic_map_copy(bmap2
));
3834 disjoint
= isl_basic_map_is_empty(test
);
3835 isl_basic_map_free(test
);
3840 /* Are "bset1" and "bset2" disjoint?
3842 isl_bool
isl_basic_set_is_disjoint(__isl_keep isl_basic_set
*bset1
,
3843 __isl_keep isl_basic_set
*bset2
)
3845 return isl_basic_map_is_disjoint(bset1
, bset2
);
3848 isl_bool
isl_set_plain_is_disjoint(__isl_keep isl_set
*set1
,
3849 __isl_keep isl_set
*set2
)
3851 return isl_map_plain_is_disjoint(set_to_map(set1
), set_to_map(set2
));
3854 /* Are "set1" and "set2" disjoint?
3856 isl_bool
isl_set_is_disjoint(__isl_keep isl_set
*set1
, __isl_keep isl_set
*set2
)
3858 return isl_map_is_disjoint(set1
, set2
);
3861 /* Is "v" equal to 0, 1 or -1?
3863 static int is_zero_or_one(isl_int v
)
3865 return isl_int_is_zero(v
) || isl_int_is_one(v
) || isl_int_is_negone(v
);
3868 /* Check if we can combine a given div with lower bound l and upper
3869 * bound u with some other div and if so return that other div.
3870 * Otherwise return -1.
3872 * We first check that
3873 * - the bounds are opposites of each other (except for the constant
3875 * - the bounds do not reference any other div
3876 * - no div is defined in terms of this div
3878 * Let m be the size of the range allowed on the div by the bounds.
3879 * That is, the bounds are of the form
3881 * e <= a <= e + m - 1
3883 * with e some expression in the other variables.
3884 * We look for another div b such that no third div is defined in terms
3885 * of this second div b and such that in any constraint that contains
3886 * a (except for the given lower and upper bound), also contains b
3887 * with a coefficient that is m times that of b.
3888 * That is, all constraints (except for the lower and upper bound)
3891 * e + f (a + m b) >= 0
3893 * Furthermore, in the constraints that only contain b, the coefficient
3894 * of b should be equal to 1 or -1.
3895 * If so, we return b so that "a + m b" can be replaced by
3896 * a single div "c = a + m b".
3898 static int div_find_coalesce(__isl_keep isl_basic_map
*bmap
, int *pairs
,
3899 unsigned div
, unsigned l
, unsigned u
)
3905 if (bmap
->n_div
<= 1)
3907 dim
= isl_space_dim(bmap
->dim
, isl_dim_all
);
3908 if (isl_seq_first_non_zero(bmap
->ineq
[l
] + 1 + dim
, div
) != -1)
3910 if (isl_seq_first_non_zero(bmap
->ineq
[l
] + 1 + dim
+ div
+ 1,
3911 bmap
->n_div
- div
- 1) != -1)
3913 if (!isl_seq_is_neg(bmap
->ineq
[l
] + 1, bmap
->ineq
[u
] + 1,
3917 for (i
= 0; i
< bmap
->n_div
; ++i
) {
3918 if (isl_int_is_zero(bmap
->div
[i
][0]))
3920 if (!isl_int_is_zero(bmap
->div
[i
][1 + 1 + dim
+ div
]))
3924 isl_int_add(bmap
->ineq
[l
][0], bmap
->ineq
[l
][0], bmap
->ineq
[u
][0]);
3925 if (isl_int_is_neg(bmap
->ineq
[l
][0])) {
3926 isl_int_sub(bmap
->ineq
[l
][0],
3927 bmap
->ineq
[l
][0], bmap
->ineq
[u
][0]);
3928 bmap
= isl_basic_map_copy(bmap
);
3929 bmap
= isl_basic_map_set_to_empty(bmap
);
3930 isl_basic_map_free(bmap
);
3933 isl_int_add_ui(bmap
->ineq
[l
][0], bmap
->ineq
[l
][0], 1);
3934 for (i
= 0; i
< bmap
->n_div
; ++i
) {
3939 for (j
= 0; j
< bmap
->n_div
; ++j
) {
3940 if (isl_int_is_zero(bmap
->div
[j
][0]))
3942 if (!isl_int_is_zero(bmap
->div
[j
][1 + 1 + dim
+ i
]))
3945 if (j
< bmap
->n_div
)
3947 for (j
= 0; j
< bmap
->n_ineq
; ++j
) {
3949 if (j
== l
|| j
== u
)
3951 if (isl_int_is_zero(bmap
->ineq
[j
][1 + dim
+ div
])) {
3952 if (is_zero_or_one(bmap
->ineq
[j
][1 + dim
+ i
]))
3956 if (isl_int_is_zero(bmap
->ineq
[j
][1 + dim
+ i
]))
3958 isl_int_mul(bmap
->ineq
[j
][1 + dim
+ div
],
3959 bmap
->ineq
[j
][1 + dim
+ div
],
3961 valid
= isl_int_eq(bmap
->ineq
[j
][1 + dim
+ div
],
3962 bmap
->ineq
[j
][1 + dim
+ i
]);
3963 isl_int_divexact(bmap
->ineq
[j
][1 + dim
+ div
],
3964 bmap
->ineq
[j
][1 + dim
+ div
],
3969 if (j
< bmap
->n_ineq
)
3974 isl_int_sub_ui(bmap
->ineq
[l
][0], bmap
->ineq
[l
][0], 1);
3975 isl_int_sub(bmap
->ineq
[l
][0], bmap
->ineq
[l
][0], bmap
->ineq
[u
][0]);
3979 /* Internal data structure used during the construction and/or evaluation of
3980 * an inequality that ensures that a pair of bounds always allows
3981 * for an integer value.
3983 * "tab" is the tableau in which the inequality is evaluated. It may
3984 * be NULL until it is actually needed.
3985 * "v" contains the inequality coefficients.
3986 * "g", "fl" and "fu" are temporary scalars used during the construction and
3989 struct test_ineq_data
{
3990 struct isl_tab
*tab
;
3997 /* Free all the memory allocated by the fields of "data".
3999 static void test_ineq_data_clear(struct test_ineq_data
*data
)
4001 isl_tab_free(data
->tab
);
4002 isl_vec_free(data
->v
);
4003 isl_int_clear(data
->g
);
4004 isl_int_clear(data
->fl
);
4005 isl_int_clear(data
->fu
);
4008 /* Is the inequality stored in data->v satisfied by "bmap"?
4009 * That is, does it only attain non-negative values?
4010 * data->tab is a tableau corresponding to "bmap".
4012 static isl_bool
test_ineq_is_satisfied(__isl_keep isl_basic_map
*bmap
,
4013 struct test_ineq_data
*data
)
4016 enum isl_lp_result res
;
4018 ctx
= isl_basic_map_get_ctx(bmap
);
4020 data
->tab
= isl_tab_from_basic_map(bmap
, 0);
4021 res
= isl_tab_min(data
->tab
, data
->v
->el
, ctx
->one
, &data
->g
, NULL
, 0);
4022 if (res
== isl_lp_error
)
4023 return isl_bool_error
;
4024 return res
== isl_lp_ok
&& isl_int_is_nonneg(data
->g
);
4027 /* Given a lower and an upper bound on div i, do they always allow
4028 * for an integer value of the given div?
4029 * Determine this property by constructing an inequality
4030 * such that the property is guaranteed when the inequality is nonnegative.
4031 * The lower bound is inequality l, while the upper bound is inequality u.
4032 * The constructed inequality is stored in data->v.
4034 * Let the upper bound be
4038 * and the lower bound
4042 * Let n_u = f_u g and n_l = f_l g, with g = gcd(n_u, n_l).
4045 * - f_u e_l <= f_u f_l g a <= f_l e_u
4047 * Since all variables are integer valued, this is equivalent to
4049 * - f_u e_l - (f_u - 1) <= f_u f_l g a <= f_l e_u + (f_l - 1)
4051 * If this interval is at least f_u f_l g, then it contains at least
4052 * one integer value for a.
4053 * That is, the test constraint is
4055 * f_l e_u + f_u e_l + f_l - 1 + f_u - 1 + 1 >= f_u f_l g
4059 * f_l e_u + f_u e_l + f_l - 1 + f_u - 1 + 1 - f_u f_l g >= 0
4061 * If the coefficients of f_l e_u + f_u e_l have a common divisor g',
4062 * then the constraint can be scaled down by a factor g',
4063 * with the constant term replaced by
4064 * floor((f_l e_{u,0} + f_u e_{l,0} + f_l - 1 + f_u - 1 + 1 - f_u f_l g)/g').
4065 * Note that the result of applying Fourier-Motzkin to this pair
4068 * f_l e_u + f_u e_l >= 0
4070 * If the constant term of the scaled down version of this constraint,
4071 * i.e., floor((f_l e_{u,0} + f_u e_{l,0})/g') is equal to the constant
4072 * term of the scaled down test constraint, then the test constraint
4073 * is known to hold and no explicit evaluation is required.
4074 * This is essentially the Omega test.
4076 * If the test constraint consists of only a constant term, then
4077 * it is sufficient to look at the sign of this constant term.
4079 static isl_bool
int_between_bounds(__isl_keep isl_basic_map
*bmap
, int i
,
4080 int l
, int u
, struct test_ineq_data
*data
)
4082 unsigned offset
, n_div
;
4083 offset
= isl_basic_map_offset(bmap
, isl_dim_div
);
4084 n_div
= isl_basic_map_dim(bmap
, isl_dim_div
);
4086 isl_int_gcd(data
->g
,
4087 bmap
->ineq
[l
][offset
+ i
], bmap
->ineq
[u
][offset
+ i
]);
4088 isl_int_divexact(data
->fl
, bmap
->ineq
[l
][offset
+ i
], data
->g
);
4089 isl_int_divexact(data
->fu
, bmap
->ineq
[u
][offset
+ i
], data
->g
);
4090 isl_int_neg(data
->fu
, data
->fu
);
4091 isl_seq_combine(data
->v
->el
, data
->fl
, bmap
->ineq
[u
],
4092 data
->fu
, bmap
->ineq
[l
], offset
+ n_div
);
4093 isl_int_mul(data
->g
, data
->g
, data
->fl
);
4094 isl_int_mul(data
->g
, data
->g
, data
->fu
);
4095 isl_int_sub(data
->g
, data
->g
, data
->fl
);
4096 isl_int_sub(data
->g
, data
->g
, data
->fu
);
4097 isl_int_add_ui(data
->g
, data
->g
, 1);
4098 isl_int_sub(data
->fl
, data
->v
->el
[0], data
->g
);
4100 isl_seq_gcd(data
->v
->el
+ 1, offset
- 1 + n_div
, &data
->g
);
4101 if (isl_int_is_zero(data
->g
))
4102 return isl_int_is_nonneg(data
->fl
);
4103 if (isl_int_is_one(data
->g
)) {
4104 isl_int_set(data
->v
->el
[0], data
->fl
);
4105 return test_ineq_is_satisfied(bmap
, data
);
4107 isl_int_fdiv_q(data
->fl
, data
->fl
, data
->g
);
4108 isl_int_fdiv_q(data
->v
->el
[0], data
->v
->el
[0], data
->g
);
4109 if (isl_int_eq(data
->fl
, data
->v
->el
[0]))
4110 return isl_bool_true
;
4111 isl_int_set(data
->v
->el
[0], data
->fl
);
4112 isl_seq_scale_down(data
->v
->el
+ 1, data
->v
->el
+ 1, data
->g
,
4113 offset
- 1 + n_div
);
4115 return test_ineq_is_satisfied(bmap
, data
);
4118 /* Remove more kinds of divs that are not strictly needed.
4119 * In particular, if all pairs of lower and upper bounds on a div
4120 * are such that they allow at least one integer value of the div,
4121 * then we can eliminate the div using Fourier-Motzkin without
4122 * introducing any spurious solutions.
4124 * If at least one of the two constraints has a unit coefficient for the div,
4125 * then the presence of such a value is guaranteed so there is no need to check.
4126 * In particular, the value attained by the bound with unit coefficient
4127 * can serve as this intermediate value.
4129 static __isl_give isl_basic_map
*drop_more_redundant_divs(
4130 __isl_take isl_basic_map
*bmap
, __isl_take
int *pairs
, int n
)
4133 struct test_ineq_data data
= { NULL
, NULL
};
4134 unsigned off
, n_div
;
4137 isl_int_init(data
.g
);
4138 isl_int_init(data
.fl
);
4139 isl_int_init(data
.fu
);
4144 ctx
= isl_basic_map_get_ctx(bmap
);
4145 off
= isl_basic_map_offset(bmap
, isl_dim_div
);
4146 n_div
= isl_basic_map_dim(bmap
, isl_dim_div
);
4147 data
.v
= isl_vec_alloc(ctx
, off
+ n_div
);
4156 for (i
= 0; i
< n_div
; ++i
) {
4159 if (best
>= 0 && pairs
[best
] <= pairs
[i
])
4165 for (l
= 0; l
< bmap
->n_ineq
; ++l
) {
4166 if (!isl_int_is_pos(bmap
->ineq
[l
][off
+ i
]))
4168 if (isl_int_is_one(bmap
->ineq
[l
][off
+ i
]))
4170 for (u
= 0; u
< bmap
->n_ineq
; ++u
) {
4171 if (!isl_int_is_neg(bmap
->ineq
[u
][off
+ i
]))
4173 if (isl_int_is_negone(bmap
->ineq
[u
][off
+ i
]))
4175 has_int
= int_between_bounds(bmap
, i
, l
, u
,
4179 if (data
.tab
&& data
.tab
->empty
)
4184 if (u
< bmap
->n_ineq
)
4187 if (data
.tab
&& data
.tab
->empty
) {
4188 bmap
= isl_basic_map_set_to_empty(bmap
);
4191 if (l
== bmap
->n_ineq
) {
4199 test_ineq_data_clear(&data
);
4206 bmap
= isl_basic_map_remove_dims(bmap
, isl_dim_div
, remove
, 1);
4207 return isl_basic_map_drop_redundant_divs(bmap
);
4210 isl_basic_map_free(bmap
);
4211 test_ineq_data_clear(&data
);
4215 /* Given a pair of divs div1 and div2 such that, except for the lower bound l
4216 * and the upper bound u, div1 always occurs together with div2 in the form
4217 * (div1 + m div2), where m is the constant range on the variable div1
4218 * allowed by l and u, replace the pair div1 and div2 by a single
4219 * div that is equal to div1 + m div2.
4221 * The new div will appear in the location that contains div2.
4222 * We need to modify all constraints that contain
4223 * div2 = (div - div1) / m
4224 * The coefficient of div2 is known to be equal to 1 or -1.
4225 * (If a constraint does not contain div2, it will also not contain div1.)
4226 * If the constraint also contains div1, then we know they appear
4227 * as f (div1 + m div2) and we can simply replace (div1 + m div2) by div,
4228 * i.e., the coefficient of div is f.
4230 * Otherwise, we first need to introduce div1 into the constraint.
4239 * A lower bound on div2
4243 * can be replaced by
4245 * m div2 + div1 + m t + f >= 0
4251 * can be replaced by
4253 * -(m div2 + div1) + m t + f' >= 0
4255 * These constraint are those that we would obtain from eliminating
4256 * div1 using Fourier-Motzkin.
4258 * After all constraints have been modified, we drop the lower and upper
4259 * bound and then drop div1.
4260 * Since the new div is only placed in the same location that used
4261 * to store div2, but otherwise has a different meaning, any possible
4262 * explicit representation of the original div2 is removed.
4264 static __isl_give isl_basic_map
*coalesce_divs(__isl_take isl_basic_map
*bmap
,
4265 unsigned div1
, unsigned div2
, unsigned l
, unsigned u
)
4269 unsigned dim
, total
;
4272 ctx
= isl_basic_map_get_ctx(bmap
);
4274 dim
= isl_space_dim(bmap
->dim
, isl_dim_all
);
4275 total
= 1 + dim
+ bmap
->n_div
;
4278 isl_int_add(m
, bmap
->ineq
[l
][0], bmap
->ineq
[u
][0]);
4279 isl_int_add_ui(m
, m
, 1);
4281 for (i
= 0; i
< bmap
->n_ineq
; ++i
) {
4282 if (i
== l
|| i
== u
)
4284 if (isl_int_is_zero(bmap
->ineq
[i
][1 + dim
+ div2
]))
4286 if (isl_int_is_zero(bmap
->ineq
[i
][1 + dim
+ div1
])) {
4287 if (isl_int_is_pos(bmap
->ineq
[i
][1 + dim
+ div2
]))
4288 isl_seq_combine(bmap
->ineq
[i
], m
, bmap
->ineq
[i
],
4289 ctx
->one
, bmap
->ineq
[l
], total
);
4291 isl_seq_combine(bmap
->ineq
[i
], m
, bmap
->ineq
[i
],
4292 ctx
->one
, bmap
->ineq
[u
], total
);
4294 isl_int_set(bmap
->ineq
[i
][1 + dim
+ div2
],
4295 bmap
->ineq
[i
][1 + dim
+ div1
]);
4296 isl_int_set_si(bmap
->ineq
[i
][1 + dim
+ div1
], 0);
4301 isl_basic_map_drop_inequality(bmap
, l
);
4302 isl_basic_map_drop_inequality(bmap
, u
);
4304 isl_basic_map_drop_inequality(bmap
, u
);
4305 isl_basic_map_drop_inequality(bmap
, l
);
4307 bmap
= isl_basic_map_mark_div_unknown(bmap
, div2
);
4308 bmap
= isl_basic_map_drop_div(bmap
, div1
);
4312 /* First check if we can coalesce any pair of divs and
4313 * then continue with dropping more redundant divs.
4315 * We loop over all pairs of lower and upper bounds on a div
4316 * with coefficient 1 and -1, respectively, check if there
4317 * is any other div "c" with which we can coalesce the div
4318 * and if so, perform the coalescing.
4320 static __isl_give isl_basic_map
*coalesce_or_drop_more_redundant_divs(
4321 __isl_take isl_basic_map
*bmap
, int *pairs
, int n
)
4326 dim
= isl_space_dim(bmap
->dim
, isl_dim_all
);
4328 for (i
= 0; i
< bmap
->n_div
; ++i
) {
4331 for (l
= 0; l
< bmap
->n_ineq
; ++l
) {
4332 if (!isl_int_is_one(bmap
->ineq
[l
][1 + dim
+ i
]))
4334 for (u
= 0; u
< bmap
->n_ineq
; ++u
) {
4337 if (!isl_int_is_negone(bmap
->ineq
[u
][1+dim
+i
]))
4339 c
= div_find_coalesce(bmap
, pairs
, i
, l
, u
);
4343 bmap
= coalesce_divs(bmap
, i
, c
, l
, u
);
4344 return isl_basic_map_drop_redundant_divs(bmap
);
4349 if (ISL_F_ISSET(bmap
, ISL_BASIC_MAP_EMPTY
)) {
4354 return drop_more_redundant_divs(bmap
, pairs
, n
);
4357 /* Are the "n" coefficients starting at "first" of inequality constraints
4358 * "i" and "j" of "bmap" equal to each other?
4360 static int is_parallel_part(__isl_keep isl_basic_map
*bmap
, int i
, int j
,
4363 return isl_seq_eq(bmap
->ineq
[i
] + first
, bmap
->ineq
[j
] + first
, n
);
4366 /* Are the "n" coefficients starting at "first" of inequality constraints
4367 * "i" and "j" of "bmap" opposite to each other?
4369 static int is_opposite_part(__isl_keep isl_basic_map
*bmap
, int i
, int j
,
4372 return isl_seq_is_neg(bmap
->ineq
[i
] + first
, bmap
->ineq
[j
] + first
, n
);
4375 /* Are inequality constraints "i" and "j" of "bmap" opposite to each other,
4376 * apart from the constant term?
4378 static isl_bool
is_opposite(__isl_keep isl_basic_map
*bmap
, int i
, int j
)
4382 total
= isl_basic_map_dim(bmap
, isl_dim_all
);
4383 return is_opposite_part(bmap
, i
, j
, 1, total
);
4386 /* Are inequality constraints "i" and "j" of "bmap" equal to each other,
4387 * apart from the constant term and the coefficient at position "pos"?
4389 static int is_parallel_except(__isl_keep isl_basic_map
*bmap
, int i
, int j
,
4394 total
= isl_basic_map_dim(bmap
, isl_dim_all
);
4395 return is_parallel_part(bmap
, i
, j
, 1, pos
- 1) &&
4396 is_parallel_part(bmap
, i
, j
, pos
+ 1, total
- pos
);
4399 /* Are inequality constraints "i" and "j" of "bmap" opposite to each other,
4400 * apart from the constant term and the coefficient at position "pos"?
4402 static int is_opposite_except(__isl_keep isl_basic_map
*bmap
, int i
, int j
,
4407 total
= isl_basic_map_dim(bmap
, isl_dim_all
);
4408 return is_opposite_part(bmap
, i
, j
, 1, pos
- 1) &&
4409 is_opposite_part(bmap
, i
, j
, pos
+ 1, total
- pos
);
4412 /* Restart isl_basic_map_drop_redundant_divs after "bmap" has
4413 * been modified, simplying it if "simplify" is set.
4414 * Free the temporary data structure "pairs" that was associated
4415 * to the old version of "bmap".
4417 static __isl_give isl_basic_map
*drop_redundant_divs_again(
4418 __isl_take isl_basic_map
*bmap
, __isl_take
int *pairs
, int simplify
)
4421 bmap
= isl_basic_map_simplify(bmap
);
4423 return isl_basic_map_drop_redundant_divs(bmap
);
4426 /* Is "div" the single unknown existentially quantified variable
4427 * in inequality constraint "ineq" of "bmap"?
4428 * "div" is known to have a non-zero coefficient in "ineq".
4430 static isl_bool
single_unknown(__isl_keep isl_basic_map
*bmap
, int ineq
,
4434 unsigned n_div
, o_div
;
4437 known
= isl_basic_map_div_is_known(bmap
, div
);
4438 if (known
< 0 || known
)
4439 return isl_bool_not(known
);
4440 n_div
= isl_basic_map_dim(bmap
, isl_dim_div
);
4442 return isl_bool_true
;
4443 o_div
= isl_basic_map_offset(bmap
, isl_dim_div
);
4444 for (i
= 0; i
< n_div
; ++i
) {
4449 if (isl_int_is_zero(bmap
->ineq
[ineq
][o_div
+ i
]))
4451 known
= isl_basic_map_div_is_known(bmap
, i
);
4452 if (known
< 0 || !known
)
4456 return isl_bool_true
;
4459 /* Does integer division "div" have coefficient 1 in inequality constraint
4462 static isl_bool
has_coef_one(__isl_keep isl_basic_map
*bmap
, int div
, int ineq
)
4466 o_div
= isl_basic_map_offset(bmap
, isl_dim_div
);
4467 if (isl_int_is_one(bmap
->ineq
[ineq
][o_div
+ div
]))
4468 return isl_bool_true
;
4470 return isl_bool_false
;
4473 /* Turn inequality constraint "ineq" of "bmap" into an equality and
4474 * then try and drop redundant divs again,
4475 * freeing the temporary data structure "pairs" that was associated
4476 * to the old version of "bmap".
4478 static __isl_give isl_basic_map
*set_eq_and_try_again(
4479 __isl_take isl_basic_map
*bmap
, int ineq
, __isl_take
int *pairs
)
4481 bmap
= isl_basic_map_cow(bmap
);
4482 isl_basic_map_inequality_to_equality(bmap
, ineq
);
4483 return drop_redundant_divs_again(bmap
, pairs
, 1);
4486 /* Drop the integer division at position "div", along with the two
4487 * inequality constraints "ineq1" and "ineq2" in which it appears
4488 * from "bmap" and then try and drop redundant divs again,
4489 * freeing the temporary data structure "pairs" that was associated
4490 * to the old version of "bmap".
4492 static __isl_give isl_basic_map
*drop_div_and_try_again(
4493 __isl_take isl_basic_map
*bmap
, int div
, int ineq1
, int ineq2
,
4494 __isl_take
int *pairs
)
4496 if (ineq1
> ineq2
) {
4497 isl_basic_map_drop_inequality(bmap
, ineq1
);
4498 isl_basic_map_drop_inequality(bmap
, ineq2
);
4500 isl_basic_map_drop_inequality(bmap
, ineq2
);
4501 isl_basic_map_drop_inequality(bmap
, ineq1
);
4503 bmap
= isl_basic_map_drop_div(bmap
, div
);
4504 return drop_redundant_divs_again(bmap
, pairs
, 0);
4507 /* Given two inequality constraints
4509 * f(x) + n d + c >= 0, (ineq)
4511 * with d the variable at position "pos", and
4513 * f(x) + c0 >= 0, (lower)
4515 * compute the maximal value of the lower bound ceil((-f(x) - c)/n)
4516 * determined by the first constraint.
4523 static void lower_bound_from_parallel(__isl_keep isl_basic_map
*bmap
,
4524 int ineq
, int lower
, int pos
, isl_int
*l
)
4526 isl_int_neg(*l
, bmap
->ineq
[ineq
][0]);
4527 isl_int_add(*l
, *l
, bmap
->ineq
[lower
][0]);
4528 isl_int_cdiv_q(*l
, *l
, bmap
->ineq
[ineq
][pos
]);
4531 /* Given two inequality constraints
4533 * f(x) + n d + c >= 0, (ineq)
4535 * with d the variable at position "pos", and
4537 * -f(x) - c0 >= 0, (upper)
4539 * compute the minimal value of the lower bound ceil((-f(x) - c)/n)
4540 * determined by the first constraint.
4547 static void lower_bound_from_opposite(__isl_keep isl_basic_map
*bmap
,
4548 int ineq
, int upper
, int pos
, isl_int
*u
)
4550 isl_int_neg(*u
, bmap
->ineq
[ineq
][0]);
4551 isl_int_sub(*u
, *u
, bmap
->ineq
[upper
][0]);
4552 isl_int_cdiv_q(*u
, *u
, bmap
->ineq
[ineq
][pos
]);
4555 /* Given a lower bound constraint "ineq" on "div" in "bmap",
4556 * does the corresponding lower bound have a fixed value in "bmap"?
4558 * In particular, "ineq" is of the form
4560 * f(x) + n d + c >= 0
4562 * with n > 0, c the constant term and
4563 * d the existentially quantified variable "div".
4564 * That is, the lower bound is
4566 * ceil((-f(x) - c)/n)
4568 * Look for a pair of constraints
4573 * i.e., -c1 <= -f(x) <= c0, that fix ceil((-f(x) - c)/n) to a constant value.
4574 * That is, check that
4576 * ceil((-c1 - c)/n) = ceil((c0 - c)/n)
4578 * If so, return the index of inequality f(x) + c0 >= 0.
4579 * Otherwise, return -1.
4581 static int lower_bound_is_cst(__isl_keep isl_basic_map
*bmap
, int div
, int ineq
)
4584 int lower
= -1, upper
= -1;
4589 o_div
= isl_basic_map_offset(bmap
, isl_dim_div
);
4590 for (i
= 0; i
< bmap
->n_ineq
&& (lower
< 0 || upper
< 0); ++i
) {
4593 if (!isl_int_is_zero(bmap
->ineq
[i
][o_div
+ div
]))
4596 is_parallel_except(bmap
, ineq
, i
, o_div
+ div
)) {
4601 is_opposite_except(bmap
, ineq
, i
, o_div
+ div
)) {
4606 if (lower
< 0 || upper
< 0)
4612 lower_bound_from_parallel(bmap
, ineq
, lower
, o_div
+ div
, &l
);
4613 lower_bound_from_opposite(bmap
, ineq
, upper
, o_div
+ div
, &u
);
4615 equal
= isl_int_eq(l
, u
);
4620 return equal
? lower
: -1;
4623 /* Given a lower bound constraint "ineq" on the existentially quantified
4624 * variable "div", such that the corresponding lower bound has
4625 * a fixed value in "bmap", assign this fixed value to the variable and
4626 * then try and drop redundant divs again,
4627 * freeing the temporary data structure "pairs" that was associated
4628 * to the old version of "bmap".
4629 * "lower" determines the constant value for the lower bound.
4631 * In particular, "ineq" is of the form
4633 * f(x) + n d + c >= 0,
4635 * while "lower" is of the form
4639 * The lower bound is ceil((-f(x) - c)/n) and its constant value
4640 * is ceil((c0 - c)/n).
4642 static __isl_give isl_basic_map
*fix_cst_lower(__isl_take isl_basic_map
*bmap
,
4643 int div
, int ineq
, int lower
, int *pairs
)
4650 o_div
= isl_basic_map_offset(bmap
, isl_dim_div
);
4651 lower_bound_from_parallel(bmap
, ineq
, lower
, o_div
+ div
, &c
);
4652 bmap
= isl_basic_map_fix(bmap
, isl_dim_div
, div
, c
);
4657 return isl_basic_map_drop_redundant_divs(bmap
);
4660 /* Remove divs that are not strictly needed based on the inequality
4662 * In particular, if a div only occurs positively (or negatively)
4663 * in constraints, then it can simply be dropped.
4664 * Also, if a div occurs in only two constraints and if moreover
4665 * those two constraints are opposite to each other, except for the constant
4666 * term and if the sum of the constant terms is such that for any value
4667 * of the other values, there is always at least one integer value of the
4668 * div, i.e., if one plus this sum is greater than or equal to
4669 * the (absolute value) of the coefficient of the div in the constraints,
4670 * then we can also simply drop the div.
4672 * If an existentially quantified variable does not have an explicit
4673 * representation, appears in only a single lower bound that does not
4674 * involve any other such existentially quantified variables and appears
4675 * in this lower bound with coefficient 1,
4676 * then fix the variable to the value of the lower bound. That is,
4677 * turn the inequality into an equality.
4678 * If for any value of the other variables, there is any value
4679 * for the existentially quantified variable satisfying the constraints,
4680 * then this lower bound also satisfies the constraints.
4681 * It is therefore safe to pick this lower bound.
4683 * The same reasoning holds even if the coefficient is not one.
4684 * However, fixing the variable to the value of the lower bound may
4685 * in general introduce an extra integer division, in which case
4686 * it may be better to pick another value.
4687 * If this integer division has a known constant value, then plugging
4688 * in this constant value removes the existentially quantified variable
4689 * completely. In particular, if the lower bound is of the form
4690 * ceil((-f(x) - c)/n) and there are two constraints, f(x) + c0 >= 0 and
4691 * -f(x) + c1 >= 0 such that ceil((-c1 - c)/n) = ceil((c0 - c)/n),
4692 * then the existentially quantified variable can be assigned this
4695 * We skip divs that appear in equalities or in the definition of other divs.
4696 * Divs that appear in the definition of other divs usually occur in at least
4697 * 4 constraints, but the constraints may have been simplified.
4699 * If any divs are left after these simple checks then we move on
4700 * to more complicated cases in drop_more_redundant_divs.
4702 static __isl_give isl_basic_map
*isl_basic_map_drop_redundant_divs_ineq(
4703 __isl_take isl_basic_map
*bmap
)
4712 if (bmap
->n_div
== 0)
4715 off
= isl_space_dim(bmap
->dim
, isl_dim_all
);
4716 pairs
= isl_calloc_array(bmap
->ctx
, int, bmap
->n_div
);
4720 for (i
= 0; i
< bmap
->n_div
; ++i
) {
4722 int last_pos
, last_neg
;
4725 isl_bool opp
, set_div
;
4727 defined
= !isl_int_is_zero(bmap
->div
[i
][0]);
4728 for (j
= i
; j
< bmap
->n_div
; ++j
)
4729 if (!isl_int_is_zero(bmap
->div
[j
][1 + 1 + off
+ i
]))
4731 if (j
< bmap
->n_div
)
4733 for (j
= 0; j
< bmap
->n_eq
; ++j
)
4734 if (!isl_int_is_zero(bmap
->eq
[j
][1 + off
+ i
]))
4740 for (j
= 0; j
< bmap
->n_ineq
; ++j
) {
4741 if (isl_int_is_pos(bmap
->ineq
[j
][1 + off
+ i
])) {
4745 if (isl_int_is_neg(bmap
->ineq
[j
][1 + off
+ i
])) {
4750 pairs
[i
] = pos
* neg
;
4751 if (pairs
[i
] == 0) {
4752 for (j
= bmap
->n_ineq
- 1; j
>= 0; --j
)
4753 if (!isl_int_is_zero(bmap
->ineq
[j
][1+off
+i
]))
4754 isl_basic_map_drop_inequality(bmap
, j
);
4755 bmap
= isl_basic_map_drop_div(bmap
, i
);
4756 return drop_redundant_divs_again(bmap
, pairs
, 0);
4759 opp
= isl_bool_false
;
4761 opp
= is_opposite(bmap
, last_pos
, last_neg
);
4766 isl_bool single
, one
;
4770 single
= single_unknown(bmap
, last_pos
, i
);
4775 one
= has_coef_one(bmap
, i
, last_pos
);
4779 return set_eq_and_try_again(bmap
, last_pos
,
4781 lower
= lower_bound_is_cst(bmap
, i
, last_pos
);
4783 return fix_cst_lower(bmap
, i
, last_pos
, lower
,
4788 isl_int_add(bmap
->ineq
[last_pos
][0],
4789 bmap
->ineq
[last_pos
][0], bmap
->ineq
[last_neg
][0]);
4790 isl_int_add_ui(bmap
->ineq
[last_pos
][0],
4791 bmap
->ineq
[last_pos
][0], 1);
4792 redundant
= isl_int_ge(bmap
->ineq
[last_pos
][0],
4793 bmap
->ineq
[last_pos
][1+off
+i
]);
4794 isl_int_sub_ui(bmap
->ineq
[last_pos
][0],
4795 bmap
->ineq
[last_pos
][0], 1);
4796 isl_int_sub(bmap
->ineq
[last_pos
][0],
4797 bmap
->ineq
[last_pos
][0], bmap
->ineq
[last_neg
][0]);
4799 return drop_div_and_try_again(bmap
, i
,
4800 last_pos
, last_neg
, pairs
);
4802 set_div
= isl_bool_false
;
4804 set_div
= ok_to_set_div_from_bound(bmap
, i
, last_pos
);
4806 return isl_basic_map_free(bmap
);
4808 bmap
= set_div_from_lower_bound(bmap
, i
, last_pos
);
4809 return drop_redundant_divs_again(bmap
, pairs
, 1);
4816 return coalesce_or_drop_more_redundant_divs(bmap
, pairs
, n
);
4822 isl_basic_map_free(bmap
);
4826 /* Consider the coefficients at "c" as a row vector and replace
4827 * them with their product with "T". "T" is assumed to be a square matrix.
4829 static isl_stat
preimage(isl_int
*c
, __isl_keep isl_mat
*T
)
4836 return isl_stat_error
;
4837 n
= isl_mat_rows(T
);
4838 if (isl_seq_first_non_zero(c
, n
) == -1)
4840 ctx
= isl_mat_get_ctx(T
);
4841 v
= isl_vec_alloc(ctx
, n
);
4843 return isl_stat_error
;
4844 isl_seq_swp_or_cpy(v
->el
, c
, n
);
4845 v
= isl_vec_mat_product(v
, isl_mat_copy(T
));
4847 return isl_stat_error
;
4848 isl_seq_swp_or_cpy(c
, v
->el
, n
);
4854 /* Plug in T for the variables in "bmap" starting at "pos".
4855 * T is a linear unimodular matrix, i.e., without constant term.
4857 static __isl_give isl_basic_map
*isl_basic_map_preimage_vars(
4858 __isl_take isl_basic_map
*bmap
, unsigned pos
, __isl_take isl_mat
*T
)
4863 bmap
= isl_basic_map_cow(bmap
);
4867 n
= isl_mat_cols(T
);
4868 if (n
!= isl_mat_rows(T
))
4869 isl_die(isl_mat_get_ctx(T
), isl_error_invalid
,
4870 "expecting square matrix", goto error
);
4872 total
= isl_basic_map_dim(bmap
, isl_dim_all
);
4873 if (pos
+ n
> total
|| pos
+ n
< pos
)
4874 isl_die(isl_mat_get_ctx(T
), isl_error_invalid
,
4875 "invalid range", goto error
);
4877 for (i
= 0; i
< bmap
->n_eq
; ++i
)
4878 if (preimage(bmap
->eq
[i
] + 1 + pos
, T
) < 0)
4880 for (i
= 0; i
< bmap
->n_ineq
; ++i
)
4881 if (preimage(bmap
->ineq
[i
] + 1 + pos
, T
) < 0)
4883 for (i
= 0; i
< bmap
->n_div
; ++i
) {
4884 if (isl_basic_map_div_is_marked_unknown(bmap
, i
))
4886 if (preimage(bmap
->div
[i
] + 1 + 1 + pos
, T
) < 0)
4893 isl_basic_map_free(bmap
);
4898 /* Remove divs that are not strictly needed.
4900 * First look for an equality constraint involving two or more
4901 * existentially quantified variables without an explicit
4902 * representation. Replace the combination that appears
4903 * in the equality constraint by a single existentially quantified
4904 * variable such that the equality can be used to derive
4905 * an explicit representation for the variable.
4906 * If there are no more such equality constraints, then continue
4907 * with isl_basic_map_drop_redundant_divs_ineq.
4909 * In particular, if the equality constraint is of the form
4911 * f(x) + \sum_i c_i a_i = 0
4913 * with a_i existentially quantified variable without explicit
4914 * representation, then apply a transformation on the existentially
4915 * quantified variables to turn the constraint into
4919 * with g the gcd of the c_i.
4920 * In order to easily identify which existentially quantified variables
4921 * have a complete explicit representation, i.e., without being defined
4922 * in terms of other existentially quantified variables without
4923 * an explicit representation, the existentially quantified variables
4926 * The variable transformation is computed by extending the row
4927 * [c_1/g ... c_n/g] to a unimodular matrix, obtaining the transformation
4929 * [a_1'] [c_1/g ... c_n/g] [ a_1 ]
4934 * with [c_1/g ... c_n/g] representing the first row of U.
4935 * The inverse of U is then plugged into the original constraints.
4936 * The call to isl_basic_map_simplify makes sure the explicit
4937 * representation for a_1' is extracted from the equality constraint.
4939 __isl_give isl_basic_map
*isl_basic_map_drop_redundant_divs(
4940 __isl_take isl_basic_map
*bmap
)
4944 unsigned o_div
, n_div
;
4951 if (isl_basic_map_divs_known(bmap
))
4952 return isl_basic_map_drop_redundant_divs_ineq(bmap
);
4953 if (bmap
->n_eq
== 0)
4954 return isl_basic_map_drop_redundant_divs_ineq(bmap
);
4955 bmap
= isl_basic_map_sort_divs(bmap
);
4959 first
= isl_basic_map_first_unknown_div(bmap
);
4961 return isl_basic_map_free(bmap
);
4963 o_div
= isl_basic_map_offset(bmap
, isl_dim_div
);
4964 n_div
= isl_basic_map_dim(bmap
, isl_dim_div
);
4966 for (i
= 0; i
< bmap
->n_eq
; ++i
) {
4967 l
= isl_seq_first_non_zero(bmap
->eq
[i
] + o_div
+ first
,
4972 if (isl_seq_first_non_zero(bmap
->eq
[i
] + o_div
+ l
+ 1,
4973 n_div
- (l
+ 1)) == -1)
4977 if (i
>= bmap
->n_eq
)
4978 return isl_basic_map_drop_redundant_divs_ineq(bmap
);
4980 ctx
= isl_basic_map_get_ctx(bmap
);
4981 T
= isl_mat_alloc(ctx
, n_div
- l
, n_div
- l
);
4983 return isl_basic_map_free(bmap
);
4984 isl_seq_cpy(T
->row
[0], bmap
->eq
[i
] + o_div
+ l
, n_div
- l
);
4985 T
= isl_mat_normalize_row(T
, 0);
4986 T
= isl_mat_unimodular_complete(T
, 1);
4987 T
= isl_mat_right_inverse(T
);
4989 for (i
= l
; i
< n_div
; ++i
)
4990 bmap
= isl_basic_map_mark_div_unknown(bmap
, i
);
4991 bmap
= isl_basic_map_preimage_vars(bmap
, o_div
- 1 + l
, T
);
4992 bmap
= isl_basic_map_simplify(bmap
);
4994 return isl_basic_map_drop_redundant_divs(bmap
);
4997 /* Does "bmap" satisfy any equality that involves more than 2 variables
4998 * and/or has coefficients different from -1 and 1?
5000 static int has_multiple_var_equality(__isl_keep isl_basic_map
*bmap
)
5005 total
= isl_basic_map_dim(bmap
, isl_dim_all
);
5007 for (i
= 0; i
< bmap
->n_eq
; ++i
) {
5010 j
= isl_seq_first_non_zero(bmap
->eq
[i
] + 1, total
);
5013 if (!isl_int_is_one(bmap
->eq
[i
][1 + j
]) &&
5014 !isl_int_is_negone(bmap
->eq
[i
][1 + j
]))
5018 k
= isl_seq_first_non_zero(bmap
->eq
[i
] + 1 + j
, total
- j
);
5022 if (!isl_int_is_one(bmap
->eq
[i
][1 + j
]) &&
5023 !isl_int_is_negone(bmap
->eq
[i
][1 + j
]))
5027 k
= isl_seq_first_non_zero(bmap
->eq
[i
] + 1 + j
, total
- j
);
5035 /* Remove any common factor g from the constraint coefficients in "v".
5036 * The constant term is stored in the first position and is replaced
5037 * by floor(c/g). If any common factor is removed and if this results
5038 * in a tightening of the constraint, then set *tightened.
5040 static __isl_give isl_vec
*normalize_constraint(__isl_take isl_vec
*v
,
5047 ctx
= isl_vec_get_ctx(v
);
5048 isl_seq_gcd(v
->el
+ 1, v
->size
- 1, &ctx
->normalize_gcd
);
5049 if (isl_int_is_zero(ctx
->normalize_gcd
))
5051 if (isl_int_is_one(ctx
->normalize_gcd
))
5056 if (tightened
&& !isl_int_is_divisible_by(v
->el
[0], ctx
->normalize_gcd
))
5058 isl_int_fdiv_q(v
->el
[0], v
->el
[0], ctx
->normalize_gcd
);
5059 isl_seq_scale_down(v
->el
+ 1, v
->el
+ 1, ctx
->normalize_gcd
,
5064 /* If "bmap" is an integer set that satisfies any equality involving
5065 * more than 2 variables and/or has coefficients different from -1 and 1,
5066 * then use variable compression to reduce the coefficients by removing
5067 * any (hidden) common factor.
5068 * In particular, apply the variable compression to each constraint,
5069 * factor out any common factor in the non-constant coefficients and
5070 * then apply the inverse of the compression.
5071 * At the end, we mark the basic map as having reduced constants.
5072 * If this flag is still set on the next invocation of this function,
5073 * then we skip the computation.
5075 * Removing a common factor may result in a tightening of some of
5076 * the constraints. If this happens, then we may end up with two
5077 * opposite inequalities that can be replaced by an equality.
5078 * We therefore call isl_basic_map_detect_inequality_pairs,
5079 * which checks for such pairs of inequalities as well as eliminate_divs_eq
5080 * and isl_basic_map_gauss if such a pair was found.
5082 * Note that this function may leave the result in an inconsistent state.
5083 * In particular, the constraints may not be gaussed.
5084 * Unfortunately, isl_map_coalesce actually depends on this inconsistent state
5085 * for some of the test cases to pass successfully.
5086 * Any potential modification of the representation is therefore only
5087 * performed on a single copy of the basic map.
5089 __isl_give isl_basic_map
*isl_basic_map_reduce_coefficients(
5090 __isl_take isl_basic_map
*bmap
)
5095 isl_mat
*eq
, *T
, *T2
;
5101 if (ISL_F_ISSET(bmap
, ISL_BASIC_MAP_REDUCED_COEFFICIENTS
))
5103 if (isl_basic_map_is_rational(bmap
))
5105 if (bmap
->n_eq
== 0)
5107 if (!has_multiple_var_equality(bmap
))
5110 total
= isl_basic_map_dim(bmap
, isl_dim_all
);
5111 ctx
= isl_basic_map_get_ctx(bmap
);
5112 v
= isl_vec_alloc(ctx
, 1 + total
);
5114 return isl_basic_map_free(bmap
);
5116 eq
= isl_mat_sub_alloc6(ctx
, bmap
->eq
, 0, bmap
->n_eq
, 0, 1 + total
);
5117 T
= isl_mat_variable_compression(eq
, &T2
);
5120 if (T
->n_col
== 0) {
5124 return isl_basic_map_set_to_empty(bmap
);
5127 bmap
= isl_basic_map_cow(bmap
);
5132 for (i
= 0; i
< bmap
->n_ineq
; ++i
) {
5133 isl_seq_cpy(v
->el
, bmap
->ineq
[i
], 1 + total
);
5134 v
= isl_vec_mat_product(v
, isl_mat_copy(T
));
5135 v
= normalize_constraint(v
, &tightened
);
5136 v
= isl_vec_mat_product(v
, isl_mat_copy(T2
));
5139 isl_seq_cpy(bmap
->ineq
[i
], v
->el
, 1 + total
);
5146 ISL_F_SET(bmap
, ISL_BASIC_MAP_REDUCED_COEFFICIENTS
);
5151 bmap
= isl_basic_map_detect_inequality_pairs(bmap
, &progress
);
5153 bmap
= eliminate_divs_eq(bmap
, &progress
);
5154 bmap
= isl_basic_map_gauss(bmap
, NULL
);
5163 return isl_basic_map_free(bmap
);
5166 /* Shift the integer division at position "div" of "bmap"
5167 * by "shift" times the variable at position "pos".
5168 * "pos" is as determined by isl_basic_map_offset, i.e., pos == 0
5169 * corresponds to the constant term.
5171 * That is, if the integer division has the form
5175 * then replace it by
5177 * floor((f(x) + shift * d * x_pos)/d) - shift * x_pos
5179 __isl_give isl_basic_map
*isl_basic_map_shift_div(
5180 __isl_take isl_basic_map
*bmap
, int div
, int pos
, isl_int shift
)
5185 if (isl_int_is_zero(shift
))
5190 total
= isl_basic_map_dim(bmap
, isl_dim_all
);
5191 total
-= isl_basic_map_dim(bmap
, isl_dim_div
);
5193 isl_int_addmul(bmap
->div
[div
][1 + pos
], shift
, bmap
->div
[div
][0]);
5195 for (i
= 0; i
< bmap
->n_eq
; ++i
) {
5196 if (isl_int_is_zero(bmap
->eq
[i
][1 + total
+ div
]))
5198 isl_int_submul(bmap
->eq
[i
][pos
],
5199 shift
, bmap
->eq
[i
][1 + total
+ div
]);
5201 for (i
= 0; i
< bmap
->n_ineq
; ++i
) {
5202 if (isl_int_is_zero(bmap
->ineq
[i
][1 + total
+ div
]))
5204 isl_int_submul(bmap
->ineq
[i
][pos
],
5205 shift
, bmap
->ineq
[i
][1 + total
+ div
]);
5207 for (i
= 0; i
< bmap
->n_div
; ++i
) {
5208 if (isl_int_is_zero(bmap
->div
[i
][0]))
5210 if (isl_int_is_zero(bmap
->div
[i
][1 + 1 + total
+ div
]))
5212 isl_int_submul(bmap
->div
[i
][1 + pos
],
5213 shift
, bmap
->div
[i
][1 + 1 + total
+ div
]);