isl_tab.c: perform_undo_var: propagate errors of drop_row and drop_col
[isl.git] / isl_tab.c
blob1aea6ff6929803d3393a82c158e09a94801957cd
1 /*
2 * Copyright 2008-2009 Katholieke Universiteit Leuven
3 * Copyright 2013 Ecole Normale Superieure
5 * Use of this software is governed by the MIT license
7 * Written by Sven Verdoolaege, K.U.Leuven, Departement
8 * Computerwetenschappen, Celestijnenlaan 200A, B-3001 Leuven, Belgium
9 * and Ecole Normale Superieure, 45 rue d'Ulm, 75230 Paris, France
12 #include <isl_ctx_private.h>
13 #include <isl_mat_private.h>
14 #include <isl_vec_private.h>
15 #include "isl_map_private.h"
16 #include "isl_tab.h"
17 #include <isl_seq.h>
18 #include <isl_config.h>
21 * The implementation of tableaus in this file was inspired by Section 8
22 * of David Detlefs, Greg Nelson and James B. Saxe, "Simplify: a theorem
23 * prover for program checking".
26 struct isl_tab *isl_tab_alloc(struct isl_ctx *ctx,
27 unsigned n_row, unsigned n_var, unsigned M)
29 int i;
30 struct isl_tab *tab;
31 unsigned off = 2 + M;
33 tab = isl_calloc_type(ctx, struct isl_tab);
34 if (!tab)
35 return NULL;
36 tab->mat = isl_mat_alloc(ctx, n_row, off + n_var);
37 if (!tab->mat)
38 goto error;
39 tab->var = isl_alloc_array(ctx, struct isl_tab_var, n_var);
40 if (n_var && !tab->var)
41 goto error;
42 tab->con = isl_alloc_array(ctx, struct isl_tab_var, n_row);
43 if (n_row && !tab->con)
44 goto error;
45 tab->col_var = isl_alloc_array(ctx, int, n_var);
46 if (n_var && !tab->col_var)
47 goto error;
48 tab->row_var = isl_alloc_array(ctx, int, n_row);
49 if (n_row && !tab->row_var)
50 goto error;
51 for (i = 0; i < n_var; ++i) {
52 tab->var[i].index = i;
53 tab->var[i].is_row = 0;
54 tab->var[i].is_nonneg = 0;
55 tab->var[i].is_zero = 0;
56 tab->var[i].is_redundant = 0;
57 tab->var[i].frozen = 0;
58 tab->var[i].negated = 0;
59 tab->col_var[i] = i;
61 tab->n_row = 0;
62 tab->n_con = 0;
63 tab->n_eq = 0;
64 tab->max_con = n_row;
65 tab->n_col = n_var;
66 tab->n_var = n_var;
67 tab->max_var = n_var;
68 tab->n_param = 0;
69 tab->n_div = 0;
70 tab->n_dead = 0;
71 tab->n_redundant = 0;
72 tab->strict_redundant = 0;
73 tab->need_undo = 0;
74 tab->rational = 0;
75 tab->empty = 0;
76 tab->in_undo = 0;
77 tab->M = M;
78 tab->cone = 0;
79 tab->bottom.type = isl_tab_undo_bottom;
80 tab->bottom.next = NULL;
81 tab->top = &tab->bottom;
83 tab->n_zero = 0;
84 tab->n_unbounded = 0;
85 tab->basis = NULL;
87 return tab;
88 error:
89 isl_tab_free(tab);
90 return NULL;
93 isl_ctx *isl_tab_get_ctx(struct isl_tab *tab)
95 return tab ? isl_mat_get_ctx(tab->mat) : NULL;
98 int isl_tab_extend_cons(struct isl_tab *tab, unsigned n_new)
100 unsigned off;
102 if (!tab)
103 return -1;
105 off = 2 + tab->M;
107 if (tab->max_con < tab->n_con + n_new) {
108 struct isl_tab_var *con;
110 con = isl_realloc_array(tab->mat->ctx, tab->con,
111 struct isl_tab_var, tab->max_con + n_new);
112 if (!con)
113 return -1;
114 tab->con = con;
115 tab->max_con += n_new;
117 if (tab->mat->n_row < tab->n_row + n_new) {
118 int *row_var;
120 tab->mat = isl_mat_extend(tab->mat,
121 tab->n_row + n_new, off + tab->n_col);
122 if (!tab->mat)
123 return -1;
124 row_var = isl_realloc_array(tab->mat->ctx, tab->row_var,
125 int, tab->mat->n_row);
126 if (!row_var)
127 return -1;
128 tab->row_var = row_var;
129 if (tab->row_sign) {
130 enum isl_tab_row_sign *s;
131 s = isl_realloc_array(tab->mat->ctx, tab->row_sign,
132 enum isl_tab_row_sign, tab->mat->n_row);
133 if (!s)
134 return -1;
135 tab->row_sign = s;
138 return 0;
141 /* Make room for at least n_new extra variables.
142 * Return -1 if anything went wrong.
144 int isl_tab_extend_vars(struct isl_tab *tab, unsigned n_new)
146 struct isl_tab_var *var;
147 unsigned off = 2 + tab->M;
149 if (tab->max_var < tab->n_var + n_new) {
150 var = isl_realloc_array(tab->mat->ctx, tab->var,
151 struct isl_tab_var, tab->n_var + n_new);
152 if (!var)
153 return -1;
154 tab->var = var;
155 tab->max_var += n_new;
158 if (tab->mat->n_col < off + tab->n_col + n_new) {
159 int *p;
161 tab->mat = isl_mat_extend(tab->mat,
162 tab->mat->n_row, off + tab->n_col + n_new);
163 if (!tab->mat)
164 return -1;
165 p = isl_realloc_array(tab->mat->ctx, tab->col_var,
166 int, tab->n_col + n_new);
167 if (!p)
168 return -1;
169 tab->col_var = p;
172 return 0;
175 static void free_undo_record(struct isl_tab_undo *undo)
177 switch (undo->type) {
178 case isl_tab_undo_saved_basis:
179 free(undo->u.col_var);
180 break;
181 default:;
183 free(undo);
186 static void free_undo(struct isl_tab *tab)
188 struct isl_tab_undo *undo, *next;
190 for (undo = tab->top; undo && undo != &tab->bottom; undo = next) {
191 next = undo->next;
192 free_undo_record(undo);
194 tab->top = undo;
197 void isl_tab_free(struct isl_tab *tab)
199 if (!tab)
200 return;
201 free_undo(tab);
202 isl_mat_free(tab->mat);
203 isl_vec_free(tab->dual);
204 isl_basic_map_free(tab->bmap);
205 free(tab->var);
206 free(tab->con);
207 free(tab->row_var);
208 free(tab->col_var);
209 free(tab->row_sign);
210 isl_mat_free(tab->samples);
211 free(tab->sample_index);
212 isl_mat_free(tab->basis);
213 free(tab);
216 struct isl_tab *isl_tab_dup(struct isl_tab *tab)
218 int i;
219 struct isl_tab *dup;
220 unsigned off;
222 if (!tab)
223 return NULL;
225 off = 2 + tab->M;
226 dup = isl_calloc_type(tab->mat->ctx, struct isl_tab);
227 if (!dup)
228 return NULL;
229 dup->mat = isl_mat_dup(tab->mat);
230 if (!dup->mat)
231 goto error;
232 dup->var = isl_alloc_array(tab->mat->ctx, struct isl_tab_var, tab->max_var);
233 if (tab->max_var && !dup->var)
234 goto error;
235 for (i = 0; i < tab->n_var; ++i)
236 dup->var[i] = tab->var[i];
237 dup->con = isl_alloc_array(tab->mat->ctx, struct isl_tab_var, tab->max_con);
238 if (tab->max_con && !dup->con)
239 goto error;
240 for (i = 0; i < tab->n_con; ++i)
241 dup->con[i] = tab->con[i];
242 dup->col_var = isl_alloc_array(tab->mat->ctx, int, tab->mat->n_col - off);
243 if ((tab->mat->n_col - off) && !dup->col_var)
244 goto error;
245 for (i = 0; i < tab->n_col; ++i)
246 dup->col_var[i] = tab->col_var[i];
247 dup->row_var = isl_alloc_array(tab->mat->ctx, int, tab->mat->n_row);
248 if (tab->mat->n_row && !dup->row_var)
249 goto error;
250 for (i = 0; i < tab->n_row; ++i)
251 dup->row_var[i] = tab->row_var[i];
252 if (tab->row_sign) {
253 dup->row_sign = isl_alloc_array(tab->mat->ctx, enum isl_tab_row_sign,
254 tab->mat->n_row);
255 if (tab->mat->n_row && !dup->row_sign)
256 goto error;
257 for (i = 0; i < tab->n_row; ++i)
258 dup->row_sign[i] = tab->row_sign[i];
260 if (tab->samples) {
261 dup->samples = isl_mat_dup(tab->samples);
262 if (!dup->samples)
263 goto error;
264 dup->sample_index = isl_alloc_array(tab->mat->ctx, int,
265 tab->samples->n_row);
266 if (tab->samples->n_row && !dup->sample_index)
267 goto error;
268 dup->n_sample = tab->n_sample;
269 dup->n_outside = tab->n_outside;
271 dup->n_row = tab->n_row;
272 dup->n_con = tab->n_con;
273 dup->n_eq = tab->n_eq;
274 dup->max_con = tab->max_con;
275 dup->n_col = tab->n_col;
276 dup->n_var = tab->n_var;
277 dup->max_var = tab->max_var;
278 dup->n_param = tab->n_param;
279 dup->n_div = tab->n_div;
280 dup->n_dead = tab->n_dead;
281 dup->n_redundant = tab->n_redundant;
282 dup->rational = tab->rational;
283 dup->empty = tab->empty;
284 dup->strict_redundant = 0;
285 dup->need_undo = 0;
286 dup->in_undo = 0;
287 dup->M = tab->M;
288 tab->cone = tab->cone;
289 dup->bottom.type = isl_tab_undo_bottom;
290 dup->bottom.next = NULL;
291 dup->top = &dup->bottom;
293 dup->n_zero = tab->n_zero;
294 dup->n_unbounded = tab->n_unbounded;
295 dup->basis = isl_mat_dup(tab->basis);
297 return dup;
298 error:
299 isl_tab_free(dup);
300 return NULL;
303 /* Construct the coefficient matrix of the product tableau
304 * of two tableaus.
305 * mat{1,2} is the coefficient matrix of tableau {1,2}
306 * row{1,2} is the number of rows in tableau {1,2}
307 * col{1,2} is the number of columns in tableau {1,2}
308 * off is the offset to the coefficient column (skipping the
309 * denominator, the constant term and the big parameter if any)
310 * r{1,2} is the number of redundant rows in tableau {1,2}
311 * d{1,2} is the number of dead columns in tableau {1,2}
313 * The order of the rows and columns in the result is as explained
314 * in isl_tab_product.
316 static struct isl_mat *tab_mat_product(struct isl_mat *mat1,
317 struct isl_mat *mat2, unsigned row1, unsigned row2,
318 unsigned col1, unsigned col2,
319 unsigned off, unsigned r1, unsigned r2, unsigned d1, unsigned d2)
321 int i;
322 struct isl_mat *prod;
323 unsigned n;
325 prod = isl_mat_alloc(mat1->ctx, mat1->n_row + mat2->n_row,
326 off + col1 + col2);
327 if (!prod)
328 return NULL;
330 n = 0;
331 for (i = 0; i < r1; ++i) {
332 isl_seq_cpy(prod->row[n + i], mat1->row[i], off + d1);
333 isl_seq_clr(prod->row[n + i] + off + d1, d2);
334 isl_seq_cpy(prod->row[n + i] + off + d1 + d2,
335 mat1->row[i] + off + d1, col1 - d1);
336 isl_seq_clr(prod->row[n + i] + off + col1 + d1, col2 - d2);
339 n += r1;
340 for (i = 0; i < r2; ++i) {
341 isl_seq_cpy(prod->row[n + i], mat2->row[i], off);
342 isl_seq_clr(prod->row[n + i] + off, d1);
343 isl_seq_cpy(prod->row[n + i] + off + d1,
344 mat2->row[i] + off, d2);
345 isl_seq_clr(prod->row[n + i] + off + d1 + d2, col1 - d1);
346 isl_seq_cpy(prod->row[n + i] + off + col1 + d1,
347 mat2->row[i] + off + d2, col2 - d2);
350 n += r2;
351 for (i = 0; i < row1 - r1; ++i) {
352 isl_seq_cpy(prod->row[n + i], mat1->row[r1 + i], off + d1);
353 isl_seq_clr(prod->row[n + i] + off + d1, d2);
354 isl_seq_cpy(prod->row[n + i] + off + d1 + d2,
355 mat1->row[r1 + i] + off + d1, col1 - d1);
356 isl_seq_clr(prod->row[n + i] + off + col1 + d1, col2 - d2);
359 n += row1 - r1;
360 for (i = 0; i < row2 - r2; ++i) {
361 isl_seq_cpy(prod->row[n + i], mat2->row[r2 + i], off);
362 isl_seq_clr(prod->row[n + i] + off, d1);
363 isl_seq_cpy(prod->row[n + i] + off + d1,
364 mat2->row[r2 + i] + off, d2);
365 isl_seq_clr(prod->row[n + i] + off + d1 + d2, col1 - d1);
366 isl_seq_cpy(prod->row[n + i] + off + col1 + d1,
367 mat2->row[r2 + i] + off + d2, col2 - d2);
370 return prod;
373 /* Update the row or column index of a variable that corresponds
374 * to a variable in the first input tableau.
376 static void update_index1(struct isl_tab_var *var,
377 unsigned r1, unsigned r2, unsigned d1, unsigned d2)
379 if (var->index == -1)
380 return;
381 if (var->is_row && var->index >= r1)
382 var->index += r2;
383 if (!var->is_row && var->index >= d1)
384 var->index += d2;
387 /* Update the row or column index of a variable that corresponds
388 * to a variable in the second input tableau.
390 static void update_index2(struct isl_tab_var *var,
391 unsigned row1, unsigned col1,
392 unsigned r1, unsigned r2, unsigned d1, unsigned d2)
394 if (var->index == -1)
395 return;
396 if (var->is_row) {
397 if (var->index < r2)
398 var->index += r1;
399 else
400 var->index += row1;
401 } else {
402 if (var->index < d2)
403 var->index += d1;
404 else
405 var->index += col1;
409 /* Create a tableau that represents the Cartesian product of the sets
410 * represented by tableaus tab1 and tab2.
411 * The order of the rows in the product is
412 * - redundant rows of tab1
413 * - redundant rows of tab2
414 * - non-redundant rows of tab1
415 * - non-redundant rows of tab2
416 * The order of the columns is
417 * - denominator
418 * - constant term
419 * - coefficient of big parameter, if any
420 * - dead columns of tab1
421 * - dead columns of tab2
422 * - live columns of tab1
423 * - live columns of tab2
424 * The order of the variables and the constraints is a concatenation
425 * of order in the two input tableaus.
427 struct isl_tab *isl_tab_product(struct isl_tab *tab1, struct isl_tab *tab2)
429 int i;
430 struct isl_tab *prod;
431 unsigned off;
432 unsigned r1, r2, d1, d2;
434 if (!tab1 || !tab2)
435 return NULL;
437 isl_assert(tab1->mat->ctx, tab1->M == tab2->M, return NULL);
438 isl_assert(tab1->mat->ctx, tab1->rational == tab2->rational, return NULL);
439 isl_assert(tab1->mat->ctx, tab1->cone == tab2->cone, return NULL);
440 isl_assert(tab1->mat->ctx, !tab1->row_sign, return NULL);
441 isl_assert(tab1->mat->ctx, !tab2->row_sign, return NULL);
442 isl_assert(tab1->mat->ctx, tab1->n_param == 0, return NULL);
443 isl_assert(tab1->mat->ctx, tab2->n_param == 0, return NULL);
444 isl_assert(tab1->mat->ctx, tab1->n_div == 0, return NULL);
445 isl_assert(tab1->mat->ctx, tab2->n_div == 0, return NULL);
447 off = 2 + tab1->M;
448 r1 = tab1->n_redundant;
449 r2 = tab2->n_redundant;
450 d1 = tab1->n_dead;
451 d2 = tab2->n_dead;
452 prod = isl_calloc_type(tab1->mat->ctx, struct isl_tab);
453 if (!prod)
454 return NULL;
455 prod->mat = tab_mat_product(tab1->mat, tab2->mat,
456 tab1->n_row, tab2->n_row,
457 tab1->n_col, tab2->n_col, off, r1, r2, d1, d2);
458 if (!prod->mat)
459 goto error;
460 prod->var = isl_alloc_array(tab1->mat->ctx, struct isl_tab_var,
461 tab1->max_var + tab2->max_var);
462 if ((tab1->max_var + tab2->max_var) && !prod->var)
463 goto error;
464 for (i = 0; i < tab1->n_var; ++i) {
465 prod->var[i] = tab1->var[i];
466 update_index1(&prod->var[i], r1, r2, d1, d2);
468 for (i = 0; i < tab2->n_var; ++i) {
469 prod->var[tab1->n_var + i] = tab2->var[i];
470 update_index2(&prod->var[tab1->n_var + i],
471 tab1->n_row, tab1->n_col,
472 r1, r2, d1, d2);
474 prod->con = isl_alloc_array(tab1->mat->ctx, struct isl_tab_var,
475 tab1->max_con + tab2->max_con);
476 if ((tab1->max_con + tab2->max_con) && !prod->con)
477 goto error;
478 for (i = 0; i < tab1->n_con; ++i) {
479 prod->con[i] = tab1->con[i];
480 update_index1(&prod->con[i], r1, r2, d1, d2);
482 for (i = 0; i < tab2->n_con; ++i) {
483 prod->con[tab1->n_con + i] = tab2->con[i];
484 update_index2(&prod->con[tab1->n_con + i],
485 tab1->n_row, tab1->n_col,
486 r1, r2, d1, d2);
488 prod->col_var = isl_alloc_array(tab1->mat->ctx, int,
489 tab1->n_col + tab2->n_col);
490 if ((tab1->n_col + tab2->n_col) && !prod->col_var)
491 goto error;
492 for (i = 0; i < tab1->n_col; ++i) {
493 int pos = i < d1 ? i : i + d2;
494 prod->col_var[pos] = tab1->col_var[i];
496 for (i = 0; i < tab2->n_col; ++i) {
497 int pos = i < d2 ? d1 + i : tab1->n_col + i;
498 int t = tab2->col_var[i];
499 if (t >= 0)
500 t += tab1->n_var;
501 else
502 t -= tab1->n_con;
503 prod->col_var[pos] = t;
505 prod->row_var = isl_alloc_array(tab1->mat->ctx, int,
506 tab1->mat->n_row + tab2->mat->n_row);
507 if ((tab1->mat->n_row + tab2->mat->n_row) && !prod->row_var)
508 goto error;
509 for (i = 0; i < tab1->n_row; ++i) {
510 int pos = i < r1 ? i : i + r2;
511 prod->row_var[pos] = tab1->row_var[i];
513 for (i = 0; i < tab2->n_row; ++i) {
514 int pos = i < r2 ? r1 + i : tab1->n_row + i;
515 int t = tab2->row_var[i];
516 if (t >= 0)
517 t += tab1->n_var;
518 else
519 t -= tab1->n_con;
520 prod->row_var[pos] = t;
522 prod->samples = NULL;
523 prod->sample_index = NULL;
524 prod->n_row = tab1->n_row + tab2->n_row;
525 prod->n_con = tab1->n_con + tab2->n_con;
526 prod->n_eq = 0;
527 prod->max_con = tab1->max_con + tab2->max_con;
528 prod->n_col = tab1->n_col + tab2->n_col;
529 prod->n_var = tab1->n_var + tab2->n_var;
530 prod->max_var = tab1->max_var + tab2->max_var;
531 prod->n_param = 0;
532 prod->n_div = 0;
533 prod->n_dead = tab1->n_dead + tab2->n_dead;
534 prod->n_redundant = tab1->n_redundant + tab2->n_redundant;
535 prod->rational = tab1->rational;
536 prod->empty = tab1->empty || tab2->empty;
537 prod->strict_redundant = tab1->strict_redundant || tab2->strict_redundant;
538 prod->need_undo = 0;
539 prod->in_undo = 0;
540 prod->M = tab1->M;
541 prod->cone = tab1->cone;
542 prod->bottom.type = isl_tab_undo_bottom;
543 prod->bottom.next = NULL;
544 prod->top = &prod->bottom;
546 prod->n_zero = 0;
547 prod->n_unbounded = 0;
548 prod->basis = NULL;
550 return prod;
551 error:
552 isl_tab_free(prod);
553 return NULL;
556 static struct isl_tab_var *var_from_index(struct isl_tab *tab, int i)
558 if (i >= 0)
559 return &tab->var[i];
560 else
561 return &tab->con[~i];
564 struct isl_tab_var *isl_tab_var_from_row(struct isl_tab *tab, int i)
566 return var_from_index(tab, tab->row_var[i]);
569 static struct isl_tab_var *var_from_col(struct isl_tab *tab, int i)
571 return var_from_index(tab, tab->col_var[i]);
574 /* Check if there are any upper bounds on column variable "var",
575 * i.e., non-negative rows where var appears with a negative coefficient.
576 * Return 1 if there are no such bounds.
578 static int max_is_manifestly_unbounded(struct isl_tab *tab,
579 struct isl_tab_var *var)
581 int i;
582 unsigned off = 2 + tab->M;
584 if (var->is_row)
585 return 0;
586 for (i = tab->n_redundant; i < tab->n_row; ++i) {
587 if (!isl_int_is_neg(tab->mat->row[i][off + var->index]))
588 continue;
589 if (isl_tab_var_from_row(tab, i)->is_nonneg)
590 return 0;
592 return 1;
595 /* Check if there are any lower bounds on column variable "var",
596 * i.e., non-negative rows where var appears with a positive coefficient.
597 * Return 1 if there are no such bounds.
599 static int min_is_manifestly_unbounded(struct isl_tab *tab,
600 struct isl_tab_var *var)
602 int i;
603 unsigned off = 2 + tab->M;
605 if (var->is_row)
606 return 0;
607 for (i = tab->n_redundant; i < tab->n_row; ++i) {
608 if (!isl_int_is_pos(tab->mat->row[i][off + var->index]))
609 continue;
610 if (isl_tab_var_from_row(tab, i)->is_nonneg)
611 return 0;
613 return 1;
616 static int row_cmp(struct isl_tab *tab, int r1, int r2, int c, isl_int t)
618 unsigned off = 2 + tab->M;
620 if (tab->M) {
621 int s;
622 isl_int_mul(t, tab->mat->row[r1][2], tab->mat->row[r2][off+c]);
623 isl_int_submul(t, tab->mat->row[r2][2], tab->mat->row[r1][off+c]);
624 s = isl_int_sgn(t);
625 if (s)
626 return s;
628 isl_int_mul(t, tab->mat->row[r1][1], tab->mat->row[r2][off + c]);
629 isl_int_submul(t, tab->mat->row[r2][1], tab->mat->row[r1][off + c]);
630 return isl_int_sgn(t);
633 /* Given the index of a column "c", return the index of a row
634 * that can be used to pivot the column in, with either an increase
635 * (sgn > 0) or a decrease (sgn < 0) of the corresponding variable.
636 * If "var" is not NULL, then the row returned will be different from
637 * the one associated with "var".
639 * Each row in the tableau is of the form
641 * x_r = a_r0 + \sum_i a_ri x_i
643 * Only rows with x_r >= 0 and with the sign of a_ri opposite to "sgn"
644 * impose any limit on the increase or decrease in the value of x_c
645 * and this bound is equal to a_r0 / |a_rc|. We are therefore looking
646 * for the row with the smallest (most stringent) such bound.
647 * Note that the common denominator of each row drops out of the fraction.
648 * To check if row j has a smaller bound than row r, i.e.,
649 * a_j0 / |a_jc| < a_r0 / |a_rc| or a_j0 |a_rc| < a_r0 |a_jc|,
650 * we check if -sign(a_jc) (a_j0 a_rc - a_r0 a_jc) < 0,
651 * where -sign(a_jc) is equal to "sgn".
653 static int pivot_row(struct isl_tab *tab,
654 struct isl_tab_var *var, int sgn, int c)
656 int j, r, tsgn;
657 isl_int t;
658 unsigned off = 2 + tab->M;
660 isl_int_init(t);
661 r = -1;
662 for (j = tab->n_redundant; j < tab->n_row; ++j) {
663 if (var && j == var->index)
664 continue;
665 if (!isl_tab_var_from_row(tab, j)->is_nonneg)
666 continue;
667 if (sgn * isl_int_sgn(tab->mat->row[j][off + c]) >= 0)
668 continue;
669 if (r < 0) {
670 r = j;
671 continue;
673 tsgn = sgn * row_cmp(tab, r, j, c, t);
674 if (tsgn < 0 || (tsgn == 0 &&
675 tab->row_var[j] < tab->row_var[r]))
676 r = j;
678 isl_int_clear(t);
679 return r;
682 /* Find a pivot (row and col) that will increase (sgn > 0) or decrease
683 * (sgn < 0) the value of row variable var.
684 * If not NULL, then skip_var is a row variable that should be ignored
685 * while looking for a pivot row. It is usually equal to var.
687 * As the given row in the tableau is of the form
689 * x_r = a_r0 + \sum_i a_ri x_i
691 * we need to find a column such that the sign of a_ri is equal to "sgn"
692 * (such that an increase in x_i will have the desired effect) or a
693 * column with a variable that may attain negative values.
694 * If a_ri is positive, then we need to move x_i in the same direction
695 * to obtain the desired effect. Otherwise, x_i has to move in the
696 * opposite direction.
698 static void find_pivot(struct isl_tab *tab,
699 struct isl_tab_var *var, struct isl_tab_var *skip_var,
700 int sgn, int *row, int *col)
702 int j, r, c;
703 isl_int *tr;
705 *row = *col = -1;
707 isl_assert(tab->mat->ctx, var->is_row, return);
708 tr = tab->mat->row[var->index] + 2 + tab->M;
710 c = -1;
711 for (j = tab->n_dead; j < tab->n_col; ++j) {
712 if (isl_int_is_zero(tr[j]))
713 continue;
714 if (isl_int_sgn(tr[j]) != sgn &&
715 var_from_col(tab, j)->is_nonneg)
716 continue;
717 if (c < 0 || tab->col_var[j] < tab->col_var[c])
718 c = j;
720 if (c < 0)
721 return;
723 sgn *= isl_int_sgn(tr[c]);
724 r = pivot_row(tab, skip_var, sgn, c);
725 *row = r < 0 ? var->index : r;
726 *col = c;
729 /* Return 1 if row "row" represents an obviously redundant inequality.
730 * This means
731 * - it represents an inequality or a variable
732 * - that is the sum of a non-negative sample value and a positive
733 * combination of zero or more non-negative constraints.
735 int isl_tab_row_is_redundant(struct isl_tab *tab, int row)
737 int i;
738 unsigned off = 2 + tab->M;
740 if (tab->row_var[row] < 0 && !isl_tab_var_from_row(tab, row)->is_nonneg)
741 return 0;
743 if (isl_int_is_neg(tab->mat->row[row][1]))
744 return 0;
745 if (tab->strict_redundant && isl_int_is_zero(tab->mat->row[row][1]))
746 return 0;
747 if (tab->M && isl_int_is_neg(tab->mat->row[row][2]))
748 return 0;
750 for (i = tab->n_dead; i < tab->n_col; ++i) {
751 if (isl_int_is_zero(tab->mat->row[row][off + i]))
752 continue;
753 if (tab->col_var[i] >= 0)
754 return 0;
755 if (isl_int_is_neg(tab->mat->row[row][off + i]))
756 return 0;
757 if (!var_from_col(tab, i)->is_nonneg)
758 return 0;
760 return 1;
763 static void swap_rows(struct isl_tab *tab, int row1, int row2)
765 int t;
766 enum isl_tab_row_sign s;
768 t = tab->row_var[row1];
769 tab->row_var[row1] = tab->row_var[row2];
770 tab->row_var[row2] = t;
771 isl_tab_var_from_row(tab, row1)->index = row1;
772 isl_tab_var_from_row(tab, row2)->index = row2;
773 tab->mat = isl_mat_swap_rows(tab->mat, row1, row2);
775 if (!tab->row_sign)
776 return;
777 s = tab->row_sign[row1];
778 tab->row_sign[row1] = tab->row_sign[row2];
779 tab->row_sign[row2] = s;
782 static int push_union(struct isl_tab *tab,
783 enum isl_tab_undo_type type, union isl_tab_undo_val u) WARN_UNUSED;
784 static int push_union(struct isl_tab *tab,
785 enum isl_tab_undo_type type, union isl_tab_undo_val u)
787 struct isl_tab_undo *undo;
789 if (!tab)
790 return -1;
791 if (!tab->need_undo)
792 return 0;
794 undo = isl_alloc_type(tab->mat->ctx, struct isl_tab_undo);
795 if (!undo)
796 return -1;
797 undo->type = type;
798 undo->u = u;
799 undo->next = tab->top;
800 tab->top = undo;
802 return 0;
805 int isl_tab_push_var(struct isl_tab *tab,
806 enum isl_tab_undo_type type, struct isl_tab_var *var)
808 union isl_tab_undo_val u;
809 if (var->is_row)
810 u.var_index = tab->row_var[var->index];
811 else
812 u.var_index = tab->col_var[var->index];
813 return push_union(tab, type, u);
816 int isl_tab_push(struct isl_tab *tab, enum isl_tab_undo_type type)
818 union isl_tab_undo_val u = { 0 };
819 return push_union(tab, type, u);
822 /* Push a record on the undo stack describing the current basic
823 * variables, so that the this state can be restored during rollback.
825 int isl_tab_push_basis(struct isl_tab *tab)
827 int i;
828 union isl_tab_undo_val u;
830 u.col_var = isl_alloc_array(tab->mat->ctx, int, tab->n_col);
831 if (tab->n_col && !u.col_var)
832 return -1;
833 for (i = 0; i < tab->n_col; ++i)
834 u.col_var[i] = tab->col_var[i];
835 return push_union(tab, isl_tab_undo_saved_basis, u);
838 int isl_tab_push_callback(struct isl_tab *tab, struct isl_tab_callback *callback)
840 union isl_tab_undo_val u;
841 u.callback = callback;
842 return push_union(tab, isl_tab_undo_callback, u);
845 struct isl_tab *isl_tab_init_samples(struct isl_tab *tab)
847 if (!tab)
848 return NULL;
850 tab->n_sample = 0;
851 tab->n_outside = 0;
852 tab->samples = isl_mat_alloc(tab->mat->ctx, 1, 1 + tab->n_var);
853 if (!tab->samples)
854 goto error;
855 tab->sample_index = isl_alloc_array(tab->mat->ctx, int, 1);
856 if (!tab->sample_index)
857 goto error;
858 return tab;
859 error:
860 isl_tab_free(tab);
861 return NULL;
864 int isl_tab_add_sample(struct isl_tab *tab, __isl_take isl_vec *sample)
866 if (!tab || !sample)
867 goto error;
869 if (tab->n_sample + 1 > tab->samples->n_row) {
870 int *t = isl_realloc_array(tab->mat->ctx,
871 tab->sample_index, int, tab->n_sample + 1);
872 if (!t)
873 goto error;
874 tab->sample_index = t;
877 tab->samples = isl_mat_extend(tab->samples,
878 tab->n_sample + 1, tab->samples->n_col);
879 if (!tab->samples)
880 goto error;
882 isl_seq_cpy(tab->samples->row[tab->n_sample], sample->el, sample->size);
883 isl_vec_free(sample);
884 tab->sample_index[tab->n_sample] = tab->n_sample;
885 tab->n_sample++;
887 return 0;
888 error:
889 isl_vec_free(sample);
890 return -1;
893 struct isl_tab *isl_tab_drop_sample(struct isl_tab *tab, int s)
895 if (s != tab->n_outside) {
896 int t = tab->sample_index[tab->n_outside];
897 tab->sample_index[tab->n_outside] = tab->sample_index[s];
898 tab->sample_index[s] = t;
899 isl_mat_swap_rows(tab->samples, tab->n_outside, s);
901 tab->n_outside++;
902 if (isl_tab_push(tab, isl_tab_undo_drop_sample) < 0) {
903 isl_tab_free(tab);
904 return NULL;
907 return tab;
910 /* Record the current number of samples so that we can remove newer
911 * samples during a rollback.
913 int isl_tab_save_samples(struct isl_tab *tab)
915 union isl_tab_undo_val u;
917 if (!tab)
918 return -1;
920 u.n = tab->n_sample;
921 return push_union(tab, isl_tab_undo_saved_samples, u);
924 /* Mark row with index "row" as being redundant.
925 * If we may need to undo the operation or if the row represents
926 * a variable of the original problem, the row is kept,
927 * but no longer considered when looking for a pivot row.
928 * Otherwise, the row is simply removed.
930 * The row may be interchanged with some other row. If it
931 * is interchanged with a later row, return 1. Otherwise return 0.
932 * If the rows are checked in order in the calling function,
933 * then a return value of 1 means that the row with the given
934 * row number may now contain a different row that hasn't been checked yet.
936 int isl_tab_mark_redundant(struct isl_tab *tab, int row)
938 struct isl_tab_var *var = isl_tab_var_from_row(tab, row);
939 var->is_redundant = 1;
940 isl_assert(tab->mat->ctx, row >= tab->n_redundant, return -1);
941 if (tab->preserve || tab->need_undo || tab->row_var[row] >= 0) {
942 if (tab->row_var[row] >= 0 && !var->is_nonneg) {
943 var->is_nonneg = 1;
944 if (isl_tab_push_var(tab, isl_tab_undo_nonneg, var) < 0)
945 return -1;
947 if (row != tab->n_redundant)
948 swap_rows(tab, row, tab->n_redundant);
949 tab->n_redundant++;
950 return isl_tab_push_var(tab, isl_tab_undo_redundant, var);
951 } else {
952 if (row != tab->n_row - 1)
953 swap_rows(tab, row, tab->n_row - 1);
954 isl_tab_var_from_row(tab, tab->n_row - 1)->index = -1;
955 tab->n_row--;
956 return 1;
960 int isl_tab_mark_empty(struct isl_tab *tab)
962 if (!tab)
963 return -1;
964 if (!tab->empty && tab->need_undo)
965 if (isl_tab_push(tab, isl_tab_undo_empty) < 0)
966 return -1;
967 tab->empty = 1;
968 return 0;
971 int isl_tab_freeze_constraint(struct isl_tab *tab, int con)
973 struct isl_tab_var *var;
975 if (!tab)
976 return -1;
978 var = &tab->con[con];
979 if (var->frozen)
980 return 0;
981 if (var->index < 0)
982 return 0;
983 var->frozen = 1;
985 if (tab->need_undo)
986 return isl_tab_push_var(tab, isl_tab_undo_freeze, var);
988 return 0;
991 /* Update the rows signs after a pivot of "row" and "col", with "row_sgn"
992 * the original sign of the pivot element.
993 * We only keep track of row signs during PILP solving and in this case
994 * we only pivot a row with negative sign (meaning the value is always
995 * non-positive) using a positive pivot element.
997 * For each row j, the new value of the parametric constant is equal to
999 * a_j0 - a_jc a_r0/a_rc
1001 * where a_j0 is the original parametric constant, a_rc is the pivot element,
1002 * a_r0 is the parametric constant of the pivot row and a_jc is the
1003 * pivot column entry of the row j.
1004 * Since a_r0 is non-positive and a_rc is positive, the sign of row j
1005 * remains the same if a_jc has the same sign as the row j or if
1006 * a_jc is zero. In all other cases, we reset the sign to "unknown".
1008 static void update_row_sign(struct isl_tab *tab, int row, int col, int row_sgn)
1010 int i;
1011 struct isl_mat *mat = tab->mat;
1012 unsigned off = 2 + tab->M;
1014 if (!tab->row_sign)
1015 return;
1017 if (tab->row_sign[row] == 0)
1018 return;
1019 isl_assert(mat->ctx, row_sgn > 0, return);
1020 isl_assert(mat->ctx, tab->row_sign[row] == isl_tab_row_neg, return);
1021 tab->row_sign[row] = isl_tab_row_pos;
1022 for (i = 0; i < tab->n_row; ++i) {
1023 int s;
1024 if (i == row)
1025 continue;
1026 s = isl_int_sgn(mat->row[i][off + col]);
1027 if (!s)
1028 continue;
1029 if (!tab->row_sign[i])
1030 continue;
1031 if (s < 0 && tab->row_sign[i] == isl_tab_row_neg)
1032 continue;
1033 if (s > 0 && tab->row_sign[i] == isl_tab_row_pos)
1034 continue;
1035 tab->row_sign[i] = isl_tab_row_unknown;
1039 /* Given a row number "row" and a column number "col", pivot the tableau
1040 * such that the associated variables are interchanged.
1041 * The given row in the tableau expresses
1043 * x_r = a_r0 + \sum_i a_ri x_i
1045 * or
1047 * x_c = 1/a_rc x_r - a_r0/a_rc + sum_{i \ne r} -a_ri/a_rc
1049 * Substituting this equality into the other rows
1051 * x_j = a_j0 + \sum_i a_ji x_i
1053 * with a_jc \ne 0, we obtain
1055 * 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
1057 * The tableau
1059 * n_rc/d_r n_ri/d_r
1060 * n_jc/d_j n_ji/d_j
1062 * where i is any other column and j is any other row,
1063 * is therefore transformed into
1065 * s(n_rc)d_r/|n_rc| -s(n_rc)n_ri/|n_rc|
1066 * 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)
1068 * The transformation is performed along the following steps
1070 * d_r/n_rc n_ri/n_rc
1071 * n_jc/d_j n_ji/d_j
1073 * s(n_rc)d_r/|n_rc| -s(n_rc)n_ri/|n_rc|
1074 * n_jc/d_j n_ji/d_j
1076 * s(n_rc)d_r/|n_rc| -s(n_rc)n_ri/|n_rc|
1077 * n_jc/(|n_rc| d_j) n_ji/(|n_rc| d_j)
1079 * s(n_rc)d_r/|n_rc| -s(n_rc)n_ri/|n_rc|
1080 * n_jc/(|n_rc| d_j) (n_ji |n_rc|)/(|n_rc| d_j)
1082 * s(n_rc)d_r/|n_rc| -s(n_rc)n_ri/|n_rc|
1083 * n_jc/(|n_rc| d_j) (n_ji |n_rc| - s(n_rc)n_jc n_ri)/(|n_rc| d_j)
1085 * s(n_rc)d_r/|n_rc| -s(n_rc)n_ri/|n_rc|
1086 * 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)
1089 int isl_tab_pivot(struct isl_tab *tab, int row, int col)
1091 int i, j;
1092 int sgn;
1093 int t;
1094 isl_ctx *ctx;
1095 struct isl_mat *mat = tab->mat;
1096 struct isl_tab_var *var;
1097 unsigned off = 2 + tab->M;
1099 ctx = isl_tab_get_ctx(tab);
1100 if (isl_ctx_next_operation(ctx) < 0)
1101 return -1;
1103 isl_int_swap(mat->row[row][0], mat->row[row][off + col]);
1104 sgn = isl_int_sgn(mat->row[row][0]);
1105 if (sgn < 0) {
1106 isl_int_neg(mat->row[row][0], mat->row[row][0]);
1107 isl_int_neg(mat->row[row][off + col], mat->row[row][off + col]);
1108 } else
1109 for (j = 0; j < off - 1 + tab->n_col; ++j) {
1110 if (j == off - 1 + col)
1111 continue;
1112 isl_int_neg(mat->row[row][1 + j], mat->row[row][1 + j]);
1114 if (!isl_int_is_one(mat->row[row][0]))
1115 isl_seq_normalize(mat->ctx, mat->row[row], off + tab->n_col);
1116 for (i = 0; i < tab->n_row; ++i) {
1117 if (i == row)
1118 continue;
1119 if (isl_int_is_zero(mat->row[i][off + col]))
1120 continue;
1121 isl_int_mul(mat->row[i][0], mat->row[i][0], mat->row[row][0]);
1122 for (j = 0; j < off - 1 + tab->n_col; ++j) {
1123 if (j == off - 1 + col)
1124 continue;
1125 isl_int_mul(mat->row[i][1 + j],
1126 mat->row[i][1 + j], mat->row[row][0]);
1127 isl_int_addmul(mat->row[i][1 + j],
1128 mat->row[i][off + col], mat->row[row][1 + j]);
1130 isl_int_mul(mat->row[i][off + col],
1131 mat->row[i][off + col], mat->row[row][off + col]);
1132 if (!isl_int_is_one(mat->row[i][0]))
1133 isl_seq_normalize(mat->ctx, mat->row[i], off + tab->n_col);
1135 t = tab->row_var[row];
1136 tab->row_var[row] = tab->col_var[col];
1137 tab->col_var[col] = t;
1138 var = isl_tab_var_from_row(tab, row);
1139 var->is_row = 1;
1140 var->index = row;
1141 var = var_from_col(tab, col);
1142 var->is_row = 0;
1143 var->index = col;
1144 update_row_sign(tab, row, col, sgn);
1145 if (tab->in_undo)
1146 return 0;
1147 for (i = tab->n_redundant; i < tab->n_row; ++i) {
1148 if (isl_int_is_zero(mat->row[i][off + col]))
1149 continue;
1150 if (!isl_tab_var_from_row(tab, i)->frozen &&
1151 isl_tab_row_is_redundant(tab, i)) {
1152 int redo = isl_tab_mark_redundant(tab, i);
1153 if (redo < 0)
1154 return -1;
1155 if (redo)
1156 --i;
1159 return 0;
1162 /* If "var" represents a column variable, then pivot is up (sgn > 0)
1163 * or down (sgn < 0) to a row. The variable is assumed not to be
1164 * unbounded in the specified direction.
1165 * If sgn = 0, then the variable is unbounded in both directions,
1166 * and we pivot with any row we can find.
1168 static int to_row(struct isl_tab *tab, struct isl_tab_var *var, int sign) WARN_UNUSED;
1169 static int to_row(struct isl_tab *tab, struct isl_tab_var *var, int sign)
1171 int r;
1172 unsigned off = 2 + tab->M;
1174 if (var->is_row)
1175 return 0;
1177 if (sign == 0) {
1178 for (r = tab->n_redundant; r < tab->n_row; ++r)
1179 if (!isl_int_is_zero(tab->mat->row[r][off+var->index]))
1180 break;
1181 isl_assert(tab->mat->ctx, r < tab->n_row, return -1);
1182 } else {
1183 r = pivot_row(tab, NULL, sign, var->index);
1184 isl_assert(tab->mat->ctx, r >= 0, return -1);
1187 return isl_tab_pivot(tab, r, var->index);
1190 /* Check whether all variables that are marked as non-negative
1191 * also have a non-negative sample value. This function is not
1192 * called from the current code but is useful during debugging.
1194 static void check_table(struct isl_tab *tab) __attribute__ ((unused));
1195 static void check_table(struct isl_tab *tab)
1197 int i;
1199 if (tab->empty)
1200 return;
1201 for (i = tab->n_redundant; i < tab->n_row; ++i) {
1202 struct isl_tab_var *var;
1203 var = isl_tab_var_from_row(tab, i);
1204 if (!var->is_nonneg)
1205 continue;
1206 if (tab->M) {
1207 isl_assert(tab->mat->ctx,
1208 !isl_int_is_neg(tab->mat->row[i][2]), abort());
1209 if (isl_int_is_pos(tab->mat->row[i][2]))
1210 continue;
1212 isl_assert(tab->mat->ctx, !isl_int_is_neg(tab->mat->row[i][1]),
1213 abort());
1217 /* Return the sign of the maximal value of "var".
1218 * If the sign is not negative, then on return from this function,
1219 * the sample value will also be non-negative.
1221 * If "var" is manifestly unbounded wrt positive values, we are done.
1222 * Otherwise, we pivot the variable up to a row if needed
1223 * Then we continue pivoting down until either
1224 * - no more down pivots can be performed
1225 * - the sample value is positive
1226 * - the variable is pivoted into a manifestly unbounded column
1228 static int sign_of_max(struct isl_tab *tab, struct isl_tab_var *var)
1230 int row, col;
1232 if (max_is_manifestly_unbounded(tab, var))
1233 return 1;
1234 if (to_row(tab, var, 1) < 0)
1235 return -2;
1236 while (!isl_int_is_pos(tab->mat->row[var->index][1])) {
1237 find_pivot(tab, var, var, 1, &row, &col);
1238 if (row == -1)
1239 return isl_int_sgn(tab->mat->row[var->index][1]);
1240 if (isl_tab_pivot(tab, row, col) < 0)
1241 return -2;
1242 if (!var->is_row) /* manifestly unbounded */
1243 return 1;
1245 return 1;
1248 int isl_tab_sign_of_max(struct isl_tab *tab, int con)
1250 struct isl_tab_var *var;
1252 if (!tab)
1253 return -2;
1255 var = &tab->con[con];
1256 isl_assert(tab->mat->ctx, !var->is_redundant, return -2);
1257 isl_assert(tab->mat->ctx, !var->is_zero, return -2);
1259 return sign_of_max(tab, var);
1262 static int row_is_neg(struct isl_tab *tab, int row)
1264 if (!tab->M)
1265 return isl_int_is_neg(tab->mat->row[row][1]);
1266 if (isl_int_is_pos(tab->mat->row[row][2]))
1267 return 0;
1268 if (isl_int_is_neg(tab->mat->row[row][2]))
1269 return 1;
1270 return isl_int_is_neg(tab->mat->row[row][1]);
1273 static int row_sgn(struct isl_tab *tab, int row)
1275 if (!tab->M)
1276 return isl_int_sgn(tab->mat->row[row][1]);
1277 if (!isl_int_is_zero(tab->mat->row[row][2]))
1278 return isl_int_sgn(tab->mat->row[row][2]);
1279 else
1280 return isl_int_sgn(tab->mat->row[row][1]);
1283 /* Perform pivots until the row variable "var" has a non-negative
1284 * sample value or until no more upward pivots can be performed.
1285 * Return the sign of the sample value after the pivots have been
1286 * performed.
1288 static int restore_row(struct isl_tab *tab, struct isl_tab_var *var)
1290 int row, col;
1292 while (row_is_neg(tab, var->index)) {
1293 find_pivot(tab, var, var, 1, &row, &col);
1294 if (row == -1)
1295 break;
1296 if (isl_tab_pivot(tab, row, col) < 0)
1297 return -2;
1298 if (!var->is_row) /* manifestly unbounded */
1299 return 1;
1301 return row_sgn(tab, var->index);
1304 /* Perform pivots until we are sure that the row variable "var"
1305 * can attain non-negative values. After return from this
1306 * function, "var" is still a row variable, but its sample
1307 * value may not be non-negative, even if the function returns 1.
1309 static int at_least_zero(struct isl_tab *tab, struct isl_tab_var *var)
1311 int row, col;
1313 while (isl_int_is_neg(tab->mat->row[var->index][1])) {
1314 find_pivot(tab, var, var, 1, &row, &col);
1315 if (row == -1)
1316 break;
1317 if (row == var->index) /* manifestly unbounded */
1318 return 1;
1319 if (isl_tab_pivot(tab, row, col) < 0)
1320 return -1;
1322 return !isl_int_is_neg(tab->mat->row[var->index][1]);
1325 /* Return a negative value if "var" can attain negative values.
1326 * Return a non-negative value otherwise.
1328 * If "var" is manifestly unbounded wrt negative values, we are done.
1329 * Otherwise, if var is in a column, we can pivot it down to a row.
1330 * Then we continue pivoting down until either
1331 * - the pivot would result in a manifestly unbounded column
1332 * => we don't perform the pivot, but simply return -1
1333 * - no more down pivots can be performed
1334 * - the sample value is negative
1335 * If the sample value becomes negative and the variable is supposed
1336 * to be nonnegative, then we undo the last pivot.
1337 * However, if the last pivot has made the pivoting variable
1338 * obviously redundant, then it may have moved to another row.
1339 * In that case we look for upward pivots until we reach a non-negative
1340 * value again.
1342 static int sign_of_min(struct isl_tab *tab, struct isl_tab_var *var)
1344 int row, col;
1345 struct isl_tab_var *pivot_var = NULL;
1347 if (min_is_manifestly_unbounded(tab, var))
1348 return -1;
1349 if (!var->is_row) {
1350 col = var->index;
1351 row = pivot_row(tab, NULL, -1, col);
1352 pivot_var = var_from_col(tab, col);
1353 if (isl_tab_pivot(tab, row, col) < 0)
1354 return -2;
1355 if (var->is_redundant)
1356 return 0;
1357 if (isl_int_is_neg(tab->mat->row[var->index][1])) {
1358 if (var->is_nonneg) {
1359 if (!pivot_var->is_redundant &&
1360 pivot_var->index == row) {
1361 if (isl_tab_pivot(tab, row, col) < 0)
1362 return -2;
1363 } else
1364 if (restore_row(tab, var) < -1)
1365 return -2;
1367 return -1;
1370 if (var->is_redundant)
1371 return 0;
1372 while (!isl_int_is_neg(tab->mat->row[var->index][1])) {
1373 find_pivot(tab, var, var, -1, &row, &col);
1374 if (row == var->index)
1375 return -1;
1376 if (row == -1)
1377 return isl_int_sgn(tab->mat->row[var->index][1]);
1378 pivot_var = var_from_col(tab, col);
1379 if (isl_tab_pivot(tab, row, col) < 0)
1380 return -2;
1381 if (var->is_redundant)
1382 return 0;
1384 if (pivot_var && var->is_nonneg) {
1385 /* pivot back to non-negative value */
1386 if (!pivot_var->is_redundant && pivot_var->index == row) {
1387 if (isl_tab_pivot(tab, row, col) < 0)
1388 return -2;
1389 } else
1390 if (restore_row(tab, var) < -1)
1391 return -2;
1393 return -1;
1396 static int row_at_most_neg_one(struct isl_tab *tab, int row)
1398 if (tab->M) {
1399 if (isl_int_is_pos(tab->mat->row[row][2]))
1400 return 0;
1401 if (isl_int_is_neg(tab->mat->row[row][2]))
1402 return 1;
1404 return isl_int_is_neg(tab->mat->row[row][1]) &&
1405 isl_int_abs_ge(tab->mat->row[row][1],
1406 tab->mat->row[row][0]);
1409 /* Return 1 if "var" can attain values <= -1.
1410 * Return 0 otherwise.
1412 * If the variable "var" is supposed to be non-negative (is_nonneg is set),
1413 * then the sample value of "var" is assumed to be non-negative when the
1414 * the function is called. If 1 is returned then the constraint
1415 * is not redundant and the sample value is made non-negative again before
1416 * the function returns.
1418 int isl_tab_min_at_most_neg_one(struct isl_tab *tab, struct isl_tab_var *var)
1420 int row, col;
1421 struct isl_tab_var *pivot_var;
1423 if (min_is_manifestly_unbounded(tab, var))
1424 return 1;
1425 if (!var->is_row) {
1426 col = var->index;
1427 row = pivot_row(tab, NULL, -1, col);
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 if (row_at_most_neg_one(tab, var->index)) {
1434 if (var->is_nonneg) {
1435 if (!pivot_var->is_redundant &&
1436 pivot_var->index == row) {
1437 if (isl_tab_pivot(tab, row, col) < 0)
1438 return -1;
1439 } else
1440 if (restore_row(tab, var) < -1)
1441 return -1;
1443 return 1;
1446 if (var->is_redundant)
1447 return 0;
1448 do {
1449 find_pivot(tab, var, var, -1, &row, &col);
1450 if (row == var->index) {
1451 if (var->is_nonneg && restore_row(tab, var) < -1)
1452 return -1;
1453 return 1;
1455 if (row == -1)
1456 return 0;
1457 pivot_var = var_from_col(tab, col);
1458 if (isl_tab_pivot(tab, row, col) < 0)
1459 return -1;
1460 if (var->is_redundant)
1461 return 0;
1462 } while (!row_at_most_neg_one(tab, var->index));
1463 if (var->is_nonneg) {
1464 /* pivot back to non-negative value */
1465 if (!pivot_var->is_redundant && pivot_var->index == row)
1466 if (isl_tab_pivot(tab, row, col) < 0)
1467 return -1;
1468 if (restore_row(tab, var) < -1)
1469 return -1;
1471 return 1;
1474 /* Return 1 if "var" can attain values >= 1.
1475 * Return 0 otherwise.
1477 static int at_least_one(struct isl_tab *tab, struct isl_tab_var *var)
1479 int row, col;
1480 isl_int *r;
1482 if (max_is_manifestly_unbounded(tab, var))
1483 return 1;
1484 if (to_row(tab, var, 1) < 0)
1485 return -1;
1486 r = tab->mat->row[var->index];
1487 while (isl_int_lt(r[1], r[0])) {
1488 find_pivot(tab, var, var, 1, &row, &col);
1489 if (row == -1)
1490 return isl_int_ge(r[1], r[0]);
1491 if (row == var->index) /* manifestly unbounded */
1492 return 1;
1493 if (isl_tab_pivot(tab, row, col) < 0)
1494 return -1;
1496 return 1;
1499 static void swap_cols(struct isl_tab *tab, int col1, int col2)
1501 int t;
1502 unsigned off = 2 + tab->M;
1503 t = tab->col_var[col1];
1504 tab->col_var[col1] = tab->col_var[col2];
1505 tab->col_var[col2] = t;
1506 var_from_col(tab, col1)->index = col1;
1507 var_from_col(tab, col2)->index = col2;
1508 tab->mat = isl_mat_swap_cols(tab->mat, off + col1, off + col2);
1511 /* Mark column with index "col" as representing a zero variable.
1512 * If we may need to undo the operation the column is kept,
1513 * but no longer considered.
1514 * Otherwise, the column is simply removed.
1516 * The column may be interchanged with some other column. If it
1517 * is interchanged with a later column, return 1. Otherwise return 0.
1518 * If the columns are checked in order in the calling function,
1519 * then a return value of 1 means that the column with the given
1520 * column number may now contain a different column that
1521 * hasn't been checked yet.
1523 int isl_tab_kill_col(struct isl_tab *tab, int col)
1525 var_from_col(tab, col)->is_zero = 1;
1526 if (tab->need_undo) {
1527 if (isl_tab_push_var(tab, isl_tab_undo_zero,
1528 var_from_col(tab, col)) < 0)
1529 return -1;
1530 if (col != tab->n_dead)
1531 swap_cols(tab, col, tab->n_dead);
1532 tab->n_dead++;
1533 return 0;
1534 } else {
1535 if (col != tab->n_col - 1)
1536 swap_cols(tab, col, tab->n_col - 1);
1537 var_from_col(tab, tab->n_col - 1)->index = -1;
1538 tab->n_col--;
1539 return 1;
1543 static int row_is_manifestly_non_integral(struct isl_tab *tab, int row)
1545 unsigned off = 2 + tab->M;
1547 if (tab->M && !isl_int_eq(tab->mat->row[row][2],
1548 tab->mat->row[row][0]))
1549 return 0;
1550 if (isl_seq_first_non_zero(tab->mat->row[row] + off + tab->n_dead,
1551 tab->n_col - tab->n_dead) != -1)
1552 return 0;
1554 return !isl_int_is_divisible_by(tab->mat->row[row][1],
1555 tab->mat->row[row][0]);
1558 /* For integer tableaus, check if any of the coordinates are stuck
1559 * at a non-integral value.
1561 static int tab_is_manifestly_empty(struct isl_tab *tab)
1563 int i;
1565 if (tab->empty)
1566 return 1;
1567 if (tab->rational)
1568 return 0;
1570 for (i = 0; i < tab->n_var; ++i) {
1571 if (!tab->var[i].is_row)
1572 continue;
1573 if (row_is_manifestly_non_integral(tab, tab->var[i].index))
1574 return 1;
1577 return 0;
1580 /* Row variable "var" is non-negative and cannot attain any values
1581 * larger than zero. This means that the coefficients of the unrestricted
1582 * column variables are zero and that the coefficients of the non-negative
1583 * column variables are zero or negative.
1584 * Each of the non-negative variables with a negative coefficient can
1585 * then also be written as the negative sum of non-negative variables
1586 * and must therefore also be zero.
1588 static int close_row(struct isl_tab *tab, struct isl_tab_var *var) WARN_UNUSED;
1589 static int close_row(struct isl_tab *tab, struct isl_tab_var *var)
1591 int j;
1592 struct isl_mat *mat = tab->mat;
1593 unsigned off = 2 + tab->M;
1595 isl_assert(tab->mat->ctx, var->is_nonneg, return -1);
1596 var->is_zero = 1;
1597 if (tab->need_undo)
1598 if (isl_tab_push_var(tab, isl_tab_undo_zero, var) < 0)
1599 return -1;
1600 for (j = tab->n_dead; j < tab->n_col; ++j) {
1601 int recheck;
1602 if (isl_int_is_zero(mat->row[var->index][off + j]))
1603 continue;
1604 isl_assert(tab->mat->ctx,
1605 isl_int_is_neg(mat->row[var->index][off + j]), return -1);
1606 recheck = isl_tab_kill_col(tab, j);
1607 if (recheck < 0)
1608 return -1;
1609 if (recheck)
1610 --j;
1612 if (isl_tab_mark_redundant(tab, var->index) < 0)
1613 return -1;
1614 if (tab_is_manifestly_empty(tab) && isl_tab_mark_empty(tab) < 0)
1615 return -1;
1616 return 0;
1619 /* Add a constraint to the tableau and allocate a row for it.
1620 * Return the index into the constraint array "con".
1622 int isl_tab_allocate_con(struct isl_tab *tab)
1624 int r;
1626 isl_assert(tab->mat->ctx, tab->n_row < tab->mat->n_row, return -1);
1627 isl_assert(tab->mat->ctx, tab->n_con < tab->max_con, return -1);
1629 r = tab->n_con;
1630 tab->con[r].index = tab->n_row;
1631 tab->con[r].is_row = 1;
1632 tab->con[r].is_nonneg = 0;
1633 tab->con[r].is_zero = 0;
1634 tab->con[r].is_redundant = 0;
1635 tab->con[r].frozen = 0;
1636 tab->con[r].negated = 0;
1637 tab->row_var[tab->n_row] = ~r;
1639 tab->n_row++;
1640 tab->n_con++;
1641 if (isl_tab_push_var(tab, isl_tab_undo_allocate, &tab->con[r]) < 0)
1642 return -1;
1644 return r;
1647 /* Add a variable to the tableau and allocate a column for it.
1648 * Return the index into the variable array "var".
1650 int isl_tab_allocate_var(struct isl_tab *tab)
1652 int r;
1653 int i;
1654 unsigned off = 2 + tab->M;
1656 isl_assert(tab->mat->ctx, tab->n_col < tab->mat->n_col, return -1);
1657 isl_assert(tab->mat->ctx, tab->n_var < tab->max_var, return -1);
1659 r = tab->n_var;
1660 tab->var[r].index = tab->n_col;
1661 tab->var[r].is_row = 0;
1662 tab->var[r].is_nonneg = 0;
1663 tab->var[r].is_zero = 0;
1664 tab->var[r].is_redundant = 0;
1665 tab->var[r].frozen = 0;
1666 tab->var[r].negated = 0;
1667 tab->col_var[tab->n_col] = r;
1669 for (i = 0; i < tab->n_row; ++i)
1670 isl_int_set_si(tab->mat->row[i][off + tab->n_col], 0);
1672 tab->n_var++;
1673 tab->n_col++;
1674 if (isl_tab_push_var(tab, isl_tab_undo_allocate, &tab->var[r]) < 0)
1675 return -1;
1677 return r;
1680 /* Add a row to the tableau. The row is given as an affine combination
1681 * of the original variables and needs to be expressed in terms of the
1682 * column variables.
1684 * We add each term in turn.
1685 * If r = n/d_r is the current sum and we need to add k x, then
1686 * if x is a column variable, we increase the numerator of
1687 * this column by k d_r
1688 * if x = f/d_x is a row variable, then the new representation of r is
1690 * n k f d_x/g n + d_r/g k f m/d_r n + m/d_g k f
1691 * --- + --- = ------------------- = -------------------
1692 * d_r d_r d_r d_x/g m
1694 * with g the gcd of d_r and d_x and m the lcm of d_r and d_x.
1696 * If tab->M is set, then, internally, each variable x is represented
1697 * as x' - M. We then also need no subtract k d_r from the coefficient of M.
1699 int isl_tab_add_row(struct isl_tab *tab, isl_int *line)
1701 int i;
1702 int r;
1703 isl_int *row;
1704 isl_int a, b;
1705 unsigned off = 2 + tab->M;
1707 r = isl_tab_allocate_con(tab);
1708 if (r < 0)
1709 return -1;
1711 isl_int_init(a);
1712 isl_int_init(b);
1713 row = tab->mat->row[tab->con[r].index];
1714 isl_int_set_si(row[0], 1);
1715 isl_int_set(row[1], line[0]);
1716 isl_seq_clr(row + 2, tab->M + tab->n_col);
1717 for (i = 0; i < tab->n_var; ++i) {
1718 if (tab->var[i].is_zero)
1719 continue;
1720 if (tab->var[i].is_row) {
1721 isl_int_lcm(a,
1722 row[0], tab->mat->row[tab->var[i].index][0]);
1723 isl_int_swap(a, row[0]);
1724 isl_int_divexact(a, row[0], a);
1725 isl_int_divexact(b,
1726 row[0], tab->mat->row[tab->var[i].index][0]);
1727 isl_int_mul(b, b, line[1 + i]);
1728 isl_seq_combine(row + 1, a, row + 1,
1729 b, tab->mat->row[tab->var[i].index] + 1,
1730 1 + tab->M + tab->n_col);
1731 } else
1732 isl_int_addmul(row[off + tab->var[i].index],
1733 line[1 + i], row[0]);
1734 if (tab->M && i >= tab->n_param && i < tab->n_var - tab->n_div)
1735 isl_int_submul(row[2], line[1 + i], row[0]);
1737 isl_seq_normalize(tab->mat->ctx, row, off + tab->n_col);
1738 isl_int_clear(a);
1739 isl_int_clear(b);
1741 if (tab->row_sign)
1742 tab->row_sign[tab->con[r].index] = isl_tab_row_unknown;
1744 return r;
1747 static int drop_row(struct isl_tab *tab, int row)
1749 isl_assert(tab->mat->ctx, ~tab->row_var[row] == tab->n_con - 1, return -1);
1750 if (row != tab->n_row - 1)
1751 swap_rows(tab, row, tab->n_row - 1);
1752 tab->n_row--;
1753 tab->n_con--;
1754 return 0;
1757 static int drop_col(struct isl_tab *tab, int col)
1759 isl_assert(tab->mat->ctx, tab->col_var[col] == tab->n_var - 1, return -1);
1760 if (col != tab->n_col - 1)
1761 swap_cols(tab, col, tab->n_col - 1);
1762 tab->n_col--;
1763 tab->n_var--;
1764 return 0;
1767 /* Add inequality "ineq" and check if it conflicts with the
1768 * previously added constraints or if it is obviously redundant.
1770 int isl_tab_add_ineq(struct isl_tab *tab, isl_int *ineq)
1772 int r;
1773 int sgn;
1774 isl_int cst;
1776 if (!tab)
1777 return -1;
1778 if (tab->bmap) {
1779 struct isl_basic_map *bmap = tab->bmap;
1781 isl_assert(tab->mat->ctx, tab->n_eq == bmap->n_eq, return -1);
1782 isl_assert(tab->mat->ctx,
1783 tab->n_con == bmap->n_eq + bmap->n_ineq, return -1);
1784 tab->bmap = isl_basic_map_add_ineq(tab->bmap, ineq);
1785 if (isl_tab_push(tab, isl_tab_undo_bmap_ineq) < 0)
1786 return -1;
1787 if (!tab->bmap)
1788 return -1;
1790 if (tab->cone) {
1791 isl_int_init(cst);
1792 isl_int_swap(ineq[0], cst);
1794 r = isl_tab_add_row(tab, ineq);
1795 if (tab->cone) {
1796 isl_int_swap(ineq[0], cst);
1797 isl_int_clear(cst);
1799 if (r < 0)
1800 return -1;
1801 tab->con[r].is_nonneg = 1;
1802 if (isl_tab_push_var(tab, isl_tab_undo_nonneg, &tab->con[r]) < 0)
1803 return -1;
1804 if (isl_tab_row_is_redundant(tab, tab->con[r].index)) {
1805 if (isl_tab_mark_redundant(tab, tab->con[r].index) < 0)
1806 return -1;
1807 return 0;
1810 sgn = restore_row(tab, &tab->con[r]);
1811 if (sgn < -1)
1812 return -1;
1813 if (sgn < 0)
1814 return isl_tab_mark_empty(tab);
1815 if (tab->con[r].is_row && isl_tab_row_is_redundant(tab, tab->con[r].index))
1816 if (isl_tab_mark_redundant(tab, tab->con[r].index) < 0)
1817 return -1;
1818 return 0;
1821 /* Pivot a non-negative variable down until it reaches the value zero
1822 * and then pivot the variable into a column position.
1824 static int to_col(struct isl_tab *tab, struct isl_tab_var *var) WARN_UNUSED;
1825 static int to_col(struct isl_tab *tab, struct isl_tab_var *var)
1827 int i;
1828 int row, col;
1829 unsigned off = 2 + tab->M;
1831 if (!var->is_row)
1832 return 0;
1834 while (isl_int_is_pos(tab->mat->row[var->index][1])) {
1835 find_pivot(tab, var, NULL, -1, &row, &col);
1836 isl_assert(tab->mat->ctx, row != -1, return -1);
1837 if (isl_tab_pivot(tab, row, col) < 0)
1838 return -1;
1839 if (!var->is_row)
1840 return 0;
1843 for (i = tab->n_dead; i < tab->n_col; ++i)
1844 if (!isl_int_is_zero(tab->mat->row[var->index][off + i]))
1845 break;
1847 isl_assert(tab->mat->ctx, i < tab->n_col, return -1);
1848 if (isl_tab_pivot(tab, var->index, i) < 0)
1849 return -1;
1851 return 0;
1854 /* We assume Gaussian elimination has been performed on the equalities.
1855 * The equalities can therefore never conflict.
1856 * Adding the equalities is currently only really useful for a later call
1857 * to isl_tab_ineq_type.
1859 static struct isl_tab *add_eq(struct isl_tab *tab, isl_int *eq)
1861 int i;
1862 int r;
1864 if (!tab)
1865 return NULL;
1866 r = isl_tab_add_row(tab, eq);
1867 if (r < 0)
1868 goto error;
1870 r = tab->con[r].index;
1871 i = isl_seq_first_non_zero(tab->mat->row[r] + 2 + tab->M + tab->n_dead,
1872 tab->n_col - tab->n_dead);
1873 isl_assert(tab->mat->ctx, i >= 0, goto error);
1874 i += tab->n_dead;
1875 if (isl_tab_pivot(tab, r, i) < 0)
1876 goto error;
1877 if (isl_tab_kill_col(tab, i) < 0)
1878 goto error;
1879 tab->n_eq++;
1881 return tab;
1882 error:
1883 isl_tab_free(tab);
1884 return NULL;
1887 static int row_is_manifestly_zero(struct isl_tab *tab, int row)
1889 unsigned off = 2 + tab->M;
1891 if (!isl_int_is_zero(tab->mat->row[row][1]))
1892 return 0;
1893 if (tab->M && !isl_int_is_zero(tab->mat->row[row][2]))
1894 return 0;
1895 return isl_seq_first_non_zero(tab->mat->row[row] + off + tab->n_dead,
1896 tab->n_col - tab->n_dead) == -1;
1899 /* Add an equality that is known to be valid for the given tableau.
1901 int isl_tab_add_valid_eq(struct isl_tab *tab, isl_int *eq)
1903 struct isl_tab_var *var;
1904 int r;
1906 if (!tab)
1907 return -1;
1908 r = isl_tab_add_row(tab, eq);
1909 if (r < 0)
1910 return -1;
1912 var = &tab->con[r];
1913 r = var->index;
1914 if (row_is_manifestly_zero(tab, r)) {
1915 var->is_zero = 1;
1916 if (isl_tab_mark_redundant(tab, r) < 0)
1917 return -1;
1918 return 0;
1921 if (isl_int_is_neg(tab->mat->row[r][1])) {
1922 isl_seq_neg(tab->mat->row[r] + 1, tab->mat->row[r] + 1,
1923 1 + tab->n_col);
1924 var->negated = 1;
1926 var->is_nonneg = 1;
1927 if (to_col(tab, var) < 0)
1928 return -1;
1929 var->is_nonneg = 0;
1930 if (isl_tab_kill_col(tab, var->index) < 0)
1931 return -1;
1933 return 0;
1936 static int add_zero_row(struct isl_tab *tab)
1938 int r;
1939 isl_int *row;
1941 r = isl_tab_allocate_con(tab);
1942 if (r < 0)
1943 return -1;
1945 row = tab->mat->row[tab->con[r].index];
1946 isl_seq_clr(row + 1, 1 + tab->M + tab->n_col);
1947 isl_int_set_si(row[0], 1);
1949 return r;
1952 /* Add equality "eq" and check if it conflicts with the
1953 * previously added constraints or if it is obviously redundant.
1955 int isl_tab_add_eq(struct isl_tab *tab, isl_int *eq)
1957 struct isl_tab_undo *snap = NULL;
1958 struct isl_tab_var *var;
1959 int r;
1960 int row;
1961 int sgn;
1962 isl_int cst;
1964 if (!tab)
1965 return -1;
1966 isl_assert(tab->mat->ctx, !tab->M, return -1);
1968 if (tab->need_undo)
1969 snap = isl_tab_snap(tab);
1971 if (tab->cone) {
1972 isl_int_init(cst);
1973 isl_int_swap(eq[0], cst);
1975 r = isl_tab_add_row(tab, eq);
1976 if (tab->cone) {
1977 isl_int_swap(eq[0], cst);
1978 isl_int_clear(cst);
1980 if (r < 0)
1981 return -1;
1983 var = &tab->con[r];
1984 row = var->index;
1985 if (row_is_manifestly_zero(tab, row)) {
1986 if (snap) {
1987 if (isl_tab_rollback(tab, snap) < 0)
1988 return -1;
1989 } else
1990 drop_row(tab, row);
1991 return 0;
1994 if (tab->bmap) {
1995 tab->bmap = isl_basic_map_add_ineq(tab->bmap, eq);
1996 if (isl_tab_push(tab, isl_tab_undo_bmap_ineq) < 0)
1997 return -1;
1998 isl_seq_neg(eq, eq, 1 + tab->n_var);
1999 tab->bmap = isl_basic_map_add_ineq(tab->bmap, eq);
2000 isl_seq_neg(eq, eq, 1 + tab->n_var);
2001 if (isl_tab_push(tab, isl_tab_undo_bmap_ineq) < 0)
2002 return -1;
2003 if (!tab->bmap)
2004 return -1;
2005 if (add_zero_row(tab) < 0)
2006 return -1;
2009 sgn = isl_int_sgn(tab->mat->row[row][1]);
2011 if (sgn > 0) {
2012 isl_seq_neg(tab->mat->row[row] + 1, tab->mat->row[row] + 1,
2013 1 + tab->n_col);
2014 var->negated = 1;
2015 sgn = -1;
2018 if (sgn < 0) {
2019 sgn = sign_of_max(tab, var);
2020 if (sgn < -1)
2021 return -1;
2022 if (sgn < 0) {
2023 if (isl_tab_mark_empty(tab) < 0)
2024 return -1;
2025 return 0;
2029 var->is_nonneg = 1;
2030 if (to_col(tab, var) < 0)
2031 return -1;
2032 var->is_nonneg = 0;
2033 if (isl_tab_kill_col(tab, var->index) < 0)
2034 return -1;
2036 return 0;
2039 /* Construct and return an inequality that expresses an upper bound
2040 * on the given div.
2041 * In particular, if the div is given by
2043 * d = floor(e/m)
2045 * then the inequality expresses
2047 * m d <= e
2049 static struct isl_vec *ineq_for_div(struct isl_basic_map *bmap, unsigned div)
2051 unsigned total;
2052 unsigned div_pos;
2053 struct isl_vec *ineq;
2055 if (!bmap)
2056 return NULL;
2058 total = isl_basic_map_total_dim(bmap);
2059 div_pos = 1 + total - bmap->n_div + div;
2061 ineq = isl_vec_alloc(bmap->ctx, 1 + total);
2062 if (!ineq)
2063 return NULL;
2065 isl_seq_cpy(ineq->el, bmap->div[div] + 1, 1 + total);
2066 isl_int_neg(ineq->el[div_pos], bmap->div[div][0]);
2067 return ineq;
2070 /* For a div d = floor(f/m), add the constraints
2072 * f - m d >= 0
2073 * -(f-(m-1)) + m d >= 0
2075 * Note that the second constraint is the negation of
2077 * f - m d >= m
2079 * If add_ineq is not NULL, then this function is used
2080 * instead of isl_tab_add_ineq to effectively add the inequalities.
2082 static int add_div_constraints(struct isl_tab *tab, unsigned div,
2083 int (*add_ineq)(void *user, isl_int *), void *user)
2085 unsigned total;
2086 unsigned div_pos;
2087 struct isl_vec *ineq;
2089 total = isl_basic_map_total_dim(tab->bmap);
2090 div_pos = 1 + total - tab->bmap->n_div + div;
2092 ineq = ineq_for_div(tab->bmap, div);
2093 if (!ineq)
2094 goto error;
2096 if (add_ineq) {
2097 if (add_ineq(user, ineq->el) < 0)
2098 goto error;
2099 } else {
2100 if (isl_tab_add_ineq(tab, ineq->el) < 0)
2101 goto error;
2104 isl_seq_neg(ineq->el, tab->bmap->div[div] + 1, 1 + total);
2105 isl_int_set(ineq->el[div_pos], tab->bmap->div[div][0]);
2106 isl_int_add(ineq->el[0], ineq->el[0], ineq->el[div_pos]);
2107 isl_int_sub_ui(ineq->el[0], ineq->el[0], 1);
2109 if (add_ineq) {
2110 if (add_ineq(user, ineq->el) < 0)
2111 goto error;
2112 } else {
2113 if (isl_tab_add_ineq(tab, ineq->el) < 0)
2114 goto error;
2117 isl_vec_free(ineq);
2119 return 0;
2120 error:
2121 isl_vec_free(ineq);
2122 return -1;
2125 /* Check whether the div described by "div" is obviously non-negative.
2126 * If we are using a big parameter, then we will encode the div
2127 * as div' = M + div, which is always non-negative.
2128 * Otherwise, we check whether div is a non-negative affine combination
2129 * of non-negative variables.
2131 static int div_is_nonneg(struct isl_tab *tab, __isl_keep isl_vec *div)
2133 int i;
2135 if (tab->M)
2136 return 1;
2138 if (isl_int_is_neg(div->el[1]))
2139 return 0;
2141 for (i = 0; i < tab->n_var; ++i) {
2142 if (isl_int_is_neg(div->el[2 + i]))
2143 return 0;
2144 if (isl_int_is_zero(div->el[2 + i]))
2145 continue;
2146 if (!tab->var[i].is_nonneg)
2147 return 0;
2150 return 1;
2153 /* Add an extra div, prescribed by "div" to the tableau and
2154 * the associated bmap (which is assumed to be non-NULL).
2156 * If add_ineq is not NULL, then this function is used instead
2157 * of isl_tab_add_ineq to add the div constraints.
2158 * This complication is needed because the code in isl_tab_pip
2159 * wants to perform some extra processing when an inequality
2160 * is added to the tableau.
2162 int isl_tab_add_div(struct isl_tab *tab, __isl_keep isl_vec *div,
2163 int (*add_ineq)(void *user, isl_int *), void *user)
2165 int r;
2166 int k;
2167 int nonneg;
2169 if (!tab || !div)
2170 return -1;
2172 isl_assert(tab->mat->ctx, tab->bmap, return -1);
2174 nonneg = div_is_nonneg(tab, div);
2176 if (isl_tab_extend_cons(tab, 3) < 0)
2177 return -1;
2178 if (isl_tab_extend_vars(tab, 1) < 0)
2179 return -1;
2180 r = isl_tab_allocate_var(tab);
2181 if (r < 0)
2182 return -1;
2184 if (nonneg)
2185 tab->var[r].is_nonneg = 1;
2187 tab->bmap = isl_basic_map_extend_space(tab->bmap,
2188 isl_basic_map_get_space(tab->bmap), 1, 0, 2);
2189 k = isl_basic_map_alloc_div(tab->bmap);
2190 if (k < 0)
2191 return -1;
2192 isl_seq_cpy(tab->bmap->div[k], div->el, div->size);
2193 if (isl_tab_push(tab, isl_tab_undo_bmap_div) < 0)
2194 return -1;
2196 if (add_div_constraints(tab, k, add_ineq, user) < 0)
2197 return -1;
2199 return r;
2202 /* If "track" is set, then we want to keep track of all constraints in tab
2203 * in its bmap field. This field is initialized from a copy of "bmap",
2204 * so we need to make sure that all constraints in "bmap" also appear
2205 * in the constructed tab.
2207 __isl_give struct isl_tab *isl_tab_from_basic_map(
2208 __isl_keep isl_basic_map *bmap, int track)
2210 int i;
2211 struct isl_tab *tab;
2213 if (!bmap)
2214 return NULL;
2215 tab = isl_tab_alloc(bmap->ctx,
2216 isl_basic_map_total_dim(bmap) + bmap->n_ineq + 1,
2217 isl_basic_map_total_dim(bmap), 0);
2218 if (!tab)
2219 return NULL;
2220 tab->preserve = track;
2221 tab->rational = ISL_F_ISSET(bmap, ISL_BASIC_MAP_RATIONAL);
2222 if (ISL_F_ISSET(bmap, ISL_BASIC_MAP_EMPTY)) {
2223 if (isl_tab_mark_empty(tab) < 0)
2224 goto error;
2225 goto done;
2227 for (i = 0; i < bmap->n_eq; ++i) {
2228 tab = add_eq(tab, bmap->eq[i]);
2229 if (!tab)
2230 return tab;
2232 for (i = 0; i < bmap->n_ineq; ++i) {
2233 if (isl_tab_add_ineq(tab, bmap->ineq[i]) < 0)
2234 goto error;
2235 if (tab->empty)
2236 goto done;
2238 done:
2239 if (track && isl_tab_track_bmap(tab, isl_basic_map_copy(bmap)) < 0)
2240 goto error;
2241 return tab;
2242 error:
2243 isl_tab_free(tab);
2244 return NULL;
2247 __isl_give struct isl_tab *isl_tab_from_basic_set(
2248 __isl_keep isl_basic_set *bset, int track)
2250 return isl_tab_from_basic_map(bset, track);
2253 /* Construct a tableau corresponding to the recession cone of "bset".
2255 struct isl_tab *isl_tab_from_recession_cone(__isl_keep isl_basic_set *bset,
2256 int parametric)
2258 isl_int cst;
2259 int i;
2260 struct isl_tab *tab;
2261 unsigned offset = 0;
2263 if (!bset)
2264 return NULL;
2265 if (parametric)
2266 offset = isl_basic_set_dim(bset, isl_dim_param);
2267 tab = isl_tab_alloc(bset->ctx, bset->n_eq + bset->n_ineq,
2268 isl_basic_set_total_dim(bset) - offset, 0);
2269 if (!tab)
2270 return NULL;
2271 tab->rational = ISL_F_ISSET(bset, ISL_BASIC_SET_RATIONAL);
2272 tab->cone = 1;
2274 isl_int_init(cst);
2275 for (i = 0; i < bset->n_eq; ++i) {
2276 isl_int_swap(bset->eq[i][offset], cst);
2277 if (offset > 0) {
2278 if (isl_tab_add_eq(tab, bset->eq[i] + offset) < 0)
2279 goto error;
2280 } else
2281 tab = add_eq(tab, bset->eq[i]);
2282 isl_int_swap(bset->eq[i][offset], cst);
2283 if (!tab)
2284 goto done;
2286 for (i = 0; i < bset->n_ineq; ++i) {
2287 int r;
2288 isl_int_swap(bset->ineq[i][offset], cst);
2289 r = isl_tab_add_row(tab, bset->ineq[i] + offset);
2290 isl_int_swap(bset->ineq[i][offset], cst);
2291 if (r < 0)
2292 goto error;
2293 tab->con[r].is_nonneg = 1;
2294 if (isl_tab_push_var(tab, isl_tab_undo_nonneg, &tab->con[r]) < 0)
2295 goto error;
2297 done:
2298 isl_int_clear(cst);
2299 return tab;
2300 error:
2301 isl_int_clear(cst);
2302 isl_tab_free(tab);
2303 return NULL;
2306 /* Assuming "tab" is the tableau of a cone, check if the cone is
2307 * bounded, i.e., if it is empty or only contains the origin.
2309 int isl_tab_cone_is_bounded(struct isl_tab *tab)
2311 int i;
2313 if (!tab)
2314 return -1;
2315 if (tab->empty)
2316 return 1;
2317 if (tab->n_dead == tab->n_col)
2318 return 1;
2320 for (;;) {
2321 for (i = tab->n_redundant; i < tab->n_row; ++i) {
2322 struct isl_tab_var *var;
2323 int sgn;
2324 var = isl_tab_var_from_row(tab, i);
2325 if (!var->is_nonneg)
2326 continue;
2327 sgn = sign_of_max(tab, var);
2328 if (sgn < -1)
2329 return -1;
2330 if (sgn != 0)
2331 return 0;
2332 if (close_row(tab, var) < 0)
2333 return -1;
2334 break;
2336 if (tab->n_dead == tab->n_col)
2337 return 1;
2338 if (i == tab->n_row)
2339 return 0;
2343 int isl_tab_sample_is_integer(struct isl_tab *tab)
2345 int i;
2347 if (!tab)
2348 return -1;
2350 for (i = 0; i < tab->n_var; ++i) {
2351 int row;
2352 if (!tab->var[i].is_row)
2353 continue;
2354 row = tab->var[i].index;
2355 if (!isl_int_is_divisible_by(tab->mat->row[row][1],
2356 tab->mat->row[row][0]))
2357 return 0;
2359 return 1;
2362 static struct isl_vec *extract_integer_sample(struct isl_tab *tab)
2364 int i;
2365 struct isl_vec *vec;
2367 vec = isl_vec_alloc(tab->mat->ctx, 1 + tab->n_var);
2368 if (!vec)
2369 return NULL;
2371 isl_int_set_si(vec->block.data[0], 1);
2372 for (i = 0; i < tab->n_var; ++i) {
2373 if (!tab->var[i].is_row)
2374 isl_int_set_si(vec->block.data[1 + i], 0);
2375 else {
2376 int row = tab->var[i].index;
2377 isl_int_divexact(vec->block.data[1 + i],
2378 tab->mat->row[row][1], tab->mat->row[row][0]);
2382 return vec;
2385 struct isl_vec *isl_tab_get_sample_value(struct isl_tab *tab)
2387 int i;
2388 struct isl_vec *vec;
2389 isl_int m;
2391 if (!tab)
2392 return NULL;
2394 vec = isl_vec_alloc(tab->mat->ctx, 1 + tab->n_var);
2395 if (!vec)
2396 return NULL;
2398 isl_int_init(m);
2400 isl_int_set_si(vec->block.data[0], 1);
2401 for (i = 0; i < tab->n_var; ++i) {
2402 int row;
2403 if (!tab->var[i].is_row) {
2404 isl_int_set_si(vec->block.data[1 + i], 0);
2405 continue;
2407 row = tab->var[i].index;
2408 isl_int_gcd(m, vec->block.data[0], tab->mat->row[row][0]);
2409 isl_int_divexact(m, tab->mat->row[row][0], m);
2410 isl_seq_scale(vec->block.data, vec->block.data, m, 1 + i);
2411 isl_int_divexact(m, vec->block.data[0], tab->mat->row[row][0]);
2412 isl_int_mul(vec->block.data[1 + i], m, tab->mat->row[row][1]);
2414 vec = isl_vec_normalize(vec);
2416 isl_int_clear(m);
2417 return vec;
2420 /* Update "bmap" based on the results of the tableau "tab".
2421 * In particular, implicit equalities are made explicit, redundant constraints
2422 * are removed and if the sample value happens to be integer, it is stored
2423 * in "bmap" (unless "bmap" already had an integer sample).
2425 * The tableau is assumed to have been created from "bmap" using
2426 * isl_tab_from_basic_map.
2428 struct isl_basic_map *isl_basic_map_update_from_tab(struct isl_basic_map *bmap,
2429 struct isl_tab *tab)
2431 int i;
2432 unsigned n_eq;
2434 if (!bmap)
2435 return NULL;
2436 if (!tab)
2437 return bmap;
2439 n_eq = tab->n_eq;
2440 if (tab->empty)
2441 bmap = isl_basic_map_set_to_empty(bmap);
2442 else
2443 for (i = bmap->n_ineq - 1; i >= 0; --i) {
2444 if (isl_tab_is_equality(tab, n_eq + i))
2445 isl_basic_map_inequality_to_equality(bmap, i);
2446 else if (isl_tab_is_redundant(tab, n_eq + i))
2447 isl_basic_map_drop_inequality(bmap, i);
2449 if (bmap->n_eq != n_eq)
2450 isl_basic_map_gauss(bmap, NULL);
2451 if (!tab->rational &&
2452 !bmap->sample && isl_tab_sample_is_integer(tab))
2453 bmap->sample = extract_integer_sample(tab);
2454 return bmap;
2457 struct isl_basic_set *isl_basic_set_update_from_tab(struct isl_basic_set *bset,
2458 struct isl_tab *tab)
2460 return (struct isl_basic_set *)isl_basic_map_update_from_tab(
2461 (struct isl_basic_map *)bset, tab);
2464 /* Given a non-negative variable "var", add a new non-negative variable
2465 * that is the opposite of "var", ensuring that var can only attain the
2466 * value zero.
2467 * If var = n/d is a row variable, then the new variable = -n/d.
2468 * If var is a column variables, then the new variable = -var.
2469 * If the new variable cannot attain non-negative values, then
2470 * the resulting tableau is empty.
2471 * Otherwise, we know the value will be zero and we close the row.
2473 static int cut_to_hyperplane(struct isl_tab *tab, struct isl_tab_var *var)
2475 unsigned r;
2476 isl_int *row;
2477 int sgn;
2478 unsigned off = 2 + tab->M;
2480 if (var->is_zero)
2481 return 0;
2482 isl_assert(tab->mat->ctx, !var->is_redundant, return -1);
2483 isl_assert(tab->mat->ctx, var->is_nonneg, return -1);
2485 if (isl_tab_extend_cons(tab, 1) < 0)
2486 return -1;
2488 r = tab->n_con;
2489 tab->con[r].index = tab->n_row;
2490 tab->con[r].is_row = 1;
2491 tab->con[r].is_nonneg = 0;
2492 tab->con[r].is_zero = 0;
2493 tab->con[r].is_redundant = 0;
2494 tab->con[r].frozen = 0;
2495 tab->con[r].negated = 0;
2496 tab->row_var[tab->n_row] = ~r;
2497 row = tab->mat->row[tab->n_row];
2499 if (var->is_row) {
2500 isl_int_set(row[0], tab->mat->row[var->index][0]);
2501 isl_seq_neg(row + 1,
2502 tab->mat->row[var->index] + 1, 1 + tab->n_col);
2503 } else {
2504 isl_int_set_si(row[0], 1);
2505 isl_seq_clr(row + 1, 1 + tab->n_col);
2506 isl_int_set_si(row[off + var->index], -1);
2509 tab->n_row++;
2510 tab->n_con++;
2511 if (isl_tab_push_var(tab, isl_tab_undo_allocate, &tab->con[r]) < 0)
2512 return -1;
2514 sgn = sign_of_max(tab, &tab->con[r]);
2515 if (sgn < -1)
2516 return -1;
2517 if (sgn < 0) {
2518 if (isl_tab_mark_empty(tab) < 0)
2519 return -1;
2520 return 0;
2522 tab->con[r].is_nonneg = 1;
2523 if (isl_tab_push_var(tab, isl_tab_undo_nonneg, &tab->con[r]) < 0)
2524 return -1;
2525 /* sgn == 0 */
2526 if (close_row(tab, &tab->con[r]) < 0)
2527 return -1;
2529 return 0;
2532 /* Given a tableau "tab" and an inequality constraint "con" of the tableau,
2533 * relax the inequality by one. That is, the inequality r >= 0 is replaced
2534 * by r' = r + 1 >= 0.
2535 * If r is a row variable, we simply increase the constant term by one
2536 * (taking into account the denominator).
2537 * If r is a column variable, then we need to modify each row that
2538 * refers to r = r' - 1 by substituting this equality, effectively
2539 * subtracting the coefficient of the column from the constant.
2540 * We should only do this if the minimum is manifestly unbounded,
2541 * however. Otherwise, we may end up with negative sample values
2542 * for non-negative variables.
2543 * So, if r is a column variable with a minimum that is not
2544 * manifestly unbounded, then we need to move it to a row.
2545 * However, the sample value of this row may be negative,
2546 * even after the relaxation, so we need to restore it.
2547 * We therefore prefer to pivot a column up to a row, if possible.
2549 struct isl_tab *isl_tab_relax(struct isl_tab *tab, int con)
2551 struct isl_tab_var *var;
2552 unsigned off = 2 + tab->M;
2554 if (!tab)
2555 return NULL;
2557 var = &tab->con[con];
2559 if (var->is_row && (var->index < 0 || var->index < tab->n_redundant))
2560 isl_die(tab->mat->ctx, isl_error_invalid,
2561 "cannot relax redundant constraint", goto error);
2562 if (!var->is_row && (var->index < 0 || var->index < tab->n_dead))
2563 isl_die(tab->mat->ctx, isl_error_invalid,
2564 "cannot relax dead constraint", goto error);
2566 if (!var->is_row && !max_is_manifestly_unbounded(tab, var))
2567 if (to_row(tab, var, 1) < 0)
2568 goto error;
2569 if (!var->is_row && !min_is_manifestly_unbounded(tab, var))
2570 if (to_row(tab, var, -1) < 0)
2571 goto error;
2573 if (var->is_row) {
2574 isl_int_add(tab->mat->row[var->index][1],
2575 tab->mat->row[var->index][1], tab->mat->row[var->index][0]);
2576 if (restore_row(tab, var) < 0)
2577 goto error;
2578 } else {
2579 int i;
2581 for (i = 0; i < tab->n_row; ++i) {
2582 if (isl_int_is_zero(tab->mat->row[i][off + var->index]))
2583 continue;
2584 isl_int_sub(tab->mat->row[i][1], tab->mat->row[i][1],
2585 tab->mat->row[i][off + var->index]);
2590 if (isl_tab_push_var(tab, isl_tab_undo_relax, var) < 0)
2591 goto error;
2593 return tab;
2594 error:
2595 isl_tab_free(tab);
2596 return NULL;
2599 /* Remove the sign constraint from constraint "con".
2601 * If the constraint variable was originally marked non-negative,
2602 * then we make sure we mark it non-negative again during rollback.
2604 int isl_tab_unrestrict(struct isl_tab *tab, int con)
2606 struct isl_tab_var *var;
2608 if (!tab)
2609 return -1;
2611 var = &tab->con[con];
2612 if (!var->is_nonneg)
2613 return 0;
2615 var->is_nonneg = 0;
2616 if (isl_tab_push_var(tab, isl_tab_undo_unrestrict, var) < 0)
2617 return -1;
2619 return 0;
2622 int isl_tab_select_facet(struct isl_tab *tab, int con)
2624 if (!tab)
2625 return -1;
2627 return cut_to_hyperplane(tab, &tab->con[con]);
2630 static int may_be_equality(struct isl_tab *tab, int row)
2632 return tab->rational ? isl_int_is_zero(tab->mat->row[row][1])
2633 : isl_int_lt(tab->mat->row[row][1],
2634 tab->mat->row[row][0]);
2637 /* Check for (near) equalities among the constraints.
2638 * A constraint is an equality if it is non-negative and if
2639 * its maximal value is either
2640 * - zero (in case of rational tableaus), or
2641 * - strictly less than 1 (in case of integer tableaus)
2643 * We first mark all non-redundant and non-dead variables that
2644 * are not frozen and not obviously not an equality.
2645 * Then we iterate over all marked variables if they can attain
2646 * any values larger than zero or at least one.
2647 * If the maximal value is zero, we mark any column variables
2648 * that appear in the row as being zero and mark the row as being redundant.
2649 * Otherwise, if the maximal value is strictly less than one (and the
2650 * tableau is integer), then we restrict the value to being zero
2651 * by adding an opposite non-negative variable.
2653 int isl_tab_detect_implicit_equalities(struct isl_tab *tab)
2655 int i;
2656 unsigned n_marked;
2658 if (!tab)
2659 return -1;
2660 if (tab->empty)
2661 return 0;
2662 if (tab->n_dead == tab->n_col)
2663 return 0;
2665 n_marked = 0;
2666 for (i = tab->n_redundant; i < tab->n_row; ++i) {
2667 struct isl_tab_var *var = isl_tab_var_from_row(tab, i);
2668 var->marked = !var->frozen && var->is_nonneg &&
2669 may_be_equality(tab, i);
2670 if (var->marked)
2671 n_marked++;
2673 for (i = tab->n_dead; i < tab->n_col; ++i) {
2674 struct isl_tab_var *var = var_from_col(tab, i);
2675 var->marked = !var->frozen && var->is_nonneg;
2676 if (var->marked)
2677 n_marked++;
2679 while (n_marked) {
2680 struct isl_tab_var *var;
2681 int sgn;
2682 for (i = tab->n_redundant; i < tab->n_row; ++i) {
2683 var = isl_tab_var_from_row(tab, i);
2684 if (var->marked)
2685 break;
2687 if (i == tab->n_row) {
2688 for (i = tab->n_dead; i < tab->n_col; ++i) {
2689 var = var_from_col(tab, i);
2690 if (var->marked)
2691 break;
2693 if (i == tab->n_col)
2694 break;
2696 var->marked = 0;
2697 n_marked--;
2698 sgn = sign_of_max(tab, var);
2699 if (sgn < 0)
2700 return -1;
2701 if (sgn == 0) {
2702 if (close_row(tab, var) < 0)
2703 return -1;
2704 } else if (!tab->rational && !at_least_one(tab, var)) {
2705 if (cut_to_hyperplane(tab, var) < 0)
2706 return -1;
2707 return isl_tab_detect_implicit_equalities(tab);
2709 for (i = tab->n_redundant; i < tab->n_row; ++i) {
2710 var = isl_tab_var_from_row(tab, i);
2711 if (!var->marked)
2712 continue;
2713 if (may_be_equality(tab, i))
2714 continue;
2715 var->marked = 0;
2716 n_marked--;
2720 return 0;
2723 /* Update the element of row_var or col_var that corresponds to
2724 * constraint tab->con[i] to a move from position "old" to position "i".
2726 static int update_con_after_move(struct isl_tab *tab, int i, int old)
2728 int *p;
2729 int index;
2731 index = tab->con[i].index;
2732 if (index == -1)
2733 return 0;
2734 p = tab->con[i].is_row ? tab->row_var : tab->col_var;
2735 if (p[index] != ~old)
2736 isl_die(tab->mat->ctx, isl_error_internal,
2737 "broken internal state", return -1);
2738 p[index] = ~i;
2740 return 0;
2743 /* Rotate the "n" constraints starting at "first" to the right,
2744 * putting the last constraint in the position of the first constraint.
2746 static int rotate_constraints(struct isl_tab *tab, int first, int n)
2748 int i, last;
2749 struct isl_tab_var var;
2751 if (n <= 1)
2752 return 0;
2754 last = first + n - 1;
2755 var = tab->con[last];
2756 for (i = last; i > first; --i) {
2757 tab->con[i] = tab->con[i - 1];
2758 if (update_con_after_move(tab, i, i - 1) < 0)
2759 return -1;
2761 tab->con[first] = var;
2762 if (update_con_after_move(tab, first, last) < 0)
2763 return -1;
2765 return 0;
2768 /* Make the equalities that are implicit in "bmap" but that have been
2769 * detected in the corresponding "tab" explicit in "bmap" and update
2770 * "tab" to reflect the new order of the constraints.
2772 * In particular, if inequality i is an implicit equality then
2773 * isl_basic_map_inequality_to_equality will move the inequality
2774 * in front of the other equality and it will move the last inequality
2775 * in the position of inequality i.
2776 * In the tableau, the inequalities of "bmap" are stored after the equalities
2777 * and so the original order
2779 * E E E E E A A A I B B B B L
2781 * is changed into
2783 * I E E E E E A A A L B B B B
2785 * where I is the implicit equality, the E are equalities,
2786 * the A inequalities before I, the B inequalities after I and
2787 * L the last inequality.
2788 * We therefore need to rotate to the right two sets of constraints,
2789 * those up to and including I and those after I.
2791 * If "tab" contains any constraints that are not in "bmap" then they
2792 * appear after those in "bmap" and they should be left untouched.
2794 * Note that this function leaves "bmap" in a temporary state
2795 * as it does not call isl_basic_map_gauss. Calling this function
2796 * is the responsibility of the caller.
2798 __isl_give isl_basic_map *isl_tab_make_equalities_explicit(struct isl_tab *tab,
2799 __isl_take isl_basic_map *bmap)
2801 int i;
2803 if (!tab || !bmap)
2804 return isl_basic_map_free(bmap);
2805 if (tab->empty)
2806 return bmap;
2808 for (i = bmap->n_ineq - 1; i >= 0; --i) {
2809 if (!isl_tab_is_equality(tab, bmap->n_eq + i))
2810 continue;
2811 isl_basic_map_inequality_to_equality(bmap, i);
2812 if (rotate_constraints(tab, 0, tab->n_eq + i + 1) < 0)
2813 return isl_basic_map_free(bmap);
2814 if (rotate_constraints(tab, tab->n_eq + i + 1,
2815 bmap->n_ineq - i) < 0)
2816 return isl_basic_map_free(bmap);
2817 tab->n_eq++;
2820 return bmap;
2823 static int con_is_redundant(struct isl_tab *tab, struct isl_tab_var *var)
2825 if (!tab)
2826 return -1;
2827 if (tab->rational) {
2828 int sgn = sign_of_min(tab, var);
2829 if (sgn < -1)
2830 return -1;
2831 return sgn >= 0;
2832 } else {
2833 int irred = isl_tab_min_at_most_neg_one(tab, var);
2834 if (irred < 0)
2835 return -1;
2836 return !irred;
2840 /* Check for (near) redundant constraints.
2841 * A constraint is redundant if it is non-negative and if
2842 * its minimal value (temporarily ignoring the non-negativity) is either
2843 * - zero (in case of rational tableaus), or
2844 * - strictly larger than -1 (in case of integer tableaus)
2846 * We first mark all non-redundant and non-dead variables that
2847 * are not frozen and not obviously negatively unbounded.
2848 * Then we iterate over all marked variables if they can attain
2849 * any values smaller than zero or at most negative one.
2850 * If not, we mark the row as being redundant (assuming it hasn't
2851 * been detected as being obviously redundant in the mean time).
2853 int isl_tab_detect_redundant(struct isl_tab *tab)
2855 int i;
2856 unsigned n_marked;
2858 if (!tab)
2859 return -1;
2860 if (tab->empty)
2861 return 0;
2862 if (tab->n_redundant == tab->n_row)
2863 return 0;
2865 n_marked = 0;
2866 for (i = tab->n_redundant; i < tab->n_row; ++i) {
2867 struct isl_tab_var *var = isl_tab_var_from_row(tab, i);
2868 var->marked = !var->frozen && var->is_nonneg;
2869 if (var->marked)
2870 n_marked++;
2872 for (i = tab->n_dead; i < tab->n_col; ++i) {
2873 struct isl_tab_var *var = var_from_col(tab, i);
2874 var->marked = !var->frozen && var->is_nonneg &&
2875 !min_is_manifestly_unbounded(tab, var);
2876 if (var->marked)
2877 n_marked++;
2879 while (n_marked) {
2880 struct isl_tab_var *var;
2881 int red;
2882 for (i = tab->n_redundant; i < tab->n_row; ++i) {
2883 var = isl_tab_var_from_row(tab, i);
2884 if (var->marked)
2885 break;
2887 if (i == tab->n_row) {
2888 for (i = tab->n_dead; i < tab->n_col; ++i) {
2889 var = var_from_col(tab, i);
2890 if (var->marked)
2891 break;
2893 if (i == tab->n_col)
2894 break;
2896 var->marked = 0;
2897 n_marked--;
2898 red = con_is_redundant(tab, var);
2899 if (red < 0)
2900 return -1;
2901 if (red && !var->is_redundant)
2902 if (isl_tab_mark_redundant(tab, var->index) < 0)
2903 return -1;
2904 for (i = tab->n_dead; i < tab->n_col; ++i) {
2905 var = var_from_col(tab, i);
2906 if (!var->marked)
2907 continue;
2908 if (!min_is_manifestly_unbounded(tab, var))
2909 continue;
2910 var->marked = 0;
2911 n_marked--;
2915 return 0;
2918 int isl_tab_is_equality(struct isl_tab *tab, int con)
2920 int row;
2921 unsigned off;
2923 if (!tab)
2924 return -1;
2925 if (tab->con[con].is_zero)
2926 return 1;
2927 if (tab->con[con].is_redundant)
2928 return 0;
2929 if (!tab->con[con].is_row)
2930 return tab->con[con].index < tab->n_dead;
2932 row = tab->con[con].index;
2934 off = 2 + tab->M;
2935 return isl_int_is_zero(tab->mat->row[row][1]) &&
2936 (!tab->M || isl_int_is_zero(tab->mat->row[row][2])) &&
2937 isl_seq_first_non_zero(tab->mat->row[row] + off + tab->n_dead,
2938 tab->n_col - tab->n_dead) == -1;
2941 /* Return the minimal value of the affine expression "f" with denominator
2942 * "denom" in *opt, *opt_denom, assuming the tableau is not empty and
2943 * the expression cannot attain arbitrarily small values.
2944 * If opt_denom is NULL, then *opt is rounded up to the nearest integer.
2945 * The return value reflects the nature of the result (empty, unbounded,
2946 * minimal value returned in *opt).
2948 enum isl_lp_result isl_tab_min(struct isl_tab *tab,
2949 isl_int *f, isl_int denom, isl_int *opt, isl_int *opt_denom,
2950 unsigned flags)
2952 int r;
2953 enum isl_lp_result res = isl_lp_ok;
2954 struct isl_tab_var *var;
2955 struct isl_tab_undo *snap;
2957 if (!tab)
2958 return isl_lp_error;
2960 if (tab->empty)
2961 return isl_lp_empty;
2963 snap = isl_tab_snap(tab);
2964 r = isl_tab_add_row(tab, f);
2965 if (r < 0)
2966 return isl_lp_error;
2967 var = &tab->con[r];
2968 for (;;) {
2969 int row, col;
2970 find_pivot(tab, var, var, -1, &row, &col);
2971 if (row == var->index) {
2972 res = isl_lp_unbounded;
2973 break;
2975 if (row == -1)
2976 break;
2977 if (isl_tab_pivot(tab, row, col) < 0)
2978 return isl_lp_error;
2980 isl_int_mul(tab->mat->row[var->index][0],
2981 tab->mat->row[var->index][0], denom);
2982 if (ISL_FL_ISSET(flags, ISL_TAB_SAVE_DUAL)) {
2983 int i;
2985 isl_vec_free(tab->dual);
2986 tab->dual = isl_vec_alloc(tab->mat->ctx, 1 + tab->n_con);
2987 if (!tab->dual)
2988 return isl_lp_error;
2989 isl_int_set(tab->dual->el[0], tab->mat->row[var->index][0]);
2990 for (i = 0; i < tab->n_con; ++i) {
2991 int pos;
2992 if (tab->con[i].is_row) {
2993 isl_int_set_si(tab->dual->el[1 + i], 0);
2994 continue;
2996 pos = 2 + tab->M + tab->con[i].index;
2997 if (tab->con[i].negated)
2998 isl_int_neg(tab->dual->el[1 + i],
2999 tab->mat->row[var->index][pos]);
3000 else
3001 isl_int_set(tab->dual->el[1 + i],
3002 tab->mat->row[var->index][pos]);
3005 if (opt && res == isl_lp_ok) {
3006 if (opt_denom) {
3007 isl_int_set(*opt, tab->mat->row[var->index][1]);
3008 isl_int_set(*opt_denom, tab->mat->row[var->index][0]);
3009 } else
3010 isl_int_cdiv_q(*opt, tab->mat->row[var->index][1],
3011 tab->mat->row[var->index][0]);
3013 if (isl_tab_rollback(tab, snap) < 0)
3014 return isl_lp_error;
3015 return res;
3018 int isl_tab_is_redundant(struct isl_tab *tab, int con)
3020 if (!tab)
3021 return -1;
3022 if (tab->con[con].is_zero)
3023 return 0;
3024 if (tab->con[con].is_redundant)
3025 return 1;
3026 return tab->con[con].is_row && tab->con[con].index < tab->n_redundant;
3029 /* Take a snapshot of the tableau that can be restored by s call to
3030 * isl_tab_rollback.
3032 struct isl_tab_undo *isl_tab_snap(struct isl_tab *tab)
3034 if (!tab)
3035 return NULL;
3036 tab->need_undo = 1;
3037 return tab->top;
3040 /* Undo the operation performed by isl_tab_relax.
3042 static int unrelax(struct isl_tab *tab, struct isl_tab_var *var) WARN_UNUSED;
3043 static int unrelax(struct isl_tab *tab, struct isl_tab_var *var)
3045 unsigned off = 2 + tab->M;
3047 if (!var->is_row && !max_is_manifestly_unbounded(tab, var))
3048 if (to_row(tab, var, 1) < 0)
3049 return -1;
3051 if (var->is_row) {
3052 isl_int_sub(tab->mat->row[var->index][1],
3053 tab->mat->row[var->index][1], tab->mat->row[var->index][0]);
3054 if (var->is_nonneg) {
3055 int sgn = restore_row(tab, var);
3056 isl_assert(tab->mat->ctx, sgn >= 0, return -1);
3058 } else {
3059 int i;
3061 for (i = 0; i < tab->n_row; ++i) {
3062 if (isl_int_is_zero(tab->mat->row[i][off + var->index]))
3063 continue;
3064 isl_int_add(tab->mat->row[i][1], tab->mat->row[i][1],
3065 tab->mat->row[i][off + var->index]);
3070 return 0;
3073 /* Undo the operation performed by isl_tab_unrestrict.
3075 * In particular, mark the variable as being non-negative and make
3076 * sure the sample value respects this constraint.
3078 static int ununrestrict(struct isl_tab *tab, struct isl_tab_var *var)
3080 var->is_nonneg = 1;
3082 if (var->is_row && restore_row(tab, var) < -1)
3083 return -1;
3085 return 0;
3088 static int perform_undo_var(struct isl_tab *tab, struct isl_tab_undo *undo) WARN_UNUSED;
3089 static int perform_undo_var(struct isl_tab *tab, struct isl_tab_undo *undo)
3091 struct isl_tab_var *var = var_from_index(tab, undo->u.var_index);
3092 switch (undo->type) {
3093 case isl_tab_undo_nonneg:
3094 var->is_nonneg = 0;
3095 break;
3096 case isl_tab_undo_redundant:
3097 var->is_redundant = 0;
3098 tab->n_redundant--;
3099 restore_row(tab, isl_tab_var_from_row(tab, tab->n_redundant));
3100 break;
3101 case isl_tab_undo_freeze:
3102 var->frozen = 0;
3103 break;
3104 case isl_tab_undo_zero:
3105 var->is_zero = 0;
3106 if (!var->is_row)
3107 tab->n_dead--;
3108 break;
3109 case isl_tab_undo_allocate:
3110 if (undo->u.var_index >= 0) {
3111 isl_assert(tab->mat->ctx, !var->is_row, return -1);
3112 return drop_col(tab, var->index);
3114 if (!var->is_row) {
3115 if (!max_is_manifestly_unbounded(tab, var)) {
3116 if (to_row(tab, var, 1) < 0)
3117 return -1;
3118 } else if (!min_is_manifestly_unbounded(tab, var)) {
3119 if (to_row(tab, var, -1) < 0)
3120 return -1;
3121 } else
3122 if (to_row(tab, var, 0) < 0)
3123 return -1;
3125 return drop_row(tab, var->index);
3126 case isl_tab_undo_relax:
3127 return unrelax(tab, var);
3128 case isl_tab_undo_unrestrict:
3129 return ununrestrict(tab, var);
3130 default:
3131 isl_die(tab->mat->ctx, isl_error_internal,
3132 "perform_undo_var called on invalid undo record",
3133 return -1);
3136 return 0;
3139 /* Restore the tableau to the state where the basic variables
3140 * are those in "col_var".
3141 * We first construct a list of variables that are currently in
3142 * the basis, but shouldn't. Then we iterate over all variables
3143 * that should be in the basis and for each one that is currently
3144 * not in the basis, we exchange it with one of the elements of the
3145 * list constructed before.
3146 * We can always find an appropriate variable to pivot with because
3147 * the current basis is mapped to the old basis by a non-singular
3148 * matrix and so we can never end up with a zero row.
3150 static int restore_basis(struct isl_tab *tab, int *col_var)
3152 int i, j;
3153 int n_extra = 0;
3154 int *extra = NULL; /* current columns that contain bad stuff */
3155 unsigned off = 2 + tab->M;
3157 extra = isl_alloc_array(tab->mat->ctx, int, tab->n_col);
3158 if (tab->n_col && !extra)
3159 goto error;
3160 for (i = 0; i < tab->n_col; ++i) {
3161 for (j = 0; j < tab->n_col; ++j)
3162 if (tab->col_var[i] == col_var[j])
3163 break;
3164 if (j < tab->n_col)
3165 continue;
3166 extra[n_extra++] = i;
3168 for (i = 0; i < tab->n_col && n_extra > 0; ++i) {
3169 struct isl_tab_var *var;
3170 int row;
3172 for (j = 0; j < tab->n_col; ++j)
3173 if (col_var[i] == tab->col_var[j])
3174 break;
3175 if (j < tab->n_col)
3176 continue;
3177 var = var_from_index(tab, col_var[i]);
3178 row = var->index;
3179 for (j = 0; j < n_extra; ++j)
3180 if (!isl_int_is_zero(tab->mat->row[row][off+extra[j]]))
3181 break;
3182 isl_assert(tab->mat->ctx, j < n_extra, goto error);
3183 if (isl_tab_pivot(tab, row, extra[j]) < 0)
3184 goto error;
3185 extra[j] = extra[--n_extra];
3188 free(extra);
3189 return 0;
3190 error:
3191 free(extra);
3192 return -1;
3195 /* Remove all samples with index n or greater, i.e., those samples
3196 * that were added since we saved this number of samples in
3197 * isl_tab_save_samples.
3199 static void drop_samples_since(struct isl_tab *tab, int n)
3201 int i;
3203 for (i = tab->n_sample - 1; i >= 0 && tab->n_sample > n; --i) {
3204 if (tab->sample_index[i] < n)
3205 continue;
3207 if (i != tab->n_sample - 1) {
3208 int t = tab->sample_index[tab->n_sample-1];
3209 tab->sample_index[tab->n_sample-1] = tab->sample_index[i];
3210 tab->sample_index[i] = t;
3211 isl_mat_swap_rows(tab->samples, tab->n_sample-1, i);
3213 tab->n_sample--;
3217 static int perform_undo(struct isl_tab *tab, struct isl_tab_undo *undo) WARN_UNUSED;
3218 static int perform_undo(struct isl_tab *tab, struct isl_tab_undo *undo)
3220 switch (undo->type) {
3221 case isl_tab_undo_empty:
3222 tab->empty = 0;
3223 break;
3224 case isl_tab_undo_nonneg:
3225 case isl_tab_undo_redundant:
3226 case isl_tab_undo_freeze:
3227 case isl_tab_undo_zero:
3228 case isl_tab_undo_allocate:
3229 case isl_tab_undo_relax:
3230 case isl_tab_undo_unrestrict:
3231 return perform_undo_var(tab, undo);
3232 case isl_tab_undo_bmap_eq:
3233 return isl_basic_map_free_equality(tab->bmap, 1);
3234 case isl_tab_undo_bmap_ineq:
3235 return isl_basic_map_free_inequality(tab->bmap, 1);
3236 case isl_tab_undo_bmap_div:
3237 if (isl_basic_map_free_div(tab->bmap, 1) < 0)
3238 return -1;
3239 if (tab->samples)
3240 tab->samples->n_col--;
3241 break;
3242 case isl_tab_undo_saved_basis:
3243 if (restore_basis(tab, undo->u.col_var) < 0)
3244 return -1;
3245 break;
3246 case isl_tab_undo_drop_sample:
3247 tab->n_outside--;
3248 break;
3249 case isl_tab_undo_saved_samples:
3250 drop_samples_since(tab, undo->u.n);
3251 break;
3252 case isl_tab_undo_callback:
3253 return undo->u.callback->run(undo->u.callback);
3254 default:
3255 isl_assert(tab->mat->ctx, 0, return -1);
3257 return 0;
3260 /* Return the tableau to the state it was in when the snapshot "snap"
3261 * was taken.
3263 int isl_tab_rollback(struct isl_tab *tab, struct isl_tab_undo *snap)
3265 struct isl_tab_undo *undo, *next;
3267 if (!tab)
3268 return -1;
3270 tab->in_undo = 1;
3271 for (undo = tab->top; undo && undo != &tab->bottom; undo = next) {
3272 next = undo->next;
3273 if (undo == snap)
3274 break;
3275 if (perform_undo(tab, undo) < 0) {
3276 tab->top = undo;
3277 free_undo(tab);
3278 tab->in_undo = 0;
3279 return -1;
3281 free_undo_record(undo);
3283 tab->in_undo = 0;
3284 tab->top = undo;
3285 if (!undo)
3286 return -1;
3287 return 0;
3290 /* The given row "row" represents an inequality violated by all
3291 * points in the tableau. Check for some special cases of such
3292 * separating constraints.
3293 * In particular, if the row has been reduced to the constant -1,
3294 * then we know the inequality is adjacent (but opposite) to
3295 * an equality in the tableau.
3296 * If the row has been reduced to r = c*(-1 -r'), with r' an inequality
3297 * of the tableau and c a positive constant, then the inequality
3298 * is adjacent (but opposite) to the inequality r'.
3300 static enum isl_ineq_type separation_type(struct isl_tab *tab, unsigned row)
3302 int pos;
3303 unsigned off = 2 + tab->M;
3305 if (tab->rational)
3306 return isl_ineq_separate;
3308 if (!isl_int_is_one(tab->mat->row[row][0]))
3309 return isl_ineq_separate;
3311 pos = isl_seq_first_non_zero(tab->mat->row[row] + off + tab->n_dead,
3312 tab->n_col - tab->n_dead);
3313 if (pos == -1) {
3314 if (isl_int_is_negone(tab->mat->row[row][1]))
3315 return isl_ineq_adj_eq;
3316 else
3317 return isl_ineq_separate;
3320 if (!isl_int_eq(tab->mat->row[row][1],
3321 tab->mat->row[row][off + tab->n_dead + pos]))
3322 return isl_ineq_separate;
3324 pos = isl_seq_first_non_zero(
3325 tab->mat->row[row] + off + tab->n_dead + pos + 1,
3326 tab->n_col - tab->n_dead - pos - 1);
3328 return pos == -1 ? isl_ineq_adj_ineq : isl_ineq_separate;
3331 /* Check the effect of inequality "ineq" on the tableau "tab".
3332 * The result may be
3333 * isl_ineq_redundant: satisfied by all points in the tableau
3334 * isl_ineq_separate: satisfied by no point in the tableau
3335 * isl_ineq_cut: satisfied by some by not all points
3336 * isl_ineq_adj_eq: adjacent to an equality
3337 * isl_ineq_adj_ineq: adjacent to an inequality.
3339 enum isl_ineq_type isl_tab_ineq_type(struct isl_tab *tab, isl_int *ineq)
3341 enum isl_ineq_type type = isl_ineq_error;
3342 struct isl_tab_undo *snap = NULL;
3343 int con;
3344 int row;
3346 if (!tab)
3347 return isl_ineq_error;
3349 if (isl_tab_extend_cons(tab, 1) < 0)
3350 return isl_ineq_error;
3352 snap = isl_tab_snap(tab);
3354 con = isl_tab_add_row(tab, ineq);
3355 if (con < 0)
3356 goto error;
3358 row = tab->con[con].index;
3359 if (isl_tab_row_is_redundant(tab, row))
3360 type = isl_ineq_redundant;
3361 else if (isl_int_is_neg(tab->mat->row[row][1]) &&
3362 (tab->rational ||
3363 isl_int_abs_ge(tab->mat->row[row][1],
3364 tab->mat->row[row][0]))) {
3365 int nonneg = at_least_zero(tab, &tab->con[con]);
3366 if (nonneg < 0)
3367 goto error;
3368 if (nonneg)
3369 type = isl_ineq_cut;
3370 else
3371 type = separation_type(tab, row);
3372 } else {
3373 int red = con_is_redundant(tab, &tab->con[con]);
3374 if (red < 0)
3375 goto error;
3376 if (!red)
3377 type = isl_ineq_cut;
3378 else
3379 type = isl_ineq_redundant;
3382 if (isl_tab_rollback(tab, snap))
3383 return isl_ineq_error;
3384 return type;
3385 error:
3386 return isl_ineq_error;
3389 int isl_tab_track_bmap(struct isl_tab *tab, __isl_take isl_basic_map *bmap)
3391 bmap = isl_basic_map_cow(bmap);
3392 if (!tab || !bmap)
3393 goto error;
3395 if (tab->empty) {
3396 bmap = isl_basic_map_set_to_empty(bmap);
3397 if (!bmap)
3398 goto error;
3399 tab->bmap = bmap;
3400 return 0;
3403 isl_assert(tab->mat->ctx, tab->n_eq == bmap->n_eq, goto error);
3404 isl_assert(tab->mat->ctx,
3405 tab->n_con == bmap->n_eq + bmap->n_ineq, goto error);
3407 tab->bmap = bmap;
3409 return 0;
3410 error:
3411 isl_basic_map_free(bmap);
3412 return -1;
3415 int isl_tab_track_bset(struct isl_tab *tab, __isl_take isl_basic_set *bset)
3417 return isl_tab_track_bmap(tab, (isl_basic_map *)bset);
3420 __isl_keep isl_basic_set *isl_tab_peek_bset(struct isl_tab *tab)
3422 if (!tab)
3423 return NULL;
3425 return (isl_basic_set *)tab->bmap;
3428 static void isl_tab_print_internal(__isl_keep struct isl_tab *tab,
3429 FILE *out, int indent)
3431 unsigned r, c;
3432 int i;
3434 if (!tab) {
3435 fprintf(out, "%*snull tab\n", indent, "");
3436 return;
3438 fprintf(out, "%*sn_redundant: %d, n_dead: %d", indent, "",
3439 tab->n_redundant, tab->n_dead);
3440 if (tab->rational)
3441 fprintf(out, ", rational");
3442 if (tab->empty)
3443 fprintf(out, ", empty");
3444 fprintf(out, "\n");
3445 fprintf(out, "%*s[", indent, "");
3446 for (i = 0; i < tab->n_var; ++i) {
3447 if (i)
3448 fprintf(out, (i == tab->n_param ||
3449 i == tab->n_var - tab->n_div) ? "; "
3450 : ", ");
3451 fprintf(out, "%c%d%s", tab->var[i].is_row ? 'r' : 'c',
3452 tab->var[i].index,
3453 tab->var[i].is_zero ? " [=0]" :
3454 tab->var[i].is_redundant ? " [R]" : "");
3456 fprintf(out, "]\n");
3457 fprintf(out, "%*s[", indent, "");
3458 for (i = 0; i < tab->n_con; ++i) {
3459 if (i)
3460 fprintf(out, ", ");
3461 fprintf(out, "%c%d%s", tab->con[i].is_row ? 'r' : 'c',
3462 tab->con[i].index,
3463 tab->con[i].is_zero ? " [=0]" :
3464 tab->con[i].is_redundant ? " [R]" : "");
3466 fprintf(out, "]\n");
3467 fprintf(out, "%*s[", indent, "");
3468 for (i = 0; i < tab->n_row; ++i) {
3469 const char *sign = "";
3470 if (i)
3471 fprintf(out, ", ");
3472 if (tab->row_sign) {
3473 if (tab->row_sign[i] == isl_tab_row_unknown)
3474 sign = "?";
3475 else if (tab->row_sign[i] == isl_tab_row_neg)
3476 sign = "-";
3477 else if (tab->row_sign[i] == isl_tab_row_pos)
3478 sign = "+";
3479 else
3480 sign = "+-";
3482 fprintf(out, "r%d: %d%s%s", i, tab->row_var[i],
3483 isl_tab_var_from_row(tab, i)->is_nonneg ? " [>=0]" : "", sign);
3485 fprintf(out, "]\n");
3486 fprintf(out, "%*s[", indent, "");
3487 for (i = 0; i < tab->n_col; ++i) {
3488 if (i)
3489 fprintf(out, ", ");
3490 fprintf(out, "c%d: %d%s", i, tab->col_var[i],
3491 var_from_col(tab, i)->is_nonneg ? " [>=0]" : "");
3493 fprintf(out, "]\n");
3494 r = tab->mat->n_row;
3495 tab->mat->n_row = tab->n_row;
3496 c = tab->mat->n_col;
3497 tab->mat->n_col = 2 + tab->M + tab->n_col;
3498 isl_mat_print_internal(tab->mat, out, indent);
3499 tab->mat->n_row = r;
3500 tab->mat->n_col = c;
3501 if (tab->bmap)
3502 isl_basic_map_print_internal(tab->bmap, out, indent);
3505 void isl_tab_dump(__isl_keep struct isl_tab *tab)
3507 isl_tab_print_internal(tab, stderr, 0);