isl_tab: optionally only mark strictly redundant rows
[isl.git] / isl_tab.c
blob741c27ea7ea9c277e5b1e64eddf29e12c9d8c29a
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 = 2 + tab->M;
92 if (!tab)
93 return -1;
95 if (tab->max_con < tab->n_con + n_new) {
96 struct isl_tab_var *con;
98 con = isl_realloc_array(tab->mat->ctx, tab->con,
99 struct isl_tab_var, tab->max_con + n_new);
100 if (!con)
101 return -1;
102 tab->con = con;
103 tab->max_con += n_new;
105 if (tab->mat->n_row < tab->n_row + n_new) {
106 int *row_var;
108 tab->mat = isl_mat_extend(tab->mat,
109 tab->n_row + n_new, off + tab->n_col);
110 if (!tab->mat)
111 return -1;
112 row_var = isl_realloc_array(tab->mat->ctx, tab->row_var,
113 int, tab->mat->n_row);
114 if (!row_var)
115 return -1;
116 tab->row_var = row_var;
117 if (tab->row_sign) {
118 enum isl_tab_row_sign *s;
119 s = isl_realloc_array(tab->mat->ctx, tab->row_sign,
120 enum isl_tab_row_sign, tab->mat->n_row);
121 if (!s)
122 return -1;
123 tab->row_sign = s;
126 return 0;
129 /* Make room for at least n_new extra variables.
130 * Return -1 if anything went wrong.
132 int isl_tab_extend_vars(struct isl_tab *tab, unsigned n_new)
134 struct isl_tab_var *var;
135 unsigned off = 2 + tab->M;
137 if (tab->max_var < tab->n_var + n_new) {
138 var = isl_realloc_array(tab->mat->ctx, tab->var,
139 struct isl_tab_var, tab->n_var + n_new);
140 if (!var)
141 return -1;
142 tab->var = var;
143 tab->max_var += n_new;
146 if (tab->mat->n_col < off + tab->n_col + n_new) {
147 int *p;
149 tab->mat = isl_mat_extend(tab->mat,
150 tab->mat->n_row, off + tab->n_col + n_new);
151 if (!tab->mat)
152 return -1;
153 p = isl_realloc_array(tab->mat->ctx, tab->col_var,
154 int, tab->n_col + n_new);
155 if (!p)
156 return -1;
157 tab->col_var = p;
160 return 0;
163 struct isl_tab *isl_tab_extend(struct isl_tab *tab, unsigned n_new)
165 if (isl_tab_extend_cons(tab, n_new) >= 0)
166 return tab;
168 isl_tab_free(tab);
169 return NULL;
172 static void free_undo(struct isl_tab *tab)
174 struct isl_tab_undo *undo, *next;
176 for (undo = tab->top; undo && undo != &tab->bottom; undo = next) {
177 next = undo->next;
178 free(undo);
180 tab->top = undo;
183 void isl_tab_free(struct isl_tab *tab)
185 if (!tab)
186 return;
187 free_undo(tab);
188 isl_mat_free(tab->mat);
189 isl_vec_free(tab->dual);
190 isl_basic_map_free(tab->bmap);
191 free(tab->var);
192 free(tab->con);
193 free(tab->row_var);
194 free(tab->col_var);
195 free(tab->row_sign);
196 isl_mat_free(tab->samples);
197 free(tab->sample_index);
198 isl_mat_free(tab->basis);
199 free(tab);
202 struct isl_tab *isl_tab_dup(struct isl_tab *tab)
204 int i;
205 struct isl_tab *dup;
206 unsigned off;
208 if (!tab)
209 return NULL;
211 off = 2 + tab->M;
212 dup = isl_calloc_type(tab->ctx, struct isl_tab);
213 if (!dup)
214 return NULL;
215 dup->mat = isl_mat_dup(tab->mat);
216 if (!dup->mat)
217 goto error;
218 dup->var = isl_alloc_array(tab->ctx, struct isl_tab_var, tab->max_var);
219 if (!dup->var)
220 goto error;
221 for (i = 0; i < tab->n_var; ++i)
222 dup->var[i] = tab->var[i];
223 dup->con = isl_alloc_array(tab->ctx, struct isl_tab_var, tab->max_con);
224 if (!dup->con)
225 goto error;
226 for (i = 0; i < tab->n_con; ++i)
227 dup->con[i] = tab->con[i];
228 dup->col_var = isl_alloc_array(tab->ctx, int, tab->mat->n_col - off);
229 if (!dup->col_var)
230 goto error;
231 for (i = 0; i < tab->n_col; ++i)
232 dup->col_var[i] = tab->col_var[i];
233 dup->row_var = isl_alloc_array(tab->ctx, int, tab->mat->n_row);
234 if (!dup->row_var)
235 goto error;
236 for (i = 0; i < tab->n_row; ++i)
237 dup->row_var[i] = tab->row_var[i];
238 if (tab->row_sign) {
239 dup->row_sign = isl_alloc_array(tab->ctx, enum isl_tab_row_sign,
240 tab->mat->n_row);
241 if (!dup->row_sign)
242 goto error;
243 for (i = 0; i < tab->n_row; ++i)
244 dup->row_sign[i] = tab->row_sign[i];
246 if (tab->samples) {
247 dup->samples = isl_mat_dup(tab->samples);
248 if (!dup->samples)
249 goto error;
250 dup->sample_index = isl_alloc_array(tab->mat->ctx, int,
251 tab->samples->n_row);
252 if (!dup->sample_index)
253 goto error;
254 dup->n_sample = tab->n_sample;
255 dup->n_outside = tab->n_outside;
257 dup->n_row = tab->n_row;
258 dup->n_con = tab->n_con;
259 dup->n_eq = tab->n_eq;
260 dup->max_con = tab->max_con;
261 dup->n_col = tab->n_col;
262 dup->n_var = tab->n_var;
263 dup->max_var = tab->max_var;
264 dup->n_param = tab->n_param;
265 dup->n_div = tab->n_div;
266 dup->n_dead = tab->n_dead;
267 dup->n_redundant = tab->n_redundant;
268 dup->rational = tab->rational;
269 dup->empty = tab->empty;
270 dup->strict_redundant = 0;
271 dup->need_undo = 0;
272 dup->in_undo = 0;
273 dup->M = tab->M;
274 tab->cone = tab->cone;
275 dup->bottom.type = isl_tab_undo_bottom;
276 dup->bottom.next = NULL;
277 dup->top = &dup->bottom;
279 dup->n_zero = tab->n_zero;
280 dup->n_unbounded = tab->n_unbounded;
281 dup->basis = isl_mat_dup(tab->basis);
283 return dup;
284 error:
285 isl_tab_free(dup);
286 return NULL;
289 /* Construct the coefficient matrix of the product tableau
290 * of two tableaus.
291 * mat{1,2} is the coefficient matrix of tableau {1,2}
292 * row{1,2} is the number of rows in tableau {1,2}
293 * col{1,2} is the number of columns in tableau {1,2}
294 * off is the offset to the coefficient column (skipping the
295 * denominator, the constant term and the big parameter if any)
296 * r{1,2} is the number of redundant rows in tableau {1,2}
297 * d{1,2} is the number of dead columns in tableau {1,2}
299 * The order of the rows and columns in the result is as explained
300 * in isl_tab_product.
302 static struct isl_mat *tab_mat_product(struct isl_mat *mat1,
303 struct isl_mat *mat2, unsigned row1, unsigned row2,
304 unsigned col1, unsigned col2,
305 unsigned off, unsigned r1, unsigned r2, unsigned d1, unsigned d2)
307 int i;
308 struct isl_mat *prod;
309 unsigned n;
311 prod = isl_mat_alloc(mat1->ctx, mat1->n_row + mat2->n_row,
312 off + col1 + col2);
314 n = 0;
315 for (i = 0; i < r1; ++i) {
316 isl_seq_cpy(prod->row[n + i], mat1->row[i], off + d1);
317 isl_seq_clr(prod->row[n + i] + off + d1, d2);
318 isl_seq_cpy(prod->row[n + i] + off + d1 + d2,
319 mat1->row[i] + off + d1, col1 - d1);
320 isl_seq_clr(prod->row[n + i] + off + col1 + d1, col2 - d2);
323 n += r1;
324 for (i = 0; i < r2; ++i) {
325 isl_seq_cpy(prod->row[n + i], mat2->row[i], off);
326 isl_seq_clr(prod->row[n + i] + off, d1);
327 isl_seq_cpy(prod->row[n + i] + off + d1,
328 mat2->row[i] + off, d2);
329 isl_seq_clr(prod->row[n + i] + off + d1 + d2, col1 - d1);
330 isl_seq_cpy(prod->row[n + i] + off + col1 + d1,
331 mat2->row[i] + off + d2, col2 - d2);
334 n += r2;
335 for (i = 0; i < row1 - r1; ++i) {
336 isl_seq_cpy(prod->row[n + i], mat1->row[r1 + i], off + d1);
337 isl_seq_clr(prod->row[n + i] + off + d1, d2);
338 isl_seq_cpy(prod->row[n + i] + off + d1 + d2,
339 mat1->row[r1 + i] + off + d1, col1 - d1);
340 isl_seq_clr(prod->row[n + i] + off + col1 + d1, col2 - d2);
343 n += row1 - r1;
344 for (i = 0; i < row2 - r2; ++i) {
345 isl_seq_cpy(prod->row[n + i], mat2->row[r2 + i], off);
346 isl_seq_clr(prod->row[n + i] + off, d1);
347 isl_seq_cpy(prod->row[n + i] + off + d1,
348 mat2->row[r2 + i] + off, d2);
349 isl_seq_clr(prod->row[n + i] + off + d1 + d2, col1 - d1);
350 isl_seq_cpy(prod->row[n + i] + off + col1 + d1,
351 mat2->row[r2 + i] + off + d2, col2 - d2);
354 return prod;
357 /* Update the row or column index of a variable that corresponds
358 * to a variable in the first input tableau.
360 static void update_index1(struct isl_tab_var *var,
361 unsigned r1, unsigned r2, unsigned d1, unsigned d2)
363 if (var->index == -1)
364 return;
365 if (var->is_row && var->index >= r1)
366 var->index += r2;
367 if (!var->is_row && var->index >= d1)
368 var->index += d2;
371 /* Update the row or column index of a variable that corresponds
372 * to a variable in the second input tableau.
374 static void update_index2(struct isl_tab_var *var,
375 unsigned row1, unsigned col1,
376 unsigned r1, unsigned r2, unsigned d1, unsigned d2)
378 if (var->index == -1)
379 return;
380 if (var->is_row) {
381 if (var->index < r2)
382 var->index += r1;
383 else
384 var->index += row1;
385 } else {
386 if (var->index < d2)
387 var->index += d1;
388 else
389 var->index += col1;
393 /* Create a tableau that represents the Cartesian product of the sets
394 * represented by tableaus tab1 and tab2.
395 * The order of the rows in the product is
396 * - redundant rows of tab1
397 * - redundant rows of tab2
398 * - non-redundant rows of tab1
399 * - non-redundant rows of tab2
400 * The order of the columns is
401 * - denominator
402 * - constant term
403 * - coefficient of big parameter, if any
404 * - dead columns of tab1
405 * - dead columns of tab2
406 * - live columns of tab1
407 * - live columns of tab2
408 * The order of the variables and the constraints is a concatenation
409 * of order in the two input tableaus.
411 struct isl_tab *isl_tab_product(struct isl_tab *tab1, struct isl_tab *tab2)
413 int i;
414 struct isl_tab *prod;
415 unsigned off;
416 unsigned r1, r2, d1, d2;
418 if (!tab1 || !tab2)
419 return NULL;
421 isl_assert(tab1->mat->ctx, tab1->M == tab2->M, return NULL);
422 isl_assert(tab1->mat->ctx, tab1->rational == tab2->rational, return NULL);
423 isl_assert(tab1->mat->ctx, tab1->cone == tab2->cone, return NULL);
424 isl_assert(tab1->mat->ctx, !tab1->row_sign, return NULL);
425 isl_assert(tab1->mat->ctx, !tab2->row_sign, return NULL);
426 isl_assert(tab1->mat->ctx, tab1->n_param == 0, return NULL);
427 isl_assert(tab1->mat->ctx, tab2->n_param == 0, return NULL);
428 isl_assert(tab1->mat->ctx, tab1->n_div == 0, return NULL);
429 isl_assert(tab1->mat->ctx, tab2->n_div == 0, return NULL);
431 off = 2 + tab1->M;
432 r1 = tab1->n_redundant;
433 r2 = tab2->n_redundant;
434 d1 = tab1->n_dead;
435 d2 = tab2->n_dead;
436 prod = isl_calloc_type(tab1->mat->ctx, struct isl_tab);
437 if (!prod)
438 return NULL;
439 prod->mat = tab_mat_product(tab1->mat, tab2->mat,
440 tab1->n_row, tab2->n_row,
441 tab1->n_col, tab2->n_col, off, r1, r2, d1, d2);
442 if (!prod->mat)
443 goto error;
444 prod->var = isl_alloc_array(tab1->mat->ctx, struct isl_tab_var,
445 tab1->max_var + tab2->max_var);
446 if (!prod->var)
447 goto error;
448 for (i = 0; i < tab1->n_var; ++i) {
449 prod->var[i] = tab1->var[i];
450 update_index1(&prod->var[i], r1, r2, d1, d2);
452 for (i = 0; i < tab2->n_var; ++i) {
453 prod->var[tab1->n_var + i] = tab2->var[i];
454 update_index2(&prod->var[tab1->n_var + i],
455 tab1->n_row, tab1->n_col,
456 r1, r2, d1, d2);
458 prod->con = isl_alloc_array(tab1->mat->ctx, struct isl_tab_var,
459 tab1->max_con + tab2->max_con);
460 if (!prod->con)
461 goto error;
462 for (i = 0; i < tab1->n_con; ++i) {
463 prod->con[i] = tab1->con[i];
464 update_index1(&prod->con[i], r1, r2, d1, d2);
466 for (i = 0; i < tab2->n_con; ++i) {
467 prod->con[tab1->n_con + i] = tab2->con[i];
468 update_index2(&prod->con[tab1->n_con + i],
469 tab1->n_row, tab1->n_col,
470 r1, r2, d1, d2);
472 prod->col_var = isl_alloc_array(tab1->mat->ctx, int,
473 tab1->n_col + tab2->n_col);
474 if (!prod->col_var)
475 goto error;
476 for (i = 0; i < tab1->n_col; ++i) {
477 int pos = i < d1 ? i : i + d2;
478 prod->col_var[pos] = tab1->col_var[i];
480 for (i = 0; i < tab2->n_col; ++i) {
481 int pos = i < d2 ? d1 + i : tab1->n_col + i;
482 int t = tab2->col_var[i];
483 if (t >= 0)
484 t += tab1->n_var;
485 else
486 t -= tab1->n_con;
487 prod->col_var[pos] = t;
489 prod->row_var = isl_alloc_array(tab1->mat->ctx, int,
490 tab1->mat->n_row + tab2->mat->n_row);
491 if (!prod->row_var)
492 goto error;
493 for (i = 0; i < tab1->n_row; ++i) {
494 int pos = i < r1 ? i : i + r2;
495 prod->row_var[pos] = tab1->row_var[i];
497 for (i = 0; i < tab2->n_row; ++i) {
498 int pos = i < r2 ? r1 + i : tab1->n_row + i;
499 int t = tab2->row_var[i];
500 if (t >= 0)
501 t += tab1->n_var;
502 else
503 t -= tab1->n_con;
504 prod->row_var[pos] = t;
506 prod->samples = NULL;
507 prod->sample_index = NULL;
508 prod->n_row = tab1->n_row + tab2->n_row;
509 prod->n_con = tab1->n_con + tab2->n_con;
510 prod->n_eq = 0;
511 prod->max_con = tab1->max_con + tab2->max_con;
512 prod->n_col = tab1->n_col + tab2->n_col;
513 prod->n_var = tab1->n_var + tab2->n_var;
514 prod->max_var = tab1->max_var + tab2->max_var;
515 prod->n_param = 0;
516 prod->n_div = 0;
517 prod->n_dead = tab1->n_dead + tab2->n_dead;
518 prod->n_redundant = tab1->n_redundant + tab2->n_redundant;
519 prod->rational = tab1->rational;
520 prod->empty = tab1->empty || tab2->empty;
521 prod->strict_redundant = tab1->strict_redundant || tab2->strict_redundant;
522 prod->need_undo = 0;
523 prod->in_undo = 0;
524 prod->M = tab1->M;
525 prod->cone = tab1->cone;
526 prod->bottom.type = isl_tab_undo_bottom;
527 prod->bottom.next = NULL;
528 prod->top = &prod->bottom;
530 prod->n_zero = 0;
531 prod->n_unbounded = 0;
532 prod->basis = NULL;
534 return prod;
535 error:
536 isl_tab_free(prod);
537 return NULL;
540 static struct isl_tab_var *var_from_index(struct isl_tab *tab, int i)
542 if (i >= 0)
543 return &tab->var[i];
544 else
545 return &tab->con[~i];
548 struct isl_tab_var *isl_tab_var_from_row(struct isl_tab *tab, int i)
550 return var_from_index(tab, tab->row_var[i]);
553 static struct isl_tab_var *var_from_col(struct isl_tab *tab, int i)
555 return var_from_index(tab, tab->col_var[i]);
558 /* Check if there are any upper bounds on column variable "var",
559 * i.e., non-negative rows where var appears with a negative coefficient.
560 * Return 1 if there are no such bounds.
562 static int max_is_manifestly_unbounded(struct isl_tab *tab,
563 struct isl_tab_var *var)
565 int i;
566 unsigned off = 2 + tab->M;
568 if (var->is_row)
569 return 0;
570 for (i = tab->n_redundant; i < tab->n_row; ++i) {
571 if (!isl_int_is_neg(tab->mat->row[i][off + var->index]))
572 continue;
573 if (isl_tab_var_from_row(tab, i)->is_nonneg)
574 return 0;
576 return 1;
579 /* Check if there are any lower bounds on column variable "var",
580 * i.e., non-negative rows where var appears with a positive coefficient.
581 * Return 1 if there are no such bounds.
583 static int min_is_manifestly_unbounded(struct isl_tab *tab,
584 struct isl_tab_var *var)
586 int i;
587 unsigned off = 2 + tab->M;
589 if (var->is_row)
590 return 0;
591 for (i = tab->n_redundant; i < tab->n_row; ++i) {
592 if (!isl_int_is_pos(tab->mat->row[i][off + var->index]))
593 continue;
594 if (isl_tab_var_from_row(tab, i)->is_nonneg)
595 return 0;
597 return 1;
600 static int row_cmp(struct isl_tab *tab, int r1, int r2, int c, isl_int t)
602 unsigned off = 2 + tab->M;
604 if (tab->M) {
605 int s;
606 isl_int_mul(t, tab->mat->row[r1][2], tab->mat->row[r2][off+c]);
607 isl_int_submul(t, tab->mat->row[r2][2], tab->mat->row[r1][off+c]);
608 s = isl_int_sgn(t);
609 if (s)
610 return s;
612 isl_int_mul(t, tab->mat->row[r1][1], tab->mat->row[r2][off + c]);
613 isl_int_submul(t, tab->mat->row[r2][1], tab->mat->row[r1][off + c]);
614 return isl_int_sgn(t);
617 /* Given the index of a column "c", return the index of a row
618 * that can be used to pivot the column in, with either an increase
619 * (sgn > 0) or a decrease (sgn < 0) of the corresponding variable.
620 * If "var" is not NULL, then the row returned will be different from
621 * the one associated with "var".
623 * Each row in the tableau is of the form
625 * x_r = a_r0 + \sum_i a_ri x_i
627 * Only rows with x_r >= 0 and with the sign of a_ri opposite to "sgn"
628 * impose any limit on the increase or decrease in the value of x_c
629 * and this bound is equal to a_r0 / |a_rc|. We are therefore looking
630 * for the row with the smallest (most stringent) such bound.
631 * Note that the common denominator of each row drops out of the fraction.
632 * To check if row j has a smaller bound than row r, i.e.,
633 * a_j0 / |a_jc| < a_r0 / |a_rc| or a_j0 |a_rc| < a_r0 |a_jc|,
634 * we check if -sign(a_jc) (a_j0 a_rc - a_r0 a_jc) < 0,
635 * where -sign(a_jc) is equal to "sgn".
637 static int pivot_row(struct isl_tab *tab,
638 struct isl_tab_var *var, int sgn, int c)
640 int j, r, tsgn;
641 isl_int t;
642 unsigned off = 2 + tab->M;
644 isl_int_init(t);
645 r = -1;
646 for (j = tab->n_redundant; j < tab->n_row; ++j) {
647 if (var && j == var->index)
648 continue;
649 if (!isl_tab_var_from_row(tab, j)->is_nonneg)
650 continue;
651 if (sgn * isl_int_sgn(tab->mat->row[j][off + c]) >= 0)
652 continue;
653 if (r < 0) {
654 r = j;
655 continue;
657 tsgn = sgn * row_cmp(tab, r, j, c, t);
658 if (tsgn < 0 || (tsgn == 0 &&
659 tab->row_var[j] < tab->row_var[r]))
660 r = j;
662 isl_int_clear(t);
663 return r;
666 /* Find a pivot (row and col) that will increase (sgn > 0) or decrease
667 * (sgn < 0) the value of row variable var.
668 * If not NULL, then skip_var is a row variable that should be ignored
669 * while looking for a pivot row. It is usually equal to var.
671 * As the given row in the tableau is of the form
673 * x_r = a_r0 + \sum_i a_ri x_i
675 * we need to find a column such that the sign of a_ri is equal to "sgn"
676 * (such that an increase in x_i will have the desired effect) or a
677 * column with a variable that may attain negative values.
678 * If a_ri is positive, then we need to move x_i in the same direction
679 * to obtain the desired effect. Otherwise, x_i has to move in the
680 * opposite direction.
682 static void find_pivot(struct isl_tab *tab,
683 struct isl_tab_var *var, struct isl_tab_var *skip_var,
684 int sgn, int *row, int *col)
686 int j, r, c;
687 isl_int *tr;
689 *row = *col = -1;
691 isl_assert(tab->mat->ctx, var->is_row, return);
692 tr = tab->mat->row[var->index] + 2 + tab->M;
694 c = -1;
695 for (j = tab->n_dead; j < tab->n_col; ++j) {
696 if (isl_int_is_zero(tr[j]))
697 continue;
698 if (isl_int_sgn(tr[j]) != sgn &&
699 var_from_col(tab, j)->is_nonneg)
700 continue;
701 if (c < 0 || tab->col_var[j] < tab->col_var[c])
702 c = j;
704 if (c < 0)
705 return;
707 sgn *= isl_int_sgn(tr[c]);
708 r = pivot_row(tab, skip_var, sgn, c);
709 *row = r < 0 ? var->index : r;
710 *col = c;
713 /* Return 1 if row "row" represents an obviously redundant inequality.
714 * This means
715 * - it represents an inequality or a variable
716 * - that is the sum of a non-negative sample value and a positive
717 * combination of zero or more non-negative constraints.
719 int isl_tab_row_is_redundant(struct isl_tab *tab, int row)
721 int i;
722 unsigned off = 2 + tab->M;
724 if (tab->row_var[row] < 0 && !isl_tab_var_from_row(tab, row)->is_nonneg)
725 return 0;
727 if (isl_int_is_neg(tab->mat->row[row][1]))
728 return 0;
729 if (tab->strict_redundant && isl_int_is_zero(tab->mat->row[row][1]))
730 return 0;
731 if (tab->M && isl_int_is_neg(tab->mat->row[row][2]))
732 return 0;
734 for (i = tab->n_dead; i < tab->n_col; ++i) {
735 if (isl_int_is_zero(tab->mat->row[row][off + i]))
736 continue;
737 if (tab->col_var[i] >= 0)
738 return 0;
739 if (isl_int_is_neg(tab->mat->row[row][off + i]))
740 return 0;
741 if (!var_from_col(tab, i)->is_nonneg)
742 return 0;
744 return 1;
747 static void swap_rows(struct isl_tab *tab, int row1, int row2)
749 int t;
750 enum isl_tab_row_sign s;
752 t = tab->row_var[row1];
753 tab->row_var[row1] = tab->row_var[row2];
754 tab->row_var[row2] = t;
755 isl_tab_var_from_row(tab, row1)->index = row1;
756 isl_tab_var_from_row(tab, row2)->index = row2;
757 tab->mat = isl_mat_swap_rows(tab->mat, row1, row2);
759 if (!tab->row_sign)
760 return;
761 s = tab->row_sign[row1];
762 tab->row_sign[row1] = tab->row_sign[row2];
763 tab->row_sign[row2] = s;
766 static int push_union(struct isl_tab *tab,
767 enum isl_tab_undo_type type, union isl_tab_undo_val u) WARN_UNUSED;
768 static int push_union(struct isl_tab *tab,
769 enum isl_tab_undo_type type, union isl_tab_undo_val u)
771 struct isl_tab_undo *undo;
773 if (!tab->need_undo)
774 return 0;
776 undo = isl_alloc_type(tab->mat->ctx, struct isl_tab_undo);
777 if (!undo)
778 return -1;
779 undo->type = type;
780 undo->u = u;
781 undo->next = tab->top;
782 tab->top = undo;
784 return 0;
787 int isl_tab_push_var(struct isl_tab *tab,
788 enum isl_tab_undo_type type, struct isl_tab_var *var)
790 union isl_tab_undo_val u;
791 if (var->is_row)
792 u.var_index = tab->row_var[var->index];
793 else
794 u.var_index = tab->col_var[var->index];
795 return push_union(tab, type, u);
798 int isl_tab_push(struct isl_tab *tab, enum isl_tab_undo_type type)
800 union isl_tab_undo_val u = { 0 };
801 return push_union(tab, type, u);
804 /* Push a record on the undo stack describing the current basic
805 * variables, so that the this state can be restored during rollback.
807 int isl_tab_push_basis(struct isl_tab *tab)
809 int i;
810 union isl_tab_undo_val u;
812 u.col_var = isl_alloc_array(tab->mat->ctx, int, tab->n_col);
813 if (!u.col_var)
814 return -1;
815 for (i = 0; i < tab->n_col; ++i)
816 u.col_var[i] = tab->col_var[i];
817 return push_union(tab, isl_tab_undo_saved_basis, u);
820 int isl_tab_push_callback(struct isl_tab *tab, struct isl_tab_callback *callback)
822 union isl_tab_undo_val u;
823 u.callback = callback;
824 return push_union(tab, isl_tab_undo_callback, u);
827 struct isl_tab *isl_tab_init_samples(struct isl_tab *tab)
829 if (!tab)
830 return NULL;
832 tab->n_sample = 0;
833 tab->n_outside = 0;
834 tab->samples = isl_mat_alloc(tab->mat->ctx, 1, 1 + tab->n_var);
835 if (!tab->samples)
836 goto error;
837 tab->sample_index = isl_alloc_array(tab->mat->ctx, int, 1);
838 if (!tab->sample_index)
839 goto error;
840 return tab;
841 error:
842 isl_tab_free(tab);
843 return NULL;
846 struct isl_tab *isl_tab_add_sample(struct isl_tab *tab,
847 __isl_take isl_vec *sample)
849 if (!tab || !sample)
850 goto error;
852 if (tab->n_sample + 1 > tab->samples->n_row) {
853 int *t = isl_realloc_array(tab->mat->ctx,
854 tab->sample_index, int, tab->n_sample + 1);
855 if (!t)
856 goto error;
857 tab->sample_index = t;
860 tab->samples = isl_mat_extend(tab->samples,
861 tab->n_sample + 1, tab->samples->n_col);
862 if (!tab->samples)
863 goto error;
865 isl_seq_cpy(tab->samples->row[tab->n_sample], sample->el, sample->size);
866 isl_vec_free(sample);
867 tab->sample_index[tab->n_sample] = tab->n_sample;
868 tab->n_sample++;
870 return tab;
871 error:
872 isl_vec_free(sample);
873 isl_tab_free(tab);
874 return NULL;
877 struct isl_tab *isl_tab_drop_sample(struct isl_tab *tab, int s)
879 if (s != tab->n_outside) {
880 int t = tab->sample_index[tab->n_outside];
881 tab->sample_index[tab->n_outside] = tab->sample_index[s];
882 tab->sample_index[s] = t;
883 isl_mat_swap_rows(tab->samples, tab->n_outside, s);
885 tab->n_outside++;
886 if (isl_tab_push(tab, isl_tab_undo_drop_sample) < 0) {
887 isl_tab_free(tab);
888 return NULL;
891 return tab;
894 /* Record the current number of samples so that we can remove newer
895 * samples during a rollback.
897 int isl_tab_save_samples(struct isl_tab *tab)
899 union isl_tab_undo_val u;
901 if (!tab)
902 return -1;
904 u.n = tab->n_sample;
905 return push_union(tab, isl_tab_undo_saved_samples, u);
908 /* Mark row with index "row" as being redundant.
909 * If we may need to undo the operation or if the row represents
910 * a variable of the original problem, the row is kept,
911 * but no longer considered when looking for a pivot row.
912 * Otherwise, the row is simply removed.
914 * The row may be interchanged with some other row. If it
915 * is interchanged with a later row, return 1. Otherwise return 0.
916 * If the rows are checked in order in the calling function,
917 * then a return value of 1 means that the row with the given
918 * row number may now contain a different row that hasn't been checked yet.
920 int isl_tab_mark_redundant(struct isl_tab *tab, int row)
922 struct isl_tab_var *var = isl_tab_var_from_row(tab, row);
923 var->is_redundant = 1;
924 isl_assert(tab->mat->ctx, row >= tab->n_redundant, return -1);
925 if (tab->need_undo || tab->row_var[row] >= 0) {
926 if (tab->row_var[row] >= 0 && !var->is_nonneg) {
927 var->is_nonneg = 1;
928 if (isl_tab_push_var(tab, isl_tab_undo_nonneg, var) < 0)
929 return -1;
931 if (row != tab->n_redundant)
932 swap_rows(tab, row, tab->n_redundant);
933 tab->n_redundant++;
934 return isl_tab_push_var(tab, isl_tab_undo_redundant, var);
935 } else {
936 if (row != tab->n_row - 1)
937 swap_rows(tab, row, tab->n_row - 1);
938 isl_tab_var_from_row(tab, tab->n_row - 1)->index = -1;
939 tab->n_row--;
940 return 1;
944 int isl_tab_mark_empty(struct isl_tab *tab)
946 if (!tab)
947 return -1;
948 if (!tab->empty && tab->need_undo)
949 if (isl_tab_push(tab, isl_tab_undo_empty) < 0)
950 return -1;
951 tab->empty = 1;
952 return 0;
955 int isl_tab_freeze_constraint(struct isl_tab *tab, int con)
957 struct isl_tab_var *var;
959 if (!tab)
960 return -1;
962 var = &tab->con[con];
963 if (var->frozen)
964 return 0;
965 if (var->index < 0)
966 return 0;
967 var->frozen = 1;
969 if (tab->need_undo)
970 return isl_tab_push_var(tab, isl_tab_undo_freeze, var);
972 return 0;
975 /* Update the rows signs after a pivot of "row" and "col", with "row_sgn"
976 * the original sign of the pivot element.
977 * We only keep track of row signs during PILP solving and in this case
978 * we only pivot a row with negative sign (meaning the value is always
979 * non-positive) using a positive pivot element.
981 * For each row j, the new value of the parametric constant is equal to
983 * a_j0 - a_jc a_r0/a_rc
985 * where a_j0 is the original parametric constant, a_rc is the pivot element,
986 * a_r0 is the parametric constant of the pivot row and a_jc is the
987 * pivot column entry of the row j.
988 * Since a_r0 is non-positive and a_rc is positive, the sign of row j
989 * remains the same if a_jc has the same sign as the row j or if
990 * a_jc is zero. In all other cases, we reset the sign to "unknown".
992 static void update_row_sign(struct isl_tab *tab, int row, int col, int row_sgn)
994 int i;
995 struct isl_mat *mat = tab->mat;
996 unsigned off = 2 + tab->M;
998 if (!tab->row_sign)
999 return;
1001 if (tab->row_sign[row] == 0)
1002 return;
1003 isl_assert(mat->ctx, row_sgn > 0, return);
1004 isl_assert(mat->ctx, tab->row_sign[row] == isl_tab_row_neg, return);
1005 tab->row_sign[row] = isl_tab_row_pos;
1006 for (i = 0; i < tab->n_row; ++i) {
1007 int s;
1008 if (i == row)
1009 continue;
1010 s = isl_int_sgn(mat->row[i][off + col]);
1011 if (!s)
1012 continue;
1013 if (!tab->row_sign[i])
1014 continue;
1015 if (s < 0 && tab->row_sign[i] == isl_tab_row_neg)
1016 continue;
1017 if (s > 0 && tab->row_sign[i] == isl_tab_row_pos)
1018 continue;
1019 tab->row_sign[i] = isl_tab_row_unknown;
1023 /* Given a row number "row" and a column number "col", pivot the tableau
1024 * such that the associated variables are interchanged.
1025 * The given row in the tableau expresses
1027 * x_r = a_r0 + \sum_i a_ri x_i
1029 * or
1031 * x_c = 1/a_rc x_r - a_r0/a_rc + sum_{i \ne r} -a_ri/a_rc
1033 * Substituting this equality into the other rows
1035 * x_j = a_j0 + \sum_i a_ji x_i
1037 * with a_jc \ne 0, we obtain
1039 * 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
1041 * The tableau
1043 * n_rc/d_r n_ri/d_r
1044 * n_jc/d_j n_ji/d_j
1046 * where i is any other column and j is any other row,
1047 * is therefore transformed into
1049 * s(n_rc)d_r/|n_rc| -s(n_rc)n_ri/|n_rc|
1050 * 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)
1052 * The transformation is performed along the following steps
1054 * d_r/n_rc n_ri/n_rc
1055 * n_jc/d_j n_ji/d_j
1057 * s(n_rc)d_r/|n_rc| -s(n_rc)n_ri/|n_rc|
1058 * n_jc/d_j n_ji/d_j
1060 * s(n_rc)d_r/|n_rc| -s(n_rc)n_ri/|n_rc|
1061 * n_jc/(|n_rc| d_j) n_ji/(|n_rc| d_j)
1063 * s(n_rc)d_r/|n_rc| -s(n_rc)n_ri/|n_rc|
1064 * n_jc/(|n_rc| d_j) (n_ji |n_rc|)/(|n_rc| d_j)
1066 * s(n_rc)d_r/|n_rc| -s(n_rc)n_ri/|n_rc|
1067 * n_jc/(|n_rc| d_j) (n_ji |n_rc| - s(n_rc)n_jc n_ri)/(|n_rc| d_j)
1069 * s(n_rc)d_r/|n_rc| -s(n_rc)n_ri/|n_rc|
1070 * 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)
1073 int isl_tab_pivot(struct isl_tab *tab, int row, int col)
1075 int i, j;
1076 int sgn;
1077 int t;
1078 struct isl_mat *mat = tab->mat;
1079 struct isl_tab_var *var;
1080 unsigned off = 2 + tab->M;
1082 isl_int_swap(mat->row[row][0], mat->row[row][off + col]);
1083 sgn = isl_int_sgn(mat->row[row][0]);
1084 if (sgn < 0) {
1085 isl_int_neg(mat->row[row][0], mat->row[row][0]);
1086 isl_int_neg(mat->row[row][off + col], mat->row[row][off + col]);
1087 } else
1088 for (j = 0; j < off - 1 + tab->n_col; ++j) {
1089 if (j == off - 1 + col)
1090 continue;
1091 isl_int_neg(mat->row[row][1 + j], mat->row[row][1 + j]);
1093 if (!isl_int_is_one(mat->row[row][0]))
1094 isl_seq_normalize(mat->ctx, mat->row[row], off + tab->n_col);
1095 for (i = 0; i < tab->n_row; ++i) {
1096 if (i == row)
1097 continue;
1098 if (isl_int_is_zero(mat->row[i][off + col]))
1099 continue;
1100 isl_int_mul(mat->row[i][0], mat->row[i][0], mat->row[row][0]);
1101 for (j = 0; j < off - 1 + tab->n_col; ++j) {
1102 if (j == off - 1 + col)
1103 continue;
1104 isl_int_mul(mat->row[i][1 + j],
1105 mat->row[i][1 + j], mat->row[row][0]);
1106 isl_int_addmul(mat->row[i][1 + j],
1107 mat->row[i][off + col], mat->row[row][1 + j]);
1109 isl_int_mul(mat->row[i][off + col],
1110 mat->row[i][off + col], mat->row[row][off + col]);
1111 if (!isl_int_is_one(mat->row[i][0]))
1112 isl_seq_normalize(mat->ctx, mat->row[i], off + tab->n_col);
1114 t = tab->row_var[row];
1115 tab->row_var[row] = tab->col_var[col];
1116 tab->col_var[col] = t;
1117 var = isl_tab_var_from_row(tab, row);
1118 var->is_row = 1;
1119 var->index = row;
1120 var = var_from_col(tab, col);
1121 var->is_row = 0;
1122 var->index = col;
1123 update_row_sign(tab, row, col, sgn);
1124 if (tab->in_undo)
1125 return 0;
1126 for (i = tab->n_redundant; i < tab->n_row; ++i) {
1127 if (isl_int_is_zero(mat->row[i][off + col]))
1128 continue;
1129 if (!isl_tab_var_from_row(tab, i)->frozen &&
1130 isl_tab_row_is_redundant(tab, i)) {
1131 int redo = isl_tab_mark_redundant(tab, i);
1132 if (redo < 0)
1133 return -1;
1134 if (redo)
1135 --i;
1138 return 0;
1141 /* If "var" represents a column variable, then pivot is up (sgn > 0)
1142 * or down (sgn < 0) to a row. The variable is assumed not to be
1143 * unbounded in the specified direction.
1144 * If sgn = 0, then the variable is unbounded in both directions,
1145 * and we pivot with any row we can find.
1147 static int to_row(struct isl_tab *tab, struct isl_tab_var *var, int sign) WARN_UNUSED;
1148 static int to_row(struct isl_tab *tab, struct isl_tab_var *var, int sign)
1150 int r;
1151 unsigned off = 2 + tab->M;
1153 if (var->is_row)
1154 return 0;
1156 if (sign == 0) {
1157 for (r = tab->n_redundant; r < tab->n_row; ++r)
1158 if (!isl_int_is_zero(tab->mat->row[r][off+var->index]))
1159 break;
1160 isl_assert(tab->mat->ctx, r < tab->n_row, return -1);
1161 } else {
1162 r = pivot_row(tab, NULL, sign, var->index);
1163 isl_assert(tab->mat->ctx, r >= 0, return -1);
1166 return isl_tab_pivot(tab, r, var->index);
1169 static void check_table(struct isl_tab *tab)
1171 int i;
1173 if (tab->empty)
1174 return;
1175 for (i = tab->n_redundant; i < tab->n_row; ++i) {
1176 struct isl_tab_var *var;
1177 var = isl_tab_var_from_row(tab, i);
1178 if (!var->is_nonneg)
1179 continue;
1180 if (tab->M) {
1181 assert(!isl_int_is_neg(tab->mat->row[i][2]));
1182 if (isl_int_is_pos(tab->mat->row[i][2]))
1183 continue;
1185 assert(!isl_int_is_neg(tab->mat->row[i][1]));
1189 /* Return the sign of the maximal value of "var".
1190 * If the sign is not negative, then on return from this function,
1191 * the sample value will also be non-negative.
1193 * If "var" is manifestly unbounded wrt positive values, we are done.
1194 * Otherwise, we pivot the variable up to a row if needed
1195 * Then we continue pivoting down until either
1196 * - no more down pivots can be performed
1197 * - the sample value is positive
1198 * - the variable is pivoted into a manifestly unbounded column
1200 static int sign_of_max(struct isl_tab *tab, struct isl_tab_var *var)
1202 int row, col;
1204 if (max_is_manifestly_unbounded(tab, var))
1205 return 1;
1206 if (to_row(tab, var, 1) < 0)
1207 return -2;
1208 while (!isl_int_is_pos(tab->mat->row[var->index][1])) {
1209 find_pivot(tab, var, var, 1, &row, &col);
1210 if (row == -1)
1211 return isl_int_sgn(tab->mat->row[var->index][1]);
1212 if (isl_tab_pivot(tab, row, col) < 0)
1213 return -2;
1214 if (!var->is_row) /* manifestly unbounded */
1215 return 1;
1217 return 1;
1220 int isl_tab_sign_of_max(struct isl_tab *tab, int con)
1222 struct isl_tab_var *var;
1224 if (!tab)
1225 return -2;
1227 var = &tab->con[con];
1228 isl_assert(tab->mat->ctx, !var->is_redundant, return -2);
1229 isl_assert(tab->mat->ctx, !var->is_zero, return -2);
1231 return sign_of_max(tab, var);
1234 static int row_is_neg(struct isl_tab *tab, int row)
1236 if (!tab->M)
1237 return isl_int_is_neg(tab->mat->row[row][1]);
1238 if (isl_int_is_pos(tab->mat->row[row][2]))
1239 return 0;
1240 if (isl_int_is_neg(tab->mat->row[row][2]))
1241 return 1;
1242 return isl_int_is_neg(tab->mat->row[row][1]);
1245 static int row_sgn(struct isl_tab *tab, int row)
1247 if (!tab->M)
1248 return isl_int_sgn(tab->mat->row[row][1]);
1249 if (!isl_int_is_zero(tab->mat->row[row][2]))
1250 return isl_int_sgn(tab->mat->row[row][2]);
1251 else
1252 return isl_int_sgn(tab->mat->row[row][1]);
1255 /* Perform pivots until the row variable "var" has a non-negative
1256 * sample value or until no more upward pivots can be performed.
1257 * Return the sign of the sample value after the pivots have been
1258 * performed.
1260 static int restore_row(struct isl_tab *tab, struct isl_tab_var *var)
1262 int row, col;
1264 while (row_is_neg(tab, var->index)) {
1265 find_pivot(tab, var, var, 1, &row, &col);
1266 if (row == -1)
1267 break;
1268 if (isl_tab_pivot(tab, row, col) < 0)
1269 return -2;
1270 if (!var->is_row) /* manifestly unbounded */
1271 return 1;
1273 return row_sgn(tab, var->index);
1276 /* Perform pivots until we are sure that the row variable "var"
1277 * can attain non-negative values. After return from this
1278 * function, "var" is still a row variable, but its sample
1279 * value may not be non-negative, even if the function returns 1.
1281 static int at_least_zero(struct isl_tab *tab, struct isl_tab_var *var)
1283 int row, col;
1285 while (isl_int_is_neg(tab->mat->row[var->index][1])) {
1286 find_pivot(tab, var, var, 1, &row, &col);
1287 if (row == -1)
1288 break;
1289 if (row == var->index) /* manifestly unbounded */
1290 return 1;
1291 if (isl_tab_pivot(tab, row, col) < 0)
1292 return -1;
1294 return !isl_int_is_neg(tab->mat->row[var->index][1]);
1297 /* Return a negative value if "var" can attain negative values.
1298 * Return a non-negative value otherwise.
1300 * If "var" is manifestly unbounded wrt negative values, we are done.
1301 * Otherwise, if var is in a column, we can pivot it down to a row.
1302 * Then we continue pivoting down until either
1303 * - the pivot would result in a manifestly unbounded column
1304 * => we don't perform the pivot, but simply return -1
1305 * - no more down pivots can be performed
1306 * - the sample value is negative
1307 * If the sample value becomes negative and the variable is supposed
1308 * to be nonnegative, then we undo the last pivot.
1309 * However, if the last pivot has made the pivoting variable
1310 * obviously redundant, then it may have moved to another row.
1311 * In that case we look for upward pivots until we reach a non-negative
1312 * value again.
1314 static int sign_of_min(struct isl_tab *tab, struct isl_tab_var *var)
1316 int row, col;
1317 struct isl_tab_var *pivot_var = NULL;
1319 if (min_is_manifestly_unbounded(tab, var))
1320 return -1;
1321 if (!var->is_row) {
1322 col = var->index;
1323 row = pivot_row(tab, NULL, -1, col);
1324 pivot_var = var_from_col(tab, col);
1325 if (isl_tab_pivot(tab, row, col) < 0)
1326 return -2;
1327 if (var->is_redundant)
1328 return 0;
1329 if (isl_int_is_neg(tab->mat->row[var->index][1])) {
1330 if (var->is_nonneg) {
1331 if (!pivot_var->is_redundant &&
1332 pivot_var->index == row) {
1333 if (isl_tab_pivot(tab, row, col) < 0)
1334 return -2;
1335 } else
1336 if (restore_row(tab, var) < -1)
1337 return -2;
1339 return -1;
1342 if (var->is_redundant)
1343 return 0;
1344 while (!isl_int_is_neg(tab->mat->row[var->index][1])) {
1345 find_pivot(tab, var, var, -1, &row, &col);
1346 if (row == var->index)
1347 return -1;
1348 if (row == -1)
1349 return isl_int_sgn(tab->mat->row[var->index][1]);
1350 pivot_var = var_from_col(tab, col);
1351 if (isl_tab_pivot(tab, row, col) < 0)
1352 return -2;
1353 if (var->is_redundant)
1354 return 0;
1356 if (pivot_var && var->is_nonneg) {
1357 /* pivot back to non-negative value */
1358 if (!pivot_var->is_redundant && pivot_var->index == row) {
1359 if (isl_tab_pivot(tab, row, col) < 0)
1360 return -2;
1361 } else
1362 if (restore_row(tab, var) < -1)
1363 return -2;
1365 return -1;
1368 static int row_at_most_neg_one(struct isl_tab *tab, int row)
1370 if (tab->M) {
1371 if (isl_int_is_pos(tab->mat->row[row][2]))
1372 return 0;
1373 if (isl_int_is_neg(tab->mat->row[row][2]))
1374 return 1;
1376 return isl_int_is_neg(tab->mat->row[row][1]) &&
1377 isl_int_abs_ge(tab->mat->row[row][1],
1378 tab->mat->row[row][0]);
1381 /* Return 1 if "var" can attain values <= -1.
1382 * Return 0 otherwise.
1384 * The sample value of "var" is assumed to be non-negative when the
1385 * the function is called. If 1 is returned then the constraint
1386 * is not redundant and the sample value is made non-negative again before
1387 * the function returns.
1389 int isl_tab_min_at_most_neg_one(struct isl_tab *tab, struct isl_tab_var *var)
1391 int row, col;
1392 struct isl_tab_var *pivot_var;
1394 if (min_is_manifestly_unbounded(tab, var))
1395 return 1;
1396 if (!var->is_row) {
1397 col = var->index;
1398 row = pivot_row(tab, NULL, -1, col);
1399 pivot_var = var_from_col(tab, col);
1400 if (isl_tab_pivot(tab, row, col) < 0)
1401 return -1;
1402 if (var->is_redundant)
1403 return 0;
1404 if (row_at_most_neg_one(tab, var->index)) {
1405 if (var->is_nonneg) {
1406 if (!pivot_var->is_redundant &&
1407 pivot_var->index == row) {
1408 if (isl_tab_pivot(tab, row, col) < 0)
1409 return -1;
1410 } else
1411 if (restore_row(tab, var) < -1)
1412 return -1;
1414 return 1;
1417 if (var->is_redundant)
1418 return 0;
1419 do {
1420 find_pivot(tab, var, var, -1, &row, &col);
1421 if (row == var->index) {
1422 if (restore_row(tab, var) < -1)
1423 return -1;
1424 return 1;
1426 if (row == -1)
1427 return 0;
1428 pivot_var = var_from_col(tab, col);
1429 if (isl_tab_pivot(tab, row, col) < 0)
1430 return -1;
1431 if (var->is_redundant)
1432 return 0;
1433 } while (!row_at_most_neg_one(tab, var->index));
1434 if (var->is_nonneg) {
1435 /* pivot back to non-negative value */
1436 if (!pivot_var->is_redundant && pivot_var->index == row)
1437 if (isl_tab_pivot(tab, row, col) < 0)
1438 return -1;
1439 if (restore_row(tab, var) < -1)
1440 return -1;
1442 return 1;
1445 /* Return 1 if "var" can attain values >= 1.
1446 * Return 0 otherwise.
1448 static int at_least_one(struct isl_tab *tab, struct isl_tab_var *var)
1450 int row, col;
1451 isl_int *r;
1453 if (max_is_manifestly_unbounded(tab, var))
1454 return 1;
1455 if (to_row(tab, var, 1) < 0)
1456 return -1;
1457 r = tab->mat->row[var->index];
1458 while (isl_int_lt(r[1], r[0])) {
1459 find_pivot(tab, var, var, 1, &row, &col);
1460 if (row == -1)
1461 return isl_int_ge(r[1], r[0]);
1462 if (row == var->index) /* manifestly unbounded */
1463 return 1;
1464 if (isl_tab_pivot(tab, row, col) < 0)
1465 return -1;
1467 return 1;
1470 static void swap_cols(struct isl_tab *tab, int col1, int col2)
1472 int t;
1473 unsigned off = 2 + tab->M;
1474 t = tab->col_var[col1];
1475 tab->col_var[col1] = tab->col_var[col2];
1476 tab->col_var[col2] = t;
1477 var_from_col(tab, col1)->index = col1;
1478 var_from_col(tab, col2)->index = col2;
1479 tab->mat = isl_mat_swap_cols(tab->mat, off + col1, off + col2);
1482 /* Mark column with index "col" as representing a zero variable.
1483 * If we may need to undo the operation the column is kept,
1484 * but no longer considered.
1485 * Otherwise, the column is simply removed.
1487 * The column may be interchanged with some other column. If it
1488 * is interchanged with a later column, return 1. Otherwise return 0.
1489 * If the columns are checked in order in the calling function,
1490 * then a return value of 1 means that the column with the given
1491 * column number may now contain a different column that
1492 * hasn't been checked yet.
1494 int isl_tab_kill_col(struct isl_tab *tab, int col)
1496 var_from_col(tab, col)->is_zero = 1;
1497 if (tab->need_undo) {
1498 if (isl_tab_push_var(tab, isl_tab_undo_zero,
1499 var_from_col(tab, col)) < 0)
1500 return -1;
1501 if (col != tab->n_dead)
1502 swap_cols(tab, col, tab->n_dead);
1503 tab->n_dead++;
1504 return 0;
1505 } else {
1506 if (col != tab->n_col - 1)
1507 swap_cols(tab, col, tab->n_col - 1);
1508 var_from_col(tab, tab->n_col - 1)->index = -1;
1509 tab->n_col--;
1510 return 1;
1514 /* Row variable "var" is non-negative and cannot attain any values
1515 * larger than zero. This means that the coefficients of the unrestricted
1516 * column variables are zero and that the coefficients of the non-negative
1517 * column variables are zero or negative.
1518 * Each of the non-negative variables with a negative coefficient can
1519 * then also be written as the negative sum of non-negative variables
1520 * and must therefore also be zero.
1522 static int close_row(struct isl_tab *tab, struct isl_tab_var *var) WARN_UNUSED;
1523 static int close_row(struct isl_tab *tab, struct isl_tab_var *var)
1525 int j;
1526 struct isl_mat *mat = tab->mat;
1527 unsigned off = 2 + tab->M;
1529 isl_assert(tab->mat->ctx, var->is_nonneg, return -1);
1530 var->is_zero = 1;
1531 if (tab->need_undo)
1532 if (isl_tab_push_var(tab, isl_tab_undo_zero, var) < 0)
1533 return -1;
1534 for (j = tab->n_dead; j < tab->n_col; ++j) {
1535 if (isl_int_is_zero(mat->row[var->index][off + j]))
1536 continue;
1537 isl_assert(tab->mat->ctx,
1538 isl_int_is_neg(mat->row[var->index][off + j]), return -1);
1539 if (isl_tab_kill_col(tab, j))
1540 --j;
1542 if (isl_tab_mark_redundant(tab, var->index) < 0)
1543 return -1;
1544 return 0;
1547 /* Add a constraint to the tableau and allocate a row for it.
1548 * Return the index into the constraint array "con".
1550 int isl_tab_allocate_con(struct isl_tab *tab)
1552 int r;
1554 isl_assert(tab->mat->ctx, tab->n_row < tab->mat->n_row, return -1);
1555 isl_assert(tab->mat->ctx, tab->n_con < tab->max_con, return -1);
1557 r = tab->n_con;
1558 tab->con[r].index = tab->n_row;
1559 tab->con[r].is_row = 1;
1560 tab->con[r].is_nonneg = 0;
1561 tab->con[r].is_zero = 0;
1562 tab->con[r].is_redundant = 0;
1563 tab->con[r].frozen = 0;
1564 tab->con[r].negated = 0;
1565 tab->row_var[tab->n_row] = ~r;
1567 tab->n_row++;
1568 tab->n_con++;
1569 if (isl_tab_push_var(tab, isl_tab_undo_allocate, &tab->con[r]) < 0)
1570 return -1;
1572 return r;
1575 /* Add a variable to the tableau and allocate a column for it.
1576 * Return the index into the variable array "var".
1578 int isl_tab_allocate_var(struct isl_tab *tab)
1580 int r;
1581 int i;
1582 unsigned off = 2 + tab->M;
1584 isl_assert(tab->mat->ctx, tab->n_col < tab->mat->n_col, return -1);
1585 isl_assert(tab->mat->ctx, tab->n_var < tab->max_var, return -1);
1587 r = tab->n_var;
1588 tab->var[r].index = tab->n_col;
1589 tab->var[r].is_row = 0;
1590 tab->var[r].is_nonneg = 0;
1591 tab->var[r].is_zero = 0;
1592 tab->var[r].is_redundant = 0;
1593 tab->var[r].frozen = 0;
1594 tab->var[r].negated = 0;
1595 tab->col_var[tab->n_col] = r;
1597 for (i = 0; i < tab->n_row; ++i)
1598 isl_int_set_si(tab->mat->row[i][off + tab->n_col], 0);
1600 tab->n_var++;
1601 tab->n_col++;
1602 if (isl_tab_push_var(tab, isl_tab_undo_allocate, &tab->var[r]) < 0)
1603 return -1;
1605 return r;
1608 /* Add a row to the tableau. The row is given as an affine combination
1609 * of the original variables and needs to be expressed in terms of the
1610 * column variables.
1612 * We add each term in turn.
1613 * If r = n/d_r is the current sum and we need to add k x, then
1614 * if x is a column variable, we increase the numerator of
1615 * this column by k d_r
1616 * if x = f/d_x is a row variable, then the new representation of r is
1618 * n k f d_x/g n + d_r/g k f m/d_r n + m/d_g k f
1619 * --- + --- = ------------------- = -------------------
1620 * d_r d_r d_r d_x/g m
1622 * with g the gcd of d_r and d_x and m the lcm of d_r and d_x.
1624 int isl_tab_add_row(struct isl_tab *tab, isl_int *line)
1626 int i;
1627 int r;
1628 isl_int *row;
1629 isl_int a, b;
1630 unsigned off = 2 + tab->M;
1632 r = isl_tab_allocate_con(tab);
1633 if (r < 0)
1634 return -1;
1636 isl_int_init(a);
1637 isl_int_init(b);
1638 row = tab->mat->row[tab->con[r].index];
1639 isl_int_set_si(row[0], 1);
1640 isl_int_set(row[1], line[0]);
1641 isl_seq_clr(row + 2, tab->M + tab->n_col);
1642 for (i = 0; i < tab->n_var; ++i) {
1643 if (tab->var[i].is_zero)
1644 continue;
1645 if (tab->var[i].is_row) {
1646 isl_int_lcm(a,
1647 row[0], tab->mat->row[tab->var[i].index][0]);
1648 isl_int_swap(a, row[0]);
1649 isl_int_divexact(a, row[0], a);
1650 isl_int_divexact(b,
1651 row[0], tab->mat->row[tab->var[i].index][0]);
1652 isl_int_mul(b, b, line[1 + i]);
1653 isl_seq_combine(row + 1, a, row + 1,
1654 b, tab->mat->row[tab->var[i].index] + 1,
1655 1 + tab->M + tab->n_col);
1656 } else
1657 isl_int_addmul(row[off + tab->var[i].index],
1658 line[1 + i], row[0]);
1659 if (tab->M && i >= tab->n_param && i < tab->n_var - tab->n_div)
1660 isl_int_submul(row[2], line[1 + i], row[0]);
1662 isl_seq_normalize(tab->mat->ctx, row, off + tab->n_col);
1663 isl_int_clear(a);
1664 isl_int_clear(b);
1666 if (tab->row_sign)
1667 tab->row_sign[tab->con[r].index] = isl_tab_row_unknown;
1669 return r;
1672 static int drop_row(struct isl_tab *tab, int row)
1674 isl_assert(tab->mat->ctx, ~tab->row_var[row] == tab->n_con - 1, return -1);
1675 if (row != tab->n_row - 1)
1676 swap_rows(tab, row, tab->n_row - 1);
1677 tab->n_row--;
1678 tab->n_con--;
1679 return 0;
1682 static int drop_col(struct isl_tab *tab, int col)
1684 isl_assert(tab->mat->ctx, tab->col_var[col] == tab->n_var - 1, return -1);
1685 if (col != tab->n_col - 1)
1686 swap_cols(tab, col, tab->n_col - 1);
1687 tab->n_col--;
1688 tab->n_var--;
1689 return 0;
1692 /* Add inequality "ineq" and check if it conflicts with the
1693 * previously added constraints or if it is obviously redundant.
1695 int isl_tab_add_ineq(struct isl_tab *tab, isl_int *ineq)
1697 int r;
1698 int sgn;
1699 isl_int cst;
1701 if (!tab)
1702 return -1;
1703 if (tab->bmap) {
1704 struct isl_basic_map *bmap = tab->bmap;
1706 isl_assert(tab->mat->ctx, tab->n_eq == bmap->n_eq, return -1);
1707 isl_assert(tab->mat->ctx,
1708 tab->n_con == bmap->n_eq + bmap->n_ineq, return -1);
1709 tab->bmap = isl_basic_map_add_ineq(tab->bmap, ineq);
1710 if (isl_tab_push(tab, isl_tab_undo_bmap_ineq) < 0)
1711 return -1;
1712 if (!tab->bmap)
1713 return -1;
1715 if (tab->cone) {
1716 isl_int_init(cst);
1717 isl_int_swap(ineq[0], cst);
1719 r = isl_tab_add_row(tab, ineq);
1720 if (tab->cone) {
1721 isl_int_swap(ineq[0], cst);
1722 isl_int_clear(cst);
1724 if (r < 0)
1725 return -1;
1726 tab->con[r].is_nonneg = 1;
1727 if (isl_tab_push_var(tab, isl_tab_undo_nonneg, &tab->con[r]) < 0)
1728 return -1;
1729 if (isl_tab_row_is_redundant(tab, tab->con[r].index)) {
1730 if (isl_tab_mark_redundant(tab, tab->con[r].index) < 0)
1731 return -1;
1732 return 0;
1735 sgn = restore_row(tab, &tab->con[r]);
1736 if (sgn < -1)
1737 return -1;
1738 if (sgn < 0)
1739 return isl_tab_mark_empty(tab);
1740 if (tab->con[r].is_row && isl_tab_row_is_redundant(tab, tab->con[r].index))
1741 if (isl_tab_mark_redundant(tab, tab->con[r].index) < 0)
1742 return -1;
1743 return 0;
1746 /* Pivot a non-negative variable down until it reaches the value zero
1747 * and then pivot the variable into a column position.
1749 static int to_col(struct isl_tab *tab, struct isl_tab_var *var) WARN_UNUSED;
1750 static int to_col(struct isl_tab *tab, struct isl_tab_var *var)
1752 int i;
1753 int row, col;
1754 unsigned off = 2 + tab->M;
1756 if (!var->is_row)
1757 return 0;
1759 while (isl_int_is_pos(tab->mat->row[var->index][1])) {
1760 find_pivot(tab, var, NULL, -1, &row, &col);
1761 isl_assert(tab->mat->ctx, row != -1, return -1);
1762 if (isl_tab_pivot(tab, row, col) < 0)
1763 return -1;
1764 if (!var->is_row)
1765 return 0;
1768 for (i = tab->n_dead; i < tab->n_col; ++i)
1769 if (!isl_int_is_zero(tab->mat->row[var->index][off + i]))
1770 break;
1772 isl_assert(tab->mat->ctx, i < tab->n_col, return -1);
1773 if (isl_tab_pivot(tab, var->index, i) < 0)
1774 return -1;
1776 return 0;
1779 /* We assume Gaussian elimination has been performed on the equalities.
1780 * The equalities can therefore never conflict.
1781 * Adding the equalities is currently only really useful for a later call
1782 * to isl_tab_ineq_type.
1784 static struct isl_tab *add_eq(struct isl_tab *tab, isl_int *eq)
1786 int i;
1787 int r;
1789 if (!tab)
1790 return NULL;
1791 r = isl_tab_add_row(tab, eq);
1792 if (r < 0)
1793 goto error;
1795 r = tab->con[r].index;
1796 i = isl_seq_first_non_zero(tab->mat->row[r] + 2 + tab->M + tab->n_dead,
1797 tab->n_col - tab->n_dead);
1798 isl_assert(tab->mat->ctx, i >= 0, goto error);
1799 i += tab->n_dead;
1800 if (isl_tab_pivot(tab, r, i) < 0)
1801 goto error;
1802 if (isl_tab_kill_col(tab, i) < 0)
1803 goto error;
1804 tab->n_eq++;
1806 return tab;
1807 error:
1808 isl_tab_free(tab);
1809 return NULL;
1812 static int row_is_manifestly_zero(struct isl_tab *tab, int row)
1814 unsigned off = 2 + tab->M;
1816 if (!isl_int_is_zero(tab->mat->row[row][1]))
1817 return 0;
1818 if (tab->M && !isl_int_is_zero(tab->mat->row[row][2]))
1819 return 0;
1820 return isl_seq_first_non_zero(tab->mat->row[row] + off + tab->n_dead,
1821 tab->n_col - tab->n_dead) == -1;
1824 /* Add an equality that is known to be valid for the given tableau.
1826 struct isl_tab *isl_tab_add_valid_eq(struct isl_tab *tab, isl_int *eq)
1828 struct isl_tab_var *var;
1829 int r;
1831 if (!tab)
1832 return NULL;
1833 r = isl_tab_add_row(tab, eq);
1834 if (r < 0)
1835 goto error;
1837 var = &tab->con[r];
1838 r = var->index;
1839 if (row_is_manifestly_zero(tab, r)) {
1840 var->is_zero = 1;
1841 if (isl_tab_mark_redundant(tab, r) < 0)
1842 goto error;
1843 return tab;
1846 if (isl_int_is_neg(tab->mat->row[r][1])) {
1847 isl_seq_neg(tab->mat->row[r] + 1, tab->mat->row[r] + 1,
1848 1 + tab->n_col);
1849 var->negated = 1;
1851 var->is_nonneg = 1;
1852 if (to_col(tab, var) < 0)
1853 goto error;
1854 var->is_nonneg = 0;
1855 if (isl_tab_kill_col(tab, var->index) < 0)
1856 goto error;
1858 return tab;
1859 error:
1860 isl_tab_free(tab);
1861 return NULL;
1864 static int add_zero_row(struct isl_tab *tab)
1866 int r;
1867 isl_int *row;
1869 r = isl_tab_allocate_con(tab);
1870 if (r < 0)
1871 return -1;
1873 row = tab->mat->row[tab->con[r].index];
1874 isl_seq_clr(row + 1, 1 + tab->M + tab->n_col);
1875 isl_int_set_si(row[0], 1);
1877 return r;
1880 /* Add equality "eq" and check if it conflicts with the
1881 * previously added constraints or if it is obviously redundant.
1883 struct isl_tab *isl_tab_add_eq(struct isl_tab *tab, isl_int *eq)
1885 struct isl_tab_undo *snap = NULL;
1886 struct isl_tab_var *var;
1887 int r;
1888 int row;
1889 int sgn;
1890 isl_int cst;
1892 if (!tab)
1893 return NULL;
1894 isl_assert(tab->mat->ctx, !tab->M, goto error);
1896 if (tab->need_undo)
1897 snap = isl_tab_snap(tab);
1899 if (tab->cone) {
1900 isl_int_init(cst);
1901 isl_int_swap(eq[0], cst);
1903 r = isl_tab_add_row(tab, eq);
1904 if (tab->cone) {
1905 isl_int_swap(eq[0], cst);
1906 isl_int_clear(cst);
1908 if (r < 0)
1909 goto error;
1911 var = &tab->con[r];
1912 row = var->index;
1913 if (row_is_manifestly_zero(tab, row)) {
1914 if (snap) {
1915 if (isl_tab_rollback(tab, snap) < 0)
1916 goto error;
1917 } else
1918 drop_row(tab, row);
1919 return tab;
1922 if (tab->bmap) {
1923 tab->bmap = isl_basic_map_add_ineq(tab->bmap, eq);
1924 if (isl_tab_push(tab, isl_tab_undo_bmap_ineq) < 0)
1925 goto error;
1926 isl_seq_neg(eq, eq, 1 + tab->n_var);
1927 tab->bmap = isl_basic_map_add_ineq(tab->bmap, eq);
1928 isl_seq_neg(eq, eq, 1 + tab->n_var);
1929 if (isl_tab_push(tab, isl_tab_undo_bmap_ineq) < 0)
1930 goto error;
1931 if (!tab->bmap)
1932 goto error;
1933 if (add_zero_row(tab) < 0)
1934 goto error;
1937 sgn = isl_int_sgn(tab->mat->row[row][1]);
1939 if (sgn > 0) {
1940 isl_seq_neg(tab->mat->row[row] + 1, tab->mat->row[row] + 1,
1941 1 + tab->n_col);
1942 var->negated = 1;
1943 sgn = -1;
1946 if (sgn < 0) {
1947 sgn = sign_of_max(tab, var);
1948 if (sgn < -1)
1949 goto error;
1950 if (sgn < 0) {
1951 if (isl_tab_mark_empty(tab) < 0)
1952 goto error;
1953 return tab;
1957 var->is_nonneg = 1;
1958 if (to_col(tab, var) < 0)
1959 goto error;
1960 var->is_nonneg = 0;
1961 if (isl_tab_kill_col(tab, var->index) < 0)
1962 goto error;
1964 return tab;
1965 error:
1966 isl_tab_free(tab);
1967 return NULL;
1970 /* Construct and return an inequality that expresses an upper bound
1971 * on the given div.
1972 * In particular, if the div is given by
1974 * d = floor(e/m)
1976 * then the inequality expresses
1978 * m d <= e
1980 static struct isl_vec *ineq_for_div(struct isl_basic_map *bmap, unsigned div)
1982 unsigned total;
1983 unsigned div_pos;
1984 struct isl_vec *ineq;
1986 if (!bmap)
1987 return NULL;
1989 total = isl_basic_map_total_dim(bmap);
1990 div_pos = 1 + total - bmap->n_div + div;
1992 ineq = isl_vec_alloc(bmap->ctx, 1 + total);
1993 if (!ineq)
1994 return NULL;
1996 isl_seq_cpy(ineq->el, bmap->div[div] + 1, 1 + total);
1997 isl_int_neg(ineq->el[div_pos], bmap->div[div][0]);
1998 return ineq;
2001 /* For a div d = floor(f/m), add the constraints
2003 * f - m d >= 0
2004 * -(f-(m-1)) + m d >= 0
2006 * Note that the second constraint is the negation of
2008 * f - m d >= m
2010 * If add_ineq is not NULL, then this function is used
2011 * instead of isl_tab_add_ineq to effectively add the inequalities.
2013 static int add_div_constraints(struct isl_tab *tab, unsigned div,
2014 int (*add_ineq)(void *user, isl_int *), void *user)
2016 unsigned total;
2017 unsigned div_pos;
2018 struct isl_vec *ineq;
2020 total = isl_basic_map_total_dim(tab->bmap);
2021 div_pos = 1 + total - tab->bmap->n_div + div;
2023 ineq = ineq_for_div(tab->bmap, div);
2024 if (!ineq)
2025 goto error;
2027 if (add_ineq) {
2028 if (add_ineq(user, ineq->el) < 0)
2029 goto error;
2030 } else {
2031 if (isl_tab_add_ineq(tab, ineq->el) < 0)
2032 goto error;
2035 isl_seq_neg(ineq->el, tab->bmap->div[div] + 1, 1 + total);
2036 isl_int_set(ineq->el[div_pos], tab->bmap->div[div][0]);
2037 isl_int_add(ineq->el[0], ineq->el[0], ineq->el[div_pos]);
2038 isl_int_sub_ui(ineq->el[0], ineq->el[0], 1);
2040 if (add_ineq) {
2041 if (add_ineq(user, ineq->el) < 0)
2042 goto error;
2043 } else {
2044 if (isl_tab_add_ineq(tab, ineq->el) < 0)
2045 goto error;
2048 isl_vec_free(ineq);
2050 return 0;
2051 error:
2052 isl_vec_free(ineq);
2053 return -1;
2056 /* Add an extra div, prescrived by "div" to the tableau and
2057 * the associated bmap (which is assumed to be non-NULL).
2059 * If add_ineq is not NULL, then this function is used instead
2060 * of isl_tab_add_ineq to add the div constraints.
2061 * This complication is needed because the code in isl_tab_pip
2062 * wants to perform some extra processing when an inequality
2063 * is added to the tableau.
2065 int isl_tab_add_div(struct isl_tab *tab, __isl_keep isl_vec *div,
2066 int (*add_ineq)(void *user, isl_int *), void *user)
2068 int i;
2069 int r;
2070 int k;
2071 int nonneg;
2073 if (!tab || !div)
2074 return -1;
2076 isl_assert(tab->mat->ctx, tab->bmap, return -1);
2078 for (i = 0; i < tab->n_var; ++i) {
2079 if (isl_int_is_neg(div->el[2 + i]))
2080 break;
2081 if (isl_int_is_zero(div->el[2 + i]))
2082 continue;
2083 if (!tab->var[i].is_nonneg)
2084 break;
2086 nonneg = i == tab->n_var && !isl_int_is_neg(div->el[1]);
2088 if (isl_tab_extend_cons(tab, 3) < 0)
2089 return -1;
2090 if (isl_tab_extend_vars(tab, 1) < 0)
2091 return -1;
2092 r = isl_tab_allocate_var(tab);
2093 if (r < 0)
2094 return -1;
2096 if (nonneg)
2097 tab->var[r].is_nonneg = 1;
2099 tab->bmap = isl_basic_map_extend_dim(tab->bmap,
2100 isl_basic_map_get_dim(tab->bmap), 1, 0, 2);
2101 k = isl_basic_map_alloc_div(tab->bmap);
2102 if (k < 0)
2103 return -1;
2104 isl_seq_cpy(tab->bmap->div[k], div->el, div->size);
2105 if (isl_tab_push(tab, isl_tab_undo_bmap_div) < 0)
2106 return -1;
2108 if (add_div_constraints(tab, k, add_ineq, user) < 0)
2109 return -1;
2111 return r;
2114 struct isl_tab *isl_tab_from_basic_map(struct isl_basic_map *bmap)
2116 int i;
2117 struct isl_tab *tab;
2119 if (!bmap)
2120 return NULL;
2121 tab = isl_tab_alloc(bmap->ctx,
2122 isl_basic_map_total_dim(bmap) + bmap->n_ineq + 1,
2123 isl_basic_map_total_dim(bmap), 0);
2124 if (!tab)
2125 return NULL;
2126 tab->rational = ISL_F_ISSET(bmap, ISL_BASIC_MAP_RATIONAL);
2127 if (ISL_F_ISSET(bmap, ISL_BASIC_MAP_EMPTY)) {
2128 if (isl_tab_mark_empty(tab) < 0)
2129 goto error;
2130 return tab;
2132 for (i = 0; i < bmap->n_eq; ++i) {
2133 tab = add_eq(tab, bmap->eq[i]);
2134 if (!tab)
2135 return tab;
2137 for (i = 0; i < bmap->n_ineq; ++i) {
2138 if (isl_tab_add_ineq(tab, bmap->ineq[i]) < 0)
2139 goto error;
2140 if (tab->empty)
2141 return tab;
2143 return tab;
2144 error:
2145 isl_tab_free(tab);
2146 return NULL;
2149 struct isl_tab *isl_tab_from_basic_set(struct isl_basic_set *bset)
2151 return isl_tab_from_basic_map((struct isl_basic_map *)bset);
2154 /* Construct a tableau corresponding to the recession cone of "bset".
2156 struct isl_tab *isl_tab_from_recession_cone(__isl_keep isl_basic_set *bset,
2157 int parametric)
2159 isl_int cst;
2160 int i;
2161 struct isl_tab *tab;
2162 unsigned offset = 0;
2164 if (!bset)
2165 return NULL;
2166 if (parametric)
2167 offset = isl_basic_set_dim(bset, isl_dim_param);
2168 tab = isl_tab_alloc(bset->ctx, bset->n_eq + bset->n_ineq,
2169 isl_basic_set_total_dim(bset) - offset, 0);
2170 if (!tab)
2171 return NULL;
2172 tab->rational = ISL_F_ISSET(bset, ISL_BASIC_SET_RATIONAL);
2173 tab->cone = 1;
2175 isl_int_init(cst);
2176 for (i = 0; i < bset->n_eq; ++i) {
2177 isl_int_swap(bset->eq[i][offset], cst);
2178 if (offset > 0)
2179 tab = isl_tab_add_eq(tab, bset->eq[i] + offset);
2180 else
2181 tab = add_eq(tab, bset->eq[i]);
2182 isl_int_swap(bset->eq[i][offset], cst);
2183 if (!tab)
2184 goto done;
2186 for (i = 0; i < bset->n_ineq; ++i) {
2187 int r;
2188 isl_int_swap(bset->ineq[i][offset], cst);
2189 r = isl_tab_add_row(tab, bset->ineq[i] + offset);
2190 isl_int_swap(bset->ineq[i][offset], cst);
2191 if (r < 0)
2192 goto error;
2193 tab->con[r].is_nonneg = 1;
2194 if (isl_tab_push_var(tab, isl_tab_undo_nonneg, &tab->con[r]) < 0)
2195 goto error;
2197 done:
2198 isl_int_clear(cst);
2199 return tab;
2200 error:
2201 isl_int_clear(cst);
2202 isl_tab_free(tab);
2203 return NULL;
2206 /* Assuming "tab" is the tableau of a cone, check if the cone is
2207 * bounded, i.e., if it is empty or only contains the origin.
2209 int isl_tab_cone_is_bounded(struct isl_tab *tab)
2211 int i;
2213 if (!tab)
2214 return -1;
2215 if (tab->empty)
2216 return 1;
2217 if (tab->n_dead == tab->n_col)
2218 return 1;
2220 for (;;) {
2221 for (i = tab->n_redundant; i < tab->n_row; ++i) {
2222 struct isl_tab_var *var;
2223 int sgn;
2224 var = isl_tab_var_from_row(tab, i);
2225 if (!var->is_nonneg)
2226 continue;
2227 sgn = sign_of_max(tab, var);
2228 if (sgn < -1)
2229 return -1;
2230 if (sgn != 0)
2231 return 0;
2232 if (close_row(tab, var) < 0)
2233 return -1;
2234 break;
2236 if (tab->n_dead == tab->n_col)
2237 return 1;
2238 if (i == tab->n_row)
2239 return 0;
2243 int isl_tab_sample_is_integer(struct isl_tab *tab)
2245 int i;
2247 if (!tab)
2248 return -1;
2250 for (i = 0; i < tab->n_var; ++i) {
2251 int row;
2252 if (!tab->var[i].is_row)
2253 continue;
2254 row = tab->var[i].index;
2255 if (!isl_int_is_divisible_by(tab->mat->row[row][1],
2256 tab->mat->row[row][0]))
2257 return 0;
2259 return 1;
2262 static struct isl_vec *extract_integer_sample(struct isl_tab *tab)
2264 int i;
2265 struct isl_vec *vec;
2267 vec = isl_vec_alloc(tab->mat->ctx, 1 + tab->n_var);
2268 if (!vec)
2269 return NULL;
2271 isl_int_set_si(vec->block.data[0], 1);
2272 for (i = 0; i < tab->n_var; ++i) {
2273 if (!tab->var[i].is_row)
2274 isl_int_set_si(vec->block.data[1 + i], 0);
2275 else {
2276 int row = tab->var[i].index;
2277 isl_int_divexact(vec->block.data[1 + i],
2278 tab->mat->row[row][1], tab->mat->row[row][0]);
2282 return vec;
2285 struct isl_vec *isl_tab_get_sample_value(struct isl_tab *tab)
2287 int i;
2288 struct isl_vec *vec;
2289 isl_int m;
2291 if (!tab)
2292 return NULL;
2294 vec = isl_vec_alloc(tab->mat->ctx, 1 + tab->n_var);
2295 if (!vec)
2296 return NULL;
2298 isl_int_init(m);
2300 isl_int_set_si(vec->block.data[0], 1);
2301 for (i = 0; i < tab->n_var; ++i) {
2302 int row;
2303 if (!tab->var[i].is_row) {
2304 isl_int_set_si(vec->block.data[1 + i], 0);
2305 continue;
2307 row = tab->var[i].index;
2308 isl_int_gcd(m, vec->block.data[0], tab->mat->row[row][0]);
2309 isl_int_divexact(m, tab->mat->row[row][0], m);
2310 isl_seq_scale(vec->block.data, vec->block.data, m, 1 + i);
2311 isl_int_divexact(m, vec->block.data[0], tab->mat->row[row][0]);
2312 isl_int_mul(vec->block.data[1 + i], m, tab->mat->row[row][1]);
2314 vec = isl_vec_normalize(vec);
2316 isl_int_clear(m);
2317 return vec;
2320 /* Update "bmap" based on the results of the tableau "tab".
2321 * In particular, implicit equalities are made explicit, redundant constraints
2322 * are removed and if the sample value happens to be integer, it is stored
2323 * in "bmap" (unless "bmap" already had an integer sample).
2325 * The tableau is assumed to have been created from "bmap" using
2326 * isl_tab_from_basic_map.
2328 struct isl_basic_map *isl_basic_map_update_from_tab(struct isl_basic_map *bmap,
2329 struct isl_tab *tab)
2331 int i;
2332 unsigned n_eq;
2334 if (!bmap)
2335 return NULL;
2336 if (!tab)
2337 return bmap;
2339 n_eq = tab->n_eq;
2340 if (tab->empty)
2341 bmap = isl_basic_map_set_to_empty(bmap);
2342 else
2343 for (i = bmap->n_ineq - 1; i >= 0; --i) {
2344 if (isl_tab_is_equality(tab, n_eq + i))
2345 isl_basic_map_inequality_to_equality(bmap, i);
2346 else if (isl_tab_is_redundant(tab, n_eq + i))
2347 isl_basic_map_drop_inequality(bmap, i);
2349 if (bmap->n_eq != n_eq)
2350 isl_basic_map_gauss(bmap, NULL);
2351 if (!tab->rational &&
2352 !bmap->sample && isl_tab_sample_is_integer(tab))
2353 bmap->sample = extract_integer_sample(tab);
2354 return bmap;
2357 struct isl_basic_set *isl_basic_set_update_from_tab(struct isl_basic_set *bset,
2358 struct isl_tab *tab)
2360 return (struct isl_basic_set *)isl_basic_map_update_from_tab(
2361 (struct isl_basic_map *)bset, tab);
2364 /* Given a non-negative variable "var", add a new non-negative variable
2365 * that is the opposite of "var", ensuring that var can only attain the
2366 * value zero.
2367 * If var = n/d is a row variable, then the new variable = -n/d.
2368 * If var is a column variables, then the new variable = -var.
2369 * If the new variable cannot attain non-negative values, then
2370 * the resulting tableau is empty.
2371 * Otherwise, we know the value will be zero and we close the row.
2373 static int cut_to_hyperplane(struct isl_tab *tab, struct isl_tab_var *var)
2375 unsigned r;
2376 isl_int *row;
2377 int sgn;
2378 unsigned off = 2 + tab->M;
2380 if (var->is_zero)
2381 return 0;
2382 isl_assert(tab->mat->ctx, !var->is_redundant, return -1);
2383 isl_assert(tab->mat->ctx, var->is_nonneg, return -1);
2385 if (isl_tab_extend_cons(tab, 1) < 0)
2386 return -1;
2388 r = tab->n_con;
2389 tab->con[r].index = tab->n_row;
2390 tab->con[r].is_row = 1;
2391 tab->con[r].is_nonneg = 0;
2392 tab->con[r].is_zero = 0;
2393 tab->con[r].is_redundant = 0;
2394 tab->con[r].frozen = 0;
2395 tab->con[r].negated = 0;
2396 tab->row_var[tab->n_row] = ~r;
2397 row = tab->mat->row[tab->n_row];
2399 if (var->is_row) {
2400 isl_int_set(row[0], tab->mat->row[var->index][0]);
2401 isl_seq_neg(row + 1,
2402 tab->mat->row[var->index] + 1, 1 + tab->n_col);
2403 } else {
2404 isl_int_set_si(row[0], 1);
2405 isl_seq_clr(row + 1, 1 + tab->n_col);
2406 isl_int_set_si(row[off + var->index], -1);
2409 tab->n_row++;
2410 tab->n_con++;
2411 if (isl_tab_push_var(tab, isl_tab_undo_allocate, &tab->con[r]) < 0)
2412 return -1;
2414 sgn = sign_of_max(tab, &tab->con[r]);
2415 if (sgn < -1)
2416 return -1;
2417 if (sgn < 0) {
2418 if (isl_tab_mark_empty(tab) < 0)
2419 return -1;
2420 return 0;
2422 tab->con[r].is_nonneg = 1;
2423 if (isl_tab_push_var(tab, isl_tab_undo_nonneg, &tab->con[r]) < 0)
2424 return -1;
2425 /* sgn == 0 */
2426 if (close_row(tab, &tab->con[r]) < 0)
2427 return -1;
2429 return 0;
2432 /* Given a tableau "tab" and an inequality constraint "con" of the tableau,
2433 * relax the inequality by one. That is, the inequality r >= 0 is replaced
2434 * by r' = r + 1 >= 0.
2435 * If r is a row variable, we simply increase the constant term by one
2436 * (taking into account the denominator).
2437 * If r is a column variable, then we need to modify each row that
2438 * refers to r = r' - 1 by substituting this equality, effectively
2439 * subtracting the coefficient of the column from the constant.
2440 * We should only do this if the minimum is manifestly unbounded,
2441 * however. Otherwise, we may end up with negative sample values
2442 * for non-negative variables.
2443 * So, if r is a column variable with a minimum that is not
2444 * manifestly unbounded, then we need to move it to a row.
2445 * However, the sample value of this row may be negative,
2446 * even after the relaxation, so we need to restore it.
2447 * We therefore prefer to pivot a column up to a row, if possible.
2449 struct isl_tab *isl_tab_relax(struct isl_tab *tab, int con)
2451 struct isl_tab_var *var;
2452 unsigned off = 2 + tab->M;
2454 if (!tab)
2455 return NULL;
2457 var = &tab->con[con];
2459 if (!var->is_row && !max_is_manifestly_unbounded(tab, var))
2460 if (to_row(tab, var, 1) < 0)
2461 goto error;
2462 if (!var->is_row && !min_is_manifestly_unbounded(tab, var))
2463 if (to_row(tab, var, -1) < 0)
2464 goto error;
2466 if (var->is_row) {
2467 isl_int_add(tab->mat->row[var->index][1],
2468 tab->mat->row[var->index][1], tab->mat->row[var->index][0]);
2469 if (restore_row(tab, var) < 0)
2470 goto error;
2471 } else {
2472 int i;
2474 for (i = 0; i < tab->n_row; ++i) {
2475 if (isl_int_is_zero(tab->mat->row[i][off + var->index]))
2476 continue;
2477 isl_int_sub(tab->mat->row[i][1], tab->mat->row[i][1],
2478 tab->mat->row[i][off + var->index]);
2483 if (isl_tab_push_var(tab, isl_tab_undo_relax, var) < 0)
2484 goto error;
2486 return tab;
2487 error:
2488 isl_tab_free(tab);
2489 return NULL;
2492 int isl_tab_select_facet(struct isl_tab *tab, int con)
2494 if (!tab)
2495 return -1;
2497 return cut_to_hyperplane(tab, &tab->con[con]);
2500 static int may_be_equality(struct isl_tab *tab, int row)
2502 unsigned off = 2 + tab->M;
2503 return (tab->rational ? isl_int_is_zero(tab->mat->row[row][1])
2504 : isl_int_lt(tab->mat->row[row][1],
2505 tab->mat->row[row][0])) &&
2506 isl_seq_first_non_zero(tab->mat->row[row] + off + tab->n_dead,
2507 tab->n_col - tab->n_dead) != -1;
2510 /* Check for (near) equalities among the constraints.
2511 * A constraint is an equality if it is non-negative and if
2512 * its maximal value is either
2513 * - zero (in case of rational tableaus), or
2514 * - strictly less than 1 (in case of integer tableaus)
2516 * We first mark all non-redundant and non-dead variables that
2517 * are not frozen and not obviously not an equality.
2518 * Then we iterate over all marked variables if they can attain
2519 * any values larger than zero or at least one.
2520 * If the maximal value is zero, we mark any column variables
2521 * that appear in the row as being zero and mark the row as being redundant.
2522 * Otherwise, if the maximal value is strictly less than one (and the
2523 * tableau is integer), then we restrict the value to being zero
2524 * by adding an opposite non-negative variable.
2526 int isl_tab_detect_implicit_equalities(struct isl_tab *tab)
2528 int i;
2529 unsigned n_marked;
2531 if (!tab)
2532 return -1;
2533 if (tab->empty)
2534 return 0;
2535 if (tab->n_dead == tab->n_col)
2536 return 0;
2538 n_marked = 0;
2539 for (i = tab->n_redundant; i < tab->n_row; ++i) {
2540 struct isl_tab_var *var = isl_tab_var_from_row(tab, i);
2541 var->marked = !var->frozen && var->is_nonneg &&
2542 may_be_equality(tab, i);
2543 if (var->marked)
2544 n_marked++;
2546 for (i = tab->n_dead; i < tab->n_col; ++i) {
2547 struct isl_tab_var *var = var_from_col(tab, i);
2548 var->marked = !var->frozen && var->is_nonneg;
2549 if (var->marked)
2550 n_marked++;
2552 while (n_marked) {
2553 struct isl_tab_var *var;
2554 int sgn;
2555 for (i = tab->n_redundant; i < tab->n_row; ++i) {
2556 var = isl_tab_var_from_row(tab, i);
2557 if (var->marked)
2558 break;
2560 if (i == tab->n_row) {
2561 for (i = tab->n_dead; i < tab->n_col; ++i) {
2562 var = var_from_col(tab, i);
2563 if (var->marked)
2564 break;
2566 if (i == tab->n_col)
2567 break;
2569 var->marked = 0;
2570 n_marked--;
2571 sgn = sign_of_max(tab, var);
2572 if (sgn < 0)
2573 return -1;
2574 if (sgn == 0) {
2575 if (close_row(tab, var) < 0)
2576 return -1;
2577 } else if (!tab->rational && !at_least_one(tab, var)) {
2578 if (cut_to_hyperplane(tab, var) < 0)
2579 return -1;
2580 return isl_tab_detect_implicit_equalities(tab);
2582 for (i = tab->n_redundant; i < tab->n_row; ++i) {
2583 var = isl_tab_var_from_row(tab, i);
2584 if (!var->marked)
2585 continue;
2586 if (may_be_equality(tab, i))
2587 continue;
2588 var->marked = 0;
2589 n_marked--;
2593 return 0;
2596 static int con_is_redundant(struct isl_tab *tab, struct isl_tab_var *var)
2598 if (!tab)
2599 return -1;
2600 if (tab->rational) {
2601 int sgn = sign_of_min(tab, var);
2602 if (sgn < -1)
2603 return -1;
2604 return sgn >= 0;
2605 } else {
2606 int irred = isl_tab_min_at_most_neg_one(tab, var);
2607 if (irred < 0)
2608 return -1;
2609 return !irred;
2613 /* Check for (near) redundant constraints.
2614 * A constraint is redundant if it is non-negative and if
2615 * its minimal value (temporarily ignoring the non-negativity) is either
2616 * - zero (in case of rational tableaus), or
2617 * - strictly larger than -1 (in case of integer tableaus)
2619 * We first mark all non-redundant and non-dead variables that
2620 * are not frozen and not obviously negatively unbounded.
2621 * Then we iterate over all marked variables if they can attain
2622 * any values smaller than zero or at most negative one.
2623 * If not, we mark the row as being redundant (assuming it hasn't
2624 * been detected as being obviously redundant in the mean time).
2626 int isl_tab_detect_redundant(struct isl_tab *tab)
2628 int i;
2629 unsigned n_marked;
2631 if (!tab)
2632 return -1;
2633 if (tab->empty)
2634 return 0;
2635 if (tab->n_redundant == tab->n_row)
2636 return 0;
2638 n_marked = 0;
2639 for (i = tab->n_redundant; i < tab->n_row; ++i) {
2640 struct isl_tab_var *var = isl_tab_var_from_row(tab, i);
2641 var->marked = !var->frozen && var->is_nonneg;
2642 if (var->marked)
2643 n_marked++;
2645 for (i = tab->n_dead; i < tab->n_col; ++i) {
2646 struct isl_tab_var *var = var_from_col(tab, i);
2647 var->marked = !var->frozen && var->is_nonneg &&
2648 !min_is_manifestly_unbounded(tab, var);
2649 if (var->marked)
2650 n_marked++;
2652 while (n_marked) {
2653 struct isl_tab_var *var;
2654 int red;
2655 for (i = tab->n_redundant; i < tab->n_row; ++i) {
2656 var = isl_tab_var_from_row(tab, i);
2657 if (var->marked)
2658 break;
2660 if (i == tab->n_row) {
2661 for (i = tab->n_dead; i < tab->n_col; ++i) {
2662 var = var_from_col(tab, i);
2663 if (var->marked)
2664 break;
2666 if (i == tab->n_col)
2667 break;
2669 var->marked = 0;
2670 n_marked--;
2671 red = con_is_redundant(tab, var);
2672 if (red < 0)
2673 return -1;
2674 if (red && !var->is_redundant)
2675 if (isl_tab_mark_redundant(tab, var->index) < 0)
2676 return -1;
2677 for (i = tab->n_dead; i < tab->n_col; ++i) {
2678 var = var_from_col(tab, i);
2679 if (!var->marked)
2680 continue;
2681 if (!min_is_manifestly_unbounded(tab, var))
2682 continue;
2683 var->marked = 0;
2684 n_marked--;
2688 return 0;
2691 int isl_tab_is_equality(struct isl_tab *tab, int con)
2693 int row;
2694 unsigned off;
2696 if (!tab)
2697 return -1;
2698 if (tab->con[con].is_zero)
2699 return 1;
2700 if (tab->con[con].is_redundant)
2701 return 0;
2702 if (!tab->con[con].is_row)
2703 return tab->con[con].index < tab->n_dead;
2705 row = tab->con[con].index;
2707 off = 2 + tab->M;
2708 return isl_int_is_zero(tab->mat->row[row][1]) &&
2709 isl_seq_first_non_zero(tab->mat->row[row] + 2 + tab->n_dead,
2710 tab->n_col - tab->n_dead) == -1;
2713 /* Return the minimial value of the affine expression "f" with denominator
2714 * "denom" in *opt, *opt_denom, assuming the tableau is not empty and
2715 * the expression cannot attain arbitrarily small values.
2716 * If opt_denom is NULL, then *opt is rounded up to the nearest integer.
2717 * The return value reflects the nature of the result (empty, unbounded,
2718 * minmimal value returned in *opt).
2720 enum isl_lp_result isl_tab_min(struct isl_tab *tab,
2721 isl_int *f, isl_int denom, isl_int *opt, isl_int *opt_denom,
2722 unsigned flags)
2724 int r;
2725 enum isl_lp_result res = isl_lp_ok;
2726 struct isl_tab_var *var;
2727 struct isl_tab_undo *snap;
2729 if (tab->empty)
2730 return isl_lp_empty;
2732 snap = isl_tab_snap(tab);
2733 r = isl_tab_add_row(tab, f);
2734 if (r < 0)
2735 return isl_lp_error;
2736 var = &tab->con[r];
2737 isl_int_mul(tab->mat->row[var->index][0],
2738 tab->mat->row[var->index][0], denom);
2739 for (;;) {
2740 int row, col;
2741 find_pivot(tab, var, var, -1, &row, &col);
2742 if (row == var->index) {
2743 res = isl_lp_unbounded;
2744 break;
2746 if (row == -1)
2747 break;
2748 if (isl_tab_pivot(tab, row, col) < 0)
2749 return isl_lp_error;
2751 if (ISL_FL_ISSET(flags, ISL_TAB_SAVE_DUAL)) {
2752 int i;
2754 isl_vec_free(tab->dual);
2755 tab->dual = isl_vec_alloc(tab->mat->ctx, 1 + tab->n_con);
2756 if (!tab->dual)
2757 return isl_lp_error;
2758 isl_int_set(tab->dual->el[0], tab->mat->row[var->index][0]);
2759 for (i = 0; i < tab->n_con; ++i) {
2760 int pos;
2761 if (tab->con[i].is_row) {
2762 isl_int_set_si(tab->dual->el[1 + i], 0);
2763 continue;
2765 pos = 2 + tab->M + tab->con[i].index;
2766 if (tab->con[i].negated)
2767 isl_int_neg(tab->dual->el[1 + i],
2768 tab->mat->row[var->index][pos]);
2769 else
2770 isl_int_set(tab->dual->el[1 + i],
2771 tab->mat->row[var->index][pos]);
2774 if (opt && res == isl_lp_ok) {
2775 if (opt_denom) {
2776 isl_int_set(*opt, tab->mat->row[var->index][1]);
2777 isl_int_set(*opt_denom, tab->mat->row[var->index][0]);
2778 } else
2779 isl_int_cdiv_q(*opt, tab->mat->row[var->index][1],
2780 tab->mat->row[var->index][0]);
2782 if (isl_tab_rollback(tab, snap) < 0)
2783 return isl_lp_error;
2784 return res;
2787 int isl_tab_is_redundant(struct isl_tab *tab, int con)
2789 if (!tab)
2790 return -1;
2791 if (tab->con[con].is_zero)
2792 return 0;
2793 if (tab->con[con].is_redundant)
2794 return 1;
2795 return tab->con[con].is_row && tab->con[con].index < tab->n_redundant;
2798 /* Take a snapshot of the tableau that can be restored by s call to
2799 * isl_tab_rollback.
2801 struct isl_tab_undo *isl_tab_snap(struct isl_tab *tab)
2803 if (!tab)
2804 return NULL;
2805 tab->need_undo = 1;
2806 return tab->top;
2809 /* Undo the operation performed by isl_tab_relax.
2811 static int unrelax(struct isl_tab *tab, struct isl_tab_var *var) WARN_UNUSED;
2812 static int unrelax(struct isl_tab *tab, struct isl_tab_var *var)
2814 unsigned off = 2 + tab->M;
2816 if (!var->is_row && !max_is_manifestly_unbounded(tab, var))
2817 if (to_row(tab, var, 1) < 0)
2818 return -1;
2820 if (var->is_row) {
2821 isl_int_sub(tab->mat->row[var->index][1],
2822 tab->mat->row[var->index][1], tab->mat->row[var->index][0]);
2823 if (var->is_nonneg) {
2824 int sgn = restore_row(tab, var);
2825 isl_assert(tab->mat->ctx, sgn >= 0, return -1);
2827 } else {
2828 int i;
2830 for (i = 0; i < tab->n_row; ++i) {
2831 if (isl_int_is_zero(tab->mat->row[i][off + var->index]))
2832 continue;
2833 isl_int_add(tab->mat->row[i][1], tab->mat->row[i][1],
2834 tab->mat->row[i][off + var->index]);
2839 return 0;
2842 static int perform_undo_var(struct isl_tab *tab, struct isl_tab_undo *undo) WARN_UNUSED;
2843 static int perform_undo_var(struct isl_tab *tab, struct isl_tab_undo *undo)
2845 struct isl_tab_var *var = var_from_index(tab, undo->u.var_index);
2846 switch(undo->type) {
2847 case isl_tab_undo_nonneg:
2848 var->is_nonneg = 0;
2849 break;
2850 case isl_tab_undo_redundant:
2851 var->is_redundant = 0;
2852 tab->n_redundant--;
2853 restore_row(tab, isl_tab_var_from_row(tab, tab->n_redundant));
2854 break;
2855 case isl_tab_undo_freeze:
2856 var->frozen = 0;
2857 break;
2858 case isl_tab_undo_zero:
2859 var->is_zero = 0;
2860 if (!var->is_row)
2861 tab->n_dead--;
2862 break;
2863 case isl_tab_undo_allocate:
2864 if (undo->u.var_index >= 0) {
2865 isl_assert(tab->mat->ctx, !var->is_row, return -1);
2866 drop_col(tab, var->index);
2867 break;
2869 if (!var->is_row) {
2870 if (!max_is_manifestly_unbounded(tab, var)) {
2871 if (to_row(tab, var, 1) < 0)
2872 return -1;
2873 } else if (!min_is_manifestly_unbounded(tab, var)) {
2874 if (to_row(tab, var, -1) < 0)
2875 return -1;
2876 } else
2877 if (to_row(tab, var, 0) < 0)
2878 return -1;
2880 drop_row(tab, var->index);
2881 break;
2882 case isl_tab_undo_relax:
2883 return unrelax(tab, var);
2886 return 0;
2889 /* Restore the tableau to the state where the basic variables
2890 * are those in "col_var".
2891 * We first construct a list of variables that are currently in
2892 * the basis, but shouldn't. Then we iterate over all variables
2893 * that should be in the basis and for each one that is currently
2894 * not in the basis, we exchange it with one of the elements of the
2895 * list constructed before.
2896 * We can always find an appropriate variable to pivot with because
2897 * the current basis is mapped to the old basis by a non-singular
2898 * matrix and so we can never end up with a zero row.
2900 static int restore_basis(struct isl_tab *tab, int *col_var)
2902 int i, j;
2903 int n_extra = 0;
2904 int *extra = NULL; /* current columns that contain bad stuff */
2905 unsigned off = 2 + tab->M;
2907 extra = isl_alloc_array(tab->mat->ctx, int, tab->n_col);
2908 if (!extra)
2909 goto error;
2910 for (i = 0; i < tab->n_col; ++i) {
2911 for (j = 0; j < tab->n_col; ++j)
2912 if (tab->col_var[i] == col_var[j])
2913 break;
2914 if (j < tab->n_col)
2915 continue;
2916 extra[n_extra++] = i;
2918 for (i = 0; i < tab->n_col && n_extra > 0; ++i) {
2919 struct isl_tab_var *var;
2920 int row;
2922 for (j = 0; j < tab->n_col; ++j)
2923 if (col_var[i] == tab->col_var[j])
2924 break;
2925 if (j < tab->n_col)
2926 continue;
2927 var = var_from_index(tab, col_var[i]);
2928 row = var->index;
2929 for (j = 0; j < n_extra; ++j)
2930 if (!isl_int_is_zero(tab->mat->row[row][off+extra[j]]))
2931 break;
2932 isl_assert(tab->mat->ctx, j < n_extra, goto error);
2933 if (isl_tab_pivot(tab, row, extra[j]) < 0)
2934 goto error;
2935 extra[j] = extra[--n_extra];
2938 free(extra);
2939 free(col_var);
2940 return 0;
2941 error:
2942 free(extra);
2943 free(col_var);
2944 return -1;
2947 /* Remove all samples with index n or greater, i.e., those samples
2948 * that were added since we saved this number of samples in
2949 * isl_tab_save_samples.
2951 static void drop_samples_since(struct isl_tab *tab, int n)
2953 int i;
2955 for (i = tab->n_sample - 1; i >= 0 && tab->n_sample > n; --i) {
2956 if (tab->sample_index[i] < n)
2957 continue;
2959 if (i != tab->n_sample - 1) {
2960 int t = tab->sample_index[tab->n_sample-1];
2961 tab->sample_index[tab->n_sample-1] = tab->sample_index[i];
2962 tab->sample_index[i] = t;
2963 isl_mat_swap_rows(tab->samples, tab->n_sample-1, i);
2965 tab->n_sample--;
2969 static int perform_undo(struct isl_tab *tab, struct isl_tab_undo *undo) WARN_UNUSED;
2970 static int perform_undo(struct isl_tab *tab, struct isl_tab_undo *undo)
2972 switch (undo->type) {
2973 case isl_tab_undo_empty:
2974 tab->empty = 0;
2975 break;
2976 case isl_tab_undo_nonneg:
2977 case isl_tab_undo_redundant:
2978 case isl_tab_undo_freeze:
2979 case isl_tab_undo_zero:
2980 case isl_tab_undo_allocate:
2981 case isl_tab_undo_relax:
2982 return perform_undo_var(tab, undo);
2983 case isl_tab_undo_bmap_eq:
2984 return isl_basic_map_free_equality(tab->bmap, 1);
2985 case isl_tab_undo_bmap_ineq:
2986 return isl_basic_map_free_inequality(tab->bmap, 1);
2987 case isl_tab_undo_bmap_div:
2988 if (isl_basic_map_free_div(tab->bmap, 1) < 0)
2989 return -1;
2990 if (tab->samples)
2991 tab->samples->n_col--;
2992 break;
2993 case isl_tab_undo_saved_basis:
2994 if (restore_basis(tab, undo->u.col_var) < 0)
2995 return -1;
2996 break;
2997 case isl_tab_undo_drop_sample:
2998 tab->n_outside--;
2999 break;
3000 case isl_tab_undo_saved_samples:
3001 drop_samples_since(tab, undo->u.n);
3002 break;
3003 case isl_tab_undo_callback:
3004 return undo->u.callback->run(undo->u.callback);
3005 default:
3006 isl_assert(tab->mat->ctx, 0, return -1);
3008 return 0;
3011 /* Return the tableau to the state it was in when the snapshot "snap"
3012 * was taken.
3014 int isl_tab_rollback(struct isl_tab *tab, struct isl_tab_undo *snap)
3016 struct isl_tab_undo *undo, *next;
3018 if (!tab)
3019 return -1;
3021 tab->in_undo = 1;
3022 for (undo = tab->top; undo && undo != &tab->bottom; undo = next) {
3023 next = undo->next;
3024 if (undo == snap)
3025 break;
3026 if (perform_undo(tab, undo) < 0) {
3027 free_undo(tab);
3028 tab->in_undo = 0;
3029 return -1;
3031 free(undo);
3033 tab->in_undo = 0;
3034 tab->top = undo;
3035 if (!undo)
3036 return -1;
3037 return 0;
3040 /* The given row "row" represents an inequality violated by all
3041 * points in the tableau. Check for some special cases of such
3042 * separating constraints.
3043 * In particular, if the row has been reduced to the constant -1,
3044 * then we know the inequality is adjacent (but opposite) to
3045 * an equality in the tableau.
3046 * If the row has been reduced to r = -1 -r', with r' an inequality
3047 * of the tableau, then the inequality is adjacent (but opposite)
3048 * to the inequality r'.
3050 static enum isl_ineq_type separation_type(struct isl_tab *tab, unsigned row)
3052 int pos;
3053 unsigned off = 2 + tab->M;
3055 if (tab->rational)
3056 return isl_ineq_separate;
3058 if (!isl_int_is_one(tab->mat->row[row][0]))
3059 return isl_ineq_separate;
3060 if (!isl_int_is_negone(tab->mat->row[row][1]))
3061 return isl_ineq_separate;
3063 pos = isl_seq_first_non_zero(tab->mat->row[row] + off + tab->n_dead,
3064 tab->n_col - tab->n_dead);
3065 if (pos == -1)
3066 return isl_ineq_adj_eq;
3068 if (!isl_int_is_negone(tab->mat->row[row][off + tab->n_dead + pos]))
3069 return isl_ineq_separate;
3071 pos = isl_seq_first_non_zero(
3072 tab->mat->row[row] + off + tab->n_dead + pos + 1,
3073 tab->n_col - tab->n_dead - pos - 1);
3075 return pos == -1 ? isl_ineq_adj_ineq : isl_ineq_separate;
3078 /* Check the effect of inequality "ineq" on the tableau "tab".
3079 * The result may be
3080 * isl_ineq_redundant: satisfied by all points in the tableau
3081 * isl_ineq_separate: satisfied by no point in the tableau
3082 * isl_ineq_cut: satisfied by some by not all points
3083 * isl_ineq_adj_eq: adjacent to an equality
3084 * isl_ineq_adj_ineq: adjacent to an inequality.
3086 enum isl_ineq_type isl_tab_ineq_type(struct isl_tab *tab, isl_int *ineq)
3088 enum isl_ineq_type type = isl_ineq_error;
3089 struct isl_tab_undo *snap = NULL;
3090 int con;
3091 int row;
3093 if (!tab)
3094 return isl_ineq_error;
3096 if (isl_tab_extend_cons(tab, 1) < 0)
3097 return isl_ineq_error;
3099 snap = isl_tab_snap(tab);
3101 con = isl_tab_add_row(tab, ineq);
3102 if (con < 0)
3103 goto error;
3105 row = tab->con[con].index;
3106 if (isl_tab_row_is_redundant(tab, row))
3107 type = isl_ineq_redundant;
3108 else if (isl_int_is_neg(tab->mat->row[row][1]) &&
3109 (tab->rational ||
3110 isl_int_abs_ge(tab->mat->row[row][1],
3111 tab->mat->row[row][0]))) {
3112 int nonneg = at_least_zero(tab, &tab->con[con]);
3113 if (nonneg < 0)
3114 goto error;
3115 if (nonneg)
3116 type = isl_ineq_cut;
3117 else
3118 type = separation_type(tab, row);
3119 } else {
3120 int red = con_is_redundant(tab, &tab->con[con]);
3121 if (red < 0)
3122 goto error;
3123 if (!red)
3124 type = isl_ineq_cut;
3125 else
3126 type = isl_ineq_redundant;
3129 if (isl_tab_rollback(tab, snap))
3130 return isl_ineq_error;
3131 return type;
3132 error:
3133 return isl_ineq_error;
3136 int isl_tab_track_bmap(struct isl_tab *tab, __isl_take isl_basic_map *bmap)
3138 if (!tab || !bmap)
3139 goto error;
3141 isl_assert(tab->mat->ctx, tab->n_eq == bmap->n_eq, return -1);
3142 isl_assert(tab->mat->ctx,
3143 tab->n_con == bmap->n_eq + bmap->n_ineq, return -1);
3145 tab->bmap = bmap;
3147 return 0;
3148 error:
3149 isl_basic_map_free(bmap);
3150 return -1;
3153 int isl_tab_track_bset(struct isl_tab *tab, __isl_take isl_basic_set *bset)
3155 return isl_tab_track_bmap(tab, (isl_basic_map *)bset);
3158 __isl_keep isl_basic_set *isl_tab_peek_bset(struct isl_tab *tab)
3160 if (!tab)
3161 return NULL;
3163 return (isl_basic_set *)tab->bmap;
3166 void isl_tab_dump(struct isl_tab *tab, FILE *out, int indent)
3168 unsigned r, c;
3169 int i;
3171 if (!tab) {
3172 fprintf(out, "%*snull tab\n", indent, "");
3173 return;
3175 fprintf(out, "%*sn_redundant: %d, n_dead: %d", indent, "",
3176 tab->n_redundant, tab->n_dead);
3177 if (tab->rational)
3178 fprintf(out, ", rational");
3179 if (tab->empty)
3180 fprintf(out, ", empty");
3181 fprintf(out, "\n");
3182 fprintf(out, "%*s[", indent, "");
3183 for (i = 0; i < tab->n_var; ++i) {
3184 if (i)
3185 fprintf(out, (i == tab->n_param ||
3186 i == tab->n_var - tab->n_div) ? "; "
3187 : ", ");
3188 fprintf(out, "%c%d%s", tab->var[i].is_row ? 'r' : 'c',
3189 tab->var[i].index,
3190 tab->var[i].is_zero ? " [=0]" :
3191 tab->var[i].is_redundant ? " [R]" : "");
3193 fprintf(out, "]\n");
3194 fprintf(out, "%*s[", indent, "");
3195 for (i = 0; i < tab->n_con; ++i) {
3196 if (i)
3197 fprintf(out, ", ");
3198 fprintf(out, "%c%d%s", tab->con[i].is_row ? 'r' : 'c',
3199 tab->con[i].index,
3200 tab->con[i].is_zero ? " [=0]" :
3201 tab->con[i].is_redundant ? " [R]" : "");
3203 fprintf(out, "]\n");
3204 fprintf(out, "%*s[", indent, "");
3205 for (i = 0; i < tab->n_row; ++i) {
3206 const char *sign = "";
3207 if (i)
3208 fprintf(out, ", ");
3209 if (tab->row_sign) {
3210 if (tab->row_sign[i] == isl_tab_row_unknown)
3211 sign = "?";
3212 else if (tab->row_sign[i] == isl_tab_row_neg)
3213 sign = "-";
3214 else if (tab->row_sign[i] == isl_tab_row_pos)
3215 sign = "+";
3216 else
3217 sign = "+-";
3219 fprintf(out, "r%d: %d%s%s", i, tab->row_var[i],
3220 isl_tab_var_from_row(tab, i)->is_nonneg ? " [>=0]" : "", sign);
3222 fprintf(out, "]\n");
3223 fprintf(out, "%*s[", indent, "");
3224 for (i = 0; i < tab->n_col; ++i) {
3225 if (i)
3226 fprintf(out, ", ");
3227 fprintf(out, "c%d: %d%s", i, tab->col_var[i],
3228 var_from_col(tab, i)->is_nonneg ? " [>=0]" : "");
3230 fprintf(out, "]\n");
3231 r = tab->mat->n_row;
3232 tab->mat->n_row = tab->n_row;
3233 c = tab->mat->n_col;
3234 tab->mat->n_col = 2 + tab->M + tab->n_col;
3235 isl_mat_dump(tab->mat, out, indent);
3236 tab->mat->n_row = r;
3237 tab->mat->n_col = c;
3238 if (tab->bmap)
3239 isl_basic_map_dump(tab->bmap, out, indent);