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