isl_tab: optionally keep track of row signs
[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 };
18
19 enum isl_tab_undo_type {
20         isl_tab_undo_bottom,
21         isl_tab_undo_empty,
22         isl_tab_undo_nonneg,
23         isl_tab_undo_redundant,
24         isl_tab_undo_zero,
25         isl_tab_undo_allocate,
26         isl_tab_undo_relax,
27         isl_tab_undo_bset_ineq,
28         isl_tab_undo_bset_eq,
29         isl_tab_undo_bset_div,
30         isl_tab_undo_saved_basis,
31 };
32
33 union isl_tab_undo_val {
34         int             var_index;
35         int             *col_var;
36 };
37
38 struct isl_tab_undo {
39         enum isl_tab_undo_type  type;
40         union isl_tab_undo_val  u;
41         struct isl_tab_undo     *next;
42 };
43
44 /* The tableau maintains equality relations.
45  * Each column and each row is associated to a variable or a constraint.
46  * The "value" of an inequality constraint is the value of the corresponding
47  * slack variable.
48  * The "row_var" and "col_var" arrays map column and row indices
49  * to indices in the "var" and "con" arrays.  The elements of these
50  * arrays maintain extra information about the variables and the constraints.
51  * Each row expresses the corresponding row variable as an affine expression
52  * of the column variables.
53  * The first two columns in the matrix contain the common denominator of
54  * the row and the numerator of the constant term.
55  * If "M" is set, then the third column represents the "big parameter".
56  * The third (M = 0) or fourth (M = 1) column
57  * in the matrix is called column 0 with respect to the col_var array.
58  * The sample value of the tableau is the value that assigns zero
59  * to all the column variables and the constant term of each affine
60  * expression to the corresponding row variable.
61  * The operations on the tableau maintain the property that the sample
62  * value satisfies the non-negativity constraints (usually on the slack
63  * variables).
64  *
65  * The big parameter represents an arbitrarily big (and divisible)
66  * positive number.  If present, then the sign of a row is determined
67  * lexicographically, with the sign of the big parameter coefficient
68  * considered first.  The big parameter will only be used while
69  * solving PILP problems.
70  *
71  * The first n_dead column variables have their values fixed to zero.
72  * The corresponding tab_vars are flagged "is_zero".
73  * Some of the rows that have have zero coefficients in all but
74  * the dead columns are also flagged "is_zero".
75  *
76  * The first n_redundant rows correspond to inequality constraints
77  * that are always satisfied for any value satisfying the non-redundant
78  * rows.  The corresponding tab_vars are flagged "is_redundant".
79  * A row variable that is flagged "is_zero" is also flagged "is_redundant"
80  * since the constraint has been reduced to 0 = 0 and is therefore always
81  * satisfied.
82  *
83  * There are "n_var" variables in total.  The first "n_param" of these
84  * are called parameters and the last "n_div" of these are called divs.
85  * The basic tableau operations makes no distinction between different
86  * kinds of variables.
87  *
88  * Dead columns and redundant rows are detected on the fly.
89  * However, the basic operations do not ensure that all dead columns
90  * or all redundant rows are detected.
91  * isl_tab_detect_equalities and isl_tab_detect_redundant can be used
92  * to perform and exhaustive search for dead columns and redundant rows.
93  */
94 enum isl_tab_row_sign {
95         isl_tab_row_unknown = 0,
96         isl_tab_row_pos,
97         isl_tab_row_neg,
98         isl_tab_row_any,
99 };
100 struct isl_tab {
101         struct isl_mat *mat;
102
103         unsigned n_row;
104         unsigned n_col;
105         unsigned n_dead;
106         unsigned n_redundant;
107
108         unsigned n_var;
109         unsigned n_param;
110         unsigned n_div;
111         unsigned max_var;
112         unsigned n_con;
113         unsigned n_eq;
114         unsigned max_con;
115         struct isl_tab_var *var;
116         struct isl_tab_var *con;
117         int *row_var;   /* v >= 0 -> var v;     v < 0 -> con ~v */
118         int *col_var;   /* v >= 0 -> var v;     v < 0 -> con ~v */
119         enum isl_tab_row_sign *row_sign;
120
121         struct isl_tab_undo bottom;
122         struct isl_tab_undo *top;
123
124         struct isl_vec *dual;
125         struct isl_basic_set *bset;
126
127         unsigned need_undo : 1;
128         unsigned rational : 1;
129         unsigned empty : 1;
130         unsigned in_undo : 1;
131         unsigned M : 1;
132 };
133
134 struct isl_tab *isl_tab_alloc(struct isl_ctx *ctx,
135         unsigned n_row, unsigned n_var, unsigned M);
136 void isl_tab_free(struct isl_tab *tab);
137
138 struct isl_tab *isl_tab_from_basic_map(struct isl_basic_map *bmap);
139 struct isl_tab *isl_tab_from_basic_set(struct isl_basic_set *bset);
140 struct isl_tab *isl_tab_from_recession_cone(struct isl_basic_map *bmap);
141 int isl_tab_cone_is_bounded(struct isl_tab *tab);
142 struct isl_basic_map *isl_basic_map_update_from_tab(struct isl_basic_map *bmap,
143         struct isl_tab *tab);
144 struct isl_basic_set *isl_basic_set_update_from_tab(struct isl_basic_set *bset,
145         struct isl_tab *tab);
146 struct isl_tab *isl_tab_detect_equalities(struct isl_tab *tab);
147 struct isl_tab *isl_tab_detect_redundant(struct isl_tab *tab);
148 #define ISL_TAB_SAVE_DUAL       (1 << 0)
149 enum isl_lp_result isl_tab_min(struct isl_tab *tab,
150         isl_int *f, isl_int denom, isl_int *opt, isl_int *opt_denom,
151         unsigned flags);
152
153 struct isl_tab *isl_tab_extend(struct isl_tab *tab, unsigned n_new);
154 struct isl_tab *isl_tab_add_ineq(struct isl_tab *tab, isl_int *ineq);
155 struct isl_tab *isl_tab_add_valid_eq(struct isl_tab *tab, isl_int *eq);
156
157 int isl_tab_is_equality(struct isl_tab *tab, int con);
158 int isl_tab_is_redundant(struct isl_tab *tab, int con);
159
160 int isl_tab_sample_is_integer(struct isl_tab *tab);
161 struct isl_vec *isl_tab_get_sample_value(struct isl_tab *tab);
162
163 enum isl_ineq_type {
164         isl_ineq_error = -1,
165         isl_ineq_redundant,
166         isl_ineq_separate,
167         isl_ineq_cut,
168         isl_ineq_adj_eq,
169         isl_ineq_adj_ineq,
170 };
171
172 enum isl_ineq_type isl_tab_ineq_type(struct isl_tab *tab, isl_int *ineq);
173
174 struct isl_tab_undo *isl_tab_snap(struct isl_tab *tab);
175 int isl_tab_rollback(struct isl_tab *tab, struct isl_tab_undo *snap);
176
177 struct isl_tab *isl_tab_relax(struct isl_tab *tab, int con);
178 struct isl_tab *isl_tab_select_facet(struct isl_tab *tab, int con);
179
180 void isl_tab_dump(struct isl_tab *tab, FILE *out, int indent);
181
182 /* private */
183
184 struct isl_tab_var *isl_tab_var_from_row(struct isl_tab *tab, int i);
185 int isl_tab_mark_redundant(struct isl_tab *tab, int row);
186 struct isl_tab *isl_tab_mark_empty(struct isl_tab *tab);
187 struct isl_tab *isl_tab_dup(struct isl_tab *tab);
188 int isl_tab_extend_cons(struct isl_tab *tab, unsigned n_new);
189 int isl_tab_allocate_con(struct isl_tab *tab);
190 int isl_tab_extend_vars(struct isl_tab *tab, unsigned n_new);
191 int isl_tab_allocate_var(struct isl_tab *tab);
192 void isl_tab_pivot(struct isl_tab *tab, int row, int col);
193 int isl_tab_add_row(struct isl_tab *tab, isl_int *line);
194 int isl_tab_row_is_redundant(struct isl_tab *tab, int row);
195 int isl_tab_min_at_most_neg_one(struct isl_tab *tab, struct isl_tab_var *var);
196
197 void isl_tab_push(struct isl_tab *tab, enum isl_tab_undo_type type);
198 void isl_tab_push_var(struct isl_tab *tab,
199         enum isl_tab_undo_type type, struct isl_tab_var *var);
200 void isl_tab_push_basis(struct isl_tab *tab);
201
202 #endif