Bug Summary

File:tools/polly/lib/External/isl/isl_int_sioimath.h
Warning:line 580, column 7
Assigned value is garbage or undefined

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_tab.c -analyzer-store=region -analyzer-opt-analyze-nested-blocks -analyzer-eagerly-assume -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 -mrelocation-model pic -pic-level 2 -mthread-model posix -fmath-errno -masm-verbose -mconstructor-aliases -munwind-tables -fuse-init-array -target-cpu x86-64 -dwarf-column-info -debugger-tuning=gdb -momit-leaf-frame-pointer -ffunction-sections -fdata-sections -resource-dir /usr/lib/llvm-7/lib/clang/7.0.0 -D _DEBUG -D _GNU_SOURCE -D __STDC_CONSTANT_MACROS -D __STDC_FORMAT_MACROS -D __STDC_LIMIT_MACROS -I /build/llvm-toolchain-snapshot-7~svn338205/build-llvm/tools/polly/lib/External -I /build/llvm-toolchain-snapshot-7~svn338205/tools/polly/lib/External -I /build/llvm-toolchain-snapshot-7~svn338205/tools/polly/lib/External/pet/include -I /build/llvm-toolchain-snapshot-7~svn338205/tools/polly/lib/External/ppcg/include -I /build/llvm-toolchain-snapshot-7~svn338205/tools/polly/lib/External/ppcg/imath -I /build/llvm-toolchain-snapshot-7~svn338205/build-llvm/tools/polly/lib/External/ppcg -I /build/llvm-toolchain-snapshot-7~svn338205/tools/polly/lib/External/isl -I /build/llvm-toolchain-snapshot-7~svn338205/tools/polly/lib/External/isl/include -I /build/llvm-toolchain-snapshot-7~svn338205/tools/polly/lib/External/isl/imath -I /build/llvm-toolchain-snapshot-7~svn338205/build-llvm/tools/polly/lib/External/isl -I /build/llvm-toolchain-snapshot-7~svn338205/build-llvm/tools/polly/include -I /usr/include/jsoncpp -I /build/llvm-toolchain-snapshot-7~svn338205/build-llvm/tools/polly/lib/External/isl/include -I /build/llvm-toolchain-snapshot-7~svn338205/tools/polly/include -I /build/llvm-toolchain-snapshot-7~svn338205/build-llvm/include -I /build/llvm-toolchain-snapshot-7~svn338205/include -U NDEBUG -internal-isystem /usr/local/include -internal-isystem /usr/lib/llvm-7/lib/clang/7.0.0/include -internal-externc-isystem /usr/lib/gcc/x86_64-linux-gnu/8/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-7~svn338205/build-llvm/tools/polly/lib/External -fdebug-prefix-map=/build/llvm-toolchain-snapshot-7~svn338205=. -ferror-limit 19 -fmessage-length 0 -stack-protector 2 -fobjc-runtime=gcc -fdiagnostics-show-option -vectorize-loops -vectorize-slp -analyzer-output=html -analyzer-config stable-report-filename=true -o /tmp/scan-build-2018-07-29-043837-17923-1 -x c /build/llvm-toolchain-snapshot-7~svn338205/tools/polly/lib/External/isl/isl_tab.c -faddrsig

/build/llvm-toolchain-snapshot-7~svn338205/tools/polly/lib/External/isl/isl_tab.c

