Add __isl_take to isl_constraint_set_coefficient_val
[isl.git] / isl_tab.c
blob85afa0bc89018bc570530061037a35a2d8925aae
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 struct isl_tab *isl_tab_extend(struct isl_tab *tab, unsigned n_new)
177 if (isl_tab_extend_cons(tab, n_new) >= 0)
178 return tab;
180 isl_tab_free(tab);
181 return NULL;
184 static void free_undo_record(struct isl_tab_undo *undo)
186 switch (undo->type) {
187 case isl_tab_undo_saved_basis:
188 free(undo->u.col_var);
189 break;
190 default:;
192 free(undo);
195 static void free_undo(struct isl_tab *tab)
197 struct isl_tab_undo *undo, *next;
199 for (undo = tab->top; undo && undo != &tab->bottom; undo = next) {
200 next = undo->next;
201 free_undo_record(undo);
203 tab->top = undo;
206 void isl_tab_free(struct isl_tab *tab)
208 if (!tab)
209 return;
210 free_undo(tab);
211 isl_mat_free(tab->mat);
212 isl_vec_free(tab->dual);
213 isl_basic_map_free(tab->bmap);
214 free(tab->var);
215 free(tab->con);
216 free(tab->row_var);
217 free(tab->col_var);
218 free(tab->row_sign);
219 isl_mat_free(tab->samples);
220 free(tab->sample_index);
221 isl_mat_free(tab->basis);
222 free(tab);
225 struct isl_tab *isl_tab_dup(struct isl_tab *tab)
227 int i;
228 struct isl_tab *dup;
229 unsigned off;
231 if (!tab)
232 return NULL;
234 off = 2 + tab->M;
235 dup = isl_calloc_type(tab->mat->ctx, struct isl_tab);
236 if (!dup)
237 return NULL;
238 dup->mat = isl_mat_dup(tab->mat);
239 if (!dup->mat)
240 goto error;
241 dup->var = isl_alloc_array(tab->mat->ctx, struct isl_tab_var, tab->max_var);
242 if (tab->max_var && !dup->var)
243 goto error;
244 for (i = 0; i < tab->n_var; ++i)
245 dup->var[i] = tab->var[i];
246 dup->con = isl_alloc_array(tab->mat->ctx, struct isl_tab_var, tab->max_con);
247 if (tab->max_con && !dup->con)
248 goto error;
249 for (i = 0; i < tab->n_con; ++i)
250 dup->con[i] = tab->con[i];
251 dup->col_var = isl_alloc_array(tab->mat->ctx, int, tab->mat->n_col - off);
252 if ((tab->mat->n_col - off) && !dup->col_var)
253 goto error;
254 for (i = 0; i < tab->n_col; ++i)
255 dup->col_var[i] = tab->col_var[i];
256 dup->row_var = isl_alloc_array(tab->mat->ctx, int, tab->mat->n_row);
257 if (tab->mat->n_row && !dup->row_var)
258 goto error;
259 for (i = 0; i < tab->n_row; ++i)
260 dup->row_var[i] = tab->row_var[i];
261 if (tab->row_sign) {
262 dup->row_sign = isl_alloc_array(tab->mat->ctx, enum isl_tab_row_sign,
263 tab->mat->n_row);
264 if (tab->mat->n_row && !dup->row_sign)
265 goto error;
266 for (i = 0; i < tab->n_row; ++i)
267 dup->row_sign[i] = tab->row_sign[i];
269 if (tab->samples) {
270 dup->samples = isl_mat_dup(tab->samples);
271 if (!dup->samples)
272 goto error;
273 dup->sample_index = isl_alloc_array(tab->mat->ctx, int,
274 tab->samples->n_row);
275 if (tab->samples->n_row && !dup->sample_index)
276 goto error;
277 dup->n_sample = tab->n_sample;
278 dup->n_outside = tab->n_outside;
280 dup->n_row = tab->n_row;
281 dup->n_con = tab->n_con;
282 dup->n_eq = tab->n_eq;
283 dup->max_con = tab->max_con;
284 dup->n_col = tab->n_col;
285 dup->n_var = tab->n_var;
286 dup->max_var = tab->max_var;
287 dup->n_param = tab->n_param;
288 dup->n_div = tab->n_div;
289 dup->n_dead = tab->n_dead;
290 dup->n_redundant = tab->n_redundant;
291 dup->rational = tab->rational;
292 dup->empty = tab->empty;
293 dup->strict_redundant = 0;
294 dup->need_undo = 0;
295 dup->in_undo = 0;
296 dup->M = tab->M;
297 tab->cone = tab->cone;
298 dup->bottom.type = isl_tab_undo_bottom;
299 dup->bottom.next = NULL;
300 dup->top = &dup->bottom;
302 dup->n_zero = tab->n_zero;
303 dup->n_unbounded = tab->n_unbounded;
304 dup->basis = isl_mat_dup(tab->basis);
306 return dup;
307 error:
308 isl_tab_free(dup);
309 return NULL;
312 /* Construct the coefficient matrix of the product tableau
313 * of two tableaus.
314 * mat{1,2} is the coefficient matrix of tableau {1,2}
315 * row{1,2} is the number of rows in tableau {1,2}
316 * col{1,2} is the number of columns in tableau {1,2}
317 * off is the offset to the coefficient column (skipping the
318 * denominator, the constant term and the big parameter if any)
319 * r{1,2} is the number of redundant rows in tableau {1,2}
320 * d{1,2} is the number of dead columns in tableau {1,2}
322 * The order of the rows and columns in the result is as explained
323 * in isl_tab_product.
325 static struct isl_mat *tab_mat_product(struct isl_mat *mat1,
326 struct isl_mat *mat2, unsigned row1, unsigned row2,
327 unsigned col1, unsigned col2,
328 unsigned off, unsigned r1, unsigned r2, unsigned d1, unsigned d2)
330 int i;
331 struct isl_mat *prod;
332 unsigned n;
334 prod = isl_mat_alloc(mat1->ctx, mat1->n_row + mat2->n_row,
335 off + col1 + col2);
336 if (!prod)
337 return NULL;
339 n = 0;
340 for (i = 0; i < r1; ++i) {
341 isl_seq_cpy(prod->row[n + i], mat1->row[i], off + d1);
342 isl_seq_clr(prod->row[n + i] + off + d1, d2);
343 isl_seq_cpy(prod->row[n + i] + off + d1 + d2,
344 mat1->row[i] + off + d1, col1 - d1);
345 isl_seq_clr(prod->row[n + i] + off + col1 + d1, col2 - d2);
348 n += r1;
349 for (i = 0; i < r2; ++i) {
350 isl_seq_cpy(prod->row[n + i], mat2->row[i], off);
351 isl_seq_clr(prod->row[n + i] + off, d1);
352 isl_seq_cpy(prod->row[n + i] + off + d1,
353 mat2->row[i] + off, d2);
354 isl_seq_clr(prod->row[n + i] + off + d1 + d2, col1 - d1);
355 isl_seq_cpy(prod->row[n + i] + off + col1 + d1,
356 mat2->row[i] + off + d2, col2 - d2);
359 n += r2;
360 for (i = 0; i < row1 - r1; ++i) {
361 isl_seq_cpy(prod->row[n + i], mat1->row[r1 + i], off + d1);
362 isl_seq_clr(prod->row[n + i] + off + d1, d2);
363 isl_seq_cpy(prod->row[n + i] + off + d1 + d2,
364 mat1->row[r1 + i] + off + d1, col1 - d1);
365 isl_seq_clr(prod->row[n + i] + off + col1 + d1, col2 - d2);
368 n += row1 - r1;
369 for (i = 0; i < row2 - r2; ++i) {
370 isl_seq_cpy(prod->row[n + i], mat2->row[r2 + i], off);
371 isl_seq_clr(prod->row[n + i] + off, d1);
372 isl_seq_cpy(prod->row[n + i] + off + d1,
373 mat2->row[r2 + i] + off, d2);
374 isl_seq_clr(prod->row[n + i] + off + d1 + d2, col1 - d1);
375 isl_seq_cpy(prod->row[n + i] + off + col1 + d1,
376 mat2->row[r2 + i] + off + d2, col2 - d2);
379 return prod;
382 /* Update the row or column index of a variable that corresponds
383 * to a variable in the first input tableau.
385 static void update_index1(struct isl_tab_var *var,
386 unsigned r1, unsigned r2, unsigned d1, unsigned d2)
388 if (var->index == -1)
389 return;
390 if (var->is_row && var->index >= r1)
391 var->index += r2;
392 if (!var->is_row && var->index >= d1)
393 var->index += d2;
396 /* Update the row or column index of a variable that corresponds
397 * to a variable in the second input tableau.
399 static void update_index2(struct isl_tab_var *var,
400 unsigned row1, unsigned col1,
401 unsigned r1, unsigned r2, unsigned d1, unsigned d2)
403 if (var->index == -1)
404 return;
405 if (var->is_row) {
406 if (var->index < r2)
407 var->index += r1;
408 else
409 var->index += row1;
410 } else {
411 if (var->index < d2)
412 var->index += d1;
413 else
414 var->index += col1;
418 /* Create a tableau that represents the Cartesian product of the sets
419 * represented by tableaus tab1 and tab2.
420 * The order of the rows in the product is
421 * - redundant rows of tab1
422 * - redundant rows of tab2
423 * - non-redundant rows of tab1
424 * - non-redundant rows of tab2
425 * The order of the columns is
426 * - denominator
427 * - constant term
428 * - coefficient of big parameter, if any
429 * - dead columns of tab1
430 * - dead columns of tab2
431 * - live columns of tab1
432 * - live columns of tab2
433 * The order of the variables and the constraints is a concatenation
434 * of order in the two input tableaus.
436 struct isl_tab *isl_tab_product(struct isl_tab *tab1, struct isl_tab *tab2)
438 int i;
439 struct isl_tab *prod;
440 unsigned off;
441 unsigned r1, r2, d1, d2;
443 if (!tab1 || !tab2)
444 return NULL;
446 isl_assert(tab1->mat->ctx, tab1->M == tab2->M, return NULL);
447 isl_assert(tab1->mat->ctx, tab1->rational == tab2->rational, return NULL);
448 isl_assert(tab1->mat->ctx, tab1->cone == tab2->cone, return NULL);
449 isl_assert(tab1->mat->ctx, !tab1->row_sign, return NULL);
450 isl_assert(tab1->mat->ctx, !tab2->row_sign, return NULL);
451 isl_assert(tab1->mat->ctx, tab1->n_param == 0, return NULL);
452 isl_assert(tab1->mat->ctx, tab2->n_param == 0, return NULL);
453 isl_assert(tab1->mat->ctx, tab1->n_div == 0, return NULL);
454 isl_assert(tab1->mat->ctx, tab2->n_div == 0, return NULL);
456 off = 2 + tab1->M;
457 r1 = tab1->n_redundant;
458 r2 = tab2->n_redundant;
459 d1 = tab1->n_dead;
460 d2 = tab2->n_dead;
461 prod = isl_calloc_type(tab1->mat->ctx, struct isl_tab);
462 if (!prod)
463 return NULL;
464 prod->mat = tab_mat_product(tab1->mat, tab2->mat,
465 tab1->n_row, tab2->n_row,
466 tab1->n_col, tab2->n_col, off, r1, r2, d1, d2);
467 if (!prod->mat)
468 goto error;
469 prod->var = isl_alloc_array(tab1->mat->ctx, struct isl_tab_var,
470 tab1->max_var + tab2->max_var);
471 if ((tab1->max_var + tab2->max_var) && !prod->var)
472 goto error;
473 for (i = 0; i < tab1->n_var; ++i) {
474 prod->var[i] = tab1->var[i];
475 update_index1(&prod->var[i], r1, r2, d1, d2);
477 for (i = 0; i < tab2->n_var; ++i) {
478 prod->var[tab1->n_var + i] = tab2->var[i];
479 update_index2(&prod->var[tab1->n_var + i],
480 tab1->n_row, tab1->n_col,
481 r1, r2, d1, d2);
483 prod->con = isl_alloc_array(tab1->mat->ctx, struct isl_tab_var,
484 tab1->max_con + tab2->max_con);
485 if ((tab1->max_con + tab2->max_con) && !prod->con)
486 goto error;
487 for (i = 0; i < tab1->n_con; ++i) {
488 prod->con[i] = tab1->con[i];
489 update_index1(&prod->con[i], r1, r2, d1, d2);
491 for (i = 0; i < tab2->n_con; ++i) {
492 prod->con[tab1->n_con + i] = tab2->con[i];
493 update_index2(&prod->con[tab1->n_con + i],
494 tab1->n_row, tab1->n_col,
495 r1, r2, d1, d2);
497 prod->col_var = isl_alloc_array(tab1->mat->ctx, int,
498 tab1->n_col + tab2->n_col);
499 if ((tab1->n_col + tab2->n_col) && !prod->col_var)
500 goto error;
501 for (i = 0; i < tab1->n_col; ++i) {
502 int pos = i < d1 ? i : i + d2;
503 prod->col_var[pos] = tab1->col_var[i];
505 for (i = 0; i < tab2->n_col; ++i) {
506 int pos = i < d2 ? d1 + i : tab1->n_col + i;
507 int t = tab2->col_var[i];
508 if (t >= 0)
509 t += tab1->n_var;
510 else
511 t -= tab1->n_con;
512 prod->col_var[pos] = t;
514 prod->row_var = isl_alloc_array(tab1->mat->ctx, int,
515 tab1->mat->n_row + tab2->mat->n_row);
516 if ((tab1->mat->n_row + tab2->mat->n_row) && !prod->row_var)
517 goto error;
518 for (i = 0; i < tab1->n_row; ++i) {
519 int pos = i < r1 ? i : i + r2;
520 prod->row_var[pos] = tab1->row_var[i];
522 for (i = 0; i < tab2->n_row; ++i) {
523 int pos = i < r2 ? r1 + i : tab1->n_row + i;
524 int t = tab2->row_var[i];
525 if (t >= 0)
526 t += tab1->n_var;
527 else
528 t -= tab1->n_con;
529 prod->row_var[pos] = t;
531 prod->samples = NULL;
532 prod->sample_index = NULL;
533 prod->n_row = tab1->n_row + tab2->n_row;
534 prod->n_con = tab1->n_con + tab2->n_con;
535 prod->n_eq = 0;
536 prod->max_con = tab1->max_con + tab2->max_con;
537 prod->n_col = tab1->n_col + tab2->n_col;
538 prod->n_var = tab1->n_var + tab2->n_var;
539 prod->max_var = tab1->max_var + tab2->max_var;
540 prod->n_param = 0;
541 prod->n_div = 0;
542 prod->n_dead = tab1->n_dead + tab2->n_dead;
543 prod->n_redundant = tab1->n_redundant + tab2->n_redundant;
544 prod->rational = tab1->rational;
545 prod->empty = tab1->empty || tab2->empty;
546 prod->strict_redundant = tab1->strict_redundant || tab2->strict_redundant;
547 prod->need_undo = 0;
548 prod->in_undo = 0;
549 prod->M = tab1->M;
550 prod->cone = tab1->cone;
551 prod->bottom.type = isl_tab_undo_bottom;
552 prod->bottom.next = NULL;
553 prod->top = &prod->bottom;
555 prod->n_zero = 0;
556 prod->n_unbounded = 0;
557 prod->basis = NULL;
559 return prod;
560 error:
561 isl_tab_free(prod);
562 return NULL;
565 static struct isl_tab_var *var_from_index(struct isl_tab *tab, int i)
567 if (i >= 0)
568 return &tab->var[i];
569 else
570 return &tab->con[~i];
573 struct isl_tab_var *isl_tab_var_from_row(struct isl_tab *tab, int i)
575 return var_from_index(tab, tab->row_var[i]);
578 static struct isl_tab_var *var_from_col(struct isl_tab *tab, int i)
580 return var_from_index(tab, tab->col_var[i]);
583 /* Check if there are any upper bounds on column variable "var",
584 * i.e., non-negative rows where var appears with a negative coefficient.
585 * Return 1 if there are no such bounds.
587 static int max_is_manifestly_unbounded(struct isl_tab *tab,
588 struct isl_tab_var *var)
590 int i;
591 unsigned off = 2 + tab->M;
593 if (var->is_row)
594 return 0;
595 for (i = tab->n_redundant; i < tab->n_row; ++i) {
596 if (!isl_int_is_neg(tab->mat->row[i][off + var->index]))
597 continue;
598 if (isl_tab_var_from_row(tab, i)->is_nonneg)
599 return 0;
601 return 1;
604 /* Check if there are any lower bounds on column variable "var",
605 * i.e., non-negative rows where var appears with a positive coefficient.
606 * Return 1 if there are no such bounds.
608 static int min_is_manifestly_unbounded(struct isl_tab *tab,
609 struct isl_tab_var *var)
611 int i;
612 unsigned off = 2 + tab->M;
614 if (var->is_row)
615 return 0;
616 for (i = tab->n_redundant; i < tab->n_row; ++i) {
617 if (!isl_int_is_pos(tab->mat->row[i][off + var->index]))
618 continue;
619 if (isl_tab_var_from_row(tab, i)->is_nonneg)
620 return 0;
622 return 1;
625 static int row_cmp(struct isl_tab *tab, int r1, int r2, int c, isl_int t)
627 unsigned off = 2 + tab->M;
629 if (tab->M) {
630 int s;
631 isl_int_mul(t, tab->mat->row[r1][2], tab->mat->row[r2][off+c]);
632 isl_int_submul(t, tab->mat->row[r2][2], tab->mat->row[r1][off+c]);
633 s = isl_int_sgn(t);
634 if (s)
635 return s;
637 isl_int_mul(t, tab->mat->row[r1][1], tab->mat->row[r2][off + c]);
638 isl_int_submul(t, tab->mat->row[r2][1], tab->mat->row[r1][off + c]);
639 return isl_int_sgn(t);
642 /* Given the index of a column "c", return the index of a row
643 * that can be used to pivot the column in, with either an increase
644 * (sgn > 0) or a decrease (sgn < 0) of the corresponding variable.
645 * If "var" is not NULL, then the row returned will be different from
646 * the one associated with "var".
648 * Each row in the tableau is of the form
650 * x_r = a_r0 + \sum_i a_ri x_i
652 * Only rows with x_r >= 0 and with the sign of a_ri opposite to "sgn"
653 * impose any limit on the increase or decrease in the value of x_c
654 * and this bound is equal to a_r0 / |a_rc|. We are therefore looking
655 * for the row with the smallest (most stringent) such bound.
656 * Note that the common denominator of each row drops out of the fraction.
657 * To check if row j has a smaller bound than row r, i.e.,
658 * a_j0 / |a_jc| < a_r0 / |a_rc| or a_j0 |a_rc| < a_r0 |a_jc|,
659 * we check if -sign(a_jc) (a_j0 a_rc - a_r0 a_jc) < 0,
660 * where -sign(a_jc) is equal to "sgn".
662 static int pivot_row(struct isl_tab *tab,
663 struct isl_tab_var *var, int sgn, int c)
665 int j, r, tsgn;
666 isl_int t;
667 unsigned off = 2 + tab->M;
669 isl_int_init(t);
670 r = -1;
671 for (j = tab->n_redundant; j < tab->n_row; ++j) {
672 if (var && j == var->index)
673 continue;
674 if (!isl_tab_var_from_row(tab, j)->is_nonneg)
675 continue;
676 if (sgn * isl_int_sgn(tab->mat->row[j][off + c]) >= 0)
677 continue;
678 if (r < 0) {
679 r = j;
680 continue;
682 tsgn = sgn * row_cmp(tab, r, j, c, t);
683 if (tsgn < 0 || (tsgn == 0 &&
684 tab->row_var[j] < tab->row_var[r]))
685 r = j;
687 isl_int_clear(t);
688 return r;
691 /* Find a pivot (row and col) that will increase (sgn > 0) or decrease
692 * (sgn < 0) the value of row variable var.
693 * If not NULL, then skip_var is a row variable that should be ignored
694 * while looking for a pivot row. It is usually equal to var.
696 * As the given row in the tableau is of the form
698 * x_r = a_r0 + \sum_i a_ri x_i
700 * we need to find a column such that the sign of a_ri is equal to "sgn"
701 * (such that an increase in x_i will have the desired effect) or a
702 * column with a variable that may attain negative values.
703 * If a_ri is positive, then we need to move x_i in the same direction
704 * to obtain the desired effect. Otherwise, x_i has to move in the
705 * opposite direction.
707 static void find_pivot(struct isl_tab *tab,
708 struct isl_tab_var *var, struct isl_tab_var *skip_var,
709 int sgn, int *row, int *col)
711 int j, r, c;
712 isl_int *tr;
714 *row = *col = -1;
716 isl_assert(tab->mat->ctx, var->is_row, return);
717 tr = tab->mat->row[var->index] + 2 + tab->M;
719 c = -1;
720 for (j = tab->n_dead; j < tab->n_col; ++j) {
721 if (isl_int_is_zero(tr[j]))
722 continue;
723 if (isl_int_sgn(tr[j]) != sgn &&
724 var_from_col(tab, j)->is_nonneg)
725 continue;
726 if (c < 0 || tab->col_var[j] < tab->col_var[c])
727 c = j;
729 if (c < 0)
730 return;
732 sgn *= isl_int_sgn(tr[c]);
733 r = pivot_row(tab, skip_var, sgn, c);
734 *row = r < 0 ? var->index : r;
735 *col = c;
738 /* Return 1 if row "row" represents an obviously redundant inequality.
739 * This means
740 * - it represents an inequality or a variable
741 * - that is the sum of a non-negative sample value and a positive
742 * combination of zero or more non-negative constraints.
744 int isl_tab_row_is_redundant(struct isl_tab *tab, int row)
746 int i;
747 unsigned off = 2 + tab->M;
749 if (tab->row_var[row] < 0 && !isl_tab_var_from_row(tab, row)->is_nonneg)
750 return 0;
752 if (isl_int_is_neg(tab->mat->row[row][1]))
753 return 0;
754 if (tab->strict_redundant && isl_int_is_zero(tab->mat->row[row][1]))
755 return 0;
756 if (tab->M && isl_int_is_neg(tab->mat->row[row][2]))
757 return 0;
759 for (i = tab->n_dead; i < tab->n_col; ++i) {
760 if (isl_int_is_zero(tab->mat->row[row][off + i]))
761 continue;
762 if (tab->col_var[i] >= 0)
763 return 0;
764 if (isl_int_is_neg(tab->mat->row[row][off + i]))
765 return 0;
766 if (!var_from_col(tab, i)->is_nonneg)
767 return 0;
769 return 1;
772 static void swap_rows(struct isl_tab *tab, int row1, int row2)
774 int t;
775 enum isl_tab_row_sign s;
777 t = tab->row_var[row1];
778 tab->row_var[row1] = tab->row_var[row2];
779 tab->row_var[row2] = t;
780 isl_tab_var_from_row(tab, row1)->index = row1;
781 isl_tab_var_from_row(tab, row2)->index = row2;
782 tab->mat = isl_mat_swap_rows(tab->mat, row1, row2);
784 if (!tab->row_sign)
785 return;
786 s = tab->row_sign[row1];
787 tab->row_sign[row1] = tab->row_sign[row2];
788 tab->row_sign[row2] = s;
791 static int push_union(struct isl_tab *tab,
792 enum isl_tab_undo_type type, union isl_tab_undo_val u) WARN_UNUSED;
793 static int push_union(struct isl_tab *tab,
794 enum isl_tab_undo_type type, union isl_tab_undo_val u)
796 struct isl_tab_undo *undo;
798 if (!tab)
799 return -1;
800 if (!tab->need_undo)
801 return 0;
803 undo = isl_alloc_type(tab->mat->ctx, struct isl_tab_undo);
804 if (!undo)
805 return -1;
806 undo->type = type;
807 undo->u = u;
808 undo->next = tab->top;
809 tab->top = undo;
811 return 0;
814 int isl_tab_push_var(struct isl_tab *tab,
815 enum isl_tab_undo_type type, struct isl_tab_var *var)
817 union isl_tab_undo_val u;
818 if (var->is_row)
819 u.var_index = tab->row_var[var->index];
820 else
821 u.var_index = tab->col_var[var->index];
822 return push_union(tab, type, u);
825 int isl_tab_push(struct isl_tab *tab, enum isl_tab_undo_type type)
827 union isl_tab_undo_val u = { 0 };
828 return push_union(tab, type, u);
831 /* Push a record on the undo stack describing the current basic
832 * variables, so that the this state can be restored during rollback.
834 int isl_tab_push_basis(struct isl_tab *tab)
836 int i;
837 union isl_tab_undo_val u;
839 u.col_var = isl_alloc_array(tab->mat->ctx, int, tab->n_col);
840 if (tab->n_col && !u.col_var)
841 return -1;
842 for (i = 0; i < tab->n_col; ++i)
843 u.col_var[i] = tab->col_var[i];
844 return push_union(tab, isl_tab_undo_saved_basis, u);
847 int isl_tab_push_callback(struct isl_tab *tab, struct isl_tab_callback *callback)
849 union isl_tab_undo_val u;
850 u.callback = callback;
851 return push_union(tab, isl_tab_undo_callback, u);
854 struct isl_tab *isl_tab_init_samples(struct isl_tab *tab)
856 if (!tab)
857 return NULL;
859 tab->n_sample = 0;
860 tab->n_outside = 0;
861 tab->samples = isl_mat_alloc(tab->mat->ctx, 1, 1 + tab->n_var);
862 if (!tab->samples)
863 goto error;
864 tab->sample_index = isl_alloc_array(tab->mat->ctx, int, 1);
865 if (!tab->sample_index)
866 goto error;
867 return tab;
868 error:
869 isl_tab_free(tab);
870 return NULL;
873 struct isl_tab *isl_tab_add_sample(struct isl_tab *tab,
874 __isl_take isl_vec *sample)
876 if (!tab || !sample)
877 goto error;
879 if (tab->n_sample + 1 > tab->samples->n_row) {
880 int *t = isl_realloc_array(tab->mat->ctx,
881 tab->sample_index, int, tab->n_sample + 1);
882 if (!t)
883 goto error;
884 tab->sample_index = t;
887 tab->samples = isl_mat_extend(tab->samples,
888 tab->n_sample + 1, tab->samples->n_col);
889 if (!tab->samples)
890 goto error;
892 isl_seq_cpy(tab->samples->row[tab->n_sample], sample->el, sample->size);
893 isl_vec_free(sample);
894 tab->sample_index[tab->n_sample] = tab->n_sample;
895 tab->n_sample++;
897 return tab;
898 error:
899 isl_vec_free(sample);
900 isl_tab_free(tab);
901 return NULL;
904 struct isl_tab *isl_tab_drop_sample(struct isl_tab *tab, int s)
906 if (s != tab->n_outside) {
907 int t = tab->sample_index[tab->n_outside];
908 tab->sample_index[tab->n_outside] = tab->sample_index[s];
909 tab->sample_index[s] = t;
910 isl_mat_swap_rows(tab->samples, tab->n_outside, s);
912 tab->n_outside++;
913 if (isl_tab_push(tab, isl_tab_undo_drop_sample) < 0) {
914 isl_tab_free(tab);
915 return NULL;
918 return tab;
921 /* Record the current number of samples so that we can remove newer
922 * samples during a rollback.
924 int isl_tab_save_samples(struct isl_tab *tab)
926 union isl_tab_undo_val u;
928 if (!tab)
929 return -1;
931 u.n = tab->n_sample;
932 return push_union(tab, isl_tab_undo_saved_samples, u);
935 /* Mark row with index "row" as being redundant.
936 * If we may need to undo the operation or if the row represents
937 * a variable of the original problem, the row is kept,
938 * but no longer considered when looking for a pivot row.
939 * Otherwise, the row is simply removed.
941 * The row may be interchanged with some other row. If it
942 * is interchanged with a later row, return 1. Otherwise return 0.
943 * If the rows are checked in order in the calling function,
944 * then a return value of 1 means that the row with the given
945 * row number may now contain a different row that hasn't been checked yet.
947 int isl_tab_mark_redundant(struct isl_tab *tab, int row)
949 struct isl_tab_var *var = isl_tab_var_from_row(tab, row);
950 var->is_redundant = 1;
951 isl_assert(tab->mat->ctx, row >= tab->n_redundant, return -1);
952 if (tab->preserve || tab->need_undo || tab->row_var[row] >= 0) {
953 if (tab->row_var[row] >= 0 && !var->is_nonneg) {
954 var->is_nonneg = 1;
955 if (isl_tab_push_var(tab, isl_tab_undo_nonneg, var) < 0)
956 return -1;
958 if (row != tab->n_redundant)
959 swap_rows(tab, row, tab->n_redundant);
960 tab->n_redundant++;
961 return isl_tab_push_var(tab, isl_tab_undo_redundant, var);
962 } else {
963 if (row != tab->n_row - 1)
964 swap_rows(tab, row, tab->n_row - 1);
965 isl_tab_var_from_row(tab, tab->n_row - 1)->index = -1;
966 tab->n_row--;
967 return 1;
971 int isl_tab_mark_empty(struct isl_tab *tab)
973 if (!tab)
974 return -1;
975 if (!tab->empty && tab->need_undo)
976 if (isl_tab_push(tab, isl_tab_undo_empty) < 0)
977 return -1;
978 tab->empty = 1;
979 return 0;
982 int isl_tab_freeze_constraint(struct isl_tab *tab, int con)
984 struct isl_tab_var *var;
986 if (!tab)
987 return -1;
989 var = &tab->con[con];
990 if (var->frozen)
991 return 0;
992 if (var->index < 0)
993 return 0;
994 var->frozen = 1;
996 if (tab->need_undo)
997 return isl_tab_push_var(tab, isl_tab_undo_freeze, var);
999 return 0;
1002 /* Update the rows signs after a pivot of "row" and "col", with "row_sgn"
1003 * the original sign of the pivot element.
1004 * We only keep track of row signs during PILP solving and in this case
1005 * we only pivot a row with negative sign (meaning the value is always
1006 * non-positive) using a positive pivot element.
1008 * For each row j, the new value of the parametric constant is equal to
1010 * a_j0 - a_jc a_r0/a_rc
1012 * where a_j0 is the original parametric constant, a_rc is the pivot element,
1013 * a_r0 is the parametric constant of the pivot row and a_jc is the
1014 * pivot column entry of the row j.
1015 * Since a_r0 is non-positive and a_rc is positive, the sign of row j
1016 * remains the same if a_jc has the same sign as the row j or if
1017 * a_jc is zero. In all other cases, we reset the sign to "unknown".
1019 static void update_row_sign(struct isl_tab *tab, int row, int col, int row_sgn)
1021 int i;
1022 struct isl_mat *mat = tab->mat;
1023 unsigned off = 2 + tab->M;
1025 if (!tab->row_sign)
1026 return;
1028 if (tab->row_sign[row] == 0)
1029 return;
1030 isl_assert(mat->ctx, row_sgn > 0, return);
1031 isl_assert(mat->ctx, tab->row_sign[row] == isl_tab_row_neg, return);
1032 tab->row_sign[row] = isl_tab_row_pos;
1033 for (i = 0; i < tab->n_row; ++i) {
1034 int s;
1035 if (i == row)
1036 continue;
1037 s = isl_int_sgn(mat->row[i][off + col]);
1038 if (!s)
1039 continue;
1040 if (!tab->row_sign[i])
1041 continue;
1042 if (s < 0 && tab->row_sign[i] == isl_tab_row_neg)
1043 continue;
1044 if (s > 0 && tab->row_sign[i] == isl_tab_row_pos)
1045 continue;
1046 tab->row_sign[i] = isl_tab_row_unknown;
1050 /* Given a row number "row" and a column number "col", pivot the tableau
1051 * such that the associated variables are interchanged.
1052 * The given row in the tableau expresses
1054 * x_r = a_r0 + \sum_i a_ri x_i
1056 * or
1058 * x_c = 1/a_rc x_r - a_r0/a_rc + sum_{i \ne r} -a_ri/a_rc
1060 * Substituting this equality into the other rows
1062 * x_j = a_j0 + \sum_i a_ji x_i
1064 * with a_jc \ne 0, we obtain
1066 * 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
1068 * The tableau
1070 * n_rc/d_r n_ri/d_r
1071 * n_jc/d_j n_ji/d_j
1073 * where i is any other column and j is any other row,
1074 * is therefore transformed into
1076 * s(n_rc)d_r/|n_rc| -s(n_rc)n_ri/|n_rc|
1077 * 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)
1079 * The transformation is performed along the following steps
1081 * d_r/n_rc n_ri/n_rc
1082 * n_jc/d_j n_ji/d_j
1084 * s(n_rc)d_r/|n_rc| -s(n_rc)n_ri/|n_rc|
1085 * n_jc/d_j n_ji/d_j
1087 * s(n_rc)d_r/|n_rc| -s(n_rc)n_ri/|n_rc|
1088 * n_jc/(|n_rc| d_j) n_ji/(|n_rc| d_j)
1090 * s(n_rc)d_r/|n_rc| -s(n_rc)n_ri/|n_rc|
1091 * n_jc/(|n_rc| d_j) (n_ji |n_rc|)/(|n_rc| d_j)
1093 * s(n_rc)d_r/|n_rc| -s(n_rc)n_ri/|n_rc|
1094 * n_jc/(|n_rc| d_j) (n_ji |n_rc| - s(n_rc)n_jc n_ri)/(|n_rc| d_j)
1096 * s(n_rc)d_r/|n_rc| -s(n_rc)n_ri/|n_rc|
1097 * 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)
1100 int isl_tab_pivot(struct isl_tab *tab, int row, int col)
1102 int i, j;
1103 int sgn;
1104 int t;
1105 isl_ctx *ctx;
1106 struct isl_mat *mat = tab->mat;
1107 struct isl_tab_var *var;
1108 unsigned off = 2 + tab->M;
1110 ctx = isl_tab_get_ctx(tab);
1111 if (ctx->abort) {
1112 isl_ctx_set_error(ctx, isl_error_abort);
1113 return -1;
1115 if (ctx->max_operations && ctx->operations >= ctx->max_operations)
1116 isl_die(ctx, isl_error_quota,
1117 "maximal number of operations exceeded", return -1);
1118 ctx->operations++;
1120 isl_int_swap(mat->row[row][0], mat->row[row][off + col]);
1121 sgn = isl_int_sgn(mat->row[row][0]);
1122 if (sgn < 0) {
1123 isl_int_neg(mat->row[row][0], mat->row[row][0]);
1124 isl_int_neg(mat->row[row][off + col], mat->row[row][off + col]);
1125 } else
1126 for (j = 0; j < off - 1 + tab->n_col; ++j) {
1127 if (j == off - 1 + col)
1128 continue;
1129 isl_int_neg(mat->row[row][1 + j], mat->row[row][1 + j]);
1131 if (!isl_int_is_one(mat->row[row][0]))
1132 isl_seq_normalize(mat->ctx, mat->row[row], off + tab->n_col);
1133 for (i = 0; i < tab->n_row; ++i) {
1134 if (i == row)
1135 continue;
1136 if (isl_int_is_zero(mat->row[i][off + col]))
1137 continue;
1138 isl_int_mul(mat->row[i][0], mat->row[i][0], mat->row[row][0]);
1139 for (j = 0; j < off - 1 + tab->n_col; ++j) {
1140 if (j == off - 1 + col)
1141 continue;
1142 isl_int_mul(mat->row[i][1 + j],
1143 mat->row[i][1 + j], mat->row[row][0]);
1144 isl_int_addmul(mat->row[i][1 + j],
1145 mat->row[i][off + col], mat->row[row][1 + j]);
1147 isl_int_mul(mat->row[i][off + col],
1148 mat->row[i][off + col], mat->row[row][off + col]);
1149 if (!isl_int_is_one(mat->row[i][0]))
1150 isl_seq_normalize(mat->ctx, mat->row[i], off + tab->n_col);
1152 t = tab->row_var[row];
1153 tab->row_var[row] = tab->col_var[col];
1154 tab->col_var[col] = t;
1155 var = isl_tab_var_from_row(tab, row);
1156 var->is_row = 1;
1157 var->index = row;
1158 var = var_from_col(tab, col);
1159 var->is_row = 0;
1160 var->index = col;
1161 update_row_sign(tab, row, col, sgn);
1162 if (tab->in_undo)
1163 return 0;
1164 for (i = tab->n_redundant; i < tab->n_row; ++i) {
1165 if (isl_int_is_zero(mat->row[i][off + col]))
1166 continue;
1167 if (!isl_tab_var_from_row(tab, i)->frozen &&
1168 isl_tab_row_is_redundant(tab, i)) {
1169 int redo = isl_tab_mark_redundant(tab, i);
1170 if (redo < 0)
1171 return -1;
1172 if (redo)
1173 --i;
1176 return 0;
1179 /* If "var" represents a column variable, then pivot is up (sgn > 0)
1180 * or down (sgn < 0) to a row. The variable is assumed not to be
1181 * unbounded in the specified direction.
1182 * If sgn = 0, then the variable is unbounded in both directions,
1183 * and we pivot with any row we can find.
1185 static int to_row(struct isl_tab *tab, struct isl_tab_var *var, int sign) WARN_UNUSED;
1186 static int to_row(struct isl_tab *tab, struct isl_tab_var *var, int sign)
1188 int r;
1189 unsigned off = 2 + tab->M;
1191 if (var->is_row)
1192 return 0;
1194 if (sign == 0) {
1195 for (r = tab->n_redundant; r < tab->n_row; ++r)
1196 if (!isl_int_is_zero(tab->mat->row[r][off+var->index]))
1197 break;
1198 isl_assert(tab->mat->ctx, r < tab->n_row, return -1);
1199 } else {
1200 r = pivot_row(tab, NULL, sign, var->index);
1201 isl_assert(tab->mat->ctx, r >= 0, return -1);
1204 return isl_tab_pivot(tab, r, var->index);
1207 /* Check whether all variables that are marked as non-negative
1208 * also have a non-negative sample value. This function is not
1209 * called from the current code but is useful during debugging.
1211 static void check_table(struct isl_tab *tab) __attribute__ ((unused));
1212 static void check_table(struct isl_tab *tab)
1214 int i;
1216 if (tab->empty)
1217 return;
1218 for (i = tab->n_redundant; i < tab->n_row; ++i) {
1219 struct isl_tab_var *var;
1220 var = isl_tab_var_from_row(tab, i);
1221 if (!var->is_nonneg)
1222 continue;
1223 if (tab->M) {
1224 isl_assert(tab->mat->ctx,
1225 !isl_int_is_neg(tab->mat->row[i][2]), abort());
1226 if (isl_int_is_pos(tab->mat->row[i][2]))
1227 continue;
1229 isl_assert(tab->mat->ctx, !isl_int_is_neg(tab->mat->row[i][1]),
1230 abort());
1234 /* Return the sign of the maximal value of "var".
1235 * If the sign is not negative, then on return from this function,
1236 * the sample value will also be non-negative.
1238 * If "var" is manifestly unbounded wrt positive values, we are done.
1239 * Otherwise, we pivot the variable up to a row if needed
1240 * Then we continue pivoting down until either
1241 * - no more down pivots can be performed
1242 * - the sample value is positive
1243 * - the variable is pivoted into a manifestly unbounded column
1245 static int sign_of_max(struct isl_tab *tab, struct isl_tab_var *var)
1247 int row, col;
1249 if (max_is_manifestly_unbounded(tab, var))
1250 return 1;
1251 if (to_row(tab, var, 1) < 0)
1252 return -2;
1253 while (!isl_int_is_pos(tab->mat->row[var->index][1])) {
1254 find_pivot(tab, var, var, 1, &row, &col);
1255 if (row == -1)
1256 return isl_int_sgn(tab->mat->row[var->index][1]);
1257 if (isl_tab_pivot(tab, row, col) < 0)
1258 return -2;
1259 if (!var->is_row) /* manifestly unbounded */
1260 return 1;
1262 return 1;
1265 int isl_tab_sign_of_max(struct isl_tab *tab, int con)
1267 struct isl_tab_var *var;
1269 if (!tab)
1270 return -2;
1272 var = &tab->con[con];
1273 isl_assert(tab->mat->ctx, !var->is_redundant, return -2);
1274 isl_assert(tab->mat->ctx, !var->is_zero, return -2);
1276 return sign_of_max(tab, var);
1279 static int row_is_neg(struct isl_tab *tab, int row)
1281 if (!tab->M)
1282 return isl_int_is_neg(tab->mat->row[row][1]);
1283 if (isl_int_is_pos(tab->mat->row[row][2]))
1284 return 0;
1285 if (isl_int_is_neg(tab->mat->row[row][2]))
1286 return 1;
1287 return isl_int_is_neg(tab->mat->row[row][1]);
1290 static int row_sgn(struct isl_tab *tab, int row)
1292 if (!tab->M)
1293 return isl_int_sgn(tab->mat->row[row][1]);
1294 if (!isl_int_is_zero(tab->mat->row[row][2]))
1295 return isl_int_sgn(tab->mat->row[row][2]);
1296 else
1297 return isl_int_sgn(tab->mat->row[row][1]);
1300 /* Perform pivots until the row variable "var" has a non-negative
1301 * sample value or until no more upward pivots can be performed.
1302 * Return the sign of the sample value after the pivots have been
1303 * performed.
1305 static int restore_row(struct isl_tab *tab, struct isl_tab_var *var)
1307 int row, col;
1309 while (row_is_neg(tab, var->index)) {
1310 find_pivot(tab, var, var, 1, &row, &col);
1311 if (row == -1)
1312 break;
1313 if (isl_tab_pivot(tab, row, col) < 0)
1314 return -2;
1315 if (!var->is_row) /* manifestly unbounded */
1316 return 1;
1318 return row_sgn(tab, var->index);
1321 /* Perform pivots until we are sure that the row variable "var"
1322 * can attain non-negative values. After return from this
1323 * function, "var" is still a row variable, but its sample
1324 * value may not be non-negative, even if the function returns 1.
1326 static int at_least_zero(struct isl_tab *tab, struct isl_tab_var *var)
1328 int row, col;
1330 while (isl_int_is_neg(tab->mat->row[var->index][1])) {
1331 find_pivot(tab, var, var, 1, &row, &col);
1332 if (row == -1)
1333 break;
1334 if (row == var->index) /* manifestly unbounded */
1335 return 1;
1336 if (isl_tab_pivot(tab, row, col) < 0)
1337 return -1;
1339 return !isl_int_is_neg(tab->mat->row[var->index][1]);
1342 /* Return a negative value if "var" can attain negative values.
1343 * Return a non-negative value otherwise.
1345 * If "var" is manifestly unbounded wrt negative values, we are done.
1346 * Otherwise, if var is in a column, we can pivot it down to a row.
1347 * Then we continue pivoting down until either
1348 * - the pivot would result in a manifestly unbounded column
1349 * => we don't perform the pivot, but simply return -1
1350 * - no more down pivots can be performed
1351 * - the sample value is negative
1352 * If the sample value becomes negative and the variable is supposed
1353 * to be nonnegative, then we undo the last pivot.
1354 * However, if the last pivot has made the pivoting variable
1355 * obviously redundant, then it may have moved to another row.
1356 * In that case we look for upward pivots until we reach a non-negative
1357 * value again.
1359 static int sign_of_min(struct isl_tab *tab, struct isl_tab_var *var)
1361 int row, col;
1362 struct isl_tab_var *pivot_var = NULL;
1364 if (min_is_manifestly_unbounded(tab, var))
1365 return -1;
1366 if (!var->is_row) {
1367 col = var->index;
1368 row = pivot_row(tab, NULL, -1, col);
1369 pivot_var = var_from_col(tab, col);
1370 if (isl_tab_pivot(tab, row, col) < 0)
1371 return -2;
1372 if (var->is_redundant)
1373 return 0;
1374 if (isl_int_is_neg(tab->mat->row[var->index][1])) {
1375 if (var->is_nonneg) {
1376 if (!pivot_var->is_redundant &&
1377 pivot_var->index == row) {
1378 if (isl_tab_pivot(tab, row, col) < 0)
1379 return -2;
1380 } else
1381 if (restore_row(tab, var) < -1)
1382 return -2;
1384 return -1;
1387 if (var->is_redundant)
1388 return 0;
1389 while (!isl_int_is_neg(tab->mat->row[var->index][1])) {
1390 find_pivot(tab, var, var, -1, &row, &col);
1391 if (row == var->index)
1392 return -1;
1393 if (row == -1)
1394 return isl_int_sgn(tab->mat->row[var->index][1]);
1395 pivot_var = var_from_col(tab, col);
1396 if (isl_tab_pivot(tab, row, col) < 0)
1397 return -2;
1398 if (var->is_redundant)
1399 return 0;
1401 if (pivot_var && var->is_nonneg) {
1402 /* pivot back to non-negative value */
1403 if (!pivot_var->is_redundant && pivot_var->index == row) {
1404 if (isl_tab_pivot(tab, row, col) < 0)
1405 return -2;
1406 } else
1407 if (restore_row(tab, var) < -1)
1408 return -2;
1410 return -1;
1413 static int row_at_most_neg_one(struct isl_tab *tab, int row)
1415 if (tab->M) {
1416 if (isl_int_is_pos(tab->mat->row[row][2]))
1417 return 0;
1418 if (isl_int_is_neg(tab->mat->row[row][2]))
1419 return 1;
1421 return isl_int_is_neg(tab->mat->row[row][1]) &&
1422 isl_int_abs_ge(tab->mat->row[row][1],
1423 tab->mat->row[row][0]);
1426 /* Return 1 if "var" can attain values <= -1.
1427 * Return 0 otherwise.
1429 * The sample value of "var" is assumed to be non-negative when the
1430 * the function is called. If 1 is returned then the constraint
1431 * is not redundant and the sample value is made non-negative again before
1432 * the function returns.
1434 int isl_tab_min_at_most_neg_one(struct isl_tab *tab, struct isl_tab_var *var)
1436 int row, col;
1437 struct isl_tab_var *pivot_var;
1439 if (min_is_manifestly_unbounded(tab, var))
1440 return 1;
1441 if (!var->is_row) {
1442 col = var->index;
1443 row = pivot_row(tab, NULL, -1, col);
1444 pivot_var = var_from_col(tab, col);
1445 if (isl_tab_pivot(tab, row, col) < 0)
1446 return -1;
1447 if (var->is_redundant)
1448 return 0;
1449 if (row_at_most_neg_one(tab, var->index)) {
1450 if (var->is_nonneg) {
1451 if (!pivot_var->is_redundant &&
1452 pivot_var->index == row) {
1453 if (isl_tab_pivot(tab, row, col) < 0)
1454 return -1;
1455 } else
1456 if (restore_row(tab, var) < -1)
1457 return -1;
1459 return 1;
1462 if (var->is_redundant)
1463 return 0;
1464 do {
1465 find_pivot(tab, var, var, -1, &row, &col);
1466 if (row == var->index) {
1467 if (restore_row(tab, var) < -1)
1468 return -1;
1469 return 1;
1471 if (row == -1)
1472 return 0;
1473 pivot_var = var_from_col(tab, col);
1474 if (isl_tab_pivot(tab, row, col) < 0)
1475 return -1;
1476 if (var->is_redundant)
1477 return 0;
1478 } while (!row_at_most_neg_one(tab, var->index));
1479 if (var->is_nonneg) {
1480 /* pivot back to non-negative value */
1481 if (!pivot_var->is_redundant && pivot_var->index == row)
1482 if (isl_tab_pivot(tab, row, col) < 0)
1483 return -1;
1484 if (restore_row(tab, var) < -1)
1485 return -1;
1487 return 1;
1490 /* Return 1 if "var" can attain values >= 1.
1491 * Return 0 otherwise.
1493 static int at_least_one(struct isl_tab *tab, struct isl_tab_var *var)
1495 int row, col;
1496 isl_int *r;
1498 if (max_is_manifestly_unbounded(tab, var))
1499 return 1;
1500 if (to_row(tab, var, 1) < 0)
1501 return -1;
1502 r = tab->mat->row[var->index];
1503 while (isl_int_lt(r[1], r[0])) {
1504 find_pivot(tab, var, var, 1, &row, &col);
1505 if (row == -1)
1506 return isl_int_ge(r[1], r[0]);
1507 if (row == var->index) /* manifestly unbounded */
1508 return 1;
1509 if (isl_tab_pivot(tab, row, col) < 0)
1510 return -1;
1512 return 1;
1515 static void swap_cols(struct isl_tab *tab, int col1, int col2)
1517 int t;
1518 unsigned off = 2 + tab->M;
1519 t = tab->col_var[col1];
1520 tab->col_var[col1] = tab->col_var[col2];
1521 tab->col_var[col2] = t;
1522 var_from_col(tab, col1)->index = col1;
1523 var_from_col(tab, col2)->index = col2;
1524 tab->mat = isl_mat_swap_cols(tab->mat, off + col1, off + col2);
1527 /* Mark column with index "col" as representing a zero variable.
1528 * If we may need to undo the operation the column is kept,
1529 * but no longer considered.
1530 * Otherwise, the column is simply removed.
1532 * The column may be interchanged with some other column. If it
1533 * is interchanged with a later column, return 1. Otherwise return 0.
1534 * If the columns are checked in order in the calling function,
1535 * then a return value of 1 means that the column with the given
1536 * column number may now contain a different column that
1537 * hasn't been checked yet.
1539 int isl_tab_kill_col(struct isl_tab *tab, int col)
1541 var_from_col(tab, col)->is_zero = 1;
1542 if (tab->need_undo) {
1543 if (isl_tab_push_var(tab, isl_tab_undo_zero,
1544 var_from_col(tab, col)) < 0)
1545 return -1;
1546 if (col != tab->n_dead)
1547 swap_cols(tab, col, tab->n_dead);
1548 tab->n_dead++;
1549 return 0;
1550 } else {
1551 if (col != tab->n_col - 1)
1552 swap_cols(tab, col, tab->n_col - 1);
1553 var_from_col(tab, tab->n_col - 1)->index = -1;
1554 tab->n_col--;
1555 return 1;
1559 static int row_is_manifestly_non_integral(struct isl_tab *tab, int row)
1561 unsigned off = 2 + tab->M;
1563 if (tab->M && !isl_int_eq(tab->mat->row[row][2],
1564 tab->mat->row[row][0]))
1565 return 0;
1566 if (isl_seq_first_non_zero(tab->mat->row[row] + off + tab->n_dead,
1567 tab->n_col - tab->n_dead) != -1)
1568 return 0;
1570 return !isl_int_is_divisible_by(tab->mat->row[row][1],
1571 tab->mat->row[row][0]);
1574 /* For integer tableaus, check if any of the coordinates are stuck
1575 * at a non-integral value.
1577 static int tab_is_manifestly_empty(struct isl_tab *tab)
1579 int i;
1581 if (tab->empty)
1582 return 1;
1583 if (tab->rational)
1584 return 0;
1586 for (i = 0; i < tab->n_var; ++i) {
1587 if (!tab->var[i].is_row)
1588 continue;
1589 if (row_is_manifestly_non_integral(tab, tab->var[i].index))
1590 return 1;
1593 return 0;
1596 /* Row variable "var" is non-negative and cannot attain any values
1597 * larger than zero. This means that the coefficients of the unrestricted
1598 * column variables are zero and that the coefficients of the non-negative
1599 * column variables are zero or negative.
1600 * Each of the non-negative variables with a negative coefficient can
1601 * then also be written as the negative sum of non-negative variables
1602 * and must therefore also be zero.
1604 static int close_row(struct isl_tab *tab, struct isl_tab_var *var) WARN_UNUSED;
1605 static int close_row(struct isl_tab *tab, struct isl_tab_var *var)
1607 int j;
1608 struct isl_mat *mat = tab->mat;
1609 unsigned off = 2 + tab->M;
1611 isl_assert(tab->mat->ctx, var->is_nonneg, return -1);
1612 var->is_zero = 1;
1613 if (tab->need_undo)
1614 if (isl_tab_push_var(tab, isl_tab_undo_zero, var) < 0)
1615 return -1;
1616 for (j = tab->n_dead; j < tab->n_col; ++j) {
1617 int recheck;
1618 if (isl_int_is_zero(mat->row[var->index][off + j]))
1619 continue;
1620 isl_assert(tab->mat->ctx,
1621 isl_int_is_neg(mat->row[var->index][off + j]), return -1);
1622 recheck = isl_tab_kill_col(tab, j);
1623 if (recheck < 0)
1624 return -1;
1625 if (recheck)
1626 --j;
1628 if (isl_tab_mark_redundant(tab, var->index) < 0)
1629 return -1;
1630 if (tab_is_manifestly_empty(tab) && isl_tab_mark_empty(tab) < 0)
1631 return -1;
1632 return 0;
1635 /* Add a constraint to the tableau and allocate a row for it.
1636 * Return the index into the constraint array "con".
1638 int isl_tab_allocate_con(struct isl_tab *tab)
1640 int r;
1642 isl_assert(tab->mat->ctx, tab->n_row < tab->mat->n_row, return -1);
1643 isl_assert(tab->mat->ctx, tab->n_con < tab->max_con, return -1);
1645 r = tab->n_con;
1646 tab->con[r].index = tab->n_row;
1647 tab->con[r].is_row = 1;
1648 tab->con[r].is_nonneg = 0;
1649 tab->con[r].is_zero = 0;
1650 tab->con[r].is_redundant = 0;
1651 tab->con[r].frozen = 0;
1652 tab->con[r].negated = 0;
1653 tab->row_var[tab->n_row] = ~r;
1655 tab->n_row++;
1656 tab->n_con++;
1657 if (isl_tab_push_var(tab, isl_tab_undo_allocate, &tab->con[r]) < 0)
1658 return -1;
1660 return r;
1663 /* Add a variable to the tableau and allocate a column for it.
1664 * Return the index into the variable array "var".
1666 int isl_tab_allocate_var(struct isl_tab *tab)
1668 int r;
1669 int i;
1670 unsigned off = 2 + tab->M;
1672 isl_assert(tab->mat->ctx, tab->n_col < tab->mat->n_col, return -1);
1673 isl_assert(tab->mat->ctx, tab->n_var < tab->max_var, return -1);
1675 r = tab->n_var;
1676 tab->var[r].index = tab->n_col;
1677 tab->var[r].is_row = 0;
1678 tab->var[r].is_nonneg = 0;
1679 tab->var[r].is_zero = 0;
1680 tab->var[r].is_redundant = 0;
1681 tab->var[r].frozen = 0;
1682 tab->var[r].negated = 0;
1683 tab->col_var[tab->n_col] = r;
1685 for (i = 0; i < tab->n_row; ++i)
1686 isl_int_set_si(tab->mat->row[i][off + tab->n_col], 0);
1688 tab->n_var++;
1689 tab->n_col++;
1690 if (isl_tab_push_var(tab, isl_tab_undo_allocate, &tab->var[r]) < 0)
1691 return -1;
1693 return r;
1696 /* Add a row to the tableau. The row is given as an affine combination
1697 * of the original variables and needs to be expressed in terms of the
1698 * column variables.
1700 * We add each term in turn.
1701 * If r = n/d_r is the current sum and we need to add k x, then
1702 * if x is a column variable, we increase the numerator of
1703 * this column by k d_r
1704 * if x = f/d_x is a row variable, then the new representation of r is
1706 * n k f d_x/g n + d_r/g k f m/d_r n + m/d_g k f
1707 * --- + --- = ------------------- = -------------------
1708 * d_r d_r d_r d_x/g m
1710 * with g the gcd of d_r and d_x and m the lcm of d_r and d_x.
1712 * If tab->M is set, then, internally, each variable x is represented
1713 * as x' - M. We then also need no subtract k d_r from the coefficient of M.
1715 int isl_tab_add_row(struct isl_tab *tab, isl_int *line)
1717 int i;
1718 int r;
1719 isl_int *row;
1720 isl_int a, b;
1721 unsigned off = 2 + tab->M;
1723 r = isl_tab_allocate_con(tab);
1724 if (r < 0)
1725 return -1;
1727 isl_int_init(a);
1728 isl_int_init(b);
1729 row = tab->mat->row[tab->con[r].index];
1730 isl_int_set_si(row[0], 1);
1731 isl_int_set(row[1], line[0]);
1732 isl_seq_clr(row + 2, tab->M + tab->n_col);
1733 for (i = 0; i < tab->n_var; ++i) {
1734 if (tab->var[i].is_zero)
1735 continue;
1736 if (tab->var[i].is_row) {
1737 isl_int_lcm(a,
1738 row[0], tab->mat->row[tab->var[i].index][0]);
1739 isl_int_swap(a, row[0]);
1740 isl_int_divexact(a, row[0], a);
1741 isl_int_divexact(b,
1742 row[0], tab->mat->row[tab->var[i].index][0]);
1743 isl_int_mul(b, b, line[1 + i]);
1744 isl_seq_combine(row + 1, a, row + 1,
1745 b, tab->mat->row[tab->var[i].index] + 1,
1746 1 + tab->M + tab->n_col);
1747 } else
1748 isl_int_addmul(row[off + tab->var[i].index],
1749 line[1 + i], row[0]);
1750 if (tab->M && i >= tab->n_param && i < tab->n_var - tab->n_div)
1751 isl_int_submul(row[2], line[1 + i], row[0]);
1753 isl_seq_normalize(tab->mat->ctx, row, off + tab->n_col);
1754 isl_int_clear(a);
1755 isl_int_clear(b);
1757 if (tab->row_sign)
1758 tab->row_sign[tab->con[r].index] = isl_tab_row_unknown;
1760 return r;
1763 static int drop_row(struct isl_tab *tab, int row)
1765 isl_assert(tab->mat->ctx, ~tab->row_var[row] == tab->n_con - 1, return -1);
1766 if (row != tab->n_row - 1)
1767 swap_rows(tab, row, tab->n_row - 1);
1768 tab->n_row--;
1769 tab->n_con--;
1770 return 0;
1773 static int drop_col(struct isl_tab *tab, int col)
1775 isl_assert(tab->mat->ctx, tab->col_var[col] == tab->n_var - 1, return -1);
1776 if (col != tab->n_col - 1)
1777 swap_cols(tab, col, tab->n_col - 1);
1778 tab->n_col--;
1779 tab->n_var--;
1780 return 0;
1783 /* Add inequality "ineq" and check if it conflicts with the
1784 * previously added constraints or if it is obviously redundant.
1786 int isl_tab_add_ineq(struct isl_tab *tab, isl_int *ineq)
1788 int r;
1789 int sgn;
1790 isl_int cst;
1792 if (!tab)
1793 return -1;
1794 if (tab->bmap) {
1795 struct isl_basic_map *bmap = tab->bmap;
1797 isl_assert(tab->mat->ctx, tab->n_eq == bmap->n_eq, return -1);
1798 isl_assert(tab->mat->ctx,
1799 tab->n_con == bmap->n_eq + bmap->n_ineq, return -1);
1800 tab->bmap = isl_basic_map_add_ineq(tab->bmap, ineq);
1801 if (isl_tab_push(tab, isl_tab_undo_bmap_ineq) < 0)
1802 return -1;
1803 if (!tab->bmap)
1804 return -1;
1806 if (tab->cone) {
1807 isl_int_init(cst);
1808 isl_int_swap(ineq[0], cst);
1810 r = isl_tab_add_row(tab, ineq);
1811 if (tab->cone) {
1812 isl_int_swap(ineq[0], cst);
1813 isl_int_clear(cst);
1815 if (r < 0)
1816 return -1;
1817 tab->con[r].is_nonneg = 1;
1818 if (isl_tab_push_var(tab, isl_tab_undo_nonneg, &tab->con[r]) < 0)
1819 return -1;
1820 if (isl_tab_row_is_redundant(tab, tab->con[r].index)) {
1821 if (isl_tab_mark_redundant(tab, tab->con[r].index) < 0)
1822 return -1;
1823 return 0;
1826 sgn = restore_row(tab, &tab->con[r]);
1827 if (sgn < -1)
1828 return -1;
1829 if (sgn < 0)
1830 return isl_tab_mark_empty(tab);
1831 if (tab->con[r].is_row && isl_tab_row_is_redundant(tab, tab->con[r].index))
1832 if (isl_tab_mark_redundant(tab, tab->con[r].index) < 0)
1833 return -1;
1834 return 0;
1837 /* Pivot a non-negative variable down until it reaches the value zero
1838 * and then pivot the variable into a column position.
1840 static int to_col(struct isl_tab *tab, struct isl_tab_var *var) WARN_UNUSED;
1841 static int to_col(struct isl_tab *tab, struct isl_tab_var *var)
1843 int i;
1844 int row, col;
1845 unsigned off = 2 + tab->M;
1847 if (!var->is_row)
1848 return 0;
1850 while (isl_int_is_pos(tab->mat->row[var->index][1])) {
1851 find_pivot(tab, var, NULL, -1, &row, &col);
1852 isl_assert(tab->mat->ctx, row != -1, return -1);
1853 if (isl_tab_pivot(tab, row, col) < 0)
1854 return -1;
1855 if (!var->is_row)
1856 return 0;
1859 for (i = tab->n_dead; i < tab->n_col; ++i)
1860 if (!isl_int_is_zero(tab->mat->row[var->index][off + i]))
1861 break;
1863 isl_assert(tab->mat->ctx, i < tab->n_col, return -1);
1864 if (isl_tab_pivot(tab, var->index, i) < 0)
1865 return -1;
1867 return 0;
1870 /* We assume Gaussian elimination has been performed on the equalities.
1871 * The equalities can therefore never conflict.
1872 * Adding the equalities is currently only really useful for a later call
1873 * to isl_tab_ineq_type.
1875 static struct isl_tab *add_eq(struct isl_tab *tab, isl_int *eq)
1877 int i;
1878 int r;
1880 if (!tab)
1881 return NULL;
1882 r = isl_tab_add_row(tab, eq);
1883 if (r < 0)
1884 goto error;
1886 r = tab->con[r].index;
1887 i = isl_seq_first_non_zero(tab->mat->row[r] + 2 + tab->M + tab->n_dead,
1888 tab->n_col - tab->n_dead);
1889 isl_assert(tab->mat->ctx, i >= 0, goto error);
1890 i += tab->n_dead;
1891 if (isl_tab_pivot(tab, r, i) < 0)
1892 goto error;
1893 if (isl_tab_kill_col(tab, i) < 0)
1894 goto error;
1895 tab->n_eq++;
1897 return tab;
1898 error:
1899 isl_tab_free(tab);
1900 return NULL;
1903 static int row_is_manifestly_zero(struct isl_tab *tab, int row)
1905 unsigned off = 2 + tab->M;
1907 if (!isl_int_is_zero(tab->mat->row[row][1]))
1908 return 0;
1909 if (tab->M && !isl_int_is_zero(tab->mat->row[row][2]))
1910 return 0;
1911 return isl_seq_first_non_zero(tab->mat->row[row] + off + tab->n_dead,
1912 tab->n_col - tab->n_dead) == -1;
1915 /* Add an equality that is known to be valid for the given tableau.
1917 int isl_tab_add_valid_eq(struct isl_tab *tab, isl_int *eq)
1919 struct isl_tab_var *var;
1920 int r;
1922 if (!tab)
1923 return -1;
1924 r = isl_tab_add_row(tab, eq);
1925 if (r < 0)
1926 return -1;
1928 var = &tab->con[r];
1929 r = var->index;
1930 if (row_is_manifestly_zero(tab, r)) {
1931 var->is_zero = 1;
1932 if (isl_tab_mark_redundant(tab, r) < 0)
1933 return -1;
1934 return 0;
1937 if (isl_int_is_neg(tab->mat->row[r][1])) {
1938 isl_seq_neg(tab->mat->row[r] + 1, tab->mat->row[r] + 1,
1939 1 + tab->n_col);
1940 var->negated = 1;
1942 var->is_nonneg = 1;
1943 if (to_col(tab, var) < 0)
1944 return -1;
1945 var->is_nonneg = 0;
1946 if (isl_tab_kill_col(tab, var->index) < 0)
1947 return -1;
1949 return 0;
1952 static int add_zero_row(struct isl_tab *tab)
1954 int r;
1955 isl_int *row;
1957 r = isl_tab_allocate_con(tab);
1958 if (r < 0)
1959 return -1;
1961 row = tab->mat->row[tab->con[r].index];
1962 isl_seq_clr(row + 1, 1 + tab->M + tab->n_col);
1963 isl_int_set_si(row[0], 1);
1965 return r;
1968 /* Add equality "eq" and check if it conflicts with the
1969 * previously added constraints or if it is obviously redundant.
1971 int isl_tab_add_eq(struct isl_tab *tab, isl_int *eq)
1973 struct isl_tab_undo *snap = NULL;
1974 struct isl_tab_var *var;
1975 int r;
1976 int row;
1977 int sgn;
1978 isl_int cst;
1980 if (!tab)
1981 return -1;
1982 isl_assert(tab->mat->ctx, !tab->M, return -1);
1984 if (tab->need_undo)
1985 snap = isl_tab_snap(tab);
1987 if (tab->cone) {
1988 isl_int_init(cst);
1989 isl_int_swap(eq[0], cst);
1991 r = isl_tab_add_row(tab, eq);
1992 if (tab->cone) {
1993 isl_int_swap(eq[0], cst);
1994 isl_int_clear(cst);
1996 if (r < 0)
1997 return -1;
1999 var = &tab->con[r];
2000 row = var->index;
2001 if (row_is_manifestly_zero(tab, row)) {
2002 if (snap) {
2003 if (isl_tab_rollback(tab, snap) < 0)
2004 return -1;
2005 } else
2006 drop_row(tab, row);
2007 return 0;
2010 if (tab->bmap) {
2011 tab->bmap = isl_basic_map_add_ineq(tab->bmap, eq);
2012 if (isl_tab_push(tab, isl_tab_undo_bmap_ineq) < 0)
2013 return -1;
2014 isl_seq_neg(eq, eq, 1 + tab->n_var);
2015 tab->bmap = isl_basic_map_add_ineq(tab->bmap, eq);
2016 isl_seq_neg(eq, eq, 1 + tab->n_var);
2017 if (isl_tab_push(tab, isl_tab_undo_bmap_ineq) < 0)
2018 return -1;
2019 if (!tab->bmap)
2020 return -1;
2021 if (add_zero_row(tab) < 0)
2022 return -1;
2025 sgn = isl_int_sgn(tab->mat->row[row][1]);
2027 if (sgn > 0) {
2028 isl_seq_neg(tab->mat->row[row] + 1, tab->mat->row[row] + 1,
2029 1 + tab->n_col);
2030 var->negated = 1;
2031 sgn = -1;
2034 if (sgn < 0) {
2035 sgn = sign_of_max(tab, var);
2036 if (sgn < -1)
2037 return -1;
2038 if (sgn < 0) {
2039 if (isl_tab_mark_empty(tab) < 0)
2040 return -1;
2041 return 0;
2045 var->is_nonneg = 1;
2046 if (to_col(tab, var) < 0)
2047 return -1;
2048 var->is_nonneg = 0;
2049 if (isl_tab_kill_col(tab, var->index) < 0)
2050 return -1;
2052 return 0;
2055 /* Construct and return an inequality that expresses an upper bound
2056 * on the given div.
2057 * In particular, if the div is given by
2059 * d = floor(e/m)
2061 * then the inequality expresses
2063 * m d <= e
2065 static struct isl_vec *ineq_for_div(struct isl_basic_map *bmap, unsigned div)
2067 unsigned total;
2068 unsigned div_pos;
2069 struct isl_vec *ineq;
2071 if (!bmap)
2072 return NULL;
2074 total = isl_basic_map_total_dim(bmap);
2075 div_pos = 1 + total - bmap->n_div + div;
2077 ineq = isl_vec_alloc(bmap->ctx, 1 + total);
2078 if (!ineq)
2079 return NULL;
2081 isl_seq_cpy(ineq->el, bmap->div[div] + 1, 1 + total);
2082 isl_int_neg(ineq->el[div_pos], bmap->div[div][0]);
2083 return ineq;
2086 /* For a div d = floor(f/m), add the constraints
2088 * f - m d >= 0
2089 * -(f-(m-1)) + m d >= 0
2091 * Note that the second constraint is the negation of
2093 * f - m d >= m
2095 * If add_ineq is not NULL, then this function is used
2096 * instead of isl_tab_add_ineq to effectively add the inequalities.
2098 static int add_div_constraints(struct isl_tab *tab, unsigned div,
2099 int (*add_ineq)(void *user, isl_int *), void *user)
2101 unsigned total;
2102 unsigned div_pos;
2103 struct isl_vec *ineq;
2105 total = isl_basic_map_total_dim(tab->bmap);
2106 div_pos = 1 + total - tab->bmap->n_div + div;
2108 ineq = ineq_for_div(tab->bmap, div);
2109 if (!ineq)
2110 goto error;
2112 if (add_ineq) {
2113 if (add_ineq(user, ineq->el) < 0)
2114 goto error;
2115 } else {
2116 if (isl_tab_add_ineq(tab, ineq->el) < 0)
2117 goto error;
2120 isl_seq_neg(ineq->el, tab->bmap->div[div] + 1, 1 + total);
2121 isl_int_set(ineq->el[div_pos], tab->bmap->div[div][0]);
2122 isl_int_add(ineq->el[0], ineq->el[0], ineq->el[div_pos]);
2123 isl_int_sub_ui(ineq->el[0], ineq->el[0], 1);
2125 if (add_ineq) {
2126 if (add_ineq(user, ineq->el) < 0)
2127 goto error;
2128 } else {
2129 if (isl_tab_add_ineq(tab, ineq->el) < 0)
2130 goto error;
2133 isl_vec_free(ineq);
2135 return 0;
2136 error:
2137 isl_vec_free(ineq);
2138 return -1;
2141 /* Check whether the div described by "div" is obviously non-negative.
2142 * If we are using a big parameter, then we will encode the div
2143 * as div' = M + div, which is always non-negative.
2144 * Otherwise, we check whether div is a non-negative affine combination
2145 * of non-negative variables.
2147 static int div_is_nonneg(struct isl_tab *tab, __isl_keep isl_vec *div)
2149 int i;
2151 if (tab->M)
2152 return 1;
2154 if (isl_int_is_neg(div->el[1]))
2155 return 0;
2157 for (i = 0; i < tab->n_var; ++i) {
2158 if (isl_int_is_neg(div->el[2 + i]))
2159 return 0;
2160 if (isl_int_is_zero(div->el[2 + i]))
2161 continue;
2162 if (!tab->var[i].is_nonneg)
2163 return 0;
2166 return 1;
2169 /* Add an extra div, prescribed by "div" to the tableau and
2170 * the associated bmap (which is assumed to be non-NULL).
2172 * If add_ineq is not NULL, then this function is used instead
2173 * of isl_tab_add_ineq to add the div constraints.
2174 * This complication is needed because the code in isl_tab_pip
2175 * wants to perform some extra processing when an inequality
2176 * is added to the tableau.
2178 int isl_tab_add_div(struct isl_tab *tab, __isl_keep isl_vec *div,
2179 int (*add_ineq)(void *user, isl_int *), void *user)
2181 int r;
2182 int k;
2183 int nonneg;
2185 if (!tab || !div)
2186 return -1;
2188 isl_assert(tab->mat->ctx, tab->bmap, return -1);
2190 nonneg = div_is_nonneg(tab, div);
2192 if (isl_tab_extend_cons(tab, 3) < 0)
2193 return -1;
2194 if (isl_tab_extend_vars(tab, 1) < 0)
2195 return -1;
2196 r = isl_tab_allocate_var(tab);
2197 if (r < 0)
2198 return -1;
2200 if (nonneg)
2201 tab->var[r].is_nonneg = 1;
2203 tab->bmap = isl_basic_map_extend_space(tab->bmap,
2204 isl_basic_map_get_space(tab->bmap), 1, 0, 2);
2205 k = isl_basic_map_alloc_div(tab->bmap);
2206 if (k < 0)
2207 return -1;
2208 isl_seq_cpy(tab->bmap->div[k], div->el, div->size);
2209 if (isl_tab_push(tab, isl_tab_undo_bmap_div) < 0)
2210 return -1;
2212 if (add_div_constraints(tab, k, add_ineq, user) < 0)
2213 return -1;
2215 return r;
2218 /* If "track" is set, then we want to keep track of all constraints in tab
2219 * in its bmap field. This field is initialized from a copy of "bmap",
2220 * so we need to make sure that all constraints in "bmap" also appear
2221 * in the constructed tab.
2223 __isl_give struct isl_tab *isl_tab_from_basic_map(
2224 __isl_keep isl_basic_map *bmap, int track)
2226 int i;
2227 struct isl_tab *tab;
2229 if (!bmap)
2230 return NULL;
2231 tab = isl_tab_alloc(bmap->ctx,
2232 isl_basic_map_total_dim(bmap) + bmap->n_ineq + 1,
2233 isl_basic_map_total_dim(bmap), 0);
2234 if (!tab)
2235 return NULL;
2236 tab->preserve = track;
2237 tab->rational = ISL_F_ISSET(bmap, ISL_BASIC_MAP_RATIONAL);
2238 if (ISL_F_ISSET(bmap, ISL_BASIC_MAP_EMPTY)) {
2239 if (isl_tab_mark_empty(tab) < 0)
2240 goto error;
2241 goto done;
2243 for (i = 0; i < bmap->n_eq; ++i) {
2244 tab = add_eq(tab, bmap->eq[i]);
2245 if (!tab)
2246 return tab;
2248 for (i = 0; i < bmap->n_ineq; ++i) {
2249 if (isl_tab_add_ineq(tab, bmap->ineq[i]) < 0)
2250 goto error;
2251 if (tab->empty)
2252 goto done;
2254 done:
2255 if (track && isl_tab_track_bmap(tab, isl_basic_map_copy(bmap)) < 0)
2256 goto error;
2257 return tab;
2258 error:
2259 isl_tab_free(tab);
2260 return NULL;
2263 __isl_give struct isl_tab *isl_tab_from_basic_set(
2264 __isl_keep isl_basic_set *bset, int track)
2266 return isl_tab_from_basic_map(bset, track);
2269 /* Construct a tableau corresponding to the recession cone of "bset".
2271 struct isl_tab *isl_tab_from_recession_cone(__isl_keep isl_basic_set *bset,
2272 int parametric)
2274 isl_int cst;
2275 int i;
2276 struct isl_tab *tab;
2277 unsigned offset = 0;
2279 if (!bset)
2280 return NULL;
2281 if (parametric)
2282 offset = isl_basic_set_dim(bset, isl_dim_param);
2283 tab = isl_tab_alloc(bset->ctx, bset->n_eq + bset->n_ineq,
2284 isl_basic_set_total_dim(bset) - offset, 0);
2285 if (!tab)
2286 return NULL;
2287 tab->rational = ISL_F_ISSET(bset, ISL_BASIC_SET_RATIONAL);
2288 tab->cone = 1;
2290 isl_int_init(cst);
2291 for (i = 0; i < bset->n_eq; ++i) {
2292 isl_int_swap(bset->eq[i][offset], cst);
2293 if (offset > 0) {
2294 if (isl_tab_add_eq(tab, bset->eq[i] + offset) < 0)
2295 goto error;
2296 } else
2297 tab = add_eq(tab, bset->eq[i]);
2298 isl_int_swap(bset->eq[i][offset], cst);
2299 if (!tab)
2300 goto done;
2302 for (i = 0; i < bset->n_ineq; ++i) {
2303 int r;
2304 isl_int_swap(bset->ineq[i][offset], cst);
2305 r = isl_tab_add_row(tab, bset->ineq[i] + offset);
2306 isl_int_swap(bset->ineq[i][offset], cst);
2307 if (r < 0)
2308 goto error;
2309 tab->con[r].is_nonneg = 1;
2310 if (isl_tab_push_var(tab, isl_tab_undo_nonneg, &tab->con[r]) < 0)
2311 goto error;
2313 done:
2314 isl_int_clear(cst);
2315 return tab;
2316 error:
2317 isl_int_clear(cst);
2318 isl_tab_free(tab);
2319 return NULL;
2322 /* Assuming "tab" is the tableau of a cone, check if the cone is
2323 * bounded, i.e., if it is empty or only contains the origin.
2325 int isl_tab_cone_is_bounded(struct isl_tab *tab)
2327 int i;
2329 if (!tab)
2330 return -1;
2331 if (tab->empty)
2332 return 1;
2333 if (tab->n_dead == tab->n_col)
2334 return 1;
2336 for (;;) {
2337 for (i = tab->n_redundant; i < tab->n_row; ++i) {
2338 struct isl_tab_var *var;
2339 int sgn;
2340 var = isl_tab_var_from_row(tab, i);
2341 if (!var->is_nonneg)
2342 continue;
2343 sgn = sign_of_max(tab, var);
2344 if (sgn < -1)
2345 return -1;
2346 if (sgn != 0)
2347 return 0;
2348 if (close_row(tab, var) < 0)
2349 return -1;
2350 break;
2352 if (tab->n_dead == tab->n_col)
2353 return 1;
2354 if (i == tab->n_row)
2355 return 0;
2359 int isl_tab_sample_is_integer(struct isl_tab *tab)
2361 int i;
2363 if (!tab)
2364 return -1;
2366 for (i = 0; i < tab->n_var; ++i) {
2367 int row;
2368 if (!tab->var[i].is_row)
2369 continue;
2370 row = tab->var[i].index;
2371 if (!isl_int_is_divisible_by(tab->mat->row[row][1],
2372 tab->mat->row[row][0]))
2373 return 0;
2375 return 1;
2378 static struct isl_vec *extract_integer_sample(struct isl_tab *tab)
2380 int i;
2381 struct isl_vec *vec;
2383 vec = isl_vec_alloc(tab->mat->ctx, 1 + tab->n_var);
2384 if (!vec)
2385 return NULL;
2387 isl_int_set_si(vec->block.data[0], 1);
2388 for (i = 0; i < tab->n_var; ++i) {
2389 if (!tab->var[i].is_row)
2390 isl_int_set_si(vec->block.data[1 + i], 0);
2391 else {
2392 int row = tab->var[i].index;
2393 isl_int_divexact(vec->block.data[1 + i],
2394 tab->mat->row[row][1], tab->mat->row[row][0]);
2398 return vec;
2401 struct isl_vec *isl_tab_get_sample_value(struct isl_tab *tab)
2403 int i;
2404 struct isl_vec *vec;
2405 isl_int m;
2407 if (!tab)
2408 return NULL;
2410 vec = isl_vec_alloc(tab->mat->ctx, 1 + tab->n_var);
2411 if (!vec)
2412 return NULL;
2414 isl_int_init(m);
2416 isl_int_set_si(vec->block.data[0], 1);
2417 for (i = 0; i < tab->n_var; ++i) {
2418 int row;
2419 if (!tab->var[i].is_row) {
2420 isl_int_set_si(vec->block.data[1 + i], 0);
2421 continue;
2423 row = tab->var[i].index;
2424 isl_int_gcd(m, vec->block.data[0], tab->mat->row[row][0]);
2425 isl_int_divexact(m, tab->mat->row[row][0], m);
2426 isl_seq_scale(vec->block.data, vec->block.data, m, 1 + i);
2427 isl_int_divexact(m, vec->block.data[0], tab->mat->row[row][0]);
2428 isl_int_mul(vec->block.data[1 + i], m, tab->mat->row[row][1]);
2430 vec = isl_vec_normalize(vec);
2432 isl_int_clear(m);
2433 return vec;
2436 /* Update "bmap" based on the results of the tableau "tab".
2437 * In particular, implicit equalities are made explicit, redundant constraints
2438 * are removed and if the sample value happens to be integer, it is stored
2439 * in "bmap" (unless "bmap" already had an integer sample).
2441 * The tableau is assumed to have been created from "bmap" using
2442 * isl_tab_from_basic_map.
2444 struct isl_basic_map *isl_basic_map_update_from_tab(struct isl_basic_map *bmap,
2445 struct isl_tab *tab)
2447 int i;
2448 unsigned n_eq;
2450 if (!bmap)
2451 return NULL;
2452 if (!tab)
2453 return bmap;
2455 n_eq = tab->n_eq;
2456 if (tab->empty)
2457 bmap = isl_basic_map_set_to_empty(bmap);
2458 else
2459 for (i = bmap->n_ineq - 1; i >= 0; --i) {
2460 if (isl_tab_is_equality(tab, n_eq + i))
2461 isl_basic_map_inequality_to_equality(bmap, i);
2462 else if (isl_tab_is_redundant(tab, n_eq + i))
2463 isl_basic_map_drop_inequality(bmap, i);
2465 if (bmap->n_eq != n_eq)
2466 isl_basic_map_gauss(bmap, NULL);
2467 if (!tab->rational &&
2468 !bmap->sample && isl_tab_sample_is_integer(tab))
2469 bmap->sample = extract_integer_sample(tab);
2470 return bmap;
2473 struct isl_basic_set *isl_basic_set_update_from_tab(struct isl_basic_set *bset,
2474 struct isl_tab *tab)
2476 return (struct isl_basic_set *)isl_basic_map_update_from_tab(
2477 (struct isl_basic_map *)bset, tab);
2480 /* Given a non-negative variable "var", add a new non-negative variable
2481 * that is the opposite of "var", ensuring that var can only attain the
2482 * value zero.
2483 * If var = n/d is a row variable, then the new variable = -n/d.
2484 * If var is a column variables, then the new variable = -var.
2485 * If the new variable cannot attain non-negative values, then
2486 * the resulting tableau is empty.
2487 * Otherwise, we know the value will be zero and we close the row.
2489 static int cut_to_hyperplane(struct isl_tab *tab, struct isl_tab_var *var)
2491 unsigned r;
2492 isl_int *row;
2493 int sgn;
2494 unsigned off = 2 + tab->M;
2496 if (var->is_zero)
2497 return 0;
2498 isl_assert(tab->mat->ctx, !var->is_redundant, return -1);
2499 isl_assert(tab->mat->ctx, var->is_nonneg, return -1);
2501 if (isl_tab_extend_cons(tab, 1) < 0)
2502 return -1;
2504 r = tab->n_con;
2505 tab->con[r].index = tab->n_row;
2506 tab->con[r].is_row = 1;
2507 tab->con[r].is_nonneg = 0;
2508 tab->con[r].is_zero = 0;
2509 tab->con[r].is_redundant = 0;
2510 tab->con[r].frozen = 0;
2511 tab->con[r].negated = 0;
2512 tab->row_var[tab->n_row] = ~r;
2513 row = tab->mat->row[tab->n_row];
2515 if (var->is_row) {
2516 isl_int_set(row[0], tab->mat->row[var->index][0]);
2517 isl_seq_neg(row + 1,
2518 tab->mat->row[var->index] + 1, 1 + tab->n_col);
2519 } else {
2520 isl_int_set_si(row[0], 1);
2521 isl_seq_clr(row + 1, 1 + tab->n_col);
2522 isl_int_set_si(row[off + var->index], -1);
2525 tab->n_row++;
2526 tab->n_con++;
2527 if (isl_tab_push_var(tab, isl_tab_undo_allocate, &tab->con[r]) < 0)
2528 return -1;
2530 sgn = sign_of_max(tab, &tab->con[r]);
2531 if (sgn < -1)
2532 return -1;
2533 if (sgn < 0) {
2534 if (isl_tab_mark_empty(tab) < 0)
2535 return -1;
2536 return 0;
2538 tab->con[r].is_nonneg = 1;
2539 if (isl_tab_push_var(tab, isl_tab_undo_nonneg, &tab->con[r]) < 0)
2540 return -1;
2541 /* sgn == 0 */
2542 if (close_row(tab, &tab->con[r]) < 0)
2543 return -1;
2545 return 0;
2548 /* Given a tableau "tab" and an inequality constraint "con" of the tableau,
2549 * relax the inequality by one. That is, the inequality r >= 0 is replaced
2550 * by r' = r + 1 >= 0.
2551 * If r is a row variable, we simply increase the constant term by one
2552 * (taking into account the denominator).
2553 * If r is a column variable, then we need to modify each row that
2554 * refers to r = r' - 1 by substituting this equality, effectively
2555 * subtracting the coefficient of the column from the constant.
2556 * We should only do this if the minimum is manifestly unbounded,
2557 * however. Otherwise, we may end up with negative sample values
2558 * for non-negative variables.
2559 * So, if r is a column variable with a minimum that is not
2560 * manifestly unbounded, then we need to move it to a row.
2561 * However, the sample value of this row may be negative,
2562 * even after the relaxation, so we need to restore it.
2563 * We therefore prefer to pivot a column up to a row, if possible.
2565 struct isl_tab *isl_tab_relax(struct isl_tab *tab, int con)
2567 struct isl_tab_var *var;
2568 unsigned off = 2 + tab->M;
2570 if (!tab)
2571 return NULL;
2573 var = &tab->con[con];
2575 if (var->is_row && (var->index < 0 || var->index < tab->n_redundant))
2576 isl_die(tab->mat->ctx, isl_error_invalid,
2577 "cannot relax redundant constraint", goto error);
2578 if (!var->is_row && (var->index < 0 || var->index < tab->n_dead))
2579 isl_die(tab->mat->ctx, isl_error_invalid,
2580 "cannot relax dead constraint", goto error);
2582 if (!var->is_row && !max_is_manifestly_unbounded(tab, var))
2583 if (to_row(tab, var, 1) < 0)
2584 goto error;
2585 if (!var->is_row && !min_is_manifestly_unbounded(tab, var))
2586 if (to_row(tab, var, -1) < 0)
2587 goto error;
2589 if (var->is_row) {
2590 isl_int_add(tab->mat->row[var->index][1],
2591 tab->mat->row[var->index][1], tab->mat->row[var->index][0]);
2592 if (restore_row(tab, var) < 0)
2593 goto error;
2594 } else {
2595 int i;
2597 for (i = 0; i < tab->n_row; ++i) {
2598 if (isl_int_is_zero(tab->mat->row[i][off + var->index]))
2599 continue;
2600 isl_int_sub(tab->mat->row[i][1], tab->mat->row[i][1],
2601 tab->mat->row[i][off + var->index]);
2606 if (isl_tab_push_var(tab, isl_tab_undo_relax, var) < 0)
2607 goto error;
2609 return tab;
2610 error:
2611 isl_tab_free(tab);
2612 return NULL;
2615 /* Remove the sign constraint from constraint "con".
2617 * If the constraint variable was originally marked non-negative,
2618 * then we make sure we mark it non-negative again during rollback.
2620 int isl_tab_unrestrict(struct isl_tab *tab, int con)
2622 struct isl_tab_var *var;
2624 if (!tab)
2625 return -1;
2627 var = &tab->con[con];
2628 if (!var->is_nonneg)
2629 return 0;
2631 var->is_nonneg = 0;
2632 if (isl_tab_push_var(tab, isl_tab_undo_unrestrict, var) < 0)
2633 return -1;
2635 return 0;
2638 int isl_tab_select_facet(struct isl_tab *tab, int con)
2640 if (!tab)
2641 return -1;
2643 return cut_to_hyperplane(tab, &tab->con[con]);
2646 static int may_be_equality(struct isl_tab *tab, int row)
2648 return tab->rational ? isl_int_is_zero(tab->mat->row[row][1])
2649 : isl_int_lt(tab->mat->row[row][1],
2650 tab->mat->row[row][0]);
2653 /* Check for (near) equalities among the constraints.
2654 * A constraint is an equality if it is non-negative and if
2655 * its maximal value is either
2656 * - zero (in case of rational tableaus), or
2657 * - strictly less than 1 (in case of integer tableaus)
2659 * We first mark all non-redundant and non-dead variables that
2660 * are not frozen and not obviously not an equality.
2661 * Then we iterate over all marked variables if they can attain
2662 * any values larger than zero or at least one.
2663 * If the maximal value is zero, we mark any column variables
2664 * that appear in the row as being zero and mark the row as being redundant.
2665 * Otherwise, if the maximal value is strictly less than one (and the
2666 * tableau is integer), then we restrict the value to being zero
2667 * by adding an opposite non-negative variable.
2669 int isl_tab_detect_implicit_equalities(struct isl_tab *tab)
2671 int i;
2672 unsigned n_marked;
2674 if (!tab)
2675 return -1;
2676 if (tab->empty)
2677 return 0;
2678 if (tab->n_dead == tab->n_col)
2679 return 0;
2681 n_marked = 0;
2682 for (i = tab->n_redundant; i < tab->n_row; ++i) {
2683 struct isl_tab_var *var = isl_tab_var_from_row(tab, i);
2684 var->marked = !var->frozen && var->is_nonneg &&
2685 may_be_equality(tab, i);
2686 if (var->marked)
2687 n_marked++;
2689 for (i = tab->n_dead; i < tab->n_col; ++i) {
2690 struct isl_tab_var *var = var_from_col(tab, i);
2691 var->marked = !var->frozen && var->is_nonneg;
2692 if (var->marked)
2693 n_marked++;
2695 while (n_marked) {
2696 struct isl_tab_var *var;
2697 int sgn;
2698 for (i = tab->n_redundant; i < tab->n_row; ++i) {
2699 var = isl_tab_var_from_row(tab, i);
2700 if (var->marked)
2701 break;
2703 if (i == tab->n_row) {
2704 for (i = tab->n_dead; i < tab->n_col; ++i) {
2705 var = var_from_col(tab, i);
2706 if (var->marked)
2707 break;
2709 if (i == tab->n_col)
2710 break;
2712 var->marked = 0;
2713 n_marked--;
2714 sgn = sign_of_max(tab, var);
2715 if (sgn < 0)
2716 return -1;
2717 if (sgn == 0) {
2718 if (close_row(tab, var) < 0)
2719 return -1;
2720 } else if (!tab->rational && !at_least_one(tab, var)) {
2721 if (cut_to_hyperplane(tab, var) < 0)
2722 return -1;
2723 return isl_tab_detect_implicit_equalities(tab);
2725 for (i = tab->n_redundant; i < tab->n_row; ++i) {
2726 var = isl_tab_var_from_row(tab, i);
2727 if (!var->marked)
2728 continue;
2729 if (may_be_equality(tab, i))
2730 continue;
2731 var->marked = 0;
2732 n_marked--;
2736 return 0;
2739 /* Update the element of row_var or col_var that corresponds to
2740 * constraint tab->con[i] to a move from position "old" to position "i".
2742 static int update_con_after_move(struct isl_tab *tab, int i, int old)
2744 int *p;
2745 int index;
2747 index = tab->con[i].index;
2748 if (index == -1)
2749 return 0;
2750 p = tab->con[i].is_row ? tab->row_var : tab->col_var;
2751 if (p[index] != ~old)
2752 isl_die(tab->mat->ctx, isl_error_internal,
2753 "broken internal state", return -1);
2754 p[index] = ~i;
2756 return 0;
2759 /* Rotate the "n" constraints starting at "first" to the right,
2760 * putting the last constraint in the position of the first constraint.
2762 static int rotate_constraints(struct isl_tab *tab, int first, int n)
2764 int i, last;
2765 struct isl_tab_var var;
2767 if (n <= 1)
2768 return 0;
2770 last = first + n - 1;
2771 var = tab->con[last];
2772 for (i = last; i > first; --i) {
2773 tab->con[i] = tab->con[i - 1];
2774 if (update_con_after_move(tab, i, i - 1) < 0)
2775 return -1;
2777 tab->con[first] = var;
2778 if (update_con_after_move(tab, first, last) < 0)
2779 return -1;
2781 return 0;
2784 /* Make the equalities that are implicit in "bmap" but that have been
2785 * detected in the corresponding "tab" explicit in "bmap" and update
2786 * "tab" to reflect the new order of the constraints.
2788 * In particular, if inequality i is an implicit equality then
2789 * isl_basic_map_inequality_to_equality will move the inequality
2790 * in front of the other equality and it will move the last inequality
2791 * in the position of inequality i.
2792 * In the tableau, the inequalities of "bmap" are stored after the equalities
2793 * and so the original order
2795 * E E E E E A A A I B B B B L
2797 * is changed into
2799 * I E E E E E A A A L B B B B
2801 * where I is the implicit equality, the E are equalities,
2802 * the A inequalities before I, the B inequalities after I and
2803 * L the last inequality.
2804 * We therefore need to rotate to the right two sets of constraints,
2805 * those up to and including I and those after I.
2807 * If "tab" contains any constraints that are not in "bmap" then they
2808 * appear after those in "bmap" and they should be left untouched.
2810 * Note that this function leaves "bmap" in a temporary state
2811 * as it does not call isl_basic_map_gauss. Calling this function
2812 * is the responsibility of the caller.
2814 __isl_give isl_basic_map *isl_tab_make_equalities_explicit(struct isl_tab *tab,
2815 __isl_take isl_basic_map *bmap)
2817 int i;
2819 if (!tab || !bmap)
2820 return isl_basic_map_free(bmap);
2821 if (tab->empty)
2822 return bmap;
2824 for (i = bmap->n_ineq - 1; i >= 0; --i) {
2825 if (!isl_tab_is_equality(tab, bmap->n_eq + i))
2826 continue;
2827 isl_basic_map_inequality_to_equality(bmap, i);
2828 if (rotate_constraints(tab, 0, tab->n_eq + i + 1) < 0)
2829 return isl_basic_map_free(bmap);
2830 if (rotate_constraints(tab, tab->n_eq + i + 1,
2831 bmap->n_ineq - i) < 0)
2832 return isl_basic_map_free(bmap);
2833 tab->n_eq++;
2836 return bmap;
2839 static int con_is_redundant(struct isl_tab *tab, struct isl_tab_var *var)
2841 if (!tab)
2842 return -1;
2843 if (tab->rational) {
2844 int sgn = sign_of_min(tab, var);
2845 if (sgn < -1)
2846 return -1;
2847 return sgn >= 0;
2848 } else {
2849 int irred = isl_tab_min_at_most_neg_one(tab, var);
2850 if (irred < 0)
2851 return -1;
2852 return !irred;
2856 /* Check for (near) redundant constraints.
2857 * A constraint is redundant if it is non-negative and if
2858 * its minimal value (temporarily ignoring the non-negativity) is either
2859 * - zero (in case of rational tableaus), or
2860 * - strictly larger than -1 (in case of integer tableaus)
2862 * We first mark all non-redundant and non-dead variables that
2863 * are not frozen and not obviously negatively unbounded.
2864 * Then we iterate over all marked variables if they can attain
2865 * any values smaller than zero or at most negative one.
2866 * If not, we mark the row as being redundant (assuming it hasn't
2867 * been detected as being obviously redundant in the mean time).
2869 int isl_tab_detect_redundant(struct isl_tab *tab)
2871 int i;
2872 unsigned n_marked;
2874 if (!tab)
2875 return -1;
2876 if (tab->empty)
2877 return 0;
2878 if (tab->n_redundant == tab->n_row)
2879 return 0;
2881 n_marked = 0;
2882 for (i = tab->n_redundant; i < tab->n_row; ++i) {
2883 struct isl_tab_var *var = isl_tab_var_from_row(tab, i);
2884 var->marked = !var->frozen && var->is_nonneg;
2885 if (var->marked)
2886 n_marked++;
2888 for (i = tab->n_dead; i < tab->n_col; ++i) {
2889 struct isl_tab_var *var = var_from_col(tab, i);
2890 var->marked = !var->frozen && var->is_nonneg &&
2891 !min_is_manifestly_unbounded(tab, var);
2892 if (var->marked)
2893 n_marked++;
2895 while (n_marked) {
2896 struct isl_tab_var *var;
2897 int red;
2898 for (i = tab->n_redundant; i < tab->n_row; ++i) {
2899 var = isl_tab_var_from_row(tab, i);
2900 if (var->marked)
2901 break;
2903 if (i == tab->n_row) {
2904 for (i = tab->n_dead; i < tab->n_col; ++i) {
2905 var = var_from_col(tab, i);
2906 if (var->marked)
2907 break;
2909 if (i == tab->n_col)
2910 break;
2912 var->marked = 0;
2913 n_marked--;
2914 red = con_is_redundant(tab, var);
2915 if (red < 0)
2916 return -1;
2917 if (red && !var->is_redundant)
2918 if (isl_tab_mark_redundant(tab, var->index) < 0)
2919 return -1;
2920 for (i = tab->n_dead; i < tab->n_col; ++i) {
2921 var = var_from_col(tab, i);
2922 if (!var->marked)
2923 continue;
2924 if (!min_is_manifestly_unbounded(tab, var))
2925 continue;
2926 var->marked = 0;
2927 n_marked--;
2931 return 0;
2934 int isl_tab_is_equality(struct isl_tab *tab, int con)
2936 int row;
2937 unsigned off;
2939 if (!tab)
2940 return -1;
2941 if (tab->con[con].is_zero)
2942 return 1;
2943 if (tab->con[con].is_redundant)
2944 return 0;
2945 if (!tab->con[con].is_row)
2946 return tab->con[con].index < tab->n_dead;
2948 row = tab->con[con].index;
2950 off = 2 + tab->M;
2951 return isl_int_is_zero(tab->mat->row[row][1]) &&
2952 (!tab->M || isl_int_is_zero(tab->mat->row[row][2])) &&
2953 isl_seq_first_non_zero(tab->mat->row[row] + off + tab->n_dead,
2954 tab->n_col - tab->n_dead) == -1;
2957 /* Return the minimal value of the affine expression "f" with denominator
2958 * "denom" in *opt, *opt_denom, assuming the tableau is not empty and
2959 * the expression cannot attain arbitrarily small values.
2960 * If opt_denom is NULL, then *opt is rounded up to the nearest integer.
2961 * The return value reflects the nature of the result (empty, unbounded,
2962 * minimal value returned in *opt).
2964 enum isl_lp_result isl_tab_min(struct isl_tab *tab,
2965 isl_int *f, isl_int denom, isl_int *opt, isl_int *opt_denom,
2966 unsigned flags)
2968 int r;
2969 enum isl_lp_result res = isl_lp_ok;
2970 struct isl_tab_var *var;
2971 struct isl_tab_undo *snap;
2973 if (!tab)
2974 return isl_lp_error;
2976 if (tab->empty)
2977 return isl_lp_empty;
2979 snap = isl_tab_snap(tab);
2980 r = isl_tab_add_row(tab, f);
2981 if (r < 0)
2982 return isl_lp_error;
2983 var = &tab->con[r];
2984 for (;;) {
2985 int row, col;
2986 find_pivot(tab, var, var, -1, &row, &col);
2987 if (row == var->index) {
2988 res = isl_lp_unbounded;
2989 break;
2991 if (row == -1)
2992 break;
2993 if (isl_tab_pivot(tab, row, col) < 0)
2994 return isl_lp_error;
2996 isl_int_mul(tab->mat->row[var->index][0],
2997 tab->mat->row[var->index][0], denom);
2998 if (ISL_FL_ISSET(flags, ISL_TAB_SAVE_DUAL)) {
2999 int i;
3001 isl_vec_free(tab->dual);
3002 tab->dual = isl_vec_alloc(tab->mat->ctx, 1 + tab->n_con);
3003 if (!tab->dual)
3004 return isl_lp_error;
3005 isl_int_set(tab->dual->el[0], tab->mat->row[var->index][0]);
3006 for (i = 0; i < tab->n_con; ++i) {
3007 int pos;
3008 if (tab->con[i].is_row) {
3009 isl_int_set_si(tab->dual->el[1 + i], 0);
3010 continue;
3012 pos = 2 + tab->M + tab->con[i].index;
3013 if (tab->con[i].negated)
3014 isl_int_neg(tab->dual->el[1 + i],
3015 tab->mat->row[var->index][pos]);
3016 else
3017 isl_int_set(tab->dual->el[1 + i],
3018 tab->mat->row[var->index][pos]);
3021 if (opt && res == isl_lp_ok) {
3022 if (opt_denom) {
3023 isl_int_set(*opt, tab->mat->row[var->index][1]);
3024 isl_int_set(*opt_denom, tab->mat->row[var->index][0]);
3025 } else
3026 isl_int_cdiv_q(*opt, tab->mat->row[var->index][1],
3027 tab->mat->row[var->index][0]);
3029 if (isl_tab_rollback(tab, snap) < 0)
3030 return isl_lp_error;
3031 return res;
3034 int isl_tab_is_redundant(struct isl_tab *tab, int con)
3036 if (!tab)
3037 return -1;
3038 if (tab->con[con].is_zero)
3039 return 0;
3040 if (tab->con[con].is_redundant)
3041 return 1;
3042 return tab->con[con].is_row && tab->con[con].index < tab->n_redundant;
3045 /* Take a snapshot of the tableau that can be restored by s call to
3046 * isl_tab_rollback.
3048 struct isl_tab_undo *isl_tab_snap(struct isl_tab *tab)
3050 if (!tab)
3051 return NULL;
3052 tab->need_undo = 1;
3053 return tab->top;
3056 /* Undo the operation performed by isl_tab_relax.
3058 static int unrelax(struct isl_tab *tab, struct isl_tab_var *var) WARN_UNUSED;
3059 static int unrelax(struct isl_tab *tab, struct isl_tab_var *var)
3061 unsigned off = 2 + tab->M;
3063 if (!var->is_row && !max_is_manifestly_unbounded(tab, var))
3064 if (to_row(tab, var, 1) < 0)
3065 return -1;
3067 if (var->is_row) {
3068 isl_int_sub(tab->mat->row[var->index][1],
3069 tab->mat->row[var->index][1], tab->mat->row[var->index][0]);
3070 if (var->is_nonneg) {
3071 int sgn = restore_row(tab, var);
3072 isl_assert(tab->mat->ctx, sgn >= 0, return -1);
3074 } else {
3075 int i;
3077 for (i = 0; i < tab->n_row; ++i) {
3078 if (isl_int_is_zero(tab->mat->row[i][off + var->index]))
3079 continue;
3080 isl_int_add(tab->mat->row[i][1], tab->mat->row[i][1],
3081 tab->mat->row[i][off + var->index]);
3086 return 0;
3089 /* Undo the operation performed by isl_tab_unrestrict.
3091 * In particular, mark the variable as being non-negative and make
3092 * sure the sample value respects this constraint.
3094 static int ununrestrict(struct isl_tab *tab, struct isl_tab_var *var)
3096 var->is_nonneg = 1;
3098 if (var->is_row && restore_row(tab, var) < -1)
3099 return -1;
3101 return 0;
3104 static int perform_undo_var(struct isl_tab *tab, struct isl_tab_undo *undo) WARN_UNUSED;
3105 static int perform_undo_var(struct isl_tab *tab, struct isl_tab_undo *undo)
3107 struct isl_tab_var *var = var_from_index(tab, undo->u.var_index);
3108 switch (undo->type) {
3109 case isl_tab_undo_nonneg:
3110 var->is_nonneg = 0;
3111 break;
3112 case isl_tab_undo_redundant:
3113 var->is_redundant = 0;
3114 tab->n_redundant--;
3115 restore_row(tab, isl_tab_var_from_row(tab, tab->n_redundant));
3116 break;
3117 case isl_tab_undo_freeze:
3118 var->frozen = 0;
3119 break;
3120 case isl_tab_undo_zero:
3121 var->is_zero = 0;
3122 if (!var->is_row)
3123 tab->n_dead--;
3124 break;
3125 case isl_tab_undo_allocate:
3126 if (undo->u.var_index >= 0) {
3127 isl_assert(tab->mat->ctx, !var->is_row, return -1);
3128 drop_col(tab, var->index);
3129 break;
3131 if (!var->is_row) {
3132 if (!max_is_manifestly_unbounded(tab, var)) {
3133 if (to_row(tab, var, 1) < 0)
3134 return -1;
3135 } else if (!min_is_manifestly_unbounded(tab, var)) {
3136 if (to_row(tab, var, -1) < 0)
3137 return -1;
3138 } else
3139 if (to_row(tab, var, 0) < 0)
3140 return -1;
3142 drop_row(tab, var->index);
3143 break;
3144 case isl_tab_undo_relax:
3145 return unrelax(tab, var);
3146 case isl_tab_undo_unrestrict:
3147 return ununrestrict(tab, var);
3148 default:
3149 isl_die(tab->mat->ctx, isl_error_internal,
3150 "perform_undo_var called on invalid undo record",
3151 return -1);
3154 return 0;
3157 /* Restore the tableau to the state where the basic variables
3158 * are those in "col_var".
3159 * We first construct a list of variables that are currently in
3160 * the basis, but shouldn't. Then we iterate over all variables
3161 * that should be in the basis and for each one that is currently
3162 * not in the basis, we exchange it with one of the elements of the
3163 * list constructed before.
3164 * We can always find an appropriate variable to pivot with because
3165 * the current basis is mapped to the old basis by a non-singular
3166 * matrix and so we can never end up with a zero row.
3168 static int restore_basis(struct isl_tab *tab, int *col_var)
3170 int i, j;
3171 int n_extra = 0;
3172 int *extra = NULL; /* current columns that contain bad stuff */
3173 unsigned off = 2 + tab->M;
3175 extra = isl_alloc_array(tab->mat->ctx, int, tab->n_col);
3176 if (tab->n_col && !extra)
3177 goto error;
3178 for (i = 0; i < tab->n_col; ++i) {
3179 for (j = 0; j < tab->n_col; ++j)
3180 if (tab->col_var[i] == col_var[j])
3181 break;
3182 if (j < tab->n_col)
3183 continue;
3184 extra[n_extra++] = i;
3186 for (i = 0; i < tab->n_col && n_extra > 0; ++i) {
3187 struct isl_tab_var *var;
3188 int row;
3190 for (j = 0; j < tab->n_col; ++j)
3191 if (col_var[i] == tab->col_var[j])
3192 break;
3193 if (j < tab->n_col)
3194 continue;
3195 var = var_from_index(tab, col_var[i]);
3196 row = var->index;
3197 for (j = 0; j < n_extra; ++j)
3198 if (!isl_int_is_zero(tab->mat->row[row][off+extra[j]]))
3199 break;
3200 isl_assert(tab->mat->ctx, j < n_extra, goto error);
3201 if (isl_tab_pivot(tab, row, extra[j]) < 0)
3202 goto error;
3203 extra[j] = extra[--n_extra];
3206 free(extra);
3207 return 0;
3208 error:
3209 free(extra);
3210 return -1;
3213 /* Remove all samples with index n or greater, i.e., those samples
3214 * that were added since we saved this number of samples in
3215 * isl_tab_save_samples.
3217 static void drop_samples_since(struct isl_tab *tab, int n)
3219 int i;
3221 for (i = tab->n_sample - 1; i >= 0 && tab->n_sample > n; --i) {
3222 if (tab->sample_index[i] < n)
3223 continue;
3225 if (i != tab->n_sample - 1) {
3226 int t = tab->sample_index[tab->n_sample-1];
3227 tab->sample_index[tab->n_sample-1] = tab->sample_index[i];
3228 tab->sample_index[i] = t;
3229 isl_mat_swap_rows(tab->samples, tab->n_sample-1, i);
3231 tab->n_sample--;
3235 static int perform_undo(struct isl_tab *tab, struct isl_tab_undo *undo) WARN_UNUSED;
3236 static int perform_undo(struct isl_tab *tab, struct isl_tab_undo *undo)
3238 switch (undo->type) {
3239 case isl_tab_undo_empty:
3240 tab->empty = 0;
3241 break;
3242 case isl_tab_undo_nonneg:
3243 case isl_tab_undo_redundant:
3244 case isl_tab_undo_freeze:
3245 case isl_tab_undo_zero:
3246 case isl_tab_undo_allocate:
3247 case isl_tab_undo_relax:
3248 case isl_tab_undo_unrestrict:
3249 return perform_undo_var(tab, undo);
3250 case isl_tab_undo_bmap_eq:
3251 return isl_basic_map_free_equality(tab->bmap, 1);
3252 case isl_tab_undo_bmap_ineq:
3253 return isl_basic_map_free_inequality(tab->bmap, 1);
3254 case isl_tab_undo_bmap_div:
3255 if (isl_basic_map_free_div(tab->bmap, 1) < 0)
3256 return -1;
3257 if (tab->samples)
3258 tab->samples->n_col--;
3259 break;
3260 case isl_tab_undo_saved_basis:
3261 if (restore_basis(tab, undo->u.col_var) < 0)
3262 return -1;
3263 break;
3264 case isl_tab_undo_drop_sample:
3265 tab->n_outside--;
3266 break;
3267 case isl_tab_undo_saved_samples:
3268 drop_samples_since(tab, undo->u.n);
3269 break;
3270 case isl_tab_undo_callback:
3271 return undo->u.callback->run(undo->u.callback);
3272 default:
3273 isl_assert(tab->mat->ctx, 0, return -1);
3275 return 0;
3278 /* Return the tableau to the state it was in when the snapshot "snap"
3279 * was taken.
3281 int isl_tab_rollback(struct isl_tab *tab, struct isl_tab_undo *snap)
3283 struct isl_tab_undo *undo, *next;
3285 if (!tab)
3286 return -1;
3288 tab->in_undo = 1;
3289 for (undo = tab->top; undo && undo != &tab->bottom; undo = next) {
3290 next = undo->next;
3291 if (undo == snap)
3292 break;
3293 if (perform_undo(tab, undo) < 0) {
3294 tab->top = undo;
3295 free_undo(tab);
3296 tab->in_undo = 0;
3297 return -1;
3299 free_undo_record(undo);
3301 tab->in_undo = 0;
3302 tab->top = undo;
3303 if (!undo)
3304 return -1;
3305 return 0;
3308 /* The given row "row" represents an inequality violated by all
3309 * points in the tableau. Check for some special cases of such
3310 * separating constraints.
3311 * In particular, if the row has been reduced to the constant -1,
3312 * then we know the inequality is adjacent (but opposite) to
3313 * an equality in the tableau.
3314 * If the row has been reduced to r = c*(-1 -r'), with r' an inequality
3315 * of the tableau and c a positive constant, then the inequality
3316 * is adjacent (but opposite) to the inequality r'.
3318 static enum isl_ineq_type separation_type(struct isl_tab *tab, unsigned row)
3320 int pos;
3321 unsigned off = 2 + tab->M;
3323 if (tab->rational)
3324 return isl_ineq_separate;
3326 if (!isl_int_is_one(tab->mat->row[row][0]))
3327 return isl_ineq_separate;
3329 pos = isl_seq_first_non_zero(tab->mat->row[row] + off + tab->n_dead,
3330 tab->n_col - tab->n_dead);
3331 if (pos == -1) {
3332 if (isl_int_is_negone(tab->mat->row[row][1]))
3333 return isl_ineq_adj_eq;
3334 else
3335 return isl_ineq_separate;
3338 if (!isl_int_eq(tab->mat->row[row][1],
3339 tab->mat->row[row][off + tab->n_dead + pos]))
3340 return isl_ineq_separate;
3342 pos = isl_seq_first_non_zero(
3343 tab->mat->row[row] + off + tab->n_dead + pos + 1,
3344 tab->n_col - tab->n_dead - pos - 1);
3346 return pos == -1 ? isl_ineq_adj_ineq : isl_ineq_separate;
3349 /* Check the effect of inequality "ineq" on the tableau "tab".
3350 * The result may be
3351 * isl_ineq_redundant: satisfied by all points in the tableau
3352 * isl_ineq_separate: satisfied by no point in the tableau
3353 * isl_ineq_cut: satisfied by some by not all points
3354 * isl_ineq_adj_eq: adjacent to an equality
3355 * isl_ineq_adj_ineq: adjacent to an inequality.
3357 enum isl_ineq_type isl_tab_ineq_type(struct isl_tab *tab, isl_int *ineq)
3359 enum isl_ineq_type type = isl_ineq_error;
3360 struct isl_tab_undo *snap = NULL;
3361 int con;
3362 int row;
3364 if (!tab)
3365 return isl_ineq_error;
3367 if (isl_tab_extend_cons(tab, 1) < 0)
3368 return isl_ineq_error;
3370 snap = isl_tab_snap(tab);
3372 con = isl_tab_add_row(tab, ineq);
3373 if (con < 0)
3374 goto error;
3376 row = tab->con[con].index;
3377 if (isl_tab_row_is_redundant(tab, row))
3378 type = isl_ineq_redundant;
3379 else if (isl_int_is_neg(tab->mat->row[row][1]) &&
3380 (tab->rational ||
3381 isl_int_abs_ge(tab->mat->row[row][1],
3382 tab->mat->row[row][0]))) {
3383 int nonneg = at_least_zero(tab, &tab->con[con]);
3384 if (nonneg < 0)
3385 goto error;
3386 if (nonneg)
3387 type = isl_ineq_cut;
3388 else
3389 type = separation_type(tab, row);
3390 } else {
3391 int red = con_is_redundant(tab, &tab->con[con]);
3392 if (red < 0)
3393 goto error;
3394 if (!red)
3395 type = isl_ineq_cut;
3396 else
3397 type = isl_ineq_redundant;
3400 if (isl_tab_rollback(tab, snap))
3401 return isl_ineq_error;
3402 return type;
3403 error:
3404 return isl_ineq_error;
3407 int isl_tab_track_bmap(struct isl_tab *tab, __isl_take isl_basic_map *bmap)
3409 bmap = isl_basic_map_cow(bmap);
3410 if (!tab || !bmap)
3411 goto error;
3413 if (tab->empty) {
3414 bmap = isl_basic_map_set_to_empty(bmap);
3415 if (!bmap)
3416 goto error;
3417 tab->bmap = bmap;
3418 return 0;
3421 isl_assert(tab->mat->ctx, tab->n_eq == bmap->n_eq, goto error);
3422 isl_assert(tab->mat->ctx,
3423 tab->n_con == bmap->n_eq + bmap->n_ineq, goto error);
3425 tab->bmap = bmap;
3427 return 0;
3428 error:
3429 isl_basic_map_free(bmap);
3430 return -1;
3433 int isl_tab_track_bset(struct isl_tab *tab, __isl_take isl_basic_set *bset)
3435 return isl_tab_track_bmap(tab, (isl_basic_map *)bset);
3438 __isl_keep isl_basic_set *isl_tab_peek_bset(struct isl_tab *tab)
3440 if (!tab)
3441 return NULL;
3443 return (isl_basic_set *)tab->bmap;
3446 static void isl_tab_print_internal(__isl_keep struct isl_tab *tab,
3447 FILE *out, int indent)
3449 unsigned r, c;
3450 int i;
3452 if (!tab) {
3453 fprintf(out, "%*snull tab\n", indent, "");
3454 return;
3456 fprintf(out, "%*sn_redundant: %d, n_dead: %d", indent, "",
3457 tab->n_redundant, tab->n_dead);
3458 if (tab->rational)
3459 fprintf(out, ", rational");
3460 if (tab->empty)
3461 fprintf(out, ", empty");
3462 fprintf(out, "\n");
3463 fprintf(out, "%*s[", indent, "");
3464 for (i = 0; i < tab->n_var; ++i) {
3465 if (i)
3466 fprintf(out, (i == tab->n_param ||
3467 i == tab->n_var - tab->n_div) ? "; "
3468 : ", ");
3469 fprintf(out, "%c%d%s", tab->var[i].is_row ? 'r' : 'c',
3470 tab->var[i].index,
3471 tab->var[i].is_zero ? " [=0]" :
3472 tab->var[i].is_redundant ? " [R]" : "");
3474 fprintf(out, "]\n");
3475 fprintf(out, "%*s[", indent, "");
3476 for (i = 0; i < tab->n_con; ++i) {
3477 if (i)
3478 fprintf(out, ", ");
3479 fprintf(out, "%c%d%s", tab->con[i].is_row ? 'r' : 'c',
3480 tab->con[i].index,
3481 tab->con[i].is_zero ? " [=0]" :
3482 tab->con[i].is_redundant ? " [R]" : "");
3484 fprintf(out, "]\n");
3485 fprintf(out, "%*s[", indent, "");
3486 for (i = 0; i < tab->n_row; ++i) {
3487 const char *sign = "";
3488 if (i)
3489 fprintf(out, ", ");
3490 if (tab->row_sign) {
3491 if (tab->row_sign[i] == isl_tab_row_unknown)
3492 sign = "?";
3493 else if (tab->row_sign[i] == isl_tab_row_neg)
3494 sign = "-";
3495 else if (tab->row_sign[i] == isl_tab_row_pos)
3496 sign = "+";
3497 else
3498 sign = "+-";
3500 fprintf(out, "r%d: %d%s%s", i, tab->row_var[i],
3501 isl_tab_var_from_row(tab, i)->is_nonneg ? " [>=0]" : "", sign);
3503 fprintf(out, "]\n");
3504 fprintf(out, "%*s[", indent, "");
3505 for (i = 0; i < tab->n_col; ++i) {
3506 if (i)
3507 fprintf(out, ", ");
3508 fprintf(out, "c%d: %d%s", i, tab->col_var[i],
3509 var_from_col(tab, i)->is_nonneg ? " [>=0]" : "");
3511 fprintf(out, "]\n");
3512 r = tab->mat->n_row;
3513 tab->mat->n_row = tab->n_row;
3514 c = tab->mat->n_col;
3515 tab->mat->n_col = 2 + tab->M + tab->n_col;
3516 isl_mat_print_internal(tab->mat, out, indent);
3517 tab->mat->n_row = r;
3518 tab->mat->n_col = c;
3519 if (tab->bmap)
3520 isl_basic_map_print_internal(tab->bmap, out, indent);
3523 void isl_tab_dump(__isl_keep struct isl_tab *tab)
3525 isl_tab_print_internal(tab, stderr, 0);