isl_affine_hull.c: outside_point: check for obvious candidate first
[platform/upstream/isl.git] / isl_affine_hull.c
1 #include "isl_ctx.h"
2 #include "isl_seq.h"
3 #include "isl_set.h"
4 #include "isl_lp.h"
5 #include "isl_map.h"
6 #include "isl_map_private.h"
7 #include "isl_equalities.h"
8 #include "isl_sample.h"
9 #include "isl_tab.h"
10
11 struct isl_basic_map *isl_basic_map_implicit_equalities(
12                                                 struct isl_basic_map *bmap)
13 {
14         struct isl_tab *tab;
15
16         if (!bmap)
17                 return bmap;
18
19         bmap = isl_basic_map_gauss(bmap, NULL);
20         if (ISL_F_ISSET(bmap, ISL_BASIC_MAP_EMPTY))
21                 return bmap;
22         if (ISL_F_ISSET(bmap, ISL_BASIC_MAP_NO_IMPLICIT))
23                 return bmap;
24         if (bmap->n_ineq <= 1)
25                 return bmap;
26
27         tab = isl_tab_from_basic_map(bmap);
28         tab = isl_tab_detect_equalities(bmap->ctx, tab);
29         bmap = isl_basic_map_update_from_tab(bmap, tab);
30         isl_tab_free(bmap->ctx, tab);
31         ISL_F_SET(bmap, ISL_BASIC_MAP_NO_IMPLICIT);
32         return bmap;
33 }
34
35 /* Make eq[row][col] of both bmaps equal so we can add the row
36  * add the column to the common matrix.
37  * Note that because of the echelon form, the columns of row row
38  * after column col are zero.
39  */
40 static void set_common_multiple(
41         struct isl_basic_set *bset1, struct isl_basic_set *bset2,
42         unsigned row, unsigned col)
43 {
44         isl_int m, c;
45
46         if (isl_int_eq(bset1->eq[row][col], bset2->eq[row][col]))
47                 return;
48
49         isl_int_init(c);
50         isl_int_init(m);
51         isl_int_lcm(m, bset1->eq[row][col], bset2->eq[row][col]);
52         isl_int_divexact(c, m, bset1->eq[row][col]);
53         isl_seq_scale(bset1->eq[row], bset1->eq[row], c, col+1);
54         isl_int_divexact(c, m, bset2->eq[row][col]);
55         isl_seq_scale(bset2->eq[row], bset2->eq[row], c, col+1);
56         isl_int_clear(c);
57         isl_int_clear(m);
58 }
59
60 /* Delete a given equality, moving all the following equalities one up.
61  */
62 static void delete_row(struct isl_basic_set *bset, unsigned row)
63 {
64         isl_int *t;
65         int r;
66
67         t = bset->eq[row];
68         bset->n_eq--;
69         for (r = row; r < bset->n_eq; ++r)
70                 bset->eq[r] = bset->eq[r+1];
71         bset->eq[bset->n_eq] = t;
72 }
73
74 /* Make first row entries in column col of bset1 identical to
75  * those of bset2, using the fact that entry bset1->eq[row][col]=a
76  * is non-zero.  Initially, these elements of bset1 are all zero.
77  * For each row i < row, we set
78  *              A[i] = a * A[i] + B[i][col] * A[row]
79  *              B[i] = a * B[i]
80  * so that
81  *              A[i][col] = B[i][col] = a * old(B[i][col])
82  */
83 static void construct_column(
84         struct isl_basic_set *bset1, struct isl_basic_set *bset2,
85         unsigned row, unsigned col)
86 {
87         int r;
88         isl_int a;
89         isl_int b;
90         unsigned total;
91
92         isl_int_init(a);
93         isl_int_init(b);
94         total = 1 + isl_basic_set_n_dim(bset1);
95         for (r = 0; r < row; ++r) {
96                 if (isl_int_is_zero(bset2->eq[r][col]))
97                         continue;
98                 isl_int_gcd(b, bset2->eq[r][col], bset1->eq[row][col]);
99                 isl_int_divexact(a, bset1->eq[row][col], b);
100                 isl_int_divexact(b, bset2->eq[r][col], b);
101                 isl_seq_combine(bset1->eq[r], a, bset1->eq[r],
102                                               b, bset1->eq[row], total);
103                 isl_seq_scale(bset2->eq[r], bset2->eq[r], a, total);
104         }
105         isl_int_clear(a);
106         isl_int_clear(b);
107         delete_row(bset1, row);
108 }
109
110 /* Make first row entries in column col of bset1 identical to
111  * those of bset2, using only these entries of the two matrices.
112  * Let t be the last row with different entries.
113  * For each row i < t, we set
114  *      A[i] = (A[t][col]-B[t][col]) * A[i] + (B[i][col]-A[i][col) * A[t]
115  *      B[i] = (A[t][col]-B[t][col]) * B[i] + (B[i][col]-A[i][col) * B[t]
116  * so that
117  *      A[i][col] = B[i][col] = old(A[t][col]*B[i][col]-A[i][col]*B[t][col])
118  */
119 static int transform_column(
120         struct isl_basic_set *bset1, struct isl_basic_set *bset2,
121         unsigned row, unsigned col)
122 {
123         int i, t;
124         isl_int a, b, g;
125         unsigned total;
126
127         for (t = row-1; t >= 0; --t)
128                 if (isl_int_ne(bset1->eq[t][col], bset2->eq[t][col]))
129                         break;
130         if (t < 0)
131                 return 0;
132
133         total = 1 + isl_basic_set_n_dim(bset1);
134         isl_int_init(a);
135         isl_int_init(b);
136         isl_int_init(g);
137         isl_int_sub(b, bset1->eq[t][col], bset2->eq[t][col]);
138         for (i = 0; i < t; ++i) {
139                 isl_int_sub(a, bset2->eq[i][col], bset1->eq[i][col]);
140                 isl_int_gcd(g, a, b);
141                 isl_int_divexact(a, a, g);
142                 isl_int_divexact(g, b, g);
143                 isl_seq_combine(bset1->eq[i], g, bset1->eq[i], a, bset1->eq[t],
144                                 total);
145                 isl_seq_combine(bset2->eq[i], g, bset2->eq[i], a, bset2->eq[t],
146                                 total);
147         }
148         isl_int_clear(a);
149         isl_int_clear(b);
150         isl_int_clear(g);
151         delete_row(bset1, t);
152         delete_row(bset2, t);
153         return 1;
154 }
155
156 /* The implementation is based on Section 5.2 of Michael Karr,
157  * "Affine Relationships Among Variables of a Program",
158  * except that the echelon form we use starts from the last column
159  * and that we are dealing with integer coefficients.
160  */
161 static struct isl_basic_set *affine_hull(
162         struct isl_basic_set *bset1, struct isl_basic_set *bset2)
163 {
164         unsigned total;
165         int col;
166         int row;
167
168         total = 1 + isl_basic_set_n_dim(bset1);
169
170         row = 0;
171         for (col = total-1; col >= 0; --col) {
172                 int is_zero1 = row >= bset1->n_eq ||
173                         isl_int_is_zero(bset1->eq[row][col]);
174                 int is_zero2 = row >= bset2->n_eq ||
175                         isl_int_is_zero(bset2->eq[row][col]);
176                 if (!is_zero1 && !is_zero2) {
177                         set_common_multiple(bset1, bset2, row, col);
178                         ++row;
179                 } else if (!is_zero1 && is_zero2) {
180                         construct_column(bset1, bset2, row, col);
181                 } else if (is_zero1 && !is_zero2) {
182                         construct_column(bset2, bset1, row, col);
183                 } else {
184                         if (transform_column(bset1, bset2, row, col))
185                                 --row;
186                 }
187         }
188         isl_basic_set_free(bset2);
189         isl_assert(ctx, row == bset1->n_eq, goto error);
190         return bset1;
191 error:
192         isl_basic_set_free(bset1);
193         return NULL;
194 }
195
196 static struct isl_basic_set *isl_basic_set_from_vec(struct isl_ctx *ctx,
197         struct isl_vec *vec)
198 {
199         int i;
200         int k;
201         struct isl_basic_set *bset = NULL;
202         unsigned dim;
203
204         if (!vec)
205                 return NULL;
206         isl_assert(ctx, vec->size != 0, goto error);
207
208         bset = isl_basic_set_alloc(ctx, 0, vec->size - 1, 0, vec->size - 1, 0);
209         if (!bset)
210                 goto error;
211         dim = isl_basic_set_n_dim(bset);
212         for (i = dim - 1; i >= 0; --i) {
213                 k = isl_basic_set_alloc_equality(bset);
214                 if (k < 0)
215                         goto error;
216                 isl_seq_clr(bset->eq[k], 1 + dim);
217                 isl_int_neg(bset->eq[k][0], vec->block.data[1 + i]);
218                 isl_int_set(bset->eq[k][1 + i], vec->block.data[0]);
219         }
220         isl_vec_free(ctx, vec);
221
222         return bset;
223 error:
224         isl_basic_set_free(bset);
225         isl_vec_free(ctx, vec);
226         return NULL;
227 }
228
229 /* Find an integer point in "bset" that lies outside of the equality
230  * "eq" e(x) = 0.
231  * If "up" is true, look for a point satisfying e(x) - 1 >= 0.
232  * Otherwise, look for a point satisfying -e(x) - 1 >= 0 (i.e., e(x) <= -1).
233  * The point, if found, is returned as a singleton set.
234  * If no point can be found, the empty set is returned.
235  *
236  * Before solving an ILP problem, we first check if simply
237  * adding the normal of the constraint to one of the known
238  * integer points in the basic set yields another point
239  * inside the basic set.
240  */
241 static struct isl_basic_set *outside_point(struct isl_ctx *ctx,
242         struct isl_basic_set *bset, isl_int *eq, int up)
243 {
244         struct isl_basic_set *slice = NULL;
245         struct isl_vec *sample;
246         struct isl_basic_set *point;
247         unsigned dim;
248         int k;
249
250         dim = isl_basic_set_n_dim(bset);
251         sample = isl_vec_alloc(ctx, 1 + dim);
252         if (!sample)
253                 return NULL;
254         isl_int_set_si(sample->block.data[0], 1);
255         isl_seq_combine(sample->block.data + 1,
256                 ctx->one, bset->sample->block.data + 1,
257                 up ? ctx->one : ctx->negone, eq + 1, dim);
258         if (isl_basic_set_contains(bset, sample))
259                 return isl_basic_set_from_vec(ctx, sample);
260         isl_vec_free(ctx, sample);
261         sample = NULL;
262
263         slice = isl_basic_set_copy(bset);
264         if (!slice)
265                 goto error;
266         slice = isl_basic_set_cow(slice);
267         slice = isl_basic_set_extend(slice, 0, dim, 0, 0, 1);
268         k = isl_basic_set_alloc_inequality(slice);
269         if (k < 0)
270                 goto error;
271         if (up)
272                 isl_seq_cpy(slice->ineq[k], eq, 1 + dim);
273         else
274                 isl_seq_neg(slice->ineq[k], eq, 1 + dim);
275         isl_int_sub_ui(slice->ineq[k][0], slice->ineq[k][0], 1);
276
277         sample = isl_basic_set_sample(slice);
278         if (!sample)
279                 goto error;
280         if (sample->size == 0) {
281                 isl_vec_free(ctx, sample);
282                 point = isl_basic_set_empty_like(bset);
283         } else
284                 point = isl_basic_set_from_vec(ctx, sample);
285
286         return point;
287 error:
288         isl_basic_set_free(slice);
289         return NULL;
290 }
291
292 /* Look for all equalities satisfied by the integer points in bset,
293  * which is assume not to have any explicit equalities.
294  *
295  * The equalities are obtained by successively looking for
296  * a point that is affinely independent of the points found so far.
297  * In particular, for each equality satisfied by the points so far,
298  * we check if there is any point on a hyperplane parallel to the
299  * corresponding hyperplane shifted by at least one (in either direction).
300  */
301 static struct isl_basic_set *uset_affine_hull(struct isl_basic_set *bset)
302 {
303         int i, j;
304         struct isl_basic_set *hull = NULL;
305         struct isl_vec *sample;
306         struct isl_ctx *ctx;
307         unsigned dim;
308
309         if (isl_basic_set_is_empty(bset))
310                 return bset;
311
312         ctx = bset->ctx;
313         sample = isl_basic_set_sample(isl_basic_set_copy(bset));
314         if (!sample)
315                 goto error;
316         if (sample->size == 0) {
317                 isl_vec_free(ctx, sample);
318                 hull = isl_basic_set_empty_like(bset);
319                 isl_basic_set_free(bset);
320                 return hull;
321         } else
322                 hull = isl_basic_set_from_vec(ctx, sample);
323
324         dim = isl_basic_set_n_dim(bset);
325         for (i = 0; i < dim; ++i) {
326                 struct isl_basic_set *point;
327                 for (j = 0; j < hull->n_eq; ++j) {
328                         point = outside_point(ctx, bset, hull->eq[j], 1);
329                         if (!point)
330                                 goto error;
331                         if (!ISL_F_ISSET(point, ISL_BASIC_SET_EMPTY))
332                                 break;
333                         isl_basic_set_free(point);
334                         point = outside_point(ctx, bset, hull->eq[j], 0);
335                         if (!point)
336                                 goto error;
337                         if (!ISL_F_ISSET(point, ISL_BASIC_SET_EMPTY))
338                                 break;
339                         isl_basic_set_free(point);
340                 }
341                 if (j == hull->n_eq)
342                         break;
343                 hull = affine_hull(hull, point);
344         }
345         isl_basic_set_free(bset);
346
347         return hull;
348 error:
349         isl_basic_set_free(bset);
350         isl_basic_set_free(hull);
351         return NULL;
352 }
353
354 /* Look for all equalities satisfied by the integer points in bmap
355  * that are independent of the equalities already explicitly available
356  * in bmap.
357  *
358  * We first remove all equalities already explicitly available,
359  * then look for additional equalities in the reduced space
360  * and then transform the result to the original space.
361  * The original equalities are _not_ added to this set.  This is
362  * the responsibility of the calling function.
363  * The resulting basic set has all meaning about the dimensions removed.
364  * In particular, dimensions that correspond to existential variables
365  * in bmap and that are found to be fixed are not removed.
366  */
367 static struct isl_basic_set *equalities_in_underlying_set(
368                                                 struct isl_basic_map *bmap)
369 {
370         struct isl_mat *T2 = NULL;
371         struct isl_basic_set *bset = NULL;
372         struct isl_basic_set *hull = NULL;
373         struct isl_ctx *ctx;
374
375         ctx = bmap->ctx;
376         bset = isl_basic_map_underlying_set(bmap);
377         bset = isl_basic_set_remove_equalities(bset, NULL, &T2);
378         if (!bset)
379                 goto error;
380
381         hull = uset_affine_hull(bset);
382         if (T2)
383                 hull = isl_basic_set_preimage(hull, T2);
384
385         return hull;
386 error:
387         isl_mat_free(ctx, T2);
388         isl_basic_set_free(bset);
389         isl_basic_set_free(hull);
390         return NULL;
391 }
392
393 /* Detect and make explicit all equalities satisfied by the (integer)
394  * points in bmap.
395  */
396 struct isl_basic_map *isl_basic_map_detect_equalities(
397                                                 struct isl_basic_map *bmap)
398 {
399         int i, j;
400         struct isl_basic_set *hull = NULL;
401
402         if (!bmap)
403                 return NULL;
404         if (bmap->n_ineq == 0)
405                 return bmap;
406         if (ISL_F_ISSET(bmap, ISL_BASIC_MAP_EMPTY))
407                 return bmap;
408         if (ISL_F_ISSET(bmap, ISL_BASIC_MAP_ALL_EQUALITIES))
409                 return bmap;
410         if (ISL_F_ISSET(bmap, ISL_BASIC_MAP_RATIONAL))
411                 return isl_basic_map_implicit_equalities(bmap);
412
413         hull = equalities_in_underlying_set(isl_basic_map_copy(bmap));
414         if (!hull)
415                 goto error;
416         bmap = isl_basic_map_extend_dim(bmap, isl_dim_copy(bmap->dim), 0,
417                                         hull->n_eq, 0);
418         for (i = 0; i < hull->n_eq; ++i) {
419                 j = isl_basic_map_alloc_equality(bmap);
420                 if (j < 0)
421                         goto error;
422                 isl_seq_cpy(bmap->eq[j], hull->eq[i],
423                                 1 + isl_basic_set_total_dim(hull));
424         }
425         isl_basic_set_free(hull);
426         ISL_F_SET(bmap, ISL_BASIC_MAP_NO_IMPLICIT | ISL_BASIC_MAP_ALL_EQUALITIES);
427         bmap = isl_basic_map_simplify(bmap);
428         return isl_basic_map_finalize(bmap);
429 error:
430         isl_basic_set_free(hull);
431         isl_basic_map_free(bmap);
432         return NULL;
433 }
434
435 struct isl_map *isl_map_detect_equalities(struct isl_map *map)
436 {
437         struct isl_basic_map *bmap;
438         int i;
439
440         if (!map)
441                 return NULL;
442
443         for (i = 0; i < map->n; ++i) {
444                 bmap = isl_basic_map_copy(map->p[i]);
445                 bmap = isl_basic_map_detect_equalities(bmap);
446                 if (!bmap)
447                         goto error;
448                 isl_basic_map_free(map->p[i]);
449                 map->p[i] = bmap;
450         }
451
452         return map;
453 error:
454         isl_map_free(map);
455         return NULL;
456 }
457
458 /* After computing the rational affine hull (by detecting the implicit
459  * equalities), we compute the additional equalities satisfied by
460  * the integer points (if any) and add the original equalities back in.
461  */
462 struct isl_basic_map *isl_basic_map_affine_hull(struct isl_basic_map *bmap)
463 {
464         struct isl_basic_set *hull = NULL;
465
466         bmap = isl_basic_map_detect_equalities(bmap);
467         bmap = isl_basic_map_cow(bmap);
468         isl_basic_map_free_inequality(bmap, bmap->n_ineq);
469         return bmap;
470 }
471
472 struct isl_basic_set *isl_basic_set_affine_hull(struct isl_basic_set *bset)
473 {
474         return (struct isl_basic_set *)
475                 isl_basic_map_affine_hull((struct isl_basic_map *)bset);
476 }
477
478 struct isl_basic_map *isl_map_affine_hull(struct isl_map *map)
479 {
480         int i;
481         struct isl_basic_map *model = NULL;
482         struct isl_basic_map *hull = NULL;
483         struct isl_set *set;
484
485         if (!map)
486                 return NULL;
487
488         if (map->n == 0) {
489                 hull = isl_basic_map_empty_like_map(map);
490                 isl_map_free(map);
491                 return hull;
492         }
493
494         map = isl_map_detect_equalities(map);
495         map = isl_map_align_divs(map);
496         if (!map)
497                 return NULL;
498         model = isl_basic_map_copy(map->p[0]);
499         set = isl_map_underlying_set(map);
500         set = isl_set_cow(set);
501         if (!set)
502                 goto error;
503
504         for (i = 0; i < set->n; ++i) {
505                 set->p[i] = isl_basic_set_cow(set->p[i]);
506                 set->p[i] = isl_basic_set_affine_hull(set->p[i]);
507                 set->p[i] = isl_basic_set_gauss(set->p[i], NULL);
508                 if (!set->p[i])
509                         goto error;
510         }
511         set = isl_set_remove_empty_parts(set);
512         if (set->n == 0) {
513                 hull = isl_basic_map_empty_like(model);
514                 isl_basic_map_free(model);
515         } else {
516                 struct isl_basic_set *bset;
517                 while (set->n > 1) {
518                         set->p[0] = affine_hull(set->p[0], set->p[--set->n]);
519                         if (!set->p[0])
520                                 goto error;
521                 }
522                 bset = isl_basic_set_copy(set->p[0]);
523                 hull = isl_basic_map_overlying_set(bset, model);
524         }
525         isl_set_free(set);
526         hull = isl_basic_map_simplify(hull);
527         return isl_basic_map_finalize(hull);
528 error:
529         isl_basic_map_free(model);
530         isl_set_free(set);
531         return NULL;
532 }
533
534 struct isl_basic_set *isl_set_affine_hull(struct isl_set *set)
535 {
536         return (struct isl_basic_set *)
537                 isl_map_affine_hull((struct isl_map *)set);
538 }