1/*
2 * Copyright 2008-2009 Katholieke Universiteit Leuven
3 *
4 * Use of this software is governed by the MIT license
5 *
6 * Written by Sven Verdoolaege, K.U.Leuven, Departement
7 * Computerwetenschappen, Celestijnenlaan 200A, B-3001 Leuven, Belgium
8 */
9
10#ifndef ISL_TAB_H
11#define ISL_TAB_H
12
13#include <isl/lp.h>
14#include <isl/map.h>
15#include <isl/mat.h>
16#include <isl/set.h>
17
18struct isl_tab_var {
19	int index;
20	unsigned is_row : 1;
21	unsigned is_nonneg : 1;
22	unsigned is_zero : 1;
23	unsigned is_redundant : 1;
24	unsigned marked : 1;
25	unsigned frozen : 1;
26	unsigned negated : 1;
27};
28
29enum isl_tab_undo_type {
30	isl_tab_undo_bottom,
31	isl_tab_undo_empty,
32	isl_tab_undo_nonneg,
33	isl_tab_undo_redundant,
34	isl_tab_undo_freeze,
35	isl_tab_undo_zero,
36	isl_tab_undo_allocate,
37	isl_tab_undo_relax,
38	isl_tab_undo_unrestrict,
39	isl_tab_undo_bmap_ineq,
40	isl_tab_undo_bmap_eq,
41	isl_tab_undo_bmap_div,
42	isl_tab_undo_saved_basis,
43	isl_tab_undo_drop_sample,
44	isl_tab_undo_saved_samples,
45	isl_tab_undo_callback,
46};
47
48struct isl_tab_callback {
49	int (*run)(struct isl_tab_callback *cb);
50};
51
52union isl_tab_undo_val {
53	int		var_index;
54	int		*col_var;
55	int		n;
56	struct isl_tab_callback	*callback;
57};
58
59struct isl_tab_undo {
60	enum isl_tab_undo_type	type;
61	union isl_tab_undo_val	u;
62	struct isl_tab_undo	*next;
63};
64
65/* The tableau maintains equality relations.
66 * Each column and each row is associated to a variable or a constraint.
67 * The "value" of an inequality constraint is the value of the corresponding
68 * slack variable.
69 * The "row_var" and "col_var" arrays map column and row indices
70 * to indices in the "var" and "con" arrays.  The elements of these
71 * arrays maintain extra information about the variables and the constraints.
72 * Each row expresses the corresponding row variable as an affine expression
73 * of the column variables.
74 * The first two columns in the matrix contain the common denominator of
75 * the row and the numerator of the constant term.
76 * If "M" is set, then the third column represents the "big parameter".
77 * The third (M = 0) or fourth (M = 1) column
78 * in the matrix is called column 0 with respect to the col_var array.
79 * The sample value of the tableau is the value that assigns zero
80 * to all the column variables and the constant term of each affine
81 * expression to the corresponding row variable.
82 * The operations on the tableau maintain the property that the sample
83 * value satisfies the non-negativity constraints (usually on the slack
84 * variables).
85 *
86 * The big parameter represents an arbitrarily big (and divisible)
87 * positive number.  If present, then the sign of a row is determined
88 * lexicographically, with the sign of the big parameter coefficient
89 * considered first.  The big parameter is only used while
90 * solving PILP problems.
91 *
92 * The first n_dead column variables have their values fixed to zero.
93 * The corresponding tab_vars are flagged "is_zero".
94 * Some of the rows that have have zero coefficients in all but
95 * the dead columns are also flagged "is_zero".
96 *
97 * The first n_redundant rows correspond to inequality constraints
98 * that are always satisfied for any value satisfying the non-redundant
99 * rows.  The corresponding tab_vars are flagged "is_redundant".
100 * A row variable that is flagged "is_zero" is also flagged "is_redundant"
101 * since the constraint has been reduced to 0 = 0 and is therefore always
102 * satisfied.
103 *
104 * There are "n_var" variables in total.  The first "n_param" of these
105 * are called parameters and the last "n_div" of these are called divs.
106 * The basic tableau operations makes no distinction between different
107 * kinds of variables.  These special variables are only used while
108 * solving PILP problems.
109 *
110 * Dead columns and redundant rows are detected on the fly.
111 * However, the basic operations do not ensure that all dead columns
112 * or all redundant rows are detected.
113 * isl_tab_detect_implicit_equalities and isl_tab_detect_redundant can be used
114 * to perform and exhaustive search for dead columns and redundant rows.
115 *
116 * The samples matrix contains "n_sample" integer points that have at some
117 * point been elements satisfying the tableau.  The first "n_outside"
118 * of them no longer satisfy the tableau.  They are kept because they
119 * can be reinstated during rollback when the constraint that cut them
120 * out is removed.  These samples are only maintained for the context
121 * tableau while solving PILP problems.
122 *
123 * If "preserve" is set, then we want to keep all constraints in the
124 * tableau, even if they turn out to be redundant.
125 */
126enum isl_tab_row_sign {
127	isl_tab_row_unknown = 0,
128	isl_tab_row_pos,
129	isl_tab_row_neg,
130	isl_tab_row_any,
131};
132struct isl_tab {
133	struct isl_mat *mat;
134
135	unsigned n_row;
136	unsigned n_col;
137	unsigned n_dead;
138	unsigned n_redundant;
139
140	unsigned n_var;
141	unsigned n_param;
142	unsigned n_div;
143	unsigned max_var;
144	unsigned n_con;
145	unsigned n_eq;
146	unsigned max_con;
147	struct isl_tab_var *var;
148	struct isl_tab_var *con;
149	int *row_var;	/* v >= 0 -> var v;	v < 0 -> con ~v */
150	int *col_var;	/* v >= 0 -> var v;	v < 0 -> con ~v */
151	enum isl_tab_row_sign *row_sign;
152
153	struct isl_tab_undo bottom;
154	struct isl_tab_undo *top;
155
156	struct isl_vec *dual;
157	struct isl_basic_map *bmap;
158
159	unsigned n_sample;
160	unsigned n_outside;
161	int *sample_index;
162	struct isl_mat *samples;
163
164	int n_zero;
165	int n_unbounded;
166	struct isl_mat *basis;
167
168	int (*conflict)(int con, void *user);
169	void *conflict_user;
170
171	unsigned strict_redundant : 1;
172	unsigned need_undo : 1;
173	unsigned preserve : 1;
174	unsigned rational : 1;
175	unsigned empty : 1;
176	unsigned in_undo : 1;
177	unsigned M : 1;
178	unsigned cone : 1;
179};
180
181struct isl_tab *isl_tab_alloc(struct isl_ctx *ctx,
182	unsigned n_row, unsigned n_var, unsigned M);
183void isl_tab_free(struct isl_tab *tab);
184
185isl_ctx *isl_tab_get_ctx(struct isl_tab *tab);
186
187__isl_give struct isl_tab *isl_tab_from_basic_map(
188	__isl_keep isl_basic_map *bmap, int track);
189__isl_give struct isl_tab *isl_tab_from_basic_set(
190	__isl_keep isl_basic_set *bset, int track);
191struct isl_tab *isl_tab_from_recession_cone(struct isl_basic_set *bset,
192	int parametric);
193int isl_tab_cone_is_bounded(struct isl_tab *tab);
194struct isl_basic_map *isl_basic_map_update_from_tab(struct isl_basic_map *bmap,
195	struct isl_tab *tab);
196struct isl_basic_set *isl_basic_set_update_from_tab(struct isl_basic_set *bset,
197	struct isl_tab *tab);
198int isl_tab_detect_implicit_equalities(struct isl_tab *tab) WARN_UNUSED;
199__isl_give isl_basic_map *isl_tab_make_equalities_explicit(struct isl_tab *tab,
200	__isl_take isl_basic_map *bmap);
201int isl_tab_detect_redundant(struct isl_tab *tab) WARN_UNUSED;
202#define ISL_TAB_SAVE_DUAL	(1 << 0)
203enum isl_lp_result isl_tab_min(struct isl_tab *tab,
204	isl_int *f, isl_int denom, isl_int *opt, isl_int *opt_denom,
205	unsigned flags) WARN_UNUSED;
206
207struct isl_tab *isl_tab_extend(struct isl_tab *tab, unsigned n_new) WARN_UNUSED;
208int isl_tab_add_ineq(struct isl_tab *tab, isl_int *ineq) WARN_UNUSED;
209int isl_tab_add_eq(struct isl_tab *tab, isl_int *eq) WARN_UNUSED;
210int isl_tab_add_valid_eq(struct isl_tab *tab, isl_int *eq) WARN_UNUSED;
211
212int isl_tab_freeze_constraint(struct isl_tab *tab, int con) WARN_UNUSED;
213
214int isl_tab_track_bmap(struct isl_tab *tab, __isl_take isl_basic_map *bmap) WARN_UNUSED;
215int isl_tab_track_bset(struct isl_tab *tab, __isl_take isl_basic_set *bset) WARN_UNUSED;
216__isl_keep isl_basic_set *isl_tab_peek_bset(struct isl_tab *tab);
217
218int isl_tab_is_equality(struct isl_tab *tab, int con);
219int isl_tab_is_redundant(struct isl_tab *tab, int con);
220
221int isl_tab_sample_is_integer(struct isl_tab *tab);
222struct isl_vec *isl_tab_get_sample_value(struct isl_tab *tab);
223
224enum isl_ineq_type {
225	isl_ineq_error = -1,
226	isl_ineq_redundant,
227	isl_ineq_separate,
228	isl_ineq_cut,
229	isl_ineq_adj_eq,
230	isl_ineq_adj_ineq,
231};
232
233enum isl_ineq_type isl_tab_ineq_type(struct isl_tab *tab, isl_int *ineq);
234
235struct isl_tab_undo *isl_tab_snap(struct isl_tab *tab);
236int isl_tab_rollback(struct isl_tab *tab, struct isl_tab_undo *snap) WARN_UNUSED;
237
238struct isl_tab *isl_tab_relax(struct isl_tab *tab, int con) WARN_UNUSED;
239int isl_tab_select_facet(struct isl_tab *tab, int con) WARN_UNUSED;
240int isl_tab_unrestrict(struct isl_tab *tab, int con) WARN_UNUSED;
241
242void isl_tab_dump(__isl_keep struct isl_tab *tab);
243
244struct isl_map *isl_tab_basic_map_partial_lexopt(
245		struct isl_basic_map *bmap, struct isl_basic_set *dom,
246		struct isl_set **empty, int max);
247__isl_give isl_pw_multi_aff *isl_basic_map_partial_lexopt_pw_multi_aff(
248	__isl_take isl_basic_map *bmap, __isl_take isl_basic_set *dom,
249	__isl_give isl_set **empty, int max);
250
251/* An isl_region represents a sequence of consecutive variables.
252 * pos is the location (starting at 0) of the first variable in the sequence.
253 */
254struct isl_region {
255	int pos;
256	int len;
257};
258
259__isl_give isl_vec *isl_tab_basic_set_non_trivial_lexmin(
260	__isl_take isl_basic_set *bset, int n_op, int n_region,
261	struct isl_region *region,
262	int (*conflict)(int con, void *user), void *user);
263__isl_give isl_vec *isl_tab_basic_set_non_neg_lexmin(
264	__isl_take isl_basic_set *bset);
265
266/* private */
267
268struct isl_tab_var *isl_tab_var_from_row(struct isl_tab *tab, int i);
269int isl_tab_mark_redundant(struct isl_tab *tab, int row) WARN_UNUSED;
270int isl_tab_mark_empty(struct isl_tab *tab) WARN_UNUSED;
271struct isl_tab *isl_tab_dup(struct isl_tab *tab);
272struct isl_tab *isl_tab_product(struct isl_tab *tab1, struct isl_tab *tab2);
273int isl_tab_extend_cons(struct isl_tab *tab, unsigned n_new) WARN_UNUSED;
274int isl_tab_allocate_con(struct isl_tab *tab) WARN_UNUSED;
275int isl_tab_extend_vars(struct isl_tab *tab, unsigned n_new) WARN_UNUSED;
276int isl_tab_allocate_var(struct isl_tab *tab) WARN_UNUSED;
277int isl_tab_pivot(struct isl_tab *tab, int row, int col) WARN_UNUSED;
278int isl_tab_add_row(struct isl_tab *tab, isl_int *line) WARN_UNUSED;
279int isl_tab_row_is_redundant(struct isl_tab *tab, int row);
280int isl_tab_min_at_most_neg_one(struct isl_tab *tab, struct isl_tab_var *var);
281int isl_tab_sign_of_max(struct isl_tab *tab, int con);
282int isl_tab_kill_col(struct isl_tab *tab, int col) WARN_UNUSED;
283
284int isl_tab_push(struct isl_tab *tab, enum isl_tab_undo_type type) WARN_UNUSED;
285int isl_tab_push_var(struct isl_tab *tab,
286	enum isl_tab_undo_type type, struct isl_tab_var *var) WARN_UNUSED;
287int isl_tab_push_basis(struct isl_tab *tab) WARN_UNUSED;
288
289struct isl_tab *isl_tab_init_samples(struct isl_tab *tab) WARN_UNUSED;
290struct isl_tab *isl_tab_add_sample(struct isl_tab *tab,
291	__isl_take isl_vec *sample) WARN_UNUSED;
292struct isl_tab *isl_tab_drop_sample(struct isl_tab *tab, int s);
293int isl_tab_save_samples(struct isl_tab *tab) WARN_UNUSED;
294
295struct isl_tab *isl_tab_detect_equalities(struct isl_tab *tab,
296	struct isl_tab *tab_cone) WARN_UNUSED;
297
298int isl_tab_push_callback(struct isl_tab *tab,
299	struct isl_tab_callback *callback) WARN_UNUSED;
300
301int isl_tab_add_div(struct isl_tab *tab, __isl_keep isl_vec *div,
302	int (*add_ineq)(void *user, isl_int *), void *user);
303
304#endif
305