mark isl_set_dim_is_unique as deprecated
[isl.git] / isl_tab.c
blobceaaeff7e26932914aa6e7a28e903802de5bb14f
1 /*
2 * Copyright 2008-2009 Katholieke Universiteit Leuven
3 * Copyright 2013 Ecole Normale Superieure
4 * Copyright 2014 INRIA Rocquencourt
6 * Use of this software is governed by the MIT license
8 * Written by Sven Verdoolaege, K.U.Leuven, Departement
9 * Computerwetenschappen, Celestijnenlaan 200A, B-3001 Leuven, Belgium
10 * and Ecole Normale Superieure, 45 rue d'Ulm, 75230 Paris, France
11 * and Inria Paris - Rocquencourt, Domaine de Voluceau - Rocquencourt,
12 * B.P. 105 - 78153 Le Chesnay, France
15 #include <isl_ctx_private.h>
16 #include <isl_mat_private.h>
17 #include <isl_vec_private.h>
18 #include "isl_map_private.h"
19 #include "isl_tab.h"
20 #include <isl_seq.h>
21 #include <isl_config.h>
23 #include <bset_to_bmap.c>
24 #include <bset_from_bmap.c>
27 * The implementation of tableaus in this file was inspired by Section 8
28 * of David Detlefs, Greg Nelson and James B. Saxe, "Simplify: a theorem
29 * prover for program checking".
32 struct isl_tab *isl_tab_alloc(struct isl_ctx *ctx,
33 unsigned n_row, unsigned n_var, unsigned M)
35 int i;
36 struct isl_tab *tab;
37 unsigned off = 2 + M;
39 tab = isl_calloc_type(ctx, struct isl_tab);
40 if (!tab)
41 return NULL;
42 tab->mat = isl_mat_alloc(ctx, n_row, off + n_var);
43 if (!tab->mat)
44 goto error;
45 tab->var = isl_alloc_array(ctx, struct isl_tab_var, n_var);
46 if (n_var && !tab->var)
47 goto error;
48 tab->con = isl_alloc_array(ctx, struct isl_tab_var, n_row);
49 if (n_row && !tab->con)
50 goto error;
51 tab->col_var = isl_alloc_array(ctx, int, n_var);
52 if (n_var && !tab->col_var)
53 goto error;
54 tab->row_var = isl_alloc_array(ctx, int, n_row);
55 if (n_row && !tab->row_var)
56 goto error;
57 for (i = 0; i < n_var; ++i) {
58 tab->var[i].index = i;
59 tab->var[i].is_row = 0;
60 tab->var[i].is_nonneg = 0;
61 tab->var[i].is_zero = 0;
62 tab->var[i].is_redundant = 0;
63 tab->var[i].frozen = 0;
64 tab->var[i].negated = 0;
65 tab->col_var[i] = i;
67 tab->n_row = 0;
68 tab->n_con = 0;
69 tab->n_eq = 0;
70 tab->max_con = n_row;
71 tab->n_col = n_var;
72 tab->n_var = n_var;
73 tab->max_var = n_var;
74 tab->n_param = 0;
75 tab->n_div = 0;
76 tab->n_dead = 0;
77 tab->n_redundant = 0;
78 tab->strict_redundant = 0;
79 tab->need_undo = 0;
80 tab->rational = 0;
81 tab->empty = 0;
82 tab->in_undo = 0;
83 tab->M = M;
84 tab->cone = 0;
85 tab->bottom.type = isl_tab_undo_bottom;
86 tab->bottom.next = NULL;
87 tab->top = &tab->bottom;
89 tab->n_zero = 0;
90 tab->n_unbounded = 0;
91 tab->basis = NULL;
93 return tab;
94 error:
95 isl_tab_free(tab);
96 return NULL;
99 isl_ctx *isl_tab_get_ctx(struct isl_tab *tab)
101 return tab ? isl_mat_get_ctx(tab->mat) : NULL;
104 int isl_tab_extend_cons(struct isl_tab *tab, unsigned n_new)
106 unsigned off;
108 if (!tab)
109 return -1;
111 off = 2 + tab->M;
113 if (tab->max_con < tab->n_con + n_new) {
114 struct isl_tab_var *con;
116 con = isl_realloc_array(tab->mat->ctx, tab->con,
117 struct isl_tab_var, tab->max_con + n_new);
118 if (!con)
119 return -1;
120 tab->con = con;
121 tab->max_con += n_new;
123 if (tab->mat->n_row < tab->n_row + n_new) {
124 int *row_var;
126 tab->mat = isl_mat_extend(tab->mat,
127 tab->n_row + n_new, off + tab->n_col);
128 if (!tab->mat)
129 return -1;
130 row_var = isl_realloc_array(tab->mat->ctx, tab->row_var,
131 int, tab->mat->n_row);
132 if (!row_var)
133 return -1;
134 tab->row_var = row_var;
135 if (tab->row_sign) {
136 enum isl_tab_row_sign *s;
137 s = isl_realloc_array(tab->mat->ctx, tab->row_sign,
138 enum isl_tab_row_sign, tab->mat->n_row);
139 if (!s)
140 return -1;
141 tab->row_sign = s;
144 return 0;
147 /* Make room for at least n_new extra variables.
148 * Return -1 if anything went wrong.
150 int isl_tab_extend_vars(struct isl_tab *tab, unsigned n_new)
152 struct isl_tab_var *var;
153 unsigned off = 2 + tab->M;
155 if (tab->max_var < tab->n_var + n_new) {
156 var = isl_realloc_array(tab->mat->ctx, tab->var,
157 struct isl_tab_var, tab->n_var + n_new);
158 if (!var)
159 return -1;
160 tab->var = var;
161 tab->max_var = tab->n_var + n_new;
164 if (tab->mat->n_col < off + tab->n_col + n_new) {
165 int *p;
167 tab->mat = isl_mat_extend(tab->mat,
168 tab->mat->n_row, off + tab->n_col + n_new);
169 if (!tab->mat)
170 return -1;
171 p = isl_realloc_array(tab->mat->ctx, tab->col_var,
172 int, tab->n_col + n_new);
173 if (!p)
174 return -1;
175 tab->col_var = p;
178 return 0;
181 static void free_undo_record(struct isl_tab_undo *undo)
183 switch (undo->type) {
184 case isl_tab_undo_saved_basis:
185 free(undo->u.col_var);
186 break;
187 default:;
189 free(undo);
192 static void free_undo(struct isl_tab *tab)
194 struct isl_tab_undo *undo, *next;
196 for (undo = tab->top; undo && undo != &tab->bottom; undo = next) {
197 next = undo->next;
198 free_undo_record(undo);
200 tab->top = undo;
203 void isl_tab_free(struct isl_tab *tab)
205 if (!tab)
206 return;
207 free_undo(tab);
208 isl_mat_free(tab->mat);
209 isl_vec_free(tab->dual);
210 isl_basic_map_free(tab->bmap);
211 free(tab->var);
212 free(tab->con);
213 free(tab->row_var);
214 free(tab->col_var);
215 free(tab->row_sign);
216 isl_mat_free(tab->samples);
217 free(tab->sample_index);
218 isl_mat_free(tab->basis);
219 free(tab);
222 struct isl_tab *isl_tab_dup(struct isl_tab *tab)
224 int i;
225 struct isl_tab *dup;
226 unsigned off;
228 if (!tab)
229 return NULL;
231 off = 2 + tab->M;
232 dup = isl_calloc_type(tab->mat->ctx, struct isl_tab);
233 if (!dup)
234 return NULL;
235 dup->mat = isl_mat_dup(tab->mat);
236 if (!dup->mat)
237 goto error;
238 dup->var = isl_alloc_array(tab->mat->ctx, struct isl_tab_var, tab->max_var);
239 if (tab->max_var && !dup->var)
240 goto error;
241 for (i = 0; i < tab->n_var; ++i)
242 dup->var[i] = tab->var[i];
243 dup->con = isl_alloc_array(tab->mat->ctx, struct isl_tab_var, tab->max_con);
244 if (tab->max_con && !dup->con)
245 goto error;
246 for (i = 0; i < tab->n_con; ++i)
247 dup->con[i] = tab->con[i];
248 dup->col_var = isl_alloc_array(tab->mat->ctx, int, tab->mat->n_col - off);
249 if ((tab->mat->n_col - off) && !dup->col_var)
250 goto error;
251 for (i = 0; i < tab->n_col; ++i)
252 dup->col_var[i] = tab->col_var[i];
253 dup->row_var = isl_alloc_array(tab->mat->ctx, int, tab->mat->n_row);
254 if (tab->mat->n_row && !dup->row_var)
255 goto error;
256 for (i = 0; i < tab->n_row; ++i)
257 dup->row_var[i] = tab->row_var[i];
258 if (tab->row_sign) {
259 dup->row_sign = isl_alloc_array(tab->mat->ctx, enum isl_tab_row_sign,
260 tab->mat->n_row);
261 if (tab->mat->n_row && !dup->row_sign)
262 goto error;
263 for (i = 0; i < tab->n_row; ++i)
264 dup->row_sign[i] = tab->row_sign[i];
266 if (tab->samples) {
267 dup->samples = isl_mat_dup(tab->samples);
268 if (!dup->samples)
269 goto error;
270 dup->sample_index = isl_alloc_array(tab->mat->ctx, int,
271 tab->samples->n_row);
272 if (tab->samples->n_row && !dup->sample_index)
273 goto error;
274 dup->n_sample = tab->n_sample;
275 dup->n_outside = tab->n_outside;
277 dup->n_row = tab->n_row;
278 dup->n_con = tab->n_con;
279 dup->n_eq = tab->n_eq;
280 dup->max_con = tab->max_con;
281 dup->n_col = tab->n_col;
282 dup->n_var = tab->n_var;
283 dup->max_var = tab->max_var;
284 dup->n_param = tab->n_param;
285 dup->n_div = tab->n_div;
286 dup->n_dead = tab->n_dead;
287 dup->n_redundant = tab->n_redundant;
288 dup->rational = tab->rational;
289 dup->empty = tab->empty;
290 dup->strict_redundant = 0;
291 dup->need_undo = 0;
292 dup->in_undo = 0;
293 dup->M = tab->M;
294 tab->cone = tab->cone;
295 dup->bottom.type = isl_tab_undo_bottom;
296 dup->bottom.next = NULL;
297 dup->top = &dup->bottom;
299 dup->n_zero = tab->n_zero;
300 dup->n_unbounded = tab->n_unbounded;
301 dup->basis = isl_mat_dup(tab->basis);
303 return dup;
304 error:
305 isl_tab_free(dup);
306 return NULL;
309 /* Construct the coefficient matrix of the product tableau
310 * of two tableaus.
311 * mat{1,2} is the coefficient matrix of tableau {1,2}
312 * row{1,2} is the number of rows in tableau {1,2}
313 * col{1,2} is the number of columns in tableau {1,2}
314 * off is the offset to the coefficient column (skipping the
315 * denominator, the constant term and the big parameter if any)
316 * r{1,2} is the number of redundant rows in tableau {1,2}
317 * d{1,2} is the number of dead columns in tableau {1,2}
319 * The order of the rows and columns in the result is as explained
320 * in isl_tab_product.
322 static struct isl_mat *tab_mat_product(struct isl_mat *mat1,
323 struct isl_mat *mat2, unsigned row1, unsigned row2,
324 unsigned col1, unsigned col2,
325 unsigned off, unsigned r1, unsigned r2, unsigned d1, unsigned d2)
327 int i;
328 struct isl_mat *prod;
329 unsigned n;
331 prod = isl_mat_alloc(mat1->ctx, mat1->n_row + mat2->n_row,
332 off + col1 + col2);
333 if (!prod)
334 return NULL;
336 n = 0;
337 for (i = 0; i < r1; ++i) {
338 isl_seq_cpy(prod->row[n + i], mat1->row[i], off + d1);
339 isl_seq_clr(prod->row[n + i] + off + d1, d2);
340 isl_seq_cpy(prod->row[n + i] + off + d1 + d2,
341 mat1->row[i] + off + d1, col1 - d1);
342 isl_seq_clr(prod->row[n + i] + off + col1 + d1, col2 - d2);
345 n += r1;
346 for (i = 0; i < r2; ++i) {
347 isl_seq_cpy(prod->row[n + i], mat2->row[i], off);
348 isl_seq_clr(prod->row[n + i] + off, d1);
349 isl_seq_cpy(prod->row[n + i] + off + d1,
350 mat2->row[i] + off, d2);
351 isl_seq_clr(prod->row[n + i] + off + d1 + d2, col1 - d1);
352 isl_seq_cpy(prod->row[n + i] + off + col1 + d1,
353 mat2->row[i] + off + d2, col2 - d2);
356 n += r2;
357 for (i = 0; i < row1 - r1; ++i) {
358 isl_seq_cpy(prod->row[n + i], mat1->row[r1 + i], off + d1);
359 isl_seq_clr(prod->row[n + i] + off + d1, d2);
360 isl_seq_cpy(prod->row[n + i] + off + d1 + d2,
361 mat1->row[r1 + i] + off + d1, col1 - d1);
362 isl_seq_clr(prod->row[n + i] + off + col1 + d1, col2 - d2);
365 n += row1 - r1;
366 for (i = 0; i < row2 - r2; ++i) {
367 isl_seq_cpy(prod->row[n + i], mat2->row[r2 + i], off);
368 isl_seq_clr(prod->row[n + i] + off, d1);
369 isl_seq_cpy(prod->row[n + i] + off + d1,
370 mat2->row[r2 + i] + off, d2);
371 isl_seq_clr(prod->row[n + i] + off + d1 + d2, col1 - d1);
372 isl_seq_cpy(prod->row[n + i] + off + col1 + d1,
373 mat2->row[r2 + i] + off + d2, col2 - d2);
376 return prod;
379 /* Update the row or column index of a variable that corresponds
380 * to a variable in the first input tableau.
382 static void update_index1(struct isl_tab_var *var,
383 unsigned r1, unsigned r2, unsigned d1, unsigned d2)
385 if (var->index == -1)
386 return;
387 if (var->is_row && var->index >= r1)
388 var->index += r2;
389 if (!var->is_row && var->index >= d1)
390 var->index += d2;
393 /* Update the row or column index of a variable that corresponds
394 * to a variable in the second input tableau.
396 static void update_index2(struct isl_tab_var *var,
397 unsigned row1, unsigned col1,
398 unsigned r1, unsigned r2, unsigned d1, unsigned d2)
400 if (var->index == -1)
401 return;
402 if (var->is_row) {
403 if (var->index < r2)
404 var->index += r1;
405 else
406 var->index += row1;
407 } else {
408 if (var->index < d2)
409 var->index += d1;
410 else
411 var->index += col1;
415 /* Create a tableau that represents the Cartesian product of the sets
416 * represented by tableaus tab1 and tab2.
417 * The order of the rows in the product is
418 * - redundant rows of tab1
419 * - redundant rows of tab2
420 * - non-redundant rows of tab1
421 * - non-redundant rows of tab2
422 * The order of the columns is
423 * - denominator
424 * - constant term
425 * - coefficient of big parameter, if any
426 * - dead columns of tab1
427 * - dead columns of tab2
428 * - live columns of tab1
429 * - live columns of tab2
430 * The order of the variables and the constraints is a concatenation
431 * of order in the two input tableaus.
433 struct isl_tab *isl_tab_product(struct isl_tab *tab1, struct isl_tab *tab2)
435 int i;
436 struct isl_tab *prod;
437 unsigned off;
438 unsigned r1, r2, d1, d2;
440 if (!tab1 || !tab2)
441 return NULL;
443 isl_assert(tab1->mat->ctx, tab1->M == tab2->M, return NULL);
444 isl_assert(tab1->mat->ctx, tab1->rational == tab2->rational, return NULL);
445 isl_assert(tab1->mat->ctx, tab1->cone == tab2->cone, return NULL);
446 isl_assert(tab1->mat->ctx, !tab1->row_sign, return NULL);
447 isl_assert(tab1->mat->ctx, !tab2->row_sign, return NULL);
448 isl_assert(tab1->mat->ctx, tab1->n_param == 0, return NULL);
449 isl_assert(tab1->mat->ctx, tab2->n_param == 0, return NULL);
450 isl_assert(tab1->mat->ctx, tab1->n_div == 0, return NULL);
451 isl_assert(tab1->mat->ctx, tab2->n_div == 0, return NULL);
453 off = 2 + tab1->M;
454 r1 = tab1->n_redundant;
455 r2 = tab2->n_redundant;
456 d1 = tab1->n_dead;
457 d2 = tab2->n_dead;
458 prod = isl_calloc_type(tab1->mat->ctx, struct isl_tab);
459 if (!prod)
460 return NULL;
461 prod->mat = tab_mat_product(tab1->mat, tab2->mat,
462 tab1->n_row, tab2->n_row,
463 tab1->n_col, tab2->n_col, off, r1, r2, d1, d2);
464 if (!prod->mat)
465 goto error;
466 prod->var = isl_alloc_array(tab1->mat->ctx, struct isl_tab_var,
467 tab1->max_var + tab2->max_var);
468 if ((tab1->max_var + tab2->max_var) && !prod->var)
469 goto error;
470 for (i = 0; i < tab1->n_var; ++i) {
471 prod->var[i] = tab1->var[i];
472 update_index1(&prod->var[i], r1, r2, d1, d2);
474 for (i = 0; i < tab2->n_var; ++i) {
475 prod->var[tab1->n_var + i] = tab2->var[i];
476 update_index2(&prod->var[tab1->n_var + i],
477 tab1->n_row, tab1->n_col,
478 r1, r2, d1, d2);
480 prod->con = isl_alloc_array(tab1->mat->ctx, struct isl_tab_var,
481 tab1->max_con + tab2->max_con);
482 if ((tab1->max_con + tab2->max_con) && !prod->con)
483 goto error;
484 for (i = 0; i < tab1->n_con; ++i) {
485 prod->con[i] = tab1->con[i];
486 update_index1(&prod->con[i], r1, r2, d1, d2);
488 for (i = 0; i < tab2->n_con; ++i) {
489 prod->con[tab1->n_con + i] = tab2->con[i];
490 update_index2(&prod->con[tab1->n_con + i],
491 tab1->n_row, tab1->n_col,
492 r1, r2, d1, d2);
494 prod->col_var = isl_alloc_array(tab1->mat->ctx, int,
495 tab1->n_col + tab2->n_col);
496 if ((tab1->n_col + tab2->n_col) && !prod->col_var)
497 goto error;
498 for (i = 0; i < tab1->n_col; ++i) {
499 int pos = i < d1 ? i : i + d2;
500 prod->col_var[pos] = tab1->col_var[i];
502 for (i = 0; i < tab2->n_col; ++i) {
503 int pos = i < d2 ? d1 + i : tab1->n_col + i;
504 int t = tab2->col_var[i];
505 if (t >= 0)
506 t += tab1->n_var;
507 else
508 t -= tab1->n_con;
509 prod->col_var[pos] = t;
511 prod->row_var = isl_alloc_array(tab1->mat->ctx, int,
512 tab1->mat->n_row + tab2->mat->n_row);
513 if ((tab1->mat->n_row + tab2->mat->n_row) && !prod->row_var)
514 goto error;
515 for (i = 0; i < tab1->n_row; ++i) {
516 int pos = i < r1 ? i : i + r2;
517 prod->row_var[pos] = tab1->row_var[i];
519 for (i = 0; i < tab2->n_row; ++i) {
520 int pos = i < r2 ? r1 + i : tab1->n_row + i;
521 int t = tab2->row_var[i];
522 if (t >= 0)
523 t += tab1->n_var;
524 else
525 t -= tab1->n_con;
526 prod->row_var[pos] = t;
528 prod->samples = NULL;
529 prod->sample_index = NULL;
530 prod->n_row = tab1->n_row + tab2->n_row;
531 prod->n_con = tab1->n_con + tab2->n_con;
532 prod->n_eq = 0;
533 prod->max_con = tab1->max_con + tab2->max_con;
534 prod->n_col = tab1->n_col + tab2->n_col;
535 prod->n_var = tab1->n_var + tab2->n_var;
536 prod->max_var = tab1->max_var + tab2->max_var;
537 prod->n_param = 0;
538 prod->n_div = 0;
539 prod->n_dead = tab1->n_dead + tab2->n_dead;
540 prod->n_redundant = tab1->n_redundant + tab2->n_redundant;
541 prod->rational = tab1->rational;
542 prod->empty = tab1->empty || tab2->empty;
543 prod->strict_redundant = tab1->strict_redundant || tab2->strict_redundant;
544 prod->need_undo = 0;
545 prod->in_undo = 0;
546 prod->M = tab1->M;
547 prod->cone = tab1->cone;
548 prod->bottom.type = isl_tab_undo_bottom;
549 prod->bottom.next = NULL;
550 prod->top = &prod->bottom;
552 prod->n_zero = 0;
553 prod->n_unbounded = 0;
554 prod->basis = NULL;
556 return prod;
557 error:
558 isl_tab_free(prod);
559 return NULL;
562 static struct isl_tab_var *var_from_index(struct isl_tab *tab, int i)
564 if (i >= 0)
565 return &tab->var[i];
566 else
567 return &tab->con[~i];
570 struct isl_tab_var *isl_tab_var_from_row(struct isl_tab *tab, int i)
572 return var_from_index(tab, tab->row_var[i]);
575 static struct isl_tab_var *var_from_col(struct isl_tab *tab, int i)
577 return var_from_index(tab, tab->col_var[i]);
580 /* Check if there are any upper bounds on column variable "var",
581 * i.e., non-negative rows where var appears with a negative coefficient.
582 * Return 1 if there are no such bounds.
584 static int max_is_manifestly_unbounded(struct isl_tab *tab,
585 struct isl_tab_var *var)
587 int i;
588 unsigned off = 2 + tab->M;
590 if (var->is_row)
591 return 0;
592 for (i = tab->n_redundant; i < tab->n_row; ++i) {
593 if (!isl_int_is_neg(tab->mat->row[i][off + var->index]))
594 continue;
595 if (isl_tab_var_from_row(tab, i)->is_nonneg)
596 return 0;
598 return 1;
601 /* Check if there are any lower bounds on column variable "var",
602 * i.e., non-negative rows where var appears with a positive coefficient.
603 * Return 1 if there are no such bounds.
605 static int min_is_manifestly_unbounded(struct isl_tab *tab,
606 struct isl_tab_var *var)
608 int i;
609 unsigned off = 2 + tab->M;
611 if (var->is_row)
612 return 0;
613 for (i = tab->n_redundant; i < tab->n_row; ++i) {
614 if (!isl_int_is_pos(tab->mat->row[i][off + var->index]))
615 continue;
616 if (isl_tab_var_from_row(tab, i)->is_nonneg)
617 return 0;
619 return 1;
622 static int row_cmp(struct isl_tab *tab, int r1, int r2, int c, isl_int *t)
624 unsigned off = 2 + tab->M;
626 if (tab->M) {
627 int s;
628 isl_int_mul(*t, tab->mat->row[r1][2], tab->mat->row[r2][off+c]);
629 isl_int_submul(*t, tab->mat->row[r2][2], tab->mat->row[r1][off+c]);
630 s = isl_int_sgn(*t);
631 if (s)
632 return s;
634 isl_int_mul(*t, tab->mat->row[r1][1], tab->mat->row[r2][off + c]);
635 isl_int_submul(*t, tab->mat->row[r2][1], tab->mat->row[r1][off + c]);
636 return isl_int_sgn(*t);
639 /* Given the index of a column "c", return the index of a row
640 * that can be used to pivot the column in, with either an increase
641 * (sgn > 0) or a decrease (sgn < 0) of the corresponding variable.
642 * If "var" is not NULL, then the row returned will be different from
643 * the one associated with "var".
645 * Each row in the tableau is of the form
647 * x_r = a_r0 + \sum_i a_ri x_i
649 * Only rows with x_r >= 0 and with the sign of a_ri opposite to "sgn"
650 * impose any limit on the increase or decrease in the value of x_c
651 * and this bound is equal to a_r0 / |a_rc|. We are therefore looking
652 * for the row with the smallest (most stringent) such bound.
653 * Note that the common denominator of each row drops out of the fraction.
654 * To check if row j has a smaller bound than row r, i.e.,
655 * a_j0 / |a_jc| < a_r0 / |a_rc| or a_j0 |a_rc| < a_r0 |a_jc|,
656 * we check if -sign(a_jc) (a_j0 a_rc - a_r0 a_jc) < 0,
657 * where -sign(a_jc) is equal to "sgn".
659 static int pivot_row(struct isl_tab *tab,
660 struct isl_tab_var *var, int sgn, int c)
662 int j, r, tsgn;
663 isl_int t;
664 unsigned off = 2 + tab->M;
666 isl_int_init(t);
667 r = -1;
668 for (j = tab->n_redundant; j < tab->n_row; ++j) {
669 if (var && j == var->index)
670 continue;
671 if (!isl_tab_var_from_row(tab, j)->is_nonneg)
672 continue;
673 if (sgn * isl_int_sgn(tab->mat->row[j][off + c]) >= 0)
674 continue;
675 if (r < 0) {
676 r = j;
677 continue;
679 tsgn = sgn * row_cmp(tab, r, j, c, &t);
680 if (tsgn < 0 || (tsgn == 0 &&
681 tab->row_var[j] < tab->row_var[r]))
682 r = j;
684 isl_int_clear(t);
685 return r;
688 /* Find a pivot (row and col) that will increase (sgn > 0) or decrease
689 * (sgn < 0) the value of row variable var.
690 * If not NULL, then skip_var is a row variable that should be ignored
691 * while looking for a pivot row. It is usually equal to var.
693 * As the given row in the tableau is of the form
695 * x_r = a_r0 + \sum_i a_ri x_i
697 * we need to find a column such that the sign of a_ri is equal to "sgn"
698 * (such that an increase in x_i will have the desired effect) or a
699 * column with a variable that may attain negative values.
700 * If a_ri is positive, then we need to move x_i in the same direction
701 * to obtain the desired effect. Otherwise, x_i has to move in the
702 * opposite direction.
704 static void find_pivot(struct isl_tab *tab,
705 struct isl_tab_var *var, struct isl_tab_var *skip_var,
706 int sgn, int *row, int *col)
708 int j, r, c;
709 isl_int *tr;
711 *row = *col = -1;
713 isl_assert(tab->mat->ctx, var->is_row, return);
714 tr = tab->mat->row[var->index] + 2 + tab->M;
716 c = -1;
717 for (j = tab->n_dead; j < tab->n_col; ++j) {
718 if (isl_int_is_zero(tr[j]))
719 continue;
720 if (isl_int_sgn(tr[j]) != sgn &&
721 var_from_col(tab, j)->is_nonneg)
722 continue;
723 if (c < 0 || tab->col_var[j] < tab->col_var[c])
724 c = j;
726 if (c < 0)
727 return;
729 sgn *= isl_int_sgn(tr[c]);
730 r = pivot_row(tab, skip_var, sgn, c);
731 *row = r < 0 ? var->index : r;
732 *col = c;
735 /* Return 1 if row "row" represents an obviously redundant inequality.
736 * This means
737 * - it represents an inequality or a variable
738 * - that is the sum of a non-negative sample value and a positive
739 * combination of zero or more non-negative constraints.
741 int isl_tab_row_is_redundant(struct isl_tab *tab, int row)
743 int i;
744 unsigned off = 2 + tab->M;
746 if (tab->row_var[row] < 0 && !isl_tab_var_from_row(tab, row)->is_nonneg)
747 return 0;
749 if (isl_int_is_neg(tab->mat->row[row][1]))
750 return 0;
751 if (tab->strict_redundant && isl_int_is_zero(tab->mat->row[row][1]))
752 return 0;
753 if (tab->M && isl_int_is_neg(tab->mat->row[row][2]))
754 return 0;
756 for (i = tab->n_dead; i < tab->n_col; ++i) {
757 if (isl_int_is_zero(tab->mat->row[row][off + i]))
758 continue;
759 if (tab->col_var[i] >= 0)
760 return 0;
761 if (isl_int_is_neg(tab->mat->row[row][off + i]))
762 return 0;
763 if (!var_from_col(tab, i)->is_nonneg)
764 return 0;
766 return 1;
769 static void swap_rows(struct isl_tab *tab, int row1, int row2)
771 int t;
772 enum isl_tab_row_sign s;
774 t = tab->row_var[row1];
775 tab->row_var[row1] = tab->row_var[row2];
776 tab->row_var[row2] = t;
777 isl_tab_var_from_row(tab, row1)->index = row1;
778 isl_tab_var_from_row(tab, row2)->index = row2;
779 tab->mat = isl_mat_swap_rows(tab->mat, row1, row2);
781 if (!tab->row_sign)
782 return;
783 s = tab->row_sign[row1];
784 tab->row_sign[row1] = tab->row_sign[row2];
785 tab->row_sign[row2] = s;
788 static int push_union(struct isl_tab *tab,
789 enum isl_tab_undo_type type, union isl_tab_undo_val u) WARN_UNUSED;
790 static int push_union(struct isl_tab *tab,
791 enum isl_tab_undo_type type, union isl_tab_undo_val u)
793 struct isl_tab_undo *undo;
795 if (!tab)
796 return -1;
797 if (!tab->need_undo)
798 return 0;
800 undo = isl_alloc_type(tab->mat->ctx, struct isl_tab_undo);
801 if (!undo)
802 return -1;
803 undo->type = type;
804 undo->u = u;
805 undo->next = tab->top;
806 tab->top = undo;
808 return 0;
811 int isl_tab_push_var(struct isl_tab *tab,
812 enum isl_tab_undo_type type, struct isl_tab_var *var)
814 union isl_tab_undo_val u;
815 if (var->is_row)
816 u.var_index = tab->row_var[var->index];
817 else
818 u.var_index = tab->col_var[var->index];
819 return push_union(tab, type, u);
822 int isl_tab_push(struct isl_tab *tab, enum isl_tab_undo_type type)
824 union isl_tab_undo_val u = { 0 };
825 return push_union(tab, type, u);
828 /* Push a record on the undo stack describing the current basic
829 * variables, so that the this state can be restored during rollback.
831 int isl_tab_push_basis(struct isl_tab *tab)
833 int i;
834 union isl_tab_undo_val u;
836 u.col_var = isl_alloc_array(tab->mat->ctx, int, tab->n_col);
837 if (tab->n_col && !u.col_var)
838 return -1;
839 for (i = 0; i < tab->n_col; ++i)
840 u.col_var[i] = tab->col_var[i];
841 return push_union(tab, isl_tab_undo_saved_basis, u);
844 int isl_tab_push_callback(struct isl_tab *tab, struct isl_tab_callback *callback)
846 union isl_tab_undo_val u;
847 u.callback = callback;
848 return push_union(tab, isl_tab_undo_callback, u);
851 struct isl_tab *isl_tab_init_samples(struct isl_tab *tab)
853 if (!tab)
854 return NULL;
856 tab->n_sample = 0;
857 tab->n_outside = 0;
858 tab->samples = isl_mat_alloc(tab->mat->ctx, 1, 1 + tab->n_var);
859 if (!tab->samples)
860 goto error;
861 tab->sample_index = isl_alloc_array(tab->mat->ctx, int, 1);
862 if (!tab->sample_index)
863 goto error;
864 return tab;
865 error:
866 isl_tab_free(tab);
867 return NULL;
870 int isl_tab_add_sample(struct isl_tab *tab, __isl_take isl_vec *sample)
872 if (!tab || !sample)
873 goto error;
875 if (tab->n_sample + 1 > tab->samples->n_row) {
876 int *t = isl_realloc_array(tab->mat->ctx,
877 tab->sample_index, int, tab->n_sample + 1);
878 if (!t)
879 goto error;
880 tab->sample_index = t;
883 tab->samples = isl_mat_extend(tab->samples,
884 tab->n_sample + 1, tab->samples->n_col);
885 if (!tab->samples)
886 goto error;
888 isl_seq_cpy(tab->samples->row[tab->n_sample], sample->el, sample->size);
889 isl_vec_free(sample);
890 tab->sample_index[tab->n_sample] = tab->n_sample;
891 tab->n_sample++;
893 return 0;
894 error:
895 isl_vec_free(sample);
896 return -1;
899 struct isl_tab *isl_tab_drop_sample(struct isl_tab *tab, int s)
901 if (s != tab->n_outside) {
902 int t = tab->sample_index[tab->n_outside];
903 tab->sample_index[tab->n_outside] = tab->sample_index[s];
904 tab->sample_index[s] = t;
905 isl_mat_swap_rows(tab->samples, tab->n_outside, s);
907 tab->n_outside++;
908 if (isl_tab_push(tab, isl_tab_undo_drop_sample) < 0) {
909 isl_tab_free(tab);
910 return NULL;
913 return tab;
916 /* Record the current number of samples so that we can remove newer
917 * samples during a rollback.
919 int isl_tab_save_samples(struct isl_tab *tab)
921 union isl_tab_undo_val u;
923 if (!tab)
924 return -1;
926 u.n = tab->n_sample;
927 return push_union(tab, isl_tab_undo_saved_samples, u);
930 /* Mark row with index "row" as being redundant.
931 * If we may need to undo the operation or if the row represents
932 * a variable of the original problem, the row is kept,
933 * but no longer considered when looking for a pivot row.
934 * Otherwise, the row is simply removed.
936 * The row may be interchanged with some other row. If it
937 * is interchanged with a later row, return 1. Otherwise return 0.
938 * If the rows are checked in order in the calling function,
939 * then a return value of 1 means that the row with the given
940 * row number may now contain a different row that hasn't been checked yet.
942 int isl_tab_mark_redundant(struct isl_tab *tab, int row)
944 struct isl_tab_var *var = isl_tab_var_from_row(tab, row);
945 var->is_redundant = 1;
946 isl_assert(tab->mat->ctx, row >= tab->n_redundant, return -1);
947 if (tab->preserve || tab->need_undo || tab->row_var[row] >= 0) {
948 if (tab->row_var[row] >= 0 && !var->is_nonneg) {
949 var->is_nonneg = 1;
950 if (isl_tab_push_var(tab, isl_tab_undo_nonneg, var) < 0)
951 return -1;
953 if (row != tab->n_redundant)
954 swap_rows(tab, row, tab->n_redundant);
955 tab->n_redundant++;
956 return isl_tab_push_var(tab, isl_tab_undo_redundant, var);
957 } else {
958 if (row != tab->n_row - 1)
959 swap_rows(tab, row, tab->n_row - 1);
960 isl_tab_var_from_row(tab, tab->n_row - 1)->index = -1;
961 tab->n_row--;
962 return 1;
966 /* Mark "tab" as a rational tableau.
967 * If it wasn't marked as a rational tableau already and if we may
968 * need to undo changes, then arrange for the marking to be undone
969 * during the undo.
971 int isl_tab_mark_rational(struct isl_tab *tab)
973 if (!tab)
974 return -1;
975 if (!tab->rational && tab->need_undo)
976 if (isl_tab_push(tab, isl_tab_undo_rational) < 0)
977 return -1;
978 tab->rational = 1;
979 return 0;
982 int isl_tab_mark_empty(struct isl_tab *tab)
984 if (!tab)
985 return -1;
986 if (!tab->empty && tab->need_undo)
987 if (isl_tab_push(tab, isl_tab_undo_empty) < 0)
988 return -1;
989 tab->empty = 1;
990 return 0;
993 int isl_tab_freeze_constraint(struct isl_tab *tab, int con)
995 struct isl_tab_var *var;
997 if (!tab)
998 return -1;
1000 var = &tab->con[con];
1001 if (var->frozen)
1002 return 0;
1003 if (var->index < 0)
1004 return 0;
1005 var->frozen = 1;
1007 if (tab->need_undo)
1008 return isl_tab_push_var(tab, isl_tab_undo_freeze, var);
1010 return 0;
1013 /* Update the rows signs after a pivot of "row" and "col", with "row_sgn"
1014 * the original sign of the pivot element.
1015 * We only keep track of row signs during PILP solving and in this case
1016 * we only pivot a row with negative sign (meaning the value is always
1017 * non-positive) using a positive pivot element.
1019 * For each row j, the new value of the parametric constant is equal to
1021 * a_j0 - a_jc a_r0/a_rc
1023 * where a_j0 is the original parametric constant, a_rc is the pivot element,
1024 * a_r0 is the parametric constant of the pivot row and a_jc is the
1025 * pivot column entry of the row j.
1026 * Since a_r0 is non-positive and a_rc is positive, the sign of row j
1027 * remains the same if a_jc has the same sign as the row j or if
1028 * a_jc is zero. In all other cases, we reset the sign to "unknown".
1030 static void update_row_sign(struct isl_tab *tab, int row, int col, int row_sgn)
1032 int i;
1033 struct isl_mat *mat = tab->mat;
1034 unsigned off = 2 + tab->M;
1036 if (!tab->row_sign)
1037 return;
1039 if (tab->row_sign[row] == 0)
1040 return;
1041 isl_assert(mat->ctx, row_sgn > 0, return);
1042 isl_assert(mat->ctx, tab->row_sign[row] == isl_tab_row_neg, return);
1043 tab->row_sign[row] = isl_tab_row_pos;
1044 for (i = 0; i < tab->n_row; ++i) {
1045 int s;
1046 if (i == row)
1047 continue;
1048 s = isl_int_sgn(mat->row[i][off + col]);
1049 if (!s)
1050 continue;
1051 if (!tab->row_sign[i])
1052 continue;
1053 if (s < 0 && tab->row_sign[i] == isl_tab_row_neg)
1054 continue;
1055 if (s > 0 && tab->row_sign[i] == isl_tab_row_pos)
1056 continue;
1057 tab->row_sign[i] = isl_tab_row_unknown;
1061 /* Given a row number "row" and a column number "col", pivot the tableau
1062 * such that the associated variables are interchanged.
1063 * The given row in the tableau expresses
1065 * x_r = a_r0 + \sum_i a_ri x_i
1067 * or
1069 * x_c = 1/a_rc x_r - a_r0/a_rc + sum_{i \ne r} -a_ri/a_rc
1071 * Substituting this equality into the other rows
1073 * x_j = a_j0 + \sum_i a_ji x_i
1075 * with a_jc \ne 0, we obtain
1077 * 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
1079 * The tableau
1081 * n_rc/d_r n_ri/d_r
1082 * n_jc/d_j n_ji/d_j
1084 * where i is any other column and j is any other row,
1085 * is therefore transformed into
1087 * s(n_rc)d_r/|n_rc| -s(n_rc)n_ri/|n_rc|
1088 * 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)
1090 * The transformation is performed along the following steps
1092 * d_r/n_rc n_ri/n_rc
1093 * n_jc/d_j n_ji/d_j
1095 * s(n_rc)d_r/|n_rc| -s(n_rc)n_ri/|n_rc|
1096 * n_jc/d_j n_ji/d_j
1098 * s(n_rc)d_r/|n_rc| -s(n_rc)n_ri/|n_rc|
1099 * n_jc/(|n_rc| d_j) n_ji/(|n_rc| d_j)
1101 * s(n_rc)d_r/|n_rc| -s(n_rc)n_ri/|n_rc|
1102 * n_jc/(|n_rc| d_j) (n_ji |n_rc|)/(|n_rc| d_j)
1104 * s(n_rc)d_r/|n_rc| -s(n_rc)n_ri/|n_rc|
1105 * n_jc/(|n_rc| d_j) (n_ji |n_rc| - s(n_rc)n_jc n_ri)/(|n_rc| d_j)
1107 * s(n_rc)d_r/|n_rc| -s(n_rc)n_ri/|n_rc|
1108 * 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)
1111 int isl_tab_pivot(struct isl_tab *tab, int row, int col)
1113 int i, j;
1114 int sgn;
1115 int t;
1116 isl_ctx *ctx;
1117 struct isl_mat *mat = tab->mat;
1118 struct isl_tab_var *var;
1119 unsigned off = 2 + tab->M;
1121 ctx = isl_tab_get_ctx(tab);
1122 if (isl_ctx_next_operation(ctx) < 0)
1123 return -1;
1125 isl_int_swap(mat->row[row][0], mat->row[row][off + col]);
1126 sgn = isl_int_sgn(mat->row[row][0]);
1127 if (sgn < 0) {
1128 isl_int_neg(mat->row[row][0], mat->row[row][0]);
1129 isl_int_neg(mat->row[row][off + col], mat->row[row][off + col]);
1130 } else
1131 for (j = 0; j < off - 1 + tab->n_col; ++j) {
1132 if (j == off - 1 + col)
1133 continue;
1134 isl_int_neg(mat->row[row][1 + j], mat->row[row][1 + j]);
1136 if (!isl_int_is_one(mat->row[row][0]))
1137 isl_seq_normalize(mat->ctx, mat->row[row], off + tab->n_col);
1138 for (i = 0; i < tab->n_row; ++i) {
1139 if (i == row)
1140 continue;
1141 if (isl_int_is_zero(mat->row[i][off + col]))
1142 continue;
1143 isl_int_mul(mat->row[i][0], mat->row[i][0], mat->row[row][0]);
1144 for (j = 0; j < off - 1 + tab->n_col; ++j) {
1145 if (j == off - 1 + col)
1146 continue;
1147 isl_int_mul(mat->row[i][1 + j],
1148 mat->row[i][1 + j], mat->row[row][0]);
1149 isl_int_addmul(mat->row[i][1 + j],
1150 mat->row[i][off + col], mat->row[row][1 + j]);
1152 isl_int_mul(mat->row[i][off + col],
1153 mat->row[i][off + col], mat->row[row][off + col]);
1154 if (!isl_int_is_one(mat->row[i][0]))
1155 isl_seq_normalize(mat->ctx, mat->row[i], off + tab->n_col);
1157 t = tab->row_var[row];
1158 tab->row_var[row] = tab->col_var[col];
1159 tab->col_var[col] = t;
1160 var = isl_tab_var_from_row(tab, row);
1161 var->is_row = 1;
1162 var->index = row;
1163 var = var_from_col(tab, col);
1164 var->is_row = 0;
1165 var->index = col;
1166 update_row_sign(tab, row, col, sgn);
1167 if (tab->in_undo)
1168 return 0;
1169 for (i = tab->n_redundant; i < tab->n_row; ++i) {
1170 if (isl_int_is_zero(mat->row[i][off + col]))
1171 continue;
1172 if (!isl_tab_var_from_row(tab, i)->frozen &&
1173 isl_tab_row_is_redundant(tab, i)) {
1174 int redo = isl_tab_mark_redundant(tab, i);
1175 if (redo < 0)
1176 return -1;
1177 if (redo)
1178 --i;
1181 return 0;
1184 /* If "var" represents a column variable, then pivot is up (sgn > 0)
1185 * or down (sgn < 0) to a row. The variable is assumed not to be
1186 * unbounded in the specified direction.
1187 * If sgn = 0, then the variable is unbounded in both directions,
1188 * and we pivot with any row we can find.
1190 static int to_row(struct isl_tab *tab, struct isl_tab_var *var, int sign) WARN_UNUSED;
1191 static int to_row(struct isl_tab *tab, struct isl_tab_var *var, int sign)
1193 int r;
1194 unsigned off = 2 + tab->M;
1196 if (var->is_row)
1197 return 0;
1199 if (sign == 0) {
1200 for (r = tab->n_redundant; r < tab->n_row; ++r)
1201 if (!isl_int_is_zero(tab->mat->row[r][off+var->index]))
1202 break;
1203 isl_assert(tab->mat->ctx, r < tab->n_row, return -1);
1204 } else {
1205 r = pivot_row(tab, NULL, sign, var->index);
1206 isl_assert(tab->mat->ctx, r >= 0, return -1);
1209 return isl_tab_pivot(tab, r, var->index);
1212 /* Check whether all variables that are marked as non-negative
1213 * also have a non-negative sample value. This function is not
1214 * called from the current code but is useful during debugging.
1216 static void check_table(struct isl_tab *tab) __attribute__ ((unused));
1217 static void check_table(struct isl_tab *tab)
1219 int i;
1221 if (tab->empty)
1222 return;
1223 for (i = tab->n_redundant; i < tab->n_row; ++i) {
1224 struct isl_tab_var *var;
1225 var = isl_tab_var_from_row(tab, i);
1226 if (!var->is_nonneg)
1227 continue;
1228 if (tab->M) {
1229 isl_assert(tab->mat->ctx,
1230 !isl_int_is_neg(tab->mat->row[i][2]), abort());
1231 if (isl_int_is_pos(tab->mat->row[i][2]))
1232 continue;
1234 isl_assert(tab->mat->ctx, !isl_int_is_neg(tab->mat->row[i][1]),
1235 abort());
1239 /* Return the sign of the maximal value of "var".
1240 * If the sign is not negative, then on return from this function,
1241 * the sample value will also be non-negative.
1243 * If "var" is manifestly unbounded wrt positive values, we are done.
1244 * Otherwise, we pivot the variable up to a row if needed
1245 * Then we continue pivoting down until either
1246 * - no more down pivots can be performed
1247 * - the sample value is positive
1248 * - the variable is pivoted into a manifestly unbounded column
1250 static int sign_of_max(struct isl_tab *tab, struct isl_tab_var *var)
1252 int row, col;
1254 if (max_is_manifestly_unbounded(tab, var))
1255 return 1;
1256 if (to_row(tab, var, 1) < 0)
1257 return -2;
1258 while (!isl_int_is_pos(tab->mat->row[var->index][1])) {
1259 find_pivot(tab, var, var, 1, &row, &col);
1260 if (row == -1)
1261 return isl_int_sgn(tab->mat->row[var->index][1]);
1262 if (isl_tab_pivot(tab, row, col) < 0)
1263 return -2;
1264 if (!var->is_row) /* manifestly unbounded */
1265 return 1;
1267 return 1;
1270 int isl_tab_sign_of_max(struct isl_tab *tab, int con)
1272 struct isl_tab_var *var;
1274 if (!tab)
1275 return -2;
1277 var = &tab->con[con];
1278 isl_assert(tab->mat->ctx, !var->is_redundant, return -2);
1279 isl_assert(tab->mat->ctx, !var->is_zero, return -2);
1281 return sign_of_max(tab, var);
1284 static int row_is_neg(struct isl_tab *tab, int row)
1286 if (!tab->M)
1287 return isl_int_is_neg(tab->mat->row[row][1]);
1288 if (isl_int_is_pos(tab->mat->row[row][2]))
1289 return 0;
1290 if (isl_int_is_neg(tab->mat->row[row][2]))
1291 return 1;
1292 return isl_int_is_neg(tab->mat->row[row][1]);
1295 static int row_sgn(struct isl_tab *tab, int row)
1297 if (!tab->M)
1298 return isl_int_sgn(tab->mat->row[row][1]);
1299 if (!isl_int_is_zero(tab->mat->row[row][2]))
1300 return isl_int_sgn(tab->mat->row[row][2]);
1301 else
1302 return isl_int_sgn(tab->mat->row[row][1]);
1305 /* Perform pivots until the row variable "var" has a non-negative
1306 * sample value or until no more upward pivots can be performed.
1307 * Return the sign of the sample value after the pivots have been
1308 * performed.
1310 static int restore_row(struct isl_tab *tab, struct isl_tab_var *var)
1312 int row, col;
1314 while (row_is_neg(tab, var->index)) {
1315 find_pivot(tab, var, var, 1, &row, &col);
1316 if (row == -1)
1317 break;
1318 if (isl_tab_pivot(tab, row, col) < 0)
1319 return -2;
1320 if (!var->is_row) /* manifestly unbounded */
1321 return 1;
1323 return row_sgn(tab, var->index);
1326 /* Perform pivots until we are sure that the row variable "var"
1327 * can attain non-negative values. After return from this
1328 * function, "var" is still a row variable, but its sample
1329 * value may not be non-negative, even if the function returns 1.
1331 static int at_least_zero(struct isl_tab *tab, struct isl_tab_var *var)
1333 int row, col;
1335 while (isl_int_is_neg(tab->mat->row[var->index][1])) {
1336 find_pivot(tab, var, var, 1, &row, &col);
1337 if (row == -1)
1338 break;
1339 if (row == var->index) /* manifestly unbounded */
1340 return 1;
1341 if (isl_tab_pivot(tab, row, col) < 0)
1342 return -1;
1344 return !isl_int_is_neg(tab->mat->row[var->index][1]);
1347 /* Return a negative value if "var" can attain negative values.
1348 * Return a non-negative value otherwise.
1350 * If "var" is manifestly unbounded wrt negative values, we are done.
1351 * Otherwise, if var is in a column, we can pivot it down to a row.
1352 * Then we continue pivoting down until either
1353 * - the pivot would result in a manifestly unbounded column
1354 * => we don't perform the pivot, but simply return -1
1355 * - no more down pivots can be performed
1356 * - the sample value is negative
1357 * If the sample value becomes negative and the variable is supposed
1358 * to be nonnegative, then we undo the last pivot.
1359 * However, if the last pivot has made the pivoting variable
1360 * obviously redundant, then it may have moved to another row.
1361 * In that case we look for upward pivots until we reach a non-negative
1362 * value again.
1364 static int sign_of_min(struct isl_tab *tab, struct isl_tab_var *var)
1366 int row, col;
1367 struct isl_tab_var *pivot_var = NULL;
1369 if (min_is_manifestly_unbounded(tab, var))
1370 return -1;
1371 if (!var->is_row) {
1372 col = var->index;
1373 row = pivot_row(tab, NULL, -1, col);
1374 pivot_var = var_from_col(tab, col);
1375 if (isl_tab_pivot(tab, row, col) < 0)
1376 return -2;
1377 if (var->is_redundant)
1378 return 0;
1379 if (isl_int_is_neg(tab->mat->row[var->index][1])) {
1380 if (var->is_nonneg) {
1381 if (!pivot_var->is_redundant &&
1382 pivot_var->index == row) {
1383 if (isl_tab_pivot(tab, row, col) < 0)
1384 return -2;
1385 } else
1386 if (restore_row(tab, var) < -1)
1387 return -2;
1389 return -1;
1392 if (var->is_redundant)
1393 return 0;
1394 while (!isl_int_is_neg(tab->mat->row[var->index][1])) {
1395 find_pivot(tab, var, var, -1, &row, &col);
1396 if (row == var->index)
1397 return -1;
1398 if (row == -1)
1399 return isl_int_sgn(tab->mat->row[var->index][1]);
1400 pivot_var = var_from_col(tab, col);
1401 if (isl_tab_pivot(tab, row, col) < 0)
1402 return -2;
1403 if (var->is_redundant)
1404 return 0;
1406 if (pivot_var && var->is_nonneg) {
1407 /* pivot back to non-negative value */
1408 if (!pivot_var->is_redundant && pivot_var->index == row) {
1409 if (isl_tab_pivot(tab, row, col) < 0)
1410 return -2;
1411 } else
1412 if (restore_row(tab, var) < -1)
1413 return -2;
1415 return -1;
1418 static int row_at_most_neg_one(struct isl_tab *tab, int row)
1420 if (tab->M) {
1421 if (isl_int_is_pos(tab->mat->row[row][2]))
1422 return 0;
1423 if (isl_int_is_neg(tab->mat->row[row][2]))
1424 return 1;
1426 return isl_int_is_neg(tab->mat->row[row][1]) &&
1427 isl_int_abs_ge(tab->mat->row[row][1],
1428 tab->mat->row[row][0]);
1431 /* Return 1 if "var" can attain values <= -1.
1432 * Return 0 otherwise.
1434 * If the variable "var" is supposed to be non-negative (is_nonneg is set),
1435 * then the sample value of "var" is assumed to be non-negative when the
1436 * the function is called. If 1 is returned then the constraint
1437 * is not redundant and the sample value is made non-negative again before
1438 * the function returns.
1440 int isl_tab_min_at_most_neg_one(struct isl_tab *tab, struct isl_tab_var *var)
1442 int row, col;
1443 struct isl_tab_var *pivot_var;
1445 if (min_is_manifestly_unbounded(tab, var))
1446 return 1;
1447 if (!var->is_row) {
1448 col = var->index;
1449 row = pivot_row(tab, NULL, -1, col);
1450 pivot_var = var_from_col(tab, col);
1451 if (isl_tab_pivot(tab, row, col) < 0)
1452 return -1;
1453 if (var->is_redundant)
1454 return 0;
1455 if (row_at_most_neg_one(tab, var->index)) {
1456 if (var->is_nonneg) {
1457 if (!pivot_var->is_redundant &&
1458 pivot_var->index == row) {
1459 if (isl_tab_pivot(tab, row, col) < 0)
1460 return -1;
1461 } else
1462 if (restore_row(tab, var) < -1)
1463 return -1;
1465 return 1;
1468 if (var->is_redundant)
1469 return 0;
1470 do {
1471 find_pivot(tab, var, var, -1, &row, &col);
1472 if (row == var->index) {
1473 if (var->is_nonneg && restore_row(tab, var) < -1)
1474 return -1;
1475 return 1;
1477 if (row == -1)
1478 return 0;
1479 pivot_var = var_from_col(tab, col);
1480 if (isl_tab_pivot(tab, row, col) < 0)
1481 return -1;
1482 if (var->is_redundant)
1483 return 0;
1484 } while (!row_at_most_neg_one(tab, var->index));
1485 if (var->is_nonneg) {
1486 /* pivot back to non-negative value */
1487 if (!pivot_var->is_redundant && pivot_var->index == row)
1488 if (isl_tab_pivot(tab, row, col) < 0)
1489 return -1;
1490 if (restore_row(tab, var) < -1)
1491 return -1;
1493 return 1;
1496 /* Return 1 if "var" can attain values >= 1.
1497 * Return 0 otherwise.
1499 static int at_least_one(struct isl_tab *tab, struct isl_tab_var *var)
1501 int row, col;
1502 isl_int *r;
1504 if (max_is_manifestly_unbounded(tab, var))
1505 return 1;
1506 if (to_row(tab, var, 1) < 0)
1507 return -1;
1508 r = tab->mat->row[var->index];
1509 while (isl_int_lt(r[1], r[0])) {
1510 find_pivot(tab, var, var, 1, &row, &col);
1511 if (row == -1)
1512 return isl_int_ge(r[1], r[0]);
1513 if (row == var->index) /* manifestly unbounded */
1514 return 1;
1515 if (isl_tab_pivot(tab, row, col) < 0)
1516 return -1;
1518 return 1;
1521 static void swap_cols(struct isl_tab *tab, int col1, int col2)
1523 int t;
1524 unsigned off = 2 + tab->M;
1525 t = tab->col_var[col1];
1526 tab->col_var[col1] = tab->col_var[col2];
1527 tab->col_var[col2] = t;
1528 var_from_col(tab, col1)->index = col1;
1529 var_from_col(tab, col2)->index = col2;
1530 tab->mat = isl_mat_swap_cols(tab->mat, off + col1, off + col2);
1533 /* Mark column with index "col" as representing a zero variable.
1534 * If we may need to undo the operation the column is kept,
1535 * but no longer considered.
1536 * Otherwise, the column is simply removed.
1538 * The column may be interchanged with some other column. If it
1539 * is interchanged with a later column, return 1. Otherwise return 0.
1540 * If the columns are checked in order in the calling function,
1541 * then a return value of 1 means that the column with the given
1542 * column number may now contain a different column that
1543 * hasn't been checked yet.
1545 int isl_tab_kill_col(struct isl_tab *tab, int col)
1547 var_from_col(tab, col)->is_zero = 1;
1548 if (tab->need_undo) {
1549 if (isl_tab_push_var(tab, isl_tab_undo_zero,
1550 var_from_col(tab, col)) < 0)
1551 return -1;
1552 if (col != tab->n_dead)
1553 swap_cols(tab, col, tab->n_dead);
1554 tab->n_dead++;
1555 return 0;
1556 } else {
1557 if (col != tab->n_col - 1)
1558 swap_cols(tab, col, tab->n_col - 1);
1559 var_from_col(tab, tab->n_col - 1)->index = -1;
1560 tab->n_col--;
1561 return 1;
1565 static int row_is_manifestly_non_integral(struct isl_tab *tab, int row)
1567 unsigned off = 2 + tab->M;
1569 if (tab->M && !isl_int_eq(tab->mat->row[row][2],
1570 tab->mat->row[row][0]))
1571 return 0;
1572 if (isl_seq_first_non_zero(tab->mat->row[row] + off + tab->n_dead,
1573 tab->n_col - tab->n_dead) != -1)
1574 return 0;
1576 return !isl_int_is_divisible_by(tab->mat->row[row][1],
1577 tab->mat->row[row][0]);
1580 /* For integer tableaus, check if any of the coordinates are stuck
1581 * at a non-integral value.
1583 static int tab_is_manifestly_empty(struct isl_tab *tab)
1585 int i;
1587 if (tab->empty)
1588 return 1;
1589 if (tab->rational)
1590 return 0;
1592 for (i = 0; i < tab->n_var; ++i) {
1593 if (!tab->var[i].is_row)
1594 continue;
1595 if (row_is_manifestly_non_integral(tab, tab->var[i].index))
1596 return 1;
1599 return 0;
1602 /* Row variable "var" is non-negative and cannot attain any values
1603 * larger than zero. This means that the coefficients of the unrestricted
1604 * column variables are zero and that the coefficients of the non-negative
1605 * column variables are zero or negative.
1606 * Each of the non-negative variables with a negative coefficient can
1607 * then also be written as the negative sum of non-negative variables
1608 * and must therefore also be zero.
1610 * If "temp_var" is set, then "var" is a temporary variable that
1611 * will be removed after this function returns and for which
1612 * no information is recorded on the undo stack.
1613 * Do not add any undo records involving this variable in this case
1614 * since the variable will have been removed before any future undo
1615 * operations. Also avoid marking the variable as redundant,
1616 * since that either adds an undo record or needlessly removes the row
1617 * (the caller will take care of removing the row).
1619 static isl_stat close_row(struct isl_tab *tab, struct isl_tab_var *var,
1620 int temp_var) WARN_UNUSED;
1621 static isl_stat close_row(struct isl_tab *tab, struct isl_tab_var *var,
1622 int temp_var)
1624 int j;
1625 struct isl_mat *mat = tab->mat;
1626 unsigned off = 2 + tab->M;
1628 if (!var->is_nonneg)
1629 isl_die(isl_tab_get_ctx(tab), isl_error_internal,
1630 "expecting non-negative variable",
1631 return isl_stat_error);
1632 var->is_zero = 1;
1633 if (!temp_var && tab->need_undo)
1634 if (isl_tab_push_var(tab, isl_tab_undo_zero, var) < 0)
1635 return isl_stat_error;
1636 for (j = tab->n_dead; j < tab->n_col; ++j) {
1637 int recheck;
1638 if (isl_int_is_zero(mat->row[var->index][off + j]))
1639 continue;
1640 if (isl_int_is_pos(mat->row[var->index][off + j]))
1641 isl_die(isl_tab_get_ctx(tab), isl_error_internal,
1642 "row cannot have positive coefficients",
1643 return isl_stat_error);
1644 recheck = isl_tab_kill_col(tab, j);
1645 if (recheck < 0)
1646 return isl_stat_error;
1647 if (recheck)
1648 --j;
1650 if (!temp_var && isl_tab_mark_redundant(tab, var->index) < 0)
1651 return isl_stat_error;
1652 if (tab_is_manifestly_empty(tab) && isl_tab_mark_empty(tab) < 0)
1653 return isl_stat_error;
1654 return isl_stat_ok;
1657 /* Add a constraint to the tableau and allocate a row for it.
1658 * Return the index into the constraint array "con".
1660 * This function assumes that at least one more row and at least
1661 * one more element in the constraint array are available in the tableau.
1663 int isl_tab_allocate_con(struct isl_tab *tab)
1665 int r;
1667 isl_assert(tab->mat->ctx, tab->n_row < tab->mat->n_row, return -1);
1668 isl_assert(tab->mat->ctx, tab->n_con < tab->max_con, return -1);
1670 r = tab->n_con;
1671 tab->con[r].index = tab->n_row;
1672 tab->con[r].is_row = 1;
1673 tab->con[r].is_nonneg = 0;
1674 tab->con[r].is_zero = 0;
1675 tab->con[r].is_redundant = 0;
1676 tab->con[r].frozen = 0;
1677 tab->con[r].negated = 0;
1678 tab->row_var[tab->n_row] = ~r;
1680 tab->n_row++;
1681 tab->n_con++;
1682 if (isl_tab_push_var(tab, isl_tab_undo_allocate, &tab->con[r]) < 0)
1683 return -1;
1685 return r;
1688 /* Move the entries in tab->var up one position, starting at "first",
1689 * creating room for an extra entry at position "first".
1690 * Since some of the entries of tab->row_var and tab->col_var contain
1691 * indices into this array, they have to be updated accordingly.
1693 static int var_insert_entry(struct isl_tab *tab, int first)
1695 int i;
1697 if (tab->n_var >= tab->max_var)
1698 isl_die(isl_tab_get_ctx(tab), isl_error_internal,
1699 "not enough room for new variable", return -1);
1700 if (first > tab->n_var)
1701 isl_die(isl_tab_get_ctx(tab), isl_error_internal,
1702 "invalid initial position", return -1);
1704 for (i = tab->n_var - 1; i >= first; --i) {
1705 tab->var[i + 1] = tab->var[i];
1706 if (tab->var[i + 1].is_row)
1707 tab->row_var[tab->var[i + 1].index]++;
1708 else
1709 tab->col_var[tab->var[i + 1].index]++;
1712 tab->n_var++;
1714 return 0;
1717 /* Drop the entry at position "first" in tab->var, moving all
1718 * subsequent entries down.
1719 * Since some of the entries of tab->row_var and tab->col_var contain
1720 * indices into this array, they have to be updated accordingly.
1722 static int var_drop_entry(struct isl_tab *tab, int first)
1724 int i;
1726 if (first >= tab->n_var)
1727 isl_die(isl_tab_get_ctx(tab), isl_error_internal,
1728 "invalid initial position", return -1);
1730 tab->n_var--;
1732 for (i = first; i < tab->n_var; ++i) {
1733 tab->var[i] = tab->var[i + 1];
1734 if (tab->var[i + 1].is_row)
1735 tab->row_var[tab->var[i].index]--;
1736 else
1737 tab->col_var[tab->var[i].index]--;
1740 return 0;
1743 /* Add a variable to the tableau at position "r" and allocate a column for it.
1744 * Return the index into the variable array "var", i.e., "r",
1745 * or -1 on error.
1747 int isl_tab_insert_var(struct isl_tab *tab, int r)
1749 int i;
1750 unsigned off = 2 + tab->M;
1752 isl_assert(tab->mat->ctx, tab->n_col < tab->mat->n_col, return -1);
1754 if (var_insert_entry(tab, r) < 0)
1755 return -1;
1757 tab->var[r].index = tab->n_col;
1758 tab->var[r].is_row = 0;
1759 tab->var[r].is_nonneg = 0;
1760 tab->var[r].is_zero = 0;
1761 tab->var[r].is_redundant = 0;
1762 tab->var[r].frozen = 0;
1763 tab->var[r].negated = 0;
1764 tab->col_var[tab->n_col] = r;
1766 for (i = 0; i < tab->n_row; ++i)
1767 isl_int_set_si(tab->mat->row[i][off + tab->n_col], 0);
1769 tab->n_col++;
1770 if (isl_tab_push_var(tab, isl_tab_undo_allocate, &tab->var[r]) < 0)
1771 return -1;
1773 return r;
1776 /* Add a variable to the tableau and allocate a column for it.
1777 * Return the index into the variable array "var".
1779 int isl_tab_allocate_var(struct isl_tab *tab)
1781 if (!tab)
1782 return -1;
1784 return isl_tab_insert_var(tab, tab->n_var);
1787 /* Add a row to the tableau. The row is given as an affine combination
1788 * of the original variables and needs to be expressed in terms of the
1789 * column variables.
1791 * This function assumes that at least one more row and at least
1792 * one more element in the constraint array are available in the tableau.
1794 * We add each term in turn.
1795 * If r = n/d_r is the current sum and we need to add k x, then
1796 * if x is a column variable, we increase the numerator of
1797 * this column by k d_r
1798 * if x = f/d_x is a row variable, then the new representation of r is
1800 * n k f d_x/g n + d_r/g k f m/d_r n + m/d_g k f
1801 * --- + --- = ------------------- = -------------------
1802 * d_r d_r d_r d_x/g m
1804 * with g the gcd of d_r and d_x and m the lcm of d_r and d_x.
1806 * If tab->M is set, then, internally, each variable x is represented
1807 * as x' - M. We then also need no subtract k d_r from the coefficient of M.
1809 int isl_tab_add_row(struct isl_tab *tab, isl_int *line)
1811 int i;
1812 int r;
1813 isl_int *row;
1814 isl_int a, b;
1815 unsigned off = 2 + tab->M;
1817 r = isl_tab_allocate_con(tab);
1818 if (r < 0)
1819 return -1;
1821 isl_int_init(a);
1822 isl_int_init(b);
1823 row = tab->mat->row[tab->con[r].index];
1824 isl_int_set_si(row[0], 1);
1825 isl_int_set(row[1], line[0]);
1826 isl_seq_clr(row + 2, tab->M + tab->n_col);
1827 for (i = 0; i < tab->n_var; ++i) {
1828 if (tab->var[i].is_zero)
1829 continue;
1830 if (tab->var[i].is_row) {
1831 isl_int_lcm(a,
1832 row[0], tab->mat->row[tab->var[i].index][0]);
1833 isl_int_swap(a, row[0]);
1834 isl_int_divexact(a, row[0], a);
1835 isl_int_divexact(b,
1836 row[0], tab->mat->row[tab->var[i].index][0]);
1837 isl_int_mul(b, b, line[1 + i]);
1838 isl_seq_combine(row + 1, a, row + 1,
1839 b, tab->mat->row[tab->var[i].index] + 1,
1840 1 + tab->M + tab->n_col);
1841 } else
1842 isl_int_addmul(row[off + tab->var[i].index],
1843 line[1 + i], row[0]);
1844 if (tab->M && i >= tab->n_param && i < tab->n_var - tab->n_div)
1845 isl_int_submul(row[2], line[1 + i], row[0]);
1847 isl_seq_normalize(tab->mat->ctx, row, off + tab->n_col);
1848 isl_int_clear(a);
1849 isl_int_clear(b);
1851 if (tab->row_sign)
1852 tab->row_sign[tab->con[r].index] = isl_tab_row_unknown;
1854 return r;
1857 static int drop_row(struct isl_tab *tab, int row)
1859 isl_assert(tab->mat->ctx, ~tab->row_var[row] == tab->n_con - 1, return -1);
1860 if (row != tab->n_row - 1)
1861 swap_rows(tab, row, tab->n_row - 1);
1862 tab->n_row--;
1863 tab->n_con--;
1864 return 0;
1867 /* Drop the variable in column "col" along with the column.
1868 * The column is removed first because it may need to be moved
1869 * into the last position and this process requires
1870 * the contents of the col_var array in a state
1871 * before the removal of the variable.
1873 static int drop_col(struct isl_tab *tab, int col)
1875 int var;
1877 var = tab->col_var[col];
1878 if (col != tab->n_col - 1)
1879 swap_cols(tab, col, tab->n_col - 1);
1880 tab->n_col--;
1881 if (var_drop_entry(tab, var) < 0)
1882 return -1;
1883 return 0;
1886 /* Add inequality "ineq" and check if it conflicts with the
1887 * previously added constraints or if it is obviously redundant.
1889 * This function assumes that at least one more row and at least
1890 * one more element in the constraint array are available in the tableau.
1892 int isl_tab_add_ineq(struct isl_tab *tab, isl_int *ineq)
1894 int r;
1895 int sgn;
1896 isl_int cst;
1898 if (!tab)
1899 return -1;
1900 if (tab->bmap) {
1901 struct isl_basic_map *bmap = tab->bmap;
1903 isl_assert(tab->mat->ctx, tab->n_eq == bmap->n_eq, return -1);
1904 isl_assert(tab->mat->ctx,
1905 tab->n_con == bmap->n_eq + bmap->n_ineq, return -1);
1906 tab->bmap = isl_basic_map_add_ineq(tab->bmap, ineq);
1907 if (isl_tab_push(tab, isl_tab_undo_bmap_ineq) < 0)
1908 return -1;
1909 if (!tab->bmap)
1910 return -1;
1912 if (tab->cone) {
1913 isl_int_init(cst);
1914 isl_int_set_si(cst, 0);
1915 isl_int_swap(ineq[0], cst);
1917 r = isl_tab_add_row(tab, ineq);
1918 if (tab->cone) {
1919 isl_int_swap(ineq[0], cst);
1920 isl_int_clear(cst);
1922 if (r < 0)
1923 return -1;
1924 tab->con[r].is_nonneg = 1;
1925 if (isl_tab_push_var(tab, isl_tab_undo_nonneg, &tab->con[r]) < 0)
1926 return -1;
1927 if (isl_tab_row_is_redundant(tab, tab->con[r].index)) {
1928 if (isl_tab_mark_redundant(tab, tab->con[r].index) < 0)
1929 return -1;
1930 return 0;
1933 sgn = restore_row(tab, &tab->con[r]);
1934 if (sgn < -1)
1935 return -1;
1936 if (sgn < 0)
1937 return isl_tab_mark_empty(tab);
1938 if (tab->con[r].is_row && isl_tab_row_is_redundant(tab, tab->con[r].index))
1939 if (isl_tab_mark_redundant(tab, tab->con[r].index) < 0)
1940 return -1;
1941 return 0;
1944 /* Pivot a non-negative variable down until it reaches the value zero
1945 * and then pivot the variable into a column position.
1947 static int to_col(struct isl_tab *tab, struct isl_tab_var *var) WARN_UNUSED;
1948 static int to_col(struct isl_tab *tab, struct isl_tab_var *var)
1950 int i;
1951 int row, col;
1952 unsigned off = 2 + tab->M;
1954 if (!var->is_row)
1955 return 0;
1957 while (isl_int_is_pos(tab->mat->row[var->index][1])) {
1958 find_pivot(tab, var, NULL, -1, &row, &col);
1959 isl_assert(tab->mat->ctx, row != -1, return -1);
1960 if (isl_tab_pivot(tab, row, col) < 0)
1961 return -1;
1962 if (!var->is_row)
1963 return 0;
1966 for (i = tab->n_dead; i < tab->n_col; ++i)
1967 if (!isl_int_is_zero(tab->mat->row[var->index][off + i]))
1968 break;
1970 isl_assert(tab->mat->ctx, i < tab->n_col, return -1);
1971 if (isl_tab_pivot(tab, var->index, i) < 0)
1972 return -1;
1974 return 0;
1977 /* We assume Gaussian elimination has been performed on the equalities.
1978 * The equalities can therefore never conflict.
1979 * Adding the equalities is currently only really useful for a later call
1980 * to isl_tab_ineq_type.
1982 * This function assumes that at least one more row and at least
1983 * one more element in the constraint array are available in the tableau.
1985 static struct isl_tab *add_eq(struct isl_tab *tab, isl_int *eq)
1987 int i;
1988 int r;
1990 if (!tab)
1991 return NULL;
1992 r = isl_tab_add_row(tab, eq);
1993 if (r < 0)
1994 goto error;
1996 r = tab->con[r].index;
1997 i = isl_seq_first_non_zero(tab->mat->row[r] + 2 + tab->M + tab->n_dead,
1998 tab->n_col - tab->n_dead);
1999 isl_assert(tab->mat->ctx, i >= 0, goto error);
2000 i += tab->n_dead;
2001 if (isl_tab_pivot(tab, r, i) < 0)
2002 goto error;
2003 if (isl_tab_kill_col(tab, i) < 0)
2004 goto error;
2005 tab->n_eq++;
2007 return tab;
2008 error:
2009 isl_tab_free(tab);
2010 return NULL;
2013 static int row_is_manifestly_zero(struct isl_tab *tab, int row)
2015 unsigned off = 2 + tab->M;
2017 if (!isl_int_is_zero(tab->mat->row[row][1]))
2018 return 0;
2019 if (tab->M && !isl_int_is_zero(tab->mat->row[row][2]))
2020 return 0;
2021 return isl_seq_first_non_zero(tab->mat->row[row] + off + tab->n_dead,
2022 tab->n_col - tab->n_dead) == -1;
2025 /* Add an equality that is known to be valid for the given tableau.
2027 * This function assumes that at least one more row and at least
2028 * one more element in the constraint array are available in the tableau.
2030 int isl_tab_add_valid_eq(struct isl_tab *tab, isl_int *eq)
2032 struct isl_tab_var *var;
2033 int r;
2035 if (!tab)
2036 return -1;
2037 r = isl_tab_add_row(tab, eq);
2038 if (r < 0)
2039 return -1;
2041 var = &tab->con[r];
2042 r = var->index;
2043 if (row_is_manifestly_zero(tab, r)) {
2044 var->is_zero = 1;
2045 if (isl_tab_mark_redundant(tab, r) < 0)
2046 return -1;
2047 return 0;
2050 if (isl_int_is_neg(tab->mat->row[r][1])) {
2051 isl_seq_neg(tab->mat->row[r] + 1, tab->mat->row[r] + 1,
2052 1 + tab->n_col);
2053 var->negated = 1;
2055 var->is_nonneg = 1;
2056 if (to_col(tab, var) < 0)
2057 return -1;
2058 var->is_nonneg = 0;
2059 if (isl_tab_kill_col(tab, var->index) < 0)
2060 return -1;
2062 return 0;
2065 /* Add a zero row to "tab" and return the corresponding index
2066 * in the constraint array.
2068 * This function assumes that at least one more row and at least
2069 * one more element in the constraint array are available in the tableau.
2071 static int add_zero_row(struct isl_tab *tab)
2073 int r;
2074 isl_int *row;
2076 r = isl_tab_allocate_con(tab);
2077 if (r < 0)
2078 return -1;
2080 row = tab->mat->row[tab->con[r].index];
2081 isl_seq_clr(row + 1, 1 + tab->M + tab->n_col);
2082 isl_int_set_si(row[0], 1);
2084 return r;
2087 /* Add equality "eq" and check if it conflicts with the
2088 * previously added constraints or if it is obviously redundant.
2090 * This function assumes that at least one more row and at least
2091 * one more element in the constraint array are available in the tableau.
2092 * If tab->bmap is set, then two rows are needed instead of one.
2094 int isl_tab_add_eq(struct isl_tab *tab, isl_int *eq)
2096 struct isl_tab_undo *snap = NULL;
2097 struct isl_tab_var *var;
2098 int r;
2099 int row;
2100 int sgn;
2101 isl_int cst;
2103 if (!tab)
2104 return -1;
2105 isl_assert(tab->mat->ctx, !tab->M, return -1);
2107 if (tab->need_undo)
2108 snap = isl_tab_snap(tab);
2110 if (tab->cone) {
2111 isl_int_init(cst);
2112 isl_int_set_si(cst, 0);
2113 isl_int_swap(eq[0], cst);
2115 r = isl_tab_add_row(tab, eq);
2116 if (tab->cone) {
2117 isl_int_swap(eq[0], cst);
2118 isl_int_clear(cst);
2120 if (r < 0)
2121 return -1;
2123 var = &tab->con[r];
2124 row = var->index;
2125 if (row_is_manifestly_zero(tab, row)) {
2126 if (snap)
2127 return isl_tab_rollback(tab, snap);
2128 return drop_row(tab, row);
2131 if (tab->bmap) {
2132 tab->bmap = isl_basic_map_add_ineq(tab->bmap, eq);
2133 if (isl_tab_push(tab, isl_tab_undo_bmap_ineq) < 0)
2134 return -1;
2135 isl_seq_neg(eq, eq, 1 + tab->n_var);
2136 tab->bmap = isl_basic_map_add_ineq(tab->bmap, eq);
2137 isl_seq_neg(eq, eq, 1 + tab->n_var);
2138 if (isl_tab_push(tab, isl_tab_undo_bmap_ineq) < 0)
2139 return -1;
2140 if (!tab->bmap)
2141 return -1;
2142 if (add_zero_row(tab) < 0)
2143 return -1;
2146 sgn = isl_int_sgn(tab->mat->row[row][1]);
2148 if (sgn > 0) {
2149 isl_seq_neg(tab->mat->row[row] + 1, tab->mat->row[row] + 1,
2150 1 + tab->n_col);
2151 var->negated = 1;
2152 sgn = -1;
2155 if (sgn < 0) {
2156 sgn = sign_of_max(tab, var);
2157 if (sgn < -1)
2158 return -1;
2159 if (sgn < 0) {
2160 if (isl_tab_mark_empty(tab) < 0)
2161 return -1;
2162 return 0;
2166 var->is_nonneg = 1;
2167 if (to_col(tab, var) < 0)
2168 return -1;
2169 var->is_nonneg = 0;
2170 if (isl_tab_kill_col(tab, var->index) < 0)
2171 return -1;
2173 return 0;
2176 /* Construct and return an inequality that expresses an upper bound
2177 * on the given div.
2178 * In particular, if the div is given by
2180 * d = floor(e/m)
2182 * then the inequality expresses
2184 * m d <= e
2186 static struct isl_vec *ineq_for_div(struct isl_basic_map *bmap, unsigned div)
2188 unsigned total;
2189 unsigned div_pos;
2190 struct isl_vec *ineq;
2192 if (!bmap)
2193 return NULL;
2195 total = isl_basic_map_total_dim(bmap);
2196 div_pos = 1 + total - bmap->n_div + div;
2198 ineq = isl_vec_alloc(bmap->ctx, 1 + total);
2199 if (!ineq)
2200 return NULL;
2202 isl_seq_cpy(ineq->el, bmap->div[div] + 1, 1 + total);
2203 isl_int_neg(ineq->el[div_pos], bmap->div[div][0]);
2204 return ineq;
2207 /* For a div d = floor(f/m), add the constraints
2209 * f - m d >= 0
2210 * -(f-(m-1)) + m d >= 0
2212 * Note that the second constraint is the negation of
2214 * f - m d >= m
2216 * If add_ineq is not NULL, then this function is used
2217 * instead of isl_tab_add_ineq to effectively add the inequalities.
2219 * This function assumes that at least two more rows and at least
2220 * two more elements in the constraint array are available in the tableau.
2222 static int add_div_constraints(struct isl_tab *tab, unsigned div,
2223 int (*add_ineq)(void *user, isl_int *), void *user)
2225 unsigned total;
2226 unsigned div_pos;
2227 struct isl_vec *ineq;
2229 total = isl_basic_map_total_dim(tab->bmap);
2230 div_pos = 1 + total - tab->bmap->n_div + div;
2232 ineq = ineq_for_div(tab->bmap, div);
2233 if (!ineq)
2234 goto error;
2236 if (add_ineq) {
2237 if (add_ineq(user, ineq->el) < 0)
2238 goto error;
2239 } else {
2240 if (isl_tab_add_ineq(tab, ineq->el) < 0)
2241 goto error;
2244 isl_seq_neg(ineq->el, tab->bmap->div[div] + 1, 1 + total);
2245 isl_int_set(ineq->el[div_pos], tab->bmap->div[div][0]);
2246 isl_int_add(ineq->el[0], ineq->el[0], ineq->el[div_pos]);
2247 isl_int_sub_ui(ineq->el[0], ineq->el[0], 1);
2249 if (add_ineq) {
2250 if (add_ineq(user, ineq->el) < 0)
2251 goto error;
2252 } else {
2253 if (isl_tab_add_ineq(tab, ineq->el) < 0)
2254 goto error;
2257 isl_vec_free(ineq);
2259 return 0;
2260 error:
2261 isl_vec_free(ineq);
2262 return -1;
2265 /* Check whether the div described by "div" is obviously non-negative.
2266 * If we are using a big parameter, then we will encode the div
2267 * as div' = M + div, which is always non-negative.
2268 * Otherwise, we check whether div is a non-negative affine combination
2269 * of non-negative variables.
2271 static int div_is_nonneg(struct isl_tab *tab, __isl_keep isl_vec *div)
2273 int i;
2275 if (tab->M)
2276 return 1;
2278 if (isl_int_is_neg(div->el[1]))
2279 return 0;
2281 for (i = 0; i < tab->n_var; ++i) {
2282 if (isl_int_is_neg(div->el[2 + i]))
2283 return 0;
2284 if (isl_int_is_zero(div->el[2 + i]))
2285 continue;
2286 if (!tab->var[i].is_nonneg)
2287 return 0;
2290 return 1;
2293 /* Insert an extra div, prescribed by "div", to the tableau and
2294 * the associated bmap (which is assumed to be non-NULL).
2295 * The extra integer division is inserted at (tableau) position "pos".
2296 * Return "pos" or -1 if an error occurred.
2298 * If add_ineq is not NULL, then this function is used instead
2299 * of isl_tab_add_ineq to add the div constraints.
2300 * This complication is needed because the code in isl_tab_pip
2301 * wants to perform some extra processing when an inequality
2302 * is added to the tableau.
2304 int isl_tab_insert_div(struct isl_tab *tab, int pos, __isl_keep isl_vec *div,
2305 int (*add_ineq)(void *user, isl_int *), void *user)
2307 int r;
2308 int nonneg;
2309 int n_div, o_div;
2311 if (!tab || !div)
2312 return -1;
2314 if (div->size != 1 + 1 + tab->n_var)
2315 isl_die(isl_tab_get_ctx(tab), isl_error_invalid,
2316 "unexpected size", return -1);
2318 isl_assert(tab->mat->ctx, tab->bmap, return -1);
2319 n_div = isl_basic_map_dim(tab->bmap, isl_dim_div);
2320 o_div = tab->n_var - n_div;
2321 if (pos < o_div || pos > tab->n_var)
2322 isl_die(isl_tab_get_ctx(tab), isl_error_invalid,
2323 "invalid position", return -1);
2325 nonneg = div_is_nonneg(tab, div);
2327 if (isl_tab_extend_cons(tab, 3) < 0)
2328 return -1;
2329 if (isl_tab_extend_vars(tab, 1) < 0)
2330 return -1;
2331 r = isl_tab_insert_var(tab, pos);
2332 if (r < 0)
2333 return -1;
2335 if (nonneg)
2336 tab->var[r].is_nonneg = 1;
2338 tab->bmap = isl_basic_map_insert_div(tab->bmap, pos - o_div, div);
2339 if (!tab->bmap)
2340 return -1;
2341 if (isl_tab_push_var(tab, isl_tab_undo_bmap_div, &tab->var[r]) < 0)
2342 return -1;
2344 if (add_div_constraints(tab, pos - o_div, add_ineq, user) < 0)
2345 return -1;
2347 return r;
2350 /* Add an extra div, prescribed by "div", to the tableau and
2351 * the associated bmap (which is assumed to be non-NULL).
2353 int isl_tab_add_div(struct isl_tab *tab, __isl_keep isl_vec *div)
2355 if (!tab)
2356 return -1;
2357 return isl_tab_insert_div(tab, tab->n_var, div, NULL, NULL);
2360 /* If "track" is set, then we want to keep track of all constraints in tab
2361 * in its bmap field. This field is initialized from a copy of "bmap",
2362 * so we need to make sure that all constraints in "bmap" also appear
2363 * in the constructed tab.
2365 __isl_give struct isl_tab *isl_tab_from_basic_map(
2366 __isl_keep isl_basic_map *bmap, int track)
2368 int i;
2369 struct isl_tab *tab;
2371 if (!bmap)
2372 return NULL;
2373 tab = isl_tab_alloc(bmap->ctx,
2374 isl_basic_map_total_dim(bmap) + bmap->n_ineq + 1,
2375 isl_basic_map_total_dim(bmap), 0);
2376 if (!tab)
2377 return NULL;
2378 tab->preserve = track;
2379 tab->rational = ISL_F_ISSET(bmap, ISL_BASIC_MAP_RATIONAL);
2380 if (ISL_F_ISSET(bmap, ISL_BASIC_MAP_EMPTY)) {
2381 if (isl_tab_mark_empty(tab) < 0)
2382 goto error;
2383 goto done;
2385 for (i = 0; i < bmap->n_eq; ++i) {
2386 tab = add_eq(tab, bmap->eq[i]);
2387 if (!tab)
2388 return tab;
2390 for (i = 0; i < bmap->n_ineq; ++i) {
2391 if (isl_tab_add_ineq(tab, bmap->ineq[i]) < 0)
2392 goto error;
2393 if (tab->empty)
2394 goto done;
2396 done:
2397 if (track && isl_tab_track_bmap(tab, isl_basic_map_copy(bmap)) < 0)
2398 goto error;
2399 return tab;
2400 error:
2401 isl_tab_free(tab);
2402 return NULL;
2405 __isl_give struct isl_tab *isl_tab_from_basic_set(
2406 __isl_keep isl_basic_set *bset, int track)
2408 return isl_tab_from_basic_map(bset, track);
2411 /* Construct a tableau corresponding to the recession cone of "bset".
2413 struct isl_tab *isl_tab_from_recession_cone(__isl_keep isl_basic_set *bset,
2414 int parametric)
2416 isl_int cst;
2417 int i;
2418 struct isl_tab *tab;
2419 unsigned offset = 0;
2421 if (!bset)
2422 return NULL;
2423 if (parametric)
2424 offset = isl_basic_set_dim(bset, isl_dim_param);
2425 tab = isl_tab_alloc(bset->ctx, bset->n_eq + bset->n_ineq,
2426 isl_basic_set_total_dim(bset) - offset, 0);
2427 if (!tab)
2428 return NULL;
2429 tab->rational = ISL_F_ISSET(bset, ISL_BASIC_SET_RATIONAL);
2430 tab->cone = 1;
2432 isl_int_init(cst);
2433 isl_int_set_si(cst, 0);
2434 for (i = 0; i < bset->n_eq; ++i) {
2435 isl_int_swap(bset->eq[i][offset], cst);
2436 if (offset > 0) {
2437 if (isl_tab_add_eq(tab, bset->eq[i] + offset) < 0)
2438 goto error;
2439 } else
2440 tab = add_eq(tab, bset->eq[i]);
2441 isl_int_swap(bset->eq[i][offset], cst);
2442 if (!tab)
2443 goto done;
2445 for (i = 0; i < bset->n_ineq; ++i) {
2446 int r;
2447 isl_int_swap(bset->ineq[i][offset], cst);
2448 r = isl_tab_add_row(tab, bset->ineq[i] + offset);
2449 isl_int_swap(bset->ineq[i][offset], cst);
2450 if (r < 0)
2451 goto error;
2452 tab->con[r].is_nonneg = 1;
2453 if (isl_tab_push_var(tab, isl_tab_undo_nonneg, &tab->con[r]) < 0)
2454 goto error;
2456 done:
2457 isl_int_clear(cst);
2458 return tab;
2459 error:
2460 isl_int_clear(cst);
2461 isl_tab_free(tab);
2462 return NULL;
2465 /* Assuming "tab" is the tableau of a cone, check if the cone is
2466 * bounded, i.e., if it is empty or only contains the origin.
2468 int isl_tab_cone_is_bounded(struct isl_tab *tab)
2470 int i;
2472 if (!tab)
2473 return -1;
2474 if (tab->empty)
2475 return 1;
2476 if (tab->n_dead == tab->n_col)
2477 return 1;
2479 for (;;) {
2480 for (i = tab->n_redundant; i < tab->n_row; ++i) {
2481 struct isl_tab_var *var;
2482 int sgn;
2483 var = isl_tab_var_from_row(tab, i);
2484 if (!var->is_nonneg)
2485 continue;
2486 sgn = sign_of_max(tab, var);
2487 if (sgn < -1)
2488 return -1;
2489 if (sgn != 0)
2490 return 0;
2491 if (close_row(tab, var, 0) < 0)
2492 return -1;
2493 break;
2495 if (tab->n_dead == tab->n_col)
2496 return 1;
2497 if (i == tab->n_row)
2498 return 0;
2502 int isl_tab_sample_is_integer(struct isl_tab *tab)
2504 int i;
2506 if (!tab)
2507 return -1;
2509 for (i = 0; i < tab->n_var; ++i) {
2510 int row;
2511 if (!tab->var[i].is_row)
2512 continue;
2513 row = tab->var[i].index;
2514 if (!isl_int_is_divisible_by(tab->mat->row[row][1],
2515 tab->mat->row[row][0]))
2516 return 0;
2518 return 1;
2521 static struct isl_vec *extract_integer_sample(struct isl_tab *tab)
2523 int i;
2524 struct isl_vec *vec;
2526 vec = isl_vec_alloc(tab->mat->ctx, 1 + tab->n_var);
2527 if (!vec)
2528 return NULL;
2530 isl_int_set_si(vec->block.data[0], 1);
2531 for (i = 0; i < tab->n_var; ++i) {
2532 if (!tab->var[i].is_row)
2533 isl_int_set_si(vec->block.data[1 + i], 0);
2534 else {
2535 int row = tab->var[i].index;
2536 isl_int_divexact(vec->block.data[1 + i],
2537 tab->mat->row[row][1], tab->mat->row[row][0]);
2541 return vec;
2544 struct isl_vec *isl_tab_get_sample_value(struct isl_tab *tab)
2546 int i;
2547 struct isl_vec *vec;
2548 isl_int m;
2550 if (!tab)
2551 return NULL;
2553 vec = isl_vec_alloc(tab->mat->ctx, 1 + tab->n_var);
2554 if (!vec)
2555 return NULL;
2557 isl_int_init(m);
2559 isl_int_set_si(vec->block.data[0], 1);
2560 for (i = 0; i < tab->n_var; ++i) {
2561 int row;
2562 if (!tab->var[i].is_row) {
2563 isl_int_set_si(vec->block.data[1 + i], 0);
2564 continue;
2566 row = tab->var[i].index;
2567 isl_int_gcd(m, vec->block.data[0], tab->mat->row[row][0]);
2568 isl_int_divexact(m, tab->mat->row[row][0], m);
2569 isl_seq_scale(vec->block.data, vec->block.data, m, 1 + i);
2570 isl_int_divexact(m, vec->block.data[0], tab->mat->row[row][0]);
2571 isl_int_mul(vec->block.data[1 + i], m, tab->mat->row[row][1]);
2573 vec = isl_vec_normalize(vec);
2575 isl_int_clear(m);
2576 return vec;
2579 /* Update "bmap" based on the results of the tableau "tab".
2580 * In particular, implicit equalities are made explicit, redundant constraints
2581 * are removed and if the sample value happens to be integer, it is stored
2582 * in "bmap" (unless "bmap" already had an integer sample).
2584 * The tableau is assumed to have been created from "bmap" using
2585 * isl_tab_from_basic_map.
2587 struct isl_basic_map *isl_basic_map_update_from_tab(struct isl_basic_map *bmap,
2588 struct isl_tab *tab)
2590 int i;
2591 unsigned n_eq;
2593 if (!bmap)
2594 return NULL;
2595 if (!tab)
2596 return bmap;
2598 n_eq = tab->n_eq;
2599 if (tab->empty)
2600 bmap = isl_basic_map_set_to_empty(bmap);
2601 else
2602 for (i = bmap->n_ineq - 1; i >= 0; --i) {
2603 if (isl_tab_is_equality(tab, n_eq + i))
2604 isl_basic_map_inequality_to_equality(bmap, i);
2605 else if (isl_tab_is_redundant(tab, n_eq + i))
2606 isl_basic_map_drop_inequality(bmap, i);
2608 if (bmap->n_eq != n_eq)
2609 bmap = isl_basic_map_gauss(bmap, NULL);
2610 if (!tab->rational &&
2611 bmap && !bmap->sample && isl_tab_sample_is_integer(tab))
2612 bmap->sample = extract_integer_sample(tab);
2613 return bmap;
2616 struct isl_basic_set *isl_basic_set_update_from_tab(struct isl_basic_set *bset,
2617 struct isl_tab *tab)
2619 return bset_from_bmap(isl_basic_map_update_from_tab(bset_to_bmap(bset),
2620 tab));
2623 /* Drop the last constraint added to "tab" in position "r".
2624 * The constraint is expected to have remained in a row.
2626 static isl_stat drop_last_con_in_row(struct isl_tab *tab, int r)
2628 if (!tab->con[r].is_row)
2629 isl_die(isl_tab_get_ctx(tab), isl_error_internal,
2630 "row unexpectedly moved to column",
2631 return isl_stat_error);
2632 if (r + 1 != tab->n_con)
2633 isl_die(isl_tab_get_ctx(tab), isl_error_internal,
2634 "additional constraints added", return isl_stat_error);
2635 if (drop_row(tab, tab->con[r].index) < 0)
2636 return isl_stat_error;
2638 return isl_stat_ok;
2641 /* Given a non-negative variable "var", temporarily add a new non-negative
2642 * variable that is the opposite of "var", ensuring that "var" can only attain
2643 * the value zero. The new variable is removed again before this function
2644 * returns. However, the effect of forcing "var" to be zero remains.
2645 * If var = n/d is a row variable, then the new variable = -n/d.
2646 * If var is a column variables, then the new variable = -var.
2647 * If the new variable cannot attain non-negative values, then
2648 * the resulting tableau is empty.
2649 * Otherwise, we know the value will be zero and we close the row.
2651 static isl_stat cut_to_hyperplane(struct isl_tab *tab, struct isl_tab_var *var)
2653 unsigned r;
2654 isl_int *row;
2655 int sgn;
2656 unsigned off = 2 + tab->M;
2658 if (var->is_zero)
2659 return isl_stat_ok;
2660 if (var->is_redundant || !var->is_nonneg)
2661 isl_die(isl_tab_get_ctx(tab), isl_error_invalid,
2662 "expecting non-redundant non-negative variable",
2663 return isl_stat_error);
2665 if (isl_tab_extend_cons(tab, 1) < 0)
2666 return isl_stat_error;
2668 r = tab->n_con;
2669 tab->con[r].index = tab->n_row;
2670 tab->con[r].is_row = 1;
2671 tab->con[r].is_nonneg = 0;
2672 tab->con[r].is_zero = 0;
2673 tab->con[r].is_redundant = 0;
2674 tab->con[r].frozen = 0;
2675 tab->con[r].negated = 0;
2676 tab->row_var[tab->n_row] = ~r;
2677 row = tab->mat->row[tab->n_row];
2679 if (var->is_row) {
2680 isl_int_set(row[0], tab->mat->row[var->index][0]);
2681 isl_seq_neg(row + 1,
2682 tab->mat->row[var->index] + 1, 1 + tab->n_col);
2683 } else {
2684 isl_int_set_si(row[0], 1);
2685 isl_seq_clr(row + 1, 1 + tab->n_col);
2686 isl_int_set_si(row[off + var->index], -1);
2689 tab->n_row++;
2690 tab->n_con++;
2692 sgn = sign_of_max(tab, &tab->con[r]);
2693 if (sgn < -1)
2694 return isl_stat_error;
2695 if (sgn < 0) {
2696 if (drop_last_con_in_row(tab, r) < 0)
2697 return isl_stat_error;
2698 if (isl_tab_mark_empty(tab) < 0)
2699 return isl_stat_error;
2700 return isl_stat_ok;
2702 tab->con[r].is_nonneg = 1;
2703 /* sgn == 0 */
2704 if (close_row(tab, &tab->con[r], 1) < 0)
2705 return isl_stat_error;
2706 if (drop_last_con_in_row(tab, r) < 0)
2707 return isl_stat_error;
2709 return isl_stat_ok;
2712 /* Given a tableau "tab" and an inequality constraint "con" of the tableau,
2713 * relax the inequality by one. That is, the inequality r >= 0 is replaced
2714 * by r' = r + 1 >= 0.
2715 * If r is a row variable, we simply increase the constant term by one
2716 * (taking into account the denominator).
2717 * If r is a column variable, then we need to modify each row that
2718 * refers to r = r' - 1 by substituting this equality, effectively
2719 * subtracting the coefficient of the column from the constant.
2720 * We should only do this if the minimum is manifestly unbounded,
2721 * however. Otherwise, we may end up with negative sample values
2722 * for non-negative variables.
2723 * So, if r is a column variable with a minimum that is not
2724 * manifestly unbounded, then we need to move it to a row.
2725 * However, the sample value of this row may be negative,
2726 * even after the relaxation, so we need to restore it.
2727 * We therefore prefer to pivot a column up to a row, if possible.
2729 int isl_tab_relax(struct isl_tab *tab, int con)
2731 struct isl_tab_var *var;
2733 if (!tab)
2734 return -1;
2736 var = &tab->con[con];
2738 if (var->is_row && (var->index < 0 || var->index < tab->n_redundant))
2739 isl_die(tab->mat->ctx, isl_error_invalid,
2740 "cannot relax redundant constraint", return -1);
2741 if (!var->is_row && (var->index < 0 || var->index < tab->n_dead))
2742 isl_die(tab->mat->ctx, isl_error_invalid,
2743 "cannot relax dead constraint", return -1);
2745 if (!var->is_row && !max_is_manifestly_unbounded(tab, var))
2746 if (to_row(tab, var, 1) < 0)
2747 return -1;
2748 if (!var->is_row && !min_is_manifestly_unbounded(tab, var))
2749 if (to_row(tab, var, -1) < 0)
2750 return -1;
2752 if (var->is_row) {
2753 isl_int_add(tab->mat->row[var->index][1],
2754 tab->mat->row[var->index][1], tab->mat->row[var->index][0]);
2755 if (restore_row(tab, var) < 0)
2756 return -1;
2757 } else {
2758 int i;
2759 unsigned off = 2 + tab->M;
2761 for (i = 0; i < tab->n_row; ++i) {
2762 if (isl_int_is_zero(tab->mat->row[i][off + var->index]))
2763 continue;
2764 isl_int_sub(tab->mat->row[i][1], tab->mat->row[i][1],
2765 tab->mat->row[i][off + var->index]);
2770 if (isl_tab_push_var(tab, isl_tab_undo_relax, var) < 0)
2771 return -1;
2773 return 0;
2776 /* Replace the variable v at position "pos" in the tableau "tab"
2777 * by v' = v + shift.
2779 * If the variable is in a column, then we first check if we can
2780 * simply plug in v = v' - shift. The effect on a row with
2781 * coefficient f/d for variable v is that the constant term c/d
2782 * is replaced by (c - f * shift)/d. If shift is positive and
2783 * f is negative for each row that needs to remain non-negative,
2784 * then this is clearly safe. In other words, if the minimum of v
2785 * is manifestly unbounded, then we can keep v in a column position.
2786 * Otherwise, we can pivot it down to a row.
2787 * Similarly, if shift is negative, we need to check if the maximum
2788 * of is manifestly unbounded.
2790 * If the variable is in a row (from the start or after pivoting),
2791 * then the constant term c/d is replaced by (c + d * shift)/d.
2793 int isl_tab_shift_var(struct isl_tab *tab, int pos, isl_int shift)
2795 struct isl_tab_var *var;
2797 if (!tab)
2798 return -1;
2799 if (isl_int_is_zero(shift))
2800 return 0;
2802 var = &tab->var[pos];
2803 if (!var->is_row) {
2804 if (isl_int_is_neg(shift)) {
2805 if (!max_is_manifestly_unbounded(tab, var))
2806 if (to_row(tab, var, 1) < 0)
2807 return -1;
2808 } else {
2809 if (!min_is_manifestly_unbounded(tab, var))
2810 if (to_row(tab, var, -1) < 0)
2811 return -1;
2815 if (var->is_row) {
2816 isl_int_addmul(tab->mat->row[var->index][1],
2817 shift, tab->mat->row[var->index][0]);
2818 } else {
2819 int i;
2820 unsigned off = 2 + tab->M;
2822 for (i = 0; i < tab->n_row; ++i) {
2823 if (isl_int_is_zero(tab->mat->row[i][off + var->index]))
2824 continue;
2825 isl_int_submul(tab->mat->row[i][1],
2826 shift, tab->mat->row[i][off + var->index]);
2831 return 0;
2834 /* Remove the sign constraint from constraint "con".
2836 * If the constraint variable was originally marked non-negative,
2837 * then we make sure we mark it non-negative again during rollback.
2839 int isl_tab_unrestrict(struct isl_tab *tab, int con)
2841 struct isl_tab_var *var;
2843 if (!tab)
2844 return -1;
2846 var = &tab->con[con];
2847 if (!var->is_nonneg)
2848 return 0;
2850 var->is_nonneg = 0;
2851 if (isl_tab_push_var(tab, isl_tab_undo_unrestrict, var) < 0)
2852 return -1;
2854 return 0;
2857 int isl_tab_select_facet(struct isl_tab *tab, int con)
2859 if (!tab)
2860 return -1;
2862 return cut_to_hyperplane(tab, &tab->con[con]);
2865 static int may_be_equality(struct isl_tab *tab, int row)
2867 return tab->rational ? isl_int_is_zero(tab->mat->row[row][1])
2868 : isl_int_lt(tab->mat->row[row][1],
2869 tab->mat->row[row][0]);
2872 /* Check for (near) equalities among the constraints.
2873 * A constraint is an equality if it is non-negative and if
2874 * its maximal value is either
2875 * - zero (in case of rational tableaus), or
2876 * - strictly less than 1 (in case of integer tableaus)
2878 * We first mark all non-redundant and non-dead variables that
2879 * are not frozen and not obviously not an equality.
2880 * Then we iterate over all marked variables if they can attain
2881 * any values larger than zero or at least one.
2882 * If the maximal value is zero, we mark any column variables
2883 * that appear in the row as being zero and mark the row as being redundant.
2884 * Otherwise, if the maximal value is strictly less than one (and the
2885 * tableau is integer), then we restrict the value to being zero
2886 * by adding an opposite non-negative variable.
2888 int isl_tab_detect_implicit_equalities(struct isl_tab *tab)
2890 int i;
2891 unsigned n_marked;
2893 if (!tab)
2894 return -1;
2895 if (tab->empty)
2896 return 0;
2897 if (tab->n_dead == tab->n_col)
2898 return 0;
2900 n_marked = 0;
2901 for (i = tab->n_redundant; i < tab->n_row; ++i) {
2902 struct isl_tab_var *var = isl_tab_var_from_row(tab, i);
2903 var->marked = !var->frozen && var->is_nonneg &&
2904 may_be_equality(tab, i);
2905 if (var->marked)
2906 n_marked++;
2908 for (i = tab->n_dead; i < tab->n_col; ++i) {
2909 struct isl_tab_var *var = var_from_col(tab, i);
2910 var->marked = !var->frozen && var->is_nonneg;
2911 if (var->marked)
2912 n_marked++;
2914 while (n_marked) {
2915 struct isl_tab_var *var;
2916 int sgn;
2917 for (i = tab->n_redundant; i < tab->n_row; ++i) {
2918 var = isl_tab_var_from_row(tab, i);
2919 if (var->marked)
2920 break;
2922 if (i == tab->n_row) {
2923 for (i = tab->n_dead; i < tab->n_col; ++i) {
2924 var = var_from_col(tab, i);
2925 if (var->marked)
2926 break;
2928 if (i == tab->n_col)
2929 break;
2931 var->marked = 0;
2932 n_marked--;
2933 sgn = sign_of_max(tab, var);
2934 if (sgn < 0)
2935 return -1;
2936 if (sgn == 0) {
2937 if (close_row(tab, var, 0) < 0)
2938 return -1;
2939 } else if (!tab->rational && !at_least_one(tab, var)) {
2940 if (cut_to_hyperplane(tab, var) < 0)
2941 return -1;
2942 return isl_tab_detect_implicit_equalities(tab);
2944 for (i = tab->n_redundant; i < tab->n_row; ++i) {
2945 var = isl_tab_var_from_row(tab, i);
2946 if (!var->marked)
2947 continue;
2948 if (may_be_equality(tab, i))
2949 continue;
2950 var->marked = 0;
2951 n_marked--;
2955 return 0;
2958 /* Update the element of row_var or col_var that corresponds to
2959 * constraint tab->con[i] to a move from position "old" to position "i".
2961 static int update_con_after_move(struct isl_tab *tab, int i, int old)
2963 int *p;
2964 int index;
2966 index = tab->con[i].index;
2967 if (index == -1)
2968 return 0;
2969 p = tab->con[i].is_row ? tab->row_var : tab->col_var;
2970 if (p[index] != ~old)
2971 isl_die(tab->mat->ctx, isl_error_internal,
2972 "broken internal state", return -1);
2973 p[index] = ~i;
2975 return 0;
2978 /* Rotate the "n" constraints starting at "first" to the right,
2979 * putting the last constraint in the position of the first constraint.
2981 static int rotate_constraints(struct isl_tab *tab, int first, int n)
2983 int i, last;
2984 struct isl_tab_var var;
2986 if (n <= 1)
2987 return 0;
2989 last = first + n - 1;
2990 var = tab->con[last];
2991 for (i = last; i > first; --i) {
2992 tab->con[i] = tab->con[i - 1];
2993 if (update_con_after_move(tab, i, i - 1) < 0)
2994 return -1;
2996 tab->con[first] = var;
2997 if (update_con_after_move(tab, first, last) < 0)
2998 return -1;
3000 return 0;
3003 /* Make the equalities that are implicit in "bmap" but that have been
3004 * detected in the corresponding "tab" explicit in "bmap" and update
3005 * "tab" to reflect the new order of the constraints.
3007 * In particular, if inequality i is an implicit equality then
3008 * isl_basic_map_inequality_to_equality will move the inequality
3009 * in front of the other equality and it will move the last inequality
3010 * in the position of inequality i.
3011 * In the tableau, the inequalities of "bmap" are stored after the equalities
3012 * and so the original order
3014 * E E E E E A A A I B B B B L
3016 * is changed into
3018 * I E E E E E A A A L B B B B
3020 * where I is the implicit equality, the E are equalities,
3021 * the A inequalities before I, the B inequalities after I and
3022 * L the last inequality.
3023 * We therefore need to rotate to the right two sets of constraints,
3024 * those up to and including I and those after I.
3026 * If "tab" contains any constraints that are not in "bmap" then they
3027 * appear after those in "bmap" and they should be left untouched.
3029 * Note that this function leaves "bmap" in a temporary state
3030 * as it does not call isl_basic_map_gauss. Calling this function
3031 * is the responsibility of the caller.
3033 __isl_give isl_basic_map *isl_tab_make_equalities_explicit(struct isl_tab *tab,
3034 __isl_take isl_basic_map *bmap)
3036 int i;
3038 if (!tab || !bmap)
3039 return isl_basic_map_free(bmap);
3040 if (tab->empty)
3041 return bmap;
3043 for (i = bmap->n_ineq - 1; i >= 0; --i) {
3044 if (!isl_tab_is_equality(tab, bmap->n_eq + i))
3045 continue;
3046 isl_basic_map_inequality_to_equality(bmap, i);
3047 if (rotate_constraints(tab, 0, tab->n_eq + i + 1) < 0)
3048 return isl_basic_map_free(bmap);
3049 if (rotate_constraints(tab, tab->n_eq + i + 1,
3050 bmap->n_ineq - i) < 0)
3051 return isl_basic_map_free(bmap);
3052 tab->n_eq++;
3055 return bmap;
3058 static int con_is_redundant(struct isl_tab *tab, struct isl_tab_var *var)
3060 if (!tab)
3061 return -1;
3062 if (tab->rational) {
3063 int sgn = sign_of_min(tab, var);
3064 if (sgn < -1)
3065 return -1;
3066 return sgn >= 0;
3067 } else {
3068 int irred = isl_tab_min_at_most_neg_one(tab, var);
3069 if (irred < 0)
3070 return -1;
3071 return !irred;
3075 /* Return an isl_tab_var that has been marked or NULL if no such
3076 * variable can be found.
3077 * The marked field has only been set for variables that
3078 * appear in non-redundant rows or non-dead columns.
3080 * Pick the last constraint variable that is marked and
3081 * that appears in either a non-redundant row or a non-dead columns.
3082 * Since the returned variable is tested for being a redundant constraint,
3083 * there is no need to return any tab variable that corresponds to a variable.
3085 static struct isl_tab_var *select_marked(struct isl_tab *tab)
3087 int i;
3088 struct isl_tab_var *var;
3090 for (i = tab->n_con - 1; i >= 0; --i) {
3091 var = &tab->con[i];
3092 if (var->index < 0)
3093 continue;
3094 if (var->is_row && var->index < tab->n_redundant)
3095 continue;
3096 if (!var->is_row && var->index < tab->n_dead)
3097 continue;
3098 if (var->marked)
3099 return var;
3102 return NULL;
3105 /* Check for (near) redundant constraints.
3106 * A constraint is redundant if it is non-negative and if
3107 * its minimal value (temporarily ignoring the non-negativity) is either
3108 * - zero (in case of rational tableaus), or
3109 * - strictly larger than -1 (in case of integer tableaus)
3111 * We first mark all non-redundant and non-dead variables that
3112 * are not frozen and not obviously negatively unbounded.
3113 * Then we iterate over all marked variables if they can attain
3114 * any values smaller than zero or at most negative one.
3115 * If not, we mark the row as being redundant (assuming it hasn't
3116 * been detected as being obviously redundant in the mean time).
3118 int isl_tab_detect_redundant(struct isl_tab *tab)
3120 int i;
3121 unsigned n_marked;
3123 if (!tab)
3124 return -1;
3125 if (tab->empty)
3126 return 0;
3127 if (tab->n_redundant == tab->n_row)
3128 return 0;
3130 n_marked = 0;
3131 for (i = tab->n_redundant; i < tab->n_row; ++i) {
3132 struct isl_tab_var *var = isl_tab_var_from_row(tab, i);
3133 var->marked = !var->frozen && var->is_nonneg;
3134 if (var->marked)
3135 n_marked++;
3137 for (i = tab->n_dead; i < tab->n_col; ++i) {
3138 struct isl_tab_var *var = var_from_col(tab, i);
3139 var->marked = !var->frozen && var->is_nonneg &&
3140 !min_is_manifestly_unbounded(tab, var);
3141 if (var->marked)
3142 n_marked++;
3144 while (n_marked) {
3145 struct isl_tab_var *var;
3146 int red;
3147 var = select_marked(tab);
3148 if (!var)
3149 break;
3150 var->marked = 0;
3151 n_marked--;
3152 red = con_is_redundant(tab, var);
3153 if (red < 0)
3154 return -1;
3155 if (red && !var->is_redundant)
3156 if (isl_tab_mark_redundant(tab, var->index) < 0)
3157 return -1;
3158 for (i = tab->n_dead; i < tab->n_col; ++i) {
3159 var = var_from_col(tab, i);
3160 if (!var->marked)
3161 continue;
3162 if (!min_is_manifestly_unbounded(tab, var))
3163 continue;
3164 var->marked = 0;
3165 n_marked--;
3169 return 0;
3172 int isl_tab_is_equality(struct isl_tab *tab, int con)
3174 int row;
3175 unsigned off;
3177 if (!tab)
3178 return -1;
3179 if (tab->con[con].is_zero)
3180 return 1;
3181 if (tab->con[con].is_redundant)
3182 return 0;
3183 if (!tab->con[con].is_row)
3184 return tab->con[con].index < tab->n_dead;
3186 row = tab->con[con].index;
3188 off = 2 + tab->M;
3189 return isl_int_is_zero(tab->mat->row[row][1]) &&
3190 (!tab->M || isl_int_is_zero(tab->mat->row[row][2])) &&
3191 isl_seq_first_non_zero(tab->mat->row[row] + off + tab->n_dead,
3192 tab->n_col - tab->n_dead) == -1;
3195 /* Return the minimal value of the affine expression "f" with denominator
3196 * "denom" in *opt, *opt_denom, assuming the tableau is not empty and
3197 * the expression cannot attain arbitrarily small values.
3198 * If opt_denom is NULL, then *opt is rounded up to the nearest integer.
3199 * The return value reflects the nature of the result (empty, unbounded,
3200 * minimal value returned in *opt).
3202 * This function assumes that at least one more row and at least
3203 * one more element in the constraint array are available in the tableau.
3205 enum isl_lp_result isl_tab_min(struct isl_tab *tab,
3206 isl_int *f, isl_int denom, isl_int *opt, isl_int *opt_denom,
3207 unsigned flags)
3209 int r;
3210 enum isl_lp_result res = isl_lp_ok;
3211 struct isl_tab_var *var;
3212 struct isl_tab_undo *snap;
3214 if (!tab)
3215 return isl_lp_error;
3217 if (tab->empty)
3218 return isl_lp_empty;
3220 snap = isl_tab_snap(tab);
3221 r = isl_tab_add_row(tab, f);
3222 if (r < 0)
3223 return isl_lp_error;
3224 var = &tab->con[r];
3225 for (;;) {
3226 int row, col;
3227 find_pivot(tab, var, var, -1, &row, &col);
3228 if (row == var->index) {
3229 res = isl_lp_unbounded;
3230 break;
3232 if (row == -1)
3233 break;
3234 if (isl_tab_pivot(tab, row, col) < 0)
3235 return isl_lp_error;
3237 isl_int_mul(tab->mat->row[var->index][0],
3238 tab->mat->row[var->index][0], denom);
3239 if (ISL_FL_ISSET(flags, ISL_TAB_SAVE_DUAL)) {
3240 int i;
3242 isl_vec_free(tab->dual);
3243 tab->dual = isl_vec_alloc(tab->mat->ctx, 1 + tab->n_con);
3244 if (!tab->dual)
3245 return isl_lp_error;
3246 isl_int_set(tab->dual->el[0], tab->mat->row[var->index][0]);
3247 for (i = 0; i < tab->n_con; ++i) {
3248 int pos;
3249 if (tab->con[i].is_row) {
3250 isl_int_set_si(tab->dual->el[1 + i], 0);
3251 continue;
3253 pos = 2 + tab->M + tab->con[i].index;
3254 if (tab->con[i].negated)
3255 isl_int_neg(tab->dual->el[1 + i],
3256 tab->mat->row[var->index][pos]);
3257 else
3258 isl_int_set(tab->dual->el[1 + i],
3259 tab->mat->row[var->index][pos]);
3262 if (opt && res == isl_lp_ok) {
3263 if (opt_denom) {
3264 isl_int_set(*opt, tab->mat->row[var->index][1]);
3265 isl_int_set(*opt_denom, tab->mat->row[var->index][0]);
3266 } else
3267 isl_int_cdiv_q(*opt, tab->mat->row[var->index][1],
3268 tab->mat->row[var->index][0]);
3270 if (isl_tab_rollback(tab, snap) < 0)
3271 return isl_lp_error;
3272 return res;
3275 /* Is the constraint at position "con" marked as being redundant?
3276 * If it is marked as representing an equality, then it is not
3277 * considered to be redundant.
3278 * Note that isl_tab_mark_redundant marks both the isl_tab_var as
3279 * redundant and moves the corresponding row into the first
3280 * tab->n_redundant positions (or removes the row, assigning it index -1),
3281 * so the final test is actually redundant itself.
3283 int isl_tab_is_redundant(struct isl_tab *tab, int con)
3285 if (!tab)
3286 return -1;
3287 if (con < 0 || con >= tab->n_con)
3288 isl_die(isl_tab_get_ctx(tab), isl_error_invalid,
3289 "position out of bounds", return -1);
3290 if (tab->con[con].is_zero)
3291 return 0;
3292 if (tab->con[con].is_redundant)
3293 return 1;
3294 return tab->con[con].is_row && tab->con[con].index < tab->n_redundant;
3297 /* Take a snapshot of the tableau that can be restored by a call to
3298 * isl_tab_rollback.
3300 struct isl_tab_undo *isl_tab_snap(struct isl_tab *tab)
3302 if (!tab)
3303 return NULL;
3304 tab->need_undo = 1;
3305 return tab->top;
3308 /* Does "tab" need to keep track of undo information?
3309 * That is, was a snapshot taken that may need to be restored?
3311 isl_bool isl_tab_need_undo(struct isl_tab *tab)
3313 if (!tab)
3314 return isl_bool_error;
3316 return tab->need_undo;
3319 /* Remove all tracking of undo information from "tab", invalidating
3320 * any snapshots that may have been taken of the tableau.
3321 * Since all snapshots have been invalidated, there is also
3322 * no need to start keeping track of undo information again.
3324 void isl_tab_clear_undo(struct isl_tab *tab)
3326 if (!tab)
3327 return;
3329 free_undo(tab);
3330 tab->need_undo = 0;
3333 /* Undo the operation performed by isl_tab_relax.
3335 static int unrelax(struct isl_tab *tab, struct isl_tab_var *var) WARN_UNUSED;
3336 static int unrelax(struct isl_tab *tab, struct isl_tab_var *var)
3338 unsigned off = 2 + tab->M;
3340 if (!var->is_row && !max_is_manifestly_unbounded(tab, var))
3341 if (to_row(tab, var, 1) < 0)
3342 return -1;
3344 if (var->is_row) {
3345 isl_int_sub(tab->mat->row[var->index][1],
3346 tab->mat->row[var->index][1], tab->mat->row[var->index][0]);
3347 if (var->is_nonneg) {
3348 int sgn = restore_row(tab, var);
3349 isl_assert(tab->mat->ctx, sgn >= 0, return -1);
3351 } else {
3352 int i;
3354 for (i = 0; i < tab->n_row; ++i) {
3355 if (isl_int_is_zero(tab->mat->row[i][off + var->index]))
3356 continue;
3357 isl_int_add(tab->mat->row[i][1], tab->mat->row[i][1],
3358 tab->mat->row[i][off + var->index]);
3363 return 0;
3366 /* Undo the operation performed by isl_tab_unrestrict.
3368 * In particular, mark the variable as being non-negative and make
3369 * sure the sample value respects this constraint.
3371 static int ununrestrict(struct isl_tab *tab, struct isl_tab_var *var)
3373 var->is_nonneg = 1;
3375 if (var->is_row && restore_row(tab, var) < -1)
3376 return -1;
3378 return 0;
3381 static int perform_undo_var(struct isl_tab *tab, struct isl_tab_undo *undo) WARN_UNUSED;
3382 static int perform_undo_var(struct isl_tab *tab, struct isl_tab_undo *undo)
3384 struct isl_tab_var *var = var_from_index(tab, undo->u.var_index);
3385 switch (undo->type) {
3386 case isl_tab_undo_nonneg:
3387 var->is_nonneg = 0;
3388 break;
3389 case isl_tab_undo_redundant:
3390 var->is_redundant = 0;
3391 tab->n_redundant--;
3392 restore_row(tab, isl_tab_var_from_row(tab, tab->n_redundant));
3393 break;
3394 case isl_tab_undo_freeze:
3395 var->frozen = 0;
3396 break;
3397 case isl_tab_undo_zero:
3398 var->is_zero = 0;
3399 if (!var->is_row)
3400 tab->n_dead--;
3401 break;
3402 case isl_tab_undo_allocate:
3403 if (undo->u.var_index >= 0) {
3404 isl_assert(tab->mat->ctx, !var->is_row, return -1);
3405 return drop_col(tab, var->index);
3407 if (!var->is_row) {
3408 if (!max_is_manifestly_unbounded(tab, var)) {
3409 if (to_row(tab, var, 1) < 0)
3410 return -1;
3411 } else if (!min_is_manifestly_unbounded(tab, var)) {
3412 if (to_row(tab, var, -1) < 0)
3413 return -1;
3414 } else
3415 if (to_row(tab, var, 0) < 0)
3416 return -1;
3418 return drop_row(tab, var->index);
3419 case isl_tab_undo_relax:
3420 return unrelax(tab, var);
3421 case isl_tab_undo_unrestrict:
3422 return ununrestrict(tab, var);
3423 default:
3424 isl_die(tab->mat->ctx, isl_error_internal,
3425 "perform_undo_var called on invalid undo record",
3426 return -1);
3429 return 0;
3432 /* Undo the addition of an integer division to the basic map representation
3433 * of "tab" in position "pos".
3435 static isl_stat drop_bmap_div(struct isl_tab *tab, int pos)
3437 int off;
3439 off = tab->n_var - isl_basic_map_dim(tab->bmap, isl_dim_div);
3440 if (isl_basic_map_drop_div(tab->bmap, pos - off) < 0)
3441 return isl_stat_error;
3442 if (tab->samples) {
3443 tab->samples = isl_mat_drop_cols(tab->samples, 1 + pos, 1);
3444 if (!tab->samples)
3445 return isl_stat_error;
3448 return isl_stat_ok;
3451 /* Restore the tableau to the state where the basic variables
3452 * are those in "col_var".
3453 * We first construct a list of variables that are currently in
3454 * the basis, but shouldn't. Then we iterate over all variables
3455 * that should be in the basis and for each one that is currently
3456 * not in the basis, we exchange it with one of the elements of the
3457 * list constructed before.
3458 * We can always find an appropriate variable to pivot with because
3459 * the current basis is mapped to the old basis by a non-singular
3460 * matrix and so we can never end up with a zero row.
3462 static int restore_basis(struct isl_tab *tab, int *col_var)
3464 int i, j;
3465 int n_extra = 0;
3466 int *extra = NULL; /* current columns that contain bad stuff */
3467 unsigned off = 2 + tab->M;
3469 extra = isl_alloc_array(tab->mat->ctx, int, tab->n_col);
3470 if (tab->n_col && !extra)
3471 goto error;
3472 for (i = 0; i < tab->n_col; ++i) {
3473 for (j = 0; j < tab->n_col; ++j)
3474 if (tab->col_var[i] == col_var[j])
3475 break;
3476 if (j < tab->n_col)
3477 continue;
3478 extra[n_extra++] = i;
3480 for (i = 0; i < tab->n_col && n_extra > 0; ++i) {
3481 struct isl_tab_var *var;
3482 int row;
3484 for (j = 0; j < tab->n_col; ++j)
3485 if (col_var[i] == tab->col_var[j])
3486 break;
3487 if (j < tab->n_col)
3488 continue;
3489 var = var_from_index(tab, col_var[i]);
3490 row = var->index;
3491 for (j = 0; j < n_extra; ++j)
3492 if (!isl_int_is_zero(tab->mat->row[row][off+extra[j]]))
3493 break;
3494 isl_assert(tab->mat->ctx, j < n_extra, goto error);
3495 if (isl_tab_pivot(tab, row, extra[j]) < 0)
3496 goto error;
3497 extra[j] = extra[--n_extra];
3500 free(extra);
3501 return 0;
3502 error:
3503 free(extra);
3504 return -1;
3507 /* Remove all samples with index n or greater, i.e., those samples
3508 * that were added since we saved this number of samples in
3509 * isl_tab_save_samples.
3511 static void drop_samples_since(struct isl_tab *tab, int n)
3513 int i;
3515 for (i = tab->n_sample - 1; i >= 0 && tab->n_sample > n; --i) {
3516 if (tab->sample_index[i] < n)
3517 continue;
3519 if (i != tab->n_sample - 1) {
3520 int t = tab->sample_index[tab->n_sample-1];
3521 tab->sample_index[tab->n_sample-1] = tab->sample_index[i];
3522 tab->sample_index[i] = t;
3523 isl_mat_swap_rows(tab->samples, tab->n_sample-1, i);
3525 tab->n_sample--;
3529 static int perform_undo(struct isl_tab *tab, struct isl_tab_undo *undo) WARN_UNUSED;
3530 static int perform_undo(struct isl_tab *tab, struct isl_tab_undo *undo)
3532 switch (undo->type) {
3533 case isl_tab_undo_rational:
3534 tab->rational = 0;
3535 break;
3536 case isl_tab_undo_empty:
3537 tab->empty = 0;
3538 break;
3539 case isl_tab_undo_nonneg:
3540 case isl_tab_undo_redundant:
3541 case isl_tab_undo_freeze:
3542 case isl_tab_undo_zero:
3543 case isl_tab_undo_allocate:
3544 case isl_tab_undo_relax:
3545 case isl_tab_undo_unrestrict:
3546 return perform_undo_var(tab, undo);
3547 case isl_tab_undo_bmap_eq:
3548 return isl_basic_map_free_equality(tab->bmap, 1);
3549 case isl_tab_undo_bmap_ineq:
3550 return isl_basic_map_free_inequality(tab->bmap, 1);
3551 case isl_tab_undo_bmap_div:
3552 return drop_bmap_div(tab, undo->u.var_index);
3553 case isl_tab_undo_saved_basis:
3554 if (restore_basis(tab, undo->u.col_var) < 0)
3555 return -1;
3556 break;
3557 case isl_tab_undo_drop_sample:
3558 tab->n_outside--;
3559 break;
3560 case isl_tab_undo_saved_samples:
3561 drop_samples_since(tab, undo->u.n);
3562 break;
3563 case isl_tab_undo_callback:
3564 return undo->u.callback->run(undo->u.callback);
3565 default:
3566 isl_assert(tab->mat->ctx, 0, return -1);
3568 return 0;
3571 /* Return the tableau to the state it was in when the snapshot "snap"
3572 * was taken.
3574 int isl_tab_rollback(struct isl_tab *tab, struct isl_tab_undo *snap)
3576 struct isl_tab_undo *undo, *next;
3578 if (!tab)
3579 return -1;
3581 tab->in_undo = 1;
3582 for (undo = tab->top; undo && undo != &tab->bottom; undo = next) {
3583 next = undo->next;
3584 if (undo == snap)
3585 break;
3586 if (perform_undo(tab, undo) < 0) {
3587 tab->top = undo;
3588 free_undo(tab);
3589 tab->in_undo = 0;
3590 return -1;
3592 free_undo_record(undo);
3594 tab->in_undo = 0;
3595 tab->top = undo;
3596 if (!undo)
3597 return -1;
3598 return 0;
3601 /* The given row "row" represents an inequality violated by all
3602 * points in the tableau. Check for some special cases of such
3603 * separating constraints.
3604 * In particular, if the row has been reduced to the constant -1,
3605 * then we know the inequality is adjacent (but opposite) to
3606 * an equality in the tableau.
3607 * If the row has been reduced to r = c*(-1 -r'), with r' an inequality
3608 * of the tableau and c a positive constant, then the inequality
3609 * is adjacent (but opposite) to the inequality r'.
3611 static enum isl_ineq_type separation_type(struct isl_tab *tab, unsigned row)
3613 int pos;
3614 unsigned off = 2 + tab->M;
3616 if (tab->rational)
3617 return isl_ineq_separate;
3619 if (!isl_int_is_one(tab->mat->row[row][0]))
3620 return isl_ineq_separate;
3622 pos = isl_seq_first_non_zero(tab->mat->row[row] + off + tab->n_dead,
3623 tab->n_col - tab->n_dead);
3624 if (pos == -1) {
3625 if (isl_int_is_negone(tab->mat->row[row][1]))
3626 return isl_ineq_adj_eq;
3627 else
3628 return isl_ineq_separate;
3631 if (!isl_int_eq(tab->mat->row[row][1],
3632 tab->mat->row[row][off + tab->n_dead + pos]))
3633 return isl_ineq_separate;
3635 pos = isl_seq_first_non_zero(
3636 tab->mat->row[row] + off + tab->n_dead + pos + 1,
3637 tab->n_col - tab->n_dead - pos - 1);
3639 return pos == -1 ? isl_ineq_adj_ineq : isl_ineq_separate;
3642 /* Check the effect of inequality "ineq" on the tableau "tab".
3643 * The result may be
3644 * isl_ineq_redundant: satisfied by all points in the tableau
3645 * isl_ineq_separate: satisfied by no point in the tableau
3646 * isl_ineq_cut: satisfied by some by not all points
3647 * isl_ineq_adj_eq: adjacent to an equality
3648 * isl_ineq_adj_ineq: adjacent to an inequality.
3650 enum isl_ineq_type isl_tab_ineq_type(struct isl_tab *tab, isl_int *ineq)
3652 enum isl_ineq_type type = isl_ineq_error;
3653 struct isl_tab_undo *snap = NULL;
3654 int con;
3655 int row;
3657 if (!tab)
3658 return isl_ineq_error;
3660 if (isl_tab_extend_cons(tab, 1) < 0)
3661 return isl_ineq_error;
3663 snap = isl_tab_snap(tab);
3665 con = isl_tab_add_row(tab, ineq);
3666 if (con < 0)
3667 goto error;
3669 row = tab->con[con].index;
3670 if (isl_tab_row_is_redundant(tab, row))
3671 type = isl_ineq_redundant;
3672 else if (isl_int_is_neg(tab->mat->row[row][1]) &&
3673 (tab->rational ||
3674 isl_int_abs_ge(tab->mat->row[row][1],
3675 tab->mat->row[row][0]))) {
3676 int nonneg = at_least_zero(tab, &tab->con[con]);
3677 if (nonneg < 0)
3678 goto error;
3679 if (nonneg)
3680 type = isl_ineq_cut;
3681 else
3682 type = separation_type(tab, row);
3683 } else {
3684 int red = con_is_redundant(tab, &tab->con[con]);
3685 if (red < 0)
3686 goto error;
3687 if (!red)
3688 type = isl_ineq_cut;
3689 else
3690 type = isl_ineq_redundant;
3693 if (isl_tab_rollback(tab, snap))
3694 return isl_ineq_error;
3695 return type;
3696 error:
3697 return isl_ineq_error;
3700 int isl_tab_track_bmap(struct isl_tab *tab, __isl_take isl_basic_map *bmap)
3702 bmap = isl_basic_map_cow(bmap);
3703 if (!tab || !bmap)
3704 goto error;
3706 if (tab->empty) {
3707 bmap = isl_basic_map_set_to_empty(bmap);
3708 if (!bmap)
3709 goto error;
3710 tab->bmap = bmap;
3711 return 0;
3714 isl_assert(tab->mat->ctx, tab->n_eq == bmap->n_eq, goto error);
3715 isl_assert(tab->mat->ctx,
3716 tab->n_con == bmap->n_eq + bmap->n_ineq, goto error);
3718 tab->bmap = bmap;
3720 return 0;
3721 error:
3722 isl_basic_map_free(bmap);
3723 return -1;
3726 int isl_tab_track_bset(struct isl_tab *tab, __isl_take isl_basic_set *bset)
3728 return isl_tab_track_bmap(tab, bset_to_bmap(bset));
3731 __isl_keep isl_basic_set *isl_tab_peek_bset(struct isl_tab *tab)
3733 if (!tab)
3734 return NULL;
3736 return bset_from_bmap(tab->bmap);
3739 static void isl_tab_print_internal(__isl_keep struct isl_tab *tab,
3740 FILE *out, int indent)
3742 unsigned r, c;
3743 int i;
3745 if (!tab) {
3746 fprintf(out, "%*snull tab\n", indent, "");
3747 return;
3749 fprintf(out, "%*sn_redundant: %d, n_dead: %d", indent, "",
3750 tab->n_redundant, tab->n_dead);
3751 if (tab->rational)
3752 fprintf(out, ", rational");
3753 if (tab->empty)
3754 fprintf(out, ", empty");
3755 fprintf(out, "\n");
3756 fprintf(out, "%*s[", indent, "");
3757 for (i = 0; i < tab->n_var; ++i) {
3758 if (i)
3759 fprintf(out, (i == tab->n_param ||
3760 i == tab->n_var - tab->n_div) ? "; "
3761 : ", ");
3762 fprintf(out, "%c%d%s", tab->var[i].is_row ? 'r' : 'c',
3763 tab->var[i].index,
3764 tab->var[i].is_zero ? " [=0]" :
3765 tab->var[i].is_redundant ? " [R]" : "");
3767 fprintf(out, "]\n");
3768 fprintf(out, "%*s[", indent, "");
3769 for (i = 0; i < tab->n_con; ++i) {
3770 if (i)
3771 fprintf(out, ", ");
3772 fprintf(out, "%c%d%s", tab->con[i].is_row ? 'r' : 'c',
3773 tab->con[i].index,
3774 tab->con[i].is_zero ? " [=0]" :
3775 tab->con[i].is_redundant ? " [R]" : "");
3777 fprintf(out, "]\n");
3778 fprintf(out, "%*s[", indent, "");
3779 for (i = 0; i < tab->n_row; ++i) {
3780 const char *sign = "";
3781 if (i)
3782 fprintf(out, ", ");
3783 if (tab->row_sign) {
3784 if (tab->row_sign[i] == isl_tab_row_unknown)
3785 sign = "?";
3786 else if (tab->row_sign[i] == isl_tab_row_neg)
3787 sign = "-";
3788 else if (tab->row_sign[i] == isl_tab_row_pos)
3789 sign = "+";
3790 else
3791 sign = "+-";
3793 fprintf(out, "r%d: %d%s%s", i, tab->row_var[i],
3794 isl_tab_var_from_row(tab, i)->is_nonneg ? " [>=0]" : "", sign);
3796 fprintf(out, "]\n");
3797 fprintf(out, "%*s[", indent, "");
3798 for (i = 0; i < tab->n_col; ++i) {
3799 if (i)
3800 fprintf(out, ", ");
3801 fprintf(out, "c%d: %d%s", i, tab->col_var[i],
3802 var_from_col(tab, i)->is_nonneg ? " [>=0]" : "");
3804 fprintf(out, "]\n");
3805 r = tab->mat->n_row;
3806 tab->mat->n_row = tab->n_row;
3807 c = tab->mat->n_col;
3808 tab->mat->n_col = 2 + tab->M + tab->n_col;
3809 isl_mat_print_internal(tab->mat, out, indent);
3810 tab->mat->n_row = r;
3811 tab->mat->n_col = c;
3812 if (tab->bmap)
3813 isl_basic_map_print_internal(tab->bmap, out, indent);
3816 void isl_tab_dump(__isl_keep struct isl_tab *tab)
3818 isl_tab_print_internal(tab, stderr, 0);