1/*
2 * Copyright 2008-2009 Katholieke Universiteit Leuven
3 * Copyright 2013 Ecole Normale Superieure
4 * Copyright 2014 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_mat_private.h>
18#include <isl_vec_private.h>
19#include "isl_map_private.h"
20#include "isl_tab.h"
21#include <isl_seq.h>
22#include <isl_config.h>
23
24#include <bset_to_bmap.c>
25#include <bset_from_bmap.c>
26
27/*
28 * The implementation of tableaus in this file was inspired by Section 8
29 * of David Detlefs, Greg Nelson and James B. Saxe, "Simplify: a theorem
30 * prover for program checking".
31 */
32
33struct isl_tab *isl_tab_alloc(struct isl_ctx *ctx,
34 unsigned n_row, unsigned n_var, unsigned M)
35{
36 int i;
37 struct isl_tab *tab;
38 unsigned off = 2 + M;
39
40 tab = isl_calloc_type(ctx, struct isl_tab)((struct isl_tab *)isl_calloc_or_die(ctx, 1, sizeof(struct isl_tab
)))
;
41 if (!tab)
42 return NULL((void*)0);
43 tab->mat = isl_mat_alloc(ctx, n_row, off + n_var);
44 if (!tab->mat)
45 goto error;
46 tab->var = isl_alloc_array(ctx, struct isl_tab_var, n_var)((struct isl_tab_var *)isl_malloc_or_die(ctx, (n_var)*sizeof(
struct isl_tab_var)))
;
47 if (n_var && !tab->var)
48 goto error;
49 tab->con = isl_alloc_array(ctx, struct isl_tab_var, n_row)((struct isl_tab_var *)isl_malloc_or_die(ctx, (n_row)*sizeof(
struct isl_tab_var)))
;
50 if (n_row && !tab->con)
51 goto error;
52 tab->col_var = isl_alloc_array(ctx, int, n_var)((int *)isl_malloc_or_die(ctx, (n_var)*sizeof(int)));
53 if (n_var && !tab->col_var)
54 goto error;
55 tab->row_var = isl_alloc_array(ctx, int, n_row)((int *)isl_malloc_or_die(ctx, (n_row)*sizeof(int)));
56 if (n_row && !tab->row_var)
57 goto error;
58 for (i = 0; i < n_var; ++i) {
59 tab->var[i].index = i;
60 tab->var[i].is_row = 0;
61 tab->var[i].is_nonneg = 0;
62 tab->var[i].is_zero = 0;
63 tab->var[i].is_redundant = 0;
64 tab->var[i].frozen = 0;
65 tab->var[i].negated = 0;
66 tab->col_var[i] = i;
67 }
68 tab->n_row = 0;
69 tab->n_con = 0;
70 tab->n_eq = 0;
71 tab->max_con = n_row;
72 tab->n_col = n_var;
73 tab->n_var = n_var;
74 tab->max_var = n_var;
75 tab->n_param = 0;
76 tab->n_div = 0;
77 tab->n_dead = 0;
78 tab->n_redundant = 0;
79 tab->strict_redundant = 0;
80 tab->need_undo = 0;
81 tab->rational = 0;
82 tab->empty = 0;
83 tab->in_undo = 0;
84 tab->M = M;
85 tab->cone = 0;
86 tab->bottom.type = isl_tab_undo_bottom;
87 tab->bottom.next = NULL((void*)0);
88 tab->top = &tab->bottom;
89
90 tab->n_zero = 0;
91 tab->n_unbounded = 0;
92 tab->basis = NULL((void*)0);
93
94 return tab;
95error:
96 isl_tab_free(tab);
97 return NULL((void*)0);
98}
99
100isl_ctx *isl_tab_get_ctx(struct isl_tab *tab)
101{
102 return tab ? isl_mat_get_ctx(tab->mat) : NULL((void*)0);
103}
104
105int isl_tab_extend_cons(struct isl_tab *tab, unsigned n_new)
106{
107 unsigned off;
108
109 if (!tab)
110 return -1;
111
112 off = 2 + tab->M;
113
114 if (tab->max_con < tab->n_con + n_new) {
115 struct isl_tab_var *con;
116
117 con = isl_realloc_array(tab->mat->ctx, tab->con,((struct isl_tab_var *)isl_realloc_or_die(tab->mat->ctx
, tab->con, (tab->max_con + n_new)*sizeof(struct isl_tab_var
)))
118 struct isl_tab_var, tab->max_con + n_new)((struct isl_tab_var *)isl_realloc_or_die(tab->mat->ctx
, tab->con, (tab->max_con + n_new)*sizeof(struct isl_tab_var
)))
;
119 if (!con)
120 return -1;
121 tab->con = con;
122 tab->max_con += n_new;
123 }
124 if (tab->mat->n_row < tab->n_row + n_new) {
125 int *row_var;
126
127 tab->mat = isl_mat_extend(tab->mat,
128 tab->n_row + n_new, off + tab->n_col);
129 if (!tab->mat)
130 return -1;
131 row_var = isl_realloc_array(tab->mat->ctx, tab->row_var,((int *)isl_realloc_or_die(tab->mat->ctx, tab->row_var
, (tab->mat->n_row)*sizeof(int)))
132 int, tab->mat->n_row)((int *)isl_realloc_or_die(tab->mat->ctx, tab->row_var
, (tab->mat->n_row)*sizeof(int)))
;
133 if (!row_var)
134 return -1;
135 tab->row_var = row_var;
136 if (tab->row_sign) {
137 enum isl_tab_row_sign *s;
138 s = isl_realloc_array(tab->mat->ctx, tab->row_sign,((enum isl_tab_row_sign *)isl_realloc_or_die(tab->mat->
ctx, tab->row_sign, (tab->mat->n_row)*sizeof(enum isl_tab_row_sign
)))
139 enum isl_tab_row_sign, tab->mat->n_row)((enum isl_tab_row_sign *)isl_realloc_or_die(tab->mat->
ctx, tab->row_sign, (tab->mat->n_row)*sizeof(enum isl_tab_row_sign
)))
;
140 if (!s)
141 return -1;
142 tab->row_sign = s;
143 }
144 }
145 return 0;
146}
147
148/* Make room for at least n_new extra variables.
149 * Return -1 if anything went wrong.
150 */
151int isl_tab_extend_vars(struct isl_tab *tab, unsigned n_new)
152{
153 struct isl_tab_var *var;
154 unsigned off = 2 + tab->M;
155
156 if (tab->max_var < tab->n_var + n_new) {
157 var = isl_realloc_array(tab->mat->ctx, tab->var,((struct isl_tab_var *)isl_realloc_or_die(tab->mat->ctx
, tab->var, (tab->n_var + n_new)*sizeof(struct isl_tab_var
)))
158 struct isl_tab_var, tab->n_var + n_new)((struct isl_tab_var *)isl_realloc_or_die(tab->mat->ctx
, tab->var, (tab->n_var + n_new)*sizeof(struct isl_tab_var
)))
;
159 if (!var)
160 return -1;
161 tab->var = var;
162 tab->max_var = tab->n_var + n_new;
163 }
164
165 if (tab->mat->n_col < off + tab->n_col + n_new) {
166 int *p;
167
168 tab->mat = isl_mat_extend(tab->mat,
169 tab->mat->n_row, off + tab->n_col + n_new);
170 if (!tab->mat)
171 return -1;
172 p = isl_realloc_array(tab->mat->ctx, tab->col_var,((int *)isl_realloc_or_die(tab->mat->ctx, tab->col_var
, (tab->n_col + n_new)*sizeof(int)))
173 int, tab->n_col + n_new)((int *)isl_realloc_or_die(tab->mat->ctx, tab->col_var
, (tab->n_col + n_new)*sizeof(int)))
;
174 if (!p)
175 return -1;
176 tab->col_var = p;
177 }
178
179 return 0;
180}
181
182static void free_undo_record(struct isl_tab_undo *undo)
183{
184 switch (undo->type) {
185 case isl_tab_undo_saved_basis:
186 free(undo->u.col_var);
187 break;
188 default:;
189 }
190 free(undo);
191}
192
193static void free_undo(struct isl_tab *tab)
194{
195 struct isl_tab_undo *undo, *next;
196
197 for (undo = tab->top; undo && undo != &tab->bottom; undo = next) {
198 next = undo->next;
199 free_undo_record(undo);
200 }
201 tab->top = undo;
202}
203
204void isl_tab_free(struct isl_tab *tab)
205{
206 if (!tab)
207 return;
208 free_undo(tab);
209 isl_mat_free(tab->mat);
210 isl_vec_free(tab->dual);
211 isl_basic_map_free(tab->bmap);
212 free(tab->var);
213 free(tab->con);
214 free(tab->row_var);
215 free(tab->col_var);
216 free(tab->row_sign);
217 isl_mat_free(tab->samples);
218 free(tab->sample_index);
219 isl_mat_free(tab->basis);
220 free(tab);
221}
222
223struct isl_tab *isl_tab_dup(struct isl_tab *tab)
224{
225 int i;
226 struct isl_tab *dup;
227 unsigned off;
228
229 if (!tab)
230 return NULL((void*)0);
231
232 off = 2 + tab->M;
233 dup = isl_calloc_type(tab->mat->ctx, struct isl_tab)((struct isl_tab *)isl_calloc_or_die(tab->mat->ctx, 1, sizeof
(struct isl_tab)))
;
234 if (!dup)
235 return NULL((void*)0);
236 dup->mat = isl_mat_dup(tab->mat);
237 if (!dup->mat)
238 goto error;
239 dup->var = isl_alloc_array(tab->mat->ctx, struct isl_tab_var, tab->max_var)((struct isl_tab_var *)isl_malloc_or_die(tab->mat->ctx,
(tab->max_var)*sizeof(struct isl_tab_var)))
;
240 if (tab->max_var && !dup->var)
241 goto error;
242 for (i = 0; i < tab->n_var; ++i)
243 dup->var[i] = tab->var[i];
244 dup->con = isl_alloc_array(tab->mat->ctx, struct isl_tab_var, tab->max_con)((struct isl_tab_var *)isl_malloc_or_die(tab->mat->ctx,
(tab->max_con)*sizeof(struct isl_tab_var)))
;
245 if (tab->max_con && !dup->con)
246 goto error;
247 for (i = 0; i < tab->n_con; ++i)
248 dup->con[i] = tab->con[i];
249 dup->col_var = isl_alloc_array(tab->mat->ctx, int, tab->mat->n_col - off)((int *)isl_malloc_or_die(tab->mat->ctx, (tab->mat->
n_col - off)*sizeof(int)))
;
250 if ((tab->mat->n_col - off) && !dup->col_var)
251 goto error;
252 for (i = 0; i < tab->n_col; ++i)
253 dup->col_var[i] = tab->col_var[i];
254 dup->row_var = isl_alloc_array(tab->mat->ctx, int, tab->mat->n_row)((int *)isl_malloc_or_die(tab->mat->ctx, (tab->mat->
n_row)*sizeof(int)))
;
255 if (tab->mat->n_row && !dup->row_var)
256 goto error;
257 for (i = 0; i < tab->n_row; ++i)
258 dup->row_var[i] = tab->row_var[i];
259 if (tab->row_sign) {
260 dup->row_sign = isl_alloc_array(tab->mat->ctx, enum isl_tab_row_sign,((enum isl_tab_row_sign *)isl_malloc_or_die(tab->mat->ctx
, (tab->mat->n_row)*sizeof(enum isl_tab_row_sign)))
261 tab->mat->n_row)((enum isl_tab_row_sign *)isl_malloc_or_die(tab->mat->ctx
, (tab->mat->n_row)*sizeof(enum isl_tab_row_sign)))
;
262 if (tab->mat->n_row && !dup->row_sign)
263 goto error;
264 for (i = 0; i < tab->n_row; ++i)
265 dup->row_sign[i] = tab->row_sign[i];
266 }
267 if (tab->samples) {
268 dup->samples = isl_mat_dup(tab->samples);
269 if (!dup->samples)
270 goto error;
271 dup->sample_index = isl_alloc_array(tab->mat->ctx, int,((int *)isl_malloc_or_die(tab->mat->ctx, (tab->samples
->n_row)*sizeof(int)))
272 tab->samples->n_row)((int *)isl_malloc_or_die(tab->mat->ctx, (tab->samples
->n_row)*sizeof(int)))
;
273 if (tab->samples->n_row && !dup->sample_index)
274 goto error;
275 dup->n_sample = tab->n_sample;
276 dup->n_outside = tab->n_outside;
277 }
278 dup->n_row = tab->n_row;
279 dup->n_con = tab->n_con;
280 dup->n_eq = tab->n_eq;
281 dup->max_con = tab->max_con;
282 dup->n_col = tab->n_col;
283 dup->n_var = tab->n_var;
284 dup->max_var = tab->max_var;
285 dup->n_param = tab->n_param;
286 dup->n_div = tab->n_div;
287 dup->n_dead = tab->n_dead;
288 dup->n_redundant = tab->n_redundant;
289 dup->rational = tab->rational;
290 dup->empty = tab->empty;
291 dup->strict_redundant = 0;
292 dup->need_undo = 0;
293 dup->in_undo = 0;
294 dup->M = tab->M;
295 tab->cone = tab->cone;
296 dup->bottom.type = isl_tab_undo_bottom;
297 dup->bottom.next = NULL((void*)0);
298 dup->top = &dup->bottom;
299
300 dup->n_zero = tab->n_zero;
301 dup->n_unbounded = tab->n_unbounded;
302 dup->basis = isl_mat_dup(tab->basis);
303
304 return dup;
305error:
306 isl_tab_free(dup);
307 return NULL((void*)0);
308}
309
310/* Construct the coefficient matrix of the product tableau
311 * of two tableaus.
312 * mat{1,2} is the coefficient matrix of tableau {1,2}
313 * row{1,2} is the number of rows in tableau {1,2}
314 * col{1,2} is the number of columns in tableau {1,2}
315 * off is the offset to the coefficient column (skipping the
316 * denominator, the constant term and the big parameter if any)
317 * r{1,2} is the number of redundant rows in tableau {1,2}
318 * d{1,2} is the number of dead columns in tableau {1,2}
319 *
320 * The order of the rows and columns in the result is as explained
321 * in isl_tab_product.
322 */
323static struct isl_mat *tab_mat_product(struct isl_mat *mat1,
324 struct isl_mat *mat2, unsigned row1, unsigned row2,
325 unsigned col1, unsigned col2,
326 unsigned off, unsigned r1, unsigned r2, unsigned d1, unsigned d2)
327{
328 int i;
329 struct isl_mat *prod;
330 unsigned n;
331
332 prod = isl_mat_alloc(mat1->ctx, mat1->n_row + mat2->n_row,
333 off + col1 + col2);
334 if (!prod)
335 return NULL((void*)0);
336
337 n = 0;
338 for (i = 0; i < r1; ++i) {
339 isl_seq_cpy(prod->row[n + i], mat1->row[i], off + d1);
340 isl_seq_clr(prod->row[n + i] + off + d1, d2);
341 isl_seq_cpy(prod->row[n + i] + off + d1 + d2,
342 mat1->row[i] + off + d1, col1 - d1);
343 isl_seq_clr(prod->row[n + i] + off + col1 + d1, col2 - d2);
344 }
345
346 n += r1;
347 for (i = 0; i < r2; ++i) {
348 isl_seq_cpy(prod->row[n + i], mat2->row[i], off);
349 isl_seq_clr(prod->row[n + i] + off, d1);
350 isl_seq_cpy(prod->row[n + i] + off + d1,
351 mat2->row[i] + off, d2);
352 isl_seq_clr(prod->row[n + i] + off + d1 + d2, col1 - d1);
353 isl_seq_cpy(prod->row[n + i] + off + col1 + d1,
354 mat2->row[i] + off + d2, col2 - d2);
355 }
356
357 n += r2;
358 for (i = 0; i < row1 - r1; ++i) {
359 isl_seq_cpy(prod->row[n + i], mat1->row[r1 + i], off + d1);
360 isl_seq_clr(prod->row[n + i] + off + d1, d2);
361 isl_seq_cpy(prod->row[n + i] + off + d1 + d2,
362 mat1->row[r1 + i] + off + d1, col1 - d1);
363 isl_seq_clr(prod->row[n + i] + off + col1 + d1, col2 - d2);
364 }
365
366 n += row1 - r1;
367 for (i = 0; i < row2 - r2; ++i) {
368 isl_seq_cpy(prod->row[n + i], mat2->row[r2 + i], off);
369 isl_seq_clr(prod->row[n + i] + off, d1);
370 isl_seq_cpy(prod->row[n + i] + off + d1,
371 mat2->row[r2 + i] + off, d2);
372 isl_seq_clr(prod->row[n + i] + off + d1 + d2, col1 - d1);
373 isl_seq_cpy(prod->row[n + i] + off + col1 + d1,
374 mat2->row[r2 + i] + off + d2, col2 - d2);
375 }
376
377 return prod;
378}
379
380/* Update the row or column index of a variable that corresponds
381 * to a variable in the first input tableau.
382 */
383static void update_index1(struct isl_tab_var *var,
384 unsigned r1, unsigned r2, unsigned d1, unsigned d2)
385{
386 if (var->index == -1)
387 return;
388 if (var->is_row && var->index >= r1)
389 var->index += r2;
390 if (!var->is_row && var->index >= d1)
391 var->index += d2;
392}
393
394/* Update the row or column index of a variable that corresponds
395 * to a variable in the second input tableau.
396 */
397static void update_index2(struct isl_tab_var *var,
398 unsigned row1, unsigned col1,
399 unsigned r1, unsigned r2, unsigned d1, unsigned d2)
400{
401 if (var->index == -1)
402 return;
403 if (var->is_row) {
404 if (var->index < r2)
405 var->index += r1;
406 else
407 var->index += row1;
408 } else {
409 if (var->index < d2)
410 var->index += d1;
411 else
412 var->index += col1;
413 }
414}
415
416/* Create a tableau that represents the Cartesian product of the sets
417 * represented by tableaus tab1 and tab2.
418 * The order of the rows in the product is
419 * - redundant rows of tab1
420 * - redundant rows of tab2
421 * - non-redundant rows of tab1
422 * - non-redundant rows of tab2
423 * The order of the columns is
424 * - denominator
425 * - constant term
426 * - coefficient of big parameter, if any
427 * - dead columns of tab1
428 * - dead columns of tab2
429 * - live columns of tab1
430 * - live columns of tab2
431 * The order of the variables and the constraints is a concatenation
432 * of order in the two input tableaus.
433 */
434struct isl_tab *isl_tab_product(struct isl_tab *tab1, struct isl_tab *tab2)
435{
436 int i;
437 struct isl_tab *prod;
438 unsigned off;
439 unsigned r1, r2, d1, d2;
440
441 if (!tab1 || !tab2)
442 return NULL((void*)0);
443
444 isl_assert(tab1->mat->ctx, tab1->M == tab2->M, return NULL)do { if (tab1->M == tab2->M) break; do { isl_handle_error
(tab1->mat->ctx, isl_error_unknown, "Assertion \"" "tab1->M == tab2->M"
"\" failed", "/build/llvm-toolchain-snapshot-7~svn338205/tools/polly/lib/External/isl/isl_tab.c"
, 444); return ((void*)0); } while (0); } while (0)
;
445 isl_assert(tab1->mat->ctx, tab1->rational == tab2->rational, return NULL)do { if (tab1->rational == tab2->rational) break; do { isl_handle_error
(tab1->mat->ctx, isl_error_unknown, "Assertion \"" "tab1->rational == tab2->rational"
"\" failed", "/build/llvm-toolchain-snapshot-7~svn338205/tools/polly/lib/External/isl/isl_tab.c"
, 445); return ((void*)0); } while (0); } while (0)
;
446 isl_assert(tab1->mat->ctx, tab1->cone == tab2->cone, return NULL)do { if (tab1->cone == tab2->cone) break; do { isl_handle_error
(tab1->mat->ctx, isl_error_unknown, "Assertion \"" "tab1->cone == tab2->cone"
"\" failed", "/build/llvm-toolchain-snapshot-7~svn338205/tools/polly/lib/External/isl/isl_tab.c"
, 446); return ((void*)0); } while (0); } while (0)
;
447 isl_assert(tab1->mat->ctx, !tab1->row_sign, return NULL)do { if (!tab1->row_sign) break; do { isl_handle_error(tab1
->mat->ctx, isl_error_unknown, "Assertion \"" "!tab1->row_sign"
"\" failed", "/build/llvm-toolchain-snapshot-7~svn338205/tools/polly/lib/External/isl/isl_tab.c"
, 447); return ((void*)0); } while (0); } while (0)
;
448 isl_assert(tab1->mat->ctx, !tab2->row_sign, return NULL)do { if (!tab2->row_sign) break; do { isl_handle_error(tab1
->mat->ctx, isl_error_unknown, "Assertion \"" "!tab2->row_sign"
"\" failed", "/build/llvm-toolchain-snapshot-7~svn338205/tools/polly/lib/External/isl/isl_tab.c"
, 448); return ((void*)0); } while (0); } while (0)
;
449 isl_assert(tab1->mat->ctx, tab1->n_param == 0, return NULL)do { if (tab1->n_param == 0) break; do { isl_handle_error(
tab1->mat->ctx, isl_error_unknown, "Assertion \"" "tab1->n_param == 0"
"\" failed", "/build/llvm-toolchain-snapshot-7~svn338205/tools/polly/lib/External/isl/isl_tab.c"
, 449); return ((void*)0); } while (0); } while (0)
;
450 isl_assert(tab1->mat->ctx, tab2->n_param == 0, return NULL)do { if (tab2->n_param == 0) break; do { isl_handle_error(
tab1->mat->ctx, isl_error_unknown, "Assertion \"" "tab2->n_param == 0"
"\" failed", "/build/llvm-toolchain-snapshot-7~svn338205/tools/polly/lib/External/isl/isl_tab.c"
, 450); return ((void*)0); } while (0); } while (0)
;
451 isl_assert(tab1->mat->ctx, tab1->n_div == 0, return NULL)do { if (tab1->n_div == 0) break; do { isl_handle_error(tab1
->mat->ctx, isl_error_unknown, "Assertion \"" "tab1->n_div == 0"
"\" failed", "/build/llvm-toolchain-snapshot-7~svn338205/tools/polly/lib/External/isl/isl_tab.c"
, 451); return ((void*)0); } while (0); } while (0)
;
452 isl_assert(tab1->mat->ctx, tab2->n_div == 0, return NULL)do { if (tab2->n_div == 0) break; do { isl_handle_error(tab1
->mat->ctx, isl_error_unknown, "Assertion \"" "tab2->n_div == 0"
"\" failed", "/build/llvm-toolchain-snapshot-7~svn338205/tools/polly/lib/External/isl/isl_tab.c"
, 452); return ((void*)0); } while (0); } while (0)
;
453
454 off = 2 + tab1->M;
455 r1 = tab1->n_redundant;
456 r2 = tab2->n_redundant;
457 d1 = tab1->n_dead;
458 d2 = tab2->n_dead;
459 prod = isl_calloc_type(tab1->mat->ctx, struct isl_tab)((struct isl_tab *)isl_calloc_or_die(tab1->mat->ctx, 1,
sizeof(struct isl_tab)))
;
460 if (!prod)
461 return NULL((void*)0);
462 prod->mat = tab_mat_product(tab1->mat, tab2->mat,
463 tab1->n_row, tab2->n_row,
464 tab1->n_col, tab2->n_col, off, r1, r2, d1, d2);
465 if (!prod->mat)
466 goto error;
467 prod->var = isl_alloc_array(tab1->mat->ctx, struct isl_tab_var,((struct isl_tab_var *)isl_malloc_or_die(tab1->mat->ctx
, (tab1->max_var + tab2->max_var)*sizeof(struct isl_tab_var
)))
468 tab1->max_var + tab2->max_var)((struct isl_tab_var *)isl_malloc_or_die(tab1->mat->ctx
, (tab1->max_var + tab2->max_var)*sizeof(struct isl_tab_var
)))
;
469 if ((tab1->max_var + tab2->max_var) && !prod->var)
470 goto error;
471 for (i = 0; i < tab1->n_var; ++i) {
472 prod->var[i] = tab1->var[i];
473 update_index1(&prod->var[i], r1, r2, d1, d2);
474 }
475 for (i = 0; i < tab2->n_var; ++i) {
476 prod->var[tab1->n_var + i] = tab2->var[i];
477 update_index2(&prod->var[tab1->n_var + i],
478 tab1->n_row, tab1->n_col,
479 r1, r2, d1, d2);
480 }
481 prod->con = isl_alloc_array(tab1->mat->ctx, struct isl_tab_var,((struct isl_tab_var *)isl_malloc_or_die(tab1->mat->ctx
, (tab1->max_con + tab2->max_con)*sizeof(struct isl_tab_var
)))
482 tab1->max_con + tab2->max_con)((struct isl_tab_var *)isl_malloc_or_die(tab1->mat->ctx
, (tab1->max_con + tab2->max_con)*sizeof(struct isl_tab_var
)))
;
483 if ((tab1->max_con + tab2->max_con) && !prod->con)
484 goto error;
485 for (i = 0; i < tab1->n_con; ++i) {
486 prod->con[i] = tab1->con[i];
487 update_index1(&prod->con[i], r1, r2, d1, d2);
488 }
489 for (i = 0; i < tab2->n_con; ++i) {
490 prod->con[tab1->n_con + i] = tab2->con[i];
491 update_index2(&prod->con[tab1->n_con + i],
492 tab1->n_row, tab1->n_col,
493 r1, r2, d1, d2);
494 }
495 prod->col_var = isl_alloc_array(tab1->mat->ctx, int,((int *)isl_malloc_or_die(tab1->mat->ctx, (tab1->n_col
+ tab2->n_col)*sizeof(int)))
496 tab1->n_col + tab2->n_col)((int *)isl_malloc_or_die(tab1->mat->ctx, (tab1->n_col
+ tab2->n_col)*sizeof(int)))
;
497 if ((tab1->n_col + tab2->n_col) && !prod->col_var)
498 goto error;
499 for (i = 0; i < tab1->n_col; ++i) {
500 int pos = i < d1 ? i : i + d2;
501 prod->col_var[pos] = tab1->col_var[i];
502 }
503 for (i = 0; i < tab2->n_col; ++i) {
504 int pos = i < d2 ? d1 + i : tab1->n_col + i;
505 int t = tab2->col_var[i];
506 if (t >= 0)
507 t += tab1->n_var;
508 else
509 t -= tab1->n_con;
510 prod->col_var[pos] = t;
511 }
512 prod->row_var = isl_alloc_array(tab1->mat->ctx, int,((int *)isl_malloc_or_die(tab1->mat->ctx, (tab1->mat
->n_row + tab2->mat->n_row)*sizeof(int)))
513 tab1->mat->n_row + tab2->mat->n_row)((int *)isl_malloc_or_die(tab1->mat->ctx, (tab1->mat
->n_row + tab2->mat->n_row)*sizeof(int)))
;
514 if ((tab1->mat->n_row + tab2->mat->n_row) && !prod->row_var)
515 goto error;
516 for (i = 0; i < tab1->n_row; ++i) {
517 int pos = i < r1 ? i : i + r2;
518 prod->row_var[pos] = tab1->row_var[i];
519 }
520 for (i = 0; i < tab2->n_row; ++i) {
521 int pos = i < r2 ? r1 + i : tab1->n_row + i;
522 int t = tab2->row_var[i];
523 if (t >= 0)
524 t += tab1->n_var;
525 else
526 t -= tab1->n_con;
527 prod->row_var[pos] = t;
528 }
529 prod->samples = NULL((void*)0);
530 prod->sample_index = NULL((void*)0);
531 prod->n_row = tab1->n_row + tab2->n_row;
532 prod->n_con = tab1->n_con + tab2->n_con;
533 prod->n_eq = 0;
534 prod->max_con = tab1->max_con + tab2->max_con;
535 prod->n_col = tab1->n_col + tab2->n_col;
536 prod->n_var = tab1->n_var + tab2->n_var;
537 prod->max_var = tab1->max_var + tab2->max_var;
538 prod->n_param = 0;
539 prod->n_div = 0;
540 prod->n_dead = tab1->n_dead + tab2->n_dead;
541 prod->n_redundant = tab1->n_redundant + tab2->n_redundant;
542 prod->rational = tab1->rational;
543 prod->empty = tab1->empty || tab2->empty;
544 prod->strict_redundant = tab1->strict_redundant || tab2->strict_redundant;
545 prod->need_undo = 0;
546 prod->in_undo = 0;
547 prod->M = tab1->M;
548 prod->cone = tab1->cone;
549 prod->bottom.type = isl_tab_undo_bottom;
550 prod->bottom.next = NULL((void*)0);
551 prod->top = &prod->bottom;
552
553 prod->n_zero = 0;
554 prod->n_unbounded = 0;
555 prod->basis = NULL((void*)0);
556
557 return prod;
558error:
559 isl_tab_free(prod);
560 return NULL((void*)0);
561}
562
563static struct isl_tab_var *var_from_index(struct isl_tab *tab, int i)
564{
565 if (i >= 0)
566 return &tab->var[i];
567 else
568 return &tab->con[~i];
569}
570
571struct isl_tab_var *isl_tab_var_from_row(struct isl_tab *tab, int i)
572{
573 return var_from_index(tab, tab->row_var[i]);
574}
575
576static struct isl_tab_var *var_from_col(struct isl_tab *tab, int i)
577{
578 return var_from_index(tab, tab->col_var[i]);
579}
580
581/* Check if there are any upper bounds on column variable "var",
582 * i.e., non-negative rows where var appears with a negative coefficient.
583 * Return 1 if there are no such bounds.
584 */
585static int max_is_manifestly_unbounded(struct isl_tab *tab,
586 struct isl_tab_var *var)
587{
588 int i;
589 unsigned off = 2 + tab->M;
590
591 if (var->is_row)
592 return 0;
593 for (i = tab->n_redundant; i < tab->n_row; ++i) {
594 if (!isl_int_is_neg(tab->mat->row[i][off + var->index])(isl_sioimath_sgn(*(tab->mat->row[i][off + var->index
])) < 0)
)
595 continue;
596 if (isl_tab_var_from_row(tab, i)->is_nonneg)
597 return 0;
598 }
599 return 1;
600}
601
602/* Check if there are any lower bounds on column variable "var",
603 * i.e., non-negative rows where var appears with a positive coefficient.
604 * Return 1 if there are no such bounds.
605 */
606static int min_is_manifestly_unbounded(struct isl_tab *tab,
607 struct isl_tab_var *var)
608{
609 int i;
610 unsigned off = 2 + tab->M;
611
612 if (var->is_row)
613 return 0;
614 for (i = tab->n_redundant; i < tab->n_row; ++i) {
615 if (!isl_int_is_pos(tab->mat->row[i][off + var->index])(isl_sioimath_sgn(*(tab->mat->row[i][off + var->index
])) > 0)
)
616 continue;
617 if (isl_tab_var_from_row(tab, i)->is_nonneg)
618 return 0;
619 }
620 return 1;
621}
622
623static int row_cmp(struct isl_tab *tab, int r1, int r2, int c, isl_int *t)
624{
625 unsigned off = 2 + tab->M;
626
627 if (tab->M) {
628 int s;
629 isl_int_mul(*t, tab->mat->row[r1][2], tab->mat->row[r2][off+c])isl_sioimath_mul((*t), *(tab->mat->row[r1][2]), *(tab->
mat->row[r2][off+c]))
;
630 isl_int_submul(*t, tab->mat->row[r2][2], tab->mat->row[r1][off+c])isl_sioimath_submul((*t), *(tab->mat->row[r2][2]), *(tab
->mat->row[r1][off+c]))
;
631 s = isl_int_sgn(*t)isl_sioimath_sgn(*(*t));
632 if (s)
633 return s;
634 }
635 isl_int_mul(*t, tab->mat->row[r1][1], tab->mat->row[r2][off + c])isl_sioimath_mul((*t), *(tab->mat->row[r1][1]), *(tab->
mat->row[r2][off + c]))
;
636 isl_int_submul(*t, tab->mat->row[r2][1], tab->mat->row[r1][off + c])isl_sioimath_submul((*t), *(tab->mat->row[r2][1]), *(tab
->mat->row[r1][off + c]))
;
637 return isl_int_sgn(*t)isl_sioimath_sgn(*(*t));
638}
639
640/* Given the index of a column "c", return the index of a row
641 * that can be used to pivot the column in, with either an increase
642 * (sgn > 0) or a decrease (sgn < 0) of the corresponding variable.
643 * If "var" is not NULL, then the row returned will be different from
644 * the one associated with "var".
645 *
646 * Each row in the tableau is of the form
647 *
648 * x_r = a_r0 + \sum_i a_ri x_i
649 *
650 * Only rows with x_r >= 0 and with the sign of a_ri opposite to "sgn"
651 * impose any limit on the increase or decrease in the value of x_c
652 * and this bound is equal to a_r0 / |a_rc|. We are therefore looking
653 * for the row with the smallest (most stringent) such bound.
654 * Note that the common denominator of each row drops out of the fraction.
655 * To check if row j has a smaller bound than row r, i.e.,
656 * a_j0 / |a_jc| < a_r0 / |a_rc| or a_j0 |a_rc| < a_r0 |a_jc|,
657 * we check if -sign(a_jc) (a_j0 a_rc - a_r0 a_jc) < 0,
658 * where -sign(a_jc) is equal to "sgn".
659 */
660static int pivot_row(struct isl_tab *tab,
661 struct isl_tab_var *var, int sgn, int c)
662{
663 int j, r, tsgn;
664 isl_int t;
665 unsigned off = 2 + tab->M;
666
667 isl_int_init(t)isl_sioimath_init((t));
668 r = -1;
669 for (j = tab->n_redundant; j < tab->n_row; ++j) {
670 if (var && j == var->index)
671 continue;
672 if (!isl_tab_var_from_row(tab, j)->is_nonneg)
673 continue;
674 if (sgn * isl_int_sgn(tab->mat->row[j][off + c])isl_sioimath_sgn(*(tab->mat->row[j][off + c])) >= 0)
675 continue;
676 if (r < 0) {
677 r = j;
678 continue;
679 }
680 tsgn = sgn * row_cmp(tab, r, j, c, &t);
681 if (tsgn < 0 || (tsgn == 0 &&
682 tab->row_var[j] < tab->row_var[r]))
683 r = j;
684 }
685 isl_int_clear(t)isl_sioimath_clear((t));
686 return r;
687}
688
689/* Find a pivot (row and col) that will increase (sgn > 0) or decrease
690 * (sgn < 0) the value of row variable var.
691 * If not NULL, then skip_var is a row variable that should be ignored
692 * while looking for a pivot row. It is usually equal to var.
693 *
694 * As the given row in the tableau is of the form
695 *
696 * x_r = a_r0 + \sum_i a_ri x_i
697 *
698 * we need to find a column such that the sign of a_ri is equal to "sgn"
699 * (such that an increase in x_i will have the desired effect) or a
700 * column with a variable that may attain negative values.
701 * If a_ri is positive, then we need to move x_i in the same direction
702 * to obtain the desired effect. Otherwise, x_i has to move in the
703 * opposite direction.
704 */
705static void find_pivot(struct isl_tab *tab,
706 struct isl_tab_var *var, struct isl_tab_var *skip_var,
707 int sgn, int *row, int *col)
708{
709 int j, r, c;
710 isl_int *tr;
711
712 *row = *col = -1;
713
714 isl_assert(tab->mat->ctx, var->is_row, return)do { if (var->is_row) break; do { isl_handle_error(tab->
mat->ctx, isl_error_unknown, "Assertion \"" "var->is_row"
"\" failed", "/build/llvm-toolchain-snapshot-7~svn338205/tools/polly/lib/External/isl/isl_tab.c"
, 714); return; } while (0); } while (0)
;
715 tr = tab->mat->row[var->index] + 2 + tab->M;
716
717 c = -1;
718 for (j = tab->n_dead; j < tab->n_col; ++j) {
719 if (isl_int_is_zero(tr[j])(isl_sioimath_sgn(*(tr[j])) == 0))
720 continue;
721 if (isl_int_sgn(tr[j])isl_sioimath_sgn(*(tr[j])) != sgn &&
722 var_from_col(tab, j)->is_nonneg)
723 continue;
724 if (c < 0 || tab->col_var[j] < tab->col_var[c])
725 c = j;
726 }
727 if (c < 0)
728 return;
729
730 sgn *= isl_int_sgn(tr[c])isl_sioimath_sgn(*(tr[c]));
731 r = pivot_row(tab, skip_var, sgn, c);
732 *row = r < 0 ? var->index : r;
733 *col = c;
734}
735
736/* Return 1 if row "row" represents an obviously redundant inequality.
737 * This means
738 * - it represents an inequality or a variable
739 * - that is the sum of a non-negative sample value and a positive
740 * combination of zero or more non-negative constraints.
741 */
742int isl_tab_row_is_redundant(struct isl_tab *tab, int row)
743{
744 int i;
745 unsigned off = 2 + tab->M;
746
747 if (tab->row_var[row] < 0 && !isl_tab_var_from_row(tab, row)->is_nonneg)
748 return 0;
749
750 if (isl_int_is_neg(tab->mat->row[row][1])(isl_sioimath_sgn(*(tab->mat->row[row][1])) < 0))
751 return 0;
752 if (tab->strict_redundant && isl_int_is_zero(tab->mat->row[row][1])(isl_sioimath_sgn(*(tab->mat->row[row][1])) == 0))
753 return 0;
754 if (tab->M && isl_int_is_neg(tab->mat->row[row][2])(isl_sioimath_sgn(*(tab->mat->row[row][2])) < 0))
755 return 0;
756
757 for (i = tab->n_dead; i < tab->n_col; ++i) {
758 if (isl_int_is_zero(tab->mat->row[row][off + i])(isl_sioimath_sgn(*(tab->mat->row[row][off + i])) == 0))
759 continue;
760 if (tab->col_var[i] >= 0)
761 return 0;
762 if (isl_int_is_neg(tab->mat->row[row][off + i])(isl_sioimath_sgn(*(tab->mat->row[row][off + i])) < 0
)
)
763 return 0;
764 if (!var_from_col(tab, i)->is_nonneg)
765 return 0;
766 }
767 return 1;
768}
769
770static void swap_rows(struct isl_tab *tab, int row1, int row2)
771{
772 int t;
773 enum isl_tab_row_sign s;
774
775 t = tab->row_var[row1];
776 tab->row_var[row1] = tab->row_var[row2];
777 tab->row_var[row2] = t;
778 isl_tab_var_from_row(tab, row1)->index = row1;
779 isl_tab_var_from_row(tab, row2)->index = row2;
780 tab->mat = isl_mat_swap_rows(tab->mat, row1, row2);
781
782 if (!tab->row_sign)
783 return;
784 s = tab->row_sign[row1];
785 tab->row_sign[row1] = tab->row_sign[row2];
786 tab->row_sign[row2] = s;
787}
788
789static isl_stat push_union(struct isl_tab *tab,
790 enum isl_tab_undo_type type, union isl_tab_undo_val u) WARN_UNUSED__attribute__((__warn_unused_result__));
791
792/* Push record "u" onto the undo stack of "tab", provided "tab"
793 * keeps track of undo information.
794 *
795 * If the record cannot be pushed, then mark the undo stack as invalid
796 * such that a later rollback attempt will not try to undo earlier
797 * records without having been able to undo the current record.
798 */
799static isl_stat push_union(struct isl_tab *tab,
800 enum isl_tab_undo_type type, union isl_tab_undo_val u)
801{
802 struct isl_tab_undo *undo;
803
804 if (!tab)
805 return isl_stat_error;
806 if (!tab->need_undo)
807 return isl_stat_ok;
808
809 undo = isl_alloc_type(tab->mat->ctx, struct isl_tab_undo)((struct isl_tab_undo *)isl_malloc_or_die(tab->mat->ctx
, sizeof(struct isl_tab_undo)))
;
810 if (!undo)
811 goto error;
812 undo->type = type;
813 undo->u = u;
814 undo->next = tab->top;
815 tab->top = undo;
816
817 return isl_stat_ok;
818error:
819 free_undo(tab);
820 tab->top = NULL((void*)0);
821 return isl_stat_error;
822}
823
824isl_stat isl_tab_push_var(struct isl_tab *tab,
825 enum isl_tab_undo_type type, struct isl_tab_var *var)
826{
827 union isl_tab_undo_val u;
828 if (var->is_row)
829 u.var_index = tab->row_var[var->index];
830 else
831 u.var_index = tab->col_var[var->index];
832 return push_union(tab, type, u);
833}
834
835isl_stat isl_tab_push(struct isl_tab *tab, enum isl_tab_undo_type type)
836{
837 union isl_tab_undo_val u = { 0 };
838 return push_union(tab, type, u);
839}
840
841/* Push a record on the undo stack describing the current basic
842 * variables, so that the this state can be restored during rollback.
843 */
844isl_stat isl_tab_push_basis(struct isl_tab *tab)
845{
846 int i;
847 union isl_tab_undo_val u;
848
849 u.col_var = isl_alloc_array(tab->mat->ctx, int, tab->n_col)((int *)isl_malloc_or_die(tab->mat->ctx, (tab->n_col
)*sizeof(int)))
;
850 if (tab->n_col && !u.col_var)
851 return isl_stat_error;
852 for (i = 0; i < tab->n_col; ++i)
853 u.col_var[i] = tab->col_var[i];
854 return push_union(tab, isl_tab_undo_saved_basis, u);
855}
856
857isl_stat isl_tab_push_callback(struct isl_tab *tab,
858 struct isl_tab_callback *callback)
859{
860 union isl_tab_undo_val u;
861 u.callback = callback;
862 return push_union(tab, isl_tab_undo_callback, u);
863}
864
865struct isl_tab *isl_tab_init_samples(struct isl_tab *tab)
866{
867 if (!tab)
868 return NULL((void*)0);
869
870 tab->n_sample = 0;
871 tab->n_outside = 0;
872 tab->samples = isl_mat_alloc(tab->mat->ctx, 1, 1 + tab->n_var);
873 if (!tab->samples)
874 goto error;
875 tab->sample_index = isl_alloc_array(tab->mat->ctx, int, 1)((int *)isl_malloc_or_die(tab->mat->ctx, (1)*sizeof(int
)))
;
876 if (!tab->sample_index)
877 goto error;
878 return tab;
879error:
880 isl_tab_free(tab);
881 return NULL((void*)0);
882}
883
884int isl_tab_add_sample(struct isl_tab *tab, __isl_take isl_vec *sample)
885{
886 if (!tab || !sample)
887 goto error;
888
889 if (tab->n_sample + 1 > tab->samples->n_row) {
890 int *t = isl_realloc_array(tab->mat->ctx,((int *)isl_realloc_or_die(tab->mat->ctx, tab->sample_index
, (tab->n_sample + 1)*sizeof(int)))
891 tab->sample_index, int, tab->n_sample + 1)((int *)isl_realloc_or_die(tab->mat->ctx, tab->sample_index
, (tab->n_sample + 1)*sizeof(int)))
;
892 if (!t)
893 goto error;
894 tab->sample_index = t;
895 }
896
897 tab->samples = isl_mat_extend(tab->samples,
898 tab->n_sample + 1, tab->samples->n_col);
899 if (!tab->samples)
900 goto error;
901
902 isl_seq_cpy(tab->samples->row[tab->n_sample], sample->el, sample->size);
903 isl_vec_free(sample);
904 tab->sample_index[tab->n_sample] = tab->n_sample;
905 tab->n_sample++;
906
907 return 0;
908error:
909 isl_vec_free(sample);
910 return -1;
911}
912
913struct isl_tab *isl_tab_drop_sample(struct isl_tab *tab, int s)
914{
915 if (s != tab->n_outside) {
916 int t = tab->sample_index[tab->n_outside];
917 tab->sample_index[tab->n_outside] = tab->sample_index[s];
918 tab->sample_index[s] = t;
919 isl_mat_swap_rows(tab->samples, tab->n_outside, s);
920 }
921 tab->n_outside++;
922 if (isl_tab_push(tab, isl_tab_undo_drop_sample) < 0) {
923 isl_tab_free(tab);
924 return NULL((void*)0);
925 }
926
927 return tab;
928}
929
930/* Record the current number of samples so that we can remove newer
931 * samples during a rollback.
932 */
933isl_stat isl_tab_save_samples(struct isl_tab *tab)
934{
935 union isl_tab_undo_val u;
936
937 if (!tab)
938 return isl_stat_error;
939
940 u.n = tab->n_sample;
941 return push_union(tab, isl_tab_undo_saved_samples, u);
942}
943
944/* Mark row with index "row" as being redundant.
945 * If we may need to undo the operation or if the row represents
946 * a variable of the original problem, the row is kept,
947 * but no longer considered when looking for a pivot row.
948 * Otherwise, the row is simply removed.
949 *
950 * The row may be interchanged with some other row. If it
951 * is interchanged with a later row, return 1. Otherwise return 0.
952 * If the rows are checked in order in the calling function,
953 * then a return value of 1 means that the row with the given
954 * row number may now contain a different row that hasn't been checked yet.
955 */
956int isl_tab_mark_redundant(struct isl_tab *tab, int row)
957{
958 struct isl_tab_var *var = isl_tab_var_from_row(tab, row);
959 var->is_redundant = 1;
960 isl_assert(tab->mat->ctx, row >= tab->n_redundant, return -1)do { if (row >= tab->n_redundant) break; do { isl_handle_error
(tab->mat->ctx, isl_error_unknown, "Assertion \"" "row >= tab->n_redundant"
"\" failed", "/build/llvm-toolchain-snapshot-7~svn338205/tools/polly/lib/External/isl/isl_tab.c"
, 960); return -1; } while (0); } while (0)
;
961 if (tab->preserve || tab->need_undo || tab->row_var[row] >= 0) {
962 if (tab->row_var[row] >= 0 && !var->is_nonneg) {
963 var->is_nonneg = 1;
964 if (isl_tab_push_var(tab, isl_tab_undo_nonneg, var) < 0)
965 return -1;
966 }
967 if (row != tab->n_redundant)
968 swap_rows(tab, row, tab->n_redundant);
969 tab->n_redundant++;
970 return isl_tab_push_var(tab, isl_tab_undo_redundant, var);
971 } else {
972 if (row != tab->n_row - 1)
973 swap_rows(tab, row, tab->n_row - 1);
974 isl_tab_var_from_row(tab, tab->n_row - 1)->index = -1;
975 tab->n_row--;
976 return 1;
977 }
978}
979
980/* Mark "tab" as a rational tableau.
981 * If it wasn't marked as a rational tableau already and if we may
982 * need to undo changes, then arrange for the marking to be undone
983 * during the undo.
984 */
985int isl_tab_mark_rational(struct isl_tab *tab)
986{
987 if (!tab)
988 return -1;
989 if (!tab->rational && tab->need_undo)
990 if (isl_tab_push(tab, isl_tab_undo_rational) < 0)
991 return -1;
992 tab->rational = 1;
993 return 0;
994}
995
996isl_stat isl_tab_mark_empty(struct isl_tab *tab)
997{
998 if (!tab)
999 return isl_stat_error;
1000 if (!tab->empty && tab->need_undo)
1001 if (isl_tab_push(tab, isl_tab_undo_empty) < 0)
1002 return isl_stat_error;
1003 tab->empty = 1;
1004 return isl_stat_ok;
1005}
1006
1007int isl_tab_freeze_constraint(struct isl_tab *tab, int con)
1008{
1009 struct isl_tab_var *var;
1010
1011 if (!tab)
1012 return -1;
1013
1014 var = &tab->con[con];
1015 if (var->frozen)
1016 return 0;
1017 if (var->index < 0)
1018 return 0;
1019 var->frozen = 1;
1020
1021 if (tab->need_undo)
1022 return isl_tab_push_var(tab, isl_tab_undo_freeze, var);
1023
1024 return 0;
1025}
1026
1027/* Update the rows signs after a pivot of "row" and "col", with "row_sgn"
1028 * the original sign of the pivot element.
1029 * We only keep track of row signs during PILP solving and in this case
1030 * we only pivot a row with negative sign (meaning the value is always
1031 * non-positive) using a positive pivot element.
1032 *
1033 * For each row j, the new value of the parametric constant is equal to
1034 *
1035 * a_j0 - a_jc a_r0/a_rc
1036 *
1037 * where a_j0 is the original parametric constant, a_rc is the pivot element,
1038 * a_r0 is the parametric constant of the pivot row and a_jc is the
1039 * pivot column entry of the row j.
1040 * Since a_r0 is non-positive and a_rc is positive, the sign of row j
1041 * remains the same if a_jc has the same sign as the row j or if
1042 * a_jc is zero. In all other cases, we reset the sign to "unknown".
1043 */
1044static void update_row_sign(struct isl_tab *tab, int row, int col, int row_sgn)
1045{
1046 int i;
1047 struct isl_mat *mat = tab->mat;
1048 unsigned off = 2 + tab->M;
1049
1050 if (!tab->row_sign)
1051 return;
1052
1053 if (tab->row_sign[row] == 0)
1054 return;
1055 isl_assert(mat->ctx, row_sgn > 0, return)do { if (row_sgn > 0) break; do { isl_handle_error(mat->
ctx, isl_error_unknown, "Assertion \"" "row_sgn > 0" "\" failed"
, "/build/llvm-toolchain-snapshot-7~svn338205/tools/polly/lib/External/isl/isl_tab.c"
, 1055); return; } while (0); } while (0)
;
1056 isl_assert(mat->ctx, tab->row_sign[row] == isl_tab_row_neg, return)do { if (tab->row_sign[row] == isl_tab_row_neg) break; do {
isl_handle_error(mat->ctx, isl_error_unknown, "Assertion \""
"tab->row_sign[row] == isl_tab_row_neg" "\" failed", "/build/llvm-toolchain-snapshot-7~svn338205/tools/polly/lib/External/isl/isl_tab.c"
, 1056); return; } while (0); } while (0)
;
1057 tab->row_sign[row] = isl_tab_row_pos;
1058 for (i = 0; i < tab->n_row; ++i) {
1059 int s;
1060 if (i == row)
1061 continue;
1062 s = isl_int_sgn(mat->row[i][off + col])isl_sioimath_sgn(*(mat->row[i][off + col]));
1063 if (!s)
1064 continue;
1065 if (!tab->row_sign[i])
1066 continue;
1067 if (s < 0 && tab->row_sign[i] == isl_tab_row_neg)
1068 continue;
1069 if (s > 0 && tab->row_sign[i] == isl_tab_row_pos)
1070 continue;
1071 tab->row_sign[i] = isl_tab_row_unknown;
1072 }
1073}
1074
1075/* Given a row number "row" and a column number "col", pivot the tableau
1076 * such that the associated variables are interchanged.
1077 * The given row in the tableau expresses
1078 *
1079 * x_r = a_r0 + \sum_i a_ri x_i
1080 *
1081 * or
1082 *
1083 * x_c = 1/a_rc x_r - a_r0/a_rc + sum_{i \ne r} -a_ri/a_rc
1084 *
1085 * Substituting this equality into the other rows
1086 *
1087 * x_j = a_j0 + \sum_i a_ji x_i
1088 *
1089 * with a_jc \ne 0, we obtain
1090 *
1091 * x_j = a_jc/a_rc x_r + a_j0 - a_jc a_r0/a_rc + sum a_ji - a_jc a_ri/a_rc
1092 *
1093 * The tableau
1094 *
1095 * n_rc/d_r n_ri/d_r
1096 * n_jc/d_j n_ji/d_j
1097 *
1098 * where i is any other column and j is any other row,
1099 * is therefore transformed into
1100 *
1101 * s(n_rc)d_r/|n_rc| -s(n_rc)n_ri/|n_rc|
1102 * s(n_rc)d_r n_jc/(|n_rc| d_j) (n_ji |n_rc| - s(n_rc)n_jc n_ri)/(|n_rc| d_j)
1103 *
1104 * The transformation is performed along the following steps
1105 *
1106 * d_r/n_rc n_ri/n_rc
1107 * n_jc/d_j n_ji/d_j
1108 *
1109 * s(n_rc)d_r/|n_rc| -s(n_rc)n_ri/|n_rc|
1110 * n_jc/d_j n_ji/d_j
1111 *
1112 * s(n_rc)d_r/|n_rc| -s(n_rc)n_ri/|n_rc|
1113 * n_jc/(|n_rc| d_j) n_ji/(|n_rc| d_j)
1114 *
1115 * s(n_rc)d_r/|n_rc| -s(n_rc)n_ri/|n_rc|
1116 * n_jc/(|n_rc| d_j) (n_ji |n_rc|)/(|n_rc| d_j)
1117 *
1118 * s(n_rc)d_r/|n_rc| -s(n_rc)n_ri/|n_rc|
1119 * n_jc/(|n_rc| d_j) (n_ji |n_rc| - s(n_rc)n_jc n_ri)/(|n_rc| d_j)
1120 *
1121 * s(n_rc)d_r/|n_rc| -s(n_rc)n_ri/|n_rc|
1122 * s(n_rc)d_r n_jc/(|n_rc| d_j) (n_ji |n_rc| - s(n_rc)n_jc n_ri)/(|n_rc| d_j)
1123 *
1124 */
1125int isl_tab_pivot(struct isl_tab *tab, int row, int col)
1126{
1127 int i, j;
1128 int sgn;
1129 int t;
1130 isl_ctx *ctx;
1131 struct isl_mat *mat = tab->mat;
1132 struct isl_tab_var *var;
1133 unsigned off = 2 + tab->M;
1134
1135 ctx = isl_tab_get_ctx(tab);
1136 if (isl_ctx_next_operation(ctx) < 0)
1137 return -1;
1138
1139 isl_int_swap(mat->row[row][0], mat->row[row][off + col])isl_sioimath_swap((mat->row[row][0]), (mat->row[row][off
+ col]))
;
1140 sgn = isl_int_sgn(mat->row[row][0])isl_sioimath_sgn(*(mat->row[row][0]));
1141 if (sgn < 0) {
1142 isl_int_neg(mat->row[row][0], mat->row[row][0])isl_sioimath_neg((mat->row[row][0]), *(mat->row[row][0]
))
;
1143 isl_int_neg(mat->row[row][off + col], mat->row[row][off + col])isl_sioimath_neg((mat->row[row][off + col]), *(mat->row
[row][off + col]))
;
1144 } else
1145 for (j = 0; j < off - 1 + tab->n_col; ++j) {
1146 if (j == off - 1 + col)
1147 continue;
1148 isl_int_neg(mat->row[row][1 + j], mat->row[row][1 + j])isl_sioimath_neg((mat->row[row][1 + j]), *(mat->row[row
][1 + j]))
;
1149 }
1150 if (!isl_int_is_one(mat->row[row][0])(isl_sioimath_cmp_si(*(mat->row[row][0]), 1) == 0))
1151 isl_seq_normalize(mat->ctx, mat->row[row], off + tab->n_col);
1152 for (i = 0; i < tab->n_row; ++i) {
1153 if (i == row)
1154 continue;
1155 if (isl_int_is_zero(mat->row[i][off + col])(isl_sioimath_sgn(*(mat->row[i][off + col])) == 0))
1156 continue;
1157 isl_int_mul(mat->row[i][0], mat->row[i][0], mat->row[row][0])isl_sioimath_mul((mat->row[i][0]), *(mat->row[i][0]), *
(mat->row[row][0]))
;
1158 for (j = 0; j < off - 1 + tab->n_col; ++j) {
1159 if (j == off - 1 + col)
1160 continue;
1161 isl_int_mul(mat->row[i][1 + j],isl_sioimath_mul((mat->row[i][1 + j]), *(mat->row[i][1 +
j]), *(mat->row[row][0]))
1162 mat->row[i][1 + j], mat->row[row][0])isl_sioimath_mul((mat->row[i][1 + j]), *(mat->row[i][1 +
j]), *(mat->row[row][0]))
;
1163 isl_int_addmul(mat->row[i][1 + j],isl_sioimath_addmul((mat->row[i][1 + j]), *(mat->row[i]
[off + col]), *(mat->row[row][1 + j]))
1164 mat->row[i][off + col], mat->row[row][1 + j])isl_sioimath_addmul((mat->row[i][1 + j]), *(mat->row[i]
[off + col]), *(mat->row[row][1 + j]))
;
1165 }
1166 isl_int_mul(mat->row[i][off + col],isl_sioimath_mul((mat->row[i][off + col]), *(mat->row[i
][off + col]), *(mat->row[row][off + col]))
1167 mat->row[i][off + col], mat->row[row][off + col])isl_sioimath_mul((mat->row[i][off + col]), *(mat->row[i
][off + col]), *(mat->row[row][off + col]))
;
1168 if (!isl_int_is_one(mat->row[i][0])(isl_sioimath_cmp_si(*(mat->row[i][0]), 1) == 0))
1169 isl_seq_normalize(mat->ctx, mat->row[i], off + tab->n_col);
1170 }
1171 t = tab->row_var[row];
1172 tab->row_var[row] = tab->col_var[col];
1173 tab->col_var[col] = t;
1174 var = isl_tab_var_from_row(tab, row);
1175 var->is_row = 1;
1176 var->index = row;
1177 var = var_from_col(tab, col);
1178 var->is_row = 0;
1179 var->index = col;
1180 update_row_sign(tab, row, col, sgn);
1181 if (tab->in_undo)
1182 return 0;
1183 for (i = tab->n_redundant; i < tab->n_row; ++i) {
1184 if (isl_int_is_zero(mat->row[i][off + col])(isl_sioimath_sgn(*(mat->row[i][off + col])) == 0))
1185 continue;
1186 if (!isl_tab_var_from_row(tab, i)->frozen &&
1187 isl_tab_row_is_redundant(tab, i)) {
1188 int redo = isl_tab_mark_redundant(tab, i);
1189 if (redo < 0)
1190 return -1;
1191 if (redo)
1192 --i;
1193 }
1194 }
1195 return 0;
1196}
1197
1198/* If "var" represents a column variable, then pivot is up (sgn > 0)
1199 * or down (sgn < 0) to a row. The variable is assumed not to be
1200 * unbounded in the specified direction.
1201 * If sgn = 0, then the variable is unbounded in both directions,
1202 * and we pivot with any row we can find.
1203 */
1204static int to_row(struct isl_tab *tab, struct isl_tab_var *var, int sign) WARN_UNUSED__attribute__((__warn_unused_result__));
1205static int to_row(struct isl_tab *tab, struct isl_tab_var *var, int sign)
1206{
1207 int r;
1208 unsigned off = 2 + tab->M;
1209
1210 if (var->is_row)
1211 return 0;
1212
1213 if (sign == 0) {
1214 for (r = tab->n_redundant; r < tab->n_row; ++r)
1215 if (!isl_int_is_zero(tab->mat->row[r][off+var->index])(isl_sioimath_sgn(*(tab->mat->row[r][off+var->index]
)) == 0)
)
1216 break;
1217 isl_assert(tab->mat->ctx, r < tab->n_row, return -1)do { if (r < tab->n_row) break; do { isl_handle_error(tab
->mat->ctx, isl_error_unknown, "Assertion \"" "r < tab->n_row"
"\" failed", "/build/llvm-toolchain-snapshot-7~svn338205/tools/polly/lib/External/isl/isl_tab.c"
, 1217); return -1; } while (0); } while (0)
;
1218 } else {
1219 r = pivot_row(tab, NULL((void*)0), sign, var->index);
1220 isl_assert(tab->mat->ctx, r >= 0, return -1)do { if (r >= 0) break; do { isl_handle_error(tab->mat->
ctx, isl_error_unknown, "Assertion \"" "r >= 0" "\" failed"
, "/build/llvm-toolchain-snapshot-7~svn338205/tools/polly/lib/External/isl/isl_tab.c"
, 1220); return -1; } while (0); } while (0)
;
1221 }
1222
1223 return isl_tab_pivot(tab, r, var->index);
1224}
1225
1226/* Check whether all variables that are marked as non-negative
1227 * also have a non-negative sample value. This function is not
1228 * called from the current code but is useful during debugging.
1229 */
1230static void check_table(struct isl_tab *tab) __attribute__ ((unused));
1231static void check_table(struct isl_tab *tab)
1232{
1233 int i;
1234
1235 if (tab->empty)
1236 return;
1237 for (i = tab->n_redundant; i < tab->n_row; ++i) {
1238 struct isl_tab_var *var;
1239 var = isl_tab_var_from_row(tab, i);
1240 if (!var->is_nonneg)
1241 continue;
1242 if (tab->M) {
1243 isl_assert(tab->mat->ctx,do { if (!(isl_sioimath_sgn(*(tab->mat->row[i][2])) <
0)) break; do { isl_handle_error(tab->mat->ctx, isl_error_unknown
, "Assertion \"" "!(isl_sioimath_sgn(*(tab->mat->row[i][2])) < 0)"
"\" failed", "/build/llvm-toolchain-snapshot-7~svn338205/tools/polly/lib/External/isl/isl_tab.c"
, 1244); abort(); } while (0); } while (0)
1244 !isl_int_is_neg(tab->mat->row[i][2]), abort())do { if (!(isl_sioimath_sgn(*(tab->mat->row[i][2])) <
0)) break; do { isl_handle_error(tab->mat->ctx, isl_error_unknown
, "Assertion \"" "!(isl_sioimath_sgn(*(tab->mat->row[i][2])) < 0)"
"\" failed", "/build/llvm-toolchain-snapshot-7~svn338205/tools/polly/lib/External/isl/isl_tab.c"
, 1244); abort(); } while (0); } while (0)
;
1245 if (isl_int_is_pos(tab->mat->row[i][2])(isl_sioimath_sgn(*(tab->mat->row[i][2])) > 0))
1246 continue;
1247 }
1248 isl_assert(tab->mat->ctx, !isl_int_is_neg(tab->mat->row[i][1]),do { if (!(isl_sioimath_sgn(*(tab->mat->row[i][1])) <
0)) break; do { isl_handle_error(tab->mat->ctx, isl_error_unknown
, "Assertion \"" "!(isl_sioimath_sgn(*(tab->mat->row[i][1])) < 0)"
"\" failed", "/build/llvm-toolchain-snapshot-7~svn338205/tools/polly/lib/External/isl/isl_tab.c"
, 1249); abort(); } while (0); } while (0)
1249 abort())do { if (!(isl_sioimath_sgn(*(tab->mat->row[i][1])) <
0)) break; do { isl_handle_error(tab->mat->ctx, isl_error_unknown
, "Assertion \"" "!(isl_sioimath_sgn(*(tab->mat->row[i][1])) < 0)"
"\" failed", "/build/llvm-toolchain-snapshot-7~svn338205/tools/polly/lib/External/isl/isl_tab.c"
, 1249); abort(); } while (0); } while (0)
;
1250 }
1251}
1252
1253/* Return the sign of the maximal value of "var".
1254 * If the sign is not negative, then on return from this function,
1255 * the sample value will also be non-negative.
1256 *
1257 * If "var" is manifestly unbounded wrt positive values, we are done.
1258 * Otherwise, we pivot the variable up to a row if needed
1259 * Then we continue pivoting down until either
1260 * - no more down pivots can be performed
1261 * - the sample value is positive
1262 * - the variable is pivoted into a manifestly unbounded column
1263 */
1264static int sign_of_max(struct isl_tab *tab, struct isl_tab_var *var)
1265{
1266 int row, col;
1267
1268 if (max_is_manifestly_unbounded(tab, var))
1269 return 1;
1270 if (to_row(tab, var, 1) < 0)
1271 return -2;
1272 while (!isl_int_is_pos(tab->mat->row[var->index][1])(isl_sioimath_sgn(*(tab->mat->row[var->index][1])) >
0)
) {
1273 find_pivot(tab, var, var, 1, &row, &col);
1274 if (row == -1)
1275 return isl_int_sgn(tab->mat->row[var->index][1])isl_sioimath_sgn(*(tab->mat->row[var->index][1]));
1276 if (isl_tab_pivot(tab, row, col) < 0)
1277 return -2;
1278 if (!var->is_row) /* manifestly unbounded */
1279 return 1;
1280 }
1281 return 1;
1282}
1283
1284int isl_tab_sign_of_max(struct isl_tab *tab, int con)
1285{
1286 struct isl_tab_var *var;
1287
1288 if (!tab)
1289 return -2;
1290
1291 var = &tab->con[con];
1292 isl_assert(tab->mat->ctx, !var->is_redundant, return -2)do { if (!var->is_redundant) break; do { isl_handle_error(
tab->mat->ctx, isl_error_unknown, "Assertion \"" "!var->is_redundant"
"\" failed", "/build/llvm-toolchain-snapshot-7~svn338205/tools/polly/lib/External/isl/isl_tab.c"
, 1292); return -2; } while (0); } while (0)
;
1293 isl_assert(tab->mat->ctx, !var->is_zero, return -2)do { if (!var->is_zero) break; do { isl_handle_error(tab->
mat->ctx, isl_error_unknown, "Assertion \"" "!var->is_zero"
"\" failed", "/build/llvm-toolchain-snapshot-7~svn338205/tools/polly/lib/External/isl/isl_tab.c"
, 1293); return -2; } while (0); } while (0)
;
1294
1295 return sign_of_max(tab, var);
1296}
1297
1298static int row_is_neg(struct isl_tab *tab, int row)
1299{
1300 if (!tab->M)
1301 return isl_int_is_neg(tab->mat->row[row][1])(isl_sioimath_sgn(*(tab->mat->row[row][1])) < 0);
1302 if (isl_int_is_pos(tab->mat->row[row][2])(isl_sioimath_sgn(*(tab->mat->row[row][2])) > 0))
1303 return 0;
1304 if (isl_int_is_neg(tab->mat->row[row][2])(isl_sioimath_sgn(*(tab->mat->row[row][2])) < 0))
1305 return 1;
1306 return isl_int_is_neg(tab->mat->row[row][1])(isl_sioimath_sgn(*(tab->mat->row[row][1])) < 0);
1307}
1308
1309static int row_sgn(struct isl_tab *tab, int row)
1310{
1311 if (!tab->M)
1312 return isl_int_sgn(tab->mat->row[row][1])isl_sioimath_sgn(*(tab->mat->row[row][1]));
1313 if (!isl_int_is_zero(tab->mat->row[row][2])(isl_sioimath_sgn(*(tab->mat->row[row][2])) == 0))
1314 return isl_int_sgn(tab->mat->row[row][2])isl_sioimath_sgn(*(tab->mat->row[row][2]));
1315 else
1316 return isl_int_sgn(tab->mat->row[row][1])isl_sioimath_sgn(*(tab->mat->row[row][1]));
1317}
1318
1319/* Perform pivots until the row variable "var" has a non-negative
1320 * sample value or until no more upward pivots can be performed.
1321 * Return the sign of the sample value after the pivots have been
1322 * performed.
1323 */
1324static int restore_row(struct isl_tab *tab, struct isl_tab_var *var)
1325{
1326 int row, col;
1327
1328 while (row_is_neg(tab, var->index)) {
1329 find_pivot(tab, var, var, 1, &row, &col);
1330 if (row == -1)
1331 break;
1332 if (isl_tab_pivot(tab, row, col) < 0)
1333 return -2;
1334 if (!var->is_row) /* manifestly unbounded */
1335 return 1;
1336 }
1337 return row_sgn(tab, var->index);
1338}
1339
1340/* Perform pivots until we are sure that the row variable "var"
1341 * can attain non-negative values. After return from this
1342 * function, "var" is still a row variable, but its sample
1343 * value may not be non-negative, even if the function returns 1.
1344 */
1345static int at_least_zero(struct isl_tab *tab, struct isl_tab_var *var)
1346{
1347 int row, col;
1348
1349 while (isl_int_is_neg(tab->mat->row[var->index][1])(isl_sioimath_sgn(*(tab->mat->row[var->index][1])) <
0)
) {
1350 find_pivot(tab, var, var, 1, &row, &col);
1351 if (row == -1)
1352 break;
1353 if (row == var->index) /* manifestly unbounded */
1354 return 1;
1355 if (isl_tab_pivot(tab, row, col) < 0)
1356 return -1;
1357 }
1358 return !isl_int_is_neg(tab->mat->row[var->index][1])(isl_sioimath_sgn(*(tab->mat->row[var->index][1])) <
0)
;
1359}
1360
1361/* Return a negative value if "var" can attain negative values.
1362 * Return a non-negative value otherwise.
1363 *
1364 * If "var" is manifestly unbounded wrt negative values, we are done.
1365 * Otherwise, if var is in a column, we can pivot it down to a row.
1366 * Then we continue pivoting down until either
1367 * - the pivot would result in a manifestly unbounded column
1368 * => we don't perform the pivot, but simply return -1
1369 * - no more down pivots can be performed
1370 * - the sample value is negative
1371 * If the sample value becomes negative and the variable is supposed
1372 * to be nonnegative, then we undo the last pivot.
1373 * However, if the last pivot has made the pivoting variable
1374 * obviously redundant, then it may have moved to another row.
1375 * In that case we look for upward pivots until we reach a non-negative
1376 * value again.
1377 */
1378static int sign_of_min(struct isl_tab *tab, struct isl_tab_var *var)
1379{
1380 int row, col;
1381 struct isl_tab_var *pivot_var = NULL((void*)0);
1382
1383 if (min_is_manifestly_unbounded(tab, var))
1384 return -1;
1385 if (!var->is_row) {
1386 col = var->index;
1387 row = pivot_row(tab, NULL((void*)0), -1, col);
1388 pivot_var = var_from_col(tab, col);
1389 if (isl_tab_pivot(tab, row, col) < 0)
1390 return -2;
1391 if (var->is_redundant)
1392 return 0;
1393 if (isl_int_is_neg(tab->mat->row[var->index][1])(isl_sioimath_sgn(*(tab->mat->row[var->index][1])) <
0)
) {
1394 if (var->is_nonneg) {
1395 if (!pivot_var->is_redundant &&
1396 pivot_var->index == row) {
1397 if (isl_tab_pivot(tab, row, col) < 0)
1398 return -2;
1399 } else
1400 if (restore_row(tab, var) < -1)
1401 return -2;
1402 }
1403 return -1;
1404 }
1405 }
1406 if (var->is_redundant)
1407 return 0;
1408 while (!isl_int_is_neg(tab->mat->row[var->index][1])(isl_sioimath_sgn(*(tab->mat->row[var->index][1])) <
0)
) {
1409 find_pivot(tab, var, var, -1, &row, &col);
1410 if (row == var->index)
1411 return -1;
1412 if (row == -1)
1413 return isl_int_sgn(tab->mat->row[var->index][1])isl_sioimath_sgn(*(tab->mat->row[var->index][1]));
1414 pivot_var = var_from_col(tab, col);
1415 if (isl_tab_pivot(tab, row, col) < 0)
1416 return -2;
1417 if (var->is_redundant)
1418 return 0;
1419 }
1420 if (pivot_var && var->is_nonneg) {
1421 /* pivot back to non-negative value */
1422 if (!pivot_var->is_redundant && pivot_var->index == row) {
1423 if (isl_tab_pivot(tab, row, col) < 0)
1424 return -2;
1425 } else
1426 if (restore_row(tab, var) < -1)
1427 return -2;
1428 }
1429 return -1;
1430}
1431
1432static int row_at_most_neg_one(struct isl_tab *tab, int row)
1433{
1434 if (tab->M) {
1435 if (isl_int_is_pos(tab->mat->row[row][2])(isl_sioimath_sgn(*(tab->mat->row[row][2])) > 0))
1436 return 0;
1437 if (isl_int_is_neg(tab->mat->row[row][2])(isl_sioimath_sgn(*(tab->mat->row[row][2])) < 0))
1438 return 1;
1439 }
1440 return isl_int_is_neg(tab->mat->row[row][1])(isl_sioimath_sgn(*(tab->mat->row[row][1])) < 0) &&
1441 isl_int_abs_ge(tab->mat->row[row][1],(isl_sioimath_abs_cmp(*(tab->mat->row[row][1]), *(tab->
mat->row[row][0])) >= 0)
1442 tab->mat->row[row][0])(isl_sioimath_abs_cmp(*(tab->mat->row[row][1]), *(tab->
mat->row[row][0])) >= 0)
;
1443}
1444
1445/* Return 1 if "var" can attain values <= -1.
1446 * Return 0 otherwise.
1447 *
1448 * If the variable "var" is supposed to be non-negative (is_nonneg is set),
1449 * then the sample value of "var" is assumed to be non-negative when the
1450 * the function is called. If 1 is returned then the constraint
1451 * is not redundant and the sample value is made non-negative again before
1452 * the function returns.
1453 */
1454int isl_tab_min_at_most_neg_one(struct isl_tab *tab, struct isl_tab_var *var)
1455{
1456 int row, col;
1457 struct isl_tab_var *pivot_var;
1458
1459 if (min_is_manifestly_unbounded(tab, var))
1460 return 1;
1461 if (!var->is_row) {
1462 col = var->index;
1463 row = pivot_row(tab, NULL((void*)0), -1, col);
1464 pivot_var = var_from_col(tab, col);
1465 if (isl_tab_pivot(tab, row, col) < 0)
1466 return -1;
1467 if (var->is_redundant)
1468 return 0;
1469 if (row_at_most_neg_one(tab, var->index)) {
1470 if (var->is_nonneg) {
1471 if (!pivot_var->is_redundant &&
1472 pivot_var->index == row) {
1473 if (isl_tab_pivot(tab, row, col) < 0)
1474 return -1;
1475 } else
1476 if (restore_row(tab, var) < -1)
1477 return -1;
1478 }
1479 return 1;
1480 }
1481 }
1482 if (var->is_redundant)
1483 return 0;
1484 do {
1485 find_pivot(tab, var, var, -1, &row, &col);
1486 if (row == var->index) {
1487 if (var->is_nonneg && restore_row(tab, var) < -1)
1488 return -1;
1489 return 1;
1490 }
1491 if (row == -1)
1492 return 0;
1493 pivot_var = var_from_col(tab, col);
1494 if (isl_tab_pivot(tab, row, col) < 0)
1495 return -1;
1496 if (var->is_redundant)
1497 return 0;
1498 } while (!row_at_most_neg_one(tab, var->index));
1499 if (var->is_nonneg) {
1500 /* pivot back to non-negative value */
1501 if (!pivot_var->is_redundant && pivot_var->index == row)
1502 if (isl_tab_pivot(tab, row, col) < 0)
1503 return -1;
1504 if (restore_row(tab, var) < -1)
1505 return -1;
1506 }
1507 return 1;
1508}
1509
1510/* Return 1 if "var" can attain values >= 1.
1511 * Return 0 otherwise.
1512 */
1513static int at_least_one(struct isl_tab *tab, struct isl_tab_var *var)
1514{
1515 int row, col;
1516 isl_int *r;
1517
1518 if (max_is_manifestly_unbounded(tab, var))
1519 return 1;
1520 if (to_row(tab, var, 1) < 0)
1521 return -1;
1522 r = tab->mat->row[var->index];
1523 while (isl_int_lt(r[1], r[0])(isl_sioimath_cmp(*(r[1]), *(r[0])) < 0)) {
1524 find_pivot(tab, var, var, 1, &row, &col);
1525 if (row == -1)
1526 return isl_int_ge(r[1], r[0])(isl_sioimath_cmp(*(r[1]), *(r[0])) >= 0);
1527 if (row == var->index) /* manifestly unbounded */
1528 return 1;
1529 if (isl_tab_pivot(tab, row, col) < 0)
1530 return -1;
1531 }
1532 return 1;
1533}
1534
1535static void swap_cols(struct isl_tab *tab, int col1, int col2)
1536{
1537 int t;
1538 unsigned off = 2 + tab->M;
1539 t = tab->col_var[col1];
1540 tab->col_var[col1] = tab->col_var[col2];
1541 tab->col_var[col2] = t;
1542 var_from_col(tab, col1)->index = col1;
1543 var_from_col(tab, col2)->index = col2;
1544 tab->mat = isl_mat_swap_cols(tab->mat, off + col1, off + col2);
1545}
1546
1547/* Mark column with index "col" as representing a zero variable.
1548 * If we may need to undo the operation the column is kept,
1549 * but no longer considered.
1550 * Otherwise, the column is simply removed.
1551 *
1552 * The column may be interchanged with some other column. If it
1553 * is interchanged with a later column, return 1. Otherwise return 0.
1554 * If the columns are checked in order in the calling function,
1555 * then a return value of 1 means that the column with the given
1556 * column number may now contain a different column that
1557 * hasn't been checked yet.
1558 */
1559int isl_tab_kill_col(struct isl_tab *tab, int col)
1560{
1561 var_from_col(tab, col)->is_zero = 1;
1562 if (tab->need_undo) {
1563 if (isl_tab_push_var(tab, isl_tab_undo_zero,
1564 var_from_col(tab, col)) < 0)
1565 return -1;
1566 if (col != tab->n_dead)
1567 swap_cols(tab, col, tab->n_dead);
1568 tab->n_dead++;
1569 return 0;
1570 } else {
1571 if (col != tab->n_col - 1)
1572 swap_cols(tab, col, tab->n_col - 1);
1573 var_from_col(tab, tab->n_col - 1)->index = -1;
1574 tab->n_col--;
1575 return 1;
1576 }
1577}
1578
1579static int row_is_manifestly_non_integral(struct isl_tab *tab, int row)
1580{
1581 unsigned off = 2 + tab->M;
1582
1583 if (tab->M && !isl_int_eq(tab->mat->row[row][2],(isl_sioimath_cmp(*(tab->mat->row[row][2]), *(tab->mat
->row[row][0])) == 0)
1584 tab->mat->row[row][0])(isl_sioimath_cmp(*(tab->mat->row[row][2]), *(tab->mat
->row[row][0])) == 0)
)
1585 return 0;
1586 if (isl_seq_first_non_zero(tab->mat->row[row] + off + tab->n_dead,
1587 tab->n_col - tab->n_dead) != -1)
1588 return 0;
1589
1590 return !isl_int_is_divisible_by(tab->mat->row[row][1],isl_sioimath_is_divisible_by(*(tab->mat->row[row][1]), *
(tab->mat->row[row][0]))
1591 tab->mat->row[row][0])isl_sioimath_is_divisible_by(*(tab->mat->row[row][1]), *
(tab->mat->row[row][0]))
;
1592}
1593
1594/* For integer tableaus, check if any of the coordinates are stuck
1595 * at a non-integral value.
1596 */
1597static int tab_is_manifestly_empty(struct isl_tab *tab)
1598{
1599 int i;
1600
1601 if (tab->empty)
1602 return 1;
1603 if (tab->rational)
1604 return 0;
1605
1606 for (i = 0; i < tab->n_var; ++i) {
1607 if (!tab->var[i].is_row)
1608 continue;
1609 if (row_is_manifestly_non_integral(tab, tab->var[i].index))
1610 return 1;
1611 }
1612
1613 return 0;
1614}
1615
1616/* Row variable "var" is non-negative and cannot attain any values
1617 * larger than zero. This means that the coefficients of the unrestricted
1618 * column variables are zero and that the coefficients of the non-negative
1619 * column variables are zero or negative.
1620 * Each of the non-negative variables with a negative coefficient can
1621 * then also be written as the negative sum of non-negative variables
1622 * and must therefore also be zero.
1623 *
1624 * If "temp_var" is set, then "var" is a temporary variable that
1625 * will be removed after this function returns and for which
1626 * no information is recorded on the undo stack.
1627 * Do not add any undo records involving this variable in this case
1628 * since the variable will have been removed before any future undo
1629 * operations. Also avoid marking the variable as redundant,
1630 * since that either adds an undo record or needlessly removes the row
1631 * (the caller will take care of removing the row).
1632 */
1633static isl_stat close_row(struct isl_tab *tab, struct isl_tab_var *var,
1634 int temp_var) WARN_UNUSED__attribute__((__warn_unused_result__));
1635static isl_stat close_row(struct isl_tab *tab, struct isl_tab_var *var,
1636 int temp_var)
1637{
1638 int j;
1639 struct isl_mat *mat = tab->mat;
1640 unsigned off = 2 + tab->M;
1641
1642 if (!var->is_nonneg)
1643 isl_die(isl_tab_get_ctx(tab), isl_error_internal,do { isl_handle_error(isl_tab_get_ctx(tab), isl_error_internal
, "expecting non-negative variable", "/build/llvm-toolchain-snapshot-7~svn338205/tools/polly/lib/External/isl/isl_tab.c"
, 1645); return isl_stat_error; } while (0)
1644 "expecting non-negative variable",do { isl_handle_error(isl_tab_get_ctx(tab), isl_error_internal
, "expecting non-negative variable", "/build/llvm-toolchain-snapshot-7~svn338205/tools/polly/lib/External/isl/isl_tab.c"
, 1645); return isl_stat_error; } while (0)
1645 return isl_stat_error)do { isl_handle_error(isl_tab_get_ctx(tab), isl_error_internal
, "expecting non-negative variable", "/build/llvm-toolchain-snapshot-7~svn338205/tools/polly/lib/External/isl/isl_tab.c"
, 1645); return isl_stat_error; } while (0)
;
1646 var->is_zero = 1;
1647 if (!temp_var && tab->need_undo)
1648 if (isl_tab_push_var(tab, isl_tab_undo_zero, var) < 0)
1649 return isl_stat_error;
1650 for (j = tab->n_dead; j < tab->n_col; ++j) {
1651 int recheck;
1652 if (isl_int_is_zero(mat->row[var->index][off + j])(isl_sioimath_sgn(*(mat->row[var->index][off + j])) == 0
)
)
1653 continue;
1654 if (isl_int_is_pos(mat->row[var->index][off + j])(isl_sioimath_sgn(*(mat->row[var->index][off + j])) >
0)
)
1655 isl_die(isl_tab_get_ctx(tab), isl_error_internal,do { isl_handle_error(isl_tab_get_ctx(tab), isl_error_internal
, "row cannot have positive coefficients", "/build/llvm-toolchain-snapshot-7~svn338205/tools/polly/lib/External/isl/isl_tab.c"
, 1657); return isl_stat_error; } while (0)
1656 "row cannot have positive coefficients",do { isl_handle_error(isl_tab_get_ctx(tab), isl_error_internal
, "row cannot have positive coefficients", "/build/llvm-toolchain-snapshot-7~svn338205/tools/polly/lib/External/isl/isl_tab.c"
, 1657); return isl_stat_error; } while (0)
1657 return isl_stat_error)do { isl_handle_error(isl_tab_get_ctx(tab), isl_error_internal
, "row cannot have positive coefficients", "/build/llvm-toolchain-snapshot-7~svn338205/tools/polly/lib/External/isl/isl_tab.c"
, 1657); return isl_stat_error; } while (0)
;
1658 recheck = isl_tab_kill_col(tab, j);
1659 if (recheck < 0)
1660 return isl_stat_error;
1661 if (recheck)
1662 --j;
1663 }
1664 if (!temp_var && isl_tab_mark_redundant(tab, var->index) < 0)
1665 return isl_stat_error;
1666 if (tab_is_manifestly_empty(tab) && isl_tab_mark_empty(tab) < 0)
1667 return isl_stat_error;
1668 return isl_stat_ok;
1669}
1670
1671/* Add a constraint to the tableau and allocate a row for it.
1672 * Return the index into the constraint array "con".
1673 *
1674 * This function assumes that at least one more row and at least
1675 * one more element in the constraint array are available in the tableau.
1676 */
1677int isl_tab_allocate_con(struct isl_tab *tab)
1678{
1679 int r;
1680
1681 isl_assert(tab->mat->ctx, tab->n_row < tab->mat->n_row, return -1)do { if (tab->n_row < tab->mat->n_row) break; do {
isl_handle_error(tab->mat->ctx, isl_error_unknown, "Assertion \""
"tab->n_row < tab->mat->n_row" "\" failed", "/build/llvm-toolchain-snapshot-7~svn338205/tools/polly/lib/External/isl/isl_tab.c"
, 1681); return -1; } while (0); } while (0)
;
1682 isl_assert(tab->mat->ctx, tab->n_con < tab->max_con, return -1)do { if (tab->n_con < tab->max_con) break; do { isl_handle_error
(tab->mat->ctx, isl_error_unknown, "Assertion \"" "tab->n_con < tab->max_con"
"\" failed", "/build/llvm-toolchain-snapshot-7~svn338205/tools/polly/lib/External/isl/isl_tab.c"
, 1682); return -1; } while (0); } while (0)
;
1683
1684 r = tab->n_con;
1685 tab->con[r].index = tab->n_row;
1686 tab->con[r].is_row = 1;
1687 tab->con[r].is_nonneg = 0;
1688 tab->con[r].is_zero = 0;
1689 tab->con[r].is_redundant = 0;
1690 tab->con[r].frozen = 0;
1691 tab->con[r].negated = 0;
1692 tab->row_var[tab->n_row] = ~r;
1693
1694 tab->n_row++;
1695 tab->n_con++;
1696 if (isl_tab_push_var(tab, isl_tab_undo_allocate, &tab->con[r]) < 0)
1697 return -1;
1698
1699 return r;
1700}
1701
1702/* Move the entries in tab->var up one position, starting at "first",
1703 * creating room for an extra entry at position "first".
1704 * Since some of the entries of tab->row_var and tab->col_var contain
1705 * indices into this array, they have to be updated accordingly.
1706 */
1707static int var_insert_entry(struct isl_tab *tab, int first)
1708{
1709 int i;
1710
1711 if (tab->n_var >= tab->max_var)
1712 isl_die(isl_tab_get_ctx(tab), isl_error_internal,do { isl_handle_error(isl_tab_get_ctx(tab), isl_error_internal
, "not enough room for new variable", "/build/llvm-toolchain-snapshot-7~svn338205/tools/polly/lib/External/isl/isl_tab.c"
, 1713); return -1; } while (0)
1713 "not enough room for new variable", return -1)do { isl_handle_error(isl_tab_get_ctx(tab), isl_error_internal
, "not enough room for new variable", "/build/llvm-toolchain-snapshot-7~svn338205/tools/polly/lib/External/isl/isl_tab.c"
, 1713); return -1; } while (0)
;
1714 if (first > tab->n_var)
1715 isl_die(isl_tab_get_ctx(tab), isl_error_internal,do { isl_handle_error(isl_tab_get_ctx(tab), isl_error_internal
, "invalid initial position", "/build/llvm-toolchain-snapshot-7~svn338205/tools/polly/lib/External/isl/isl_tab.c"
, 1716); return -1; } while (0)
1716 "invalid initial position", return -1)do { isl_handle_error(isl_tab_get_ctx(tab), isl_error_internal
, "invalid initial position", "/build/llvm-toolchain-snapshot-7~svn338205/tools/polly/lib/External/isl/isl_tab.c"
, 1716); return -1; } while (0)
;
1717
1718 for (i = tab->n_var - 1; i >= first; --i) {
1719 tab->var[i + 1] = tab->var[i];
1720 if (tab->var[i + 1].is_row)
1721 tab->row_var[tab->var[i + 1].index]++;
1722 else
1723 tab->col_var[tab->var[i + 1].index]++;
1724 }
1725
1726 tab->n_var++;
1727
1728 return 0;
1729}
1730
1731/* Drop the entry at position "first" in tab->var, moving all
1732 * subsequent entries down.
1733 * Since some of the entries of tab->row_var and tab->col_var contain
1734 * indices into this array, they have to be updated accordingly.
1735 */
1736static int var_drop_entry(struct isl_tab *tab, int first)
1737{
1738 int i;
1739
1740 if (first >= tab->n_var)
1741 isl_die(isl_tab_get_ctx(tab), isl_error_internal,do { isl_handle_error(isl_tab_get_ctx(tab), isl_error_internal
, "invalid initial position", "/build/llvm-toolchain-snapshot-7~svn338205/tools/polly/lib/External/isl/isl_tab.c"
, 1742); return -1; } while (0)
1742 "invalid initial position", return -1)do { isl_handle_error(isl_tab_get_ctx(tab), isl_error_internal
, "invalid initial position", "/build/llvm-toolchain-snapshot-7~svn338205/tools/polly/lib/External/isl/isl_tab.c"
, 1742); return -1; } while (0)
;
1743
1744 tab->n_var--;
1745
1746 for (i = first; i < tab->n_var; ++i) {
1747 tab->var[i] = tab->var[i + 1];
1748 if (tab->var[i + 1].is_row)
1749 tab->row_var[tab->var[i].index]--;
1750 else
1751 tab->col_var[tab->var[i].index]--;
1752 }
1753
1754 return 0;
1755}
1756
1757/* Add a variable to the tableau at position "r" and allocate a column for it.
1758 * Return the index into the variable array "var", i.e., "r",
1759 * or -1 on error.
1760 */
1761int isl_tab_insert_var(struct isl_tab *tab, int r)
1762{
1763 int i;
1764 unsigned off = 2 + tab->M;
1765
1766 isl_assert(tab->mat->ctx, tab->n_col < tab->mat->n_col, return -1)do { if (tab->n_col < tab->mat->n_col) break; do {
isl_handle_error(tab->mat->ctx, isl_error_unknown, "Assertion \""
"tab->n_col < tab->mat->n_col" "\" failed", "/build/llvm-toolchain-snapshot-7~svn338205/tools/polly/lib/External/isl/isl_tab.c"
, 1766); return -1; } while (0); } while (0)
;
1767
1768 if (var_insert_entry(tab, r) < 0)
1769 return -1;
1770
1771 tab->var[r].index = tab->n_col;
1772 tab->var[r].is_row = 0;
1773 tab->var[r].is_nonneg = 0;
1774 tab->var[r].is_zero = 0;
1775 tab->var[r].is_redundant = 0;
1776 tab->var[r].frozen = 0;
1777 tab->var[r].negated = 0;
1778 tab->col_var[tab->n_col] = r;
1779
1780 for (i = 0; i < tab->n_row; ++i)
1781 isl_int_set_si(tab->mat->row[i][off + tab->n_col], 0)isl_sioimath_set_si((tab->mat->row[i][off + tab->n_col
]), 0)
;
1782
1783 tab->n_col++;
1784 if (isl_tab_push_var(tab, isl_tab_undo_allocate, &tab->var[r]) < 0)
1785 return -1;
1786
1787 return r;
1788}
1789
1790/* Add a variable to the tableau and allocate a column for it.
1791 * Return the index into the variable array "var".
1792 */
1793int isl_tab_allocate_var(struct isl_tab *tab)
1794{
1795 if (!tab)
1796 return -1;
1797
1798 return isl_tab_insert_var(tab, tab->n_var);
1799}
1800
1801/* Add a row to the tableau. The row is given as an affine combination
1802 * of the original variables and needs to be expressed in terms of the
1803 * column variables.
1804 *
1805 * This function assumes that at least one more row and at least
1806 * one more element in the constraint array are available in the tableau.
1807 *
1808 * We add each term in turn.
1809 * If r = n/d_r is the current sum and we need to add k x, then
1810 * if x is a column variable, we increase the numerator of
1811 * this column by k d_r
1812 * if x = f/d_x is a row variable, then the new representation of r is
1813 *
1814 * n k f d_x/g n + d_r/g k f m/d_r n + m/d_g k f
1815 * --- + --- = ------------------- = -------------------
1816 * d_r d_r d_r d_x/g m
1817 *
1818 * with g the gcd of d_r and d_x and m the lcm of d_r and d_x.
1819 *
1820 * If tab->M is set, then, internally, each variable x is represented
1821 * as x' - M. We then also need no subtract k d_r from the coefficient of M.
1822 */
1823int isl_tab_add_row(struct isl_tab *tab, isl_int *line)
1824{
1825 int i;
1826 int r;
1827 isl_int *row;
1828 isl_int a, b;
1829 unsigned off = 2 + tab->M;
1830
1831 r = isl_tab_allocate_con(tab);
1832 if (r < 0)
1833 return -1;
1834
1835 isl_int_init(a)isl_sioimath_init((a));
1836 isl_int_init(b)isl_sioimath_init((b));
1837 row = tab->mat->row[tab->con[r].index];
1838 isl_int_set_si(row[0], 1)isl_sioimath_set_si((row[0]), 1);
1839 isl_int_set(row[1], line[0])isl_sioimath_set((row[1]), *(line[0]));
1840 isl_seq_clr(row + 2, tab->M + tab->n_col);
1841 for (i = 0; i < tab->n_var; ++i) {
1842 if (tab->var[i].is_zero)
1843 continue;
1844 if (tab->var[i].is_row) {
1845 isl_int_lcm(a,isl_sioimath_lcm((a), *(row[0]), *(tab->mat->row[tab->
var[i].index][0]))
1846 row[0], tab->mat->row[tab->var[i].index][0])isl_sioimath_lcm((a), *(row[0]), *(tab->mat->row[tab->
var[i].index][0]))
;
1847 isl_int_swap(a, row[0])isl_sioimath_swap((a), (row[0]));
1848 isl_int_divexact(a, row[0], a)isl_sioimath_tdiv_q((a), *(row[0]), *(a));
1849 isl_int_divexact(b,isl_sioimath_tdiv_q((b), *(row[0]), *(tab->mat->row[tab
->var[i].index][0]))
1850 row[0], tab->mat->row[tab->var[i].index][0])isl_sioimath_tdiv_q((b), *(row[0]), *(tab->mat->row[tab
->var[i].index][0]))
;
1851 isl_int_mul(b, b, line[1 + i])isl_sioimath_mul((b), *(b), *(line[1 + i]));
1852 isl_seq_combine(row + 1, a, row + 1,
1853 b, tab->mat->row[tab->var[i].index] + 1,
1854 1 + tab->M + tab->n_col);
1855 } else
1856 isl_int_addmul(row[off + tab->var[i].index],isl_sioimath_addmul((row[off + tab->var[i].index]), *(line
[1 + i]), *(row[0]))
1857 line[1 + i], row[0])isl_sioimath_addmul((row[off + tab->var[i].index]), *(line
[1 + i]), *(row[0]))
;
1858 if (tab->M && i >= tab->n_param && i < tab->n_var - tab->n_div)
1859 isl_int_submul(row[2], line[1 + i], row[0])isl_sioimath_submul((row[2]), *(line[1 + i]), *(row[0]));
1860 }
1861 isl_seq_normalize(tab->mat->ctx, row, off + tab->n_col);
1862 isl_int_clear(a)isl_sioimath_clear((a));
1863 isl_int_clear(b)isl_sioimath_clear((b));
1864
1865 if (tab->row_sign)
1866 tab->row_sign[tab->con[r].index] = isl_tab_row_unknown;
1867
1868 return r;
1869}
1870
1871static isl_stat drop_row(struct isl_tab *tab, int row)
1872{
1873 isl_assert(tab->mat->ctx, ~tab->row_var[row] == tab->n_con - 1,do { if (~tab->row_var[row] == tab->n_con - 1) break; do
{ isl_handle_error(tab->mat->ctx, isl_error_unknown, "Assertion \""
"~tab->row_var[row] == tab->n_con - 1" "\" failed", "/build/llvm-toolchain-snapshot-7~svn338205/tools/polly/lib/External/isl/isl_tab.c"
, 1874); return isl_stat_error; } while (0); } while (0)
1874 return isl_stat_error)do { if (~tab->row_var[row] == tab->n_con - 1) break; do
{ isl_handle_error(tab->mat->ctx, isl_error_unknown, "Assertion \""
"~tab->row_var[row] == tab->n_con - 1" "\" failed", "/build/llvm-toolchain-snapshot-7~svn338205/tools/polly/lib/External/isl/isl_tab.c"
, 1874); return isl_stat_error; } while (0); } while (0)
;
1875 if (row != tab->n_row - 1)
1876 swap_rows(tab, row, tab->n_row - 1);
1877 tab->n_row--;
1878 tab->n_con--;
1879 return isl_stat_ok;
1880}
1881
1882/* Drop the variable in column "col" along with the column.
1883 * The column is removed first because it may need to be moved
1884 * into the last position and this process requires
1885 * the contents of the col_var array in a state
1886 * before the removal of the variable.
1887 */
1888static isl_stat drop_col(struct isl_tab *tab, int col)
1889{
1890 int var;
1891
1892 var = tab->col_var[col];
1893 if (col != tab->n_col - 1)
1894 swap_cols(tab, col, tab->n_col - 1);
1895 tab->n_col--;
1896 if (var_drop_entry(tab, var) < 0)
1897 return isl_stat_error;
1898 return isl_stat_ok;
1899}
1900
1901/* Add inequality "ineq" and check if it conflicts with the
1902 * previously added constraints or if it is obviously redundant.
1903 *
1904 * This function assumes that at least one more row and at least
1905 * one more element in the constraint array are available in the tableau.
1906 */
1907isl_stat isl_tab_add_ineq(struct isl_tab *tab, isl_int *ineq)
1908{
1909 int r;
1910 int sgn;
1911 isl_int cst;
1912
1913 if (!tab)
12
Taking false branch
1914 return isl_stat_error;
1915 if (tab->bmap) {
13
Assuming the condition is false
14
Taking false branch
1916 struct isl_basic_map *bmap = tab->bmap;
1917
1918 isl_assert(tab->mat->ctx, tab->n_eq == bmap->n_eq,do { if (tab->n_eq == bmap->n_eq) break; do { isl_handle_error
(tab->mat->ctx, isl_error_unknown, "Assertion \"" "tab->n_eq == bmap->n_eq"
"\" failed", "/build/llvm-toolchain-snapshot-7~svn338205/tools/polly/lib/External/isl/isl_tab.c"
, 1919); return isl_stat_error; } while (0); } while (0)
1919 return isl_stat_error)do { if (tab->n_eq == bmap->n_eq) break; do { isl_handle_error
(tab->mat->ctx, isl_error_unknown, "Assertion \"" "tab->n_eq == bmap->n_eq"
"\" failed", "/build/llvm-toolchain-snapshot-7~svn338205/tools/polly/lib/External/isl/isl_tab.c"
, 1919); return isl_stat_error; } while (0); } while (0)
;
1920 isl_assert(tab->mat->ctx,do { if (tab->n_con == bmap->n_eq + bmap->n_ineq) break
; do { isl_handle_error(tab->mat->ctx, isl_error_unknown
, "Assertion \"" "tab->n_con == bmap->n_eq + bmap->n_ineq"
"\" failed", "/build/llvm-toolchain-snapshot-7~svn338205/tools/polly/lib/External/isl/isl_tab.c"
, 1922); return isl_stat_error; } while (0); } while (0)
1921 tab->n_con == bmap->n_eq + bmap->n_ineq,do { if (tab->n_con == bmap->n_eq + bmap->n_ineq) break
; do { isl_handle_error(tab->mat->ctx, isl_error_unknown
, "Assertion \"" "tab->n_con == bmap->n_eq + bmap->n_ineq"
"\" failed", "/build/llvm-toolchain-snapshot-7~svn338205/tools/polly/lib/External/isl/isl_tab.c"
, 1922); return isl_stat_error; } while (0); } while (0)
1922 return isl_stat_error)do { if (tab->n_con == bmap->n_eq + bmap->n_ineq) break
; do { isl_handle_error(tab->mat->ctx, isl_error_unknown
, "Assertion \"" "tab->n_con == bmap->n_eq + bmap->n_ineq"
"\" failed", "/build/llvm-toolchain-snapshot-7~svn338205/tools/polly/lib/External/isl/isl_tab.c"
, 1922); return isl_stat_error; } while (0); } while (0)
;
1923 tab->bmap = isl_basic_map_add_ineq(tab->bmap, ineq);
1924 if (isl_tab_push(tab, isl_tab_undo_bmap_ineq) < 0)
1925 return isl_stat_error;
1926 if (!tab->bmap)
1927 return isl_stat_error;
1928 }
1929 if (tab->cone) {
15
Assuming the condition is false
16
Taking false branch
1930 isl_int_init(cst)isl_sioimath_init((cst));
1931 isl_int_set_si(cst, 0)isl_sioimath_set_si((cst), 0);
1932 isl_int_swap(ineq[0], cst)isl_sioimath_swap((ineq[0]), (cst));
1933 }
1934 r = isl_tab_add_row(tab, ineq);
1935 if (tab->cone) {
17
Assuming the condition is true
18
Taking true branch
1936 isl_int_swap(ineq[0], cst)isl_sioimath_swap((ineq[0]), (cst));
19
Within the expansion of the macro 'isl_int_swap':
a
Calling 'isl_sioimath_swap'
1937 isl_int_clear(cst)isl_sioimath_clear((cst));
1938 }
1939 if (r < 0)
1940 return isl_stat_error;
1941 tab->con[r].is_nonneg = 1;
1942 if (isl_tab_push_var(tab, isl_tab_undo_nonneg, &tab->con[r]) < 0)
1943 return isl_stat_error;
1944 if (isl_tab_row_is_redundant(tab, tab->con[r].index)) {
1945 if (isl_tab_mark_redundant(tab, tab->con[r].index) < 0)
1946 return isl_stat_error;
1947 return isl_stat_ok;
1948 }
1949
1950 sgn = restore_row(tab, &tab->con[r]);
1951 if (sgn < -1)
1952 return isl_stat_error;
1953 if (sgn < 0)
1954 return isl_tab_mark_empty(tab);
1955 if (tab->con[r].is_row && isl_tab_row_is_redundant(tab, tab->con[r].index))
1956 if (isl_tab_mark_redundant(tab, tab->con[r].index) < 0)
1957 return isl_stat_error;
1958 return isl_stat_ok;
1959}
1960
1961/* Pivot a non-negative variable down until it reaches the value zero
1962 * and then pivot the variable into a column position.
1963 */
1964static int to_col(struct isl_tab *tab, struct isl_tab_var *var) WARN_UNUSED__attribute__((__warn_unused_result__));
1965static int to_col(struct isl_tab *tab, struct isl_tab_var *var)
1966{
1967 int i;
1968 int row, col;
1969 unsigned off = 2 + tab->M;
1970
1971 if (!var->is_row)
1972 return 0;
1973
1974 while (isl_int_is_pos(tab->mat->row[var->index][1])(isl_sioimath_sgn(*(tab->mat->row[var->index][1])) >
0)
) {
1975 find_pivot(tab, var, NULL((void*)0), -1, &row, &col);
1976 isl_assert(tab->mat->ctx, row != -1, return -1)do { if (row != -1) break; do { isl_handle_error(tab->mat->
ctx, isl_error_unknown, "Assertion \"" "row != -1" "\" failed"
, "/build/llvm-toolchain-snapshot-7~svn338205/tools/polly/lib/External/isl/isl_tab.c"
, 1976); return -1; } while (0); } while (0)
;
1977 if (isl_tab_pivot(tab, row, col) < 0)
1978 return -1;
1979 if (!var->is_row)
1980 return 0;
1981 }
1982
1983 for (i = tab->n_dead; i < tab->n_col; ++i)
1984 if (!isl_int_is_zero(tab->mat->row[var->index][off + i])(isl_sioimath_sgn(*(tab->mat->row[var->index][off + i
])) == 0)
)
1985 break;
1986
1987 isl_assert(tab->mat->ctx, i < tab->n_col, return -1)do { if (i < tab->n_col) break; do { isl_handle_error(tab
->mat->ctx, isl_error_unknown, "Assertion \"" "i < tab->n_col"
"\" failed", "/build/llvm-toolchain-snapshot-7~svn338205/tools/polly/lib/External/isl/isl_tab.c"
, 1987); return -1; } while (0); } while (0)
;
1988 if (isl_tab_pivot(tab, var->index, i) < 0)
1989 return -1;
1990
1991 return 0;
1992}
1993
1994/* We assume Gaussian elimination has been performed on the equalities.
1995 * The equalities can therefore never conflict.
1996 * Adding the equalities is currently only really useful for a later call
1997 * to isl_tab_ineq_type.
1998 *
1999 * This function assumes that at least one more row and at least
2000 * one more element in the constraint array are available in the tableau.
2001 */
2002static struct isl_tab *add_eq(struct isl_tab *tab, isl_int *eq)
2003{
2004 int i;
2005 int r;
2006
2007 if (!tab)
2008 return NULL((void*)0);
2009 r = isl_tab_add_row(tab, eq);
2010 if (r < 0)
2011 goto error;
2012
2013 r = tab->con[r].index;
2014 i = isl_seq_first_non_zero(tab->mat->row[r] + 2 + tab->M + tab->n_dead,
2015 tab->n_col - tab->n_dead);
2016 isl_assert(tab->mat->ctx, i >= 0, goto error)do { if (i >= 0) break; do { isl_handle_error(tab->mat->
ctx, isl_error_unknown, "Assertion \"" "i >= 0" "\" failed"
, "/build/llvm-toolchain-snapshot-7~svn338205/tools/polly/lib/External/isl/isl_tab.c"
, 2016); goto error; } while (0); } while (0)
;
2017 i += tab->n_dead;
2018 if (isl_tab_pivot(tab, r, i) < 0)
2019 goto error;
2020 if (isl_tab_kill_col(tab, i) < 0)
2021 goto error;
2022 tab->n_eq++;
2023
2024 return tab;
2025error:
2026 isl_tab_free(tab);
2027 return NULL((void*)0);
2028}
2029
2030/* Does the sample value of row "row" of "tab" involve the big parameter,
2031 * if any?
2032 */
2033static int row_is_big(struct isl_tab *tab, int row)
2034{
2035 return tab->M && !isl_int_is_zero(tab->mat->row[row][2])(isl_sioimath_sgn(*(tab->mat->row[row][2])) == 0);
2036}
2037
2038static int row_is_manifestly_zero(struct isl_tab *tab, int row)
2039{
2040 unsigned off = 2 + tab->M;
2041
2042 if (!isl_int_is_zero(tab->mat->row[row][1])(isl_sioimath_sgn(*(tab->mat->row[row][1])) == 0))
2043 return 0;
2044 if (row_is_big(tab, row))
2045 return 0;
2046 return isl_seq_first_non_zero(tab->mat->row[row] + off + tab->n_dead,
2047 tab->n_col - tab->n_dead) == -1;
2048}
2049
2050/* Add an equality that is known to be valid for the given tableau.
2051 *
2052 * This function assumes that at least one more row and at least
2053 * one more element in the constraint array are available in the tableau.
2054 */
2055int isl_tab_add_valid_eq(struct isl_tab *tab, isl_int *eq)
2056{
2057 struct isl_tab_var *var;
2058 int r;
2059
2060 if (!tab)
2061 return -1;
2062 r = isl_tab_add_row(tab, eq);
2063 if (r < 0)
2064 return -1;
2065
2066 var = &tab->con[r];
2067 r = var->index;
2068 if (row_is_manifestly_zero(tab, r)) {
2069 var->is_zero = 1;
2070 if (isl_tab_mark_redundant(tab, r) < 0)
2071 return -1;
2072 return 0;
2073 }
2074
2075 if (isl_int_is_neg(tab->mat->row[r][1])(isl_sioimath_sgn(*(tab->mat->row[r][1])) < 0)) {
2076 isl_seq_neg(tab->mat->row[r] + 1, tab->mat->row[r] + 1,
2077 1 + tab->n_col);
2078 var->negated = 1;
2079 }
2080 var->is_nonneg = 1;
2081 if (to_col(tab, var) < 0)
2082 return -1;
2083 var->is_nonneg = 0;
2084 if (isl_tab_kill_col(tab, var->index) < 0)
2085 return -1;
2086
2087 return 0;
2088}
2089
2090/* Add a zero row to "tab" and return the corresponding index
2091 * in the constraint array.
2092 *
2093 * This function assumes that at least one more row and at least
2094 * one more element in the constraint array are available in the tableau.
2095 */
2096static int add_zero_row(struct isl_tab *tab)
2097{
2098 int r;
2099 isl_int *row;
2100
2101 r = isl_tab_allocate_con(tab);
2102 if (r < 0)
2103 return -1;
2104
2105 row = tab->mat->row[tab->con[r].index];
2106 isl_seq_clr(row + 1, 1 + tab->M + tab->n_col);
2107 isl_int_set_si(row[0], 1)isl_sioimath_set_si((row[0]), 1);
2108
2109 return r;
2110}
2111
2112/* Add equality "eq" and check if it conflicts with the
2113 * previously added constraints or if it is obviously redundant.
2114 *
2115 * This function assumes that at least one more row and at least
2116 * one more element in the constraint array are available in the tableau.
2117 * If tab->bmap is set, then two rows are needed instead of one.
2118 */
2119int isl_tab_add_eq(struct isl_tab *tab, isl_int *eq)
2120{
2121 struct isl_tab_undo *snap = NULL((void*)0);
2122 struct isl_tab_var *var;
2123 int r;
2124 int row;
2125 int sgn;
2126 isl_int cst;
2127
2128 if (!tab)
2129 return -1;
2130 isl_assert(tab->mat->ctx, !tab->M, return -1)do { if (!tab->M) break; do { isl_handle_error(tab->mat
->ctx, isl_error_unknown, "Assertion \"" "!tab->M" "\" failed"
, "/build/llvm-toolchain-snapshot-7~svn338205/tools/polly/lib/External/isl/isl_tab.c"
, 2130); return -1; } while (0); } while (0)
;
2131
2132 if (tab->need_undo)
2133 snap = isl_tab_snap(tab);
2134
2135 if (tab->cone) {
2136 isl_int_init(cst)isl_sioimath_init((cst));
2137 isl_int_set_si(cst, 0)isl_sioimath_set_si((cst), 0);
2138 isl_int_swap(eq[0], cst)isl_sioimath_swap((eq[0]), (cst));
2139 }
2140 r = isl_tab_add_row(tab, eq);
2141 if (tab->cone) {
2142 isl_int_swap(eq[0], cst)isl_sioimath_swap((eq[0]), (cst));
2143 isl_int_clear(cst)isl_sioimath_clear((cst));
2144 }
2145 if (r < 0)
2146 return -1;
2147
2148 var = &tab->con[r];
2149 row = var->index;
2150 if (row_is_manifestly_zero(tab, row)) {
2151 if (snap)
2152 return isl_tab_rollback(tab, snap);
2153 return drop_row(tab, row);
2154 }
2155
2156 if (tab->bmap) {
2157 tab->bmap = isl_basic_map_add_ineq(tab->bmap, eq);
2158 if (isl_tab_push(tab, isl_tab_undo_bmap_ineq) < 0)
2159 return -1;
2160 isl_seq_neg(eq, eq, 1 + tab->n_var);
2161 tab->bmap = isl_basic_map_add_ineq(tab->bmap, eq);
2162 isl_seq_neg(eq, eq, 1 + tab->n_var);
2163 if (isl_tab_push(tab, isl_tab_undo_bmap_ineq) < 0)
2164 return -1;
2165 if (!tab->bmap)
2166 return -1;
2167 if (add_zero_row(tab) < 0)
2168 return -1;
2169 }
2170
2171 sgn = isl_int_sgn(tab->mat->row[row][1])isl_sioimath_sgn(*(tab->mat->row[row][1]));
2172
2173 if (sgn > 0) {
2174 isl_seq_neg(tab->mat->row[row] + 1, tab->mat->row[row] + 1,
2175 1 + tab->n_col);
2176 var->negated = 1;
2177 sgn = -1;
2178 }
2179
2180 if (sgn < 0) {
2181 sgn = sign_of_max(tab, var);
2182 if (sgn < -1)
2183 return -1;
2184 if (sgn < 0) {
2185 if (isl_tab_mark_empty(tab) < 0)
2186 return -1;
2187 return 0;
2188 }
2189 }
2190
2191 var->is_nonneg = 1;
2192 if (to_col(tab, var) < 0)
2193 return -1;
2194 var->is_nonneg = 0;
2195 if (isl_tab_kill_col(tab, var->index) < 0)
2196 return -1;
2197
2198 return 0;
2199}
2200
2201/* Construct and return an inequality that expresses an upper bound
2202 * on the given div.
2203 * In particular, if the div is given by
2204 *
2205 * d = floor(e/m)
2206 *
2207 * then the inequality expresses
2208 *
2209 * m d <= e
2210 */
2211static struct isl_vec *ineq_for_div(struct isl_basic_map *bmap, unsigned div)
2212{
2213 unsigned total;
2214 unsigned div_pos;
2215 struct isl_vec *ineq;
2216
2217 if (!bmap)
2218 return NULL((void*)0);
2219
2220 total = isl_basic_map_total_dim(bmap);
2221 div_pos = 1 + total - bmap->n_div + div;
2222
2223 ineq = isl_vec_alloc(bmap->ctx, 1 + total);
2224 if (!ineq)
2225 return NULL((void*)0);
2226
2227 isl_seq_cpy(ineq->el, bmap->div[div] + 1, 1 + total);
2228 isl_int_neg(ineq->el[div_pos], bmap->div[div][0])isl_sioimath_neg((ineq->el[div_pos]), *(bmap->div[div][
0]))
;
2229 return ineq;
2230}
2231
2232/* For a div d = floor(f/m), add the constraints
2233 *
2234 * f - m d >= 0
2235 * -(f-(m-1)) + m d >= 0
2236 *
2237 * Note that the second constraint is the negation of
2238 *
2239 * f - m d >= m
2240 *
2241 * If add_ineq is not NULL, then this function is used
2242 * instead of isl_tab_add_ineq to effectively add the inequalities.
2243 *
2244 * This function assumes that at least two more rows and at least
2245 * two more elements in the constraint array are available in the tableau.
2246 */
2247static isl_stat add_div_constraints(struct isl_tab *tab, unsigned div,
2248 isl_stat (*add_ineq)(void *user, isl_int *), void *user)
2249{
2250 unsigned total;
2251 unsigned div_pos;
2252 struct isl_vec *ineq;
2253
2254 total = isl_basic_map_total_dim(tab->bmap);
2255 div_pos = 1 + total - tab->bmap->n_div + div;
2256
2257 ineq = ineq_for_div(tab->bmap, div);
2258 if (!ineq)
2259 goto error;
2260
2261 if (add_ineq) {
2262 if (add_ineq(user, ineq->el) < 0)
2263 goto error;
2264 } else {
2265 if (isl_tab_add_ineq(tab, ineq->el) < 0)
2266 goto error;
2267 }
2268
2269 isl_seq_neg(ineq->el, tab->bmap->div[div] + 1, 1 + total);
2270 isl_int_set(ineq->el[div_pos], tab->bmap->div[div][0])isl_sioimath_set((ineq->el[div_pos]), *(tab->bmap->div
[div][0]))
;
2271 isl_int_add(ineq->el[0], ineq->el[0], ineq->el[div_pos])isl_sioimath_add((ineq->el[0]), *(ineq->el[0]), *(ineq->
el[div_pos]))
;
2272 isl_int_sub_ui(ineq->el[0], ineq->el[0], 1)isl_sioimath_sub_ui((ineq->el[0]), *(ineq->el[0]), 1);
2273
2274 if (add_ineq) {
2275 if (add_ineq(user, ineq->el) < 0)
2276 goto error;
2277 } else {
2278 if (isl_tab_add_ineq(tab, ineq->el) < 0)
2279 goto error;
2280 }
2281
2282 isl_vec_free(ineq);
2283
2284 return isl_stat_ok;
2285error:
2286 isl_vec_free(ineq);
2287 return isl_stat_error;
2288}
2289
2290/* Check whether the div described by "div" is obviously non-negative.
2291 * If we are using a big parameter, then we will encode the div
2292 * as div' = M + div, which is always non-negative.
2293 * Otherwise, we check whether div is a non-negative affine combination
2294 * of non-negative variables.
2295 */
2296static int div_is_nonneg(struct isl_tab *tab, __isl_keep isl_vec *div)
2297{
2298 int i;
2299
2300 if (tab->M)
2301 return 1;
2302
2303 if (isl_int_is_neg(div->el[1])(isl_sioimath_sgn(*(div->el[1])) < 0))
2304 return 0;
2305
2306 for (i = 0; i < tab->n_var; ++i) {
2307 if (isl_int_is_neg(div->el[2 + i])(isl_sioimath_sgn(*(div->el[2 + i])) < 0))
2308 return 0;
2309 if (isl_int_is_zero(div->el[2 + i])(isl_sioimath_sgn(*(div->el[2 + i])) == 0))
2310 continue;
2311 if (!tab->var[i].is_nonneg)
2312 return 0;
2313 }
2314
2315 return 1;
2316}
2317
2318/* Insert an extra div, prescribed by "div", to the tableau and
2319 * the associated bmap (which is assumed to be non-NULL).
2320 * The extra integer division is inserted at (tableau) position "pos".
2321 * Return "pos" or -1 if an error occurred.
2322 *
2323 * If add_ineq is not NULL, then this function is used instead
2324 * of isl_tab_add_ineq to add the div constraints.
2325 * This complication is needed because the code in isl_tab_pip
2326 * wants to perform some extra processing when an inequality
2327 * is added to the tableau.
2328 */
2329int isl_tab_insert_div(struct isl_tab *tab, int pos, __isl_keep isl_vec *div,
2330 isl_stat (*add_ineq)(void *user, isl_int *), void *user)
2331{
2332 int r;
2333 int nonneg;
2334 int n_div, o_div;
2335
2336 if (!tab || !div)
2337 return -1;
2338
2339 if (div->size != 1 + 1 + tab->n_var)
2340 isl_die(isl_tab_get_ctx(tab), isl_error_invalid,do { isl_handle_error(isl_tab_get_ctx(tab), isl_error_invalid
, "unexpected size", "/build/llvm-toolchain-snapshot-7~svn338205/tools/polly/lib/External/isl/isl_tab.c"
, 2341); return -1; } while (0)
2341 "unexpected size", return -1)do { isl_handle_error(isl_tab_get_ctx(tab), isl_error_invalid
, "unexpected size", "/build/llvm-toolchain-snapshot-7~svn338205/tools/polly/lib/External/isl/isl_tab.c"
, 2341); return -1; } while (0)
;
2342
2343 isl_assert(tab->mat->ctx, tab->bmap, return -1)do { if (tab->bmap) break; do { isl_handle_error(tab->mat
->ctx, isl_error_unknown, "Assertion \"" "tab->bmap" "\" failed"
, "/build/llvm-toolchain-snapshot-7~svn338205/tools/polly/lib/External/isl/isl_tab.c"
, 2343); return -1; } while (0); } while (0)
;
2344 n_div = isl_basic_map_dim(tab->bmap, isl_dim_div);
2345 o_div = tab->n_var - n_div;
2346 if (pos < o_div || pos > tab->n_var)
2347 isl_die(isl_tab_get_ctx(tab), isl_error_invalid,do { isl_handle_error(isl_tab_get_ctx(tab), isl_error_invalid
, "invalid position", "/build/llvm-toolchain-snapshot-7~svn338205/tools/polly/lib/External/isl/isl_tab.c"
, 2348); return -1; } while (0)
2348 "invalid position", return -1)do { isl_handle_error(isl_tab_get_ctx(tab), isl_error_invalid
, "invalid position", "/build/llvm-toolchain-snapshot-7~svn338205/tools/polly/lib/External/isl/isl_tab.c"
, 2348); return -1; } while (0)
;
2349
2350 nonneg = div_is_nonneg(tab, div);
2351
2352 if (isl_tab_extend_cons(tab, 3) < 0)
2353 return -1;
2354 if (isl_tab_extend_vars(tab, 1) < 0)
2355 return -1;
2356 r = isl_tab_insert_var(tab, pos);
2357 if (r < 0)
2358 return -1;
2359
2360 if (nonneg)
2361 tab->var[r].is_nonneg = 1;
2362
2363 tab->bmap = isl_basic_map_insert_div(tab->bmap, pos - o_div, div);
2364 if (!tab->bmap)
2365 return -1;
2366 if (isl_tab_push_var(tab, isl_tab_undo_bmap_div, &tab->var[r]) < 0)
2367 return -1;
2368
2369 if (add_div_constraints(tab, pos - o_div, add_ineq, user) < 0)
2370 return -1;
2371
2372 return r;
2373}
2374
2375/* Add an extra div, prescribed by "div", to the tableau and
2376 * the associated bmap (which is assumed to be non-NULL).
2377 */
2378int isl_tab_add_div(struct isl_tab *tab, __isl_keep isl_vec *div)
2379{
2380 if (!tab)
2381 return -1;
2382 return isl_tab_insert_div(tab, tab->n_var, div, NULL((void*)0), NULL((void*)0));
2383}
2384
2385/* If "track" is set, then we want to keep track of all constraints in tab
2386 * in its bmap field. This field is initialized from a copy of "bmap",
2387 * so we need to make sure that all constraints in "bmap" also appear
2388 * in the constructed tab.
2389 */
2390__isl_give struct isl_tab *isl_tab_from_basic_map(
2391 __isl_keep isl_basic_map *bmap, int track)
2392{
2393 int i;
2394 struct isl_tab *tab;
2395
2396 if (!bmap)
2
Assuming 'bmap' is non-null
3
Taking false branch
2397 return NULL((void*)0);
2398 tab = isl_tab_alloc(bmap->ctx,
2399 isl_basic_map_total_dim(bmap) + bmap->n_ineq + 1,
2400 isl_basic_map_total_dim(bmap), 0);
2401 if (!tab)
4
Assuming 'tab' is non-null
5
Taking false branch
2402 return NULL((void*)0);
2403 tab->preserve = track;
2404 tab->rational = ISL_F_ISSET(bmap, ISL_BASIC_MAP_RATIONAL)(!!(((bmap)->flags) & ((1 << 4))));
2405 if (ISL_F_ISSET(bmap, ISL_BASIC_MAP_EMPTY)(!!(((bmap)->flags) & ((1 << 1))))) {
6
Taking false branch
2406 if (isl_tab_mark_empty(tab) < 0)
2407 goto error;
2408 goto done;
2409 }
2410 for (i = 0; i < bmap->n_eq; ++i) {
7
Assuming the condition is false
8
Loop condition is false. Execution continues on line 2415
2411 tab = add_eq(tab, bmap->eq[i]);
2412 if (!tab)
2413 return tab;
2414 }
2415 for (i = 0; i < bmap->n_ineq; ++i) {
9
Assuming the condition is true
10
Loop condition is true. Entering loop body
2416 if (isl_tab_add_ineq(tab, bmap->ineq[i]) < 0)
11
Calling 'isl_tab_add_ineq'
2417 goto error;
2418 if (tab->empty)
2419 goto done;
2420 }
2421done:
2422 if (track && isl_tab_track_bmap(tab, isl_basic_map_copy(bmap)) < 0)
2423 goto error;
2424 return tab;
2425error:
2426 isl_tab_free(tab);
2427 return NULL((void*)0);
2428}
2429
2430__isl_give struct isl_tab *isl_tab_from_basic_set(
2431 __isl_keep isl_basic_setisl_basic_map *bset, int track)
2432{
2433 return isl_tab_from_basic_map(bset, track);
1
Calling 'isl_tab_from_basic_map'
2434}
2435
2436/* Construct a tableau corresponding to the recession cone of "bset".
2437 */
2438struct isl_tab *isl_tab_from_recession_cone(__isl_keep isl_basic_setisl_basic_map *bset,
2439 int parametric)
2440{
2441 isl_int cst;
2442 int i;
2443 struct isl_tab *tab;
2444 unsigned offset = 0;
2445
2446 if (!bset)
2447 return NULL((void*)0);
2448 if (parametric)
2449 offset = isl_basic_set_dim(bset, isl_dim_param);
2450 tab = isl_tab_alloc(bset->ctx, bset->n_eq + bset->n_ineq,
2451 isl_basic_set_total_dim(bset) - offset, 0);
2452 if (!tab)
2453 return NULL((void*)0);
2454 tab->rational = ISL_F_ISSET(bset, ISL_BASIC_SET_RATIONAL)(!!(((bset)->flags) & ((1 << 4))));
2455 tab->cone = 1;
2456
2457 isl_int_init(cst)isl_sioimath_init((cst));
2458 isl_int_set_si(cst, 0)isl_sioimath_set_si((cst), 0);
2459 for (i = 0; i < bset->n_eq; ++i) {
2460 isl_int_swap(bset->eq[i][offset], cst)isl_sioimath_swap((bset->eq[i][offset]), (cst));
2461 if (offset > 0) {
2462 if (isl_tab_add_eq(tab, bset->eq[i] + offset) < 0)
2463 goto error;
2464 } else
2465 tab = add_eq(tab, bset->eq[i]);
2466 isl_int_swap(bset->eq[i][offset], cst)isl_sioimath_swap((bset->eq[i][offset]), (cst));
2467 if (!tab)
2468 goto done;
2469 }
2470 for (i = 0; i < bset->n_ineq; ++i) {
2471 int r;
2472 isl_int_swap(bset->ineq[i][offset], cst)isl_sioimath_swap((bset->ineq[i][offset]), (cst));
2473 r = isl_tab_add_row(tab, bset->ineq[i] + offset);
2474 isl_int_swap(bset->ineq[i][offset], cst)isl_sioimath_swap((bset->ineq[i][offset]), (cst));
2475 if (r < 0)
2476 goto error;
2477 tab->con[r].is_nonneg = 1;
2478 if (isl_tab_push_var(tab, isl_tab_undo_nonneg, &tab->con[r]) < 0)
2479 goto error;
2480 }
2481done:
2482 isl_int_clear(cst)isl_sioimath_clear((cst));
2483 return tab;
2484error:
2485 isl_int_clear(cst)isl_sioimath_clear((cst));
2486 isl_tab_free(tab);
2487 return NULL((void*)0);
2488}
2489
2490/* Assuming "tab" is the tableau of a cone, check if the cone is
2491 * bounded, i.e., if it is empty or only contains the origin.
2492 */
2493isl_bool isl_tab_cone_is_bounded(struct isl_tab *tab)
2494{
2495 int i;
2496
2497 if (!tab)
2498 return isl_bool_error;
2499 if (tab->empty)
2500 return isl_bool_true;
2501 if (tab->n_dead == tab->n_col)
2502 return isl_bool_true;
2503
2504 for (;;) {
2505 for (i = tab->n_redundant; i < tab->n_row; ++i) {
2506 struct isl_tab_var *var;
2507 int sgn;
2508 var = isl_tab_var_from_row(tab, i);
2509 if (!var->is_nonneg)
2510 continue;
2511 sgn = sign_of_max(tab, var);
2512 if (sgn < -1)
2513 return isl_bool_error;
2514 if (sgn != 0)
2515 return isl_bool_false;
2516 if (close_row(tab, var, 0) < 0)
2517 return isl_bool_error;
2518 break;
2519 }
2520 if (tab->n_dead == tab->n_col)
2521 return isl_bool_true;
2522 if (i == tab->n_row)
2523 return isl_bool_false;
2524 }
2525}
2526
2527int isl_tab_sample_is_integer(struct isl_tab *tab)
2528{
2529 int i;
2530
2531 if (!tab)
2532 return -1;
2533
2534 for (i = 0; i < tab->n_var; ++i) {
2535 int row;
2536 if (!tab->var[i].is_row)
2537 continue;
2538 row = tab->var[i].index;
2539 if (!isl_int_is_divisible_by(tab->mat->row[row][1],isl_sioimath_is_divisible_by(*(tab->mat->row[row][1]), *
(tab->mat->row[row][0]))
2540 tab->mat->row[row][0])isl_sioimath_is_divisible_by(*(tab->mat->row[row][1]), *
(tab->mat->row[row][0]))
)
2541 return 0;
2542 }
2543 return 1;
2544}
2545
2546static struct isl_vec *extract_integer_sample(struct isl_tab *tab)
2547{
2548 int i;
2549 struct isl_vec *vec;
2550
2551 vec = isl_vec_alloc(tab->mat->ctx, 1 + tab->n_var);
2552 if (!vec)
2553 return NULL((void*)0);
2554
2555 isl_int_set_si(vec->block.data[0], 1)isl_sioimath_set_si((vec->block.data[0]), 1);
2556 for (i = 0; i < tab->n_var; ++i) {
2557 if (!tab->var[i].is_row)
2558 isl_int_set_si(vec->block.data[1 + i], 0)isl_sioimath_set_si((vec->block.data[1 + i]), 0);
2559 else {
2560 int row = tab->var[i].index;
2561 isl_int_divexact(vec->block.data[1 + i],isl_sioimath_tdiv_q((vec->block.data[1 + i]), *(tab->mat
->row[row][1]), *(tab->mat->row[row][0]))
2562 tab->mat->row[row][1], tab->mat->row[row][0])isl_sioimath_tdiv_q((vec->block.data[1 + i]), *(tab->mat
->row[row][1]), *(tab->mat->row[row][0]))
;
2563 }
2564 }
2565
2566 return vec;
2567}
2568
2569struct isl_vec *isl_tab_get_sample_value(struct isl_tab *tab)
2570{
2571 int i;
2572 struct isl_vec *vec;
2573 isl_int m;
2574
2575 if (!tab)
2576 return NULL((void*)0);
2577
2578 vec = isl_vec_alloc(tab->mat->ctx, 1 + tab->n_var);
2579 if (!vec)
2580 return NULL((void*)0);
2581
2582 isl_int_init(m)isl_sioimath_init((m));
2583
2584 isl_int_set_si(vec->block.data[0], 1)isl_sioimath_set_si((vec->block.data[0]), 1);
2585 for (i = 0; i < tab->n_var; ++i) {
2586 int row;
2587 if (!tab->var[i].is_row) {
2588 isl_int_set_si(vec->block.data[1 + i], 0)isl_sioimath_set_si((vec->block.data[1 + i]), 0);
2589 continue;
2590 }
2591 row = tab->var[i].index;
2592 isl_int_gcd(m, vec->block.data[0], tab->mat->row[row][0])isl_sioimath_gcd((m), *(vec->block.data[0]), *(tab->mat
->row[row][0]))
;
2593 isl_int_divexact(m, tab->mat->row[row][0], m)isl_sioimath_tdiv_q((m), *(tab->mat->row[row][0]), *(m)
)
;
2594 isl_seq_scale(vec->block.data, vec->block.data, m, 1 + i);
2595 isl_int_divexact(m, vec->block.data[0], tab->mat->row[row][0])isl_sioimath_tdiv_q((m), *(vec->block.data[0]), *(tab->
mat->row[row][0]))
;
2596 isl_int_mul(vec->block.data[1 + i], m, tab->mat->row[row][1])isl_sioimath_mul((vec->block.data[1 + i]), *(m), *(tab->
mat->row[row][1]))
;
2597 }
2598 vec = isl_vec_normalize(vec);
2599
2600 isl_int_clear(m)isl_sioimath_clear((m));
2601 return vec;
2602}
2603
2604/* Store the sample value of "var" of "tab" rounded up (if sgn > 0)
2605 * or down (if sgn < 0) to the nearest integer in *v.
2606 */
2607static void get_rounded_sample_value(struct isl_tab *tab,
2608 struct isl_tab_var *var, int sgn, isl_int *v)
2609{
2610 if (!var->is_row)
2611 isl_int_set_si(*v, 0)isl_sioimath_set_si((*v), 0);
2612 else if (sgn > 0)
2613 isl_int_cdiv_q(*v, tab->mat->row[var->index][1],isl_sioimath_cdiv_q((*v), *(tab->mat->row[var->index
][1]), *(tab->mat->row[var->index][0]))
2614 tab->mat->row[var->index][0])isl_sioimath_cdiv_q((*v), *(tab->mat->row[var->index
][1]), *(tab->mat->row[var->index][0]))
;
2615 else
2616 isl_int_fdiv_q(*v, tab->mat->row[var->index][1],isl_sioimath_fdiv_q((*v), *(tab->mat->row[var->index
][1]), *(tab->mat->row[var->index][0]))
2617 tab->mat->row[var->index][0])isl_sioimath_fdiv_q((*v), *(tab->mat->row[var->index
][1]), *(tab->mat->row[var->index][0]))
;
2618}
2619
2620/* Update "bmap" based on the results of the tableau "tab".
2621 * In particular, implicit equalities are made explicit, redundant constraints
2622 * are removed and if the sample value happens to be integer, it is stored
2623 * in "bmap" (unless "bmap" already had an integer sample).
2624 *
2625 * The tableau is assumed to have been created from "bmap" using
2626 * isl_tab_from_basic_map.
2627 */
2628struct isl_basic_map *isl_basic_map_update_from_tab(struct isl_basic_map *bmap,
2629 struct isl_tab *tab)
2630{
2631 int i;
2632 unsigned n_eq;
2633
2634 if (!bmap)
2635 return NULL((void*)0);
2636 if (!tab)
2637 return bmap;
2638
2639 n_eq = tab->n_eq;
2640 if (tab->empty)
2641 bmap = isl_basic_map_set_to_empty(bmap);
2642 else
2643 for (i = bmap->n_ineq - 1; i >= 0; --i) {
2644 if (isl_tab_is_equality(tab, n_eq + i))
2645 isl_basic_map_inequality_to_equality(bmap, i);
2646 else if (isl_tab_is_redundant(tab, n_eq + i))
2647 isl_basic_map_drop_inequality(bmap, i);
2648 }
2649 if (bmap->n_eq != n_eq)
2650 bmap = isl_basic_map_gauss(bmap, NULL((void*)0));
2651 if (!tab->rational &&
2652 bmap && !bmap->sample && isl_tab_sample_is_integer(tab))
2653 bmap->sample = extract_integer_sample(tab);
2654 return bmap;
2655}
2656
2657struct isl_basic_setisl_basic_map *isl_basic_set_update_from_tab(struct isl_basic_setisl_basic_map *bset,
2658 struct isl_tab *tab)
2659{
2660 return bset_from_bmap(isl_basic_map_update_from_tab(bset_to_bmap(bset),
2661 tab));
2662}
2663
2664/* Drop the last constraint added to "tab" in position "r".
2665 * The constraint is expected to have remained in a row.
2666 */
2667static isl_stat drop_last_con_in_row(struct isl_tab *tab, int r)
2668{
2669 if (!tab->con[r].is_row)
2670 isl_die(isl_tab_get_ctx(tab), isl_error_internal,do { isl_handle_error(isl_tab_get_ctx(tab), isl_error_internal
, "row unexpectedly moved to column", "/build/llvm-toolchain-snapshot-7~svn338205/tools/polly/lib/External/isl/isl_tab.c"
, 2672); return isl_stat_error; } while (0)
2671 "row unexpectedly moved to column",do { isl_handle_error(isl_tab_get_ctx(tab), isl_error_internal
, "row unexpectedly moved to column", "/build/llvm-toolchain-snapshot-7~svn338205/tools/polly/lib/External/isl/isl_tab.c"
, 2672); return isl_stat_error; } while (0)
2672 return isl_stat_error)do { isl_handle_error(isl_tab_get_ctx(tab), isl_error_internal
, "row unexpectedly moved to column", "/build/llvm-toolchain-snapshot-7~svn338205/tools/polly/lib/External/isl/isl_tab.c"
, 2672); return isl_stat_error; } while (0)
;
2673 if (r + 1 != tab->n_con)
2674 isl_die(isl_tab_get_ctx(tab), isl_error_internal,do { isl_handle_error(isl_tab_get_ctx(tab), isl_error_internal
, "additional constraints added", "/build/llvm-toolchain-snapshot-7~svn338205/tools/polly/lib/External/isl/isl_tab.c"
, 2675); return isl_stat_error; } while (0)
2675 "additional constraints added", return isl_stat_error)do { isl_handle_error(isl_tab_get_ctx(tab), isl_error_internal
, "additional constraints added", "/build/llvm-toolchain-snapshot-7~svn338205/tools/polly/lib/External/isl/isl_tab.c"
, 2675); return isl_stat_error; } while (0)
;
2676 if (drop_row(tab, tab->con[r].index) < 0)
2677 return isl_stat_error;
2678
2679 return isl_stat_ok;
2680}
2681
2682/* Given a non-negative variable "var", temporarily add a new non-negative
2683 * variable that is the opposite of "var", ensuring that "var" can only attain
2684 * the value zero. The new variable is removed again before this function
2685 * returns. However, the effect of forcing "var" to be zero remains.
2686 * If var = n/d is a row variable, then the new variable = -n/d.
2687 * If var is a column variables, then the new variable = -var.
2688 * If the new variable cannot attain non-negative values, then
2689 * the resulting tableau is empty.
2690 * Otherwise, we know the value will be zero and we close the row.
2691 */
2692static isl_stat cut_to_hyperplane(struct isl_tab *tab, struct isl_tab_var *var)
2693{
2694 unsigned r;
2695 isl_int *row;
2696 int sgn;
2697 unsigned off = 2 + tab->M;
2698
2699 if (var->is_zero)
2700 return isl_stat_ok;
2701 if (var->is_redundant || !var->is_nonneg)
2702 isl_die(isl_tab_get_ctx(tab), isl_error_invalid,do { isl_handle_error(isl_tab_get_ctx(tab), isl_error_invalid
, "expecting non-redundant non-negative variable", "/build/llvm-toolchain-snapshot-7~svn338205/tools/polly/lib/External/isl/isl_tab.c"
, 2704); return isl_stat_error; } while (0)
2703 "expecting non-redundant non-negative variable",do { isl_handle_error(isl_tab_get_ctx(tab), isl_error_invalid
, "expecting non-redundant non-negative variable", "/build/llvm-toolchain-snapshot-7~svn338205/tools/polly/lib/External/isl/isl_tab.c"
, 2704); return isl_stat_error; } while (0)
2704 return isl_stat_error)do { isl_handle_error(isl_tab_get_ctx(tab), isl_error_invalid
, "expecting non-redundant non-negative variable", "/build/llvm-toolchain-snapshot-7~svn338205/tools/polly/lib/External/isl/isl_tab.c"
, 2704); return isl_stat_error; } while (0)
;
2705
2706 if (isl_tab_extend_cons(tab, 1) < 0)
2707 return isl_stat_error;
2708
2709 r = tab->n_con;
2710 tab->con[r].index = tab->n_row;
2711 tab->con[r].is_row = 1;
2712 tab->con[r].is_nonneg = 0;
2713 tab->con[r].is_zero = 0;
2714 tab->con[r].is_redundant = 0;
2715 tab->con[r].frozen = 0;
2716 tab->con[r].negated = 0;
2717 tab->row_var[tab->n_row] = ~r;
2718 row = tab->mat->row[tab->n_row];
2719
2720 if (var->is_row) {
2721 isl_int_set(row[0], tab->mat->row[var->index][0])isl_sioimath_set((row[0]), *(tab->mat->row[var->index
][0]))
;
2722 isl_seq_neg(row + 1,
2723 tab->mat->row[var->index] + 1, 1 + tab->n_col);
2724 } else {
2725 isl_int_set_si(row[0], 1)isl_sioimath_set_si((row[0]), 1);
2726 isl_seq_clr(row + 1, 1 + tab->n_col);
2727 isl_int_set_si(row[off + var->index], -1)isl_sioimath_set_si((row[off + var->index]), -1);
2728 }
2729
2730 tab->n_row++;
2731 tab->n_con++;
2732
2733 sgn = sign_of_max(tab, &tab->con[r]);
2734 if (sgn < -1)
2735 return isl_stat_error;
2736 if (sgn < 0) {
2737 if (drop_last_con_in_row(tab, r) < 0)
2738 return isl_stat_error;
2739 if (isl_tab_mark_empty(tab) < 0)
2740 return isl_stat_error;
2741 return isl_stat_ok;
2742 }
2743 tab->con[r].is_nonneg = 1;
2744 /* sgn == 0 */
2745 if (close_row(tab, &tab->con[r], 1) < 0)
2746 return isl_stat_error;
2747 if (drop_last_con_in_row(tab, r) < 0)
2748 return isl_stat_error;
2749
2750 return isl_stat_ok;
2751}
2752
2753/* Given a tableau "tab" and an inequality constraint "con" of the tableau,
2754 * relax the inequality by one. That is, the inequality r >= 0 is replaced
2755 * by r' = r + 1 >= 0.
2756 * If r is a row variable, we simply increase the constant term by one
2757 * (taking into account the denominator).
2758 * If r is a column variable, then we need to modify each row that
2759 * refers to r = r' - 1 by substituting this equality, effectively
2760 * subtracting the coefficient of the column from the constant.
2761 * We should only do this if the minimum is manifestly unbounded,
2762 * however. Otherwise, we may end up with negative sample values
2763 * for non-negative variables.
2764 * So, if r is a column variable with a minimum that is not
2765 * manifestly unbounded, then we need to move it to a row.
2766 * However, the sample value of this row may be negative,
2767 * even after the relaxation, so we need to restore it.
2768 * We therefore prefer to pivot a column up to a row, if possible.
2769 */
2770int isl_tab_relax(struct isl_tab *tab, int con)
2771{
2772 struct isl_tab_var *var;
2773
2774 if (!tab)
2775 return -1;
2776
2777 var = &tab->con[con];
2778
2779 if (var->is_row && (var->index < 0 || var->index < tab->n_redundant))
2780 isl_die(tab->mat->ctx, isl_error_invalid,do { isl_handle_error(tab->mat->ctx, isl_error_invalid,
"cannot relax redundant constraint", "/build/llvm-toolchain-snapshot-7~svn338205/tools/polly/lib/External/isl/isl_tab.c"
, 2781); return -1; } while (0)
2781 "cannot relax redundant constraint", return -1)do { isl_handle_error(tab->mat->ctx, isl_error_invalid,
"cannot relax redundant constraint", "/build/llvm-toolchain-snapshot-7~svn338205/tools/polly/lib/External/isl/isl_tab.c"
, 2781); return -1; } while (0)
;
2782 if (!var->is_row && (var->index < 0 || var->index < tab->n_dead))
2783 isl_die(tab->mat->ctx, isl_error_invalid,do { isl_handle_error(tab->mat->ctx, isl_error_invalid,
"cannot relax dead constraint", "/build/llvm-toolchain-snapshot-7~svn338205/tools/polly/lib/External/isl/isl_tab.c"
, 2784); return -1; } while (0)
2784 "cannot relax dead constraint", return -1)do { isl_handle_error(tab->mat->ctx, isl_error_invalid,
"cannot relax dead constraint", "/build/llvm-toolchain-snapshot-7~svn338205/tools/polly/lib/External/isl/isl_tab.c"
, 2784); return -1; } while (0)
;
2785
2786 if (!var->is_row && !max_is_manifestly_unbounded(tab, var))
2787 if (to_row(tab, var, 1) < 0)
2788 return -1;
2789 if (!var->is_row && !min_is_manifestly_unbounded(tab, var))
2790 if (to_row(tab, var, -1) < 0)
2791 return -1;
2792
2793 if (var->is_row) {
2794 isl_int_add(tab->mat->row[var->index][1],isl_sioimath_add((tab->mat->row[var->index][1]), *(tab
->mat->row[var->index][1]), *(tab->mat->row[var
->index][0]))
2795 tab->mat->row[var->index][1], tab->mat->row[var->index][0])isl_sioimath_add((tab->mat->row[var->index][1]), *(tab
->mat->row[var->index][1]), *(tab->mat->row[var
->index][0]))
;
2796 if (restore_row(tab, var) < 0)
2797 return -1;
2798 } else {
2799 int i;
2800 unsigned off = 2 + tab->M;
2801
2802 for (i = 0; i < tab->n_row; ++i) {
2803 if (isl_int_is_zero(tab->mat->row[i][off + var->index])(isl_sioimath_sgn(*(tab->mat->row[i][off + var->index
])) == 0)
)
2804 continue;
2805 isl_int_sub(tab->mat->row[i][1], tab->mat->row[i][1],isl_sioimath_sub((tab->mat->row[i][1]), *(tab->mat->
row[i][1]), *(tab->mat->row[i][off + var->index]))
2806 tab->mat->row[i][off + var->index])isl_sioimath_sub((tab->mat->row[i][1]), *(tab->mat->
row[i][1]), *(tab->mat->row[i][off + var->index]))
;
2807 }
2808
2809 }
2810
2811 if (isl_tab_push_var(tab, isl_tab_undo_relax, var) < 0)
2812 return -1;
2813
2814 return 0;
2815}
2816
2817/* Replace the variable v at position "pos" in the tableau "tab"
2818 * by v' = v + shift.
2819 *
2820 * If the variable is in a column, then we first check if we can
2821 * simply plug in v = v' - shift. The effect on a row with
2822 * coefficient f/d for variable v is that the constant term c/d
2823 * is replaced by (c - f * shift)/d. If shift is positive and
2824 * f is negative for each row that needs to remain non-negative,
2825 * then this is clearly safe. In other words, if the minimum of v
2826 * is manifestly unbounded, then we can keep v in a column position.
2827 * Otherwise, we can pivot it down to a row.
2828 * Similarly, if shift is negative, we need to check if the maximum
2829 * of is manifestly unbounded.
2830 *
2831 * If the variable is in a row (from the start or after pivoting),
2832 * then the constant term c/d is replaced by (c + d * shift)/d.
2833 */
2834int isl_tab_shift_var(struct isl_tab *tab, int pos, isl_int shift)
2835{
2836 struct isl_tab_var *var;
2837
2838 if (!tab)
2839 return -1;
2840 if (isl_int_is_zero(shift)(isl_sioimath_sgn(*(shift)) == 0))
2841 return 0;
2842
2843 var = &tab->var[pos];
2844 if (!var->is_row) {
2845 if (isl_int_is_neg(shift)(isl_sioimath_sgn(*(shift)) < 0)) {
2846 if (!max_is_manifestly_unbounded(tab, var))
2847 if (to_row(tab, var, 1) < 0)
2848 return -1;
2849 } else {
2850 if (!min_is_manifestly_unbounded(tab, var))
2851 if (to_row(tab, var, -1) < 0)
2852 return -1;
2853 }
2854 }
2855
2856 if (var->is_row) {
2857 isl_int_addmul(tab->mat->row[var->index][1],isl_sioimath_addmul((tab->mat->row[var->index][1]), *
(shift), *(tab->mat->row[var->index][0]))
2858 shift, tab->mat->row[var->index][0])isl_sioimath_addmul((tab->mat->row[var->index][1]), *
(shift), *(tab->mat->row[var->index][0]))
;
2859 } else {
2860 int i;
2861 unsigned off = 2 + tab->M;
2862
2863 for (i = 0; i < tab->n_row; ++i) {
2864 if (isl_int_is_zero(tab->mat->row[i][off + var->index])(isl_sioimath_sgn(*(tab->mat->row[i][off + var->index
])) == 0)
)
2865 continue;
2866 isl_int_submul(tab->mat->row[i][1],isl_sioimath_submul((tab->mat->row[i][1]), *(shift), *(
tab->mat->row[i][off + var->index]))
2867 shift, tab->mat->row[i][off + var->index])isl_sioimath_submul((tab->mat->row[i][1]), *(shift), *(
tab->mat->row[i][off + var->index]))
;
2868 }
2869
2870 }
2871
2872 return 0;
2873}
2874
2875/* Remove the sign constraint from constraint "con".
2876 *
2877 * If the constraint variable was originally marked non-negative,
2878 * then we make sure we mark it non-negative again during rollback.
2879 */
2880int isl_tab_unrestrict(struct isl_tab *tab, int con)
2881{
2882 struct isl_tab_var *var;
2883
2884 if (!tab)
2885 return -1;
2886
2887 var = &tab->con[con];
2888 if (!var->is_nonneg)
2889 return 0;
2890
2891 var->is_nonneg = 0;
2892 if (isl_tab_push_var(tab, isl_tab_undo_unrestrict, var) < 0)
2893 return -1;
2894
2895 return 0;
2896}
2897
2898int isl_tab_select_facet(struct isl_tab *tab, int con)
2899{
2900 if (!tab)
2901 return -1;
2902
2903 return cut_to_hyperplane(tab, &tab->con[con]);
2904}
2905
2906static int may_be_equality(struct isl_tab *tab, int row)
2907{
2908 return tab->rational ? isl_int_is_zero(tab->mat->row[row][1])(isl_sioimath_sgn(*(tab->mat->row[row][1])) == 0)
2909 : isl_int_lt(tab->mat->row[row][1],(isl_sioimath_cmp(*(tab->mat->row[row][1]), *(tab->mat
->row[row][0])) < 0)
2910 tab->mat->row[row][0])(isl_sioimath_cmp(*(tab->mat->row[row][1]), *(tab->mat
->row[row][0])) < 0)
;
2911}
2912
2913/* Return an isl_tab_var that has been marked or NULL if no such
2914 * variable can be found.
2915 * The marked field has only been set for variables that
2916 * appear in non-redundant rows or non-dead columns.
2917 *
2918 * Pick the last constraint variable that is marked and
2919 * that appears in either a non-redundant row or a non-dead columns.
2920 * Since the returned variable is tested for being a redundant constraint or
2921 * an implicit equality, there is no need to return any tab variable that
2922 * corresponds to a variable.
2923 */
2924static struct isl_tab_var *select_marked(struct isl_tab *tab)
2925{
2926 int i;
2927 struct isl_tab_var *var;
2928
2929 for (i = tab->n_con - 1; i >= 0; --i) {
2930 var = &tab->con[i];
2931 if (var->index < 0)
2932 continue;
2933 if (var->is_row && var->index < tab->n_redundant)
2934 continue;
2935 if (!var->is_row && var->index < tab->n_dead)
2936 continue;
2937 if (var->marked)
2938 return var;
2939 }
2940
2941 return NULL((void*)0);
2942}
2943
2944/* Check for (near) equalities among the constraints.
2945 * A constraint is an equality if it is non-negative and if
2946 * its maximal value is either
2947 * - zero (in case of rational tableaus), or
2948 * - strictly less than 1 (in case of integer tableaus)
2949 *
2950 * We first mark all non-redundant and non-dead variables that
2951 * are not frozen and not obviously not an equality.
2952 * Then we iterate over all marked variables if they can attain
2953 * any values larger than zero or at least one.
2954 * If the maximal value is zero, we mark any column variables
2955 * that appear in the row as being zero and mark the row as being redundant.
2956 * Otherwise, if the maximal value is strictly less than one (and the
2957 * tableau is integer), then we restrict the value to being zero
2958 * by adding an opposite non-negative variable.
2959 * The order in which the variables are considered is not important.
2960 */
2961int isl_tab_detect_implicit_equalities(struct isl_tab *tab)
2962{
2963 int i;
2964 unsigned n_marked;
2965
2966 if (!tab)
2967 return -1;
2968 if (tab->empty)
2969 return 0;
2970 if (tab->n_dead == tab->n_col)
2971 return 0;
2972
2973 n_marked = 0;
2974 for (i = tab->n_redundant; i < tab->n_row; ++i) {
2975 struct isl_tab_var *var = isl_tab_var_from_row(tab, i);
2976 var->marked = !var->frozen && var->is_nonneg &&
2977 may_be_equality(tab, i);
2978 if (var->marked)
2979 n_marked++;
2980 }
2981 for (i = tab->n_dead; i < tab->n_col; ++i) {
2982 struct isl_tab_var *var = var_from_col(tab, i);
2983 var->marked = !var->frozen && var->is_nonneg;
2984 if (var->marked)
2985 n_marked++;
2986 }
2987 while (n_marked) {
2988 struct isl_tab_var *var;
2989 int sgn;
2990 var = select_marked(tab);
2991 if (!var)
2992 break;
2993 var->marked = 0;
2994 n_marked--;
2995 sgn = sign_of_max(tab, var);
2996 if (sgn < 0)
2997 return -1;
2998 if (sgn == 0) {
2999 if (close_row(tab, var, 0) < 0)
3000 return -1;
3001 } else if (!tab->rational && !at_least_one(tab, var)) {
3002 if (cut_to_hyperplane(tab, var) < 0)
3003 return -1;
3004 return isl_tab_detect_implicit_equalities(tab);
3005 }
3006 for (i = tab->n_redundant; i < tab->n_row; ++i) {
3007 var = isl_tab_var_from_row(tab, i);
3008 if (!var->marked)
3009 continue;
3010 if (may_be_equality(tab, i))
3011 continue;
3012 var->marked = 0;
3013 n_marked--;
3014 }
3015 }
3016
3017 return 0;
3018}
3019
3020/* Update the element of row_var or col_var that corresponds to
3021 * constraint tab->con[i] to a move from position "old" to position "i".
3022 */
3023static int update_con_after_move(struct isl_tab *tab, int i, int old)
3024{
3025 int *p;
3026 int index;
3027
3028 index = tab->con[i].index;
3029 if (index == -1)
3030 return 0;
3031 p = tab->con[i].is_row ? tab->row_var : tab->col_var;
3032 if (p[index] != ~old)
3033 isl_die(tab->mat->ctx, isl_error_internal,do { isl_handle_error(tab->mat->ctx, isl_error_internal
, "broken internal state", "/build/llvm-toolchain-snapshot-7~svn338205/tools/polly/lib/External/isl/isl_tab.c"
, 3034); return -1; } while (0)
3034 "broken internal state", return -1)do { isl_handle_error(tab->mat->ctx, isl_error_internal
, "broken internal state", "/build/llvm-toolchain-snapshot-7~svn338205/tools/polly/lib/External/isl/isl_tab.c"
, 3034); return -1; } while (0)
;
3035 p[index] = ~i;
3036
3037 return 0;
3038}
3039
3040/* Rotate the "n" constraints starting at "first" to the right,
3041 * putting the last constraint in the position of the first constraint.
3042 */
3043static int rotate_constraints(struct isl_tab *tab, int first, int n)
3044{
3045 int i, last;
3046 struct isl_tab_var var;
3047
3048 if (n <= 1)
3049 return 0;
3050
3051 last = first + n - 1;
3052 var = tab->con[last];
3053 for (i = last; i > first; --i) {
3054 tab->con[i] = tab->con[i - 1];
3055 if (update_con_after_move(tab, i, i - 1) < 0)
3056 return -1;
3057 }
3058 tab->con[first] = var;
3059 if (update_con_after_move(tab, first, last) < 0)
3060 return -1;
3061
3062 return 0;
3063}
3064
3065/* Make the equalities that are implicit in "bmap" but that have been
3066 * detected in the corresponding "tab" explicit in "bmap" and update
3067 * "tab" to reflect the new order of the constraints.
3068 *
3069 * In particular, if inequality i is an implicit equality then
3070 * isl_basic_map_inequality_to_equality will move the inequality
3071 * in front of the other equality and it will move the last inequality
3072 * in the position of inequality i.
3073 * In the tableau, the inequalities of "bmap" are stored after the equalities
3074 * and so the original order
3075 *
3076 * E E E E E A A A I B B B B L
3077 *
3078 * is changed into
3079 *
3080 * I E E E E E A A A L B B B B
3081 *
3082 * where I is the implicit equality, the E are equalities,
3083 * the A inequalities before I, the B inequalities after I and
3084 * L the last inequality.
3085 * We therefore need to rotate to the right two sets of constraints,
3086 * those up to and including I and those after I.
3087 *
3088 * If "tab" contains any constraints that are not in "bmap" then they
3089 * appear after those in "bmap" and they should be left untouched.
3090 *
3091 * Note that this function leaves "bmap" in a temporary state
3092 * as it does not call isl_basic_map_gauss. Calling this function
3093 * is the responsibility of the caller.
3094 */
3095__isl_give isl_basic_map *isl_tab_make_equalities_explicit(struct isl_tab *tab,
3096 __isl_take isl_basic_map *bmap)
3097{
3098 int i;
3099
3100 if (!tab || !bmap)
3101 return isl_basic_map_free(bmap);
3102 if (tab->empty)
3103 return bmap;
3104
3105 for (i = bmap->n_ineq - 1; i >= 0; --i) {
3106 if (!isl_tab_is_equality(tab, bmap->n_eq + i))
3107 continue;
3108 isl_basic_map_inequality_to_equality(bmap, i);
3109 if (rotate_constraints(tab, 0, tab->n_eq + i + 1) < 0)
3110 return isl_basic_map_free(bmap);
3111 if (rotate_constraints(tab, tab->n_eq + i + 1,
3112 bmap->n_ineq - i) < 0)
3113 return isl_basic_map_free(bmap);
3114 tab->n_eq++;
3115 }
3116
3117 return bmap;
3118}
3119
3120static int con_is_redundant(struct isl_tab *tab, struct isl_tab_var *var)
3121{
3122 if (!tab)
3123 return -1;
3124 if (tab->rational) {
3125 int sgn = sign_of_min(tab, var);
3126 if (sgn < -1)
3127 return -1;
3128 return sgn >= 0;
3129 } else {
3130 int irred = isl_tab_min_at_most_neg_one(tab, var);
3131 if (irred < 0)
3132 return -1;
3133 return !irred;
3134 }
3135}
3136
3137/* Check for (near) redundant constraints.
3138 * A constraint is redundant if it is non-negative and if
3139 * its minimal value (temporarily ignoring the non-negativity) is either
3140 * - zero (in case of rational tableaus), or
3141 * - strictly larger than -1 (in case of integer tableaus)
3142 *
3143 * We first mark all non-redundant and non-dead variables that
3144 * are not frozen and not obviously negatively unbounded.
3145 * Then we iterate over all marked variables if they can attain
3146 * any values smaller than zero or at most negative one.
3147 * If not, we mark the row as being redundant (assuming it hasn't
3148 * been detected as being obviously redundant in the mean time).
3149 */
3150int isl_tab_detect_redundant(struct isl_tab *tab)
3151{
3152 int i;
3153 unsigned n_marked;
3154
3155 if (!tab)
3156 return -1;
3157 if (tab->empty)
3158 return 0;
3159 if (tab->n_redundant == tab->n_row)
3160 return 0;
3161
3162 n_marked = 0;
3163 for (i = tab->n_redundant; i < tab->n_row; ++i) {
3164 struct isl_tab_var *var = isl_tab_var_from_row(tab, i);
3165 var->marked = !var->frozen && var->is_nonneg;
3166 if (var->marked)
3167 n_marked++;
3168 }
3169 for (i = tab->n_dead; i < tab->n_col; ++i) {
3170 struct isl_tab_var *var = var_from_col(tab, i);
3171 var->marked = !var->frozen && var->is_nonneg &&
3172 !min_is_manifestly_unbounded(tab, var);
3173 if (var->marked)
3174 n_marked++;
3175 }
3176 while (n_marked) {
3177 struct isl_tab_var *var;
3178 int red;
3179 var = select_marked(tab);
3180 if (!var)
3181 break;
3182 var->marked = 0;
3183 n_marked--;
3184 red = con_is_redundant(tab, var);
3185 if (red < 0)
3186 return -1;
3187 if (red && !var->is_redundant)
3188 if (isl_tab_mark_redundant(tab, var->index) < 0)
3189 return -1;
3190 for (i = tab->n_dead; i < tab->n_col; ++i) {
3191 var = var_from_col(tab, i);
3192 if (!var->marked)
3193 continue;
3194 if (!min_is_manifestly_unbounded(tab, var))
3195 continue;
3196 var->marked = 0;
3197 n_marked--;
3198 }
3199 }
3200
3201 return 0;
3202}
3203
3204int isl_tab_is_equality(struct isl_tab *tab, int con)
3205{
3206 int row;
3207 unsigned off;
3208
3209 if (!tab)
3210 return -1;
3211 if (tab->con[con].is_zero)
3212 return 1;
3213 if (tab->con[con].is_redundant)
3214 return 0;
3215 if (!tab->con[con].is_row)
3216 return tab->con[con].index < tab->n_dead;
3217
3218 row = tab->con[con].index;
3219
3220 off = 2 + tab->M;
3221 return isl_int_is_zero(tab->mat->row[row][1])(isl_sioimath_sgn(*(tab->mat->row[row][1])) == 0) &&
3222 !row_is_big(tab, row) &&
3223 isl_seq_first_non_zero(tab->mat->row[row] + off + tab->n_dead,
3224 tab->n_col - tab->n_dead) == -1;
3225}
3226
3227/* Return the minimal value of the affine expression "f" with denominator
3228 * "denom" in *opt, *opt_denom, assuming the tableau is not empty and
3229 * the expression cannot attain arbitrarily small values.
3230 * If opt_denom is NULL, then *opt is rounded up to the nearest integer.
3231 * The return value reflects the nature of the result (empty, unbounded,
3232 * minimal value returned in *opt).
3233 *
3234 * This function assumes that at least one more row and at least
3235 * one more element in the constraint array are available in the tableau.
3236 */
3237enum isl_lp_result isl_tab_min(struct isl_tab *tab,
3238 isl_int *f, isl_int denom, isl_int *opt, isl_int *opt_denom,
3239 unsigned flags)
3240{
3241 int r;
3242 enum isl_lp_result res = isl_lp_ok;
3243 struct isl_tab_var *var;
3244 struct isl_tab_undo *snap;
3245
3246 if (!tab)
3247 return isl_lp_error;
3248
3249 if (tab->empty)
3250 return isl_lp_empty;
3251
3252 snap = isl_tab_snap(tab);
3253 r = isl_tab_add_row(tab, f);
3254 if (r < 0)
3255 return isl_lp_error;
3256 var = &tab->con[r];
3257 for (;;) {
3258 int row, col;
3259 find_pivot(tab, var, var, -1, &row, &col);
3260 if (row == var->index) {
3261 res = isl_lp_unbounded;
3262 break;
3263 }
3264 if (row == -1)
3265 break;
3266 if (isl_tab_pivot(tab, row, col) < 0)
3267 return isl_lp_error;
3268 }
3269 isl_int_mul(tab->mat->row[var->index][0],isl_sioimath_mul((tab->mat->row[var->index][0]), *(tab
->mat->row[var->index][0]), *(denom))
3270 tab->mat->row[var->index][0], denom)isl_sioimath_mul((tab->mat->row[var->index][0]), *(tab
->mat->row[var->index][0]), *(denom))
;
3271 if (ISL_FL_ISSET(flags, ISL_TAB_SAVE_DUAL)(!!((flags) & ((1 << 0))))) {
3272 int i;
3273
3274 isl_vec_free(tab->dual);
3275 tab->dual = isl_vec_alloc(tab->mat->ctx, 1 + tab->n_con);
3276 if (!tab->dual)
3277 return isl_lp_error;
3278 isl_int_set(tab->dual->el[0], tab->mat->row[var->index][0])isl_sioimath_set((tab->dual->el[0]), *(tab->mat->
row[var->index][0]))
;
3279 for (i = 0; i < tab->n_con; ++i) {
3280 int pos;
3281 if (tab->con[i].is_row) {
3282 isl_int_set_si(tab->dual->el[1 + i], 0)isl_sioimath_set_si((tab->dual->el[1 + i]), 0);
3283 continue;
3284 }
3285 pos = 2 + tab->M + tab->con[i].index;
3286 if (tab->con[i].negated)
3287 isl_int_neg(tab->dual->el[1 + i],isl_sioimath_neg((tab->dual->el[1 + i]), *(tab->mat->
row[var->index][pos]))
3288 tab->mat->row[var->index][pos])isl_sioimath_neg((tab->dual->el[1 + i]), *(tab->mat->
row[var->index][pos]))
;
3289 else
3290 isl_int_set(tab->dual->el[1 + i],isl_sioimath_set((tab->dual->el[1 + i]), *(tab->mat->
row[var->index][pos]))
3291 tab->mat->row[var->index][pos])isl_sioimath_set((tab->dual->el[1 + i]), *(tab->mat->
row[var->index][pos]))
;
3292 }
3293 }
3294 if (opt && res == isl_lp_ok) {
3295 if (opt_denom) {
3296 isl_int_set(*opt, tab->mat->row[var->index][1])isl_sioimath_set((*opt), *(tab->mat->row[var->index]
[1]))
;
3297 isl_int_set(*opt_denom, tab->mat->row[var->index][0])isl_sioimath_set((*opt_denom), *(tab->mat->row[var->
index][0]))
;
3298 } else
3299 get_rounded_sample_value(tab, var, 1, opt);
3300 }
3301 if (isl_tab_rollback(tab, snap) < 0)
3302 return isl_lp_error;
3303 return res;
3304}
3305
3306/* Is the constraint at position "con" marked as being redundant?
3307 * If it is marked as representing an equality, then it is not
3308 * considered to be redundant.
3309 * Note that isl_tab_mark_redundant marks both the isl_tab_var as
3310 * redundant and moves the corresponding row into the first
3311 * tab->n_redundant positions (or removes the row, assigning it index -1),
3312 * so the final test is actually redundant itself.
3313 */
3314int isl_tab_is_redundant(struct isl_tab *tab, int con)
3315{
3316 if (!tab)
3317 return -1;
3318 if (con < 0 || con >= tab->n_con)
3319 isl_die(isl_tab_get_ctx(tab), isl_error_invalid,do { isl_handle_error(isl_tab_get_ctx(tab), isl_error_invalid
, "position out of bounds", "/build/llvm-toolchain-snapshot-7~svn338205/tools/polly/lib/External/isl/isl_tab.c"
, 3320); return -1; } while (0)
3320 "position out of bounds", return -1)do { isl_handle_error(isl_tab_get_ctx(tab), isl_error_invalid
, "position out of bounds", "/build/llvm-toolchain-snapshot-7~svn338205/tools/polly/lib/External/isl/isl_tab.c"
, 3320); return -1; } while (0)
;
3321 if (tab->con[con].is_zero)
3322 return 0;
3323 if (tab->con[con].is_redundant)
3324 return 1;
3325 return tab->con[con].is_row && tab->con[con].index < tab->n_redundant;
3326}
3327
3328/* Is variable "var" of "tab" fixed to a constant value by its row
3329 * in the tableau?
3330 * If so and if "value" is not NULL, then store this constant value
3331 * in "value".
3332 *
3333 * That is, is it a row variable that only has non-zero coefficients
3334 * for dead columns?
3335 */
3336static isl_bool is_constant(struct isl_tab *tab, struct isl_tab_var *var,
3337 isl_int *value)
3338{
3339 unsigned off = 2 + tab->M;
3340 isl_mat *mat = tab->mat;
3341 int n;
3342 int row;
3343 int pos;
3344
3345 if (!var->is_row)
3346 return isl_bool_false;
3347 row = var->index;
3348 if (row_is_big(tab, row))
3349 return isl_bool_false;
3350 n = tab->n_col - tab->n_dead;
3351 pos = isl_seq_first_non_zero(mat->row[row] + off + tab->n_dead, n);
3352 if (pos != -1)
3353 return isl_bool_false;
3354 if (value)
3355 isl_int_divexact(*value, mat->row[row][1], mat->row[row][0])isl_sioimath_tdiv_q((*value), *(mat->row[row][1]), *(mat->
row[row][0]))
;
3356 return isl_bool_true;
3357}
3358
3359/* Has the variable "var' of "tab" reached a value that is greater than
3360 * or equal (if sgn > 0) or smaller than or equal (if sgn < 0) to "target"?
3361 * "tmp" has been initialized by the caller and can be used
3362 * to perform local computations.
3363 *
3364 * If the sample value involves the big parameter, then any value
3365 * is reached.
3366 * Otherwise check if n/d >= t, i.e., n >= d * t (if sgn > 0)
3367 * or n/d <= t, i.e., n <= d * t (if sgn < 0).
3368 */
3369static int reached(struct isl_tab *tab, struct isl_tab_var *var, int sgn,
3370 isl_int target, isl_int *tmp)
3371{
3372 if (row_is_big(tab, var->index))
3373 return 1;
3374 isl_int_mul(*tmp, tab->mat->row[var->index][0], target)isl_sioimath_mul((*tmp), *(tab->mat->row[var->index]
[0]), *(target))
;
3375 if (sgn > 0)
3376 return isl_int_ge(tab->mat->row[var->index][1], *tmp)(isl_sioimath_cmp(*(tab->mat->row[var->index][1]), *
(*tmp)) >= 0)
;
3377 else
3378 return isl_int_le(tab->mat->row[var->index][1], *tmp)(isl_sioimath_cmp(*(tab->mat->row[var->index][1]), *
(*tmp)) <= 0)
;
3379}
3380
3381/* Can variable "var" of "tab" attain the value "target" by
3382 * pivoting up (if sgn > 0) or down (if sgn < 0)?
3383 * If not, then pivot up [down] to the greatest [smallest]
3384 * rational value.
3385 * "tmp" has been initialized by the caller and can be used
3386 * to perform local computations.
3387 *
3388 * If the variable is manifestly unbounded in the desired direction,
3389 * then it can attain any value.
3390 * Otherwise, it can be moved to a row.
3391 * Continue pivoting until the target is reached.
3392 * If no more pivoting can be performed, the maximal [minimal]
3393 * rational value has been reached and the target cannot be reached.
3394 * If the variable would be pivoted into a manifestly unbounded column,
3395 * then the target can be reached.
3396 */
3397static isl_bool var_reaches(struct isl_tab *tab, struct isl_tab_var *var,
3398 int sgn, isl_int target, isl_int *tmp)
3399{
3400 int row, col;
3401
3402 if (sgn < 0 && min_is_manifestly_unbounded(tab, var))
3403 return isl_bool_true;
3404 if (sgn > 0 && max_is_manifestly_unbounded(tab, var))
3405 return isl_bool_true;
3406 if (to_row(tab, var, sgn) < 0)
3407 return isl_bool_error;
3408 while (!reached(tab, var, sgn, target, tmp)) {
3409 find_pivot(tab, var, var, sgn, &row, &col);
3410 if (row == -1)
3411 return isl_bool_false;
3412 if (row == var->index)
3413 return isl_bool_true;
3414 if (isl_tab_pivot(tab, row, col) < 0)
3415 return isl_bool_error;
3416 }
3417
3418 return isl_bool_true;
3419}
3420
3421/* Check if variable "var" of "tab" can only attain a single (integer)
3422 * value, and, if so, add an equality constraint to fix the variable
3423 * to this single value and store the result in "target".
3424 * "target" and "tmp" have been initialized by the caller.
3425 *
3426 * Given the current sample value, round it down and check
3427 * whether it is possible to attain a strictly smaller integer value.
3428 * If so, the variable is not restricted to a single integer value.
3429 * Otherwise, the search stops at the smallest rational value.
3430 * Round up this value and check whether it is possible to attain
3431 * a strictly greater integer value.
3432 * If so, the variable is not restricted to a single integer value.
3433 * Otherwise, the search stops at the greatest rational value.
3434 * If rounding down this value yields a value that is different
3435 * from rounding up the smallest rational value, then the variable
3436 * cannot attain any integer value. Mark the tableau empty.
3437 * Otherwise, add an equality constraint that fixes the variable
3438 * to the single integer value found.
3439 */
3440static isl_bool detect_constant_with_tmp(struct isl_tab *tab,
3441 struct isl_tab_var *var, isl_int *target, isl_int *tmp)
3442{
3443 isl_bool reached;
3444 isl_vec *eq;
3445 int pos;
3446 isl_stat r;
3447
3448 get_rounded_sample_value(tab, var, -1, target);
3449 isl_int_sub_ui(*target, *target, 1)isl_sioimath_sub_ui((*target), *(*target), 1);
3450 reached = var_reaches(tab, var, -1, *target, tmp);
3451 if (reached < 0 || reached)
3452 return isl_bool_not(reached);
3453 get_rounded_sample_value(tab, var, 1, target);
3454 isl_int_add_ui(*target, *target, 1)isl_sioimath_add_ui((*target), *(*target), 1);
3455 reached = var_reaches(tab, var, 1, *target, tmp);
3456 if (reached < 0 || reached)
3457 return isl_bool_not(reached);
3458 get_rounded_sample_value(tab, var, -1, tmp);
3459 isl_int_sub_ui(*target, *target, 1)isl_sioimath_sub_ui((*target), *(*target), 1);
3460 if (isl_int_ne(*target, *tmp)(isl_sioimath_cmp(*(*target), *(*tmp)) != 0)) {
3461 if (isl_tab_mark_empty(tab) < 0)
3462 return isl_bool_error;
3463 return isl_bool_false;
3464 }
3465
3466 if (isl_tab_extend_cons(tab, 1) < 0)
3467 return isl_bool_error;
3468 eq = isl_vec_alloc(isl_tab_get_ctx(tab), 1 + tab->n_var);
3469 if (!eq)
3470 return isl_bool_error;
3471 pos = var - tab->var;
3472 isl_seq_clr(eq->el + 1, tab->n_var);
3473 isl_int_set_si(eq->el[1 + pos], -1)isl_sioimath_set_si((eq->el[1 + pos]), -1);
3474 isl_int_set(eq->el[0], *target)isl_sioimath_set((eq->el[0]), *(*target));
3475 r = isl_tab_add_eq(tab, eq->el);
3476 isl_vec_free(eq);
3477
3478 return r < 0 ? isl_bool_error : isl_bool_true;
3479}
3480
3481/* Check if variable "var" of "tab" can only attain a single (integer)
3482 * value, and, if so, add an equality constraint to fix the variable
3483 * to this single value and store the result in "value" (if "value"
3484 * is not NULL).
3485 *
3486 * If the current sample value involves the big parameter,
3487 * then the variable cannot have a fixed integer value.
3488 * If the variable is already fixed to a single value by its row, then
3489 * there is no need to add another equality constraint.
3490 *
3491 * Otherwise, allocate some temporary variables and continue
3492 * with detect_constant_with_tmp.
3493 */
3494static isl_bool get_constant(struct isl_tab *tab, struct isl_tab_var *var,
3495 isl_int *value)
3496{
3497 isl_int target, tmp;
3498 isl_bool is_cst;
3499
3500 if (var->is_row && row_is_big(tab, var->index))
3501 return isl_bool_false;
3502 is_cst = is_constant(tab, var, value);
3503 if (is_cst < 0 || is_cst)
3504 return is_cst;
3505
3506 if (!value)
3507 isl_int_init(target)isl_sioimath_init((target));
3508 isl_int_init(tmp)isl_sioimath_init((tmp));
3509
3510 is_cst = detect_constant_with_tmp(tab, var,
3511 value ? value : &target, &tmp);
3512
3513 isl_int_clear(tmp)isl_sioimath_clear((tmp));
3514 if (!value)
3515 isl_int_clear(target)isl_sioimath_clear((target));
3516
3517 return is_cst;
3518}
3519
3520/* Check if variable "var" of "tab" can only attain a single (integer)
3521 * value, and, if so, add an equality constraint to fix the variable
3522 * to this single value and store the result in "value" (if "value"
3523 * is not NULL).
3524 *
3525 * For rational tableaus, nothing needs to be done.
3526 */
3527isl_bool isl_tab_is_constant(struct isl_tab *tab, int var, isl_int *value)
3528{
3529 if (!tab)
3530 return isl_bool_error;
3531 if (var < 0 || var >= tab->n_var)
3532 isl_die(isl_tab_get_ctx(tab), isl_error_invalid,do { isl_handle_error(isl_tab_get_ctx(tab), isl_error_invalid
, "position out of bounds", "/build/llvm-toolchain-snapshot-7~svn338205/tools/polly/lib/External/isl/isl_tab.c"
, 3533); return isl_bool_error; } while (0)
3533 "position out of bounds", return isl_bool_error)do { isl_handle_error(isl_tab_get_ctx(tab), isl_error_invalid
, "position out of bounds", "/build/llvm-toolchain-snapshot-7~svn338205/tools/polly/lib/External/isl/isl_tab.c"
, 3533); return isl_bool_error; } while (0)
;
3534 if (tab->rational)
3535 return isl_bool_false;
3536
3537 return get_constant(tab, &tab->var[var], value);
3538}
3539
3540/* Check if any of the variables of "tab" can only attain a single (integer)
3541 * value, and, if so, add equality constraints to fix those variables
3542 * to these single values.
3543 *
3544 * For rational tableaus, nothing needs to be done.
3545 */
3546isl_stat isl_tab_detect_constants(struct isl_tab *tab)
3547{
3548 int i;
3549
3550 if (!tab)
3551 return isl_stat_error;
3552 if (tab->rational)
3553 return isl_stat_ok;
3554
3555 for (i = 0; i < tab->n_var; ++i) {
3556 if (get_constant(tab, &tab->var[i], NULL((void*)0)) < 0)
3557 return isl_stat_error;
3558 }
3559
3560 return isl_stat_ok;
3561}
3562
3563/* Take a snapshot of the tableau that can be restored by a call to
3564 * isl_tab_rollback.
3565 */
3566struct isl_tab_undo *isl_tab_snap(struct isl_tab *tab)
3567{
3568 if (!tab)
3569 return NULL((void*)0);
3570 tab->need_undo = 1;
3571 return tab->top;
3572}
3573
3574/* Does "tab" need to keep track of undo information?
3575 * That is, was a snapshot taken that may need to be restored?
3576 */
3577isl_bool isl_tab_need_undo(struct isl_tab *tab)
3578{
3579 if (!tab)
3580 return isl_bool_error;
3581
3582 return tab->need_undo;
3583}
3584
3585/* Remove all tracking of undo information from "tab", invalidating
3586 * any snapshots that may have been taken of the tableau.
3587 * Since all snapshots have been invalidated, there is also
3588 * no need to start keeping track of undo information again.
3589 */
3590void isl_tab_clear_undo(struct isl_tab *tab)
3591{
3592 if (!tab)
3593 return;
3594
3595 free_undo(tab);
3596 tab->need_undo = 0;
3597}
3598
3599/* Undo the operation performed by isl_tab_relax.
3600 */
3601static isl_stat unrelax(struct isl_tab *tab, struct isl_tab_var *var)
3602 WARN_UNUSED__attribute__((__warn_unused_result__));
3603static isl_stat unrelax(struct isl_tab *tab, struct isl_tab_var *var)
3604{
3605 unsigned off = 2 + tab->M;
3606
3607 if (!var->is_row && !max_is_manifestly_unbounded(tab, var))
3608 if (to_row(tab, var, 1) < 0)
3609 return isl_stat_error;
3610
3611 if (var->is_row) {
3612 isl_int_sub(tab->mat->row[var->index][1],isl_sioimath_sub((tab->mat->row[var->index][1]), *(tab
->mat->row[var->index][1]), *(tab->mat->row[var
->index][0]))
3613 tab->mat->row[var->index][1], tab->mat->row[var->index][0])isl_sioimath_sub((tab->mat->row[var->index][1]), *(tab
->mat->row[var->index][1]), *(tab->mat->row[var
->index][0]))
;
3614 if (var->is_nonneg) {
3615 int sgn = restore_row(tab, var);
3616 isl_assert(tab->mat->ctx, sgn >= 0,do { if (sgn >= 0) break; do { isl_handle_error(tab->mat
->ctx, isl_error_unknown, "Assertion \"" "sgn >= 0" "\" failed"
, "/build/llvm-toolchain-snapshot-7~svn338205/tools/polly/lib/External/isl/isl_tab.c"
, 3617); return isl_stat_error; } while (0); } while (0)
3617 return isl_stat_error)do { if (sgn >= 0) break; do { isl_handle_error(tab->mat
->ctx, isl_error_unknown, "Assertion \"" "sgn >= 0" "\" failed"
, "/build/llvm-toolchain-snapshot-7~svn338205/tools/polly/lib/External/isl/isl_tab.c"
, 3617); return isl_stat_error; } while (0); } while (0)
;
3618 }
3619 } else {
3620 int i;
3621
3622 for (i = 0; i < tab->n_row; ++i) {
3623 if (isl_int_is_zero(tab->mat->row[i][off + var->index])(isl_sioimath_sgn(*(tab->mat->row[i][off + var->index
])) == 0)
)
3624 continue;
3625 isl_int_add(tab->mat->row[i][1], tab->mat->row[i][1],isl_sioimath_add((tab->mat->row[i][1]), *(tab->mat->
row[i][1]), *(tab->mat->row[i][off + var->index]))
3626 tab->mat->row[i][off + var->index])isl_sioimath_add((tab->mat->row[i][1]), *(tab->mat->
row[i][1]), *(tab->mat->row[i][off + var->index]))
;
3627 }
3628
3629 }
3630
3631 return isl_stat_ok;
3632}
3633
3634/* Undo the operation performed by isl_tab_unrestrict.
3635 *
3636 * In particular, mark the variable as being non-negative and make
3637 * sure the sample value respects this constraint.
3638 */
3639static isl_stat ununrestrict(struct isl_tab *tab, struct isl_tab_var *var)
3640{
3641 var->is_nonneg = 1;
3642
3643 if (var->is_row && restore_row(tab, var) < -1)
3644 return isl_stat_error;
3645
3646 return isl_stat_ok;
3647}
3648
3649/* Unmark the last redundant row in "tab" as being redundant.
3650 * This undoes part of the modifications performed by isl_tab_mark_redundant.
3651 * In particular, remove the redundant mark and make
3652 * sure the sample value respects the constraint again.
3653 * A variable that is marked non-negative by isl_tab_mark_redundant
3654 * is covered by a separate undo record.
3655 */
3656static isl_stat restore_last_redundant(struct isl_tab *tab)
3657{
3658 struct isl_tab_var *var;
3659
3660 if (tab->n_redundant < 1)
3661 isl_die(isl_tab_get_ctx(tab), isl_error_internal,do { isl_handle_error(isl_tab_get_ctx(tab), isl_error_internal
, "no redundant rows", "/build/llvm-toolchain-snapshot-7~svn338205/tools/polly/lib/External/isl/isl_tab.c"
, 3662); return isl_stat_error; } while (0)
3662 "no redundant rows", return isl_stat_error)do { isl_handle_error(isl_tab_get_ctx(tab), isl_error_internal
, "no redundant rows", "/build/llvm-toolchain-snapshot-7~svn338205/tools/polly/lib/External/isl/isl_tab.c"
, 3662); return isl_stat_error; } while (0)
;
3663
3664 var = isl_tab_var_from_row(tab, tab->n_redundant - 1);
3665 var->is_redundant = 0;
3666 tab->n_redundant--;
3667 restore_row(tab, var);
3668
3669 return isl_stat_ok;
3670}
3671
3672static isl_stat perform_undo_var(struct isl_tab *tab, struct isl_tab_undo *undo)
3673 WARN_UNUSED__attribute__((__warn_unused_result__));
3674static isl_stat perform_undo_var(struct isl_tab *tab, struct isl_tab_undo *undo)
3675{
3676 struct isl_tab_var *var = var_from_index(tab, undo->u.var_index);
3677 switch (undo->type) {
3678 case isl_tab_undo_nonneg:
3679 var->is_nonneg = 0;
3680 break;
3681 case isl_tab_undo_redundant:
3682 if (!var->is_row || var->index != tab->n_redundant - 1)
3683 isl_die(isl_tab_get_ctx(tab), isl_error_internal,do { isl_handle_error(isl_tab_get_ctx(tab), isl_error_internal
, "not undoing last redundant row", "/build/llvm-toolchain-snapshot-7~svn338205/tools/polly/lib/External/isl/isl_tab.c"
, 3685); return isl_stat_error; } while (0)
3684 "not undoing last redundant row",do { isl_handle_error(isl_tab_get_ctx(tab), isl_error_internal
, "not undoing last redundant row", "/build/llvm-toolchain-snapshot-7~svn338205/tools/polly/lib/External/isl/isl_tab.c"
, 3685); return isl_stat_error; } while (0)
3685 return isl_stat_error)do { isl_handle_error(isl_tab_get_ctx(tab), isl_error_internal
, "not undoing last redundant row", "/build/llvm-toolchain-snapshot-7~svn338205/tools/polly/lib/External/isl/isl_tab.c"
, 3685); return isl_stat_error; } while (0)
;
3686 return restore_last_redundant(tab);
3687 case isl_tab_undo_freeze:
3688 var->frozen = 0;
3689 break;
3690 case isl_tab_undo_zero:
3691 var->is_zero = 0;
3692 if (!var->is_row)
3693 tab->n_dead--;
3694 break;
3695 case isl_tab_undo_allocate:
3696 if (undo->u.var_index >= 0) {
3697 isl_assert(tab->mat->ctx, !var->is_row,do { if (!var->is_row) break; do { isl_handle_error(tab->
mat->ctx, isl_error_unknown, "Assertion \"" "!var->is_row"
"\" failed", "/build/llvm-toolchain-snapshot-7~svn338205/tools/polly/lib/External/isl/isl_tab.c"
, 3698); return isl_stat_error; } while (0); } while (0)
3698 return isl_stat_error)do { if (!var->is_row) break; do { isl_handle_error(tab->
mat->ctx, isl_error_unknown, "Assertion \"" "!var->is_row"
"\" failed", "/build/llvm-toolchain-snapshot-7~svn338205/tools/polly/lib/External/isl/isl_tab.c"
, 3698); return isl_stat_error; } while (0); } while (0)
;
3699 return drop_col(tab, var->index);
3700 }
3701 if (!var->is_row) {
3702 if (!max_is_manifestly_unbounded(tab, var)) {
3703 if (to_row(tab, var, 1) < 0)
3704 return isl_stat_error;
3705 } else if (!min_is_manifestly_unbounded(tab, var)) {
3706 if (to_row(tab, var, -1) < 0)
3707 return isl_stat_error;
3708 } else
3709 if (to_row(tab, var, 0) < 0)
3710 return isl_stat_error;
3711 }
3712 return drop_row(tab, var->index);
3713 case isl_tab_undo_relax:
3714 return unrelax(tab, var);
3715 case isl_tab_undo_unrestrict:
3716 return ununrestrict(tab, var);
3717 default:
3718 isl_die(tab->mat->ctx, isl_error_internal,do { isl_handle_error(tab->mat->ctx, isl_error_internal
, "perform_undo_var called on invalid undo record", "/build/llvm-toolchain-snapshot-7~svn338205/tools/polly/lib/External/isl/isl_tab.c"
, 3720); return isl_stat_error; } while (0)
3719 "perform_undo_var called on invalid undo record",do { isl_handle_error(tab->mat->ctx, isl_error_internal
, "perform_undo_var called on invalid undo record", "/build/llvm-toolchain-snapshot-7~svn338205/tools/polly/lib/External/isl/isl_tab.c"
, 3720); return isl_stat_error; } while (0)
3720 return isl_stat_error)do { isl_handle_error(tab->mat->ctx, isl_error_internal
, "perform_undo_var called on invalid undo record", "/build/llvm-toolchain-snapshot-7~svn338205/tools/polly/lib/External/isl/isl_tab.c"
, 3720); return isl_stat_error; } while (0)
;
3721 }
3722
3723 return isl_stat_ok;
3724}
3725
3726/* Restore all rows that have been marked redundant by isl_tab_mark_redundant
3727 * and that have been preserved in the tableau.
3728 * Note that isl_tab_mark_redundant may also have marked some variables
3729 * as being non-negative before marking them redundant. These need
3730 * to be removed as well as otherwise some constraints could end up
3731 * getting marked redundant with respect to the variable.
3732 */
3733isl_stat isl_tab_restore_redundant(struct isl_tab *tab)
3734{
3735 if (!tab)
3736 return isl_stat_error;
3737
3738 if (tab->need_undo)
3739 isl_die(isl_tab_get_ctx(tab), isl_error_invalid,do { isl_handle_error(isl_tab_get_ctx(tab), isl_error_invalid
, "manually restoring redundant constraints " "interferes with undo history"
, "/build/llvm-toolchain-snapshot-7~svn338205/tools/polly/lib/External/isl/isl_tab.c"
, 3742); return isl_stat_error; } while (0)
3740 "manually restoring redundant constraints "do { isl_handle_error(isl_tab_get_ctx(tab), isl_error_invalid
, "manually restoring redundant constraints " "interferes with undo history"
, "/build/llvm-toolchain-snapshot-7~svn338205/tools/polly/lib/External/isl/isl_tab.c"
, 3742); return isl_stat_error; } while (0)
3741 "interferes with undo history",do { isl_handle_error(isl_tab_get_ctx(tab), isl_error_invalid
, "manually restoring redundant constraints " "interferes with undo history"
, "/build/llvm-toolchain-snapshot-7~svn338205/tools/polly/lib/External/isl/isl_tab.c"
, 3742); return isl_stat_error; } while (0)
3742 return isl_stat_error)do { isl_handle_error(isl_tab_get_ctx(tab), isl_error_invalid
, "manually restoring redundant constraints " "interferes with undo history"
, "/build/llvm-toolchain-snapshot-7~svn338205/tools/polly/lib/External/isl/isl_tab.c"
, 3742); return isl_stat_error; } while (0)
;
3743
3744 while (tab->n_redundant > 0) {
3745 if (tab->row_var[tab->n_redundant - 1] >= 0) {
3746 struct isl_tab_var *var;
3747
3748 var = isl_tab_var_from_row(tab, tab->n_redundant - 1);
3749 var->is_nonneg = 0;
3750 }
3751 restore_last_redundant(tab);
3752 }
3753 return isl_stat_ok;
3754}
3755
3756/* Undo the addition of an integer division to the basic map representation
3757 * of "tab" in position "pos".
3758 */
3759static isl_stat drop_bmap_div(struct isl_tab *tab, int pos)
3760{
3761 int off;
3762
3763 off = tab->n_var - isl_basic_map_dim(tab->bmap, isl_dim_div);
3764 if (isl_basic_map_drop_div(tab->bmap, pos - off) < 0)
3765 return isl_stat_error;
3766 if (tab->samples) {
3767 tab->samples = isl_mat_drop_cols(tab->samples, 1 + pos, 1);
3768 if (!tab->samples)
3769 return isl_stat_error;
3770 }
3771
3772 return isl_stat_ok;
3773}
3774
3775/* Restore the tableau to the state where the basic variables
3776 * are those in "col_var".
3777 * We first construct a list of variables that are currently in
3778 * the basis, but shouldn't. Then we iterate over all variables
3779 * that should be in the basis and for each one that is currently
3780 * not in the basis, we exchange it with one of the elements of the
3781 * list constructed before.
3782 * We can always find an appropriate variable to pivot with because
3783 * the current basis is mapped to the old basis by a non-singular
3784 * matrix and so we can never end up with a zero row.
3785 */
3786static int restore_basis(struct isl_tab *tab, int *col_var)
3787{
3788 int i, j;
3789 int n_extra = 0;
3790 int *extra = NULL((void*)0); /* current columns that contain bad stuff */
3791 unsigned off = 2 + tab->M;
3792
3793 extra = isl_alloc_array(tab->mat->ctx, int, tab->n_col)((int *)isl_malloc_or_die(tab->mat->ctx, (tab->n_col
)*sizeof(int)))
;
3794 if (tab->n_col && !extra)
3795 goto error;
3796 for (i = 0; i < tab->n_col; ++i) {
3797 for (j = 0; j < tab->n_col; ++j)
3798 if (tab->col_var[i] == col_var[j])
3799 break;
3800 if (j < tab->n_col)
3801 continue;
3802 extra[n_extra++] = i;
3803 }
3804 for (i = 0; i < tab->n_col && n_extra > 0; ++i) {
3805 struct isl_tab_var *var;
3806 int row;
3807
3808 for (j = 0; j < tab->n_col; ++j)
3809 if (col_var[i] == tab->col_var[j])
3810 break;
3811 if (j < tab->n_col)
3812 continue;
3813 var = var_from_index(tab, col_var[i]);
3814 row = var->index;
3815 for (j = 0; j < n_extra; ++j)
3816 if (!isl_int_is_zero(tab->mat->row[row][off+extra[j]])(isl_sioimath_sgn(*(tab->mat->row[row][off+extra[j]])) ==
0)
)
3817 break;
3818 isl_assert(tab->mat->ctx, j < n_extra, goto error)do { if (j < n_extra) break; do { isl_handle_error(tab->
mat->ctx, isl_error_unknown, "Assertion \"" "j < n_extra"
"\" failed", "/build/llvm-toolchain-snapshot-7~svn338205/tools/polly/lib/External/isl/isl_tab.c"
, 3818); goto error; } while (0); } while (0)
;
3819 if (isl_tab_pivot(tab, row, extra[j]) < 0)
3820 goto error;
3821 extra[j] = extra[--n_extra];
3822 }
3823
3824 free(extra);
3825 return 0;
3826error:
3827 free(extra);
3828 return -1;
3829}
3830
3831/* Remove all samples with index n or greater, i.e., those samples
3832 * that were added since we saved this number of samples in
3833 * isl_tab_save_samples.
3834 */
3835static void drop_samples_since(struct isl_tab *tab, int n)
3836{
3837 int i;
3838
3839 for (i = tab->n_sample - 1; i >= 0 && tab->n_sample > n; --i) {
3840 if (tab->sample_index[i] < n)
3841 continue;
3842
3843 if (i != tab->n_sample - 1) {
3844 int t = tab->sample_index[tab->n_sample-1];
3845 tab->sample_index[tab->n_sample-1] = tab->sample_index[i];
3846 tab->sample_index[i] = t;
3847 isl_mat_swap_rows(tab->samples, tab->n_sample-1, i);
3848 }
3849 tab->n_sample--;
3850 }
3851}
3852
3853static isl_stat perform_undo(struct isl_tab *tab, struct isl_tab_undo *undo)
3854 WARN_UNUSED__attribute__((__warn_unused_result__));
3855static isl_stat perform_undo(struct isl_tab *tab, struct isl_tab_undo *undo)
3856{
3857 switch (undo->type) {
3858 case isl_tab_undo_rational:
3859 tab->rational = 0;
3860 break;
3861 case isl_tab_undo_empty:
3862 tab->empty = 0;
3863 break;
3864 case isl_tab_undo_nonneg:
3865 case isl_tab_undo_redundant:
3866 case isl_tab_undo_freeze:
3867 case isl_tab_undo_zero:
3868 case isl_tab_undo_allocate:
3869 case isl_tab_undo_relax:
3870 case isl_tab_undo_unrestrict:
3871 return perform_undo_var(tab, undo);
3872 case isl_tab_undo_bmap_eq:
3873 return isl_basic_map_free_equality(tab->bmap, 1);
3874 case isl_tab_undo_bmap_ineq:
3875 return isl_basic_map_free_inequality(tab->bmap, 1);
3876 case isl_tab_undo_bmap_div:
3877 return drop_bmap_div(tab, undo->u.var_index);
3878 case isl_tab_undo_saved_basis:
3879 if (restore_basis(tab, undo->u.col_var) < 0)
3880 return isl_stat_error;
3881 break;
3882 case isl_tab_undo_drop_sample:
3883 tab->n_outside--;
3884 break;
3885 case isl_tab_undo_saved_samples:
3886 drop_samples_since(tab, undo->u.n);
3887 break;
3888 case isl_tab_undo_callback:
3889 return undo->u.callback->run(undo->u.callback);
3890 default:
3891 isl_assert(tab->mat->ctx, 0, return isl_stat_error)do { if (0) break; do { isl_handle_error(tab->mat->ctx,
isl_error_unknown, "Assertion \"" "0" "\" failed", "/build/llvm-toolchain-snapshot-7~svn338205/tools/polly/lib/External/isl/isl_tab.c"
, 3891); return isl_stat_error; } while (0); } while (0)
;
3892 }
3893 return isl_stat_ok;
3894}
3895
3896/* Return the tableau to the state it was in when the snapshot "snap"
3897 * was taken.
3898 */
3899int isl_tab_rollback(struct isl_tab *tab, struct isl_tab_undo *snap)
3900{
3901 struct isl_tab_undo *undo, *next;
3902
3903 if (!tab)
3904 return -1;
3905
3906 tab->in_undo = 1;
3907 for (undo = tab->top; undo && undo != &tab->bottom; undo = next) {
3908 next = undo->next;
3909 if (undo == snap)
3910 break;
3911 if (perform_undo(tab, undo) < 0) {
3912 tab->top = undo;
3913 free_undo(tab);
3914 tab->in_undo = 0;
3915 return -1;
3916 }
3917 free_undo_record(undo);
3918 }
3919 tab->in_undo = 0;
3920 tab->top = undo;
3921 if (!undo)
3922 return -1;
3923 return 0;
3924}
3925
3926/* The given row "row" represents an inequality violated by all
3927 * points in the tableau. Check for some special cases of such
3928 * separating constraints.
3929 * In particular, if the row has been reduced to the constant -1,
3930 * then we know the inequality is adjacent (but opposite) to
3931 * an equality in the tableau.
3932 * If the row has been reduced to r = c*(-1 -r'), with r' an inequality
3933 * of the tableau and c a positive constant, then the inequality
3934 * is adjacent (but opposite) to the inequality r'.
3935 */
3936static enum isl_ineq_type separation_type(struct isl_tab *tab, unsigned row)
3937{
3938 int pos;
3939 unsigned off = 2 + tab->M;
3940
3941 if (tab->rational)
3942 return isl_ineq_separate;
3943
3944 if (!isl_int_is_one(tab->mat->row[row][0])(isl_sioimath_cmp_si(*(tab->mat->row[row][0]), 1) == 0))
3945 return isl_ineq_separate;
3946
3947 pos = isl_seq_first_non_zero(tab->mat->row[row] + off + tab->n_dead,
3948 tab->n_col - tab->n_dead);
3949 if (pos == -1) {
3950 if (isl_int_is_negone(tab->mat->row[row][1])(isl_sioimath_cmp_si(*(tab->mat->row[row][1]), -1) == 0
)
)
3951 return isl_ineq_adj_eq;
3952 else
3953 return isl_ineq_separate;
3954 }
3955
3956 if (!isl_int_eq(tab->mat->row[row][1],(isl_sioimath_cmp(*(tab->mat->row[row][1]), *(tab->mat
->row[row][off + tab->n_dead + pos])) == 0)
3957 tab->mat->row[row][off + tab->n_dead + pos])(isl_sioimath_cmp(*(tab->mat->row[row][1]), *(tab->mat
->row[row][off + tab->n_dead + pos])) == 0)
)
3958 return isl_ineq_separate;
3959
3960 pos = isl_seq_first_non_zero(
3961 tab->mat->row[row] + off + tab->n_dead + pos + 1,
3962 tab->n_col - tab->n_dead - pos - 1);
3963
3964 return pos == -1 ? isl_ineq_adj_ineq : isl_ineq_separate;
3965}
3966
3967/* Check the effect of inequality "ineq" on the tableau "tab".
3968 * The result may be
3969 * isl_ineq_redundant: satisfied by all points in the tableau
3970 * isl_ineq_separate: satisfied by no point in the tableau
3971 * isl_ineq_cut: satisfied by some by not all points
3972 * isl_ineq_adj_eq: adjacent to an equality
3973 * isl_ineq_adj_ineq: adjacent to an inequality.
3974 */
3975enum isl_ineq_type isl_tab_ineq_type(struct isl_tab *tab, isl_int *ineq)
3976{
3977 enum isl_ineq_type type = isl_ineq_error;
3978 struct isl_tab_undo *snap = NULL((void*)0);
3979 int con;
3980 int row;
3981
3982 if (!tab)
3983 return isl_ineq_error;
3984
3985 if (isl_tab_extend_cons(tab, 1) < 0)
3986 return isl_ineq_error;
3987
3988 snap = isl_tab_snap(tab);
3989
3990 con = isl_tab_add_row(tab, ineq);
3991 if (con < 0)
3992 goto error;
3993
3994 row = tab->con[con].index;
3995 if (isl_tab_row_is_redundant(tab, row))
3996 type = isl_ineq_redundant;
3997 else if (isl_int_is_neg(tab->mat->row[row][1])(isl_sioimath_sgn(*(tab->mat->row[row][1])) < 0) &&
3998 (tab->rational ||
3999 isl_int_abs_ge(tab->mat->row[row][1],(isl_sioimath_abs_cmp(*(tab->mat->row[row][1]), *(tab->
mat->row[row][0])) >= 0)
4000 tab->mat->row[row][0])(isl_sioimath_abs_cmp(*(tab->mat->row[row][1]), *(tab->
mat->row[row][0])) >= 0)
)) {
4001 int nonneg = at_least_zero(tab, &tab->con[con]);
4002 if (nonneg < 0)
4003 goto error;
4004 if (nonneg)
4005 type = isl_ineq_cut;
4006 else
4007 type = separation_type(tab, row);
4008 } else {
4009 int red = con_is_redundant(tab, &tab->con[con]);
4010 if (red < 0)
4011 goto error;
4012 if (!red)
4013 type = isl_ineq_cut;
4014 else
4015 type = isl_ineq_redundant;
4016 }
4017
4018 if (isl_tab_rollback(tab, snap))
4019 return isl_ineq_error;
4020 return type;
4021error:
4022 return isl_ineq_error;
4023}
4024
4025isl_stat isl_tab_track_bmap(struct isl_tab *tab, __isl_take isl_basic_map *bmap)
4026{
4027 bmap = isl_basic_map_cow(bmap);
4028 if (!tab || !bmap)
4029 goto error;
4030
4031 if (tab->empty) {
4032 bmap = isl_basic_map_set_to_empty(bmap);
4033 if (!bmap)
4034 goto error;
4035 tab->bmap = bmap;
4036 return isl_stat_ok;
4037 }
4038
4039 isl_assert(tab->mat->ctx, tab->n_eq == bmap->n_eq, goto error)do { if (tab->n_eq == bmap->n_eq) break; do { isl_handle_error
(tab->mat->ctx, isl_error_unknown, "Assertion \"" "tab->n_eq == bmap->n_eq"
"\" failed", "/build/llvm-toolchain-snapshot-7~svn338205/tools/polly/lib/External/isl/isl_tab.c"
, 4039); goto error; } while (0); } while (0)
;
4040 isl_assert(tab->mat->ctx,do { if (tab->n_con == bmap->n_eq + bmap->n_ineq) break
; do { isl_handle_error(tab->mat->ctx, isl_error_unknown
, "Assertion \"" "tab->n_con == bmap->n_eq + bmap->n_ineq"
"\" failed", "/build/llvm-toolchain-snapshot-7~svn338205/tools/polly/lib/External/isl/isl_tab.c"
, 4041); goto error; } while (0); } while (0)
4041 tab->n_con == bmap->n_eq + bmap->n_ineq, goto error)do { if (tab->n_con == bmap->n_eq + bmap->n_ineq) break
; do { isl_handle_error(tab->mat->ctx, isl_error_unknown
, "Assertion \"" "tab->n_con == bmap->n_eq + bmap->n_ineq"
"\" failed", "/build/llvm-toolchain-snapshot-7~svn338205/tools/polly/lib/External/isl/isl_tab.c"
, 4041); goto error; } while (0); } while (0)
;
4042
4043 tab->bmap = bmap;
4044
4045 return isl_stat_ok;
4046error:
4047 isl_basic_map_free(bmap);
4048 return isl_stat_error;
4049}
4050
4051isl_stat isl_tab_track_bset(struct isl_tab *tab, __isl_take isl_basic_setisl_basic_map *bset)
4052{
4053 return isl_tab_track_bmap(tab, bset_to_bmap(bset));
4054}
4055
4056__isl_keep isl_basic_setisl_basic_map *isl_tab_peek_bset(struct isl_tab *tab)
4057{
4058 if (!tab)
4059 return NULL((void*)0);
4060
4061 return bset_from_bmap(tab->bmap);
4062}
4063
4064static void isl_tab_print_internal(__isl_keep struct isl_tab *tab,
4065 FILE *out, int indent)
4066{
4067 unsigned r, c;
4068 int i;
4069
4070 if (!tab) {
4071 fprintf(out, "%*snull tab\n", indent, "");
4072 return;
4073 }
4074 fprintf(out, "%*sn_redundant: %d, n_dead: %d", indent, "",
4075 tab->n_redundant, tab->n_dead);
4076 if (tab->rational)
4077 fprintf(out, ", rational");
4078 if (tab->empty)
4079 fprintf(out, ", empty");
4080 fprintf(out, "\n");
4081 fprintf(out, "%*s[", indent, "");
4082 for (i = 0; i < tab->n_var; ++i) {
4083 if (i)
4084 fprintf(out, (i == tab->n_param ||
4085 i == tab->n_var - tab->n_div) ? "; "
4086 : ", ");
4087 fprintf(out, "%c%d%s", tab->var[i].is_row ? 'r' : 'c',
4088 tab->var[i].index,
4089 tab->var[i].is_zero ? " [=0]" :
4090 tab->var[i].is_redundant ? " [R]" : "");
4091 }
4092 fprintf(out, "]\n");
4093 fprintf(out, "%*s[", indent, "");
4094 for (i = 0; i < tab->n_con; ++i) {
4095 if (i)
4096 fprintf(out, ", ");
4097 fprintf(out, "%c%d%s", tab->con[i].is_row ? 'r' : 'c',
4098 tab->con[i].index,
4099 tab->con[i].is_zero ? " [=0]" :
4100 tab->con[i].is_redundant ? " [R]" : "");
4101 }
4102 fprintf(out, "]\n");
4103 fprintf(out, "%*s[", indent, "");
4104 for (i = 0; i < tab->n_row; ++i) {
4105 const char *sign = "";
4106 if (i)
4107 fprintf(out, ", ");
4108 if (tab->row_sign) {
4109 if (tab->row_sign[i] == isl_tab_row_unknown)
4110 sign = "?";
4111 else if (tab->row_sign[i] == isl_tab_row_neg)
4112 sign = "-";
4113 else if (tab->row_sign[i] == isl_tab_row_pos)
4114 sign = "+";
4115 else
4116 sign = "+-";
4117 }
4118 fprintf(out, "r%d: %d%s%s", i, tab->row_var[i],
4119 isl_tab_var_from_row(tab, i)->is_nonneg ? " [>=0]" : "", sign);
4120 }
4121 fprintf(out, "]\n");
4122 fprintf(out, "%*s[", indent, "");
4123 for (i = 0; i < tab->n_col; ++i) {
4124 if (i)
4125 fprintf(out, ", ");
4126 fprintf(out, "c%d: %d%s", i, tab->col_var[i],
4127 var_from_col(tab, i)->is_nonneg ? " [>=0]" : "");
4128 }
4129 fprintf(out, "]\n");
4130 r = tab->mat->n_row;
4131 tab->mat->n_row = tab->n_row;
4132 c = tab->mat->n_col;
4133 tab->mat->n_col = 2 + tab->M + tab->n_col;
4134 isl_mat_print_internal(tab->mat, out, indent);
4135 tab->mat->n_row = r;
4136 tab->mat->n_col = c;
4137 if (tab->bmap)
4138 isl_basic_map_print_internal(tab->bmap, out, indent);
4139}
4140
4141void isl_tab_dump(__isl_keep struct isl_tab *tab)
4142{
4143 isl_tab_print_internal(tab, stderrstderr, 0);
4144}

