add isl_basic_map_add_ineq and isl_basic_map_add_eq
[platform/upstream/isl.git] / isl_scan.c
1 #include "isl_basis_reduction.h"
2 #include "isl_scan.h"
3 #include "isl_seq.h"
4 #include "isl_tab.h"
5
6 /* Call callback->add with the current sample value of the tableau "tab".
7  */
8 static int add_solution(struct isl_tab *tab, struct isl_scan_callback *callback)
9 {
10         struct isl_vec *sample;
11
12         if (!tab)
13                 return -1;
14         sample = isl_tab_get_sample_value(tab);
15         if (!sample)
16                 return -1;
17
18         return callback->add(callback, sample);
19 }
20
21 static int scan_0D(struct isl_basic_set *bset,
22         struct isl_scan_callback *callback)
23 {
24         struct isl_vec *sample;
25
26         sample = isl_vec_alloc(bset->ctx, 1);
27         isl_basic_set_free(bset);
28
29         if (!sample)
30                 return -1;
31
32         isl_int_set_si(sample->el[0], 1);
33
34         return callback->add(callback, sample);
35 }
36
37 /* Look for all integer points in "bset", which is assumed to be unbounded,
38  * and call callback->add on each of them.
39  *
40  * We first compute a reduced basis for the set and then scan
41  * the set in the directions of this basis.
42  * We basically perform a depth first search, where in each level i
43  * we compute the range in the i-th basis vector direction, given
44  * fixed values in the directions of the previous basis vector.
45  * We then add an equality to the tableau fixing the value in the
46  * direction of the current basis vector to each value in the range
47  * in turn and then continue to the next level.
48  *
49  * The search is implemented iteratively.  "level" identifies the current
50  * basis vector.  "init" is true if we want the first value at the current
51  * level and false if we want the next value.
52  * Solutions are added in the leaves of the search tree, i.e., after
53  * we have fixed a value in each direction of the basis.
54  */
55 int isl_basic_set_scan(struct isl_basic_set *bset,
56         struct isl_scan_callback *callback)
57 {
58         unsigned dim;
59         struct isl_mat *B = NULL;
60         struct isl_tab *tab = NULL;
61         struct isl_vec *min;
62         struct isl_vec *max;
63         struct isl_tab_undo **snap;
64         int level;
65         int init;
66         enum isl_lp_result res;
67
68         if (!bset)
69                 return -1;
70
71         dim = isl_basic_set_total_dim(bset);
72         if (dim == 0)
73                 return scan_0D(bset, callback);
74
75         min = isl_vec_alloc(bset->ctx, dim);
76         max = isl_vec_alloc(bset->ctx, dim);
77         snap = isl_alloc_array(bset->ctx, struct isl_tab_undo *, dim);
78
79         if (!min || !max || !snap)
80                 goto error;
81
82         tab = isl_tab_from_basic_set(bset);
83         if (!tab)
84                 goto error;
85
86         tab->basis = isl_mat_identity(bset->ctx, 1 + dim);
87         if (1)
88                 tab = isl_tab_compute_reduced_basis(tab);
89         if (!tab)
90                 goto error;
91         B = isl_mat_copy(tab->basis);
92         if (!B)
93                 goto error;
94
95         level = 0;
96         init = 1;
97
98         while (level >= 0) {
99                 int empty = 0;
100                 if (init) {
101                         res = isl_tab_min(tab, B->row[1 + level],
102                                     bset->ctx->one, &min->el[level], NULL, 0);
103                         if (res == isl_lp_empty)
104                                 empty = 1;
105                         if (res == isl_lp_error || res == isl_lp_unbounded)
106                                 goto error;
107                         isl_seq_neg(B->row[1 + level] + 1,
108                                     B->row[1 + level] + 1, dim);
109                         res = isl_tab_min(tab, B->row[1 + level],
110                                     bset->ctx->one, &max->el[level], NULL, 0);
111                         isl_seq_neg(B->row[1 + level] + 1,
112                                     B->row[1 + level] + 1, dim);
113                         isl_int_neg(max->el[level], max->el[level]);
114                         if (res == isl_lp_empty)
115                                 empty = 1;
116                         if (res == isl_lp_error || res == isl_lp_unbounded)
117                                 goto error;
118                         snap[level] = isl_tab_snap(tab);
119                 } else
120                         isl_int_add_ui(min->el[level], min->el[level], 1);
121
122                 if (empty || isl_int_gt(min->el[level], max->el[level])) {
123                         level--;
124                         init = 0;
125                         if (level >= 0)
126                                 if (isl_tab_rollback(tab, snap[level]) < 0)
127                                         goto error;
128                         continue;
129                 }
130                 isl_int_neg(B->row[1 + level][0], min->el[level]);
131                 tab = isl_tab_add_valid_eq(tab, B->row[1 + level]);
132                 isl_int_set_si(B->row[1 + level][0], 0);
133                 if (level < dim - 1) {
134                         ++level;
135                         init = 1;
136                         continue;
137                 }
138                 if (add_solution(tab, callback) < 0)
139                         goto error;
140                 init = 0;
141                 if (isl_tab_rollback(tab, snap[level]) < 0)
142                         goto error;
143         }
144
145         isl_tab_free(tab);
146         free(snap);
147         isl_vec_free(min);
148         isl_vec_free(max);
149         isl_basic_set_free(bset);
150         isl_mat_free(B);
151         return 0;
152 error:
153         isl_tab_free(tab);
154         free(snap);
155         isl_vec_free(min);
156         isl_vec_free(max);
157         isl_basic_set_free(bset);
158         isl_mat_free(B);
159         return -1;
160 }