Blame isl-0.14/isl_tab.h

Packit fb9d21
/*
Packit fb9d21
 * Copyright 2008-2009 Katholieke Universiteit Leuven
Packit fb9d21
 *
Packit fb9d21
 * Use of this software is governed by the MIT license
Packit fb9d21
 *
Packit fb9d21
 * Written by Sven Verdoolaege, K.U.Leuven, Departement
Packit fb9d21
 * Computerwetenschappen, Celestijnenlaan 200A, B-3001 Leuven, Belgium
Packit fb9d21
 */
Packit fb9d21
Packit fb9d21
#ifndef ISL_TAB_H
Packit fb9d21
#define ISL_TAB_H
Packit fb9d21
Packit fb9d21
#include <isl/lp.h>
Packit fb9d21
#include <isl/map.h>
Packit fb9d21
#include <isl/mat.h>
Packit fb9d21
#include <isl/set.h>
Packit fb9d21
#include <isl_config.h>
Packit fb9d21
Packit fb9d21
struct isl_tab_var {
Packit fb9d21
	int index;
Packit fb9d21
	unsigned is_row : 1;
Packit fb9d21
	unsigned is_nonneg : 1;
Packit fb9d21
	unsigned is_zero : 1;
Packit fb9d21
	unsigned is_redundant : 1;
Packit fb9d21
	unsigned marked : 1;
Packit fb9d21
	unsigned frozen : 1;
Packit fb9d21
	unsigned negated : 1;
Packit fb9d21
};
Packit fb9d21
Packit fb9d21
enum isl_tab_undo_type {
Packit fb9d21
	isl_tab_undo_bottom,
Packit fb9d21
	isl_tab_undo_empty,
Packit fb9d21
	isl_tab_undo_nonneg,
Packit fb9d21
	isl_tab_undo_redundant,
Packit fb9d21
	isl_tab_undo_freeze,
Packit fb9d21
	isl_tab_undo_zero,
Packit fb9d21
	isl_tab_undo_allocate,
Packit fb9d21
	isl_tab_undo_relax,
Packit fb9d21
	isl_tab_undo_unrestrict,
Packit fb9d21
	isl_tab_undo_bmap_ineq,
Packit fb9d21
	isl_tab_undo_bmap_eq,
Packit fb9d21
	isl_tab_undo_bmap_div,
Packit fb9d21
	isl_tab_undo_saved_basis,
Packit fb9d21
	isl_tab_undo_drop_sample,
Packit fb9d21
	isl_tab_undo_saved_samples,
Packit fb9d21
	isl_tab_undo_callback,
Packit fb9d21
};
Packit fb9d21
Packit fb9d21
struct isl_tab_callback {
Packit fb9d21
	int (*run)(struct isl_tab_callback *cb);
Packit fb9d21
};
Packit fb9d21
Packit fb9d21
union isl_tab_undo_val {
Packit fb9d21
	int		var_index;
Packit fb9d21
	int		*col_var;
Packit fb9d21
	int		n;
Packit fb9d21
	struct isl_tab_callback	*callback;
Packit fb9d21
};
Packit fb9d21
Packit fb9d21
struct isl_tab_undo {
Packit fb9d21
	enum isl_tab_undo_type	type;
Packit fb9d21
	union isl_tab_undo_val	u;
Packit fb9d21
	struct isl_tab_undo	*next;
Packit fb9d21
};
Packit fb9d21
Packit fb9d21
/* The tableau maintains equality relations.
Packit fb9d21
 * Each column and each row is associated to a variable or a constraint.
Packit fb9d21
 * The "value" of an inequality constraint is the value of the corresponding
Packit fb9d21
 * slack variable.
Packit fb9d21
 * The "row_var" and "col_var" arrays map column and row indices
Packit fb9d21
 * to indices in the "var" and "con" arrays.  The elements of these
Packit fb9d21
 * arrays maintain extra information about the variables and the constraints.
Packit fb9d21
 * Each row expresses the corresponding row variable as an affine expression
Packit fb9d21
 * of the column variables.
Packit fb9d21
 * The first two columns in the matrix contain the common denominator of
Packit fb9d21
 * the row and the numerator of the constant term.
Packit fb9d21
 * If "M" is set, then the third column represents the "big parameter".
Packit fb9d21
 * The third (M = 0) or fourth (M = 1) column
Packit fb9d21
 * in the matrix is called column 0 with respect to the col_var array.
Packit fb9d21
 * The sample value of the tableau is the value that assigns zero
Packit fb9d21
 * to all the column variables and the constant term of each affine
Packit fb9d21
 * expression to the corresponding row variable.
Packit fb9d21
 * The operations on the tableau maintain the property that the sample
Packit fb9d21
 * value satisfies the non-negativity constraints (usually on the slack
Packit fb9d21
 * variables).
Packit fb9d21
 *
Packit fb9d21
 * The big parameter represents an arbitrarily big (and divisible)
Packit fb9d21
 * positive number.  If present, then the sign of a row is determined
Packit fb9d21
 * lexicographically, with the sign of the big parameter coefficient
Packit fb9d21
 * considered first.  The big parameter is only used while
Packit fb9d21
 * solving PILP problems.
Packit fb9d21
 *
Packit fb9d21
 * The first n_dead column variables have their values fixed to zero.
Packit fb9d21
 * The corresponding tab_vars are flagged "is_zero".
Packit fb9d21
 * Some of the rows that have have zero coefficients in all but
Packit fb9d21
 * the dead columns are also flagged "is_zero".
Packit fb9d21
 *
Packit fb9d21
 * The first n_redundant rows correspond to inequality constraints
Packit fb9d21
 * that are always satisfied for any value satisfying the non-redundant
Packit fb9d21
 * rows.  The corresponding tab_vars are flagged "is_redundant".
Packit fb9d21
 * A row variable that is flagged "is_zero" is also flagged "is_redundant"
Packit fb9d21
 * since the constraint has been reduced to 0 = 0 and is therefore always
Packit fb9d21
 * satisfied.
Packit fb9d21
 *
Packit fb9d21
 * There are "n_var" variables in total.  The first "n_param" of these
Packit fb9d21
 * are called parameters and the last "n_div" of these are called divs.
Packit fb9d21
 * The basic tableau operations makes no distinction between different
Packit fb9d21
 * kinds of variables.  These special variables are only used while
Packit fb9d21
 * solving PILP problems.
Packit fb9d21
 *
Packit fb9d21
 * Dead columns and redundant rows are detected on the fly.
Packit fb9d21
 * However, the basic operations do not ensure that all dead columns
Packit fb9d21
 * or all redundant rows are detected.
Packit fb9d21
 * isl_tab_detect_implicit_equalities and isl_tab_detect_redundant can be used
Packit fb9d21
 * to perform and exhaustive search for dead columns and redundant rows.
Packit fb9d21
 *
Packit fb9d21
 * The samples matrix contains "n_sample" integer points that have at some
Packit fb9d21
 * point been elements satisfying the tableau.  The first "n_outside"
Packit fb9d21
 * of them no longer satisfy the tableau.  They are kept because they
Packit fb9d21
 * can be reinstated during rollback when the constraint that cut them
Packit fb9d21
 * out is removed.  These samples are only maintained for the context
Packit fb9d21
 * tableau while solving PILP problems.
Packit fb9d21
 *
Packit fb9d21
 * If "preserve" is set, then we want to keep all constraints in the
Packit fb9d21
 * tableau, even if they turn out to be redundant.
Packit fb9d21
 */