/build/llvm-toolchain-snapshot-7~svn338205/tools/polly/lib/External/isl/isl_int_sioimath.h

1/*
2 * Copyright 2015 INRIA Paris-Rocquencourt
3 *
4 * Use of this software is governed by the MIT license
5 *
6 * Written by Michael Kruse, INRIA Paris-Rocquencourt,
7 * Domaine de Voluceau, Rocquenqourt, B.P. 105,
8 * 78153 Le Chesnay Cedex France
9 */
10#ifndef ISL_INT_SIOIMATH_H
11#define ISL_INT_SIOIMATH_H
12
13#include <inttypes.h>
14#include <limits.h>
15#include <stdint.h>
16#include <stdlib.h>
17
18#include <isl_imath.h>
19#include <isl/hash.h>
20
21#define ARRAY_SIZE(array)(sizeof(array)/sizeof(*array)) (sizeof(array)/sizeof(*array))
22
23/* Visual Studio before VS2015 does not support the inline keyword when
24 * compiling in C mode because it was introduced in C99 which it does not
25 * officially support. Instead, it has a proprietary extension using __inline.
26 */
27#if defined(_MSC_VER) && (_MSC_VER < 1900)
28#define inline __inline
29#endif
30
31/* The type to represent integers optimized for small values. It is either a
32 * pointer to an mp_int ( = mpz_t*; big representation) or an int32_t (small
33 * represenation) with a discriminator at the least significant bit. In big
34 * representation it will be always zero because of heap alignment. It is set
35 * to 1 for small representation and use the 32 most significant bits for the
36 * int32_t.
37 *
38 * Structure on 64 bit machines, with 8-byte aligment (3 bits):
39 *
40 * Big representation:
41 * MSB LSB
42 * |------------------------------------------------------------000
43 * | mpz_t* |
44 * | != NULL |
45 *
46 * Small representation:
47 * MSB 32 LSB
48 * |------------------------------|00000000000000000000000000000001
49 * | int32_t |
50 * | 2147483647 ... -2147483647 |
51 * ^
52 * |
53 * discriminator bit
54 *
55 * On 32 bit machines isl_sioimath type is blown up to 8 bytes, i.e.
56 * isl_sioimath is guaranteed to be at least 8 bytes. This is to ensure the
57 * int32_t can be hidden in that type without data loss. In the future we might
58 * optimize this to use 31 hidden bits in a 32 bit pointer. We may also use 63
59 * bits on 64 bit machines, but this comes with the cost of additional overflow
60 * checks because there is no standardized 128 bit integer we could expand to.
61 *
62 * We use native integer types and avoid union structures to avoid assumptions
63 * on the machine's endianness.
64 *
65 * This implementation makes the following assumptions:
66 * - long can represent any int32_t
67 * - mp_small is signed long
68 * - mp_usmall is unsigned long
69 * - adresses returned by malloc are aligned to 2-byte boundaries (leastmost
70 * bit is zero)
71 */
72#if UINT64_MAX(18446744073709551615UL) > UINTPTR_MAX(18446744073709551615UL)
73typedef uint64_t isl_sioimath;
74#else
75typedef uintptr_t isl_sioimath;
76#endif
77
78/* The negation of the smallest possible number in int32_t, INT32_MIN
79 * (0x80000000u, -2147483648), cannot be represented in an int32_t, therefore
80 * every operation that may produce this value needs to special-case it.
81 * The operations are:
82 * abs(INT32_MIN)
83 * -INT32_MIN (negation)
84 * -1 * INT32_MIN (multiplication)
85 * INT32_MIN/-1 (any division: divexact, fdiv, cdiv, tdiv)
86 * To avoid checking these cases, we exclude INT32_MIN from small
87 * representation.
88 */
89#define ISL_SIOIMATH_SMALL_MIN(-(2147483647)) (-INT32_MAX(2147483647))
90
91/* Largest possible number in small representation */
92#define ISL_SIOIMATH_SMALL_MAX(2147483647) INT32_MAX(2147483647)
93
94/* Used for function parameters the function modifies. */
95typedef isl_sioimath *isl_sioimath_ptr;
96
97/* Used for function parameters that are read-only. */
98typedef isl_sioimath isl_sioimath_src;
99
100/* Return whether the argument is stored in small representation.
101 */
102inline int isl_sioimath_is_small(isl_sioimath val)
103{
104 return val & 0x00000001;
105}
106
107/* Return whether the argument is stored in big representation.
108 */
109inline int isl_sioimath_is_big(isl_sioimath val)
110{
111 return !isl_sioimath_is_small(val);
112}
113
114/* Get the number of an isl_int in small representation. Result is undefined if
115 * val is not stored in that format.
116 */
117inline int32_t isl_sioimath_get_small(isl_sioimath val)
118{
119 return val >> 32;
120}
121
122/* Get the number of an in isl_int in big representation. Result is undefined if
123 * val is not stored in that format.
124 */
125inline mp_int isl_sioimath_get_big(isl_sioimath val)
126{
127 return (mp_int)(uintptr_t) val;
128}
129
130/* Return 1 if val is stored in small representation and store its value to
131 * small. We rely on the compiler to optimize the isl_sioimath_get_small such
132 * that the shift is moved into the branch that executes in case of small
133 * representation. If there is no such branch, then a single shift is still
134 * cheaper than introducing branching code.
135 */
136inline int isl_sioimath_decode_small(isl_sioimath val, int32_t *small)
137{
138 *small = isl_sioimath_get_small(val);
139 return isl_sioimath_is_small(val);
140}
141
142/* Return 1 if val is stored in big representation and store its value to big.
143 */
144inline int isl_sioimath_decode_big(isl_sioimath val, mp_int *big)
145{
146 *big = isl_sioimath_get_big(val);
147 return isl_sioimath_is_big(val);
148}
149
150/* Encode a small representation into an isl_int.
151 */
152inline isl_sioimath isl_sioimath_encode_small(int32_t val)
153{
154 return ((isl_sioimath) val) << 32 | 0x00000001;
155}
156
157/* Encode a big representation.
158 */
159inline isl_sioimath isl_sioimath_encode_big(mp_int val)
160{
161 return (isl_sioimath)(uintptr_t) val;
162}
163
164/* A common situation is to call an IMath function with at least one argument
165 * that is currently in small representation or an integer parameter, i.e. a big
166 * representation of the same number is required. Promoting the original
167 * argument comes with multiple problems, such as modifying a read-only
168 * argument, the responsibility of deallocation and the execution cost. Instead,
169 * we make a copy by 'faking' the IMath internal structure.
170 *
171 * We reserve the maximum number of required digits on the stack to avoid heap
172 * allocations.
173 *
174 * mp_digit can be uint32_t or uint16_t. This code must work for little and big
175 * endian digits. The structure for an uint64_t argument and 32-bit mp_digits is
176 * sketched below.
177 *
178 * |----------------------------|
179 * uint64_t
180 *
181 * |-------------||-------------|
182 * mp_digit mp_digit
183 * digits[1] digits[0]
184 * Most sig digit Least sig digit
185 */
186typedef struct {
187 mpz_t big;
188 mp_digit digits[(sizeof(uintmax_t) + sizeof(mp_digit) - 1) /
189 sizeof(mp_digit)];
190} isl_sioimath_scratchspace_t;
191
192/* Convert a native integer to IMath's digit representation. A native integer
193 * might be big- or little endian, but IMath always stores the least significant
194 * digit in the lowest array indices. memcpy therefore is not possible.
195 *
196 * We also have to consider that long and mp_digit can be of different sizes,
197 * depending on the compiler (LP64, LLP64) and IMath's USE_64BIT_WORDS. This
198 * macro should work for all of them.
199 *
200 * "used" is set to the number of written digits. It must be minimal (IMath
201 * checks zeroness using the used field), but always at least one. Also note
202 * that the result of num>>(sizeof(num)*CHAR_BIT) is undefined.
203 */
204#define ISL_SIOIMATH_TO_DIGITS(num, digits, used)do { int i = 0; do { (digits)[i] = ((num) >> (sizeof(mp_digit
) * 8 * i)); i += 1; if (i >= (sizeof(num) + sizeof(mp_digit
) - 1) / sizeof(mp_digit)) break; if (((num) >> (sizeof
(mp_digit) * 8 * i)) == 0) break; } while (1); (used) = i; } while
(0)
\
205 do { \
206 int i = 0; \
207 do { \
208 (digits)[i] = \
209 ((num) >> (sizeof(mp_digit) * CHAR_BIT8 * i)); \
210 i += 1; \
211 if (i >= (sizeof(num) + sizeof(mp_digit) - 1) / \
212 sizeof(mp_digit)) \
213 break; \
214 if (((num) >> (sizeof(mp_digit) * CHAR_BIT8 * i)) == 0) \
215 break; \
216 } while (1); \
217 (used) = i; \
218 } while (0)
219
220inline void isl_siomath_uint32_to_digits(uint32_t num, mp_digit *digits,
221 mp_size *used)
222{
223 ISL_SIOIMATH_TO_DIGITS(num, digits, *used)do { int i = 0; do { (digits)[i] = ((num) >> (sizeof(mp_digit
) * 8 * i)); i += 1; if (i >= (sizeof(num) + sizeof(mp_digit
) - 1) / sizeof(mp_digit)) break; if (((num) >> (sizeof
(mp_digit) * 8 * i)) == 0) break; } while (1); (*used) = i; }
while (0)
;
224}
225
226inline void isl_siomath_ulong_to_digits(unsigned long num, mp_digit *digits,
227 mp_size *used)
228{
229 ISL_SIOIMATH_TO_DIGITS(num, digits, *used)do { int i = 0; do { (digits)[i] = ((num) >> (sizeof(mp_digit
) * 8 * i)); i += 1; if (i >= (sizeof(num) + sizeof(mp_digit
) - 1) / sizeof(mp_digit)) break; if (((num) >> (sizeof
(mp_digit) * 8 * i)) == 0) break; } while (1); (*used) = i; }
while (0)
;
230}
231
232inline void isl_siomath_uint64_to_digits(uint64_t num, mp_digit *digits,
233 mp_size *used)
234{
235 ISL_SIOIMATH_TO_DIGITS(num, digits, *used)do { int i = 0; do { (digits)[i] = ((num) >> (sizeof(mp_digit
) * 8 * i)); i += 1; if (i >= (sizeof(num) + sizeof(mp_digit
) - 1) / sizeof(mp_digit)) break; if (((num) >> (sizeof
(mp_digit) * 8 * i)) == 0) break; } while (1); (*used) = i; }
while (0)
;
236}
237
238/* Get the IMath representation of an isl_int without modifying it.
239 * For the case it is not in big representation yet, pass some scratch space we
240 * can use to store the big representation in.
241 * In order to avoid requiring init and free on the scratch space, we directly
242 * modify the internal representation.
243 *
244 * The name derives from its indented use: getting the big representation of an
245 * input (src) argument.
246 */
247inline mp_int isl_sioimath_bigarg_src(isl_sioimath arg,
248 isl_sioimath_scratchspace_t *scratch)
249{
250 mp_int big;
251 int32_t small;
252 uint32_t num;
253
254 if (isl_sioimath_decode_big(arg, &big))
255 return big;
256
257 small = isl_sioimath_get_small(arg);
258 scratch->big.digits = scratch->digits;
259 scratch->big.alloc = ARRAY_SIZE(scratch->digits)(sizeof(scratch->digits)/sizeof(*scratch->digits));
260 if (small >= 0) {
261 scratch->big.sign = MP_ZPOS;
262 num = small;
263 } else {
264 scratch->big.sign = MP_NEG;
265 num = -small;
266 }
267
268 isl_siomath_uint32_to_digits(num, scratch->digits, &scratch->big.used);
269 return &scratch->big;
270}
271
272/* Create a temporary IMath mp_int for a signed long.
273 */
274inline mp_int isl_sioimath_siarg_src(signed long arg,
275 isl_sioimath_scratchspace_t *scratch)
276{
277 unsigned long num;
278
279 scratch->big.digits = scratch->digits;
280 scratch->big.alloc = ARRAY_SIZE(scratch->digits)(sizeof(scratch->digits)/sizeof(*scratch->digits));
281 if (arg >= 0) {
282 scratch->big.sign = MP_ZPOS;
283 num = arg;
284 } else {
285 scratch->big.sign = MP_NEG;
286 num = (arg == LONG_MIN(-9223372036854775807L -1L)) ? ((unsigned long) LONG_MAX9223372036854775807L) + 1 : -arg;
287 }
288
289 isl_siomath_ulong_to_digits(num, scratch->digits, &scratch->big.used);
290 return &scratch->big;
291}
292
293/* Create a temporary IMath mp_int for an int64_t.
294 */
295inline mp_int isl_sioimath_si64arg_src(int64_t arg,
296 isl_sioimath_scratchspace_t *scratch)
297{
298 uint64_t num;
299
300 scratch->big.digits = scratch->digits;
301 scratch->big.alloc = ARRAY_SIZE(scratch->digits)(sizeof(scratch->digits)/sizeof(*scratch->digits));
302 if (arg >= 0) {
303 scratch->big.sign = MP_ZPOS;
304 num = arg;
305 } else {
306 scratch->big.sign = MP_NEG;
307 num = (arg == INT64_MIN(-9223372036854775807L -1)) ? ((uint64_t) INT64_MAX(9223372036854775807L)) + 1 : -arg;
308 }
309
310 isl_siomath_uint64_to_digits(num, scratch->digits, &scratch->big.used);
311 return &scratch->big;
312}
313
314/* Create a temporary IMath mp_int for an unsigned long.
315 */
316inline mp_int isl_sioimath_uiarg_src(unsigned long arg,
317 isl_sioimath_scratchspace_t *scratch)
318{
319 scratch->big.digits = scratch->digits;
320 scratch->big.alloc = ARRAY_SIZE(scratch->digits)(sizeof(scratch->digits)/sizeof(*scratch->digits));
321 scratch->big.sign = MP_ZPOS;
322
323 isl_siomath_ulong_to_digits(arg, scratch->digits, &scratch->big.used);
324 return &scratch->big;
325}
326
327/* Ensure big representation. Does not preserve the current number.
328 * Callers may use the fact that the value _is_ preserved if the presentation
329 * was big before.
330 */
331inline mp_int isl_sioimath_reinit_big(isl_sioimath_ptr ptr)
332{
333 if (isl_sioimath_is_small(*ptr))
334 *ptr = isl_sioimath_encode_big(mp_int_alloc());
335 return isl_sioimath_get_big(*ptr);
336}
337
338/* Set ptr to a number in small representation.
339 */
340inline void isl_sioimath_set_small(isl_sioimath_ptr ptr, int32_t val)
341{
342 if (isl_sioimath_is_big(*ptr))
343 mp_int_free(isl_sioimath_get_big(*ptr));
344 *ptr = isl_sioimath_encode_small(val);
345}
346
347/* Set ptr to val, choosing small representation if possible.
348 */
349inline void isl_sioimath_set_int32(isl_sioimath_ptr ptr, int32_t val)
350{
351 if (ISL_SIOIMATH_SMALL_MIN(-(2147483647)) <= val && val <= ISL_SIOIMATH_SMALL_MAX(2147483647)) {
352 isl_sioimath_set_small(ptr, val);
353 return;
354 }
355
356 mp_int_init_value(isl_sioimath_reinit_big(ptr), val);
357}
358
359/* Assign an int64_t number using small representation if possible.
360 */
361inline void isl_sioimath_set_int64(isl_sioimath_ptr ptr, int64_t val)
362{
363 if (ISL_SIOIMATH_SMALL_MIN(-(2147483647)) <= val && val <= ISL_SIOIMATH_SMALL_MAX(2147483647)) {
364 isl_sioimath_set_small(ptr, val);
365 return;
366 }
367
368 isl_sioimath_scratchspace_t scratch;
369 mp_int_copy(isl_sioimath_si64arg_src(val, &scratch),
370 isl_sioimath_reinit_big(ptr));
371}
372
373/* Convert to big representation while preserving the current number.
374 */
375inline void isl_sioimath_promote(isl_sioimath_ptr dst)
376{
377 int32_t small;
378
379 if (isl_sioimath_is_big(*dst))
380 return;
381
382 small = isl_sioimath_get_small(*dst);
383 mp_int_set_value(isl_sioimath_reinit_big(dst), small);
384}
385
386/* Convert to small representation while preserving the current number. Does
387 * nothing if dst doesn't fit small representation.
388 */
389inline void isl_sioimath_try_demote(isl_sioimath_ptr dst)
390{
391 mp_small small;
392
393 if (isl_sioimath_is_small(*dst))
394 return;
395
396 if (mp_int_to_int(isl_sioimath_get_big(*dst), &small) != MP_OK)
397 return;
398
399 if (ISL_SIOIMATH_SMALL_MIN(-(2147483647)) <= small && small <= ISL_SIOIMATH_SMALL_MAX(2147483647))
400 isl_sioimath_set_small(dst, small);
401}
402
403/* Initialize an isl_int. The implicit value is 0 in small representation.
404 */
405inline void isl_sioimath_init(isl_sioimath_ptr dst)
406{
407 *dst = isl_sioimath_encode_small(0);
408}
409
410/* Free the resources taken by an isl_int.
411 */
412inline void isl_sioimath_clear(isl_sioimath_ptr dst)
413{
414 if (isl_sioimath_is_small(*dst))
415 return;
416
417 mp_int_free(isl_sioimath_get_big(*dst));
418}
419
420/* Copy the value of one isl_int to another.
421 */
422inline void isl_sioimath_set(isl_sioimath_ptr dst, isl_sioimath_src val)
423{
424 if (isl_sioimath_is_small(val)) {
425 isl_sioimath_set_small(dst, isl_sioimath_get_small(val));
426 return;
427 }
428
429 mp_int_copy(isl_sioimath_get_big(val), isl_sioimath_reinit_big(dst));
430}
431
432/* Store a signed long into an isl_int.
433 */
434inline void isl_sioimath_set_si(isl_sioimath_ptr dst, long val)
435{
436 if (ISL_SIOIMATH_SMALL_MIN(-(2147483647)) <= val && val <= ISL_SIOIMATH_SMALL_MAX(2147483647)) {
437 isl_sioimath_set_small(dst, val);
438 return;
439 }
440
441 mp_int_set_value(isl_sioimath_reinit_big(dst), val);
442}
443
444/* Store an unsigned long into an isl_int.
445 */
446inline void isl_sioimath_set_ui(isl_sioimath_ptr dst, unsigned long val)
447{
448 if (val <= ISL_SIOIMATH_SMALL_MAX(2147483647)) {
449 isl_sioimath_set_small(dst, val);
450 return;
451 }
452
453 mp_int_set_uvalue(isl_sioimath_reinit_big(dst), val);
454}
455
456/* Return whether a number can be represented by a signed long.
457 */
458inline int isl_sioimath_fits_slong(isl_sioimath_src val)
459{
460 mp_small dummy;
461
462 if (isl_sioimath_is_small(val))
463 return 1;
464
465 return mp_int_to_int(isl_sioimath_get_big(val), &dummy) == MP_OK;
466}
467
468/* Return a number as signed long. Result is undefined if the number cannot be
469 * represented as long.
470 */
471inline long isl_sioimath_get_si(isl_sioimath_src val)
472{
473 mp_small result;
474
475 if (isl_sioimath_is_small(val))
476 return isl_sioimath_get_small(val);
477
478 mp_int_to_int(isl_sioimath_get_big(val), &result);
479 return result;
480}
481
482/* Return whether a number can be represented as unsigned long.
483 */
484inline int isl_sioimath_fits_ulong(isl_sioimath_src val)
485{
486 mp_usmall dummy;
487
488 if (isl_sioimath_is_small(val))
489 return isl_sioimath_get_small(val) >= 0;
490
491 return mp_int_to_uint(isl_sioimath_get_big(val), &dummy) == MP_OK;
492}
493
494/* Return a number as unsigned long. Result is undefined if the number cannot be
495 * represented as unsigned long.
496 */
497inline unsigned long isl_sioimath_get_ui(isl_sioimath_src val)
498{
499 mp_usmall result;
500
501 if (isl_sioimath_is_small(val))
502 return isl_sioimath_get_small(val);
503
504 mp_int_to_uint(isl_sioimath_get_big(val), &result);
505 return result;
506}
507
508/* Return a number as floating point value.
509 */
510inline double isl_sioimath_get_d(isl_sioimath_src val)
511{
512 mp_int big;
513 double result = 0;
514 int i;
515
516 if (isl_sioimath_is_small(val))
517 return isl_sioimath_get_small(val);
518
519 big = isl_sioimath_get_big(val);
520 for (i = 0; i < big->used; ++i)
521 result = result * (double) ((uintmax_t) MP_DIGIT_MAX((4294967295U) * 1UL) + 1) +
522 (double) big->digits[i];
523
524 if (big->sign == MP_NEG)
525 result = -result;
526
527 return result;
528}
529
530/* Format a number as decimal string.
531 *
532 * The largest possible string from small representation is 12 characters
533 * ("-2147483647").
534 */
535inline char *isl_sioimath_get_str(isl_sioimath_src val)
536{
537 char *result;
538
539 if (isl_sioimath_is_small(val)) {
540 result = malloc(12);
541 snprintf(result, 12, "%" PRIi32"i", isl_sioimath_get_small(val));
542 return result;
543 }
544
545 return impz_get_str(NULL((void*)0), 10, isl_sioimath_get_big(val));
546}
547
548/* Return the absolute value.
549 */
550inline void isl_sioimath_abs(isl_sioimath_ptr dst, isl_sioimath_src arg)
551{
552 if (isl_sioimath_is_small(arg)) {
553 isl_sioimath_set_small(dst, labs(isl_sioimath_get_small(arg)));
554 return;
555 }
556
557 mp_int_abs(isl_sioimath_get_big(arg), isl_sioimath_reinit_big(dst));
558}
559
560/* Return the negation of a number.
561 */
562inline void isl_sioimath_neg(isl_sioimath_ptr dst, isl_sioimath_src arg)
563{
564 if (isl_sioimath_is_small(arg)) {
565 isl_sioimath_set_small(dst, -isl_sioimath_get_small(arg));
566 return;
567 }
568
569 mp_int_neg(isl_sioimath_get_big(arg), isl_sioimath_reinit_big(dst));
570}
571
572/* Swap two isl_ints.
573 *
574 * isl_sioimath can be copied bytewise; nothing depends on its address. It can
575 * also be stored in a CPU register.
576 */
577inline void isl_sioimath_swap(isl_sioimath_ptr lhs, isl_sioimath_ptr rhs)
578{
579 isl_sioimath tmp = *lhs;
580 *lhs = *rhs;
20
Assigned value is garbage or undefined
581 *rhs = tmp;
582}
583
584/* Add an unsigned long to the number.
585 *
586 * On LP64 unsigned long exceeds the range of an int64_t, therefore we check in
587 * advance whether small representation possibly overflows.
588 */
589inline void isl_sioimath_add_ui(isl_sioimath_ptr dst, isl_sioimath lhs,
590 unsigned long rhs)
591{
592 int32_t smalllhs;
593 isl_sioimath_scratchspace_t lhsscratch;
594
595 if (isl_sioimath_decode_small(lhs, &smalllhs) &&
596 (rhs <= (uint64_t) INT64_MAX(9223372036854775807L) - (uint64_t) ISL_SIOIMATH_SMALL_MAX(2147483647))) {
597 isl_sioimath_set_int64(dst, (int64_t) smalllhs + rhs);
598 return;
599 }
600
601 impz_add_ui(isl_sioimath_reinit_big(dst),
602 isl_sioimath_bigarg_src(lhs, &lhsscratch), rhs);
603 isl_sioimath_try_demote(dst);
604}
605
606/* Subtract an unsigned long.
607 *
608 * On LP64 unsigned long exceeds the range of an int64_t. If
609 * ISL_SIOIMATH_SMALL_MIN-rhs>=INT64_MIN we can do the calculation using int64_t
610 * without risking an overflow.
611 */
612inline void isl_sioimath_sub_ui(isl_sioimath_ptr dst, isl_sioimath lhs,
613 unsigned long rhs)
614{
615 int32_t smalllhs;
616 isl_sioimath_scratchspace_t lhsscratch;
617
618 if (isl_sioimath_decode_small(lhs, &smalllhs) &&
619 (rhs < (uint64_t) INT64_MIN(-9223372036854775807L -1) - (uint64_t) ISL_SIOIMATH_SMALL_MIN(-(2147483647)))) {
620 isl_sioimath_set_int64(dst, (int64_t) smalllhs - rhs);
621 return;
622 }
623
624 impz_sub_ui(isl_sioimath_reinit_big(dst),
625 isl_sioimath_bigarg_src(lhs, &lhsscratch), rhs);
626 isl_sioimath_try_demote(dst);
627}
628
629/* Sum of two isl_ints.
630 */
631inline void isl_sioimath_add(isl_sioimath_ptr dst, isl_sioimath_src lhs,
632 isl_sioimath_src rhs)
633{
634 isl_sioimath_scratchspace_t scratchlhs, scratchrhs;
635 int32_t smalllhs, smallrhs;
636
637 if (isl_sioimath_decode_small(lhs, &smalllhs) &&
638 isl_sioimath_decode_small(rhs, &smallrhs)) {
639 isl_sioimath_set_int64(
640 dst, (int64_t) smalllhs + (int64_t) smallrhs);
641 return;
642 }
643
644 mp_int_add(isl_sioimath_bigarg_src(lhs, &scratchlhs),
645 isl_sioimath_bigarg_src(rhs, &scratchrhs),
646 isl_sioimath_reinit_big(dst));
647 isl_sioimath_try_demote(dst);
648}
649
650/* Subtract two isl_ints.
651 */
652inline void isl_sioimath_sub(isl_sioimath_ptr dst, isl_sioimath_src lhs,
653 isl_sioimath_src rhs)
654{
655 isl_sioimath_scratchspace_t scratchlhs, scratchrhs;
656 int32_t smalllhs, smallrhs;
657
658 if (isl_sioimath_decode_small(lhs, &smalllhs) &&
659 isl_sioimath_decode_small(rhs, &smallrhs)) {
660 isl_sioimath_set_int64(
661 dst, (int64_t) smalllhs - (int64_t) smallrhs);
662 return;
663 }
664
665 mp_int_sub(isl_sioimath_bigarg_src(lhs, &scratchlhs),
666 isl_sioimath_bigarg_src(rhs, &scratchrhs),
667 isl_sioimath_reinit_big(dst));
668 isl_sioimath_try_demote(dst);
669}
670
671/* Multiply two isl_ints.
672 */
673inline void isl_sioimath_mul(isl_sioimath_ptr dst, isl_sioimath_src lhs,
674 isl_sioimath_src rhs)
675{
676 isl_sioimath_scratchspace_t scratchlhs, scratchrhs;
677 int32_t smalllhs, smallrhs;
678
679 if (isl_sioimath_decode_small(lhs, &smalllhs) &&
680 isl_sioimath_decode_small(rhs, &smallrhs)) {
681 isl_sioimath_set_int64(
682 dst, (int64_t) smalllhs * (int64_t) smallrhs);
683 return;
684 }
685
686 mp_int_mul(isl_sioimath_bigarg_src(lhs, &scratchlhs),
687 isl_sioimath_bigarg_src(rhs, &scratchrhs),
688 isl_sioimath_reinit_big(dst));
689 isl_sioimath_try_demote(dst);
690}
691
692/* Shift lhs by rhs bits to the left and store the result in dst. Effectively,
693 * this operation computes 'lhs * 2^rhs'.
694 */
695inline void isl_sioimath_mul_2exp(isl_sioimath_ptr dst, isl_sioimath lhs,
696 unsigned long rhs)
697{
698 isl_sioimath_scratchspace_t scratchlhs;
699 int32_t smalllhs;
700
701 if (isl_sioimath_decode_small(lhs, &smalllhs) && (rhs <= 32ul)) {
702 isl_sioimath_set_int64(dst, ((int64_t) smalllhs) << rhs);
703 return;
704 }
705
706 mp_int_mul_pow2(isl_sioimath_bigarg_src(lhs, &scratchlhs), rhs,
707 isl_sioimath_reinit_big(dst));
708}
709
710/* Multiply an isl_int and a signed long.
711 */
712inline void isl_sioimath_mul_si(isl_sioimath_ptr dst, isl_sioimath lhs,
713 signed long rhs)
714{
715 isl_sioimath_scratchspace_t scratchlhs, scratchrhs;
716 int32_t smalllhs;
717
718 if (isl_sioimath_decode_small(lhs, &smalllhs) && (rhs > LONG_MIN(-9223372036854775807L -1L)) &&
719 (labs(rhs) <= UINT32_MAX(4294967295U))) {
720 isl_sioimath_set_int64(dst, (int64_t) smalllhs * (int64_t) rhs);
721 return;
722 }
723
724 mp_int_mul(isl_sioimath_bigarg_src(lhs, &scratchlhs),
725 isl_sioimath_siarg_src(rhs, &scratchrhs),
726 isl_sioimath_reinit_big(dst));
727 isl_sioimath_try_demote(dst);
728}
729
730/* Multiply an isl_int and an unsigned long.
731 */
732inline void isl_sioimath_mul_ui(isl_sioimath_ptr dst, isl_sioimath lhs,
733 unsigned long rhs)
734{
735 isl_sioimath_scratchspace_t scratchlhs, scratchrhs;
736 int32_t smalllhs;
737
738 if (isl_sioimath_decode_small(lhs, &smalllhs) && (rhs <= UINT32_MAX(4294967295U))) {
739 isl_sioimath_set_int64(dst, (int64_t) smalllhs * (int64_t) rhs);
740 return;
741 }
742
743 mp_int_mul(isl_sioimath_bigarg_src(lhs, &scratchlhs),
744 isl_sioimath_uiarg_src(rhs, &scratchrhs),
745 isl_sioimath_reinit_big(dst));
746 isl_sioimath_try_demote(dst);
747}
748
749/* Compute the power of an isl_int to an unsigned long.
750 * Always let IMath do it; the result is unlikely to be small except in some
751 * special cases.
752 * Note: 0^0 == 1
753 */
754inline void isl_sioimath_pow_ui(isl_sioimath_ptr dst, isl_sioimath_src lhs,
755 unsigned long rhs)
756{
757 isl_sioimath_scratchspace_t scratchlhs, scratchrhs;
758 int32_t smalllhs;
759
760 switch (rhs) {
761 case 0:
762 isl_sioimath_set_small(dst, 1);
763 return;
764 case 1:
765 isl_sioimath_set(dst, lhs);
766 return;
767 case 2:
768 isl_sioimath_mul(dst, lhs, lhs);
769 return;
770 }
771
772 if (isl_sioimath_decode_small(lhs, &smalllhs)) {
773 switch (smalllhs) {
774 case 0:
775 isl_sioimath_set_small(dst, 0);
776 return;
777 case 1:
778 isl_sioimath_set_small(dst, 1);
779 return;
780 case 2:
781 isl_sioimath_set_small(dst, 1);
782 isl_sioimath_mul_2exp(dst, *dst, rhs);
783 return;
784 default:
785 if ((MP_SMALL_MIN(-9223372036854775807L -1L) <= rhs) && (rhs <= MP_SMALL_MAX9223372036854775807L)) {
786 mp_int_expt_value(smalllhs, rhs,
787 isl_sioimath_reinit_big(dst));
788 isl_sioimath_try_demote(dst);
789 return;
790 }
791 }
792 }
793
794 mp_int_expt_full(isl_sioimath_bigarg_src(lhs, &scratchlhs),
795 isl_sioimath_uiarg_src(rhs, &scratchrhs),
796 isl_sioimath_reinit_big(dst));
797 isl_sioimath_try_demote(dst);
798}
799
800/* Fused multiply-add.
801 */
802inline void isl_sioimath_addmul(isl_sioimath_ptr dst, isl_sioimath_src lhs,
803 isl_sioimath_src rhs)
804{
805 isl_sioimath tmp;
806 isl_sioimath_init(&tmp);
807 isl_sioimath_mul(&tmp, lhs, rhs);
808 isl_sioimath_add(dst, *dst, tmp);
809 isl_sioimath_clear(&tmp);
810}
811
812/* Fused multiply-add with an unsigned long.
813 */
814inline void isl_sioimath_addmul_ui(isl_sioimath_ptr dst, isl_sioimath_src lhs,
815 unsigned long rhs)
816{
817 isl_sioimath tmp;
818 isl_sioimath_init(&tmp);
819 isl_sioimath_mul_ui(&tmp, lhs, rhs);
820 isl_sioimath_add(dst, *dst, tmp);
821 isl_sioimath_clear(&tmp);
822}
823
824/* Fused multiply-subtract.
825 */
826inline void isl_sioimath_submul(isl_sioimath_ptr dst, isl_sioimath_src lhs,
827 isl_sioimath_src rhs)
828{
829 isl_sioimath tmp;
830 isl_sioimath_init(&tmp);
831 isl_sioimath_mul(&tmp, lhs, rhs);
832 isl_sioimath_sub(dst, *dst, tmp);
833 isl_sioimath_clear(&tmp);
834}
835
836/* Fused multiply-add with an unsigned long.
837 */
838inline void isl_sioimath_submul_ui(isl_sioimath_ptr dst, isl_sioimath_src lhs,
839 unsigned long rhs)
840{
841 isl_sioimath tmp;
842 isl_sioimath_init(&tmp);
843 isl_sioimath_mul_ui(&tmp, lhs, rhs);
844 isl_sioimath_sub(dst, *dst, tmp);
845 isl_sioimath_clear(&tmp);
846}
847
848void isl_sioimath_gcd(isl_sioimath_ptr dst, isl_sioimath_src lhs,
849 isl_sioimath_src rhs);
850void isl_sioimath_lcm(isl_sioimath_ptr dst, isl_sioimath_src lhs,
851 isl_sioimath_src rhs);
852
853/* Divide lhs by rhs, rounding to zero (Truncate).
854 */
855inline void isl_sioimath_tdiv_q(isl_sioimath_ptr dst, isl_sioimath_src lhs,
856 isl_sioimath_src rhs)
857{
858 isl_sioimath_scratchspace_t lhsscratch, rhsscratch;
859 int32_t lhssmall, rhssmall;
860
861 if (isl_sioimath_decode_small(lhs, &lhssmall) &&
862 isl_sioimath_decode_small(rhs, &rhssmall)) {
863 isl_sioimath_set_small(dst, lhssmall / rhssmall);
864 return;
865 }
866
867 mp_int_div(isl_sioimath_bigarg_src(lhs, &lhsscratch),
868 isl_sioimath_bigarg_src(rhs, &rhsscratch),
869 isl_sioimath_reinit_big(dst), NULL((void*)0));
870 isl_sioimath_try_demote(dst);
871 return;
872}
873
874/* Divide lhs by an unsigned long rhs, rounding to zero (Truncate).
875 */
876inline void isl_sioimath_tdiv_q_ui(isl_sioimath_ptr dst, isl_sioimath_src lhs,
877 unsigned long rhs)
878{
879 isl_sioimath_scratchspace_t lhsscratch, rhsscratch;
880 int32_t lhssmall;
881
882 if (isl_sioimath_is_small(lhs) && (rhs <= (unsigned long) INT32_MAX(2147483647))) {
883 lhssmall = isl_sioimath_get_small(lhs);
884 isl_sioimath_set_small(dst, lhssmall / (int32_t) rhs);
885 return;
886 }
887
888 if (rhs <= MP_SMALL_MAX9223372036854775807L) {
889 mp_int_div_value(isl_sioimath_bigarg_src(lhs, &lhsscratch), rhs,
890 isl_sioimath_reinit_big(dst), NULL((void*)0));
891 isl_sioimath_try_demote(dst);
892 return;
893 }
894
895 mp_int_div(isl_sioimath_bigarg_src(lhs, &lhsscratch),
896 isl_sioimath_uiarg_src(rhs, &rhsscratch),
897 isl_sioimath_reinit_big(dst), NULL((void*)0));
898 isl_sioimath_try_demote(dst);
899}
900
901/* Divide lhs by rhs, rounding to positive infinity (Ceil).
902 */
903inline void isl_sioimath_cdiv_q(isl_sioimath_ptr dst, isl_sioimath_src lhs,
904 isl_sioimath_src rhs)
905{
906 int32_t lhssmall, rhssmall;
907 isl_sioimath_scratchspace_t lhsscratch, rhsscratch;
908 int32_t q;
909
910 if (isl_sioimath_decode_small(lhs, &lhssmall) &&
911 isl_sioimath_decode_small(rhs, &rhssmall)) {
912 if ((lhssmall >= 0) && (rhssmall >= 0))
913 q = ((int64_t) lhssmall + (int64_t) rhssmall - 1) /
914 rhssmall;
915 else if ((lhssmall < 0) && (rhssmall < 0))
916 q = ((int64_t) lhssmall + (int64_t) rhssmall + 1) /
917 rhssmall;
918 else
919 q = lhssmall / rhssmall;
920 isl_sioimath_set_small(dst, q);
921 return;
922 }
923
924 impz_cdiv_q(isl_sioimath_reinit_big(dst),
925 isl_sioimath_bigarg_src(lhs, &lhsscratch),
926 isl_sioimath_bigarg_src(rhs, &rhsscratch));
927 isl_sioimath_try_demote(dst);
928}
929
930/* Compute the division of lhs by a rhs of type unsigned long, rounding towards
931 * positive infinity (Ceil).
932 */
933inline void isl_sioimath_cdiv_q_ui(isl_sioimath_ptr dst, isl_sioimath_src lhs,
934 unsigned long rhs)
935{
936 isl_sioimath_scratchspace_t lhsscratch, rhsscratch;
937 int32_t lhssmall, q;
938
939 if (isl_sioimath_decode_small(lhs, &lhssmall) && (rhs <= INT32_MAX(2147483647))) {
940 if (lhssmall >= 0)
941 q = ((int64_t) lhssmall + ((int64_t) rhs - 1)) /
942 (int64_t) rhs;
943 else
944 q = lhssmall / (int32_t) rhs;
945 isl_sioimath_set_small(dst, q);
946 return;
947 }
948
949 impz_cdiv_q(isl_sioimath_reinit_big(dst),
950 isl_sioimath_bigarg_src(lhs, &lhsscratch),
951 isl_sioimath_uiarg_src(rhs, &rhsscratch));
952 isl_sioimath_try_demote(dst);
953}
954
955/* Divide lhs by rhs, rounding to negative infinity (Floor).
956 */
957inline void isl_sioimath_fdiv_q(isl_sioimath_ptr dst, isl_sioimath_src lhs,
958 isl_sioimath_src rhs)
959{
960 isl_sioimath_scratchspace_t lhsscratch, rhsscratch;
961 int32_t lhssmall, rhssmall;
962 int32_t q;
963
964 if (isl_sioimath_decode_small(lhs, &lhssmall) &&
965 isl_sioimath_decode_small(rhs, &rhssmall)) {
966 if ((lhssmall < 0) && (rhssmall >= 0))
967 q = ((int64_t) lhssmall - ((int64_t) rhssmall - 1)) /
968 rhssmall;
969 else if ((lhssmall >= 0) && (rhssmall < 0))
970 q = ((int64_t) lhssmall - ((int64_t) rhssmall + 1)) /
971 rhssmall;
972 else
973 q = lhssmall / rhssmall;
974 isl_sioimath_set_small(dst, q);
975 return;
976 }
977
978 impz_fdiv_q(isl_sioimath_reinit_big(dst),
979 isl_sioimath_bigarg_src(lhs, &lhsscratch),
980 isl_sioimath_bigarg_src(rhs, &rhsscratch));
981 isl_sioimath_try_demote(dst);
982}
983
984/* Compute the division of lhs by a rhs of type unsigned long, rounding towards
985 * negative infinity (Floor).
986 */
987inline void isl_sioimath_fdiv_q_ui(isl_sioimath_ptr dst, isl_sioimath_src lhs,
988 unsigned long rhs)
989{
990 isl_sioimath_scratchspace_t lhsscratch, rhsscratch;
991 int32_t lhssmall, q;
992
993 if (isl_sioimath_decode_small(lhs, &lhssmall) && (rhs <= INT32_MAX(2147483647))) {
994 if (lhssmall >= 0)
995 q = (uint32_t) lhssmall / rhs;
996 else
997 q = ((int64_t) lhssmall - ((int64_t) rhs - 1)) /
998 (int64_t) rhs;
999 isl_sioimath_set_small(dst, q);
1000 return;
1001 }
1002
1003 impz_fdiv_q(isl_sioimath_reinit_big(dst),
1004 isl_sioimath_bigarg_src(lhs, &lhsscratch),
1005 isl_sioimath_uiarg_src(rhs, &rhsscratch));
1006 isl_sioimath_try_demote(dst);
1007}
1008
1009/* Get the remainder of: lhs divided by rhs rounded towards negative infinite
1010 * (Floor).
1011 */
1012inline void isl_sioimath_fdiv_r(isl_sioimath_ptr dst, isl_sioimath_src lhs,
1013 isl_sioimath_src rhs)
1014{
1015 isl_sioimath_scratchspace_t lhsscratch, rhsscratch;
1016 int64_t lhssmall, rhssmall;
1017 int32_t r;
1018
1019 if (isl_sioimath_is_small(lhs) && isl_sioimath_is_small(rhs)) {
1020 lhssmall = isl_sioimath_get_small(lhs);
1021 rhssmall = isl_sioimath_get_small(rhs);
1022 r = (rhssmall + lhssmall % rhssmall) % rhssmall;
1023 isl_sioimath_set_small(dst, r);
1024 return;
1025 }
1026
1027 impz_fdiv_r(isl_sioimath_reinit_big(dst),
1028 isl_sioimath_bigarg_src(lhs, &lhsscratch),
1029 isl_sioimath_bigarg_src(rhs, &rhsscratch));
1030 isl_sioimath_try_demote(dst);
1031}
1032
1033void isl_sioimath_read(isl_sioimath_ptr dst, const char *str);
1034
1035/* Return:
1036 * +1 for a positive number
1037 * -1 for a negative number
1038 * 0 if the number is zero
1039 */
1040inline int isl_sioimath_sgn(isl_sioimath_src arg)
1041{
1042 int32_t small;
1043
1044 if (isl_sioimath_decode_small(arg, &small))
1045 return (small > 0) - (small < 0);
1046
1047 return mp_int_compare_zero(isl_sioimath_get_big(arg));
1048}
1049
1050/* Return:
1051 * +1 if lhs > rhs
1052 * -1 if lhs < rhs
1053 * 0 if lhs = rhs
1054 */
1055inline int isl_sioimath_cmp(isl_sioimath_src lhs, isl_sioimath_src rhs)
1056{
1057 isl_sioimath_scratchspace_t lhsscratch, rhsscratch;
1058 int32_t lhssmall, rhssmall;
1059
1060 if (isl_sioimath_decode_small(lhs, &lhssmall) &&
1061 isl_sioimath_decode_small(rhs, &rhssmall))
1062 return (lhssmall > rhssmall) - (lhssmall < rhssmall);
1063
1064 if (isl_sioimath_decode_small(rhs, &rhssmall))
1065 return mp_int_compare_value(
1066 isl_sioimath_bigarg_src(lhs, &lhsscratch), rhssmall);
1067
1068 if (isl_sioimath_decode_small(lhs, &lhssmall))
1069 return -mp_int_compare_value(
1070 isl_sioimath_bigarg_src(rhs, &rhsscratch), lhssmall);
1071
1072 return mp_int_compare(
1073 isl_sioimath_get_big(lhs), isl_sioimath_get_big(rhs));
1074}
1075
1076/* As isl_sioimath_cmp, but with signed long rhs.
1077 */
1078inline int isl_sioimath_cmp_si(isl_sioimath_src lhs, signed long rhs)
1079{
1080 int32_t lhssmall;
1081
1082 if (isl_sioimath_decode_small(lhs, &lhssmall))
1083 return (lhssmall > rhs) - (lhssmall < rhs);
1084
1085 return mp_int_compare_value(isl_sioimath_get_big(lhs), rhs);
1086}
1087
1088/* Return:
1089 * +1 if |lhs| > |rhs|
1090 * -1 if |lhs| < |rhs|
1091 * 0 if |lhs| = |rhs|
1092 */
1093inline int isl_sioimath_abs_cmp(isl_sioimath_src lhs, isl_sioimath_src rhs)
1094{
1095 isl_sioimath_scratchspace_t lhsscratch, rhsscratch;
1096 int32_t lhssmall, rhssmall;
1097
1098 if (isl_sioimath_decode_small(lhs, &lhssmall) &&
1099 isl_sioimath_decode_small(rhs, &rhssmall)) {
1100 lhssmall = labs(lhssmall);
1101 rhssmall = labs(rhssmall);
1102 return (lhssmall > rhssmall) - (lhssmall < rhssmall);
1103 }
1104
1105 return mp_int_compare_unsigned(
1106 isl_sioimath_bigarg_src(lhs, &lhsscratch),
1107 isl_sioimath_bigarg_src(rhs, &rhsscratch));
1108}
1109
1110/* Return whether lhs is divisible by rhs.
1111 * In particular, can rhs be multiplied by some integer to result in lhs?
1112 * If rhs is zero, then this means lhs has to be zero too.
1113 */
1114inline int isl_sioimath_is_divisible_by(isl_sioimath_src lhs,
1115 isl_sioimath_src rhs)
1116{
1117 isl_sioimath_scratchspace_t lhsscratch, rhsscratch;
1118 int32_t lhssmall, rhssmall;
1119 mpz_t rem;
1120 int cmp;
1121
1122 if (isl_sioimath_sgn(rhs) == 0)
1123 return isl_sioimath_sgn(lhs) == 0;
1124
1125 if (isl_sioimath_decode_small(lhs, &lhssmall) &&
1126 isl_sioimath_decode_small(rhs, &rhssmall))
1127 return lhssmall % rhssmall == 0;
1128
1129 if (isl_sioimath_decode_small(rhs, &rhssmall))
1130 return mp_int_divisible_value(
1131 isl_sioimath_bigarg_src(lhs, &lhsscratch), rhssmall);
1132
1133 mp_int_init(&rem);
1134 mp_int_div(isl_sioimath_bigarg_src(lhs, &lhsscratch),
1135 isl_sioimath_bigarg_src(rhs, &rhsscratch), NULL((void*)0), &rem);
1136 cmp = mp_int_compare_zero(&rem);
1137 mp_int_clear(&rem);
1138 return cmp == 0;
1139}
1140
1141/* Return a hash code of an isl_sioimath.
1142 * The hash code for a number in small and big representation must be identical
1143 * on the same machine because small representation if not obligatory if fits.
1144 */
1145inline uint32_t isl_sioimath_hash(isl_sioimath_src arg, uint32_t hash)
1146{
1147 int32_t small;
1148 int i;
1149 uint32_t num;
1150 mp_digit digits[(sizeof(uint32_t) + sizeof(mp_digit) - 1) /
1151 sizeof(mp_digit)];
1152 mp_size used;
1153 const unsigned char *digitdata = (const unsigned char *) &digits;
1154
1155 if (isl_sioimath_decode_small(arg, &small)) {
1156 if (small < 0)
1157 isl_hash_byte(hash, 0xFF)do { hash *= 16777619; hash ^= 0xFF; } while(0);
1158 num = labs(small);
1159
1160 isl_siomath_uint32_to_digits(num, digits, &used);
1161 for (i = 0; i < used * sizeof(mp_digit); i += 1)
1162 isl_hash_byte(hash, digitdata[i])do { hash *= 16777619; hash ^= digitdata[i]; } while(0);
1163 return hash;
1164 }
1165
1166 return isl_imath_hash(isl_sioimath_get_big(arg), hash);
1167}
1168
1169/* Return the number of digits in a number of the given base or more, i.e. the
1170 * string length without sign and null terminator.
1171 *
1172 * Current implementation for small representation returns the maximal number
1173 * of binary digits in that representation, which can be much larger than the
1174 * smallest possible solution.
1175 */
1176inline size_t isl_sioimath_sizeinbase(isl_sioimath_src arg, int base)
1177{
1178 int32_t small;
1179
1180 if (isl_sioimath_decode_small(arg, &small))
1181 return sizeof(int32_t) * CHAR_BIT8 - 1;
1182
1183 return impz_sizeinbase(isl_sioimath_get_big(arg), base);
1184}
1185
1186void isl_sioimath_print(FILE *out, isl_sioimath_src i, int width);
1187void isl_sioimath_dump(isl_sioimath_src arg);
1188
1189typedef isl_sioimath isl_int[1];
1190#define isl_int_init(i)isl_sioimath_init((i)) isl_sioimath_init((i))
1191#define isl_int_clear(i)isl_sioimath_clear((i)) isl_sioimath_clear((i))
1192
1193#define isl_int_set(r, i)isl_sioimath_set((r), *(i)) isl_sioimath_set((r), *(i))
1194#define isl_int_set_si(r, i)isl_sioimath_set_si((r), i) isl_sioimath_set_si((r), i)
1195#define isl_int_set_ui(r, i)isl_sioimath_set_ui((r), i) isl_sioimath_set_ui((r), i)
1196#define isl_int_fits_slong(r)isl_sioimath_fits_slong(*(r)) isl_sioimath_fits_slong(*(r))
1197#define isl_int_get_si(r)isl_sioimath_get_si(*(r)) isl_sioimath_get_si(*(r))
1198#define isl_int_fits_ulong(r)isl_sioimath_fits_ulong(*(r)) isl_sioimath_fits_ulong(*(r))
1199#define isl_int_get_ui(r)isl_sioimath_get_ui(*(r)) isl_sioimath_get_ui(*(r))
1200#define isl_int_get_d(r)isl_sioimath_get_d(*(r)) isl_sioimath_get_d(*(r))
1201#define isl_int_get_str(r)isl_sioimath_get_str(*(r)) isl_sioimath_get_str(*(r))
1202#define isl_int_abs(r, i)isl_sioimath_abs((r), *(i)) isl_sioimath_abs((r), *(i))
1203#define isl_int_neg(r, i)isl_sioimath_neg((r), *(i)) isl_sioimath_neg((r), *(i))
1204#define isl_int_swap(i, j)isl_sioimath_swap((i), (j)) isl_sioimath_swap((i), (j))
1205#define isl_int_swap_or_set(i, j)isl_sioimath_swap((i), (j)) isl_sioimath_swap((i), (j))
1206#define isl_int_add_ui(r, i, j)isl_sioimath_add_ui((r), *(i), j) isl_sioimath_add_ui((r), *(i), j)
1207#define isl_int_sub_ui(r, i, j)isl_sioimath_sub_ui((r), *(i), j) isl_sioimath_sub_ui((r), *(i), j)
1208
1209#define isl_int_add(r, i, j)isl_sioimath_add((r), *(i), *(j)) isl_sioimath_add((r), *(i), *(j))
1210#define isl_int_sub(r, i, j)isl_sioimath_sub((r), *(i), *(j)) isl_sioimath_sub((r), *(i), *(j))
1211#define isl_int_mul(r, i, j)isl_sioimath_mul((r), *(i), *(j)) isl_sioimath_mul((r), *(i), *(j))
1212#define isl_int_mul_2exp(r, i, j)isl_sioimath_mul_2exp((r), *(i), j) isl_sioimath_mul_2exp((r), *(i), j)
1213#define isl_int_mul_si(r, i, j)isl_sioimath_mul_si((r), *(i), j) isl_sioimath_mul_si((r), *(i), j)
1214#define isl_int_mul_ui(r, i, j)isl_sioimath_mul_ui((r), *(i), j) isl_sioimath_mul_ui((r), *(i), j)
1215#define isl_int_pow_ui(r, i, j)isl_sioimath_pow_ui((r), *(i), j) isl_sioimath_pow_ui((r), *(i), j)
1216#define isl_int_addmul(r, i, j)isl_sioimath_addmul((r), *(i), *(j)) isl_sioimath_addmul((r), *(i), *(j))
1217#define isl_int_addmul_ui(r, i, j)isl_sioimath_addmul_ui((r), *(i), j) isl_sioimath_addmul_ui((r), *(i), j)
1218#define isl_int_submul(r, i, j)isl_sioimath_submul((r), *(i), *(j)) isl_sioimath_submul((r), *(i), *(j))
1219#define isl_int_submul_ui(r, i, j)isl_sioimath_submul_ui((r), *(i), j) isl_sioimath_submul_ui((r), *(i), j)
1220
1221#define isl_int_gcd(r, i, j)isl_sioimath_gcd((r), *(i), *(j)) isl_sioimath_gcd((r), *(i), *(j))
1222#define isl_int_lcm(r, i, j)isl_sioimath_lcm((r), *(i), *(j)) isl_sioimath_lcm((r), *(i), *(j))
1223#define isl_int_divexact(r, i, j)isl_sioimath_tdiv_q((r), *(i), *(j)) isl_sioimath_tdiv_q((r), *(i), *(j))
1224#define isl_int_divexact_ui(r, i, j)isl_sioimath_tdiv_q_ui((r), *(i), j) isl_sioimath_tdiv_q_ui((r), *(i), j)
1225#define isl_int_tdiv_q(r, i, j)isl_sioimath_tdiv_q((r), *(i), *(j)) isl_sioimath_tdiv_q((r), *(i), *(j))
1226#define isl_int_cdiv_q(r, i, j)isl_sioimath_cdiv_q((r), *(i), *(j)) isl_sioimath_cdiv_q((r), *(i), *(j))
1227#define isl_int_cdiv_q_ui(r, i, j)isl_sioimath_cdiv_q_ui((r), *(i), j) isl_sioimath_cdiv_q_ui((r), *(i), j)
1228#define isl_int_fdiv_q(r, i, j)isl_sioimath_fdiv_q((r), *(i), *(j)) isl_sioimath_fdiv_q((r), *(i), *(j))
1229#define isl_int_fdiv_r(r, i, j)isl_sioimath_fdiv_r((r), *(i), *(j)) isl_sioimath_fdiv_r((r), *(i), *(j))
1230#define isl_int_fdiv_q_ui(r, i, j)isl_sioimath_fdiv_q_ui((r), *(i), j) isl_sioimath_fdiv_q_ui((r), *(i), j)
1231
1232#define isl_int_read(r, s)isl_sioimath_read((r), s) isl_sioimath_read((r), s)
1233#define isl_int_sgn(i)isl_sioimath_sgn(*(i)) isl_sioimath_sgn(*(i))
1234#define isl_int_cmp(i, j)isl_sioimath_cmp(*(i), *(j)) isl_sioimath_cmp(*(i), *(j))
1235#define isl_int_cmp_si(i, si)isl_sioimath_cmp_si(*(i), si) isl_sioimath_cmp_si(*(i), si)
1236#define isl_int_eq(i, j)(isl_sioimath_cmp(*(i), *(j)) == 0) (isl_sioimath_cmp(*(i), *(j)) == 0)
1237#define isl_int_ne(i, j)(isl_sioimath_cmp(*(i), *(j)) != 0) (isl_sioimath_cmp(*(i), *(j)) != 0)
1238#define isl_int_lt(i, j)(isl_sioimath_cmp(*(i), *(j)) < 0) (isl_sioimath_cmp(*(i), *(j)) < 0)
1239#define isl_int_le(i, j)(isl_sioimath_cmp(*(i), *(j)) <= 0) (isl_sioimath_cmp(*(i), *(j)) <= 0)
1240#define isl_int_gt(i, j)(isl_sioimath_cmp(*(i), *(j)) > 0) (isl_sioimath_cmp(*(i), *(j)) > 0)
1241#define isl_int_ge(i, j)(isl_sioimath_cmp(*(i), *(j)) >= 0) (isl_sioimath_cmp(*(i), *(j)) >= 0)
1242#define isl_int_abs_cmp(i, j)isl_sioimath_abs_cmp(*(i), *(j)) isl_sioimath_abs_cmp(*(i), *(j))
1243#define isl_int_abs_eq(i, j)(isl_sioimath_abs_cmp(*(i), *(j)) == 0) (isl_sioimath_abs_cmp(*(i), *(j)) == 0)
1244#define isl_int_abs_ne(i, j)(isl_sioimath_abs_cmp(*(i), *(j)) != 0) (isl_sioimath_abs_cmp(*(i), *(j)) != 0)
1245#define isl_int_abs_lt(i, j)(isl_sioimath_abs_cmp(*(i), *(j)) < 0) (isl_sioimath_abs_cmp(*(i), *(j)) < 0)
1246#define isl_int_abs_gt(i, j)(isl_sioimath_abs_cmp(*(i), *(j)) > 0) (isl_sioimath_abs_cmp(*(i), *(j)) > 0)
1247#define isl_int_abs_ge(i, j)(isl_sioimath_abs_cmp(*(i), *(j)) >= 0) (isl_sioimath_abs_cmp(*(i), *(j)) >= 0)
1248#define isl_int_is_divisible_by(i, j)isl_sioimath_is_divisible_by(*(i), *(j)) isl_sioimath_is_divisible_by(*(i), *(j))
1249
1250#define isl_int_hash(v, h)isl_sioimath_hash(*(v), h) isl_sioimath_hash(*(v), h)
1251#define isl_int_free_str(s)free(s) free(s)
1252#define isl_int_print(out, i, width)isl_sioimath_print(out, *(i), width) isl_sioimath_print(out, *(i), width)
1253
1254#endif /* ISL_INT_SIOIMATH_H */