doc: document isl_constraint_is_equality
[isl.git] / isl_tab.c
blobd0d9c26a3da01b9105e03836e9f29516581de22e
1 /*
2 * Copyright 2008-2009 Katholieke Universiteit Leuven
4 * Use of this software is governed by the GNU LGPLv2.1 license
6 * Written by Sven Verdoolaege, K.U.Leuven, Departement
7 * Computerwetenschappen, Celestijnenlaan 200A, B-3001 Leuven, Belgium
8 */
10 #include "isl_mat.h"
11 #include "isl_map_private.h"
12 #include "isl_tab.h"
13 #include "isl_seq.h"
16 * The implementation of tableaus in this file was inspired by Section 8
17 * of David Detlefs, Greg Nelson and James B. Saxe, "Simplify: a theorem
18 * prover for program checking".
21 struct isl_tab *isl_tab_alloc(struct isl_ctx *ctx,
22 unsigned n_row, unsigned n_var, unsigned M)
24 int i;
25 struct isl_tab *tab;
26 unsigned off = 2 + M;
28 tab = isl_calloc_type(ctx, struct isl_tab);
29 if (!tab)
30 return NULL;
31 tab->mat = isl_mat_alloc(ctx, n_row, off + n_var);
32 if (!tab->mat)
33 goto error;
34 tab->var = isl_alloc_array(ctx, struct isl_tab_var, n_var);
35 if (!tab->var)
36 goto error;
37 tab->con = isl_alloc_array(ctx, struct isl_tab_var, n_row);
38 if (!tab->con)
39 goto error;
40 tab->col_var = isl_alloc_array(ctx, int, n_var);
41 if (!tab->col_var)
42 goto error;
43 tab->row_var = isl_alloc_array(ctx, int, n_row);
44 if (!tab->row_var)
45 goto error;
46 for (i = 0; i < n_var; ++i) {
47 tab->var[i].index = i;
48 tab->var[i].is_row = 0;
49 tab->var[i].is_nonneg = 0;
50 tab->var[i].is_zero = 0;
51 tab->var[i].is_redundant = 0;
52 tab->var[i].frozen = 0;
53 tab->var[i].negated = 0;
54 tab->col_var[i] = i;
56 tab->n_row = 0;
57 tab->n_con = 0;
58 tab->n_eq = 0;
59 tab->max_con = n_row;
60 tab->n_col = n_var;
61 tab->n_var = n_var;
62 tab->max_var = n_var;
63 tab->n_param = 0;
64 tab->n_div = 0;
65 tab->n_dead = 0;
66 tab->n_redundant = 0;
67 tab->need_undo = 0;
68 tab->rational = 0;
69 tab->empty = 0;
70 tab->in_undo = 0;
71 tab->M = M;
72 tab->cone = 0;
73 tab->bottom.type = isl_tab_undo_bottom;
74 tab->bottom.next = NULL;
75 tab->top = &tab->bottom;
77 tab->n_zero = 0;
78 tab->n_unbounded = 0;
79 tab->basis = NULL;
81 return tab;
82 error:
83 isl_tab_free(tab);
84 return NULL;
87 int isl_tab_extend_cons(struct isl_tab *tab, unsigned n_new)
89 unsigned off = 2 + tab->M;
91 if (!tab)
92 return -1;
94 if (tab->max_con < tab->n_con + n_new) {
95 struct isl_tab_var *con;
97 con = isl_realloc_array(tab->mat->ctx, tab->con,
98 struct isl_tab_var, tab->max_con + n_new);
99 if (!con)
100 return -1;
101 tab->con = con;
102 tab->max_con += n_new;
104 if (tab->mat->n_row < tab->n_row + n_new) {
105 int *row_var;
107 tab->mat = isl_mat_extend(tab->mat,
108 tab->n_row + n_new, off + tab->n_col);
109 if (!tab->mat)
110 return -1;
111 row_var = isl_realloc_array(tab->mat->ctx, tab->row_var,
112 int, tab->mat->n_row);
113 if (!row_var)
114 return -1;
115 tab->row_var = row_var;
116 if (tab->row_sign) {
117 enum isl_tab_row_sign *s;
118 s = isl_realloc_array(tab->mat->ctx, tab->row_sign,
119 enum isl_tab_row_sign, tab->mat->n_row);
120 if (!s)
121 return -1;
122 tab->row_sign = s;
125 return 0;
128 /* Make room for at least n_new extra variables.
129 * Return -1 if anything went wrong.
131 int isl_tab_extend_vars(struct isl_tab *tab, unsigned n_new)
133 struct isl_tab_var *var;
134 unsigned off = 2 + tab->M;
136 if (tab->max_var < tab->n_var + n_new) {
137 var = isl_realloc_array(tab->mat->ctx, tab->var,
138 struct isl_tab_var, tab->n_var + n_new);
139 if (!var)
140 return -1;
141 tab->var = var;
142 tab->max_var += n_new;
145 if (tab->mat->n_col < off + tab->n_col + n_new) {
146 int *p;
148 tab->mat = isl_mat_extend(tab->mat,
149 tab->mat->n_row, off + tab->n_col + n_new);
150 if (!tab->mat)
151 return -1;
152 p = isl_realloc_array(tab->mat->ctx, tab->col_var,
153 int, tab->n_col + n_new);
154 if (!p)
155 return -1;
156 tab->col_var = p;
159 return 0;
162 struct isl_tab *isl_tab_extend(struct isl_tab *tab, unsigned n_new)
164 if (isl_tab_extend_cons(tab, n_new) >= 0)
165 return tab;
167 isl_tab_free(tab);
168 return NULL;
171 static void free_undo(struct isl_tab *tab)
173 struct isl_tab_undo *undo, *next;
175 for (undo = tab->top; undo && undo != &tab->bottom; undo = next) {
176 next = undo->next;
177 free(undo);
179 tab->top = undo;
182 void isl_tab_free(struct isl_tab *tab)
184 if (!tab)
185 return;
186 free_undo(tab);
187 isl_mat_free(tab->mat);
188 isl_vec_free(tab->dual);
189 isl_basic_map_free(tab->bmap);
190 free(tab->var);
191 free(tab->con);
192 free(tab->row_var);
193 free(tab->col_var);
194 free(tab->row_sign);
195 isl_mat_free(tab->samples);
196 free(tab->sample_index);
197 isl_mat_free(tab->basis);
198 free(tab);
201 struct isl_tab *isl_tab_dup(struct isl_tab *tab)
203 int i;
204 struct isl_tab *dup;
205 unsigned off;
207 if (!tab)
208 return NULL;
210 off = 2 + tab->M;
211 dup = isl_calloc_type(tab->ctx, struct isl_tab);
212 if (!dup)
213 return NULL;
214 dup->mat = isl_mat_dup(tab->mat);
215 if (!dup->mat)
216 goto error;
217 dup->var = isl_alloc_array(tab->ctx, struct isl_tab_var, tab->max_var);
218 if (!dup->var)
219 goto error;
220 for (i = 0; i < tab->n_var; ++i)
221 dup->var[i] = tab->var[i];
222 dup->con = isl_alloc_array(tab->ctx, struct isl_tab_var, tab->max_con);
223 if (!dup->con)
224 goto error;
225 for (i = 0; i < tab->n_con; ++i)
226 dup->con[i] = tab->con[i];
227 dup->col_var = isl_alloc_array(tab->ctx, int, tab->mat->n_col - off);
228 if (!dup->col_var)
229 goto error;
230 for (i = 0; i < tab->n_col; ++i)
231 dup->col_var[i] = tab->col_var[i];
232 dup->row_var = isl_alloc_array(tab->ctx, int, tab->mat->n_row);
233 if (!dup->row_var)
234 goto error;
235 for (i = 0; i < tab->n_row; ++i)
236 dup->row_var[i] = tab->row_var[i];
237 if (tab->row_sign) {
238 dup->row_sign = isl_alloc_array(tab->ctx, enum isl_tab_row_sign,
239 tab->mat->n_row);
240 if (!dup->row_sign)
241 goto error;
242 for (i = 0; i < tab->n_row; ++i)
243 dup->row_sign[i] = tab->row_sign[i];
245 if (tab->samples) {
246 dup->samples = isl_mat_dup(tab->samples);
247 if (!dup->samples)
248 goto error;
249 dup->sample_index = isl_alloc_array(tab->mat->ctx, int,
250 tab->samples->n_row);
251 if (!dup->sample_index)
252 goto error;
253 dup->n_sample = tab->n_sample;
254 dup->n_outside = tab->n_outside;
256 dup->n_row = tab->n_row;
257 dup->n_con = tab->n_con;
258 dup->n_eq = tab->n_eq;
259 dup->max_con = tab->max_con;
260 dup->n_col = tab->n_col;
261 dup->n_var = tab->n_var;
262 dup->max_var = tab->max_var;
263 dup->n_param = tab->n_param;
264 dup->n_div = tab->n_div;
265 dup->n_dead = tab->n_dead;
266 dup->n_redundant = tab->n_redundant;
267 dup->rational = tab->rational;
268 dup->empty = tab->empty;
269 dup->need_undo = 0;
270 dup->in_undo = 0;
271 dup->M = tab->M;
272 tab->cone = tab->cone;
273 dup->bottom.type = isl_tab_undo_bottom;
274 dup->bottom.next = NULL;
275 dup->top = &dup->bottom;
277 dup->n_zero = tab->n_zero;
278 dup->n_unbounded = tab->n_unbounded;
279 dup->basis = isl_mat_dup(tab->basis);
281 return dup;
282 error:
283 isl_tab_free(dup);
284 return NULL;
287 /* Construct the coefficient matrix of the product tableau
288 * of two tableaus.
289 * mat{1,2} is the coefficient matrix of tableau {1,2}
290 * row{1,2} is the number of rows in tableau {1,2}
291 * col{1,2} is the number of columns in tableau {1,2}
292 * off is the offset to the coefficient column (skipping the
293 * denominator, the constant term and the big parameter if any)
294 * r{1,2} is the number of redundant rows in tableau {1,2}
295 * d{1,2} is the number of dead columns in tableau {1,2}
297 * The order of the rows and columns in the result is as explained
298 * in isl_tab_product.
300 static struct isl_mat *tab_mat_product(struct isl_mat *mat1,
301 struct isl_mat *mat2, unsigned row1, unsigned row2,
302 unsigned col1, unsigned col2,
303 unsigned off, unsigned r1, unsigned r2, unsigned d1, unsigned d2)
305 int i;
306 struct isl_mat *prod;
307 unsigned n;
309 prod = isl_mat_alloc(mat1->ctx, mat1->n_row + mat2->n_row,
310 off + col1 + col2);
312 n = 0;
313 for (i = 0; i < r1; ++i) {
314 isl_seq_cpy(prod->row[n + i], mat1->row[i], off + d1);
315 isl_seq_clr(prod->row[n + i] + off + d1, d2);
316 isl_seq_cpy(prod->row[n + i] + off + d1 + d2,
317 mat1->row[i] + off + d1, col1 - d1);
318 isl_seq_clr(prod->row[n + i] + off + col1 + d1, col2 - d2);
321 n += r1;
322 for (i = 0; i < r2; ++i) {
323 isl_seq_cpy(prod->row[n + i], mat2->row[i], off);
324 isl_seq_clr(prod->row[n + i] + off, d1);
325 isl_seq_cpy(prod->row[n + i] + off + d1,
326 mat2->row[i] + off, d2);
327 isl_seq_clr(prod->row[n + i] + off + d1 + d2, col1 - d1);
328 isl_seq_cpy(prod->row[n + i] + off + col1 + d1,
329 mat2->row[i] + off + d2, col2 - d2);
332 n += r2;
333 for (i = 0; i < row1 - r1; ++i) {
334 isl_seq_cpy(prod->row[n + i], mat1->row[r1 + i], off + d1);
335 isl_seq_clr(prod->row[n + i] + off + d1, d2);
336 isl_seq_cpy(prod->row[n + i] + off + d1 + d2,
337 mat1->row[r1 + i] + off + d1, col1 - d1);
338 isl_seq_clr(prod->row[n + i] + off + col1 + d1, col2 - d2);
341 n += row1 - r1;
342 for (i = 0; i < row2 - r2; ++i) {
343 isl_seq_cpy(prod->row[n + i], mat2->row[r2 + i], off);
344 isl_seq_clr(prod->row[n + i] + off, d1);
345 isl_seq_cpy(prod->row[n + i] + off + d1,
346 mat2->row[r2 + i] + off, d2);
347 isl_seq_clr(prod->row[n + i] + off + d1 + d2, col1 - d1);
348 isl_seq_cpy(prod->row[n + i] + off + col1 + d1,
349 mat2->row[r2 + i] + off + d2, col2 - d2);
352 return prod;
355 /* Update the row or column index of a variable that corresponds
356 * to a variable in the first input tableau.
358 static void update_index1(struct isl_tab_var *var,
359 unsigned r1, unsigned r2, unsigned d1, unsigned d2)
361 if (var->index == -1)
362 return;
363 if (var->is_row && var->index >= r1)
364 var->index += r2;
365 if (!var->is_row && var->index >= d1)
366 var->index += d2;
369 /* Update the row or column index of a variable that corresponds
370 * to a variable in the second input tableau.
372 static void update_index2(struct isl_tab_var *var,
373 unsigned row1, unsigned col1,
374 unsigned r1, unsigned r2, unsigned d1, unsigned d2)
376 if (var->index == -1)
377 return;
378 if (var->is_row) {
379 if (var->index < r2)
380 var->index += r1;
381 else
382 var->index += row1;
383 } else {
384 if (var->index < d2)
385 var->index += d1;
386 else
387 var->index += col1;
391 /* Create a tableau that represents the Cartesian product of the sets
392 * represented by tableaus tab1 and tab2.
393 * The order of the rows in the product is
394 * - redundant rows of tab1
395 * - redundant rows of tab2
396 * - non-redundant rows of tab1
397 * - non-redundant rows of tab2
398 * The order of the columns is
399 * - denominator
400 * - constant term
401 * - coefficient of big parameter, if any
402 * - dead columns of tab1
403 * - dead columns of tab2
404 * - live columns of tab1
405 * - live columns of tab2
406 * The order of the variables and the constraints is a concatenation
407 * of order in the two input tableaus.
409 struct isl_tab *isl_tab_product(struct isl_tab *tab1, struct isl_tab *tab2)
411 int i;
412 struct isl_tab *prod;
413 unsigned off;
414 unsigned r1, r2, d1, d2;
416 if (!tab1 || !tab2)
417 return NULL;
419 isl_assert(tab1->mat->ctx, tab1->M == tab2->M, return NULL);
420 isl_assert(tab1->mat->ctx, tab1->rational == tab2->rational, return NULL);
421 isl_assert(tab1->mat->ctx, tab1->cone == tab2->cone, return NULL);
422 isl_assert(tab1->mat->ctx, !tab1->row_sign, return NULL);
423 isl_assert(tab1->mat->ctx, !tab2->row_sign, return NULL);
424 isl_assert(tab1->mat->ctx, tab1->n_param == 0, return NULL);
425 isl_assert(tab1->mat->ctx, tab2->n_param == 0, return NULL);
426 isl_assert(tab1->mat->ctx, tab1->n_div == 0, return NULL);
427 isl_assert(tab1->mat->ctx, tab2->n_div == 0, return NULL);
429 off = 2 + tab1->M;
430 r1 = tab1->n_redundant;
431 r2 = tab2->n_redundant;
432 d1 = tab1->n_dead;
433 d2 = tab2->n_dead;
434 prod = isl_calloc_type(tab1->mat->ctx, struct isl_tab);
435 if (!prod)
436 return NULL;
437 prod->mat = tab_mat_product(tab1->mat, tab2->mat,
438 tab1->n_row, tab2->n_row,
439 tab1->n_col, tab2->n_col, off, r1, r2, d1, d2);
440 if (!prod->mat)
441 goto error;
442 prod->var = isl_alloc_array(tab1->mat->ctx, struct isl_tab_var,
443 tab1->max_var + tab2->max_var);
444 if (!prod->var)
445 goto error;
446 for (i = 0; i < tab1->n_var; ++i) {
447 prod->var[i] = tab1->var[i];
448 update_index1(&prod->var[i], r1, r2, d1, d2);
450 for (i = 0; i < tab2->n_var; ++i) {
451 prod->var[tab1->n_var + i] = tab2->var[i];
452 update_index2(&prod->var[tab1->n_var + i],
453 tab1->n_row, tab1->n_col,
454 r1, r2, d1, d2);
456 prod->con = isl_alloc_array(tab1->mat->ctx, struct isl_tab_var,
457 tab1->max_con + tab2->max_con);
458 if (!prod->con)
459 goto error;
460 for (i = 0; i < tab1->n_con; ++i) {
461 prod->con[i] = tab1->con[i];
462 update_index1(&prod->con[i], r1, r2, d1, d2);
464 for (i = 0; i < tab2->n_con; ++i) {
465 prod->con[tab1->n_con + i] = tab2->con[i];
466 update_index2(&prod->con[tab1->n_con + i],
467 tab1->n_row, tab1->n_col,
468 r1, r2, d1, d2);
470 prod->col_var = isl_alloc_array(tab1->mat->ctx, int,
471 tab1->n_col + tab2->n_col);
472 if (!prod->col_var)
473 goto error;
474 for (i = 0; i < tab1->n_col; ++i) {
475 int pos = i < d1 ? i : i + d2;
476 prod->col_var[pos] = tab1->col_var[i];
478 for (i = 0; i < tab2->n_col; ++i) {
479 int pos = i < d2 ? d1 + i : tab1->n_col + i;
480 int t = tab2->col_var[i];
481 if (t >= 0)
482 t += tab1->n_var;
483 else
484 t -= tab1->n_con;
485 prod->col_var[pos] = t;
487 prod->row_var = isl_alloc_array(tab1->mat->ctx, int,
488 tab1->mat->n_row + tab2->mat->n_row);
489 if (!prod->row_var)
490 goto error;
491 for (i = 0; i < tab1->n_row; ++i) {
492 int pos = i < r1 ? i : i + r2;
493 prod->row_var[pos] = tab1->row_var[i];
495 for (i = 0; i < tab2->n_row; ++i) {
496 int pos = i < r2 ? r1 + i : tab1->n_row + i;
497 int t = tab2->row_var[i];
498 if (t >= 0)
499 t += tab1->n_var;
500 else
501 t -= tab1->n_con;
502 prod->row_var[pos] = t;
504 prod->samples = NULL;
505 prod->sample_index = NULL;
506 prod->n_row = tab1->n_row + tab2->n_row;
507 prod->n_con = tab1->n_con + tab2->n_con;
508 prod->n_eq = 0;
509 prod->max_con = tab1->max_con + tab2->max_con;
510 prod->n_col = tab1->n_col + tab2->n_col;
511 prod->n_var = tab1->n_var + tab2->n_var;
512 prod->max_var = tab1->max_var + tab2->max_var;
513 prod->n_param = 0;
514 prod->n_div = 0;
515 prod->n_dead = tab1->n_dead + tab2->n_dead;
516 prod->n_redundant = tab1->n_redundant + tab2->n_redundant;
517 prod->rational = tab1->rational;
518 prod->empty = tab1->empty || tab2->empty;
519 prod->need_undo = 0;
520 prod->in_undo = 0;
521 prod->M = tab1->M;
522 prod->cone = tab1->cone;
523 prod->bottom.type = isl_tab_undo_bottom;
524 prod->bottom.next = NULL;
525 prod->top = &prod->bottom;
527 prod->n_zero = 0;
528 prod->n_unbounded = 0;
529 prod->basis = NULL;
531 return prod;
532 error:
533 isl_tab_free(prod);
534 return NULL;
537 static struct isl_tab_var *var_from_index(struct isl_tab *tab, int i)
539 if (i >= 0)
540 return &tab->var[i];
541 else
542 return &tab->con[~i];
545 struct isl_tab_var *isl_tab_var_from_row(struct isl_tab *tab, int i)
547 return var_from_index(tab, tab->row_var[i]);
550 static struct isl_tab_var *var_from_col(struct isl_tab *tab, int i)
552 return var_from_index(tab, tab->col_var[i]);
555 /* Check if there are any upper bounds on column variable "var",
556 * i.e., non-negative rows where var appears with a negative coefficient.
557 * Return 1 if there are no such bounds.
559 static int max_is_manifestly_unbounded(struct isl_tab *tab,
560 struct isl_tab_var *var)
562 int i;
563 unsigned off = 2 + tab->M;
565 if (var->is_row)
566 return 0;
567 for (i = tab->n_redundant; i < tab->n_row; ++i) {
568 if (!isl_int_is_neg(tab->mat->row[i][off + var->index]))
569 continue;
570 if (isl_tab_var_from_row(tab, i)->is_nonneg)
571 return 0;
573 return 1;
576 /* Check if there are any lower bounds on column variable "var",
577 * i.e., non-negative rows where var appears with a positive coefficient.
578 * Return 1 if there are no such bounds.
580 static int min_is_manifestly_unbounded(struct isl_tab *tab,
581 struct isl_tab_var *var)
583 int i;
584 unsigned off = 2 + tab->M;
586 if (var->is_row)
587 return 0;
588 for (i = tab->n_redundant; i < tab->n_row; ++i) {
589 if (!isl_int_is_pos(tab->mat->row[i][off + var->index]))
590 continue;
591 if (isl_tab_var_from_row(tab, i)->is_nonneg)
592 return 0;
594 return 1;
597 static int row_cmp(struct isl_tab *tab, int r1, int r2, int c, isl_int t)
599 unsigned off = 2 + tab->M;
601 if (tab->M) {
602 int s;
603 isl_int_mul(t, tab->mat->row[r1][2], tab->mat->row[r2][off+c]);
604 isl_int_submul(t, tab->mat->row[r2][2], tab->mat->row[r1][off+c]);
605 s = isl_int_sgn(t);
606 if (s)
607 return s;
609 isl_int_mul(t, tab->mat->row[r1][1], tab->mat->row[r2][off + c]);
610 isl_int_submul(t, tab->mat->row[r2][1], tab->mat->row[r1][off + c]);
611 return isl_int_sgn(t);
614 /* Given the index of a column "c", return the index of a row
615 * that can be used to pivot the column in, with either an increase
616 * (sgn > 0) or a decrease (sgn < 0) of the corresponding variable.
617 * If "var" is not NULL, then the row returned will be different from
618 * the one associated with "var".
620 * Each row in the tableau is of the form
622 * x_r = a_r0 + \sum_i a_ri x_i
624 * Only rows with x_r >= 0 and with the sign of a_ri opposite to "sgn"
625 * impose any limit on the increase or decrease in the value of x_c
626 * and this bound is equal to a_r0 / |a_rc|. We are therefore looking
627 * for the row with the smallest (most stringent) such bound.
628 * Note that the common denominator of each row drops out of the fraction.
629 * To check if row j has a smaller bound than row r, i.e.,
630 * a_j0 / |a_jc| < a_r0 / |a_rc| or a_j0 |a_rc| < a_r0 |a_jc|,
631 * we check if -sign(a_jc) (a_j0 a_rc - a_r0 a_jc) < 0,
632 * where -sign(a_jc) is equal to "sgn".
634 static int pivot_row(struct isl_tab *tab,
635 struct isl_tab_var *var, int sgn, int c)
637 int j, r, tsgn;
638 isl_int t;
639 unsigned off = 2 + tab->M;
641 isl_int_init(t);
642 r = -1;
643 for (j = tab->n_redundant; j < tab->n_row; ++j) {
644 if (var && j == var->index)
645 continue;
646 if (!isl_tab_var_from_row(tab, j)->is_nonneg)
647 continue;
648 if (sgn * isl_int_sgn(tab->mat->row[j][off + c]) >= 0)
649 continue;
650 if (r < 0) {
651 r = j;
652 continue;
654 tsgn = sgn * row_cmp(tab, r, j, c, t);
655 if (tsgn < 0 || (tsgn == 0 &&
656 tab->row_var[j] < tab->row_var[r]))
657 r = j;
659 isl_int_clear(t);
660 return r;
663 /* Find a pivot (row and col) that will increase (sgn > 0) or decrease
664 * (sgn < 0) the value of row variable var.
665 * If not NULL, then skip_var is a row variable that should be ignored
666 * while looking for a pivot row. It is usually equal to var.
668 * As the given row in the tableau is of the form
670 * x_r = a_r0 + \sum_i a_ri x_i
672 * we need to find a column such that the sign of a_ri is equal to "sgn"
673 * (such that an increase in x_i will have the desired effect) or a
674 * column with a variable that may attain negative values.
675 * If a_ri is positive, then we need to move x_i in the same direction
676 * to obtain the desired effect. Otherwise, x_i has to move in the
677 * opposite direction.
679 static void find_pivot(struct isl_tab *tab,
680 struct isl_tab_var *var, struct isl_tab_var *skip_var,
681 int sgn, int *row, int *col)
683 int j, r, c;
684 isl_int *tr;
686 *row = *col = -1;
688 isl_assert(tab->mat->ctx, var->is_row, return);
689 tr = tab->mat->row[var->index] + 2 + tab->M;
691 c = -1;
692 for (j = tab->n_dead; j < tab->n_col; ++j) {
693 if (isl_int_is_zero(tr[j]))
694 continue;
695 if (isl_int_sgn(tr[j]) != sgn &&
696 var_from_col(tab, j)->is_nonneg)
697 continue;
698 if (c < 0 || tab->col_var[j] < tab->col_var[c])
699 c = j;
701 if (c < 0)
702 return;
704 sgn *= isl_int_sgn(tr[c]);
705 r = pivot_row(tab, skip_var, sgn, c);
706 *row = r < 0 ? var->index : r;
707 *col = c;
710 /* Return 1 if row "row" represents an obviously redundant inequality.
711 * This means
712 * - it represents an inequality or a variable
713 * - that is the sum of a non-negative sample value and a positive
714 * combination of zero or more non-negative constraints.
716 int isl_tab_row_is_redundant(struct isl_tab *tab, int row)
718 int i;
719 unsigned off = 2 + tab->M;
721 if (tab->row_var[row] < 0 && !isl_tab_var_from_row(tab, row)->is_nonneg)
722 return 0;
724 if (isl_int_is_neg(tab->mat->row[row][1]))
725 return 0;
726 if (tab->M && isl_int_is_neg(tab->mat->row[row][2]))
727 return 0;
729 for (i = tab->n_dead; i < tab->n_col; ++i) {
730 if (isl_int_is_zero(tab->mat->row[row][off + i]))
731 continue;
732 if (tab->col_var[i] >= 0)
733 return 0;
734 if (isl_int_is_neg(tab->mat->row[row][off + i]))
735 return 0;
736 if (!var_from_col(tab, i)->is_nonneg)
737 return 0;
739 return 1;
742 static void swap_rows(struct isl_tab *tab, int row1, int row2)
744 int t;
745 enum isl_tab_row_sign s;
747 t = tab->row_var[row1];
748 tab->row_var[row1] = tab->row_var[row2];
749 tab->row_var[row2] = t;
750 isl_tab_var_from_row(tab, row1)->index = row1;
751 isl_tab_var_from_row(tab, row2)->index = row2;
752 tab->mat = isl_mat_swap_rows(tab->mat, row1, row2);
754 if (!tab->row_sign)
755 return;
756 s = tab->row_sign[row1];
757 tab->row_sign[row1] = tab->row_sign[row2];
758 tab->row_sign[row2] = s;
761 static int push_union(struct isl_tab *tab,
762 enum isl_tab_undo_type type, union isl_tab_undo_val u) WARN_UNUSED;
763 static int push_union(struct isl_tab *tab,
764 enum isl_tab_undo_type type, union isl_tab_undo_val u)
766 struct isl_tab_undo *undo;
768 if (!tab->need_undo)
769 return 0;
771 undo = isl_alloc_type(tab->mat->ctx, struct isl_tab_undo);
772 if (!undo)
773 return -1;
774 undo->type = type;
775 undo->u = u;
776 undo->next = tab->top;
777 tab->top = undo;
779 return 0;
782 int isl_tab_push_var(struct isl_tab *tab,
783 enum isl_tab_undo_type type, struct isl_tab_var *var)
785 union isl_tab_undo_val u;
786 if (var->is_row)
787 u.var_index = tab->row_var[var->index];
788 else
789 u.var_index = tab->col_var[var->index];
790 return push_union(tab, type, u);
793 int isl_tab_push(struct isl_tab *tab, enum isl_tab_undo_type type)
795 union isl_tab_undo_val u = { 0 };
796 return push_union(tab, type, u);
799 /* Push a record on the undo stack describing the current basic
800 * variables, so that the this state can be restored during rollback.
802 int isl_tab_push_basis(struct isl_tab *tab)
804 int i;
805 union isl_tab_undo_val u;
807 u.col_var = isl_alloc_array(tab->mat->ctx, int, tab->n_col);
808 if (!u.col_var)
809 return -1;
810 for (i = 0; i < tab->n_col; ++i)
811 u.col_var[i] = tab->col_var[i];
812 return push_union(tab, isl_tab_undo_saved_basis, u);
815 int isl_tab_push_callback(struct isl_tab *tab, struct isl_tab_callback *callback)
817 union isl_tab_undo_val u;
818 u.callback = callback;
819 return push_union(tab, isl_tab_undo_callback, u);
822 struct isl_tab *isl_tab_init_samples(struct isl_tab *tab)
824 if (!tab)
825 return NULL;
827 tab->n_sample = 0;
828 tab->n_outside = 0;
829 tab->samples = isl_mat_alloc(tab->mat->ctx, 1, 1 + tab->n_var);
830 if (!tab->samples)
831 goto error;
832 tab->sample_index = isl_alloc_array(tab->mat->ctx, int, 1);
833 if (!tab->sample_index)
834 goto error;
835 return tab;
836 error:
837 isl_tab_free(tab);
838 return NULL;
841 struct isl_tab *isl_tab_add_sample(struct isl_tab *tab,
842 __isl_take isl_vec *sample)
844 if (!tab || !sample)
845 goto error;
847 if (tab->n_sample + 1 > tab->samples->n_row) {
848 int *t = isl_realloc_array(tab->mat->ctx,
849 tab->sample_index, int, tab->n_sample + 1);
850 if (!t)
851 goto error;
852 tab->sample_index = t;
855 tab->samples = isl_mat_extend(tab->samples,
856 tab->n_sample + 1, tab->samples->n_col);
857 if (!tab->samples)
858 goto error;
860 isl_seq_cpy(tab->samples->row[tab->n_sample], sample->el, sample->size);
861 isl_vec_free(sample);
862 tab->sample_index[tab->n_sample] = tab->n_sample;
863 tab->n_sample++;
865 return tab;
866 error:
867 isl_vec_free(sample);
868 isl_tab_free(tab);
869 return NULL;
872 struct isl_tab *isl_tab_drop_sample(struct isl_tab *tab, int s)
874 if (s != tab->n_outside) {
875 int t = tab->sample_index[tab->n_outside];
876 tab->sample_index[tab->n_outside] = tab->sample_index[s];
877 tab->sample_index[s] = t;
878 isl_mat_swap_rows(tab->samples, tab->n_outside, s);
880 tab->n_outside++;
881 if (isl_tab_push(tab, isl_tab_undo_drop_sample) < 0) {
882 isl_tab_free(tab);
883 return NULL;
886 return tab;
889 /* Record the current number of samples so that we can remove newer
890 * samples during a rollback.
892 int isl_tab_save_samples(struct isl_tab *tab)
894 union isl_tab_undo_val u;
896 if (!tab)
897 return -1;
899 u.n = tab->n_sample;
900 return push_union(tab, isl_tab_undo_saved_samples, u);
903 /* Mark row with index "row" as being redundant.
904 * If we may need to undo the operation or if the row represents
905 * a variable of the original problem, the row is kept,
906 * but no longer considered when looking for a pivot row.
907 * Otherwise, the row is simply removed.
909 * The row may be interchanged with some other row. If it
910 * is interchanged with a later row, return 1. Otherwise return 0.
911 * If the rows are checked in order in the calling function,
912 * then a return value of 1 means that the row with the given
913 * row number may now contain a different row that hasn't been checked yet.
915 int isl_tab_mark_redundant(struct isl_tab *tab, int row)
917 struct isl_tab_var *var = isl_tab_var_from_row(tab, row);
918 var->is_redundant = 1;
919 isl_assert(tab->mat->ctx, row >= tab->n_redundant, return -1);
920 if (tab->need_undo || tab->row_var[row] >= 0) {
921 if (tab->row_var[row] >= 0 && !var->is_nonneg) {
922 var->is_nonneg = 1;
923 if (isl_tab_push_var(tab, isl_tab_undo_nonneg, var) < 0)
924 return -1;
926 if (row != tab->n_redundant)
927 swap_rows(tab, row, tab->n_redundant);
928 tab->n_redundant++;
929 return isl_tab_push_var(tab, isl_tab_undo_redundant, var);
930 } else {
931 if (row != tab->n_row - 1)
932 swap_rows(tab, row, tab->n_row - 1);
933 isl_tab_var_from_row(tab, tab->n_row - 1)->index = -1;
934 tab->n_row--;
935 return 1;
939 int isl_tab_mark_empty(struct isl_tab *tab)
941 if (!tab)
942 return -1;
943 if (!tab->empty && tab->need_undo)
944 if (isl_tab_push(tab, isl_tab_undo_empty) < 0)
945 return -1;
946 tab->empty = 1;
947 return 0;
950 int isl_tab_freeze_constraint(struct isl_tab *tab, int con)
952 struct isl_tab_var *var;
954 if (!tab)
955 return -1;
957 var = &tab->con[con];
958 if (var->frozen)
959 return 0;
960 if (var->index < 0)
961 return 0;
962 var->frozen = 1;
964 if (tab->need_undo)
965 return isl_tab_push_var(tab, isl_tab_undo_freeze, var);
967 return 0;
970 /* Update the rows signs after a pivot of "row" and "col", with "row_sgn"
971 * the original sign of the pivot element.
972 * We only keep track of row signs during PILP solving and in this case
973 * we only pivot a row with negative sign (meaning the value is always
974 * non-positive) using a positive pivot element.
976 * For each row j, the new value of the parametric constant is equal to
978 * a_j0 - a_jc a_r0/a_rc
980 * where a_j0 is the original parametric constant, a_rc is the pivot element,
981 * a_r0 is the parametric constant of the pivot row and a_jc is the
982 * pivot column entry of the row j.
983 * Since a_r0 is non-positive and a_rc is positive, the sign of row j
984 * remains the same if a_jc has the same sign as the row j or if
985 * a_jc is zero. In all other cases, we reset the sign to "unknown".
987 static void update_row_sign(struct isl_tab *tab, int row, int col, int row_sgn)
989 int i;
990 struct isl_mat *mat = tab->mat;
991 unsigned off = 2 + tab->M;
993 if (!tab->row_sign)
994 return;
996 if (tab->row_sign[row] == 0)
997 return;
998 isl_assert(mat->ctx, row_sgn > 0, return);
999 isl_assert(mat->ctx, tab->row_sign[row] == isl_tab_row_neg, return);
1000 tab->row_sign[row] = isl_tab_row_pos;
1001 for (i = 0; i < tab->n_row; ++i) {
1002 int s;
1003 if (i == row)
1004 continue;
1005 s = isl_int_sgn(mat->row[i][off + col]);
1006 if (!s)
1007 continue;
1008 if (!tab->row_sign[i])
1009 continue;
1010 if (s < 0 && tab->row_sign[i] == isl_tab_row_neg)
1011 continue;
1012 if (s > 0 && tab->row_sign[i] == isl_tab_row_pos)
1013 continue;
1014 tab->row_sign[i] = isl_tab_row_unknown;
1018 /* Given a row number "row" and a column number "col", pivot the tableau
1019 * such that the associated variables are interchanged.
1020 * The given row in the tableau expresses
1022 * x_r = a_r0 + \sum_i a_ri x_i
1024 * or
1026 * x_c = 1/a_rc x_r - a_r0/a_rc + sum_{i \ne r} -a_ri/a_rc
1028 * Substituting this equality into the other rows
1030 * x_j = a_j0 + \sum_i a_ji x_i
1032 * with a_jc \ne 0, we obtain
1034 * 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
1036 * The tableau
1038 * n_rc/d_r n_ri/d_r
1039 * n_jc/d_j n_ji/d_j
1041 * where i is any other column and j is any other row,
1042 * is therefore transformed into
1044 * s(n_rc)d_r/|n_rc| -s(n_rc)n_ri/|n_rc|
1045 * 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)
1047 * The transformation is performed along the following steps
1049 * d_r/n_rc n_ri/n_rc
1050 * n_jc/d_j n_ji/d_j
1052 * s(n_rc)d_r/|n_rc| -s(n_rc)n_ri/|n_rc|
1053 * n_jc/d_j n_ji/d_j
1055 * s(n_rc)d_r/|n_rc| -s(n_rc)n_ri/|n_rc|
1056 * n_jc/(|n_rc| d_j) n_ji/(|n_rc| d_j)
1058 * s(n_rc)d_r/|n_rc| -s(n_rc)n_ri/|n_rc|
1059 * n_jc/(|n_rc| d_j) (n_ji |n_rc|)/(|n_rc| d_j)
1061 * s(n_rc)d_r/|n_rc| -s(n_rc)n_ri/|n_rc|
1062 * n_jc/(|n_rc| d_j) (n_ji |n_rc| - s(n_rc)n_jc n_ri)/(|n_rc| d_j)
1064 * s(n_rc)d_r/|n_rc| -s(n_rc)n_ri/|n_rc|
1065 * 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 int isl_tab_pivot(struct isl_tab *tab, int row, int col)
1070 int i, j;
1071 int sgn;
1072 int t;
1073 struct isl_mat *mat = tab->mat;
1074 struct isl_tab_var *var;
1075 unsigned off = 2 + tab->M;
1077 isl_int_swap(mat->row[row][0], mat->row[row][off + col]);
1078 sgn = isl_int_sgn(mat->row[row][0]);
1079 if (sgn < 0) {
1080 isl_int_neg(mat->row[row][0], mat->row[row][0]);
1081 isl_int_neg(mat->row[row][off + col], mat->row[row][off + col]);
1082 } else
1083 for (j = 0; j < off - 1 + tab->n_col; ++j) {
1084 if (j == off - 1 + col)
1085 continue;
1086 isl_int_neg(mat->row[row][1 + j], mat->row[row][1 + j]);
1088 if (!isl_int_is_one(mat->row[row][0]))
1089 isl_seq_normalize(mat->ctx, mat->row[row], off + tab->n_col);
1090 for (i = 0; i < tab->n_row; ++i) {
1091 if (i == row)
1092 continue;
1093 if (isl_int_is_zero(mat->row[i][off + col]))
1094 continue;
1095 isl_int_mul(mat->row[i][0], mat->row[i][0], mat->row[row][0]);
1096 for (j = 0; j < off - 1 + tab->n_col; ++j) {
1097 if (j == off - 1 + col)
1098 continue;
1099 isl_int_mul(mat->row[i][1 + j],
1100 mat->row[i][1 + j], mat->row[row][0]);
1101 isl_int_addmul(mat->row[i][1 + j],
1102 mat->row[i][off + col], mat->row[row][1 + j]);
1104 isl_int_mul(mat->row[i][off + col],
1105 mat->row[i][off + col], mat->row[row][off + col]);
1106 if (!isl_int_is_one(mat->row[i][0]))
1107 isl_seq_normalize(mat->ctx, mat->row[i], off + tab->n_col);
1109 t = tab->row_var[row];
1110 tab->row_var[row] = tab->col_var[col];
1111 tab->col_var[col] = t;
1112 var = isl_tab_var_from_row(tab, row);
1113 var->is_row = 1;
1114 var->index = row;
1115 var = var_from_col(tab, col);
1116 var->is_row = 0;
1117 var->index = col;
1118 update_row_sign(tab, row, col, sgn);
1119 if (tab->in_undo)
1120 return 0;
1121 for (i = tab->n_redundant; i < tab->n_row; ++i) {
1122 if (isl_int_is_zero(mat->row[i][off + col]))
1123 continue;
1124 if (!isl_tab_var_from_row(tab, i)->frozen &&
1125 isl_tab_row_is_redundant(tab, i)) {
1126 int redo = isl_tab_mark_redundant(tab, i);
1127 if (redo < 0)
1128 return -1;
1129 if (redo)
1130 --i;
1133 return 0;
1136 /* If "var" represents a column variable, then pivot is up (sgn > 0)
1137 * or down (sgn < 0) to a row. The variable is assumed not to be
1138 * unbounded in the specified direction.
1139 * If sgn = 0, then the variable is unbounded in both directions,
1140 * and we pivot with any row we can find.
1142 static int to_row(struct isl_tab *tab, struct isl_tab_var *var, int sign) WARN_UNUSED;
1143 static int to_row(struct isl_tab *tab, struct isl_tab_var *var, int sign)
1145 int r;
1146 unsigned off = 2 + tab->M;
1148 if (var->is_row)
1149 return 0;
1151 if (sign == 0) {
1152 for (r = tab->n_redundant; r < tab->n_row; ++r)
1153 if (!isl_int_is_zero(tab->mat->row[r][off+var->index]))
1154 break;
1155 isl_assert(tab->mat->ctx, r < tab->n_row, return -1);
1156 } else {
1157 r = pivot_row(tab, NULL, sign, var->index);
1158 isl_assert(tab->mat->ctx, r >= 0, return -1);
1161 return isl_tab_pivot(tab, r, var->index);
1164 static void check_table(struct isl_tab *tab)
1166 int i;
1168 if (tab->empty)
1169 return;
1170 for (i = tab->n_redundant; i < tab->n_row; ++i) {
1171 struct isl_tab_var *var;
1172 var = isl_tab_var_from_row(tab, i);
1173 if (!var->is_nonneg)
1174 continue;
1175 if (tab->M) {
1176 assert(!isl_int_is_neg(tab->mat->row[i][2]));
1177 if (isl_int_is_pos(tab->mat->row[i][2]))
1178 continue;
1180 assert(!isl_int_is_neg(tab->mat->row[i][1]));
1184 /* Return the sign of the maximal value of "var".
1185 * If the sign is not negative, then on return from this function,
1186 * the sample value will also be non-negative.
1188 * If "var" is manifestly unbounded wrt positive values, we are done.
1189 * Otherwise, we pivot the variable up to a row if needed
1190 * Then we continue pivoting down until either
1191 * - no more down pivots can be performed
1192 * - the sample value is positive
1193 * - the variable is pivoted into a manifestly unbounded column
1195 static int sign_of_max(struct isl_tab *tab, struct isl_tab_var *var)
1197 int row, col;
1199 if (max_is_manifestly_unbounded(tab, var))
1200 return 1;
1201 if (to_row(tab, var, 1) < 0)
1202 return -2;
1203 while (!isl_int_is_pos(tab->mat->row[var->index][1])) {
1204 find_pivot(tab, var, var, 1, &row, &col);
1205 if (row == -1)
1206 return isl_int_sgn(tab->mat->row[var->index][1]);
1207 if (isl_tab_pivot(tab, row, col) < 0)
1208 return -2;
1209 if (!var->is_row) /* manifestly unbounded */
1210 return 1;
1212 return 1;
1215 static int row_is_neg(struct isl_tab *tab, int row)
1217 if (!tab->M)
1218 return isl_int_is_neg(tab->mat->row[row][1]);
1219 if (isl_int_is_pos(tab->mat->row[row][2]))
1220 return 0;
1221 if (isl_int_is_neg(tab->mat->row[row][2]))
1222 return 1;
1223 return isl_int_is_neg(tab->mat->row[row][1]);
1226 static int row_sgn(struct isl_tab *tab, int row)
1228 if (!tab->M)
1229 return isl_int_sgn(tab->mat->row[row][1]);
1230 if (!isl_int_is_zero(tab->mat->row[row][2]))
1231 return isl_int_sgn(tab->mat->row[row][2]);
1232 else
1233 return isl_int_sgn(tab->mat->row[row][1]);
1236 /* Perform pivots until the row variable "var" has a non-negative
1237 * sample value or until no more upward pivots can be performed.
1238 * Return the sign of the sample value after the pivots have been
1239 * performed.
1241 static int restore_row(struct isl_tab *tab, struct isl_tab_var *var)
1243 int row, col;
1245 while (row_is_neg(tab, var->index)) {
1246 find_pivot(tab, var, var, 1, &row, &col);
1247 if (row == -1)
1248 break;
1249 if (isl_tab_pivot(tab, row, col) < 0)
1250 return -2;
1251 if (!var->is_row) /* manifestly unbounded */
1252 return 1;
1254 return row_sgn(tab, var->index);
1257 /* Perform pivots until we are sure that the row variable "var"
1258 * can attain non-negative values. After return from this
1259 * function, "var" is still a row variable, but its sample
1260 * value may not be non-negative, even if the function returns 1.
1262 static int at_least_zero(struct isl_tab *tab, struct isl_tab_var *var)
1264 int row, col;
1266 while (isl_int_is_neg(tab->mat->row[var->index][1])) {
1267 find_pivot(tab, var, var, 1, &row, &col);
1268 if (row == -1)
1269 break;
1270 if (row == var->index) /* manifestly unbounded */
1271 return 1;
1272 if (isl_tab_pivot(tab, row, col) < 0)
1273 return -1;
1275 return !isl_int_is_neg(tab->mat->row[var->index][1]);
1278 /* Return a negative value if "var" can attain negative values.
1279 * Return a non-negative value otherwise.
1281 * If "var" is manifestly unbounded wrt negative values, we are done.
1282 * Otherwise, if var is in a column, we can pivot it down to a row.
1283 * Then we continue pivoting down until either
1284 * - the pivot would result in a manifestly unbounded column
1285 * => we don't perform the pivot, but simply return -1
1286 * - no more down pivots can be performed
1287 * - the sample value is negative
1288 * If the sample value becomes negative and the variable is supposed
1289 * to be nonnegative, then we undo the last pivot.
1290 * However, if the last pivot has made the pivoting variable
1291 * obviously redundant, then it may have moved to another row.
1292 * In that case we look for upward pivots until we reach a non-negative
1293 * value again.
1295 static int sign_of_min(struct isl_tab *tab, struct isl_tab_var *var)
1297 int row, col;
1298 struct isl_tab_var *pivot_var = NULL;
1300 if (min_is_manifestly_unbounded(tab, var))
1301 return -1;
1302 if (!var->is_row) {
1303 col = var->index;
1304 row = pivot_row(tab, NULL, -1, col);
1305 pivot_var = var_from_col(tab, col);
1306 if (isl_tab_pivot(tab, row, col) < 0)
1307 return -2;
1308 if (var->is_redundant)
1309 return 0;
1310 if (isl_int_is_neg(tab->mat->row[var->index][1])) {
1311 if (var->is_nonneg) {
1312 if (!pivot_var->is_redundant &&
1313 pivot_var->index == row) {
1314 if (isl_tab_pivot(tab, row, col) < 0)
1315 return -2;
1316 } else
1317 if (restore_row(tab, var) < -1)
1318 return -2;
1320 return -1;
1323 if (var->is_redundant)
1324 return 0;
1325 while (!isl_int_is_neg(tab->mat->row[var->index][1])) {
1326 find_pivot(tab, var, var, -1, &row, &col);
1327 if (row == var->index)
1328 return -1;
1329 if (row == -1)
1330 return isl_int_sgn(tab->mat->row[var->index][1]);
1331 pivot_var = var_from_col(tab, col);
1332 if (isl_tab_pivot(tab, row, col) < 0)
1333 return -2;
1334 if (var->is_redundant)
1335 return 0;
1337 if (pivot_var && var->is_nonneg) {
1338 /* pivot back to non-negative value */
1339 if (!pivot_var->is_redundant && pivot_var->index == row) {
1340 if (isl_tab_pivot(tab, row, col) < 0)
1341 return -2;
1342 } else
1343 if (restore_row(tab, var) < -1)
1344 return -2;
1346 return -1;
1349 static int row_at_most_neg_one(struct isl_tab *tab, int row)
1351 if (tab->M) {
1352 if (isl_int_is_pos(tab->mat->row[row][2]))
1353 return 0;
1354 if (isl_int_is_neg(tab->mat->row[row][2]))
1355 return 1;
1357 return isl_int_is_neg(tab->mat->row[row][1]) &&
1358 isl_int_abs_ge(tab->mat->row[row][1],
1359 tab->mat->row[row][0]);
1362 /* Return 1 if "var" can attain values <= -1.
1363 * Return 0 otherwise.
1365 * The sample value of "var" is assumed to be non-negative when the
1366 * the function is called. If 1 is returned then the constraint
1367 * is not redundant and the sample value is made non-negative again before
1368 * the function returns.
1370 int isl_tab_min_at_most_neg_one(struct isl_tab *tab, struct isl_tab_var *var)
1372 int row, col;
1373 struct isl_tab_var *pivot_var;
1375 if (min_is_manifestly_unbounded(tab, var))
1376 return 1;
1377 if (!var->is_row) {
1378 col = var->index;
1379 row = pivot_row(tab, NULL, -1, col);
1380 pivot_var = var_from_col(tab, col);
1381 if (isl_tab_pivot(tab, row, col) < 0)
1382 return -1;
1383 if (var->is_redundant)
1384 return 0;
1385 if (row_at_most_neg_one(tab, var->index)) {
1386 if (var->is_nonneg) {
1387 if (!pivot_var->is_redundant &&
1388 pivot_var->index == row) {
1389 if (isl_tab_pivot(tab, row, col) < 0)
1390 return -1;
1391 } else
1392 if (restore_row(tab, var) < -1)
1393 return -1;
1395 return 1;
1398 if (var->is_redundant)
1399 return 0;
1400 do {
1401 find_pivot(tab, var, var, -1, &row, &col);
1402 if (row == var->index) {
1403 if (restore_row(tab, var) < -1)
1404 return -1;
1405 return 1;
1407 if (row == -1)
1408 return 0;
1409 pivot_var = var_from_col(tab, col);
1410 if (isl_tab_pivot(tab, row, col) < 0)
1411 return -1;
1412 if (var->is_redundant)
1413 return 0;
1414 } while (!row_at_most_neg_one(tab, var->index));
1415 if (var->is_nonneg) {
1416 /* pivot back to non-negative value */
1417 if (!pivot_var->is_redundant && pivot_var->index == row)
1418 if (isl_tab_pivot(tab, row, col) < 0)
1419 return -1;
1420 if (restore_row(tab, var) < -1)
1421 return -1;
1423 return 1;
1426 /* Return 1 if "var" can attain values >= 1.
1427 * Return 0 otherwise.
1429 static int at_least_one(struct isl_tab *tab, struct isl_tab_var *var)
1431 int row, col;
1432 isl_int *r;
1434 if (max_is_manifestly_unbounded(tab, var))
1435 return 1;
1436 if (to_row(tab, var, 1) < 0)
1437 return -1;
1438 r = tab->mat->row[var->index];
1439 while (isl_int_lt(r[1], r[0])) {
1440 find_pivot(tab, var, var, 1, &row, &col);
1441 if (row == -1)
1442 return isl_int_ge(r[1], r[0]);
1443 if (row == var->index) /* manifestly unbounded */
1444 return 1;
1445 if (isl_tab_pivot(tab, row, col) < 0)
1446 return -1;
1448 return 1;
1451 static void swap_cols(struct isl_tab *tab, int col1, int col2)
1453 int t;
1454 unsigned off = 2 + tab->M;
1455 t = tab->col_var[col1];
1456 tab->col_var[col1] = tab->col_var[col2];
1457 tab->col_var[col2] = t;
1458 var_from_col(tab, col1)->index = col1;
1459 var_from_col(tab, col2)->index = col2;
1460 tab->mat = isl_mat_swap_cols(tab->mat, off + col1, off + col2);
1463 /* Mark column with index "col" as representing a zero variable.
1464 * If we may need to undo the operation the column is kept,
1465 * but no longer considered.
1466 * Otherwise, the column is simply removed.
1468 * The column may be interchanged with some other column. If it
1469 * is interchanged with a later column, return 1. Otherwise return 0.
1470 * If the columns are checked in order in the calling function,
1471 * then a return value of 1 means that the column with the given
1472 * column number may now contain a different column that
1473 * hasn't been checked yet.
1475 int isl_tab_kill_col(struct isl_tab *tab, int col)
1477 var_from_col(tab, col)->is_zero = 1;
1478 if (tab->need_undo) {
1479 if (isl_tab_push_var(tab, isl_tab_undo_zero,
1480 var_from_col(tab, col)) < 0)
1481 return -1;
1482 if (col != tab->n_dead)
1483 swap_cols(tab, col, tab->n_dead);
1484 tab->n_dead++;
1485 return 0;
1486 } else {
1487 if (col != tab->n_col - 1)
1488 swap_cols(tab, col, tab->n_col - 1);
1489 var_from_col(tab, tab->n_col - 1)->index = -1;
1490 tab->n_col--;
1491 return 1;
1495 /* Row variable "var" is non-negative and cannot attain any values
1496 * larger than zero. This means that the coefficients of the unrestricted
1497 * column variables are zero and that the coefficients of the non-negative
1498 * column variables are zero or negative.
1499 * Each of the non-negative variables with a negative coefficient can
1500 * then also be written as the negative sum of non-negative variables
1501 * and must therefore also be zero.
1503 static int close_row(struct isl_tab *tab, struct isl_tab_var *var) WARN_UNUSED;
1504 static int close_row(struct isl_tab *tab, struct isl_tab_var *var)
1506 int j;
1507 struct isl_mat *mat = tab->mat;
1508 unsigned off = 2 + tab->M;
1510 isl_assert(tab->mat->ctx, var->is_nonneg, return -1);
1511 var->is_zero = 1;
1512 if (tab->need_undo)
1513 if (isl_tab_push_var(tab, isl_tab_undo_zero, var) < 0)
1514 return -1;
1515 for (j = tab->n_dead; j < tab->n_col; ++j) {
1516 if (isl_int_is_zero(mat->row[var->index][off + j]))
1517 continue;
1518 isl_assert(tab->mat->ctx,
1519 isl_int_is_neg(mat->row[var->index][off + j]), return -1);
1520 if (isl_tab_kill_col(tab, j))
1521 --j;
1523 if (isl_tab_mark_redundant(tab, var->index) < 0)
1524 return -1;
1525 return 0;
1528 /* Add a constraint to the tableau and allocate a row for it.
1529 * Return the index into the constraint array "con".
1531 int isl_tab_allocate_con(struct isl_tab *tab)
1533 int r;
1535 isl_assert(tab->mat->ctx, tab->n_row < tab->mat->n_row, return -1);
1536 isl_assert(tab->mat->ctx, tab->n_con < tab->max_con, return -1);
1538 r = tab->n_con;
1539 tab->con[r].index = tab->n_row;
1540 tab->con[r].is_row = 1;
1541 tab->con[r].is_nonneg = 0;
1542 tab->con[r].is_zero = 0;
1543 tab->con[r].is_redundant = 0;
1544 tab->con[r].frozen = 0;
1545 tab->con[r].negated = 0;
1546 tab->row_var[tab->n_row] = ~r;
1548 tab->n_row++;
1549 tab->n_con++;
1550 if (isl_tab_push_var(tab, isl_tab_undo_allocate, &tab->con[r]) < 0)
1551 return -1;
1553 return r;
1556 /* Add a variable to the tableau and allocate a column for it.
1557 * Return the index into the variable array "var".
1559 int isl_tab_allocate_var(struct isl_tab *tab)
1561 int r;
1562 int i;
1563 unsigned off = 2 + tab->M;
1565 isl_assert(tab->mat->ctx, tab->n_col < tab->mat->n_col, return -1);
1566 isl_assert(tab->mat->ctx, tab->n_var < tab->max_var, return -1);
1568 r = tab->n_var;
1569 tab->var[r].index = tab->n_col;
1570 tab->var[r].is_row = 0;
1571 tab->var[r].is_nonneg = 0;
1572 tab->var[r].is_zero = 0;
1573 tab->var[r].is_redundant = 0;
1574 tab->var[r].frozen = 0;
1575 tab->var[r].negated = 0;
1576 tab->col_var[tab->n_col] = r;
1578 for (i = 0; i < tab->n_row; ++i)
1579 isl_int_set_si(tab->mat->row[i][off + tab->n_col], 0);
1581 tab->n_var++;
1582 tab->n_col++;
1583 if (isl_tab_push_var(tab, isl_tab_undo_allocate, &tab->var[r]) < 0)
1584 return -1;
1586 return r;
1589 /* Add a row to the tableau. The row is given as an affine combination
1590 * of the original variables and needs to be expressed in terms of the
1591 * column variables.
1593 * We add each term in turn.
1594 * If r = n/d_r is the current sum and we need to add k x, then
1595 * if x is a column variable, we increase the numerator of
1596 * this column by k d_r
1597 * if x = f/d_x is a row variable, then the new representation of r is
1599 * n k f d_x/g n + d_r/g k f m/d_r n + m/d_g k f
1600 * --- + --- = ------------------- = -------------------
1601 * d_r d_r d_r d_x/g m
1603 * with g the gcd of d_r and d_x and m the lcm of d_r and d_x.
1605 int isl_tab_add_row(struct isl_tab *tab, isl_int *line)
1607 int i;
1608 int r;
1609 isl_int *row;
1610 isl_int a, b;
1611 unsigned off = 2 + tab->M;
1613 r = isl_tab_allocate_con(tab);
1614 if (r < 0)
1615 return -1;
1617 isl_int_init(a);
1618 isl_int_init(b);
1619 row = tab->mat->row[tab->con[r].index];
1620 isl_int_set_si(row[0], 1);
1621 isl_int_set(row[1], line[0]);
1622 isl_seq_clr(row + 2, tab->M + tab->n_col);
1623 for (i = 0; i < tab->n_var; ++i) {
1624 if (tab->var[i].is_zero)
1625 continue;
1626 if (tab->var[i].is_row) {
1627 isl_int_lcm(a,
1628 row[0], tab->mat->row[tab->var[i].index][0]);
1629 isl_int_swap(a, row[0]);
1630 isl_int_divexact(a, row[0], a);
1631 isl_int_divexact(b,
1632 row[0], tab->mat->row[tab->var[i].index][0]);
1633 isl_int_mul(b, b, line[1 + i]);
1634 isl_seq_combine(row + 1, a, row + 1,
1635 b, tab->mat->row[tab->var[i].index] + 1,
1636 1 + tab->M + tab->n_col);
1637 } else
1638 isl_int_addmul(row[off + tab->var[i].index],
1639 line[1 + i], row[0]);
1640 if (tab->M && i >= tab->n_param && i < tab->n_var - tab->n_div)
1641 isl_int_submul(row[2], line[1 + i], row[0]);
1643 isl_seq_normalize(tab->mat->ctx, row, off + tab->n_col);
1644 isl_int_clear(a);
1645 isl_int_clear(b);
1647 if (tab->row_sign)
1648 tab->row_sign[tab->con[r].index] = isl_tab_row_unknown;
1650 return r;
1653 static int drop_row(struct isl_tab *tab, int row)
1655 isl_assert(tab->mat->ctx, ~tab->row_var[row] == tab->n_con - 1, return -1);
1656 if (row != tab->n_row - 1)
1657 swap_rows(tab, row, tab->n_row - 1);
1658 tab->n_row--;
1659 tab->n_con--;
1660 return 0;
1663 static int drop_col(struct isl_tab *tab, int col)
1665 isl_assert(tab->mat->ctx, tab->col_var[col] == tab->n_var - 1, return -1);
1666 if (col != tab->n_col - 1)
1667 swap_cols(tab, col, tab->n_col - 1);
1668 tab->n_col--;
1669 tab->n_var--;
1670 return 0;
1673 /* Add inequality "ineq" and check if it conflicts with the
1674 * previously added constraints or if it is obviously redundant.
1676 int isl_tab_add_ineq(struct isl_tab *tab, isl_int *ineq)
1678 int r;
1679 int sgn;
1680 isl_int cst;
1682 if (!tab)
1683 return -1;
1684 if (tab->bmap) {
1685 struct isl_basic_map *bmap = tab->bmap;
1687 isl_assert(tab->mat->ctx, tab->n_eq == bmap->n_eq, return -1);
1688 isl_assert(tab->mat->ctx,
1689 tab->n_con == bmap->n_eq + bmap->n_ineq, return -1);
1690 tab->bmap = isl_basic_map_add_ineq(tab->bmap, ineq);
1691 if (isl_tab_push(tab, isl_tab_undo_bmap_ineq) < 0)
1692 return -1;
1693 if (!tab->bmap)
1694 return -1;
1696 if (tab->cone) {
1697 isl_int_init(cst);
1698 isl_int_swap(ineq[0], cst);
1700 r = isl_tab_add_row(tab, ineq);
1701 if (tab->cone) {
1702 isl_int_swap(ineq[0], cst);
1703 isl_int_clear(cst);
1705 if (r < 0)
1706 return -1;
1707 tab->con[r].is_nonneg = 1;
1708 if (isl_tab_push_var(tab, isl_tab_undo_nonneg, &tab->con[r]) < 0)
1709 return -1;
1710 if (isl_tab_row_is_redundant(tab, tab->con[r].index)) {
1711 if (isl_tab_mark_redundant(tab, tab->con[r].index) < 0)
1712 return -1;
1713 return 0;
1716 sgn = restore_row(tab, &tab->con[r]);
1717 if (sgn < -1)
1718 return -1;
1719 if (sgn < 0)
1720 return isl_tab_mark_empty(tab);
1721 if (tab->con[r].is_row && isl_tab_row_is_redundant(tab, tab->con[r].index))
1722 if (isl_tab_mark_redundant(tab, tab->con[r].index) < 0)
1723 return -1;
1724 return 0;
1727 /* Pivot a non-negative variable down until it reaches the value zero
1728 * and then pivot the variable into a column position.
1730 static int to_col(struct isl_tab *tab, struct isl_tab_var *var) WARN_UNUSED;
1731 static int to_col(struct isl_tab *tab, struct isl_tab_var *var)
1733 int i;
1734 int row, col;
1735 unsigned off = 2 + tab->M;
1737 if (!var->is_row)
1738 return 0;
1740 while (isl_int_is_pos(tab->mat->row[var->index][1])) {
1741 find_pivot(tab, var, NULL, -1, &row, &col);
1742 isl_assert(tab->mat->ctx, row != -1, return -1);
1743 if (isl_tab_pivot(tab, row, col) < 0)
1744 return -1;
1745 if (!var->is_row)
1746 return 0;
1749 for (i = tab->n_dead; i < tab->n_col; ++i)
1750 if (!isl_int_is_zero(tab->mat->row[var->index][off + i]))
1751 break;
1753 isl_assert(tab->mat->ctx, i < tab->n_col, return -1);
1754 if (isl_tab_pivot(tab, var->index, i) < 0)
1755 return -1;
1757 return 0;
1760 /* We assume Gaussian elimination has been performed on the equalities.
1761 * The equalities can therefore never conflict.
1762 * Adding the equalities is currently only really useful for a later call
1763 * to isl_tab_ineq_type.
1765 static struct isl_tab *add_eq(struct isl_tab *tab, isl_int *eq)
1767 int i;
1768 int r;
1770 if (!tab)
1771 return NULL;
1772 r = isl_tab_add_row(tab, eq);
1773 if (r < 0)
1774 goto error;
1776 r = tab->con[r].index;
1777 i = isl_seq_first_non_zero(tab->mat->row[r] + 2 + tab->M + tab->n_dead,
1778 tab->n_col - tab->n_dead);
1779 isl_assert(tab->mat->ctx, i >= 0, goto error);
1780 i += tab->n_dead;
1781 if (isl_tab_pivot(tab, r, i) < 0)
1782 goto error;
1783 if (isl_tab_kill_col(tab, i) < 0)
1784 goto error;
1785 tab->n_eq++;
1787 return tab;
1788 error:
1789 isl_tab_free(tab);
1790 return NULL;
1793 static int row_is_manifestly_zero(struct isl_tab *tab, int row)
1795 unsigned off = 2 + tab->M;
1797 if (!isl_int_is_zero(tab->mat->row[row][1]))
1798 return 0;
1799 if (tab->M && !isl_int_is_zero(tab->mat->row[row][2]))
1800 return 0;
1801 return isl_seq_first_non_zero(tab->mat->row[row] + off + tab->n_dead,
1802 tab->n_col - tab->n_dead) == -1;
1805 /* Add an equality that is known to be valid for the given tableau.
1807 struct isl_tab *isl_tab_add_valid_eq(struct isl_tab *tab, isl_int *eq)
1809 struct isl_tab_var *var;
1810 int r;
1812 if (!tab)
1813 return NULL;
1814 r = isl_tab_add_row(tab, eq);
1815 if (r < 0)
1816 goto error;
1818 var = &tab->con[r];
1819 r = var->index;
1820 if (row_is_manifestly_zero(tab, r)) {
1821 var->is_zero = 1;
1822 if (isl_tab_mark_redundant(tab, r) < 0)
1823 goto error;
1824 return tab;
1827 if (isl_int_is_neg(tab->mat->row[r][1])) {
1828 isl_seq_neg(tab->mat->row[r] + 1, tab->mat->row[r] + 1,
1829 1 + tab->n_col);
1830 var->negated = 1;
1832 var->is_nonneg = 1;
1833 if (to_col(tab, var) < 0)
1834 goto error;
1835 var->is_nonneg = 0;
1836 if (isl_tab_kill_col(tab, var->index) < 0)
1837 goto error;
1839 return tab;
1840 error:
1841 isl_tab_free(tab);
1842 return NULL;
1845 static int add_zero_row(struct isl_tab *tab)
1847 int r;
1848 isl_int *row;
1850 r = isl_tab_allocate_con(tab);
1851 if (r < 0)
1852 return -1;
1854 row = tab->mat->row[tab->con[r].index];
1855 isl_seq_clr(row + 1, 1 + tab->M + tab->n_col);
1856 isl_int_set_si(row[0], 1);
1858 return r;
1861 /* Add equality "eq" and check if it conflicts with the
1862 * previously added constraints or if it is obviously redundant.
1864 struct isl_tab *isl_tab_add_eq(struct isl_tab *tab, isl_int *eq)
1866 struct isl_tab_undo *snap = NULL;
1867 struct isl_tab_var *var;
1868 int r;
1869 int row;
1870 int sgn;
1871 isl_int cst;
1873 if (!tab)
1874 return NULL;
1875 isl_assert(tab->mat->ctx, !tab->M, goto error);
1877 if (tab->need_undo)
1878 snap = isl_tab_snap(tab);
1880 if (tab->cone) {
1881 isl_int_init(cst);
1882 isl_int_swap(eq[0], cst);
1884 r = isl_tab_add_row(tab, eq);
1885 if (tab->cone) {
1886 isl_int_swap(eq[0], cst);
1887 isl_int_clear(cst);
1889 if (r < 0)
1890 goto error;
1892 var = &tab->con[r];
1893 row = var->index;
1894 if (row_is_manifestly_zero(tab, row)) {
1895 if (snap) {
1896 if (isl_tab_rollback(tab, snap) < 0)
1897 goto error;
1898 } else
1899 drop_row(tab, row);
1900 return tab;
1903 if (tab->bmap) {
1904 tab->bmap = isl_basic_map_add_ineq(tab->bmap, eq);
1905 if (isl_tab_push(tab, isl_tab_undo_bmap_ineq) < 0)
1906 goto error;
1907 isl_seq_neg(eq, eq, 1 + tab->n_var);
1908 tab->bmap = isl_basic_map_add_ineq(tab->bmap, eq);
1909 isl_seq_neg(eq, eq, 1 + tab->n_var);
1910 if (isl_tab_push(tab, isl_tab_undo_bmap_ineq) < 0)
1911 goto error;
1912 if (!tab->bmap)
1913 goto error;
1914 if (add_zero_row(tab) < 0)
1915 goto error;
1918 sgn = isl_int_sgn(tab->mat->row[row][1]);
1920 if (sgn > 0) {
1921 isl_seq_neg(tab->mat->row[row] + 1, tab->mat->row[row] + 1,
1922 1 + tab->n_col);
1923 var->negated = 1;
1924 sgn = -1;
1927 if (sgn < 0) {
1928 sgn = sign_of_max(tab, var);
1929 if (sgn < -1)
1930 goto error;
1931 if (sgn < 0) {
1932 if (isl_tab_mark_empty(tab) < 0)
1933 goto error;
1934 return tab;
1938 var->is_nonneg = 1;
1939 if (to_col(tab, var) < 0)
1940 goto error;
1941 var->is_nonneg = 0;
1942 if (isl_tab_kill_col(tab, var->index) < 0)
1943 goto error;
1945 return tab;
1946 error:
1947 isl_tab_free(tab);
1948 return NULL;
1951 /* Construct and return an inequality that expresses an upper bound
1952 * on the given div.
1953 * In particular, if the div is given by
1955 * d = floor(e/m)
1957 * then the inequality expresses
1959 * m d <= e
1961 static struct isl_vec *ineq_for_div(struct isl_basic_map *bmap, unsigned div)
1963 unsigned total;
1964 unsigned div_pos;
1965 struct isl_vec *ineq;
1967 if (!bmap)
1968 return NULL;
1970 total = isl_basic_map_total_dim(bmap);
1971 div_pos = 1 + total - bmap->n_div + div;
1973 ineq = isl_vec_alloc(bmap->ctx, 1 + total);
1974 if (!ineq)
1975 return NULL;
1977 isl_seq_cpy(ineq->el, bmap->div[div] + 1, 1 + total);
1978 isl_int_neg(ineq->el[div_pos], bmap->div[div][0]);
1979 return ineq;
1982 /* For a div d = floor(f/m), add the constraints
1984 * f - m d >= 0
1985 * -(f-(m-1)) + m d >= 0
1987 * Note that the second constraint is the negation of
1989 * f - m d >= m
1991 * If add_ineq is not NULL, then this function is used
1992 * instead of isl_tab_add_ineq to effectively add the inequalities.
1994 static int add_div_constraints(struct isl_tab *tab, unsigned div,
1995 int (*add_ineq)(void *user, isl_int *), void *user)
1997 unsigned total;
1998 unsigned div_pos;
1999 struct isl_vec *ineq;
2001 total = isl_basic_map_total_dim(tab->bmap);
2002 div_pos = 1 + total - tab->bmap->n_div + div;
2004 ineq = ineq_for_div(tab->bmap, div);
2005 if (!ineq)
2006 goto error;
2008 if (add_ineq) {
2009 if (add_ineq(user, ineq->el) < 0)
2010 goto error;
2011 } else {
2012 if (isl_tab_add_ineq(tab, ineq->el) < 0)
2013 goto error;
2016 isl_seq_neg(ineq->el, tab->bmap->div[div] + 1, 1 + total);
2017 isl_int_set(ineq->el[div_pos], tab->bmap->div[div][0]);
2018 isl_int_add(ineq->el[0], ineq->el[0], ineq->el[div_pos]);
2019 isl_int_sub_ui(ineq->el[0], ineq->el[0], 1);
2021 if (add_ineq) {
2022 if (add_ineq(user, ineq->el) < 0)
2023 goto error;
2024 } else {
2025 if (isl_tab_add_ineq(tab, ineq->el) < 0)
2026 goto error;
2029 isl_vec_free(ineq);
2031 return 0;
2032 error:
2033 isl_vec_free(ineq);
2034 return -1;
2037 /* Add an extra div, prescrived by "div" to the tableau and
2038 * the associated bmap (which is assumed to be non-NULL).
2040 * If add_ineq is not NULL, then this function is used instead
2041 * of isl_tab_add_ineq to add the div constraints.
2042 * This complication is needed because the code in isl_tab_pip
2043 * wants to perform some extra processing when an inequality
2044 * is added to the tableau.
2046 int isl_tab_add_div(struct isl_tab *tab, __isl_keep isl_vec *div,
2047 int (*add_ineq)(void *user, isl_int *), void *user)
2049 int i;
2050 int r;
2051 int k;
2052 int nonneg;
2054 if (!tab || !div)
2055 return -1;
2057 isl_assert(tab->mat->ctx, tab->bmap, return -1);
2059 for (i = 0; i < tab->n_var; ++i) {
2060 if (isl_int_is_neg(div->el[2 + i]))
2061 break;
2062 if (isl_int_is_zero(div->el[2 + i]))
2063 continue;
2064 if (!tab->var[i].is_nonneg)
2065 break;
2067 nonneg = i == tab->n_var && !isl_int_is_neg(div->el[1]);
2069 if (isl_tab_extend_cons(tab, 3) < 0)
2070 return -1;
2071 if (isl_tab_extend_vars(tab, 1) < 0)
2072 return -1;
2073 r = isl_tab_allocate_var(tab);
2074 if (r < 0)
2075 return -1;
2077 if (nonneg)
2078 tab->var[r].is_nonneg = 1;
2080 tab->bmap = isl_basic_map_extend_dim(tab->bmap,
2081 isl_basic_map_get_dim(tab->bmap), 1, 0, 2);
2082 k = isl_basic_map_alloc_div(tab->bmap);
2083 if (k < 0)
2084 return -1;
2085 isl_seq_cpy(tab->bmap->div[k], div->el, div->size);
2086 if (isl_tab_push(tab, isl_tab_undo_bmap_div) < 0)
2087 return -1;
2089 if (add_div_constraints(tab, k, add_ineq, user) < 0)
2090 return -1;
2092 return r;
2095 struct isl_tab *isl_tab_from_basic_map(struct isl_basic_map *bmap)
2097 int i;
2098 struct isl_tab *tab;
2100 if (!bmap)
2101 return NULL;
2102 tab = isl_tab_alloc(bmap->ctx,
2103 isl_basic_map_total_dim(bmap) + bmap->n_ineq + 1,
2104 isl_basic_map_total_dim(bmap), 0);
2105 if (!tab)
2106 return NULL;
2107 tab->rational = ISL_F_ISSET(bmap, ISL_BASIC_MAP_RATIONAL);
2108 if (ISL_F_ISSET(bmap, ISL_BASIC_MAP_EMPTY)) {
2109 if (isl_tab_mark_empty(tab) < 0)
2110 goto error;
2111 return tab;
2113 for (i = 0; i < bmap->n_eq; ++i) {
2114 tab = add_eq(tab, bmap->eq[i]);
2115 if (!tab)
2116 return tab;
2118 for (i = 0; i < bmap->n_ineq; ++i) {
2119 if (isl_tab_add_ineq(tab, bmap->ineq[i]) < 0)
2120 goto error;
2121 if (tab->empty)
2122 return tab;
2124 return tab;
2125 error:
2126 isl_tab_free(tab);
2127 return NULL;
2130 struct isl_tab *isl_tab_from_basic_set(struct isl_basic_set *bset)
2132 return isl_tab_from_basic_map((struct isl_basic_map *)bset);
2135 /* Construct a tableau corresponding to the recession cone of "bset".
2137 struct isl_tab *isl_tab_from_recession_cone(struct isl_basic_set *bset)
2139 isl_int cst;
2140 int i;
2141 struct isl_tab *tab;
2143 if (!bset)
2144 return NULL;
2145 tab = isl_tab_alloc(bset->ctx, bset->n_eq + bset->n_ineq,
2146 isl_basic_set_total_dim(bset), 0);
2147 if (!tab)
2148 return NULL;
2149 tab->rational = ISL_F_ISSET(bset, ISL_BASIC_SET_RATIONAL);
2150 tab->cone = 1;
2152 isl_int_init(cst);
2153 for (i = 0; i < bset->n_eq; ++i) {
2154 isl_int_swap(bset->eq[i][0], cst);
2155 tab = add_eq(tab, bset->eq[i]);
2156 isl_int_swap(bset->eq[i][0], cst);
2157 if (!tab)
2158 goto done;
2160 for (i = 0; i < bset->n_ineq; ++i) {
2161 int r;
2162 isl_int_swap(bset->ineq[i][0], cst);
2163 r = isl_tab_add_row(tab, bset->ineq[i]);
2164 isl_int_swap(bset->ineq[i][0], cst);
2165 if (r < 0)
2166 goto error;
2167 tab->con[r].is_nonneg = 1;
2168 if (isl_tab_push_var(tab, isl_tab_undo_nonneg, &tab->con[r]) < 0)
2169 goto error;
2171 done:
2172 isl_int_clear(cst);
2173 return tab;
2174 error:
2175 isl_int_clear(cst);
2176 isl_tab_free(tab);
2177 return NULL;
2180 /* Assuming "tab" is the tableau of a cone, check if the cone is
2181 * bounded, i.e., if it is empty or only contains the origin.
2183 int isl_tab_cone_is_bounded(struct isl_tab *tab)
2185 int i;
2187 if (!tab)
2188 return -1;
2189 if (tab->empty)
2190 return 1;
2191 if (tab->n_dead == tab->n_col)
2192 return 1;
2194 for (;;) {
2195 for (i = tab->n_redundant; i < tab->n_row; ++i) {
2196 struct isl_tab_var *var;
2197 int sgn;
2198 var = isl_tab_var_from_row(tab, i);
2199 if (!var->is_nonneg)
2200 continue;
2201 sgn = sign_of_max(tab, var);
2202 if (sgn < -1)
2203 return -1;
2204 if (sgn != 0)
2205 return 0;
2206 if (close_row(tab, var) < 0)
2207 return -1;
2208 break;
2210 if (tab->n_dead == tab->n_col)
2211 return 1;
2212 if (i == tab->n_row)
2213 return 0;
2217 int isl_tab_sample_is_integer(struct isl_tab *tab)
2219 int i;
2221 if (!tab)
2222 return -1;
2224 for (i = 0; i < tab->n_var; ++i) {
2225 int row;
2226 if (!tab->var[i].is_row)
2227 continue;
2228 row = tab->var[i].index;
2229 if (!isl_int_is_divisible_by(tab->mat->row[row][1],
2230 tab->mat->row[row][0]))
2231 return 0;
2233 return 1;
2236 static struct isl_vec *extract_integer_sample(struct isl_tab *tab)
2238 int i;
2239 struct isl_vec *vec;
2241 vec = isl_vec_alloc(tab->mat->ctx, 1 + tab->n_var);
2242 if (!vec)
2243 return NULL;
2245 isl_int_set_si(vec->block.data[0], 1);
2246 for (i = 0; i < tab->n_var; ++i) {
2247 if (!tab->var[i].is_row)
2248 isl_int_set_si(vec->block.data[1 + i], 0);
2249 else {
2250 int row = tab->var[i].index;
2251 isl_int_divexact(vec->block.data[1 + i],
2252 tab->mat->row[row][1], tab->mat->row[row][0]);
2256 return vec;
2259 struct isl_vec *isl_tab_get_sample_value(struct isl_tab *tab)
2261 int i;
2262 struct isl_vec *vec;
2263 isl_int m;
2265 if (!tab)
2266 return NULL;
2268 vec = isl_vec_alloc(tab->mat->ctx, 1 + tab->n_var);
2269 if (!vec)
2270 return NULL;
2272 isl_int_init(m);
2274 isl_int_set_si(vec->block.data[0], 1);
2275 for (i = 0; i < tab->n_var; ++i) {
2276 int row;
2277 if (!tab->var[i].is_row) {
2278 isl_int_set_si(vec->block.data[1 + i], 0);
2279 continue;
2281 row = tab->var[i].index;
2282 isl_int_gcd(m, vec->block.data[0], tab->mat->row[row][0]);
2283 isl_int_divexact(m, tab->mat->row[row][0], m);
2284 isl_seq_scale(vec->block.data, vec->block.data, m, 1 + i);
2285 isl_int_divexact(m, vec->block.data[0], tab->mat->row[row][0]);
2286 isl_int_mul(vec->block.data[1 + i], m, tab->mat->row[row][1]);
2288 vec = isl_vec_normalize(vec);
2290 isl_int_clear(m);
2291 return vec;
2294 /* Update "bmap" based on the results of the tableau "tab".
2295 * In particular, implicit equalities are made explicit, redundant constraints
2296 * are removed and if the sample value happens to be integer, it is stored
2297 * in "bmap" (unless "bmap" already had an integer sample).
2299 * The tableau is assumed to have been created from "bmap" using
2300 * isl_tab_from_basic_map.
2302 struct isl_basic_map *isl_basic_map_update_from_tab(struct isl_basic_map *bmap,
2303 struct isl_tab *tab)
2305 int i;
2306 unsigned n_eq;
2308 if (!bmap)
2309 return NULL;
2310 if (!tab)
2311 return bmap;
2313 n_eq = tab->n_eq;
2314 if (tab->empty)
2315 bmap = isl_basic_map_set_to_empty(bmap);
2316 else
2317 for (i = bmap->n_ineq - 1; i >= 0; --i) {
2318 if (isl_tab_is_equality(tab, n_eq + i))
2319 isl_basic_map_inequality_to_equality(bmap, i);
2320 else if (isl_tab_is_redundant(tab, n_eq + i))
2321 isl_basic_map_drop_inequality(bmap, i);
2323 if (bmap->n_eq != n_eq)
2324 isl_basic_map_gauss(bmap, NULL);
2325 if (!tab->rational &&
2326 !bmap->sample && isl_tab_sample_is_integer(tab))
2327 bmap->sample = extract_integer_sample(tab);
2328 return bmap;
2331 struct isl_basic_set *isl_basic_set_update_from_tab(struct isl_basic_set *bset,
2332 struct isl_tab *tab)
2334 return (struct isl_basic_set *)isl_basic_map_update_from_tab(
2335 (struct isl_basic_map *)bset, tab);
2338 /* Given a non-negative variable "var", add a new non-negative variable
2339 * that is the opposite of "var", ensuring that var can only attain the
2340 * value zero.
2341 * If var = n/d is a row variable, then the new variable = -n/d.
2342 * If var is a column variables, then the new variable = -var.
2343 * If the new variable cannot attain non-negative values, then
2344 * the resulting tableau is empty.
2345 * Otherwise, we know the value will be zero and we close the row.
2347 static struct isl_tab *cut_to_hyperplane(struct isl_tab *tab,
2348 struct isl_tab_var *var)
2350 unsigned r;
2351 isl_int *row;
2352 int sgn;
2353 unsigned off = 2 + tab->M;
2355 if (var->is_zero)
2356 return tab;
2357 isl_assert(tab->mat->ctx, !var->is_redundant, goto error);
2358 isl_assert(tab->mat->ctx, var->is_nonneg, goto error);
2360 if (isl_tab_extend_cons(tab, 1) < 0)
2361 goto error;
2363 r = tab->n_con;
2364 tab->con[r].index = tab->n_row;
2365 tab->con[r].is_row = 1;
2366 tab->con[r].is_nonneg = 0;
2367 tab->con[r].is_zero = 0;
2368 tab->con[r].is_redundant = 0;
2369 tab->con[r].frozen = 0;
2370 tab->con[r].negated = 0;
2371 tab->row_var[tab->n_row] = ~r;
2372 row = tab->mat->row[tab->n_row];
2374 if (var->is_row) {
2375 isl_int_set(row[0], tab->mat->row[var->index][0]);
2376 isl_seq_neg(row + 1,
2377 tab->mat->row[var->index] + 1, 1 + tab->n_col);
2378 } else {
2379 isl_int_set_si(row[0], 1);
2380 isl_seq_clr(row + 1, 1 + tab->n_col);
2381 isl_int_set_si(row[off + var->index], -1);
2384 tab->n_row++;
2385 tab->n_con++;
2386 if (isl_tab_push_var(tab, isl_tab_undo_allocate, &tab->con[r]) < 0)
2387 goto error;
2389 sgn = sign_of_max(tab, &tab->con[r]);
2390 if (sgn < -1)
2391 goto error;
2392 if (sgn < 0) {
2393 if (isl_tab_mark_empty(tab) < 0)
2394 goto error;
2395 return tab;
2397 tab->con[r].is_nonneg = 1;
2398 if (isl_tab_push_var(tab, isl_tab_undo_nonneg, &tab->con[r]) < 0)
2399 goto error;
2400 /* sgn == 0 */
2401 if (close_row(tab, &tab->con[r]) < 0)
2402 goto error;
2404 return tab;
2405 error:
2406 isl_tab_free(tab);
2407 return NULL;
2410 /* Given a tableau "tab" and an inequality constraint "con" of the tableau,
2411 * relax the inequality by one. That is, the inequality r >= 0 is replaced
2412 * by r' = r + 1 >= 0.
2413 * If r is a row variable, we simply increase the constant term by one
2414 * (taking into account the denominator).
2415 * If r is a column variable, then we need to modify each row that
2416 * refers to r = r' - 1 by substituting this equality, effectively
2417 * subtracting the coefficient of the column from the constant.
2418 * We should only do this if the minimum is manifestly unbounded,
2419 * however. Otherwise, we may end up with negative sample values
2420 * for non-negative variables.
2421 * So, if r is a column variable with a minimum that is not
2422 * manifestly unbounded, then we need to move it to a row.
2423 * However, the sample value of this row may be negative,
2424 * even after the relaxation, so we need to restore it.
2425 * We therefore prefer to pivot a column up to a row, if possible.
2427 struct isl_tab *isl_tab_relax(struct isl_tab *tab, int con)
2429 struct isl_tab_var *var;
2430 unsigned off = 2 + tab->M;
2432 if (!tab)
2433 return NULL;
2435 var = &tab->con[con];
2437 if (!var->is_row && !max_is_manifestly_unbounded(tab, var))
2438 if (to_row(tab, var, 1) < 0)
2439 goto error;
2440 if (!var->is_row && !min_is_manifestly_unbounded(tab, var))
2441 if (to_row(tab, var, -1) < 0)
2442 goto error;
2444 if (var->is_row) {
2445 isl_int_add(tab->mat->row[var->index][1],
2446 tab->mat->row[var->index][1], tab->mat->row[var->index][0]);
2447 if (restore_row(tab, var) < 0)
2448 goto error;
2449 } else {
2450 int i;
2452 for (i = 0; i < tab->n_row; ++i) {
2453 if (isl_int_is_zero(tab->mat->row[i][off + var->index]))
2454 continue;
2455 isl_int_sub(tab->mat->row[i][1], tab->mat->row[i][1],
2456 tab->mat->row[i][off + var->index]);
2461 if (isl_tab_push_var(tab, isl_tab_undo_relax, var) < 0)
2462 goto error;
2464 return tab;
2465 error:
2466 isl_tab_free(tab);
2467 return NULL;
2470 struct isl_tab *isl_tab_select_facet(struct isl_tab *tab, int con)
2472 if (!tab)
2473 return NULL;
2475 return cut_to_hyperplane(tab, &tab->con[con]);
2478 static int may_be_equality(struct isl_tab *tab, int row)
2480 unsigned off = 2 + tab->M;
2481 return (tab->rational ? isl_int_is_zero(tab->mat->row[row][1])
2482 : isl_int_lt(tab->mat->row[row][1],
2483 tab->mat->row[row][0])) &&
2484 isl_seq_first_non_zero(tab->mat->row[row] + off + tab->n_dead,
2485 tab->n_col - tab->n_dead) != -1;
2488 /* Check for (near) equalities among the constraints.
2489 * A constraint is an equality if it is non-negative and if
2490 * its maximal value is either
2491 * - zero (in case of rational tableaus), or
2492 * - strictly less than 1 (in case of integer tableaus)
2494 * We first mark all non-redundant and non-dead variables that
2495 * are not frozen and not obviously not an equality.
2496 * Then we iterate over all marked variables if they can attain
2497 * any values larger than zero or at least one.
2498 * If the maximal value is zero, we mark any column variables
2499 * that appear in the row as being zero and mark the row as being redundant.
2500 * Otherwise, if the maximal value is strictly less than one (and the
2501 * tableau is integer), then we restrict the value to being zero
2502 * by adding an opposite non-negative variable.
2504 struct isl_tab *isl_tab_detect_implicit_equalities(struct isl_tab *tab)
2506 int i;
2507 unsigned n_marked;
2509 if (!tab)
2510 return NULL;
2511 if (tab->empty)
2512 return tab;
2513 if (tab->n_dead == tab->n_col)
2514 return tab;
2516 n_marked = 0;
2517 for (i = tab->n_redundant; i < tab->n_row; ++i) {
2518 struct isl_tab_var *var = isl_tab_var_from_row(tab, i);
2519 var->marked = !var->frozen && var->is_nonneg &&
2520 may_be_equality(tab, i);
2521 if (var->marked)
2522 n_marked++;
2524 for (i = tab->n_dead; i < tab->n_col; ++i) {
2525 struct isl_tab_var *var = var_from_col(tab, i);
2526 var->marked = !var->frozen && var->is_nonneg;
2527 if (var->marked)
2528 n_marked++;
2530 while (n_marked) {
2531 struct isl_tab_var *var;
2532 int sgn;
2533 for (i = tab->n_redundant; i < tab->n_row; ++i) {
2534 var = isl_tab_var_from_row(tab, i);
2535 if (var->marked)
2536 break;
2538 if (i == tab->n_row) {
2539 for (i = tab->n_dead; i < tab->n_col; ++i) {
2540 var = var_from_col(tab, i);
2541 if (var->marked)
2542 break;
2544 if (i == tab->n_col)
2545 break;
2547 var->marked = 0;
2548 n_marked--;
2549 sgn = sign_of_max(tab, var);
2550 if (sgn < 0)
2551 goto error;
2552 if (sgn == 0) {
2553 if (close_row(tab, var) < 0)
2554 goto error;
2555 } else if (!tab->rational && !at_least_one(tab, var)) {
2556 tab = cut_to_hyperplane(tab, var);
2557 return isl_tab_detect_implicit_equalities(tab);
2559 for (i = tab->n_redundant; i < tab->n_row; ++i) {
2560 var = isl_tab_var_from_row(tab, i);
2561 if (!var->marked)
2562 continue;
2563 if (may_be_equality(tab, i))
2564 continue;
2565 var->marked = 0;
2566 n_marked--;
2570 return tab;
2571 error:
2572 isl_tab_free(tab);
2573 return NULL;
2576 static int con_is_redundant(struct isl_tab *tab, struct isl_tab_var *var)
2578 if (!tab)
2579 return -1;
2580 if (tab->rational) {
2581 int sgn = sign_of_min(tab, var);
2582 if (sgn < -1)
2583 return -1;
2584 return sgn >= 0;
2585 } else {
2586 int irred = isl_tab_min_at_most_neg_one(tab, var);
2587 if (irred < 0)
2588 return -1;
2589 return !irred;
2593 /* Check for (near) redundant constraints.
2594 * A constraint is redundant if it is non-negative and if
2595 * its minimal value (temporarily ignoring the non-negativity) is either
2596 * - zero (in case of rational tableaus), or
2597 * - strictly larger than -1 (in case of integer tableaus)
2599 * We first mark all non-redundant and non-dead variables that
2600 * are not frozen and not obviously negatively unbounded.
2601 * Then we iterate over all marked variables if they can attain
2602 * any values smaller than zero or at most negative one.
2603 * If not, we mark the row as being redundant (assuming it hasn't
2604 * been detected as being obviously redundant in the mean time).
2606 int isl_tab_detect_redundant(struct isl_tab *tab)
2608 int i;
2609 unsigned n_marked;
2611 if (!tab)
2612 return -1;
2613 if (tab->empty)
2614 return 0;
2615 if (tab->n_redundant == tab->n_row)
2616 return 0;
2618 n_marked = 0;
2619 for (i = tab->n_redundant; i < tab->n_row; ++i) {
2620 struct isl_tab_var *var = isl_tab_var_from_row(tab, i);
2621 var->marked = !var->frozen && var->is_nonneg;
2622 if (var->marked)
2623 n_marked++;
2625 for (i = tab->n_dead; i < tab->n_col; ++i) {
2626 struct isl_tab_var *var = var_from_col(tab, i);
2627 var->marked = !var->frozen && var->is_nonneg &&
2628 !min_is_manifestly_unbounded(tab, var);
2629 if (var->marked)
2630 n_marked++;
2632 while (n_marked) {
2633 struct isl_tab_var *var;
2634 int red;
2635 for (i = tab->n_redundant; i < tab->n_row; ++i) {
2636 var = isl_tab_var_from_row(tab, i);
2637 if (var->marked)
2638 break;
2640 if (i == tab->n_row) {
2641 for (i = tab->n_dead; i < tab->n_col; ++i) {
2642 var = var_from_col(tab, i);
2643 if (var->marked)
2644 break;
2646 if (i == tab->n_col)
2647 break;
2649 var->marked = 0;
2650 n_marked--;
2651 red = con_is_redundant(tab, var);
2652 if (red < 0)
2653 return -1;
2654 if (red && !var->is_redundant)
2655 if (isl_tab_mark_redundant(tab, var->index) < 0)
2656 return -1;
2657 for (i = tab->n_dead; i < tab->n_col; ++i) {
2658 var = var_from_col(tab, i);
2659 if (!var->marked)
2660 continue;
2661 if (!min_is_manifestly_unbounded(tab, var))
2662 continue;
2663 var->marked = 0;
2664 n_marked--;
2668 return 0;
2671 int isl_tab_is_equality(struct isl_tab *tab, int con)
2673 int row;
2674 unsigned off;
2676 if (!tab)
2677 return -1;
2678 if (tab->con[con].is_zero)
2679 return 1;
2680 if (tab->con[con].is_redundant)
2681 return 0;
2682 if (!tab->con[con].is_row)
2683 return tab->con[con].index < tab->n_dead;
2685 row = tab->con[con].index;
2687 off = 2 + tab->M;
2688 return isl_int_is_zero(tab->mat->row[row][1]) &&
2689 isl_seq_first_non_zero(tab->mat->row[row] + 2 + tab->n_dead,
2690 tab->n_col - tab->n_dead) == -1;
2693 /* Return the minimial value of the affine expression "f" with denominator
2694 * "denom" in *opt, *opt_denom, assuming the tableau is not empty and
2695 * the expression cannot attain arbitrarily small values.
2696 * If opt_denom is NULL, then *opt is rounded up to the nearest integer.
2697 * The return value reflects the nature of the result (empty, unbounded,
2698 * minmimal value returned in *opt).
2700 enum isl_lp_result isl_tab_min(struct isl_tab *tab,
2701 isl_int *f, isl_int denom, isl_int *opt, isl_int *opt_denom,
2702 unsigned flags)
2704 int r;
2705 enum isl_lp_result res = isl_lp_ok;
2706 struct isl_tab_var *var;
2707 struct isl_tab_undo *snap;
2709 if (tab->empty)
2710 return isl_lp_empty;
2712 snap = isl_tab_snap(tab);
2713 r = isl_tab_add_row(tab, f);
2714 if (r < 0)
2715 return isl_lp_error;
2716 var = &tab->con[r];
2717 isl_int_mul(tab->mat->row[var->index][0],
2718 tab->mat->row[var->index][0], denom);
2719 for (;;) {
2720 int row, col;
2721 find_pivot(tab, var, var, -1, &row, &col);
2722 if (row == var->index) {
2723 res = isl_lp_unbounded;
2724 break;
2726 if (row == -1)
2727 break;
2728 if (isl_tab_pivot(tab, row, col) < 0)
2729 return isl_lp_error;
2731 if (ISL_FL_ISSET(flags, ISL_TAB_SAVE_DUAL)) {
2732 int i;
2734 isl_vec_free(tab->dual);
2735 tab->dual = isl_vec_alloc(tab->mat->ctx, 1 + tab->n_con);
2736 if (!tab->dual)
2737 return isl_lp_error;
2738 isl_int_set(tab->dual->el[0], tab->mat->row[var->index][0]);
2739 for (i = 0; i < tab->n_con; ++i) {
2740 int pos;
2741 if (tab->con[i].is_row) {
2742 isl_int_set_si(tab->dual->el[1 + i], 0);
2743 continue;
2745 pos = 2 + tab->M + tab->con[i].index;
2746 if (tab->con[i].negated)
2747 isl_int_neg(tab->dual->el[1 + i],
2748 tab->mat->row[var->index][pos]);
2749 else
2750 isl_int_set(tab->dual->el[1 + i],
2751 tab->mat->row[var->index][pos]);
2754 if (opt && res == isl_lp_ok) {
2755 if (opt_denom) {
2756 isl_int_set(*opt, tab->mat->row[var->index][1]);
2757 isl_int_set(*opt_denom, tab->mat->row[var->index][0]);
2758 } else
2759 isl_int_cdiv_q(*opt, tab->mat->row[var->index][1],
2760 tab->mat->row[var->index][0]);
2762 if (isl_tab_rollback(tab, snap) < 0)
2763 return isl_lp_error;
2764 return res;
2767 int isl_tab_is_redundant(struct isl_tab *tab, int con)
2769 if (!tab)
2770 return -1;
2771 if (tab->con[con].is_zero)
2772 return 0;
2773 if (tab->con[con].is_redundant)
2774 return 1;
2775 return tab->con[con].is_row && tab->con[con].index < tab->n_redundant;
2778 /* Take a snapshot of the tableau that can be restored by s call to
2779 * isl_tab_rollback.
2781 struct isl_tab_undo *isl_tab_snap(struct isl_tab *tab)
2783 if (!tab)
2784 return NULL;
2785 tab->need_undo = 1;
2786 return tab->top;
2789 /* Undo the operation performed by isl_tab_relax.
2791 static int unrelax(struct isl_tab *tab, struct isl_tab_var *var) WARN_UNUSED;
2792 static int unrelax(struct isl_tab *tab, struct isl_tab_var *var)
2794 unsigned off = 2 + tab->M;
2796 if (!var->is_row && !max_is_manifestly_unbounded(tab, var))
2797 if (to_row(tab, var, 1) < 0)
2798 return -1;
2800 if (var->is_row) {
2801 isl_int_sub(tab->mat->row[var->index][1],
2802 tab->mat->row[var->index][1], tab->mat->row[var->index][0]);
2803 if (var->is_nonneg) {
2804 int sgn = restore_row(tab, var);
2805 isl_assert(tab->mat->ctx, sgn >= 0, return -1);
2807 } else {
2808 int i;
2810 for (i = 0; i < tab->n_row; ++i) {
2811 if (isl_int_is_zero(tab->mat->row[i][off + var->index]))
2812 continue;
2813 isl_int_add(tab->mat->row[i][1], tab->mat->row[i][1],
2814 tab->mat->row[i][off + var->index]);
2819 return 0;
2822 static int perform_undo_var(struct isl_tab *tab, struct isl_tab_undo *undo) WARN_UNUSED;
2823 static int perform_undo_var(struct isl_tab *tab, struct isl_tab_undo *undo)
2825 struct isl_tab_var *var = var_from_index(tab, undo->u.var_index);
2826 switch(undo->type) {
2827 case isl_tab_undo_nonneg:
2828 var->is_nonneg = 0;
2829 break;
2830 case isl_tab_undo_redundant:
2831 var->is_redundant = 0;
2832 tab->n_redundant--;
2833 restore_row(tab, isl_tab_var_from_row(tab, tab->n_redundant));
2834 break;
2835 case isl_tab_undo_freeze:
2836 var->frozen = 0;
2837 break;
2838 case isl_tab_undo_zero:
2839 var->is_zero = 0;
2840 if (!var->is_row)
2841 tab->n_dead--;
2842 break;
2843 case isl_tab_undo_allocate:
2844 if (undo->u.var_index >= 0) {
2845 isl_assert(tab->mat->ctx, !var->is_row, return -1);
2846 drop_col(tab, var->index);
2847 break;
2849 if (!var->is_row) {
2850 if (!max_is_manifestly_unbounded(tab, var)) {
2851 if (to_row(tab, var, 1) < 0)
2852 return -1;
2853 } else if (!min_is_manifestly_unbounded(tab, var)) {
2854 if (to_row(tab, var, -1) < 0)
2855 return -1;
2856 } else
2857 if (to_row(tab, var, 0) < 0)
2858 return -1;
2860 drop_row(tab, var->index);
2861 break;
2862 case isl_tab_undo_relax:
2863 return unrelax(tab, var);
2866 return 0;
2869 /* Restore the tableau to the state where the basic variables
2870 * are those in "col_var".
2871 * We first construct a list of variables that are currently in
2872 * the basis, but shouldn't. Then we iterate over all variables
2873 * that should be in the basis and for each one that is currently
2874 * not in the basis, we exchange it with one of the elements of the
2875 * list constructed before.
2876 * We can always find an appropriate variable to pivot with because
2877 * the current basis is mapped to the old basis by a non-singular
2878 * matrix and so we can never end up with a zero row.
2880 static int restore_basis(struct isl_tab *tab, int *col_var)
2882 int i, j;
2883 int n_extra = 0;
2884 int *extra = NULL; /* current columns that contain bad stuff */
2885 unsigned off = 2 + tab->M;
2887 extra = isl_alloc_array(tab->mat->ctx, int, tab->n_col);
2888 if (!extra)
2889 goto error;
2890 for (i = 0; i < tab->n_col; ++i) {
2891 for (j = 0; j < tab->n_col; ++j)
2892 if (tab->col_var[i] == col_var[j])
2893 break;
2894 if (j < tab->n_col)
2895 continue;
2896 extra[n_extra++] = i;
2898 for (i = 0; i < tab->n_col && n_extra > 0; ++i) {
2899 struct isl_tab_var *var;
2900 int row;
2902 for (j = 0; j < tab->n_col; ++j)
2903 if (col_var[i] == tab->col_var[j])
2904 break;
2905 if (j < tab->n_col)
2906 continue;
2907 var = var_from_index(tab, col_var[i]);
2908 row = var->index;
2909 for (j = 0; j < n_extra; ++j)
2910 if (!isl_int_is_zero(tab->mat->row[row][off+extra[j]]))
2911 break;
2912 isl_assert(tab->mat->ctx, j < n_extra, goto error);
2913 if (isl_tab_pivot(tab, row, extra[j]) < 0)
2914 goto error;
2915 extra[j] = extra[--n_extra];
2918 free(extra);
2919 free(col_var);
2920 return 0;
2921 error:
2922 free(extra);
2923 free(col_var);
2924 return -1;
2927 /* Remove all samples with index n or greater, i.e., those samples
2928 * that were added since we saved this number of samples in
2929 * isl_tab_save_samples.
2931 static void drop_samples_since(struct isl_tab *tab, int n)
2933 int i;
2935 for (i = tab->n_sample - 1; i >= 0 && tab->n_sample > n; --i) {
2936 if (tab->sample_index[i] < n)
2937 continue;
2939 if (i != tab->n_sample - 1) {
2940 int t = tab->sample_index[tab->n_sample-1];
2941 tab->sample_index[tab->n_sample-1] = tab->sample_index[i];
2942 tab->sample_index[i] = t;
2943 isl_mat_swap_rows(tab->samples, tab->n_sample-1, i);
2945 tab->n_sample--;
2949 static int perform_undo(struct isl_tab *tab, struct isl_tab_undo *undo) WARN_UNUSED;
2950 static int perform_undo(struct isl_tab *tab, struct isl_tab_undo *undo)
2952 switch (undo->type) {
2953 case isl_tab_undo_empty:
2954 tab->empty = 0;
2955 break;
2956 case isl_tab_undo_nonneg:
2957 case isl_tab_undo_redundant:
2958 case isl_tab_undo_freeze:
2959 case isl_tab_undo_zero:
2960 case isl_tab_undo_allocate:
2961 case isl_tab_undo_relax:
2962 return perform_undo_var(tab, undo);
2963 case isl_tab_undo_bmap_eq:
2964 return isl_basic_map_free_equality(tab->bmap, 1);
2965 case isl_tab_undo_bmap_ineq:
2966 return isl_basic_map_free_inequality(tab->bmap, 1);
2967 case isl_tab_undo_bmap_div:
2968 if (isl_basic_map_free_div(tab->bmap, 1) < 0)
2969 return -1;
2970 if (tab->samples)
2971 tab->samples->n_col--;
2972 break;
2973 case isl_tab_undo_saved_basis:
2974 if (restore_basis(tab, undo->u.col_var) < 0)
2975 return -1;
2976 break;
2977 case isl_tab_undo_drop_sample:
2978 tab->n_outside--;
2979 break;
2980 case isl_tab_undo_saved_samples:
2981 drop_samples_since(tab, undo->u.n);
2982 break;
2983 case isl_tab_undo_callback:
2984 return undo->u.callback->run(undo->u.callback);
2985 default:
2986 isl_assert(tab->mat->ctx, 0, return -1);
2988 return 0;
2991 /* Return the tableau to the state it was in when the snapshot "snap"
2992 * was taken.
2994 int isl_tab_rollback(struct isl_tab *tab, struct isl_tab_undo *snap)
2996 struct isl_tab_undo *undo, *next;
2998 if (!tab)
2999 return -1;
3001 tab->in_undo = 1;
3002 for (undo = tab->top; undo && undo != &tab->bottom; undo = next) {
3003 next = undo->next;
3004 if (undo == snap)
3005 break;
3006 if (perform_undo(tab, undo) < 0) {
3007 free_undo(tab);
3008 tab->in_undo = 0;
3009 return -1;
3011 free(undo);
3013 tab->in_undo = 0;
3014 tab->top = undo;
3015 if (!undo)
3016 return -1;
3017 return 0;
3020 /* The given row "row" represents an inequality violated by all
3021 * points in the tableau. Check for some special cases of such
3022 * separating constraints.
3023 * In particular, if the row has been reduced to the constant -1,
3024 * then we know the inequality is adjacent (but opposite) to
3025 * an equality in the tableau.
3026 * If the row has been reduced to r = -1 -r', with r' an inequality
3027 * of the tableau, then the inequality is adjacent (but opposite)
3028 * to the inequality r'.
3030 static enum isl_ineq_type separation_type(struct isl_tab *tab, unsigned row)
3032 int pos;
3033 unsigned off = 2 + tab->M;
3035 if (tab->rational)
3036 return isl_ineq_separate;
3038 if (!isl_int_is_one(tab->mat->row[row][0]))
3039 return isl_ineq_separate;
3040 if (!isl_int_is_negone(tab->mat->row[row][1]))
3041 return isl_ineq_separate;
3043 pos = isl_seq_first_non_zero(tab->mat->row[row] + off + tab->n_dead,
3044 tab->n_col - tab->n_dead);
3045 if (pos == -1)
3046 return isl_ineq_adj_eq;
3048 if (!isl_int_is_negone(tab->mat->row[row][off + tab->n_dead + pos]))
3049 return isl_ineq_separate;
3051 pos = isl_seq_first_non_zero(
3052 tab->mat->row[row] + off + tab->n_dead + pos + 1,
3053 tab->n_col - tab->n_dead - pos - 1);
3055 return pos == -1 ? isl_ineq_adj_ineq : isl_ineq_separate;
3058 /* Check the effect of inequality "ineq" on the tableau "tab".
3059 * The result may be
3060 * isl_ineq_redundant: satisfied by all points in the tableau
3061 * isl_ineq_separate: satisfied by no point in the tableau
3062 * isl_ineq_cut: satisfied by some by not all points
3063 * isl_ineq_adj_eq: adjacent to an equality
3064 * isl_ineq_adj_ineq: adjacent to an inequality.
3066 enum isl_ineq_type isl_tab_ineq_type(struct isl_tab *tab, isl_int *ineq)
3068 enum isl_ineq_type type = isl_ineq_error;
3069 struct isl_tab_undo *snap = NULL;
3070 int con;
3071 int row;
3073 if (!tab)
3074 return isl_ineq_error;
3076 if (isl_tab_extend_cons(tab, 1) < 0)
3077 return isl_ineq_error;
3079 snap = isl_tab_snap(tab);
3081 con = isl_tab_add_row(tab, ineq);
3082 if (con < 0)
3083 goto error;
3085 row = tab->con[con].index;
3086 if (isl_tab_row_is_redundant(tab, row))
3087 type = isl_ineq_redundant;
3088 else if (isl_int_is_neg(tab->mat->row[row][1]) &&
3089 (tab->rational ||
3090 isl_int_abs_ge(tab->mat->row[row][1],
3091 tab->mat->row[row][0]))) {
3092 int nonneg = at_least_zero(tab, &tab->con[con]);
3093 if (nonneg < 0)
3094 goto error;
3095 if (nonneg)
3096 type = isl_ineq_cut;
3097 else
3098 type = separation_type(tab, row);
3099 } else {
3100 int red = con_is_redundant(tab, &tab->con[con]);
3101 if (red < 0)
3102 goto error;
3103 if (!red)
3104 type = isl_ineq_cut;
3105 else
3106 type = isl_ineq_redundant;
3109 if (isl_tab_rollback(tab, snap))
3110 return isl_ineq_error;
3111 return type;
3112 error:
3113 return isl_ineq_error;
3116 int isl_tab_track_bmap(struct isl_tab *tab, __isl_take isl_basic_map *bmap)
3118 if (!tab || !bmap)
3119 goto error;
3121 isl_assert(tab->mat->ctx, tab->n_eq == bmap->n_eq, return -1);
3122 isl_assert(tab->mat->ctx,
3123 tab->n_con == bmap->n_eq + bmap->n_ineq, return -1);
3125 tab->bmap = bmap;
3127 return 0;
3128 error:
3129 isl_basic_map_free(bmap);
3130 return -1;
3133 int isl_tab_track_bset(struct isl_tab *tab, __isl_take isl_basic_set *bset)
3135 return isl_tab_track_bmap(tab, (isl_basic_map *)bset);
3138 __isl_keep isl_basic_set *isl_tab_peek_bset(struct isl_tab *tab)
3140 if (!tab)
3141 return NULL;
3143 return (isl_basic_set *)tab->bmap;
3146 void isl_tab_dump(struct isl_tab *tab, FILE *out, int indent)
3148 unsigned r, c;
3149 int i;
3151 if (!tab) {
3152 fprintf(out, "%*snull tab\n", indent, "");
3153 return;
3155 fprintf(out, "%*sn_redundant: %d, n_dead: %d", indent, "",
3156 tab->n_redundant, tab->n_dead);
3157 if (tab->rational)
3158 fprintf(out, ", rational");
3159 if (tab->empty)
3160 fprintf(out, ", empty");
3161 fprintf(out, "\n");
3162 fprintf(out, "%*s[", indent, "");
3163 for (i = 0; i < tab->n_var; ++i) {
3164 if (i)
3165 fprintf(out, (i == tab->n_param ||
3166 i == tab->n_var - tab->n_div) ? "; "
3167 : ", ");
3168 fprintf(out, "%c%d%s", tab->var[i].is_row ? 'r' : 'c',
3169 tab->var[i].index,
3170 tab->var[i].is_zero ? " [=0]" :
3171 tab->var[i].is_redundant ? " [R]" : "");
3173 fprintf(out, "]\n");
3174 fprintf(out, "%*s[", indent, "");
3175 for (i = 0; i < tab->n_con; ++i) {
3176 if (i)
3177 fprintf(out, ", ");
3178 fprintf(out, "%c%d%s", tab->con[i].is_row ? 'r' : 'c',
3179 tab->con[i].index,
3180 tab->con[i].is_zero ? " [=0]" :
3181 tab->con[i].is_redundant ? " [R]" : "");
3183 fprintf(out, "]\n");
3184 fprintf(out, "%*s[", indent, "");
3185 for (i = 0; i < tab->n_row; ++i) {
3186 const char *sign = "";
3187 if (i)
3188 fprintf(out, ", ");
3189 if (tab->row_sign) {
3190 if (tab->row_sign[i] == isl_tab_row_unknown)
3191 sign = "?";
3192 else if (tab->row_sign[i] == isl_tab_row_neg)
3193 sign = "-";
3194 else if (tab->row_sign[i] == isl_tab_row_pos)
3195 sign = "+";
3196 else
3197 sign = "+-";
3199 fprintf(out, "r%d: %d%s%s", i, tab->row_var[i],
3200 isl_tab_var_from_row(tab, i)->is_nonneg ? " [>=0]" : "", sign);
3202 fprintf(out, "]\n");
3203 fprintf(out, "%*s[", indent, "");
3204 for (i = 0; i < tab->n_col; ++i) {
3205 if (i)
3206 fprintf(out, ", ");
3207 fprintf(out, "c%d: %d%s", i, tab->col_var[i],
3208 var_from_col(tab, i)->is_nonneg ? " [>=0]" : "");
3210 fprintf(out, "]\n");
3211 r = tab->mat->n_row;
3212 tab->mat->n_row = tab->n_row;
3213 c = tab->mat->n_col;
3214 tab->mat->n_col = 2 + tab->M + tab->n_col;
3215 isl_mat_dump(tab->mat, out, indent);
3216 tab->mat->n_row = r;
3217 tab->mat->n_col = c;
3218 if (tab->bmap)
3219 isl_basic_map_dump(tab->bmap, out, indent);