Polly 23.0.0git
isl_tab.c
Go to the documentation of this file.
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);
41 if (!tab)
42 return NULL;
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);
47 if (n_var && !tab->var)
48 goto error;
49 tab->con = isl_alloc_array(ctx, struct isl_tab_var, n_row);
50 if (n_row && !tab->con)
51 goto error;
52 tab->col_var = isl_alloc_array(ctx, int, n_var);
53 if (n_var && !tab->col_var)
54 goto error;
55 tab->row_var = isl_alloc_array(ctx, int, n_row);
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;
87 tab->bottom.next = NULL;
88 tab->top = &tab->bottom;
89
90 tab->n_zero = 0;
91 tab->n_unbounded = 0;
92 tab->basis = NULL;
93
94 return tab;
95error:
96 isl_tab_free(tab);
97 return NULL;
98}
99
101{
102 return tab ? isl_mat_get_ctx(tab->mat) : NULL;
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,
118 struct isl_tab_var, tab->max_con + n_new);
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,
132 int, tab->mat->n_row);
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,
139 enum isl_tab_row_sign, tab->mat->n_row);
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,
158 struct isl_tab_var, tab->n_var + n_new);
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,
173 int, tab->n_col + n_new);
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) {
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);
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;
231
232 off = 2 + tab->M;
233 dup = isl_calloc_type(tab->mat->ctx, struct isl_tab);
234 if (!dup)
235 return NULL;
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);
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);
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);
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);
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,
261 tab->mat->n_row);
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,
272 tab->samples->n_row);
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 dup->cone = tab->cone;
296 dup->bottom.type = isl_tab_undo_bottom;
297 dup->bottom.next = NULL;
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:
307 return NULL;
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 */
324 __isl_keep 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;
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;
443
444 isl_assert(tab1->mat->ctx, tab1->M == tab2->M, return NULL);
445 isl_assert(tab1->mat->ctx, tab1->rational == tab2->rational, return NULL);
446 isl_assert(tab1->mat->ctx, tab1->cone == tab2->cone, return NULL);
447 isl_assert(tab1->mat->ctx, !tab1->row_sign, return NULL);
448 isl_assert(tab1->mat->ctx, !tab2->row_sign, return NULL);
449 isl_assert(tab1->mat->ctx, tab1->n_param == 0, return NULL);
450 isl_assert(tab1->mat->ctx, tab2->n_param == 0, return NULL);
451 isl_assert(tab1->mat->ctx, tab1->n_div == 0, return NULL);
452 isl_assert(tab1->mat->ctx, tab2->n_div == 0, return NULL);
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);
460 if (!prod)
461 return NULL;
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,
468 tab1->max_var + tab2->max_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,
482 tab1->max_con + tab2->max_con);
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,
496 tab1->n_col + tab2->n_col);
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,
513 tab1->mat->n_row + tab2->mat->n_row);
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;
530 prod->sample_index = NULL;
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;
545 prod->need_undo = 0;
546 prod->in_undo = 0;
547 prod->M = tab1->M;
548 prod->cone = tab1->cone;
550 prod->bottom.next = NULL;
551 prod->top = &prod->bottom;
552
553 prod->n_zero = 0;
554 prod->n_unbounded = 0;
555 prod->basis = NULL;
556
557 return prod;
558error:
559 isl_tab_free(prod);
560 return NULL;
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 */
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]))
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 */
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]))
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]);
630 isl_int_submul(*t, tab->mat->row[r2][2], tab->mat->row[r1][off+c]);
631 s = isl_int_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]);
636 isl_int_submul(*t, tab->mat->row[r2][1], tab->mat->row[r1][off + c]);
637 return isl_int_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
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]) >= 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 }
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);
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]))
720 continue;
721 if (isl_int_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]);
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]))
751 return 0;
752 if (tab->strict_redundant && isl_int_is_zero(tab->mat->row[row][1]))
753 return 0;
754 if (tab->M && isl_int_is_neg(tab->mat->row[row][2]))
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]))
759 continue;
760 if (tab->col_var[i] >= 0)
761 return 0;
762 if (isl_int_is_neg(tab->mat->row[row][off + i]))
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,
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,
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);
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;
821 return isl_stat_error;
822}
823
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
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 */
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);
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
859{
860 union isl_tab_undo_val u;
861 u.callback = callback;
862 return push_union(tab, isl_tab_undo_callback, u);
863}
864
865/* Push a record onto the undo stack indicating that inequality "ineq"
866 * has been turned into an equality constraint (in the first position).
867 */
868static isl_stat isl_tab_push_ineq_to_eq(struct isl_tab *tab, int ineq)
869{
870 union isl_tab_undo_val u = { .n = ineq };
871
872 return push_union(tab, isl_tab_undo_ineq_to_eq, u);
873}
874
876{
877 if (!tab)
878 return NULL;
879
880 tab->n_sample = 0;
881 tab->n_outside = 0;
882 tab->samples = isl_mat_alloc(tab->mat->ctx, 1, 1 + tab->n_var);
883 if (!tab->samples)
884 goto error;
885 tab->sample_index = isl_alloc_array(tab->mat->ctx, int, 1);
886 if (!tab->sample_index)
887 goto error;
888 return tab;
889error:
890 isl_tab_free(tab);
891 return NULL;
892}
893
895{
896 if (!tab || !sample)
897 goto error;
898
899 if (tab->n_sample + 1 > tab->samples->n_row) {
900 int *t = isl_realloc_array(tab->mat->ctx,
901 tab->sample_index, int, tab->n_sample + 1);
902 if (!t)
903 goto error;
904 tab->sample_index = t;
905 }
906
907 tab->samples = isl_mat_extend(tab->samples,
908 tab->n_sample + 1, tab->samples->n_col);
909 if (!tab->samples)
910 goto error;
911
912 isl_seq_cpy(tab->samples->row[tab->n_sample], sample->el, sample->size);
913 isl_vec_free(sample);
914 tab->sample_index[tab->n_sample] = tab->n_sample;
915 tab->n_sample++;
916
917 return 0;
918error:
919 isl_vec_free(sample);
920 return -1;
921}
922
923struct isl_tab *isl_tab_drop_sample(struct isl_tab *tab, int s)
924{
925 if (s != tab->n_outside) {
926 int t = tab->sample_index[tab->n_outside];
927 tab->sample_index[tab->n_outside] = tab->sample_index[s];
928 tab->sample_index[s] = t;
929 isl_mat_swap_rows(tab->samples, tab->n_outside, s);
930 }
931 tab->n_outside++;
933 isl_tab_free(tab);
934 return NULL;
935 }
936
937 return tab;
938}
939
940/* Record the current number of samples so that we can remove newer
941 * samples during a rollback.
942 */
944{
945 union isl_tab_undo_val u;
946
947 if (!tab)
948 return isl_stat_error;
949
950 u.n = tab->n_sample;
952}
953
954/* Mark row with index "row" as being redundant.
955 * If we may need to undo the operation or if the row represents
956 * a variable of the original problem, the row is kept,
957 * but no longer considered when looking for a pivot row.
958 * Otherwise, the row is simply removed.
959 *
960 * The row may be interchanged with some other row. If it
961 * is interchanged with a later row, return 1. Otherwise return 0.
962 * If the rows are checked in order in the calling function,
963 * then a return value of 1 means that the row with the given
964 * row number may now contain a different row that hasn't been checked yet.
965 */
966int isl_tab_mark_redundant(struct isl_tab *tab, int row)
967{
968 struct isl_tab_var *var = isl_tab_var_from_row(tab, row);
969 var->is_redundant = 1;
970 isl_assert(tab->mat->ctx, row >= tab->n_redundant, return -1);
971 if (tab->preserve || tab->need_undo || tab->row_var[row] >= 0) {
972 if (tab->row_var[row] >= 0 && !var->is_nonneg) {
973 var->is_nonneg = 1;
975 return -1;
976 }
977 if (row != tab->n_redundant)
978 swap_rows(tab, row, tab->n_redundant);
979 tab->n_redundant++;
981 } else {
982 if (row != tab->n_row - 1)
983 swap_rows(tab, row, tab->n_row - 1);
984 isl_tab_var_from_row(tab, tab->n_row - 1)->index = -1;
985 tab->n_row--;
986 return 1;
987 }
988}
989
990/* Mark "tab" as a rational tableau.
991 * If it wasn't marked as a rational tableau already and if we may
992 * need to undo changes, then arrange for the marking to be undone
993 * during the undo.
994 */
996{
997 if (!tab)
998 return -1;
999 if (!tab->rational && tab->need_undo)
1000 if (isl_tab_push(tab, isl_tab_undo_rational) < 0)
1001 return -1;
1002 tab->rational = 1;
1003 return 0;
1004}
1005
1007{
1008 if (!tab)
1009 return isl_stat_error;
1010 if (!tab->empty && tab->need_undo)
1011 if (isl_tab_push(tab, isl_tab_undo_empty) < 0)
1012 return isl_stat_error;
1013 tab->empty = 1;
1014 return isl_stat_ok;
1015}
1016
1017int isl_tab_freeze_constraint(struct isl_tab *tab, int con)
1018{
1019 struct isl_tab_var *var;
1020
1021 if (!tab)
1022 return -1;
1023
1024 var = &tab->con[con];
1025 if (var->frozen)
1026 return 0;
1027 if (var->index < 0)
1028 return 0;
1029 var->frozen = 1;
1030
1031 if (tab->need_undo)
1033
1034 return 0;
1035}
1036
1037/* Update the rows signs after a pivot of "row" and "col", with "row_sgn"
1038 * the original sign of the pivot element.
1039 * We only keep track of row signs during PILP solving and in this case
1040 * we only pivot a row with negative sign (meaning the value is always
1041 * non-positive) using a positive pivot element.
1042 *
1043 * For each row j, the new value of the parametric constant is equal to
1044 *
1045 * a_j0 - a_jc a_r0/a_rc
1046 *
1047 * where a_j0 is the original parametric constant, a_rc is the pivot element,
1048 * a_r0 is the parametric constant of the pivot row and a_jc is the
1049 * pivot column entry of the row j.
1050 * Since a_r0 is non-positive and a_rc is positive, the sign of row j
1051 * remains the same if a_jc has the same sign as the row j or if
1052 * a_jc is zero. In all other cases, we reset the sign to "unknown".
1053 */
1054static void update_row_sign(struct isl_tab *tab, int row, int col, int row_sgn)
1055{
1056 int i;
1057 struct isl_mat *mat = tab->mat;
1058 unsigned off = 2 + tab->M;
1059
1060 if (!tab->row_sign)
1061 return;
1062
1063 if (tab->row_sign[row] == 0)
1064 return;
1065 isl_assert(mat->ctx, row_sgn > 0, return);
1066 isl_assert(mat->ctx, tab->row_sign[row] == isl_tab_row_neg, return);
1068 for (i = 0; i < tab->n_row; ++i) {
1069 int s;
1070 if (i == row)
1071 continue;
1072 s = isl_int_sgn(mat->row[i][off + col]);
1073 if (!s)
1074 continue;
1075 if (!tab->row_sign[i])
1076 continue;
1077 if (s < 0 && tab->row_sign[i] == isl_tab_row_neg)
1078 continue;
1079 if (s > 0 && tab->row_sign[i] == isl_tab_row_pos)
1080 continue;
1081 tab->row_sign[i] = isl_tab_row_unknown;
1082 }
1083}
1084
1085/* Given a row number "row" and a column number "col", pivot the tableau
1086 * such that the associated variables are interchanged.
1087 * The given row in the tableau expresses
1088 *
1089 * x_r = a_r0 + \sum_i a_ri x_i
1090 *
1091 * or
1092 *
1093 * x_c = 1/a_rc x_r - a_r0/a_rc + sum_{i \ne r} -a_ri/a_rc
1094 *
1095 * Substituting this equality into the other rows
1096 *
1097 * x_j = a_j0 + \sum_i a_ji x_i
1098 *
1099 * with a_jc \ne 0, we obtain
1100 *
1101 * 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
1102 *
1103 * The tableau
1104 *
1105 * n_rc/d_r n_ri/d_r
1106 * n_jc/d_j n_ji/d_j
1107 *
1108 * where i is any other column and j is any other row,
1109 * is therefore transformed into
1110 *
1111 * s(n_rc)d_r/|n_rc| -s(n_rc)n_ri/|n_rc|
1112 * 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)
1113 *
1114 * The transformation is performed along the following steps
1115 *
1116 * d_r/n_rc n_ri/n_rc
1117 * n_jc/d_j n_ji/d_j
1118 *
1119 * s(n_rc)d_r/|n_rc| -s(n_rc)n_ri/|n_rc|
1120 * n_jc/d_j n_ji/d_j
1121 *
1122 * s(n_rc)d_r/|n_rc| -s(n_rc)n_ri/|n_rc|
1123 * n_jc/(|n_rc| d_j) n_ji/(|n_rc| d_j)
1124 *
1125 * s(n_rc)d_r/|n_rc| -s(n_rc)n_ri/|n_rc|
1126 * n_jc/(|n_rc| d_j) (n_ji |n_rc|)/(|n_rc| d_j)
1127 *
1128 * s(n_rc)d_r/|n_rc| -s(n_rc)n_ri/|n_rc|
1129 * n_jc/(|n_rc| d_j) (n_ji |n_rc| - s(n_rc)n_jc n_ri)/(|n_rc| d_j)
1130 *
1131 * s(n_rc)d_r/|n_rc| -s(n_rc)n_ri/|n_rc|
1132 * 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)
1133 *
1134 */
1135int isl_tab_pivot(struct isl_tab *tab, int row, int col)
1136{
1137 int i, j;
1138 int sgn;
1139 int t;
1140 isl_ctx *ctx;
1141 struct isl_mat *mat = tab->mat;
1142 struct isl_tab_var *var;
1143 unsigned off = 2 + tab->M;
1144
1145 ctx = isl_tab_get_ctx(tab);
1146 if (isl_ctx_next_operation(ctx) < 0)
1147 return -1;
1148
1149 isl_int_swap(mat->row[row][0], mat->row[row][off + col]);
1150 sgn = isl_int_sgn(mat->row[row][0]);
1151 if (sgn < 0) {
1152 isl_int_neg(mat->row[row][0], mat->row[row][0]);
1153 isl_int_neg(mat->row[row][off + col], mat->row[row][off + col]);
1154 } else
1155 for (j = 0; j < off - 1 + tab->n_col; ++j) {
1156 if (j == off - 1 + col)
1157 continue;
1158 isl_int_neg(mat->row[row][1 + j], mat->row[row][1 + j]);
1159 }
1160 if (!isl_int_is_one(mat->row[row][0]))
1161 isl_seq_normalize(mat->ctx, mat->row[row], off + tab->n_col);
1162 for (i = 0; i < tab->n_row; ++i) {
1163 if (i == row)
1164 continue;
1165 if (isl_int_is_zero(mat->row[i][off + col]))
1166 continue;
1167 isl_int_mul(mat->row[i][0], mat->row[i][0], mat->row[row][0]);
1168 for (j = 0; j < off - 1 + tab->n_col; ++j) {
1169 if (j == off - 1 + col)
1170 continue;
1171 isl_int_mul(mat->row[i][1 + j],
1172 mat->row[i][1 + j], mat->row[row][0]);
1173 isl_int_addmul(mat->row[i][1 + j],
1174 mat->row[i][off + col], mat->row[row][1 + j]);
1175 }
1176 isl_int_mul(mat->row[i][off + col],
1177 mat->row[i][off + col], mat->row[row][off + col]);
1178 if (!isl_int_is_one(mat->row[i][0]))
1179 isl_seq_normalize(mat->ctx, mat->row[i], off + tab->n_col);
1180 }
1181 t = tab->row_var[row];
1182 tab->row_var[row] = tab->col_var[col];
1183 tab->col_var[col] = t;
1184 var = isl_tab_var_from_row(tab, row);
1185 var->is_row = 1;
1186 var->index = row;
1187 var = var_from_col(tab, col);
1188 var->is_row = 0;
1189 var->index = col;
1190 update_row_sign(tab, row, col, sgn);
1191 if (tab->in_undo)
1192 return 0;
1193 for (i = tab->n_redundant; i < tab->n_row; ++i) {
1194 if (isl_int_is_zero(mat->row[i][off + col]))
1195 continue;
1196 if (!isl_tab_var_from_row(tab, i)->frozen &&
1197 isl_tab_row_is_redundant(tab, i)) {
1198 int redo = isl_tab_mark_redundant(tab, i);
1199 if (redo < 0)
1200 return -1;
1201 if (redo)
1202 --i;
1203 }
1204 }
1205 return 0;
1206}
1207
1208/* If "var" represents a column variable, then pivot is up (sgn > 0)
1209 * or down (sgn < 0) to a row. The variable is assumed not to be
1210 * unbounded in the specified direction.
1211 * If sgn = 0, then the variable is unbounded in both directions,
1212 * and we pivot with any row we can find.
1213 */
1214static int to_row(struct isl_tab *tab, struct isl_tab_var *var, int sign) WARN_UNUSED;
1215static int to_row(struct isl_tab *tab, struct isl_tab_var *var, int sign)
1216{
1217 int r;
1218 unsigned off = 2 + tab->M;
1219
1220 if (var->is_row)
1221 return 0;
1222
1223 if (sign == 0) {
1224 for (r = tab->n_redundant; r < tab->n_row; ++r)
1225 if (!isl_int_is_zero(tab->mat->row[r][off+var->index]))
1226 break;
1227 isl_assert(tab->mat->ctx, r < tab->n_row, return -1);
1228 } else {
1229 r = pivot_row(tab, NULL, sign, var->index);
1230 isl_assert(tab->mat->ctx, r >= 0, return -1);
1231 }
1232
1233 return isl_tab_pivot(tab, r, var->index);
1234}
1235
1236/* Check whether all variables that are marked as non-negative
1237 * also have a non-negative sample value. This function is not
1238 * called from the current code but is useful during debugging.
1239 */
1240static void check_table(struct isl_tab *tab) __attribute__ ((unused));
1241static void check_table(struct isl_tab *tab)
1242{
1243 int i;
1244
1245 if (tab->empty)
1246 return;
1247 for (i = tab->n_redundant; i < tab->n_row; ++i) {
1248 struct isl_tab_var *var;
1249 var = isl_tab_var_from_row(tab, i);
1250 if (!var->is_nonneg)
1251 continue;
1252 if (tab->M) {
1253 isl_assert(tab->mat->ctx,
1254 !isl_int_is_neg(tab->mat->row[i][2]), abort());
1255 if (isl_int_is_pos(tab->mat->row[i][2]))
1256 continue;
1257 }
1258 isl_assert(tab->mat->ctx, !isl_int_is_neg(tab->mat->row[i][1]),
1259 abort());
1260 }
1261}
1262
1263/* Return the sign of the maximal value of "var".
1264 * If the sign is not negative, then on return from this function,
1265 * the sample value will also be non-negative.
1266 *
1267 * If "var" is manifestly unbounded wrt positive values, we are done.
1268 * Otherwise, we pivot the variable up to a row if needed.
1269 * Then we continue pivoting up until either
1270 * - no more up pivots can be performed
1271 * - the sample value is positive
1272 * - the variable is pivoted into a manifestly unbounded column
1273 */
1274static int sign_of_max(struct isl_tab *tab, struct isl_tab_var *var)
1275{
1276 int row, col;
1277
1279 return 1;
1280 if (to_row(tab, var, 1) < 0)
1281 return -2;
1282 while (!isl_int_is_pos(tab->mat->row[var->index][1])) {
1283 find_pivot(tab, var, var, 1, &row, &col);
1284 if (row == -1)
1285 return isl_int_sgn(tab->mat->row[var->index][1]);
1286 if (isl_tab_pivot(tab, row, col) < 0)
1287 return -2;
1288 if (!var->is_row) /* manifestly unbounded */
1289 return 1;
1290 }
1291 return 1;
1292}
1293
1294int isl_tab_sign_of_max(struct isl_tab *tab, int con)
1295{
1296 struct isl_tab_var *var;
1297
1298 if (!tab)
1299 return -2;
1300
1301 var = &tab->con[con];
1302 isl_assert(tab->mat->ctx, !var->is_redundant, return -2);
1303 isl_assert(tab->mat->ctx, !var->is_zero, return -2);
1304
1305 return sign_of_max(tab, var);
1306}
1307
1308static int row_is_neg(struct isl_tab *tab, int row)
1309{
1310 if (!tab->M)
1311 return isl_int_is_neg(tab->mat->row[row][1]);
1312 if (isl_int_is_pos(tab->mat->row[row][2]))
1313 return 0;
1314 if (isl_int_is_neg(tab->mat->row[row][2]))
1315 return 1;
1316 return isl_int_is_neg(tab->mat->row[row][1]);
1317}
1318
1319static int row_sgn(struct isl_tab *tab, int row)
1320{
1321 if (!tab->M)
1322 return isl_int_sgn(tab->mat->row[row][1]);
1323 if (!isl_int_is_zero(tab->mat->row[row][2]))
1324 return isl_int_sgn(tab->mat->row[row][2]);
1325 else
1326 return isl_int_sgn(tab->mat->row[row][1]);
1327}
1328
1329/* Perform pivots until the row variable "var" has a non-negative
1330 * sample value or until no more upward pivots can be performed.
1331 * Return the sign of the sample value after the pivots have been
1332 * performed.
1333 */
1334static int restore_row(struct isl_tab *tab, struct isl_tab_var *var)
1335{
1336 int row, col;
1337
1338 while (row_is_neg(tab, var->index)) {
1339 find_pivot(tab, var, var, 1, &row, &col);
1340 if (row == -1)
1341 break;
1342 if (isl_tab_pivot(tab, row, col) < 0)
1343 return -2;
1344 if (!var->is_row) /* manifestly unbounded */
1345 return 1;
1346 }
1347 return row_sgn(tab, var->index);
1348}
1349
1350/* Perform pivots until we are sure that the row variable "var"
1351 * can attain non-negative values. After return from this
1352 * function, "var" is still a row variable, but its sample
1353 * value may not be non-negative, even if the function returns 1.
1354 */
1355static int at_least_zero(struct isl_tab *tab, struct isl_tab_var *var)
1356{
1357 int row, col;
1358
1359 while (isl_int_is_neg(tab->mat->row[var->index][1])) {
1360 find_pivot(tab, var, var, 1, &row, &col);
1361 if (row == -1)
1362 break;
1363 if (row == var->index) /* manifestly unbounded */
1364 return 1;
1365 if (isl_tab_pivot(tab, row, col) < 0)
1366 return -1;
1367 }
1368 return !isl_int_is_neg(tab->mat->row[var->index][1]);
1369}
1370
1371/* Return a negative value if "var" can attain negative values.
1372 * Return a non-negative value otherwise.
1373 *
1374 * If "var" is manifestly unbounded wrt negative values, we are done.
1375 * Otherwise, if var is in a column, we can pivot it down to a row.
1376 * Then we continue pivoting down until either
1377 * - the pivot would result in a manifestly unbounded column
1378 * => we don't perform the pivot, but simply return -1
1379 * - no more down pivots can be performed
1380 * - the sample value is negative
1381 * If the sample value becomes negative and the variable is supposed
1382 * to be nonnegative, then we undo the last pivot.
1383 * However, if the last pivot has made the pivoting variable
1384 * obviously redundant, then it may have moved to another row.
1385 * In that case we look for upward pivots until we reach a non-negative
1386 * value again.
1387 */
1388static int sign_of_min(struct isl_tab *tab, struct isl_tab_var *var)
1389{
1390 int row, col;
1391 struct isl_tab_var *pivot_var = NULL;
1392
1394 return -1;
1395 if (!var->is_row) {
1396 col = var->index;
1397 row = pivot_row(tab, NULL, -1, col);
1398 pivot_var = var_from_col(tab, col);
1399 if (isl_tab_pivot(tab, row, col) < 0)
1400 return -2;
1401 if (var->is_redundant)
1402 return 0;
1403 if (isl_int_is_neg(tab->mat->row[var->index][1])) {
1404 if (var->is_nonneg) {
1405 if (!pivot_var->is_redundant &&
1406 pivot_var->index == row) {
1407 if (isl_tab_pivot(tab, row, col) < 0)
1408 return -2;
1409 } else
1410 if (restore_row(tab, var) < -1)
1411 return -2;
1412 }
1413 return -1;
1414 }
1415 }
1416 if (var->is_redundant)
1417 return 0;
1418 while (!isl_int_is_neg(tab->mat->row[var->index][1])) {
1419 find_pivot(tab, var, var, -1, &row, &col);
1420 if (row == var->index)
1421 return -1;
1422 if (row == -1)
1423 return isl_int_sgn(tab->mat->row[var->index][1]);
1424 pivot_var = var_from_col(tab, col);
1425 if (isl_tab_pivot(tab, row, col) < 0)
1426 return -2;
1427 if (var->is_redundant)
1428 return 0;
1429 }
1430 if (pivot_var && var->is_nonneg) {
1431 /* pivot back to non-negative value */
1432 if (!pivot_var->is_redundant && pivot_var->index == row) {
1433 if (isl_tab_pivot(tab, row, col) < 0)
1434 return -2;
1435 } else
1436 if (restore_row(tab, var) < -1)
1437 return -2;
1438 }
1439 return -1;
1440}
1441
1442static int row_at_most_neg_one(struct isl_tab *tab, int row)
1443{
1444 if (tab->M) {
1445 if (isl_int_is_pos(tab->mat->row[row][2]))
1446 return 0;
1447 if (isl_int_is_neg(tab->mat->row[row][2]))
1448 return 1;
1449 }
1450 return isl_int_is_neg(tab->mat->row[row][1]) &&
1451 isl_int_abs_ge(tab->mat->row[row][1],
1452 tab->mat->row[row][0]);
1453}
1454
1455/* Return 1 if "var" can attain values <= -1.
1456 * Return 0 otherwise.
1457 *
1458 * If the variable "var" is supposed to be non-negative (is_nonneg is set),
1459 * then the sample value of "var" is assumed to be non-negative when the
1460 * the function is called. If 1 is returned then the constraint
1461 * is not redundant and the sample value is made non-negative again before
1462 * the function returns.
1463 */
1465{
1466 int row, col;
1467 struct isl_tab_var *pivot_var;
1468
1470 return 1;
1471 if (!var->is_row) {
1472 col = var->index;
1473 row = pivot_row(tab, NULL, -1, col);
1474 pivot_var = var_from_col(tab, col);
1475 if (isl_tab_pivot(tab, row, col) < 0)
1476 return -1;
1477 if (var->is_redundant)
1478 return 0;
1479 if (row_at_most_neg_one(tab, var->index)) {
1480 if (var->is_nonneg) {
1481 if (!pivot_var->is_redundant &&
1482 pivot_var->index == row) {
1483 if (isl_tab_pivot(tab, row, col) < 0)
1484 return -1;
1485 } else
1486 if (restore_row(tab, var) < -1)
1487 return -1;
1488 }
1489 return 1;
1490 }
1491 }
1492 if (var->is_redundant)
1493 return 0;
1494 do {
1495 find_pivot(tab, var, var, -1, &row, &col);
1496 if (row == var->index) {
1497 if (var->is_nonneg && restore_row(tab, var) < -1)
1498 return -1;
1499 return 1;
1500 }
1501 if (row == -1)
1502 return 0;
1503 pivot_var = var_from_col(tab, col);
1504 if (isl_tab_pivot(tab, row, col) < 0)
1505 return -1;
1506 if (var->is_redundant)
1507 return 0;
1508 } while (!row_at_most_neg_one(tab, var->index));
1509 if (var->is_nonneg) {
1510 /* pivot back to non-negative value */
1511 if (!pivot_var->is_redundant && pivot_var->index == row)
1512 if (isl_tab_pivot(tab, row, col) < 0)
1513 return -1;
1514 if (restore_row(tab, var) < -1)
1515 return -1;
1516 }
1517 return 1;
1518}
1519
1520/* Return 1 if "var" can attain values >= 1.
1521 * Return 0 otherwise.
1522 */
1523static int at_least_one(struct isl_tab *tab, struct isl_tab_var *var)
1524{
1525 int row, col;
1526 isl_int *r;
1527
1529 return 1;
1530 if (to_row(tab, var, 1) < 0)
1531 return -1;
1532 r = tab->mat->row[var->index];
1533 while (isl_int_lt(r[1], r[0])) {
1534 find_pivot(tab, var, var, 1, &row, &col);
1535 if (row == -1)
1536 return isl_int_ge(r[1], r[0]);
1537 if (row == var->index) /* manifestly unbounded */
1538 return 1;
1539 if (isl_tab_pivot(tab, row, col) < 0)
1540 return -1;
1541 }
1542 return 1;
1543}
1544
1545static void swap_cols(struct isl_tab *tab, int col1, int col2)
1546{
1547 int t;
1548 unsigned off = 2 + tab->M;
1549 t = tab->col_var[col1];
1550 tab->col_var[col1] = tab->col_var[col2];
1551 tab->col_var[col2] = t;
1552 var_from_col(tab, col1)->index = col1;
1553 var_from_col(tab, col2)->index = col2;
1554 tab->mat = isl_mat_swap_cols(tab->mat, off + col1, off + col2);
1555}
1556
1557/* Mark column with index "col" as representing a zero variable.
1558 * If we may need to undo the operation the column is kept,
1559 * but no longer considered.
1560 * Otherwise, the column is simply removed.
1561 *
1562 * The column may be interchanged with some other column. If it
1563 * is interchanged with a later column, return 1. Otherwise return 0.
1564 * If the columns are checked in order in the calling function,
1565 * then a return value of 1 means that the column with the given
1566 * column number may now contain a different column that
1567 * hasn't been checked yet.
1568 */
1569int isl_tab_kill_col(struct isl_tab *tab, int col)
1570{
1571 var_from_col(tab, col)->is_zero = 1;
1572 if (tab->need_undo) {
1574 var_from_col(tab, col)) < 0)
1575 return -1;
1576 if (col != tab->n_dead)
1577 swap_cols(tab, col, tab->n_dead);
1578 tab->n_dead++;
1579 return 0;
1580 } else {
1581 if (col != tab->n_col - 1)
1582 swap_cols(tab, col, tab->n_col - 1);
1583 var_from_col(tab, tab->n_col - 1)->index = -1;
1584 tab->n_col--;
1585 return 1;
1586 }
1587}
1588
1589static int row_is_manifestly_non_integral(struct isl_tab *tab, int row)
1590{
1591 unsigned off = 2 + tab->M;
1592
1593 if (tab->M && !isl_int_eq(tab->mat->row[row][2],
1594 tab->mat->row[row][0]))
1595 return 0;
1596 if (isl_seq_any_non_zero(tab->mat->row[row] + off + tab->n_dead,
1597 tab->n_col - tab->n_dead))
1598 return 0;
1599
1600 return !isl_int_is_divisible_by(tab->mat->row[row][1],
1601 tab->mat->row[row][0]);
1602}
1603
1604/* For integer tableaus, check if any of the coordinates are stuck
1605 * at a non-integral value.
1606 */
1607static int tab_is_manifestly_empty(struct isl_tab *tab)
1608{
1609 int i;
1610
1611 if (tab->empty)
1612 return 1;
1613 if (tab->rational)
1614 return 0;
1615
1616 for (i = 0; i < tab->n_var; ++i) {
1617 if (!tab->var[i].is_row)
1618 continue;
1619 if (row_is_manifestly_non_integral(tab, tab->var[i].index))
1620 return 1;
1621 }
1622
1623 return 0;
1624}
1625
1626/* Row variable "var" is non-negative and cannot attain any values
1627 * larger than zero. This means that the coefficients of the unrestricted
1628 * column variables are zero and that the coefficients of the non-negative
1629 * column variables are zero or negative.
1630 * Each of the non-negative variables with a negative coefficient can
1631 * then also be written as the negative sum of non-negative variables
1632 * and must therefore also be zero.
1633 *
1634 * If "temp_var" is set, then "var" is a temporary variable that
1635 * will be removed after this function returns and for which
1636 * no information is recorded on the undo stack.
1637 * Do not add any undo records involving this variable in this case
1638 * since the variable will have been removed before any future undo
1639 * operations. Also avoid marking the variable as redundant,
1640 * since that either adds an undo record or needlessly removes the row
1641 * (the caller will take care of removing the row).
1642 */
1643static isl_stat close_row(struct isl_tab *tab, struct isl_tab_var *var,
1644 int temp_var) WARN_UNUSED;
1645static isl_stat close_row(struct isl_tab *tab, struct isl_tab_var *var,
1646 int temp_var)
1647{
1648 int j;
1649 struct isl_mat *mat = tab->mat;
1650 unsigned off = 2 + tab->M;
1651
1652 if (!var->is_nonneg)
1654 "expecting non-negative variable",
1655 return isl_stat_error);
1656 var->is_zero = 1;
1657 if (!temp_var && tab->need_undo)
1658 if (isl_tab_push_var(tab, isl_tab_undo_zero, var) < 0)
1659 return isl_stat_error;
1660 for (j = tab->n_dead; j < tab->n_col; ++j) {
1661 int recheck;
1662 if (isl_int_is_zero(mat->row[var->index][off + j]))
1663 continue;
1664 if (isl_int_is_pos(mat->row[var->index][off + j]))
1666 "row cannot have positive coefficients",
1667 return isl_stat_error);
1668 recheck = isl_tab_kill_col(tab, j);
1669 if (recheck < 0)
1670 return isl_stat_error;
1671 if (recheck)
1672 --j;
1673 }
1674 if (!temp_var && isl_tab_mark_redundant(tab, var->index) < 0)
1675 return isl_stat_error;
1676 if (tab_is_manifestly_empty(tab) && isl_tab_mark_empty(tab) < 0)
1677 return isl_stat_error;
1678 return isl_stat_ok;
1679}
1680
1681/* Add a constraint to the tableau and allocate a row for it.
1682 * Return the index into the constraint array "con".
1683 *
1684 * This function assumes that at least one more row and at least
1685 * one more element in the constraint array are available in the tableau.
1686 */
1688{
1689 int r;
1690
1691 isl_assert(tab->mat->ctx, tab->n_row < tab->mat->n_row, return -1);
1692 isl_assert(tab->mat->ctx, tab->n_con < tab->max_con, return -1);
1693
1694 r = tab->n_con;
1695 tab->con[r].index = tab->n_row;
1696 tab->con[r].is_row = 1;
1697 tab->con[r].is_nonneg = 0;
1698 tab->con[r].is_zero = 0;
1699 tab->con[r].is_redundant = 0;
1700 tab->con[r].frozen = 0;
1701 tab->con[r].negated = 0;
1702 tab->row_var[tab->n_row] = ~r;
1703
1704 tab->n_row++;
1705 tab->n_con++;
1706 if (isl_tab_push_var(tab, isl_tab_undo_allocate, &tab->con[r]) < 0)
1707 return -1;
1708
1709 return r;
1710}
1711
1712/* Move the entries in tab->var up one position, starting at "first",
1713 * creating room for an extra entry at position "first".
1714 * Since some of the entries of tab->row_var and tab->col_var contain
1715 * indices into this array, they have to be updated accordingly.
1716 */
1717static int var_insert_entry(struct isl_tab *tab, int first)
1718{
1719 int i;
1720
1721 if (tab->n_var >= tab->max_var)
1723 "not enough room for new variable", return -1);
1724 if (first > tab->n_var)
1726 "invalid initial position", return -1);
1727
1728 for (i = tab->n_var - 1; i >= first; --i) {
1729 tab->var[i + 1] = tab->var[i];
1730 if (tab->var[i + 1].is_row)
1731 tab->row_var[tab->var[i + 1].index]++;
1732 else
1733 tab->col_var[tab->var[i + 1].index]++;
1734 }
1735
1736 tab->n_var++;
1737
1738 return 0;
1739}
1740
1741/* Drop the entry at position "first" in tab->var, moving all
1742 * subsequent entries down.
1743 * Since some of the entries of tab->row_var and tab->col_var contain
1744 * indices into this array, they have to be updated accordingly.
1745 */
1746static int var_drop_entry(struct isl_tab *tab, int first)
1747{
1748 int i;
1749
1750 if (first >= tab->n_var)
1752 "invalid initial position", return -1);
1753
1754 tab->n_var--;
1755
1756 for (i = first; i < tab->n_var; ++i) {
1757 tab->var[i] = tab->var[i + 1];
1758 if (tab->var[i + 1].is_row)
1759 tab->row_var[tab->var[i].index]--;
1760 else
1761 tab->col_var[tab->var[i].index]--;
1762 }
1763
1764 return 0;
1765}
1766
1767/* Add a variable to the tableau at position "r" and allocate a column for it.
1768 * Return the index into the variable array "var", i.e., "r",
1769 * or -1 on error.
1770 */
1771int isl_tab_insert_var(struct isl_tab *tab, int r)
1772{
1773 int i;
1774 unsigned off = 2 + tab->M;
1775
1776 isl_assert(tab->mat->ctx, tab->n_col < tab->mat->n_col, return -1);
1777
1778 if (var_insert_entry(tab, r) < 0)
1779 return -1;
1780
1781 tab->var[r].index = tab->n_col;
1782 tab->var[r].is_row = 0;
1783 tab->var[r].is_nonneg = 0;
1784 tab->var[r].is_zero = 0;
1785 tab->var[r].is_redundant = 0;
1786 tab->var[r].frozen = 0;
1787 tab->var[r].negated = 0;
1788 tab->col_var[tab->n_col] = r;
1789
1790 for (i = 0; i < tab->n_row; ++i)
1791 isl_int_set_si(tab->mat->row[i][off + tab->n_col], 0);
1792
1793 tab->n_col++;
1794 if (isl_tab_push_var(tab, isl_tab_undo_allocate, &tab->var[r]) < 0)
1795 return -1;
1796
1797 return r;
1798}
1799
1800/* Add a row to the tableau. The row is given as an affine combination
1801 * of the original variables and needs to be expressed in terms of the
1802 * column variables.
1803 *
1804 * This function assumes that at least one more row and at least
1805 * one more element in the constraint array are available in the tableau.
1806 *
1807 * We add each term in turn.
1808 * If r = n/d_r is the current sum and we need to add k x, then
1809 * if x is a column variable, we increase the numerator of
1810 * this column by k d_r
1811 * if x = f/d_x is a row variable, then the new representation of r is
1812 *
1813 * n k f d_x/g n + d_r/g k f m/d_r n + m/d_g k f
1814 * --- + --- = ------------------- = -------------------
1815 * d_r d_r d_r d_x/g m
1816 *
1817 * with g the gcd of d_r and d_x and m the lcm of d_r and d_x.
1818 *
1819 * If tab->M is set, then, internally, each variable x is represented
1820 * as x' - M. We then also need no subtract k d_r from the coefficient of M.
1821 */
1822int isl_tab_add_row(struct isl_tab *tab, isl_int *line)
1823{
1824 int i;
1825 int r;
1826 isl_int *row;
1827 isl_int a, b;
1828 unsigned off = 2 + tab->M;
1829
1830 r = isl_tab_allocate_con(tab);
1831 if (r < 0)
1832 return -1;
1833
1834 isl_int_init(a);
1835 isl_int_init(b);
1836 row = tab->mat->row[tab->con[r].index];
1837 isl_int_set_si(row[0], 1);
1838 isl_int_set(row[1], line[0]);
1839 isl_seq_clr(row + 2, tab->M + tab->n_col);
1840 for (i = 0; i < tab->n_var; ++i) {
1841 if (tab->var[i].is_zero)
1842 continue;
1843 if (tab->var[i].is_row) {
1844 isl_int_lcm(a,
1845 row[0], tab->mat->row[tab->var[i].index][0]);
1846 isl_int_swap(a, row[0]);
1847 isl_int_divexact(a, row[0], a);
1849 row[0], tab->mat->row[tab->var[i].index][0]);
1850 isl_int_mul(b, b, line[1 + i]);
1851 isl_seq_combine(row + 1, a, row + 1,
1852 b, tab->mat->row[tab->var[i].index] + 1,
1853 1 + tab->M + tab->n_col);
1854 } else
1855 isl_int_addmul(row[off + tab->var[i].index],
1856 line[1 + i], row[0]);
1857 if (tab->M && i >= tab->n_param && i < tab->n_var - tab->n_div)
1858 isl_int_submul(row[2], line[1 + i], row[0]);
1859 }
1860 isl_seq_normalize(tab->mat->ctx, row, off + tab->n_col);
1863
1864 if (tab->row_sign)
1865 tab->row_sign[tab->con[r].index] = isl_tab_row_unknown;
1866
1867 return r;
1868}
1869
1870static isl_stat drop_row(struct isl_tab *tab, int row)
1871{
1872 isl_assert(tab->mat->ctx, ~tab->row_var[row] == tab->n_con - 1,
1873 return isl_stat_error);
1874 if (row != tab->n_row - 1)
1875 swap_rows(tab, row, tab->n_row - 1);
1876 tab->n_row--;
1877 tab->n_con--;
1878 return isl_stat_ok;
1879}
1880
1881/* Drop the variable in column "col" along with the column.
1882 * The column is removed first because it may need to be moved
1883 * into the last position and this process requires
1884 * the contents of the col_var array in a state
1885 * before the removal of the variable.
1886 */
1887static isl_stat drop_col(struct isl_tab *tab, int col)
1888{
1889 int var;
1890
1891 var = tab->col_var[col];
1892 if (col != tab->n_col - 1)
1893 swap_cols(tab, col, tab->n_col - 1);
1894 tab->n_col--;
1895 if (var_drop_entry(tab, var) < 0)
1896 return isl_stat_error;
1897 return isl_stat_ok;
1898}
1899
1900/* Add inequality "ineq" and check if it conflicts with the
1901 * previously added constraints or if it is obviously redundant.
1902 *
1903 * This function assumes that at least one more row and at least
1904 * one more element in the constraint array are available in the tableau.
1905 */
1907{
1908 int r;
1909 int sgn;
1910 isl_int cst;
1911
1912 if (!tab)
1913 return isl_stat_error;
1914 if (tab->bmap) {
1915 struct isl_basic_map *bmap = tab->bmap;
1916
1917 isl_assert(tab->mat->ctx, tab->n_eq == bmap->n_eq,
1918 return isl_stat_error);
1919 isl_assert(tab->mat->ctx,
1920 tab->n_con == bmap->n_eq + bmap->n_ineq,
1921 return isl_stat_error);
1922 tab->bmap = isl_basic_map_add_ineq(tab->bmap, ineq);
1924 return isl_stat_error;
1925 if (!tab->bmap)
1926 return isl_stat_error;
1927 }
1928 if (tab->cone) {
1929 isl_int_init(cst);
1930 isl_int_set_si(cst, 0);
1931 isl_int_swap(ineq[0], cst);
1932 }
1933 r = isl_tab_add_row(tab, ineq);
1934 if (tab->cone) {
1935 isl_int_swap(ineq[0], cst);
1936 isl_int_clear(cst);
1937 }
1938 if (r < 0)
1939 return isl_stat_error;
1940 tab->con[r].is_nonneg = 1;
1941 if (isl_tab_push_var(tab, isl_tab_undo_nonneg, &tab->con[r]) < 0)
1942 return isl_stat_error;
1943 if (isl_tab_row_is_redundant(tab, tab->con[r].index)) {
1944 if (isl_tab_mark_redundant(tab, tab->con[r].index) < 0)
1945 return isl_stat_error;
1946 return isl_stat_ok;
1947 }
1948
1949 sgn = restore_row(tab, &tab->con[r]);
1950 if (sgn < -1)
1951 return isl_stat_error;
1952 if (sgn < 0)
1953 return isl_tab_mark_empty(tab);
1954 if (tab->con[r].is_row && isl_tab_row_is_redundant(tab, tab->con[r].index))
1955 if (isl_tab_mark_redundant(tab, tab->con[r].index) < 0)
1956 return isl_stat_error;
1957 return isl_stat_ok;
1958}
1959
1960/* Pivot a non-negative variable down until it reaches the value zero
1961 * and then pivot the variable into a column position.
1962 */
1963static int to_col(struct isl_tab *tab, struct isl_tab_var *var) WARN_UNUSED;
1964static int to_col(struct isl_tab *tab, struct isl_tab_var *var)
1965{
1966 int i;
1967 int row, col;
1968 unsigned off = 2 + tab->M;
1969
1970 if (!var->is_row)
1971 return 0;
1972
1973 while (isl_int_is_pos(tab->mat->row[var->index][1])) {
1974 find_pivot(tab, var, NULL, -1, &row, &col);
1975 isl_assert(tab->mat->ctx, row != -1, return -1);
1976 if (isl_tab_pivot(tab, row, col) < 0)
1977 return -1;
1978 if (!var->is_row)
1979 return 0;
1980 }
1981
1982 for (i = tab->n_dead; i < tab->n_col; ++i)
1983 if (!isl_int_is_zero(tab->mat->row[var->index][off + i]))
1984 break;
1985
1986 isl_assert(tab->mat->ctx, i < tab->n_col, return -1);
1987 if (isl_tab_pivot(tab, var->index, i) < 0)
1988 return -1;
1989
1990 return 0;
1991}
1992
1993/* We assume Gaussian elimination has been performed on the equalities.
1994 * The equalities can therefore never conflict.
1995 * Adding the equalities is currently only really useful for a later call
1996 * to isl_tab_ineq_type.
1997 *
1998 * This function assumes that at least one more row and at least
1999 * one more element in the constraint array are available in the tableau.
2000 */
2001static struct isl_tab *add_eq(struct isl_tab *tab, isl_int *eq)
2002{
2003 int i;
2004 int r;
2005
2006 if (!tab)
2007 return NULL;
2008 r = isl_tab_add_row(tab, eq);
2009 if (r < 0)
2010 goto error;
2011
2012 r = tab->con[r].index;
2013 i = isl_seq_first_non_zero(tab->mat->row[r] + 2 + tab->M + tab->n_dead,
2014 tab->n_col - tab->n_dead);
2015 isl_assert(tab->mat->ctx, i >= 0, goto error);
2016 i += tab->n_dead;
2017 if (isl_tab_pivot(tab, r, i) < 0)
2018 goto error;
2019 if (isl_tab_kill_col(tab, i) < 0)
2020 goto error;
2021 tab->n_eq++;
2022
2023 return tab;
2024error:
2025 isl_tab_free(tab);
2026 return NULL;
2027}
2028
2029/* Does the sample value of row "row" of "tab" involve the big parameter,
2030 * if any?
2031 */
2032static int row_is_big(struct isl_tab *tab, int row)
2033{
2034 return tab->M && !isl_int_is_zero(tab->mat->row[row][2]);
2035}
2036
2037static int row_is_manifestly_zero(struct isl_tab *tab, int row)
2038{
2039 unsigned off = 2 + tab->M;
2040
2041 if (!isl_int_is_zero(tab->mat->row[row][1]))
2042 return 0;
2043 if (row_is_big(tab, row))
2044 return 0;
2045 return !isl_seq_any_non_zero(tab->mat->row[row] + off + tab->n_dead,
2046 tab->n_col - tab->n_dead);
2047}
2048
2049/* Add an equality that is known to be valid for the given tableau.
2050 *
2051 * This function assumes that at least one more row and at least
2052 * one more element in the constraint array are available in the tableau.
2053 */
2055{
2056 struct isl_tab_var *var;
2057 int r;
2058
2059 if (!tab)
2060 return -1;
2061 r = isl_tab_add_row(tab, eq);
2062 if (r < 0)
2063 return -1;
2064
2065 var = &tab->con[r];
2066 r = var->index;
2067 if (row_is_manifestly_zero(tab, r)) {
2068 var->is_zero = 1;
2069 if (isl_tab_mark_redundant(tab, r) < 0)
2070 return -1;
2071 return 0;
2072 }
2073
2074 if (isl_int_is_neg(tab->mat->row[r][1])) {
2075 isl_seq_neg(tab->mat->row[r] + 1, tab->mat->row[r] + 1,
2076 1 + tab->n_col);
2077 var->negated = 1;
2078 }
2079 var->is_nonneg = 1;
2080 if (to_col(tab, var) < 0)
2081 return -1;
2082 var->is_nonneg = 0;
2083 if (isl_tab_kill_col(tab, var->index) < 0)
2084 return -1;
2085
2086 return 0;
2087}
2088
2089/* Add a zero row to "tab" and return the corresponding index
2090 * in the constraint array.
2091 *
2092 * This function assumes that at least one more row and at least
2093 * one more element in the constraint array are available in the tableau.
2094 */
2095static int add_zero_row(struct isl_tab *tab)
2096{
2097 int r;
2098 isl_int *row;
2099
2100 r = isl_tab_allocate_con(tab);
2101 if (r < 0)
2102 return -1;
2103
2104 row = tab->mat->row[tab->con[r].index];
2105 isl_seq_clr(row + 1, 1 + tab->M + tab->n_col);
2106 isl_int_set_si(row[0], 1);
2107
2108 return r;
2109}
2110
2111/* Add equality "eq" and check if it conflicts with the
2112 * previously added constraints or if it is obviously redundant.
2113 *
2114 * This function assumes that at least one more row and at least
2115 * one more element in the constraint array are available in the tableau.
2116 * If tab->bmap is set, then two rows are needed instead of one.
2117 */
2119{
2120 struct isl_tab_undo *snap = NULL;
2121 struct isl_tab_var *var;
2122 int r;
2123 int row;
2124 int sgn;
2125 isl_int cst;
2126
2127 if (!tab)
2128 return isl_stat_error;
2129 isl_assert(tab->mat->ctx, !tab->M, return isl_stat_error);
2130
2131 if (tab->need_undo)
2132 snap = isl_tab_snap(tab);
2133
2134 if (tab->cone) {
2135 isl_int_init(cst);
2136 isl_int_set_si(cst, 0);
2137 isl_int_swap(eq[0], cst);
2138 }
2139 r = isl_tab_add_row(tab, eq);
2140 if (tab->cone) {
2141 isl_int_swap(eq[0], cst);
2142 isl_int_clear(cst);
2143 }
2144 if (r < 0)
2145 return isl_stat_error;
2146
2147 var = &tab->con[r];
2148 row = var->index;
2149 if (row_is_manifestly_zero(tab, row)) {
2150 if (snap)
2151 return isl_tab_rollback(tab, snap);
2152 return drop_row(tab, row);
2153 }
2154
2155 if (tab->bmap) {
2156 tab->bmap = isl_basic_map_add_ineq(tab->bmap, eq);
2158 return isl_stat_error;
2159 isl_seq_neg(eq, eq, 1 + tab->n_var);
2160 tab->bmap = isl_basic_map_add_ineq(tab->bmap, eq);
2161 isl_seq_neg(eq, eq, 1 + tab->n_var);
2163 return isl_stat_error;
2164 if (!tab->bmap)
2165 return isl_stat_error;
2166 if (add_zero_row(tab) < 0)
2167 return isl_stat_error;
2168 }
2169
2170 sgn = isl_int_sgn(tab->mat->row[row][1]);
2171
2172 if (sgn > 0) {
2173 isl_seq_neg(tab->mat->row[row] + 1, tab->mat->row[row] + 1,
2174 1 + tab->n_col);
2175 var->negated = 1;
2176 sgn = -1;
2177 }
2178
2179 if (sgn < 0) {
2180 sgn = sign_of_max(tab, var);
2181 if (sgn < -1)
2182 return isl_stat_error;
2183 if (sgn < 0) {
2184 if (isl_tab_mark_empty(tab) < 0)
2185 return isl_stat_error;
2186 return isl_stat_ok;
2187 }
2188 }
2189
2190 var->is_nonneg = 1;
2191 if (to_col(tab, var) < 0)
2192 return isl_stat_error;
2193 var->is_nonneg = 0;
2194 if (isl_tab_kill_col(tab, var->index) < 0)
2195 return isl_stat_error;
2196
2197 return isl_stat_ok;
2198}
2199
2200/* Construct and return an inequality that expresses an upper bound
2201 * on the given div.
2202 * In particular, if the div is given by
2203 *
2204 * d = floor(e/m)
2205 *
2206 * then the inequality expresses
2207 *
2208 * m d <= e
2209 */
2211 unsigned div)
2212{
2214 unsigned div_pos;
2215 struct isl_vec *ineq;
2216
2218 if (total < 0)
2219 return NULL;
2220
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;
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]);
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{
2251 unsigned div_pos;
2252 struct isl_vec *ineq;
2253
2255 if (total < 0)
2256 return isl_stat_error;
2257 div_pos = 1 + total - tab->bmap->n_div + div;
2258
2259 ineq = ineq_for_div(tab->bmap, div);
2260 if (!ineq)
2261 goto error;
2262
2263 if (add_ineq) {
2264 if (add_ineq(user, ineq->el) < 0)
2265 goto error;
2266 } else {
2267 if (isl_tab_add_ineq(tab, ineq->el) < 0)
2268 goto error;
2269 }
2270
2271 isl_seq_neg(ineq->el, tab->bmap->div[div] + 1, 1 + total);
2272 isl_int_set(ineq->el[div_pos], tab->bmap->div[div][0]);
2273 isl_int_add(ineq->el[0], ineq->el[0], ineq->el[div_pos]);
2274 isl_int_sub_ui(ineq->el[0], ineq->el[0], 1);
2275
2276 if (add_ineq) {
2277 if (add_ineq(user, ineq->el) < 0)
2278 goto error;
2279 } else {
2280 if (isl_tab_add_ineq(tab, ineq->el) < 0)
2281 goto error;
2282 }
2283
2284 isl_vec_free(ineq);
2285
2286 return isl_stat_ok;
2287error:
2288 isl_vec_free(ineq);
2289 return isl_stat_error;
2290}
2291
2292/* Check whether the div described by "div" is obviously non-negative.
2293 * If we are using a big parameter, then we will encode the div
2294 * as div' = M + div, which is always non-negative.
2295 * Otherwise, we check whether div is a non-negative affine combination
2296 * of non-negative variables.
2297 */
2298static int div_is_nonneg(struct isl_tab *tab, __isl_keep isl_vec *div)
2299{
2300 int i;
2301
2302 if (tab->M)
2303 return 1;
2304
2305 if (isl_int_is_neg(div->el[1]))
2306 return 0;
2307
2308 for (i = 0; i < tab->n_var; ++i) {
2309 if (isl_int_is_neg(div->el[2 + i]))
2310 return 0;
2311 if (isl_int_is_zero(div->el[2 + i]))
2312 continue;
2313 if (!tab->var[i].is_nonneg)
2314 return 0;
2315 }
2316
2317 return 1;
2318}
2319
2320/* Insert an extra div, prescribed by "div", to the tableau and
2321 * the associated bmap (which is assumed to be non-NULL).
2322 * The extra integer division is inserted at (tableau) position "pos".
2323 * Return "pos" or -1 if an error occurred.
2324 *
2325 * If add_ineq is not NULL, then this function is used instead
2326 * of isl_tab_add_ineq to add the div constraints.
2327 * This complication is needed because the code in isl_tab_pip
2328 * wants to perform some extra processing when an inequality
2329 * is added to the tableau.
2330 */
2332 isl_stat (*add_ineq)(void *user, isl_int *), void *user)
2333{
2334 int r;
2335 int nonneg;
2336 isl_size n_div;
2337 int o_div;
2338
2339 if (!tab || !div)
2340 return -1;
2341
2342 if (div->size != 1 + 1 + tab->n_var)
2344 "unexpected size", return -1);
2345
2346 n_div = isl_basic_map_dim(tab->bmap, isl_dim_div);
2347 if (n_div < 0)
2348 return -1;
2349 o_div = tab->n_var - n_div;
2350 if (pos < o_div || pos > tab->n_var)
2352 "invalid position", return -1);
2353
2354 nonneg = div_is_nonneg(tab, div);
2355
2356 if (isl_tab_extend_cons(tab, 3) < 0)
2357 return -1;
2358 if (isl_tab_extend_vars(tab, 1) < 0)
2359 return -1;
2360 r = isl_tab_insert_var(tab, pos);
2361 if (r < 0)
2362 return -1;
2363
2364 if (nonneg)
2365 tab->var[r].is_nonneg = 1;
2366
2367 tab->bmap = isl_basic_map_insert_div(tab->bmap, pos - o_div, div);
2368 if (!tab->bmap)
2369 return -1;
2370 if (isl_tab_push_var(tab, isl_tab_undo_bmap_div, &tab->var[r]) < 0)
2371 return -1;
2372
2373 if (add_div_constraints(tab, pos - o_div, add_ineq, user) < 0)
2374 return -1;
2375
2376 return r;
2377}
2378
2379/* Add an extra div, prescribed by "div", to the tableau and
2380 * the associated bmap (which is assumed to be non-NULL).
2381 */
2383{
2384 if (!tab)
2385 return -1;
2386 return isl_tab_insert_div(tab, tab->n_var, div, NULL, NULL);
2387}
2388
2389/* If "track" is set, then we want to keep track of all constraints in tab
2390 * in its bmap field. This field is initialized from a copy of "bmap",
2391 * so we need to make sure that all constraints in "bmap" also appear
2392 * in the constructed tab.
2393 */
2395 __isl_keep isl_basic_map *bmap, int track)
2396{
2397 int i;
2398 struct isl_tab *tab;
2400
2402 if (total < 0)
2403 return NULL;
2404 tab = isl_tab_alloc(bmap->ctx, total + bmap->n_ineq + 1, total, 0);
2405 if (!tab)
2406 return NULL;
2407 tab->preserve = track;
2410 if (isl_tab_mark_empty(tab) < 0)
2411 goto error;
2412 goto done;
2413 }
2414 for (i = 0; i < bmap->n_eq; ++i) {
2415 tab = add_eq(tab, bmap->eq[i]);
2416 if (!tab)
2417 return tab;
2418 }
2419 for (i = 0; i < bmap->n_ineq; ++i) {
2420 if (isl_tab_add_ineq(tab, bmap->ineq[i]) < 0)
2421 goto error;
2422 if (tab->empty)
2423 goto done;
2424 }
2425done:
2426 if (track && isl_tab_track_bmap(tab, isl_basic_map_copy(bmap)) < 0)
2427 goto error;
2428 return tab;
2429error:
2430 isl_tab_free(tab);
2431 return NULL;
2432}
2433
2435 __isl_keep isl_basic_set *bset, int track)
2436{
2437 return isl_tab_from_basic_map(bset, track);
2438}
2439
2440/* Construct a tableau corresponding to the recession cone of "bset".
2441 */
2443 int parametric)
2444{
2445 isl_int cst;
2446 int i;
2447 struct isl_tab *tab;
2448 isl_size offset = 0;
2450
2452 if (parametric)
2454 if (total < 0 || offset < 0)
2455 return NULL;
2456 tab = isl_tab_alloc(bset->ctx, bset->n_eq + bset->n_ineq,
2457 total - offset, 0);
2458 if (!tab)
2459 return NULL;
2461 tab->cone = 1;
2462
2463 isl_int_init(cst);
2464 isl_int_set_si(cst, 0);
2465 for (i = 0; i < bset->n_eq; ++i) {
2466 isl_int_swap(bset->eq[i][offset], cst);
2467 if (offset > 0) {
2468 if (isl_tab_add_eq(tab, bset->eq[i] + offset) < 0)
2469 goto error;
2470 } else
2471 tab = add_eq(tab, bset->eq[i]);
2472 isl_int_swap(bset->eq[i][offset], cst);
2473 if (!tab)
2474 goto done;
2475 }
2476 for (i = 0; i < bset->n_ineq; ++i) {
2477 int r;
2478 isl_int_swap(bset->ineq[i][offset], cst);
2479 r = isl_tab_add_row(tab, bset->ineq[i] + offset);
2480 isl_int_swap(bset->ineq[i][offset], cst);
2481 if (r < 0)
2482 goto error;
2483 tab->con[r].is_nonneg = 1;
2484 if (isl_tab_push_var(tab, isl_tab_undo_nonneg, &tab->con[r]) < 0)
2485 goto error;
2486 }
2487done:
2488 isl_int_clear(cst);
2489 return tab;
2490error:
2491 isl_int_clear(cst);
2492 isl_tab_free(tab);
2493 return NULL;
2494}
2495
2496/* Assuming "tab" is the tableau of a cone, check if the cone is
2497 * bounded, i.e., if it is empty or only contains the origin.
2498 */
2500{
2501 int i;
2502
2503 if (!tab)
2504 return isl_bool_error;
2505 if (tab->empty)
2506 return isl_bool_true;
2507 if (tab->n_dead == tab->n_col)
2508 return isl_bool_true;
2509
2510 for (;;) {
2511 for (i = tab->n_redundant; i < tab->n_row; ++i) {
2512 struct isl_tab_var *var;
2513 int sgn;
2514 var = isl_tab_var_from_row(tab, i);
2515 if (!var->is_nonneg)
2516 continue;
2517 sgn = sign_of_max(tab, var);
2518 if (sgn < -1)
2519 return isl_bool_error;
2520 if (sgn != 0)
2521 return isl_bool_false;
2522 if (close_row(tab, var, 0) < 0)
2523 return isl_bool_error;
2524 break;
2525 }
2526 if (tab->n_dead == tab->n_col)
2527 return isl_bool_true;
2528 if (i == tab->n_row)
2529 return isl_bool_false;
2530 }
2531}
2532
2534{
2535 int i;
2536
2537 if (!tab)
2538 return -1;
2539
2540 for (i = 0; i < tab->n_var; ++i) {
2541 int row;
2542 if (!tab->var[i].is_row)
2543 continue;
2544 row = tab->var[i].index;
2545 if (!isl_int_is_divisible_by(tab->mat->row[row][1],
2546 tab->mat->row[row][0]))
2547 return 0;
2548 }
2549 return 1;
2550}
2551
2552static struct isl_vec *extract_integer_sample(struct isl_tab *tab)
2553{
2554 int i;
2555 struct isl_vec *vec;
2556
2557 vec = isl_vec_alloc(tab->mat->ctx, 1 + tab->n_var);
2558 if (!vec)
2559 return NULL;
2560
2561 isl_int_set_si(vec->block.data[0], 1);
2562 for (i = 0; i < tab->n_var; ++i) {
2563 if (!tab->var[i].is_row)
2564 isl_int_set_si(vec->block.data[1 + i], 0);
2565 else {
2566 int row = tab->var[i].index;
2567 isl_int_divexact(vec->block.data[1 + i],
2568 tab->mat->row[row][1], tab->mat->row[row][0]);
2569 }
2570 }
2571
2572 return vec;
2573}
2574
2576{
2577 int i;
2578 struct isl_vec *vec;
2579 isl_int m;
2580
2581 if (!tab)
2582 return NULL;
2583
2584 vec = isl_vec_alloc(tab->mat->ctx, 1 + tab->n_var);
2585 if (!vec)
2586 return NULL;
2587
2588 isl_int_init(m);
2589
2590 isl_int_set_si(vec->block.data[0], 1);
2591 for (i = 0; i < tab->n_var; ++i) {
2592 int row;
2593 if (!tab->var[i].is_row) {
2594 isl_int_set_si(vec->block.data[1 + i], 0);
2595 continue;
2596 }
2597 row = tab->var[i].index;
2598 isl_int_gcd(m, vec->block.data[0], tab->mat->row[row][0]);
2599 isl_int_divexact(m, tab->mat->row[row][0], m);
2600 isl_seq_scale(vec->block.data, vec->block.data, m, 1 + i);
2601 isl_int_divexact(m, vec->block.data[0], tab->mat->row[row][0]);
2602 isl_int_mul(vec->block.data[1 + i], m, tab->mat->row[row][1]);
2603 }
2604 vec = isl_vec_normalize(vec);
2605
2607 return vec;
2608}
2609
2610/* Store the sample value of "var" of "tab" rounded up (if sgn > 0)
2611 * or down (if sgn < 0) to the nearest integer in *v.
2612 */
2613static void get_rounded_sample_value(struct isl_tab *tab,
2614 struct isl_tab_var *var, int sgn, isl_int *v)
2615{
2616 if (!var->is_row)
2617 isl_int_set_si(*v, 0);
2618 else if (sgn > 0)
2619 isl_int_cdiv_q(*v, tab->mat->row[var->index][1],
2620 tab->mat->row[var->index][0]);
2621 else
2622 isl_int_fdiv_q(*v, tab->mat->row[var->index][1],
2623 tab->mat->row[var->index][0]);
2624}
2625
2626/* Update "bmap" based on the results of the tableau "tab".
2627 * In particular, implicit equalities are made explicit, redundant constraints
2628 * are removed and if the sample value happens to be integer, it is stored
2629 * in "bmap" (unless "bmap" already had an integer sample).
2630 *
2631 * The tableau is assumed to have been created from "bmap" using
2632 * isl_tab_from_basic_map.
2633 */
2635 __isl_take isl_basic_map *bmap, struct isl_tab *tab)
2636{
2637 int i;
2638 unsigned n_eq;
2639
2640 if (!bmap)
2641 return NULL;
2642 if (!tab)
2643 return bmap;
2644
2645 n_eq = tab->n_eq;
2646 if (tab->empty)
2647 bmap = isl_basic_map_set_to_empty(bmap);
2648 else
2649 for (i = bmap->n_ineq - 1; i >= 0; --i) {
2650 if (isl_tab_is_equality(tab, n_eq + i))
2652 else if (isl_tab_is_redundant(tab, n_eq + i))
2654 }
2655 if (bmap->n_eq != n_eq)
2656 bmap = isl_basic_map_gauss(bmap, NULL);
2657 if (!tab->rational &&
2658 bmap && !bmap->sample && isl_tab_sample_is_integer(tab))
2659 bmap->sample = extract_integer_sample(tab);
2660 return bmap;
2661}
2662
2669
2670/* Drop the last constraint added to "tab" in position "r".
2671 * The constraint is expected to have remained in a row.
2672 */
2673static isl_stat drop_last_con_in_row(struct isl_tab *tab, int r)
2674{
2675 if (!tab->con[r].is_row)
2677 "row unexpectedly moved to column",
2678 return isl_stat_error);
2679 if (r + 1 != tab->n_con)
2681 "additional constraints added", return isl_stat_error);
2682 if (drop_row(tab, tab->con[r].index) < 0)
2683 return isl_stat_error;
2684
2685 return isl_stat_ok;
2686}
2687
2688/* Given a non-negative variable "var", temporarily add a new non-negative
2689 * variable that is the opposite of "var", ensuring that "var" can only attain
2690 * the value zero. The new variable is removed again before this function
2691 * returns. However, the effect of forcing "var" to be zero remains.
2692 * If var = n/d is a row variable, then the new variable = -n/d.
2693 * If var is a column variables, then the new variable = -var.
2694 * If the new variable cannot attain non-negative values, then
2695 * the resulting tableau is empty.
2696 * Otherwise, we know the value will be zero and we close the row.
2697 */
2699{
2700 unsigned r;
2701 isl_int *row;
2702 int sgn;
2703 unsigned off = 2 + tab->M;
2704
2705 if (var->is_zero)
2706 return isl_stat_ok;
2707 if (var->is_redundant || !var->is_nonneg)
2709 "expecting non-redundant non-negative variable",
2710 return isl_stat_error);
2711
2712 if (isl_tab_extend_cons(tab, 1) < 0)
2713 return isl_stat_error;
2714
2715 r = tab->n_con;
2716 tab->con[r].index = tab->n_row;
2717 tab->con[r].is_row = 1;
2718 tab->con[r].is_nonneg = 0;
2719 tab->con[r].is_zero = 0;
2720 tab->con[r].is_redundant = 0;
2721 tab->con[r].frozen = 0;
2722 tab->con[r].negated = 0;
2723 tab->row_var[tab->n_row] = ~r;
2724 row = tab->mat->row[tab->n_row];
2725
2726 if (var->is_row) {
2727 isl_int_set(row[0], tab->mat->row[var->index][0]);
2728 isl_seq_neg(row + 1,
2729 tab->mat->row[var->index] + 1, 1 + tab->n_col);
2730 } else {
2731 isl_int_set_si(row[0], 1);
2732 isl_seq_clr(row + 1, 1 + tab->n_col);
2733 isl_int_set_si(row[off + var->index], -1);
2734 }
2735
2736 tab->n_row++;
2737 tab->n_con++;
2738
2739 sgn = sign_of_max(tab, &tab->con[r]);
2740 if (sgn < -1)
2741 return isl_stat_error;
2742 if (sgn < 0) {
2743 if (drop_last_con_in_row(tab, r) < 0)
2744 return isl_stat_error;
2745 if (isl_tab_mark_empty(tab) < 0)
2746 return isl_stat_error;
2747 return isl_stat_ok;
2748 }
2749 tab->con[r].is_nonneg = 1;
2750 /* sgn == 0 */
2751 if (close_row(tab, &tab->con[r], 1) < 0)
2752 return isl_stat_error;
2753 if (drop_last_con_in_row(tab, r) < 0)
2754 return isl_stat_error;
2755
2756 return isl_stat_ok;
2757}
2758
2759/* Check that "con" is a valid constraint position for "tab".
2760 */
2761static isl_stat isl_tab_check_con(struct isl_tab *tab, int con)
2762{
2763 if (!tab)
2764 return isl_stat_error;
2765 if (con < 0 || con >= tab->n_con)
2767 "position out of bounds", return isl_stat_error);
2768 return isl_stat_ok;
2769}
2770
2771/* Given a tableau "tab" and an inequality constraint "con" of the tableau,
2772 * relax the inequality by one. That is, the inequality r >= 0 is replaced
2773 * by r' = r + 1 >= 0.
2774 * If r is a row variable, we simply increase the constant term by one
2775 * (taking into account the denominator).
2776 * If r is a column variable, then we need to modify each row that
2777 * refers to r = r' - 1 by substituting this equality, effectively
2778 * subtracting the coefficient of the column from the constant.
2779 * We should only do this if the minimum is manifestly unbounded,
2780 * however. Otherwise, we may end up with negative sample values
2781 * for non-negative variables.
2782 * So, if r is a column variable with a minimum that is not
2783 * manifestly unbounded, then we need to move it to a row.
2784 * However, the sample value of this row may be negative,
2785 * even after the relaxation, so we need to restore it.
2786 * We therefore prefer to pivot a column up to a row, if possible.
2787 */
2788int isl_tab_relax(struct isl_tab *tab, int con)
2789{
2790 struct isl_tab_var *var;
2791
2792 if (!tab)
2793 return -1;
2794
2795 var = &tab->con[con];
2796
2797 if (var->is_row && (var->index < 0 || var->index < tab->n_redundant))
2799 "cannot relax redundant constraint", return -1);
2800 if (!var->is_row && (var->index < 0 || var->index < tab->n_dead))
2802 "cannot relax dead constraint", return -1);
2803
2804 if (!var->is_row && !max_is_manifestly_unbounded(tab, var))
2805 if (to_row(tab, var, 1) < 0)
2806 return -1;
2807 if (!var->is_row && !min_is_manifestly_unbounded(tab, var))
2808 if (to_row(tab, var, -1) < 0)
2809 return -1;
2810
2811 if (var->is_row) {
2812 isl_int_add(tab->mat->row[var->index][1],
2813 tab->mat->row[var->index][1], tab->mat->row[var->index][0]);
2814 if (restore_row(tab, var) < 0)
2815 return -1;
2816 } else {
2817 int i;
2818 unsigned off = 2 + tab->M;
2819
2820 for (i = 0; i < tab->n_row; ++i) {
2821 if (isl_int_is_zero(tab->mat->row[i][off + var->index]))
2822 continue;
2823 isl_int_sub(tab->mat->row[i][1], tab->mat->row[i][1],
2824 tab->mat->row[i][off + var->index]);
2825 }
2826
2827 }
2828
2830 return -1;
2831
2832 return 0;
2833}
2834
2835/* Replace the variable v at position "pos" in the tableau "tab"
2836 * by v' = v + shift.
2837 *
2838 * If the variable is in a column, then we first check if we can
2839 * simply plug in v = v' - shift. The effect on a row with
2840 * coefficient f/d for variable v is that the constant term c/d
2841 * is replaced by (c - f * shift)/d. If shift is positive and
2842 * f is negative for each row that needs to remain non-negative,
2843 * then this is clearly safe. In other words, if the minimum of v
2844 * is manifestly unbounded, then we can keep v in a column position.
2845 * Otherwise, we can pivot it down to a row.
2846 * Similarly, if shift is negative, we need to check if the maximum
2847 * of is manifestly unbounded.
2848 *
2849 * If the variable is in a row (from the start or after pivoting),
2850 * then the constant term c/d is replaced by (c + d * shift)/d.
2851 */
2852int isl_tab_shift_var(struct isl_tab *tab, int pos, isl_int shift)
2853{
2854 struct isl_tab_var *var;
2855
2856 if (!tab)
2857 return -1;
2858 if (isl_int_is_zero(shift))
2859 return 0;
2860
2861 var = &tab->var[pos];
2862 if (!var->is_row) {
2863 if (isl_int_is_neg(shift)) {
2865 if (to_row(tab, var, 1) < 0)
2866 return -1;
2867 } else {
2869 if (to_row(tab, var, -1) < 0)
2870 return -1;
2871 }
2872 }
2873
2874 if (var->is_row) {
2875 isl_int_addmul(tab->mat->row[var->index][1],
2876 shift, tab->mat->row[var->index][0]);
2877 } else {
2878 int i;
2879 unsigned off = 2 + tab->M;
2880
2881 for (i = 0; i < tab->n_row; ++i) {
2882 if (isl_int_is_zero(tab->mat->row[i][off + var->index]))
2883 continue;
2884 isl_int_submul(tab->mat->row[i][1],
2885 shift, tab->mat->row[i][off + var->index]);
2886 }
2887
2888 }
2889
2890 return 0;
2891}
2892
2893/* Remove the sign constraint from constraint "con".
2894 *
2895 * If the constraint variable was originally marked non-negative,
2896 * then we make sure we mark it non-negative again during rollback.
2897 */
2898int isl_tab_unrestrict(struct isl_tab *tab, int con)
2899{
2900 struct isl_tab_var *var;
2901
2902 if (!tab)
2903 return -1;
2904
2905 var = &tab->con[con];
2906 if (!var->is_nonneg)
2907 return 0;
2908
2909 var->is_nonneg = 0;
2911 return -1;
2912
2913 return 0;
2914}
2915
2916int isl_tab_select_facet(struct isl_tab *tab, int con)
2917{
2918 if (!tab)
2919 return -1;
2920
2921 return cut_to_hyperplane(tab, &tab->con[con]);
2922}
2923
2924static int may_be_equality(struct isl_tab *tab, int row)
2925{
2926 return tab->rational ? isl_int_is_zero(tab->mat->row[row][1])
2927 : isl_int_lt(tab->mat->row[row][1],
2928 tab->mat->row[row][0]);
2929}
2930
2931/* Return an isl_tab_var that has been marked or NULL if no such
2932 * variable can be found.
2933 * The marked field has only been set for variables that
2934 * appear in non-redundant rows or non-dead columns.
2935 *
2936 * Pick the last constraint variable that is marked and
2937 * that appears in either a non-redundant row or a non-dead columns.
2938 * Since the returned variable is tested for being a redundant constraint or
2939 * an implicit equality, there is no need to return any tab variable that
2940 * corresponds to a variable.
2941 */
2942static struct isl_tab_var *select_marked(struct isl_tab *tab)
2943{
2944 int i;
2945 struct isl_tab_var *var;
2946
2947 for (i = tab->n_con - 1; i >= 0; --i) {
2948 var = &tab->con[i];
2949 if (var->index < 0)
2950 continue;
2951 if (var->is_row && var->index < tab->n_redundant)
2952 continue;
2953 if (!var->is_row && var->index < tab->n_dead)
2954 continue;
2955 if (var->marked)
2956 return var;
2957 }
2958
2959 return NULL;
2960}
2961
2962/* Check for (near) equalities among the constraints.
2963 * A constraint is an equality if it is non-negative and if
2964 * its maximal value is either
2965 * - zero (in case of rational tableaus), or
2966 * - strictly less than 1 (in case of integer tableaus)
2967 *
2968 * We first mark all non-redundant and non-dead variables that
2969 * are not frozen and not obviously not an equality.
2970 * Then we iterate over all marked variables if they can attain
2971 * any values larger than zero or at least one.
2972 * If the maximal value is zero, we mark any column variables
2973 * that appear in the row as being zero and mark the row as being redundant.
2974 * Otherwise, if the maximal value is strictly less than one (and the
2975 * tableau is integer), then we restrict the value to being zero
2976 * by adding an opposite non-negative variable.
2977 * The order in which the variables are considered is not important.
2978 */
2980{
2981 int i;
2982 unsigned n_marked;
2983
2984 if (!tab)
2985 return -1;
2986 if (tab->empty)
2987 return 0;
2988 if (tab->n_dead == tab->n_col)
2989 return 0;
2990
2991 n_marked = 0;
2992 for (i = tab->n_redundant; i < tab->n_row; ++i) {
2993 struct isl_tab_var *var = isl_tab_var_from_row(tab, i);
2994 var->marked = !var->frozen && var->is_nonneg &&
2995 may_be_equality(tab, i);
2996 if (var->marked)
2997 n_marked++;
2998 }
2999 for (i = tab->n_dead; i < tab->n_col; ++i) {
3000 struct isl_tab_var *var = var_from_col(tab, i);
3001 var->marked = !var->frozen && var->is_nonneg;
3002 if (var->marked)
3003 n_marked++;
3004 }
3005 while (n_marked) {
3006 struct isl_tab_var *var;
3007 int sgn;
3008 var = select_marked(tab);
3009 if (!var)
3010 break;
3011 var->marked = 0;
3012 n_marked--;
3013 sgn = sign_of_max(tab, var);
3014 if (sgn < 0)
3015 return -1;
3016 if (sgn == 0) {
3017 if (close_row(tab, var, 0) < 0)
3018 return -1;
3019 } else if (!tab->rational && !at_least_one(tab, var)) {
3020 if (cut_to_hyperplane(tab, var) < 0)
3021 return -1;
3023 }
3024 for (i = tab->n_redundant; i < tab->n_row; ++i) {
3025 var = isl_tab_var_from_row(tab, i);
3026 if (!var->marked)
3027 continue;
3028 if (may_be_equality(tab, i))
3029 continue;
3030 var->marked = 0;
3031 n_marked--;
3032 }
3033 }
3034
3035 return 0;
3036}
3037
3038/* Update the element of row_var or col_var that corresponds to
3039 * constraint tab->con[i] to a move from position "old" to position "i".
3040 */
3041static int update_con_after_move(struct isl_tab *tab, int i, int old)
3042{
3043 int *p;
3044 int index;
3045
3046 index = tab->con[i].index;
3047 if (index == -1)
3048 return 0;
3049 p = tab->con[i].is_row ? tab->row_var : tab->col_var;
3050 if (p[index] != ~old)
3052 "broken internal state", return -1);
3053 p[index] = ~i;
3054
3055 return 0;
3056}
3057
3058/* Interchange constraints "con1" and "con2" in "tab".
3059 * In particular, interchange the contents of these entries in tab->con.
3060 * Since tab->col_var and tab->row_var point back into this array,
3061 * they need to be updated accordingly.
3062 */
3063isl_stat isl_tab_swap_constraints(struct isl_tab *tab, int con1, int con2)
3064{
3065 struct isl_tab_var var;
3066
3067 if (isl_tab_check_con(tab, con1) < 0 ||
3068 isl_tab_check_con(tab, con2) < 0)
3069 return isl_stat_error;
3070
3071 var = tab->con[con1];
3072 tab->con[con1] = tab->con[con2];
3073 if (update_con_after_move(tab, con1, con2) < 0)
3074 return isl_stat_error;
3075 tab->con[con2] = var;
3076 if (update_con_after_move(tab, con2, con1) < 0)
3077 return isl_stat_error;
3078
3079 return isl_stat_ok;
3080}
3081
3082/* Rotate the "n" constraints starting at "first" to the right,
3083 * putting the last constraint in the position of the first constraint.
3084 */
3085static isl_stat rotate_constraints_right(struct isl_tab *tab, int first, int n)
3086{
3087 int i, last;
3088 struct isl_tab_var var;
3089
3090 if (n <= 1)
3091 return isl_stat_ok;
3092
3093 last = first + n - 1;
3094 var = tab->con[last];
3095 for (i = last; i > first; --i) {
3096 tab->con[i] = tab->con[i - 1];
3097 if (update_con_after_move(tab, i, i - 1) < 0)
3098 return isl_stat_error;
3099 }
3100 tab->con[first] = var;
3101 if (update_con_after_move(tab, first, last) < 0)
3102 return isl_stat_error;
3103
3104 return isl_stat_ok;
3105}
3106
3107/* Rotate the "n" constraints starting at "first" to the left,
3108 * putting the first constraint in the position of the last constraint.
3109 */
3110static isl_stat rotate_constraints_left(struct isl_tab *tab, int first, int n)
3111{
3112 int i, last;
3113 struct isl_tab_var var;
3114
3115 if (n <= 1)
3116 return isl_stat_ok;
3117
3118 last = first + n - 1;
3119 var = tab->con[first];
3120 for (i = first; i < last; ++i) {
3121 tab->con[i] = tab->con[i + 1];
3122 if (update_con_after_move(tab, i, i + 1) < 0)
3123 return isl_stat_error;
3124 }
3125 tab->con[last] = var;
3126 if (update_con_after_move(tab, last, first) < 0)
3127 return isl_stat_error;
3128
3129 return isl_stat_ok;
3130}
3131
3132/* Drop the "n" entries starting at position "first" in tab->con, moving all
3133 * subsequent entries down.
3134 * Since some of the entries of tab->row_var and tab->col_var contain
3135 * indices into this array, they have to be updated accordingly.
3136 */
3138 unsigned first, unsigned n)
3139{
3140 int i;
3141
3142 if (first + n > tab->n_con || first + n < first)
3144 "invalid range", return isl_stat_error);
3145
3146 tab->n_con -= n;
3147
3148 for (i = first; i < tab->n_con; ++i) {
3149 tab->con[i] = tab->con[i + n];
3150 if (update_con_after_move(tab, i, i + n) < 0)
3151 return isl_stat_error;
3152 }
3153
3154 return isl_stat_ok;
3155}
3156
3157/* isl_basic_map_gauss5 callback that gets called when
3158 * two (equality) constraints "a" and "b" get interchanged
3159 * in the basic map. Perform the same interchange in "tab".
3160 */
3161static isl_stat swap_eq(unsigned a, unsigned b, void *user)
3162{
3163 struct isl_tab *tab = user;
3164
3165 return isl_tab_swap_constraints(tab, a, b);
3166}
3167
3168/* isl_basic_map_gauss5 callback that gets called when
3169 * the final "n" equality constraints get removed.
3170 * As a special case, if "n" is equal to the total number
3171 * of equality constraints, then this means the basic map
3172 * turned out to be empty.
3173 * Drop the same number of equality constraints from "tab" or
3174 * mark it empty in the special case.
3175 */
3176static isl_stat drop_eq(unsigned n, void *user)
3177{
3178 struct isl_tab *tab = user;
3179
3180 if (tab->n_eq == n)
3181 return isl_tab_mark_empty(tab);
3182
3183 tab->n_eq -= n;
3184 return con_drop_entries(tab, tab->n_eq, n);
3185}
3186
3187/* If "bmap" has more than a single reference, then call
3188 * isl_basic_map_gauss on it, updating "tab" accordingly.
3189 */
3191 struct isl_tab *tab)
3192{
3193 isl_bool single;
3194
3196 if (single < 0)
3197 return isl_basic_map_free(bmap);
3198 if (single)
3199 return bmap;
3200 return isl_basic_map_gauss5(bmap, NULL, &swap_eq, &drop_eq, tab);
3201}
3202
3203/* Make the equalities that are implicit in "bmap" but that have been
3204 * detected in the corresponding "tab" explicit in "bmap" and update
3205 * "tab" to reflect the new order of the constraints.
3206 *
3207 * In particular, if inequality i is an implicit equality then
3208 * isl_basic_map_inequality_to_equality will move the inequality
3209 * in front of the other equality and it will move the last inequality
3210 * in the position of inequality i.
3211 * In the tableau, the inequalities of "bmap" are stored after the equalities
3212 * and so the original order
3213 *
3214 * E E E E E A A A I B B B B L
3215 *
3216 * is changed into
3217 *
3218 * I E E E E E A A A L B B B B
3219 *
3220 * where I is the implicit equality, the E are equalities,
3221 * the A inequalities before I, the B inequalities after I and
3222 * L the last inequality.
3223 * We therefore need to rotate to the right two sets of constraints,
3224 * those up to and including I and those after I.
3225 *
3226 * If "tab" contains any constraints that are not in "bmap" then they
3227 * appear after those in "bmap" and they should be left untouched.
3228 *
3229 * If the operation may need to be undone, then keep track
3230 * of the inequality constraints that have been turned
3231 * into equality constraints.
3232 *
3233 * Note that this function only calls isl_basic_map_gauss
3234 * (in case some equality constraints got detected)
3235 * if "bmap" has more than one reference and if the operation
3236 * does not need to be undone.
3237 * If it only has a single reference, then it is left in a temporary state,
3238 * because the caller may require this state.
3239 * Calling isl_basic_map_gauss is then the responsibility of the caller.
3240 * This is also the case if the operation may need to be undone.
3241 */
3244{
3245 int i;
3246 unsigned n_eq;
3247
3248 if (!tab || !bmap)
3249 return isl_basic_map_free(bmap);
3250 if (tab->empty)
3251 return bmap;
3252
3253 n_eq = tab->n_eq;
3254 for (i = bmap->n_ineq - 1; i >= 0; --i) {
3255 if (!isl_tab_is_equality(tab, bmap->n_eq + i))
3256 continue;
3258 if (rotate_constraints_right(tab, 0, tab->n_eq + i + 1) < 0)
3259 return isl_basic_map_free(bmap);
3260 if (rotate_constraints_right(tab, tab->n_eq + i + 1,
3261 bmap->n_ineq - i) < 0)
3262 return isl_basic_map_free(bmap);
3263 tab->n_eq++;
3264 if (tab->need_undo)
3266 }
3267
3268 if (!tab->need_undo && n_eq != tab->n_eq)
3269 bmap = gauss_if_shared(bmap, tab);
3270
3271 return bmap;
3272}
3273
3274/* Undo the effect of turning an inequality constraint
3275 * into an equality constraint in isl_tab_make_equalities_explicit.
3276 * "ineq" is the original position of the inequality constraint that
3277 * now appears as the first equality constraint.
3278 *
3279 * That is, the order
3280 *
3281 * I E E E E E A A A L B B B B
3282 *
3283 * needs to be changed back into
3284 *
3285 * E E E E E A A A I B B B B L
3286 *
3287 * where I is the inequality turned equality, the E are the original equalities,
3288 * the A inequalities originally before I,
3289 * the B inequalities originally after I and
3290 * L the originally last inequality.
3291 *
3292 * Two groups of constraints therefore need to be rotated left,
3293 * those up to and including the original position of I and
3294 * those after this position.
3295 */
3296static isl_stat first_eq_to_ineq(struct isl_tab *tab, int ineq)
3297{
3298 unsigned n_ineq, n_eq;
3299
3300 if (!tab)
3301 return isl_stat_error;
3302
3303 n_ineq = tab->n_con - tab->n_eq;
3304 tab->n_eq--;
3305 n_eq = tab->n_eq;
3306 if (rotate_constraints_left(tab, 0, n_eq + ineq + 1) < 0)
3307 return isl_stat_error;
3308 if (rotate_constraints_left(tab, n_eq + ineq + 1, n_ineq - ineq) < 0)
3309 return isl_stat_error;
3310 return isl_stat_ok;
3311}
3312
3313static int con_is_redundant(struct isl_tab *tab, struct isl_tab_var *var)
3314{
3315 if (!tab)
3316 return -1;
3317 if (tab->rational) {
3318 int sgn = sign_of_min(tab, var);
3319 if (sgn < -1)
3320 return -1;
3321 return sgn >= 0;
3322 } else {
3323 int irred = isl_tab_min_at_most_neg_one(tab, var);
3324 if (irred < 0)
3325 return -1;
3326 return !irred;
3327 }
3328}
3329
3330/* Check for (near) redundant constraints.
3331 * A constraint is redundant if it is non-negative and if
3332 * its minimal value (temporarily ignoring the non-negativity) is either
3333 * - zero (in case of rational tableaus), or
3334 * - strictly larger than -1 (in case of integer tableaus)
3335 *
3336 * We first mark all non-redundant and non-dead variables that
3337 * are not frozen and not obviously negatively unbounded.
3338 * Then we iterate over all marked variables if they can attain
3339 * any values smaller than zero or at most negative one.
3340 * If not, we mark the row as being redundant (assuming it hasn't
3341 * been detected as being obviously redundant in the mean time).
3342 */
3344{
3345 int i;
3346 unsigned n_marked;
3347
3348 if (!tab)
3349 return -1;
3350 if (tab->empty)
3351 return 0;
3352 if (tab->n_redundant == tab->n_row)
3353 return 0;
3354
3355 n_marked = 0;
3356 for (i = tab->n_redundant; i < tab->n_row; ++i) {
3357 struct isl_tab_var *var = isl_tab_var_from_row(tab, i);
3358 var->marked = !var->frozen && var->is_nonneg;
3359 if (var->marked)
3360 n_marked++;
3361 }
3362 for (i = tab->n_dead; i < tab->n_col; ++i) {
3363 struct isl_tab_var *var = var_from_col(tab, i);
3364 var->marked = !var->frozen && var->is_nonneg &&
3366 if (var->marked)
3367 n_marked++;
3368 }
3369 while (n_marked) {
3370 struct isl_tab_var *var;
3371 int red;
3372 var = select_marked(tab);
3373 if (!var)
3374 break;
3375 var->marked = 0;
3376 n_marked--;
3377 red = con_is_redundant(tab, var);
3378 if (red < 0)
3379 return -1;
3380 if (red && !var->is_redundant)
3381 if (isl_tab_mark_redundant(tab, var->index) < 0)
3382 return -1;
3383 for (i = tab->n_dead; i < tab->n_col; ++i) {
3384 var = var_from_col(tab, i);
3385 if (!var->marked)
3386 continue;
3388 continue;
3389 var->marked = 0;
3390 n_marked--;
3391 }
3392 }
3393
3394 return 0;
3395}
3396
3397int isl_tab_is_equality(struct isl_tab *tab, int con)
3398{
3399 int row;
3400 unsigned off;
3401
3402 if (!tab)
3403 return -1;
3404 if (tab->con[con].is_zero)
3405 return 1;
3406 if (tab->con[con].is_redundant)
3407 return 0;
3408 if (!tab->con[con].is_row)
3409 return tab->con[con].index < tab->n_dead;
3410
3411 row = tab->con[con].index;
3412
3413 off = 2 + tab->M;
3414 return isl_int_is_zero(tab->mat->row[row][1]) &&
3415 !row_is_big(tab, row) &&
3416 !isl_seq_any_non_zero(tab->mat->row[row] + off + tab->n_dead,
3417 tab->n_col - tab->n_dead);
3418}
3419
3420/* Return the minimal value of the affine expression "f" with denominator
3421 * "denom" in *opt, *opt_denom, assuming the tableau is not empty and
3422 * the expression cannot attain arbitrarily small values.
3423 * If opt_denom is NULL, then *opt is rounded up to the nearest integer.
3424 * The return value reflects the nature of the result (empty, unbounded,
3425 * minimal value returned in *opt).
3426 *
3427 * This function assumes that at least one more row and at least
3428 * one more element in the constraint array are available in the tableau.
3429 */
3431 isl_int *f, isl_int denom, isl_int *opt, isl_int *opt_denom,
3432 unsigned flags)
3433{
3434 int r;
3436 struct isl_tab_var *var;
3437 struct isl_tab_undo *snap;
3438
3439 if (!tab)
3440 return isl_lp_error;
3441
3442 if (tab->empty)
3443 return isl_lp_empty;
3444
3445 snap = isl_tab_snap(tab);
3446 r = isl_tab_add_row(tab, f);
3447 if (r < 0)
3448 return isl_lp_error;
3449 var = &tab->con[r];
3450 for (;;) {
3451 int row, col;
3452 find_pivot(tab, var, var, -1, &row, &col);
3453 if (row == var->index) {
3455 break;
3456 }
3457 if (row == -1)
3458 break;
3459 if (isl_tab_pivot(tab, row, col) < 0)
3460 return isl_lp_error;
3461 }
3462 isl_int_mul(tab->mat->row[var->index][0],
3463 tab->mat->row[var->index][0], denom);
3464 if (ISL_FL_ISSET(flags, ISL_TAB_SAVE_DUAL)) {
3465 int i;
3466
3467 isl_vec_free(tab->dual);
3468 tab->dual = isl_vec_alloc(tab->mat->ctx, 1 + tab->n_con);
3469 if (!tab->dual)
3470 return isl_lp_error;
3471 isl_int_set(tab->dual->el[0], tab->mat->row[var->index][0]);
3472 for (i = 0; i < tab->n_con; ++i) {
3473 int pos;
3474 if (tab->con[i].is_row) {
3475 isl_int_set_si(tab->dual->el[1 + i], 0);
3476 continue;
3477 }
3478 pos = 2 + tab->M + tab->con[i].index;
3479 if (tab->con[i].negated)
3480 isl_int_neg(tab->dual->el[1 + i],
3481 tab->mat->row[var->index][pos]);
3482 else
3483 isl_int_set(tab->dual->el[1 + i],
3484 tab->mat->row[var->index][pos]);
3485 }
3486 }
3487 if (opt && res == isl_lp_ok) {
3488 if (opt_denom) {
3489 isl_int_set(*opt, tab->mat->row[var->index][1]);
3490 isl_int_set(*opt_denom, tab->mat->row[var->index][0]);
3491 } else
3492 get_rounded_sample_value(tab, var, 1, opt);
3493 }
3494 if (isl_tab_rollback(tab, snap) < 0)
3495 return isl_lp_error;
3496 return res;
3497}
3498
3499/* Is the constraint at position "con" marked as being redundant?
3500 * If it is marked as representing an equality, then it is not
3501 * considered to be redundant.
3502 * Note that isl_tab_mark_redundant marks both the isl_tab_var as
3503 * redundant and moves the corresponding row into the first
3504 * tab->n_redundant positions (or removes the row, assigning it index -1),
3505 * so the final test is actually redundant itself.
3506 */
3507int isl_tab_is_redundant(struct isl_tab *tab, int con)
3508{
3509 if (isl_tab_check_con(tab, con) < 0)
3510 return -1;
3511 if (tab->con[con].is_zero)
3512 return 0;
3513 if (tab->con[con].is_redundant)
3514 return 1;
3515 return tab->con[con].is_row && tab->con[con].index < tab->n_redundant;
3516}
3517
3518/* Is variable "var" of "tab" fixed to a constant value by its row
3519 * in the tableau?
3520 * If so and if "value" is not NULL, then store this constant value
3521 * in "value".
3522 *
3523 * That is, is it a row variable that only has non-zero coefficients
3524 * for dead columns?
3525 */
3526static isl_bool is_constant(struct isl_tab *tab, struct isl_tab_var *var,
3527 isl_int *value)
3528{
3529 unsigned off = 2 + tab->M;
3530 isl_mat *mat = tab->mat;
3531 int n;
3532 int row;
3533
3534 if (!var->is_row)
3535 return isl_bool_false;
3536 row = var->index;
3537 if (row_is_big(tab, row))
3538 return isl_bool_false;
3539 n = tab->n_col - tab->n_dead;
3540 if (isl_seq_any_non_zero(mat->row[row] + off + tab->n_dead, n))
3541 return isl_bool_false;
3542 if (value)
3543 isl_int_divexact(*value, mat->row[row][1], mat->row[row][0]);
3544 return isl_bool_true;
3545}
3546
3547/* Has the variable "var' of "tab" reached a value that is greater than
3548 * or equal (if sgn > 0) or smaller than or equal (if sgn < 0) to "target"?
3549 * "tmp" has been initialized by the caller and can be used
3550 * to perform local computations.
3551 *
3552 * If the sample value involves the big parameter, then any value
3553 * is reached.
3554 * Otherwise check if n/d >= t, i.e., n >= d * t (if sgn > 0)
3555 * or n/d <= t, i.e., n <= d * t (if sgn < 0).
3556 */
3557static int reached(struct isl_tab *tab, struct isl_tab_var *var, int sgn,
3558 isl_int target, isl_int *tmp)
3559{
3560 if (row_is_big(tab, var->index))
3561 return 1;
3562 isl_int_mul(*tmp, tab->mat->row[var->index][0], target);
3563 if (sgn > 0)
3564 return isl_int_ge(tab->mat->row[var->index][1], *tmp);
3565 else
3566 return isl_int_le(tab->mat->row[var->index][1], *tmp);
3567}
3568
3569/* Can variable "var" of "tab" attain the value "target" by
3570 * pivoting up (if sgn > 0) or down (if sgn < 0)?
3571 * If not, then pivot up [down] to the greatest [smallest]
3572 * rational value.
3573 * "tmp" has been initialized by the caller and can be used
3574 * to perform local computations.
3575 *
3576 * If the variable is manifestly unbounded in the desired direction,
3577 * then it can attain any value.
3578 * Otherwise, it can be moved to a row.
3579 * Continue pivoting until the target is reached.
3580 * If no more pivoting can be performed, the maximal [minimal]
3581 * rational value has been reached and the target cannot be reached.
3582 * If the variable would be pivoted into a manifestly unbounded column,
3583 * then the target can be reached.
3584 */
3585static isl_bool var_reaches(struct isl_tab *tab, struct isl_tab_var *var,
3586 int sgn, isl_int target, isl_int *tmp)
3587{
3588 int row, col;
3589
3590 if (sgn < 0 && min_is_manifestly_unbounded(tab, var))
3591 return isl_bool_true;
3592 if (sgn > 0 && max_is_manifestly_unbounded(tab, var))
3593 return isl_bool_true;
3594 if (to_row(tab, var, sgn) < 0)
3595 return isl_bool_error;
3596 while (!reached(tab, var, sgn, target, tmp)) {
3597 find_pivot(tab, var, var, sgn, &row, &col);
3598 if (row == -1)
3599 return isl_bool_false;
3600 if (row == var->index)
3601 return isl_bool_true;
3602 if (isl_tab_pivot(tab, row, col) < 0)
3603 return isl_bool_error;
3604 }
3605
3606 return isl_bool_true;
3607}
3608
3609/* Check if variable "var" of "tab" can only attain a single (integer)
3610 * value, and, if so, add an equality constraint to fix the variable
3611 * to this single value and store the result in "target".
3612 * "target" and "tmp" have been initialized by the caller.
3613 *
3614 * Given the current sample value, round it down and check
3615 * whether it is possible to attain a strictly smaller integer value.
3616 * If so, the variable is not restricted to a single integer value.
3617 * Otherwise, the search stops at the smallest rational value.
3618 * Round up this value and check whether it is possible to attain
3619 * a strictly greater integer value.
3620 * If so, the variable is not restricted to a single integer value.
3621 * Otherwise, the search stops at the greatest rational value.
3622 * If rounding down this value yields a value that is different
3623 * from rounding up the smallest rational value, then the variable
3624 * cannot attain any integer value. Mark the tableau empty.
3625 * Otherwise, add an equality constraint that fixes the variable
3626 * to the single integer value found.
3627 */
3629 struct isl_tab_var *var, isl_int *target, isl_int *tmp)
3630{
3632 isl_vec *eq;
3633 int pos;
3634 isl_stat r;
3635
3636 get_rounded_sample_value(tab, var, -1, target);
3637 isl_int_sub_ui(*target, *target, 1);
3638 reached = var_reaches(tab, var, -1, *target, tmp);
3639 if (reached < 0 || reached)
3640 return isl_bool_not(reached);
3641 get_rounded_sample_value(tab, var, 1, target);
3642 isl_int_add_ui(*target, *target, 1);
3643 reached = var_reaches(tab, var, 1, *target, tmp);
3644 if (reached < 0 || reached)
3645 return isl_bool_not(reached);
3646 get_rounded_sample_value(tab, var, -1, tmp);
3647 isl_int_sub_ui(*target, *target, 1);
3648 if (isl_int_ne(*target, *tmp)) {
3649 if (isl_tab_mark_empty(tab) < 0)
3650 return isl_bool_error;
3651 return isl_bool_false;
3652 }
3653
3654 if (isl_tab_extend_cons(tab, 1) < 0)
3655 return isl_bool_error;
3656 eq = isl_vec_alloc(isl_tab_get_ctx(tab), 1 + tab->n_var);
3657 if (!eq)
3658 return isl_bool_error;
3659 pos = var - tab->var;
3660 isl_seq_clr(eq->el + 1, tab->n_var);
3661 isl_int_set_si(eq->el[1 + pos], -1);
3662 isl_int_set(eq->el[0], *target);
3663 r = isl_tab_add_eq(tab, eq->el);
3664 isl_vec_free(eq);
3665
3666 return r < 0 ? isl_bool_error : isl_bool_true;
3667}
3668
3669/* Check if variable "var" of "tab" can only attain a single (integer)
3670 * value, and, if so, add an equality constraint to fix the variable
3671 * to this single value and store the result in "value" (if "value"
3672 * is not NULL).
3673 *
3674 * If the current sample value involves the big parameter,
3675 * then the variable cannot have a fixed integer value.
3676 * If the variable is already fixed to a single value by its row, then
3677 * there is no need to add another equality constraint.
3678 *
3679 * Otherwise, allocate some temporary variables and continue
3680 * with detect_constant_with_tmp.
3681 */
3682static isl_bool get_constant(struct isl_tab *tab, struct isl_tab_var *var,
3683 isl_int *value)
3684{
3685 isl_int target, tmp;
3686 isl_bool is_cst;
3687
3688 if (var->is_row && row_is_big(tab, var->index))
3689 return isl_bool_false;
3690 is_cst = is_constant(tab, var, value);
3691 if (is_cst < 0 || is_cst)
3692 return is_cst;
3693
3694 if (!value)
3695 isl_int_init(target);
3696 isl_int_init(tmp);
3697
3698 is_cst = detect_constant_with_tmp(tab, var,
3699 value ? value : &target, &tmp);
3700
3701 isl_int_clear(tmp);
3702 if (!value)
3703 isl_int_clear(target);
3704
3705 return is_cst;
3706}
3707
3708/* Check if variable "var" of "tab" can only attain a single (integer)
3709 * value, and, if so, add an equality constraint to fix the variable
3710 * to this single value and store the result in "value" (if "value"
3711 * is not NULL).
3712 *
3713 * For rational tableaus, nothing needs to be done.
3714 */
3716{
3717 if (!tab)
3718 return isl_bool_error;
3719 if (var < 0 || var >= tab->n_var)
3721 "position out of bounds", return isl_bool_error);
3722 if (tab->rational)
3723 return isl_bool_false;
3724
3725 return get_constant(tab, &tab->var[var], value);
3726}
3727
3728/* Check if any of the variables of "tab" can only attain a single (integer)
3729 * value, and, if so, add equality constraints to fix those variables
3730 * to these single values.
3731 *
3732 * For rational tableaus, nothing needs to be done.
3733 */
3735{
3736 int i;
3737
3738 if (!tab)
3739 return isl_stat_error;
3740 if (tab->rational)
3741 return isl_stat_ok;
3742
3743 for (i = 0; i < tab->n_var; ++i) {
3744 if (get_constant(tab, &tab->var[i], NULL) < 0)
3745 return isl_stat_error;
3746 }
3747
3748 return isl_stat_ok;
3749}
3750
3751/* Take a snapshot of the tableau that can be restored by a call to
3752 * isl_tab_rollback.
3753 */
3755{
3756 if (!tab)
3757 return NULL;
3758 tab->need_undo = 1;
3759 return tab->top;
3760}
3761
3762/* Does "tab" need to keep track of undo information?
3763 * That is, was a snapshot taken that may need to be restored?
3764 */
3766{
3767 if (!tab)
3768 return isl_bool_error;
3769
3770 return isl_bool_ok(tab->need_undo);
3771}
3772
3773/* Remove all tracking of undo information from "tab", invalidating
3774 * any snapshots that may have been taken of the tableau.
3775 * Since all snapshots have been invalidated, there is also
3776 * no need to start keeping track of undo information again.
3777 */
3779{
3780 if (!tab)
3781 return;
3782
3783 free_undo(tab);
3784 tab->need_undo = 0;
3785}
3786
3787/* Undo the operation performed by isl_tab_relax.
3788 */
3789static isl_stat unrelax(struct isl_tab *tab, struct isl_tab_var *var)
3791static isl_stat unrelax(struct isl_tab *tab, struct isl_tab_var *var)
3792{
3793 unsigned off = 2 + tab->M;
3794
3795 if (!var->is_row && !max_is_manifestly_unbounded(tab, var))
3796 if (to_row(tab, var, 1) < 0)
3797 return isl_stat_error;
3798
3799 if (var->is_row) {
3800 isl_int_sub(tab->mat->row[var->index][1],
3801 tab->mat->row[var->index][1], tab->mat->row[var->index][0]);
3802 if (var->is_nonneg) {
3803 int sgn = restore_row(tab, var);
3804 isl_assert(tab->mat->ctx, sgn >= 0,
3805 return isl_stat_error);
3806 }
3807 } else {
3808 int i;
3809
3810 for (i = 0; i < tab->n_row; ++i) {
3811 if (isl_int_is_zero(tab->mat->row[i][off + var->index]))
3812 continue;
3813 isl_int_add(tab->mat->row[i][1], tab->mat->row[i][1],
3814 tab->mat->row[i][off + var->index]);
3815 }
3816
3817 }
3818
3819 return isl_stat_ok;
3820}
3821
3822/* Undo the operation performed by isl_tab_unrestrict.
3823 *
3824 * In particular, mark the variable as being non-negative and make
3825 * sure the sample value respects this constraint.
3826 */
3827static isl_stat ununrestrict(struct isl_tab *tab, struct isl_tab_var *var)
3828{
3829 var->is_nonneg = 1;
3830
3831 if (var->is_row && restore_row(tab, var) < -1)
3832 return isl_stat_error;
3833
3834 return isl_stat_ok;
3835}
3836
3837/* Unmark the last redundant row in "tab" as being redundant.
3838 * This undoes part of the modifications performed by isl_tab_mark_redundant.
3839 * In particular, remove the redundant mark and make
3840 * sure the sample value respects the constraint again.
3841 * A variable that is marked non-negative by isl_tab_mark_redundant
3842 * is covered by a separate undo record.
3843 */
3845{
3846 struct isl_tab_var *var;
3847
3848 if (tab->n_redundant < 1)
3850 "no redundant rows", return isl_stat_error);
3851
3852 var = isl_tab_var_from_row(tab, tab->n_redundant - 1);
3853 var->is_redundant = 0;
3854 tab->n_redundant--;
3855 restore_row(tab, var);
3856
3857 return isl_stat_ok;
3858}
3859
3860static isl_stat perform_undo_var(struct isl_tab *tab, struct isl_tab_undo *undo)
3862static isl_stat perform_undo_var(struct isl_tab *tab, struct isl_tab_undo *undo)
3863{
3864 struct isl_tab_var *var = var_from_index(tab, undo->u.var_index);
3865 switch (undo->type) {
3867 var->is_nonneg = 0;
3868 break;
3870 if (!var->is_row || var->index != tab->n_redundant - 1)
3872 "not undoing last redundant row",
3873 return isl_stat_error);
3874 return restore_last_redundant(tab);
3876 var->frozen = 0;
3877 break;
3878 case isl_tab_undo_zero:
3879 var->is_zero = 0;
3880 if (!var->is_row)
3881 tab->n_dead--;
3882 break;
3884 if (undo->u.var_index >= 0) {
3885 isl_assert(tab->mat->ctx, !var->is_row,
3886 return isl_stat_error);
3887 return drop_col(tab, var->index);
3888 }
3889 if (!var->is_row) {
3890 if (!max_is_manifestly_unbounded(tab, var)) {
3891 if (to_row(tab, var, 1) < 0)
3892 return isl_stat_error;
3893 } else if (!min_is_manifestly_unbounded(tab, var)) {
3894 if (to_row(tab, var, -1) < 0)
3895 return isl_stat_error;
3896 } else
3897 if (to_row(tab, var, 0) < 0)
3898 return isl_stat_error;
3899 }
3900 return drop_row(tab, var->index);
3901 case isl_tab_undo_relax:
3902 return unrelax(tab, var);
3904 return ununrestrict(tab, var);
3905 default:
3907 "perform_undo_var called on invalid undo record",
3908 return isl_stat_error);
3909 }
3910
3911 return isl_stat_ok;
3912}
3913
3914/* Restore all rows that have been marked redundant by isl_tab_mark_redundant
3915 * and that have been preserved in the tableau.
3916 * Note that isl_tab_mark_redundant may also have marked some variables
3917 * as being non-negative before marking them redundant. These need
3918 * to be removed as well as otherwise some constraints could end up
3919 * getting marked redundant with respect to the variable.
3920 */
3922{
3923 if (!tab)
3924 return isl_stat_error;
3925
3926 if (tab->need_undo)
3928 "manually restoring redundant constraints "
3929 "interferes with undo history",
3930 return isl_stat_error);
3931
3932 while (tab->n_redundant > 0) {
3933 if (tab->row_var[tab->n_redundant - 1] >= 0) {
3934 struct isl_tab_var *var;
3935
3936 var = isl_tab_var_from_row(tab, tab->n_redundant - 1);
3937 var->is_nonneg = 0;
3938 }
3940 }
3941 return isl_stat_ok;
3942}
3943
3944/* Undo the addition of an integer division to the basic map representation
3945 * of "tab" in position "pos".
3946 */
3947static isl_stat drop_bmap_div(struct isl_tab *tab, int pos)
3948{
3949 int off;
3950 isl_size n_div;
3951
3952 n_div = isl_basic_map_dim(tab->bmap, isl_dim_div);
3953 if (n_div < 0)
3954 return isl_stat_error;
3955 off = tab->n_var - n_div;
3956 tab->bmap = isl_basic_map_drop_div(tab->bmap, pos - off);
3957 if (!tab->bmap)
3958 return isl_stat_error;
3959 if (tab->samples) {
3960 tab->samples = isl_mat_drop_cols(tab->samples, 1 + pos, 1);
3961 if (!tab->samples)
3962 return isl_stat_error;
3963 }
3964
3965 return isl_stat_ok;
3966}
3967
3968/* Restore the tableau to the state where the basic variables
3969 * are those in "col_var".
3970 * We first construct a list of variables that are currently in
3971 * the basis, but shouldn't. Then we iterate over all variables
3972 * that should be in the basis and for each one that is currently
3973 * not in the basis, we exchange it with one of the elements of the
3974 * list constructed before.
3975 * We can always find an appropriate variable to pivot with because
3976 * the current basis is mapped to the old basis by a non-singular
3977 * matrix and so we can never end up with a zero row.
3978 */
3979static int restore_basis(struct isl_tab *tab, int *col_var)
3980{
3981 int i, j;
3982 int n_extra = 0;
3983 int *extra = NULL; /* current columns that contain bad stuff */
3984 unsigned off = 2 + tab->M;
3985
3986 extra = isl_alloc_array(tab->mat->ctx, int, tab->n_col);
3987 if (tab->n_col && !extra)
3988 goto error;
3989 for (i = 0; i < tab->n_col; ++i) {
3990 for (j = 0; j < tab->n_col; ++j)
3991 if (tab->col_var[i] == col_var[j])
3992 break;
3993 if (j < tab->n_col)
3994 continue;
3995 extra[n_extra++] = i;
3996 }
3997 for (i = 0; i < tab->n_col && n_extra > 0; ++i) {
3998 struct isl_tab_var *var;
3999 int row;
4000
4001 for (j = 0; j < tab->n_col; ++j)
4002 if (col_var[i] == tab->col_var[j])
4003 break;
4004 if (j < tab->n_col)
4005 continue;
4006 var = var_from_index(tab, col_var[i]);
4007 row = var->index;
4008 for (j = 0; j < n_extra; ++j)
4009 if (!isl_int_is_zero(tab->mat->row[row][off+extra[j]]))
4010 break;
4011 isl_assert(tab->mat->ctx, j < n_extra, goto error);
4012 if (isl_tab_pivot(tab, row, extra[j]) < 0)
4013 goto error;
4014 extra[j] = extra[--n_extra];
4015 }
4016
4017 free(extra);
4018 return 0;
4019error:
4020 free(extra);
4021 return -1;
4022}
4023
4024/* Remove all samples with index n or greater, i.e., those samples
4025 * that were added since we saved this number of samples in
4026 * isl_tab_save_samples.
4027 */
4028static void drop_samples_since(struct isl_tab *tab, int n)
4029{
4030 int i;
4031
4032 for (i = tab->n_sample - 1; i >= 0 && tab->n_sample > n; --i) {
4033 if (tab->sample_index[i] < n)
4034 continue;
4035
4036 if (i != tab->n_sample - 1) {
4037 int t = tab->sample_index[tab->n_sample-1];
4038 tab->sample_index[tab->n_sample-1] = tab->sample_index[i];
4039 tab->sample_index[i] = t;
4040 isl_mat_swap_rows(tab->samples, tab->n_sample-1, i);
4041 }
4042 tab->n_sample--;
4043 }
4044}
4045
4046static isl_stat perform_undo(struct isl_tab *tab, struct isl_tab_undo *undo)
4048static isl_stat perform_undo(struct isl_tab *tab, struct isl_tab_undo *undo)
4049{
4050 switch (undo->type) {
4052 tab->rational = 0;
4053 break;
4054 case isl_tab_undo_empty:
4055 tab->empty = 0;
4056 break;
4060 case isl_tab_undo_zero:
4062 case isl_tab_undo_relax:
4064 return perform_undo_var(tab, undo);
4066 tab->bmap = isl_basic_map_free_equality(tab->bmap, 1);
4067 return tab->bmap ? isl_stat_ok : isl_stat_error;
4069 tab->bmap = isl_basic_map_free_inequality(tab->bmap, 1);
4070 return tab->bmap ? isl_stat_ok : isl_stat_error;
4072 return drop_bmap_div(tab, undo->u.var_index);
4074 if (restore_basis(tab, undo->u.col_var) < 0)
4075 return isl_stat_error;
4076 break;
4078 tab->n_outside--;
4079 break;
4081 drop_samples_since(tab, undo->u.n);
4082 break;
4084 return undo->u.callback->run(undo->u.callback);
4086 return first_eq_to_ineq(tab, undo->u.n);
4087 default:
4088 isl_assert(tab->mat->ctx, 0, return isl_stat_error);
4089 }
4090 return isl_stat_ok;
4091}
4092
4093/* Return the tableau to the state it was in when the snapshot "snap"
4094 * was taken.
4095 */
4097{
4098 struct isl_tab_undo *undo, *next;
4099
4100 if (!tab)
4101 return isl_stat_error;
4102
4103 tab->in_undo = 1;
4104 for (undo = tab->top; undo && undo != &tab->bottom; undo = next) {
4105 next = undo->next;
4106 if (undo == snap)
4107 break;
4108 if (perform_undo(tab, undo) < 0) {
4109 tab->top = undo;
4110 free_undo(tab);
4111 tab->in_undo = 0;
4112 return isl_stat_error;
4113 }
4114 free_undo_record(undo);
4115 }
4116 tab->in_undo = 0;
4117 tab->top = undo;
4118 if (!undo)
4119 return isl_stat_error;
4120 return isl_stat_ok;
4121}
4122
4123/* The given row "row" represents an inequality violated by all
4124 * points in the tableau. Check for some special cases of such
4125 * separating constraints.
4126 * In particular, if the row has been reduced to the constant -1,
4127 * then we know the inequality is adjacent (but opposite) to
4128 * an equality in the tableau.
4129 * If the row has been reduced to r = c*(-1 -r'), with r' an inequality
4130 * of the tableau and c a positive constant, then the inequality
4131 * is adjacent (but opposite) to the inequality r'.
4132 */
4133static enum isl_ineq_type separation_type(struct isl_tab *tab, unsigned row)
4134{
4135 int pos;
4136 int separate;
4137 unsigned off = 2 + tab->M;
4138
4139 if (tab->rational)
4140 return isl_ineq_separate;
4141
4142 if (!isl_int_is_one(tab->mat->row[row][0]))
4143 return isl_ineq_separate;
4144
4145 pos = isl_seq_first_non_zero(tab->mat->row[row] + off + tab->n_dead,
4146 tab->n_col - tab->n_dead);
4147 if (pos == -1) {
4148 if (isl_int_is_negone(tab->mat->row[row][1]))
4149 return isl_ineq_adj_eq;
4150 else
4151 return isl_ineq_separate;
4152 }
4153
4154 if (!isl_int_eq(tab->mat->row[row][1],
4155 tab->mat->row[row][off + tab->n_dead + pos]))
4156 return isl_ineq_separate;
4157
4158 separate = isl_seq_any_non_zero(
4159 tab->mat->row[row] + off + tab->n_dead + pos + 1,
4160 tab->n_col - tab->n_dead - pos - 1);
4161
4162 return !separate ? isl_ineq_adj_ineq : isl_ineq_separate;
4163}
4164
4165/* Check the effect of inequality "ineq" on the tableau "tab".
4166 * The result may be
4167 * isl_ineq_redundant: satisfied by all points in the tableau
4168 * isl_ineq_separate: satisfied by no point in the tableau
4169 * isl_ineq_cut: satisfied by some by not all points
4170 * isl_ineq_adj_eq: adjacent to an equality
4171 * isl_ineq_adj_ineq: adjacent to an inequality.
4172 */
4174{
4176 struct isl_tab_undo *snap = NULL;
4177 int con;
4178 int row;
4179
4180 if (!tab)
4181 return isl_ineq_error;
4182
4183 if (isl_tab_extend_cons(tab, 1) < 0)
4184 return isl_ineq_error;
4185
4186 snap = isl_tab_snap(tab);
4187
4188 con = isl_tab_add_row(tab, ineq);
4189 if (con < 0)
4190 goto error;
4191
4192 row = tab->con[con].index;
4193 if (isl_tab_row_is_redundant(tab, row))
4195 else if (isl_int_is_neg(tab->mat->row[row][1]) &&
4196 (tab->rational ||
4197 isl_int_abs_ge(tab->mat->row[row][1],
4198 tab->mat->row[row][0]))) {
4199 int nonneg = at_least_zero(tab, &tab->con[con]);
4200 if (nonneg < 0)
4201 goto error;
4202 if (nonneg)
4204 else
4205 type = separation_type(tab, row);
4206 } else {
4207 int red = con_is_redundant(tab, &tab->con[con]);
4208 if (red < 0)
4209 goto error;
4210 if (!red)
4212 else
4214 }
4215
4216 if (isl_tab_rollback(tab, snap))
4217 return isl_ineq_error;
4218 return type;
4219error:
4220 return isl_ineq_error;
4221}
4222
4224{
4225 bmap = isl_basic_map_cow(bmap);
4226 if (!tab || !bmap)
4227 goto error;
4228
4229 if (tab->empty) {
4230 bmap = isl_basic_map_set_to_empty(bmap);
4231 if (!bmap)
4232 goto error;
4233 tab->bmap = bmap;
4234 return isl_stat_ok;
4235 }
4236
4237 isl_assert(tab->mat->ctx, tab->n_eq == bmap->n_eq, goto error);
4238 isl_assert(tab->mat->ctx,
4239 tab->n_con == bmap->n_eq + bmap->n_ineq, goto error);
4240
4241 tab->bmap = bmap;
4242
4243 return isl_stat_ok;
4244error:
4245 isl_basic_map_free(bmap);
4246 return isl_stat_error;
4247}
4248
4250{
4251 return isl_tab_track_bmap(tab, bset_to_bmap(bset));
4252}
4253
4255{
4256 if (!tab)
4257 return NULL;
4258
4259 return bset_from_bmap(tab->bmap);
4260}
4261
4262/* Print information about a tab variable representing a variable or
4263 * a constraint.
4264 * In particular, print its position (row or column) in the tableau and
4265 * an indication of whether it is zero, redundant and/or frozen.
4266 * Note that only constraints can be frozen.
4267 */
4268static void print_tab_var(FILE *out, struct isl_tab_var *var)
4269{
4270 fprintf(out, "%c%d%s%s", var->is_row ? 'r' : 'c',
4271 var->index,
4272 var->is_zero ? " [=0]" :
4273 var->is_redundant ? " [R]" : "",
4274 var->frozen ? " [F]" : "");
4275}
4276
4278 FILE *out, int indent)
4279{
4280 unsigned r, c;
4281 int i;
4282
4283 if (!tab) {
4284 fprintf(out, "%*snull tab\n", indent, "");
4285 return;
4286 }
4287 fprintf(out, "%*sn_redundant: %d, n_dead: %d", indent, "",
4288 tab->n_redundant, tab->n_dead);
4289 if (tab->rational)
4290 fprintf(out, ", rational");
4291 if (tab->empty)
4292 fprintf(out, ", empty");
4293 fprintf(out, "\n");
4294 fprintf(out, "%*s[", indent, "");
4295 for (i = 0; i < tab->n_var; ++i) {
4296 if (i)
4297 fprintf(out, (i == tab->n_param ||
4298 i == tab->n_var - tab->n_div) ? "; "
4299 : ", ");
4300 print_tab_var(out, &tab->var[i]);
4301 }
4302 fprintf(out, "]\n");
4303 fprintf(out, "%*s[", indent, "");
4304 for (i = 0; i < tab->n_con; ++i) {
4305 if (i)
4306 fprintf(out, ", ");
4307 print_tab_var(out, &tab->con[i]);
4308 }
4309 fprintf(out, "]\n");
4310 fprintf(out, "%*s[", indent, "");
4311 for (i = 0; i < tab->n_row; ++i) {
4312 const char *sign = "";
4313 if (i)
4314 fprintf(out, ", ");
4315 if (tab->row_sign) {
4316 if (tab->row_sign[i] == isl_tab_row_unknown)
4317 sign = "?";
4318 else if (tab->row_sign[i] == isl_tab_row_neg)
4319 sign = "-";
4320 else if (tab->row_sign[i] == isl_tab_row_pos)
4321 sign = "+";
4322 else
4323 sign = "+-";
4324 }
4325 fprintf(out, "r%d: %d%s%s", i, tab->row_var[i],
4326 isl_tab_var_from_row(tab, i)->is_nonneg ? " [>=0]" : "", sign);
4327 }
4328 fprintf(out, "]\n");
4329 fprintf(out, "%*s[", indent, "");
4330 for (i = 0; i < tab->n_col; ++i) {
4331 if (i)
4332 fprintf(out, ", ");
4333 fprintf(out, "c%d: %d%s", i, tab->col_var[i],
4334 var_from_col(tab, i)->is_nonneg ? " [>=0]" : "");
4335 }
4336 fprintf(out, "]\n");
4337 r = tab->mat->n_row;
4338 tab->mat->n_row = tab->n_row;
4339 c = tab->mat->n_col;
4340 tab->mat->n_col = 2 + tab->M + tab->n_col;
4341 isl_mat_print_internal(tab->mat, out, indent);
4342 tab->mat->n_row = r;
4343 tab->mat->n_col = c;
4344 if (tab->bmap)
4345 isl_basic_map_print_internal(tab->bmap, out, indent);
4346}
4347
4349{
4350 isl_tab_print_internal(tab, stderr, 0);
4351}
static __isl_give isl_basic_set * bset_from_bmap(__isl_take isl_basic_map *bmap)
static __isl_give isl_basic_map * bset_to_bmap(__isl_take isl_basic_set *bset)
Definition bset_to_bmap.c:7
#define __isl_take
Definition ctx.h:22
#define isl_calloc_type(ctx, type)
Definition ctx.h:130
isl_stat
Definition ctx.h:84
@ isl_stat_error
Definition ctx.h:85
@ isl_stat_ok
Definition ctx.h:86
#define __isl_give
Definition ctx.h:19
#define ISL_FL_ISSET(l, f)
Definition ctx.h:113
#define isl_die(ctx, errno, msg, code)
Definition ctx.h:138
#define isl_assert(ctx, test, code)
Definition ctx.h:153
isl_bool isl_bool_ok(int b)
Definition isl_ctx.c:58
@ isl_error_invalid
Definition ctx.h:80
@ isl_error_internal
Definition ctx.h:79
#define isl_alloc_array(ctx, type, n)
Definition ctx.h:132
#define ISL_F_ISSET(p, f)
Definition ctx.h:118
#define __isl_keep
Definition ctx.h:25
int isl_size
Definition ctx.h:97
#define isl_alloc_type(ctx, type)
Definition ctx.h:129
#define isl_realloc_array(ctx, ptr, type, n)
Definition ctx.h:135
isl_bool isl_bool_not(isl_bool b)
Definition isl_ctx.c:44
isl_bool
Definition ctx.h:89
@ isl_bool_false
Definition ctx.h:91
@ isl_bool_true
Definition ctx.h:92
@ isl_bool_error
Definition ctx.h:90
m
Definition guard1-0.c:2
isl_stat isl_stat void * user
Definition hmap.h:39
int GMPQAPI sgn(mp_rat op)
static __isl_give isl_ast_expr * var(struct isl_ast_add_term_data *data, enum isl_dim_type type, int pos)
#define WARN_UNUSED
#define __attribute__(x)
int isl_ctx_next_operation(isl_ctx *ctx)
Definition isl_ctx.c:83
#define isl_int_is_zero(i)
Definition isl_int.h:31
#define isl_int_is_one(i)
Definition isl_int.h:32
#define isl_int_is_pos(i)
Definition isl_int.h:34
#define isl_int_is_negone(i)
Definition isl_int.h:33
#define isl_int_is_neg(i)
Definition isl_int.h:35
#define isl_int_gcd(r, i, j)
Definition isl_int_gmp.h:42
#define isl_int_neg(r, i)
Definition isl_int_gmp.h:24
#define isl_int_add_ui(r, i, j)
Definition isl_int_gmp.h:27
#define isl_int_le(i, j)
Definition isl_int_gmp.h:60
#define isl_int_add(r, i, j)
Definition isl_int_gmp.h:30
#define isl_int_abs_ge(i, j)
Definition isl_int_gmp.h:68
#define isl_int_ne(i, j)
Definition isl_int_gmp.h:58
#define isl_int_addmul(r, i, j)
Definition isl_int_gmp.h:37
#define isl_int_is_divisible_by(i, j)
Definition isl_int_gmp.h:69
#define isl_int_eq(i, j)
Definition isl_int_gmp.h:57
#define isl_int_set(r, i)
Definition isl_int_gmp.h:14
#define isl_int_cdiv_q(r, i, j)
Definition isl_int_gmp.h:47
#define isl_int_lcm(r, i, j)
Definition isl_int_gmp.h:43
#define isl_int_divexact(r, i, j)
Definition isl_int_gmp.h:44
#define isl_int_sgn(i)
Definition isl_int_gmp.h:54
#define isl_int_mul(r, i, j)
Definition isl_int_gmp.h:32
#define isl_int_lt(i, j)
Definition isl_int_gmp.h:59
#define isl_int_set_si(r, i)
Definition isl_int_gmp.h:15
#define isl_int_ge(i, j)
Definition isl_int_gmp.h:62
mpz_t isl_int
Definition isl_int_gmp.h:9
#define isl_int_sub_ui(r, i, j)
Definition isl_int_gmp.h:28
#define isl_int_fdiv_q(r, i, j)
Definition isl_int_gmp.h:49
#define isl_int_swap(i, j)
Definition isl_int_gmp.h:25
#define isl_int_sub(r, i, j)
Definition isl_int_gmp.h:31
#define isl_int_init(i)
Definition isl_int_gmp.h:11
#define isl_int_clear(i)
Definition isl_int_gmp.h:12
#define isl_int_submul(r, i, j)
Definition isl_int_gmp.h:39
__isl_give dup(__isl_keep LIST(EL) *list)
__isl_give isl_basic_map * isl_basic_map_free_inequality(__isl_take isl_basic_map *bmap, unsigned n)
Definition isl_map.c:1767
__isl_give isl_basic_map * isl_basic_map_add_ineq(__isl_take isl_basic_map *bmap, isl_int *ineq)
Definition isl_map.c:1843
void isl_basic_map_inequality_to_equality(__isl_keep isl_basic_map *bmap, unsigned pos)
Definition isl_map.c:1719
static unsigned pos(__isl_keep isl_space *space, enum isl_dim_type type)
Definition isl_map.c:73
__isl_give isl_basic_map * isl_basic_map_set_to_empty(__isl_take isl_basic_map *bmap)
Definition isl_map.c:2177
__isl_give isl_basic_map * isl_basic_map_free_equality(__isl_take isl_basic_map *bmap, unsigned n)
Definition isl_map.c:1671
int isl_basic_map_drop_inequality(__isl_keep isl_basic_map *bmap, unsigned pos)
Definition isl_map.c:1787
__isl_give isl_basic_map * isl_basic_map_cow(__isl_take isl_basic_map *bmap)
Definition isl_map.c:2064
__isl_give isl_basic_map * isl_basic_map_insert_div(__isl_take isl_basic_map *bmap, int pos, __isl_keep isl_vec *div)
Definition isl_map.c:1909
__isl_give isl_basic_map * isl_basic_map_drop_div(__isl_take isl_basic_map *bmap, unsigned div)
Definition isl_map.c:2707
#define ISL_BASIC_SET_RATIONAL
#define ISL_BASIC_MAP_EMPTY
__isl_give isl_basic_map * isl_basic_map_gauss(__isl_take isl_basic_map *bmap, int *progress)
#define ISL_BASIC_MAP_RATIONAL
isl_bool isl_basic_map_has_single_reference(__isl_keep isl_basic_map *bmap)
__isl_give isl_basic_map * isl_basic_map_gauss5(__isl_take isl_basic_map *bmap, int *progress, isl_stat(*swap)(unsigned a, unsigned b, void *user), isl_stat(*drop)(unsigned n, void *user), void *user)
#define isl_basic_set
__isl_give isl_mat * isl_mat_dup(__isl_keep isl_mat *mat)
Definition isl_mat.c:211
void isl_seq_combine(isl_int *dst, isl_int m1, isl_int *src1, isl_int m2, isl_int *src2, unsigned len)
Definition isl_seq.c:116
int isl_seq_first_non_zero(isl_int *p, unsigned len)
Definition isl_seq.c:207
void isl_seq_clr(isl_int *p, unsigned len)
Definition isl_seq.c:14
void isl_seq_scale(isl_int *dst, isl_int *src, isl_int m, unsigned len)
Definition isl_seq.c:81
void isl_seq_cpy(isl_int *dst, isl_int *src, unsigned len)
Definition isl_seq.c:42
int isl_seq_any_non_zero(isl_int *p, unsigned len)
Definition isl_seq.c:230
void isl_seq_normalize(struct isl_ctx *ctx, isl_int *p, unsigned len)
Definition isl_seq.c:278
void isl_seq_neg(isl_int *dst, isl_int *src, unsigned len)
Definition isl_seq.c:35
int isl_tab_detect_redundant(struct isl_tab *tab)
Definition isl_tab.c:3343
static isl_stat con_drop_entries(struct isl_tab *tab, unsigned first, unsigned n)
Definition isl_tab.c:3137
struct isl_tab * isl_tab_drop_sample(struct isl_tab *tab, int s)
Definition isl_tab.c:923
int isl_tab_min_at_most_neg_one(struct isl_tab *tab, struct isl_tab_var *var)
Definition isl_tab.c:1464
void isl_tab_free(struct isl_tab *tab)
Definition isl_tab.c:204
static isl_stat perform_undo(struct isl_tab *tab, struct isl_tab_undo *undo) WARN_UNUSED
Definition isl_tab.c:4048
static struct isl_tab * add_eq(struct isl_tab *tab, isl_int *eq)
Definition isl_tab.c:2001
static int sign_of_min(struct isl_tab *tab, struct isl_tab_var *var)
Definition isl_tab.c:1388
static isl_stat push_union(struct isl_tab *tab, enum isl_tab_undo_type type, union isl_tab_undo_val u) WARN_UNUSED
Definition isl_tab.c:799
static int sign_of_max(struct isl_tab *tab, struct isl_tab_var *var)
Definition isl_tab.c:1274
static int at_least_one(struct isl_tab *tab, struct isl_tab_var *var)
Definition isl_tab.c:1523
static isl_stat close_row(struct isl_tab *tab, struct isl_tab_var *var, int temp_var) WARN_UNUSED
Definition isl_tab.c:1645
static int con_is_redundant(struct isl_tab *tab, struct isl_tab_var *var)
Definition isl_tab.c:3313
static struct isl_tab_var * select_marked(struct isl_tab *tab)
Definition isl_tab.c:2942
static isl_stat restore_last_redundant(struct isl_tab *tab)
Definition isl_tab.c:3844
static int reached(struct isl_tab *tab, struct isl_tab_var *var, int sgn, isl_int target, isl_int *tmp)
Definition isl_tab.c:3557
static struct isl_tab_var * var_from_col(struct isl_tab *tab, int i)
Definition isl_tab.c:576
static void update_row_sign(struct isl_tab *tab, int row, int col, int row_sgn)
Definition isl_tab.c:1054
static int to_col(struct isl_tab *tab, struct isl_tab_var *var) WARN_UNUSED
Definition isl_tab.c:1964
isl_stat isl_tab_push_basis(struct isl_tab *tab)
Definition isl_tab.c:844
static void print_tab_var(FILE *out, struct isl_tab_var *var)
Definition isl_tab.c:4268
void isl_tab_clear_undo(struct isl_tab *tab)
Definition isl_tab.c:3778
int isl_tab_insert_var(struct isl_tab *tab, int r)
Definition isl_tab.c:1771
isl_stat isl_tab_track_bset(struct isl_tab *tab, __isl_take isl_basic_set *bset)
Definition isl_tab.c:4249
struct isl_tab * isl_tab_init_samples(struct isl_tab *tab)
Definition isl_tab.c:875
int isl_tab_extend_vars(struct isl_tab *tab, unsigned n_new)
Definition isl_tab.c:151
struct isl_tab * isl_tab_product(struct isl_tab *tab1, struct isl_tab *tab2)
Definition isl_tab.c:434
int isl_tab_add_sample(struct isl_tab *tab, __isl_take isl_vec *sample)
Definition isl_tab.c:894
static isl_stat drop_last_con_in_row(struct isl_tab *tab, int r)
Definition isl_tab.c:2673
static void swap_cols(struct isl_tab *tab, int col1, int col2)
Definition isl_tab.c:1545
static enum isl_ineq_type separation_type(struct isl_tab *tab, unsigned row)
Definition isl_tab.c:4133
static isl_stat swap_eq(unsigned a, unsigned b, void *user)
Definition isl_tab.c:3161
int isl_tab_is_redundant(struct isl_tab *tab, int con)
Definition isl_tab.c:3507
struct isl_tab_var * isl_tab_var_from_row(struct isl_tab *tab, int i)
Definition isl_tab.c:571
static int tab_is_manifestly_empty(struct isl_tab *tab)
Definition isl_tab.c:1607
static isl_bool get_constant(struct isl_tab *tab, struct isl_tab_var *var, isl_int *value)
Definition isl_tab.c:3682
static void drop_samples_since(struct isl_tab *tab, int n)
Definition isl_tab.c:4028
int isl_tab_mark_rational(struct isl_tab *tab)
Definition isl_tab.c:995
static int var_insert_entry(struct isl_tab *tab, int first)
Definition isl_tab.c:1717
static int at_least_zero(struct isl_tab *tab, struct isl_tab_var *var)
Definition isl_tab.c:1355
isl_stat isl_tab_track_bmap(struct isl_tab *tab, __isl_take isl_basic_map *bmap)
Definition isl_tab.c:4223
isl_stat isl_tab_add_eq(struct isl_tab *tab, isl_int *eq)
Definition isl_tab.c:2118
static isl_bool is_constant(struct isl_tab *tab, struct isl_tab_var *var, isl_int *value)
Definition isl_tab.c:3526
static isl_stat first_eq_to_ineq(struct isl_tab *tab, int ineq)
Definition isl_tab.c:3296
static isl_stat ununrestrict(struct isl_tab *tab, struct isl_tab_var *var)
Definition isl_tab.c:3827
int isl_tab_mark_redundant(struct isl_tab *tab, int row)
Definition isl_tab.c:966
isl_stat isl_tab_swap_constraints(struct isl_tab *tab, int con1, int con2)
Definition isl_tab.c:3063
static struct isl_vec * extract_integer_sample(struct isl_tab *tab)
Definition isl_tab.c:2552
__isl_keep isl_basic_set * isl_tab_peek_bset(struct isl_tab *tab)
Definition isl_tab.c:4254
int isl_tab_pivot(struct isl_tab *tab, int row, int col)
Definition isl_tab.c:1135
static isl_stat perform_undo_var(struct isl_tab *tab, struct isl_tab_undo *undo) WARN_UNUSED
Definition isl_tab.c:3862
static int var_drop_entry(struct isl_tab *tab, int first)
Definition isl_tab.c:1746
isl_stat isl_tab_push_callback(struct isl_tab *tab, struct isl_tab_callback *callback)
Definition isl_tab.c:857
static isl_stat rotate_constraints_left(struct isl_tab *tab, int first, int n)
Definition isl_tab.c:3110
int isl_tab_relax(struct isl_tab *tab, int con)
Definition isl_tab.c:2788
static __isl_give isl_vec * ineq_for_div(__isl_keep isl_basic_map *bmap, unsigned div)
Definition isl_tab.c:2210
static int may_be_equality(struct isl_tab *tab, int row)
Definition isl_tab.c:2924
static isl_stat unrelax(struct isl_tab *tab, struct isl_tab_var *var) WARN_UNUSED
Definition isl_tab.c:3791
int isl_tab_add_row(struct isl_tab *tab, isl_int *line)
Definition isl_tab.c:1822
static struct isl_tab_var * var_from_index(struct isl_tab *tab, int i)
Definition isl_tab.c:563
static int row_is_manifestly_non_integral(struct isl_tab *tab, int row)
Definition isl_tab.c:1589
static isl_stat drop_col(struct isl_tab *tab, int col)
Definition isl_tab.c:1887
struct isl_tab_undo * isl_tab_snap(struct isl_tab *tab)
Definition isl_tab.c:3754
isl_stat isl_tab_restore_redundant(struct isl_tab *tab)
Definition isl_tab.c:3921
static int min_is_manifestly_unbounded(struct isl_tab *tab, struct isl_tab_var *var)
Definition isl_tab.c:606
int isl_tab_unrestrict(struct isl_tab *tab, int con)
Definition isl_tab.c:2898
int isl_tab_sign_of_max(struct isl_tab *tab, int con)
Definition isl_tab.c:1294
static isl_stat isl_tab_push_ineq_to_eq(struct isl_tab *tab, int ineq)
Definition isl_tab.c:868
int isl_tab_is_equality(struct isl_tab *tab, int con)
Definition isl_tab.c:3397
int isl_tab_kill_col(struct isl_tab *tab, int col)
Definition isl_tab.c:1569
__isl_give isl_basic_set * isl_basic_set_update_from_tab(__isl_take isl_basic_set *bset, struct isl_tab *tab)
Definition isl_tab.c:2663
struct isl_tab * isl_tab_from_recession_cone(__isl_keep isl_basic_set *bset, int parametric)
Definition isl_tab.c:2442
static isl_stat add_div_constraints(struct isl_tab *tab, unsigned div, isl_stat(*add_ineq)(void *user, isl_int *), void *user)
Definition isl_tab.c:2247
isl_stat isl_tab_mark_empty(struct isl_tab *tab)
Definition isl_tab.c:1006
static isl_bool detect_constant_with_tmp(struct isl_tab *tab, struct isl_tab_var *var, isl_int *target, isl_int *tmp)
Definition isl_tab.c:3628
void isl_tab_dump(__isl_keep struct isl_tab *tab)
Definition isl_tab.c:4348
static isl_stat isl_tab_check_con(struct isl_tab *tab, int con)
Definition isl_tab.c:2761
static __isl_give isl_basic_map * gauss_if_shared(__isl_take isl_basic_map *bmap, struct isl_tab *tab)
Definition isl_tab.c:3190
static int row_sgn(struct isl_tab *tab, int row)
Definition isl_tab.c:1319
static void swap_rows(struct isl_tab *tab, int row1, int row2)
Definition isl_tab.c:770
isl_stat isl_tab_add_ineq(struct isl_tab *tab, isl_int *ineq)
Definition isl_tab.c:1906
static __isl_give isl_mat * tab_mat_product(__isl_keep isl_mat *mat1, __isl_keep isl_mat *mat2, unsigned row1, unsigned row2, unsigned col1, unsigned col2, unsigned off, unsigned r1, unsigned r2, unsigned d1, unsigned d2)
Definition isl_tab.c:323
static int row_is_big(struct isl_tab *tab, int row)
Definition isl_tab.c:2032
__isl_give isl_vec * isl_tab_get_sample_value(struct isl_tab *tab)
Definition isl_tab.c:2575
isl_ctx * isl_tab_get_ctx(struct isl_tab *tab)
Definition isl_tab.c:100
static int row_at_most_neg_one(struct isl_tab *tab, int row)
Definition isl_tab.c:1442
int isl_tab_add_valid_eq(struct isl_tab *tab, isl_int *eq)
Definition isl_tab.c:2054
static void free_undo_record(struct isl_tab_undo *undo)
Definition isl_tab.c:182
static isl_stat cut_to_hyperplane(struct isl_tab *tab, struct isl_tab_var *var)
Definition isl_tab.c:2698
static void free_undo(struct isl_tab *tab)
Definition isl_tab.c:193
int isl_tab_add_div(struct isl_tab *tab, __isl_keep isl_vec *div)
Definition isl_tab.c:2382
isl_stat isl_tab_detect_constants(struct isl_tab *tab)
Definition isl_tab.c:3734
int isl_tab_sample_is_integer(struct isl_tab *tab)
Definition isl_tab.c:2533
isl_bool isl_tab_cone_is_bounded(struct isl_tab *tab)
Definition isl_tab.c:2499
__isl_give isl_basic_map * isl_tab_make_equalities_explicit(struct isl_tab *tab, __isl_take isl_basic_map *bmap)
Definition isl_tab.c:3242
isl_bool isl_tab_is_constant(struct isl_tab *tab, int var, isl_int *value)
Definition isl_tab.c:3715
static int row_is_neg(struct isl_tab *tab, int row)
Definition isl_tab.c:1308
static isl_stat drop_eq(unsigned n, void *user)
Definition isl_tab.c:3176
isl_bool isl_tab_need_undo(struct isl_tab *tab)
Definition isl_tab.c:3765
struct isl_tab * isl_tab_dup(struct isl_tab *tab)
Definition isl_tab.c:223
struct isl_tab * isl_tab_alloc(struct isl_ctx *ctx, unsigned n_row, unsigned n_var, unsigned M)
Definition isl_tab.c:33
enum isl_ineq_type isl_tab_ineq_type(struct isl_tab *tab, isl_int *ineq)
Definition isl_tab.c:4173
static isl_bool var_reaches(struct isl_tab *tab, struct isl_tab_var *var, int sgn, isl_int target, isl_int *tmp)
Definition isl_tab.c:3585
static int max_is_manifestly_unbounded(struct isl_tab *tab, struct isl_tab_var *var)
Definition isl_tab.c:585
static int pivot_row(struct isl_tab *tab, struct isl_tab_var *var, int sgn, int c)
Definition isl_tab.c:660
isl_stat isl_tab_push(struct isl_tab *tab, enum isl_tab_undo_type type)
Definition isl_tab.c:835
int isl_tab_shift_var(struct isl_tab *tab, int pos, isl_int shift)
Definition isl_tab.c:2852
static int to_row(struct isl_tab *tab, struct isl_tab_var *var, int sign) WARN_UNUSED
Definition isl_tab.c:1215
int isl_tab_row_is_redundant(struct isl_tab *tab, int row)
Definition isl_tab.c:742
static int add_zero_row(struct isl_tab *tab)
Definition isl_tab.c:2095
enum isl_lp_result isl_tab_min(struct isl_tab *tab, isl_int *f, isl_int denom, isl_int *opt, isl_int *opt_denom, unsigned flags)
Definition isl_tab.c:3430
static isl_stat rotate_constraints_right(struct isl_tab *tab, int first, int n)
Definition isl_tab.c:3085
static void find_pivot(struct isl_tab *tab, struct isl_tab_var *var, struct isl_tab_var *skip_var, int sgn, int *row, int *col)
Definition isl_tab.c:705
static void get_rounded_sample_value(struct isl_tab *tab, struct isl_tab_var *var, int sgn, isl_int *v)
Definition isl_tab.c:2613
static int restore_row(struct isl_tab *tab, struct isl_tab_var *var)
Definition isl_tab.c:1334
static void update_index2(struct isl_tab_var *var, unsigned row1, unsigned col1, unsigned r1, unsigned r2, unsigned d1, unsigned d2)
Definition isl_tab.c:397
int isl_tab_detect_implicit_equalities(struct isl_tab *tab)
Definition isl_tab.c:2979
static void update_index1(struct isl_tab_var *var, unsigned r1, unsigned r2, unsigned d1, unsigned d2)
Definition isl_tab.c:383
static int restore_basis(struct isl_tab *tab, int *col_var)
Definition isl_tab.c:3979
int isl_tab_insert_div(struct isl_tab *tab, int pos, __isl_keep isl_vec *div, isl_stat(*add_ineq)(void *user, isl_int *), void *user)
Definition isl_tab.c:2331
static void check_table(struct isl_tab *tab) __attribute__((unused))
Definition isl_tab.c:1241
__isl_give struct isl_tab * isl_tab_from_basic_map(__isl_keep isl_basic_map *bmap, int track)
Definition isl_tab.c:2394
__isl_give struct isl_tab * isl_tab_from_basic_set(__isl_keep isl_basic_set *bset, int track)
Definition isl_tab.c:2434
isl_stat isl_tab_push_var(struct isl_tab *tab, enum isl_tab_undo_type type, struct isl_tab_var *var)
Definition isl_tab.c:824
static isl_stat drop_bmap_div(struct isl_tab *tab, int pos)
Definition isl_tab.c:3947
isl_stat isl_tab_save_samples(struct isl_tab *tab)
Definition isl_tab.c:943
static isl_stat drop_row(struct isl_tab *tab, int row)
Definition isl_tab.c:1870
isl_stat isl_tab_rollback(struct isl_tab *tab, struct isl_tab_undo *snap)
Definition isl_tab.c:4096
int isl_tab_freeze_constraint(struct isl_tab *tab, int con)
Definition isl_tab.c:1017
static int row_is_manifestly_zero(struct isl_tab *tab, int row)
Definition isl_tab.c:2037
int isl_tab_allocate_con(struct isl_tab *tab)
Definition isl_tab.c:1687
static int update_con_after_move(struct isl_tab *tab, int i, int old)
Definition isl_tab.c:3041
static int div_is_nonneg(struct isl_tab *tab, __isl_keep isl_vec *div)
Definition isl_tab.c:2298
static void isl_tab_print_internal(__isl_keep struct isl_tab *tab, FILE *out, int indent)
Definition isl_tab.c:4277
int isl_tab_select_facet(struct isl_tab *tab, int con)
Definition isl_tab.c:2916
int isl_tab_extend_cons(struct isl_tab *tab, unsigned n_new)
Definition isl_tab.c:105
__isl_give isl_basic_map * isl_basic_map_update_from_tab(__isl_take isl_basic_map *bmap, struct isl_tab *tab)
Definition isl_tab.c:2634
static int row_cmp(struct isl_tab *tab, int r1, int r2, int c, isl_int *t)
Definition isl_tab.c:623
isl_ineq_type
Definition isl_tab.h:230
@ isl_ineq_cut
Definition isl_tab.h:234
@ isl_ineq_adj_eq
Definition isl_tab.h:235
@ isl_ineq_adj_ineq
Definition isl_tab.h:236
@ isl_ineq_redundant
Definition isl_tab.h:232
@ isl_ineq_error
Definition isl_tab.h:231
@ isl_ineq_separate
Definition isl_tab.h:233
#define ISL_TAB_SAVE_DUAL
Definition isl_tab.h:207
isl_tab_row_sign
Definition isl_tab.h:130
@ isl_tab_row_unknown
Definition isl_tab.h:131
@ isl_tab_row_pos
Definition isl_tab.h:132
@ isl_tab_row_neg
Definition isl_tab.h:133
isl_tab_undo_type
Definition isl_tab.h:31
@ isl_tab_undo_bmap_ineq
Definition isl_tab.h:42
@ isl_tab_undo_bottom
Definition isl_tab.h:32
@ isl_tab_undo_bmap_eq
Definition isl_tab.h:43
@ isl_tab_undo_callback
Definition isl_tab.h:48
@ isl_tab_undo_redundant
Definition isl_tab.h:36
@ isl_tab_undo_saved_samples
Definition isl_tab.h:47
@ isl_tab_undo_drop_sample
Definition isl_tab.h:46
@ isl_tab_undo_relax
Definition isl_tab.h:40
@ isl_tab_undo_ineq_to_eq
Definition isl_tab.h:49
@ isl_tab_undo_rational
Definition isl_tab.h:33
@ isl_tab_undo_allocate
Definition isl_tab.h:39
@ isl_tab_undo_freeze
Definition isl_tab.h:37
@ isl_tab_undo_nonneg
Definition isl_tab.h:35
@ isl_tab_undo_saved_basis
Definition isl_tab.h:45
@ isl_tab_undo_unrestrict
Definition isl_tab.h:41
@ isl_tab_undo_zero
Definition isl_tab.h:38
@ isl_tab_undo_bmap_div
Definition isl_tab.h:44
@ isl_tab_undo_empty
Definition isl_tab.h:34
static enum isl_tab_row_sign row_sign(struct isl_tab *tab, struct isl_sol *sol, int row)
enum isl_fold type
Definition isl_test.c:3867
const char * p
Definition isl_test.c:8454
const char * offset
Definition isl_test.c:1577
const char * res
Definition isl_test.c:783
const char * f
Definition isl_test.c:8453
static __isl_give isl_union_map * total(__isl_take isl_union_map *umap, __isl_give isl_map *(*fn)(__isl_take isl_map *))
t0 *a *b *t *a *b * t
isl_lp_result
Definition lp.h:17
@ isl_lp_error
Definition lp.h:18
@ isl_lp_ok
Definition lp.h:19
@ isl_lp_empty
Definition lp.h:21
@ isl_lp_unbounded
Definition lp.h:20
__isl_null isl_basic_map * isl_basic_map_free(__isl_take isl_basic_map *bmap)
Definition isl_map.c:1503
isl_size isl_basic_map_dim(__isl_keep isl_basic_map *bmap, enum isl_dim_type type)
Definition isl_map.c:83
__isl_give isl_basic_map * isl_basic_map_copy(__isl_keep isl_basic_map *bmap)
Definition isl_map.c:1479
void isl_basic_map_print_internal(__isl_keep isl_basic_map *bmap, FILE *out, int indent)
Definition isl_map.c:3933
void isl_mat_print_internal(__isl_keep isl_mat *mat, FILE *out, int indent)
Definition isl_mat.c:1472
__isl_give isl_mat * isl_mat_swap_cols(__isl_take isl_mat *mat, unsigned i, unsigned j)
Definition isl_mat.c:1233
__isl_give isl_mat * isl_mat_swap_rows(__isl_take isl_mat *mat, unsigned i, unsigned j)
Definition isl_mat.c:1248
__isl_null isl_mat * isl_mat_free(__isl_take isl_mat *mat)
Definition isl_mat.c:240
__isl_give isl_mat * isl_mat_alloc(isl_ctx *ctx, unsigned n_row, unsigned n_col)
Definition isl_mat.c:53
isl_ctx * isl_mat_get_ctx(__isl_keep isl_mat *mat)
Definition isl_mat.c:25
__isl_give isl_mat * isl_mat_extend(__isl_take isl_mat *mat, unsigned n_row, unsigned n_col)
Definition isl_mat.c:91
__isl_give isl_mat * isl_mat_drop_cols(__isl_take isl_mat *mat, unsigned col, unsigned n)
Definition isl_mat.c:1506
a(0)
b(9)
isl_size isl_basic_set_dim(__isl_keep isl_basic_set *bset, enum isl_dim_type type)
Definition isl_map.c:202
@ isl_dim_param
Definition space_type.h:15
@ isl_dim_all
Definition space_type.h:20
@ isl_dim_div
Definition space_type.h:19
struct isl_ctx * ctx
isl_int ** ineq
isl_int * data
Definition isl_blk.h:21
unsigned n_row
unsigned n_col
struct isl_ctx * ctx
isl_int ** row
isl_stat(* run)(struct isl_tab_callback *cb)
Definition isl_tab.h:53
enum isl_tab_undo_type type
Definition isl_tab.h:64
struct isl_tab_undo * next
Definition isl_tab.h:66
union isl_tab_undo_val u
Definition isl_tab.h:65
unsigned negated
Definition isl_tab.h:28
int index
Definition isl_tab.h:21
unsigned frozen
Definition isl_tab.h:27
unsigned is_nonneg
Definition isl_tab.h:23
unsigned is_zero
Definition isl_tab.h:24
unsigned is_redundant
Definition isl_tab.h:25
unsigned is_row
Definition isl_tab.h:22
struct isl_mat * mat
Definition isl_tab.h:137
unsigned n_con
Definition isl_tab.h:148
unsigned n_col
Definition isl_tab.h:140
struct isl_tab_undo bottom
Definition isl_tab.h:157
unsigned rational
Definition isl_tab.h:178
unsigned need_undo
Definition isl_tab.h:176
unsigned in_undo
Definition isl_tab.h:180
unsigned n_sample
Definition isl_tab.h:163
unsigned n_var
Definition isl_tab.h:144
int n_unbounded
Definition isl_tab.h:169
unsigned cone
Definition isl_tab.h:182
int * col_var
Definition isl_tab.h:154
unsigned empty
Definition isl_tab.h:179
unsigned n_eq
Definition isl_tab.h:149
struct isl_vec * dual
Definition isl_tab.h:160
struct isl_tab_var * con
Definition isl_tab.h:152
unsigned max_con
Definition isl_tab.h:150
unsigned n_dead
Definition isl_tab.h:141
struct isl_basic_map * bmap
Definition isl_tab.h:161
struct isl_mat * samples
Definition isl_tab.h:166
struct isl_tab_undo * top
Definition isl_tab.h:158
int * sample_index
Definition isl_tab.h:165
unsigned strict_redundant
Definition isl_tab.h:175
unsigned M
Definition isl_tab.h:181
enum isl_tab_row_sign * row_sign
Definition isl_tab.h:155
int * row_var
Definition isl_tab.h:153
unsigned n_param
Definition isl_tab.h:145
unsigned n_redundant
Definition isl_tab.h:142
struct isl_mat * basis
Definition isl_tab.h:170
unsigned n_outside
Definition isl_tab.h:164
unsigned n_row
Definition isl_tab.h:139
unsigned n_div
Definition isl_tab.h:146
int n_zero
Definition isl_tab.h:168
struct isl_tab_var * var
Definition isl_tab.h:151
unsigned max_var
Definition isl_tab.h:147
unsigned preserve
Definition isl_tab.h:177
isl_int * el
struct isl_blk block
struct isl_tab_callback * callback
Definition isl_tab.h:60
__isl_null isl_vec * isl_vec_free(__isl_take isl_vec *vec)
Definition isl_vec.c:234
__isl_give isl_vec * isl_vec_normalize(__isl_take isl_vec *vec)
Definition isl_vec.c:456
__isl_give isl_vec * isl_vec_alloc(isl_ctx *ctx, unsigned size)
Definition isl_vec.c:33
n
Definition youcefn.c:8