2 * Copyright 2008-2009 Katholieke Universiteit Leuven
3 * Copyright 2010 INRIA Saclay
4 * Copyright 2016-2017 Sven Verdoolaege
6 * Use of this software is governed by the MIT license
8 * Written by Sven Verdoolaege, K.U.Leuven, Departement
9 * Computerwetenschappen, Celestijnenlaan 200A, B-3001 Leuven, Belgium
10 * and INRIA Saclay - Ile-de-France, Parc Club Orsay Universite,
11 * ZAC des vignes, 4 rue Jacques Monod, 91893 Orsay, France
14 #include <isl_ctx_private.h>
15 #include "isl_map_private.h"
18 #include "isl_sample.h"
19 #include <isl_mat_private.h>
20 #include <isl_vec_private.h>
21 #include <isl_aff_private.h>
22 #include <isl_constraint_private.h>
23 #include <isl_options_private.h>
24 #include <isl_config.h>
26 #include <bset_to_bmap.c>
29 * The implementation of parametric integer linear programming in this file
30 * was inspired by the paper "Parametric Integer Programming" and the
31 * report "Solving systems of affine (in)equalities" by Paul Feautrier
34 * The strategy used for obtaining a feasible solution is different
35 * from the one used in isl_tab.c. In particular, in isl_tab.c,
36 * upon finding a constraint that is not yet satisfied, we pivot
37 * in a row that increases the constant term of the row holding the
38 * constraint, making sure the sample solution remains feasible
39 * for all the constraints it already satisfied.
40 * Here, we always pivot in the row holding the constraint,
41 * choosing a column that induces the lexicographically smallest
42 * increment to the sample solution.
44 * By starting out from a sample value that is lexicographically
45 * smaller than any integer point in the problem space, the first
46 * feasible integer sample point we find will also be the lexicographically
47 * smallest. If all variables can be assumed to be non-negative,
48 * then the initial sample value may be chosen equal to zero.
49 * However, we will not make this assumption. Instead, we apply
50 * the "big parameter" trick. Any variable x is then not directly
51 * used in the tableau, but instead it is represented by another
52 * variable x' = M + x, where M is an arbitrarily large (positive)
53 * value. x' is therefore always non-negative, whatever the value of x.
54 * Taking as initial sample value x' = 0 corresponds to x = -M,
55 * which is always smaller than any possible value of x.
57 * The big parameter trick is used in the main tableau and
58 * also in the context tableau if isl_context_lex is used.
59 * In this case, each tableaus has its own big parameter.
60 * Before doing any real work, we check if all the parameters
61 * happen to be non-negative. If so, we drop the column corresponding
62 * to M from the initial context tableau.
63 * If isl_context_gbr is used, then the big parameter trick is only
64 * used in the main tableau.
68 struct isl_context_op
{
69 /* detect nonnegative parameters in context and mark them in tab */
70 struct isl_tab
*(*detect_nonnegative_parameters
)(
71 struct isl_context
*context
, struct isl_tab
*tab
);
72 /* return temporary reference to basic set representation of context */
73 struct isl_basic_set
*(*peek_basic_set
)(struct isl_context
*context
);
74 /* return temporary reference to tableau representation of context */
75 struct isl_tab
*(*peek_tab
)(struct isl_context
*context
);
76 /* add equality; check is 1 if eq may not be valid;
77 * update is 1 if we may want to call ineq_sign on context later.
79 void (*add_eq
)(struct isl_context
*context
, isl_int
*eq
,
80 int check
, int update
);
81 /* add inequality; check is 1 if ineq may not be valid;
82 * update is 1 if we may want to call ineq_sign on context later.
84 void (*add_ineq
)(struct isl_context
*context
, isl_int
*ineq
,
85 int check
, int update
);
86 /* check sign of ineq based on previous information.
87 * strict is 1 if saturation should be treated as a positive sign.
89 enum isl_tab_row_sign (*ineq_sign
)(struct isl_context
*context
,
90 isl_int
*ineq
, int strict
);
91 /* check if inequality maintains feasibility */
92 int (*test_ineq
)(struct isl_context
*context
, isl_int
*ineq
);
93 /* return index of a div that corresponds to "div" */
94 int (*get_div
)(struct isl_context
*context
, struct isl_tab
*tab
,
96 /* insert div "div" to context at "pos" and return non-negativity */
97 isl_bool (*insert_div
)(struct isl_context
*context
, int pos
,
98 __isl_keep isl_vec
*div
);
99 int (*detect_equalities
)(struct isl_context
*context
,
100 struct isl_tab
*tab
);
101 /* return row index of "best" split */
102 int (*best_split
)(struct isl_context
*context
, struct isl_tab
*tab
);
103 /* check if context has already been determined to be empty */
104 int (*is_empty
)(struct isl_context
*context
);
105 /* check if context is still usable */
106 int (*is_ok
)(struct isl_context
*context
);
107 /* save a copy/snapshot of context */
108 void *(*save
)(struct isl_context
*context
);
109 /* restore saved context */
110 void (*restore
)(struct isl_context
*context
, void *);
111 /* discard saved context */
112 void (*discard
)(void *);
113 /* invalidate context */
114 void (*invalidate
)(struct isl_context
*context
);
116 __isl_null
struct isl_context
*(*free
)(struct isl_context
*context
);
119 /* Shared parts of context representation.
121 * "n_unknown" is the number of final unknown integer divisions
122 * in the input domain.
125 struct isl_context_op
*op
;
129 struct isl_context_lex
{
130 struct isl_context context
;
134 /* A stack (linked list) of solutions of subtrees of the search space.
136 * "ma" describes the solution as a function of "dom".
137 * In particular, the domain space of "ma" is equal to the space of "dom".
139 * If "ma" is NULL, then there is no solution on "dom".
141 struct isl_partial_sol
{
143 struct isl_basic_set
*dom
;
146 struct isl_partial_sol
*next
;
150 struct isl_sol_callback
{
151 struct isl_tab_callback callback
;
155 /* isl_sol is an interface for constructing a solution to
156 * a parametric integer linear programming problem.
157 * Every time the algorithm reaches a state where a solution
158 * can be read off from the tableau, the function "add" is called
159 * on the isl_sol passed to find_solutions_main. In a state where
160 * the tableau is empty, "add_empty" is called instead.
161 * "free" is called to free the implementation specific fields, if any.
163 * "error" is set if some error has occurred. This flag invalidates
164 * the remainder of the data structure.
165 * If "rational" is set, then a rational optimization is being performed.
166 * "level" is the current level in the tree with nodes for each
167 * split in the context.
168 * If "max" is set, then a maximization problem is being solved, rather than
169 * a minimization problem, which means that the variables in the
170 * tableau have value "M - x" rather than "M + x".
171 * "n_out" is the number of output dimensions in the input.
172 * "space" is the space in which the solution (and also the input) lives.
174 * The context tableau is owned by isl_sol and is updated incrementally.
176 * There are currently three implementations of this interface,
177 * isl_sol_map, which simply collects the solutions in an isl_map
178 * and (optionally) the parts of the context where there is no solution
180 * isl_sol_pma, which collects an isl_pw_multi_aff instead, and
181 * isl_sol_for, which calls a user-defined function for each part of
191 struct isl_context
*context
;
192 struct isl_partial_sol
*partial
;
193 void (*add
)(struct isl_sol
*sol
,
194 __isl_take isl_basic_set
*dom
, __isl_take isl_multi_aff
*ma
);
195 void (*add_empty
)(struct isl_sol
*sol
, struct isl_basic_set
*bset
);
196 void (*free
)(struct isl_sol
*sol
);
197 struct isl_sol_callback dec_level
;
200 static void sol_free(struct isl_sol
*sol
)
202 struct isl_partial_sol
*partial
, *next
;
205 for (partial
= sol
->partial
; partial
; partial
= next
) {
206 next
= partial
->next
;
207 isl_basic_set_free(partial
->dom
);
208 isl_multi_aff_free(partial
->ma
);
211 isl_space_free(sol
->space
);
213 sol
->context
->op
->free(sol
->context
);
218 /* Push a partial solution represented by a domain and function "ma"
219 * onto the stack of partial solutions.
220 * If "ma" is NULL, then "dom" represents a part of the domain
223 static void sol_push_sol(struct isl_sol
*sol
,
224 __isl_take isl_basic_set
*dom
, __isl_take isl_multi_aff
*ma
)
226 struct isl_partial_sol
*partial
;
228 if (sol
->error
|| !dom
)
231 partial
= isl_alloc_type(dom
->ctx
, struct isl_partial_sol
);
235 partial
->level
= sol
->level
;
238 partial
->next
= sol
->partial
;
240 sol
->partial
= partial
;
244 isl_basic_set_free(dom
);
245 isl_multi_aff_free(ma
);
249 /* Check that the final columns of "M", starting at "first", are zero.
251 static isl_stat
check_final_columns_are_zero(__isl_keep isl_mat
*M
,
255 unsigned rows
, cols
, n
;
258 return isl_stat_error
;
259 rows
= isl_mat_rows(M
);
260 cols
= isl_mat_cols(M
);
262 for (i
= 0; i
< rows
; ++i
)
263 if (isl_seq_first_non_zero(M
->row
[i
] + first
, n
) != -1)
264 isl_die(isl_mat_get_ctx(M
), isl_error_internal
,
265 "final columns should be zero",
266 return isl_stat_error
);
270 /* Set the affine expressions in "ma" according to the rows in "M", which
271 * are defined over the local space "ls".
272 * The matrix "M" may have extra (zero) columns beyond the number
273 * of variables in "ls".
275 static __isl_give isl_multi_aff
*set_from_affine_matrix(
276 __isl_take isl_multi_aff
*ma
, __isl_take isl_local_space
*ls
,
277 __isl_take isl_mat
*M
)
282 if (!ma
|| !ls
|| !M
)
285 dim
= isl_local_space_dim(ls
, isl_dim_all
);
286 if (check_final_columns_are_zero(M
, 1 + dim
) < 0)
288 for (i
= 1; i
< M
->n_row
; ++i
) {
289 aff
= isl_aff_alloc(isl_local_space_copy(ls
));
291 isl_int_set(aff
->v
->el
[0], M
->row
[0][0]);
292 isl_seq_cpy(aff
->v
->el
+ 1, M
->row
[i
], 1 + dim
);
294 aff
= isl_aff_normalize(aff
);
295 ma
= isl_multi_aff_set_aff(ma
, i
- 1, aff
);
297 isl_local_space_free(ls
);
302 isl_local_space_free(ls
);
304 isl_multi_aff_free(ma
);
308 /* Push a partial solution represented by a domain and mapping M
309 * onto the stack of partial solutions.
311 * The affine matrix "M" maps the dimensions of the context
312 * to the output variables. Convert it into an isl_multi_aff and
313 * then call sol_push_sol.
315 * Note that the description of the initial context may have involved
316 * existentially quantified variables, in which case they also appear
317 * in "dom". These need to be removed before creating the affine
318 * expression because an affine expression cannot be defined in terms
319 * of existentially quantified variables without a known representation.
320 * Since newly added integer divisions are inserted before these
321 * existentially quantified variables, they are still in the final
322 * positions and the corresponding final columns of "M" are zero
323 * because align_context_divs adds the existentially quantified
324 * variables of the context to the main tableau without any constraints and
325 * any equality constraints that are added later on can only serve
326 * to eliminate these existentially quantified variables.
328 static void sol_push_sol_mat(struct isl_sol
*sol
,
329 __isl_take isl_basic_set
*dom
, __isl_take isl_mat
*M
)
335 n_div
= isl_basic_set_dim(dom
, isl_dim_div
);
336 n_known
= n_div
- sol
->context
->n_unknown
;
338 ma
= isl_multi_aff_alloc(isl_space_copy(sol
->space
));
339 ls
= isl_basic_set_get_local_space(dom
);
340 ls
= isl_local_space_drop_dims(ls
, isl_dim_div
,
341 n_known
, n_div
- n_known
);
342 ma
= set_from_affine_matrix(ma
, ls
, M
);
345 dom
= isl_basic_set_free(dom
);
346 sol_push_sol(sol
, dom
, ma
);
349 /* Pop one partial solution from the partial solution stack and
350 * pass it on to sol->add or sol->add_empty.
352 static void sol_pop_one(struct isl_sol
*sol
)
354 struct isl_partial_sol
*partial
;
356 partial
= sol
->partial
;
357 sol
->partial
= partial
->next
;
360 sol
->add(sol
, partial
->dom
, partial
->ma
);
362 sol
->add_empty(sol
, partial
->dom
);
366 /* Return a fresh copy of the domain represented by the context tableau.
368 static struct isl_basic_set
*sol_domain(struct isl_sol
*sol
)
370 struct isl_basic_set
*bset
;
375 bset
= isl_basic_set_dup(sol
->context
->op
->peek_basic_set(sol
->context
));
376 bset
= isl_basic_set_update_from_tab(bset
,
377 sol
->context
->op
->peek_tab(sol
->context
));
382 /* Check whether two partial solutions have the same affine expressions.
384 static isl_bool
same_solution(struct isl_partial_sol
*s1
,
385 struct isl_partial_sol
*s2
)
387 if (!s1
->ma
!= !s2
->ma
)
388 return isl_bool_false
;
390 return isl_bool_true
;
392 return isl_multi_aff_plain_is_equal(s1
->ma
, s2
->ma
);
395 /* Swap the initial two partial solutions in "sol".
399 * sol->partial = p1; p1->next = p2; p2->next = p3
403 * sol->partial = p2; p2->next = p1; p1->next = p3
405 static void swap_initial(struct isl_sol
*sol
)
407 struct isl_partial_sol
*partial
;
409 partial
= sol
->partial
;
410 sol
->partial
= partial
->next
;
411 partial
->next
= partial
->next
->next
;
412 sol
->partial
->next
= partial
;
415 /* Combine the initial two partial solution of "sol" into
416 * a partial solution with the current context domain of "sol" and
417 * the function description of the second partial solution in the list.
418 * The level of the new partial solution is set to the current level.
420 * That is, the first two partial solutions (D1,M1) and (D2,M2) are
421 * replaced by (D,M2), where D is the domain of "sol", which is assumed
422 * to be the union of D1 and D2, while M1 is assumed to be equal to M2
425 static isl_stat
combine_initial_into_second(struct isl_sol
*sol
)
427 struct isl_partial_sol
*partial
;
430 partial
= sol
->partial
;
432 bset
= sol_domain(sol
);
433 isl_basic_set_free(partial
->next
->dom
);
434 partial
->next
->dom
= bset
;
435 partial
->next
->level
= sol
->level
;
438 return isl_stat_error
;
440 sol
->partial
= partial
->next
;
441 isl_basic_set_free(partial
->dom
);
442 isl_multi_aff_free(partial
->ma
);
448 /* Are "ma1" and "ma2" equal to each other on "dom"?
450 * Combine "ma1" and "ma2" with "dom" and check if the results are the same.
451 * "dom" may have existentially quantified variables. Eliminate them first
452 * as otherwise they would have to be eliminated twice, in a more complicated
455 static isl_bool
equal_on_domain(__isl_keep isl_multi_aff
*ma1
,
456 __isl_keep isl_multi_aff
*ma2
, __isl_keep isl_basic_set
*dom
)
459 isl_pw_multi_aff
*pma1
, *pma2
;
462 set
= isl_basic_set_compute_divs(isl_basic_set_copy(dom
));
463 pma1
= isl_pw_multi_aff_alloc(isl_set_copy(set
),
464 isl_multi_aff_copy(ma1
));
465 pma2
= isl_pw_multi_aff_alloc(set
, isl_multi_aff_copy(ma2
));
466 equal
= isl_pw_multi_aff_is_equal(pma1
, pma2
);
467 isl_pw_multi_aff_free(pma1
);
468 isl_pw_multi_aff_free(pma2
);
473 /* The initial two partial solutions of "sol" are known to be at
475 * If they represent the same solution (on different parts of the domain),
476 * then combine them into a single solution at the current level.
477 * Otherwise, pop them both.
479 * Even if the two partial solution are not obviously the same,
480 * one may still be a simplification of the other over its own domain.
481 * Also check if the two sets of affine functions are equal when
482 * restricted to one of the domains. If so, combine the two
483 * using the set of affine functions on the other domain.
484 * That is, for two partial solutions (D1,M1) and (D2,M2),
485 * if M1 = M2 on D1, then the pair of partial solutions can
486 * be replaced by (D1+D2,M2) and similarly when M1 = M2 on D2.
488 static isl_stat
combine_initial_if_equal(struct isl_sol
*sol
)
490 struct isl_partial_sol
*partial
;
493 partial
= sol
->partial
;
495 same
= same_solution(partial
, partial
->next
);
497 return isl_stat_error
;
499 return combine_initial_into_second(sol
);
500 if (partial
->ma
&& partial
->next
->ma
) {
501 same
= equal_on_domain(partial
->ma
, partial
->next
->ma
,
504 return isl_stat_error
;
506 return combine_initial_into_second(sol
);
507 same
= equal_on_domain(partial
->ma
, partial
->next
->ma
,
511 return combine_initial_into_second(sol
);
521 /* Pop all solutions from the partial solution stack that were pushed onto
522 * the stack at levels that are deeper than the current level.
523 * If the two topmost elements on the stack have the same level
524 * and represent the same solution, then their domains are combined.
525 * This combined domain is the same as the current context domain
526 * as sol_pop is called each time we move back to a higher level.
527 * If the outer level (0) has been reached, then all partial solutions
528 * at the current level are also popped off.
530 static void sol_pop(struct isl_sol
*sol
)
532 struct isl_partial_sol
*partial
;
537 partial
= sol
->partial
;
541 if (partial
->level
== 0 && sol
->level
== 0) {
542 for (partial
= sol
->partial
; partial
; partial
= sol
->partial
)
547 if (partial
->level
<= sol
->level
)
550 if (partial
->next
&& partial
->next
->level
== partial
->level
) {
551 if (combine_initial_if_equal(sol
) < 0)
556 if (sol
->level
== 0) {
557 for (partial
= sol
->partial
; partial
; partial
= sol
->partial
)
563 error
: sol
->error
= 1;
566 static void sol_dec_level(struct isl_sol
*sol
)
576 static isl_stat
sol_dec_level_wrap(struct isl_tab_callback
*cb
)
578 struct isl_sol_callback
*callback
= (struct isl_sol_callback
*)cb
;
580 sol_dec_level(callback
->sol
);
582 return callback
->sol
->error
? isl_stat_error
: isl_stat_ok
;
585 /* Move down to next level and push callback onto context tableau
586 * to decrease the level again when it gets rolled back across
587 * the current state. That is, dec_level will be called with
588 * the context tableau in the same state as it is when inc_level
591 static void sol_inc_level(struct isl_sol
*sol
)
599 tab
= sol
->context
->op
->peek_tab(sol
->context
);
600 if (isl_tab_push_callback(tab
, &sol
->dec_level
.callback
) < 0)
604 static void scale_rows(struct isl_mat
*mat
, isl_int m
, int n_row
)
608 if (isl_int_is_one(m
))
611 for (i
= 0; i
< n_row
; ++i
)
612 isl_seq_scale(mat
->row
[i
], mat
->row
[i
], m
, mat
->n_col
);
615 /* Add the solution identified by the tableau and the context tableau.
617 * The layout of the variables is as follows.
618 * tab->n_var is equal to the total number of variables in the input
619 * map (including divs that were copied from the context)
620 * + the number of extra divs constructed
621 * Of these, the first tab->n_param and the last tab->n_div variables
622 * correspond to the variables in the context, i.e.,
623 * tab->n_param + tab->n_div = context_tab->n_var
624 * tab->n_param is equal to the number of parameters and input
625 * dimensions in the input map
626 * tab->n_div is equal to the number of divs in the context
628 * If there is no solution, then call add_empty with a basic set
629 * that corresponds to the context tableau. (If add_empty is NULL,
632 * If there is a solution, then first construct a matrix that maps
633 * all dimensions of the context to the output variables, i.e.,
634 * the output dimensions in the input map.
635 * The divs in the input map (if any) that do not correspond to any
636 * div in the context do not appear in the solution.
637 * The algorithm will make sure that they have an integer value,
638 * but these values themselves are of no interest.
639 * We have to be careful not to drop or rearrange any divs in the
640 * context because that would change the meaning of the matrix.
642 * To extract the value of the output variables, it should be noted
643 * that we always use a big parameter M in the main tableau and so
644 * the variable stored in this tableau is not an output variable x itself, but
645 * x' = M + x (in case of minimization)
647 * x' = M - x (in case of maximization)
648 * If x' appears in a column, then its optimal value is zero,
649 * which means that the optimal value of x is an unbounded number
650 * (-M for minimization and M for maximization).
651 * We currently assume that the output dimensions in the original map
652 * are bounded, so this cannot occur.
653 * Similarly, when x' appears in a row, then the coefficient of M in that
654 * row is necessarily 1.
655 * If the row in the tableau represents
656 * d x' = c + d M + e(y)
657 * then, in case of minimization, the corresponding row in the matrix
660 * with a d = m, the (updated) common denominator of the matrix.
661 * In case of maximization, the row will be
664 static void sol_add(struct isl_sol
*sol
, struct isl_tab
*tab
)
666 struct isl_basic_set
*bset
= NULL
;
667 struct isl_mat
*mat
= NULL
;
672 if (sol
->error
|| !tab
)
675 if (tab
->empty
&& !sol
->add_empty
)
677 if (sol
->context
->op
->is_empty(sol
->context
))
680 bset
= sol_domain(sol
);
683 sol_push_sol(sol
, bset
, NULL
);
689 mat
= isl_mat_alloc(tab
->mat
->ctx
, 1 + sol
->n_out
,
690 1 + tab
->n_param
+ tab
->n_div
);
696 isl_seq_clr(mat
->row
[0] + 1, mat
->n_col
- 1);
697 isl_int_set_si(mat
->row
[0][0], 1);
698 for (row
= 0; row
< sol
->n_out
; ++row
) {
699 int i
= tab
->n_param
+ row
;
702 isl_seq_clr(mat
->row
[1 + row
], mat
->n_col
);
703 if (!tab
->var
[i
].is_row
) {
705 isl_die(mat
->ctx
, isl_error_invalid
,
706 "unbounded optimum", goto error2
);
710 r
= tab
->var
[i
].index
;
712 isl_int_ne(tab
->mat
->row
[r
][2], tab
->mat
->row
[r
][0]))
713 isl_die(mat
->ctx
, isl_error_invalid
,
714 "unbounded optimum", goto error2
);
715 isl_int_gcd(m
, mat
->row
[0][0], tab
->mat
->row
[r
][0]);
716 isl_int_divexact(m
, tab
->mat
->row
[r
][0], m
);
717 scale_rows(mat
, m
, 1 + row
);
718 isl_int_divexact(m
, mat
->row
[0][0], tab
->mat
->row
[r
][0]);
719 isl_int_mul(mat
->row
[1 + row
][0], m
, tab
->mat
->row
[r
][1]);
720 for (j
= 0; j
< tab
->n_param
; ++j
) {
722 if (tab
->var
[j
].is_row
)
724 col
= tab
->var
[j
].index
;
725 isl_int_mul(mat
->row
[1 + row
][1 + j
], m
,
726 tab
->mat
->row
[r
][off
+ col
]);
728 for (j
= 0; j
< tab
->n_div
; ++j
) {
730 if (tab
->var
[tab
->n_var
- tab
->n_div
+j
].is_row
)
732 col
= tab
->var
[tab
->n_var
- tab
->n_div
+j
].index
;
733 isl_int_mul(mat
->row
[1 + row
][1 + tab
->n_param
+ j
], m
,
734 tab
->mat
->row
[r
][off
+ col
]);
737 isl_seq_neg(mat
->row
[1 + row
], mat
->row
[1 + row
],
743 sol_push_sol_mat(sol
, bset
, mat
);
748 isl_basic_set_free(bset
);
756 struct isl_set
*empty
;
759 static void sol_map_free(struct isl_sol
*sol
)
761 struct isl_sol_map
*sol_map
= (struct isl_sol_map
*) sol
;
762 isl_map_free(sol_map
->map
);
763 isl_set_free(sol_map
->empty
);
766 /* This function is called for parts of the context where there is
767 * no solution, with "bset" corresponding to the context tableau.
768 * Simply add the basic set to the set "empty".
770 static void sol_map_add_empty(struct isl_sol_map
*sol
,
771 struct isl_basic_set
*bset
)
773 if (!bset
|| !sol
->empty
)
776 sol
->empty
= isl_set_grow(sol
->empty
, 1);
777 bset
= isl_basic_set_simplify(bset
);
778 bset
= isl_basic_set_finalize(bset
);
779 sol
->empty
= isl_set_add_basic_set(sol
->empty
, isl_basic_set_copy(bset
));
782 isl_basic_set_free(bset
);
785 isl_basic_set_free(bset
);
789 static void sol_map_add_empty_wrap(struct isl_sol
*sol
,
790 struct isl_basic_set
*bset
)
792 sol_map_add_empty((struct isl_sol_map
*)sol
, bset
);
795 /* Given a basic set "dom" that represents the context and a tuple of
796 * affine expressions "ma" defined over this domain, construct a basic map
797 * that expresses this function on the domain.
799 static void sol_map_add(struct isl_sol_map
*sol
,
800 __isl_take isl_basic_set
*dom
, __isl_take isl_multi_aff
*ma
)
804 if (sol
->sol
.error
|| !dom
|| !ma
)
807 bmap
= isl_basic_map_from_multi_aff2(ma
, sol
->sol
.rational
);
808 bmap
= isl_basic_map_intersect_domain(bmap
, dom
);
809 sol
->map
= isl_map_grow(sol
->map
, 1);
810 sol
->map
= isl_map_add_basic_map(sol
->map
, bmap
);
815 isl_basic_set_free(dom
);
816 isl_multi_aff_free(ma
);
820 static void sol_map_add_wrap(struct isl_sol
*sol
,
821 __isl_take isl_basic_set
*dom
, __isl_take isl_multi_aff
*ma
)
823 sol_map_add((struct isl_sol_map
*)sol
, dom
, ma
);
827 /* Store the "parametric constant" of row "row" of tableau "tab" in "line",
828 * i.e., the constant term and the coefficients of all variables that
829 * appear in the context tableau.
830 * Note that the coefficient of the big parameter M is NOT copied.
831 * The context tableau may not have a big parameter and even when it
832 * does, it is a different big parameter.
834 static void get_row_parameter_line(struct isl_tab
*tab
, int row
, isl_int
*line
)
837 unsigned off
= 2 + tab
->M
;
839 isl_int_set(line
[0], tab
->mat
->row
[row
][1]);
840 for (i
= 0; i
< tab
->n_param
; ++i
) {
841 if (tab
->var
[i
].is_row
)
842 isl_int_set_si(line
[1 + i
], 0);
844 int col
= tab
->var
[i
].index
;
845 isl_int_set(line
[1 + i
], tab
->mat
->row
[row
][off
+ col
]);
848 for (i
= 0; i
< tab
->n_div
; ++i
) {
849 if (tab
->var
[tab
->n_var
- tab
->n_div
+ i
].is_row
)
850 isl_int_set_si(line
[1 + tab
->n_param
+ i
], 0);
852 int col
= tab
->var
[tab
->n_var
- tab
->n_div
+ i
].index
;
853 isl_int_set(line
[1 + tab
->n_param
+ i
],
854 tab
->mat
->row
[row
][off
+ col
]);
859 /* Check if rows "row1" and "row2" have identical "parametric constants",
860 * as explained above.
861 * In this case, we also insist that the coefficients of the big parameter
862 * be the same as the values of the constants will only be the same
863 * if these coefficients are also the same.
865 static int identical_parameter_line(struct isl_tab
*tab
, int row1
, int row2
)
868 unsigned off
= 2 + tab
->M
;
870 if (isl_int_ne(tab
->mat
->row
[row1
][1], tab
->mat
->row
[row2
][1]))
873 if (tab
->M
&& isl_int_ne(tab
->mat
->row
[row1
][2],
874 tab
->mat
->row
[row2
][2]))
877 for (i
= 0; i
< tab
->n_param
+ tab
->n_div
; ++i
) {
878 int pos
= i
< tab
->n_param
? i
:
879 tab
->n_var
- tab
->n_div
+ i
- tab
->n_param
;
882 if (tab
->var
[pos
].is_row
)
884 col
= tab
->var
[pos
].index
;
885 if (isl_int_ne(tab
->mat
->row
[row1
][off
+ col
],
886 tab
->mat
->row
[row2
][off
+ col
]))
892 /* Return an inequality that expresses that the "parametric constant"
893 * should be non-negative.
894 * This function is only called when the coefficient of the big parameter
897 static struct isl_vec
*get_row_parameter_ineq(struct isl_tab
*tab
, int row
)
899 struct isl_vec
*ineq
;
901 ineq
= isl_vec_alloc(tab
->mat
->ctx
, 1 + tab
->n_param
+ tab
->n_div
);
905 get_row_parameter_line(tab
, row
, ineq
->el
);
907 ineq
= isl_vec_normalize(ineq
);
912 /* Normalize a div expression of the form
914 * [(g*f(x) + c)/(g * m)]
916 * with c the constant term and f(x) the remaining coefficients, to
920 static void normalize_div(__isl_keep isl_vec
*div
)
922 isl_ctx
*ctx
= isl_vec_get_ctx(div
);
923 int len
= div
->size
- 2;
925 isl_seq_gcd(div
->el
+ 2, len
, &ctx
->normalize_gcd
);
926 isl_int_gcd(ctx
->normalize_gcd
, ctx
->normalize_gcd
, div
->el
[0]);
928 if (isl_int_is_one(ctx
->normalize_gcd
))
931 isl_int_divexact(div
->el
[0], div
->el
[0], ctx
->normalize_gcd
);
932 isl_int_fdiv_q(div
->el
[1], div
->el
[1], ctx
->normalize_gcd
);
933 isl_seq_scale_down(div
->el
+ 2, div
->el
+ 2, ctx
->normalize_gcd
, len
);
936 /* Return an integer division for use in a parametric cut based
938 * In particular, let the parametric constant of the row be
942 * where y_0 = 1, but none of the y_i corresponds to the big parameter M.
943 * The div returned is equal to
945 * floor(\sum_i {-a_i} y_i) = floor((\sum_i (-a_i mod d) y_i)/d)
947 static struct isl_vec
*get_row_parameter_div(struct isl_tab
*tab
, int row
)
951 div
= isl_vec_alloc(tab
->mat
->ctx
, 1 + 1 + tab
->n_param
+ tab
->n_div
);
955 isl_int_set(div
->el
[0], tab
->mat
->row
[row
][0]);
956 get_row_parameter_line(tab
, row
, div
->el
+ 1);
957 isl_seq_neg(div
->el
+ 1, div
->el
+ 1, div
->size
- 1);
959 isl_seq_fdiv_r(div
->el
+ 1, div
->el
+ 1, div
->el
[0], div
->size
- 1);
964 /* Return an integer division for use in transferring an integrality constraint
966 * In particular, let the parametric constant of the row be
970 * where y_0 = 1, but none of the y_i corresponds to the big parameter M.
971 * The the returned div is equal to
973 * floor(\sum_i {a_i} y_i) = floor((\sum_i (a_i mod d) y_i)/d)
975 static struct isl_vec
*get_row_split_div(struct isl_tab
*tab
, int row
)
979 div
= isl_vec_alloc(tab
->mat
->ctx
, 1 + 1 + tab
->n_param
+ tab
->n_div
);
983 isl_int_set(div
->el
[0], tab
->mat
->row
[row
][0]);
984 get_row_parameter_line(tab
, row
, div
->el
+ 1);
986 isl_seq_fdiv_r(div
->el
+ 1, div
->el
+ 1, div
->el
[0], div
->size
- 1);
991 /* Construct and return an inequality that expresses an upper bound
993 * In particular, if the div is given by
997 * then the inequality expresses
1001 static __isl_give isl_vec
*ineq_for_div(__isl_keep isl_basic_set
*bset
,
1006 struct isl_vec
*ineq
;
1011 total
= isl_basic_set_total_dim(bset
);
1012 div_pos
= 1 + total
- bset
->n_div
+ div
;
1014 ineq
= isl_vec_alloc(bset
->ctx
, 1 + total
);
1018 isl_seq_cpy(ineq
->el
, bset
->div
[div
] + 1, 1 + total
);
1019 isl_int_neg(ineq
->el
[div_pos
], bset
->div
[div
][0]);
1023 /* Given a row in the tableau and a div that was created
1024 * using get_row_split_div and that has been constrained to equality, i.e.,
1026 * d = floor(\sum_i {a_i} y_i) = \sum_i {a_i} y_i
1028 * replace the expression "\sum_i {a_i} y_i" in the row by d,
1029 * i.e., we subtract "\sum_i {a_i} y_i" and add 1 d.
1030 * The coefficients of the non-parameters in the tableau have been
1031 * verified to be integral. We can therefore simply replace coefficient b
1032 * by floor(b). For the coefficients of the parameters we have
1033 * floor(a_i) = a_i - {a_i}, while for the other coefficients, we have
1036 static struct isl_tab
*set_row_cst_to_div(struct isl_tab
*tab
, int row
, int div
)
1038 isl_seq_fdiv_q(tab
->mat
->row
[row
] + 1, tab
->mat
->row
[row
] + 1,
1039 tab
->mat
->row
[row
][0], 1 + tab
->M
+ tab
->n_col
);
1041 isl_int_set_si(tab
->mat
->row
[row
][0], 1);
1043 if (tab
->var
[tab
->n_var
- tab
->n_div
+ div
].is_row
) {
1044 int drow
= tab
->var
[tab
->n_var
- tab
->n_div
+ div
].index
;
1046 isl_assert(tab
->mat
->ctx
,
1047 isl_int_is_one(tab
->mat
->row
[drow
][0]), goto error
);
1048 isl_seq_combine(tab
->mat
->row
[row
] + 1,
1049 tab
->mat
->ctx
->one
, tab
->mat
->row
[row
] + 1,
1050 tab
->mat
->ctx
->one
, tab
->mat
->row
[drow
] + 1,
1051 1 + tab
->M
+ tab
->n_col
);
1053 int dcol
= tab
->var
[tab
->n_var
- tab
->n_div
+ div
].index
;
1055 isl_int_add_ui(tab
->mat
->row
[row
][2 + tab
->M
+ dcol
],
1056 tab
->mat
->row
[row
][2 + tab
->M
+ dcol
], 1);
1065 /* Check if the (parametric) constant of the given row is obviously
1066 * negative, meaning that we don't need to consult the context tableau.
1067 * If there is a big parameter and its coefficient is non-zero,
1068 * then this coefficient determines the outcome.
1069 * Otherwise, we check whether the constant is negative and
1070 * all non-zero coefficients of parameters are negative and
1071 * belong to non-negative parameters.
1073 static int is_obviously_neg(struct isl_tab
*tab
, int row
)
1077 unsigned off
= 2 + tab
->M
;
1080 if (isl_int_is_pos(tab
->mat
->row
[row
][2]))
1082 if (isl_int_is_neg(tab
->mat
->row
[row
][2]))
1086 if (isl_int_is_nonneg(tab
->mat
->row
[row
][1]))
1088 for (i
= 0; i
< tab
->n_param
; ++i
) {
1089 /* Eliminated parameter */
1090 if (tab
->var
[i
].is_row
)
1092 col
= tab
->var
[i
].index
;
1093 if (isl_int_is_zero(tab
->mat
->row
[row
][off
+ col
]))
1095 if (!tab
->var
[i
].is_nonneg
)
1097 if (isl_int_is_pos(tab
->mat
->row
[row
][off
+ col
]))
1100 for (i
= 0; i
< tab
->n_div
; ++i
) {
1101 if (tab
->var
[tab
->n_var
- tab
->n_div
+ i
].is_row
)
1103 col
= tab
->var
[tab
->n_var
- tab
->n_div
+ i
].index
;
1104 if (isl_int_is_zero(tab
->mat
->row
[row
][off
+ col
]))
1106 if (!tab
->var
[tab
->n_var
- tab
->n_div
+ i
].is_nonneg
)
1108 if (isl_int_is_pos(tab
->mat
->row
[row
][off
+ col
]))
1114 /* Check if the (parametric) constant of the given row is obviously
1115 * non-negative, meaning that we don't need to consult the context tableau.
1116 * If there is a big parameter and its coefficient is non-zero,
1117 * then this coefficient determines the outcome.
1118 * Otherwise, we check whether the constant is non-negative and
1119 * all non-zero coefficients of parameters are positive and
1120 * belong to non-negative parameters.
1122 static int is_obviously_nonneg(struct isl_tab
*tab
, int row
)
1126 unsigned off
= 2 + tab
->M
;
1129 if (isl_int_is_pos(tab
->mat
->row
[row
][2]))
1131 if (isl_int_is_neg(tab
->mat
->row
[row
][2]))
1135 if (isl_int_is_neg(tab
->mat
->row
[row
][1]))
1137 for (i
= 0; i
< tab
->n_param
; ++i
) {
1138 /* Eliminated parameter */
1139 if (tab
->var
[i
].is_row
)
1141 col
= tab
->var
[i
].index
;
1142 if (isl_int_is_zero(tab
->mat
->row
[row
][off
+ col
]))
1144 if (!tab
->var
[i
].is_nonneg
)
1146 if (isl_int_is_neg(tab
->mat
->row
[row
][off
+ col
]))
1149 for (i
= 0; i
< tab
->n_div
; ++i
) {
1150 if (tab
->var
[tab
->n_var
- tab
->n_div
+ i
].is_row
)
1152 col
= tab
->var
[tab
->n_var
- tab
->n_div
+ i
].index
;
1153 if (isl_int_is_zero(tab
->mat
->row
[row
][off
+ col
]))
1155 if (!tab
->var
[tab
->n_var
- tab
->n_div
+ i
].is_nonneg
)
1157 if (isl_int_is_neg(tab
->mat
->row
[row
][off
+ col
]))
1163 /* Given a row r and two columns, return the column that would
1164 * lead to the lexicographically smallest increment in the sample
1165 * solution when leaving the basis in favor of the row.
1166 * Pivoting with column c will increment the sample value by a non-negative
1167 * constant times a_{V,c}/a_{r,c}, with a_{V,c} the elements of column c
1168 * corresponding to the non-parametric variables.
1169 * If variable v appears in a column c_v, the a_{v,c} = 1 iff c = c_v,
1170 * with all other entries in this virtual row equal to zero.
1171 * If variable v appears in a row, then a_{v,c} is the element in column c
1174 * Let v be the first variable with a_{v,c1}/a_{r,c1} != a_{v,c2}/a_{r,c2}.
1175 * Then if a_{v,c1}/a_{r,c1} < a_{v,c2}/a_{r,c2}, i.e.,
1176 * a_{v,c2} a_{r,c1} - a_{v,c1} a_{r,c2} > 0, c1 results in the minimal
1177 * increment. Otherwise, it's c2.
1179 static int lexmin_col_pair(struct isl_tab
*tab
,
1180 int row
, int col1
, int col2
, isl_int tmp
)
1185 tr
= tab
->mat
->row
[row
] + 2 + tab
->M
;
1187 for (i
= tab
->n_param
; i
< tab
->n_var
- tab
->n_div
; ++i
) {
1191 if (!tab
->var
[i
].is_row
) {
1192 if (tab
->var
[i
].index
== col1
)
1194 if (tab
->var
[i
].index
== col2
)
1199 if (tab
->var
[i
].index
== row
)
1202 r
= tab
->mat
->row
[tab
->var
[i
].index
] + 2 + tab
->M
;
1203 s1
= isl_int_sgn(r
[col1
]);
1204 s2
= isl_int_sgn(r
[col2
]);
1205 if (s1
== 0 && s2
== 0)
1212 isl_int_mul(tmp
, r
[col2
], tr
[col1
]);
1213 isl_int_submul(tmp
, r
[col1
], tr
[col2
]);
1214 if (isl_int_is_pos(tmp
))
1216 if (isl_int_is_neg(tmp
))
1222 /* Does the index into the tab->var or tab->con array "index"
1223 * correspond to a variable in the context tableau?
1224 * In particular, it needs to be an index into the tab->var array and
1225 * it needs to refer to either one of the first tab->n_param variables or
1226 * one of the last tab->n_div variables.
1228 static int is_parameter_var(struct isl_tab
*tab
, int index
)
1232 if (index
< tab
->n_param
)
1234 if (index
>= tab
->n_var
- tab
->n_div
)
1239 /* Does column "col" of "tab" refer to a variable in the context tableau?
1241 static int col_is_parameter_var(struct isl_tab
*tab
, int col
)
1243 return is_parameter_var(tab
, tab
->col_var
[col
]);
1246 /* Does row "row" of "tab" refer to a variable in the context tableau?
1248 static int row_is_parameter_var(struct isl_tab
*tab
, int row
)
1250 return is_parameter_var(tab
, tab
->row_var
[row
]);
1253 /* Given a row in the tableau, find and return the column that would
1254 * result in the lexicographically smallest, but positive, increment
1255 * in the sample point.
1256 * If there is no such column, then return tab->n_col.
1257 * If anything goes wrong, return -1.
1259 static int lexmin_pivot_col(struct isl_tab
*tab
, int row
)
1262 int col
= tab
->n_col
;
1266 tr
= tab
->mat
->row
[row
] + 2 + tab
->M
;
1270 for (j
= tab
->n_dead
; j
< tab
->n_col
; ++j
) {
1271 if (col_is_parameter_var(tab
, j
))
1274 if (!isl_int_is_pos(tr
[j
]))
1277 if (col
== tab
->n_col
)
1280 col
= lexmin_col_pair(tab
, row
, col
, j
, tmp
);
1281 isl_assert(tab
->mat
->ctx
, col
>= 0, goto error
);
1291 /* Return the first known violated constraint, i.e., a non-negative
1292 * constraint that currently has an either obviously negative value
1293 * or a previously determined to be negative value.
1295 * If any constraint has a negative coefficient for the big parameter,
1296 * if any, then we return one of these first.
1298 static int first_neg(struct isl_tab
*tab
)
1303 for (row
= tab
->n_redundant
; row
< tab
->n_row
; ++row
) {
1304 if (!isl_tab_var_from_row(tab
, row
)->is_nonneg
)
1306 if (!isl_int_is_neg(tab
->mat
->row
[row
][2]))
1309 tab
->row_sign
[row
] = isl_tab_row_neg
;
1312 for (row
= tab
->n_redundant
; row
< tab
->n_row
; ++row
) {
1313 if (!isl_tab_var_from_row(tab
, row
)->is_nonneg
)
1315 if (tab
->row_sign
) {
1316 if (tab
->row_sign
[row
] == 0 &&
1317 is_obviously_neg(tab
, row
))
1318 tab
->row_sign
[row
] = isl_tab_row_neg
;
1319 if (tab
->row_sign
[row
] != isl_tab_row_neg
)
1321 } else if (!is_obviously_neg(tab
, row
))
1328 /* Check whether the invariant that all columns are lexico-positive
1329 * is satisfied. This function is not called from the current code
1330 * but is useful during debugging.
1332 static void check_lexpos(struct isl_tab
*tab
) __attribute__ ((unused
));
1333 static void check_lexpos(struct isl_tab
*tab
)
1335 unsigned off
= 2 + tab
->M
;
1340 for (col
= tab
->n_dead
; col
< tab
->n_col
; ++col
) {
1341 if (col_is_parameter_var(tab
, col
))
1343 for (var
= tab
->n_param
; var
< tab
->n_var
- tab
->n_div
; ++var
) {
1344 if (!tab
->var
[var
].is_row
) {
1345 if (tab
->var
[var
].index
== col
)
1350 row
= tab
->var
[var
].index
;
1351 if (isl_int_is_zero(tab
->mat
->row
[row
][off
+ col
]))
1353 if (isl_int_is_pos(tab
->mat
->row
[row
][off
+ col
]))
1355 fprintf(stderr
, "lexneg column %d (row %d)\n",
1358 if (var
>= tab
->n_var
- tab
->n_div
)
1359 fprintf(stderr
, "zero column %d\n", col
);
1363 /* Report to the caller that the given constraint is part of an encountered
1366 static int report_conflicting_constraint(struct isl_tab
*tab
, int con
)
1368 return tab
->conflict(con
, tab
->conflict_user
);
1371 /* Given a conflicting row in the tableau, report all constraints
1372 * involved in the row to the caller. That is, the row itself
1373 * (if it represents a constraint) and all constraint columns with
1374 * non-zero (and therefore negative) coefficients.
1376 static int report_conflict(struct isl_tab
*tab
, int row
)
1384 if (tab
->row_var
[row
] < 0 &&
1385 report_conflicting_constraint(tab
, ~tab
->row_var
[row
]) < 0)
1388 tr
= tab
->mat
->row
[row
] + 2 + tab
->M
;
1390 for (j
= tab
->n_dead
; j
< tab
->n_col
; ++j
) {
1391 if (col_is_parameter_var(tab
, j
))
1394 if (!isl_int_is_neg(tr
[j
]))
1397 if (tab
->col_var
[j
] < 0 &&
1398 report_conflicting_constraint(tab
, ~tab
->col_var
[j
]) < 0)
1405 /* Resolve all known or obviously violated constraints through pivoting.
1406 * In particular, as long as we can find any violated constraint, we
1407 * look for a pivoting column that would result in the lexicographically
1408 * smallest increment in the sample point. If there is no such column
1409 * then the tableau is infeasible.
1411 static int restore_lexmin(struct isl_tab
*tab
) WARN_UNUSED
;
1412 static int restore_lexmin(struct isl_tab
*tab
)
1420 while ((row
= first_neg(tab
)) != -1) {
1421 col
= lexmin_pivot_col(tab
, row
);
1422 if (col
>= tab
->n_col
) {
1423 if (report_conflict(tab
, row
) < 0)
1425 if (isl_tab_mark_empty(tab
) < 0)
1431 if (isl_tab_pivot(tab
, row
, col
) < 0)
1437 /* Given a row that represents an equality, look for an appropriate
1439 * In particular, if there are any non-zero coefficients among
1440 * the non-parameter variables, then we take the last of these
1441 * variables. Eliminating this variable in terms of the other
1442 * variables and/or parameters does not influence the property
1443 * that all column in the initial tableau are lexicographically
1444 * positive. The row corresponding to the eliminated variable
1445 * will only have non-zero entries below the diagonal of the
1446 * initial tableau. That is, we transform
1452 * If there is no such non-parameter variable, then we are dealing with
1453 * pure parameter equality and we pick any parameter with coefficient 1 or -1
1454 * for elimination. This will ensure that the eliminated parameter
1455 * always has an integer value whenever all the other parameters are integral.
1456 * If there is no such parameter then we return -1.
1458 static int last_var_col_or_int_par_col(struct isl_tab
*tab
, int row
)
1460 unsigned off
= 2 + tab
->M
;
1463 for (i
= tab
->n_var
- tab
->n_div
- 1; i
>= 0 && i
>= tab
->n_param
; --i
) {
1465 if (tab
->var
[i
].is_row
)
1467 col
= tab
->var
[i
].index
;
1468 if (col
<= tab
->n_dead
)
1470 if (!isl_int_is_zero(tab
->mat
->row
[row
][off
+ col
]))
1473 for (i
= tab
->n_dead
; i
< tab
->n_col
; ++i
) {
1474 if (isl_int_is_one(tab
->mat
->row
[row
][off
+ i
]))
1476 if (isl_int_is_negone(tab
->mat
->row
[row
][off
+ i
]))
1482 /* Add an equality that is known to be valid to the tableau.
1483 * We first check if we can eliminate a variable or a parameter.
1484 * If not, we add the equality as two inequalities.
1485 * In this case, the equality was a pure parameter equality and there
1486 * is no need to resolve any constraint violations.
1488 * This function assumes that at least two more rows and at least
1489 * two more elements in the constraint array are available in the tableau.
1491 static struct isl_tab
*add_lexmin_valid_eq(struct isl_tab
*tab
, isl_int
*eq
)
1498 r
= isl_tab_add_row(tab
, eq
);
1502 r
= tab
->con
[r
].index
;
1503 i
= last_var_col_or_int_par_col(tab
, r
);
1505 tab
->con
[r
].is_nonneg
= 1;
1506 if (isl_tab_push_var(tab
, isl_tab_undo_nonneg
, &tab
->con
[r
]) < 0)
1508 isl_seq_neg(eq
, eq
, 1 + tab
->n_var
);
1509 r
= isl_tab_add_row(tab
, eq
);
1512 tab
->con
[r
].is_nonneg
= 1;
1513 if (isl_tab_push_var(tab
, isl_tab_undo_nonneg
, &tab
->con
[r
]) < 0)
1516 if (isl_tab_pivot(tab
, r
, i
) < 0)
1518 if (isl_tab_kill_col(tab
, i
) < 0)
1529 /* Check if the given row is a pure constant.
1531 static int is_constant(struct isl_tab
*tab
, int row
)
1533 unsigned off
= 2 + tab
->M
;
1535 return isl_seq_first_non_zero(tab
->mat
->row
[row
] + off
+ tab
->n_dead
,
1536 tab
->n_col
- tab
->n_dead
) == -1;
1539 /* Is the given row a parametric constant?
1540 * That is, does it only involve variables that also appear in the context?
1542 static int is_parametric_constant(struct isl_tab
*tab
, int row
)
1544 unsigned off
= 2 + tab
->M
;
1547 for (col
= tab
->n_dead
; col
< tab
->n_col
; ++col
) {
1548 if (col_is_parameter_var(tab
, col
))
1550 if (isl_int_is_zero(tab
->mat
->row
[row
][off
+ col
]))
1558 /* Add an equality that may or may not be valid to the tableau.
1559 * If the resulting row is a pure constant, then it must be zero.
1560 * Otherwise, the resulting tableau is empty.
1562 * If the row is not a pure constant, then we add two inequalities,
1563 * each time checking that they can be satisfied.
1564 * In the end we try to use one of the two constraints to eliminate
1567 * This function assumes that at least two more rows and at least
1568 * two more elements in the constraint array are available in the tableau.
1570 static int add_lexmin_eq(struct isl_tab
*tab
, isl_int
*eq
) WARN_UNUSED
;
1571 static int add_lexmin_eq(struct isl_tab
*tab
, isl_int
*eq
)
1575 struct isl_tab_undo
*snap
;
1579 snap
= isl_tab_snap(tab
);
1580 r1
= isl_tab_add_row(tab
, eq
);
1583 tab
->con
[r1
].is_nonneg
= 1;
1584 if (isl_tab_push_var(tab
, isl_tab_undo_nonneg
, &tab
->con
[r1
]) < 0)
1587 row
= tab
->con
[r1
].index
;
1588 if (is_constant(tab
, row
)) {
1589 if (!isl_int_is_zero(tab
->mat
->row
[row
][1]) ||
1590 (tab
->M
&& !isl_int_is_zero(tab
->mat
->row
[row
][2]))) {
1591 if (isl_tab_mark_empty(tab
) < 0)
1595 if (isl_tab_rollback(tab
, snap
) < 0)
1600 if (restore_lexmin(tab
) < 0)
1605 isl_seq_neg(eq
, eq
, 1 + tab
->n_var
);
1607 r2
= isl_tab_add_row(tab
, eq
);
1610 tab
->con
[r2
].is_nonneg
= 1;
1611 if (isl_tab_push_var(tab
, isl_tab_undo_nonneg
, &tab
->con
[r2
]) < 0)
1614 if (restore_lexmin(tab
) < 0)
1619 if (!tab
->con
[r1
].is_row
) {
1620 if (isl_tab_kill_col(tab
, tab
->con
[r1
].index
) < 0)
1622 } else if (!tab
->con
[r2
].is_row
) {
1623 if (isl_tab_kill_col(tab
, tab
->con
[r2
].index
) < 0)
1628 tab
->bmap
= isl_basic_map_add_ineq(tab
->bmap
, eq
);
1629 if (isl_tab_push(tab
, isl_tab_undo_bmap_ineq
) < 0)
1631 isl_seq_neg(eq
, eq
, 1 + tab
->n_var
);
1632 tab
->bmap
= isl_basic_map_add_ineq(tab
->bmap
, eq
);
1633 isl_seq_neg(eq
, eq
, 1 + tab
->n_var
);
1634 if (isl_tab_push(tab
, isl_tab_undo_bmap_ineq
) < 0)
1643 /* Add an inequality to the tableau, resolving violations using
1646 * This function assumes that at least one more row and at least
1647 * one more element in the constraint array are available in the tableau.
1649 static struct isl_tab
*add_lexmin_ineq(struct isl_tab
*tab
, isl_int
*ineq
)
1656 tab
->bmap
= isl_basic_map_add_ineq(tab
->bmap
, ineq
);
1657 if (isl_tab_push(tab
, isl_tab_undo_bmap_ineq
) < 0)
1662 r
= isl_tab_add_row(tab
, ineq
);
1665 tab
->con
[r
].is_nonneg
= 1;
1666 if (isl_tab_push_var(tab
, isl_tab_undo_nonneg
, &tab
->con
[r
]) < 0)
1668 if (isl_tab_row_is_redundant(tab
, tab
->con
[r
].index
)) {
1669 if (isl_tab_mark_redundant(tab
, tab
->con
[r
].index
) < 0)
1674 if (restore_lexmin(tab
) < 0)
1676 if (!tab
->empty
&& tab
->con
[r
].is_row
&&
1677 isl_tab_row_is_redundant(tab
, tab
->con
[r
].index
))
1678 if (isl_tab_mark_redundant(tab
, tab
->con
[r
].index
) < 0)
1686 /* Check if the coefficients of the parameters are all integral.
1688 static int integer_parameter(struct isl_tab
*tab
, int row
)
1692 unsigned off
= 2 + tab
->M
;
1694 for (i
= 0; i
< tab
->n_param
; ++i
) {
1695 /* Eliminated parameter */
1696 if (tab
->var
[i
].is_row
)
1698 col
= tab
->var
[i
].index
;
1699 if (!isl_int_is_divisible_by(tab
->mat
->row
[row
][off
+ col
],
1700 tab
->mat
->row
[row
][0]))
1703 for (i
= 0; i
< tab
->n_div
; ++i
) {
1704 if (tab
->var
[tab
->n_var
- tab
->n_div
+ i
].is_row
)
1706 col
= tab
->var
[tab
->n_var
- tab
->n_div
+ i
].index
;
1707 if (!isl_int_is_divisible_by(tab
->mat
->row
[row
][off
+ col
],
1708 tab
->mat
->row
[row
][0]))
1714 /* Check if the coefficients of the non-parameter variables are all integral.
1716 static int integer_variable(struct isl_tab
*tab
, int row
)
1719 unsigned off
= 2 + tab
->M
;
1721 for (i
= tab
->n_dead
; i
< tab
->n_col
; ++i
) {
1722 if (col_is_parameter_var(tab
, i
))
1724 if (!isl_int_is_divisible_by(tab
->mat
->row
[row
][off
+ i
],
1725 tab
->mat
->row
[row
][0]))
1731 /* Check if the constant term is integral.
1733 static int integer_constant(struct isl_tab
*tab
, int row
)
1735 return isl_int_is_divisible_by(tab
->mat
->row
[row
][1],
1736 tab
->mat
->row
[row
][0]);
1739 #define I_CST 1 << 0
1740 #define I_PAR 1 << 1
1741 #define I_VAR 1 << 2
1743 /* Check for next (non-parameter) variable after "var" (first if var == -1)
1744 * that is non-integer and therefore requires a cut and return
1745 * the index of the variable.
1746 * For parametric tableaus, there are three parts in a row,
1747 * the constant, the coefficients of the parameters and the rest.
1748 * For each part, we check whether the coefficients in that part
1749 * are all integral and if so, set the corresponding flag in *f.
1750 * If the constant and the parameter part are integral, then the
1751 * current sample value is integral and no cut is required
1752 * (irrespective of whether the variable part is integral).
1754 static int next_non_integer_var(struct isl_tab
*tab
, int var
, int *f
)
1756 var
= var
< 0 ? tab
->n_param
: var
+ 1;
1758 for (; var
< tab
->n_var
- tab
->n_div
; ++var
) {
1761 if (!tab
->var
[var
].is_row
)
1763 row
= tab
->var
[var
].index
;
1764 if (integer_constant(tab
, row
))
1765 ISL_FL_SET(flags
, I_CST
);
1766 if (integer_parameter(tab
, row
))
1767 ISL_FL_SET(flags
, I_PAR
);
1768 if (ISL_FL_ISSET(flags
, I_CST
) && ISL_FL_ISSET(flags
, I_PAR
))
1770 if (integer_variable(tab
, row
))
1771 ISL_FL_SET(flags
, I_VAR
);
1778 /* Check for first (non-parameter) variable that is non-integer and
1779 * therefore requires a cut and return the corresponding row.
1780 * For parametric tableaus, there are three parts in a row,
1781 * the constant, the coefficients of the parameters and the rest.
1782 * For each part, we check whether the coefficients in that part
1783 * are all integral and if so, set the corresponding flag in *f.
1784 * If the constant and the parameter part are integral, then the
1785 * current sample value is integral and no cut is required
1786 * (irrespective of whether the variable part is integral).
1788 static int first_non_integer_row(struct isl_tab
*tab
, int *f
)
1790 int var
= next_non_integer_var(tab
, -1, f
);
1792 return var
< 0 ? -1 : tab
->var
[var
].index
;
1795 /* Add a (non-parametric) cut to cut away the non-integral sample
1796 * value of the given row.
1798 * If the row is given by
1800 * m r = f + \sum_i a_i y_i
1804 * c = - {-f/m} + \sum_i {a_i/m} y_i >= 0
1806 * The big parameter, if any, is ignored, since it is assumed to be big
1807 * enough to be divisible by any integer.
1808 * If the tableau is actually a parametric tableau, then this function
1809 * is only called when all coefficients of the parameters are integral.
1810 * The cut therefore has zero coefficients for the parameters.
1812 * The current value is known to be negative, so row_sign, if it
1813 * exists, is set accordingly.
1815 * Return the row of the cut or -1.
1817 static int add_cut(struct isl_tab
*tab
, int row
)
1822 unsigned off
= 2 + tab
->M
;
1824 if (isl_tab_extend_cons(tab
, 1) < 0)
1826 r
= isl_tab_allocate_con(tab
);
1830 r_row
= tab
->mat
->row
[tab
->con
[r
].index
];
1831 isl_int_set(r_row
[0], tab
->mat
->row
[row
][0]);
1832 isl_int_neg(r_row
[1], tab
->mat
->row
[row
][1]);
1833 isl_int_fdiv_r(r_row
[1], r_row
[1], tab
->mat
->row
[row
][0]);
1834 isl_int_neg(r_row
[1], r_row
[1]);
1836 isl_int_set_si(r_row
[2], 0);
1837 for (i
= 0; i
< tab
->n_col
; ++i
)
1838 isl_int_fdiv_r(r_row
[off
+ i
],
1839 tab
->mat
->row
[row
][off
+ i
], tab
->mat
->row
[row
][0]);
1841 tab
->con
[r
].is_nonneg
= 1;
1842 if (isl_tab_push_var(tab
, isl_tab_undo_nonneg
, &tab
->con
[r
]) < 0)
1845 tab
->row_sign
[tab
->con
[r
].index
] = isl_tab_row_neg
;
1847 return tab
->con
[r
].index
;
1853 /* Given a non-parametric tableau, add cuts until an integer
1854 * sample point is obtained or until the tableau is determined
1855 * to be integer infeasible.
1856 * As long as there is any non-integer value in the sample point,
1857 * we add appropriate cuts, if possible, for each of these
1858 * non-integer values and then resolve the violated
1859 * cut constraints using restore_lexmin.
1860 * If one of the corresponding rows is equal to an integral
1861 * combination of variables/constraints plus a non-integral constant,
1862 * then there is no way to obtain an integer point and we return
1863 * a tableau that is marked empty.
1864 * The parameter cutting_strategy controls the strategy used when adding cuts
1865 * to remove non-integer points. CUT_ALL adds all possible cuts
1866 * before continuing the search. CUT_ONE adds only one cut at a time.
1868 static struct isl_tab
*cut_to_integer_lexmin(struct isl_tab
*tab
,
1869 int cutting_strategy
)
1880 while ((var
= next_non_integer_var(tab
, -1, &flags
)) != -1) {
1882 if (ISL_FL_ISSET(flags
, I_VAR
)) {
1883 if (isl_tab_mark_empty(tab
) < 0)
1887 row
= tab
->var
[var
].index
;
1888 row
= add_cut(tab
, row
);
1891 if (cutting_strategy
== CUT_ONE
)
1893 } while ((var
= next_non_integer_var(tab
, var
, &flags
)) != -1);
1894 if (restore_lexmin(tab
) < 0)
1905 /* Check whether all the currently active samples also satisfy the inequality
1906 * "ineq" (treated as an equality if eq is set).
1907 * Remove those samples that do not.
1909 static struct isl_tab
*check_samples(struct isl_tab
*tab
, isl_int
*ineq
, int eq
)
1917 isl_assert(tab
->mat
->ctx
, tab
->bmap
, goto error
);
1918 isl_assert(tab
->mat
->ctx
, tab
->samples
, goto error
);
1919 isl_assert(tab
->mat
->ctx
, tab
->samples
->n_col
== 1 + tab
->n_var
, goto error
);
1922 for (i
= tab
->n_outside
; i
< tab
->n_sample
; ++i
) {
1924 isl_seq_inner_product(ineq
, tab
->samples
->row
[i
],
1925 1 + tab
->n_var
, &v
);
1926 sgn
= isl_int_sgn(v
);
1927 if (eq
? (sgn
== 0) : (sgn
>= 0))
1929 tab
= isl_tab_drop_sample(tab
, i
);
1941 /* Check whether the sample value of the tableau is finite,
1942 * i.e., either the tableau does not use a big parameter, or
1943 * all values of the variables are equal to the big parameter plus
1944 * some constant. This constant is the actual sample value.
1946 static int sample_is_finite(struct isl_tab
*tab
)
1953 for (i
= 0; i
< tab
->n_var
; ++i
) {
1955 if (!tab
->var
[i
].is_row
)
1957 row
= tab
->var
[i
].index
;
1958 if (isl_int_ne(tab
->mat
->row
[row
][0], tab
->mat
->row
[row
][2]))
1964 /* Check if the context tableau of sol has any integer points.
1965 * Leave tab in empty state if no integer point can be found.
1966 * If an integer point can be found and if moreover it is finite,
1967 * then it is added to the list of sample values.
1969 * This function is only called when none of the currently active sample
1970 * values satisfies the most recently added constraint.
1972 static struct isl_tab
*check_integer_feasible(struct isl_tab
*tab
)
1974 struct isl_tab_undo
*snap
;
1979 snap
= isl_tab_snap(tab
);
1980 if (isl_tab_push_basis(tab
) < 0)
1983 tab
= cut_to_integer_lexmin(tab
, CUT_ALL
);
1987 if (!tab
->empty
&& sample_is_finite(tab
)) {
1988 struct isl_vec
*sample
;
1990 sample
= isl_tab_get_sample_value(tab
);
1992 if (isl_tab_add_sample(tab
, sample
) < 0)
1996 if (!tab
->empty
&& isl_tab_rollback(tab
, snap
) < 0)
2005 /* Check if any of the currently active sample values satisfies
2006 * the inequality "ineq" (an equality if eq is set).
2008 static int tab_has_valid_sample(struct isl_tab
*tab
, isl_int
*ineq
, int eq
)
2016 isl_assert(tab
->mat
->ctx
, tab
->bmap
, return -1);
2017 isl_assert(tab
->mat
->ctx
, tab
->samples
, return -1);
2018 isl_assert(tab
->mat
->ctx
, tab
->samples
->n_col
== 1 + tab
->n_var
, return -1);
2021 for (i
= tab
->n_outside
; i
< tab
->n_sample
; ++i
) {
2023 isl_seq_inner_product(ineq
, tab
->samples
->row
[i
],
2024 1 + tab
->n_var
, &v
);
2025 sgn
= isl_int_sgn(v
);
2026 if (eq
? (sgn
== 0) : (sgn
>= 0))
2031 return i
< tab
->n_sample
;
2034 /* Insert a div specified by "div" to the tableau "tab" at position "pos" and
2035 * return isl_bool_true if the div is obviously non-negative.
2037 static isl_bool
context_tab_insert_div(struct isl_tab
*tab
, int pos
,
2038 __isl_keep isl_vec
*div
,
2039 isl_stat (*add_ineq
)(void *user
, isl_int
*), void *user
)
2043 struct isl_mat
*samples
;
2046 r
= isl_tab_insert_div(tab
, pos
, div
, add_ineq
, user
);
2048 return isl_bool_error
;
2049 nonneg
= tab
->var
[r
].is_nonneg
;
2050 tab
->var
[r
].frozen
= 1;
2052 samples
= isl_mat_extend(tab
->samples
,
2053 tab
->n_sample
, 1 + tab
->n_var
);
2054 tab
->samples
= samples
;
2056 return isl_bool_error
;
2057 for (i
= tab
->n_outside
; i
< samples
->n_row
; ++i
) {
2058 isl_seq_inner_product(div
->el
+ 1, samples
->row
[i
],
2059 div
->size
- 1, &samples
->row
[i
][samples
->n_col
- 1]);
2060 isl_int_fdiv_q(samples
->row
[i
][samples
->n_col
- 1],
2061 samples
->row
[i
][samples
->n_col
- 1], div
->el
[0]);
2063 tab
->samples
= isl_mat_move_cols(tab
->samples
, 1 + pos
,
2064 1 + tab
->n_var
- 1, 1);
2066 return isl_bool_error
;
2071 /* Add a div specified by "div" to both the main tableau and
2072 * the context tableau. In case of the main tableau, we only
2073 * need to add an extra div. In the context tableau, we also
2074 * need to express the meaning of the div.
2075 * Return the index of the div or -1 if anything went wrong.
2077 * The new integer division is added before any unknown integer
2078 * divisions in the context to ensure that it does not get
2079 * equated to some linear combination involving unknown integer
2082 static int add_div(struct isl_tab
*tab
, struct isl_context
*context
,
2083 __isl_keep isl_vec
*div
)
2088 struct isl_tab
*context_tab
= context
->op
->peek_tab(context
);
2090 if (!tab
|| !context_tab
)
2093 pos
= context_tab
->n_var
- context
->n_unknown
;
2094 if ((nonneg
= context
->op
->insert_div(context
, pos
, div
)) < 0)
2097 if (!context
->op
->is_ok(context
))
2100 pos
= tab
->n_var
- context
->n_unknown
;
2101 if (isl_tab_extend_vars(tab
, 1) < 0)
2103 r
= isl_tab_insert_var(tab
, pos
);
2107 tab
->var
[r
].is_nonneg
= 1;
2108 tab
->var
[r
].frozen
= 1;
2111 return tab
->n_div
- 1 - context
->n_unknown
;
2113 context
->op
->invalidate(context
);
2117 static int find_div(struct isl_tab
*tab
, isl_int
*div
, isl_int denom
)
2120 unsigned total
= isl_basic_map_total_dim(tab
->bmap
);
2122 for (i
= 0; i
< tab
->bmap
->n_div
; ++i
) {
2123 if (isl_int_ne(tab
->bmap
->div
[i
][0], denom
))
2125 if (!isl_seq_eq(tab
->bmap
->div
[i
] + 1, div
, 1 + total
))
2132 /* Return the index of a div that corresponds to "div".
2133 * We first check if we already have such a div and if not, we create one.
2135 static int get_div(struct isl_tab
*tab
, struct isl_context
*context
,
2136 struct isl_vec
*div
)
2139 struct isl_tab
*context_tab
= context
->op
->peek_tab(context
);
2144 d
= find_div(context_tab
, div
->el
+ 1, div
->el
[0]);
2148 return add_div(tab
, context
, div
);
2151 /* Add a parametric cut to cut away the non-integral sample value
2153 * Let a_i be the coefficients of the constant term and the parameters
2154 * and let b_i be the coefficients of the variables or constraints
2155 * in basis of the tableau.
2156 * Let q be the div q = floor(\sum_i {-a_i} y_i).
2158 * The cut is expressed as
2160 * c = \sum_i -{-a_i} y_i + \sum_i {b_i} x_i + q >= 0
2162 * If q did not already exist in the context tableau, then it is added first.
2163 * If q is in a column of the main tableau then the "+ q" can be accomplished
2164 * by setting the corresponding entry to the denominator of the constraint.
2165 * If q happens to be in a row of the main tableau, then the corresponding
2166 * row needs to be added instead (taking care of the denominators).
2167 * Note that this is very unlikely, but perhaps not entirely impossible.
2169 * The current value of the cut is known to be negative (or at least
2170 * non-positive), so row_sign is set accordingly.
2172 * Return the row of the cut or -1.
2174 static int add_parametric_cut(struct isl_tab
*tab
, int row
,
2175 struct isl_context
*context
)
2177 struct isl_vec
*div
;
2184 unsigned off
= 2 + tab
->M
;
2189 div
= get_row_parameter_div(tab
, row
);
2193 n
= tab
->n_div
- context
->n_unknown
;
2194 d
= context
->op
->get_div(context
, tab
, div
);
2199 if (isl_tab_extend_cons(tab
, 1) < 0)
2201 r
= isl_tab_allocate_con(tab
);
2205 r_row
= tab
->mat
->row
[tab
->con
[r
].index
];
2206 isl_int_set(r_row
[0], tab
->mat
->row
[row
][0]);
2207 isl_int_neg(r_row
[1], tab
->mat
->row
[row
][1]);
2208 isl_int_fdiv_r(r_row
[1], r_row
[1], tab
->mat
->row
[row
][0]);
2209 isl_int_neg(r_row
[1], r_row
[1]);
2211 isl_int_set_si(r_row
[2], 0);
2212 for (i
= 0; i
< tab
->n_param
; ++i
) {
2213 if (tab
->var
[i
].is_row
)
2215 col
= tab
->var
[i
].index
;
2216 isl_int_neg(r_row
[off
+ col
], tab
->mat
->row
[row
][off
+ col
]);
2217 isl_int_fdiv_r(r_row
[off
+ col
], r_row
[off
+ col
],
2218 tab
->mat
->row
[row
][0]);
2219 isl_int_neg(r_row
[off
+ col
], r_row
[off
+ col
]);
2221 for (i
= 0; i
< tab
->n_div
; ++i
) {
2222 if (tab
->var
[tab
->n_var
- tab
->n_div
+ i
].is_row
)
2224 col
= tab
->var
[tab
->n_var
- tab
->n_div
+ i
].index
;
2225 isl_int_neg(r_row
[off
+ col
], tab
->mat
->row
[row
][off
+ col
]);
2226 isl_int_fdiv_r(r_row
[off
+ col
], r_row
[off
+ col
],
2227 tab
->mat
->row
[row
][0]);
2228 isl_int_neg(r_row
[off
+ col
], r_row
[off
+ col
]);
2230 for (i
= 0; i
< tab
->n_col
; ++i
) {
2231 if (tab
->col_var
[i
] >= 0 &&
2232 (tab
->col_var
[i
] < tab
->n_param
||
2233 tab
->col_var
[i
] >= tab
->n_var
- tab
->n_div
))
2235 isl_int_fdiv_r(r_row
[off
+ i
],
2236 tab
->mat
->row
[row
][off
+ i
], tab
->mat
->row
[row
][0]);
2238 if (tab
->var
[tab
->n_var
- tab
->n_div
+ d
].is_row
) {
2240 int d_row
= tab
->var
[tab
->n_var
- tab
->n_div
+ d
].index
;
2242 isl_int_gcd(gcd
, tab
->mat
->row
[d_row
][0], r_row
[0]);
2243 isl_int_divexact(r_row
[0], r_row
[0], gcd
);
2244 isl_int_divexact(gcd
, tab
->mat
->row
[d_row
][0], gcd
);
2245 isl_seq_combine(r_row
+ 1, gcd
, r_row
+ 1,
2246 r_row
[0], tab
->mat
->row
[d_row
] + 1,
2247 off
- 1 + tab
->n_col
);
2248 isl_int_mul(r_row
[0], r_row
[0], tab
->mat
->row
[d_row
][0]);
2251 col
= tab
->var
[tab
->n_var
- tab
->n_div
+ d
].index
;
2252 isl_int_set(r_row
[off
+ col
], tab
->mat
->row
[row
][0]);
2255 tab
->con
[r
].is_nonneg
= 1;
2256 if (isl_tab_push_var(tab
, isl_tab_undo_nonneg
, &tab
->con
[r
]) < 0)
2259 tab
->row_sign
[tab
->con
[r
].index
] = isl_tab_row_neg
;
2261 row
= tab
->con
[r
].index
;
2263 if (d
>= n
&& context
->op
->detect_equalities(context
, tab
) < 0)
2269 /* Construct a tableau for bmap that can be used for computing
2270 * the lexicographic minimum (or maximum) of bmap.
2271 * If not NULL, then dom is the domain where the minimum
2272 * should be computed. In this case, we set up a parametric
2273 * tableau with row signs (initialized to "unknown").
2274 * If M is set, then the tableau will use a big parameter.
2275 * If max is set, then a maximum should be computed instead of a minimum.
2276 * This means that for each variable x, the tableau will contain the variable
2277 * x' = M - x, rather than x' = M + x. This in turn means that the coefficient
2278 * of the variables in all constraints are negated prior to adding them
2281 static __isl_give
struct isl_tab
*tab_for_lexmin(__isl_keep isl_basic_map
*bmap
,
2282 __isl_keep isl_basic_set
*dom
, unsigned M
, int max
)
2285 struct isl_tab
*tab
;
2289 tab
= isl_tab_alloc(bmap
->ctx
, 2 * bmap
->n_eq
+ bmap
->n_ineq
+ 1,
2290 isl_basic_map_total_dim(bmap
), M
);
2294 tab
->rational
= ISL_F_ISSET(bmap
, ISL_BASIC_MAP_RATIONAL
);
2296 tab
->n_param
= isl_basic_set_total_dim(dom
) - dom
->n_div
;
2297 tab
->n_div
= dom
->n_div
;
2298 tab
->row_sign
= isl_calloc_array(bmap
->ctx
,
2299 enum isl_tab_row_sign
, tab
->mat
->n_row
);
2300 if (tab
->mat
->n_row
&& !tab
->row_sign
)
2303 if (ISL_F_ISSET(bmap
, ISL_BASIC_MAP_EMPTY
)) {
2304 if (isl_tab_mark_empty(tab
) < 0)
2309 for (i
= tab
->n_param
; i
< tab
->n_var
- tab
->n_div
; ++i
) {
2310 tab
->var
[i
].is_nonneg
= 1;
2311 tab
->var
[i
].frozen
= 1;
2313 o_var
= 1 + tab
->n_param
;
2314 n_var
= tab
->n_var
- tab
->n_param
- tab
->n_div
;
2315 for (i
= 0; i
< bmap
->n_eq
; ++i
) {
2317 isl_seq_neg(bmap
->eq
[i
] + o_var
,
2318 bmap
->eq
[i
] + o_var
, n_var
);
2319 tab
= add_lexmin_valid_eq(tab
, bmap
->eq
[i
]);
2321 isl_seq_neg(bmap
->eq
[i
] + o_var
,
2322 bmap
->eq
[i
] + o_var
, n_var
);
2323 if (!tab
|| tab
->empty
)
2326 if (bmap
->n_eq
&& restore_lexmin(tab
) < 0)
2328 for (i
= 0; i
< bmap
->n_ineq
; ++i
) {
2330 isl_seq_neg(bmap
->ineq
[i
] + o_var
,
2331 bmap
->ineq
[i
] + o_var
, n_var
);
2332 tab
= add_lexmin_ineq(tab
, bmap
->ineq
[i
]);
2334 isl_seq_neg(bmap
->ineq
[i
] + o_var
,
2335 bmap
->ineq
[i
] + o_var
, n_var
);
2336 if (!tab
|| tab
->empty
)
2345 /* Given a main tableau where more than one row requires a split,
2346 * determine and return the "best" row to split on.
2348 * If any of the rows requiring a split only involves
2349 * variables that also appear in the context tableau,
2350 * then the negative part is guaranteed not to have a solution.
2351 * It is therefore best to split on any of these rows first.
2354 * given two rows in the main tableau, if the inequality corresponding
2355 * to the first row is redundant with respect to that of the second row
2356 * in the current tableau, then it is better to split on the second row,
2357 * since in the positive part, both rows will be positive.
2358 * (In the negative part a pivot will have to be performed and just about
2359 * anything can happen to the sign of the other row.)
2361 * As a simple heuristic, we therefore select the row that makes the most
2362 * of the other rows redundant.
2364 * Perhaps it would also be useful to look at the number of constraints
2365 * that conflict with any given constraint.
2367 * best is the best row so far (-1 when we have not found any row yet).
2368 * best_r is the number of other rows made redundant by row best.
2369 * When best is still -1, bset_r is meaningless, but it is initialized
2370 * to some arbitrary value (0) anyway. Without this redundant initialization
2371 * valgrind may warn about uninitialized memory accesses when isl
2372 * is compiled with some versions of gcc.
2374 static int best_split(struct isl_tab
*tab
, struct isl_tab
*context_tab
)
2376 struct isl_tab_undo
*snap
;
2382 if (isl_tab_extend_cons(context_tab
, 2) < 0)
2385 snap
= isl_tab_snap(context_tab
);
2387 for (split
= tab
->n_redundant
; split
< tab
->n_row
; ++split
) {
2388 struct isl_tab_undo
*snap2
;
2389 struct isl_vec
*ineq
= NULL
;
2393 if (!isl_tab_var_from_row(tab
, split
)->is_nonneg
)
2395 if (tab
->row_sign
[split
] != isl_tab_row_any
)
2398 if (is_parametric_constant(tab
, split
))
2401 ineq
= get_row_parameter_ineq(tab
, split
);
2404 ok
= isl_tab_add_ineq(context_tab
, ineq
->el
) >= 0;
2409 snap2
= isl_tab_snap(context_tab
);
2411 for (row
= tab
->n_redundant
; row
< tab
->n_row
; ++row
) {
2412 struct isl_tab_var
*var
;
2416 if (!isl_tab_var_from_row(tab
, row
)->is_nonneg
)
2418 if (tab
->row_sign
[row
] != isl_tab_row_any
)
2421 ineq
= get_row_parameter_ineq(tab
, row
);
2424 ok
= isl_tab_add_ineq(context_tab
, ineq
->el
) >= 0;
2428 var
= &context_tab
->con
[context_tab
->n_con
- 1];
2429 if (!context_tab
->empty
&&
2430 !isl_tab_min_at_most_neg_one(context_tab
, var
))
2432 if (isl_tab_rollback(context_tab
, snap2
) < 0)
2435 if (best
== -1 || r
> best_r
) {
2439 if (isl_tab_rollback(context_tab
, snap
) < 0)
2446 static struct isl_basic_set
*context_lex_peek_basic_set(
2447 struct isl_context
*context
)
2449 struct isl_context_lex
*clex
= (struct isl_context_lex
*)context
;
2452 return isl_tab_peek_bset(clex
->tab
);
2455 static struct isl_tab
*context_lex_peek_tab(struct isl_context
*context
)
2457 struct isl_context_lex
*clex
= (struct isl_context_lex
*)context
;
2461 static void context_lex_add_eq(struct isl_context
*context
, isl_int
*eq
,
2462 int check
, int update
)
2464 struct isl_context_lex
*clex
= (struct isl_context_lex
*)context
;
2465 if (isl_tab_extend_cons(clex
->tab
, 2) < 0)
2467 if (add_lexmin_eq(clex
->tab
, eq
) < 0)
2470 int v
= tab_has_valid_sample(clex
->tab
, eq
, 1);
2474 clex
->tab
= check_integer_feasible(clex
->tab
);
2477 clex
->tab
= check_samples(clex
->tab
, eq
, 1);
2480 isl_tab_free(clex
->tab
);
2484 static void context_lex_add_ineq(struct isl_context
*context
, isl_int
*ineq
,
2485 int check
, int update
)
2487 struct isl_context_lex
*clex
= (struct isl_context_lex
*)context
;
2488 if (isl_tab_extend_cons(clex
->tab
, 1) < 0)
2490 clex
->tab
= add_lexmin_ineq(clex
->tab
, ineq
);
2492 int v
= tab_has_valid_sample(clex
->tab
, ineq
, 0);
2496 clex
->tab
= check_integer_feasible(clex
->tab
);
2499 clex
->tab
= check_samples(clex
->tab
, ineq
, 0);
2502 isl_tab_free(clex
->tab
);
2506 static isl_stat
context_lex_add_ineq_wrap(void *user
, isl_int
*ineq
)
2508 struct isl_context
*context
= (struct isl_context
*)user
;
2509 context_lex_add_ineq(context
, ineq
, 0, 0);
2510 return context
->op
->is_ok(context
) ? isl_stat_ok
: isl_stat_error
;
2513 /* Check which signs can be obtained by "ineq" on all the currently
2514 * active sample values. See row_sign for more information.
2516 static enum isl_tab_row_sign
tab_ineq_sign(struct isl_tab
*tab
, isl_int
*ineq
,
2522 enum isl_tab_row_sign res
= isl_tab_row_unknown
;
2524 isl_assert(tab
->mat
->ctx
, tab
->samples
, return isl_tab_row_unknown
);
2525 isl_assert(tab
->mat
->ctx
, tab
->samples
->n_col
== 1 + tab
->n_var
,
2526 return isl_tab_row_unknown
);
2529 for (i
= tab
->n_outside
; i
< tab
->n_sample
; ++i
) {
2530 isl_seq_inner_product(tab
->samples
->row
[i
], ineq
,
2531 1 + tab
->n_var
, &tmp
);
2532 sgn
= isl_int_sgn(tmp
);
2533 if (sgn
> 0 || (sgn
== 0 && strict
)) {
2534 if (res
== isl_tab_row_unknown
)
2535 res
= isl_tab_row_pos
;
2536 if (res
== isl_tab_row_neg
)
2537 res
= isl_tab_row_any
;
2540 if (res
== isl_tab_row_unknown
)
2541 res
= isl_tab_row_neg
;
2542 if (res
== isl_tab_row_pos
)
2543 res
= isl_tab_row_any
;
2545 if (res
== isl_tab_row_any
)
2553 static enum isl_tab_row_sign
context_lex_ineq_sign(struct isl_context
*context
,
2554 isl_int
*ineq
, int strict
)
2556 struct isl_context_lex
*clex
= (struct isl_context_lex
*)context
;
2557 return tab_ineq_sign(clex
->tab
, ineq
, strict
);
2560 /* Check whether "ineq" can be added to the tableau without rendering
2563 static int context_lex_test_ineq(struct isl_context
*context
, isl_int
*ineq
)
2565 struct isl_context_lex
*clex
= (struct isl_context_lex
*)context
;
2566 struct isl_tab_undo
*snap
;
2572 if (isl_tab_extend_cons(clex
->tab
, 1) < 0)
2575 snap
= isl_tab_snap(clex
->tab
);
2576 if (isl_tab_push_basis(clex
->tab
) < 0)
2578 clex
->tab
= add_lexmin_ineq(clex
->tab
, ineq
);
2579 clex
->tab
= check_integer_feasible(clex
->tab
);
2582 feasible
= !clex
->tab
->empty
;
2583 if (isl_tab_rollback(clex
->tab
, snap
) < 0)
2589 static int context_lex_get_div(struct isl_context
*context
, struct isl_tab
*tab
,
2590 struct isl_vec
*div
)
2592 return get_div(tab
, context
, div
);
2595 /* Insert a div specified by "div" to the context tableau at position "pos" and
2596 * return isl_bool_true if the div is obviously non-negative.
2597 * context_tab_add_div will always return isl_bool_true, because all variables
2598 * in a isl_context_lex tableau are non-negative.
2599 * However, if we are using a big parameter in the context, then this only
2600 * reflects the non-negativity of the variable used to _encode_ the
2601 * div, i.e., div' = M + div, so we can't draw any conclusions.
2603 static isl_bool
context_lex_insert_div(struct isl_context
*context
, int pos
,
2604 __isl_keep isl_vec
*div
)
2606 struct isl_context_lex
*clex
= (struct isl_context_lex
*)context
;
2608 nonneg
= context_tab_insert_div(clex
->tab
, pos
, div
,
2609 context_lex_add_ineq_wrap
, context
);
2611 return isl_bool_error
;
2613 return isl_bool_false
;
2617 static int context_lex_detect_equalities(struct isl_context
*context
,
2618 struct isl_tab
*tab
)
2623 static int context_lex_best_split(struct isl_context
*context
,
2624 struct isl_tab
*tab
)
2626 struct isl_context_lex
*clex
= (struct isl_context_lex
*)context
;
2627 struct isl_tab_undo
*snap
;
2630 snap
= isl_tab_snap(clex
->tab
);
2631 if (isl_tab_push_basis(clex
->tab
) < 0)
2633 r
= best_split(tab
, clex
->tab
);
2635 if (r
>= 0 && isl_tab_rollback(clex
->tab
, snap
) < 0)
2641 static int context_lex_is_empty(struct isl_context
*context
)
2643 struct isl_context_lex
*clex
= (struct isl_context_lex
*)context
;
2646 return clex
->tab
->empty
;
2649 static void *context_lex_save(struct isl_context
*context
)
2651 struct isl_context_lex
*clex
= (struct isl_context_lex
*)context
;
2652 struct isl_tab_undo
*snap
;
2654 snap
= isl_tab_snap(clex
->tab
);
2655 if (isl_tab_push_basis(clex
->tab
) < 0)
2657 if (isl_tab_save_samples(clex
->tab
) < 0)
2663 static void context_lex_restore(struct isl_context
*context
, void *save
)
2665 struct isl_context_lex
*clex
= (struct isl_context_lex
*)context
;
2666 if (isl_tab_rollback(clex
->tab
, (struct isl_tab_undo
*)save
) < 0) {
2667 isl_tab_free(clex
->tab
);
2672 static void context_lex_discard(void *save
)
2676 static int context_lex_is_ok(struct isl_context
*context
)
2678 struct isl_context_lex
*clex
= (struct isl_context_lex
*)context
;
2682 /* For each variable in the context tableau, check if the variable can
2683 * only attain non-negative values. If so, mark the parameter as non-negative
2684 * in the main tableau. This allows for a more direct identification of some
2685 * cases of violated constraints.
2687 static struct isl_tab
*tab_detect_nonnegative_parameters(struct isl_tab
*tab
,
2688 struct isl_tab
*context_tab
)
2691 struct isl_tab_undo
*snap
;
2692 struct isl_vec
*ineq
= NULL
;
2693 struct isl_tab_var
*var
;
2696 if (context_tab
->n_var
== 0)
2699 ineq
= isl_vec_alloc(tab
->mat
->ctx
, 1 + context_tab
->n_var
);
2703 if (isl_tab_extend_cons(context_tab
, 1) < 0)
2706 snap
= isl_tab_snap(context_tab
);
2709 isl_seq_clr(ineq
->el
, ineq
->size
);
2710 for (i
= 0; i
< context_tab
->n_var
; ++i
) {
2711 isl_int_set_si(ineq
->el
[1 + i
], 1);
2712 if (isl_tab_add_ineq(context_tab
, ineq
->el
) < 0)
2714 var
= &context_tab
->con
[context_tab
->n_con
- 1];
2715 if (!context_tab
->empty
&&
2716 !isl_tab_min_at_most_neg_one(context_tab
, var
)) {
2718 if (i
>= tab
->n_param
)
2719 j
= i
- tab
->n_param
+ tab
->n_var
- tab
->n_div
;
2720 tab
->var
[j
].is_nonneg
= 1;
2723 isl_int_set_si(ineq
->el
[1 + i
], 0);
2724 if (isl_tab_rollback(context_tab
, snap
) < 0)
2728 if (context_tab
->M
&& n
== context_tab
->n_var
) {
2729 context_tab
->mat
= isl_mat_drop_cols(context_tab
->mat
, 2, 1);
2741 static struct isl_tab
*context_lex_detect_nonnegative_parameters(
2742 struct isl_context
*context
, struct isl_tab
*tab
)
2744 struct isl_context_lex
*clex
= (struct isl_context_lex
*)context
;
2745 struct isl_tab_undo
*snap
;
2750 snap
= isl_tab_snap(clex
->tab
);
2751 if (isl_tab_push_basis(clex
->tab
) < 0)
2754 tab
= tab_detect_nonnegative_parameters(tab
, clex
->tab
);
2756 if (isl_tab_rollback(clex
->tab
, snap
) < 0)
2765 static void context_lex_invalidate(struct isl_context
*context
)
2767 struct isl_context_lex
*clex
= (struct isl_context_lex
*)context
;
2768 isl_tab_free(clex
->tab
);
2772 static __isl_null
struct isl_context
*context_lex_free(
2773 struct isl_context
*context
)
2775 struct isl_context_lex
*clex
= (struct isl_context_lex
*)context
;
2776 isl_tab_free(clex
->tab
);
2782 struct isl_context_op isl_context_lex_op
= {
2783 context_lex_detect_nonnegative_parameters
,
2784 context_lex_peek_basic_set
,
2785 context_lex_peek_tab
,
2787 context_lex_add_ineq
,
2788 context_lex_ineq_sign
,
2789 context_lex_test_ineq
,
2790 context_lex_get_div
,
2791 context_lex_insert_div
,
2792 context_lex_detect_equalities
,
2793 context_lex_best_split
,
2794 context_lex_is_empty
,
2797 context_lex_restore
,
2798 context_lex_discard
,
2799 context_lex_invalidate
,
2803 static struct isl_tab
*context_tab_for_lexmin(__isl_take isl_basic_set
*bset
)
2805 struct isl_tab
*tab
;
2809 tab
= tab_for_lexmin(bset_to_bmap(bset
), NULL
, 1, 0);
2810 if (isl_tab_track_bset(tab
, bset
) < 0)
2812 tab
= isl_tab_init_samples(tab
);
2819 static struct isl_context
*isl_context_lex_alloc(struct isl_basic_set
*dom
)
2821 struct isl_context_lex
*clex
;
2826 clex
= isl_alloc_type(dom
->ctx
, struct isl_context_lex
);
2830 clex
->context
.op
= &isl_context_lex_op
;
2832 clex
->tab
= context_tab_for_lexmin(isl_basic_set_copy(dom
));
2833 if (restore_lexmin(clex
->tab
) < 0)
2835 clex
->tab
= check_integer_feasible(clex
->tab
);
2839 return &clex
->context
;
2841 clex
->context
.op
->free(&clex
->context
);
2845 /* Representation of the context when using generalized basis reduction.
2847 * "shifted" contains the offsets of the unit hypercubes that lie inside the
2848 * context. Any rational point in "shifted" can therefore be rounded
2849 * up to an integer point in the context.
2850 * If the context is constrained by any equality, then "shifted" is not used
2851 * as it would be empty.
2853 struct isl_context_gbr
{
2854 struct isl_context context
;
2855 struct isl_tab
*tab
;
2856 struct isl_tab
*shifted
;
2857 struct isl_tab
*cone
;
2860 static struct isl_tab
*context_gbr_detect_nonnegative_parameters(
2861 struct isl_context
*context
, struct isl_tab
*tab
)
2863 struct isl_context_gbr
*cgbr
= (struct isl_context_gbr
*)context
;
2866 return tab_detect_nonnegative_parameters(tab
, cgbr
->tab
);
2869 static struct isl_basic_set
*context_gbr_peek_basic_set(
2870 struct isl_context
*context
)
2872 struct isl_context_gbr
*cgbr
= (struct isl_context_gbr
*)context
;
2875 return isl_tab_peek_bset(cgbr
->tab
);
2878 static struct isl_tab
*context_gbr_peek_tab(struct isl_context
*context
)
2880 struct isl_context_gbr
*cgbr
= (struct isl_context_gbr
*)context
;
2884 /* Initialize the "shifted" tableau of the context, which
2885 * contains the constraints of the original tableau shifted
2886 * by the sum of all negative coefficients. This ensures
2887 * that any rational point in the shifted tableau can
2888 * be rounded up to yield an integer point in the original tableau.
2890 static void gbr_init_shifted(struct isl_context_gbr
*cgbr
)
2893 struct isl_vec
*cst
;
2894 struct isl_basic_set
*bset
= isl_tab_peek_bset(cgbr
->tab
);
2895 unsigned dim
= isl_basic_set_total_dim(bset
);
2897 cst
= isl_vec_alloc(cgbr
->tab
->mat
->ctx
, bset
->n_ineq
);
2901 for (i
= 0; i
< bset
->n_ineq
; ++i
) {
2902 isl_int_set(cst
->el
[i
], bset
->ineq
[i
][0]);
2903 for (j
= 0; j
< dim
; ++j
) {
2904 if (!isl_int_is_neg(bset
->ineq
[i
][1 + j
]))
2906 isl_int_add(bset
->ineq
[i
][0], bset
->ineq
[i
][0],
2907 bset
->ineq
[i
][1 + j
]);
2911 cgbr
->shifted
= isl_tab_from_basic_set(bset
, 0);
2913 for (i
= 0; i
< bset
->n_ineq
; ++i
)
2914 isl_int_set(bset
->ineq
[i
][0], cst
->el
[i
]);
2919 /* Check if the shifted tableau is non-empty, and if so
2920 * use the sample point to construct an integer point
2921 * of the context tableau.
2923 static struct isl_vec
*gbr_get_shifted_sample(struct isl_context_gbr
*cgbr
)
2925 struct isl_vec
*sample
;
2928 gbr_init_shifted(cgbr
);
2931 if (cgbr
->shifted
->empty
)
2932 return isl_vec_alloc(cgbr
->tab
->mat
->ctx
, 0);
2934 sample
= isl_tab_get_sample_value(cgbr
->shifted
);
2935 sample
= isl_vec_ceil(sample
);
2940 static __isl_give isl_basic_set
*drop_constant_terms(
2941 __isl_take isl_basic_set
*bset
)
2948 for (i
= 0; i
< bset
->n_eq
; ++i
)
2949 isl_int_set_si(bset
->eq
[i
][0], 0);
2951 for (i
= 0; i
< bset
->n_ineq
; ++i
)
2952 isl_int_set_si(bset
->ineq
[i
][0], 0);
2957 static int use_shifted(struct isl_context_gbr
*cgbr
)
2961 return cgbr
->tab
->bmap
->n_eq
== 0 && cgbr
->tab
->bmap
->n_div
== 0;
2964 static struct isl_vec
*gbr_get_sample(struct isl_context_gbr
*cgbr
)
2966 struct isl_basic_set
*bset
;
2967 struct isl_basic_set
*cone
;
2969 if (isl_tab_sample_is_integer(cgbr
->tab
))
2970 return isl_tab_get_sample_value(cgbr
->tab
);
2972 if (use_shifted(cgbr
)) {
2973 struct isl_vec
*sample
;
2975 sample
= gbr_get_shifted_sample(cgbr
);
2976 if (!sample
|| sample
->size
> 0)
2979 isl_vec_free(sample
);
2983 bset
= isl_tab_peek_bset(cgbr
->tab
);
2984 cgbr
->cone
= isl_tab_from_recession_cone(bset
, 0);
2987 if (isl_tab_track_bset(cgbr
->cone
,
2988 isl_basic_set_copy(bset
)) < 0)
2991 if (isl_tab_detect_implicit_equalities(cgbr
->cone
) < 0)
2994 if (cgbr
->cone
->n_dead
== cgbr
->cone
->n_col
) {
2995 struct isl_vec
*sample
;
2996 struct isl_tab_undo
*snap
;
2998 if (cgbr
->tab
->basis
) {
2999 if (cgbr
->tab
->basis
->n_col
!= 1 + cgbr
->tab
->n_var
) {
3000 isl_mat_free(cgbr
->tab
->basis
);
3001 cgbr
->tab
->basis
= NULL
;
3003 cgbr
->tab
->n_zero
= 0;
3004 cgbr
->tab
->n_unbounded
= 0;
3007 snap
= isl_tab_snap(cgbr
->tab
);
3009 sample
= isl_tab_sample(cgbr
->tab
);
3011 if (!sample
|| isl_tab_rollback(cgbr
->tab
, snap
) < 0) {
3012 isl_vec_free(sample
);
3019 cone
= isl_basic_set_dup(isl_tab_peek_bset(cgbr
->cone
));
3020 cone
= drop_constant_terms(cone
);
3021 cone
= isl_basic_set_update_from_tab(cone
, cgbr
->cone
);
3022 cone
= isl_basic_set_underlying_set(cone
);
3023 cone
= isl_basic_set_gauss(cone
, NULL
);
3025 bset
= isl_basic_set_dup(isl_tab_peek_bset(cgbr
->tab
));
3026 bset
= isl_basic_set_update_from_tab(bset
, cgbr
->tab
);
3027 bset
= isl_basic_set_underlying_set(bset
);
3028 bset
= isl_basic_set_gauss(bset
, NULL
);
3030 return isl_basic_set_sample_with_cone(bset
, cone
);
3033 static void check_gbr_integer_feasible(struct isl_context_gbr
*cgbr
)
3035 struct isl_vec
*sample
;
3040 if (cgbr
->tab
->empty
)
3043 sample
= gbr_get_sample(cgbr
);
3047 if (sample
->size
== 0) {
3048 isl_vec_free(sample
);
3049 if (isl_tab_mark_empty(cgbr
->tab
) < 0)
3054 if (isl_tab_add_sample(cgbr
->tab
, sample
) < 0)
3059 isl_tab_free(cgbr
->tab
);
3063 static struct isl_tab
*add_gbr_eq(struct isl_tab
*tab
, isl_int
*eq
)
3068 if (isl_tab_extend_cons(tab
, 2) < 0)
3071 if (isl_tab_add_eq(tab
, eq
) < 0)
3080 /* Add the equality described by "eq" to the context.
3081 * If "check" is set, then we check if the context is empty after
3082 * adding the equality.
3083 * If "update" is set, then we check if the samples are still valid.
3085 * We do not explicitly add shifted copies of the equality to
3086 * cgbr->shifted since they would conflict with each other.
3087 * Instead, we directly mark cgbr->shifted empty.
3089 static void context_gbr_add_eq(struct isl_context
*context
, isl_int
*eq
,
3090 int check
, int update
)
3092 struct isl_context_gbr
*cgbr
= (struct isl_context_gbr
*)context
;
3094 cgbr
->tab
= add_gbr_eq(cgbr
->tab
, eq
);
3096 if (cgbr
->shifted
&& !cgbr
->shifted
->empty
&& use_shifted(cgbr
)) {
3097 if (isl_tab_mark_empty(cgbr
->shifted
) < 0)
3101 if (cgbr
->cone
&& cgbr
->cone
->n_col
!= cgbr
->cone
->n_dead
) {
3102 if (isl_tab_extend_cons(cgbr
->cone
, 2) < 0)
3104 if (isl_tab_add_eq(cgbr
->cone
, eq
) < 0)
3109 int v
= tab_has_valid_sample(cgbr
->tab
, eq
, 1);
3113 check_gbr_integer_feasible(cgbr
);
3116 cgbr
->tab
= check_samples(cgbr
->tab
, eq
, 1);
3119 isl_tab_free(cgbr
->tab
);
3123 static void add_gbr_ineq(struct isl_context_gbr
*cgbr
, isl_int
*ineq
)
3128 if (isl_tab_extend_cons(cgbr
->tab
, 1) < 0)
3131 if (isl_tab_add_ineq(cgbr
->tab
, ineq
) < 0)
3134 if (cgbr
->shifted
&& !cgbr
->shifted
->empty
&& use_shifted(cgbr
)) {
3137 dim
= isl_basic_map_total_dim(cgbr
->tab
->bmap
);
3139 if (isl_tab_extend_cons(cgbr
->shifted
, 1) < 0)
3142 for (i
= 0; i
< dim
; ++i
) {
3143 if (!isl_int_is_neg(ineq
[1 + i
]))
3145 isl_int_add(ineq
[0], ineq
[0], ineq
[1 + i
]);
3148 if (isl_tab_add_ineq(cgbr
->shifted
, ineq
) < 0)
3151 for (i
= 0; i
< dim
; ++i
) {
3152 if (!isl_int_is_neg(ineq
[1 + i
]))
3154 isl_int_sub(ineq
[0], ineq
[0], ineq
[1 + i
]);
3158 if (cgbr
->cone
&& cgbr
->cone
->n_col
!= cgbr
->cone
->n_dead
) {
3159 if (isl_tab_extend_cons(cgbr
->cone
, 1) < 0)
3161 if (isl_tab_add_ineq(cgbr
->cone
, ineq
) < 0)
3167 isl_tab_free(cgbr
->tab
);
3171 static void context_gbr_add_ineq(struct isl_context
*context
, isl_int
*ineq
,
3172 int check
, int update
)
3174 struct isl_context_gbr
*cgbr
= (struct isl_context_gbr
*)context
;
3176 add_gbr_ineq(cgbr
, ineq
);
3181 int v
= tab_has_valid_sample(cgbr
->tab
, ineq
, 0);
3185 check_gbr_integer_feasible(cgbr
);
3188 cgbr
->tab
= check_samples(cgbr
->tab
, ineq
, 0);
3191 isl_tab_free(cgbr
->tab
);
3195 static isl_stat
context_gbr_add_ineq_wrap(void *user
, isl_int
*ineq
)
3197 struct isl_context
*context
= (struct isl_context
*)user
;
3198 context_gbr_add_ineq(context
, ineq
, 0, 0);
3199 return context
->op
->is_ok(context
) ? isl_stat_ok
: isl_stat_error
;
3202 static enum isl_tab_row_sign
context_gbr_ineq_sign(struct isl_context
*context
,
3203 isl_int
*ineq
, int strict
)
3205 struct isl_context_gbr
*cgbr
= (struct isl_context_gbr
*)context
;
3206 return tab_ineq_sign(cgbr
->tab
, ineq
, strict
);
3209 /* Check whether "ineq" can be added to the tableau without rendering
3212 static int context_gbr_test_ineq(struct isl_context
*context
, isl_int
*ineq
)
3214 struct isl_context_gbr
*cgbr
= (struct isl_context_gbr
*)context
;
3215 struct isl_tab_undo
*snap
;
3216 struct isl_tab_undo
*shifted_snap
= NULL
;
3217 struct isl_tab_undo
*cone_snap
= NULL
;
3223 if (isl_tab_extend_cons(cgbr
->tab
, 1) < 0)
3226 snap
= isl_tab_snap(cgbr
->tab
);
3228 shifted_snap
= isl_tab_snap(cgbr
->shifted
);
3230 cone_snap
= isl_tab_snap(cgbr
->cone
);
3231 add_gbr_ineq(cgbr
, ineq
);
3232 check_gbr_integer_feasible(cgbr
);
3235 feasible
= !cgbr
->tab
->empty
;
3236 if (isl_tab_rollback(cgbr
->tab
, snap
) < 0)
3239 if (isl_tab_rollback(cgbr
->shifted
, shifted_snap
))
3241 } else if (cgbr
->shifted
) {
3242 isl_tab_free(cgbr
->shifted
);
3243 cgbr
->shifted
= NULL
;
3246 if (isl_tab_rollback(cgbr
->cone
, cone_snap
))
3248 } else if (cgbr
->cone
) {
3249 isl_tab_free(cgbr
->cone
);
3256 /* Return the column of the last of the variables associated to
3257 * a column that has a non-zero coefficient.
3258 * This function is called in a context where only coefficients
3259 * of parameters or divs can be non-zero.
3261 static int last_non_zero_var_col(struct isl_tab
*tab
, isl_int
*p
)
3266 if (tab
->n_var
== 0)
3269 for (i
= tab
->n_var
- 1; i
>= 0; --i
) {
3270 if (i
>= tab
->n_param
&& i
< tab
->n_var
- tab
->n_div
)
3272 if (tab
->var
[i
].is_row
)
3274 col
= tab
->var
[i
].index
;
3275 if (!isl_int_is_zero(p
[col
]))
3282 /* Look through all the recently added equalities in the context
3283 * to see if we can propagate any of them to the main tableau.
3285 * The newly added equalities in the context are encoded as pairs
3286 * of inequalities starting at inequality "first".
3288 * We tentatively add each of these equalities to the main tableau
3289 * and if this happens to result in a row with a final coefficient
3290 * that is one or negative one, we use it to kill a column
3291 * in the main tableau. Otherwise, we discard the tentatively
3293 * This tentative addition of equality constraints turns
3294 * on the undo facility of the tableau. Turn it off again
3295 * at the end, assuming it was turned off to begin with.
3297 * Return 0 on success and -1 on failure.
3299 static int propagate_equalities(struct isl_context_gbr
*cgbr
,
3300 struct isl_tab
*tab
, unsigned first
)
3303 struct isl_vec
*eq
= NULL
;
3304 isl_bool needs_undo
;
3306 needs_undo
= isl_tab_need_undo(tab
);
3309 eq
= isl_vec_alloc(tab
->mat
->ctx
, 1 + tab
->n_var
);
3313 if (isl_tab_extend_cons(tab
, (cgbr
->tab
->bmap
->n_ineq
- first
)/2) < 0)
3316 isl_seq_clr(eq
->el
+ 1 + tab
->n_param
,
3317 tab
->n_var
- tab
->n_param
- tab
->n_div
);
3318 for (i
= first
; i
< cgbr
->tab
->bmap
->n_ineq
; i
+= 2) {
3321 struct isl_tab_undo
*snap
;
3322 snap
= isl_tab_snap(tab
);
3324 isl_seq_cpy(eq
->el
, cgbr
->tab
->bmap
->ineq
[i
], 1 + tab
->n_param
);
3325 isl_seq_cpy(eq
->el
+ 1 + tab
->n_var
- tab
->n_div
,
3326 cgbr
->tab
->bmap
->ineq
[i
] + 1 + tab
->n_param
,
3329 r
= isl_tab_add_row(tab
, eq
->el
);
3332 r
= tab
->con
[r
].index
;
3333 j
= last_non_zero_var_col(tab
, tab
->mat
->row
[r
] + 2 + tab
->M
);
3334 if (j
< 0 || j
< tab
->n_dead
||
3335 !isl_int_is_one(tab
->mat
->row
[r
][0]) ||
3336 (!isl_int_is_one(tab
->mat
->row
[r
][2 + tab
->M
+ j
]) &&
3337 !isl_int_is_negone(tab
->mat
->row
[r
][2 + tab
->M
+ j
]))) {
3338 if (isl_tab_rollback(tab
, snap
) < 0)
3342 if (isl_tab_pivot(tab
, r
, j
) < 0)
3344 if (isl_tab_kill_col(tab
, j
) < 0)
3347 if (restore_lexmin(tab
) < 0)
3352 isl_tab_clear_undo(tab
);
3358 isl_tab_free(cgbr
->tab
);
3363 static int context_gbr_detect_equalities(struct isl_context
*context
,
3364 struct isl_tab
*tab
)
3366 struct isl_context_gbr
*cgbr
= (struct isl_context_gbr
*)context
;
3370 struct isl_basic_set
*bset
= isl_tab_peek_bset(cgbr
->tab
);
3371 cgbr
->cone
= isl_tab_from_recession_cone(bset
, 0);
3374 if (isl_tab_track_bset(cgbr
->cone
,
3375 isl_basic_set_copy(bset
)) < 0)
3378 if (isl_tab_detect_implicit_equalities(cgbr
->cone
) < 0)
3381 n_ineq
= cgbr
->tab
->bmap
->n_ineq
;
3382 cgbr
->tab
= isl_tab_detect_equalities(cgbr
->tab
, cgbr
->cone
);
3385 if (cgbr
->tab
->bmap
->n_ineq
> n_ineq
&&
3386 propagate_equalities(cgbr
, tab
, n_ineq
) < 0)
3391 isl_tab_free(cgbr
->tab
);
3396 static int context_gbr_get_div(struct isl_context
*context
, struct isl_tab
*tab
,
3397 struct isl_vec
*div
)
3399 return get_div(tab
, context
, div
);
3402 static isl_bool
context_gbr_insert_div(struct isl_context
*context
, int pos
,
3403 __isl_keep isl_vec
*div
)
3405 struct isl_context_gbr
*cgbr
= (struct isl_context_gbr
*)context
;
3407 int r
, n_div
, o_div
;
3409 n_div
= isl_basic_map_dim(cgbr
->cone
->bmap
, isl_dim_div
);
3410 o_div
= cgbr
->cone
->n_var
- n_div
;
3412 if (isl_tab_extend_cons(cgbr
->cone
, 3) < 0)
3413 return isl_bool_error
;
3414 if (isl_tab_extend_vars(cgbr
->cone
, 1) < 0)
3415 return isl_bool_error
;
3416 if ((r
= isl_tab_insert_var(cgbr
->cone
, pos
)) <0)
3417 return isl_bool_error
;
3419 cgbr
->cone
->bmap
= isl_basic_map_insert_div(cgbr
->cone
->bmap
,
3421 if (!cgbr
->cone
->bmap
)
3422 return isl_bool_error
;
3423 if (isl_tab_push_var(cgbr
->cone
, isl_tab_undo_bmap_div
,
3424 &cgbr
->cone
->var
[r
]) < 0)
3425 return isl_bool_error
;
3427 return context_tab_insert_div(cgbr
->tab
, pos
, div
,
3428 context_gbr_add_ineq_wrap
, context
);
3431 static int context_gbr_best_split(struct isl_context
*context
,
3432 struct isl_tab
*tab
)
3434 struct isl_context_gbr
*cgbr
= (struct isl_context_gbr
*)context
;
3435 struct isl_tab_undo
*snap
;
3438 snap
= isl_tab_snap(cgbr
->tab
);
3439 r
= best_split(tab
, cgbr
->tab
);
3441 if (r
>= 0 && isl_tab_rollback(cgbr
->tab
, snap
) < 0)
3447 static int context_gbr_is_empty(struct isl_context
*context
)
3449 struct isl_context_gbr
*cgbr
= (struct isl_context_gbr
*)context
;
3452 return cgbr
->tab
->empty
;
3455 struct isl_gbr_tab_undo
{
3456 struct isl_tab_undo
*tab_snap
;
3457 struct isl_tab_undo
*shifted_snap
;
3458 struct isl_tab_undo
*cone_snap
;
3461 static void *context_gbr_save(struct isl_context
*context
)
3463 struct isl_context_gbr
*cgbr
= (struct isl_context_gbr
*)context
;
3464 struct isl_gbr_tab_undo
*snap
;
3469 snap
= isl_alloc_type(cgbr
->tab
->mat
->ctx
, struct isl_gbr_tab_undo
);
3473 snap
->tab_snap
= isl_tab_snap(cgbr
->tab
);
3474 if (isl_tab_save_samples(cgbr
->tab
) < 0)
3478 snap
->shifted_snap
= isl_tab_snap(cgbr
->shifted
);
3480 snap
->shifted_snap
= NULL
;
3483 snap
->cone_snap
= isl_tab_snap(cgbr
->cone
);
3485 snap
->cone_snap
= NULL
;
3493 static void context_gbr_restore(struct isl_context
*context
, void *save
)
3495 struct isl_context_gbr
*cgbr
= (struct isl_context_gbr
*)context
;
3496 struct isl_gbr_tab_undo
*snap
= (struct isl_gbr_tab_undo
*)save
;
3499 if (isl_tab_rollback(cgbr
->tab
, snap
->tab_snap
) < 0)
3502 if (snap
->shifted_snap
) {
3503 if (isl_tab_rollback(cgbr
->shifted
, snap
->shifted_snap
) < 0)
3505 } else if (cgbr
->shifted
) {
3506 isl_tab_free(cgbr
->shifted
);
3507 cgbr
->shifted
= NULL
;
3510 if (snap
->cone_snap
) {
3511 if (isl_tab_rollback(cgbr
->cone
, snap
->cone_snap
) < 0)
3513 } else if (cgbr
->cone
) {
3514 isl_tab_free(cgbr
->cone
);
3523 isl_tab_free(cgbr
->tab
);
3527 static void context_gbr_discard(void *save
)
3529 struct isl_gbr_tab_undo
*snap
= (struct isl_gbr_tab_undo
*)save
;
3533 static int context_gbr_is_ok(struct isl_context
*context
)
3535 struct isl_context_gbr
*cgbr
= (struct isl_context_gbr
*)context
;
3539 static void context_gbr_invalidate(struct isl_context
*context
)
3541 struct isl_context_gbr
*cgbr
= (struct isl_context_gbr
*)context
;
3542 isl_tab_free(cgbr
->tab
);
3546 static __isl_null
struct isl_context
*context_gbr_free(
3547 struct isl_context
*context
)
3549 struct isl_context_gbr
*cgbr
= (struct isl_context_gbr
*)context
;
3550 isl_tab_free(cgbr
->tab
);
3551 isl_tab_free(cgbr
->shifted
);
3552 isl_tab_free(cgbr
->cone
);
3558 struct isl_context_op isl_context_gbr_op
= {
3559 context_gbr_detect_nonnegative_parameters
,
3560 context_gbr_peek_basic_set
,
3561 context_gbr_peek_tab
,
3563 context_gbr_add_ineq
,
3564 context_gbr_ineq_sign
,
3565 context_gbr_test_ineq
,
3566 context_gbr_get_div
,
3567 context_gbr_insert_div
,
3568 context_gbr_detect_equalities
,
3569 context_gbr_best_split
,
3570 context_gbr_is_empty
,
3573 context_gbr_restore
,
3574 context_gbr_discard
,
3575 context_gbr_invalidate
,
3579 static struct isl_context
*isl_context_gbr_alloc(__isl_keep isl_basic_set
*dom
)
3581 struct isl_context_gbr
*cgbr
;
3586 cgbr
= isl_calloc_type(dom
->ctx
, struct isl_context_gbr
);
3590 cgbr
->context
.op
= &isl_context_gbr_op
;
3592 cgbr
->shifted
= NULL
;
3594 cgbr
->tab
= isl_tab_from_basic_set(dom
, 1);
3595 cgbr
->tab
= isl_tab_init_samples(cgbr
->tab
);
3598 check_gbr_integer_feasible(cgbr
);
3600 return &cgbr
->context
;
3602 cgbr
->context
.op
->free(&cgbr
->context
);
3606 /* Allocate a context corresponding to "dom".
3607 * The representation specific fields are initialized by
3608 * isl_context_lex_alloc or isl_context_gbr_alloc.
3609 * The shared "n_unknown" field is initialized to the number
3610 * of final unknown integer divisions in "dom".
3612 static struct isl_context
*isl_context_alloc(__isl_keep isl_basic_set
*dom
)
3614 struct isl_context
*context
;
3620 if (dom
->ctx
->opt
->context
== ISL_CONTEXT_LEXMIN
)
3621 context
= isl_context_lex_alloc(dom
);
3623 context
= isl_context_gbr_alloc(dom
);
3628 first
= isl_basic_set_first_unknown_div(dom
);
3630 return context
->op
->free(context
);
3631 context
->n_unknown
= isl_basic_set_dim(dom
, isl_dim_div
) - first
;
3636 /* Initialize some common fields of "sol", which keeps track
3637 * of the solution of an optimization problem on "bmap" over
3639 * If "max" is set, then a maximization problem is being solved, rather than
3640 * a minimization problem, which means that the variables in the
3641 * tableau have value "M - x" rather than "M + x".
3643 static isl_stat
sol_init(struct isl_sol
*sol
, __isl_keep isl_basic_map
*bmap
,
3644 __isl_keep isl_basic_set
*dom
, int max
)
3646 sol
->rational
= ISL_F_ISSET(bmap
, ISL_BASIC_MAP_RATIONAL
);
3647 sol
->dec_level
.callback
.run
= &sol_dec_level_wrap
;
3648 sol
->dec_level
.sol
= sol
;
3650 sol
->n_out
= isl_basic_map_dim(bmap
, isl_dim_out
);
3651 sol
->space
= isl_basic_map_get_space(bmap
);
3653 sol
->context
= isl_context_alloc(dom
);
3654 if (!sol
->space
|| !sol
->context
)
3655 return isl_stat_error
;
3660 /* Construct an isl_sol_map structure for accumulating the solution.
3661 * If track_empty is set, then we also keep track of the parts
3662 * of the context where there is no solution.
3663 * If max is set, then we are solving a maximization, rather than
3664 * a minimization problem, which means that the variables in the
3665 * tableau have value "M - x" rather than "M + x".
3667 static struct isl_sol
*sol_map_init(__isl_keep isl_basic_map
*bmap
,
3668 __isl_take isl_basic_set
*dom
, int track_empty
, int max
)
3670 struct isl_sol_map
*sol_map
= NULL
;
3676 sol_map
= isl_calloc_type(bmap
->ctx
, struct isl_sol_map
);
3680 sol_map
->sol
.free
= &sol_map_free
;
3681 if (sol_init(&sol_map
->sol
, bmap
, dom
, max
) < 0)
3683 sol_map
->sol
.add
= &sol_map_add_wrap
;
3684 sol_map
->sol
.add_empty
= track_empty
? &sol_map_add_empty_wrap
: NULL
;
3685 space
= isl_space_copy(sol_map
->sol
.space
);
3686 sol_map
->map
= isl_map_alloc_space(space
, 1, ISL_MAP_DISJOINT
);
3691 sol_map
->empty
= isl_set_alloc_space(isl_basic_set_get_space(dom
),
3692 1, ISL_SET_DISJOINT
);
3693 if (!sol_map
->empty
)
3697 isl_basic_set_free(dom
);
3698 return &sol_map
->sol
;
3700 isl_basic_set_free(dom
);
3701 sol_free(&sol_map
->sol
);
3705 /* Check whether all coefficients of (non-parameter) variables
3706 * are non-positive, meaning that no pivots can be performed on the row.
3708 static int is_critical(struct isl_tab
*tab
, int row
)
3711 unsigned off
= 2 + tab
->M
;
3713 for (j
= tab
->n_dead
; j
< tab
->n_col
; ++j
) {
3714 if (col_is_parameter_var(tab
, j
))
3717 if (isl_int_is_pos(tab
->mat
->row
[row
][off
+ j
]))
3724 /* Check whether the inequality represented by vec is strict over the integers,
3725 * i.e., there are no integer values satisfying the constraint with
3726 * equality. This happens if the gcd of the coefficients is not a divisor
3727 * of the constant term. If so, scale the constraint down by the gcd
3728 * of the coefficients.
3730 static int is_strict(struct isl_vec
*vec
)
3736 isl_seq_gcd(vec
->el
+ 1, vec
->size
- 1, &gcd
);
3737 if (!isl_int_is_one(gcd
)) {
3738 strict
= !isl_int_is_divisible_by(vec
->el
[0], gcd
);
3739 isl_int_fdiv_q(vec
->el
[0], vec
->el
[0], gcd
);
3740 isl_seq_scale_down(vec
->el
+ 1, vec
->el
+ 1, gcd
, vec
->size
-1);
3747 /* Determine the sign of the given row of the main tableau.
3748 * The result is one of
3749 * isl_tab_row_pos: always non-negative; no pivot needed
3750 * isl_tab_row_neg: always non-positive; pivot
3751 * isl_tab_row_any: can be both positive and negative; split
3753 * We first handle some simple cases
3754 * - the row sign may be known already
3755 * - the row may be obviously non-negative
3756 * - the parametric constant may be equal to that of another row
3757 * for which we know the sign. This sign will be either "pos" or
3758 * "any". If it had been "neg" then we would have pivoted before.
3760 * If none of these cases hold, we check the value of the row for each
3761 * of the currently active samples. Based on the signs of these values
3762 * we make an initial determination of the sign of the row.
3764 * all zero -> unk(nown)
3765 * all non-negative -> pos
3766 * all non-positive -> neg
3767 * both negative and positive -> all
3769 * If we end up with "all", we are done.
3770 * Otherwise, we perform a check for positive and/or negative
3771 * values as follows.
3773 * samples neg unk pos
3779 * There is no special sign for "zero", because we can usually treat zero
3780 * as either non-negative or non-positive, whatever works out best.
3781 * However, if the row is "critical", meaning that pivoting is impossible
3782 * then we don't want to limp zero with the non-positive case, because
3783 * then we we would lose the solution for those values of the parameters
3784 * where the value of the row is zero. Instead, we treat 0 as non-negative
3785 * ensuring a split if the row can attain both zero and negative values.
3786 * The same happens when the original constraint was one that could not
3787 * be satisfied with equality by any integer values of the parameters.
3788 * In this case, we normalize the constraint, but then a value of zero
3789 * for the normalized constraint is actually a positive value for the
3790 * original constraint, so again we need to treat zero as non-negative.
3791 * In both these cases, we have the following decision tree instead:
3793 * all non-negative -> pos
3794 * all negative -> neg
3795 * both negative and non-negative -> all
3803 static enum isl_tab_row_sign
row_sign(struct isl_tab
*tab
,
3804 struct isl_sol
*sol
, int row
)
3806 struct isl_vec
*ineq
= NULL
;
3807 enum isl_tab_row_sign res
= isl_tab_row_unknown
;
3812 if (tab
->row_sign
[row
] != isl_tab_row_unknown
)
3813 return tab
->row_sign
[row
];
3814 if (is_obviously_nonneg(tab
, row
))
3815 return isl_tab_row_pos
;
3816 for (row2
= tab
->n_redundant
; row2
< tab
->n_row
; ++row2
) {
3817 if (tab
->row_sign
[row2
] == isl_tab_row_unknown
)
3819 if (identical_parameter_line(tab
, row
, row2
))
3820 return tab
->row_sign
[row2
];
3823 critical
= is_critical(tab
, row
);
3825 ineq
= get_row_parameter_ineq(tab
, row
);
3829 strict
= is_strict(ineq
);
3831 res
= sol
->context
->op
->ineq_sign(sol
->context
, ineq
->el
,
3832 critical
|| strict
);
3834 if (res
== isl_tab_row_unknown
|| res
== isl_tab_row_pos
) {
3835 /* test for negative values */
3837 isl_seq_neg(ineq
->el
, ineq
->el
, ineq
->size
);
3838 isl_int_sub_ui(ineq
->el
[0], ineq
->el
[0], 1);
3840 feasible
= sol
->context
->op
->test_ineq(sol
->context
, ineq
->el
);
3844 res
= isl_tab_row_pos
;
3846 res
= (res
== isl_tab_row_unknown
) ? isl_tab_row_neg
3848 if (res
== isl_tab_row_neg
) {
3849 isl_seq_neg(ineq
->el
, ineq
->el
, ineq
->size
);
3850 isl_int_sub_ui(ineq
->el
[0], ineq
->el
[0], 1);
3854 if (res
== isl_tab_row_neg
) {
3855 /* test for positive values */
3857 if (!critical
&& !strict
)
3858 isl_int_sub_ui(ineq
->el
[0], ineq
->el
[0], 1);
3860 feasible
= sol
->context
->op
->test_ineq(sol
->context
, ineq
->el
);
3864 res
= isl_tab_row_any
;
3871 return isl_tab_row_unknown
;
3874 static void find_solutions(struct isl_sol
*sol
, struct isl_tab
*tab
);
3876 /* Find solutions for values of the parameters that satisfy the given
3879 * We currently take a snapshot of the context tableau that is reset
3880 * when we return from this function, while we make a copy of the main
3881 * tableau, leaving the original main tableau untouched.
3882 * These are fairly arbitrary choices. Making a copy also of the context
3883 * tableau would obviate the need to undo any changes made to it later,
3884 * while taking a snapshot of the main tableau could reduce memory usage.
3885 * If we were to switch to taking a snapshot of the main tableau,
3886 * we would have to keep in mind that we need to save the row signs
3887 * and that we need to do this before saving the current basis
3888 * such that the basis has been restore before we restore the row signs.
3890 static void find_in_pos(struct isl_sol
*sol
, struct isl_tab
*tab
, isl_int
*ineq
)
3896 saved
= sol
->context
->op
->save(sol
->context
);
3898 tab
= isl_tab_dup(tab
);
3902 sol
->context
->op
->add_ineq(sol
->context
, ineq
, 0, 1);
3904 find_solutions(sol
, tab
);
3907 sol
->context
->op
->restore(sol
->context
, saved
);
3909 sol
->context
->op
->discard(saved
);
3915 /* Record the absence of solutions for those values of the parameters
3916 * that do not satisfy the given inequality with equality.
3918 static void no_sol_in_strict(struct isl_sol
*sol
,
3919 struct isl_tab
*tab
, struct isl_vec
*ineq
)
3924 if (!sol
->context
|| sol
->error
)
3926 saved
= sol
->context
->op
->save(sol
->context
);
3928 isl_int_sub_ui(ineq
->el
[0], ineq
->el
[0], 1);
3930 sol
->context
->op
->add_ineq(sol
->context
, ineq
->el
, 1, 0);
3939 isl_int_add_ui(ineq
->el
[0], ineq
->el
[0], 1);
3941 sol
->context
->op
->restore(sol
->context
, saved
);
3947 /* Reset all row variables that are marked to have a sign that may
3948 * be both positive and negative to have an unknown sign.
3950 static void reset_any_to_unknown(struct isl_tab
*tab
)
3954 for (row
= tab
->n_redundant
; row
< tab
->n_row
; ++row
) {
3955 if (!isl_tab_var_from_row(tab
, row
)->is_nonneg
)
3957 if (tab
->row_sign
[row
] == isl_tab_row_any
)
3958 tab
->row_sign
[row
] = isl_tab_row_unknown
;
3962 /* Compute the lexicographic minimum of the set represented by the main
3963 * tableau "tab" within the context "sol->context_tab".
3964 * On entry the sample value of the main tableau is lexicographically
3965 * less than or equal to this lexicographic minimum.
3966 * Pivots are performed until a feasible point is found, which is then
3967 * necessarily equal to the minimum, or until the tableau is found to
3968 * be infeasible. Some pivots may need to be performed for only some
3969 * feasible values of the context tableau. If so, the context tableau
3970 * is split into a part where the pivot is needed and a part where it is not.
3972 * Whenever we enter the main loop, the main tableau is such that no
3973 * "obvious" pivots need to be performed on it, where "obvious" means
3974 * that the given row can be seen to be negative without looking at
3975 * the context tableau. In particular, for non-parametric problems,
3976 * no pivots need to be performed on the main tableau.
3977 * The caller of find_solutions is responsible for making this property
3978 * hold prior to the first iteration of the loop, while restore_lexmin
3979 * is called before every other iteration.
3981 * Inside the main loop, we first examine the signs of the rows of
3982 * the main tableau within the context of the context tableau.
3983 * If we find a row that is always non-positive for all values of
3984 * the parameters satisfying the context tableau and negative for at
3985 * least one value of the parameters, we perform the appropriate pivot
3986 * and start over. An exception is the case where no pivot can be
3987 * performed on the row. In this case, we require that the sign of
3988 * the row is negative for all values of the parameters (rather than just
3989 * non-positive). This special case is handled inside row_sign, which
3990 * will say that the row can have any sign if it determines that it can
3991 * attain both negative and zero values.
3993 * If we can't find a row that always requires a pivot, but we can find
3994 * one or more rows that require a pivot for some values of the parameters
3995 * (i.e., the row can attain both positive and negative signs), then we split
3996 * the context tableau into two parts, one where we force the sign to be
3997 * non-negative and one where we force is to be negative.
3998 * The non-negative part is handled by a recursive call (through find_in_pos).
3999 * Upon returning from this call, we continue with the negative part and
4000 * perform the required pivot.
4002 * If no such rows can be found, all rows are non-negative and we have
4003 * found a (rational) feasible point. If we only wanted a rational point
4005 * Otherwise, we check if all values of the sample point of the tableau
4006 * are integral for the variables. If so, we have found the minimal
4007 * integral point and we are done.
4008 * If the sample point is not integral, then we need to make a distinction
4009 * based on whether the constant term is non-integral or the coefficients
4010 * of the parameters. Furthermore, in order to decide how to handle
4011 * the non-integrality, we also need to know whether the coefficients
4012 * of the other columns in the tableau are integral. This leads
4013 * to the following table. The first two rows do not correspond
4014 * to a non-integral sample point and are only mentioned for completeness.
4016 * constant parameters other
4019 * int int rat | -> no problem
4021 * rat int int -> fail
4023 * rat int rat -> cut
4026 * rat rat rat | -> parametric cut
4029 * rat rat int | -> split context
4031 * If the parametric constant is completely integral, then there is nothing
4032 * to be done. If the constant term is non-integral, but all the other
4033 * coefficient are integral, then there is nothing that can be done
4034 * and the tableau has no integral solution.
4035 * If, on the other hand, one or more of the other columns have rational
4036 * coefficients, but the parameter coefficients are all integral, then
4037 * we can perform a regular (non-parametric) cut.
4038 * Finally, if there is any parameter coefficient that is non-integral,
4039 * then we need to involve the context tableau. There are two cases here.
4040 * If at least one other column has a rational coefficient, then we
4041 * can perform a parametric cut in the main tableau by adding a new
4042 * integer division in the context tableau.
4043 * If all other columns have integral coefficients, then we need to
4044 * enforce that the rational combination of parameters (c + \sum a_i y_i)/m
4045 * is always integral. We do this by introducing an integer division
4046 * q = floor((c + \sum a_i y_i)/m) and stipulating that its argument should
4047 * always be integral in the context tableau, i.e., m q = c + \sum a_i y_i.
4048 * Since q is expressed in the tableau as
4049 * c + \sum a_i y_i - m q >= 0
4050 * -c - \sum a_i y_i + m q + m - 1 >= 0
4051 * it is sufficient to add the inequality
4052 * -c - \sum a_i y_i + m q >= 0
4053 * In the part of the context where this inequality does not hold, the
4054 * main tableau is marked as being empty.
4056 static void find_solutions(struct isl_sol
*sol
, struct isl_tab
*tab
)
4058 struct isl_context
*context
;
4061 if (!tab
|| sol
->error
)
4064 context
= sol
->context
;
4068 if (context
->op
->is_empty(context
))
4071 for (r
= 0; r
>= 0 && tab
&& !tab
->empty
; r
= restore_lexmin(tab
)) {
4074 enum isl_tab_row_sign sgn
;
4078 for (row
= tab
->n_redundant
; row
< tab
->n_row
; ++row
) {
4079 if (!isl_tab_var_from_row(tab
, row
)->is_nonneg
)
4081 sgn
= row_sign(tab
, sol
, row
);
4084 tab
->row_sign
[row
] = sgn
;
4085 if (sgn
== isl_tab_row_any
)
4087 if (sgn
== isl_tab_row_any
&& split
== -1)
4089 if (sgn
== isl_tab_row_neg
)
4092 if (row
< tab
->n_row
)
4095 struct isl_vec
*ineq
;
4097 split
= context
->op
->best_split(context
, tab
);
4100 ineq
= get_row_parameter_ineq(tab
, split
);
4104 reset_any_to_unknown(tab
);
4105 tab
->row_sign
[split
] = isl_tab_row_pos
;
4107 find_in_pos(sol
, tab
, ineq
->el
);
4108 tab
->row_sign
[split
] = isl_tab_row_neg
;
4109 isl_seq_neg(ineq
->el
, ineq
->el
, ineq
->size
);
4110 isl_int_sub_ui(ineq
->el
[0], ineq
->el
[0], 1);
4112 context
->op
->add_ineq(context
, ineq
->el
, 0, 1);
4120 row
= first_non_integer_row(tab
, &flags
);
4123 if (ISL_FL_ISSET(flags
, I_PAR
)) {
4124 if (ISL_FL_ISSET(flags
, I_VAR
)) {
4125 if (isl_tab_mark_empty(tab
) < 0)
4129 row
= add_cut(tab
, row
);
4130 } else if (ISL_FL_ISSET(flags
, I_VAR
)) {
4131 struct isl_vec
*div
;
4132 struct isl_vec
*ineq
;
4134 div
= get_row_split_div(tab
, row
);
4137 d
= context
->op
->get_div(context
, tab
, div
);
4141 ineq
= ineq_for_div(context
->op
->peek_basic_set(context
), d
);
4145 no_sol_in_strict(sol
, tab
, ineq
);
4146 isl_seq_neg(ineq
->el
, ineq
->el
, ineq
->size
);
4147 context
->op
->add_ineq(context
, ineq
->el
, 1, 1);
4149 if (sol
->error
|| !context
->op
->is_ok(context
))
4151 tab
= set_row_cst_to_div(tab
, row
, d
);
4152 if (context
->op
->is_empty(context
))
4155 row
= add_parametric_cut(tab
, row
, context
);
4170 /* Does "sol" contain a pair of partial solutions that could potentially
4173 * We currently only check that "sol" is not in an error state
4174 * and that there are at least two partial solutions of which the final two
4175 * are defined at the same level.
4177 static int sol_has_mergeable_solutions(struct isl_sol
*sol
)
4183 if (!sol
->partial
->next
)
4185 return sol
->partial
->level
== sol
->partial
->next
->level
;
4188 /* Compute the lexicographic minimum of the set represented by the main
4189 * tableau "tab" within the context "sol->context_tab".
4191 * As a preprocessing step, we first transfer all the purely parametric
4192 * equalities from the main tableau to the context tableau, i.e.,
4193 * parameters that have been pivoted to a row.
4194 * These equalities are ignored by the main algorithm, because the
4195 * corresponding rows may not be marked as being non-negative.
4196 * In parts of the context where the added equality does not hold,
4197 * the main tableau is marked as being empty.
4199 * Before we embark on the actual computation, we save a copy
4200 * of the context. When we return, we check if there are any
4201 * partial solutions that can potentially be merged. If so,
4202 * we perform a rollback to the initial state of the context.
4203 * The merging of partial solutions happens inside calls to
4204 * sol_dec_level that are pushed onto the undo stack of the context.
4205 * If there are no partial solutions that can potentially be merged
4206 * then the rollback is skipped as it would just be wasted effort.
4208 static void find_solutions_main(struct isl_sol
*sol
, struct isl_tab
*tab
)
4218 for (row
= tab
->n_redundant
; row
< tab
->n_row
; ++row
) {
4222 if (!row_is_parameter_var(tab
, row
))
4224 if (tab
->row_var
[row
] < tab
->n_param
)
4225 p
= tab
->row_var
[row
];
4227 p
= tab
->row_var
[row
]
4228 + tab
->n_param
- (tab
->n_var
- tab
->n_div
);
4230 eq
= isl_vec_alloc(tab
->mat
->ctx
, 1+tab
->n_param
+tab
->n_div
);
4233 get_row_parameter_line(tab
, row
, eq
->el
);
4234 isl_int_neg(eq
->el
[1 + p
], tab
->mat
->row
[row
][0]);
4235 eq
= isl_vec_normalize(eq
);
4238 no_sol_in_strict(sol
, tab
, eq
);
4240 isl_seq_neg(eq
->el
, eq
->el
, eq
->size
);
4242 no_sol_in_strict(sol
, tab
, eq
);
4243 isl_seq_neg(eq
->el
, eq
->el
, eq
->size
);
4245 sol
->context
->op
->add_eq(sol
->context
, eq
->el
, 1, 1);
4249 if (isl_tab_mark_redundant(tab
, row
) < 0)
4252 if (sol
->context
->op
->is_empty(sol
->context
))
4255 row
= tab
->n_redundant
- 1;
4258 saved
= sol
->context
->op
->save(sol
->context
);
4260 find_solutions(sol
, tab
);
4262 if (sol_has_mergeable_solutions(sol
))
4263 sol
->context
->op
->restore(sol
->context
, saved
);
4265 sol
->context
->op
->discard(saved
);
4276 /* Check if integer division "div" of "dom" also occurs in "bmap".
4277 * If so, return its position within the divs.
4278 * If not, return -1.
4280 static int find_context_div(struct isl_basic_map
*bmap
,
4281 struct isl_basic_set
*dom
, unsigned div
)
4284 unsigned b_dim
= isl_space_dim(bmap
->dim
, isl_dim_all
);
4285 unsigned d_dim
= isl_space_dim(dom
->dim
, isl_dim_all
);
4287 if (isl_int_is_zero(dom
->div
[div
][0]))
4289 if (isl_seq_first_non_zero(dom
->div
[div
] + 2 + d_dim
, dom
->n_div
) != -1)
4292 for (i
= 0; i
< bmap
->n_div
; ++i
) {
4293 if (isl_int_is_zero(bmap
->div
[i
][0]))
4295 if (isl_seq_first_non_zero(bmap
->div
[i
] + 2 + d_dim
,
4296 (b_dim
- d_dim
) + bmap
->n_div
) != -1)
4298 if (isl_seq_eq(bmap
->div
[i
], dom
->div
[div
], 2 + d_dim
))
4304 /* The correspondence between the variables in the main tableau,
4305 * the context tableau, and the input map and domain is as follows.
4306 * The first n_param and the last n_div variables of the main tableau
4307 * form the variables of the context tableau.
4308 * In the basic map, these n_param variables correspond to the
4309 * parameters and the input dimensions. In the domain, they correspond
4310 * to the parameters and the set dimensions.
4311 * The n_div variables correspond to the integer divisions in the domain.
4312 * To ensure that everything lines up, we may need to copy some of the
4313 * integer divisions of the domain to the map. These have to be placed
4314 * in the same order as those in the context and they have to be placed
4315 * after any other integer divisions that the map may have.
4316 * This function performs the required reordering.
4318 static __isl_give isl_basic_map
*align_context_divs(
4319 __isl_take isl_basic_map
*bmap
, __isl_keep isl_basic_set
*dom
)
4325 for (i
= 0; i
< dom
->n_div
; ++i
)
4326 if (find_context_div(bmap
, dom
, i
) != -1)
4328 other
= bmap
->n_div
- common
;
4329 if (dom
->n_div
- common
> 0) {
4330 bmap
= isl_basic_map_extend_space(bmap
, isl_space_copy(bmap
->dim
),
4331 dom
->n_div
- common
, 0, 0);
4335 for (i
= 0; i
< dom
->n_div
; ++i
) {
4336 int pos
= find_context_div(bmap
, dom
, i
);
4338 pos
= isl_basic_map_alloc_div(bmap
);
4341 isl_int_set_si(bmap
->div
[pos
][0], 0);
4343 if (pos
!= other
+ i
)
4344 isl_basic_map_swap_div(bmap
, pos
, other
+ i
);
4348 isl_basic_map_free(bmap
);
4352 /* Base case of isl_tab_basic_map_partial_lexopt, after removing
4353 * some obvious symmetries.
4355 * We make sure the divs in the domain are properly ordered,
4356 * because they will be added one by one in the given order
4357 * during the construction of the solution map.
4358 * Furthermore, make sure that the known integer divisions
4359 * appear before any unknown integer division because the solution
4360 * may depend on the known integer divisions, while anything that
4361 * depends on any variable starting from the first unknown integer
4362 * division is ignored in sol_pma_add.
4364 static struct isl_sol
*basic_map_partial_lexopt_base_sol(
4365 __isl_take isl_basic_map
*bmap
, __isl_take isl_basic_set
*dom
,
4366 __isl_give isl_set
**empty
, int max
,
4367 struct isl_sol
*(*init
)(__isl_keep isl_basic_map
*bmap
,
4368 __isl_take isl_basic_set
*dom
, int track_empty
, int max
))
4370 struct isl_tab
*tab
;
4371 struct isl_sol
*sol
= NULL
;
4372 struct isl_context
*context
;
4375 dom
= isl_basic_set_sort_divs(dom
);
4376 bmap
= align_context_divs(bmap
, dom
);
4378 sol
= init(bmap
, dom
, !!empty
, max
);
4382 context
= sol
->context
;
4383 if (isl_basic_set_plain_is_empty(context
->op
->peek_basic_set(context
)))
4385 else if (isl_basic_map_plain_is_empty(bmap
)) {
4388 isl_basic_set_copy(context
->op
->peek_basic_set(context
)));
4390 tab
= tab_for_lexmin(bmap
,
4391 context
->op
->peek_basic_set(context
), 1, max
);
4392 tab
= context
->op
->detect_nonnegative_parameters(context
, tab
);
4393 find_solutions_main(sol
, tab
);
4398 isl_basic_map_free(bmap
);
4402 isl_basic_map_free(bmap
);
4406 /* Base case of isl_tab_basic_map_partial_lexopt, after removing
4407 * some obvious symmetries.
4409 * We call basic_map_partial_lexopt_base_sol and extract the results.
4411 static __isl_give isl_map
*basic_map_partial_lexopt_base(
4412 __isl_take isl_basic_map
*bmap
, __isl_take isl_basic_set
*dom
,
4413 __isl_give isl_set
**empty
, int max
)
4415 isl_map
*result
= NULL
;
4416 struct isl_sol
*sol
;
4417 struct isl_sol_map
*sol_map
;
4419 sol
= basic_map_partial_lexopt_base_sol(bmap
, dom
, empty
, max
,
4423 sol_map
= (struct isl_sol_map
*) sol
;
4425 result
= isl_map_copy(sol_map
->map
);
4427 *empty
= isl_set_copy(sol_map
->empty
);
4428 sol_free(&sol_map
->sol
);
4432 /* Return a count of the number of occurrences of the "n" first
4433 * variables in the inequality constraints of "bmap".
4435 static __isl_give
int *count_occurrences(__isl_keep isl_basic_map
*bmap
,
4444 ctx
= isl_basic_map_get_ctx(bmap
);
4445 occurrences
= isl_calloc_array(ctx
, int, n
);
4449 for (i
= 0; i
< bmap
->n_ineq
; ++i
) {
4450 for (j
= 0; j
< n
; ++j
) {
4451 if (!isl_int_is_zero(bmap
->ineq
[i
][1 + j
]))
4459 /* Do all of the "n" variables with non-zero coefficients in "c"
4460 * occur in exactly a single constraint.
4461 * "occurrences" is an array of length "n" containing the number
4462 * of occurrences of each of the variables in the inequality constraints.
4464 static int single_occurrence(int n
, isl_int
*c
, int *occurrences
)
4468 for (i
= 0; i
< n
; ++i
) {
4469 if (isl_int_is_zero(c
[i
]))
4471 if (occurrences
[i
] != 1)
4478 /* Do all of the "n" initial variables that occur in inequality constraint
4479 * "ineq" of "bmap" only occur in that constraint?
4481 static int all_single_occurrence(__isl_keep isl_basic_map
*bmap
, int ineq
,
4486 for (i
= 0; i
< n
; ++i
) {
4487 if (isl_int_is_zero(bmap
->ineq
[ineq
][1 + i
]))
4489 for (j
= 0; j
< bmap
->n_ineq
; ++j
) {
4492 if (!isl_int_is_zero(bmap
->ineq
[j
][1 + i
]))
4500 /* Structure used during detection of parallel constraints.
4501 * n_in: number of "input" variables: isl_dim_param + isl_dim_in
4502 * n_out: number of "output" variables: isl_dim_out + isl_dim_div
4503 * val: the coefficients of the output variables
4505 struct isl_constraint_equal_info
{
4511 /* Check whether the coefficients of the output variables
4512 * of the constraint in "entry" are equal to info->val.
4514 static int constraint_equal(const void *entry
, const void *val
)
4516 isl_int
**row
= (isl_int
**)entry
;
4517 const struct isl_constraint_equal_info
*info
= val
;
4519 return isl_seq_eq((*row
) + 1 + info
->n_in
, info
->val
, info
->n_out
);
4522 /* Check whether "bmap" has a pair of constraints that have
4523 * the same coefficients for the output variables.
4524 * Note that the coefficients of the existentially quantified
4525 * variables need to be zero since the existentially quantified
4526 * of the result are usually not the same as those of the input.
4527 * Furthermore, check that each of the input variables that occur
4528 * in those constraints does not occur in any other constraint.
4529 * If so, return true and return the row indices of the two constraints
4530 * in *first and *second.
4532 static isl_bool
parallel_constraints(__isl_keep isl_basic_map
*bmap
,
4533 int *first
, int *second
)
4537 int *occurrences
= NULL
;
4538 struct isl_hash_table
*table
= NULL
;
4539 struct isl_hash_table_entry
*entry
;
4540 struct isl_constraint_equal_info info
;
4544 ctx
= isl_basic_map_get_ctx(bmap
);
4545 table
= isl_hash_table_alloc(ctx
, bmap
->n_ineq
);
4549 info
.n_in
= isl_basic_map_dim(bmap
, isl_dim_param
) +
4550 isl_basic_map_dim(bmap
, isl_dim_in
);
4551 occurrences
= count_occurrences(bmap
, info
.n_in
);
4552 if (info
.n_in
&& !occurrences
)
4554 n_out
= isl_basic_map_dim(bmap
, isl_dim_out
);
4555 n_div
= isl_basic_map_dim(bmap
, isl_dim_div
);
4556 info
.n_out
= n_out
+ n_div
;
4557 for (i
= 0; i
< bmap
->n_ineq
; ++i
) {
4560 info
.val
= bmap
->ineq
[i
] + 1 + info
.n_in
;
4561 if (isl_seq_first_non_zero(info
.val
, n_out
) < 0)
4563 if (isl_seq_first_non_zero(info
.val
+ n_out
, n_div
) >= 0)
4565 if (!single_occurrence(info
.n_in
, bmap
->ineq
[i
] + 1,
4568 hash
= isl_seq_get_hash(info
.val
, info
.n_out
);
4569 entry
= isl_hash_table_find(ctx
, table
, hash
,
4570 constraint_equal
, &info
, 1);
4575 entry
->data
= &bmap
->ineq
[i
];
4578 if (i
< bmap
->n_ineq
) {
4579 *first
= ((isl_int
**)entry
->data
) - bmap
->ineq
;
4583 isl_hash_table_free(ctx
, table
);
4586 return i
< bmap
->n_ineq
;
4588 isl_hash_table_free(ctx
, table
);
4590 return isl_bool_error
;
4593 /* Given a set of upper bounds in "var", add constraints to "bset"
4594 * that make the i-th bound smallest.
4596 * In particular, if there are n bounds b_i, then add the constraints
4598 * b_i <= b_j for j > i
4599 * b_i < b_j for j < i
4601 static __isl_give isl_basic_set
*select_minimum(__isl_take isl_basic_set
*bset
,
4602 __isl_keep isl_mat
*var
, int i
)
4607 ctx
= isl_mat_get_ctx(var
);
4609 for (j
= 0; j
< var
->n_row
; ++j
) {
4612 k
= isl_basic_set_alloc_inequality(bset
);
4615 isl_seq_combine(bset
->ineq
[k
], ctx
->one
, var
->row
[j
],
4616 ctx
->negone
, var
->row
[i
], var
->n_col
);
4617 isl_int_set_si(bset
->ineq
[k
][var
->n_col
], 0);
4619 isl_int_sub_ui(bset
->ineq
[k
][0], bset
->ineq
[k
][0], 1);
4622 bset
= isl_basic_set_finalize(bset
);
4626 isl_basic_set_free(bset
);
4630 /* Given a set of upper bounds on the last "input" variable m,
4631 * construct a set that assigns the minimal upper bound to m, i.e.,
4632 * construct a set that divides the space into cells where one
4633 * of the upper bounds is smaller than all the others and assign
4634 * this upper bound to m.
4636 * In particular, if there are n bounds b_i, then the result
4637 * consists of n basic sets, each one of the form
4640 * b_i <= b_j for j > i
4641 * b_i < b_j for j < i
4643 static __isl_give isl_set
*set_minimum(__isl_take isl_space
*dim
,
4644 __isl_take isl_mat
*var
)
4647 isl_basic_set
*bset
= NULL
;
4648 isl_set
*set
= NULL
;
4653 set
= isl_set_alloc_space(isl_space_copy(dim
),
4654 var
->n_row
, ISL_SET_DISJOINT
);
4656 for (i
= 0; i
< var
->n_row
; ++i
) {
4657 bset
= isl_basic_set_alloc_space(isl_space_copy(dim
), 0,
4659 k
= isl_basic_set_alloc_equality(bset
);
4662 isl_seq_cpy(bset
->eq
[k
], var
->row
[i
], var
->n_col
);
4663 isl_int_set_si(bset
->eq
[k
][var
->n_col
], -1);
4664 bset
= select_minimum(bset
, var
, i
);
4665 set
= isl_set_add_basic_set(set
, bset
);
4668 isl_space_free(dim
);
4672 isl_basic_set_free(bset
);
4674 isl_space_free(dim
);
4679 /* Given that the last input variable of "bmap" represents the minimum
4680 * of the bounds in "cst", check whether we need to split the domain
4681 * based on which bound attains the minimum.
4683 * A split is needed when the minimum appears in an integer division
4684 * or in an equality. Otherwise, it is only needed if it appears in
4685 * an upper bound that is different from the upper bounds on which it
4688 static isl_bool
need_split_basic_map(__isl_keep isl_basic_map
*bmap
,
4689 __isl_keep isl_mat
*cst
)
4695 pos
= cst
->n_col
- 1;
4696 total
= isl_basic_map_dim(bmap
, isl_dim_all
);
4698 for (i
= 0; i
< bmap
->n_div
; ++i
)
4699 if (!isl_int_is_zero(bmap
->div
[i
][2 + pos
]))
4700 return isl_bool_true
;
4702 for (i
= 0; i
< bmap
->n_eq
; ++i
)
4703 if (!isl_int_is_zero(bmap
->eq
[i
][1 + pos
]))
4704 return isl_bool_true
;
4706 for (i
= 0; i
< bmap
->n_ineq
; ++i
) {
4707 if (isl_int_is_nonneg(bmap
->ineq
[i
][1 + pos
]))
4709 if (!isl_int_is_negone(bmap
->ineq
[i
][1 + pos
]))
4710 return isl_bool_true
;
4711 if (isl_seq_first_non_zero(bmap
->ineq
[i
] + 1 + pos
+ 1,
4712 total
- pos
- 1) >= 0)
4713 return isl_bool_true
;
4715 for (j
= 0; j
< cst
->n_row
; ++j
)
4716 if (isl_seq_eq(bmap
->ineq
[i
], cst
->row
[j
], cst
->n_col
))
4718 if (j
>= cst
->n_row
)
4719 return isl_bool_true
;
4722 return isl_bool_false
;
4725 /* Given that the last set variable of "bset" represents the minimum
4726 * of the bounds in "cst", check whether we need to split the domain
4727 * based on which bound attains the minimum.
4729 * We simply call need_split_basic_map here. This is safe because
4730 * the position of the minimum is computed from "cst" and not
4733 static isl_bool
need_split_basic_set(__isl_keep isl_basic_set
*bset
,
4734 __isl_keep isl_mat
*cst
)
4736 return need_split_basic_map(bset_to_bmap(bset
), cst
);
4739 /* Given that the last set variable of "set" represents the minimum
4740 * of the bounds in "cst", check whether we need to split the domain
4741 * based on which bound attains the minimum.
4743 static isl_bool
need_split_set(__isl_keep isl_set
*set
, __isl_keep isl_mat
*cst
)
4747 for (i
= 0; i
< set
->n
; ++i
) {
4750 split
= need_split_basic_set(set
->p
[i
], cst
);
4751 if (split
< 0 || split
)
4755 return isl_bool_false
;
4758 /* Given a set of which the last set variable is the minimum
4759 * of the bounds in "cst", split each basic set in the set
4760 * in pieces where one of the bounds is (strictly) smaller than the others.
4761 * This subdivision is given in "min_expr".
4762 * The variable is subsequently projected out.
4764 * We only do the split when it is needed.
4765 * For example if the last input variable m = min(a,b) and the only
4766 * constraints in the given basic set are lower bounds on m,
4767 * i.e., l <= m = min(a,b), then we can simply project out m
4768 * to obtain l <= a and l <= b, without having to split on whether
4769 * m is equal to a or b.
4771 static __isl_give isl_set
*split(__isl_take isl_set
*empty
,
4772 __isl_take isl_set
*min_expr
, __isl_take isl_mat
*cst
)
4779 if (!empty
|| !min_expr
|| !cst
)
4782 n_in
= isl_set_dim(empty
, isl_dim_set
);
4783 dim
= isl_set_get_space(empty
);
4784 dim
= isl_space_drop_dims(dim
, isl_dim_set
, n_in
- 1, 1);
4785 res
= isl_set_empty(dim
);
4787 for (i
= 0; i
< empty
->n
; ++i
) {
4791 set
= isl_set_from_basic_set(isl_basic_set_copy(empty
->p
[i
]));
4792 split
= need_split_basic_set(empty
->p
[i
], cst
);
4794 set
= isl_set_free(set
);
4796 set
= isl_set_intersect(set
, isl_set_copy(min_expr
));
4797 set
= isl_set_remove_dims(set
, isl_dim_set
, n_in
- 1, 1);
4799 res
= isl_set_union_disjoint(res
, set
);
4802 isl_set_free(empty
);
4803 isl_set_free(min_expr
);
4807 isl_set_free(empty
);
4808 isl_set_free(min_expr
);
4813 /* Given a map of which the last input variable is the minimum
4814 * of the bounds in "cst", split each basic set in the set
4815 * in pieces where one of the bounds is (strictly) smaller than the others.
4816 * This subdivision is given in "min_expr".
4817 * The variable is subsequently projected out.
4819 * The implementation is essentially the same as that of "split".
4821 static __isl_give isl_map
*split_domain(__isl_take isl_map
*opt
,
4822 __isl_take isl_set
*min_expr
, __isl_take isl_mat
*cst
)
4829 if (!opt
|| !min_expr
|| !cst
)
4832 n_in
= isl_map_dim(opt
, isl_dim_in
);
4833 dim
= isl_map_get_space(opt
);
4834 dim
= isl_space_drop_dims(dim
, isl_dim_in
, n_in
- 1, 1);
4835 res
= isl_map_empty(dim
);
4837 for (i
= 0; i
< opt
->n
; ++i
) {
4841 map
= isl_map_from_basic_map(isl_basic_map_copy(opt
->p
[i
]));
4842 split
= need_split_basic_map(opt
->p
[i
], cst
);
4844 map
= isl_map_free(map
);
4846 map
= isl_map_intersect_domain(map
,
4847 isl_set_copy(min_expr
));
4848 map
= isl_map_remove_dims(map
, isl_dim_in
, n_in
- 1, 1);
4850 res
= isl_map_union_disjoint(res
, map
);
4854 isl_set_free(min_expr
);
4859 isl_set_free(min_expr
);
4864 static __isl_give isl_map
*basic_map_partial_lexopt(
4865 __isl_take isl_basic_map
*bmap
, __isl_take isl_basic_set
*dom
,
4866 __isl_give isl_set
**empty
, int max
);
4868 /* This function is called from basic_map_partial_lexopt_symm.
4869 * The last variable of "bmap" and "dom" corresponds to the minimum
4870 * of the bounds in "cst". "map_space" is the space of the original
4871 * input relation (of basic_map_partial_lexopt_symm) and "set_space"
4872 * is the space of the original domain.
4874 * We recursively call basic_map_partial_lexopt and then plug in
4875 * the definition of the minimum in the result.
4877 static __isl_give isl_map
*basic_map_partial_lexopt_symm_core(
4878 __isl_take isl_basic_map
*bmap
, __isl_take isl_basic_set
*dom
,
4879 __isl_give isl_set
**empty
, int max
, __isl_take isl_mat
*cst
,
4880 __isl_take isl_space
*map_space
, __isl_take isl_space
*set_space
)
4885 min_expr
= set_minimum(isl_basic_set_get_space(dom
), isl_mat_copy(cst
));
4887 opt
= basic_map_partial_lexopt(bmap
, dom
, empty
, max
);
4890 *empty
= split(*empty
,
4891 isl_set_copy(min_expr
), isl_mat_copy(cst
));
4892 *empty
= isl_set_reset_space(*empty
, set_space
);
4895 opt
= split_domain(opt
, min_expr
, cst
);
4896 opt
= isl_map_reset_space(opt
, map_space
);
4901 /* Extract a domain from "bmap" for the purpose of computing
4902 * a lexicographic optimum.
4904 * This function is only called when the caller wants to compute a full
4905 * lexicographic optimum, i.e., without specifying a domain. In this case,
4906 * the caller is not interested in the part of the domain space where
4907 * there is no solution and the domain can be initialized to those constraints
4908 * of "bmap" that only involve the parameters and the input dimensions.
4909 * This relieves the parametric programming engine from detecting those
4910 * inequalities and transferring them to the context. More importantly,
4911 * it ensures that those inequalities are transferred first and not
4912 * intermixed with inequalities that actually split the domain.
4914 * If the caller does not require the absence of existentially quantified
4915 * variables in the result (i.e., if ISL_OPT_QE is not set in "flags"),
4916 * then the actual domain of "bmap" can be used. This ensures that
4917 * the domain does not need to be split at all just to separate out
4918 * pieces of the domain that do not have a solution from piece that do.
4919 * This domain cannot be used in general because it may involve
4920 * (unknown) existentially quantified variables which will then also
4921 * appear in the solution.
4923 static __isl_give isl_basic_set
*extract_domain(__isl_keep isl_basic_map
*bmap
,
4929 n_div
= isl_basic_map_dim(bmap
, isl_dim_div
);
4930 n_out
= isl_basic_map_dim(bmap
, isl_dim_out
);
4931 bmap
= isl_basic_map_copy(bmap
);
4932 if (ISL_FL_ISSET(flags
, ISL_OPT_QE
)) {
4933 bmap
= isl_basic_map_drop_constraints_involving_dims(bmap
,
4934 isl_dim_div
, 0, n_div
);
4935 bmap
= isl_basic_map_drop_constraints_involving_dims(bmap
,
4936 isl_dim_out
, 0, n_out
);
4938 return isl_basic_map_domain(bmap
);
4942 #define TYPE isl_map
4945 #include "isl_tab_lexopt_templ.c"
4947 struct isl_sol_for
{
4949 isl_stat (*fn
)(__isl_take isl_basic_set
*dom
,
4950 __isl_take isl_aff_list
*list
, void *user
);
4954 static void sol_for_free(struct isl_sol
*sol
)
4958 /* Add the solution identified by the tableau and the context tableau.
4959 * In particular, "dom" represents the context and "ma" expresses
4960 * the solution on that context.
4962 * See documentation of sol_add for more details.
4964 * Instead of constructing a basic map, this function calls a user
4965 * defined function with the current context as a basic set and
4966 * a list of affine expressions representing the relation between
4967 * the input and output. The space over which the affine expressions
4968 * are defined is the same as that of the domain. The number of
4969 * affine expressions in the list is equal to the number of output variables.
4971 static void sol_for_add(struct isl_sol_for
*sol
,
4972 __isl_take isl_basic_set
*dom
, __isl_take isl_multi_aff
*ma
)
4979 if (sol
->sol
.error
|| !dom
|| !ma
)
4982 ctx
= isl_basic_set_get_ctx(dom
);
4983 n
= isl_multi_aff_dim(ma
, isl_dim_out
);
4984 list
= isl_aff_list_alloc(ctx
, n
);
4985 for (i
= 0; i
< n
; ++i
) {
4986 aff
= isl_multi_aff_get_aff(ma
, i
);
4987 list
= isl_aff_list_add(list
, aff
);
4990 dom
= isl_basic_set_finalize(dom
);
4992 if (sol
->fn(isl_basic_set_copy(dom
), list
, sol
->user
) < 0)
4995 isl_basic_set_free(dom
);
4996 isl_multi_aff_free(ma
);
4999 isl_basic_set_free(dom
);
5000 isl_multi_aff_free(ma
);
5004 static void sol_for_add_wrap(struct isl_sol
*sol
,
5005 __isl_take isl_basic_set
*dom
, __isl_take isl_multi_aff
*ma
)
5007 sol_for_add((struct isl_sol_for
*)sol
, dom
, ma
);
5010 static struct isl_sol_for
*sol_for_init(__isl_keep isl_basic_map
*bmap
, int max
,
5011 isl_stat (*fn
)(__isl_take isl_basic_set
*dom
,
5012 __isl_take isl_aff_list
*list
, void *user
),
5015 struct isl_sol_for
*sol_for
= NULL
;
5017 struct isl_basic_set
*dom
= NULL
;
5019 sol_for
= isl_calloc_type(bmap
->ctx
, struct isl_sol_for
);
5023 dom_dim
= isl_space_domain(isl_space_copy(bmap
->dim
));
5024 dom
= isl_basic_set_universe(dom_dim
);
5026 sol_for
->sol
.free
= &sol_for_free
;
5027 if (sol_init(&sol_for
->sol
, bmap
, dom
, max
) < 0)
5030 sol_for
->user
= user
;
5031 sol_for
->sol
.add
= &sol_for_add_wrap
;
5032 sol_for
->sol
.add_empty
= NULL
;
5034 isl_basic_set_free(dom
);
5037 isl_basic_set_free(dom
);
5038 sol_free(&sol_for
->sol
);
5042 static void sol_for_find_solutions(struct isl_sol_for
*sol_for
,
5043 struct isl_tab
*tab
)
5045 find_solutions_main(&sol_for
->sol
, tab
);
5048 isl_stat
isl_basic_map_foreach_lexopt(__isl_keep isl_basic_map
*bmap
, int max
,
5049 isl_stat (*fn
)(__isl_take isl_basic_set
*dom
,
5050 __isl_take isl_aff_list
*list
, void *user
),
5053 struct isl_sol_for
*sol_for
= NULL
;
5055 bmap
= isl_basic_map_copy(bmap
);
5056 bmap
= isl_basic_map_detect_equalities(bmap
);
5058 return isl_stat_error
;
5060 sol_for
= sol_for_init(bmap
, max
, fn
, user
);
5064 if (isl_basic_map_plain_is_empty(bmap
))
5067 struct isl_tab
*tab
;
5068 struct isl_context
*context
= sol_for
->sol
.context
;
5069 tab
= tab_for_lexmin(bmap
,
5070 context
->op
->peek_basic_set(context
), 1, max
);
5071 tab
= context
->op
->detect_nonnegative_parameters(context
, tab
);
5072 sol_for_find_solutions(sol_for
, tab
);
5073 if (sol_for
->sol
.error
)
5077 sol_free(&sol_for
->sol
);
5078 isl_basic_map_free(bmap
);
5081 sol_free(&sol_for
->sol
);
5082 isl_basic_map_free(bmap
);
5083 return isl_stat_error
;
5086 /* Extract the subsequence of the sample value of "tab"
5087 * starting at "pos" and of length "len".
5089 static __isl_give isl_vec
*extract_sample_sequence(struct isl_tab
*tab
,
5096 ctx
= isl_tab_get_ctx(tab
);
5097 v
= isl_vec_alloc(ctx
, len
);
5100 for (i
= 0; i
< len
; ++i
) {
5101 if (!tab
->var
[pos
+ i
].is_row
) {
5102 isl_int_set_si(v
->el
[i
], 0);
5106 row
= tab
->var
[pos
+ i
].index
;
5107 isl_int_divexact(v
->el
[i
], tab
->mat
->row
[row
][1],
5108 tab
->mat
->row
[row
][0]);
5115 /* Check if the sequence of variables starting at "pos"
5116 * represents a trivial solution according to "trivial".
5117 * That is, is the result of applying "trivial" to this sequence
5118 * equal to the zero vector?
5120 static isl_bool
region_is_trivial(struct isl_tab
*tab
, int pos
,
5121 __isl_keep isl_mat
*trivial
)
5125 isl_bool is_trivial
;
5128 return isl_bool_error
;
5130 n
= isl_mat_rows(trivial
);
5132 return isl_bool_false
;
5134 len
= isl_mat_cols(trivial
);
5135 v
= extract_sample_sequence(tab
, pos
, len
);
5136 v
= isl_mat_vec_product(isl_mat_copy(trivial
), v
);
5137 is_trivial
= isl_vec_is_zero(v
);
5143 /* Global internal data for isl_tab_basic_set_non_trivial_lexmin.
5145 * "n_op" is the number of initial coordinates to optimize,
5146 * as passed to isl_tab_basic_set_non_trivial_lexmin.
5147 * "region" is the "n_region"-sized array of regions passed
5148 * to isl_tab_basic_set_non_trivial_lexmin.
5150 * "tab" is the tableau that corresponds to the ILP problem.
5151 * "local" is an array of local data structure, one for each
5152 * (potential) level of the backtracking procedure of
5153 * isl_tab_basic_set_non_trivial_lexmin.
5154 * "v" is a pre-allocated vector that can be used for adding
5155 * constraints to the tableau.
5157 * "sol" contains the best solution found so far.
5158 * It is initialized to a vector of size zero.
5160 struct isl_lexmin_data
{
5163 struct isl_trivial_region
*region
;
5165 struct isl_tab
*tab
;
5166 struct isl_local_region
*local
;
5172 /* Return the index of the first trivial region, "n_region" if all regions
5173 * are non-trivial or -1 in case of error.
5175 static int first_trivial_region(struct isl_lexmin_data
*data
)
5179 for (i
= 0; i
< data
->n_region
; ++i
) {
5181 trivial
= region_is_trivial(data
->tab
, data
->region
[i
].pos
,
5182 data
->region
[i
].trivial
);
5189 return data
->n_region
;
5192 /* Check if the solution is optimal, i.e., whether the first
5193 * n_op entries are zero.
5195 static int is_optimal(__isl_keep isl_vec
*sol
, int n_op
)
5199 for (i
= 0; i
< n_op
; ++i
)
5200 if (!isl_int_is_zero(sol
->el
[1 + i
]))
5205 /* Add constraints to "tab" that ensure that any solution is significantly
5206 * better than that represented by "sol". That is, find the first
5207 * relevant (within first n_op) non-zero coefficient and force it (along
5208 * with all previous coefficients) to be zero.
5209 * If the solution is already optimal (all relevant coefficients are zero),
5210 * then just mark the table as empty.
5211 * "n_zero" is the number of coefficients that have been forced zero
5212 * by previous calls to this function at the same level.
5213 * Return the updated number of forced zero coefficients or -1 on error.
5215 * This function assumes that at least 2 * (n_op - n_zero) more rows and
5216 * at least 2 * (n_op - n_zero) more elements in the constraint array
5217 * are available in the tableau.
5219 static int force_better_solution(struct isl_tab
*tab
,
5220 __isl_keep isl_vec
*sol
, int n_op
, int n_zero
)
5229 for (i
= n_zero
; i
< n_op
; ++i
)
5230 if (!isl_int_is_zero(sol
->el
[1 + i
]))
5234 if (isl_tab_mark_empty(tab
) < 0)
5239 ctx
= isl_vec_get_ctx(sol
);
5240 v
= isl_vec_alloc(ctx
, 1 + tab
->n_var
);
5245 for (; i
>= n_zero
; --i
) {
5247 isl_int_set_si(v
->el
[1 + i
], -1);
5248 if (add_lexmin_eq(tab
, v
->el
) < 0)
5259 /* Fix triviality direction "dir" of the given region to zero.
5261 * This function assumes that at least two more rows and at least
5262 * two more elements in the constraint array are available in the tableau.
5264 static isl_stat
fix_zero(struct isl_tab
*tab
, struct isl_trivial_region
*region
,
5265 int dir
, struct isl_lexmin_data
*data
)
5269 data
->v
= isl_vec_clr(data
->v
);
5271 return isl_stat_error
;
5272 len
= isl_mat_cols(region
->trivial
);
5273 isl_seq_cpy(data
->v
->el
+ 1 + region
->pos
, region
->trivial
->row
[dir
],
5275 if (add_lexmin_eq(tab
, data
->v
->el
) < 0)
5276 return isl_stat_error
;
5281 /* This function selects case "side" for non-triviality region "region",
5282 * assuming all the equality constraints have been imposed already.
5283 * In particular, the triviality direction side/2 is made positive
5284 * if side is even and made negative if side is odd.
5286 * This function assumes that at least one more row and at least
5287 * one more element in the constraint array are available in the tableau.
5289 static struct isl_tab
*pos_neg(struct isl_tab
*tab
,
5290 struct isl_trivial_region
*region
,
5291 int side
, struct isl_lexmin_data
*data
)
5295 data
->v
= isl_vec_clr(data
->v
);
5298 isl_int_set_si(data
->v
->el
[0], -1);
5299 len
= isl_mat_cols(region
->trivial
);
5301 isl_seq_cpy(data
->v
->el
+ 1 + region
->pos
,
5302 region
->trivial
->row
[side
/ 2], len
);
5304 isl_seq_neg(data
->v
->el
+ 1 + region
->pos
,
5305 region
->trivial
->row
[side
/ 2], len
);
5306 return add_lexmin_ineq(tab
, data
->v
->el
);
5312 /* Local data at each level of the backtracking procedure of
5313 * isl_tab_basic_set_non_trivial_lexmin.
5315 * "update" is set if a solution has been found in the current case
5316 * of this level, such that a better solution needs to be enforced
5318 * "n_zero" is the number of initial coordinates that have already
5319 * been forced to be zero at this level.
5320 * "region" is the non-triviality region considered at this level.
5321 * "side" is the index of the current case at this level.
5322 * "n" is the number of triviality directions.
5323 * "snap" is a snapshot of the tableau holding a state that needs
5324 * to be satisfied by all subsequent cases.
5326 struct isl_local_region
{
5332 struct isl_tab_undo
*snap
;
5335 /* Initialize the global data structure "data" used while solving
5336 * the ILP problem "bset".
5338 static isl_stat
init_lexmin_data(struct isl_lexmin_data
*data
,
5339 __isl_keep isl_basic_set
*bset
)
5343 ctx
= isl_basic_set_get_ctx(bset
);
5345 data
->tab
= tab_for_lexmin(bset
, NULL
, 0, 0);
5347 return isl_stat_error
;
5349 data
->v
= isl_vec_alloc(ctx
, 1 + data
->tab
->n_var
);
5351 return isl_stat_error
;
5352 data
->local
= isl_calloc_array(ctx
, struct isl_local_region
,
5354 if (data
->n_region
&& !data
->local
)
5355 return isl_stat_error
;
5357 data
->sol
= isl_vec_alloc(ctx
, 0);
5362 /* Mark all outer levels as requiring a better solution
5363 * in the next cases.
5365 static void update_outer_levels(struct isl_lexmin_data
*data
, int level
)
5369 for (i
= 0; i
< level
; ++i
)
5370 data
->local
[i
].update
= 1;
5373 /* Initialize "local" to refer to region "region" and
5374 * to initiate processing at this level.
5376 static void init_local_region(struct isl_local_region
*local
, int region
,
5377 struct isl_lexmin_data
*data
)
5379 local
->n
= isl_mat_rows(data
->region
[region
].trivial
);
5380 local
->region
= region
;
5386 /* What to do next after entering a level of the backtracking procedure.
5388 * error: some error has occurred; abort
5389 * done: an optimal solution has been found; stop search
5390 * backtrack: backtrack to the previous level
5391 * handle: add the constraints for the current level and
5392 * move to the next level
5395 isl_next_error
= -1,
5401 /* Have all cases of the current region been considered?
5402 * If there are n directions, then there are 2n cases.
5404 * The constraints in the current tableau are imposed
5405 * in all subsequent cases. This means that if the current
5406 * tableau is empty, then none of those cases should be considered
5407 * anymore and all cases have effectively been considered.
5409 static int finished_all_cases(struct isl_local_region
*local
,
5410 struct isl_lexmin_data
*data
)
5412 if (data
->tab
->empty
)
5414 return local
->side
>= 2 * local
->n
;
5417 /* Enter level "level" of the backtracking search and figure out
5418 * what to do next. "init" is set if the level was entered
5419 * from a higher level and needs to be initialized.
5420 * Otherwise, the level is entered as a result of backtracking and
5421 * the tableau needs to be restored to a position that can
5422 * be used for the next case at this level.
5423 * The snapshot is assumed to have been saved in the previous case,
5424 * before the constraints specific to that case were added.
5426 * In the initialization case, the local region is initialized
5427 * to point to the first violated region.
5428 * If the constraints of all regions are satisfied by the current
5429 * sample of the tableau, then tell the caller to continue looking
5430 * for a better solution or to stop searching if an optimal solution
5433 * If the tableau is empty or if all cases at the current level
5434 * have been considered, then the caller needs to backtrack as well.
5436 static enum isl_next
enter_level(int level
, int init
,
5437 struct isl_lexmin_data
*data
)
5439 struct isl_local_region
*local
= &data
->local
[level
];
5444 data
->tab
= cut_to_integer_lexmin(data
->tab
, CUT_ONE
);
5446 return isl_next_error
;
5447 if (data
->tab
->empty
)
5448 return isl_next_backtrack
;
5449 r
= first_trivial_region(data
);
5451 return isl_next_error
;
5452 if (r
== data
->n_region
) {
5453 update_outer_levels(data
, level
);
5454 isl_vec_free(data
->sol
);
5455 data
->sol
= isl_tab_get_sample_value(data
->tab
);
5457 return isl_next_error
;
5458 if (is_optimal(data
->sol
, data
->n_op
))
5459 return isl_next_done
;
5460 return isl_next_backtrack
;
5462 if (level
>= data
->n_region
)
5463 isl_die(isl_vec_get_ctx(data
->v
), isl_error_internal
,
5464 "nesting level too deep",
5465 return isl_next_error
);
5466 init_local_region(local
, r
, data
);
5467 if (isl_tab_extend_cons(data
->tab
,
5468 2 * local
->n
+ 2 * data
->n_op
) < 0)
5469 return isl_next_error
;
5471 if (isl_tab_rollback(data
->tab
, local
->snap
) < 0)
5472 return isl_next_error
;
5475 if (finished_all_cases(local
, data
))
5476 return isl_next_backtrack
;
5477 return isl_next_handle
;
5480 /* If a solution has been found in the previous case at this level
5481 * (marked by local->update being set), then add constraints
5482 * that enforce a better solution in the present and all following cases.
5483 * The constraints only need to be imposed once because they are
5484 * included in the snapshot (taken in pick_side) that will be used in
5487 static isl_stat
better_next_side(struct isl_local_region
*local
,
5488 struct isl_lexmin_data
*data
)
5493 local
->n_zero
= force_better_solution(data
->tab
,
5494 data
->sol
, data
->n_op
, local
->n_zero
);
5495 if (local
->n_zero
< 0)
5496 return isl_stat_error
;
5503 /* Add constraints to data->tab that select the current case (local->side)
5504 * at the current level.
5506 * If the linear combinations v should not be zero, then the cases are
5509 * v_0 = 0 and v_1 >= 1
5510 * v_0 = 0 and v_1 <= -1
5511 * v_0 = 0 and v_1 = 0 and v_2 >= 1
5512 * v_0 = 0 and v_1 = 0 and v_2 <= -1
5516 * A snapshot is taken after the equality constraint (if any) has been added
5517 * such that the next case can start off from this position.
5518 * The rollback to this position is performed in enter_level.
5520 static isl_stat
pick_side(struct isl_local_region
*local
,
5521 struct isl_lexmin_data
*data
)
5523 struct isl_trivial_region
*region
;
5526 region
= &data
->region
[local
->region
];
5528 base
= 2 * (side
/2);
5530 if (side
== base
&& base
>= 2 &&
5531 fix_zero(data
->tab
, region
, base
/ 2 - 1, data
) < 0)
5532 return isl_stat_error
;
5534 local
->snap
= isl_tab_snap(data
->tab
);
5535 if (isl_tab_push_basis(data
->tab
) < 0)
5536 return isl_stat_error
;
5538 data
->tab
= pos_neg(data
->tab
, region
, side
, data
);
5540 return isl_stat_error
;
5544 /* Free the memory associated to "data".
5546 static void clear_lexmin_data(struct isl_lexmin_data
*data
)
5549 isl_vec_free(data
->v
);
5550 isl_tab_free(data
->tab
);
5553 /* Return the lexicographically smallest non-trivial solution of the
5554 * given ILP problem.
5556 * All variables are assumed to be non-negative.
5558 * n_op is the number of initial coordinates to optimize.
5559 * That is, once a solution has been found, we will only continue looking
5560 * for solutions that result in significantly better values for those
5561 * initial coordinates. That is, we only continue looking for solutions
5562 * that increase the number of initial zeros in this sequence.
5564 * A solution is non-trivial, if it is non-trivial on each of the
5565 * specified regions. Each region represents a sequence of
5566 * triviality directions on a sequence of variables that starts
5567 * at a given position. A solution is non-trivial on such a region if
5568 * at least one of the triviality directions is non-zero
5569 * on that sequence of variables.
5571 * Whenever a conflict is encountered, all constraints involved are
5572 * reported to the caller through a call to "conflict".
5574 * We perform a simple branch-and-bound backtracking search.
5575 * Each level in the search represents an initially trivial region
5576 * that is forced to be non-trivial.
5577 * At each level we consider 2 * n cases, where n
5578 * is the number of triviality directions.
5579 * In terms of those n directions v_i, we consider the cases
5582 * v_0 = 0 and v_1 >= 1
5583 * v_0 = 0 and v_1 <= -1
5584 * v_0 = 0 and v_1 = 0 and v_2 >= 1
5585 * v_0 = 0 and v_1 = 0 and v_2 <= -1
5589 __isl_give isl_vec
*isl_tab_basic_set_non_trivial_lexmin(
5590 __isl_take isl_basic_set
*bset
, int n_op
, int n_region
,
5591 struct isl_trivial_region
*region
,
5592 int (*conflict
)(int con
, void *user
), void *user
)
5594 struct isl_lexmin_data data
= { n_op
, n_region
, region
};
5600 if (init_lexmin_data(&data
, bset
) < 0)
5602 data
.tab
->conflict
= conflict
;
5603 data
.tab
->conflict_user
= user
;
5608 while (level
>= 0) {
5610 struct isl_local_region
*local
= &data
.local
[level
];
5612 next
= enter_level(level
, init
, &data
);
5615 if (next
== isl_next_done
)
5617 if (next
== isl_next_backtrack
) {
5623 if (better_next_side(local
, &data
) < 0)
5625 if (pick_side(local
, &data
) < 0)
5633 clear_lexmin_data(&data
);
5634 isl_basic_set_free(bset
);
5638 clear_lexmin_data(&data
);
5639 isl_basic_set_free(bset
);
5640 isl_vec_free(data
.sol
);
5644 /* Wrapper for a tableau that is used for computing
5645 * the lexicographically smallest rational point of a non-negative set.
5646 * This point is represented by the sample value of "tab",
5647 * unless "tab" is empty.
5649 struct isl_tab_lexmin
{
5651 struct isl_tab
*tab
;
5654 /* Free "tl" and return NULL.
5656 __isl_null isl_tab_lexmin
*isl_tab_lexmin_free(__isl_take isl_tab_lexmin
*tl
)
5660 isl_ctx_deref(tl
->ctx
);
5661 isl_tab_free(tl
->tab
);
5667 /* Construct an isl_tab_lexmin for computing
5668 * the lexicographically smallest rational point in "bset",
5669 * assuming that all variables are non-negative.
5671 __isl_give isl_tab_lexmin
*isl_tab_lexmin_from_basic_set(
5672 __isl_take isl_basic_set
*bset
)
5680 ctx
= isl_basic_set_get_ctx(bset
);
5681 tl
= isl_calloc_type(ctx
, struct isl_tab_lexmin
);
5686 tl
->tab
= tab_for_lexmin(bset
, NULL
, 0, 0);
5687 isl_basic_set_free(bset
);
5689 return isl_tab_lexmin_free(tl
);
5692 isl_basic_set_free(bset
);
5693 isl_tab_lexmin_free(tl
);
5697 /* Return the dimension of the set represented by "tl".
5699 int isl_tab_lexmin_dim(__isl_keep isl_tab_lexmin
*tl
)
5701 return tl
? tl
->tab
->n_var
: -1;
5704 /* Add the equality with coefficients "eq" to "tl", updating the optimal
5705 * solution if needed.
5706 * The equality is added as two opposite inequality constraints.
5708 __isl_give isl_tab_lexmin
*isl_tab_lexmin_add_eq(__isl_take isl_tab_lexmin
*tl
,
5714 return isl_tab_lexmin_free(tl
);
5716 if (isl_tab_extend_cons(tl
->tab
, 2) < 0)
5717 return isl_tab_lexmin_free(tl
);
5718 n_var
= tl
->tab
->n_var
;
5719 isl_seq_neg(eq
, eq
, 1 + n_var
);
5720 tl
->tab
= add_lexmin_ineq(tl
->tab
, eq
);
5721 isl_seq_neg(eq
, eq
, 1 + n_var
);
5722 tl
->tab
= add_lexmin_ineq(tl
->tab
, eq
);
5725 return isl_tab_lexmin_free(tl
);
5730 /* Add cuts to "tl" until the sample value reaches an integer value or
5731 * until the result becomes empty.
5733 __isl_give isl_tab_lexmin
*isl_tab_lexmin_cut_to_integer(
5734 __isl_take isl_tab_lexmin
*tl
)
5738 tl
->tab
= cut_to_integer_lexmin(tl
->tab
, CUT_ONE
);
5740 return isl_tab_lexmin_free(tl
);
5744 /* Return the lexicographically smallest rational point in the basic set
5745 * from which "tl" was constructed.
5746 * If the original input was empty, then return a zero-length vector.
5748 __isl_give isl_vec
*isl_tab_lexmin_get_solution(__isl_keep isl_tab_lexmin
*tl
)
5753 return isl_vec_alloc(tl
->ctx
, 0);
5755 return isl_tab_get_sample_value(tl
->tab
);
5758 struct isl_sol_pma
{
5760 isl_pw_multi_aff
*pma
;
5764 static void sol_pma_free(struct isl_sol
*sol
)
5766 struct isl_sol_pma
*sol_pma
= (struct isl_sol_pma
*) sol
;
5767 isl_pw_multi_aff_free(sol_pma
->pma
);
5768 isl_set_free(sol_pma
->empty
);
5771 /* This function is called for parts of the context where there is
5772 * no solution, with "bset" corresponding to the context tableau.
5773 * Simply add the basic set to the set "empty".
5775 static void sol_pma_add_empty(struct isl_sol_pma
*sol
,
5776 __isl_take isl_basic_set
*bset
)
5778 if (!bset
|| !sol
->empty
)
5781 sol
->empty
= isl_set_grow(sol
->empty
, 1);
5782 bset
= isl_basic_set_simplify(bset
);
5783 bset
= isl_basic_set_finalize(bset
);
5784 sol
->empty
= isl_set_add_basic_set(sol
->empty
, bset
);
5789 isl_basic_set_free(bset
);
5793 /* Given a basic set "dom" that represents the context and a tuple of
5794 * affine expressions "maff" defined over this domain, construct
5795 * an isl_pw_multi_aff with a single cell corresponding to "dom" and
5796 * the affine expressions in "maff".
5798 static void sol_pma_add(struct isl_sol_pma
*sol
,
5799 __isl_take isl_basic_set
*dom
, __isl_take isl_multi_aff
*maff
)
5801 isl_pw_multi_aff
*pma
;
5803 dom
= isl_basic_set_simplify(dom
);
5804 dom
= isl_basic_set_finalize(dom
);
5805 pma
= isl_pw_multi_aff_alloc(isl_set_from_basic_set(dom
), maff
);
5806 sol
->pma
= isl_pw_multi_aff_add_disjoint(sol
->pma
, pma
);
5811 static void sol_pma_add_empty_wrap(struct isl_sol
*sol
,
5812 __isl_take isl_basic_set
*bset
)
5814 sol_pma_add_empty((struct isl_sol_pma
*)sol
, bset
);
5817 static void sol_pma_add_wrap(struct isl_sol
*sol
,
5818 __isl_take isl_basic_set
*dom
, __isl_take isl_multi_aff
*ma
)
5820 sol_pma_add((struct isl_sol_pma
*)sol
, dom
, ma
);
5823 /* Construct an isl_sol_pma structure for accumulating the solution.
5824 * If track_empty is set, then we also keep track of the parts
5825 * of the context where there is no solution.
5826 * If max is set, then we are solving a maximization, rather than
5827 * a minimization problem, which means that the variables in the
5828 * tableau have value "M - x" rather than "M + x".
5830 static struct isl_sol
*sol_pma_init(__isl_keep isl_basic_map
*bmap
,
5831 __isl_take isl_basic_set
*dom
, int track_empty
, int max
)
5833 struct isl_sol_pma
*sol_pma
= NULL
;
5839 sol_pma
= isl_calloc_type(bmap
->ctx
, struct isl_sol_pma
);
5843 sol_pma
->sol
.free
= &sol_pma_free
;
5844 if (sol_init(&sol_pma
->sol
, bmap
, dom
, max
) < 0)
5846 sol_pma
->sol
.add
= &sol_pma_add_wrap
;
5847 sol_pma
->sol
.add_empty
= track_empty
? &sol_pma_add_empty_wrap
: NULL
;
5848 space
= isl_space_copy(sol_pma
->sol
.space
);
5849 sol_pma
->pma
= isl_pw_multi_aff_empty(space
);
5854 sol_pma
->empty
= isl_set_alloc_space(isl_basic_set_get_space(dom
),
5855 1, ISL_SET_DISJOINT
);
5856 if (!sol_pma
->empty
)
5860 isl_basic_set_free(dom
);
5861 return &sol_pma
->sol
;
5863 isl_basic_set_free(dom
);
5864 sol_free(&sol_pma
->sol
);
5868 /* Base case of isl_tab_basic_map_partial_lexopt, after removing
5869 * some obvious symmetries.
5871 * We call basic_map_partial_lexopt_base_sol and extract the results.
5873 static __isl_give isl_pw_multi_aff
*basic_map_partial_lexopt_base_pw_multi_aff(
5874 __isl_take isl_basic_map
*bmap
, __isl_take isl_basic_set
*dom
,
5875 __isl_give isl_set
**empty
, int max
)
5877 isl_pw_multi_aff
*result
= NULL
;
5878 struct isl_sol
*sol
;
5879 struct isl_sol_pma
*sol_pma
;
5881 sol
= basic_map_partial_lexopt_base_sol(bmap
, dom
, empty
, max
,
5885 sol_pma
= (struct isl_sol_pma
*) sol
;
5887 result
= isl_pw_multi_aff_copy(sol_pma
->pma
);
5889 *empty
= isl_set_copy(sol_pma
->empty
);
5890 sol_free(&sol_pma
->sol
);
5894 /* Given that the last input variable of "maff" represents the minimum
5895 * of some bounds, check whether we need to plug in the expression
5898 * In particular, check if the last input variable appears in any
5899 * of the expressions in "maff".
5901 static int need_substitution(__isl_keep isl_multi_aff
*maff
)
5906 pos
= isl_multi_aff_dim(maff
, isl_dim_in
) - 1;
5908 for (i
= 0; i
< maff
->n
; ++i
)
5909 if (isl_aff_involves_dims(maff
->p
[i
], isl_dim_in
, pos
, 1))
5915 /* Given a set of upper bounds on the last "input" variable m,
5916 * construct a piecewise affine expression that selects
5917 * the minimal upper bound to m, i.e.,
5918 * divide the space into cells where one
5919 * of the upper bounds is smaller than all the others and select
5920 * this upper bound on that cell.
5922 * In particular, if there are n bounds b_i, then the result
5923 * consists of n cell, each one of the form
5925 * b_i <= b_j for j > i
5926 * b_i < b_j for j < i
5928 * The affine expression on this cell is
5932 static __isl_give isl_pw_aff
*set_minimum_pa(__isl_take isl_space
*space
,
5933 __isl_take isl_mat
*var
)
5936 isl_aff
*aff
= NULL
;
5937 isl_basic_set
*bset
= NULL
;
5938 isl_pw_aff
*paff
= NULL
;
5939 isl_space
*pw_space
;
5940 isl_local_space
*ls
= NULL
;
5945 ls
= isl_local_space_from_space(isl_space_copy(space
));
5946 pw_space
= isl_space_copy(space
);
5947 pw_space
= isl_space_from_domain(pw_space
);
5948 pw_space
= isl_space_add_dims(pw_space
, isl_dim_out
, 1);
5949 paff
= isl_pw_aff_alloc_size(pw_space
, var
->n_row
);
5951 for (i
= 0; i
< var
->n_row
; ++i
) {
5954 aff
= isl_aff_alloc(isl_local_space_copy(ls
));
5955 bset
= isl_basic_set_alloc_space(isl_space_copy(space
), 0,
5959 isl_int_set_si(aff
->v
->el
[0], 1);
5960 isl_seq_cpy(aff
->v
->el
+ 1, var
->row
[i
], var
->n_col
);
5961 isl_int_set_si(aff
->v
->el
[1 + var
->n_col
], 0);
5962 bset
= select_minimum(bset
, var
, i
);
5963 paff_i
= isl_pw_aff_alloc(isl_set_from_basic_set(bset
), aff
);
5964 paff
= isl_pw_aff_add_disjoint(paff
, paff_i
);
5967 isl_local_space_free(ls
);
5968 isl_space_free(space
);
5973 isl_basic_set_free(bset
);
5974 isl_pw_aff_free(paff
);
5975 isl_local_space_free(ls
);
5976 isl_space_free(space
);
5981 /* Given a piecewise multi-affine expression of which the last input variable
5982 * is the minimum of the bounds in "cst", plug in the value of the minimum.
5983 * This minimum expression is given in "min_expr_pa".
5984 * The set "min_expr" contains the same information, but in the form of a set.
5985 * The variable is subsequently projected out.
5987 * The implementation is similar to those of "split" and "split_domain".
5988 * If the variable appears in a given expression, then minimum expression
5989 * is plugged in. Otherwise, if the variable appears in the constraints
5990 * and a split is required, then the domain is split. Otherwise, no split
5993 static __isl_give isl_pw_multi_aff
*split_domain_pma(
5994 __isl_take isl_pw_multi_aff
*opt
, __isl_take isl_pw_aff
*min_expr_pa
,
5995 __isl_take isl_set
*min_expr
, __isl_take isl_mat
*cst
)
6000 isl_pw_multi_aff
*res
;
6002 if (!opt
|| !min_expr
|| !cst
)
6005 n_in
= isl_pw_multi_aff_dim(opt
, isl_dim_in
);
6006 space
= isl_pw_multi_aff_get_space(opt
);
6007 space
= isl_space_drop_dims(space
, isl_dim_in
, n_in
- 1, 1);
6008 res
= isl_pw_multi_aff_empty(space
);
6010 for (i
= 0; i
< opt
->n
; ++i
) {
6011 isl_pw_multi_aff
*pma
;
6013 pma
= isl_pw_multi_aff_alloc(isl_set_copy(opt
->p
[i
].set
),
6014 isl_multi_aff_copy(opt
->p
[i
].maff
));
6015 if (need_substitution(opt
->p
[i
].maff
))
6016 pma
= isl_pw_multi_aff_substitute(pma
,
6017 isl_dim_in
, n_in
- 1, min_expr_pa
);
6020 split
= need_split_set(opt
->p
[i
].set
, cst
);
6022 pma
= isl_pw_multi_aff_free(pma
);
6024 pma
= isl_pw_multi_aff_intersect_domain(pma
,
6025 isl_set_copy(min_expr
));
6027 pma
= isl_pw_multi_aff_project_out(pma
,
6028 isl_dim_in
, n_in
- 1, 1);
6030 res
= isl_pw_multi_aff_add_disjoint(res
, pma
);
6033 isl_pw_multi_aff_free(opt
);
6034 isl_pw_aff_free(min_expr_pa
);
6035 isl_set_free(min_expr
);
6039 isl_pw_multi_aff_free(opt
);
6040 isl_pw_aff_free(min_expr_pa
);
6041 isl_set_free(min_expr
);
6046 static __isl_give isl_pw_multi_aff
*basic_map_partial_lexopt_pw_multi_aff(
6047 __isl_take isl_basic_map
*bmap
, __isl_take isl_basic_set
*dom
,
6048 __isl_give isl_set
**empty
, int max
);
6050 /* This function is called from basic_map_partial_lexopt_symm.
6051 * The last variable of "bmap" and "dom" corresponds to the minimum
6052 * of the bounds in "cst". "map_space" is the space of the original
6053 * input relation (of basic_map_partial_lexopt_symm) and "set_space"
6054 * is the space of the original domain.
6056 * We recursively call basic_map_partial_lexopt and then plug in
6057 * the definition of the minimum in the result.
6059 static __isl_give isl_pw_multi_aff
*
6060 basic_map_partial_lexopt_symm_core_pw_multi_aff(
6061 __isl_take isl_basic_map
*bmap
, __isl_take isl_basic_set
*dom
,
6062 __isl_give isl_set
**empty
, int max
, __isl_take isl_mat
*cst
,
6063 __isl_take isl_space
*map_space
, __isl_take isl_space
*set_space
)
6065 isl_pw_multi_aff
*opt
;
6066 isl_pw_aff
*min_expr_pa
;
6069 min_expr
= set_minimum(isl_basic_set_get_space(dom
), isl_mat_copy(cst
));
6070 min_expr_pa
= set_minimum_pa(isl_basic_set_get_space(dom
),
6073 opt
= basic_map_partial_lexopt_pw_multi_aff(bmap
, dom
, empty
, max
);
6076 *empty
= split(*empty
,
6077 isl_set_copy(min_expr
), isl_mat_copy(cst
));
6078 *empty
= isl_set_reset_space(*empty
, set_space
);
6081 opt
= split_domain_pma(opt
, min_expr_pa
, min_expr
, cst
);
6082 opt
= isl_pw_multi_aff_reset_space(opt
, map_space
);
6088 #define TYPE isl_pw_multi_aff
6090 #define SUFFIX _pw_multi_aff
6091 #include "isl_tab_lexopt_templ.c"