isl_tab: privately export some functionality
[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
8 struct isl_tab_var {
9         int index;
10         unsigned is_row : 1;
11         unsigned is_nonneg : 1;
12         unsigned is_zero : 1;
13         unsigned is_redundant : 1;
14         unsigned marked : 1;
15         unsigned frozen : 1;
16 };
17
18 enum isl_tab_undo_type {
19         isl_tab_undo_bottom,
20         isl_tab_undo_empty,
21         isl_tab_undo_nonneg,
22         isl_tab_undo_redundant,
23         isl_tab_undo_zero,
24         isl_tab_undo_allocate,
25         isl_tab_undo_relax,
26 };
27
28 union isl_tab_undo_val {
29         int             var_index;
30 };
31
32 struct isl_tab_undo {
33         enum isl_tab_undo_type  type;
34         union isl_tab_undo_val  u;
35         struct isl_tab_undo     *next;
36 };
37
38 /* The tableau maintains equality relations.
39  * Each column and each row is associated to a variable or a constraint.
40  * The "value" of an inequality constraint is the value of the corresponding
41  * slack variable.
42  * The "row_var" and "col_var" arrays map column and row indices
43  * to indices in the "var" and "con" arrays.  The elements of these
44  * arrays maintain extra information about the variables and the constraints.
45  * Each row expresses the corresponding row variable as an affine expression
46  * of the column variables.
47  * The first two columns in the matrix contain the common denominator of
48  * the row and the numerator of the constant term.  The third column
49  * in the matrix is called column 0 with respect to the col_var array.
50  * The sample value of the tableau is the value that assigns zero
51  * to all the column variables and the constant term of each affine
52  * expression to the corresponding row variable.
53  * The operations on the tableau maintain the property that the sample
54  * value satisfies the non-negativity constraints (usually on the slack
55  * variables).
56  *
57  * The first n_dead column variables have their values fixed to zero.
58  * The corresponding tab_vars are flagged "is_zero".
59  * Some of the rows that have have zero coefficients in all but
60  * the dead columns are also flagged "is_zero".
61  *
62  * The first n_redundant rows correspond to inequality constraints
63  * that are always satisfied for any value satisfying the non-redundant
64  * rows.  The corresponding tab_vars are flagged "is_redundant".
65  * A row variable that is flagged "is_zero" is also flagged "is_redundant"
66  * since the constraint has been reduced to 0 = 0 and is therefore always
67  * satisfied.
68  *
69  * Dead columns and redundant rows are detected on the fly.
70  * However, the basic operations do not ensure that all dead columns
71  * or all redundant rows are detected.
72  * isl_tab_detect_equalities and isl_tab_detect_redundant can be used
73  * to peform and exhaustive search for dead columns and redundant rows.
74  */
75 struct isl_tab {
76         struct isl_mat *mat;
77
78         unsigned n_row;
79         unsigned n_col;
80         unsigned n_dead;
81         unsigned n_redundant;
82
83         unsigned n_var;
84         unsigned n_con;
85         unsigned n_eq;
86         unsigned max_con;
87         struct isl_tab_var *var;
88         struct isl_tab_var *con;
89         int *row_var;   /* v >= 0 -> var v;     v < 0 -> con ~v */
90         int *col_var;   /* v >= 0 -> var v;     v < 0 -> con ~v */
91
92         struct isl_tab_undo bottom;
93         struct isl_tab_undo *top;
94
95         struct isl_vec *dual;
96
97         unsigned need_undo : 1;
98         unsigned rational : 1;
99         unsigned empty : 1;
100         unsigned in_undo : 1;
101 };
102
103 struct isl_tab *isl_tab_alloc(struct isl_ctx *ctx,
104         unsigned n_row, unsigned n_var);
105 void isl_tab_free(struct isl_tab *tab);
106
107 struct isl_tab *isl_tab_from_basic_map(struct isl_basic_map *bmap);
108 struct isl_tab *isl_tab_from_basic_set(struct isl_basic_set *bset);
109 struct isl_tab *isl_tab_from_recession_cone(struct isl_basic_map *bmap);
110 int isl_tab_cone_is_bounded(struct isl_tab *tab);
111 struct isl_basic_map *isl_basic_map_update_from_tab(struct isl_basic_map *bmap,
112         struct isl_tab *tab);
113 struct isl_basic_set *isl_basic_set_update_from_tab(struct isl_basic_set *bset,
114         struct isl_tab *tab);
115 struct isl_tab *isl_tab_detect_equalities(struct isl_tab *tab);
116 struct isl_tab *isl_tab_detect_redundant(struct isl_tab *tab);
117 #define ISL_TAB_SAVE_DUAL       (1 << 0)
118 enum isl_lp_result isl_tab_min(struct isl_tab *tab,
119         isl_int *f, isl_int denom, isl_int *opt, isl_int *opt_denom,
120         unsigned flags);
121
122 struct isl_tab *isl_tab_extend(struct isl_tab *tab, unsigned n_new);
123 struct isl_tab *isl_tab_add_ineq(struct isl_tab *tab, isl_int *ineq);
124 struct isl_tab *isl_tab_add_valid_eq(struct isl_tab *tab, isl_int *eq);
125
126 int isl_tab_is_equality(struct isl_tab *tab, int con);
127 int isl_tab_is_redundant(struct isl_tab *tab, int con);
128
129 int isl_tab_sample_is_integer(struct isl_tab *tab);
130 struct isl_vec *isl_tab_get_sample_value(struct isl_tab *tab);
131
132 enum isl_ineq_type {
133         isl_ineq_error = -1,
134         isl_ineq_redundant,
135         isl_ineq_separate,
136         isl_ineq_cut,
137         isl_ineq_adj_eq,
138         isl_ineq_adj_ineq,
139 };
140
141 enum isl_ineq_type isl_tab_ineq_type(struct isl_tab *tab, isl_int *ineq);
142
143 struct isl_tab_undo *isl_tab_snap(struct isl_tab *tab);
144 int isl_tab_rollback(struct isl_tab *tab, struct isl_tab_undo *snap);
145
146 struct isl_tab *isl_tab_relax(struct isl_tab *tab, int con);
147 struct isl_tab *isl_tab_select_facet(struct isl_tab *tab, int con);
148
149 void isl_tab_dump(struct isl_tab *tab, FILE *out, int indent);
150
151 /* private */
152
153 struct isl_tab_var *isl_tab_var_from_row(struct isl_tab *tab, int i);
154 int isl_tab_mark_redundant(struct isl_tab *tab, int row);
155 struct isl_tab *isl_tab_mark_empty(struct isl_tab *tab);
156 struct isl_tab *isl_tab_dup(struct isl_tab *tab);
157 int isl_tab_extend_cons(struct isl_tab *tab, unsigned n_new);
158 int isl_tab_allocate_con(struct isl_tab *tab);
159 void isl_tab_pivot(struct isl_tab *tab, int row, int col);
160 int isl_tab_add_row(struct isl_tab *tab, isl_int *line);
161 int isl_tab_row_is_redundant(struct isl_tab *tab, int row);
162 int isl_tab_min_at_most_neg_one(struct isl_tab *tab, struct isl_tab_var *var);
163
164 void isl_tab_push(struct isl_tab *tab, enum isl_tab_undo_type type);
165 void isl_tab_push_var(struct isl_tab *tab,
166         enum isl_tab_undo_type type, struct isl_tab_var *var);
167
168 #endif