Packit fb9d21
enum isl_tab_row_sign {
Packit fb9d21
	isl_tab_row_unknown = 0,
Packit fb9d21
	isl_tab_row_pos,
Packit fb9d21
	isl_tab_row_neg,
Packit fb9d21
	isl_tab_row_any,
Packit fb9d21
};
Packit fb9d21
struct isl_tab {
Packit fb9d21
	struct isl_mat *mat;
Packit fb9d21
Packit fb9d21
	unsigned n_row;
Packit fb9d21
	unsigned n_col;
Packit fb9d21
	unsigned n_dead;
Packit fb9d21
	unsigned n_redundant;
Packit fb9d21
Packit fb9d21
	unsigned n_var;
Packit fb9d21
	unsigned n_param;
Packit fb9d21
	unsigned n_div;
Packit fb9d21
	unsigned max_var;
Packit fb9d21
	unsigned n_con;
Packit fb9d21
	unsigned n_eq;
Packit fb9d21
	unsigned max_con;
Packit fb9d21
	struct isl_tab_var *var;
Packit fb9d21
	struct isl_tab_var *con;
Packit fb9d21
	int *row_var;	/* v >= 0 -> var v;	v < 0 -> con ~v */
Packit fb9d21
	int *col_var;	/* v >= 0 -> var v;	v < 0 -> con ~v */
Packit fb9d21
	enum isl_tab_row_sign *row_sign;
Packit fb9d21
Packit fb9d21
	struct isl_tab_undo bottom;
Packit fb9d21
	struct isl_tab_undo *top;
Packit fb9d21
Packit fb9d21
	struct isl_vec *dual;
Packit fb9d21
	struct isl_basic_map *bmap;
Packit fb9d21
Packit fb9d21
	unsigned n_sample;
Packit fb9d21
	unsigned n_outside;
Packit fb9d21
	int *sample_index;
Packit fb9d21
	struct isl_mat *samples;
Packit fb9d21
Packit fb9d21
	int n_zero;
Packit fb9d21
	int n_unbounded;
Packit fb9d21
	struct isl_mat *basis;
Packit fb9d21
Packit fb9d21
	int (*conflict)(int con, void *user);
Packit fb9d21
	void *conflict_user;
Packit fb9d21
Packit fb9d21
	unsigned strict_redundant : 1;
Packit fb9d21
	unsigned need_undo : 1;
Packit fb9d21
	unsigned preserve : 1;
Packit fb9d21
	unsigned rational : 1;
Packit fb9d21
	unsigned empty : 1;
Packit fb9d21
	unsigned in_undo : 1;
Packit fb9d21
	unsigned M : 1;
Packit fb9d21
	unsigned cone : 1;
Packit fb9d21
};
Packit fb9d21
Packit fb9d21
struct isl_tab *isl_tab_alloc(struct isl_ctx *ctx,
Packit fb9d21
	unsigned n_row, unsigned n_var, unsigned M);
