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 two 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.
189 struct isl_context
*context
;
190 struct isl_partial_sol
*partial
;
191 void (*add
)(struct isl_sol
*sol
,
192 __isl_take isl_basic_set
*dom
, __isl_take isl_multi_aff
*ma
);
193 void (*add_empty
)(struct isl_sol
*sol
, struct isl_basic_set
*bset
);
194 void (*free
)(struct isl_sol
*sol
);
195 struct isl_sol_callback dec_level
;
198 static void sol_free(struct isl_sol
*sol
)
200 struct isl_partial_sol
*partial
, *next
;
203 for (partial
= sol
->partial
; partial
; partial
= next
) {
204 next
= partial
->next
;
205 isl_basic_set_free(partial
->dom
);
206 isl_multi_aff_free(partial
->ma
);
209 isl_space_free(sol
->space
);
211 sol
->context
->op
->free(sol
->context
);
216 /* Push a partial solution represented by a domain and function "ma"
217 * onto the stack of partial solutions.
218 * If "ma" is NULL, then "dom" represents a part of the domain
221 static void sol_push_sol(struct isl_sol
*sol
,
222 __isl_take isl_basic_set
*dom
, __isl_take isl_multi_aff
*ma
)
224 struct isl_partial_sol
*partial
;
226 if (sol
->error
|| !dom
)
229 partial
= isl_alloc_type(dom
->ctx
, struct isl_partial_sol
);
233 partial
->level
= sol
->level
;
236 partial
->next
= sol
->partial
;
238 sol
->partial
= partial
;
242 isl_basic_set_free(dom
);
243 isl_multi_aff_free(ma
);
247 /* Check that the final columns of "M", starting at "first", are zero.
249 static isl_stat
check_final_columns_are_zero(__isl_keep isl_mat
*M
,
253 unsigned rows
, cols
, n
;
256 return isl_stat_error
;
257 rows
= isl_mat_rows(M
);
258 cols
= isl_mat_cols(M
);
260 for (i
= 0; i
< rows
; ++i
)
261 if (isl_seq_first_non_zero(M
->row
[i
] + first
, n
) != -1)
262 isl_die(isl_mat_get_ctx(M
), isl_error_internal
,
263 "final columns should be zero",
264 return isl_stat_error
);
268 /* Set the affine expressions in "ma" according to the rows in "M", which
269 * are defined over the local space "ls".
270 * The matrix "M" may have extra (zero) columns beyond the number
271 * of variables in "ls".
273 static __isl_give isl_multi_aff
*set_from_affine_matrix(
274 __isl_take isl_multi_aff
*ma
, __isl_take isl_local_space
*ls
,
275 __isl_take isl_mat
*M
)
280 if (!ma
|| !ls
|| !M
)
283 dim
= isl_local_space_dim(ls
, isl_dim_all
);
284 if (check_final_columns_are_zero(M
, 1 + dim
) < 0)
286 for (i
= 1; i
< M
->n_row
; ++i
) {
287 aff
= isl_aff_alloc(isl_local_space_copy(ls
));
289 isl_int_set(aff
->v
->el
[0], M
->row
[0][0]);
290 isl_seq_cpy(aff
->v
->el
+ 1, M
->row
[i
], 1 + dim
);
292 aff
= isl_aff_normalize(aff
);
293 ma
= isl_multi_aff_set_aff(ma
, i
- 1, aff
);
295 isl_local_space_free(ls
);
300 isl_local_space_free(ls
);
302 isl_multi_aff_free(ma
);
306 /* Push a partial solution represented by a domain and mapping M
307 * onto the stack of partial solutions.
309 * The affine matrix "M" maps the dimensions of the context
310 * to the output variables. Convert it into an isl_multi_aff and
311 * then call sol_push_sol.
313 * Note that the description of the initial context may have involved
314 * existentially quantified variables, in which case they also appear
315 * in "dom". These need to be removed before creating the affine
316 * expression because an affine expression cannot be defined in terms
317 * of existentially quantified variables without a known representation.
318 * Since newly added integer divisions are inserted before these
319 * existentially quantified variables, they are still in the final
320 * positions and the corresponding final columns of "M" are zero
321 * because align_context_divs adds the existentially quantified
322 * variables of the context to the main tableau without any constraints and
323 * any equality constraints that are added later on can only serve
324 * to eliminate these existentially quantified variables.
326 static void sol_push_sol_mat(struct isl_sol
*sol
,
327 __isl_take isl_basic_set
*dom
, __isl_take isl_mat
*M
)
333 n_div
= isl_basic_set_dim(dom
, isl_dim_div
);
334 n_known
= n_div
- sol
->context
->n_unknown
;
336 ma
= isl_multi_aff_alloc(isl_space_copy(sol
->space
));
337 ls
= isl_basic_set_get_local_space(dom
);
338 ls
= isl_local_space_drop_dims(ls
, isl_dim_div
,
339 n_known
, n_div
- n_known
);
340 ma
= set_from_affine_matrix(ma
, ls
, M
);
343 dom
= isl_basic_set_free(dom
);
344 sol_push_sol(sol
, dom
, ma
);
347 /* Pop one partial solution from the partial solution stack and
348 * pass it on to sol->add or sol->add_empty.
350 static void sol_pop_one(struct isl_sol
*sol
)
352 struct isl_partial_sol
*partial
;
354 partial
= sol
->partial
;
355 sol
->partial
= partial
->next
;
358 sol
->add(sol
, partial
->dom
, partial
->ma
);
360 sol
->add_empty(sol
, partial
->dom
);
364 /* Return a fresh copy of the domain represented by the context tableau.
366 static struct isl_basic_set
*sol_domain(struct isl_sol
*sol
)
368 struct isl_basic_set
*bset
;
373 bset
= isl_basic_set_dup(sol
->context
->op
->peek_basic_set(sol
->context
));
374 bset
= isl_basic_set_update_from_tab(bset
,
375 sol
->context
->op
->peek_tab(sol
->context
));
380 /* Check whether two partial solutions have the same affine expressions.
382 static isl_bool
same_solution(struct isl_partial_sol
*s1
,
383 struct isl_partial_sol
*s2
)
385 if (!s1
->ma
!= !s2
->ma
)
386 return isl_bool_false
;
388 return isl_bool_true
;
390 return isl_multi_aff_plain_is_equal(s1
->ma
, s2
->ma
);
393 /* Swap the initial two partial solutions in "sol".
397 * sol->partial = p1; p1->next = p2; p2->next = p3
401 * sol->partial = p2; p2->next = p1; p1->next = p3
403 static void swap_initial(struct isl_sol
*sol
)
405 struct isl_partial_sol
*partial
;
407 partial
= sol
->partial
;
408 sol
->partial
= partial
->next
;
409 partial
->next
= partial
->next
->next
;
410 sol
->partial
->next
= partial
;
413 /* Combine the initial two partial solution of "sol" into
414 * a partial solution with the current context domain of "sol" and
415 * the function description of the second partial solution in the list.
416 * The level of the new partial solution is set to the current level.
418 * That is, the first two partial solutions (D1,M1) and (D2,M2) are
419 * replaced by (D,M2), where D is the domain of "sol", which is assumed
420 * to be the union of D1 and D2, while M1 is assumed to be equal to M2
423 static isl_stat
combine_initial_into_second(struct isl_sol
*sol
)
425 struct isl_partial_sol
*partial
;
428 partial
= sol
->partial
;
430 bset
= sol_domain(sol
);
431 isl_basic_set_free(partial
->next
->dom
);
432 partial
->next
->dom
= bset
;
433 partial
->next
->level
= sol
->level
;
436 return isl_stat_error
;
438 sol
->partial
= partial
->next
;
439 isl_basic_set_free(partial
->dom
);
440 isl_multi_aff_free(partial
->ma
);
446 /* Are "ma1" and "ma2" equal to each other on "dom"?
448 * Combine "ma1" and "ma2" with "dom" and check if the results are the same.
449 * "dom" may have existentially quantified variables. Eliminate them first
450 * as otherwise they would have to be eliminated twice, in a more complicated
453 static isl_bool
equal_on_domain(__isl_keep isl_multi_aff
*ma1
,
454 __isl_keep isl_multi_aff
*ma2
, __isl_keep isl_basic_set
*dom
)
457 isl_pw_multi_aff
*pma1
, *pma2
;
460 set
= isl_basic_set_compute_divs(isl_basic_set_copy(dom
));
461 pma1
= isl_pw_multi_aff_alloc(isl_set_copy(set
),
462 isl_multi_aff_copy(ma1
));
463 pma2
= isl_pw_multi_aff_alloc(set
, isl_multi_aff_copy(ma2
));
464 equal
= isl_pw_multi_aff_is_equal(pma1
, pma2
);
465 isl_pw_multi_aff_free(pma1
);
466 isl_pw_multi_aff_free(pma2
);
471 /* The initial two partial solutions of "sol" are known to be at
473 * If they represent the same solution (on different parts of the domain),
474 * then combine them into a single solution at the current level.
475 * Otherwise, pop them both.
477 * Even if the two partial solution are not obviously the same,
478 * one may still be a simplification of the other over its own domain.
479 * Also check if the two sets of affine functions are equal when
480 * restricted to one of the domains. If so, combine the two
481 * using the set of affine functions on the other domain.
482 * That is, for two partial solutions (D1,M1) and (D2,M2),
483 * if M1 = M2 on D1, then the pair of partial solutions can
484 * be replaced by (D1+D2,M2) and similarly when M1 = M2 on D2.
486 static isl_stat
combine_initial_if_equal(struct isl_sol
*sol
)
488 struct isl_partial_sol
*partial
;
491 partial
= sol
->partial
;
493 same
= same_solution(partial
, partial
->next
);
495 return isl_stat_error
;
497 return combine_initial_into_second(sol
);
498 if (partial
->ma
&& partial
->next
->ma
) {
499 same
= equal_on_domain(partial
->ma
, partial
->next
->ma
,
502 return isl_stat_error
;
504 return combine_initial_into_second(sol
);
505 same
= equal_on_domain(partial
->ma
, partial
->next
->ma
,
509 return combine_initial_into_second(sol
);
519 /* Pop all solutions from the partial solution stack that were pushed onto
520 * the stack at levels that are deeper than the current level.
521 * If the two topmost elements on the stack have the same level
522 * and represent the same solution, then their domains are combined.
523 * This combined domain is the same as the current context domain
524 * as sol_pop is called each time we move back to a higher level.
525 * If the outer level (0) has been reached, then all partial solutions
526 * at the current level are also popped off.
528 static void sol_pop(struct isl_sol
*sol
)
530 struct isl_partial_sol
*partial
;
535 partial
= sol
->partial
;
539 if (partial
->level
== 0 && sol
->level
== 0) {
540 for (partial
= sol
->partial
; partial
; partial
= sol
->partial
)
545 if (partial
->level
<= sol
->level
)
548 if (partial
->next
&& partial
->next
->level
== partial
->level
) {
549 if (combine_initial_if_equal(sol
) < 0)
554 if (sol
->level
== 0) {
555 for (partial
= sol
->partial
; partial
; partial
= sol
->partial
)
561 error
: sol
->error
= 1;
564 static void sol_dec_level(struct isl_sol
*sol
)
574 static isl_stat
sol_dec_level_wrap(struct isl_tab_callback
*cb
)
576 struct isl_sol_callback
*callback
= (struct isl_sol_callback
*)cb
;
578 sol_dec_level(callback
->sol
);
580 return callback
->sol
->error
? isl_stat_error
: isl_stat_ok
;
583 /* Move down to next level and push callback onto context tableau
584 * to decrease the level again when it gets rolled back across
585 * the current state. That is, dec_level will be called with
586 * the context tableau in the same state as it is when inc_level
589 static void sol_inc_level(struct isl_sol
*sol
)
597 tab
= sol
->context
->op
->peek_tab(sol
->context
);
598 if (isl_tab_push_callback(tab
, &sol
->dec_level
.callback
) < 0)
602 static void scale_rows(struct isl_mat
*mat
, isl_int m
, int n_row
)
606 if (isl_int_is_one(m
))
609 for (i
= 0; i
< n_row
; ++i
)
610 isl_seq_scale(mat
->row
[i
], mat
->row
[i
], m
, mat
->n_col
);
613 /* Add the solution identified by the tableau and the context tableau.
615 * The layout of the variables is as follows.
616 * tab->n_var is equal to the total number of variables in the input
617 * map (including divs that were copied from the context)
618 * + the number of extra divs constructed
619 * Of these, the first tab->n_param and the last tab->n_div variables
620 * correspond to the variables in the context, i.e.,
621 * tab->n_param + tab->n_div = context_tab->n_var
622 * tab->n_param is equal to the number of parameters and input
623 * dimensions in the input map
624 * tab->n_div is equal to the number of divs in the context
626 * If there is no solution, then call add_empty with a basic set
627 * that corresponds to the context tableau. (If add_empty is NULL,
630 * If there is a solution, then first construct a matrix that maps
631 * all dimensions of the context to the output variables, i.e.,
632 * the output dimensions in the input map.
633 * The divs in the input map (if any) that do not correspond to any
634 * div in the context do not appear in the solution.
635 * The algorithm will make sure that they have an integer value,
636 * but these values themselves are of no interest.
637 * We have to be careful not to drop or rearrange any divs in the
638 * context because that would change the meaning of the matrix.
640 * To extract the value of the output variables, it should be noted
641 * that we always use a big parameter M in the main tableau and so
642 * the variable stored in this tableau is not an output variable x itself, but
643 * x' = M + x (in case of minimization)
645 * x' = M - x (in case of maximization)
646 * If x' appears in a column, then its optimal value is zero,
647 * which means that the optimal value of x is an unbounded number
648 * (-M for minimization and M for maximization).
649 * We currently assume that the output dimensions in the original map
650 * are bounded, so this cannot occur.
651 * Similarly, when x' appears in a row, then the coefficient of M in that
652 * row is necessarily 1.
653 * If the row in the tableau represents
654 * d x' = c + d M + e(y)
655 * then, in case of minimization, the corresponding row in the matrix
658 * with a d = m, the (updated) common denominator of the matrix.
659 * In case of maximization, the row will be
662 static void sol_add(struct isl_sol
*sol
, struct isl_tab
*tab
)
664 struct isl_basic_set
*bset
= NULL
;
665 struct isl_mat
*mat
= NULL
;
670 if (sol
->error
|| !tab
)
673 if (tab
->empty
&& !sol
->add_empty
)
675 if (sol
->context
->op
->is_empty(sol
->context
))
678 bset
= sol_domain(sol
);
681 sol_push_sol(sol
, bset
, NULL
);
687 mat
= isl_mat_alloc(tab
->mat
->ctx
, 1 + sol
->n_out
,
688 1 + tab
->n_param
+ tab
->n_div
);
694 isl_seq_clr(mat
->row
[0] + 1, mat
->n_col
- 1);
695 isl_int_set_si(mat
->row
[0][0], 1);
696 for (row
= 0; row
< sol
->n_out
; ++row
) {
697 int i
= tab
->n_param
+ row
;
700 isl_seq_clr(mat
->row
[1 + row
], mat
->n_col
);
701 if (!tab
->var
[i
].is_row
) {
703 isl_die(mat
->ctx
, isl_error_invalid
,
704 "unbounded optimum", goto error2
);
708 r
= tab
->var
[i
].index
;
710 isl_int_ne(tab
->mat
->row
[r
][2], tab
->mat
->row
[r
][0]))
711 isl_die(mat
->ctx
, isl_error_invalid
,
712 "unbounded optimum", goto error2
);
713 isl_int_gcd(m
, mat
->row
[0][0], tab
->mat
->row
[r
][0]);
714 isl_int_divexact(m
, tab
->mat
->row
[r
][0], m
);
715 scale_rows(mat
, m
, 1 + row
);
716 isl_int_divexact(m
, mat
->row
[0][0], tab
->mat
->row
[r
][0]);
717 isl_int_mul(mat
->row
[1 + row
][0], m
, tab
->mat
->row
[r
][1]);
718 for (j
= 0; j
< tab
->n_param
; ++j
) {
720 if (tab
->var
[j
].is_row
)
722 col
= tab
->var
[j
].index
;
723 isl_int_mul(mat
->row
[1 + row
][1 + j
], m
,
724 tab
->mat
->row
[r
][off
+ col
]);
726 for (j
= 0; j
< tab
->n_div
; ++j
) {
728 if (tab
->var
[tab
->n_var
- tab
->n_div
+j
].is_row
)
730 col
= tab
->var
[tab
->n_var
- tab
->n_div
+j
].index
;
731 isl_int_mul(mat
->row
[1 + row
][1 + tab
->n_param
+ j
], m
,
732 tab
->mat
->row
[r
][off
+ col
]);
735 isl_seq_neg(mat
->row
[1 + row
], mat
->row
[1 + row
],
741 sol_push_sol_mat(sol
, bset
, mat
);
746 isl_basic_set_free(bset
);
754 struct isl_set
*empty
;
757 static void sol_map_free(struct isl_sol
*sol
)
759 struct isl_sol_map
*sol_map
= (struct isl_sol_map
*) sol
;
760 isl_map_free(sol_map
->map
);
761 isl_set_free(sol_map
->empty
);
764 /* This function is called for parts of the context where there is
765 * no solution, with "bset" corresponding to the context tableau.
766 * Simply add the basic set to the set "empty".
768 static void sol_map_add_empty(struct isl_sol_map
*sol
,
769 struct isl_basic_set
*bset
)
771 if (!bset
|| !sol
->empty
)
774 sol
->empty
= isl_set_grow(sol
->empty
, 1);
775 bset
= isl_basic_set_simplify(bset
);
776 bset
= isl_basic_set_finalize(bset
);
777 sol
->empty
= isl_set_add_basic_set(sol
->empty
, isl_basic_set_copy(bset
));
780 isl_basic_set_free(bset
);
783 isl_basic_set_free(bset
);
787 static void sol_map_add_empty_wrap(struct isl_sol
*sol
,
788 struct isl_basic_set
*bset
)
790 sol_map_add_empty((struct isl_sol_map
*)sol
, bset
);
793 /* Given a basic set "dom" that represents the context and a tuple of
794 * affine expressions "ma" defined over this domain, construct a basic map
795 * that expresses this function on the domain.
797 static void sol_map_add(struct isl_sol_map
*sol
,
798 __isl_take isl_basic_set
*dom
, __isl_take isl_multi_aff
*ma
)
802 if (sol
->sol
.error
|| !dom
|| !ma
)
805 bmap
= isl_basic_map_from_multi_aff2(ma
, sol
->sol
.rational
);
806 bmap
= isl_basic_map_intersect_domain(bmap
, dom
);
807 sol
->map
= isl_map_grow(sol
->map
, 1);
808 sol
->map
= isl_map_add_basic_map(sol
->map
, bmap
);
813 isl_basic_set_free(dom
);
814 isl_multi_aff_free(ma
);
818 static void sol_map_add_wrap(struct isl_sol
*sol
,
819 __isl_take isl_basic_set
*dom
, __isl_take isl_multi_aff
*ma
)
821 sol_map_add((struct isl_sol_map
*)sol
, dom
, ma
);
825 /* Store the "parametric constant" of row "row" of tableau "tab" in "line",
826 * i.e., the constant term and the coefficients of all variables that
827 * appear in the context tableau.
828 * Note that the coefficient of the big parameter M is NOT copied.
829 * The context tableau may not have a big parameter and even when it
830 * does, it is a different big parameter.
832 static void get_row_parameter_line(struct isl_tab
*tab
, int row
, isl_int
*line
)
835 unsigned off
= 2 + tab
->M
;
837 isl_int_set(line
[0], tab
->mat
->row
[row
][1]);
838 for (i
= 0; i
< tab
->n_param
; ++i
) {
839 if (tab
->var
[i
].is_row
)
840 isl_int_set_si(line
[1 + i
], 0);
842 int col
= tab
->var
[i
].index
;
843 isl_int_set(line
[1 + i
], tab
->mat
->row
[row
][off
+ col
]);
846 for (i
= 0; i
< tab
->n_div
; ++i
) {
847 if (tab
->var
[tab
->n_var
- tab
->n_div
+ i
].is_row
)
848 isl_int_set_si(line
[1 + tab
->n_param
+ i
], 0);
850 int col
= tab
->var
[tab
->n_var
- tab
->n_div
+ i
].index
;
851 isl_int_set(line
[1 + tab
->n_param
+ i
],
852 tab
->mat
->row
[row
][off
+ col
]);
857 /* Check if rows "row1" and "row2" have identical "parametric constants",
858 * as explained above.
859 * In this case, we also insist that the coefficients of the big parameter
860 * be the same as the values of the constants will only be the same
861 * if these coefficients are also the same.
863 static int identical_parameter_line(struct isl_tab
*tab
, int row1
, int row2
)
866 unsigned off
= 2 + tab
->M
;
868 if (isl_int_ne(tab
->mat
->row
[row1
][1], tab
->mat
->row
[row2
][1]))
871 if (tab
->M
&& isl_int_ne(tab
->mat
->row
[row1
][2],
872 tab
->mat
->row
[row2
][2]))
875 for (i
= 0; i
< tab
->n_param
+ tab
->n_div
; ++i
) {
876 int pos
= i
< tab
->n_param
? i
:
877 tab
->n_var
- tab
->n_div
+ i
- tab
->n_param
;
880 if (tab
->var
[pos
].is_row
)
882 col
= tab
->var
[pos
].index
;
883 if (isl_int_ne(tab
->mat
->row
[row1
][off
+ col
],
884 tab
->mat
->row
[row2
][off
+ col
]))
890 /* Return an inequality that expresses that the "parametric constant"
891 * should be non-negative.
892 * This function is only called when the coefficient of the big parameter
895 static struct isl_vec
*get_row_parameter_ineq(struct isl_tab
*tab
, int row
)
897 struct isl_vec
*ineq
;
899 ineq
= isl_vec_alloc(tab
->mat
->ctx
, 1 + tab
->n_param
+ tab
->n_div
);
903 get_row_parameter_line(tab
, row
, ineq
->el
);
905 ineq
= isl_vec_normalize(ineq
);
910 /* Normalize a div expression of the form
912 * [(g*f(x) + c)/(g * m)]
914 * with c the constant term and f(x) the remaining coefficients, to
918 static void normalize_div(__isl_keep isl_vec
*div
)
920 isl_ctx
*ctx
= isl_vec_get_ctx(div
);
921 int len
= div
->size
- 2;
923 isl_seq_gcd(div
->el
+ 2, len
, &ctx
->normalize_gcd
);
924 isl_int_gcd(ctx
->normalize_gcd
, ctx
->normalize_gcd
, div
->el
[0]);
926 if (isl_int_is_one(ctx
->normalize_gcd
))
929 isl_int_divexact(div
->el
[0], div
->el
[0], ctx
->normalize_gcd
);
930 isl_int_fdiv_q(div
->el
[1], div
->el
[1], ctx
->normalize_gcd
);
931 isl_seq_scale_down(div
->el
+ 2, div
->el
+ 2, ctx
->normalize_gcd
, len
);
934 /* Return an integer division for use in a parametric cut based
936 * In particular, let the parametric constant of the row be
940 * where y_0 = 1, but none of the y_i corresponds to the big parameter M.
941 * The div returned is equal to
943 * floor(\sum_i {-a_i} y_i) = floor((\sum_i (-a_i mod d) y_i)/d)
945 static struct isl_vec
*get_row_parameter_div(struct isl_tab
*tab
, int row
)
949 div
= isl_vec_alloc(tab
->mat
->ctx
, 1 + 1 + tab
->n_param
+ tab
->n_div
);
953 isl_int_set(div
->el
[0], tab
->mat
->row
[row
][0]);
954 get_row_parameter_line(tab
, row
, div
->el
+ 1);
955 isl_seq_neg(div
->el
+ 1, div
->el
+ 1, div
->size
- 1);
957 isl_seq_fdiv_r(div
->el
+ 1, div
->el
+ 1, div
->el
[0], div
->size
- 1);
962 /* Return an integer division for use in transferring an integrality constraint
964 * In particular, let the parametric constant of the row be
968 * where y_0 = 1, but none of the y_i corresponds to the big parameter M.
969 * The the returned div is equal to
971 * floor(\sum_i {a_i} y_i) = floor((\sum_i (a_i mod d) y_i)/d)
973 static struct isl_vec
*get_row_split_div(struct isl_tab
*tab
, int row
)
977 div
= isl_vec_alloc(tab
->mat
->ctx
, 1 + 1 + tab
->n_param
+ tab
->n_div
);
981 isl_int_set(div
->el
[0], tab
->mat
->row
[row
][0]);
982 get_row_parameter_line(tab
, row
, div
->el
+ 1);
984 isl_seq_fdiv_r(div
->el
+ 1, div
->el
+ 1, div
->el
[0], div
->size
- 1);
989 /* Construct and return an inequality that expresses an upper bound
991 * In particular, if the div is given by
995 * then the inequality expresses
999 static __isl_give isl_vec
*ineq_for_div(__isl_keep isl_basic_set
*bset
,
1004 struct isl_vec
*ineq
;
1009 total
= isl_basic_set_total_dim(bset
);
1010 div_pos
= 1 + total
- bset
->n_div
+ div
;
1012 ineq
= isl_vec_alloc(bset
->ctx
, 1 + total
);
1016 isl_seq_cpy(ineq
->el
, bset
->div
[div
] + 1, 1 + total
);
1017 isl_int_neg(ineq
->el
[div_pos
], bset
->div
[div
][0]);
1021 /* Given a row in the tableau and a div that was created
1022 * using get_row_split_div and that has been constrained to equality, i.e.,
1024 * d = floor(\sum_i {a_i} y_i) = \sum_i {a_i} y_i
1026 * replace the expression "\sum_i {a_i} y_i" in the row by d,
1027 * i.e., we subtract "\sum_i {a_i} y_i" and add 1 d.
1028 * The coefficients of the non-parameters in the tableau have been
1029 * verified to be integral. We can therefore simply replace coefficient b
1030 * by floor(b). For the coefficients of the parameters we have
1031 * floor(a_i) = a_i - {a_i}, while for the other coefficients, we have
1034 static struct isl_tab
*set_row_cst_to_div(struct isl_tab
*tab
, int row
, int div
)
1036 isl_seq_fdiv_q(tab
->mat
->row
[row
] + 1, tab
->mat
->row
[row
] + 1,
1037 tab
->mat
->row
[row
][0], 1 + tab
->M
+ tab
->n_col
);
1039 isl_int_set_si(tab
->mat
->row
[row
][0], 1);
1041 if (tab
->var
[tab
->n_var
- tab
->n_div
+ div
].is_row
) {
1042 int drow
= tab
->var
[tab
->n_var
- tab
->n_div
+ div
].index
;
1044 isl_assert(tab
->mat
->ctx
,
1045 isl_int_is_one(tab
->mat
->row
[drow
][0]), goto error
);
1046 isl_seq_combine(tab
->mat
->row
[row
] + 1,
1047 tab
->mat
->ctx
->one
, tab
->mat
->row
[row
] + 1,
1048 tab
->mat
->ctx
->one
, tab
->mat
->row
[drow
] + 1,
1049 1 + tab
->M
+ tab
->n_col
);
1051 int dcol
= tab
->var
[tab
->n_var
- tab
->n_div
+ div
].index
;
1053 isl_int_add_ui(tab
->mat
->row
[row
][2 + tab
->M
+ dcol
],
1054 tab
->mat
->row
[row
][2 + tab
->M
+ dcol
], 1);
1063 /* Check if the (parametric) constant of the given row is obviously
1064 * negative, meaning that we don't need to consult the context tableau.
1065 * If there is a big parameter and its coefficient is non-zero,
1066 * then this coefficient determines the outcome.
1067 * Otherwise, we check whether the constant is negative and
1068 * all non-zero coefficients of parameters are negative and
1069 * belong to non-negative parameters.
1071 static int is_obviously_neg(struct isl_tab
*tab
, int row
)
1075 unsigned off
= 2 + tab
->M
;
1078 if (isl_int_is_pos(tab
->mat
->row
[row
][2]))
1080 if (isl_int_is_neg(tab
->mat
->row
[row
][2]))
1084 if (isl_int_is_nonneg(tab
->mat
->row
[row
][1]))
1086 for (i
= 0; i
< tab
->n_param
; ++i
) {
1087 /* Eliminated parameter */
1088 if (tab
->var
[i
].is_row
)
1090 col
= tab
->var
[i
].index
;
1091 if (isl_int_is_zero(tab
->mat
->row
[row
][off
+ col
]))
1093 if (!tab
->var
[i
].is_nonneg
)
1095 if (isl_int_is_pos(tab
->mat
->row
[row
][off
+ col
]))
1098 for (i
= 0; i
< tab
->n_div
; ++i
) {
1099 if (tab
->var
[tab
->n_var
- tab
->n_div
+ i
].is_row
)
1101 col
= tab
->var
[tab
->n_var
- tab
->n_div
+ i
].index
;
1102 if (isl_int_is_zero(tab
->mat
->row
[row
][off
+ col
]))
1104 if (!tab
->var
[tab
->n_var
- tab
->n_div
+ i
].is_nonneg
)
1106 if (isl_int_is_pos(tab
->mat
->row
[row
][off
+ col
]))
1112 /* Check if the (parametric) constant of the given row is obviously
1113 * non-negative, meaning that we don't need to consult the context tableau.
1114 * If there is a big parameter and its coefficient is non-zero,
1115 * then this coefficient determines the outcome.
1116 * Otherwise, we check whether the constant is non-negative and
1117 * all non-zero coefficients of parameters are positive and
1118 * belong to non-negative parameters.
1120 static int is_obviously_nonneg(struct isl_tab
*tab
, int row
)
1124 unsigned off
= 2 + tab
->M
;
1127 if (isl_int_is_pos(tab
->mat
->row
[row
][2]))
1129 if (isl_int_is_neg(tab
->mat
->row
[row
][2]))
1133 if (isl_int_is_neg(tab
->mat
->row
[row
][1]))
1135 for (i
= 0; i
< tab
->n_param
; ++i
) {
1136 /* Eliminated parameter */
1137 if (tab
->var
[i
].is_row
)
1139 col
= tab
->var
[i
].index
;
1140 if (isl_int_is_zero(tab
->mat
->row
[row
][off
+ col
]))
1142 if (!tab
->var
[i
].is_nonneg
)
1144 if (isl_int_is_neg(tab
->mat
->row
[row
][off
+ col
]))
1147 for (i
= 0; i
< tab
->n_div
; ++i
) {
1148 if (tab
->var
[tab
->n_var
- tab
->n_div
+ i
].is_row
)
1150 col
= tab
->var
[tab
->n_var
- tab
->n_div
+ i
].index
;
1151 if (isl_int_is_zero(tab
->mat
->row
[row
][off
+ col
]))
1153 if (!tab
->var
[tab
->n_var
- tab
->n_div
+ i
].is_nonneg
)
1155 if (isl_int_is_neg(tab
->mat
->row
[row
][off
+ col
]))
1161 /* Given a row r and two columns, return the column that would
1162 * lead to the lexicographically smallest increment in the sample
1163 * solution when leaving the basis in favor of the row.
1164 * Pivoting with column c will increment the sample value by a non-negative
1165 * constant times a_{V,c}/a_{r,c}, with a_{V,c} the elements of column c
1166 * corresponding to the non-parametric variables.
1167 * If variable v appears in a column c_v, then a_{v,c} = 1 iff c = c_v,
1168 * with all other entries in this virtual row equal to zero.
1169 * If variable v appears in a row, then a_{v,c} is the element in column c
1172 * Let v be the first variable with a_{v,c1}/a_{r,c1} != a_{v,c2}/a_{r,c2}.
1173 * Then if a_{v,c1}/a_{r,c1} < a_{v,c2}/a_{r,c2}, i.e.,
1174 * a_{v,c2} a_{r,c1} - a_{v,c1} a_{r,c2} > 0, c1 results in the minimal
1175 * increment. Otherwise, it's c2.
1177 static int lexmin_col_pair(struct isl_tab
*tab
,
1178 int row
, int col1
, int col2
, isl_int tmp
)
1183 tr
= tab
->mat
->row
[row
] + 2 + tab
->M
;
1185 for (i
= tab
->n_param
; i
< tab
->n_var
- tab
->n_div
; ++i
) {
1189 if (!tab
->var
[i
].is_row
) {
1190 if (tab
->var
[i
].index
== col1
)
1192 if (tab
->var
[i
].index
== col2
)
1197 if (tab
->var
[i
].index
== row
)
1200 r
= tab
->mat
->row
[tab
->var
[i
].index
] + 2 + tab
->M
;
1201 s1
= isl_int_sgn(r
[col1
]);
1202 s2
= isl_int_sgn(r
[col2
]);
1203 if (s1
== 0 && s2
== 0)
1210 isl_int_mul(tmp
, r
[col2
], tr
[col1
]);
1211 isl_int_submul(tmp
, r
[col1
], tr
[col2
]);
1212 if (isl_int_is_pos(tmp
))
1214 if (isl_int_is_neg(tmp
))
1220 /* Does the index into the tab->var or tab->con array "index"
1221 * correspond to a variable in the context tableau?
1222 * In particular, it needs to be an index into the tab->var array and
1223 * it needs to refer to either one of the first tab->n_param variables or
1224 * one of the last tab->n_div variables.
1226 static int is_parameter_var(struct isl_tab
*tab
, int index
)
1230 if (index
< tab
->n_param
)
1232 if (index
>= tab
->n_var
- tab
->n_div
)
1237 /* Does column "col" of "tab" refer to a variable in the context tableau?
1239 static int col_is_parameter_var(struct isl_tab
*tab
, int col
)
1241 return is_parameter_var(tab
, tab
->col_var
[col
]);
1244 /* Does row "row" of "tab" refer to a variable in the context tableau?
1246 static int row_is_parameter_var(struct isl_tab
*tab
, int row
)
1248 return is_parameter_var(tab
, tab
->row_var
[row
]);
1251 /* Given a row in the tableau, find and return the column that would
1252 * result in the lexicographically smallest, but positive, increment
1253 * in the sample point.
1254 * If there is no such column, then return tab->n_col.
1255 * If anything goes wrong, return -1.
1257 static int lexmin_pivot_col(struct isl_tab
*tab
, int row
)
1260 int col
= tab
->n_col
;
1264 tr
= tab
->mat
->row
[row
] + 2 + tab
->M
;
1268 for (j
= tab
->n_dead
; j
< tab
->n_col
; ++j
) {
1269 if (col_is_parameter_var(tab
, j
))
1272 if (!isl_int_is_pos(tr
[j
]))
1275 if (col
== tab
->n_col
)
1278 col
= lexmin_col_pair(tab
, row
, col
, j
, tmp
);
1279 isl_assert(tab
->mat
->ctx
, col
>= 0, goto error
);
1289 /* Return the first known violated constraint, i.e., a non-negative
1290 * constraint that currently has an either obviously negative value
1291 * or a previously determined to be negative value.
1293 * If any constraint has a negative coefficient for the big parameter,
1294 * if any, then we return one of these first.
1296 static int first_neg(struct isl_tab
*tab
)
1301 for (row
= tab
->n_redundant
; row
< tab
->n_row
; ++row
) {
1302 if (!isl_tab_var_from_row(tab
, row
)->is_nonneg
)
1304 if (!isl_int_is_neg(tab
->mat
->row
[row
][2]))
1307 tab
->row_sign
[row
] = isl_tab_row_neg
;
1310 for (row
= tab
->n_redundant
; row
< tab
->n_row
; ++row
) {
1311 if (!isl_tab_var_from_row(tab
, row
)->is_nonneg
)
1313 if (tab
->row_sign
) {
1314 if (tab
->row_sign
[row
] == 0 &&
1315 is_obviously_neg(tab
, row
))
1316 tab
->row_sign
[row
] = isl_tab_row_neg
;
1317 if (tab
->row_sign
[row
] != isl_tab_row_neg
)
1319 } else if (!is_obviously_neg(tab
, row
))
1326 /* Check whether the invariant that all columns are lexico-positive
1327 * is satisfied. This function is not called from the current code
1328 * but is useful during debugging.
1330 static void check_lexpos(struct isl_tab
*tab
) __attribute__ ((unused
));
1331 static void check_lexpos(struct isl_tab
*tab
)
1333 unsigned off
= 2 + tab
->M
;
1338 for (col
= tab
->n_dead
; col
< tab
->n_col
; ++col
) {
1339 if (col_is_parameter_var(tab
, col
))
1341 for (var
= tab
->n_param
; var
< tab
->n_var
- tab
->n_div
; ++var
) {
1342 if (!tab
->var
[var
].is_row
) {
1343 if (tab
->var
[var
].index
== col
)
1348 row
= tab
->var
[var
].index
;
1349 if (isl_int_is_zero(tab
->mat
->row
[row
][off
+ col
]))
1351 if (isl_int_is_pos(tab
->mat
->row
[row
][off
+ col
]))
1353 fprintf(stderr
, "lexneg column %d (row %d)\n",
1356 if (var
>= tab
->n_var
- tab
->n_div
)
1357 fprintf(stderr
, "zero column %d\n", col
);
1361 /* Report to the caller that the given constraint is part of an encountered
1364 static int report_conflicting_constraint(struct isl_tab
*tab
, int con
)
1366 return tab
->conflict(con
, tab
->conflict_user
);
1369 /* Given a conflicting row in the tableau, report all constraints
1370 * involved in the row to the caller. That is, the row itself
1371 * (if it represents a constraint) and all constraint columns with
1372 * non-zero (and therefore negative) coefficients.
1374 static int report_conflict(struct isl_tab
*tab
, int row
)
1382 if (tab
->row_var
[row
] < 0 &&
1383 report_conflicting_constraint(tab
, ~tab
->row_var
[row
]) < 0)
1386 tr
= tab
->mat
->row
[row
] + 2 + tab
->M
;
1388 for (j
= tab
->n_dead
; j
< tab
->n_col
; ++j
) {
1389 if (col_is_parameter_var(tab
, j
))
1392 if (!isl_int_is_neg(tr
[j
]))
1395 if (tab
->col_var
[j
] < 0 &&
1396 report_conflicting_constraint(tab
, ~tab
->col_var
[j
]) < 0)
1403 /* Resolve all known or obviously violated constraints through pivoting.
1404 * In particular, as long as we can find any violated constraint, we
1405 * look for a pivoting column that would result in the lexicographically
1406 * smallest increment in the sample point. If there is no such column
1407 * then the tableau is infeasible.
1409 static int restore_lexmin(struct isl_tab
*tab
) WARN_UNUSED
;
1410 static int restore_lexmin(struct isl_tab
*tab
)
1418 while ((row
= first_neg(tab
)) != -1) {
1419 col
= lexmin_pivot_col(tab
, row
);
1420 if (col
>= tab
->n_col
) {
1421 if (report_conflict(tab
, row
) < 0)
1423 if (isl_tab_mark_empty(tab
) < 0)
1429 if (isl_tab_pivot(tab
, row
, col
) < 0)
1435 /* Given a row that represents an equality, look for an appropriate
1437 * In particular, if there are any non-zero coefficients among
1438 * the non-parameter variables, then we take the last of these
1439 * variables. Eliminating this variable in terms of the other
1440 * variables and/or parameters does not influence the property
1441 * that all column in the initial tableau are lexicographically
1442 * positive. The row corresponding to the eliminated variable
1443 * will only have non-zero entries below the diagonal of the
1444 * initial tableau. That is, we transform
1450 * If there is no such non-parameter variable, then we are dealing with
1451 * pure parameter equality and we pick any parameter with coefficient 1 or -1
1452 * for elimination. This will ensure that the eliminated parameter
1453 * always has an integer value whenever all the other parameters are integral.
1454 * If there is no such parameter then we return -1.
1456 static int last_var_col_or_int_par_col(struct isl_tab
*tab
, int row
)
1458 unsigned off
= 2 + tab
->M
;
1461 for (i
= tab
->n_var
- tab
->n_div
- 1; i
>= 0 && i
>= tab
->n_param
; --i
) {
1463 if (tab
->var
[i
].is_row
)
1465 col
= tab
->var
[i
].index
;
1466 if (col
<= tab
->n_dead
)
1468 if (!isl_int_is_zero(tab
->mat
->row
[row
][off
+ col
]))
1471 for (i
= tab
->n_dead
; i
< tab
->n_col
; ++i
) {
1472 if (isl_int_is_one(tab
->mat
->row
[row
][off
+ i
]))
1474 if (isl_int_is_negone(tab
->mat
->row
[row
][off
+ i
]))
1480 /* Add an equality that is known to be valid to the tableau.
1481 * We first check if we can eliminate a variable or a parameter.
1482 * If not, we add the equality as two inequalities.
1483 * In this case, the equality was a pure parameter equality and there
1484 * is no need to resolve any constraint violations.
1486 * This function assumes that at least two more rows and at least
1487 * two more elements in the constraint array are available in the tableau.
1489 static struct isl_tab
*add_lexmin_valid_eq(struct isl_tab
*tab
, isl_int
*eq
)
1496 r
= isl_tab_add_row(tab
, eq
);
1500 r
= tab
->con
[r
].index
;
1501 i
= last_var_col_or_int_par_col(tab
, r
);
1503 tab
->con
[r
].is_nonneg
= 1;
1504 if (isl_tab_push_var(tab
, isl_tab_undo_nonneg
, &tab
->con
[r
]) < 0)
1506 isl_seq_neg(eq
, eq
, 1 + tab
->n_var
);
1507 r
= isl_tab_add_row(tab
, eq
);
1510 tab
->con
[r
].is_nonneg
= 1;
1511 if (isl_tab_push_var(tab
, isl_tab_undo_nonneg
, &tab
->con
[r
]) < 0)
1514 if (isl_tab_pivot(tab
, r
, i
) < 0)
1516 if (isl_tab_kill_col(tab
, i
) < 0)
1527 /* Check if the given row is a pure constant.
1529 static int is_constant(struct isl_tab
*tab
, int row
)
1531 unsigned off
= 2 + tab
->M
;
1533 return isl_seq_first_non_zero(tab
->mat
->row
[row
] + off
+ tab
->n_dead
,
1534 tab
->n_col
- tab
->n_dead
) == -1;
1537 /* Is the given row a parametric constant?
1538 * That is, does it only involve variables that also appear in the context?
1540 static int is_parametric_constant(struct isl_tab
*tab
, int row
)
1542 unsigned off
= 2 + tab
->M
;
1545 for (col
= tab
->n_dead
; col
< tab
->n_col
; ++col
) {
1546 if (col_is_parameter_var(tab
, col
))
1548 if (isl_int_is_zero(tab
->mat
->row
[row
][off
+ col
]))
1556 /* Add an equality that may or may not be valid to the tableau.
1557 * If the resulting row is a pure constant, then it must be zero.
1558 * Otherwise, the resulting tableau is empty.
1560 * If the row is not a pure constant, then we add two inequalities,
1561 * each time checking that they can be satisfied.
1562 * In the end we try to use one of the two constraints to eliminate
1565 * This function assumes that at least two more rows and at least
1566 * two more elements in the constraint array are available in the tableau.
1568 static int add_lexmin_eq(struct isl_tab
*tab
, isl_int
*eq
) WARN_UNUSED
;
1569 static int add_lexmin_eq(struct isl_tab
*tab
, isl_int
*eq
)
1573 struct isl_tab_undo
*snap
;
1577 snap
= isl_tab_snap(tab
);
1578 r1
= isl_tab_add_row(tab
, eq
);
1581 tab
->con
[r1
].is_nonneg
= 1;
1582 if (isl_tab_push_var(tab
, isl_tab_undo_nonneg
, &tab
->con
[r1
]) < 0)
1585 row
= tab
->con
[r1
].index
;
1586 if (is_constant(tab
, row
)) {
1587 if (!isl_int_is_zero(tab
->mat
->row
[row
][1]) ||
1588 (tab
->M
&& !isl_int_is_zero(tab
->mat
->row
[row
][2]))) {
1589 if (isl_tab_mark_empty(tab
) < 0)
1593 if (isl_tab_rollback(tab
, snap
) < 0)
1598 if (restore_lexmin(tab
) < 0)
1603 isl_seq_neg(eq
, eq
, 1 + tab
->n_var
);
1605 r2
= isl_tab_add_row(tab
, eq
);
1608 tab
->con
[r2
].is_nonneg
= 1;
1609 if (isl_tab_push_var(tab
, isl_tab_undo_nonneg
, &tab
->con
[r2
]) < 0)
1612 if (restore_lexmin(tab
) < 0)
1617 if (!tab
->con
[r1
].is_row
) {
1618 if (isl_tab_kill_col(tab
, tab
->con
[r1
].index
) < 0)
1620 } else if (!tab
->con
[r2
].is_row
) {
1621 if (isl_tab_kill_col(tab
, tab
->con
[r2
].index
) < 0)
1626 tab
->bmap
= isl_basic_map_add_ineq(tab
->bmap
, eq
);
1627 if (isl_tab_push(tab
, isl_tab_undo_bmap_ineq
) < 0)
1629 isl_seq_neg(eq
, eq
, 1 + tab
->n_var
);
1630 tab
->bmap
= isl_basic_map_add_ineq(tab
->bmap
, eq
);
1631 isl_seq_neg(eq
, eq
, 1 + tab
->n_var
);
1632 if (isl_tab_push(tab
, isl_tab_undo_bmap_ineq
) < 0)
1641 /* Add an inequality to the tableau, resolving violations using
1644 * This function assumes that at least one more row and at least
1645 * one more element in the constraint array are available in the tableau.
1647 static struct isl_tab
*add_lexmin_ineq(struct isl_tab
*tab
, isl_int
*ineq
)
1654 tab
->bmap
= isl_basic_map_add_ineq(tab
->bmap
, ineq
);
1655 if (isl_tab_push(tab
, isl_tab_undo_bmap_ineq
) < 0)
1660 r
= isl_tab_add_row(tab
, ineq
);
1663 tab
->con
[r
].is_nonneg
= 1;
1664 if (isl_tab_push_var(tab
, isl_tab_undo_nonneg
, &tab
->con
[r
]) < 0)
1666 if (isl_tab_row_is_redundant(tab
, tab
->con
[r
].index
)) {
1667 if (isl_tab_mark_redundant(tab
, tab
->con
[r
].index
) < 0)
1672 if (restore_lexmin(tab
) < 0)
1674 if (!tab
->empty
&& tab
->con
[r
].is_row
&&
1675 isl_tab_row_is_redundant(tab
, tab
->con
[r
].index
))
1676 if (isl_tab_mark_redundant(tab
, tab
->con
[r
].index
) < 0)
1684 /* Check if the coefficients of the parameters are all integral.
1686 static int integer_parameter(struct isl_tab
*tab
, int row
)
1690 unsigned off
= 2 + tab
->M
;
1692 for (i
= 0; i
< tab
->n_param
; ++i
) {
1693 /* Eliminated parameter */
1694 if (tab
->var
[i
].is_row
)
1696 col
= tab
->var
[i
].index
;
1697 if (!isl_int_is_divisible_by(tab
->mat
->row
[row
][off
+ col
],
1698 tab
->mat
->row
[row
][0]))
1701 for (i
= 0; i
< tab
->n_div
; ++i
) {
1702 if (tab
->var
[tab
->n_var
- tab
->n_div
+ i
].is_row
)
1704 col
= tab
->var
[tab
->n_var
- tab
->n_div
+ i
].index
;
1705 if (!isl_int_is_divisible_by(tab
->mat
->row
[row
][off
+ col
],
1706 tab
->mat
->row
[row
][0]))
1712 /* Check if the coefficients of the non-parameter variables are all integral.
1714 static int integer_variable(struct isl_tab
*tab
, int row
)
1717 unsigned off
= 2 + tab
->M
;
1719 for (i
= tab
->n_dead
; i
< tab
->n_col
; ++i
) {
1720 if (col_is_parameter_var(tab
, i
))
1722 if (!isl_int_is_divisible_by(tab
->mat
->row
[row
][off
+ i
],
1723 tab
->mat
->row
[row
][0]))
1729 /* Check if the constant term is integral.
1731 static int integer_constant(struct isl_tab
*tab
, int row
)
1733 return isl_int_is_divisible_by(tab
->mat
->row
[row
][1],
1734 tab
->mat
->row
[row
][0]);
1737 #define I_CST 1 << 0
1738 #define I_PAR 1 << 1
1739 #define I_VAR 1 << 2
1741 /* Check for next (non-parameter) variable after "var" (first if var == -1)
1742 * that is non-integer and therefore requires a cut and return
1743 * the index of the variable.
1744 * For parametric tableaus, there are three parts in a row,
1745 * the constant, the coefficients of the parameters and the rest.
1746 * For each part, we check whether the coefficients in that part
1747 * are all integral and if so, set the corresponding flag in *f.
1748 * If the constant and the parameter part are integral, then the
1749 * current sample value is integral and no cut is required
1750 * (irrespective of whether the variable part is integral).
1752 static int next_non_integer_var(struct isl_tab
*tab
, int var
, int *f
)
1754 var
= var
< 0 ? tab
->n_param
: var
+ 1;
1756 for (; var
< tab
->n_var
- tab
->n_div
; ++var
) {
1759 if (!tab
->var
[var
].is_row
)
1761 row
= tab
->var
[var
].index
;
1762 if (integer_constant(tab
, row
))
1763 ISL_FL_SET(flags
, I_CST
);
1764 if (integer_parameter(tab
, row
))
1765 ISL_FL_SET(flags
, I_PAR
);
1766 if (ISL_FL_ISSET(flags
, I_CST
) && ISL_FL_ISSET(flags
, I_PAR
))
1768 if (integer_variable(tab
, row
))
1769 ISL_FL_SET(flags
, I_VAR
);
1776 /* Check for first (non-parameter) variable that is non-integer and
1777 * therefore requires a cut and return the corresponding row.
1778 * For parametric tableaus, there are three parts in a row,
1779 * the constant, the coefficients of the parameters and the rest.
1780 * For each part, we check whether the coefficients in that part
1781 * are all integral and if so, set the corresponding flag in *f.
1782 * If the constant and the parameter part are integral, then the
1783 * current sample value is integral and no cut is required
1784 * (irrespective of whether the variable part is integral).
1786 static int first_non_integer_row(struct isl_tab
*tab
, int *f
)
1788 int var
= next_non_integer_var(tab
, -1, f
);
1790 return var
< 0 ? -1 : tab
->var
[var
].index
;
1793 /* Add a (non-parametric) cut to cut away the non-integral sample
1794 * value of the given row.
1796 * If the row is given by
1798 * m r = f + \sum_i a_i y_i
1802 * c = - {-f/m} + \sum_i {a_i/m} y_i >= 0
1804 * The big parameter, if any, is ignored, since it is assumed to be big
1805 * enough to be divisible by any integer.
1806 * If the tableau is actually a parametric tableau, then this function
1807 * is only called when all coefficients of the parameters are integral.
1808 * The cut therefore has zero coefficients for the parameters.
1810 * The current value is known to be negative, so row_sign, if it
1811 * exists, is set accordingly.
1813 * Return the row of the cut or -1.
1815 static int add_cut(struct isl_tab
*tab
, int row
)
1820 unsigned off
= 2 + tab
->M
;
1822 if (isl_tab_extend_cons(tab
, 1) < 0)
1824 r
= isl_tab_allocate_con(tab
);
1828 r_row
= tab
->mat
->row
[tab
->con
[r
].index
];
1829 isl_int_set(r_row
[0], tab
->mat
->row
[row
][0]);
1830 isl_int_neg(r_row
[1], tab
->mat
->row
[row
][1]);
1831 isl_int_fdiv_r(r_row
[1], r_row
[1], tab
->mat
->row
[row
][0]);
1832 isl_int_neg(r_row
[1], r_row
[1]);
1834 isl_int_set_si(r_row
[2], 0);
1835 for (i
= 0; i
< tab
->n_col
; ++i
)
1836 isl_int_fdiv_r(r_row
[off
+ i
],
1837 tab
->mat
->row
[row
][off
+ i
], tab
->mat
->row
[row
][0]);
1839 tab
->con
[r
].is_nonneg
= 1;
1840 if (isl_tab_push_var(tab
, isl_tab_undo_nonneg
, &tab
->con
[r
]) < 0)
1843 tab
->row_sign
[tab
->con
[r
].index
] = isl_tab_row_neg
;
1845 return tab
->con
[r
].index
;
1851 /* Given a non-parametric tableau, add cuts until an integer
1852 * sample point is obtained or until the tableau is determined
1853 * to be integer infeasible.
1854 * As long as there is any non-integer value in the sample point,
1855 * we add appropriate cuts, if possible, for each of these
1856 * non-integer values and then resolve the violated
1857 * cut constraints using restore_lexmin.
1858 * If one of the corresponding rows is equal to an integral
1859 * combination of variables/constraints plus a non-integral constant,
1860 * then there is no way to obtain an integer point and we return
1861 * a tableau that is marked empty.
1862 * The parameter cutting_strategy controls the strategy used when adding cuts
1863 * to remove non-integer points. CUT_ALL adds all possible cuts
1864 * before continuing the search. CUT_ONE adds only one cut at a time.
1866 static struct isl_tab
*cut_to_integer_lexmin(struct isl_tab
*tab
,
1867 int cutting_strategy
)
1878 while ((var
= next_non_integer_var(tab
, -1, &flags
)) != -1) {
1880 if (ISL_FL_ISSET(flags
, I_VAR
)) {
1881 if (isl_tab_mark_empty(tab
) < 0)
1885 row
= tab
->var
[var
].index
;
1886 row
= add_cut(tab
, row
);
1889 if (cutting_strategy
== CUT_ONE
)
1891 } while ((var
= next_non_integer_var(tab
, var
, &flags
)) != -1);
1892 if (restore_lexmin(tab
) < 0)
1903 /* Check whether all the currently active samples also satisfy the inequality
1904 * "ineq" (treated as an equality if eq is set).
1905 * Remove those samples that do not.
1907 static struct isl_tab
*check_samples(struct isl_tab
*tab
, isl_int
*ineq
, int eq
)
1915 isl_assert(tab
->mat
->ctx
, tab
->bmap
, goto error
);
1916 isl_assert(tab
->mat
->ctx
, tab
->samples
, goto error
);
1917 isl_assert(tab
->mat
->ctx
, tab
->samples
->n_col
== 1 + tab
->n_var
, goto error
);
1920 for (i
= tab
->n_outside
; i
< tab
->n_sample
; ++i
) {
1922 isl_seq_inner_product(ineq
, tab
->samples
->row
[i
],
1923 1 + tab
->n_var
, &v
);
1924 sgn
= isl_int_sgn(v
);
1925 if (eq
? (sgn
== 0) : (sgn
>= 0))
1927 tab
= isl_tab_drop_sample(tab
, i
);
1939 /* Check whether the sample value of the tableau is finite,
1940 * i.e., either the tableau does not use a big parameter, or
1941 * all values of the variables are equal to the big parameter plus
1942 * some constant. This constant is the actual sample value.
1944 static int sample_is_finite(struct isl_tab
*tab
)
1951 for (i
= 0; i
< tab
->n_var
; ++i
) {
1953 if (!tab
->var
[i
].is_row
)
1955 row
= tab
->var
[i
].index
;
1956 if (isl_int_ne(tab
->mat
->row
[row
][0], tab
->mat
->row
[row
][2]))
1962 /* Check if the context tableau of sol has any integer points.
1963 * Leave tab in empty state if no integer point can be found.
1964 * If an integer point can be found and if moreover it is finite,
1965 * then it is added to the list of sample values.
1967 * This function is only called when none of the currently active sample
1968 * values satisfies the most recently added constraint.
1970 static struct isl_tab
*check_integer_feasible(struct isl_tab
*tab
)
1972 struct isl_tab_undo
*snap
;
1977 snap
= isl_tab_snap(tab
);
1978 if (isl_tab_push_basis(tab
) < 0)
1981 tab
= cut_to_integer_lexmin(tab
, CUT_ALL
);
1985 if (!tab
->empty
&& sample_is_finite(tab
)) {
1986 struct isl_vec
*sample
;
1988 sample
= isl_tab_get_sample_value(tab
);
1990 if (isl_tab_add_sample(tab
, sample
) < 0)
1994 if (!tab
->empty
&& isl_tab_rollback(tab
, snap
) < 0)
2003 /* Check if any of the currently active sample values satisfies
2004 * the inequality "ineq" (an equality if eq is set).
2006 static int tab_has_valid_sample(struct isl_tab
*tab
, isl_int
*ineq
, int eq
)
2014 isl_assert(tab
->mat
->ctx
, tab
->bmap
, return -1);
2015 isl_assert(tab
->mat
->ctx
, tab
->samples
, return -1);
2016 isl_assert(tab
->mat
->ctx
, tab
->samples
->n_col
== 1 + tab
->n_var
, return -1);
2019 for (i
= tab
->n_outside
; i
< tab
->n_sample
; ++i
) {
2021 isl_seq_inner_product(ineq
, tab
->samples
->row
[i
],
2022 1 + tab
->n_var
, &v
);
2023 sgn
= isl_int_sgn(v
);
2024 if (eq
? (sgn
== 0) : (sgn
>= 0))
2029 return i
< tab
->n_sample
;
2032 /* Insert a div specified by "div" to the tableau "tab" at position "pos" and
2033 * return isl_bool_true if the div is obviously non-negative.
2035 static isl_bool
context_tab_insert_div(struct isl_tab
*tab
, int pos
,
2036 __isl_keep isl_vec
*div
,
2037 isl_stat (*add_ineq
)(void *user
, isl_int
*), void *user
)
2041 struct isl_mat
*samples
;
2044 r
= isl_tab_insert_div(tab
, pos
, div
, add_ineq
, user
);
2046 return isl_bool_error
;
2047 nonneg
= tab
->var
[r
].is_nonneg
;
2048 tab
->var
[r
].frozen
= 1;
2050 samples
= isl_mat_extend(tab
->samples
,
2051 tab
->n_sample
, 1 + tab
->n_var
);
2052 tab
->samples
= samples
;
2054 return isl_bool_error
;
2055 for (i
= tab
->n_outside
; i
< samples
->n_row
; ++i
) {
2056 isl_seq_inner_product(div
->el
+ 1, samples
->row
[i
],
2057 div
->size
- 1, &samples
->row
[i
][samples
->n_col
- 1]);
2058 isl_int_fdiv_q(samples
->row
[i
][samples
->n_col
- 1],
2059 samples
->row
[i
][samples
->n_col
- 1], div
->el
[0]);
2061 tab
->samples
= isl_mat_move_cols(tab
->samples
, 1 + pos
,
2062 1 + tab
->n_var
- 1, 1);
2064 return isl_bool_error
;
2069 /* Add a div specified by "div" to both the main tableau and
2070 * the context tableau. In case of the main tableau, we only
2071 * need to add an extra div. In the context tableau, we also
2072 * need to express the meaning of the div.
2073 * Return the index of the div or -1 if anything went wrong.
2075 * The new integer division is added before any unknown integer
2076 * divisions in the context to ensure that it does not get
2077 * equated to some linear combination involving unknown integer
2080 static int add_div(struct isl_tab
*tab
, struct isl_context
*context
,
2081 __isl_keep isl_vec
*div
)
2086 struct isl_tab
*context_tab
= context
->op
->peek_tab(context
);
2088 if (!tab
|| !context_tab
)
2091 pos
= context_tab
->n_var
- context
->n_unknown
;
2092 if ((nonneg
= context
->op
->insert_div(context
, pos
, div
)) < 0)
2095 if (!context
->op
->is_ok(context
))
2098 pos
= tab
->n_var
- context
->n_unknown
;
2099 if (isl_tab_extend_vars(tab
, 1) < 0)
2101 r
= isl_tab_insert_var(tab
, pos
);
2105 tab
->var
[r
].is_nonneg
= 1;
2106 tab
->var
[r
].frozen
= 1;
2109 return tab
->n_div
- 1 - context
->n_unknown
;
2111 context
->op
->invalidate(context
);
2115 /* Return the position of the integer division that is equal to div/denom
2116 * if there is one. Otherwise, return a position beyond the integer divisions.
2118 static int find_div(struct isl_tab
*tab
, isl_int
*div
, isl_int denom
)
2121 unsigned total
= isl_basic_map_total_dim(tab
->bmap
);
2124 n_div
= isl_basic_map_dim(tab
->bmap
, isl_dim_div
);
2125 for (i
= 0; i
< n_div
; ++i
) {
2126 if (isl_int_ne(tab
->bmap
->div
[i
][0], denom
))
2128 if (!isl_seq_eq(tab
->bmap
->div
[i
] + 1, div
, 1 + total
))
2135 /* Return the index of a div that corresponds to "div".
2136 * We first check if we already have such a div and if not, we create one.
2138 static int get_div(struct isl_tab
*tab
, struct isl_context
*context
,
2139 struct isl_vec
*div
)
2142 struct isl_tab
*context_tab
= context
->op
->peek_tab(context
);
2148 n_div
= isl_basic_map_dim(context_tab
->bmap
, isl_dim_div
);
2149 d
= find_div(context_tab
, div
->el
+ 1, div
->el
[0]);
2155 return add_div(tab
, context
, div
);
2158 /* Add a parametric cut to cut away the non-integral sample value
2160 * Let a_i be the coefficients of the constant term and the parameters
2161 * and let b_i be the coefficients of the variables or constraints
2162 * in basis of the tableau.
2163 * Let q be the div q = floor(\sum_i {-a_i} y_i).
2165 * The cut is expressed as
2167 * c = \sum_i -{-a_i} y_i + \sum_i {b_i} x_i + q >= 0
2169 * If q did not already exist in the context tableau, then it is added first.
2170 * If q is in a column of the main tableau then the "+ q" can be accomplished
2171 * by setting the corresponding entry to the denominator of the constraint.
2172 * If q happens to be in a row of the main tableau, then the corresponding
2173 * row needs to be added instead (taking care of the denominators).
2174 * Note that this is very unlikely, but perhaps not entirely impossible.
2176 * The current value of the cut is known to be negative (or at least
2177 * non-positive), so row_sign is set accordingly.
2179 * Return the row of the cut or -1.
2181 static int add_parametric_cut(struct isl_tab
*tab
, int row
,
2182 struct isl_context
*context
)
2184 struct isl_vec
*div
;
2191 unsigned off
= 2 + tab
->M
;
2196 div
= get_row_parameter_div(tab
, row
);
2200 n
= tab
->n_div
- context
->n_unknown
;
2201 d
= context
->op
->get_div(context
, tab
, div
);
2206 if (isl_tab_extend_cons(tab
, 1) < 0)
2208 r
= isl_tab_allocate_con(tab
);
2212 r_row
= tab
->mat
->row
[tab
->con
[r
].index
];
2213 isl_int_set(r_row
[0], tab
->mat
->row
[row
][0]);
2214 isl_int_neg(r_row
[1], tab
->mat
->row
[row
][1]);
2215 isl_int_fdiv_r(r_row
[1], r_row
[1], tab
->mat
->row
[row
][0]);
2216 isl_int_neg(r_row
[1], r_row
[1]);
2218 isl_int_set_si(r_row
[2], 0);
2219 for (i
= 0; i
< tab
->n_param
; ++i
) {
2220 if (tab
->var
[i
].is_row
)
2222 col
= tab
->var
[i
].index
;
2223 isl_int_neg(r_row
[off
+ col
], tab
->mat
->row
[row
][off
+ col
]);
2224 isl_int_fdiv_r(r_row
[off
+ col
], r_row
[off
+ col
],
2225 tab
->mat
->row
[row
][0]);
2226 isl_int_neg(r_row
[off
+ col
], r_row
[off
+ col
]);
2228 for (i
= 0; i
< tab
->n_div
; ++i
) {
2229 if (tab
->var
[tab
->n_var
- tab
->n_div
+ i
].is_row
)
2231 col
= tab
->var
[tab
->n_var
- tab
->n_div
+ i
].index
;
2232 isl_int_neg(r_row
[off
+ col
], tab
->mat
->row
[row
][off
+ col
]);
2233 isl_int_fdiv_r(r_row
[off
+ col
], r_row
[off
+ col
],
2234 tab
->mat
->row
[row
][0]);
2235 isl_int_neg(r_row
[off
+ col
], r_row
[off
+ col
]);
2237 for (i
= 0; i
< tab
->n_col
; ++i
) {
2238 if (tab
->col_var
[i
] >= 0 &&
2239 (tab
->col_var
[i
] < tab
->n_param
||
2240 tab
->col_var
[i
] >= tab
->n_var
- tab
->n_div
))
2242 isl_int_fdiv_r(r_row
[off
+ i
],
2243 tab
->mat
->row
[row
][off
+ i
], tab
->mat
->row
[row
][0]);
2245 if (tab
->var
[tab
->n_var
- tab
->n_div
+ d
].is_row
) {
2247 int d_row
= tab
->var
[tab
->n_var
- tab
->n_div
+ d
].index
;
2249 isl_int_gcd(gcd
, tab
->mat
->row
[d_row
][0], r_row
[0]);
2250 isl_int_divexact(r_row
[0], r_row
[0], gcd
);
2251 isl_int_divexact(gcd
, tab
->mat
->row
[d_row
][0], gcd
);
2252 isl_seq_combine(r_row
+ 1, gcd
, r_row
+ 1,
2253 r_row
[0], tab
->mat
->row
[d_row
] + 1,
2254 off
- 1 + tab
->n_col
);
2255 isl_int_mul(r_row
[0], r_row
[0], tab
->mat
->row
[d_row
][0]);
2258 col
= tab
->var
[tab
->n_var
- tab
->n_div
+ d
].index
;
2259 isl_int_set(r_row
[off
+ col
], tab
->mat
->row
[row
][0]);
2262 tab
->con
[r
].is_nonneg
= 1;
2263 if (isl_tab_push_var(tab
, isl_tab_undo_nonneg
, &tab
->con
[r
]) < 0)
2266 tab
->row_sign
[tab
->con
[r
].index
] = isl_tab_row_neg
;
2268 row
= tab
->con
[r
].index
;
2270 if (d
>= n
&& context
->op
->detect_equalities(context
, tab
) < 0)
2276 /* Construct a tableau for bmap that can be used for computing
2277 * the lexicographic minimum (or maximum) of bmap.
2278 * If not NULL, then dom is the domain where the minimum
2279 * should be computed. In this case, we set up a parametric
2280 * tableau with row signs (initialized to "unknown").
2281 * If M is set, then the tableau will use a big parameter.
2282 * If max is set, then a maximum should be computed instead of a minimum.
2283 * This means that for each variable x, the tableau will contain the variable
2284 * x' = M - x, rather than x' = M + x. This in turn means that the coefficient
2285 * of the variables in all constraints are negated prior to adding them
2288 static __isl_give
struct isl_tab
*tab_for_lexmin(__isl_keep isl_basic_map
*bmap
,
2289 __isl_keep isl_basic_set
*dom
, unsigned M
, int max
)
2292 struct isl_tab
*tab
;
2296 tab
= isl_tab_alloc(bmap
->ctx
, 2 * bmap
->n_eq
+ bmap
->n_ineq
+ 1,
2297 isl_basic_map_total_dim(bmap
), M
);
2301 tab
->rational
= ISL_F_ISSET(bmap
, ISL_BASIC_MAP_RATIONAL
);
2303 tab
->n_param
= isl_basic_set_total_dim(dom
) - dom
->n_div
;
2304 tab
->n_div
= dom
->n_div
;
2305 tab
->row_sign
= isl_calloc_array(bmap
->ctx
,
2306 enum isl_tab_row_sign
, tab
->mat
->n_row
);
2307 if (tab
->mat
->n_row
&& !tab
->row_sign
)
2310 if (ISL_F_ISSET(bmap
, ISL_BASIC_MAP_EMPTY
)) {
2311 if (isl_tab_mark_empty(tab
) < 0)
2316 for (i
= tab
->n_param
; i
< tab
->n_var
- tab
->n_div
; ++i
) {
2317 tab
->var
[i
].is_nonneg
= 1;
2318 tab
->var
[i
].frozen
= 1;
2320 o_var
= 1 + tab
->n_param
;
2321 n_var
= tab
->n_var
- tab
->n_param
- tab
->n_div
;
2322 for (i
= 0; i
< bmap
->n_eq
; ++i
) {
2324 isl_seq_neg(bmap
->eq
[i
] + o_var
,
2325 bmap
->eq
[i
] + o_var
, n_var
);
2326 tab
= add_lexmin_valid_eq(tab
, bmap
->eq
[i
]);
2328 isl_seq_neg(bmap
->eq
[i
] + o_var
,
2329 bmap
->eq
[i
] + o_var
, n_var
);
2330 if (!tab
|| tab
->empty
)
2333 if (bmap
->n_eq
&& restore_lexmin(tab
) < 0)
2335 for (i
= 0; i
< bmap
->n_ineq
; ++i
) {
2337 isl_seq_neg(bmap
->ineq
[i
] + o_var
,
2338 bmap
->ineq
[i
] + o_var
, n_var
);
2339 tab
= add_lexmin_ineq(tab
, bmap
->ineq
[i
]);
2341 isl_seq_neg(bmap
->ineq
[i
] + o_var
,
2342 bmap
->ineq
[i
] + o_var
, n_var
);
2343 if (!tab
|| tab
->empty
)
2352 /* Given a main tableau where more than one row requires a split,
2353 * determine and return the "best" row to split on.
2355 * If any of the rows requiring a split only involves
2356 * variables that also appear in the context tableau,
2357 * then the negative part is guaranteed not to have a solution.
2358 * It is therefore best to split on any of these rows first.
2361 * given two rows in the main tableau, if the inequality corresponding
2362 * to the first row is redundant with respect to that of the second row
2363 * in the current tableau, then it is better to split on the second row,
2364 * since in the positive part, both rows will be positive.
2365 * (In the negative part a pivot will have to be performed and just about
2366 * anything can happen to the sign of the other row.)
2368 * As a simple heuristic, we therefore select the row that makes the most
2369 * of the other rows redundant.
2371 * Perhaps it would also be useful to look at the number of constraints
2372 * that conflict with any given constraint.
2374 * best is the best row so far (-1 when we have not found any row yet).
2375 * best_r is the number of other rows made redundant by row best.
2376 * When best is still -1, bset_r is meaningless, but it is initialized
2377 * to some arbitrary value (0) anyway. Without this redundant initialization
2378 * valgrind may warn about uninitialized memory accesses when isl
2379 * is compiled with some versions of gcc.
2381 static int best_split(struct isl_tab
*tab
, struct isl_tab
*context_tab
)
2383 struct isl_tab_undo
*snap
;
2389 if (isl_tab_extend_cons(context_tab
, 2) < 0)
2392 snap
= isl_tab_snap(context_tab
);
2394 for (split
= tab
->n_redundant
; split
< tab
->n_row
; ++split
) {
2395 struct isl_tab_undo
*snap2
;
2396 struct isl_vec
*ineq
= NULL
;
2400 if (!isl_tab_var_from_row(tab
, split
)->is_nonneg
)
2402 if (tab
->row_sign
[split
] != isl_tab_row_any
)
2405 if (is_parametric_constant(tab
, split
))
2408 ineq
= get_row_parameter_ineq(tab
, split
);
2411 ok
= isl_tab_add_ineq(context_tab
, ineq
->el
) >= 0;
2416 snap2
= isl_tab_snap(context_tab
);
2418 for (row
= tab
->n_redundant
; row
< tab
->n_row
; ++row
) {
2419 struct isl_tab_var
*var
;
2423 if (!isl_tab_var_from_row(tab
, row
)->is_nonneg
)
2425 if (tab
->row_sign
[row
] != isl_tab_row_any
)
2428 ineq
= get_row_parameter_ineq(tab
, row
);
2431 ok
= isl_tab_add_ineq(context_tab
, ineq
->el
) >= 0;
2435 var
= &context_tab
->con
[context_tab
->n_con
- 1];
2436 if (!context_tab
->empty
&&
2437 !isl_tab_min_at_most_neg_one(context_tab
, var
))
2439 if (isl_tab_rollback(context_tab
, snap2
) < 0)
2442 if (best
== -1 || r
> best_r
) {
2446 if (isl_tab_rollback(context_tab
, snap
) < 0)
2453 static struct isl_basic_set
*context_lex_peek_basic_set(
2454 struct isl_context
*context
)
2456 struct isl_context_lex
*clex
= (struct isl_context_lex
*)context
;
2459 return isl_tab_peek_bset(clex
->tab
);
2462 static struct isl_tab
*context_lex_peek_tab(struct isl_context
*context
)
2464 struct isl_context_lex
*clex
= (struct isl_context_lex
*)context
;
2468 static void context_lex_add_eq(struct isl_context
*context
, isl_int
*eq
,
2469 int check
, int update
)
2471 struct isl_context_lex
*clex
= (struct isl_context_lex
*)context
;
2472 if (isl_tab_extend_cons(clex
->tab
, 2) < 0)
2474 if (add_lexmin_eq(clex
->tab
, eq
) < 0)
2477 int v
= tab_has_valid_sample(clex
->tab
, eq
, 1);
2481 clex
->tab
= check_integer_feasible(clex
->tab
);
2484 clex
->tab
= check_samples(clex
->tab
, eq
, 1);
2487 isl_tab_free(clex
->tab
);
2491 static void context_lex_add_ineq(struct isl_context
*context
, isl_int
*ineq
,
2492 int check
, int update
)
2494 struct isl_context_lex
*clex
= (struct isl_context_lex
*)context
;
2495 if (isl_tab_extend_cons(clex
->tab
, 1) < 0)
2497 clex
->tab
= add_lexmin_ineq(clex
->tab
, ineq
);
2499 int v
= tab_has_valid_sample(clex
->tab
, ineq
, 0);
2503 clex
->tab
= check_integer_feasible(clex
->tab
);
2506 clex
->tab
= check_samples(clex
->tab
, ineq
, 0);
2509 isl_tab_free(clex
->tab
);
2513 static isl_stat
context_lex_add_ineq_wrap(void *user
, isl_int
*ineq
)
2515 struct isl_context
*context
= (struct isl_context
*)user
;
2516 context_lex_add_ineq(context
, ineq
, 0, 0);
2517 return context
->op
->is_ok(context
) ? isl_stat_ok
: isl_stat_error
;
2520 /* Check which signs can be obtained by "ineq" on all the currently
2521 * active sample values. See row_sign for more information.
2523 static enum isl_tab_row_sign
tab_ineq_sign(struct isl_tab
*tab
, isl_int
*ineq
,
2529 enum isl_tab_row_sign res
= isl_tab_row_unknown
;
2531 isl_assert(tab
->mat
->ctx
, tab
->samples
, return isl_tab_row_unknown
);
2532 isl_assert(tab
->mat
->ctx
, tab
->samples
->n_col
== 1 + tab
->n_var
,
2533 return isl_tab_row_unknown
);
2536 for (i
= tab
->n_outside
; i
< tab
->n_sample
; ++i
) {
2537 isl_seq_inner_product(tab
->samples
->row
[i
], ineq
,
2538 1 + tab
->n_var
, &tmp
);
2539 sgn
= isl_int_sgn(tmp
);
2540 if (sgn
> 0 || (sgn
== 0 && strict
)) {
2541 if (res
== isl_tab_row_unknown
)
2542 res
= isl_tab_row_pos
;
2543 if (res
== isl_tab_row_neg
)
2544 res
= isl_tab_row_any
;
2547 if (res
== isl_tab_row_unknown
)
2548 res
= isl_tab_row_neg
;
2549 if (res
== isl_tab_row_pos
)
2550 res
= isl_tab_row_any
;
2552 if (res
== isl_tab_row_any
)
2560 static enum isl_tab_row_sign
context_lex_ineq_sign(struct isl_context
*context
,
2561 isl_int
*ineq
, int strict
)
2563 struct isl_context_lex
*clex
= (struct isl_context_lex
*)context
;
2564 return tab_ineq_sign(clex
->tab
, ineq
, strict
);
2567 /* Check whether "ineq" can be added to the tableau without rendering
2570 static int context_lex_test_ineq(struct isl_context
*context
, isl_int
*ineq
)
2572 struct isl_context_lex
*clex
= (struct isl_context_lex
*)context
;
2573 struct isl_tab_undo
*snap
;
2579 if (isl_tab_extend_cons(clex
->tab
, 1) < 0)
2582 snap
= isl_tab_snap(clex
->tab
);
2583 if (isl_tab_push_basis(clex
->tab
) < 0)
2585 clex
->tab
= add_lexmin_ineq(clex
->tab
, ineq
);
2586 clex
->tab
= check_integer_feasible(clex
->tab
);
2589 feasible
= !clex
->tab
->empty
;
2590 if (isl_tab_rollback(clex
->tab
, snap
) < 0)
2596 static int context_lex_get_div(struct isl_context
*context
, struct isl_tab
*tab
,
2597 struct isl_vec
*div
)
2599 return get_div(tab
, context
, div
);
2602 /* Insert a div specified by "div" to the context tableau at position "pos" and
2603 * return isl_bool_true if the div is obviously non-negative.
2604 * context_tab_add_div will always return isl_bool_true, because all variables
2605 * in a isl_context_lex tableau are non-negative.
2606 * However, if we are using a big parameter in the context, then this only
2607 * reflects the non-negativity of the variable used to _encode_ the
2608 * div, i.e., div' = M + div, so we can't draw any conclusions.
2610 static isl_bool
context_lex_insert_div(struct isl_context
*context
, int pos
,
2611 __isl_keep isl_vec
*div
)
2613 struct isl_context_lex
*clex
= (struct isl_context_lex
*)context
;
2615 nonneg
= context_tab_insert_div(clex
->tab
, pos
, div
,
2616 context_lex_add_ineq_wrap
, context
);
2618 return isl_bool_error
;
2620 return isl_bool_false
;
2624 static int context_lex_detect_equalities(struct isl_context
*context
,
2625 struct isl_tab
*tab
)
2630 static int context_lex_best_split(struct isl_context
*context
,
2631 struct isl_tab
*tab
)
2633 struct isl_context_lex
*clex
= (struct isl_context_lex
*)context
;
2634 struct isl_tab_undo
*snap
;
2637 snap
= isl_tab_snap(clex
->tab
);
2638 if (isl_tab_push_basis(clex
->tab
) < 0)
2640 r
= best_split(tab
, clex
->tab
);
2642 if (r
>= 0 && isl_tab_rollback(clex
->tab
, snap
) < 0)
2648 static int context_lex_is_empty(struct isl_context
*context
)
2650 struct isl_context_lex
*clex
= (struct isl_context_lex
*)context
;
2653 return clex
->tab
->empty
;
2656 static void *context_lex_save(struct isl_context
*context
)
2658 struct isl_context_lex
*clex
= (struct isl_context_lex
*)context
;
2659 struct isl_tab_undo
*snap
;
2661 snap
= isl_tab_snap(clex
->tab
);
2662 if (isl_tab_push_basis(clex
->tab
) < 0)
2664 if (isl_tab_save_samples(clex
->tab
) < 0)
2670 static void context_lex_restore(struct isl_context
*context
, void *save
)
2672 struct isl_context_lex
*clex
= (struct isl_context_lex
*)context
;
2673 if (isl_tab_rollback(clex
->tab
, (struct isl_tab_undo
*)save
) < 0) {
2674 isl_tab_free(clex
->tab
);
2679 static void context_lex_discard(void *save
)
2683 static int context_lex_is_ok(struct isl_context
*context
)
2685 struct isl_context_lex
*clex
= (struct isl_context_lex
*)context
;
2689 /* For each variable in the context tableau, check if the variable can
2690 * only attain non-negative values. If so, mark the parameter as non-negative
2691 * in the main tableau. This allows for a more direct identification of some
2692 * cases of violated constraints.
2694 static struct isl_tab
*tab_detect_nonnegative_parameters(struct isl_tab
*tab
,
2695 struct isl_tab
*context_tab
)
2698 struct isl_tab_undo
*snap
;
2699 struct isl_vec
*ineq
= NULL
;
2700 struct isl_tab_var
*var
;
2703 if (context_tab
->n_var
== 0)
2706 ineq
= isl_vec_alloc(tab
->mat
->ctx
, 1 + context_tab
->n_var
);
2710 if (isl_tab_extend_cons(context_tab
, 1) < 0)
2713 snap
= isl_tab_snap(context_tab
);
2716 isl_seq_clr(ineq
->el
, ineq
->size
);
2717 for (i
= 0; i
< context_tab
->n_var
; ++i
) {
2718 isl_int_set_si(ineq
->el
[1 + i
], 1);
2719 if (isl_tab_add_ineq(context_tab
, ineq
->el
) < 0)
2721 var
= &context_tab
->con
[context_tab
->n_con
- 1];
2722 if (!context_tab
->empty
&&
2723 !isl_tab_min_at_most_neg_one(context_tab
, var
)) {
2725 if (i
>= tab
->n_param
)
2726 j
= i
- tab
->n_param
+ tab
->n_var
- tab
->n_div
;
2727 tab
->var
[j
].is_nonneg
= 1;
2730 isl_int_set_si(ineq
->el
[1 + i
], 0);
2731 if (isl_tab_rollback(context_tab
, snap
) < 0)
2735 if (context_tab
->M
&& n
== context_tab
->n_var
) {
2736 context_tab
->mat
= isl_mat_drop_cols(context_tab
->mat
, 2, 1);
2748 static struct isl_tab
*context_lex_detect_nonnegative_parameters(
2749 struct isl_context
*context
, struct isl_tab
*tab
)
2751 struct isl_context_lex
*clex
= (struct isl_context_lex
*)context
;
2752 struct isl_tab_undo
*snap
;
2757 snap
= isl_tab_snap(clex
->tab
);
2758 if (isl_tab_push_basis(clex
->tab
) < 0)
2761 tab
= tab_detect_nonnegative_parameters(tab
, clex
->tab
);
2763 if (isl_tab_rollback(clex
->tab
, snap
) < 0)
2772 static void context_lex_invalidate(struct isl_context
*context
)
2774 struct isl_context_lex
*clex
= (struct isl_context_lex
*)context
;
2775 isl_tab_free(clex
->tab
);
2779 static __isl_null
struct isl_context
*context_lex_free(
2780 struct isl_context
*context
)
2782 struct isl_context_lex
*clex
= (struct isl_context_lex
*)context
;
2783 isl_tab_free(clex
->tab
);
2789 struct isl_context_op isl_context_lex_op
= {
2790 context_lex_detect_nonnegative_parameters
,
2791 context_lex_peek_basic_set
,
2792 context_lex_peek_tab
,
2794 context_lex_add_ineq
,
2795 context_lex_ineq_sign
,
2796 context_lex_test_ineq
,
2797 context_lex_get_div
,
2798 context_lex_insert_div
,
2799 context_lex_detect_equalities
,
2800 context_lex_best_split
,
2801 context_lex_is_empty
,
2804 context_lex_restore
,
2805 context_lex_discard
,
2806 context_lex_invalidate
,
2810 static struct isl_tab
*context_tab_for_lexmin(__isl_take isl_basic_set
*bset
)
2812 struct isl_tab
*tab
;
2816 tab
= tab_for_lexmin(bset_to_bmap(bset
), NULL
, 1, 0);
2817 if (isl_tab_track_bset(tab
, bset
) < 0)
2819 tab
= isl_tab_init_samples(tab
);
2826 static struct isl_context
*isl_context_lex_alloc(struct isl_basic_set
*dom
)
2828 struct isl_context_lex
*clex
;
2833 clex
= isl_alloc_type(dom
->ctx
, struct isl_context_lex
);
2837 clex
->context
.op
= &isl_context_lex_op
;
2839 clex
->tab
= context_tab_for_lexmin(isl_basic_set_copy(dom
));
2840 if (restore_lexmin(clex
->tab
) < 0)
2842 clex
->tab
= check_integer_feasible(clex
->tab
);
2846 return &clex
->context
;
2848 clex
->context
.op
->free(&clex
->context
);
2852 /* Representation of the context when using generalized basis reduction.
2854 * "shifted" contains the offsets of the unit hypercubes that lie inside the
2855 * context. Any rational point in "shifted" can therefore be rounded
2856 * up to an integer point in the context.
2857 * If the context is constrained by any equality, then "shifted" is not used
2858 * as it would be empty.
2860 struct isl_context_gbr
{
2861 struct isl_context context
;
2862 struct isl_tab
*tab
;
2863 struct isl_tab
*shifted
;
2864 struct isl_tab
*cone
;
2867 static struct isl_tab
*context_gbr_detect_nonnegative_parameters(
2868 struct isl_context
*context
, struct isl_tab
*tab
)
2870 struct isl_context_gbr
*cgbr
= (struct isl_context_gbr
*)context
;
2873 return tab_detect_nonnegative_parameters(tab
, cgbr
->tab
);
2876 static struct isl_basic_set
*context_gbr_peek_basic_set(
2877 struct isl_context
*context
)
2879 struct isl_context_gbr
*cgbr
= (struct isl_context_gbr
*)context
;
2882 return isl_tab_peek_bset(cgbr
->tab
);
2885 static struct isl_tab
*context_gbr_peek_tab(struct isl_context
*context
)
2887 struct isl_context_gbr
*cgbr
= (struct isl_context_gbr
*)context
;
2891 /* Initialize the "shifted" tableau of the context, which
2892 * contains the constraints of the original tableau shifted
2893 * by the sum of all negative coefficients. This ensures
2894 * that any rational point in the shifted tableau can
2895 * be rounded up to yield an integer point in the original tableau.
2897 static void gbr_init_shifted(struct isl_context_gbr
*cgbr
)
2900 struct isl_vec
*cst
;
2901 struct isl_basic_set
*bset
= isl_tab_peek_bset(cgbr
->tab
);
2902 unsigned dim
= isl_basic_set_total_dim(bset
);
2904 cst
= isl_vec_alloc(cgbr
->tab
->mat
->ctx
, bset
->n_ineq
);
2908 for (i
= 0; i
< bset
->n_ineq
; ++i
) {
2909 isl_int_set(cst
->el
[i
], bset
->ineq
[i
][0]);
2910 for (j
= 0; j
< dim
; ++j
) {
2911 if (!isl_int_is_neg(bset
->ineq
[i
][1 + j
]))
2913 isl_int_add(bset
->ineq
[i
][0], bset
->ineq
[i
][0],
2914 bset
->ineq
[i
][1 + j
]);
2918 cgbr
->shifted
= isl_tab_from_basic_set(bset
, 0);
2920 for (i
= 0; i
< bset
->n_ineq
; ++i
)
2921 isl_int_set(bset
->ineq
[i
][0], cst
->el
[i
]);
2926 /* Check if the shifted tableau is non-empty, and if so
2927 * use the sample point to construct an integer point
2928 * of the context tableau.
2930 static struct isl_vec
*gbr_get_shifted_sample(struct isl_context_gbr
*cgbr
)
2932 struct isl_vec
*sample
;
2935 gbr_init_shifted(cgbr
);
2938 if (cgbr
->shifted
->empty
)
2939 return isl_vec_alloc(cgbr
->tab
->mat
->ctx
, 0);
2941 sample
= isl_tab_get_sample_value(cgbr
->shifted
);
2942 sample
= isl_vec_ceil(sample
);
2947 static __isl_give isl_basic_set
*drop_constant_terms(
2948 __isl_take isl_basic_set
*bset
)
2955 for (i
= 0; i
< bset
->n_eq
; ++i
)
2956 isl_int_set_si(bset
->eq
[i
][0], 0);
2958 for (i
= 0; i
< bset
->n_ineq
; ++i
)
2959 isl_int_set_si(bset
->ineq
[i
][0], 0);
2964 static int use_shifted(struct isl_context_gbr
*cgbr
)
2968 return cgbr
->tab
->bmap
->n_eq
== 0 && cgbr
->tab
->bmap
->n_div
== 0;
2971 static struct isl_vec
*gbr_get_sample(struct isl_context_gbr
*cgbr
)
2973 struct isl_basic_set
*bset
;
2974 struct isl_basic_set
*cone
;
2976 if (isl_tab_sample_is_integer(cgbr
->tab
))
2977 return isl_tab_get_sample_value(cgbr
->tab
);
2979 if (use_shifted(cgbr
)) {
2980 struct isl_vec
*sample
;
2982 sample
= gbr_get_shifted_sample(cgbr
);
2983 if (!sample
|| sample
->size
> 0)
2986 isl_vec_free(sample
);
2990 bset
= isl_tab_peek_bset(cgbr
->tab
);
2991 cgbr
->cone
= isl_tab_from_recession_cone(bset
, 0);
2994 if (isl_tab_track_bset(cgbr
->cone
,
2995 isl_basic_set_copy(bset
)) < 0)
2998 if (isl_tab_detect_implicit_equalities(cgbr
->cone
) < 0)
3001 if (cgbr
->cone
->n_dead
== cgbr
->cone
->n_col
) {
3002 struct isl_vec
*sample
;
3003 struct isl_tab_undo
*snap
;
3005 if (cgbr
->tab
->basis
) {
3006 if (cgbr
->tab
->basis
->n_col
!= 1 + cgbr
->tab
->n_var
) {
3007 isl_mat_free(cgbr
->tab
->basis
);
3008 cgbr
->tab
->basis
= NULL
;
3010 cgbr
->tab
->n_zero
= 0;
3011 cgbr
->tab
->n_unbounded
= 0;
3014 snap
= isl_tab_snap(cgbr
->tab
);
3016 sample
= isl_tab_sample(cgbr
->tab
);
3018 if (!sample
|| isl_tab_rollback(cgbr
->tab
, snap
) < 0) {
3019 isl_vec_free(sample
);
3026 cone
= isl_basic_set_dup(isl_tab_peek_bset(cgbr
->cone
));
3027 cone
= drop_constant_terms(cone
);
3028 cone
= isl_basic_set_update_from_tab(cone
, cgbr
->cone
);
3029 cone
= isl_basic_set_underlying_set(cone
);
3030 cone
= isl_basic_set_gauss(cone
, NULL
);
3032 bset
= isl_basic_set_dup(isl_tab_peek_bset(cgbr
->tab
));
3033 bset
= isl_basic_set_update_from_tab(bset
, cgbr
->tab
);
3034 bset
= isl_basic_set_underlying_set(bset
);
3035 bset
= isl_basic_set_gauss(bset
, NULL
);
3037 return isl_basic_set_sample_with_cone(bset
, cone
);
3040 static void check_gbr_integer_feasible(struct isl_context_gbr
*cgbr
)
3042 struct isl_vec
*sample
;
3047 if (cgbr
->tab
->empty
)
3050 sample
= gbr_get_sample(cgbr
);
3054 if (sample
->size
== 0) {
3055 isl_vec_free(sample
);
3056 if (isl_tab_mark_empty(cgbr
->tab
) < 0)
3061 if (isl_tab_add_sample(cgbr
->tab
, sample
) < 0)
3066 isl_tab_free(cgbr
->tab
);
3070 static struct isl_tab
*add_gbr_eq(struct isl_tab
*tab
, isl_int
*eq
)
3075 if (isl_tab_extend_cons(tab
, 2) < 0)
3078 if (isl_tab_add_eq(tab
, eq
) < 0)
3087 /* Add the equality described by "eq" to the context.
3088 * If "check" is set, then we check if the context is empty after
3089 * adding the equality.
3090 * If "update" is set, then we check if the samples are still valid.
3092 * We do not explicitly add shifted copies of the equality to
3093 * cgbr->shifted since they would conflict with each other.
3094 * Instead, we directly mark cgbr->shifted empty.
3096 static void context_gbr_add_eq(struct isl_context
*context
, isl_int
*eq
,
3097 int check
, int update
)
3099 struct isl_context_gbr
*cgbr
= (struct isl_context_gbr
*)context
;
3101 cgbr
->tab
= add_gbr_eq(cgbr
->tab
, eq
);
3103 if (cgbr
->shifted
&& !cgbr
->shifted
->empty
&& use_shifted(cgbr
)) {
3104 if (isl_tab_mark_empty(cgbr
->shifted
) < 0)
3108 if (cgbr
->cone
&& cgbr
->cone
->n_col
!= cgbr
->cone
->n_dead
) {
3109 if (isl_tab_extend_cons(cgbr
->cone
, 2) < 0)
3111 if (isl_tab_add_eq(cgbr
->cone
, eq
) < 0)
3116 int v
= tab_has_valid_sample(cgbr
->tab
, eq
, 1);
3120 check_gbr_integer_feasible(cgbr
);
3123 cgbr
->tab
= check_samples(cgbr
->tab
, eq
, 1);
3126 isl_tab_free(cgbr
->tab
);
3130 static void add_gbr_ineq(struct isl_context_gbr
*cgbr
, isl_int
*ineq
)
3135 if (isl_tab_extend_cons(cgbr
->tab
, 1) < 0)
3138 if (isl_tab_add_ineq(cgbr
->tab
, ineq
) < 0)
3141 if (cgbr
->shifted
&& !cgbr
->shifted
->empty
&& use_shifted(cgbr
)) {
3144 dim
= isl_basic_map_total_dim(cgbr
->tab
->bmap
);
3146 if (isl_tab_extend_cons(cgbr
->shifted
, 1) < 0)
3149 for (i
= 0; i
< dim
; ++i
) {
3150 if (!isl_int_is_neg(ineq
[1 + i
]))
3152 isl_int_add(ineq
[0], ineq
[0], ineq
[1 + i
]);
3155 if (isl_tab_add_ineq(cgbr
->shifted
, ineq
) < 0)
3158 for (i
= 0; i
< dim
; ++i
) {
3159 if (!isl_int_is_neg(ineq
[1 + i
]))
3161 isl_int_sub(ineq
[0], ineq
[0], ineq
[1 + i
]);
3165 if (cgbr
->cone
&& cgbr
->cone
->n_col
!= cgbr
->cone
->n_dead
) {
3166 if (isl_tab_extend_cons(cgbr
->cone
, 1) < 0)
3168 if (isl_tab_add_ineq(cgbr
->cone
, ineq
) < 0)
3174 isl_tab_free(cgbr
->tab
);
3178 static void context_gbr_add_ineq(struct isl_context
*context
, isl_int
*ineq
,
3179 int check
, int update
)
3181 struct isl_context_gbr
*cgbr
= (struct isl_context_gbr
*)context
;
3183 add_gbr_ineq(cgbr
, ineq
);
3188 int v
= tab_has_valid_sample(cgbr
->tab
, ineq
, 0);
3192 check_gbr_integer_feasible(cgbr
);
3195 cgbr
->tab
= check_samples(cgbr
->tab
, ineq
, 0);
3198 isl_tab_free(cgbr
->tab
);
3202 static isl_stat
context_gbr_add_ineq_wrap(void *user
, isl_int
*ineq
)
3204 struct isl_context
*context
= (struct isl_context
*)user
;
3205 context_gbr_add_ineq(context
, ineq
, 0, 0);
3206 return context
->op
->is_ok(context
) ? isl_stat_ok
: isl_stat_error
;
3209 static enum isl_tab_row_sign
context_gbr_ineq_sign(struct isl_context
*context
,
3210 isl_int
*ineq
, int strict
)
3212 struct isl_context_gbr
*cgbr
= (struct isl_context_gbr
*)context
;
3213 return tab_ineq_sign(cgbr
->tab
, ineq
, strict
);
3216 /* Check whether "ineq" can be added to the tableau without rendering
3219 static int context_gbr_test_ineq(struct isl_context
*context
, isl_int
*ineq
)
3221 struct isl_context_gbr
*cgbr
= (struct isl_context_gbr
*)context
;
3222 struct isl_tab_undo
*snap
;
3223 struct isl_tab_undo
*shifted_snap
= NULL
;
3224 struct isl_tab_undo
*cone_snap
= NULL
;
3230 if (isl_tab_extend_cons(cgbr
->tab
, 1) < 0)
3233 snap
= isl_tab_snap(cgbr
->tab
);
3235 shifted_snap
= isl_tab_snap(cgbr
->shifted
);
3237 cone_snap
= isl_tab_snap(cgbr
->cone
);
3238 add_gbr_ineq(cgbr
, ineq
);
3239 check_gbr_integer_feasible(cgbr
);
3242 feasible
= !cgbr
->tab
->empty
;
3243 if (isl_tab_rollback(cgbr
->tab
, snap
) < 0)
3246 if (isl_tab_rollback(cgbr
->shifted
, shifted_snap
))
3248 } else if (cgbr
->shifted
) {
3249 isl_tab_free(cgbr
->shifted
);
3250 cgbr
->shifted
= NULL
;
3253 if (isl_tab_rollback(cgbr
->cone
, cone_snap
))
3255 } else if (cgbr
->cone
) {
3256 isl_tab_free(cgbr
->cone
);
3263 /* Return the column of the last of the variables associated to
3264 * a column that has a non-zero coefficient.
3265 * This function is called in a context where only coefficients
3266 * of parameters or divs can be non-zero.
3268 static int last_non_zero_var_col(struct isl_tab
*tab
, isl_int
*p
)
3273 if (tab
->n_var
== 0)
3276 for (i
= tab
->n_var
- 1; i
>= 0; --i
) {
3277 if (i
>= tab
->n_param
&& i
< tab
->n_var
- tab
->n_div
)
3279 if (tab
->var
[i
].is_row
)
3281 col
= tab
->var
[i
].index
;
3282 if (!isl_int_is_zero(p
[col
]))
3289 /* Look through all the recently added equalities in the context
3290 * to see if we can propagate any of them to the main tableau.
3292 * The newly added equalities in the context are encoded as pairs
3293 * of inequalities starting at inequality "first".
3295 * We tentatively add each of these equalities to the main tableau
3296 * and if this happens to result in a row with a final coefficient
3297 * that is one or negative one, we use it to kill a column
3298 * in the main tableau. Otherwise, we discard the tentatively
3300 * This tentative addition of equality constraints turns
3301 * on the undo facility of the tableau. Turn it off again
3302 * at the end, assuming it was turned off to begin with.
3304 * Return 0 on success and -1 on failure.
3306 static int propagate_equalities(struct isl_context_gbr
*cgbr
,
3307 struct isl_tab
*tab
, unsigned first
)
3310 struct isl_vec
*eq
= NULL
;
3311 isl_bool needs_undo
;
3313 needs_undo
= isl_tab_need_undo(tab
);
3316 eq
= isl_vec_alloc(tab
->mat
->ctx
, 1 + tab
->n_var
);
3320 if (isl_tab_extend_cons(tab
, (cgbr
->tab
->bmap
->n_ineq
- first
)/2) < 0)
3323 isl_seq_clr(eq
->el
+ 1 + tab
->n_param
,
3324 tab
->n_var
- tab
->n_param
- tab
->n_div
);
3325 for (i
= first
; i
< cgbr
->tab
->bmap
->n_ineq
; i
+= 2) {
3328 struct isl_tab_undo
*snap
;
3329 snap
= isl_tab_snap(tab
);
3331 isl_seq_cpy(eq
->el
, cgbr
->tab
->bmap
->ineq
[i
], 1 + tab
->n_param
);
3332 isl_seq_cpy(eq
->el
+ 1 + tab
->n_var
- tab
->n_div
,
3333 cgbr
->tab
->bmap
->ineq
[i
] + 1 + tab
->n_param
,
3336 r
= isl_tab_add_row(tab
, eq
->el
);
3339 r
= tab
->con
[r
].index
;
3340 j
= last_non_zero_var_col(tab
, tab
->mat
->row
[r
] + 2 + tab
->M
);
3341 if (j
< 0 || j
< tab
->n_dead
||
3342 !isl_int_is_one(tab
->mat
->row
[r
][0]) ||
3343 (!isl_int_is_one(tab
->mat
->row
[r
][2 + tab
->M
+ j
]) &&
3344 !isl_int_is_negone(tab
->mat
->row
[r
][2 + tab
->M
+ j
]))) {
3345 if (isl_tab_rollback(tab
, snap
) < 0)
3349 if (isl_tab_pivot(tab
, r
, j
) < 0)
3351 if (isl_tab_kill_col(tab
, j
) < 0)
3354 if (restore_lexmin(tab
) < 0)
3359 isl_tab_clear_undo(tab
);
3365 isl_tab_free(cgbr
->tab
);
3370 static int context_gbr_detect_equalities(struct isl_context
*context
,
3371 struct isl_tab
*tab
)
3373 struct isl_context_gbr
*cgbr
= (struct isl_context_gbr
*)context
;
3377 struct isl_basic_set
*bset
= isl_tab_peek_bset(cgbr
->tab
);
3378 cgbr
->cone
= isl_tab_from_recession_cone(bset
, 0);
3381 if (isl_tab_track_bset(cgbr
->cone
,
3382 isl_basic_set_copy(bset
)) < 0)
3385 if (isl_tab_detect_implicit_equalities(cgbr
->cone
) < 0)
3388 n_ineq
= cgbr
->tab
->bmap
->n_ineq
;
3389 cgbr
->tab
= isl_tab_detect_equalities(cgbr
->tab
, cgbr
->cone
);
3392 if (cgbr
->tab
->bmap
->n_ineq
> n_ineq
&&
3393 propagate_equalities(cgbr
, tab
, n_ineq
) < 0)
3398 isl_tab_free(cgbr
->tab
);
3403 static int context_gbr_get_div(struct isl_context
*context
, struct isl_tab
*tab
,
3404 struct isl_vec
*div
)
3406 return get_div(tab
, context
, div
);
3409 static isl_bool
context_gbr_insert_div(struct isl_context
*context
, int pos
,
3410 __isl_keep isl_vec
*div
)
3412 struct isl_context_gbr
*cgbr
= (struct isl_context_gbr
*)context
;
3414 int r
, n_div
, o_div
;
3416 n_div
= isl_basic_map_dim(cgbr
->cone
->bmap
, isl_dim_div
);
3417 o_div
= cgbr
->cone
->n_var
- n_div
;
3419 if (isl_tab_extend_cons(cgbr
->cone
, 3) < 0)
3420 return isl_bool_error
;
3421 if (isl_tab_extend_vars(cgbr
->cone
, 1) < 0)
3422 return isl_bool_error
;
3423 if ((r
= isl_tab_insert_var(cgbr
->cone
, pos
)) <0)
3424 return isl_bool_error
;
3426 cgbr
->cone
->bmap
= isl_basic_map_insert_div(cgbr
->cone
->bmap
,
3428 if (!cgbr
->cone
->bmap
)
3429 return isl_bool_error
;
3430 if (isl_tab_push_var(cgbr
->cone
, isl_tab_undo_bmap_div
,
3431 &cgbr
->cone
->var
[r
]) < 0)
3432 return isl_bool_error
;
3434 return context_tab_insert_div(cgbr
->tab
, pos
, div
,
3435 context_gbr_add_ineq_wrap
, context
);
3438 static int context_gbr_best_split(struct isl_context
*context
,
3439 struct isl_tab
*tab
)
3441 struct isl_context_gbr
*cgbr
= (struct isl_context_gbr
*)context
;
3442 struct isl_tab_undo
*snap
;
3445 snap
= isl_tab_snap(cgbr
->tab
);
3446 r
= best_split(tab
, cgbr
->tab
);
3448 if (r
>= 0 && isl_tab_rollback(cgbr
->tab
, snap
) < 0)
3454 static int context_gbr_is_empty(struct isl_context
*context
)
3456 struct isl_context_gbr
*cgbr
= (struct isl_context_gbr
*)context
;
3459 return cgbr
->tab
->empty
;
3462 struct isl_gbr_tab_undo
{
3463 struct isl_tab_undo
*tab_snap
;
3464 struct isl_tab_undo
*shifted_snap
;
3465 struct isl_tab_undo
*cone_snap
;
3468 static void *context_gbr_save(struct isl_context
*context
)
3470 struct isl_context_gbr
*cgbr
= (struct isl_context_gbr
*)context
;
3471 struct isl_gbr_tab_undo
*snap
;
3476 snap
= isl_alloc_type(cgbr
->tab
->mat
->ctx
, struct isl_gbr_tab_undo
);
3480 snap
->tab_snap
= isl_tab_snap(cgbr
->tab
);
3481 if (isl_tab_save_samples(cgbr
->tab
) < 0)
3485 snap
->shifted_snap
= isl_tab_snap(cgbr
->shifted
);
3487 snap
->shifted_snap
= NULL
;
3490 snap
->cone_snap
= isl_tab_snap(cgbr
->cone
);
3492 snap
->cone_snap
= NULL
;
3500 static void context_gbr_restore(struct isl_context
*context
, void *save
)
3502 struct isl_context_gbr
*cgbr
= (struct isl_context_gbr
*)context
;
3503 struct isl_gbr_tab_undo
*snap
= (struct isl_gbr_tab_undo
*)save
;
3506 if (isl_tab_rollback(cgbr
->tab
, snap
->tab_snap
) < 0)
3509 if (snap
->shifted_snap
) {
3510 if (isl_tab_rollback(cgbr
->shifted
, snap
->shifted_snap
) < 0)
3512 } else if (cgbr
->shifted
) {
3513 isl_tab_free(cgbr
->shifted
);
3514 cgbr
->shifted
= NULL
;
3517 if (snap
->cone_snap
) {
3518 if (isl_tab_rollback(cgbr
->cone
, snap
->cone_snap
) < 0)
3520 } else if (cgbr
->cone
) {
3521 isl_tab_free(cgbr
->cone
);
3530 isl_tab_free(cgbr
->tab
);
3534 static void context_gbr_discard(void *save
)
3536 struct isl_gbr_tab_undo
*snap
= (struct isl_gbr_tab_undo
*)save
;
3540 static int context_gbr_is_ok(struct isl_context
*context
)
3542 struct isl_context_gbr
*cgbr
= (struct isl_context_gbr
*)context
;
3546 static void context_gbr_invalidate(struct isl_context
*context
)
3548 struct isl_context_gbr
*cgbr
= (struct isl_context_gbr
*)context
;
3549 isl_tab_free(cgbr
->tab
);
3553 static __isl_null
struct isl_context
*context_gbr_free(
3554 struct isl_context
*context
)
3556 struct isl_context_gbr
*cgbr
= (struct isl_context_gbr
*)context
;
3557 isl_tab_free(cgbr
->tab
);
3558 isl_tab_free(cgbr
->shifted
);
3559 isl_tab_free(cgbr
->cone
);
3565 struct isl_context_op isl_context_gbr_op
= {
3566 context_gbr_detect_nonnegative_parameters
,
3567 context_gbr_peek_basic_set
,
3568 context_gbr_peek_tab
,
3570 context_gbr_add_ineq
,
3571 context_gbr_ineq_sign
,
3572 context_gbr_test_ineq
,
3573 context_gbr_get_div
,
3574 context_gbr_insert_div
,
3575 context_gbr_detect_equalities
,
3576 context_gbr_best_split
,
3577 context_gbr_is_empty
,
3580 context_gbr_restore
,
3581 context_gbr_discard
,
3582 context_gbr_invalidate
,
3586 static struct isl_context
*isl_context_gbr_alloc(__isl_keep isl_basic_set
*dom
)
3588 struct isl_context_gbr
*cgbr
;
3593 cgbr
= isl_calloc_type(dom
->ctx
, struct isl_context_gbr
);
3597 cgbr
->context
.op
= &isl_context_gbr_op
;
3599 cgbr
->shifted
= NULL
;
3601 cgbr
->tab
= isl_tab_from_basic_set(dom
, 1);
3602 cgbr
->tab
= isl_tab_init_samples(cgbr
->tab
);
3605 check_gbr_integer_feasible(cgbr
);
3607 return &cgbr
->context
;
3609 cgbr
->context
.op
->free(&cgbr
->context
);
3613 /* Allocate a context corresponding to "dom".
3614 * The representation specific fields are initialized by
3615 * isl_context_lex_alloc or isl_context_gbr_alloc.
3616 * The shared "n_unknown" field is initialized to the number
3617 * of final unknown integer divisions in "dom".
3619 static struct isl_context
*isl_context_alloc(__isl_keep isl_basic_set
*dom
)
3621 struct isl_context
*context
;
3627 if (dom
->ctx
->opt
->context
== ISL_CONTEXT_LEXMIN
)
3628 context
= isl_context_lex_alloc(dom
);
3630 context
= isl_context_gbr_alloc(dom
);
3635 first
= isl_basic_set_first_unknown_div(dom
);
3637 return context
->op
->free(context
);
3638 context
->n_unknown
= isl_basic_set_dim(dom
, isl_dim_div
) - first
;
3643 /* Initialize some common fields of "sol", which keeps track
3644 * of the solution of an optimization problem on "bmap" over
3646 * If "max" is set, then a maximization problem is being solved, rather than
3647 * a minimization problem, which means that the variables in the
3648 * tableau have value "M - x" rather than "M + x".
3650 static isl_stat
sol_init(struct isl_sol
*sol
, __isl_keep isl_basic_map
*bmap
,
3651 __isl_keep isl_basic_set
*dom
, int max
)
3653 sol
->rational
= ISL_F_ISSET(bmap
, ISL_BASIC_MAP_RATIONAL
);
3654 sol
->dec_level
.callback
.run
= &sol_dec_level_wrap
;
3655 sol
->dec_level
.sol
= sol
;
3657 sol
->n_out
= isl_basic_map_dim(bmap
, isl_dim_out
);
3658 sol
->space
= isl_basic_map_get_space(bmap
);
3660 sol
->context
= isl_context_alloc(dom
);
3661 if (!sol
->space
|| !sol
->context
)
3662 return isl_stat_error
;
3667 /* Construct an isl_sol_map structure for accumulating the solution.
3668 * If track_empty is set, then we also keep track of the parts
3669 * of the context where there is no solution.
3670 * If max is set, then we are solving a maximization, rather than
3671 * a minimization problem, which means that the variables in the
3672 * tableau have value "M - x" rather than "M + x".
3674 static struct isl_sol
*sol_map_init(__isl_keep isl_basic_map
*bmap
,
3675 __isl_take isl_basic_set
*dom
, int track_empty
, int max
)
3677 struct isl_sol_map
*sol_map
= NULL
;
3683 sol_map
= isl_calloc_type(bmap
->ctx
, struct isl_sol_map
);
3687 sol_map
->sol
.free
= &sol_map_free
;
3688 if (sol_init(&sol_map
->sol
, bmap
, dom
, max
) < 0)
3690 sol_map
->sol
.add
= &sol_map_add_wrap
;
3691 sol_map
->sol
.add_empty
= track_empty
? &sol_map_add_empty_wrap
: NULL
;
3692 space
= isl_space_copy(sol_map
->sol
.space
);
3693 sol_map
->map
= isl_map_alloc_space(space
, 1, ISL_MAP_DISJOINT
);
3698 sol_map
->empty
= isl_set_alloc_space(isl_basic_set_get_space(dom
),
3699 1, ISL_SET_DISJOINT
);
3700 if (!sol_map
->empty
)
3704 isl_basic_set_free(dom
);
3705 return &sol_map
->sol
;
3707 isl_basic_set_free(dom
);
3708 sol_free(&sol_map
->sol
);
3712 /* Check whether all coefficients of (non-parameter) variables
3713 * are non-positive, meaning that no pivots can be performed on the row.
3715 static int is_critical(struct isl_tab
*tab
, int row
)
3718 unsigned off
= 2 + tab
->M
;
3720 for (j
= tab
->n_dead
; j
< tab
->n_col
; ++j
) {
3721 if (col_is_parameter_var(tab
, j
))
3724 if (isl_int_is_pos(tab
->mat
->row
[row
][off
+ j
]))
3731 /* Check whether the inequality represented by vec is strict over the integers,
3732 * i.e., there are no integer values satisfying the constraint with
3733 * equality. This happens if the gcd of the coefficients is not a divisor
3734 * of the constant term. If so, scale the constraint down by the gcd
3735 * of the coefficients.
3737 static int is_strict(struct isl_vec
*vec
)
3743 isl_seq_gcd(vec
->el
+ 1, vec
->size
- 1, &gcd
);
3744 if (!isl_int_is_one(gcd
)) {
3745 strict
= !isl_int_is_divisible_by(vec
->el
[0], gcd
);
3746 isl_int_fdiv_q(vec
->el
[0], vec
->el
[0], gcd
);
3747 isl_seq_scale_down(vec
->el
+ 1, vec
->el
+ 1, gcd
, vec
->size
-1);
3754 /* Determine the sign of the given row of the main tableau.
3755 * The result is one of
3756 * isl_tab_row_pos: always non-negative; no pivot needed
3757 * isl_tab_row_neg: always non-positive; pivot
3758 * isl_tab_row_any: can be both positive and negative; split
3760 * We first handle some simple cases
3761 * - the row sign may be known already
3762 * - the row may be obviously non-negative
3763 * - the parametric constant may be equal to that of another row
3764 * for which we know the sign. This sign will be either "pos" or
3765 * "any". If it had been "neg" then we would have pivoted before.
3767 * If none of these cases hold, we check the value of the row for each
3768 * of the currently active samples. Based on the signs of these values
3769 * we make an initial determination of the sign of the row.
3771 * all zero -> unk(nown)
3772 * all non-negative -> pos
3773 * all non-positive -> neg
3774 * both negative and positive -> all
3776 * If we end up with "all", we are done.
3777 * Otherwise, we perform a check for positive and/or negative
3778 * values as follows.
3780 * samples neg unk pos
3786 * There is no special sign for "zero", because we can usually treat zero
3787 * as either non-negative or non-positive, whatever works out best.
3788 * However, if the row is "critical", meaning that pivoting is impossible
3789 * then we don't want to limp zero with the non-positive case, because
3790 * then we we would lose the solution for those values of the parameters
3791 * where the value of the row is zero. Instead, we treat 0 as non-negative
3792 * ensuring a split if the row can attain both zero and negative values.
3793 * The same happens when the original constraint was one that could not
3794 * be satisfied with equality by any integer values of the parameters.
3795 * In this case, we normalize the constraint, but then a value of zero
3796 * for the normalized constraint is actually a positive value for the
3797 * original constraint, so again we need to treat zero as non-negative.
3798 * In both these cases, we have the following decision tree instead:
3800 * all non-negative -> pos
3801 * all negative -> neg
3802 * both negative and non-negative -> all
3810 static enum isl_tab_row_sign
row_sign(struct isl_tab
*tab
,
3811 struct isl_sol
*sol
, int row
)
3813 struct isl_vec
*ineq
= NULL
;
3814 enum isl_tab_row_sign res
= isl_tab_row_unknown
;
3819 if (tab
->row_sign
[row
] != isl_tab_row_unknown
)
3820 return tab
->row_sign
[row
];
3821 if (is_obviously_nonneg(tab
, row
))
3822 return isl_tab_row_pos
;
3823 for (row2
= tab
->n_redundant
; row2
< tab
->n_row
; ++row2
) {
3824 if (tab
->row_sign
[row2
] == isl_tab_row_unknown
)
3826 if (identical_parameter_line(tab
, row
, row2
))
3827 return tab
->row_sign
[row2
];
3830 critical
= is_critical(tab
, row
);
3832 ineq
= get_row_parameter_ineq(tab
, row
);
3836 strict
= is_strict(ineq
);
3838 res
= sol
->context
->op
->ineq_sign(sol
->context
, ineq
->el
,
3839 critical
|| strict
);
3841 if (res
== isl_tab_row_unknown
|| res
== isl_tab_row_pos
) {
3842 /* test for negative values */
3844 isl_seq_neg(ineq
->el
, ineq
->el
, ineq
->size
);
3845 isl_int_sub_ui(ineq
->el
[0], ineq
->el
[0], 1);
3847 feasible
= sol
->context
->op
->test_ineq(sol
->context
, ineq
->el
);
3851 res
= isl_tab_row_pos
;
3853 res
= (res
== isl_tab_row_unknown
) ? isl_tab_row_neg
3855 if (res
== isl_tab_row_neg
) {
3856 isl_seq_neg(ineq
->el
, ineq
->el
, ineq
->size
);
3857 isl_int_sub_ui(ineq
->el
[0], ineq
->el
[0], 1);
3861 if (res
== isl_tab_row_neg
) {
3862 /* test for positive values */
3864 if (!critical
&& !strict
)
3865 isl_int_sub_ui(ineq
->el
[0], ineq
->el
[0], 1);
3867 feasible
= sol
->context
->op
->test_ineq(sol
->context
, ineq
->el
);
3871 res
= isl_tab_row_any
;
3878 return isl_tab_row_unknown
;
3881 static void find_solutions(struct isl_sol
*sol
, struct isl_tab
*tab
);
3883 /* Find solutions for values of the parameters that satisfy the given
3886 * We currently take a snapshot of the context tableau that is reset
3887 * when we return from this function, while we make a copy of the main
3888 * tableau, leaving the original main tableau untouched.
3889 * These are fairly arbitrary choices. Making a copy also of the context
3890 * tableau would obviate the need to undo any changes made to it later,
3891 * while taking a snapshot of the main tableau could reduce memory usage.
3892 * If we were to switch to taking a snapshot of the main tableau,
3893 * we would have to keep in mind that we need to save the row signs
3894 * and that we need to do this before saving the current basis
3895 * such that the basis has been restore before we restore the row signs.
3897 static void find_in_pos(struct isl_sol
*sol
, struct isl_tab
*tab
, isl_int
*ineq
)
3903 saved
= sol
->context
->op
->save(sol
->context
);
3905 tab
= isl_tab_dup(tab
);
3909 sol
->context
->op
->add_ineq(sol
->context
, ineq
, 0, 1);
3911 find_solutions(sol
, tab
);
3914 sol
->context
->op
->restore(sol
->context
, saved
);
3916 sol
->context
->op
->discard(saved
);
3922 /* Record the absence of solutions for those values of the parameters
3923 * that do not satisfy the given inequality with equality.
3925 static void no_sol_in_strict(struct isl_sol
*sol
,
3926 struct isl_tab
*tab
, struct isl_vec
*ineq
)
3931 if (!sol
->context
|| sol
->error
)
3933 saved
= sol
->context
->op
->save(sol
->context
);
3935 isl_int_sub_ui(ineq
->el
[0], ineq
->el
[0], 1);
3937 sol
->context
->op
->add_ineq(sol
->context
, ineq
->el
, 1, 0);
3946 isl_int_add_ui(ineq
->el
[0], ineq
->el
[0], 1);
3948 sol
->context
->op
->restore(sol
->context
, saved
);
3954 /* Reset all row variables that are marked to have a sign that may
3955 * be both positive and negative to have an unknown sign.
3957 static void reset_any_to_unknown(struct isl_tab
*tab
)
3961 for (row
= tab
->n_redundant
; row
< tab
->n_row
; ++row
) {
3962 if (!isl_tab_var_from_row(tab
, row
)->is_nonneg
)
3964 if (tab
->row_sign
[row
] == isl_tab_row_any
)
3965 tab
->row_sign
[row
] = isl_tab_row_unknown
;
3969 /* Compute the lexicographic minimum of the set represented by the main
3970 * tableau "tab" within the context "sol->context_tab".
3971 * On entry the sample value of the main tableau is lexicographically
3972 * less than or equal to this lexicographic minimum.
3973 * Pivots are performed until a feasible point is found, which is then
3974 * necessarily equal to the minimum, or until the tableau is found to
3975 * be infeasible. Some pivots may need to be performed for only some
3976 * feasible values of the context tableau. If so, the context tableau
3977 * is split into a part where the pivot is needed and a part where it is not.
3979 * Whenever we enter the main loop, the main tableau is such that no
3980 * "obvious" pivots need to be performed on it, where "obvious" means
3981 * that the given row can be seen to be negative without looking at
3982 * the context tableau. In particular, for non-parametric problems,
3983 * no pivots need to be performed on the main tableau.
3984 * The caller of find_solutions is responsible for making this property
3985 * hold prior to the first iteration of the loop, while restore_lexmin
3986 * is called before every other iteration.
3988 * Inside the main loop, we first examine the signs of the rows of
3989 * the main tableau within the context of the context tableau.
3990 * If we find a row that is always non-positive for all values of
3991 * the parameters satisfying the context tableau and negative for at
3992 * least one value of the parameters, we perform the appropriate pivot
3993 * and start over. An exception is the case where no pivot can be
3994 * performed on the row. In this case, we require that the sign of
3995 * the row is negative for all values of the parameters (rather than just
3996 * non-positive). This special case is handled inside row_sign, which
3997 * will say that the row can have any sign if it determines that it can
3998 * attain both negative and zero values.
4000 * If we can't find a row that always requires a pivot, but we can find
4001 * one or more rows that require a pivot for some values of the parameters
4002 * (i.e., the row can attain both positive and negative signs), then we split
4003 * the context tableau into two parts, one where we force the sign to be
4004 * non-negative and one where we force is to be negative.
4005 * The non-negative part is handled by a recursive call (through find_in_pos).
4006 * Upon returning from this call, we continue with the negative part and
4007 * perform the required pivot.
4009 * If no such rows can be found, all rows are non-negative and we have
4010 * found a (rational) feasible point. If we only wanted a rational point
4012 * Otherwise, we check if all values of the sample point of the tableau
4013 * are integral for the variables. If so, we have found the minimal
4014 * integral point and we are done.
4015 * If the sample point is not integral, then we need to make a distinction
4016 * based on whether the constant term is non-integral or the coefficients
4017 * of the parameters. Furthermore, in order to decide how to handle
4018 * the non-integrality, we also need to know whether the coefficients
4019 * of the other columns in the tableau are integral. This leads
4020 * to the following table. The first two rows do not correspond
4021 * to a non-integral sample point and are only mentioned for completeness.
4023 * constant parameters other
4026 * int int rat | -> no problem
4028 * rat int int -> fail
4030 * rat int rat -> cut
4033 * rat rat rat | -> parametric cut
4036 * rat rat int | -> split context
4038 * If the parametric constant is completely integral, then there is nothing
4039 * to be done. If the constant term is non-integral, but all the other
4040 * coefficient are integral, then there is nothing that can be done
4041 * and the tableau has no integral solution.
4042 * If, on the other hand, one or more of the other columns have rational
4043 * coefficients, but the parameter coefficients are all integral, then
4044 * we can perform a regular (non-parametric) cut.
4045 * Finally, if there is any parameter coefficient that is non-integral,
4046 * then we need to involve the context tableau. There are two cases here.
4047 * If at least one other column has a rational coefficient, then we
4048 * can perform a parametric cut in the main tableau by adding a new
4049 * integer division in the context tableau.
4050 * If all other columns have integral coefficients, then we need to
4051 * enforce that the rational combination of parameters (c + \sum a_i y_i)/m
4052 * is always integral. We do this by introducing an integer division
4053 * q = floor((c + \sum a_i y_i)/m) and stipulating that its argument should
4054 * always be integral in the context tableau, i.e., m q = c + \sum a_i y_i.
4055 * Since q is expressed in the tableau as
4056 * c + \sum a_i y_i - m q >= 0
4057 * -c - \sum a_i y_i + m q + m - 1 >= 0
4058 * it is sufficient to add the inequality
4059 * -c - \sum a_i y_i + m q >= 0
4060 * In the part of the context where this inequality does not hold, the
4061 * main tableau is marked as being empty.
4063 static void find_solutions(struct isl_sol
*sol
, struct isl_tab
*tab
)
4065 struct isl_context
*context
;
4068 if (!tab
|| sol
->error
)
4071 context
= sol
->context
;
4075 if (context
->op
->is_empty(context
))
4078 for (r
= 0; r
>= 0 && tab
&& !tab
->empty
; r
= restore_lexmin(tab
)) {
4081 enum isl_tab_row_sign sgn
;
4085 for (row
= tab
->n_redundant
; row
< tab
->n_row
; ++row
) {
4086 if (!isl_tab_var_from_row(tab
, row
)->is_nonneg
)
4088 sgn
= row_sign(tab
, sol
, row
);
4091 tab
->row_sign
[row
] = sgn
;
4092 if (sgn
== isl_tab_row_any
)
4094 if (sgn
== isl_tab_row_any
&& split
== -1)
4096 if (sgn
== isl_tab_row_neg
)
4099 if (row
< tab
->n_row
)
4102 struct isl_vec
*ineq
;
4104 split
= context
->op
->best_split(context
, tab
);
4107 ineq
= get_row_parameter_ineq(tab
, split
);
4111 reset_any_to_unknown(tab
);
4112 tab
->row_sign
[split
] = isl_tab_row_pos
;
4114 find_in_pos(sol
, tab
, ineq
->el
);
4115 tab
->row_sign
[split
] = isl_tab_row_neg
;
4116 isl_seq_neg(ineq
->el
, ineq
->el
, ineq
->size
);
4117 isl_int_sub_ui(ineq
->el
[0], ineq
->el
[0], 1);
4119 context
->op
->add_ineq(context
, ineq
->el
, 0, 1);
4127 row
= first_non_integer_row(tab
, &flags
);
4130 if (ISL_FL_ISSET(flags
, I_PAR
)) {
4131 if (ISL_FL_ISSET(flags
, I_VAR
)) {
4132 if (isl_tab_mark_empty(tab
) < 0)
4136 row
= add_cut(tab
, row
);
4137 } else if (ISL_FL_ISSET(flags
, I_VAR
)) {
4138 struct isl_vec
*div
;
4139 struct isl_vec
*ineq
;
4141 div
= get_row_split_div(tab
, row
);
4144 d
= context
->op
->get_div(context
, tab
, div
);
4148 ineq
= ineq_for_div(context
->op
->peek_basic_set(context
), d
);
4152 no_sol_in_strict(sol
, tab
, ineq
);
4153 isl_seq_neg(ineq
->el
, ineq
->el
, ineq
->size
);
4154 context
->op
->add_ineq(context
, ineq
->el
, 1, 1);
4156 if (sol
->error
|| !context
->op
->is_ok(context
))
4158 tab
= set_row_cst_to_div(tab
, row
, d
);
4159 if (context
->op
->is_empty(context
))
4162 row
= add_parametric_cut(tab
, row
, context
);
4177 /* Does "sol" contain a pair of partial solutions that could potentially
4180 * We currently only check that "sol" is not in an error state
4181 * and that there are at least two partial solutions of which the final two
4182 * are defined at the same level.
4184 static int sol_has_mergeable_solutions(struct isl_sol
*sol
)
4190 if (!sol
->partial
->next
)
4192 return sol
->partial
->level
== sol
->partial
->next
->level
;
4195 /* Compute the lexicographic minimum of the set represented by the main
4196 * tableau "tab" within the context "sol->context_tab".
4198 * As a preprocessing step, we first transfer all the purely parametric
4199 * equalities from the main tableau to the context tableau, i.e.,
4200 * parameters that have been pivoted to a row.
4201 * These equalities are ignored by the main algorithm, because the
4202 * corresponding rows may not be marked as being non-negative.
4203 * In parts of the context where the added equality does not hold,
4204 * the main tableau is marked as being empty.
4206 * Before we embark on the actual computation, we save a copy
4207 * of the context. When we return, we check if there are any
4208 * partial solutions that can potentially be merged. If so,
4209 * we perform a rollback to the initial state of the context.
4210 * The merging of partial solutions happens inside calls to
4211 * sol_dec_level that are pushed onto the undo stack of the context.
4212 * If there are no partial solutions that can potentially be merged
4213 * then the rollback is skipped as it would just be wasted effort.
4215 static void find_solutions_main(struct isl_sol
*sol
, struct isl_tab
*tab
)
4225 for (row
= tab
->n_redundant
; row
< tab
->n_row
; ++row
) {
4229 if (!row_is_parameter_var(tab
, row
))
4231 if (tab
->row_var
[row
] < tab
->n_param
)
4232 p
= tab
->row_var
[row
];
4234 p
= tab
->row_var
[row
]
4235 + tab
->n_param
- (tab
->n_var
- tab
->n_div
);
4237 eq
= isl_vec_alloc(tab
->mat
->ctx
, 1+tab
->n_param
+tab
->n_div
);
4240 get_row_parameter_line(tab
, row
, eq
->el
);
4241 isl_int_neg(eq
->el
[1 + p
], tab
->mat
->row
[row
][0]);
4242 eq
= isl_vec_normalize(eq
);
4245 no_sol_in_strict(sol
, tab
, eq
);
4247 isl_seq_neg(eq
->el
, eq
->el
, eq
->size
);
4249 no_sol_in_strict(sol
, tab
, eq
);
4250 isl_seq_neg(eq
->el
, eq
->el
, eq
->size
);
4252 sol
->context
->op
->add_eq(sol
->context
, eq
->el
, 1, 1);
4256 if (isl_tab_mark_redundant(tab
, row
) < 0)
4259 if (sol
->context
->op
->is_empty(sol
->context
))
4262 row
= tab
->n_redundant
- 1;
4265 saved
= sol
->context
->op
->save(sol
->context
);
4267 find_solutions(sol
, tab
);
4269 if (sol_has_mergeable_solutions(sol
))
4270 sol
->context
->op
->restore(sol
->context
, saved
);
4272 sol
->context
->op
->discard(saved
);
4283 /* Check if integer division "div" of "dom" also occurs in "bmap".
4284 * If so, return its position within the divs.
4285 * Otherwise, return a position beyond the integer divisions.
4287 static int find_context_div(__isl_keep isl_basic_map
*bmap
,
4288 __isl_keep isl_basic_set
*dom
, unsigned div
)
4291 unsigned b_dim
, d_dim
, n_div
;
4296 b_dim
= isl_space_dim(bmap
->dim
, isl_dim_all
);
4297 d_dim
= isl_space_dim(dom
->dim
, isl_dim_all
);
4298 n_div
= isl_basic_map_dim(bmap
, isl_dim_div
);
4300 if (isl_int_is_zero(dom
->div
[div
][0]))
4302 if (isl_seq_first_non_zero(dom
->div
[div
] + 2 + d_dim
, dom
->n_div
) != -1)
4305 for (i
= 0; i
< n_div
; ++i
) {
4306 if (isl_int_is_zero(bmap
->div
[i
][0]))
4308 if (isl_seq_first_non_zero(bmap
->div
[i
] + 2 + d_dim
,
4309 (b_dim
- d_dim
) + n_div
) != -1)
4311 if (isl_seq_eq(bmap
->div
[i
], dom
->div
[div
], 2 + d_dim
))
4317 /* The correspondence between the variables in the main tableau,
4318 * the context tableau, and the input map and domain is as follows.
4319 * The first n_param and the last n_div variables of the main tableau
4320 * form the variables of the context tableau.
4321 * In the basic map, these n_param variables correspond to the
4322 * parameters and the input dimensions. In the domain, they correspond
4323 * to the parameters and the set dimensions.
4324 * The n_div variables correspond to the integer divisions in the domain.
4325 * To ensure that everything lines up, we may need to copy some of the
4326 * integer divisions of the domain to the map. These have to be placed
4327 * in the same order as those in the context and they have to be placed
4328 * after any other integer divisions that the map may have.
4329 * This function performs the required reordering.
4331 static __isl_give isl_basic_map
*align_context_divs(
4332 __isl_take isl_basic_map
*bmap
, __isl_keep isl_basic_set
*dom
)
4337 unsigned bmap_n_div
;
4339 bmap_n_div
= isl_basic_map_dim(bmap
, isl_dim_div
);
4341 for (i
= 0; i
< dom
->n_div
; ++i
) {
4344 pos
= find_context_div(bmap
, dom
, i
);
4346 return isl_basic_map_free(bmap
);
4347 if (pos
< bmap_n_div
)
4350 other
= bmap_n_div
- common
;
4351 if (dom
->n_div
- common
> 0) {
4352 bmap
= isl_basic_map_extend_space(bmap
, isl_space_copy(bmap
->dim
),
4353 dom
->n_div
- common
, 0, 0);
4357 for (i
= 0; i
< dom
->n_div
; ++i
) {
4358 int pos
= find_context_div(bmap
, dom
, i
);
4360 bmap
= isl_basic_map_free(bmap
);
4361 if (pos
>= bmap_n_div
) {
4362 pos
= isl_basic_map_alloc_div(bmap
);
4365 isl_int_set_si(bmap
->div
[pos
][0], 0);
4368 if (pos
!= other
+ i
)
4369 bmap
= isl_basic_map_swap_div(bmap
, pos
, other
+ i
);
4373 isl_basic_map_free(bmap
);
4377 /* Base case of isl_tab_basic_map_partial_lexopt, after removing
4378 * some obvious symmetries.
4380 * We make sure the divs in the domain are properly ordered,
4381 * because they will be added one by one in the given order
4382 * during the construction of the solution map.
4383 * Furthermore, make sure that the known integer divisions
4384 * appear before any unknown integer division because the solution
4385 * may depend on the known integer divisions, while anything that
4386 * depends on any variable starting from the first unknown integer
4387 * division is ignored in sol_pma_add.
4389 static struct isl_sol
*basic_map_partial_lexopt_base_sol(
4390 __isl_take isl_basic_map
*bmap
, __isl_take isl_basic_set
*dom
,
4391 __isl_give isl_set
**empty
, int max
,
4392 struct isl_sol
*(*init
)(__isl_keep isl_basic_map
*bmap
,
4393 __isl_take isl_basic_set
*dom
, int track_empty
, int max
))
4395 struct isl_tab
*tab
;
4396 struct isl_sol
*sol
= NULL
;
4397 struct isl_context
*context
;
4400 dom
= isl_basic_set_sort_divs(dom
);
4401 bmap
= align_context_divs(bmap
, dom
);
4403 sol
= init(bmap
, dom
, !!empty
, max
);
4407 context
= sol
->context
;
4408 if (isl_basic_set_plain_is_empty(context
->op
->peek_basic_set(context
)))
4410 else if (isl_basic_map_plain_is_empty(bmap
)) {
4413 isl_basic_set_copy(context
->op
->peek_basic_set(context
)));
4415 tab
= tab_for_lexmin(bmap
,
4416 context
->op
->peek_basic_set(context
), 1, max
);
4417 tab
= context
->op
->detect_nonnegative_parameters(context
, tab
);
4418 find_solutions_main(sol
, tab
);
4423 isl_basic_map_free(bmap
);
4427 isl_basic_map_free(bmap
);
4431 /* Base case of isl_tab_basic_map_partial_lexopt, after removing
4432 * some obvious symmetries.
4434 * We call basic_map_partial_lexopt_base_sol and extract the results.
4436 static __isl_give isl_map
*basic_map_partial_lexopt_base(
4437 __isl_take isl_basic_map
*bmap
, __isl_take isl_basic_set
*dom
,
4438 __isl_give isl_set
**empty
, int max
)
4440 isl_map
*result
= NULL
;
4441 struct isl_sol
*sol
;
4442 struct isl_sol_map
*sol_map
;
4444 sol
= basic_map_partial_lexopt_base_sol(bmap
, dom
, empty
, max
,
4448 sol_map
= (struct isl_sol_map
*) sol
;
4450 result
= isl_map_copy(sol_map
->map
);
4452 *empty
= isl_set_copy(sol_map
->empty
);
4453 sol_free(&sol_map
->sol
);
4457 /* Return a count of the number of occurrences of the "n" first
4458 * variables in the inequality constraints of "bmap".
4460 static __isl_give
int *count_occurrences(__isl_keep isl_basic_map
*bmap
,
4469 ctx
= isl_basic_map_get_ctx(bmap
);
4470 occurrences
= isl_calloc_array(ctx
, int, n
);
4474 for (i
= 0; i
< bmap
->n_ineq
; ++i
) {
4475 for (j
= 0; j
< n
; ++j
) {
4476 if (!isl_int_is_zero(bmap
->ineq
[i
][1 + j
]))
4484 /* Do all of the "n" variables with non-zero coefficients in "c"
4485 * occur in exactly a single constraint.
4486 * "occurrences" is an array of length "n" containing the number
4487 * of occurrences of each of the variables in the inequality constraints.
4489 static int single_occurrence(int n
, isl_int
*c
, int *occurrences
)
4493 for (i
= 0; i
< n
; ++i
) {
4494 if (isl_int_is_zero(c
[i
]))
4496 if (occurrences
[i
] != 1)
4503 /* Do all of the "n" initial variables that occur in inequality constraint
4504 * "ineq" of "bmap" only occur in that constraint?
4506 static int all_single_occurrence(__isl_keep isl_basic_map
*bmap
, int ineq
,
4511 for (i
= 0; i
< n
; ++i
) {
4512 if (isl_int_is_zero(bmap
->ineq
[ineq
][1 + i
]))
4514 for (j
= 0; j
< bmap
->n_ineq
; ++j
) {
4517 if (!isl_int_is_zero(bmap
->ineq
[j
][1 + i
]))
4525 /* Structure used during detection of parallel constraints.
4526 * n_in: number of "input" variables: isl_dim_param + isl_dim_in
4527 * n_out: number of "output" variables: isl_dim_out + isl_dim_div
4528 * val: the coefficients of the output variables
4530 struct isl_constraint_equal_info
{
4536 /* Check whether the coefficients of the output variables
4537 * of the constraint in "entry" are equal to info->val.
4539 static int constraint_equal(const void *entry
, const void *val
)
4541 isl_int
**row
= (isl_int
**)entry
;
4542 const struct isl_constraint_equal_info
*info
= val
;
4544 return isl_seq_eq((*row
) + 1 + info
->n_in
, info
->val
, info
->n_out
);
4547 /* Check whether "bmap" has a pair of constraints that have
4548 * the same coefficients for the output variables.
4549 * Note that the coefficients of the existentially quantified
4550 * variables need to be zero since the existentially quantified
4551 * of the result are usually not the same as those of the input.
4552 * Furthermore, check that each of the input variables that occur
4553 * in those constraints does not occur in any other constraint.
4554 * If so, return true and return the row indices of the two constraints
4555 * in *first and *second.
4557 static isl_bool
parallel_constraints(__isl_keep isl_basic_map
*bmap
,
4558 int *first
, int *second
)
4562 int *occurrences
= NULL
;
4563 struct isl_hash_table
*table
= NULL
;
4564 struct isl_hash_table_entry
*entry
;
4565 struct isl_constraint_equal_info info
;
4569 ctx
= isl_basic_map_get_ctx(bmap
);
4570 table
= isl_hash_table_alloc(ctx
, bmap
->n_ineq
);
4574 info
.n_in
= isl_basic_map_dim(bmap
, isl_dim_param
) +
4575 isl_basic_map_dim(bmap
, isl_dim_in
);
4576 occurrences
= count_occurrences(bmap
, info
.n_in
);
4577 if (info
.n_in
&& !occurrences
)
4579 n_out
= isl_basic_map_dim(bmap
, isl_dim_out
);
4580 n_div
= isl_basic_map_dim(bmap
, isl_dim_div
);
4581 info
.n_out
= n_out
+ n_div
;
4582 for (i
= 0; i
< bmap
->n_ineq
; ++i
) {
4585 info
.val
= bmap
->ineq
[i
] + 1 + info
.n_in
;
4586 if (isl_seq_first_non_zero(info
.val
, n_out
) < 0)
4588 if (isl_seq_first_non_zero(info
.val
+ n_out
, n_div
) >= 0)
4590 if (!single_occurrence(info
.n_in
, bmap
->ineq
[i
] + 1,
4593 hash
= isl_seq_get_hash(info
.val
, info
.n_out
);
4594 entry
= isl_hash_table_find(ctx
, table
, hash
,
4595 constraint_equal
, &info
, 1);
4600 entry
->data
= &bmap
->ineq
[i
];
4603 if (i
< bmap
->n_ineq
) {
4604 *first
= ((isl_int
**)entry
->data
) - bmap
->ineq
;
4608 isl_hash_table_free(ctx
, table
);
4611 return i
< bmap
->n_ineq
;
4613 isl_hash_table_free(ctx
, table
);
4615 return isl_bool_error
;
4618 /* Given a set of upper bounds in "var", add constraints to "bset"
4619 * that make the i-th bound smallest.
4621 * In particular, if there are n bounds b_i, then add the constraints
4623 * b_i <= b_j for j > i
4624 * b_i < b_j for j < i
4626 static __isl_give isl_basic_set
*select_minimum(__isl_take isl_basic_set
*bset
,
4627 __isl_keep isl_mat
*var
, int i
)
4632 ctx
= isl_mat_get_ctx(var
);
4634 for (j
= 0; j
< var
->n_row
; ++j
) {
4637 k
= isl_basic_set_alloc_inequality(bset
);
4640 isl_seq_combine(bset
->ineq
[k
], ctx
->one
, var
->row
[j
],
4641 ctx
->negone
, var
->row
[i
], var
->n_col
);
4642 isl_int_set_si(bset
->ineq
[k
][var
->n_col
], 0);
4644 isl_int_sub_ui(bset
->ineq
[k
][0], bset
->ineq
[k
][0], 1);
4647 bset
= isl_basic_set_finalize(bset
);
4651 isl_basic_set_free(bset
);
4655 /* Given a set of upper bounds on the last "input" variable m,
4656 * construct a set that assigns the minimal upper bound to m, i.e.,
4657 * construct a set that divides the space into cells where one
4658 * of the upper bounds is smaller than all the others and assign
4659 * this upper bound to m.
4661 * In particular, if there are n bounds b_i, then the result
4662 * consists of n basic sets, each one of the form
4665 * b_i <= b_j for j > i
4666 * b_i < b_j for j < i
4668 static __isl_give isl_set
*set_minimum(__isl_take isl_space
*space
,
4669 __isl_take isl_mat
*var
)
4672 isl_basic_set
*bset
= NULL
;
4673 isl_set
*set
= NULL
;
4678 set
= isl_set_alloc_space(isl_space_copy(space
),
4679 var
->n_row
, ISL_SET_DISJOINT
);
4681 for (i
= 0; i
< var
->n_row
; ++i
) {
4682 bset
= isl_basic_set_alloc_space(isl_space_copy(space
), 0,
4684 k
= isl_basic_set_alloc_equality(bset
);
4687 isl_seq_cpy(bset
->eq
[k
], var
->row
[i
], var
->n_col
);
4688 isl_int_set_si(bset
->eq
[k
][var
->n_col
], -1);
4689 bset
= select_minimum(bset
, var
, i
);
4690 set
= isl_set_add_basic_set(set
, bset
);
4693 isl_space_free(space
);
4697 isl_basic_set_free(bset
);
4699 isl_space_free(space
);
4704 /* Given that the last input variable of "bmap" represents the minimum
4705 * of the bounds in "cst", check whether we need to split the domain
4706 * based on which bound attains the minimum.
4708 * A split is needed when the minimum appears in an integer division
4709 * or in an equality. Otherwise, it is only needed if it appears in
4710 * an upper bound that is different from the upper bounds on which it
4713 static isl_bool
need_split_basic_map(__isl_keep isl_basic_map
*bmap
,
4714 __isl_keep isl_mat
*cst
)
4720 pos
= cst
->n_col
- 1;
4721 total
= isl_basic_map_dim(bmap
, isl_dim_all
);
4723 for (i
= 0; i
< bmap
->n_div
; ++i
)
4724 if (!isl_int_is_zero(bmap
->div
[i
][2 + pos
]))
4725 return isl_bool_true
;
4727 for (i
= 0; i
< bmap
->n_eq
; ++i
)
4728 if (!isl_int_is_zero(bmap
->eq
[i
][1 + pos
]))
4729 return isl_bool_true
;
4731 for (i
= 0; i
< bmap
->n_ineq
; ++i
) {
4732 if (isl_int_is_nonneg(bmap
->ineq
[i
][1 + pos
]))
4734 if (!isl_int_is_negone(bmap
->ineq
[i
][1 + pos
]))
4735 return isl_bool_true
;
4736 if (isl_seq_first_non_zero(bmap
->ineq
[i
] + 1 + pos
+ 1,
4737 total
- pos
- 1) >= 0)
4738 return isl_bool_true
;
4740 for (j
= 0; j
< cst
->n_row
; ++j
)
4741 if (isl_seq_eq(bmap
->ineq
[i
], cst
->row
[j
], cst
->n_col
))
4743 if (j
>= cst
->n_row
)
4744 return isl_bool_true
;
4747 return isl_bool_false
;
4750 /* Given that the last set variable of "bset" represents the minimum
4751 * of the bounds in "cst", check whether we need to split the domain
4752 * based on which bound attains the minimum.
4754 * We simply call need_split_basic_map here. This is safe because
4755 * the position of the minimum is computed from "cst" and not
4758 static isl_bool
need_split_basic_set(__isl_keep isl_basic_set
*bset
,
4759 __isl_keep isl_mat
*cst
)
4761 return need_split_basic_map(bset_to_bmap(bset
), cst
);
4764 /* Given that the last set variable of "set" represents the minimum
4765 * of the bounds in "cst", check whether we need to split the domain
4766 * based on which bound attains the minimum.
4768 static isl_bool
need_split_set(__isl_keep isl_set
*set
, __isl_keep isl_mat
*cst
)
4772 for (i
= 0; i
< set
->n
; ++i
) {
4775 split
= need_split_basic_set(set
->p
[i
], cst
);
4776 if (split
< 0 || split
)
4780 return isl_bool_false
;
4783 /* Given a map of which the last input variable is the minimum
4784 * of the bounds in "cst", split each basic set in the set
4785 * in pieces where one of the bounds is (strictly) smaller than the others.
4786 * This subdivision is given in "min_expr".
4787 * The variable is subsequently projected out.
4789 * We only do the split when it is needed.
4790 * For example if the last input variable m = min(a,b) and the only
4791 * constraints in the given basic set are lower bounds on m,
4792 * i.e., l <= m = min(a,b), then we can simply project out m
4793 * to obtain l <= a and l <= b, without having to split on whether
4794 * m is equal to a or b.
4796 static __isl_give isl_map
*split_domain(__isl_take isl_map
*opt
,
4797 __isl_take isl_set
*min_expr
, __isl_take isl_mat
*cst
)
4804 if (!opt
|| !min_expr
|| !cst
)
4807 n_in
= isl_map_dim(opt
, isl_dim_in
);
4808 space
= isl_map_get_space(opt
);
4809 space
= isl_space_drop_dims(space
, isl_dim_in
, n_in
- 1, 1);
4810 res
= isl_map_empty(space
);
4812 for (i
= 0; i
< opt
->n
; ++i
) {
4816 map
= isl_map_from_basic_map(isl_basic_map_copy(opt
->p
[i
]));
4817 split
= need_split_basic_map(opt
->p
[i
], cst
);
4819 map
= isl_map_free(map
);
4821 map
= isl_map_intersect_domain(map
,
4822 isl_set_copy(min_expr
));
4823 map
= isl_map_remove_dims(map
, isl_dim_in
, n_in
- 1, 1);
4825 res
= isl_map_union_disjoint(res
, map
);
4829 isl_set_free(min_expr
);
4834 isl_set_free(min_expr
);
4839 /* Given a set of which the last set variable is the minimum
4840 * of the bounds in "cst", split each basic set in the set
4841 * in pieces where one of the bounds is (strictly) smaller than the others.
4842 * This subdivision is given in "min_expr".
4843 * The variable is subsequently projected out.
4845 static __isl_give isl_set
*split(__isl_take isl_set
*empty
,
4846 __isl_take isl_set
*min_expr
, __isl_take isl_mat
*cst
)
4850 map
= isl_map_from_domain(empty
);
4851 map
= split_domain(map
, min_expr
, cst
);
4852 empty
= isl_map_domain(map
);
4857 static __isl_give isl_map
*basic_map_partial_lexopt(
4858 __isl_take isl_basic_map
*bmap
, __isl_take isl_basic_set
*dom
,
4859 __isl_give isl_set
**empty
, int max
);
4861 /* This function is called from basic_map_partial_lexopt_symm.
4862 * The last variable of "bmap" and "dom" corresponds to the minimum
4863 * of the bounds in "cst". "map_space" is the space of the original
4864 * input relation (of basic_map_partial_lexopt_symm) and "set_space"
4865 * is the space of the original domain.
4867 * We recursively call basic_map_partial_lexopt and then plug in
4868 * the definition of the minimum in the result.
4870 static __isl_give isl_map
*basic_map_partial_lexopt_symm_core(
4871 __isl_take isl_basic_map
*bmap
, __isl_take isl_basic_set
*dom
,
4872 __isl_give isl_set
**empty
, int max
, __isl_take isl_mat
*cst
,
4873 __isl_take isl_space
*map_space
, __isl_take isl_space
*set_space
)
4878 min_expr
= set_minimum(isl_basic_set_get_space(dom
), isl_mat_copy(cst
));
4880 opt
= basic_map_partial_lexopt(bmap
, dom
, empty
, max
);
4883 *empty
= split(*empty
,
4884 isl_set_copy(min_expr
), isl_mat_copy(cst
));
4885 *empty
= isl_set_reset_space(*empty
, set_space
);
4888 opt
= split_domain(opt
, min_expr
, cst
);
4889 opt
= isl_map_reset_space(opt
, map_space
);
4894 /* Extract a domain from "bmap" for the purpose of computing
4895 * a lexicographic optimum.
4897 * This function is only called when the caller wants to compute a full
4898 * lexicographic optimum, i.e., without specifying a domain. In this case,
4899 * the caller is not interested in the part of the domain space where
4900 * there is no solution and the domain can be initialized to those constraints
4901 * of "bmap" that only involve the parameters and the input dimensions.
4902 * This relieves the parametric programming engine from detecting those
4903 * inequalities and transferring them to the context. More importantly,
4904 * it ensures that those inequalities are transferred first and not
4905 * intermixed with inequalities that actually split the domain.
4907 * If the caller does not require the absence of existentially quantified
4908 * variables in the result (i.e., if ISL_OPT_QE is not set in "flags"),
4909 * then the actual domain of "bmap" can be used. This ensures that
4910 * the domain does not need to be split at all just to separate out
4911 * pieces of the domain that do not have a solution from piece that do.
4912 * This domain cannot be used in general because it may involve
4913 * (unknown) existentially quantified variables which will then also
4914 * appear in the solution.
4916 static __isl_give isl_basic_set
*extract_domain(__isl_keep isl_basic_map
*bmap
,
4922 n_div
= isl_basic_map_dim(bmap
, isl_dim_div
);
4923 n_out
= isl_basic_map_dim(bmap
, isl_dim_out
);
4924 bmap
= isl_basic_map_copy(bmap
);
4925 if (ISL_FL_ISSET(flags
, ISL_OPT_QE
)) {
4926 bmap
= isl_basic_map_drop_constraints_involving_dims(bmap
,
4927 isl_dim_div
, 0, n_div
);
4928 bmap
= isl_basic_map_drop_constraints_involving_dims(bmap
,
4929 isl_dim_out
, 0, n_out
);
4931 return isl_basic_map_domain(bmap
);
4935 #define TYPE isl_map
4938 #include "isl_tab_lexopt_templ.c"
4940 /* Extract the subsequence of the sample value of "tab"
4941 * starting at "pos" and of length "len".
4943 static __isl_give isl_vec
*extract_sample_sequence(struct isl_tab
*tab
,
4950 ctx
= isl_tab_get_ctx(tab
);
4951 v
= isl_vec_alloc(ctx
, len
);
4954 for (i
= 0; i
< len
; ++i
) {
4955 if (!tab
->var
[pos
+ i
].is_row
) {
4956 isl_int_set_si(v
->el
[i
], 0);
4960 row
= tab
->var
[pos
+ i
].index
;
4961 isl_int_divexact(v
->el
[i
], tab
->mat
->row
[row
][1],
4962 tab
->mat
->row
[row
][0]);
4969 /* Check if the sequence of variables starting at "pos"
4970 * represents a trivial solution according to "trivial".
4971 * That is, is the result of applying "trivial" to this sequence
4972 * equal to the zero vector?
4974 static isl_bool
region_is_trivial(struct isl_tab
*tab
, int pos
,
4975 __isl_keep isl_mat
*trivial
)
4979 isl_bool is_trivial
;
4982 return isl_bool_error
;
4984 n
= isl_mat_rows(trivial
);
4986 return isl_bool_false
;
4988 len
= isl_mat_cols(trivial
);
4989 v
= extract_sample_sequence(tab
, pos
, len
);
4990 v
= isl_mat_vec_product(isl_mat_copy(trivial
), v
);
4991 is_trivial
= isl_vec_is_zero(v
);
4997 /* Global internal data for isl_tab_basic_set_non_trivial_lexmin.
4999 * "n_op" is the number of initial coordinates to optimize,
5000 * as passed to isl_tab_basic_set_non_trivial_lexmin.
5001 * "region" is the "n_region"-sized array of regions passed
5002 * to isl_tab_basic_set_non_trivial_lexmin.
5004 * "tab" is the tableau that corresponds to the ILP problem.
5005 * "local" is an array of local data structure, one for each
5006 * (potential) level of the backtracking procedure of
5007 * isl_tab_basic_set_non_trivial_lexmin.
5008 * "v" is a pre-allocated vector that can be used for adding
5009 * constraints to the tableau.
5011 * "sol" contains the best solution found so far.
5012 * It is initialized to a vector of size zero.
5014 struct isl_lexmin_data
{
5017 struct isl_trivial_region
*region
;
5019 struct isl_tab
*tab
;
5020 struct isl_local_region
*local
;
5026 /* Return the index of the first trivial region, "n_region" if all regions
5027 * are non-trivial or -1 in case of error.
5029 static int first_trivial_region(struct isl_lexmin_data
*data
)
5033 for (i
= 0; i
< data
->n_region
; ++i
) {
5035 trivial
= region_is_trivial(data
->tab
, data
->region
[i
].pos
,
5036 data
->region
[i
].trivial
);
5043 return data
->n_region
;
5046 /* Check if the solution is optimal, i.e., whether the first
5047 * n_op entries are zero.
5049 static int is_optimal(__isl_keep isl_vec
*sol
, int n_op
)
5053 for (i
= 0; i
< n_op
; ++i
)
5054 if (!isl_int_is_zero(sol
->el
[1 + i
]))
5059 /* Add constraints to "tab" that ensure that any solution is significantly
5060 * better than that represented by "sol". That is, find the first
5061 * relevant (within first n_op) non-zero coefficient and force it (along
5062 * with all previous coefficients) to be zero.
5063 * If the solution is already optimal (all relevant coefficients are zero),
5064 * then just mark the table as empty.
5065 * "n_zero" is the number of coefficients that have been forced zero
5066 * by previous calls to this function at the same level.
5067 * Return the updated number of forced zero coefficients or -1 on error.
5069 * This function assumes that at least 2 * (n_op - n_zero) more rows and
5070 * at least 2 * (n_op - n_zero) more elements in the constraint array
5071 * are available in the tableau.
5073 static int force_better_solution(struct isl_tab
*tab
,
5074 __isl_keep isl_vec
*sol
, int n_op
, int n_zero
)
5083 for (i
= n_zero
; i
< n_op
; ++i
)
5084 if (!isl_int_is_zero(sol
->el
[1 + i
]))
5088 if (isl_tab_mark_empty(tab
) < 0)
5093 ctx
= isl_vec_get_ctx(sol
);
5094 v
= isl_vec_alloc(ctx
, 1 + tab
->n_var
);
5099 for (; i
>= n_zero
; --i
) {
5101 isl_int_set_si(v
->el
[1 + i
], -1);
5102 if (add_lexmin_eq(tab
, v
->el
) < 0)
5113 /* Fix triviality direction "dir" of the given region to zero.
5115 * This function assumes that at least two more rows and at least
5116 * two more elements in the constraint array are available in the tableau.
5118 static isl_stat
fix_zero(struct isl_tab
*tab
, struct isl_trivial_region
*region
,
5119 int dir
, struct isl_lexmin_data
*data
)
5123 data
->v
= isl_vec_clr(data
->v
);
5125 return isl_stat_error
;
5126 len
= isl_mat_cols(region
->trivial
);
5127 isl_seq_cpy(data
->v
->el
+ 1 + region
->pos
, region
->trivial
->row
[dir
],
5129 if (add_lexmin_eq(tab
, data
->v
->el
) < 0)
5130 return isl_stat_error
;
5135 /* This function selects case "side" for non-triviality region "region",
5136 * assuming all the equality constraints have been imposed already.
5137 * In particular, the triviality direction side/2 is made positive
5138 * if side is even and made negative if side is odd.
5140 * This function assumes that at least one more row and at least
5141 * one more element in the constraint array are available in the tableau.
5143 static struct isl_tab
*pos_neg(struct isl_tab
*tab
,
5144 struct isl_trivial_region
*region
,
5145 int side
, struct isl_lexmin_data
*data
)
5149 data
->v
= isl_vec_clr(data
->v
);
5152 isl_int_set_si(data
->v
->el
[0], -1);
5153 len
= isl_mat_cols(region
->trivial
);
5155 isl_seq_cpy(data
->v
->el
+ 1 + region
->pos
,
5156 region
->trivial
->row
[side
/ 2], len
);
5158 isl_seq_neg(data
->v
->el
+ 1 + region
->pos
,
5159 region
->trivial
->row
[side
/ 2], len
);
5160 return add_lexmin_ineq(tab
, data
->v
->el
);
5166 /* Local data at each level of the backtracking procedure of
5167 * isl_tab_basic_set_non_trivial_lexmin.
5169 * "update" is set if a solution has been found in the current case
5170 * of this level, such that a better solution needs to be enforced
5172 * "n_zero" is the number of initial coordinates that have already
5173 * been forced to be zero at this level.
5174 * "region" is the non-triviality region considered at this level.
5175 * "side" is the index of the current case at this level.
5176 * "n" is the number of triviality directions.
5177 * "snap" is a snapshot of the tableau holding a state that needs
5178 * to be satisfied by all subsequent cases.
5180 struct isl_local_region
{
5186 struct isl_tab_undo
*snap
;
5189 /* Initialize the global data structure "data" used while solving
5190 * the ILP problem "bset".
5192 static isl_stat
init_lexmin_data(struct isl_lexmin_data
*data
,
5193 __isl_keep isl_basic_set
*bset
)
5197 ctx
= isl_basic_set_get_ctx(bset
);
5199 data
->tab
= tab_for_lexmin(bset
, NULL
, 0, 0);
5201 return isl_stat_error
;
5203 data
->v
= isl_vec_alloc(ctx
, 1 + data
->tab
->n_var
);
5205 return isl_stat_error
;
5206 data
->local
= isl_calloc_array(ctx
, struct isl_local_region
,
5208 if (data
->n_region
&& !data
->local
)
5209 return isl_stat_error
;
5211 data
->sol
= isl_vec_alloc(ctx
, 0);
5216 /* Mark all outer levels as requiring a better solution
5217 * in the next cases.
5219 static void update_outer_levels(struct isl_lexmin_data
*data
, int level
)
5223 for (i
= 0; i
< level
; ++i
)
5224 data
->local
[i
].update
= 1;
5227 /* Initialize "local" to refer to region "region" and
5228 * to initiate processing at this level.
5230 static isl_stat
init_local_region(struct isl_local_region
*local
, int region
,
5231 struct isl_lexmin_data
*data
)
5233 local
->n
= isl_mat_rows(data
->region
[region
].trivial
);
5234 local
->region
= region
;
5242 /* What to do next after entering a level of the backtracking procedure.
5244 * error: some error has occurred; abort
5245 * done: an optimal solution has been found; stop search
5246 * backtrack: backtrack to the previous level
5247 * handle: add the constraints for the current level and
5248 * move to the next level
5251 isl_next_error
= -1,
5257 /* Have all cases of the current region been considered?
5258 * If there are n directions, then there are 2n cases.
5260 * The constraints in the current tableau are imposed
5261 * in all subsequent cases. This means that if the current
5262 * tableau is empty, then none of those cases should be considered
5263 * anymore and all cases have effectively been considered.
5265 static int finished_all_cases(struct isl_local_region
*local
,
5266 struct isl_lexmin_data
*data
)
5268 if (data
->tab
->empty
)
5270 return local
->side
>= 2 * local
->n
;
5273 /* Enter level "level" of the backtracking search and figure out
5274 * what to do next. "init" is set if the level was entered
5275 * from a higher level and needs to be initialized.
5276 * Otherwise, the level is entered as a result of backtracking and
5277 * the tableau needs to be restored to a position that can
5278 * be used for the next case at this level.
5279 * The snapshot is assumed to have been saved in the previous case,
5280 * before the constraints specific to that case were added.
5282 * In the initialization case, the local region is initialized
5283 * to point to the first violated region.
5284 * If the constraints of all regions are satisfied by the current
5285 * sample of the tableau, then tell the caller to continue looking
5286 * for a better solution or to stop searching if an optimal solution
5289 * If the tableau is empty or if all cases at the current level
5290 * have been considered, then the caller needs to backtrack as well.
5292 static enum isl_next
enter_level(int level
, int init
,
5293 struct isl_lexmin_data
*data
)
5295 struct isl_local_region
*local
= &data
->local
[level
];
5300 data
->tab
= cut_to_integer_lexmin(data
->tab
, CUT_ONE
);
5302 return isl_next_error
;
5303 if (data
->tab
->empty
)
5304 return isl_next_backtrack
;
5305 r
= first_trivial_region(data
);
5307 return isl_next_error
;
5308 if (r
== data
->n_region
) {
5309 update_outer_levels(data
, level
);
5310 isl_vec_free(data
->sol
);
5311 data
->sol
= isl_tab_get_sample_value(data
->tab
);
5313 return isl_next_error
;
5314 if (is_optimal(data
->sol
, data
->n_op
))
5315 return isl_next_done
;
5316 return isl_next_backtrack
;
5318 if (level
>= data
->n_region
)
5319 isl_die(isl_vec_get_ctx(data
->v
), isl_error_internal
,
5320 "nesting level too deep",
5321 return isl_next_error
);
5322 if (init_local_region(local
, r
, data
) < 0)
5323 return isl_next_error
;
5324 if (isl_tab_extend_cons(data
->tab
,
5325 2 * local
->n
+ 2 * data
->n_op
) < 0)
5326 return isl_next_error
;
5328 if (isl_tab_rollback(data
->tab
, local
->snap
) < 0)
5329 return isl_next_error
;
5332 if (finished_all_cases(local
, data
))
5333 return isl_next_backtrack
;
5334 return isl_next_handle
;
5337 /* If a solution has been found in the previous case at this level
5338 * (marked by local->update being set), then add constraints
5339 * that enforce a better solution in the present and all following cases.
5340 * The constraints only need to be imposed once because they are
5341 * included in the snapshot (taken in pick_side) that will be used in
5344 static isl_stat
better_next_side(struct isl_local_region
*local
,
5345 struct isl_lexmin_data
*data
)
5350 local
->n_zero
= force_better_solution(data
->tab
,
5351 data
->sol
, data
->n_op
, local
->n_zero
);
5352 if (local
->n_zero
< 0)
5353 return isl_stat_error
;
5360 /* Add constraints to data->tab that select the current case (local->side)
5361 * at the current level.
5363 * If the linear combinations v should not be zero, then the cases are
5366 * v_0 = 0 and v_1 >= 1
5367 * v_0 = 0 and v_1 <= -1
5368 * v_0 = 0 and v_1 = 0 and v_2 >= 1
5369 * v_0 = 0 and v_1 = 0 and v_2 <= -1
5373 * A snapshot is taken after the equality constraint (if any) has been added
5374 * such that the next case can start off from this position.
5375 * The rollback to this position is performed in enter_level.
5377 static isl_stat
pick_side(struct isl_local_region
*local
,
5378 struct isl_lexmin_data
*data
)
5380 struct isl_trivial_region
*region
;
5383 region
= &data
->region
[local
->region
];
5385 base
= 2 * (side
/2);
5387 if (side
== base
&& base
>= 2 &&
5388 fix_zero(data
->tab
, region
, base
/ 2 - 1, data
) < 0)
5389 return isl_stat_error
;
5391 local
->snap
= isl_tab_snap(data
->tab
);
5392 if (isl_tab_push_basis(data
->tab
) < 0)
5393 return isl_stat_error
;
5395 data
->tab
= pos_neg(data
->tab
, region
, side
, data
);
5397 return isl_stat_error
;
5401 /* Free the memory associated to "data".
5403 static void clear_lexmin_data(struct isl_lexmin_data
*data
)
5406 isl_vec_free(data
->v
);
5407 isl_tab_free(data
->tab
);
5410 /* Return the lexicographically smallest non-trivial solution of the
5411 * given ILP problem.
5413 * All variables are assumed to be non-negative.
5415 * n_op is the number of initial coordinates to optimize.
5416 * That is, once a solution has been found, we will only continue looking
5417 * for solutions that result in significantly better values for those
5418 * initial coordinates. That is, we only continue looking for solutions
5419 * that increase the number of initial zeros in this sequence.
5421 * A solution is non-trivial, if it is non-trivial on each of the
5422 * specified regions. Each region represents a sequence of
5423 * triviality directions on a sequence of variables that starts
5424 * at a given position. A solution is non-trivial on such a region if
5425 * at least one of the triviality directions is non-zero
5426 * on that sequence of variables.
5428 * Whenever a conflict is encountered, all constraints involved are
5429 * reported to the caller through a call to "conflict".
5431 * We perform a simple branch-and-bound backtracking search.
5432 * Each level in the search represents an initially trivial region
5433 * that is forced to be non-trivial.
5434 * At each level we consider 2 * n cases, where n
5435 * is the number of triviality directions.
5436 * In terms of those n directions v_i, we consider the cases
5439 * v_0 = 0 and v_1 >= 1
5440 * v_0 = 0 and v_1 <= -1
5441 * v_0 = 0 and v_1 = 0 and v_2 >= 1
5442 * v_0 = 0 and v_1 = 0 and v_2 <= -1
5446 __isl_give isl_vec
*isl_tab_basic_set_non_trivial_lexmin(
5447 __isl_take isl_basic_set
*bset
, int n_op
, int n_region
,
5448 struct isl_trivial_region
*region
,
5449 int (*conflict
)(int con
, void *user
), void *user
)
5451 struct isl_lexmin_data data
= { n_op
, n_region
, region
};
5457 if (init_lexmin_data(&data
, bset
) < 0)
5459 data
.tab
->conflict
= conflict
;
5460 data
.tab
->conflict_user
= user
;
5465 while (level
>= 0) {
5467 struct isl_local_region
*local
= &data
.local
[level
];
5469 next
= enter_level(level
, init
, &data
);
5472 if (next
== isl_next_done
)
5474 if (next
== isl_next_backtrack
) {
5480 if (better_next_side(local
, &data
) < 0)
5482 if (pick_side(local
, &data
) < 0)
5490 clear_lexmin_data(&data
);
5491 isl_basic_set_free(bset
);
5495 clear_lexmin_data(&data
);
5496 isl_basic_set_free(bset
);
5497 isl_vec_free(data
.sol
);
5501 /* Wrapper for a tableau that is used for computing
5502 * the lexicographically smallest rational point of a non-negative set.
5503 * This point is represented by the sample value of "tab",
5504 * unless "tab" is empty.
5506 struct isl_tab_lexmin
{
5508 struct isl_tab
*tab
;
5511 /* Free "tl" and return NULL.
5513 __isl_null isl_tab_lexmin
*isl_tab_lexmin_free(__isl_take isl_tab_lexmin
*tl
)
5517 isl_ctx_deref(tl
->ctx
);
5518 isl_tab_free(tl
->tab
);
5524 /* Construct an isl_tab_lexmin for computing
5525 * the lexicographically smallest rational point in "bset",
5526 * assuming that all variables are non-negative.
5528 __isl_give isl_tab_lexmin
*isl_tab_lexmin_from_basic_set(
5529 __isl_take isl_basic_set
*bset
)
5537 ctx
= isl_basic_set_get_ctx(bset
);
5538 tl
= isl_calloc_type(ctx
, struct isl_tab_lexmin
);
5543 tl
->tab
= tab_for_lexmin(bset
, NULL
, 0, 0);
5544 isl_basic_set_free(bset
);
5546 return isl_tab_lexmin_free(tl
);
5549 isl_basic_set_free(bset
);
5550 isl_tab_lexmin_free(tl
);
5554 /* Return the dimension of the set represented by "tl".
5556 int isl_tab_lexmin_dim(__isl_keep isl_tab_lexmin
*tl
)
5558 return tl
? tl
->tab
->n_var
: -1;
5561 /* Add the equality with coefficients "eq" to "tl", updating the optimal
5562 * solution if needed.
5563 * The equality is added as two opposite inequality constraints.
5565 __isl_give isl_tab_lexmin
*isl_tab_lexmin_add_eq(__isl_take isl_tab_lexmin
*tl
,
5571 return isl_tab_lexmin_free(tl
);
5573 if (isl_tab_extend_cons(tl
->tab
, 2) < 0)
5574 return isl_tab_lexmin_free(tl
);
5575 n_var
= tl
->tab
->n_var
;
5576 isl_seq_neg(eq
, eq
, 1 + n_var
);
5577 tl
->tab
= add_lexmin_ineq(tl
->tab
, eq
);
5578 isl_seq_neg(eq
, eq
, 1 + n_var
);
5579 tl
->tab
= add_lexmin_ineq(tl
->tab
, eq
);
5582 return isl_tab_lexmin_free(tl
);
5587 /* Add cuts to "tl" until the sample value reaches an integer value or
5588 * until the result becomes empty.
5590 __isl_give isl_tab_lexmin
*isl_tab_lexmin_cut_to_integer(
5591 __isl_take isl_tab_lexmin
*tl
)
5595 tl
->tab
= cut_to_integer_lexmin(tl
->tab
, CUT_ONE
);
5597 return isl_tab_lexmin_free(tl
);
5601 /* Return the lexicographically smallest rational point in the basic set
5602 * from which "tl" was constructed.
5603 * If the original input was empty, then return a zero-length vector.
5605 __isl_give isl_vec
*isl_tab_lexmin_get_solution(__isl_keep isl_tab_lexmin
*tl
)
5610 return isl_vec_alloc(tl
->ctx
, 0);
5612 return isl_tab_get_sample_value(tl
->tab
);
5615 struct isl_sol_pma
{
5617 isl_pw_multi_aff
*pma
;
5621 static void sol_pma_free(struct isl_sol
*sol
)
5623 struct isl_sol_pma
*sol_pma
= (struct isl_sol_pma
*) sol
;
5624 isl_pw_multi_aff_free(sol_pma
->pma
);
5625 isl_set_free(sol_pma
->empty
);
5628 /* This function is called for parts of the context where there is
5629 * no solution, with "bset" corresponding to the context tableau.
5630 * Simply add the basic set to the set "empty".
5632 static void sol_pma_add_empty(struct isl_sol_pma
*sol
,
5633 __isl_take isl_basic_set
*bset
)
5635 if (!bset
|| !sol
->empty
)
5638 sol
->empty
= isl_set_grow(sol
->empty
, 1);
5639 bset
= isl_basic_set_simplify(bset
);
5640 bset
= isl_basic_set_finalize(bset
);
5641 sol
->empty
= isl_set_add_basic_set(sol
->empty
, bset
);
5646 isl_basic_set_free(bset
);
5650 /* Given a basic set "dom" that represents the context and a tuple of
5651 * affine expressions "maff" defined over this domain, construct
5652 * an isl_pw_multi_aff with a single cell corresponding to "dom" and
5653 * the affine expressions in "maff".
5655 static void sol_pma_add(struct isl_sol_pma
*sol
,
5656 __isl_take isl_basic_set
*dom
, __isl_take isl_multi_aff
*maff
)
5658 isl_pw_multi_aff
*pma
;
5660 dom
= isl_basic_set_simplify(dom
);
5661 dom
= isl_basic_set_finalize(dom
);
5662 pma
= isl_pw_multi_aff_alloc(isl_set_from_basic_set(dom
), maff
);
5663 sol
->pma
= isl_pw_multi_aff_add_disjoint(sol
->pma
, pma
);
5668 static void sol_pma_add_empty_wrap(struct isl_sol
*sol
,
5669 __isl_take isl_basic_set
*bset
)
5671 sol_pma_add_empty((struct isl_sol_pma
*)sol
, bset
);
5674 static void sol_pma_add_wrap(struct isl_sol
*sol
,
5675 __isl_take isl_basic_set
*dom
, __isl_take isl_multi_aff
*ma
)
5677 sol_pma_add((struct isl_sol_pma
*)sol
, dom
, ma
);
5680 /* Construct an isl_sol_pma structure for accumulating the solution.
5681 * If track_empty is set, then we also keep track of the parts
5682 * of the context where there is no solution.
5683 * If max is set, then we are solving a maximization, rather than
5684 * a minimization problem, which means that the variables in the
5685 * tableau have value "M - x" rather than "M + x".
5687 static struct isl_sol
*sol_pma_init(__isl_keep isl_basic_map
*bmap
,
5688 __isl_take isl_basic_set
*dom
, int track_empty
, int max
)
5690 struct isl_sol_pma
*sol_pma
= NULL
;
5696 sol_pma
= isl_calloc_type(bmap
->ctx
, struct isl_sol_pma
);
5700 sol_pma
->sol
.free
= &sol_pma_free
;
5701 if (sol_init(&sol_pma
->sol
, bmap
, dom
, max
) < 0)
5703 sol_pma
->sol
.add
= &sol_pma_add_wrap
;
5704 sol_pma
->sol
.add_empty
= track_empty
? &sol_pma_add_empty_wrap
: NULL
;
5705 space
= isl_space_copy(sol_pma
->sol
.space
);
5706 sol_pma
->pma
= isl_pw_multi_aff_empty(space
);
5711 sol_pma
->empty
= isl_set_alloc_space(isl_basic_set_get_space(dom
),
5712 1, ISL_SET_DISJOINT
);
5713 if (!sol_pma
->empty
)
5717 isl_basic_set_free(dom
);
5718 return &sol_pma
->sol
;
5720 isl_basic_set_free(dom
);
5721 sol_free(&sol_pma
->sol
);
5725 /* Base case of isl_tab_basic_map_partial_lexopt, after removing
5726 * some obvious symmetries.
5728 * We call basic_map_partial_lexopt_base_sol and extract the results.
5730 static __isl_give isl_pw_multi_aff
*basic_map_partial_lexopt_base_pw_multi_aff(
5731 __isl_take isl_basic_map
*bmap
, __isl_take isl_basic_set
*dom
,
5732 __isl_give isl_set
**empty
, int max
)
5734 isl_pw_multi_aff
*result
= NULL
;
5735 struct isl_sol
*sol
;
5736 struct isl_sol_pma
*sol_pma
;
5738 sol
= basic_map_partial_lexopt_base_sol(bmap
, dom
, empty
, max
,
5742 sol_pma
= (struct isl_sol_pma
*) sol
;
5744 result
= isl_pw_multi_aff_copy(sol_pma
->pma
);
5746 *empty
= isl_set_copy(sol_pma
->empty
);
5747 sol_free(&sol_pma
->sol
);
5751 /* Given that the last input variable of "maff" represents the minimum
5752 * of some bounds, check whether we need to plug in the expression
5755 * In particular, check if the last input variable appears in any
5756 * of the expressions in "maff".
5758 static isl_bool
need_substitution(__isl_keep isl_multi_aff
*maff
)
5763 pos
= isl_multi_aff_dim(maff
, isl_dim_in
) - 1;
5765 for (i
= 0; i
< maff
->n
; ++i
) {
5768 involves
= isl_aff_involves_dims(maff
->u
.p
[i
],
5769 isl_dim_in
, pos
, 1);
5770 if (involves
< 0 || involves
)
5774 return isl_bool_false
;
5777 /* Given a set of upper bounds on the last "input" variable m,
5778 * construct a piecewise affine expression that selects
5779 * the minimal upper bound to m, i.e.,
5780 * divide the space into cells where one
5781 * of the upper bounds is smaller than all the others and select
5782 * this upper bound on that cell.
5784 * In particular, if there are n bounds b_i, then the result
5785 * consists of n cell, each one of the form
5787 * b_i <= b_j for j > i
5788 * b_i < b_j for j < i
5790 * The affine expression on this cell is
5794 static __isl_give isl_pw_aff
*set_minimum_pa(__isl_take isl_space
*space
,
5795 __isl_take isl_mat
*var
)
5798 isl_aff
*aff
= NULL
;
5799 isl_basic_set
*bset
= NULL
;
5800 isl_pw_aff
*paff
= NULL
;
5801 isl_space
*pw_space
;
5802 isl_local_space
*ls
= NULL
;
5807 ls
= isl_local_space_from_space(isl_space_copy(space
));
5808 pw_space
= isl_space_copy(space
);
5809 pw_space
= isl_space_from_domain(pw_space
);
5810 pw_space
= isl_space_add_dims(pw_space
, isl_dim_out
, 1);
5811 paff
= isl_pw_aff_alloc_size(pw_space
, var
->n_row
);
5813 for (i
= 0; i
< var
->n_row
; ++i
) {
5816 aff
= isl_aff_alloc(isl_local_space_copy(ls
));
5817 bset
= isl_basic_set_alloc_space(isl_space_copy(space
), 0,
5821 isl_int_set_si(aff
->v
->el
[0], 1);
5822 isl_seq_cpy(aff
->v
->el
+ 1, var
->row
[i
], var
->n_col
);
5823 isl_int_set_si(aff
->v
->el
[1 + var
->n_col
], 0);
5824 bset
= select_minimum(bset
, var
, i
);
5825 paff_i
= isl_pw_aff_alloc(isl_set_from_basic_set(bset
), aff
);
5826 paff
= isl_pw_aff_add_disjoint(paff
, paff_i
);
5829 isl_local_space_free(ls
);
5830 isl_space_free(space
);
5835 isl_basic_set_free(bset
);
5836 isl_pw_aff_free(paff
);
5837 isl_local_space_free(ls
);
5838 isl_space_free(space
);
5843 /* Given a piecewise multi-affine expression of which the last input variable
5844 * is the minimum of the bounds in "cst", plug in the value of the minimum.
5845 * This minimum expression is given in "min_expr_pa".
5846 * The set "min_expr" contains the same information, but in the form of a set.
5847 * The variable is subsequently projected out.
5849 * The implementation is similar to those of "split" and "split_domain".
5850 * If the variable appears in a given expression, then minimum expression
5851 * is plugged in. Otherwise, if the variable appears in the constraints
5852 * and a split is required, then the domain is split. Otherwise, no split
5855 static __isl_give isl_pw_multi_aff
*split_domain_pma(
5856 __isl_take isl_pw_multi_aff
*opt
, __isl_take isl_pw_aff
*min_expr_pa
,
5857 __isl_take isl_set
*min_expr
, __isl_take isl_mat
*cst
)
5862 isl_pw_multi_aff
*res
;
5864 if (!opt
|| !min_expr
|| !cst
)
5867 n_in
= isl_pw_multi_aff_dim(opt
, isl_dim_in
);
5868 space
= isl_pw_multi_aff_get_space(opt
);
5869 space
= isl_space_drop_dims(space
, isl_dim_in
, n_in
- 1, 1);
5870 res
= isl_pw_multi_aff_empty(space
);
5872 for (i
= 0; i
< opt
->n
; ++i
) {
5874 isl_pw_multi_aff
*pma
;
5876 pma
= isl_pw_multi_aff_alloc(isl_set_copy(opt
->p
[i
].set
),
5877 isl_multi_aff_copy(opt
->p
[i
].maff
));
5878 subs
= need_substitution(opt
->p
[i
].maff
);
5880 pma
= isl_pw_multi_aff_free(pma
);
5882 pma
= isl_pw_multi_aff_substitute(pma
,
5883 isl_dim_in
, n_in
- 1, min_expr_pa
);
5886 split
= need_split_set(opt
->p
[i
].set
, cst
);
5888 pma
= isl_pw_multi_aff_free(pma
);
5890 pma
= isl_pw_multi_aff_intersect_domain(pma
,
5891 isl_set_copy(min_expr
));
5893 pma
= isl_pw_multi_aff_project_out(pma
,
5894 isl_dim_in
, n_in
- 1, 1);
5896 res
= isl_pw_multi_aff_add_disjoint(res
, pma
);
5899 isl_pw_multi_aff_free(opt
);
5900 isl_pw_aff_free(min_expr_pa
);
5901 isl_set_free(min_expr
);
5905 isl_pw_multi_aff_free(opt
);
5906 isl_pw_aff_free(min_expr_pa
);
5907 isl_set_free(min_expr
);
5912 static __isl_give isl_pw_multi_aff
*basic_map_partial_lexopt_pw_multi_aff(
5913 __isl_take isl_basic_map
*bmap
, __isl_take isl_basic_set
*dom
,
5914 __isl_give isl_set
**empty
, int max
);
5916 /* This function is called from basic_map_partial_lexopt_symm.
5917 * The last variable of "bmap" and "dom" corresponds to the minimum
5918 * of the bounds in "cst". "map_space" is the space of the original
5919 * input relation (of basic_map_partial_lexopt_symm) and "set_space"
5920 * is the space of the original domain.
5922 * We recursively call basic_map_partial_lexopt and then plug in
5923 * the definition of the minimum in the result.
5925 static __isl_give isl_pw_multi_aff
*
5926 basic_map_partial_lexopt_symm_core_pw_multi_aff(
5927 __isl_take isl_basic_map
*bmap
, __isl_take isl_basic_set
*dom
,
5928 __isl_give isl_set
**empty
, int max
, __isl_take isl_mat
*cst
,
5929 __isl_take isl_space
*map_space
, __isl_take isl_space
*set_space
)
5931 isl_pw_multi_aff
*opt
;
5932 isl_pw_aff
*min_expr_pa
;
5935 min_expr
= set_minimum(isl_basic_set_get_space(dom
), isl_mat_copy(cst
));
5936 min_expr_pa
= set_minimum_pa(isl_basic_set_get_space(dom
),
5939 opt
= basic_map_partial_lexopt_pw_multi_aff(bmap
, dom
, empty
, max
);
5942 *empty
= split(*empty
,
5943 isl_set_copy(min_expr
), isl_mat_copy(cst
));
5944 *empty
= isl_set_reset_space(*empty
, set_space
);
5947 opt
= split_domain_pma(opt
, min_expr_pa
, min_expr
, cst
);
5948 opt
= isl_pw_multi_aff_reset_space(opt
, map_space
);
5954 #define TYPE isl_pw_multi_aff
5956 #define SUFFIX _pw_multi_aff
5957 #include "isl_tab_lexopt_templ.c"