Bug Summary

File:polly/lib/External/isl/isl_affine_hull.c
Warning:line 218, column 25
Access to field 'n_eq' results in a dereference of a null pointer (loaded from variable 'bset1')

Annotated Source Code

Press '?' to see keyboard shortcuts

clang -cc1 -triple x86_64-pc-linux-gnu -analyze -disable-free -disable-llvm-verifier -discard-value-names -main-file-name isl_affine_hull.c -analyzer-store=region -analyzer-opt-analyze-nested-blocks -analyzer-checker=core -analyzer-checker=apiModeling -analyzer-checker=unix -analyzer-checker=deadcode -analyzer-checker=security.insecureAPI.UncheckedReturn -analyzer-checker=security.insecureAPI.getpw -analyzer-checker=security.insecureAPI.gets -analyzer-checker=security.insecureAPI.mktemp -analyzer-checker=security.insecureAPI.mkstemp -analyzer-checker=security.insecureAPI.vfork -analyzer-checker=nullability.NullPassedToNonnull -analyzer-checker=nullability.NullReturnedFromNonnull -analyzer-output plist -w -setup-static-analyzer -analyzer-config-compatibility-mode=true -mrelocation-model pic -pic-level 2 -mthread-model posix -mframe-pointer=none -fmath-errno -fno-rounding-math -masm-verbose -mconstructor-aliases -munwind-tables -target-cpu x86-64 -dwarf-column-info -fno-split-dwarf-inlining -debugger-tuning=gdb -ffunction-sections -fdata-sections -resource-dir /usr/lib/llvm-11/lib/clang/11.0.0 -D _DEBUG -D _GNU_SOURCE -D __STDC_CONSTANT_MACROS -D __STDC_FORMAT_MACROS -D __STDC_LIMIT_MACROS -I /build/llvm-toolchain-snapshot-11~++20200309111110+2c36c23f347/build-llvm/tools/polly/lib/External -I /build/llvm-toolchain-snapshot-11~++20200309111110+2c36c23f347/polly/lib/External -I /build/llvm-toolchain-snapshot-11~++20200309111110+2c36c23f347/polly/lib/External/pet/include -I /build/llvm-toolchain-snapshot-11~++20200309111110+2c36c23f347/polly/lib/External/ppcg/include -I /build/llvm-toolchain-snapshot-11~++20200309111110+2c36c23f347/polly/lib/External/ppcg/imath -I /build/llvm-toolchain-snapshot-11~++20200309111110+2c36c23f347/build-llvm/tools/polly/lib/External/ppcg -I /build/llvm-toolchain-snapshot-11~++20200309111110+2c36c23f347/polly/lib/External/isl -I /build/llvm-toolchain-snapshot-11~++20200309111110+2c36c23f347/polly/lib/External/isl/include -I /build/llvm-toolchain-snapshot-11~++20200309111110+2c36c23f347/polly/lib/External/isl/imath -I /build/llvm-toolchain-snapshot-11~++20200309111110+2c36c23f347/build-llvm/tools/polly/lib/External/isl -I /build/llvm-toolchain-snapshot-11~++20200309111110+2c36c23f347/build-llvm/tools/polly/include -I /build/llvm-toolchain-snapshot-11~++20200309111110+2c36c23f347/build-llvm/tools/polly/lib/External/isl/include -I /build/llvm-toolchain-snapshot-11~++20200309111110+2c36c23f347/polly/include -I /build/llvm-toolchain-snapshot-11~++20200309111110+2c36c23f347/build-llvm/include -I /build/llvm-toolchain-snapshot-11~++20200309111110+2c36c23f347/llvm/include -U NDEBUG -internal-isystem /usr/local/include -internal-isystem /usr/lib/llvm-11/lib/clang/11.0.0/include -internal-externc-isystem /usr/include/x86_64-linux-gnu -internal-externc-isystem /include -internal-externc-isystem /usr/include -O2 -Wno-unused-parameter -Wwrite-strings -Wno-missing-field-initializers -Wno-long-long -Wno-comment -std=gnu99 -fconst-strings -fdebug-compilation-dir /build/llvm-toolchain-snapshot-11~++20200309111110+2c36c23f347/build-llvm/tools/polly/lib/External -fdebug-prefix-map=/build/llvm-toolchain-snapshot-11~++20200309111110+2c36c23f347=. -ferror-limit 19 -fmessage-length 0 -stack-protector 2 -fgnuc-version=4.2.1 -fobjc-runtime=gcc -fdiagnostics-show-option -vectorize-loops -vectorize-slp -analyzer-output=html -analyzer-config stable-report-filename=true -faddrsig -o /tmp/scan-build-2020-03-09-184146-41876-1 -x c /build/llvm-toolchain-snapshot-11~++20200309111110+2c36c23f347/polly/lib/External/isl/isl_affine_hull.c
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__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 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 isl_stat construct_column(
118 __isl_keep isl_basic_setisl_basic_map *bset1, __isl_keep isl_basic_setisl_basic_map *bset2,
119 unsigned row, unsigned col)
120{
121 int r;
122 isl_int a;
123 isl_int b;
124 isl_size total;
125
126 total = isl_basic_set_dim(bset1, isl_dim_set);
127 if (total < 0)
128 return isl_stat_error;
129
130 isl_int_init(a)isl_sioimath_init((a));
131 isl_int_init(b)isl_sioimath_init((b));
132 for (r = 0; r < row; ++r) {
133 if (isl_int_is_zero(bset2->eq[r][col])(isl_sioimath_sgn(*(bset2->eq[r][col])) == 0))
134 continue;
135 isl_int_gcd(b, bset2->eq[r][col], bset1->eq[row][col])isl_sioimath_gcd((b), *(bset2->eq[r][col]), *(bset1->eq
[row][col]))
;
136 isl_int_divexact(a, bset1->eq[row][col], b)isl_sioimath_tdiv_q((a), *(bset1->eq[row][col]), *(b));
137 isl_int_divexact(b, bset2->eq[r][col], b)isl_sioimath_tdiv_q((b), *(bset2->eq[r][col]), *(b));
138 isl_seq_combine(bset1->eq[r], a, bset1->eq[r],
139 b, bset1->eq[row], 1 + total);
140 isl_seq_scale(bset2->eq[r], bset2->eq[r], a, 1 + total);
141 }
142 isl_int_clear(a)isl_sioimath_clear((a));
143 isl_int_clear(b)isl_sioimath_clear((b));
144 delete_row(bset1, row);
145
146 return isl_stat_ok;
147}
148
149/* Make first row entries in column col of bset1 identical to
150 * those of bset2, using only these entries of the two matrices.
151 * Let t be the last row with different entries.
152 * For each row i < t, we set
153 * A[i] = (A[t][col]-B[t][col]) * A[i] + (B[i][col]-A[i][col) * A[t]
154 * B[i] = (A[t][col]-B[t][col]) * B[i] + (B[i][col]-A[i][col) * B[t]
155 * so that
156 * A[i][col] = B[i][col] = old(A[t][col]*B[i][col]-A[i][col]*B[t][col])
157 */
158static isl_bool transform_column(
159 __isl_keep isl_basic_setisl_basic_map *bset1, __isl_keep isl_basic_setisl_basic_map *bset2,
160 unsigned row, unsigned col)
161{
162 int i, t;
163 isl_int a, b, g;
164 isl_size total;
165
166 for (t = row-1; t >= 0; --t)
167 if (isl_int_ne(bset1->eq[t][col], bset2->eq[t][col])(isl_sioimath_cmp(*(bset1->eq[t][col]), *(bset2->eq[t][
col])) != 0)
)
168 break;
169 if (t < 0)
170 return isl_bool_false;
171
172 total = isl_basic_set_dim(bset1, isl_dim_set);
173 if (total < 0)
174 return isl_bool_error;
175 isl_int_init(a)isl_sioimath_init((a));
176 isl_int_init(b)isl_sioimath_init((b));
177 isl_int_init(g)isl_sioimath_init((g));
178 isl_int_sub(b, bset1->eq[t][col], bset2->eq[t][col])isl_sioimath_sub((b), *(bset1->eq[t][col]), *(bset2->eq
[t][col]))
;
179 for (i = 0; i < t; ++i) {
180 isl_int_sub(a, bset2->eq[i][col], bset1->eq[i][col])isl_sioimath_sub((a), *(bset2->eq[i][col]), *(bset1->eq
[i][col]))
;
181 isl_int_gcd(g, a, b)isl_sioimath_gcd((g), *(a), *(b));
182 isl_int_divexact(a, a, g)isl_sioimath_tdiv_q((a), *(a), *(g));
183 isl_int_divexact(g, b, g)isl_sioimath_tdiv_q((g), *(b), *(g));
184 isl_seq_combine(bset1->eq[i], g, bset1->eq[i], a, bset1->eq[t],
185 1 + total);
186 isl_seq_combine(bset2->eq[i], g, bset2->eq[i], a, bset2->eq[t],
187 1 + total);
188 }
189 isl_int_clear(a)isl_sioimath_clear((a));
190 isl_int_clear(b)isl_sioimath_clear((b));
191 isl_int_clear(g)isl_sioimath_clear((g));
192 delete_row(bset1, t);
193 delete_row(bset2, t);
194 return isl_bool_true;
195}
196
197/* The implementation is based on Section 5.2 of Michael Karr,
198 * "Affine Relationships Among Variables of a Program",
199 * except that the echelon form we use starts from the last column
200 * and that we are dealing with integer coefficients.
201 */
202static __isl_give isl_basic_setisl_basic_map *affine_hull(
203 __isl_take isl_basic_setisl_basic_map *bset1, __isl_take isl_basic_setisl_basic_map *bset2)
204{
205 isl_size dim;
206 unsigned total;
207 int col;
208 int row;
209
210 dim = isl_basic_set_dim(bset1, isl_dim_set);
211 if (dim < 0 || !bset2)
14
Assuming 'dim' is >= 0
15
Assuming 'bset2' is non-null
16
Taking false branch
212 goto error;
213
214 total = 1 + dim;
215
216 row = 0;
217 for (col = total-1; col
16.1
'col' is >= 0
>= 0; --col) {
17
Loop condition is true. Entering loop body
218 int is_zero1 = row >= bset1->n_eq ||
18
Access to field 'n_eq' results in a dereference of a null pointer (loaded from variable 'bset1')
219 isl_int_is_zero(bset1->eq[row][col])(isl_sioimath_sgn(*(bset1->eq[row][col])) == 0);
220 int is_zero2 = row >= bset2->n_eq ||
221 isl_int_is_zero(bset2->eq[row][col])(isl_sioimath_sgn(*(bset2->eq[row][col])) == 0);
222 if (!is_zero1 && !is_zero2) {
223 set_common_multiple(bset1, bset2, row, col);
224 ++row;
225 } else if (!is_zero1 && is_zero2) {
226 if (construct_column(bset1, bset2, row, col) < 0)
227 goto error;
228 } else if (is_zero1 && !is_zero2) {
229 if (construct_column(bset2, bset1, row, col) < 0)
230 goto error;
231 } else {
232 isl_bool transform;
233
234 transform = transform_column(bset1, bset2, row, col);
235 if (transform < 0)
236 goto error;
237 if (transform)
238 --row;
239 }
240 }
241 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", "/build/llvm-toolchain-snapshot-11~++20200309111110+2c36c23f347/polly/lib/External/isl/isl_affine_hull.c"
, 241); goto error; } while (0); } while (0)
;
242 isl_basic_set_free(bset2);
243 bset1 = isl_basic_set_normalize_constraints(bset1);
244 return bset1;
245error:
246 isl_basic_set_free(bset1);
247 isl_basic_set_free(bset2);
248 return NULL((void*)0);
249}
250
251/* Find an integer point in the set represented by "tab"
252 * that lies outside of the equality "eq" e(x) = 0.
253 * If "up" is true, look for a point satisfying e(x) - 1 >= 0.
254 * Otherwise, look for a point satisfying -e(x) - 1 >= 0 (i.e., e(x) <= -1).
255 * The point, if found, is returned.
256 * If no point can be found, a zero-length vector is returned.
257 *
258 * Before solving an ILP problem, we first check if simply
259 * adding the normal of the constraint to one of the known
260 * integer points in the basic set represented by "tab"
261 * yields another point inside the basic set.
262 *
263 * The caller of this function ensures that the tableau is bounded or
264 * that tab->basis and tab->n_unbounded have been set appropriately.
265 */
266static struct isl_vec *outside_point(struct isl_tab *tab, isl_int *eq, int up)
267{
268 struct isl_ctx *ctx;
269 struct isl_vec *sample = NULL((void*)0);
270 struct isl_tab_undo *snap;
271 unsigned dim;
272
273 if (!tab)
274 return NULL((void*)0);
275 ctx = tab->mat->ctx;
276
277 dim = tab->n_var;
278 sample = isl_vec_alloc(ctx, 1 + dim);
279 if (!sample)
280 return NULL((void*)0);
281 isl_int_set_si(sample->el[0], 1)isl_sioimath_set_si((sample->el[0]), 1);
282 isl_seq_combine(sample->el + 1,
283 ctx->one, tab->bmap->sample->el + 1,
284 up ? ctx->one : ctx->negone, eq + 1, dim);
285 if (isl_basic_map_contains(tab->bmap, sample))
286 return sample;
287 isl_vec_free(sample);
288 sample = NULL((void*)0);
289
290 snap = isl_tab_snap(tab);
291
292 if (!up)
293 isl_seq_neg(eq, eq, 1 + dim);
294 isl_int_sub_ui(eq[0], eq[0], 1)isl_sioimath_sub_ui((eq[0]), *(eq[0]), 1);
295
296 if (isl_tab_extend_cons(tab, 1) < 0)
297 goto error;
298 if (isl_tab_add_ineq(tab, eq) < 0)
299 goto error;
300
301 sample = isl_tab_sample(tab);
302
303 isl_int_add_ui(eq[0], eq[0], 1)isl_sioimath_add_ui((eq[0]), *(eq[0]), 1);
304 if (!up)
305 isl_seq_neg(eq, eq, 1 + dim);
306
307 if (sample && isl_tab_rollback(tab, snap) < 0)
308 goto error;
309
310 return sample;
311error:
312 isl_vec_free(sample);
313 return NULL((void*)0);
314}
315
316__isl_give isl_basic_setisl_basic_map *isl_basic_set_recession_cone(
317 __isl_take isl_basic_setisl_basic_map *bset)
318{
319 int i;
320 isl_bool empty;
321
322 empty = isl_basic_set_plain_is_empty(bset);
323 if (empty < 0)
324 return isl_basic_set_free(bset);
325 if (empty)
326 return bset;
327
328 bset = isl_basic_set_cow(bset);
329 if (isl_basic_set_check_no_locals(bset) < 0)
330 return isl_basic_set_free(bset);
331
332 for (i = 0; i < bset->n_eq; ++i)
333 isl_int_set_si(bset->eq[i][0], 0)isl_sioimath_set_si((bset->eq[i][0]), 0);
334
335 for (i = 0; i < bset->n_ineq; ++i)
336 isl_int_set_si(bset->ineq[i][0], 0)isl_sioimath_set_si((bset->ineq[i][0]), 0);
337
338 ISL_F_CLR(bset, ISL_BASIC_SET_NO_IMPLICIT)(((bset)->flags) &= ~((1 << 2)));
339 return isl_basic_set_implicit_equalities(bset);
340}
341
342/* Move "sample" to a point that is one up (or down) from the original
343 * point in dimension "pos".
344 */
345static void adjacent_point(__isl_keep isl_vec *sample, int pos, int up)
346{
347 if (up)
348 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)
;
349 else
350 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)
;
351}
352
353/* Check if any points that are adjacent to "sample" also belong to "bset".
354 * If so, add them to "hull" and return the updated hull.
355 *
356 * Before checking whether and adjacent point belongs to "bset", we first
357 * check whether it already belongs to "hull" as this test is typically
358 * much cheaper.
359 */
360static __isl_give isl_basic_setisl_basic_map *add_adjacent_points(
361 __isl_take isl_basic_setisl_basic_map *hull, __isl_take isl_vec *sample,
362 __isl_keep isl_basic_setisl_basic_map *bset)
363{
364 int i, up;
365 isl_size dim;
366
367 dim = isl_basic_set_dim(hull, isl_dim_set);
368 if (!sample || dim < 0)
369 goto error;
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 __isl_give isl_basic_setisl_basic_map *uset_affine_hull_bounded(
498 __isl_take isl_basic_setisl_basic_map *bset)
499{
500 struct isl_vec *sample = NULL((void*)0);
501 struct isl_basic_setisl_basic_map *hull;
502 struct isl_tab *tab = NULL((void*)0);
503 isl_size dim;
504
505 if (isl_basic_set_plain_is_empty(bset))
506 return bset;
507
508 dim = isl_basic_set_dim(bset, isl_dim_set);
509 if (dim < 0)
510 return isl_basic_set_free(bset);
511
512 if (bset->sample && bset->sample->size == 1 + dim) {
513 int contains = isl_basic_set_contains(bset, bset->sample);
514 if (contains < 0)
515 goto error;
516 if (contains) {
517 if (dim == 0)
518 return bset;
519 sample = isl_vec_copy(bset->sample);
520 } else {
521 isl_vec_free(bset->sample);
522 bset->sample = NULL((void*)0);
523 }
524 }
525
526 tab = isl_tab_from_basic_set(bset, 1);
527 if (!tab)
528 goto error;
529 if (tab->empty) {
530 isl_tab_free(tab);
531 isl_vec_free(sample);
532 return isl_basic_set_set_to_empty(bset);
533 }
534
535 if (!sample) {
536 struct isl_tab_undo *snap;
537 snap = isl_tab_snap(tab);
538 sample = isl_tab_sample(tab);
539 if (isl_tab_rollback(tab, snap) < 0)
540 goto error;
541 isl_vec_free(tab->bmap->sample);
542 tab->bmap->sample = isl_vec_copy(sample);
543 }
544
545 if (!sample)
546 goto error;
547 if (sample->size == 0) {
548 isl_tab_free(tab);
549 isl_vec_free(sample);
550 return isl_basic_set_set_to_empty(bset);
551 }
552
553 hull = initialize_hull(bset, sample);
554
555 hull = extend_affine_hull(tab, hull, bset);
556 isl_basic_set_free(bset);
557 isl_tab_free(tab);
558
559 return hull;
560error:
561 isl_vec_free(sample);
562 isl_tab_free(tab);
563 isl_basic_set_free(bset);
564 return NULL((void*)0);
565}
566
567/* Given an unbounded tableau and an integer point satisfying the tableau,
568 * construct an initial affine hull containing the recession cone
569 * shifted to the given point.
570 *
571 * The unbounded directions are taken from the last rows of the basis,
572 * which is assumed to have been initialized appropriately.
573 */
574static __isl_give isl_basic_setisl_basic_map *initial_hull(struct isl_tab *tab,
575 __isl_take isl_vec *vec)
576{
577 int i;
578 int k;
579 struct isl_basic_setisl_basic_map *bset = NULL((void*)0);
580 struct isl_ctx *ctx;
581 isl_size dim;
582
583 if (!vec || !tab)
584 return NULL((void*)0);
585 ctx = vec->ctx;
586 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"
, "/build/llvm-toolchain-snapshot-11~++20200309111110+2c36c23f347/polly/lib/External/isl/isl_affine_hull.c"
, 586); goto error; } while (0); } while (0)
;
587
588 bset = isl_basic_set_alloc(ctx, 0, vec->size - 1, 0, vec->size - 1, 0);
589 dim = isl_basic_set_dim(bset, isl_dim_set);
590 if (dim < 0)
591 goto error;
592 dim -= tab->n_unbounded;
593 for (i = 0; i < dim; ++i) {
594 k = isl_basic_set_alloc_equality(bset);
595 if (k < 0)
596 goto error;
597 isl_seq_cpy(bset->eq[k] + 1, tab->basis->row[1 + i] + 1,
598 vec->size - 1);
599 isl_seq_inner_product(bset->eq[k] + 1, vec->el +1,
600 vec->size - 1, &bset->eq[k][0]);
601 isl_int_neg(bset->eq[k][0], bset->eq[k][0])isl_sioimath_neg((bset->eq[k][0]), *(bset->eq[k][0]));
602 }
603 bset->sample = vec;
604 bset = isl_basic_set_gauss(bset, NULL((void*)0));
605
606 return bset;
607error:
608 isl_basic_set_free(bset);
609 isl_vec_free(vec);
610 return NULL((void*)0);
611}
612
613/* Given a tableau of a set and a tableau of the corresponding
614 * recession cone, detect and add all equalities to the tableau.
615 * If the tableau is bounded, then we can simply keep the
616 * tableau in its state after the return from extend_affine_hull.
617 * However, if the tableau is unbounded, then
618 * isl_tab_set_initial_basis_with_cone will add some additional
619 * constraints to the tableau that have to be removed again.
620 * In this case, we therefore rollback to the state before
621 * any constraints were added and then add the equalities back in.
622 */
623struct isl_tab *isl_tab_detect_equalities(struct isl_tab *tab,
624 struct isl_tab *tab_cone)
625{
626 int j;
627 struct isl_vec *sample;
628 struct isl_basic_setisl_basic_map *hull = NULL((void*)0);
629 struct isl_tab_undo *snap;
630
631 if (!tab || !tab_cone)
632 goto error;
633
634 snap = isl_tab_snap(tab);
635
636 isl_mat_free(tab->basis);
637 tab->basis = NULL((void*)0);
638
639 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"
, "/build/llvm-toolchain-snapshot-11~++20200309111110+2c36c23f347/polly/lib/External/isl/isl_affine_hull.c"
, 639); goto error; } while (0); } while (0)
;
640 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", "/build/llvm-toolchain-snapshot-11~++20200309111110+2c36c23f347/polly/lib/External/isl/isl_affine_hull.c"
, 640); goto error; } while (0); } while (0)
;
641 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", "/build/llvm-toolchain-snapshot-11~++20200309111110+2c36c23f347/polly/lib/External/isl/isl_affine_hull.c"
, 641); goto error; } while (0); } while (0)
;
642 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", "/build/llvm-toolchain-snapshot-11~++20200309111110+2c36c23f347/polly/lib/External/isl/isl_affine_hull.c"
, 642); goto error; } while (0); } while (0)
;
643
644 if (isl_tab_set_initial_basis_with_cone(tab, tab_cone) < 0)
645 goto error;
646
647 sample = isl_vec_alloc(tab->mat->ctx, 1 + tab->n_var);
648 if (!sample)
649 goto error;
650
651 isl_seq_cpy(sample->el, tab->samples->row[tab->n_outside], sample->size);
652
653 isl_vec_free(tab->bmap->sample);
654 tab->bmap->sample = isl_vec_copy(sample);
655
656 if (tab->n_unbounded == 0)
657 hull = isl_basic_set_from_vec(isl_vec_copy(sample));
658 else
659 hull = initial_hull(tab, isl_vec_copy(sample));
660
661 for (j = tab->n_outside + 1; j < tab->n_sample; ++j) {
662 isl_seq_cpy(sample->el, tab->samples->row[j], sample->size);
663 hull = affine_hull(hull,
664 isl_basic_set_from_vec(isl_vec_copy(sample)));
665 }
666
667 isl_vec_free(sample);
668
669 hull = extend_affine_hull(tab, hull, NULL((void*)0));
670 if (!hull)
671 goto error;
672
673 if (tab->n_unbounded == 0) {
674 isl_basic_set_free(hull);
675 return tab;
676 }
677
678 if (isl_tab_rollback(tab, snap) < 0)
679 goto error;
680
681 if (hull->n_eq > tab->n_zero) {
682 for (j = 0; j < hull->n_eq; ++j) {
683 isl_seq_normalize(tab->mat->ctx, hull->eq[j], 1 + tab->n_var);
684 if (isl_tab_add_eq(tab, hull->eq[j]) < 0)
685 goto error;
686 }
687 }
688
689 isl_basic_set_free(hull);
690
691 return tab;
692error:
693 isl_basic_set_free(hull);
694 isl_tab_free(tab);
695 return NULL((void*)0);
696}
697
698/* Compute the affine hull of "bset", where "cone" is the recession cone
699 * of "bset".
700 *
701 * We first compute a unimodular transformation that puts the unbounded
702 * directions in the last dimensions. In particular, we take a transformation
703 * that maps all equalities to equalities (in HNF) on the first dimensions.
704 * Let x be the original dimensions and y the transformed, with y_1 bounded
705 * and y_2 unbounded.
706 *
707 * [ y_1 ] [ y_1 ] [ Q_1 ]
708 * x = U [ y_2 ] [ y_2 ] = [ Q_2 ] x
709 *
710 * Let's call the input basic set S. We compute S' = preimage(S, U)
711 * and drop the final dimensions including any constraints involving them.
712 * This results in set S''.
713 * Then we compute the affine hull A'' of S''.
714 * Let F y_1 >= g be the constraint system of A''. In the transformed
715 * space the y_2 are unbounded, so we can add them back without any constraints,
716 * resulting in
717 *
718 * [ y_1 ]
719 * [ F 0 ] [ y_2 ] >= g
720 * or
721 * [ Q_1 ]
722 * [ F 0 ] [ Q_2 ] x >= g
723 * or
724 * F Q_1 x >= g
725 *
726 * The affine hull in the original space is then obtained as
727 * A = preimage(A'', Q_1).
728 */
729static struct isl_basic_setisl_basic_map *affine_hull_with_cone(struct isl_basic_setisl_basic_map *bset,
730 struct isl_basic_setisl_basic_map *cone)
731{
732 isl_size total;
733 unsigned cone_dim;
734 struct isl_basic_setisl_basic_map *hull;
735 struct isl_mat *M, *U, *Q;
736
737 total = isl_basic_set_dim(cone, isl_dim_all);
738 if (!bset || total < 0)
739 goto error;
740
741 cone_dim = total - cone->n_eq;
742
743 M = isl_mat_sub_alloc6(bset->ctx, cone->eq, 0, cone->n_eq, 1, total);
744 M = isl_mat_left_hermite(M, 0, &U, &Q);
745 if (!M)
746 goto error;
747 isl_mat_free(M);
748
749 U = isl_mat_lin_to_aff(U);
750 bset = isl_basic_set_preimage(bset, isl_mat_copy(U));
751
752 bset = isl_basic_set_drop_constraints_involving(bset, total - cone_dim,
753 cone_dim);
754 bset = isl_basic_set_drop_dims(bset, total - cone_dim, cone_dim);
755
756 Q = isl_mat_lin_to_aff(Q);
757 Q = isl_mat_drop_rows(Q, 1 + total - cone_dim, cone_dim);
758
759 if (bset && bset->sample && bset->sample->size == 1 + total)
760 bset->sample = isl_mat_vec_product(isl_mat_copy(Q), bset->sample);
761
762 hull = uset_affine_hull_bounded(bset);
763
764 if (!hull) {
765 isl_mat_free(Q);
766 isl_mat_free(U);
767 } else {
768 struct isl_vec *sample = isl_vec_copy(hull->sample);
769 U = isl_mat_drop_cols(U, 1 + total - cone_dim, cone_dim);
770 if (sample && sample->size > 0)
771 sample = isl_mat_vec_product(U, sample);
772 else
773 isl_mat_free(U);
774 hull = isl_basic_set_preimage(hull, Q);
775 if (hull) {
776 isl_vec_free(hull->sample);
777 hull->sample = sample;
778 } else
779 isl_vec_free(sample);
780 }
781
782 isl_basic_set_free(cone);
783
784 return hull;
785error:
786 isl_basic_set_free(bset);
787 isl_basic_set_free(cone);
788 return NULL((void*)0);
789}
790
791/* Look for all equalities satisfied by the integer points in bset,
792 * which is assumed not to have any explicit equalities.
793 *
794 * The equalities are obtained by successively looking for
795 * a point that is affinely independent of the points found so far.
796 * In particular, for each equality satisfied by the points so far,
797 * we check if there is any point on a hyperplane parallel to the
798 * corresponding hyperplane shifted by at least one (in either direction).
799 *
800 * Before looking for any outside points, we first compute the recession
801 * cone. The directions of this recession cone will always be part
802 * of the affine hull, so there is no need for looking for any points
803 * in these directions.
804 * In particular, if the recession cone is full-dimensional, then
805 * the affine hull is simply the whole universe.
806 */
807static struct isl_basic_setisl_basic_map *uset_affine_hull(struct isl_basic_setisl_basic_map *bset)
808{
809 struct isl_basic_setisl_basic_map *cone;
810 isl_size total;
811
812 if (isl_basic_set_plain_is_empty(bset))
813 return bset;
814
815 cone = isl_basic_set_recession_cone(isl_basic_set_copy(bset));
816 if (!cone)
817 goto error;
818 if (cone->n_eq == 0) {
819 isl_space *space;
820 space = isl_basic_set_get_space(bset);
821 isl_basic_set_free(cone);
822 isl_basic_set_free(bset);
823 return isl_basic_set_universe(space);
824 }
825
826 total = isl_basic_set_dim(cone, isl_dim_all);
827 if (total < 0)
828 bset = isl_basic_set_free(bset);
829 if (cone->n_eq < total)
830 return affine_hull_with_cone(bset, cone);
831
832 isl_basic_set_free(cone);
833 return uset_affine_hull_bounded(bset);
834error:
835 isl_basic_set_free(bset);
836 return NULL((void*)0);
837}
838
839/* Look for all equalities satisfied by the integer points in bmap
840 * that are independent of the equalities already explicitly available
841 * in bmap.
842 *
843 * We first remove all equalities already explicitly available,
844 * then look for additional equalities in the reduced space
845 * and then transform the result to the original space.
846 * The original equalities are _not_ added to this set. This is
847 * the responsibility of the calling function.
848 * The resulting basic set has all meaning about the dimensions removed.
849 * In particular, dimensions that correspond to existential variables
850 * in bmap and that are found to be fixed are not removed.
851 */
852static struct isl_basic_setisl_basic_map *equalities_in_underlying_set(
853 struct isl_basic_map *bmap)
854{
855 struct isl_mat *T1 = NULL((void*)0);
856 struct isl_mat *T2 = NULL((void*)0);
857 struct isl_basic_setisl_basic_map *bset = NULL((void*)0);
858 struct isl_basic_setisl_basic_map *hull = NULL((void*)0);
859
860 bset = isl_basic_map_underlying_set(bmap);
861 if (!bset)
862 return NULL((void*)0);
863 if (bset->n_eq)
864 bset = isl_basic_set_remove_equalities(bset, &T1, &T2);
865 if (!bset)
866 goto error;
867
868 hull = uset_affine_hull(bset);
869 if (!T2)
870 return hull;
871
872 if (!hull) {
873 isl_mat_free(T1);
874 isl_mat_free(T2);
875 } else {
876 struct isl_vec *sample = isl_vec_copy(hull->sample);
877 if (sample && sample->size > 0)
878 sample = isl_mat_vec_product(T1, sample);
879 else
880 isl_mat_free(T1);
881 hull = isl_basic_set_preimage(hull, T2);
882 if (hull) {
883 isl_vec_free(hull->sample);
884 hull->sample = sample;
885 } else
886 isl_vec_free(sample);
887 }
888
889 return hull;
890error:
891 isl_mat_free(T1);
892 isl_mat_free(T2);
893 isl_basic_set_free(bset);
894 isl_basic_set_free(hull);
895 return NULL((void*)0);
896}
897
898/* Detect and make explicit all equalities satisfied by the (integer)
899 * points in bmap.
900 */
901__isl_give isl_basic_map *isl_basic_map_detect_equalities(
902 __isl_take isl_basic_map *bmap)
903{
904 int i, j;
905 isl_size total;
906 struct isl_basic_setisl_basic_map *hull = NULL((void*)0);
907
908 if (!bmap)
909 return NULL((void*)0);
910 if (bmap->n_ineq == 0)
911 return bmap;
912 if (ISL_F_ISSET(bmap, ISL_BASIC_MAP_EMPTY)(!!(((bmap)->flags) & ((1 << 1)))))
913 return bmap;
914 if (ISL_F_ISSET(bmap, ISL_BASIC_MAP_ALL_EQUALITIES)(!!(((bmap)->flags) & ((1 << 7)))))
915 return bmap;
916 if (ISL_F_ISSET(bmap, ISL_BASIC_MAP_RATIONAL)(!!(((bmap)->flags) & ((1 << 4)))))
917 return isl_basic_map_implicit_equalities(bmap);
918
919 hull = equalities_in_underlying_set(isl_basic_map_copy(bmap));
920 if (!hull)
921 goto error;
922 if (ISL_F_ISSET(hull, ISL_BASIC_SET_EMPTY)(!!(((hull)->flags) & ((1 << 1))))) {
923 isl_basic_set_free(hull);
924 return isl_basic_map_set_to_empty(bmap);
925 }
926 bmap = isl_basic_map_extend(bmap, 0, hull->n_eq, 0);
927 total = isl_basic_set_dim(hull, isl_dim_all);
928 if (total < 0)
929 goto error;
930 for (i = 0; i < hull->n_eq; ++i) {
931 j = isl_basic_map_alloc_equality(bmap);
932 if (j < 0)
933 goto error;
934 isl_seq_cpy(bmap->eq[j], hull->eq[i], 1 + total);
935 }
936 isl_vec_free(bmap->sample);
937 bmap->sample = isl_vec_copy(hull->sample);
938 isl_basic_set_free(hull);
939 ISL_F_SET(bmap, ISL_BASIC_MAP_NO_IMPLICIT | ISL_BASIC_MAP_ALL_EQUALITIES)(((bmap)->flags) |= ((1 << 2) | (1 << 7)));
940 bmap = isl_basic_map_simplify(bmap);
941 return isl_basic_map_finalize(bmap);
942error:
943 isl_basic_set_free(hull);
944 isl_basic_map_free(bmap);
945 return NULL((void*)0);
946}
947
948__isl_give isl_basic_setisl_basic_map *isl_basic_set_detect_equalities(
949 __isl_take isl_basic_setisl_basic_map *bset)
950{
951 return bset_from_bmap(
952 isl_basic_map_detect_equalities(bset_to_bmap(bset)));
953}
954
955__isl_give isl_map *isl_map_detect_equalities(__isl_take isl_map *map)
956{
957 return isl_map_inline_foreach_basic_map(map,
958 &isl_basic_map_detect_equalities);
959}
960
961__isl_give isl_setisl_map *isl_set_detect_equalities(__isl_take isl_setisl_map *set)
962{
963 return set_from_map(isl_map_detect_equalities(set_to_map(set)));
964}
965
966/* Return the superset of "bmap" described by the equalities
967 * satisfied by "bmap" that are already known.
968 */
969__isl_give isl_basic_map *isl_basic_map_plain_affine_hull(
970 __isl_take isl_basic_map *bmap)
971{
972 bmap = isl_basic_map_cow(bmap);
973 if (bmap)
974 isl_basic_map_free_inequality(bmap, bmap->n_ineq);
975 bmap = isl_basic_map_finalize(bmap);
976 return bmap;
977}
978
979/* Return the superset of "bset" described by the equalities
980 * satisfied by "bset" that are already known.
981 */
982__isl_give isl_basic_setisl_basic_map *isl_basic_set_plain_affine_hull(
983 __isl_take isl_basic_setisl_basic_map *bset)
984{
985 return isl_basic_map_plain_affine_hull(bset);
986}
987
988/* After computing the rational affine hull (by detecting the implicit
989 * equalities), we compute the additional equalities satisfied by
990 * the integer points (if any) and add the original equalities back in.
991 */
992__isl_give isl_basic_map *isl_basic_map_affine_hull(
993 __isl_take isl_basic_map *bmap)
994{
995 bmap = isl_basic_map_detect_equalities(bmap);
996 bmap = isl_basic_map_plain_affine_hull(bmap);
997 return bmap;
998}
999
1000struct isl_basic_setisl_basic_map *isl_basic_set_affine_hull(struct isl_basic_setisl_basic_map *bset)
1001{
1002 return bset_from_bmap(isl_basic_map_affine_hull(bset_to_bmap(bset)));
1003}
1004
1005/* Given a rational affine matrix "M", add stride constraints to "bmap"
1006 * that ensure that
1007 *
1008 * M(x)
1009 *
1010 * is an integer vector. The variables x include all the variables
1011 * of "bmap" except the unknown divs.
1012 *
1013 * If d is the common denominator of M, then we need to impose that
1014 *
1015 * d M(x) = 0 mod d
1016 *
1017 * or
1018 *
1019 * exists alpha : d M(x) = d alpha
1020 *
1021 * This function is similar to add_strides in isl_morph.c
1022 */
1023static __isl_give isl_basic_map *add_strides(__isl_take isl_basic_map *bmap,
1024 __isl_keep isl_mat *M, int n_known)
1025{
1026 int i, div, k;
1027 isl_int gcd;
1028
1029 if (isl_int_is_one(M->row[0][0])(isl_sioimath_cmp_si(*(M->row[0][0]), 1) == 0))
1030 return bmap;
1031
1032 bmap = isl_basic_map_extend(bmap, M->n_row - 1, M->n_row - 1, 0);
1033
1034 isl_int_init(gcd)isl_sioimath_init((gcd));
1035 for (i = 1; i < M->n_row; ++i) {
1036 isl_seq_gcd(M->row[i], M->n_col, &gcd);
1037 if (isl_int_is_divisible_by(gcd, M->row[0][0])isl_sioimath_is_divisible_by(*(gcd), *(M->row[0][0])))
1038 continue;
1039 div = isl_basic_map_alloc_div(bmap);
1040 if (div < 0)
1041 goto error;
1042 isl_int_set_si(bmap->div[div][0], 0)isl_sioimath_set_si((bmap->div[div][0]), 0);
1043 k = isl_basic_map_alloc_equality(bmap);
1044 if (k < 0)
1045 goto error;
1046 isl_seq_cpy(bmap->eq[k], M->row[i], M->n_col);
1047 isl_seq_clr(bmap->eq[k] + M->n_col, bmap->n_div - n_known);
1048 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]))
1049 M->row[0][0])isl_sioimath_set((bmap->eq[k][M->n_col - n_known + div]
), *(M->row[0][0]))
;
1050 }
1051 isl_int_clear(gcd)isl_sioimath_clear((gcd));
1052
1053 return bmap;
1054error:
1055 isl_int_clear(gcd)isl_sioimath_clear((gcd));
1056 isl_basic_map_free(bmap);
1057 return NULL((void*)0);
1058}
1059
1060/* If there are any equalities that involve (multiple) unknown divs,
1061 * then extract the stride information encoded by those equalities
1062 * and make it explicitly available in "bmap".
1063 *
1064 * We first sort the divs so that the unknown divs appear last and
1065 * then we count how many equalities involve these divs.
1066 *
1067 * Let these equalities be of the form
1068 *
1069 * A(x) + B y = 0
1070 *
1071 * where y represents the unknown divs and x the remaining variables.
1072 * Let [H 0] be the Hermite Normal Form of B, i.e.,
1073 *
1074 * B = [H 0] Q
1075 *
1076 * Then x is a solution of the equalities iff
1077 *
1078 * H^-1 A(x) (= - [I 0] Q y)
1079 *
1080 * is an integer vector. Let d be the common denominator of H^-1.
1081 * We impose
1082 *
1083 * d H^-1 A(x) = d alpha
1084 *
1085 * in add_strides, with alpha fresh existentially quantified variables.
1086 */
1087static __isl_give isl_basic_map *isl_basic_map_make_strides_explicit(
1088 __isl_take isl_basic_map *bmap)
1089{
1090 isl_bool known;
1091 int n_known;
1092 int n, n_col;
1093 isl_size v_div;
1094 isl_ctx *ctx;
1095 isl_mat *A, *B, *M;
1096
1097 known = isl_basic_map_divs_known(bmap);
1098 if (known < 0)
1099 return isl_basic_map_free(bmap);
1100 if (known)
1101 return bmap;
1102 bmap = isl_basic_map_sort_divs(bmap);
1103 bmap = isl_basic_map_gauss(bmap, NULL((void*)0));
1104 if (!bmap)
1105 return NULL((void*)0);
1106
1107 for (n_known = 0; n_known < bmap->n_div; ++n_known)
1108 if (isl_int_is_zero(bmap->div[n_known][0])(isl_sioimath_sgn(*(bmap->div[n_known][0])) == 0))
1109 break;
1110 ctx = isl_basic_map_get_ctx(bmap);
1111 v_div = isl_basic_map_var_offset(bmap, isl_dim_div);
1112 if (v_div < 0)
1113 return isl_basic_map_free(bmap);
1114 for (n = 0; n < bmap->n_eq; ++n)
1115 if (isl_seq_first_non_zero(bmap->eq[n] + 1 + v_div + n_known,
1116 bmap->n_div - n_known) == -1)
1117 break;
1118 if (n == 0)
1119 return bmap;
1120 B = isl_mat_sub_alloc6(ctx, bmap->eq, 0, n, 0, 1 + v_div + n_known);
1121 n_col = bmap->n_div - n_known;
1122 A = isl_mat_sub_alloc6(ctx, bmap->eq, 0, n, 1 + v_div + n_known, n_col);
1123 A = isl_mat_left_hermite(A, 0, NULL((void*)0), NULL((void*)0));
1124 A = isl_mat_drop_cols(A, n, n_col - n);
1125 A = isl_mat_lin_to_aff(A);
1126 A = isl_mat_right_inverse(A);
1127 B = isl_mat_insert_zero_rows(B, 0, 1);
1128 B = isl_mat_set_element_si(B, 0, 0, 1);
1129 M = isl_mat_product(A, B);
1130 if (!M)
1131 return isl_basic_map_free(bmap);
1132 bmap = add_strides(bmap, M, n_known);
1133 bmap = isl_basic_map_gauss(bmap, NULL((void*)0));
1134 isl_mat_free(M);
1135
1136 return bmap;
1137}
1138
1139/* Compute the affine hull of each basic map in "map" separately
1140 * and make all stride information explicit so that we can remove
1141 * all unknown divs without losing this information.
1142 * The result is also guaranteed to be gaussed.
1143 *
1144 * In simple cases where a div is determined by an equality,
1145 * calling isl_basic_map_gauss is enough to make the stride information
1146 * explicit, as it will derive an explicit representation for the div
1147 * from the equality. If, however, the stride information
1148 * is encoded through multiple unknown divs then we need to make
1149 * some extra effort in isl_basic_map_make_strides_explicit.
1150 */
1151static __isl_give isl_map *isl_map_local_affine_hull(__isl_take isl_map *map)
1152{
1153 int i;
1154
1155 map = isl_map_cow(map);
1156 if (!map)
1157 return NULL((void*)0);
1158
1159 for (i = 0; i < map->n; ++i) {
1160 map->p[i] = isl_basic_map_affine_hull(map->p[i]);
1161 map->p[i] = isl_basic_map_gauss(map->p[i], NULL((void*)0));
1162 map->p[i] = isl_basic_map_make_strides_explicit(map->p[i]);
1163 if (!map->p[i])
1164 return isl_map_free(map);
1165 }
1166
1167 return map;
1168}
1169
1170static __isl_give isl_setisl_map *isl_set_local_affine_hull(__isl_take isl_setisl_map *set)
1171{
1172 return isl_map_local_affine_hull(set);
1173}
1174
1175/* Return an empty basic map living in the same space as "map".
1176 */
1177static __isl_give isl_basic_map *replace_map_by_empty_basic_map(
1178 __isl_take isl_map *map)
1179{
1180 isl_space *space;
1181
1182 space = isl_map_get_space(map);
1183 isl_map_free(map);
1184 return isl_basic_map_empty(space);
1185}
1186
1187/* Compute the affine hull of "map".
1188 *
1189 * We first compute the affine hull of each basic map separately.
1190 * Then we align the divs and recompute the affine hulls of the basic
1191 * maps since some of them may now have extra divs.
1192 * In order to avoid performing parametric integer programming to
1193 * compute explicit expressions for the divs, possible leading to
1194 * an explosion in the number of basic maps, we first drop all unknown
1195 * divs before aligning the divs. Note that isl_map_local_affine_hull tries
1196 * to make sure that all stride information is explicitly available
1197 * in terms of known divs. This involves calling isl_basic_set_gauss,
1198 * which is also needed because affine_hull assumes its input has been gaussed,
1199 * while isl_map_affine_hull may be called on input that has not been gaussed,
1200 * in particular from initial_facet_constraint.
1201 * Similarly, align_divs may reorder some divs so that we need to
1202 * gauss the result again.
1203 * Finally, we combine the individual affine hulls into a single
1204 * affine hull.
1205 */
1206__isl_give isl_basic_map *isl_map_affine_hull(__isl_take isl_map *map)
1207{
1208 struct isl_basic_map *model = NULL((void*)0);
1209 struct isl_basic_map *hull = NULL((void*)0);
1210 struct isl_setisl_map *set;
1211 isl_basic_setisl_basic_map *bset;
1212
1213 map = isl_map_detect_equalities(map);
1214 map = isl_map_local_affine_hull(map);
1215 map = isl_map_remove_empty_parts(map);
1216 map = isl_map_remove_unknown_divs(map);
1217 map = isl_map_align_divs_internal(map);
1218
1219 if (!map)
2
Assuming 'map' is non-null
3
Taking false branch
1220 return NULL((void*)0);
1221
1222 if (map->n == 0)
4
Assuming field 'n' is not equal to 0
5
Taking false branch
1223 return replace_map_by_empty_basic_map(map);
1224
1225 model = isl_basic_map_copy(map->p[0]);
1226 set = isl_map_underlying_set(map);
1227 set = isl_set_cow(set);
1228 set = isl_set_local_affine_hull(set);
1229 if (!set)
6
Assuming 'set' is non-null
7
Taking false branch
1230 goto error;
1231
1232 while (set->n > 1)
8
Assuming field 'n' is > 1
9
Loop condition is true. Entering loop body
10
Assuming field 'n' is > 1
11
Loop condition is true. Entering loop body
1233 set->p[0] = affine_hull(set->p[0], set->p[--set->n]);
12
Passing null pointer value via 1st parameter 'bset1'
13
Calling 'affine_hull'
1234
1235 bset = isl_basic_set_copy(set->p[0]);
1236 hull = isl_basic_map_overlying_set(bset, model);
1237 isl_set_free(set);
1238 hull = isl_basic_map_simplify(hull);
1239 return isl_basic_map_finalize(hull);
1240error:
1241 isl_basic_map_free(model);
1242 isl_set_free(set);
1243 return NULL((void*)0);
1244}
1245
1246struct isl_basic_setisl_basic_map *isl_set_affine_hull(struct isl_setisl_map *set)
1247{
1248 return bset_from_bmap(isl_map_affine_hull(set_to_map(set)));
1
Calling 'isl_map_affine_hull'
1249}