Packit fb9d21
void isl_tab_free(struct isl_tab *tab);
Packit fb9d21
Packit fb9d21
isl_ctx *isl_tab_get_ctx(struct isl_tab *tab);
Packit fb9d21
Packit fb9d21
__isl_give struct isl_tab *isl_tab_from_basic_map(
Packit fb9d21
	__isl_keep isl_basic_map *bmap, int track);
Packit fb9d21
__isl_give struct isl_tab *isl_tab_from_basic_set(
Packit fb9d21
	__isl_keep isl_basic_set *bset, int track);
Packit fb9d21
struct isl_tab *isl_tab_from_recession_cone(struct isl_basic_set *bset,
Packit fb9d21
	int parametric);
Packit fb9d21
int isl_tab_cone_is_bounded(struct isl_tab *tab);
Packit fb9d21
struct isl_basic_map *isl_basic_map_update_from_tab(struct isl_basic_map *bmap,
Packit fb9d21
	struct isl_tab *tab);
Packit fb9d21
struct isl_basic_set *isl_basic_set_update_from_tab(struct isl_basic_set *bset,
Packit fb9d21
	struct isl_tab *tab);
Packit fb9d21
int isl_tab_detect_implicit_equalities(struct isl_tab *tab) WARN_UNUSED;
Packit fb9d21
__isl_give isl_basic_map *isl_tab_make_equalities_explicit(struct isl_tab *tab,
Packit fb9d21
	__isl_take isl_basic_map *bmap);
