Bug Summary

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