isl_tab_extend_cons: avoid NULL pointer dereference
[isl.git] / isl_tab.c
blob5eafec454498dd021350fe0e0fa16b62cd1e9d39
1 /*
2 * Copyright 2008-2009 Katholieke Universiteit Leuven
4 * Use of this software is governed by the GNU LGPLv2.1 license
6 * Written by Sven Verdoolaege, K.U.Leuven, Departement
7 * Computerwetenschappen, Celestijnenlaan 200A, B-3001 Leuven, Belgium
8 */
10 #include "isl_mat.h"
11 #include "isl_map_private.h"
12 #include "isl_tab.h"
13 #include "isl_seq.h"
16 * The implementation of tableaus in this file was inspired by Section 8
17 * of David Detlefs, Greg Nelson and James B. Saxe, "Simplify: a theorem
18 * prover for program checking".
21 struct isl_tab *isl_tab_alloc(struct isl_ctx *ctx,
22 unsigned n_row, unsigned n_var, unsigned M)
24 int i;
25 struct isl_tab *tab;
26 unsigned off = 2 + M;
28 tab = isl_calloc_type(ctx, struct isl_tab);
29 if (!tab)
30 return NULL;
31 tab->mat = isl_mat_alloc(ctx, n_row, off + n_var);
32 if (!tab->mat)
33 goto error;
34 tab->var = isl_alloc_array(ctx, struct isl_tab_var, n_var);
35 if (!tab->var)
36 goto error;
37 tab->con = isl_alloc_array(ctx, struct isl_tab_var, n_row);
38 if (!tab->con)
39 goto error;
40 tab->col_var = isl_alloc_array(ctx, int, n_var);
41 if (!tab->col_var)
42 goto error;
43 tab->row_var = isl_alloc_array(ctx, int, n_row);
44 if (!tab->row_var)
45 goto error;
46 for (i = 0; i < n_var; ++i) {
47 tab->var[i].index = i;
48 tab->var[i].is_row = 0;
49 tab->var[i].is_nonneg = 0;
50 tab->var[i].is_zero = 0;
51 tab->var[i].is_redundant = 0;
52 tab->var[i].frozen = 0;
53 tab->var[i].negated = 0;
54 tab->col_var[i] = i;
56 tab->n_row = 0;
57 tab->n_con = 0;
58 tab->n_eq = 0;
59 tab->max_con = n_row;
60 tab->n_col = n_var;
61 tab->n_var = n_var;
62 tab->max_var = n_var;
63 tab->n_param = 0;
64 tab->n_div = 0;
65 tab->n_dead = 0;
66 tab->n_redundant = 0;
67 tab->strict_redundant = 0;
68 tab->need_undo = 0;
69 tab->rational = 0;
70 tab->empty = 0;
71 tab->in_undo = 0;
72 tab->M = M;
73 tab->cone = 0;
74 tab->bottom.type = isl_tab_undo_bottom;
75 tab->bottom.next = NULL;
76 tab->top = &tab->bottom;
78 tab->n_zero = 0;
79 tab->n_unbounded = 0;
80 tab->basis = NULL;
82 return tab;
83 error:
84 isl_tab_free(tab);
85 return NULL;
88 int isl_tab_extend_cons(struct isl_tab *tab, unsigned n_new)
90 unsigned off;
92 if (!tab)
93 return -1;
95 off = 2 + tab->M;
97 if (tab->max_con < tab->n_con + n_new) {
98 struct isl_tab_var *con;
100 con = isl_realloc_array(tab->mat->ctx, tab->con,
101 struct isl_tab_var, tab->max_con + n_new);
102 if (!con)
103 return -1;
104 tab->con = con;
105 tab->max_con += n_new;
107 if (tab->mat->n_row < tab->n_row + n_new) {
108 int *row_var;
110 tab->mat = isl_mat_extend(tab->mat,
111 tab->n_row + n_new, off + tab->n_col);
112 if (!tab->mat)
113 return -1;
114 row_var = isl_realloc_array(tab->mat->ctx, tab->row_var,
115 int, tab->mat->n_row);
116 if (!row_var)
117 return -1;
118 tab->row_var = row_var;
119 if (tab->row_sign) {
120 enum isl_tab_row_sign *s;
121 s = isl_realloc_array(tab->mat->ctx, tab->row_sign,
122 enum isl_tab_row_sign, tab->mat->n_row);
123 if (!s)
124 return -1;
125 tab->row_sign = s;
128 return 0;
131 /* Make room for at least n_new extra variables.
132 * Return -1 if anything went wrong.
134 int isl_tab_extend_vars(struct isl_tab *tab, unsigned n_new)
136 struct isl_tab_var *var;
137 unsigned off = 2 + tab->M;
139 if (tab->max_var < tab->n_var + n_new) {
140 var = isl_realloc_array(tab->mat->ctx, tab->var,
141 struct isl_tab_var, tab->n_var + n_new);
142 if (!var)
143 return -1;
144 tab->var = var;
145 tab->max_var += n_new;
148 if (tab->mat->n_col < off + tab->n_col + n_new) {
149 int *p;
151 tab->mat = isl_mat_extend(tab->mat,
152 tab->mat->n_row, off + tab->n_col + n_new);
153 if (!tab->mat)
154 return -1;
155 p = isl_realloc_array(tab->mat->ctx, tab->col_var,
156 int, tab->n_col + n_new);
157 if (!p)
158 return -1;
159 tab->col_var = p;
162 return 0;
165 struct isl_tab *isl_tab_extend(struct isl_tab *tab, unsigned n_new)
167 if (isl_tab_extend_cons(tab, n_new) >= 0)
168 return tab;
170 isl_tab_free(tab);
171 return NULL;
174 static void free_undo(struct isl_tab *tab)
176 struct isl_tab_undo *undo, *next;
178 for (undo = tab->top; undo && undo != &tab->bottom; undo = next) {
179 next = undo->next;
180 free(undo);
182 tab->top = undo;
185 void isl_tab_free(struct isl_tab *tab)
187 if (!tab)
188 return;
189 free_undo(tab);
190 isl_mat_free(tab->mat);
191 isl_vec_free(tab->dual);
192 isl_basic_map_free(tab->bmap);
193 free(tab->var);
194 free(tab->con);
195 free(tab->row_var);
196 free(tab->col_var);
197 free(tab->row_sign);
198 isl_mat_free(tab->samples);
199 free(tab->sample_index);
200 isl_mat_free(tab->basis);
201 free(tab);
204 struct isl_tab *isl_tab_dup(struct isl_tab *tab)
206 int i;
207 struct isl_tab *dup;
208 unsigned off;
210 if (!tab)
211 return NULL;
213 off = 2 + tab->M;
214 dup = isl_calloc_type(tab->mat->ctx, struct isl_tab);
215 if (!dup)
216 return NULL;
217 dup->mat = isl_mat_dup(tab->mat);
218 if (!dup->mat)
219 goto error;
220 dup->var = isl_alloc_array(tab->mat->ctx, struct isl_tab_var, tab->max_var);
221 if (!dup->var)
222 goto error;
223 for (i = 0; i < tab->n_var; ++i)
224 dup->var[i] = tab->var[i];
225 dup->con = isl_alloc_array(tab->mat->ctx, struct isl_tab_var, tab->max_con);
226 if (!dup->con)
227 goto error;
228 for (i = 0; i < tab->n_con; ++i)
229 dup->con[i] = tab->con[i];
230 dup->col_var = isl_alloc_array(tab->mat->ctx, int, tab->mat->n_col - off);
231 if (!dup->col_var)
232 goto error;
233 for (i = 0; i < tab->n_col; ++i)
234 dup->col_var[i] = tab->col_var[i];
235 dup->row_var = isl_alloc_array(tab->mat->ctx, int, tab->mat->n_row);
236 if (!dup->row_var)
237 goto error;
238 for (i = 0; i < tab->n_row; ++i)
239 dup->row_var[i] = tab->row_var[i];
240 if (tab->row_sign) {
241 dup->row_sign = isl_alloc_array(tab->mat->ctx, enum isl_tab_row_sign,
242 tab->mat->n_row);
243 if (!dup->row_sign)
244 goto error;
245 for (i = 0; i < tab->n_row; ++i)
246 dup->row_sign[i] = tab->row_sign[i];
248 if (tab->samples) {
249 dup->samples = isl_mat_dup(tab->samples);
250 if (!dup->samples)
251 goto error;
252 dup->sample_index = isl_alloc_array(tab->mat->ctx, int,
253 tab->samples->n_row);
254 if (!dup->sample_index)
255 goto error;
256 dup->n_sample = tab->n_sample;
257 dup->n_outside = tab->n_outside;
259 dup->n_row = tab->n_row;
260 dup->n_con = tab->n_con;
261 dup->n_eq = tab->n_eq;
262 dup->max_con = tab->max_con;
263 dup->n_col = tab->n_col;
264 dup->n_var = tab->n_var;
265 dup->max_var = tab->max_var;
266 dup->n_param = tab->n_param;
267 dup->n_div = tab->n_div;
268 dup->n_dead = tab->n_dead;
269 dup->n_redundant = tab->n_redundant;
270 dup->rational = tab->rational;
271 dup->empty = tab->empty;
272 dup->strict_redundant = 0;
273 dup->need_undo = 0;
274 dup->in_undo = 0;
275 dup->M = tab->M;
276 tab->cone = tab->cone;
277 dup->bottom.type = isl_tab_undo_bottom;
278 dup->bottom.next = NULL;
279 dup->top = &dup->bottom;
281 dup->n_zero = tab->n_zero;
282 dup->n_unbounded = tab->n_unbounded;
283 dup->basis = isl_mat_dup(tab->basis);
285 return dup;
286 error:
287 isl_tab_free(dup);
288 return NULL;
291 /* Construct the coefficient matrix of the product tableau
292 * of two tableaus.
293 * mat{1,2} is the coefficient matrix of tableau {1,2}
294 * row{1,2} is the number of rows in tableau {1,2}
295 * col{1,2} is the number of columns in tableau {1,2}
296 * off is the offset to the coefficient column (skipping the
297 * denominator, the constant term and the big parameter if any)
298 * r{1,2} is the number of redundant rows in tableau {1,2}
299 * d{1,2} is the number of dead columns in tableau {1,2}
301 * The order of the rows and columns in the result is as explained
302 * in isl_tab_product.
304 static struct isl_mat *tab_mat_product(struct isl_mat *mat1,
305 struct isl_mat *mat2, unsigned row1, unsigned row2,
306 unsigned col1, unsigned col2,
307 unsigned off, unsigned r1, unsigned r2, unsigned d1, unsigned d2)
309 int i;
310 struct isl_mat *prod;
311 unsigned n;
313 prod = isl_mat_alloc(mat1->ctx, mat1->n_row + mat2->n_row,
314 off + col1 + col2);
316 n = 0;
317 for (i = 0; i < r1; ++i) {
318 isl_seq_cpy(prod->row[n + i], mat1->row[i], off + d1);
319 isl_seq_clr(prod->row[n + i] + off + d1, d2);
320 isl_seq_cpy(prod->row[n + i] + off + d1 + d2,
321 mat1->row[i] + off + d1, col1 - d1);
322 isl_seq_clr(prod->row[n + i] + off + col1 + d1, col2 - d2);
325 n += r1;
326 for (i = 0; i < r2; ++i) {
327 isl_seq_cpy(prod->row[n + i], mat2->row[i], off);
328 isl_seq_clr(prod->row[n + i] + off, d1);
329 isl_seq_cpy(prod->row[n + i] + off + d1,
330 mat2->row[i] + off, d2);
331 isl_seq_clr(prod->row[n + i] + off + d1 + d2, col1 - d1);
332 isl_seq_cpy(prod->row[n + i] + off + col1 + d1,
333 mat2->row[i] + off + d2, col2 - d2);
336 n += r2;
337 for (i = 0; i < row1 - r1; ++i) {
338 isl_seq_cpy(prod->row[n + i], mat1->row[r1 + 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[r1 + i] + off + d1, col1 - d1);
342 isl_seq_clr(prod->row[n + i] + off + col1 + d1, col2 - d2);
345 n += row1 - r1;
346 for (i = 0; i < row2 - r2; ++i) {
347 isl_seq_cpy(prod->row[n + i], mat2->row[r2 + 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[r2 + 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[r2 + i] + off + d2, col2 - d2);
356 return prod;
359 /* Update the row or column index of a variable that corresponds
360 * to a variable in the first input tableau.
362 static void update_index1(struct isl_tab_var *var,
363 unsigned r1, unsigned r2, unsigned d1, unsigned d2)
365 if (var->index == -1)
366 return;
367 if (var->is_row && var->index >= r1)
368 var->index += r2;
369 if (!var->is_row && var->index >= d1)
370 var->index += d2;
373 /* Update the row or column index of a variable that corresponds
374 * to a variable in the second input tableau.
376 static void update_index2(struct isl_tab_var *var,
377 unsigned row1, unsigned col1,
378 unsigned r1, unsigned r2, unsigned d1, unsigned d2)
380 if (var->index == -1)
381 return;
382 if (var->is_row) {
383 if (var->index < r2)
384 var->index += r1;
385 else
386 var->index += row1;
387 } else {
388 if (var->index < d2)
389 var->index += d1;
390 else
391 var->index += col1;
395 /* Create a tableau that represents the Cartesian product of the sets
396 * represented by tableaus tab1 and tab2.
397 * The order of the rows in the product is
398 * - redundant rows of tab1
399 * - redundant rows of tab2
400 * - non-redundant rows of tab1
401 * - non-redundant rows of tab2
402 * The order of the columns is
403 * - denominator
404 * - constant term
405 * - coefficient of big parameter, if any
406 * - dead columns of tab1
407 * - dead columns of tab2
408 * - live columns of tab1
409 * - live columns of tab2
410 * The order of the variables and the constraints is a concatenation
411 * of order in the two input tableaus.
413 struct isl_tab *isl_tab_product(struct isl_tab *tab1, struct isl_tab *tab2)
415 int i;
416 struct isl_tab *prod;
417 unsigned off;
418 unsigned r1, r2, d1, d2;
420 if (!tab1 || !tab2)
421 return NULL;
423 isl_assert(tab1->mat->ctx, tab1->M == tab2->M, return NULL);
424 isl_assert(tab1->mat->ctx, tab1->rational == tab2->rational, return NULL);
425 isl_assert(tab1->mat->ctx, tab1->cone == tab2->cone, return NULL);
426 isl_assert(tab1->mat->ctx, !tab1->row_sign, return NULL);
427 isl_assert(tab1->mat->ctx, !tab2->row_sign, return NULL);
428 isl_assert(tab1->mat->ctx, tab1->n_param == 0, return NULL);
429 isl_assert(tab1->mat->ctx, tab2->n_param == 0, return NULL);
430 isl_assert(tab1->mat->ctx, tab1->n_div == 0, return NULL);
431 isl_assert(tab1->mat->ctx, tab2->n_div == 0, return NULL);
433 off = 2 + tab1->M;
434 r1 = tab1->n_redundant;
435 r2 = tab2->n_redundant;
436 d1 = tab1->n_dead;
437 d2 = tab2->n_dead;
438 prod = isl_calloc_type(tab1->mat->ctx, struct isl_tab);
439 if (!prod)
440 return NULL;
441 prod->mat = tab_mat_product(tab1->mat, tab2->mat,
442 tab1->n_row, tab2->n_row,
443 tab1->n_col, tab2->n_col, off, r1, r2, d1, d2);
444 if (!prod->mat)
445 goto error;
446 prod->var = isl_alloc_array(tab1->mat->ctx, struct isl_tab_var,
447 tab1->max_var + tab2->max_var);
448 if (!prod->var)
449 goto error;
450 for (i = 0; i < tab1->n_var; ++i) {
451 prod->var[i] = tab1->var[i];
452 update_index1(&prod->var[i], r1, r2, d1, d2);
454 for (i = 0; i < tab2->n_var; ++i) {
455 prod->var[tab1->n_var + i] = tab2->var[i];
456 update_index2(&prod->var[tab1->n_var + i],
457 tab1->n_row, tab1->n_col,
458 r1, r2, d1, d2);
460 prod->con = isl_alloc_array(tab1->mat->ctx, struct isl_tab_var,
461 tab1->max_con + tab2->max_con);
462 if (!prod->con)
463 goto error;
464 for (i = 0; i < tab1->n_con; ++i) {
465 prod->con[i] = tab1->con[i];
466 update_index1(&prod->con[i], r1, r2, d1, d2);
468 for (i = 0; i < tab2->n_con; ++i) {
469 prod->con[tab1->n_con + i] = tab2->con[i];
470 update_index2(&prod->con[tab1->n_con + i],
471 tab1->n_row, tab1->n_col,
472 r1, r2, d1, d2);
474 prod->col_var = isl_alloc_array(tab1->mat->ctx, int,
475 tab1->n_col + tab2->n_col);
476 if (!prod->col_var)
477 goto error;
478 for (i = 0; i < tab1->n_col; ++i) {
479 int pos = i < d1 ? i : i + d2;
480 prod->col_var[pos] = tab1->col_var[i];
482 for (i = 0; i < tab2->n_col; ++i) {
483 int pos = i < d2 ? d1 + i : tab1->n_col + i;
484 int t = tab2->col_var[i];
485 if (t >= 0)
486 t += tab1->n_var;
487 else
488 t -= tab1->n_con;
489 prod->col_var[pos] = t;
491 prod->row_var = isl_alloc_array(tab1->mat->ctx, int,
492 tab1->mat->n_row + tab2->mat->n_row);
493 if (!prod->row_var)
494 goto error;
495 for (i = 0; i < tab1->n_row; ++i) {
496 int pos = i < r1 ? i : i + r2;
497 prod->row_var[pos] = tab1->row_var[i];
499 for (i = 0; i < tab2->n_row; ++i) {
500 int pos = i < r2 ? r1 + i : tab1->n_row + i;
501 int t = tab2->row_var[i];
502 if (t >= 0)
503 t += tab1->n_var;
504 else
505 t -= tab1->n_con;
506 prod->row_var[pos] = t;
508 prod->samples = NULL;
509 prod->sample_index = NULL;
510 prod->n_row = tab1->n_row + tab2->n_row;
511 prod->n_con = tab1->n_con + tab2->n_con;
512 prod->n_eq = 0;
513 prod->max_con = tab1->max_con + tab2->max_con;
514 prod->n_col = tab1->n_col + tab2->n_col;
515 prod->n_var = tab1->n_var + tab2->n_var;
516 prod->max_var = tab1->max_var + tab2->max_var;
517 prod->n_param = 0;
518 prod->n_div = 0;
519 prod->n_dead = tab1->n_dead + tab2->n_dead;
520 prod->n_redundant = tab1->n_redundant + tab2->n_redundant;
521 prod->rational = tab1->rational;
522 prod->empty = tab1->empty || tab2->empty;
523 prod->strict_redundant = tab1->strict_redundant || tab2->strict_redundant;
524 prod->need_undo = 0;
525 prod->in_undo = 0;
526 prod->M = tab1->M;
527 prod->cone = tab1->cone;
528 prod->bottom.type = isl_tab_undo_bottom;
529 prod->bottom.next = NULL;
530 prod->top = &prod->bottom;
532 prod->n_zero = 0;
533 prod->n_unbounded = 0;
534 prod->basis = NULL;
536 return prod;
537 error:
538 isl_tab_free(prod);
539 return NULL;
542 static struct isl_tab_var *var_from_index(struct isl_tab *tab, int i)
544 if (i >= 0)
545 return &tab->var[i];
546 else
547 return &tab->con[~i];
550 struct isl_tab_var *isl_tab_var_from_row(struct isl_tab *tab, int i)
552 return var_from_index(tab, tab->row_var[i]);
555 static struct isl_tab_var *var_from_col(struct isl_tab *tab, int i)
557 return var_from_index(tab, tab->col_var[i]);
560 /* Check if there are any upper bounds on column variable "var",
561 * i.e., non-negative rows where var appears with a negative coefficient.
562 * Return 1 if there are no such bounds.
564 static int max_is_manifestly_unbounded(struct isl_tab *tab,
565 struct isl_tab_var *var)
567 int i;
568 unsigned off = 2 + tab->M;
570 if (var->is_row)
571 return 0;
572 for (i = tab->n_redundant; i < tab->n_row; ++i) {
573 if (!isl_int_is_neg(tab->mat->row[i][off + var->index]))
574 continue;
575 if (isl_tab_var_from_row(tab, i)->is_nonneg)
576 return 0;
578 return 1;
581 /* Check if there are any lower bounds on column variable "var",
582 * i.e., non-negative rows where var appears with a positive coefficient.
583 * Return 1 if there are no such bounds.
585 static int min_is_manifestly_unbounded(struct isl_tab *tab,
586 struct isl_tab_var *var)
588 int i;
589 unsigned off = 2 + tab->M;
591 if (var->is_row)
592 return 0;
593 for (i = tab->n_redundant; i < tab->n_row; ++i) {
594 if (!isl_int_is_pos(tab->mat->row[i][off + var->index]))
595 continue;
596 if (isl_tab_var_from_row(tab, i)->is_nonneg)
597 return 0;
599 return 1;
602 static int row_cmp(struct isl_tab *tab, int r1, int r2, int c, isl_int t)
604 unsigned off = 2 + tab->M;
606 if (tab->M) {
607 int s;
608 isl_int_mul(t, tab->mat->row[r1][2], tab->mat->row[r2][off+c]);
609 isl_int_submul(t, tab->mat->row[r2][2], tab->mat->row[r1][off+c]);
610 s = isl_int_sgn(t);
611 if (s)
612 return s;
614 isl_int_mul(t, tab->mat->row[r1][1], tab->mat->row[r2][off + c]);
615 isl_int_submul(t, tab->mat->row[r2][1], tab->mat->row[r1][off + c]);
616 return isl_int_sgn(t);
619 /* Given the index of a column "c", return the index of a row
620 * that can be used to pivot the column in, with either an increase
621 * (sgn > 0) or a decrease (sgn < 0) of the corresponding variable.
622 * If "var" is not NULL, then the row returned will be different from
623 * the one associated with "var".
625 * Each row in the tableau is of the form
627 * x_r = a_r0 + \sum_i a_ri x_i
629 * Only rows with x_r >= 0 and with the sign of a_ri opposite to "sgn"
630 * impose any limit on the increase or decrease in the value of x_c
631 * and this bound is equal to a_r0 / |a_rc|. We are therefore looking
632 * for the row with the smallest (most stringent) such bound.
633 * Note that the common denominator of each row drops out of the fraction.
634 * To check if row j has a smaller bound than row r, i.e.,
635 * a_j0 / |a_jc| < a_r0 / |a_rc| or a_j0 |a_rc| < a_r0 |a_jc|,
636 * we check if -sign(a_jc) (a_j0 a_rc - a_r0 a_jc) < 0,
637 * where -sign(a_jc) is equal to "sgn".
639 static int pivot_row(struct isl_tab *tab,
640 struct isl_tab_var *var, int sgn, int c)
642 int j, r, tsgn;
643 isl_int t;
644 unsigned off = 2 + tab->M;
646 isl_int_init(t);
647 r = -1;
648 for (j = tab->n_redundant; j < tab->n_row; ++j) {
649 if (var && j == var->index)
650 continue;
651 if (!isl_tab_var_from_row(tab, j)->is_nonneg)
652 continue;
653 if (sgn * isl_int_sgn(tab->mat->row[j][off + c]) >= 0)
654 continue;
655 if (r < 0) {
656 r = j;
657 continue;
659 tsgn = sgn * row_cmp(tab, r, j, c, t);
660 if (tsgn < 0 || (tsgn == 0 &&
661 tab->row_var[j] < tab->row_var[r]))
662 r = j;
664 isl_int_clear(t);
665 return r;
668 /* Find a pivot (row and col) that will increase (sgn > 0) or decrease
669 * (sgn < 0) the value of row variable var.
670 * If not NULL, then skip_var is a row variable that should be ignored
671 * while looking for a pivot row. It is usually equal to var.
673 * As the given row in the tableau is of the form
675 * x_r = a_r0 + \sum_i a_ri x_i
677 * we need to find a column such that the sign of a_ri is equal to "sgn"
678 * (such that an increase in x_i will have the desired effect) or a
679 * column with a variable that may attain negative values.
680 * If a_ri is positive, then we need to move x_i in the same direction
681 * to obtain the desired effect. Otherwise, x_i has to move in the
682 * opposite direction.
684 static void find_pivot(struct isl_tab *tab,
685 struct isl_tab_var *var, struct isl_tab_var *skip_var,
686 int sgn, int *row, int *col)
688 int j, r, c;
689 isl_int *tr;
691 *row = *col = -1;
693 isl_assert(tab->mat->ctx, var->is_row, return);
694 tr = tab->mat->row[var->index] + 2 + tab->M;
696 c = -1;
697 for (j = tab->n_dead; j < tab->n_col; ++j) {
698 if (isl_int_is_zero(tr[j]))
699 continue;
700 if (isl_int_sgn(tr[j]) != sgn &&
701 var_from_col(tab, j)->is_nonneg)
702 continue;
703 if (c < 0 || tab->col_var[j] < tab->col_var[c])
704 c = j;
706 if (c < 0)
707 return;
709 sgn *= isl_int_sgn(tr[c]);
710 r = pivot_row(tab, skip_var, sgn, c);
711 *row = r < 0 ? var->index : r;
712 *col = c;
715 /* Return 1 if row "row" represents an obviously redundant inequality.
716 * This means
717 * - it represents an inequality or a variable
718 * - that is the sum of a non-negative sample value and a positive
719 * combination of zero or more non-negative constraints.
721 int isl_tab_row_is_redundant(struct isl_tab *tab, int row)
723 int i;
724 unsigned off = 2 + tab->M;
726 if (tab->row_var[row] < 0 && !isl_tab_var_from_row(tab, row)->is_nonneg)
727 return 0;
729 if (isl_int_is_neg(tab->mat->row[row][1]))
730 return 0;
731 if (tab->strict_redundant && isl_int_is_zero(tab->mat->row[row][1]))
732 return 0;
733 if (tab->M && isl_int_is_neg(tab->mat->row[row][2]))
734 return 0;
736 for (i = tab->n_dead; i < tab->n_col; ++i) {
737 if (isl_int_is_zero(tab->mat->row[row][off + i]))
738 continue;
739 if (tab->col_var[i] >= 0)
740 return 0;
741 if (isl_int_is_neg(tab->mat->row[row][off + i]))
742 return 0;
743 if (!var_from_col(tab, i)->is_nonneg)
744 return 0;
746 return 1;
749 static void swap_rows(struct isl_tab *tab, int row1, int row2)
751 int t;
752 enum isl_tab_row_sign s;
754 t = tab->row_var[row1];
755 tab->row_var[row1] = tab->row_var[row2];
756 tab->row_var[row2] = t;
757 isl_tab_var_from_row(tab, row1)->index = row1;
758 isl_tab_var_from_row(tab, row2)->index = row2;
759 tab->mat = isl_mat_swap_rows(tab->mat, row1, row2);
761 if (!tab->row_sign)
762 return;
763 s = tab->row_sign[row1];
764 tab->row_sign[row1] = tab->row_sign[row2];
765 tab->row_sign[row2] = s;
768 static int push_union(struct isl_tab *tab,
769 enum isl_tab_undo_type type, union isl_tab_undo_val u) WARN_UNUSED;
770 static int push_union(struct isl_tab *tab,
771 enum isl_tab_undo_type type, union isl_tab_undo_val u)
773 struct isl_tab_undo *undo;
775 if (!tab->need_undo)
776 return 0;
778 undo = isl_alloc_type(tab->mat->ctx, struct isl_tab_undo);
779 if (!undo)
780 return -1;
781 undo->type = type;
782 undo->u = u;
783 undo->next = tab->top;
784 tab->top = undo;
786 return 0;
789 int isl_tab_push_var(struct isl_tab *tab,
790 enum isl_tab_undo_type type, struct isl_tab_var *var)
792 union isl_tab_undo_val u;
793 if (var->is_row)
794 u.var_index = tab->row_var[var->index];
795 else
796 u.var_index = tab->col_var[var->index];
797 return push_union(tab, type, u);
800 int isl_tab_push(struct isl_tab *tab, enum isl_tab_undo_type type)
802 union isl_tab_undo_val u = { 0 };
803 return push_union(tab, type, u);
806 /* Push a record on the undo stack describing the current basic
807 * variables, so that the this state can be restored during rollback.
809 int isl_tab_push_basis(struct isl_tab *tab)
811 int i;
812 union isl_tab_undo_val u;
814 u.col_var = isl_alloc_array(tab->mat->ctx, int, tab->n_col);
815 if (!u.col_var)
816 return -1;
817 for (i = 0; i < tab->n_col; ++i)
818 u.col_var[i] = tab->col_var[i];
819 return push_union(tab, isl_tab_undo_saved_basis, u);
822 int isl_tab_push_callback(struct isl_tab *tab, struct isl_tab_callback *callback)
824 union isl_tab_undo_val u;
825 u.callback = callback;
826 return push_union(tab, isl_tab_undo_callback, u);
829 struct isl_tab *isl_tab_init_samples(struct isl_tab *tab)
831 if (!tab)
832 return NULL;
834 tab->n_sample = 0;
835 tab->n_outside = 0;
836 tab->samples = isl_mat_alloc(tab->mat->ctx, 1, 1 + tab->n_var);
837 if (!tab->samples)
838 goto error;
839 tab->sample_index = isl_alloc_array(tab->mat->ctx, int, 1);
840 if (!tab->sample_index)
841 goto error;
842 return tab;
843 error:
844 isl_tab_free(tab);
845 return NULL;
848 struct isl_tab *isl_tab_add_sample(struct isl_tab *tab,
849 __isl_take isl_vec *sample)
851 if (!tab || !sample)
852 goto error;
854 if (tab->n_sample + 1 > tab->samples->n_row) {
855 int *t = isl_realloc_array(tab->mat->ctx,
856 tab->sample_index, int, tab->n_sample + 1);
857 if (!t)
858 goto error;
859 tab->sample_index = t;
862 tab->samples = isl_mat_extend(tab->samples,
863 tab->n_sample + 1, tab->samples->n_col);
864 if (!tab->samples)
865 goto error;
867 isl_seq_cpy(tab->samples->row[tab->n_sample], sample->el, sample->size);
868 isl_vec_free(sample);
869 tab->sample_index[tab->n_sample] = tab->n_sample;
870 tab->n_sample++;
872 return tab;
873 error:
874 isl_vec_free(sample);
875 isl_tab_free(tab);
876 return NULL;
879 struct isl_tab *isl_tab_drop_sample(struct isl_tab *tab, int s)
881 if (s != tab->n_outside) {
882 int t = tab->sample_index[tab->n_outside];
883 tab->sample_index[tab->n_outside] = tab->sample_index[s];
884 tab->sample_index[s] = t;
885 isl_mat_swap_rows(tab->samples, tab->n_outside, s);
887 tab->n_outside++;
888 if (isl_tab_push(tab, isl_tab_undo_drop_sample) < 0) {
889 isl_tab_free(tab);
890 return NULL;
893 return tab;
896 /* Record the current number of samples so that we can remove newer
897 * samples during a rollback.
899 int isl_tab_save_samples(struct isl_tab *tab)
901 union isl_tab_undo_val u;
903 if (!tab)
904 return -1;
906 u.n = tab->n_sample;
907 return push_union(tab, isl_tab_undo_saved_samples, u);
910 /* Mark row with index "row" as being redundant.
911 * If we may need to undo the operation or if the row represents
912 * a variable of the original problem, the row is kept,
913 * but no longer considered when looking for a pivot row.
914 * Otherwise, the row is simply removed.
916 * The row may be interchanged with some other row. If it
917 * is interchanged with a later row, return 1. Otherwise return 0.
918 * If the rows are checked in order in the calling function,
919 * then a return value of 1 means that the row with the given
920 * row number may now contain a different row that hasn't been checked yet.
922 int isl_tab_mark_redundant(struct isl_tab *tab, int row)
924 struct isl_tab_var *var = isl_tab_var_from_row(tab, row);
925 var->is_redundant = 1;
926 isl_assert(tab->mat->ctx, row >= tab->n_redundant, return -1);
927 if (tab->need_undo || tab->row_var[row] >= 0) {
928 if (tab->row_var[row] >= 0 && !var->is_nonneg) {
929 var->is_nonneg = 1;
930 if (isl_tab_push_var(tab, isl_tab_undo_nonneg, var) < 0)
931 return -1;
933 if (row != tab->n_redundant)
934 swap_rows(tab, row, tab->n_redundant);
935 tab->n_redundant++;
936 return isl_tab_push_var(tab, isl_tab_undo_redundant, var);
937 } else {
938 if (row != tab->n_row - 1)
939 swap_rows(tab, row, tab->n_row - 1);
940 isl_tab_var_from_row(tab, tab->n_row - 1)->index = -1;
941 tab->n_row--;
942 return 1;
946 int isl_tab_mark_empty(struct isl_tab *tab)
948 if (!tab)
949 return -1;
950 if (!tab->empty && tab->need_undo)
951 if (isl_tab_push(tab, isl_tab_undo_empty) < 0)
952 return -1;
953 tab->empty = 1;
954 return 0;
957 int isl_tab_freeze_constraint(struct isl_tab *tab, int con)
959 struct isl_tab_var *var;
961 if (!tab)
962 return -1;
964 var = &tab->con[con];
965 if (var->frozen)
966 return 0;
967 if (var->index < 0)
968 return 0;
969 var->frozen = 1;
971 if (tab->need_undo)
972 return isl_tab_push_var(tab, isl_tab_undo_freeze, var);
974 return 0;
977 /* Update the rows signs after a pivot of "row" and "col", with "row_sgn"
978 * the original sign of the pivot element.
979 * We only keep track of row signs during PILP solving and in this case
980 * we only pivot a row with negative sign (meaning the value is always
981 * non-positive) using a positive pivot element.
983 * For each row j, the new value of the parametric constant is equal to
985 * a_j0 - a_jc a_r0/a_rc
987 * where a_j0 is the original parametric constant, a_rc is the pivot element,
988 * a_r0 is the parametric constant of the pivot row and a_jc is the
989 * pivot column entry of the row j.
990 * Since a_r0 is non-positive and a_rc is positive, the sign of row j
991 * remains the same if a_jc has the same sign as the row j or if
992 * a_jc is zero. In all other cases, we reset the sign to "unknown".
994 static void update_row_sign(struct isl_tab *tab, int row, int col, int row_sgn)
996 int i;
997 struct isl_mat *mat = tab->mat;
998 unsigned off = 2 + tab->M;
1000 if (!tab->row_sign)
1001 return;
1003 if (tab->row_sign[row] == 0)
1004 return;
1005 isl_assert(mat->ctx, row_sgn > 0, return);
1006 isl_assert(mat->ctx, tab->row_sign[row] == isl_tab_row_neg, return);
1007 tab->row_sign[row] = isl_tab_row_pos;
1008 for (i = 0; i < tab->n_row; ++i) {
1009 int s;
1010 if (i == row)
1011 continue;
1012 s = isl_int_sgn(mat->row[i][off + col]);
1013 if (!s)
1014 continue;
1015 if (!tab->row_sign[i])
1016 continue;
1017 if (s < 0 && tab->row_sign[i] == isl_tab_row_neg)
1018 continue;
1019 if (s > 0 && tab->row_sign[i] == isl_tab_row_pos)
1020 continue;
1021 tab->row_sign[i] = isl_tab_row_unknown;
1025 /* Given a row number "row" and a column number "col", pivot the tableau
1026 * such that the associated variables are interchanged.
1027 * The given row in the tableau expresses
1029 * x_r = a_r0 + \sum_i a_ri x_i
1031 * or
1033 * x_c = 1/a_rc x_r - a_r0/a_rc + sum_{i \ne r} -a_ri/a_rc
1035 * Substituting this equality into the other rows
1037 * x_j = a_j0 + \sum_i a_ji x_i
1039 * with a_jc \ne 0, we obtain
1041 * 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
1043 * The tableau
1045 * n_rc/d_r n_ri/d_r
1046 * n_jc/d_j n_ji/d_j
1048 * where i is any other column and j is any other row,
1049 * is therefore transformed into
1051 * s(n_rc)d_r/|n_rc| -s(n_rc)n_ri/|n_rc|
1052 * 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)
1054 * The transformation is performed along the following steps
1056 * d_r/n_rc n_ri/n_rc
1057 * n_jc/d_j n_ji/d_j
1059 * s(n_rc)d_r/|n_rc| -s(n_rc)n_ri/|n_rc|
1060 * n_jc/d_j n_ji/d_j
1062 * s(n_rc)d_r/|n_rc| -s(n_rc)n_ri/|n_rc|
1063 * n_jc/(|n_rc| d_j) n_ji/(|n_rc| d_j)
1065 * s(n_rc)d_r/|n_rc| -s(n_rc)n_ri/|n_rc|
1066 * n_jc/(|n_rc| d_j) (n_ji |n_rc|)/(|n_rc| d_j)
1068 * s(n_rc)d_r/|n_rc| -s(n_rc)n_ri/|n_rc|
1069 * n_jc/(|n_rc| d_j) (n_ji |n_rc| - s(n_rc)n_jc n_ri)/(|n_rc| d_j)
1071 * s(n_rc)d_r/|n_rc| -s(n_rc)n_ri/|n_rc|
1072 * 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)
1075 int isl_tab_pivot(struct isl_tab *tab, int row, int col)
1077 int i, j;
1078 int sgn;
1079 int t;
1080 struct isl_mat *mat = tab->mat;
1081 struct isl_tab_var *var;
1082 unsigned off = 2 + tab->M;
1084 isl_int_swap(mat->row[row][0], mat->row[row][off + col]);
1085 sgn = isl_int_sgn(mat->row[row][0]);
1086 if (sgn < 0) {
1087 isl_int_neg(mat->row[row][0], mat->row[row][0]);
1088 isl_int_neg(mat->row[row][off + col], mat->row[row][off + col]);
1089 } else
1090 for (j = 0; j < off - 1 + tab->n_col; ++j) {
1091 if (j == off - 1 + col)
1092 continue;
1093 isl_int_neg(mat->row[row][1 + j], mat->row[row][1 + j]);
1095 if (!isl_int_is_one(mat->row[row][0]))
1096 isl_seq_normalize(mat->ctx, mat->row[row], off + tab->n_col);
1097 for (i = 0; i < tab->n_row; ++i) {
1098 if (i == row)
1099 continue;
1100 if (isl_int_is_zero(mat->row[i][off + col]))
1101 continue;
1102 isl_int_mul(mat->row[i][0], mat->row[i][0], mat->row[row][0]);
1103 for (j = 0; j < off - 1 + tab->n_col; ++j) {
1104 if (j == off - 1 + col)
1105 continue;
1106 isl_int_mul(mat->row[i][1 + j],
1107 mat->row[i][1 + j], mat->row[row][0]);
1108 isl_int_addmul(mat->row[i][1 + j],
1109 mat->row[i][off + col], mat->row[row][1 + j]);
1111 isl_int_mul(mat->row[i][off + col],
1112 mat->row[i][off + col], mat->row[row][off + col]);
1113 if (!isl_int_is_one(mat->row[i][0]))
1114 isl_seq_normalize(mat->ctx, mat->row[i], off + tab->n_col);
1116 t = tab->row_var[row];
1117 tab->row_var[row] = tab->col_var[col];
1118 tab->col_var[col] = t;
1119 var = isl_tab_var_from_row(tab, row);
1120 var->is_row = 1;
1121 var->index = row;
1122 var = var_from_col(tab, col);
1123 var->is_row = 0;
1124 var->index = col;
1125 update_row_sign(tab, row, col, sgn);
1126 if (tab->in_undo)
1127 return 0;
1128 for (i = tab->n_redundant; i < tab->n_row; ++i) {
1129 if (isl_int_is_zero(mat->row[i][off + col]))
1130 continue;
1131 if (!isl_tab_var_from_row(tab, i)->frozen &&
1132 isl_tab_row_is_redundant(tab, i)) {
1133 int redo = isl_tab_mark_redundant(tab, i);
1134 if (redo < 0)
1135 return -1;
1136 if (redo)
1137 --i;
1140 return 0;
1143 /* If "var" represents a column variable, then pivot is up (sgn > 0)
1144 * or down (sgn < 0) to a row. The variable is assumed not to be
1145 * unbounded in the specified direction.
1146 * If sgn = 0, then the variable is unbounded in both directions,
1147 * and we pivot with any row we can find.
1149 static int to_row(struct isl_tab *tab, struct isl_tab_var *var, int sign) WARN_UNUSED;
1150 static int to_row(struct isl_tab *tab, struct isl_tab_var *var, int sign)
1152 int r;
1153 unsigned off = 2 + tab->M;
1155 if (var->is_row)
1156 return 0;
1158 if (sign == 0) {
1159 for (r = tab->n_redundant; r < tab->n_row; ++r)
1160 if (!isl_int_is_zero(tab->mat->row[r][off+var->index]))
1161 break;
1162 isl_assert(tab->mat->ctx, r < tab->n_row, return -1);
1163 } else {
1164 r = pivot_row(tab, NULL, sign, var->index);
1165 isl_assert(tab->mat->ctx, r >= 0, return -1);
1168 return isl_tab_pivot(tab, r, var->index);
1171 static void check_table(struct isl_tab *tab)
1173 int i;
1175 if (tab->empty)
1176 return;
1177 for (i = tab->n_redundant; i < tab->n_row; ++i) {
1178 struct isl_tab_var *var;
1179 var = isl_tab_var_from_row(tab, i);
1180 if (!var->is_nonneg)
1181 continue;
1182 if (tab->M) {
1183 isl_assert(tab->mat->ctx,
1184 !isl_int_is_neg(tab->mat->row[i][2]), abort());
1185 if (isl_int_is_pos(tab->mat->row[i][2]))
1186 continue;
1188 isl_assert(tab->mat->ctx, !isl_int_is_neg(tab->mat->row[i][1]),
1189 abort());
1193 /* Return the sign of the maximal value of "var".
1194 * If the sign is not negative, then on return from this function,
1195 * the sample value will also be non-negative.
1197 * If "var" is manifestly unbounded wrt positive values, we are done.
1198 * Otherwise, we pivot the variable up to a row if needed
1199 * Then we continue pivoting down until either
1200 * - no more down pivots can be performed
1201 * - the sample value is positive
1202 * - the variable is pivoted into a manifestly unbounded column
1204 static int sign_of_max(struct isl_tab *tab, struct isl_tab_var *var)
1206 int row, col;
1208 if (max_is_manifestly_unbounded(tab, var))
1209 return 1;
1210 if (to_row(tab, var, 1) < 0)
1211 return -2;
1212 while (!isl_int_is_pos(tab->mat->row[var->index][1])) {
1213 find_pivot(tab, var, var, 1, &row, &col);
1214 if (row == -1)
1215 return isl_int_sgn(tab->mat->row[var->index][1]);
1216 if (isl_tab_pivot(tab, row, col) < 0)
1217 return -2;
1218 if (!var->is_row) /* manifestly unbounded */
1219 return 1;
1221 return 1;
1224 int isl_tab_sign_of_max(struct isl_tab *tab, int con)
1226 struct isl_tab_var *var;
1228 if (!tab)
1229 return -2;
1231 var = &tab->con[con];
1232 isl_assert(tab->mat->ctx, !var->is_redundant, return -2);
1233 isl_assert(tab->mat->ctx, !var->is_zero, return -2);
1235 return sign_of_max(tab, var);
1238 static int row_is_neg(struct isl_tab *tab, int row)
1240 if (!tab->M)
1241 return isl_int_is_neg(tab->mat->row[row][1]);
1242 if (isl_int_is_pos(tab->mat->row[row][2]))
1243 return 0;
1244 if (isl_int_is_neg(tab->mat->row[row][2]))
1245 return 1;
1246 return isl_int_is_neg(tab->mat->row[row][1]);
1249 static int row_sgn(struct isl_tab *tab, int row)
1251 if (!tab->M)
1252 return isl_int_sgn(tab->mat->row[row][1]);
1253 if (!isl_int_is_zero(tab->mat->row[row][2]))
1254 return isl_int_sgn(tab->mat->row[row][2]);
1255 else
1256 return isl_int_sgn(tab->mat->row[row][1]);
1259 /* Perform pivots until the row variable "var" has a non-negative
1260 * sample value or until no more upward pivots can be performed.
1261 * Return the sign of the sample value after the pivots have been
1262 * performed.
1264 static int restore_row(struct isl_tab *tab, struct isl_tab_var *var)
1266 int row, col;
1268 while (row_is_neg(tab, var->index)) {
1269 find_pivot(tab, var, var, 1, &row, &col);
1270 if (row == -1)
1271 break;
1272 if (isl_tab_pivot(tab, row, col) < 0)
1273 return -2;
1274 if (!var->is_row) /* manifestly unbounded */
1275 return 1;
1277 return row_sgn(tab, var->index);
1280 /* Perform pivots until we are sure that the row variable "var"
1281 * can attain non-negative values. After return from this
1282 * function, "var" is still a row variable, but its sample
1283 * value may not be non-negative, even if the function returns 1.
1285 static int at_least_zero(struct isl_tab *tab, struct isl_tab_var *var)
1287 int row, col;
1289 while (isl_int_is_neg(tab->mat->row[var->index][1])) {
1290 find_pivot(tab, var, var, 1, &row, &col);
1291 if (row == -1)
1292 break;
1293 if (row == var->index) /* manifestly unbounded */
1294 return 1;
1295 if (isl_tab_pivot(tab, row, col) < 0)
1296 return -1;
1298 return !isl_int_is_neg(tab->mat->row[var->index][1]);
1301 /* Return a negative value if "var" can attain negative values.
1302 * Return a non-negative value otherwise.
1304 * If "var" is manifestly unbounded wrt negative values, we are done.
1305 * Otherwise, if var is in a column, we can pivot it down to a row.
1306 * Then we continue pivoting down until either
1307 * - the pivot would result in a manifestly unbounded column
1308 * => we don't perform the pivot, but simply return -1
1309 * - no more down pivots can be performed
1310 * - the sample value is negative
1311 * If the sample value becomes negative and the variable is supposed
1312 * to be nonnegative, then we undo the last pivot.
1313 * However, if the last pivot has made the pivoting variable
1314 * obviously redundant, then it may have moved to another row.
1315 * In that case we look for upward pivots until we reach a non-negative
1316 * value again.
1318 static int sign_of_min(struct isl_tab *tab, struct isl_tab_var *var)
1320 int row, col;
1321 struct isl_tab_var *pivot_var = NULL;
1323 if (min_is_manifestly_unbounded(tab, var))
1324 return -1;
1325 if (!var->is_row) {
1326 col = var->index;
1327 row = pivot_row(tab, NULL, -1, col);
1328 pivot_var = var_from_col(tab, col);
1329 if (isl_tab_pivot(tab, row, col) < 0)
1330 return -2;
1331 if (var->is_redundant)
1332 return 0;
1333 if (isl_int_is_neg(tab->mat->row[var->index][1])) {
1334 if (var->is_nonneg) {
1335 if (!pivot_var->is_redundant &&
1336 pivot_var->index == row) {
1337 if (isl_tab_pivot(tab, row, col) < 0)
1338 return -2;
1339 } else
1340 if (restore_row(tab, var) < -1)
1341 return -2;
1343 return -1;
1346 if (var->is_redundant)
1347 return 0;
1348 while (!isl_int_is_neg(tab->mat->row[var->index][1])) {
1349 find_pivot(tab, var, var, -1, &row, &col);
1350 if (row == var->index)
1351 return -1;
1352 if (row == -1)
1353 return isl_int_sgn(tab->mat->row[var->index][1]);
1354 pivot_var = var_from_col(tab, col);
1355 if (isl_tab_pivot(tab, row, col) < 0)
1356 return -2;
1357 if (var->is_redundant)
1358 return 0;
1360 if (pivot_var && var->is_nonneg) {
1361 /* pivot back to non-negative value */
1362 if (!pivot_var->is_redundant && pivot_var->index == row) {
1363 if (isl_tab_pivot(tab, row, col) < 0)
1364 return -2;
1365 } else
1366 if (restore_row(tab, var) < -1)
1367 return -2;
1369 return -1;
1372 static int row_at_most_neg_one(struct isl_tab *tab, int row)
1374 if (tab->M) {
1375 if (isl_int_is_pos(tab->mat->row[row][2]))
1376 return 0;
1377 if (isl_int_is_neg(tab->mat->row[row][2]))
1378 return 1;
1380 return isl_int_is_neg(tab->mat->row[row][1]) &&
1381 isl_int_abs_ge(tab->mat->row[row][1],
1382 tab->mat->row[row][0]);
1385 /* Return 1 if "var" can attain values <= -1.
1386 * Return 0 otherwise.
1388 * The sample value of "var" is assumed to be non-negative when the
1389 * the function is called. If 1 is returned then the constraint
1390 * is not redundant and the sample value is made non-negative again before
1391 * the function returns.
1393 int isl_tab_min_at_most_neg_one(struct isl_tab *tab, struct isl_tab_var *var)
1395 int row, col;
1396 struct isl_tab_var *pivot_var;
1398 if (min_is_manifestly_unbounded(tab, var))
1399 return 1;
1400 if (!var->is_row) {
1401 col = var->index;
1402 row = pivot_row(tab, NULL, -1, col);
1403 pivot_var = var_from_col(tab, col);
1404 if (isl_tab_pivot(tab, row, col) < 0)
1405 return -1;
1406 if (var->is_redundant)
1407 return 0;
1408 if (row_at_most_neg_one(tab, var->index)) {
1409 if (var->is_nonneg) {
1410 if (!pivot_var->is_redundant &&
1411 pivot_var->index == row) {
1412 if (isl_tab_pivot(tab, row, col) < 0)
1413 return -1;
1414 } else
1415 if (restore_row(tab, var) < -1)
1416 return -1;
1418 return 1;
1421 if (var->is_redundant)
1422 return 0;
1423 do {
1424 find_pivot(tab, var, var, -1, &row, &col);
1425 if (row == var->index) {
1426 if (restore_row(tab, var) < -1)
1427 return -1;
1428 return 1;
1430 if (row == -1)
1431 return 0;
1432 pivot_var = var_from_col(tab, col);
1433 if (isl_tab_pivot(tab, row, col) < 0)
1434 return -1;
1435 if (var->is_redundant)
1436 return 0;
1437 } while (!row_at_most_neg_one(tab, var->index));
1438 if (var->is_nonneg) {
1439 /* pivot back to non-negative value */
1440 if (!pivot_var->is_redundant && pivot_var->index == row)
1441 if (isl_tab_pivot(tab, row, col) < 0)
1442 return -1;
1443 if (restore_row(tab, var) < -1)
1444 return -1;
1446 return 1;
1449 /* Return 1 if "var" can attain values >= 1.
1450 * Return 0 otherwise.
1452 static int at_least_one(struct isl_tab *tab, struct isl_tab_var *var)
1454 int row, col;
1455 isl_int *r;
1457 if (max_is_manifestly_unbounded(tab, var))
1458 return 1;
1459 if (to_row(tab, var, 1) < 0)
1460 return -1;
1461 r = tab->mat->row[var->index];
1462 while (isl_int_lt(r[1], r[0])) {
1463 find_pivot(tab, var, var, 1, &row, &col);
1464 if (row == -1)
1465 return isl_int_ge(r[1], r[0]);
1466 if (row == var->index) /* manifestly unbounded */
1467 return 1;
1468 if (isl_tab_pivot(tab, row, col) < 0)
1469 return -1;
1471 return 1;
1474 static void swap_cols(struct isl_tab *tab, int col1, int col2)
1476 int t;
1477 unsigned off = 2 + tab->M;
1478 t = tab->col_var[col1];
1479 tab->col_var[col1] = tab->col_var[col2];
1480 tab->col_var[col2] = t;
1481 var_from_col(tab, col1)->index = col1;
1482 var_from_col(tab, col2)->index = col2;
1483 tab->mat = isl_mat_swap_cols(tab->mat, off + col1, off + col2);
1486 /* Mark column with index "col" as representing a zero variable.
1487 * If we may need to undo the operation the column is kept,
1488 * but no longer considered.
1489 * Otherwise, the column is simply removed.
1491 * The column may be interchanged with some other column. If it
1492 * is interchanged with a later column, return 1. Otherwise return 0.
1493 * If the columns are checked in order in the calling function,
1494 * then a return value of 1 means that the column with the given
1495 * column number may now contain a different column that
1496 * hasn't been checked yet.
1498 int isl_tab_kill_col(struct isl_tab *tab, int col)
1500 var_from_col(tab, col)->is_zero = 1;
1501 if (tab->need_undo) {
1502 if (isl_tab_push_var(tab, isl_tab_undo_zero,
1503 var_from_col(tab, col)) < 0)
1504 return -1;
1505 if (col != tab->n_dead)
1506 swap_cols(tab, col, tab->n_dead);
1507 tab->n_dead++;
1508 return 0;
1509 } else {
1510 if (col != tab->n_col - 1)
1511 swap_cols(tab, col, tab->n_col - 1);
1512 var_from_col(tab, tab->n_col - 1)->index = -1;
1513 tab->n_col--;
1514 return 1;
1518 /* Row variable "var" is non-negative and cannot attain any values
1519 * larger than zero. This means that the coefficients of the unrestricted
1520 * column variables are zero and that the coefficients of the non-negative
1521 * column variables are zero or negative.
1522 * Each of the non-negative variables with a negative coefficient can
1523 * then also be written as the negative sum of non-negative variables
1524 * and must therefore also be zero.
1526 static int close_row(struct isl_tab *tab, struct isl_tab_var *var) WARN_UNUSED;
1527 static int close_row(struct isl_tab *tab, struct isl_tab_var *var)
1529 int j;
1530 struct isl_mat *mat = tab->mat;
1531 unsigned off = 2 + tab->M;
1533 isl_assert(tab->mat->ctx, var->is_nonneg, return -1);
1534 var->is_zero = 1;
1535 if (tab->need_undo)
1536 if (isl_tab_push_var(tab, isl_tab_undo_zero, var) < 0)
1537 return -1;
1538 for (j = tab->n_dead; j < tab->n_col; ++j) {
1539 int recheck;
1540 if (isl_int_is_zero(mat->row[var->index][off + j]))
1541 continue;
1542 isl_assert(tab->mat->ctx,
1543 isl_int_is_neg(mat->row[var->index][off + j]), return -1);
1544 recheck = isl_tab_kill_col(tab, j);
1545 if (recheck < 0)
1546 return -1;
1547 if (recheck)
1548 --j;
1550 if (isl_tab_mark_redundant(tab, var->index) < 0)
1551 return -1;
1552 return 0;
1555 /* Add a constraint to the tableau and allocate a row for it.
1556 * Return the index into the constraint array "con".
1558 int isl_tab_allocate_con(struct isl_tab *tab)
1560 int r;
1562 isl_assert(tab->mat->ctx, tab->n_row < tab->mat->n_row, return -1);
1563 isl_assert(tab->mat->ctx, tab->n_con < tab->max_con, return -1);
1565 r = tab->n_con;
1566 tab->con[r].index = tab->n_row;
1567 tab->con[r].is_row = 1;
1568 tab->con[r].is_nonneg = 0;
1569 tab->con[r].is_zero = 0;
1570 tab->con[r].is_redundant = 0;
1571 tab->con[r].frozen = 0;
1572 tab->con[r].negated = 0;
1573 tab->row_var[tab->n_row] = ~r;
1575 tab->n_row++;
1576 tab->n_con++;
1577 if (isl_tab_push_var(tab, isl_tab_undo_allocate, &tab->con[r]) < 0)
1578 return -1;
1580 return r;
1583 /* Add a variable to the tableau and allocate a column for it.
1584 * Return the index into the variable array "var".
1586 int isl_tab_allocate_var(struct isl_tab *tab)
1588 int r;
1589 int i;
1590 unsigned off = 2 + tab->M;
1592 isl_assert(tab->mat->ctx, tab->n_col < tab->mat->n_col, return -1);
1593 isl_assert(tab->mat->ctx, tab->n_var < tab->max_var, return -1);
1595 r = tab->n_var;
1596 tab->var[r].index = tab->n_col;
1597 tab->var[r].is_row = 0;
1598 tab->var[r].is_nonneg = 0;
1599 tab->var[r].is_zero = 0;
1600 tab->var[r].is_redundant = 0;
1601 tab->var[r].frozen = 0;
1602 tab->var[r].negated = 0;
1603 tab->col_var[tab->n_col] = r;
1605 for (i = 0; i < tab->n_row; ++i)
1606 isl_int_set_si(tab->mat->row[i][off + tab->n_col], 0);
1608 tab->n_var++;
1609 tab->n_col++;
1610 if (isl_tab_push_var(tab, isl_tab_undo_allocate, &tab->var[r]) < 0)
1611 return -1;
1613 return r;
1616 /* Add a row to the tableau. The row is given as an affine combination
1617 * of the original variables and needs to be expressed in terms of the
1618 * column variables.
1620 * We add each term in turn.
1621 * If r = n/d_r is the current sum and we need to add k x, then
1622 * if x is a column variable, we increase the numerator of
1623 * this column by k d_r
1624 * if x = f/d_x is a row variable, then the new representation of r is
1626 * n k f d_x/g n + d_r/g k f m/d_r n + m/d_g k f
1627 * --- + --- = ------------------- = -------------------
1628 * d_r d_r d_r d_x/g m
1630 * with g the gcd of d_r and d_x and m the lcm of d_r and d_x.
1632 int isl_tab_add_row(struct isl_tab *tab, isl_int *line)
1634 int i;
1635 int r;
1636 isl_int *row;
1637 isl_int a, b;
1638 unsigned off = 2 + tab->M;
1640 r = isl_tab_allocate_con(tab);
1641 if (r < 0)
1642 return -1;
1644 isl_int_init(a);
1645 isl_int_init(b);
1646 row = tab->mat->row[tab->con[r].index];
1647 isl_int_set_si(row[0], 1);
1648 isl_int_set(row[1], line[0]);
1649 isl_seq_clr(row + 2, tab->M + tab->n_col);
1650 for (i = 0; i < tab->n_var; ++i) {
1651 if (tab->var[i].is_zero)
1652 continue;
1653 if (tab->var[i].is_row) {
1654 isl_int_lcm(a,
1655 row[0], tab->mat->row[tab->var[i].index][0]);
1656 isl_int_swap(a, row[0]);
1657 isl_int_divexact(a, row[0], a);
1658 isl_int_divexact(b,
1659 row[0], tab->mat->row[tab->var[i].index][0]);
1660 isl_int_mul(b, b, line[1 + i]);
1661 isl_seq_combine(row + 1, a, row + 1,
1662 b, tab->mat->row[tab->var[i].index] + 1,
1663 1 + tab->M + tab->n_col);
1664 } else
1665 isl_int_addmul(row[off + tab->var[i].index],
1666 line[1 + i], row[0]);
1667 if (tab->M && i >= tab->n_param && i < tab->n_var - tab->n_div)
1668 isl_int_submul(row[2], line[1 + i], row[0]);
1670 isl_seq_normalize(tab->mat->ctx, row, off + tab->n_col);
1671 isl_int_clear(a);
1672 isl_int_clear(b);
1674 if (tab->row_sign)
1675 tab->row_sign[tab->con[r].index] = isl_tab_row_unknown;
1677 return r;
1680 static int drop_row(struct isl_tab *tab, int row)
1682 isl_assert(tab->mat->ctx, ~tab->row_var[row] == tab->n_con - 1, return -1);
1683 if (row != tab->n_row - 1)
1684 swap_rows(tab, row, tab->n_row - 1);
1685 tab->n_row--;
1686 tab->n_con--;
1687 return 0;
1690 static int drop_col(struct isl_tab *tab, int col)
1692 isl_assert(tab->mat->ctx, tab->col_var[col] == tab->n_var - 1, return -1);
1693 if (col != tab->n_col - 1)
1694 swap_cols(tab, col, tab->n_col - 1);
1695 tab->n_col--;
1696 tab->n_var--;
1697 return 0;
1700 /* Add inequality "ineq" and check if it conflicts with the
1701 * previously added constraints or if it is obviously redundant.
1703 int isl_tab_add_ineq(struct isl_tab *tab, isl_int *ineq)
1705 int r;
1706 int sgn;
1707 isl_int cst;
1709 if (!tab)
1710 return -1;
1711 if (tab->bmap) {
1712 struct isl_basic_map *bmap = tab->bmap;
1714 isl_assert(tab->mat->ctx, tab->n_eq == bmap->n_eq, return -1);
1715 isl_assert(tab->mat->ctx,
1716 tab->n_con == bmap->n_eq + bmap->n_ineq, return -1);
1717 tab->bmap = isl_basic_map_add_ineq(tab->bmap, ineq);
1718 if (isl_tab_push(tab, isl_tab_undo_bmap_ineq) < 0)
1719 return -1;
1720 if (!tab->bmap)
1721 return -1;
1723 if (tab->cone) {
1724 isl_int_init(cst);
1725 isl_int_swap(ineq[0], cst);
1727 r = isl_tab_add_row(tab, ineq);
1728 if (tab->cone) {
1729 isl_int_swap(ineq[0], cst);
1730 isl_int_clear(cst);
1732 if (r < 0)
1733 return -1;
1734 tab->con[r].is_nonneg = 1;
1735 if (isl_tab_push_var(tab, isl_tab_undo_nonneg, &tab->con[r]) < 0)
1736 return -1;
1737 if (isl_tab_row_is_redundant(tab, tab->con[r].index)) {
1738 if (isl_tab_mark_redundant(tab, tab->con[r].index) < 0)
1739 return -1;
1740 return 0;
1743 sgn = restore_row(tab, &tab->con[r]);
1744 if (sgn < -1)
1745 return -1;
1746 if (sgn < 0)
1747 return isl_tab_mark_empty(tab);
1748 if (tab->con[r].is_row && isl_tab_row_is_redundant(tab, tab->con[r].index))
1749 if (isl_tab_mark_redundant(tab, tab->con[r].index) < 0)
1750 return -1;
1751 return 0;
1754 /* Pivot a non-negative variable down until it reaches the value zero
1755 * and then pivot the variable into a column position.
1757 static int to_col(struct isl_tab *tab, struct isl_tab_var *var) WARN_UNUSED;
1758 static int to_col(struct isl_tab *tab, struct isl_tab_var *var)
1760 int i;
1761 int row, col;
1762 unsigned off = 2 + tab->M;
1764 if (!var->is_row)
1765 return 0;
1767 while (isl_int_is_pos(tab->mat->row[var->index][1])) {
1768 find_pivot(tab, var, NULL, -1, &row, &col);
1769 isl_assert(tab->mat->ctx, row != -1, return -1);
1770 if (isl_tab_pivot(tab, row, col) < 0)
1771 return -1;
1772 if (!var->is_row)
1773 return 0;
1776 for (i = tab->n_dead; i < tab->n_col; ++i)
1777 if (!isl_int_is_zero(tab->mat->row[var->index][off + i]))
1778 break;
1780 isl_assert(tab->mat->ctx, i < tab->n_col, return -1);
1781 if (isl_tab_pivot(tab, var->index, i) < 0)
1782 return -1;
1784 return 0;
1787 /* We assume Gaussian elimination has been performed on the equalities.
1788 * The equalities can therefore never conflict.
1789 * Adding the equalities is currently only really useful for a later call
1790 * to isl_tab_ineq_type.
1792 static struct isl_tab *add_eq(struct isl_tab *tab, isl_int *eq)
1794 int i;
1795 int r;
1797 if (!tab)
1798 return NULL;
1799 r = isl_tab_add_row(tab, eq);
1800 if (r < 0)
1801 goto error;
1803 r = tab->con[r].index;
1804 i = isl_seq_first_non_zero(tab->mat->row[r] + 2 + tab->M + tab->n_dead,
1805 tab->n_col - tab->n_dead);
1806 isl_assert(tab->mat->ctx, i >= 0, goto error);
1807 i += tab->n_dead;
1808 if (isl_tab_pivot(tab, r, i) < 0)
1809 goto error;
1810 if (isl_tab_kill_col(tab, i) < 0)
1811 goto error;
1812 tab->n_eq++;
1814 return tab;
1815 error:
1816 isl_tab_free(tab);
1817 return NULL;
1820 static int row_is_manifestly_zero(struct isl_tab *tab, int row)
1822 unsigned off = 2 + tab->M;
1824 if (!isl_int_is_zero(tab->mat->row[row][1]))
1825 return 0;
1826 if (tab->M && !isl_int_is_zero(tab->mat->row[row][2]))
1827 return 0;
1828 return isl_seq_first_non_zero(tab->mat->row[row] + off + tab->n_dead,
1829 tab->n_col - tab->n_dead) == -1;
1832 /* Add an equality that is known to be valid for the given tableau.
1834 int isl_tab_add_valid_eq(struct isl_tab *tab, isl_int *eq)
1836 struct isl_tab_var *var;
1837 int r;
1839 if (!tab)
1840 return -1;
1841 r = isl_tab_add_row(tab, eq);
1842 if (r < 0)
1843 return -1;
1845 var = &tab->con[r];
1846 r = var->index;
1847 if (row_is_manifestly_zero(tab, r)) {
1848 var->is_zero = 1;
1849 if (isl_tab_mark_redundant(tab, r) < 0)
1850 return -1;
1851 return 0;
1854 if (isl_int_is_neg(tab->mat->row[r][1])) {
1855 isl_seq_neg(tab->mat->row[r] + 1, tab->mat->row[r] + 1,
1856 1 + tab->n_col);
1857 var->negated = 1;
1859 var->is_nonneg = 1;
1860 if (to_col(tab, var) < 0)
1861 return -1;
1862 var->is_nonneg = 0;
1863 if (isl_tab_kill_col(tab, var->index) < 0)
1864 return -1;
1866 return 0;
1869 static int add_zero_row(struct isl_tab *tab)
1871 int r;
1872 isl_int *row;
1874 r = isl_tab_allocate_con(tab);
1875 if (r < 0)
1876 return -1;
1878 row = tab->mat->row[tab->con[r].index];
1879 isl_seq_clr(row + 1, 1 + tab->M + tab->n_col);
1880 isl_int_set_si(row[0], 1);
1882 return r;
1885 /* Add equality "eq" and check if it conflicts with the
1886 * previously added constraints or if it is obviously redundant.
1888 int isl_tab_add_eq(struct isl_tab *tab, isl_int *eq)
1890 struct isl_tab_undo *snap = NULL;
1891 struct isl_tab_var *var;
1892 int r;
1893 int row;
1894 int sgn;
1895 isl_int cst;
1897 if (!tab)
1898 return -1;
1899 isl_assert(tab->mat->ctx, !tab->M, return -1);
1901 if (tab->need_undo)
1902 snap = isl_tab_snap(tab);
1904 if (tab->cone) {
1905 isl_int_init(cst);
1906 isl_int_swap(eq[0], cst);
1908 r = isl_tab_add_row(tab, eq);
1909 if (tab->cone) {
1910 isl_int_swap(eq[0], cst);
1911 isl_int_clear(cst);
1913 if (r < 0)
1914 return -1;
1916 var = &tab->con[r];
1917 row = var->index;
1918 if (row_is_manifestly_zero(tab, row)) {
1919 if (snap) {
1920 if (isl_tab_rollback(tab, snap) < 0)
1921 return -1;
1922 } else
1923 drop_row(tab, row);
1924 return 0;
1927 if (tab->bmap) {
1928 tab->bmap = isl_basic_map_add_ineq(tab->bmap, eq);
1929 if (isl_tab_push(tab, isl_tab_undo_bmap_ineq) < 0)
1930 return -1;
1931 isl_seq_neg(eq, eq, 1 + tab->n_var);
1932 tab->bmap = isl_basic_map_add_ineq(tab->bmap, eq);
1933 isl_seq_neg(eq, eq, 1 + tab->n_var);
1934 if (isl_tab_push(tab, isl_tab_undo_bmap_ineq) < 0)
1935 return -1;
1936 if (!tab->bmap)
1937 return -1;
1938 if (add_zero_row(tab) < 0)
1939 return -1;
1942 sgn = isl_int_sgn(tab->mat->row[row][1]);
1944 if (sgn > 0) {
1945 isl_seq_neg(tab->mat->row[row] + 1, tab->mat->row[row] + 1,
1946 1 + tab->n_col);
1947 var->negated = 1;
1948 sgn = -1;
1951 if (sgn < 0) {
1952 sgn = sign_of_max(tab, var);
1953 if (sgn < -1)
1954 return -1;
1955 if (sgn < 0) {
1956 if (isl_tab_mark_empty(tab) < 0)
1957 return -1;
1958 return 0;
1962 var->is_nonneg = 1;
1963 if (to_col(tab, var) < 0)
1964 return -1;
1965 var->is_nonneg = 0;
1966 if (isl_tab_kill_col(tab, var->index) < 0)
1967 return -1;
1969 return 0;
1972 /* Construct and return an inequality that expresses an upper bound
1973 * on the given div.
1974 * In particular, if the div is given by
1976 * d = floor(e/m)
1978 * then the inequality expresses
1980 * m d <= e
1982 static struct isl_vec *ineq_for_div(struct isl_basic_map *bmap, unsigned div)
1984 unsigned total;
1985 unsigned div_pos;
1986 struct isl_vec *ineq;
1988 if (!bmap)
1989 return NULL;
1991 total = isl_basic_map_total_dim(bmap);
1992 div_pos = 1 + total - bmap->n_div + div;
1994 ineq = isl_vec_alloc(bmap->ctx, 1 + total);
1995 if (!ineq)
1996 return NULL;
1998 isl_seq_cpy(ineq->el, bmap->div[div] + 1, 1 + total);
1999 isl_int_neg(ineq->el[div_pos], bmap->div[div][0]);
2000 return ineq;
2003 /* For a div d = floor(f/m), add the constraints
2005 * f - m d >= 0
2006 * -(f-(m-1)) + m d >= 0
2008 * Note that the second constraint is the negation of
2010 * f - m d >= m
2012 * If add_ineq is not NULL, then this function is used
2013 * instead of isl_tab_add_ineq to effectively add the inequalities.
2015 static int add_div_constraints(struct isl_tab *tab, unsigned div,
2016 int (*add_ineq)(void *user, isl_int *), void *user)
2018 unsigned total;
2019 unsigned div_pos;
2020 struct isl_vec *ineq;
2022 total = isl_basic_map_total_dim(tab->bmap);
2023 div_pos = 1 + total - tab->bmap->n_div + div;
2025 ineq = ineq_for_div(tab->bmap, div);
2026 if (!ineq)
2027 goto error;
2029 if (add_ineq) {
2030 if (add_ineq(user, ineq->el) < 0)
2031 goto error;
2032 } else {
2033 if (isl_tab_add_ineq(tab, ineq->el) < 0)
2034 goto error;
2037 isl_seq_neg(ineq->el, tab->bmap->div[div] + 1, 1 + total);
2038 isl_int_set(ineq->el[div_pos], tab->bmap->div[div][0]);
2039 isl_int_add(ineq->el[0], ineq->el[0], ineq->el[div_pos]);
2040 isl_int_sub_ui(ineq->el[0], ineq->el[0], 1);
2042 if (add_ineq) {
2043 if (add_ineq(user, ineq->el) < 0)
2044 goto error;
2045 } else {
2046 if (isl_tab_add_ineq(tab, ineq->el) < 0)
2047 goto error;
2050 isl_vec_free(ineq);
2052 return 0;
2053 error:
2054 isl_vec_free(ineq);
2055 return -1;
2058 /* Add an extra div, prescrived by "div" to the tableau and
2059 * the associated bmap (which is assumed to be non-NULL).
2061 * If add_ineq is not NULL, then this function is used instead
2062 * of isl_tab_add_ineq to add the div constraints.
2063 * This complication is needed because the code in isl_tab_pip
2064 * wants to perform some extra processing when an inequality
2065 * is added to the tableau.
2067 int isl_tab_add_div(struct isl_tab *tab, __isl_keep isl_vec *div,
2068 int (*add_ineq)(void *user, isl_int *), void *user)
2070 int i;
2071 int r;
2072 int k;
2073 int nonneg;
2075 if (!tab || !div)
2076 return -1;
2078 isl_assert(tab->mat->ctx, tab->bmap, return -1);
2080 for (i = 0; i < tab->n_var; ++i) {
2081 if (isl_int_is_neg(div->el[2 + i]))
2082 break;
2083 if (isl_int_is_zero(div->el[2 + i]))
2084 continue;
2085 if (!tab->var[i].is_nonneg)
2086 break;
2088 nonneg = i == tab->n_var && !isl_int_is_neg(div->el[1]);
2090 if (isl_tab_extend_cons(tab, 3) < 0)
2091 return -1;
2092 if (isl_tab_extend_vars(tab, 1) < 0)
2093 return -1;
2094 r = isl_tab_allocate_var(tab);
2095 if (r < 0)
2096 return -1;
2098 if (nonneg)
2099 tab->var[r].is_nonneg = 1;
2101 tab->bmap = isl_basic_map_extend_dim(tab->bmap,
2102 isl_basic_map_get_dim(tab->bmap), 1, 0, 2);
2103 k = isl_basic_map_alloc_div(tab->bmap);
2104 if (k < 0)
2105 return -1;
2106 isl_seq_cpy(tab->bmap->div[k], div->el, div->size);
2107 if (isl_tab_push(tab, isl_tab_undo_bmap_div) < 0)
2108 return -1;
2110 if (add_div_constraints(tab, k, add_ineq, user) < 0)
2111 return -1;
2113 return r;
2116 struct isl_tab *isl_tab_from_basic_map(struct isl_basic_map *bmap)
2118 int i;
2119 struct isl_tab *tab;
2121 if (!bmap)
2122 return NULL;
2123 tab = isl_tab_alloc(bmap->ctx,
2124 isl_basic_map_total_dim(bmap) + bmap->n_ineq + 1,
2125 isl_basic_map_total_dim(bmap), 0);
2126 if (!tab)
2127 return NULL;
2128 tab->rational = ISL_F_ISSET(bmap, ISL_BASIC_MAP_RATIONAL);
2129 if (ISL_F_ISSET(bmap, ISL_BASIC_MAP_EMPTY)) {
2130 if (isl_tab_mark_empty(tab) < 0)
2131 goto error;
2132 return tab;
2134 for (i = 0; i < bmap->n_eq; ++i) {
2135 tab = add_eq(tab, bmap->eq[i]);
2136 if (!tab)
2137 return tab;
2139 for (i = 0; i < bmap->n_ineq; ++i) {
2140 if (isl_tab_add_ineq(tab, bmap->ineq[i]) < 0)
2141 goto error;
2142 if (tab->empty)
2143 return tab;
2145 return tab;
2146 error:
2147 isl_tab_free(tab);
2148 return NULL;
2151 struct isl_tab *isl_tab_from_basic_set(struct isl_basic_set *bset)
2153 return isl_tab_from_basic_map((struct isl_basic_map *)bset);
2156 /* Construct a tableau corresponding to the recession cone of "bset".
2158 struct isl_tab *isl_tab_from_recession_cone(__isl_keep isl_basic_set *bset,
2159 int parametric)
2161 isl_int cst;
2162 int i;
2163 struct isl_tab *tab;
2164 unsigned offset = 0;
2166 if (!bset)
2167 return NULL;
2168 if (parametric)
2169 offset = isl_basic_set_dim(bset, isl_dim_param);
2170 tab = isl_tab_alloc(bset->ctx, bset->n_eq + bset->n_ineq,
2171 isl_basic_set_total_dim(bset) - offset, 0);
2172 if (!tab)
2173 return NULL;
2174 tab->rational = ISL_F_ISSET(bset, ISL_BASIC_SET_RATIONAL);
2175 tab->cone = 1;
2177 isl_int_init(cst);
2178 for (i = 0; i < bset->n_eq; ++i) {
2179 isl_int_swap(bset->eq[i][offset], cst);
2180 if (offset > 0) {
2181 if (isl_tab_add_eq(tab, bset->eq[i] + offset) < 0)
2182 goto error;
2183 } else
2184 tab = add_eq(tab, bset->eq[i]);
2185 isl_int_swap(bset->eq[i][offset], cst);
2186 if (!tab)
2187 goto done;
2189 for (i = 0; i < bset->n_ineq; ++i) {
2190 int r;
2191 isl_int_swap(bset->ineq[i][offset], cst);
2192 r = isl_tab_add_row(tab, bset->ineq[i] + offset);
2193 isl_int_swap(bset->ineq[i][offset], cst);
2194 if (r < 0)
2195 goto error;
2196 tab->con[r].is_nonneg = 1;
2197 if (isl_tab_push_var(tab, isl_tab_undo_nonneg, &tab->con[r]) < 0)
2198 goto error;
2200 done:
2201 isl_int_clear(cst);
2202 return tab;
2203 error:
2204 isl_int_clear(cst);
2205 isl_tab_free(tab);
2206 return NULL;
2209 /* Assuming "tab" is the tableau of a cone, check if the cone is
2210 * bounded, i.e., if it is empty or only contains the origin.
2212 int isl_tab_cone_is_bounded(struct isl_tab *tab)
2214 int i;
2216 if (!tab)
2217 return -1;
2218 if (tab->empty)
2219 return 1;
2220 if (tab->n_dead == tab->n_col)
2221 return 1;
2223 for (;;) {
2224 for (i = tab->n_redundant; i < tab->n_row; ++i) {
2225 struct isl_tab_var *var;
2226 int sgn;
2227 var = isl_tab_var_from_row(tab, i);
2228 if (!var->is_nonneg)
2229 continue;
2230 sgn = sign_of_max(tab, var);
2231 if (sgn < -1)
2232 return -1;
2233 if (sgn != 0)
2234 return 0;
2235 if (close_row(tab, var) < 0)
2236 return -1;
2237 break;
2239 if (tab->n_dead == tab->n_col)
2240 return 1;
2241 if (i == tab->n_row)
2242 return 0;
2246 int isl_tab_sample_is_integer(struct isl_tab *tab)
2248 int i;
2250 if (!tab)
2251 return -1;
2253 for (i = 0; i < tab->n_var; ++i) {
2254 int row;
2255 if (!tab->var[i].is_row)
2256 continue;
2257 row = tab->var[i].index;
2258 if (!isl_int_is_divisible_by(tab->mat->row[row][1],
2259 tab->mat->row[row][0]))
2260 return 0;
2262 return 1;
2265 static struct isl_vec *extract_integer_sample(struct isl_tab *tab)
2267 int i;
2268 struct isl_vec *vec;
2270 vec = isl_vec_alloc(tab->mat->ctx, 1 + tab->n_var);
2271 if (!vec)
2272 return NULL;
2274 isl_int_set_si(vec->block.data[0], 1);
2275 for (i = 0; i < tab->n_var; ++i) {
2276 if (!tab->var[i].is_row)
2277 isl_int_set_si(vec->block.data[1 + i], 0);
2278 else {
2279 int row = tab->var[i].index;
2280 isl_int_divexact(vec->block.data[1 + i],
2281 tab->mat->row[row][1], tab->mat->row[row][0]);
2285 return vec;
2288 struct isl_vec *isl_tab_get_sample_value(struct isl_tab *tab)
2290 int i;
2291 struct isl_vec *vec;
2292 isl_int m;
2294 if (!tab)
2295 return NULL;
2297 vec = isl_vec_alloc(tab->mat->ctx, 1 + tab->n_var);
2298 if (!vec)
2299 return NULL;
2301 isl_int_init(m);
2303 isl_int_set_si(vec->block.data[0], 1);
2304 for (i = 0; i < tab->n_var; ++i) {
2305 int row;
2306 if (!tab->var[i].is_row) {
2307 isl_int_set_si(vec->block.data[1 + i], 0);
2308 continue;
2310 row = tab->var[i].index;
2311 isl_int_gcd(m, vec->block.data[0], tab->mat->row[row][0]);
2312 isl_int_divexact(m, tab->mat->row[row][0], m);
2313 isl_seq_scale(vec->block.data, vec->block.data, m, 1 + i);
2314 isl_int_divexact(m, vec->block.data[0], tab->mat->row[row][0]);
2315 isl_int_mul(vec->block.data[1 + i], m, tab->mat->row[row][1]);
2317 vec = isl_vec_normalize(vec);
2319 isl_int_clear(m);
2320 return vec;
2323 /* Update "bmap" based on the results of the tableau "tab".
2324 * In particular, implicit equalities are made explicit, redundant constraints
2325 * are removed and if the sample value happens to be integer, it is stored
2326 * in "bmap" (unless "bmap" already had an integer sample).
2328 * The tableau is assumed to have been created from "bmap" using
2329 * isl_tab_from_basic_map.
2331 struct isl_basic_map *isl_basic_map_update_from_tab(struct isl_basic_map *bmap,
2332 struct isl_tab *tab)
2334 int i;
2335 unsigned n_eq;
2337 if (!bmap)
2338 return NULL;
2339 if (!tab)
2340 return bmap;
2342 n_eq = tab->n_eq;
2343 if (tab->empty)
2344 bmap = isl_basic_map_set_to_empty(bmap);
2345 else
2346 for (i = bmap->n_ineq - 1; i >= 0; --i) {
2347 if (isl_tab_is_equality(tab, n_eq + i))
2348 isl_basic_map_inequality_to_equality(bmap, i);
2349 else if (isl_tab_is_redundant(tab, n_eq + i))
2350 isl_basic_map_drop_inequality(bmap, i);
2352 if (bmap->n_eq != n_eq)
2353 isl_basic_map_gauss(bmap, NULL);
2354 if (!tab->rational &&
2355 !bmap->sample && isl_tab_sample_is_integer(tab))
2356 bmap->sample = extract_integer_sample(tab);
2357 return bmap;
2360 struct isl_basic_set *isl_basic_set_update_from_tab(struct isl_basic_set *bset,
2361 struct isl_tab *tab)
2363 return (struct isl_basic_set *)isl_basic_map_update_from_tab(
2364 (struct isl_basic_map *)bset, tab);
2367 /* Given a non-negative variable "var", add a new non-negative variable
2368 * that is the opposite of "var", ensuring that var can only attain the
2369 * value zero.
2370 * If var = n/d is a row variable, then the new variable = -n/d.
2371 * If var is a column variables, then the new variable = -var.
2372 * If the new variable cannot attain non-negative values, then
2373 * the resulting tableau is empty.
2374 * Otherwise, we know the value will be zero and we close the row.
2376 static int cut_to_hyperplane(struct isl_tab *tab, struct isl_tab_var *var)
2378 unsigned r;
2379 isl_int *row;
2380 int sgn;
2381 unsigned off = 2 + tab->M;
2383 if (var->is_zero)
2384 return 0;
2385 isl_assert(tab->mat->ctx, !var->is_redundant, return -1);
2386 isl_assert(tab->mat->ctx, var->is_nonneg, return -1);
2388 if (isl_tab_extend_cons(tab, 1) < 0)
2389 return -1;
2391 r = tab->n_con;
2392 tab->con[r].index = tab->n_row;
2393 tab->con[r].is_row = 1;
2394 tab->con[r].is_nonneg = 0;
2395 tab->con[r].is_zero = 0;
2396 tab->con[r].is_redundant = 0;
2397 tab->con[r].frozen = 0;
2398 tab->con[r].negated = 0;
2399 tab->row_var[tab->n_row] = ~r;
2400 row = tab->mat->row[tab->n_row];
2402 if (var->is_row) {
2403 isl_int_set(row[0], tab->mat->row[var->index][0]);
2404 isl_seq_neg(row + 1,
2405 tab->mat->row[var->index] + 1, 1 + tab->n_col);
2406 } else {
2407 isl_int_set_si(row[0], 1);
2408 isl_seq_clr(row + 1, 1 + tab->n_col);
2409 isl_int_set_si(row[off + var->index], -1);
2412 tab->n_row++;
2413 tab->n_con++;
2414 if (isl_tab_push_var(tab, isl_tab_undo_allocate, &tab->con[r]) < 0)
2415 return -1;
2417 sgn = sign_of_max(tab, &tab->con[r]);
2418 if (sgn < -1)
2419 return -1;
2420 if (sgn < 0) {
2421 if (isl_tab_mark_empty(tab) < 0)
2422 return -1;
2423 return 0;
2425 tab->con[r].is_nonneg = 1;
2426 if (isl_tab_push_var(tab, isl_tab_undo_nonneg, &tab->con[r]) < 0)
2427 return -1;
2428 /* sgn == 0 */
2429 if (close_row(tab, &tab->con[r]) < 0)
2430 return -1;
2432 return 0;
2435 /* Given a tableau "tab" and an inequality constraint "con" of the tableau,
2436 * relax the inequality by one. That is, the inequality r >= 0 is replaced
2437 * by r' = r + 1 >= 0.
2438 * If r is a row variable, we simply increase the constant term by one
2439 * (taking into account the denominator).
2440 * If r is a column variable, then we need to modify each row that
2441 * refers to r = r' - 1 by substituting this equality, effectively
2442 * subtracting the coefficient of the column from the constant.
2443 * We should only do this if the minimum is manifestly unbounded,
2444 * however. Otherwise, we may end up with negative sample values
2445 * for non-negative variables.
2446 * So, if r is a column variable with a minimum that is not
2447 * manifestly unbounded, then we need to move it to a row.
2448 * However, the sample value of this row may be negative,
2449 * even after the relaxation, so we need to restore it.
2450 * We therefore prefer to pivot a column up to a row, if possible.
2452 struct isl_tab *isl_tab_relax(struct isl_tab *tab, int con)
2454 struct isl_tab_var *var;
2455 unsigned off = 2 + tab->M;
2457 if (!tab)
2458 return NULL;
2460 var = &tab->con[con];
2462 if (!var->is_row && !max_is_manifestly_unbounded(tab, var))
2463 if (to_row(tab, var, 1) < 0)
2464 goto error;
2465 if (!var->is_row && !min_is_manifestly_unbounded(tab, var))
2466 if (to_row(tab, var, -1) < 0)
2467 goto error;
2469 if (var->is_row) {
2470 isl_int_add(tab->mat->row[var->index][1],
2471 tab->mat->row[var->index][1], tab->mat->row[var->index][0]);
2472 if (restore_row(tab, var) < 0)
2473 goto error;
2474 } else {
2475 int i;
2477 for (i = 0; i < tab->n_row; ++i) {
2478 if (isl_int_is_zero(tab->mat->row[i][off + var->index]))
2479 continue;
2480 isl_int_sub(tab->mat->row[i][1], tab->mat->row[i][1],
2481 tab->mat->row[i][off + var->index]);
2486 if (isl_tab_push_var(tab, isl_tab_undo_relax, var) < 0)
2487 goto error;
2489 return tab;
2490 error:
2491 isl_tab_free(tab);
2492 return NULL;
2495 int isl_tab_select_facet(struct isl_tab *tab, int con)
2497 if (!tab)
2498 return -1;
2500 return cut_to_hyperplane(tab, &tab->con[con]);
2503 static int may_be_equality(struct isl_tab *tab, int row)
2505 unsigned off = 2 + tab->M;
2506 return tab->rational ? isl_int_is_zero(tab->mat->row[row][1])
2507 : isl_int_lt(tab->mat->row[row][1],
2508 tab->mat->row[row][0]);
2511 /* Check for (near) equalities among the constraints.
2512 * A constraint is an equality if it is non-negative and if
2513 * its maximal value is either
2514 * - zero (in case of rational tableaus), or
2515 * - strictly less than 1 (in case of integer tableaus)
2517 * We first mark all non-redundant and non-dead variables that
2518 * are not frozen and not obviously not an equality.
2519 * Then we iterate over all marked variables if they can attain
2520 * any values larger than zero or at least one.
2521 * If the maximal value is zero, we mark any column variables
2522 * that appear in the row as being zero and mark the row as being redundant.
2523 * Otherwise, if the maximal value is strictly less than one (and the
2524 * tableau is integer), then we restrict the value to being zero
2525 * by adding an opposite non-negative variable.
2527 int isl_tab_detect_implicit_equalities(struct isl_tab *tab)
2529 int i;
2530 unsigned n_marked;
2532 if (!tab)
2533 return -1;
2534 if (tab->empty)
2535 return 0;
2536 if (tab->n_dead == tab->n_col)
2537 return 0;
2539 n_marked = 0;
2540 for (i = tab->n_redundant; i < tab->n_row; ++i) {
2541 struct isl_tab_var *var = isl_tab_var_from_row(tab, i);
2542 var->marked = !var->frozen && var->is_nonneg &&
2543 may_be_equality(tab, i);
2544 if (var->marked)
2545 n_marked++;
2547 for (i = tab->n_dead; i < tab->n_col; ++i) {
2548 struct isl_tab_var *var = var_from_col(tab, i);
2549 var->marked = !var->frozen && var->is_nonneg;
2550 if (var->marked)
2551 n_marked++;
2553 while (n_marked) {
2554 struct isl_tab_var *var;
2555 int sgn;
2556 for (i = tab->n_redundant; i < tab->n_row; ++i) {
2557 var = isl_tab_var_from_row(tab, i);
2558 if (var->marked)
2559 break;
2561 if (i == tab->n_row) {
2562 for (i = tab->n_dead; i < tab->n_col; ++i) {
2563 var = var_from_col(tab, i);
2564 if (var->marked)
2565 break;
2567 if (i == tab->n_col)
2568 break;
2570 var->marked = 0;
2571 n_marked--;
2572 sgn = sign_of_max(tab, var);
2573 if (sgn < 0)
2574 return -1;
2575 if (sgn == 0) {
2576 if (close_row(tab, var) < 0)
2577 return -1;
2578 } else if (!tab->rational && !at_least_one(tab, var)) {
2579 if (cut_to_hyperplane(tab, var) < 0)
2580 return -1;
2581 return isl_tab_detect_implicit_equalities(tab);
2583 for (i = tab->n_redundant; i < tab->n_row; ++i) {
2584 var = isl_tab_var_from_row(tab, i);
2585 if (!var->marked)
2586 continue;
2587 if (may_be_equality(tab, i))
2588 continue;
2589 var->marked = 0;
2590 n_marked--;
2594 return 0;
2597 static int con_is_redundant(struct isl_tab *tab, struct isl_tab_var *var)
2599 if (!tab)
2600 return -1;
2601 if (tab->rational) {
2602 int sgn = sign_of_min(tab, var);
2603 if (sgn < -1)
2604 return -1;
2605 return sgn >= 0;
2606 } else {
2607 int irred = isl_tab_min_at_most_neg_one(tab, var);
2608 if (irred < 0)
2609 return -1;
2610 return !irred;
2614 /* Check for (near) redundant constraints.
2615 * A constraint is redundant if it is non-negative and if
2616 * its minimal value (temporarily ignoring the non-negativity) is either
2617 * - zero (in case of rational tableaus), or
2618 * - strictly larger than -1 (in case of integer tableaus)
2620 * We first mark all non-redundant and non-dead variables that
2621 * are not frozen and not obviously negatively unbounded.
2622 * Then we iterate over all marked variables if they can attain
2623 * any values smaller than zero or at most negative one.
2624 * If not, we mark the row as being redundant (assuming it hasn't
2625 * been detected as being obviously redundant in the mean time).
2627 int isl_tab_detect_redundant(struct isl_tab *tab)
2629 int i;
2630 unsigned n_marked;
2632 if (!tab)
2633 return -1;
2634 if (tab->empty)
2635 return 0;
2636 if (tab->n_redundant == tab->n_row)
2637 return 0;
2639 n_marked = 0;
2640 for (i = tab->n_redundant; i < tab->n_row; ++i) {
2641 struct isl_tab_var *var = isl_tab_var_from_row(tab, i);
2642 var->marked = !var->frozen && var->is_nonneg;
2643 if (var->marked)
2644 n_marked++;
2646 for (i = tab->n_dead; i < tab->n_col; ++i) {
2647 struct isl_tab_var *var = var_from_col(tab, i);
2648 var->marked = !var->frozen && var->is_nonneg &&
2649 !min_is_manifestly_unbounded(tab, var);
2650 if (var->marked)
2651 n_marked++;
2653 while (n_marked) {
2654 struct isl_tab_var *var;
2655 int red;
2656 for (i = tab->n_redundant; i < tab->n_row; ++i) {
2657 var = isl_tab_var_from_row(tab, i);
2658 if (var->marked)
2659 break;
2661 if (i == tab->n_row) {
2662 for (i = tab->n_dead; i < tab->n_col; ++i) {
2663 var = var_from_col(tab, i);
2664 if (var->marked)
2665 break;
2667 if (i == tab->n_col)
2668 break;
2670 var->marked = 0;
2671 n_marked--;
2672 red = con_is_redundant(tab, var);
2673 if (red < 0)
2674 return -1;
2675 if (red && !var->is_redundant)
2676 if (isl_tab_mark_redundant(tab, var->index) < 0)
2677 return -1;
2678 for (i = tab->n_dead; i < tab->n_col; ++i) {
2679 var = var_from_col(tab, i);
2680 if (!var->marked)
2681 continue;
2682 if (!min_is_manifestly_unbounded(tab, var))
2683 continue;
2684 var->marked = 0;
2685 n_marked--;
2689 return 0;
2692 int isl_tab_is_equality(struct isl_tab *tab, int con)
2694 int row;
2695 unsigned off;
2697 if (!tab)
2698 return -1;
2699 if (tab->con[con].is_zero)
2700 return 1;
2701 if (tab->con[con].is_redundant)
2702 return 0;
2703 if (!tab->con[con].is_row)
2704 return tab->con[con].index < tab->n_dead;
2706 row = tab->con[con].index;
2708 off = 2 + tab->M;
2709 return isl_int_is_zero(tab->mat->row[row][1]) &&
2710 isl_seq_first_non_zero(tab->mat->row[row] + 2 + tab->n_dead,
2711 tab->n_col - tab->n_dead) == -1;
2714 /* Return the minimial value of the affine expression "f" with denominator
2715 * "denom" in *opt, *opt_denom, assuming the tableau is not empty and
2716 * the expression cannot attain arbitrarily small values.
2717 * If opt_denom is NULL, then *opt is rounded up to the nearest integer.
2718 * The return value reflects the nature of the result (empty, unbounded,
2719 * minmimal value returned in *opt).
2721 enum isl_lp_result isl_tab_min(struct isl_tab *tab,
2722 isl_int *f, isl_int denom, isl_int *opt, isl_int *opt_denom,
2723 unsigned flags)
2725 int r;
2726 enum isl_lp_result res = isl_lp_ok;
2727 struct isl_tab_var *var;
2728 struct isl_tab_undo *snap;
2730 if (!tab)
2731 return isl_lp_error;
2733 if (tab->empty)
2734 return isl_lp_empty;
2736 snap = isl_tab_snap(tab);
2737 r = isl_tab_add_row(tab, f);
2738 if (r < 0)
2739 return isl_lp_error;
2740 var = &tab->con[r];
2741 isl_int_mul(tab->mat->row[var->index][0],
2742 tab->mat->row[var->index][0], denom);
2743 for (;;) {
2744 int row, col;
2745 find_pivot(tab, var, var, -1, &row, &col);
2746 if (row == var->index) {
2747 res = isl_lp_unbounded;
2748 break;
2750 if (row == -1)
2751 break;
2752 if (isl_tab_pivot(tab, row, col) < 0)
2753 return isl_lp_error;
2755 if (ISL_FL_ISSET(flags, ISL_TAB_SAVE_DUAL)) {
2756 int i;
2758 isl_vec_free(tab->dual);
2759 tab->dual = isl_vec_alloc(tab->mat->ctx, 1 + tab->n_con);
2760 if (!tab->dual)
2761 return isl_lp_error;
2762 isl_int_set(tab->dual->el[0], tab->mat->row[var->index][0]);
2763 for (i = 0; i < tab->n_con; ++i) {
2764 int pos;
2765 if (tab->con[i].is_row) {
2766 isl_int_set_si(tab->dual->el[1 + i], 0);
2767 continue;
2769 pos = 2 + tab->M + tab->con[i].index;
2770 if (tab->con[i].negated)
2771 isl_int_neg(tab->dual->el[1 + i],
2772 tab->mat->row[var->index][pos]);
2773 else
2774 isl_int_set(tab->dual->el[1 + i],
2775 tab->mat->row[var->index][pos]);
2778 if (opt && res == isl_lp_ok) {
2779 if (opt_denom) {
2780 isl_int_set(*opt, tab->mat->row[var->index][1]);
2781 isl_int_set(*opt_denom, tab->mat->row[var->index][0]);
2782 } else
2783 isl_int_cdiv_q(*opt, tab->mat->row[var->index][1],
2784 tab->mat->row[var->index][0]);
2786 if (isl_tab_rollback(tab, snap) < 0)
2787 return isl_lp_error;
2788 return res;
2791 int isl_tab_is_redundant(struct isl_tab *tab, int con)
2793 if (!tab)
2794 return -1;
2795 if (tab->con[con].is_zero)
2796 return 0;
2797 if (tab->con[con].is_redundant)
2798 return 1;
2799 return tab->con[con].is_row && tab->con[con].index < tab->n_redundant;
2802 /* Take a snapshot of the tableau that can be restored by s call to
2803 * isl_tab_rollback.
2805 struct isl_tab_undo *isl_tab_snap(struct isl_tab *tab)
2807 if (!tab)
2808 return NULL;
2809 tab->need_undo = 1;
2810 return tab->top;
2813 /* Undo the operation performed by isl_tab_relax.
2815 static int unrelax(struct isl_tab *tab, struct isl_tab_var *var) WARN_UNUSED;
2816 static int unrelax(struct isl_tab *tab, struct isl_tab_var *var)
2818 unsigned off = 2 + tab->M;
2820 if (!var->is_row && !max_is_manifestly_unbounded(tab, var))
2821 if (to_row(tab, var, 1) < 0)
2822 return -1;
2824 if (var->is_row) {
2825 isl_int_sub(tab->mat->row[var->index][1],
2826 tab->mat->row[var->index][1], tab->mat->row[var->index][0]);
2827 if (var->is_nonneg) {
2828 int sgn = restore_row(tab, var);
2829 isl_assert(tab->mat->ctx, sgn >= 0, return -1);
2831 } else {
2832 int i;
2834 for (i = 0; i < tab->n_row; ++i) {
2835 if (isl_int_is_zero(tab->mat->row[i][off + var->index]))
2836 continue;
2837 isl_int_add(tab->mat->row[i][1], tab->mat->row[i][1],
2838 tab->mat->row[i][off + var->index]);
2843 return 0;
2846 static int perform_undo_var(struct isl_tab *tab, struct isl_tab_undo *undo) WARN_UNUSED;
2847 static int perform_undo_var(struct isl_tab *tab, struct isl_tab_undo *undo)
2849 struct isl_tab_var *var = var_from_index(tab, undo->u.var_index);
2850 switch(undo->type) {
2851 case isl_tab_undo_nonneg:
2852 var->is_nonneg = 0;
2853 break;
2854 case isl_tab_undo_redundant:
2855 var->is_redundant = 0;
2856 tab->n_redundant--;
2857 restore_row(tab, isl_tab_var_from_row(tab, tab->n_redundant));
2858 break;
2859 case isl_tab_undo_freeze:
2860 var->frozen = 0;
2861 break;
2862 case isl_tab_undo_zero:
2863 var->is_zero = 0;
2864 if (!var->is_row)
2865 tab->n_dead--;
2866 break;
2867 case isl_tab_undo_allocate:
2868 if (undo->u.var_index >= 0) {
2869 isl_assert(tab->mat->ctx, !var->is_row, return -1);
2870 drop_col(tab, var->index);
2871 break;
2873 if (!var->is_row) {
2874 if (!max_is_manifestly_unbounded(tab, var)) {
2875 if (to_row(tab, var, 1) < 0)
2876 return -1;
2877 } else if (!min_is_manifestly_unbounded(tab, var)) {
2878 if (to_row(tab, var, -1) < 0)
2879 return -1;
2880 } else
2881 if (to_row(tab, var, 0) < 0)
2882 return -1;
2884 drop_row(tab, var->index);
2885 break;
2886 case isl_tab_undo_relax:
2887 return unrelax(tab, var);
2890 return 0;
2893 /* Restore the tableau to the state where the basic variables
2894 * are those in "col_var".
2895 * We first construct a list of variables that are currently in
2896 * the basis, but shouldn't. Then we iterate over all variables
2897 * that should be in the basis and for each one that is currently
2898 * not in the basis, we exchange it with one of the elements of the
2899 * list constructed before.
2900 * We can always find an appropriate variable to pivot with because
2901 * the current basis is mapped to the old basis by a non-singular
2902 * matrix and so we can never end up with a zero row.
2904 static int restore_basis(struct isl_tab *tab, int *col_var)
2906 int i, j;
2907 int n_extra = 0;
2908 int *extra = NULL; /* current columns that contain bad stuff */
2909 unsigned off = 2 + tab->M;
2911 extra = isl_alloc_array(tab->mat->ctx, int, tab->n_col);
2912 if (!extra)
2913 goto error;
2914 for (i = 0; i < tab->n_col; ++i) {
2915 for (j = 0; j < tab->n_col; ++j)
2916 if (tab->col_var[i] == col_var[j])
2917 break;
2918 if (j < tab->n_col)
2919 continue;
2920 extra[n_extra++] = i;
2922 for (i = 0; i < tab->n_col && n_extra > 0; ++i) {
2923 struct isl_tab_var *var;
2924 int row;
2926 for (j = 0; j < tab->n_col; ++j)
2927 if (col_var[i] == tab->col_var[j])
2928 break;
2929 if (j < tab->n_col)
2930 continue;
2931 var = var_from_index(tab, col_var[i]);
2932 row = var->index;
2933 for (j = 0; j < n_extra; ++j)
2934 if (!isl_int_is_zero(tab->mat->row[row][off+extra[j]]))
2935 break;
2936 isl_assert(tab->mat->ctx, j < n_extra, goto error);
2937 if (isl_tab_pivot(tab, row, extra[j]) < 0)
2938 goto error;
2939 extra[j] = extra[--n_extra];
2942 free(extra);
2943 free(col_var);
2944 return 0;
2945 error:
2946 free(extra);
2947 free(col_var);
2948 return -1;
2951 /* Remove all samples with index n or greater, i.e., those samples
2952 * that were added since we saved this number of samples in
2953 * isl_tab_save_samples.
2955 static void drop_samples_since(struct isl_tab *tab, int n)
2957 int i;
2959 for (i = tab->n_sample - 1; i >= 0 && tab->n_sample > n; --i) {
2960 if (tab->sample_index[i] < n)
2961 continue;
2963 if (i != tab->n_sample - 1) {
2964 int t = tab->sample_index[tab->n_sample-1];
2965 tab->sample_index[tab->n_sample-1] = tab->sample_index[i];
2966 tab->sample_index[i] = t;
2967 isl_mat_swap_rows(tab->samples, tab->n_sample-1, i);
2969 tab->n_sample--;
2973 static int perform_undo(struct isl_tab *tab, struct isl_tab_undo *undo) WARN_UNUSED;
2974 static int perform_undo(struct isl_tab *tab, struct isl_tab_undo *undo)
2976 switch (undo->type) {
2977 case isl_tab_undo_empty:
2978 tab->empty = 0;
2979 break;
2980 case isl_tab_undo_nonneg:
2981 case isl_tab_undo_redundant:
2982 case isl_tab_undo_freeze:
2983 case isl_tab_undo_zero:
2984 case isl_tab_undo_allocate:
2985 case isl_tab_undo_relax:
2986 return perform_undo_var(tab, undo);
2987 case isl_tab_undo_bmap_eq:
2988 return isl_basic_map_free_equality(tab->bmap, 1);
2989 case isl_tab_undo_bmap_ineq:
2990 return isl_basic_map_free_inequality(tab->bmap, 1);
2991 case isl_tab_undo_bmap_div:
2992 if (isl_basic_map_free_div(tab->bmap, 1) < 0)
2993 return -1;
2994 if (tab->samples)
2995 tab->samples->n_col--;
2996 break;
2997 case isl_tab_undo_saved_basis:
2998 if (restore_basis(tab, undo->u.col_var) < 0)
2999 return -1;
3000 break;
3001 case isl_tab_undo_drop_sample:
3002 tab->n_outside--;
3003 break;
3004 case isl_tab_undo_saved_samples:
3005 drop_samples_since(tab, undo->u.n);
3006 break;
3007 case isl_tab_undo_callback:
3008 return undo->u.callback->run(undo->u.callback);
3009 default:
3010 isl_assert(tab->mat->ctx, 0, return -1);
3012 return 0;
3015 /* Return the tableau to the state it was in when the snapshot "snap"
3016 * was taken.
3018 int isl_tab_rollback(struct isl_tab *tab, struct isl_tab_undo *snap)
3020 struct isl_tab_undo *undo, *next;
3022 if (!tab)
3023 return -1;
3025 tab->in_undo = 1;
3026 for (undo = tab->top; undo && undo != &tab->bottom; undo = next) {
3027 next = undo->next;
3028 if (undo == snap)
3029 break;
3030 if (perform_undo(tab, undo) < 0) {
3031 free_undo(tab);
3032 tab->in_undo = 0;
3033 return -1;
3035 free(undo);
3037 tab->in_undo = 0;
3038 tab->top = undo;
3039 if (!undo)
3040 return -1;
3041 return 0;
3044 /* The given row "row" represents an inequality violated by all
3045 * points in the tableau. Check for some special cases of such
3046 * separating constraints.
3047 * In particular, if the row has been reduced to the constant -1,
3048 * then we know the inequality is adjacent (but opposite) to
3049 * an equality in the tableau.
3050 * If the row has been reduced to r = -1 -r', with r' an inequality
3051 * of the tableau, then the inequality is adjacent (but opposite)
3052 * to the inequality r'.
3054 static enum isl_ineq_type separation_type(struct isl_tab *tab, unsigned row)
3056 int pos;
3057 unsigned off = 2 + tab->M;
3059 if (tab->rational)
3060 return isl_ineq_separate;
3062 if (!isl_int_is_one(tab->mat->row[row][0]))
3063 return isl_ineq_separate;
3064 if (!isl_int_is_negone(tab->mat->row[row][1]))
3065 return isl_ineq_separate;
3067 pos = isl_seq_first_non_zero(tab->mat->row[row] + off + tab->n_dead,
3068 tab->n_col - tab->n_dead);
3069 if (pos == -1)
3070 return isl_ineq_adj_eq;
3072 if (!isl_int_is_negone(tab->mat->row[row][off + tab->n_dead + pos]))
3073 return isl_ineq_separate;
3075 pos = isl_seq_first_non_zero(
3076 tab->mat->row[row] + off + tab->n_dead + pos + 1,
3077 tab->n_col - tab->n_dead - pos - 1);
3079 return pos == -1 ? isl_ineq_adj_ineq : isl_ineq_separate;
3082 /* Check the effect of inequality "ineq" on the tableau "tab".
3083 * The result may be
3084 * isl_ineq_redundant: satisfied by all points in the tableau
3085 * isl_ineq_separate: satisfied by no point in the tableau
3086 * isl_ineq_cut: satisfied by some by not all points
3087 * isl_ineq_adj_eq: adjacent to an equality
3088 * isl_ineq_adj_ineq: adjacent to an inequality.
3090 enum isl_ineq_type isl_tab_ineq_type(struct isl_tab *tab, isl_int *ineq)
3092 enum isl_ineq_type type = isl_ineq_error;
3093 struct isl_tab_undo *snap = NULL;
3094 int con;
3095 int row;
3097 if (!tab)
3098 return isl_ineq_error;
3100 if (isl_tab_extend_cons(tab, 1) < 0)
3101 return isl_ineq_error;
3103 snap = isl_tab_snap(tab);
3105 con = isl_tab_add_row(tab, ineq);
3106 if (con < 0)
3107 goto error;
3109 row = tab->con[con].index;
3110 if (isl_tab_row_is_redundant(tab, row))
3111 type = isl_ineq_redundant;
3112 else if (isl_int_is_neg(tab->mat->row[row][1]) &&
3113 (tab->rational ||
3114 isl_int_abs_ge(tab->mat->row[row][1],
3115 tab->mat->row[row][0]))) {
3116 int nonneg = at_least_zero(tab, &tab->con[con]);
3117 if (nonneg < 0)
3118 goto error;
3119 if (nonneg)
3120 type = isl_ineq_cut;
3121 else
3122 type = separation_type(tab, row);
3123 } else {
3124 int red = con_is_redundant(tab, &tab->con[con]);
3125 if (red < 0)
3126 goto error;
3127 if (!red)
3128 type = isl_ineq_cut;
3129 else
3130 type = isl_ineq_redundant;
3133 if (isl_tab_rollback(tab, snap))
3134 return isl_ineq_error;
3135 return type;
3136 error:
3137 return isl_ineq_error;
3140 int isl_tab_track_bmap(struct isl_tab *tab, __isl_take isl_basic_map *bmap)
3142 if (!tab || !bmap)
3143 goto error;
3145 isl_assert(tab->mat->ctx, tab->n_eq == bmap->n_eq, return -1);
3146 isl_assert(tab->mat->ctx,
3147 tab->n_con == bmap->n_eq + bmap->n_ineq, return -1);
3149 tab->bmap = bmap;
3151 return 0;
3152 error:
3153 isl_basic_map_free(bmap);
3154 return -1;
3157 int isl_tab_track_bset(struct isl_tab *tab, __isl_take isl_basic_set *bset)
3159 return isl_tab_track_bmap(tab, (isl_basic_map *)bset);
3162 __isl_keep isl_basic_set *isl_tab_peek_bset(struct isl_tab *tab)
3164 if (!tab)
3165 return NULL;
3167 return (isl_basic_set *)tab->bmap;
3170 void isl_tab_dump(struct isl_tab *tab, FILE *out, int indent)
3172 unsigned r, c;
3173 int i;
3175 if (!tab) {
3176 fprintf(out, "%*snull tab\n", indent, "");
3177 return;
3179 fprintf(out, "%*sn_redundant: %d, n_dead: %d", indent, "",
3180 tab->n_redundant, tab->n_dead);
3181 if (tab->rational)
3182 fprintf(out, ", rational");
3183 if (tab->empty)
3184 fprintf(out, ", empty");
3185 fprintf(out, "\n");
3186 fprintf(out, "%*s[", indent, "");
3187 for (i = 0; i < tab->n_var; ++i) {
3188 if (i)
3189 fprintf(out, (i == tab->n_param ||
3190 i == tab->n_var - tab->n_div) ? "; "
3191 : ", ");
3192 fprintf(out, "%c%d%s", tab->var[i].is_row ? 'r' : 'c',
3193 tab->var[i].index,
3194 tab->var[i].is_zero ? " [=0]" :
3195 tab->var[i].is_redundant ? " [R]" : "");
3197 fprintf(out, "]\n");
3198 fprintf(out, "%*s[", indent, "");
3199 for (i = 0; i < tab->n_con; ++i) {
3200 if (i)
3201 fprintf(out, ", ");
3202 fprintf(out, "%c%d%s", tab->con[i].is_row ? 'r' : 'c',
3203 tab->con[i].index,
3204 tab->con[i].is_zero ? " [=0]" :
3205 tab->con[i].is_redundant ? " [R]" : "");
3207 fprintf(out, "]\n");
3208 fprintf(out, "%*s[", indent, "");
3209 for (i = 0; i < tab->n_row; ++i) {
3210 const char *sign = "";
3211 if (i)
3212 fprintf(out, ", ");
3213 if (tab->row_sign) {
3214 if (tab->row_sign[i] == isl_tab_row_unknown)
3215 sign = "?";
3216 else if (tab->row_sign[i] == isl_tab_row_neg)
3217 sign = "-";
3218 else if (tab->row_sign[i] == isl_tab_row_pos)
3219 sign = "+";
3220 else
3221 sign = "+-";
3223 fprintf(out, "r%d: %d%s%s", i, tab->row_var[i],
3224 isl_tab_var_from_row(tab, i)->is_nonneg ? " [>=0]" : "", sign);
3226 fprintf(out, "]\n");
3227 fprintf(out, "%*s[", indent, "");
3228 for (i = 0; i < tab->n_col; ++i) {
3229 if (i)
3230 fprintf(out, ", ");
3231 fprintf(out, "c%d: %d%s", i, tab->col_var[i],
3232 var_from_col(tab, i)->is_nonneg ? " [>=0]" : "");
3234 fprintf(out, "]\n");
3235 r = tab->mat->n_row;
3236 tab->mat->n_row = tab->n_row;
3237 c = tab->mat->n_col;
3238 tab->mat->n_col = 2 + tab->M + tab->n_col;
3239 isl_mat_dump(tab->mat, out, indent);
3240 tab->mat->n_row = r;
3241 tab->mat->n_col = c;
3242 if (tab->bmap)
3243 isl_basic_map_dump(tab->bmap, out, indent);