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