Bug Summary

File:polly/lib/External/isl/isl_map_simplify.c
Warning:line 1253, column 6
3rd function call argument is an uninitialized value

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_map_simplify.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_map_simplify.c
1/*
2 * Copyright 2008-2009 Katholieke Universiteit Leuven
3 * Copyright 2012-2013 Ecole Normale Superieure
4 * Copyright 2014-2015 INRIA Rocquencourt
5 * Copyright 2016 Sven Verdoolaege
6 *
7 * Use of this software is governed by the MIT license
8 *
9 * Written by Sven Verdoolaege, K.U.Leuven, Departement
10 * Computerwetenschappen, Celestijnenlaan 200A, B-3001 Leuven, Belgium
11 * and Ecole Normale Superieure, 45 rue d’Ulm, 75230 Paris, France
12 * and Inria Paris - Rocquencourt, Domaine de Voluceau - Rocquencourt,
13 * B.P. 105 - 78153 Le Chesnay, France
14 */
15
16#include <isl_ctx_private.h>
17#include <isl_map_private.h>
18#include "isl_equalities.h"
19#include <isl/map.h>
20#include <isl_seq.h>
21#include "isl_tab.h"
22#include <isl_space_private.h>
23#include <isl_mat_private.h>
24#include <isl_vec_private.h>
25
26#include <bset_to_bmap.c>
27#include <bset_from_bmap.c>
28#include <set_to_map.c>
29#include <set_from_map.c>
30
31static void swap_equality(struct isl_basic_map *bmap, int a, int b)
32{
33 isl_int *t = bmap->eq[a];
34 bmap->eq[a] = bmap->eq[b];
35 bmap->eq[b] = t;
36}
37
38static void swap_inequality(struct isl_basic_map *bmap, int a, int b)
39{
40 if (a != b) {
41 isl_int *t = bmap->ineq[a];
42 bmap->ineq[a] = bmap->ineq[b];
43 bmap->ineq[b] = t;
44 }
45}
46
47__isl_give isl_basic_map *isl_basic_map_normalize_constraints(
48 __isl_take isl_basic_map *bmap)
49{
50 int i;
51 isl_int gcd;
52 isl_size total = isl_basic_map_dim(bmap, isl_dim_all);
53
54 if (total < 0)
55 return isl_basic_map_free(bmap);
56
57 isl_int_init(gcd)isl_sioimath_init((gcd));
58 for (i = bmap->n_eq - 1; i >= 0; --i) {
59 isl_seq_gcd(bmap->eq[i]+1, total, &gcd);
60 if (isl_int_is_zero(gcd)(isl_sioimath_sgn(*(gcd)) == 0)) {
61 if (!isl_int_is_zero(bmap->eq[i][0])(isl_sioimath_sgn(*(bmap->eq[i][0])) == 0)) {
62 bmap = isl_basic_map_set_to_empty(bmap);
63 break;
64 }
65 if (isl_basic_map_drop_equality(bmap, i) < 0)
66 goto error;
67 continue;
68 }
69 if (ISL_F_ISSET(bmap, ISL_BASIC_MAP_RATIONAL)(!!(((bmap)->flags) & ((1 << 4)))))
70 isl_int_gcd(gcd, gcd, bmap->eq[i][0])isl_sioimath_gcd((gcd), *(gcd), *(bmap->eq[i][0]));
71 if (isl_int_is_one(gcd)(isl_sioimath_cmp_si(*(gcd), 1) == 0))
72 continue;
73 if (!isl_int_is_divisible_by(bmap->eq[i][0], gcd)isl_sioimath_is_divisible_by(*(bmap->eq[i][0]), *(gcd))) {
74 bmap = isl_basic_map_set_to_empty(bmap);
75 break;
76 }
77 isl_seq_scale_down(bmap->eq[i], bmap->eq[i], gcd, 1+total);
78 }
79
80 for (i = bmap->n_ineq - 1; i >= 0; --i) {
81 isl_seq_gcd(bmap->ineq[i]+1, total, &gcd);
82 if (isl_int_is_zero(gcd)(isl_sioimath_sgn(*(gcd)) == 0)) {
83 if (isl_int_is_neg(bmap->ineq[i][0])(isl_sioimath_sgn(*(bmap->ineq[i][0])) < 0)) {
84 bmap = isl_basic_map_set_to_empty(bmap);
85 break;
86 }
87 if (isl_basic_map_drop_inequality(bmap, i) < 0)
88 goto error;
89 continue;
90 }
91 if (ISL_F_ISSET(bmap, ISL_BASIC_MAP_RATIONAL)(!!(((bmap)->flags) & ((1 << 4)))))
92 isl_int_gcd(gcd, gcd, bmap->ineq[i][0])isl_sioimath_gcd((gcd), *(gcd), *(bmap->ineq[i][0]));
93 if (isl_int_is_one(gcd)(isl_sioimath_cmp_si(*(gcd), 1) == 0))
94 continue;
95 isl_int_fdiv_q(bmap->ineq[i][0], bmap->ineq[i][0], gcd)isl_sioimath_fdiv_q((bmap->ineq[i][0]), *(bmap->ineq[i]
[0]), *(gcd))
;
96 isl_seq_scale_down(bmap->ineq[i]+1, bmap->ineq[i]+1, gcd, total);
97 }
98 isl_int_clear(gcd)isl_sioimath_clear((gcd));
99
100 return bmap;
101error:
102 isl_int_clear(gcd)isl_sioimath_clear((gcd));
103 isl_basic_map_free(bmap);
104 return NULL((void*)0);
105}
106
107__isl_give isl_basic_setisl_basic_map *isl_basic_set_normalize_constraints(
108 __isl_take isl_basic_setisl_basic_map *bset)
109{
110 isl_basic_map *bmap = bset_to_bmap(bset);
111 return bset_from_bmap(isl_basic_map_normalize_constraints(bmap));
112}
113
114/* Reduce the coefficient of the variable at position "pos"
115 * in integer division "div", such that it lies in the half-open
116 * interval (1/2,1/2], extracting any excess value from this integer division.
117 * "pos" is as determined by isl_basic_map_offset, i.e., pos == 0
118 * corresponds to the constant term.
119 *
120 * That is, the integer division is of the form
121 *
122 * floor((... + (c * d + r) * x_pos + ...)/d)
123 *
124 * with -d < 2 * r <= d.
125 * Replace it by
126 *
127 * floor((... + r * x_pos + ...)/d) + c * x_pos
128 *
129 * If 2 * ((c * d + r) % d) <= d, then c = floor((c * d + r)/d).
130 * Otherwise, c = floor((c * d + r)/d) + 1.
131 *
132 * This is the same normalization that is performed by isl_aff_floor.
133 */
134static __isl_give isl_basic_map *reduce_coefficient_in_div(
135 __isl_take isl_basic_map *bmap, int div, int pos)
136{
137 isl_int shift;
138 int add_one;
139
140 isl_int_init(shift)isl_sioimath_init((shift));
141 isl_int_fdiv_r(shift, bmap->div[div][1 + pos], bmap->div[div][0])isl_sioimath_fdiv_r((shift), *(bmap->div[div][1 + pos]), *
(bmap->div[div][0]))
;
142 isl_int_mul_ui(shift, shift, 2)isl_sioimath_mul_ui((shift), *(shift), 2);
143 add_one = isl_int_gt(shift, bmap->div[div][0])(isl_sioimath_cmp(*(shift), *(bmap->div[div][0])) > 0);
144 isl_int_fdiv_q(shift, bmap->div[div][1 + pos], bmap->div[div][0])isl_sioimath_fdiv_q((shift), *(bmap->div[div][1 + pos]), *
(bmap->div[div][0]))
;
145 if (add_one)
146 isl_int_add_ui(shift, shift, 1)isl_sioimath_add_ui((shift), *(shift), 1);
147 isl_int_neg(shift, shift)isl_sioimath_neg((shift), *(shift));
148 bmap = isl_basic_map_shift_div(bmap, div, pos, shift);
149 isl_int_clear(shift)isl_sioimath_clear((shift));
150
151 return bmap;
152}
153
154/* Does the coefficient of the variable at position "pos"
155 * in integer division "div" need to be reduced?
156 * That is, does it lie outside the half-open interval (1/2,1/2]?
157 * The coefficient c/d lies outside this interval if abs(2 * c) >= d and
158 * 2 * c != d.
159 */
160static isl_bool needs_reduction(__isl_keep isl_basic_map *bmap, int div,
161 int pos)
162{
163 isl_bool r;
164
165 if (isl_int_is_zero(bmap->div[div][1 + pos])(isl_sioimath_sgn(*(bmap->div[div][1 + pos])) == 0))
166 return isl_bool_false;
167
168 isl_int_mul_ui(bmap->div[div][1 + pos], bmap->div[div][1 + pos], 2)isl_sioimath_mul_ui((bmap->div[div][1 + pos]), *(bmap->
div[div][1 + pos]), 2)
;
169 r = isl_int_abs_ge(bmap->div[div][1 + pos], bmap->div[div][0])(isl_sioimath_abs_cmp(*(bmap->div[div][1 + pos]), *(bmap->
div[div][0])) >= 0)
&&
170 !isl_int_eq(bmap->div[div][1 + pos], bmap->div[div][0])(isl_sioimath_cmp(*(bmap->div[div][1 + pos]), *(bmap->div
[div][0])) == 0)
;
171 isl_int_divexact_ui(bmap->div[div][1 + pos],isl_sioimath_tdiv_q_ui((bmap->div[div][1 + pos]), *(bmap->
div[div][1 + pos]), 2)
172 bmap->div[div][1 + pos], 2)isl_sioimath_tdiv_q_ui((bmap->div[div][1 + pos]), *(bmap->
div[div][1 + pos]), 2)
;
173
174 return r;
175}
176
177/* Reduce the coefficients (including the constant term) of
178 * integer division "div", if needed.
179 * In particular, make sure all coefficients lie in
180 * the half-open interval (1/2,1/2].
181 */
182static __isl_give isl_basic_map *reduce_div_coefficients_of_div(
183 __isl_take isl_basic_map *bmap, int div)
184{
185 int i;
186 isl_size total;
187
188 total = isl_basic_map_dim(bmap, isl_dim_all);
189 if (total < 0)
190 return isl_basic_map_free(bmap);
191 for (i = 0; i < 1 + total; ++i) {
192 isl_bool reduce;
193
194 reduce = needs_reduction(bmap, div, i);
195 if (reduce < 0)
196 return isl_basic_map_free(bmap);
197 if (!reduce)
198 continue;
199 bmap = reduce_coefficient_in_div(bmap, div, i);
200 if (!bmap)
201 break;
202 }
203
204 return bmap;
205}
206
207/* Reduce the coefficients (including the constant term) of
208 * the known integer divisions, if needed
209 * In particular, make sure all coefficients lie in
210 * the half-open interval (1/2,1/2].
211 */
212static __isl_give isl_basic_map *reduce_div_coefficients(
213 __isl_take isl_basic_map *bmap)
214{
215 int i;
216
217 if (!bmap)
218 return NULL((void*)0);
219 if (bmap->n_div == 0)
220 return bmap;
221
222 for (i = 0; i < bmap->n_div; ++i) {
223 if (isl_int_is_zero(bmap->div[i][0])(isl_sioimath_sgn(*(bmap->div[i][0])) == 0))
224 continue;
225 bmap = reduce_div_coefficients_of_div(bmap, i);
226 if (!bmap)
227 break;
228 }
229
230 return bmap;
231}
232
233/* Remove any common factor in numerator and denominator of the div expression,
234 * not taking into account the constant term.
235 * That is, if the div is of the form
236 *
237 * floor((a + m f(x))/(m d))
238 *
239 * then replace it by
240 *
241 * floor((floor(a/m) + f(x))/d)
242 *
243 * The difference {a/m}/d in the argument satisfies 0 <= {a/m}/d < 1/d
244 * and can therefore not influence the result of the floor.
245 */
246static __isl_give isl_basic_map *normalize_div_expression(
247 __isl_take isl_basic_map *bmap, int div)
248{
249 isl_size total = isl_basic_map_dim(bmap, isl_dim_all);
250 isl_ctx *ctx = bmap->ctx;
251
252 if (total < 0)
253 return isl_basic_map_free(bmap);
254 if (isl_int_is_zero(bmap->div[div][0])(isl_sioimath_sgn(*(bmap->div[div][0])) == 0))
255 return bmap;
256 isl_seq_gcd(bmap->div[div] + 2, total, &ctx->normalize_gcd);
257 isl_int_gcd(ctx->normalize_gcd, ctx->normalize_gcd, bmap->div[div][0])isl_sioimath_gcd((ctx->normalize_gcd), *(ctx->normalize_gcd
), *(bmap->div[div][0]))
;
258 if (isl_int_is_one(ctx->normalize_gcd)(isl_sioimath_cmp_si(*(ctx->normalize_gcd), 1) == 0))
259 return bmap;
260 isl_int_fdiv_q(bmap->div[div][1], bmap->div[div][1],isl_sioimath_fdiv_q((bmap->div[div][1]), *(bmap->div[div
][1]), *(ctx->normalize_gcd))
261 ctx->normalize_gcd)isl_sioimath_fdiv_q((bmap->div[div][1]), *(bmap->div[div
][1]), *(ctx->normalize_gcd))
;
262 isl_int_divexact(bmap->div[div][0], bmap->div[div][0],isl_sioimath_tdiv_q((bmap->div[div][0]), *(bmap->div[div
][0]), *(ctx->normalize_gcd))
263 ctx->normalize_gcd)isl_sioimath_tdiv_q((bmap->div[div][0]), *(bmap->div[div
][0]), *(ctx->normalize_gcd))
;
264 isl_seq_scale_down(bmap->div[div] + 2, bmap->div[div] + 2,
265 ctx->normalize_gcd, total);
266
267 return bmap;
268}
269
270/* Remove any common factor in numerator and denominator of a div expression,
271 * not taking into account the constant term.
272 * That is, look for any div of the form
273 *
274 * floor((a + m f(x))/(m d))
275 *
276 * and replace it by
277 *
278 * floor((floor(a/m) + f(x))/d)
279 *
280 * The difference {a/m}/d in the argument satisfies 0 <= {a/m}/d < 1/d
281 * and can therefore not influence the result of the floor.
282 */
283static __isl_give isl_basic_map *normalize_div_expressions(
284 __isl_take isl_basic_map *bmap)
285{
286 int i;
287
288 if (!bmap)
289 return NULL((void*)0);
290 if (bmap->n_div == 0)
291 return bmap;
292
293 for (i = 0; i < bmap->n_div; ++i)
294 bmap = normalize_div_expression(bmap, i);
295
296 return bmap;
297}
298
299/* Assumes divs have been ordered if keep_divs is set.
300 */
301static __isl_give isl_basic_map *eliminate_var_using_equality(
302 __isl_take isl_basic_map *bmap,
303 unsigned pos, isl_int *eq, int keep_divs, int *progress)
304{
305 isl_size total;
306 isl_size v_div;
307 int k;
308 int last_div;
309
310 total = isl_basic_map_dim(bmap, isl_dim_all);
311 v_div = isl_basic_map_var_offset(bmap, isl_dim_div);
312 if (total < 0 || v_div < 0)
313 return isl_basic_map_free(bmap);
314 last_div = isl_seq_last_non_zero(eq + 1 + v_div, bmap->n_div);
315 for (k = 0; k < bmap->n_eq; ++k) {
316 if (bmap->eq[k] == eq)
317 continue;
318 if (isl_int_is_zero(bmap->eq[k][1+pos])(isl_sioimath_sgn(*(bmap->eq[k][1+pos])) == 0))
319 continue;
320 if (progress)
321 *progress = 1;
322 isl_seq_elim(bmap->eq[k], eq, 1+pos, 1+total, NULL((void*)0));
323 isl_seq_normalize(bmap->ctx, bmap->eq[k], 1 + total);
324 }
325
326 for (k = 0; k < bmap->n_ineq; ++k) {
327 if (isl_int_is_zero(bmap->ineq[k][1+pos])(isl_sioimath_sgn(*(bmap->ineq[k][1+pos])) == 0))
328 continue;
329 if (progress)
330 *progress = 1;
331 isl_seq_elim(bmap->ineq[k], eq, 1+pos, 1+total, NULL((void*)0));
332 isl_seq_normalize(bmap->ctx, bmap->ineq[k], 1 + total);
333 ISL_F_CLR(bmap, ISL_BASIC_MAP_NO_REDUNDANT)(((bmap)->flags) &= ~((1 << 3)));
334 ISL_F_CLR(bmap, ISL_BASIC_MAP_SORTED)(((bmap)->flags) &= ~((1 << 5)));
335 }
336
337 for (k = 0; k < bmap->n_div; ++k) {
338 if (isl_int_is_zero(bmap->div[k][0])(isl_sioimath_sgn(*(bmap->div[k][0])) == 0))
339 continue;
340 if (isl_int_is_zero(bmap->div[k][1+1+pos])(isl_sioimath_sgn(*(bmap->div[k][1+1+pos])) == 0))
341 continue;
342 if (progress)
343 *progress = 1;
344 /* We need to be careful about circular definitions,
345 * so for now we just remove the definition of div k
346 * if the equality contains any divs.
347 * If keep_divs is set, then the divs have been ordered
348 * and we can keep the definition as long as the result
349 * is still ordered.
350 */
351 if (last_div == -1 || (keep_divs && last_div < k)) {
352 isl_seq_elim(bmap->div[k]+1, eq,
353 1+pos, 1+total, &bmap->div[k][0]);
354 bmap = normalize_div_expression(bmap, k);
355 if (!bmap)
356 return NULL((void*)0);
357 } else
358 isl_seq_clr(bmap->div[k], 1 + total);
359 }
360
361 return bmap;
362}
363
364/* Assumes divs have been ordered if keep_divs is set.
365 */
366static __isl_give isl_basic_map *eliminate_div(__isl_take isl_basic_map *bmap,
367 isl_int *eq, unsigned div, int keep_divs)
368{
369 isl_size v_div;
370 unsigned pos;
371
372 v_div = isl_basic_map_var_offset(bmap, isl_dim_div);
373 if (v_div < 0)
374 return isl_basic_map_free(bmap);
375 pos = v_div + div;
376 bmap = eliminate_var_using_equality(bmap, pos, eq, keep_divs, NULL((void*)0));
377
378 bmap = isl_basic_map_drop_div(bmap, div);
379
380 return bmap;
381}
382
383/* Check if elimination of div "div" using equality "eq" would not
384 * result in a div depending on a later div.
385 */
386static isl_bool ok_to_eliminate_div(__isl_keep isl_basic_map *bmap, isl_int *eq,
387 unsigned div)
388{
389 int k;
390 int last_div;
391 isl_size v_div;
392 unsigned pos;
393
394 v_div = isl_basic_map_var_offset(bmap, isl_dim_div);
395 if (v_div < 0)
396 return isl_bool_error;
397 pos = v_div + div;
398
399 last_div = isl_seq_last_non_zero(eq + 1 + v_div, bmap->n_div);
400 if (last_div < 0 || last_div <= div)
401 return isl_bool_true;
402
403 for (k = 0; k <= last_div; ++k) {
404 if (isl_int_is_zero(bmap->div[k][0])(isl_sioimath_sgn(*(bmap->div[k][0])) == 0))
405 continue;
406 if (!isl_int_is_zero(bmap->div[k][1 + 1 + pos])(isl_sioimath_sgn(*(bmap->div[k][1 + 1 + pos])) == 0))
407 return isl_bool_false;
408 }
409
410 return isl_bool_true;
411}
412
413/* Eliminate divs based on equalities
414 */
415static __isl_give isl_basic_map *eliminate_divs_eq(
416 __isl_take isl_basic_map *bmap, int *progress)
417{
418 int d;
419 int i;
420 int modified = 0;
421 unsigned off;
422
423 bmap = isl_basic_map_order_divs(bmap);
424
425 if (!bmap)
426 return NULL((void*)0);
427
428 off = isl_basic_map_offset(bmap, isl_dim_div);
429
430 for (d = bmap->n_div - 1; d >= 0 ; --d) {
431 for (i = 0; i < bmap->n_eq; ++i) {
432 isl_bool ok;
433
434 if (!isl_int_is_one(bmap->eq[i][off + d])(isl_sioimath_cmp_si(*(bmap->eq[i][off + d]), 1) == 0) &&
435 !isl_int_is_negone(bmap->eq[i][off + d])(isl_sioimath_cmp_si(*(bmap->eq[i][off + d]), -1) == 0))
436 continue;
437 ok = ok_to_eliminate_div(bmap, bmap->eq[i], d);
438 if (ok < 0)
439 return isl_basic_map_free(bmap);
440 if (!ok)
441 continue;
442 modified = 1;
443 *progress = 1;
444 bmap = eliminate_div(bmap, bmap->eq[i], d, 1);
445 if (isl_basic_map_drop_equality(bmap, i) < 0)
446 return isl_basic_map_free(bmap);
447 break;
448 }
449 }
450 if (modified)
451 return eliminate_divs_eq(bmap, progress);
452 return bmap;
453}
454
455/* Eliminate divs based on inequalities
456 */
457static __isl_give isl_basic_map *eliminate_divs_ineq(
458 __isl_take isl_basic_map *bmap, int *progress)
459{
460 int d;
461 int i;
462 unsigned off;
463 struct isl_ctx *ctx;
464
465 if (!bmap)
466 return NULL((void*)0);
467
468 ctx = bmap->ctx;
469 off = isl_basic_map_offset(bmap, isl_dim_div);
470
471 for (d = bmap->n_div - 1; d >= 0 ; --d) {
472 for (i = 0; i < bmap->n_eq; ++i)
473 if (!isl_int_is_zero(bmap->eq[i][off + d])(isl_sioimath_sgn(*(bmap->eq[i][off + d])) == 0))
474 break;
475 if (i < bmap->n_eq)
476 continue;
477 for (i = 0; i < bmap->n_ineq; ++i)
478 if (isl_int_abs_gt(bmap->ineq[i][off + d], ctx->one)(isl_sioimath_abs_cmp(*(bmap->ineq[i][off + d]), *(ctx->
one)) > 0)
)
479 break;
480 if (i < bmap->n_ineq)
481 continue;
482 *progress = 1;
483 bmap = isl_basic_map_eliminate_vars(bmap, (off-1)+d, 1);
484 if (!bmap || ISL_F_ISSET(bmap, ISL_BASIC_MAP_EMPTY)(!!(((bmap)->flags) & ((1 << 1)))))
485 break;
486 bmap = isl_basic_map_drop_div(bmap, d);
487 if (!bmap)
488 break;
489 }
490 return bmap;
491}
492
493/* Does the equality constraint at position "eq" in "bmap" involve
494 * any local variables in the range [first, first + n)
495 * that are not marked as having an explicit representation?
496 */
497static isl_bool bmap_eq_involves_unknown_divs(__isl_keep isl_basic_map *bmap,
498 int eq, unsigned first, unsigned n)
499{
500 unsigned o_div;
501 int i;
502
503 if (!bmap)
504 return isl_bool_error;
505
506 o_div = isl_basic_map_offset(bmap, isl_dim_div);
507 for (i = 0; i < n; ++i) {
508 isl_bool unknown;
509
510 if (isl_int_is_zero(bmap->eq[eq][o_div + first + i])(isl_sioimath_sgn(*(bmap->eq[eq][o_div + first + i])) == 0
)
)
511 continue;
512 unknown = isl_basic_map_div_is_marked_unknown(bmap, first + i);
513 if (unknown < 0)
514 return isl_bool_error;
515 if (unknown)
516 return isl_bool_true;
517 }
518
519 return isl_bool_false;
520}
521
522/* The last local variable involved in the equality constraint
523 * at position "eq" in "bmap" is the local variable at position "div".
524 * It can therefore be used to extract an explicit representation
525 * for that variable.
526 * Do so unless the local variable already has an explicit representation or
527 * the explicit representation would involve any other local variables
528 * that in turn do not have an explicit representation.
529 * An equality constraint involving local variables without an explicit
530 * representation can be used in isl_basic_map_drop_redundant_divs
531 * to separate out an independent local variable. Introducing
532 * an explicit representation here would block this transformation,
533 * while the partial explicit representation in itself is not very useful.
534 * Set *progress if anything is changed.
535 *
536 * The equality constraint is of the form
537 *
538 * f(x) + n e >= 0
539 *
540 * with n a positive number. The explicit representation derived from
541 * this constraint is
542 *
543 * floor((-f(x))/n)
544 */
545static __isl_give isl_basic_map *set_div_from_eq(__isl_take isl_basic_map *bmap,
546 int div, int eq, int *progress)
547{
548 isl_size total;
549 unsigned o_div;
550 isl_bool involves;
551
552 if (!bmap)
553 return NULL((void*)0);
554
555 if (!isl_int_is_zero(bmap->div[div][0])(isl_sioimath_sgn(*(bmap->div[div][0])) == 0))
556 return bmap;
557
558 involves = bmap_eq_involves_unknown_divs(bmap, eq, 0, div);
559 if (involves < 0)
560 return isl_basic_map_free(bmap);
561 if (involves)
562 return bmap;
563
564 total = isl_basic_map_dim(bmap, isl_dim_all);
565 if (total < 0)
566 return isl_basic_map_free(bmap);
567 o_div = isl_basic_map_offset(bmap, isl_dim_div);
568 isl_seq_neg(bmap->div[div] + 1, bmap->eq[eq], 1 + total);
569 isl_int_set_si(bmap->div[div][1 + o_div + div], 0)isl_sioimath_set_si((bmap->div[div][1 + o_div + div]), 0);
570 isl_int_set(bmap->div[div][0], bmap->eq[eq][o_div + div])isl_sioimath_set((bmap->div[div][0]), *(bmap->eq[eq][o_div
+ div]))
;
571 if (progress)
572 *progress = 1;
573
574 return bmap;
575}
576
577/* Perform fangcheng (Gaussian elimination) on the equality
578 * constraints of "bmap".
579 * That is, put them into row-echelon form, starting from the last column
580 * backward and use them to eliminate the corresponding coefficients
581 * from all constraints.
582 *
583 * If "progress" is not NULL, then it gets set if the elimination
584 * result in any changes.
585 * The elimination process may result in some equality constraints
586 * getting interchanged or removed.
587 * If "swap" or "drop" are not NULL, then they get called when
588 * two equality constraints get interchanged or
589 * when a number of final equality constraints get removed.
590 * As a special case, if the input turns out to be empty,
591 * then drop gets called with the number of removed equality
592 * constraints set to the total number of equality constraints.
593 * If "swap" or "drop" are not NULL, then the local variables (if any)
594 * are assumed to be in a valid order.
595 */
596__isl_give isl_basic_map *isl_basic_map_gauss5(__isl_take isl_basic_map *bmap,
597 int *progress,
598 isl_stat (*swap)(unsigned a, unsigned b, void *user),
599 isl_stat (*drop)(unsigned n, void *user), void *user)
600{
601 int k;
602 int done;
603 int last_var;
604 unsigned total_var;
605 isl_size total;
606 unsigned n_drop;
607
608 if (!swap && !drop)
609 bmap = isl_basic_map_order_divs(bmap);
610
611 total = isl_basic_map_dim(bmap, isl_dim_all);
612 if (total < 0)
613 return isl_basic_map_free(bmap);
614
615 total_var = total - bmap->n_div;
616
617 last_var = total - 1;
618 for (done = 0; done < bmap->n_eq; ++done) {
619 for (; last_var >= 0; --last_var) {
620 for (k = done; k < bmap->n_eq; ++k)
621 if (!isl_int_is_zero(bmap->eq[k][1+last_var])(isl_sioimath_sgn(*(bmap->eq[k][1+last_var])) == 0))
622 break;
623 if (k < bmap->n_eq)
624 break;
625 }
626 if (last_var < 0)
627 break;
628 if (k != done) {
629 swap_equality(bmap, k, done);
630 if (swap && swap(k, done, user) < 0)
631 return isl_basic_map_free(bmap);
632 }
633 if (isl_int_is_neg(bmap->eq[done][1+last_var])(isl_sioimath_sgn(*(bmap->eq[done][1+last_var])) < 0))
634 isl_seq_neg(bmap->eq[done], bmap->eq[done], 1+total);
635
636 bmap = eliminate_var_using_equality(bmap, last_var,
637 bmap->eq[done], 1, progress);
638
639 if (last_var >= total_var)
640 bmap = set_div_from_eq(bmap, last_var - total_var,
641 done, progress);
642 if (!bmap)
643 return NULL((void*)0);
644 }
645 if (done == bmap->n_eq)
646 return bmap;
647 for (k = done; k < bmap->n_eq; ++k) {
648 if (isl_int_is_zero(bmap->eq[k][0])(isl_sioimath_sgn(*(bmap->eq[k][0])) == 0))
649 continue;
650 if (drop && drop(bmap->n_eq, user) < 0)
651 return isl_basic_map_free(bmap);
652 return isl_basic_map_set_to_empty(bmap);
653 }
654 n_drop = bmap->n_eq - done;
655 bmap = isl_basic_map_free_equality(bmap, n_drop);
656 if (drop && drop(n_drop, user) < 0)
657 return isl_basic_map_free(bmap);
658 return bmap;
659}
660
661__isl_give isl_basic_map *isl_basic_map_gauss(__isl_take isl_basic_map *bmap,
662 int *progress)
663{
664 return isl_basic_map_gauss5(bmap, progress, NULL((void*)0), NULL((void*)0), NULL((void*)0));
665}
666
667__isl_give isl_basic_setisl_basic_map *isl_basic_set_gauss(
668 __isl_take isl_basic_setisl_basic_map *bset, int *progress)
669{
670 return bset_from_bmap(isl_basic_map_gauss(bset_to_bmap(bset),
671 progress));
672}
673
674
675static unsigned int round_up(unsigned int v)
676{
677 int old_v = v;
678
679 while (v) {
680 old_v = v;
681 v ^= v & -v;
682 }
683 return old_v << 1;
684}
685
686/* Hash table of inequalities in a basic map.
687 * "index" is an array of addresses of inequalities in the basic map, some
688 * of which are NULL. The inequalities are hashed on the coefficients
689 * except the constant term.
690 * "size" is the number of elements in the array and is always a power of two
691 * "bits" is the number of bits need to represent an index into the array.
692 * "total" is the total dimension of the basic map.
693 */
694struct isl_constraint_index {
695 unsigned int size;
696 int bits;
697 isl_int ***index;
698 isl_size total;
699};
700
701/* Fill in the "ci" data structure for holding the inequalities of "bmap".
702 */
703static isl_stat create_constraint_index(struct isl_constraint_index *ci,
704 __isl_keep isl_basic_map *bmap)
705{
706 isl_ctx *ctx;
707
708 ci->index = NULL((void*)0);
709 if (!bmap
38.1
'bmap' is non-null
)
39
Taking false branch
710 return isl_stat_error;
711 ci->total = isl_basic_map_dim(bmap, isl_dim_all);
712 if (ci->total < 0)
40
Assuming field 'total' is >= 0
41
Taking false branch
713 return isl_stat_error;
714 if (bmap->n_ineq == 0)
42
Assuming field 'n_ineq' is equal to 0
43
Taking true branch
715 return isl_stat_ok;
44
Returning without writing to 'ci->bits'
716 ci->size = round_up(4 * (bmap->n_ineq + 1) / 3 - 1);
717 ci->bits = ffs(ci->size) - 1;
718 ctx = isl_basic_map_get_ctx(bmap);
719 ci->index = isl_calloc_array(ctx, isl_int **, ci->size)((isl_int ** *)isl_calloc_or_die(ctx, ci->size, sizeof(isl_int
**)))
;
720 if (!ci->index)
721 return isl_stat_error;
722
723 return isl_stat_ok;
724}
725
726/* Free the memory allocated by create_constraint_index.
727 */
728static void constraint_index_free(struct isl_constraint_index *ci)
729{
730 free(ci->index);
731}
732
733/* Return the position in ci->index that contains the address of
734 * an inequality that is equal to *ineq up to the constant term,
735 * provided this address is not identical to "ineq".
736 * If there is no such inequality, then return the position where
737 * such an inequality should be inserted.
738 */
739static int hash_index_ineq(struct isl_constraint_index *ci, isl_int **ineq)
740{
741 int h;
742 uint32_t hash = isl_seq_get_hash_bits((*ineq) + 1, ci->total, ci->bits);
743 for (h = hash; ci->index[h]; h = (h+1) % ci->size)
744 if (ineq != ci->index[h] &&
745 isl_seq_eq((*ineq) + 1, ci->index[h][0]+1, ci->total))
746 break;
747 return h;
748}
749
750/* Return the position in ci->index that contains the address of
751 * an inequality that is equal to the k'th inequality of "bmap"
752 * up to the constant term, provided it does not point to the very
753 * same inequality.
754 * If there is no such inequality, then return the position where
755 * such an inequality should be inserted.
756 */
757static int hash_index(struct isl_constraint_index *ci,
758 __isl_keep isl_basic_map *bmap, int k)
759{
760 return hash_index_ineq(ci, &bmap->ineq[k]);
761}
762
763static int set_hash_index(struct isl_constraint_index *ci,
764 __isl_keep isl_basic_setisl_basic_map *bset, int k)
765{
766 return hash_index(ci, bset, k);
767}
768
769/* Fill in the "ci" data structure with the inequalities of "bset".
770 */
771static isl_stat setup_constraint_index(struct isl_constraint_index *ci,
772 __isl_keep isl_basic_setisl_basic_map *bset)
773{
774 int k, h;
775
776 if (create_constraint_index(ci, bset) < 0)
777 return isl_stat_error;
778
779 for (k = 0; k < bset->n_ineq; ++k) {
780 h = set_hash_index(ci, bset, k);
781 ci->index[h] = &bset->ineq[k];
782 }
783
784 return isl_stat_ok;
785}
786
787/* Is the inequality ineq (obviously) redundant with respect
788 * to the constraints in "ci"?
789 *
790 * Look for an inequality in "ci" with the same coefficients and then
791 * check if the contant term of "ineq" is greater than or equal
792 * to the constant term of that inequality. If so, "ineq" is clearly
793 * redundant.
794 *
795 * Note that hash_index_ineq ignores a stored constraint if it has
796 * the same address as the passed inequality. It is ok to pass
797 * the address of a local variable here since it will never be
798 * the same as the address of a constraint in "ci".
799 */
800static isl_bool constraint_index_is_redundant(struct isl_constraint_index *ci,
801 isl_int *ineq)
802{
803 int h;
804
805 h = hash_index_ineq(ci, &ineq);
806 if (!ci->index[h])
807 return isl_bool_false;
808 return isl_int_ge(ineq[0], (*ci->index[h])[0])(isl_sioimath_cmp(*(ineq[0]), *((*ci->index[h])[0])) >=
0)
;
809}
810
811/* If we can eliminate more than one div, then we need to make
812 * sure we do it from last div to first div, in order not to
813 * change the position of the other divs that still need to
814 * be removed.
815 */
816static __isl_give isl_basic_map *remove_duplicate_divs(
817 __isl_take isl_basic_map *bmap, int *progress)
818{
819 unsigned int size;
820 int *index;
821 int *elim_for;
822 int k, l, h;
823 int bits;
824 struct isl_blk eq;
825 isl_size v_div;
826 unsigned total;
827 struct isl_ctx *ctx;
828
829 bmap = isl_basic_map_order_divs(bmap);
830 if (!bmap || bmap->n_div <= 1)
831 return bmap;
832
833 v_div = isl_basic_map_var_offset(bmap, isl_dim_div);
834 if (v_div < 0)
835 return isl_basic_map_free(bmap);
836 total = v_div + bmap->n_div;
837
838 ctx = bmap->ctx;
839 for (k = bmap->n_div - 1; k >= 0; --k)
840 if (!isl_int_is_zero(bmap->div[k][0])(isl_sioimath_sgn(*(bmap->div[k][0])) == 0))
841 break;
842 if (k <= 0)
843 return bmap;
844
845 size = round_up(4 * bmap->n_div / 3 - 1);
846 if (size == 0)
847 return bmap;
848 elim_for = isl_calloc_array(ctx, int, bmap->n_div)((int *)isl_calloc_or_die(ctx, bmap->n_div, sizeof(int)));
849 bits = ffs(size) - 1;
850 index = isl_calloc_array(ctx, int, size)((int *)isl_calloc_or_die(ctx, size, sizeof(int)));
851 if (!elim_for || !index)
852 goto out;
853 eq = isl_blk_alloc(ctx, 1+total);
854 if (isl_blk_is_error(eq))
855 goto out;
856
857 isl_seq_clr(eq.data, 1+total);
858 index[isl_seq_get_hash_bits(bmap->div[k], 2+total, bits)] = k + 1;
859 for (--k; k >= 0; --k) {
860 uint32_t hash;
861
862 if (isl_int_is_zero(bmap->div[k][0])(isl_sioimath_sgn(*(bmap->div[k][0])) == 0))
863 continue;
864
865 hash = isl_seq_get_hash_bits(bmap->div[k], 2+total, bits);
866 for (h = hash; index[h]; h = (h+1) % size)
867 if (isl_seq_eq(bmap->div[k],
868 bmap->div[index[h]-1], 2+total))
869 break;
870 if (index[h]) {
871 *progress = 1;
872 l = index[h] - 1;
873 elim_for[l] = k + 1;
874 }
875 index[h] = k+1;
876 }
877 for (l = bmap->n_div - 1; l >= 0; --l) {
878 if (!elim_for[l])
879 continue;
880 k = elim_for[l] - 1;
881 isl_int_set_si(eq.data[1 + v_div + k], -1)isl_sioimath_set_si((eq.data[1 + v_div + k]), -1);
882 isl_int_set_si(eq.data[1 + v_div + l], 1)isl_sioimath_set_si((eq.data[1 + v_div + l]), 1);
883 bmap = eliminate_div(bmap, eq.data, l, 1);
884 if (!bmap)
885 break;
886 isl_int_set_si(eq.data[1 + v_div + k], 0)isl_sioimath_set_si((eq.data[1 + v_div + k]), 0);
887 isl_int_set_si(eq.data[1 + v_div + l], 0)isl_sioimath_set_si((eq.data[1 + v_div + l]), 0);
888 }
889
890 isl_blk_free(ctx, eq);
891out:
892 free(index);
893 free(elim_for);
894 return bmap;
895}
896
897static int n_pure_div_eq(struct isl_basic_map *bmap)
898{
899 int i, j;
900 isl_size v_div;
901
902 v_div = isl_basic_map_var_offset(bmap, isl_dim_div);
903 if (v_div < 0)
904 return -1;
905 for (i = 0, j = bmap->n_div-1; i < bmap->n_eq; ++i) {
906 while (j >= 0 && isl_int_is_zero(bmap->eq[i][1 + v_div + j])(isl_sioimath_sgn(*(bmap->eq[i][1 + v_div + j])) == 0))
907 --j;
908 if (j < 0)
909 break;
910 if (isl_seq_first_non_zero(bmap->eq[i] + 1 + v_div, j) != -1)
911 return 0;
912 }
913 return i;
914}
915
916/* Normalize divs that appear in equalities.
917 *
918 * In particular, we assume that bmap contains some equalities
919 * of the form
920 *
921 * a x = m * e_i
922 *
923 * and we want to replace the set of e_i by a minimal set and
924 * such that the new e_i have a canonical representation in terms
925 * of the vector x.
926 * If any of the equalities involves more than one divs, then
927 * we currently simply bail out.
928 *
929 * Let us first additionally assume that all equalities involve
930 * a div. The equalities then express modulo constraints on the
931 * remaining variables and we can use "parameter compression"
932 * to find a minimal set of constraints. The result is a transformation
933 *
934 * x = T(x') = x_0 + G x'
935 *
936 * with G a lower-triangular matrix with all elements below the diagonal
937 * non-negative and smaller than the diagonal element on the same row.
938 * We first normalize x_0 by making the same property hold in the affine
939 * T matrix.
940 * The rows i of G with a 1 on the diagonal do not impose any modulo
941 * constraint and simply express x_i = x'_i.
942 * For each of the remaining rows i, we introduce a div and a corresponding
943 * equality. In particular
944 *
945 * g_ii e_j = x_i - g_i(x')
946 *
947 * where each x'_k is replaced either by x_k (if g_kk = 1) or the
948 * corresponding div (if g_kk != 1).
949 *
950 * If there are any equalities not involving any div, then we
951 * first apply a variable compression on the variables x:
952 *
953 * x = C x'' x'' = C_2 x
954 *
955 * and perform the above parameter compression on A C instead of on A.
956 * The resulting compression is then of the form
957 *
958 * x'' = T(x') = x_0 + G x'
959 *
960 * and in constructing the new divs and the corresponding equalities,
961 * we have to replace each x'', i.e., the x'_k with (g_kk = 1),
962 * by the corresponding row from C_2.
963 */
964static __isl_give isl_basic_map *normalize_divs(__isl_take isl_basic_map *bmap,
965 int *progress)
966{
967 int i, j, k;
968 isl_size v_div;
969 int div_eq;
970 struct isl_mat *B;
971 struct isl_vec *d;
972 struct isl_mat *T = NULL((void*)0);
973 struct isl_mat *C = NULL((void*)0);
974 struct isl_mat *C2 = NULL((void*)0);
975 isl_int v;
976 int *pos = NULL((void*)0);
977 int dropped, needed;
978
979 if (!bmap)
980 return NULL((void*)0);
981
982 if (bmap->n_div == 0)
983 return bmap;
984
985 if (bmap->n_eq == 0)
986 return bmap;
987
988 if (ISL_F_ISSET(bmap, ISL_BASIC_MAP_NORMALIZED_DIVS)(!!(((bmap)->flags) & ((1 << 6)))))
989 return bmap;
990
991 v_div = isl_basic_map_var_offset(bmap, isl_dim_div);
992 div_eq = n_pure_div_eq(bmap);
993 if (v_div < 0 || div_eq < 0)
994 return isl_basic_map_free(bmap);
995 if (div_eq == 0)
996 return bmap;
997
998 if (div_eq < bmap->n_eq) {
999 B = isl_mat_sub_alloc6(bmap->ctx, bmap->eq, div_eq,
1000 bmap->n_eq - div_eq, 0, 1 + v_div);
1001 C = isl_mat_variable_compression(B, &C2);
1002 if (!C || !C2)
1003 goto error;
1004 if (C->n_col == 0) {
1005 bmap = isl_basic_map_set_to_empty(bmap);
1006 isl_mat_free(C);
1007 isl_mat_free(C2);
1008 goto done;
1009 }
1010 }
1011
1012 d = isl_vec_alloc(bmap->ctx, div_eq);
1013 if (!d)
1014 goto error;
1015 for (i = 0, j = bmap->n_div-1; i < div_eq; ++i) {
1016 while (j >= 0 && isl_int_is_zero(bmap->eq[i][1 + v_div + j])(isl_sioimath_sgn(*(bmap->eq[i][1 + v_div + j])) == 0))
1017 --j;
1018 isl_int_set(d->block.data[i], bmap->eq[i][1 + v_div + j])isl_sioimath_set((d->block.data[i]), *(bmap->eq[i][1 + v_div
+ j]))
;
1019 }
1020 B = isl_mat_sub_alloc6(bmap->ctx, bmap->eq, 0, div_eq, 0, 1 + v_div);
1021
1022 if (C) {
1023 B = isl_mat_product(B, C);
1024 C = NULL((void*)0);
1025 }
1026
1027 T = isl_mat_parameter_compression(B, d);
1028 if (!T)
1029 goto error;
1030 if (T->n_col == 0) {
1031 bmap = isl_basic_map_set_to_empty(bmap);
1032 isl_mat_free(C2);
1033 isl_mat_free(T);
1034 goto done;
1035 }
1036 isl_int_init(v)isl_sioimath_init((v));
1037 for (i = 0; i < T->n_row - 1; ++i) {
1038 isl_int_fdiv_q(v, T->row[1 + i][0], T->row[1 + i][1 + i])isl_sioimath_fdiv_q((v), *(T->row[1 + i][0]), *(T->row[
1 + i][1 + i]))
;
1039 if (isl_int_is_zero(v)(isl_sioimath_sgn(*(v)) == 0))
1040 continue;
1041 isl_mat_col_submul(T, 0, v, 1 + i);
1042 }
1043 isl_int_clear(v)isl_sioimath_clear((v));
1044 pos = isl_alloc_array(bmap->ctx, int, T->n_row)((int *)isl_malloc_or_die(bmap->ctx, (T->n_row)*sizeof(
int)))
;
1045 if (!pos)
1046 goto error;
1047 /* We have to be careful because dropping equalities may reorder them */
1048 dropped = 0;
1049 for (j = bmap->n_div - 1; j >= 0; --j) {
1050 for (i = 0; i < bmap->n_eq; ++i)
1051 if (!isl_int_is_zero(bmap->eq[i][1 + v_div + j])(isl_sioimath_sgn(*(bmap->eq[i][1 + v_div + j])) == 0))
1052 break;
1053 if (i < bmap->n_eq) {
1054 bmap = isl_basic_map_drop_div(bmap, j);
1055 if (isl_basic_map_drop_equality(bmap, i) < 0)
1056 goto error;
1057 ++dropped;
1058 }
1059 }
1060 pos[0] = 0;
1061 needed = 0;
1062 for (i = 1; i < T->n_row; ++i) {
1063 if (isl_int_is_one(T->row[i][i])(isl_sioimath_cmp_si(*(T->row[i][i]), 1) == 0))
1064 pos[i] = i;
1065 else
1066 needed++;
1067 }
1068 if (needed > dropped) {
1069 bmap = isl_basic_map_extend(bmap, needed, needed, 0);
1070 if (!bmap)
1071 goto error;
1072 }
1073 for (i = 1; i < T->n_row; ++i) {
1074 if (isl_int_is_one(T->row[i][i])(isl_sioimath_cmp_si(*(T->row[i][i]), 1) == 0))
1075 continue;
1076 k = isl_basic_map_alloc_div(bmap);
1077 pos[i] = 1 + v_div + k;
1078 isl_seq_clr(bmap->div[k] + 1, 1 + v_div + bmap->n_div);
1079 isl_int_set(bmap->div[k][0], T->row[i][i])isl_sioimath_set((bmap->div[k][0]), *(T->row[i][i]));
1080 if (C2)
1081 isl_seq_cpy(bmap->div[k] + 1, C2->row[i], 1 + v_div);
1082 else
1083 isl_int_set_si(bmap->div[k][1 + i], 1)isl_sioimath_set_si((bmap->div[k][1 + i]), 1);
1084 for (j = 0; j < i; ++j) {
1085 if (isl_int_is_zero(T->row[i][j])(isl_sioimath_sgn(*(T->row[i][j])) == 0))
1086 continue;
1087 if (pos[j] < T->n_row && C2)
1088 isl_seq_submul(bmap->div[k] + 1, T->row[i][j],
1089 C2->row[pos[j]], 1 + v_div);
1090 else
1091 isl_int_neg(bmap->div[k][1 + pos[j]],isl_sioimath_neg((bmap->div[k][1 + pos[j]]), *(T->row[i
][j]))
1092 T->row[i][j])isl_sioimath_neg((bmap->div[k][1 + pos[j]]), *(T->row[i
][j]))
;
1093 }
1094 j = isl_basic_map_alloc_equality(bmap);
1095 isl_seq_neg(bmap->eq[j], bmap->div[k]+1, 1+v_div+bmap->n_div);
1096 isl_int_set(bmap->eq[j][pos[i]], bmap->div[k][0])isl_sioimath_set((bmap->eq[j][pos[i]]), *(bmap->div[k][
0]))
;
1097 }
1098 free(pos);
1099 isl_mat_free(C2);
1100 isl_mat_free(T);
1101
1102 if (progress)
1103 *progress = 1;
1104done:
1105 ISL_F_SET(bmap, ISL_BASIC_MAP_NORMALIZED_DIVS)(((bmap)->flags) |= ((1 << 6)));
1106
1107 return bmap;
1108error:
1109 free(pos);
1110 isl_mat_free(C);
1111 isl_mat_free(C2);
1112 isl_mat_free(T);
1113 isl_basic_map_free(bmap);
1114 return NULL((void*)0);
1115}
1116
1117static __isl_give isl_basic_map *set_div_from_lower_bound(
1118 __isl_take isl_basic_map *bmap, int div, int ineq)
1119{
1120 unsigned total = isl_basic_map_offset(bmap, isl_dim_div);
1121
1122 isl_seq_neg(bmap->div[div] + 1, bmap->ineq[ineq], total + bmap->n_div);
1123 isl_int_set(bmap->div[div][0], bmap->ineq[ineq][total + div])isl_sioimath_set((bmap->div[div][0]), *(bmap->ineq[ineq
][total + div]))
;
1124 isl_int_add(bmap->div[div][1], bmap->div[div][1], bmap->div[div][0])isl_sioimath_add((bmap->div[div][1]), *(bmap->div[div][
1]), *(bmap->div[div][0]))
;
1125 isl_int_sub_ui(bmap->div[div][1], bmap->div[div][1], 1)isl_sioimath_sub_ui((bmap->div[div][1]), *(bmap->div[div
][1]), 1)
;
1126 isl_int_set_si(bmap->div[div][1 + total + div], 0)isl_sioimath_set_si((bmap->div[div][1 + total + div]), 0);
1127
1128 return bmap;
1129}
1130
1131/* Check whether it is ok to define a div based on an inequality.
1132 * To avoid the introduction of circular definitions of divs, we
1133 * do not allow such a definition if the resulting expression would refer to
1134 * any other undefined divs or if any known div is defined in
1135 * terms of the unknown div.
1136 */
1137static isl_bool ok_to_set_div_from_bound(__isl_keep isl_basic_map *bmap,
1138 int div, int ineq)
1139{
1140 int j;
1141 unsigned total = isl_basic_map_offset(bmap, isl_dim_div);
1142
1143 /* Not defined in terms of unknown divs */
1144 for (j = 0; j < bmap->n_div; ++j) {
1145 if (div == j)
1146 continue;
1147 if (isl_int_is_zero(bmap->ineq[ineq][total + j])(isl_sioimath_sgn(*(bmap->ineq[ineq][total + j])) == 0))
1148 continue;
1149 if (isl_int_is_zero(bmap->div[j][0])(isl_sioimath_sgn(*(bmap->div[j][0])) == 0))
1150 return isl_bool_false;
1151 }
1152
1153 /* No other div defined in terms of this one => avoid loops */
1154 for (j = 0; j < bmap->n_div; ++j) {
1155 if (div == j)
1156 continue;
1157 if (isl_int_is_zero(bmap->div[j][0])(isl_sioimath_sgn(*(bmap->div[j][0])) == 0))
1158 continue;
1159 if (!isl_int_is_zero(bmap->div[j][1 + total + div])(isl_sioimath_sgn(*(bmap->div[j][1 + total + div])) == 0))
1160 return isl_bool_false;
1161 }
1162
1163 return isl_bool_true;
1164}
1165
1166/* Would an expression for div "div" based on inequality "ineq" of "bmap"
1167 * be a better expression than the current one?
1168 *
1169 * If we do not have any expression yet, then any expression would be better.
1170 * Otherwise we check if the last variable involved in the inequality
1171 * (disregarding the div that it would define) is in an earlier position
1172 * than the last variable involved in the current div expression.
1173 */
1174static isl_bool better_div_constraint(__isl_keep isl_basic_map *bmap,
1175 int div, int ineq)
1176{
1177 unsigned total = isl_basic_map_offset(bmap, isl_dim_div);
1178 int last_div;
1179 int last_ineq;
1180
1181 if (isl_int_is_zero(bmap->div[div][0])(isl_sioimath_sgn(*(bmap->div[div][0])) == 0))
1182 return isl_bool_true;
1183
1184 if (isl_seq_last_non_zero(bmap->ineq[ineq] + total + div + 1,
1185 bmap->n_div - (div + 1)) >= 0)
1186 return isl_bool_false;
1187
1188 last_ineq = isl_seq_last_non_zero(bmap->ineq[ineq], total + div);
1189 last_div = isl_seq_last_non_zero(bmap->div[div] + 1,
1190 total + bmap->n_div);
1191
1192 return last_ineq < last_div;
1193}
1194
1195/* Given two constraints "k" and "l" that are opposite to each other,
1196 * except for the constant term, check if we can use them
1197 * to obtain an expression for one of the hitherto unknown divs or
1198 * a "better" expression for a div for which we already have an expression.
1199 * "sum" is the sum of the constant terms of the constraints.
1200 * If this sum is strictly smaller than the coefficient of one
1201 * of the divs, then this pair can be used define the div.
1202 * To avoid the introduction of circular definitions of divs, we
1203 * do not use the pair if the resulting expression would refer to
1204 * any other undefined divs or if any known div is defined in
1205 * terms of the unknown div.
1206 */
1207static __isl_give isl_basic_map *check_for_div_constraints(
1208 __isl_take isl_basic_map *bmap, int k, int l, isl_int sum,
1209 int *progress)
1210{
1211 int i;
1212 unsigned total = isl_basic_map_offset(bmap, isl_dim_div);
1213
1214 for (i = 0; i < bmap->n_div; ++i) {
1215 isl_bool set_div;
1216
1217 if (isl_int_is_zero(bmap->ineq[k][total + i])(isl_sioimath_sgn(*(bmap->ineq[k][total + i])) == 0))
1218 continue;
1219 if (isl_int_abs_ge(sum, bmap->ineq[k][total + i])(isl_sioimath_abs_cmp(*(sum), *(bmap->ineq[k][total + i]))
>= 0)
)
1220 continue;
1221 set_div = better_div_constraint(bmap, i, k);
1222 if (set_div >= 0 && set_div)
1223 set_div = ok_to_set_div_from_bound(bmap, i, k);
1224 if (set_div < 0)
1225 return isl_basic_map_free(bmap);
1226 if (!set_div)
1227 break;
1228 if (isl_int_is_pos(bmap->ineq[k][total + i])(isl_sioimath_sgn(*(bmap->ineq[k][total + i])) > 0))
1229 bmap = set_div_from_lower_bound(bmap, i, k);
1230 else
1231 bmap = set_div_from_lower_bound(bmap, i, l);
1232 if (progress)
1233 *progress = 1;
1234 break;
1235 }
1236 return bmap;
1237}
1238
1239__isl_give isl_basic_map *isl_basic_map_remove_duplicate_constraints(
1240 __isl_take isl_basic_map *bmap, int *progress, int detect_divs)
1241{
1242 struct isl_constraint_index ci;
1243 int k, l, h;
1244 isl_size total = isl_basic_map_dim(bmap, isl_dim_all);
1245 isl_int sum;
1246
1247 if (total < 0 || bmap->n_ineq <= 1)
35
Assuming 'total' is >= 0
36
Assuming field 'n_ineq' is > 1
37
Taking false branch
1248 return bmap;
1249
1250 if (create_constraint_index(&ci, bmap) < 0)
38
Calling 'create_constraint_index'
45
Returning from 'create_constraint_index'
46
Taking false branch
1251 return bmap;
1252
1253 h = isl_seq_get_hash_bits(bmap->ineq[0] + 1, total, ci.bits);
47
3rd function call argument is an uninitialized value
1254 ci.index[h] = &bmap->ineq[0];
1255 for (k = 1; k < bmap->n_ineq; ++k) {
1256 h = hash_index(&ci, bmap, k);
1257 if (!ci.index[h]) {
1258 ci.index[h] = &bmap->ineq[k];
1259 continue;
1260 }
1261 if (progress)
1262 *progress = 1;
1263 l = ci.index[h] - &bmap->ineq[0];
1264 if (isl_int_lt(bmap->ineq[k][0], bmap->ineq[l][0])(isl_sioimath_cmp(*(bmap->ineq[k][0]), *(bmap->ineq[l][
0])) < 0)
)
1265 swap_inequality(bmap, k, l);
1266 isl_basic_map_drop_inequality(bmap, k);
1267 --k;
1268 }
1269 isl_int_init(sum)isl_sioimath_init((sum));
1270 for (k = 0; bmap && k < bmap->n_ineq-1; ++k) {
1271 isl_seq_neg(bmap->ineq[k]+1, bmap->ineq[k]+1, total);
1272 h = hash_index(&ci, bmap, k);
1273 isl_seq_neg(bmap->ineq[k]+1, bmap->ineq[k]+1, total);
1274 if (!ci.index[h])
1275 continue;
1276 l = ci.index[h] - &bmap->ineq[0];
1277 isl_int_add(sum, bmap->ineq[k][0], bmap->ineq[l][0])isl_sioimath_add((sum), *(bmap->ineq[k][0]), *(bmap->ineq
[l][0]))
;
1278 if (isl_int_is_pos(sum)(isl_sioimath_sgn(*(sum)) > 0)) {
1279 if (detect_divs)
1280 bmap = check_for_div_constraints(bmap, k, l,
1281 sum, progress);
1282 continue;
1283 }
1284 if (isl_int_is_zero(sum)(isl_sioimath_sgn(*(sum)) == 0)) {
1285 /* We need to break out of the loop after these
1286 * changes since the contents of the hash
1287 * will no longer be valid.
1288 * Plus, we probably we want to regauss first.
1289 */
1290 if (progress)
1291 *progress = 1;
1292 isl_basic_map_drop_inequality(bmap, l);
1293 isl_basic_map_inequality_to_equality(bmap, k);
1294 } else
1295 bmap = isl_basic_map_set_to_empty(bmap);
1296 break;
1297 }
1298 isl_int_clear(sum)isl_sioimath_clear((sum));
1299
1300 constraint_index_free(&ci);
1301 return bmap;
1302}
1303
1304/* Detect all pairs of inequalities that form an equality.
1305 *
1306 * isl_basic_map_remove_duplicate_constraints detects at most one such pair.
1307 * Call it repeatedly while it is making progress.
1308 */
1309__isl_give isl_basic_map *isl_basic_map_detect_inequality_pairs(
1310 __isl_take isl_basic_map *bmap, int *progress)
1311{
1312 int duplicate;
1313
1314 do {
1315 duplicate = 0;
1316 bmap = isl_basic_map_remove_duplicate_constraints(bmap,
34
Calling 'isl_basic_map_remove_duplicate_constraints'
1317 &duplicate, 0);
1318 if (progress && duplicate)
1319 *progress = 1;
1320 } while (duplicate);
1321
1322 return bmap;
1323}
1324
1325/* Eliminate knowns divs from constraints where they appear with
1326 * a (positive or negative) unit coefficient.
1327 *
1328 * That is, replace
1329 *
1330 * floor(e/m) + f >= 0
1331 *
1332 * by
1333 *
1334 * e + m f >= 0
1335 *
1336 * and
1337 *
1338 * -floor(e/m) + f >= 0
1339 *
1340 * by
1341 *
1342 * -e + m f + m - 1 >= 0
1343 *
1344 * The first conversion is valid because floor(e/m) >= -f is equivalent
1345 * to e/m >= -f because -f is an integral expression.
1346 * The second conversion follows from the fact that
1347 *
1348 * -floor(e/m) = ceil(-e/m) = floor((-e + m - 1)/m)
1349 *
1350 *
1351 * Note that one of the div constraints may have been eliminated
1352 * due to being redundant with respect to the constraint that is
1353 * being modified by this function. The modified constraint may
1354 * no longer imply this div constraint, so we add it back to make
1355 * sure we do not lose any information.
1356 *
1357 * We skip integral divs, i.e., those with denominator 1, as we would
1358 * risk eliminating the div from the div constraints. We do not need
1359 * to handle those divs here anyway since the div constraints will turn
1360 * out to form an equality and this equality can then be used to eliminate
1361 * the div from all constraints.
1362 */
1363static __isl_give isl_basic_map *eliminate_unit_divs(
1364 __isl_take isl_basic_map *bmap, int *progress)
1365{
1366 int i, j;
1367 isl_ctx *ctx;
1368 unsigned total;
1369
1370 if (!bmap)
1371 return NULL((void*)0);
1372
1373 ctx = isl_basic_map_get_ctx(bmap);
1374 total = isl_basic_map_offset(bmap, isl_dim_div);
1375
1376 for (i = 0; i < bmap->n_div; ++i) {
1377 if (isl_int_is_zero(bmap->div[i][0])(isl_sioimath_sgn(*(bmap->div[i][0])) == 0))
1378 continue;
1379 if (isl_int_is_one(bmap->div[i][0])(isl_sioimath_cmp_si(*(bmap->div[i][0]), 1) == 0))
1380 continue;
1381 for (j = 0; j < bmap->n_ineq; ++j) {
1382 int s;
1383
1384 if (!isl_int_is_one(bmap->ineq[j][total + i])(isl_sioimath_cmp_si(*(bmap->ineq[j][total + i]), 1) == 0) &&
1385 !isl_int_is_negone(bmap->ineq[j][total + i])(isl_sioimath_cmp_si(*(bmap->ineq[j][total + i]), -1) == 0
)
)
1386 continue;
1387
1388 *progress = 1;
1389
1390 s = isl_int_sgn(bmap->ineq[j][total + i])isl_sioimath_sgn(*(bmap->ineq[j][total + i]));
1391 isl_int_set_si(bmap->ineq[j][total + i], 0)isl_sioimath_set_si((bmap->ineq[j][total + i]), 0);
1392 if (s < 0)
1393 isl_seq_combine(bmap->ineq[j],
1394 ctx->negone, bmap->div[i] + 1,
1395 bmap->div[i][0], bmap->ineq[j],
1396 total + bmap->n_div);
1397 else
1398 isl_seq_combine(bmap->ineq[j],
1399 ctx->one, bmap->div[i] + 1,
1400 bmap->div[i][0], bmap->ineq[j],
1401 total + bmap->n_div);
1402 if (s < 0) {
1403 isl_int_add(bmap->ineq[j][0],isl_sioimath_add((bmap->ineq[j][0]), *(bmap->ineq[j][0]
), *(bmap->div[i][0]))
1404 bmap->ineq[j][0], bmap->div[i][0])isl_sioimath_add((bmap->ineq[j][0]), *(bmap->ineq[j][0]
), *(bmap->div[i][0]))
;
1405 isl_int_sub_ui(bmap->ineq[j][0],isl_sioimath_sub_ui((bmap->ineq[j][0]), *(bmap->ineq[j]
[0]), 1)
1406 bmap->ineq[j][0], 1)isl_sioimath_sub_ui((bmap->ineq[j][0]), *(bmap->ineq[j]
[0]), 1)
;
1407 }
1408
1409 bmap = isl_basic_map_extend_constraints(bmap, 0, 1);
1410 bmap = isl_basic_map_add_div_constraint(bmap, i, s);
1411 if (!bmap)
1412 return NULL((void*)0);
1413 }
1414 }
1415
1416 return bmap;
1417}
1418
1419__isl_give isl_basic_map *isl_basic_map_simplify(__isl_take isl_basic_map *bmap)
1420{
1421 int progress = 1;
1422 if (!bmap)
1423 return NULL((void*)0);
1424 while (progress) {
1425 isl_bool empty;
1426
1427 progress = 0;
1428 empty = isl_basic_map_plain_is_empty(bmap);
1429 if (empty < 0)
1430 return isl_basic_map_free(bmap);
1431 if (empty)
1432 break;
1433 bmap = isl_basic_map_normalize_constraints(bmap);
1434 bmap = reduce_div_coefficients(bmap);
1435 bmap = normalize_div_expressions(bmap);
1436 bmap = remove_duplicate_divs(bmap, &progress);
1437 bmap = eliminate_unit_divs(bmap, &progress);
1438 bmap = eliminate_divs_eq(bmap, &progress);
1439 bmap = eliminate_divs_ineq(bmap, &progress);
1440 bmap = isl_basic_map_gauss(bmap, &progress);
1441 /* requires equalities in normal form */
1442 bmap = normalize_divs(bmap, &progress);
1443 bmap = isl_basic_map_remove_duplicate_constraints(bmap,
1444 &progress, 1);
1445 if (bmap && progress)
1446 ISL_F_CLR(bmap, ISL_BASIC_MAP_REDUCED_COEFFICIENTS)(((bmap)->flags) &= ~((1 << 8)));
1447 }
1448 return bmap;
1449}
1450
1451struct isl_basic_setisl_basic_map *isl_basic_set_simplify(struct isl_basic_setisl_basic_map *bset)
1452{
1453 return bset_from_bmap(isl_basic_map_simplify(bset_to_bmap(bset)));
1454}
1455
1456
1457isl_bool isl_basic_map_is_div_constraint(__isl_keep isl_basic_map *bmap,
1458 isl_int *constraint, unsigned div)
1459{
1460 unsigned pos;
1461
1462 if (!bmap)
1463 return isl_bool_error;
1464
1465 pos = isl_basic_map_offset(bmap, isl_dim_div) + div;
1466
1467 if (isl_int_eq(constraint[pos], bmap->div[div][0])(isl_sioimath_cmp(*(constraint[pos]), *(bmap->div[div][0])
) == 0)
) {
1468 int neg;
1469 isl_int_sub(bmap->div[div][1],isl_sioimath_sub((bmap->div[div][1]), *(bmap->div[div][
1]), *(bmap->div[div][0]))
1470 bmap->div[div][1], bmap->div[div][0])isl_sioimath_sub((bmap->div[div][1]), *(bmap->div[div][
1]), *(bmap->div[div][0]))
;
1471 isl_int_add_ui(bmap->div[div][1], bmap->div[div][1], 1)isl_sioimath_add_ui((bmap->div[div][1]), *(bmap->div[div
][1]), 1)
;
1472 neg = isl_seq_is_neg(constraint, bmap->div[div]+1, pos);
1473 isl_int_sub_ui(bmap->div[div][1], bmap->div[div][1], 1)isl_sioimath_sub_ui((bmap->div[div][1]), *(bmap->div[div
][1]), 1)
;
1474 isl_int_add(bmap->div[div][1],isl_sioimath_add((bmap->div[div][1]), *(bmap->div[div][
1]), *(bmap->div[div][0]))
1475 bmap->div[div][1], bmap->div[div][0])isl_sioimath_add((bmap->div[div][1]), *(bmap->div[div][
1]), *(bmap->div[div][0]))
;
1476 if (!neg)
1477 return isl_bool_false;
1478 if (isl_seq_first_non_zero(constraint+pos+1,
1479 bmap->n_div-div-1) != -1)
1480 return isl_bool_false;
1481 } else if (isl_int_abs_eq(constraint[pos], bmap->div[div][0])(isl_sioimath_abs_cmp(*(constraint[pos]), *(bmap->div[div]
[0])) == 0)
) {
1482 if (!isl_seq_eq(constraint, bmap->div[div]+1, pos))
1483 return isl_bool_false;
1484 if (isl_seq_first_non_zero(constraint+pos+1,
1485 bmap->n_div-div-1) != -1)
1486 return isl_bool_false;
1487 } else
1488 return isl_bool_false;
1489
1490 return isl_bool_true;
1491}
1492
1493isl_bool isl_basic_set_is_div_constraint(__isl_keep isl_basic_setisl_basic_map *bset,
1494 isl_int *constraint, unsigned div)
1495{
1496 return isl_basic_map_is_div_constraint(bset, constraint, div);
1497}
1498
1499
1500/* If the only constraints a div d=floor(f/m)
1501 * appears in are its two defining constraints
1502 *
1503 * f - m d >=0
1504 * -(f - (m - 1)) + m d >= 0
1505 *
1506 * then it can safely be removed.
1507 */
1508static isl_bool div_is_redundant(__isl_keep isl_basic_map *bmap, int div)
1509{
1510 int i;
1511 isl_size v_div = isl_basic_map_var_offset(bmap, isl_dim_div);
1512 unsigned pos = 1 + v_div + div;
1513
1514 if (v_div < 0)
1515 return isl_bool_error;
1516
1517 for (i = 0; i < bmap->n_eq; ++i)
1518 if (!isl_int_is_zero(bmap->eq[i][pos])(isl_sioimath_sgn(*(bmap->eq[i][pos])) == 0))
1519 return isl_bool_false;
1520
1521 for (i = 0; i < bmap->n_ineq; ++i) {
1522 isl_bool red;
1523
1524 if (isl_int_is_zero(bmap->ineq[i][pos])(isl_sioimath_sgn(*(bmap->ineq[i][pos])) == 0))
1525 continue;
1526 red = isl_basic_map_is_div_constraint(bmap, bmap->ineq[i], div);
1527 if (red < 0 || !red)
1528 return red;
1529 }
1530
1531 for (i = 0; i < bmap->n_div; ++i) {
1532 if (isl_int_is_zero(bmap->div[i][0])(isl_sioimath_sgn(*(bmap->div[i][0])) == 0))
1533 continue;
1534 if (!isl_int_is_zero(bmap->div[i][1+pos])(isl_sioimath_sgn(*(bmap->div[i][1+pos])) == 0))
1535 return isl_bool_false;
1536 }
1537
1538 return isl_bool_true;
1539}
1540
1541/*
1542 * Remove divs that don't occur in any of the constraints or other divs.
1543 * These can arise when dropping constraints from a basic map or
1544 * when the divs of a basic map have been temporarily aligned
1545 * with the divs of another basic map.
1546 */
1547static __isl_give isl_basic_map *remove_redundant_divs(
1548 __isl_take isl_basic_map *bmap)
1549{
1550 int i;
1551 isl_size v_div;
1552
1553 v_div = isl_basic_map_var_offset(bmap, isl_dim_div);
1554 if (v_div < 0)
1555 return isl_basic_map_free(bmap);
1556
1557 for (i = bmap->n_div-1; i >= 0; --i) {
1558 isl_bool redundant;
1559
1560 redundant = div_is_redundant(bmap, i);
1561 if (redundant < 0)
1562 return isl_basic_map_free(bmap);
1563 if (!redundant)
1564 continue;
1565 bmap = isl_basic_map_drop_constraints_involving(bmap,
1566 v_div + i, 1);
1567 bmap = isl_basic_map_drop_div(bmap, i);
1568 }
1569 return bmap;
1570}
1571
1572/* Mark "bmap" as final, without checking for obviously redundant
1573 * integer divisions. This function should be used when "bmap"
1574 * is known not to involve any such integer divisions.
1575 */
1576__isl_give isl_basic_map *isl_basic_map_mark_final(
1577 __isl_take isl_basic_map *bmap)
1578{
1579 if (!bmap)
1580 return NULL((void*)0);
1581 ISL_F_SET(bmap, ISL_BASIC_SET_FINAL)(((bmap)->flags) |= ((1 << 0)));
1582 return bmap;
1583}
1584
1585/* Mark "bmap" as final, after removing obviously redundant integer divisions.
1586 */
1587__isl_give isl_basic_map *isl_basic_map_finalize(__isl_take isl_basic_map *bmap)
1588{
1589 bmap = remove_redundant_divs(bmap);
1590 bmap = isl_basic_map_mark_final(bmap);
1591 return bmap;
1592}
1593
1594struct isl_basic_setisl_basic_map *isl_basic_set_finalize(struct isl_basic_setisl_basic_map *bset)
1595{
1596 return bset_from_bmap(isl_basic_map_finalize(bset_to_bmap(bset)));
1597}
1598
1599/* Remove definition of any div that is defined in terms of the given variable.
1600 * The div itself is not removed. Functions such as
1601 * eliminate_divs_ineq depend on the other divs remaining in place.
1602 */
1603static __isl_give isl_basic_map *remove_dependent_vars(
1604 __isl_take isl_basic_map *bmap, int pos)
1605{
1606 int i;
1607
1608 if (!bmap)
1609 return NULL((void*)0);
1610
1611 for (i = 0; i < bmap->n_div; ++i) {
1612 if (isl_int_is_zero(bmap->div[i][0])(isl_sioimath_sgn(*(bmap->div[i][0])) == 0))
1613 continue;
1614 if (isl_int_is_zero(bmap->div[i][1+1+pos])(isl_sioimath_sgn(*(bmap->div[i][1+1+pos])) == 0))
1615 continue;
1616 bmap = isl_basic_map_mark_div_unknown(bmap, i);
1617 if (!bmap)
1618 return NULL((void*)0);
1619 }
1620 return bmap;
1621}
1622
1623/* Eliminate the specified variables from the constraints using
1624 * Fourier-Motzkin. The variables themselves are not removed.
1625 */
1626__isl_give isl_basic_map *isl_basic_map_eliminate_vars(
1627 __isl_take isl_basic_map *bmap, unsigned pos, unsigned n)
1628{
1629 int d;
1630 int i, j, k;
1631 isl_size total;
1632 int need_gauss = 0;
1633
1634 if (n == 0)
1635 return bmap;
1636 total = isl_basic_map_dim(bmap, isl_dim_all);
1637 if (total < 0)
1638 return isl_basic_map_free(bmap);
1639
1640 bmap = isl_basic_map_cow(bmap);
1641 for (d = pos + n - 1; d >= 0 && d >= pos; --d)
1642 bmap = remove_dependent_vars(bmap, d);
1643 if (!bmap)
1644 return NULL((void*)0);
1645
1646 for (d = pos + n - 1;
1647 d >= 0 && d >= total - bmap->n_div && d >= pos; --d)
1648 isl_seq_clr(bmap->div[d-(total-bmap->n_div)], 2+total);
1649 for (d = pos + n - 1; d >= 0 && d >= pos; --d) {
1650 int n_lower, n_upper;
1651 if (!bmap)
1652 return NULL((void*)0);
1653 for (i = 0; i < bmap->n_eq; ++i) {
1654 if (isl_int_is_zero(bmap->eq[i][1+d])(isl_sioimath_sgn(*(bmap->eq[i][1+d])) == 0))
1655 continue;
1656 bmap = eliminate_var_using_equality(bmap, d,
1657 bmap->eq[i], 0, NULL((void*)0));
1658 if (isl_basic_map_drop_equality(bmap, i) < 0)
1659 return isl_basic_map_free(bmap);
1660 need_gauss = 1;
1661 break;
1662 }
1663 if (i < bmap->n_eq)
1664 continue;
1665 n_lower = 0;
1666 n_upper = 0;
1667 for (i = 0; i < bmap->n_ineq; ++i) {
1668 if (isl_int_is_pos(bmap->ineq[i][1+d])(isl_sioimath_sgn(*(bmap->ineq[i][1+d])) > 0))
1669 n_lower++;
1670 else if (isl_int_is_neg(bmap->ineq[i][1+d])(isl_sioimath_sgn(*(bmap->ineq[i][1+d])) < 0))
1671 n_upper++;
1672 }
1673 bmap = isl_basic_map_extend_constraints(bmap,
1674 0, n_lower * n_upper);
1675 if (!bmap)
1676 goto error;
1677 for (i = bmap->n_ineq - 1; i >= 0; --i) {
1678 int last;
1679 if (isl_int_is_zero(bmap->ineq[i][1+d])(isl_sioimath_sgn(*(bmap->ineq[i][1+d])) == 0))
1680 continue;
1681 last = -1;
1682 for (j = 0; j < i; ++j) {
1683 if (isl_int_is_zero(bmap->ineq[j][1+d])(isl_sioimath_sgn(*(bmap->ineq[j][1+d])) == 0))
1684 continue;
1685 last = j;
1686 if (isl_int_sgn(bmap->ineq[i][1+d])isl_sioimath_sgn(*(bmap->ineq[i][1+d])) ==
1687 isl_int_sgn(bmap->ineq[j][1+d])isl_sioimath_sgn(*(bmap->ineq[j][1+d])))
1688 continue;
1689 k = isl_basic_map_alloc_inequality(bmap);
1690 if (k < 0)
1691 goto error;
1692 isl_seq_cpy(bmap->ineq[k], bmap->ineq[i],
1693 1+total);
1694 isl_seq_elim(bmap->ineq[k], bmap->ineq[j],
1695 1+d, 1+total, NULL((void*)0));
1696 }
1697 isl_basic_map_drop_inequality(bmap, i);
1698 i = last + 1;
1699 }
1700 if (n_lower > 0 && n_upper > 0) {
1701 bmap = isl_basic_map_normalize_constraints(bmap);
1702 bmap = isl_basic_map_remove_duplicate_constraints(bmap,
1703 NULL((void*)0), 0);
1704 bmap = isl_basic_map_gauss(bmap, NULL((void*)0));
1705 bmap = isl_basic_map_remove_redundancies(bmap);
1706 need_gauss = 0;
1707 if (!bmap)
1708 goto error;
1709 if (ISL_F_ISSET(bmap, ISL_BASIC_MAP_EMPTY)(!!(((bmap)->flags) & ((1 << 1)))))
1710 break;
1711 }
1712 }
1713 if (need_gauss)
1714 bmap = isl_basic_map_gauss(bmap, NULL((void*)0));
1715 return bmap;
1716error:
1717 isl_basic_map_free(bmap);
1718 return NULL((void*)0);
1719}
1720
1721struct isl_basic_setisl_basic_map *isl_basic_set_eliminate_vars(
1722 struct isl_basic_setisl_basic_map *bset, unsigned pos, unsigned n)
1723{
1724 return bset_from_bmap(isl_basic_map_eliminate_vars(bset_to_bmap(bset),
1725 pos, n));
1726}
1727
1728/* Eliminate the specified n dimensions starting at first from the
1729 * constraints, without removing the dimensions from the space.
1730 * If the set is rational, the dimensions are eliminated using Fourier-Motzkin.
1731 * Otherwise, they are projected out and the original space is restored.
1732 */
1733__isl_give isl_basic_map *isl_basic_map_eliminate(
1734 __isl_take isl_basic_map *bmap,
1735 enum isl_dim_type type, unsigned first, unsigned n)
1736{
1737 isl_space *space;
1738
1739 if (!bmap)
1740 return NULL((void*)0);
1741 if (n == 0)
1742 return bmap;
1743
1744 if (isl_basic_map_check_range(bmap, type, first, n) < 0)
1745 return isl_basic_map_free(bmap);
1746
1747 if (ISL_F_ISSET(bmap, ISL_BASIC_MAP_RATIONAL)(!!(((bmap)->flags) & ((1 << 4))))) {
1748 first += isl_basic_map_offset(bmap, type) - 1;
1749 bmap = isl_basic_map_eliminate_vars(bmap, first, n);
1750 return isl_basic_map_finalize(bmap);
1751 }
1752
1753 space = isl_basic_map_get_space(bmap);
1754 bmap = isl_basic_map_project_out(bmap, type, first, n);
1755 bmap = isl_basic_map_insert_dims(bmap, type, first, n);
1756 bmap = isl_basic_map_reset_space(bmap, space);
1757 return bmap;
1758}
1759
1760__isl_give isl_basic_setisl_basic_map *isl_basic_set_eliminate(
1761 __isl_take isl_basic_setisl_basic_map *bset,
1762 enum isl_dim_type type, unsigned first, unsigned n)
1763{
1764 return isl_basic_map_eliminate(bset, type, first, n);
1765}
1766
1767/* Remove all constraints from "bmap" that reference any unknown local
1768 * variables (directly or indirectly).
1769 *
1770 * Dropping all constraints on a local variable will make it redundant,
1771 * so it will get removed implicitly by
1772 * isl_basic_map_drop_constraints_involving_dims. Some other local
1773 * variables may also end up becoming redundant if they only appear
1774 * in constraints together with the unknown local variable.
1775 * Therefore, start over after calling
1776 * isl_basic_map_drop_constraints_involving_dims.
1777 */
1778__isl_give isl_basic_map *isl_basic_map_drop_constraint_involving_unknown_divs(
1779 __isl_take isl_basic_map *bmap)
1780{
1781 isl_bool known;
1782 isl_size n_div;
1783 int i, o_div;
1784
1785 known = isl_basic_map_divs_known(bmap);
1786 if (known < 0)
1787 return isl_basic_map_free(bmap);
1788 if (known)
1789 return bmap;
1790
1791 n_div = isl_basic_map_dim(bmap, isl_dim_div);
1792 if (n_div < 0)
1793 return isl_basic_map_free(bmap);
1794 o_div = isl_basic_map_offset(bmap, isl_dim_div) - 1;
1795
1796 for (i = 0; i < n_div; ++i) {
1797 known = isl_basic_map_div_is_known(bmap, i);
1798 if (known < 0)
1799 return isl_basic_map_free(bmap);
1800 if (known)
1801 continue;
1802 bmap = remove_dependent_vars(bmap, o_div + i);
1803 bmap = isl_basic_map_drop_constraints_involving_dims(bmap,
1804 isl_dim_div, i, 1);
1805 n_div = isl_basic_map_dim(bmap, isl_dim_div);
1806 if (n_div < 0)
1807 return isl_basic_map_free(bmap);
1808 i = -1;
1809 }
1810
1811 return bmap;
1812}
1813
1814/* Remove all constraints from "map" that reference any unknown local
1815 * variables (directly or indirectly).
1816 *
1817 * Since constraints may get dropped from the basic maps,
1818 * they may no longer be disjoint from each other.
1819 */
1820__isl_give isl_map *isl_map_drop_constraint_involving_unknown_divs(
1821 __isl_take isl_map *map)
1822{
1823 int i;
1824 isl_bool known;
1825
1826 known = isl_map_divs_known(map);
1827 if (known < 0)
1828 return isl_map_free(map);
1829 if (known)
1830 return map;
1831
1832 map = isl_map_cow(map);
1833 if (!map)
1834 return NULL((void*)0);
1835
1836 for (i = 0; i < map->n; ++i) {
1837 map->p[i] =
1838 isl_basic_map_drop_constraint_involving_unknown_divs(
1839 map->p[i]);
1840 if (!map->p[i])
1841 return isl_map_free(map);
1842 }
1843
1844 if (map->n > 1)
1845 ISL_F_CLR(map, ISL_MAP_DISJOINT)(((map)->flags) &= ~((1 << 0)));
1846
1847 return map;
1848}
1849
1850/* Don't assume equalities are in order, because align_divs
1851 * may have changed the order of the divs.
1852 */
1853static void compute_elimination_index(__isl_keep isl_basic_map *bmap, int *elim)
1854{
1855 int d, i;
1856 unsigned total;
1857
1858 total = isl_space_dim(bmap->dim, isl_dim_all);
1859 for (d = 0; d < total; ++d)
1860 elim[d] = -1;
1861 for (i = 0; i < bmap->n_eq; ++i) {
1862 for (d = total - 1; d >= 0; --d) {
1863 if (isl_int_is_zero(bmap->eq[i][1+d])(isl_sioimath_sgn(*(bmap->eq[i][1+d])) == 0))
1864 continue;
1865 elim[d] = i;
1866 break;
1867 }
1868 }
1869}
1870
1871static void set_compute_elimination_index(__isl_keep isl_basic_setisl_basic_map *bset,
1872 int *elim)
1873{
1874 compute_elimination_index(bset_to_bmap(bset), elim);
1875}
1876
1877static int reduced_using_equalities(isl_int *dst, isl_int *src,
1878 __isl_keep isl_basic_map *bmap, int *elim)
1879{
1880 int d;
1881 int copied = 0;
1882 unsigned total;
1883
1884 total = isl_space_dim(bmap->dim, isl_dim_all);
1885 for (d = total - 1; d >= 0; --d) {
1886 if (isl_int_is_zero(src[1+d])(isl_sioimath_sgn(*(src[1+d])) == 0))
1887 continue;
1888 if (elim[d] == -1)
1889 continue;
1890 if (!copied) {
1891 isl_seq_cpy(dst, src, 1 + total);
1892 copied = 1;
1893 }
1894 isl_seq_elim(dst, bmap->eq[elim[d]], 1 + d, 1 + total, NULL((void*)0));
1895 }
1896 return copied;
1897}
1898
1899static int set_reduced_using_equalities(isl_int *dst, isl_int *src,
1900 __isl_keep isl_basic_setisl_basic_map *bset, int *elim)
1901{
1902 return reduced_using_equalities(dst, src,
1903 bset_to_bmap(bset), elim);
1904}
1905
1906static __isl_give isl_basic_setisl_basic_map *isl_basic_set_reduce_using_equalities(
1907 __isl_take isl_basic_setisl_basic_map *bset, __isl_take isl_basic_setisl_basic_map *context)
1908{
1909 int i;
1910 int *elim;
1911 isl_size dim;
1912
1913 if (!bset || !context)
1914 goto error;
1915
1916 if (context->n_eq == 0) {
1917 isl_basic_set_free(context);
1918 return bset;
1919 }
1920
1921 bset = isl_basic_set_cow(bset);
1922 dim = isl_basic_set_dim(bset, isl_dim_set);
1923 if (dim < 0)
1924 goto error;
1925
1926 elim = isl_alloc_array(bset->ctx, int, dim)((int *)isl_malloc_or_die(bset->ctx, (dim)*sizeof(int)));
1927 if (!elim)
1928 goto error;
1929 set_compute_elimination_index(context, elim);
1930 for (i = 0; i < bset->n_eq; ++i)
1931 set_reduced_using_equalities(bset->eq[i], bset->eq[i],
1932 context, elim);
1933 for (i = 0; i < bset->n_ineq; ++i)
1934 set_reduced_using_equalities(bset->ineq[i], bset->ineq[i],
1935 context, elim);
1936 isl_basic_set_free(context);
1937 free(elim);
1938 bset = isl_basic_set_simplify(bset);
1939 bset = isl_basic_set_finalize(bset);
1940 return bset;
1941error:
1942 isl_basic_set_free(bset);
1943 isl_basic_set_free(context);
1944 return NULL((void*)0);
1945}
1946
1947/* For each inequality in "ineq" that is a shifted (more relaxed)
1948 * copy of an inequality in "context", mark the corresponding entry
1949 * in "row" with -1.
1950 * If an inequality only has a non-negative constant term, then
1951 * mark it as well.
1952 */
1953static isl_stat mark_shifted_constraints(__isl_keep isl_mat *ineq,
1954 __isl_keep isl_basic_setisl_basic_map *context, int *row)
1955{
1956 struct isl_constraint_index ci;
1957 isl_size n_ineq, cols;
1958 unsigned total;
1959 int k;
1960
1961 if (!ineq || !context)
1962 return isl_stat_error;
1963 if (context->n_ineq == 0)
1964 return isl_stat_ok;
1965 if (setup_constraint_index(&ci, context) < 0)
1966 return isl_stat_error;
1967
1968 n_ineq = isl_mat_rows(ineq);
1969 cols = isl_mat_cols(ineq);
1970 if (n_ineq < 0 || cols < 0)
1971 return isl_stat_error;
1972 total = cols - 1;
1973 for (k = 0; k < n_ineq; ++k) {
1974 int l;
1975 isl_bool redundant;
1976
1977 l = isl_seq_first_non_zero(ineq->row[k] + 1, total);
1978 if (l < 0 && isl_int_is_nonneg(ineq->row[k][0])(isl_sioimath_sgn(*(ineq->row[k][0])) >= 0)) {
1979 row[k] = -1;
1980 continue;
1981 }
1982 redundant = constraint_index_is_redundant(&ci, ineq->row[k]);
1983 if (redundant < 0)
1984 goto error;
1985 if (!redundant)
1986 continue;
1987 row[k] = -1;
1988 }
1989 constraint_index_free(&ci);
1990 return isl_stat_ok;
1991error:
1992 constraint_index_free(&ci);
1993 return isl_stat_error;
1994}
1995
1996static __isl_give isl_basic_setisl_basic_map *remove_shifted_constraints(
1997 __isl_take isl_basic_setisl_basic_map *bset, __isl_keep isl_basic_setisl_basic_map *context)
1998{
1999 struct isl_constraint_index ci;
2000 int k;
2001
2002 if (!bset || !context)
2003 return bset;
2004
2005 if (context->n_ineq == 0)
2006 return bset;
2007 if (setup_constraint_index(&ci, context) < 0)
2008 return bset;
2009
2010 for (k = 0; k < bset->n_ineq; ++k) {
2011 isl_bool redundant;
2012
2013 redundant = constraint_index_is_redundant(&ci, bset->ineq[k]);
2014 if (redundant < 0)
2015 goto error;
2016 if (!redundant)
2017 continue;
2018 bset = isl_basic_set_cow(bset);
2019 if (!bset)
2020 goto error;
2021 isl_basic_set_drop_inequality(bset, k);
2022 --k;
2023 }
2024 constraint_index_free(&ci);
2025 return bset;
2026error:
2027 constraint_index_free(&ci);
2028 return bset;
2029}
2030
2031/* Remove constraints from "bmap" that are identical to constraints
2032 * in "context" or that are more relaxed (greater constant term).
2033 *
2034 * We perform the test for shifted copies on the pure constraints
2035 * in remove_shifted_constraints.
2036 */
2037static __isl_give isl_basic_map *isl_basic_map_remove_shifted_constraints(
2038 __isl_take isl_basic_map *bmap, __isl_take isl_basic_map *context)
2039{
2040 isl_basic_setisl_basic_map *bset, *bset_context;
2041
2042 if (!bmap || !context)
2043 goto error;
2044
2045 if (bmap->n_ineq == 0 || context->n_ineq == 0) {
2046 isl_basic_map_free(context);
2047 return bmap;
2048 }
2049
2050 context = isl_basic_map_align_divs(context, bmap);
2051 bmap = isl_basic_map_align_divs(bmap, context);
2052
2053 bset = isl_basic_map_underlying_set(isl_basic_map_copy(bmap));
2054 bset_context = isl_basic_map_underlying_set(context);
2055 bset = remove_shifted_constraints(bset, bset_context);
2056 isl_basic_set_free(bset_context);
2057
2058 bmap = isl_basic_map_overlying_set(bset, bmap);
2059
2060 return bmap;
2061error:
2062 isl_basic_map_free(bmap);
2063 isl_basic_map_free(context);
2064 return NULL((void*)0);
2065}
2066
2067/* Does the (linear part of a) constraint "c" involve any of the "len"
2068 * "relevant" dimensions?
2069 */
2070static int is_related(isl_int *c, int len, int *relevant)
2071{
2072 int i;
2073
2074 for (i = 0; i < len; ++i) {
2075 if (!relevant[i])
2076 continue;
2077 if (!isl_int_is_zero(c[i])(isl_sioimath_sgn(*(c[i])) == 0))
2078 return 1;
2079 }
2080
2081 return 0;
2082}
2083
2084/* Drop constraints from "bmap" that do not involve any of
2085 * the dimensions marked "relevant".
2086 */
2087static __isl_give isl_basic_map *drop_unrelated_constraints(
2088 __isl_take isl_basic_map *bmap, int *relevant)
2089{
2090 int i;
2091 isl_size dim;
2092
2093 dim = isl_basic_map_dim(bmap, isl_dim_all);
2094 if (dim < 0)
2095 return isl_basic_map_free(bmap);
2096 for (i = 0; i < dim; ++i)
2097 if (!relevant[i])
2098 break;
2099 if (i >= dim)
2100 return bmap;
2101
2102 for (i = bmap->n_eq - 1; i >= 0; --i)
2103 if (!is_related(bmap->eq[i] + 1, dim, relevant)) {
2104 bmap = isl_basic_map_cow(bmap);
2105 if (isl_basic_map_drop_equality(bmap, i) < 0)
2106 return isl_basic_map_free(bmap);
2107 }
2108
2109 for (i = bmap->n_ineq - 1; i >= 0; --i)
2110 if (!is_related(bmap->ineq[i] + 1, dim, relevant)) {
2111 bmap = isl_basic_map_cow(bmap);
2112 if (isl_basic_map_drop_inequality(bmap, i) < 0)
2113 return isl_basic_map_free(bmap);
2114 }
2115
2116 return bmap;
2117}
2118
2119/* Update the groups in "group" based on the (linear part of a) constraint "c".
2120 *
2121 * In particular, for any variable involved in the constraint,
2122 * find the actual group id from before and replace the group
2123 * of the corresponding variable by the minimal group of all
2124 * the variables involved in the constraint considered so far
2125 * (if this minimum is smaller) or replace the minimum by this group
2126 * (if the minimum is larger).
2127 *
2128 * At the end, all the variables in "c" will (indirectly) point
2129 * to the minimal of the groups that they referred to originally.
2130 */
2131static void update_groups(int dim, int *group, isl_int *c)
2132{
2133 int j;
2134 int min = dim;
2135
2136 for (j = 0; j < dim; ++j) {
2137 if (isl_int_is_zero(c[j])(isl_sioimath_sgn(*(c[j])) == 0))
2138 continue;
2139 while (group[j] >= 0 && group[group[j]] != group[j])
2140 group[j] = group[group[j]];
2141 if (group[j] == min)
2142 continue;
2143 if (group[j] < min) {
2144 if (min >= 0 && min < dim)
2145 group[min] = group[j];
2146 min = group[j];
2147 } else
2148 group[group[j]] = min;
2149 }
2150}
2151
2152/* Allocate an array of groups of variables, one for each variable
2153 * in "context", initialized to zero.
2154 */
2155static int *alloc_groups(__isl_keep isl_basic_setisl_basic_map *context)
2156{
2157 isl_ctx *ctx;
2158 isl_size dim;
2159
2160 dim = isl_basic_set_dim(context, isl_dim_set);
2161 if (dim < 0)
2162 return NULL((void*)0);
2163 ctx = isl_basic_set_get_ctx(context);
2164 return isl_calloc_array(ctx, int, dim)((int *)isl_calloc_or_die(ctx, dim, sizeof(int)));
2165}
2166
2167/* Drop constraints from "bmap" that only involve variables that are
2168 * not related to any of the variables marked with a "-1" in "group".
2169 *
2170 * We construct groups of variables that collect variables that
2171 * (indirectly) appear in some common constraint of "bmap".
2172 * Each group is identified by the first variable in the group,
2173 * except for the special group of variables that was already identified
2174 * in the input as -1 (or are related to those variables).
2175 * If group[i] is equal to i (or -1), then the group of i is i (or -1),
2176 * otherwise the group of i is the group of group[i].
2177 *
2178 * We first initialize groups for the remaining variables.
2179 * Then we iterate over the constraints of "bmap" and update the
2180 * group of the variables in the constraint by the smallest group.
2181 * Finally, we resolve indirect references to groups by running over
2182 * the variables.
2183 *
2184 * After computing the groups, we drop constraints that do not involve
2185 * any variables in the -1 group.
2186 */
2187__isl_give isl_basic_map *isl_basic_map_drop_unrelated_constraints(
2188 __isl_take isl_basic_map *bmap, __isl_take int *group)
2189{
2190 isl_size dim;
2191 int i;
2192 int last;
2193
2194 dim = isl_basic_map_dim(bmap, isl_dim_all);
2195 if (dim < 0)
2196 return isl_basic_map_free(bmap);
2197
2198 last = -1;
2199 for (i = 0; i < dim; ++i)
2200 if (group[i] >= 0)
2201 last = group[i] = i;
2202 if (last < 0) {
2203 free(group);
2204 return bmap;
2205 }
2206
2207 for (i = 0; i < bmap->n_eq; ++i)
2208 update_groups(dim, group, bmap->eq[i] + 1);
2209 for (i = 0; i < bmap->n_ineq; ++i)
2210 update_groups(dim, group, bmap->ineq[i] + 1);
2211
2212 for (i = 0; i < dim; ++i)
2213 if (group[i] >= 0)
2214 group[i] = group[group[i]];
2215
2216 for (i = 0; i < dim; ++i)
2217 group[i] = group[i] == -1;
2218
2219 bmap = drop_unrelated_constraints(bmap, group);
2220
2221 free(group);
2222 return bmap;
2223}
2224
2225/* Drop constraints from "context" that are irrelevant for computing
2226 * the gist of "bset".
2227 *
2228 * In particular, drop constraints in variables that are not related
2229 * to any of the variables involved in the constraints of "bset"
2230 * in the sense that there is no sequence of constraints that connects them.
2231 *
2232 * We first mark all variables that appear in "bset" as belonging
2233 * to a "-1" group and then continue with group_and_drop_irrelevant_constraints.
2234 */
2235static __isl_give isl_basic_setisl_basic_map *drop_irrelevant_constraints(
2236 __isl_take isl_basic_setisl_basic_map *context, __isl_keep isl_basic_setisl_basic_map *bset)
2237{
2238 int *group;
2239 isl_size dim;
2240 int i, j;
2241
2242 dim = isl_basic_set_dim(bset, isl_dim_set);
2243 if (!context || dim < 0)
2244 return isl_basic_set_free(context);
2245
2246 group = alloc_groups(context);
2247
2248 if (!group)
2249 return isl_basic_set_free(context);
2250
2251 for (i = 0; i < dim; ++i) {
2252 for (j = 0; j < bset->n_eq; ++j)
2253 if (!isl_int_is_zero(bset->eq[j][1 + i])(isl_sioimath_sgn(*(bset->eq[j][1 + i])) == 0))
2254 break;
2255 if (j < bset->n_eq) {
2256 group[i] = -1;
2257 continue;
2258 }
2259 for (j = 0; j < bset->n_ineq; ++j)
2260 if (!isl_int_is_zero(bset->ineq[j][1 + i])(isl_sioimath_sgn(*(bset->ineq[j][1 + i])) == 0))
2261 break;
2262 if (j < bset->n_ineq)
2263 group[i] = -1;
2264 }
2265
2266 return isl_basic_map_drop_unrelated_constraints(context, group);
2267}
2268
2269/* Drop constraints from "context" that are irrelevant for computing
2270 * the gist of the inequalities "ineq".
2271 * Inequalities in "ineq" for which the corresponding element of row
2272 * is set to -1 have already been marked for removal and should be ignored.
2273 *
2274 * In particular, drop constraints in variables that are not related
2275 * to any of the variables involved in "ineq"
2276 * in the sense that there is no sequence of constraints that connects them.
2277 *
2278 * We first mark all variables that appear in "bset" as belonging
2279 * to a "-1" group and then continue with group_and_drop_irrelevant_constraints.
2280 */
2281static __isl_give isl_basic_setisl_basic_map *drop_irrelevant_constraints_marked(
2282 __isl_take isl_basic_setisl_basic_map *context, __isl_keep isl_mat *ineq, int *row)
2283{
2284 int *group;
2285 isl_size dim;
2286 int i, j;
2287 isl_size n;
2288
2289 dim = isl_basic_set_dim(context, isl_dim_set);
2290 n = isl_mat_rows(ineq);
2291 if (dim < 0 || n < 0)
2292 return isl_basic_set_free(context);
2293
2294 group = alloc_groups(context);
2295
2296 if (!group)
2297 return isl_basic_set_free(context);
2298
2299 for (i = 0; i < dim; ++i) {
2300 for (j = 0; j < n; ++j) {
2301 if (row[j] < 0)
2302 continue;
2303 if (!isl_int_is_zero(ineq->row[j][1 + i])(isl_sioimath_sgn(*(ineq->row[j][1 + i])) == 0))
2304 break;
2305 }
2306 if (j < n)
2307 group[i] = -1;
2308 }
2309
2310 return isl_basic_map_drop_unrelated_constraints(context, group);
2311}
2312
2313/* Do all "n" entries of "row" contain a negative value?
2314 */
2315static int all_neg(int *row, int n)
2316{
2317 int i;
2318
2319 for (i = 0; i < n; ++i)
2320 if (row[i] >= 0)
2321 return 0;
2322
2323 return 1;
2324}
2325
2326/* Update the inequalities in "bset" based on the information in "row"
2327 * and "tab".
2328 *
2329 * In particular, the array "row" contains either -1, meaning that
2330 * the corresponding inequality of "bset" is redundant, or the index
2331 * of an inequality in "tab".
2332 *
2333 * If the row entry is -1, then drop the inequality.
2334 * Otherwise, if the constraint is marked redundant in the tableau,
2335 * then drop the inequality. Similarly, if it is marked as an equality
2336 * in the tableau, then turn the inequality into an equality and
2337 * perform Gaussian elimination.
2338 */
2339static __isl_give isl_basic_setisl_basic_map *update_ineq(__isl_take isl_basic_setisl_basic_map *bset,
2340 __isl_keep int *row, struct isl_tab *tab)
2341{
2342 int i;
2343 unsigned n_ineq;
2344 unsigned n_eq;
2345 int found_equality = 0;
2346
2347 if (!bset)
2348 return NULL((void*)0);
2349 if (tab && tab->empty)
2350 return isl_basic_set_set_to_empty(bset);
2351
2352 n_ineq = bset->n_ineq;
2353 for (i = n_ineq - 1; i >= 0; --i) {
2354 if (row[i] < 0) {
2355 if (isl_basic_set_drop_inequality(bset, i) < 0)
2356 return isl_basic_set_free(bset);
2357 continue;
2358 }
2359 if (!tab)
2360 continue;
2361 n_eq = tab->n_eq;
2362 if (isl_tab_is_equality(tab, n_eq + row[i])) {
2363 isl_basic_map_inequality_to_equality(bset, i);
2364 found_equality = 1;
2365 } else if (isl_tab_is_redundant(tab, n_eq + row[i])) {
2366 if (isl_basic_set_drop_inequality(bset, i) < 0)
2367 return isl_basic_set_free(bset);
2368 }
2369 }
2370
2371 if (found_equality)
2372 bset = isl_basic_set_gauss(bset, NULL((void*)0));
2373 bset = isl_basic_set_finalize(bset);
2374 return bset;
2375}
2376
2377/* Update the inequalities in "bset" based on the information in "row"
2378 * and "tab" and free all arguments (other than "bset").
2379 */
2380static __isl_give isl_basic_setisl_basic_map *update_ineq_free(
2381 __isl_take isl_basic_setisl_basic_map *bset, __isl_take isl_mat *ineq,
2382 __isl_take isl_basic_setisl_basic_map *context, __isl_take int *row,
2383 struct isl_tab *tab)
2384{
2385 isl_mat_free(ineq);
2386 isl_basic_set_free(context);
2387
2388 bset = update_ineq(bset, row, tab);
2389
2390 free(row);
2391 isl_tab_free(tab);
2392 return bset;
2393}
2394
2395/* Remove all information from bset that is redundant in the context
2396 * of context.
2397 * "ineq" contains the (possibly transformed) inequalities of "bset",
2398 * in the same order.
2399 * The (explicit) equalities of "bset" are assumed to have been taken
2400 * into account by the transformation such that only the inequalities
2401 * are relevant.
2402 * "context" is assumed not to be empty.
2403 *
2404 * "row" keeps track of the constraint index of a "bset" inequality in "tab".
2405 * A value of -1 means that the inequality is obviously redundant and may
2406 * not even appear in "tab".
2407 *
2408 * We first mark the inequalities of "bset"
2409 * that are obviously redundant with respect to some inequality in "context".
2410 * Then we remove those constraints from "context" that have become
2411 * irrelevant for computing the gist of "bset".
2412 * Note that this removal of constraints cannot be replaced by
2413 * a factorization because factors in "bset" may still be connected
2414 * to each other through constraints in "context".
2415 *
2416 * If there are any inequalities left, we construct a tableau for
2417 * the context and then add the inequalities of "bset".
2418 * Before adding these inequalities, we freeze all constraints such that
2419 * they won't be considered redundant in terms of the constraints of "bset".
2420 * Then we detect all redundant constraints (among the
2421 * constraints that weren't frozen), first by checking for redundancy in the
2422 * the tableau and then by checking if replacing a constraint by its negation
2423 * would lead to an empty set. This last step is fairly expensive
2424 * and could be optimized by more reuse of the tableau.
2425 * Finally, we update bset according to the results.
2426 */
2427static __isl_give isl_basic_setisl_basic_map *uset_gist_full(__isl_take isl_basic_setisl_basic_map *bset,
2428 __isl_take isl_mat *ineq, __isl_take isl_basic_setisl_basic_map *context)
2429{
2430 int i, r;
2431 int *row = NULL((void*)0);
2432 isl_ctx *ctx;
2433 isl_basic_setisl_basic_map *combined = NULL((void*)0);
2434 struct isl_tab *tab = NULL((void*)0);
2435 unsigned n_eq, context_ineq;
2436
2437 if (!bset || !ineq || !context)
2438 goto error;
2439
2440 if (bset->n_ineq == 0 || isl_basic_set_plain_is_universe(context)) {
2441 isl_basic_set_free(context);
2442 isl_mat_free(ineq);
2443 return bset;
2444 }
2445
2446 ctx = isl_basic_set_get_ctx(context);
2447 row = isl_calloc_array(ctx, int, bset->n_ineq)((int *)isl_calloc_or_die(ctx, bset->n_ineq, sizeof(int)));
2448 if (!row)
2449 goto error;
2450
2451 if (mark_shifted_constraints(ineq, context, row) < 0)
2452 goto error;
2453 if (all_neg(row, bset->n_ineq))
2454 return update_ineq_free(bset, ineq, context, row, NULL((void*)0));
2455
2456 context = drop_irrelevant_constraints_marked(context, ineq, row);
2457 if (!context)
2458 goto error;
2459 if (isl_basic_set_plain_is_universe(context))
2460 return update_ineq_free(bset, ineq, context, row, NULL((void*)0));
2461
2462 n_eq = context->n_eq;
2463 context_ineq = context->n_ineq;
2464 combined = isl_basic_set_cow(isl_basic_set_copy(context));
2465 combined = isl_basic_set_extend_constraints(combined, 0, bset->n_ineq);
2466 tab = isl_tab_from_basic_set(combined, 0);
2467 for (i = 0; i < context_ineq; ++i)
2468 if (isl_tab_freeze_constraint(tab, n_eq + i) < 0)
2469 goto error;
2470 if (isl_tab_extend_cons(tab, bset->n_ineq) < 0)
2471 goto error;
2472 r = context_ineq;
2473 for (i = 0; i < bset->n_ineq; ++i) {
2474 if (row[i] < 0)
2475 continue;
2476 combined = isl_basic_set_add_ineq(combined, ineq->row[i]);
2477 if (isl_tab_add_ineq(tab, ineq->row[i]) < 0)
2478 goto error;
2479 row[i] = r++;
2480 }
2481 if (isl_tab_detect_implicit_equalities(tab) < 0)
2482 goto error;
2483 if (isl_tab_detect_redundant(tab) < 0)
2484 goto error;
2485 for (i = bset->n_ineq - 1; i >= 0; --i) {
2486 isl_basic_setisl_basic_map *test;
2487 int is_empty;
2488
2489 if (row[i] < 0)
2490 continue;
2491 r = row[i];
2492 if (tab->con[n_eq + r].is_redundant)
2493 continue;
2494 test = isl_basic_set_dup(combined);
2495 test = isl_inequality_negate(test, r);
2496 test = isl_basic_set_update_from_tab(test, tab);
2497 is_empty = isl_basic_set_is_empty(test);
2498 isl_basic_set_free(test);
2499 if (is_empty < 0)
2500 goto error;
2501 if (is_empty)
2502 tab->con[n_eq + r].is_redundant = 1;
2503 }
2504 bset = update_ineq_free(bset, ineq, context, row, tab);
2505 if (bset) {
2506 ISL_F_SET(bset, ISL_BASIC_SET_NO_IMPLICIT)(((bset)->flags) |= ((1 << 2)));
2507 ISL_F_SET(bset, ISL_BASIC_SET_NO_REDUNDANT)(((bset)->flags) |= ((1 << 3)));
2508 }
2509
2510 isl_basic_set_free(combined);
2511 return bset;
2512error:
2513 free(row);
2514 isl_mat_free(ineq);
2515 isl_tab_free(tab);
2516 isl_basic_set_free(combined);
2517 isl_basic_set_free(context);
2518 isl_basic_set_free(bset);
2519 return NULL((void*)0);
2520}
2521
2522/* Extract the inequalities of "bset" as an isl_mat.
2523 */
2524static __isl_give isl_mat *extract_ineq(__isl_keep isl_basic_setisl_basic_map *bset)
2525{
2526 isl_size total;
2527 isl_ctx *ctx;
2528 isl_mat *ineq;
2529
2530 total = isl_basic_set_dim(bset, isl_dim_all);
2531 if (total < 0)
2532 return NULL((void*)0);
2533
2534 ctx = isl_basic_set_get_ctx(bset);
2535 ineq = isl_mat_sub_alloc6(ctx, bset->ineq, 0, bset->n_ineq,
2536 0, 1 + total);
2537
2538 return ineq;
2539}
2540
2541/* Remove all information from "bset" that is redundant in the context
2542 * of "context", for the case where both "bset" and "context" are
2543 * full-dimensional.
2544 */
2545static __isl_give isl_basic_setisl_basic_map *uset_gist_uncompressed(
2546 __isl_take isl_basic_setisl_basic_map *bset, __isl_take isl_basic_setisl_basic_map *context)
2547{
2548 isl_mat *ineq;
2549
2550 ineq = extract_ineq(bset);
2551 return uset_gist_full(bset, ineq, context);
2552}
2553
2554/* Remove all information from "bset" that is redundant in the context
2555 * of "context", for the case where the combined equalities of
2556 * "bset" and "context" allow for a compression that can be obtained
2557 * by preapplication of "T".
2558 *
2559 * "bset" itself is not transformed by "T". Instead, the inequalities
2560 * are extracted from "bset" and those are transformed by "T".
2561 * uset_gist_full then determines which of the transformed inequalities
2562 * are redundant with respect to the transformed "context" and removes
2563 * the corresponding inequalities from "bset".
2564 *
2565 * After preapplying "T" to the inequalities, any common factor is
2566 * removed from the coefficients. If this results in a tightening
2567 * of the constant term, then the same tightening is applied to
2568 * the corresponding untransformed inequality in "bset".
2569 * That is, if after plugging in T, a constraint f(x) >= 0 is of the form
2570 *
2571 * g f'(x) + r >= 0
2572 *
2573 * with 0 <= r < g, then it is equivalent to
2574 *
2575 * f'(x) >= 0
2576 *
2577 * This means that f(x) >= 0 is equivalent to f(x) - r >= 0 in the affine
2578 * subspace compressed by T since the latter would be transformed to
2579 *
2580 * g f'(x) >= 0
2581 */
2582static __isl_give isl_basic_setisl_basic_map *uset_gist_compressed(
2583 __isl_take isl_basic_setisl_basic_map *bset, __isl_take isl_basic_setisl_basic_map *context,
2584 __isl_take isl_mat *T)
2585{
2586 isl_ctx *ctx;
2587 isl_mat *ineq;
2588 int i;
2589 isl_size n_row, n_col;
2590 isl_int rem;
2591
2592 ineq = extract_ineq(bset);
2593 ineq = isl_mat_product(ineq, isl_mat_copy(T));
2594 context = isl_basic_set_preimage(context, T);
2595
2596 if (!ineq || !context)
2597 goto error;
2598 if (isl_basic_set_plain_is_empty(context)) {
2599 isl_mat_free(ineq);
2600 isl_basic_set_free(context);
2601 return isl_basic_set_set_to_empty(bset);
2602 }
2603
2604 ctx = isl_mat_get_ctx(ineq);
2605 n_row = isl_mat_rows(ineq);
2606 n_col = isl_mat_cols(ineq);
2607 if (n_row < 0 || n_col < 0)
2608 goto error;
2609 isl_int_init(rem)isl_sioimath_init((rem));
2610 for (i = 0; i < n_row; ++i) {
2611 isl_seq_gcd(ineq->row[i] + 1, n_col - 1, &ctx->normalize_gcd);
2612 if (isl_int_is_zero(ctx->normalize_gcd)(isl_sioimath_sgn(*(ctx->normalize_gcd)) == 0))
2613 continue;
2614 if (isl_int_is_one(ctx->normalize_gcd)(isl_sioimath_cmp_si(*(ctx->normalize_gcd), 1) == 0))
2615 continue;
2616 isl_seq_scale_down(ineq->row[i] + 1, ineq->row[i] + 1,
2617 ctx->normalize_gcd, n_col - 1);
2618 isl_int_fdiv_r(rem, ineq->row[i][0], ctx->normalize_gcd)isl_sioimath_fdiv_r((rem), *(ineq->row[i][0]), *(ctx->normalize_gcd
))
;
2619 isl_int_fdiv_q(ineq->row[i][0],isl_sioimath_fdiv_q((ineq->row[i][0]), *(ineq->row[i][0
]), *(ctx->normalize_gcd))
2620 ineq->row[i][0], ctx->normalize_gcd)isl_sioimath_fdiv_q((ineq->row[i][0]), *(ineq->row[i][0
]), *(ctx->normalize_gcd))
;
2621 if (isl_int_is_zero(rem)(isl_sioimath_sgn(*(rem)) == 0))
2622 continue;
2623 bset = isl_basic_set_cow(bset);
2624 if (!bset)
2625 break;
2626 isl_int_sub(bset->ineq[i][0], bset->ineq[i][0], rem)isl_sioimath_sub((bset->ineq[i][0]), *(bset->ineq[i][0]
), *(rem))
;
2627 }
2628 isl_int_clear(rem)isl_sioimath_clear((rem));
2629
2630 return uset_gist_full(bset, ineq, context);
2631error:
2632 isl_mat_free(ineq);
2633 isl_basic_set_free(context);
2634 isl_basic_set_free(bset);
2635 return NULL((void*)0);
2636}
2637
2638/* Project "bset" onto the variables that are involved in "template".
2639 */
2640static __isl_give isl_basic_setisl_basic_map *project_onto_involved(
2641 __isl_take isl_basic_setisl_basic_map *bset, __isl_keep isl_basic_setisl_basic_map *template)
2642{
2643 int i;
2644 isl_size n;
2645
2646 n = isl_basic_set_dim(template, isl_dim_set);
2647 if (n < 0 || !template)
2648 return isl_basic_set_free(bset);
2649
2650 for (i = 0; i < n; ++i) {
2651 isl_bool involved;
2652
2653 involved = isl_basic_set_involves_dims(template,
2654 isl_dim_set, i, 1);
2655 if (involved < 0)
2656 return isl_basic_set_free(bset);
2657 if (involved)
2658 continue;
2659 bset = isl_basic_set_eliminate_vars(bset, i, 1);
2660 }
2661
2662 return bset;
2663}
2664
2665/* Remove all information from bset that is redundant in the context
2666 * of context. In particular, equalities that are linear combinations
2667 * of those in context are removed. Then the inequalities that are
2668 * redundant in the context of the equalities and inequalities of
2669 * context are removed.
2670 *
2671 * First of all, we drop those constraints from "context"
2672 * that are irrelevant for computing the gist of "bset".
2673 * Alternatively, we could factorize the intersection of "context" and "bset".
2674 *
2675 * We first compute the intersection of the integer affine hulls
2676 * of "bset" and "context",
2677 * compute the gist inside this intersection and then reduce
2678 * the constraints with respect to the equalities of the context
2679 * that only involve variables already involved in the input.
2680 *
2681 * If two constraints are mutually redundant, then uset_gist_full
2682 * will remove the second of those constraints. We therefore first
2683 * sort the constraints so that constraints not involving existentially
2684 * quantified variables are given precedence over those that do.
2685 * We have to perform this sorting before the variable compression,
2686 * because that may effect the order of the variables.
2687 */
2688static __isl_give isl_basic_setisl_basic_map *uset_gist(__isl_take isl_basic_setisl_basic_map *bset,
2689 __isl_take isl_basic_setisl_basic_map *context)
2690{
2691 isl_mat *eq;
2692 isl_mat *T;
2693 isl_basic_setisl_basic_map *aff;
2694 isl_basic_setisl_basic_map *aff_context;
2695 isl_size total;
2696
2697 total = isl_basic_set_dim(bset, isl_dim_all);
2698 if (total < 0 || !context)
2699 goto error;
2700
2701 context = drop_irrelevant_constraints(context, bset);
2702
2703 bset = isl_basic_set_detect_equalities(bset);
2704 aff = isl_basic_set_copy(bset);
2705 aff = isl_basic_set_plain_affine_hull(aff);
2706 context = isl_basic_set_detect_equalities(context);
2707 aff_context = isl_basic_set_copy(context);
2708 aff_context = isl_basic_set_plain_affine_hull(aff_context);
2709 aff = isl_basic_set_intersect(aff, aff_context);
2710 if (!aff)
2711 goto error;
2712 if (isl_basic_set_plain_is_empty(aff)) {
2713 isl_basic_set_free(bset);
2714 isl_basic_set_free(context);
2715 return aff;
2716 }
2717 bset = isl_basic_set_sort_constraints(bset);
2718 if (aff->n_eq == 0) {
2719 isl_basic_set_free(aff);
2720 return uset_gist_uncompressed(bset, context);
2721 }
2722 eq = isl_mat_sub_alloc6(bset->ctx, aff->eq, 0, aff->n_eq, 0, 1 + total);
2723 eq = isl_mat_cow(eq);
2724 T = isl_mat_variable_compression(eq, NULL((void*)0));
2725 isl_basic_set_free(aff);
2726 if (T && T->n_col == 0) {
2727 isl_mat_free(T);
2728 isl_basic_set_free(context);
2729 return isl_basic_set_set_to_empty(bset);
2730 }
2731
2732 aff_context = isl_basic_set_affine_hull(isl_basic_set_copy(context));
2733 aff_context = project_onto_involved(aff_context, bset);
2734
2735 bset = uset_gist_compressed(bset, context, T);
2736 bset = isl_basic_set_reduce_using_equalities(bset, aff_context);
2737
2738 if (bset) {
2739 ISL_F_SET(bset, ISL_BASIC_SET_NO_IMPLICIT)(((bset)->flags) |= ((1 << 2)));
2740 ISL_F_SET(bset, ISL_BASIC_SET_NO_REDUNDANT)(((bset)->flags) |= ((1 << 3)));
2741 }
2742
2743 return bset;
2744error:
2745 isl_basic_set_free(bset);
2746 isl_basic_set_free(context);
2747 return NULL((void*)0);
2748}
2749
2750/* Return the number of equality constraints in "bmap" that involve
2751 * local variables. This function assumes that Gaussian elimination
2752 * has been applied to the equality constraints.
2753 */
2754static int n_div_eq(__isl_keep isl_basic_map *bmap)
2755{
2756 int i;
2757 isl_size total, n_div;
2758
2759 if (!bmap)
2760 return -1;
2761
2762 if (bmap->n_eq == 0)
2763 return 0;
2764
2765 total = isl_basic_map_dim(bmap, isl_dim_all);
2766 n_div = isl_basic_map_dim(bmap, isl_dim_div);
2767 if (total < 0 || n_div < 0)
2768 return -1;
2769 total -= n_div;
2770
2771 for (i = 0; i < bmap->n_eq; ++i)
2772 if (isl_seq_first_non_zero(bmap->eq[i] + 1 + total,
2773 n_div) == -1)
2774 return i;
2775
2776 return bmap->n_eq;
2777}
2778
2779/* Construct a basic map in "space" defined by the equality constraints in "eq".
2780 * The constraints are assumed not to involve any local variables.
2781 */
2782static __isl_give isl_basic_map *basic_map_from_equalities(
2783 __isl_take isl_space *space, __isl_take isl_mat *eq)
2784{
2785 int i, k;
2786 isl_size total;
2787 isl_basic_map *bmap = NULL((void*)0);
2788
2789 total = isl_space_dim(space, isl_dim_all);
2790 if (total < 0 || !eq)
2791 goto error;
2792
2793 if (1 + total != eq->n_col)
2794 isl_die(isl_space_get_ctx(space), isl_error_internal,do { isl_handle_error(isl_space_get_ctx(space), isl_error_internal
, "unexpected number of columns", "/build/llvm-toolchain-snapshot-11~++20200309111110+2c36c23f347/polly/lib/External/isl/isl_map_simplify.c"
, 2795); goto error; } while (0)
2795 "unexpected number of columns", goto error)do { isl_handle_error(isl_space_get_ctx(space), isl_error_internal
, "unexpected number of columns", "/build/llvm-toolchain-snapshot-11~++20200309111110+2c36c23f347/polly/lib/External/isl/isl_map_simplify.c"
, 2795); goto error; } while (0)
;
2796
2797 bmap = isl_basic_map_alloc_space(isl_space_copy(space),
2798 0, eq->n_row, 0);
2799 for (i = 0; i < eq->n_row; ++i) {
2800 k = isl_basic_map_alloc_equality(bmap);
2801 if (k < 0)
2802 goto error;
2803 isl_seq_cpy(bmap->eq[k], eq->row[i], eq->n_col);
2804 }
2805
2806 isl_space_free(space);
2807 isl_mat_free(eq);
2808 return bmap;
2809error:
2810 isl_space_free(space);
2811 isl_mat_free(eq);
2812 isl_basic_map_free(bmap);
2813 return NULL((void*)0);
2814}
2815
2816/* Construct and return a variable compression based on the equality
2817 * constraints in "bmap1" and "bmap2" that do not involve the local variables.
2818 * "n1" is the number of (initial) equality constraints in "bmap1"
2819 * that do involve local variables.
2820 * "n2" is the number of (initial) equality constraints in "bmap2"
2821 * that do involve local variables.
2822 * "total" is the total number of other variables.
2823 * This function assumes that Gaussian elimination
2824 * has been applied to the equality constraints in both "bmap1" and "bmap2"
2825 * such that the equality constraints not involving local variables
2826 * are those that start at "n1" or "n2".
2827 *
2828 * If either of "bmap1" and "bmap2" does not have such equality constraints,
2829 * then simply compute the compression based on the equality constraints
2830 * in the other basic map.
2831 * Otherwise, combine the equality constraints from both into a new
2832 * basic map such that Gaussian elimination can be applied to this combination
2833 * and then construct a variable compression from the resulting
2834 * equality constraints.
2835 */
2836static __isl_give isl_mat *combined_variable_compression(
2837 __isl_keep isl_basic_map *bmap1, int n1,
2838 __isl_keep isl_basic_map *bmap2, int n2, int total)
2839{
2840 isl_ctx *ctx;
2841 isl_mat *E1, *E2, *V;
2842 isl_basic_map *bmap;
2843
2844 ctx = isl_basic_map_get_ctx(bmap1);
2845 if (bmap1->n_eq == n1) {
2846 E2 = isl_mat_sub_alloc6(ctx, bmap2->eq,
2847 n2, bmap2->n_eq - n2, 0, 1 + total);
2848 return isl_mat_variable_compression(E2, NULL((void*)0));
2849 }
2850 if (bmap2->n_eq == n2) {
2851 E1 = isl_mat_sub_alloc6(ctx, bmap1->eq,
2852 n1, bmap1->n_eq - n1, 0, 1 + total);
2853 return isl_mat_variable_compression(E1, NULL((void*)0));
2854 }
2855 E1 = isl_mat_sub_alloc6(ctx, bmap1->eq,
2856 n1, bmap1->n_eq - n1, 0, 1 + total);
2857 E2 = isl_mat_sub_alloc6(ctx, bmap2->eq,
2858 n2, bmap2->n_eq - n2, 0, 1 + total);
2859 E1 = isl_mat_concat(E1, E2);
2860 bmap = basic_map_from_equalities(isl_basic_map_get_space(bmap1), E1);
2861 bmap = isl_basic_map_gauss(bmap, NULL((void*)0));
2862 if (!bmap)
2863 return NULL((void*)0);
2864 E1 = isl_mat_sub_alloc6(ctx, bmap->eq, 0, bmap->n_eq, 0, 1 + total);
2865 V = isl_mat_variable_compression(E1, NULL((void*)0));
2866 isl_basic_map_free(bmap);
2867
2868 return V;
2869}
2870
2871/* Extract the stride constraints from "bmap", compressed
2872 * with respect to both the stride constraints in "context" and
2873 * the remaining equality constraints in both "bmap" and "context".
2874 * "bmap_n_eq" is the number of (initial) stride constraints in "bmap".
2875 * "context_n_eq" is the number of (initial) stride constraints in "context".
2876 *
2877 * Let x be all variables in "bmap" (and "context") other than the local
2878 * variables. First compute a variable compression
2879 *
2880 * x = V x'
2881 *
2882 * based on the non-stride equality constraints in "bmap" and "context".
2883 * Consider the stride constraints of "context",
2884 *
2885 * A(x) + B(y) = 0
2886 *
2887 * with y the local variables and plug in the variable compression,
2888 * resulting in
2889 *
2890 * A(V x') + B(y) = 0
2891 *
2892 * Use these constraints to compute a parameter compression on x'
2893 *
2894 * x' = T x''
2895 *
2896 * Now consider the stride constraints of "bmap"
2897 *
2898 * C(x) + D(y) = 0
2899 *
2900 * and plug in x = V*T x''.
2901 * That is, return A = [C*V*T D].
2902 */
2903static __isl_give isl_mat *extract_compressed_stride_constraints(
2904 __isl_keep isl_basic_map *bmap, int bmap_n_eq,
2905 __isl_keep isl_basic_map *context, int context_n_eq)
2906{
2907 isl_size total, n_div;
2908 isl_ctx *ctx;
2909 isl_mat *A, *B, *T, *V;
2910
2911 total = isl_basic_map_dim(context, isl_dim_all);
2912 n_div = isl_basic_map_dim(context, isl_dim_div);
2913 if (total < 0 || n_div < 0)
2914 return NULL((void*)0);
2915 total -= n_div;
2916
2917 ctx = isl_basic_map_get_ctx(bmap);
2918
2919 V = combined_variable_compression(bmap, bmap_n_eq,
2920 context, context_n_eq, total);
2921
2922 A = isl_mat_sub_alloc6(ctx, context->eq, 0, context_n_eq, 0, 1 + total);
2923 B = isl_mat_sub_alloc6(ctx, context->eq,
2924 0, context_n_eq, 1 + total, n_div);
2925 A = isl_mat_product(A, isl_mat_copy(V));
2926 T = isl_mat_parameter_compression_ext(A, B);
2927 T = isl_mat_product(V, T);
2928
2929 n_div = isl_basic_map_dim(bmap, isl_dim_div);
2930 if (n_div < 0)
2931 T = isl_mat_free(T);
2932 else
2933 T = isl_mat_diagonal(T, isl_mat_identity(ctx, n_div));
2934
2935 A = isl_mat_sub_alloc6(ctx, bmap->eq,
2936 0, bmap_n_eq, 0, 1 + total + n_div);
2937 A = isl_mat_product(A, T);
2938
2939 return A;
2940}
2941
2942/* Remove the prime factors from *g that have an exponent that
2943 * is strictly smaller than the exponent in "c".
2944 * All exponents in *g are known to be smaller than or equal
2945 * to those in "c".
2946 *
2947 * That is, if *g is equal to
2948 *
2949 * p_1^{e_1} p_2^{e_2} ... p_n^{e_n}
2950 *
2951 * and "c" is equal to
2952 *
2953 * p_1^{f_1} p_2^{f_2} ... p_n^{f_n}
2954 *
2955 * then update *g to
2956 *
2957 * p_1^{e_1 * (e_1 = f_1)} p_2^{e_2 * (e_2 = f_2)} ...
2958 * p_n^{e_n * (e_n = f_n)}
2959 *
2960 * If e_i = f_i, then c / *g does not have any p_i factors and therefore
2961 * neither does the gcd of *g and c / *g.
2962 * If e_i < f_i, then the gcd of *g and c / *g has a positive
2963 * power min(e_i, s_i) of p_i with s_i = f_i - e_i among its factors.
2964 * Dividing *g by this gcd therefore strictly reduces the exponent
2965 * of the prime factors that need to be removed, while leaving the
2966 * other prime factors untouched.
2967 * Repeating this process until gcd(*g, c / *g) = 1 therefore
2968 * removes all undesired factors, without removing any others.
2969 */
2970static void remove_incomplete_powers(isl_int *g, isl_int c)
2971{
2972 isl_int t;
2973
2974 isl_int_init(t)isl_sioimath_init((t));
2975 for (;;) {
2976 isl_int_divexact(t, c, *g)isl_sioimath_tdiv_q((t), *(c), *(*g));
2977 isl_int_gcd(t, t, *g)isl_sioimath_gcd((t), *(t), *(*g));
2978 if (isl_int_is_one(t)(isl_sioimath_cmp_si(*(t), 1) == 0))
2979 break;
2980 isl_int_divexact(*g, *g, t)isl_sioimath_tdiv_q((*g), *(*g), *(t));
2981 }
2982 isl_int_clear(t)isl_sioimath_clear((t));
2983}
2984
2985/* Reduce the "n" stride constraints in "bmap" based on a copy "A"
2986 * of the same stride constraints in a compressed space that exploits
2987 * all equalities in the context and the other equalities in "bmap".
2988 *
2989 * If the stride constraints of "bmap" are of the form
2990 *
2991 * C(x) + D(y) = 0
2992 *
2993 * then A is of the form
2994 *
2995 * B(x') + D(y) = 0
2996 *
2997 * If any of these constraints involves only a single local variable y,
2998 * then the constraint appears as
2999 *
3000 * f(x) + m y_i = 0
3001 *
3002 * in "bmap" and as
3003 *
3004 * h(x') + m y_i = 0
3005 *
3006 * in "A".
3007 *
3008 * Let g be the gcd of m and the coefficients of h.
3009 * Then, in particular, g is a divisor of the coefficients of h and
3010 *
3011 * f(x) = h(x')
3012 *
3013 * is known to be a multiple of g.
3014 * If some prime factor in m appears with the same exponent in g,
3015 * then it can be removed from m because f(x) is already known
3016 * to be a multiple of g and therefore in particular of this power
3017 * of the prime factors.
3018 * Prime factors that appear with a smaller exponent in g cannot
3019 * be removed from m.
3020 * Let g' be the divisor of g containing all prime factors that
3021 * appear with the same exponent in m and g, then
3022 *
3023 * f(x) + m y_i = 0
3024 *
3025 * can be replaced by
3026 *
3027 * f(x) + m/g' y_i' = 0
3028 *
3029 * Note that (if g' != 1) this changes the explicit representation
3030 * of y_i to that of y_i', so the integer division at position i
3031 * is marked unknown and later recomputed by a call to
3032 * isl_basic_map_gauss.
3033 */
3034static __isl_give isl_basic_map *reduce_stride_constraints(
3035 __isl_take isl_basic_map *bmap, int n, __isl_keep isl_mat *A)
3036{
3037 int i;
3038 isl_size total, n_div;
3039 int any = 0;
3040 isl_int gcd;
3041
3042 total = isl_basic_map_dim(bmap, isl_dim_all);
3043 n_div = isl_basic_map_dim(bmap, isl_dim_div);
3044 if (total < 0 || n_div < 0 || !A)
3045 return isl_basic_map_free(bmap);
3046 total -= n_div;
3047
3048 isl_int_init(gcd)isl_sioimath_init((gcd));
3049 for (i = 0; i < n; ++i) {
3050 int div;
3051
3052 div = isl_seq_first_non_zero(bmap->eq[i] + 1 + total, n_div);
3053 if (div < 0)
3054 isl_die(isl_basic_map_get_ctx(bmap), isl_error_internal,do { isl_handle_error(isl_basic_map_get_ctx(bmap), isl_error_internal
, "equality constraints modified unexpectedly", "/build/llvm-toolchain-snapshot-11~++20200309111110+2c36c23f347/polly/lib/External/isl/isl_map_simplify.c"
, 3056); goto error; } while (0)
3055 "equality constraints modified unexpectedly",do { isl_handle_error(isl_basic_map_get_ctx(bmap), isl_error_internal
, "equality constraints modified unexpectedly", "/build/llvm-toolchain-snapshot-11~++20200309111110+2c36c23f347/polly/lib/External/isl/isl_map_simplify.c"
, 3056); goto error; } while (0)
3056 goto error)do { isl_handle_error(isl_basic_map_get_ctx(bmap), isl_error_internal
, "equality constraints modified unexpectedly", "/build/llvm-toolchain-snapshot-11~++20200309111110+2c36c23f347/polly/lib/External/isl/isl_map_simplify.c"
, 3056); goto error; } while (0)
;
3057 if (isl_seq_first_non_zero(bmap->eq[i] + 1 + total + div + 1,
3058 n_div - div - 1) != -1)
3059 continue;
3060 if (isl_mat_row_gcd(A, i, &gcd) < 0)
3061 goto error;
3062 if (isl_int_is_one(gcd)(isl_sioimath_cmp_si(*(gcd), 1) == 0))
3063 continue;
3064 remove_incomplete_powers(&gcd, bmap->eq[i][1 + total + div]);
3065 if (isl_int_is_one(gcd)(isl_sioimath_cmp_si(*(gcd), 1) == 0))
3066 continue;
3067 isl_int_divexact(bmap->eq[i][1 + total + div],isl_sioimath_tdiv_q((bmap->eq[i][1 + total + div]), *(bmap
->eq[i][1 + total + div]), *(gcd))
3068 bmap->eq[i][1 + total + div], gcd)isl_sioimath_tdiv_q((bmap->eq[i][1 + total + div]), *(bmap
->eq[i][1 + total + div]), *(gcd))
;
3069 bmap = isl_basic_map_mark_div_unknown(bmap, div);
3070 if (!bmap)
3071 goto error;
3072 any = 1;
3073 }
3074 isl_int_clear(gcd)isl_sioimath_clear((gcd));
3075
3076 if (any)
3077 bmap = isl_basic_map_gauss(bmap, NULL((void*)0));
3078
3079 return bmap;
3080error:
3081 isl_int_clear(gcd)isl_sioimath_clear((gcd));
3082 isl_basic_map_free(bmap);
3083 return NULL((void*)0);
3084}
3085
3086/* Simplify the stride constraints in "bmap" based on
3087 * the remaining equality constraints in "bmap" and all equality
3088 * constraints in "context".
3089 * Only do this if both "bmap" and "context" have stride constraints.
3090 *
3091 * First extract a copy of the stride constraints in "bmap" in a compressed
3092 * space exploiting all the other equality constraints and then
3093 * use this compressed copy to simplify the original stride constraints.
3094 */
3095static __isl_give isl_basic_map *gist_strides(__isl_take isl_basic_map *bmap,
3096 __isl_keep isl_basic_map *context)
3097{
3098 int bmap_n_eq, context_n_eq;
3099 isl_mat *A;
3100
3101 if (!bmap || !context)
3102 return isl_basic_map_free(bmap);
3103
3104 bmap_n_eq = n_div_eq(bmap);
3105 context_n_eq = n_div_eq(context);
3106
3107 if (bmap_n_eq < 0 || context_n_eq < 0)
3108 return isl_basic_map_free(bmap);
3109 if (bmap_n_eq == 0 || context_n_eq == 0)
3110 return bmap;
3111
3112 A = extract_compressed_stride_constraints(bmap, bmap_n_eq,
3113 context, context_n_eq);
3114 bmap = reduce_stride_constraints(bmap, bmap_n_eq, A);
3115
3116 isl_mat_free(A);
3117
3118 return bmap;
3119}
3120
3121/* Return a basic map that has the same intersection with "context" as "bmap"
3122 * and that is as "simple" as possible.
3123 *
3124 * The core computation is performed on the pure constraints.
3125 * When we add back the meaning of the integer divisions, we need
3126 * to (re)introduce the div constraints. If we happen to have
3127 * discovered that some of these integer divisions are equal to
3128 * some affine combination of other variables, then these div
3129 * constraints may end up getting simplified in terms of the equalities,
3130 * resulting in extra inequalities on the other variables that
3131 * may have been removed already or that may not even have been
3132 * part of the input. We try and remove those constraints of
3133 * this form that are most obviously redundant with respect to
3134 * the context. We also remove those div constraints that are
3135 * redundant with respect to the other constraints in the result.
3136 *
3137 * The stride constraints among the equality constraints in "bmap" are
3138 * also simplified with respecting to the other equality constraints
3139 * in "bmap" and with respect to all equality constraints in "context".
3140 */
3141__isl_give isl_basic_map *isl_basic_map_gist(__isl_take isl_basic_map *bmap,
3142 __isl_take isl_basic_map *context)
3143{
3144 isl_basic_setisl_basic_map *bset, *eq;
3145 isl_basic_map *eq_bmap;
3146 isl_size total, n_div, n_div_bmap;
3147 unsigned extra, n_eq, n_ineq;
3148
3149 if (!bmap || !context)
3150 goto error;
3151
3152 if (isl_basic_map_plain_is_universe(bmap)) {
3153 isl_basic_map_free(context);
3154 return bmap;
3155 }
3156 if (isl_basic_map_plain_is_empty(context)) {
3157 isl_space *space = isl_basic_map_get_space(bmap);
3158 isl_basic_map_free(bmap);
3159 isl_basic_map_free(context);
3160 return isl_basic_map_universe(space);
3161 }
3162 if (isl_basic_map_plain_is_empty(bmap)) {
3163 isl_basic_map_free(context);
3164 return bmap;
3165 }
3166
3167 bmap = isl_basic_map_remove_redundancies(bmap);
3168 context = isl_basic_map_remove_redundancies(context);
3169 context = isl_basic_map_align_divs(context, bmap);
3170
3171 n_div = isl_basic_map_dim(context, isl_dim_div);
3172 total = isl_basic_map_dim(bmap, isl_dim_all);
3173 n_div_bmap = isl_basic_map_dim(bmap, isl_dim_div);
3174 if (n_div < 0 || total < 0 || n_div_bmap < 0)
3175 goto error;
3176 extra = n_div - n_div_bmap;
3177
3178 bset = isl_basic_map_underlying_set(isl_basic_map_copy(bmap));
3179 bset = isl_basic_set_add_dims(bset, isl_dim_set, extra);
3180 bset = uset_gist(bset,
3181 isl_basic_map_underlying_set(isl_basic_map_copy(context)));
3182 bset = isl_basic_set_project_out(bset, isl_dim_set, total, extra);
3183
3184 if (!bset || bset->n_eq == 0 || n_div == 0 ||
3185 isl_basic_set_plain_is_empty(bset)) {
3186 isl_basic_map_free(context);
3187 return isl_basic_map_overlying_set(bset, bmap);
3188 }
3189
3190 n_eq = bset->n_eq;
3191 n_ineq = bset->n_ineq;
3192 eq = isl_basic_set_copy(bset);
3193 eq = isl_basic_set_cow(eq);
3194 eq = isl_basic_set_free_inequality(eq, n_ineq);
3195 bset = isl_basic_set_free_equality(bset, n_eq);
3196
3197 eq_bmap = isl_basic_map_overlying_set(eq, isl_basic_map_copy(bmap));
3198 eq_bmap = gist_strides(eq_bmap, context);
3199 eq_bmap = isl_basic_map_remove_shifted_constraints(eq_bmap, context);
3200 bmap = isl_basic_map_overlying_set(bset, bmap);
3201 bmap = isl_basic_map_intersect(bmap, eq_bmap);
3202 bmap = isl_basic_map_remove_redundancies(bmap);
3203
3204 return bmap;
3205error:
3206 isl_basic_map_free(bmap);
3207 isl_basic_map_free(context);
3208 return NULL((void*)0);
3209}
3210
3211/*
3212 * Assumes context has no implicit divs.
3213 */
3214__isl_give isl_map *isl_map_gist_basic_map(__isl_take isl_map *map,
3215 __isl_take isl_basic_map *context)
3216{
3217 int i;
3218
3219 if (!map || !context)
3220 goto error;
3221
3222 if (isl_basic_map_plain_is_empty(context)) {
3223 isl_space *space = isl_map_get_space(map);
3224 isl_map_free(map);
3225 isl_basic_map_free(context);
3226 return isl_map_universe(space);
3227 }
3228
3229 context = isl_basic_map_remove_redundancies(context);
3230 map = isl_map_cow(map);
3231 if (isl_map_basic_map_check_equal_space(map, context) < 0)
3232 goto error;
3233 map = isl_map_compute_divs(map);
3234 if (!map)
3235 goto error;
3236 for (i = map->n - 1; i >= 0; --i) {
3237 map->p[i] = isl_basic_map_gist(map->p[i],
3238 isl_basic_map_copy(context));
3239 if (!map->p[i])
3240 goto error;
3241 if (isl_basic_map_plain_is_empty(map->p[i])) {
3242 isl_basic_map_free(map->p[i]);
3243 if (i != map->n - 1)
3244 map->p[i] = map->p[map->n - 1];
3245 map->n--;
3246 }
3247 }
3248 isl_basic_map_free(context);
3249 ISL_F_CLR(map, ISL_MAP_NORMALIZED)(((map)->flags) &= ~((1 << 1)));
3250 return map;
3251error:
3252 isl_map_free(map);
3253 isl_basic_map_free(context);
3254 return NULL((void*)0);
3255}
3256
3257/* Drop all inequalities from "bmap" that also appear in "context".
3258 * "context" is assumed to have only known local variables and
3259 * the initial local variables of "bmap" are assumed to be the same
3260 * as those of "context".
3261 * The constraints of both "bmap" and "context" are assumed
3262 * to have been sorted using isl_basic_map_sort_constraints.
3263 *
3264 * Run through the inequality constraints of "bmap" and "context"
3265 * in sorted order.
3266 * If a constraint of "bmap" involves variables not in "context",
3267 * then it cannot appear in "context".
3268 * If a matching constraint is found, it is removed from "bmap".
3269 */
3270static __isl_give isl_basic_map *drop_inequalities(
3271 __isl_take isl_basic_map *bmap, __isl_keep isl_basic_map *context)
3272{
3273 int i1, i2;
3274 isl_size total, bmap_total;
3275 unsigned extra;
3276
3277 total = isl_basic_map_dim(context, isl_dim_all);
3278 bmap_total = isl_basic_map_dim(bmap, isl_dim_all);
3279 if (total < 0 || bmap_total < 0)
3280 return isl_basic_map_free(bmap);
3281
3282 extra = bmap_total - total;
3283
3284 i1 = bmap->n_ineq - 1;
3285 i2 = context->n_ineq - 1;
3286 while (bmap && i1 >= 0 && i2 >= 0) {
3287 int cmp;
3288
3289 if (isl_seq_first_non_zero(bmap->ineq[i1] + 1 + total,
3290 extra) != -1) {
3291 --i1;
3292 continue;
3293 }
3294 cmp = isl_basic_map_constraint_cmp(context, bmap->ineq[i1],
3295 context->ineq[i2]);
3296 if (cmp < 0) {
3297 --i2;
3298 continue;
3299 }
3300 if (cmp > 0) {
3301 --i1;
3302 continue;
3303 }
3304 if (isl_int_eq(bmap->ineq[i1][0], context->ineq[i2][0])(isl_sioimath_cmp(*(bmap->ineq[i1][0]), *(context->ineq
[i2][0])) == 0)
) {
3305 bmap = isl_basic_map_cow(bmap);
3306 if (isl_basic_map_drop_inequality(bmap, i1) < 0)
3307 bmap = isl_basic_map_free(bmap);
3308 }
3309 --i1;
3310 --i2;
3311 }
3312
3313 return bmap;
3314}
3315
3316/* Drop all equalities from "bmap" that also appear in "context".
3317 * "context" is assumed to have only known local variables and
3318 * the initial local variables of "bmap" are assumed to be the same
3319 * as those of "context".
3320 *
3321 * Run through the equality constraints of "bmap" and "context"
3322 * in sorted order.
3323 * If a constraint of "bmap" involves variables not in "context",
3324 * then it cannot appear in "context".
3325 * If a matching constraint is found, it is removed from "bmap".
3326 */
3327static __isl_give isl_basic_map *drop_equalities(
3328 __isl_take isl_basic_map *bmap, __isl_keep isl_basic_map *context)
3329{
3330 int i1, i2;
3331 isl_size total, bmap_total;
3332 unsigned extra;
3333
3334 total = isl_basic_map_dim(context, isl_dim_all);
3335 bmap_total = isl_basic_map_dim(bmap, isl_dim_all);
3336 if (total < 0 || bmap_total < 0)
3337 return isl_basic_map_free(bmap);
3338
3339 extra = bmap_total - total;
3340
3341 i1 = bmap->n_eq - 1;
3342 i2 = context->n_eq - 1;
3343
3344 while (bmap && i1 >= 0 && i2 >= 0) {
3345 int last1, last2;
3346
3347 if (isl_seq_first_non_zero(bmap->eq[i1] + 1 + total,
3348 extra) != -1)
3349 break;
3350 last1 = isl_seq_last_non_zero(bmap->eq[i1] + 1, total);
3351 last2 = isl_seq_last_non_zero(context->eq[i2] + 1, total);
3352 if (last1 > last2) {
3353 --i2;
3354 continue;
3355 }
3356 if (last1 < last2) {
3357 --i1;
3358 continue;
3359 }
3360 if (isl_seq_eq(bmap->eq[i1], context->eq[i2], 1 + total)) {
3361 bmap = isl_basic_map_cow(bmap);
3362 if (isl_basic_map_drop_equality(bmap, i1) < 0)
3363 bmap = isl_basic_map_free(bmap);
3364 }
3365 --i1;
3366 --i2;
3367 }
3368
3369 return bmap;
3370}
3371
3372/* Remove the constraints in "context" from "bmap".
3373 * "context" is assumed to have explicit representations
3374 * for all local variables.
3375 *
3376 * First align the divs of "bmap" to those of "context" and
3377 * sort the constraints. Then drop all constraints from "bmap"
3378 * that appear in "context".
3379 */
3380__isl_give isl_basic_map *isl_basic_map_plain_gist(
3381 __isl_take isl_basic_map *bmap, __isl_take isl_basic_map *context)
3382{
3383 isl_bool done, known;
3384
3385 done = isl_basic_map_plain_is_universe(context);
3386 if (done == isl_bool_false)
3387 done = isl_basic_map_plain_is_universe(bmap);
3388 if (done == isl_bool_false)
3389 done = isl_basic_map_plain_is_empty(context);
3390 if (done == isl_bool_false)
3391 done = isl_basic_map_plain_is_empty(bmap);
3392 if (done < 0)
3393 goto error;
3394 if (done) {
3395 isl_basic_map_free(context);
3396 return bmap;
3397 }
3398 known = isl_basic_map_divs_known(context);
3399 if (known < 0)
3400 goto error;
3401 if (!known)
3402 isl_die(isl_basic_map_get_ctx(bmap), isl_error_invalid,do { isl_handle_error(isl_basic_map_get_ctx(bmap), isl_error_invalid
, "context has unknown divs", "/build/llvm-toolchain-snapshot-11~++20200309111110+2c36c23f347/polly/lib/External/isl/isl_map_simplify.c"
, 3403); goto error; } while (0)
3403 "context has unknown divs", goto error)do { isl_handle_error(isl_basic_map_get_ctx(bmap), isl_error_invalid
, "context has unknown divs", "/build/llvm-toolchain-snapshot-11~++20200309111110+2c36c23f347/polly/lib/External/isl/isl_map_simplify.c"
, 3403); goto error; } while (0)
;
3404
3405 bmap = isl_basic_map_align_divs(bmap, context);
3406 bmap = isl_basic_map_gauss(bmap, NULL((void*)0));
3407 bmap = isl_basic_map_sort_constraints(bmap);
3408 context = isl_basic_map_sort_constraints(context);
3409
3410 bmap = drop_inequalities(bmap, context);
3411 bmap = drop_equalities(bmap, context);
3412
3413 isl_basic_map_free(context);
3414 bmap = isl_basic_map_finalize(bmap);
3415 return bmap;
3416error:
3417 isl_basic_map_free(bmap);
3418 isl_basic_map_free(context);
3419 return NULL((void*)0);
3420}
3421
3422/* Replace "map" by the disjunct at position "pos" and free "context".
3423 */
3424static __isl_give isl_map *replace_by_disjunct(__isl_take isl_map *map,
3425 int pos, __isl_take isl_basic_map *context)
3426{
3427 isl_basic_map *bmap;
3428
3429 bmap = isl_basic_map_copy(map->p[pos]);
3430 isl_map_free(map);
3431 isl_basic_map_free(context);
3432 return isl_map_from_basic_map(bmap);
3433}
3434
3435/* Remove the constraints in "context" from "map".
3436 * If any of the disjuncts in the result turns out to be the universe,
3437 * then return this universe.
3438 * "context" is assumed to have explicit representations
3439 * for all local variables.
3440 */
3441__isl_give isl_map *isl_map_plain_gist_basic_map(__isl_take isl_map *map,
3442 __isl_take isl_basic_map *context)
3443{
3444 int i;
3445 isl_bool univ, known;
3446
3447 univ = isl_basic_map_plain_is_universe(context);
3448 if (univ < 0)
3449 goto error;
3450 if (univ) {
3451 isl_basic_map_free(context);
3452 return map;
3453 }
3454 known = isl_basic_map_divs_known(context);
3455 if (known < 0)
3456 goto error;
3457 if (!known)
3458 isl_die(isl_map_get_ctx(map), isl_error_invalid,do { isl_handle_error(isl_map_get_ctx(map), isl_error_invalid
, "context has unknown divs", "/build/llvm-toolchain-snapshot-11~++20200309111110+2c36c23f347/polly/lib/External/isl/isl_map_simplify.c"
, 3459); goto error; } while (0)
3459 "context has unknown divs", goto error)do { isl_handle_error(isl_map_get_ctx(map), isl_error_invalid
, "context has unknown divs", "/build/llvm-toolchain-snapshot-11~++20200309111110+2c36c23f347/polly/lib/External/isl/isl_map_simplify.c"
, 3459); goto error; } while (0)
;
3460
3461 map = isl_map_cow(map);
3462 if (!map)
3463 goto error;
3464 for (i = 0; i < map->n; ++i) {
3465 map->p[i] = isl_basic_map_plain_gist(map->p[i],
3466 isl_basic_map_copy(context));
3467 univ = isl_basic_map_plain_is_universe(map->p[i]);
3468 if (univ < 0)
3469 goto error;
3470 if (univ && map->n > 1)
3471 return replace_by_disjunct(map, i, context);
3472 }
3473
3474 isl_basic_map_free(context);
3475 ISL_F_CLR(map, ISL_MAP_NORMALIZED)(((map)->flags) &= ~((1 << 1)));
3476 if (map->n > 1)
3477 ISL_F_CLR(map, ISL_MAP_DISJOINT)(((map)->flags) &= ~((1 << 0)));
3478 return map;
3479error:
3480 isl_map_free(map);
3481 isl_basic_map_free(context);
3482 return NULL((void*)0);
3483}
3484
3485/* Remove the constraints in "context" from "set".
3486 * If any of the disjuncts in the result turns out to be the universe,
3487 * then return this universe.
3488 * "context" is assumed to have explicit representations
3489 * for all local variables.
3490 */
3491__isl_give isl_setisl_map *isl_set_plain_gist_basic_set(__isl_take isl_setisl_map *set,
3492 __isl_take isl_basic_setisl_basic_map *context)
3493{
3494 return set_from_map(isl_map_plain_gist_basic_map(set_to_map(set),
3495 bset_to_bmap(context)));
3496}
3497
3498/* Remove the constraints in "context" from "map".
3499 * If any of the disjuncts in the result turns out to be the universe,
3500 * then return this universe.
3501 * "context" is assumed to consist of a single disjunct and
3502 * to have explicit representations for all local variables.
3503 */
3504__isl_give isl_map *isl_map_plain_gist(__isl_take isl_map *map,
3505 __isl_take isl_map *context)
3506{
3507 isl_basic_map *hull;
3508
3509 hull = isl_map_unshifted_simple_hull(context);
3510 return isl_map_plain_gist_basic_map(map, hull);
3511}
3512
3513/* Replace "map" by a universe map in the same space and free "drop".
3514 */
3515static __isl_give isl_map *replace_by_universe(__isl_take isl_map *map,
3516 __isl_take isl_map *drop)
3517{
3518 isl_map *res;
3519
3520 res = isl_map_universe(isl_map_get_space(map));
3521 isl_map_free(map);
3522 isl_map_free(drop);
3523 return res;
3524}
3525
3526/* Return a map that has the same intersection with "context" as "map"
3527 * and that is as "simple" as possible.
3528 *
3529 * If "map" is already the universe, then we cannot make it any simpler.
3530 * Similarly, if "context" is the universe, then we cannot exploit it
3531 * to simplify "map"
3532 * If "map" and "context" are identical to each other, then we can
3533 * return the corresponding universe.
3534 *
3535 * If either "map" or "context" consists of multiple disjuncts,
3536 * then check if "context" happens to be a subset of "map",
3537 * in which case all constraints can be removed.
3538 * In case of multiple disjuncts, the standard procedure
3539 * may not be able to detect that all constraints can be removed.
3540 *
3541 * If none of these cases apply, we have to work a bit harder.
3542 * During this computation, we make use of a single disjunct context,
3543 * so if the original context consists of more than one disjunct
3544 * then we need to approximate the context by a single disjunct set.
3545 * Simply taking the simple hull may drop constraints that are
3546 * only implicitly available in each disjunct. We therefore also
3547 * look for constraints among those defining "map" that are valid
3548 * for the context. These can then be used to simplify away
3549 * the corresponding constraints in "map".
3550 */
3551__isl_give isl_map *isl_map_gist(__isl_take isl_map *map,
3552 __isl_take isl_map *context)
3553{
3554 int equal;
3555 int is_universe;
3556 isl_size n_disjunct_map, n_disjunct_context;
3557 isl_bool subset;
3558 isl_basic_map *hull;
3559
3560 is_universe = isl_map_plain_is_universe(map);
3561 if (is_universe >= 0 && !is_universe)
3562 is_universe = isl_map_plain_is_universe(context);
3563 if (is_universe < 0)
3564 goto error;
3565 if (is_universe) {
3566 isl_map_free(context);
3567 return map;
3568 }
3569
3570 isl_map_align_params_bin(&map, &context);
3571 equal = isl_map_plain_is_equal(map, context);
3572 if (equal < 0)
3573 goto error;
3574 if (equal)
3575 return replace_by_universe(map, context);
3576
3577 n_disjunct_map = isl_map_n_basic_map(map);
3578 n_disjunct_context = isl_map_n_basic_map(context);
3579 if (n_disjunct_map < 0 || n_disjunct_context < 0)
3580 goto error;
3581 if (n_disjunct_map != 1 || n_disjunct_context != 1) {
3582 subset = isl_map_is_subset(context, map);
3583 if (subset < 0)
3584 goto error;
3585 if (subset)
3586 return replace_by_universe(map, context);
3587 }
3588
3589 context = isl_map_compute_divs(context);
3590 if (!context)
3591 goto error;
3592 if (n_disjunct_context == 1) {
3593 hull = isl_map_simple_hull(context);
3594 } else {
3595 isl_ctx *ctx;
3596 isl_map_list *list;
3597
3598 ctx = isl_map_get_ctx(map);
3599 list = isl_map_list_alloc(ctx, 2);
3600 list = isl_map_list_add(list, isl_map_copy(context));
3601 list = isl_map_list_add(list, isl_map_copy(map));
3602 hull = isl_map_unshifted_simple_hull_from_map_list(context,
3603 list);
3604 }
3605 return isl_map_gist_basic_map(map, hull);
3606error:
3607 isl_map_free(map);
3608 isl_map_free(context);
3609 return NULL((void*)0);
3610}
3611
3612struct isl_basic_setisl_basic_map *isl_basic_set_gist(struct isl_basic_setisl_basic_map *bset,
3613 struct isl_basic_setisl_basic_map *context)
3614{
3615 return bset_from_bmap(isl_basic_map_gist(bset_to_bmap(bset),
3616 bset_to_bmap(context)));
3617}
3618
3619__isl_give isl_setisl_map *isl_set_gist_basic_set(__isl_take isl_setisl_map *set,
3620 __isl_take isl_basic_setisl_basic_map *context)
3621{
3622 return set_from_map(isl_map_gist_basic_map(set_to_map(set),
3623 bset_to_bmap(context)));
3624}
3625
3626__isl_give isl_setisl_map *isl_set_gist_params_basic_set(__isl_take isl_setisl_map *set,
3627 __isl_take isl_basic_setisl_basic_map *context)
3628{
3629 isl_space *space = isl_set_get_space(set);
3630 isl_basic_setisl_basic_map *dom_context = isl_basic_set_universe(space);
3631 dom_context = isl_basic_set_intersect_params(dom_context, context);
3632 return isl_set_gist_basic_set(set, dom_context);
3633}
3634
3635__isl_give isl_setisl_map *isl_set_gist(__isl_take isl_setisl_map *set,
3636 __isl_take isl_setisl_map *context)
3637{
3638 return set_from_map(isl_map_gist(set_to_map(set), set_to_map(context)));
3639}
3640
3641/* Compute the gist of "bmap" with respect to the constraints "context"
3642 * on the domain.
3643 */
3644__isl_give isl_basic_map *isl_basic_map_gist_domain(
3645 __isl_take isl_basic_map *bmap, __isl_take isl_basic_setisl_basic_map *context)
3646{
3647 isl_space *space = isl_basic_map_get_space(bmap);
3648 isl_basic_map *bmap_context = isl_basic_map_universe(space);
3649
3650 bmap_context = isl_basic_map_intersect_domain(bmap_context, context);
3651 return isl_basic_map_gist(bmap, bmap_context);
3652}
3653
3654__isl_give isl_map *isl_map_gist_domain(__isl_take isl_map *map,
3655 __isl_take isl_setisl_map *context)
3656{
3657 isl_map *map_context = isl_map_universe(isl_map_get_space(map));
3658 map_context = isl_map_intersect_domain(map_context, context);
3659 return isl_map_gist(map, map_context);
3660}
3661
3662__isl_give isl_map *isl_map_gist_range(__isl_take isl_map *map,
3663 __isl_take isl_setisl_map *context)
3664{
3665 isl_map *map_context = isl_map_universe(isl_map_get_space(map));
3666 map_context = isl_map_intersect_range(map_context, context);
3667 return isl_map_gist(map, map_context);
3668}
3669
3670__isl_give isl_map *isl_map_gist_params(__isl_take isl_map *map,
3671 __isl_take isl_setisl_map *context)
3672{
3673 isl_map *map_context = isl_map_universe(isl_map_get_space(map));
3674 map_context = isl_map_intersect_params(map_context, context);
3675 return isl_map_gist(map, map_context);
3676}
3677
3678__isl_give isl_setisl_map *isl_set_gist_params(__isl_take isl_setisl_map *set,
3679 __isl_take isl_setisl_map *context)
3680{
3681 return isl_map_gist_params(set, context);
3682}
3683
3684/* Quick check to see if two basic maps are disjoint.
3685 * In particular, we reduce the equalities and inequalities of
3686 * one basic map in the context of the equalities of the other
3687 * basic map and check if we get a contradiction.
3688 */
3689isl_bool isl_basic_map_plain_is_disjoint(__isl_keep isl_basic_map *bmap1,
3690 __isl_keep isl_basic_map *bmap2)
3691{
3692 struct isl_vec *v = NULL((void*)0);
3693 int *elim = NULL((void*)0);
3694 isl_size total;
3695 int i;
3696
3697 if (isl_basic_map_check_equal_space(bmap1, bmap2) < 0)
3698 return isl_bool_error;
3699 if (bmap1->n_div || bmap2->n_div)
3700 return isl_bool_false;
3701 if (!bmap1->n_eq && !bmap2->n_eq)
3702 return isl_bool_false;
3703
3704 total = isl_space_dim(bmap1->dim, isl_dim_all);
3705 if (total < 0)
3706 return isl_bool_error;
3707 if (total == 0)
3708 return isl_bool_false;
3709 v = isl_vec_alloc(bmap1->ctx, 1 + total);
3710 if (!v)
3711 goto error;
3712 elim = isl_alloc_array(bmap1->ctx, int, total)((int *)isl_malloc_or_die(bmap1->ctx, (total)*sizeof(int))
)
;
3713 if (!elim)
3714 goto error;
3715 compute_elimination_index(bmap1, elim);
3716 for (i = 0; i < bmap2->n_eq; ++i) {
3717 int reduced;
3718 reduced = reduced_using_equalities(v->block.data, bmap2->eq[i],
3719 bmap1, elim);
3720 if (reduced && !isl_int_is_zero(v->block.data[0])(isl_sioimath_sgn(*(v->block.data[0])) == 0) &&
3721 isl_seq_first_non_zero(v->block.data + 1, total) == -1)
3722 goto disjoint;
3723 }
3724 for (i = 0; i < bmap2->n_ineq; ++i) {
3725 int reduced;
3726 reduced = reduced_using_equalities(v->block.data,
3727 bmap2->ineq[i], bmap1, elim);
3728 if (reduced && isl_int_is_neg(v->block.data[0])(isl_sioimath_sgn(*(v->block.data[0])) < 0) &&
3729 isl_seq_first_non_zero(v->block.data + 1, total) == -1)
3730 goto disjoint;
3731 }
3732 compute_elimination_index(bmap2, elim);
3733 for (i = 0; i < bmap1->n_ineq; ++i) {
3734 int reduced;
3735 reduced = reduced_using_equalities(v->block.data,
3736 bmap1->ineq[i], bmap2, elim);
3737 if (reduced && isl_int_is_neg(v->block.data[0])(isl_sioimath_sgn(*(v->block.data[0])) < 0) &&
3738 isl_seq_first_non_zero(v->block.data + 1, total) == -1)
3739 goto disjoint;
3740 }
3741 isl_vec_free(v);
3742 free(elim);
3743 return isl_bool_false;
3744disjoint:
3745 isl_vec_free(v);
3746 free(elim);
3747 return isl_bool_true;
3748error:
3749 isl_vec_free(v);
3750 free(elim);
3751 return isl_bool_error;
3752}
3753
3754int isl_basic_set_plain_is_disjoint(__isl_keep isl_basic_setisl_basic_map *bset1,
3755 __isl_keep isl_basic_setisl_basic_map *bset2)
3756{
3757 return isl_basic_map_plain_is_disjoint(bset_to_bmap(bset1),
3758 bset_to_bmap(bset2));
3759}
3760
3761/* Does "test" hold for all pairs of basic maps in "map1" and "map2"?
3762 */
3763static isl_bool all_pairs(__isl_keep isl_map *map1, __isl_keep isl_map *map2,
3764 isl_bool (*test)(__isl_keep isl_basic_map *bmap1,
3765 __isl_keep isl_basic_map *bmap2))
3766{
3767 int i, j;
3768
3769 if (!map1 || !map2)
3770 return isl_bool_error;
3771
3772 for (i = 0; i < map1->n; ++i) {
3773 for (j = 0; j < map2->n; ++j) {
3774 isl_bool d = test(map1->p[i], map2->p[j]);
3775 if (d != isl_bool_true)
3776 return d;
3777 }
3778 }
3779
3780 return isl_bool_true;
3781}
3782
3783/* Are "map1" and "map2" obviously disjoint, based on information
3784 * that can be derived without looking at the individual basic maps?
3785 *
3786 * In particular, if one of them is empty or if they live in different spaces
3787 * (ignoring parameters), then they are clearly disjoint.
3788 */
3789static isl_bool isl_map_plain_is_disjoint_global(__isl_keep isl_map *map1,
3790 __isl_keep isl_map *map2)
3791{
3792 isl_bool disjoint;
3793 isl_bool match;
3794
3795 if (!map1 || !map2)
3796 return isl_bool_error;
3797
3798 disjoint = isl_map_plain_is_empty(map1);
3799 if (disjoint < 0 || disjoint)
3800 return disjoint;
3801
3802 disjoint = isl_map_plain_is_empty(map2);
3803 if (disjoint < 0 || disjoint)
3804 return disjoint;
3805
3806 match = isl_space_tuple_is_equal(map1->dim, isl_dim_in,
3807 map2->dim, isl_dim_in);
3808 if (match < 0 || !match)
3809 return match < 0 ? isl_bool_error : isl_bool_true;
3810
3811 match = isl_space_tuple_is_equal(map1->dim, isl_dim_out,
3812 map2->dim, isl_dim_out);
3813 if (match < 0 || !match)
3814 return match < 0 ? isl_bool_error : isl_bool_true;
3815
3816 return isl_bool_false;
3817}
3818
3819/* Are "map1" and "map2" obviously disjoint?
3820 *
3821 * If one of them is empty or if they live in different spaces (ignoring
3822 * parameters), then they are clearly disjoint.
3823 * This is checked by isl_map_plain_is_disjoint_global.
3824 *
3825 * If they have different parameters, then we skip any further tests.
3826 *
3827 * If they are obviously equal, but not obviously empty, then we will
3828 * not be able to detect if they are disjoint.
3829 *
3830 * Otherwise we check if each basic map in "map1" is obviously disjoint
3831 * from each basic map in "map2".
3832 */
3833isl_bool isl_map_plain_is_disjoint(__isl_keep isl_map *map1,
3834 __isl_keep isl_map *map2)
3835{
3836 isl_bool disjoint;
3837 isl_bool intersect;
3838 isl_bool match;
3839
3840 disjoint = isl_map_plain_is_disjoint_global(map1, map2);
3841 if (disjoint < 0 || disjoint)
3842 return disjoint;
3843
3844 match = isl_map_has_equal_params(map1, map2);
3845 if (match < 0 || !match)
3846 return match < 0 ? isl_bool_error : isl_bool_false;
3847
3848 intersect = isl_map_plain_is_equal(map1, map2);
3849 if (intersect < 0 || intersect)
3850 return intersect < 0 ? isl_bool_error : isl_bool_false;
3851
3852 return all_pairs(map1, map2, &isl_basic_map_plain_is_disjoint);
3853}
3854
3855/* Are "map1" and "map2" disjoint?
3856 * The parameters are assumed to have been aligned.
3857 *
3858 * In particular, check whether all pairs of basic maps are disjoint.
3859 */
3860static isl_bool isl_map_is_disjoint_aligned(__isl_keep isl_map *map1,
3861 __isl_keep isl_map *map2)
3862{
3863 return all_pairs(map1, map2, &isl_basic_map_is_disjoint);
3864}
3865
3866/* Are "map1" and "map2" disjoint?
3867 *
3868 * They are disjoint if they are "obviously disjoint" or if one of them
3869 * is empty. Otherwise, they are not disjoint if one of them is universal.
3870 * If the two inputs are (obviously) equal and not empty, then they are
3871 * not disjoint.
3872 * If none of these cases apply, then check if all pairs of basic maps
3873 * are disjoint after aligning the parameters.
3874 */
3875isl_bool isl_map_is_disjoint(__isl_keep isl_map *map1, __isl_keep isl_map *map2)
3876{
3877 isl_bool disjoint;
3878 isl_bool intersect;
3879
3880 disjoint = isl_map_plain_is_disjoint_global(map1, map2);
3881 if (disjoint < 0 || disjoint)
3882 return disjoint;
3883
3884 disjoint = isl_map_is_empty(map1);
3885 if (disjoint < 0 || disjoint)
3886 return disjoint;
3887
3888 disjoint = isl_map_is_empty(map2);
3889 if (disjoint < 0 || disjoint)
3890 return disjoint;
3891
3892 intersect = isl_map_plain_is_universe(map1);
3893 if (intersect < 0 || intersect)
3894 return isl_bool_not(intersect);
3895
3896 intersect = isl_map_plain_is_universe(map2);
3897 if (intersect < 0 || intersect)
3898 return isl_bool_not(intersect);
3899
3900 intersect = isl_map_plain_is_equal(map1, map2);
3901 if (intersect < 0 || intersect)
3902 return isl_bool_not(intersect);
3903
3904 return isl_map_align_params_map_map_and_test(map1, map2,
3905 &isl_map_is_disjoint_aligned);
3906}
3907
3908/* Are "bmap1" and "bmap2" disjoint?
3909 *
3910 * They are disjoint if they are "obviously disjoint" or if one of them
3911 * is empty. Otherwise, they are not disjoint if one of them is universal.
3912 * If none of these cases apply, we compute the intersection and see if
3913 * the result is empty.
3914 */
3915isl_bool isl_basic_map_is_disjoint(__isl_keep isl_basic_map *bmap1,
3916 __isl_keep isl_basic_map *bmap2)
3917{
3918 isl_bool disjoint;
3919 isl_bool intersect;
3920 isl_basic_map *test;
3921
3922 disjoint = isl_basic_map_plain_is_disjoint(bmap1, bmap2);
3923 if (disjoint < 0 || disjoint)
3924 return disjoint;
3925
3926 disjoint = isl_basic_map_is_empty(bmap1);
3927 if (disjoint < 0 || disjoint)
3928 return disjoint;
3929
3930 disjoint = isl_basic_map_is_empty(bmap2);
3931 if (disjoint < 0 || disjoint)
3932 return disjoint;
3933
3934 intersect = isl_basic_map_plain_is_universe(bmap1);
3935 if (intersect < 0 || intersect)
3936 return isl_bool_not(intersect);
3937
3938 intersect = isl_basic_map_plain_is_universe(bmap2);
3939 if (intersect < 0 || intersect)
3940 return isl_bool_not(intersect);
3941
3942 test = isl_basic_map_intersect(isl_basic_map_copy(bmap1),
3943 isl_basic_map_copy(bmap2));
3944 disjoint = isl_basic_map_is_empty(test);
3945 isl_basic_map_free(test);
3946
3947 return disjoint;
3948}
3949
3950/* Are "bset1" and "bset2" disjoint?
3951 */
3952isl_bool isl_basic_set_is_disjoint(__isl_keep isl_basic_setisl_basic_map *bset1,
3953 __isl_keep isl_basic_setisl_basic_map *bset2)
3954{
3955 return isl_basic_map_is_disjoint(bset1, bset2);
3956}
3957
3958isl_bool isl_set_plain_is_disjoint(__isl_keep isl_setisl_map *set1,
3959 __isl_keep isl_setisl_map *set2)
3960{
3961 return isl_map_plain_is_disjoint(set_to_map(set1), set_to_map(set2));
3962}
3963
3964/* Are "set1" and "set2" disjoint?
3965 */
3966isl_bool isl_set_is_disjoint(__isl_keep isl_setisl_map *set1, __isl_keep isl_setisl_map *set2)
3967{
3968 return isl_map_is_disjoint(set1, set2);
3969}
3970
3971/* Is "v" equal to 0, 1 or -1?
3972 */
3973static int is_zero_or_one(isl_int v)
3974{
3975 return isl_int_is_zero(v)(isl_sioimath_sgn(*(v)) == 0) || isl_int_is_one(v)(isl_sioimath_cmp_si(*(v), 1) == 0) || isl_int_is_negone(v)(isl_sioimath_cmp_si(*(v), -1) == 0);
3976}
3977
3978/* Are the "n" coefficients starting at "first" of inequality constraints
3979 * "i" and "j" of "bmap" opposite to each other?
3980 */
3981static int is_opposite_part(__isl_keep isl_basic_map *bmap, int i, int j,
3982 int first, int n)
3983{
3984 return isl_seq_is_neg(bmap->ineq[i] + first, bmap->ineq[j] + first, n);
3985}
3986
3987/* Are inequality constraints "i" and "j" of "bmap" opposite to each other,
3988 * apart from the constant term?
3989 */
3990static isl_bool is_opposite(__isl_keep isl_basic_map *bmap, int i, int j)
3991{
3992 isl_size total;
3993
3994 total = isl_basic_map_dim(bmap, isl_dim_all);
3995 if (total < 0)
3996 return isl_bool_error;
3997 return is_opposite_part(bmap, i, j, 1, total);
3998}
3999
4000/* Check if we can combine a given div with lower bound l and upper
4001 * bound u with some other div and if so return that other div.
4002 * Otherwise, return a position beyond the integer divisions.
4003 * Return -1 on error.
4004 *
4005 * We first check that
4006 * - the bounds are opposites of each other (except for the constant
4007 * term)
4008 * - the bounds do not reference any other div
4009 * - no div is defined in terms of this div
4010 *
4011 * Let m be the size of the range allowed on the div by the bounds.
4012 * That is, the bounds are of the form
4013 *
4014 * e <= a <= e + m - 1
4015 *
4016 * with e some expression in the other variables.
4017 * We look for another div b such that no third div is defined in terms
4018 * of this second div b and such that in any constraint that contains
4019 * a (except for the given lower and upper bound), also contains b
4020 * with a coefficient that is m times that of b.
4021 * That is, all constraints (except for the lower and upper bound)
4022 * are of the form
4023 *
4024 * e + f (a + m b) >= 0
4025 *
4026 * Furthermore, in the constraints that only contain b, the coefficient
4027 * of b should be equal to 1 or -1.
4028 * If so, we return b so that "a + m b" can be replaced by
4029 * a single div "c = a + m b".
4030 */
4031static int div_find_coalesce(__isl_keep isl_basic_map *bmap, int *pairs,
4032 unsigned div, unsigned l, unsigned u)
4033{
4034 int i, j;
4035 unsigned n_div;
4036 isl_size v_div;
4037 int coalesce;
4038 isl_bool opp;
4039
4040 n_div = isl_basic_map_dim(bmap, isl_dim_div);
4041 if (n_div <= 1)
4042 return n_div;
4043 v_div = isl_basic_map_var_offset(bmap, isl_dim_div);
4044 if (v_div < 0)
4045 return -1;
4046 if (isl_seq_first_non_zero(bmap->ineq[l] + 1 + v_div, div) != -1)
4047 return n_div;
4048 if (isl_seq_first_non_zero(bmap->ineq[l] + 1 + v_div + div + 1,
4049 n_div - div - 1) != -1)
4050 return n_div;
4051 opp = is_opposite(bmap, l, u);
4052 if (opp < 0 || !opp)
4053 return opp < 0 ? -1 : n_div;
4054
4055 for (i = 0; i < n_div; ++i) {
4056 if (isl_int_is_zero(bmap->div[i][0])(isl_sioimath_sgn(*(bmap->div[i][0])) == 0))
4057 continue;
4058 if (!isl_int_is_zero(bmap->div[i][1 + 1 + v_div + div])(isl_sioimath_sgn(*(bmap->div[i][1 + 1 + v_div + div])) ==
0)
)
4059 return n_div;
4060 }
4061
4062 isl_int_add(bmap->ineq[l][0], bmap->ineq[l][0], bmap->ineq[u][0])isl_sioimath_add((bmap->ineq[l][0]), *(bmap->ineq[l][0]
), *(bmap->ineq[u][0]))
;
4063 if (isl_int_is_neg(bmap->ineq[l][0])(isl_sioimath_sgn(*(bmap->ineq[l][0])) < 0)) {
4064 isl_int_sub(bmap->ineq[l][0],isl_sioimath_sub((bmap->ineq[l][0]), *(bmap->ineq[l][0]
), *(bmap->ineq[u][0]))
4065 bmap->ineq[l][0], bmap->ineq[u][0])isl_sioimath_sub((bmap->ineq[l][0]), *(bmap->ineq[l][0]
), *(bmap->ineq[u][0]))
;
4066 bmap = isl_basic_map_copy(bmap);
4067 bmap = isl_basic_map_set_to_empty(bmap);
4068 isl_basic_map_free(bmap);
4069 return n_div;
4070 }
4071 isl_int_add_ui(bmap->ineq[l][0], bmap->ineq[l][0], 1)isl_sioimath_add_ui((bmap->ineq[l][0]), *(bmap->ineq[l]
[0]), 1)
;
4072 coalesce = n_div;
4073 for (i = 0; i < n_div; ++i) {
4074 if (i == div)
4075 continue;
4076 if (!pairs[i])
4077 continue;
4078 for (j = 0; j < n_div; ++j) {
4079 if (isl_int_is_zero(bmap->div[j][0])(isl_sioimath_sgn(*(bmap->div[j][0])) == 0))
4080 continue;
4081 if (!isl_int_is_zero(bmap->div[j][1 + 1 + v_div + i])(isl_sioimath_sgn(*(bmap->div[j][1 + 1 + v_div + i])) == 0
)
)
4082 break;
4083 }
4084 if (j < n_div)
4085 continue;
4086 for (j = 0; j < bmap->n_ineq; ++j) {
4087 int valid;
4088 if (j == l || j == u)
4089 continue;
4090 if (isl_int_is_zero(bmap->ineq[j][1 + v_div + div])(isl_sioimath_sgn(*(bmap->ineq[j][1 + v_div + div])) == 0)) {
4091 if (is_zero_or_one(bmap->ineq[j][1 + v_div + i]))
4092 continue;
4093 break;
4094 }
4095 if (isl_int_is_zero(bmap->ineq[j][1 + v_div + i])(isl_sioimath_sgn(*(bmap->ineq[j][1 + v_div + i])) == 0))
4096 break;
4097 isl_int_mul(bmap->ineq[j][1 + v_div + div],isl_sioimath_mul((bmap->ineq[j][1 + v_div + div]), *(bmap->
ineq[j][1 + v_div + div]), *(bmap->ineq[l][0]))
4098 bmap->ineq[j][1 + v_div + div],isl_sioimath_mul((bmap->ineq[j][1 + v_div + div]), *(bmap->
ineq[j][1 + v_div + div]), *(bmap->ineq[l][0]))
4099 bmap->ineq[l][0])isl_sioimath_mul((bmap->ineq[j][1 + v_div + div]), *(bmap->
ineq[j][1 + v_div + div]), *(bmap->ineq[l][0]))
;
4100 valid = isl_int_eq(bmap->ineq[j][1 + v_div + div],(isl_sioimath_cmp(*(bmap->ineq[j][1 + v_div + div]), *(bmap
->ineq[j][1 + v_div + i])) == 0)
4101 bmap->ineq[j][1 + v_div + i])(isl_sioimath_cmp(*(bmap->ineq[j][1 + v_div + div]), *(bmap
->ineq[j][1 + v_div + i])) == 0)
;
4102 isl_int_divexact(bmap->ineq[j][1 + v_div + div],isl_sioimath_tdiv_q((bmap->ineq[j][1 + v_div + div]), *(bmap
->ineq[j][1 + v_div + div]), *(bmap->ineq[l][0]))
4103 bmap->ineq[j][1 + v_div + div],isl_sioimath_tdiv_q((bmap->ineq[j][1 + v_div + div]), *(bmap
->ineq[j][1 + v_div + div]), *(bmap->ineq[l][0]))
4104 bmap->ineq[l][0])isl_sioimath_tdiv_q((bmap->ineq[j][1 + v_div + div]), *(bmap
->ineq[j][1 + v_div + div]), *(bmap->ineq[l][0]))
;
4105 if (!valid)
4106 break;
4107 }
4108 if (j < bmap->n_ineq)
4109 continue;
4110 coalesce = i;
4111 break;
4112 }
4113 isl_int_sub_ui(bmap->ineq[l][0], bmap->ineq[l][0], 1)isl_sioimath_sub_ui((bmap->ineq[l][0]), *(bmap->ineq[l]
[0]), 1)
;
4114 isl_int_sub(bmap->ineq[l][0], bmap->ineq[l][0], bmap->ineq[u][0])isl_sioimath_sub((bmap->ineq[l][0]), *(bmap->ineq[l][0]
), *(bmap->ineq[u][0]))
;
4115 return coalesce;
4116}
4117
4118/* Internal data structure used during the construction and/or evaluation of
4119 * an inequality that ensures that a pair of bounds always allows
4120 * for an integer value.
4121 *
4122 * "tab" is the tableau in which the inequality is evaluated. It may
4123 * be NULL until it is actually needed.
4124 * "v" contains the inequality coefficients.
4125 * "g", "fl" and "fu" are temporary scalars used during the construction and
4126 * evaluation.
4127 */
4128struct test_ineq_data {
4129 struct isl_tab *tab;
4130 isl_vec *v;
4131 isl_int g;
4132 isl_int fl;
4133 isl_int fu;
4134};
4135
4136/* Free all the memory allocated by the fields of "data".
4137 */
4138static void test_ineq_data_clear(struct test_ineq_data *data)
4139{
4140 isl_tab_free(data->tab);
4141 isl_vec_free(data->v);
4142 isl_int_clear(data->g)isl_sioimath_clear((data->g));
4143 isl_int_clear(data->fl)isl_sioimath_clear((data->fl));
4144 isl_int_clear(data->fu)isl_sioimath_clear((data->fu));
4145}
4146
4147/* Is the inequality stored in data->v satisfied by "bmap"?
4148 * That is, does it only attain non-negative values?
4149 * data->tab is a tableau corresponding to "bmap".
4150 */
4151static isl_bool test_ineq_is_satisfied(__isl_keep isl_basic_map *bmap,
4152 struct test_ineq_data *data)
4153{
4154 isl_ctx *ctx;
4155 enum isl_lp_result res;
4156
4157 ctx = isl_basic_map_get_ctx(bmap);
4158 if (!data->tab)
4159 data->tab = isl_tab_from_basic_map(bmap, 0);
4160 res = isl_tab_min(data->tab, data->v->el, ctx->one, &data->g, NULL((void*)0), 0);
4161 if (res == isl_lp_error)
4162 return isl_bool_error;
4163 return res == isl_lp_ok && isl_int_is_nonneg(data->g)(isl_sioimath_sgn(*(data->g)) >= 0);
4164}
4165
4166/* Given a lower and an upper bound on div i, do they always allow
4167 * for an integer value of the given div?
4168 * Determine this property by constructing an inequality
4169 * such that the property is guaranteed when the inequality is nonnegative.
4170 * The lower bound is inequality l, while the upper bound is inequality u.
4171 * The constructed inequality is stored in data->v.
4172 *
4173 * Let the upper bound be
4174 *
4175 * -n_u a + e_u >= 0
4176 *
4177 * and the lower bound
4178 *
4179 * n_l a + e_l >= 0
4180 *
4181 * Let n_u = f_u g and n_l = f_l g, with g = gcd(n_u, n_l).
4182 * We have
4183 *
4184 * - f_u e_l <= f_u f_l g a <= f_l e_u
4185 *
4186 * Since all variables are integer valued, this is equivalent to
4187 *
4188 * - f_u e_l - (f_u - 1) <= f_u f_l g a <= f_l e_u + (f_l - 1)
4189 *
4190 * If this interval is at least f_u f_l g, then it contains at least
4191 * one integer value for a.
4192 * That is, the test constraint is
4193 *
4194 * f_l e_u + f_u e_l + f_l - 1 + f_u - 1 + 1 >= f_u f_l g
4195 *
4196 * or
4197 *
4198 * f_l e_u + f_u e_l + f_l - 1 + f_u - 1 + 1 - f_u f_l g >= 0
4199 *
4200 * If the coefficients of f_l e_u + f_u e_l have a common divisor g',
4201 * then the constraint can be scaled down by a factor g',
4202 * with the constant term replaced by
4203 * floor((f_l e_{u,0} + f_u e_{l,0} + f_l - 1 + f_u - 1 + 1 - f_u f_l g)/g').
4204 * Note that the result of applying Fourier-Motzkin to this pair
4205 * of constraints is
4206 *
4207 * f_l e_u + f_u e_l >= 0
4208 *
4209 * If the constant term of the scaled down version of this constraint,
4210 * i.e., floor((f_l e_{u,0} + f_u e_{l,0})/g') is equal to the constant
4211 * term of the scaled down test constraint, then the test constraint
4212 * is known to hold and no explicit evaluation is required.
4213 * This is essentially the Omega test.
4214 *
4215 * If the test constraint consists of only a constant term, then
4216 * it is sufficient to look at the sign of this constant term.
4217 */
4218static isl_bool int_between_bounds(__isl_keep isl_basic_map *bmap, int i,
4219 int l, int u, struct test_ineq_data *data)
4220{
4221 unsigned offset;
4222 isl_size n_div;
4223
4224 offset = isl_basic_map_offset(bmap, isl_dim_div);
4225 n_div = isl_basic_map_dim(bmap, isl_dim_div);
4226 if (n_div < 0)
4227 return isl_bool_error;
4228
4229 isl_int_gcd(data->g,isl_sioimath_gcd((data->g), *(bmap->ineq[l][offset + i]
), *(bmap->ineq[u][offset + i]))
4230 bmap->ineq[l][offset + i], bmap->ineq[u][offset + i])isl_sioimath_gcd((data->g), *(bmap->ineq[l][offset + i]
), *(bmap->ineq[u][offset + i]))
;
4231 isl_int_divexact(data->fl, bmap->ineq[l][offset + i], data->g)isl_sioimath_tdiv_q((data->fl), *(bmap->ineq[l][offset +
i]), *(data->g))
;
4232 isl_int_divexact(data->fu, bmap->ineq[u][offset + i], data->g)isl_sioimath_tdiv_q((data->fu), *(bmap->ineq[u][offset +
i]), *(data->g))
;
4233 isl_int_neg(data->fu, data->fu)isl_sioimath_neg((data->fu), *(data->fu));
4234 isl_seq_combine(data->v->el, data->fl, bmap->ineq[u],
4235 data->fu, bmap->ineq[l], offset + n_div);
4236 isl_int_mul(data->g, data->g, data->fl)isl_sioimath_mul((data->g), *(data->g), *(data->fl));
4237 isl_int_mul(data->g, data->g, data->fu)isl_sioimath_mul((data->g), *(data->g), *(data->fu));
4238 isl_int_sub(data->g, data->g, data->fl)isl_sioimath_sub((data->g), *(data->g), *(data->fl));
4239 isl_int_sub(data->g, data->g, data->fu)isl_sioimath_sub((data->g), *(data->g), *(data->fu));
4240 isl_int_add_ui(data->g, data->g, 1)isl_sioimath_add_ui((data->g), *(data->g), 1);
4241 isl_int_sub(data->fl, data->v->el[0], data->g)isl_sioimath_sub((data->fl), *(data->v->el[0]), *(data
->g))
;
4242
4243 isl_seq_gcd(data->v->el + 1, offset - 1 + n_div, &data->g);
4244 if (isl_int_is_zero(data->g)(isl_sioimath_sgn(*(data->g)) == 0))
4245 return isl_int_is_nonneg(data->fl)(isl_sioimath_sgn(*(data->fl)) >= 0);
4246 if (isl_int_is_one(data->g)(isl_sioimath_cmp_si(*(data->g), 1) == 0)) {
4247 isl_int_set(data->v->el[0], data->fl)isl_sioimath_set((data->v->el[0]), *(data->fl));
4248 return test_ineq_is_satisfied(bmap, data);
4249 }
4250 isl_int_fdiv_q(data->fl, data->fl, data->g)isl_sioimath_fdiv_q((data->fl), *(data->fl), *(data->
g))
;
4251 isl_int_fdiv_q(data->v->el[0], data->v->el[0], data->g)isl_sioimath_fdiv_q((data->v->el[0]), *(data->v->
el[0]), *(data->g))
;
4252 if (isl_int_eq(data->fl, data->v->el[0])(isl_sioimath_cmp(*(data->fl), *(data->v->el[0])) ==
0)
)
4253 return isl_bool_true;
4254 isl_int_set(data->v->el[0], data->fl)isl_sioimath_set((data->v->el[0]), *(data->fl));
4255 isl_seq_scale_down(data->v->el + 1, data->v->el + 1, data->g,
4256 offset - 1 + n_div);
4257
4258 return test_ineq_is_satisfied(bmap, data);
4259}
4260
4261/* Remove more kinds of divs that are not strictly needed.
4262 * In particular, if all pairs of lower and upper bounds on a div
4263 * are such that they allow at least one integer value of the div,
4264 * then we can eliminate the div using Fourier-Motzkin without
4265 * introducing any spurious solutions.
4266 *
4267 * If at least one of the two constraints has a unit coefficient for the div,
4268 * then the presence of such a value is guaranteed so there is no need to check.
4269 * In particular, the value attained by the bound with unit coefficient
4270 * can serve as this intermediate value.
4271 */
4272static __isl_give isl_basic_map *drop_more_redundant_divs(
4273 __isl_take isl_basic_map *bmap, __isl_take int *pairs, int n)
4274{
4275 isl_ctx *ctx;
4276 struct test_ineq_data data = { NULL((void*)0), NULL((void*)0) };
4277 unsigned off;
4278 isl_size n_div;
4279 int remove = -1;
4280
4281 isl_int_init(data.g)isl_sioimath_init((data.g));
4282 isl_int_init(data.fl)isl_sioimath_init((data.fl));
4283 isl_int_init(data.fu)isl_sioimath_init((data.fu));
4284
4285 n_div = isl_basic_map_dim(bmap, isl_dim_div);
4286 if (n_div < 0)
4287 goto error;
4288
4289 ctx = isl_basic_map_get_ctx(bmap);
4290 off = isl_basic_map_offset(bmap, isl_dim_div);
4291 data.v = isl_vec_alloc(ctx, off + n_div);
4292 if (!data.v)
4293 goto error;
4294
4295 while (n > 0) {
4296 int i, l, u;
4297 int best = -1;
4298 isl_bool has_int;
4299
4300 for (i = 0; i < n_div; ++i) {
4301 if (!pairs[i])
4302 continue;
4303 if (best >= 0 && pairs[best] <= pairs[i])
4304 continue;
4305 best = i;
4306 }
4307
4308 i = best;
4309 for (l = 0; l < bmap->n_ineq; ++l) {
4310 if (!isl_int_is_pos(bmap->ineq[l][off + i])(isl_sioimath_sgn(*(bmap->ineq[l][off + i])) > 0))
4311 continue;
4312 if (isl_int_is_one(bmap->ineq[l][off + i])(isl_sioimath_cmp_si(*(bmap->ineq[l][off + i]), 1) == 0))
4313 continue;
4314 for (u = 0; u < bmap->n_ineq; ++u) {
4315 if (!isl_int_is_neg(bmap->ineq[u][off + i])(isl_sioimath_sgn(*(bmap->ineq[u][off + i])) < 0))
4316 continue;
4317 if (isl_int_is_negone(bmap->ineq[u][off + i])(isl_sioimath_cmp_si(*(bmap->ineq[u][off + i]), -1) == 0))
4318 continue;
4319 has_int = int_between_bounds(bmap, i, l, u,
4320 &data);
4321 if (has_int < 0)
4322 goto error;
4323 if (data.tab && data.tab->empty)
4324 break;
4325 if (!has_int)
4326 break;
4327 }
4328 if (u < bmap->n_ineq)
4329 break;
4330 }
4331 if (data.tab && data.tab->empty) {
4332 bmap = isl_basic_map_set_to_empty(bmap);
4333 break;
4334 }
4335 if (l == bmap->n_ineq) {
4336 remove = i;
4337 break;
4338 }
4339 pairs[i] = 0;
4340 --n;
4341 }
4342
4343 test_ineq_data_clear(&data);
4344
4345 free(pairs);
4346
4347 if (remove < 0)
4348 return bmap;
4349
4350 bmap = isl_basic_map_remove_dims(bmap, isl_dim_div, remove, 1);
4351 return isl_basic_map_drop_redundant_divs(bmap);
4352error:
4353 free(pairs);
4354 isl_basic_map_free(bmap);
4355 test_ineq_data_clear(&data);
4356 return NULL((void*)0);
4357}
4358
4359/* Given a pair of divs div1 and div2 such that, except for the lower bound l
4360 * and the upper bound u, div1 always occurs together with div2 in the form
4361 * (div1 + m div2), where m is the constant range on the variable div1
4362 * allowed by l and u, replace the pair div1 and div2 by a single
4363 * div that is equal to div1 + m div2.
4364 *
4365 * The new div will appear in the location that contains div2.
4366 * We need to modify all constraints that contain
4367 * div2 = (div - div1) / m
4368 * The coefficient of div2 is known to be equal to 1 or -1.
4369 * (If a constraint does not contain div2, it will also not contain div1.)
4370 * If the constraint also contains div1, then we know they appear
4371 * as f (div1 + m div2) and we can simply replace (div1 + m div2) by div,
4372 * i.e., the coefficient of div is f.
4373 *
4374 * Otherwise, we first need to introduce div1 into the constraint.
4375 * Let l be
4376 *
4377 * div1 + f >=0
4378 *
4379 * and u
4380 *
4381 * -div1 + f' >= 0
4382 *
4383 * A lower bound on div2
4384 *
4385 * div2 + t >= 0
4386 *
4387 * can be replaced by
4388 *
4389 * m div2 + div1 + m t + f >= 0
4390 *
4391 * An upper bound
4392 *
4393 * -div2 + t >= 0
4394 *
4395 * can be replaced by
4396 *
4397 * -(m div2 + div1) + m t + f' >= 0
4398 *
4399 * These constraint are those that we would obtain from eliminating
4400 * div1 using Fourier-Motzkin.
4401 *
4402 * After all constraints have been modified, we drop the lower and upper
4403 * bound and then drop div1.
4404 * Since the new div is only placed in the same location that used
4405 * to store div2, but otherwise has a different meaning, any possible
4406 * explicit representation of the original div2 is removed.
4407 */
4408static __isl_give isl_basic_map *coalesce_divs(__isl_take isl_basic_map *bmap,
4409 unsigned div1, unsigned div2, unsigned l, unsigned u)
4410{
4411 isl_ctx *ctx;
4412 isl_int m;
4413 isl_size v_div;
4414 unsigned total;
4415 int i;
4416
4417 ctx = isl_basic_map_get_ctx(bmap);
4418
4419 v_div = isl_basic_map_var_offset(bmap, isl_dim_div);
4420 if (v_div < 0)
4421 return isl_basic_map_free(bmap);
4422 total = 1 + v_div + bmap->n_div;
4423
4424 isl_int_init(m)isl_sioimath_init((m));
4425 isl_int_add(m, bmap->ineq[l][0], bmap->ineq[u][0])isl_sioimath_add((m), *(bmap->ineq[l][0]), *(bmap->ineq
[u][0]))
;
4426 isl_int_add_ui(m, m, 1)isl_sioimath_add_ui((m), *(m), 1);
4427
4428 for (i = 0; i < bmap->n_ineq; ++i) {
4429 if (i == l || i == u)
4430 continue;
4431 if (isl_int_is_zero(bmap->ineq[i][1 + v_div + div2])(isl_sioimath_sgn(*(bmap->ineq[i][1 + v_div + div2])) == 0
)
)
4432 continue;
4433 if (isl_int_is_zero(bmap->ineq[i][1 + v_div + div1])(isl_sioimath_sgn(*(bmap->ineq[i][1 + v_div + div1])) == 0
)
) {
4434 if (isl_int_is_pos(bmap->ineq[i][1 + v_div + div2])(isl_sioimath_sgn(*(bmap->ineq[i][1 + v_div + div2])) >
0)
)
4435 isl_seq_combine(bmap->ineq[i], m, bmap->ineq[i],
4436 ctx->one, bmap->ineq[l], total);
4437 else
4438 isl_seq_combine(bmap->ineq[i], m, bmap->ineq[i],
4439 ctx->one, bmap->ineq[u], total);
4440 }
4441 isl_int_set(bmap->ineq[i][1 + v_div + div2],isl_sioimath_set((bmap->ineq[i][1 + v_div + div2]), *(bmap
->ineq[i][1 + v_div + div1]))
4442 bmap->ineq[i][1 + v_div + div1])isl_sioimath_set((bmap->ineq[i][1 + v_div + div2]), *(bmap
->ineq[i][1 + v_div + div1]))
;
4443 isl_int_set_si(bmap->ineq[i][1 + v_div + div1], 0)isl_sioimath_set_si((bmap->ineq[i][1 + v_div + div1]), 0);
4444 }
4445
4446 isl_int_clear(m)isl_sioimath_clear((m));
4447 if (l > u) {
4448 isl_basic_map_drop_inequality(bmap, l);
4449 isl_basic_map_drop_inequality(bmap, u);
4450 } else {
4451 isl_basic_map_drop_inequality(bmap, u);
4452 isl_basic_map_drop_inequality(bmap, l);
4453 }
4454 bmap = isl_basic_map_mark_div_unknown(bmap, div2);
4455 bmap = isl_basic_map_drop_div(bmap, div1);
4456 return bmap;
4457}
4458
4459/* First check if we can coalesce any pair of divs and
4460 * then continue with dropping more redundant divs.
4461 *
4462 * We loop over all pairs of lower and upper bounds on a div
4463 * with coefficient 1 and -1, respectively, check if there
4464 * is any other div "c" with which we can coalesce the div
4465 * and if so, perform the coalescing.
4466 */
4467static __isl_give isl_basic_map *coalesce_or_drop_more_redundant_divs(
4468 __isl_take isl_basic_map *bmap, int *pairs, int n)
4469{
4470 int i, l, u;
4471 isl_size v_div;
4472 isl_size n_div;
4473
4474 v_div = isl_basic_map_var_offset(bmap, isl_dim_div);
4475 n_div = isl_basic_map_dim(bmap, isl_dim_div);
4476 if (v_div < 0 || n_div < 0)
4477 return isl_basic_map_free(bmap);
4478
4479 for (i = 0; i < n_div; ++i) {
4480 if (!pairs[i])
4481 continue;
4482 for (l = 0; l < bmap->n_ineq; ++l) {
4483 if (!isl_int_is_one(bmap->ineq[l][1 + v_div + i])(isl_sioimath_cmp_si(*(bmap->ineq[l][1 + v_div + i]), 1) ==
0)
)
4484 continue;
4485 for (u = 0; u < bmap->n_ineq; ++u) {
4486 int c;
4487
4488 if (!isl_int_is_negone(bmap->ineq[u][1+v_div+i])(isl_sioimath_cmp_si(*(bmap->ineq[u][1+v_div+i]), -1) == 0
)
)
4489 continue;
4490 c = div_find_coalesce(bmap, pairs, i, l, u);
4491 if (c < 0)
4492 goto error;
4493 if (c >= n_div)
4494 continue;
4495 free(pairs);
4496 bmap = coalesce_divs(bmap, i, c, l, u);
4497 return isl_basic_map_drop_redundant_divs(bmap);
4498 }
4499 }
4500 }
4501
4502 if (ISL_F_ISSET(bmap, ISL_BASIC_MAP_EMPTY)(!!(((bmap)->flags) & ((1 << 1))))) {
4503 free(pairs);
4504 return bmap;
4505 }
4506
4507 return drop_more_redundant_divs(bmap, pairs, n);
4508error:
4509 free(pairs);
4510 isl_basic_map_free(bmap);
4511 return NULL((void*)0);
4512}
4513
4514/* Are the "n" coefficients starting at "first" of inequality constraints
4515 * "i" and "j" of "bmap" equal to each other?
4516 */
4517static int is_parallel_part(__isl_keep isl_basic_map *bmap, int i, int j,
4518 int first, int n)
4519{
4520 return isl_seq_eq(bmap->ineq[i] + first, bmap->ineq[j] + first, n);
4521}
4522
4523/* Are inequality constraints "i" and "j" of "bmap" equal to each other,
4524 * apart from the constant term and the coefficient at position "pos"?
4525 */
4526static isl_bool is_parallel_except(__isl_keep isl_basic_map *bmap, int i, int j,
4527 int pos)
4528{
4529 isl_size total;
4530
4531 total = isl_basic_map_dim(bmap, isl_dim_all);
4532 if (total < 0)
4533 return isl_bool_error;
4534 return is_parallel_part(bmap, i, j, 1, pos - 1) &&
4535 is_parallel_part(bmap, i, j, pos + 1, total - pos);
4536}
4537
4538/* Are inequality constraints "i" and "j" of "bmap" opposite to each other,
4539 * apart from the constant term and the coefficient at position "pos"?
4540 */
4541static isl_bool is_opposite_except(__isl_keep isl_basic_map *bmap, int i, int j,
4542 int pos)
4543{
4544 isl_size total;
4545
4546 total = isl_basic_map_dim(bmap, isl_dim_all);
4547 if (total < 0)
4548 return isl_bool_error;
4549 return is_opposite_part(bmap, i, j, 1, pos - 1) &&
4550 is_opposite_part(bmap, i, j, pos + 1, total - pos);
4551}
4552
4553/* Restart isl_basic_map_drop_redundant_divs after "bmap" has
4554 * been modified, simplying it if "simplify" is set.
4555 * Free the temporary data structure "pairs" that was associated
4556 * to the old version of "bmap".
4557 */
4558static __isl_give isl_basic_map *drop_redundant_divs_again(
4559 __isl_take isl_basic_map *bmap, __isl_take int *pairs, int simplify)
4560{
4561 if (simplify)
4562 bmap = isl_basic_map_simplify(bmap);
4563 free(pairs);
4564 return isl_basic_map_drop_redundant_divs(bmap);
4565}
4566
4567/* Is "div" the single unknown existentially quantified variable
4568 * in inequality constraint "ineq" of "bmap"?
4569 * "div" is known to have a non-zero coefficient in "ineq".
4570 */
4571static isl_bool single_unknown(__isl_keep isl_basic_map *bmap, int ineq,
4572 int div)
4573{
4574 int i;
4575 isl_size n_div;
4576 unsigned o_div;
4577 isl_bool known;
4578
4579 known = isl_basic_map_div_is_known(bmap, div);
4580 if (known < 0 || known)
4581 return isl_bool_not(known);
4582 n_div = isl_basic_map_dim(bmap, isl_dim_div);
4583 if (n_div < 0)
4584 return isl_bool_error;
4585 if (n_div == 1)
4586 return isl_bool_true;
4587 o_div = isl_basic_map_offset(bmap, isl_dim_div);
4588 for (i = 0; i < n_div; ++i) {
4589 isl_bool known;
4590
4591 if (i == div)
4592 continue;
4593 if (isl_int_is_zero(bmap->ineq[ineq][o_div + i])(isl_sioimath_sgn(*(bmap->ineq[ineq][o_div + i])) == 0))
4594 continue;
4595 known = isl_basic_map_div_is_known(bmap, i);
4596 if (known < 0 || !known)
4597 return known;
4598 }
4599
4600 return isl_bool_true;
4601}
4602
4603/* Does integer division "div" have coefficient 1 in inequality constraint
4604 * "ineq" of "map"?
4605 */
4606static isl_bool has_coef_one(__isl_keep isl_basic_map *bmap, int div, int ineq)
4607{
4608 unsigned o_div;
4609
4610 o_div = isl_basic_map_offset(bmap, isl_dim_div);
4611 if (isl_int_is_one(bmap->ineq[ineq][o_div + div])(isl_sioimath_cmp_si(*(bmap->ineq[ineq][o_div + div]), 1) ==
0)
)
4612 return isl_bool_true;
4613
4614 return isl_bool_false;
4615}
4616
4617/* Turn inequality constraint "ineq" of "bmap" into an equality and
4618 * then try and drop redundant divs again,
4619 * freeing the temporary data structure "pairs" that was associated
4620 * to the old version of "bmap".
4621 */
4622static __isl_give isl_basic_map *set_eq_and_try_again(
4623 __isl_take isl_basic_map *bmap, int ineq, __isl_take int *pairs)
4624{
4625 bmap = isl_basic_map_cow(bmap);
4626 isl_basic_map_inequality_to_equality(bmap, ineq);
4627 return drop_redundant_divs_again(bmap, pairs, 1);
4628}
4629
4630/* Drop the integer division at position "div", along with the two
4631 * inequality constraints "ineq1" and "ineq2" in which it appears
4632 * from "bmap" and then try and drop redundant divs again,
4633 * freeing the temporary data structure "pairs" that was associated
4634 * to the old version of "bmap".
4635 */
4636static __isl_give isl_basic_map *drop_div_and_try_again(
4637 __isl_take isl_basic_map *bmap, int div, int ineq1, int ineq2,
4638 __isl_take int *pairs)
4639{
4640 if (ineq1 > ineq2) {
4641 isl_basic_map_drop_inequality(bmap, ineq1);
4642 isl_basic_map_drop_inequality(bmap, ineq2);
4643 } else {
4644 isl_basic_map_drop_inequality(bmap, ineq2);
4645 isl_basic_map_drop_inequality(bmap, ineq1);
4646 }
4647 bmap = isl_basic_map_drop_div(bmap, div);
4648 return drop_redundant_divs_again(bmap, pairs, 0);
4649}
4650
4651/* Given two inequality constraints
4652 *
4653 * f(x) + n d + c >= 0, (ineq)
4654 *
4655 * with d the variable at position "pos", and
4656 *
4657 * f(x) + c0 >= 0, (lower)
4658 *
4659 * compute the maximal value of the lower bound ceil((-f(x) - c)/n)
4660 * determined by the first constraint.
4661 * That is, store
4662 *
4663 * ceil((c0 - c)/n)
4664 *
4665 * in *l.
4666 */
4667static void lower_bound_from_parallel(__isl_keep isl_basic_map *bmap,
4668 int ineq, int lower, int pos, isl_int *l)
4669{
4670 isl_int_neg(*l, bmap->ineq[ineq][0])isl_sioimath_neg((*l), *(bmap->ineq[ineq][0]));
4671 isl_int_add(*l, *l, bmap->ineq[lower][0])isl_sioimath_add((*l), *(*l), *(bmap->ineq[lower][0]));
4672 isl_int_cdiv_q(*l, *l, bmap->ineq[ineq][pos])isl_sioimath_cdiv_q((*l), *(*l), *(bmap->ineq[ineq][pos]));
4673}
4674
4675/* Given two inequality constraints
4676 *
4677 * f(x) + n d + c >= 0, (ineq)
4678 *
4679 * with d the variable at position "pos", and
4680 *
4681 * -f(x) - c0 >= 0, (upper)
4682 *
4683 * compute the minimal value of the lower bound ceil((-f(x) - c)/n)
4684 * determined by the first constraint.
4685 * That is, store
4686 *
4687 * ceil((-c1 - c)/n)
4688 *
4689 * in *u.
4690 */
4691static void lower_bound_from_opposite(__isl_keep isl_basic_map *bmap,
4692 int ineq, int upper, int pos, isl_int *u)
4693{
4694 isl_int_neg(*u, bmap->ineq[ineq][0])isl_sioimath_neg((*u), *(bmap->ineq[ineq][0]));
4695 isl_int_sub(*u, *u, bmap->ineq[upper][0])isl_sioimath_sub((*u), *(*u), *(bmap->ineq[upper][0]));
4696 isl_int_cdiv_q(*u, *u, bmap->ineq[ineq][pos])isl_sioimath_cdiv_q((*u), *(*u), *(bmap->ineq[ineq][pos]));
4697}
4698
4699/* Given a lower bound constraint "ineq" on "div" in "bmap",
4700 * does the corresponding lower bound have a fixed value in "bmap"?
4701 *
4702 * In particular, "ineq" is of the form
4703 *
4704 * f(x) + n d + c >= 0
4705 *
4706 * with n > 0, c the constant term and
4707 * d the existentially quantified variable "div".
4708 * That is, the lower bound is
4709 *
4710 * ceil((-f(x) - c)/n)
4711 *
4712 * Look for a pair of constraints
4713 *
4714 * f(x) + c0 >= 0
4715 * -f(x) + c1 >= 0
4716 *
4717 * i.e., -c1 <= -f(x) <= c0, that fix ceil((-f(x) - c)/n) to a constant value.
4718 * That is, check that
4719 *
4720 * ceil((-c1 - c)/n) = ceil((c0 - c)/n)
4721 *
4722 * If so, return the index of inequality f(x) + c0 >= 0.
4723 * Otherwise, return bmap->n_ineq.
4724 * Return -1 on error.
4725 */
4726static int lower_bound_is_cst(__isl_keep isl_basic_map *bmap, int div, int ineq)
4727{
4728 int i;
4729 int lower = -1, upper = -1;
4730 unsigned o_div;
4731 isl_int l, u;
4732 int equal;
4733
4734 o_div = isl_basic_map_offset(bmap, isl_dim_div);
4735 for (i = 0; i < bmap->n_ineq && (lower < 0 || upper < 0); ++i) {
4736 isl_bool par, opp;
4737
4738 if (i == ineq)
4739 continue;
4740 if (!isl_int_is_zero(bmap->ineq[i][o_div + div])(isl_sioimath_sgn(*(bmap->ineq[i][o_div + div])) == 0))
4741 continue;
4742 par = isl_bool_false;
4743 if (lower < 0)
4744 par = is_parallel_except(bmap, ineq, i, o_div + div);
4745 if (par < 0)
4746 return -1;
4747 if (par) {
4748 lower = i;
4749 continue;
4750 }
4751 opp = isl_bool_false;
4752 if (upper < 0)
4753 opp = is_opposite_except(bmap, ineq, i, o_div + div);
4754 if (opp < 0)
4755 return -1;
4756 if (opp)
4757 upper = i;
4758 }
4759
4760 if (lower < 0 || upper < 0)
4761 return bmap->n_ineq;
4762
4763 isl_int_init(l)isl_sioimath_init((l));
4764 isl_int_init(u)isl_sioimath_init((u));
4765
4766 lower_bound_from_parallel(bmap, ineq, lower, o_div + div, &l);
4767 lower_bound_from_opposite(bmap, ineq, upper, o_div + div, &u);
4768
4769 equal = isl_int_eq(l, u)(isl_sioimath_cmp(*(l), *(u)) == 0);
4770
4771 isl_int_clear(l)isl_sioimath_clear((l));
4772 isl_int_clear(u)isl_sioimath_clear((u));
4773
4774 return equal ? lower : bmap->n_ineq;
4775}
4776
4777/* Given a lower bound constraint "ineq" on the existentially quantified
4778 * variable "div", such that the corresponding lower bound has
4779 * a fixed value in "bmap", assign this fixed value to the variable and
4780 * then try and drop redundant divs again,
4781 * freeing the temporary data structure "pairs" that was associated
4782 * to the old version of "bmap".
4783 * "lower" determines the constant value for the lower bound.
4784 *
4785 * In particular, "ineq" is of the form
4786 *
4787 * f(x) + n d + c >= 0,
4788 *
4789 * while "lower" is of the form
4790 *
4791 * f(x) + c0 >= 0
4792 *
4793 * The lower bound is ceil((-f(x) - c)/n) and its constant value
4794 * is ceil((c0 - c)/n).
4795 */
4796static __isl_give isl_basic_map *fix_cst_lower(__isl_take isl_basic_map *bmap,
4797 int div, int ineq, int lower, int *pairs)
4798{
4799 isl_int c;
4800 unsigned o_div;
4801
4802 isl_int_init(c)isl_sioimath_init((c));
4803
4804 o_div = isl_basic_map_offset(bmap, isl_dim_div);
4805 lower_bound_from_parallel(bmap, ineq, lower, o_div + div, &c);
4806 bmap = isl_basic_map_fix(bmap, isl_dim_div, div, c);
4807 free(pairs);
4808
4809 isl_int_clear(c)isl_sioimath_clear((c));
4810
4811 return isl_basic_map_drop_redundant_divs(bmap);
4812}
4813
4814/* Remove divs that are not strictly needed based on the inequality
4815 * constraints.
4816 * In particular, if a div only occurs positively (or negatively)
4817 * in constraints, then it can simply be dropped.
4818 * Also, if a div occurs in only two constraints and if moreover
4819 * those two constraints are opposite to each other, except for the constant
4820 * term and if the sum of the constant terms is such that for any value
4821 * of the other values, there is always at least one integer value of the
4822 * div, i.e., if one plus this sum is greater than or equal to
4823 * the (absolute value) of the coefficient of the div in the constraints,
4824 * then we can also simply drop the div.
4825 *
4826 * If an existentially quantified variable does not have an explicit
4827 * representation, appears in only a single lower bound that does not
4828 * involve any other such existentially quantified variables and appears
4829 * in this lower bound with coefficient 1,
4830 * then fix the variable to the value of the lower bound. That is,
4831 * turn the inequality into an equality.
4832 * If for any value of the other variables, there is any value
4833 * for the existentially quantified variable satisfying the constraints,
4834 * then this lower bound also satisfies the constraints.
4835 * It is therefore safe to pick this lower bound.
4836 *
4837 * The same reasoning holds even if the coefficient is not one.
4838 * However, fixing the variable to the value of the lower bound may
4839 * in general introduce an extra integer division, in which case
4840 * it may be better to pick another value.
4841 * If this integer division has a known constant value, then plugging
4842 * in this constant value removes the existentially quantified variable
4843 * completely. In particular, if the lower bound is of the form
4844 * ceil((-f(x) - c)/n) and there are two constraints, f(x) + c0 >= 0 and
4845 * -f(x) + c1 >= 0 such that ceil((-c1 - c)/n) = ceil((c0 - c)/n),
4846 * then the existentially quantified variable can be assigned this
4847 * shared value.
4848 *
4849 * We skip divs that appear in equalities or in the definition of other divs.
4850 * Divs that appear in the definition of other divs usually occur in at least
4851 * 4 constraints, but the constraints may have been simplified.
4852 *
4853 * If any divs are left after these simple checks then we move on
4854 * to more complicated cases in drop_more_redundant_divs.
4855 */
4856static __isl_give isl_basic_map *isl_basic_map_drop_redundant_divs_ineq(
4857 __isl_take isl_basic_map *bmap)
4858{
4859 int i, j;
4860 isl_size off;
4861 int *pairs = NULL((void*)0);
4862 int n = 0;
4863 int n_ineq;
4864
4865 if (!bmap)
4866 goto error;
4867 if (bmap->n_div == 0)
4868 return bmap;
4869
4870 off = isl_basic_map_var_offset(bmap, isl_dim_div);
4871 if (off < 0)
4872 return isl_basic_map_free(bmap);
4873 pairs = isl_calloc_array(bmap->ctx, int, bmap->n_div)((int *)isl_calloc_or_die(bmap->ctx, bmap->n_div, sizeof
(int)))
;
4874 if (!pairs)
4875 goto error;
4876
4877 n_ineq = isl_basic_map_n_inequality(bmap);
4878 for (i = 0; i < bmap->n_div; ++i) {
4879 int pos, neg;
4880 int last_pos, last_neg;
4881 int redundant;
4882 int defined;
4883 isl_bool opp, set_div;
4884
4885 defined = !isl_int_is_zero(bmap->div[i][0])(isl_sioimath_sgn(*(bmap->div[i][0])) == 0);
4886 for (j = i; j < bmap->n_div; ++j)
4887 if (!isl_int_is_zero(bmap->div[j][1 + 1 + off + i])(isl_sioimath_sgn(*(bmap->div[j][1 + 1 + off + i])) == 0))
4888 break;
4889 if (j < bmap->n_div)
4890 continue;
4891 for (j = 0; j < bmap->n_eq; ++j)
4892 if (!isl_int_is_zero(bmap->eq[j][1 + off + i])(isl_sioimath_sgn(*(bmap->eq[j][1 + off + i])) == 0))
4893 break;
4894 if (j < bmap->n_eq)
4895 continue;
4896 ++n;
4897 pos = neg = 0;
4898 for (j = 0; j < bmap->n_ineq; ++j) {
4899 if (isl_int_is_pos(bmap->ineq[j][1 + off + i])(isl_sioimath_sgn(*(bmap->ineq[j][1 + off + i])) > 0)) {
4900 last_pos = j;
4901 ++pos;
4902 }
4903 if (isl_int_is_neg(bmap->ineq[j][1 + off + i])(isl_sioimath_sgn(*(bmap->ineq[j][1 + off + i])) < 0)) {
4904 last_neg = j;
4905 ++neg;
4906 }
4907 }
4908 pairs[i] = pos * neg;
4909 if (pairs[i] == 0) {
4910 for (j = bmap->n_ineq - 1; j >= 0; --j)
4911 if (!isl_int_is_zero(bmap->ineq[j][1+off+i])(isl_sioimath_sgn(*(bmap->ineq[j][1+off+i])) == 0))
4912 isl_basic_map_drop_inequality(bmap, j);
4913 bmap = isl_basic_map_drop_div(bmap, i);
4914 return drop_redundant_divs_again(bmap, pairs, 0);
4915 }
4916 if (pairs[i] != 1)
4917 opp = isl_bool_false;
4918 else
4919 opp = is_opposite(bmap, last_pos, last_neg);
4920 if (opp < 0)
4921 goto error;
4922 if (!opp) {
4923 int lower;
4924 isl_bool single, one;
4925
4926 if (pos != 1)
4927 continue;
4928 single = single_unknown(bmap, last_pos, i);
4929 if (single < 0)
4930 goto error;
4931 if (!single)
4932 continue;
4933 one = has_coef_one(bmap, i, last_pos);
4934 if (one < 0)
4935 goto error;
4936 if (one)
4937 return set_eq_and_try_again(bmap, last_pos,
4938 pairs);
4939 lower = lower_bound_is_cst(bmap, i, last_pos);
4940 if (lower < 0)
4941 goto error;
4942 if (lower < n_ineq)
4943 return fix_cst_lower(bmap, i, last_pos, lower,
4944 pairs);
4945 continue;
4946 }
4947
4948 isl_int_add(bmap->ineq[last_pos][0],isl_sioimath_add((bmap->ineq[last_pos][0]), *(bmap->ineq
[last_pos][0]), *(bmap->ineq[last_neg][0]))
4949 bmap->ineq[last_pos][0], bmap->ineq[last_neg][0])isl_sioimath_add((bmap->ineq[last_pos][0]), *(bmap->ineq
[last_pos][0]), *(bmap->ineq[last_neg][0]))
;
4950 isl_int_add_ui(bmap->ineq[last_pos][0],isl_sioimath_add_ui((bmap->ineq[last_pos][0]), *(bmap->
ineq[last_pos][0]), 1)
4951 bmap->ineq[last_pos][0], 1)isl_sioimath_add_ui((bmap->ineq[last_pos][0]), *(bmap->
ineq[last_pos][0]), 1)
;
4952 redundant = isl_int_ge(bmap->ineq[last_pos][0],(isl_sioimath_cmp(*(bmap->ineq[last_pos][0]), *(bmap->ineq
[last_pos][1+off+i])) >= 0)
4953 bmap->ineq[last_pos][1+off+i])(isl_sioimath_cmp(*(bmap->ineq[last_pos][0]), *(bmap->ineq
[last_pos][1+off+i])) >= 0)
;
4954 isl_int_sub_ui(bmap->ineq[last_pos][0],isl_sioimath_sub_ui((bmap->ineq[last_pos][0]), *(bmap->
ineq[last_pos][0]), 1)
4955 bmap->ineq[last_pos][0], 1)isl_sioimath_sub_ui((bmap->ineq[last_pos][0]), *(bmap->
ineq[last_pos][0]), 1)
;
4956 isl_int_sub(bmap->ineq[last_pos][0],isl_sioimath_sub((bmap->ineq[last_pos][0]), *(bmap->ineq
[last_pos][0]), *(bmap->ineq[last_neg][0]))
4957 bmap->ineq[last_pos][0], bmap->ineq[last_neg][0])isl_sioimath_sub((bmap->ineq[last_pos][0]), *(bmap->ineq
[last_pos][0]), *(bmap->ineq[last_neg][0]))
;
4958 if (redundant)
4959 return drop_div_and_try_again(bmap, i,
4960 last_pos, last_neg, pairs);
4961 if (defined)
4962 set_div = isl_bool_false;
4963 else
4964 set_div = ok_to_set_div_from_bound(bmap, i, last_pos);
4965 if (set_div < 0)
4966 return isl_basic_map_free(bmap);
4967 if (set_div) {
4968 bmap = set_div_from_lower_bound(bmap, i, last_pos);
4969 return drop_redundant_divs_again(bmap, pairs, 1);
4970 }
4971 pairs[i] = 0;
4972 --n;
4973 }
4974
4975 if (n > 0)
4976 return coalesce_or_drop_more_redundant_divs(bmap, pairs, n);
4977
4978 free(pairs);
4979 return bmap;
4980error:
4981 free(pairs);
4982 isl_basic_map_free(bmap);
4983 return NULL((void*)0);
4984}
4985
4986/* Consider the coefficients at "c" as a row vector and replace
4987 * them with their product with "T". "T" is assumed to be a square matrix.
4988 */
4989static isl_stat preimage(isl_int *c, __isl_keep isl_mat *T)
4990{
4991 isl_size n;
4992 isl_ctx *ctx;
4993 isl_vec *v;
4994
4995 n = isl_mat_rows(T);
4996 if (n < 0)
4997 return isl_stat_error;
4998 if (isl_seq_first_non_zero(c, n) == -1)
4999 return isl_stat_ok;
5000 ctx = isl_mat_get_ctx(T);
5001 v = isl_vec_alloc(ctx, n);
5002 if (!v)
5003 return isl_stat_error;
5004 isl_seq_swp_or_cpy(v->el, c, n);
5005 v = isl_vec_mat_product(v, isl_mat_copy(T));
5006 if (!v)
5007 return isl_stat_error;
5008 isl_seq_swp_or_cpy(c, v->el, n);
5009 isl_vec_free(v);
5010
5011 return isl_stat_ok;
5012}
5013
5014/* Plug in T for the variables in "bmap" starting at "pos".
5015 * T is a linear unimodular matrix, i.e., without constant term.
5016 */
5017static __isl_give isl_basic_map *isl_basic_map_preimage_vars(
5018 __isl_take isl_basic_map *bmap, unsigned pos, __isl_take isl_mat *T)
5019{
5020 int i;
5021 isl_size n_row, n_col;
5022
5023 bmap = isl_basic_map_cow(bmap);
5024 n_row = isl_mat_rows(T);
5025 n_col = isl_mat_cols(T);
5026 if (!bmap || n_row < 0 || n_col < 0)
5027 goto error;
5028
5029 if (n_col != n_row)
5030 isl_die(isl_mat_get_ctx(T), isl_error_invalid,do { isl_handle_error(isl_mat_get_ctx(T), isl_error_invalid, "expecting square matrix"
, "/build/llvm-toolchain-snapshot-11~++20200309111110+2c36c23f347/polly/lib/External/isl/isl_map_simplify.c"
, 5031); goto error; } while (0)
5031 "expecting square matrix", goto error)do { isl_handle_error(isl_mat_get_ctx(T), isl_error_invalid, "expecting square matrix"
, "/build/llvm-toolchain-snapshot-11~++20200309111110+2c36c23f347/polly/lib/External/isl/isl_map_simplify.c"
, 5031); goto error; } while (0)
;
5032
5033 if (isl_basic_map_check_range(bmap, isl_dim_all, pos, n_col) < 0)
5034 goto error;
5035
5036 for (i = 0; i < bmap->n_eq; ++i)
5037 if (preimage(bmap->eq[i] + 1 + pos, T) < 0)
5038 goto error;
5039 for (i = 0; i < bmap->n_ineq; ++i)
5040 if (preimage(bmap->ineq[i] + 1 + pos, T) < 0)
5041 goto error;
5042 for (i = 0; i < bmap->n_div; ++i) {
5043 if (isl_basic_map_div_is_marked_unknown(bmap, i))
5044 continue;
5045 if (preimage(bmap->div[i] + 1 + 1 + pos, T) < 0)
5046 goto error;
5047 }
5048
5049 isl_mat_free(T);
5050 return bmap;
5051error:
5052 isl_basic_map_free(bmap);
5053 isl_mat_free(T);
5054 return NULL((void*)0);
5055}
5056
5057/* Remove divs that are not strictly needed.
5058 *
5059 * First look for an equality constraint involving two or more
5060 * existentially quantified variables without an explicit
5061 * representation. Replace the combination that appears
5062 * in the equality constraint by a single existentially quantified
5063 * variable such that the equality can be used to derive
5064 * an explicit representation for the variable.
5065 * If there are no more such equality constraints, then continue
5066 * with isl_basic_map_drop_redundant_divs_ineq.
5067 *
5068 * In particular, if the equality constraint is of the form
5069 *
5070 * f(x) + \sum_i c_i a_i = 0
5071 *
5072 * with a_i existentially quantified variable without explicit
5073 * representation, then apply a transformation on the existentially
5074 * quantified variables to turn the constraint into
5075 *
5076 * f(x) + g a_1' = 0
5077 *
5078 * with g the gcd of the c_i.
5079 * In order to easily identify which existentially quantified variables
5080 * have a complete explicit representation, i.e., without being defined
5081 * in terms of other existentially quantified variables without
5082 * an explicit representation, the existentially quantified variables
5083 * are first sorted.
5084 *
5085 * The variable transformation is computed by extending the row
5086 * [c_1/g ... c_n/g] to a unimodular matrix, obtaining the transformation
5087 *
5088 * [a_1'] [c_1/g ... c_n/g] [ a_1 ]
5089 * [a_2'] [ a_2 ]
5090 * ... = U ....
5091 * [a_n'] [ a_n ]
5092 *
5093 * with [c_1/g ... c_n/g] representing the first row of U.
5094 * The inverse of U is then plugged into the original constraints.
5095 * The call to isl_basic_map_simplify makes sure the explicit
5096 * representation for a_1' is extracted from the equality constraint.
5097 */
5098__isl_give isl_basic_map *isl_basic_map_drop_redundant_divs(
5099 __isl_take isl_basic_map *bmap)
5100{
5101 int first;
5102 int i;
5103 unsigned o_div;
5104 isl_size n_div;
5105 int l;
5106 isl_ctx *ctx;
5107 isl_mat *T;
5108
5109 if (!bmap)
5110 return NULL((void*)0);
5111 if (isl_basic_map_divs_known(bmap))
5112 return isl_basic_map_drop_redundant_divs_ineq(bmap);
5113 if (bmap->n_eq == 0)
5114 return isl_basic_map_drop_redundant_divs_ineq(bmap);
5115 bmap = isl_basic_map_sort_divs(bmap);
5116 if (!bmap)
5117 return NULL((void*)0);
5118
5119 first = isl_basic_map_first_unknown_div(bmap);
5120 if (first < 0)
5121 return isl_basic_map_free(bmap);
5122
5123 o_div = isl_basic_map_offset(bmap, isl_dim_div);
5124 n_div = isl_basic_map_dim(bmap, isl_dim_div);
5125 if (n_div < 0)
5126 return isl_basic_map_free(bmap);
5127
5128 for (i = 0; i < bmap->n_eq; ++i) {
5129 l = isl_seq_first_non_zero(bmap->eq[i] + o_div + first,
5130 n_div - (first));
5131 if (l < 0)
5132 continue;
5133 l += first;
5134 if (isl_seq_first_non_zero(bmap->eq[i] + o_div + l + 1,
5135 n_div - (l + 1)) == -1)
5136 continue;
5137 break;
5138 }
5139 if (i >= bmap->n_eq)
5140 return isl_basic_map_drop_redundant_divs_ineq(bmap);
5141
5142 ctx = isl_basic_map_get_ctx(bmap);
5143 T = isl_mat_alloc(ctx, n_div - l, n_div - l);
5144 if (!T)
5145 return isl_basic_map_free(bmap);
5146 isl_seq_cpy(T->row[0], bmap->eq[i] + o_div + l, n_div - l);
5147 T = isl_mat_normalize_row(T, 0);
5148 T = isl_mat_unimodular_complete(T, 1);
5149 T = isl_mat_right_inverse(T);
5150
5151 for (i = l; i < n_div; ++i)
5152 bmap = isl_basic_map_mark_div_unknown(bmap, i);
5153 bmap = isl_basic_map_preimage_vars(bmap, o_div - 1 + l, T);
5154 bmap = isl_basic_map_simplify(bmap);
5155
5156 return isl_basic_map_drop_redundant_divs(bmap);
5157}
5158
5159/* Does "bmap" satisfy any equality that involves more than 2 variables
5160 * and/or has coefficients different from -1 and 1?
5161 */
5162static isl_bool has_multiple_var_equality(__isl_keep isl_basic_map *bmap)
5163{
5164 int i;
5165 isl_size total;
5166
5167 total = isl_basic_map_dim(bmap, isl_dim_all);
5168 if (total < 0)
5169 return isl_bool_error;
5170
5171 for (i = 0; i < bmap->n_eq; ++i) {
5172 int j, k;
5173
5174 j = isl_seq_first_non_zero(bmap->eq[i] + 1, total);
5175 if (j < 0)
5176 continue;
5177 if (!isl_int_is_one(bmap->eq[i][1 + j])(isl_sioimath_cmp_si(*(bmap->eq[i][1 + j]), 1) == 0) &&
5178 !isl_int_is_negone(bmap->eq[i][1 + j])(isl_sioimath_cmp_si(*(bmap->eq[i][1 + j]), -1) == 0))
5179 return isl_bool_true;
5180
5181 j += 1;
5182 k = isl_seq_first_non_zero(bmap->eq[i] + 1 + j, total - j);
5183 if (k < 0)
5184 continue;
5185 j += k;
5186 if (!isl_int_is_one(bmap->eq[i][1 + j])(isl_sioimath_cmp_si(*(bmap->eq[i][1 + j]), 1) == 0) &&
5187 !isl_int_is_negone(bmap->eq[i][1 + j])(isl_sioimath_cmp_si(*(bmap->eq[i][1 + j]), -1) == 0))
5188 return isl_bool_true;
5189
5190 j += 1;
5191 k = isl_seq_first_non_zero(bmap->eq[i] + 1 + j, total - j);
5192 if (k >= 0)
5193 return isl_bool_true;
5194 }
5195
5196 return isl_bool_false;
5197}
5198
5199/* Remove any common factor g from the constraint coefficients in "v".
5200 * The constant term is stored in the first position and is replaced
5201 * by floor(c/g). If any common factor is removed and if this results
5202 * in a tightening of the constraint, then set *tightened.
5203 */
5204static __isl_give isl_vec *normalize_constraint(__isl_take isl_vec *v,
5205 int *tightened)
5206{
5207 isl_ctx *ctx;
5208
5209 if (!v)
5210 return NULL((void*)0);
5211 ctx = isl_vec_get_ctx(v);
5212 isl_seq_gcd(v->el + 1, v->size - 1, &ctx->normalize_gcd);
5213 if (isl_int_is_zero(ctx->normalize_gcd)(isl_sioimath_sgn(*(ctx->normalize_gcd)) == 0))
5214 return v;
5215 if (isl_int_is_one(ctx->normalize_gcd)(isl_sioimath_cmp_si(*(ctx->normalize_gcd), 1) == 0))
5216 return v;
5217 v = isl_vec_cow(v);
5218 if (!v)
5219 return NULL((void*)0);
5220 if (tightened && !isl_int_is_divisible_by(v->el[0], ctx->normalize_gcd)isl_sioimath_is_divisible_by(*(v->el[0]), *(ctx->normalize_gcd
))
)
5221 *tightened = 1;
5222 isl_int_fdiv_q(v->el[0], v->el[0], ctx->normalize_gcd)isl_sioimath_fdiv_q((v->el[0]), *(v->el[0]), *(ctx->
normalize_gcd))
;
5223 isl_seq_scale_down(v->el + 1, v->el + 1, ctx->normalize_gcd,
5224 v->size - 1);
5225 return v;
5226}
5227
5228/* If "bmap" is an integer set that satisfies any equality involving
5229 * more than 2 variables and/or has coefficients different from -1 and 1,
5230 * then use variable compression to reduce the coefficients by removing
5231 * any (hidden) common factor.
5232 * In particular, apply the variable compression to each constraint,
5233 * factor out any common factor in the non-constant coefficients and
5234 * then apply the inverse of the compression.
5235 * At the end, we mark the basic map as having reduced constants.
5236 * If this flag is still set on the next invocation of this function,
5237 * then we skip the computation.
5238 *
5239 * Removing a common factor may result in a tightening of some of
5240 * the constraints. If this happens, then we may end up with two
5241 * opposite inequalities that can be replaced by an equality.
5242 * We therefore call isl_basic_map_detect_inequality_pairs,
5243 * which checks for such pairs of inequalities as well as eliminate_divs_eq
5244 * and isl_basic_map_gauss if such a pair was found.
5245 *
5246 * Tightening may also result in some other constraints becoming
5247 * (rationally) redundant with respect to the tightened constraint
5248 * (in combination with other constraints). The basic map may
5249 * therefore no longer be assumed to have no redundant constraints.
5250 *
5251 * Note that this function may leave the result in an inconsistent state.
5252 * In particular, the constraints may not be gaussed.
5253 * Unfortunately, isl_map_coalesce actually depends on this inconsistent state
5254 * for some of the test cases to pass successfully.
5255 * Any potential modification of the representation is therefore only
5256 * performed on a single copy of the basic map.
5257 */
5258__isl_give isl_basic_map *isl_basic_map_reduce_coefficients(
5259 __isl_take isl_basic_map *bmap)
5260{
5261 isl_size total;
5262 isl_bool multi;
5263 isl_ctx *ctx;
5264 isl_vec *v;
5265 isl_mat *eq, *T, *T2;
5266 int i;
5267 int tightened;
5268
5269 if (!bmap)
1
Assuming 'bmap' is non-null
2
Taking false branch
5270 return NULL((void*)0);
5271 if (ISL_F_ISSET(bmap, ISL_BASIC_MAP_REDUCED_COEFFICIENTS)(!!(((bmap)->flags) & ((1 << 8)))))
3
Assuming the condition is true
4
Taking false branch
5272 return bmap;
5273 if (isl_basic_map_is_rational(bmap))
5
Assuming the condition is false
6
Taking false branch
5274 return bmap;
5275 if (bmap->n_eq == 0)
7
Assuming field 'n_eq' is not equal to 0
8
Taking false branch
5276 return bmap;
5277 multi = has_multiple_var_equality(bmap);
5278 if (multi
8.1
'multi' is >= 0
< 0)
9
Taking false branch
5279 return isl_basic_map_free(bmap);
5280 if (!multi
9.1
'multi' is 1
)
10
Taking false branch
5281 return bmap;
5282
5283 total = isl_basic_map_dim(bmap, isl_dim_all);
5284 if (total < 0)
11
Assuming 'total' is >= 0
12
Taking false branch
5285 return isl_basic_map_free(bmap);
5286 ctx = isl_basic_map_get_ctx(bmap);
5287 v = isl_vec_alloc(ctx, 1 + total);
5288 if (!v)
13
Assuming 'v' is non-null
14
Taking false branch
5289 return isl_basic_map_free(bmap);
5290
5291 eq = isl_mat_sub_alloc6(ctx, bmap->eq, 0, bmap->n_eq, 0, 1 + total);
5292 T = isl_mat_variable_compression(eq, &T2);
5293 if (!T || !T2)
15
Assuming 'T' is non-null
16
Assuming 'T2' is non-null
17
Taking false branch
5294 goto error;
5295 if (T->n_col == 0) {
18
Assuming field 'n_col' is not equal to 0
19
Taking false branch
5296 isl_mat_free(T);
5297 isl_mat_free(T2);
5298 isl_vec_free(v);
5299 return isl_basic_map_set_to_empty(bmap);
5300 }
5301
5302 bmap = isl_basic_map_cow(bmap);
5303 if (!bmap)
20
Assuming 'bmap' is non-null
21
Taking false branch
5304 goto error;
5305
5306 tightened = 0;
5307 for (i = 0; i < bmap->n_ineq; ++i) {
22
Assuming 'i' is < field 'n_ineq'
23
Loop condition is true. Entering loop body
26
Assuming 'i' is < field 'n_ineq'
27
Loop condition is true. Entering loop body
30
Assuming 'i' is >= field 'n_ineq'
31
Loop condition is false. Execution continues on line 5317
5308 isl_seq_cpy(v->el, bmap->ineq[i], 1 + total);
5309 v = isl_vec_mat_product(v, isl_mat_copy(T));
5310 v = normalize_constraint(v, &tightened);
5311 v = isl_vec_mat_product(v, isl_mat_copy(T2));
5312 if (!v)
24
Assuming 'v' is non-null
25
Taking false branch
28
Assuming 'v' is non-null
29
Taking false branch
5313 goto error;
5314 isl_seq_cpy(bmap->ineq[i], v->el, 1 + total);
5315 }
5316
5317 isl_mat_free(T);
5318 isl_mat_free(T2);
5319 isl_vec_free(v);
5320
5321 ISL_F_SET(bmap, ISL_BASIC_MAP_REDUCED_COEFFICIENTS)(((bmap)->flags) |= ((1 << 8)));
5322
5323 if (tightened
31.1
'tightened' is 1
) {
32
Taking true branch
5324 int progress = 0;
5325
5326 ISL_F_CLR(bmap, ISL_BASIC_MAP_NO_REDUNDANT)(((bmap)->flags) &= ~((1 << 3)));
5327 bmap = isl_basic_map_detect_inequality_pairs(bmap, &progress);
33
Calling 'isl_basic_map_detect_inequality_pairs'
5328 if (progress) {
5329 bmap = eliminate_divs_eq(bmap, &progress);
5330 bmap = isl_basic_map_gauss(bmap, NULL((void*)0));
5331 }
5332 }
5333
5334 return bmap;
5335error:
5336 isl_mat_free(T);
5337 isl_mat_free(T2);
5338 isl_vec_free(v);
5339 return isl_basic_map_free(bmap);
5340}
5341
5342/* Shift the integer division at position "div" of "bmap"
5343 * by "shift" times the variable at position "pos".
5344 * "pos" is as determined by isl_basic_map_offset, i.e., pos == 0
5345 * corresponds to the constant term.
5346 *
5347 * That is, if the integer division has the form
5348 *
5349 * floor(f(x)/d)
5350 *
5351 * then replace it by
5352 *
5353 * floor((f(x) + shift * d * x_pos)/d) - shift * x_pos
5354 */
5355__isl_give isl_basic_map *isl_basic_map_shift_div(
5356 __isl_take isl_basic_map *bmap, int div, int pos, isl_int shift)
5357{
5358 int i;
5359 isl_size total, n_div;
5360
5361 if (isl_int_is_zero(shift)(isl_sioimath_sgn(*(shift)) == 0))
5362 return bmap;
5363 total = isl_basic_map_dim(bmap, isl_dim_all);
5364 n_div = isl_basic_map_dim(bmap, isl_dim_div);
5365 total -= n_div;
5366 if (total < 0 || n_div < 0)
5367 return isl_basic_map_free(bmap);
5368
5369 isl_int_addmul(bmap->div[div][1 + pos], shift, bmap->div[div][0])isl_sioimath_addmul((bmap->div[div][1 + pos]), *(shift), *
(bmap->div[div][0]))
;
5370
5371 for (i = 0; i < bmap->n_eq; ++i) {
5372 if (isl_int_is_zero(bmap->eq[i][1 + total + div])(isl_sioimath_sgn(*(bmap->eq[i][1 + total + div])) == 0))
5373 continue;
5374 isl_int_submul(bmap->eq[i][pos],isl_sioimath_submul((bmap->eq[i][pos]), *(shift), *(bmap->
eq[i][1 + total + div]))
5375 shift, bmap->eq[i][1 + total + div])isl_sioimath_submul((bmap->eq[i][pos]), *(shift), *(bmap->
eq[i][1 + total + div]))
;
5376 }
5377 for (i = 0; i < bmap->n_ineq; ++i) {
5378 if (isl_int_is_zero(bmap->ineq[i][1 + total + div])(isl_sioimath_sgn(*(bmap->ineq[i][1 + total + div])) == 0))
5379 continue;
5380 isl_int_submul(bmap->ineq[i][pos],isl_sioimath_submul((bmap->ineq[i][pos]), *(shift), *(bmap
->ineq[i][1 + total + div]))
5381 shift, bmap->ineq[i][1 + total + div])isl_sioimath_submul((bmap->ineq[i][pos]), *(shift), *(bmap
->ineq[i][1 + total + div]))
;
5382 }
5383 for (i = 0; i < bmap->n_div; ++i) {
5384 if (isl_int_is_zero(bmap->div[i][0])(isl_sioimath_sgn(*(bmap->div[i][0])) == 0))
5385 continue;
5386 if (isl_int_is_zero(bmap->div[i][1 + 1 + total + div])(isl_sioimath_sgn(*(bmap->div[i][1 + 1 + total + div])) ==
0)
)
5387 continue;
5388 isl_int_submul(bmap->div[i][1 + pos],isl_sioimath_submul((bmap->div[i][1 + pos]), *(shift), *(bmap
->div[i][1 + 1 + total + div]))
5389 shift, bmap->div[i][1 + 1 + total + div])isl_sioimath_submul((bmap->div[i][1 + pos]), *(shift), *(bmap
->div[i][1 + 1 + total + div]))
;
5390 }
5391
5392 return bmap;
5393}