python interface: support 'char *' and 'const char *' return types
[isl.git] / isl_tab.c
blob020300d9c4c1b0ef2e5eb510c0baf323d6a22e85
1 /*
2 * Copyright 2008-2009 Katholieke Universiteit Leuven
3 * Copyright 2013 Ecole Normale Superieure
4 * Copyright 2014 INRIA Rocquencourt
6 * Use of this software is governed by the MIT license
8 * Written by Sven Verdoolaege, K.U.Leuven, Departement
9 * Computerwetenschappen, Celestijnenlaan 200A, B-3001 Leuven, Belgium
10 * and Ecole Normale Superieure, 45 rue d'Ulm, 75230 Paris, France
11 * and Inria Paris - Rocquencourt, Domaine de Voluceau - Rocquencourt,
12 * B.P. 105 - 78153 Le Chesnay, France
15 #include <isl_ctx_private.h>
16 #include <isl_mat_private.h>
17 #include <isl_vec_private.h>
18 #include "isl_map_private.h"
19 #include "isl_tab.h"
20 #include <isl_seq.h>
21 #include <isl_config.h>
24 * The implementation of tableaus in this file was inspired by Section 8
25 * of David Detlefs, Greg Nelson and James B. Saxe, "Simplify: a theorem
26 * prover for program checking".
29 struct isl_tab *isl_tab_alloc(struct isl_ctx *ctx,
30 unsigned n_row, unsigned n_var, unsigned M)
32 int i;
33 struct isl_tab *tab;
34 unsigned off = 2 + M;
36 tab = isl_calloc_type(ctx, struct isl_tab);
37 if (!tab)
38 return NULL;
39 tab->mat = isl_mat_alloc(ctx, n_row, off + n_var);
40 if (!tab->mat)
41 goto error;
42 tab->var = isl_alloc_array(ctx, struct isl_tab_var, n_var);
43 if (n_var && !tab->var)
44 goto error;
45 tab->con = isl_alloc_array(ctx, struct isl_tab_var, n_row);
46 if (n_row && !tab->con)
47 goto error;
48 tab->col_var = isl_alloc_array(ctx, int, n_var);
49 if (n_var && !tab->col_var)
50 goto error;
51 tab->row_var = isl_alloc_array(ctx, int, n_row);
52 if (n_row && !tab->row_var)
53 goto error;
54 for (i = 0; i < n_var; ++i) {
55 tab->var[i].index = i;
56 tab->var[i].is_row = 0;
57 tab->var[i].is_nonneg = 0;
58 tab->var[i].is_zero = 0;
59 tab->var[i].is_redundant = 0;
60 tab->var[i].frozen = 0;
61 tab->var[i].negated = 0;
62 tab->col_var[i] = i;
64 tab->n_row = 0;
65 tab->n_con = 0;
66 tab->n_eq = 0;
67 tab->max_con = n_row;
68 tab->n_col = n_var;
69 tab->n_var = n_var;
70 tab->max_var = n_var;
71 tab->n_param = 0;
72 tab->n_div = 0;
73 tab->n_dead = 0;
74 tab->n_redundant = 0;
75 tab->strict_redundant = 0;
76 tab->need_undo = 0;
77 tab->rational = 0;
78 tab->empty = 0;
79 tab->in_undo = 0;
80 tab->M = M;
81 tab->cone = 0;
82 tab->bottom.type = isl_tab_undo_bottom;
83 tab->bottom.next = NULL;
84 tab->top = &tab->bottom;
86 tab->n_zero = 0;
87 tab->n_unbounded = 0;
88 tab->basis = NULL;
90 return tab;
91 error:
92 isl_tab_free(tab);
93 return NULL;
96 isl_ctx *isl_tab_get_ctx(struct isl_tab *tab)
98 return tab ? isl_mat_get_ctx(tab->mat) : NULL;
101 int isl_tab_extend_cons(struct isl_tab *tab, unsigned n_new)
103 unsigned off;
105 if (!tab)
106 return -1;
108 off = 2 + tab->M;
110 if (tab->max_con < tab->n_con + n_new) {
111 struct isl_tab_var *con;
113 con = isl_realloc_array(tab->mat->ctx, tab->con,
114 struct isl_tab_var, tab->max_con + n_new);
115 if (!con)
116 return -1;
117 tab->con = con;
118 tab->max_con += n_new;
120 if (tab->mat->n_row < tab->n_row + n_new) {
121 int *row_var;
123 tab->mat = isl_mat_extend(tab->mat,
124 tab->n_row + n_new, off + tab->n_col);
125 if (!tab->mat)
126 return -1;
127 row_var = isl_realloc_array(tab->mat->ctx, tab->row_var,
128 int, tab->mat->n_row);
129 if (!row_var)
130 return -1;
131 tab->row_var = row_var;
132 if (tab->row_sign) {
133 enum isl_tab_row_sign *s;
134 s = isl_realloc_array(tab->mat->ctx, tab->row_sign,
135 enum isl_tab_row_sign, tab->mat->n_row);
136 if (!s)
137 return -1;
138 tab->row_sign = s;
141 return 0;
144 /* Make room for at least n_new extra variables.
145 * Return -1 if anything went wrong.
147 int isl_tab_extend_vars(struct isl_tab *tab, unsigned n_new)
149 struct isl_tab_var *var;
150 unsigned off = 2 + tab->M;
152 if (tab->max_var < tab->n_var + n_new) {
153 var = isl_realloc_array(tab->mat->ctx, tab->var,
154 struct isl_tab_var, tab->n_var + n_new);
155 if (!var)
156 return -1;
157 tab->var = var;
158 tab->max_var = tab->n_var + n_new;
161 if (tab->mat->n_col < off + tab->n_col + n_new) {
162 int *p;
164 tab->mat = isl_mat_extend(tab->mat,
165 tab->mat->n_row, off + tab->n_col + n_new);
166 if (!tab->mat)
167 return -1;
168 p = isl_realloc_array(tab->mat->ctx, tab->col_var,
169 int, tab->n_col + n_new);
170 if (!p)
171 return -1;
172 tab->col_var = p;
175 return 0;
178 static void free_undo_record(struct isl_tab_undo *undo)
180 switch (undo->type) {
181 case isl_tab_undo_saved_basis:
182 free(undo->u.col_var);
183 break;
184 default:;
186 free(undo);
189 static void free_undo(struct isl_tab *tab)
191 struct isl_tab_undo *undo, *next;
193 for (undo = tab->top; undo && undo != &tab->bottom; undo = next) {
194 next = undo->next;
195 free_undo_record(undo);
197 tab->top = undo;
200 void isl_tab_free(struct isl_tab *tab)
202 if (!tab)
203 return;
204 free_undo(tab);
205 isl_mat_free(tab->mat);
206 isl_vec_free(tab->dual);
207 isl_basic_map_free(tab->bmap);
208 free(tab->var);
209 free(tab->con);
210 free(tab->row_var);
211 free(tab->col_var);
212 free(tab->row_sign);
213 isl_mat_free(tab->samples);
214 free(tab->sample_index);
215 isl_mat_free(tab->basis);
216 free(tab);
219 struct isl_tab *isl_tab_dup(struct isl_tab *tab)
221 int i;
222 struct isl_tab *dup;
223 unsigned off;
225 if (!tab)
226 return NULL;
228 off = 2 + tab->M;
229 dup = isl_calloc_type(tab->mat->ctx, struct isl_tab);
230 if (!dup)
231 return NULL;
232 dup->mat = isl_mat_dup(tab->mat);
233 if (!dup->mat)
234 goto error;
235 dup->var = isl_alloc_array(tab->mat->ctx, struct isl_tab_var, tab->max_var);
236 if (tab->max_var && !dup->var)
237 goto error;
238 for (i = 0; i < tab->n_var; ++i)
239 dup->var[i] = tab->var[i];
240 dup->con = isl_alloc_array(tab->mat->ctx, struct isl_tab_var, tab->max_con);
241 if (tab->max_con && !dup->con)
242 goto error;
243 for (i = 0; i < tab->n_con; ++i)
244 dup->con[i] = tab->con[i];
245 dup->col_var = isl_alloc_array(tab->mat->ctx, int, tab->mat->n_col - off);
246 if ((tab->mat->n_col - off) && !dup->col_var)
247 goto error;
248 for (i = 0; i < tab->n_col; ++i)
249 dup->col_var[i] = tab->col_var[i];
250 dup->row_var = isl_alloc_array(tab->mat->ctx, int, tab->mat->n_row);
251 if (tab->mat->n_row && !dup->row_var)
252 goto error;
253 for (i = 0; i < tab->n_row; ++i)
254 dup->row_var[i] = tab->row_var[i];
255 if (tab->row_sign) {
256 dup->row_sign = isl_alloc_array(tab->mat->ctx, enum isl_tab_row_sign,
257 tab->mat->n_row);
258 if (tab->mat->n_row && !dup->row_sign)
259 goto error;
260 for (i = 0; i < tab->n_row; ++i)
261 dup->row_sign[i] = tab->row_sign[i];
263 if (tab->samples) {
264 dup->samples = isl_mat_dup(tab->samples);
265 if (!dup->samples)
266 goto error;
267 dup->sample_index = isl_alloc_array(tab->mat->ctx, int,
268 tab->samples->n_row);
269 if (tab->samples->n_row && !dup->sample_index)
270 goto error;
271 dup->n_sample = tab->n_sample;
272 dup->n_outside = tab->n_outside;
274 dup->n_row = tab->n_row;
275 dup->n_con = tab->n_con;
276 dup->n_eq = tab->n_eq;
277 dup->max_con = tab->max_con;
278 dup->n_col = tab->n_col;
279 dup->n_var = tab->n_var;
280 dup->max_var = tab->max_var;
281 dup->n_param = tab->n_param;
282 dup->n_div = tab->n_div;
283 dup->n_dead = tab->n_dead;
284 dup->n_redundant = tab->n_redundant;
285 dup->rational = tab->rational;
286 dup->empty = tab->empty;
287 dup->strict_redundant = 0;
288 dup->need_undo = 0;
289 dup->in_undo = 0;
290 dup->M = tab->M;
291 tab->cone = tab->cone;
292 dup->bottom.type = isl_tab_undo_bottom;
293 dup->bottom.next = NULL;
294 dup->top = &dup->bottom;
296 dup->n_zero = tab->n_zero;
297 dup->n_unbounded = tab->n_unbounded;
298 dup->basis = isl_mat_dup(tab->basis);
300 return dup;
301 error:
302 isl_tab_free(dup);
303 return NULL;
306 /* Construct the coefficient matrix of the product tableau
307 * of two tableaus.
308 * mat{1,2} is the coefficient matrix of tableau {1,2}
309 * row{1,2} is the number of rows in tableau {1,2}
310 * col{1,2} is the number of columns in tableau {1,2}
311 * off is the offset to the coefficient column (skipping the
312 * denominator, the constant term and the big parameter if any)
313 * r{1,2} is the number of redundant rows in tableau {1,2}
314 * d{1,2} is the number of dead columns in tableau {1,2}
316 * The order of the rows and columns in the result is as explained
317 * in isl_tab_product.
319 static struct isl_mat *tab_mat_product(struct isl_mat *mat1,
320 struct isl_mat *mat2, unsigned row1, unsigned row2,
321 unsigned col1, unsigned col2,
322 unsigned off, unsigned r1, unsigned r2, unsigned d1, unsigned d2)
324 int i;
325 struct isl_mat *prod;
326 unsigned n;
328 prod = isl_mat_alloc(mat1->ctx, mat1->n_row + mat2->n_row,
329 off + col1 + col2);
330 if (!prod)
331 return NULL;
333 n = 0;
334 for (i = 0; i < r1; ++i) {
335 isl_seq_cpy(prod->row[n + i], mat1->row[i], off + d1);
336 isl_seq_clr(prod->row[n + i] + off + d1, d2);
337 isl_seq_cpy(prod->row[n + i] + off + d1 + d2,
338 mat1->row[i] + off + d1, col1 - d1);
339 isl_seq_clr(prod->row[n + i] + off + col1 + d1, col2 - d2);
342 n += r1;
343 for (i = 0; i < r2; ++i) {
344 isl_seq_cpy(prod->row[n + i], mat2->row[i], off);
345 isl_seq_clr(prod->row[n + i] + off, d1);
346 isl_seq_cpy(prod->row[n + i] + off + d1,
347 mat2->row[i] + off, d2);
348 isl_seq_clr(prod->row[n + i] + off + d1 + d2, col1 - d1);
349 isl_seq_cpy(prod->row[n + i] + off + col1 + d1,
350 mat2->row[i] + off + d2, col2 - d2);
353 n += r2;
354 for (i = 0; i < row1 - r1; ++i) {
355 isl_seq_cpy(prod->row[n + i], mat1->row[r1 + i], off + d1);
356 isl_seq_clr(prod->row[n + i] + off + d1, d2);
357 isl_seq_cpy(prod->row[n + i] + off + d1 + d2,
358 mat1->row[r1 + i] + off + d1, col1 - d1);
359 isl_seq_clr(prod->row[n + i] + off + col1 + d1, col2 - d2);
362 n += row1 - r1;
363 for (i = 0; i < row2 - r2; ++i) {
364 isl_seq_cpy(prod->row[n + i], mat2->row[r2 + i], off);
365 isl_seq_clr(prod->row[n + i] + off, d1);
366 isl_seq_cpy(prod->row[n + i] + off + d1,
367 mat2->row[r2 + i] + off, d2);
368 isl_seq_clr(prod->row[n + i] + off + d1 + d2, col1 - d1);
369 isl_seq_cpy(prod->row[n + i] + off + col1 + d1,
370 mat2->row[r2 + i] + off + d2, col2 - d2);
373 return prod;
376 /* Update the row or column index of a variable that corresponds
377 * to a variable in the first input tableau.
379 static void update_index1(struct isl_tab_var *var,
380 unsigned r1, unsigned r2, unsigned d1, unsigned d2)
382 if (var->index == -1)
383 return;
384 if (var->is_row && var->index >= r1)
385 var->index += r2;
386 if (!var->is_row && var->index >= d1)
387 var->index += d2;
390 /* Update the row or column index of a variable that corresponds
391 * to a variable in the second input tableau.
393 static void update_index2(struct isl_tab_var *var,
394 unsigned row1, unsigned col1,
395 unsigned r1, unsigned r2, unsigned d1, unsigned d2)
397 if (var->index == -1)
398 return;
399 if (var->is_row) {
400 if (var->index < r2)
401 var->index += r1;
402 else
403 var->index += row1;
404 } else {
405 if (var->index < d2)
406 var->index += d1;
407 else
408 var->index += col1;
412 /* Create a tableau that represents the Cartesian product of the sets
413 * represented by tableaus tab1 and tab2.
414 * The order of the rows in the product is
415 * - redundant rows of tab1
416 * - redundant rows of tab2
417 * - non-redundant rows of tab1
418 * - non-redundant rows of tab2
419 * The order of the columns is
420 * - denominator
421 * - constant term
422 * - coefficient of big parameter, if any
423 * - dead columns of tab1
424 * - dead columns of tab2
425 * - live columns of tab1
426 * - live columns of tab2
427 * The order of the variables and the constraints is a concatenation
428 * of order in the two input tableaus.
430 struct isl_tab *isl_tab_product(struct isl_tab *tab1, struct isl_tab *tab2)
432 int i;
433 struct isl_tab *prod;
434 unsigned off;
435 unsigned r1, r2, d1, d2;
437 if (!tab1 || !tab2)
438 return NULL;
440 isl_assert(tab1->mat->ctx, tab1->M == tab2->M, return NULL);
441 isl_assert(tab1->mat->ctx, tab1->rational == tab2->rational, return NULL);
442 isl_assert(tab1->mat->ctx, tab1->cone == tab2->cone, return NULL);
443 isl_assert(tab1->mat->ctx, !tab1->row_sign, return NULL);
444 isl_assert(tab1->mat->ctx, !tab2->row_sign, return NULL);
445 isl_assert(tab1->mat->ctx, tab1->n_param == 0, return NULL);
446 isl_assert(tab1->mat->ctx, tab2->n_param == 0, return NULL);
447 isl_assert(tab1->mat->ctx, tab1->n_div == 0, return NULL);
448 isl_assert(tab1->mat->ctx, tab2->n_div == 0, return NULL);
450 off = 2 + tab1->M;
451 r1 = tab1->n_redundant;
452 r2 = tab2->n_redundant;
453 d1 = tab1->n_dead;
454 d2 = tab2->n_dead;
455 prod = isl_calloc_type(tab1->mat->ctx, struct isl_tab);
456 if (!prod)
457 return NULL;
458 prod->mat = tab_mat_product(tab1->mat, tab2->mat,
459 tab1->n_row, tab2->n_row,
460 tab1->n_col, tab2->n_col, off, r1, r2, d1, d2);
461 if (!prod->mat)
462 goto error;
463 prod->var = isl_alloc_array(tab1->mat->ctx, struct isl_tab_var,
464 tab1->max_var + tab2->max_var);
465 if ((tab1->max_var + tab2->max_var) && !prod->var)
466 goto error;
467 for (i = 0; i < tab1->n_var; ++i) {
468 prod->var[i] = tab1->var[i];
469 update_index1(&prod->var[i], r1, r2, d1, d2);
471 for (i = 0; i < tab2->n_var; ++i) {
472 prod->var[tab1->n_var + i] = tab2->var[i];
473 update_index2(&prod->var[tab1->n_var + i],
474 tab1->n_row, tab1->n_col,
475 r1, r2, d1, d2);
477 prod->con = isl_alloc_array(tab1->mat->ctx, struct isl_tab_var,
478 tab1->max_con + tab2->max_con);
479 if ((tab1->max_con + tab2->max_con) && !prod->con)
480 goto error;
481 for (i = 0; i < tab1->n_con; ++i) {
482 prod->con[i] = tab1->con[i];
483 update_index1(&prod->con[i], r1, r2, d1, d2);
485 for (i = 0; i < tab2->n_con; ++i) {
486 prod->con[tab1->n_con + i] = tab2->con[i];
487 update_index2(&prod->con[tab1->n_con + i],
488 tab1->n_row, tab1->n_col,
489 r1, r2, d1, d2);
491 prod->col_var = isl_alloc_array(tab1->mat->ctx, int,
492 tab1->n_col + tab2->n_col);
493 if ((tab1->n_col + tab2->n_col) && !prod->col_var)
494 goto error;
495 for (i = 0; i < tab1->n_col; ++i) {
496 int pos = i < d1 ? i : i + d2;
497 prod->col_var[pos] = tab1->col_var[i];
499 for (i = 0; i < tab2->n_col; ++i) {
500 int pos = i < d2 ? d1 + i : tab1->n_col + i;
501 int t = tab2->col_var[i];
502 if (t >= 0)
503 t += tab1->n_var;
504 else
505 t -= tab1->n_con;
506 prod->col_var[pos] = t;
508 prod->row_var = isl_alloc_array(tab1->mat->ctx, int,
509 tab1->mat->n_row + tab2->mat->n_row);
510 if ((tab1->mat->n_row + tab2->mat->n_row) && !prod->row_var)
511 goto error;
512 for (i = 0; i < tab1->n_row; ++i) {
513 int pos = i < r1 ? i : i + r2;
514 prod->row_var[pos] = tab1->row_var[i];
516 for (i = 0; i < tab2->n_row; ++i) {
517 int pos = i < r2 ? r1 + i : tab1->n_row + i;
518 int t = tab2->row_var[i];
519 if (t >= 0)
520 t += tab1->n_var;
521 else
522 t -= tab1->n_con;
523 prod->row_var[pos] = t;
525 prod->samples = NULL;
526 prod->sample_index = NULL;
527 prod->n_row = tab1->n_row + tab2->n_row;
528 prod->n_con = tab1->n_con + tab2->n_con;
529 prod->n_eq = 0;
530 prod->max_con = tab1->max_con + tab2->max_con;
531 prod->n_col = tab1->n_col + tab2->n_col;
532 prod->n_var = tab1->n_var + tab2->n_var;
533 prod->max_var = tab1->max_var + tab2->max_var;
534 prod->n_param = 0;
535 prod->n_div = 0;
536 prod->n_dead = tab1->n_dead + tab2->n_dead;
537 prod->n_redundant = tab1->n_redundant + tab2->n_redundant;
538 prod->rational = tab1->rational;
539 prod->empty = tab1->empty || tab2->empty;
540 prod->strict_redundant = tab1->strict_redundant || tab2->strict_redundant;
541 prod->need_undo = 0;
542 prod->in_undo = 0;
543 prod->M = tab1->M;
544 prod->cone = tab1->cone;
545 prod->bottom.type = isl_tab_undo_bottom;
546 prod->bottom.next = NULL;
547 prod->top = &prod->bottom;
549 prod->n_zero = 0;
550 prod->n_unbounded = 0;
551 prod->basis = NULL;
553 return prod;
554 error:
555 isl_tab_free(prod);
556 return NULL;
559 static struct isl_tab_var *var_from_index(struct isl_tab *tab, int i)
561 if (i >= 0)
562 return &tab->var[i];
563 else
564 return &tab->con[~i];
567 struct isl_tab_var *isl_tab_var_from_row(struct isl_tab *tab, int i)
569 return var_from_index(tab, tab->row_var[i]);
572 static struct isl_tab_var *var_from_col(struct isl_tab *tab, int i)
574 return var_from_index(tab, tab->col_var[i]);
577 /* Check if there are any upper bounds on column variable "var",
578 * i.e., non-negative rows where var appears with a negative coefficient.
579 * Return 1 if there are no such bounds.
581 static int max_is_manifestly_unbounded(struct isl_tab *tab,
582 struct isl_tab_var *var)
584 int i;
585 unsigned off = 2 + tab->M;
587 if (var->is_row)
588 return 0;
589 for (i = tab->n_redundant; i < tab->n_row; ++i) {
590 if (!isl_int_is_neg(tab->mat->row[i][off + var->index]))
591 continue;
592 if (isl_tab_var_from_row(tab, i)->is_nonneg)
593 return 0;
595 return 1;
598 /* Check if there are any lower bounds on column variable "var",
599 * i.e., non-negative rows where var appears with a positive coefficient.
600 * Return 1 if there are no such bounds.
602 static int min_is_manifestly_unbounded(struct isl_tab *tab,
603 struct isl_tab_var *var)
605 int i;
606 unsigned off = 2 + tab->M;
608 if (var->is_row)
609 return 0;
610 for (i = tab->n_redundant; i < tab->n_row; ++i) {
611 if (!isl_int_is_pos(tab->mat->row[i][off + var->index]))
612 continue;
613 if (isl_tab_var_from_row(tab, i)->is_nonneg)
614 return 0;
616 return 1;
619 static int row_cmp(struct isl_tab *tab, int r1, int r2, int c, isl_int *t)
621 unsigned off = 2 + tab->M;
623 if (tab->M) {
624 int s;
625 isl_int_mul(*t, tab->mat->row[r1][2], tab->mat->row[r2][off+c]);
626 isl_int_submul(*t, tab->mat->row[r2][2], tab->mat->row[r1][off+c]);
627 s = isl_int_sgn(*t);
628 if (s)
629 return s;
631 isl_int_mul(*t, tab->mat->row[r1][1], tab->mat->row[r2][off + c]);
632 isl_int_submul(*t, tab->mat->row[r2][1], tab->mat->row[r1][off + c]);
633 return isl_int_sgn(*t);
636 /* Given the index of a column "c", return the index of a row
637 * that can be used to pivot the column in, with either an increase
638 * (sgn > 0) or a decrease (sgn < 0) of the corresponding variable.
639 * If "var" is not NULL, then the row returned will be different from
640 * the one associated with "var".
642 * Each row in the tableau is of the form
644 * x_r = a_r0 + \sum_i a_ri x_i
646 * Only rows with x_r >= 0 and with the sign of a_ri opposite to "sgn"
647 * impose any limit on the increase or decrease in the value of x_c
648 * and this bound is equal to a_r0 / |a_rc|. We are therefore looking
649 * for the row with the smallest (most stringent) such bound.
650 * Note that the common denominator of each row drops out of the fraction.
651 * To check if row j has a smaller bound than row r, i.e.,
652 * a_j0 / |a_jc| < a_r0 / |a_rc| or a_j0 |a_rc| < a_r0 |a_jc|,
653 * we check if -sign(a_jc) (a_j0 a_rc - a_r0 a_jc) < 0,
654 * where -sign(a_jc) is equal to "sgn".
656 static int pivot_row(struct isl_tab *tab,
657 struct isl_tab_var *var, int sgn, int c)
659 int j, r, tsgn;
660 isl_int t;
661 unsigned off = 2 + tab->M;
663 isl_int_init(t);
664 r = -1;
665 for (j = tab->n_redundant; j < tab->n_row; ++j) {
666 if (var && j == var->index)
667 continue;
668 if (!isl_tab_var_from_row(tab, j)->is_nonneg)
669 continue;
670 if (sgn * isl_int_sgn(tab->mat->row[j][off + c]) >= 0)
671 continue;
672 if (r < 0) {
673 r = j;
674 continue;
676 tsgn = sgn * row_cmp(tab, r, j, c, &t);
677 if (tsgn < 0 || (tsgn == 0 &&
678 tab->row_var[j] < tab->row_var[r]))
679 r = j;
681 isl_int_clear(t);
682 return r;
685 /* Find a pivot (row and col) that will increase (sgn > 0) or decrease
686 * (sgn < 0) the value of row variable var.
687 * If not NULL, then skip_var is a row variable that should be ignored
688 * while looking for a pivot row. It is usually equal to var.
690 * As the given row in the tableau is of the form
692 * x_r = a_r0 + \sum_i a_ri x_i
694 * we need to find a column such that the sign of a_ri is equal to "sgn"
695 * (such that an increase in x_i will have the desired effect) or a
696 * column with a variable that may attain negative values.
697 * If a_ri is positive, then we need to move x_i in the same direction
698 * to obtain the desired effect. Otherwise, x_i has to move in the
699 * opposite direction.
701 static void find_pivot(struct isl_tab *tab,
702 struct isl_tab_var *var, struct isl_tab_var *skip_var,
703 int sgn, int *row, int *col)
705 int j, r, c;
706 isl_int *tr;
708 *row = *col = -1;
710 isl_assert(tab->mat->ctx, var->is_row, return);
711 tr = tab->mat->row[var->index] + 2 + tab->M;
713 c = -1;
714 for (j = tab->n_dead; j < tab->n_col; ++j) {
715 if (isl_int_is_zero(tr[j]))
716 continue;
717 if (isl_int_sgn(tr[j]) != sgn &&
718 var_from_col(tab, j)->is_nonneg)
719 continue;
720 if (c < 0 || tab->col_var[j] < tab->col_var[c])
721 c = j;
723 if (c < 0)
724 return;
726 sgn *= isl_int_sgn(tr[c]);
727 r = pivot_row(tab, skip_var, sgn, c);
728 *row = r < 0 ? var->index : r;
729 *col = c;
732 /* Return 1 if row "row" represents an obviously redundant inequality.
733 * This means
734 * - it represents an inequality or a variable
735 * - that is the sum of a non-negative sample value and a positive
736 * combination of zero or more non-negative constraints.
738 int isl_tab_row_is_redundant(struct isl_tab *tab, int row)
740 int i;
741 unsigned off = 2 + tab->M;
743 if (tab->row_var[row] < 0 && !isl_tab_var_from_row(tab, row)->is_nonneg)
744 return 0;
746 if (isl_int_is_neg(tab->mat->row[row][1]))
747 return 0;
748 if (tab->strict_redundant && isl_int_is_zero(tab->mat->row[row][1]))
749 return 0;
750 if (tab->M && isl_int_is_neg(tab->mat->row[row][2]))
751 return 0;
753 for (i = tab->n_dead; i < tab->n_col; ++i) {
754 if (isl_int_is_zero(tab->mat->row[row][off + i]))
755 continue;
756 if (tab->col_var[i] >= 0)
757 return 0;
758 if (isl_int_is_neg(tab->mat->row[row][off + i]))
759 return 0;
760 if (!var_from_col(tab, i)->is_nonneg)
761 return 0;
763 return 1;
766 static void swap_rows(struct isl_tab *tab, int row1, int row2)
768 int t;
769 enum isl_tab_row_sign s;
771 t = tab->row_var[row1];
772 tab->row_var[row1] = tab->row_var[row2];
773 tab->row_var[row2] = t;
774 isl_tab_var_from_row(tab, row1)->index = row1;
775 isl_tab_var_from_row(tab, row2)->index = row2;
776 tab->mat = isl_mat_swap_rows(tab->mat, row1, row2);
778 if (!tab->row_sign)
779 return;
780 s = tab->row_sign[row1];
781 tab->row_sign[row1] = tab->row_sign[row2];
782 tab->row_sign[row2] = s;
785 static int push_union(struct isl_tab *tab,
786 enum isl_tab_undo_type type, union isl_tab_undo_val u) WARN_UNUSED;
787 static int push_union(struct isl_tab *tab,
788 enum isl_tab_undo_type type, union isl_tab_undo_val u)
790 struct isl_tab_undo *undo;
792 if (!tab)
793 return -1;
794 if (!tab->need_undo)
795 return 0;
797 undo = isl_alloc_type(tab->mat->ctx, struct isl_tab_undo);
798 if (!undo)
799 return -1;
800 undo->type = type;
801 undo->u = u;
802 undo->next = tab->top;
803 tab->top = undo;
805 return 0;
808 int isl_tab_push_var(struct isl_tab *tab,
809 enum isl_tab_undo_type type, struct isl_tab_var *var)
811 union isl_tab_undo_val u;
812 if (var->is_row)
813 u.var_index = tab->row_var[var->index];
814 else
815 u.var_index = tab->col_var[var->index];
816 return push_union(tab, type, u);
819 int isl_tab_push(struct isl_tab *tab, enum isl_tab_undo_type type)
821 union isl_tab_undo_val u = { 0 };
822 return push_union(tab, type, u);
825 /* Push a record on the undo stack describing the current basic
826 * variables, so that the this state can be restored during rollback.
828 int isl_tab_push_basis(struct isl_tab *tab)
830 int i;
831 union isl_tab_undo_val u;
833 u.col_var = isl_alloc_array(tab->mat->ctx, int, tab->n_col);
834 if (tab->n_col && !u.col_var)
835 return -1;
836 for (i = 0; i < tab->n_col; ++i)
837 u.col_var[i] = tab->col_var[i];
838 return push_union(tab, isl_tab_undo_saved_basis, u);
841 int isl_tab_push_callback(struct isl_tab *tab, struct isl_tab_callback *callback)
843 union isl_tab_undo_val u;
844 u.callback = callback;
845 return push_union(tab, isl_tab_undo_callback, u);
848 struct isl_tab *isl_tab_init_samples(struct isl_tab *tab)
850 if (!tab)
851 return NULL;
853 tab->n_sample = 0;
854 tab->n_outside = 0;
855 tab->samples = isl_mat_alloc(tab->mat->ctx, 1, 1 + tab->n_var);
856 if (!tab->samples)
857 goto error;
858 tab->sample_index = isl_alloc_array(tab->mat->ctx, int, 1);
859 if (!tab->sample_index)
860 goto error;
861 return tab;
862 error:
863 isl_tab_free(tab);
864 return NULL;
867 int isl_tab_add_sample(struct isl_tab *tab, __isl_take isl_vec *sample)
869 if (!tab || !sample)
870 goto error;
872 if (tab->n_sample + 1 > tab->samples->n_row) {
873 int *t = isl_realloc_array(tab->mat->ctx,
874 tab->sample_index, int, tab->n_sample + 1);
875 if (!t)
876 goto error;
877 tab->sample_index = t;
880 tab->samples = isl_mat_extend(tab->samples,
881 tab->n_sample + 1, tab->samples->n_col);
882 if (!tab->samples)
883 goto error;
885 isl_seq_cpy(tab->samples->row[tab->n_sample], sample->el, sample->size);
886 isl_vec_free(sample);
887 tab->sample_index[tab->n_sample] = tab->n_sample;
888 tab->n_sample++;
890 return 0;
891 error:
892 isl_vec_free(sample);
893 return -1;
896 struct isl_tab *isl_tab_drop_sample(struct isl_tab *tab, int s)
898 if (s != tab->n_outside) {
899 int t = tab->sample_index[tab->n_outside];
900 tab->sample_index[tab->n_outside] = tab->sample_index[s];
901 tab->sample_index[s] = t;
902 isl_mat_swap_rows(tab->samples, tab->n_outside, s);
904 tab->n_outside++;
905 if (isl_tab_push(tab, isl_tab_undo_drop_sample) < 0) {
906 isl_tab_free(tab);
907 return NULL;
910 return tab;
913 /* Record the current number of samples so that we can remove newer
914 * samples during a rollback.
916 int isl_tab_save_samples(struct isl_tab *tab)
918 union isl_tab_undo_val u;
920 if (!tab)
921 return -1;
923 u.n = tab->n_sample;
924 return push_union(tab, isl_tab_undo_saved_samples, u);
927 /* Mark row with index "row" as being redundant.
928 * If we may need to undo the operation or if the row represents
929 * a variable of the original problem, the row is kept,
930 * but no longer considered when looking for a pivot row.
931 * Otherwise, the row is simply removed.
933 * The row may be interchanged with some other row. If it
934 * is interchanged with a later row, return 1. Otherwise return 0.
935 * If the rows are checked in order in the calling function,
936 * then a return value of 1 means that the row with the given
937 * row number may now contain a different row that hasn't been checked yet.
939 int isl_tab_mark_redundant(struct isl_tab *tab, int row)
941 struct isl_tab_var *var = isl_tab_var_from_row(tab, row);
942 var->is_redundant = 1;
943 isl_assert(tab->mat->ctx, row >= tab->n_redundant, return -1);
944 if (tab->preserve || tab->need_undo || tab->row_var[row] >= 0) {
945 if (tab->row_var[row] >= 0 && !var->is_nonneg) {
946 var->is_nonneg = 1;
947 if (isl_tab_push_var(tab, isl_tab_undo_nonneg, var) < 0)
948 return -1;
950 if (row != tab->n_redundant)
951 swap_rows(tab, row, tab->n_redundant);
952 tab->n_redundant++;
953 return isl_tab_push_var(tab, isl_tab_undo_redundant, var);
954 } else {
955 if (row != tab->n_row - 1)
956 swap_rows(tab, row, tab->n_row - 1);
957 isl_tab_var_from_row(tab, tab->n_row - 1)->index = -1;
958 tab->n_row--;
959 return 1;
963 /* Mark "tab" as a rational tableau.
964 * If it wasn't marked as a rational tableau already and if we may
965 * need to undo changes, then arrange for the marking to be undone
966 * during the undo.
968 int isl_tab_mark_rational(struct isl_tab *tab)
970 if (!tab)
971 return -1;
972 if (!tab->rational && tab->need_undo)
973 if (isl_tab_push(tab, isl_tab_undo_rational) < 0)
974 return -1;
975 tab->rational = 1;
976 return 0;
979 int isl_tab_mark_empty(struct isl_tab *tab)
981 if (!tab)
982 return -1;
983 if (!tab->empty && tab->need_undo)
984 if (isl_tab_push(tab, isl_tab_undo_empty) < 0)
985 return -1;
986 tab->empty = 1;
987 return 0;
990 int isl_tab_freeze_constraint(struct isl_tab *tab, int con)
992 struct isl_tab_var *var;
994 if (!tab)
995 return -1;
997 var = &tab->con[con];
998 if (var->frozen)
999 return 0;
1000 if (var->index < 0)
1001 return 0;
1002 var->frozen = 1;
1004 if (tab->need_undo)
1005 return isl_tab_push_var(tab, isl_tab_undo_freeze, var);
1007 return 0;
1010 /* Update the rows signs after a pivot of "row" and "col", with "row_sgn"
1011 * the original sign of the pivot element.
1012 * We only keep track of row signs during PILP solving and in this case
1013 * we only pivot a row with negative sign (meaning the value is always
1014 * non-positive) using a positive pivot element.
1016 * For each row j, the new value of the parametric constant is equal to
1018 * a_j0 - a_jc a_r0/a_rc
1020 * where a_j0 is the original parametric constant, a_rc is the pivot element,
1021 * a_r0 is the parametric constant of the pivot row and a_jc is the
1022 * pivot column entry of the row j.
1023 * Since a_r0 is non-positive and a_rc is positive, the sign of row j
1024 * remains the same if a_jc has the same sign as the row j or if
1025 * a_jc is zero. In all other cases, we reset the sign to "unknown".
1027 static void update_row_sign(struct isl_tab *tab, int row, int col, int row_sgn)
1029 int i;
1030 struct isl_mat *mat = tab->mat;
1031 unsigned off = 2 + tab->M;
1033 if (!tab->row_sign)
1034 return;
1036 if (tab->row_sign[row] == 0)
1037 return;
1038 isl_assert(mat->ctx, row_sgn > 0, return);
1039 isl_assert(mat->ctx, tab->row_sign[row] == isl_tab_row_neg, return);
1040 tab->row_sign[row] = isl_tab_row_pos;
1041 for (i = 0; i < tab->n_row; ++i) {
1042 int s;
1043 if (i == row)
1044 continue;
1045 s = isl_int_sgn(mat->row[i][off + col]);
1046 if (!s)
1047 continue;
1048 if (!tab->row_sign[i])
1049 continue;
1050 if (s < 0 && tab->row_sign[i] == isl_tab_row_neg)
1051 continue;
1052 if (s > 0 && tab->row_sign[i] == isl_tab_row_pos)
1053 continue;
1054 tab->row_sign[i] = isl_tab_row_unknown;
1058 /* Given a row number "row" and a column number "col", pivot the tableau
1059 * such that the associated variables are interchanged.
1060 * The given row in the tableau expresses
1062 * x_r = a_r0 + \sum_i a_ri x_i
1064 * or
1066 * x_c = 1/a_rc x_r - a_r0/a_rc + sum_{i \ne r} -a_ri/a_rc
1068 * Substituting this equality into the other rows
1070 * x_j = a_j0 + \sum_i a_ji x_i
1072 * with a_jc \ne 0, we obtain
1074 * 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
1076 * The tableau
1078 * n_rc/d_r n_ri/d_r
1079 * n_jc/d_j n_ji/d_j
1081 * where i is any other column and j is any other row,
1082 * is therefore transformed into
1084 * s(n_rc)d_r/|n_rc| -s(n_rc)n_ri/|n_rc|
1085 * 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)
1087 * The transformation is performed along the following steps
1089 * d_r/n_rc n_ri/n_rc
1090 * n_jc/d_j n_ji/d_j
1092 * s(n_rc)d_r/|n_rc| -s(n_rc)n_ri/|n_rc|
1093 * n_jc/d_j n_ji/d_j
1095 * s(n_rc)d_r/|n_rc| -s(n_rc)n_ri/|n_rc|
1096 * n_jc/(|n_rc| d_j) n_ji/(|n_rc| d_j)
1098 * s(n_rc)d_r/|n_rc| -s(n_rc)n_ri/|n_rc|
1099 * n_jc/(|n_rc| d_j) (n_ji |n_rc|)/(|n_rc| d_j)
1101 * s(n_rc)d_r/|n_rc| -s(n_rc)n_ri/|n_rc|
1102 * n_jc/(|n_rc| d_j) (n_ji |n_rc| - s(n_rc)n_jc n_ri)/(|n_rc| d_j)
1104 * s(n_rc)d_r/|n_rc| -s(n_rc)n_ri/|n_rc|
1105 * 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)
1108 int isl_tab_pivot(struct isl_tab *tab, int row, int col)
1110 int i, j;
1111 int sgn;
1112 int t;
1113 isl_ctx *ctx;
1114 struct isl_mat *mat = tab->mat;
1115 struct isl_tab_var *var;
1116 unsigned off = 2 + tab->M;
1118 ctx = isl_tab_get_ctx(tab);
1119 if (isl_ctx_next_operation(ctx) < 0)
1120 return -1;
1122 isl_int_swap(mat->row[row][0], mat->row[row][off + col]);
1123 sgn = isl_int_sgn(mat->row[row][0]);
1124 if (sgn < 0) {
1125 isl_int_neg(mat->row[row][0], mat->row[row][0]);
1126 isl_int_neg(mat->row[row][off + col], mat->row[row][off + col]);
1127 } else
1128 for (j = 0; j < off - 1 + tab->n_col; ++j) {
1129 if (j == off - 1 + col)
1130 continue;
1131 isl_int_neg(mat->row[row][1 + j], mat->row[row][1 + j]);
1133 if (!isl_int_is_one(mat->row[row][0]))
1134 isl_seq_normalize(mat->ctx, mat->row[row], off + tab->n_col);
1135 for (i = 0; i < tab->n_row; ++i) {
1136 if (i == row)
1137 continue;
1138 if (isl_int_is_zero(mat->row[i][off + col]))
1139 continue;
1140 isl_int_mul(mat->row[i][0], mat->row[i][0], mat->row[row][0]);
1141 for (j = 0; j < off - 1 + tab->n_col; ++j) {
1142 if (j == off - 1 + col)
1143 continue;
1144 isl_int_mul(mat->row[i][1 + j],
1145 mat->row[i][1 + j], mat->row[row][0]);
1146 isl_int_addmul(mat->row[i][1 + j],
1147 mat->row[i][off + col], mat->row[row][1 + j]);
1149 isl_int_mul(mat->row[i][off + col],
1150 mat->row[i][off + col], mat->row[row][off + col]);
1151 if (!isl_int_is_one(mat->row[i][0]))
1152 isl_seq_normalize(mat->ctx, mat->row[i], off + tab->n_col);
1154 t = tab->row_var[row];
1155 tab->row_var[row] = tab->col_var[col];
1156 tab->col_var[col] = t;
1157 var = isl_tab_var_from_row(tab, row);
1158 var->is_row = 1;
1159 var->index = row;
1160 var = var_from_col(tab, col);
1161 var->is_row = 0;
1162 var->index = col;
1163 update_row_sign(tab, row, col, sgn);
1164 if (tab->in_undo)
1165 return 0;
1166 for (i = tab->n_redundant; i < tab->n_row; ++i) {
1167 if (isl_int_is_zero(mat->row[i][off + col]))
1168 continue;
1169 if (!isl_tab_var_from_row(tab, i)->frozen &&
1170 isl_tab_row_is_redundant(tab, i)) {
1171 int redo = isl_tab_mark_redundant(tab, i);
1172 if (redo < 0)
1173 return -1;
1174 if (redo)
1175 --i;
1178 return 0;
1181 /* If "var" represents a column variable, then pivot is up (sgn > 0)
1182 * or down (sgn < 0) to a row. The variable is assumed not to be
1183 * unbounded in the specified direction.
1184 * If sgn = 0, then the variable is unbounded in both directions,
1185 * and we pivot with any row we can find.
1187 static int to_row(struct isl_tab *tab, struct isl_tab_var *var, int sign) WARN_UNUSED;
1188 static int to_row(struct isl_tab *tab, struct isl_tab_var *var, int sign)
1190 int r;
1191 unsigned off = 2 + tab->M;
1193 if (var->is_row)
1194 return 0;
1196 if (sign == 0) {
1197 for (r = tab->n_redundant; r < tab->n_row; ++r)
1198 if (!isl_int_is_zero(tab->mat->row[r][off+var->index]))
1199 break;
1200 isl_assert(tab->mat->ctx, r < tab->n_row, return -1);
1201 } else {
1202 r = pivot_row(tab, NULL, sign, var->index);
1203 isl_assert(tab->mat->ctx, r >= 0, return -1);
1206 return isl_tab_pivot(tab, r, var->index);
1209 /* Check whether all variables that are marked as non-negative
1210 * also have a non-negative sample value. This function is not
1211 * called from the current code but is useful during debugging.
1213 static void check_table(struct isl_tab *tab) __attribute__ ((unused));
1214 static void check_table(struct isl_tab *tab)
1216 int i;
1218 if (tab->empty)
1219 return;
1220 for (i = tab->n_redundant; i < tab->n_row; ++i) {
1221 struct isl_tab_var *var;
1222 var = isl_tab_var_from_row(tab, i);
1223 if (!var->is_nonneg)
1224 continue;
1225 if (tab->M) {
1226 isl_assert(tab->mat->ctx,
1227 !isl_int_is_neg(tab->mat->row[i][2]), abort());
1228 if (isl_int_is_pos(tab->mat->row[i][2]))
1229 continue;
1231 isl_assert(tab->mat->ctx, !isl_int_is_neg(tab->mat->row[i][1]),
1232 abort());
1236 /* Return the sign of the maximal value of "var".
1237 * If the sign is not negative, then on return from this function,
1238 * the sample value will also be non-negative.
1240 * If "var" is manifestly unbounded wrt positive values, we are done.
1241 * Otherwise, we pivot the variable up to a row if needed
1242 * Then we continue pivoting down until either
1243 * - no more down pivots can be performed
1244 * - the sample value is positive
1245 * - the variable is pivoted into a manifestly unbounded column
1247 static int sign_of_max(struct isl_tab *tab, struct isl_tab_var *var)
1249 int row, col;
1251 if (max_is_manifestly_unbounded(tab, var))
1252 return 1;
1253 if (to_row(tab, var, 1) < 0)
1254 return -2;
1255 while (!isl_int_is_pos(tab->mat->row[var->index][1])) {
1256 find_pivot(tab, var, var, 1, &row, &col);
1257 if (row == -1)
1258 return isl_int_sgn(tab->mat->row[var->index][1]);
1259 if (isl_tab_pivot(tab, row, col) < 0)
1260 return -2;
1261 if (!var->is_row) /* manifestly unbounded */
1262 return 1;
1264 return 1;
1267 int isl_tab_sign_of_max(struct isl_tab *tab, int con)
1269 struct isl_tab_var *var;
1271 if (!tab)
1272 return -2;
1274 var = &tab->con[con];
1275 isl_assert(tab->mat->ctx, !var->is_redundant, return -2);
1276 isl_assert(tab->mat->ctx, !var->is_zero, return -2);
1278 return sign_of_max(tab, var);
1281 static int row_is_neg(struct isl_tab *tab, int row)
1283 if (!tab->M)
1284 return isl_int_is_neg(tab->mat->row[row][1]);
1285 if (isl_int_is_pos(tab->mat->row[row][2]))
1286 return 0;
1287 if (isl_int_is_neg(tab->mat->row[row][2]))
1288 return 1;
1289 return isl_int_is_neg(tab->mat->row[row][1]);
1292 static int row_sgn(struct isl_tab *tab, int row)
1294 if (!tab->M)
1295 return isl_int_sgn(tab->mat->row[row][1]);
1296 if (!isl_int_is_zero(tab->mat->row[row][2]))
1297 return isl_int_sgn(tab->mat->row[row][2]);
1298 else
1299 return isl_int_sgn(tab->mat->row[row][1]);
1302 /* Perform pivots until the row variable "var" has a non-negative
1303 * sample value or until no more upward pivots can be performed.
1304 * Return the sign of the sample value after the pivots have been
1305 * performed.
1307 static int restore_row(struct isl_tab *tab, struct isl_tab_var *var)
1309 int row, col;
1311 while (row_is_neg(tab, var->index)) {
1312 find_pivot(tab, var, var, 1, &row, &col);
1313 if (row == -1)
1314 break;
1315 if (isl_tab_pivot(tab, row, col) < 0)
1316 return -2;
1317 if (!var->is_row) /* manifestly unbounded */
1318 return 1;
1320 return row_sgn(tab, var->index);
1323 /* Perform pivots until we are sure that the row variable "var"
1324 * can attain non-negative values. After return from this
1325 * function, "var" is still a row variable, but its sample
1326 * value may not be non-negative, even if the function returns 1.
1328 static int at_least_zero(struct isl_tab *tab, struct isl_tab_var *var)
1330 int row, col;
1332 while (isl_int_is_neg(tab->mat->row[var->index][1])) {
1333 find_pivot(tab, var, var, 1, &row, &col);
1334 if (row == -1)
1335 break;
1336 if (row == var->index) /* manifestly unbounded */
1337 return 1;
1338 if (isl_tab_pivot(tab, row, col) < 0)
1339 return -1;
1341 return !isl_int_is_neg(tab->mat->row[var->index][1]);
1344 /* Return a negative value if "var" can attain negative values.
1345 * Return a non-negative value otherwise.
1347 * If "var" is manifestly unbounded wrt negative values, we are done.
1348 * Otherwise, if var is in a column, we can pivot it down to a row.
1349 * Then we continue pivoting down until either
1350 * - the pivot would result in a manifestly unbounded column
1351 * => we don't perform the pivot, but simply return -1
1352 * - no more down pivots can be performed
1353 * - the sample value is negative
1354 * If the sample value becomes negative and the variable is supposed
1355 * to be nonnegative, then we undo the last pivot.
1356 * However, if the last pivot has made the pivoting variable
1357 * obviously redundant, then it may have moved to another row.
1358 * In that case we look for upward pivots until we reach a non-negative
1359 * value again.
1361 static int sign_of_min(struct isl_tab *tab, struct isl_tab_var *var)
1363 int row, col;
1364 struct isl_tab_var *pivot_var = NULL;
1366 if (min_is_manifestly_unbounded(tab, var))
1367 return -1;
1368 if (!var->is_row) {
1369 col = var->index;
1370 row = pivot_row(tab, NULL, -1, col);
1371 pivot_var = var_from_col(tab, col);
1372 if (isl_tab_pivot(tab, row, col) < 0)
1373 return -2;
1374 if (var->is_redundant)
1375 return 0;
1376 if (isl_int_is_neg(tab->mat->row[var->index][1])) {
1377 if (var->is_nonneg) {
1378 if (!pivot_var->is_redundant &&
1379 pivot_var->index == row) {
1380 if (isl_tab_pivot(tab, row, col) < 0)
1381 return -2;
1382 } else
1383 if (restore_row(tab, var) < -1)
1384 return -2;
1386 return -1;
1389 if (var->is_redundant)
1390 return 0;
1391 while (!isl_int_is_neg(tab->mat->row[var->index][1])) {
1392 find_pivot(tab, var, var, -1, &row, &col);
1393 if (row == var->index)
1394 return -1;
1395 if (row == -1)
1396 return isl_int_sgn(tab->mat->row[var->index][1]);
1397 pivot_var = var_from_col(tab, col);
1398 if (isl_tab_pivot(tab, row, col) < 0)
1399 return -2;
1400 if (var->is_redundant)
1401 return 0;
1403 if (pivot_var && var->is_nonneg) {
1404 /* pivot back to non-negative value */
1405 if (!pivot_var->is_redundant && pivot_var->index == row) {
1406 if (isl_tab_pivot(tab, row, col) < 0)
1407 return -2;
1408 } else
1409 if (restore_row(tab, var) < -1)
1410 return -2;
1412 return -1;
1415 static int row_at_most_neg_one(struct isl_tab *tab, int row)
1417 if (tab->M) {
1418 if (isl_int_is_pos(tab->mat->row[row][2]))
1419 return 0;
1420 if (isl_int_is_neg(tab->mat->row[row][2]))
1421 return 1;
1423 return isl_int_is_neg(tab->mat->row[row][1]) &&
1424 isl_int_abs_ge(tab->mat->row[row][1],
1425 tab->mat->row[row][0]);
1428 /* Return 1 if "var" can attain values <= -1.
1429 * Return 0 otherwise.
1431 * If the variable "var" is supposed to be non-negative (is_nonneg is set),
1432 * then the sample value of "var" is assumed to be non-negative when the
1433 * the function is called. If 1 is returned then the constraint
1434 * is not redundant and the sample value is made non-negative again before
1435 * the function returns.
1437 int isl_tab_min_at_most_neg_one(struct isl_tab *tab, struct isl_tab_var *var)
1439 int row, col;
1440 struct isl_tab_var *pivot_var;
1442 if (min_is_manifestly_unbounded(tab, var))
1443 return 1;
1444 if (!var->is_row) {
1445 col = var->index;
1446 row = pivot_row(tab, NULL, -1, col);
1447 pivot_var = var_from_col(tab, col);
1448 if (isl_tab_pivot(tab, row, col) < 0)
1449 return -1;
1450 if (var->is_redundant)
1451 return 0;
1452 if (row_at_most_neg_one(tab, var->index)) {
1453 if (var->is_nonneg) {
1454 if (!pivot_var->is_redundant &&
1455 pivot_var->index == row) {
1456 if (isl_tab_pivot(tab, row, col) < 0)
1457 return -1;
1458 } else
1459 if (restore_row(tab, var) < -1)
1460 return -1;
1462 return 1;
1465 if (var->is_redundant)
1466 return 0;
1467 do {
1468 find_pivot(tab, var, var, -1, &row, &col);
1469 if (row == var->index) {
1470 if (var->is_nonneg && restore_row(tab, var) < -1)
1471 return -1;
1472 return 1;
1474 if (row == -1)
1475 return 0;
1476 pivot_var = var_from_col(tab, col);
1477 if (isl_tab_pivot(tab, row, col) < 0)
1478 return -1;
1479 if (var->is_redundant)
1480 return 0;
1481 } while (!row_at_most_neg_one(tab, var->index));
1482 if (var->is_nonneg) {
1483 /* pivot back to non-negative value */
1484 if (!pivot_var->is_redundant && pivot_var->index == row)
1485 if (isl_tab_pivot(tab, row, col) < 0)
1486 return -1;
1487 if (restore_row(tab, var) < -1)
1488 return -1;
1490 return 1;
1493 /* Return 1 if "var" can attain values >= 1.
1494 * Return 0 otherwise.
1496 static int at_least_one(struct isl_tab *tab, struct isl_tab_var *var)
1498 int row, col;
1499 isl_int *r;
1501 if (max_is_manifestly_unbounded(tab, var))
1502 return 1;
1503 if (to_row(tab, var, 1) < 0)
1504 return -1;
1505 r = tab->mat->row[var->index];
1506 while (isl_int_lt(r[1], r[0])) {
1507 find_pivot(tab, var, var, 1, &row, &col);
1508 if (row == -1)
1509 return isl_int_ge(r[1], r[0]);
1510 if (row == var->index) /* manifestly unbounded */
1511 return 1;
1512 if (isl_tab_pivot(tab, row, col) < 0)
1513 return -1;
1515 return 1;
1518 static void swap_cols(struct isl_tab *tab, int col1, int col2)
1520 int t;
1521 unsigned off = 2 + tab->M;
1522 t = tab->col_var[col1];
1523 tab->col_var[col1] = tab->col_var[col2];
1524 tab->col_var[col2] = t;
1525 var_from_col(tab, col1)->index = col1;
1526 var_from_col(tab, col2)->index = col2;
1527 tab->mat = isl_mat_swap_cols(tab->mat, off + col1, off + col2);
1530 /* Mark column with index "col" as representing a zero variable.
1531 * If we may need to undo the operation the column is kept,
1532 * but no longer considered.
1533 * Otherwise, the column is simply removed.
1535 * The column may be interchanged with some other column. If it
1536 * is interchanged with a later column, return 1. Otherwise return 0.
1537 * If the columns are checked in order in the calling function,
1538 * then a return value of 1 means that the column with the given
1539 * column number may now contain a different column that
1540 * hasn't been checked yet.
1542 int isl_tab_kill_col(struct isl_tab *tab, int col)
1544 var_from_col(tab, col)->is_zero = 1;
1545 if (tab->need_undo) {
1546 if (isl_tab_push_var(tab, isl_tab_undo_zero,
1547 var_from_col(tab, col)) < 0)
1548 return -1;
1549 if (col != tab->n_dead)
1550 swap_cols(tab, col, tab->n_dead);
1551 tab->n_dead++;
1552 return 0;
1553 } else {
1554 if (col != tab->n_col - 1)
1555 swap_cols(tab, col, tab->n_col - 1);
1556 var_from_col(tab, tab->n_col - 1)->index = -1;
1557 tab->n_col--;
1558 return 1;
1562 static int row_is_manifestly_non_integral(struct isl_tab *tab, int row)
1564 unsigned off = 2 + tab->M;
1566 if (tab->M && !isl_int_eq(tab->mat->row[row][2],
1567 tab->mat->row[row][0]))
1568 return 0;
1569 if (isl_seq_first_non_zero(tab->mat->row[row] + off + tab->n_dead,
1570 tab->n_col - tab->n_dead) != -1)
1571 return 0;
1573 return !isl_int_is_divisible_by(tab->mat->row[row][1],
1574 tab->mat->row[row][0]);
1577 /* For integer tableaus, check if any of the coordinates are stuck
1578 * at a non-integral value.
1580 static int tab_is_manifestly_empty(struct isl_tab *tab)
1582 int i;
1584 if (tab->empty)
1585 return 1;
1586 if (tab->rational)
1587 return 0;
1589 for (i = 0; i < tab->n_var; ++i) {
1590 if (!tab->var[i].is_row)
1591 continue;
1592 if (row_is_manifestly_non_integral(tab, tab->var[i].index))
1593 return 1;
1596 return 0;
1599 /* Row variable "var" is non-negative and cannot attain any values
1600 * larger than zero. This means that the coefficients of the unrestricted
1601 * column variables are zero and that the coefficients of the non-negative
1602 * column variables are zero or negative.
1603 * Each of the non-negative variables with a negative coefficient can
1604 * then also be written as the negative sum of non-negative variables
1605 * and must therefore also be zero.
1607 static int close_row(struct isl_tab *tab, struct isl_tab_var *var) WARN_UNUSED;
1608 static int close_row(struct isl_tab *tab, struct isl_tab_var *var)
1610 int j;
1611 struct isl_mat *mat = tab->mat;
1612 unsigned off = 2 + tab->M;
1614 isl_assert(tab->mat->ctx, var->is_nonneg, return -1);
1615 var->is_zero = 1;
1616 if (tab->need_undo)
1617 if (isl_tab_push_var(tab, isl_tab_undo_zero, var) < 0)
1618 return -1;
1619 for (j = tab->n_dead; j < tab->n_col; ++j) {
1620 int recheck;
1621 if (isl_int_is_zero(mat->row[var->index][off + j]))
1622 continue;
1623 isl_assert(tab->mat->ctx,
1624 isl_int_is_neg(mat->row[var->index][off + j]), return -1);
1625 recheck = isl_tab_kill_col(tab, j);
1626 if (recheck < 0)
1627 return -1;
1628 if (recheck)
1629 --j;
1631 if (isl_tab_mark_redundant(tab, var->index) < 0)
1632 return -1;
1633 if (tab_is_manifestly_empty(tab) && isl_tab_mark_empty(tab) < 0)
1634 return -1;
1635 return 0;
1638 /* Add a constraint to the tableau and allocate a row for it.
1639 * Return the index into the constraint array "con".
1641 * This function assumes that at least one more row and at least
1642 * one more element in the constraint array are available in the tableau.
1644 int isl_tab_allocate_con(struct isl_tab *tab)
1646 int r;
1648 isl_assert(tab->mat->ctx, tab->n_row < tab->mat->n_row, return -1);
1649 isl_assert(tab->mat->ctx, tab->n_con < tab->max_con, return -1);
1651 r = tab->n_con;
1652 tab->con[r].index = tab->n_row;
1653 tab->con[r].is_row = 1;
1654 tab->con[r].is_nonneg = 0;
1655 tab->con[r].is_zero = 0;
1656 tab->con[r].is_redundant = 0;
1657 tab->con[r].frozen = 0;
1658 tab->con[r].negated = 0;
1659 tab->row_var[tab->n_row] = ~r;
1661 tab->n_row++;
1662 tab->n_con++;
1663 if (isl_tab_push_var(tab, isl_tab_undo_allocate, &tab->con[r]) < 0)
1664 return -1;
1666 return r;
1669 /* Move the entries in tab->var up one position, starting at "first",
1670 * creating room for an extra entry at position "first".
1671 * Since some of the entries of tab->row_var and tab->col_var contain
1672 * indices into this array, they have to be updated accordingly.
1674 static int var_insert_entry(struct isl_tab *tab, int first)
1676 int i;
1678 if (tab->n_var >= tab->max_var)
1679 isl_die(isl_tab_get_ctx(tab), isl_error_internal,
1680 "not enough room for new variable", return -1);
1681 if (first > tab->n_var)
1682 isl_die(isl_tab_get_ctx(tab), isl_error_internal,
1683 "invalid initial position", return -1);
1685 for (i = tab->n_var - 1; i >= first; --i) {
1686 tab->var[i + 1] = tab->var[i];
1687 if (tab->var[i + 1].is_row)
1688 tab->row_var[tab->var[i + 1].index]++;
1689 else
1690 tab->col_var[tab->var[i + 1].index]++;
1693 tab->n_var++;
1695 return 0;
1698 /* Drop the entry at position "first" in tab->var, moving all
1699 * subsequent entries down.
1700 * Since some of the entries of tab->row_var and tab->col_var contain
1701 * indices into this array, they have to be updated accordingly.
1703 static int var_drop_entry(struct isl_tab *tab, int first)
1705 int i;
1707 if (first >= tab->n_var)
1708 isl_die(isl_tab_get_ctx(tab), isl_error_internal,
1709 "invalid initial position", return -1);
1711 tab->n_var--;
1713 for (i = first; i < tab->n_var; ++i) {
1714 tab->var[i] = tab->var[i + 1];
1715 if (tab->var[i + 1].is_row)
1716 tab->row_var[tab->var[i].index]--;
1717 else
1718 tab->col_var[tab->var[i].index]--;
1721 return 0;
1724 /* Add a variable to the tableau at position "r" and allocate a column for it.
1725 * Return the index into the variable array "var", i.e., "r",
1726 * or -1 on error.
1728 int isl_tab_insert_var(struct isl_tab *tab, int r)
1730 int i;
1731 unsigned off = 2 + tab->M;
1733 isl_assert(tab->mat->ctx, tab->n_col < tab->mat->n_col, return -1);
1735 if (var_insert_entry(tab, r) < 0)
1736 return -1;
1738 tab->var[r].index = tab->n_col;
1739 tab->var[r].is_row = 0;
1740 tab->var[r].is_nonneg = 0;
1741 tab->var[r].is_zero = 0;
1742 tab->var[r].is_redundant = 0;
1743 tab->var[r].frozen = 0;
1744 tab->var[r].negated = 0;
1745 tab->col_var[tab->n_col] = r;
1747 for (i = 0; i < tab->n_row; ++i)
1748 isl_int_set_si(tab->mat->row[i][off + tab->n_col], 0);
1750 tab->n_col++;
1751 if (isl_tab_push_var(tab, isl_tab_undo_allocate, &tab->var[r]) < 0)
1752 return -1;
1754 return r;
1757 /* Add a variable to the tableau and allocate a column for it.
1758 * Return the index into the variable array "var".
1760 int isl_tab_allocate_var(struct isl_tab *tab)
1762 if (!tab)
1763 return -1;
1765 return isl_tab_insert_var(tab, tab->n_var);
1768 /* Add a row to the tableau. The row is given as an affine combination
1769 * of the original variables and needs to be expressed in terms of the
1770 * column variables.
1772 * This function assumes that at least one more row and at least
1773 * one more element in the constraint array are available in the tableau.
1775 * We add each term in turn.
1776 * If r = n/d_r is the current sum and we need to add k x, then
1777 * if x is a column variable, we increase the numerator of
1778 * this column by k d_r
1779 * if x = f/d_x is a row variable, then the new representation of r is
1781 * n k f d_x/g n + d_r/g k f m/d_r n + m/d_g k f
1782 * --- + --- = ------------------- = -------------------
1783 * d_r d_r d_r d_x/g m
1785 * with g the gcd of d_r and d_x and m the lcm of d_r and d_x.
1787 * If tab->M is set, then, internally, each variable x is represented
1788 * as x' - M. We then also need no subtract k d_r from the coefficient of M.
1790 int isl_tab_add_row(struct isl_tab *tab, isl_int *line)
1792 int i;
1793 int r;
1794 isl_int *row;
1795 isl_int a, b;
1796 unsigned off = 2 + tab->M;
1798 r = isl_tab_allocate_con(tab);
1799 if (r < 0)
1800 return -1;
1802 isl_int_init(a);
1803 isl_int_init(b);
1804 row = tab->mat->row[tab->con[r].index];
1805 isl_int_set_si(row[0], 1);
1806 isl_int_set(row[1], line[0]);
1807 isl_seq_clr(row + 2, tab->M + tab->n_col);
1808 for (i = 0; i < tab->n_var; ++i) {
1809 if (tab->var[i].is_zero)
1810 continue;
1811 if (tab->var[i].is_row) {
1812 isl_int_lcm(a,
1813 row[0], tab->mat->row[tab->var[i].index][0]);
1814 isl_int_swap(a, row[0]);
1815 isl_int_divexact(a, row[0], a);
1816 isl_int_divexact(b,
1817 row[0], tab->mat->row[tab->var[i].index][0]);
1818 isl_int_mul(b, b, line[1 + i]);
1819 isl_seq_combine(row + 1, a, row + 1,
1820 b, tab->mat->row[tab->var[i].index] + 1,
1821 1 + tab->M + tab->n_col);
1822 } else
1823 isl_int_addmul(row[off + tab->var[i].index],
1824 line[1 + i], row[0]);
1825 if (tab->M && i >= tab->n_param && i < tab->n_var - tab->n_div)
1826 isl_int_submul(row[2], line[1 + i], row[0]);
1828 isl_seq_normalize(tab->mat->ctx, row, off + tab->n_col);
1829 isl_int_clear(a);
1830 isl_int_clear(b);
1832 if (tab->row_sign)
1833 tab->row_sign[tab->con[r].index] = isl_tab_row_unknown;
1835 return r;
1838 static int drop_row(struct isl_tab *tab, int row)
1840 isl_assert(tab->mat->ctx, ~tab->row_var[row] == tab->n_con - 1, return -1);
1841 if (row != tab->n_row - 1)
1842 swap_rows(tab, row, tab->n_row - 1);
1843 tab->n_row--;
1844 tab->n_con--;
1845 return 0;
1848 /* Drop the variable in column "col" along with the column.
1849 * The column is removed first because it may need to be moved
1850 * into the last position and this process requires
1851 * the contents of the col_var array in a state
1852 * before the removal of the variable.
1854 static int drop_col(struct isl_tab *tab, int col)
1856 int var;
1858 var = tab->col_var[col];
1859 if (col != tab->n_col - 1)
1860 swap_cols(tab, col, tab->n_col - 1);
1861 tab->n_col--;
1862 if (var_drop_entry(tab, var) < 0)
1863 return -1;
1864 return 0;
1867 /* Add inequality "ineq" and check if it conflicts with the
1868 * previously added constraints or if it is obviously redundant.
1870 * This function assumes that at least one more row and at least
1871 * one more element in the constraint array are available in the tableau.
1873 int isl_tab_add_ineq(struct isl_tab *tab, isl_int *ineq)
1875 int r;
1876 int sgn;
1877 isl_int cst;
1879 if (!tab)
1880 return -1;
1881 if (tab->bmap) {
1882 struct isl_basic_map *bmap = tab->bmap;
1884 isl_assert(tab->mat->ctx, tab->n_eq == bmap->n_eq, return -1);
1885 isl_assert(tab->mat->ctx,
1886 tab->n_con == bmap->n_eq + bmap->n_ineq, return -1);
1887 tab->bmap = isl_basic_map_add_ineq(tab->bmap, ineq);
1888 if (isl_tab_push(tab, isl_tab_undo_bmap_ineq) < 0)
1889 return -1;
1890 if (!tab->bmap)
1891 return -1;
1893 if (tab->cone) {
1894 isl_int_init(cst);
1895 isl_int_set_si(cst, 0);
1896 isl_int_swap(ineq[0], cst);
1898 r = isl_tab_add_row(tab, ineq);
1899 if (tab->cone) {
1900 isl_int_swap(ineq[0], cst);
1901 isl_int_clear(cst);
1903 if (r < 0)
1904 return -1;
1905 tab->con[r].is_nonneg = 1;
1906 if (isl_tab_push_var(tab, isl_tab_undo_nonneg, &tab->con[r]) < 0)
1907 return -1;
1908 if (isl_tab_row_is_redundant(tab, tab->con[r].index)) {
1909 if (isl_tab_mark_redundant(tab, tab->con[r].index) < 0)
1910 return -1;
1911 return 0;
1914 sgn = restore_row(tab, &tab->con[r]);
1915 if (sgn < -1)
1916 return -1;
1917 if (sgn < 0)
1918 return isl_tab_mark_empty(tab);
1919 if (tab->con[r].is_row && isl_tab_row_is_redundant(tab, tab->con[r].index))
1920 if (isl_tab_mark_redundant(tab, tab->con[r].index) < 0)
1921 return -1;
1922 return 0;
1925 /* Pivot a non-negative variable down until it reaches the value zero
1926 * and then pivot the variable into a column position.
1928 static int to_col(struct isl_tab *tab, struct isl_tab_var *var) WARN_UNUSED;
1929 static int to_col(struct isl_tab *tab, struct isl_tab_var *var)
1931 int i;
1932 int row, col;
1933 unsigned off = 2 + tab->M;
1935 if (!var->is_row)
1936 return 0;
1938 while (isl_int_is_pos(tab->mat->row[var->index][1])) {
1939 find_pivot(tab, var, NULL, -1, &row, &col);
1940 isl_assert(tab->mat->ctx, row != -1, return -1);
1941 if (isl_tab_pivot(tab, row, col) < 0)
1942 return -1;
1943 if (!var->is_row)
1944 return 0;
1947 for (i = tab->n_dead; i < tab->n_col; ++i)
1948 if (!isl_int_is_zero(tab->mat->row[var->index][off + i]))
1949 break;
1951 isl_assert(tab->mat->ctx, i < tab->n_col, return -1);
1952 if (isl_tab_pivot(tab, var->index, i) < 0)
1953 return -1;
1955 return 0;
1958 /* We assume Gaussian elimination has been performed on the equalities.
1959 * The equalities can therefore never conflict.
1960 * Adding the equalities is currently only really useful for a later call
1961 * to isl_tab_ineq_type.
1963 * This function assumes that at least one more row and at least
1964 * one more element in the constraint array are available in the tableau.
1966 static struct isl_tab *add_eq(struct isl_tab *tab, isl_int *eq)
1968 int i;
1969 int r;
1971 if (!tab)
1972 return NULL;
1973 r = isl_tab_add_row(tab, eq);
1974 if (r < 0)
1975 goto error;
1977 r = tab->con[r].index;
1978 i = isl_seq_first_non_zero(tab->mat->row[r] + 2 + tab->M + tab->n_dead,
1979 tab->n_col - tab->n_dead);
1980 isl_assert(tab->mat->ctx, i >= 0, goto error);
1981 i += tab->n_dead;
1982 if (isl_tab_pivot(tab, r, i) < 0)
1983 goto error;
1984 if (isl_tab_kill_col(tab, i) < 0)
1985 goto error;
1986 tab->n_eq++;
1988 return tab;
1989 error:
1990 isl_tab_free(tab);
1991 return NULL;
1994 static int row_is_manifestly_zero(struct isl_tab *tab, int row)
1996 unsigned off = 2 + tab->M;
1998 if (!isl_int_is_zero(tab->mat->row[row][1]))
1999 return 0;
2000 if (tab->M && !isl_int_is_zero(tab->mat->row[row][2]))
2001 return 0;
2002 return isl_seq_first_non_zero(tab->mat->row[row] + off + tab->n_dead,
2003 tab->n_col - tab->n_dead) == -1;
2006 /* Add an equality that is known to be valid for the given tableau.
2008 * This function assumes that at least one more row and at least
2009 * one more element in the constraint array are available in the tableau.
2011 int isl_tab_add_valid_eq(struct isl_tab *tab, isl_int *eq)
2013 struct isl_tab_var *var;
2014 int r;
2016 if (!tab)
2017 return -1;
2018 r = isl_tab_add_row(tab, eq);
2019 if (r < 0)
2020 return -1;
2022 var = &tab->con[r];
2023 r = var->index;
2024 if (row_is_manifestly_zero(tab, r)) {
2025 var->is_zero = 1;
2026 if (isl_tab_mark_redundant(tab, r) < 0)
2027 return -1;
2028 return 0;
2031 if (isl_int_is_neg(tab->mat->row[r][1])) {
2032 isl_seq_neg(tab->mat->row[r] + 1, tab->mat->row[r] + 1,
2033 1 + tab->n_col);
2034 var->negated = 1;
2036 var->is_nonneg = 1;
2037 if (to_col(tab, var) < 0)
2038 return -1;
2039 var->is_nonneg = 0;
2040 if (isl_tab_kill_col(tab, var->index) < 0)
2041 return -1;
2043 return 0;
2046 /* Add a zero row to "tab" and return the corresponding index
2047 * in the constraint array.
2049 * This function assumes that at least one more row and at least
2050 * one more element in the constraint array are available in the tableau.
2052 static int add_zero_row(struct isl_tab *tab)
2054 int r;
2055 isl_int *row;
2057 r = isl_tab_allocate_con(tab);
2058 if (r < 0)
2059 return -1;
2061 row = tab->mat->row[tab->con[r].index];
2062 isl_seq_clr(row + 1, 1 + tab->M + tab->n_col);
2063 isl_int_set_si(row[0], 1);
2065 return r;
2068 /* Add equality "eq" and check if it conflicts with the
2069 * previously added constraints or if it is obviously redundant.
2071 * This function assumes that at least one more row and at least
2072 * one more element in the constraint array are available in the tableau.
2073 * If tab->bmap is set, then two rows are needed instead of one.
2075 int isl_tab_add_eq(struct isl_tab *tab, isl_int *eq)
2077 struct isl_tab_undo *snap = NULL;
2078 struct isl_tab_var *var;
2079 int r;
2080 int row;
2081 int sgn;
2082 isl_int cst;
2084 if (!tab)
2085 return -1;
2086 isl_assert(tab->mat->ctx, !tab->M, return -1);
2088 if (tab->need_undo)
2089 snap = isl_tab_snap(tab);
2091 if (tab->cone) {
2092 isl_int_init(cst);
2093 isl_int_set_si(cst, 0);
2094 isl_int_swap(eq[0], cst);
2096 r = isl_tab_add_row(tab, eq);
2097 if (tab->cone) {
2098 isl_int_swap(eq[0], cst);
2099 isl_int_clear(cst);
2101 if (r < 0)
2102 return -1;
2104 var = &tab->con[r];
2105 row = var->index;
2106 if (row_is_manifestly_zero(tab, row)) {
2107 if (snap)
2108 return isl_tab_rollback(tab, snap);
2109 return drop_row(tab, row);
2112 if (tab->bmap) {
2113 tab->bmap = isl_basic_map_add_ineq(tab->bmap, eq);
2114 if (isl_tab_push(tab, isl_tab_undo_bmap_ineq) < 0)
2115 return -1;
2116 isl_seq_neg(eq, eq, 1 + tab->n_var);
2117 tab->bmap = isl_basic_map_add_ineq(tab->bmap, eq);
2118 isl_seq_neg(eq, eq, 1 + tab->n_var);
2119 if (isl_tab_push(tab, isl_tab_undo_bmap_ineq) < 0)
2120 return -1;
2121 if (!tab->bmap)
2122 return -1;
2123 if (add_zero_row(tab) < 0)
2124 return -1;
2127 sgn = isl_int_sgn(tab->mat->row[row][1]);
2129 if (sgn > 0) {
2130 isl_seq_neg(tab->mat->row[row] + 1, tab->mat->row[row] + 1,
2131 1 + tab->n_col);
2132 var->negated = 1;
2133 sgn = -1;
2136 if (sgn < 0) {
2137 sgn = sign_of_max(tab, var);
2138 if (sgn < -1)
2139 return -1;
2140 if (sgn < 0) {
2141 if (isl_tab_mark_empty(tab) < 0)
2142 return -1;
2143 return 0;
2147 var->is_nonneg = 1;
2148 if (to_col(tab, var) < 0)
2149 return -1;
2150 var->is_nonneg = 0;
2151 if (isl_tab_kill_col(tab, var->index) < 0)
2152 return -1;
2154 return 0;
2157 /* Construct and return an inequality that expresses an upper bound
2158 * on the given div.
2159 * In particular, if the div is given by
2161 * d = floor(e/m)
2163 * then the inequality expresses
2165 * m d <= e
2167 static struct isl_vec *ineq_for_div(struct isl_basic_map *bmap, unsigned div)
2169 unsigned total;
2170 unsigned div_pos;
2171 struct isl_vec *ineq;
2173 if (!bmap)
2174 return NULL;
2176 total = isl_basic_map_total_dim(bmap);
2177 div_pos = 1 + total - bmap->n_div + div;
2179 ineq = isl_vec_alloc(bmap->ctx, 1 + total);
2180 if (!ineq)
2181 return NULL;
2183 isl_seq_cpy(ineq->el, bmap->div[div] + 1, 1 + total);
2184 isl_int_neg(ineq->el[div_pos], bmap->div[div][0]);
2185 return ineq;
2188 /* For a div d = floor(f/m), add the constraints
2190 * f - m d >= 0
2191 * -(f-(m-1)) + m d >= 0
2193 * Note that the second constraint is the negation of
2195 * f - m d >= m
2197 * If add_ineq is not NULL, then this function is used
2198 * instead of isl_tab_add_ineq to effectively add the inequalities.
2200 * This function assumes that at least two more rows and at least
2201 * two more elements in the constraint array are available in the tableau.
2203 static int add_div_constraints(struct isl_tab *tab, unsigned div,
2204 int (*add_ineq)(void *user, isl_int *), void *user)
2206 unsigned total;
2207 unsigned div_pos;
2208 struct isl_vec *ineq;
2210 total = isl_basic_map_total_dim(tab->bmap);
2211 div_pos = 1 + total - tab->bmap->n_div + div;
2213 ineq = ineq_for_div(tab->bmap, div);
2214 if (!ineq)
2215 goto error;
2217 if (add_ineq) {
2218 if (add_ineq(user, ineq->el) < 0)
2219 goto error;
2220 } else {
2221 if (isl_tab_add_ineq(tab, ineq->el) < 0)
2222 goto error;
2225 isl_seq_neg(ineq->el, tab->bmap->div[div] + 1, 1 + total);
2226 isl_int_set(ineq->el[div_pos], tab->bmap->div[div][0]);
2227 isl_int_add(ineq->el[0], ineq->el[0], ineq->el[div_pos]);
2228 isl_int_sub_ui(ineq->el[0], ineq->el[0], 1);
2230 if (add_ineq) {
2231 if (add_ineq(user, ineq->el) < 0)
2232 goto error;
2233 } else {
2234 if (isl_tab_add_ineq(tab, ineq->el) < 0)
2235 goto error;
2238 isl_vec_free(ineq);
2240 return 0;
2241 error:
2242 isl_vec_free(ineq);
2243 return -1;
2246 /* Check whether the div described by "div" is obviously non-negative.
2247 * If we are using a big parameter, then we will encode the div
2248 * as div' = M + div, which is always non-negative.
2249 * Otherwise, we check whether div is a non-negative affine combination
2250 * of non-negative variables.
2252 static int div_is_nonneg(struct isl_tab *tab, __isl_keep isl_vec *div)
2254 int i;
2256 if (tab->M)
2257 return 1;
2259 if (isl_int_is_neg(div->el[1]))
2260 return 0;
2262 for (i = 0; i < tab->n_var; ++i) {
2263 if (isl_int_is_neg(div->el[2 + i]))
2264 return 0;
2265 if (isl_int_is_zero(div->el[2 + i]))
2266 continue;
2267 if (!tab->var[i].is_nonneg)
2268 return 0;
2271 return 1;
2274 /* Add an extra div, prescribed by "div" to the tableau and
2275 * the associated bmap (which is assumed to be non-NULL).
2277 * If add_ineq is not NULL, then this function is used instead
2278 * of isl_tab_add_ineq to add the div constraints.
2279 * This complication is needed because the code in isl_tab_pip
2280 * wants to perform some extra processing when an inequality
2281 * is added to the tableau.
2283 int isl_tab_add_div(struct isl_tab *tab, __isl_keep isl_vec *div,
2284 int (*add_ineq)(void *user, isl_int *), void *user)
2286 int r;
2287 int k;
2288 int nonneg;
2290 if (!tab || !div)
2291 return -1;
2293 isl_assert(tab->mat->ctx, tab->bmap, return -1);
2295 nonneg = div_is_nonneg(tab, div);
2297 if (isl_tab_extend_cons(tab, 3) < 0)
2298 return -1;
2299 if (isl_tab_extend_vars(tab, 1) < 0)
2300 return -1;
2301 r = isl_tab_allocate_var(tab);
2302 if (r < 0)
2303 return -1;
2305 if (nonneg)
2306 tab->var[r].is_nonneg = 1;
2308 tab->bmap = isl_basic_map_extend_space(tab->bmap,
2309 isl_basic_map_get_space(tab->bmap), 1, 0, 2);
2310 k = isl_basic_map_alloc_div(tab->bmap);
2311 if (k < 0)
2312 return -1;
2313 isl_seq_cpy(tab->bmap->div[k], div->el, div->size);
2314 if (isl_tab_push(tab, isl_tab_undo_bmap_div) < 0)
2315 return -1;
2317 if (add_div_constraints(tab, k, add_ineq, user) < 0)
2318 return -1;
2320 return r;
2323 /* If "track" is set, then we want to keep track of all constraints in tab
2324 * in its bmap field. This field is initialized from a copy of "bmap",
2325 * so we need to make sure that all constraints in "bmap" also appear
2326 * in the constructed tab.
2328 __isl_give struct isl_tab *isl_tab_from_basic_map(
2329 __isl_keep isl_basic_map *bmap, int track)
2331 int i;
2332 struct isl_tab *tab;
2334 if (!bmap)
2335 return NULL;
2336 tab = isl_tab_alloc(bmap->ctx,
2337 isl_basic_map_total_dim(bmap) + bmap->n_ineq + 1,
2338 isl_basic_map_total_dim(bmap), 0);
2339 if (!tab)
2340 return NULL;
2341 tab->preserve = track;
2342 tab->rational = ISL_F_ISSET(bmap, ISL_BASIC_MAP_RATIONAL);
2343 if (ISL_F_ISSET(bmap, ISL_BASIC_MAP_EMPTY)) {
2344 if (isl_tab_mark_empty(tab) < 0)
2345 goto error;
2346 goto done;
2348 for (i = 0; i < bmap->n_eq; ++i) {
2349 tab = add_eq(tab, bmap->eq[i]);
2350 if (!tab)
2351 return tab;
2353 for (i = 0; i < bmap->n_ineq; ++i) {
2354 if (isl_tab_add_ineq(tab, bmap->ineq[i]) < 0)
2355 goto error;
2356 if (tab->empty)
2357 goto done;
2359 done:
2360 if (track && isl_tab_track_bmap(tab, isl_basic_map_copy(bmap)) < 0)
2361 goto error;
2362 return tab;
2363 error:
2364 isl_tab_free(tab);
2365 return NULL;
2368 __isl_give struct isl_tab *isl_tab_from_basic_set(
2369 __isl_keep isl_basic_set *bset, int track)
2371 return isl_tab_from_basic_map(bset, track);
2374 /* Construct a tableau corresponding to the recession cone of "bset".
2376 struct isl_tab *isl_tab_from_recession_cone(__isl_keep isl_basic_set *bset,
2377 int parametric)
2379 isl_int cst;
2380 int i;
2381 struct isl_tab *tab;
2382 unsigned offset = 0;
2384 if (!bset)
2385 return NULL;
2386 if (parametric)
2387 offset = isl_basic_set_dim(bset, isl_dim_param);
2388 tab = isl_tab_alloc(bset->ctx, bset->n_eq + bset->n_ineq,
2389 isl_basic_set_total_dim(bset) - offset, 0);
2390 if (!tab)
2391 return NULL;
2392 tab->rational = ISL_F_ISSET(bset, ISL_BASIC_SET_RATIONAL);
2393 tab->cone = 1;
2395 isl_int_init(cst);
2396 isl_int_set_si(cst, 0);
2397 for (i = 0; i < bset->n_eq; ++i) {
2398 isl_int_swap(bset->eq[i][offset], cst);
2399 if (offset > 0) {
2400 if (isl_tab_add_eq(tab, bset->eq[i] + offset) < 0)
2401 goto error;
2402 } else
2403 tab = add_eq(tab, bset->eq[i]);
2404 isl_int_swap(bset->eq[i][offset], cst);
2405 if (!tab)
2406 goto done;
2408 for (i = 0; i < bset->n_ineq; ++i) {
2409 int r;
2410 isl_int_swap(bset->ineq[i][offset], cst);
2411 r = isl_tab_add_row(tab, bset->ineq[i] + offset);
2412 isl_int_swap(bset->ineq[i][offset], cst);
2413 if (r < 0)
2414 goto error;
2415 tab->con[r].is_nonneg = 1;
2416 if (isl_tab_push_var(tab, isl_tab_undo_nonneg, &tab->con[r]) < 0)
2417 goto error;
2419 done:
2420 isl_int_clear(cst);
2421 return tab;
2422 error:
2423 isl_int_clear(cst);
2424 isl_tab_free(tab);
2425 return NULL;
2428 /* Assuming "tab" is the tableau of a cone, check if the cone is
2429 * bounded, i.e., if it is empty or only contains the origin.
2431 int isl_tab_cone_is_bounded(struct isl_tab *tab)
2433 int i;
2435 if (!tab)
2436 return -1;
2437 if (tab->empty)
2438 return 1;
2439 if (tab->n_dead == tab->n_col)
2440 return 1;
2442 for (;;) {
2443 for (i = tab->n_redundant; i < tab->n_row; ++i) {
2444 struct isl_tab_var *var;
2445 int sgn;
2446 var = isl_tab_var_from_row(tab, i);
2447 if (!var->is_nonneg)
2448 continue;
2449 sgn = sign_of_max(tab, var);
2450 if (sgn < -1)
2451 return -1;
2452 if (sgn != 0)
2453 return 0;
2454 if (close_row(tab, var) < 0)
2455 return -1;
2456 break;
2458 if (tab->n_dead == tab->n_col)
2459 return 1;
2460 if (i == tab->n_row)
2461 return 0;
2465 int isl_tab_sample_is_integer(struct isl_tab *tab)
2467 int i;
2469 if (!tab)
2470 return -1;
2472 for (i = 0; i < tab->n_var; ++i) {
2473 int row;
2474 if (!tab->var[i].is_row)
2475 continue;
2476 row = tab->var[i].index;
2477 if (!isl_int_is_divisible_by(tab->mat->row[row][1],
2478 tab->mat->row[row][0]))
2479 return 0;
2481 return 1;
2484 static struct isl_vec *extract_integer_sample(struct isl_tab *tab)
2486 int i;
2487 struct isl_vec *vec;
2489 vec = isl_vec_alloc(tab->mat->ctx, 1 + tab->n_var);
2490 if (!vec)
2491 return NULL;
2493 isl_int_set_si(vec->block.data[0], 1);
2494 for (i = 0; i < tab->n_var; ++i) {
2495 if (!tab->var[i].is_row)
2496 isl_int_set_si(vec->block.data[1 + i], 0);
2497 else {
2498 int row = tab->var[i].index;
2499 isl_int_divexact(vec->block.data[1 + i],
2500 tab->mat->row[row][1], tab->mat->row[row][0]);
2504 return vec;
2507 struct isl_vec *isl_tab_get_sample_value(struct isl_tab *tab)
2509 int i;
2510 struct isl_vec *vec;
2511 isl_int m;
2513 if (!tab)
2514 return NULL;
2516 vec = isl_vec_alloc(tab->mat->ctx, 1 + tab->n_var);
2517 if (!vec)
2518 return NULL;
2520 isl_int_init(m);
2522 isl_int_set_si(vec->block.data[0], 1);
2523 for (i = 0; i < tab->n_var; ++i) {
2524 int row;
2525 if (!tab->var[i].is_row) {
2526 isl_int_set_si(vec->block.data[1 + i], 0);
2527 continue;
2529 row = tab->var[i].index;
2530 isl_int_gcd(m, vec->block.data[0], tab->mat->row[row][0]);
2531 isl_int_divexact(m, tab->mat->row[row][0], m);
2532 isl_seq_scale(vec->block.data, vec->block.data, m, 1 + i);
2533 isl_int_divexact(m, vec->block.data[0], tab->mat->row[row][0]);
2534 isl_int_mul(vec->block.data[1 + i], m, tab->mat->row[row][1]);
2536 vec = isl_vec_normalize(vec);
2538 isl_int_clear(m);
2539 return vec;
2542 /* Update "bmap" based on the results of the tableau "tab".
2543 * In particular, implicit equalities are made explicit, redundant constraints
2544 * are removed and if the sample value happens to be integer, it is stored
2545 * in "bmap" (unless "bmap" already had an integer sample).
2547 * The tableau is assumed to have been created from "bmap" using
2548 * isl_tab_from_basic_map.
2550 struct isl_basic_map *isl_basic_map_update_from_tab(struct isl_basic_map *bmap,
2551 struct isl_tab *tab)
2553 int i;
2554 unsigned n_eq;
2556 if (!bmap)
2557 return NULL;
2558 if (!tab)
2559 return bmap;
2561 n_eq = tab->n_eq;
2562 if (tab->empty)
2563 bmap = isl_basic_map_set_to_empty(bmap);
2564 else
2565 for (i = bmap->n_ineq - 1; i >= 0; --i) {
2566 if (isl_tab_is_equality(tab, n_eq + i))
2567 isl_basic_map_inequality_to_equality(bmap, i);
2568 else if (isl_tab_is_redundant(tab, n_eq + i))
2569 isl_basic_map_drop_inequality(bmap, i);
2571 if (bmap->n_eq != n_eq)
2572 bmap = isl_basic_map_gauss(bmap, NULL);
2573 if (!tab->rational &&
2574 bmap && !bmap->sample && isl_tab_sample_is_integer(tab))
2575 bmap->sample = extract_integer_sample(tab);
2576 return bmap;
2579 struct isl_basic_set *isl_basic_set_update_from_tab(struct isl_basic_set *bset,
2580 struct isl_tab *tab)
2582 return (struct isl_basic_set *)isl_basic_map_update_from_tab(
2583 (struct isl_basic_map *)bset, tab);
2586 /* Given a non-negative variable "var", add a new non-negative variable
2587 * that is the opposite of "var", ensuring that var can only attain the
2588 * value zero.
2589 * If var = n/d is a row variable, then the new variable = -n/d.
2590 * If var is a column variables, then the new variable = -var.
2591 * If the new variable cannot attain non-negative values, then
2592 * the resulting tableau is empty.
2593 * Otherwise, we know the value will be zero and we close the row.
2595 static int cut_to_hyperplane(struct isl_tab *tab, struct isl_tab_var *var)
2597 unsigned r;
2598 isl_int *row;
2599 int sgn;
2600 unsigned off = 2 + tab->M;
2602 if (var->is_zero)
2603 return 0;
2604 isl_assert(tab->mat->ctx, !var->is_redundant, return -1);
2605 isl_assert(tab->mat->ctx, var->is_nonneg, return -1);
2607 if (isl_tab_extend_cons(tab, 1) < 0)
2608 return -1;
2610 r = tab->n_con;
2611 tab->con[r].index = tab->n_row;
2612 tab->con[r].is_row = 1;
2613 tab->con[r].is_nonneg = 0;
2614 tab->con[r].is_zero = 0;
2615 tab->con[r].is_redundant = 0;
2616 tab->con[r].frozen = 0;
2617 tab->con[r].negated = 0;
2618 tab->row_var[tab->n_row] = ~r;
2619 row = tab->mat->row[tab->n_row];
2621 if (var->is_row) {
2622 isl_int_set(row[0], tab->mat->row[var->index][0]);
2623 isl_seq_neg(row + 1,
2624 tab->mat->row[var->index] + 1, 1 + tab->n_col);
2625 } else {
2626 isl_int_set_si(row[0], 1);
2627 isl_seq_clr(row + 1, 1 + tab->n_col);
2628 isl_int_set_si(row[off + var->index], -1);
2631 tab->n_row++;
2632 tab->n_con++;
2633 if (isl_tab_push_var(tab, isl_tab_undo_allocate, &tab->con[r]) < 0)
2634 return -1;
2636 sgn = sign_of_max(tab, &tab->con[r]);
2637 if (sgn < -1)
2638 return -1;
2639 if (sgn < 0) {
2640 if (isl_tab_mark_empty(tab) < 0)
2641 return -1;
2642 return 0;
2644 tab->con[r].is_nonneg = 1;
2645 if (isl_tab_push_var(tab, isl_tab_undo_nonneg, &tab->con[r]) < 0)
2646 return -1;
2647 /* sgn == 0 */
2648 if (close_row(tab, &tab->con[r]) < 0)
2649 return -1;
2651 return 0;
2654 /* Given a tableau "tab" and an inequality constraint "con" of the tableau,
2655 * relax the inequality by one. That is, the inequality r >= 0 is replaced
2656 * by r' = r + 1 >= 0.
2657 * If r is a row variable, we simply increase the constant term by one
2658 * (taking into account the denominator).
2659 * If r is a column variable, then we need to modify each row that
2660 * refers to r = r' - 1 by substituting this equality, effectively
2661 * subtracting the coefficient of the column from the constant.
2662 * We should only do this if the minimum is manifestly unbounded,
2663 * however. Otherwise, we may end up with negative sample values
2664 * for non-negative variables.
2665 * So, if r is a column variable with a minimum that is not
2666 * manifestly unbounded, then we need to move it to a row.
2667 * However, the sample value of this row may be negative,
2668 * even after the relaxation, so we need to restore it.
2669 * We therefore prefer to pivot a column up to a row, if possible.
2671 int isl_tab_relax(struct isl_tab *tab, int con)
2673 struct isl_tab_var *var;
2675 if (!tab)
2676 return -1;
2678 var = &tab->con[con];
2680 if (var->is_row && (var->index < 0 || var->index < tab->n_redundant))
2681 isl_die(tab->mat->ctx, isl_error_invalid,
2682 "cannot relax redundant constraint", return -1);
2683 if (!var->is_row && (var->index < 0 || var->index < tab->n_dead))
2684 isl_die(tab->mat->ctx, isl_error_invalid,
2685 "cannot relax dead constraint", return -1);
2687 if (!var->is_row && !max_is_manifestly_unbounded(tab, var))
2688 if (to_row(tab, var, 1) < 0)
2689 return -1;
2690 if (!var->is_row && !min_is_manifestly_unbounded(tab, var))
2691 if (to_row(tab, var, -1) < 0)
2692 return -1;
2694 if (var->is_row) {
2695 isl_int_add(tab->mat->row[var->index][1],
2696 tab->mat->row[var->index][1], tab->mat->row[var->index][0]);
2697 if (restore_row(tab, var) < 0)
2698 return -1;
2699 } else {
2700 int i;
2701 unsigned off = 2 + tab->M;
2703 for (i = 0; i < tab->n_row; ++i) {
2704 if (isl_int_is_zero(tab->mat->row[i][off + var->index]))
2705 continue;
2706 isl_int_sub(tab->mat->row[i][1], tab->mat->row[i][1],
2707 tab->mat->row[i][off + var->index]);
2712 if (isl_tab_push_var(tab, isl_tab_undo_relax, var) < 0)
2713 return -1;
2715 return 0;
2718 /* Replace the variable v at position "pos" in the tableau "tab"
2719 * by v' = v + shift.
2721 * If the variable is in a column, then we first check if we can
2722 * simply plug in v = v' - shift. The effect on a row with
2723 * coefficient f/d for variable v is that the constant term c/d
2724 * is replaced by (c - f * shift)/d. If shift is positive and
2725 * f is negative for each row that needs to remain non-negative,
2726 * then this is clearly safe. In other words, if the minimum of v
2727 * is manifestly unbounded, then we can keep v in a column position.
2728 * Otherwise, we can pivot it down to a row.
2729 * Similarly, if shift is negative, we need to check if the maximum
2730 * of is manifestly unbounded.
2732 * If the variable is in a row (from the start or after pivoting),
2733 * then the constant term c/d is replaced by (c + d * shift)/d.
2735 int isl_tab_shift_var(struct isl_tab *tab, int pos, isl_int shift)
2737 struct isl_tab_var *var;
2739 if (!tab)
2740 return -1;
2741 if (isl_int_is_zero(shift))
2742 return 0;
2744 var = &tab->var[pos];
2745 if (!var->is_row) {
2746 if (isl_int_is_neg(shift)) {
2747 if (!max_is_manifestly_unbounded(tab, var))
2748 if (to_row(tab, var, 1) < 0)
2749 return -1;
2750 } else {
2751 if (!min_is_manifestly_unbounded(tab, var))
2752 if (to_row(tab, var, -1) < 0)
2753 return -1;
2757 if (var->is_row) {
2758 isl_int_addmul(tab->mat->row[var->index][1],
2759 shift, tab->mat->row[var->index][0]);
2760 } else {
2761 int i;
2762 unsigned off = 2 + tab->M;
2764 for (i = 0; i < tab->n_row; ++i) {
2765 if (isl_int_is_zero(tab->mat->row[i][off + var->index]))
2766 continue;
2767 isl_int_submul(tab->mat->row[i][1],
2768 shift, tab->mat->row[i][off + var->index]);
2773 return 0;
2776 /* Remove the sign constraint from constraint "con".
2778 * If the constraint variable was originally marked non-negative,
2779 * then we make sure we mark it non-negative again during rollback.
2781 int isl_tab_unrestrict(struct isl_tab *tab, int con)
2783 struct isl_tab_var *var;
2785 if (!tab)
2786 return -1;
2788 var = &tab->con[con];
2789 if (!var->is_nonneg)
2790 return 0;
2792 var->is_nonneg = 0;
2793 if (isl_tab_push_var(tab, isl_tab_undo_unrestrict, var) < 0)
2794 return -1;
2796 return 0;
2799 int isl_tab_select_facet(struct isl_tab *tab, int con)
2801 if (!tab)
2802 return -1;
2804 return cut_to_hyperplane(tab, &tab->con[con]);
2807 static int may_be_equality(struct isl_tab *tab, int row)
2809 return tab->rational ? isl_int_is_zero(tab->mat->row[row][1])
2810 : isl_int_lt(tab->mat->row[row][1],
2811 tab->mat->row[row][0]);
2814 /* Check for (near) equalities among the constraints.
2815 * A constraint is an equality if it is non-negative and if
2816 * its maximal value is either
2817 * - zero (in case of rational tableaus), or
2818 * - strictly less than 1 (in case of integer tableaus)
2820 * We first mark all non-redundant and non-dead variables that
2821 * are not frozen and not obviously not an equality.
2822 * Then we iterate over all marked variables if they can attain
2823 * any values larger than zero or at least one.
2824 * If the maximal value is zero, we mark any column variables
2825 * that appear in the row as being zero and mark the row as being redundant.
2826 * Otherwise, if the maximal value is strictly less than one (and the
2827 * tableau is integer), then we restrict the value to being zero
2828 * by adding an opposite non-negative variable.
2830 int isl_tab_detect_implicit_equalities(struct isl_tab *tab)
2832 int i;
2833 unsigned n_marked;
2835 if (!tab)
2836 return -1;
2837 if (tab->empty)
2838 return 0;
2839 if (tab->n_dead == tab->n_col)
2840 return 0;
2842 n_marked = 0;
2843 for (i = tab->n_redundant; i < tab->n_row; ++i) {
2844 struct isl_tab_var *var = isl_tab_var_from_row(tab, i);
2845 var->marked = !var->frozen && var->is_nonneg &&
2846 may_be_equality(tab, i);
2847 if (var->marked)
2848 n_marked++;
2850 for (i = tab->n_dead; i < tab->n_col; ++i) {
2851 struct isl_tab_var *var = var_from_col(tab, i);
2852 var->marked = !var->frozen && var->is_nonneg;
2853 if (var->marked)
2854 n_marked++;
2856 while (n_marked) {
2857 struct isl_tab_var *var;
2858 int sgn;
2859 for (i = tab->n_redundant; i < tab->n_row; ++i) {
2860 var = isl_tab_var_from_row(tab, i);
2861 if (var->marked)
2862 break;
2864 if (i == tab->n_row) {
2865 for (i = tab->n_dead; i < tab->n_col; ++i) {
2866 var = var_from_col(tab, i);
2867 if (var->marked)
2868 break;
2870 if (i == tab->n_col)
2871 break;
2873 var->marked = 0;
2874 n_marked--;
2875 sgn = sign_of_max(tab, var);
2876 if (sgn < 0)
2877 return -1;
2878 if (sgn == 0) {
2879 if (close_row(tab, var) < 0)
2880 return -1;
2881 } else if (!tab->rational && !at_least_one(tab, var)) {
2882 if (cut_to_hyperplane(tab, var) < 0)
2883 return -1;
2884 return isl_tab_detect_implicit_equalities(tab);
2886 for (i = tab->n_redundant; i < tab->n_row; ++i) {
2887 var = isl_tab_var_from_row(tab, i);
2888 if (!var->marked)
2889 continue;
2890 if (may_be_equality(tab, i))
2891 continue;
2892 var->marked = 0;
2893 n_marked--;
2897 return 0;
2900 /* Update the element of row_var or col_var that corresponds to
2901 * constraint tab->con[i] to a move from position "old" to position "i".
2903 static int update_con_after_move(struct isl_tab *tab, int i, int old)
2905 int *p;
2906 int index;
2908 index = tab->con[i].index;
2909 if (index == -1)
2910 return 0;
2911 p = tab->con[i].is_row ? tab->row_var : tab->col_var;
2912 if (p[index] != ~old)
2913 isl_die(tab->mat->ctx, isl_error_internal,
2914 "broken internal state", return -1);
2915 p[index] = ~i;
2917 return 0;
2920 /* Rotate the "n" constraints starting at "first" to the right,
2921 * putting the last constraint in the position of the first constraint.
2923 static int rotate_constraints(struct isl_tab *tab, int first, int n)
2925 int i, last;
2926 struct isl_tab_var var;
2928 if (n <= 1)
2929 return 0;
2931 last = first + n - 1;
2932 var = tab->con[last];
2933 for (i = last; i > first; --i) {
2934 tab->con[i] = tab->con[i - 1];
2935 if (update_con_after_move(tab, i, i - 1) < 0)
2936 return -1;
2938 tab->con[first] = var;
2939 if (update_con_after_move(tab, first, last) < 0)
2940 return -1;
2942 return 0;
2945 /* Make the equalities that are implicit in "bmap" but that have been
2946 * detected in the corresponding "tab" explicit in "bmap" and update
2947 * "tab" to reflect the new order of the constraints.
2949 * In particular, if inequality i is an implicit equality then
2950 * isl_basic_map_inequality_to_equality will move the inequality
2951 * in front of the other equality and it will move the last inequality
2952 * in the position of inequality i.
2953 * In the tableau, the inequalities of "bmap" are stored after the equalities
2954 * and so the original order
2956 * E E E E E A A A I B B B B L
2958 * is changed into
2960 * I E E E E E A A A L B B B B
2962 * where I is the implicit equality, the E are equalities,
2963 * the A inequalities before I, the B inequalities after I and
2964 * L the last inequality.
2965 * We therefore need to rotate to the right two sets of constraints,
2966 * those up to and including I and those after I.
2968 * If "tab" contains any constraints that are not in "bmap" then they
2969 * appear after those in "bmap" and they should be left untouched.
2971 * Note that this function leaves "bmap" in a temporary state
2972 * as it does not call isl_basic_map_gauss. Calling this function
2973 * is the responsibility of the caller.
2975 __isl_give isl_basic_map *isl_tab_make_equalities_explicit(struct isl_tab *tab,
2976 __isl_take isl_basic_map *bmap)
2978 int i;
2980 if (!tab || !bmap)
2981 return isl_basic_map_free(bmap);
2982 if (tab->empty)
2983 return bmap;
2985 for (i = bmap->n_ineq - 1; i >= 0; --i) {
2986 if (!isl_tab_is_equality(tab, bmap->n_eq + i))
2987 continue;
2988 isl_basic_map_inequality_to_equality(bmap, i);
2989 if (rotate_constraints(tab, 0, tab->n_eq + i + 1) < 0)
2990 return isl_basic_map_free(bmap);
2991 if (rotate_constraints(tab, tab->n_eq + i + 1,
2992 bmap->n_ineq - i) < 0)
2993 return isl_basic_map_free(bmap);
2994 tab->n_eq++;
2997 return bmap;
3000 static int con_is_redundant(struct isl_tab *tab, struct isl_tab_var *var)
3002 if (!tab)
3003 return -1;
3004 if (tab->rational) {
3005 int sgn = sign_of_min(tab, var);
3006 if (sgn < -1)
3007 return -1;
3008 return sgn >= 0;
3009 } else {
3010 int irred = isl_tab_min_at_most_neg_one(tab, var);
3011 if (irred < 0)
3012 return -1;
3013 return !irred;
3017 /* Return an isl_tab_var that has been marked or NULL if no such
3018 * variable can be found.
3019 * The marked field has only been set for variables that
3020 * appear in non-redundant rows or non-dead columns.
3022 * Pick the last constraint variable that is marked and
3023 * that appears in either a non-redundant row or a non-dead columns.
3024 * Since the returned variable is tested for being a redundant constraint,
3025 * there is no need to return any tab variable that corresponds to a variable.
3027 static struct isl_tab_var *select_marked(struct isl_tab *tab)
3029 int i;
3030 struct isl_tab_var *var;
3032 for (i = tab->n_con - 1; i >= 0; --i) {
3033 var = &tab->con[i];
3034 if (var->index < 0)
3035 continue;
3036 if (var->is_row && var->index < tab->n_redundant)
3037 continue;
3038 if (!var->is_row && var->index < tab->n_dead)
3039 continue;
3040 if (var->marked)
3041 return var;
3044 return NULL;
3047 /* Check for (near) redundant constraints.
3048 * A constraint is redundant if it is non-negative and if
3049 * its minimal value (temporarily ignoring the non-negativity) is either
3050 * - zero (in case of rational tableaus), or
3051 * - strictly larger than -1 (in case of integer tableaus)
3053 * We first mark all non-redundant and non-dead variables that
3054 * are not frozen and not obviously negatively unbounded.
3055 * Then we iterate over all marked variables if they can attain
3056 * any values smaller than zero or at most negative one.
3057 * If not, we mark the row as being redundant (assuming it hasn't
3058 * been detected as being obviously redundant in the mean time).
3060 int isl_tab_detect_redundant(struct isl_tab *tab)
3062 int i;
3063 unsigned n_marked;
3065 if (!tab)
3066 return -1;
3067 if (tab->empty)
3068 return 0;
3069 if (tab->n_redundant == tab->n_row)
3070 return 0;
3072 n_marked = 0;
3073 for (i = tab->n_redundant; i < tab->n_row; ++i) {
3074 struct isl_tab_var *var = isl_tab_var_from_row(tab, i);
3075 var->marked = !var->frozen && var->is_nonneg;
3076 if (var->marked)
3077 n_marked++;
3079 for (i = tab->n_dead; i < tab->n_col; ++i) {
3080 struct isl_tab_var *var = var_from_col(tab, i);
3081 var->marked = !var->frozen && var->is_nonneg &&
3082 !min_is_manifestly_unbounded(tab, var);
3083 if (var->marked)
3084 n_marked++;
3086 while (n_marked) {
3087 struct isl_tab_var *var;
3088 int red;
3089 var = select_marked(tab);
3090 if (!var)
3091 break;
3092 var->marked = 0;
3093 n_marked--;
3094 red = con_is_redundant(tab, var);
3095 if (red < 0)
3096 return -1;
3097 if (red && !var->is_redundant)
3098 if (isl_tab_mark_redundant(tab, var->index) < 0)
3099 return -1;
3100 for (i = tab->n_dead; i < tab->n_col; ++i) {
3101 var = var_from_col(tab, i);
3102 if (!var->marked)
3103 continue;
3104 if (!min_is_manifestly_unbounded(tab, var))
3105 continue;
3106 var->marked = 0;
3107 n_marked--;
3111 return 0;
3114 int isl_tab_is_equality(struct isl_tab *tab, int con)
3116 int row;
3117 unsigned off;
3119 if (!tab)
3120 return -1;
3121 if (tab->con[con].is_zero)
3122 return 1;
3123 if (tab->con[con].is_redundant)
3124 return 0;
3125 if (!tab->con[con].is_row)
3126 return tab->con[con].index < tab->n_dead;
3128 row = tab->con[con].index;
3130 off = 2 + tab->M;
3131 return isl_int_is_zero(tab->mat->row[row][1]) &&
3132 (!tab->M || isl_int_is_zero(tab->mat->row[row][2])) &&
3133 isl_seq_first_non_zero(tab->mat->row[row] + off + tab->n_dead,
3134 tab->n_col - tab->n_dead) == -1;
3137 /* Return the minimal value of the affine expression "f" with denominator
3138 * "denom" in *opt, *opt_denom, assuming the tableau is not empty and
3139 * the expression cannot attain arbitrarily small values.
3140 * If opt_denom is NULL, then *opt is rounded up to the nearest integer.
3141 * The return value reflects the nature of the result (empty, unbounded,
3142 * minimal value returned in *opt).
3144 * This function assumes that at least one more row and at least
3145 * one more element in the constraint array are available in the tableau.
3147 enum isl_lp_result isl_tab_min(struct isl_tab *tab,
3148 isl_int *f, isl_int denom, isl_int *opt, isl_int *opt_denom,
3149 unsigned flags)
3151 int r;
3152 enum isl_lp_result res = isl_lp_ok;
3153 struct isl_tab_var *var;
3154 struct isl_tab_undo *snap;
3156 if (!tab)
3157 return isl_lp_error;
3159 if (tab->empty)
3160 return isl_lp_empty;
3162 snap = isl_tab_snap(tab);
3163 r = isl_tab_add_row(tab, f);
3164 if (r < 0)
3165 return isl_lp_error;
3166 var = &tab->con[r];
3167 for (;;) {
3168 int row, col;
3169 find_pivot(tab, var, var, -1, &row, &col);
3170 if (row == var->index) {
3171 res = isl_lp_unbounded;
3172 break;
3174 if (row == -1)
3175 break;
3176 if (isl_tab_pivot(tab, row, col) < 0)
3177 return isl_lp_error;
3179 isl_int_mul(tab->mat->row[var->index][0],
3180 tab->mat->row[var->index][0], denom);
3181 if (ISL_FL_ISSET(flags, ISL_TAB_SAVE_DUAL)) {
3182 int i;
3184 isl_vec_free(tab->dual);
3185 tab->dual = isl_vec_alloc(tab->mat->ctx, 1 + tab->n_con);
3186 if (!tab->dual)
3187 return isl_lp_error;
3188 isl_int_set(tab->dual->el[0], tab->mat->row[var->index][0]);
3189 for (i = 0; i < tab->n_con; ++i) {
3190 int pos;
3191 if (tab->con[i].is_row) {
3192 isl_int_set_si(tab->dual->el[1 + i], 0);
3193 continue;
3195 pos = 2 + tab->M + tab->con[i].index;
3196 if (tab->con[i].negated)
3197 isl_int_neg(tab->dual->el[1 + i],
3198 tab->mat->row[var->index][pos]);
3199 else
3200 isl_int_set(tab->dual->el[1 + i],
3201 tab->mat->row[var->index][pos]);
3204 if (opt && res == isl_lp_ok) {
3205 if (opt_denom) {
3206 isl_int_set(*opt, tab->mat->row[var->index][1]);
3207 isl_int_set(*opt_denom, tab->mat->row[var->index][0]);
3208 } else
3209 isl_int_cdiv_q(*opt, tab->mat->row[var->index][1],
3210 tab->mat->row[var->index][0]);
3212 if (isl_tab_rollback(tab, snap) < 0)
3213 return isl_lp_error;
3214 return res;
3217 /* Is the constraint at position "con" marked as being redundant?
3218 * If it is marked as representing an equality, then it is not
3219 * considered to be redundant.
3220 * Note that isl_tab_mark_redundant marks both the isl_tab_var as
3221 * redundant and moves the corresponding row into the first
3222 * tab->n_redundant positions (or removes the row, assigning it index -1),
3223 * so the final test is actually redundant itself.
3225 int isl_tab_is_redundant(struct isl_tab *tab, int con)
3227 if (!tab)
3228 return -1;
3229 if (con < 0 || con >= tab->n_con)
3230 isl_die(isl_tab_get_ctx(tab), isl_error_invalid,
3231 "position out of bounds", return -1);
3232 if (tab->con[con].is_zero)
3233 return 0;
3234 if (tab->con[con].is_redundant)
3235 return 1;
3236 return tab->con[con].is_row && tab->con[con].index < tab->n_redundant;
3239 /* Take a snapshot of the tableau that can be restored by a call to
3240 * isl_tab_rollback.
3242 struct isl_tab_undo *isl_tab_snap(struct isl_tab *tab)
3244 if (!tab)
3245 return NULL;
3246 tab->need_undo = 1;
3247 return tab->top;
3250 /* Does "tab" need to keep track of undo information?
3251 * That is, was a snapshot taken that may need to be restored?
3253 isl_bool isl_tab_need_undo(struct isl_tab *tab)
3255 if (!tab)
3256 return isl_bool_error;
3258 return tab->need_undo;
3261 /* Remove all tracking of undo information from "tab", invalidating
3262 * any snapshots that may have been taken of the tableau.
3263 * Since all snapshots have been invalidated, there is also
3264 * no need to start keeping track of undo information again.
3266 void isl_tab_clear_undo(struct isl_tab *tab)
3268 if (!tab)
3269 return;
3271 free_undo(tab);
3272 tab->need_undo = 0;
3275 /* Undo the operation performed by isl_tab_relax.
3277 static int unrelax(struct isl_tab *tab, struct isl_tab_var *var) WARN_UNUSED;
3278 static int unrelax(struct isl_tab *tab, struct isl_tab_var *var)
3280 unsigned off = 2 + tab->M;
3282 if (!var->is_row && !max_is_manifestly_unbounded(tab, var))
3283 if (to_row(tab, var, 1) < 0)
3284 return -1;
3286 if (var->is_row) {
3287 isl_int_sub(tab->mat->row[var->index][1],
3288 tab->mat->row[var->index][1], tab->mat->row[var->index][0]);
3289 if (var->is_nonneg) {
3290 int sgn = restore_row(tab, var);
3291 isl_assert(tab->mat->ctx, sgn >= 0, return -1);
3293 } else {
3294 int i;
3296 for (i = 0; i < tab->n_row; ++i) {
3297 if (isl_int_is_zero(tab->mat->row[i][off + var->index]))
3298 continue;
3299 isl_int_add(tab->mat->row[i][1], tab->mat->row[i][1],
3300 tab->mat->row[i][off + var->index]);
3305 return 0;
3308 /* Undo the operation performed by isl_tab_unrestrict.
3310 * In particular, mark the variable as being non-negative and make
3311 * sure the sample value respects this constraint.
3313 static int ununrestrict(struct isl_tab *tab, struct isl_tab_var *var)
3315 var->is_nonneg = 1;
3317 if (var->is_row && restore_row(tab, var) < -1)
3318 return -1;
3320 return 0;
3323 static int perform_undo_var(struct isl_tab *tab, struct isl_tab_undo *undo) WARN_UNUSED;
3324 static int perform_undo_var(struct isl_tab *tab, struct isl_tab_undo *undo)
3326 struct isl_tab_var *var = var_from_index(tab, undo->u.var_index);
3327 switch (undo->type) {
3328 case isl_tab_undo_nonneg:
3329 var->is_nonneg = 0;
3330 break;
3331 case isl_tab_undo_redundant:
3332 var->is_redundant = 0;
3333 tab->n_redundant--;
3334 restore_row(tab, isl_tab_var_from_row(tab, tab->n_redundant));
3335 break;
3336 case isl_tab_undo_freeze:
3337 var->frozen = 0;
3338 break;
3339 case isl_tab_undo_zero:
3340 var->is_zero = 0;
3341 if (!var->is_row)
3342 tab->n_dead--;
3343 break;
3344 case isl_tab_undo_allocate:
3345 if (undo->u.var_index >= 0) {
3346 isl_assert(tab->mat->ctx, !var->is_row, return -1);
3347 return drop_col(tab, var->index);
3349 if (!var->is_row) {
3350 if (!max_is_manifestly_unbounded(tab, var)) {
3351 if (to_row(tab, var, 1) < 0)
3352 return -1;
3353 } else if (!min_is_manifestly_unbounded(tab, var)) {
3354 if (to_row(tab, var, -1) < 0)
3355 return -1;
3356 } else
3357 if (to_row(tab, var, 0) < 0)
3358 return -1;
3360 return drop_row(tab, var->index);
3361 case isl_tab_undo_relax:
3362 return unrelax(tab, var);
3363 case isl_tab_undo_unrestrict:
3364 return ununrestrict(tab, var);
3365 default:
3366 isl_die(tab->mat->ctx, isl_error_internal,
3367 "perform_undo_var called on invalid undo record",
3368 return -1);
3371 return 0;
3374 /* Restore the tableau to the state where the basic variables
3375 * are those in "col_var".
3376 * We first construct a list of variables that are currently in
3377 * the basis, but shouldn't. Then we iterate over all variables
3378 * that should be in the basis and for each one that is currently
3379 * not in the basis, we exchange it with one of the elements of the
3380 * list constructed before.
3381 * We can always find an appropriate variable to pivot with because
3382 * the current basis is mapped to the old basis by a non-singular
3383 * matrix and so we can never end up with a zero row.
3385 static int restore_basis(struct isl_tab *tab, int *col_var)
3387 int i, j;
3388 int n_extra = 0;
3389 int *extra = NULL; /* current columns that contain bad stuff */
3390 unsigned off = 2 + tab->M;
3392 extra = isl_alloc_array(tab->mat->ctx, int, tab->n_col);
3393 if (tab->n_col && !extra)
3394 goto error;
3395 for (i = 0; i < tab->n_col; ++i) {
3396 for (j = 0; j < tab->n_col; ++j)
3397 if (tab->col_var[i] == col_var[j])
3398 break;
3399 if (j < tab->n_col)
3400 continue;
3401 extra[n_extra++] = i;
3403 for (i = 0; i < tab->n_col && n_extra > 0; ++i) {
3404 struct isl_tab_var *var;
3405 int row;
3407 for (j = 0; j < tab->n_col; ++j)
3408 if (col_var[i] == tab->col_var[j])
3409 break;
3410 if (j < tab->n_col)
3411 continue;
3412 var = var_from_index(tab, col_var[i]);
3413 row = var->index;
3414 for (j = 0; j < n_extra; ++j)
3415 if (!isl_int_is_zero(tab->mat->row[row][off+extra[j]]))
3416 break;
3417 isl_assert(tab->mat->ctx, j < n_extra, goto error);
3418 if (isl_tab_pivot(tab, row, extra[j]) < 0)
3419 goto error;
3420 extra[j] = extra[--n_extra];
3423 free(extra);
3424 return 0;
3425 error:
3426 free(extra);
3427 return -1;
3430 /* Remove all samples with index n or greater, i.e., those samples
3431 * that were added since we saved this number of samples in
3432 * isl_tab_save_samples.
3434 static void drop_samples_since(struct isl_tab *tab, int n)
3436 int i;
3438 for (i = tab->n_sample - 1; i >= 0 && tab->n_sample > n; --i) {
3439 if (tab->sample_index[i] < n)
3440 continue;
3442 if (i != tab->n_sample - 1) {
3443 int t = tab->sample_index[tab->n_sample-1];
3444 tab->sample_index[tab->n_sample-1] = tab->sample_index[i];
3445 tab->sample_index[i] = t;
3446 isl_mat_swap_rows(tab->samples, tab->n_sample-1, i);
3448 tab->n_sample--;
3452 static int perform_undo(struct isl_tab *tab, struct isl_tab_undo *undo) WARN_UNUSED;
3453 static int perform_undo(struct isl_tab *tab, struct isl_tab_undo *undo)
3455 switch (undo->type) {
3456 case isl_tab_undo_rational:
3457 tab->rational = 0;
3458 break;
3459 case isl_tab_undo_empty:
3460 tab->empty = 0;
3461 break;
3462 case isl_tab_undo_nonneg:
3463 case isl_tab_undo_redundant:
3464 case isl_tab_undo_freeze:
3465 case isl_tab_undo_zero:
3466 case isl_tab_undo_allocate:
3467 case isl_tab_undo_relax:
3468 case isl_tab_undo_unrestrict:
3469 return perform_undo_var(tab, undo);
3470 case isl_tab_undo_bmap_eq:
3471 return isl_basic_map_free_equality(tab->bmap, 1);
3472 case isl_tab_undo_bmap_ineq:
3473 return isl_basic_map_free_inequality(tab->bmap, 1);
3474 case isl_tab_undo_bmap_div:
3475 if (isl_basic_map_free_div(tab->bmap, 1) < 0)
3476 return -1;
3477 if (tab->samples)
3478 tab->samples->n_col--;
3479 break;
3480 case isl_tab_undo_saved_basis:
3481 if (restore_basis(tab, undo->u.col_var) < 0)
3482 return -1;
3483 break;
3484 case isl_tab_undo_drop_sample:
3485 tab->n_outside--;
3486 break;
3487 case isl_tab_undo_saved_samples:
3488 drop_samples_since(tab, undo->u.n);
3489 break;
3490 case isl_tab_undo_callback:
3491 return undo->u.callback->run(undo->u.callback);
3492 default:
3493 isl_assert(tab->mat->ctx, 0, return -1);
3495 return 0;
3498 /* Return the tableau to the state it was in when the snapshot "snap"
3499 * was taken.
3501 int isl_tab_rollback(struct isl_tab *tab, struct isl_tab_undo *snap)
3503 struct isl_tab_undo *undo, *next;
3505 if (!tab)
3506 return -1;
3508 tab->in_undo = 1;
3509 for (undo = tab->top; undo && undo != &tab->bottom; undo = next) {
3510 next = undo->next;
3511 if (undo == snap)
3512 break;
3513 if (perform_undo(tab, undo) < 0) {
3514 tab->top = undo;
3515 free_undo(tab);
3516 tab->in_undo = 0;
3517 return -1;
3519 free_undo_record(undo);
3521 tab->in_undo = 0;
3522 tab->top = undo;
3523 if (!undo)
3524 return -1;
3525 return 0;
3528 /* The given row "row" represents an inequality violated by all
3529 * points in the tableau. Check for some special cases of such
3530 * separating constraints.
3531 * In particular, if the row has been reduced to the constant -1,
3532 * then we know the inequality is adjacent (but opposite) to
3533 * an equality in the tableau.
3534 * If the row has been reduced to r = c*(-1 -r'), with r' an inequality
3535 * of the tableau and c a positive constant, then the inequality
3536 * is adjacent (but opposite) to the inequality r'.
3538 static enum isl_ineq_type separation_type(struct isl_tab *tab, unsigned row)
3540 int pos;
3541 unsigned off = 2 + tab->M;
3543 if (tab->rational)
3544 return isl_ineq_separate;
3546 if (!isl_int_is_one(tab->mat->row[row][0]))
3547 return isl_ineq_separate;
3549 pos = isl_seq_first_non_zero(tab->mat->row[row] + off + tab->n_dead,
3550 tab->n_col - tab->n_dead);
3551 if (pos == -1) {
3552 if (isl_int_is_negone(tab->mat->row[row][1]))
3553 return isl_ineq_adj_eq;
3554 else
3555 return isl_ineq_separate;
3558 if (!isl_int_eq(tab->mat->row[row][1],
3559 tab->mat->row[row][off + tab->n_dead + pos]))
3560 return isl_ineq_separate;
3562 pos = isl_seq_first_non_zero(
3563 tab->mat->row[row] + off + tab->n_dead + pos + 1,
3564 tab->n_col - tab->n_dead - pos - 1);
3566 return pos == -1 ? isl_ineq_adj_ineq : isl_ineq_separate;
3569 /* Check the effect of inequality "ineq" on the tableau "tab".
3570 * The result may be
3571 * isl_ineq_redundant: satisfied by all points in the tableau
3572 * isl_ineq_separate: satisfied by no point in the tableau
3573 * isl_ineq_cut: satisfied by some by not all points
3574 * isl_ineq_adj_eq: adjacent to an equality
3575 * isl_ineq_adj_ineq: adjacent to an inequality.
3577 enum isl_ineq_type isl_tab_ineq_type(struct isl_tab *tab, isl_int *ineq)
3579 enum isl_ineq_type type = isl_ineq_error;
3580 struct isl_tab_undo *snap = NULL;
3581 int con;
3582 int row;
3584 if (!tab)
3585 return isl_ineq_error;
3587 if (isl_tab_extend_cons(tab, 1) < 0)
3588 return isl_ineq_error;
3590 snap = isl_tab_snap(tab);
3592 con = isl_tab_add_row(tab, ineq);
3593 if (con < 0)
3594 goto error;
3596 row = tab->con[con].index;
3597 if (isl_tab_row_is_redundant(tab, row))
3598 type = isl_ineq_redundant;
3599 else if (isl_int_is_neg(tab->mat->row[row][1]) &&
3600 (tab->rational ||
3601 isl_int_abs_ge(tab->mat->row[row][1],
3602 tab->mat->row[row][0]))) {
3603 int nonneg = at_least_zero(tab, &tab->con[con]);
3604 if (nonneg < 0)
3605 goto error;
3606 if (nonneg)
3607 type = isl_ineq_cut;
3608 else
3609 type = separation_type(tab, row);
3610 } else {
3611 int red = con_is_redundant(tab, &tab->con[con]);
3612 if (red < 0)
3613 goto error;
3614 if (!red)
3615 type = isl_ineq_cut;
3616 else
3617 type = isl_ineq_redundant;
3620 if (isl_tab_rollback(tab, snap))
3621 return isl_ineq_error;
3622 return type;
3623 error:
3624 return isl_ineq_error;
3627 int isl_tab_track_bmap(struct isl_tab *tab, __isl_take isl_basic_map *bmap)
3629 bmap = isl_basic_map_cow(bmap);
3630 if (!tab || !bmap)
3631 goto error;
3633 if (tab->empty) {
3634 bmap = isl_basic_map_set_to_empty(bmap);
3635 if (!bmap)
3636 goto error;
3637 tab->bmap = bmap;
3638 return 0;
3641 isl_assert(tab->mat->ctx, tab->n_eq == bmap->n_eq, goto error);
3642 isl_assert(tab->mat->ctx,
3643 tab->n_con == bmap->n_eq + bmap->n_ineq, goto error);
3645 tab->bmap = bmap;
3647 return 0;
3648 error:
3649 isl_basic_map_free(bmap);
3650 return -1;
3653 int isl_tab_track_bset(struct isl_tab *tab, __isl_take isl_basic_set *bset)
3655 return isl_tab_track_bmap(tab, (isl_basic_map *)bset);
3658 __isl_keep isl_basic_set *isl_tab_peek_bset(struct isl_tab *tab)
3660 if (!tab)
3661 return NULL;
3663 return (isl_basic_set *)tab->bmap;
3666 static void isl_tab_print_internal(__isl_keep struct isl_tab *tab,
3667 FILE *out, int indent)
3669 unsigned r, c;
3670 int i;
3672 if (!tab) {
3673 fprintf(out, "%*snull tab\n", indent, "");
3674 return;
3676 fprintf(out, "%*sn_redundant: %d, n_dead: %d", indent, "",
3677 tab->n_redundant, tab->n_dead);
3678 if (tab->rational)
3679 fprintf(out, ", rational");
3680 if (tab->empty)
3681 fprintf(out, ", empty");
3682 fprintf(out, "\n");
3683 fprintf(out, "%*s[", indent, "");
3684 for (i = 0; i < tab->n_var; ++i) {
3685 if (i)
3686 fprintf(out, (i == tab->n_param ||
3687 i == tab->n_var - tab->n_div) ? "; "
3688 : ", ");
3689 fprintf(out, "%c%d%s", tab->var[i].is_row ? 'r' : 'c',
3690 tab->var[i].index,
3691 tab->var[i].is_zero ? " [=0]" :
3692 tab->var[i].is_redundant ? " [R]" : "");
3694 fprintf(out, "]\n");
3695 fprintf(out, "%*s[", indent, "");
3696 for (i = 0; i < tab->n_con; ++i) {
3697 if (i)
3698 fprintf(out, ", ");
3699 fprintf(out, "%c%d%s", tab->con[i].is_row ? 'r' : 'c',
3700 tab->con[i].index,
3701 tab->con[i].is_zero ? " [=0]" :
3702 tab->con[i].is_redundant ? " [R]" : "");
3704 fprintf(out, "]\n");
3705 fprintf(out, "%*s[", indent, "");
3706 for (i = 0; i < tab->n_row; ++i) {
3707 const char *sign = "";
3708 if (i)
3709 fprintf(out, ", ");
3710 if (tab->row_sign) {
3711 if (tab->row_sign[i] == isl_tab_row_unknown)
3712 sign = "?";
3713 else if (tab->row_sign[i] == isl_tab_row_neg)
3714 sign = "-";
3715 else if (tab->row_sign[i] == isl_tab_row_pos)
3716 sign = "+";
3717 else
3718 sign = "+-";
3720 fprintf(out, "r%d: %d%s%s", i, tab->row_var[i],
3721 isl_tab_var_from_row(tab, i)->is_nonneg ? " [>=0]" : "", sign);
3723 fprintf(out, "]\n");
3724 fprintf(out, "%*s[", indent, "");
3725 for (i = 0; i < tab->n_col; ++i) {
3726 if (i)
3727 fprintf(out, ", ");
3728 fprintf(out, "c%d: %d%s", i, tab->col_var[i],
3729 var_from_col(tab, i)->is_nonneg ? " [>=0]" : "");
3731 fprintf(out, "]\n");
3732 r = tab->mat->n_row;
3733 tab->mat->n_row = tab->n_row;
3734 c = tab->mat->n_col;
3735 tab->mat->n_col = 2 + tab->M + tab->n_col;
3736 isl_mat_print_internal(tab->mat, out, indent);
3737 tab->mat->n_row = r;
3738 tab->mat->n_col = c;
3739 if (tab->bmap)
3740 isl_basic_map_print_internal(tab->bmap, out, indent);
3743 void isl_tab_dump(__isl_keep struct isl_tab *tab)
3745 isl_tab_print_internal(tab, stderr, 0);