doc: add some implementation details on parametric integer programming
[isl.git] / isl_tab.c
blobf418517f731a4a6f1ca998c8e365efb0884149b2
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_ctx_private.h>
11 #include <isl_mat_private.h>
12 #include "isl_map_private.h"
13 #include "isl_tab.h"
14 #include <isl/seq.h>
17 * The implementation of tableaus in this file was inspired by Section 8
18 * of David Detlefs, Greg Nelson and James B. Saxe, "Simplify: a theorem
19 * prover for program checking".
22 struct isl_tab *isl_tab_alloc(struct isl_ctx *ctx,
23 unsigned n_row, unsigned n_var, unsigned M)
25 int i;
26 struct isl_tab *tab;
27 unsigned off = 2 + M;
29 tab = isl_calloc_type(ctx, struct isl_tab);
30 if (!tab)
31 return NULL;
32 tab->mat = isl_mat_alloc(ctx, n_row, off + n_var);
33 if (!tab->mat)
34 goto error;
35 tab->var = isl_alloc_array(ctx, struct isl_tab_var, n_var);
36 if (!tab->var)
37 goto error;
38 tab->con = isl_alloc_array(ctx, struct isl_tab_var, n_row);
39 if (!tab->con)
40 goto error;
41 tab->col_var = isl_alloc_array(ctx, int, n_var);
42 if (!tab->col_var)
43 goto error;
44 tab->row_var = isl_alloc_array(ctx, int, n_row);
45 if (!tab->row_var)
46 goto error;
47 for (i = 0; i < n_var; ++i) {
48 tab->var[i].index = i;
49 tab->var[i].is_row = 0;
50 tab->var[i].is_nonneg = 0;
51 tab->var[i].is_zero = 0;
52 tab->var[i].is_redundant = 0;
53 tab->var[i].frozen = 0;
54 tab->var[i].negated = 0;
55 tab->col_var[i] = i;
57 tab->n_row = 0;
58 tab->n_con = 0;
59 tab->n_eq = 0;
60 tab->max_con = n_row;
61 tab->n_col = n_var;
62 tab->n_var = n_var;
63 tab->max_var = n_var;
64 tab->n_param = 0;
65 tab->n_div = 0;
66 tab->n_dead = 0;
67 tab->n_redundant = 0;
68 tab->strict_redundant = 0;
69 tab->need_undo = 0;
70 tab->rational = 0;
71 tab->empty = 0;
72 tab->in_undo = 0;
73 tab->M = M;
74 tab->cone = 0;
75 tab->bottom.type = isl_tab_undo_bottom;
76 tab->bottom.next = NULL;
77 tab->top = &tab->bottom;
79 tab->n_zero = 0;
80 tab->n_unbounded = 0;
81 tab->basis = NULL;
83 return tab;
84 error:
85 isl_tab_free(tab);
86 return NULL;
89 int isl_tab_extend_cons(struct isl_tab *tab, unsigned n_new)
91 unsigned off;
93 if (!tab)
94 return -1;
96 off = 2 + tab->M;
98 if (tab->max_con < tab->n_con + n_new) {
99 struct isl_tab_var *con;
101 con = isl_realloc_array(tab->mat->ctx, tab->con,
102 struct isl_tab_var, tab->max_con + n_new);
103 if (!con)
104 return -1;
105 tab->con = con;
106 tab->max_con += n_new;
108 if (tab->mat->n_row < tab->n_row + n_new) {
109 int *row_var;
111 tab->mat = isl_mat_extend(tab->mat,
112 tab->n_row + n_new, off + tab->n_col);
113 if (!tab->mat)
114 return -1;
115 row_var = isl_realloc_array(tab->mat->ctx, tab->row_var,
116 int, tab->mat->n_row);
117 if (!row_var)
118 return -1;
119 tab->row_var = row_var;
120 if (tab->row_sign) {
121 enum isl_tab_row_sign *s;
122 s = isl_realloc_array(tab->mat->ctx, tab->row_sign,
123 enum isl_tab_row_sign, tab->mat->n_row);
124 if (!s)
125 return -1;
126 tab->row_sign = s;
129 return 0;
132 /* Make room for at least n_new extra variables.
133 * Return -1 if anything went wrong.
135 int isl_tab_extend_vars(struct isl_tab *tab, unsigned n_new)
137 struct isl_tab_var *var;
138 unsigned off = 2 + tab->M;
140 if (tab->max_var < tab->n_var + n_new) {
141 var = isl_realloc_array(tab->mat->ctx, tab->var,
142 struct isl_tab_var, tab->n_var + n_new);
143 if (!var)
144 return -1;
145 tab->var = var;
146 tab->max_var += n_new;
149 if (tab->mat->n_col < off + tab->n_col + n_new) {
150 int *p;
152 tab->mat = isl_mat_extend(tab->mat,
153 tab->mat->n_row, off + tab->n_col + n_new);
154 if (!tab->mat)
155 return -1;
156 p = isl_realloc_array(tab->mat->ctx, tab->col_var,
157 int, tab->n_col + n_new);
158 if (!p)
159 return -1;
160 tab->col_var = p;
163 return 0;
166 struct isl_tab *isl_tab_extend(struct isl_tab *tab, unsigned n_new)
168 if (isl_tab_extend_cons(tab, n_new) >= 0)
169 return tab;
171 isl_tab_free(tab);
172 return NULL;
175 static void free_undo(struct isl_tab *tab)
177 struct isl_tab_undo *undo, *next;
179 for (undo = tab->top; undo && undo != &tab->bottom; undo = next) {
180 next = undo->next;
181 free(undo);
183 tab->top = undo;
186 void isl_tab_free(struct isl_tab *tab)
188 if (!tab)
189 return;
190 free_undo(tab);
191 isl_mat_free(tab->mat);
192 isl_vec_free(tab->dual);
193 isl_basic_map_free(tab->bmap);
194 free(tab->var);
195 free(tab->con);
196 free(tab->row_var);
197 free(tab->col_var);
198 free(tab->row_sign);
199 isl_mat_free(tab->samples);
200 free(tab->sample_index);
201 isl_mat_free(tab->basis);
202 free(tab);
205 struct isl_tab *isl_tab_dup(struct isl_tab *tab)
207 int i;
208 struct isl_tab *dup;
209 unsigned off;
211 if (!tab)
212 return NULL;
214 off = 2 + tab->M;
215 dup = isl_calloc_type(tab->mat->ctx, struct isl_tab);
216 if (!dup)
217 return NULL;
218 dup->mat = isl_mat_dup(tab->mat);
219 if (!dup->mat)
220 goto error;
221 dup->var = isl_alloc_array(tab->mat->ctx, struct isl_tab_var, tab->max_var);
222 if (!dup->var)
223 goto error;
224 for (i = 0; i < tab->n_var; ++i)
225 dup->var[i] = tab->var[i];
226 dup->con = isl_alloc_array(tab->mat->ctx, struct isl_tab_var, tab->max_con);
227 if (!dup->con)
228 goto error;
229 for (i = 0; i < tab->n_con; ++i)
230 dup->con[i] = tab->con[i];
231 dup->col_var = isl_alloc_array(tab->mat->ctx, int, tab->mat->n_col - off);
232 if (!dup->col_var)
233 goto error;
234 for (i = 0; i < tab->n_col; ++i)
235 dup->col_var[i] = tab->col_var[i];
236 dup->row_var = isl_alloc_array(tab->mat->ctx, int, tab->mat->n_row);
237 if (!dup->row_var)
238 goto error;
239 for (i = 0; i < tab->n_row; ++i)
240 dup->row_var[i] = tab->row_var[i];
241 if (tab->row_sign) {
242 dup->row_sign = isl_alloc_array(tab->mat->ctx, enum isl_tab_row_sign,
243 tab->mat->n_row);
244 if (!dup->row_sign)
245 goto error;
246 for (i = 0; i < tab->n_row; ++i)
247 dup->row_sign[i] = tab->row_sign[i];
249 if (tab->samples) {
250 dup->samples = isl_mat_dup(tab->samples);
251 if (!dup->samples)
252 goto error;
253 dup->sample_index = isl_alloc_array(tab->mat->ctx, int,
254 tab->samples->n_row);
255 if (!dup->sample_index)
256 goto error;
257 dup->n_sample = tab->n_sample;
258 dup->n_outside = tab->n_outside;
260 dup->n_row = tab->n_row;
261 dup->n_con = tab->n_con;
262 dup->n_eq = tab->n_eq;
263 dup->max_con = tab->max_con;
264 dup->n_col = tab->n_col;
265 dup->n_var = tab->n_var;
266 dup->max_var = tab->max_var;
267 dup->n_param = tab->n_param;
268 dup->n_div = tab->n_div;
269 dup->n_dead = tab->n_dead;
270 dup->n_redundant = tab->n_redundant;
271 dup->rational = tab->rational;
272 dup->empty = tab->empty;
273 dup->strict_redundant = 0;
274 dup->need_undo = 0;
275 dup->in_undo = 0;
276 dup->M = tab->M;
277 tab->cone = tab->cone;
278 dup->bottom.type = isl_tab_undo_bottom;
279 dup->bottom.next = NULL;
280 dup->top = &dup->bottom;
282 dup->n_zero = tab->n_zero;
283 dup->n_unbounded = tab->n_unbounded;
284 dup->basis = isl_mat_dup(tab->basis);
286 return dup;
287 error:
288 isl_tab_free(dup);
289 return NULL;
292 /* Construct the coefficient matrix of the product tableau
293 * of two tableaus.
294 * mat{1,2} is the coefficient matrix of tableau {1,2}
295 * row{1,2} is the number of rows in tableau {1,2}
296 * col{1,2} is the number of columns in tableau {1,2}
297 * off is the offset to the coefficient column (skipping the
298 * denominator, the constant term and the big parameter if any)
299 * r{1,2} is the number of redundant rows in tableau {1,2}
300 * d{1,2} is the number of dead columns in tableau {1,2}
302 * The order of the rows and columns in the result is as explained
303 * in isl_tab_product.
305 static struct isl_mat *tab_mat_product(struct isl_mat *mat1,
306 struct isl_mat *mat2, unsigned row1, unsigned row2,
307 unsigned col1, unsigned col2,
308 unsigned off, unsigned r1, unsigned r2, unsigned d1, unsigned d2)
310 int i;
311 struct isl_mat *prod;
312 unsigned n;
314 prod = isl_mat_alloc(mat1->ctx, mat1->n_row + mat2->n_row,
315 off + col1 + col2);
316 if (!prod)
317 return NULL;
319 n = 0;
320 for (i = 0; i < r1; ++i) {
321 isl_seq_cpy(prod->row[n + i], mat1->row[i], off + d1);
322 isl_seq_clr(prod->row[n + i] + off + d1, d2);
323 isl_seq_cpy(prod->row[n + i] + off + d1 + d2,
324 mat1->row[i] + off + d1, col1 - d1);
325 isl_seq_clr(prod->row[n + i] + off + col1 + d1, col2 - d2);
328 n += r1;
329 for (i = 0; i < r2; ++i) {
330 isl_seq_cpy(prod->row[n + i], mat2->row[i], off);
331 isl_seq_clr(prod->row[n + i] + off, d1);
332 isl_seq_cpy(prod->row[n + i] + off + d1,
333 mat2->row[i] + off, d2);
334 isl_seq_clr(prod->row[n + i] + off + d1 + d2, col1 - d1);
335 isl_seq_cpy(prod->row[n + i] + off + col1 + d1,
336 mat2->row[i] + off + d2, col2 - d2);
339 n += r2;
340 for (i = 0; i < row1 - r1; ++i) {
341 isl_seq_cpy(prod->row[n + i], mat1->row[r1 + i], off + d1);
342 isl_seq_clr(prod->row[n + i] + off + d1, d2);
343 isl_seq_cpy(prod->row[n + i] + off + d1 + d2,
344 mat1->row[r1 + i] + off + d1, col1 - d1);
345 isl_seq_clr(prod->row[n + i] + off + col1 + d1, col2 - d2);
348 n += row1 - r1;
349 for (i = 0; i < row2 - r2; ++i) {
350 isl_seq_cpy(prod->row[n + i], mat2->row[r2 + i], off);
351 isl_seq_clr(prod->row[n + i] + off, d1);
352 isl_seq_cpy(prod->row[n + i] + off + d1,
353 mat2->row[r2 + i] + off, d2);
354 isl_seq_clr(prod->row[n + i] + off + d1 + d2, col1 - d1);
355 isl_seq_cpy(prod->row[n + i] + off + col1 + d1,
356 mat2->row[r2 + i] + off + d2, col2 - d2);
359 return prod;
362 /* Update the row or column index of a variable that corresponds
363 * to a variable in the first input tableau.
365 static void update_index1(struct isl_tab_var *var,
366 unsigned r1, unsigned r2, unsigned d1, unsigned d2)
368 if (var->index == -1)
369 return;
370 if (var->is_row && var->index >= r1)
371 var->index += r2;
372 if (!var->is_row && var->index >= d1)
373 var->index += d2;
376 /* Update the row or column index of a variable that corresponds
377 * to a variable in the second input tableau.
379 static void update_index2(struct isl_tab_var *var,
380 unsigned row1, unsigned col1,
381 unsigned r1, unsigned r2, unsigned d1, unsigned d2)
383 if (var->index == -1)
384 return;
385 if (var->is_row) {
386 if (var->index < r2)
387 var->index += r1;
388 else
389 var->index += row1;
390 } else {
391 if (var->index < d2)
392 var->index += d1;
393 else
394 var->index += col1;
398 /* Create a tableau that represents the Cartesian product of the sets
399 * represented by tableaus tab1 and tab2.
400 * The order of the rows in the product is
401 * - redundant rows of tab1
402 * - redundant rows of tab2
403 * - non-redundant rows of tab1
404 * - non-redundant rows of tab2
405 * The order of the columns is
406 * - denominator
407 * - constant term
408 * - coefficient of big parameter, if any
409 * - dead columns of tab1
410 * - dead columns of tab2
411 * - live columns of tab1
412 * - live columns of tab2
413 * The order of the variables and the constraints is a concatenation
414 * of order in the two input tableaus.
416 struct isl_tab *isl_tab_product(struct isl_tab *tab1, struct isl_tab *tab2)
418 int i;
419 struct isl_tab *prod;
420 unsigned off;
421 unsigned r1, r2, d1, d2;
423 if (!tab1 || !tab2)
424 return NULL;
426 isl_assert(tab1->mat->ctx, tab1->M == tab2->M, return NULL);
427 isl_assert(tab1->mat->ctx, tab1->rational == tab2->rational, return NULL);
428 isl_assert(tab1->mat->ctx, tab1->cone == tab2->cone, return NULL);
429 isl_assert(tab1->mat->ctx, !tab1->row_sign, return NULL);
430 isl_assert(tab1->mat->ctx, !tab2->row_sign, return NULL);
431 isl_assert(tab1->mat->ctx, tab1->n_param == 0, return NULL);
432 isl_assert(tab1->mat->ctx, tab2->n_param == 0, return NULL);
433 isl_assert(tab1->mat->ctx, tab1->n_div == 0, return NULL);
434 isl_assert(tab1->mat->ctx, tab2->n_div == 0, return NULL);
436 off = 2 + tab1->M;
437 r1 = tab1->n_redundant;
438 r2 = tab2->n_redundant;
439 d1 = tab1->n_dead;
440 d2 = tab2->n_dead;
441 prod = isl_calloc_type(tab1->mat->ctx, struct isl_tab);
442 if (!prod)
443 return NULL;
444 prod->mat = tab_mat_product(tab1->mat, tab2->mat,
445 tab1->n_row, tab2->n_row,
446 tab1->n_col, tab2->n_col, off, r1, r2, d1, d2);
447 if (!prod->mat)
448 goto error;
449 prod->var = isl_alloc_array(tab1->mat->ctx, struct isl_tab_var,
450 tab1->max_var + tab2->max_var);
451 if (!prod->var)
452 goto error;
453 for (i = 0; i < tab1->n_var; ++i) {
454 prod->var[i] = tab1->var[i];
455 update_index1(&prod->var[i], r1, r2, d1, d2);
457 for (i = 0; i < tab2->n_var; ++i) {
458 prod->var[tab1->n_var + i] = tab2->var[i];
459 update_index2(&prod->var[tab1->n_var + i],
460 tab1->n_row, tab1->n_col,
461 r1, r2, d1, d2);
463 prod->con = isl_alloc_array(tab1->mat->ctx, struct isl_tab_var,
464 tab1->max_con + tab2->max_con);
465 if (!prod->con)
466 goto error;
467 for (i = 0; i < tab1->n_con; ++i) {
468 prod->con[i] = tab1->con[i];
469 update_index1(&prod->con[i], r1, r2, d1, d2);
471 for (i = 0; i < tab2->n_con; ++i) {
472 prod->con[tab1->n_con + i] = tab2->con[i];
473 update_index2(&prod->con[tab1->n_con + i],
474 tab1->n_row, tab1->n_col,
475 r1, r2, d1, d2);
477 prod->col_var = isl_alloc_array(tab1->mat->ctx, int,
478 tab1->n_col + tab2->n_col);
479 if (!prod->col_var)
480 goto error;
481 for (i = 0; i < tab1->n_col; ++i) {
482 int pos = i < d1 ? i : i + d2;
483 prod->col_var[pos] = tab1->col_var[i];
485 for (i = 0; i < tab2->n_col; ++i) {
486 int pos = i < d2 ? d1 + i : tab1->n_col + i;
487 int t = tab2->col_var[i];
488 if (t >= 0)
489 t += tab1->n_var;
490 else
491 t -= tab1->n_con;
492 prod->col_var[pos] = t;
494 prod->row_var = isl_alloc_array(tab1->mat->ctx, int,
495 tab1->mat->n_row + tab2->mat->n_row);
496 if (!prod->row_var)
497 goto error;
498 for (i = 0; i < tab1->n_row; ++i) {
499 int pos = i < r1 ? i : i + r2;
500 prod->row_var[pos] = tab1->row_var[i];
502 for (i = 0; i < tab2->n_row; ++i) {
503 int pos = i < r2 ? r1 + i : tab1->n_row + i;
504 int t = tab2->row_var[i];
505 if (t >= 0)
506 t += tab1->n_var;
507 else
508 t -= tab1->n_con;
509 prod->row_var[pos] = t;
511 prod->samples = NULL;
512 prod->sample_index = NULL;
513 prod->n_row = tab1->n_row + tab2->n_row;
514 prod->n_con = tab1->n_con + tab2->n_con;
515 prod->n_eq = 0;
516 prod->max_con = tab1->max_con + tab2->max_con;
517 prod->n_col = tab1->n_col + tab2->n_col;
518 prod->n_var = tab1->n_var + tab2->n_var;
519 prod->max_var = tab1->max_var + tab2->max_var;
520 prod->n_param = 0;
521 prod->n_div = 0;
522 prod->n_dead = tab1->n_dead + tab2->n_dead;
523 prod->n_redundant = tab1->n_redundant + tab2->n_redundant;
524 prod->rational = tab1->rational;
525 prod->empty = tab1->empty || tab2->empty;
526 prod->strict_redundant = tab1->strict_redundant || tab2->strict_redundant;
527 prod->need_undo = 0;
528 prod->in_undo = 0;
529 prod->M = tab1->M;
530 prod->cone = tab1->cone;
531 prod->bottom.type = isl_tab_undo_bottom;
532 prod->bottom.next = NULL;
533 prod->top = &prod->bottom;
535 prod->n_zero = 0;
536 prod->n_unbounded = 0;
537 prod->basis = NULL;
539 return prod;
540 error:
541 isl_tab_free(prod);
542 return NULL;
545 static struct isl_tab_var *var_from_index(struct isl_tab *tab, int i)
547 if (i >= 0)
548 return &tab->var[i];
549 else
550 return &tab->con[~i];
553 struct isl_tab_var *isl_tab_var_from_row(struct isl_tab *tab, int i)
555 return var_from_index(tab, tab->row_var[i]);
558 static struct isl_tab_var *var_from_col(struct isl_tab *tab, int i)
560 return var_from_index(tab, tab->col_var[i]);
563 /* Check if there are any upper bounds on column variable "var",
564 * i.e., non-negative rows where var appears with a negative coefficient.
565 * Return 1 if there are no such bounds.
567 static int max_is_manifestly_unbounded(struct isl_tab *tab,
568 struct isl_tab_var *var)
570 int i;
571 unsigned off = 2 + tab->M;
573 if (var->is_row)
574 return 0;
575 for (i = tab->n_redundant; i < tab->n_row; ++i) {
576 if (!isl_int_is_neg(tab->mat->row[i][off + var->index]))
577 continue;
578 if (isl_tab_var_from_row(tab, i)->is_nonneg)
579 return 0;
581 return 1;
584 /* Check if there are any lower bounds on column variable "var",
585 * i.e., non-negative rows where var appears with a positive coefficient.
586 * Return 1 if there are no such bounds.
588 static int min_is_manifestly_unbounded(struct isl_tab *tab,
589 struct isl_tab_var *var)
591 int i;
592 unsigned off = 2 + tab->M;
594 if (var->is_row)
595 return 0;
596 for (i = tab->n_redundant; i < tab->n_row; ++i) {
597 if (!isl_int_is_pos(tab->mat->row[i][off + var->index]))
598 continue;
599 if (isl_tab_var_from_row(tab, i)->is_nonneg)
600 return 0;
602 return 1;
605 static int row_cmp(struct isl_tab *tab, int r1, int r2, int c, isl_int t)
607 unsigned off = 2 + tab->M;
609 if (tab->M) {
610 int s;
611 isl_int_mul(t, tab->mat->row[r1][2], tab->mat->row[r2][off+c]);
612 isl_int_submul(t, tab->mat->row[r2][2], tab->mat->row[r1][off+c]);
613 s = isl_int_sgn(t);
614 if (s)
615 return s;
617 isl_int_mul(t, tab->mat->row[r1][1], tab->mat->row[r2][off + c]);
618 isl_int_submul(t, tab->mat->row[r2][1], tab->mat->row[r1][off + c]);
619 return isl_int_sgn(t);
622 /* Given the index of a column "c", return the index of a row
623 * that can be used to pivot the column in, with either an increase
624 * (sgn > 0) or a decrease (sgn < 0) of the corresponding variable.
625 * If "var" is not NULL, then the row returned will be different from
626 * the one associated with "var".
628 * Each row in the tableau is of the form
630 * x_r = a_r0 + \sum_i a_ri x_i
632 * Only rows with x_r >= 0 and with the sign of a_ri opposite to "sgn"
633 * impose any limit on the increase or decrease in the value of x_c
634 * and this bound is equal to a_r0 / |a_rc|. We are therefore looking
635 * for the row with the smallest (most stringent) such bound.
636 * Note that the common denominator of each row drops out of the fraction.
637 * To check if row j has a smaller bound than row r, i.e.,
638 * a_j0 / |a_jc| < a_r0 / |a_rc| or a_j0 |a_rc| < a_r0 |a_jc|,
639 * we check if -sign(a_jc) (a_j0 a_rc - a_r0 a_jc) < 0,
640 * where -sign(a_jc) is equal to "sgn".
642 static int pivot_row(struct isl_tab *tab,
643 struct isl_tab_var *var, int sgn, int c)
645 int j, r, tsgn;
646 isl_int t;
647 unsigned off = 2 + tab->M;
649 isl_int_init(t);
650 r = -1;
651 for (j = tab->n_redundant; j < tab->n_row; ++j) {
652 if (var && j == var->index)
653 continue;
654 if (!isl_tab_var_from_row(tab, j)->is_nonneg)
655 continue;
656 if (sgn * isl_int_sgn(tab->mat->row[j][off + c]) >= 0)
657 continue;
658 if (r < 0) {
659 r = j;
660 continue;
662 tsgn = sgn * row_cmp(tab, r, j, c, t);
663 if (tsgn < 0 || (tsgn == 0 &&
664 tab->row_var[j] < tab->row_var[r]))
665 r = j;
667 isl_int_clear(t);
668 return r;
671 /* Find a pivot (row and col) that will increase (sgn > 0) or decrease
672 * (sgn < 0) the value of row variable var.
673 * If not NULL, then skip_var is a row variable that should be ignored
674 * while looking for a pivot row. It is usually equal to var.
676 * As the given row in the tableau is of the form
678 * x_r = a_r0 + \sum_i a_ri x_i
680 * we need to find a column such that the sign of a_ri is equal to "sgn"
681 * (such that an increase in x_i will have the desired effect) or a
682 * column with a variable that may attain negative values.
683 * If a_ri is positive, then we need to move x_i in the same direction
684 * to obtain the desired effect. Otherwise, x_i has to move in the
685 * opposite direction.
687 static void find_pivot(struct isl_tab *tab,
688 struct isl_tab_var *var, struct isl_tab_var *skip_var,
689 int sgn, int *row, int *col)
691 int j, r, c;
692 isl_int *tr;
694 *row = *col = -1;
696 isl_assert(tab->mat->ctx, var->is_row, return);
697 tr = tab->mat->row[var->index] + 2 + tab->M;
699 c = -1;
700 for (j = tab->n_dead; j < tab->n_col; ++j) {
701 if (isl_int_is_zero(tr[j]))
702 continue;
703 if (isl_int_sgn(tr[j]) != sgn &&
704 var_from_col(tab, j)->is_nonneg)
705 continue;
706 if (c < 0 || tab->col_var[j] < tab->col_var[c])
707 c = j;
709 if (c < 0)
710 return;
712 sgn *= isl_int_sgn(tr[c]);
713 r = pivot_row(tab, skip_var, sgn, c);
714 *row = r < 0 ? var->index : r;
715 *col = c;
718 /* Return 1 if row "row" represents an obviously redundant inequality.
719 * This means
720 * - it represents an inequality or a variable
721 * - that is the sum of a non-negative sample value and a positive
722 * combination of zero or more non-negative constraints.
724 int isl_tab_row_is_redundant(struct isl_tab *tab, int row)
726 int i;
727 unsigned off = 2 + tab->M;
729 if (tab->row_var[row] < 0 && !isl_tab_var_from_row(tab, row)->is_nonneg)
730 return 0;
732 if (isl_int_is_neg(tab->mat->row[row][1]))
733 return 0;
734 if (tab->strict_redundant && isl_int_is_zero(tab->mat->row[row][1]))
735 return 0;
736 if (tab->M && isl_int_is_neg(tab->mat->row[row][2]))
737 return 0;
739 for (i = tab->n_dead; i < tab->n_col; ++i) {
740 if (isl_int_is_zero(tab->mat->row[row][off + i]))
741 continue;
742 if (tab->col_var[i] >= 0)
743 return 0;
744 if (isl_int_is_neg(tab->mat->row[row][off + i]))
745 return 0;
746 if (!var_from_col(tab, i)->is_nonneg)
747 return 0;
749 return 1;
752 static void swap_rows(struct isl_tab *tab, int row1, int row2)
754 int t;
755 enum isl_tab_row_sign s;
757 t = tab->row_var[row1];
758 tab->row_var[row1] = tab->row_var[row2];
759 tab->row_var[row2] = t;
760 isl_tab_var_from_row(tab, row1)->index = row1;
761 isl_tab_var_from_row(tab, row2)->index = row2;
762 tab->mat = isl_mat_swap_rows(tab->mat, row1, row2);
764 if (!tab->row_sign)
765 return;
766 s = tab->row_sign[row1];
767 tab->row_sign[row1] = tab->row_sign[row2];
768 tab->row_sign[row2] = s;
771 static int push_union(struct isl_tab *tab,
772 enum isl_tab_undo_type type, union isl_tab_undo_val u) WARN_UNUSED;
773 static int push_union(struct isl_tab *tab,
774 enum isl_tab_undo_type type, union isl_tab_undo_val u)
776 struct isl_tab_undo *undo;
778 if (!tab->need_undo)
779 return 0;
781 undo = isl_alloc_type(tab->mat->ctx, struct isl_tab_undo);
782 if (!undo)
783 return -1;
784 undo->type = type;
785 undo->u = u;
786 undo->next = tab->top;
787 tab->top = undo;
789 return 0;
792 int isl_tab_push_var(struct isl_tab *tab,
793 enum isl_tab_undo_type type, struct isl_tab_var *var)
795 union isl_tab_undo_val u;
796 if (var->is_row)
797 u.var_index = tab->row_var[var->index];
798 else
799 u.var_index = tab->col_var[var->index];
800 return push_union(tab, type, u);
803 int isl_tab_push(struct isl_tab *tab, enum isl_tab_undo_type type)
805 union isl_tab_undo_val u = { 0 };
806 return push_union(tab, type, u);
809 /* Push a record on the undo stack describing the current basic
810 * variables, so that the this state can be restored during rollback.
812 int isl_tab_push_basis(struct isl_tab *tab)
814 int i;
815 union isl_tab_undo_val u;
817 u.col_var = isl_alloc_array(tab->mat->ctx, int, tab->n_col);
818 if (!u.col_var)
819 return -1;
820 for (i = 0; i < tab->n_col; ++i)
821 u.col_var[i] = tab->col_var[i];
822 return push_union(tab, isl_tab_undo_saved_basis, u);
825 int isl_tab_push_callback(struct isl_tab *tab, struct isl_tab_callback *callback)
827 union isl_tab_undo_val u;
828 u.callback = callback;
829 return push_union(tab, isl_tab_undo_callback, u);
832 struct isl_tab *isl_tab_init_samples(struct isl_tab *tab)
834 if (!tab)
835 return NULL;
837 tab->n_sample = 0;
838 tab->n_outside = 0;
839 tab->samples = isl_mat_alloc(tab->mat->ctx, 1, 1 + tab->n_var);
840 if (!tab->samples)
841 goto error;
842 tab->sample_index = isl_alloc_array(tab->mat->ctx, int, 1);
843 if (!tab->sample_index)
844 goto error;
845 return tab;
846 error:
847 isl_tab_free(tab);
848 return NULL;
851 struct isl_tab *isl_tab_add_sample(struct isl_tab *tab,
852 __isl_take isl_vec *sample)
854 if (!tab || !sample)
855 goto error;
857 if (tab->n_sample + 1 > tab->samples->n_row) {
858 int *t = isl_realloc_array(tab->mat->ctx,
859 tab->sample_index, int, tab->n_sample + 1);
860 if (!t)
861 goto error;
862 tab->sample_index = t;
865 tab->samples = isl_mat_extend(tab->samples,
866 tab->n_sample + 1, tab->samples->n_col);
867 if (!tab->samples)
868 goto error;
870 isl_seq_cpy(tab->samples->row[tab->n_sample], sample->el, sample->size);
871 isl_vec_free(sample);
872 tab->sample_index[tab->n_sample] = tab->n_sample;
873 tab->n_sample++;
875 return tab;
876 error:
877 isl_vec_free(sample);
878 isl_tab_free(tab);
879 return NULL;
882 struct isl_tab *isl_tab_drop_sample(struct isl_tab *tab, int s)
884 if (s != tab->n_outside) {
885 int t = tab->sample_index[tab->n_outside];
886 tab->sample_index[tab->n_outside] = tab->sample_index[s];
887 tab->sample_index[s] = t;
888 isl_mat_swap_rows(tab->samples, tab->n_outside, s);
890 tab->n_outside++;
891 if (isl_tab_push(tab, isl_tab_undo_drop_sample) < 0) {
892 isl_tab_free(tab);
893 return NULL;
896 return tab;
899 /* Record the current number of samples so that we can remove newer
900 * samples during a rollback.
902 int isl_tab_save_samples(struct isl_tab *tab)
904 union isl_tab_undo_val u;
906 if (!tab)
907 return -1;
909 u.n = tab->n_sample;
910 return push_union(tab, isl_tab_undo_saved_samples, u);
913 /* Mark row with index "row" as being redundant.
914 * If we may need to undo the operation or if the row represents
915 * a variable of the original problem, the row is kept,
916 * but no longer considered when looking for a pivot row.
917 * Otherwise, the row is simply removed.
919 * The row may be interchanged with some other row. If it
920 * is interchanged with a later row, return 1. Otherwise return 0.
921 * If the rows are checked in order in the calling function,
922 * then a return value of 1 means that the row with the given
923 * row number may now contain a different row that hasn't been checked yet.
925 int isl_tab_mark_redundant(struct isl_tab *tab, int row)
927 struct isl_tab_var *var = isl_tab_var_from_row(tab, row);
928 var->is_redundant = 1;
929 isl_assert(tab->mat->ctx, row >= tab->n_redundant, return -1);
930 if (tab->need_undo || tab->row_var[row] >= 0) {
931 if (tab->row_var[row] >= 0 && !var->is_nonneg) {
932 var->is_nonneg = 1;
933 if (isl_tab_push_var(tab, isl_tab_undo_nonneg, var) < 0)
934 return -1;
936 if (row != tab->n_redundant)
937 swap_rows(tab, row, tab->n_redundant);
938 tab->n_redundant++;
939 return isl_tab_push_var(tab, isl_tab_undo_redundant, var);
940 } else {
941 if (row != tab->n_row - 1)
942 swap_rows(tab, row, tab->n_row - 1);
943 isl_tab_var_from_row(tab, tab->n_row - 1)->index = -1;
944 tab->n_row--;
945 return 1;
949 int isl_tab_mark_empty(struct isl_tab *tab)
951 if (!tab)
952 return -1;
953 if (!tab->empty && tab->need_undo)
954 if (isl_tab_push(tab, isl_tab_undo_empty) < 0)
955 return -1;
956 tab->empty = 1;
957 return 0;
960 int isl_tab_freeze_constraint(struct isl_tab *tab, int con)
962 struct isl_tab_var *var;
964 if (!tab)
965 return -1;
967 var = &tab->con[con];
968 if (var->frozen)
969 return 0;
970 if (var->index < 0)
971 return 0;
972 var->frozen = 1;
974 if (tab->need_undo)
975 return isl_tab_push_var(tab, isl_tab_undo_freeze, var);
977 return 0;
980 /* Update the rows signs after a pivot of "row" and "col", with "row_sgn"
981 * the original sign of the pivot element.
982 * We only keep track of row signs during PILP solving and in this case
983 * we only pivot a row with negative sign (meaning the value is always
984 * non-positive) using a positive pivot element.
986 * For each row j, the new value of the parametric constant is equal to
988 * a_j0 - a_jc a_r0/a_rc
990 * where a_j0 is the original parametric constant, a_rc is the pivot element,
991 * a_r0 is the parametric constant of the pivot row and a_jc is the
992 * pivot column entry of the row j.
993 * Since a_r0 is non-positive and a_rc is positive, the sign of row j
994 * remains the same if a_jc has the same sign as the row j or if
995 * a_jc is zero. In all other cases, we reset the sign to "unknown".
997 static void update_row_sign(struct isl_tab *tab, int row, int col, int row_sgn)
999 int i;
1000 struct isl_mat *mat = tab->mat;
1001 unsigned off = 2 + tab->M;
1003 if (!tab->row_sign)
1004 return;
1006 if (tab->row_sign[row] == 0)
1007 return;
1008 isl_assert(mat->ctx, row_sgn > 0, return);
1009 isl_assert(mat->ctx, tab->row_sign[row] == isl_tab_row_neg, return);
1010 tab->row_sign[row] = isl_tab_row_pos;
1011 for (i = 0; i < tab->n_row; ++i) {
1012 int s;
1013 if (i == row)
1014 continue;
1015 s = isl_int_sgn(mat->row[i][off + col]);
1016 if (!s)
1017 continue;
1018 if (!tab->row_sign[i])
1019 continue;
1020 if (s < 0 && tab->row_sign[i] == isl_tab_row_neg)
1021 continue;
1022 if (s > 0 && tab->row_sign[i] == isl_tab_row_pos)
1023 continue;
1024 tab->row_sign[i] = isl_tab_row_unknown;
1028 /* Given a row number "row" and a column number "col", pivot the tableau
1029 * such that the associated variables are interchanged.
1030 * The given row in the tableau expresses
1032 * x_r = a_r0 + \sum_i a_ri x_i
1034 * or
1036 * x_c = 1/a_rc x_r - a_r0/a_rc + sum_{i \ne r} -a_ri/a_rc
1038 * Substituting this equality into the other rows
1040 * x_j = a_j0 + \sum_i a_ji x_i
1042 * with a_jc \ne 0, we obtain
1044 * 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
1046 * The tableau
1048 * n_rc/d_r n_ri/d_r
1049 * n_jc/d_j n_ji/d_j
1051 * where i is any other column and j is any other row,
1052 * is therefore transformed into
1054 * s(n_rc)d_r/|n_rc| -s(n_rc)n_ri/|n_rc|
1055 * 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)
1057 * The transformation is performed along the following steps
1059 * d_r/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/d_j n_ji/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| 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|)/(|n_rc| d_j)
1071 * s(n_rc)d_r/|n_rc| -s(n_rc)n_ri/|n_rc|
1072 * n_jc/(|n_rc| d_j) (n_ji |n_rc| - s(n_rc)n_jc n_ri)/(|n_rc| d_j)
1074 * s(n_rc)d_r/|n_rc| -s(n_rc)n_ri/|n_rc|
1075 * 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)
1078 int isl_tab_pivot(struct isl_tab *tab, int row, int col)
1080 int i, j;
1081 int sgn;
1082 int t;
1083 struct isl_mat *mat = tab->mat;
1084 struct isl_tab_var *var;
1085 unsigned off = 2 + tab->M;
1087 if (tab->mat->ctx->abort) {
1088 isl_ctx_set_error(tab->mat->ctx, isl_error_abort);
1089 return -1;
1092 isl_int_swap(mat->row[row][0], mat->row[row][off + col]);
1093 sgn = isl_int_sgn(mat->row[row][0]);
1094 if (sgn < 0) {
1095 isl_int_neg(mat->row[row][0], mat->row[row][0]);
1096 isl_int_neg(mat->row[row][off + col], mat->row[row][off + col]);
1097 } else
1098 for (j = 0; j < off - 1 + tab->n_col; ++j) {
1099 if (j == off - 1 + col)
1100 continue;
1101 isl_int_neg(mat->row[row][1 + j], mat->row[row][1 + j]);
1103 if (!isl_int_is_one(mat->row[row][0]))
1104 isl_seq_normalize(mat->ctx, mat->row[row], off + tab->n_col);
1105 for (i = 0; i < tab->n_row; ++i) {
1106 if (i == row)
1107 continue;
1108 if (isl_int_is_zero(mat->row[i][off + col]))
1109 continue;
1110 isl_int_mul(mat->row[i][0], mat->row[i][0], mat->row[row][0]);
1111 for (j = 0; j < off - 1 + tab->n_col; ++j) {
1112 if (j == off - 1 + col)
1113 continue;
1114 isl_int_mul(mat->row[i][1 + j],
1115 mat->row[i][1 + j], mat->row[row][0]);
1116 isl_int_addmul(mat->row[i][1 + j],
1117 mat->row[i][off + col], mat->row[row][1 + j]);
1119 isl_int_mul(mat->row[i][off + col],
1120 mat->row[i][off + col], mat->row[row][off + col]);
1121 if (!isl_int_is_one(mat->row[i][0]))
1122 isl_seq_normalize(mat->ctx, mat->row[i], off + tab->n_col);
1124 t = tab->row_var[row];
1125 tab->row_var[row] = tab->col_var[col];
1126 tab->col_var[col] = t;
1127 var = isl_tab_var_from_row(tab, row);
1128 var->is_row = 1;
1129 var->index = row;
1130 var = var_from_col(tab, col);
1131 var->is_row = 0;
1132 var->index = col;
1133 update_row_sign(tab, row, col, sgn);
1134 if (tab->in_undo)
1135 return 0;
1136 for (i = tab->n_redundant; i < tab->n_row; ++i) {
1137 if (isl_int_is_zero(mat->row[i][off + col]))
1138 continue;
1139 if (!isl_tab_var_from_row(tab, i)->frozen &&
1140 isl_tab_row_is_redundant(tab, i)) {
1141 int redo = isl_tab_mark_redundant(tab, i);
1142 if (redo < 0)
1143 return -1;
1144 if (redo)
1145 --i;
1148 return 0;
1151 /* If "var" represents a column variable, then pivot is up (sgn > 0)
1152 * or down (sgn < 0) to a row. The variable is assumed not to be
1153 * unbounded in the specified direction.
1154 * If sgn = 0, then the variable is unbounded in both directions,
1155 * and we pivot with any row we can find.
1157 static int to_row(struct isl_tab *tab, struct isl_tab_var *var, int sign) WARN_UNUSED;
1158 static int to_row(struct isl_tab *tab, struct isl_tab_var *var, int sign)
1160 int r;
1161 unsigned off = 2 + tab->M;
1163 if (var->is_row)
1164 return 0;
1166 if (sign == 0) {
1167 for (r = tab->n_redundant; r < tab->n_row; ++r)
1168 if (!isl_int_is_zero(tab->mat->row[r][off+var->index]))
1169 break;
1170 isl_assert(tab->mat->ctx, r < tab->n_row, return -1);
1171 } else {
1172 r = pivot_row(tab, NULL, sign, var->index);
1173 isl_assert(tab->mat->ctx, r >= 0, return -1);
1176 return isl_tab_pivot(tab, r, var->index);
1179 static void check_table(struct isl_tab *tab)
1181 int i;
1183 if (tab->empty)
1184 return;
1185 for (i = tab->n_redundant; i < tab->n_row; ++i) {
1186 struct isl_tab_var *var;
1187 var = isl_tab_var_from_row(tab, i);
1188 if (!var->is_nonneg)
1189 continue;
1190 if (tab->M) {
1191 isl_assert(tab->mat->ctx,
1192 !isl_int_is_neg(tab->mat->row[i][2]), abort());
1193 if (isl_int_is_pos(tab->mat->row[i][2]))
1194 continue;
1196 isl_assert(tab->mat->ctx, !isl_int_is_neg(tab->mat->row[i][1]),
1197 abort());
1201 /* Return the sign of the maximal value of "var".
1202 * If the sign is not negative, then on return from this function,
1203 * the sample value will also be non-negative.
1205 * If "var" is manifestly unbounded wrt positive values, we are done.
1206 * Otherwise, we pivot the variable up to a row if needed
1207 * Then we continue pivoting down until either
1208 * - no more down pivots can be performed
1209 * - the sample value is positive
1210 * - the variable is pivoted into a manifestly unbounded column
1212 static int sign_of_max(struct isl_tab *tab, struct isl_tab_var *var)
1214 int row, col;
1216 if (max_is_manifestly_unbounded(tab, var))
1217 return 1;
1218 if (to_row(tab, var, 1) < 0)
1219 return -2;
1220 while (!isl_int_is_pos(tab->mat->row[var->index][1])) {
1221 find_pivot(tab, var, var, 1, &row, &col);
1222 if (row == -1)
1223 return isl_int_sgn(tab->mat->row[var->index][1]);
1224 if (isl_tab_pivot(tab, row, col) < 0)
1225 return -2;
1226 if (!var->is_row) /* manifestly unbounded */
1227 return 1;
1229 return 1;
1232 int isl_tab_sign_of_max(struct isl_tab *tab, int con)
1234 struct isl_tab_var *var;
1236 if (!tab)
1237 return -2;
1239 var = &tab->con[con];
1240 isl_assert(tab->mat->ctx, !var->is_redundant, return -2);
1241 isl_assert(tab->mat->ctx, !var->is_zero, return -2);
1243 return sign_of_max(tab, var);
1246 static int row_is_neg(struct isl_tab *tab, int row)
1248 if (!tab->M)
1249 return isl_int_is_neg(tab->mat->row[row][1]);
1250 if (isl_int_is_pos(tab->mat->row[row][2]))
1251 return 0;
1252 if (isl_int_is_neg(tab->mat->row[row][2]))
1253 return 1;
1254 return isl_int_is_neg(tab->mat->row[row][1]);
1257 static int row_sgn(struct isl_tab *tab, int row)
1259 if (!tab->M)
1260 return isl_int_sgn(tab->mat->row[row][1]);
1261 if (!isl_int_is_zero(tab->mat->row[row][2]))
1262 return isl_int_sgn(tab->mat->row[row][2]);
1263 else
1264 return isl_int_sgn(tab->mat->row[row][1]);
1267 /* Perform pivots until the row variable "var" has a non-negative
1268 * sample value or until no more upward pivots can be performed.
1269 * Return the sign of the sample value after the pivots have been
1270 * performed.
1272 static int restore_row(struct isl_tab *tab, struct isl_tab_var *var)
1274 int row, col;
1276 while (row_is_neg(tab, var->index)) {
1277 find_pivot(tab, var, var, 1, &row, &col);
1278 if (row == -1)
1279 break;
1280 if (isl_tab_pivot(tab, row, col) < 0)
1281 return -2;
1282 if (!var->is_row) /* manifestly unbounded */
1283 return 1;
1285 return row_sgn(tab, var->index);
1288 /* Perform pivots until we are sure that the row variable "var"
1289 * can attain non-negative values. After return from this
1290 * function, "var" is still a row variable, but its sample
1291 * value may not be non-negative, even if the function returns 1.
1293 static int at_least_zero(struct isl_tab *tab, struct isl_tab_var *var)
1295 int row, col;
1297 while (isl_int_is_neg(tab->mat->row[var->index][1])) {
1298 find_pivot(tab, var, var, 1, &row, &col);
1299 if (row == -1)
1300 break;
1301 if (row == var->index) /* manifestly unbounded */
1302 return 1;
1303 if (isl_tab_pivot(tab, row, col) < 0)
1304 return -1;
1306 return !isl_int_is_neg(tab->mat->row[var->index][1]);
1309 /* Return a negative value if "var" can attain negative values.
1310 * Return a non-negative value otherwise.
1312 * If "var" is manifestly unbounded wrt negative values, we are done.
1313 * Otherwise, if var is in a column, we can pivot it down to a row.
1314 * Then we continue pivoting down until either
1315 * - the pivot would result in a manifestly unbounded column
1316 * => we don't perform the pivot, but simply return -1
1317 * - no more down pivots can be performed
1318 * - the sample value is negative
1319 * If the sample value becomes negative and the variable is supposed
1320 * to be nonnegative, then we undo the last pivot.
1321 * However, if the last pivot has made the pivoting variable
1322 * obviously redundant, then it may have moved to another row.
1323 * In that case we look for upward pivots until we reach a non-negative
1324 * value again.
1326 static int sign_of_min(struct isl_tab *tab, struct isl_tab_var *var)
1328 int row, col;
1329 struct isl_tab_var *pivot_var = NULL;
1331 if (min_is_manifestly_unbounded(tab, var))
1332 return -1;
1333 if (!var->is_row) {
1334 col = var->index;
1335 row = pivot_row(tab, NULL, -1, col);
1336 pivot_var = var_from_col(tab, col);
1337 if (isl_tab_pivot(tab, row, col) < 0)
1338 return -2;
1339 if (var->is_redundant)
1340 return 0;
1341 if (isl_int_is_neg(tab->mat->row[var->index][1])) {
1342 if (var->is_nonneg) {
1343 if (!pivot_var->is_redundant &&
1344 pivot_var->index == row) {
1345 if (isl_tab_pivot(tab, row, col) < 0)
1346 return -2;
1347 } else
1348 if (restore_row(tab, var) < -1)
1349 return -2;
1351 return -1;
1354 if (var->is_redundant)
1355 return 0;
1356 while (!isl_int_is_neg(tab->mat->row[var->index][1])) {
1357 find_pivot(tab, var, var, -1, &row, &col);
1358 if (row == var->index)
1359 return -1;
1360 if (row == -1)
1361 return isl_int_sgn(tab->mat->row[var->index][1]);
1362 pivot_var = var_from_col(tab, col);
1363 if (isl_tab_pivot(tab, row, col) < 0)
1364 return -2;
1365 if (var->is_redundant)
1366 return 0;
1368 if (pivot_var && var->is_nonneg) {
1369 /* pivot back to non-negative value */
1370 if (!pivot_var->is_redundant && pivot_var->index == row) {
1371 if (isl_tab_pivot(tab, row, col) < 0)
1372 return -2;
1373 } else
1374 if (restore_row(tab, var) < -1)
1375 return -2;
1377 return -1;
1380 static int row_at_most_neg_one(struct isl_tab *tab, int row)
1382 if (tab->M) {
1383 if (isl_int_is_pos(tab->mat->row[row][2]))
1384 return 0;
1385 if (isl_int_is_neg(tab->mat->row[row][2]))
1386 return 1;
1388 return isl_int_is_neg(tab->mat->row[row][1]) &&
1389 isl_int_abs_ge(tab->mat->row[row][1],
1390 tab->mat->row[row][0]);
1393 /* Return 1 if "var" can attain values <= -1.
1394 * Return 0 otherwise.
1396 * The sample value of "var" is assumed to be non-negative when the
1397 * the function is called. If 1 is returned then the constraint
1398 * is not redundant and the sample value is made non-negative again before
1399 * the function returns.
1401 int isl_tab_min_at_most_neg_one(struct isl_tab *tab, struct isl_tab_var *var)
1403 int row, col;
1404 struct isl_tab_var *pivot_var;
1406 if (min_is_manifestly_unbounded(tab, var))
1407 return 1;
1408 if (!var->is_row) {
1409 col = var->index;
1410 row = pivot_row(tab, NULL, -1, col);
1411 pivot_var = var_from_col(tab, col);
1412 if (isl_tab_pivot(tab, row, col) < 0)
1413 return -1;
1414 if (var->is_redundant)
1415 return 0;
1416 if (row_at_most_neg_one(tab, var->index)) {
1417 if (var->is_nonneg) {
1418 if (!pivot_var->is_redundant &&
1419 pivot_var->index == row) {
1420 if (isl_tab_pivot(tab, row, col) < 0)
1421 return -1;
1422 } else
1423 if (restore_row(tab, var) < -1)
1424 return -1;
1426 return 1;
1429 if (var->is_redundant)
1430 return 0;
1431 do {
1432 find_pivot(tab, var, var, -1, &row, &col);
1433 if (row == var->index) {
1434 if (restore_row(tab, var) < -1)
1435 return -1;
1436 return 1;
1438 if (row == -1)
1439 return 0;
1440 pivot_var = var_from_col(tab, col);
1441 if (isl_tab_pivot(tab, row, col) < 0)
1442 return -1;
1443 if (var->is_redundant)
1444 return 0;
1445 } while (!row_at_most_neg_one(tab, var->index));
1446 if (var->is_nonneg) {
1447 /* pivot back to non-negative value */
1448 if (!pivot_var->is_redundant && pivot_var->index == row)
1449 if (isl_tab_pivot(tab, row, col) < 0)
1450 return -1;
1451 if (restore_row(tab, var) < -1)
1452 return -1;
1454 return 1;
1457 /* Return 1 if "var" can attain values >= 1.
1458 * Return 0 otherwise.
1460 static int at_least_one(struct isl_tab *tab, struct isl_tab_var *var)
1462 int row, col;
1463 isl_int *r;
1465 if (max_is_manifestly_unbounded(tab, var))
1466 return 1;
1467 if (to_row(tab, var, 1) < 0)
1468 return -1;
1469 r = tab->mat->row[var->index];
1470 while (isl_int_lt(r[1], r[0])) {
1471 find_pivot(tab, var, var, 1, &row, &col);
1472 if (row == -1)
1473 return isl_int_ge(r[1], r[0]);
1474 if (row == var->index) /* manifestly unbounded */
1475 return 1;
1476 if (isl_tab_pivot(tab, row, col) < 0)
1477 return -1;
1479 return 1;
1482 static void swap_cols(struct isl_tab *tab, int col1, int col2)
1484 int t;
1485 unsigned off = 2 + tab->M;
1486 t = tab->col_var[col1];
1487 tab->col_var[col1] = tab->col_var[col2];
1488 tab->col_var[col2] = t;
1489 var_from_col(tab, col1)->index = col1;
1490 var_from_col(tab, col2)->index = col2;
1491 tab->mat = isl_mat_swap_cols(tab->mat, off + col1, off + col2);
1494 /* Mark column with index "col" as representing a zero variable.
1495 * If we may need to undo the operation the column is kept,
1496 * but no longer considered.
1497 * Otherwise, the column is simply removed.
1499 * The column may be interchanged with some other column. If it
1500 * is interchanged with a later column, return 1. Otherwise return 0.
1501 * If the columns are checked in order in the calling function,
1502 * then a return value of 1 means that the column with the given
1503 * column number may now contain a different column that
1504 * hasn't been checked yet.
1506 int isl_tab_kill_col(struct isl_tab *tab, int col)
1508 var_from_col(tab, col)->is_zero = 1;
1509 if (tab->need_undo) {
1510 if (isl_tab_push_var(tab, isl_tab_undo_zero,
1511 var_from_col(tab, col)) < 0)
1512 return -1;
1513 if (col != tab->n_dead)
1514 swap_cols(tab, col, tab->n_dead);
1515 tab->n_dead++;
1516 return 0;
1517 } else {
1518 if (col != tab->n_col - 1)
1519 swap_cols(tab, col, tab->n_col - 1);
1520 var_from_col(tab, tab->n_col - 1)->index = -1;
1521 tab->n_col--;
1522 return 1;
1526 static int row_is_manifestly_non_integral(struct isl_tab *tab, int row)
1528 unsigned off = 2 + tab->M;
1530 if (tab->M && !isl_int_eq(tab->mat->row[row][2],
1531 tab->mat->row[row][0]))
1532 return 0;
1533 if (isl_seq_first_non_zero(tab->mat->row[row] + off + tab->n_dead,
1534 tab->n_col - tab->n_dead) != -1)
1535 return 0;
1537 return !isl_int_is_divisible_by(tab->mat->row[row][1],
1538 tab->mat->row[row][0]);
1541 /* For integer tableaus, check if any of the coordinates are stuck
1542 * at a non-integral value.
1544 static int tab_is_manifestly_empty(struct isl_tab *tab)
1546 int i;
1548 if (tab->empty)
1549 return 1;
1550 if (tab->rational)
1551 return 0;
1553 for (i = 0; i < tab->n_var; ++i) {
1554 if (!tab->var[i].is_row)
1555 continue;
1556 if (row_is_manifestly_non_integral(tab, tab->var[i].index))
1557 return 1;
1560 return 0;
1563 /* Row variable "var" is non-negative and cannot attain any values
1564 * larger than zero. This means that the coefficients of the unrestricted
1565 * column variables are zero and that the coefficients of the non-negative
1566 * column variables are zero or negative.
1567 * Each of the non-negative variables with a negative coefficient can
1568 * then also be written as the negative sum of non-negative variables
1569 * and must therefore also be zero.
1571 static int close_row(struct isl_tab *tab, struct isl_tab_var *var) WARN_UNUSED;
1572 static int close_row(struct isl_tab *tab, struct isl_tab_var *var)
1574 int j;
1575 struct isl_mat *mat = tab->mat;
1576 unsigned off = 2 + tab->M;
1578 isl_assert(tab->mat->ctx, var->is_nonneg, return -1);
1579 var->is_zero = 1;
1580 if (tab->need_undo)
1581 if (isl_tab_push_var(tab, isl_tab_undo_zero, var) < 0)
1582 return -1;
1583 for (j = tab->n_dead; j < tab->n_col; ++j) {
1584 int recheck;
1585 if (isl_int_is_zero(mat->row[var->index][off + j]))
1586 continue;
1587 isl_assert(tab->mat->ctx,
1588 isl_int_is_neg(mat->row[var->index][off + j]), return -1);
1589 recheck = isl_tab_kill_col(tab, j);
1590 if (recheck < 0)
1591 return -1;
1592 if (recheck)
1593 --j;
1595 if (isl_tab_mark_redundant(tab, var->index) < 0)
1596 return -1;
1597 if (tab_is_manifestly_empty(tab) && isl_tab_mark_empty(tab) < 0)
1598 return -1;
1599 return 0;
1602 /* Add a constraint to the tableau and allocate a row for it.
1603 * Return the index into the constraint array "con".
1605 int isl_tab_allocate_con(struct isl_tab *tab)
1607 int r;
1609 isl_assert(tab->mat->ctx, tab->n_row < tab->mat->n_row, return -1);
1610 isl_assert(tab->mat->ctx, tab->n_con < tab->max_con, return -1);
1612 r = tab->n_con;
1613 tab->con[r].index = tab->n_row;
1614 tab->con[r].is_row = 1;
1615 tab->con[r].is_nonneg = 0;
1616 tab->con[r].is_zero = 0;
1617 tab->con[r].is_redundant = 0;
1618 tab->con[r].frozen = 0;
1619 tab->con[r].negated = 0;
1620 tab->row_var[tab->n_row] = ~r;
1622 tab->n_row++;
1623 tab->n_con++;
1624 if (isl_tab_push_var(tab, isl_tab_undo_allocate, &tab->con[r]) < 0)
1625 return -1;
1627 return r;
1630 /* Add a variable to the tableau and allocate a column for it.
1631 * Return the index into the variable array "var".
1633 int isl_tab_allocate_var(struct isl_tab *tab)
1635 int r;
1636 int i;
1637 unsigned off = 2 + tab->M;
1639 isl_assert(tab->mat->ctx, tab->n_col < tab->mat->n_col, return -1);
1640 isl_assert(tab->mat->ctx, tab->n_var < tab->max_var, return -1);
1642 r = tab->n_var;
1643 tab->var[r].index = tab->n_col;
1644 tab->var[r].is_row = 0;
1645 tab->var[r].is_nonneg = 0;
1646 tab->var[r].is_zero = 0;
1647 tab->var[r].is_redundant = 0;
1648 tab->var[r].frozen = 0;
1649 tab->var[r].negated = 0;
1650 tab->col_var[tab->n_col] = r;
1652 for (i = 0; i < tab->n_row; ++i)
1653 isl_int_set_si(tab->mat->row[i][off + tab->n_col], 0);
1655 tab->n_var++;
1656 tab->n_col++;
1657 if (isl_tab_push_var(tab, isl_tab_undo_allocate, &tab->var[r]) < 0)
1658 return -1;
1660 return r;
1663 /* Add a row to the tableau. The row is given as an affine combination
1664 * of the original variables and needs to be expressed in terms of the
1665 * column variables.
1667 * We add each term in turn.
1668 * If r = n/d_r is the current sum and we need to add k x, then
1669 * if x is a column variable, we increase the numerator of
1670 * this column by k d_r
1671 * if x = f/d_x is a row variable, then the new representation of r is
1673 * n k f d_x/g n + d_r/g k f m/d_r n + m/d_g k f
1674 * --- + --- = ------------------- = -------------------
1675 * d_r d_r d_r d_x/g m
1677 * with g the gcd of d_r and d_x and m the lcm of d_r and d_x.
1679 * If tab->M is set, then, internally, each variable x is represented
1680 * as x' - M. We then also need no subtract k d_r from the coefficient of M.
1682 int isl_tab_add_row(struct isl_tab *tab, isl_int *line)
1684 int i;
1685 int r;
1686 isl_int *row;
1687 isl_int a, b;
1688 unsigned off = 2 + tab->M;
1690 r = isl_tab_allocate_con(tab);
1691 if (r < 0)
1692 return -1;
1694 isl_int_init(a);
1695 isl_int_init(b);
1696 row = tab->mat->row[tab->con[r].index];
1697 isl_int_set_si(row[0], 1);
1698 isl_int_set(row[1], line[0]);
1699 isl_seq_clr(row + 2, tab->M + tab->n_col);
1700 for (i = 0; i < tab->n_var; ++i) {
1701 if (tab->var[i].is_zero)
1702 continue;
1703 if (tab->var[i].is_row) {
1704 isl_int_lcm(a,
1705 row[0], tab->mat->row[tab->var[i].index][0]);
1706 isl_int_swap(a, row[0]);
1707 isl_int_divexact(a, row[0], a);
1708 isl_int_divexact(b,
1709 row[0], tab->mat->row[tab->var[i].index][0]);
1710 isl_int_mul(b, b, line[1 + i]);
1711 isl_seq_combine(row + 1, a, row + 1,
1712 b, tab->mat->row[tab->var[i].index] + 1,
1713 1 + tab->M + tab->n_col);
1714 } else
1715 isl_int_addmul(row[off + tab->var[i].index],
1716 line[1 + i], row[0]);
1717 if (tab->M && i >= tab->n_param && i < tab->n_var - tab->n_div)
1718 isl_int_submul(row[2], line[1 + i], row[0]);
1720 isl_seq_normalize(tab->mat->ctx, row, off + tab->n_col);
1721 isl_int_clear(a);
1722 isl_int_clear(b);
1724 if (tab->row_sign)
1725 tab->row_sign[tab->con[r].index] = isl_tab_row_unknown;
1727 return r;
1730 static int drop_row(struct isl_tab *tab, int row)
1732 isl_assert(tab->mat->ctx, ~tab->row_var[row] == tab->n_con - 1, return -1);
1733 if (row != tab->n_row - 1)
1734 swap_rows(tab, row, tab->n_row - 1);
1735 tab->n_row--;
1736 tab->n_con--;
1737 return 0;
1740 static int drop_col(struct isl_tab *tab, int col)
1742 isl_assert(tab->mat->ctx, tab->col_var[col] == tab->n_var - 1, return -1);
1743 if (col != tab->n_col - 1)
1744 swap_cols(tab, col, tab->n_col - 1);
1745 tab->n_col--;
1746 tab->n_var--;
1747 return 0;
1750 /* Add inequality "ineq" and check if it conflicts with the
1751 * previously added constraints or if it is obviously redundant.
1753 int isl_tab_add_ineq(struct isl_tab *tab, isl_int *ineq)
1755 int r;
1756 int sgn;
1757 isl_int cst;
1759 if (!tab)
1760 return -1;
1761 if (tab->bmap) {
1762 struct isl_basic_map *bmap = tab->bmap;
1764 isl_assert(tab->mat->ctx, tab->n_eq == bmap->n_eq, return -1);
1765 isl_assert(tab->mat->ctx,
1766 tab->n_con == bmap->n_eq + bmap->n_ineq, return -1);
1767 tab->bmap = isl_basic_map_add_ineq(tab->bmap, ineq);
1768 if (isl_tab_push(tab, isl_tab_undo_bmap_ineq) < 0)
1769 return -1;
1770 if (!tab->bmap)
1771 return -1;
1773 if (tab->cone) {
1774 isl_int_init(cst);
1775 isl_int_swap(ineq[0], cst);
1777 r = isl_tab_add_row(tab, ineq);
1778 if (tab->cone) {
1779 isl_int_swap(ineq[0], cst);
1780 isl_int_clear(cst);
1782 if (r < 0)
1783 return -1;
1784 tab->con[r].is_nonneg = 1;
1785 if (isl_tab_push_var(tab, isl_tab_undo_nonneg, &tab->con[r]) < 0)
1786 return -1;
1787 if (isl_tab_row_is_redundant(tab, tab->con[r].index)) {
1788 if (isl_tab_mark_redundant(tab, tab->con[r].index) < 0)
1789 return -1;
1790 return 0;
1793 sgn = restore_row(tab, &tab->con[r]);
1794 if (sgn < -1)
1795 return -1;
1796 if (sgn < 0)
1797 return isl_tab_mark_empty(tab);
1798 if (tab->con[r].is_row && isl_tab_row_is_redundant(tab, tab->con[r].index))
1799 if (isl_tab_mark_redundant(tab, tab->con[r].index) < 0)
1800 return -1;
1801 return 0;
1804 /* Pivot a non-negative variable down until it reaches the value zero
1805 * and then pivot the variable into a column position.
1807 static int to_col(struct isl_tab *tab, struct isl_tab_var *var) WARN_UNUSED;
1808 static int to_col(struct isl_tab *tab, struct isl_tab_var *var)
1810 int i;
1811 int row, col;
1812 unsigned off = 2 + tab->M;
1814 if (!var->is_row)
1815 return 0;
1817 while (isl_int_is_pos(tab->mat->row[var->index][1])) {
1818 find_pivot(tab, var, NULL, -1, &row, &col);
1819 isl_assert(tab->mat->ctx, row != -1, return -1);
1820 if (isl_tab_pivot(tab, row, col) < 0)
1821 return -1;
1822 if (!var->is_row)
1823 return 0;
1826 for (i = tab->n_dead; i < tab->n_col; ++i)
1827 if (!isl_int_is_zero(tab->mat->row[var->index][off + i]))
1828 break;
1830 isl_assert(tab->mat->ctx, i < tab->n_col, return -1);
1831 if (isl_tab_pivot(tab, var->index, i) < 0)
1832 return -1;
1834 return 0;
1837 /* We assume Gaussian elimination has been performed on the equalities.
1838 * The equalities can therefore never conflict.
1839 * Adding the equalities is currently only really useful for a later call
1840 * to isl_tab_ineq_type.
1842 static struct isl_tab *add_eq(struct isl_tab *tab, isl_int *eq)
1844 int i;
1845 int r;
1847 if (!tab)
1848 return NULL;
1849 r = isl_tab_add_row(tab, eq);
1850 if (r < 0)
1851 goto error;
1853 r = tab->con[r].index;
1854 i = isl_seq_first_non_zero(tab->mat->row[r] + 2 + tab->M + tab->n_dead,
1855 tab->n_col - tab->n_dead);
1856 isl_assert(tab->mat->ctx, i >= 0, goto error);
1857 i += tab->n_dead;
1858 if (isl_tab_pivot(tab, r, i) < 0)
1859 goto error;
1860 if (isl_tab_kill_col(tab, i) < 0)
1861 goto error;
1862 tab->n_eq++;
1864 return tab;
1865 error:
1866 isl_tab_free(tab);
1867 return NULL;
1870 static int row_is_manifestly_zero(struct isl_tab *tab, int row)
1872 unsigned off = 2 + tab->M;
1874 if (!isl_int_is_zero(tab->mat->row[row][1]))
1875 return 0;
1876 if (tab->M && !isl_int_is_zero(tab->mat->row[row][2]))
1877 return 0;
1878 return isl_seq_first_non_zero(tab->mat->row[row] + off + tab->n_dead,
1879 tab->n_col - tab->n_dead) == -1;
1882 /* Add an equality that is known to be valid for the given tableau.
1884 int isl_tab_add_valid_eq(struct isl_tab *tab, isl_int *eq)
1886 struct isl_tab_var *var;
1887 int r;
1889 if (!tab)
1890 return -1;
1891 r = isl_tab_add_row(tab, eq);
1892 if (r < 0)
1893 return -1;
1895 var = &tab->con[r];
1896 r = var->index;
1897 if (row_is_manifestly_zero(tab, r)) {
1898 var->is_zero = 1;
1899 if (isl_tab_mark_redundant(tab, r) < 0)
1900 return -1;
1901 return 0;
1904 if (isl_int_is_neg(tab->mat->row[r][1])) {
1905 isl_seq_neg(tab->mat->row[r] + 1, tab->mat->row[r] + 1,
1906 1 + tab->n_col);
1907 var->negated = 1;
1909 var->is_nonneg = 1;
1910 if (to_col(tab, var) < 0)
1911 return -1;
1912 var->is_nonneg = 0;
1913 if (isl_tab_kill_col(tab, var->index) < 0)
1914 return -1;
1916 return 0;
1919 static int add_zero_row(struct isl_tab *tab)
1921 int r;
1922 isl_int *row;
1924 r = isl_tab_allocate_con(tab);
1925 if (r < 0)
1926 return -1;
1928 row = tab->mat->row[tab->con[r].index];
1929 isl_seq_clr(row + 1, 1 + tab->M + tab->n_col);
1930 isl_int_set_si(row[0], 1);
1932 return r;
1935 /* Add equality "eq" and check if it conflicts with the
1936 * previously added constraints or if it is obviously redundant.
1938 int isl_tab_add_eq(struct isl_tab *tab, isl_int *eq)
1940 struct isl_tab_undo *snap = NULL;
1941 struct isl_tab_var *var;
1942 int r;
1943 int row;
1944 int sgn;
1945 isl_int cst;
1947 if (!tab)
1948 return -1;
1949 isl_assert(tab->mat->ctx, !tab->M, return -1);
1951 if (tab->need_undo)
1952 snap = isl_tab_snap(tab);
1954 if (tab->cone) {
1955 isl_int_init(cst);
1956 isl_int_swap(eq[0], cst);
1958 r = isl_tab_add_row(tab, eq);
1959 if (tab->cone) {
1960 isl_int_swap(eq[0], cst);
1961 isl_int_clear(cst);
1963 if (r < 0)
1964 return -1;
1966 var = &tab->con[r];
1967 row = var->index;
1968 if (row_is_manifestly_zero(tab, row)) {
1969 if (snap) {
1970 if (isl_tab_rollback(tab, snap) < 0)
1971 return -1;
1972 } else
1973 drop_row(tab, row);
1974 return 0;
1977 if (tab->bmap) {
1978 tab->bmap = isl_basic_map_add_ineq(tab->bmap, eq);
1979 if (isl_tab_push(tab, isl_tab_undo_bmap_ineq) < 0)
1980 return -1;
1981 isl_seq_neg(eq, eq, 1 + tab->n_var);
1982 tab->bmap = isl_basic_map_add_ineq(tab->bmap, eq);
1983 isl_seq_neg(eq, eq, 1 + tab->n_var);
1984 if (isl_tab_push(tab, isl_tab_undo_bmap_ineq) < 0)
1985 return -1;
1986 if (!tab->bmap)
1987 return -1;
1988 if (add_zero_row(tab) < 0)
1989 return -1;
1992 sgn = isl_int_sgn(tab->mat->row[row][1]);
1994 if (sgn > 0) {
1995 isl_seq_neg(tab->mat->row[row] + 1, tab->mat->row[row] + 1,
1996 1 + tab->n_col);
1997 var->negated = 1;
1998 sgn = -1;
2001 if (sgn < 0) {
2002 sgn = sign_of_max(tab, var);
2003 if (sgn < -1)
2004 return -1;
2005 if (sgn < 0) {
2006 if (isl_tab_mark_empty(tab) < 0)
2007 return -1;
2008 return 0;
2012 var->is_nonneg = 1;
2013 if (to_col(tab, var) < 0)
2014 return -1;
2015 var->is_nonneg = 0;
2016 if (isl_tab_kill_col(tab, var->index) < 0)
2017 return -1;
2019 return 0;
2022 /* Construct and return an inequality that expresses an upper bound
2023 * on the given div.
2024 * In particular, if the div is given by
2026 * d = floor(e/m)
2028 * then the inequality expresses
2030 * m d <= e
2032 static struct isl_vec *ineq_for_div(struct isl_basic_map *bmap, unsigned div)
2034 unsigned total;
2035 unsigned div_pos;
2036 struct isl_vec *ineq;
2038 if (!bmap)
2039 return NULL;
2041 total = isl_basic_map_total_dim(bmap);
2042 div_pos = 1 + total - bmap->n_div + div;
2044 ineq = isl_vec_alloc(bmap->ctx, 1 + total);
2045 if (!ineq)
2046 return NULL;
2048 isl_seq_cpy(ineq->el, bmap->div[div] + 1, 1 + total);
2049 isl_int_neg(ineq->el[div_pos], bmap->div[div][0]);
2050 return ineq;
2053 /* For a div d = floor(f/m), add the constraints
2055 * f - m d >= 0
2056 * -(f-(m-1)) + m d >= 0
2058 * Note that the second constraint is the negation of
2060 * f - m d >= m
2062 * If add_ineq is not NULL, then this function is used
2063 * instead of isl_tab_add_ineq to effectively add the inequalities.
2065 static int add_div_constraints(struct isl_tab *tab, unsigned div,
2066 int (*add_ineq)(void *user, isl_int *), void *user)
2068 unsigned total;
2069 unsigned div_pos;
2070 struct isl_vec *ineq;
2072 total = isl_basic_map_total_dim(tab->bmap);
2073 div_pos = 1 + total - tab->bmap->n_div + div;
2075 ineq = ineq_for_div(tab->bmap, div);
2076 if (!ineq)
2077 goto error;
2079 if (add_ineq) {
2080 if (add_ineq(user, ineq->el) < 0)
2081 goto error;
2082 } else {
2083 if (isl_tab_add_ineq(tab, ineq->el) < 0)
2084 goto error;
2087 isl_seq_neg(ineq->el, tab->bmap->div[div] + 1, 1 + total);
2088 isl_int_set(ineq->el[div_pos], tab->bmap->div[div][0]);
2089 isl_int_add(ineq->el[0], ineq->el[0], ineq->el[div_pos]);
2090 isl_int_sub_ui(ineq->el[0], ineq->el[0], 1);
2092 if (add_ineq) {
2093 if (add_ineq(user, ineq->el) < 0)
2094 goto error;
2095 } else {
2096 if (isl_tab_add_ineq(tab, ineq->el) < 0)
2097 goto error;
2100 isl_vec_free(ineq);
2102 return 0;
2103 error:
2104 isl_vec_free(ineq);
2105 return -1;
2108 /* Check whether the div described by "div" is obviously non-negative.
2109 * If we are using a big parameter, then we will encode the div
2110 * as div' = M + div, which is always non-negative.
2111 * Otherwise, we check whether div is a non-negative affine combination
2112 * of non-negative variables.
2114 static int div_is_nonneg(struct isl_tab *tab, __isl_keep isl_vec *div)
2116 int i;
2118 if (tab->M)
2119 return 1;
2121 if (isl_int_is_neg(div->el[1]))
2122 return 0;
2124 for (i = 0; i < tab->n_var; ++i) {
2125 if (isl_int_is_neg(div->el[2 + i]))
2126 return 0;
2127 if (isl_int_is_zero(div->el[2 + i]))
2128 continue;
2129 if (!tab->var[i].is_nonneg)
2130 return 0;
2133 return 1;
2136 /* Add an extra div, prescribed by "div" to the tableau and
2137 * the associated bmap (which is assumed to be non-NULL).
2139 * If add_ineq is not NULL, then this function is used instead
2140 * of isl_tab_add_ineq to add the div constraints.
2141 * This complication is needed because the code in isl_tab_pip
2142 * wants to perform some extra processing when an inequality
2143 * is added to the tableau.
2145 int isl_tab_add_div(struct isl_tab *tab, __isl_keep isl_vec *div,
2146 int (*add_ineq)(void *user, isl_int *), void *user)
2148 int r;
2149 int k;
2150 int nonneg;
2152 if (!tab || !div)
2153 return -1;
2155 isl_assert(tab->mat->ctx, tab->bmap, return -1);
2157 nonneg = div_is_nonneg(tab, div);
2159 if (isl_tab_extend_cons(tab, 3) < 0)
2160 return -1;
2161 if (isl_tab_extend_vars(tab, 1) < 0)
2162 return -1;
2163 r = isl_tab_allocate_var(tab);
2164 if (r < 0)
2165 return -1;
2167 if (nonneg)
2168 tab->var[r].is_nonneg = 1;
2170 tab->bmap = isl_basic_map_extend_dim(tab->bmap,
2171 isl_basic_map_get_dim(tab->bmap), 1, 0, 2);
2172 k = isl_basic_map_alloc_div(tab->bmap);
2173 if (k < 0)
2174 return -1;
2175 isl_seq_cpy(tab->bmap->div[k], div->el, div->size);
2176 if (isl_tab_push(tab, isl_tab_undo_bmap_div) < 0)
2177 return -1;
2179 if (add_div_constraints(tab, k, add_ineq, user) < 0)
2180 return -1;
2182 return r;
2185 struct isl_tab *isl_tab_from_basic_map(struct isl_basic_map *bmap)
2187 int i;
2188 struct isl_tab *tab;
2190 if (!bmap)
2191 return NULL;
2192 tab = isl_tab_alloc(bmap->ctx,
2193 isl_basic_map_total_dim(bmap) + bmap->n_ineq + 1,
2194 isl_basic_map_total_dim(bmap), 0);
2195 if (!tab)
2196 return NULL;
2197 tab->rational = ISL_F_ISSET(bmap, ISL_BASIC_MAP_RATIONAL);
2198 if (ISL_F_ISSET(bmap, ISL_BASIC_MAP_EMPTY)) {
2199 if (isl_tab_mark_empty(tab) < 0)
2200 goto error;
2201 return tab;
2203 for (i = 0; i < bmap->n_eq; ++i) {
2204 tab = add_eq(tab, bmap->eq[i]);
2205 if (!tab)
2206 return tab;
2208 for (i = 0; i < bmap->n_ineq; ++i) {
2209 if (isl_tab_add_ineq(tab, bmap->ineq[i]) < 0)
2210 goto error;
2211 if (tab->empty)
2212 return tab;
2214 return tab;
2215 error:
2216 isl_tab_free(tab);
2217 return NULL;
2220 struct isl_tab *isl_tab_from_basic_set(struct isl_basic_set *bset)
2222 return isl_tab_from_basic_map((struct isl_basic_map *)bset);
2225 /* Construct a tableau corresponding to the recession cone of "bset".
2227 struct isl_tab *isl_tab_from_recession_cone(__isl_keep isl_basic_set *bset,
2228 int parametric)
2230 isl_int cst;
2231 int i;
2232 struct isl_tab *tab;
2233 unsigned offset = 0;
2235 if (!bset)
2236 return NULL;
2237 if (parametric)
2238 offset = isl_basic_set_dim(bset, isl_dim_param);
2239 tab = isl_tab_alloc(bset->ctx, bset->n_eq + bset->n_ineq,
2240 isl_basic_set_total_dim(bset) - offset, 0);
2241 if (!tab)
2242 return NULL;
2243 tab->rational = ISL_F_ISSET(bset, ISL_BASIC_SET_RATIONAL);
2244 tab->cone = 1;
2246 isl_int_init(cst);
2247 for (i = 0; i < bset->n_eq; ++i) {
2248 isl_int_swap(bset->eq[i][offset], cst);
2249 if (offset > 0) {
2250 if (isl_tab_add_eq(tab, bset->eq[i] + offset) < 0)
2251 goto error;
2252 } else
2253 tab = add_eq(tab, bset->eq[i]);
2254 isl_int_swap(bset->eq[i][offset], cst);
2255 if (!tab)
2256 goto done;
2258 for (i = 0; i < bset->n_ineq; ++i) {
2259 int r;
2260 isl_int_swap(bset->ineq[i][offset], cst);
2261 r = isl_tab_add_row(tab, bset->ineq[i] + offset);
2262 isl_int_swap(bset->ineq[i][offset], cst);
2263 if (r < 0)
2264 goto error;
2265 tab->con[r].is_nonneg = 1;
2266 if (isl_tab_push_var(tab, isl_tab_undo_nonneg, &tab->con[r]) < 0)
2267 goto error;
2269 done:
2270 isl_int_clear(cst);
2271 return tab;
2272 error:
2273 isl_int_clear(cst);
2274 isl_tab_free(tab);
2275 return NULL;
2278 /* Assuming "tab" is the tableau of a cone, check if the cone is
2279 * bounded, i.e., if it is empty or only contains the origin.
2281 int isl_tab_cone_is_bounded(struct isl_tab *tab)
2283 int i;
2285 if (!tab)
2286 return -1;
2287 if (tab->empty)
2288 return 1;
2289 if (tab->n_dead == tab->n_col)
2290 return 1;
2292 for (;;) {
2293 for (i = tab->n_redundant; i < tab->n_row; ++i) {
2294 struct isl_tab_var *var;
2295 int sgn;
2296 var = isl_tab_var_from_row(tab, i);
2297 if (!var->is_nonneg)
2298 continue;
2299 sgn = sign_of_max(tab, var);
2300 if (sgn < -1)
2301 return -1;
2302 if (sgn != 0)
2303 return 0;
2304 if (close_row(tab, var) < 0)
2305 return -1;
2306 break;
2308 if (tab->n_dead == tab->n_col)
2309 return 1;
2310 if (i == tab->n_row)
2311 return 0;
2315 int isl_tab_sample_is_integer(struct isl_tab *tab)
2317 int i;
2319 if (!tab)
2320 return -1;
2322 for (i = 0; i < tab->n_var; ++i) {
2323 int row;
2324 if (!tab->var[i].is_row)
2325 continue;
2326 row = tab->var[i].index;
2327 if (!isl_int_is_divisible_by(tab->mat->row[row][1],
2328 tab->mat->row[row][0]))
2329 return 0;
2331 return 1;
2334 static struct isl_vec *extract_integer_sample(struct isl_tab *tab)
2336 int i;
2337 struct isl_vec *vec;
2339 vec = isl_vec_alloc(tab->mat->ctx, 1 + tab->n_var);
2340 if (!vec)
2341 return NULL;
2343 isl_int_set_si(vec->block.data[0], 1);
2344 for (i = 0; i < tab->n_var; ++i) {
2345 if (!tab->var[i].is_row)
2346 isl_int_set_si(vec->block.data[1 + i], 0);
2347 else {
2348 int row = tab->var[i].index;
2349 isl_int_divexact(vec->block.data[1 + i],
2350 tab->mat->row[row][1], tab->mat->row[row][0]);
2354 return vec;
2357 struct isl_vec *isl_tab_get_sample_value(struct isl_tab *tab)
2359 int i;
2360 struct isl_vec *vec;
2361 isl_int m;
2363 if (!tab)
2364 return NULL;
2366 vec = isl_vec_alloc(tab->mat->ctx, 1 + tab->n_var);
2367 if (!vec)
2368 return NULL;
2370 isl_int_init(m);
2372 isl_int_set_si(vec->block.data[0], 1);
2373 for (i = 0; i < tab->n_var; ++i) {
2374 int row;
2375 if (!tab->var[i].is_row) {
2376 isl_int_set_si(vec->block.data[1 + i], 0);
2377 continue;
2379 row = tab->var[i].index;
2380 isl_int_gcd(m, vec->block.data[0], tab->mat->row[row][0]);
2381 isl_int_divexact(m, tab->mat->row[row][0], m);
2382 isl_seq_scale(vec->block.data, vec->block.data, m, 1 + i);
2383 isl_int_divexact(m, vec->block.data[0], tab->mat->row[row][0]);
2384 isl_int_mul(vec->block.data[1 + i], m, tab->mat->row[row][1]);
2386 vec = isl_vec_normalize(vec);
2388 isl_int_clear(m);
2389 return vec;
2392 /* Update "bmap" based on the results of the tableau "tab".
2393 * In particular, implicit equalities are made explicit, redundant constraints
2394 * are removed and if the sample value happens to be integer, it is stored
2395 * in "bmap" (unless "bmap" already had an integer sample).
2397 * The tableau is assumed to have been created from "bmap" using
2398 * isl_tab_from_basic_map.
2400 struct isl_basic_map *isl_basic_map_update_from_tab(struct isl_basic_map *bmap,
2401 struct isl_tab *tab)
2403 int i;
2404 unsigned n_eq;
2406 if (!bmap)
2407 return NULL;
2408 if (!tab)
2409 return bmap;
2411 n_eq = tab->n_eq;
2412 if (tab->empty)
2413 bmap = isl_basic_map_set_to_empty(bmap);
2414 else
2415 for (i = bmap->n_ineq - 1; i >= 0; --i) {
2416 if (isl_tab_is_equality(tab, n_eq + i))
2417 isl_basic_map_inequality_to_equality(bmap, i);
2418 else if (isl_tab_is_redundant(tab, n_eq + i))
2419 isl_basic_map_drop_inequality(bmap, i);
2421 if (bmap->n_eq != n_eq)
2422 isl_basic_map_gauss(bmap, NULL);
2423 if (!tab->rational &&
2424 !bmap->sample && isl_tab_sample_is_integer(tab))
2425 bmap->sample = extract_integer_sample(tab);
2426 return bmap;
2429 struct isl_basic_set *isl_basic_set_update_from_tab(struct isl_basic_set *bset,
2430 struct isl_tab *tab)
2432 return (struct isl_basic_set *)isl_basic_map_update_from_tab(
2433 (struct isl_basic_map *)bset, tab);
2436 /* Given a non-negative variable "var", add a new non-negative variable
2437 * that is the opposite of "var", ensuring that var can only attain the
2438 * value zero.
2439 * If var = n/d is a row variable, then the new variable = -n/d.
2440 * If var is a column variables, then the new variable = -var.
2441 * If the new variable cannot attain non-negative values, then
2442 * the resulting tableau is empty.
2443 * Otherwise, we know the value will be zero and we close the row.
2445 static int cut_to_hyperplane(struct isl_tab *tab, struct isl_tab_var *var)
2447 unsigned r;
2448 isl_int *row;
2449 int sgn;
2450 unsigned off = 2 + tab->M;
2452 if (var->is_zero)
2453 return 0;
2454 isl_assert(tab->mat->ctx, !var->is_redundant, return -1);
2455 isl_assert(tab->mat->ctx, var->is_nonneg, return -1);
2457 if (isl_tab_extend_cons(tab, 1) < 0)
2458 return -1;
2460 r = tab->n_con;
2461 tab->con[r].index = tab->n_row;
2462 tab->con[r].is_row = 1;
2463 tab->con[r].is_nonneg = 0;
2464 tab->con[r].is_zero = 0;
2465 tab->con[r].is_redundant = 0;
2466 tab->con[r].frozen = 0;
2467 tab->con[r].negated = 0;
2468 tab->row_var[tab->n_row] = ~r;
2469 row = tab->mat->row[tab->n_row];
2471 if (var->is_row) {
2472 isl_int_set(row[0], tab->mat->row[var->index][0]);
2473 isl_seq_neg(row + 1,
2474 tab->mat->row[var->index] + 1, 1 + tab->n_col);
2475 } else {
2476 isl_int_set_si(row[0], 1);
2477 isl_seq_clr(row + 1, 1 + tab->n_col);
2478 isl_int_set_si(row[off + var->index], -1);
2481 tab->n_row++;
2482 tab->n_con++;
2483 if (isl_tab_push_var(tab, isl_tab_undo_allocate, &tab->con[r]) < 0)
2484 return -1;
2486 sgn = sign_of_max(tab, &tab->con[r]);
2487 if (sgn < -1)
2488 return -1;
2489 if (sgn < 0) {
2490 if (isl_tab_mark_empty(tab) < 0)
2491 return -1;
2492 return 0;
2494 tab->con[r].is_nonneg = 1;
2495 if (isl_tab_push_var(tab, isl_tab_undo_nonneg, &tab->con[r]) < 0)
2496 return -1;
2497 /* sgn == 0 */
2498 if (close_row(tab, &tab->con[r]) < 0)
2499 return -1;
2501 return 0;
2504 /* Given a tableau "tab" and an inequality constraint "con" of the tableau,
2505 * relax the inequality by one. That is, the inequality r >= 0 is replaced
2506 * by r' = r + 1 >= 0.
2507 * If r is a row variable, we simply increase the constant term by one
2508 * (taking into account the denominator).
2509 * If r is a column variable, then we need to modify each row that
2510 * refers to r = r' - 1 by substituting this equality, effectively
2511 * subtracting the coefficient of the column from the constant.
2512 * We should only do this if the minimum is manifestly unbounded,
2513 * however. Otherwise, we may end up with negative sample values
2514 * for non-negative variables.
2515 * So, if r is a column variable with a minimum that is not
2516 * manifestly unbounded, then we need to move it to a row.
2517 * However, the sample value of this row may be negative,
2518 * even after the relaxation, so we need to restore it.
2519 * We therefore prefer to pivot a column up to a row, if possible.
2521 struct isl_tab *isl_tab_relax(struct isl_tab *tab, int con)
2523 struct isl_tab_var *var;
2524 unsigned off = 2 + tab->M;
2526 if (!tab)
2527 return NULL;
2529 var = &tab->con[con];
2531 if (!var->is_row && !max_is_manifestly_unbounded(tab, var))
2532 if (to_row(tab, var, 1) < 0)
2533 goto error;
2534 if (!var->is_row && !min_is_manifestly_unbounded(tab, var))
2535 if (to_row(tab, var, -1) < 0)
2536 goto error;
2538 if (var->is_row) {
2539 isl_int_add(tab->mat->row[var->index][1],
2540 tab->mat->row[var->index][1], tab->mat->row[var->index][0]);
2541 if (restore_row(tab, var) < 0)
2542 goto error;
2543 } else {
2544 int i;
2546 for (i = 0; i < tab->n_row; ++i) {
2547 if (isl_int_is_zero(tab->mat->row[i][off + var->index]))
2548 continue;
2549 isl_int_sub(tab->mat->row[i][1], tab->mat->row[i][1],
2550 tab->mat->row[i][off + var->index]);
2555 if (isl_tab_push_var(tab, isl_tab_undo_relax, var) < 0)
2556 goto error;
2558 return tab;
2559 error:
2560 isl_tab_free(tab);
2561 return NULL;
2564 int isl_tab_select_facet(struct isl_tab *tab, int con)
2566 if (!tab)
2567 return -1;
2569 return cut_to_hyperplane(tab, &tab->con[con]);
2572 static int may_be_equality(struct isl_tab *tab, int row)
2574 unsigned off = 2 + tab->M;
2575 return tab->rational ? isl_int_is_zero(tab->mat->row[row][1])
2576 : isl_int_lt(tab->mat->row[row][1],
2577 tab->mat->row[row][0]);
2580 /* Check for (near) equalities among the constraints.
2581 * A constraint is an equality if it is non-negative and if
2582 * its maximal value is either
2583 * - zero (in case of rational tableaus), or
2584 * - strictly less than 1 (in case of integer tableaus)
2586 * We first mark all non-redundant and non-dead variables that
2587 * are not frozen and not obviously not an equality.
2588 * Then we iterate over all marked variables if they can attain
2589 * any values larger than zero or at least one.
2590 * If the maximal value is zero, we mark any column variables
2591 * that appear in the row as being zero and mark the row as being redundant.
2592 * Otherwise, if the maximal value is strictly less than one (and the
2593 * tableau is integer), then we restrict the value to being zero
2594 * by adding an opposite non-negative variable.
2596 int isl_tab_detect_implicit_equalities(struct isl_tab *tab)
2598 int i;
2599 unsigned n_marked;
2601 if (!tab)
2602 return -1;
2603 if (tab->empty)
2604 return 0;
2605 if (tab->n_dead == tab->n_col)
2606 return 0;
2608 n_marked = 0;
2609 for (i = tab->n_redundant; i < tab->n_row; ++i) {
2610 struct isl_tab_var *var = isl_tab_var_from_row(tab, i);
2611 var->marked = !var->frozen && var->is_nonneg &&
2612 may_be_equality(tab, i);
2613 if (var->marked)
2614 n_marked++;
2616 for (i = tab->n_dead; i < tab->n_col; ++i) {
2617 struct isl_tab_var *var = var_from_col(tab, i);
2618 var->marked = !var->frozen && var->is_nonneg;
2619 if (var->marked)
2620 n_marked++;
2622 while (n_marked) {
2623 struct isl_tab_var *var;
2624 int sgn;
2625 for (i = tab->n_redundant; i < tab->n_row; ++i) {
2626 var = isl_tab_var_from_row(tab, i);
2627 if (var->marked)
2628 break;
2630 if (i == tab->n_row) {
2631 for (i = tab->n_dead; i < tab->n_col; ++i) {
2632 var = var_from_col(tab, i);
2633 if (var->marked)
2634 break;
2636 if (i == tab->n_col)
2637 break;
2639 var->marked = 0;
2640 n_marked--;
2641 sgn = sign_of_max(tab, var);
2642 if (sgn < 0)
2643 return -1;
2644 if (sgn == 0) {
2645 if (close_row(tab, var) < 0)
2646 return -1;
2647 } else if (!tab->rational && !at_least_one(tab, var)) {
2648 if (cut_to_hyperplane(tab, var) < 0)
2649 return -1;
2650 return isl_tab_detect_implicit_equalities(tab);
2652 for (i = tab->n_redundant; i < tab->n_row; ++i) {
2653 var = isl_tab_var_from_row(tab, i);
2654 if (!var->marked)
2655 continue;
2656 if (may_be_equality(tab, i))
2657 continue;
2658 var->marked = 0;
2659 n_marked--;
2663 return 0;
2666 static int con_is_redundant(struct isl_tab *tab, struct isl_tab_var *var)
2668 if (!tab)
2669 return -1;
2670 if (tab->rational) {
2671 int sgn = sign_of_min(tab, var);
2672 if (sgn < -1)
2673 return -1;
2674 return sgn >= 0;
2675 } else {
2676 int irred = isl_tab_min_at_most_neg_one(tab, var);
2677 if (irred < 0)
2678 return -1;
2679 return !irred;
2683 /* Check for (near) redundant constraints.
2684 * A constraint is redundant if it is non-negative and if
2685 * its minimal value (temporarily ignoring the non-negativity) is either
2686 * - zero (in case of rational tableaus), or
2687 * - strictly larger than -1 (in case of integer tableaus)
2689 * We first mark all non-redundant and non-dead variables that
2690 * are not frozen and not obviously negatively unbounded.
2691 * Then we iterate over all marked variables if they can attain
2692 * any values smaller than zero or at most negative one.
2693 * If not, we mark the row as being redundant (assuming it hasn't
2694 * been detected as being obviously redundant in the mean time).
2696 int isl_tab_detect_redundant(struct isl_tab *tab)
2698 int i;
2699 unsigned n_marked;
2701 if (!tab)
2702 return -1;
2703 if (tab->empty)
2704 return 0;
2705 if (tab->n_redundant == tab->n_row)
2706 return 0;
2708 n_marked = 0;
2709 for (i = tab->n_redundant; i < tab->n_row; ++i) {
2710 struct isl_tab_var *var = isl_tab_var_from_row(tab, i);
2711 var->marked = !var->frozen && var->is_nonneg;
2712 if (var->marked)
2713 n_marked++;
2715 for (i = tab->n_dead; i < tab->n_col; ++i) {
2716 struct isl_tab_var *var = var_from_col(tab, i);
2717 var->marked = !var->frozen && var->is_nonneg &&
2718 !min_is_manifestly_unbounded(tab, var);
2719 if (var->marked)
2720 n_marked++;
2722 while (n_marked) {
2723 struct isl_tab_var *var;
2724 int red;
2725 for (i = tab->n_redundant; i < tab->n_row; ++i) {
2726 var = isl_tab_var_from_row(tab, i);
2727 if (var->marked)
2728 break;
2730 if (i == tab->n_row) {
2731 for (i = tab->n_dead; i < tab->n_col; ++i) {
2732 var = var_from_col(tab, i);
2733 if (var->marked)
2734 break;
2736 if (i == tab->n_col)
2737 break;
2739 var->marked = 0;
2740 n_marked--;
2741 red = con_is_redundant(tab, var);
2742 if (red < 0)
2743 return -1;
2744 if (red && !var->is_redundant)
2745 if (isl_tab_mark_redundant(tab, var->index) < 0)
2746 return -1;
2747 for (i = tab->n_dead; i < tab->n_col; ++i) {
2748 var = var_from_col(tab, i);
2749 if (!var->marked)
2750 continue;
2751 if (!min_is_manifestly_unbounded(tab, var))
2752 continue;
2753 var->marked = 0;
2754 n_marked--;
2758 return 0;
2761 int isl_tab_is_equality(struct isl_tab *tab, int con)
2763 int row;
2764 unsigned off;
2766 if (!tab)
2767 return -1;
2768 if (tab->con[con].is_zero)
2769 return 1;
2770 if (tab->con[con].is_redundant)
2771 return 0;
2772 if (!tab->con[con].is_row)
2773 return tab->con[con].index < tab->n_dead;
2775 row = tab->con[con].index;
2777 off = 2 + tab->M;
2778 return isl_int_is_zero(tab->mat->row[row][1]) &&
2779 (!tab->M || isl_int_is_zero(tab->mat->row[row][2])) &&
2780 isl_seq_first_non_zero(tab->mat->row[row] + off + tab->n_dead,
2781 tab->n_col - tab->n_dead) == -1;
2784 /* Return the minimal value of the affine expression "f" with denominator
2785 * "denom" in *opt, *opt_denom, assuming the tableau is not empty and
2786 * the expression cannot attain arbitrarily small values.
2787 * If opt_denom is NULL, then *opt is rounded up to the nearest integer.
2788 * The return value reflects the nature of the result (empty, unbounded,
2789 * minimal value returned in *opt).
2791 enum isl_lp_result isl_tab_min(struct isl_tab *tab,
2792 isl_int *f, isl_int denom, isl_int *opt, isl_int *opt_denom,
2793 unsigned flags)
2795 int r;
2796 enum isl_lp_result res = isl_lp_ok;
2797 struct isl_tab_var *var;
2798 struct isl_tab_undo *snap;
2800 if (!tab)
2801 return isl_lp_error;
2803 if (tab->empty)
2804 return isl_lp_empty;
2806 snap = isl_tab_snap(tab);
2807 r = isl_tab_add_row(tab, f);
2808 if (r < 0)
2809 return isl_lp_error;
2810 var = &tab->con[r];
2811 for (;;) {
2812 int row, col;
2813 find_pivot(tab, var, var, -1, &row, &col);
2814 if (row == var->index) {
2815 res = isl_lp_unbounded;
2816 break;
2818 if (row == -1)
2819 break;
2820 if (isl_tab_pivot(tab, row, col) < 0)
2821 return isl_lp_error;
2823 isl_int_mul(tab->mat->row[var->index][0],
2824 tab->mat->row[var->index][0], denom);
2825 if (ISL_FL_ISSET(flags, ISL_TAB_SAVE_DUAL)) {
2826 int i;
2828 isl_vec_free(tab->dual);
2829 tab->dual = isl_vec_alloc(tab->mat->ctx, 1 + tab->n_con);
2830 if (!tab->dual)
2831 return isl_lp_error;
2832 isl_int_set(tab->dual->el[0], tab->mat->row[var->index][0]);
2833 for (i = 0; i < tab->n_con; ++i) {
2834 int pos;
2835 if (tab->con[i].is_row) {
2836 isl_int_set_si(tab->dual->el[1 + i], 0);
2837 continue;
2839 pos = 2 + tab->M + tab->con[i].index;
2840 if (tab->con[i].negated)
2841 isl_int_neg(tab->dual->el[1 + i],
2842 tab->mat->row[var->index][pos]);
2843 else
2844 isl_int_set(tab->dual->el[1 + i],
2845 tab->mat->row[var->index][pos]);
2848 if (opt && res == isl_lp_ok) {
2849 if (opt_denom) {
2850 isl_int_set(*opt, tab->mat->row[var->index][1]);
2851 isl_int_set(*opt_denom, tab->mat->row[var->index][0]);
2852 } else
2853 isl_int_cdiv_q(*opt, tab->mat->row[var->index][1],
2854 tab->mat->row[var->index][0]);
2856 if (isl_tab_rollback(tab, snap) < 0)
2857 return isl_lp_error;
2858 return res;
2861 int isl_tab_is_redundant(struct isl_tab *tab, int con)
2863 if (!tab)
2864 return -1;
2865 if (tab->con[con].is_zero)
2866 return 0;
2867 if (tab->con[con].is_redundant)
2868 return 1;
2869 return tab->con[con].is_row && tab->con[con].index < tab->n_redundant;
2872 /* Take a snapshot of the tableau that can be restored by s call to
2873 * isl_tab_rollback.
2875 struct isl_tab_undo *isl_tab_snap(struct isl_tab *tab)
2877 if (!tab)
2878 return NULL;
2879 tab->need_undo = 1;
2880 return tab->top;
2883 /* Undo the operation performed by isl_tab_relax.
2885 static int unrelax(struct isl_tab *tab, struct isl_tab_var *var) WARN_UNUSED;
2886 static int unrelax(struct isl_tab *tab, struct isl_tab_var *var)
2888 unsigned off = 2 + tab->M;
2890 if (!var->is_row && !max_is_manifestly_unbounded(tab, var))
2891 if (to_row(tab, var, 1) < 0)
2892 return -1;
2894 if (var->is_row) {
2895 isl_int_sub(tab->mat->row[var->index][1],
2896 tab->mat->row[var->index][1], tab->mat->row[var->index][0]);
2897 if (var->is_nonneg) {
2898 int sgn = restore_row(tab, var);
2899 isl_assert(tab->mat->ctx, sgn >= 0, return -1);
2901 } else {
2902 int i;
2904 for (i = 0; i < tab->n_row; ++i) {
2905 if (isl_int_is_zero(tab->mat->row[i][off + var->index]))
2906 continue;
2907 isl_int_add(tab->mat->row[i][1], tab->mat->row[i][1],
2908 tab->mat->row[i][off + var->index]);
2913 return 0;
2916 static int perform_undo_var(struct isl_tab *tab, struct isl_tab_undo *undo) WARN_UNUSED;
2917 static int perform_undo_var(struct isl_tab *tab, struct isl_tab_undo *undo)
2919 struct isl_tab_var *var = var_from_index(tab, undo->u.var_index);
2920 switch(undo->type) {
2921 case isl_tab_undo_nonneg:
2922 var->is_nonneg = 0;
2923 break;
2924 case isl_tab_undo_redundant:
2925 var->is_redundant = 0;
2926 tab->n_redundant--;
2927 restore_row(tab, isl_tab_var_from_row(tab, tab->n_redundant));
2928 break;
2929 case isl_tab_undo_freeze:
2930 var->frozen = 0;
2931 break;
2932 case isl_tab_undo_zero:
2933 var->is_zero = 0;
2934 if (!var->is_row)
2935 tab->n_dead--;
2936 break;
2937 case isl_tab_undo_allocate:
2938 if (undo->u.var_index >= 0) {
2939 isl_assert(tab->mat->ctx, !var->is_row, return -1);
2940 drop_col(tab, var->index);
2941 break;
2943 if (!var->is_row) {
2944 if (!max_is_manifestly_unbounded(tab, var)) {
2945 if (to_row(tab, var, 1) < 0)
2946 return -1;
2947 } else if (!min_is_manifestly_unbounded(tab, var)) {
2948 if (to_row(tab, var, -1) < 0)
2949 return -1;
2950 } else
2951 if (to_row(tab, var, 0) < 0)
2952 return -1;
2954 drop_row(tab, var->index);
2955 break;
2956 case isl_tab_undo_relax:
2957 return unrelax(tab, var);
2960 return 0;
2963 /* Restore the tableau to the state where the basic variables
2964 * are those in "col_var".
2965 * We first construct a list of variables that are currently in
2966 * the basis, but shouldn't. Then we iterate over all variables
2967 * that should be in the basis and for each one that is currently
2968 * not in the basis, we exchange it with one of the elements of the
2969 * list constructed before.
2970 * We can always find an appropriate variable to pivot with because
2971 * the current basis is mapped to the old basis by a non-singular
2972 * matrix and so we can never end up with a zero row.
2974 static int restore_basis(struct isl_tab *tab, int *col_var)
2976 int i, j;
2977 int n_extra = 0;
2978 int *extra = NULL; /* current columns that contain bad stuff */
2979 unsigned off = 2 + tab->M;
2981 extra = isl_alloc_array(tab->mat->ctx, int, tab->n_col);
2982 if (!extra)
2983 goto error;
2984 for (i = 0; i < tab->n_col; ++i) {
2985 for (j = 0; j < tab->n_col; ++j)
2986 if (tab->col_var[i] == col_var[j])
2987 break;
2988 if (j < tab->n_col)
2989 continue;
2990 extra[n_extra++] = i;
2992 for (i = 0; i < tab->n_col && n_extra > 0; ++i) {
2993 struct isl_tab_var *var;
2994 int row;
2996 for (j = 0; j < tab->n_col; ++j)
2997 if (col_var[i] == tab->col_var[j])
2998 break;
2999 if (j < tab->n_col)
3000 continue;
3001 var = var_from_index(tab, col_var[i]);
3002 row = var->index;
3003 for (j = 0; j < n_extra; ++j)
3004 if (!isl_int_is_zero(tab->mat->row[row][off+extra[j]]))
3005 break;
3006 isl_assert(tab->mat->ctx, j < n_extra, goto error);
3007 if (isl_tab_pivot(tab, row, extra[j]) < 0)
3008 goto error;
3009 extra[j] = extra[--n_extra];
3012 free(extra);
3013 free(col_var);
3014 return 0;
3015 error:
3016 free(extra);
3017 free(col_var);
3018 return -1;
3021 /* Remove all samples with index n or greater, i.e., those samples
3022 * that were added since we saved this number of samples in
3023 * isl_tab_save_samples.
3025 static void drop_samples_since(struct isl_tab *tab, int n)
3027 int i;
3029 for (i = tab->n_sample - 1; i >= 0 && tab->n_sample > n; --i) {
3030 if (tab->sample_index[i] < n)
3031 continue;
3033 if (i != tab->n_sample - 1) {
3034 int t = tab->sample_index[tab->n_sample-1];
3035 tab->sample_index[tab->n_sample-1] = tab->sample_index[i];
3036 tab->sample_index[i] = t;
3037 isl_mat_swap_rows(tab->samples, tab->n_sample-1, i);
3039 tab->n_sample--;
3043 static int perform_undo(struct isl_tab *tab, struct isl_tab_undo *undo) WARN_UNUSED;
3044 static int perform_undo(struct isl_tab *tab, struct isl_tab_undo *undo)
3046 switch (undo->type) {
3047 case isl_tab_undo_empty:
3048 tab->empty = 0;
3049 break;
3050 case isl_tab_undo_nonneg:
3051 case isl_tab_undo_redundant:
3052 case isl_tab_undo_freeze:
3053 case isl_tab_undo_zero:
3054 case isl_tab_undo_allocate:
3055 case isl_tab_undo_relax:
3056 return perform_undo_var(tab, undo);
3057 case isl_tab_undo_bmap_eq:
3058 return isl_basic_map_free_equality(tab->bmap, 1);
3059 case isl_tab_undo_bmap_ineq:
3060 return isl_basic_map_free_inequality(tab->bmap, 1);
3061 case isl_tab_undo_bmap_div:
3062 if (isl_basic_map_free_div(tab->bmap, 1) < 0)
3063 return -1;
3064 if (tab->samples)
3065 tab->samples->n_col--;
3066 break;
3067 case isl_tab_undo_saved_basis:
3068 if (restore_basis(tab, undo->u.col_var) < 0)
3069 return -1;
3070 break;
3071 case isl_tab_undo_drop_sample:
3072 tab->n_outside--;
3073 break;
3074 case isl_tab_undo_saved_samples:
3075 drop_samples_since(tab, undo->u.n);
3076 break;
3077 case isl_tab_undo_callback:
3078 return undo->u.callback->run(undo->u.callback);
3079 default:
3080 isl_assert(tab->mat->ctx, 0, return -1);
3082 return 0;
3085 /* Return the tableau to the state it was in when the snapshot "snap"
3086 * was taken.
3088 int isl_tab_rollback(struct isl_tab *tab, struct isl_tab_undo *snap)
3090 struct isl_tab_undo *undo, *next;
3092 if (!tab)
3093 return -1;
3095 tab->in_undo = 1;
3096 for (undo = tab->top; undo && undo != &tab->bottom; undo = next) {
3097 next = undo->next;
3098 if (undo == snap)
3099 break;
3100 if (perform_undo(tab, undo) < 0) {
3101 tab->top = undo;
3102 free_undo(tab);
3103 tab->in_undo = 0;
3104 return -1;
3106 free(undo);
3108 tab->in_undo = 0;
3109 tab->top = undo;
3110 if (!undo)
3111 return -1;
3112 return 0;
3115 /* The given row "row" represents an inequality violated by all
3116 * points in the tableau. Check for some special cases of such
3117 * separating constraints.
3118 * In particular, if the row has been reduced to the constant -1,
3119 * then we know the inequality is adjacent (but opposite) to
3120 * an equality in the tableau.
3121 * If the row has been reduced to r = c*(-1 -r'), with r' an inequality
3122 * of the tableau and c a positive constant, then the inequality
3123 * is adjacent (but opposite) to the inequality r'.
3125 static enum isl_ineq_type separation_type(struct isl_tab *tab, unsigned row)
3127 int pos;
3128 unsigned off = 2 + tab->M;
3130 if (tab->rational)
3131 return isl_ineq_separate;
3133 if (!isl_int_is_one(tab->mat->row[row][0]))
3134 return isl_ineq_separate;
3136 pos = isl_seq_first_non_zero(tab->mat->row[row] + off + tab->n_dead,
3137 tab->n_col - tab->n_dead);
3138 if (pos == -1) {
3139 if (isl_int_is_negone(tab->mat->row[row][1]))
3140 return isl_ineq_adj_eq;
3141 else
3142 return isl_ineq_separate;
3145 if (!isl_int_eq(tab->mat->row[row][1],
3146 tab->mat->row[row][off + tab->n_dead + pos]))
3147 return isl_ineq_separate;
3149 pos = isl_seq_first_non_zero(
3150 tab->mat->row[row] + off + tab->n_dead + pos + 1,
3151 tab->n_col - tab->n_dead - pos - 1);
3153 return pos == -1 ? isl_ineq_adj_ineq : isl_ineq_separate;
3156 /* Check the effect of inequality "ineq" on the tableau "tab".
3157 * The result may be
3158 * isl_ineq_redundant: satisfied by all points in the tableau
3159 * isl_ineq_separate: satisfied by no point in the tableau
3160 * isl_ineq_cut: satisfied by some by not all points
3161 * isl_ineq_adj_eq: adjacent to an equality
3162 * isl_ineq_adj_ineq: adjacent to an inequality.
3164 enum isl_ineq_type isl_tab_ineq_type(struct isl_tab *tab, isl_int *ineq)
3166 enum isl_ineq_type type = isl_ineq_error;
3167 struct isl_tab_undo *snap = NULL;
3168 int con;
3169 int row;
3171 if (!tab)
3172 return isl_ineq_error;
3174 if (isl_tab_extend_cons(tab, 1) < 0)
3175 return isl_ineq_error;
3177 snap = isl_tab_snap(tab);
3179 con = isl_tab_add_row(tab, ineq);
3180 if (con < 0)
3181 goto error;
3183 row = tab->con[con].index;
3184 if (isl_tab_row_is_redundant(tab, row))
3185 type = isl_ineq_redundant;
3186 else if (isl_int_is_neg(tab->mat->row[row][1]) &&
3187 (tab->rational ||
3188 isl_int_abs_ge(tab->mat->row[row][1],
3189 tab->mat->row[row][0]))) {
3190 int nonneg = at_least_zero(tab, &tab->con[con]);
3191 if (nonneg < 0)
3192 goto error;
3193 if (nonneg)
3194 type = isl_ineq_cut;
3195 else
3196 type = separation_type(tab, row);
3197 } else {
3198 int red = con_is_redundant(tab, &tab->con[con]);
3199 if (red < 0)
3200 goto error;
3201 if (!red)
3202 type = isl_ineq_cut;
3203 else
3204 type = isl_ineq_redundant;
3207 if (isl_tab_rollback(tab, snap))
3208 return isl_ineq_error;
3209 return type;
3210 error:
3211 return isl_ineq_error;
3214 int isl_tab_track_bmap(struct isl_tab *tab, __isl_take isl_basic_map *bmap)
3216 if (!tab || !bmap)
3217 goto error;
3219 isl_assert(tab->mat->ctx, tab->n_eq == bmap->n_eq, return -1);
3220 isl_assert(tab->mat->ctx,
3221 tab->n_con == bmap->n_eq + bmap->n_ineq, return -1);
3223 tab->bmap = bmap;
3225 return 0;
3226 error:
3227 isl_basic_map_free(bmap);
3228 return -1;
3231 int isl_tab_track_bset(struct isl_tab *tab, __isl_take isl_basic_set *bset)
3233 return isl_tab_track_bmap(tab, (isl_basic_map *)bset);
3236 __isl_keep isl_basic_set *isl_tab_peek_bset(struct isl_tab *tab)
3238 if (!tab)
3239 return NULL;
3241 return (isl_basic_set *)tab->bmap;
3244 void isl_tab_dump(struct isl_tab *tab, FILE *out, int indent)
3246 unsigned r, c;
3247 int i;
3249 if (!tab) {
3250 fprintf(out, "%*snull tab\n", indent, "");
3251 return;
3253 fprintf(out, "%*sn_redundant: %d, n_dead: %d", indent, "",
3254 tab->n_redundant, tab->n_dead);
3255 if (tab->rational)
3256 fprintf(out, ", rational");
3257 if (tab->empty)
3258 fprintf(out, ", empty");
3259 fprintf(out, "\n");
3260 fprintf(out, "%*s[", indent, "");
3261 for (i = 0; i < tab->n_var; ++i) {
3262 if (i)
3263 fprintf(out, (i == tab->n_param ||
3264 i == tab->n_var - tab->n_div) ? "; "
3265 : ", ");
3266 fprintf(out, "%c%d%s", tab->var[i].is_row ? 'r' : 'c',
3267 tab->var[i].index,
3268 tab->var[i].is_zero ? " [=0]" :
3269 tab->var[i].is_redundant ? " [R]" : "");
3271 fprintf(out, "]\n");
3272 fprintf(out, "%*s[", indent, "");
3273 for (i = 0; i < tab->n_con; ++i) {
3274 if (i)
3275 fprintf(out, ", ");
3276 fprintf(out, "%c%d%s", tab->con[i].is_row ? 'r' : 'c',
3277 tab->con[i].index,
3278 tab->con[i].is_zero ? " [=0]" :
3279 tab->con[i].is_redundant ? " [R]" : "");
3281 fprintf(out, "]\n");
3282 fprintf(out, "%*s[", indent, "");
3283 for (i = 0; i < tab->n_row; ++i) {
3284 const char *sign = "";
3285 if (i)
3286 fprintf(out, ", ");
3287 if (tab->row_sign) {
3288 if (tab->row_sign[i] == isl_tab_row_unknown)
3289 sign = "?";
3290 else if (tab->row_sign[i] == isl_tab_row_neg)
3291 sign = "-";
3292 else if (tab->row_sign[i] == isl_tab_row_pos)
3293 sign = "+";
3294 else
3295 sign = "+-";
3297 fprintf(out, "r%d: %d%s%s", i, tab->row_var[i],
3298 isl_tab_var_from_row(tab, i)->is_nonneg ? " [>=0]" : "", sign);
3300 fprintf(out, "]\n");
3301 fprintf(out, "%*s[", indent, "");
3302 for (i = 0; i < tab->n_col; ++i) {
3303 if (i)
3304 fprintf(out, ", ");
3305 fprintf(out, "c%d: %d%s", i, tab->col_var[i],
3306 var_from_col(tab, i)->is_nonneg ? " [>=0]" : "");
3308 fprintf(out, "]\n");
3309 r = tab->mat->n_row;
3310 tab->mat->n_row = tab->n_row;
3311 c = tab->mat->n_col;
3312 tab->mat->n_col = 2 + tab->M + tab->n_col;
3313 isl_mat_dump(tab->mat, out, indent);
3314 tab->mat->n_row = r;
3315 tab->mat->n_col = c;
3316 if (tab->bmap)
3317 isl_basic_map_print_internal(tab->bmap, out, indent);