Line data Source code
1 : /*
2 : * Copyright 2008-2009 Katholieke Universiteit Leuven
3 : * Copyright 2010 INRIA Saclay
4 : * Copyright 2012 Ecole Normale Superieure
5 : *
6 : * Use of this software is governed by the MIT license
7 : *
8 : * Written by Sven Verdoolaege, K.U.Leuven, Departement
9 : * Computerwetenschappen, Celestijnenlaan 200A, B-3001 Leuven, Belgium
10 : * and INRIA Saclay - Ile-de-France, Parc Club Orsay Universite,
11 : * ZAC des vignes, 4 rue Jacques Monod, 91893 Orsay, France
12 : * and Ecole Normale Superieure, 45 rue d'Ulm, 75230 Paris, France
13 : */
14 :
15 : #include <isl_ctx_private.h>
16 : #include <isl_map_private.h>
17 : #include <isl_seq.h>
18 : #include <isl/set.h>
19 : #include <isl/lp.h>
20 : #include <isl/map.h>
21 : #include "isl_equalities.h"
22 : #include "isl_sample.h"
23 : #include "isl_tab.h"
24 : #include <isl_mat_private.h>
25 : #include <isl_vec_private.h>
26 :
27 : #include <bset_to_bmap.c>
28 : #include <bset_from_bmap.c>
29 : #include <set_to_map.c>
30 : #include <set_from_map.c>
31 :
32 91210153 : __isl_give isl_basic_map *isl_basic_map_implicit_equalities(
33 : __isl_take isl_basic_map *bmap)
34 : {
35 : struct isl_tab *tab;
36 :
37 91210153 : if (!bmap)
38 0 : return bmap;
39 :
40 91210153 : bmap = isl_basic_map_gauss(bmap, NULL);
41 91210153 : if (ISL_F_ISSET(bmap, ISL_BASIC_MAP_EMPTY))
42 3851456 : return bmap;
43 87358697 : if (ISL_F_ISSET(bmap, ISL_BASIC_MAP_NO_IMPLICIT))
44 8593789 : return bmap;
45 78764908 : if (bmap->n_ineq <= 1)
46 625297 : return bmap;
47 :
48 78139611 : tab = isl_tab_from_basic_map(bmap, 0);
49 78139611 : if (isl_tab_detect_implicit_equalities(tab) < 0)
50 0 : goto error;
51 78139611 : bmap = isl_basic_map_update_from_tab(bmap, tab);
52 78139611 : isl_tab_free(tab);
53 78139611 : bmap = isl_basic_map_gauss(bmap, NULL);
54 78139611 : ISL_F_SET(bmap, ISL_BASIC_MAP_NO_IMPLICIT);
55 78139611 : return bmap;
56 : error:
57 0 : isl_tab_free(tab);
58 0 : isl_basic_map_free(bmap);
59 0 : return NULL;
60 : }
61 :
62 55443935 : struct isl_basic_set *isl_basic_set_implicit_equalities(
63 : struct isl_basic_set *bset)
64 : {
65 55443935 : return bset_from_bmap(
66 : isl_basic_map_implicit_equalities(bset_to_bmap(bset)));
67 : }
68 :
69 : /* Make eq[row][col] of both bmaps equal so we can add the row
70 : * add the column to the common matrix.
71 : * Note that because of the echelon form, the columns of row row
72 : * after column col are zero.
73 : */
74 1292869704 : static void set_common_multiple(
75 : struct isl_basic_set *bset1, struct isl_basic_set *bset2,
76 : unsigned row, unsigned col)
77 : {
78 : isl_int m, c;
79 :
80 1292869704 : if (isl_int_eq(bset1->eq[row][col], bset2->eq[row][col]))
81 1123604714 : return;
82 :
83 169264990 : isl_int_init(c);
84 169264990 : isl_int_init(m);
85 169264990 : isl_int_lcm(m, bset1->eq[row][col], bset2->eq[row][col]);
86 169264990 : isl_int_divexact(c, m, bset1->eq[row][col]);
87 169264990 : isl_seq_scale(bset1->eq[row], bset1->eq[row], c, col+1);
88 169264990 : isl_int_divexact(c, m, bset2->eq[row][col]);
89 169264990 : isl_seq_scale(bset2->eq[row], bset2->eq[row], c, col+1);
90 169264990 : isl_int_clear(c);
91 169264990 : isl_int_clear(m);
92 : }
93 :
94 : /* Delete a given equality, moving all the following equalities one up.
95 : */
96 1224811082 : static void delete_row(struct isl_basic_set *bset, unsigned row)
97 : {
98 : isl_int *t;
99 : int r;
100 :
101 1224811082 : t = bset->eq[row];
102 1224811082 : bset->n_eq--;
103 1550629791 : for (r = row; r < bset->n_eq; ++r)
104 325818709 : bset->eq[r] = bset->eq[r+1];
105 1224811082 : bset->eq[bset->n_eq] = t;
106 1224811082 : }
107 :
108 : /* Make first row entries in column col of bset1 identical to
109 : * those of bset2, using the fact that entry bset1->eq[row][col]=a
110 : * is non-zero. Initially, these elements of bset1 are all zero.
111 : * For each row i < row, we set
112 : * A[i] = a * A[i] + B[i][col] * A[row]
113 : * B[i] = a * B[i]
114 : * so that
115 : * A[i][col] = B[i][col] = a * old(B[i][col])
116 : */
117 157484488 : static void construct_column(
118 : struct isl_basic_set *bset1, struct isl_basic_set *bset2,
119 : unsigned row, unsigned col)
120 : {
121 : int r;
122 : isl_int a;
123 : isl_int b;
124 : unsigned total;
125 :
126 157484488 : isl_int_init(a);
127 157484488 : isl_int_init(b);
128 157484488 : total = 1 + isl_basic_set_n_dim(bset1);
129 443615388 : for (r = 0; r < row; ++r) {
130 286130900 : if (isl_int_is_zero(bset2->eq[r][col]))
131 127606218 : continue;
132 158524682 : isl_int_gcd(b, bset2->eq[r][col], bset1->eq[row][col]);
133 158524682 : isl_int_divexact(a, bset1->eq[row][col], b);
134 158524682 : isl_int_divexact(b, bset2->eq[r][col], b);
135 158524682 : isl_seq_combine(bset1->eq[r], a, bset1->eq[r],
136 158524682 : b, bset1->eq[row], total);
137 158524682 : isl_seq_scale(bset2->eq[r], bset2->eq[r], a, total);
138 : }
139 157484488 : isl_int_clear(a);
140 157484488 : isl_int_clear(b);
141 157484488 : delete_row(bset1, row);
142 157484488 : }
143 :
144 : /* Make first row entries in column col of bset1 identical to
145 : * those of bset2, using only these entries of the two matrices.
146 : * Let t be the last row with different entries.
147 : * For each row i < t, we set
148 : * A[i] = (A[t][col]-B[t][col]) * A[i] + (B[i][col]-A[i][col) * A[t]
149 : * B[i] = (A[t][col]-B[t][col]) * B[i] + (B[i][col]-A[i][col) * B[t]
150 : * so that
151 : * A[i][col] = B[i][col] = old(A[t][col]*B[i][col]-A[i][col]*B[t][col])
152 : */
153 543356513 : static int transform_column(
154 : struct isl_basic_set *bset1, struct isl_basic_set *bset2,
155 : unsigned row, unsigned col)
156 : {
157 : int i, t;
158 : isl_int a, b, g;
159 : unsigned total;
160 :
161 670148263 : for (t = row-1; t >= 0; --t)
162 660455047 : if (isl_int_ne(bset1->eq[t][col], bset2->eq[t][col]))
163 533663297 : break;
164 543356513 : if (t < 0)
165 9693216 : return 0;
166 :
167 533663297 : total = 1 + isl_basic_set_n_dim(bset1);
168 533663297 : isl_int_init(a);
169 533663297 : isl_int_init(b);
170 533663297 : isl_int_init(g);
171 533663297 : isl_int_sub(b, bset1->eq[t][col], bset2->eq[t][col]);
172 1166365422 : for (i = 0; i < t; ++i) {
173 632702125 : isl_int_sub(a, bset2->eq[i][col], bset1->eq[i][col]);
174 632702125 : isl_int_gcd(g, a, b);
175 632702125 : isl_int_divexact(a, a, g);
176 632702125 : isl_int_divexact(g, b, g);
177 632702125 : isl_seq_combine(bset1->eq[i], g, bset1->eq[i], a, bset1->eq[t],
178 : total);
179 632702125 : isl_seq_combine(bset2->eq[i], g, bset2->eq[i], a, bset2->eq[t],
180 : total);
181 : }
182 533663297 : isl_int_clear(a);
183 533663297 : isl_int_clear(b);
184 533663297 : isl_int_clear(g);
185 533663297 : delete_row(bset1, t);
186 533663297 : delete_row(bset2, t);
187 533663297 : return 1;
188 : }
189 :
190 : /* The implementation is based on Section 5.2 of Michael Karr,
191 : * "Affine Relationships Among Variables of a Program",
192 : * except that the echelon form we use starts from the last column
193 : * and that we are dealing with integer coefficients.
194 : */
195 543141296 : static struct isl_basic_set *affine_hull(
196 : struct isl_basic_set *bset1, struct isl_basic_set *bset2)
197 : {
198 : unsigned total;
199 : int col;
200 : int row;
201 :
202 543141296 : if (!bset1 || !bset2)
203 : goto error;
204 :
205 543141296 : total = 1 + isl_basic_set_n_dim(bset1);
206 :
207 543141296 : row = 0;
208 2536852001 : for (col = total-1; col >= 0; --col) {
209 4029344425 : int is_zero1 = row >= bset1->n_eq ||
210 2559302544 : isl_int_is_zero(bset1->eq[row][col]);
211 3988571708 : int is_zero2 = row >= bset2->n_eq ||
212 2898769600 : isl_int_is_zero(bset2->eq[row][col]);
213 1993710705 : if (!is_zero1 && !is_zero2) {
214 1292869704 : set_common_multiple(bset1, bset2, row, col);
215 1292869704 : ++row;
216 700841001 : } else if (!is_zero1 && is_zero2) {
217 2119690 : construct_column(bset1, bset2, row, col);
218 698721311 : } else if (is_zero1 && !is_zero2) {
219 155364798 : construct_column(bset2, bset1, row, col);
220 : } else {
221 543356513 : if (transform_column(bset1, bset2, row, col))
222 533663297 : --row;
223 : }
224 : }
225 543141296 : isl_assert(bset1->ctx, row == bset1->n_eq, goto error);
226 543141296 : isl_basic_set_free(bset2);
227 543141296 : bset1 = isl_basic_set_normalize_constraints(bset1);
228 543141296 : return bset1;
229 : error:
230 0 : isl_basic_set_free(bset1);
231 0 : isl_basic_set_free(bset2);
232 0 : return NULL;
233 : }
234 :
235 : /* Find an integer point in the set represented by "tab"
236 : * that lies outside of the equality "eq" e(x) = 0.
237 : * If "up" is true, look for a point satisfying e(x) - 1 >= 0.
238 : * Otherwise, look for a point satisfying -e(x) - 1 >= 0 (i.e., e(x) <= -1).
239 : * The point, if found, is returned.
240 : * If no point can be found, a zero-length vector is returned.
241 : *
242 : * Before solving an ILP problem, we first check if simply
243 : * adding the normal of the constraint to one of the known
244 : * integer points in the basic set represented by "tab"
245 : * yields another point inside the basic set.
246 : *
247 : * The caller of this function ensures that the tableau is bounded or
248 : * that tab->basis and tab->n_unbounded have been set appropriately.
249 : */
250 198386 : static struct isl_vec *outside_point(struct isl_tab *tab, isl_int *eq, int up)
251 : {
252 : struct isl_ctx *ctx;
253 198386 : struct isl_vec *sample = NULL;
254 : struct isl_tab_undo *snap;
255 : unsigned dim;
256 :
257 198386 : if (!tab)
258 0 : return NULL;
259 198386 : ctx = tab->mat->ctx;
260 :
261 198386 : dim = tab->n_var;
262 198386 : sample = isl_vec_alloc(ctx, 1 + dim);
263 198386 : if (!sample)
264 0 : return NULL;
265 198386 : isl_int_set_si(sample->el[0], 1);
266 595158 : isl_seq_combine(sample->el + 1,
267 396772 : ctx->one, tab->bmap->sample->el + 1,
268 : up ? ctx->one : ctx->negone, eq + 1, dim);
269 198386 : if (isl_basic_map_contains(tab->bmap, sample))
270 102 : return sample;
271 198284 : isl_vec_free(sample);
272 198284 : sample = NULL;
273 :
274 198284 : snap = isl_tab_snap(tab);
275 :
276 198284 : if (!up)
277 56582 : isl_seq_neg(eq, eq, 1 + dim);
278 198284 : isl_int_sub_ui(eq[0], eq[0], 1);
279 :
280 198284 : if (isl_tab_extend_cons(tab, 1) < 0)
281 0 : goto error;
282 198284 : if (isl_tab_add_ineq(tab, eq) < 0)
283 0 : goto error;
284 :
285 198284 : sample = isl_tab_sample(tab);
286 :
287 198284 : isl_int_add_ui(eq[0], eq[0], 1);
288 198284 : if (!up)
289 56582 : isl_seq_neg(eq, eq, 1 + dim);
290 :
291 198284 : if (sample && isl_tab_rollback(tab, snap) < 0)
292 0 : goto error;
293 :
294 198284 : return sample;
295 : error:
296 0 : isl_vec_free(sample);
297 0 : return NULL;
298 : }
299 :
300 55443935 : __isl_give isl_basic_set *isl_basic_set_recession_cone(
301 : __isl_take isl_basic_set *bset)
302 : {
303 : int i;
304 :
305 55443935 : bset = isl_basic_set_cow(bset);
306 55443935 : if (!bset)
307 0 : return NULL;
308 55443935 : isl_assert(bset->ctx, bset->n_div == 0, goto error);
309 :
310 55444219 : for (i = 0; i < bset->n_eq; ++i)
311 284 : isl_int_set_si(bset->eq[i][0], 0);
312 :
313 776746086 : for (i = 0; i < bset->n_ineq; ++i)
314 721302151 : isl_int_set_si(bset->ineq[i][0], 0);
315 :
316 55443935 : ISL_F_CLR(bset, ISL_BASIC_SET_NO_IMPLICIT);
317 55443935 : return isl_basic_set_implicit_equalities(bset);
318 : error:
319 0 : isl_basic_set_free(bset);
320 0 : return NULL;
321 : }
322 :
323 : /* Move "sample" to a point that is one up (or down) from the original
324 : * point in dimension "pos".
325 : */
326 3481114 : static void adjacent_point(__isl_keep isl_vec *sample, int pos, int up)
327 : {
328 3481114 : if (up)
329 1740557 : isl_int_add_ui(sample->el[1 + pos], sample->el[1 + pos], 1);
330 : else
331 1740557 : isl_int_sub_ui(sample->el[1 + pos], sample->el[1 + pos], 1);
332 3481114 : }
333 :
334 : /* Check if any points that are adjacent to "sample" also belong to "bset".
335 : * If so, add them to "hull" and return the updated hull.
336 : *
337 : * Before checking whether and adjacent point belongs to "bset", we first
338 : * check whether it already belongs to "hull" as this test is typically
339 : * much cheaper.
340 : */
341 230957 : static __isl_give isl_basic_set *add_adjacent_points(
342 : __isl_take isl_basic_set *hull, __isl_take isl_vec *sample,
343 : __isl_keep isl_basic_set *bset)
344 : {
345 : int i, up;
346 : int dim;
347 :
348 230957 : if (!sample)
349 0 : goto error;
350 :
351 230957 : dim = isl_basic_set_dim(hull, isl_dim_set);
352 :
353 1243028 : for (i = 0; i < dim; ++i) {
354 2331441 : for (up = 0; up <= 1; ++up) {
355 : int contains;
356 : isl_basic_set *point;
357 :
358 1740557 : adjacent_point(sample, i, up);
359 1740557 : contains = isl_basic_set_contains(hull, sample);
360 1740557 : if (contains < 0)
361 0 : goto error;
362 1740557 : if (contains) {
363 361163 : adjacent_point(sample, i, !up);
364 361163 : continue;
365 : }
366 1379394 : contains = isl_basic_set_contains(bset, sample);
367 1379394 : if (contains < 0)
368 0 : goto error;
369 1379394 : if (contains) {
370 421187 : point = isl_basic_set_from_vec(
371 : isl_vec_copy(sample));
372 421187 : hull = affine_hull(hull, point);
373 : }
374 1379394 : adjacent_point(sample, i, !up);
375 1379394 : if (contains)
376 421187 : break;
377 : }
378 : }
379 :
380 230957 : isl_vec_free(sample);
381 :
382 230957 : return hull;
383 : error:
384 0 : isl_vec_free(sample);
385 0 : isl_basic_set_free(hull);
386 0 : return NULL;
387 : }
388 :
389 : /* Extend an initial (under-)approximation of the affine hull of basic
390 : * set represented by the tableau "tab"
391 : * by looking for points that do not satisfy one of the equalities
392 : * in the current approximation and adding them to that approximation
393 : * until no such points can be found any more.
394 : *
395 : * The caller of this function ensures that "tab" is bounded or
396 : * that tab->basis and tab->n_unbounded have been set appropriately.
397 : *
398 : * "bset" may be either NULL or the basic set represented by "tab".
399 : * If "bset" is not NULL, we check for any point we find if any
400 : * of its adjacent points also belong to "bset".
401 : */
402 117239 : static __isl_give isl_basic_set *extend_affine_hull(struct isl_tab *tab,
403 : __isl_take isl_basic_set *hull, __isl_keep isl_basic_set *bset)
404 : {
405 : int i, j;
406 : unsigned dim;
407 :
408 117239 : if (!tab || !hull)
409 : goto error;
410 :
411 117239 : dim = tab->n_var;
412 :
413 117239 : if (isl_tab_extend_cons(tab, 2 * dim + 1) < 0)
414 0 : goto error;
415 :
416 230957 : for (i = 0; i < dim; ++i) {
417 : struct isl_vec *sample;
418 : struct isl_basic_set *point;
419 258461 : for (j = 0; j < hull->n_eq; ++j) {
420 141787 : sample = outside_point(tab, hull->eq[j], 1);
421 141787 : if (!sample)
422 0 : goto error;
423 141787 : if (sample->size > 0)
424 85188 : break;
425 56599 : isl_vec_free(sample);
426 56599 : sample = outside_point(tab, hull->eq[j], 0);
427 56599 : if (!sample)
428 0 : goto error;
429 56599 : if (sample->size > 0)
430 28530 : break;
431 28069 : isl_vec_free(sample);
432 :
433 28069 : if (isl_tab_add_eq(tab, hull->eq[j]) < 0)
434 0 : goto error;
435 : }
436 230392 : if (j == hull->n_eq)
437 116674 : break;
438 113718 : if (tab->samples &&
439 0 : isl_tab_add_sample(tab, isl_vec_copy(sample)) < 0)
440 0 : hull = isl_basic_set_free(hull);
441 113718 : if (bset)
442 113718 : hull = add_adjacent_points(hull, isl_vec_copy(sample),
443 : bset);
444 113718 : point = isl_basic_set_from_vec(sample);
445 113718 : hull = affine_hull(hull, point);
446 113718 : if (!hull)
447 0 : return NULL;
448 : }
449 :
450 117239 : return hull;
451 : error:
452 0 : isl_basic_set_free(hull);
453 0 : return NULL;
454 : }
455 :
456 : /* Construct an initial underapproximation of the hull of "bset"
457 : * from "sample" and any of its adjacent points that also belong to "bset".
458 : */
459 117239 : static __isl_give isl_basic_set *initialize_hull(__isl_keep isl_basic_set *bset,
460 : __isl_take isl_vec *sample)
461 : {
462 : isl_basic_set *hull;
463 :
464 117239 : hull = isl_basic_set_from_vec(isl_vec_copy(sample));
465 117239 : hull = add_adjacent_points(hull, sample, bset);
466 :
467 117239 : return hull;
468 : }
469 :
470 : /* Look for all equalities satisfied by the integer points in bset,
471 : * which is assumed to be bounded.
472 : *
473 : * The equalities are obtained by successively looking for
474 : * a point that is affinely independent of the points found so far.
475 : * In particular, for each equality satisfied by the points so far,
476 : * we check if there is any point on a hyperplane parallel to the
477 : * corresponding hyperplane shifted by at least one (in either direction).
478 : */
479 117239 : static struct isl_basic_set *uset_affine_hull_bounded(struct isl_basic_set *bset)
480 : {
481 117239 : struct isl_vec *sample = NULL;
482 : struct isl_basic_set *hull;
483 117239 : struct isl_tab *tab = NULL;
484 : unsigned dim;
485 :
486 117239 : if (isl_basic_set_plain_is_empty(bset))
487 0 : return bset;
488 :
489 117239 : dim = isl_basic_set_n_dim(bset);
490 :
491 117239 : if (bset->sample && bset->sample->size == 1 + dim) {
492 66475 : int contains = isl_basic_set_contains(bset, bset->sample);
493 66475 : if (contains < 0)
494 0 : goto error;
495 66475 : if (contains) {
496 66475 : if (dim == 0)
497 0 : return bset;
498 66475 : sample = isl_vec_copy(bset->sample);
499 : } else {
500 0 : isl_vec_free(bset->sample);
501 0 : bset->sample = NULL;
502 : }
503 : }
504 :
505 117239 : tab = isl_tab_from_basic_set(bset, 1);
506 117239 : if (!tab)
507 0 : goto error;
508 117239 : if (tab->empty) {
509 0 : isl_tab_free(tab);
510 0 : isl_vec_free(sample);
511 0 : return isl_basic_set_set_to_empty(bset);
512 : }
513 :
514 117239 : if (!sample) {
515 : struct isl_tab_undo *snap;
516 50764 : snap = isl_tab_snap(tab);
517 50764 : sample = isl_tab_sample(tab);
518 50764 : if (isl_tab_rollback(tab, snap) < 0)
519 0 : goto error;
520 50764 : isl_vec_free(tab->bmap->sample);
521 50764 : tab->bmap->sample = isl_vec_copy(sample);
522 : }
523 :
524 117239 : if (!sample)
525 0 : goto error;
526 117239 : if (sample->size == 0) {
527 0 : isl_tab_free(tab);
528 0 : isl_vec_free(sample);
529 0 : return isl_basic_set_set_to_empty(bset);
530 : }
531 :
532 117239 : hull = initialize_hull(bset, sample);
533 :
534 117239 : hull = extend_affine_hull(tab, hull, bset);
535 117239 : isl_basic_set_free(bset);
536 117239 : isl_tab_free(tab);
537 :
538 117239 : return hull;
539 : error:
540 0 : isl_vec_free(sample);
541 0 : isl_tab_free(tab);
542 0 : isl_basic_set_free(bset);
543 0 : return NULL;
544 : }
545 :
546 : /* Given an unbounded tableau and an integer point satisfying the tableau,
547 : * construct an initial affine hull containing the recession cone
548 : * shifted to the given point.
549 : *
550 : * The unbounded directions are taken from the last rows of the basis,
551 : * which is assumed to have been initialized appropriately.
552 : */
553 0 : static __isl_give isl_basic_set *initial_hull(struct isl_tab *tab,
554 : __isl_take isl_vec *vec)
555 : {
556 : int i;
557 : int k;
558 0 : struct isl_basic_set *bset = NULL;
559 : struct isl_ctx *ctx;
560 : unsigned dim;
561 :
562 0 : if (!vec || !tab)
563 0 : return NULL;
564 0 : ctx = vec->ctx;
565 0 : isl_assert(ctx, vec->size != 0, goto error);
566 :
567 0 : bset = isl_basic_set_alloc(ctx, 0, vec->size - 1, 0, vec->size - 1, 0);
568 0 : if (!bset)
569 0 : goto error;
570 0 : dim = isl_basic_set_n_dim(bset) - tab->n_unbounded;
571 0 : for (i = 0; i < dim; ++i) {
572 0 : k = isl_basic_set_alloc_equality(bset);
573 0 : if (k < 0)
574 0 : goto error;
575 0 : isl_seq_cpy(bset->eq[k] + 1, tab->basis->row[1 + i] + 1,
576 0 : vec->size - 1);
577 0 : isl_seq_inner_product(bset->eq[k] + 1, vec->el +1,
578 0 : vec->size - 1, &bset->eq[k][0]);
579 0 : isl_int_neg(bset->eq[k][0], bset->eq[k][0]);
580 : }
581 0 : bset->sample = vec;
582 0 : bset = isl_basic_set_gauss(bset, NULL);
583 :
584 0 : return bset;
585 : error:
586 0 : isl_basic_set_free(bset);
587 0 : isl_vec_free(vec);
588 0 : return NULL;
589 : }
590 :
591 : /* Given a tableau of a set and a tableau of the corresponding
592 : * recession cone, detect and add all equalities to the tableau.
593 : * If the tableau is bounded, then we can simply keep the
594 : * tableau in its state after the return from extend_affine_hull.
595 : * However, if the tableau is unbounded, then
596 : * isl_tab_set_initial_basis_with_cone will add some additional
597 : * constraints to the tableau that have to be removed again.
598 : * In this case, we therefore rollback to the state before
599 : * any constraints were added and then add the equalities back in.
600 : */
601 0 : struct isl_tab *isl_tab_detect_equalities(struct isl_tab *tab,
602 : struct isl_tab *tab_cone)
603 : {
604 : int j;
605 : struct isl_vec *sample;
606 0 : struct isl_basic_set *hull = NULL;
607 : struct isl_tab_undo *snap;
608 :
609 0 : if (!tab || !tab_cone)
610 : goto error;
611 :
612 0 : snap = isl_tab_snap(tab);
613 :
614 0 : isl_mat_free(tab->basis);
615 0 : tab->basis = NULL;
616 :
617 0 : isl_assert(tab->mat->ctx, tab->bmap, goto error);
618 0 : isl_assert(tab->mat->ctx, tab->samples, goto error);
619 0 : isl_assert(tab->mat->ctx, tab->samples->n_col == 1 + tab->n_var, goto error);
620 0 : isl_assert(tab->mat->ctx, tab->n_sample > tab->n_outside, goto error);
621 :
622 0 : if (isl_tab_set_initial_basis_with_cone(tab, tab_cone) < 0)
623 0 : goto error;
624 :
625 0 : sample = isl_vec_alloc(tab->mat->ctx, 1 + tab->n_var);
626 0 : if (!sample)
627 0 : goto error;
628 :
629 0 : isl_seq_cpy(sample->el, tab->samples->row[tab->n_outside], sample->size);
630 :
631 0 : isl_vec_free(tab->bmap->sample);
632 0 : tab->bmap->sample = isl_vec_copy(sample);
633 :
634 0 : if (tab->n_unbounded == 0)
635 0 : hull = isl_basic_set_from_vec(isl_vec_copy(sample));
636 : else
637 0 : hull = initial_hull(tab, isl_vec_copy(sample));
638 :
639 0 : for (j = tab->n_outside + 1; j < tab->n_sample; ++j) {
640 0 : isl_seq_cpy(sample->el, tab->samples->row[j], sample->size);
641 0 : hull = affine_hull(hull,
642 0 : isl_basic_set_from_vec(isl_vec_copy(sample)));
643 : }
644 :
645 0 : isl_vec_free(sample);
646 :
647 0 : hull = extend_affine_hull(tab, hull, NULL);
648 0 : if (!hull)
649 0 : goto error;
650 :
651 0 : if (tab->n_unbounded == 0) {
652 0 : isl_basic_set_free(hull);
653 0 : return tab;
654 : }
655 :
656 0 : if (isl_tab_rollback(tab, snap) < 0)
657 0 : goto error;
658 :
659 0 : if (hull->n_eq > tab->n_zero) {
660 0 : for (j = 0; j < hull->n_eq; ++j) {
661 0 : isl_seq_normalize(tab->mat->ctx, hull->eq[j], 1 + tab->n_var);
662 0 : if (isl_tab_add_eq(tab, hull->eq[j]) < 0)
663 0 : goto error;
664 : }
665 : }
666 :
667 0 : isl_basic_set_free(hull);
668 :
669 0 : return tab;
670 : error:
671 0 : isl_basic_set_free(hull);
672 0 : isl_tab_free(tab);
673 0 : return NULL;
674 : }
675 :
676 : /* Compute the affine hull of "bset", where "cone" is the recession cone
677 : * of "bset".
678 : *
679 : * We first compute a unimodular transformation that puts the unbounded
680 : * directions in the last dimensions. In particular, we take a transformation
681 : * that maps all equalities to equalities (in HNF) on the first dimensions.
682 : * Let x be the original dimensions and y the transformed, with y_1 bounded
683 : * and y_2 unbounded.
684 : *
685 : * [ y_1 ] [ y_1 ] [ Q_1 ]
686 : * x = U [ y_2 ] [ y_2 ] = [ Q_2 ] x
687 : *
688 : * Let's call the input basic set S. We compute S' = preimage(S, U)
689 : * and drop the final dimensions including any constraints involving them.
690 : * This results in set S''.
691 : * Then we compute the affine hull A'' of S''.
692 : * Let F y_1 >= g be the constraint system of A''. In the transformed
693 : * space the y_2 are unbounded, so we can add them back without any constraints,
694 : * resulting in
695 : *
696 : * [ y_1 ]
697 : * [ F 0 ] [ y_2 ] >= g
698 : * or
699 : * [ Q_1 ]
700 : * [ F 0 ] [ Q_2 ] x >= g
701 : * or
702 : * F Q_1 x >= g
703 : *
704 : * The affine hull in the original space is then obtained as
705 : * A = preimage(A'', Q_1).
706 : */
707 28597 : static struct isl_basic_set *affine_hull_with_cone(struct isl_basic_set *bset,
708 : struct isl_basic_set *cone)
709 : {
710 : unsigned total;
711 : unsigned cone_dim;
712 : struct isl_basic_set *hull;
713 : struct isl_mat *M, *U, *Q;
714 :
715 28597 : if (!bset || !cone)
716 : goto error;
717 :
718 28597 : total = isl_basic_set_total_dim(cone);
719 28597 : cone_dim = total - cone->n_eq;
720 :
721 28597 : M = isl_mat_sub_alloc6(bset->ctx, cone->eq, 0, cone->n_eq, 1, total);
722 28597 : M = isl_mat_left_hermite(M, 0, &U, &Q);
723 28597 : if (!M)
724 0 : goto error;
725 28597 : isl_mat_free(M);
726 :
727 28597 : U = isl_mat_lin_to_aff(U);
728 28597 : bset = isl_basic_set_preimage(bset, isl_mat_copy(U));
729 :
730 28597 : bset = isl_basic_set_drop_constraints_involving(bset, total - cone_dim,
731 : cone_dim);
732 28597 : bset = isl_basic_set_drop_dims(bset, total - cone_dim, cone_dim);
733 :
734 28597 : Q = isl_mat_lin_to_aff(Q);
735 28597 : Q = isl_mat_drop_rows(Q, 1 + total - cone_dim, cone_dim);
736 :
737 28597 : if (bset && bset->sample && bset->sample->size == 1 + total)
738 13525 : bset->sample = isl_mat_vec_product(isl_mat_copy(Q), bset->sample);
739 :
740 28597 : hull = uset_affine_hull_bounded(bset);
741 :
742 28597 : if (!hull) {
743 0 : isl_mat_free(Q);
744 0 : isl_mat_free(U);
745 : } else {
746 28597 : struct isl_vec *sample = isl_vec_copy(hull->sample);
747 28597 : U = isl_mat_drop_cols(U, 1 + total - cone_dim, cone_dim);
748 28597 : if (sample && sample->size > 0)
749 28597 : sample = isl_mat_vec_product(U, sample);
750 : else
751 0 : isl_mat_free(U);
752 28597 : hull = isl_basic_set_preimage(hull, Q);
753 28597 : if (hull) {
754 28597 : isl_vec_free(hull->sample);
755 28597 : hull->sample = sample;
756 : } else
757 0 : isl_vec_free(sample);
758 : }
759 :
760 28597 : isl_basic_set_free(cone);
761 :
762 28597 : return hull;
763 : error:
764 0 : isl_basic_set_free(bset);
765 0 : isl_basic_set_free(cone);
766 0 : return NULL;
767 : }
768 :
769 : /* Look for all equalities satisfied by the integer points in bset,
770 : * which is assumed not to have any explicit equalities.
771 : *
772 : * The equalities are obtained by successively looking for
773 : * a point that is affinely independent of the points found so far.
774 : * In particular, for each equality satisfied by the points so far,
775 : * we check if there is any point on a hyperplane parallel to the
776 : * corresponding hyperplane shifted by at least one (in either direction).
777 : *
778 : * Before looking for any outside points, we first compute the recession
779 : * cone. The directions of this recession cone will always be part
780 : * of the affine hull, so there is no need for looking for any points
781 : * in these directions.
782 : * In particular, if the recession cone is full-dimensional, then
783 : * the affine hull is simply the whole universe.
784 : */
785 223630 : static struct isl_basic_set *uset_affine_hull(struct isl_basic_set *bset)
786 : {
787 : struct isl_basic_set *cone;
788 :
789 223630 : if (isl_basic_set_plain_is_empty(bset))
790 0 : return bset;
791 :
792 223630 : cone = isl_basic_set_recession_cone(isl_basic_set_copy(bset));
793 223630 : if (!cone)
794 0 : goto error;
795 223630 : if (cone->n_eq == 0) {
796 : isl_space *space;
797 106391 : space = isl_basic_set_get_space(bset);
798 106391 : isl_basic_set_free(cone);
799 106391 : isl_basic_set_free(bset);
800 106391 : return isl_basic_set_universe(space);
801 : }
802 :
803 117239 : if (cone->n_eq < isl_basic_set_total_dim(cone))
804 28597 : return affine_hull_with_cone(bset, cone);
805 :
806 88642 : isl_basic_set_free(cone);
807 88642 : return uset_affine_hull_bounded(bset);
808 : error:
809 0 : isl_basic_set_free(bset);
810 0 : return NULL;
811 : }
812 :
813 : /* Look for all equalities satisfied by the integer points in bmap
814 : * that are independent of the equalities already explicitly available
815 : * in bmap.
816 : *
817 : * We first remove all equalities already explicitly available,
818 : * then look for additional equalities in the reduced space
819 : * and then transform the result to the original space.
820 : * The original equalities are _not_ added to this set. This is
821 : * the responsibility of the calling function.
822 : * The resulting basic set has all meaning about the dimensions removed.
823 : * In particular, dimensions that correspond to existential variables
824 : * in bmap and that are found to be fixed are not removed.
825 : */
826 223630 : static struct isl_basic_set *equalities_in_underlying_set(
827 : struct isl_basic_map *bmap)
828 : {
829 223630 : struct isl_mat *T1 = NULL;
830 223630 : struct isl_mat *T2 = NULL;
831 223630 : struct isl_basic_set *bset = NULL;
832 223630 : struct isl_basic_set *hull = NULL;
833 :
834 223630 : bset = isl_basic_map_underlying_set(bmap);
835 223630 : if (!bset)
836 0 : return NULL;
837 223630 : if (bset->n_eq)
838 118664 : bset = isl_basic_set_remove_equalities(bset, &T1, &T2);
839 223630 : if (!bset)
840 0 : goto error;
841 :
842 223630 : hull = uset_affine_hull(bset);
843 223630 : if (!T2)
844 104966 : return hull;
845 :
846 118664 : if (!hull) {
847 0 : isl_mat_free(T1);
848 0 : isl_mat_free(T2);
849 : } else {
850 118664 : struct isl_vec *sample = isl_vec_copy(hull->sample);
851 118664 : if (sample && sample->size > 0)
852 50732 : sample = isl_mat_vec_product(T1, sample);
853 : else
854 67932 : isl_mat_free(T1);
855 118664 : hull = isl_basic_set_preimage(hull, T2);
856 118664 : if (hull) {
857 118664 : isl_vec_free(hull->sample);
858 118664 : hull->sample = sample;
859 : } else
860 0 : isl_vec_free(sample);
861 : }
862 :
863 118664 : return hull;
864 : error:
865 0 : isl_mat_free(T1);
866 0 : isl_mat_free(T2);
867 0 : isl_basic_set_free(bset);
868 0 : isl_basic_set_free(hull);
869 0 : return NULL;
870 : }
871 :
872 : /* Detect and make explicit all equalities satisfied by the (integer)
873 : * points in bmap.
874 : */
875 5538575720 : __isl_give isl_basic_map *isl_basic_map_detect_equalities(
876 : __isl_take isl_basic_map *bmap)
877 : {
878 : int i, j;
879 5538575720 : struct isl_basic_set *hull = NULL;
880 :
881 5538575720 : if (!bmap)
882 0 : return NULL;
883 5538575720 : if (bmap->n_ineq == 0)
884 5502551355 : return bmap;
885 36024365 : if (ISL_F_ISSET(bmap, ISL_BASIC_MAP_EMPTY))
886 0 : return bmap;
887 36024365 : if (ISL_F_ISSET(bmap, ISL_BASIC_MAP_ALL_EQUALITIES))
888 34517 : return bmap;
889 35989848 : if (ISL_F_ISSET(bmap, ISL_BASIC_MAP_RATIONAL))
890 35766218 : return isl_basic_map_implicit_equalities(bmap);
891 :
892 223630 : hull = equalities_in_underlying_set(isl_basic_map_copy(bmap));
893 223630 : if (!hull)
894 0 : goto error;
895 223630 : if (ISL_F_ISSET(hull, ISL_BASIC_SET_EMPTY)) {
896 0 : isl_basic_set_free(hull);
897 0 : return isl_basic_map_set_to_empty(bmap);
898 : }
899 223630 : bmap = isl_basic_map_extend_space(bmap, isl_space_copy(bmap->dim), 0,
900 : hull->n_eq, 0);
901 248183 : for (i = 0; i < hull->n_eq; ++i) {
902 24553 : j = isl_basic_map_alloc_equality(bmap);
903 24553 : if (j < 0)
904 0 : goto error;
905 24553 : isl_seq_cpy(bmap->eq[j], hull->eq[i],
906 24553 : 1 + isl_basic_set_total_dim(hull));
907 : }
908 223630 : isl_vec_free(bmap->sample);
909 223630 : bmap->sample = isl_vec_copy(hull->sample);
910 223630 : isl_basic_set_free(hull);
911 223630 : ISL_F_SET(bmap, ISL_BASIC_MAP_NO_IMPLICIT | ISL_BASIC_MAP_ALL_EQUALITIES);
912 223630 : bmap = isl_basic_map_simplify(bmap);
913 223630 : return isl_basic_map_finalize(bmap);
914 : error:
915 0 : isl_basic_set_free(hull);
916 0 : isl_basic_map_free(bmap);
917 0 : return NULL;
918 : }
919 :
920 0 : __isl_give isl_basic_set *isl_basic_set_detect_equalities(
921 : __isl_take isl_basic_set *bset)
922 : {
923 0 : return bset_from_bmap(
924 : isl_basic_map_detect_equalities(bset_to_bmap(bset)));
925 : }
926 :
927 645860546 : __isl_give isl_map *isl_map_detect_equalities(__isl_take isl_map *map)
928 : {
929 645860546 : return isl_map_inline_foreach_basic_map(map,
930 : &isl_basic_map_detect_equalities);
931 : }
932 :
933 48675 : __isl_give isl_set *isl_set_detect_equalities(__isl_take isl_set *set)
934 : {
935 48675 : return set_from_map(isl_map_detect_equalities(set_to_map(set)));
936 : }
937 :
938 : /* Return the superset of "bmap" described by the equalities
939 : * satisfied by "bmap" that are already known.
940 : */
941 6258293514 : __isl_give isl_basic_map *isl_basic_map_plain_affine_hull(
942 : __isl_take isl_basic_map *bmap)
943 : {
944 6258293514 : bmap = isl_basic_map_cow(bmap);
945 6258293514 : if (bmap)
946 6258293514 : isl_basic_map_free_inequality(bmap, bmap->n_ineq);
947 6258293514 : bmap = isl_basic_map_finalize(bmap);
948 6258293514 : return bmap;
949 : }
950 :
951 : /* Return the superset of "bset" described by the equalities
952 : * satisfied by "bset" that are already known.
953 : */
954 0 : __isl_give isl_basic_set *isl_basic_set_plain_affine_hull(
955 : __isl_take isl_basic_set *bset)
956 : {
957 0 : return isl_basic_map_plain_affine_hull(bset);
958 : }
959 :
960 : /* After computing the rational affine hull (by detecting the implicit
961 : * equalities), we compute the additional equalities satisfied by
962 : * the integer points (if any) and add the original equalities back in.
963 : */
964 3359919417 : __isl_give isl_basic_map *isl_basic_map_affine_hull(
965 : __isl_take isl_basic_map *bmap)
966 : {
967 3359919417 : bmap = isl_basic_map_detect_equalities(bmap);
968 3359919417 : bmap = isl_basic_map_plain_affine_hull(bmap);
969 3359919417 : return bmap;
970 : }
971 :
972 0 : struct isl_basic_set *isl_basic_set_affine_hull(struct isl_basic_set *bset)
973 : {
974 0 : return bset_from_bmap(isl_basic_map_affine_hull(bset_to_bmap(bset)));
975 : }
976 :
977 : /* Given a rational affine matrix "M", add stride constraints to "bmap"
978 : * that ensure that
979 : *
980 : * M(x)
981 : *
982 : * is an integer vector. The variables x include all the variables
983 : * of "bmap" except the unknown divs.
984 : *
985 : * If d is the common denominator of M, then we need to impose that
986 : *
987 : * d M(x) = 0 mod d
988 : *
989 : * or
990 : *
991 : * exists alpha : d M(x) = d alpha
992 : *
993 : * This function is similar to add_strides in isl_morph.c
994 : */
995 0 : static __isl_give isl_basic_map *add_strides(__isl_take isl_basic_map *bmap,
996 : __isl_keep isl_mat *M, int n_known)
997 : {
998 : int i, div, k;
999 : isl_int gcd;
1000 :
1001 0 : if (isl_int_is_one(M->row[0][0]))
1002 0 : return bmap;
1003 :
1004 0 : bmap = isl_basic_map_extend_space(bmap, isl_space_copy(bmap->dim),
1005 0 : M->n_row - 1, M->n_row - 1, 0);
1006 :
1007 0 : isl_int_init(gcd);
1008 0 : for (i = 1; i < M->n_row; ++i) {
1009 0 : isl_seq_gcd(M->row[i], M->n_col, &gcd);
1010 0 : if (isl_int_is_divisible_by(gcd, M->row[0][0]))
1011 0 : continue;
1012 0 : div = isl_basic_map_alloc_div(bmap);
1013 0 : if (div < 0)
1014 0 : goto error;
1015 0 : isl_int_set_si(bmap->div[div][0], 0);
1016 0 : k = isl_basic_map_alloc_equality(bmap);
1017 0 : if (k < 0)
1018 0 : goto error;
1019 0 : isl_seq_cpy(bmap->eq[k], M->row[i], M->n_col);
1020 0 : isl_seq_clr(bmap->eq[k] + M->n_col, bmap->n_div - n_known);
1021 0 : isl_int_set(bmap->eq[k][M->n_col - n_known + div],
1022 : M->row[0][0]);
1023 : }
1024 0 : isl_int_clear(gcd);
1025 :
1026 0 : return bmap;
1027 : error:
1028 0 : isl_int_clear(gcd);
1029 0 : isl_basic_map_free(bmap);
1030 0 : return NULL;
1031 : }
1032 :
1033 : /* If there are any equalities that involve (multiple) unknown divs,
1034 : * then extract the stride information encoded by those equalities
1035 : * and make it explicitly available in "bmap".
1036 : *
1037 : * We first sort the divs so that the unknown divs appear last and
1038 : * then we count how many equalities involve these divs.
1039 : *
1040 : * Let these equalities be of the form
1041 : *
1042 : * A(x) + B y = 0
1043 : *
1044 : * where y represents the unknown divs and x the remaining variables.
1045 : * Let [H 0] be the Hermite Normal Form of B, i.e.,
1046 : *
1047 : * B = [H 0] Q
1048 : *
1049 : * Then x is a solution of the equalities iff
1050 : *
1051 : * H^-1 A(x) (= - [I 0] Q y)
1052 : *
1053 : * is an integer vector. Let d be the common denominator of H^-1.
1054 : * We impose
1055 : *
1056 : * d H^-1 A(x) = d alpha
1057 : *
1058 : * in add_strides, with alpha fresh existentially quantified variables.
1059 : */
1060 3359919417 : static __isl_give isl_basic_map *isl_basic_map_make_strides_explicit(
1061 : __isl_take isl_basic_map *bmap)
1062 : {
1063 : int known;
1064 : int n_known;
1065 : int n, n_col;
1066 : int total;
1067 : isl_ctx *ctx;
1068 : isl_mat *A, *B, *M;
1069 :
1070 3359919417 : known = isl_basic_map_divs_known(bmap);
1071 3359919417 : if (known < 0)
1072 0 : return isl_basic_map_free(bmap);
1073 3359919417 : if (known)
1074 3359919417 : return bmap;
1075 0 : bmap = isl_basic_map_sort_divs(bmap);
1076 0 : bmap = isl_basic_map_gauss(bmap, NULL);
1077 0 : if (!bmap)
1078 0 : return NULL;
1079 :
1080 0 : for (n_known = 0; n_known < bmap->n_div; ++n_known)
1081 0 : if (isl_int_is_zero(bmap->div[n_known][0]))
1082 0 : break;
1083 0 : ctx = isl_basic_map_get_ctx(bmap);
1084 0 : total = isl_space_dim(bmap->dim, isl_dim_all);
1085 0 : for (n = 0; n < bmap->n_eq; ++n)
1086 0 : if (isl_seq_first_non_zero(bmap->eq[n] + 1 + total + n_known,
1087 0 : bmap->n_div - n_known) == -1)
1088 0 : break;
1089 0 : if (n == 0)
1090 0 : return bmap;
1091 0 : B = isl_mat_sub_alloc6(ctx, bmap->eq, 0, n, 0, 1 + total + n_known);
1092 0 : n_col = bmap->n_div - n_known;
1093 0 : A = isl_mat_sub_alloc6(ctx, bmap->eq, 0, n, 1 + total + n_known, n_col);
1094 0 : A = isl_mat_left_hermite(A, 0, NULL, NULL);
1095 0 : A = isl_mat_drop_cols(A, n, n_col - n);
1096 0 : A = isl_mat_lin_to_aff(A);
1097 0 : A = isl_mat_right_inverse(A);
1098 0 : B = isl_mat_insert_zero_rows(B, 0, 1);
1099 0 : B = isl_mat_set_element_si(B, 0, 0, 1);
1100 0 : M = isl_mat_product(A, B);
1101 0 : if (!M)
1102 0 : return isl_basic_map_free(bmap);
1103 0 : bmap = add_strides(bmap, M, n_known);
1104 0 : bmap = isl_basic_map_gauss(bmap, NULL);
1105 0 : isl_mat_free(M);
1106 :
1107 0 : return bmap;
1108 : }
1109 :
1110 : /* Compute the affine hull of each basic map in "map" separately
1111 : * and make all stride information explicit so that we can remove
1112 : * all unknown divs without losing this information.
1113 : * The result is also guaranteed to be gaussed.
1114 : *
1115 : * In simple cases where a div is determined by an equality,
1116 : * calling isl_basic_map_gauss is enough to make the stride information
1117 : * explicit, as it will derive an explicit representation for the div
1118 : * from the equality. If, however, the stride information
1119 : * is encoded through multiple unknown divs then we need to make
1120 : * some extra effort in isl_basic_map_make_strides_explicit.
1121 : */
1122 1291397878 : static __isl_give isl_map *isl_map_local_affine_hull(__isl_take isl_map *map)
1123 : {
1124 : int i;
1125 :
1126 1291397878 : map = isl_map_cow(map);
1127 1291397878 : if (!map)
1128 0 : return NULL;
1129 :
1130 4651317295 : for (i = 0; i < map->n; ++i) {
1131 3359919417 : map->p[i] = isl_basic_map_affine_hull(map->p[i]);
1132 3359919417 : map->p[i] = isl_basic_map_gauss(map->p[i], NULL);
1133 3359919417 : map->p[i] = isl_basic_map_make_strides_explicit(map->p[i]);
1134 3359919417 : if (!map->p[i])
1135 0 : return isl_map_free(map);
1136 : }
1137 :
1138 1291397878 : return map;
1139 : }
1140 :
1141 645698939 : static __isl_give isl_set *isl_set_local_affine_hull(__isl_take isl_set *set)
1142 : {
1143 645698939 : return isl_map_local_affine_hull(set);
1144 : }
1145 :
1146 : /* Return an empty basic map living in the same space as "map".
1147 : */
1148 0 : static __isl_give isl_basic_map *replace_map_by_empty_basic_map(
1149 : __isl_take isl_map *map)
1150 : {
1151 : isl_space *space;
1152 :
1153 0 : space = isl_map_get_space(map);
1154 0 : isl_map_free(map);
1155 0 : return isl_basic_map_empty(space);
1156 : }
1157 :
1158 : /* Compute the affine hull of "map".
1159 : *
1160 : * We first compute the affine hull of each basic map separately.
1161 : * Then we align the divs and recompute the affine hulls of the basic
1162 : * maps since some of them may now have extra divs.
1163 : * In order to avoid performing parametric integer programming to
1164 : * compute explicit expressions for the divs, possible leading to
1165 : * an explosion in the number of basic maps, we first drop all unknown
1166 : * divs before aligning the divs. Note that isl_map_local_affine_hull tries
1167 : * to make sure that all stride information is explicitly available
1168 : * in terms of known divs. This involves calling isl_basic_set_gauss,
1169 : * which is also needed because affine_hull assumes its input has been gaussed,
1170 : * while isl_map_affine_hull may be called on input that has not been gaussed,
1171 : * in particular from initial_facet_constraint.
1172 : * Similarly, align_divs may reorder some divs so that we need to
1173 : * gauss the result again.
1174 : * Finally, we combine the individual affine hulls into a single
1175 : * affine hull.
1176 : */
1177 645698939 : __isl_give isl_basic_map *isl_map_affine_hull(__isl_take isl_map *map)
1178 : {
1179 645698939 : struct isl_basic_map *model = NULL;
1180 645698939 : struct isl_basic_map *hull = NULL;
1181 : struct isl_set *set;
1182 : isl_basic_set *bset;
1183 :
1184 645698939 : map = isl_map_detect_equalities(map);
1185 645698939 : map = isl_map_local_affine_hull(map);
1186 645698939 : map = isl_map_remove_empty_parts(map);
1187 645698939 : map = isl_map_remove_unknown_divs(map);
1188 645698939 : map = isl_map_align_divs_internal(map);
1189 :
1190 645698939 : if (!map)
1191 0 : return NULL;
1192 :
1193 645698939 : if (map->n == 0)
1194 0 : return replace_map_by_empty_basic_map(map);
1195 :
1196 645698939 : model = isl_basic_map_copy(map->p[0]);
1197 645698939 : set = isl_map_underlying_set(map);
1198 645698939 : set = isl_set_cow(set);
1199 645698939 : set = isl_set_local_affine_hull(set);
1200 645698939 : if (!set)
1201 0 : goto error;
1202 :
1203 1834004269 : while (set->n > 1)
1204 542606391 : set->p[0] = affine_hull(set->p[0], set->p[--set->n]);
1205 :
1206 645698939 : bset = isl_basic_set_copy(set->p[0]);
1207 645698939 : hull = isl_basic_map_overlying_set(bset, model);
1208 645698939 : isl_set_free(set);
1209 645698939 : hull = isl_basic_map_simplify(hull);
1210 645698939 : return isl_basic_map_finalize(hull);
1211 : error:
1212 0 : isl_basic_map_free(model);
1213 0 : isl_set_free(set);
1214 0 : return NULL;
1215 : }
1216 :
1217 645698939 : struct isl_basic_set *isl_set_affine_hull(struct isl_set *set)
1218 : {
1219 645698939 : return bset_from_bmap(isl_map_affine_hull(set_to_map(set)));
1220 : }
|