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