Bug Summary

File:tools/polly/lib/External/isl/isl_affine_hull.c
Warning:line 509, column 6
Access to field 'sample' results in a dereference of a null pointer (loaded from variable 'bset')

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