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