Packit fb9d21
int isl_tab_detect_redundant(struct isl_tab *tab) WARN_UNUSED;
Packit fb9d21
#define ISL_TAB_SAVE_DUAL	(1 << 0)
Packit fb9d21
enum isl_lp_result isl_tab_min(struct isl_tab *tab,
Packit fb9d21
	isl_int *f, isl_int denom, isl_int *opt, isl_int *opt_denom,
Packit fb9d21
	unsigned flags) WARN_UNUSED;
Packit fb9d21
Packit fb9d21
int isl_tab_add_ineq(struct isl_tab *tab, isl_int *ineq) WARN_UNUSED;
Packit fb9d21
int isl_tab_add_eq(struct isl_tab *tab, isl_int *eq) WARN_UNUSED;
Packit fb9d21
int isl_tab_add_valid_eq(struct isl_tab *tab, isl_int *eq) WARN_UNUSED;
Packit fb9d21
Packit fb9d21
int isl_tab_freeze_constraint(struct isl_tab *tab, int con) WARN_UNUSED;
Packit fb9d21
Packit fb9d21
int isl_tab_track_bmap(struct isl_tab *tab, __isl_take isl_basic_map *bmap) WARN_UNUSED;
Packit fb9d21
int isl_tab_track_bset(struct isl_tab *tab, __isl_take isl_basic_set *bset) WARN_UNUSED;
Packit fb9d21
__isl_keep isl_basic_set *isl_tab_peek_bset(struct isl_tab *tab);
Packit fb9d21
Packit fb9d21
int isl_tab_is_equality(struct isl_tab *tab, int con);
Packit fb9d21
int isl_tab_is_redundant(struct isl_tab *tab, int con);
Packit fb9d21
Packit fb9d21
int isl_tab_sample_is_integer(struct isl_tab *tab);
Packit fb9d21
struct isl_vec *isl_tab_get_sample_value(struct isl_tab *tab);
Packit fb9d21
Packit fb9d21
enum isl_ineq_type {
Packit fb9d21
	isl_ineq_error = -1,
Packit fb9d21
	isl_ineq_redundant,
Packit fb9d21
	isl_ineq_separate,
Packit fb9d21
	isl_ineq_cut,
Packit fb9d21
	isl_ineq_adj_eq,
Packit fb9d21
	isl_ineq_adj_ineq,
Packit fb9d21
};
Packit fb9d21
Packit fb9d21
enum isl_ineq_type isl_tab_ineq_type(struct isl_tab *tab, isl_int *ineq);
Packit fb9d21
Packit fb9d21
struct isl_tab_undo *isl_tab_snap(struct isl_tab *tab);
Packit fb9d21
int isl_tab_rollback(struct isl_tab *tab, struct isl_tab_undo *snap) WARN_UNUSED;
Packit fb9d21
Packit fb9d21
struct isl_tab *isl_tab_relax(struct isl_tab *tab, int con) WARN_UNUSED;
Packit fb9d21
int isl_tab_select_facet(struct isl_tab *tab, int con) WARN_UNUSED;
Packit fb9d21
int isl_tab_unrestrict(struct isl_tab *tab, int con) WARN_UNUSED;
Packit fb9d21
Packit fb9d21
void isl_tab_dump(__isl_keep struct isl_tab *tab);
Packit fb9d21
Packit fb9d21
struct isl_map *isl_tab_basic_map_partial_lexopt(
Packit fb9d21
		struct isl_basic_map *bmap, struct isl_basic_set *dom,
Packit fb9d21
		struct isl_set **empty, int max);
Packit fb9d21
__isl_give isl_pw_multi_aff *isl_basic_map_partial_lexopt_pw_multi_aff(
Packit fb9d21
	__isl_take isl_basic_map *bmap, __isl_take isl_basic_set *dom,
Packit fb9d21
	__isl_give isl_set **empty, int max);
Packit fb9d21
Packit fb9d21
/* An isl_region represents a sequence of consecutive variables.
Packit fb9d21
 * pos is the location (starting at 0) of the first variable in the sequence.
Packit fb9d21
 */
