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