add isl_basic_map_fast_is_empty
[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 struct isl_map *isl_map_implicit_equalities(struct isl_map *map)
44 {
45         int i;
46
47         if (!map)
48                 return map;
49
50         for (i = 0; i < map->n; ++i) {
51                 map->p[i] = isl_basic_map_implicit_equalities(map->p[i]);
52                 if (!map->p[i])
53                         goto error;
54         }
55
56         return map;
57 error:
58         isl_map_free(map);
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         bset1 = isl_basic_set_normalize_constraints(bset1);
218         return bset1;
219 error:
220         isl_basic_set_free(bset1);
221         return NULL;
222 }
223
224 static struct isl_basic_set *isl_basic_set_from_vec(struct isl_ctx *ctx,
225         struct isl_vec *vec)
226 {
227         int i;
228         int k;
229         struct isl_basic_set *bset = NULL;
230         unsigned dim;
231
232         if (!vec)
233                 return NULL;
234         isl_assert(ctx, vec->size != 0, goto error);
235
236         bset = isl_basic_set_alloc(ctx, 0, vec->size - 1, 0, vec->size - 1, 0);
237         if (!bset)
238                 goto error;
239         dim = isl_basic_set_n_dim(bset);
240         for (i = dim - 1; i >= 0; --i) {
241                 k = isl_basic_set_alloc_equality(bset);
242                 if (k < 0)
243                         goto error;
244                 isl_seq_clr(bset->eq[k], 1 + dim);
245                 isl_int_neg(bset->eq[k][0], vec->block.data[1 + i]);
246                 isl_int_set(bset->eq[k][1 + i], vec->block.data[0]);
247         }
248         isl_vec_free(ctx, vec);
249
250         return bset;
251 error:
252         isl_basic_set_free(bset);
253         isl_vec_free(ctx, vec);
254         return NULL;
255 }
256
257 /* Find an integer point in "bset" that lies outside of the equality
258  * "eq" e(x) = 0.
259  * If "up" is true, look for a point satisfying e(x) - 1 >= 0.
260  * Otherwise, look for a point satisfying -e(x) - 1 >= 0 (i.e., e(x) <= -1).
261  * The point, if found, is returned as a singleton set.
262  * If no point can be found, the empty set is returned.
263  *
264  * Before solving an ILP problem, we first check if simply
265  * adding the normal of the constraint to one of the known
266  * integer points in the basic set yields another point
267  * inside the basic set.
268  */
269 static struct isl_basic_set *outside_point(struct isl_ctx *ctx,
270         struct isl_basic_set *bset, isl_int *eq, int up)
271 {
272         struct isl_basic_set *slice = NULL;
273         struct isl_vec *sample;
274         struct isl_basic_set *point;
275         unsigned dim;
276         int k;
277
278         dim = isl_basic_set_n_dim(bset);
279         sample = isl_vec_alloc(ctx, 1 + dim);
280         if (!sample)
281                 return NULL;
282         isl_int_set_si(sample->block.data[0], 1);
283         isl_seq_combine(sample->block.data + 1,
284                 ctx->one, bset->sample->block.data + 1,
285                 up ? ctx->one : ctx->negone, eq + 1, dim);
286         if (isl_basic_set_contains(bset, sample))
287                 return isl_basic_set_from_vec(ctx, sample);
288         isl_vec_free(ctx, sample);
289         sample = NULL;
290
291         slice = isl_basic_set_copy(bset);
292         if (!slice)
293                 goto error;
294         slice = isl_basic_set_cow(slice);
295         slice = isl_basic_set_extend(slice, 0, dim, 0, 0, 1);
296         k = isl_basic_set_alloc_inequality(slice);
297         if (k < 0)
298                 goto error;
299         if (up)
300                 isl_seq_cpy(slice->ineq[k], eq, 1 + dim);
301         else
302                 isl_seq_neg(slice->ineq[k], eq, 1 + dim);
303         isl_int_sub_ui(slice->ineq[k][0], slice->ineq[k][0], 1);
304
305         sample = isl_basic_set_sample(slice);
306         if (!sample)
307                 goto error;
308         if (sample->size == 0) {
309                 isl_vec_free(ctx, sample);
310                 point = isl_basic_set_empty_like(bset);
311         } else
312                 point = isl_basic_set_from_vec(ctx, sample);
313
314         return point;
315 error:
316         isl_basic_set_free(slice);
317         return NULL;
318 }
319
320 static struct isl_basic_set *recession_cone(struct isl_basic_set *bset)
321 {
322         int i;
323
324         bset = isl_basic_set_cow(bset);
325         if (!bset)
326                 return NULL;
327
328         for (i = 0; i < bset->n_eq; ++i)
329                 isl_int_set_si(bset->eq[i][0], 0);
330
331         for (i = 0; i < bset->n_ineq; ++i)
332                 isl_int_set_si(bset->ineq[i][0], 0);
333
334         ISL_F_CLR(bset, ISL_BASIC_SET_NO_IMPLICIT);
335         return isl_basic_set_implicit_equalities(bset);
336 }
337
338 static struct isl_basic_set *shift(struct isl_basic_set *bset, isl_int *point)
339 {
340         int i;
341         unsigned dim;
342
343         bset = isl_basic_set_cow(bset);
344         if (!bset)
345                 return NULL;
346
347         dim = isl_basic_set_n_dim(bset);
348         for (i = 0; i < bset->n_eq; ++i) {
349                 isl_seq_inner_product(bset->eq[i]+1, point+1, dim,
350                                         &bset->eq[i][0]);
351                 isl_int_neg(bset->eq[i][0], bset->eq[i][0]);
352         }
353
354         for (i = 0; i < bset->n_ineq; ++i) {
355                 isl_seq_inner_product(bset->ineq[i]+1, point+1, dim,
356                                         &bset->ineq[i][0]);
357                 isl_int_neg(bset->ineq[i][0], bset->ineq[i][0]);
358         }
359
360         return bset;
361 }
362
363 /* Look for all equalities satisfied by the integer points in bset,
364  * which is assume not to have any explicit equalities.
365  *
366  * The equalities are obtained by successively looking for
367  * a point that is affinely independent of the points found so far.
368  * In particular, for each equality satisfied by the points so far,
369  * we check if there is any point on a hyperplane parallel to the
370  * corresponding hyperplane shifted by at least one (in either direction).
371  *
372  * Before looking for any outside points, we first remove the equalities
373  * that correspond to the affine hull of the recession cone.
374  * These equalities will never be equalities over the whols basic set.
375  */
376 static struct isl_basic_set *uset_affine_hull(struct isl_basic_set *bset)
377 {
378         int i, j;
379         struct isl_basic_set *hull = NULL;
380         struct isl_vec *sample;
381         struct isl_ctx *ctx;
382         unsigned dim;
383
384         if (isl_basic_set_is_empty(bset))
385                 return bset;
386
387         ctx = bset->ctx;
388         sample = isl_basic_set_sample(isl_basic_set_copy(bset));
389         if (!sample)
390                 goto error;
391         if (sample->size == 0) {
392                 isl_vec_free(ctx, sample);
393                 hull = isl_basic_set_empty_like(bset);
394                 isl_basic_set_free(bset);
395                 return hull;
396         } else
397                 hull = isl_basic_set_from_vec(ctx, sample);
398
399         if (hull->n_eq > 0) {
400                 struct isl_basic_set *cone;
401                 cone = recession_cone(isl_basic_set_copy(bset));
402                 isl_basic_set_free_inequality(cone, cone->n_ineq);
403                 cone = isl_basic_set_normalize_constraints(cone);
404                 cone = shift(cone, bset->sample->block.data);
405                 hull = affine_hull(hull, cone);
406         }
407
408         dim = isl_basic_set_n_dim(bset);
409         for (i = 0; i < dim; ++i) {
410                 struct isl_basic_set *point;
411                 for (j = 0; j < hull->n_eq; ++j) {
412                         point = outside_point(ctx, bset, hull->eq[j], 1);
413                         if (!point)
414                                 goto error;
415                         if (!ISL_F_ISSET(point, ISL_BASIC_SET_EMPTY))
416                                 break;
417                         isl_basic_set_free(point);
418                         point = outside_point(ctx, bset, hull->eq[j], 0);
419                         if (!point)
420                                 goto error;
421                         if (!ISL_F_ISSET(point, ISL_BASIC_SET_EMPTY))
422                                 break;
423                         isl_basic_set_free(point);
424                 }
425                 if (j == hull->n_eq)
426                         break;
427                 hull = affine_hull(hull, point);
428         }
429         isl_basic_set_free(bset);
430
431         return hull;
432 error:
433         isl_basic_set_free(bset);
434         isl_basic_set_free(hull);
435         return NULL;
436 }
437
438 /* Look for all equalities satisfied by the integer points in bmap
439  * that are independent of the equalities already explicitly available
440  * in bmap.
441  *
442  * We first remove all equalities already explicitly available,
443  * then look for additional equalities in the reduced space
444  * and then transform the result to the original space.
445  * The original equalities are _not_ added to this set.  This is
446  * the responsibility of the calling function.
447  * The resulting basic set has all meaning about the dimensions removed.
448  * In particular, dimensions that correspond to existential variables
449  * in bmap and that are found to be fixed are not removed.
450  */
451 static struct isl_basic_set *equalities_in_underlying_set(
452                                                 struct isl_basic_map *bmap)
453 {
454         struct isl_mat *T2 = NULL;
455         struct isl_basic_set *bset = NULL;
456         struct isl_basic_set *hull = NULL;
457         struct isl_ctx *ctx;
458
459         ctx = bmap->ctx;
460         bset = isl_basic_map_underlying_set(bmap);
461         bset = isl_basic_set_remove_equalities(bset, NULL, &T2);
462         if (!bset)
463                 goto error;
464
465         hull = uset_affine_hull(bset);
466         if (T2)
467                 hull = isl_basic_set_preimage(hull, T2);
468
469         return hull;
470 error:
471         isl_mat_free(ctx, T2);
472         isl_basic_set_free(bset);
473         isl_basic_set_free(hull);
474         return NULL;
475 }
476
477 /* Detect and make explicit all equalities satisfied by the (integer)
478  * points in bmap.
479  */
480 struct isl_basic_map *isl_basic_map_detect_equalities(
481                                                 struct isl_basic_map *bmap)
482 {
483         int i, j;
484         struct isl_basic_set *hull = NULL;
485
486         if (!bmap)
487                 return NULL;
488         if (bmap->n_ineq == 0)
489                 return bmap;
490         if (ISL_F_ISSET(bmap, ISL_BASIC_MAP_EMPTY))
491                 return bmap;
492         if (ISL_F_ISSET(bmap, ISL_BASIC_MAP_ALL_EQUALITIES))
493                 return bmap;
494         if (ISL_F_ISSET(bmap, ISL_BASIC_MAP_RATIONAL))
495                 return isl_basic_map_implicit_equalities(bmap);
496
497         hull = equalities_in_underlying_set(isl_basic_map_copy(bmap));
498         if (!hull)
499                 goto error;
500         bmap = isl_basic_map_extend_dim(bmap, isl_dim_copy(bmap->dim), 0,
501                                         hull->n_eq, 0);
502         for (i = 0; i < hull->n_eq; ++i) {
503                 j = isl_basic_map_alloc_equality(bmap);
504                 if (j < 0)
505                         goto error;
506                 isl_seq_cpy(bmap->eq[j], hull->eq[i],
507                                 1 + isl_basic_set_total_dim(hull));
508         }
509         isl_basic_set_free(hull);
510         ISL_F_SET(bmap, ISL_BASIC_MAP_NO_IMPLICIT | ISL_BASIC_MAP_ALL_EQUALITIES);
511         bmap = isl_basic_map_simplify(bmap);
512         return isl_basic_map_finalize(bmap);
513 error:
514         isl_basic_set_free(hull);
515         isl_basic_map_free(bmap);
516         return NULL;
517 }
518
519 struct isl_map *isl_map_detect_equalities(struct isl_map *map)
520 {
521         struct isl_basic_map *bmap;
522         int i;
523
524         if (!map)
525                 return NULL;
526
527         for (i = 0; i < map->n; ++i) {
528                 bmap = isl_basic_map_copy(map->p[i]);
529                 bmap = isl_basic_map_detect_equalities(bmap);
530                 if (!bmap)
531                         goto error;
532                 isl_basic_map_free(map->p[i]);
533                 map->p[i] = bmap;
534         }
535
536         return map;
537 error:
538         isl_map_free(map);
539         return NULL;
540 }
541
542 /* After computing the rational affine hull (by detecting the implicit
543  * equalities), we compute the additional equalities satisfied by
544  * the integer points (if any) and add the original equalities back in.
545  */
546 struct isl_basic_map *isl_basic_map_affine_hull(struct isl_basic_map *bmap)
547 {
548         struct isl_basic_set *hull = NULL;
549
550         bmap = isl_basic_map_detect_equalities(bmap);
551         bmap = isl_basic_map_cow(bmap);
552         isl_basic_map_free_inequality(bmap, bmap->n_ineq);
553         return bmap;
554 }
555
556 struct isl_basic_set *isl_basic_set_affine_hull(struct isl_basic_set *bset)
557 {
558         return (struct isl_basic_set *)
559                 isl_basic_map_affine_hull((struct isl_basic_map *)bset);
560 }
561
562 struct isl_basic_map *isl_map_affine_hull(struct isl_map *map)
563 {
564         int i;
565         struct isl_basic_map *model = NULL;
566         struct isl_basic_map *hull = NULL;
567         struct isl_set *set;
568
569         if (!map)
570                 return NULL;
571
572         if (map->n == 0) {
573                 hull = isl_basic_map_empty_like_map(map);
574                 isl_map_free(map);
575                 return hull;
576         }
577
578         map = isl_map_detect_equalities(map);
579         map = isl_map_align_divs(map);
580         if (!map)
581                 return NULL;
582         model = isl_basic_map_copy(map->p[0]);
583         set = isl_map_underlying_set(map);
584         set = isl_set_cow(set);
585         if (!set)
586                 goto error;
587
588         for (i = 0; i < set->n; ++i) {
589                 set->p[i] = isl_basic_set_cow(set->p[i]);
590                 set->p[i] = isl_basic_set_affine_hull(set->p[i]);
591                 set->p[i] = isl_basic_set_gauss(set->p[i], NULL);
592                 if (!set->p[i])
593                         goto error;
594         }
595         set = isl_set_remove_empty_parts(set);
596         if (set->n == 0) {
597                 hull = isl_basic_map_empty_like(model);
598                 isl_basic_map_free(model);
599         } else {
600                 struct isl_basic_set *bset;
601                 while (set->n > 1) {
602                         set->p[0] = affine_hull(set->p[0], set->p[--set->n]);
603                         if (!set->p[0])
604                                 goto error;
605                 }
606                 bset = isl_basic_set_copy(set->p[0]);
607                 hull = isl_basic_map_overlying_set(bset, model);
608         }
609         isl_set_free(set);
610         hull = isl_basic_map_simplify(hull);
611         return isl_basic_map_finalize(hull);
612 error:
613         isl_basic_map_free(model);
614         isl_set_free(set);
615         return NULL;
616 }
617
618 struct isl_basic_set *isl_set_affine_hull(struct isl_set *set)
619 {
620         return (struct isl_basic_set *)
621                 isl_map_affine_hull((struct isl_map *)set);
622 }