Packit fb9d21
struct isl_region {
Packit fb9d21
	int pos;
Packit fb9d21
	int len;
Packit fb9d21
};
Packit fb9d21
Packit fb9d21
__isl_give isl_vec *isl_tab_basic_set_non_trivial_lexmin(
Packit fb9d21
	__isl_take isl_basic_set *bset, int n_op, int n_region,
Packit fb9d21
	struct isl_region *region,
Packit fb9d21
	int (*conflict)(int con, void *user), void *user);
Packit fb9d21
__isl_give isl_vec *isl_tab_basic_set_non_neg_lexmin(
Packit fb9d21
	__isl_take isl_basic_set *bset);
Packit fb9d21
Packit fb9d21
/* private */
Packit fb9d21
Packit fb9d21
struct isl_tab_var *isl_tab_var_from_row(struct isl_tab *tab, int i);
Packit fb9d21
int isl_tab_mark_redundant(struct isl_tab *tab, int row) WARN_UNUSED;
Packit fb9d21
int isl_tab_mark_empty(struct isl_tab *tab) WARN_UNUSED;
Packit fb9d21
struct isl_tab *isl_tab_dup(struct isl_tab *tab);
Packit fb9d21
struct isl_tab *isl_tab_product(struct isl_tab *tab1, struct isl_tab *tab2);
Packit fb9d21
int isl_tab_extend_cons(struct isl_tab *tab, unsigned n_new) WARN_UNUSED;
Packit fb9d21
int isl_tab_allocate_con(struct isl_tab *tab) WARN_UNUSED;
Packit fb9d21
int isl_tab_extend_vars(struct isl_tab *tab, unsigned n_new) WARN_UNUSED;
Packit fb9d21
int isl_tab_allocate_var(struct isl_tab *tab) WARN_UNUSED;
Packit fb9d21
int isl_tab_pivot(struct isl_tab *tab, int row, int col) WARN_UNUSED;
Packit fb9d21
int isl_tab_add_row(struct isl_tab *tab, isl_int *line) WARN_UNUSED;
Packit fb9d21
int isl_tab_row_is_redundant(struct isl_tab *tab, int row);
Packit fb9d21
int isl_tab_min_at_most_neg_one(struct isl_tab *tab, struct isl_tab_var *var);
Packit fb9d21
int isl_tab_sign_of_max(struct isl_tab *tab, int con);
Packit fb9d21
int isl_tab_kill_col(struct isl_tab *tab, int col) WARN_UNUSED;
Packit fb9d21
Packit fb9d21
int isl_tab_push(struct isl_tab *tab, enum isl_tab_undo_type type) WARN_UNUSED;
Packit fb9d21
int isl_tab_push_var(struct isl_tab *tab,
Packit fb9d21
	enum isl_tab_undo_type type, struct isl_tab_var *var) WARN_UNUSED;
Packit fb9d21
int isl_tab_push_basis(struct isl_tab *tab) WARN_UNUSED;
Packit fb9d21
Packit fb9d21
struct isl_tab *isl_tab_init_samples(struct isl_tab *tab) WARN_UNUSED;
Packit fb9d21
int isl_tab_add_sample(struct isl_tab *tab,
Packit fb9d21
	__isl_take isl_vec *sample) WARN_UNUSED;
Packit fb9d21
struct isl_tab *isl_tab_drop_sample(struct isl_tab *tab, int s);
Packit fb9d21
int isl_tab_save_samples(struct isl_tab *tab) WARN_UNUSED;
Packit fb9d21
Packit fb9d21
struct isl_tab *isl_tab_detect_equalities(struct isl_tab *tab,
Packit fb9d21
	struct isl_tab *tab_cone) WARN_UNUSED;
Packit fb9d21
Packit fb9d21
int isl_tab_push_callback(struct isl_tab *tab,
Packit fb9d21
	struct isl_tab_callback *callback) WARN_UNUSED;
Packit fb9d21
Packit fb9d21
int isl_tab_add_div(struct isl_tab *tab, __isl_keep isl_vec *div,
Packit fb9d21
	int (*add_ineq)(void *user, isl_int *), void *user);
Packit fb9d21
Packit fb9d21
#endif