blob: 2f39a08f845dc046aa912c74e9b641b79077e69c [file] [log] [blame]
/*
* Copyright 2008-2009 Katholieke Universiteit Leuven
* Copyright 2010 INRIA Saclay
*
* Use of this software is governed by the GNU LGPLv2.1 license
*
* Written by Sven Verdoolaege, K.U.Leuven, Departement
* Computerwetenschappen, Celestijnenlaan 200A, B-3001 Leuven, Belgium
* and INRIA Saclay - Ile-de-France, Parc Club Orsay Universite,
* ZAC des vignes, 4 rue Jacques Monod, 91893 Orsay, France
*/
#include <isl_ctx_private.h>
#include "isl_map_private.h"
#include <isl/seq.h>
#include "isl_tab.h"
#include "isl_sample.h"
#include <isl_mat_private.h>
#include <isl_aff_private.h>
#include <isl_config.h>
/*
* The implementation of parametric integer linear programming in this file
* was inspired by the paper "Parametric Integer Programming" and the
* report "Solving systems of affine (in)equalities" by Paul Feautrier
* (and others).
*
* The strategy used for obtaining a feasible solution is different
* from the one used in isl_tab.c. In particular, in isl_tab.c,
* upon finding a constraint that is not yet satisfied, we pivot
* in a row that increases the constant term of the row holding the
* constraint, making sure the sample solution remains feasible
* for all the constraints it already satisfied.
* Here, we always pivot in the row holding the constraint,
* choosing a column that induces the lexicographically smallest
* increment to the sample solution.
*
* By starting out from a sample value that is lexicographically
* smaller than any integer point in the problem space, the first
* feasible integer sample point we find will also be the lexicographically
* smallest. If all variables can be assumed to be non-negative,
* then the initial sample value may be chosen equal to zero.
* However, we will not make this assumption. Instead, we apply
* the "big parameter" trick. Any variable x is then not directly
* used in the tableau, but instead it is represented by another
* variable x' = M + x, where M is an arbitrarily large (positive)
* value. x' is therefore always non-negative, whatever the value of x.
* Taking as initial sample value x' = 0 corresponds to x = -M,
* which is always smaller than any possible value of x.
*
* The big parameter trick is used in the main tableau and
* also in the context tableau if isl_context_lex is used.
* In this case, each tableaus has its own big parameter.
* Before doing any real work, we check if all the parameters
* happen to be non-negative. If so, we drop the column corresponding
* to M from the initial context tableau.
* If isl_context_gbr is used, then the big parameter trick is only
* used in the main tableau.
*/
struct isl_context;
struct isl_context_op {
/* detect nonnegative parameters in context and mark them in tab */
struct isl_tab *(*detect_nonnegative_parameters)(
struct isl_context *context, struct isl_tab *tab);
/* return temporary reference to basic set representation of context */
struct isl_basic_set *(*peek_basic_set)(struct isl_context *context);
/* return temporary reference to tableau representation of context */
struct isl_tab *(*peek_tab)(struct isl_context *context);
/* add equality; check is 1 if eq may not be valid;
* update is 1 if we may want to call ineq_sign on context later.
*/
void (*add_eq)(struct isl_context *context, isl_int *eq,
int check, int update);
/* add inequality; check is 1 if ineq may not be valid;
* update is 1 if we may want to call ineq_sign on context later.
*/
void (*add_ineq)(struct isl_context *context, isl_int *ineq,
int check, int update);
/* check sign of ineq based on previous information.
* strict is 1 if saturation should be treated as a positive sign.
*/
enum isl_tab_row_sign (*ineq_sign)(struct isl_context *context,
isl_int *ineq, int strict);
/* check if inequality maintains feasibility */
int (*test_ineq)(struct isl_context *context, isl_int *ineq);
/* return index of a div that corresponds to "div" */
int (*get_div)(struct isl_context *context, struct isl_tab *tab,
struct isl_vec *div);
/* add div "div" to context and return non-negativity */
int (*add_div)(struct isl_context *context, struct isl_vec *div);
int (*detect_equalities)(struct isl_context *context,
struct isl_tab *tab);
/* return row index of "best" split */
int (*best_split)(struct isl_context *context, struct isl_tab *tab);
/* check if context has already been determined to be empty */
int (*is_empty)(struct isl_context *context);
/* check if context is still usable */
int (*is_ok)(struct isl_context *context);
/* save a copy/snapshot of context */
void *(*save)(struct isl_context *context);
/* restore saved context */
void (*restore)(struct isl_context *context, void *);
/* invalidate context */
void (*invalidate)(struct isl_context *context);
/* free context */
void (*free)(struct isl_context *context);
};
struct isl_context {
struct isl_context_op *op;
};
struct isl_context_lex {
struct isl_context context;
struct isl_tab *tab;
};
struct isl_partial_sol {
int level;
struct isl_basic_set *dom;
struct isl_mat *M;
struct isl_partial_sol *next;
};
struct isl_sol;
struct isl_sol_callback {
struct isl_tab_callback callback;
struct isl_sol *sol;
};
/* isl_sol is an interface for constructing a solution to
* a parametric integer linear programming problem.
* Every time the algorithm reaches a state where a solution
* can be read off from the tableau (including cases where the tableau
* is empty), the function "add" is called on the isl_sol passed
* to find_solutions_main.
*
* The context tableau is owned by isl_sol and is updated incrementally.
*
* There are currently two implementations of this interface,
* isl_sol_map, which simply collects the solutions in an isl_map
* and (optionally) the parts of the context where there is no solution
* in an isl_set, and
* isl_sol_for, which calls a user-defined function for each part of
* the solution.
*/
struct isl_sol {
int error;
int rational;
int level;
int max;
int n_out;
struct isl_context *context;
struct isl_partial_sol *partial;
void (*add)(struct isl_sol *sol,
struct isl_basic_set *dom, struct isl_mat *M);
void (*add_empty)(struct isl_sol *sol, struct isl_basic_set *bset);
void (*free)(struct isl_sol *sol);
struct isl_sol_callback dec_level;
};
static void sol_free(struct isl_sol *sol)
{
struct isl_partial_sol *partial, *next;
if (!sol)
return;
for (partial = sol->partial; partial; partial = next) {
next = partial->next;
isl_basic_set_free(partial->dom);
isl_mat_free(partial->M);
free(partial);
}
sol->free(sol);
}
/* Push a partial solution represented by a domain and mapping M
* onto the stack of partial solutions.
*/
static void sol_push_sol(struct isl_sol *sol,
struct isl_basic_set *dom, struct isl_mat *M)
{
struct isl_partial_sol *partial;
if (sol->error || !dom)
goto error;
partial = isl_alloc_type(dom->ctx, struct isl_partial_sol);
if (!partial)
goto error;
partial->level = sol->level;
partial->dom = dom;
partial->M = M;
partial->next = sol->partial;
sol->partial = partial;
return;
error:
isl_basic_set_free(dom);
sol->error = 1;
}
/* Pop one partial solution from the partial solution stack and
* pass it on to sol->add or sol->add_empty.
*/
static void sol_pop_one(struct isl_sol *sol)
{
struct isl_partial_sol *partial;
partial = sol->partial;
sol->partial = partial->next;
if (partial->M)
sol->add(sol, partial->dom, partial->M);
else
sol->add_empty(sol, partial->dom);
free(partial);
}
/* Return a fresh copy of the domain represented by the context tableau.
*/
static struct isl_basic_set *sol_domain(struct isl_sol *sol)
{
struct isl_basic_set *bset;
if (sol->error)
return NULL;
bset = isl_basic_set_dup(sol->context->op->peek_basic_set(sol->context));
bset = isl_basic_set_update_from_tab(bset,
sol->context->op->peek_tab(sol->context));
return bset;
}
/* Check whether two partial solutions have the same mapping, where n_div
* is the number of divs that the two partial solutions have in common.
*/
static int same_solution(struct isl_partial_sol *s1, struct isl_partial_sol *s2,
unsigned n_div)
{
int i;
unsigned dim;
if (!s1->M != !s2->M)
return 0;
if (!s1->M)
return 1;
dim = isl_basic_set_total_dim(s1->dom) - s1->dom->n_div;
for (i = 0; i < s1->M->n_row; ++i) {
if (isl_seq_first_non_zero(s1->M->row[i]+1+dim+n_div,
s1->M->n_col-1-dim-n_div) != -1)
return 0;
if (isl_seq_first_non_zero(s2->M->row[i]+1+dim+n_div,
s2->M->n_col-1-dim-n_div) != -1)
return 0;
if (!isl_seq_eq(s1->M->row[i], s2->M->row[i], 1+dim+n_div))
return 0;
}
return 1;
}
/* Pop all solutions from the partial solution stack that were pushed onto
* the stack at levels that are deeper than the current level.
* If the two topmost elements on the stack have the same level
* and represent the same solution, then their domains are combined.
* This combined domain is the same as the current context domain
* as sol_pop is called each time we move back to a higher level.
*/
static void sol_pop(struct isl_sol *sol)
{
struct isl_partial_sol *partial;
unsigned n_div;
if (sol->error)
return;
if (sol->level == 0) {
for (partial = sol->partial; partial; partial = sol->partial)
sol_pop_one(sol);
return;
}
partial = sol->partial;
if (!partial)
return;
if (partial->level <= sol->level)
return;
if (partial->next && partial->next->level == partial->level) {
n_div = isl_basic_set_dim(
sol->context->op->peek_basic_set(sol->context),
isl_dim_div);
if (!same_solution(partial, partial->next, n_div)) {
sol_pop_one(sol);
sol_pop_one(sol);
} else {
struct isl_basic_set *bset;
bset = sol_domain(sol);
isl_basic_set_free(partial->next->dom);
partial->next->dom = bset;
partial->next->level = sol->level;
sol->partial = partial->next;
isl_basic_set_free(partial->dom);
isl_mat_free(partial->M);
free(partial);
}
} else
sol_pop_one(sol);
}
static void sol_dec_level(struct isl_sol *sol)
{
if (sol->error)
return;
sol->level--;
sol_pop(sol);
}
static int sol_dec_level_wrap(struct isl_tab_callback *cb)
{
struct isl_sol_callback *callback = (struct isl_sol_callback *)cb;
sol_dec_level(callback->sol);
return callback->sol->error ? -1 : 0;
}
/* Move down to next level and push callback onto context tableau
* to decrease the level again when it gets rolled back across
* the current state. That is, dec_level will be called with
* the context tableau in the same state as it is when inc_level
* is called.
*/
static void sol_inc_level(struct isl_sol *sol)
{
struct isl_tab *tab;
if (sol->error)
return;
sol->level++;
tab = sol->context->op->peek_tab(sol->context);
if (isl_tab_push_callback(tab, &sol->dec_level.callback) < 0)
sol->error = 1;
}
static void scale_rows(struct isl_mat *mat, isl_int m, int n_row)
{
int i;
if (isl_int_is_one(m))
return;
for (i = 0; i < n_row; ++i)
isl_seq_scale(mat->row[i], mat->row[i], m, mat->n_col);
}
/* Add the solution identified by the tableau and the context tableau.
*
* The layout of the variables is as follows.
* tab->n_var is equal to the total number of variables in the input
* map (including divs that were copied from the context)
* + the number of extra divs constructed
* Of these, the first tab->n_param and the last tab->n_div variables
* correspond to the variables in the context, i.e.,
* tab->n_param + tab->n_div = context_tab->n_var
* tab->n_param is equal to the number of parameters and input
* dimensions in the input map
* tab->n_div is equal to the number of divs in the context
*
* If there is no solution, then call add_empty with a basic set
* that corresponds to the context tableau. (If add_empty is NULL,
* then do nothing).
*
* If there is a solution, then first construct a matrix that maps
* all dimensions of the context to the output variables, i.e.,
* the output dimensions in the input map.
* The divs in the input map (if any) that do not correspond to any
* div in the context do not appear in the solution.
* The algorithm will make sure that they have an integer value,
* but these values themselves are of no interest.
* We have to be careful not to drop or rearrange any divs in the
* context because that would change the meaning of the matrix.
*
* To extract the value of the output variables, it should be noted
* that we always use a big parameter M in the main tableau and so
* the variable stored in this tableau is not an output variable x itself, but
* x' = M + x (in case of minimization)
* or
* x' = M - x (in case of maximization)
* If x' appears in a column, then its optimal value is zero,
* which means that the optimal value of x is an unbounded number
* (-M for minimization and M for maximization).
* We currently assume that the output dimensions in the original map
* are bounded, so this cannot occur.
* Similarly, when x' appears in a row, then the coefficient of M in that
* row is necessarily 1.
* If the row in the tableau represents
* d x' = c + d M + e(y)
* then, in case of minimization, the corresponding row in the matrix
* will be
* a c + a e(y)
* with a d = m, the (updated) common denominator of the matrix.
* In case of maximization, the row will be
* -a c - a e(y)
*/
static void sol_add(struct isl_sol *sol, struct isl_tab *tab)
{
struct isl_basic_set *bset = NULL;
struct isl_mat *mat = NULL;
unsigned off;
int row;
isl_int m;
if (sol->error || !tab)
goto error;
if (tab->empty && !sol->add_empty)
return;
bset = sol_domain(sol);
if (tab->empty) {
sol_push_sol(sol, bset, NULL);
return;
}
off = 2 + tab->M;
mat = isl_mat_alloc(tab->mat->ctx, 1 + sol->n_out,
1 + tab->n_param + tab->n_div);
if (!mat)
goto error;
isl_int_init(m);
isl_seq_clr(mat->row[0] + 1, mat->n_col - 1);
isl_int_set_si(mat->row[0][0], 1);
for (row = 0; row < sol->n_out; ++row) {
int i = tab->n_param + row;
int r, j;
isl_seq_clr(mat->row[1 + row], mat->n_col);
if (!tab->var[i].is_row) {
if (tab->M)
isl_die(mat->ctx, isl_error_invalid,
"unbounded optimum", goto error2);
continue;
}
r = tab->var[i].index;
if (tab->M &&
isl_int_ne(tab->mat->row[r][2], tab->mat->row[r][0]))
isl_die(mat->ctx, isl_error_invalid,
"unbounded optimum", goto error2);
isl_int_gcd(m, mat->row[0][0], tab->mat->row[r][0]);
isl_int_divexact(m, tab->mat->row[r][0], m);
scale_rows(mat, m, 1 + row);
isl_int_divexact(m, mat->row[0][0], tab->mat->row[r][0]);
isl_int_mul(mat->row[1 + row][0], m, tab->mat->row[r][1]);
for (j = 0; j < tab->n_param; ++j) {
int col;
if (tab->var[j].is_row)
continue;
col = tab->var[j].index;
isl_int_mul(mat->row[1 + row][1 + j], m,
tab->mat->row[r][off + col]);
}
for (j = 0; j < tab->n_div; ++j) {
int col;
if (tab->var[tab->n_var - tab->n_div+j].is_row)
continue;
col = tab->var[tab->n_var - tab->n_div+j].index;
isl_int_mul(mat->row[1 + row][1 + tab->n_param + j], m,
tab->mat->row[r][off + col]);
}
if (sol->max)
isl_seq_neg(mat->row[1 + row], mat->row[1 + row],
mat->n_col);
}
isl_int_clear(m);
sol_push_sol(sol, bset, mat);
return;
error2:
isl_int_clear(m);
error:
isl_basic_set_free(bset);
isl_mat_free(mat);
sol->error = 1;
}
struct isl_sol_map {
struct isl_sol sol;
struct isl_map *map;
struct isl_set *empty;
};
static void sol_map_free(struct isl_sol_map *sol_map)
{
if (!sol_map)
return;
if (sol_map->sol.context)
sol_map->sol.context->op->free(sol_map->sol.context);
isl_map_free(sol_map->map);
isl_set_free(sol_map->empty);
free(sol_map);
}
static void sol_map_free_wrap(struct isl_sol *sol)
{
sol_map_free((struct isl_sol_map *)sol);
}
/* This function is called for parts of the context where there is
* no solution, with "bset" corresponding to the context tableau.
* Simply add the basic set to the set "empty".
*/
static void sol_map_add_empty(struct isl_sol_map *sol,
struct isl_basic_set *bset)
{
if (!bset)
goto error;
isl_assert(bset->ctx, sol->empty, goto error);
sol->empty = isl_set_grow(sol->empty, 1);
bset = isl_basic_set_simplify(bset);
bset = isl_basic_set_finalize(bset);
sol->empty = isl_set_add_basic_set(sol->empty, isl_basic_set_copy(bset));
if (!sol->empty)
goto error;
isl_basic_set_free(bset);
return;
error:
isl_basic_set_free(bset);
sol->sol.error = 1;
}
static void sol_map_add_empty_wrap(struct isl_sol *sol,
struct isl_basic_set *bset)
{
sol_map_add_empty((struct isl_sol_map *)sol, bset);
}
/* Add bset to sol's empty, but only if we are actually collecting
* the empty set.
*/
static void sol_map_add_empty_if_needed(struct isl_sol_map *sol,
struct isl_basic_set *bset)
{
if (sol->empty)
sol_map_add_empty(sol, bset);
else
isl_basic_set_free(bset);
}
/* Given a basic map "dom" that represents the context and an affine
* matrix "M" that maps the dimensions of the context to the
* output variables, construct a basic map with the same parameters
* and divs as the context, the dimensions of the context as input
* dimensions and a number of output dimensions that is equal to
* the number of output dimensions in the input map.
*
* The constraints and divs of the context are simply copied
* from "dom". For each row
* x = c + e(y)
* an equality
* c + e(y) - d x = 0
* is added, with d the common denominator of M.
*/
static void sol_map_add(struct isl_sol_map *sol,
struct isl_basic_set *dom, struct isl_mat *M)
{
int i;
struct isl_basic_map *bmap = NULL;
unsigned n_eq;
unsigned n_ineq;
unsigned nparam;
unsigned total;
unsigned n_div;
unsigned n_out;
if (sol->sol.error || !dom || !M)
goto error;
n_out = sol->sol.n_out;
n_eq = dom->n_eq + n_out;
n_ineq = dom->n_ineq;
n_div = dom->n_div;
nparam = isl_basic_set_total_dim(dom) - n_div;
total = isl_map_dim(sol->map, isl_dim_all);
bmap = isl_basic_map_alloc_dim(isl_map_get_dim(sol->map),
n_div, n_eq, 2 * n_div + n_ineq);
if (!bmap)
goto error;
if (sol->sol.rational)
ISL_F_SET(bmap, ISL_BASIC_MAP_RATIONAL);
for (i = 0; i < dom->n_div; ++i) {
int k = isl_basic_map_alloc_div(bmap);
if (k < 0)
goto error;
isl_seq_cpy(bmap->div[k], dom->div[i], 1 + 1 + nparam);
isl_seq_clr(bmap->div[k] + 1 + 1 + nparam, total - nparam);
isl_seq_cpy(bmap->div[k] + 1 + 1 + total,
dom->div[i] + 1 + 1 + nparam, i);
}
for (i = 0; i < dom->n_eq; ++i) {
int k = isl_basic_map_alloc_equality(bmap);
if (k < 0)
goto error;
isl_seq_cpy(bmap->eq[k], dom->eq[i], 1 + nparam);
isl_seq_clr(bmap->eq[k] + 1 + nparam, total - nparam);
isl_seq_cpy(bmap->eq[k] + 1 + total,
dom->eq[i] + 1 + nparam, n_div);
}
for (i = 0; i < dom->n_ineq; ++i) {
int k = isl_basic_map_alloc_inequality(bmap);
if (k < 0)
goto error;
isl_seq_cpy(bmap->ineq[k], dom->ineq[i], 1 + nparam);
isl_seq_clr(bmap->ineq[k] + 1 + nparam, total - nparam);
isl_seq_cpy(bmap->ineq[k] + 1 + total,
dom->ineq[i] + 1 + nparam, n_div);
}
for (i = 0; i < M->n_row - 1; ++i) {
int k = isl_basic_map_alloc_equality(bmap);
if (k < 0)
goto error;
isl_seq_cpy(bmap->eq[k], M->row[1 + i], 1 + nparam);
isl_seq_clr(bmap->eq[k] + 1 + nparam, n_out);
isl_int_neg(bmap->eq[k][1 + nparam + i], M->row[0][0]);
isl_seq_cpy(bmap->eq[k] + 1 + nparam + n_out,
M->row[1 + i] + 1 + nparam, n_div);
}
bmap = isl_basic_map_simplify(bmap);
bmap = isl_basic_map_finalize(bmap);
sol->map = isl_map_grow(sol->map, 1);
sol->map = isl_map_add_basic_map(sol->map, bmap);
if (!sol->map)
goto error;
isl_basic_set_free(dom);
isl_mat_free(M);
return;
error:
isl_basic_set_free(dom);
isl_mat_free(M);
isl_basic_map_free(bmap);
sol->sol.error = 1;
}
static void sol_map_add_wrap(struct isl_sol *sol,
struct isl_basic_set *dom, struct isl_mat *M)
{
sol_map_add((struct isl_sol_map *)sol, dom, M);
}
/* Store the "parametric constant" of row "row" of tableau "tab" in "line",
* i.e., the constant term and the coefficients of all variables that
* appear in the context tableau.
* Note that the coefficient of the big parameter M is NOT copied.
* The context tableau may not have a big parameter and even when it
* does, it is a different big parameter.
*/
static void get_row_parameter_line(struct isl_tab *tab, int row, isl_int *line)
{
int i;
unsigned off = 2 + tab->M;
isl_int_set(line[0], tab->mat->row[row][1]);
for (i = 0; i < tab->n_param; ++i) {
if (tab->var[i].is_row)
isl_int_set_si(line[1 + i], 0);
else {
int col = tab->var[i].index;
isl_int_set(line[1 + i], tab->mat->row[row][off + col]);
}
}
for (i = 0; i < tab->n_div; ++i) {
if (tab->var[tab->n_var - tab->n_div + i].is_row)
isl_int_set_si(line[1 + tab->n_param + i], 0);
else {
int col = tab->var[tab->n_var - tab->n_div + i].index;
isl_int_set(line[1 + tab->n_param + i],
tab->mat->row[row][off + col]);
}
}
}
/* Check if rows "row1" and "row2" have identical "parametric constants",
* as explained above.
* In this case, we also insist that the coefficients of the big parameter
* be the same as the values of the constants will only be the same
* if these coefficients are also the same.
*/
static int identical_parameter_line(struct isl_tab *tab, int row1, int row2)
{
int i;
unsigned off = 2 + tab->M;
if (isl_int_ne(tab->mat->row[row1][1], tab->mat->row[row2][1]))
return 0;
if (tab->M && isl_int_ne(tab->mat->row[row1][2],
tab->mat->row[row2][2]))
return 0;
for (i = 0; i < tab->n_param + tab->n_div; ++i) {
int pos = i < tab->n_param ? i :
tab->n_var - tab->n_div + i - tab->n_param;
int col;
if (tab->var[pos].is_row)
continue;
col = tab->var[pos].index;
if (isl_int_ne(tab->mat->row[row1][off + col],
tab->mat->row[row2][off + col]))
return 0;
}
return 1;
}
/* Return an inequality that expresses that the "parametric constant"
* should be non-negative.
* This function is only called when the coefficient of the big parameter
* is equal to zero.
*/
static struct isl_vec *get_row_parameter_ineq(struct isl_tab *tab, int row)
{
struct isl_vec *ineq;
ineq = isl_vec_alloc(tab->mat->ctx, 1 + tab->n_param + tab->n_div);
if (!ineq)
return NULL;
get_row_parameter_line(tab, row, ineq->el);
if (ineq)
ineq = isl_vec_normalize(ineq);
return ineq;
}
/* Return a integer division for use in a parametric cut based on the given row.
* In particular, let the parametric constant of the row be
*
* \sum_i a_i y_i
*
* where y_0 = 1, but none of the y_i corresponds to the big parameter M.
* The div returned is equal to
*
* floor(\sum_i {-a_i} y_i) = floor((\sum_i (-a_i mod d) y_i)/d)
*/
static struct isl_vec *get_row_parameter_div(struct isl_tab *tab, int row)
{
struct isl_vec *div;
div = isl_vec_alloc(tab->mat->ctx, 1 + 1 + tab->n_param + tab->n_div);
if (!div)
return NULL;
isl_int_set(div->el[0], tab->mat->row[row][0]);
get_row_parameter_line(tab, row, div->el + 1);
div = isl_vec_normalize(div);
isl_seq_neg(div->el + 1, div->el + 1, div->size - 1);
isl_seq_fdiv_r(div->el + 1, div->el + 1, div->el[0], div->size - 1);
return div;
}
/* Return a integer division for use in transferring an integrality constraint
* to the context.
* In particular, let the parametric constant of the row be
*
* \sum_i a_i y_i
*
* where y_0 = 1, but none of the y_i corresponds to the big parameter M.
* The the returned div is equal to
*
* floor(\sum_i {a_i} y_i) = floor((\sum_i (a_i mod d) y_i)/d)
*/
static struct isl_vec *get_row_split_div(struct isl_tab *tab, int row)
{
struct isl_vec *div;
div = isl_vec_alloc(tab->mat->ctx, 1 + 1 + tab->n_param + tab->n_div);
if (!div)
return NULL;
isl_int_set(div->el[0], tab->mat->row[row][0]);
get_row_parameter_line(tab, row, div->el + 1);
div = isl_vec_normalize(div);
isl_seq_fdiv_r(div->el + 1, div->el + 1, div->el[0], div->size - 1);
return div;
}
/* Construct and return an inequality that expresses an upper bound
* on the given div.
* In particular, if the div is given by
*
* d = floor(e/m)
*
* then the inequality expresses
*
* m d <= e
*/
static struct isl_vec *ineq_for_div(struct isl_basic_set *bset, unsigned div)
{
unsigned total;
unsigned div_pos;
struct isl_vec *ineq;
if (!bset)
return NULL;
total = isl_basic_set_total_dim(bset);
div_pos = 1 + total - bset->n_div + div;
ineq = isl_vec_alloc(bset->ctx, 1 + total);
if (!ineq)
return NULL;
isl_seq_cpy(ineq->el, bset->div[div] + 1, 1 + total);
isl_int_neg(ineq->el[div_pos], bset->div[div][0]);
return ineq;
}
/* Given a row in the tableau and a div that was created
* using get_row_split_div and that been constrained to equality, i.e.,
*
* d = floor(\sum_i {a_i} y_i) = \sum_i {a_i} y_i
*
* replace the expression "\sum_i {a_i} y_i" in the row by d,
* i.e., we subtract "\sum_i {a_i} y_i" and add 1 d.
* The coefficients of the non-parameters in the tableau have been
* verified to be integral. We can therefore simply replace coefficient b
* by floor(b). For the coefficients of the parameters we have
* floor(a_i) = a_i - {a_i}, while for the other coefficients, we have
* floor(b) = b.
*/
static struct isl_tab *set_row_cst_to_div(struct isl_tab *tab, int row, int div)
{
isl_seq_fdiv_q(tab->mat->row[row] + 1, tab->mat->row[row] + 1,
tab->mat->row[row][0], 1 + tab->M + tab->n_col);
isl_int_set_si(tab->mat->row[row][0], 1);
if (tab->var[tab->n_var - tab->n_div + div].is_row) {
int drow = tab->var[tab->n_var - tab->n_div + div].index;
isl_assert(tab->mat->ctx,
isl_int_is_one(tab->mat->row[drow][0]), goto error);
isl_seq_combine(tab->mat->row[row] + 1,
tab->mat->ctx->one, tab->mat->row[row] + 1,
tab->mat->ctx->one, tab->mat->row[drow] + 1,
1 + tab->M + tab->n_col);
} else {
int dcol = tab->var[tab->n_var - tab->n_div + div].index;
isl_int_set_si(tab->mat->row[row][2 + tab->M + dcol], 1);
}
return tab;
error:
isl_tab_free(tab);
return NULL;
}
/* Check if the (parametric) constant of the given row is obviously
* negative, meaning that we don't need to consult the context tableau.
* If there is a big parameter and its coefficient is non-zero,
* then this coefficient determines the outcome.
* Otherwise, we check whether the constant is negative and
* all non-zero coefficients of parameters are negative and
* belong to non-negative parameters.
*/
static int is_obviously_neg(struct isl_tab *tab, int row)
{
int i;
int col;
unsigned off = 2 + tab->M;
if (tab->M) {
if (isl_int_is_pos(tab->mat->row[row][2]))
return 0;
if (isl_int_is_neg(tab->mat->row[row][2]))
return 1;
}
if (isl_int_is_nonneg(tab->mat->row[row][1]))
return 0;
for (i = 0; i < tab->n_param; ++i) {
/* Eliminated parameter */
if (tab->var[i].is_row)
continue;
col = tab->var[i].index;
if (isl_int_is_zero(tab->mat->row[row][off + col]))
continue;
if (!tab->var[i].is_nonneg)
return 0;
if (isl_int_is_pos(tab->mat->row[row][off + col]))
return 0;
}
for (i = 0; i < tab->n_div; ++i) {
if (tab->var[tab->n_var - tab->n_div + i].is_row)
continue;
col = tab->var[tab->n_var - tab->n_div + i].index;
if (isl_int_is_zero(tab->mat->row[row][off + col]))
continue;
if (!tab->var[tab->n_var - tab->n_div + i].is_nonneg)
return 0;
if (isl_int_is_pos(tab->mat->row[row][off + col]))
return 0;
}
return 1;
}
/* Check if the (parametric) constant of the given row is obviously
* non-negative, meaning that we don't need to consult the context tableau.
* If there is a big parameter and its coefficient is non-zero,
* then this coefficient determines the outcome.
* Otherwise, we check whether the constant is non-negative and
* all non-zero coefficients of parameters are positive and
* belong to non-negative parameters.
*/
static int is_obviously_nonneg(struct isl_tab *tab, int row)
{
int i;
int col;
unsigned off = 2 + tab->M;
if (tab->M) {
if (isl_int_is_pos(tab->mat->row[row][2]))
return 1;
if (isl_int_is_neg(tab->mat->row[row][2]))
return 0;
}
if (isl_int_is_neg(tab->mat->row[row][1]))
return 0;
for (i = 0; i < tab->n_param; ++i) {
/* Eliminated parameter */
if (tab->var[i].is_row)
continue;
col = tab->var[i].index;
if (isl_int_is_zero(tab->mat->row[row][off + col]))
continue;
if (!tab->var[i].is_nonneg)
return 0;
if (isl_int_is_neg(tab->mat->row[row][off + col]))
return 0;
}
for (i = 0; i < tab->n_div; ++i) {
if (tab->var[tab->n_var - tab->n_div + i].is_row)
continue;
col = tab->var[tab->n_var - tab->n_div + i].index;
if (isl_int_is_zero(tab->mat->row[row][off + col]))
continue;
if (!tab->var[tab->n_var - tab->n_div + i].is_nonneg)
return 0;
if (isl_int_is_neg(tab->mat->row[row][off + col]))
return 0;
}
return 1;
}
/* Given a row r and two columns, return the column that would
* lead to the lexicographically smallest increment in the sample
* solution when leaving the basis in favor of the row.
* Pivoting with column c will increment the sample value by a non-negative
* constant times a_{V,c}/a_{r,c}, with a_{V,c} the elements of column c
* corresponding to the non-parametric variables.
* If variable v appears in a column c_v, the a_{v,c} = 1 iff c = c_v,
* with all other entries in this virtual row equal to zero.
* If variable v appears in a row, then a_{v,c} is the element in column c
* of that row.
*
* Let v be the first variable with a_{v,c1}/a_{r,c1} != a_{v,c2}/a_{r,c2}.
* Then if a_{v,c1}/a_{r,c1} < a_{v,c2}/a_{r,c2}, i.e.,
* a_{v,c2} a_{r,c1} - a_{v,c1} a_{r,c2} > 0, c1 results in the minimal
* increment. Otherwise, it's c2.
*/
static int lexmin_col_pair(struct isl_tab *tab,
int row, int col1, int col2, isl_int tmp)
{
int i;
isl_int *tr;
tr = tab->mat->row[row] + 2 + tab->M;
for (i = tab->n_param; i < tab->n_var - tab->n_div; ++i) {
int s1, s2;
isl_int *r;
if (!tab->var[i].is_row) {
if (tab->var[i].index == col1)
return col2;
if (tab->var[i].index == col2)
return col1;
continue;
}
if (tab->var[i].index == row)
continue;
r = tab->mat->row[tab->var[i].index] + 2 + tab->M;
s1 = isl_int_sgn(r[col1]);
s2 = isl_int_sgn(r[col2]);
if (s1 == 0 && s2 == 0)
continue;
if (s1 < s2)
return col1;
if (s2 < s1)
return col2;
isl_int_mul(tmp, r[col2], tr[col1]);
isl_int_submul(tmp, r[col1], tr[col2]);
if (isl_int_is_pos(tmp))
return col1;
if (isl_int_is_neg(tmp))
return col2;
}
return -1;
}
/* Given a row in the tableau, find and return the column that would
* result in the lexicographically smallest, but positive, increment
* in the sample point.
* If there is no such column, then return tab->n_col.
* If anything goes wrong, return -1.
*/
static int lexmin_pivot_col(struct isl_tab *tab, int row)
{
int j;
int col = tab->n_col;
isl_int *tr;
isl_int tmp;
tr = tab->mat->row[row] + 2 + tab->M;
isl_int_init(tmp);
for (j = tab->n_dead; j < tab->n_col; ++j) {
if (tab->col_var[j] >= 0 &&
(tab->col_var[j] < tab->n_param ||
tab->col_var[j] >= tab->n_var - tab->n_div))
continue;
if (!isl_int_is_pos(tr[j]))
continue;
if (col == tab->n_col)
col = j;
else
col = lexmin_col_pair(tab, row, col, j, tmp);
isl_assert(tab->mat->ctx, col >= 0, goto error);
}
isl_int_clear(tmp);
return col;
error:
isl_int_clear(tmp);
return -1;
}
/* Return the first known violated constraint, i.e., a non-negative
* constraint that currently has an either obviously negative value
* or a previously determined to be negative value.
*
* If any constraint has a negative coefficient for the big parameter,
* if any, then we return one of these first.
*/
static int first_neg(struct isl_tab *tab)
{
int row;
if (tab->M)
for (row = tab->n_redundant; row < tab->n_row; ++row) {
if (!isl_tab_var_from_row(tab, row)->is_nonneg)
continue;
if (!isl_int_is_neg(tab->mat->row[row][2]))
continue;
if (tab->row_sign)
tab->row_sign[row] = isl_tab_row_neg;
return row;
}
for (row = tab->n_redundant; row < tab->n_row; ++row) {
if (!isl_tab_var_from_row(tab, row)->is_nonneg)
continue;
if (tab->row_sign) {
if (tab->row_sign[row] == 0 &&
is_obviously_neg(tab, row))
tab->row_sign[row] = isl_tab_row_neg;
if (tab->row_sign[row] != isl_tab_row_neg)
continue;
} else if (!is_obviously_neg(tab, row))
continue;
return row;
}
return -1;
}
/* Check whether the invariant that all columns are lexico-positive
* is satisfied. This function is not called from the current code
* but is useful during debugging.
*/
static void check_lexpos(struct isl_tab *tab) __attribute__ ((unused));
static void check_lexpos(struct isl_tab *tab)
{
unsigned off = 2 + tab->M;
int col;
int var;
int row;
for (col = tab->n_dead; col < tab->n_col; ++col) {
if (tab->col_var[col] >= 0 &&
(tab->col_var[col] < tab->n_param ||
tab->col_var[col] >= tab->n_var - tab->n_div))
continue;
for (var = tab->n_param; var < tab->n_var - tab->n_div; ++var) {
if (!tab->var[var].is_row) {
if (tab->var[var].index == col)
break;
else
continue;
}
row = tab->var[var].index;
if (isl_int_is_zero(tab->mat->row[row][off + col]))
continue;
if (isl_int_is_pos(tab->mat->row[row][off + col]))
break;
fprintf(stderr, "lexneg column %d (row %d)\n",
col, row);
}
if (var >= tab->n_var - tab->n_div)
fprintf(stderr, "zero column %d\n", col);
}
}
/* Report to the caller that the given constraint is part of an encountered
* conflict.
*/
static int report_conflicting_constraint(struct isl_tab *tab, int con)
{
return tab->conflict(con, tab->conflict_user);
}
/* Given a conflicting row in the tableau, report all constraints
* involved in the row to the caller. That is, the row itself
* (if represents a constraint) and all constraint columns with
* non-zero (and therefore negative) coefficient.
*/
static int report_conflict(struct isl_tab *tab, int row)
{
int j;
isl_int *tr;
if (!tab->conflict)
return 0;
if (tab->row_var[row] < 0 &&
report_conflicting_constraint(tab, ~tab->row_var[row]) < 0)
return -1;
tr = tab->mat->row[row] + 2 + tab->M;
for (j = tab->n_dead; j < tab->n_col; ++j) {
if (tab->col_var[j] >= 0 &&
(tab->col_var[j] < tab->n_param ||
tab->col_var[j] >= tab->n_var - tab->n_div))
continue;
if (!isl_int_is_neg(tr[j]))
continue;
if (tab->col_var[j] < 0 &&
report_conflicting_constraint(tab, ~tab->col_var[j]) < 0)
return -1;
}
return 0;
}
/* Resolve all known or obviously violated constraints through pivoting.
* In particular, as long as we can find any violated constraint, we
* look for a pivoting column that would result in the lexicographically
* smallest increment in the sample point. If there is no such column
* then the tableau is infeasible.
*/
static int restore_lexmin(struct isl_tab *tab) WARN_UNUSED;
static int restore_lexmin(struct isl_tab *tab)
{
int row, col;
if (!tab)
return -1;
if (tab->empty)
return 0;
while ((row = first_neg(tab)) != -1) {
col = lexmin_pivot_col(tab, row);
if (col >= tab->n_col) {
if (report_conflict(tab, row) < 0)
return -1;
if (isl_tab_mark_empty(tab) < 0)
return -1;
return 0;
}
if (col < 0)
return -1;
if (isl_tab_pivot(tab, row, col) < 0)
return -1;
}
return 0;
}
/* Given a row that represents an equality, look for an appropriate
* pivoting column.
* In particular, if there are any non-zero coefficients among
* the non-parameter variables, then we take the last of these
* variables. Eliminating this variable in terms of the other
* variables and/or parameters does not influence the property
* that all column in the initial tableau are lexicographically
* positive. The row corresponding to the eliminated variable
* will only have non-zero entries below the diagonal of the
* initial tableau. That is, we transform
*
* I I
* 1 into a
* I I
*
* If there is no such non-parameter variable, then we are dealing with
* pure parameter equality and we pick any parameter with coefficient 1 or -1
* for elimination. This will ensure that the eliminated parameter
* always has an integer value whenever all the other parameters are integral.
* If there is no such parameter then we return -1.
*/
static int last_var_col_or_int_par_col(struct isl_tab *tab, int row)
{
unsigned off = 2 + tab->M;
int i;
for (i = tab->n_var - tab->n_div - 1; i >= 0 && i >= tab->n_param; --i) {
int col;
if (tab->var[i].is_row)
continue;
col = tab->var[i].index;
if (col <= tab->n_dead)
continue;
if (!isl_int_is_zero(tab->mat->row[row][off + col]))
return col;
}
for (i = tab->n_dead; i < tab->n_col; ++i) {
if (isl_int_is_one(tab->mat->row[row][off + i]))
return i;
if (isl_int_is_negone(tab->mat->row[row][off + i]))
return i;
}
return -1;
}
/* Add an equality that is known to be valid to the tableau.
* We first check if we can eliminate a variable or a parameter.
* If not, we add the equality as two inequalities.
* In this case, the equality was a pure parameter equality and there
* is no need to resolve any constraint violations.
*/
static struct isl_tab *add_lexmin_valid_eq(struct isl_tab *tab, isl_int *eq)
{
int i;
int r;
if (!tab)
return NULL;
r = isl_tab_add_row(tab, eq);
if (r < 0)
goto error;
r = tab->con[r].index;
i = last_var_col_or_int_par_col(tab, r);
if (i < 0) {
tab->con[r].is_nonneg = 1;
if (isl_tab_push_var(tab, isl_tab_undo_nonneg, &tab->con[r]) < 0)
goto error;
isl_seq_neg(eq, eq, 1 + tab->n_var);
r = isl_tab_add_row(tab, eq);
if (r < 0)
goto error;
tab->con[r].is_nonneg = 1;
if (isl_tab_push_var(tab, isl_tab_undo_nonneg, &tab->con[r]) < 0)
goto error;
} else {
if (isl_tab_pivot(tab, r, i) < 0)
goto error;
if (isl_tab_kill_col(tab, i) < 0)
goto error;
tab->n_eq++;
}
return tab;
error:
isl_tab_free(tab);
return NULL;
}
/* Check if the given row is a pure constant.
*/
static int is_constant(struct isl_tab *tab, int row)
{
unsigned off = 2 + tab->M;
return isl_seq_first_non_zero(tab->mat->row[row] + off + tab->n_dead,
tab->n_col - tab->n_dead) == -1;
}
/* Add an equality that may or may not be valid to the tableau.
* If the resulting row is a pure constant, then it must be zero.
* Otherwise, the resulting tableau is empty.
*
* If the row is not a pure constant, then we add two inequalities,
* each time checking that they can be satisfied.
* In the end we try to use one of the two constraints to eliminate
* a column.
*/
static int add_lexmin_eq(struct isl_tab *tab, isl_int *eq) WARN_UNUSED;
static int add_lexmin_eq(struct isl_tab *tab, isl_int *eq)
{
int r1, r2;
int row;
struct isl_tab_undo *snap;
if (!tab)
return -1;
snap = isl_tab_snap(tab);
r1 = isl_tab_add_row(tab, eq);
if (r1 < 0)
return -1;
tab->con[r1].is_nonneg = 1;
if (isl_tab_push_var(tab, isl_tab_undo_nonneg, &tab->con[r1]) < 0)
return -1;
row = tab->con[r1].index;
if (is_constant(tab, row)) {
if (!isl_int_is_zero(tab->mat->row[row][1]) ||
(tab->M && !isl_int_is_zero(tab->mat->row[row][2]))) {
if (isl_tab_mark_empty(tab) < 0)
return -1;
return 0;
}
if (isl_tab_rollback(tab, snap) < 0)
return -1;
return 0;
}
if (restore_lexmin(tab) < 0)
return -1;
if (tab->empty)
return 0;
isl_seq_neg(eq, eq, 1 + tab->n_var);
r2 = isl_tab_add_row(tab, eq);
if (r2 < 0)
return -1;
tab->con[r2].is_nonneg = 1;
if (isl_tab_push_var(tab, isl_tab_undo_nonneg, &tab->con[r2]) < 0)
return -1;
if (restore_lexmin(tab) < 0)
return -1;
if (tab->empty)
return 0;
if (!tab->con[r1].is_row) {
if (isl_tab_kill_col(tab, tab->con[r1].index) < 0)
return -1;
} else if (!tab->con[r2].is_row) {
if (isl_tab_kill_col(tab, tab->con[r2].index) < 0)
return -1;
}
if (tab->bmap) {
tab->bmap = isl_basic_map_add_ineq(tab->bmap, eq);
if (isl_tab_push(tab, isl_tab_undo_bmap_ineq) < 0)
return -1;
isl_seq_neg(eq, eq, 1 + tab->n_var);
tab->bmap = isl_basic_map_add_ineq(tab->bmap, eq);
isl_seq_neg(eq, eq, 1 + tab->n_var);
if (isl_tab_push(tab, isl_tab_undo_bmap_ineq) < 0)
return -1;
if (!tab->bmap)
return -1;
}
return 0;
}
/* Add an inequality to the tableau, resolving violations using
* restore_lexmin.
*/
static struct isl_tab *add_lexmin_ineq(struct isl_tab *tab, isl_int *ineq)
{
int r;
if (!tab)
return NULL;
if (tab->bmap) {
tab->bmap = isl_basic_map_add_ineq(tab->bmap, ineq);
if (isl_tab_push(tab, isl_tab_undo_bmap_ineq) < 0)
goto error;
if (!tab->bmap)
goto error;
}
r = isl_tab_add_row(tab, ineq);
if (r < 0)
goto error;
tab->con[r].is_nonneg = 1;
if (isl_tab_push_var(tab, isl_tab_undo_nonneg, &tab->con[r]) < 0)
goto error;
if (isl_tab_row_is_redundant(tab, tab->con[r].index)) {
if (isl_tab_mark_redundant(tab, tab->con[r].index) < 0)
goto error;
return tab;
}
if (restore_lexmin(tab) < 0)
goto error;
if (!tab->empty && tab->con[r].is_row &&
isl_tab_row_is_redundant(tab, tab->con[r].index))
if (isl_tab_mark_redundant(tab, tab->con[r].index) < 0)
goto error;
return tab;
error:
isl_tab_free(tab);
return NULL;
}
/* Check if the coefficients of the parameters are all integral.
*/
static int integer_parameter(struct isl_tab *tab, int row)
{
int i;
int col;
unsigned off = 2 + tab->M;
for (i = 0; i < tab->n_param; ++i) {
/* Eliminated parameter */
if (tab->var[i].is_row)
continue;
col = tab->var[i].index;
if (!isl_int_is_divisible_by(tab->mat->row[row][off + col],
tab->mat->row[row][0]))
return 0;
}
for (i = 0; i < tab->n_div; ++i) {
if (tab->var[tab->n_var - tab->n_div + i].is_row)
continue;
col = tab->var[tab->n_var - tab->n_div + i].index;
if (!isl_int_is_divisible_by(tab->mat->row[row][off + col],
tab->mat->row[row][0]))
return 0;
}
return 1;
}
/* Check if the coefficients of the non-parameter variables are all integral.
*/
static int integer_variable(struct isl_tab *tab, int row)
{
int i;
unsigned off = 2 + tab->M;
for (i = tab->n_dead; i < tab->n_col; ++i) {
if (tab->col_var[i] >= 0 &&
(tab->col_var[i] < tab->n_param ||
tab->col_var[i] >= tab->n_var - tab->n_div))
continue;
if (!isl_int_is_divisible_by(tab->mat->row[row][off + i],
tab->mat->row[row][0]))
return 0;
}
return 1;
}
/* Check if the constant term is integral.
*/
static int integer_constant(struct isl_tab *tab, int row)
{
return isl_int_is_divisible_by(tab->mat->row[row][1],
tab->mat->row[row][0]);
}
#define I_CST 1 << 0
#define I_PAR 1 << 1
#define I_VAR 1 << 2
/* Check for next (non-parameter) variable after "var" (first if var == -1)
* that is non-integer and therefore requires a cut and return
* the index of the variable.
* For parametric tableaus, there are three parts in a row,
* the constant, the coefficients of the parameters and the rest.
* For each part, we check whether the coefficients in that part
* are all integral and if so, set the corresponding flag in *f.
* If the constant and the parameter part are integral, then the
* current sample value is integral and no cut is required
* (irrespective of whether the variable part is integral).
*/
static int next_non_integer_var(struct isl_tab *tab, int var, int *f)
{
var = var < 0 ? tab->n_param : var + 1;
for (; var < tab->n_var - tab->n_div; ++var) {
int flags = 0;
int row;
if (!tab->var[var].is_row)
continue;
row = tab->var[var].index;
if (integer_constant(tab, row))
ISL_FL_SET(flags, I_CST);
if (integer_parameter(tab, row))
ISL_FL_SET(flags, I_PAR);
if (ISL_FL_ISSET(flags, I_CST) && ISL_FL_ISSET(flags, I_PAR))
continue;
if (integer_variable(tab, row))
ISL_FL_SET(flags, I_VAR);
*f = flags;
return var;
}
return -1;
}
/* Check for first (non-parameter) variable that is non-integer and
* therefore requires a cut and return the corresponding row.
* For parametric tableaus, there are three parts in a row,
* the constant, the coefficients of the parameters and the rest.
* For each part, we check whether the coefficients in that part
* are all integral and if so, set the corresponding flag in *f.
* If the constant and the parameter part are integral, then the
* current sample value is integral and no cut is required
* (irrespective of whether the variable part is integral).
*/
static int first_non_integer_row(struct isl_tab *tab, int *f)
{
int var = next_non_integer_var(tab, -1, f);
return var < 0 ? -1 : tab->var[var].index;
}
/* Add a (non-parametric) cut to cut away the non-integral sample
* value of the given row.
*
* If the row is given by
*
* m r = f + \sum_i a_i y_i
*
* then the cut is
*
* c = - {-f/m} + \sum_i {a_i/m} y_i >= 0
*
* The big parameter, if any, is ignored, since it is assumed to be big
* enough to be divisible by any integer.
* If the tableau is actually a parametric tableau, then this function
* is only called when all coefficients of the parameters are integral.
* The cut therefore has zero coefficients for the parameters.
*
* The current value is known to be negative, so row_sign, if it
* exists, is set accordingly.
*
* Return the row of the cut or -1.
*/
static int add_cut(struct isl_tab *tab, int row)
{
int i;
int r;
isl_int *r_row;
unsigned off = 2 + tab->M;
if (isl_tab_extend_cons(tab, 1) < 0)
return -1;
r = isl_tab_allocate_con(tab);
if (r < 0)
return -1;
r_row = tab->mat->row[tab->con[r].index];
isl_int_set(r_row[0], tab->mat->row[row][0]);
isl_int_neg(r_row[1], tab->mat->row[row][1]);
isl_int_fdiv_r(r_row[1], r_row[1], tab->mat->row[row][0]);
isl_int_neg(r_row[1], r_row[1]);
if (tab->M)
isl_int_set_si(r_row[2], 0);
for (i = 0; i < tab->n_col; ++i)
isl_int_fdiv_r(r_row[off + i],
tab->mat->row[row][off + i], tab->mat->row[row][0]);
tab->con[r].is_nonneg = 1;
if (isl_tab_push_var(tab, isl_tab_undo_nonneg, &tab->con[r]) < 0)
return -1;
if (tab->row_sign)
tab->row_sign[tab->con[r].index] = isl_tab_row_neg;
return tab->con[r].index;
}
/* Given a non-parametric tableau, add cuts until an integer
* sample point is obtained or until the tableau is determined
* to be integer infeasible.
* As long as there is any non-integer value in the sample point,
* we add appropriate cuts, if possible, for each of these
* non-integer values and then resolve the violated
* cut constraints using restore_lexmin.
* If one of the corresponding rows is equal to an integral
* combination of variables/constraints plus a non-integral constant,
* then there is no way to obtain an integer point and we return
* a tableau that is marked empty.
*/
static struct isl_tab *cut_to_integer_lexmin(struct isl_tab *tab)
{
int var;
int row;
int flags;
if (!tab)
return NULL;
if (tab->empty)
return tab;
while ((var = next_non_integer_var(tab, -1, &flags)) != -1) {
do {
if (ISL_FL_ISSET(flags, I_VAR)) {
if (isl_tab_mark_empty(tab) < 0)
goto error;
return tab;
}
row = tab->var[var].index;
row = add_cut(tab, row);
if (row < 0)
goto error;
} while ((var = next_non_integer_var(tab, var, &flags)) != -1);
if (restore_lexmin(tab) < 0)
goto error;
if (tab->empty)
break;
}
return tab;
error:
isl_tab_free(tab);
return NULL;
}
/* Check whether all the currently active samples also satisfy the inequality
* "ineq" (treated as an equality if eq is set).
* Remove those samples that do not.
*/
static struct isl_tab *check_samples(struct isl_tab *tab, isl_int *ineq, int eq)
{
int i;
isl_int v;
if (!tab)
return NULL;
isl_assert(tab->mat->ctx, tab->bmap, goto error);
isl_assert(tab->mat->ctx, tab->samples, goto error);
isl_assert(tab->mat->ctx, tab->samples->n_col == 1 + tab->n_var, goto error);
isl_int_init(v);
for (i = tab->n_outside; i < tab->n_sample; ++i) {
int sgn;
isl_seq_inner_product(ineq, tab->samples->row[i],
1 + tab->n_var, &v);
sgn = isl_int_sgn(v);
if (eq ? (sgn == 0) : (sgn >= 0))
continue;
tab = isl_tab_drop_sample(tab, i);
if (!tab)
break;
}
isl_int_clear(v);
return tab;
error:
isl_tab_free(tab);
return NULL;
}
/* Check whether the sample value of the tableau is finite,
* i.e., either the tableau does not use a big parameter, or
* all values of the variables are equal to the big parameter plus
* some constant. This constant is the actual sample value.
*/
static int sample_is_finite(struct isl_tab *tab)
{
int i;
if (!tab->M)
return 1;
for (i = 0; i < tab->n_var; ++i) {
int row;
if (!tab->var[i].is_row)
return 0;
row = tab->var[i].index;
if (isl_int_ne(tab->mat->row[row][0], tab->mat->row[row][2]))
return 0;
}
return 1;
}
/* Check if the context tableau of sol has any integer points.
* Leave tab in empty state if no integer point can be found.
* If an integer point can be found and if moreover it is finite,
* then it is added to the list of sample values.
*
* This function is only called when none of the currently active sample
* values satisfies the most recently added constraint.
*/
static struct isl_tab *check_integer_feasible(struct isl_tab *tab)
{
struct isl_tab_undo *snap;
if (!tab)
return NULL;
snap = isl_tab_snap(tab);
if (isl_tab_push_basis(tab) < 0)
goto error;
tab = cut_to_integer_lexmin(tab);
if (!tab)
goto error;
if (!tab->empty && sample_is_finite(tab)) {
struct isl_vec *sample;
sample = isl_tab_get_sample_value(tab);
tab = isl_tab_add_sample(tab, sample);
}
if (!tab->empty && isl_tab_rollback(tab, snap) < 0)
goto error;
return tab;
error:
isl_tab_free(tab);
return NULL;
}
/* Check if any of the currently active sample values satisfies
* the inequality "ineq" (an equality if eq is set).
*/
static int tab_has_valid_sample(struct isl_tab *tab, isl_int *ineq, int eq)
{
int i;
isl_int v;
if (!tab)
return -1;
isl_assert(tab->mat->ctx, tab->bmap, return -1);
isl_assert(tab->mat->ctx, tab->samples, return -1);
isl_assert(tab->mat->ctx, tab->samples->n_col == 1 + tab->n_var, return -1);
isl_int_init(v);
for (i = tab->n_outside; i < tab->n_sample; ++i) {
int sgn;
isl_seq_inner_product(ineq, tab->samples->row[i],
1 + tab->n_var, &v);
sgn = isl_int_sgn(v);
if (eq ? (sgn == 0) : (sgn >= 0))
break;
}
isl_int_clear(v);
return i < tab->n_sample;
}
/* Add a div specified by "div" to the tableau "tab" and return
* 1 if the div is obviously non-negative.
*/
static int context_tab_add_div(struct isl_tab *tab, struct isl_vec *div,
int (*add_ineq)(void *user, isl_int *), void *user)
{
int i;
int r;
struct isl_mat *samples;
int nonneg;
r = isl_tab_add_div(tab, div, add_ineq, user);
if (r < 0)
return -1;
nonneg = tab->var[r].is_nonneg;
tab->var[r].frozen = 1;
samples = isl_mat_extend(tab->samples,
tab->n_sample, 1 + tab->n_var);
tab->samples = samples;
if (!samples)
return -1;
for (i = tab->n_outside; i < samples->n_row; ++i) {
isl_seq_inner_product(div->el + 1, samples->row[i],
div->size - 1, &samples->row[i][samples->n_col - 1]);
isl_int_fdiv_q(samples->row[i][samples->n_col - 1],
samples->row[i][samples->n_col - 1], div->el[0]);
}
return nonneg;
}
/* Add a div specified by "div" to both the main tableau and
* the context tableau. In case of the main tableau, we only
* need to add an extra div. In the context tableau, we also
* need to express the meaning of the div.
* Return the index of the div or -1 if anything went wrong.
*/
static int add_div(struct isl_tab *tab, struct isl_context *context,
struct isl_vec *div)
{
int r;
int nonneg;
if ((nonneg = context->op->add_div(context, div)) < 0)
goto error;
if (!context->op->is_ok(context))
goto error;
if (isl_tab_extend_vars(tab, 1) < 0)
goto error;
r = isl_tab_allocate_var(tab);
if (r < 0)
goto error;
if (nonneg)
tab->var[r].is_nonneg = 1;
tab->var[r].frozen = 1;
tab->n_div++;
return tab->n_div - 1;
error:
context->op->invalidate(context);
return -1;
}
static int find_div(struct isl_tab *tab, isl_int *div, isl_int denom)
{
int i;
unsigned total = isl_basic_map_total_dim(tab->bmap);
for (i = 0; i < tab->bmap->n_div; ++i) {
if (isl_int_ne(tab->bmap->div[i][0], denom))
continue;
if (!isl_seq_eq(tab->bmap->div[i] + 1, div, 1 + total))
continue;
return i;
}
return -1;
}
/* Return the index of a div that corresponds to "div".
* We first check if we already have such a div and if not, we create one.
*/
static int get_div(struct isl_tab *tab, struct isl_context *context,
struct isl_vec *div)
{
int d;
struct isl_tab *context_tab = context->op->peek_tab(context);
if (!context_tab)
return -1;
d = find_div(context_tab, div->el + 1, div->el[0]);
if (d != -1)
return d;
return add_div(tab, context, div);
}
/* Add a parametric cut to cut away the non-integral sample value
* of the give row.
* Let a_i be the coefficients of the constant term and the parameters
* and let b_i be the coefficients of the variables or constraints
* in basis of the tableau.
* Let q be the div q = floor(\sum_i {-a_i} y_i).
*
* The cut is expressed as
*
* c = \sum_i -{-a_i} y_i + \sum_i {b_i} x_i + q >= 0
*
* If q did not already exist in the context tableau, then it is added first.
* If q is in a column of the main tableau then the "+ q" can be accomplished
* by setting the corresponding entry to the denominator of the constraint.
* If q happens to be in a row of the main tableau, then the corresponding
* row needs to be added instead (taking care of the denominators).
* Note that this is very unlikely, but perhaps not entirely impossible.
*
* The current value of the cut is known to be negative (or at least
* non-positive), so row_sign is set accordingly.
*
* Return the row of the cut or -1.
*/
static int add_parametric_cut(struct isl_tab *tab, int row,
struct isl_context *context)
{
struct isl_vec *div;
int d;
int i;
int r;
isl_int *r_row;
int col;
int n;
unsigned off = 2 + tab->M;
if (!context)
return -1;
div = get_row_parameter_div(tab, row);
if (!div)
return -1;
n = tab->n_div;
d = context->op->get_div(context, tab, div);
if (d < 0)
return -1;
if (isl_tab_extend_cons(tab, 1) < 0)
return -1;
r = isl_tab_allocate_con(tab);
if (r < 0)
return -1;
r_row = tab->mat->row[tab->con[r].index];
isl_int_set(r_row[0], tab->mat->row[row][0]);
isl_int_neg(r_row[1], tab->mat->row[row][1]);
isl_int_fdiv_r(r_row[1], r_row[1], tab->mat->row[row][0]);
isl_int_neg(r_row[1], r_row[1]);
if (tab->M)
isl_int_set_si(r_row[2], 0);
for (i = 0; i < tab->n_param; ++i) {
if (tab->var[i].is_row)
continue;
col = tab->var[i].index;
isl_int_neg(r_row[off + col], tab->mat->row[row][off + col]);
isl_int_fdiv_r(r_row[off + col], r_row[off + col],
tab->mat->row[row][0]);
isl_int_neg(r_row[off + col], r_row[off + col]);
}
for (i = 0; i < tab->n_div; ++i) {
if (tab->var[tab->n_var - tab->n_div + i].is_row)
continue;
col = tab->var[tab->n_var - tab->n_div + i].index;
isl_int_neg(r_row[off + col], tab->mat->row[row][off + col]);
isl_int_fdiv_r(r_row[off + col], r_row[off + col],
tab->mat->row[row][0]);
isl_int_neg(r_row[off + col], r_row[off + col]);
}
for (i = 0; i < tab->n_col; ++i) {
if (tab->col_var[i] >= 0 &&
(tab->col_var[i] < tab->n_param ||
tab->col_var[i] >= tab->n_var - tab->n_div))
continue;
isl_int_fdiv_r(r_row[off + i],
tab->mat->row[row][off + i], tab->mat->row[row][0]);
}
if (tab->var[tab->n_var - tab->n_div + d].is_row) {
isl_int gcd;
int d_row = tab->var[tab->n_var - tab->n_div + d].index;
isl_int_init(gcd);
isl_int_gcd(gcd, tab->mat->row[d_row][0], r_row[0]);
isl_int_divexact(r_row[0], r_row[0], gcd);
isl_int_divexact(gcd, tab->mat->row[d_row][0], gcd);
isl_seq_combine(r_row + 1, gcd, r_row + 1,
r_row[0], tab->mat->row[d_row] + 1,
off - 1 + tab->n_col);
isl_int_mul(r_row[0], r_row[0], tab->mat->row[d_row][0]);
isl_int_clear(gcd);
} else {
col = tab->var[tab->n_var - tab->n_div + d].index;
isl_int_set(r_row[off + col], tab->mat->row[row][0]);
}
tab->con[r].is_nonneg = 1;
if (isl_tab_push_var(tab, isl_tab_undo_nonneg, &tab->con[r]) < 0)
return -1;
if (tab->row_sign)
tab->row_sign[tab->con[r].index] = isl_tab_row_neg;
isl_vec_free(div);
row = tab->con[r].index;
if (d >= n && context->op->detect_equalities(context, tab) < 0)
return -1;
return row;
}
/* Construct a tableau for bmap that can be used for computing
* the lexicographic minimum (or maximum) of bmap.
* If not NULL, then dom is the domain where the minimum
* should be computed. In this case, we set up a parametric
* tableau with row signs (initialized to "unknown").
* If M is set, then the tableau will use a big parameter.
* If max is set, then a maximum should be computed instead of a minimum.
* This means that for each variable x, the tableau will contain the variable
* x' = M - x, rather than x' = M + x. This in turn means that the coefficient
* of the variables in all constraints are negated prior to adding them
* to the tableau.
*/
static struct isl_tab *tab_for_lexmin(struct isl_basic_map *bmap,
struct isl_basic_set *dom, unsigned M, int max)
{
int i;
struct isl_tab *tab;
tab = isl_tab_alloc(bmap->ctx, 2 * bmap->n_eq + bmap->n_ineq + 1,
isl_basic_map_total_dim(bmap), M);
if (!tab)
return NULL;
tab->rational = ISL_F_ISSET(bmap, ISL_BASIC_MAP_RATIONAL);
if (dom) {
tab->n_param = isl_basic_set_total_dim(dom) - dom->n_div;
tab->n_div = dom->n_div;
tab->row_sign = isl_calloc_array(bmap->ctx,
enum isl_tab_row_sign, tab->mat->n_row);
if (!tab->row_sign)
goto error;
}
if (ISL_F_ISSET(bmap, ISL_BASIC_MAP_EMPTY)) {
if (isl_tab_mark_empty(tab) < 0)
goto error;
return tab;
}
for (i = tab->n_param; i < tab->n_var - tab->n_div; ++i) {
tab->var[i].is_nonneg = 1;
tab->var[i].frozen = 1;
}
for (i = 0; i < bmap->n_eq; ++i) {
if (max)
isl_seq_neg(bmap->eq[i] + 1 + tab->n_param,
bmap->eq[i] + 1 + tab->n_param,
tab->n_var - tab->n_param - tab->n_div);
tab = add_lexmin_valid_eq(tab, bmap->eq[i]);
if (max)
isl_seq_neg(bmap->eq[i] + 1 + tab->n_param,
bmap->eq[i] + 1 + tab->n_param,
tab->n_var - tab->n_param - tab->n_div);
if (!tab || tab->empty)
return tab;
}
if (bmap->n_eq && restore_lexmin(tab) < 0)
goto error;
for (i = 0; i < bmap->n_ineq; ++i) {
if (max)
isl_seq_neg(bmap->ineq[i] + 1 + tab->n_param,
bmap->ineq[i] + 1 + tab->n_param,
tab->n_var - tab->n_param - tab->n_div);
tab = add_lexmin_ineq(tab, bmap->ineq[i]);
if (max)
isl_seq_neg(bmap->ineq[i] + 1 + tab->n_param,
bmap->ineq[i] + 1 + tab->n_param,
tab->n_var - tab->n_param - tab->n_div);
if (!tab || tab->empty)
return tab;
}
return tab;
error:
isl_tab_free(tab);
return NULL;
}
/* Given a main tableau where more than one row requires a split,
* determine and return the "best" row to split on.
*
* Given two rows in the main tableau, if the inequality corresponding
* to the first row is redundant with respect to that of the second row
* in the current tableau, then it is better to split on the second row,
* since in the positive part, both row will be positive.
* (In the negative part a pivot will have to be performed and just about
* anything can happen to the sign of the other row.)
*
* As a simple heuristic, we therefore select the row that makes the most
* of the other rows redundant.
*
* Perhaps it would also be useful to look at the number of constraints
* that conflict with any given constraint.
*/
static int best_split(struct isl_tab *tab, struct isl_tab *context_tab)
{
struct isl_tab_undo *snap;
int split;
int row;
int best = -1;
int best_r;
if (isl_tab_extend_cons(context_tab, 2) < 0)
return -1;
snap = isl_tab_snap(context_tab);
for (split = tab->n_redundant; split < tab->n_row; ++split) {
struct isl_tab_undo *snap2;
struct isl_vec *ineq = NULL;
int r = 0;
int ok;
if (!isl_tab_var_from_row(tab, split)->is_nonneg)
continue;
if (tab->row_sign[split] != isl_tab_row_any)
continue;
ineq = get_row_parameter_ineq(tab, split);
if (!ineq)
return -1;
ok = isl_tab_add_ineq(context_tab, ineq->el) >= 0;
isl_vec_free(ineq);
if (!ok)
return -1;
snap2 = isl_tab_snap(context_tab);
for (row = tab->n_redundant; row < tab->n_row; ++row) {
struct isl_tab_var *var;
if (row == split)
continue;
if (!isl_tab_var_from_row(tab, row)->is_nonneg)
continue;
if (tab->row_sign[row] != isl_tab_row_any)
continue;
ineq = get_row_parameter_ineq(tab, row);
if (!ineq)
return -1;
ok = isl_tab_add_ineq(context_tab, ineq->el) >= 0;
isl_vec_free(ineq);
if (!ok)
return -1;
var = &context_tab->con[context_tab->n_con - 1];
if (!context_tab->empty &&
!isl_tab_min_at_most_neg_one(context_tab, var))
r++;
if (isl_tab_rollback(context_tab, snap2) < 0)
return -1;
}
if (best == -1 || r > best_r) {
best = split;
best_r = r;
}
if (isl_tab_rollback(context_tab, snap) < 0)
return -1;
}
return best;
}
static struct isl_basic_set *context_lex_peek_basic_set(
struct isl_context *context)
{
struct isl_context_lex *clex = (struct isl_context_lex *)context;
if (!clex->tab)
return NULL;
return isl_tab_peek_bset(clex->tab);
}
static struct isl_tab *context_lex_peek_tab(struct isl_context *context)
{
struct isl_context_lex *clex = (struct isl_context_lex *)context;
return clex->tab;
}
static void context_lex_add_eq(struct isl_context *context, isl_int *eq,
int check, int update)
{
struct isl_context_lex *clex = (struct isl_context_lex *)context;
if (isl_tab_extend_cons(clex->tab, 2) < 0)
goto error;
if (add_lexmin_eq(clex->tab, eq) < 0)
goto error;
if (check) {
int v = tab_has_valid_sample(clex->tab, eq, 1);
if (v < 0)
goto error;
if (!v)
clex->tab = check_integer_feasible(clex->tab);
}
if (update)
clex->tab = check_samples(clex->tab, eq, 1);
return;
error:
isl_tab_free(clex->tab);
clex->tab = NULL;
}
static void context_lex_add_ineq(struct isl_context *context, isl_int *ineq,
int check, int update)
{
struct isl_context_lex *clex = (struct isl_context_lex *)context;
if (isl_tab_extend_cons(clex->tab, 1) < 0)
goto error;
clex->tab = add_lexmin_ineq(clex->tab, ineq);
if (check) {
int v = tab_has_valid_sample(clex->tab, ineq, 0);
if (v < 0)
goto error;
if (!v)
clex->tab = check_integer_feasible(clex->tab);
}
if (update)
clex->tab = check_samples(clex->tab, ineq, 0);
return;
error:
isl_tab_free(clex->tab);
clex->tab = NULL;
}
static int context_lex_add_ineq_wrap(void *user, isl_int *ineq)
{
struct isl_context *context = (struct isl_context *)user;
context_lex_add_ineq(context, ineq, 0, 0);
return context->op->is_ok(context) ? 0 : -1;
}
/* Check which signs can be obtained by "ineq" on all the currently
* active sample values. See row_sign for more information.
*/
static enum isl_tab_row_sign tab_ineq_sign(struct isl_tab *tab, isl_int *ineq,
int strict)
{
int i;
int sgn;
isl_int tmp;
enum isl_tab_row_sign res = isl_tab_row_unknown;
isl_assert(tab->mat->ctx, tab->samples, return isl_tab_row_unknown);
isl_assert(tab->mat->ctx, tab->samples->n_col == 1 + tab->n_var,
return isl_tab_row_unknown);
isl_int_init(tmp);
for (i = tab->n_outside; i < tab->n_sample; ++i) {
isl_seq_inner_product(tab->samples->row[i], ineq,
1 + tab->n_var, &tmp);
sgn = isl_int_sgn(tmp);
if (sgn > 0 || (sgn == 0 && strict)) {
if (res == isl_tab_row_unknown)
res = isl_tab_row_pos;
if (res == isl_tab_row_neg)
res = isl_tab_row_any;
}
if (sgn < 0) {
if (res == isl_tab_row_unknown)
res = isl_tab_row_neg;
if (res == isl_tab_row_pos)
res = isl_tab_row_any;
}
if (res == isl_tab_row_any)
break;
}
isl_int_clear(tmp);
return res;
}
static enum isl_tab_row_sign context_lex_ineq_sign(struct isl_context *context,
isl_int *ineq, int strict)
{
struct isl_context_lex *clex = (struct isl_context_lex *)context;
return tab_ineq_sign(clex->tab, ineq, strict);
}
/* Check whether "ineq" can be added to the tableau without rendering
* it infeasible.
*/
static int context_lex_test_ineq(struct isl_context *context, isl_int *ineq)
{
struct isl_context_lex *clex = (struct isl_context_lex *)context;
struct isl_tab_undo *snap;
int feasible;
if (!clex->tab)
return -1;
if (isl_tab_extend_cons(clex->tab, 1) < 0)
return -1;
snap = isl_tab_snap(clex->tab);
if (isl_tab_push_basis(clex->tab) < 0)
return -1;
clex->tab = add_lexmin_ineq(clex->tab, ineq);
clex->tab = check_integer_feasible(clex->tab);
if (!clex->tab)
return -1;
feasible = !clex->tab->empty;
if (isl_tab_rollback(clex->tab, snap) < 0)
return -1;
return feasible;
}
static int context_lex_get_div(struct isl_context *context, struct isl_tab *tab,
struct isl_vec *div)
{
return get_div(tab, context, div);
}
/* Add a div specified by "div" to the context tableau and return
* 1 if the div is obviously non-negative.
* context_tab_add_div will always return 1, because all variables
* in a isl_context_lex tableau are non-negative.
* However, if we are using a big parameter in the context, then this only
* reflects the non-negativity of the variable used to _encode_ the
* div, i.e., div' = M + div, so we can't draw any conclusions.
*/
static int context_lex_add_div(struct isl_context *context, struct isl_vec *div)
{
struct isl_context_lex *clex = (struct isl_context_lex *)context;
int nonneg;
nonneg = context_tab_add_div(clex->tab, div,
context_lex_add_ineq_wrap, context);
if (nonneg < 0)
return -1;
if (clex->tab->M)
return 0;
return nonneg;
}
static int context_lex_detect_equalities(struct isl_context *context,
struct isl_tab *tab)
{
return 0;
}
static int context_lex_best_split(struct isl_context *context,
struct isl_tab *tab)
{
struct isl_context_lex *clex = (struct isl_context_lex *)context;
struct isl_tab_undo *snap;
int r;
snap = isl_tab_snap(clex->tab);
if (isl_tab_push_basis(clex->tab) < 0)
return -1;
r = best_split(tab, clex->tab);
if (r >= 0 && isl_tab_rollback(clex->tab, snap) < 0)
return -1;
return r;
}
static int context_lex_is_empty(struct isl_context *context)
{
struct isl_context_lex *clex = (struct isl_context_lex *)context;
if (!clex->tab)
return -1;
return clex->tab->empty;
}
static void *context_lex_save(struct isl_context *context)
{
struct isl_context_lex *clex = (struct isl_context_lex *)context;
struct isl_tab_undo *snap;
snap = isl_tab_snap(clex->tab);
if (isl_tab_push_basis(clex->tab) < 0)
return NULL;
if (isl_tab_save_samples(clex->tab) < 0)
return NULL;
return snap;
}
static void context_lex_restore(struct isl_context *context, void *save)
{
struct isl_context_lex *clex = (struct isl_context_lex *)context;
if (isl_tab_rollback(clex->tab, (struct isl_tab_undo *)save) < 0) {
isl_tab_free(clex->tab);
clex->tab = NULL;
}
}
static int context_lex_is_ok(struct isl_context *context)
{
struct isl_context_lex *clex = (struct isl_context_lex *)context;
return !!clex->tab;
}
/* For each variable in the context tableau, check if the variable can
* only attain non-negative values. If so, mark the parameter as non-negative
* in the main tableau. This allows for a more direct identification of some
* cases of violated constraints.
*/
static struct isl_tab *tab_detect_nonnegative_parameters(struct isl_tab *tab,
struct isl_tab *context_tab)
{
int i;
struct isl_tab_undo *snap;
struct isl_vec *ineq = NULL;
struct isl_tab_var *var;
int n;
if (context_tab->n_var == 0)
return tab;
ineq = isl_vec_alloc(tab->mat->ctx, 1 + context_tab->n_var);
if (!ineq)
goto error;
if (isl_tab_extend_cons(context_tab, 1) < 0)
goto error;
snap = isl_tab_snap(context_tab);
n = 0;
isl_seq_clr(ineq->el, ineq->size);
for (i = 0; i < context_tab->n_var; ++i) {
isl_int_set_si(ineq->el[1 + i], 1);
if (isl_tab_add_ineq(context_tab, ineq->el) < 0)
goto error;
var = &context_tab->con[context_tab->n_con - 1];
if (!context_tab->empty &&
!isl_tab_min_at_most_neg_one(context_tab, var)) {
int j = i;
if (i >= tab->n_param)
j = i - tab->n_param + tab->n_var - tab->n_div;
tab->var[j].is_nonneg = 1;
n++;
}
isl_int_set_si(ineq->el[1 + i], 0);
if (isl_tab_rollback(context_tab, snap) < 0)
goto error;
}
if (context_tab->M && n == context_tab->n_var) {
context_tab->mat = isl_mat_drop_cols(context_tab->mat, 2, 1);
context_tab->M = 0;
}
isl_vec_free(ineq);
return tab;
error:
isl_vec_free(ineq);
isl_tab_free(tab);
return NULL;
}
static struct isl_tab *context_lex_detect_nonnegative_parameters(
struct isl_context *context, struct isl_tab *tab)
{
struct isl_context_lex *clex = (struct isl_context_lex *)context;
struct isl_tab_undo *snap;
if (!tab)
return NULL;
snap = isl_tab_snap(clex->tab);
if (isl_tab_push_basis(clex->tab) < 0)
goto error;
tab = tab_detect_nonnegative_parameters(tab, clex->tab);
if (isl_tab_rollback(clex->tab, snap) < 0)
goto error;
return tab;
error:
isl_tab_free(tab);
return NULL;
}
static void context_lex_invalidate(struct isl_context *context)
{
struct isl_context_lex *clex = (struct isl_context_lex *)context;
isl_tab_free(clex->tab);
clex->tab = NULL;
}
static void context_lex_free(struct isl_context *context)
{
struct isl_context_lex *clex = (struct isl_context_lex *)context;
isl_tab_free(clex->tab);
free(clex);
}
struct isl_context_op isl_context_lex_op = {
context_lex_detect_nonnegative_parameters,
context_lex_peek_basic_set,
context_lex_peek_tab,
context_lex_add_eq,
context_lex_add_ineq,
context_lex_ineq_sign,
context_lex_test_ineq,
context_lex_get_div,
context_lex_add_div,
context_lex_detect_equalities,
context_lex_best_split,
context_lex_is_empty,
context_lex_is_ok,
context_lex_save,
context_lex_restore,
context_lex_invalidate,
context_lex_free,
};
static struct isl_tab *context_tab_for_lexmin(struct isl_basic_set *bset)
{
struct isl_tab *tab;
bset = isl_basic_set_cow(bset);
if (!bset)
return NULL;
tab = tab_for_lexmin((struct isl_basic_map *)bset, NULL, 1, 0);
if (!tab)
goto error;
if (isl_tab_track_bset(tab, bset) < 0)
goto error;
tab = isl_tab_init_samples(tab);
return tab;
error:
isl_basic_set_free(bset);
return NULL;
}
static struct isl_context *isl_context_lex_alloc(struct isl_basic_set *dom)
{
struct isl_context_lex *clex;
if (!dom)
return NULL;
clex = isl_alloc_type(dom->ctx, struct isl_context_lex);
if (!clex)
return NULL;
clex->context.op = &isl_context_lex_op;
clex->tab = context_tab_for_lexmin(isl_basic_set_copy(dom));
if (restore_lexmin(clex->tab) < 0)
goto error;
clex->tab = check_integer_feasible(clex->tab);
if (!clex->tab)
goto error;
return &clex->context;
error:
clex->context.op->free(&clex->context);
return NULL;
}
struct isl_context_gbr {
struct isl_context context;
struct isl_tab *tab;
struct isl_tab *shifted;
struct isl_tab *cone;
};
static struct isl_tab *context_gbr_detect_nonnegative_parameters(
struct isl_context *context, struct isl_tab *tab)
{
struct isl_context_gbr *cgbr = (struct isl_context_gbr *)context;
if (!tab)
return NULL;
return tab_detect_nonnegative_parameters(tab, cgbr->tab);
}
static struct isl_basic_set *context_gbr_peek_basic_set(
struct isl_context *context)
{
struct isl_context_gbr *cgbr = (struct isl_context_gbr *)context;
if (!cgbr->tab)
return NULL;
return isl_tab_peek_bset(cgbr->tab);
}
static struct isl_tab *context_gb